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