provision.c 16 KB

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