Sat Mar 26 11:17:35 2011

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 613 of file cli.c.

Referenced by modlist_modentry().

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

Definition at line 614 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 2218 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().

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

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

Definition at line 1905 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().

01906 {
01907    struct ast_cli_entry *cur;
01908    int i, lf, ret = -1;
01909 
01910    struct ast_cli_args a;  /* fake argument */
01911    char **dst = (char **)e->cmda;   /* need to cast as the entry is readonly */
01912    char *s;
01913 
01914    memset(&a, '\0', sizeof(a));
01915    e->handler(e, CLI_INIT, &a);
01916    /* XXX check that usage and command are filled up */
01917    s = ast_skip_blanks(e->command);
01918    s = e->command = ast_strdup(s);
01919    for (i=0; !ast_strlen_zero(s) && i < AST_MAX_CMD_LEN-1; i++) {
01920       *dst++ = s; /* store string */
01921       s = ast_skip_nonblanks(s);
01922       if (*s == '\0')   /* we are done */
01923          break;
01924       *s++ = '\0';
01925       s = ast_skip_blanks(s);
01926    }
01927    *dst++ = NULL;
01928    
01929    AST_RWLIST_WRLOCK(&helpers);
01930    
01931    if (find_cli(e->cmda, 1)) {
01932       ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", S_OR(e->_full_cmd, e->command));
01933       goto done;
01934    }
01935    if (set_full_cmd(e))
01936       goto done;
01937 
01938    lf = e->cmdlen;
01939    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&helpers, cur, list) {
01940       int len = cur->cmdlen;
01941       if (lf < len)
01942          len = lf;
01943       if (strncasecmp(e->_full_cmd, cur->_full_cmd, len) < 0) {
01944          AST_RWLIST_INSERT_BEFORE_CURRENT(e, list); 
01945          break;
01946       }
01947    }
01948    AST_RWLIST_TRAVERSE_SAFE_END;
01949 
01950    if (!cur)
01951       AST_RWLIST_INSERT_TAIL(&helpers, e, list); 
01952    ret = 0; /* success */
01953 
01954 done:
01955    AST_RWLIST_UNLOCK(&helpers);
01956 
01957    return ret;
01958 }

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

Definition at line 1883 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().

01884 {
01885    if (e->inuse) {
01886       ast_log(LOG_WARNING, "Can't remove command that is in use\n");
01887    } else {
01888       AST_RWLIST_WRLOCK(&helpers);
01889       AST_RWLIST_REMOVE(&helpers, e, list);
01890       AST_RWLIST_UNLOCK(&helpers);
01891       ast_free(e->_full_cmd);
01892       e->_full_cmd = NULL;
01893       if (e->handler) {
01894          /* this is a new-style entry. Reset fields and free memory. */
01895          char *cmda = (char *) e->cmda;
01896          memset(cmda, '\0', sizeof(e->cmda));
01897          ast_free(e->command);
01898          e->command = NULL;
01899          e->usage = NULL;
01900       }
01901    }
01902    return 0;
01903 }

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 1725 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 2306 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().

02307 {
02308    char *args[AST_MAX_ARGS + 1];
02309    struct ast_cli_entry *e;
02310    int x;
02311    char *duplicate = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
02312    char tmp[AST_MAX_ARGS + 1];
02313    char *retval = NULL;
02314    struct ast_cli_args a = {
02315       .fd = fd, .argc = x, .argv = args+1 };
02316 
02317    if (duplicate == NULL)
02318       return -1;
02319 
02320    if (x < 1)  /* We need at least one entry, otherwise ignore */
02321       goto done;
02322 
02323    AST_RWLIST_RDLOCK(&helpers);
02324    e = find_cli(args + 1, 0);
02325    if (e)
02326       ast_atomic_fetchadd_int(&e->inuse, 1);
02327    AST_RWLIST_UNLOCK(&helpers);
02328    if (e == NULL) {
02329       ast_cli(fd, "No such command '%s' (type 'core show help %s' for other possible commands)\n", s, find_best(args + 1));
02330       goto done;
02331    }
02332 
02333    ast_join(tmp, sizeof(tmp), args + 1);
02334    /* Check if the user has rights to run this command. */
02335    if (!cli_has_permissions(uid, gid, tmp)) {
02336       ast_cli(fd, "You don't have permissions to run '%s' command\n", tmp);
02337       ast_free(duplicate);
02338       return 0;
02339    }
02340 
02341    /*
02342     * Within the handler, argv[-1] contains a pointer to the ast_cli_entry.
02343     * Remember that the array returned by parse_args is NULL-terminated.
02344     */
02345    args[0] = (char *)e;
02346 
02347    retval = e->handler(e, CLI_HANDLER, &a);
02348 
02349    if (retval == CLI_SHOWUSAGE) {
02350       ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
02351    } else {
02352       if (retval == CLI_FAILURE)
02353          ast_cli(fd, "Command '%s' failed.\n", s);
02354    }
02355    ast_atomic_fetchadd_int(&e->inuse, -1);
02356 done:
02357    ast_free(duplicate);
02358    return 0;
02359 }

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 2361 of file cli.c.

References ast_cli_command_full().

Referenced by netconsole().

02362 {
02363    char cmd[512];
02364    int x, y = 0, count = 0;
02365 
02366    for (x = 0; x < size; x++) {
02367       cmd[y] = s[x];
02368       y++;
02369       if (s[x] == '\0') {
02370          ast_cli_command_full(uid, gid, fd, cmd);
02371          y = 0;
02372          count++;
02373       }
02374    }
02375    return count;
02376 }

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 1402 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().

01403 {
01404    int i, which = 0, len;
01405    len = ast_strlen_zero(word) ? 0 : strlen(word);
01406 
01407    for (i = 0; choices[i]; i++) {
01408       if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state)
01409          return ast_strdup(choices[i]);
01410    }
01411    return NULL;
01412 }

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 2157 of file cli.c.

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

Referenced by cli_complete(), and handle_commandmatchesarray().

02158 {
02159    char **match_list = NULL, *retstr, *prevstr;
02160    size_t match_list_len, max_equal, which, i;
02161    int matches = 0;
02162 
02163    /* leave entry 0 free for the longest common substring */
02164    match_list_len = 1;
02165    while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
02166       if (matches + 1 >= match_list_len) {
02167          match_list_len <<= 1;
02168          if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
02169             return NULL;
02170       }
02171       match_list[++matches] = retstr;
02172    }
02173 
02174    if (!match_list)
02175       return match_list; /* NULL */
02176 
02177    /* Find the longest substring that is common to all results
02178     * (it is a candidate for completion), and store a copy in entry 0.
02179     */
02180    prevstr = match_list[1];
02181    max_equal = strlen(prevstr);
02182    for (which = 2; which <= matches; which++) {
02183       for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
02184          continue;
02185       max_equal = i;
02186    }
02187 
02188    if (!(retstr = ast_malloc(max_equal + 1)))
02189       return NULL;
02190    
02191    ast_copy_string(retstr, match_list[1], max_equal + 1);
02192    match_list[0] = retstr;
02193 
02194    /* ensure that the array is NULL terminated */
02195    if (matches + 1 >= match_list_len) {
02196       if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list))))
02197          return NULL;
02198    }
02199    match_list[matches + 1] = NULL;
02200 
02201    return match_list;
02202 }

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 2301 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().

02302 {
02303    return __ast_cli_generator(text, word, state, 1);
02304 }

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

Return the number of unique matches for the generator.

Definition at line 2140 of file cli.c.

References ast_cli_generator(), ast_free, and buf.

Referenced by handle_commandnummatches().

02141 {
02142    int matches = 0, i = 0;
02143    char *buf = NULL, *oldbuf = NULL;
02144 
02145    while ((buf = ast_cli_generator(text, word, i++))) {
02146       if (!oldbuf || strcmp(buf,oldbuf))
02147          matches++;
02148       if (oldbuf)
02149          ast_free(oldbuf);
02150       oldbuf = buf;
02151    }
02152    if (oldbuf)
02153       ast_free(oldbuf);
02154    return matches;
02155 }

int ast_cli_perms_init ( int  reload  ) 

Provided by cli.c

Definition at line 1602 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().

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

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 1967 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().

01968 {
01969    return __ast_cli_register(e, NULL);
01970 }

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 1975 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().

01976 {
01977    int i, res = 0;
01978 
01979    for (i = 0; i < len; i++)
01980       res |= ast_cli_register(e + i);
01981 
01982    return res;
01983 }

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 1961 of file cli.c.

References __ast_cli_unregister().

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

01962 {
01963    return __ast_cli_unregister(e, NULL);
01964 }

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 1985 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().

01986 {
01987    int i, res = 0;
01988 
01989    for (i = 0; i < len; i++)
01990       res |= ast_cli_unregister(e + i);
01991 
01992    return res;
01993 }

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 1414 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().

01415 {
01416    struct ast_channel *c = NULL;
01417    int which = 0;
01418    int wordlen;
01419    char notfound = '\0';
01420    char *ret = &notfound; /* so NULL can break the loop */
01421 
01422    if (pos != rpos)
01423       return NULL;
01424 
01425    wordlen = strlen(word); 
01426 
01427    while (ret == &notfound && (c = ast_channel_walk_locked(c))) {
01428       if (!strncasecmp(word, c->name, wordlen) && ++which > state)
01429          ret = ast_strdup(c->name);
01430       ast_channel_unlock(c);
01431    }
01432    return ret == &notfound ? NULL : ret;
01433 }

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 680 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().

00681 {
00682    if (e) {
00683       return AST_LIST_NEXT(e, list);
00684    } else {
00685       return AST_LIST_FIRST(&helpers);
00686    }
00687 }

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 1586 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().

01587 {
01588    struct cli_perm *perm;
01589    struct usergroup_cli_perm *user_perm;
01590 
01591    AST_RWLIST_WRLOCK(&cli_perms);
01592    while ((user_perm = AST_LIST_REMOVE_HEAD(&cli_perms, list))) {
01593       while ((perm = AST_LIST_REMOVE_HEAD(user_perm->perms, list))) {
01594          ast_free(perm->command);
01595          ast_free(perm);
01596       }
01597       ast_free(user_perm);
01598    }
01599    AST_RWLIST_UNLOCK(&cli_perms);
01600 }

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

Definition at line 1864 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().

01865 {
01866    static char cmdline[80];
01867    int x;
01868    /* See how close we get, then print the candidate */
01869    char *myargv[AST_MAX_CMD_LEN];
01870    for (x=0;x<AST_MAX_CMD_LEN;x++)
01871       myargv[x]=NULL;
01872    AST_RWLIST_RDLOCK(&helpers);
01873    for (x=0;argv[x];x++) {
01874       myargv[x] = argv[x];
01875       if (!find_cli(myargv, -1))
01876          break;
01877    }
01878    AST_RWLIST_UNLOCK(&helpers);
01879    ast_join(cmdline, sizeof(cmdline), myargv);
01880    return cmdline;
01881 }

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

