acl.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 (!current) {
  249. ast_free_ha(ret);
  250. return NULL;
  251. }
  252. if (prev) {
  253. prev->next = current; /* Link previous to this object */
  254. }
  255. if (!ret) {
  256. ret = current; /* Save starting point */
  257. }
  258. start = start->next; /* Go to next object */
  259. prev = current; /* Save pointer to this object */
  260. }
  261. return ret; /* Return start of list */
  262. }
  263. static int acl_new(struct ast_acl **pointer, const char *name) {
  264. struct ast_acl *acl;
  265. if (!(acl = ast_calloc(1, sizeof(*acl)))) {
  266. return 1;
  267. }
  268. *pointer = acl;
  269. ast_copy_string(acl->name, name, ACL_NAME_LENGTH);
  270. return 0;
  271. }
  272. struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original)
  273. {
  274. struct ast_acl_list *clone;
  275. struct ast_acl *current_cursor;
  276. struct ast_acl *current_clone;
  277. /* Early return if we receive a duplication request for a NULL original. */
  278. if (!original) {
  279. return NULL;
  280. }
  281. if (!(clone = ast_calloc(1, sizeof(*clone)))) {
  282. ast_log(LOG_ERROR, "Failed to allocate ast_acl_list struct while cloning an ACL\n");
  283. return NULL;
  284. }
  285. AST_LIST_HEAD_INIT(clone);
  286. AST_LIST_LOCK(original);
  287. AST_LIST_TRAVERSE(original, current_cursor, list) {
  288. if ((acl_new(&current_clone, current_cursor->name))) {
  289. ast_log(LOG_ERROR, "Failed to allocate ast_acl struct while cloning an ACL.\n");
  290. ast_free_acl_list(clone);
  291. clone = NULL;
  292. break;
  293. }
  294. /* Copy data from original ACL to clone ACL */
  295. current_clone->acl = ast_duplicate_ha_list(current_cursor->acl);
  296. current_clone->is_invalid = current_cursor->is_invalid;
  297. current_clone->is_realtime = current_cursor->is_realtime;
  298. AST_LIST_INSERT_TAIL(clone, current_clone, list);
  299. if (current_cursor->acl && !current_clone->acl) {
  300. /* Deal with failure after adding to clone so we don't have to free
  301. * current_clone separately. */
  302. ast_log(LOG_ERROR, "Failed to duplicate HA list while cloning ACL.\n");
  303. ast_free_acl_list(clone);
  304. clone = NULL;
  305. break;
  306. }
  307. }
  308. AST_LIST_UNLOCK(original);
  309. return clone;
  310. }
  311. /*!
  312. * \brief
  313. * Parse a netmask in CIDR notation
  314. *
  315. * \details
  316. * For a mask of an IPv4 address, this should be a number between 0 and 32. For
  317. * a mask of an IPv6 address, this should be a number between 0 and 128. This
  318. * function creates an IPv6 ast_sockaddr from the given netmask. For masks of
  319. * IPv4 addresses, this is accomplished by adding 96 to the original netmask.
  320. *
  321. * \param[out] addr The ast_sockaddr produced from the CIDR netmask
  322. * \param is_v4 Tells if the address we are masking is IPv4.
  323. * \param mask_str The CIDR mask to convert
  324. * \retval -1 Failure
  325. * \retval 0 Success
  326. */
  327. static int parse_cidr_mask(struct ast_sockaddr *addr, int is_v4, const char *mask_str)
  328. {
  329. int mask;
  330. if (sscanf(mask_str, "%30d", &mask) != 1) {
  331. return -1;
  332. }
  333. if (is_v4) {
  334. struct sockaddr_in sin;
  335. if (mask < 0 || mask > 32) {
  336. return -1;
  337. }
  338. memset(&sin, 0, sizeof(sin));
  339. sin.sin_family = AF_INET;
  340. /* If mask is 0, then we already have the
  341. * appropriate all 0s address in sin from
  342. * the above memset.
  343. */
  344. if (mask != 0) {
  345. sin.sin_addr.s_addr = htonl(0xFFFFFFFF << (32 - mask));
  346. }
  347. ast_sockaddr_from_sin(addr, &sin);
  348. } else {
  349. struct sockaddr_in6 sin6;
  350. int i;
  351. if (mask < 0 || mask > 128) {
  352. return -1;
  353. }
  354. memset(&sin6, 0, sizeof(sin6));
  355. sin6.sin6_family = AF_INET6;
  356. for (i = 0; i < 4; ++i) {
  357. /* Once mask reaches 0, we don't have
  358. * to explicitly set anything anymore
  359. * since sin6 was zeroed out already
  360. */
  361. if (mask > 0) {
  362. V6_WORD(&sin6, i) = htonl(0xFFFFFFFF << (mask < 32 ? (32 - mask) : 0));
  363. mask -= mask < 32 ? mask : 32;
  364. }
  365. }
  366. memcpy(&addr->ss, &sin6, sizeof(sin6));
  367. addr->len = sizeof(sin6);
  368. }
  369. return 0;
  370. }
  371. void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
  372. {
  373. struct ast_acl *acl = NULL;
  374. struct ast_acl *current;
  375. struct ast_acl_list *working_list;
  376. char *tmp, *list;
  377. /* If the ACL list is currently uninitialized, it must be initialized. */
  378. if (*path == NULL) {
  379. struct ast_acl_list *list;
  380. list = ast_calloc(1, sizeof(*list));
  381. if (!list) {
  382. /* Allocation Error */
  383. if (error) {
  384. *error = 1;
  385. }
  386. return;
  387. }
  388. AST_LIST_HEAD_INIT(list);
  389. *path = list;
  390. }
  391. working_list = *path;
  392. AST_LIST_LOCK(working_list);
  393. /* First we need to determine if we will need to add a new ACL node or if we can use an existing one. */
  394. if (strncasecmp(sense, "a", 1)) {
  395. /* The first element in the path should be the unnamed, base ACL. If that's the case, we use it. If not,
  396. * we have to make one and link it up appropriately. */
  397. current = AST_LIST_FIRST(working_list);
  398. if (!current || !ast_strlen_zero(current->name)) {
  399. if (acl_new(&acl, "")) {
  400. if (error) {
  401. *error = 1;
  402. }
  403. AST_LIST_UNLOCK(working_list);
  404. return;
  405. }
  406. // Need to INSERT the ACL at the head here.
  407. AST_LIST_INSERT_HEAD(working_list, acl, list);
  408. } else {
  409. /* If the first element was already the unnamed base ACL, we just use that one. */
  410. acl = current;
  411. }
  412. /* With the proper ACL set for modification, we can just pass this off to the ast_ha append function. */
  413. acl->acl = ast_append_ha(sense, stuff, acl->acl, error);
  414. AST_LIST_UNLOCK(working_list);
  415. return;
  416. }
  417. /* We are in ACL append mode, so we know we'll be adding one or more named ACLs. */
  418. list = ast_strdupa(stuff);
  419. while ((tmp = strsep(&list, ","))) {
  420. struct ast_ha *named_ha;
  421. int already_included = 0;
  422. /* Remove leading whitespace from the string in case the user put spaces between items */
  423. tmp = ast_skip_blanks(tmp);
  424. /* The first step is to check for a duplicate */
  425. AST_LIST_TRAVERSE(working_list, current, list) {
  426. if (!strcasecmp(current->name, tmp)) { /* ACL= */
  427. /* Inclusion of the same ACL multiple times isn't a catastrophic error, but it will raise the error flag and skip the entry. */
  428. ast_log(LOG_ERROR, "Named ACL '%s' occurs multiple times in ACL definition. "
  429. "Please update your ACL configuration.\n", tmp);
  430. if (error) {
  431. *error = 1;
  432. }
  433. already_included = 1;
  434. break;
  435. }
  436. }
  437. if (already_included) {
  438. continue;
  439. }
  440. if (acl_new(&acl, tmp)) {
  441. /* This is a catastrophic allocation error and we'll return immediately if this happens. */
  442. if (error) {
  443. *error = 1;
  444. }
  445. AST_LIST_UNLOCK(working_list);
  446. return;
  447. }
  448. /* Attempt to grab the Named ACL we are looking for. */
  449. named_ha = ast_named_acl_find(tmp, &acl->is_realtime, &acl->is_invalid);
  450. /* Set the ACL's ast_ha to the duplicated named ACL retrieved above. */
  451. acl->acl = named_ha;
  452. /* Raise the named_acl_flag since we are adding a named ACL to the ACL container. */
  453. if (named_acl_flag) {
  454. *named_acl_flag = 1;
  455. }
  456. /* Now insert the new ACL at the end of the list. */
  457. AST_LIST_INSERT_TAIL(working_list, acl, list);
  458. }
  459. AST_LIST_UNLOCK(working_list);
  460. }
  461. int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
  462. {
  463. struct ast_acl *head;
  464. if (!acl_list) {
  465. return 1;
  466. }
  467. AST_LIST_LOCK(acl_list);
  468. head = AST_LIST_FIRST(acl_list);
  469. AST_LIST_UNLOCK(acl_list);
  470. if (head) {
  471. return 0;
  472. }
  473. return 1;
  474. }
  475. /*!
  476. * \internal
  477. * \brief Used by ast_append_ha to avoid ast_strdupa in a loop.
  478. *
  479. * \note This function is only called at debug level 3 and higher.
  480. */
  481. static void debug_ha_sense_appended(struct ast_ha *ha)
  482. {
  483. const char *parsed_mask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
  484. ast_log(LOG_DEBUG, "%s/%s sense %u appended to ACL\n",
  485. ast_sockaddr_stringify(&ha->addr),
  486. parsed_mask,
  487. ha->sense);
  488. }
  489. struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
  490. {
  491. struct ast_ha *ha;
  492. struct ast_ha *prev = NULL;
  493. struct ast_ha *ret;
  494. char *tmp, *list = ast_strdupa(stuff);
  495. char *address = NULL, *mask = NULL;
  496. int addr_is_v4;
  497. int allowing = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
  498. ret = path;
  499. while (path) {
  500. prev = path;
  501. path = path->next;
  502. }
  503. while ((tmp = strsep(&list, ","))) {
  504. if (!(ha = ast_calloc(1, sizeof(*ha)))) {
  505. if (error) {
  506. *error = 1;
  507. }
  508. return ret;
  509. }
  510. address = strsep(&tmp, "/");
  511. if (!address) {
  512. address = tmp;
  513. } else {
  514. mask = tmp;
  515. }
  516. if (*address == '!') {
  517. ha->sense = (allowing == AST_SENSE_DENY) ? AST_SENSE_ALLOW : AST_SENSE_DENY;
  518. address++;
  519. } else {
  520. ha->sense = allowing;
  521. }
  522. if (!ast_sockaddr_parse(&ha->addr, address, PARSE_PORT_FORBID)) {
  523. ast_log(LOG_WARNING, "Invalid IP address: %s\n", address);
  524. ast_free_ha(ha);
  525. if (error) {
  526. *error = 1;
  527. }
  528. return ret;
  529. }
  530. /* If someone specifies an IPv4-mapped IPv6 address,
  531. * we just convert this to an IPv4 ACL
  532. */
  533. if (ast_sockaddr_ipv4_mapped(&ha->addr, &ha->addr)) {
  534. ast_log(LOG_NOTICE, "IPv4-mapped ACL network address specified. "
  535. "Converting to an IPv4 ACL network address.\n");
  536. }
  537. addr_is_v4 = ast_sockaddr_is_ipv4(&ha->addr);
  538. if (!mask) {
  539. parse_cidr_mask(&ha->netmask, addr_is_v4, addr_is_v4 ? "32" : "128");
  540. } else if (strchr(mask, ':') || strchr(mask, '.')) {
  541. int mask_is_v4;
  542. /* Mask is of x.x.x.x or x:x:x:x:x:x:x:x variety */
  543. if (!ast_sockaddr_parse(&ha->netmask, mask, PARSE_PORT_FORBID)) {
  544. ast_log(LOG_WARNING, "Invalid netmask: %s\n", mask);
  545. ast_free_ha(ha);
  546. if (error) {
  547. *error = 1;
  548. }
  549. return ret;
  550. }
  551. /* If someone specifies an IPv4-mapped IPv6 netmask,
  552. * we just convert this to an IPv4 ACL
  553. */
  554. if (ast_sockaddr_ipv4_mapped(&ha->netmask, &ha->netmask)) {
  555. ast_log(LOG_NOTICE, "IPv4-mapped ACL netmask specified. "
  556. "Converting to an IPv4 ACL netmask.\n");
  557. }
  558. mask_is_v4 = ast_sockaddr_is_ipv4(&ha->netmask);
  559. if (addr_is_v4 ^ mask_is_v4) {
  560. ast_log(LOG_WARNING, "Address and mask are not using same address scheme.\n");
  561. ast_free_ha(ha);
  562. if (error) {
  563. *error = 1;
  564. }
  565. return ret;
  566. }
  567. } else if (parse_cidr_mask(&ha->netmask, addr_is_v4, mask)) {
  568. ast_log(LOG_WARNING, "Invalid CIDR netmask: %s\n", mask);
  569. ast_free_ha(ha);
  570. if (error) {
  571. *error = 1;
  572. }
  573. return ret;
  574. }
  575. if (ast_sockaddr_apply_netmask(&ha->addr, &ha->netmask, &ha->addr)) {
  576. /* This shouldn't happen because ast_sockaddr_parse would
  577. * have failed much earlier on an unsupported address scheme
  578. */
  579. char *failmask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
  580. char *failaddr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
  581. ast_log(LOG_WARNING, "Unable to apply netmask %s to address %s\n", failmask, failaddr);
  582. ast_free_ha(ha);
  583. if (error) {
  584. *error = 1;
  585. }
  586. return ret;
  587. }
  588. if (prev) {
  589. prev->next = ha;
  590. } else {
  591. ret = ha;
  592. }
  593. prev = ha;
  594. if (DEBUG_ATLEAST(3)) {
  595. debug_ha_sense_appended(ha);
  596. }
  597. }
  598. return ret;
  599. }
  600. void ast_ha_join(const struct ast_ha *ha, struct ast_str **buf)
  601. {
  602. for (; ha; ha = ha->next) {
  603. ast_str_append(buf, 0, "%s%s/",
  604. ha->sense == AST_SENSE_ALLOW ? "!" : "",
  605. ast_sockaddr_stringify_addr(&ha->addr));
  606. /* Separated to avoid duplicating stringified addresses. */
  607. ast_str_append(buf, 0, "%s", ast_sockaddr_stringify_addr(&ha->netmask));
  608. if (ha->next) {
  609. ast_str_append(buf, 0, ",");
  610. }
  611. }
  612. }
  613. void ast_ha_join_cidr(const struct ast_ha *ha, struct ast_str **buf)
  614. {
  615. for (; ha; ha = ha->next) {
  616. const char *addr = ast_sockaddr_stringify_addr(&ha->addr);
  617. ast_str_append(buf, 0, "%s%s/%d",
  618. ha->sense == AST_SENSE_ALLOW ? "!" : "",
  619. addr, ast_sockaddr_cidr_bits(&ha->netmask));
  620. if (ha->next) {
  621. ast_str_append(buf, 0, ",");
  622. }
  623. }
  624. }
  625. enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
  626. {
  627. struct ast_acl *acl;
  628. /* If the list is NULL, there are no rules, so we'll allow automatically. */
  629. if (!acl_list) {
  630. return AST_SENSE_ALLOW;
  631. }
  632. AST_LIST_LOCK(acl_list);
  633. AST_LIST_TRAVERSE(acl_list, acl, list) {
  634. if (acl->is_invalid) {
  635. /* In this case, the baseline ACL shouldn't ever trigger this, but if that somehow happens, it'll still be shown. */
  636. ast_log(LOG_WARNING, "%sRejecting '%s' due to use of an invalid ACL '%s'.\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
  637. ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
  638. AST_LIST_UNLOCK(acl_list);
  639. return AST_SENSE_DENY;
  640. }
  641. if (acl->acl) {
  642. if (ast_apply_ha(acl->acl, addr) == AST_SENSE_DENY) {
  643. ast_log(LOG_NOTICE, "%sRejecting '%s' due to a failure to pass ACL '%s'\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
  644. ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
  645. AST_LIST_UNLOCK(acl_list);
  646. return AST_SENSE_DENY;
  647. }
  648. }
  649. }
  650. AST_LIST_UNLOCK(acl_list);
  651. return AST_SENSE_ALLOW;
  652. }
  653. enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr)
  654. {
  655. /* Start optimistic */
  656. enum ast_acl_sense res = AST_SENSE_ALLOW;
  657. const struct ast_ha *current_ha;
  658. for (current_ha = ha; current_ha; current_ha = current_ha->next) {
  659. struct ast_sockaddr result;
  660. struct ast_sockaddr mapped_addr;
  661. const struct ast_sockaddr *addr_to_use;
  662. #if 0 /* debugging code */
  663. char iabuf[INET_ADDRSTRLEN];
  664. char iabuf2[INET_ADDRSTRLEN];
  665. /* DEBUG */
  666. ast_copy_string(iabuf, ast_sockaddr_stringify(addr), sizeof(iabuf));
  667. ast_copy_string(iabuf2, ast_sockaddr_stringify(&current_ha->addr), sizeof(iabuf2));
  668. ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
  669. #endif
  670. if (ast_sockaddr_is_ipv4(&current_ha->addr)) {
  671. if (ast_sockaddr_is_ipv6(addr)) {
  672. if (ast_sockaddr_is_ipv4_mapped(addr)) {
  673. /* IPv4 ACLs apply to IPv4-mapped addresses */
  674. if (!ast_sockaddr_ipv4_mapped(addr, &mapped_addr)) {
  675. ast_log(LOG_ERROR, "%s provided to ast_sockaddr_ipv4_mapped could not be converted. That shouldn't be possible.\n",
  676. ast_sockaddr_stringify(addr));
  677. continue;
  678. }
  679. addr_to_use = &mapped_addr;
  680. } else {
  681. /* An IPv4 ACL does not apply to an IPv6 address */
  682. continue;
  683. }
  684. } else {
  685. /* Address is IPv4 and ACL is IPv4. No biggie */
  686. addr_to_use = addr;
  687. }
  688. } else {
  689. if (ast_sockaddr_is_ipv6(addr) && !ast_sockaddr_is_ipv4_mapped(addr)) {
  690. addr_to_use = addr;
  691. } else {
  692. /* Address is IPv4 or IPv4 mapped but ACL is IPv6. Skip */
  693. continue;
  694. }
  695. }
  696. /* For each rule, if this address and the netmask = the net address
  697. apply the current rule */
  698. if (ast_sockaddr_apply_netmask(addr_to_use, &current_ha->netmask, &result)) {
  699. /* Unlikely to happen since we know the address to be IPv4 or IPv6 */
  700. continue;
  701. }
  702. if (!ast_sockaddr_cmp_addr(&result, &current_ha->addr)) {
  703. res = current_ha->sense;
  704. }
  705. }
  706. return res;
  707. }
  708. static int resolve_first(struct ast_sockaddr *addr, const char *name, int flag,
  709. int family)
  710. {
  711. struct ast_sockaddr *addrs;
  712. int addrs_cnt;
  713. addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
  714. if (addrs_cnt > 0) {
  715. if (addrs_cnt > 1) {
  716. ast_debug(1, "Multiple addresses. Using the first only\n");
  717. }
  718. ast_sockaddr_copy(addr, &addrs[0]);
  719. ast_free(addrs);
  720. } else {
  721. ast_log(LOG_WARNING, "Unable to lookup '%s'\n", name);
  722. return -1;
  723. }
  724. return 0;
  725. }
  726. int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
  727. {
  728. char srv[256];
  729. char host[256];
  730. int srv_ret = 0;
  731. int tportno;
  732. if (service) {
  733. snprintf(srv, sizeof(srv), "%s.%s", service, hostname);
  734. if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, srv)) > 0) {
  735. hostname = host;
  736. }
  737. }
  738. if (resolve_first(addr, hostname, PARSE_PORT_FORBID, addr->ss.ss_family) != 0) {
  739. return -1;
  740. }
  741. if (srv_ret > 0) {
  742. ast_sockaddr_set_port(addr, tportno);
  743. }
  744. return 0;
  745. }
  746. struct dscp_codepoint {
  747. char *name;
  748. unsigned int space;
  749. };
  750. /* IANA registered DSCP codepoints */
  751. static const struct dscp_codepoint dscp_pool1[] = {
  752. { "CS0", 0x00 },
  753. { "CS1", 0x08 },
  754. { "CS2", 0x10 },
  755. { "CS3", 0x18 },
  756. { "CS4", 0x20 },
  757. { "CS5", 0x28 },
  758. { "CS6", 0x30 },
  759. { "CS7", 0x38 },
  760. { "AF11", 0x0A },
  761. { "AF12", 0x0C },
  762. { "AF13", 0x0E },
  763. { "AF21", 0x12 },
  764. { "AF22", 0x14 },
  765. { "AF23", 0x16 },
  766. { "AF31", 0x1A },
  767. { "AF32", 0x1C },
  768. { "AF33", 0x1E },
  769. { "AF41", 0x22 },
  770. { "AF42", 0x24 },
  771. { "AF43", 0x26 },
  772. { "EF", 0x2E },
  773. };
  774. int ast_str2cos(const char *value, unsigned int *cos)
  775. {
  776. int fval;
  777. if (sscanf(value, "%30d", &fval) == 1) {
  778. if (fval < 8) {
  779. *cos = fval;
  780. return 0;
  781. }
  782. }
  783. return -1;
  784. }
  785. int ast_str2tos(const char *value, unsigned int *tos)
  786. {
  787. int fval;
  788. unsigned int x;
  789. if (sscanf(value, "%30i", &fval) == 1) {
  790. *tos = fval & 0xFF;
  791. return 0;
  792. }
  793. for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
  794. if (!strcasecmp(value, dscp_pool1[x].name)) {
  795. *tos = dscp_pool1[x].space << 2;
  796. return 0;
  797. }
  798. }
  799. return -1;
  800. }
  801. const char *ast_tos2str(unsigned int tos)
  802. {
  803. unsigned int x;
  804. for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
  805. if (dscp_pool1[x].space == (tos >> 2)) {
  806. return dscp_pool1[x].name;
  807. }
  808. }
  809. return "unknown";
  810. }
  811. int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
  812. {
  813. return ast_get_ip_or_srv(addr, hostname, NULL);
  814. }
  815. int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
  816. {
  817. /*
  818. * We must create the errno string before creating the address
  819. * string because it could wipe out errno on the error return
  820. * paths.
  821. */
  822. const char *sock_err;
  823. int port;
  824. int s;
  825. /* Preserve our original address port */
  826. port = ast_sockaddr_port(us);
  827. s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
  828. if (s < 0) {
  829. sock_err = ast_strdupa(strerror(errno));
  830. ast_log(LOG_ERROR, "Cannot create socket to %s: %s\n",
  831. ast_sockaddr_stringify_addr(them), sock_err);
  832. return -1;
  833. }
  834. if (ast_connect(s, them)) {
  835. sock_err = ast_strdupa(strerror(errno));
  836. ast_log(LOG_WARNING, "Cannot connect to %s: %s\n",
  837. ast_sockaddr_stringify_addr(them), sock_err);
  838. close(s);
  839. return -1;
  840. }
  841. if (ast_getsockname(s, us)) {
  842. sock_err = ast_strdupa(strerror(errno));
  843. ast_log(LOG_WARNING, "Cannot get socket name for connection to %s: %s\n",
  844. ast_sockaddr_stringify_addr(them), sock_err);
  845. close(s);
  846. return -1;
  847. }
  848. close(s);
  849. ast_sockaddr_set_port(us, port);
  850. ast_debug(3, "For destination '%s', our source address is '%s'.\n",
  851. ast_strdupa(ast_sockaddr_stringify_addr(them)),
  852. ast_strdupa(ast_sockaddr_stringify_addr(us)));
  853. return 0;
  854. }
  855. int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
  856. {
  857. char ourhost[MAXHOSTNAMELEN] = "";
  858. struct ast_sockaddr root;
  859. int res, port = ast_sockaddr_port(ourip);
  860. /* just use the bind address if it is nonzero */
  861. if (!ast_sockaddr_is_any(bindaddr)) {
  862. ast_sockaddr_copy(ourip, bindaddr);
  863. ast_debug(3, "Attached to given IP address\n");
  864. return 0;
  865. }
  866. /* try to use our hostname */
  867. if (gethostname(ourhost, sizeof(ourhost) - 1)) {
  868. ast_log(LOG_WARNING, "Unable to get hostname\n");
  869. } else {
  870. if (resolve_first(ourip, ourhost, PARSE_PORT_FORBID, family) == 0) {
  871. /* reset port since resolve_first wipes this out */
  872. ast_sockaddr_set_port(ourip, port);
  873. return 0;
  874. }
  875. }
  876. ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
  877. /* A.ROOT-SERVERS.NET. */
  878. if (!resolve_first(&root, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID, 0) &&
  879. !ast_ouraddrfor(&root, ourip)) {
  880. /* reset port since resolve_first wipes this out */
  881. ast_sockaddr_set_port(ourip, port);
  882. return 0;
  883. }
  884. res = get_local_address(ourip);
  885. ast_sockaddr_set_port(ourip, port);
  886. return res;
  887. }