#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include "asterisk.h"
#include "asterisk/config.h"
#include "asterisk/logger.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/options.h"
#include "asterisk/md5.h"
#include "asterisk/astdb.h"
#include "asterisk/utils.h"
#include "iax2.h"
#include "iax2-provision.h"
#include "iax2-parser.h"
Go to the source code of this file.
Data Structures | |
struct | iax_flag |
struct | iax_template |
Defines | |
#define | IPTOS_MINCOST 0x02 |
Functions | |
AST_MUTEX_DEFINE_STATIC (provlock) | |
static int | iax_process_template (struct ast_config *cfg, char *s, char *def) |
char * | iax_prov_complete_template (char *line, char *word, int pos, int state) |
char * | iax_provflags2str (char *buf, int buflen, unsigned int flags) |
int | iax_provision_build (struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force) |
static int | iax_provision_init (void) |
int | iax_provision_reload (void) |
int | iax_provision_unload (void) |
int | iax_provision_version (unsigned int *version, const char *template, int force) |
static const char * | iax_server (char *a, int alen, unsigned int addr) |
static int | iax_show_provisioning (int fd, int argc, char *argv[]) |
static unsigned int | iax_str2flags (const char *buf) |
static struct iax_template * | iax_template_find (const char *s, int allowdead) |
static int | iax_template_parse (struct iax_template *cur, struct ast_config *cfg, char *s, char *def) |
static const char * | ifthere (const char *s) |
static unsigned int | prov_ver_calc (struct iax_ie_data *provdata) |
Variables | |
static struct ast_cli_entry | cli_show_provisioning |
static struct iax_flag | iax_flags [] |
static int | provinit = 0 |
static char | show_provisioning_usage [] |
iax_template * | templates |
Definition in file iax2-provision.c.
|
Definition at line 52 of file iax2-provision.c. |
|
|
|
Definition at line 374 of file iax2-provision.c. References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), cfg, iax_template::dead, iax_template_parse(), LOG_WARNING, malloc, iax_template::name, iax_template::next, and templates. Referenced by iax_provision_reload(). 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 }
|
|
Definition at line 155 of file iax2-provision.c. References ast_mutex_lock(), ast_mutex_unlock(), iax_template::name, iax_template::next, strdup, and templates. Referenced by iax2_prov_complete_template_3rd(). 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 }
|
|
Definition at line 88 of file iax2-provision.c. References iax_flags, and name. Referenced by dump_prov_flags(), and iax_show_provisioning(). 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 }
|
|
Definition at line 187 of file iax2-provision.c. References iax_template::altserver, ast_db_put(), ast_mutex_lock(), ast_mutex_unlock(), iax_template::flags, iax_template::format, iax_ie_append_byte(), iax_ie_append_int(), iax_ie_append_short(), iax_ie_append_str(), iax_template_find(), iax_template::lang, iax_template::pass, iax_template::port, PROV_IE_ALTSERVER, PROV_IE_FLAGS, PROV_IE_FORMAT, PROV_IE_LANG, PROV_IE_PASS, PROV_IE_PORTNO, PROV_IE_PROVVER, PROV_IE_SERVERIP, PROV_IE_SERVERPORT, PROV_IE_TOS, PROV_IE_USER, prov_ver_calc(), iax_template::server, iax_template::serverport, iax_template::tos, and iax_template::user. Referenced by iax2_provision(), and iax_provision_version(). 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 }
|
|
Definition at line 472 of file iax2-provision.c. References ast_cli_register(), cli_show_provisioning, and provinit. Referenced by iax_provision_reload(). 00473 { 00474 ast_cli_register(&cli_show_provisioning); 00475 provinit = 1; 00476 return 0; 00477 }
|
|
Definition at line 486 of file iax2-provision.c. References ast_category_browse(), ast_config_destroy(), ast_config_load(), ast_db_deltree(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_verbose(), cfg, iax_template::dead, free, iax_process_template(), iax_provision_init(), LOG_NOTICE, iax_template::next, option_verbose, provinit, templates, and VERBOSE_PREFIX_3. Referenced by load_module(). 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 }
|
|
Definition at line 479 of file iax2-provision.c. References ast_cli_unregister(), cli_show_provisioning, and provinit. Referenced by __unload_module(). 00480 { 00481 provinit = 0; 00482 ast_cli_unregister(&cli_show_provisioning); 00483 return 0; 00484 }
|
|
Definition at line 236 of file iax2-provision.c. References ast_db_get(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax_provision_build(), LOG_DEBUG, and option_debug. Referenced by check_provisioning(). 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%x", 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 }
|
|
Definition at line 423 of file iax2-provision.c. References ast_inet_ntoa(). Referenced by iax_show_provisioning(). 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 }
|
|
Definition at line 433 of file iax2-provision.c. References iax_template::altserver, ast_cli(), ast_getformatname(), ast_mutex_lock(), iax_template::flags, iax_template::format, iax_provflags2str(), iax_server(), ifthere(), iax_template::lang, iax_template::name, iax_template::next, iax_template::pass, iax_template::port, RESULT_SHOWUSAGE, iax_template::server, iax_template::serverport, iax_template::src, templates, iax_template::tos, and iax_template::user. 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 }
|
|
Definition at line 108 of file iax2-provision.c. References iax_flags, and name. Referenced by iax_template_parse(). 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 }
|
|
Definition at line 140 of file iax2-provision.c. References iax_template::dead, iax_template::name, iax_template::next, and templates. Referenced by iax_provision_build(), and iax_template_parse(). 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 }
|
|
Definition at line 258 of file iax2-provision.c. References iax_template::altserver, ast_getformatbyname(), ast_gethostbyname(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_variable_browse(), ast_variable_retrieve(), cfg, iax_template::dead, iax_template::flags, iax_template::format, hp, IAX_DEFAULT_PORTNO, iax_str2flags(), iax_template_find(), IPTOS_MINCOST, iax_template::lang, ast_variable::lineno, LOG_WARNING, ast_variable::name, iax_template::name, ast_variable::next, iax_template::next, iax_template::pass, iax_template::port, iax_template::server, iax_template::serverport, iax_template::src, t, iax_template::tos, iax_template::user, and ast_variable::value. Referenced by iax_process_template(). 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, "%d", &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, "%d", &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 }
|
|
Definition at line 415 of file iax2-provision.c. Referenced by iax_show_provisioning(). 00416 { 00417 if (strlen(s)) 00418 return s; 00419 else 00420 return "<unspecified>"; 00421 }
|
|
Definition at line 177 of file iax2-provision.c. References iax_ie_data::buf, MD5Final(), MD5Init(), MD5Update(), and iax_ie_data::pos. Referenced by iax_provision_build(). 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 }
|
|
Initial value: { { "iax2", "show", "provisioning", NULL }, iax_show_provisioning, "Show iax provisioning", show_provisioning_usage, iax_prov_complete_template } Definition at line 469 of file iax2-provision.c. Referenced by iax_provision_init(), and iax_provision_unload(). |
|
Referenced by iax_provflags2str(), and iax_str2flags(). |
|
Definition at line 55 of file iax2-provision.c. Referenced by iax_provision_init(), iax_provision_reload(), and iax_provision_unload(). |
|
Initial value: "Usage: iax show provisioning [template]\n" " Lists all known IAX provisioning templates or a\n" " specific one if specified.\n" Definition at line 410 of file iax2-provision.c. |
|
Referenced by iax_process_template(), iax_prov_complete_template(), iax_provision_reload(), iax_show_provisioning(), and iax_template_find(). |