res/ari/cli.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Digium, Inc.
00005  *
00006  * David M. Lee, II <dlee@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Command line for ARI.
00022  * \author David M. Lee, II <dlee@digium.com>
00023  */
00024 
00025 #include "asterisk.h"
00026 
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 395603 $")
00028 
00029 #include "asterisk/astobj2.h"
00030 #include "asterisk/cli.h"
00031 #include "internal.h"
00032 
00033 static char *ari_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00034 {
00035    RAII_VAR(struct ast_ari_conf *, conf, NULL, ao2_cleanup);
00036 
00037    switch (cmd) {
00038    case CLI_INIT:
00039       e->command = "ari show status";
00040       e->usage =
00041          "Usage: ari show status\n"
00042          "       Shows all ARI settings\n";
00043       return NULL;
00044    case CLI_GENERATE:
00045       return NULL;
00046    default:
00047       break;
00048    }
00049 
00050    if (a->argc != 3) {
00051       return CLI_SHOWUSAGE;
00052    }
00053 
00054    conf = ast_ari_config_get();
00055 
00056    if (!conf) {
00057       ast_cli(a->fd, "Error getting ARI configuration\n");
00058       return CLI_FAILURE;
00059    }
00060 
00061    ast_cli(a->fd, "ARI Status:\n");
00062    ast_cli(a->fd, "Enabled: %s\n", AST_CLI_YESNO(conf->general->enabled));
00063    ast_cli(a->fd, "Output format: ");
00064    switch (conf->general->format) {
00065    case AST_JSON_COMPACT:
00066       ast_cli(a->fd, "compact");
00067       break;
00068    case AST_JSON_PRETTY:
00069       ast_cli(a->fd, "pretty");
00070       break;
00071    }
00072    ast_cli(a->fd, "\n");
00073    ast_cli(a->fd, "Auth realm: %s\n", conf->general->auth_realm);
00074    ast_cli(a->fd, "Allowed Origins: %s\n", conf->general->allowed_origins);
00075    ast_cli(a->fd, "User count: %d\n", ao2_container_count(conf->users));
00076    return CLI_SUCCESS;
00077 }
00078 
00079 static int show_users_cb(void *obj, void *arg, int flags)
00080 {
00081    struct ast_ari_conf_user *user = obj;
00082    struct ast_cli_args *a = arg;
00083 
00084    ast_cli(a->fd, "%-4s  %s\n",
00085       AST_CLI_YESNO(user->read_only),
00086       user->username);
00087    return 0;
00088 }
00089 
00090 static char *ari_show_users(struct ast_cli_entry *e, int cmd,
00091    struct ast_cli_args *a)
00092 {
00093    RAII_VAR(struct ast_ari_conf *, conf, NULL, ao2_cleanup);
00094 
00095    switch (cmd) {
00096    case CLI_INIT:
00097       e->command = "ari show users";
00098       e->usage =
00099          "Usage: ari show users\n"
00100          "       Shows all ARI users\n";
00101       return NULL;
00102    case CLI_GENERATE:
00103       return NULL;
00104    default:
00105       break;
00106    }
00107 
00108    if (a->argc != 3) {
00109       return CLI_SHOWUSAGE;
00110    }
00111 
00112    conf = ast_ari_config_get();
00113    if (!conf) {
00114       ast_cli(a->fd, "Error getting ARI configuration\n");
00115       return CLI_FAILURE;
00116    }
00117 
00118    ast_cli(a->fd, "r/o?  Username\n");
00119    ast_cli(a->fd, "----  --------\n");
00120 
00121    ao2_callback(conf->users, OBJ_NODATA, show_users_cb, a);
00122 
00123    return CLI_SUCCESS;
00124 }
00125 
00126 struct user_complete {
00127    /*! Nth user to search for */
00128    int state;
00129    /*! Which user currently on */
00130    int which;
00131 };
00132 
00133 static int complete_ari_user_search(void *obj, void *arg, void *data, int flags)
00134 {
00135    struct user_complete *search = data;
00136 
00137    if (++search->which > search->state) {
00138       return CMP_MATCH;
00139    }
00140    return 0;
00141 }
00142 
00143 static char *complete_ari_user(struct ast_cli_args *a)
00144 {
00145    RAII_VAR(struct ast_ari_conf *, conf, NULL, ao2_cleanup);
00146    RAII_VAR(struct ast_ari_conf_user *, user, NULL, ao2_cleanup);
00147 
00148    struct user_complete search = {
00149       .state = a->n,
00150    };
00151 
00152    conf = ast_ari_config_get();
00153    if (!conf) {
00154       ast_cli(a->fd, "Error getting ARI configuration\n");
00155       return CLI_FAILURE;
00156    }
00157 
00158    user = ao2_callback_data(conf->users,
00159       ast_strlen_zero(a->word) ? 0 : OBJ_PARTIAL_KEY,
00160       complete_ari_user_search, (char*)a->word, &search);
00161 
00162    return user ? ast_strdup(user->username) : NULL;
00163 }
00164 
00165 static char *complete_ari_show_user(struct ast_cli_args *a)
00166 {
00167    if (a->pos == 3) {
00168       return complete_ari_user(a);
00169    }
00170 
00171    return NULL;
00172 }
00173 
00174 static char *ari_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00175 {
00176    RAII_VAR(struct ast_ari_conf *, conf, NULL, ao2_cleanup);
00177    RAII_VAR(struct ast_ari_conf_user *, user, NULL, ao2_cleanup);
00178 
00179    switch (cmd) {
00180    case CLI_INIT:
00181       e->command = "ari show user";
00182       e->usage =
00183          "Usage: ari show user <username>\n"
00184          "       Shows a specific ARI user\n";
00185       return NULL;
00186    case CLI_GENERATE:
00187       return complete_ari_show_user(a);
00188    default:
00189       break;
00190    }
00191 
00192    if (a->argc != 4) {
00193       return CLI_SHOWUSAGE;
00194    }
00195 
00196    conf = ast_ari_config_get();
00197 
00198    if (!conf) {
00199       ast_cli(a->fd, "Error getting ARI configuration\n");
00200       return CLI_FAILURE;
00201    }
00202 
00203    user = ao2_find(conf->users, a->argv[3], OBJ_KEY);
00204    if (!user) {
00205       ast_cli(a->fd, "User '%s' not found\n", a->argv[3]);
00206       return CLI_SUCCESS;
00207    }
00208 
00209    ast_cli(a->fd, "Username: %s\n", user->username);
00210    ast_cli(a->fd, "Read only?: %s\n", AST_CLI_YESNO(user->read_only));
00211 
00212    return CLI_SUCCESS;
00213 }
00214 
00215 static char *ari_mkpasswd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00216 {
00217    RAII_VAR(char *, crypted, NULL, ast_free);
00218 
00219    switch (cmd) {
00220    case CLI_INIT:
00221       e->command = "ari mkpasswd";
00222       e->usage =
00223          "Usage: ari mkpasswd <password>\n"
00224          "       Encrypts a password for use in ari.conf\n"
00225          "       Be aware that the password will be shown in the\n"
00226          "       command line history. The mkpasswd shell command\n"
00227          "       may be preferable.\n"
00228          ;
00229       return NULL;
00230    case CLI_GENERATE:
00231       return NULL;
00232    default:
00233       break;
00234    }
00235 
00236    if (a->argc != 3) {
00237       return CLI_SHOWUSAGE;
00238    }
00239 
00240    crypted = ast_crypt_encrypt(a->argv[2]);
00241    if (!crypted) {
00242       ast_cli(a->fd, "Failed to encrypt password\n");
00243       return CLI_FAILURE;
00244    }
00245 
00246    ast_cli(a->fd,
00247       "; Copy the following two lines into ari.conf\n");
00248    ast_cli(a->fd, "password_format = crypt\n");
00249    ast_cli(a->fd, "password = %s\n", crypted);
00250 
00251    return CLI_SUCCESS;
00252 }
00253 
00254 static struct ast_cli_entry cli_ari[] = {
00255    AST_CLI_DEFINE(ari_show, "Show ARI settings"),
00256    AST_CLI_DEFINE(ari_show_users, "List ARI users"),
00257    AST_CLI_DEFINE(ari_show_user, "List single ARI user"),
00258    AST_CLI_DEFINE(ari_mkpasswd, "Encrypts a password"),
00259 };
00260 
00261 int ast_ari_cli_register(void) {
00262    return ast_cli_register_multiple(cli_ari, ARRAY_LEN(cli_ari));
00263 }
00264 
00265 void ast_ari_cli_unregister(void) {
00266    ast_cli_unregister_multiple(cli_ari, ARRAY_LEN(cli_ari));
00267 }

Generated on Thu Apr 16 06:27:30 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6