Mon Aug 31 12:30:08 2015

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 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 373342 $")
00033 
00034 #include <netdb.h>
00035 #include <netinet/in.h>
00036 #include <netinet/in_systm.h>
00037 #include <netinet/ip.h>
00038 #include <sys/socket.h>
00039 
00040 #include "asterisk/config.h"
00041 #include "asterisk/cli.h"
00042 #include "asterisk/lock.h"
00043 #include "asterisk/frame.h"
00044 #include "asterisk/md5.h"
00045 #include "asterisk/astdb.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/acl.h"
00048 #include "iax2.h"
00049 #include "iax2-provision.h"
00050 #include "iax2-parser.h"
00051 
00052 static int provinit = 0;
00053 
00054 struct iax_template {
00055    int dead;
00056    char name[80];
00057    char src[80];
00058    char user[20];
00059    char pass[20];
00060    char lang[10];
00061    unsigned short port;
00062    unsigned int server;
00063    unsigned short serverport;
00064    unsigned int altserver;
00065    unsigned int flags;
00066    unsigned int format;
00067    unsigned int tos;
00068    AST_LIST_ENTRY(iax_template) list;
00069 };
00070 
00071 static AST_LIST_HEAD_NOLOCK_STATIC(templates, iax_template);
00072 
00073 AST_MUTEX_DEFINE_STATIC(provlock);
00074 
00075 static struct iax_flag {
00076    char *name;
00077    int value;
00078 } iax_flags[] = {
00079    { "register", PROV_FLAG_REGISTER },
00080    { "secure", PROV_FLAG_SECURE },
00081    { "heartbeat", PROV_FLAG_HEARTBEAT },
00082    { "debug", PROV_FLAG_DEBUG },
00083    { "disablecid", PROV_FLAG_DIS_CALLERID },
00084    { "disablecw", PROV_FLAG_DIS_CALLWAIT },
00085    { "disablecidcw", PROV_FLAG_DIS_CIDCW },
00086    { "disable3way", PROV_FLAG_DIS_THREEWAY },
00087 };
00088 
00089 char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
00090 {
00091    int x;
00092 
00093    if (!buf || buflen < 1)
00094       return NULL;
00095    
00096    buf[0] = '\0';
00097 
00098    for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
00099       if (flags & iax_flags[x].value){
00100          strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
00101          strncat(buf, ",", buflen - strlen(buf) - 1);
00102       }
00103    }
00104 
00105    if (!ast_strlen_zero(buf)) 
00106       buf[strlen(buf) - 1] = '\0';
00107    else
00108       strncpy(buf, "none", buflen - 1);
00109 
00110    return buf;
00111 }
00112 
00113 static unsigned int iax_str2flags(const char *buf)
00114 {
00115    int x;
00116    int len;
00117    unsigned int flags = 0;
00118    char *e;
00119    while(buf && *buf) {
00120       e = strchr(buf, ',');
00121       if (e)
00122          len = e - buf;
00123       else
00124          len = 0;
00125       for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
00126          if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
00127              (!len && !strcasecmp(iax_flags[x].name, buf))) {
00128             flags |= iax_flags[x].value;
00129             break;
00130          }
00131       }
00132       if (e) {
00133          buf = e + 1;
00134          while(*buf && (*buf < 33))
00135             buf++;
00136       } else
00137          break;
00138    }
00139    return flags;
00140 }
00141 
00142 static void iax_template_copy(struct iax_template *dst, struct iax_template *src)
00143 {
00144    if (!dst || !src) {
00145       return;
00146    }
00147 
00148    dst->dead = src->dead;
00149    ast_copy_string(dst->name, src->name, sizeof(dst->name));
00150    ast_copy_string(dst->src, src->src, sizeof(dst->src));
00151    ast_copy_string(dst->user, src->user, sizeof(dst->user));
00152    ast_copy_string(dst->pass, src->pass, sizeof(dst->pass));
00153    ast_copy_string(dst->lang, src->lang, sizeof(dst->lang));
00154    dst->port = src->port;
00155    dst->server = src->server;
00156    dst->altserver = src->altserver;
00157    dst->flags = src->flags;
00158    dst->format = src->format;
00159    dst->tos = src->tos;
00160 }
00161 
00162 static struct iax_template *iax_template_find(const char *s, int allowdead)
00163 {
00164    struct iax_template *cur;
00165 
00166    AST_LIST_TRAVERSE(&templates, cur, list) {
00167       if (!strcasecmp(s, cur->name)) {
00168          if (!allowdead && cur->dead) {
00169             cur = NULL;
00170          }
00171          break;
00172       }
00173    }
00174 
00175    return cur;
00176 }
00177 
00178 char *iax_prov_complete_template(const char *line, const char *word, int pos, int state)
00179 {
00180    struct iax_template *c;
00181    int which=0;
00182    char *ret = NULL;
00183    int wordlen = strlen(word);
00184 
00185    if (pos == 3) {
00186       ast_mutex_lock(&provlock);
00187       AST_LIST_TRAVERSE(&templates, c, list) {
00188          if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
00189             ret = ast_strdup(c->name);
00190             break;
00191          }
00192       }
00193       ast_mutex_unlock(&provlock);
00194    }
00195    return ret;
00196 }
00197 
00198 static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
00199 {
00200    struct MD5Context md5;
00201    unsigned int tmp[4];
00202    MD5Init(&md5);
00203    MD5Update(&md5, provdata->buf, provdata->pos);
00204    MD5Final((unsigned char *)tmp, &md5);
00205    return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
00206 }
00207 
00208 int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
00209 {
00210    struct iax_template *cur;
00211    unsigned int sig;
00212    char tmp[40];
00213    memset(provdata, 0, sizeof(*provdata));
00214    ast_mutex_lock(&provlock);
00215    cur = iax_template_find(template, 1);
00216    /* If no match, try searching for '*' */
00217    if (!cur)
00218       cur = iax_template_find("*", 1);
00219    if (cur) {
00220       /* found it -- add information elements as appropriate */
00221       if (force || strlen(cur->user))
00222          iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
00223       if (force || strlen(cur->pass))
00224          iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
00225       if (force || strlen(cur->lang))
00226          iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
00227       if (force || cur->port)
00228          iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
00229       if (force || cur->server)
00230          iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
00231       if (force || cur->serverport)
00232          iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
00233       if (force || cur->altserver)
00234          iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
00235       if (force || cur->flags)
00236          iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
00237       if (force || cur->format)
00238          iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
00239       if (force || cur->tos)
00240          iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
00241       
00242       /* Calculate checksum of message so far */
00243       sig = prov_ver_calc(provdata);
00244       if (signature)
00245          *signature = sig;
00246       /* Store signature */
00247       iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
00248       /* Cache signature for later verification so we need not recalculate all this */
00249       snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
00250       ast_db_put("iax/provisioning/cache", template, tmp);
00251    } else
00252       ast_db_put("iax/provisioning/cache", template, "u");
00253    ast_mutex_unlock(&provlock);
00254    return cur ? 0 : -1;
00255 }
00256 
00257 int iax_provision_version(unsigned int *version, const char *template, int force)
00258 {
00259    char tmp[80] = "";
00260    struct iax_ie_data ied;
00261    int ret=0;
00262    memset(&ied, 0, sizeof(ied));
00263 
00264    ast_mutex_lock(&provlock);
00265    if (ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp))) {
00266       ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache/%s\n", template);
00267    }
00268    if (sscanf(tmp, "v%30x", version) != 1) {
00269       if (strcmp(tmp, "u")) {
00270          ret = iax_provision_build(&ied, version, template, force);
00271          if (ret)
00272             ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
00273       } else
00274          ret = -1;
00275    } else
00276       ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
00277    ast_mutex_unlock(&provlock);
00278    return ret;
00279 }
00280 
00281 static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
00282 {
00283    struct ast_variable *v;
00284    int foundportno = 0;
00285    int foundserverportno = 0;
00286    int x;
00287    struct in_addr ia;
00288    struct hostent *hp;
00289    struct ast_hostent h;
00290    struct iax_template *src, tmp;
00291    const char *t;
00292    if (def) {
00293       t = ast_variable_retrieve(cfg, s ,"template");
00294       src = NULL;
00295       if (t && strlen(t)) {
00296          src = iax_template_find(t, 0);
00297          if (!src)
00298             ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'.  Trying '%s'\n", t, s, def);
00299          else
00300             def = t;
00301       } 
00302       if (!src) {
00303          src = iax_template_find(def, 0);
00304          if (!src)
00305             ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
00306       }
00307       if (!src)
00308          return -1;
00309       ast_mutex_lock(&provlock);
00310       /* Backup old data */
00311       iax_template_copy(&tmp, cur);
00312       /* Restore from src */
00313       iax_template_copy(cur, src);
00314       /* Restore important headers */
00315       memcpy(cur->name, tmp.name, sizeof(cur->name));
00316       cur->dead = tmp.dead;
00317       ast_mutex_unlock(&provlock);
00318    }
00319    if (def)
00320       strncpy(cur->src, def, sizeof(cur->src) - 1);
00321    else
00322       cur->src[0] = '\0';
00323    v = ast_variable_browse(cfg, s);
00324    while(v) {
00325       if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
00326          if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
00327             if (!strcasecmp(v->name, "port")) {
00328                cur->port = x;
00329                foundportno = 1;
00330             } else {
00331                cur->serverport = x;
00332                foundserverportno = 1;
00333             }
00334          } else
00335             ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00336       } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
00337          hp = ast_gethostbyname(v->value, &h);
00338          if (hp) {
00339             memcpy(&ia, hp->h_addr, sizeof(ia));
00340             if (!strcasecmp(v->name, "server"))
00341                cur->server = ntohl(ia.s_addr);
00342             else
00343                cur->altserver = ntohl(ia.s_addr);
00344          } else 
00345             ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
00346       } else if (!strcasecmp(v->name, "codec")) {
00347          if ((x = ast_getformatbyname(v->value)) > 0) {
00348             cur->format = x;
00349          } else
00350             ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
00351       } else if (!strcasecmp(v->name, "tos")) {
00352          if (ast_str2tos(v->value, &cur->tos))
00353             ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
00354       } else if (!strcasecmp(v->name, "user")) {
00355          strncpy(cur->user, v->value, sizeof(cur->user) - 1);
00356          if (strcmp(cur->user, v->value))
00357             ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
00358       } else if (!strcasecmp(v->name, "pass")) {
00359          strncpy(cur->pass, v->value, sizeof(cur->pass) - 1);
00360          if (strcmp(cur->pass, v->value))
00361             ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
00362       } else if (!strcasecmp(v->name, "language")) {
00363          strncpy(cur->lang, v->value, sizeof(cur->lang) - 1);
00364          if (strcmp(cur->lang, v->value))
00365             ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
00366       } else if (!strcasecmp(v->name, "flags")) {
00367          cur->flags = iax_str2flags(v->value);
00368       } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
00369          cur->flags |= iax_str2flags(v->value);
00370       } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
00371          cur->flags &= ~iax_str2flags(v->value);
00372       } else if (strcasecmp(v->name, "template")) {
00373          ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
00374       }
00375       v = v->next;
00376    }
00377    if (!foundportno)
00378       cur->port = IAX_DEFAULT_PORTNO;
00379    if (!foundserverportno)
00380       cur->serverport = IAX_DEFAULT_PORTNO;
00381    return 0;
00382 }
00383 
00384 static int iax_process_template(struct ast_config *cfg, char *s, char *def)
00385 {
00386    /* Find an already existing one if there */
00387    struct iax_template *cur;
00388    int mallocd = 0;
00389 
00390    cur = iax_template_find(s, 1 /* allow dead */);
00391    if (!cur) {
00392       mallocd = 1;
00393       cur = ast_calloc(1, sizeof(*cur));
00394       if (!cur) {
00395          ast_log(LOG_WARNING, "Out of memory!\n");
00396          return -1;
00397       }
00398       /* Initialize entry */
00399       strncpy(cur->name, s, sizeof(cur->name) - 1);
00400       cur->dead = 1;
00401    }
00402    if (!iax_template_parse(cur, cfg, s, def))
00403       cur->dead = 0;
00404 
00405    /* Link if we're mallocd */
00406    if (mallocd) {
00407       ast_mutex_lock(&provlock);
00408       AST_LIST_INSERT_HEAD(&templates, cur, list);
00409       ast_mutex_unlock(&provlock);
00410    }
00411    return 0;
00412 }
00413 
00414 static const char *ifthere(const char *s)
00415 {
00416    if (strlen(s))
00417       return s;
00418    else
00419       return "<unspecified>";
00420 }
00421 
00422 static const char *iax_server(unsigned int addr)
00423 {
00424    struct in_addr ia;
00425    
00426    if (!addr)
00427       return "<unspecified>";
00428    
00429    ia.s_addr = htonl(addr);
00430 
00431    return ast_inet_ntoa(ia);
00432 }
00433 
00434 
00435 static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00436 {
00437    struct iax_template *cur;
00438    char server[INET_ADDRSTRLEN];
00439    char alternate[INET_ADDRSTRLEN];
00440    char flags[80];   /* Has to be big enough for 'flags' too */
00441    int found = 0;
00442 
00443    switch (cmd) {
00444    case CLI_INIT:
00445       e->command = "iax2 show provisioning";
00446       e->usage =
00447          "Usage: iax2 show provisioning [template]\n"
00448          "       Lists all known IAX provisioning templates or a\n"
00449          "       specific one if specified.\n";
00450       return NULL;
00451    case CLI_GENERATE:
00452       return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
00453    }
00454 
00455    if ((a->argc != 3) && (a->argc != 4))
00456       return CLI_SHOWUSAGE;
00457 
00458    ast_mutex_lock(&provlock);
00459    AST_LIST_TRAVERSE(&templates, cur, list) {
00460       if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name)))  {
00461          if (found) 
00462             ast_cli(a->fd, "\n");
00463          ast_copy_string(server, iax_server(cur->server), sizeof(server));
00464          ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
00465          ast_cli(a->fd, "== %s ==\n", cur->name);
00466          ast_cli(a->fd, "Base Templ:   %s\n", strlen(cur->src) ? cur->src : "<none>");
00467          ast_cli(a->fd, "Username:     %s\n", ifthere(cur->user));
00468          ast_cli(a->fd, "Secret:       %s\n", ifthere(cur->pass));
00469          ast_cli(a->fd, "Language:     %s\n", ifthere(cur->lang));
00470          ast_cli(a->fd, "Bind Port:    %d\n", cur->port);
00471          ast_cli(a->fd, "Server:       %s\n", server);
00472          ast_cli(a->fd, "Server Port:  %d\n", cur->serverport);
00473          ast_cli(a->fd, "Alternate:    %s\n", alternate);
00474          ast_cli(a->fd, "Flags:        %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
00475          ast_cli(a->fd, "Format:       %s\n", ast_getformatname(cur->format));
00476          ast_cli(a->fd, "TOS:          0x%x\n", cur->tos);
00477          found++;
00478       }
00479    }
00480    ast_mutex_unlock(&provlock);
00481    if (!found) {
00482       if (a->argc == 3)
00483          ast_cli(a->fd, "No provisioning templates found\n");
00484       else
00485          ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
00486    }
00487    return CLI_SUCCESS;
00488 }
00489 
00490 static struct ast_cli_entry cli_iax2_provision[] = {
00491    AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
00492 };
00493 
00494 static int iax_provision_init(void)
00495 {
00496    ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00497    provinit = 1;
00498    return 0;
00499 }
00500 
00501 static void iax_provision_free_templates(int dead)
00502 {
00503    struct iax_template *cur;
00504 
00505    /* Drop dead or not (depending on dead) entries while locked */
00506    ast_mutex_lock(&provlock);
00507    AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
00508       if ((dead && cur->dead) || !dead) {
00509          AST_LIST_REMOVE_CURRENT(list);
00510          ast_free(cur);
00511       }
00512    }
00513    AST_LIST_TRAVERSE_SAFE_END;
00514    ast_mutex_unlock(&provlock);
00515 }
00516 
00517 int iax_provision_unload(void)
00518 {
00519    provinit = 0;
00520    ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
00521    iax_provision_free_templates(0 /* Remove all templates. */);
00522 
00523    return 0;
00524 }
00525 
00526 int iax_provision_reload(int reload)
00527 {
00528    struct ast_config *cfg;
00529    struct iax_template *cur;
00530    char *cat;
00531    int found = 0;
00532    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00533    if (!provinit)
00534       iax_provision_init();
00535    
00536    cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
00537    if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
00538       /* Mark all as dead.  No need for locking */
00539       AST_LIST_TRAVERSE(&templates, cur, list) {
00540          cur->dead = 1;
00541       }
00542 
00543       /* Load as appropriate */
00544       cat = ast_category_browse(cfg, NULL);
00545       while(cat) {
00546          if (strcasecmp(cat, "general")) {
00547             iax_process_template(cfg, cat, found ? "default" : NULL);
00548             found++;
00549             ast_verb(3, "Loaded provisioning template '%s'\n", cat);
00550          }
00551          cat = ast_category_browse(cfg, cat);
00552       }
00553       ast_config_destroy(cfg);
00554    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00555       return 0;
00556    else
00557       ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
00558 
00559    iax_provision_free_templates(1 /* remove only marked as dead */);
00560 
00561    /* Purge cached signature DB entries */
00562    ast_db_deltree("iax/provisioning/cache", NULL);
00563    return 0;
00564 }

Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1