iax2-provision.c 15 KB

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