enum.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. * Funding provided by nic.at
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief ENUM Support for Asterisk
  23. *
  24. * \author Mark Spencer <markster@digium.com>
  25. *
  26. * \arg Funding provided by nic.at
  27. *
  28. * \par Enum standards
  29. *
  30. * - NAPTR records: http://ietf.nri.reston.va.us/rfc/rfc2915.txt
  31. * - DNS SRV records: http://www.ietf.org/rfc/rfc2782.txt
  32. * - ENUM http://www.ietf.org/rfc/rfc3761.txt
  33. * - ENUM for H.323: http://www.ietf.org/rfc/rfc3762.txt
  34. * - ENUM SIP: http://www.ietf.org/rfc/rfc3764.txt
  35. * - IANA ENUM Services: http://www.iana.org/assignments/enum-services
  36. *
  37. * - I-ENUM:
  38. * http://tools.ietf.org/wg/enum/draft-ietf-enum-combined/
  39. * http://tools.ietf.org/wg/enum/draft-ietf-enum-branch-location-record/
  40. *
  41. * \par Possible improvement
  42. * \todo Implement a caching mechanism for multile enum lookups
  43. * - See http://bugs.digium.com/view.php?id=6739
  44. * \todo The service type selection needs to be redone.
  45. */
  46. #include "asterisk.h"
  47. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  48. #include <sys/socket.h>
  49. #include <netinet/in.h>
  50. #include <arpa/nameser.h>
  51. #ifdef __APPLE__
  52. #if __APPLE_CC__ >= 1495
  53. #include <arpa/nameser_compat.h>
  54. #endif
  55. #endif
  56. #include <resolv.h>
  57. #include <ctype.h>
  58. #include <regex.h>
  59. #include "asterisk/enum.h"
  60. #include "asterisk/dns.h"
  61. #include "asterisk/channel.h"
  62. #include "asterisk/config.h"
  63. #include "asterisk/utils.h"
  64. #include "asterisk/manager.h"
  65. #ifdef __APPLE__
  66. #undef T_NAPTR
  67. #define T_NAPTR 35
  68. #endif
  69. #ifdef __APPLE__
  70. #undef T_TXT
  71. #define T_TXT 16
  72. #endif
  73. static char ienum_branchlabel[32] = "i";
  74. /* how to do infrastructure enum branch location resolution? */
  75. #define ENUMLOOKUP_BLR_CC 0
  76. #define ENUMLOOKUP_BLR_TXT 1
  77. #define ENUMLOOKUP_BLR_EBL 2
  78. static int ebl_alg = ENUMLOOKUP_BLR_CC;
  79. /* EBL record provisional type code */
  80. #define T_EBL 65300
  81. AST_MUTEX_DEFINE_STATIC(enumlock);
  82. /*! \brief Determine the length of a country code when given an E.164 string */
  83. /*
  84. * Input: E.164 number w/o leading +
  85. *
  86. * Output: number of digits in the country code
  87. * 0 on invalid number
  88. *
  89. * Algorithm:
  90. * 3 digits is the default length of a country code.
  91. * country codes 1 and 7 are a single digit.
  92. * the following country codes are two digits: 20, 27, 30-34, 36, 39,
  93. * 40, 41, 43-49, 51-58, 60-66, 81, 82, 84, 86, 90-95, 98.
  94. */
  95. static int cclen(const char *number)
  96. {
  97. int cc;
  98. char digits[3] = "";
  99. if (!number || (strlen(number) < 3)) {
  100. return 0;
  101. }
  102. strncpy(digits, number, 2);
  103. if (!sscanf(digits, "%30d", &cc)) {
  104. return 0;
  105. }
  106. if (cc / 10 == 1 || cc / 10 == 7)
  107. return 1;
  108. if (cc == 20 || cc == 27 || (cc >= 30 && cc <= 34) || cc == 36 ||
  109. cc == 39 || cc == 40 || cc == 41 || (cc >= 40 && cc <= 41) ||
  110. (cc >= 43 && cc <= 49) || (cc >= 51 && cc <= 58) ||
  111. (cc >= 60 && cc <= 66) || cc == 81 || cc == 82 || cc == 84 ||
  112. cc == 86 || (cc >= 90 && cc <= 95) || cc == 98) {
  113. return 2;
  114. }
  115. return 3;
  116. }
  117. struct txt_context {
  118. char txt[1024]; /* TXT record in TXT lookup */
  119. int txtlen; /* Length */
  120. };
  121. /*! \brief Callback for TXT record lookup, /ol version */
  122. static int txt_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
  123. {
  124. struct txt_context *c = context;
  125. unsigned int i;
  126. c->txt[0] = 0; /* default to empty */
  127. c->txtlen = 0;
  128. if (answer == NULL) {
  129. return 0;
  130. }
  131. /* RFC1035:
  132. *
  133. * <character-string> is a single length octet followed by that number of characters.
  134. * TXT-DATA One or more <character-string>s.
  135. *
  136. * We only take the first string here.
  137. */
  138. i = *answer++;
  139. len -= 1;
  140. if (i > len) { /* illegal packet */
  141. ast_log(LOG_WARNING, "txt_callback: malformed TXT record.\n");
  142. return 0;
  143. }
  144. if (i >= sizeof(c->txt)) { /* too long? */
  145. ast_log(LOG_WARNING, "txt_callback: TXT record too long.\n");
  146. i = sizeof(c->txt) - 1;
  147. }
  148. ast_copy_string(c->txt, (char *)answer, i + 1); /* this handles the \0 termination */
  149. c->txtlen = i;
  150. return 1;
  151. }
  152. /*! \brief Determine the branch location record as stored in a TXT record */
  153. /*
  154. * Input: CC code
  155. *
  156. * Output: number of digits in the number before the i-enum branch
  157. *
  158. * Algorithm: Build <ienum_branchlabel>.c.c.<suffix> and look for a TXT lookup.
  159. * Return atoi(TXT-record).
  160. * Return -1 on not found.
  161. *
  162. */
  163. static int blr_txt(const char *cc, const char *suffix)
  164. {
  165. struct txt_context context;
  166. char domain[128] = "";
  167. char *p1, *p2;
  168. int ret;
  169. ast_mutex_lock(&enumlock);
  170. ast_verb(4, "blr_txt() cc='%s', suffix='%s', c_bl='%s'\n", cc, suffix, ienum_branchlabel);
  171. if (sizeof(domain) < (strlen(cc) * 2 + strlen(ienum_branchlabel) + strlen(suffix) + 2)) {
  172. ast_mutex_unlock(&enumlock);
  173. ast_log(LOG_WARNING, "ERROR: string sizing in blr_txt.\n");
  174. return -1;
  175. }
  176. p1 = domain + snprintf(domain, sizeof(domain), "%s.", ienum_branchlabel);
  177. ast_mutex_unlock(&enumlock);
  178. for (p2 = (char *) cc + strlen(cc) - 1; p2 >= cc; p2--) {
  179. if (isdigit(*p2)) {
  180. *p1++ = *p2;
  181. *p1++ = '.';
  182. }
  183. }
  184. strcat(p1, suffix);
  185. ast_verb(4, "blr_txt() FQDN for TXT record: %s, cc was %s\n", domain, cc);
  186. ret = ast_search_dns(&context, domain, C_IN, T_TXT, txt_callback);
  187. if (ret > 0) {
  188. ret = atoi(context.txt);
  189. if ((ret >= 0) && (ret < 20)) {
  190. ast_verb(3, "blr_txt() BLR TXT record for %s is %d (apex: %s)\n", cc, ret, suffix);
  191. return ret;
  192. }
  193. }
  194. ast_verb(3, "blr_txt() BLR TXT record for %s not found (apex: %s)\n", cc, suffix);
  195. return -1;
  196. }
  197. struct ebl_context {
  198. unsigned char pos;
  199. char separator[256]; /* label to insert */
  200. int sep_len; /* Length */
  201. char apex[256]; /* new Apex */
  202. int apex_len; /* Length */
  203. };
  204. /*! \brief Callback for EBL record lookup */
  205. static int ebl_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
  206. {
  207. struct ebl_context *c = context;
  208. unsigned int i;
  209. c->pos = 0; /* default to empty */
  210. c->separator[0] = 0;
  211. c->sep_len = 0;
  212. c->apex[0] = 0;
  213. c->apex_len = 0;
  214. if (answer == NULL) {
  215. return 0;
  216. }
  217. /* draft-lendl-enum-branch-location-record-00
  218. *
  219. * 0 1 2 3 4 5 6 7
  220. * +--+--+--+--+--+--+--+--+
  221. * | POSITION |
  222. * +--+--+--+--+--+--+--+--+
  223. * / SEPARATOR /
  224. * +--+--+--+--+--+--+--+--+
  225. * / APEX /
  226. * +--+--+--+--+--+--+--+--+
  227. *
  228. * where POSITION is a single byte, SEPARATOR is a <character-string>
  229. * and APEX is a <domain-name>.
  230. *
  231. */
  232. c->pos = *answer++;
  233. len -= 1;
  234. if ((c->pos > 15) || len < 2) { /* illegal packet */
  235. ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
  236. return 0;
  237. }
  238. i = *answer++;
  239. len -= 1;
  240. if (i > len) { /* illegal packet */
  241. ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
  242. return 0;
  243. }
  244. ast_copy_string(c->separator, (char *)answer, i + 1);
  245. c->sep_len = i;
  246. answer += i;
  247. len -= i;
  248. if ((i = dn_expand((unsigned char *)fullanswer, (unsigned char *)answer + len,
  249. (unsigned char *)answer, c->apex, sizeof(c->apex) - 1)) < 0) {
  250. ast_log(LOG_WARNING, "Failed to expand hostname\n");
  251. return 0;
  252. }
  253. c->apex[i] = 0;
  254. c->apex_len = i;
  255. return 1;
  256. }
  257. /*! \brief Evaluate the I-ENUM branch as stored in an EBL record */
  258. /*
  259. * Input: CC code
  260. *
  261. * Output: number of digits in the number before the i-enum branch
  262. *
  263. * Algorithm: Build <ienum_branchlabel>.c.c.<suffix> and look for an EBL record
  264. * Return pos and fill in separator and apex.
  265. * Return -1 on not found.
  266. *
  267. */
  268. static int blr_ebl(const char *cc, const char *suffix, char *separator, int sep_len, char* apex, int apex_len)
  269. {
  270. struct ebl_context context;
  271. char domain[128] = "";
  272. char *p1,*p2;
  273. int ret;
  274. ast_mutex_lock(&enumlock);
  275. ast_verb(4, "blr_ebl() cc='%s', suffix='%s', c_bl='%s'\n", cc, suffix, ienum_branchlabel);
  276. if (sizeof(domain) < (strlen(cc) * 2 + strlen(ienum_branchlabel) + strlen(suffix) + 2)) {
  277. ast_mutex_unlock(&enumlock);
  278. ast_log(LOG_WARNING, "ERROR: string sizing in blr_EBL.\n");
  279. return -1;
  280. }
  281. p1 = domain + snprintf(domain, sizeof(domain), "%s.", ienum_branchlabel);
  282. ast_mutex_unlock(&enumlock);
  283. for (p2 = (char *) cc + strlen(cc) - 1; p2 >= cc; p2--) {
  284. if (isdigit(*p2)) {
  285. *p1++ = *p2;
  286. *p1++ = '.';
  287. }
  288. }
  289. strcat(p1, suffix);
  290. ast_verb(4, "blr_ebl() FQDN for EBL record: %s, cc was %s\n", domain, cc);
  291. ret = ast_search_dns(&context, domain, C_IN, T_EBL, ebl_callback);
  292. if (ret > 0) {
  293. ret = context.pos;
  294. if ((ret >= 0) && (ret < 20)) {
  295. ast_verb(3, "blr_txt() BLR EBL record for %s is %d/%s/%s)\n", cc, ret, context.separator, context.apex);
  296. ast_copy_string(separator, context.separator, sep_len);
  297. ast_copy_string(apex, context.apex, apex_len);
  298. return ret;
  299. }
  300. }
  301. ast_verb(3, "blr_txt() BLR EBL record for %s not found (apex: %s)\n", cc, suffix);
  302. return -1;
  303. }
  304. /*! \brief Parse NAPTR record information elements */
  305. static unsigned int parse_ie(char *data, unsigned int maxdatalen, unsigned char *src, unsigned int srclen)
  306. {
  307. unsigned int len, olen;
  308. len = olen = (unsigned int) src[0];
  309. src++;
  310. srclen--;
  311. if (len > srclen) {
  312. ast_log(LOG_WARNING, "ENUM parsing failed: Wanted %d characters, got %d\n", len, srclen);
  313. return -1;
  314. }
  315. if (len > maxdatalen)
  316. len = maxdatalen;
  317. memcpy(data, src, len);
  318. return olen + 1;
  319. }
  320. /*! \brief Parse DNS NAPTR record used in ENUM ---*/
  321. static int parse_naptr(unsigned char *dst, int dstsize, char *tech, int techsize, unsigned char *answer, int len, unsigned char *naptrinput)
  322. {
  323. char tech_return[80];
  324. char *oanswer = (char *)answer;
  325. char flags[512] = "";
  326. char services[512] = "";
  327. char *p;
  328. char regexp[512] = "";
  329. char repl[512] = "";
  330. char tempdst[512] = "";
  331. char errbuff[512] = "";
  332. char delim;
  333. char *delim2;
  334. char *pattern, *subst, *d;
  335. int res;
  336. int regexp_len, rc;
  337. static const int max_bt = 10; /* max num of regexp backreference allowed, must remain 10 to guarantee a valid backreference index */
  338. int size, matchindex; /* size is the size of the backreference sub. */
  339. size_t d_len = sizeof(tempdst) - 1;
  340. regex_t preg;
  341. regmatch_t pmatch[max_bt];
  342. tech_return[0] = '\0';
  343. dst[0] = '\0';
  344. if (len < sizeof(struct naptr)) {
  345. ast_log(LOG_WARNING, "NAPTR record length too short\n");
  346. return -1;
  347. }
  348. answer += sizeof(struct naptr);
  349. len -= sizeof(struct naptr);
  350. if ((res = parse_ie(flags, sizeof(flags) - 1, answer, len)) < 0) {
  351. ast_log(LOG_WARNING, "Failed to get flags from NAPTR record\n");
  352. return -1;
  353. } else {
  354. answer += res;
  355. len -= res;
  356. }
  357. if ((res = parse_ie(services, sizeof(services) - 1, answer, len)) < 0) {
  358. ast_log(LOG_WARNING, "Failed to get services from NAPTR record\n");
  359. return -1;
  360. } else {
  361. answer += res;
  362. len -= res;
  363. }
  364. if ((res = parse_ie(regexp, sizeof(regexp) - 1, answer, len)) < 0) {
  365. ast_log(LOG_WARNING, "Failed to get regexp from NAPTR record\n");
  366. return -1;
  367. } else {
  368. answer += res;
  369. len -= res;
  370. }
  371. if ((res = dn_expand((unsigned char *)oanswer, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
  372. ast_log(LOG_WARNING, "Failed to expand hostname\n");
  373. return -1;
  374. }
  375. ast_debug(3, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
  376. naptrinput, flags, services, regexp, repl);
  377. if (tolower(flags[0]) != 'u') {
  378. ast_log(LOG_WARNING, "NAPTR Flag must be 'U' or 'u'.\n");
  379. return -1;
  380. }
  381. p = strstr(services, "e2u+");
  382. if (p == NULL)
  383. p = strstr(services, "E2U+");
  384. if (p){
  385. p = p + 4;
  386. if (strchr(p, ':')){
  387. p = strchr(p, ':') + 1;
  388. }
  389. ast_copy_string(tech_return, p, sizeof(tech_return));
  390. } else {
  391. p = strstr(services, "+e2u");
  392. if (p == NULL)
  393. p = strstr(services, "+E2U");
  394. if (p) {
  395. *p = 0;
  396. p = strchr(services, ':');
  397. if (p)
  398. *p = 0;
  399. ast_copy_string(tech_return, services, sizeof(tech_return));
  400. }
  401. }
  402. regexp_len = strlen(regexp);
  403. if (regexp_len < 7) {
  404. ast_log(LOG_WARNING, "Regex too short to be meaningful.\n");
  405. return -1;
  406. }
  407. /* this takes the first character of the regexp (which is a delimiter)
  408. * and uses that character to find the index of the second delimiter */
  409. delim = regexp[0];
  410. delim2 = strchr(regexp + 1, delim);
  411. if ((delim2 == NULL) || (regexp[regexp_len - 1] != delim)) { /* is the second delimiter found, and is the end of the regexp a delimiter */
  412. ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
  413. return -1;
  414. } else if (strchr((delim2 + 1), delim) == NULL) { /* if the second delimiter is found, make sure there is a third instance. this could be the end one instead of the middle */
  415. ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
  416. return -1;
  417. }
  418. pattern = regexp + 1; /* pattern is the regex without the begining and ending delimiter */
  419. *delim2 = 0; /* zero out the middle delimiter */
  420. subst = delim2 + 1; /* dst substring is everything after the second delimiter. */
  421. regexp[regexp_len - 1] = 0; /* zero out the last delimiter */
  422. /*
  423. * now do the regex wizardry.
  424. */
  425. if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
  426. ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n", regexp);
  427. return -1;
  428. }
  429. if (preg.re_nsub > ARRAY_LEN(pmatch)) {
  430. ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
  431. regfree(&preg);
  432. return -1;
  433. }
  434. /* pmatch is an array containing the substring indexes for the regex backreference sub.
  435. * max_bt is the maximum number of backreferences allowed to be stored in pmatch */
  436. if ((rc = regexec(&preg, (char *) naptrinput, max_bt, pmatch, 0))) {
  437. regerror(rc, &preg, errbuff, sizeof(errbuff));
  438. ast_log(LOG_WARNING, "NAPTR Regex match failed. Reason: %s\n", errbuff);
  439. regfree(&preg);
  440. return -1;
  441. }
  442. regfree(&preg);
  443. d = tempdst;
  444. d_len--;
  445. /* perform the backreference sub. Search the subst for backreferences,
  446. * when a backreference is found, retrieve the backreferences number.
  447. * use the backreference number as an index for pmatch to retrieve the
  448. * beginning and ending indexes of the substring to insert as the backreference.
  449. * if no backreference is found, continue copying the subst into tempdst */
  450. while (*subst && (d_len > 0)) {
  451. if ((subst[0] == '\\') && isdigit(subst[1])) { /* is this character the beginning of a backreference */
  452. matchindex = (int) (subst[1] - '0');
  453. if (matchindex >= ARRAY_LEN(pmatch)) {
  454. ast_log(LOG_WARNING, "Error during regex substitution. Invalid pmatch index.\n");
  455. return -1;
  456. }
  457. /* pmatch len is 10. we are garanteed a single char 0-9 is a valid index */
  458. size = pmatch[matchindex].rm_eo - pmatch[matchindex].rm_so;
  459. if (size > d_len) {
  460. ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
  461. return -1;
  462. }
  463. /* are the pmatch indexes valid for the input length */
  464. if ((strlen((char *) naptrinput) >= pmatch[matchindex].rm_eo) && (pmatch[matchindex].rm_so <= pmatch[matchindex].rm_eo)) {
  465. memcpy(d, (naptrinput + (int) pmatch[matchindex].rm_so), size); /* copy input substring into backreference marker */
  466. d_len -= size;
  467. subst += 2; /* skip over backreference characters to next valid character */
  468. d += size;
  469. } else {
  470. ast_log(LOG_WARNING, "Error during regex substitution. Invalid backreference index.\n");
  471. return -1;
  472. }
  473. } else if (isprint(*subst)) {
  474. *d++ = *subst++;
  475. d_len--;
  476. } else {
  477. ast_log(LOG_WARNING, "Error during regex substitution.\n");
  478. return -1;
  479. }
  480. }
  481. *d = 0;
  482. ast_copy_string((char *) dst, tempdst, dstsize);
  483. dst[dstsize - 1] = '\0';
  484. if (*tech != '\0'){ /* check if it is requested NAPTR */
  485. if (!strncasecmp(tech, "ALL", techsize)){
  486. return 0; /* return or count any RR */
  487. }
  488. if (!strncasecmp(tech_return, tech, sizeof(tech_return) < techsize ? sizeof(tech_return): techsize)){
  489. ast_copy_string(tech, tech_return, techsize);
  490. return 0; /* we got our RR */
  491. } else { /* go to the next RR in the DNS answer */
  492. return 1;
  493. }
  494. }
  495. /* tech was not specified, return first parsed RR */
  496. ast_copy_string(tech, tech_return, techsize);
  497. return 0;
  498. }
  499. /* do not return requested value, just count RRs and return thei number in dst */
  500. #define ENUMLOOKUP_OPTIONS_COUNT 1
  501. /* do an ISN style lookup */
  502. #define ENUMLOOKUP_OPTIONS_ISN 2
  503. /* do a infrastructure ENUM lookup */
  504. #define ENUMLOOKUP_OPTIONS_IENUM 4
  505. /* do a direct DNS lookup: no reversal */
  506. #define ENUMLOOKUP_OPTIONS_DIRECT 8
  507. /*! \brief Callback from ENUM lookup function */
  508. static int enum_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
  509. {
  510. struct enum_context *c = context;
  511. void *p = NULL;
  512. int res;
  513. res = parse_naptr((unsigned char *)c->dst, c->dstlen, c->tech, c->techlen, answer, len, (unsigned char *)c->naptrinput);
  514. if (res < 0) {
  515. ast_log(LOG_WARNING, "Failed to parse naptr\n");
  516. return -1;
  517. } else if ((res == 0) && !ast_strlen_zero(c->dst)) { /* ok, we got needed NAPTR */
  518. if (c->options & ENUMLOOKUP_OPTIONS_COUNT) { /* counting RRs */
  519. c->count++;
  520. snprintf(c->dst, c->dstlen, "%d", c->count);
  521. } else {
  522. if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
  523. c->naptr_rrs = p;
  524. memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
  525. c->naptr_rrs[c->naptr_rrs_count].result = ast_strdup(c->dst);
  526. c->naptr_rrs[c->naptr_rrs_count].tech = ast_strdup(c->tech);
  527. c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
  528. c->naptr_rrs_count++;
  529. }
  530. c->dst[0] = 0;
  531. }
  532. return 0;
  533. }
  534. return 0;
  535. }
  536. /* ENUM lookup */
  537. int ast_get_enum(struct ast_channel *chan, const char *number, char *dst, int dstlen, char *tech, int techlen, char* suffix, char* options, unsigned int record, struct enum_context **argcontext)
  538. {
  539. struct enum_context *context;
  540. char tmp[512];
  541. char domain[256];
  542. char left[128];
  543. char middle[128];
  544. char naptrinput[128];
  545. char apex[128] = "";
  546. int ret = -1;
  547. /* for ISN rewrite */
  548. char *p1 = NULL;
  549. char *p2 = NULL;
  550. char *p3 = NULL;
  551. int k = 0;
  552. int i = 0;
  553. int z = 0;
  554. int spaceleft = 0;
  555. struct timeval time_start, time_end;
  556. if (ast_strlen_zero(suffix)) {
  557. ast_log(LOG_WARNING, "ast_get_enum need a suffix parameter now.\n");
  558. return -1;
  559. }
  560. ast_verb(2, "ast_get_enum(num='%s', tech='%s', suffix='%s', options='%s', record=%d\n", number, tech, suffix, options, record);
  561. /*
  562. We don't need that any more, that "n" preceding the number has been replaced by a flag
  563. in the options paramter.
  564. ast_copy_string(naptrinput, number, sizeof(naptrinput));
  565. */
  566. /*
  567. * The "number" parameter includes a leading '+' if it's a full E.164 number (and not ISN)
  568. * We need to preserve that as the regex inside NAPTRs expect the +.
  569. *
  570. * But for the domain generation, the '+' is a nuissance, so we get rid of it.
  571. */
  572. ast_copy_string(naptrinput, number[0] == 'n' ? number + 1 : number, sizeof(naptrinput));
  573. if (number[0] == '+') {
  574. number++;
  575. }
  576. if (!(context = ast_calloc(1, sizeof(*context))))
  577. return -1;
  578. if((p3 = strchr(naptrinput, '*'))) {
  579. *p3='\0';
  580. }
  581. context->naptrinput = naptrinput; /* The number */
  582. context->dst = dst; /* Return string */
  583. context->dstlen = dstlen;
  584. context->tech = tech;
  585. context->techlen = techlen;
  586. context->options = 0;
  587. context->position = record > 0 ? record : 1;
  588. context->count = 0;
  589. context->naptr_rrs = NULL;
  590. context->naptr_rrs_count = 0;
  591. /*
  592. * Process options:
  593. *
  594. * c Return count, not URI
  595. * i Use infrastructure ENUM
  596. * s Do ISN transformation
  597. * d Direct DNS query: no reversing.
  598. *
  599. */
  600. if (options != NULL) {
  601. if (strchr(options,'s')) {
  602. context->options |= ENUMLOOKUP_OPTIONS_ISN;
  603. } else if (strchr(options,'i')) {
  604. context->options |= ENUMLOOKUP_OPTIONS_IENUM;
  605. } else if (strchr(options,'d')) {
  606. context->options |= ENUMLOOKUP_OPTIONS_DIRECT;
  607. }
  608. if (strchr(options,'c')) {
  609. context->options |= ENUMLOOKUP_OPTIONS_COUNT;
  610. }
  611. if (strchr(number,'*')) {
  612. context->options |= ENUMLOOKUP_OPTIONS_ISN;
  613. }
  614. }
  615. ast_verb(2, "ENUM options(%s): pos=%d, options='%d'\n", options, context->position, context->options);
  616. ast_debug(1, "ast_get_enum(): n='%s', tech='%s', suffix='%s', options='%d', record='%d'\n",
  617. number, tech, suffix, context->options, context->position);
  618. /*
  619. * This code does more than simple RFC3261 ENUM. All these rewriting
  620. * schemes have in common that they build the FQDN for the NAPTR lookup
  621. * by concatenating
  622. * - a number which needs be flipped and "."-seperated (left)
  623. * - some fixed string (middle)
  624. * - an Apex. (apex)
  625. *
  626. * The RFC3261 ENUM is: left=full number, middle="", apex=from args.
  627. * ISN: number = "middle*left", apex=from args
  628. * I-ENUM: EBL parameters build the split, can change apex
  629. * Direct: left="", middle=argument, apex=from args
  630. *
  631. */
  632. /* default: the whole number will be flipped, no middle domain component */
  633. ast_copy_string(left, number, sizeof(left));
  634. middle[0] = '\0';
  635. /*
  636. * I-ENUM can change the apex, thus we copy it
  637. */
  638. ast_copy_string(apex, suffix, sizeof(apex));
  639. /* ISN rewrite */
  640. if ((context->options & ENUMLOOKUP_OPTIONS_ISN) && (p1 = strchr(number, '*'))) {
  641. *p1++ = '\0';
  642. ast_copy_string(left, number, sizeof(left));
  643. ast_copy_string(middle, p1, sizeof(middle) - 1);
  644. strcat(middle, ".");
  645. ast_verb(2, "ISN ENUM: left=%s, middle='%s'\n", left, middle);
  646. /* Direct DNS lookup rewrite */
  647. } else if (context->options & ENUMLOOKUP_OPTIONS_DIRECT) {
  648. left[0] = 0; /* nothing to flip around */
  649. ast_copy_string(middle, number, sizeof(middle) - 1);
  650. strcat(middle, ".");
  651. ast_verb(2, "DIRECT ENUM: middle='%s'\n", middle);
  652. /* Infrastructure ENUM rewrite */
  653. } else if (context->options & ENUMLOOKUP_OPTIONS_IENUM) {
  654. int sdl = 0;
  655. char cc[8];
  656. char sep[256], n_apex[256];
  657. int cc_len = cclen(number);
  658. sdl = cc_len;
  659. ast_mutex_lock(&enumlock);
  660. ast_copy_string(sep, ienum_branchlabel, sizeof(sep)); /* default */
  661. ast_mutex_unlock(&enumlock);
  662. switch (ebl_alg) {
  663. case ENUMLOOKUP_BLR_EBL:
  664. ast_copy_string(cc, number, cc_len); /* cclen() never returns more than 3 */
  665. sdl = blr_ebl(cc, suffix, sep, sizeof(sep) - 1, n_apex, sizeof(n_apex) - 1);
  666. if (sdl >= 0) {
  667. ast_copy_string(apex, n_apex, sizeof(apex));
  668. ast_verb(2, "EBL ENUM: sep=%s, apex='%s'\n", sep, n_apex);
  669. } else {
  670. sdl = cc_len;
  671. }
  672. break;
  673. case ENUMLOOKUP_BLR_TXT:
  674. ast_copy_string(cc, number, cc_len); /* cclen() never returns more than 3 */
  675. sdl = blr_txt(cc, suffix);
  676. if (sdl < 0)
  677. sdl = cc_len;
  678. break;
  679. case ENUMLOOKUP_BLR_CC: /* BLR is at the country-code level */
  680. default:
  681. sdl = cc_len;
  682. break;
  683. }
  684. if (sdl > strlen(number)) { /* Number too short for this sdl? */
  685. ast_log(LOG_WARNING, "I-ENUM: subdomain location %d behind number %s\n", sdl, number);
  686. return 0;
  687. }
  688. ast_copy_string(left, number + sdl, sizeof(left));
  689. ast_mutex_lock(&enumlock);
  690. ast_copy_string(middle, sep, sizeof(middle) - 1);
  691. strcat(middle, ".");
  692. ast_mutex_unlock(&enumlock);
  693. /* check the space we need for middle */
  694. if ((sdl * 2 + strlen(middle) + 2) > sizeof(middle)) {
  695. ast_log(LOG_WARNING, "ast_get_enum: not enough space for I-ENUM rewrite.\n");
  696. return -1;
  697. }
  698. p1 = middle + strlen(middle);
  699. for (p2 = (char *) number + sdl - 1; p2 >= number; p2--) {
  700. if (isdigit(*p2)) {
  701. *p1++ = *p2;
  702. *p1++ = '.';
  703. }
  704. }
  705. *p1 = '\0';
  706. ast_verb(2, "I-ENUM: cclen=%d, left=%s, middle='%s', apex='%s'\n", cc_len, left, middle, apex);
  707. }
  708. if (strlen(left) * 2 + 2 > sizeof(domain)) {
  709. ast_log(LOG_WARNING, "string to long in ast_get_enum\n");
  710. return -1;
  711. }
  712. /* flip left into domain */
  713. p1 = domain;
  714. for (p2 = left + strlen(left); p2 >= left; p2--) {
  715. if (isdigit(*p2)) {
  716. *p1++ = *p2;
  717. *p1++ = '.';
  718. }
  719. }
  720. *p1 = '\0';
  721. if (chan && ast_autoservice_start(chan) < 0) {
  722. ast_free(context);
  723. return -1;
  724. }
  725. spaceleft = sizeof(tmp) - 2;
  726. ast_copy_string(tmp, domain, spaceleft);
  727. spaceleft -= strlen(domain);
  728. if (*middle) {
  729. strncat(tmp, middle, spaceleft);
  730. spaceleft -= strlen(middle);
  731. }
  732. strncat(tmp,apex,spaceleft);
  733. time_start = ast_tvnow();
  734. ret = ast_search_dns(context, tmp, C_IN, T_NAPTR, enum_callback);
  735. time_end = ast_tvnow();
  736. ast_verb(2, "ast_get_enum() profiling: %s, %s, %d ms\n",
  737. (ret == 0) ? "OK" : "FAIL", tmp, ast_tvdiff_ms(time_end, time_start));
  738. if (ret < 0) {
  739. ast_debug(1, "No such number found: %s (%s)\n", tmp, strerror(errno));
  740. strcpy(dst, "0");
  741. ret = 0;
  742. }
  743. if (context->naptr_rrs_count >= context->position && ! (context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
  744. /* sort array by NAPTR order/preference */
  745. for (k = 0; k < context->naptr_rrs_count; k++) {
  746. for (i = 0; i < context->naptr_rrs_count; i++) {
  747. /* use order first and then preference to compare */
  748. if ((ntohs(context->naptr_rrs[k].naptr.order) < ntohs(context->naptr_rrs[i].naptr.order)
  749. && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
  750. || (ntohs(context->naptr_rrs[k].naptr.order) > ntohs(context->naptr_rrs[i].naptr.order)
  751. && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
  752. z = context->naptr_rrs[k].sort_pos;
  753. context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
  754. context->naptr_rrs[i].sort_pos = z;
  755. continue;
  756. }
  757. if (ntohs(context->naptr_rrs[k].naptr.order) == ntohs(context->naptr_rrs[i].naptr.order)) {
  758. if ((ntohs(context->naptr_rrs[k].naptr.pref) < ntohs(context->naptr_rrs[i].naptr.pref)
  759. && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
  760. || (ntohs(context->naptr_rrs[k].naptr.pref) > ntohs(context->naptr_rrs[i].naptr.pref)
  761. && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
  762. z = context->naptr_rrs[k].sort_pos;
  763. context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
  764. context->naptr_rrs[i].sort_pos = z;
  765. }
  766. }
  767. }
  768. }
  769. for (k = 0; k < context->naptr_rrs_count; k++) {
  770. if (context->naptr_rrs[k].sort_pos == context->position - 1) {
  771. ast_copy_string(context->dst, context->naptr_rrs[k].result, dstlen);
  772. ast_copy_string(context->tech, context->naptr_rrs[k].tech, techlen);
  773. break;
  774. }
  775. }
  776. } else if (!(context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
  777. context->dst[0] = 0;
  778. } else if ((context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
  779. snprintf(context->dst,context->dstlen,"%d",context->count);
  780. }
  781. if (chan)
  782. ret |= ast_autoservice_stop(chan);
  783. if (!argcontext) {
  784. for (k = 0; k < context->naptr_rrs_count; k++) {
  785. ast_free(context->naptr_rrs[k].result);
  786. ast_free(context->naptr_rrs[k].tech);
  787. }
  788. ast_free(context->naptr_rrs);
  789. ast_free(context);
  790. } else
  791. *argcontext = context;
  792. return ret;
  793. }
  794. int ast_get_txt(struct ast_channel *chan, const char *number, char *txt, int txtlen, char *suffix)
  795. {
  796. struct txt_context context;
  797. char tmp[259 + 512];
  798. int pos = strlen(number) - 1;
  799. int newpos = 0;
  800. int ret = -1;
  801. ast_debug(4, "ast_get_txt: Number = '%s', suffix = '%s'\n", number, suffix);
  802. if (chan && ast_autoservice_start(chan) < 0) {
  803. return -1;
  804. }
  805. if (pos > 128) {
  806. pos = 128;
  807. }
  808. while (pos >= 0) {
  809. if (isdigit(number[pos])) {
  810. tmp[newpos++] = number[pos];
  811. tmp[newpos++] = '.';
  812. }
  813. pos--;
  814. }
  815. ast_copy_string(&tmp[newpos], suffix, sizeof(tmp) - newpos);
  816. if (ret < 0) {
  817. ast_debug(2, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
  818. ret = 0;
  819. } else {
  820. ast_copy_string(txt, context.txt, txtlen);
  821. }
  822. if (chan) {
  823. ret |= ast_autoservice_stop(chan);
  824. }
  825. return ret;
  826. }
  827. /*! \brief Initialize the ENUM support subsystem */
  828. static int private_enum_init(int reload)
  829. {
  830. struct ast_config *cfg;
  831. const char *string;
  832. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  833. if ((cfg = ast_config_load2("enum.conf", "enum", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
  834. return 0;
  835. /* Destroy existing list */
  836. ast_mutex_lock(&enumlock);
  837. if (cfg) {
  838. if ((string = ast_variable_retrieve(cfg, "ienum", "branchlabel"))) {
  839. ast_copy_string(ienum_branchlabel, string, sizeof(ienum_branchlabel));
  840. }
  841. if ((string = ast_variable_retrieve(cfg, "ienum", "ebl_alg"))) {
  842. ebl_alg = ENUMLOOKUP_BLR_CC; /* default */
  843. if (!strcasecmp(string, "txt"))
  844. ebl_alg = ENUMLOOKUP_BLR_TXT;
  845. else if (!strcasecmp(string, "ebl"))
  846. ebl_alg = ENUMLOOKUP_BLR_EBL;
  847. else if (!strcasecmp(string, "cc"))
  848. ebl_alg = ENUMLOOKUP_BLR_CC;
  849. else
  850. ast_log(LOG_WARNING, "No valid parameter for ienum/ebl_alg.\n");
  851. }
  852. ast_config_destroy(cfg);
  853. }
  854. ast_mutex_unlock(&enumlock);
  855. manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: Enum\r\nStatus: Enabled\r\nMessage: ENUM reload Requested\r\n");
  856. return 0;
  857. }
  858. int ast_enum_init(void)
  859. {
  860. return private_enum_init(0);
  861. }
  862. int ast_enum_reload(void)
  863. {
  864. return private_enum_init(1);
  865. }