another round of version tag updates, along with 'show version files' pattern filtering
[asterisk/asterisk.git] / channels / iax2-provision.c
1 /*
2  * IAX Provisioning Protocol 
3  *
4  * Asterisk Interface
5  *
6  * Copyright (C) 2004-2005 Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  *
13  */
14
15 #include "asterisk.h"
16
17 ASTERISK_FILE_VERSION("$Revision$")
18
19 #include "asterisk/config.h"
20 #include "asterisk/logger.h"
21 #include "asterisk/cli.h"
22 #include "asterisk/lock.h"
23 #include "asterisk/frame.h"
24 #include "asterisk/options.h"
25 #include "asterisk/md5.h"
26 #include "asterisk/astdb.h"
27 #include <netinet/in.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/ip.h>
30 #include <sys/socket.h>
31 #include "asterisk/utils.h"
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <netdb.h>
37 #include "iax2.h"
38 #include "iax2-provision.h"
39 #include "iax2-parser.h"
40
41 #ifndef IPTOS_MINCOST
42 #define IPTOS_MINCOST 0x02
43 #endif
44
45 static int provinit = 0;
46
47 struct iax_template {
48         int dead;
49         char name[80];
50         char src[80];
51         struct iax_template *next;
52         char user[20];
53         char pass[20];
54         char lang[10];
55         unsigned short port;
56         unsigned int server;
57         unsigned short serverport;
58         unsigned int altserver;
59         unsigned int flags;
60         unsigned int format;
61         int tos;        
62 } *templates;
63
64 static struct iax_flag {
65         char *name;
66         int value;
67 } iax_flags[] = {
68         { "register", PROV_FLAG_REGISTER },
69         { "secure", PROV_FLAG_SECURE },
70         { "heartbeat", PROV_FLAG_HEARTBEAT },
71         { "debug", PROV_FLAG_DEBUG },
72         { "disablecid", PROV_FLAG_DIS_CALLERID },
73         { "disablecw", PROV_FLAG_DIS_CALLWAIT },
74         { "disablecidcw", PROV_FLAG_DIS_CIDCW },
75         { "disable3way", PROV_FLAG_DIS_THREEWAY },
76 };
77
78 char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
79 {
80         int x;
81         if (!buf || buflen < 1) {
82                 return(NULL);
83         }
84         buf[0] = '\0';
85         for (x=0;x<sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
86                 if (flags & iax_flags[x].value){
87                         strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
88                         strncat(buf, ",", buflen - strlen(buf) - 1);
89                 }
90         }
91         if (strlen(buf)) 
92                 buf[strlen(buf) - 1] = '\0';
93         else
94                 strncpy(buf, "none", buflen - 1);
95         return buf;
96 }
97
98 static unsigned int iax_str2flags(const char *buf)
99 {
100         int x;
101         int len;
102         int found;
103         unsigned int flags = 0;
104         char *e;
105         while(buf && *buf) {
106                 e = strchr(buf, ',');
107                 if (e)
108                         len = e - buf;
109                 else
110                         len = 0;
111                 found = 0;
112                 for (x=0;x<sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
113                         if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
114                             (!len && !strcasecmp(iax_flags[x].name, buf))) {
115                                 flags |= iax_flags[x].value;
116                                 break;
117                         }
118                 }
119                 if (e) {
120                         buf = e + 1;
121                         while(*buf && (*buf < 33))
122                                 buf++;
123                 } else
124                         break;
125         }
126         return flags;
127 }
128 AST_MUTEX_DEFINE_STATIC(provlock);
129
130 static struct iax_template *iax_template_find(const char *s, int allowdead)
131 {
132         struct iax_template *cur;
133         cur = templates;
134         while(cur) {
135                 if (!strcasecmp(s, cur->name)) {
136                         if (!allowdead && cur->dead)
137                                 cur = NULL;
138                         break;
139                 }
140                 cur = cur->next;
141         }
142         return cur;
143 }
144
145 char *iax_prov_complete_template(char *line, char *word, int pos, int state)
146 {
147         struct iax_template *c;
148         int which=0;
149         char *ret;
150         ast_mutex_lock(&provlock);
151         c = templates;
152         while(c) {
153                 if (!strncasecmp(word, c->name, strlen(word))) {
154                         if (++which > state)
155                                 break;
156                 }
157                 c = c->next;
158         }
159         if (c) {
160                 ret = strdup(c->name);
161         } else
162                 ret = NULL;
163         ast_mutex_unlock(&provlock);
164         return ret;
165 }
166
167 static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
168 {
169         struct MD5Context md5;
170         unsigned int tmp[4];
171         MD5Init(&md5);
172         MD5Update(&md5, provdata->buf, provdata->pos);
173         MD5Final((unsigned char *)tmp, &md5);
174         return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
175 }
176
177 int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
178 {
179         struct iax_template *cur;
180         unsigned int sig;
181         char tmp[40];
182         memset(provdata, 0, sizeof(*provdata));
183         ast_mutex_lock(&provlock);
184         cur = iax_template_find(template, 1);
185         /* If no match, try searching for '*' */
186         if (!cur)
187                 cur = iax_template_find("*", 1);
188         if (cur) {
189                 /* found it -- add information elements as appropriate */
190                 if (force || strlen(cur->user))
191                         iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
192                 if (force || strlen(cur->pass))
193                         iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
194                 if (force || strlen(cur->lang))
195                         iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
196                 if (force || cur->port)
197                         iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
198                 if (force || cur->server)
199                         iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
200                 if (force || cur->serverport)
201                         iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
202                 if (force || cur->altserver)
203                         iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
204                 if (force || cur->flags)
205                         iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
206                 if (force || cur->format)
207                         iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
208                 if (force || cur->tos)
209                         iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
210                 
211                 /* Calculate checksum of message so far */
212                 sig = prov_ver_calc(provdata);
213                 if (signature)
214                         *signature = sig;
215                 /* Store signature */
216                 iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
217                 /* Cache signature for later verification so we need not recalculate all this */
218                 snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
219                 ast_db_put("iax/provisioning/cache", template, tmp);
220         } else
221                 ast_db_put("iax/provisioning/cache", template, "u");
222         ast_mutex_unlock(&provlock);
223         return cur ? 0 : -1;
224 }
225
226 int iax_provision_version(unsigned int *version, const char *template, int force)
227 {
228         char tmp[80] = "";
229         struct iax_ie_data ied;
230         int ret=0;
231         memset(&ied, 0, sizeof(ied));
232
233         ast_mutex_lock(&provlock);
234         ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp));
235         if (sscanf(tmp, "v%x", version) != 1) {
236                 if (strcmp(tmp, "u")) {
237                         ret = iax_provision_build(&ied, version, template, force);
238                         if (ret)
239                                 ast_log(LOG_DEBUG, "Unable to create provisioning packet for '%s'\n", template);
240                 } else
241                         ret = -1;
242         } else if (option_debug)
243                 ast_log(LOG_DEBUG, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
244         ast_mutex_unlock(&provlock);
245         return ret;
246 }
247
248 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, char *s, char *def)
249 {
250         struct ast_variable *v;
251         int foundportno = 0;
252         int foundserverportno = 0;
253         int x;
254         struct in_addr ia;
255         struct hostent *hp;
256         struct ast_hostent h;
257         struct iax_template *src, tmp;
258         char *t;
259         if (def) {
260                 t = ast_variable_retrieve(cfg, s ,"template");
261                 src = NULL;
262                 if (t && strlen(t)) {
263                         src = iax_template_find(t, 0);
264                         if (!src)
265                                 ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'.  Trying '%s'\n", t, s, def);
266                         else
267                                 def = t;
268                 } 
269                 if (!src) {
270                         src = iax_template_find(def, 0);
271                         if (!src)
272                                 ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.", def, s);
273                 }
274                 if (!src)
275                         return -1;
276                 ast_mutex_lock(&provlock);      
277                 /* Backup old data */
278                 memcpy(&tmp, cur, sizeof(tmp));
279                 /* Restore from src */
280                 memcpy(cur, src, sizeof(tmp));
281                 /* Restore important headers */
282                 memcpy(cur->name, tmp.name, sizeof(cur->name));
283                 cur->dead = tmp.dead;
284                 cur->next = tmp.next;
285                 ast_mutex_unlock(&provlock);    
286         }
287         if (def)
288                 strncpy(cur->src, def, sizeof(cur->src) - 1);
289         else
290                 cur->src[0] = '\0';
291         v = ast_variable_browse(cfg, s);
292         while(v) {
293                 if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
294                         if ((sscanf(v->value, "%d", &x) == 1) && (x > 0) && (x < 65535)) {
295                                 if (!strcasecmp(v->name, "port")) {
296                                         cur->port = x;
297                                         foundportno = 1;
298                                 } else {
299                                         cur->serverport = x;
300                                         foundserverportno = 1;
301                                 }
302                         } else
303                                 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
304                 } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
305                         hp = ast_gethostbyname(v->value, &h);
306                         if (hp) {
307                                 memcpy(&ia, hp->h_addr, sizeof(ia));
308                                 if (!strcasecmp(v->name, "server"))
309                                         cur->server = ntohl(ia.s_addr);
310                                 else
311                                         cur->altserver = ntohl(ia.s_addr);
312                         } else 
313                                 ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
314                 } else if (!strcasecmp(v->name, "codec")) {
315                         if ((x = ast_getformatbyname(v->value)) > 0) {
316                                 cur->format = x;
317                         } else
318                                 ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
319                 } else if (!strcasecmp(v->name, "tos")) {
320                         if (sscanf(v->value, "%d", &x) == 1)
321                                 cur->tos = x & 0xff;
322                         else if (!strcasecmp(v->value, "lowdelay"))
323                                 cur->tos = IPTOS_LOWDELAY;
324                         else if (!strcasecmp(v->value, "throughput"))
325                                 cur->tos = IPTOS_THROUGHPUT;
326                         else if (!strcasecmp(v->value, "reliability"))
327                                 cur->tos = IPTOS_RELIABILITY;
328                         else if (!strcasecmp(v->value, "mincost"))
329                                 cur->tos = IPTOS_MINCOST;
330                         else if (!strcasecmp(v->value, "none"))
331                                 cur->tos = 0;
332                         else
333                                 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
334                 } else if (!strcasecmp(v->name, "user")) {
335                         strncpy(cur->user, v->value, sizeof(cur->user) - 1);
336                         if (strcmp(cur->user, v->value))
337                                 ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
338                 } else if (!strcasecmp(v->name, "pass")) {
339                         strncpy(cur->pass, v->value, sizeof(cur->pass) - 1);
340                         if (strcmp(cur->pass, v->value))
341                                 ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
342                 } else if (!strcasecmp(v->name, "language")) {
343                         strncpy(cur->lang, v->value, sizeof(cur->lang) - 1);
344                         if (strcmp(cur->lang, v->value))
345                                 ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
346                 } else if (!strcasecmp(v->name, "flags")) {
347                         cur->flags = iax_str2flags(v->value);
348                 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
349                         cur->flags |= iax_str2flags(v->value);
350                 } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
351                         cur->flags &= ~iax_str2flags(v->value);
352                 } else if (strcasecmp(v->name, "template")) {
353                         ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
354                 }
355                 v = v->next;
356         }
357         if (!foundportno)
358                 cur->port = IAX_DEFAULT_PORTNO;
359         if (!foundserverportno)
360                 cur->serverport = IAX_DEFAULT_PORTNO;
361         return 0;
362 }
363
364 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
365 {
366         /* Find an already existing one if there */
367         struct iax_template *cur;
368         int mallocd = 0;
369         cur = templates;
370         while(cur) {
371                 if (!strcasecmp(cur->name, s))
372                         break;
373                 cur = cur->next;
374         }
375         if (!cur) {
376                 mallocd = 1;
377                 cur = malloc(sizeof(struct iax_template));
378                 if (!cur) {
379                         ast_log(LOG_WARNING, "Out of memory!\n");
380                         return -1;
381                 }
382                 /* Initialize entry */
383                 memset(cur, 0, sizeof(*cur));
384                 strncpy(cur->name, s, sizeof(cur->name) - 1);
385                 cur->dead = 1;
386         }
387         if (!iax_template_parse(cur, cfg, s, def))
388                 cur->dead = 0;
389
390         /* Link if we're mallocd */
391         if (mallocd) {
392                 ast_mutex_lock(&provlock);
393                 cur->next = templates;
394                 templates = cur;
395                 ast_mutex_unlock(&provlock);
396         }
397         return 0;
398 }
399
400 static char show_provisioning_usage[] = 
401 "Usage: iax show provisioning [template]\n"
402 "       Lists all known IAX provisioning templates or a\n"
403 "       specific one if specified.\n";
404
405 static const char *ifthere(const char *s)
406 {
407         if (strlen(s))
408                 return s;
409         else
410                 return "<unspecified>";
411 }
412
413 static const char *iax_server(char *a, int alen, unsigned int addr)
414 {
415         struct in_addr ia;
416         if (!addr)
417                 return "<unspecified>";
418         ia.s_addr = htonl(addr);
419         return ast_inet_ntoa(a, alen, ia);
420 }
421
422
423 static int iax_show_provisioning(int fd, int argc, char *argv[])
424 {
425         struct iax_template *cur;
426         char iabuf[80]; /* Has to be big enough for 'flags' too */
427         int found = 0;
428         if ((argc != 3) && (argc != 4))
429                 return RESULT_SHOWUSAGE;
430         ast_mutex_lock(&provlock);
431         for (cur = templates;cur;cur = cur->next) {
432                 if ((argc == 3) || (!strcasecmp(argv[3], cur->name)))  {
433                         if (found) ast_cli(fd, "\n");
434                         ast_cli(fd, "== %s ==\n", cur->name);
435                         ast_cli(fd, "Base Templ:   %s\n", strlen(cur->src) ? cur->src : "<none>");
436                         ast_cli(fd, "Username:     %s\n", ifthere(cur->user));
437                         ast_cli(fd, "Secret:       %s\n", ifthere(cur->pass));
438                         ast_cli(fd, "Language:     %s\n", ifthere(cur->lang));
439                         ast_cli(fd, "Bind Port:    %d\n", cur->port);
440                         ast_cli(fd, "Server:       %s\n", iax_server(iabuf, sizeof(iabuf), cur->server));
441                         ast_cli(fd, "Server Port:  %d\n", cur->serverport);
442                         ast_cli(fd, "Alternate:    %s\n", iax_server(iabuf, sizeof(iabuf), cur->altserver));
443                         ast_cli(fd, "Flags:        %s\n", iax_provflags2str(iabuf, sizeof(iabuf), cur->flags));
444                         ast_cli(fd, "Format:       %s\n", ast_getformatname(cur->format));
445                         ast_cli(fd, "TOS:          %d\n", cur->tos);
446                         found++;
447                 }
448         }
449         ast_mutex_unlock(&provlock);
450         if (!found) {
451                 if (argc == 3)
452                         ast_cli(fd, "No provisioning templates found\n");
453                 else
454                         ast_cli(fd, "No provisioning template matching '%s' found\n", argv[3]);
455         }
456         return RESULT_SUCCESS;
457 }
458
459 static struct ast_cli_entry  cli_show_provisioning = 
460         { { "iax2", "show", "provisioning", NULL }, iax_show_provisioning, "Show iax provisioning", show_provisioning_usage, iax_prov_complete_template };
461
462 static int iax_provision_init(void)
463 {
464         ast_cli_register(&cli_show_provisioning);
465         provinit = 1;
466         return 0;
467 }
468
469 int iax_provision_unload(void)
470 {
471         provinit = 0;
472         ast_cli_unregister(&cli_show_provisioning);
473         return 0;
474 }
475
476 int iax_provision_reload(void)
477 {
478         struct ast_config *cfg;
479         struct iax_template *cur, *prev, *next;
480         char *cat;
481         int found = 0;
482         if (!provinit)
483                 iax_provision_init();
484         /* Mark all as dead.  No need for locking */
485         cur = templates;
486         while(cur) {
487                 cur->dead = 1;
488                 cur = cur->next;
489         }
490         cfg = ast_config_load("iaxprov.conf");
491         if (cfg) {
492                 /* Load as appropriate */
493                 cat = ast_category_browse(cfg, NULL);
494                 while(cat) {
495                         if (strcasecmp(cat, "general")) {
496                                 iax_process_template(cfg, cat, found ? "default" : NULL);
497                                 found++;
498                                 if (option_verbose > 2)
499                                         ast_verbose(VERBOSE_PREFIX_3 "Loaded provisioning template '%s'\n", cat);
500                         }
501                         cat = ast_category_browse(cfg, cat);
502                 }
503         } else
504                 ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
505         ast_mutex_lock(&provlock);
506         /* Drop dead entries while locked */
507         prev = NULL;
508         cur = templates;
509         while(cur) {
510                 next = cur->next;
511                 if (cur->dead) {
512                         if (prev)
513                                 prev->next = next;
514                         else
515                                 templates = next;
516                         free(cur);
517                 } else 
518                         prev = cur;
519                 cur = next;
520         }
521         ast_mutex_unlock(&provlock);
522         /* Purge cached signature DB entries */
523         ast_db_deltree("iax/provisioning/cache", NULL);
524         return 0;
525         
526 }