Debian Codegraph @

channels/iax2/provision.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 *
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.
13 *
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.
17 */
18
19 /*! \file
20 *
21 * \brief IAX Provisioning Protocol
22 *
23 * \author Mark Spencer <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include <netdb.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <sys/socket.h>
37
38 #include "asterisk/config.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/frame.h"
42 #include "asterisk/md5.h"
43 #include "asterisk/astdb.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/acl.h"
46 #include "asterisk/format_cache.h"
47 #include "asterisk/format_compatibility.h"
48
49 #include "include/iax2.h"
50 #include "include/provision.h"
51 #include "include/parser.h"
52
53 static int provinit = 0;
54
55 struct iax_template {
56 int dead;
57 char name[80];
58 char src[80];
59 char user[20];
60 char pass[20];
61 char lang[10];
62 unsigned short port;
63 unsigned int server;
64 unsigned short serverport;
65 unsigned int altserver;
66 unsigned int flags;
67 iax2_format format;
68 unsigned int tos;
69 AST_LIST_ENTRY(iax_template) list;
70 };
71
72 static AST_LIST_HEAD_NOLOCK_STATIC(templates, iax_template);
73
74 AST_MUTEX_DEFINE_STATIC(provlock);
75
76 static struct iax_flag {
77 char *name;
78 int value;
79 } iax_flags[] = {
80 { "register", PROV_FLAG_REGISTER },
81 { "secure", PROV_FLAG_SECURE },
82 { "heartbeat", PROV_FLAG_HEARTBEAT },
83 { "debug", PROV_FLAG_DEBUG },
84 { "disablecid", PROV_FLAG_DIS_CALLERID },
85 { "disablecw", PROV_FLAG_DIS_CALLWAIT },
86 { "disablecidcw", PROV_FLAG_DIS_CIDCW },
87 { "disable3way", PROV_FLAG_DIS_THREEWAY },
88 };
89
90 char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
91 {
92 int x;
93
94 if (!buf || buflen < 1)
95 return NULL;
96
97 buf[0] = '\0';
98
99 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
100 if (flags & iax_flags[x].value){
101 strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
102 strncat(buf, ",", buflen - strlen(buf) - 1);
103 }
104 }
105
106 if (!ast_strlen_zero(buf))
107 buf[strlen(buf) - 1] = '\0';
108 else
109 strncpy(buf, "none", buflen - 1);
110
111 return buf;
112 }
113
114 static unsigned int iax_str2flags(const char *buf)
115 {
116 int x;
117 int len;
118 unsigned int flags = 0;
119 const char *e;
120 while(buf && *buf) {
121 e = strchr(buf, ',');
122 if (e)
123 len = e - buf;
124 else
125 len = 0;
126 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
127 if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
128 (!len && !strcasecmp(iax_flags[x].name, buf))) {
129 flags |= iax_flags[x].value;
130 break;
131 }
132 }
133 if (e) {
134 buf = e + 1;
135 while(*buf && (*buf < 33))
136 buf++;
137 } else
138 break;
139 }
140 return flags;
141 }
142
143 static void iax_template_copy(struct iax_template *dst, struct iax_template *src)
144 {
145 if (!dst || !src) {
146 return;
147 }
148
149 dst->dead = src->dead;
150 ast_copy_string(dst->name, src->name, sizeof(dst->name));
151 ast_copy_string(dst->src, src->src, sizeof(dst->src));
152 ast_copy_string(dst->user, src->user, sizeof(dst->user));
153 ast_copy_string(dst->pass, src->pass, sizeof(dst->pass));
154 ast_copy_string(dst->lang, src->lang, sizeof(dst->lang));
155 dst->port = src->port;
156 dst->server = src->server;
157 dst->altserver = src->altserver;
158 dst->flags = src->flags;
159 dst->format = src->format;
160 dst->tos = src->tos;
161 }
162
163 static struct iax_template *iax_template_find(const char *s, int allowdead)
164 {
165 struct iax_template *cur;
166
167 AST_LIST_TRAVERSE(&templates, cur, list) {
168 if (!strcasecmp(s, cur->name)) {
169 if (!allowdead && cur->dead) {
170 cur = NULL;
171 }
172 break;
173 }
174 }
175
176 return cur;
177 }
178
179 char *iax_prov_complete_template(const char *line, const char *word, int pos, int state)
180 {
181 struct iax_template *c;
182 int which=0;
183 char *ret = NULL;
184 int wordlen = strlen(word);
185
186 if (pos == 3) {
187 ast_mutex_lock(&provlock);
188 AST_LIST_TRAVERSE(&templates, c, list) {
189 if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
190 ret = ast_strdup(c->name);
191 break;
192 }
193 }
194 ast_mutex_unlock(&provlock);
195 }
196 return ret;
197 }
198
199 static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
200 {
201 struct MD5Context md5;
202 unsigned int tmp[4];
203 MD5Init(&md5);
204 MD5Update(&md5, provdata->buf, provdata->pos);
205 MD5Final((unsigned char *)tmp, &md5);
206 return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
207 }
208
209 int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
210 {
211 struct iax_template *cur;
212 unsigned int sig;
213 char tmp[40];
214 memset(provdata, 0, sizeof(*provdata));
215 ast_mutex_lock(&provlock);
216 cur = iax_template_find(template, 1);
217 /* If no match, try searching for '*' */
218 if (!cur)
219 cur = iax_template_find("*", 1);
220 if (cur) {
221 /* found it -- add information elements as appropriate */
222 if (force || strlen(cur->user))
223 iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
224 if (force || strlen(cur->pass))
225 iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
226 if (force || strlen(cur->lang))
227 iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
228 if (force || cur->port)
229 iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
230 if (force || cur->server)
231 iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
232 if (force || cur->serverport)
233 iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
234 if (force || cur->altserver)
235 iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
236 if (force || cur->flags)
237 iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
238 if (force || cur->format)
239 iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
240 if (force || cur->tos)
241 iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
242
243 /* Calculate checksum of message so far */
244 sig = prov_ver_calc(provdata);
245 if (signature)
246 *signature = sig;
247 /* Store signature */
248 iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
249 /* Cache signature for later verification so we need not recalculate all this */
250 snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
251 ast_db_put("iax/provisioning/cache", template, tmp);
252 } else
253 ast_db_put("iax/provisioning/cache", template, "u");
254 ast_mutex_unlock(&provlock);
255 return cur ? 0 : -1;
256 }
257
258 int iax_provision_version(unsigned int *version, const char *template, int force)
259 {
260 char tmp[80] = "";
261 struct iax_ie_data ied;
262 int ret=0;
263 memset(&ied, 0, sizeof(ied));
264
265 ast_mutex_lock(&provlock);
266 if (ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp))) {
267 ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache/%s\n", template);
268 }
269 if (sscanf(tmp, "v%30x", version) != 1) {
270 if (strcmp(tmp, "u")) {
271 ret = iax_provision_build(&ied, version, template, force);
272 if (ret)
273 ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
274 } else
275 ret = -1;
276 } else
277 ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
278 ast_mutex_unlock(&provlock);
279 return ret;
280 }
281
282 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
283 {
284 struct ast_variable *v;
285 int foundportno = 0;
286 int foundserverportno = 0;
287 int x;
288 struct iax_template *src, tmp;
289 const char *t;
290 if (def) {
291 t = ast_variable_retrieve(cfg, s ,"template");
292 src = NULL;
293 if (t && strlen(t)) {
294 src = iax_template_find(t, 0);
295 if (!src)
296 ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
297 else
298 def = t;
299 }
300 if (!src) {
301 src = iax_template_find(def, 0);
302 if (!src)
303 ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
304 }
305 if (!src)
306 return -1;
307 ast_mutex_lock(&provlock);
308 /* Backup old data */
309 iax_template_copy(&tmp, cur);
310 /* Restore from src */
311 iax_template_copy(cur, src);
312 /* Restore important headers */
313 memcpy(cur->name, tmp.name, sizeof(cur->name));
314 cur->dead = tmp.dead;
315 ast_mutex_unlock(&provlock);
316 }
317 if (def)
318 ast_copy_string(cur->src, def, sizeof(cur->src));
319 else
320 cur->src[0] = '\0';
321 v = ast_variable_browse(cfg, s);
322 while(v) {
323 if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
324 if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
325 if (!strcasecmp(v->name, "port")) {
326 cur->port = x;
327 foundportno = 1;
328 } else {
329 cur->serverport = x;
330 foundserverportno = 1;
331 }
332 } else
333 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
334 } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
335 struct ast_sockaddr addr = { {0,} };
336 if (ast_sockaddr_resolve_first_af(&addr, v->value, PARSE_PORT_FORBID, AF_INET)) {
337 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
338 } else {
339 if (!strcasecmp(v->name, "server"))
340 cur->server = ast_sockaddr_ipv4(&addr);
341 else
342 cur->altserver = ast_sockaddr_ipv4(&addr);
343 }
344 } else if (!strcasecmp(v->name, "codec")) {
345 struct ast_format *tmpfmt;
346 if ((tmpfmt = ast_format_cache_get(v->value))) {
347 cur->format = ast_format_compatibility_format2bitfield(tmpfmt);
348 ao2_ref(tmpfmt, -1);
349 } else
350 ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
351 } else if (!strcasecmp(v->name, "tos")) {
352 if (ast_str2tos(v->value, &cur->tos))
353 ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
354 } else if (!strcasecmp(v->name, "user")) {
355 ast_copy_string(cur->user, v->value, sizeof(cur->user));
356 if (strcmp(cur->user, v->value))
357 ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
358 } else if (!strcasecmp(v->name, "pass")) {
359 ast_copy_string(cur->pass, v->value, sizeof(cur->pass));
360 if (strcmp(cur->pass, v->value))
361 ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
362 } else if (!strcasecmp(v->name, "language")) {
363 ast_copy_string(cur->lang, v->value, sizeof(cur->lang));
364 if (strcmp(cur->lang, v->value))
365 ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
366 } else if (!strcasecmp(v->name, "flags")) {
367 cur->flags = iax_str2flags(v->value);
368 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
369 cur->flags |= iax_str2flags(v->value);
370 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
371 cur->flags &= ~iax_str2flags(v->value);
372 } else if (strcasecmp(v->name, "template")) {
373 ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
374 }
375 v = v->next;
376 }
377 if (!foundportno)
378 cur->port = IAX_DEFAULT_PORTNO;
379 if (!foundserverportno)
380 cur->serverport = IAX_DEFAULT_PORTNO;
381 return 0;
382 }
383
384 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
385 {
386 /* Find an already existing one if there */
387 struct iax_template *cur;
388 int mallocd = 0;
389
390 cur = iax_template_find(s, 1 /* allow dead */);
391 if (!cur) {
392 mallocd = 1;
393 cur = ast_calloc(1, sizeof(*cur));
394 if (!cur) {
395 ast_log(LOG_WARNING, "Out of memory!\n");
396 return -1;
397 }
398 /* Initialize entry */
399 ast_copy_string(cur->name, s, sizeof(cur->name));
400 cur->dead = 1;
401 }
402 if (!iax_template_parse(cur, cfg, s, def))
403 cur->dead = 0;
404
405 /* Link if we're mallocd */
406 if (mallocd) {
407 ast_mutex_lock(&provlock);
408 AST_LIST_INSERT_HEAD(&templates, cur, list);
409 ast_mutex_unlock(&provlock);
410 }
411 return 0;
412 }
413
414 static const char *ifthere(const char *s)
415 {
416 if (strlen(s))
417 return s;
418 else
419 return "<unspecified>";
420 }
421
422 static const char *iax_server(unsigned int addr)
423 {
424 struct in_addr ia;
425
426 if (!addr)
427 return "<unspecified>";
428
429 ia.s_addr = htonl(addr);
430
431 return ast_inet_ntoa(ia);
432 }
433
434
435 static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
436 {
437 struct iax_template *cur;
438 char server[INET_ADDRSTRLEN];
439 char alternate[INET_ADDRSTRLEN];
440 char flags[80]; /* Has to be big enough for 'flags' too */
441 int found = 0;
442
443 switch (cmd) {
444 case CLI_INIT:
445 e->command = "iax2 show provisioning";
446 e->usage =
447 "Usage: iax2 show provisioning [template]\n"
448 " Lists all known IAX provisioning templates or a\n"
449 " specific one if specified.\n";
450 return NULL;
451 case CLI_GENERATE:
452 return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
453 }
454
455 if ((a->argc != 3) && (a->argc != 4))
456 return CLI_SHOWUSAGE;
457
458 ast_mutex_lock(&provlock);
459 AST_LIST_TRAVERSE(&templates, cur, list) {
460 if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
461 if (found)
462 ast_cli(a->fd, "\n");
463 ast_copy_string(server, iax_server(cur->server), sizeof(server));
464 ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
465 ast_cli(a->fd, "== %s ==\n", cur->name);
466 ast_cli(a->fd, "Base Templ: %s\n", strlen(cur->src) ? cur->src : "<none>");
467 ast_cli(a->fd, "Username: %s\n", ifthere(cur->user));
468 ast_cli(a->fd, "Secret: %s\n", ifthere(cur->pass));
469 ast_cli(a->fd, "Language: %s\n", ifthere(cur->lang));
470 ast_cli(a->fd, "Bind Port: %d\n", cur->port);
471 ast_cli(a->fd, "Server: %s\n", server);
472 ast_cli(a->fd, "Server Port: %d\n", cur->serverport);
473 ast_cli(a->fd, "Alternate: %s\n", alternate);
474 ast_cli(a->fd, "Flags: %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
475 ast_cli(a->fd, "Format: %s\n", iax2_getformatname(cur->format));
476 ast_cli(a->fd, "TOS: 0x%x\n", cur->tos);
477 found++;
478 }
479 }
480 ast_mutex_unlock(&provlock);
481 if (!found) {
482 if (a->argc == 3)
483 ast_cli(a->fd, "No provisioning templates found\n");
484 else
485 ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
486 }
487 return CLI_SUCCESS;
488 }
489
490 static struct ast_cli_entry cli_iax2_provision[] = {
491 AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
492 };
493
494 static int iax_provision_init(void)
495 {
496 ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
497 provinit = 1;
498 return 0;
499 }
500
501 static void iax_provision_free_templates(int dead)
502 {
503 struct iax_template *cur;
504
505 /* Drop dead or not (depending on dead) entries while locked */
506 ast_mutex_lock(&provlock);
507 AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
508 if ((dead && cur->dead) || !dead) {
509 AST_LIST_REMOVE_CURRENT(list);
510 ast_free(cur);
511 }
512 }
513 AST_LIST_TRAVERSE_SAFE_END;
514 ast_mutex_unlock(&provlock);
515 }
516
517 int iax_provision_unload(void)
518 {
519 provinit = 0;
520 ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
521 iax_provision_free_templates(0 /* Remove all templates. */);
522
523 return 0;
524 }
525
526 int iax_provision_reload(int reload)
527 {
528 struct ast_config *cfg;
529 struct iax_template *cur;
530 char *cat;
531 int found = 0;
532 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
533 if (!provinit)
534 iax_provision_init();
535
536 cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
537 if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
538 /* Mark all as dead. No need for locking */
539 AST_LIST_TRAVERSE(&templates, cur, list) {
540 cur->dead = 1;
541 }
542
543 /* Load as appropriate */
544 cat = ast_category_browse(cfg, NULL);
545 while(cat) {
546 if (strcasecmp(cat, "general")) {
547 iax_process_template(cfg, cat, found ? "default" : NULL);
548 found++;
549 ast_verb(3, "Loaded provisioning template '%s'\n", cat);
550 }
551 cat = ast_category_browse(cfg, cat);
552 }
553 ast_config_destroy(cfg);
554 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
555 return 0;
556 else
557 ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
558
559 iax_provision_free_templates(1 /* remove only marked as dead */);
560
561 /* Purge cached signature DB entries */
562 ast_db_deltree("iax/provisioning/cache", NULL);
563 return 0;
564 }