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: 160698 $")
00029
00030 #include <netdb.h>
00031 #include <netinet/in.h>
00032 #include <netinet/in_systm.h>
00033 #include <netinet/ip.h>
00034 #include <sys/socket.h>
00035
00036 #include "asterisk/config.h"
00037 #include "asterisk/cli.h"
00038 #include "asterisk/lock.h"
00039 #include "asterisk/frame.h"
00040 #include "asterisk/md5.h"
00041 #include "asterisk/astdb.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/acl.h"
00044 #include "iax2.h"
00045 #include "iax2-provision.h"
00046 #include "iax2-parser.h"
00047
00048 static int provinit = 0;
00049
00050 struct iax_template {
00051 int dead;
00052 char name[80];
00053 char src[80];
00054 char user[20];
00055 char pass[20];
00056 char lang[10];
00057 unsigned short port;
00058 unsigned int server;
00059 unsigned short serverport;
00060 unsigned int altserver;
00061 unsigned int flags;
00062 unsigned int format;
00063 unsigned int tos;
00064 AST_LIST_ENTRY(iax_template) list;
00065 };
00066
00067 static AST_LIST_HEAD_NOLOCK_STATIC(templates, iax_template);
00068
00069 AST_MUTEX_DEFINE_STATIC(provlock);
00070
00071 static struct iax_flag {
00072 char *name;
00073 int value;
00074 } iax_flags[] = {
00075 { "register", PROV_FLAG_REGISTER },
00076 { "secure", PROV_FLAG_SECURE },
00077 { "heartbeat", PROV_FLAG_HEARTBEAT },
00078 { "debug", PROV_FLAG_DEBUG },
00079 { "disablecid", PROV_FLAG_DIS_CALLERID },
00080 { "disablecw", PROV_FLAG_DIS_CALLWAIT },
00081 { "disablecidcw", PROV_FLAG_DIS_CIDCW },
00082 { "disable3way", PROV_FLAG_DIS_THREEWAY },
00083 };
00084
00085 char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
00086 {
00087 int x;
00088
00089 if (!buf || buflen < 1)
00090 return NULL;
00091
00092 buf[0] = '\0';
00093
00094 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
00095 if (flags & iax_flags[x].value){
00096 strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
00097 strncat(buf, ",", buflen - strlen(buf) - 1);
00098 }
00099 }
00100
00101 if (!ast_strlen_zero(buf))
00102 buf[strlen(buf) - 1] = '\0';
00103 else
00104 strncpy(buf, "none", buflen - 1);
00105
00106 return buf;
00107 }
00108
00109 static unsigned int iax_str2flags(const char *buf)
00110 {
00111 int x;
00112 int len;
00113 int found;
00114 unsigned int flags = 0;
00115 char *e;
00116 while(buf && *buf) {
00117 e = strchr(buf, ',');
00118 if (e)
00119 len = e - buf;
00120 else
00121 len = 0;
00122 found = 0;
00123 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
00124 if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
00125 (!len && !strcasecmp(iax_flags[x].name, buf))) {
00126 flags |= iax_flags[x].value;
00127 break;
00128 }
00129 }
00130 if (e) {
00131 buf = e + 1;
00132 while(*buf && (*buf < 33))
00133 buf++;
00134 } else
00135 break;
00136 }
00137 return flags;
00138 }
00139
00140 static void iax_template_copy(struct iax_template *dst, struct iax_template *src)
00141 {
00142 if (!dst || !src) {
00143 return;
00144 }
00145
00146 dst->dead = src->dead;
00147 ast_copy_string(dst->name, src->name, sizeof(dst->name));
00148 ast_copy_string(dst->src, src->src, sizeof(dst->src));
00149 ast_copy_string(dst->user, src->user, sizeof(dst->user));
00150 ast_copy_string(dst->pass, src->pass, sizeof(dst->pass));
00151 ast_copy_string(dst->lang, src->lang, sizeof(dst->lang));
00152 dst->port = src->port;
00153 dst->server = src->server;
00154 dst->altserver = src->altserver;
00155 dst->flags = src->flags;
00156 dst->format = src->format;
00157 dst->tos = src->tos;
00158 }
00159
00160 static struct iax_template *iax_template_find(const char *s, int allowdead)
00161 {
00162 struct iax_template *cur;
00163
00164 AST_LIST_TRAVERSE(&templates, cur, list) {
00165 if (!strcasecmp(s, cur->name)) {
00166 if (!allowdead && cur->dead) {
00167 cur = NULL;
00168 }
00169 break;
00170 }
00171 }
00172
00173 return cur;
00174 }
00175
00176 char *iax_prov_complete_template(const char *line, const char *word, int pos, int state)
00177 {
00178 struct iax_template *c;
00179 int which=0;
00180 char *ret = NULL;
00181 int wordlen = strlen(word);
00182
00183 if (pos == 3) {
00184 ast_mutex_lock(&provlock);
00185 AST_LIST_TRAVERSE(&templates, c, list) {
00186 if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
00187 ret = ast_strdup(c->name);
00188 break;
00189 }
00190 }
00191 ast_mutex_unlock(&provlock);
00192 }
00193 return ret;
00194 }
00195
00196 static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
00197 {
00198 struct MD5Context md5;
00199 unsigned int tmp[4];
00200 MD5Init(&md5);
00201 MD5Update(&md5, provdata->buf, provdata->pos);
00202 MD5Final((unsigned char *)tmp, &md5);
00203 return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
00204 }
00205
00206 int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
00207 {
00208 struct iax_template *cur;
00209 unsigned int sig;
00210 char tmp[40];
00211 memset(provdata, 0, sizeof(*provdata));
00212 ast_mutex_lock(&provlock);
00213 cur = iax_template_find(template, 1);
00214
00215 if (!cur)
00216 cur = iax_template_find("*", 1);
00217 if (cur) {
00218
00219 if (force || strlen(cur->user))
00220 iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
00221 if (force || strlen(cur->pass))
00222 iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
00223 if (force || strlen(cur->lang))
00224 iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
00225 if (force || cur->port)
00226 iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
00227 if (force || cur->server)
00228 iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
00229 if (force || cur->serverport)
00230 iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
00231 if (force || cur->altserver)
00232 iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
00233 if (force || cur->flags)
00234 iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
00235 if (force || cur->format)
00236 iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
00237 if (force || cur->tos)
00238 iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
00239
00240
00241 sig = prov_ver_calc(provdata);
00242 if (signature)
00243 *signature = sig;
00244
00245 iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
00246
00247 snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
00248 ast_db_put("iax/provisioning/cache", template, tmp);
00249 } else
00250 ast_db_put("iax/provisioning/cache", template, "u");
00251 ast_mutex_unlock(&provlock);
00252 return cur ? 0 : -1;
00253 }
00254
00255 int iax_provision_version(unsigned int *version, const char *template, int force)
00256 {
00257 char tmp[80] = "";
00258 struct iax_ie_data ied;
00259 int ret=0;
00260 memset(&ied, 0, sizeof(ied));
00261
00262 ast_mutex_lock(&provlock);
00263 ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp));
00264 if (sscanf(tmp, "v%x", version) != 1) {
00265 if (strcmp(tmp, "u")) {
00266 ret = iax_provision_build(&ied, version, template, force);
00267 if (ret)
00268 ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
00269 } else
00270 ret = -1;
00271 } else
00272 ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
00273 ast_mutex_unlock(&provlock);
00274 return ret;
00275 }
00276
00277 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
00278 {
00279 struct ast_variable *v;
00280 int foundportno = 0;
00281 int foundserverportno = 0;
00282 int x;
00283 struct in_addr ia;
00284 struct hostent *hp;
00285 struct ast_hostent h;
00286 struct iax_template *src, tmp;
00287 const char *t;
00288 if (def) {
00289 t = ast_variable_retrieve(cfg, s ,"template");
00290 src = NULL;
00291 if (t && strlen(t)) {
00292 src = iax_template_find(t, 0);
00293 if (!src)
00294 ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
00295 else
00296 def = t;
00297 }
00298 if (!src) {
00299 src = iax_template_find(def, 0);
00300 if (!src)
00301 ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
00302 }
00303 if (!src)
00304 return -1;
00305 ast_mutex_lock(&provlock);
00306
00307 iax_template_copy(&tmp, cur);
00308
00309 iax_template_copy(cur, src);
00310
00311 memcpy(cur->name, tmp.name, sizeof(cur->name));
00312 cur->dead = tmp.dead;
00313 ast_mutex_unlock(&provlock);
00314 }
00315 if (def)
00316 strncpy(cur->src, def, sizeof(cur->src) - 1);
00317 else
00318 cur->src[0] = '\0';
00319 v = ast_variable_browse(cfg, s);
00320 while(v) {
00321 if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
00322 if ((sscanf(v->value, "%d", &x) == 1) && (x > 0) && (x < 65535)) {
00323 if (!strcasecmp(v->name, "port")) {
00324 cur->port = x;
00325 foundportno = 1;
00326 } else {
00327 cur->serverport = x;
00328 foundserverportno = 1;
00329 }
00330 } else
00331 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00332 } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
00333 hp = ast_gethostbyname(v->value, &h);
00334 if (hp) {
00335 memcpy(&ia, hp->h_addr, sizeof(ia));
00336 if (!strcasecmp(v->name, "server"))
00337 cur->server = ntohl(ia.s_addr);
00338 else
00339 cur->altserver = ntohl(ia.s_addr);
00340 } else
00341 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00342 } else if (!strcasecmp(v->name, "codec")) {
00343 if ((x = ast_getformatbyname(v->value)) > 0) {
00344 cur->format = x;
00345 } else
00346 ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
00347 } else if (!strcasecmp(v->name, "tos")) {
00348 if (ast_str2tos(v->value, &cur->tos))
00349 ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
00350 } else if (!strcasecmp(v->name, "user")) {
00351 strncpy(cur->user, v->value, sizeof(cur->user) - 1);
00352 if (strcmp(cur->user, v->value))
00353 ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
00354 } else if (!strcasecmp(v->name, "pass")) {
00355 strncpy(cur->pass, v->value, sizeof(cur->pass) - 1);
00356 if (strcmp(cur->pass, v->value))
00357 ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
00358 } else if (!strcasecmp(v->name, "language")) {
00359 strncpy(cur->lang, v->value, sizeof(cur->lang) - 1);
00360 if (strcmp(cur->lang, v->value))
00361 ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
00362 } else if (!strcasecmp(v->name, "flags")) {
00363 cur->flags = iax_str2flags(v->value);
00364 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
00365 cur->flags |= iax_str2flags(v->value);
00366 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
00367 cur->flags &= ~iax_str2flags(v->value);
00368 } else if (strcasecmp(v->name, "template")) {
00369 ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
00370 }
00371 v = v->next;
00372 }
00373 if (!foundportno)
00374 cur->port = IAX_DEFAULT_PORTNO;
00375 if (!foundserverportno)
00376 cur->serverport = IAX_DEFAULT_PORTNO;
00377 return 0;
00378 }
00379
00380 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
00381 {
00382
00383 struct iax_template *cur;
00384 int mallocd = 0;
00385
00386 cur = iax_template_find(s, 1 );
00387 if (!cur) {
00388 mallocd = 1;
00389 cur = ast_calloc(1, sizeof(*cur));
00390 if (!cur) {
00391 ast_log(LOG_WARNING, "Out of memory!\n");
00392 return -1;
00393 }
00394
00395 strncpy(cur->name, s, sizeof(cur->name) - 1);
00396 cur->dead = 1;
00397 }
00398 if (!iax_template_parse(cur, cfg, s, def))
00399 cur->dead = 0;
00400
00401
00402 if (mallocd) {
00403 ast_mutex_lock(&provlock);
00404 AST_LIST_INSERT_HEAD(&templates, cur, list);
00405 ast_mutex_unlock(&provlock);
00406 }
00407 return 0;
00408 }
00409
00410 static const char *ifthere(const char *s)
00411 {
00412 if (strlen(s))
00413 return s;
00414 else
00415 return "<unspecified>";
00416 }
00417
00418 static const char *iax_server(unsigned int addr)
00419 {
00420 struct in_addr ia;
00421
00422 if (!addr)
00423 return "<unspecified>";
00424
00425 ia.s_addr = htonl(addr);
00426
00427 return ast_inet_ntoa(ia);
00428 }
00429
00430
00431 static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00432 {
00433 struct iax_template *cur;
00434 char server[INET_ADDRSTRLEN];
00435 char alternate[INET_ADDRSTRLEN];
00436 char flags[80];
00437 int found = 0;
00438
00439 switch (cmd) {
00440 case CLI_INIT:
00441 e->command = "iax2 show provisioning";
00442 e->usage =
00443 "Usage: iax2 show provisioning [template]\n"
00444 " Lists all known IAX provisioning templates or a\n"
00445 " specific one if specified.\n";
00446 return NULL;
00447 case CLI_GENERATE:
00448 return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
00449 }
00450
00451 if ((a->argc != 3) && (a->argc != 4))
00452 return CLI_SHOWUSAGE;
00453
00454 ast_mutex_lock(&provlock);
00455 AST_LIST_TRAVERSE(&templates, cur, list) {
00456 if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
00457 if (found)
00458 ast_cli(a->fd, "\n");
00459 ast_copy_string(server, iax_server(cur->server), sizeof(server));
00460 ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
00461 ast_cli(a->fd, "== %s ==\n", cur->name);
00462 ast_cli(a->fd, "Base Templ: %s\n", strlen(cur->src) ? cur->src : "<none>");
00463 ast_cli(a->fd, "Username: %s\n", ifthere(cur->user));
00464 ast_cli(a->fd, "Secret: %s\n", ifthere(cur->pass));
00465 ast_cli(a->fd, "Language: %s\n", ifthere(cur->lang));
00466 ast_cli(a->fd, "Bind Port: %d\n", cur->port);
00467 ast_cli(a->fd, "Server: %s\n", server);
00468 ast_cli(a->fd, "Server Port: %d\n", cur->serverport);
00469 ast_cli(a->fd, "Alternate: %s\n", alternate);
00470 ast_cli(a->fd, "Flags: %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
00471 ast_cli(a->fd, "Format: %s\n", ast_getformatname(cur->format));
00472 ast_cli(a->fd, "TOS: 0x%x\n", cur->tos);
00473 found++;
00474 }
00475 }
00476 ast_mutex_unlock(&provlock);
00477 if (!found) {
00478 if (a->argc == 3)
00479 ast_cli(a->fd, "No provisioning templates found\n");
00480 else
00481 ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
00482 }
00483 return CLI_SUCCESS;
00484 }
00485
00486 static struct ast_cli_entry cli_iax2_provision[] = {
00487 AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
00488 };
00489
00490 static int iax_provision_init(void)
00491 {
00492 ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00493 provinit = 1;
00494 return 0;
00495 }
00496
00497 static void iax_provision_free_templates(int dead)
00498 {
00499 struct iax_template *cur;
00500
00501
00502 ast_mutex_lock(&provlock);
00503 AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
00504 if ((dead && cur->dead) || !dead) {
00505 AST_LIST_REMOVE_CURRENT(list);
00506 ast_free(cur);
00507 }
00508 }
00509 AST_LIST_TRAVERSE_SAFE_END;
00510 ast_mutex_unlock(&provlock);
00511 }
00512
00513 int iax_provision_unload(void)
00514 {
00515 provinit = 0;
00516 ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00517 iax_provision_free_templates(0 );
00518
00519 return 0;
00520 }
00521
00522 int iax_provision_reload(int reload)
00523 {
00524 struct ast_config *cfg;
00525 struct iax_template *cur;
00526 char *cat;
00527 int found = 0;
00528 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00529 if (!provinit)
00530 iax_provision_init();
00531
00532 cfg = ast_config_load("iaxprov.conf", config_flags);
00533 if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED) {
00534
00535 AST_LIST_TRAVERSE(&templates, cur, list) {
00536 cur->dead = 1;
00537 }
00538
00539
00540 cat = ast_category_browse(cfg, NULL);
00541 while(cat) {
00542 if (strcasecmp(cat, "general")) {
00543 iax_process_template(cfg, cat, found ? "default" : NULL);
00544 found++;
00545 ast_verb(3, "Loaded provisioning template '%s'\n", cat);
00546 }
00547 cat = ast_category_browse(cfg, cat);
00548 }
00549 ast_config_destroy(cfg);
00550 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00551 return 0;
00552 else
00553 ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
00554
00555 iax_provision_free_templates(1 );
00556
00557
00558 ast_db_deltree("iax/provisioning/cache", NULL);
00559 return 0;
00560 }