enum.c 29 KB

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