Sun May 20 06:33:54 2012

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 - 2006, 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  * 
00021  * \brief IAX Provisioning Protocol 
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 366126 $")
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    iax2_format 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    /* If no match, try searching for '*' */
00213    if (!cur)
00214       cur = iax_template_find("*", 1);
00215    if (cur) {
00216       /* found it -- add information elements as appropriate */
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       /* Calculate checksum of message so far */
00239       sig = prov_ver_calc(provdata);
00240       if (signature)
00241          *signature = sig;
00242       /* Store signature */
00243       iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
00244       /* Cache signature for later verification so we need not recalculate all this */
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    if (!(ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp)))) {
00262       ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache\n");
00263       ast_mutex_unlock(&provlock);
00264       return -1;
00265    }
00266    if (sscanf(tmp, "v%30x", version) != 1) {
00267       if (strcmp(tmp, "u")) {
00268          ret = iax_provision_build(&ied, version, template, force);
00269          if (ret)
00270             ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
00271       } else
00272          ret = -1;
00273    } else
00274       ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
00275    ast_mutex_unlock(&provlock);
00276    return ret;
00277 }
00278 
00279 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
00280 {
00281    struct ast_variable *v;
00282    int foundportno = 0;
00283    int foundserverportno = 0;
00284    int x;
00285    struct in_addr ia;
00286    struct hostent *hp;
00287    struct ast_hostent h;
00288    struct iax_template *src, tmp;
00289    const char *t;
00290    if (def) {
00291       t = ast_variable_retrieve(cfg, s ,"template");
00292       src = NULL;
00293       if (t && strlen(t)) {
00294          src = iax_template_find(t, 0);
00295          if (!src)
00296             ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'.  Trying '%s'\n", t, s, def);
00297          else
00298             def = t;
00299       } 
00300       if (!src) {
00301          src = iax_template_find(def, 0);
00302          if (!src)
00303             ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
00304       }
00305       if (!src)
00306          return -1;
00307       ast_mutex_lock(&provlock);
00308       /* Backup old data */
00309       iax_template_copy(&tmp, cur);
00310       /* Restore from src */
00311       iax_template_copy(cur, src);
00312       /* Restore important headers */
00313       memcpy(cur->name, tmp.name, sizeof(cur->name));
00314       cur->dead = tmp.dead;
00315       ast_mutex_unlock(&provlock);
00316    }
00317    if (def)
00318       ast_copy_string(cur->src, def, sizeof(cur->src));
00319    else
00320       cur->src[0] = '\0';
00321    v = ast_variable_browse(cfg, s);
00322    while(v) {
00323       if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
00324          if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
00325             if (!strcasecmp(v->name, "port")) {
00326                cur->port = x;
00327                foundportno = 1;
00328             } else {
00329                cur->serverport = x;
00330                foundserverportno = 1;
00331             }
00332          } else
00333             ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00334       } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
00335          hp = ast_gethostbyname(v->value, &h);
00336          if (hp) {
00337             memcpy(&ia, hp->h_addr, sizeof(ia));
00338             if (!strcasecmp(v->name, "server"))
00339                cur->server = ntohl(ia.s_addr);
00340             else
00341                cur->altserver = ntohl(ia.s_addr);
00342          } else 
00343             ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00344       } else if (!strcasecmp(v->name, "codec")) {
00345          struct ast_format tmpfmt;
00346          if ((ast_getformatbyname(v->value, &tmpfmt)) > 0) {
00347             cur->format = ast_format_to_old_bitfield(&tmpfmt);
00348          } else
00349             ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
00350       } else if (!strcasecmp(v->name, "tos")) {
00351          if (ast_str2tos(v->value, &cur->tos))
00352             ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
00353       } else if (!strcasecmp(v->name, "user")) {
00354          ast_copy_string(cur->user, v->value, sizeof(cur->user));
00355          if (strcmp(cur->user, v->value))
00356             ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
00357       } else if (!strcasecmp(v->name, "pass")) {
00358          ast_copy_string(cur->pass, v->value, sizeof(cur->pass));
00359          if (strcmp(cur->pass, v->value))
00360             ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
00361       } else if (!strcasecmp(v->name, "language")) {
00362          ast_copy_string(cur->lang, v->value, sizeof(cur->lang));
00363          if (strcmp(cur->lang, v->value))
00364             ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
00365       } else if (!strcasecmp(v->name, "flags")) {
00366          cur->flags = iax_str2flags(v->value);
00367       } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
00368          cur->flags |= iax_str2flags(v->value);
00369       } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
00370          cur->flags &= ~iax_str2flags(v->value);
00371       } else if (strcasecmp(v->name, "template")) {
00372          ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
00373       }
00374       v = v->next;
00375    }
00376    if (!foundportno)
00377       cur->port = IAX_DEFAULT_PORTNO;
00378    if (!foundserverportno)
00379       cur->serverport = IAX_DEFAULT_PORTNO;
00380    return 0;
00381 }
00382 
00383 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
00384 {
00385    /* Find an already existing one if there */
00386    struct iax_template *cur;
00387    int mallocd = 0;
00388 
00389    cur = iax_template_find(s, 1 /* allow dead */);
00390    if (!cur) {
00391       mallocd = 1;
00392       cur = ast_calloc(1, sizeof(*cur));
00393       if (!cur) {
00394          ast_log(LOG_WARNING, "Out of memory!\n");
00395          return -1;
00396       }
00397       /* Initialize entry */
00398       ast_copy_string(cur->name, s, sizeof(cur->name));
00399       cur->dead = 1;
00400    }
00401    if (!iax_template_parse(cur, cfg, s, def))
00402       cur->dead = 0;
00403 
00404    /* Link if we're mallocd */
00405    if (mallocd) {
00406       ast_mutex_lock(&provlock);
00407       AST_LIST_INSERT_HEAD(&templates, cur, list);
00408       ast_mutex_unlock(&provlock);
00409    }
00410    return 0;
00411 }
00412 
00413 static const char *ifthere(const char *s)
00414 {
00415    if (strlen(s))
00416       return s;
00417    else
00418       return "<unspecified>";
00419 }
00420 
00421 static const char *iax_server(unsigned int addr)
00422 {
00423    struct in_addr ia;
00424    
00425    if (!addr)
00426       return "<unspecified>";
00427    
00428    ia.s_addr = htonl(addr);
00429 
00430    return ast_inet_ntoa(ia);
00431 }
00432 
00433 
00434 static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00435 {
00436    struct iax_template *cur;
00437    char server[INET_ADDRSTRLEN];
00438    char alternate[INET_ADDRSTRLEN];
00439    char flags[80];   /* Has to be big enough for 'flags' too */
00440    int found = 0;
00441 
00442    switch (cmd) {
00443    case CLI_INIT:
00444       e->command = "iax2 show provisioning";
00445       e->usage =
00446          "Usage: iax2 show provisioning [template]\n"
00447          "       Lists all known IAX provisioning templates or a\n"
00448          "       specific one if specified.\n";
00449       return NULL;
00450    case CLI_GENERATE:
00451       return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
00452    }
00453 
00454    if ((a->argc != 3) && (a->argc != 4))
00455       return CLI_SHOWUSAGE;
00456 
00457    ast_mutex_lock(&provlock);
00458    AST_LIST_TRAVERSE(&templates, cur, list) {
00459       if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name)))  {
00460          if (found) 
00461             ast_cli(a->fd, "\n");
00462          ast_copy_string(server, iax_server(cur->server), sizeof(server));
00463          ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
00464          ast_cli(a->fd, "== %s ==\n", cur->name);
00465          ast_cli(a->fd, "Base Templ:   %s\n", strlen(cur->src) ? cur->src : "<none>");
00466          ast_cli(a->fd, "Username:     %s\n", ifthere(cur->user));
00467          ast_cli(a->fd, "Secret:       %s\n", ifthere(cur->pass));
00468          ast_cli(a->fd, "Language:     %s\n", ifthere(cur->lang));
00469          ast_cli(a->fd, "Bind Port:    %d\n", cur->port);
00470          ast_cli(a->fd, "Server:       %s\n", server);
00471          ast_cli(a->fd, "Server Port:  %d\n", cur->serverport);
00472          ast_cli(a->fd, "Alternate:    %s\n", alternate);
00473          ast_cli(a->fd, "Flags:        %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
00474          ast_cli(a->fd, "Format:       %s\n", iax2_getformatname(cur->format));
00475          ast_cli(a->fd, "TOS:          0x%x\n", cur->tos);
00476          found++;
00477       }
00478    }
00479    ast_mutex_unlock(&provlock);
00480    if (!found) {
00481       if (a->argc == 3)
00482          ast_cli(a->fd, "No provisioning templates found\n");
00483       else
00484          ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
00485    }
00486    return CLI_SUCCESS;
00487 }
00488 
00489 static struct ast_cli_entry cli_iax2_provision[] = {
00490    AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
00491 };
00492 
00493 static int iax_provision_init(void)
00494 {
00495    ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00496    provinit = 1;
00497    return 0;
00498 }
00499 
00500 static void iax_provision_free_templates(int dead)
00501 {
00502    struct iax_template *cur;
00503 
00504    /* Drop dead or not (depending on dead) entries while locked */
00505    ast_mutex_lock(&provlock);
00506    AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
00507       if ((dead && cur->dead) || !dead) {
00508          AST_LIST_REMOVE_CURRENT(list);
00509          ast_free(cur);
00510       }
00511    }
00512    AST_LIST_TRAVERSE_SAFE_END;
00513    ast_mutex_unlock(&provlock);
00514 }
00515 
00516 int iax_provision_unload(void)
00517 {
00518    provinit = 0;
00519    ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00520    iax_provision_free_templates(0 /* Remove all templates. */);
00521 
00522    return 0;
00523 }
00524 
00525 int iax_provision_reload(int reload)
00526 {
00527    struct ast_config *cfg;
00528    struct iax_template *cur;
00529    char *cat;
00530    int found = 0;
00531    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00532    if (!provinit)
00533       iax_provision_init();
00534    
00535    cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
00536    if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
00537       /* Mark all as dead.  No need for locking */
00538       AST_LIST_TRAVERSE(&templates, cur, list) {
00539          cur->dead = 1;
00540       }
00541 
00542       /* Load as appropriate */
00543       cat = ast_category_browse(cfg, NULL);
00544       while(cat) {
00545          if (strcasecmp(cat, "general")) {
00546             iax_process_template(cfg, cat, found ? "default" : NULL);
00547             found++;
00548             ast_verb(3, "Loaded provisioning template '%s'\n", cat);
00549          }
00550          cat = ast_category_browse(cfg, cat);
00551       }
00552       ast_config_destroy(cfg);
00553    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00554       return 0;
00555    else
00556       ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
00557 
00558    iax_provision_free_templates(1 /* remove only marked as dead */);
00559 
00560    /* Purge cached signature DB entries */
00561    ast_db_deltree("iax/provisioning/cache", NULL);
00562    return 0;
00563 }

Generated on Sun May 20 06:33:54 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6