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