Wed Oct 28 15:47:54 2009

Asterisk developer's documentation


iax2-provision.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief IAX Provisioning Protocol 
00021  *
00022  */
00023 
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <netdb.h>
00029 #include <netinet/in.h>
00030 #include <netinet/in_systm.h>
00031 #include <netinet/ip.h>
00032 #include <sys/socket.h>
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211526 $")
00037 
00038 #include "asterisk/config.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/cli.h"
00041 #include "asterisk/lock.h"
00042 #include "asterisk/frame.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/md5.h"
00045 #include "asterisk/astdb.h"
00046 #include "asterisk/utils.h"
00047 #include "iax2.h"
00048 #include "iax2-provision.h"
00049 #include "iax2-parser.h"
00050 
00051 #ifndef IPTOS_MINCOST
00052 #define IPTOS_MINCOST 0x02
00053 #endif
00054 
00055 static int provinit = 0;
00056 
00057 struct iax_template {
00058    int dead;
00059    char name[80];
00060    char src[80];
00061    struct iax_template *next;
00062    char user[20];
00063    char pass[20];
00064    char lang[10];
00065    unsigned short port;
00066    unsigned int server;
00067    unsigned short serverport;
00068    unsigned int altserver;
00069    unsigned int flags;
00070    unsigned int format;
00071    int tos; 
00072 } *templates;
00073 
00074 static struct iax_flag {
00075    char *name;
00076    int value;
00077 } iax_flags[] = {
00078    { "register", PROV_FLAG_REGISTER },
00079    { "secure", PROV_FLAG_SECURE },
00080    { "heartbeat", PROV_FLAG_HEARTBEAT },
00081    { "debug", PROV_FLAG_DEBUG },
00082    { "disablecid", PROV_FLAG_DIS_CALLERID },
00083    { "disablecw", PROV_FLAG_DIS_CALLWAIT },
00084    { "disablecidcw", PROV_FLAG_DIS_CIDCW },
00085    { "disable3way", PROV_FLAG_DIS_THREEWAY },
00086 };
00087 
00088 char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
00089 {
00090    int x;
00091    if (!buf || buflen < 1) {
00092       return(NULL);
00093    }
00094    buf[0] = '\0';
00095    for (x=0;x<sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
00096       if (flags & iax_flags[x].value){
00097          strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
00098          strncat(buf, ",", buflen - strlen(buf) - 1);
00099       }
00100    }
00101    if (strlen(buf)) 
00102       buf[strlen(buf) - 1] = '\0';
00103    else
00104       strncpy(buf, "none", buflen - 1);
00105    return buf;
00106 }
00107 
00108 static unsigned int iax_str2flags(const char *buf)
00109 {
00110    int x;
00111    int len;
00112    int found;
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       found = 0;
00122       for (x=0;x<sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
00123          if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
00124              (!len && !strcasecmp(iax_flags[x].name, buf))) {
00125             flags |= iax_flags[x].value;
00126             break;
00127          }
00128       }
00129       if (e) {
00130          buf = e + 1;
00131          while(*buf && (*buf < 33))
00132             buf++;
00133       } else
00134          break;
00135    }
00136    return flags;
00137 }
00138 AST_MUTEX_DEFINE_STATIC(provlock);
00139 
00140 static struct iax_template *iax_template_find(const char *s, int allowdead)
00141 {
00142    struct iax_template *cur;
00143    cur = templates;
00144    while(cur) {
00145       if (!strcasecmp(s, cur->name)) {
00146          if (!allowdead && cur->dead)
00147             cur = NULL;
00148          break;
00149       }
00150       cur = cur->next;
00151    }
00152    return cur;
00153 }
00154 
00155 char *iax_prov_complete_template(char *line, char *word, int pos, int state)
00156 {
00157    struct iax_template *c;
00158    int which=0;
00159    char *ret;
00160    ast_mutex_lock(&provlock);
00161    c = templates;
00162    while(c) {
00163       if (!strncasecmp(word, c->name, strlen(word))) {
00164          if (++which > state)
00165             break;
00166       }
00167       c = c->next;
00168    }
00169    if (c) {
00170       ret = strdup(c->name);
00171    } else
00172       ret = NULL;
00173    ast_mutex_unlock(&provlock);
00174    return ret;
00175 }
00176 
00177 static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
00178 {
00179    struct MD5Context md5;
00180    unsigned int tmp[4];
00181    MD5Init(&md5);
00182    MD5Update(&md5, provdata->buf, provdata->pos);
00183    MD5Final((unsigned char *)tmp, &md5);
00184    return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
00185 }
00186 
00187 int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
00188 {
00189    struct iax_template *cur;
00190    unsigned int sig;
00191    char tmp[40];
00192    memset(provdata, 0, sizeof(*provdata));
00193    ast_mutex_lock(&provlock);
00194    cur = iax_template_find(template, 1);
00195    /* If no match, try searching for '*' */
00196    if (!cur)
00197       cur = iax_template_find("*", 1);
00198    if (cur) {
00199       /* found it -- add information elements as appropriate */
00200       if (force || strlen(cur->user))
00201          iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
00202       if (force || strlen(cur->pass))
00203          iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
00204       if (force || strlen(cur->lang))
00205          iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
00206       if (force || cur->port)
00207          iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
00208       if (force || cur->server)
00209          iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
00210       if (force || cur->serverport)
00211          iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
00212       if (force || cur->altserver)
00213          iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
00214       if (force || cur->flags)
00215          iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
00216       if (force || cur->format)
00217          iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
00218       if (force || cur->tos)
00219          iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
00220       
00221       /* Calculate checksum of message so far */
00222       sig = prov_ver_calc(provdata);
00223       if (signature)
00224          *signature = sig;
00225       /* Store signature */
00226       iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
00227       /* Cache signature for later verification so we need not recalculate all this */
00228       snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
00229       ast_db_put("iax/provisioning/cache", template, tmp);
00230    } else
00231       ast_db_put("iax/provisioning/cache", template, "u");
00232    ast_mutex_unlock(&provlock);
00233    return cur ? 0 : -1;
00234 }
00235 
00236 int iax_provision_version(unsigned int *version, const char *template, int force)
00237 {
00238    char tmp[80] = "";
00239    struct iax_ie_data ied;
00240    int ret=0;
00241    memset(&ied, 0, sizeof(ied));
00242 
00243    ast_mutex_lock(&provlock);
00244    ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp));
00245    if (sscanf(tmp, "v%30x", version) != 1) {
00246       if (strcmp(tmp, "u")) {
00247          ret = iax_provision_build(&ied, version, template, force);
00248          if (ret)
00249             ast_log(LOG_DEBUG, "Unable to create provisioning packet for '%s'\n", template);
00250       } else
00251          ret = -1;
00252    } else if (option_debug)
00253       ast_log(LOG_DEBUG, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
00254    ast_mutex_unlock(&provlock);
00255    return ret;
00256 }
00257 
00258 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, char *s, char *def)
00259 {
00260    struct ast_variable *v;
00261    int foundportno = 0;
00262    int foundserverportno = 0;
00263    int x;
00264    struct in_addr ia;
00265    struct hostent *hp;
00266    struct ast_hostent h;
00267    struct iax_template *src, tmp;
00268    char *t;
00269    if (def) {
00270       t = ast_variable_retrieve(cfg, s ,"template");
00271       src = NULL;
00272       if (t && strlen(t)) {
00273          src = iax_template_find(t, 0);
00274          if (!src)
00275             ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'.  Trying '%s'\n", t, s, def);
00276          else
00277             def = t;
00278       } 
00279       if (!src) {
00280          src = iax_template_find(def, 0);
00281          if (!src)
00282             ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
00283       }
00284       if (!src)
00285          return -1;
00286       ast_mutex_lock(&provlock); 
00287       /* Backup old data */
00288       memcpy(&tmp, cur, sizeof(tmp));
00289       /* Restore from src */
00290       memcpy(cur, src, sizeof(tmp));
00291       /* Restore important headers */
00292       memcpy(cur->name, tmp.name, sizeof(cur->name));
00293       cur->dead = tmp.dead;
00294       cur->next = tmp.next;
00295       ast_mutex_unlock(&provlock);  
00296    }
00297    if (def)
00298       strncpy(cur->src, def, sizeof(cur->src) - 1);
00299    else
00300       cur->src[0] = '\0';
00301    v = ast_variable_browse(cfg, s);
00302    while(v) {
00303       if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
00304          if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
00305             if (!strcasecmp(v->name, "port")) {
00306                cur->port = x;
00307                foundportno = 1;
00308             } else {
00309                cur->serverport = x;
00310                foundserverportno = 1;
00311             }
00312          } else
00313             ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00314       } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
00315          hp = ast_gethostbyname(v->value, &h);
00316          if (hp) {
00317             memcpy(&ia, hp->h_addr, sizeof(ia));
00318             if (!strcasecmp(v->name, "server"))
00319                cur->server = ntohl(ia.s_addr);
00320             else
00321                cur->altserver = ntohl(ia.s_addr);
00322          } else 
00323             ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00324       } else if (!strcasecmp(v->name, "codec")) {
00325          if ((x = ast_getformatbyname(v->value)) > 0) {
00326             cur->format = x;
00327          } else
00328             ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
00329       } else if (!strcasecmp(v->name, "tos")) {
00330          if (sscanf(v->value, "%3d", &x) == 1)
00331             cur->tos = x & 0xff;
00332          else if (!strcasecmp(v->value, "lowdelay"))
00333             cur->tos = IPTOS_LOWDELAY;
00334          else if (!strcasecmp(v->value, "throughput"))
00335             cur->tos = IPTOS_THROUGHPUT;
00336          else if (!strcasecmp(v->value, "reliability"))
00337             cur->tos = IPTOS_RELIABILITY;
00338          else if (!strcasecmp(v->value, "mincost"))
00339             cur->tos = IPTOS_MINCOST;
00340          else if (!strcasecmp(v->value, "none"))
00341             cur->tos = 0;
00342          else
00343             ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
00344       } else if (!strcasecmp(v->name, "user")) {
00345          strncpy(cur->user, v->value, sizeof(cur->user) - 1);
00346          if (strcmp(cur->user, v->value))
00347             ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
00348       } else if (!strcasecmp(v->name, "pass")) {
00349          strncpy(cur->pass, v->value, sizeof(cur->pass) - 1);
00350          if (strcmp(cur->pass, v->value))
00351             ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
00352       } else if (!strcasecmp(v->name, "language")) {
00353          strncpy(cur->lang, v->value, sizeof(cur->lang) - 1);
00354          if (strcmp(cur->lang, v->value))
00355             ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
00356       } else if (!strcasecmp(v->name, "flags")) {
00357          cur->flags = iax_str2flags(v->value);
00358       } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
00359          cur->flags |= iax_str2flags(v->value);
00360       } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
00361          cur->flags &= ~iax_str2flags(v->value);
00362       } else if (strcasecmp(v->name, "template")) {
00363          ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
00364       }
00365       v = v->next;
00366    }
00367    if (!foundportno)
00368       cur->port = IAX_DEFAULT_PORTNO;
00369    if (!foundserverportno)
00370       cur->serverport = IAX_DEFAULT_PORTNO;
00371    return 0;
00372 }
00373 
00374 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
00375 {
00376    /* Find an already existing one if there */
00377    struct iax_template *cur;
00378    int mallocd = 0;
00379    cur = templates;
00380    while(cur) {
00381       if (!strcasecmp(cur->name, s))
00382          break;
00383       cur = cur->next;
00384    }
00385    if (!cur) {
00386       mallocd = 1;
00387       cur = malloc(sizeof(struct iax_template));
00388       if (!cur) {
00389          ast_log(LOG_WARNING, "Out of memory!\n");
00390          return -1;
00391       }
00392       /* Initialize entry */
00393       memset(cur, 0, sizeof(*cur));
00394       strncpy(cur->name, s, sizeof(cur->name) - 1);
00395       cur->dead = 1;
00396    }
00397    if (!iax_template_parse(cur, cfg, s, def))
00398       cur->dead = 0;
00399 
00400    /* Link if we're mallocd */
00401    if (mallocd) {
00402       ast_mutex_lock(&provlock);
00403       cur->next = templates;
00404       templates = cur;
00405       ast_mutex_unlock(&provlock);
00406    }
00407    return 0;
00408 }
00409 
00410 static char show_provisioning_usage[] = 
00411 "Usage: iax show provisioning [template]\n"
00412 "       Lists all known IAX provisioning templates or a\n"
00413 "       specific one if specified.\n";
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(char *a, int alen, unsigned int addr)
00424 {
00425    struct in_addr ia;
00426    if (!addr)
00427       return "<unspecified>";
00428    ia.s_addr = htonl(addr);
00429    return ast_inet_ntoa(a, alen, ia);
00430 }
00431 
00432 
00433 static int iax_show_provisioning(int fd, int argc, char *argv[])
00434 {
00435    struct iax_template *cur;
00436    char iabuf[80];   /* Has to be big enough for 'flags' too */
00437    int found = 0;
00438    if ((argc != 3) && (argc != 4))
00439       return RESULT_SHOWUSAGE;
00440    ast_mutex_lock(&provlock);
00441    for (cur = templates;cur;cur = cur->next) {
00442       if ((argc == 3) || (!strcasecmp(argv[3], cur->name)))  {
00443          if (found) ast_cli(fd, "\n");
00444          ast_cli(fd, "== %s ==\n", cur->name);
00445          ast_cli(fd, "Base Templ:   %s\n", strlen(cur->src) ? cur->src : "<none>");
00446          ast_cli(fd, "Username:     %s\n", ifthere(cur->user));
00447          ast_cli(fd, "Secret:       %s\n", ifthere(cur->pass));
00448          ast_cli(fd, "Language:     %s\n", ifthere(cur->lang));
00449          ast_cli(fd, "Bind Port:    %d\n", cur->port);
00450          ast_cli(fd, "Server:       %s\n", iax_server(iabuf, sizeof(iabuf), cur->server));
00451          ast_cli(fd, "Server Port:  %d\n", cur->serverport);
00452          ast_cli(fd, "Alternate:    %s\n", iax_server(iabuf, sizeof(iabuf), cur->altserver));
00453          ast_cli(fd, "Flags:        %s\n", iax_provflags2str(iabuf, sizeof(iabuf), cur->flags));
00454          ast_cli(fd, "Format:       %s\n", ast_getformatname(cur->format));
00455          ast_cli(fd, "TOS:          %d\n", cur->tos);
00456          found++;
00457       }
00458    }
00459    ast_mutex_unlock(&provlock);
00460    if (!found) {
00461       if (argc == 3)
00462          ast_cli(fd, "No provisioning templates found\n");
00463       else
00464          ast_cli(fd, "No provisioning template matching '%s' found\n", argv[3]);
00465    }
00466    return RESULT_SUCCESS;
00467 }
00468 
00469 static struct ast_cli_entry  cli_show_provisioning = 
00470    { { "iax2", "show", "provisioning", NULL }, iax_show_provisioning, "Show iax provisioning", show_provisioning_usage, iax_prov_complete_template };
00471 
00472 static int iax_provision_init(void)
00473 {
00474    ast_cli_register(&cli_show_provisioning);
00475    provinit = 1;
00476    return 0;
00477 }
00478 
00479 int iax_provision_unload(void)
00480 {
00481    provinit = 0;
00482    ast_cli_unregister(&cli_show_provisioning);
00483    return 0;
00484 }
00485 
00486 int iax_provision_reload(void)
00487 {
00488    struct ast_config *cfg;
00489    struct iax_template *cur, *prev, *next;
00490    char *cat;
00491    int found = 0;
00492    if (!provinit)
00493       iax_provision_init();
00494    /* Mark all as dead.  No need for locking */
00495    cur = templates;
00496    while(cur) {
00497       cur->dead = 1;
00498       cur = cur->next;
00499    }
00500    cfg = ast_config_load("iaxprov.conf");
00501    if (cfg) {
00502       /* Load as appropriate */
00503       cat = ast_category_browse(cfg, NULL);
00504       while(cat) {
00505          if (strcasecmp(cat, "general")) {
00506             iax_process_template(cfg, cat, found ? "default" : NULL);
00507             found++;
00508             if (option_verbose > 2)
00509                ast_verbose(VERBOSE_PREFIX_3 "Loaded provisioning template '%s'\n", cat);
00510          }
00511          cat = ast_category_browse(cfg, cat);
00512       }
00513       ast_config_destroy(cfg);
00514    } else
00515       ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
00516    ast_mutex_lock(&provlock);
00517    /* Drop dead entries while locked */
00518    prev = NULL;
00519    cur = templates;
00520    while(cur) {
00521       next = cur->next;
00522       if (cur->dead) {
00523          if (prev)
00524             prev->next = next;
00525          else
00526             templates = next;
00527          free(cur);
00528       } else 
00529          prev = cur;
00530       cur = next;
00531    }
00532    ast_mutex_unlock(&provlock);
00533    /* Purge cached signature DB entries */
00534    ast_db_deltree("iax/provisioning/cache", NULL);
00535    return 0;
00536    
00537 }

Generated on Wed Oct 28 15:47:54 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6