Even better, let it be specified per-context
[asterisk/asterisk.git] / acl.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Various sorts of access control
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <pthread.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <signal.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <asterisk/acl.h>
23 #include <asterisk/logger.h>
24 #include <asterisk/channel.h>
25 #include <arpa/inet.h>
26 #include <sys/socket.h>
27 #include <netdb.h>
28 #include <net/if.h>
29 #include <netinet/in_systm.h>
30 #include <netinet/ip.h>
31 #include <sys/ioctl.h>
32 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
33 #include <fcntl.h>
34 #include <net/route.h>
35
36 static ast_mutex_t routeseq_lock = AST_MUTEX_INITIALIZER;
37 #endif
38
39 #define AST_SENSE_DENY                  0
40 #define AST_SENSE_ALLOW                 1
41
42 struct ast_ha {
43         /* Host access rule */
44         struct in_addr netaddr;
45         struct in_addr netmask;
46         int sense;
47         struct ast_ha *next;
48 };
49
50 /* Default IP - if not otherwise set, don't breathe garbage */
51 static struct in_addr __ourip = { 0x00000000 };
52
53 struct my_ifreq {
54         char ifrn_name[IFNAMSIZ];       /* Interface name, e.g. "eth0", "ppp0", etc.  */
55         struct sockaddr_in ifru_addr;
56 };
57
58 void ast_free_ha(struct ast_ha *ha)
59 {
60         struct ast_ha *hal;
61         while(ha) {
62                 hal = ha;
63                 ha = ha->next;
64                 free(hal);
65         }
66 }
67
68 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
69 {
70         struct ast_ha *ha = malloc(sizeof(struct ast_ha));
71         char *nm;
72         char tmp[256] = "";
73         struct ast_ha *prev = NULL;
74         struct ast_ha *ret;
75         int x,z;
76         unsigned int y;
77         ret = path;
78         while(path) {
79                 prev = path;
80                 path = path->next;
81         }
82         if (ha) {
83                 strncpy(tmp, stuff, sizeof(tmp) - 1);
84                 nm = strchr(tmp, '/');
85                 if (!nm)
86                         nm = "255.255.255.255";
87                 else {
88                         *nm = '\0';
89                         nm++;
90                 }
91                 if (!strchr(nm, '.')) {
92                         if ((sscanf(nm, "%i", &x) == 1) && (x >= 0) && (x <= 32)) {
93                                 y = 0;
94                                 for (z=0;z<x;z++) {
95                                         y >>= 1;
96                                         y |= 0x80000000;
97                                 }
98                                 ha->netmask.s_addr = htonl(y);
99                         }
100                 } else if (!inet_aton(nm, &ha->netmask)) {
101                         ast_log(LOG_WARNING, "%s not a valid netmask\n", nm);
102                         free(ha);
103                         return path;
104                 }
105                 if (!inet_aton(tmp, &ha->netaddr)) {
106                         ast_log(LOG_WARNING, "%s not a valid IP\n", tmp);
107                         free(ha);
108                         return path;
109                 }
110                 ha->netaddr.s_addr &= ha->netmask.s_addr;
111                 if (!strncasecmp(sense, "p", 1)) {
112                         ha->sense = AST_SENSE_ALLOW;
113                 } else {
114                         ha->sense = AST_SENSE_DENY;
115                 }
116                 ha->next = NULL;
117                 if (prev)
118                         prev->next = ha;
119                 else
120                         ret = ha;
121         }
122         return ret;
123 }
124
125 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
126 {
127         /* Start optimistic */
128         int res = AST_SENSE_ALLOW;
129         while(ha) {
130                 /* For each rule, if this address and the netmask = the net address
131                    apply the current rule */
132                 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == (ha->netaddr.s_addr))
133                         res = ha->sense;
134                 ha = ha->next;
135         }
136         return res;
137 }
138
139 int ast_get_ip(struct sockaddr_in *sin, char *value)
140 {
141         struct hostent *hp;
142         struct ast_hostent ahp;
143         hp = ast_gethostbyname(value, &ahp);
144         if (hp) {
145                 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
146         } else {
147                 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
148                 return -1;
149         }
150         return 0;
151 }
152
153 /* iface is the interface (e.g. eth0); address is the return value */
154 int ast_lookup_iface(char *iface, struct in_addr *address) {
155         int mysock, res = 0;
156         struct my_ifreq ifreq;
157
158         memset(&ifreq, 0, sizeof(ifreq));
159         strncpy(ifreq.ifrn_name,iface,sizeof(ifreq.ifrn_name) - 1);
160
161         mysock = socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);
162         res = ioctl(mysock,SIOCGIFADDR,&ifreq);
163
164         close(mysock);
165         if (res < 0) {
166                 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
167                 memcpy((char *)address,(char *)&__ourip,sizeof(__ourip));
168                 return -1;
169         } else {
170                 memcpy((char *)address,(char *)&ifreq.ifru_addr.sin_addr,sizeof(ifreq.ifru_addr.sin_addr));
171                 return 0;
172         }
173 }
174
175 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
176 {
177 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
178         struct sockaddr_in *sin;
179         struct sockaddr *sa;
180         struct {
181                 struct  rt_msghdr m_rtm;
182                 char    m_space[512];
183         } m_rtmsg;
184         char *cp, *p = ast_strdupa(inet_ntoa(*them));
185         int i, l, s, seq, flags;
186         pid_t pid = getpid();
187         static int routeseq;    /* Protected by "routeseq_lock" mutex */
188
189         memset(us, 0, sizeof(struct in_addr));
190
191         memset(&m_rtmsg, 0, sizeof(m_rtmsg));
192         m_rtmsg.m_rtm.rtm_type = RTM_GET;
193         m_rtmsg.m_rtm.rtm_flags = RTF_UP | RTF_HOST;
194         m_rtmsg.m_rtm.rtm_version = RTM_VERSION;
195         ast_mutex_lock(&routeseq_lock);
196         seq = ++routeseq;
197         ast_mutex_unlock(&routeseq_lock);
198         m_rtmsg.m_rtm.rtm_seq = seq;
199         m_rtmsg.m_rtm.rtm_addrs = RTA_IFA | RTA_DST;
200         m_rtmsg.m_rtm.rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
201         sin = (struct sockaddr_in *)m_rtmsg.m_space;
202         sin->sin_family = AF_INET;
203         sin->sin_len = sizeof(struct sockaddr_in);
204         sin->sin_addr = *them;
205
206         if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
207                 ast_log(LOG_ERROR, "Error opening routing socket\n");
208                 return -1;
209         }
210         flags = fcntl(s, F_GETFL);
211         fcntl(s, F_SETFL, flags | O_NONBLOCK);
212         if (write(s, (char *)&m_rtmsg, m_rtmsg.m_rtm.rtm_msglen) < 0) {
213                 ast_log(LOG_ERROR, "Error writing to routing socket: %s\n", strerror(errno));
214                 close(s);
215                 return -1;
216         }
217         do {
218                 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
219         } while (l > 0 && (m_rtmsg.m_rtm.rtm_seq != 1 || m_rtmsg.m_rtm.rtm_pid != pid));
220         if (l < 0) {
221                 if (errno != EAGAIN)
222                         ast_log(LOG_ERROR, "Error reading from routing socket\n");
223                 close(s);
224                 return -1;
225         }
226         close(s);
227
228         if (m_rtmsg.m_rtm.rtm_version != RTM_VERSION) {
229                 ast_log(LOG_ERROR, "Unsupported route socket protocol version\n");
230                 return -1;
231         }
232
233         if (m_rtmsg.m_rtm.rtm_msglen != l)
234                 ast_log(LOG_WARNING, "Message length mismatch, in packet %d, returned %d\n",
235                                 m_rtmsg.m_rtm.rtm_msglen, l);
236
237         if (m_rtmsg.m_rtm.rtm_errno) {
238                 ast_log(LOG_ERROR, "RTM_GET got %s (%d)\n",
239                                 strerror(m_rtmsg.m_rtm.rtm_errno), m_rtmsg.m_rtm.rtm_errno);
240                 return -1;
241         }
242
243         cp = (char *)m_rtmsg.m_space;
244         if (m_rtmsg.m_rtm.rtm_addrs)
245                 for (i = 1; i; i <<= 1)
246                         if (m_rtmsg.m_rtm.rtm_addrs & i) {
247                                 sa = (struct sockaddr *)cp;
248                                 if (i == RTA_IFA && sa->sa_family == AF_INET) {
249                                         sin = (struct sockaddr_in *)sa;
250                                         *us = sin->sin_addr;
251                                         ast_log(LOG_DEBUG, "Found route to %s, output from our address %s.\n", p, inet_ntoa(*us));
252                                         return 0;
253                                 }
254                                 cp += sa->sa_len > 0 ?
255                                           (1 + ((sa->sa_len - 1) | (sizeof(long) - 1))) :
256                                           sizeof(long);
257                         }
258
259         ast_log(LOG_DEBUG, "No route found for address %s!\n", p);
260         return -1;
261 #else
262         FILE *PROC;
263         unsigned int remote_ip;
264         int res = 1;
265         char line[256];
266         remote_ip = them->s_addr;
267         
268         PROC = fopen("/proc/net/route","r");
269         if (!PROC) {
270                 bzero(us,sizeof(struct in_addr));
271                 return -1;
272         }
273         /* First line contains headers */
274         fgets(line,sizeof(line),PROC);
275
276         while (!feof(PROC)) {
277                 char iface[256];
278                 unsigned int dest, gateway, mask;
279                 int i,fieldnum;
280                 char *fields[40];
281
282                 fgets(line,sizeof(line),PROC);
283
284                 fieldnum = 0;
285                 for (i=0;i<sizeof(line);i++) {
286                         char *offset;
287
288                         fields[fieldnum++] = line + i;
289                         offset = strchr(line + i,'\t');
290                         if (offset == NULL) {
291                                 /* Exit loop */
292                                 break;
293                         } else if (fieldnum >= 9) {
294                                 /* Short-circuit: can't break at 8, since the end of field 7 is figured when fieldnum=8 */
295                                 break;
296                         } else {
297                                 *offset = '\0';
298                                 i = offset - line;
299                         }
300                 }
301                 if (fieldnum >= 8) {
302
303                         sscanf(fields[0],"%s",iface);
304                         sscanf(fields[1],"%x",&dest);
305                         sscanf(fields[2],"%x",&gateway);
306                         sscanf(fields[7],"%x",&mask);
307 #if 0
308                         printf("Addr: %s %08x Dest: %08x Mask: %08x\n", inet_ntoa(*them), remote_ip, dest, mask);
309 #endif          
310                         /* Looks simple, but here is the magic */
311                         if (((remote_ip & mask) ^ dest) == 0) {
312                                 res = ast_lookup_iface(iface,us);
313                                 break;
314                         }
315                 }
316         }
317         fclose(PROC);
318         if (res == 1) {
319                 ast_log(LOG_WARNING, "Yikes!  No default route?!!\n");
320                 bzero(us,sizeof(struct in_addr));
321                 return -2;
322         } else if (res) {
323                 /* We've already warned in subroutine */
324                 return -1;
325         }
326         return 0;
327 #endif
328 }