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