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