res_pjsip_endpoint_identifier_ip.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include "asterisk/res_pjsip.h"
  26. #include "asterisk/res_pjsip_cli.h"
  27. #include "asterisk/module.h"
  28. #include "asterisk/acl.h"
  29. #include "asterisk/manager.h"
  30. #include "res_pjsip/include/res_pjsip_private.h"
  31. /*** DOCUMENTATION
  32. <configInfo name="res_pjsip_endpoint_identifier_ip" language="en_US">
  33. <synopsis>Module that identifies endpoints</synopsis>
  34. <configFile name="pjsip.conf">
  35. <configObject name="identify">
  36. <synopsis>Identifies endpoints via some criteria.</synopsis>
  37. <description>
  38. <para>This module provides alternatives to matching inbound requests to
  39. a configured endpoint. At least one of the matching mechanisms
  40. must be provided, or the object configuration is invalid.</para>
  41. <para>The matching mechanisms are provided by the following
  42. configuration options:</para>
  43. <enumlist>
  44. <enum name="match"><para>Match by source IP address.</para></enum>
  45. <enum name="match_header"><para>Match by SIP header.</para></enum>
  46. </enumlist>
  47. <note><para>If multiple matching criteria are provided then an inbound
  48. request will be matched to the endpoint if it matches
  49. <emphasis>any</emphasis> of the criteria.</para></note>
  50. </description>
  51. <configOption name="endpoint">
  52. <synopsis>Name of endpoint identified</synopsis>
  53. </configOption>
  54. <configOption name="match">
  55. <synopsis>IP addresses or networks to match against.</synopsis>
  56. <description>
  57. <para>The value is a comma-delimited list of IP addresses or
  58. hostnames. IP addresses may have a subnet mask appended. The
  59. subnet mask may be written in either CIDR or dotted-decimal
  60. notation. Separate the IP address and subnet mask with a slash
  61. ('/').
  62. </para>
  63. </description>
  64. </configOption>
  65. <configOption name="srv_lookups" default="yes">
  66. <synopsis>Perform SRV lookups for provided hostnames.</synopsis>
  67. <description>
  68. <para>When enabled, <replaceable>srv_lookups</replaceable> will
  69. perform SRV lookups for _sip._udp, _sip._tcp, and _sips._tcp of
  70. the given hostnames to determine additional addresses that traffic
  71. may originate from.
  72. </para>
  73. </description>
  74. </configOption>
  75. <configOption name="match_header">
  76. <synopsis>Header/value pair to match against.</synopsis>
  77. <description>
  78. <para>A SIP header whose value is used to match against. SIP
  79. requests containing the header, along with the specified value,
  80. will be mapped to the specified endpoint. The header must be
  81. specified with a <literal>:</literal>, as in
  82. <literal>match_header = SIPHeader: value</literal>.
  83. </para>
  84. <para>The specified SIP header value can be a regular
  85. expression if the value is of the form
  86. /<replaceable>regex</replaceable>/.
  87. </para>
  88. <note><para>Use of a regex is expensive so be sure you need
  89. to use a regex to match your endpoint.
  90. </para></note>
  91. </description>
  92. </configOption>
  93. <configOption name="type">
  94. <synopsis>Must be of type 'identify'.</synopsis>
  95. </configOption>
  96. </configObject>
  97. </configFile>
  98. </configInfo>
  99. ***/
  100. /*! \brief The number of buckets for storing hosts for resolution */
  101. #define HOSTS_BUCKETS 53
  102. /*! \brief Structure for an IP identification matching object */
  103. struct ip_identify_match {
  104. /*! \brief Sorcery object details */
  105. SORCERY_OBJECT(details);
  106. /*! \brief Stringfields */
  107. AST_DECLARE_STRING_FIELDS(
  108. /*! The name of the endpoint */
  109. AST_STRING_FIELD(endpoint_name);
  110. /*! If matching by header, the header/value to match against */
  111. AST_STRING_FIELD(match_header);
  112. /*! SIP header name of the match_header string */
  113. AST_STRING_FIELD(match_header_name);
  114. /*! SIP header value of the match_header string */
  115. AST_STRING_FIELD(match_header_value);
  116. );
  117. /*! Compiled match_header regular expression when is_regex is non-zero */
  118. regex_t regex_buf;
  119. /*! \brief Networks or addresses that should match this */
  120. struct ast_ha *matches;
  121. /*! \brief Hosts to be resolved when applying configuration */
  122. struct ao2_container *hosts;
  123. /*! \brief Perform SRV resolution of hostnames */
  124. unsigned int srv_lookups;
  125. /*! Non-zero if match_header has a regular expression (i.e., regex_buf is valid) */
  126. unsigned int is_regex:1;
  127. };
  128. /*! \brief Destructor function for a matching object */
  129. static void ip_identify_destroy(void *obj)
  130. {
  131. struct ip_identify_match *identify = obj;
  132. ast_string_field_free_memory(identify);
  133. ast_free_ha(identify->matches);
  134. ao2_cleanup(identify->hosts);
  135. if (identify->is_regex) {
  136. regfree(&identify->regex_buf);
  137. }
  138. }
  139. /*! \brief Allocator function for a matching object */
  140. static void *ip_identify_alloc(const char *name)
  141. {
  142. struct ip_identify_match *identify = ast_sorcery_generic_alloc(sizeof(*identify), ip_identify_destroy);
  143. if (!identify || ast_string_field_init(identify, 256)) {
  144. ao2_cleanup(identify);
  145. return NULL;
  146. }
  147. return identify;
  148. }
  149. /*! \brief Comparator function for matching an object by header */
  150. static int header_identify_match_check(void *obj, void *arg, int flags)
  151. {
  152. struct ip_identify_match *identify = obj;
  153. struct pjsip_rx_data *rdata = arg;
  154. pjsip_hdr *header;
  155. pj_str_t pj_header_name;
  156. int header_present;
  157. if (ast_strlen_zero(identify->match_header)) {
  158. return 0;
  159. }
  160. pj_header_name = pj_str((void *) identify->match_header_name);
  161. /* Check all headers of the given name for a match. */
  162. header_present = 0;
  163. for (header = NULL;
  164. (header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &pj_header_name, header));
  165. header = header->next) {
  166. char *pos;
  167. int len;
  168. char buf[PATH_MAX];
  169. header_present = 1;
  170. /* Print header line to buf */
  171. len = pjsip_hdr_print_on(header, buf, sizeof(buf) - 1);
  172. if (len < 0) {
  173. /* Buffer not large enough or no header vptr! */
  174. ast_assert(0);
  175. continue;
  176. }
  177. buf[len] = '\0';
  178. /* Remove header name from pj_buf and trim blanks. */
  179. pos = strchr(buf, ':');
  180. if (!pos) {
  181. /* No header name? Bug in PJPROJECT if so. */
  182. ast_assert(0);
  183. continue;
  184. }
  185. pos = ast_strip(pos + 1);
  186. /* Does header value match what we are looking for? */
  187. if (identify->is_regex) {
  188. if (!regexec(&identify->regex_buf, pos, 0, NULL, 0)) {
  189. return CMP_MATCH;
  190. }
  191. } else if (!strcmp(identify->match_header_value, pos)) {
  192. return CMP_MATCH;
  193. }
  194. ast_debug(3, "Identify '%s': SIP message has '%s' header but value '%s' does not match '%s'.\n",
  195. ast_sorcery_object_get_id(identify),
  196. identify->match_header_name,
  197. pos,
  198. identify->match_header_value);
  199. }
  200. if (!header_present) {
  201. ast_debug(3, "Identify '%s': SIP message does not have '%s' header.\n",
  202. ast_sorcery_object_get_id(identify),
  203. identify->match_header_name);
  204. }
  205. return 0;
  206. }
  207. /*! \brief Comparator function for matching an object by IP address */
  208. static int ip_identify_match_check(void *obj, void *arg, int flags)
  209. {
  210. struct ip_identify_match *identify = obj;
  211. struct ast_sockaddr *addr = arg;
  212. int sense;
  213. sense = ast_apply_ha(identify->matches, addr);
  214. if (sense != AST_SENSE_ALLOW) {
  215. ast_debug(3, "Source address %s matches identify '%s'\n",
  216. ast_sockaddr_stringify(addr),
  217. ast_sorcery_object_get_id(identify));
  218. return CMP_MATCH;
  219. } else {
  220. ast_debug(3, "Source address %s does not match identify '%s'\n",
  221. ast_sockaddr_stringify(addr),
  222. ast_sorcery_object_get_id(identify));
  223. return 0;
  224. }
  225. }
  226. static struct ast_sip_endpoint *common_identify(ao2_callback_fn *identify_match_cb, void *arg)
  227. {
  228. RAII_VAR(struct ao2_container *, candidates, NULL, ao2_cleanup);
  229. struct ip_identify_match *match;
  230. struct ast_sip_endpoint *endpoint;
  231. /* If no possibilities exist return early to save some time */
  232. candidates = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify",
  233. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  234. if (!candidates || !ao2_container_count(candidates)) {
  235. ast_debug(3, "No identify sections to match against\n");
  236. return NULL;
  237. }
  238. match = ao2_callback(candidates, 0, identify_match_cb, arg);
  239. if (!match) {
  240. return NULL;
  241. }
  242. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
  243. match->endpoint_name);
  244. if (endpoint) {
  245. ast_debug(3, "Identify '%s' SIP message matched to endpoint %s\n",
  246. ast_sorcery_object_get_id(match), match->endpoint_name);
  247. } else {
  248. ast_log(LOG_WARNING, "Identify '%s' points to endpoint '%s' but endpoint could not be found\n",
  249. ast_sorcery_object_get_id(match), match->endpoint_name);
  250. }
  251. ao2_ref(match, -1);
  252. return endpoint;
  253. }
  254. static struct ast_sip_endpoint *ip_identify(pjsip_rx_data *rdata)
  255. {
  256. struct ast_sockaddr addr = { { 0, } };
  257. ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
  258. ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
  259. return common_identify(ip_identify_match_check, &addr);
  260. }
  261. static struct ast_sip_endpoint_identifier ip_identifier = {
  262. .identify_endpoint = ip_identify,
  263. };
  264. static struct ast_sip_endpoint *header_identify(pjsip_rx_data *rdata)
  265. {
  266. return common_identify(header_identify_match_check, rdata);
  267. }
  268. static struct ast_sip_endpoint_identifier header_identifier = {
  269. .identify_endpoint = header_identify,
  270. };
  271. /*! \brief Helper function which performs a host lookup and adds result to identify match */
  272. static int ip_identify_match_host_lookup(struct ip_identify_match *identify, const char *host)
  273. {
  274. struct ast_sockaddr *addrs;
  275. int num_addrs = 0, error = 0, i;
  276. int results = 0;
  277. num_addrs = ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC);
  278. if (!num_addrs) {
  279. return -1;
  280. }
  281. for (i = 0; i < num_addrs; ++i) {
  282. /* Check if the address is already in the list, if so don't add it again */
  283. if (identify->matches && (ast_apply_ha(identify->matches, &addrs[i]) != AST_SENSE_ALLOW)) {
  284. continue;
  285. }
  286. /* We deny what we actually want to match because there is an implicit permit all rule for ACLs */
  287. identify->matches = ast_append_ha("d", ast_sockaddr_stringify_addr(&addrs[i]), identify->matches, &error);
  288. if (!identify->matches || error) {
  289. results = -1;
  290. break;
  291. }
  292. results += 1;
  293. }
  294. ast_free(addrs);
  295. return results;
  296. }
  297. /*! \brief Helper function which performs an SRV lookup and then resolves the hostname */
  298. static int ip_identify_match_srv_lookup(struct ip_identify_match *identify, const char *prefix, const char *host, int results)
  299. {
  300. char service[NI_MAXHOST];
  301. struct srv_context *context = NULL;
  302. int srv_ret;
  303. const char *srvhost;
  304. unsigned short srvport;
  305. snprintf(service, sizeof(service), "%s.%s", prefix, host);
  306. while (!(srv_ret = ast_srv_lookup(&context, service, &srvhost, &srvport))) {
  307. int hosts;
  308. /* In the case of the SRV lookup we don't care if it fails, we will output a log message
  309. * when we fallback to a normal lookup.
  310. */
  311. hosts = ip_identify_match_host_lookup(identify, srvhost);
  312. if (hosts == -1) {
  313. results = -1;
  314. break;
  315. } else {
  316. results += hosts;
  317. }
  318. }
  319. ast_srv_cleanup(&context);
  320. return results;
  321. }
  322. /*! \brief Custom handler for match field */
  323. static int ip_identify_match_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  324. {
  325. struct ip_identify_match *identify = obj;
  326. char *input_string = ast_strdupa(var->value);
  327. char *current_string;
  328. if (ast_strlen_zero(var->value)) {
  329. return 0;
  330. }
  331. while ((current_string = ast_strip(strsep(&input_string, ",")))) {
  332. char *mask = strrchr(current_string, '/');
  333. int error = 0;
  334. if (ast_strlen_zero(current_string)) {
  335. continue;
  336. }
  337. if (mask) {
  338. identify->matches = ast_append_ha("d", current_string, identify->matches, &error);
  339. if (!identify->matches || error) {
  340. ast_log(LOG_ERROR, "Failed to add address '%s' to ip endpoint identifier '%s'\n",
  341. current_string, ast_sorcery_object_get_id(obj));
  342. return -1;
  343. }
  344. continue;
  345. }
  346. if (!identify->hosts) {
  347. identify->hosts = ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, HOSTS_BUCKETS);
  348. if (!identify->hosts) {
  349. ast_log(LOG_ERROR, "Failed to create container to store hosts on ip endpoint identifier '%s'\n",
  350. ast_sorcery_object_get_id(obj));
  351. return -1;
  352. }
  353. }
  354. error = ast_str_container_add(identify->hosts, current_string);
  355. if (error) {
  356. ast_log(LOG_ERROR, "Failed to store host '%s' for resolution on ip endpoint identifier '%s'\n",
  357. current_string, ast_sorcery_object_get_id(obj));
  358. return -1;
  359. }
  360. }
  361. return 0;
  362. }
  363. /*! \brief Apply handler for identify type */
  364. static int ip_identify_apply(const struct ast_sorcery *sorcery, void *obj)
  365. {
  366. struct ip_identify_match *identify = obj;
  367. char *current_string;
  368. struct ao2_iterator i;
  369. /* Validate the identify object configuration */
  370. if (ast_strlen_zero(identify->endpoint_name)) {
  371. ast_log(LOG_ERROR, "Identify '%s' missing required endpoint name.\n",
  372. ast_sorcery_object_get_id(identify));
  373. return -1;
  374. }
  375. if (ast_strlen_zero(identify->match_header) /* No header to match */
  376. /* and no static IP addresses with a mask */
  377. && !identify->matches
  378. /* and no addresses to resolve */
  379. && (!identify->hosts || !ao2_container_count(identify->hosts))) {
  380. ast_log(LOG_ERROR, "Identify '%s' is not configured to match anything.\n",
  381. ast_sorcery_object_get_id(identify));
  382. return -1;
  383. }
  384. if (!ast_strlen_zero(identify->match_header)) {
  385. char *c_header;
  386. char *c_value;
  387. int len;
  388. /* Split the header name and value */
  389. c_header = ast_strdupa(identify->match_header);
  390. c_value = strchr(c_header, ':');
  391. if (!c_value) {
  392. ast_log(LOG_ERROR, "Identify '%s' missing ':' separator in match_header '%s'.\n",
  393. ast_sorcery_object_get_id(identify), identify->match_header);
  394. return -1;
  395. }
  396. *c_value = '\0';
  397. c_value = ast_strip(c_value + 1);
  398. c_header = ast_strip(c_header);
  399. if (ast_strlen_zero(c_header)) {
  400. ast_log(LOG_ERROR, "Identify '%s' has no SIP header to match in match_header '%s'.\n",
  401. ast_sorcery_object_get_id(identify), identify->match_header);
  402. return -1;
  403. }
  404. if (!strcmp(c_value, "//")) {
  405. /* An empty regex is the same as an empty literal string. */
  406. c_value = "";
  407. }
  408. if (ast_string_field_set(identify, match_header_name, c_header)
  409. || ast_string_field_set(identify, match_header_value, c_value)) {
  410. return -1;
  411. }
  412. len = strlen(c_value);
  413. if (2 < len && c_value[0] == '/' && c_value[len - 1] == '/') {
  414. /* Make "/regex/" into "regex" */
  415. c_value[len - 1] = '\0';
  416. ++c_value;
  417. if (regcomp(&identify->regex_buf, c_value, REG_EXTENDED | REG_NOSUB)) {
  418. ast_log(LOG_ERROR, "Identify '%s' failed to compile match_header regex '%s'.\n",
  419. ast_sorcery_object_get_id(identify), c_value);
  420. return -1;
  421. }
  422. identify->is_regex = 1;
  423. }
  424. }
  425. if (!identify->hosts) {
  426. /* No match addresses to resolve */
  427. return 0;
  428. }
  429. /* Resolve the match addresses now */
  430. i = ao2_iterator_init(identify->hosts, 0);
  431. while ((current_string = ao2_iterator_next(&i))) {
  432. struct ast_sockaddr address;
  433. int results = 0;
  434. /* If the provided string is not an IP address perform SRV resolution on it */
  435. if (identify->srv_lookups && !ast_sockaddr_parse(&address, current_string, 0)) {
  436. results = ip_identify_match_srv_lookup(identify, "_sip._udp", current_string,
  437. results);
  438. if (results != -1) {
  439. results = ip_identify_match_srv_lookup(identify, "_sip._tcp",
  440. current_string, results);
  441. }
  442. if (results != -1) {
  443. results = ip_identify_match_srv_lookup(identify, "_sips._tcp",
  444. current_string, results);
  445. }
  446. }
  447. /* If SRV fails fall back to a normal lookup on the host itself */
  448. if (!results) {
  449. results = ip_identify_match_host_lookup(identify, current_string);
  450. }
  451. if (results == 0) {
  452. ast_log(LOG_WARNING, "Identify '%s' provided address '%s' did not resolve to any address\n",
  453. ast_sorcery_object_get_id(identify), current_string);
  454. } else if (results == -1) {
  455. ast_log(LOG_ERROR, "Identify '%s' failed when adding resolution results of '%s'\n",
  456. ast_sorcery_object_get_id(identify), current_string);
  457. ao2_ref(current_string, -1);
  458. ao2_iterator_destroy(&i);
  459. return -1;
  460. }
  461. ao2_ref(current_string, -1);
  462. }
  463. ao2_iterator_destroy(&i);
  464. ao2_ref(identify->hosts, -1);
  465. identify->hosts = NULL;
  466. return 0;
  467. }
  468. static int match_to_str(const void *obj, const intptr_t *args, char **buf)
  469. {
  470. RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
  471. const struct ip_identify_match *identify = obj;
  472. ast_ha_join(identify->matches, &str);
  473. *buf = ast_strdup(ast_str_buffer(str));
  474. return 0;
  475. }
  476. static int match_to_var_list(const void *obj, struct ast_variable **fields)
  477. {
  478. char str[MAX_OBJECT_FIELD];
  479. const struct ip_identify_match *identify = obj;
  480. struct ast_variable *head = NULL;
  481. struct ast_ha *ha = identify->matches;
  482. for (; ha; ha = ha->next) {
  483. const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
  484. snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
  485. addr, ast_sockaddr_stringify_addr(&ha->netmask));
  486. ast_variable_list_append(&head, ast_variable_new("match", str, ""));
  487. }
  488. if (head) {
  489. *fields = head;
  490. }
  491. return 0;
  492. }
  493. static int sip_identify_to_ami(const struct ip_identify_match *identify,
  494. struct ast_str **buf)
  495. {
  496. return ast_sip_sorcery_object_to_ami(identify, buf);
  497. }
  498. static int send_identify_ami_event(void *obj, void *arg, void *data, int flags)
  499. {
  500. struct ip_identify_match *identify = obj;
  501. const char *endpoint_name = arg;
  502. struct ast_sip_ami *ami = data;
  503. struct ast_str *buf;
  504. /* Build AMI event */
  505. buf = ast_sip_create_ami_event("IdentifyDetail", ami);
  506. if (!buf) {
  507. return CMP_STOP;
  508. }
  509. if (sip_identify_to_ami(identify, &buf)) {
  510. ast_free(buf);
  511. return CMP_STOP;
  512. }
  513. ast_str_append(&buf, 0, "EndpointName: %s\r\n", endpoint_name);
  514. /* Send AMI event */
  515. astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
  516. ++ami->count;
  517. ast_free(buf);
  518. return 0;
  519. }
  520. static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint,
  521. struct ast_sip_ami *ami)
  522. {
  523. struct ao2_container *identifies;
  524. struct ast_variable fields = {
  525. .name = "endpoint",
  526. .value = ast_sorcery_object_get_id(endpoint),
  527. };
  528. identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify",
  529. AST_RETRIEVE_FLAG_MULTIPLE, &fields);
  530. if (!identifies) {
  531. return -1;
  532. }
  533. /* Build and send any found identify object's AMI IdentifyDetail event. */
  534. ao2_callback_data(identifies, OBJ_MULTIPLE | OBJ_NODATA,
  535. send_identify_ami_event,
  536. (void *) ast_sorcery_object_get_id(endpoint),
  537. ami);
  538. ao2_ref(identifies, -1);
  539. return 0;
  540. }
  541. struct ast_sip_endpoint_formatter endpoint_identify_formatter = {
  542. .format_ami = format_ami_endpoint_identify
  543. };
  544. static int cli_iterator(void *container, ao2_callback_fn callback, void *args)
  545. {
  546. const struct ast_sip_endpoint *endpoint = container;
  547. struct ao2_container *identifies;
  548. struct ast_variable fields = {
  549. .name = "endpoint",
  550. .value = ast_sorcery_object_get_id(endpoint),
  551. .next = NULL,
  552. };
  553. identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify",
  554. AST_RETRIEVE_FLAG_MULTIPLE, &fields);
  555. if (!identifies) {
  556. return -1;
  557. }
  558. ao2_callback(identifies, OBJ_NODATA, callback, args);
  559. ao2_cleanup(identifies);
  560. return 0;
  561. }
  562. static struct ao2_container *cli_get_container(const char *regex)
  563. {
  564. RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
  565. struct ao2_container *s_container;
  566. container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "identify", regex);
  567. if (!container) {
  568. return NULL;
  569. }
  570. s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  571. ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
  572. if (!s_container) {
  573. return NULL;
  574. }
  575. if (ao2_container_dup(s_container, container, 0)) {
  576. ao2_ref(s_container, -1);
  577. return NULL;
  578. }
  579. return s_container;
  580. }
  581. static void *cli_retrieve_by_id(const char *id)
  582. {
  583. return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "identify", id);
  584. }
  585. static int cli_print_header(void *obj, void *arg, int flags)
  586. {
  587. struct ast_sip_cli_context *context = arg;
  588. int indent = CLI_INDENT_TO_SPACES(context->indent_level);
  589. int filler = CLI_MAX_WIDTH - indent - 22;
  590. ast_assert(context->output_buffer != NULL);
  591. ast_str_append(&context->output_buffer, 0,
  592. "%*s: <Identify/Endpoint%*.*s>\n",
  593. indent, "Identify", filler, filler, CLI_HEADER_FILLER);
  594. if (context->recurse) {
  595. context->indent_level++;
  596. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  597. filler = CLI_LAST_TABSTOP - indent - 24;
  598. ast_str_append(&context->output_buffer, 0,
  599. "%*s: <criteria%*.*s>\n",
  600. indent, "Match", filler, filler, CLI_HEADER_FILLER);
  601. context->indent_level--;
  602. }
  603. return 0;
  604. }
  605. static int cli_print_body(void *obj, void *arg, int flags)
  606. {
  607. RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
  608. struct ip_identify_match *ident = obj;
  609. struct ast_sip_cli_context *context = arg;
  610. struct ast_ha *match;
  611. int indent;
  612. ast_assert(context->output_buffer != NULL);
  613. ast_str_append(&context->output_buffer, 0, "%*s: %s/%s\n",
  614. CLI_INDENT_TO_SPACES(context->indent_level), "Identify",
  615. ast_sorcery_object_get_id(ident), ident->endpoint_name);
  616. if (context->recurse) {
  617. context->indent_level++;
  618. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  619. for (match = ident->matches; match; match = match->next) {
  620. const char *addr = ast_sockaddr_stringify_addr(&match->addr);
  621. ast_str_append(&context->output_buffer, 0, "%*s: %s%s/%d\n",
  622. indent,
  623. "Match",
  624. match->sense == AST_SENSE_ALLOW ? "!" : "",
  625. addr, ast_sockaddr_cidr_bits(&match->netmask));
  626. }
  627. if (!ast_strlen_zero(ident->match_header)) {
  628. ast_str_append(&context->output_buffer, 0, "%*s: %s\n",
  629. indent,
  630. "Match",
  631. ident->match_header);
  632. }
  633. context->indent_level--;
  634. if (context->indent_level == 0) {
  635. ast_str_append(&context->output_buffer, 0, "\n");
  636. }
  637. }
  638. if (context->show_details
  639. || (context->show_details_only_level_0 && context->indent_level == 0)) {
  640. ast_str_append(&context->output_buffer, 0, "\n");
  641. ast_sip_cli_print_sorcery_objectset(ident, context, 0);
  642. }
  643. return 0;
  644. }
  645. /*
  646. * A function pointer to callback needs to be within the
  647. * module in order to avoid problems with an undefined
  648. * symbol when the module is loaded.
  649. */
  650. static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd,
  651. struct ast_cli_args *a)
  652. {
  653. return ast_sip_cli_traverse_objects(e, cmd, a);
  654. }
  655. static struct ast_cli_entry cli_identify[] = {
  656. AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Identifies",
  657. .command = "pjsip list identifies",
  658. .usage = "Usage: pjsip list identifies [ like <pattern> ]\n"
  659. " List the configured PJSIP Identifies\n"
  660. " Optional regular expression pattern is used to filter the list.\n"),
  661. AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identifies",
  662. .command = "pjsip show identifies",
  663. .usage = "Usage: pjsip show identifies [ like <pattern> ]\n"
  664. " Show the configured PJSIP Identifies\n"
  665. " Optional regular expression pattern is used to filter the list.\n"),
  666. AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identify",
  667. .command = "pjsip show identify",
  668. .usage = "Usage: pjsip show identify <id>\n"
  669. " Show the configured PJSIP Identify\n"),
  670. };
  671. static struct ast_sip_cli_formatter_entry *cli_formatter;
  672. static int load_module(void)
  673. {
  674. CHECK_PJSIP_MODULE_LOADED();
  675. ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_endpoint_identifier_ip");
  676. ast_sorcery_apply_default(ast_sip_get_sorcery(), "identify", "config", "pjsip.conf,criteria=type=identify");
  677. if (ast_sorcery_object_register(ast_sip_get_sorcery(), "identify", ip_identify_alloc, NULL, ip_identify_apply)) {
  678. return AST_MODULE_LOAD_DECLINE;
  679. }
  680. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "type", "", OPT_NOOP_T, 0, 0);
  681. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, endpoint_name));
  682. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "identify", "match", "", ip_identify_match_handler, match_to_str, match_to_var_list, 0, 0);
  683. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "match_header", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, match_header));
  684. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "srv_lookups", "yes", OPT_BOOL_T, 1, FLDSET(struct ip_identify_match, srv_lookups));
  685. ast_sorcery_load_object(ast_sip_get_sorcery(), "identify");
  686. ast_sip_register_endpoint_identifier_with_name(&ip_identifier, "ip");
  687. ast_sip_register_endpoint_identifier_with_name(&header_identifier, "header");
  688. ast_sip_register_endpoint_formatter(&endpoint_identify_formatter);
  689. cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
  690. if (!cli_formatter) {
  691. ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
  692. return AST_MODULE_LOAD_DECLINE;
  693. }
  694. cli_formatter->name = "identify";
  695. cli_formatter->print_header = cli_print_header;
  696. cli_formatter->print_body = cli_print_body;
  697. cli_formatter->get_container = cli_get_container;
  698. cli_formatter->iterate = cli_iterator;
  699. cli_formatter->get_id = ast_sorcery_object_get_id;
  700. cli_formatter->retrieve_by_id = cli_retrieve_by_id;
  701. ast_sip_register_cli_formatter(cli_formatter);
  702. ast_cli_register_multiple(cli_identify, ARRAY_LEN(cli_identify));
  703. return AST_MODULE_LOAD_SUCCESS;
  704. }
  705. static int reload_module(void)
  706. {
  707. ast_sorcery_reload_object(ast_sip_get_sorcery(), "identify");
  708. return 0;
  709. }
  710. static int unload_module(void)
  711. {
  712. ast_cli_unregister_multiple(cli_identify, ARRAY_LEN(cli_identify));
  713. ast_sip_unregister_cli_formatter(cli_formatter);
  714. ast_sip_unregister_endpoint_formatter(&endpoint_identify_formatter);
  715. ast_sip_unregister_endpoint_identifier(&header_identifier);
  716. ast_sip_unregister_endpoint_identifier(&ip_identifier);
  717. return 0;
  718. }
  719. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP IP endpoint identifier",
  720. .support_level = AST_MODULE_SUPPORT_CORE,
  721. .load = load_module,
  722. .reload = reload_module,
  723. .unload = unload_module,
  724. .load_pri = AST_MODPRI_CHANNEL_DEPEND - 4,
  725. );