Definition at line 1820 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().

01821 {
01822    int matchlen = -1;   /* length of longest match so far */
01823    struct ast_cli_entry *cand = NULL, *e=NULL;
01824 
01825    while ( (e = cli_next(e)) ) {
01826       /* word-by word regexp comparison */
01827       char * const *src = cmds;
01828       char * const *dst = e->cmda;
01829       int n = 0;
01830       for (;; dst++, src += n) {
01831          n = word_match(*src, *dst);
01832          if (n < 0)
01833             break;
01834       }
01835       if (ast_strlen_zero(*dst) || ((*dst)[0] == '[' && ast_strlen_zero(dst[1]))) {
01836          /* no more words in 'e' */
01837          if (ast_strlen_zero(*src)) /* exact match, cannot do better */
01838             break;
01839          /* Here, cmds has more words than the entry 'e' */
01840          if (match_type != 0) /* but we look for almost exact match... */
01841             continue;   /* so we skip this one. */
01842          /* otherwise we like it (case 0) */
01843       } else { /* still words in 'e' */
01844          if (ast_strlen_zero(*src))
01845             continue; /* cmds is shorter than 'e', not good */
01846          /* Here we have leftover words in cmds and 'e',
01847           * but there is a mismatch. We only accept this one if match_type == -1
01848           * and this is the last word for both.
01849           */
01850          if (match_type != -1 || !ast_strlen_zero(src[1]) ||
01851              !ast_strlen_zero(dst[1])) /* not the one we look for */
01852             continue;
01853          /* good, we are in case match_type == -1 and mismatch on last word */
01854       }
01855       if (src - cmds > matchlen) {  /* remember the candidate */
01856          matchlen = src - cmds;
01857          cand = e;
01858       }
01859    }
01860 
01861    return e ? e : cand;
01862 }

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 1435 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.

01436 {
01437 #define FORMAT_STRING  "%-25s  %-20s  %-20s\n"
01438 
01439    struct ast_group_info *gi = NULL;
01440    int numchans = 0;
01441    regex_t regexbuf;
01442    int havepattern = 0;
01443 
01444    switch (cmd) {
01445    case CLI_INIT:
01446       e->command = "group show channels";
01447       e->usage = 
01448          "Usage: group show channels [pattern]\n"
01449          "       Lists all currently active channels with channel group(s) specified.\n"
01450          "       Optional regular expression pattern is matched to group names for each\n"
01451          "       channel.\n";
01452       return NULL;
01453    case CLI_GENERATE:
01454       return NULL;
01455    }
01456 
01457    if (a->argc < 3 || a->argc > 4)
01458       return CLI_SHOWUSAGE;
01459    
01460    if (a->argc == 4) {
01461       if (regcomp(&regexbuf, a->argv[3], REG_EXTENDED | REG_NOSUB))
01462          return CLI_SHOWUSAGE;
01463       havepattern = 1;
01464    }
01465 
01466    ast_cli(a->fd, FORMAT_STRING, "Channel", "Group", "Category");
01467 
01468    ast_app_group_list_rdlock();
01469    
01470    gi = ast_app_group_list_head();
01471    while (gi) {
01472       if (!havepattern || !regexec(&regexbuf, gi->group, 0, NULL, 0)) {
01473          ast_cli(a->fd, FORMAT_STRING, gi->chan->name, gi->group, (ast_strlen_zero(gi->category) ? "(default)" : gi->category));
01474          numchans++;
01475       }
01476       gi = AST_LIST_NEXT(gi, group_list);
01477    }
01478    
01479    ast_app_group_list_unlock();
01480    
01481    if (havepattern)
01482       regfree(&regexbuf);
01483 
01484    ast_cli(a->fd, "%d active channel%s\n", numchans, ESS(numchans));
01485    return CLI_SUCCESS;
01486 #undef FORMAT_STRING
01487 }

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

Definition at line 814 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.

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

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 1028 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.

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

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 1009 of file cli.c.

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

01010 {
01011    switch (cmd) {
01012    case CLI_INIT:
01013       e->command = "cli reload permissions";
01014       e->usage =
01015          "Usage: cli reload permissions\n"
01016          "       Reload the 'cli_permissions.conf' file.\n";
01017       return NULL;
01018    case CLI_GENERATE:
01019       return NULL;
01020    }
01021 
01022    ast_cli_perms_init(1);
01023 
01024    return CLI_SUCCESS;
01025 }

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 964 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.

