2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief IAX Provisioning Protocol
23 * \author Mark Spencer <markster@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <sys/socket.h>
40 #include "asterisk/config.h"
41 #include "asterisk/cli.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/md5.h"
45 #include "asterisk/astdb.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/acl.h"
49 #include "iax2-provision.h"
50 #include "iax2-parser.h"
52 static int provinit = 0;
63 unsigned short serverport;
64 unsigned int altserver;
68 AST_LIST_ENTRY(iax_template) list;
71 static AST_LIST_HEAD_NOLOCK_STATIC(templates, iax_template);
73 AST_MUTEX_DEFINE_STATIC(provlock);
75 static struct iax_flag {
79 { "register", PROV_FLAG_REGISTER },
80 { "secure", PROV_FLAG_SECURE },
81 { "heartbeat", PROV_FLAG_HEARTBEAT },
82 { "debug", PROV_FLAG_DEBUG },
83 { "disablecid", PROV_FLAG_DIS_CALLERID },
84 { "disablecw", PROV_FLAG_DIS_CALLWAIT },
85 { "disablecidcw", PROV_FLAG_DIS_CIDCW },
86 { "disable3way", PROV_FLAG_DIS_THREEWAY },
89 char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
93 if (!buf || buflen < 1)
98 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
99 if (flags & iax_flags[x].value){
100 strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
101 strncat(buf, ",", buflen - strlen(buf) - 1);
105 if (!ast_strlen_zero(buf))
106 buf[strlen(buf) - 1] = '\0';
108 strncpy(buf, "none", buflen - 1);
113 static unsigned int iax_str2flags(const char *buf)
117 unsigned int flags = 0;
120 e = strchr(buf, ',');
125 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
126 if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
127 (!len && !strcasecmp(iax_flags[x].name, buf))) {
128 flags |= iax_flags[x].value;
134 while(*buf && (*buf < 33))
142 static void iax_template_copy(struct iax_template *dst, struct iax_template *src)
148 dst->dead = src->dead;
149 ast_copy_string(dst->name, src->name, sizeof(dst->name));
150 ast_copy_string(dst->src, src->src, sizeof(dst->src));
151 ast_copy_string(dst->user, src->user, sizeof(dst->user));
152 ast_copy_string(dst->pass, src->pass, sizeof(dst->pass));
153 ast_copy_string(dst->lang, src->lang, sizeof(dst->lang));
154 dst->port = src->port;
155 dst->server = src->server;
156 dst->altserver = src->altserver;
157 dst->flags = src->flags;
158 dst->format = src->format;
162 static struct iax_template *iax_template_find(const char *s, int allowdead)
164 struct iax_template *cur;
166 AST_LIST_TRAVERSE(&templates, cur, list) {
167 if (!strcasecmp(s, cur->name)) {
168 if (!allowdead && cur->dead) {
178 char *iax_prov_complete_template(const char *line, const char *word, int pos, int state)
180 struct iax_template *c;
183 int wordlen = strlen(word);
186 ast_mutex_lock(&provlock);
187 AST_LIST_TRAVERSE(&templates, c, list) {
188 if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
189 ret = ast_strdup(c->name);
193 ast_mutex_unlock(&provlock);
198 static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
200 struct MD5Context md5;
203 MD5Update(&md5, provdata->buf, provdata->pos);
204 MD5Final((unsigned char *)tmp, &md5);
205 return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
208 int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
210 struct iax_template *cur;
213 memset(provdata, 0, sizeof(*provdata));
214 ast_mutex_lock(&provlock);
215 cur = iax_template_find(template, 1);
216 /* If no match, try searching for '*' */
218 cur = iax_template_find("*", 1);
220 /* found it -- add information elements as appropriate */
221 if (force || strlen(cur->user))
222 iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
223 if (force || strlen(cur->pass))
224 iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
225 if (force || strlen(cur->lang))
226 iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
227 if (force || cur->port)
228 iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
229 if (force || cur->server)
230 iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
231 if (force || cur->serverport)
232 iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
233 if (force || cur->altserver)
234 iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
235 if (force || cur->flags)
236 iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
237 if (force || cur->format)
238 iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
239 if (force || cur->tos)
240 iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
242 /* Calculate checksum of message so far */
243 sig = prov_ver_calc(provdata);
246 /* Store signature */
247 iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
248 /* Cache signature for later verification so we need not recalculate all this */
249 snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
250 ast_db_put("iax/provisioning/cache", template, tmp);
252 ast_db_put("iax/provisioning/cache", template, "u");
253 ast_mutex_unlock(&provlock);
257 int iax_provision_version(unsigned int *version, const char *template, int force)
260 struct iax_ie_data ied;
262 memset(&ied, 0, sizeof(ied));
264 ast_mutex_lock(&provlock);
265 if (!(ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp)))) {
266 ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache\n");
267 ast_mutex_unlock(&provlock);
270 if (sscanf(tmp, "v%30x", version) != 1) {
271 if (strcmp(tmp, "u")) {
272 ret = iax_provision_build(&ied, version, template, force);
274 ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
278 ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
279 ast_mutex_unlock(&provlock);
283 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
285 struct ast_variable *v;
287 int foundserverportno = 0;
291 struct ast_hostent h;
292 struct iax_template *src, tmp;
295 t = ast_variable_retrieve(cfg, s ,"template");
297 if (t && strlen(t)) {
298 src = iax_template_find(t, 0);
300 ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
305 src = iax_template_find(def, 0);
307 ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
311 ast_mutex_lock(&provlock);
312 /* Backup old data */
313 iax_template_copy(&tmp, cur);
314 /* Restore from src */
315 iax_template_copy(cur, src);
316 /* Restore important headers */
317 memcpy(cur->name, tmp.name, sizeof(cur->name));
318 cur->dead = tmp.dead;
319 ast_mutex_unlock(&provlock);
322 ast_copy_string(cur->src, def, sizeof(cur->src));
325 v = ast_variable_browse(cfg, s);
327 if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
328 if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
329 if (!strcasecmp(v->name, "port")) {
334 foundserverportno = 1;
337 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
338 } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
339 hp = ast_gethostbyname(v->value, &h);
341 memcpy(&ia, hp->h_addr, sizeof(ia));
342 if (!strcasecmp(v->name, "server"))
343 cur->server = ntohl(ia.s_addr);
345 cur->altserver = ntohl(ia.s_addr);
347 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
348 } else if (!strcasecmp(v->name, "codec")) {
349 struct ast_format tmpfmt;
350 if ((ast_getformatbyname(v->value, &tmpfmt)) > 0) {
351 cur->format = ast_format_to_old_bitfield(&tmpfmt);
353 ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
354 } else if (!strcasecmp(v->name, "tos")) {
355 if (ast_str2tos(v->value, &cur->tos))
356 ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
357 } else if (!strcasecmp(v->name, "user")) {
358 ast_copy_string(cur->user, v->value, sizeof(cur->user));
359 if (strcmp(cur->user, v->value))
360 ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
361 } else if (!strcasecmp(v->name, "pass")) {
362 ast_copy_string(cur->pass, v->value, sizeof(cur->pass));
363 if (strcmp(cur->pass, v->value))
364 ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
365 } else if (!strcasecmp(v->name, "language")) {
366 ast_copy_string(cur->lang, v->value, sizeof(cur->lang));
367 if (strcmp(cur->lang, v->value))
368 ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
369 } else if (!strcasecmp(v->name, "flags")) {
370 cur->flags = iax_str2flags(v->value);
371 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
372 cur->flags |= iax_str2flags(v->value);
373 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
374 cur->flags &= ~iax_str2flags(v->value);
375 } else if (strcasecmp(v->name, "template")) {
376 ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
381 cur->port = IAX_DEFAULT_PORTNO;
382 if (!foundserverportno)
383 cur->serverport = IAX_DEFAULT_PORTNO;
387 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
389 /* Find an already existing one if there */
390 struct iax_template *cur;
393 cur = iax_template_find(s, 1 /* allow dead */);
396 cur = ast_calloc(1, sizeof(*cur));
398 ast_log(LOG_WARNING, "Out of memory!\n");
401 /* Initialize entry */
402 ast_copy_string(cur->name, s, sizeof(cur->name));
405 if (!iax_template_parse(cur, cfg, s, def))
408 /* Link if we're mallocd */
410 ast_mutex_lock(&provlock);
411 AST_LIST_INSERT_HEAD(&templates, cur, list);
412 ast_mutex_unlock(&provlock);
417 static const char *ifthere(const char *s)
422 return "<unspecified>";
425 static const char *iax_server(unsigned int addr)
430 return "<unspecified>";
432 ia.s_addr = htonl(addr);
434 return ast_inet_ntoa(ia);
438 static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
440 struct iax_template *cur;
441 char server[INET_ADDRSTRLEN];
442 char alternate[INET_ADDRSTRLEN];
443 char flags[80]; /* Has to be big enough for 'flags' too */
448 e->command = "iax2 show provisioning";
450 "Usage: iax2 show provisioning [template]\n"
451 " Lists all known IAX provisioning templates or a\n"
452 " specific one if specified.\n";
455 return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
458 if ((a->argc != 3) && (a->argc != 4))
459 return CLI_SHOWUSAGE;
461 ast_mutex_lock(&provlock);
462 AST_LIST_TRAVERSE(&templates, cur, list) {
463 if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
465 ast_cli(a->fd, "\n");
466 ast_copy_string(server, iax_server(cur->server), sizeof(server));
467 ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
468 ast_cli(a->fd, "== %s ==\n", cur->name);
469 ast_cli(a->fd, "Base Templ: %s\n", strlen(cur->src) ? cur->src : "<none>");
470 ast_cli(a->fd, "Username: %s\n", ifthere(cur->user));
471 ast_cli(a->fd, "Secret: %s\n", ifthere(cur->pass));
472 ast_cli(a->fd, "Language: %s\n", ifthere(cur->lang));
473 ast_cli(a->fd, "Bind Port: %d\n", cur->port);
474 ast_cli(a->fd, "Server: %s\n", server);
475 ast_cli(a->fd, "Server Port: %d\n", cur->serverport);
476 ast_cli(a->fd, "Alternate: %s\n", alternate);
477 ast_cli(a->fd, "Flags: %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
478 ast_cli(a->fd, "Format: %s\n", iax2_getformatname(cur->format));
479 ast_cli(a->fd, "TOS: 0x%x\n", cur->tos);
483 ast_mutex_unlock(&provlock);
486 ast_cli(a->fd, "No provisioning templates found\n");
488 ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
493 static struct ast_cli_entry cli_iax2_provision[] = {
494 AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
497 static int iax_provision_init(void)
499 ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
504 static void iax_provision_free_templates(int dead)
506 struct iax_template *cur;
508 /* Drop dead or not (depending on dead) entries while locked */
509 ast_mutex_lock(&provlock);
510 AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
511 if ((dead && cur->dead) || !dead) {
512 AST_LIST_REMOVE_CURRENT(list);
516 AST_LIST_TRAVERSE_SAFE_END;
517 ast_mutex_unlock(&provlock);
520 int iax_provision_unload(void)
523 ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
524 iax_provision_free_templates(0 /* Remove all templates. */);
529 int iax_provision_reload(int reload)
531 struct ast_config *cfg;
532 struct iax_template *cur;
535 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
537 iax_provision_init();
539 cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
540 if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
541 /* Mark all as dead. No need for locking */
542 AST_LIST_TRAVERSE(&templates, cur, list) {
546 /* Load as appropriate */
547 cat = ast_category_browse(cfg, NULL);
549 if (strcasecmp(cat, "general")) {
550 iax_process_template(cfg, cat, found ? "default" : NULL);
552 ast_verb(3, "Loaded provisioning template '%s'\n", cat);
554 cat = ast_category_browse(cfg, cat);
556 ast_config_destroy(cfg);
557 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
560 ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
562 iax_provision_free_templates(1 /* remove only marked as dead */);
564 /* Purge cached signature DB entries */
565 ast_db_deltree("iax/provisioning/cache", NULL);