00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 267139 $")
00029
00030 #include "asterisk/_private.h"
00031 #include "asterisk/paths.h"
00032 #include <sys/signal.h>
00033 #include <signal.h>
00034 #include <ctype.h>
00035 #include <regex.h>
00036 #include <pwd.h>
00037 #include <grp.h>
00038
00039 #include "asterisk/cli.h"
00040 #include "asterisk/linkedlists.h"
00041 #include "asterisk/module.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/channel.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/lock.h"
00047 #include "editline/readline/readline.h"
00048 #include "asterisk/threadstorage.h"
00049
00050
00051
00052
00053 struct cli_perm {
00054 unsigned int permit:1;
00055 char *command;
00056 AST_LIST_ENTRY(cli_perm) list;
00057 };
00058
00059 AST_LIST_HEAD_NOLOCK(cli_perm_head, cli_perm);
00060
00061
00062 struct usergroup_cli_perm {
00063 int uid;
00064 int gid;
00065 struct cli_perm_head *perms;
00066 AST_LIST_ENTRY(usergroup_cli_perm) list;
00067 };
00068
00069 static const char perms_config[] = "cli_permissions.conf";
00070
00071 static int cli_default_perm = 1;
00072
00073
00074
00075 AST_MUTEX_DEFINE_STATIC(permsconfiglock);
00076
00077 AST_RWLIST_HEAD_STATIC(cli_perms, usergroup_cli_perm);
00078
00079
00080
00081
00082 struct ast_debug_file {
00083 unsigned int level;
00084 AST_RWLIST_ENTRY(ast_debug_file) entry;
00085 char filename[0];
00086 };
00087
00088 AST_RWLIST_HEAD(debug_file_list, ast_debug_file);
00089
00090
00091 static struct debug_file_list debug_files;
00092
00093 static struct debug_file_list verbose_files;
00094
00095 AST_THREADSTORAGE(ast_cli_buf);
00096
00097
00098 #define AST_CLI_INITLEN 256
00099
00100 void ast_cli(int fd, const char *fmt, ...)
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 }
00117
00118 unsigned int ast_debug_get_by_file(const char *file)
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 }
00134
00135 unsigned int ast_verbose_get_by_file(const char *file)
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 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 static int cli_has_permissions(int uid, int gid, const char *command)
00166 {
00167 struct usergroup_cli_perm *user_perm;
00168 struct cli_perm *perm;
00169
00170 int isallowg = cli_default_perm, isallowu = -1, ispattern;
00171 regex_t regexbuf;
00172
00173
00174
00175
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
00192 ispattern = !regcomp(®exbuf, perm->command, REG_EXTENDED | REG_NOSUB | REG_ICASE);
00193 if (ispattern && regexec(®exbuf, command, 0, NULL, 0)) {
00194 regfree(®exbuf);
00195 continue;
00196 }
00197 if (!ispattern) {
00198 continue;
00199 }
00200 regfree(®exbuf);
00201 }
00202 if (user_perm->uid == uid) {
00203
00204 isallowu = perm->permit;
00205 } else {
00206
00207 isallowg = perm->permit;
00208 }
00209 }
00210 }
00211 AST_RWLIST_UNLOCK(&cli_perms);
00212 if (isallowu > -1) {
00213
00214 isallowg = isallowu;
00215 }
00216
00217 return isallowg;
00218 }
00219
00220 static AST_RWLIST_HEAD_STATIC(helpers, ast_cli_entry);
00221
00222 static char *complete_fn(const char *word, int state)
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 }
00243
00244 static char *handle_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00245 {
00246
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 }
00269
00270 static char *handle_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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
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 }
00304
00305
00306
00307
00308
00309 static struct ast_debug_file *find_debug_file(const char *fn, unsigned int debug)
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 }
00321
00322 static char *complete_number(const char *partial, unsigned int min, unsigned int max, int n)
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 }
00361
00362 static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00363 {
00364 int oldval;
00365 int newlevel;
00366 int atleast = 0;
00367 int fd = a->fd;
00368 int argc = a->argc;
00369 char **argv = a->argv;
00370 char *argv3 = a->argv ? S_OR(a->argv[3], "") : "";
00371 int *dst;
00372 char *what;
00373 struct debug_file_list *dfl;
00374 struct ast_debug_file *adf;
00375 char *fn;
00376
00377 switch (cmd) {
00378 case CLI_INIT:
00379 e->command = "core set {debug|verbose}";
00380 e->usage =
00381 #if !defined(LOW_MEMORY)
00382 "Usage: core set {debug|verbose} [atleast] <level> [filename]\n"
00383 #else
00384 "Usage: core set {debug|verbose} [atleast] <level>\n"
00385 #endif
00386 " core set {debug|verbose} off\n"
00387 #if !defined(LOW_MEMORY)
00388 " Sets level of debug or verbose messages to be displayed or \n"
00389 " sets a filename to display debug messages from.\n"
00390 #else
00391 " Sets level of debug or verbose messages to be displayed.\n"
00392 #endif
00393 " 0 or off means no messages should be displayed.\n"
00394 " Equivalent to -d[d[...]] or -v[v[v...]] on startup\n";
00395 return NULL;
00396
00397 case CLI_GENERATE:
00398 if (a->pos == 3 || (a->pos == 4 && !strcasecmp(a->argv[3], "atleast"))) {
00399 char *pos = a->pos == 3 ? argv3 : S_OR(a->argv[4], "");
00400 int numbermatch = (ast_strlen_zero(pos) || strchr("123456789", pos[0])) ? 0 : 21;
00401 if (a->n < 21 && numbermatch == 0) {
00402 return complete_number(pos, 0, 0x7fffffff, a->n);
00403 } else if (pos[0] == '0') {
00404 if (a->n == 0) {
00405 return ast_strdup("0");
00406 } else {
00407 return NULL;
00408 }
00409 } else if (a->n == (21 - numbermatch)) {
00410 if (a->pos == 3 && !strncasecmp(argv3, "off", strlen(argv3))) {
00411 return ast_strdup("off");
00412 } else if (a->pos == 3 && !strncasecmp(argv3, "atleast", strlen(argv3))) {
00413 return ast_strdup("atleast");
00414 }
00415 } else if (a->n == (22 - numbermatch) && a->pos == 3 && ast_strlen_zero(argv3)) {
00416 return ast_strdup("atleast");
00417 }
00418 #if !defined(LOW_MEMORY)
00419 } else if (a->pos == 4 || (a->pos == 5 && !strcasecmp(argv3, "atleast"))) {
00420 return ast_complete_source_filename(a->pos == 4 ? S_OR(a->argv[4], "") : S_OR(a->argv[5], ""), a->n);
00421 #endif
00422 }
00423 return NULL;
00424 }
00425
00426
00427
00428
00429 if (argc <= e->args)
00430 return CLI_SHOWUSAGE;
00431 if (!strcasecmp(argv[e->args - 1], "debug")) {
00432 dst = &option_debug;
00433 oldval = option_debug;
00434 what = "Core debug";
00435 } else {
00436 dst = &option_verbose;
00437 oldval = option_verbose;
00438 what = "Verbosity";
00439 }
00440 if (argc == e->args + 1 && !strcasecmp(argv[e->args], "off")) {
00441 unsigned int debug = (*what == 'C');
00442 newlevel = 0;
00443
00444 dfl = debug ? &debug_files : &verbose_files;
00445
00446 AST_RWLIST_WRLOCK(dfl);
00447 while ((adf = AST_RWLIST_REMOVE_HEAD(dfl, entry)))
00448 ast_free(adf);
00449 ast_clear_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_FILE : AST_OPT_FLAG_VERBOSE_FILE);
00450 AST_RWLIST_UNLOCK(dfl);
00451
00452 goto done;
00453 }
00454 if (!strcasecmp(argv[e->args], "atleast"))
00455 atleast = 1;
00456 if (argc != e->args + atleast + 1 && argc != e->args + atleast + 2)
00457 return CLI_SHOWUSAGE;
00458 if (sscanf(argv[e->args + atleast], "%30d", &newlevel) != 1)
00459 return CLI_SHOWUSAGE;
00460 if (argc == e->args + atleast + 2) {
00461 unsigned int debug = (*what == 'C');
00462 dfl = debug ? &debug_files : &verbose_files;
00463
00464 fn = argv[e->args + atleast + 1];
00465
00466 AST_RWLIST_WRLOCK(dfl);
00467
00468 if ((adf = find_debug_file(fn, debug)) && !newlevel) {
00469 AST_RWLIST_REMOVE(dfl, adf, entry);
00470 if (AST_RWLIST_EMPTY(dfl))
00471 ast_clear_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_FILE : AST_OPT_FLAG_VERBOSE_FILE);
00472 AST_RWLIST_UNLOCK(dfl);
00473 ast_cli(fd, "%s was %d and has been set to 0 for '%s'\n", what, adf->level, fn);
00474 ast_free(adf);
00475 return CLI_SUCCESS;
00476 }
00477
00478 if (adf) {
00479 if ((atleast && newlevel < adf->level) || adf->level == newlevel) {
00480 ast_cli(fd, "%s is %d for '%s'\n", what, adf->level, fn);
00481 AST_RWLIST_UNLOCK(dfl);
00482 return CLI_SUCCESS;
00483 }
00484 } else if (!(adf = ast_calloc(1, sizeof(*adf) + strlen(fn) + 1))) {
00485 AST_RWLIST_UNLOCK(dfl);
00486 return CLI_FAILURE;
00487 }
00488
00489 oldval = adf->level;
00490 adf->level = newlevel;
00491 strcpy(adf->filename, fn);
00492
00493 ast_set_flag(&ast_options, debug ? AST_OPT_FLAG_DEBUG_FILE : AST_OPT_FLAG_VERBOSE_FILE);
00494
00495 AST_RWLIST_INSERT_TAIL(dfl, adf, entry);
00496 AST_RWLIST_UNLOCK(dfl);
00497
00498 ast_cli(fd, "%s was %d and has been set to %d for '%s'\n", what, oldval, adf->level, adf->filename);
00499
00500 return CLI_SUCCESS;
00501 }
00502
00503 done:
00504 if (!atleast || newlevel > *dst)
00505 *dst = newlevel;
00506 if (oldval > 0 && *dst == 0)
00507 ast_cli(fd, "%s is now OFF\n", what);
00508 else if (*dst > 0) {
00509 if (oldval == *dst)
00510 ast_cli(fd, "%s is at least %d\n", what, *dst);
00511 else
00512 ast_cli(fd, "%s was %d and is now %d\n", what, oldval, *dst);
00513 }
00514
00515 return CLI_SUCCESS;
00516 }
00517
00518 static char *handle_logger_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00519 {
00520 switch (cmd) {
00521 case CLI_INIT:
00522 e->command = "logger mute";
00523 e->usage =
00524 "Usage: logger mute\n"
00525 " Disables logging output to the current console, making it possible to\n"
00526 " gather information without being disturbed by scrolling lines.\n";
00527 return NULL;
00528 case CLI_GENERATE:
00529 return NULL;
00530 }
00531
00532 if (a->argc < 2 || a->argc > 3)
00533 return CLI_SHOWUSAGE;
00534
00535 if (a->argc == 3 && !strcasecmp(a->argv[2], "silent"))
00536 ast_console_toggle_mute(a->fd, 1);
00537 else
00538 ast_console_toggle_mute(a->fd, 0);
00539
00540 return CLI_SUCCESS;
00541 }
00542
00543 static char *handle_unload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00544 {
00545
00546 int x;
00547 int force = AST_FORCE_SOFT;
00548 char *s;
00549
00550 switch (cmd) {
00551 case CLI_INIT:
00552 e->command = "module unload";
00553 e->usage =
00554 "Usage: module unload [-f|-h] <module_1> [<module_2> ... ]\n"
00555 " Unloads the specified module from Asterisk. The -f\n"
00556 " option causes the module to be unloaded even if it is\n"
00557 " in use (may cause a crash) and the -h module causes the\n"
00558 " module to be unloaded even if the module says it cannot, \n"
00559 " which almost always will cause a crash.\n";
00560 return NULL;
00561
00562 case CLI_GENERATE:
00563 return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
00564 }
00565 if (a->argc < e->args + 1)
00566 return CLI_SHOWUSAGE;
00567 x = e->args;
00568 s = a->argv[x];
00569 if (s[0] == '-') {
00570 if (s[1] == 'f')
00571 force = AST_FORCE_FIRM;
00572 else if (s[1] == 'h')
00573 force = AST_FORCE_HARD;
00574 else
00575 return CLI_SHOWUSAGE;
00576 if (a->argc < e->args + 2)
00577 return CLI_SHOWUSAGE;
00578 x++;
00579 }
00580
00581 for (; x < a->argc; x++) {
00582 if (ast_unload_resource(a->argv[x], force)) {
00583 ast_cli(a->fd, "Unable to unload resource %s\n", a->argv[x]);
00584 return CLI_FAILURE;
00585 }
00586 ast_cli(a->fd, "Unloaded %s\n", a->argv[x]);
00587 }
00588
00589 return CLI_SUCCESS;
00590 }
00591
00592 #define MODLIST_FORMAT "%-30s %-40.40s %-10d\n"
00593 #define MODLIST_FORMAT2 "%-30s %-40.40s %-10s\n"
00594
00595 AST_MUTEX_DEFINE_STATIC(climodentrylock);
00596 static int climodentryfd = -1;
00597
00598 static int modlist_modentry(const char *module, const char *description, int usecnt, const char *like)
00599 {
00600
00601 if (strcasestr(module, like) ) {
00602 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
00603 return 1;
00604 }
00605 return 0;
00606 }
00607
00608 static void print_uptimestr(int fd, struct timeval timeval, const char *prefix, int printsec)
00609 {
00610 int x;
00611 struct ast_str *out;
00612
00613 #define SECOND (1)
00614 #define MINUTE (SECOND*60)
00615 #define HOUR (MINUTE*60)
00616 #define DAY (HOUR*24)
00617 #define WEEK (DAY*7)
00618 #define YEAR (DAY*365)
00619 #define NEEDCOMMA(x) ((x)? ",": "")
00620 if (timeval.tv_sec < 0)
00621 return;
00622
00623 if (printsec) {
00624 ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval.tv_sec);
00625 return;
00626 }
00627 out = ast_str_alloca(256);
00628 if (timeval.tv_sec > YEAR) {
00629 x = (timeval.tv_sec / YEAR);
00630 timeval.tv_sec -= (x * YEAR);
00631 ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00632 }
00633 if (timeval.tv_sec > WEEK) {
00634 x = (timeval.tv_sec / WEEK);
00635 timeval.tv_sec -= (x * WEEK);
00636 ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00637 }
00638 if (timeval.tv_sec > DAY) {
00639 x = (timeval.tv_sec / DAY);
00640 timeval.tv_sec -= (x * DAY);
00641 ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00642 }
00643 if (timeval.tv_sec > HOUR) {
00644 x = (timeval.tv_sec / HOUR);
00645 timeval.tv_sec -= (x * HOUR);
00646 ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00647 }
00648 if (timeval.tv_sec > MINUTE) {
00649 x = (timeval.tv_sec / MINUTE);
00650 timeval.tv_sec -= (x * MINUTE);
00651 ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
00652 }
00653 x = timeval.tv_sec;
00654 if (x > 0 || ast_str_strlen(out) == 0)
00655 ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
00656 ast_cli(fd, "%s: %s\n", prefix, ast_str_buffer(out));
00657 }
00658
00659 static struct ast_cli_entry *cli_next(struct ast_cli_entry *e)
00660 {
00661 if (e) {
00662 return AST_LIST_NEXT(e, list);
00663 } else {
00664 return AST_LIST_FIRST(&helpers);
00665 }
00666 }
00667
00668 static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00669 {
00670 struct timeval curtime = ast_tvnow();
00671 int printsec;
00672
00673 switch (cmd) {
00674 case CLI_INIT:
00675 e->command = "core show uptime [seconds]";
00676 e->usage =
00677 "Usage: core show uptime [seconds]\n"
00678 " Shows Asterisk uptime information.\n"
00679 " The seconds word returns the uptime in seconds only.\n";
00680 return NULL;
00681
00682 case CLI_GENERATE:
00683 return NULL;
00684 }
00685
00686 if (a->argc == e->args && !strcasecmp(a->argv[e->args-1],"seconds"))
00687 printsec = 1;
00688 else if (a->argc == e->args-1)
00689 printsec = 0;
00690 else
00691 return CLI_SHOWUSAGE;
00692 if (ast_startuptime.tv_sec)
00693 print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
00694 if (ast_lastreloadtime.tv_sec)
00695 print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload", printsec);
00696 return CLI_SUCCESS;
00697 }
00698
00699 static char *handle_modlist(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00700 {
00701 char *like;
00702
00703 switch (cmd) {
00704 case CLI_INIT:
00705 e->command = "module show [like]";
00706 e->usage =
00707 "Usage: module show [like keyword]\n"
00708 " Shows Asterisk modules currently in use, and usage statistics.\n";
00709 return NULL;
00710
00711 case CLI_GENERATE:
00712 if (a->pos == e->args)
00713 return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
00714 else
00715 return NULL;
00716 }
00717
00718
00719
00720 if (a->argc == e->args - 1)
00721 like = "";
00722 else if (a->argc == e->args + 1 && !strcasecmp(a->argv[e->args-1], "like") )
00723 like = a->argv[e->args];
00724 else
00725 return CLI_SHOWUSAGE;
00726
00727 ast_mutex_lock(&climodentrylock);
00728 climodentryfd = a->fd;
00729 ast_cli(a->fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
00730 ast_cli(a->fd,"%d modules loaded\n", ast_update_module_list(modlist_modentry, like));
00731 climodentryfd = -1;
00732 ast_mutex_unlock(&climodentrylock);
00733 return CLI_SUCCESS;
00734 }
00735 #undef MODLIST_FORMAT
00736 #undef MODLIST_FORMAT2
00737
00738 static char *handle_showcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00739 {
00740 struct timeval curtime = ast_tvnow();
00741 int showuptime, printsec;
00742
00743 switch (cmd) {
00744 case CLI_INIT:
00745 e->command = "core show calls [uptime]";
00746 e->usage =
00747 "Usage: core show calls [uptime] [seconds]\n"
00748 " Lists number of currently active calls and total number of calls\n"
00749 " processed through PBX since last restart. If 'uptime' is specified\n"
00750 " the system uptime is also displayed. If 'seconds' is specified in\n"
00751 " addition to 'uptime', the system uptime is displayed in seconds.\n";
00752 return NULL;
00753
00754 case CLI_GENERATE:
00755 if (a->pos != e->args)
00756 return NULL;
00757 return a->n == 0 ? ast_strdup("seconds") : NULL;
00758 }
00759
00760
00761 if (a->argc >= e->args && !strcasecmp(a->argv[e->args-1],"uptime")) {
00762 showuptime = 1;
00763
00764 if (a->argc == e->args+1 && !strcasecmp(a->argv[e->args],"seconds"))
00765 printsec = 1;
00766 else if (a->argc == e->args)
00767 printsec = 0;
00768 else
00769 return CLI_SHOWUSAGE;
00770 } else if (a->argc == e->args-1) {
00771 showuptime = 0;
00772 printsec = 0;
00773 } else
00774 return CLI_SHOWUSAGE;
00775
00776 if (option_maxcalls) {
00777 ast_cli(a->fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
00778 ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
00779 ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
00780 } else {
00781 ast_cli(a->fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
00782 }
00783
00784 ast_cli(a->fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
00785
00786 if (ast_startuptime.tv_sec && showuptime) {
00787 print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
00788 }
00789
00790 return RESULT_SUCCESS;
00791 }
00792
00793 static char *handle_chanlist(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00794 {
00795 #define FORMAT_STRING "%-20.20s %-20.20s %-7.7s %-30.30s\n"
00796 #define FORMAT_STRING2 "%-20.20s %-20.20s %-7.7s %-30.30s\n"
00797 #define CONCISE_FORMAT_STRING "%s!%s!%s!%d!%s!%s!%s!%s!%s!%d!%s!%s!%s\n"
00798 #define VERBOSE_FORMAT_STRING "%-20.20s %-20.20s %-16.16s %4d %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"
00799 #define VERBOSE_FORMAT_STRING2 "%-20.20s %-20.20s %-16.16s %-4.4s %-7.7s %-12.12s %-25.25s %-15.15s %8.8s %-11.11s %-20.20s\n"
00800
00801 struct ast_channel *c = NULL;
00802 int numchans = 0, concise = 0, verbose = 0, count = 0;
00803 int fd, argc;
00804 char **argv;
00805
00806 switch (cmd) {
00807 case CLI_INIT:
00808 e->command = "core show channels [concise|verbose|count]";
00809 e->usage =
00810 "Usage: core show channels [concise|verbose|count]\n"
00811 " Lists currently defined channels and some information about them. If\n"
00812 " 'concise' is specified, the format is abridged and in a more easily\n"
00813 " machine parsable format. If 'verbose' is specified, the output includes\n"
00814 " more and longer fields. If 'count' is specified only the channel and call\n"
00815 " count is output.\n"
00816 " The 'concise' option is deprecated and will be removed from future versions\n"
00817 " of Asterisk.\n";
00818 return NULL;
00819
00820 case CLI_GENERATE:
00821 return NULL;
00822 }
00823 fd = a->fd;
00824 argc = a->argc;
00825 argv = a->argv;
00826
00827 if (a->argc == e->args) {
00828 if (!strcasecmp(argv[e->args-1],"concise"))
00829 concise = 1;
00830 else if (!strcasecmp(argv[e->args-1],"verbose"))
00831 verbose = 1;
00832 else if (!strcasecmp(argv[e->args-1],"count"))
00833 count = 1;
00834 else
00835 return CLI_SHOWUSAGE;
00836 } else if (a->argc != e->args - 1)
00837 return CLI_SHOWUSAGE;
00838
00839 if (!count) {
00840 if (!concise && !verbose)
00841 ast_cli(fd, FORMAT_STRING2, "Channel", "Location", "State", "Application(Data)");
00842 else if (verbose)
00843 ast_cli(fd, VERBOSE_FORMAT_STRING2, "Channel", "Context", "Extension", "Priority", "State", "Application", "Data",
00844 "CallerID", "Duration", "Accountcode", "BridgedTo");
00845 }
00846
00847 while ((c = ast_channel_walk_locked(c)) != NULL) {
00848 struct ast_channel *bc = ast_bridged_channel(c);
00849 char durbuf[10] = "-";
00850
00851 if (!count) {
00852 if ((concise || verbose) && c->cdr && !ast_tvzero(c->cdr->start)) {
00853 int duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
00854 if (verbose) {
00855 int durh = duration / 3600;
00856 int durm = (duration % 3600) / 60;
00857 int durs = duration % 60;
00858 snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
00859 } else {
00860 snprintf(durbuf, sizeof(durbuf), "%d", duration);
00861 }
00862 }
00863 if (concise) {
00864 ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
00865 c->appl ? c->appl : "(None)",
00866 S_OR(c->data, ""),
00867 S_OR(c->cid.cid_num, ""),
00868 S_OR(c->accountcode, ""),
00869 c->amaflags,
00870 durbuf,
00871 bc ? bc->name : "(None)",
00872 c->uniqueid);
00873 } else if (verbose) {
00874 ast_cli(fd, VERBOSE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
00875 c->appl ? c->appl : "(None)",
00876 c->data ? S_OR(c->data, "(Empty)" ): "(None)",
00877 S_OR(c->cid.cid_num, ""),
00878 durbuf,
00879 S_OR(c->accountcode, ""),
00880 bc ? bc->name : "(None)");
00881 } else {
00882 char locbuf[40] = "(None)";
00883 char appdata[40] = "(None)";
00884
00885 if (!ast_strlen_zero(c->context) && !ast_strlen_zero(c->exten))
00886 snprintf(locbuf, sizeof(locbuf), "%s@%s:%d", c->exten, c->context, c->priority);
00887 if (c->appl)
00888 snprintf(appdata, sizeof(appdata), "%s(%s)", c->appl, S_OR(c->data, ""));
00889 ast_cli(fd, FORMAT_STRING, c->name, locbuf, ast_state2str(c->_state), appdata);
00890 }
00891 }
00892 numchans++;
00893 ast_channel_unlock(c);
00894 }
00895 if (!concise) {
00896 ast_cli(fd, "%d active channel%s\n", numchans, ESS(numchans));
00897 if (option_maxcalls)
00898 ast_cli(fd, "%d of %d max active call%s (%5.2f%% of capacity)\n",
00899 ast_active_calls(), option_maxcalls, ESS(ast_active_calls()),
00900 ((double)ast_active_calls() / (double)option_maxcalls) * 100.0);
00901 else
00902 ast_cli(fd, "%d active call%s\n", ast_active_calls(), ESS(ast_active_calls()));
00903
00904 ast_cli(fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls()));
00905 }
00906 return CLI_SUCCESS;
00907
00908 #undef FORMAT_STRING
00909 #undef FORMAT_STRING2
00910 #undef CONCISE_FORMAT_STRING
00911 #undef VERBOSE_FORMAT_STRING
00912 #undef VERBOSE_FORMAT_STRING2
00913 }
00914
00915 static char *handle_softhangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00916 {
00917 struct ast_channel *c=NULL;
00918
00919 switch (cmd) {
00920 case CLI_INIT:
00921 e->command = "channel request hangup";
00922 e->usage =
00923 "Usage: channel request hangup <channel>\n"
00924 " Request that a channel be hung up. The hangup takes effect\n"
00925 " the next time the driver reads or writes from the channel\n";
00926 return NULL;
00927 case CLI_GENERATE:
00928 return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
00929 }
00930 if (a->argc != 4)
00931 return CLI_SHOWUSAGE;
00932 c = ast_get_channel_by_name_locked(a->argv[3]);
00933 if (c) {
00934 ast_cli(a->fd, "Requested Hangup on channel '%s'\n", c->name);
00935 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
00936 ast_channel_unlock(c);
00937 } else
00938 ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
00939 return CLI_SUCCESS;
00940 }
00941
00942
00943 static char *handle_cli_show_permissions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00944 {
00945 struct usergroup_cli_perm *cp;
00946 struct cli_perm *perm;
00947 struct passwd *pw = NULL;
00948 struct group *gr = NULL;
00949
00950 switch (cmd) {
00951 case CLI_INIT:
00952 e->command = "cli show permissions";
00953 e->usage =
00954 "Usage: cli show permissions\n"
00955 " Shows CLI configured permissions.\n";
00956 return NULL;
00957 case CLI_GENERATE:
00958 return NULL;
00959 }
00960
00961 AST_RWLIST_RDLOCK(&cli_perms);
00962 AST_LIST_TRAVERSE(&cli_perms, cp, list) {
00963 if (cp->uid >= 0) {
00964 pw = getpwuid(cp->uid);
00965 if (pw) {
00966 ast_cli(a->fd, "user: %s [uid=%d]\n", pw->pw_name, cp->uid);
00967 }
00968 } else {
00969 gr = getgrgid(cp->gid);
00970 if (gr) {
00971 ast_cli(a->fd, "group: %s [gid=%d]\n", gr->gr_name, cp->gid);
00972 }
00973 }
00974 ast_cli(a->fd, "Permissions:\n");
00975 if (cp->perms) {
00976 AST_LIST_TRAVERSE(cp->perms, perm, list) {
00977 ast_cli(a->fd, "\t%s -> %s\n", perm->permit ? "permit" : "deny", perm->command);
00978 }
00979 }
00980 ast_cli(a->fd, "\n");
00981 }
00982 AST_RWLIST_UNLOCK(&cli_perms);
00983
00984 return CLI_SUCCESS;
00985 }
00986
00987
00988 static char *handle_cli_reload_permissions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00989 {
00990 switch (cmd) {
00991 case CLI_INIT:
00992 e->command = "cli reload permissions";
00993 e->usage =
00994 "Usage: cli reload permissions\n"
00995 " Reload the 'cli_permissions.conf' file.\n";
00996 return NULL;
00997 case CLI_GENERATE:
00998 return NULL;
00999 }
01000
01001 ast_cli_perms_init(1);
01002
01003 return CLI_SUCCESS;
01004 }
01005
01006
01007 static char *handle_cli_check_permissions(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01008 {
01009 struct passwd *pw = NULL;
01010 struct group *gr;
01011 int gid = -1, uid = -1;
01012 char command[AST_MAX_ARGS] = "";
01013 struct ast_cli_entry *ce = NULL;
01014 int found = 0;
01015 char *group, *tmp;
01016
01017 switch (cmd) {
01018 case CLI_INIT:
01019 e->command = "cli check permissions";
01020 e->usage =
01021 "Usage: cli check permissions {<username>|@<groupname>|<username>@<groupname>} [<command>]\n"
01022 " Check permissions config for a user@group or list the allowed commands for the specified user.\n"
01023 " The username or the groupname may be omitted.\n";
01024 return NULL;
01025 case CLI_GENERATE:
01026 if (a->pos >= 4) {
01027 return ast_cli_generator(a->line + strlen("cli check permissions") + strlen(a->argv[3]) + 1, a->word, a->n);
01028 }
01029 return NULL;
01030 }
01031
01032 if (a->argc < 4) {
01033 return CLI_SHOWUSAGE;
01034 }
01035
01036 tmp = ast_strdupa(a->argv[3]);
01037 group = strchr(tmp, '@');
01038 if (group) {
01039 gr = getgrnam(&group[1]);
01040 if (!gr) {
01041 ast_cli(a->fd, "Unknown group '%s'\n", &group[1]);
01042 return CLI_FAILURE;
01043 }
01044 group[0] = '\0';
01045 gid = gr->gr_gid;
01046 }
01047
01048 if (!group && ast_strlen_zero(tmp)) {
01049 ast_cli(a->fd, "You didn't supply a username\n");
01050 } else if (!ast_strlen_zero(tmp) && !(pw = getpwnam(tmp))) {
01051 ast_cli(a->fd, "Unknown user '%s'\n", tmp);
01052 return CLI_FAILURE;
01053 } else if (pw) {
01054 uid = pw->pw_uid;
01055 }
01056
01057 if (a->argc == 4) {
01058 while ((ce = cli_next(ce))) {
01059
01060 if (ce->_full_cmd[0] == '_') {
01061 continue;
01062 }
01063 if (cli_has_permissions(uid, gid, ce->_full_cmd)) {
01064 ast_cli(a->fd, "%30.30s %s\n", ce->_full_cmd, S_OR(ce->summary, "<no description available>"));
01065 found++;
01066 }
01067 }
01068 if (!found) {
01069 ast_cli(a->fd, "You are not allowed to run any command on Asterisk\n");
01070 }
01071 } else {
01072 ast_join(command, sizeof(command), a->argv + 4);
01073 ast_cli(a->fd, "%s '%s%s%s' is %s to run command: '%s'\n", uid >= 0 ? "User" : "Group", tmp,
01074 group && uid >= 0 ? "@" : "",
01075 group ? &group[1] : "",
01076 cli_has_permissions(uid, gid, command) ? "allowed" : "not allowed", command);
01077 }
01078
01079 return CLI_SUCCESS;
01080 }
01081
01082 static char *__ast_cli_generator(const char *text, const char *word, int state, int lock);
01083
01084 static char *handle_commandmatchesarray(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01085 {
01086 char *buf, *obuf;
01087 int buflen = 2048;
01088 int len = 0;
01089 char **matches;
01090 int x, matchlen;
01091
01092 switch (cmd) {
01093 case CLI_INIT:
01094 e->command = "_command matchesarray";
01095 e->usage =
01096 "Usage: _command matchesarray \"<line>\" text \n"
01097 " This function is used internally to help with command completion and should.\n"
01098 " never be called by the user directly.\n";
01099 return NULL;
01100 case CLI_GENERATE:
01101 return NULL;
01102 }
01103
01104 if (a->argc != 4)
01105 return CLI_SHOWUSAGE;
01106 if (!(buf = ast_malloc(buflen)))
01107 return CLI_FAILURE;
01108 buf[len] = '\0';
01109 matches = ast_cli_completion_matches(a->argv[2], a->argv[3]);
01110 if (matches) {
01111 for (x=0; matches[x]; x++) {
01112 matchlen = strlen(matches[x]) + 1;
01113 if (len + matchlen >= buflen) {
01114 buflen += matchlen * 3;
01115 obuf = buf;
01116 if (!(buf = ast_realloc(obuf, buflen)))
01117
01118 ast_free(obuf);
01119 }
01120 if (buf)
01121 len += sprintf( buf + len, "%s ", matches[x]);
01122 ast_free(matches[x]);
01123 matches[x] = NULL;
01124 }
01125 ast_free(matches);
01126 }
01127
01128 if (buf) {
01129 ast_cli(a->fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
01130 ast_free(buf);
01131 } else
01132 ast_cli(a->fd, "NULL\n");
01133
01134 return CLI_SUCCESS;
01135 }
01136
01137
01138
01139 static char *handle_commandnummatches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01140 {
01141 int matches = 0;
01142
01143 switch (cmd) {
01144 case CLI_INIT:
01145 e->command = "_command nummatches";
01146 e->usage =
01147 "Usage: _command nummatches \"<line>\" text \n"
01148 " This function is used internally to help with command completion and should.\n"
01149 " never be called by the user directly.\n";
01150 return NULL;
01151 case CLI_GENERATE:
01152 return NULL;
01153 }
01154
01155 if (a->argc != 4)
01156 return CLI_SHOWUSAGE;
01157
01158 matches = ast_cli_generatornummatches(a->argv[2], a->argv[3]);
01159
01160 ast_cli(a->fd, "%d", matches);
01161
01162 return CLI_SUCCESS;
01163 }
01164
01165 static char *handle_commandcomplete(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01166 {
01167 char *buf;
01168 switch (cmd) {
01169 case CLI_INIT:
01170 e->command = "_command complete";
01171 e->usage =
01172 "Usage: _command complete \"<line>\" text state\n"
01173 " This function is used internally to help with command completion and should.\n"
01174 " never be called by the user directly.\n";
01175 return NULL;
01176 case CLI_GENERATE:
01177 return NULL;
01178 }
01179 if (a->argc != 5)
01180 return CLI_SHOWUSAGE;
01181 buf = __ast_cli_generator(a->argv[2], a->argv[3], atoi(a->argv[4]), 0);
01182 if (buf) {
01183 ast_cli(a->fd, "%s", buf);
01184 ast_free(buf);
01185 } else
01186 ast_cli(a->fd, "NULL\n");
01187 return CLI_SUCCESS;
01188 }
01189
01190 static char *handle_core_set_debug_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01191 {
01192 struct ast_channel *c = NULL;
01193 int is_all, is_off = 0;
01194
01195 switch (cmd) {
01196 case CLI_INIT:
01197 e->command = "core set debug channel";
01198 e->usage =
01199 "Usage: core set debug channel <all|channel> [off]\n"
01200 " Enables/disables debugging on all or on a specific channel.\n";
01201 return NULL;
01202
01203 case CLI_GENERATE:
01204
01205 if (a->pos != e->args)
01206 return NULL;
01207 return a->n == 0 ? ast_strdup("all") : ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
01208 }
01209
01210 if (a->argc == e->args + 2) {
01211 if (!strcasecmp(a->argv[e->args + 1], "off"))
01212 is_off = 1;
01213 else
01214 return CLI_SHOWUSAGE;
01215 } else if (a->argc != e->args + 1)
01216 return CLI_SHOWUSAGE;
01217
01218 is_all = !strcasecmp("all", a->argv[e->args]);
01219 if (is_all) {
01220 if (is_off) {
01221 global_fin &= ~DEBUGCHAN_FLAG;
01222 global_fout &= ~DEBUGCHAN_FLAG;
01223 } else {
01224 global_fin |= DEBUGCHAN_FLAG;
01225 global_fout |= DEBUGCHAN_FLAG;
01226 }
01227 c = ast_channel_walk_locked(NULL);
01228 } else {
01229 c = ast_get_channel_by_name_locked(a->argv[e->args]);
01230 if (c == NULL)
01231 ast_cli(a->fd, "No such channel %s\n", a->argv[e->args]);
01232 }
01233 while (c) {
01234 if (!(c->fin & DEBUGCHAN_FLAG) || !(c->fout & DEBUGCHAN_FLAG)) {
01235 if (is_off) {
01236 c->fin &= ~DEBUGCHAN_FLAG;
01237 c->fout &= ~DEBUGCHAN_FLAG;
01238 } else {
01239 c->fin |= DEBUGCHAN_FLAG;
01240 c->fout |= DEBUGCHAN_FLAG;
01241 }
01242 ast_cli(a->fd, "Debugging %s on channel %s\n", is_off ? "disabled" : "enabled", c->name);
01243 }
01244 ast_channel_unlock(c);
01245 if (!is_all)
01246 break;
01247 c = ast_channel_walk_locked(c);
01248 }
01249 ast_cli(a->fd, "Debugging on new channels is %s\n", is_off ? "disabled" : "enabled");
01250 return CLI_SUCCESS;
01251 }
01252
01253 static char *handle_nodebugchan_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01254 {
01255 char *res;
01256 if (cmd == CLI_HANDLER) {
01257 if (a->argc != e->args + 1)
01258 return CLI_SHOWUSAGE;
01259
01260
01261
01262 a->argv[e->args+1] = "off";
01263 a->argc++;
01264 }
01265 res = handle_core_set_debug_channel(e, cmd, a);
01266 if (cmd == CLI_INIT)
01267 e->command = "no debug channel";
01268 return res;
01269 }
01270
01271 static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01272 {
01273 struct ast_channel *c=NULL;
01274 struct timeval now;
01275 struct ast_str *out = ast_str_thread_get(&ast_str_thread_global_buf, 16);
01276 char cdrtime[256];
01277 char nf[256], wf[256], rf[256];
01278 long elapsed_seconds=0;
01279 int hour=0, min=0, sec=0;
01280 #ifdef CHANNEL_TRACE
01281 int trace_enabled;
01282 #endif
01283
01284 switch (cmd) {
01285 case CLI_INIT:
01286 e->command = "core show channel";
01287 e->usage =
01288 "Usage: core show channel <channel>\n"
01289 " Shows lots of information about the specified channel.\n";
01290 return NULL;
01291 case CLI_GENERATE:
01292 return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
01293 }
01294
01295 if (a->argc != 4)
01296 return CLI_SHOWUSAGE;
01297 now = ast_tvnow();
01298 c = ast_get_channel_by_name_locked(a->argv[3]);
01299 if (!c) {
01300 ast_cli(a->fd, "%s is not a known channel\n", a->argv[3]);
01301 return CLI_SUCCESS;
01302 }
01303 if (c->cdr) {
01304 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
01305 hour = elapsed_seconds / 3600;
01306 min = (elapsed_seconds % 3600) / 60;
01307 sec = elapsed_seconds % 60;
01308 snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
01309 } else
01310 strcpy(cdrtime, "N/A");
01311 ast_cli(a->fd,
01312 " -- General --\n"
01313 " Name: %s\n"
01314 " Type: %s\n"
01315 " UniqueID: %s\n"
01316 " Caller ID: %s\n"
01317 " Caller ID Name: %s\n"
01318 " DNID Digits: %s\n"
01319 " Language: %s\n"
01320 " State: %s (%d)\n"
01321 " Rings: %d\n"
01322 " NativeFormats: %s\n"
01323 " WriteFormat: %s\n"
01324 " ReadFormat: %s\n"
01325 " WriteTranscode: %s\n"
01326 " ReadTranscode: %s\n"
01327 "1st File Descriptor: %d\n"
01328 " Frames in: %d%s\n"
01329 " Frames out: %d%s\n"
01330 " Time to Hangup: %ld\n"
01331 " Elapsed Time: %s\n"
01332 " Direct Bridge: %s\n"
01333 "Indirect Bridge: %s\n"
01334 " -- PBX --\n"
01335 " Context: %s\n"
01336 " Extension: %s\n"
01337 " Priority: %d\n"
01338 " Call Group: %llu\n"
01339 " Pickup Group: %llu\n"
01340 " Application: %s\n"
01341 " Data: %s\n"
01342 " Blocking in: %s\n",
01343 c->name, c->tech->type, c->uniqueid,
01344 S_OR(c->cid.cid_num, "(N/A)"),
01345 S_OR(c->cid.cid_name, "(N/A)"),
01346 S_OR(c->cid.cid_dnid, "(N/A)"),
01347 c->language,
01348 ast_state2str(c->_state), c->_state, c->rings,
01349 ast_getformatname_multiple(nf, sizeof(nf), c->nativeformats),
01350 ast_getformatname_multiple(wf, sizeof(wf), c->writeformat),
01351 ast_getformatname_multiple(rf, sizeof(rf), c->readformat),
01352 c->writetrans ? "Yes" : "No",
01353 c->readtrans ? "Yes" : "No",
01354 c->fds[0],
01355 c->fin & ~DEBUGCHAN_FLAG, (c->fin & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
01356 c->fout & ~DEBUGCHAN_FLAG, (c->fout & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
01357 (long)c->whentohangup.tv_sec,
01358 cdrtime, c->_bridge ? c->_bridge->name : "<none>", ast_bridged_channel(c) ? ast_bridged_channel(c)->name : "<none>",
01359 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
01360 ( c-> data ? S_OR(c->data, "(Empty)") : "(None)"),
01361 (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
01362
01363 if (pbx_builtin_serialize_variables(c, &out))
01364 ast_cli(a->fd," Variables:\n%s\n", ast_str_buffer(out));
01365 if (c->cdr && ast_cdr_serialize_variables(c->cdr, &out, '=', '\n', 1))
01366 ast_cli(a->fd," CDR Variables:\n%s\n", ast_str_buffer(out));
01367 #ifdef CHANNEL_TRACE
01368 trace_enabled = ast_channel_trace_is_enabled(c);
01369 ast_cli(a->fd, " Context Trace: %s\n", trace_enabled ? "Enabled" : "Disabled");
01370 if (trace_enabled && ast_channel_trace_serialize(c, &out))
01371 ast_cli(a->fd, " Trace:\n%s\n", ast_str_buffer(out));
01372 #endif
01373 ast_channel_unlock(c);
01374 return CLI_SUCCESS;
01375 }
01376
01377
01378
01379
01380
01381 char *ast_cli_complete(const char *word, char *const choices[], int state)
01382 {
01383 int i, which = 0, len;
01384 len = ast_strlen_zero(word) ? 0 : strlen(word);
01385
01386 for (i = 0; choices[i]; i++) {
01387 if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state)
01388 return ast_strdup(choices[i]);
01389 }
01390 return NULL;
01391 }
01392
01393 char *ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos)
01394 {
01395 struct ast_channel *c = NULL;
01396 int which = 0;
01397 int wordlen;
01398 char notfound = '\0';
01399 char *ret = ¬found;
01400
01401 if (pos != rpos)
01402 return NULL;
01403
01404 wordlen = strlen(word);
01405
01406 while (ret == ¬found && (c = ast_channel_walk_locked(c))) {
01407 if (!strncasecmp(word, c->name, wordlen) && ++which > state)
01408 ret = ast_strdup(c->name);
01409 ast_channel_unlock(c);
01410 }
01411 return ret == ¬found ? NULL : ret;
01412 }
01413
01414 static char *group_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01415 {
01416 #define FORMAT_STRING "%-25s %-20s %-20s\n"
01417
01418 struct ast_group_info *gi = NULL;
01419 int numchans = 0;
01420 regex_t regexbuf;
01421 int havepattern = 0;
01422
01423 switch (cmd) {
01424 case CLI_INIT:
01425 e->command = "group show channels";
01426 e->usage =
01427 "Usage: group show channels [pattern]\n"
01428 " Lists all currently active channels with channel group(s) specified.\n"
01429 " Optional regular expression pattern is matched to group names for each\n"
01430 " channel.\n";
01431 return NULL;
01432 case CLI_GENERATE:
01433 return NULL;
01434 }
01435
01436 if (a->argc < 3 || a->argc > 4)
01437 return CLI_SHOWUSAGE;
01438
01439 if (a->argc == 4) {
01440 if (regcomp(®exbuf, a->argv[3], REG_EXTENDED | REG_NOSUB))
01441 return CLI_SHOWUSAGE;
01442 havepattern = 1;
01443 }
01444
01445 ast_cli(a->fd, FORMAT_STRING, "Channel", "Group", "Category");
01446
01447 ast_app_group_list_rdlock();
01448
01449 gi = ast_app_group_list_head();
01450 while (gi) {
01451 if (!havepattern || !regexec(®exbuf, gi->group, 0, NULL, 0)) {
01452 ast_cli(a->fd, FORMAT_STRING, gi->chan->name, gi->group, (ast_strlen_zero(gi->category) ? "(default)" : gi->category));
01453 numchans++;
01454 }
01455 gi = AST_LIST_NEXT(gi, group_list);
01456 }
01457
01458 ast_app_group_list_unlock();
01459
01460 if (havepattern)
01461 regfree(®exbuf);
01462
01463 ast_cli(a->fd, "%d active channel%s\n", numchans, ESS(numchans));
01464 return CLI_SUCCESS;
01465 #undef FORMAT_STRING
01466 }
01467
01468 static char *handle_cli_wait_fullybooted(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01469 {
01470 switch (cmd) {
01471 case CLI_INIT:
01472 e->command = "core waitfullybooted";
01473 e->usage =
01474 "Usage: core waitfullybooted\n"
01475 " Wait until Asterisk has fully booted.\n";
01476 return NULL;
01477 case CLI_GENERATE:
01478 return NULL;
01479 }
01480
01481 while (!ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
01482 usleep(100);
01483 }
01484
01485 ast_cli(a->fd, "Asterisk has fully booted.\n");
01486
01487 return CLI_SUCCESS;
01488 }
01489
01490 static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01491
01492 static struct ast_cli_entry cli_cli[] = {
01493
01494 AST_CLI_DEFINE(handle_commandcomplete, "Command complete"),
01495 AST_CLI_DEFINE(handle_commandnummatches, "Returns number of command matches"),
01496 AST_CLI_DEFINE(handle_commandmatchesarray, "Returns command matches array"),
01497
01498 AST_CLI_DEFINE(handle_nodebugchan_deprecated, "Disable debugging on channel(s)"),
01499
01500 AST_CLI_DEFINE(handle_chanlist, "Display information on channels"),
01501
01502 AST_CLI_DEFINE(handle_showcalls, "Display information on calls"),
01503
01504 AST_CLI_DEFINE(handle_showchan, "Display information on a specific channel"),
01505
01506 AST_CLI_DEFINE(handle_core_set_debug_channel, "Enable/disable debugging on a channel"),
01507
01508 AST_CLI_DEFINE(handle_verbose, "Set level of debug/verbose chattiness"),
01509
01510 AST_CLI_DEFINE(group_show_channels, "Display active channels with group(s)"),
01511
01512 AST_CLI_DEFINE(handle_help, "Display help list, or specific help on a command"),
01513
01514 AST_CLI_DEFINE(handle_logger_mute, "Toggle logging output to a console"),
01515
01516 AST_CLI_DEFINE(handle_modlist, "List modules and info"),
01517
01518 AST_CLI_DEFINE(handle_load, "Load a module by name"),
01519
01520 AST_CLI_DEFINE(handle_reload, "Reload configuration"),
01521
01522 AST_CLI_DEFINE(handle_unload, "Unload a module by name"),
01523
01524 AST_CLI_DEFINE(handle_showuptime, "Show uptime information"),
01525
01526 AST_CLI_DEFINE(handle_softhangup, "Request a hangup on a given channel"),
01527
01528 AST_CLI_DEFINE(handle_cli_reload_permissions, "Reload CLI permissions config"),
01529
01530 AST_CLI_DEFINE(handle_cli_show_permissions, "Show CLI permissions"),
01531
01532 AST_CLI_DEFINE(handle_cli_check_permissions, "Try a permissions config for a user"),
01533
01534 AST_CLI_DEFINE(handle_cli_wait_fullybooted, "Wait for Asterisk to be fully booted"),
01535 };
01536
01537
01538
01539
01540 static const char cli_rsvd[] = "[]{}|*%";
01541
01542
01543
01544
01545
01546 static int set_full_cmd(struct ast_cli_entry *e)
01547 {
01548 int i;
01549 char buf[80];
01550
01551 ast_join(buf, sizeof(buf), e->cmda);
01552 e->_full_cmd = ast_strdup(buf);
01553 if (!e->_full_cmd) {
01554 ast_log(LOG_WARNING, "-- cannot allocate <%s>\n", buf);
01555 return -1;
01556 }
01557 e->cmdlen = strcspn(e->_full_cmd, cli_rsvd);
01558 for (i = 0; e->cmda[i]; i++)
01559 ;
01560 e->args = i;
01561 return 0;
01562 }
01563
01564
01565 static void destroy_user_perms(void)
01566 {
01567 struct cli_perm *perm;
01568 struct usergroup_cli_perm *user_perm;
01569
01570 AST_RWLIST_WRLOCK(&cli_perms);
01571 while ((user_perm = AST_LIST_REMOVE_HEAD(&cli_perms, list))) {
01572 while ((perm = AST_LIST_REMOVE_HEAD(user_perm->perms, list))) {
01573 ast_free(perm->command);
01574 ast_free(perm);
01575 }
01576 ast_free(user_perm);
01577 }
01578 AST_RWLIST_UNLOCK(&cli_perms);
01579 }
01580
01581 int ast_cli_perms_init(int reload)
01582 {
01583 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
01584 struct ast_config *cfg;
01585 char *cat = NULL;
01586 struct ast_variable *v;
01587 struct usergroup_cli_perm *user_group, *cp_entry;
01588 struct cli_perm *perm = NULL;
01589 struct passwd *pw;
01590 struct group *gr;
01591
01592 if (ast_mutex_trylock(&permsconfiglock)) {
01593 ast_log(LOG_NOTICE, "You must wait until last 'cli reload permissions' command finish\n");
01594 return 1;
01595 }
01596
01597 cfg = ast_config_load2(perms_config, "" , config_flags);
01598 if (!cfg) {
01599 ast_mutex_unlock(&permsconfiglock);
01600 return 1;
01601 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
01602 ast_mutex_unlock(&permsconfiglock);
01603 return 0;
01604 }
01605
01606
01607 destroy_user_perms();
01608
01609 while ((cat = ast_category_browse(cfg, cat))) {
01610 if (!strcasecmp(cat, "general")) {
01611
01612 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
01613 if (!strcasecmp(v->name, "default_perm")) {
01614 cli_default_perm = (!strcasecmp(v->value, "permit")) ? 1: 0;
01615 }
01616 }
01617 continue;
01618 }
01619
01620
01621 gr = NULL, pw = NULL;
01622 if (cat[0] == '@') {
01623
01624 gr = getgrnam(&cat[1]);
01625 if (!gr) {
01626 ast_log (LOG_WARNING, "Unknown group '%s'\n", &cat[1]);
01627 continue;
01628 }
01629 } else {
01630
01631 pw = getpwnam(cat);
01632 if (!pw) {
01633 ast_log (LOG_WARNING, "Unknown user '%s'\n", cat);
01634 continue;
01635 }
01636 }
01637 user_group = NULL;
01638
01639 AST_RWLIST_WRLOCK(&cli_perms);
01640 AST_LIST_TRAVERSE(&cli_perms, cp_entry, list) {
01641 if ((pw && cp_entry->uid == pw->pw_uid) || (gr && cp_entry->gid == gr->gr_gid)) {
01642
01643
01644 user_group = cp_entry;
01645 break;
01646 }
01647 }
01648 AST_RWLIST_UNLOCK(&cli_perms);
01649
01650 if (!user_group) {
01651
01652 user_group = ast_calloc(1, sizeof(*user_group));
01653 if (!user_group) {
01654 continue;
01655 }
01656 user_group->uid = (pw ? pw->pw_uid : -1);
01657 user_group->gid = (gr ? gr->gr_gid : -1);
01658 user_group->perms = ast_calloc(1, sizeof(*user_group->perms));
01659 if (!user_group->perms) {
01660 ast_free(user_group);
01661 continue;
01662 }
01663 }
01664 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
01665 if (ast_strlen_zero(v->value)) {
01666
01667 ast_log(LOG_WARNING, "Empty permit/deny option in user '%s'\n", cat);
01668 continue;
01669 }
01670 if (!strcasecmp(v->name, "permit")) {
01671 perm = ast_calloc(1, sizeof(*perm));
01672 if (perm) {
01673 perm->permit = 1;
01674 perm->command = ast_strdup(v->value);
01675 }
01676 } else if (!strcasecmp(v->name, "deny")) {
01677 perm = ast_calloc(1, sizeof(*perm));
01678 if (perm) {
01679 perm->permit = 0;
01680 perm->command = ast_strdup(v->value);
01681 }
01682 } else {
01683
01684 ast_log(LOG_WARNING, "Unknown '%s' option\n", v->name);
01685 continue;
01686 }
01687 if (perm) {
01688
01689 AST_LIST_INSERT_TAIL(user_group->perms, perm, list);
01690 perm = NULL;
01691 }
01692 }
01693 AST_RWLIST_WRLOCK(&cli_perms);
01694 AST_RWLIST_INSERT_TAIL(&cli_perms, user_group, list);
01695 AST_RWLIST_UNLOCK(&cli_perms);
01696 }
01697
01698 ast_config_destroy(cfg);
01699 ast_mutex_unlock(&permsconfiglock);
01700 return 0;
01701 }
01702
01703
01704 void ast_builtins_init(void)
01705 {
01706 ast_cli_register_multiple(cli_cli, ARRAY_LEN(cli_cli));
01707 }
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720 static int word_match(const char *cmd, const char *cli_word)
01721 {
01722 int l;
01723 char *pos;
01724
01725 if (ast_strlen_zero(cmd) || ast_strlen_zero(cli_word))
01726 return -1;
01727 if (!strchr(cli_rsvd, cli_word[0]))
01728 return (strcasecmp(cmd, cli_word) == 0) ? 1 : -1;
01729
01730 l = strlen(cmd);
01731
01732 if (l > 0 && cli_word[0] == '%') {
01733 return 1;
01734 }
01735 pos = strcasestr(cli_word, cmd);
01736 if (pos == NULL)
01737 return cli_word[0] == '[' ? 0 : -1;
01738 if (pos == cli_word)
01739 return -1;
01740 if (strchr(cli_rsvd, pos[-1]) && strchr(cli_rsvd, pos[l]))
01741 return 1;
01742 return -1;
01743 }
01744
01745
01746
01747
01748
01749 static char *is_prefix(const char *word, const char *token,
01750 int pos, int *actual)
01751 {
01752 int lw;
01753 char *s, *t1;
01754
01755 *actual = 0;
01756 if (ast_strlen_zero(token))
01757 return NULL;
01758 if (ast_strlen_zero(word))
01759 word = "";
01760 lw = strlen(word);
01761 if (strcspn(word, cli_rsvd) != lw)
01762 return NULL;
01763 if (strchr(cli_rsvd, token[0]) == NULL) {
01764 if (strncasecmp(token, word, lw))
01765 return NULL;
01766 *actual = 1;
01767 return (pos != 0) ? NULL : ast_strdup(token);
01768 }
01769
01770
01771
01772
01773 t1 = ast_strdupa(token + 1);
01774 while (pos >= 0 && (s = strsep(&t1, cli_rsvd)) && *s) {
01775 if (*s == '%')
01776 continue;
01777 if (strncasecmp(s, word, lw))
01778 continue;
01779 (*actual)++;
01780 if (pos-- == 0)
01781 return ast_strdup(s);
01782 }
01783 return NULL;
01784 }
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799 static struct ast_cli_entry *find_cli(char *const cmds[], int match_type)
01800 {
01801 int matchlen = -1;
01802 struct ast_cli_entry *cand = NULL, *e=NULL;
01803
01804 while ( (e = cli_next(e)) ) {
01805
01806 char * const *src = cmds;
01807 char * const *dst = e->cmda;
01808 int n = 0;
01809 for (;; dst++, src += n) {
01810 n = word_match(*src, *dst);
01811 if (n < 0)
01812 break;
01813 }
01814 if (ast_strlen_zero(*dst) || ((*dst)[0] == '[' && ast_strlen_zero(dst[1]))) {
01815
01816 if (ast_strlen_zero(*src))
01817 break;
01818
01819 if (match_type != 0)
01820 continue;
01821
01822 } else {
01823 if (ast_strlen_zero(*src))
01824 continue;
01825
01826
01827
01828
01829 if (match_type != -1 || !ast_strlen_zero(src[1]) ||
01830 !ast_strlen_zero(dst[1]))
01831 continue;
01832
01833 }
01834 if (src - cmds > matchlen) {
01835 matchlen = src - cmds;
01836 cand = e;
01837 }
01838 }
01839
01840 return e ? e : cand;
01841 }
01842
01843 static char *find_best(char *argv[])
01844 {
01845 static char cmdline[80];
01846 int x;
01847
01848 char *myargv[AST_MAX_CMD_LEN];
01849 for (x=0;x<AST_MAX_CMD_LEN;x++)
01850 myargv[x]=NULL;
01851 AST_RWLIST_RDLOCK(&helpers);
01852 for (x=0;argv[x];x++) {
01853 myargv[x] = argv[x];
01854 if (!find_cli(myargv, -1))
01855 break;
01856 }
01857 AST_RWLIST_UNLOCK(&helpers);
01858 ast_join(cmdline, sizeof(cmdline), myargv);
01859 return cmdline;
01860 }
01861
01862 static int __ast_cli_unregister(struct ast_cli_entry *e, struct ast_cli_entry *ed)
01863 {
01864 if (e->inuse) {
01865 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
01866 } else {
01867 AST_RWLIST_WRLOCK(&helpers);
01868 AST_RWLIST_REMOVE(&helpers, e, list);
01869 AST_RWLIST_UNLOCK(&helpers);
01870 ast_free(e->_full_cmd);
01871 e->_full_cmd = NULL;
01872 if (e->handler) {
01873
01874 char *cmda = (char *) e->cmda;
01875 memset(cmda, '\0', sizeof(e->cmda));
01876 ast_free(e->command);
01877 e->command = NULL;
01878 e->usage = NULL;
01879 }
01880 }
01881 return 0;
01882 }
01883
01884 static int __ast_cli_register(struct ast_cli_entry *e, struct ast_cli_entry *ed)
01885 {
01886 struct ast_cli_entry *cur;
01887 int i, lf, ret = -1;
01888
01889 struct ast_cli_args a;
01890 char **dst = (char **)e->cmda;
01891 char *s;
01892
01893 memset(&a, '\0', sizeof(a));
01894 e->handler(e, CLI_INIT, &a);
01895
01896 s = ast_skip_blanks(e->command);
01897 s = e->command = ast_strdup(s);
01898 for (i=0; !ast_strlen_zero(s) && i < AST_MAX_CMD_LEN-1; i++) {
01899 *dst++ = s;
01900 s = ast_skip_nonblanks(s);
01901 if (*s == '\0')
01902 break;
01903 *s++ = '\0';
01904 s = ast_skip_blanks(s);
01905 }
01906 *dst++ = NULL;
01907
01908 AST_RWLIST_WRLOCK(&helpers);
01909
01910 if (find_cli(e->cmda, 1)) {
01911 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", S_OR(e->_full_cmd, e->command));
01912 goto done;
01913 }
01914 if (set_full_cmd(e))
01915 goto done;
01916
01917 lf = e->cmdlen;
01918 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&helpers, cur, list) {
01919 int len = cur->cmdlen;
01920 if (lf < len)
01921 len = lf;
01922 if (strncasecmp(e->_full_cmd, cur->_full_cmd, len) < 0) {
01923 AST_RWLIST_INSERT_BEFORE_CURRENT(e, list);
01924 break;
01925 }
01926 }
01927 AST_RWLIST_TRAVERSE_SAFE_END;
01928
01929 if (!cur)
01930 AST_RWLIST_INSERT_TAIL(&helpers, e, list);
01931 ret = 0;
01932
01933 done:
01934 AST_RWLIST_UNLOCK(&helpers);
01935
01936 return ret;
01937 }
01938
01939
01940 int ast_cli_unregister(struct ast_cli_entry *e)
01941 {
01942 return __ast_cli_unregister(e, NULL);
01943 }
01944
01945
01946 int ast_cli_register(struct ast_cli_entry *e)
01947 {
01948 return __ast_cli_register(e, NULL);
01949 }
01950
01951
01952
01953
01954 int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
01955 {
01956 int i, res = 0;
01957
01958 for (i = 0; i < len; i++)
01959 res |= ast_cli_register(e + i);
01960
01961 return res;
01962 }
01963
01964 int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
01965 {
01966 int i, res = 0;
01967
01968 for (i = 0; i < len; i++)
01969 res |= ast_cli_unregister(e + i);
01970
01971 return res;
01972 }
01973
01974
01975
01976
01977
01978 static char *help1(int fd, char *match[], int locked)
01979 {
01980 char matchstr[80] = "";
01981 struct ast_cli_entry *e = NULL;
01982 int len = 0;
01983 int found = 0;
01984
01985 if (match) {
01986 ast_join(matchstr, sizeof(matchstr), match);
01987 len = strlen(matchstr);
01988 }
01989 if (!locked)
01990 AST_RWLIST_RDLOCK(&helpers);
01991 while ( (e = cli_next(e)) ) {
01992
01993 if (e->_full_cmd[0] == '_')
01994 continue;
01995 if (match && strncasecmp(matchstr, e->_full_cmd, len))
01996 continue;
01997 ast_cli(fd, "%30.30s %s\n", e->_full_cmd, S_OR(e->summary, "<no description available>"));
01998 found++;
01999 }
02000 if (!locked)
02001 AST_RWLIST_UNLOCK(&helpers);
02002 if (!found && matchstr[0])
02003 ast_cli(fd, "No such command '%s'.\n", matchstr);
02004 return CLI_SUCCESS;
02005 }
02006
02007 static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02008 {
02009 char fullcmd[80];
02010 struct ast_cli_entry *my_e;
02011 char *res = CLI_SUCCESS;
02012
02013 if (cmd == CLI_INIT) {
02014 e->command = "core show help";
02015 e->usage =
02016 "Usage: core show help [topic]\n"
02017 " When called with a topic as an argument, displays usage\n"
02018 " information on the given command. If called without a\n"
02019 " topic, it provides a list of commands.\n";
02020 return NULL;
02021
02022 } else if (cmd == CLI_GENERATE) {
02023
02024 int l = strlen(a->line);
02025
02026 if (l > 15) {
02027 l = 15;
02028 }
02029
02030 return __ast_cli_generator(a->line + l, a->word, a->n, 0);
02031 }
02032 if (a->argc == e->args) {
02033 return help1(a->fd, NULL, 0);
02034 }
02035
02036 AST_RWLIST_RDLOCK(&helpers);
02037 my_e = find_cli(a->argv + 3, 1);
02038 if (!my_e) {
02039 res = help1(a->fd, a->argv + 3, 1 );
02040 AST_RWLIST_UNLOCK(&helpers);
02041 return res;
02042 }
02043 if (my_e->usage)
02044 ast_cli(a->fd, "%s", my_e->usage);
02045 else {
02046 ast_join(fullcmd, sizeof(fullcmd), a->argv + 3);
02047 ast_cli(a->fd, "No help text available for '%s'.\n", fullcmd);
02048 }
02049 AST_RWLIST_UNLOCK(&helpers);
02050 return res;
02051 }
02052
02053 static char *parse_args(const char *s, int *argc, char *argv[], int max, int *trailingwhitespace)
02054 {
02055 char *duplicate, *cur;
02056 int x = 0;
02057 int quoted = 0;
02058 int escaped = 0;
02059 int whitespace = 1;
02060 int dummy = 0;
02061
02062 if (trailingwhitespace == NULL)
02063 trailingwhitespace = &dummy;
02064 *trailingwhitespace = 0;
02065 if (s == NULL)
02066 return NULL;
02067
02068 if (!(duplicate = ast_strdup(s)))
02069 return NULL;
02070
02071 cur = duplicate;
02072
02073 for (; *s ; s++) {
02074 if (x >= max - 1) {
02075 ast_log(LOG_WARNING, "Too many arguments, truncating at %s\n", s);
02076 break;
02077 }
02078 if (*s == '"' && !escaped) {
02079 quoted = !quoted;
02080 if (quoted && whitespace) {
02081
02082 argv[x++] = cur;
02083 whitespace = 0;
02084 }
02085 } else if ((*s == ' ' || *s == '\t') && !(quoted || escaped)) {
02086
02087
02088
02089
02090 if (!whitespace) {
02091 *cur++ = '\0';
02092 whitespace = 1;
02093 }
02094 } else if (*s == '\\' && !escaped) {
02095 escaped = 1;
02096 } else {
02097 if (whitespace) {
02098
02099 argv[x++] = cur;
02100 whitespace = 0;
02101 }
02102 *cur++ = *s;
02103 escaped = 0;
02104 }
02105 }
02106
02107 *cur++ = '\0';
02108
02109
02110
02111
02112 argv[x] = NULL;
02113 *argc = x;
02114 *trailingwhitespace = whitespace;
02115 return duplicate;
02116 }
02117
02118
02119 int ast_cli_generatornummatches(const char *text, const char *word)
02120 {
02121 int matches = 0, i = 0;
02122 char *buf = NULL, *oldbuf = NULL;
02123
02124 while ((buf = ast_cli_generator(text, word, i++))) {
02125 if (!oldbuf || strcmp(buf,oldbuf))
02126 matches++;
02127 if (oldbuf)
02128 ast_free(oldbuf);
02129 oldbuf = buf;
02130 }
02131 if (oldbuf)
02132 ast_free(oldbuf);
02133 return matches;
02134 }
02135
02136 char **ast_cli_completion_matches(const char *text, const char *word)
02137 {
02138 char **match_list = NULL, *retstr, *prevstr;
02139 size_t match_list_len, max_equal, which, i;
02140 int matches = 0;
02141
02142
02143 match_list_len = 1;
02144 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
02145 if (matches + 1 >= match_list_len) {
02146 match_list_len <<= 1;
02147 if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
02148 return NULL;
02149 }
02150 match_list[++matches] = retstr;
02151 }
02152
02153 if (!match_list)
02154 return match_list;
02155
02156
02157
02158
02159 prevstr = match_list[1];
02160 max_equal = strlen(prevstr);
02161 for (which = 2; which <= matches; which++) {
02162 for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
02163 continue;
02164 max_equal = i;
02165 }
02166
02167 if (!(retstr = ast_malloc(max_equal + 1)))
02168 return NULL;
02169
02170 ast_copy_string(retstr, match_list[1], max_equal + 1);
02171 match_list[0] = retstr;
02172
02173
02174 if (matches + 1 >= match_list_len) {
02175 if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list))))
02176 return NULL;
02177 }
02178 match_list[matches + 1] = NULL;
02179
02180 return match_list;
02181 }
02182
02183
02184 static int more_words (char * const *dst)
02185 {
02186 int i;
02187 for (i = 0; dst[i]; i++) {
02188 if (dst[i][0] != '[')
02189 return -1;
02190 }
02191 return 0;
02192 }
02193
02194
02195
02196
02197 static char *__ast_cli_generator(const char *text, const char *word, int state, int lock)
02198 {
02199 char *argv[AST_MAX_ARGS];
02200 struct ast_cli_entry *e = NULL;
02201 int x = 0, argindex, matchlen;
02202 int matchnum=0;
02203 char *ret = NULL;
02204 char matchstr[80] = "";
02205 int tws = 0;
02206
02207 char *duplicate = parse_args(text, &x, argv, ARRAY_LEN(argv), &tws);
02208
02209 if (!duplicate)
02210 return NULL;
02211
02212
02213 argindex = (!ast_strlen_zero(word) && x>0) ? x-1 : x;
02214
02215
02216 ast_join(matchstr, sizeof(matchstr)-1, argv);
02217 matchlen = strlen(matchstr);
02218 if (tws) {
02219 strcat(matchstr, " ");
02220 if (matchlen)
02221 matchlen++;
02222 }
02223 if (lock)
02224 AST_RWLIST_RDLOCK(&helpers);
02225 while ( (e = cli_next(e)) ) {
02226
02227 int src = 0, dst = 0, n = 0;
02228
02229 if (e->command[0] == '_')
02230 continue;
02231
02232
02233
02234
02235
02236 for (;src < argindex; dst++, src += n) {
02237 n = word_match(argv[src], e->cmda[dst]);
02238 if (n < 0)
02239 break;
02240 }
02241
02242 if (src != argindex && more_words(e->cmda + dst))
02243 continue;
02244 ret = is_prefix(argv[src], e->cmda[dst], state - matchnum, &n);
02245 matchnum += n;
02246 if (ret) {
02247
02248
02249
02250
02251 if (matchnum > state)
02252 break;
02253 ast_free(ret);
02254 ret = NULL;
02255 } else if (ast_strlen_zero(e->cmda[dst])) {
02256
02257
02258
02259
02260
02261 if (e->handler) {
02262 struct ast_cli_args a = {
02263 .line = matchstr, .word = word,
02264 .pos = argindex,
02265 .n = state - matchnum,
02266 .argv = argv,
02267 .argc = x};
02268 ret = e->handler(e, CLI_GENERATE, &a);
02269 }
02270 if (ret)
02271 break;
02272 }
02273 }
02274 if (lock)
02275 AST_RWLIST_UNLOCK(&helpers);
02276 ast_free(duplicate);
02277 return ret;
02278 }
02279
02280 char *ast_cli_generator(const char *text, const char *word, int state)
02281 {
02282 return __ast_cli_generator(text, word, state, 1);
02283 }
02284
02285 int ast_cli_command_full(int uid, int gid, int fd, const char *s)
02286 {
02287 char *args[AST_MAX_ARGS + 1];
02288 struct ast_cli_entry *e;
02289 int x;
02290 char *duplicate = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
02291 char tmp[AST_MAX_ARGS + 1];
02292 char *retval = NULL;
02293 struct ast_cli_args a = {
02294 .fd = fd, .argc = x, .argv = args+1 };
02295
02296 if (duplicate == NULL)
02297 return -1;
02298
02299 if (x < 1)
02300 goto done;
02301
02302 AST_RWLIST_RDLOCK(&helpers);
02303 e = find_cli(args + 1, 0);
02304 if (e)
02305 ast_atomic_fetchadd_int(&e->inuse, 1);
02306 AST_RWLIST_UNLOCK(&helpers);
02307 if (e == NULL) {
02308 ast_cli(fd, "No such command '%s' (type 'core show help %s' for other possible commands)\n", s, find_best(args + 1));
02309 goto done;
02310 }
02311
02312 ast_join(tmp, sizeof(tmp), args + 1);
02313
02314 if (!cli_has_permissions(uid, gid, tmp)) {
02315 ast_cli(fd, "You don't have permissions to run '%s' command\n", tmp);
02316 ast_free(duplicate);
02317 return 0;
02318 }
02319
02320
02321
02322
02323
02324 args[0] = (char *)e;
02325
02326 retval = e->handler(e, CLI_HANDLER, &a);
02327
02328 if (retval == CLI_SHOWUSAGE) {
02329 ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
02330 } else {
02331 if (retval == CLI_FAILURE)
02332 ast_cli(fd, "Command '%s' failed.\n", s);
02333 }
02334 ast_atomic_fetchadd_int(&e->inuse, -1);
02335 done:
02336 ast_free(duplicate);
02337 return 0;
02338 }
02339
02340 int ast_cli_command_multiple_full(int uid, int gid, int fd, size_t size, const char *s)
02341 {
02342 char cmd[512];
02343 int x, y = 0, count = 0;
02344
02345 for (x = 0; x < size; x++) {
02346 cmd[y] = s[x];
02347 y++;
02348 if (s[x] == '\0') {
02349 ast_cli_command_full(uid, gid, fd, cmd);
02350 y = 0;
02351 count++;
02352 }
02353 }
02354 return count;
02355 }