acl.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. /*! \file
  19. *
  20. * \brief Various sorts of access control
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/network.h"
  30. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
  31. #include <fcntl.h>
  32. #include <net/route.h>
  33. #endif
  34. #if defined(SOLARIS)
  35. #include <sys/sockio.h>
  36. #include <net/if.h>
  37. #elif defined(HAVE_GETIFADDRS)
  38. #include <ifaddrs.h>
  39. #endif
  40. #include "asterisk/acl.h"
  41. #include "asterisk/channel.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/lock.h"
  44. #include "asterisk/srv.h"
  45. #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
  46. static int get_local_address(struct ast_sockaddr *ourip)
  47. {
  48. return -1;
  49. }
  50. #else
  51. static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
  52. {
  53. const char *address;
  54. int score;
  55. address = ast_inet_ntoa(sin->sin_addr);
  56. /* RFC 1700 alias for the local network */
  57. if (address[0] == '0') {
  58. score = -25;
  59. /* RFC 1700 localnet */
  60. } else if (strncmp(address, "127", 3) == 0) {
  61. score = -20;
  62. /* RFC 1918 non-public address space */
  63. } else if (strncmp(address, "10.", 3) == 0) {
  64. score = -5;
  65. /* RFC 1918 non-public address space */
  66. } else if (strncmp(address, "172", 3) == 0) {
  67. /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
  68. if (address[4] == '1' && address[5] >= '6' && address[6] == '.') {
  69. score = -5;
  70. /* 172.20.0.0 - 172.29.255.255, but not 172.200.0.0 - 172.255.255.255 nor 172.2.0.0 - 172.2.255.255 */
  71. } else if (address[4] == '2' && address[6] == '.') {
  72. score = -5;
  73. /* 172.30.0.0 - 172.31.255.255, but not 172.3.0.0 - 172.3.255.255 */
  74. } else if (address[4] == '3' && (address[5] == '0' || address[5] == '1')) {
  75. score = -5;
  76. /* All other 172 addresses are public */
  77. } else {
  78. score = 0;
  79. }
  80. /* RFC 2544 Benchmark test range (198.18.0.0 - 198.19.255.255, but not 198.180.0.0 - 198.199.255.255) */
  81. } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') {
  82. score = -10;
  83. /* RFC 1918 non-public address space */
  84. } else if (strncmp(address, "192.168", 7) == 0) {
  85. score = -5;
  86. /* RFC 3330 Zeroconf network */
  87. } else if (strncmp(address, "169.254", 7) == 0) {
  88. /*!\note Better score than a test network, but not quite as good as RFC 1918
  89. * address space. The reason is that some Linux distributions automatically
  90. * configure a Zeroconf address before trying DHCP, so we want to prefer a
  91. * DHCP lease to a Zeroconf address.
  92. */
  93. score = -10;
  94. /* RFC 3330 Test network */
  95. } else if (strncmp(address, "192.0.2.", 8) == 0) {
  96. score = -15;
  97. /* Every other address should be publically routable */
  98. } else {
  99. score = 0;
  100. }
  101. if (score > *best_score) {
  102. *best_score = score;
  103. memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
  104. }
  105. }
  106. static int get_local_address(struct ast_sockaddr *ourip)
  107. {
  108. int s, res = -1;
  109. #ifdef SOLARIS
  110. struct lifreq *ifr = NULL;
  111. struct lifnum ifn;
  112. struct lifconf ifc;
  113. struct sockaddr_in *sa;
  114. char *buf = NULL;
  115. int bufsz, x;
  116. #endif /* SOLARIS */
  117. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
  118. struct ifaddrs *ifap, *ifaphead;
  119. int rtnerr;
  120. const struct sockaddr_in *sin;
  121. #endif /* BSD_OR_LINUX */
  122. struct in_addr best_addr;
  123. int best_score = -100;
  124. memset(&best_addr, 0, sizeof(best_addr));
  125. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
  126. rtnerr = getifaddrs(&ifaphead);
  127. if (rtnerr) {
  128. perror(NULL);
  129. return -1;
  130. }
  131. #endif /* BSD_OR_LINUX */
  132. s = socket(AF_INET, SOCK_STREAM, 0);
  133. if (s > 0) {
  134. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
  135. for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
  136. if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
  137. sin = (const struct sockaddr_in *) ifap->ifa_addr;
  138. score_address(sin, &best_addr, &best_score);
  139. res = 0;
  140. if (best_score == 0) {
  141. break;
  142. }
  143. }
  144. }
  145. #endif /* BSD_OR_LINUX */
  146. /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
  147. #ifdef SOLARIS
  148. /* Get a count of interfaces on the machine */
  149. ifn.lifn_family = AF_INET;
  150. ifn.lifn_flags = 0;
  151. ifn.lifn_count = 0;
  152. if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
  153. close(s);
  154. return -1;
  155. }
  156. bufsz = ifn.lifn_count * sizeof(struct lifreq);
  157. if (!(buf = malloc(bufsz))) {
  158. close(s);
  159. return -1;
  160. }
  161. memset(buf, 0, bufsz);
  162. /* Get a list of interfaces on the machine */
  163. ifc.lifc_len = bufsz;
  164. ifc.lifc_buf = buf;
  165. ifc.lifc_family = AF_INET;
  166. ifc.lifc_flags = 0;
  167. if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
  168. close(s);
  169. free(buf);
  170. return -1;
  171. }
  172. for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
  173. sa = (struct sockaddr_in *)&(ifr->lifr_addr);
  174. score_address(sa, &best_addr, &best_score);
  175. res = 0;
  176. if (best_score == 0) {
  177. break;
  178. }
  179. }
  180. free(buf);
  181. #endif /* SOLARIS */
  182. close(s);
  183. }
  184. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
  185. freeifaddrs(ifaphead);
  186. #endif /* BSD_OR_LINUX */
  187. if (res == 0 && ourip) {
  188. ast_sockaddr_setnull(ourip);
  189. ourip->ss.ss_family = AF_INET;
  190. ((struct sockaddr_in *)&ourip->ss)->sin_addr = best_addr;
  191. }
  192. return res;
  193. }
  194. #endif /* HAVE_GETIFADDRS */
  195. /* Free HA structure */
  196. void ast_free_ha(struct ast_ha *ha)
  197. {
  198. struct ast_ha *hal;
  199. while (ha) {
  200. hal = ha;
  201. ha = ha->next;
  202. ast_free(hal);
  203. }
  204. }
  205. /* Free ACL list structure */
  206. struct ast_acl_list *ast_free_acl_list(struct ast_acl_list *acl_list)
  207. {
  208. struct ast_acl *current;
  209. if (!acl_list) {
  210. return NULL;
  211. }
  212. AST_LIST_LOCK(acl_list);
  213. while ((current = AST_LIST_REMOVE_HEAD(acl_list, list))) {
  214. ast_free_ha(current->acl);
  215. ast_free(current);
  216. }
  217. AST_LIST_UNLOCK(acl_list);
  218. AST_LIST_HEAD_DESTROY(acl_list);
  219. ast_free(acl_list);
  220. return NULL;
  221. }
  222. /* Copy HA structure */
  223. void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
  224. {
  225. ast_sockaddr_copy(&to->addr, &from->addr);
  226. ast_sockaddr_copy(&to->netmask, &from->netmask);
  227. to->sense = from->sense;
  228. }
  229. /* Create duplicate of ha structure */
  230. static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
  231. {
  232. struct ast_ha *new_ha;
  233. if ((new_ha = ast_calloc(1, sizeof(*new_ha)))) {
  234. /* Copy from original to new object */
  235. ast_copy_ha(original, new_ha);
  236. }
  237. return new_ha;
  238. }
  239. /* Create duplicate HA link list */
  240. /* Used in chan_sip2 templates */
  241. struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
  242. {
  243. struct ast_ha *start = original;
  244. struct ast_ha *ret = NULL;
  245. struct ast_ha *current, *prev = NULL;
  246. while (start) {
  247. current = ast_duplicate_ha(start); /* Create copy of this object */
  248. if (prev) {
  249. prev->next = current; /* Link previous to this object */
  250. }
  251. if (!ret) {
  252. ret = current; /* Save starting point */
  253. }
  254. start = start->next; /* Go to next object */
  255. prev = current; /* Save pointer to this object */
  256. }
  257. return ret; /* Return start of list */
  258. }
  259. static int acl_new(struct ast_acl **pointer, const char *name) {
  260. struct ast_acl *acl;
  261. if (!(acl = ast_calloc(1, sizeof(*acl)))) {
  262. return 1;
  263. }
  264. *pointer = acl;
  265. ast_copy_string(acl->name, name, ACL_NAME_LENGTH);
  266. return 0;
  267. }
  268. struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original)
  269. {
  270. struct ast_acl_list *clone;
  271. struct ast_acl *current_cursor;
  272. struct ast_acl *current_clone;
  273. /* Early return if we receive a duplication request for a NULL original. */
  274. if (!original) {
  275. return NULL;
  276. }
  277. if (!(clone = ast_calloc(1, sizeof(*clone)))) {
  278. ast_log(LOG_WARNING, "Failed to allocate ast_acl_list struct while cloning an ACL\n");
  279. return NULL;
  280. }
  281. AST_LIST_HEAD_INIT(clone);
  282. AST_LIST_LOCK(original);
  283. AST_LIST_TRAVERSE(original, current_cursor, list) {
  284. if ((acl_new(&current_clone, current_cursor->name))) {
  285. ast_log(LOG_WARNING, "Failed to allocate ast_acl struct while cloning an ACL.");
  286. continue;
  287. }
  288. /* Copy data from original ACL to clone ACL */
  289. current_clone->acl = ast_duplicate_ha_list(current_cursor->acl);
  290. current_clone->is_invalid = current_cursor->is_invalid;
  291. current_clone->is_realtime = current_cursor->is_realtime;
  292. AST_LIST_INSERT_TAIL(clone, current_clone, list);
  293. }
  294. AST_LIST_UNLOCK(original);
  295. return clone;
  296. }
  297. /*!
  298. * \brief
  299. * Isolate a 32-bit section of an IPv6 address
  300. *
  301. * An IPv6 address can be divided into 4 32-bit chunks. This gives
  302. * easy access to one of these chunks.
  303. *
  304. * \param sin6 A pointer to a struct sockaddr_in6
  305. * \param index Which 32-bit chunk to operate on. Must be in the range 0-3.
  306. */
  307. #define V6_WORD(sin6, index) ((uint32_t *)&((sin6)->sin6_addr))[(index)]
  308. /*!
  309. * \brief
  310. * Apply a netmask to an address and store the result in a separate structure.
  311. *
  312. * When dealing with IPv6 addresses, one cannot apply a netmask with a simple
  313. * logical and operation. Furthermore, the incoming address may be an IPv4 address
  314. * and need to be mapped properly before attempting to apply a rule.
  315. *
  316. * \param addr The IP address to apply the mask to.
  317. * \param netmask The netmask configured in the host access rule.
  318. * \param result The resultant address after applying the netmask to the given address
  319. * \retval 0 Successfully applied netmask
  320. * \reval -1 Failed to apply netmask
  321. */
  322. static int apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask,
  323. struct ast_sockaddr *result)
  324. {
  325. int res = 0;
  326. if (ast_sockaddr_is_ipv4(addr)) {
  327. struct sockaddr_in result4 = { 0, };
  328. struct sockaddr_in *addr4 = (struct sockaddr_in *) &addr->ss;
  329. struct sockaddr_in *mask4 = (struct sockaddr_in *) &netmask->ss;
  330. result4.sin_family = AF_INET;
  331. result4.sin_addr.s_addr = addr4->sin_addr.s_addr & mask4->sin_addr.s_addr;
  332. ast_sockaddr_from_sin(result, &result4);
  333. } else if (ast_sockaddr_is_ipv6(addr)) {
  334. struct sockaddr_in6 result6 = { 0, };
  335. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr->ss;
  336. struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *) &netmask->ss;
  337. int i;
  338. result6.sin6_family = AF_INET6;
  339. for (i = 0; i < 4; ++i) {
  340. V6_WORD(&result6, i) = V6_WORD(addr6, i) & V6_WORD(mask6, i);
  341. }
  342. memcpy(&result->ss, &result6, sizeof(result6));
  343. result->len = sizeof(result6);
  344. } else {
  345. /* Unsupported address scheme */
  346. res = -1;
  347. }
  348. return res;
  349. }
  350. /*!
  351. * \brief
  352. * Parse a netmask in CIDR notation
  353. *
  354. * \details
  355. * For a mask of an IPv4 address, this should be a number between 0 and 32. For
  356. * a mask of an IPv6 address, this should be a number between 0 and 128. This
  357. * function creates an IPv6 ast_sockaddr from the given netmask. For masks of
  358. * IPv4 addresses, this is accomplished by adding 96 to the original netmask.
  359. *
  360. * \param[out] addr The ast_sockaddr produced from the CIDR netmask
  361. * \param is_v4 Tells if the address we are masking is IPv4.
  362. * \param mask_str The CIDR mask to convert
  363. * \retval -1 Failure
  364. * \retval 0 Success
  365. */
  366. static int parse_cidr_mask(struct ast_sockaddr *addr, int is_v4, const char *mask_str)
  367. {
  368. int mask;
  369. if (sscanf(mask_str, "%30d", &mask) != 1) {
  370. return -1;
  371. }
  372. if (is_v4) {
  373. struct sockaddr_in sin;
  374. if (mask < 0 || mask > 32) {
  375. return -1;
  376. }
  377. memset(&sin, 0, sizeof(sin));
  378. sin.sin_family = AF_INET;
  379. /* If mask is 0, then we already have the
  380. * appropriate all 0s address in sin from
  381. * the above memset.
  382. */
  383. if (mask != 0) {
  384. sin.sin_addr.s_addr = htonl(0xFFFFFFFF << (32 - mask));
  385. }
  386. ast_sockaddr_from_sin(addr, &sin);
  387. } else {
  388. struct sockaddr_in6 sin6;
  389. int i;
  390. if (mask < 0 || mask > 128) {
  391. return -1;
  392. }
  393. memset(&sin6, 0, sizeof(sin6));
  394. sin6.sin6_family = AF_INET6;
  395. for (i = 0; i < 4; ++i) {
  396. /* Once mask reaches 0, we don't have
  397. * to explicitly set anything anymore
  398. * since sin6 was zeroed out already
  399. */
  400. if (mask > 0) {
  401. V6_WORD(&sin6, i) = htonl(0xFFFFFFFF << (mask < 32 ? (32 - mask) : 0));
  402. mask -= mask < 32 ? mask : 32;
  403. }
  404. }
  405. memcpy(&addr->ss, &sin6, sizeof(sin6));
  406. addr->len = sizeof(sin6);
  407. }
  408. return 0;
  409. }
  410. void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
  411. {
  412. struct ast_acl *acl = NULL;
  413. struct ast_acl *current;
  414. struct ast_acl_list *working_list;
  415. char *tmp, *list;
  416. /* If the ACL list is currently uninitialized, it must be initialized. */
  417. if (*path == NULL) {
  418. struct ast_acl_list *list;
  419. list = ast_calloc(1, sizeof(*list));
  420. if (!list) {
  421. /* Allocation Error */
  422. if (error) {
  423. *error = 1;
  424. }
  425. return;
  426. }
  427. AST_LIST_HEAD_INIT(list);
  428. *path = list;
  429. }
  430. working_list = *path;
  431. AST_LIST_LOCK(working_list);
  432. /* First we need to determine if we will need to add a new ACL node or if we can use an existing one. */
  433. if (strncasecmp(sense, "a", 1)) {
  434. /* The first element in the path should be the unnamed, base ACL. If that's the case, we use it. If not,
  435. * we have to make one and link it up appropriately. */
  436. current = AST_LIST_FIRST(working_list);
  437. if (!current || !ast_strlen_zero(current->name)) {
  438. if (acl_new(&acl, "")) {
  439. if (error) {
  440. *error = 1;
  441. }
  442. }
  443. // Need to INSERT the ACL at the head here.
  444. AST_LIST_INSERT_HEAD(working_list, acl, list);
  445. } else {
  446. /* If the first element was already the unnamed base ACL, we just use that one. */
  447. acl = current;
  448. }
  449. /* With the proper ACL set for modification, we can just pass this off to the ast_ha append function. */
  450. acl->acl = ast_append_ha(sense, stuff, acl->acl, error);
  451. AST_LIST_UNLOCK(working_list);
  452. return;
  453. }
  454. /* We are in ACL append mode, so we know we'll be adding one or more named ACLs. */
  455. list = ast_strdupa(stuff);
  456. while ((tmp = strsep(&list, ","))) {
  457. struct ast_ha *named_ha;
  458. int already_included = 0;
  459. /* Remove leading whitespace from the string in case the user put spaces between items */
  460. tmp = ast_skip_blanks(tmp);
  461. /* The first step is to check for a duplicate */
  462. AST_LIST_TRAVERSE(working_list, current, list) {
  463. if (!strcasecmp(current->name, tmp)) { /* ACL= */
  464. /* Inclusion of the same ACL multiple times isn't a catastrophic error, but it will raise the error flag and skip the entry. */
  465. ast_log(LOG_ERROR, "Named ACL '%s' is already included in the ast_acl container.", tmp);
  466. if (error) {
  467. *error = 1;
  468. }
  469. already_included = 1;
  470. break;
  471. }
  472. }
  473. if (already_included) {
  474. continue;
  475. }
  476. if (acl_new(&acl, tmp)) {
  477. /* This is a catastrophic allocation error and we'll return immediately if this happens. */
  478. if (error) {
  479. *error = 1;
  480. }
  481. AST_LIST_UNLOCK(working_list);
  482. return;
  483. }
  484. /* Attempt to grab the Named ACL we are looking for. */
  485. named_ha = ast_named_acl_find(tmp, &acl->is_realtime, &acl->is_invalid);
  486. /* Set the ACL's ast_ha to the duplicated named ACL retrieved above. */
  487. acl->acl = named_ha;
  488. /* Raise the named_acl_flag since we are adding a named ACL to the ACL container. */
  489. if (named_acl_flag) {
  490. *named_acl_flag = 1;
  491. }
  492. /* Now insert the new ACL at the end of the list. */
  493. AST_LIST_INSERT_TAIL(working_list, acl, list);
  494. }
  495. AST_LIST_UNLOCK(working_list);
  496. }
  497. int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
  498. {
  499. struct ast_acl *head;
  500. if (!acl_list) {
  501. return 1;
  502. }
  503. AST_LIST_LOCK(acl_list);
  504. head = AST_LIST_FIRST(acl_list);
  505. AST_LIST_UNLOCK(acl_list);
  506. if (head) {
  507. return 0;
  508. }
  509. return 1;
  510. }
  511. struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
  512. {
  513. struct ast_ha *ha;
  514. struct ast_ha *prev = NULL;
  515. struct ast_ha *ret;
  516. char *tmp, *list = ast_strdupa(stuff);
  517. char *address = NULL, *mask = NULL;
  518. int addr_is_v4;
  519. int allowing = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
  520. const char *parsed_addr, *parsed_mask;
  521. ret = path;
  522. while (path) {
  523. prev = path;
  524. path = path->next;
  525. }
  526. while ((tmp = strsep(&list, ","))) {
  527. if (!(ha = ast_calloc(1, sizeof(*ha)))) {
  528. if (error) {
  529. *error = 1;
  530. }
  531. return ret;
  532. }
  533. address = strsep(&tmp, "/");
  534. if (!address) {
  535. address = tmp;
  536. } else {
  537. mask = tmp;
  538. }
  539. if (*address == '!') {
  540. ha->sense = (allowing == AST_SENSE_DENY) ? AST_SENSE_ALLOW : AST_SENSE_DENY;
  541. address++;
  542. } else {
  543. ha->sense = allowing;
  544. }
  545. if (!ast_sockaddr_parse(&ha->addr, address, PARSE_PORT_FORBID)) {
  546. ast_log(LOG_WARNING, "Invalid IP address: %s\n", address);
  547. ast_free_ha(ha);
  548. if (error) {
  549. *error = 1;
  550. }
  551. return ret;
  552. }
  553. /* If someone specifies an IPv4-mapped IPv6 address,
  554. * we just convert this to an IPv4 ACL
  555. */
  556. if (ast_sockaddr_ipv4_mapped(&ha->addr, &ha->addr)) {
  557. ast_log(LOG_NOTICE, "IPv4-mapped ACL network address specified. "
  558. "Converting to an IPv4 ACL network address.\n");
  559. }
  560. addr_is_v4 = ast_sockaddr_is_ipv4(&ha->addr);
  561. if (!mask) {
  562. parse_cidr_mask(&ha->netmask, addr_is_v4, addr_is_v4 ? "32" : "128");
  563. } else if (strchr(mask, ':') || strchr(mask, '.')) {
  564. int mask_is_v4;
  565. /* Mask is of x.x.x.x or x:x:x:x:x:x:x:x variety */
  566. if (!ast_sockaddr_parse(&ha->netmask, mask, PARSE_PORT_FORBID)) {
  567. ast_log(LOG_WARNING, "Invalid netmask: %s\n", mask);
  568. ast_free_ha(ha);
  569. if (error) {
  570. *error = 1;
  571. }
  572. return ret;
  573. }
  574. /* If someone specifies an IPv4-mapped IPv6 netmask,
  575. * we just convert this to an IPv4 ACL
  576. */
  577. if (ast_sockaddr_ipv4_mapped(&ha->netmask, &ha->netmask)) {
  578. ast_log(LOG_NOTICE, "IPv4-mapped ACL netmask specified. "
  579. "Converting to an IPv4 ACL netmask.\n");
  580. }
  581. mask_is_v4 = ast_sockaddr_is_ipv4(&ha->netmask);
  582. if (addr_is_v4 ^ mask_is_v4) {
  583. ast_log(LOG_WARNING, "Address and mask are not using same address scheme.\n");
  584. ast_free_ha(ha);
  585. if (error) {
  586. *error = 1;
  587. }
  588. return ret;
  589. }
  590. } else if (parse_cidr_mask(&ha->netmask, addr_is_v4, mask)) {
  591. ast_log(LOG_WARNING, "Invalid CIDR netmask: %s\n", mask);
  592. ast_free_ha(ha);
  593. if (error) {
  594. *error = 1;
  595. }
  596. return ret;
  597. }
  598. if (apply_netmask(&ha->addr, &ha->netmask, &ha->addr)) {
  599. /* This shouldn't happen because ast_sockaddr_parse would
  600. * have failed much earlier on an unsupported address scheme
  601. */
  602. char *failmask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
  603. char *failaddr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
  604. ast_log(LOG_WARNING, "Unable to apply netmask %s to address %s\n", failmask, failaddr);
  605. ast_free_ha(ha);
  606. if (error) {
  607. *error = 1;
  608. }
  609. return ret;
  610. }
  611. if (prev) {
  612. prev->next = ha;
  613. } else {
  614. ret = ha;
  615. }
  616. prev = ha;
  617. parsed_addr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
  618. parsed_mask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
  619. ast_debug(3, "%s/%s sense %u appended to ACL\n", parsed_addr, parsed_mask, ha->sense);
  620. }
  621. return ret;
  622. }
  623. enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
  624. {
  625. struct ast_acl *acl;
  626. /* If the list is NULL, there are no rules, so we'll allow automatically. */
  627. if (!acl_list) {
  628. return AST_SENSE_ALLOW;
  629. }
  630. AST_LIST_LOCK(acl_list);
  631. AST_LIST_TRAVERSE(acl_list, acl, list) {
  632. if (acl->is_invalid) {
  633. /* In this case, the baseline ACL shouldn't ever trigger this, but if that somehow happens, it'll still be shown. */
  634. ast_log(LOG_WARNING, "%sRejecting '%s' due to use of an invalid ACL '%s'.\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
  635. ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
  636. AST_LIST_UNLOCK(acl_list);
  637. return AST_SENSE_DENY;
  638. }
  639. if (acl->acl) {
  640. if (ast_apply_ha(acl->acl, addr) == AST_SENSE_DENY) {
  641. ast_log(LOG_NOTICE, "%sRejecting '%s' due to a failure to pass ACL '%s'\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
  642. ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
  643. AST_LIST_UNLOCK(acl_list);
  644. return AST_SENSE_DENY;
  645. }
  646. }
  647. }
  648. AST_LIST_UNLOCK(acl_list);
  649. return AST_SENSE_ALLOW;
  650. }
  651. enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr)
  652. {
  653. /* Start optimistic */
  654. enum ast_acl_sense res = AST_SENSE_ALLOW;
  655. const struct ast_ha *current_ha;
  656. for (current_ha = ha; current_ha; current_ha = current_ha->next) {
  657. struct ast_sockaddr result;
  658. struct ast_sockaddr mapped_addr;
  659. const struct ast_sockaddr *addr_to_use;
  660. #if 0 /* debugging code */
  661. char iabuf[INET_ADDRSTRLEN];
  662. char iabuf2[INET_ADDRSTRLEN];
  663. /* DEBUG */
  664. ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
  665. ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
  666. ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
  667. #endif
  668. if (ast_sockaddr_is_ipv4(&current_ha->addr)) {
  669. if (ast_sockaddr_is_ipv6(addr)) {
  670. if (ast_sockaddr_is_ipv4_mapped(addr)) {
  671. /* IPv4 ACLs apply to IPv4-mapped addresses */
  672. if (!ast_sockaddr_ipv4_mapped(addr, &mapped_addr)) {
  673. ast_log(LOG_ERROR, "%s provided to ast_sockaddr_ipv4_mapped could not be converted. That shouldn't be possible.\n",
  674. ast_sockaddr_stringify(addr));
  675. continue;
  676. }
  677. addr_to_use = &mapped_addr;
  678. } else {
  679. /* An IPv4 ACL does not apply to an IPv6 address */
  680. continue;
  681. }
  682. } else {
  683. /* Address is IPv4 and ACL is IPv4. No biggie */
  684. addr_to_use = addr;
  685. }
  686. } else {
  687. if (ast_sockaddr_is_ipv6(addr) && !ast_sockaddr_is_ipv4_mapped(addr)) {
  688. addr_to_use = addr;
  689. } else {
  690. /* Address is IPv4 or IPv4 mapped but ACL is IPv6. Skip */
  691. continue;
  692. }
  693. }
  694. /* For each rule, if this address and the netmask = the net address
  695. apply the current rule */
  696. if (apply_netmask(addr_to_use, &current_ha->netmask, &result)) {
  697. /* Unlikely to happen since we know the address to be IPv4 or IPv6 */
  698. continue;
  699. }
  700. if (!ast_sockaddr_cmp_addr(&result, &current_ha->addr)) {
  701. res = current_ha->sense;
  702. }
  703. }
  704. return res;
  705. }
  706. static int resolve_first(struct ast_sockaddr *addr, const char *name, int flag,
  707. int family)
  708. {
  709. struct ast_sockaddr *addrs;
  710. int addrs_cnt;
  711. addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
  712. if (addrs_cnt > 0) {
  713. if (addrs_cnt > 1) {
  714. ast_debug(1, "Multiple addresses. Using the first only\n");
  715. }
  716. ast_sockaddr_copy(addr, &addrs[0]);
  717. ast_free(addrs);
  718. } else {
  719. ast_log(LOG_WARNING, "Unable to lookup '%s'\n", name);
  720. return -1;
  721. }
  722. return 0;
  723. }
  724. int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
  725. {
  726. char srv[256];
  727. char host[256];
  728. int srv_ret = 0;
  729. int tportno;
  730. if (service) {
  731. snprintf(srv, sizeof(srv), "%s.%s", service, hostname);
  732. if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, srv)) > 0) {
  733. hostname = host;
  734. }
  735. }
  736. if (resolve_first(addr, hostname, PARSE_PORT_FORBID, addr->ss.ss_family) != 0) {
  737. return -1;
  738. }
  739. if (srv_ret > 0) {
  740. ast_sockaddr_set_port(addr, tportno);
  741. }
  742. return 0;
  743. }
  744. struct dscp_codepoint {
  745. char *name;
  746. unsigned int space;
  747. };
  748. /* IANA registered DSCP codepoints */
  749. static const struct dscp_codepoint dscp_pool1[] = {
  750. { "CS0", 0x00 },
  751. { "CS1", 0x08 },
  752. { "CS2", 0x10 },
  753. { "CS3", 0x18 },
  754. { "CS4", 0x20 },
  755. { "CS5", 0x28 },
  756. { "CS6", 0x30 },
  757. { "CS7", 0x38 },
  758. { "AF11", 0x0A },
  759. { "AF12", 0x0C },
  760. { "AF13", 0x0E },
  761. { "AF21", 0x12 },
  762. { "AF22", 0x14 },
  763. { "AF23", 0x16 },
  764. { "AF31", 0x1A },
  765. { "AF32", 0x1C },
  766. { "AF33", 0x1E },
  767. { "AF41", 0x22 },
  768. { "AF42", 0x24 },
  769. { "AF43", 0x26 },
  770. { "EF", 0x2E },
  771. };
  772. int ast_str2cos(const char *value, unsigned int *cos)
  773. {
  774. int fval;
  775. if (sscanf(value, "%30d", &fval) == 1) {
  776. if (fval < 8) {
  777. *cos = fval;
  778. return 0;
  779. }
  780. }
  781. return -1;
  782. }
  783. int ast_str2tos(const char *value, unsigned int *tos)
  784. {
  785. int fval;
  786. unsigned int x;
  787. if (sscanf(value, "%30i", &fval) == 1) {
  788. *tos = fval & 0xFF;
  789. return 0;
  790. }
  791. for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
  792. if (!strcasecmp(value, dscp_pool1[x].name)) {
  793. *tos = dscp_pool1[x].space << 2;
  794. return 0;
  795. }
  796. }
  797. return -1;
  798. }
  799. const char *ast_tos2str(unsigned int tos)
  800. {
  801. unsigned int x;
  802. for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
  803. if (dscp_pool1[x].space == (tos >> 2)) {
  804. return dscp_pool1[x].name;
  805. }
  806. }
  807. return "unknown";
  808. }
  809. int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
  810. {
  811. return ast_get_ip_or_srv(addr, hostname, NULL);
  812. }
  813. int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
  814. {
  815. int port;
  816. int s;
  817. port = ast_sockaddr_port(us);
  818. if ((s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET,
  819. SOCK_DGRAM, 0)) < 0) {
  820. ast_log(LOG_ERROR, "Cannot create socket\n");
  821. return -1;
  822. }
  823. if (ast_connect(s, them)) {
  824. ast_log(LOG_WARNING, "Cannot connect\n");
  825. close(s);
  826. return -1;
  827. }
  828. if (ast_getsockname(s, us)) {
  829. ast_log(LOG_WARNING, "Cannot get socket name\n");
  830. close(s);
  831. return -1;
  832. }
  833. close(s);
  834. {
  835. const char *them_addr = ast_strdupa(ast_sockaddr_stringify_addr(them));
  836. const char *us_addr = ast_strdupa(ast_sockaddr_stringify_addr(us));
  837. ast_debug(3, "For destination '%s', our source address is '%s'.\n",
  838. them_addr, us_addr);
  839. }
  840. ast_sockaddr_set_port(us, port);
  841. return 0;
  842. }
  843. int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
  844. {
  845. char ourhost[MAXHOSTNAMELEN] = "";
  846. struct ast_sockaddr root;
  847. int res, port = ast_sockaddr_port(ourip);
  848. /* just use the bind address if it is nonzero */
  849. if (!ast_sockaddr_is_any(bindaddr)) {
  850. ast_sockaddr_copy(ourip, bindaddr);
  851. ast_debug(3, "Attached to given IP address\n");
  852. return 0;
  853. }
  854. /* try to use our hostname */
  855. if (gethostname(ourhost, sizeof(ourhost) - 1)) {
  856. ast_log(LOG_WARNING, "Unable to get hostname\n");
  857. } else {
  858. if (resolve_first(ourip, ourhost, PARSE_PORT_FORBID, family) == 0) {
  859. /* reset port since resolve_first wipes this out */
  860. ast_sockaddr_set_port(ourip, port);
  861. return 0;
  862. }
  863. }
  864. ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
  865. /* A.ROOT-SERVERS.NET. */
  866. if (!resolve_first(&root, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID, 0) &&
  867. !ast_ouraddrfor(&root, ourip)) {
  868. /* reset port since resolve_first wipes this out */
  869. ast_sockaddr_set_port(ourip, port);
  870. return 0;
  871. }
  872. res = get_local_address(ourip);
  873. ast_sockaddr_set_port(ourip, port);
  874. return res;
  875. }