Merged revisions 37441-37442 via svnmerge from
[asterisk-bristuff.git] / acl.c
blob15057aa24f9c6de8937d7b3e6facffffc8016078
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Various sorts of access control
23 * \author Mark Spencer <markster@digium.com>
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <sys/ioctl.h>
47 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
48 #include <fcntl.h>
49 #include <net/route.h>
50 #endif
52 #if defined(SOLARIS)
53 #include <sys/sockio.h>
54 #endif
56 /* netinet/ip.h may not define the following (See RFCs 791 and 1349) */
57 #if !defined(IPTOS_LOWCOST)
58 #define IPTOS_LOWCOST 0x02
59 #endif
61 #if !defined(IPTOS_MINCOST)
62 #define IPTOS_MINCOST IPTOS_LOWCOST
63 #endif
65 #include "asterisk/acl.h"
66 #include "asterisk/logger.h"
67 #include "asterisk/channel.h"
68 #include "asterisk/options.h"
69 #include "asterisk/utils.h"
70 #include "asterisk/lock.h"
71 #include "asterisk/srv.h"
73 struct ast_ha {
74 /* Host access rule */
75 struct in_addr netaddr;
76 struct in_addr netmask;
77 int sense;
78 struct ast_ha *next;
81 /* Default IP - if not otherwise set, don't breathe garbage */
82 static struct in_addr __ourip = { 0x00000000 };
84 struct my_ifreq {
85 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
86 struct sockaddr_in ifru_addr;
89 /* Free HA structure */
90 void ast_free_ha(struct ast_ha *ha)
92 struct ast_ha *hal;
93 while (ha) {
94 hal = ha;
95 ha = ha->next;
96 free(hal);
100 /* Copy HA structure */
101 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
103 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
104 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
105 to->sense = from->sense;
108 /* Create duplicate of ha structure */
109 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
111 struct ast_ha *new_ha;
113 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
114 /* Copy from original to new object */
115 ast_copy_ha(original, new_ha);
118 return new_ha;
121 /* Create duplicate HA link list */
122 /* Used in chan_sip2 templates */
123 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
125 struct ast_ha *start = original;
126 struct ast_ha *ret = NULL;
127 struct ast_ha *link, *prev = NULL;
129 while (start) {
130 link = ast_duplicate_ha(start); /* Create copy of this object */
131 if (prev)
132 prev->next = link; /* Link previous to this object */
134 if (!ret)
135 ret = link; /* Save starting point */
137 start = start->next; /* Go to next object */
138 prev = link; /* Save pointer to this object */
140 return ret; /* Return start of list */
143 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
145 struct ast_ha *ha;
146 char *nm = "255.255.255.255";
147 char tmp[256];
148 struct ast_ha *prev = NULL;
149 struct ast_ha *ret;
150 int x, z;
151 unsigned int y;
153 ret = path;
154 while (path) {
155 prev = path;
156 path = path->next;
158 if ((ha = ast_malloc(sizeof(*ha)))) {
159 ast_copy_string(tmp, stuff, sizeof(tmp));
160 nm = strchr(tmp, '/');
161 if (!nm) {
162 nm = "255.255.255.255";
163 } else {
164 *nm = '\0';
165 nm++;
167 if (!strchr(nm, '.')) {
168 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32)) {
169 y = 0;
170 for (z = 0; z < x; z++) {
171 y >>= 1;
172 y |= 0x80000000;
174 ha->netmask.s_addr = htonl(y);
176 } else if (!inet_aton(nm, &ha->netmask)) {
177 ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
178 free(ha);
179 return path;
181 if (!inet_aton(tmp, &ha->netaddr)) {
182 ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
183 free(ha);
184 return path;
186 ha->netaddr.s_addr &= ha->netmask.s_addr;
187 if (!strncasecmp(sense, "p", 1)) {
188 ha->sense = AST_SENSE_ALLOW;
189 } else {
190 ha->sense = AST_SENSE_DENY;
192 ha->next = NULL;
193 if (prev) {
194 prev->next = ha;
195 } else {
196 ret = ha;
199 ast_log(LOG_DEBUG, "%s/%s appended to acl for peer\n", stuff, nm);
200 return ret;
203 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
205 /* Start optimistic */
206 int res = AST_SENSE_ALLOW;
207 while (ha) {
208 char iabuf[INET_ADDRSTRLEN];
209 char iabuf2[INET_ADDRSTRLEN];
210 /* DEBUG */
211 ast_log(LOG_DEBUG,
212 "##### Testing %s with %s\n",
213 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr),
214 ast_inet_ntoa(iabuf2, sizeof(iabuf2), ha->netaddr));
215 /* For each rule, if this address and the netmask = the net address
216 apply the current rule */
217 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
218 res = ha->sense;
219 ha = ha->next;
221 return res;
224 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
226 struct hostent *hp;
227 struct ast_hostent ahp;
228 char srv[256];
229 char host[256];
230 int tportno = ntohs(sin->sin_port);
231 if (inet_aton(value, &sin->sin_addr))
232 return 0;
233 if (service) {
234 snprintf(srv, sizeof(srv), "%s.%s", service, value);
235 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
236 sin->sin_port = htons(tportno);
237 value = host;
240 hp = ast_gethostbyname(value, &ahp);
241 if (hp) {
242 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
243 } else {
244 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
245 return -1;
247 return 0;
250 struct dscp_codepoint {
251 char *name;
252 unsigned int space;
255 /* IANA registered DSCP codepoints */
257 static const struct dscp_codepoint dscp_pool1[] = {
258 { "CS0", 0x00 },
259 { "CS1", 0x08 },
260 { "CS2", 0x10 },
261 { "CS3", 0x18 },
262 { "CS4", 0x20 },
263 { "CS5", 0x28 },
264 { "CS6", 0x30 },
265 { "CS7", 0x38 },
266 { "AF11", 0x0A },
267 { "AF12", 0x0C },
268 { "AF13", 0x0E },
269 { "AF21", 0x12 },
270 { "AF22", 0x14 },
271 { "AF23", 0x16 },
272 { "AF31", 0x1A },
273 { "AF32", 0x1C },
274 { "AF33", 0x1E },
275 { "AF41", 0x22 },
276 { "AF42", 0x24 },
277 { "AF43", 0x26 },
278 { "EF", 0x2E },
281 int ast_str2tos(const char *value, unsigned int *tos)
283 int fval;
284 unsigned int x;
286 if (sscanf(value, "%i", &fval) == 1) {
287 *tos = fval & 0xFF;
288 return 0;
291 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
292 if (!strcasecmp(value, dscp_pool1[x].name)) {
293 *tos = dscp_pool1[x].space << 2;
294 return 0;
298 if (!strcasecmp(value, "lowdelay"))
299 *tos = IPTOS_LOWDELAY;
300 else if (!strcasecmp(value, "throughput"))
301 *tos = IPTOS_THROUGHPUT;
302 else if (!strcasecmp(value, "reliability"))
303 *tos = IPTOS_RELIABILITY;
304 else if (!strcasecmp(value, "mincost"))
305 *tos = IPTOS_MINCOST;
306 else if (!strcasecmp(value, "none"))
307 *tos = 0;
308 else
309 return -1;
311 ast_log(LOG_WARNING, "TOS value %s is deprecated. Please see doc/ip-tos.txt for more information.\n", value);
313 return 0;
316 const char *ast_tos2str(unsigned int tos)
318 unsigned int x;
320 switch (tos) {
321 case 0:
322 return "none";
323 case IPTOS_LOWDELAY:
324 return "lowdelay";
325 case IPTOS_THROUGHPUT:
326 return "throughput";
327 case IPTOS_RELIABILITY:
328 return "reliability";
329 case IPTOS_MINCOST:
330 return "mincost";
331 default:
332 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
333 if (dscp_pool1[x].space == (tos >> 2))
334 return dscp_pool1[x].name;
338 return "unknown";
341 int ast_get_ip(struct sockaddr_in *sin, const char *value)
343 return ast_get_ip_or_srv(sin, value, NULL);
346 /* iface is the interface (e.g. eth0); address is the return value */
347 int ast_lookup_iface(char *iface, struct in_addr *address)
349 int mysock, res = 0;
350 struct my_ifreq ifreq;
352 memset(&ifreq, 0, sizeof(ifreq));
353 ast_copy_string(ifreq.ifrn_name, iface, sizeof(ifreq.ifrn_name));
355 mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
356 res = ioctl(mysock, SIOCGIFADDR, &ifreq);
358 close(mysock);
359 if (res < 0) {
360 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
361 memcpy((char *)address, (char *)&__ourip, sizeof(__ourip));
362 return -1;
363 } else {
364 memcpy((char *)address, (char *)&ifreq.ifru_addr.sin_addr, sizeof(ifreq.ifru_addr.sin_addr));
365 return 0;
369 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
371 int s;
372 struct sockaddr_in sin;
373 socklen_t slen;
375 s = socket(PF_INET, SOCK_DGRAM, 0);
376 if (s < 0) {
377 ast_log(LOG_WARNING, "Cannot create socket\n");
378 return -1;
380 sin.sin_family = AF_INET;
381 sin.sin_port = 5060;
382 sin.sin_addr = *them;
383 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
384 ast_log(LOG_WARNING, "Cannot connect\n");
385 close(s);
386 return -1;
388 slen = sizeof(sin);
389 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
390 ast_log(LOG_WARNING, "Cannot get socket name\n");
391 close(s);
392 return -1;
394 close(s);
395 *us = sin.sin_addr;
396 return 0;
399 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
401 char ourhost[MAXHOSTNAMELEN] = "";
402 struct ast_hostent ahp;
403 struct hostent *hp;
404 struct in_addr saddr;
406 /* just use the bind address if it is nonzero */
407 if (ntohl(bindaddr.sin_addr.s_addr)) {
408 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
409 return 0;
411 /* try to use our hostname */
412 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
413 ast_log(LOG_WARNING, "Unable to get hostname\n");
414 } else {
415 hp = ast_gethostbyname(ourhost, &ahp);
416 if (hp) {
417 memcpy(ourip, hp->h_addr, sizeof(*ourip));
418 return 0;
421 /* A.ROOT-SERVERS.NET. */
422 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
423 return 0;
424 return -1;