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