iax2-provision.c 14 KB

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