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: 373368 $")
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 iax2_format 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/%s\n", template);
00267 }
00268 if (sscanf(tmp, "v%30x", version) != 1) {
00269 if (strcmp(tmp, "u")) {
00270 ret = iax_provision_build(&ied, version, template, force);
00271 if (ret)
00272 ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
00273 } else
00274 ret = -1;
00275 } else
00276 ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
00277 ast_mutex_unlock(&provlock);
00278 return ret;
00279 }
00280
00281 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
00282 {
00283 struct ast_variable *v;
00284 int foundportno = 0;
00285 int foundserverportno = 0;
00286 int x;
00287 struct in_addr ia;
00288 struct hostent *hp;
00289 struct ast_hostent h;
00290 struct iax_template *src, tmp;
00291 const char *t;
00292 if (def) {
00293 t = ast_variable_retrieve(cfg, s ,"template");
00294 src = NULL;
00295 if (t && strlen(t)) {
00296 src = iax_template_find(t, 0);
00297 if (!src)
00298 ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
00299 else
00300 def = t;
00301 }
00302 if (!src) {
00303 src = iax_template_find(def, 0);
00304 if (!src)
00305 ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
00306 }
00307 if (!src)
00308 return -1;
00309 ast_mutex_lock(&provlock);
00310
00311 iax_template_copy(&tmp, cur);
00312
00313 iax_template_copy(cur, src);
00314
00315 memcpy(cur->name, tmp.name, sizeof(cur->name));
00316 cur->dead = tmp.dead;
00317 ast_mutex_unlock(&provlock);
00318 }
00319 if (def)
00320 ast_copy_string(cur->src, def, sizeof(cur->src));
00321 else
00322 cur->src[0] = '\0';
00323 v = ast_variable_browse(cfg, s);
00324 while(v) {
00325 if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
00326 if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
00327 if (!strcasecmp(v->name, "port")) {
00328 cur->port = x;
00329 foundportno = 1;
00330 } else {
00331 cur->serverport = x;
00332 foundserverportno = 1;
00333 }
00334 } else
00335 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00336 } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
00337 hp = ast_gethostbyname(v->value, &h);
00338 if (hp) {
00339 memcpy(&ia, hp->h_addr, sizeof(ia));
00340 if (!strcasecmp(v->name, "server"))
00341 cur->server = ntohl(ia.s_addr);
00342 else
00343 cur->altserver = ntohl(ia.s_addr);
00344 } else
00345 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00346 } else if (!strcasecmp(v->name, "codec")) {
00347 struct ast_format tmpfmt;
00348 if ((ast_getformatbyname(v->value, &tmpfmt)) > 0) {
00349 cur->format = ast_format_to_old_bitfield(&tmpfmt);
00350 } else
00351 ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
00352 } else if (!strcasecmp(v->name, "tos")) {
00353 if (ast_str2tos(v->value, &cur->tos))
00354 ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
00355 } else if (!strcasecmp(v->name, "user")) {
00356 ast_copy_string(cur->user, v->value, sizeof(cur->user));
00357 if (strcmp(cur->user, v->value))
00358 ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
00359 } else if (!strcasecmp(v->name, "pass")) {
00360 ast_copy_string(cur->pass, v->value, sizeof(cur->pass));
00361 if (strcmp(cur->pass, v->value))
00362 ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
00363 } else if (!strcasecmp(v->name, "language")) {
00364 ast_copy_string(cur->lang, v->value, sizeof(cur->lang));
00365 if (strcmp(cur->lang, v->value))
00366 ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
00367 } else if (!strcasecmp(v->name, "flags")) {
00368 cur->flags = iax_str2flags(v->value);
00369 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
00370 cur->flags |= iax_str2flags(v->value);
00371 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
00372 cur->flags &= ~iax_str2flags(v->value);
00373 } else if (strcasecmp(v->name, "template")) {
00374 ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
00375 }
00376 v = v->next;
00377 }
00378 if (!foundportno)
00379 cur->port = IAX_DEFAULT_PORTNO;
00380 if (!foundserverportno)
00381 cur->serverport = IAX_DEFAULT_PORTNO;
00382 return 0;
00383 }
00384
00385 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
00386 {
00387
00388 struct iax_template *cur;
00389 int mallocd = 0;
00390
00391 cur = iax_template_find(s, 1 );
00392 if (!cur) {
00393 mallocd = 1;
00394 cur = ast_calloc(1, sizeof(*cur));
00395 if (!cur) {
00396 ast_log(LOG_WARNING, "Out of memory!\n");
00397 return -1;
00398 }
00399
00400 ast_copy_string(cur->name, s, sizeof(cur->name));
00401 cur->dead = 1;
00402 }
00403 if (!iax_template_parse(cur, cfg, s, def))
00404 cur->dead = 0;
00405
00406
00407 if (mallocd) {
00408 ast_mutex_lock(&provlock);
00409 AST_LIST_INSERT_HEAD(&templates, cur, list);
00410 ast_mutex_unlock(&provlock);
00411 }
00412 return 0;
00413 }
00414
00415 static const char *ifthere(const char *s)
00416 {
00417 if (strlen(s))
00418 return s;
00419 else
00420 return "<unspecified>";
00421 }
00422
00423 static const char *iax_server(unsigned int addr)
00424 {
00425 struct in_addr ia;
00426
00427 if (!addr)
00428 return "<unspecified>";
00429
00430 ia.s_addr = htonl(addr);
00431
00432 return ast_inet_ntoa(ia);
00433 }
00434
00435
00436 static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00437 {
00438 struct iax_template *cur;
00439 char server[INET_ADDRSTRLEN];
00440 char alternate[INET_ADDRSTRLEN];
00441 char flags[80];
00442 int found = 0;
00443
00444 switch (cmd) {
00445 case CLI_INIT:
00446 e->command = "iax2 show provisioning";
00447 e->usage =
00448 "Usage: iax2 show provisioning [template]\n"
00449 " Lists all known IAX provisioning templates or a\n"
00450 " specific one if specified.\n";
00451 return NULL;
00452 case CLI_GENERATE:
00453 return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
00454 }
00455
00456 if ((a->argc != 3) && (a->argc != 4))
00457 return CLI_SHOWUSAGE;
00458
00459 ast_mutex_lock(&provlock);
00460 AST_LIST_TRAVERSE(&templates, cur, list) {
00461 if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
00462 if (found)
00463 ast_cli(a->fd, "\n");
00464 ast_copy_string(server, iax_server(cur->server), sizeof(server));
00465 ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
00466 ast_cli(a->fd, "== %s ==\n", cur->name);
00467 ast_cli(a->fd, "Base Templ: %s\n", strlen(cur->src) ? cur->src : "<none>");
00468 ast_cli(a->fd, "Username: %s\n", ifthere(cur->user));
00469 ast_cli(a->fd, "Secret: %s\n", ifthere(cur->pass));
00470 ast_cli(a->fd, "Language: %s\n", ifthere(cur->lang));
00471 ast_cli(a->fd, "Bind Port: %d\n", cur->port);
00472 ast_cli(a->fd, "Server: %s\n", server);
00473 ast_cli(a->fd, "Server Port: %d\n", cur->serverport);
00474 ast_cli(a->fd, "Alternate: %s\n", alternate);
00475 ast_cli(a->fd, "Flags: %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
00476 ast_cli(a->fd, "Format: %s\n", iax2_getformatname(cur->format));
00477 ast_cli(a->fd, "TOS: 0x%x\n", cur->tos);
00478 found++;
00479 }
00480 }
00481 ast_mutex_unlock(&provlock);
00482 if (!found) {
00483 if (a->argc == 3)
00484 ast_cli(a->fd, "No provisioning templates found\n");
00485 else
00486 ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
00487 }
00488 return CLI_SUCCESS;
00489 }
00490
00491 static struct ast_cli_entry cli_iax2_provision[] = {
00492 AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
00493 };
00494
00495 static int iax_provision_init(void)
00496 {
00497 ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00498 provinit = 1;
00499 return 0;
00500 }
00501
00502 static void iax_provision_free_templates(int dead)
00503 {
00504 struct iax_template *cur;
00505
00506
00507 ast_mutex_lock(&provlock);
00508 AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
00509 if ((dead && cur->dead) || !dead) {
00510 AST_LIST_REMOVE_CURRENT(list);
00511 ast_free(cur);
00512 }
00513 }
00514 AST_LIST_TRAVERSE_SAFE_END;
00515 ast_mutex_unlock(&provlock);
00516 }
00517
00518 int iax_provision_unload(void)
00519 {
00520 provinit = 0;
00521 ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00522 iax_provision_free_templates(0 );
00523
00524 return 0;
00525 }
00526
00527 int iax_provision_reload(int reload)
00528 {
00529 struct ast_config *cfg;
00530 struct iax_template *cur;
00531 char *cat;
00532 int found = 0;
00533 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00534 if (!provinit)
00535 iax_provision_init();
00536
00537 cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
00538 if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
00539
00540 AST_LIST_TRAVERSE(&templates, cur, list) {
00541 cur->dead = 1;
00542 }
00543
00544
00545 cat = ast_category_browse(cfg, NULL);
00546 while(cat) {
00547 if (strcasecmp(cat, "general")) {
00548 iax_process_template(cfg, cat, found ? "default" : NULL);
00549 found++;
00550 ast_verb(3, "Loaded provisioning template '%s'\n", cat);
00551 }
00552 cat = ast_category_browse(cfg, cat);
00553 }
00554 ast_config_destroy(cfg);
00555 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00556 return 0;
00557 else
00558 ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
00559
00560 iax_provision_free_templates(1 );
00561
00562
00563 ast_db_deltree("iax/provisioning/cache", NULL);
00564 return 0;
00565 }