acl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * 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. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/network.h"
  27. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
  28. #include <fcntl.h>
  29. #include <net/route.h>
  30. #endif
  31. #if defined(SOLARIS)
  32. #include <sys/sockio.h>
  33. #include <net/if.h>
  34. #elif defined(HAVE_GETIFADDRS)
  35. #include <ifaddrs.h>
  36. #endif
  37. #include "asterisk/acl.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/srv.h"
  42. #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
  43. static int get_local_address(struct in_addr *ourip)
  44. {
  45. return -1;
  46. }
  47. #else
  48. static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
  49. {
  50. const char *address;
  51. int score;
  52. address = ast_inet_ntoa(sin->sin_addr);
  53. /* RFC 1700 alias for the local network */
  54. if (address[0] == '0')
  55. score = -25;
  56. /* RFC 1700 localnet */
  57. else if (strncmp(address, "127", 3) == 0)
  58. score = -20;
  59. /* RFC 1918 non-public address space */
  60. else if (strncmp(address, "10.", 3) == 0)
  61. score = -5;
  62. /* RFC 1918 non-public address space */
  63. else if (strncmp(address, "172", 3) == 0) {
  64. /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
  65. if (address[4] == '1' && address[5] >= '6' && address[6] == '.')
  66. score = -5;
  67. /* 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 */
  68. else if (address[4] == '2' && address[6] == '.')
  69. score = -5;
  70. /* 172.30.0.0 - 172.31.255.255 */
  71. else if (address[4] == '3' && address[5] <= '1')
  72. score = -5;
  73. /* All other 172 addresses are public */
  74. else
  75. score = 0;
  76. /* RFC 2544 Benchmark test range */
  77. } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.')
  78. score = -10;
  79. /* RFC 1918 non-public address space */
  80. else if (strncmp(address, "192.168", 7) == 0)
  81. score = -5;
  82. /* RFC 3330 Zeroconf network */
  83. else if (strncmp(address, "169.254", 7) == 0)
  84. /*!\note Better score than a test network, but not quite as good as RFC 1918
  85. * address space. The reason is that some Linux distributions automatically
  86. * configure a Zeroconf address before trying DHCP, so we want to prefer a
  87. * DHCP lease to a Zeroconf address.
  88. */
  89. score = -10;
  90. /* RFC 3330 Test network */
  91. else if (strncmp(address, "192.0.2.", 8) == 0)
  92. score = -15;
  93. /* Every other address should be publically routable */
  94. else
  95. score = 0;
  96. if (score > *best_score) {
  97. *best_score = score;
  98. memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
  99. }
  100. }
  101. static int get_local_address(struct in_addr *ourip)
  102. {
  103. int s, res = -1;
  104. #ifdef SOLARIS
  105. struct lifreq *ifr = NULL;
  106. struct lifnum ifn;
  107. struct lifconf ifc;
  108. struct sockaddr_in *sa;
  109. char *buf = NULL;
  110. int bufsz, x;
  111. #endif /* SOLARIS */
  112. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
  113. struct ifaddrs *ifap, *ifaphead;
  114. int rtnerr;
  115. const struct sockaddr_in *sin;
  116. #endif /* BSD_OR_LINUX */
  117. struct in_addr best_addr;
  118. int best_score = -100;
  119. memset(&best_addr, 0, sizeof(best_addr));
  120. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
  121. rtnerr = getifaddrs(&ifaphead);
  122. if (rtnerr) {
  123. perror(NULL);
  124. return -1;
  125. }
  126. #endif /* BSD_OR_LINUX */
  127. s = socket(AF_INET, SOCK_STREAM, 0);
  128. if (s > 0) {
  129. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
  130. for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
  131. if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
  132. sin = (const struct sockaddr_in *) ifap->ifa_addr;
  133. score_address(sin, &best_addr, &best_score);
  134. res = 0;
  135. if (best_score == 0)
  136. break;
  137. }
  138. }
  139. #endif /* BSD_OR_LINUX */
  140. /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
  141. #ifdef SOLARIS
  142. /* Get a count of interfaces on the machine */
  143. ifn.lifn_family = AF_INET;
  144. ifn.lifn_flags = 0;
  145. ifn.lifn_count = 0;
  146. if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
  147. close(s);
  148. return -1;
  149. }
  150. bufsz = ifn.lifn_count * sizeof(struct lifreq);
  151. if (!(buf = malloc(bufsz))) {
  152. close(s);
  153. return -1;
  154. }
  155. memset(buf, 0, bufsz);
  156. /* Get a list of interfaces on the machine */
  157. ifc.lifc_len = bufsz;
  158. ifc.lifc_buf = buf;
  159. ifc.lifc_family = AF_INET;
  160. ifc.lifc_flags = 0;
  161. if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
  162. close(s);
  163. free(buf);
  164. return -1;
  165. }
  166. for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
  167. sa = (struct sockaddr_in *)&(ifr->lifr_addr);
  168. score_address(sa, &best_addr, &best_score);
  169. res = 0;
  170. if (best_score == 0)
  171. break;
  172. }
  173. free(buf);
  174. #endif /* SOLARIS */
  175. close(s);
  176. }
  177. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
  178. freeifaddrs(ifaphead);
  179. #endif /* BSD_OR_LINUX */
  180. if (res == 0 && ourip)
  181. memcpy(ourip, &best_addr, sizeof(*ourip));
  182. return res;
  183. }
  184. #endif /* HAVE_GETIFADDRS */
  185. /* Free HA structure */
  186. void ast_free_ha(struct ast_ha *ha)
  187. {
  188. struct ast_ha *hal;
  189. while (ha) {
  190. hal = ha;
  191. ha = ha->next;
  192. ast_free(hal);
  193. }
  194. }
  195. /* Copy HA structure */
  196. void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
  197. {
  198. memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
  199. memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
  200. to->sense = from->sense;
  201. }
  202. /* Create duplicate of ha structure */
  203. static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
  204. {
  205. struct ast_ha *new_ha;
  206. if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
  207. /* Copy from original to new object */
  208. ast_copy_ha(original, new_ha);
  209. }
  210. return new_ha;
  211. }
  212. /* Create duplicate HA link list */
  213. /* Used in chan_sip2 templates */
  214. struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
  215. {
  216. struct ast_ha *start = original;
  217. struct ast_ha *ret = NULL;
  218. struct ast_ha *current, *prev = NULL;
  219. while (start) {
  220. current = ast_duplicate_ha(start); /* Create copy of this object */
  221. if (prev)
  222. prev->next = current; /* Link previous to this object */
  223. if (!ret)
  224. ret = current; /* Save starting point */
  225. start = start->next; /* Go to next object */
  226. prev = current; /* Save pointer to this object */
  227. }
  228. return ret; /* Return start of list */
  229. }
  230. struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
  231. {
  232. struct ast_ha *ha;
  233. char *nm;
  234. struct ast_ha *prev = NULL;
  235. struct ast_ha *ret;
  236. int x;
  237. char *tmp = ast_strdupa(stuff);
  238. ret = path;
  239. while (path) {
  240. prev = path;
  241. path = path->next;
  242. }
  243. ha = ast_malloc(sizeof(*ha));
  244. if (!ha)
  245. return ret;
  246. nm = strchr(tmp, '/');
  247. if (!nm) {
  248. /* assume /32. Yes, htonl does not do anything for this particular mask
  249. but we better use it to show we remember about byte order */
  250. ha->netmask.s_addr = htonl(0xFFFFFFFF);
  251. } else {
  252. *nm = '\0';
  253. nm++;
  254. if (!strchr(nm, '.')) {
  255. if ((sscanf(nm, "%30d", &x) == 1) && (x >= 0) && (x <= 32))
  256. if (x == 0) {
  257. /* This is special-cased to prevent unpredictable
  258. * behavior of shifting left 32 bits
  259. */
  260. ha->netmask.s_addr = 0;
  261. } else {
  262. ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
  263. }
  264. else {
  265. ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
  266. ast_free(ha);
  267. if (error)
  268. *error = 1;
  269. return ret;
  270. }
  271. } else if (!inet_aton(nm, &ha->netmask)) {
  272. ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
  273. ast_free(ha);
  274. if (error)
  275. *error = 1;
  276. return ret;
  277. }
  278. }
  279. if (!inet_aton(tmp, &ha->netaddr)) {
  280. ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
  281. ast_free(ha);
  282. if (error)
  283. *error = 1;
  284. return ret;
  285. }
  286. ha->netaddr.s_addr &= ha->netmask.s_addr;
  287. ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
  288. ha->next = NULL;
  289. if (prev) {
  290. prev->next = ha;
  291. } else {
  292. ret = ha;
  293. }
  294. ast_debug(1, "%s/%s sense %d appended to acl for peer\n", ast_strdupa(ast_inet_ntoa(ha->netaddr)), ast_strdupa(ast_inet_ntoa(ha->netmask)), ha->sense);
  295. return ret;
  296. }
  297. int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
  298. {
  299. /* Start optimistic */
  300. int res = AST_SENSE_ALLOW;
  301. while (ha) {
  302. #if 0 /* debugging code */
  303. char iabuf[INET_ADDRSTRLEN];
  304. char iabuf2[INET_ADDRSTRLEN];
  305. /* DEBUG */
  306. ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
  307. ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
  308. ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
  309. #endif
  310. /* For each rule, if this address and the netmask = the net address
  311. apply the current rule */
  312. if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
  313. res = ha->sense;
  314. ha = ha->next;
  315. }
  316. return res;
  317. }
  318. int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
  319. {
  320. struct hostent *hp;
  321. struct ast_hostent ahp;
  322. char srv[256];
  323. char host[256];
  324. int tportno = ntohs(sin->sin_port);
  325. if (service) {
  326. snprintf(srv, sizeof(srv), "%s.%s", service, value);
  327. if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
  328. sin->sin_port = htons(tportno);
  329. value = host;
  330. }
  331. }
  332. hp = ast_gethostbyname(value, &ahp);
  333. if (hp) {
  334. memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
  335. } else {
  336. ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
  337. return -1;
  338. }
  339. return 0;
  340. }
  341. struct dscp_codepoint {
  342. char *name;
  343. unsigned int space;
  344. };
  345. /* IANA registered DSCP codepoints */
  346. static const struct dscp_codepoint dscp_pool1[] = {
  347. { "CS0", 0x00 },
  348. { "CS1", 0x08 },
  349. { "CS2", 0x10 },
  350. { "CS3", 0x18 },
  351. { "CS4", 0x20 },
  352. { "CS5", 0x28 },
  353. { "CS6", 0x30 },
  354. { "CS7", 0x38 },
  355. { "AF11", 0x0A },
  356. { "AF12", 0x0C },
  357. { "AF13", 0x0E },
  358. { "AF21", 0x12 },
  359. { "AF22", 0x14 },
  360. { "AF23", 0x16 },
  361. { "AF31", 0x1A },
  362. { "AF32", 0x1C },
  363. { "AF33", 0x1E },
  364. { "AF41", 0x22 },
  365. { "AF42", 0x24 },
  366. { "AF43", 0x26 },
  367. { "EF", 0x2E },
  368. };
  369. int ast_str2cos(const char *value, unsigned int *cos)
  370. {
  371. int fval;
  372. if (sscanf(value, "%30d", &fval) == 1) {
  373. if (fval < 8) {
  374. *cos = fval;
  375. return 0;
  376. }
  377. }
  378. return -1;
  379. }
  380. int ast_str2tos(const char *value, unsigned int *tos)
  381. {
  382. int fval;
  383. unsigned int x;
  384. if (sscanf(value, "%30i", &fval) == 1) {
  385. *tos = fval & 0xFF;
  386. return 0;
  387. }
  388. for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
  389. if (!strcasecmp(value, dscp_pool1[x].name)) {
  390. *tos = dscp_pool1[x].space << 2;
  391. return 0;
  392. }
  393. }
  394. return -1;
  395. }
  396. const char *ast_tos2str(unsigned int tos)
  397. {
  398. unsigned int x;
  399. for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
  400. if (dscp_pool1[x].space == (tos >> 2))
  401. return dscp_pool1[x].name;
  402. }
  403. return "unknown";
  404. }
  405. int ast_get_ip(struct sockaddr_in *sin, const char *value)
  406. {
  407. return ast_get_ip_or_srv(sin, value, NULL);
  408. }
  409. int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
  410. {
  411. int s;
  412. struct sockaddr_in sin;
  413. socklen_t slen;
  414. s = socket(PF_INET, SOCK_DGRAM, 0);
  415. if (s < 0) {
  416. ast_log(LOG_ERROR, "Cannot create socket\n");
  417. return -1;
  418. }
  419. sin.sin_family = AF_INET;
  420. sin.sin_port = htons(5060);
  421. sin.sin_addr = *them;
  422. if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
  423. ast_log(LOG_WARNING, "Cannot connect\n");
  424. close(s);
  425. return -1;
  426. }
  427. slen = sizeof(sin);
  428. if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
  429. ast_log(LOG_WARNING, "Cannot get socket name\n");
  430. close(s);
  431. return -1;
  432. }
  433. close(s);
  434. ast_debug(3, "Found IP address for this socket\n");
  435. *us = sin.sin_addr;
  436. return 0;
  437. }
  438. int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
  439. {
  440. char ourhost[MAXHOSTNAMELEN] = "";
  441. struct ast_hostent ahp;
  442. struct hostent *hp;
  443. struct in_addr saddr;
  444. /* just use the bind address if it is nonzero */
  445. if (ntohl(bindaddr.sin_addr.s_addr)) {
  446. memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
  447. ast_debug(3, "Attached to given IP address\n");
  448. return 0;
  449. }
  450. /* try to use our hostname */
  451. if (gethostname(ourhost, sizeof(ourhost) - 1)) {
  452. ast_log(LOG_WARNING, "Unable to get hostname\n");
  453. } else {
  454. hp = ast_gethostbyname(ourhost, &ahp);
  455. if (hp) {
  456. memcpy(ourip, hp->h_addr, sizeof(*ourip));
  457. ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
  458. return 0;
  459. }
  460. }
  461. ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
  462. /* A.ROOT-SERVERS.NET. */
  463. if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
  464. return 0;
  465. return get_local_address(ourip);
  466. }