acl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/time.h>
  27. #include <signal.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <sys/socket.h>
  33. #include <netdb.h>
  34. #include <net/if.h>
  35. #include <netinet/in.h>
  36. #include <netinet/in_systm.h>
  37. #include <netinet/ip.h>
  38. #include <sys/ioctl.h>
  39. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
  40. #include <fcntl.h>
  41. #include <net/route.h>
  42. #endif
  43. #if defined(SOLARIS)
  44. #include <sys/sockio.h>
  45. #endif
  46. /* netinet/ip.h may not define the following (See RFCs 791 and 1349) */
  47. #if !defined(IPTOS_LOWCOST)
  48. #define IPTOS_LOWCOST 0x02
  49. #endif
  50. #if !defined(IPTOS_MINCOST)
  51. #define IPTOS_MINCOST IPTOS_LOWCOST
  52. #endif
  53. #include "asterisk.h"
  54. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  55. #include "asterisk/acl.h"
  56. #include "asterisk/logger.h"
  57. #include "asterisk/channel.h"
  58. #include "asterisk/options.h"
  59. #include "asterisk/utils.h"
  60. #include "asterisk/lock.h"
  61. #include "asterisk/srv.h"
  62. #include "asterisk/compat.h"
  63. #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
  64. AST_MUTEX_DEFINE_STATIC(routeseq_lock);
  65. #endif
  66. struct ast_ha {
  67. /* Host access rule */
  68. struct in_addr netaddr;
  69. struct in_addr netmask;
  70. int sense;
  71. struct ast_ha *next;
  72. };
  73. /* Default IP - if not otherwise set, don't breathe garbage */
  74. static struct in_addr __ourip = { 0x00000000 };
  75. struct my_ifreq {
  76. char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
  77. struct sockaddr_in ifru_addr;
  78. };
  79. /* Free HA structure */
  80. void ast_free_ha(struct ast_ha *ha)
  81. {
  82. struct ast_ha *hal;
  83. while(ha) {
  84. hal = ha;
  85. ha = ha->next;
  86. free(hal);
  87. }
  88. }
  89. /* Copy HA structure */
  90. static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
  91. {
  92. memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
  93. memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
  94. to->sense = from->sense;
  95. }
  96. /* Create duplicate of ha structure */
  97. static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
  98. {
  99. struct ast_ha *new_ha = malloc(sizeof(struct ast_ha));
  100. /* Copy from original to new object */
  101. ast_copy_ha(original, new_ha);
  102. return new_ha;
  103. }
  104. /* Create duplicate HA link list */
  105. /* Used in chan_sip2 templates */
  106. struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
  107. {
  108. struct ast_ha *start=original;
  109. struct ast_ha *ret = NULL;
  110. struct ast_ha *link,*prev=NULL;
  111. while (start) {
  112. link = ast_duplicate_ha(start); /* Create copy of this object */
  113. if (prev)
  114. prev->next = link; /* Link previous to this object */
  115. if (!ret)
  116. ret = link; /* Save starting point */
  117. start = start->next; /* Go to next object */
  118. prev = link; /* Save pointer to this object */
  119. }
  120. return ret; /* Return start of list */
  121. }
  122. struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
  123. {
  124. struct ast_ha *ha = malloc(sizeof(struct ast_ha));
  125. char *nm = "255.255.255.255";
  126. char tmp[256];
  127. struct ast_ha *prev = NULL;
  128. struct ast_ha *ret;
  129. int x, z;
  130. unsigned int y;
  131. ret = path;
  132. while (path) {
  133. prev = path;
  134. path = path->next;
  135. }
  136. if (ha) {
  137. ast_copy_string(tmp, stuff, sizeof(tmp));
  138. nm = strchr(tmp, '/');
  139. if (!nm) {
  140. nm = "255.255.255.255";
  141. } else {
  142. *nm = '\0';
  143. nm++;
  144. }
  145. if (!strchr(nm, '.')) {
  146. if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32)) {
  147. y = 0;
  148. for (z=0;z<x;z++) {
  149. y >>= 1;
  150. y |= 0x80000000;
  151. }
  152. ha->netmask.s_addr = htonl(y);
  153. }
  154. } else if (!inet_aton(nm, &ha->netmask)) {
  155. ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
  156. free(ha);
  157. return ret;
  158. }
  159. if (!inet_aton(tmp, &ha->netaddr)) {
  160. ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
  161. free(ha);
  162. return ret;
  163. }
  164. ha->netaddr.s_addr &= ha->netmask.s_addr;
  165. if (!strncasecmp(sense, "p", 1)) {
  166. ha->sense = AST_SENSE_ALLOW;
  167. } else {
  168. ha->sense = AST_SENSE_DENY;
  169. }
  170. ha->next = NULL;
  171. if (prev) {
  172. prev->next = ha;
  173. } else {
  174. ret = ha;
  175. }
  176. }
  177. ast_log(LOG_DEBUG, "%s/%s appended to acl for peer\n", stuff, nm);
  178. return ret;
  179. }
  180. int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
  181. {
  182. /* Start optimistic */
  183. int res = AST_SENSE_ALLOW;
  184. while (ha) {
  185. char iabuf[INET_ADDRSTRLEN];
  186. char iabuf2[INET_ADDRSTRLEN];
  187. /* DEBUG */
  188. ast_log(LOG_DEBUG,
  189. "##### Testing %s with %s\n",
  190. ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr),
  191. ast_inet_ntoa(iabuf2, sizeof(iabuf2), ha->netaddr));
  192. /* For each rule, if this address and the netmask = the net address
  193. apply the current rule */
  194. if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
  195. res = ha->sense;
  196. ha = ha->next;
  197. }
  198. return res;
  199. }
  200. int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
  201. {
  202. struct hostent *hp;
  203. struct ast_hostent ahp;
  204. char srv[256];
  205. char host[256];
  206. int tportno = ntohs(sin->sin_port);
  207. if (inet_aton(value, &sin->sin_addr))
  208. return 0;
  209. if (service) {
  210. snprintf(srv, sizeof(srv), "%s.%s", service, value);
  211. if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
  212. sin->sin_port = htons(tportno);
  213. value = host;
  214. }
  215. }
  216. hp = ast_gethostbyname(value, &ahp);
  217. if (hp) {
  218. memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
  219. } else {
  220. ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
  221. return -1;
  222. }
  223. return 0;
  224. }
  225. int ast_str2tos(const char *value, int *tos)
  226. {
  227. int fval;
  228. if (sscanf(value, "%i", &fval) == 1)
  229. *tos = fval & 0xff;
  230. else if (!strcasecmp(value, "lowdelay"))
  231. *tos = IPTOS_LOWDELAY;
  232. else if (!strcasecmp(value, "throughput"))
  233. *tos = IPTOS_THROUGHPUT;
  234. else if (!strcasecmp(value, "reliability"))
  235. *tos = IPTOS_RELIABILITY;
  236. else if (!strcasecmp(value, "mincost"))
  237. *tos = IPTOS_MINCOST;
  238. else if (!strcasecmp(value, "none"))
  239. *tos = 0;
  240. else
  241. return -1;
  242. return 0;
  243. }
  244. int ast_get_ip(struct sockaddr_in *sin, const char *value)
  245. {
  246. return ast_get_ip_or_srv(sin, value, NULL);
  247. }
  248. /* iface is the interface (e.g. eth0); address is the return value */
  249. int ast_lookup_iface(char *iface, struct in_addr *address)
  250. {
  251. int mysock, res = 0;
  252. struct my_ifreq ifreq;
  253. memset(&ifreq, 0, sizeof(ifreq));
  254. ast_copy_string(ifreq.ifrn_name, iface, sizeof(ifreq.ifrn_name));
  255. mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  256. res = ioctl(mysock, SIOCGIFADDR, &ifreq);
  257. close(mysock);
  258. if (res < 0) {
  259. ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
  260. memcpy((char *)address, (char *)&__ourip, sizeof(__ourip));
  261. return -1;
  262. } else {
  263. memcpy((char *)address, (char *)&ifreq.ifru_addr.sin_addr, sizeof(ifreq.ifru_addr.sin_addr));
  264. return 0;
  265. }
  266. }
  267. int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
  268. {
  269. int s;
  270. struct sockaddr_in sin;
  271. socklen_t slen;
  272. s = socket(PF_INET, SOCK_DGRAM, 0);
  273. if (s < 0) {
  274. ast_log(LOG_WARNING, "Cannot create socket\n");
  275. return -1;
  276. }
  277. sin.sin_family = AF_INET;
  278. sin.sin_port = 5060;
  279. sin.sin_addr = *them;
  280. if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
  281. ast_log(LOG_WARNING, "Cannot connect\n");
  282. close(s);
  283. return -1;
  284. }
  285. slen = sizeof(sin);
  286. if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
  287. ast_log(LOG_WARNING, "Cannot get socket name\n");
  288. close(s);
  289. return -1;
  290. }
  291. close(s);
  292. *us = sin.sin_addr;
  293. return 0;
  294. }
  295. int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
  296. {
  297. char ourhost[MAXHOSTNAMELEN] = "";
  298. struct ast_hostent ahp;
  299. struct hostent *hp;
  300. struct in_addr saddr;
  301. /* just use the bind address if it is nonzero */
  302. if (ntohl(bindaddr.sin_addr.s_addr)) {
  303. memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
  304. return 0;
  305. }
  306. /* try to use our hostname */
  307. if (gethostname(ourhost, sizeof(ourhost) - 1)) {
  308. ast_log(LOG_WARNING, "Unable to get hostname\n");
  309. } else {
  310. hp = ast_gethostbyname(ourhost, &ahp);
  311. if (hp) {
  312. memcpy(ourip, hp->h_addr, sizeof(*ourip));
  313. return 0;
  314. }
  315. }
  316. /* A.ROOT-SERVERS.NET. */
  317. if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
  318. return 0;
  319. return -1;
  320. }