2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Standard Command Line Interface
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/_private.h"
31 #include "asterisk/paths.h" /* use ast_config_AST_MODULE_DIR */
32 #include <sys/signal.h>
39 #include "asterisk/cli.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/module.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/app.h"
46 #include "asterisk/lock.h"
47 #include "editline/readline/readline.h"
48 #include "asterisk/threadstorage.h"
51 * \brief List of restrictions per user.
54 unsigned int permit:1; /*!< 1=Permit 0=Deny */
55 char *command; /*!< Command name (to apply restrictions) */
56 AST_LIST_ENTRY(cli_perm) list;
59 AST_LIST_HEAD_NOLOCK(cli_perm_head, cli_perm);
61 /*! \brief list of users to apply restrictions. */
62 struct usergroup_cli_perm {
63 int uid; /*!< User ID (-1 disabled) */
64 int gid; /*!< Group ID (-1 disabled) */
65 struct cli_perm_head *perms; /*!< List of permissions. */
66 AST_LIST_ENTRY(usergroup_cli_perm) list;/*!< List mechanics */
68 /*! \brief CLI permissions config file. */
69 static const char perms_config[] = "cli_permissions.conf";
70 /*! \brief Default permissions value 1=Permit 0=Deny */
71 static int cli_default_perm = 1;
73 /*! \brief mutex used to prevent a user from running the 'cli reload permissions' command while
74 * it is already running. */
75 AST_MUTEX_DEFINE_STATIC(permsconfiglock);
76 /*! \brief List of users and permissions. */
77 static AST_RWLIST_HEAD_STATIC(cli_perms, usergroup_cli_perm);
80 * \brief map a debug or verbose level to a module name
84 AST_RWLIST_ENTRY(module_level) entry;
88 AST_RWLIST_HEAD(module_level_list, module_level);
90 /*! list of module names and their debug levels */
91 static struct module_level_list debug_modules;
92 /*! list of module names and their verbose levels */
93 static struct module_level_list verbose_modules;
95 AST_THREADSTORAGE(ast_cli_buf);
97 /*! \brief Initial buffer size for resulting strings in ast_cli() */
98 #define AST_CLI_INITLEN 256
100 void ast_cli(int fd, const char *fmt, ...)
106 if (!(buf = ast_str_thread_get(&ast_cli_buf, AST_CLI_INITLEN)))
110 res = ast_str_set_va(&buf, 0, fmt, ap);
113 if (res != AST_DYNSTR_BUILD_FAILED) {
114 ast_carefulwrite(fd, ast_str_buffer(buf), ast_str_strlen(buf), 100);
118 unsigned int ast_debug_get_by_module(const char *module)
120 struct module_level *ml;
121 unsigned int res = 0;
123 AST_RWLIST_RDLOCK(&debug_modules);
124 AST_LIST_TRAVERSE(&debug_modules, ml, entry) {
125 if (!strcasecmp(ml->module, module)) {
130 AST_RWLIST_UNLOCK(&debug_modules);
135 unsigned int ast_verbose_get_by_module(const char *module)
137 struct module_level *ml;
138 unsigned int res = 0;
140 AST_RWLIST_RDLOCK(&verbose_modules);
141 AST_LIST_TRAVERSE(&verbose_modules, ml, entry) {
142 if (!strcasecmp(ml->module, module)) {
147 AST_RWLIST_UNLOCK(&verbose_modules);
153 * \brief Check if the user with 'uid' and 'gid' is allow to execute 'command',
154 * if command starts with '_' then not check permissions, just permit
155 * to run the 'command'.
156 * if uid == -1 or gid == -1 do not check permissions.
157 * if uid == -2 and gid == -2 is because rasterisk client didn't send
158 * the credentials, so the cli_default_perm will be applied.
159 * \param uid User ID.
160 * \param gid Group ID.
161 * \param command Command name to check permissions.
162 * \retval 1 if has permission
163 * \retval 0 if it is not allowed.
165 static int cli_has_permissions(int uid, int gid, const char *command)
167 struct usergroup_cli_perm *user_perm;
168 struct cli_perm *perm;
169 /* set to the default permissions general option. */
170 int isallowg = cli_default_perm, isallowu = -1, ispattern;
173 /* if uid == -1 or gid == -1 do not check permissions.
174 if uid == -2 and gid == -2 is because rasterisk client didn't send
175 the credentials, so the cli_default_perm will be applied. */
176 if ((uid == CLI_NO_PERMS && gid == CLI_NO_PERMS) || command[0] == '_') {
180 if (gid < 0 && uid < 0) {
181 return cli_default_perm;
184 AST_RWLIST_RDLOCK(&cli_perms);
185 AST_LIST_TRAVERSE(&cli_perms, user_perm, list) {
186 if (user_perm->gid != gid && user_perm->uid != uid) {
189 AST_LIST_TRAVERSE(user_perm->perms, perm, list) {
190 if (strcasecmp(perm->command, "all") && strncasecmp(perm->command, command, strlen(perm->command))) {
191 /* if the perm->command is a pattern, check it against command. */
192 ispattern = !regcomp(®exbuf, perm->command, REG_EXTENDED | REG_NOSUB | REG_ICASE);
193 if (ispattern && regexec(®exbuf, command, 0, NULL, 0)) {
202 if (user_perm->uid == uid) {
203 /* this is a user definition. */
204 isallowu = perm->permit;
206 /* otherwise is a group definition. */
207 isallowg = perm->permit;
211 AST_RWLIST_UNLOCK(&cli_perms);
213 /* user definition override group definition. */
220 static AST_RWLIST_HEAD_STATIC(helpers, ast_cli_entry);
222 static char *complete_fn(const char *word, int state)
225 char filename[PATH_MAX];
228 ast_copy_string(filename, word, sizeof(filename));
230 snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_MODULE_DIR, word);
232 c = d = filename_completion_function(filename, state);
234 if (c && word[0] != '/')
235 c += (strlen(ast_config_AST_MODULE_DIR) + 1);
244 static char *handle_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
246 /* "module load <mod>" */
249 e->command = "module load";
251 "Usage: module load <module name>\n"
252 " Loads the specified module into Asterisk.\n";
256 if (a->pos != e->args)
258 return complete_fn(a->word, a->n);
260 if (a->argc != e->args + 1)
261 return CLI_SHOWUSAGE;
262 if (ast_load_resource(a->argv[e->args])) {
263 ast_cli(a->fd, "Unable to load module %s\n", a->argv[e->args]);
266 ast_cli(a->fd, "Loaded %s\n", a->argv[e->args]);
270 static char *handle_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
276 e->command = "module reload";
278 "Usage: module reload [module ...]\n"
279 " Reloads configuration files for all listed modules which support\n"
280 " reloading, or for all supported modules if none are listed.\n";
284 return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 1);
286 if (a->argc == e->args) {
287 ast_module_reload(NULL);
290 for (x = e->args; x < a->argc; x++) {
291 int res = ast_module_reload(a->argv[x]);
292 /* XXX reload has multiple error returns, including -1 on error and 2 on success */
295 ast_cli(a->fd, "No such module '%s'\n", a->argv[x]);
298 ast_cli(a->fd, "Module '%s' does not support reload\n", a->argv[x]);
306 * \brief Find the debug or verbose file setting
307 * \arg debug 1 for debug, 0 for verbose
309 static struct module_level *find_module_level(const char *module, unsigned int debug)
311 struct module_level *ml;
312 struct module_level_list *mll = debug ? &debug_modules : &verbose_modules;
314 AST_LIST_TRAVERSE(mll, ml, entry) {
315 if (!strcasecmp(ml->module, module))
322 static char *complete_number(const char *partial, unsigned int min, unsigned int max, int n)
325 unsigned int prospective[2];
326 unsigned int part = strtoul(partial, NULL, 10);
329 if (part < min || part > max) {
333 for (i = 0; i < 21; i++) {
335 prospective[0] = prospective[1] = part;
336 } else if (part == 0 && !ast_strlen_zero(partial)) {
339 prospective[0] = prospective[1] = part * 10 + (i - 1);
341 prospective[0] = (part * 10 + (i - 11)) * 10;
342 prospective[1] = prospective[0] + 9;
344 if (i < 11 && (prospective[0] < min || prospective[0] > max)) {
346 } else if (prospective[1] < min || prospective[0] > max) {
352 snprintf(next, sizeof(next), "%u", prospective[0]);
354 snprintf(next, sizeof(next), "%u...", prospective[0] / 10);
356 return ast_strdup(next);
362 static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
369 const char * const *argv = a->argv;
370 const char *argv3 = a->argv ? S_OR(a->argv[3], "") : "";
373 struct module_level_list *mll;
374 struct module_level *ml;
378 e->command = "core set {debug|verbose}";
380 #if !defined(LOW_MEMORY)
381 "Usage: core set {debug|verbose} [atleast] <level> [module]\n"
383 "Usage: core set {debug|verbose} [atleast] <level>\n"
385 " core set {debug|verbose} off\n"
386 #if !defined(LOW_MEMORY)
387 " Sets level of debug or verbose messages to be displayed or\n"
388 " sets a module name to display debug messages from.\n"
390 " Sets level of debug or verbose messages to be displayed.\n"
392 " 0 or off means no messages should be displayed.\n"
393 " Equivalent to -d[d[...]] or -v[v[v...]] on startup\n";
397 if (a->pos == 3 || (a->pos == 4 && !strcasecmp(a->argv[3], "atleast"))) {
398 const char *pos = a->pos == 3 ? argv3 : S_OR(a->argv[4], "");
399 int numbermatch = (ast_strlen_zero(pos) || strchr("123456789", pos[0])) ? 0 : 21;
400 if (a->n < 21 && numbermatch == 0) {
401 return complete_number(pos, 0, 0x7fffffff, a->n);
402 } else if (pos[0] == '0') {
404 return ast_strdup("0");
408 } else if (a->n == (21 - numbermatch)) {
409 if (a->pos == 3 && !strncasecmp(argv3, "off", strlen(argv3))) {
410 return ast_strdup("off");
411 } else if (a->pos == 3 && !strncasecmp(argv3, "atleast", strlen(argv3))) {
412 return ast_strdup("atleast");
414 } else if (a->n == (22 - numbermatch) && a->pos == 3 && ast_strlen_zero(argv3)) {
415 return ast_strdup("atleast");
417 #if !defined(LOW_MEMORY)
418 } else if (a->pos == 4 || (a->pos == 5 && !strcasecmp(argv3, "atleast"))) {
419 return ast_complete_source_filename(a->pos == 4 ? S_OR(a->argv[4], "") : S_OR(a->argv[5], ""), a->n);
424 /* all the above return, so we proceed with the handler.
425 * we are guaranteed to be called with argc >= e->args;
429 return CLI_SHOWUSAGE;
430 if (!strcasecmp(argv[e->args - 1], "debug")) {
432 oldval = option_debug;
435 dst = &option_verbose;
436 oldval = option_verbose;
439 if (argc == e->args + 1 && !strcasecmp(argv[e->args], "off")) {
440 unsigned int debug = (*what == 'C');
443 mll = debug ? &debug_modules : &verbose_modules;
445 AST_RWLIST_WRLOCK(mll);
446 while ((ml = AST_RWLIST_REMOVE_HEAD(mll, entry))) {
449 ast_clear_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_MODULE : AST_OPT_FLAG_VERBOSE_MODULE);
450 AST_RWLIST_UNLOCK(mll);
454 if (!strcasecmp(argv[e->args], "atleast"))
456 if (argc != e->args + atleast + 1 && argc != e->args + atleast + 2)
457 return CLI_SHOWUSAGE;
458 if (sscanf(argv[e->args + atleast], "%30d", &newlevel) != 1)
459 return CLI_SHOWUSAGE;
460 if (argc == e->args + atleast + 2) {
461 unsigned int debug = (*what == 'C');
462 char *mod = ast_strdupa(argv[e->args + atleast + 1]);
464 mll = debug ? &debug_modules : &verbose_modules;
466 if ((strlen(mod) > 3) && !strcasecmp(mod + strlen(mod) - 3, ".so")) {
467 mod[strlen(mod) - 3] = '\0';
470 AST_RWLIST_WRLOCK(mll);
472 if ((ml = find_module_level(mod, debug)) && !newlevel) {
473 AST_RWLIST_REMOVE(mll, ml, entry);
474 if (AST_RWLIST_EMPTY(mll))
475 ast_clear_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_MODULE : AST_OPT_FLAG_VERBOSE_MODULE);
476 AST_RWLIST_UNLOCK(mll);
477 ast_cli(fd, "%s was %d and has been set to 0 for '%s'\n", what, ml->level, mod);
483 if ((atleast && newlevel < ml->level) || ml->level == newlevel) {
484 ast_cli(fd, "%s is %d for '%s'\n", what, ml->level, mod);
485 AST_RWLIST_UNLOCK(mll);
488 } else if (!(ml = ast_calloc(1, sizeof(*ml) + strlen(mod) + 1))) {
489 AST_RWLIST_UNLOCK(mll);
494 ml->level = newlevel;
495 strcpy(ml->module, mod);
497 ast_set_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_MODULE : AST_OPT_FLAG_VERBOSE_MODULE);
499 AST_RWLIST_INSERT_TAIL(mll, ml, entry);
500 AST_RWLIST_UNLOCK(mll);
502 ast_cli(fd, "%s was %d and has been set to %d for '%s'\n", what, oldval, ml->level, ml->module);
508 if (!atleast || newlevel > *dst)
510 if (oldval > 0 && *dst == 0)
511 ast_cli(fd, "%s is now OFF\n", what);
514 ast_cli(fd, "%s is at least %d\n", what, *dst);
516 ast_cli(fd, "%s was %d and is now %d\n", what, oldval, *dst);
522 static char *handle_logger_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
526 e->command = "logger mute";
528 "Usage: logger mute\n"
529 " Disables logging output to the current console, making it possible to\n"
530 " gather information without being disturbed by scrolling lines.\n";
536 if (a->argc < 2 || a->argc > 3)
537 return CLI_SHOWUSAGE;
539 if (a->argc == 3 && !strcasecmp(a->argv[2], "silent"))
540 ast_console_toggle_mute(a->fd, 1);
542 ast_console_toggle_mute(a->fd, 0);
547 static char *handle_unload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
549 /* "module unload mod_1 [mod_2 .. mod_N]" */
551 int force = AST_FORCE_SOFT;
556 e->command = "module unload";
558 "Usage: module unload [-f|-h] <module_1> [<module_2> ... ]\n"
559 " Unloads the specified module from Asterisk. The -f\n"
560 " option causes the module to be unloaded even if it is\n"
561 " in use (may cause a crash) and the -h module causes the\n"
562 " module to be unloaded even if the module says it cannot, \n"
563 " which almost always will cause a crash.\n";
567 return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
569 if (a->argc < e->args + 1)
570 return CLI_SHOWUSAGE;
571 x = e->args; /* first argument */
575 force = AST_FORCE_FIRM;
576 else if (s[1] == 'h')
577 force = AST_FORCE_HARD;
579 return CLI_SHOWUSAGE;
580 if (a->argc < e->args + 2) /* need at least one module name */
581 return CLI_SHOWUSAGE;
582 x++; /* skip this argument */
585 for (; x < a->argc; x++) {
586 if (ast_unload_resource(a->argv[x], force)) {
587 ast_cli(a->fd, "Unable to unload resource %s\n", a->argv[x]);
590 ast_cli(a->fd, "Unloaded %s\n", a->argv[x]);
596 #define MODLIST_FORMAT "%-30s %-40.40s %-10d\n"
597 #define MODLIST_FORMAT2 "%-30s %-40.40s %-10s\n"
599 AST_MUTEX_DEFINE_STATIC(climodentrylock);
600 static int climodentryfd = -1;
602 static int modlist_modentry(const char *module, const char *description, int usecnt, const char *like)
604 /* Comparing the like with the module */
605 if (strcasestr(module, like) ) {
606 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
612 static void print_uptimestr(int fd, struct timeval timeval, const char *prefix, int printsec)
614 int x; /* the main part - years, weeks, etc. */
618 #define MINUTE (SECOND*60)
619 #define HOUR (MINUTE*60)
620 #define DAY (HOUR*24)
622 #define YEAR (DAY*365)
623 #define NEEDCOMMA(x) ((x)? ",": "") /* define if we need a comma */
624 if (timeval.tv_sec < 0) /* invalid, nothing to show */
627 if (printsec) { /* plain seconds output */
628 ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval.tv_sec);
631 out = ast_str_alloca(256);
632 if (timeval.tv_sec > YEAR) {
633 x = (timeval.tv_sec / YEAR);
634 timeval.tv_sec -= (x * YEAR);
635 ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
637 if (timeval.tv_sec > WEEK) {
638 x = (timeval.tv_sec / WEEK);
639 timeval.tv_sec -= (x * WEEK);
640 ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
642 if (timeval.tv_sec > DAY) {
643 x = (timeval.tv_sec / DAY);
644 timeval.tv_sec -= (x * DAY);
645 ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
647 if (timeval.tv_sec > HOUR) {
648 x = (timeval.tv_sec / HOUR);
649 timeval.tv_sec -= (x * HOUR);
650 ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
652 if (timeval.tv_sec > MINUTE) {
653 x = (timeval.tv_sec / MINUTE);
654 timeval.tv_sec -= (x * MINUTE);
655 ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
658 if (x > 0 || ast_str_strlen(out) == 0) /* if there is nothing, print 0 seconds */
659 ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
660 ast_cli(fd, "%s: %s\n", prefix, ast_str_buffer(out));
663 static struct ast_cli_entry *cli_next(struct ast_cli_entry *e)
666 return AST_LIST_NEXT(e, list);
668 return AST_LIST_FIRST(&helpers);
672 static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
674 struct timeval curtime = ast_tvnow();
679 e->command = "core show uptime [seconds]";
681 "Usage: core show uptime [seconds]\n"
682 " Shows Asterisk uptime information.\n"
683 " The seconds word returns the uptime in seconds only.\n";
689 /* regular handler */
690 if (a->argc == e->args && !strcasecmp(a->argv[e->args-1],"seconds"))
692 else if (a->argc == e->args-1)
695 return CLI_SHOWUSAGE;
696 if (ast_startuptime.tv_sec)
697 print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
698 if (ast_lastreloadtime.tv_sec)
699 print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload", printsec);
703 static char *handle_modlist(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
709 e->command = "module show [like]";
711 "Usage: module show [like keyword]\n"
712 " Shows Asterisk modules currently in use, and usage statistics.\n";
716 if (a->pos == e->args)
717 return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
721 /* all the above return, so we proceed with the handler.
722 * we are guaranteed to have argc >= e->args
724 if (a->argc == e->args - 1)
726 else if (a->argc == e->args + 1 && !strcasecmp(a->argv[e->args-1], "like") )
727 like = a->argv[e->args];
729 return CLI_SHOWUSAGE;
731 ast_mutex_lock(&climodentrylock);
732 climodentryfd = a->fd; /* global, protected by climodentrylock */
733 ast_cli(a->fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
734 ast_cli(a->fd,"%d modules loaded\n", ast_update_module_list(modlist_modentry, like));
736 ast_mutex_unlock(&climodentrylock);
739 #undef MODLIST_FORMAT
740 #undef MODLIST_FORMAT2
742 static char *handle_showcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
744 struct timeval curtime = ast_tvnow();
745 int showuptime, printsec;
749 e->command = "core show calls [uptime]";
751 "Usage: core show calls [uptime] [seconds]\n"
752 " Lists number of currently active calls and total number of calls\n"
753 " processed through PBX since last restart. If 'uptime' is specified\n"
754 " the system uptime is also displayed. If 'seconds' is specified in\n"
755 " addition to 'uptime', the system uptime is displayed in seconds.\n";
759 if (a->pos != e->args)
761 return a->n == 0 ? ast_strdup("seconds") : NULL;
764 /* regular handler */
765 if (a->argc >= e->args && !strcasecmp(a->argv[e->args-1],"uptime")) {
768 if (a->argc == e->args+1 && !strcasecmp(a->argv[e->args],"seconds"))
770 else if (a->argc == e->args)
773 return CLI_SHOWUSAGE;
774 } else if (a->argc == e->args-1) {
778 return CLI_SHOWUSAGE;
780 if (option_maxcalls) {
781 ast_cli(a->fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
782 ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
783 ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
785 ast_cli(a->fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
788 ast_cli(a->fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
790 if (ast_startuptime.tv_sec && showuptime) {
791 print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
794 return RESULT_SUCCESS;
797 static char *handle_chanlist(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
799 #define FORMAT_STRING "%-20.20s %-20.20s %-7.7s %-30.30s\n"
800 #define FORMAT_STRING2 "%-20.20s %-20.20s %-7.7s %-30.30s\n"
801 #define CONCISE_FORMAT_STRING "%s!%s!%s!%d!%s!%s!%s!%s!%s!%s!%d!%s!%s!%s\n"
802 #define VERBOSE_FORMAT_STRING "%-20.20s %-20.20s %-16.16s %4d %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-11.11s %-20.20s\n"
803 #define VERBOSE_FORMAT_STRING2 "%-20.20s %-20.20s %-16.16s %-4.4s %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-11.11s %-20.20s\n"
805 struct ast_channel *c = NULL;
806 int numchans = 0, concise = 0, verbose = 0, count = 0;
807 struct ast_channel_iterator *iter = NULL;
811 e->command = "core show channels [concise|verbose|count]";
813 "Usage: core show channels [concise|verbose|count]\n"
814 " Lists currently defined channels and some information about them. If\n"
815 " 'concise' is specified, the format is abridged and in a more easily\n"
816 " machine parsable format. If 'verbose' is specified, the output includes\n"
817 " more and longer fields. If 'count' is specified only the channel and call\n"
818 " count is output.\n"
819 " The 'concise' option is deprecated and will be removed from future versions\n"
827 if (a->argc == e->args) {
828 if (!strcasecmp(a->argv[e->args-1],"concise"))
830 else if (!strcasecmp(a->argv[e->args-1],"verbose"))
832 else if (!strcasecmp(a->argv[e->args-1],"count"))
835 return CLI_SHOWUSAGE;
836 } else if (a->argc != e->args - 1)
837 return CLI_SHOWUSAGE;
840 if (!concise && !verbose)
841 ast_cli(a->fd, FORMAT_STRING2, "Channel", "Location", "State", "Application(Data)");
843 ast_cli(a->fd, VERBOSE_FORMAT_STRING2, "Channel", "Context", "Extension", "Priority", "State", "Application", "Data",
844 "CallerID", "Duration", "Accountcode", "PeerAccount", "BridgedTo");
847 if (!count && !(iter = ast_channel_iterator_all_new())) {
851 for (; iter && (c = ast_channel_iterator_next(iter)); ast_channel_unref(c)) {
852 struct ast_channel *bc;
853 char durbuf[10] = "-";
857 bc = ast_bridged_channel(c);
860 if ((concise || verbose) && c->cdr && !ast_tvzero(c->cdr->start)) {
861 int duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
863 int durh = duration / 3600;
864 int durm = (duration % 3600) / 60;
865 int durs = duration % 60;
866 snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
868 snprintf(durbuf, sizeof(durbuf), "%d", duration);
872 ast_cli(a->fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
873 c->appl ? c->appl : "(None)",
874 S_OR(c->data, ""), /* XXX different from verbose ? */
875 S_OR(c->cid.cid_num, ""),
876 S_OR(c->accountcode, ""),
877 S_OR(c->peeraccount, ""),
880 bc ? bc->name : "(None)",
882 } else if (verbose) {
883 ast_cli(a->fd, VERBOSE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
884 c->appl ? c->appl : "(None)",
885 c->data ? S_OR(c->data, "(Empty)" ): "(None)",
886 S_OR(c->cid.cid_num, ""),
888 S_OR(c->accountcode, ""),
889 S_OR(c->peeraccount, ""),
890 bc ? bc->name : "(None)");
892 char locbuf[40] = "(None)";
893 char appdata[40] = "(None)";
895 if (!ast_strlen_zero(c->context) && !ast_strlen_zero(c->exten))
896 snprintf(locbuf, sizeof(locbuf), "%s@%s:%d", c->exten, c->context, c->priority);
898 snprintf(appdata, sizeof(appdata), "%s(%s)", c->appl, S_OR(c->data, ""));
899 ast_cli(a->fd, FORMAT_STRING, c->name, locbuf, ast_state2str(c->_state), appdata);
902 ast_channel_unlock(c);
906 ast_channel_iterator_destroy(iter);
910 numchans = ast_active_channels();
911 ast_cli(a->fd, "%d active channel%s\n", numchans, ESS(numchans));
913 ast_cli(a->fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
914 ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
915 ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
917 ast_cli(a->fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
919 ast_cli(a->fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
925 #undef FORMAT_STRING2
926 #undef CONCISE_FORMAT_STRING
927 #undef VERBOSE_FORMAT_STRING
928 #undef VERBOSE_FORMAT_STRING2
931 static char *handle_softhangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
933 struct ast_channel *c=NULL;
937 e->command = "channel request hangup";
939 "Usage: channel request hangup <channel>|<all>\n"
940 " Request that a channel be hung up. The hangup takes effect\n"
941 " the next time the driver reads or writes from the channel.\n"
942 " If 'all' is specified instead of a channel name, all channels\n"
943 " will see the hangup request.\n";
946 return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
950 return CLI_SHOWUSAGE;
953 if (!strcasecmp(a->argv[3], "all")) {
954 struct ast_channel_iterator *iter = NULL;
955 if (!(iter = ast_channel_iterator_all_new(0))) {
958 for (; iter && (c = ast_channel_iterator_next(iter)); ast_channel_unref(c)) {
960 ast_cli(a->fd, "Requested Hangup on channel '%s'\n", c->name);
961 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
962 ast_channel_unlock(c);
964 ast_channel_iterator_destroy(iter);
965 } else if ((c = ast_channel_get_by_name(a->argv[3]))) {
967 ast_cli(a->fd, "Requested Hangup on channel '%s'\n", c->name);
968 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
969 ast_channel_unlock(c);
970 c = ast_channel_unref(c);
972 ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
978 /*! \brief handles CLI command 'cli show permissions' */
979 static char *handle_cli_show_permissions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
981 struct usergroup_cli_perm *cp;
982 struct cli_perm *perm;
983 struct passwd *pw = NULL;
984 struct group *gr = NULL;
988 e->command = "cli show permissions";
990 "Usage: cli show permissions\n"
991 " Shows CLI configured permissions.\n";
997 AST_RWLIST_RDLOCK(&cli_perms);
998 AST_LIST_TRAVERSE(&cli_perms, cp, list) {
1000 pw = getpwuid(cp->uid);
1002 ast_cli(a->fd, "user: %s [uid=%d]\n", pw->pw_name, cp->uid);
1005 gr = getgrgid(cp->gid);
1007 ast_cli(a->fd, "group: %s [gid=%d]\n", gr->gr_name, cp->gid);
1010 ast_cli(a->fd, "Permissions:\n");
1012 AST_LIST_TRAVERSE(cp->perms, perm, list) {
1013 ast_cli(a->fd, "\t%s -> %s\n", perm->permit ? "permit" : "deny", perm->command);
1016 ast_cli(a->fd, "\n");
1018 AST_RWLIST_UNLOCK(&cli_perms);
1023 /*! \brief handles CLI command 'cli reload permissions' */
1024 static char *handle_cli_reload_permissions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1028 e->command = "cli reload permissions";
1030 "Usage: cli reload permissions\n"
1031 " Reload the 'cli_permissions.conf' file.\n";
1037 ast_cli_perms_init(1);
1042 /*! \brief handles CLI command 'cli check permissions' */
1043 static char *handle_cli_check_permissions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1045 struct passwd *pw = NULL;
1047 int gid = -1, uid = -1;
1048 char command[AST_MAX_ARGS] = "";
1049 struct ast_cli_entry *ce = NULL;
1055 e->command = "cli check permissions";
1057 "Usage: cli check permissions {<username>|@<groupname>|<username>@<groupname>} [<command>]\n"
1058 " Check permissions config for a user@group or list the allowed commands for the specified user.\n"
1059 " The username or the groupname may be omitted.\n";
1063 return ast_cli_generator(a->line + strlen("cli check permissions") + strlen(a->argv[3]) + 1, a->word, a->n);
1069 return CLI_SHOWUSAGE;
1072 tmp = ast_strdupa(a->argv[3]);
1073 group = strchr(tmp, '@');
1075 gr = getgrnam(&group[1]);
1077 ast_cli(a->fd, "Unknown group '%s'\n", &group[1]);
1084 if (!group && ast_strlen_zero(tmp)) {
1085 ast_cli(a->fd, "You didn't supply a username\n");
1086 } else if (!ast_strlen_zero(tmp) && !(pw = getpwnam(tmp))) {
1087 ast_cli(a->fd, "Unknown user '%s'\n", tmp);
1094 while ((ce = cli_next(ce))) {
1095 /* Hide commands that start with '_' */
1096 if (ce->_full_cmd[0] == '_') {
1099 if (cli_has_permissions(uid, gid, ce->_full_cmd)) {
1100 ast_cli(a->fd, "%30.30s %s\n", ce->_full_cmd, S_OR(ce->summary, "<no description available>"));
1105 ast_cli(a->fd, "You are not allowed to run any command on Asterisk\n");
1108 ast_join(command, sizeof(command), a->argv + 4);
1109 ast_cli(a->fd, "%s '%s%s%s' is %s to run command: '%s'\n", uid >= 0 ? "User" : "Group", tmp,
1110 group && uid >= 0 ? "@" : "",
1111 group ? &group[1] : "",
1112 cli_has_permissions(uid, gid, command) ? "allowed" : "not allowed", command);
1118 static char *__ast_cli_generator(const char *text, const char *word, int state, int lock);
1120 static char *handle_commandmatchesarray(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1130 e->command = "_command matchesarray";
1132 "Usage: _command matchesarray \"<line>\" text \n"
1133 " This function is used internally to help with command completion and should.\n"
1134 " never be called by the user directly.\n";
1141 return CLI_SHOWUSAGE;
1142 if (!(buf = ast_malloc(buflen)))
1145 matches = ast_cli_completion_matches(a->argv[2], a->argv[3]);
1147 for (x=0; matches[x]; x++) {
1148 matchlen = strlen(matches[x]) + 1;
1149 if (len + matchlen >= buflen) {
1150 buflen += matchlen * 3;
1152 if (!(buf = ast_realloc(obuf, buflen)))
1153 /* Memory allocation failure... Just free old buffer and be done */
1157 len += sprintf( buf + len, "%s ", matches[x]);
1158 ast_free(matches[x]);
1165 ast_cli(a->fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
1168 ast_cli(a->fd, "NULL\n");
1175 static char *handle_commandnummatches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1181 e->command = "_command nummatches";
1183 "Usage: _command nummatches \"<line>\" text \n"
1184 " This function is used internally to help with command completion and should.\n"
1185 " never be called by the user directly.\n";
1192 return CLI_SHOWUSAGE;
1194 matches = ast_cli_generatornummatches(a->argv[2], a->argv[3]);
1196 ast_cli(a->fd, "%d", matches);
1201 static char *handle_commandcomplete(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1206 e->command = "_command complete";
1208 "Usage: _command complete \"<line>\" text state\n"
1209 " This function is used internally to help with command completion and should.\n"
1210 " never be called by the user directly.\n";
1216 return CLI_SHOWUSAGE;
1217 buf = __ast_cli_generator(a->argv[2], a->argv[3], atoi(a->argv[4]), 0);
1219 ast_cli(a->fd, "%s", buf);
1222 ast_cli(a->fd, "NULL\n");
1226 struct channel_set_debug_args {
1231 static int channel_set_debug(void *obj, void *arg, void *data, int flags)
1233 struct ast_channel *chan = obj;
1234 struct channel_set_debug_args *args = data;
1236 ast_channel_lock(chan);
1238 if (!(chan->fin & DEBUGCHAN_FLAG) || !(chan->fout & DEBUGCHAN_FLAG)) {
1240 chan->fin &= ~DEBUGCHAN_FLAG;
1241 chan->fout &= ~DEBUGCHAN_FLAG;
1243 chan->fin |= DEBUGCHAN_FLAG;
1244 chan->fout |= DEBUGCHAN_FLAG;
1246 ast_cli(args->fd, "Debugging %s on channel %s\n", args->is_off ? "disabled" : "enabled",
1250 ast_channel_unlock(chan);
1255 static char *handle_core_set_debug_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1257 struct ast_channel *c = NULL;
1258 struct channel_set_debug_args args = {
1264 e->command = "core set debug channel";
1266 "Usage: core set debug channel <all|channel> [off]\n"
1267 " Enables/disables debugging on all or on a specific channel.\n";
1270 /* XXX remember to handle the optional "off" */
1271 if (a->pos != e->args)
1273 return a->n == 0 ? ast_strdup("all") : ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
1276 if (cmd == (CLI_HANDLER + 1000)) {
1277 /* called from handle_nodebugchan_deprecated */
1279 } else if (a->argc == e->args + 2) {
1280 /* 'core set debug channel {all|chan_id}' */
1281 if (!strcasecmp(a->argv[e->args + 1], "off"))
1284 return CLI_SHOWUSAGE;
1285 } else if (a->argc != e->args + 1) {
1286 return CLI_SHOWUSAGE;
1289 if (!strcasecmp("all", a->argv[e->args])) {
1291 global_fin &= ~DEBUGCHAN_FLAG;
1292 global_fout &= ~DEBUGCHAN_FLAG;
1294 global_fin |= DEBUGCHAN_FLAG;
1295 global_fout |= DEBUGCHAN_FLAG;
1297 ast_channel_callback(channel_set_debug, NULL, &args, OBJ_NODATA | OBJ_MULTIPLE);
1299 if ((c = ast_channel_get_by_name(a->argv[e->args]))) {
1300 channel_set_debug(c, NULL, &args, 0);
1301 ast_channel_unref(c);
1303 ast_cli(a->fd, "No such channel %s\n", a->argv[e->args]);
1307 ast_cli(a->fd, "Debugging on new channels is %s\n", args.is_off ? "disabled" : "enabled");
1312 static char *handle_nodebugchan_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1318 e->command = "no debug channel";
1321 /* exit out of switch statement */
1327 if (a->argc != e->args + 1)
1328 return CLI_SHOWUSAGE;
1330 /* add a 'magic' value to the CLI_HANDLER command so that
1331 * handle_core_set_debug_channel() will act as if 'off'
1332 * had been specified as part of the command
1334 res = handle_core_set_debug_channel(e, CLI_HANDLER + 1000, a);
1339 static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1341 struct ast_channel *c=NULL;
1343 struct ast_str *out = ast_str_thread_get(&ast_str_thread_global_buf, 16);
1345 char nf[256], wf[256], rf[256];
1346 long elapsed_seconds=0;
1347 int hour=0, min=0, sec=0;
1348 #ifdef CHANNEL_TRACE
1354 e->command = "core show channel";
1356 "Usage: core show channel <channel>\n"
1357 " Shows lots of information about the specified channel.\n";
1360 return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
1364 return CLI_SHOWUSAGE;
1369 if (!(c = ast_channel_get_by_name(a->argv[3]))) {
1370 ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
1374 ast_channel_lock(c);
1377 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
1378 hour = elapsed_seconds / 3600;
1379 min = (elapsed_seconds % 3600) / 60;
1380 sec = elapsed_seconds % 60;
1381 snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
1383 strcpy(cdrtime, "N/A");
1393 " Caller ID Name: %s\n"
1394 " DNID Digits: %s\n"
1398 " NativeFormats: %s\n"
1399 " WriteFormat: %s\n"
1401 " WriteTranscode: %s\n"
1402 " ReadTranscode: %s\n"
1403 "1st File Descriptor: %d\n"
1404 " Frames in: %d%s\n"
1405 " Frames out: %d%s\n"
1406 " Time to Hangup: %ld\n"
1407 " Elapsed Time: %s\n"
1408 " Direct Bridge: %s\n"
1409 "Indirect Bridge: %s\n"
1414 " Call Group: %llu\n"
1415 " Pickup Group: %llu\n"
1416 " Application: %s\n"
1418 " Blocking in: %s\n",
1419 c->name, c->tech->type, c->uniqueid, c->linkedid,
1420 S_OR(c->cid.cid_num, "(N/A)"),
1421 S_OR(c->cid.cid_name, "(N/A)"),
1422 S_OR(c->cid.cid_dnid, "(N/A)"),
1424 ast_state2str(c->_state), c->_state, c->rings,
1425 ast_getformatname_multiple(nf, sizeof(nf), c->nativeformats),
1426 ast_getformatname_multiple(wf, sizeof(wf), c->writeformat),
1427 ast_getformatname_multiple(rf, sizeof(rf), c->readformat),
1428 c->writetrans ? "Yes" : "No",
1429 c->readtrans ? "Yes" : "No",
1431 c->fin & ~DEBUGCHAN_FLAG, (c->fin & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
1432 c->fout & ~DEBUGCHAN_FLAG, (c->fout & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
1433 (long)c->whentohangup.tv_sec,
1434 cdrtime, c->_bridge ? c->_bridge->name : "<none>", ast_bridged_channel(c) ? ast_bridged_channel(c)->name : "<none>",
1435 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
1436 ( c-> data ? S_OR(c->data, "(Empty)") : "(None)"),
1437 (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
1439 if (pbx_builtin_serialize_variables(c, &out)) {
1440 ast_cli(a->fd," Variables:\n%s\n", ast_str_buffer(out));
1443 if (c->cdr && ast_cdr_serialize_variables(c->cdr, &out, '=', '\n', 1)) {
1444 ast_cli(a->fd," CDR Variables:\n%s\n", ast_str_buffer(out));
1447 #ifdef CHANNEL_TRACE
1448 trace_enabled = ast_channel_trace_is_enabled(c);
1449 ast_cli(a->fd, " Context Trace: %s\n", trace_enabled ? "Enabled" : "Disabled");
1450 if (trace_enabled && ast_channel_trace_serialize(c, &out))
1451 ast_cli(a->fd, " Trace:\n%s\n", ast_str_buffer(out));
1454 ast_channel_unlock(c);
1455 c = ast_channel_unref(c);
1461 * helper function to generate CLI matches from a fixed set of values.
1462 * A NULL word is acceptable.
1464 char *ast_cli_complete(const char *word, const char * const choices[], int state)
1466 int i, which = 0, len;
1467 len = ast_strlen_zero(word) ? 0 : strlen(word);
1469 for (i = 0; choices[i]; i++) {
1470 if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state)
1471 return ast_strdup(choices[i]);
1476 char *ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos)
1478 struct ast_channel *c = NULL;
1480 char notfound = '\0';
1481 char *ret = ¬found; /* so NULL can break the loop */
1482 struct ast_channel_iterator *iter;
1488 if (!(iter = ast_channel_iterator_by_name_new(word, strlen(word)))) {
1492 while (ret == ¬found && (c = ast_channel_iterator_next(iter))) {
1493 if (++which > state) {
1494 ast_channel_lock(c);
1495 ret = ast_strdup(c->name);
1496 ast_channel_unlock(c);
1498 ast_channel_unref(c);
1501 ast_channel_iterator_destroy(iter);
1503 return ret == ¬found ? NULL : ret;
1506 static char *group_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1508 #define FORMAT_STRING "%-25s %-20s %-20s\n"
1510 struct ast_group_info *gi = NULL;
1513 int havepattern = 0;
1517 e->command = "group show channels";
1519 "Usage: group show channels [pattern]\n"
1520 " Lists all currently active channels with channel group(s) specified.\n"
1521 " Optional regular expression pattern is matched to group names for each\n"
1528 if (a->argc < 3 || a->argc > 4)
1529 return CLI_SHOWUSAGE;
1532 if (regcomp(®exbuf, a->argv[3], REG_EXTENDED | REG_NOSUB))
1533 return CLI_SHOWUSAGE;
1537 ast_cli(a->fd, FORMAT_STRING, "Channel", "Group", "Category");
1539 ast_app_group_list_rdlock();
1541 gi = ast_app_group_list_head();
1543 if (!havepattern || !regexec(®exbuf, gi->group, 0, NULL, 0)) {
1544 ast_cli(a->fd, FORMAT_STRING, gi->chan->name, gi->group, (ast_strlen_zero(gi->category) ? "(default)" : gi->category));
1547 gi = AST_LIST_NEXT(gi, group_list);
1550 ast_app_group_list_unlock();
1555 ast_cli(a->fd, "%d active channel%s\n", numchans, ESS(numchans));
1557 #undef FORMAT_STRING
1560 static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
1562 static struct ast_cli_entry cli_cli[] = {
1563 /* Deprecated, but preferred command is now consolidated (and already has a deprecated command for it). */
1564 AST_CLI_DEFINE(handle_commandcomplete, "Command complete"),
1565 AST_CLI_DEFINE(handle_commandnummatches, "Returns number of command matches"),
1566 AST_CLI_DEFINE(handle_commandmatchesarray, "Returns command matches array"),
1568 AST_CLI_DEFINE(handle_nodebugchan_deprecated, "Disable debugging on channel(s)"),
1570 AST_CLI_DEFINE(handle_chanlist, "Display information on channels"),
1572 AST_CLI_DEFINE(handle_showcalls, "Display information on calls"),
1574 AST_CLI_DEFINE(handle_showchan, "Display information on a specific channel"),
1576 AST_CLI_DEFINE(handle_core_set_debug_channel, "Enable/disable debugging on a channel"),
1578 AST_CLI_DEFINE(handle_verbose, "Set level of debug/verbose chattiness"),
1580 AST_CLI_DEFINE(group_show_channels, "Display active channels with group(s)"),
1582 AST_CLI_DEFINE(handle_help, "Display help list, or specific help on a command"),
1584 AST_CLI_DEFINE(handle_logger_mute, "Toggle logging output to a console"),
1586 AST_CLI_DEFINE(handle_modlist, "List modules and info"),
1588 AST_CLI_DEFINE(handle_load, "Load a module by name"),
1590 AST_CLI_DEFINE(handle_reload, "Reload configuration"),
1592 AST_CLI_DEFINE(handle_unload, "Unload a module by name"),
1594 AST_CLI_DEFINE(handle_showuptime, "Show uptime information"),
1596 AST_CLI_DEFINE(handle_softhangup, "Request a hangup on a given channel"),
1598 AST_CLI_DEFINE(handle_cli_reload_permissions, "Reload CLI permissions config"),
1600 AST_CLI_DEFINE(handle_cli_show_permissions, "Show CLI permissions"),
1602 AST_CLI_DEFINE(handle_cli_check_permissions, "Try a permissions config for a user"),
1606 * Some regexp characters in cli arguments are reserved and used as separators.
1608 static const char cli_rsvd[] = "[]{}|*%";
1611 * initialize the _full_cmd string and related parameters,
1612 * return 0 on success, -1 on error.
1614 static int set_full_cmd(struct ast_cli_entry *e)
1619 ast_join(buf, sizeof(buf), e->cmda);
1620 e->_full_cmd = ast_strdup(buf);
1621 if (!e->_full_cmd) {
1622 ast_log(LOG_WARNING, "-- cannot allocate <%s>\n", buf);
1625 e->cmdlen = strcspn(e->_full_cmd, cli_rsvd);
1626 for (i = 0; e->cmda[i]; i++)
1632 /*! \brief cleanup (free) cli_perms linkedlist. */
1633 static void destroy_user_perms(void)
1635 struct cli_perm *perm;
1636 struct usergroup_cli_perm *user_perm;
1638 AST_RWLIST_WRLOCK(&cli_perms);
1639 while ((user_perm = AST_LIST_REMOVE_HEAD(&cli_perms, list))) {
1640 while ((perm = AST_LIST_REMOVE_HEAD(user_perm->perms, list))) {
1641 ast_free(perm->command);
1644 ast_free(user_perm);
1646 AST_RWLIST_UNLOCK(&cli_perms);
1649 int ast_cli_perms_init(int reload)
1651 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1652 struct ast_config *cfg;
1654 struct ast_variable *v;
1655 struct usergroup_cli_perm *user_group, *cp_entry;
1656 struct cli_perm *perm = NULL;
1660 if (ast_mutex_trylock(&permsconfiglock)) {
1661 ast_log(LOG_NOTICE, "You must wait until last 'cli reload permissions' command finish\n");
1665 cfg = ast_config_load2(perms_config, "" /* core, can't reload */, config_flags);
1667 ast_mutex_unlock(&permsconfiglock);
1669 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
1670 ast_mutex_unlock(&permsconfiglock);
1674 /* free current structures. */
1675 destroy_user_perms();
1677 while ((cat = ast_category_browse(cfg, cat))) {
1678 if (!strcasecmp(cat, "general")) {
1679 /* General options */
1680 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
1681 if (!strcasecmp(v->name, "default_perm")) {
1682 cli_default_perm = (!strcasecmp(v->value, "permit")) ? 1: 0;
1688 /* users or groups */
1689 gr = NULL, pw = NULL;
1690 if (cat[0] == '@') {
1691 /* This is a group */
1692 gr = getgrnam(&cat[1]);
1694 ast_log (LOG_WARNING, "Unknown group '%s'\n", &cat[1]);
1698 /* This is a user */
1701 ast_log (LOG_WARNING, "Unknown user '%s'\n", cat);
1706 /* Check for duplicates */
1707 AST_RWLIST_WRLOCK(&cli_perms);
1708 AST_LIST_TRAVERSE(&cli_perms, cp_entry, list) {
1709 if ((pw && cp_entry->uid == pw->pw_uid) || (gr && cp_entry->gid == gr->gr_gid)) {
1710 /* if it is duplicated, just added this new settings, to
1711 the current list. */
1712 user_group = cp_entry;
1716 AST_RWLIST_UNLOCK(&cli_perms);
1719 /* alloc space for the new user config. */
1720 user_group = ast_calloc(1, sizeof(*user_group));
1724 user_group->uid = (pw ? pw->pw_uid : -1);
1725 user_group->gid = (gr ? gr->gr_gid : -1);
1726 user_group->perms = ast_calloc(1, sizeof(*user_group->perms));
1727 if (!user_group->perms) {
1728 ast_free(user_group);
1732 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
1733 if (ast_strlen_zero(v->value)) {
1734 /* we need to check this condition cause it could break security. */
1735 ast_log(LOG_WARNING, "Empty permit/deny option in user '%s'\n", cat);
1738 if (!strcasecmp(v->name, "permit")) {
1739 perm = ast_calloc(1, sizeof(*perm));
1742 perm->command = ast_strdup(v->value);
1744 } else if (!strcasecmp(v->name, "deny")) {
1745 perm = ast_calloc(1, sizeof(*perm));
1748 perm->command = ast_strdup(v->value);
1751 /* up to now, only 'permit' and 'deny' are possible values. */
1752 ast_log(LOG_WARNING, "Unknown '%s' option\n", v->name);
1756 /* Added the permission to the user's list. */
1757 AST_LIST_INSERT_TAIL(user_group->perms, perm, list);
1761 AST_RWLIST_WRLOCK(&cli_perms);
1762 AST_RWLIST_INSERT_TAIL(&cli_perms, user_group, list);
1763 AST_RWLIST_UNLOCK(&cli_perms);
1766 ast_config_destroy(cfg);
1767 ast_mutex_unlock(&permsconfiglock);
1771 /*! \brief initialize the _full_cmd string in * each of the builtins. */
1772 void ast_builtins_init(void)
1774 ast_cli_register_multiple(cli_cli, ARRAY_LEN(cli_cli));
1778 * match a word in the CLI entry.
1779 * returns -1 on mismatch, 0 on match of an optional word,
1780 * 1 on match of a full word.
1782 * The pattern can be
1783 * any_word match for equal
1784 * [foo|bar|baz] optionally, one of these words
1785 * {foo|bar|baz} exactly, one of these words
1788 static int word_match(const char *cmd, const char *cli_word)
1793 if (ast_strlen_zero(cmd) || ast_strlen_zero(cli_word))
1795 if (!strchr(cli_rsvd, cli_word[0])) /* normal match */
1796 return (strcasecmp(cmd, cli_word) == 0) ? 1 : -1;
1797 /* regexp match, takes [foo|bar] or {foo|bar} */
1799 /* wildcard match - will extend in the future */
1800 if (l > 0 && cli_word[0] == '%') {
1801 return 1; /* wildcard */
1803 pos = strcasestr(cli_word, cmd);
1804 if (pos == NULL) /* not found, say ok if optional */
1805 return cli_word[0] == '[' ? 0 : -1;
1806 if (pos == cli_word) /* no valid match at the beginning */
1808 if (strchr(cli_rsvd, pos[-1]) && strchr(cli_rsvd, pos[l]))
1809 return 1; /* valid match */
1810 return -1; /* not found */
1813 /*! \brief if word is a valid prefix for token, returns the pos-th
1814 * match as a malloced string, or NULL otherwise.
1815 * Always tell in *actual how many matches we got.
1817 static char *is_prefix(const char *word, const char *token,
1818 int pos, int *actual)
1824 if (ast_strlen_zero(token))
1826 if (ast_strlen_zero(word))
1827 word = ""; /* dummy */
1829 if (strcspn(word, cli_rsvd) != lw)
1830 return NULL; /* no match if word has reserved chars */
1831 if (strchr(cli_rsvd, token[0]) == NULL) { /* regular match */
1832 if (strncasecmp(token, word, lw)) /* no match */
1835 return (pos != 0) ? NULL : ast_strdup(token);
1837 /* now handle regexp match */
1839 /* Wildcard always matches, so we never do is_prefix on them */
1841 t1 = ast_strdupa(token + 1); /* copy, skipping first char */
1842 while (pos >= 0 && (s = strsep(&t1, cli_rsvd)) && *s) {
1843 if (*s == '%') /* wildcard */
1845 if (strncasecmp(s, word, lw)) /* no match */
1849 return ast_strdup(s);
1856 * \brief locate a cli command in the 'helpers' list (which must be locked).
1857 * The search compares word by word taking care of regexps in e->cmda
1858 * This function will return NULL when nothing is matched, or the ast_cli_entry that matched.
1860 * \param match_type has 3 possible values:
1861 * 0 returns if the search key is equal or longer than the entry.
1862 * note that trailing optional arguments are skipped.
1863 * -1 true if the mismatch is on the last word XXX not true!
1864 * 1 true only on complete, exact match.
1867 static struct ast_cli_entry *find_cli(const char * const cmds[], int match_type)
1869 int matchlen = -1; /* length of longest match so far */
1870 struct ast_cli_entry *cand = NULL, *e=NULL;
1872 while ( (e = cli_next(e)) ) {
1873 /* word-by word regexp comparison */
1874 const char * const *src = cmds;
1875 const char * const *dst = e->cmda;
1877 for (;; dst++, src += n) {
1878 n = word_match(*src, *dst);
1882 if (ast_strlen_zero(*dst) || ((*dst)[0] == '[' && ast_strlen_zero(dst[1]))) {
1883 /* no more words in 'e' */
1884 if (ast_strlen_zero(*src)) /* exact match, cannot do better */
1886 /* Here, cmds has more words than the entry 'e' */
1887 if (match_type != 0) /* but we look for almost exact match... */
1888 continue; /* so we skip this one. */
1889 /* otherwise we like it (case 0) */
1890 } else { /* still words in 'e' */
1891 if (ast_strlen_zero(*src))
1892 continue; /* cmds is shorter than 'e', not good */
1893 /* Here we have leftover words in cmds and 'e',
1894 * but there is a mismatch. We only accept this one if match_type == -1
1895 * and this is the last word for both.
1897 if (match_type != -1 || !ast_strlen_zero(src[1]) ||
1898 !ast_strlen_zero(dst[1])) /* not the one we look for */
1900 /* good, we are in case match_type == -1 and mismatch on last word */
1902 if (src - cmds > matchlen) { /* remember the candidate */
1903 matchlen = src - cmds;
1908 return e ? e : cand;
1911 static char *find_best(const char *argv[])
1913 static char cmdline[80];
1915 /* See how close we get, then print the candidate */
1916 const char *myargv[AST_MAX_CMD_LEN] = { NULL, };
1918 AST_RWLIST_RDLOCK(&helpers);
1919 for (x = 0; argv[x]; x++) {
1920 myargv[x] = argv[x];
1921 if (!find_cli(myargv, -1))
1924 AST_RWLIST_UNLOCK(&helpers);
1925 ast_join(cmdline, sizeof(cmdline), myargv);
1929 static int __ast_cli_unregister(struct ast_cli_entry *e, struct ast_cli_entry *ed)
1932 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
1934 AST_RWLIST_WRLOCK(&helpers);
1935 AST_RWLIST_REMOVE(&helpers, e, list);
1936 AST_RWLIST_UNLOCK(&helpers);
1937 ast_free(e->_full_cmd);
1938 e->_full_cmd = NULL;
1940 /* this is a new-style entry. Reset fields and free memory. */
1941 char *cmda = (char *) e->cmda;
1942 memset(cmda, '\0', sizeof(e->cmda));
1943 ast_free(e->command);
1951 static int __ast_cli_register(struct ast_cli_entry *e, struct ast_cli_entry *ed)
1953 struct ast_cli_entry *cur;
1954 int i, lf, ret = -1;
1956 struct ast_cli_args a; /* fake argument */
1957 char **dst = (char **)e->cmda; /* need to cast as the entry is readonly */
1960 memset(&a, '\0', sizeof(a));
1961 e->handler(e, CLI_INIT, &a);
1962 /* XXX check that usage and command are filled up */
1963 s = ast_skip_blanks(e->command);
1964 s = e->command = ast_strdup(s);
1965 for (i=0; !ast_strlen_zero(s) && i < AST_MAX_CMD_LEN-1; i++) {
1966 *dst++ = s; /* store string */
1967 s = ast_skip_nonblanks(s);
1968 if (*s == '\0') /* we are done */
1971 s = ast_skip_blanks(s);
1975 AST_RWLIST_WRLOCK(&helpers);
1977 if (find_cli(e->cmda, 1)) {
1978 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", S_OR(e->_full_cmd, e->command));
1981 if (set_full_cmd(e))
1985 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&helpers, cur, list) {
1986 int len = cur->cmdlen;
1989 if (strncasecmp(e->_full_cmd, cur->_full_cmd, len) < 0) {
1990 AST_RWLIST_INSERT_BEFORE_CURRENT(e, list);
1994 AST_RWLIST_TRAVERSE_SAFE_END;
1997 AST_RWLIST_INSERT_TAIL(&helpers, e, list);
1998 ret = 0; /* success */
2001 AST_RWLIST_UNLOCK(&helpers);
2006 /* wrapper function, so we can unregister deprecated commands recursively */
2007 int ast_cli_unregister(struct ast_cli_entry *e)
2009 return __ast_cli_unregister(e, NULL);
2012 /* wrapper function, so we can register deprecated commands recursively */
2013 int ast_cli_register(struct ast_cli_entry *e)
2015 return __ast_cli_register(e, NULL);
2019 * register/unregister an array of entries.
2021 int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
2025 for (i = 0; i < len; i++)
2026 res |= ast_cli_register(e + i);
2031 int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
2035 for (i = 0; i < len; i++)
2036 res |= ast_cli_unregister(e + i);
2042 /*! \brief helper for final part of handle_help
2043 * if locked = 1, assume the list is already locked
2045 static char *help1(int fd, const char * const match[], int locked)
2047 char matchstr[80] = "";
2048 struct ast_cli_entry *e = NULL;
2053 ast_join(matchstr, sizeof(matchstr), match);
2054 len = strlen(matchstr);
2057 AST_RWLIST_RDLOCK(&helpers);
2058 while ( (e = cli_next(e)) ) {
2059 /* Hide commands that start with '_' */
2060 if (e->_full_cmd[0] == '_')
2062 if (match && strncasecmp(matchstr, e->_full_cmd, len))
2064 ast_cli(fd, "%30.30s %s\n", e->_full_cmd, S_OR(e->summary, "<no description available>"));
2068 AST_RWLIST_UNLOCK(&helpers);
2069 if (!found && matchstr[0])
2070 ast_cli(fd, "No such command '%s'.\n", matchstr);
2074 static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2077 struct ast_cli_entry *my_e;
2078 char *res = CLI_SUCCESS;
2080 if (cmd == CLI_INIT) {
2081 e->command = "core show help";
2083 "Usage: core show help [topic]\n"
2084 " When called with a topic as an argument, displays usage\n"
2085 " information on the given command. If called without a\n"
2086 " topic, it provides a list of commands.\n";
2089 } else if (cmd == CLI_GENERATE) {
2090 /* skip first 14 or 15 chars, "core show help " */
2091 int l = strlen(a->line);
2096 /* XXX watch out, should stop to the non-generator parts */
2097 return __ast_cli_generator(a->line + l, a->word, a->n, 0);
2099 if (a->argc == e->args) {
2100 return help1(a->fd, NULL, 0);
2103 AST_RWLIST_RDLOCK(&helpers);
2104 my_e = find_cli(a->argv + 3, 1); /* try exact match first */
2106 res = help1(a->fd, a->argv + 3, 1 /* locked */);
2107 AST_RWLIST_UNLOCK(&helpers);
2111 ast_cli(a->fd, "%s", my_e->usage);
2113 ast_join(fullcmd, sizeof(fullcmd), a->argv + 3);
2114 ast_cli(a->fd, "No help text available for '%s'.\n", fullcmd);
2116 AST_RWLIST_UNLOCK(&helpers);
2120 static char *parse_args(const char *s, int *argc, const char *argv[], int max, int *trailingwhitespace)
2122 char *duplicate, *cur;
2129 if (trailingwhitespace == NULL)
2130 trailingwhitespace = &dummy;
2131 *trailingwhitespace = 0;
2132 if (s == NULL) /* invalid, though! */
2134 /* make a copy to store the parsed string */
2135 if (!(duplicate = ast_strdup(s)))
2139 /* scan the original string copying into cur when needed */
2142 ast_log(LOG_WARNING, "Too many arguments, truncating at %s\n", s);
2145 if (*s == '"' && !escaped) {
2147 if (quoted && whitespace) {
2148 /* start a quoted string from previous whitespace: new argument */
2152 } else if ((*s == ' ' || *s == '\t') && !(quoted || escaped)) {
2153 /* If we are not already in whitespace, and not in a quoted string or
2154 processing an escape sequence, and just entered whitespace, then
2155 finalize the previous argument and remember that we are in whitespace
2161 } else if (*s == '\\' && !escaped) {
2165 /* we leave whitespace, and are not quoted. So it's a new argument */
2173 /* Null terminate */
2175 /* XXX put a NULL in the last argument, because some functions that take
2176 * the array may want a null-terminated array.
2177 * argc still reflects the number of non-NULL entries.
2181 *trailingwhitespace = whitespace;
2185 /*! \brief Return the number of unique matches for the generator */
2186 int ast_cli_generatornummatches(const char *text, const char *word)
2188 int matches = 0, i = 0;
2189 char *buf = NULL, *oldbuf = NULL;
2191 while ((buf = ast_cli_generator(text, word, i++))) {
2192 if (!oldbuf || strcmp(buf,oldbuf))
2203 char **ast_cli_completion_matches(const char *text, const char *word)
2205 char **match_list = NULL, *retstr, *prevstr;
2206 size_t match_list_len, max_equal, which, i;
2209 /* leave entry 0 free for the longest common substring */
2211 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
2212 if (matches + 1 >= match_list_len) {
2213 match_list_len <<= 1;
2214 if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
2217 match_list[++matches] = retstr;
2221 return match_list; /* NULL */
2223 /* Find the longest substring that is common to all results
2224 * (it is a candidate for completion), and store a copy in entry 0.
2226 prevstr = match_list[1];
2227 max_equal = strlen(prevstr);
2228 for (which = 2; which <= matches; which++) {
2229 for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
2234 if (!(retstr = ast_malloc(max_equal + 1)))
2237 ast_copy_string(retstr, match_list[1], max_equal + 1);
2238 match_list[0] = retstr;
2240 /* ensure that the array is NULL terminated */
2241 if (matches + 1 >= match_list_len) {
2242 if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list))))
2245 match_list[matches + 1] = NULL;
2250 /*! \brief returns true if there are more words to match */
2251 static int more_words (const char * const *dst)
2254 for (i = 0; dst[i]; i++) {
2255 if (dst[i][0] != '[')
2262 * generate the entry at position 'state'
2264 static char *__ast_cli_generator(const char *text, const char *word, int state, int lock)
2266 const char *argv[AST_MAX_ARGS];
2267 struct ast_cli_entry *e = NULL;
2268 int x = 0, argindex, matchlen;
2271 char matchstr[80] = "";
2273 /* Split the argument into an array of words */
2274 char *duplicate = parse_args(text, &x, argv, ARRAY_LEN(argv), &tws);
2276 if (!duplicate) /* malloc error */
2279 /* Compute the index of the last argument (could be an empty string) */
2280 argindex = (!ast_strlen_zero(word) && x>0) ? x-1 : x;
2282 /* rebuild the command, ignore terminating white space and flatten space */
2283 ast_join(matchstr, sizeof(matchstr)-1, argv);
2284 matchlen = strlen(matchstr);
2286 strcat(matchstr, " "); /* XXX */
2291 AST_RWLIST_RDLOCK(&helpers);
2292 while ( (e = cli_next(e)) ) {
2293 /* XXX repeated code */
2294 int src = 0, dst = 0, n = 0;
2296 if (e->command[0] == '_')
2300 * Try to match words, up to and excluding the last word, which
2301 * is either a blank or something that we want to extend.
2303 for (;src < argindex; dst++, src += n) {
2304 n = word_match(argv[src], e->cmda[dst]);
2309 if (src != argindex && more_words(e->cmda + dst)) /* not a match */
2311 ret = is_prefix(argv[src], e->cmda[dst], state - matchnum, &n);
2312 matchnum += n; /* this many matches here */
2315 * argv[src] is a valid prefix of the next word in this
2316 * command. If this is also the correct entry, return it.
2318 if (matchnum > state)
2322 } else if (ast_strlen_zero(e->cmda[dst])) {
2324 * This entry is a prefix of the command string entered
2325 * (only one entry in the list should have this property).
2326 * Run the generator if one is available. In any case we are done.
2328 if (e->handler) { /* new style command */
2329 struct ast_cli_args a = {
2330 .line = matchstr, .word = word,
2332 .n = state - matchnum,
2335 ret = e->handler(e, CLI_GENERATE, &a);
2342 AST_RWLIST_UNLOCK(&helpers);
2343 ast_free(duplicate);
2347 char *ast_cli_generator(const char *text, const char *word, int state)
2349 return __ast_cli_generator(text, word, state, 1);
2352 int ast_cli_command_full(int uid, int gid, int fd, const char *s)
2354 const char *args[AST_MAX_ARGS + 1];
2355 struct ast_cli_entry *e;
2357 char *duplicate = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
2358 char tmp[AST_MAX_ARGS + 1];
2359 char *retval = NULL;
2360 struct ast_cli_args a = {
2361 .fd = fd, .argc = x, .argv = args+1 };
2363 if (duplicate == NULL)
2366 if (x < 1) /* We need at least one entry, otherwise ignore */
2369 AST_RWLIST_RDLOCK(&helpers);
2370 e = find_cli(args + 1, 0);
2372 ast_atomic_fetchadd_int(&e->inuse, 1);
2373 AST_RWLIST_UNLOCK(&helpers);
2375 ast_cli(fd, "No such command '%s' (type 'core show help %s' for other possible commands)\n", s, find_best(args + 1));
2379 ast_join(tmp, sizeof(tmp), args + 1);
2380 /* Check if the user has rights to run this command. */
2381 if (!cli_has_permissions(uid, gid, tmp)) {
2382 ast_cli(fd, "You don't have permissions to run '%s' command\n", tmp);
2383 ast_free(duplicate);
2388 * Within the handler, argv[-1] contains a pointer to the ast_cli_entry.
2389 * Remember that the array returned by parse_args is NULL-terminated.
2391 args[0] = (char *)e;
2393 retval = e->handler(e, CLI_HANDLER, &a);
2395 if (retval == CLI_SHOWUSAGE) {
2396 ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
2398 if (retval == CLI_FAILURE)
2399 ast_cli(fd, "Command '%s' failed.\n", s);
2401 ast_atomic_fetchadd_int(&e->inuse, -1);
2403 ast_free(duplicate);
2407 int ast_cli_command_multiple_full(int uid, int gid, int fd, size_t size, const char *s)
2410 int x, y = 0, count = 0;
2412 for (x = 0; x < size; x++) {
2416 ast_cli_command_full(uid, gid, fd, cmd);