enum.c 29 KB

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