michael@12: /* michael@12: * $Id: enum.c 5901 2009-07-21 07:45:05Z bogdan_iancu $ michael@12: * michael@12: * Enum and E164 related functions michael@12: * michael@12: * Copyright (C) 2002-2008 Juha Heinanen michael@12: * michael@12: * This file is part of opensips, a free SIP server. michael@12: * michael@12: * opensips is free software; you can redistribute it and/or modify michael@12: * it under the terms of the GNU General Public License as published by michael@12: * the Free Software Foundation; either version 2 of the License, or michael@12: * (at your option) any later version michael@12: * michael@12: * opensips is distributed in the hope that it will be useful, michael@12: * but WITHOUT ANY WARRANTY; without even the implied warranty of michael@12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the michael@12: * GNU General Public License for more details. michael@12: * michael@12: * You should have received a copy of the GNU General Public License michael@12: * along with this program; if not, write to the Free Software michael@12: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA michael@12: * michael@12: */ michael@12: michael@12: #include michael@12: michael@12: #include "enum.h" michael@12: #include "../../parser/parse_uri.h" michael@12: #include "../../parser/parse_from.h" michael@12: #include "../../ut.h" michael@12: #include "../../resolve.h" michael@12: #include "../../mem/mem.h" michael@12: #include "../../dset.h" michael@12: #include "../../qvalue.h" michael@12: #include "enum_mod.h" michael@12: #include "../../regexp.h" michael@12: #include "../../pvar.h" michael@12: michael@12: /* michael@12: * Input: E.164 number w/o leading + michael@12: * michael@12: * Output: number of digits in the country code michael@12: * 0 on invalid number michael@12: * michael@12: * convention: michael@12: * 3 digits is the default length of a country code. michael@12: * country codes 1 and 7 are a single digit. michael@12: * the following country codes are two digits: 20, 27, 30-34, 36, 39, michael@12: * 40, 41, 43-49, 51-58, 60-66, 81, 82, 84, 86, 90-95, 98. michael@12: */ michael@12: static int cclen(const char *number) michael@12: { michael@12: char d1,d2; michael@12: michael@12: if (!number || (strlen(number) < 3)) michael@12: return(0); michael@12: michael@12: d1 = number[0]; michael@12: d2 = number[1]; michael@12: michael@12: if (!isdigit((int)d2)) michael@12: return(0); michael@12: michael@12: switch(d1) { michael@12: case '1': michael@12: case '7': michael@12: return(1); michael@12: case '2': michael@12: if ((d2 == '0') || (d1 == '7')) michael@12: return(2); michael@12: break; michael@12: case '3': michael@12: if ((d2 >= '0') && (d1 <= '4')) michael@12: return(2); michael@12: if ((d2 == '6') || (d1 == '9')) michael@12: return(2); michael@12: break; michael@12: case '4': michael@12: if (d2 != '2') michael@12: return(2); michael@12: break; michael@12: case '5': michael@12: if ((d2 >= '1') && (d1 <= '8')) michael@12: return(2); michael@12: break; michael@12: case '6': michael@12: if (d1 <= '6') michael@12: return(2); michael@12: break; michael@12: case '8': michael@12: if ((d2 == '1') || (d1 == '2') || (d1 == '4') || (d1 == '6')) michael@12: return(2); michael@12: break; michael@12: case '9': michael@12: if (d1 <= '5') michael@12: return(2); michael@12: if (d2 == '8') michael@12: return(2); michael@12: break; michael@12: default: michael@12: return(0); michael@12: } michael@12: michael@12: return(3); michael@12: } michael@12: michael@12: michael@12: michael@12: /* return the length of the string until c, if not found returns n */ michael@12: static inline int findchr(char* p, int c, unsigned int size) michael@12: { michael@12: int len=0; michael@12: michael@12: for(;len 0) { michael@12: if (*first == '!') { michael@12: second = (char *)memchr((void *)(first + 1), '!', len - 1); michael@12: if (second) { michael@12: len = len - (second - first + 1); michael@12: if (len > 0) { michael@12: third = memchr(second + 1, '!', len); michael@12: if (third) { michael@12: pattern->len = second - first - 1; michael@12: pattern->s = first + 1; michael@12: replacement->len = third - second - 1; michael@12: replacement->s = second + 1; michael@12: return 1; michael@12: } else { michael@12: LM_ERR("Third ! missing from regexp\n"); michael@12: return -1; michael@12: } michael@12: } else { michael@12: LM_ERR("Third ! missing from regexp\n"); michael@12: return -2; michael@12: } michael@12: } else { michael@12: LM_ERR("Second ! missing from regexp\n"); michael@12: return -3; michael@12: } michael@12: } else { michael@12: LM_ERR("First ! missing from regexp\n"); michael@12: return -4; michael@12: } michael@12: } else { michael@12: LM_ERR("Regexp missing\n"); michael@12: return -5; michael@12: } michael@12: } michael@12: /* Checks if NAPTR record has flag u and its services field michael@12: * e2u+[service:]sip or michael@12: * e2u+service[+service[+service[+...]]] michael@12: */ michael@12: static inline int sip_match( struct naptr_rdata* naptr, str* service) michael@12: { michael@12: if (service->len == 0) { michael@12: return (naptr->flags_len == 1) && michael@12: ((naptr->flags[0] == 'u') || (naptr->flags[0] == 'U')) && michael@12: (naptr->services_len == 7) && michael@12: ((strncasecmp(naptr->services, "e2u+sip", 7) == 0) || michael@12: (strncasecmp(naptr->services, "sip+e2u", 7) == 0)); michael@12: } else if (service->s[0] != '+') { michael@12: return (naptr->flags_len == 1) && michael@12: ((naptr->flags[0] == 'u') || (naptr->flags[0] == 'U')) && michael@12: (naptr->services_len == service->len + 8) && michael@12: (strncasecmp(naptr->services, "e2u+", 4) == 0) && michael@12: (strncasecmp(naptr->services + 4, service->s, service->len) == 0) && michael@12: (strncasecmp(naptr->services + 4 + service->len, ":sip", 4) == 0); michael@12: } else { /* handle compound NAPTRs and multiple services */ michael@12: str bakservice, baknaptr; /* we bakup the str */ michael@12: int naptrlen, len; /* length of the extracted service */ michael@12: michael@12: /* RFC 3761, NAPTR service field must start with E2U+ */ michael@12: if (strncasecmp(naptr->services, "e2u+", 4) != 0) { michael@12: return 0; michael@12: } michael@12: baknaptr.s = naptr->services + 4; /* leading 'e2u+' */ michael@12: baknaptr.len = naptr->services_len - 4; michael@12: for (;;) { /* iterate over services in NAPTR */ michael@12: bakservice.s = service->s + 1; /* leading '+' */ michael@12: bakservice.len = service->len - 1; michael@12: naptrlen = findchr(baknaptr.s,'+',baknaptr.len); michael@12: michael@12: for (;;) { /* iterate over services in enum_query */ michael@12: len = findchr(bakservice.s,'+',bakservice.len); michael@12: if ((naptrlen == len ) && !strncasecmp(baknaptr.s, bakservice.s, len)){ michael@12: return 1; michael@12: } michael@12: if ( (bakservice.len -= len+1) > 0) { michael@12: bakservice.s += len+1; michael@12: continue; michael@12: } michael@12: break; michael@12: } michael@12: if ( (baknaptr.len -= naptrlen+1) > 0) { michael@12: baknaptr.s += naptrlen+1; michael@12: continue; michael@12: } michael@12: break; michael@12: } michael@12: /* no matching service found */ michael@12: return 0; michael@12: } michael@12: } michael@12: michael@12: michael@12: /* michael@12: * Checks if argument is an e164 number starting with + michael@12: */ michael@12: static inline int is_e164(str* _user) michael@12: { michael@12: int i; michael@12: char c; michael@12: michael@12: if ((_user->len > 2) && (_user->len < 17) && ((_user->s)[0] == '+')) { michael@12: for (i = 1; i < _user->len; i++) { michael@12: c = (_user->s)[i]; michael@12: if ((c < '0') || (c > '9')) return -1; michael@12: } michael@12: return 1; michael@12: } else { michael@12: return -1; michael@12: } michael@12: } michael@12: michael@12: michael@12: /* michael@12: * Call is_from_user_enum_2 with module parameter suffix and default service. michael@12: */ michael@12: int is_from_user_enum_0(struct sip_msg* _msg, char* _str1, char* _str2) michael@12: { michael@12: return is_from_user_enum_2(_msg, (char *)(&suffix), (char *)(&service)); michael@12: } michael@12: michael@12: /* michael@12: * Call is_from_user_enum_2 with given suffix and default service. michael@12: */ michael@12: int is_from_user_enum_1(struct sip_msg* _msg, char* _suffix, char* _str2) michael@12: { michael@12: return is_from_user_enum_2(_msg, _suffix, (char *)(&service)); michael@12: } michael@12: michael@12: /* michael@12: * Check if from user is a valid enum based user, and check to make sure michael@12: * that the src_ip == an srv record that maps to the enum from user. michael@12: */ michael@12: int is_from_user_enum_2(struct sip_msg* _msg, char* _suffix, char* _service) michael@12: { michael@12: struct ip_addr addr; michael@12: struct hostent* he; michael@12: unsigned short zp; michael@12: unsigned short proto; michael@12: char *user_s; michael@12: int user_len, i, j; michael@12: char name[MAX_DOMAIN_SIZE]; michael@12: char uri[MAX_URI_SIZE]; michael@12: struct sip_uri *furi; michael@12: struct sip_uri luri; michael@12: struct rdata* head; michael@12: michael@12: str* suffix; michael@12: str* service; michael@12: michael@12: struct rdata* l; michael@12: struct naptr_rdata* naptr; michael@12: michael@12: str pattern, replacement, result; michael@12: char string[17]; michael@12: michael@12: if (parse_from_header(_msg) < 0) { michael@12: LM_ERR("Failed to parse From header\n"); michael@12: return -1; michael@12: } michael@12: michael@12: if(_msg->from==NULL || get_from(_msg)==NULL) { michael@12: LM_DBG("No From header\n"); michael@12: return -1; michael@12: } michael@12: michael@12: if ((furi = parse_from_uri(_msg)) == NULL) { michael@12: LM_ERR("Failed to parse From URI\n"); michael@12: return -1; michael@12: } michael@12: michael@12: suffix = (str*)_suffix; michael@12: service = (str*)_service; michael@12: michael@12: if (is_e164(&(furi->user)) == -1) { michael@12: LM_ERR("From URI user is not an E164 number\n"); michael@12: return -1; michael@12: } michael@12: michael@12: /* assert: the from user is a valid formatted e164 string */ michael@12: michael@12: user_s = furi->user.s; michael@12: user_len = furi->user.len; michael@12: michael@12: j = 0; michael@12: for (i = user_len - 1; i > 0; i--) { michael@12: name[j] = user_s[i]; michael@12: name[j + 1] = '.'; michael@12: j = j + 2; michael@12: } michael@12: michael@12: memcpy(name + j, suffix->s, suffix->len + 1); michael@12: michael@12: head = get_record(name, T_NAPTR); michael@12: michael@12: if (head == 0) { michael@12: LM_DBG("No NAPTR record found for %s.\n", name); michael@12: return -3; michael@12: } michael@12: michael@12: /* we have the naptr records, loop and find an srv record with */ michael@12: /* same ip address as source ip address, if we do then true is returned */ michael@12: michael@12: for (l = head; l; l = l->next) { michael@12: michael@12: if (l->type != T_NAPTR) continue; /*should never happen*/ michael@12: naptr = (struct naptr_rdata*)l->rdata; michael@12: if (naptr == 0) { michael@12: LM_ERR("Null rdata in DNS response\n"); michael@12: free_rdata_list(head); michael@12: return -4; michael@12: } michael@12: michael@12: LM_DBG("ENUM query on %s: order %u, pref %u, flen %u, flags " michael@12: "'%.*s', slen %u, services '%.*s', rlen %u, " michael@12: "regexp '%.*s'\n", michael@12: name, naptr->order, naptr->pref, michael@12: naptr->flags_len, (int)(naptr->flags_len), ZSW(naptr->flags), naptr->services_len, michael@12: (int)(naptr->services_len), ZSW(naptr->services), naptr->regexp_len, michael@12: (int)(naptr->regexp_len), ZSW(naptr->regexp)); michael@12: michael@12: if (sip_match(naptr, service) != 0) { michael@12: if (parse_naptr_regexp(&(naptr->regexp[0]), naptr->regexp_len, michael@12: &pattern, &replacement) < 0) { michael@12: free_rdata_list(head); /*clean up*/ michael@12: LM_ERR("Parsing of NAPTR regexp failed\n"); michael@12: return -5; michael@12: } michael@12: #ifdef LATER michael@12: if ((pattern.len == 4) && (strncmp(pattern.s, "^.*$", 4) == 0)) { michael@12: LM_DBG("Resulted in replacement: '%.*s'\n", michael@12: replacement.len, ZSW(replacement.s)); michael@12: retval = set_uri(_msg, replacement.s, replacement.len); michael@12: free_rdata_list(head); /*clean up*/ michael@12: return retval; michael@12: } michael@12: #endif michael@12: result.s = &(uri[0]); michael@12: result.len = MAX_URI_SIZE; michael@12: /* Avoid making copies of pattern and replacement */ michael@12: pattern.s[pattern.len] = (char)0; michael@12: replacement.s[replacement.len] = (char)0; michael@12: /* We have already checked the size of michael@12: _msg->parsed_uri.user.s */ michael@12: memcpy(&(string[0]), user_s, user_len); michael@12: string[user_len] = (char)0; michael@12: if (reg_replace(pattern.s, replacement.s, &(string[0]), michael@12: &result) < 0) { michael@12: pattern.s[pattern.len] = '!'; michael@12: replacement.s[replacement.len] = '!'; michael@12: LM_ERR("Regexp replace failed\n"); michael@12: free_rdata_list(head); /*clean up*/ michael@12: return -6; michael@12: } michael@12: LM_DBG("Resulted in replacement: '%.*s'\n", michael@12: result.len, ZSW(result.s)); michael@12: michael@12: if(parse_uri(result.s, result.len, &luri) < 0) michael@12: { michael@12: LM_ERR("Parsing of URI <%.*s> failed\n", michael@12: result.len, result.s); michael@12: free_rdata_list(head); /*clean up*/ michael@12: return -7; michael@12: } michael@12: michael@12: pattern.s[pattern.len] = '!'; michael@12: replacement.s[replacement.len] = '!'; michael@12: michael@12: zp = 0; michael@12: proto = PROTO_NONE; michael@12: he = sip_resolvehost(&luri.host, &zp, &proto, michael@12: (luri.type==SIPS_URI_T)?1:0 , 0); michael@12: michael@12: hostent2ip_addr(&addr, he, 0); michael@12: michael@12: if(ip_addr_cmp(&addr, &_msg->rcv.src_ip)) michael@12: { michael@12: free_rdata_list(head); michael@12: return(1); michael@12: } michael@12: } michael@12: } michael@12: free_rdata_list(head); /*clean up*/ michael@12: LM_DBG("FAIL\n"); michael@12: michael@12: /* must not have found the record */ michael@12: return(-8); michael@12: } michael@12: michael@12: michael@12: michael@12: /* michael@12: * Add parameter to URI. michael@12: */ michael@12: int add_uri_param(str *uri, str *param, str *new_uri) michael@12: { michael@12: struct sip_uri puri; michael@12: char *at; michael@12: michael@12: if (parse_uri(uri->s, uri->len, &puri) < 0) { michael@12: return 0; michael@12: } michael@12: michael@12: /* if current uri has no headers, pad param to the end of uri */ michael@12: if (puri.headers.len == 0) { michael@12: memcpy(uri->s + uri->len, param->s, param->len); michael@12: uri->len = uri->len + param->len; michael@12: new_uri->len = 0; michael@12: return 1; michael@12: } michael@12: michael@12: /* otherwise take the long path and create new_uri */ michael@12: at = new_uri->s; michael@12: switch (puri.type) { michael@12: case SIP_URI_T: michael@12: memcpy(at, "sip:", 4); michael@12: at = at + 4; michael@12: break; michael@12: case SIPS_URI_T: michael@12: memcpy(at, "sips:", 5); michael@12: at = at + 5; michael@12: break; michael@12: case TEL_URI_T: michael@12: memcpy(at, "tel:", 4); michael@12: at = at + 4; michael@12: break; michael@12: case TELS_URI_T: michael@12: memcpy(at, "tels:", 5); michael@12: at = at + 5; michael@12: break; michael@12: default: michael@12: LM_ERR("Unknown URI scheme <%d>\n", puri.type); michael@12: return 0; michael@12: } michael@12: if (puri.user.len) { michael@12: memcpy(at, puri.user.s, puri.user.len); michael@12: at = at + puri.user.len; michael@12: if (puri.passwd.len) { michael@12: *at = ':'; michael@12: at = at + 1; michael@12: memcpy(at, puri.passwd.s, puri.passwd.len); michael@12: at = at + puri.passwd.len; michael@12: }; michael@12: *at = '@'; michael@12: at = at + 1; michael@12: } michael@12: memcpy(at, puri.host.s, puri.host.len); michael@12: at = at + puri.host.len; michael@12: if (puri.port.len) { michael@12: *at = ':'; michael@12: at = at + 1; michael@12: memcpy(at, puri.port.s, puri.port.len); michael@12: at = at + puri.port.len; michael@12: } michael@12: if (puri.params.len) { michael@12: *at = ';'; michael@12: at = at + 1; michael@12: memcpy(at, puri.params.s, puri.params.len); michael@12: at = at + puri.params.len; michael@12: } michael@12: memcpy(at, param->s, param->len); michael@12: at = at + param->len; michael@12: *at = '?'; michael@12: at = at + 1; michael@12: memcpy(at, puri.headers.s, puri.headers.len); michael@12: at = at + puri.headers.len; michael@12: new_uri->len = at - new_uri->s; michael@12: return 1; michael@12: } michael@12: michael@12: /* michael@12: * Tests if one result record is "greater" that the other. Non-NAPTR records michael@12: * greater that NAPTR record. An invalid NAPTR record is greater than a michael@12: * valid one. Valid NAPTR records are compared based on their michael@12: * (order,preference). michael@12: */ michael@12: static inline int naptr_greater(struct rdata* a, struct rdata* b) michael@12: { michael@12: struct naptr_rdata *na, *nb; michael@12: michael@12: if (a->type != T_NAPTR) return 1; michael@12: if (b->type != T_NAPTR) return 0; michael@12: michael@12: na = (struct naptr_rdata*)a->rdata; michael@12: if (na == 0) return 1; michael@12: michael@12: nb = (struct naptr_rdata*)b->rdata; michael@12: if (nb == 0) return 0; michael@12: michael@12: return (((na->order) << 16) + na->pref) > michael@12: (((nb->order) << 16) + nb->pref); michael@12: } michael@12: michael@12: michael@12: /* michael@12: * Bubble sorts result record list according to naptr (order,preference). michael@12: */ michael@12: static inline void naptr_sort(struct rdata** head) michael@12: { michael@12: struct rdata *p, *q, *r, *s, *temp, *start; michael@12: michael@12: /* r precedes p and s points to the node up to which comparisons michael@12: are to be made */ michael@12: michael@12: s = NULL; michael@12: start = *head; michael@12: while ( s != start -> next ) { michael@12: r = p = start ; michael@12: q = p -> next ; michael@12: while ( p != s ) { michael@12: if ( naptr_greater(p, q) ) { michael@12: if ( p == start ) { michael@12: temp = q -> next ; michael@12: q -> next = p ; michael@12: p -> next = temp ; michael@12: start = q ; michael@12: r = q ; michael@12: } else { michael@12: temp = q -> next ; michael@12: q -> next = p ; michael@12: p -> next = temp ; michael@12: r -> next = q ; michael@12: r = q ; michael@12: } michael@12: } else { michael@12: r = p ; michael@12: p = p -> next ; michael@12: } michael@12: q = p -> next ; michael@12: if ( q == s ) s = p ; michael@12: } michael@12: } michael@12: *head = start; michael@12: } michael@12: michael@12: michael@12: /* michael@12: * Makes enum query on name. On success, rewrites user part and michael@12: * replaces Request-URI. michael@12: */ michael@12: int do_query(struct sip_msg* _msg, char *user, char *name, str *service) { michael@12: michael@12: char uri[MAX_URI_SIZE]; michael@12: char new_uri[MAX_URI_SIZE]; michael@12: unsigned int priority, curr_prio, first; michael@12: qvalue_t q; michael@12: struct rdata* head; michael@12: struct rdata* l; michael@12: struct naptr_rdata* naptr; michael@12: str pattern, replacement, result, new_result; michael@12: michael@12: head = get_record(name, T_NAPTR); michael@12: michael@12: if (head == 0) { michael@12: LM_DBG("No NAPTR record found for %s.\n", name); michael@12: return -1; michael@12: } michael@12: michael@12: naptr_sort(&head); michael@12: michael@12: q = MAX_Q - 10; michael@12: curr_prio = 0; michael@12: first = 1; michael@12: michael@12: for (l = head; l; l = l->next) { michael@12: michael@12: if (l->type != T_NAPTR) continue; /*should never happen*/ michael@12: naptr = (struct naptr_rdata*)l->rdata; michael@12: if (naptr == 0) { michael@12: LM_ERR("Null rdata in DNS response\n"); michael@12: continue; michael@12: } michael@12: michael@12: LM_DBG("ENUM query on %s: order %u, pref %u, flen %u, flags '%.*s', " michael@12: "slen %u, services '%.*s', rlen %u, regexp '%.*s'\n", michael@12: name, naptr->order, naptr->pref, michael@12: naptr->flags_len, (int)(naptr->flags_len), ZSW(naptr->flags), michael@12: naptr->services_len, michael@12: (int)(naptr->services_len), ZSW(naptr->services), naptr->regexp_len, michael@12: (int)(naptr->regexp_len), ZSW(naptr->regexp)); michael@12: michael@12: if (sip_match(naptr, service) == 0) continue; michael@12: michael@12: if (parse_naptr_regexp(&(naptr->regexp[0]), naptr->regexp_len, michael@12: &pattern, &replacement) < 0) { michael@12: LM_ERR("Parsing of NAPTR regexp failed\n"); michael@12: continue; michael@12: } michael@12: result.s = &(uri[0]); michael@12: result.len = MAX_URI_SIZE; michael@12: /* Avoid making copies of pattern and replacement */ michael@12: pattern.s[pattern.len] = (char)0; michael@12: replacement.s[replacement.len] = (char)0; michael@12: if (reg_replace(pattern.s, replacement.s, user, &result) < 0) { michael@12: pattern.s[pattern.len] = '!'; michael@12: replacement.s[replacement.len] = '!'; michael@12: LM_ERR("Regexp replace failed\n"); michael@12: continue; michael@12: } michael@12: LM_DBG("Resulted in replacement: '%.*s'\n", result.len, ZSW(result.s)); michael@12: pattern.s[pattern.len] = '!'; michael@12: replacement.s[replacement.len] = '!'; michael@12: michael@12: if (param.len > 0) { michael@12: if (result.len + param.len > MAX_URI_SIZE - 1) { michael@12: LM_ERR("URI is too long\n"); michael@12: continue; michael@12: } michael@12: new_result.s = &(new_uri[0]); michael@12: new_result.len = MAX_URI_SIZE; michael@12: if (add_uri_param(&result, ¶m, &new_result) == 0) { michael@12: LM_ERR("Parsing of URI <%.*s> failed\n", michael@12: result.len, result.s); michael@12: continue; michael@12: } michael@12: if (new_result.len > 0) { michael@12: result = new_result; michael@12: } michael@12: } michael@12: michael@12: if (first) { michael@12: if (set_ruri(_msg, &result) == -1) { michael@12: goto done; michael@12: } michael@12: set_ruri_q(q); michael@12: first = 0; michael@12: curr_prio = ((naptr->order) << 16) + naptr->pref; michael@12: } else { michael@12: priority = ((naptr->order) << 16) + naptr->pref; michael@12: if (priority > curr_prio) { michael@12: q = q - 10; michael@12: curr_prio = priority; michael@12: } michael@12: if (append_branch(_msg, &result, 0, 0, q, 0, 0) == -1) { michael@12: goto done; michael@12: } michael@12: } michael@12: } michael@12: michael@12: done: michael@12: free_rdata_list(head); michael@12: return first ? -1 : 1; michael@12: } michael@12: michael@12: michael@12: /* michael@12: * Call enum_query_2 with module parameter suffix and default service. michael@12: */ michael@12: int enum_query_0(struct sip_msg* _msg, char* _str1, char* _str2) michael@12: { michael@12: return enum_query_2(_msg, (char *)(&suffix), (char *)(&service)); michael@12: } michael@12: michael@12: michael@12: /* michael@12: * Call enum_query_2 with given suffix and default service. michael@12: */ michael@12: int enum_query_1(struct sip_msg* _msg, char* _suffix, char* _str2) michael@12: { michael@12: return enum_query_2(_msg, _suffix, (char *)(&service)); michael@12: } michael@12: michael@12: michael@12: /* michael@12: * See documentation in README file. michael@12: */ michael@12: int enum_query_2(struct sip_msg* _msg, char* _suffix, char* _service) michael@12: { michael@12: char *user_s; michael@12: int user_len, i, j; michael@12: char name[MAX_DOMAIN_SIZE]; michael@12: char string[17]; michael@12: michael@12: str *suffix, *service; michael@12: michael@12: suffix = (str*)_suffix; michael@12: service = (str*)_service; michael@12: michael@12: if (parse_sip_msg_uri(_msg) < 0) { michael@12: LM_ERR("Parsing of R-URI failed\n"); michael@12: return -1; michael@12: } michael@12: michael@12: if (is_e164(&(_msg->parsed_uri.user)) == -1) { michael@12: LM_ERR("R-URI user is not an E164 number\n"); michael@12: return -1; michael@12: } michael@12: michael@12: user_s = _msg->parsed_uri.user.s; michael@12: user_len = _msg->parsed_uri.user.len; michael@12: michael@12: memcpy(&(string[0]), user_s, user_len); michael@12: string[user_len] = (char)0; michael@12: michael@12: j = 0; michael@12: for (i = user_len - 1; i > 0; i--) { michael@12: name[j] = user_s[i]; michael@12: name[j + 1] = '.'; michael@12: j = j + 2; michael@12: } michael@12: michael@12: memcpy(name + j, suffix->s, suffix->len + 1); michael@12: michael@12: return do_query(_msg, string, name, service); michael@12: } michael@12: michael@12: michael@13: /* michael@13: * Call isn_query_2 with module parameter suffix and default service. michael@13: */ michael@13: int isn_query_0(struct sip_msg* _msg, char* _str1, char* _str2) michael@13: { michael@13: return isn_query_2(_msg, (char *)(&isnsuffix), (char *)(&service)); michael@13: } michael@13: michael@13: michael@13: /* michael@13: * Call isn_query_2 with given suffix and default service. michael@13: */ michael@13: int isn_query_1(struct sip_msg* _msg, char* _suffix, char* _str2) michael@13: { michael@13: return isn_query_2(_msg, _suffix, (char *)(&service)); michael@13: } michael@13: michael@13: michael@13: /* michael@13: * See documentation in README file. michael@13: */ michael@13: int isn_query_2(struct sip_msg* _msg, char* _suffix, char* _service) michael@13: { michael@13: char *user_s = NULL; michael@13: int user_len, i, j; michael@13: char name[MAX_DOMAIN_SIZE] = {0}; michael@13: char string[17] = {0}; michael@13: char szItad[17] = {0}; michael@13: size_t nItlen = 0; michael@13: michael@13: str *suffix, *service; michael@13: michael@13: suffix = (str*)_suffix; michael@13: service = (str*)_service; michael@13: michael@13: if (parse_sip_msg_uri(_msg) < 0) { michael@13: LM_ERR("Parsing of R-URI failed\n"); michael@13: return -1; michael@13: } michael@13: michael@13: user_s = _msg->parsed_uri.user.s; michael@13: user_len = _msg->parsed_uri.user.len; michael@13: michael@13: memcpy(&(string[0]), user_s, user_len); michael@13: string[user_len] = (char)0; michael@13: michael@13: /* Do primitive test for correct ISN format, */ michael@13: /* and set szItad to the ISN ITAD (RFC 3872/2871). */ michael@13: /* Correct ISN format guessed from freenum.org and IANA */ michael@13: /* doc http://www.iana.org/assignments/trip-parameters/ */ michael@13: { michael@13: char *pAster = strchr(string, '*'); michael@13: if (pAster && (nItlen = strspn(pAster + sizeof(char), "0123456789"))) michael@13: strncpy(szItad, pAster + sizeof(char), nItlen); michael@13: else { michael@13: LM_ERR("R-URI user does not contain a valid ISN\n"); michael@13: return -1; michael@13: } michael@13: } michael@13: michael@13: /* Ammend the original ENUM E.164 string logic to process */ michael@13: /* ISN numbers instead, which include a nonreversed ITAD. */ michael@13: i = user_len - nItlen - sizeof(char); /* Ex: *1212 */ michael@13: j = 0; michael@13: while (i--) { michael@13: name[j] = user_s[i]; michael@13: name[j + 1] = '.'; michael@13: j = j + 2; michael@13: } michael@13: michael@13: strcat(name + j, szItad); /* Copy the unreversed ITAD, */ michael@13: name[j + nItlen] = '.'; /* and append a trailing dot. */ michael@13: memcpy(name + j + nItlen + sizeof(char), suffix->s, suffix->len + 1); michael@13: michael@13: return do_query(_msg, string, name, service); michael@13: } michael@13: michael@13: michael@12: /*********** INFRASTRUCTURE ENUM ***************/ michael@12: michael@12: /* michael@12: * Call enum_query_2 with default suffix and service. michael@12: */ michael@12: int i_enum_query_0(struct sip_msg* _msg, char* _suffix, char* _service) michael@12: { michael@12: return i_enum_query_2(_msg, (char *)(&i_suffix), (char *)(&service)); michael@12: } michael@12: michael@12: /* michael@12: * Call enum_query_2 with given suffix and default service. michael@12: */ michael@12: int i_enum_query_1(struct sip_msg* _msg, char* _suffix, char* _service) michael@12: { michael@12: return i_enum_query_2(_msg, _suffix, (char *)(&service)); michael@12: } michael@12: michael@12: michael@12: int i_enum_query_2(struct sip_msg* _msg, char* _suffix, char* _service) michael@12: { michael@12: char *user_s; michael@12: int user_len, i, j; michael@12: char name[MAX_DOMAIN_SIZE]; michael@12: char apex[MAX_COMPONENT_SIZE + 1]; michael@12: char separator[MAX_COMPONENT_SIZE + 1]; michael@12: int sdl = 0; /* subdomain location: infrastructure enum offset */ michael@12: int cc_len; michael@12: struct rdata* head; michael@12: michael@12: char string[17]; michael@12: michael@12: str *suffix, *service; michael@12: michael@12: suffix = (str*)_suffix; michael@12: service = (str*)_service; michael@12: michael@12: if (parse_sip_msg_uri(_msg) < 0) { michael@12: LM_ERR("Parsing of R-URI failed\n"); michael@12: return -1; michael@12: } michael@12: michael@12: if (is_e164(&(_msg->parsed_uri.user)) == -1) { michael@12: LM_ERR("R-URI user is not an E164 number\n"); michael@12: return -1; michael@12: } michael@12: michael@12: user_s = _msg->parsed_uri.user.s; michael@12: user_len = _msg->parsed_uri.user.len; michael@12: michael@12: /* make sure we don't run out of space in strings */ michael@12: if (( 2*user_len + MAX_COMPONENT_SIZE + MAX_COMPONENT_SIZE + 4) > MAX_DOMAIN_SIZE) { michael@12: LM_ERR("Strings too long\n"); michael@12: return -1; michael@12: } michael@12: if ( i_branchlabel.len > MAX_COMPONENT_SIZE ) { michael@12: LM_ERR("i_branchlabel too long\n"); michael@12: return -1; michael@12: } michael@12: if ( suffix->len > MAX_COMPONENT_SIZE ) { michael@12: LM_ERR("Suffix too long\n"); michael@12: return -1; michael@12: } michael@12: michael@12: michael@12: memcpy(&(string[0]), user_s, user_len); michael@12: string[user_len] = (char)0; michael@12: michael@12: /* Set up parameters as for user-enum */ michael@12: memcpy(apex, suffix->s , suffix->len); michael@12: apex[suffix->len] = (char)0; michael@12: sdl = 0; /* where to insert i-enum separator */ michael@12: separator[0] = 0; /* don't insert anything */ michael@12: michael@12: cc_len = cclen(string + 1); michael@12: michael@12: if (!strncasecmp(i_bl_alg.s,"ebl",i_bl_alg.len)) { michael@12: sdl = cc_len; /* default */ michael@12: michael@12: j = 0; michael@12: memcpy(name, i_branchlabel.s, i_branchlabel.len); michael@12: j += i_branchlabel.len; michael@12: name[j++] = '.'; michael@12: michael@12: for (i = cc_len ; i > 0; i--) { michael@12: name[j++] = user_s[i]; michael@12: name[j++] = '.'; michael@12: } michael@12: memcpy(name + j, suffix->s, suffix->len + 1); michael@12: michael@12: LM_DBG("Looking for EBL record for %s.\n", name); michael@12: head = get_record(name, T_EBL); michael@12: if (head == 0) { michael@12: LM_DBG("No EBL found for %s. Defaulting to user ENUM.\n",name); michael@12: } else { michael@12: struct ebl_rdata* ebl; michael@12: ebl = (struct ebl_rdata *) head->rdata; michael@12: michael@12: LM_DBG("EBL record for %s is %d / %.*s / %.*s.\n", michael@12: name, ebl->position, (int)ebl->separator_len, michael@12: ebl->separator,(int)ebl->apex_len, ebl->apex); michael@12: michael@12: if ((ebl->apex_len > MAX_COMPONENT_SIZE) || (ebl->separator_len > MAX_COMPONENT_SIZE)) { michael@12: LM_ERR("EBL strings too long\n"); michael@12: return -1; michael@12: } michael@12: michael@12: if (ebl->position > 15) { michael@12: LM_ERR("EBL position too large (%d)\n", michael@12: ebl->position); michael@12: return -1; michael@12: } michael@12: michael@12: sdl = ebl->position; michael@12: michael@12: memcpy(separator, ebl->separator, ebl->separator_len); michael@12: separator[ebl->separator_len] = 0; michael@12: michael@12: memcpy(apex, ebl->apex, ebl->apex_len); michael@12: apex[ebl->apex_len] = 0; michael@12: free_rdata_list(head); michael@12: } michael@12: } else if (!strncasecmp(i_bl_alg.s,"txt",i_bl_alg.len)) { michael@12: sdl = cc_len; /* default */ michael@12: memcpy(separator, i_branchlabel.s, i_branchlabel.len); michael@12: separator[i_branchlabel.len] = 0; michael@12: /* no change to apex */ michael@12: michael@12: j = 0; michael@12: memcpy(name, i_branchlabel.s, i_branchlabel.len); michael@12: j += i_branchlabel.len; michael@12: name[j++] = '.'; michael@12: michael@12: for (i = cc_len ; i > 0; i--) { michael@12: name[j++] = user_s[i]; michael@12: name[j++] = '.'; michael@12: } michael@12: memcpy(name + j, suffix->s, suffix->len + 1); michael@12: michael@12: head = get_record(name, T_TXT); michael@12: if (head == 0) { michael@12: LM_DBG("TXT found for %s. Defaulting to %d\n", michael@12: name, cc_len); michael@12: } else { michael@12: sdl = atoi(((struct txt_rdata*)head->rdata)->txt); michael@12: LM_DBG("TXT record for %s is %d.\n", name, sdl); michael@12: michael@12: if ((sdl < 0) || (sdl > 10)) { michael@12: LM_ERR("Sdl %d out of bounds. Set back to cc_len.\n", sdl); michael@12: sdl = cc_len; michael@12: } michael@12: free_rdata_list(head); michael@12: } michael@12: } else { /* defaults to CC */ michael@12: sdl = cc_len; michael@12: memcpy(separator, i_branchlabel.s, i_branchlabel.len); michael@12: separator[i_branchlabel.len] = 0; michael@12: /* no change to apex */ michael@12: } michael@12: michael@12: j = 0; michael@12: sdl++; /* to avoid comparing i to (sdl+1) */ michael@12: for (i = user_len - 1; i > 0; i--) { michael@12: name[j] = user_s[i]; michael@12: name[j + 1] = '.'; michael@12: j = j + 2; michael@12: if (separator[0] && (i == sdl)) { /* insert the I-ENUM separator here? */ michael@12: strcpy(name + j, separator); /* we've checked string sizes. */ michael@12: j += strlen(separator); michael@12: name[j++] = '.'; michael@12: } michael@12: } michael@12: michael@12: memcpy(name + j, apex, strlen(apex)+1); michael@12: michael@12: return do_query(_msg, string, name, service); michael@12: } michael@12: michael@12: michael@12: michael@12: /******************* FQUERY *******************/ michael@12: michael@12: michael@12: /* michael@12: * Call enum_pv_query_3 with pv arg, module parameter suffix, michael@12: * and default service. michael@12: */ michael@12: int enum_pv_query_1(struct sip_msg* _msg, char* _sp) michael@12: { michael@12: return enum_pv_query_3(_msg, _sp, (char *)(&suffix), (char *)(&service)); michael@12: } michael@12: michael@12: /* michael@12: * Call enum_pv_query_3 with pv and suffix args and default service. michael@12: */ michael@12: int enum_pv_query_2(struct sip_msg* _msg, char* _sp, char* _suffix) michael@12: { michael@12: return enum_pv_query_3(_msg, _sp, _suffix, (char *)(&service)); michael@12: } michael@12: michael@12: /* michael@12: * See documentation in README file. michael@12: */ michael@12: michael@12: int enum_pv_query_3(struct sip_msg* _msg, char* _sp, char* _suffix, michael@12: char* _service) michael@12: { michael@12: char *user_s; michael@12: int user_len, i, j, first; michael@12: char name[MAX_DOMAIN_SIZE]; michael@12: char uri[MAX_URI_SIZE]; michael@12: char new_uri[MAX_URI_SIZE]; michael@12: unsigned int priority, curr_prio; michael@12: qvalue_t q; michael@12: char tostring[17]; michael@12: struct rdata* head; michael@12: struct rdata* l; michael@12: struct naptr_rdata* naptr; michael@12: str pattern, replacement, result, new_result; michael@12: str *suffix, *service; michael@12: char string[17]; michael@12: pv_spec_t *sp; michael@12: pv_value_t pv_val; michael@12: michael@12: sp = (pv_spec_t *)_sp; michael@12: suffix = (str*)_suffix; michael@12: service = (str*)_service; michael@12: michael@12: /* michael@12: * Get R-URI user to tostring michael@12: */ michael@12: if (parse_sip_msg_uri(_msg) < 0) { michael@12: LM_ERR("R-URI parsing failed\n"); michael@12: return -1; michael@12: } michael@12: michael@12: if (is_e164(&(_msg->parsed_uri.user)) == -1) { michael@12: LM_ERR("R-URI user is not an E164 number\n"); michael@12: return -1; michael@12: } michael@12: michael@12: user_s = _msg->parsed_uri.user.s; michael@12: user_len = _msg->parsed_uri.user.len; michael@12: michael@12: memcpy(&(tostring[0]), user_s, user_len); michael@12: tostring[user_len] = (char)0; michael@12: michael@12: /* michael@12: * Get E.164 number from pseudo variable michael@12: */ michael@12: if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) { michael@12: if (pv_val.flags & PV_VAL_STR) { michael@12: if (pv_val.rs.len == 0 || pv_val.rs.s == NULL) { michael@12: LM_DBG("Missing E.164 number\n"); michael@12: return -1; michael@12: } michael@12: } else { michael@12: LM_DBG("Pseudo variable value is not string\n"); michael@12: return -1; michael@12: } michael@12: } else { michael@12: LM_DBG("Cannot get pseudo variable value\n"); michael@12: return -1; michael@12: } michael@12: if (is_e164(&(pv_val.rs)) == -1) { michael@12: LM_ERR("pseudo variable does not contain an E164 number\n"); michael@12: return -1; michael@12: } michael@12: michael@12: user_s = pv_val.rs.s; michael@12: user_len = pv_val.rs.len; michael@12: michael@12: memcpy(&(string[0]), user_s, user_len); michael@12: string[user_len] = (char)0; michael@12: michael@12: j = 0; michael@12: for (i = user_len - 1; i > 0; i--) { michael@12: name[j] = user_s[i]; michael@12: name[j + 1] = '.'; michael@12: j = j + 2; michael@12: } michael@12: michael@12: memcpy(name + j, suffix->s, suffix->len + 1); michael@12: michael@12: head = get_record(name, T_NAPTR); michael@12: michael@12: if (head == 0) { michael@12: LM_DBG("No NAPTR record found for %s.\n", name); michael@12: return -1; michael@12: } michael@12: michael@12: naptr_sort(&head); michael@12: michael@12: q = MAX_Q - 10; michael@12: curr_prio = 0; michael@12: first = 1; michael@12: michael@12: for (l = head; l; l = l->next) { michael@12: michael@12: if (l->type != T_NAPTR) continue; /*should never happen*/ michael@12: naptr = (struct naptr_rdata*)l->rdata; michael@12: if (naptr == 0) { michael@12: LM_ERR("Null rdata in DNS response\n"); michael@12: continue; michael@12: } michael@12: michael@12: LM_DBG("ENUM query on %s: order %u, pref %u, flen %u, flags " michael@12: "'%.*s', slen %u, services '%.*s', rlen %u, " michael@12: "regexp '%.*s'\n", michael@12: name, naptr->order, naptr->pref, michael@12: naptr->flags_len, (int)(naptr->flags_len), ZSW(naptr->flags), michael@12: naptr->services_len, michael@12: (int)(naptr->services_len), ZSW(naptr->services), naptr->regexp_len, michael@12: (int)(naptr->regexp_len), ZSW(naptr->regexp)); michael@12: michael@12: if (sip_match(naptr, service) == 0) continue; michael@12: michael@12: if (parse_naptr_regexp(&(naptr->regexp[0]), naptr->regexp_len, michael@12: &pattern, &replacement) < 0) { michael@12: LM_ERR("Parsing of NAPTR regexp failed\n"); michael@12: continue; michael@12: } michael@12: result.s = &(uri[0]); michael@12: result.len = MAX_URI_SIZE; michael@12: /* Avoid making copies of pattern and replacement */ michael@12: pattern.s[pattern.len] = (char)0; michael@12: replacement.s[replacement.len] = (char)0; michael@12: if (reg_replace(pattern.s, replacement.s, &(tostring[0]), michael@12: &result) < 0) { michael@12: pattern.s[pattern.len] = '!'; michael@12: replacement.s[replacement.len] = '!'; michael@12: LM_ERR("Regexp replace failed\n"); michael@12: continue; michael@12: } michael@12: LM_DBG("Resulted in replacement: '%.*s'\n", michael@12: result.len, ZSW(result.s)); michael@12: pattern.s[pattern.len] = '!'; michael@12: replacement.s[replacement.len] = '!'; michael@12: michael@12: if (param.len > 0) { michael@12: if (result.len + param.len > MAX_URI_SIZE - 1) { michael@12: LM_ERR("URI is too long\n"); michael@12: continue; michael@12: } michael@12: new_result.s = &(new_uri[0]); michael@12: new_result.len = MAX_URI_SIZE; michael@12: if (add_uri_param(&result, ¶m, &new_result) == 0) { michael@12: LM_ERR("Parsing of URI <%.*s> failed\n", michael@12: result.len, result.s); michael@12: continue; michael@12: } michael@12: if (new_result.len > 0) { michael@12: result = new_result; michael@12: } michael@12: } michael@12: michael@12: if (first) { michael@12: if (set_ruri(_msg, &result) == -1) { michael@12: goto done; michael@12: } michael@12: set_ruri_q(q); michael@12: first = 0; michael@12: curr_prio = ((naptr->order) << 16) + naptr->pref; michael@12: } else { michael@12: priority = ((naptr->order) << 16) + naptr->pref; michael@12: if (priority > curr_prio) { michael@12: q = q - 10; michael@12: curr_prio = priority; michael@12: } michael@12: if (append_branch(_msg, &result, 0, 0, q, 0, 0) == -1) { michael@12: goto done; michael@12: } michael@12: } michael@12: } michael@12: michael@12: done: michael@12: free_rdata_list(head); michael@12: return first ? -1 : 1; michael@12: } michael@12: