acl.c 8.3 KB

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