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