Optionally display the value of several variables within the Status command.
[asterisk-bristuff.git] / main / acl.c
blob9a141a3735c3f512184f482ce163e7972e9666ef
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 "asterisk/network.h"
32 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
33 #include <fcntl.h>
34 #include <net/route.h>
35 #endif
37 #if defined(SOLARIS)
38 #include <sys/sockio.h>
39 #include <net/if.h>
40 #elif defined(HAVE_GETIFADDRS)
41 #include <ifaddrs.h>
42 #endif
44 #include "asterisk/acl.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/srv.h"
50 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
51 static int get_local_address(struct in_addr *ourip)
53 return -1;
55 #else
56 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
58 const char *address;
59 int score;
61 address = ast_inet_ntoa(sin->sin_addr);
63 /* RFC 1700 alias for the local network */
64 if (address[0] == '0')
65 score = -25;
66 /* RFC 1700 localnet */
67 else if (strncmp(address, "127", 3) == 0)
68 score = -20;
69 /* RFC 1918 non-public address space */
70 else if (strncmp(address, "10.", 3) == 0)
71 score = -5;
72 /* RFC 1918 non-public address space */
73 else if (strncmp(address, "172", 3) == 0) {
74 /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
75 if (address[4] == '1' && address[5] >= '6' && address[6] == '.')
76 score = -5;
77 /* 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 */
78 else if (address[4] == '2' && address[6] == '.')
79 score = -5;
80 /* 172.30.0.0 - 172.31.255.255 */
81 else if (address[4] == '3' && address[5] <= '1')
82 score = -5;
83 /* All other 172 addresses are public */
84 else
85 score = 0;
86 /* RFC 2544 Benchmark test range */
87 } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.')
88 score = -10;
89 /* RFC 1918 non-public address space */
90 else if (strncmp(address, "192.168", 7) == 0)
91 score = -5;
92 /* RFC 3330 Zeroconf network */
93 else if (strncmp(address, "169.254", 7) == 0)
94 /*!\note Better score than a test network, but not quite as good as RFC 1918
95 * address space. The reason is that some Linux distributions automatically
96 * configure a Zeroconf address before trying DHCP, so we want to prefer a
97 * DHCP lease to a Zeroconf address.
99 score = -10;
100 /* RFC 3330 Test network */
101 else if (strncmp(address, "192.0.2.", 8) == 0)
102 score = -15;
103 /* Every other address should be publically routable */
104 else
105 score = 0;
107 if (score > *best_score) {
108 *best_score = score;
109 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
113 static int get_local_address(struct in_addr *ourip)
115 int s, res = -1;
116 #ifdef SOLARIS
117 struct lifreq *ifr = NULL;
118 struct lifnum ifn;
119 struct lifconf ifc;
120 struct sockaddr_in *sa;
121 char *buf = NULL;
122 int bufsz, x;
123 #endif /* SOLARIS */
124 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
125 struct ifaddrs *ifap, *ifaphead;
126 int rtnerr;
127 const struct sockaddr_in *sin;
128 #endif /* BSD_OR_LINUX */
129 struct in_addr best_addr;
130 int best_score = -100;
131 memset(&best_addr, 0, sizeof(best_addr));
133 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
134 rtnerr = getifaddrs(&ifaphead);
135 if (rtnerr) {
136 perror(NULL);
137 return -1;
139 #endif /* BSD_OR_LINUX */
141 s = socket(AF_INET, SOCK_STREAM, 0);
143 if (s > 0) {
144 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
145 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
147 if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
148 sin = (const struct sockaddr_in *) ifap->ifa_addr;
149 score_address(sin, &best_addr, &best_score);
150 res = 0;
152 if (best_score == 0)
153 break;
156 #endif /* BSD_OR_LINUX */
158 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
159 #ifdef SOLARIS
160 /* Get a count of interfaces on the machine */
161 ifn.lifn_family = AF_INET;
162 ifn.lifn_flags = 0;
163 ifn.lifn_count = 0;
164 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
165 close(s);
166 return -1;
169 bufsz = ifn.lifn_count * sizeof(struct lifreq);
170 if (!(buf = malloc(bufsz))) {
171 close(s);
172 return -1;
174 memset(buf, 0, bufsz);
176 /* Get a list of interfaces on the machine */
177 ifc.lifc_len = bufsz;
178 ifc.lifc_buf = buf;
179 ifc.lifc_family = AF_INET;
180 ifc.lifc_flags = 0;
181 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
182 close(s);
183 free(buf);
184 return -1;
187 for (ifr = (struct lifreq *)buf, x = 0; x < ifn.lifn_count; ifr++, x++) {
188 sa = (struct sockaddr_in *)&(ifr->lifr_addr);
189 score_address(sa, &best_addr, &best_score);
190 res = 0;
192 if (best_score == 0)
193 break;
196 free(buf);
197 #endif /* SOLARIS */
199 close(s);
201 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
202 freeifaddrs(ifaphead);
203 #endif /* BSD_OR_LINUX */
205 if (res == 0 && ourip)
206 memcpy(ourip, &best_addr, sizeof(*ourip));
207 return res;
209 #endif /* HAVE_GETIFADDRS */
211 /* Free HA structure */
212 void ast_free_ha(struct ast_ha *ha)
214 struct ast_ha *hal;
215 while (ha) {
216 hal = ha;
217 ha = ha->next;
218 ast_free(hal);
222 /* Copy HA structure */
223 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
225 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
226 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
227 to->sense = from->sense;
230 /* Create duplicate of ha structure */
231 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
233 struct ast_ha *new_ha;
235 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
236 /* Copy from original to new object */
237 ast_copy_ha(original, new_ha);
240 return new_ha;
243 /* Create duplicate HA link list */
244 /* Used in chan_sip2 templates */
245 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
247 struct ast_ha *start = original;
248 struct ast_ha *ret = NULL;
249 struct ast_ha *link, *prev = NULL;
251 while (start) {
252 link = ast_duplicate_ha(start); /* Create copy of this object */
253 if (prev)
254 prev->next = link; /* Link previous to this object */
256 if (!ret)
257 ret = link; /* Save starting point */
259 start = start->next; /* Go to next object */
260 prev = link; /* Save pointer to this object */
262 return ret; /* Return start of list */
265 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
267 struct ast_ha *ha;
268 char *nm;
269 struct ast_ha *prev = NULL;
270 struct ast_ha *ret;
271 int x;
272 char *tmp = ast_strdupa(stuff);
274 ret = path;
275 while (path) {
276 prev = path;
277 path = path->next;
280 ha = ast_malloc(sizeof(*ha));
281 if (!ha)
282 return ret;
284 nm = strchr(tmp, '/');
285 if (!nm) {
286 /* assume /32. Yes, htonl does not do anything for this particular mask
287 but we better use it to show we remember about byte order */
288 ha->netmask.s_addr = htonl(0xFFFFFFFF);
289 } else {
290 *nm = '\0';
291 nm++;
293 if (!strchr(nm, '.')) {
294 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
295 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
296 else {
297 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
298 ast_free(ha);
299 if (error)
300 *error = 1;
301 return ret;
303 } else if (!inet_aton(nm, &ha->netmask)) {
304 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
305 ast_free(ha);
306 if (error)
307 *error = 1;
308 return ret;
312 if (!inet_aton(tmp, &ha->netaddr)) {
313 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
314 ast_free(ha);
315 if (error)
316 *error = 1;
317 return ret;
320 ha->netaddr.s_addr &= ha->netmask.s_addr;
322 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
324 ha->next = NULL;
325 if (prev) {
326 prev->next = ha;
327 } else {
328 ret = ha;
331 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);
333 return ret;
336 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
338 /* Start optimistic */
339 int res = AST_SENSE_ALLOW;
340 while (ha) {
341 #if 0 /* debugging code */
342 char iabuf[INET_ADDRSTRLEN];
343 char iabuf2[INET_ADDRSTRLEN];
344 /* DEBUG */
345 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
346 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
347 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
348 #endif
349 /* For each rule, if this address and the netmask = the net address
350 apply the current rule */
351 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
352 res = ha->sense;
353 ha = ha->next;
355 return res;
358 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
360 struct hostent *hp;
361 struct ast_hostent ahp;
362 char srv[256];
363 char host[256];
364 int tportno = ntohs(sin->sin_port);
365 if (service) {
366 snprintf(srv, sizeof(srv), "%s.%s", service, value);
367 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
368 sin->sin_port = htons(tportno);
369 value = host;
372 hp = ast_gethostbyname(value, &ahp);
373 if (hp) {
374 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
375 } else {
376 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
377 return -1;
379 return 0;
382 struct dscp_codepoint {
383 char *name;
384 unsigned int space;
387 /* IANA registered DSCP codepoints */
389 static const struct dscp_codepoint dscp_pool1[] = {
390 { "CS0", 0x00 },
391 { "CS1", 0x08 },
392 { "CS2", 0x10 },
393 { "CS3", 0x18 },
394 { "CS4", 0x20 },
395 { "CS5", 0x28 },
396 { "CS6", 0x30 },
397 { "CS7", 0x38 },
398 { "AF11", 0x0A },
399 { "AF12", 0x0C },
400 { "AF13", 0x0E },
401 { "AF21", 0x12 },
402 { "AF22", 0x14 },
403 { "AF23", 0x16 },
404 { "AF31", 0x1A },
405 { "AF32", 0x1C },
406 { "AF33", 0x1E },
407 { "AF41", 0x22 },
408 { "AF42", 0x24 },
409 { "AF43", 0x26 },
410 { "EF", 0x2E },
413 int ast_str2cos(const char *value, unsigned int *cos)
415 int fval;
417 if (sscanf(value, "%d", &fval) == 1) {
418 if (fval < 8) {
419 *cos = fval;
420 return 0;
424 return -1;
427 int ast_str2tos(const char *value, unsigned int *tos)
429 int fval;
430 unsigned int x;
432 if (sscanf(value, "%i", &fval) == 1) {
433 *tos = fval & 0xFF;
434 return 0;
437 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
438 if (!strcasecmp(value, dscp_pool1[x].name)) {
439 *tos = dscp_pool1[x].space << 2;
440 return 0;
444 return -1;
447 const char *ast_tos2str(unsigned int tos)
449 unsigned int x;
451 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
452 if (dscp_pool1[x].space == (tos >> 2))
453 return dscp_pool1[x].name;
456 return "unknown";
459 int ast_get_ip(struct sockaddr_in *sin, const char *value)
461 return ast_get_ip_or_srv(sin, value, NULL);
464 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
466 int s;
467 struct sockaddr_in sin;
468 socklen_t slen;
470 s = socket(PF_INET, SOCK_DGRAM, 0);
471 if (s < 0) {
472 ast_log(LOG_ERROR, "Cannot create socket\n");
473 return -1;
475 sin.sin_family = AF_INET;
476 sin.sin_port = 5060;
477 sin.sin_addr = *them;
478 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
479 ast_log(LOG_WARNING, "Cannot connect\n");
480 close(s);
481 return -1;
483 slen = sizeof(sin);
484 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
485 ast_log(LOG_WARNING, "Cannot get socket name\n");
486 close(s);
487 return -1;
489 close(s);
490 ast_debug(3, "Found IP address for this socket\n");
491 *us = sin.sin_addr;
492 return 0;
495 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
497 char ourhost[MAXHOSTNAMELEN] = "";
498 struct ast_hostent ahp;
499 struct hostent *hp;
500 struct in_addr saddr;
502 /* just use the bind address if it is nonzero */
503 if (ntohl(bindaddr.sin_addr.s_addr)) {
504 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
505 ast_debug(3, "Attached to given IP address\n");
506 return 0;
508 /* try to use our hostname */
509 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
510 ast_log(LOG_WARNING, "Unable to get hostname\n");
511 } else {
512 hp = ast_gethostbyname(ourhost, &ahp);
513 if (hp) {
514 memcpy(ourip, hp->h_addr, sizeof(*ourip));
515 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
516 return 0;
519 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
520 /* A.ROOT-SERVERS.NET. */
521 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
522 return 0;
523 return get_local_address(ourip);