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