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