iax2-provision.c 16 KB

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