start using asterisk/network.h for network related headers.
[asterisk/asterisk.git] / main / acl.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, 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
19 /*! \file
20  *
21  * \brief Various sorts of access control
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <sys/time.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <sys/ioctl.h>
34
35 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
36 #include <fcntl.h>
37 #include <net/route.h>
38 #endif
39
40 #if defined(SOLARIS)
41 #include <sys/sockio.h>
42 #endif
43
44 #include "asterisk/acl.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/options.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/srv.h"
51
52 struct my_ifreq {
53         char ifrn_name[IFNAMSIZ];       /* Interface name, e.g. "eth0", "ppp0", etc.  */
54         struct sockaddr_in ifru_addr;
55 };
56
57 /* Free HA structure */
58 void ast_free_ha(struct ast_ha *ha)
59 {
60         struct ast_ha *hal;
61         while (ha) {
62                 hal = ha;
63                 ha = ha->next;
64                 ast_free(hal);
65         }
66 }
67
68 /* Copy HA structure */
69 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
70 {
71         memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
72         memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
73         to->sense = from->sense;
74 }
75
76 /* Create duplicate of ha structure */
77 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
78 {
79         struct ast_ha *new_ha;
80
81         if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
82                 /* Copy from original to new object */
83                 ast_copy_ha(original, new_ha);
84         }
85
86         return new_ha;
87 }
88
89 /* Create duplicate HA link list */
90 /*  Used in chan_sip2 templates */
91 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
92 {
93         struct ast_ha *start = original;
94         struct ast_ha *ret = NULL;
95         struct ast_ha *link, *prev = NULL;
96
97         while (start) {
98                 link = ast_duplicate_ha(start);  /* Create copy of this object */
99                 if (prev)
100                         prev->next = link;              /* Link previous to this object */
101
102                 if (!ret)
103                         ret = link;             /* Save starting point */
104
105                 start = start->next;            /* Go to next object */
106                 prev = link;                    /* Save pointer to this object */
107         }
108         return ret;                     /* Return start of list */
109 }
110
111 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
112 {
113         struct ast_ha *ha;
114         char *nm;
115         struct ast_ha *prev = NULL;
116         struct ast_ha *ret;
117         int x;
118         char *tmp = ast_strdupa(stuff);
119
120         ret = path;
121         while (path) {
122                 prev = path;
123                 path = path->next;
124         }
125
126         ha = ast_malloc(sizeof(*ha));
127         if (!ha)
128                 return ret;
129
130         nm = strchr(tmp, '/');
131         if (!nm) {
132                 /* assume /32. Yes, htonl does not do anything for this particular mask
133                    but we better use it to show we remember about byte order */
134                 ha->netmask.s_addr = htonl(0xFFFFFFFF);
135         } else {
136                 *nm = '\0';
137                 nm++;
138
139                 if (!strchr(nm, '.')) {
140                         if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
141                                 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
142                         else {
143                                 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
144                                 ast_free(ha);
145                                 if (error)
146                                         *error = 1;
147                                 return ret;
148                         }
149                 } else if (!inet_aton(nm, &ha->netmask)) {
150                         ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
151                         ast_free(ha);
152                         if (error)
153                                 *error = 1;
154                         return ret;
155                 }
156         }
157
158         if (!inet_aton(tmp, &ha->netaddr)) {
159                 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
160                 ast_free(ha);
161                 if (error)
162                         *error = 1;
163                 return ret;
164         }
165
166         ha->netaddr.s_addr &= ha->netmask.s_addr;
167
168         ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
169
170         ha->next = NULL;
171         if (prev) {
172                 prev->next = ha;
173         } else {
174                 ret = ha;
175         }
176
177         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);
178
179         return ret;
180 }
181
182 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
183 {
184         /* Start optimistic */
185         int res = AST_SENSE_ALLOW;
186         while (ha) {
187 #if 0   /* debugging code */
188                 char iabuf[INET_ADDRSTRLEN];
189                 char iabuf2[INET_ADDRSTRLEN];
190                 /* DEBUG */
191                 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
192                 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
193                 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
194 #endif
195                 /* For each rule, if this address and the netmask = the net address
196                    apply the current rule */
197                 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
198                         res = ha->sense;
199                 ha = ha->next;
200         }
201         return res;
202 }
203
204 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
205 {
206         struct hostent *hp;
207         struct ast_hostent ahp;
208         char srv[256];
209         char host[256];
210         int tportno = ntohs(sin->sin_port);
211         if (service) {
212                 snprintf(srv, sizeof(srv), "%s.%s", service, value);
213                 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
214                         sin->sin_port = htons(tportno);
215                         value = host;
216                 }
217         }
218         hp = ast_gethostbyname(value, &ahp);
219         if (hp) {
220                 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
221         } else {
222                 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
223                 return -1;
224         }
225         return 0;
226 }
227
228 struct dscp_codepoint {
229         char *name;
230         unsigned int space;
231 };
232
233 /* IANA registered DSCP codepoints */
234
235 static const struct dscp_codepoint dscp_pool1[] = {
236         { "CS0", 0x00 },
237         { "CS1", 0x08 },
238         { "CS2", 0x10 },
239         { "CS3", 0x18 },
240         { "CS4", 0x20 },
241         { "CS5", 0x28 },
242         { "CS6", 0x30 },
243         { "CS7", 0x38 },
244         { "AF11", 0x0A },
245         { "AF12", 0x0C },
246         { "AF13", 0x0E },
247         { "AF21", 0x12 },
248         { "AF22", 0x14 },
249         { "AF23", 0x16 },
250         { "AF31", 0x1A },
251         { "AF32", 0x1C },
252         { "AF33", 0x1E },
253         { "AF41", 0x22 },
254         { "AF42", 0x24 },
255         { "AF43", 0x26 },
256         { "EF", 0x2E },
257 };
258
259 int ast_str2cos(const char *value, unsigned int *cos) 
260 {
261         int fval;
262         
263         if (sscanf(value, "%d", &fval) == 1) {
264                 if (fval < 8) {
265                     *cos = fval;
266                     return 0;
267                 }
268         }
269         
270         return -1;
271 }
272
273 int ast_str2tos(const char *value, unsigned int *tos)
274 {
275         int fval;
276         unsigned int x;
277
278         if (sscanf(value, "%i", &fval) == 1) {
279                 *tos = fval & 0xFF;
280                 return 0;
281         }
282
283         for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
284                 if (!strcasecmp(value, dscp_pool1[x].name)) {
285                         *tos = dscp_pool1[x].space << 2;
286                         return 0;
287                 }
288         }
289
290         return -1;
291 }
292
293 const char *ast_tos2str(unsigned int tos)
294 {
295         unsigned int x;
296
297         for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
298                 if (dscp_pool1[x].space == (tos >> 2))
299                         return dscp_pool1[x].name;
300         }
301
302         return "unknown";
303 }
304
305 int ast_get_ip(struct sockaddr_in *sin, const char *value)
306 {
307         return ast_get_ip_or_srv(sin, value, NULL);
308 }
309
310 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
311 {
312         int s;
313         struct sockaddr_in sin;
314         socklen_t slen;
315         s = socket(PF_INET, SOCK_DGRAM, 0);
316         if (s < 0) {
317                 ast_log(LOG_WARNING, "Cannot create socket\n");
318                 return -1;
319         }
320         sin.sin_family = AF_INET;
321         sin.sin_port = 5060;
322         sin.sin_addr = *them;
323         if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
324                 ast_log(LOG_WARNING, "Cannot connect\n");
325                 close(s);
326                 return -1;
327         }
328         slen = sizeof(sin);
329         if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
330                 ast_log(LOG_WARNING, "Cannot get socket name\n");
331                 close(s);
332                 return -1;
333         }
334         close(s);
335         *us = sin.sin_addr;
336         return 0;
337 }
338
339 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
340 {
341         char ourhost[MAXHOSTNAMELEN] = "";
342         struct ast_hostent ahp;
343         struct hostent *hp;
344         struct in_addr saddr;
345
346         /* just use the bind address if it is nonzero */
347         if (ntohl(bindaddr.sin_addr.s_addr)) {
348                 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
349                 return 0;
350         }
351         /* try to use our hostname */
352         if (gethostname(ourhost, sizeof(ourhost) - 1)) {
353                 ast_log(LOG_WARNING, "Unable to get hostname\n");
354         } else {
355                 hp = ast_gethostbyname(ourhost, &ahp);
356                 if (hp) {
357                         memcpy(ourip, hp->h_addr, sizeof(*ourip));
358                         return 0;
359                 }
360         }
361         /* A.ROOT-SERVERS.NET. */
362         if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
363                 return 0;
364         return -1;
365 }
366