ast_func_read() needs a writable copy of the function name to be passed
[asterisk-bristuff.git] / main / acl.c
blob9000300f36641d0dbc63dce00d7ba248fbf51b22
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 ret;
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 ret;
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_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
212 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
213 ast_log(LOG_DEBUG, "##### Testing %s with %s\n", iabuf, iabuf2);
214 /* For each rule, if this address and the netmask = the net address
215 apply the current rule */
216 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
217 res = ha->sense;
218 ha = ha->next;
220 return res;
223 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
225 struct hostent *hp;
226 struct ast_hostent ahp;
227 char srv[256];
228 char host[256];
229 int tportno = ntohs(sin->sin_port);
230 if (inet_aton(value, &sin->sin_addr))
231 return 0;
232 if (service) {
233 snprintf(srv, sizeof(srv), "%s.%s", service, value);
234 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
235 sin->sin_port = htons(tportno);
236 value = host;
239 hp = ast_gethostbyname(value, &ahp);
240 if (hp) {
241 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
242 } else {
243 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
244 return -1;
246 return 0;
249 struct dscp_codepoint {
250 char *name;
251 unsigned int space;
254 /* IANA registered DSCP codepoints */
256 static const struct dscp_codepoint dscp_pool1[] = {
257 { "CS0", 0x00 },
258 { "CS1", 0x08 },
259 { "CS2", 0x10 },
260 { "CS3", 0x18 },
261 { "CS4", 0x20 },
262 { "CS5", 0x28 },
263 { "CS6", 0x30 },
264 { "CS7", 0x38 },
265 { "AF11", 0x0A },
266 { "AF12", 0x0C },
267 { "AF13", 0x0E },
268 { "AF21", 0x12 },
269 { "AF22", 0x14 },
270 { "AF23", 0x16 },
271 { "AF31", 0x1A },
272 { "AF32", 0x1C },
273 { "AF33", 0x1E },
274 { "AF41", 0x22 },
275 { "AF42", 0x24 },
276 { "AF43", 0x26 },
277 { "EF", 0x2E },
280 int ast_str2tos(const char *value, unsigned int *tos)
282 int fval;
283 unsigned int x;
285 if (sscanf(value, "%i", &fval) == 1) {
286 *tos = fval & 0xFF;
287 return 0;
290 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
291 if (!strcasecmp(value, dscp_pool1[x].name)) {
292 *tos = dscp_pool1[x].space << 2;
293 return 0;
297 if (!strcasecmp(value, "lowdelay"))
298 *tos = IPTOS_LOWDELAY;
299 else if (!strcasecmp(value, "throughput"))
300 *tos = IPTOS_THROUGHPUT;
301 else if (!strcasecmp(value, "reliability"))
302 *tos = IPTOS_RELIABILITY;
303 else if (!strcasecmp(value, "mincost"))
304 *tos = IPTOS_MINCOST;
305 else if (!strcasecmp(value, "none"))
306 *tos = 0;
307 else
308 return -1;
310 ast_log(LOG_WARNING, "TOS value %s is deprecated. Please see doc/ip-tos.txt for more information.\n", value);
312 return 0;
315 const char *ast_tos2str(unsigned int tos)
317 unsigned int x;
319 switch (tos) {
320 case 0:
321 return "none";
322 case IPTOS_LOWDELAY:
323 return "lowdelay";
324 case IPTOS_THROUGHPUT:
325 return "throughput";
326 case IPTOS_RELIABILITY:
327 return "reliability";
328 case IPTOS_MINCOST:
329 return "mincost";
330 default:
331 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
332 if (dscp_pool1[x].space == (tos >> 2))
333 return dscp_pool1[x].name;
337 return "unknown";
340 int ast_get_ip(struct sockaddr_in *sin, const char *value)
342 return ast_get_ip_or_srv(sin, value, NULL);
345 /* iface is the interface (e.g. eth0); address is the return value */
346 int ast_lookup_iface(char *iface, struct in_addr *address)
348 int mysock, res = 0;
349 struct my_ifreq ifreq;
351 memset(&ifreq, 0, sizeof(ifreq));
352 ast_copy_string(ifreq.ifrn_name, iface, sizeof(ifreq.ifrn_name));
354 mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
355 res = ioctl(mysock, SIOCGIFADDR, &ifreq);
357 close(mysock);
358 if (res < 0) {
359 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
360 memcpy((char *)address, (char *)&__ourip, sizeof(__ourip));
361 return -1;
362 } else {
363 memcpy((char *)address, (char *)&ifreq.ifru_addr.sin_addr, sizeof(ifreq.ifru_addr.sin_addr));
364 return 0;
368 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
370 int s;
371 struct sockaddr_in sin;
372 socklen_t slen;
374 s = socket(PF_INET, SOCK_DGRAM, 0);
375 if (s < 0) {
376 ast_log(LOG_WARNING, "Cannot create socket\n");
377 return -1;
379 sin.sin_family = AF_INET;
380 sin.sin_port = 5060;
381 sin.sin_addr = *them;
382 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
383 ast_log(LOG_WARNING, "Cannot connect\n");
384 close(s);
385 return -1;
387 slen = sizeof(sin);
388 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
389 ast_log(LOG_WARNING, "Cannot get socket name\n");
390 close(s);
391 return -1;
393 close(s);
394 *us = sin.sin_addr;
395 return 0;
398 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
400 char ourhost[MAXHOSTNAMELEN] = "";
401 struct ast_hostent ahp;
402 struct hostent *hp;
403 struct in_addr saddr;
405 /* just use the bind address if it is nonzero */
406 if (ntohl(bindaddr.sin_addr.s_addr)) {
407 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
408 return 0;
410 /* try to use our hostname */
411 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
412 ast_log(LOG_WARNING, "Unable to get hostname\n");
413 } else {
414 hp = ast_gethostbyname(ourhost, &ahp);
415 if (hp) {
416 memcpy(ourip, hp->h_addr, sizeof(*ourip));
417 return 0;
420 /* A.ROOT-SERVERS.NET. */
421 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
422 return 0;
423 return -1;