| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "asterisk.h" |
| 43 |
|
| 44 |
#include "asterisk/module.h" |
| 45 |
#include "asterisk/config.h" |
| 46 |
#include "asterisk/cli.h" |
| 47 |
#include "asterisk/astobj2.h" |
| 48 |
|
| 49 |
|
| 50 |
#define MAX_ALIAS_BUCKETS 53 |
| 51 |
|
| 52 |
|
| 53 |
static const char config_file[] = "cli_aliases.conf"; |
| 54 |
|
| 55 |
struct cli_alias { |
| 56 |
struct ast_cli_entry cli_entry; |
| 57 |
char *alias; |
| 58 |
char *real_cmd; |
| 59 |
}; |
| 60 |
|
| 61 |
static struct ao2_container *cli_aliases; |
| 62 |
|
| 63 |
|
| 64 |
static int alias_hash_cb(const void *obj, const int flags) |
| 65 |
{ |
| 66 |
const struct cli_alias *alias = obj; |
| 67 |
return ast_str_hash(alias->cli_entry.command); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
static int alias_cmp_cb(void *obj, void *arg, int flags) |
| 72 |
{ |
| 73 |
const struct cli_alias *alias0 = obj, *alias1 = arg; |
| 74 |
|
| 75 |
return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static int alias_unregister_cb(void *obj, void *arg, int flags) |
| 80 |
{ |
| 81 |
struct cli_alias *alias = obj; |
| 82 |
|
| 83 |
|
| 84 |
ast_cli_unregister(&alias->cli_entry); |
| 85 |
|
| 86 |
|
| 87 |
return !alias->cli_entry.command ? CMP_MATCH : 0; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
static int alias_name_cb(void *obj, void *arg, int flags) |
| 92 |
{ |
| 93 |
struct cli_alias *alias = obj; |
| 94 |
char *name = arg; |
| 95 |
|
| 96 |
return !strcmp(alias->alias, name) ? CMP_MATCH | CMP_STOP : 0; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 101 |
{ |
| 102 |
struct cli_alias *alias; |
| 103 |
struct cli_alias tmp = { |
| 104 |
.cli_entry.command = e->command, |
| 105 |
}; |
| 106 |
char *generator = NULL; |
| 107 |
const char *line; |
| 108 |
|
| 109 |
|
| 110 |
if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) { |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
switch (cmd) { |
| 115 |
case CLI_INIT: |
| 116 |
ao2_ref(alias, -1); |
| 117 |
return NULL; |
| 118 |
case CLI_GENERATE: |
| 119 |
line = a->line; |
| 120 |
line += (strlen(alias->alias)); |
| 121 |
if (strncasecmp(alias->alias, alias->real_cmd, strlen(alias->alias))) { |
| 122 |
struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1); |
| 123 |
ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line); |
| 124 |
generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n); |
| 125 |
} |
| 126 |
ao2_ref(alias, -1); |
| 127 |
return generator; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
if (a->argc != e->args) { |
| 132 |
struct ast_str *real_cmd = ast_str_alloca(2048); |
| 133 |
int i; |
| 134 |
|
| 135 |
ast_str_append(&real_cmd, 0, "%s", alias->real_cmd); |
| 136 |
|
| 137 |
|
| 138 |
for (i = e->args + 1; i <= a->argc; i++) { |
| 139 |
ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]); |
| 140 |
} |
| 141 |
|
| 142 |
ast_cli_command(a->fd, ast_str_buffer(real_cmd)); |
| 143 |
} else { |
| 144 |
ast_cli_command(a->fd, alias->real_cmd); |
| 145 |
} |
| 146 |
|
| 147 |
ao2_ref(alias, -1); |
| 148 |
|
| 149 |
return CLI_SUCCESS; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 154 |
{ |
| 155 |
#define FORMAT "%-50.50s %-50.50s\n" |
| 156 |
struct cli_alias *alias; |
| 157 |
struct ao2_iterator i; |
| 158 |
|
| 159 |
switch (cmd) { |
| 160 |
case CLI_INIT: |
| 161 |
e->command = "cli show aliases"; |
| 162 |
e->usage = |
| 163 |
"Usage: cli show aliases\n" |
| 164 |
" Displays a list of aliased CLI commands.\n"; |
| 165 |
return NULL; |
| 166 |
case CLI_GENERATE: |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
ast_cli(a->fd, FORMAT, "Alias Command", "Real Command"); |
| 171 |
|
| 172 |
i = ao2_iterator_init(cli_aliases, 0); |
| 173 |
for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) { |
| 174 |
ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd); |
| 175 |
} |
| 176 |
ao2_iterator_destroy(&i); |
| 177 |
|
| 178 |
return CLI_SUCCESS; |
| 179 |
#undef FORMAT |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static struct ast_cli_entry cli_alias[] = { |
| 184 |
AST_CLI_DEFINE(alias_show, "Show CLI command aliases"), |
| 185 |
}; |
| 186 |
|
| 187 |
|
| 188 |
static void load_config(int reload) |
| 189 |
{ |
| 190 |
struct ast_config *cfg = NULL; |
| 191 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 192 |
struct cli_alias *alias; |
| 193 |
struct ast_variable *v, *v1; |
| 194 |
|
| 195 |
if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) { |
| 196 |
ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file); |
| 197 |
return; |
| 198 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (reload) { |
| 204 |
ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, alias_unregister_cb, NULL); |
| 205 |
} |
| 206 |
|
| 207 |
for (v = ast_variable_browse(cfg, "general"); v; v = v->next) { |
| 208 |
if (strcmp(v->name, "template")) { |
| 209 |
ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general"); |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) { |
| 214 |
struct cli_alias *existing = ao2_callback(cli_aliases, 0, alias_name_cb, (char*)v1->name); |
| 215 |
|
| 216 |
if (existing) { |
| 217 |
ast_log(LOG_WARNING, "Alias '%s' could not be unregistered and has been retained\n", |
| 218 |
existing->alias); |
| 219 |
ao2_ref(existing, -1); |
| 220 |
continue; |
| 221 |
} |
| 222 |
|
| 223 |
if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), NULL))) { |
| 224 |
continue; |
| 225 |
} |
| 226 |
alias->alias = ((char *) alias) + sizeof(*alias); |
| 227 |
alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1; |
| 228 |
strcpy(alias->alias, v1->name); |
| 229 |
strcpy(alias->real_cmd, v1->value); |
| 230 |
alias->cli_entry.handler = cli_alias_passthrough; |
| 231 |
alias->cli_entry.command = alias->alias; |
| 232 |
alias->cli_entry.usage = "Aliased CLI Command\n"; |
| 233 |
|
| 234 |
if (ast_cli_register(&alias->cli_entry)) { |
| 235 |
ao2_ref(alias, -1); |
| 236 |
continue; |
| 237 |
} |
| 238 |
ao2_link(cli_aliases, alias); |
| 239 |
ast_verb(5, "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value); |
| 240 |
ao2_ref(alias, -1); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
ast_config_destroy(cfg); |
| 245 |
|
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static int reload_module(void) |
| 251 |
{ |
| 252 |
load_config(1); |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
static int unload_module(void) |
| 258 |
{ |
| 259 |
ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, alias_unregister_cb, NULL); |
| 260 |
|
| 261 |
if (ao2_container_count(cli_aliases)) { |
| 262 |
ast_log(LOG_ERROR, "Could not unregister all CLI aliases\n"); |
| 263 |
return -1; |
| 264 |
} |
| 265 |
|
| 266 |
ao2_ref(cli_aliases, -1); |
| 267 |
|
| 268 |
ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias)); |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
static int load_module(void) |
| 284 |
{ |
| 285 |
cli_aliases = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 286 |
MAX_ALIAS_BUCKETS, alias_hash_cb, NULL, alias_cmp_cb); |
| 287 |
if (!cli_aliases) { |
| 288 |
return AST_MODULE_LOAD_DECLINE; |
| 289 |
} |
| 290 |
|
| 291 |
load_config(0); |
| 292 |
|
| 293 |
ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias)); |
| 294 |
|
| 295 |
return AST_MODULE_LOAD_SUCCESS; |
| 296 |
} |
| 297 |
|
| 298 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "CLI Aliases", |
| 299 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 300 |
.load = load_module, |
| 301 |
.unload = unload_module, |
| 302 |
.reload = reload_module, |
| 303 |
); |