Wed Aug 11 01:07:36 2010

Asterisk developer's documentation


cli.c File Reference

Standard Command Line Interface. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include <sys/signal.h>
#include <signal.h>
#include <ctype.h>
#include <regex.h>
#include <pwd.h>
#include <grp.h>
#include "asterisk/cli.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/lock.h"
#include "editline/readline/readline.h"
#include "asterisk/threadstorage.h"

Include dependency graph for cli.c:

Go to the source code of this file.

Data Structures

struct  ast_debug_file
 map a debug or verbose value to a filename More...
struct  cli_perm
 List of restrictions per user. More...
struct  cli_perm_head
struct  cli_perms
 List of users and permissions. More...
struct  debug_file_list
struct  helpers
struct  usergroup_cli_perm
 list of users to apply restrictions. More...

Defines

#define AST_CLI_INITLEN   256
 Initial buffer size for resulting strings in ast_cli().
#define CONCISE_FORMAT_STRING   "%s!%s!%s!%d!%s!%s!%s!%s!%s!%d!%s!%s!%s\n"
#define DAY   (HOUR*24)
#define FORMAT_STRING   "%-25s %-20s %-20s\n"
#define FORMAT_STRING   "%-20.20s %-20.20s %-7.7s %-30.30s\n"
#define FORMAT_STRING2   "%-20.20s %-20.20s %-7.7s %-30.30s\n"
#define HOUR   (MINUTE*60)
#define MINUTE   (SECOND*60)
#define MODLIST_FORMAT   "%-30s %-40.40s %-10d\n"
#define MODLIST_FORMAT2   "%-30s %-40.40s %-10s\n"
#define NEEDCOMMA(x)   ((x)? ",": "")
#define SECOND   (1)
#define VERBOSE_FORMAT_STRING   "%-20.20s %-20.20s %-16.16s %4d %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"
#define VERBOSE_FORMAT_STRING2   "%-20.20s %-20.20s %-16.16s %-4.4s %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"
#define WEEK   (DAY*7)
#define YEAR   (DAY*365)

Functions

static char * __ast_cli_generator (const char *text, const char *word, int state, int lock)
static int __ast_cli_register (struct ast_cli_entry *e, struct ast_cli_entry *ed)
static int __ast_cli_unregister (struct ast_cli_entry *e, struct ast_cli_entry *ed)
static void __init_ast_cli_buf (void)
void ast_builtins_init (void)
 initialize the _full_cmd string in * each of the builtins.
void ast_cli (int fd, const char *fmt,...)
int ast_cli_command_full (int uid, int gid, int fd, const char *s)
 Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run this command. uid = CLI_NO_PERMS to avoid checking user permissions gid = CLI_NO_PERMS to avoid checking group permissions.
int ast_cli_command_multiple_full (int uid, int gid, int fd, size_t size, const char *s)
 Executes multiple CLI commands Interpret strings separated by NULL and execute each one, sending output to fd if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions. gid = CLI_NO_PERMS to avoid checking group permissions.
char * ast_cli_complete (const char *word, char *const choices[], int state)
char ** ast_cli_completion_matches (const char *text, const char *word)
 Generates a NULL-terminated array of strings that 1) begin with the string in the second parameter, and 2) are valid in a command after the string in the first parameter.
char * ast_cli_generator (const char *text, const char *word, int state)
 Readline madness Useful for readline, that's about it.
int ast_cli_generatornummatches (const char *text, const char *word)
 Return the number of unique matches for the generator.
int ast_cli_perms_init (int reload)
int ast_cli_register (struct ast_cli_entry *e)
 Registers a command or an array of commands.
int ast_cli_register_multiple (struct ast_cli_entry *e, int len)
 Register multiple commands.
int ast_cli_unregister (struct ast_cli_entry *e)
 Unregisters a command or an array of commands.
int ast_cli_unregister_multiple (struct ast_cli_entry *e, int len)
 Unregister multiple commands.
char * ast_complete_channels (const char *line, const char *word, int pos, int state, int rpos)
 Command completion for the list of active channels.
unsigned int ast_debug_get_by_file (const char *file)
 Get the debug level for a file.
unsigned int ast_verbose_get_by_file (const char *file)
 Get the debug level for a file.
static int cli_has_permissions (int uid, int gid, const char *command)
static struct ast_cli_entry * cli_next (struct ast_cli_entry *e)
static char * complete_fn (const char *word, int state)
static char * complete_number (const char *partial, unsigned int min, unsigned int max, int n)
static void destroy_user_perms (void)
 cleanup (free) cli_perms linkedlist.
static char * find_best (char *argv[])
static struct ast_cli_entry * find_cli (char *const cmds[], int match_type)
static struct ast_debug_file * find_debug_file (const char *fn, unsigned int debug)
 Find the debug or verbose file setting.