00965 {
00966    struct usergroup_cli_perm *cp;
00967    struct cli_perm *perm;
00968    struct passwd *pw = NULL;
00969    struct group *gr = NULL;
00970 
00971    switch (cmd) {
00972    case CLI_INIT:
00973       e->command = "cli show permissions";
00974       e->usage =
00975          "Usage: cli show permissions\n"
00976          "       Shows CLI configured permissions.\n";
00977       return NULL;
00978    case CLI_GENERATE:
00979       return NULL;
00980    }
00981 
00982    AST_RWLIST_RDLOCK(&cli_perms);
00983    AST_LIST_TRAVERSE(&cli_perms, cp, list) {
00984       if (cp->uid >= 0) {
00985          pw = getpwuid(cp->uid);
00986          if (pw) {
00987             ast_cli(a->fd, "user: %s [uid=%d]\n", pw->pw_name, cp->uid);
00988          }
00989       } else {
00990          gr = getgrgid(cp->gid);
00991          if (gr) {
00992             ast_cli(a->fd, "group: %s [gid=%d]\n", gr->gr_name, cp->gid);
00993          }
00994       }
00995       ast_cli(a->fd, "Permissions:\n");
00996       if (cp->perms) {
00997          AST_LIST_TRAVERSE(cp->perms, perm, list) {
00998             ast_cli(a->fd, "\t%s -> %s\n", perm->permit ? "permit" : "deny", perm->command);
00999          }
01000       }
01001       ast_cli(a->fd, "\n");
01002    }
01003    AST_RWLIST_UNLOCK(&cli_perms);
01004 
01005    return CLI_SUCCESS;
01006 }

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

Definition at line 1489 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.

01490 {
01491    switch (cmd) {
01492    case CLI_INIT:
01493       e->command = "core waitfullybooted";
01494       e->usage =
01495          "Usage: core waitfullybooted\n"
01496          "  Wait until Asterisk has fully booted.\n";
01497       return NULL;
01498    case CLI_GENERATE:
01499       return NULL;
01500    }
01501 
01502    while (!ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
01503       usleep(100);
01504    }
01505 
01506    ast_cli(a->fd, "Asterisk has fully booted.\n");
01507 
01508    return CLI_SUCCESS;
01509 }

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

Definition at line 1186 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.

01187 {
01188    char *buf;
01189    switch (cmd) {
01190    case CLI_INIT:
01191       e->command = "_command complete";
01192       e->usage = 
01193          "Usage: _command complete \"<line>\" text state\n"
01194          "       This function is used internally to help with command completion and should.\n"
01195          "       never be called by the user directly.\n";
01196       return NULL;
01197    case CLI_GENERATE:
01198       return NULL;
01199    }
01200    if (a->argc != 5)
01201       return CLI_SHOWUSAGE;
01202    buf = __ast_cli_generator(a->argv[2], a->argv[3], atoi(a->argv[4]), 0);
01203    if (buf) {
01204       ast_cli(a->fd, "%s", buf);
01205       ast_free(buf);
01206    } else
01207       ast_cli(a->fd, "NULL\n");
01208    return CLI_SUCCESS;
01209 }

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

Definition at line 1105 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.

