33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <sys/socket.h>
53 static int provinit = 0;
64 unsigned short serverport;
65 unsigned int altserver;
74 AST_MUTEX_DEFINE_STATIC(provlock);
80 {
"register", PROV_FLAG_REGISTER },
81 {
"secure", PROV_FLAG_SECURE },
82 {
"heartbeat", PROV_FLAG_HEARTBEAT },
83 {
"debug", PROV_FLAG_DEBUG },
84 {
"disablecid", PROV_FLAG_DIS_CALLERID },
85 {
"disablecw", PROV_FLAG_DIS_CALLWAIT },
86 {
"disablecidcw", PROV_FLAG_DIS_CIDCW },
87 {
"disable3way", PROV_FLAG_DIS_THREEWAY },
90 char *iax_provflags2str(
char *buf,
int buflen,
unsigned int flags)
94 if (!buf || buflen < 1)
99 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
100 if (flags & iax_flags[x].value){
101 strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
102 strncat(buf,
",", buflen - strlen(buf) - 1);
106 if (!ast_strlen_zero(buf))
107 buf[strlen(buf) - 1] =
'\0';
109 strncpy(buf,
"none", buflen - 1);
114 static unsigned int iax_str2flags(
const char *buf)
118 unsigned int flags = 0;
121 e = strchr(buf,
',');
126 for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
127 if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
128 (!len && !strcasecmp(iax_flags[x].name, buf))) {
129 flags |= iax_flags[x].value;
135 while(*buf && (*buf < 33))
149 dst->dead = src->dead;
155 dst->port = src->port;
156 dst->server = src->server;
157 dst->altserver = src->altserver;
158 dst->flags = src->flags;
159 dst->format = src->format;
163 static struct iax_template *iax_template_find(
const char *s,
int allowdead)
168 if (!strcasecmp(s, cur->name)) {
169 if (!allowdead && cur->dead) {
179 char *iax_prov_complete_template(
const char *line,
const char *word,
int pos,
int state)
184 int wordlen = strlen(word);
187 ast_mutex_lock(&provlock);
189 if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
194 ast_mutex_unlock(&provlock);
199 static unsigned int prov_ver_calc(
struct iax_ie_data *provdata)
204 MD5Update(&md5, provdata->buf, provdata->pos);
205 MD5Final((
unsigned char *)tmp, &md5);
206 return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
209 int iax_provision_build(
struct iax_ie_data *provdata,
unsigned int *signature,
const char *
template,
int force)
214 memset(provdata, 0,
sizeof(*provdata));
215 ast_mutex_lock(&provlock);
216 cur = iax_template_find(
template, 1);
219 cur = iax_template_find(
"*", 1);
222 if (force || strlen(cur->user))
223 iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
224 if (force || strlen(cur->pass))
225 iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
226 if (force || strlen(cur->lang))
227 iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
228 if (force || cur->port)
229 iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
230 if (force || cur->server)
231 iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
232 if (force || cur->serverport)
233 iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
234 if (force || cur->altserver)
235 iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
236 if (force || cur->flags)
237 iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
238 if (force || cur->format)
239 iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
240 if (force || cur->tos)
241 iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
244 sig = prov_ver_calc(provdata);
248 iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
250 snprintf(tmp,
sizeof(tmp),
"v0x%08x", sig);
251 ast_db_put(
"iax/provisioning/cache",
template, tmp);
253 ast_db_put(
"iax/provisioning/cache",
template,
"u");
254 ast_mutex_unlock(&provlock);
258 int iax_provision_version(
unsigned int *version,
const char *
template,
int force)
263 memset(&ied, 0,
sizeof(ied));
265 ast_mutex_lock(&provlock);
266 if (
ast_db_get(
"iax/provisioning/cache",
template, tmp,
sizeof(tmp))) {
267 ast_log(LOG_ERROR,
"ast_db_get failed to retrieve iax/provisioning/cache/%s\n",
template);
269 if (sscanf(tmp,
"v%30x", version) != 1) {
270 if (strcmp(tmp,
"u")) {
271 ret = iax_provision_build(&ied, version,
template, force);
273 ast_debug(1,
"Unable to create provisioning packet for '%s'\n",
template);
277 ast_debug(1,
"Retrieved cached version '%s' = '%08x'\n", tmp, *version);
278 ast_mutex_unlock(&provlock);
282 static int iax_template_parse(
struct iax_template *cur,
struct ast_config *cfg,
const char *s,
const char *def)
286 int foundserverportno = 0;
291 t = ast_variable_retrieve(cfg, s ,
"template");
293 if (t && strlen(t)) {
294 src = iax_template_find(t, 0);
296 ast_log(LOG_WARNING,
"Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
301 src = iax_template_find(def, 0);
303 ast_log(LOG_WARNING,
"Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
307 ast_mutex_lock(&provlock);
309 iax_template_copy(&tmp, cur);
311 iax_template_copy(cur, src);
313 memcpy(cur->name, tmp.name,
sizeof(cur->name));
314 cur->dead = tmp.dead;
315 ast_mutex_unlock(&provlock);
321 v = ast_variable_browse(cfg, s);
323 if (!strcasecmp(v->
name,
"port") || !strcasecmp(v->
name,
"serverport")) {
324 if ((sscanf(v->
value,
"%5d", &x) == 1) && (x > 0) && (x < 65535)) {
325 if (!strcasecmp(v->
name,
"port")) {
330 foundserverportno = 1;
333 ast_log(LOG_WARNING,
"Ignoring invalid %s '%s' for '%s' at line %d\n", v->
name, v->
value, s, v->lineno);
334 }
else if (!strcasecmp(v->
name,
"server") || !strcasecmp(v->
name,
"altserver")) {
337 ast_log(LOG_WARNING,
"Ignoring invalid %s '%s' for '%s' at line %d\n", v->
name, v->
value, s, v->lineno);
339 if (!strcasecmp(v->
name,
"server"))
344 }
else if (!strcasecmp(v->
name,
"codec")) {
350 ast_log(LOG_WARNING,
"Ignoring invalid codec '%s' for '%s' at line %d\n", v->
value, s, v->lineno);
351 }
else if (!strcasecmp(v->
name,
"tos")) {
353 ast_log(LOG_WARNING,
"Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
354 }
else if (!strcasecmp(v->
name,
"user")) {
356 if (strcmp(cur->user, v->
value))
357 ast_log(LOG_WARNING,
"Truncating username from '%s' to '%s' for '%s' at line %d\n", v->
value, cur->user, s, v->lineno);
358 }
else if (!strcasecmp(v->
name,
"pass")) {
360 if (strcmp(cur->pass, v->
value))
361 ast_log(LOG_WARNING,
"Truncating password from '%s' to '%s' for '%s' at line %d\n", v->
value, cur->pass, s, v->lineno);
362 }
else if (!strcasecmp(v->
name,
"language")) {
364 if (strcmp(cur->lang, v->
value))
365 ast_log(LOG_WARNING,
"Truncating language from '%s' to '%s' for '%s' at line %d\n", v->
value, cur->lang, s, v->lineno);
366 }
else if (!strcasecmp(v->
name,
"flags")) {
367 cur->flags = iax_str2flags(v->
value);
368 }
else if (!strncasecmp(v->
name,
"flags", 5) && strchr(v->
name,
'+')) {
369 cur->flags |= iax_str2flags(v->
value);
370 }
else if (!strncasecmp(v->
name,
"flags", 5) && strchr(v->
name,
'-')) {
371 cur->flags &= ~iax_str2flags(v->
value);
372 }
else if (strcasecmp(v->
name,
"template")) {
373 ast_log(LOG_WARNING,
"Unknown keyword '%s' in definition of '%s' at line %d\n", v->
name, s, v->lineno);
378 cur->port = IAX_DEFAULT_PORTNO;
379 if (!foundserverportno)
380 cur->serverport = IAX_DEFAULT_PORTNO;
384 static int iax_process_template(
struct ast_config *cfg,
char *s,
char *def)
390 cur = iax_template_find(s, 1 );
395 ast_log(LOG_WARNING,
"Out of memory!\n");
402 if (!iax_template_parse(cur, cfg, s, def))
407 ast_mutex_lock(&provlock);
409 ast_mutex_unlock(&provlock);
414 static const char *ifthere(
const char *s)
419 return "<unspecified>";
422 static const char *iax_server(
unsigned int addr)
427 return "<unspecified>";
429 ia.s_addr = htonl(addr);
438 char server[INET_ADDRSTRLEN];
439 char alternate[INET_ADDRSTRLEN];
445 e->
command =
"iax2 show provisioning";
447 "Usage: iax2 show provisioning [template]\n"
448 " Lists all known IAX provisioning templates or a\n"
449 " specific one if specified.\n";
452 return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
455 if ((a->argc != 3) && (a->argc != 4))
456 return CLI_SHOWUSAGE;
458 ast_mutex_lock(&provlock);
460 if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
462 ast_cli(a->fd,
"\n");
464 ast_copy_string(alternate, iax_server(cur->altserver),
sizeof(alternate));
465 ast_cli(a->fd,
"== %s ==\n", cur->name);
466 ast_cli(a->fd,
"Base Templ: %s\n", strlen(cur->src) ? cur->src :
"<none>");
467 ast_cli(a->fd,
"Username: %s\n", ifthere(cur->user));
468 ast_cli(a->fd,
"Secret: %s\n", ifthere(cur->pass));
469 ast_cli(a->fd,
"Language: %s\n", ifthere(cur->lang));
470 ast_cli(a->fd,
"Bind Port: %d\n", cur->port);
471 ast_cli(a->fd,
"Server: %s\n", server);
472 ast_cli(a->fd,
"Server Port: %d\n", cur->serverport);
473 ast_cli(a->fd,
"Alternate: %s\n", alternate);
474 ast_cli(a->fd,
"Flags: %s\n", iax_provflags2str(flags,
sizeof(flags), cur->flags));
476 ast_cli(a->fd,
"TOS: 0x%x\n", cur->tos);
480 ast_mutex_unlock(&provlock);
483 ast_cli(a->fd,
"No provisioning templates found\n");
485 ast_cli(a->fd,
"No provisioning template matching '%s' found\n", a->argv[3]);
491 AST_CLI_DEFINE(iax_show_provisioning,
"Display iax provisioning"),
494 static int iax_provision_init(
void)
501 static void iax_provision_free_templates(
int dead)
506 ast_mutex_lock(&provlock);
508 if ((dead && cur->dead) || !dead) {
514 ast_mutex_unlock(&provlock);
517 int iax_provision_unload(
void)
521 iax_provision_free_templates(0 );
526 int iax_provision_reload(
int reload)
534 iax_provision_init();
537 if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
546 if (strcasecmp(cat,
"general")) {
547 iax_process_template(cfg, cat, found ?
"default" : NULL);
549 ast_verb(3,
"Loaded provisioning template '%s'\n", cat);
554 }
else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
557 ast_log(LOG_NOTICE,
"No IAX provisioning configuration found, IAX provisioning disabled.\n");
559 iax_provision_free_templates(1 );
struct ast_variable * next
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
const char * iax2_getformatname(iax2_format format)
iax2 wrapper function for ast_getformatname
descriptor for a cli entry.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Structure for variables, used for configurations and for channel variables.
uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
Get an IPv4 address of an ast_sockaddr.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
#define ast_strdup(str)
A wrapper for strdup()
int ast_str2tos(const char *value, unsigned int *tos)
Convert a string to the appropriate TOS value.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Socket address structure.
Implementation of the IAX2 protocol.
Configuration File Parser.
Access Control of various sorts.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Asterisk internal frame definitions.
int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr, const char *name, int flag, int family)
Return the first entry from ast_sockaddr_resolve filtered by address family.
#define ast_debug(level,...)
Log a DEBUG message.
IAX2 Provisioning protocol.
#define AST_LIST_HEAD_NOLOCK_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Implementation of Inter-Asterisk eXchange, version 2 parser.c parser.h chan_iax2.c.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
#define ast_calloc(num, len)
A wrapper for calloc()
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
structure to hold users read from users.conf
Structure used to handle boolean flags.
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Persistent data storage (akin to *doze registry)
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.