static char * group_show_channels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_chanlist (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_check_permissions (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 handles CLI command 'cli check permissions'
static char * handle_cli_reload_permissions (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 handles CLI command 'cli reload permissions'
static char * handle_cli_show_permissions (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 handles CLI command 'cli show permissions'
static char * handle_cli_wait_fullybooted (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_commandcomplete (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_commandmatchesarray (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_commandnummatches (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_core_set_debug_channel (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_help (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_load (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_logger_mute (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_modlist (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_nodebugchan_deprecated (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showcalls (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showchan (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_showuptime (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_softhangup (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_unload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_verbose (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * help1 (int fd, char *match[], int locked)
 helper for final part of handle_help if locked = 1, assume the list is already locked
static char * is_prefix (const char *word, const char *token, int pos, int *actual)
 if word is a valid prefix for token, returns the pos-th match as a malloced string, or NULL otherwise. Always tell in *actual how many matches we got.
static int modlist_modentry (const char *module, const char *description, int usecnt, const char *like)
static int more_words (char *const *dst)
 returns true if there are more words to match
static char * parse_args (const char *s, int *argc, char *argv[], int max, int *trailingwhitespace)
static void print_uptimestr (int fd, struct timeval timeval, const char *prefix, int printsec)
static int set_full_cmd (struct ast_cli_entry *e)
static int word_match (const char *cmd, const char *cli_word)

Variables

static struct ast_threadstorage ast_cli_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_ast_cli_buf , .custom_init = NULL , }
static struct ast_cli_entry cli_cli []
static int cli_default_perm = 1
 Default permissions value 1=Permit 0=Deny.
static const char cli_rsvd [] = "[]{}|*%"
static int climodentryfd = -1
static ast_mutex_t climodentrylock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP )
static struct debug_file_list debug_files
static const char perms_config [] = "cli_permissions.conf"
 CLI permissions config file.
static ast_mutex_t permsconfiglock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP )
 mutex used to prevent a user from running the 'cli reload permissions' command while it is already running.
static struct debug_file_list verbose_files


Detailed Description

Standard Command Line Interface.

Author:
Mark Spencer <markster@digium.com>

Definition in file cli.c.


Define Documentation

#define AST_CLI_INITLEN   256

Initial buffer size for resulting strings in ast_cli().

Definition at line 98 of file cli.c.

Referenced by ast_cli().

#define CONCISE_FORMAT_STRING   "%s!%s!%s!%d!%s!%s!%s!%s!%s!%d!%s!%s!%s\n"

Referenced by handle_chanlist().

#define DAY   (HOUR*24)

Referenced by print_uptimestr().

#define FORMAT_STRING   "%-25s %-20s %-20s\n"

#define FORMAT_STRING   "%-20.20s %-20.20s %-7.7s %-30.30s\n"

#define FORMAT_STRING2   "%-20.20s %-20.20s %-7.7s %-30.30s\n"

Referenced by handle_chanlist().

#define HOUR   (MINUTE*60)

Referenced by print_uptimestr().

#define MINUTE   (SECOND*60)

Referenced by print_uptimestr().

#define MODLIST_FORMAT   "%-30s %-40.40s %-10d\n"

Definition at line 592 of file cli.c.

Referenced by modlist_modentry().

#define MODLIST_FORMAT2   "%-30s %-40.40s %-10s\n"

Definition at line 593 of file cli.c.

Referenced by handle_modlist().

#define NEEDCOMMA ( x   )     ((x)? ",": "")

Referenced by print_uptimestr().

#define SECOND   (1)

#define VERBOSE_FORMAT_STRING   "%-20.20s %-20.20s %-16.16s %4d %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"

Referenced by handle_chanlist().

#define VERBOSE_FORMAT_STRING2   "%-20.20s %-20.20s %-16.16s %-4.4s %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"

Referenced by handle_chanlist().

#define WEEK   (DAY*7)

Referenced by print_uptimestr().

#define YEAR   (DAY*365)

Referenced by print_uptimestr().


Function Documentation

static char * __ast_cli_generator ( const char *  text,
const char *  word,
int  state,
int  lock 
) [static]

Definition at line 2197 of file cli.c.

References ARRAY_LEN, ast_free, ast_join(), AST_MAX_ARGS, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_strlen_zero(), CLI_GENERATE, cli_next(), ast_cli_entry::cmda, ast_cli_entry::command, ast_cli_entry::handler, is_prefix(), ast_cli_args::line, more_words(), parse_args(), and word_match().

Referenced by ast_cli_generator(), handle_commandcomplete(), and handle_help().

02198 {
02199    char *argv[AST_MAX_ARGS];
02200    struct ast_cli_entry *e = NULL;
02201    int x = 0, argindex, matchlen;
02202    int matchnum=0;
02203    char *ret = NULL;
02204    char matchstr[80] = "";
02205    int tws = 0;
02206    /* Split the argument into an array of words */
02207    char *duplicate = parse_args(text, &x, argv, ARRAY_LEN(argv), &tws);
02208 
02209    if (!duplicate)   /* malloc error */
02210       return NULL;
02211 
02212    /* Compute the index of the last argument (could be an empty string) */
02213    argindex = (!ast_strlen_zero(word) && x>0) ? x-1 : x;
02214 
02215    /* rebuild the command, ignore terminating white space and flatten space */
02216    ast_join(matchstr, sizeof(matchstr)-1, argv);
02217    matchlen = strlen(matchstr);
02218    if (tws) {
02219       strcat(matchstr, " "); /* XXX */
02220       if (matchlen)
02221          matchlen++;
02222    }
02223    if (lock)
02224       AST_RWLIST_RDLOCK(&helpers);
02225    while ( (e = cli_next(e)) ) {
02226       /* XXX repeated code */
02227       int src = 0, dst = 0, n = 0;
02228 
02229       if (e->command[0] == '_')
02230          continue;
02231 
02232       /*
02233        * Try to match words, up to and excluding the last word, which
02234        * is either a blank or something that we want to extend.
02235        */
02236       for (;src < argindex; dst++, src += n) {
02237          n = word_match(argv[src], e->cmda[dst]);
02238          if (n < 0)
02239             break;
02240       }
02241 
02242       if (src != argindex && more_words(e->cmda + dst))  /* not a match */
02243          continue;
02244       ret = is_prefix(argv[src], e->cmda[dst], state - matchnum, &n);
02245       matchnum += n; /* this many matches here */
02246       if (ret) {
02247          /*
02248           * argv[src] is a valid prefix of the next word in this
02249           * command. If this is also the correct entry, return it.
02250           */
02251          if (matchnum > state)
02252             break;
02253          ast_free(ret);
02254          ret = NULL;
02255       } else if (ast_strlen_zero(e->cmda[dst])) {
02256          /*
02257           * This entry is a prefix of the command string entered
02258           * (only one entry in the list should have this property).
02259           * Run the generator if one is available. In any case we are done.
02260           */
02261          if (e->handler) { /* new style command */
02262             struct ast_cli_args a = {
02263                .line = matchstr, .word = word,
02264                .pos = argindex,
02265                .n = state - matchnum,
02266                .argv = argv,
02267                .argc = x};
02268             ret = e->handler(e, CLI_GENERATE, &a);
02269          }
02270          if (ret)
02271             break;
02272       }
02273    }
02274    if (lock)
02275       AST_RWLIST_UNLOCK(&helpers);
02276    ast_free(duplicate);
02277    return ret;
02278 }

static int __ast_cli_register ( struct ast_cli_entry *  e,
struct ast_cli_entry *  ed 
) [static]

Definition at line 1884 of file cli.c.

References ast_cli_entry::_full_cmd, ast_log(), AST_MAX_CMD_LEN, AST_RWLIST_INSERT_BEFORE_CURRENT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_skip_blanks(), ast_skip_nonblanks(), ast_strdup, ast_strlen_zero(), CLI_INIT, ast_cli_entry::cmda, ast_cli_entry::cmdlen, ast_cli_entry::command, find_cli(), ast_cli_entry::handler, len(), LOG_WARNING, s, S_OR, and set_full_cmd().

Referenced by ast_cli_register().

01885 {
01886    struct ast_cli_entry *cur;
01887    int i, lf, ret = -1;
01888 
01889    struct ast_cli_args a;  /* fake argument */
01890    char **dst = (char **)e->cmda;   /* need to cast as the entry is readonly */
01891    char *s;
01892 
01893    memset(&a, '\0', sizeof(a));
01894    e->handler(e, CLI_INIT, &a);
01895    /* XXX check that usage and command are filled up */
01896    s = ast_skip_blanks(e->command);
01897    s = e->command = ast_strdup(s);
01898    for (i=0; !ast_strlen_zero(s) && i < AST_MAX_CMD_LEN-1; i++) {
01899       *dst++ = s; /* store string */
01900       s = ast_skip_nonblanks(s);
01901       if (*s == '\0')   /* we are done */
01902          break;
01903       *s++ = '\0';
01904       s = ast_skip_blanks(s);
01905    }
01906    *dst++ = NULL;
01907    
01908    AST_RWLIST_WRLOCK(&helpers);
01909    
01910    if (find_cli(e->cmda, 1)) {
01911       ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", S_OR(e->_full_cmd, e->command));
01912       goto done;
01913    }
01914    if (set_full_cmd(e))
01915       goto done;
01916 
01917    lf = e->cmdlen;
01918    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&helpers, cur, list) {
01919       int len = cur->cmdlen;
01920       if (lf < len)
01921          len = lf;
01922       if (strncasecmp(e->_full_cmd, cur->_full_cmd, len) < 0) {
01923          AST_RWLIST_INSERT_BEFORE_CURRENT(e, list); 
01924          break;
01925       }
01926    }
01927    AST_RWLIST_TRAVERSE_SAFE_END;
01928 
01929    if (!cur)
01930       AST_RWLIST_INSERT_TAIL(&helpers, e, list); 
01931    ret = 0; /* success */
01932 
01933 done:
01934    AST_RWLIST_UNLOCK(&helpers);
01935 
01936    return ret;
01937 }

static int __ast_cli_unregister ( struct ast_cli_entry *  e,
struct ast_cli_entry *  ed 
) [static]

Definition at line 1862 of file cli.c.

References ast_cli_entry::_full_cmd, ast_free, ast_log(), AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_cli_entry::cmda, ast_cli_entry::command, ast_cli_entry::handler, ast_cli_entry::inuse, LOG_WARNING, and ast_cli_entry::usage.

Referenced by ast_cli_unregister().

01863 {
01864    if (e->inuse) {
01865       ast_log(LOG_WARNING, "Can't remove command that is in use\n");
01866    } else {
01867       AST_RWLIST_WRLOCK(&helpers);
01868       AST_RWLIST_REMOVE(&helpers, e, list);
01869       AST_RWLIST_UNLOCK(&helpers);
01870       ast_free(e->_full_cmd);
01871       e->_full_cmd = NULL;
01872       if (e->handler) {
01873          /* this is a new-style entry. Reset fields and free memory. */
01874          char *cmda = (char *) e->cmda;
01875          memset(cmda, '\0', sizeof(e->cmda));
01876          ast_free(e->command);
01877          e->command = NULL;
01878          e->usage = NULL;
01879       }
01880    }
01881    return 0;
01882 }

static void __init_ast_cli_buf ( void   )  [static]

Definition at line 95 of file cli.c.

00101 {

void ast_builtins_init ( void   ) 

initialize the _full_cmd string in * each of the builtins.

Provided by cli.c

Definition at line 1704 of file cli.c.

References ARRAY_LEN, and ast_cli_register_multiple().

Referenced by main().

void ast_cli ( int  fd,
const char *  fmt,
  ... 
)

Definition at line 100 of file cli.c.

References ast_carefulwrite(), AST_CLI_INITLEN, AST_DYNSTR_BUILD_FAILED, ast_str_buffer(), ast_str_set_va(), ast_str_strlen(), ast_str_thread_get(), and buf.

Referenced by __iax2_show_peers(), __say_cli_init(), _sip_qualify_peer(), _sip_show_peer(), _sip_show_peers(), _skinny_show_device(), _skinny_show_devices(), _skinny_show_line(), _skinny_show_lines(), agent_logoff_cmd(), agents_show(), agents_show_online(), ais_clm_show_members(), ais_evt_show_event_channels(), aji_do_reload(), aji_do_set_debug(), aji_show_buddies(), aji_show_clients(), aji_test(), alias_show(), ast_cli_command_full(), ast_cli_netstats(), ast_console_toggle_mute(), cli_console_active(), cli_console_answer(), cli_console_autoanswer(), cli_console_dial(), cli_console_flash(), cli_console_hangup(), cli_console_mute(), cli_console_sendtext(), cli_list_available(), cli_list_devices(), cli_match_char_tree(), cli_odbc_read(), cli_odbc_write(), cli_realtime_destroy(), cli_realtime_load(), cli_realtime_store(), cli_realtime_update(), cli_realtime_update2(), cli_tps_ping(), cli_tps_report(), console_active(), console_answer(), console_autoanswer(), console_boost(), console_cmd(), console_dial(), console_do_answer(), console_flash(), console_hangup(), console_mute(), console_sendtext(), console_transfer(), dahdi_set_dnd(), dahdi_set_hwgain(), dahdi_set_swgain(), dahdi_show_channel(), dahdi_show_channels(), dahdi_show_status(), dahdi_show_version(), dialog_dump_func(), do_print(), dundi_do_lookup(), dundi_do_precache(), dundi_do_query(), dundi_flush(), dundi_set_debug(), dundi_show_entityid(), dundi_show_mappings(), dundi_show_peer(), dundi_show_peers(), dundi_show_precache(), dundi_show_requests(), dundi_show_trans(), dundi_store_history(), group_show_channels(), gtalk_show_channels(), handle_chanlist(), handle_cli_agi_debug(), handle_cli_agi_dump_html(), handle_cli_agi_show(), handle_cli_check_permissions(), handle_cli_config_list(), handle_cli_core_show_channeltype(), handle_cli_core_show_channeltypes(), handle_cli_core_show_config_mappings(), handle_cli_core_show_file_formats(), handle_cli_core_show_translation(), handle_cli_database_del(), handle_cli_database_deltree(), handle_cli_database_get(), handle_cli_database_put(), handle_cli_database_show(), handle_cli_database_showkey(), handle_cli_devstate_change(), handle_cli_devstate_list(), handle_cli_dialplan_add_extension(), handle_cli_dialplan_add_ignorepat(), handle_cli_dialplan_add_include(), handle_cli_dialplan_reload(), handle_cli_dialplan_remove_extension(), handle_cli_dialplan_remove_ignorepat(), handle_cli_dialplan_remove_include(), handle_cli_dialplan_save(), handle_cli_file_convert(), handle_cli_h323_set_debug(), handle_cli_h323_set_trace(), handle_cli_iax2_provision(), handle_cli_iax2_prune_realtime(), handle_cli_iax2_set_debug(), handle_cli_iax2_set_debug_jb(), handle_cli_iax2_set_debug_trunk(), handle_cli_iax2_set_mtu(), handle_cli_iax2_show_cache(), handle_cli_iax2_show_callno_limits(), handle_cli_iax2_show_channels(), handle_cli_iax2_show_firmware(), handle_cli_iax2_show_netstats(), handle_cli_iax2_show_peer(), handle_cli_iax2_show_registry(), handle_cli_iax2_show_stats(), handle_cli_iax2_show_threads(), handle_cli_iax2_show_users(), handle_cli_iax2_unregister(), handle_cli_indication_show(), handle_cli_keys_show(), handle_cli_misdn_reload(), handle_cli_misdn_send_digit(), handle_cli_misdn_send_display(), handle_cli_misdn_set_debug(), handle_cli_misdn_show_channels(), handle_cli_misdn_show_config(), handle_cli_misdn_show_port(), handle_cli_misdn_show_ports_stats(), handle_cli_misdn_show_stacks(), handle_cli_misdn_toggle_echocancel(), handle_cli_mixmonitor(), handle_cli_moh_show_classes(), handle_cli_moh_show_files(), handle_cli_odbc_show(), handle_cli_osp_show(), handle_cli_realtime_pgsql_cache(), handle_cli_realtime_pgsql_status(), handle_cli_refresh(), handle_cli_rtcp_set_debug(), handle_cli_rtcp_set_stats(), handle_cli_rtp_set_debug(), handle_cli_show_permissions(), handle_cli_show_sqlite_status(), handle_cli_sqlite_show_tables(), handle_cli_status(), handle_cli_stun_set_debug(), handle_cli_submit(), handle_cli_transcoder_show(), handle_cli_udptl_set_debug(), handle_cli_ulimit(), handle_cli_wait_fullybooted(), handle_commandcomplete(), handle_commandmatchesarray(), handle_commandnummatches(), handle_core_set_debug_channel(), handle_core_show_image_formats(), handle_dahdi_show_cadences(), handle_debug_dialplan(), handle_feature_show(), handle_help(), handle_load(), handle_logger_reload(), handle_logger_rotate(), handle_logger_set_level(), handle_logger_show_channels(), handle_mandebug(), handle_mfcr2_call_files(), handle_mfcr2_set_blocked(), handle_mfcr2_set_debug(), handle_mfcr2_set_idle(), handle_mfcr2_show_channels(), handle_mfcr2_show_variants(), handle_mfcr2_version(), handle_mgcp_audit_endpoint(), handle_mgcp_set_debug(), handle_mgcp_show_endpoints(), handle_minivm_list_templates(), handle_minivm_reload(), handle_minivm_show_settings(), handle_minivm_show_stats(), handle_minivm_show_users(), handle_minivm_show_zones(), handle_modlist(), handle_parkedcalls(), handle_pri_debug(), handle_pri_set_debug_file(), handle_pri_show_debug(), handle_pri_show_span(), handle_pri_show_spans(), handle_pri_version(), handle_queue_add_member(), handle_queue_pause_member(), handle_queue_remove_member(), handle_queue_rule_show(), handle_queue_set_member_penalty(), handle_redirect(), handle_reload(), handle_restart_when_convenient(), handle_set_chanvar(), handle_set_extenpatternmatchnew(), handle_set_global(), handle_show_application(), handle_show_applications(), handle_show_chanvar(), handle_show_dialplan(), handle_show_function(), handle_show_functions(), handle_show_globals(), handle_show_hint(), handle_show_hints(), handle_show_http(), handle_show_profile(), handle_show_routes(), handle_show_settings(), handle_show_switches(), handle_show_sysinfo(), handle_show_threads(), handle_show_version_files(), handle_showcalls(), handle_showchan(), handle_showmanager(), handle_showmanagers(), handle_showmancmd(), handle_showmancmds(), handle_showmanconn(), handle_showmaneventq(), handle_skinny_set_debug(), handle_skinny_show_settings(), handle_softhangup(), handle_ss7_block_cic(), handle_ss7_block_linkset(), handle_ss7_debug(), handle_ss7_show_linkset(), handle_ss7_unblock_cic(), handle_ss7_unblock_linkset(), handle_ss7_version(), handle_stop_when_convenient(), handle_unload(), handle_unset_extenpatternmatchnew(), handle_verbose(), handle_version(), handle_voicemail_reload(), handle_voicemail_show_users(), handle_voicemail_show_zones(), help1(), help_workhorse(), iax_show_provisioning(), jingle_show_channels(), locals_show(), meetme_cmd(), meetme_show_cmd(), modlist_modentry(), orig_app(), orig_exten(), peer_dump_func(), print_app_docs(), print_bc_info(), print_codec_to_cli(), print_group(), print_uptimestr(), radio_active(), radio_set_debug(), radio_set_debug_off(), radio_set_xpmr_debug(), radio_tune(), realtime_ldap_status(), rpt_do_cmd(), rpt_do_debug(), rpt_do_dump(), rpt_do_fun(), rpt_do_local_nodes(), rpt_do_lstats(), rpt_do_nodes(), rpt_do_stats(), rtcp_do_debug_ip(), rtp_do_debug_ip(), show_channels_cb(), show_chanstats_cb(), show_codec_n(), show_codecs(), show_config_description(), show_debug_helper(), show_dialplan_helper(), show_license(), show_users_realtime(), show_warranty(), sip_cli_notify(), sip_do_debug(), sip_do_debug_ip(), sip_do_debug_peer(), sip_prune_realtime(), sip_set_history(), sip_show_channel(), sip_show_channels(), sip_show_channelstats(), sip_show_domains(), sip_show_history(), sip_show_inuse(), sip_show_mwi(), sip_show_objects(), sip_show_registry(), sip_show_sched(), sip_show_settings(), sip_show_tcp(), sip_show_user(), sip_show_users(), sip_unregister(), sla_show_stations(), sla_show_trunks(), timing_test(), tune_rxctcss(), tune_rxinput(), tune_rxvoice(), tune_txoutput(), unistim_do_debug(), unistim_info(), and unistim_sp().

00101 {
00102    int res;
00103    struct ast_str *buf;
00104    va_list ap;
00105 
00106    if (!(buf = ast_str_thread_get(&ast_cli_buf, AST_CLI_INITLEN)))
00107       return;
00108 
00109    va_start(ap, fmt);
00110    res = ast_str_set_va(&buf, 0, fmt, ap);
00111    va_end(ap);
00112 
00113    if (res != AST_DYNSTR_BUILD_FAILED) {
00114       ast_carefulwrite(fd, ast_str_buffer(buf), ast_str_strlen(buf), 100);
00115    }
00116 }

int ast_cli_command_full ( int  uid,
int  gid,
int  fd,
const char *  s 
)

Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run this command. uid = CLI_NO_PERMS to avoid checking user permissions gid = CLI_NO_PERMS to avoid checking group permissions.

Parameters:
uid User ID that is trying to run the command.
gid Group ID that is trying to run the command.
fd pipe
s incoming string
Return values:
0 on success
-1 on failure

Definition at line 2285 of file cli.c.

References ast_atomic_fetchadd_int(), ast_cli(), ast_free, ast_join(), AST_MAX_ARGS, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, CLI_FAILURE, CLI_HANDLER, cli_has_permissions(), CLI_SHOWUSAGE, ast_cli_args::fd, find_best(), find_cli(), ast_cli_entry::handler, ast_cli_entry::inuse, parse_args(), S_OR, and ast_cli_entry::usage.

Referenced by ast_cli_command_multiple_full().

02286 {
02287    char *args[AST_MAX_ARGS + 1];
02288    struct ast_cli_entry *e;
02289    int x;
02290    char *duplicate = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
02291    char tmp[AST_MAX_ARGS + 1];
02292    char *retval = NULL;
02293    struct ast_cli_args a = {
02294       .fd = fd, .argc = x, .argv = args+1 };
02295 
02296    if (duplicate == NULL)
02297       return -1;
02298 
02299    if (x < 1)  /* We need at least one entry, otherwise ignore */
02300       goto done;
02301 
02302    AST_RWLIST_RDLOCK(&helpers);
02303    e = find_cli(args + 1, 0);
02304    if (e)
02305       ast_atomic_fetchadd_int(&e->inuse, 1);
02306    AST_RWLIST_UNLOCK(&helpers);
02307    if (e == NULL) {
02308       ast_cli(fd, "No such command '%s' (type 'core show help %s' for other possible commands)\n", s, find_best(args + 1));
02309       goto done;
02310    }
02311 
02312    ast_join(tmp, sizeof(tmp), args + 1);
02313    /* Check if the user has rights to run this command. */
02314    if (!cli_has_permissions(uid, gid, tmp)) {
02315       ast_cli(fd, "You don't have permissions to run '%s' command\n", tmp);
02316       ast_free(duplicate);
02317       return 0;
02318    }
02319 
02320    /*
02321     * Within the handler, argv[-1] contains a pointer to the ast_cli_entry.
02322     * Remember that the array returned by parse_args is NULL-terminated.
02323     */
02324    args[0] = (char *)e;
02325 
02326    retval = e->handler(e, CLI_HANDLER, &a);
02327 
02328    if (retval == CLI_SHOWUSAGE) {
02329       ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
02330    } else {
02331       if (retval == CLI_FAILURE)
02332          ast_cli(fd, "Command '%s' failed.\n", s);
02333    }
02334    ast_atomic_fetchadd_int(&e->inuse, -1);
02335 done:
02336    ast_free(duplicate);
02337    return 0;
02338 }

int ast_cli_command_multiple_full ( int  uid,
int  gid,
int  fd,
size_t  size,
const char *  s 
)

Executes multiple CLI commands Interpret strings separated by NULL and execute each one, sending output to fd if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions. gid = CLI_NO_PERMS to avoid checking group permissions.

Parameters:
uid User ID that is trying to run the command.
gid Group ID that is trying to run the command.
fd pipe
size is the total size of the string
s incoming string
Return values:
number of commands executed

Definition at line 2340 of file cli.c.

References ast_cli_command_full().

Referenced by netconsole().

02341 {
02342    char cmd[512];
02343    int x, y = 0, count = 0;
02344 
02345    for (x = 0; x < size; x++) {
02346       cmd[y] = s[x];
02347       y++;
02348       if (s[x] == '\0') {
02349          ast_cli_command_full(uid, gid, fd, cmd);
02350          y = 0;
02351          count++;
02352       }
02353    }
02354    return count;
02355 }

char* ast_cli_complete ( const char *  word,
char *const   choices[],
int  pos 
)

Helper function to generate cli entries from a NULL-terminated array. Returns the n-th matching entry from the array, or NULL if not found. Can be used to implement generate() for static entries as below (in this example we complete the word in position 2):

    char *my_generate(const char *line, const char *word, int pos, int n)
    {
        static char *choices = { "one", "two", "three", NULL };
   if (pos == 2)
         return ast_cli_complete(word, choices, n);
   else
      return NULL;
    }

Definition at line 1381 of file cli.c.

References ast_strdup, ast_strlen_zero(), and len().

Referenced by complete_meetmecmd(), handle_cli_devstate_change(), handle_cli_iax2_prune_realtime(), handle_orig(), handle_show_applications(), and sip_prune_realtime().

01382 {
01383    int i, which = 0, len;
01384    len = ast_strlen_zero(word) ? 0 : strlen(word);
01385 
01386    for (i = 0; choices[i]; i++) {
01387       if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state)
01388          return ast_strdup(choices[i]);
01389    }
01390    return NULL;
01391 }

char** ast_cli_completion_matches ( const char *  ,
const char *   
)

Generates a NULL-terminated array of strings that 1) begin with the string in the second parameter, and 2) are valid in a command after the string in the first parameter.

The first entry (offset 0) of the result is the longest common substring in the results, useful to extend the string that has been completed. Subsequent entries are all possible values, followed by a NULL. All strings and the array itself are malloc'ed and must be freed by the caller.

Definition at line 2136 of file cli.c.

References ast_cli_generator(), ast_copy_string(), ast_malloc, and ast_realloc.

Referenced by cli_complete(), and handle_commandmatchesarray().

02137 {
02138    char **match_list = NULL, *retstr, *prevstr;
02139    size_t match_list_len, max_equal, which, i;
02140    int matches = 0;
02141 
02142    /* leave entry 0 free for the longest common substring */
02143    match_list_len = 1;
02144    while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
02145       if (matches + 1 >= match_list_len) {
02146          match_list_len <<= 1;
02147          if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
02148             return NULL;
02149       }
02150       match_list[++matches] = retstr;
02151    }
02152 
02153    if (!match_list)
02154       return match_list; /* NULL */
02155 
02156    /* Find the longest substring that is common to all results
02157     * (it is a candidate for completion), and store a copy in entry 0.
02158     */
02159    prevstr = match_list[1];
02160    max_equal = strlen(prevstr);
02161    for (which = 2; which <= matches; which++) {
02162       for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
02163          continue;
02164       max_equal = i;
02165    }
02166 
02167    if (!(retstr = ast_malloc(max_equal + 1)))
02168       return NULL;
02169    
02170    ast_copy_string(retstr, match_list[1], max_equal + 1);
02171    match_list[0] = retstr;
02172 
02173    /* ensure that the array is NULL terminated */
02174    if (matches + 1 >= match_list_len) {
02175       if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list))))
02176          return NULL;
02177    }
02178    match_list[matches + 1] = NULL;
02179 
02180    return match_list;
02181 }

char* ast_cli_generator ( const char *  ,
const char *  ,
int   
)

Readline madness Useful for readline, that's about it.

Return values:
0 on success
-1 on failure

Definition at line 2280 of file cli.c.

References __ast_cli_generator().

Referenced by ast_cli_completion_matches(), ast_cli_generatornummatches(), cli_alias_passthrough(), and handle_cli_check_permissions().

02281 {
02282    return __ast_cli_generator(text, word, state, 1);
02283 }

int ast_cli_generatornummatches ( const char *  text,
const char *  word 
)

Return the number of unique matches for the generator.

Definition at line 2119 of file cli.c.

References ast_cli_generator(), ast_free, and buf.

Referenced by handle_commandnummatches().

02120 {
02121    int matches = 0, i = 0;
02122    char *buf = NULL, *oldbuf = NULL;
02123 
02124    while ((buf = ast_cli_generator(text, word, i++))) {
02125       if (!oldbuf || strcmp(buf,oldbuf))
02126          matches++;
02127       if (oldbuf)
02128          ast_free(oldbuf);
02129       oldbuf = buf;
02130    }
02131    if (oldbuf)
02132       ast_free(oldbuf);
02133    return matches;
02134 }

int ast_cli_perms_init ( int  reload  ) 

Provided by cli.c

Definition at line 1581 of file cli.c.

References ast_calloc, ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_free, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_mutex_trylock(), ast_mutex_unlock(), AST_RWLIST_INSERT_TAIL, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strdup, ast_strlen_zero(), ast_variable_browse(), cli_perm::command, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEUNCHANGED, destroy_user_perms(), usergroup_cli_perm::gid, LOG_NOTICE, LOG_WARNING, ast_variable::name, ast_variable::next, cli_perm::permit, usergroup_cli_perm::perms, perms_config, usergroup_cli_perm::uid, and ast_variable::value.

Referenced by handle_cli_reload_permissions(), and main().

01582 {
01583    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
01584    struct ast_config *cfg;
01585    char *cat = NULL;
01586    struct ast_variable *v;
01587    struct usergroup_cli_perm *user_group, *cp_entry;
01588    struct cli_perm *perm = NULL;
01589    struct passwd *pw;
01590    struct group *gr;
01591 
01592    if (ast_mutex_trylock(&permsconfiglock)) {
01593       ast_log(LOG_NOTICE, "You must wait until last 'cli reload permissions' command finish\n");
01594       return 1;
01595    }
01596 
01597    cfg = ast_config_load2(perms_config, "" /* core, can't reload */, config_flags);
01598    if (!cfg) {
01599       ast_mutex_unlock(&permsconfiglock);
01600       return 1;
01601    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
01602       ast_mutex_unlock(&permsconfiglock);
01603       return 0;
01604    }
01605 
01606    /* free current structures. */
01607    destroy_user_perms();
01608 
01609    while ((cat = ast_category_browse(cfg, cat))) {
01610       if (!strcasecmp(cat, "general")) {
01611          /* General options */
01612          for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
01613             if (!strcasecmp(v->name, "default_perm")) {
01614                cli_default_perm = (!strcasecmp(v->value, "permit")) ? 1: 0;
01615             }
01616          }
01617          continue;
01618       }
01619 
01620       /* users or groups */
01621       gr = NULL, pw = NULL;
01622       if (cat[0] == '@') {
01623          /* This is a group */
01624          gr = getgrnam(&cat[1]);
01625          if (!gr) {
01626             ast_log (LOG_WARNING, "Unknown group '%s'\n", &cat[1]);
01627             continue;
01628          }
01629       } else {
01630          /* This is a user */
01631          pw = getpwnam(cat);
01632          if (!pw) {
01633             ast_log (LOG_WARNING, "Unknown user '%s'\n", cat);
01634             continue;
01635          }
01636       }
01637       user_group = NULL;
01638       /* Check for duplicates */
01639       AST_RWLIST_WRLOCK(&cli_perms);
01640       AST_LIST_TRAVERSE(&cli_perms, cp_entry, list) {
01641          if ((pw && cp_entry->uid == pw->pw_uid) || (gr && cp_entry->gid == gr->gr_gid)) {
01642             /* if it is duplicated, just added this new settings, to 
01643             the current list. */
01644             user_group = cp_entry;
01645             break;
01646          }
01647       }
01648       AST_RWLIST_UNLOCK(&cli_perms);
01649 
01650       if (!user_group) {
01651          /* alloc space for the new user config. */
01652          user_group = ast_calloc(1, sizeof(*user_group));
01653          if (!user_group) {
01654             continue;
01655          }
01656          user_group->uid = (pw ? pw->pw_uid : -1);
01657          user_group->gid = (gr ? gr->gr_gid : -1);
01658          user_group->perms = ast_calloc(1, sizeof(*user_group->perms));
01659          if (!user_group->perms) {
01660             ast_free(user_group);
01661             continue;
01662          }
01663       }
01664       for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
01665          if (ast_strlen_zero(v->value)) {
01666             /* we need to check this condition cause it could break security. */
01667             ast_log(LOG_WARNING, "Empty permit/deny option in user '%s'\n", cat);
01668             continue;
01669          }
01670          if (!strcasecmp(v->name, "permit")) {
01671             perm = ast_calloc(1, sizeof(*perm));
01672             if (perm) {
01673                perm->permit = 1;
01674                perm->command = ast_strdup(v->value);
01675             }
01676          } else if (!strcasecmp(v->name, "deny")) {
01677             perm = ast_calloc(1, sizeof(*perm));
01678             if (perm) {
01679                perm->permit = 0;
01680                perm->command = ast_strdup(v->value);
01681             }
01682          } else {
01683             /* up to now, only 'permit' and 'deny' are possible values. */
01684             ast_log(LOG_WARNING, "Unknown '%s' option\n", v->name);
01685             continue;
01686          }
01687          if (perm) {
01688             /* Added the permission to the user's list. */
01689             AST_LIST_INSERT_TAIL(user_group->perms, perm, list);
01690             perm = NULL;
01691          }
01692       }
01693       AST_RWLIST_WRLOCK(&cli_perms);
01694       AST_RWLIST_INSERT_TAIL(&cli_perms, user_group, list);
01695       AST_RWLIST_UNLOCK(&cli_perms);
01696    }
01697 
01698    ast_config_destroy(cfg);
01699    ast_mutex_unlock(&permsconfiglock);
01700    return 0;
01701 }

int ast_cli_register ( struct ast_cli_entry *  e  ) 

Registers a command or an array of commands.

Parameters:
e which cli entry to register. Register your own command
Return values:
0 on success
-1 on failure

Definition at line 1946 of file cli.c.

References __ast_cli_register().

Referenced by ast_cdr_engine_init(), ast_cli_register_multiple(), dnsmgr_init(), do_reload(), load_config(), and load_module().

01947 {
01948    return __ast_cli_register(e, NULL);
01949 }

int ast_cli_register_multiple ( struct ast_cli_entry *  e,
int  len 
)

Register multiple commands.

Parameters:
e pointer to first cli entry to register
len number of entries to register

Definition at line 1954 of file cli.c.

References ast_cli_register().

Referenced by __ast_register_translator(), __init_manager(), ast_ais_clm_load_module(), ast_ais_evt_load_module(), ast_builtins_init(), ast_channels_init(), ast_features_init(), ast_file_init(), ast_http_init(), ast_image_init(), ast_indications_init(), ast_rtp_init(), ast_test_init(), ast_timing_init(), ast_tps_init(), ast_udptl_init(), ast_utils_init(), astdb_init(), astobj2_init(), crypto_init(), iax_provision_init(), init_framer(), init_logger(), load_module(), load_pbx(), main(), and register_config_cli().

01955 {
01956    int i, res = 0;
01957 
01958    for (i = 0; i < len; i++)
01959       res |= ast_cli_register(e + i);
01960 
01961    return res;
01962 }

int ast_cli_unregister ( struct ast_cli_entry *  e  ) 

Unregisters a command or an array of commands.

Parameters:
e which cli entry to unregister Unregister your own command. You must pass a completed ast_cli_entry structure
Returns:
0

Definition at line 1940 of file cli.c.

References __ast_cli_unregister().

Referenced by alias_destroy(), ast_cli_unregister_multiple(), do_reload(), load_module(), and unload_module().

01941 {
01942    return __ast_cli_unregister(e, NULL);
01943 }

int ast_cli_unregister_multiple ( struct ast_cli_entry *  e,
int  len 
)

Unregister multiple commands.

Parameters:
e pointer to first cli entry to unregister
len number of entries to unregister

Definition at line 1964 of file cli.c.

References ast_cli_unregister().

Referenced by __unload_module(), ast_ais_clm_unload_module(), iax_provision_unload(), load_module(), and unload_module().

01965 {
01966    int i, res = 0;
01967 
01968    for (i = 0; i < len; i++)
01969       res |= ast_cli_unregister(e + i);
01970 
01971    return res;
01972 }

char* ast_complete_channels ( const char *  line,
const char *  word,
int  pos,
int  state,
int  rpos 
)

Command completion for the list of active channels.

This can be called from a CLI command completion function that wants to complete from the list of active channels. 'rpos' is the required position in the command. This function will return NULL immediately if 'rpos' is not the same as the current position, 'pos'.

Definition at line 1393 of file cli.c.

References ast_channel_unlock, ast_channel_walk_locked(), ast_strdup, and ast_channel::name.

Referenced by complete_ch(), handle_cli_agi_add_cmd(), handle_cli_mixmonitor(), handle_core_set_debug_channel(), handle_redirect(), handle_set_chanvar(), handle_show_chanvar(), handle_showchan(), and handle_softhangup().

01394 {
01395    struct ast_channel *c = NULL;
01396    int which = 0;
01397    int wordlen;
01398    char notfound = '\0';
01399    char *ret = &notfound; /* so NULL can break the loop */
01400 
01401    if (pos != rpos)
01402       return NULL;
01403 
01404    wordlen = strlen(word); 
01405 
01406    while (ret == &notfound && (c = ast_channel_walk_locked(c))) {
01407       if (!strncasecmp(word, c->name, wordlen) && ++which > state)
01408          ret = ast_strdup(c->name);
01409       ast_channel_unlock(c);
01410    }
01411    return ret == &notfound ? NULL : ret;
01412 }

unsigned int ast_debug_get_by_file ( const char *  file  ) 

Get the debug level for a file.

Parameters:
file the filename
Returns:
the debug level

Definition at line 118 of file cli.c.

References AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_debug_file::filename, and ast_debug_file::level.

00119 {
00120    struct ast_debug_file *adf;
00121    unsigned int res = 0;
00122 
00123    AST_RWLIST_RDLOCK(&debug_files);
00124    AST_LIST_TRAVERSE(&debug_files, adf, entry) {
00125       if (!strncasecmp(adf->filename, file, strlen(adf->filename))) {
00126          res = adf->level;
00127          break;
00128       }
00129    }
00130    AST_RWLIST_UNLOCK(&debug_files);
00131 
00132    return res;
00133 }

unsigned int ast_verbose_get_by_file ( const char *  file  ) 

Get the debug level for a file.

Parameters:
file the filename
Returns:
the debug level

Definition at line 135 of file cli.c.

References AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_debug_file::filename, and ast_debug_file::level.

00136 {
00137    struct ast_debug_file *adf;
00138    unsigned int res = 0;
00139 
00140    AST_RWLIST_RDLOCK(&verbose_files);
00141    AST_LIST_TRAVERSE(&verbose_files, adf, entry) {
00142       if (!strncasecmp(adf->filename, file, strlen(file))) {
00143          res = adf->level;
00144          break;
00145       }
00146    }
00147    AST_RWLIST_UNLOCK(&verbose_files);
00148 
00149    return res;
00150 }

static int cli_has_permissions ( int  uid,
int  gid,
const char *  command 
) [static]

Definition at line 165 of file cli.c.

References AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, CLI_NO_PERMS, cli_perm::command, usergroup_cli_perm::gid, cli_perm::permit, usergroup_cli_perm::perms, and usergroup_cli_perm::uid.

Referenced by ast_cli_command_full(), and handle_cli_check_permissions().

00166 {
00167    struct usergroup_cli_perm *user_perm;
00168    struct cli_perm *perm;
00169    /* set to the default permissions general option. */
00170    int isallowg = cli_default_perm, isallowu = -1, ispattern;
00171    regex_t regexbuf;
00172 
00173    /* if uid == -1 or gid == -1 do not check permissions.
00174       if uid == -2 and gid == -2 is because rasterisk client didn't send
00175       the credentials, so the cli_default_perm will be applied. */
00176    if ((uid == CLI_NO_PERMS && gid == CLI_NO_PERMS) || command[0] == '_') {
00177       return 1;
00178    }
00179 
00180    if (gid < 0 && uid < 0) {
00181       return cli_default_perm;
00182    }
00183 
00184    AST_RWLIST_RDLOCK(&cli_perms);
00185    AST_LIST_TRAVERSE(&cli_perms, user_perm, list) {
00186       if (user_perm->gid != gid && user_perm->uid != uid) {
00187          continue;
00188       }
00189       AST_LIST_TRAVERSE(user_perm->perms, perm, list) {
00190          if (strcasecmp(perm->command, "all") && strncasecmp(perm->command, command, strlen(perm->command))) {
00191             /* if the perm->command is a pattern, check it against command. */
00192             ispattern = !regcomp(&regexbuf, perm->command, REG_EXTENDED | REG_NOSUB | REG_ICASE);
00193             if (ispattern && regexec(&regexbuf, command, 0, NULL, 0)) {
00194                regfree(&regexbuf);
00195                continue;
00196             }
00197             if (!ispattern) {
00198                continue;
00199             }
00200             regfree(&regexbuf);
00201          }
00202          if (user_perm->uid == uid) {
00203             /* this is a user definition. */
00204             isallowu = perm->permit;
00205          } else {
00206             /* otherwise is a group definition. */
00207             isallowg = perm->permit;
00208          }
00209       }
00210    }
00211    AST_RWLIST_UNLOCK(&cli_perms);
00212    if (isallowu > -1) {
00213       /* user definition override group definition. */
00214       isallowg = isallowu;
00215    }
00216 
00217    return isallowg;
00218 }

static struct ast_cli_entry* cli_next ( struct ast_cli_entry *  e  )  [static, read]

Definition at line 659 of file cli.c.

References AST_LIST_FIRST, and AST_LIST_NEXT.

Referenced by __ast_cli_generator(), find_cli(), handle_cli_check_permissions(), and help1().

00660 {
00661    if (e) {
00662       return AST_LIST_NEXT(e, list);
00663    } else {
00664       return AST_LIST_FIRST(&helpers);
00665    }
00666 }

static char* complete_fn ( const char *  word,
int  state 
) [static]

Definition at line 222 of file cli.c.

References ast_config_AST_MODULE_DIR, ast_copy_string(), ast_strdup, and free.

Referenced by handle_load().

00223 {
00224    char *c, *d;
00225    char filename[PATH_MAX];
00226 
00227    if (word[0] == '/')
00228       ast_copy_string(filename, word, sizeof(filename));
00229    else
00230       snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_MODULE_DIR, word);
00231 
00232    c = d = filename_completion_function(filename, state);
00233    
00234    if (c && word[0] != '/')
00235       c += (strlen(ast_config_AST_MODULE_DIR) + 1);
00236    if (c)
00237       c = ast_strdup(c);
00238 
00239    free(d);
00240    
00241    return c;
00242 }

static char* complete_number ( const char *  partial,
unsigned int  min,
unsigned int  max,
int  n 
) [static]

Definition at line 322 of file cli.c.

References ast_strdup, ast_strlen_zero(), and cli_perm::next.

Referenced by handle_verbose().

00323 {
00324    int i, count = 0;
00325    unsigned int prospective[2];
00326    unsigned int part = strtoul(partial, NULL, 10);
00327    char next[12];
00328 
00329    if (part < min || part > max) {
00330       return NULL;
00331    }
00332 
00333    for (i = 0; i < 21; i++) {
00334       if (i == 0) {
00335          prospective[0] = prospective[1] = part;
00336       } else if (part == 0 && !ast_strlen_zero(partial)) {
00337          break;
00338       } else if (i < 11) {
00339          prospective[0] = prospective[1] = part * 10 + (i - 1);
00340       } else {
00341          prospective[0] = (part * 10 + (i - 11)) * 10;
00342          prospective[1] = prospective[0] + 9;
00343       }
00344       if (i < 11 && (prospective[0] < min || prospective[0] > max)) {
00345          continue;
00346       } else if (prospective[1] < min || prospective[0] > max) {
00347          continue;
00348       }
00349 
00350       if (++count > n) {
00351          if (i < 11) {
00352             snprintf(next, sizeof(next), "%u", prospective[0]);
00353          } else {
00354             snprintf(next, sizeof(next), "%u...", prospective[0] / 10);
00355          }
00356          return ast_strdup(next);
00357       }
00358    }
00359    return NULL;
00360 }

static void destroy_user_perms ( void   )  [static]

cleanup (free) cli_perms linkedlist.

Definition at line 1565 of file cli.c.

References ast_free, AST_LIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, cli_perm::command, and usergroup_cli_perm::perms.

Referenced by ast_cli_perms_init().

01566 {
01567    struct cli_perm *perm;
01568    struct usergroup_cli_perm *user_perm;
01569 
01570    AST_RWLIST_WRLOCK(&cli_perms);
01571    while ((user_perm = AST_LIST_REMOVE_HEAD(&cli_perms, list))) {
01572       while ((perm = AST_LIST_REMOVE_HEAD(user_perm->perms, list))) {
01573          ast_free(perm->command);
01574          ast_free(perm);
01575       }
01576       ast_free(user_perm);
01577    }
01578    AST_RWLIST_UNLOCK(&cli_perms);
01579 }

static char* find_best ( char *  argv[]  )  [static]

Definition at line 1843 of file cli.c.

References ast_join(), AST_MAX_CMD_LEN, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, and find_cli().

Referenced by ast_cli_command_full().

01844 {
01845    static char cmdline[80];
01846    int x;
01847    /* See how close we get, then print the candidate */
01848    char *myargv[AST_MAX_CMD_LEN];
01849    for (x=0;x<AST_MAX_CMD_LEN;x++)
01850       myargv[x]=NULL;
01851    AST_RWLIST_RDLOCK(&helpers);
01852    for (x=0;argv[x];x++) {
01853       myargv[x] = argv[x];
01854       if (!find_cli(myargv, -1))
01855          break;
01856    }
01857    AST_RWLIST_UNLOCK(&helpers);
01858    ast_join(cmdline, sizeof(cmdline), myargv);
01859    return cmdline;
01860 }

static struct ast_cli_entry* find_cli ( char *const   cmds[],
int  match_type 
) [static, read]

Definition at line 1799 of file cli.c.

References ast_strlen_zero(), cli_next(), ast_cli_entry::cmda, and word_match().

Referenced by __ast_cli_register(), ast_cli_command_full(), find_best(), and handle_help().

01800 {
01801    int matchlen = -1;   /* length of longest match so far */
01802    struct ast_cli_entry *cand = NULL, *e=NULL;
01803 
01804    while ( (e = cli_next(e)) ) {
01805       /* word-by word regexp comparison */
01806       char * const *src = cmds;
01807       char * const *dst = e->cmda;
01808       int n = 0;
01809       for (;; dst++, src += n) {
01810          n = word_match(*src, *dst);
01811          if (n < 0)
01812             break;
01813       }
01814       if (ast_strlen_zero(*dst) || ((*dst)[0] == '[' && ast_strlen_zero(dst[1]))) {
01815          /* no more words in 'e' */
01816          if (ast_strlen_zero(*src)) /* exact match, cannot do better */
01817             break;
01818          /* Here, cmds has more words than the entry 'e' */
01819          if (match_type != 0) /* but we look for almost exact match... */
01820             continue;   /* so we skip this one. */
01821          /* otherwise we like it (case 0) */
01822       } else { /* still words in 'e' */
01823          if (ast_strlen_zero(*src))
01824             continue; /* cmds is shorter than 'e', not good */
01825          /* Here we have leftover words in cmds and 'e',
01826           * but there is a mismatch. We only accept this one if match_type == -1
01827           * and this is the last word for both.
01828           */
01829          if (match_type != -1 || !ast_strlen_zero(src[1]) ||
01830              !ast_strlen_zero(dst[1])) /* not the one we look for */
01831             continue;
01832          /* good, we are in case match_type == -1 and mismatch on last word */
01833       }
01834       if (src - cmds > matchlen) {  /* remember the candidate */
01835          matchlen = src - cmds;
01836          cand = e;
01837       }
01838    }
01839 
01840    return e ? e : cand;
01841 }

static struct ast_debug_file* find_debug_file ( const char *  fn,
unsigned int  debug 
) [static, read]

Find the debug or verbose file setting.

  • debug 1 for debug, 0 for verbose

Definition at line 309 of file cli.c.

References AST_LIST_TRAVERSE, and ast_debug_file::filename.

Referenced by handle_verbose().

00310 {
00311    struct ast_debug_file *df = NULL;
00312    struct debug_file_list *dfl = debug ? &debug_files : &verbose_files;
00313 
00314    AST_LIST_TRAVERSE(dfl, df, entry) {
00315       if (!strcasecmp(df->filename, fn))
00316          break;
00317    }
00318 
00319    return df;
00320 }

static char* group_show_channels ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1414 of file cli.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_app_group_list_head(), ast_app_group_list_rdlock(), ast_app_group_list_unlock(), ast_cli(), AST_LIST_NEXT, ast_strlen_zero(), ast_group_info::category, ast_group_info::chan, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ESS, ast_cli_args::fd, FORMAT_STRING, ast_group_info::group, ast_group_info::group_list, ast_channel::name, and ast_cli_entry::usage.

01415 {
01416 #define FORMAT_STRING  "%-25s  %-20s  %-20s\n"
01417 
01418    struct ast_group_info *gi = NULL;
01419    int numchans = 0;
01420    regex_t regexbuf;
01421    int havepattern = 0;
01422 
01423    switch (cmd) {
01424    case CLI_INIT:
01425       e->command = "group show channels";
01426       e->usage = 
01427          "Usage: group show channels [pattern]\n"
01428          "       Lists all currently active channels with channel group(s) specified.\n"
01429          "       Optional regular expression pattern is matched to group names for each\n"
01430          "       channel.\n";
01431       return NULL;
01432    case CLI_GENERATE:
01433       return NULL;
01434    }
01435 
01436    if (a->argc < 3 || a->argc > 4)
01437       return CLI_SHOWUSAGE;
01438    
01439    if (a->argc == 4) {
01440       if (regcomp(&regexbuf, a->argv[3], REG_EXTENDED | REG_NOSUB))
01441          return CLI_SHOWUSAGE;
01442       havepattern = 1;
01443    }
01444 
01445    ast_cli(a->fd, FORMAT_STRING, "Channel", "Group", "Category");
01446 
01447    ast_app_group_list_rdlock();
01448    
01449    gi = ast_app_group_list_head();
01450    while (gi) {
01451       if (!havepattern || !regexec(&regexbuf, gi->group, 0, NULL, 0)) {
01452          ast_cli(a->fd, FORMAT_STRING, gi->chan->name, gi->group, (ast_strlen_zero(gi->category) ? "(default)" : gi->category));
01453          numchans++;
01454       }
01455       gi = AST_LIST_NEXT(gi, group_list);
01456    }
01457    
01458    ast_app_group_list_unlock();
01459    
01460    if (havepattern)
01461       regfree(&regexbuf);
01462 
01463    ast_cli(a->fd, "%d active channel%s\n", numchans, ESS(numchans));
01464    return CLI_SUCCESS;
01465 #undef FORMAT_STRING
01466 }

static char* handle_chanlist ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 793 of file cli.c.

References ast_channel::_state, ast_channel::accountcode, ast_channel::amaflags, ast_channel::appl, ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_active_calls(), ast_bridged_channel(), ast_channel_unlock, ast_channel_walk_locked(), ast_cli(), ast_processed_calls(), ast_state2str(), ast_strlen_zero(), ast_tvdiff_ms(), ast_tvnow(), ast_tvzero(), ast_channel::cdr, ast_channel::cid, ast_callerid::cid_num, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, CONCISE_FORMAT_STRING, ast_channel::context, ast_channel::data, ESS, ast_channel::exten, ast_cli_args::fd, FORMAT_STRING, FORMAT_STRING2, ast_channel::name, option_maxcalls, ast_channel::priority, S_OR, ast_cdr::start, ast_channel::uniqueid, ast_cli_entry::usage, VERBOSE_FORMAT_STRING, and VERBOSE_FORMAT_STRING2.

00794 {
00795 #define FORMAT_STRING  "%-20.20s %-20.20s %-7.7s %-30.30s\n"
00796 #define FORMAT_STRING2 "%-20.20s %-20.20s %-7.7s %-30.30s\n"
00797 #define CONCISE_FORMAT_STRING  "%s!%s!%s!%d!%s!%s!%s!%s!%s!%d!%s!%s!%s\n"
00798 #define VERBOSE_FORMAT_STRING  "%-20.20s %-20.20s %-16.16s %4d %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"
00799 #define VERBOSE_FORMAT_STRING2 "%-20.20s %-20.20s %-16.16s %-4.4s %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"
00800 
00801    struct ast_channel *c = NULL;
00802    int numchans = 0, concise = 0, verbose = 0, count = 0;
00803    int fd, argc;
00804    char **argv;
00805 
00806    switch (cmd) {
00807    case CLI_INIT:
00808       e->command = "core show channels [concise|verbose|count]";
00809       e->usage =
00810          "Usage: core show channels [concise|verbose|count]\n"
00811          "       Lists currently defined channels and some information about them. If\n"
00812          "       'concise' is specified, the format is abridged and in a more easily\n"
00813          "       machine parsable format. If 'verbose' is specified, the output includes\n"
00814          "       more and longer fields. If 'count' is specified only the channel and call\n"
00815          "       count is output.\n"
00816          "  The 'concise' option is deprecated and will be removed from future versions\n"
00817          "  of Asterisk.\n";
00818       return NULL;
00819 
00820    case CLI_GENERATE:
00821       return NULL;
00822    }
00823    fd = a->fd;
00824    argc = a->argc;
00825    argv = a->argv;
00826 
00827    if (a->argc == e->args) {
00828       if (!strcasecmp(argv[e->args-1],"concise"))
00829          concise = 1;
00830       else if (!strcasecmp(argv[e->args-1],"verbose"))
00831          verbose = 1;
00832       else if (!strcasecmp(argv[e->args-1],"count"))
00833          count = 1;
00834       else
00835          return CLI_SHOWUSAGE;
00836    } else if (a->argc != e->args - 1)
00837       return CLI_SHOWUSAGE;
00838 
00839    if (!count) {
00840       if (!concise && !verbose)
00841          ast_cli(fd, FORMAT_STRING2, "Channel", "Location", "State", "Application(Data)");
00842       else if (verbose)
00843          ast_cli(fd, VERBOSE_FORMAT_STRING2, "Channel", "Context", "Extension", "Priority", "State", "Application", "Data", 
00844             "CallerID", "Duration", "Accountcode", "BridgedTo");
00845    }
00846 
00847    while ((c = ast_channel_walk_locked(c)) != NULL) {
00848       struct ast_channel *bc = ast_bridged_channel(c);
00849       char durbuf[10] = "-";
00850 
00851       if (!count) {
00852          if ((concise || verbose)  && c->cdr && !ast_tvzero(c->cdr->start)) {
00853             int duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
00854             if (verbose) {
00855                int durh = duration / 3600;
00856                int durm = (duration % 3600) / 60;
00857                int durs = duration % 60;
00858                snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
00859             } else {
00860                snprintf(durbuf, sizeof(durbuf), "%d", duration);
00861             }           
00862          }
00863          if (concise) {
00864             ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
00865                c->appl ? c->appl : "(None)",
00866                S_OR(c->data, ""),   /* XXX different from verbose ? */
00867                S_OR(c->cid.cid_num, ""),
00868                S_OR(c->accountcode, ""),
00869                c->amaflags, 
00870                durbuf,
00871                bc ? bc->name : "(None)",
00872                c->uniqueid);
00873          } else if (verbose) {
00874             ast_cli(fd, VERBOSE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
00875                c->appl ? c->appl : "(None)",
00876                c->data ? S_OR(c->data, "(Empty)" ): "(None)",
00877                S_OR(c->cid.cid_num, ""),
00878                durbuf,
00879                S_OR(c->accountcode, ""),
00880                bc ? bc->name : "(None)");
00881          } else {
00882             char locbuf[40] = "(None)";
00883             char appdata[40] = "(None)";
00884             
00885             if (!ast_strlen_zero(c->context) && !ast_strlen_zero(c->exten)) 
00886                snprintf(locbuf, sizeof(locbuf), "%s@%s:%d", c->exten, c->context, c->priority);
00887             if (c->appl)
00888                snprintf(appdata, sizeof(appdata), "%s(%s)", c->appl, S_OR(c->data, ""));
00889             ast_cli(fd, FORMAT_STRING, c->name, locbuf, ast_state2str(c->_state), appdata);
00890          }
00891       }
00892       numchans++;
00893       ast_channel_unlock(c);
00894    }
00895    if (!concise) {
00896       ast_cli(fd, "%d active channel%s\n", numchans, ESS(numchans));
00897       if (option_maxcalls)
00898          ast_cli(fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
00899             ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
00900             ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
00901       else
00902          ast_cli(fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
00903 
00904       ast_cli(fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
00905    }
00906    return CLI_SUCCESS;
00907    
00908 #undef FORMAT_STRING
00909 #undef FORMAT_STRING2
00910 #undef CONCISE_FORMAT_STRING
00911 #undef VERBOSE_FORMAT_STRING
00912 #undef VERBOSE_FORMAT_STRING2
00913 }

static char* handle_cli_check_permissions ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

handles CLI command 'cli check permissions'

Definition at line 1007 of file cli.c.

References ast_cli_entry::_full_cmd, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_cli_generator(), ast_join(), AST_MAX_ARGS, ast_strdupa, ast_strlen_zero(), CLI_FAILURE, CLI_GENERATE, cli_has_permissions(), CLI_INIT, cli_next(), CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, cli_perm::command, ast_cli_args::fd, ast_cli_args::line, ast_cli_args::n, ast_cli_args::pos, S_OR, ast_cli_entry::summary, ast_cli_entry::usage, and ast_cli_args::word.

01008 {
01009    struct passwd *pw = NULL;
01010    struct group *gr;
01011    int gid = -1, uid = -1;
01012    char command[AST_MAX_ARGS] = "";
01013    struct ast_cli_entry *ce = NULL;
01014    int found = 0;
01015    char *group, *tmp;
01016 
01017    switch (cmd) {
01018    case CLI_INIT:
01019       e->command = "cli check permissions";
01020       e->usage =
01021          "Usage: cli check permissions {<username>|@<groupname>|<username>@<groupname>} [<command>]\n"
01022          "       Check permissions config for a user@group or list the allowed commands for the specified user.\n"
01023          "       The username or the groupname may be omitted.\n";
01024       return NULL;
01025    case CLI_GENERATE:
01026       if (a->pos >= 4) {
01027          return ast_cli_generator(a->line + strlen("cli check permissions") + strlen(a->argv[3]) + 1, a->word, a->n);
01028       }
01029       return NULL;
01030    }
01031 
01032    if (a->argc < 4) {
01033       return CLI_SHOWUSAGE;
01034    }
01035 
01036    tmp = ast_strdupa(a->argv[3]);
01037    group = strchr(tmp, '@');
01038    if (group) {
01039       gr = getgrnam(&group[1]);
01040       if (!gr) {
01041          ast_cli(a->fd, "Unknown group '%s'\n", &group[1]);
01042          return CLI_FAILURE;
01043       }
01044       group[0] = '\0';
01045       gid = gr->gr_gid;
01046    }
01047 
01048    if (!group && ast_strlen_zero(tmp)) {
01049       ast_cli(a->fd, "You didn't supply a username\n");
01050    } else if (!ast_strlen_zero(tmp) && !(pw = getpwnam(tmp))) {
01051       ast_cli(a->fd, "Unknown user '%s'\n", tmp);
01052       return CLI_FAILURE;
01053    } else if (pw) {
01054       uid = pw->pw_uid;
01055    }
01056 
01057    if (a->argc == 4) {
01058       while ((ce = cli_next(ce))) {
01059          /* Hide commands that start with '_' */
01060          if (ce->_full_cmd[0] == '_') {
01061             continue;
01062          }
01063          if (cli_has_permissions(uid, gid, ce->_full_cmd)) {
01064             ast_cli(a->fd, "%30.30s %s\n", ce->_full_cmd, S_OR(ce->summary, "<no description available>"));
01065             found++;
01066          }
01067       }
01068       if (!found) {
01069          ast_cli(a->fd, "You are not allowed to run any command on Asterisk\n");
01070       }
01071    } else {
01072       ast_join(command, sizeof(command), a->argv + 4);
01073       ast_cli(a->fd, "%s '%s%s%s' is %s to run command: '%s'\n", uid >= 0 ? "User" : "Group", tmp,
01074          group && uid >= 0 ? "@" : "",
01075          group ? &group[1] : "",
01076          cli_has_permissions(uid, gid, command) ? "allowed" : "not allowed", command);
01077    }
01078 
01079    return CLI_SUCCESS;
01080 }

static char* handle_cli_reload_permissions ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

handles CLI command 'cli reload permissions'

Definition at line 988 of file cli.c.

References ast_cli_perms_init(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, and ast_cli_entry::usage.

00989 {
00990    switch (cmd) {
00991    case CLI_INIT:
00992       e->command = "cli reload permissions";
00993       e->usage =
00994          "Usage: cli reload permissions\n"
00995          "       Reload the 'cli_permissions.conf' file.\n";
00996       return NULL;
00997    case CLI_GENERATE:
00998       return NULL;
00999    }
01000 
01001    ast_cli_perms_init(1);
01002 
01003    return CLI_SUCCESS;
01004 }

static char* handle_cli_show_permissions ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

handles CLI command 'cli show permissions'

Definition at line 943 of file cli.c.

References ast_cli(), AST_LIST_TRAVERSE, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, cli_perm::command, ast_cli_entry::command, ast_cli_args::fd, usergroup_cli_perm::gid, cli_perm::permit, usergroup_cli_perm::perms, usergroup_cli_perm::uid, and ast_cli_entry::usage.

00944 {
00945    struct usergroup_cli_perm *cp;
00946    struct cli_perm *perm;
00947    struct passwd *pw = NULL;
00948    struct group *gr = NULL;
00949 
00950    switch (cmd) {
00951    case CLI_INIT:
00952       e->command = "cli show permissions";
00953       e->usage =
00954          "Usage: cli show permissions\n"
00955          "       Shows CLI configured permissions.\n";
00956       return NULL;
00957    case CLI_GENERATE:
00958       return NULL;
00959    }
00960 
00961    AST_RWLIST_RDLOCK(&cli_perms);
00962    AST_LIST_TRAVERSE(&cli_perms, cp, list) {
00963       if (cp->uid >= 0) {
00964          pw = getpwuid(cp->uid);
00965          if (pw) {
00966             ast_cli(a->fd, "user: %s [uid=%d]\n", pw->pw_name, cp->uid);
00967          }
00968       } else {
00969          gr = getgrgid(cp->gid);
00970          if (gr) {
00971             ast_cli(a->fd, "group: %s [gid=%d]\n", gr->gr_name, cp->gid);
00972          }
00973       }
00974       ast_cli(a->fd, "Permissions:\n");
00975       if (cp->perms) {
00976          AST_LIST_TRAVERSE(cp->perms, perm, list) {
00977             ast_cli(a->fd, "\t%s -> %s\n", perm->permit ? "permit" : "deny", perm->command);
00978          }
00979       }
00980       ast_cli(a->fd, "\n");
00981    }
00982    AST_RWLIST_UNLOCK(&cli_perms);
00983 
00984    return CLI_SUCCESS;
00985 }

static char* handle_cli_wait_fullybooted ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1468 of file cli.c.

References ast_cli(), AST_OPT_FLAG_FULLY_BOOTED, ast_options, ast_test_flag, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

01469 {
01470    switch (cmd) {
01471    case CLI_INIT:
01472       e->command = "core waitfullybooted";
01473       e->usage =
01474          "Usage: core waitfullybooted\n"
01475          "  Wait until Asterisk has fully booted.\n";
01476       return NULL;
01477    case CLI_GENERATE:
01478       return NULL;
01479    }
01480 
01481    while (!ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
01482       usleep(100);
01483    }
01484 
01485    ast_cli(a->fd, "Asterisk has fully booted.\n");
01486 
01487    return CLI_SUCCESS;
01488 }

static char* handle_commandcomplete ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1165 of file cli.c.

References __ast_cli_generator(), ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_free, buf, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

01166 {
01167    char *buf;
01168    switch (cmd) {
01169    case CLI_INIT:
01170       e->command = "_command complete";
01171       e->usage = 
01172          "Usage: _command complete \"<line>\" text state\n"
01173          "       This function is used internally to help with command completion and should.\n"
01174          "       never be called by the user directly.\n";
01175       return NULL;
01176    case CLI_GENERATE:
01177       return NULL;
01178    }
01179    if (a->argc != 5)
01180       return CLI_SHOWUSAGE;
01181    buf = __ast_cli_generator(a->argv[2], a->argv[3], atoi(a->argv[4]), 0);
01182    if (buf) {
01183       ast_cli(a->fd, "%s", buf);
01184       ast_free(buf);
01185    } else
01186       ast_cli(a->fd, "NULL\n");
01187    return CLI_SUCCESS;
01188 }

static char* handle_commandmatchesarray ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1084 of file cli.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), AST_CLI_COMPLETE_EOF, ast_cli_completion_matches(), ast_free, ast_malloc, ast_realloc, buf, CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, len(), and ast_cli_entry::usage.

01085 {
01086    char *buf, *obuf;
01087    int buflen = 2048;
01088    int len = 0;
01089    char **matches;
01090    int x, matchlen;
01091    
01092    switch (cmd) {
01093    case CLI_INIT:
01094       e->command = "_command matchesarray";
01095       e->usage = 
01096          "Usage: _command matchesarray \"<line>\" text \n"
01097          "       This function is used internally to help with command completion and should.\n"
01098          "       never be called by the user directly.\n";
01099       return NULL;
01100    case CLI_GENERATE:
01101       return NULL;
01102    }
01103 
01104    if (a->argc != 4)
01105       return CLI_SHOWUSAGE;
01106    if (!(buf = ast_malloc(buflen)))
01107       return CLI_FAILURE;
01108    buf[len] = '\0';
01109    matches = ast_cli_completion_matches(a->argv[2], a->argv[3]);
01110    if (matches) {
01111       for (x=0; matches[x]; x++) {
01112          matchlen = strlen(matches[x]) + 1;
01113          if (len + matchlen >= buflen) {
01114             buflen += matchlen * 3;
01115             obuf = buf;
01116             if (!(buf = ast_realloc(obuf, buflen))) 
01117                /* Memory allocation failure...  Just free old buffer and be done */
01118                ast_free(obuf);
01119          }
01120          if (buf)
01121             len += sprintf( buf + len, "%s ", matches[x]);
01122          ast_free(matches[x]);
01123          matches[x] = NULL;
01124       }
01125       ast_free(matches);
01126    }
01127 
01128    if (buf) {
01129       ast_cli(a->fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
01130       ast_free(buf);
01131    } else
01132       ast_cli(a->fd, "NULL\n");
01133 
01134    return CLI_SUCCESS;
01135 }

static char* handle_commandnummatches ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1139 of file cli.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_cli_generatornummatches(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

01140 {
01141    int matches = 0;
01142 
01143    switch (cmd) {
01144    case CLI_INIT:
01145       e->command = "_command nummatches";
01146       e->usage = 
01147          "Usage: _command nummatches \"<line>\" text \n"
01148          "       This function is used internally to help with command completion and should.\n"
01149          "       never be called by the user directly.\n";
01150       return NULL;
01151    case CLI_GENERATE:
01152       return NULL;
01153    }
01154 
01155    if (a->argc != 4)
01156       return CLI_SHOWUSAGE;
01157 
01158    matches = ast_cli_generatornummatches(a->argv[2], a->argv[3]);
01159 
01160    ast_cli(a->fd, "%d", matches);
01161 
01162    return CLI_SUCCESS;
01163 }

static char* handle_core_set_debug_channel ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1190 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_channel_unlock, ast_channel_walk_locked(), ast_cli(), ast_complete_channels(), ast_get_channel_by_name_locked(), ast_strdup, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, DEBUGCHAN_FLAG, ast_cli_args::fd, ast_channel::fin, ast_channel::fout, global_fin, global_fout, ast_cli_args::line, ast_cli_args::n, ast_channel::name, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

Referenced by handle_nodebugchan_deprecated().

01191 {
01192    struct ast_channel *c = NULL;
01193    int is_all, is_off = 0;
01194 
01195    switch (cmd) {
01196    case CLI_INIT:
01197       e->command = "core set debug channel";
01198       e->usage =
01199          "Usage: core set debug channel <all|channel> [off]\n"
01200          "       Enables/disables debugging on all or on a specific channel.\n";
01201       return NULL;
01202 
01203    case CLI_GENERATE:
01204       /* XXX remember to handle the optional "off" */
01205       if (a->pos != e->args)
01206          return NULL;
01207       return a->n == 0 ? ast_strdup("all") : ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
01208    }
01209    /* 'core set debug channel {all|chan_id}' */
01210    if (a->argc == e->args + 2) {
01211       if (!strcasecmp(a->argv[e->args + 1], "off"))
01212          is_off = 1;
01213       else
01214          return CLI_SHOWUSAGE;
01215    } else if (a->argc != e->args + 1)
01216       return CLI_SHOWUSAGE;
01217 
01218    is_all = !strcasecmp("all", a->argv[e->args]);
01219    if (is_all) {
01220       if (is_off) {
01221          global_fin &= ~DEBUGCHAN_FLAG;
01222          global_fout &= ~DEBUGCHAN_FLAG;
01223       } else {
01224          global_fin |= DEBUGCHAN_FLAG;
01225          global_fout |= DEBUGCHAN_FLAG;
01226       }
01227       c = ast_channel_walk_locked(NULL);
01228    } else {
01229       c = ast_get_channel_by_name_locked(a->argv[e->args]);
01230       if (c == NULL)
01231          ast_cli(a->fd, "No such channel %s\n", a->argv[e->args]);
01232    }
01233    while (c) {
01234       if (!(c->fin & DEBUGCHAN_FLAG) || !(c->fout & DEBUGCHAN_FLAG)) {
01235          if (is_off) {
01236             c->fin &= ~DEBUGCHAN_FLAG;
01237             c->fout &= ~DEBUGCHAN_FLAG;
01238          } else {
01239             c->fin |= DEBUGCHAN_FLAG;
01240             c->fout |= DEBUGCHAN_FLAG;
01241          }
01242          ast_cli(a->fd, "Debugging %s on channel %s\n", is_off ? "disabled" : "enabled", c->name);
01243       }
01244       ast_channel_unlock(c);
01245       if (!is_all)
01246          break;
01247       c = ast_channel_walk_locked(c);
01248    }
01249    ast_cli(a->fd, "Debugging on new channels is %s\n", is_off ? "disabled" : "enabled");
01250    return CLI_SUCCESS;
01251 }

static char * handle_help ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 2007 of file cli.c.

References __ast_cli_generator(), ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_join(), AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, find_cli(), help1(), ast_cli_args::line, ast_cli_args::n, ast_cli_entry::usage, and ast_cli_args::word.

02008 {
02009    char fullcmd[80];
02010    struct ast_cli_entry *my_e;
02011    char *res = CLI_SUCCESS;
02012 
02013    if (cmd == CLI_INIT) {
02014       e->command = "core show help";
02015       e->usage =
02016          "Usage: core show help [topic]\n"
02017          "       When called with a topic as an argument, displays usage\n"
02018          "       information on the given command. If called without a\n"
02019          "       topic, it provides a list of commands.\n";
02020       return NULL;
02021 
02022    } else if (cmd == CLI_GENERATE) {
02023       /* skip first 14 or 15 chars, "core show help " */
02024       int l = strlen(a->line);
02025 
02026       if (l > 15) {
02027          l = 15;
02028       }
02029       /* XXX watch out, should stop to the non-generator parts */
02030       return __ast_cli_generator(a->line + l, a->word, a->n, 0);
02031    }
02032    if (a->argc == e->args) {
02033       return help1(a->fd, NULL, 0);
02034    }
02035 
02036    AST_RWLIST_RDLOCK(&helpers);
02037    my_e = find_cli(a->argv + 3, 1); /* try exact match first */
02038    if (!my_e) {
02039       res = help1(a->fd, a->argv + 3, 1 /* locked */);
02040       AST_RWLIST_UNLOCK(&helpers);
02041       return res;
02042    }
02043    if (my_e->usage)
02044       ast_cli(a->fd, "%s", my_e->usage);
02045    else {
02046       ast_join(fullcmd, sizeof(fullcmd), a->argv + 3);
02047       ast_cli(a->fd, "No help text available for '%s'.\n", fullcmd);
02048    }
02049    AST_RWLIST_UNLOCK(&helpers);
02050    return res;
02051 }

static char* handle_load ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 244 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_load_resource(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_fn(), ast_cli_args::fd, ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

00245 {
00246    /* "module load <mod>" */
00247    switch (cmd) {
00248    case CLI_INIT:
00249       e->command = "module load";
00250       e->usage =
00251          "Usage: module load <module name>\n"
00252          "       Loads the specified module into Asterisk.\n";
00253       return NULL;
00254 
00255    case CLI_GENERATE:
00256       if (a->pos != e->args)
00257          return NULL;
00258       return complete_fn(a->word, a->n);
00259    }
00260    if (a->argc != e->args + 1)
00261       return CLI_SHOWUSAGE;
00262    if (ast_load_resource(a->argv[e->args])) {
00263       ast_cli(a->fd, "Unable to load module %s\n", a->argv[e->args]);
00264       return CLI_FAILURE;
00265    }
00266    ast_cli(a->fd, "Loaded %s\n", a->argv[e->args]);
00267    return CLI_SUCCESS;
00268 }

static char* handle_logger_mute ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 518 of file cli.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_console_toggle_mute(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

00519 {
00520    switch (cmd) {
00521    case CLI_INIT:
00522       e->command = "logger mute";
00523       e->usage = 
00524          "Usage: logger mute\n"
00525          "       Disables logging output to the current console, making it possible to\n"
00526          "       gather information without being disturbed by scrolling lines.\n";
00527       return NULL;
00528    case CLI_GENERATE:
00529       return NULL;
00530    }
00531 
00532    if (a->argc < 2 || a->argc > 3)
00533       return CLI_SHOWUSAGE;
00534 
00535    if (a->argc == 3 && !strcasecmp(a->argv[2], "silent"))
00536       ast_console_toggle_mute(a->fd, 1);
00537    else
00538       ast_console_toggle_mute(a->fd, 0);
00539 
00540    return CLI_SUCCESS;
00541 }

static char* handle_modlist ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 699 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_module_helper(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_module_list(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, climodentrylock, ast_cli_entry::command, ast_cli_args::fd, ast_cli_args::line, MODLIST_FORMAT2, modlist_modentry(), ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

00700 {
00701    char *like;
00702 
00703    switch (cmd) {
00704    case CLI_INIT:
00705       e->command = "module show [like]";
00706       e->usage =
00707          "Usage: module show [like keyword]\n"
00708          "       Shows Asterisk modules currently in use, and usage statistics.\n";
00709       return NULL;
00710 
00711    case CLI_GENERATE:
00712       if (a->pos == e->args)
00713          return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
00714       else
00715          return NULL;
00716    }
00717    /* all the above return, so we proceed with the handler.
00718     * we are guaranteed to have argc >= e->args
00719     */
00720    if (a->argc == e->args - 1)
00721       like = "";
00722    else if (a->argc == e->args + 1 && !strcasecmp(a->argv[e->args-1], "like") )
00723       like = a->argv[e->args];
00724    else
00725       return CLI_SHOWUSAGE;
00726       
00727    ast_mutex_lock(&climodentrylock);
00728    climodentryfd = a->fd; /* global, protected by climodentrylock */
00729    ast_cli(a->fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
00730    ast_cli(a->fd,"%d modules loaded\n", ast_update_module_list(modlist_modentry, like));
00731    climodentryfd = -1;
00732    ast_mutex_unlock(&climodentrylock);
00733    return CLI_SUCCESS;
00734 }

static char* handle_nodebugchan_deprecated ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1253 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, CLI_HANDLER, CLI_INIT, CLI_SHOWUSAGE, ast_cli_entry::command, and handle_core_set_debug_channel().

01254 {
01255    char *res;
01256    if (cmd == CLI_HANDLER) {
01257       if (a->argc != e->args + 1)
01258          return CLI_SHOWUSAGE;
01259       /* pretend we have an extra "off" at the end. We can do this as the array
01260        * is NULL terminated so we overwrite that entry.
01261        */
01262       a->argv[e->args+1] = "off";
01263       a->argc++;
01264    }
01265    res = handle_core_set_debug_channel(e, cmd, a);
01266    if (cmd == CLI_INIT)
01267       e->command = "no debug channel";
01268    return res;
01269 }

static char* handle_reload ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 270 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_module_helper(), ast_module_reload(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, ast_cli_args::line, ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

00271 {
00272    int x;
00273 
00274    switch (cmd) {
00275    case CLI_INIT:
00276       e->command = "module reload";
00277       e->usage =
00278          "Usage: module reload [module ...]\n"
00279          "       Reloads configuration files for all listed modules which support\n"
00280          "       reloading, or for all supported modules if none are listed.\n";
00281       return NULL;
00282 
00283    case CLI_GENERATE:
00284       return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 1);
00285    }
00286    if (a->argc == e->args) {
00287       ast_module_reload(NULL);
00288       return CLI_SUCCESS;
00289    }
00290    for (x = e->args; x < a->argc; x++) {
00291       int res = ast_module_reload(a->argv[x]);
00292       /* XXX reload has multiple error returns, including -1 on error and 2 on success */
00293       switch (res) {
00294       case 0:
00295          ast_cli(a->fd, "No such module '%s'\n", a->argv[x]);
00296          break;
00297       case 1:
00298          ast_cli(a->fd, "Module '%s' does not support reload\n", a->argv[x]);
00299          break;
00300       }
00301    }
00302    return CLI_SUCCESS;
00303 }

static char* handle_showcalls ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 738 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_active_calls(), ast_cli(), ast_processed_calls(), ast_startuptime, ast_strdup, ast_tvnow(), ast_tvsub(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, ast_cli_entry::command, ESS, ast_cli_args::fd, ast_cli_args::n, option_maxcalls, ast_cli_args::pos, print_uptimestr(), RESULT_SUCCESS, and ast_cli_entry::usage.

00739 {
00740    struct timeval curtime = ast_tvnow();
00741    int showuptime, printsec;
00742 
00743    switch (cmd) {
00744    case CLI_INIT:
00745       e->command = "core show calls [uptime]";
00746       e->usage =
00747          "Usage: core show calls [uptime] [seconds]\n"
00748          "       Lists number of currently active calls and total number of calls\n"
00749          "       processed through PBX since last restart. If 'uptime' is specified\n"
00750          "       the system uptime is also displayed. If 'seconds' is specified in\n"
00751          "       addition to 'uptime', the system uptime is displayed in seconds.\n";
00752       return NULL;
00753 
00754    case CLI_GENERATE:
00755       if (a->pos != e->args)
00756          return NULL;
00757       return a->n == 0  ? ast_strdup("seconds") : NULL;
00758    }
00759 
00760    /* regular handler */
00761    if (a->argc >= e->args && !strcasecmp(a->argv[e->args-1],"uptime")) {
00762       showuptime = 1;
00763 
00764       if (a->argc == e->args+1 && !strcasecmp(a->argv[e->args],"seconds"))
00765          printsec = 1;
00766       else if (a->argc == e->args)
00767          printsec = 0;
00768       else
00769          return CLI_SHOWUSAGE;
00770    } else if (a->argc == e->args-1) {
00771       showuptime = 0;
00772       printsec = 0;
00773    } else
00774       return CLI_SHOWUSAGE;
00775 
00776    if (option_maxcalls) {
00777       ast_cli(a->fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
00778          ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
00779          ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
00780    } else {
00781       ast_cli(a->fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
00782    }
00783    
00784    ast_cli(a->fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
00785 
00786    if (ast_startuptime.tv_sec && showuptime) {
00787       print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
00788    }
00789 
00790    return RESULT_SUCCESS;
00791 }

static char* handle_showchan ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 1271 of file cli.c.

References ast_channel::_bridge, ast_channel::_state, ast_channel::appl, ast_cli_args::argc, ast_cli_args::argv, ast_bridged_channel(), ast_cdr_serialize_variables(), ast_channel_unlock, ast_cli(), ast_complete_channels(), AST_FLAG_BLOCKING, ast_get_channel_by_name_locked(), ast_getformatname_multiple(), ast_state2str(), ast_str_buffer(), ast_str_thread_get(), ast_test_flag, ast_tvnow(), ast_channel::blockproc, ast_channel::callgroup, ast_channel::cdr, ast_channel::cid, ast_callerid::cid_dnid, ast_callerid::cid_name, ast_callerid::cid_num, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_channel::context, ast_channel::data, DEBUGCHAN_FLAG, ast_channel::exten, ast_cli_args::fd, ast_channel::fds, ast_channel::fin, ast_channel::fout, ast_channel::language, ast_cli_args::line, ast_cli_args::n, name, ast_channel::name, ast_channel::nativeformats, pbx_builtin_serialize_variables(), ast_channel::pickupgroup, ast_cli_args::pos, ast_channel::priority, ast_channel::readformat, ast_channel::readtrans, ast_channel::rings, S_OR, sec, ast_cdr::start, ast_channel::tech, ast_channel_tech::type, ast_channel::uniqueid, ast_cli_entry::usage, ast_channel::whentohangup, ast_cli_args::word, ast_channel::writeformat, and ast_channel::writetrans.

01272 {
01273    struct ast_channel *c=NULL;
01274    struct timeval now;
01275    struct ast_str *out = ast_str_thread_get(&ast_str_thread_global_buf, 16);
01276    char cdrtime[256];
01277    char nf[256], wf[256], rf[256];
01278    long elapsed_seconds=0;
01279    int hour=0, min=0, sec=0;
01280 #ifdef CHANNEL_TRACE
01281    int trace_enabled;
01282 #endif
01283 
01284    switch (cmd) {
01285    case CLI_INIT:
01286       e->command = "core show channel";
01287       e->usage = 
01288          "Usage: core show channel <channel>\n"
01289          "       Shows lots of information about the specified channel.\n";
01290       return NULL;
01291    case CLI_GENERATE:
01292       return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
01293    }
01294    
01295    if (a->argc != 4)
01296       return CLI_SHOWUSAGE;
01297    now = ast_tvnow();
01298    c = ast_get_channel_by_name_locked(a->argv[3]);
01299    if (!c) {
01300       ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
01301       return CLI_SUCCESS;
01302    }
01303    if (c->cdr) {
01304       elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
01305       hour = elapsed_seconds / 3600;
01306       min = (elapsed_seconds % 3600) / 60;
01307       sec = elapsed_seconds % 60;
01308       snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
01309    } else
01310       strcpy(cdrtime, "N/A");
01311    ast_cli(a->fd, 
01312       " -- General --\n"
01313       "           Name: %s\n"
01314       "           Type: %s\n"
01315       "       UniqueID: %s\n"
01316       "      Caller ID: %s\n"
01317       " Caller ID Name: %s\n"
01318       "    DNID Digits: %s\n"
01319       "       Language: %s\n"
01320       "          State: %s (%d)\n"
01321       "          Rings: %d\n"
01322       "  NativeFormats: %s\n"
01323       "    WriteFormat: %s\n"
01324       "     ReadFormat: %s\n"
01325       " WriteTranscode: %s\n"
01326       "  ReadTranscode: %s\n"
01327       "1st File Descriptor: %d\n"
01328       "      Frames in: %d%s\n"
01329       "     Frames out: %d%s\n"
01330       " Time to Hangup: %ld\n"
01331       "   Elapsed Time: %s\n"
01332       "  Direct Bridge: %s\n"
01333       "Indirect Bridge: %s\n"
01334       " --   PBX   --\n"
01335       "        Context: %s\n"
01336       "      Extension: %s\n"
01337       "       Priority: %d\n"
01338       "     Call Group: %llu\n"
01339       "   Pickup Group: %llu\n"
01340       "    Application: %s\n"
01341       "           Data: %s\n"
01342       "    Blocking in: %s\n",
01343       c->name, c->tech->type, c->uniqueid,
01344       S_OR(c->cid.cid_num, "(N/A)"),
01345       S_OR(c->cid.cid_name, "(N/A)"),
01346       S_OR(c->cid.cid_dnid, "(N/A)"), 
01347       c->language,   
01348       ast_state2str(c->_state), c->_state, c->rings, 
01349       ast_getformatname_multiple(nf, sizeof(nf), c->nativeformats), 
01350       ast_getformatname_multiple(wf, sizeof(wf), c->writeformat), 
01351       ast_getformatname_multiple(rf, sizeof(rf), c->readformat),
01352       c->writetrans ? "Yes" : "No",
01353       c->readtrans ? "Yes" : "No",
01354       c->fds[0],
01355       c->fin & ~DEBUGCHAN_FLAG, (c->fin & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
01356       c->fout & ~DEBUGCHAN_FLAG, (c->fout & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
01357       (long)c->whentohangup.tv_sec,
01358       cdrtime, c->_bridge ? c->_bridge->name : "<none>", ast_bridged_channel(c) ? ast_bridged_channel(c)->name : "<none>", 
01359       c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
01360       ( c-> data ? S_OR(c->data, "(Empty)") : "(None)"),
01361       (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
01362    
01363    if (pbx_builtin_serialize_variables(c, &out))
01364       ast_cli(a->fd,"      Variables:\n%s\n", ast_str_buffer(out));
01365    if (c->cdr && ast_cdr_serialize_variables(c->cdr, &out, '=', '\n', 1))
01366       ast_cli(a->fd,"  CDR Variables:\n%s\n", ast_str_buffer(out));
01367 #ifdef CHANNEL_TRACE
01368    trace_enabled = ast_channel_trace_is_enabled(c);
01369    ast_cli(a->fd, "  Context Trace: %s\n", trace_enabled ? "Enabled" : "Disabled");
01370    if (trace_enabled && ast_channel_trace_serialize(c, &out))
01371       ast_cli(a->fd, "          Trace:\n%s\n", ast_str_buffer(out));
01372 #endif
01373    ast_channel_unlock(c);
01374    return CLI_SUCCESS;
01375 }

static char* handle_showuptime ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 668 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_lastreloadtime, ast_startuptime, ast_tvnow(), ast_tvsub(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, print_uptimestr(), and ast_cli_entry::usage.

00669 {
00670    struct timeval curtime = ast_tvnow();
00671    int printsec;
00672 
00673    switch (cmd) {
00674    case CLI_INIT:
00675       e->command = "core show uptime [seconds]";
00676       e->usage =
00677          "Usage: core show uptime [seconds]\n"
00678          "       Shows Asterisk uptime information.\n"
00679          "       The seconds word returns the uptime in seconds only.\n";
00680       return NULL;
00681 
00682    case CLI_GENERATE:
00683       return NULL;
00684    }
00685    /* regular handler */
00686    if (a->argc == e->args && !strcasecmp(a->argv[e->args-1],"seconds"))
00687       printsec = 1;
00688    else if (a->argc == e->args-1)
00689       printsec = 0;
00690    else
00691       return CLI_SHOWUSAGE;
00692    if (ast_startuptime.tv_sec)
00693       print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
00694    if (ast_lastreloadtime.tv_sec)
00695       print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload", printsec);
00696    return CLI_SUCCESS;
00697 }

static char* handle_softhangup ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 915 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_channel_unlock, ast_cli(), ast_complete_channels(), ast_get_channel_by_name_locked(), ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, ast_cli_args::line, ast_cli_args::n, ast_channel::name, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

00916 {
00917    struct ast_channel *c=NULL;
00918 
00919    switch (cmd) {
00920    case CLI_INIT:
00921       e->command = "channel request hangup";
00922       e->usage =
00923          "Usage: channel request hangup <channel>\n"
00924          "       Request that a channel be hung up. The hangup takes effect\n"
00925          "       the next time the driver reads or writes from the channel\n";
00926       return NULL;
00927    case CLI_GENERATE:
00928       return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
00929    }
00930    if (a->argc != 4)
00931       return CLI_SHOWUSAGE;
00932    c = ast_get_channel_by_name_locked(a->argv[3]);
00933    if (c) {
00934       ast_cli(a->fd, "Requested Hangup on channel '%s'\n", c->name);
00935       ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
00936       ast_channel_unlock(c);
00937    } else
00938       ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
00939    return CLI_SUCCESS;
00940 }

static char* handle_unload ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 543 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), AST_FORCE_FIRM, AST_FORCE_HARD, AST_FORCE_SOFT, ast_module_helper(), ast_unload_resource(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, ast_cli_args::line, ast_cli_args::n, ast_cli_args::pos, s, ast_cli_entry::usage, and ast_cli_args::word.

00544 {
00545    /* "module unload mod_1 [mod_2 .. mod_N]" */
00546    int x;
00547    int force = AST_FORCE_SOFT;
00548    char *s;
00549 
00550    switch (cmd) {
00551    case CLI_INIT:
00552       e->command = "module unload";
00553       e->usage =
00554          "Usage: module unload [-f|-h] <module_1> [<module_2> ... ]\n"
00555          "       Unloads the specified module from Asterisk. The -f\n"
00556          "       option causes the module to be unloaded even if it is\n"
00557          "       in use (may cause a crash) and the -h module causes the\n"
00558          "       module to be unloaded even if the module says it cannot, \n"
00559          "       which almost always will cause a crash.\n";
00560       return NULL;
00561 
00562    case CLI_GENERATE:
00563       return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
00564    }
00565    if (a->argc < e->args + 1)
00566       return CLI_SHOWUSAGE;
00567    x = e->args;   /* first argument */
00568    s = a->argv[x];
00569    if (s[0] == '-') {
00570       if (s[1] == 'f')
00571          force = AST_FORCE_FIRM;
00572       else if (s[1] == 'h')
00573          force = AST_FORCE_HARD;
00574       else
00575          return CLI_SHOWUSAGE;
00576       if (a->argc < e->args + 2) /* need at least one module name */
00577          return CLI_SHOWUSAGE;
00578       x++;  /* skip this argument */
00579    }
00580 
00581    for (; x < a->argc; x++) {
00582       if (ast_unload_resource(a->argv[x], force)) {
00583          ast_cli(a->fd, "Unable to unload resource %s\n", a->argv[x]);
00584          return CLI_FAILURE;
00585       }
00586       ast_cli(a->fd, "Unloaded %s\n", a->argv[x]);
00587    }
00588 
00589    return CLI_SUCCESS;
00590 }

static char* handle_verbose ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 362 of file cli.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_calloc, ast_clear_flag, ast_cli(), ast_complete_source_filename(), ast_free, AST_OPT_FLAG_DEBUG_FILE, AST_OPT_FLAG_VERBOSE_FILE, ast_options, AST_RWLIST_EMPTY, AST_RWLIST_INSERT_TAIL, AST_RWLIST_REMOVE, AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_set_flag, ast_strdup, ast_strlen_zero(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_number(), debug, ast_cli_args::fd, ast_debug_file::filename, find_debug_file(), ast_debug_file::level, ast_cli_args::n, option_debug, option_verbose, ast_cli_args::pos, S_OR, and ast_cli_entry::usage.

00363 {
00364    int oldval;
00365    int newlevel;
00366    int atleast = 0;
00367    int fd = a->fd;
00368    int argc = a->argc;
00369    char **argv = a->argv;
00370    char *argv3 = a->argv ? S_OR(a->argv[3], "") : "";
00371    int *dst;
00372    char *what;
00373    struct debug_file_list *dfl;
00374    struct ast_debug_file *adf;
00375    char *fn;
00376 
00377    switch (cmd) {
00378    case CLI_INIT:
00379       e->command = "core set {debug|verbose}";
00380       e->usage =
00381 #if !defined(LOW_MEMORY)
00382          "Usage: core set {debug|verbose} [atleast] <level> [filename]\n"
00383 #else
00384          "Usage: core set {debug|verbose} [atleast] <level>\n"
00385 #endif
00386          "       core set {debug|verbose} off\n"
00387 #if !defined(LOW_MEMORY)
00388          "       Sets level of debug or verbose messages to be displayed or \n"
00389          "       sets a filename to display debug messages from.\n"
00390 #else
00391          "       Sets level of debug or verbose messages to be displayed.\n"
00392 #endif
00393          "  0 or off means no messages should be displayed.\n"
00394          "  Equivalent to -d[d[...]] or -v[v[v...]] on startup\n";
00395       return NULL;
00396 
00397    case CLI_GENERATE:
00398       if (a->pos == 3 || (a->pos == 4 && !strcasecmp(a->argv[3], "atleast"))) {
00399          char *pos = a->pos == 3 ? argv3 : S_OR(a->argv[4], "");
00400          int numbermatch = (ast_strlen_zero(pos) || strchr("123456789", pos[0])) ? 0 : 21;
00401          if (a->n < 21 && numbermatch == 0) {
00402             return complete_number(pos, 0, 0x7fffffff, a->n);
00403          } else if (pos[0] == '0') {
00404             if (a->n == 0) {
00405                return ast_strdup("0");
00406             } else {
00407                return NULL;
00408             }
00409          } else if (a->n == (21 - numbermatch)) {
00410             if (a->pos == 3 && !strncasecmp(argv3, "off", strlen(argv3))) {
00411                return ast_strdup("off");
00412             } else if (a->pos == 3 && !strncasecmp(argv3, "atleast", strlen(argv3))) {
00413                return ast_strdup("atleast");
00414             }
00415          } else if (a->n == (22 - numbermatch) && a->pos == 3 && ast_strlen_zero(argv3)) {
00416             return ast_strdup("atleast");
00417          }
00418 #if !defined(LOW_MEMORY)
00419       } else if (a->pos == 4 || (a->pos == 5 && !strcasecmp(argv3, "atleast"))) {
00420          return ast_complete_source_filename(a->pos == 4 ? S_OR(a->argv[4], "") : S_OR(a->argv[5], ""), a->n);
00421 #endif
00422       }
00423       return NULL;
00424    }
00425    /* all the above return, so we proceed with the handler.
00426     * we are guaranteed to be called with argc >= e->args;
00427     */
00428 
00429    if (argc <= e->args)
00430       return CLI_SHOWUSAGE;
00431    if (!strcasecmp(argv[e->args - 1], "debug")) {
00432       dst = &option_debug;
00433       oldval = option_debug;
00434       what = "Core debug";
00435    } else {
00436       dst = &option_verbose;
00437       oldval = option_verbose;
00438       what = "Verbosity";
00439    }
00440    if (argc == e->args + 1 && !strcasecmp(argv[e->args], "off")) {
00441       unsigned int debug = (*what == 'C');
00442       newlevel = 0;
00443 
00444       dfl = debug ? &debug_files : &verbose_files;
00445 
00446       AST_RWLIST_WRLOCK(dfl);
00447       while ((adf = AST_RWLIST_REMOVE_HEAD(dfl, entry)))
00448          ast_free(adf);
00449       ast_clear_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_FILE : AST_OPT_FLAG_VERBOSE_FILE);
00450       AST_RWLIST_UNLOCK(dfl);
00451 
00452       goto done;
00453    }
00454    if (!strcasecmp(argv[e->args], "atleast"))
00455       atleast = 1;
00456    if (argc != e->args + atleast + 1 && argc != e->args + atleast + 2)
00457       return CLI_SHOWUSAGE;
00458    if (sscanf(argv[e->args + atleast], "%30d", &newlevel) != 1)
00459       return CLI_SHOWUSAGE;
00460    if (argc == e->args + atleast + 2) {
00461       unsigned int debug = (*what == 'C');
00462       dfl = debug ? &debug_files : &verbose_files;
00463 
00464       fn = argv[e->args + atleast + 1];
00465 
00466       AST_RWLIST_WRLOCK(dfl);
00467 
00468       if ((adf = find_debug_file(fn, debug)) && !newlevel) {
00469          AST_RWLIST_REMOVE(dfl, adf, entry);
00470          if (AST_RWLIST_EMPTY(dfl))
00471             ast_clear_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_FILE : AST_OPT_FLAG_VERBOSE_FILE);
00472          AST_RWLIST_UNLOCK(dfl);
00473          ast_cli(fd, "%s was %d and has been set to 0 for '%s'\n", what, adf->level, fn);
00474          ast_free(adf);
00475          return CLI_SUCCESS;
00476       }
00477 
00478       if (adf) {
00479          if ((atleast && newlevel < adf->level) || adf->level == newlevel) {
00480             ast_cli(fd, "%s is %d for '%s'\n", what, adf->level, fn);
00481             AST_RWLIST_UNLOCK(dfl);
00482             return CLI_SUCCESS;
00483          }
00484       } else if (!(adf = ast_calloc(1, sizeof(*adf) + strlen(fn) + 1))) {
00485          AST_RWLIST_UNLOCK(dfl);
00486          return CLI_FAILURE;
00487       }
00488 
00489       oldval = adf->level;
00490       adf->level = newlevel;
00491       strcpy(adf->filename, fn);
00492 
00493       ast_set_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_FILE : AST_OPT_FLAG_VERBOSE_FILE);
00494 
00495       AST_RWLIST_INSERT_TAIL(dfl, adf, entry);
00496       AST_RWLIST_UNLOCK(dfl);
00497 
00498       ast_cli(fd, "%s was %d and has been set to %d for '%s'\n", what, oldval, adf->level, adf->filename);
00499 
00500       return CLI_SUCCESS;
00501    }
00502 
00503 done:
00504    if (!atleast || newlevel > *dst)
00505       *dst = newlevel;
00506    if (oldval > 0 && *dst == 0)
00507       ast_cli(fd, "%s is now OFF\n", what);
00508    else if (*dst > 0) {
00509       if (oldval == *dst)
00510          ast_cli(fd, "%s is at least %d\n", what, *dst);
00511       else
00512          ast_cli(fd, "%s was %d and is now %d\n", what, oldval, *dst);
00513    }
00514 
00515    return CLI_SUCCESS;
00516 }

static char* help1 ( int  fd,
char *  match[],
int  locked 
) [static]

helper for final part of handle_help if locked = 1, assume the list is already locked

Definition at line 1978 of file cli.c.

References ast_cli_entry::_full_cmd, ast_cli(), ast_join(), AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, cli_next(), CLI_SUCCESS, len(), S_OR, and ast_cli_entry::summary.

Referenced by handle_help().

01979 {
01980    char matchstr[80] = "";
01981    struct ast_cli_entry *e = NULL;
01982    int len = 0;
01983    int found = 0;
01984 
01985    if (match) {
01986       ast_join(matchstr, sizeof(matchstr), match);
01987       len = strlen(matchstr);
01988    }
01989    if (!locked)
01990       AST_RWLIST_RDLOCK(&helpers);
01991    while ( (e = cli_next(e)) ) {
01992       /* Hide commands that start with '_' */
01993       if (e->_full_cmd[0] == '_')
01994          continue;
01995       if (match && strncasecmp(matchstr, e->_full_cmd, len))
01996          continue;
01997       ast_cli(fd, "%30.30s %s\n", e->_full_cmd, S_OR(e->summary, "<no description available>"));
01998       found++;
01999    }
02000    if (!locked)
02001       AST_RWLIST_UNLOCK(&helpers);
02002    if (!found && matchstr[0])
02003       ast_cli(fd, "No such command '%s'.\n", matchstr);
02004    return CLI_SUCCESS;
02005 }

static char* is_prefix ( const char *  word,
const char *  token,
int  pos,
int *  actual 
) [static]

if word is a valid prefix for token, returns the pos-th match as a malloced string, or NULL otherwise. Always tell in *actual how many matches we got.

Definition at line 1749 of file cli.c.

References ast_strdup, ast_strdupa, ast_strlen_zero(), s, and strsep().

Referenced by __ast_cli_generator().

01751 {
01752    int lw;
01753    char *s, *t1;
01754 
01755    *actual = 0;
01756    if (ast_strlen_zero(token))
01757       return NULL;
01758    if (ast_strlen_zero(word))
01759       word = "";  /* dummy */
01760    lw = strlen(word);
01761    if (strcspn(word, cli_rsvd) != lw)
01762       return NULL;   /* no match if word has reserved chars */
01763    if (strchr(cli_rsvd, token[0]) == NULL) { /* regular match */
01764       if (strncasecmp(token, word, lw))   /* no match */
01765          return NULL;
01766       *actual = 1;
01767       return (pos != 0) ? NULL : ast_strdup(token);
01768    }
01769    /* now handle regexp match */
01770 
01771    /* Wildcard always matches, so we never do is_prefix on them */
01772 
01773    t1 = ast_strdupa(token + 1);  /* copy, skipping first char */
01774    while (pos >= 0 && (s = strsep(&t1, cli_rsvd)) && *s) {
01775       if (*s == '%') /* wildcard */
01776          continue;
01777       if (strncasecmp(s, word, lw)) /* no match */
01778          continue;
01779       (*actual)++;
01780       if (pos-- == 0)
01781          return ast_strdup(s);
01782    }
01783    return NULL;
01784 }

static int modlist_modentry ( const char *  module,
const char *  description,
int  usecnt,
const char *  like 
) [static]

Definition at line 598 of file cli.c.

References ast_cli(), MODLIST_FORMAT, and strcasestr().

Referenced by handle_modlist().

00599 {
00600    /* Comparing the like with the module */
00601    if (strcasestr(module, like) ) {
00602       ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
00603       return 1;
00604    } 
00605    return 0;
00606 }

static int more_words ( char *const *  dst  )  [static]

returns true if there are more words to match

Definition at line 2184 of file cli.c.

Referenced by __ast_cli_generator().

02185 {
02186    int i;
02187    for (i = 0; dst[i]; i++) {
02188       if (dst[i][0] != '[')
02189          return -1;
02190    }
02191    return 0;
02192 }

static char* parse_args ( const char *  s,
int *  argc,
char *  argv[],
int  max,
int *  trailingwhitespace 
) [static]

Definition at line 2053 of file cli.c.

References ast_log(), ast_strdup, dummy(), and LOG_WARNING.

Referenced by __ast_cli_generator(), and ast_cli_command_full().

02054 {
02055    char *duplicate, *cur;
02056    int x = 0;
02057    int quoted = 0;
02058    int escaped = 0;
02059    int whitespace = 1;
02060    int dummy = 0;
02061 
02062    if (trailingwhitespace == NULL)
02063       trailingwhitespace = &dummy;
02064    *trailingwhitespace = 0;
02065    if (s == NULL) /* invalid, though! */
02066       return NULL;
02067    /* make a copy to store the parsed string */
02068    if (!(duplicate = ast_strdup(s)))
02069       return NULL;
02070 
02071    cur = duplicate;
02072    /* scan the original string copying into cur when needed */
02073    for (; *s ; s++) {
02074       if (x >= max - 1) {
02075          ast_log(LOG_WARNING, "Too many arguments, truncating at %s\n", s);
02076          break;
02077       }
02078       if (*s == '"' && !escaped) {
02079          quoted = !quoted;
02080          if (quoted && whitespace) {
02081             /* start a quoted string from previous whitespace: new argument */
02082             argv[x++] = cur;
02083             whitespace = 0;
02084          }
02085       } else if ((*s == ' ' || *s == '\t') && !(quoted || escaped)) {
02086          /* If we are not already in whitespace, and not in a quoted string or
02087             processing an escape sequence, and just entered whitespace, then
02088             finalize the previous argument and remember that we are in whitespace
02089          */
02090          if (!whitespace) {
02091             *cur++ = '\0';
02092             whitespace = 1;
02093          }
02094       } else if (*s == '\\' && !escaped) {
02095          escaped = 1;
02096       } else {
02097          if (whitespace) {
02098             /* we leave whitespace, and are not quoted. So it's a new argument */
02099             argv[x++] = cur;
02100             whitespace = 0;
02101          }
02102          *cur++ = *s;
02103          escaped = 0;
02104       }
02105    }
02106    /* Null terminate */
02107    *cur++ = '\0';
02108    /* XXX put a NULL in the last argument, because some functions that take
02109     * the array may want a null-terminated array.
02110     * argc still reflects the number of non-NULL entries.
02111     */
02112    argv[x] = NULL;
02113    *argc = x;
02114    *trailingwhitespace = whitespace;
02115    return duplicate;
02116 }

static void print_uptimestr ( int  fd,
struct timeval  timeval,
const char *  prefix,
int  printsec 
) [static]

Definition at line 608 of file cli.c.

References ast_cli(), ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_str_strlen(), DAY, ESS, HOUR, MINUTE, NEEDCOMMA, WEEK, and YEAR.

Referenced by handle_showcalls(), and handle_showuptime().

00609 {
00610    int x; /* the main part - years, weeks, etc. */
00611    struct ast_str *out;
00612 
00613 #define SECOND (1)
00614 #define MINUTE (SECOND*60)
00615 #define HOUR (MINUTE*60)
00616 #define DAY (HOUR*24)
00617 #define WEEK (DAY*7)
00618 #define YEAR (DAY*365)
00619 #define NEEDCOMMA(x) ((x)? ",": "") /* define if we need a comma */
00620    if (timeval.tv_sec < 0) /* invalid, nothing to show */
00621       return;
00622 
00623    if (printsec)  {  /* plain seconds output */
00624       ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval.tv_sec);
00625       return;
00626    }
00627    out = ast_str_alloca(256);
00628    if (timeval.tv_sec > YEAR) {
00629       x = (timeval.tv_sec / YEAR);
00630       timeval.tv_sec -= (x * YEAR);
00631       ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00632    }
00633    if (timeval.tv_sec > WEEK) {
00634       x = (timeval.tv_sec / WEEK);
00635       timeval.tv_sec -= (x * WEEK);
00636       ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00637    }
00638    if (timeval.tv_sec > DAY) {
00639       x = (timeval.tv_sec / DAY);
00640       timeval.tv_sec -= (x * DAY);
00641       ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00642    }
00643    if (timeval.tv_sec > HOUR) {
00644       x = (timeval.tv_sec / HOUR);
00645       timeval.tv_sec -= (x * HOUR);
00646       ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00647    }
00648    if (timeval.tv_sec > MINUTE) {
00649       x = (timeval.tv_sec / MINUTE);
00650       timeval.tv_sec -= (x * MINUTE);
00651       ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00652    }
00653    x = timeval.tv_sec;
00654    if (x > 0 || ast_str_strlen(out) == 0) /* if there is nothing, print 0 seconds */
00655       ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
00656    ast_cli(fd, "%s: %s\n", prefix, ast_str_buffer(out));
00657 }

static int set_full_cmd ( struct ast_cli_entry *  e  )  [static]

initialize the _full_cmd string and related parameters, return 0 on success, -1 on error.

Definition at line 1546 of file cli.c.

References ast_cli_entry::_full_cmd, ast_cli_entry::args, ast_join(), ast_log(), ast_strdup, buf, ast_cli_entry::cmda, ast_cli_entry::cmdlen, and LOG_WARNING.

Referenced by __ast_cli_register().

01547 {
01548    int i;
01549    char buf[80];
01550 
01551    ast_join(buf, sizeof(buf), e->cmda);
01552    e->_full_cmd = ast_strdup(buf);
01553    if (!e->_full_cmd) {
01554       ast_log(LOG_WARNING, "-- cannot allocate <%s>\n", buf);
01555       return -1;
01556    }
01557    e->cmdlen = strcspn(e->_full_cmd, cli_rsvd);
01558    for (i = 0; e->cmda[i]; i++)
01559       ;
01560    e->args = i;
01561    return 0;
01562 }

static int word_match ( const char *  cmd,
const char *  cli_word 
) [static]

match a word in the CLI entry. returns -1 on mismatch, 0 on match of an optional word, 1 on match of a full word.

The pattern can be any_word match for equal [foo|bar|baz] optionally, one of these words {foo|bar|baz} exactly, one of these words % any word

Definition at line 1720 of file cli.c.

References ast_strlen_zero(), and strcasestr().

Referenced by __ast_cli_generator(), and find_cli().

01721 {
01722    int l;
01723    char *pos;
01724 
01725    if (ast_strlen_zero(cmd) || ast_strlen_zero(cli_word))
01726       return -1;
01727    if (!strchr(cli_rsvd, cli_word[0])) /* normal match */
01728       return (strcasecmp(cmd, cli_word) == 0) ? 1 : -1;
01729    /* regexp match, takes [foo|bar] or {foo|bar} */
01730    l = strlen(cmd);
01731    /* wildcard match - will extend in the future */
01732    if (l > 0 && cli_word[0] == '%') {
01733       return 1;   /* wildcard */
01734    }
01735    pos = strcasestr(cli_word, cmd);
01736    if (pos == NULL) /* not found, say ok if optional */
01737       return cli_word[0] == '[' ? 0 : -1;
01738    if (pos == cli_word) /* no valid match at the beginning */
01739       return -1;
01740    if (strchr(cli_rsvd, pos[-1]) && strchr(cli_rsvd, pos[l]))
01741       return 1;   /* valid match */
01742    return -1;  /* not found */
01743 }


Variable Documentation

struct ast_threadstorage ast_cli_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_ast_cli_buf , .custom_init = NULL , } [static]

Definition at line 95 of file cli.c.

struct ast_cli_entry cli_cli[] [static]

Definition at line 1492 of file cli.c.

int cli_default_perm = 1 [static]

Default permissions value 1=Permit 0=Deny.

Definition at line 71 of file cli.c.

const char cli_rsvd[] = "[]{}|*%" [static]

Some regexp characters in cli arguments are reserved and used as separators.

Definition at line 1540 of file cli.c.

int climodentryfd = -1 [static]

Definition at line 596 of file cli.c.

ast_mutex_t climodentrylock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ) [static]

Definition at line 595 of file cli.c.

Referenced by handle_modlist().

struct debug_file_list debug_files [static]

list of filenames and their debug settings

Definition at line 91 of file cli.c.

const char perms_config[] = "cli_permissions.conf" [static]

CLI permissions config file.

Definition at line 69 of file cli.c.

Referenced by ast_cli_perms_init().

ast_mutex_t permsconfiglock = ((ast_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ) [static]

mutex used to prevent a user from running the 'cli reload permissions' command while it is already running.

Definition at line 75 of file cli.c.

struct debug_file_list verbose_files [static]

list of filenames and their verbose settings

Definition at line 93 of file cli.c.


Generated on Wed Aug 11 01:07:37 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9