iax2-provision.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief IAX Provisioning Protocol
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <netdb.h>
  30. #include <netinet/in.h>
  31. #include <netinet/in_systm.h>
  32. #include <netinet/ip.h>
  33. #include <sys/socket.h>
  34. #include "asterisk/config.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/frame.h"
  38. #include "asterisk/md5.h"
  39. #include "asterisk/astdb.h"
  40. #include "asterisk/utils.h"
  41. #include "asterisk/acl.h"
  42. #include "iax2.h"
  43. #include "iax2-provision.h"
  44. #include "iax2-parser.h"
  45. static int provinit = 0;
  46. struct iax_template {
  47. int dead;
  48. char name[80];
  49. char src[80];
  50. char user[20];
  51. char pass[20];
  52. char lang[10];
  53. unsigned short port;
  54. unsigned int server;
  55. unsigned short serverport;
  56. unsigned int altserver;
  57. unsigned int flags;
  58. iax2_format format;
  59. unsigned int tos;
  60. AST_LIST_ENTRY(iax_template) list;
  61. };
  62. static AST_LIST_HEAD_NOLOCK_STATIC(templates, iax_template);
  63. AST_MUTEX_DEFINE_STATIC(provlock);
  64. static struct iax_flag {
  65. char *name;
  66. int value;
  67. } iax_flags[] = {
  68. { "register", PROV_FLAG_REGISTER },
  69. { "secure", PROV_FLAG_SECURE },
  70. { "heartbeat", PROV_FLAG_HEARTBEAT },
  71. { "debug", PROV_FLAG_DEBUG },
  72. { "disablecid", PROV_FLAG_DIS_CALLERID },
  73. { "disablecw", PROV_FLAG_DIS_CALLWAIT },
  74. { "disablecidcw", PROV_FLAG_DIS_CIDCW },
  75. { "disable3way", PROV_FLAG_DIS_THREEWAY },
  76. };
  77. char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
  78. {
  79. int x;
  80. if (!buf || buflen < 1)
  81. return NULL;
  82. buf[0] = '\0';
  83. for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
  84. if (flags & iax_flags[x].value){
  85. strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
  86. strncat(buf, ",", buflen - strlen(buf) - 1);
  87. }
  88. }
  89. if (!ast_strlen_zero(buf))
  90. buf[strlen(buf) - 1] = '\0';
  91. else
  92. strncpy(buf, "none", buflen - 1);
  93. return buf;
  94. }
  95. static unsigned int iax_str2flags(const char *buf)
  96. {
  97. int x;
  98. int len;
  99. unsigned int flags = 0;
  100. char *e;
  101. while(buf && *buf) {
  102. e = strchr(buf, ',');
  103. if (e)
  104. len = e - buf;
  105. else
  106. len = 0;
  107. for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
  108. if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
  109. (!len && !strcasecmp(iax_flags[x].name, buf))) {
  110. flags |= iax_flags[x].value;
  111. break;
  112. }
  113. }
  114. if (e) {
  115. buf = e + 1;
  116. while(*buf && (*buf < 33))
  117. buf++;
  118. } else
  119. break;
  120. }
  121. return flags;
  122. }
  123. static void iax_template_copy(struct iax_template *dst, struct iax_template *src)
  124. {
  125. if (!dst || !src) {
  126. return;
  127. }
  128. dst->dead = src->dead;
  129. ast_copy_string(dst->name, src->name, sizeof(dst->name));
  130. ast_copy_string(dst->src, src->src, sizeof(dst->src));
  131. ast_copy_string(dst->user, src->user, sizeof(dst->user));
  132. ast_copy_string(dst->pass, src->pass, sizeof(dst->pass));
  133. ast_copy_string(dst->lang, src->lang, sizeof(dst->lang));
  134. dst->port = src->port;
  135. dst->server = src->server;
  136. dst->altserver = src->altserver;
  137. dst->flags = src->flags;
  138. dst->format = src->format;
  139. dst->tos = src->tos;
  140. }
  141. static struct iax_template *iax_template_find(const char *s, int allowdead)
  142. {
  143. struct iax_template *cur;
  144. AST_LIST_TRAVERSE(&templates, cur, list) {
  145. if (!strcasecmp(s, cur->name)) {
  146. if (!allowdead && cur->dead) {
  147. cur = NULL;
  148. }
  149. break;
  150. }
  151. }
  152. return cur;
  153. }
  154. char *iax_prov_complete_template(const char *line, const char *word, int pos, int state)
  155. {
  156. struct iax_template *c;
  157. int which=0;
  158. char *ret = NULL;
  159. int wordlen = strlen(word);
  160. if (pos == 3) {
  161. ast_mutex_lock(&provlock);
  162. AST_LIST_TRAVERSE(&templates, c, list) {
  163. if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
  164. ret = ast_strdup(c->name);
  165. break;
  166. }
  167. }
  168. ast_mutex_unlock(&provlock);
  169. }
  170. return ret;
  171. }
  172. static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
  173. {
  174. struct MD5Context md5;
  175. unsigned int tmp[4];
  176. MD5Init(&md5);
  177. MD5Update(&md5, provdata->buf, provdata->pos);
  178. MD5Final((unsigned char *)tmp, &md5);
  179. return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
  180. }
  181. int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
  182. {
  183. struct iax_template *cur;
  184. unsigned int sig;
  185. char tmp[40];
  186. memset(provdata, 0, sizeof(*provdata));
  187. ast_mutex_lock(&provlock);
  188. cur = iax_template_find(template, 1);
  189. /* If no match, try searching for '*' */
  190. if (!cur)
  191. cur = iax_template_find("*", 1);
  192. if (cur) {
  193. /* found it -- add information elements as appropriate */
  194. if (force || strlen(cur->user))
  195. iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
  196. if (force || strlen(cur->pass))
  197. iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
  198. if (force || strlen(cur->lang))
  199. iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
  200. if (force || cur->port)
  201. iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
  202. if (force || cur->server)
  203. iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
  204. if (force || cur->serverport)
  205. iax_ie_append_short(provdata, PROV_IE_SERVERPORT, cur->serverport);
  206. if (force || cur->altserver)
  207. iax_ie_append_int(provdata, PROV_IE_ALTSERVER, cur->altserver);
  208. if (force || cur->flags)
  209. iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
  210. if (force || cur->format)
  211. iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
  212. if (force || cur->tos)
  213. iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
  214. /* Calculate checksum of message so far */
  215. sig = prov_ver_calc(provdata);
  216. if (signature)
  217. *signature = sig;
  218. /* Store signature */
  219. iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
  220. /* Cache signature for later verification so we need not recalculate all this */
  221. snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
  222. ast_db_put("iax/provisioning/cache", template, tmp);
  223. } else
  224. ast_db_put("iax/provisioning/cache", template, "u");
  225. ast_mutex_unlock(&provlock);
  226. return cur ? 0 : -1;
  227. }
  228. int iax_provision_version(unsigned int *version, const char *template, int force)
  229. {
  230. char tmp[80] = "";
  231. struct iax_ie_data ied;
  232. int ret=0;
  233. memset(&ied, 0, sizeof(ied));
  234. ast_mutex_lock(&provlock);
  235. if (ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp))) {
  236. ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache/%s\n", template);
  237. }
  238. if (sscanf(tmp, "v%30x", version) != 1) {
  239. if (strcmp(tmp, "u")) {
  240. ret = iax_provision_build(&ied, version, template, force);
  241. if (ret)
  242. ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
  243. } else
  244. ret = -1;
  245. } else
  246. ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
  247. ast_mutex_unlock(&provlock);
  248. return ret;
  249. }
  250. static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
  251. {
  252. struct ast_variable *v;
  253. int foundportno = 0;
  254. int foundserverportno = 0;
  255. int x;
  256. struct in_addr ia;
  257. struct hostent *hp;
  258. struct ast_hostent h;
  259. struct iax_template *src, tmp;
  260. const char *t;
  261. if (def) {
  262. t = ast_variable_retrieve(cfg, s ,"template");
  263. src = NULL;
  264. if (t && strlen(t)) {
  265. src = iax_template_find(t, 0);
  266. if (!src)
  267. ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
  268. else
  269. def = t;
  270. }
  271. if (!src) {
  272. src = iax_template_find(def, 0);
  273. if (!src)
  274. ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
  275. }
  276. if (!src)
  277. return -1;
  278. ast_mutex_lock(&provlock);
  279. /* Backup old data */
  280. iax_template_copy(&tmp, cur);
  281. /* Restore from src */
  282. iax_template_copy(cur, src);
  283. /* Restore important headers */
  284. memcpy(cur->name, tmp.name, sizeof(cur->name));
  285. cur->dead = tmp.dead;
  286. ast_mutex_unlock(&provlock);
  287. }
  288. if (def)
  289. strncpy(cur->src, def, sizeof(cur->src) - 1);
  290. else
  291. cur->src[0] = '\0';
  292. v = ast_variable_browse(cfg, s);
  293. while(v) {
  294. if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
  295. if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
  296. if (!strcasecmp(v->name, "port")) {
  297. cur->port = x;
  298. foundportno = 1;
  299. } else {
  300. cur->serverport = x;
  301. foundserverportno = 1;
  302. }
  303. } else
  304. ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
  305. } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
  306. hp = ast_gethostbyname(v->value, &h);
  307. if (hp) {
  308. memcpy(&ia, hp->h_addr, sizeof(ia));
  309. if (!strcasecmp(v->name, "server"))
  310. cur->server = ntohl(ia.s_addr);
  311. else
  312. cur->altserver = ntohl(ia.s_addr);
  313. } else
  314. ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
  315. } else if (!strcasecmp(v->name, "codec")) {
  316. struct ast_format tmpfmt;
  317. if ((ast_getformatbyname(v->value, &tmpfmt)) > 0) {
  318. cur->format = ast_format_to_old_bitfield(&tmpfmt);
  319. } else
  320. ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
  321. } else if (!strcasecmp(v->name, "tos")) {
  322. if (ast_str2tos(v->value, &cur->tos))
  323. ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
  324. } else if (!strcasecmp(v->name, "user")) {
  325. strncpy(cur->user, v->value, sizeof(cur->user) - 1);
  326. if (strcmp(cur->user, v->value))
  327. ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
  328. } else if (!strcasecmp(v->name, "pass")) {
  329. strncpy(cur->pass, v->value, sizeof(cur->pass) - 1);
  330. if (strcmp(cur->pass, v->value))
  331. ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
  332. } else if (!strcasecmp(v->name, "language")) {
  333. strncpy(cur->lang, v->value, sizeof(cur->lang) - 1);
  334. if (strcmp(cur->lang, v->value))
  335. ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
  336. } else if (!strcasecmp(v->name, "flags")) {
  337. cur->flags = iax_str2flags(v->value);
  338. } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
  339. cur->flags |= iax_str2flags(v->value);
  340. } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
  341. cur->flags &= ~iax_str2flags(v->value);
  342. } else if (strcasecmp(v->name, "template")) {
  343. ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
  344. }
  345. v = v->next;
  346. }
  347. if (!foundportno)
  348. cur->port = IAX_DEFAULT_PORTNO;
  349. if (!foundserverportno)
  350. cur->serverport = IAX_DEFAULT_PORTNO;
  351. return 0;
  352. }
  353. static int iax_process_template(struct ast_config *cfg, char *s, char *def)
  354. {
  355. /* Find an already existing one if there */
  356. struct iax_template *cur;
  357. int mallocd = 0;
  358. cur = iax_template_find(s, 1 /* allow dead */);
  359. if (!cur) {
  360. mallocd = 1;
  361. cur = ast_calloc(1, sizeof(*cur));
  362. if (!cur) {
  363. ast_log(LOG_WARNING, "Out of memory!\n");
  364. return -1;
  365. }
  366. /* Initialize entry */
  367. strncpy(cur->name, s, sizeof(cur->name) - 1);
  368. cur->dead = 1;
  369. }
  370. if (!iax_template_parse(cur, cfg, s, def))
  371. cur->dead = 0;
  372. /* Link if we're mallocd */
  373. if (mallocd) {
  374. ast_mutex_lock(&provlock);
  375. AST_LIST_INSERT_HEAD(&templates, cur, list);
  376. ast_mutex_unlock(&provlock);
  377. }
  378. return 0;
  379. }
  380. static const char *ifthere(const char *s)
  381. {
  382. if (strlen(s))
  383. return s;
  384. else
  385. return "<unspecified>";
  386. }
  387. static const char *iax_server(unsigned int addr)
  388. {
  389. struct in_addr ia;
  390. if (!addr)
  391. return "<unspecified>";
  392. ia.s_addr = htonl(addr);
  393. return ast_inet_ntoa(ia);
  394. }
  395. static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  396. {
  397. struct iax_template *cur;
  398. char server[INET_ADDRSTRLEN];
  399. char alternate[INET_ADDRSTRLEN];
  400. char flags[80]; /* Has to be big enough for 'flags' too */
  401. int found = 0;
  402. switch (cmd) {
  403. case CLI_INIT:
  404. e->command = "iax2 show provisioning";
  405. e->usage =
  406. "Usage: iax2 show provisioning [template]\n"
  407. " Lists all known IAX provisioning templates or a\n"
  408. " specific one if specified.\n";
  409. return NULL;
  410. case CLI_GENERATE:
  411. return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
  412. }
  413. if ((a->argc != 3) && (a->argc != 4))
  414. return CLI_SHOWUSAGE;
  415. ast_mutex_lock(&provlock);
  416. AST_LIST_TRAVERSE(&templates, cur, list) {
  417. if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
  418. if (found)
  419. ast_cli(a->fd, "\n");
  420. ast_copy_string(server, iax_server(cur->server), sizeof(server));
  421. ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
  422. ast_cli(a->fd, "== %s ==\n", cur->name);
  423. ast_cli(a->fd, "Base Templ: %s\n", strlen(cur->src) ? cur->src : "<none>");
  424. ast_cli(a->fd, "Username: %s\n", ifthere(cur->user));
  425. ast_cli(a->fd, "Secret: %s\n", ifthere(cur->pass));
  426. ast_cli(a->fd, "Language: %s\n", ifthere(cur->lang));
  427. ast_cli(a->fd, "Bind Port: %d\n", cur->port);
  428. ast_cli(a->fd, "Server: %s\n", server);
  429. ast_cli(a->fd, "Server Port: %d\n", cur->serverport);
  430. ast_cli(a->fd, "Alternate: %s\n", alternate);
  431. ast_cli(a->fd, "Flags: %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
  432. ast_cli(a->fd, "Format: %s\n", iax2_getformatname(cur->format));
  433. ast_cli(a->fd, "TOS: 0x%x\n", cur->tos);
  434. found++;
  435. }
  436. }
  437. ast_mutex_unlock(&provlock);
  438. if (!found) {
  439. if (a->argc == 3)
  440. ast_cli(a->fd, "No provisioning templates found\n");
  441. else
  442. ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
  443. }
  444. return CLI_SUCCESS;
  445. }
  446. static struct ast_cli_entry cli_iax2_provision[] = {
  447. AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
  448. };
  449. static int iax_provision_init(void)
  450. {
  451. ast_cli_register_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
  452. provinit = 1;
  453. return 0;
  454. }
  455. static void iax_provision_free_templates(int dead)
  456. {
  457. struct iax_template *cur;
  458. /* Drop dead or not (depending on dead) entries while locked */
  459. ast_mutex_lock(&provlock);
  460. AST_LIST_TRAVERSE_SAFE_BEGIN(&templates, cur, list) {
  461. if ((dead && cur->dead) || !dead) {
  462. AST_LIST_REMOVE_CURRENT(list);
  463. ast_free(cur);
  464. }
  465. }
  466. AST_LIST_TRAVERSE_SAFE_END;
  467. ast_mutex_unlock(&provlock);
  468. }
  469. int iax_provision_unload(void)
  470. {
  471. provinit = 0;
  472. ast_cli_unregister_multiple(cli_iax2_provision, sizeof(cli_iax2_provision) / sizeof(struct ast_cli_entry));
  473. iax_provision_free_templates(0 /* Remove all templates. */);
  474. return 0;
  475. }
  476. int iax_provision_reload(int reload)
  477. {
  478. struct ast_config *cfg;
  479. struct iax_template *cur;
  480. char *cat;
  481. int found = 0;
  482. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  483. if (!provinit)
  484. iax_provision_init();
  485. cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
  486. if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
  487. /* Mark all as dead. No need for locking */
  488. AST_LIST_TRAVERSE(&templates, cur, list) {
  489. cur->dead = 1;
  490. }
  491. /* Load as appropriate */
  492. cat = ast_category_browse(cfg, NULL);
  493. while(cat) {
  494. if (strcasecmp(cat, "general")) {
  495. iax_process_template(cfg, cat, found ? "default" : NULL);
  496. found++;
  497. ast_verb(3, "Loaded provisioning template '%s'\n", cat);
  498. }
  499. cat = ast_category_browse(cfg, cat);
  500. }
  501. ast_config_destroy(cfg);
  502. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  503. return 0;
  504. else
  505. ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
  506. iax_provision_free_templates(1 /* remove only marked as dead */);
  507. /* Purge cached signature DB entries */
  508. ast_db_deltree("iax/provisioning/cache", NULL);
  509. return 0;
  510. }