01106 {
01107    char *buf, *obuf;
01108    int buflen = 2048;
01109    int len = 0;
01110    char **matches;
01111    int x, matchlen;
01112    
01113    switch (cmd) {
01114    case CLI_INIT:
01115       e->command = "_command matchesarray";
01116       e->usage = 
01117          "Usage: _command matchesarray \"<line>\" text \n"
01118          "       This function is used internally to help with command completion and should.\n"
01119          "       never be called by the user directly.\n";
01120       return NULL;
01121    case CLI_GENERATE:
01122       return NULL;
01123    }
01124 
01125    if (a->argc != 4)
01126       return CLI_SHOWUSAGE;
01127    if (!(buf = ast_malloc(buflen)))
01128       return CLI_FAILURE;
01129    buf[len] = '\0';
01130    matches = ast_cli_completion_matches(a->argv[2], a->argv[3]);
01131    if (matches) {
01132       for (x=0; matches[x]; x++) {
01133          matchlen = strlen(matches[x]) + 1;
01134          if (len + matchlen >= buflen) {
01135             buflen += matchlen * 3;
01136             obuf = buf;
01137             if (!(buf = ast_realloc(obuf, buflen))) 
01138                /* Memory allocation failure...  Just free old buffer and be done */
01139                ast_free(obuf);
01140          }
01141          if (buf)
01142             len += sprintf( buf + len, "%s ", matches[x]);
01143          ast_free(matches[x]);
01144          matches[x] = NULL;
01145       }
01146       ast_free(matches);
01147    }
01148 
01149    if (buf) {
01150       ast_cli(a->fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
01151       ast_free(buf);
01152    } else
01153       ast_cli(a->fd, "NULL\n");
01154 
01155    return CLI_SUCCESS;
01156 }

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

Definition at line 1160 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.

01161 {
01162    int matches = 0;
01163 
01164    switch (cmd) {
01165    case CLI_INIT:
01166       e->command = "_command nummatches";
01167       e->usage = 
01168          "Usage: _command nummatches \"<line>\" text \n"
01169          "       This function is used internally to help with command completion and should.\n"
01170          "       never be called by the user directly.\n";
01171       return NULL;
01172    case CLI_GENERATE:
01173       return NULL;
01174    }
01175 
01176    if (a->argc != 4)
01177       return CLI_SHOWUSAGE;
01178 
01179    matches = ast_cli_generatornummatches(a->argv[2], a->argv[3]);
01180 
01181    ast_cli(a->fd, "%d", matches);
01182 
01183    return CLI_SUCCESS;
01184 }

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

Definition at line 1211 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().

01212 {
01213    struct ast_channel *c = NULL;
01214    int is_all, is_off = 0;
01215 
01216    switch (cmd) {
01217    case CLI_INIT:
01218       e->command = "core set debug channel";
01219       e->usage =
01220          "Usage: core set debug channel <all|channel> [off]\n"
01221          "       Enables/disables debugging on all or on a specific channel.\n";
01222       return NULL;
01223 
01224    case CLI_GENERATE:
01225       /* XXX remember to handle the optional "off" */
01226       if (a->pos != e->args)
01227          return NULL;
01228       return a->n == 0 ? ast_strdup("all") : ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
01229    }
01230    /* 'core set debug channel {all|chan_id}' */
01231    if (a->argc == e->args + 2) {
01232       if (!strcasecmp(a->argv[e->args + 1], "off"))
01233          is_off = 1;
01234       else
01235          return CLI_SHOWUSAGE;
01236    } else if (a->argc != e->args + 1)
01237       return CLI_SHOWUSAGE;
01238 
01239    is_all = !strcasecmp("all", a->argv[e->args]);
01240    if (is_all) {
01241       if (is_off) {
01242          global_fin &= ~DEBUGCHAN_FLAG;
01243          global_fout &= ~DEBUGCHAN_FLAG;
01244       } else {
01245          global_fin |= DEBUGCHAN_FLAG;
01246          global_fout |= DEBUGCHAN_FLAG;
01247       }
01248       c = ast_channel_walk_locked(NULL);
01249    } else {
01250       c = ast_get_channel_by_name_locked(a->argv[e->args]);
01251       if (c == NULL)
01252          ast_cli(a->fd, "No such channel %s\n", a->argv[e->args]);
01253    }
01254    while (c) {
01255       if (!(c->fin & DEBUGCHAN_FLAG) || !(c->fout & DEBUGCHAN_FLAG)) {
01256          if (is_off) {
01257             c->fin &= ~DEBUGCHAN_FLAG;
01258             c->fout &= ~DEBUGCHAN_FLAG;
01259          } else {
01260             c->fin |= DEBUGCHAN_FLAG;
01261             c->fout |= DEBUGCHAN_FLAG;
01262          }
01263          ast_cli(a->fd, "Debugging %s on channel %s\n", is_off ? "disabled" : "enabled", c->name);
01264       }
01265       ast_channel_unlock(c);
01266       if (!is_all)
01267          break;
01268       c = ast_channel_walk_locked(c);
01269    }
01270    ast_cli(a->fd, "Debugging on new channels is %s\n", is_off ? "disabled" : "enabled");
01271    return CLI_SUCCESS;
01272 }

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

Definition at line 2028 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.

02029 {
02030    char fullcmd[80];
02031    struct ast_cli_entry *my_e;
02032    char *res = CLI_SUCCESS;
02033 
02034    if (cmd == CLI_INIT) {
02035       e->command = "core show help";
02036       e->usage =
02037          "Usage: core show help [topic]\n"
02038          "       When called with a topic as an argument, displays usage\n"
02039          "       information on the given command. If called without a\n"
02040          "       topic, it provides a list of commands.\n";
02041       return NULL;
02042 
02043    } else if (cmd == CLI_GENERATE) {
02044       /* skip first 14 or 15 chars, "core show help " */
02045       int l = strlen(a->line);
02046 
02047       if (l > 15) {
02048          l = 15;
02049       }
02050       /* XXX watch out, should stop to the non-generator parts */
02051       return __ast_cli_generator(a->line + l, a->word, a->n, 0);
02052    }
02053    if (a->argc == e->args) {
02054       return help1(a->fd, NULL, 0);
02055    }
02056 
02057    AST_RWLIST_RDLOCK(&helpers);
02058    my_e = find_cli(a->argv + 3, 1); /* try exact match first */
02059    if (!my_e) {
02060       res = help1(a->fd, a->argv + 3, 1 /* locked */);
02061       AST_RWLIST_UNLOCK(&helpers);
02062       return res;
02063    }
02064    if (my_e->usage)
02065       ast_cli(a->fd, "%s", my_e->usage);
02066    else {
02067       ast_join(fullcmd, sizeof(fullcmd), a->argv + 3);
02068       ast_cli(a->fd, "No help text available for '%s'.\n", fullcmd);
02069    }
02070    AST_RWLIST_UNLOCK(&helpers);
02071    return res;
02072 }

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 539 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.

00540 {
00541    switch (cmd) {
00542    case CLI_INIT:
00543       e->command = "logger mute";
00544       e->usage = 
00545          "Usage: logger mute\n"
00546          "       Disables logging output to the current console, making it possible to\n"
00547          "       gather information without being disturbed by scrolling lines.\n";
00548       return NULL;
00549    case CLI_GENERATE:
00550       return NULL;
00551    }
00552 
00553    if (a->argc < 2 || a->argc > 3)
00554       return CLI_SHOWUSAGE;
00555 
00556    if (a->argc == 3 && !strcasecmp(a->argv[2], "silent"))
00557       ast_console_toggle_mute(a->fd, 1);
00558    else
00559       ast_console_toggle_mute(a->fd, 0);
00560 
00561    return CLI_SUCCESS;
00562 }

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

Definition at line 720 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.

00721 {
00722    char *like;
00723 
00724    switch (cmd) {
00725    case CLI_INIT:
00726       e->command = "module show [like]";
00727       e->usage =
00728          "Usage: module show [like keyword]\n"
00729          "       Shows Asterisk modules currently in use, and usage statistics.\n";
00730       return NULL;
00731 
00732    case CLI_GENERATE:
00733       if (a->pos == e->args)
00734          return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
00735       else
00736          return NULL;
00737    }
00738    /* all the above return, so we proceed with the handler.
00739     * we are guaranteed to have argc >= e->args
00740     */
00741    if (a->argc == e->args - 1)
00742       like = "";
00743    else if (a->argc == e->args + 1 && !strcasecmp(a->argv[e->args-1], "like") )
00744       like = a->argv[e->args];
00745    else
00746       return CLI_SHOWUSAGE;
00747       
00748    ast_mutex_lock(&climodentrylock);
00749    climodentryfd = a->fd; /* global, protected by climodentrylock */
00750    ast_cli(a->fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
00751    ast_cli(a->fd,"%d modules loaded\n", ast_update_module_list(modlist_modentry, like));
00752    climodentryfd = -1;
00753    ast_mutex_unlock(&climodentrylock);
00754    return CLI_SUCCESS;
00755 }

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

Definition at line 1274 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().

01275 {
01276    char *res;
01277    if (cmd == CLI_HANDLER) {
01278       if (a->argc != e->args + 1)
01279          return CLI_SHOWUSAGE;
01280       /* pretend we have an extra "off" at the end. We can do this as the array
01281        * is NULL terminated so we overwrite that entry.
01282        */
01283       a->argv[e->args+1] = "off";
01284       a->argc++;
01285    }
01286    res = handle_core_set_debug_channel(e, cmd, a);
01287    if (cmd == CLI_INIT)
01288       e->command = "no debug channel";
01289    return res;
01290 }

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 759 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.

00760 {
00761    struct timeval curtime = ast_tvnow();
00762    int showuptime, printsec;
00763 
00764    switch (cmd) {
00765    case CLI_INIT:
00766       e->command = "core show calls [uptime]";
00767       e->usage =
00768          "Usage: core show calls [uptime] [seconds]\n"
00769          "       Lists number of currently active calls and total number of calls\n"
00770          "       processed through PBX since last restart. If 'uptime' is specified\n"
00771          "       the system uptime is also displayed. If 'seconds' is specified in\n"
00772          "       addition to 'uptime', the system uptime is displayed in seconds.\n";
00773       return NULL;
00774 
00775    case CLI_GENERATE:
00776       if (a->pos != e->args)
00777          return NULL;
00778       return a->n == 0  ? ast_strdup("seconds") : NULL;
00779    }
00780 
00781    /* regular handler */
00782    if (a->argc >= e->args && !strcasecmp(a->argv[e->args-1],"uptime")) {
00783       showuptime = 1;
00784 
00785       if (a->argc == e->args+1 && !strcasecmp(a->argv[e->args],"seconds"))
00786          printsec = 1;
00787       else if (a->argc == e->args)
00788          printsec = 0;
00789       else
00790          return CLI_SHOWUSAGE;
00791    } else if (a->argc == e->args-1) {
00792       showuptime = 0;
00793       printsec = 0;
00794    } else
00795       return CLI_SHOWUSAGE;
00796 
00797    if (option_maxcalls) {
00798       ast_cli(a->fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
00799          ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
00800          ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
00801    } else {
00802       ast_cli(a->fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
00803    }
00804    
00805    ast_cli(a->fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
00806 
00807    if (ast_startuptime.tv_sec && showuptime) {
00808       print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
00809    }
00810 
00811    return RESULT_SUCCESS;
00812 }

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

Definition at line 1292 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.

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

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

Definition at line 689 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.

00690 {
00691    struct timeval curtime = ast_tvnow();
00692    int printsec;
00693 
00694    switch (cmd) {
00695    case CLI_INIT:
00696       e->command = "core show uptime [seconds]";
00697       e->usage =
00698          "Usage: core show uptime [seconds]\n"
00699          "       Shows Asterisk uptime information.\n"
00700          "       The seconds word returns the uptime in seconds only.\n";
00701       return NULL;
00702 
00703    case CLI_GENERATE:
00704       return NULL;
00705    }
00706    /* regular handler */
00707    if (a->argc == e->args && !strcasecmp(a->argv[e->args-1],"seconds"))
00708       printsec = 1;
00709    else if (a->argc == e->args-1)
00710       printsec = 0;
00711    else
00712       return CLI_SHOWUSAGE;
00713    if (ast_startuptime.tv_sec)
00714       print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
00715    if (ast_lastreloadtime.tv_sec)
00716       print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload", printsec);
00717    return CLI_SUCCESS;
00718 }

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

Definition at line 936 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.

00937 {
00938    struct ast_channel *c=NULL;
00939 
00940    switch (cmd) {
00941    case CLI_INIT:
00942       e->command = "channel request hangup";
00943       e->usage =
00944          "Usage: channel request hangup <channel>\n"
00945          "       Request that a channel be hung up. The hangup takes effect\n"
00946          "       the next time the driver reads or writes from the channel\n";
00947       return NULL;
00948    case CLI_GENERATE:
00949       return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
00950    }
00951    if (a->argc != 4)
00952       return CLI_SHOWUSAGE;
00953    c = ast_get_channel_by_name_locked(a->argv[3]);
00954    if (c) {
00955       ast_cli(a->fd, "Requested Hangup on channel '%s'\n", c->name);
00956       ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
00957       ast_channel_unlock(c);
00958    } else
00959       ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
00960    return CLI_SUCCESS;
00961 }

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

Definition at line 564 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.

00565 {
00566    /* "module unload mod_1 [mod_2 .. mod_N]" */
00567    int x;
00568    int force = AST_FORCE_SOFT;
00569    char *s;
00570 
00571    switch (cmd) {
00572    case CLI_INIT:
00573       e->command = "module unload";
00574       e->usage =
00575          "Usage: module unload [-f|-h] <module_1> [<module_2> ... ]\n"
00576          "       Unloads the specified module from Asterisk. The -f\n"
00577          "       option causes the module to be unloaded even if it is\n"
00578          "       in use (may cause a crash) and the -h module causes the\n"
00579          "       module to be unloaded even if the module says it cannot, \n"
00580          "       which almost always will cause a crash.\n";
00581       return NULL;
00582 
00583    case CLI_GENERATE:
00584       return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
00585    }
00586    if (a->argc < e->args + 1)
00587       return CLI_SHOWUSAGE;
00588    x = e->args;   /* first argument */
00589    s = a->argv[x];
00590    if (s[0] == '-') {
00591       if (s[1] == 'f')
00592          force = AST_FORCE_FIRM;
00593       else if (s[1] == 'h')
00594          force = AST_FORCE_HARD;
00595       else
00596          return CLI_SHOWUSAGE;
00597       if (a->argc < e->args + 2) /* need at least one module name */
00598          return CLI_SHOWUSAGE;
00599       x++;  /* skip this argument */
00600    }
00601 
00602    for (; x < a->argc; x++) {
00603       if (ast_unload_resource(a->argv[x], force)) {
00604          ast_cli(a->fd, "Unable to unload resource %s\n", a->argv[x]);
00605          return CLI_FAILURE;
00606       }
00607       ast_cli(a->fd, "Unloaded %s\n", a->argv[x]);
00608    }
00609 
00610    return CLI_SUCCESS;
00611 }

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

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 1999 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().

02000 {
02001    char matchstr[80] = "";
02002    struct ast_cli_entry *e = NULL;
02003    int len = 0;
02004    int found = 0;
02005 
02006    if (match) {
02007       ast_join(matchstr, sizeof(matchstr), match);
02008       len = strlen(matchstr);
02009    }
02010    if (!locked)
02011       AST_RWLIST_RDLOCK(&helpers);
02012    while ( (e = cli_next(e)) ) {
02013       /* Hide commands that start with '_' */
02014       if (e->_full_cmd[0] == '_')
02015          continue;
02016       if (match && strncasecmp(matchstr, e->_full_cmd, len))
02017          continue;
02018       ast_cli(fd, "%30.30s %s\n", e->_full_cmd, S_OR(e->summary, "<no description available>"));
02019       found++;
02020    }
02021    if (!locked)
02022       AST_RWLIST_UNLOCK(&helpers);
02023    if (!found && matchstr[0])
02024       ast_cli(fd, "No such command '%s'.\n", matchstr);
02025    return CLI_SUCCESS;
02026 }

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 1770 of file cli.c.

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

Referenced by __ast_cli_generator().

01772 {
01773    int lw;
01774    char *s, *t1;
01775 
01776    *actual = 0;
01777    if (ast_strlen_zero(token))
01778       return NULL;
01779    if (ast_strlen_zero(word))
01780       word = "";  /* dummy */
01781    lw = strlen(word);
01782    if (strcspn(word, cli_rsvd) != lw)
01783       return NULL;   /* no match if word has reserved chars */
01784    if (strchr(cli_rsvd, token[0]) == NULL) { /* regular match */
01785       if (strncasecmp(token, word, lw))   /* no match */
01786          return NULL;
01787       *actual = 1;
01788       return (pos != 0) ? NULL : ast_strdup(token);
01789    }
01790    /* now handle regexp match */
01791 
01792    /* Wildcard always matches, so we never do is_prefix on them */
01793 
01794    t1 = ast_strdupa(token + 1);  /* copy, skipping first char */
01795    while (pos >= 0 && (s = strsep(&t1, cli_rsvd)) && *s) {
01796       if (*s == '%') /* wildcard */
01797          continue;
01798       if (strncasecmp(s, word, lw)) /* no match */
01799          continue;
01800       (*actual)++;
01801       if (pos-- == 0)
01802          return ast_strdup(s);
01803    }
01804    return NULL;
01805 }

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

Definition at line 619 of file cli.c.

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

Referenced by handle_modlist().

00620 {
00621    /* Comparing the like with the module */
00622    if (strcasestr(module, like) ) {
00623       ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
00624       return 1;
00625    } 
00626    return 0;
00627 }

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

returns true if there are more words to match

Definition at line 2205 of file cli.c.

Referenced by __ast_cli_generator().

02206 {
02207    int i;
02208    for (i = 0; dst[i]; i++) {
02209       if (dst[i][0] != '[')
02210          return -1;
02211    }
02212    return 0;
02213 }

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

Definition at line 2074 of file cli.c.

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

Referenced by __ast_cli_generator(), and ast_cli_command_full().

02075 {
02076    char *duplicate, *cur;
02077    int x = 0;
02078    int quoted = 0;
02079    int escaped = 0;
02080    int whitespace = 1;
02081    int dummy = 0;
02082 
02083    if (trailingwhitespace == NULL)
02084       trailingwhitespace = &dummy;
02085    *trailingwhitespace = 0;
02086    if (s == NULL) /* invalid, though! */
02087       return NULL;
02088    /* make a copy to store the parsed string */
02089    if (!(duplicate = ast_strdup(s)))
02090       return NULL;
02091 
02092    cur = duplicate;
02093    /* scan the original string copying into cur when needed */
02094    for (; *s ; s++) {
02095       if (x >= max - 1) {
02096          ast_log(LOG_WARNING, "Too many arguments, truncating at %s\n", s);
02097          break;
02098       }
02099       if (*s == '"' && !escaped) {
02100          quoted = !quoted;
02101          if (quoted && whitespace) {
02102             /* start a quoted string from previous whitespace: new argument */
02103             argv[x++] = cur;
02104             whitespace = 0;
02105          }
02106       } else if ((*s == ' ' || *s == '\t') && !(quoted || escaped)) {
02107          /* If we are not already in whitespace, and not in a quoted string or
02108             processing an escape sequence, and just entered whitespace, then
02109             finalize the previous argument and remember that we are in whitespace
02110          */
02111          if (!whitespace) {
02112             *cur++ = '\0';
02113             whitespace = 1;
02114          }
02115       } else if (*s == '\\' && !escaped) {
02116          escaped = 1;
02117       } else {
02118          if (whitespace) {
02119             /* we leave whitespace, and are not quoted. So it's a new argument */
02120             argv[x++] = cur;
02121             whitespace = 0;
02122          }
02123          *cur++ = *s;
02124          escaped = 0;
02125       }
02126    }
02127    /* Null terminate */
02128    *cur++ = '\0';
02129    /* XXX put a NULL in the last argument, because some functions that take
02130     * the array may want a null-terminated array.
02131     * argc still reflects the number of non-NULL entries.
02132     */
02133    argv[x] = NULL;
02134    *argc = x;
02135    *trailingwhitespace = whitespace;
02136    return duplicate;
02137 }

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

Definition at line 629 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().

00630 {
00631    int x; /* the main part - years, weeks, etc. */
00632    struct ast_str *out;
00633 
00634 #define SECOND (1)
00635 #define MINUTE (SECOND*60)
00636 #define HOUR (MINUTE*60)
00637 #define DAY (HOUR*24)
00638 #define WEEK (DAY*7)
00639 #define YEAR (DAY*365)
00640 #define NEEDCOMMA(x) ((x)? ",": "") /* define if we need a comma */
00641    if (timeval.tv_sec < 0) /* invalid, nothing to show */
00642       return;
00643 
00644    if (printsec)  {  /* plain seconds output */
00645       ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval.tv_sec);
00646       return;
00647    }
00648    out = ast_str_alloca(256);
00649    if (timeval.tv_sec > YEAR) {
00650       x = (timeval.tv_sec / YEAR);
00651       timeval.tv_sec -= (x * YEAR);
00652       ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00653    }
00654    if (timeval.tv_sec > WEEK) {
00655       x = (timeval.tv_sec / WEEK);
00656       timeval.tv_sec -= (x * WEEK);
00657       ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00658    }
00659    if (timeval.tv_sec > DAY) {
00660       x = (timeval.tv_sec / DAY);
00661       timeval.tv_sec -= (x * DAY);
00662       ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00663    }
00664    if (timeval.tv_sec > HOUR) {
00665       x = (timeval.tv_sec / HOUR);
00666       timeval.tv_sec -= (x * HOUR);
00667       ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00668    }
00669    if (timeval.tv_sec > MINUTE) {
00670       x = (timeval.tv_sec / MINUTE);
00671       timeval.tv_sec -= (x * MINUTE);
00672       ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00673    }
00674    x = timeval.tv_sec;
00675    if (x > 0 || ast_str_strlen(out) == 0) /* if there is nothing, print 0 seconds */
00676       ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
00677    ast_cli(fd, "%s: %s\n", prefix, ast_str_buffer(out));
00678 }

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 1567 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().

01568 {
01569    int i;
01570    char buf[80];
01571 
01572    ast_join(buf, sizeof(buf), e->cmda);
01573    e->_full_cmd = ast_strdup(buf);
01574    if (!e->_full_cmd) {
01575       ast_log(LOG_WARNING, "-- cannot allocate <%s>\n", buf);
01576       return -1;
01577    }
01578    e->cmdlen = strcspn(e->_full_cmd, cli_rsvd);
01579    for (i = 0; e->cmda[i]; i++)
01580       ;
01581    e->args = i;
01582    return 0;
01583 }

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 1741 of file cli.c.

References ast_strlen_zero(), and strcasestr().

Referenced by __ast_cli_generator(), and find_cli().

01742 {
01743    int l;
01744    char *pos;
01745 
01746    if (ast_strlen_zero(cmd) || ast_strlen_zero(cli_word))
01747       return -1;
01748    if (!strchr(cli_rsvd, cli_word[0])) /* normal match */
01749       return (strcasecmp(cmd, cli_word) == 0) ? 1 : -1;
01750    /* regexp match, takes [foo|bar] or {foo|bar} */
01751    l = strlen(cmd);
01752    /* wildcard match - will extend in the future */
01753    if (l > 0 && cli_word[0] == '%') {
01754       return 1;   /* wildcard */
01755    }
01756    pos = strcasestr(cli_word, cmd);
01757    if (pos == NULL) /* not found, say ok if optional */
01758       return cli_word[0] == '[' ? 0 : -1;
01759    if (pos == cli_word) /* no valid match at the beginning */
01760       return -1;
01761    if (strchr(cli_rsvd, pos[-1]) && strchr(cli_rsvd, pos[l]))
01762       return 1;   /* valid match */
01763    return -1;  /* not found */
01764 }


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 1513 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 1561 of file cli.c.

int climodentryfd = -1 [static]

Definition at line 617 of file cli.c.

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

Definition at line 616 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 Sat Mar 26 11:17:38 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.9