74d0c096d519c3b0d2c5b27e66beadd419885a58
[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                 char iabuf[INET_ADDRSTRLEN];
210                 char iabuf2[INET_ADDRSTRLEN];
211                 /* DEBUG */
212                 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
213                 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
214                 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
215                 /* For each rule, if this address and the netmask = the net address
216                    apply the current rule */
217                 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
218                         res = ha->sense;
219                 ha = ha->next;
220         }
221         return res;
222 }
223
224 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
225 {
226         struct hostent *hp;
227         struct ast_hostent ahp;
228         char srv[256];
229         char host[256];
230         int tportno = ntohs(sin->sin_port);
231         if (service) {
232                 snprintf(srv, sizeof(srv), "%s.%s", service, value);
233                 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
234                         sin->sin_port = htons(tportno);
235                         value = host;
236                 }
237         }
238         hp = ast_gethostbyname(value, &ahp);
239         if (hp) {
240                 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
241         } else {
242                 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
243                 return -1;
244         }
245         return 0;
246 }
247
248 struct dscp_codepoint {
249         char *name;
250         unsigned int space;
251 };
252
253 /* IANA registered DSCP codepoints */
254
255 static const struct dscp_codepoint dscp_pool1[] = {
256         { "CS0", 0x00 },
257         { "CS1", 0x08 },
258         { "CS2", 0x10 },
259         { "CS3", 0x18 },
260         { "CS4", 0x20 },
261         { "CS5", 0x28 },
262         { "CS6", 0x30 },
263         { "CS7", 0x38 },
264         { "AF11", 0x0A },
265         { "AF12", 0x0C },
266         { "AF13", 0x0E },
267         { "AF21", 0x12 },
268         { "AF22", 0x14 },
269         { "AF23", 0x16 },
270         { "AF31", 0x1A },
271         { "AF32", 0x1C },
272         { "AF33", 0x1E },
273         { "AF41", 0x22 },
274         { "AF42", 0x24 },
275         { "AF43", 0x26 },
276         { "EF", 0x2E },
277 };
278
279 int ast_str2cos(const char *value, unsigned int *cos) 
280 {
281         int fval;
282         
283         if (sscanf(value, "%d", &fval) == 1) {
284                 if (fval < 8) {
285                     *cos = fval;
286                     return 0;
287                 }
288         }
289         
290         return -1;
291 }
292
293 int ast_str2tos(const char *value, unsigned int *tos)
294 {
295         int fval;
296         unsigned int x;
297
298         if (sscanf(value, "%i", &fval) == 1) {
299                 *tos = fval & 0xFF;
300                 return 0;
301         }
302
303         for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
304                 if (!strcasecmp(value, dscp_pool1[x].name)) {
305                         *tos = dscp_pool1[x].space << 2;
306                         return 0;
307                 }
308         }
309
310         return -1;
311 }
312
313 const char *ast_tos2str(unsigned int tos)
314 {
315         unsigned int x;
316
317         for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
318                 if (dscp_pool1[x].space == (tos >> 2))
319                         return dscp_pool1[x].name;
320         }
321
322         return "unknown";
323 }
324
325 int ast_get_ip(struct sockaddr_in *sin, const char *value)
326 {
327         return ast_get_ip_or_srv(sin, value, NULL);
328 }
329
330 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
331 {
332         int s;
333         struct sockaddr_in sin;
334         socklen_t slen;
335
336         s = socket(PF_INET, SOCK_DGRAM, 0);
337         if (s < 0) {
338                 ast_log(LOG_WARNING, "Cannot create socket\n");
339                 return -1;
340         }
341         sin.sin_family = AF_INET;
342         sin.sin_port = 5060;
343         sin.sin_addr = *them;
344         if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
345                 ast_log(LOG_WARNING, "Cannot connect\n");
346                 close(s);
347                 return -1;
348         }
349         slen = sizeof(sin);
350         if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
351                 ast_log(LOG_WARNING, "Cannot get socket name\n");
352                 close(s);
353                 return -1;
354         }
355         close(s);
356         *us = sin.sin_addr;
357         return 0;
358 }
359
360 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
361 {
362         char ourhost[MAXHOSTNAMELEN] = "";
363         struct ast_hostent ahp;
364         struct hostent *hp;
365         struct in_addr saddr;
366
367         /* just use the bind address if it is nonzero */
368         if (ntohl(bindaddr.sin_addr.s_addr)) {
369                 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
370                 return 0;
371         }
372         /* try to use our hostname */
373         if (gethostname(ourhost, sizeof(ourhost) - 1)) {
374                 ast_log(LOG_WARNING, "Unable to get hostname\n");
375         } else {
376                 hp = ast_gethostbyname(ourhost, &ahp);
377                 if (hp) {
378                         memcpy(ourip, hp->h_addr, sizeof(*ourip));
379                         return 0;
380                 }
381         }
382         /* A.ROOT-SERVERS.NET. */
383         if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
384                 return 0;
385         return -1;
386 }
387