Route fixes for OpenBSD (bug #415)
[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 #ifdef __OpenBSD__
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         struct ast_ha *prev = NULL;
73         struct ast_ha *ret;
74         ret = path;
75         while(path) {
76                 prev = path;
77                 path = path->next;
78         }
79         if (ha) {
80                 char *stringp=NULL;
81                 stringp=stuff;
82                 strsep(&stringp, "/");
83                 nm = strsep(&stringp, "/");
84                 if (!nm)
85                         nm = "255.255.255.255";
86                 if (!inet_aton(stuff, &ha->netaddr)) {
87                         ast_log(LOG_WARNING, "%s not a valid IP\n", stuff);
88                         free(ha);
89                         return NULL;
90                 }
91                 if (!inet_aton(nm, &ha->netmask)) {
92                         ast_log(LOG_WARNING, "%s not a valid netmask\n", nm);
93                         free(ha);
94                         return NULL;
95                 }
96                 ha->netaddr.s_addr &= ha->netmask.s_addr;
97                 if (!strncasecmp(sense, "p", 1)) {
98                         ha->sense = AST_SENSE_ALLOW;
99                 } else {
100                         ha->sense = AST_SENSE_DENY;
101                 }
102                 ha->next = NULL;
103                 if (prev)
104                         prev->next = ha;
105                 else
106                         ret = ha;
107         }
108         return ret;
109 }
110
111 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
112 {
113         /* Start optimistic */
114         int res = AST_SENSE_ALLOW;
115         while(ha) {
116                 /* For each rule, if this address and the netmask = the net address
117                    apply the current rule */
118                 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == (ha->netaddr.s_addr))
119                         res = ha->sense;
120                 ha = ha->next;
121         }
122         return res;
123 }
124
125 int ast_get_ip(struct sockaddr_in *sin, char *value)
126 {
127         struct hostent *hp;
128         hp = gethostbyname(value);
129         if (hp) {
130                 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
131         } else {
132                 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
133                 return -1;
134         }
135         return 0;
136 }
137
138 int inaddrcmp(struct sockaddr_in *sin1, struct sockaddr_in *sin2)
139 {
140         return ((sin1->sin_addr.s_addr != sin2->sin_addr.s_addr )
141                         || (sin1->sin_port != sin2->sin_port));
142 }
143
144 /* iface is the interface (e.g. eth0); address is the return value */
145 int ast_lookup_iface(char *iface, struct in_addr *address) {
146         int mysock, res = 0;
147         struct my_ifreq ifreq;
148
149         memset(&ifreq, 0, sizeof(ifreq));
150         strncpy(ifreq.ifrn_name,iface,sizeof(ifreq.ifrn_name) - 1);
151
152         mysock = socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);
153         res = ioctl(mysock,SIOCGIFADDR,&ifreq);
154
155         close(mysock);
156         if (res < 0) {
157                 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
158                 memcpy((char *)address,(char *)&__ourip,sizeof(__ourip));
159                 return -1;
160         } else {
161                 memcpy((char *)address,(char *)&ifreq.ifru_addr.sin_addr,sizeof(ifreq.ifru_addr.sin_addr));
162                 return 0;
163         }
164 }
165
166 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
167 {
168 #ifdef __OpenBSD__
169         struct sockaddr_in *sin;
170         struct sockaddr *sa;
171         struct {
172                 struct  rt_msghdr m_rtm;
173                 char    m_space[512];
174         } m_rtmsg;
175         char *cp, *p = ast_strdupa(inet_ntoa(*them));
176         int i, l, s, seq, flags;
177         pid_t pid = getpid();
178         static int routeseq;    /* Protected by "routeseq_lock" mutex */
179
180         memset(us, 0, sizeof(struct in_addr));
181
182         memset(&m_rtmsg, 0, sizeof(m_rtmsg));
183         m_rtmsg.m_rtm.rtm_type = RTM_GET;
184         m_rtmsg.m_rtm.rtm_flags = RTF_UP | RTF_HOST;
185         m_rtmsg.m_rtm.rtm_version = RTM_VERSION;
186         ast_mutex_lock(&routeseq_lock);
187         seq = ++routeseq;
188         ast_mutex_unlock(&routeseq_lock);
189         m_rtmsg.m_rtm.rtm_seq = seq;
190         m_rtmsg.m_rtm.rtm_addrs = RTA_IFA | RTA_DST;
191         m_rtmsg.m_rtm.rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
192         sin = (struct sockaddr_in *)m_rtmsg.m_space;
193         sin->sin_family = AF_INET;
194         sin->sin_len = sizeof(struct sockaddr_in);
195         sin->sin_addr = *them;
196
197         if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
198                 ast_log(LOG_ERROR, "Error opening routing socket\n");
199                 return -1;
200         }
201         flags = fcntl(s, F_GETFL);
202         fcntl(s, F_SETFL, flags | O_NONBLOCK);
203         if (write(s, (char *)&m_rtmsg, m_rtmsg.m_rtm.rtm_msglen) < 0) {
204                 ast_log(LOG_ERROR, "Error writing to routing socket: %s\n", strerror(errno));
205                 close(s);
206                 return -1;
207         }
208         do {
209                 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
210         } while (l > 0 && (m_rtmsg.m_rtm.rtm_seq != 1 || m_rtmsg.m_rtm.rtm_pid != pid));
211         if (l < 0) {
212                 if (errno != EAGAIN)
213                         ast_log(LOG_ERROR, "Error reading from routing socket\n");
214                 close(s);
215                 return -1;
216         }
217         close(s);
218
219         if (m_rtmsg.m_rtm.rtm_version != RTM_VERSION) {
220                 ast_log(LOG_ERROR, "Unsupported route socket protocol version\n");
221                 return -1;
222         }
223
224         if (m_rtmsg.m_rtm.rtm_msglen != l)
225                 ast_log(LOG_WARNING, "Message length mismatch, in packet %d, returned %d\n",
226                                 m_rtmsg.m_rtm.rtm_msglen, l);
227
228         if (m_rtmsg.m_rtm.rtm_errno) {
229                 ast_log(LOG_ERROR, "RTM_GET got %s (%d)\n",
230                                 strerror(m_rtmsg.m_rtm.rtm_errno), m_rtmsg.m_rtm.rtm_errno);
231                 return -1;
232         }
233
234         cp = (char *)m_rtmsg.m_space;
235         if (m_rtmsg.m_rtm.rtm_addrs)
236                 for (i = 1; i; i <<= 1)
237                         if (m_rtmsg.m_rtm.rtm_addrs & i) {
238                                 sa = (struct sockaddr *)cp;
239                                 if (i == RTA_IFA && sa->sa_family == AF_INET) {
240                                         sin = (struct sockaddr_in *)sa;
241                                         *us = sin->sin_addr;
242                                         ast_log(LOG_DEBUG, "Found route to %s, output from our address %s.\n", p, inet_ntoa(*us));
243                                         return 0;
244                                 }
245                                 cp += sa->sa_len > 0 ?
246                                           (1 + ((sa->sa_len - 1) | (sizeof(long) - 1))) :
247                                           sizeof(long);
248                         }
249
250         ast_log(LOG_DEBUG, "No route found for address %s!\n", p);
251         return -1;
252 #else
253         FILE *PROC;
254         unsigned int remote_ip;
255         int res = 1;
256         char line[256];
257         remote_ip = them->s_addr;
258         
259         PROC = fopen("/proc/net/route","r");
260         if (!PROC) {
261                 bzero(us,sizeof(struct in_addr));
262                 return -1;
263         }
264         /* First line contains headers */
265         fgets(line,sizeof(line),PROC);
266
267         while (!feof(PROC)) {
268                 char iface[8];
269                 unsigned int dest, gateway, mask;
270                 int i,fieldnum;
271                 char *fields[40];
272
273                 fgets(line,sizeof(line),PROC);
274
275                 fieldnum = 0;
276                 for (i=0;i<sizeof(line);i++) {
277                         char *offset;
278
279                         fields[fieldnum++] = line + i;
280                         offset = strchr(line + i,'\t');
281                         if (offset == NULL) {
282                                 /* Exit loop */
283                                 break;
284                         } else if (fieldnum >= 9) {
285                                 /* Short-circuit: can't break at 8, since the end of field 7 is figured when fieldnum=8 */
286                                 break;
287                         } else {
288                                 *offset = '\0';
289                                 i = offset - line;
290                         }
291                 }
292
293                 sscanf(fields[0],"%s",iface);
294                 sscanf(fields[1],"%x",&dest);
295                 sscanf(fields[2],"%x",&gateway);
296                 sscanf(fields[7],"%x",&mask);
297 #if 0
298                 printf("Addr: %s %08x Dest: %08x Mask: %08x\n", inet_ntoa(*them), remote_ip, dest, mask);
299 #endif          
300                 /* Looks simple, but here is the magic */
301                 if (((remote_ip & mask) ^ dest) == 0) {
302                         res = ast_lookup_iface(iface,us);
303                         break;
304                 }
305         }
306         fclose(PROC);
307         if (res == 1) {
308                 ast_log(LOG_WARNING, "Yikes!  No default route?!!\n");
309                 bzero(us,sizeof(struct in_addr));
310                 return -2;
311         } else if (res) {
312                 /* We've already warned in subroutine */
313                 return -1;
314         }
315         return 0;
316 #endif
317 }