]> Pileus Git - ~andy/fetchmail/blob - interface.c
FreeBSD support.
[~andy/fetchmail] / interface.c
1 /*
2  * interface.c -- implements fetchmail 'interface' and 'monitor' commands
3  *
4  * This module was implemented by George M. Sipe <gsipe@mindspring.com>
5  * or <gsipe@acm.org> and is:
6  *
7  *      Copyright (c) 1996,1997 by George M. Sipe
8  *
9  *      FreeBSD specific portions written by and Copyright (c) 1999 
10  *      Andy Doran <ad@psn.ie>.
11  *
12  * This is free software; you can redistribute it and/or modify it under
13  * the terms of the GNU General Public License as published by the Free
14  * Software Foundation; version 2, or (at your option) any later version.
15  */
16 #include <sys/types.h>
17 #include <sys/param.h>
18
19 #if (defined(linux) && !defined(INET6)) || defined(__FreeBSD__)
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <string.h>
24 #if defined(STDC_HEADERS)
25 #include <stdlib.h>
26 #endif
27 #if defined(HAVE_UNISTD_H)
28 #include <unistd.h>
29 #endif
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <net/if.h>
35 #if defined(__FreeBSD__)
36 #if __FreeBSD_version >= 300001
37 #include <net/if_var.h>
38 #endif
39 #include <kvm.h>
40 #include <nlist.h>
41 #include <sys/fcntl.h>
42 #endif
43 #include "config.h"
44 #include "fetchmail.h"
45 #include "i18n.h"
46
47 typedef struct {
48         struct in_addr addr, dstaddr, netmask;
49         int rx_packets, tx_packets;
50 } ifinfo_t;
51
52 struct interface_pair_s {
53         struct in_addr interface_address;
54         struct in_addr interface_mask;
55 } *interface_pair;
56
57 static char *netdevfmt;
58
59 void interface_init(void)
60 /* figure out which /roc/dev/net format to use */
61 {
62     FILE *fp = popen("uname -r", "r");  /* still wins if /proc is out */
63
64     /* pre-linux-2.2 format -- transmit packet count in 8th field */
65     netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d";
66
67     if (!fp)
68         return;
69     else
70     {
71         int major, minor;
72
73         if (fscanf(fp, "%d.%d.%*d", &major, &minor) != 2)
74             return;
75
76         if (major >= 2 && minor >= 2)
77             /* Linux 2.2 -- transmit packet count in 10th field */
78             netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %*d %d %*d %d";
79     }
80 }
81
82
83 #if defined(linux)
84
85 static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname,
86                 ifinfo_t *ifinfo)
87 /* get active network interface information - return non-zero upon success */
88 {
89         int namelen = strlen(ifname);
90         struct ifreq request;
91         char *cp, buffer[256];
92         int found = 0;
93         int counts[4];
94
95         /* initialize result */
96         memset((char *) ifinfo, 0, sizeof(ifinfo_t));
97
98         /* get the packet I/O counts */
99         while (fgets(buffer, sizeof(buffer) - 1, stats_file)) {
100                 for (cp = buffer; *cp && *cp == ' '; ++cp);
101                 if (!strncmp(cp, ifname, namelen) &&
102                                 cp[namelen] == ':') {
103                         cp += namelen + 1;
104                         if (sscanf(cp, netdevfmt,
105                                    counts, counts+1, counts+2, 
106                                    counts+3,&found)>4) { /* found = dummy */
107                                 /* newer kernel with byte counts */
108                                 ifinfo->rx_packets=counts[1];
109                                 ifinfo->tx_packets=counts[3];
110                         } else {
111                                 /* older kernel, no byte counts */
112                                 ifinfo->rx_packets=counts[0];
113                                 ifinfo->tx_packets=counts[2];
114                         }
115                         found = 1;
116                 }
117         }
118         if (!found) return (FALSE);
119
120         /* see if the interface is up */
121         strcpy(request.ifr_name, ifname);
122         if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0)
123                 return(FALSE);
124         if (!(request.ifr_flags & IFF_RUNNING))
125                 return(FALSE);
126
127         /* get the IP address */
128         strcpy(request.ifr_name, ifname);
129         if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0)
130                 return(FALSE);
131         ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr;
132
133         /* get the PPP destination IP address */
134         strcpy(request.ifr_name, ifname);
135         if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0)
136                 ifinfo->dstaddr = ((struct sockaddr_in *)
137                                         (&request.ifr_dstaddr))->sin_addr;
138
139         /* get the netmask */
140         strcpy(request.ifr_name, ifname);
141         if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) {
142           ifinfo->netmask = ((struct sockaddr_in *)
143                              (&request.ifr_netmask))->sin_addr;
144           return (TRUE);
145         }
146
147         return(FALSE);
148 }
149
150 static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
151 {
152         int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
153         FILE *stats_file = fopen("/proc/net/dev", "r");
154         int result;
155
156         if (socket_fd < 0 || !stats_file)
157                 result = FALSE;
158         else
159         {
160             char        *sp = strchr(ifname, '/');
161
162             if (sp)
163                 *sp = '\0';
164             result = _get_ifinfo_(socket_fd, stats_file, ifname, ifinfo);
165             if (sp)
166                 *sp = '/';
167         }
168         if (socket_fd >= 0)
169                 close(socket_fd);
170         if (stats_file)
171                 fclose(stats_file);
172         return(result);
173 }
174
175 #elif defined __FreeBSD__
176
177 static kvm_t *kvmfd;
178 static struct nlist symbols[] = 
179 {
180         {"_ifnet"},
181         {NULL}
182 };
183 static u_long   ifnet_savedaddr;
184 static gid_t    if_rgid;
185 static gid_t    if_egid;
186
187 void 
188 interface_set_gids(gid_t egid, gid_t rgid)
189 {
190         if_rgid = rgid;
191         if_egid = egid;
192 }
193
194 static int 
195 openkvm(void)
196 {
197         if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
198                 return FALSE;
199         
200         if (kvm_nlist(kvmfd, symbols) < 0)
201                 return FALSE;
202            
203         if (kvm_read(kvmfd, (unsigned long) symbols[0].n_value, &ifnet_savedaddr, sizeof(unsigned long)) == -1)
204                 return FALSE;
205                 
206         return TRUE;
207 }
208
209 static int 
210 get_ifinfo(const char *ifname, ifinfo_t *ifinfo)
211 {
212         char                    tname[16];
213         char                    iname[16];
214         struct ifnet            ifnet;
215         unsigned long           ifnet_addr = ifnet_savedaddr;
216 #if __FreeBSD_version >= 300001
217         struct ifnethead        ifnethead;
218         struct ifaddrhead       ifaddrhead;
219 #endif
220         struct ifaddr           ifaddr;
221         unsigned long           ifaddr_addr;
222         struct sockaddr         sa;
223         unsigned long           sa_addr;
224         uint                    i;
225         
226         if (if_egid)
227                 setegid(if_egid);
228         
229         for (i = 0; ifname[i] && ifname[i] != '/'; i++)
230                 iname[i] = ifname[i];
231                 
232         iname[i] = '\0';
233         
234         if (!kvmfd)
235         {
236                 if (!openkvm())
237                 {
238                         report(stderr, 0, _("Unable to open kvm interface. Make sure fetchmail is SGID kmem."));
239                         if (if_egid)
240                                 setegid(if_rgid);
241                         exit(1);
242                 }
243         }
244
245 #if __FreeBSD_version >= 300001
246         kvm_read(kvmfd, ifnet_savedaddr, (char *) &ifnethead, sizeof ifnethead);
247         ifnet_addr = (u_long) ifnethead.tqh_first;
248 #else
249         ifnet_addr = ifnet_savedaddr;
250 #endif
251
252         while (ifnet_addr)
253         {
254                 kvm_read(kvmfd, ifnet_addr, &ifnet, sizeof(ifnet));
255                 kvm_read(kvmfd, (unsigned long) ifnet.if_name, tname, sizeof tname);
256                 snprintf(tname, sizeof tname - 1, "%s%d", tname, ifnet.if_unit);
257
258                 if (!strcmp(tname, iname))
259                 {
260                         if (!(ifnet.if_flags & IFF_UP))
261                         {
262                                 if (if_egid)
263                                         setegid(if_rgid);
264                                 return 0;
265                         }
266                                 
267                         ifinfo->rx_packets = ifnet.if_ipackets;
268                         ifinfo->tx_packets = ifnet.if_opackets;
269
270 #if __FreeBSD_version >= 300001
271                         ifaddr_addr = (u_long) ifnet.if_addrhead.tqh_first;
272 #else
273                         ifaddr_addr = (u_long) ifnet.if_addrlist;
274 #endif
275                         
276                         while(ifaddr_addr)
277                         {
278                                 kvm_read(kvmfd, ifaddr_addr, &ifaddr, sizeof(ifaddr));
279                                 kvm_read(kvmfd, (u_long)ifaddr.ifa_addr, &sa, sizeof(sa));
280                                 
281                                 if (sa.sa_family != AF_INET)
282                                 {
283 #if __FreeBSD_version >= 300001
284                                         ifaddr_addr = (u_long) ifaddr.ifa_link.tqe_next;
285 #else
286                                         ifaddr_addr = (u_long) ifaddr.ifa_next;
287 #endif
288                                         continue;
289                                 }
290                         
291                                 ifinfo->addr.s_addr = *(u_long *)(sa.sa_data + 2);
292                                 kvm_read(kvmfd, (u_long)ifaddr.ifa_dstaddr, &sa, sizeof(sa));
293                                 ifinfo->dstaddr.s_addr = *(u_long *)(sa.sa_data + 2);
294                                 kvm_read(kvmfd, (u_long)ifaddr.ifa_netmask, &sa, sizeof(sa));
295                                 ifinfo->netmask.s_addr = *(u_long *)(sa.sa_data + 2);
296
297                                 if (if_egid)
298                                         setegid(if_rgid);
299
300                                 return 1;
301                         }
302                         
303                         if (if_egid)
304                                 setegid(if_rgid);
305                         
306                         return 0;
307                 }
308
309 #if __FreeBSD_version >= 300001
310                 ifnet_addr = (u_long) ifnet.if_link.tqe_next;
311 #else
312                 ifnet_addr = (unsigned long) ifnet.if_next;
313 #endif
314         }
315
316         if (if_egid)
317                 setegid(if_rgid);
318         
319         return 0;
320 }
321 #endif /* defined __FreeBSD__ */
322
323
324 #ifndef HAVE_INET_ATON
325 /*
326  * Note: This is not a true replacement for inet_aton(), as it won't
327  * do the right thing on "255.255.255.255" (which translates to -1 on
328  * most machines).  Fortunately this code will be used only if you're
329  * on an older Linux that lacks a real implementation.
330  */
331 #ifdef HAVE_NETINET_IN_SYSTM_H
332 # include <sys/types.h>
333 # include <netinet/in_systm.h>
334 #endif
335
336 #include <netinet/in.h>
337 #include <netinet/ip.h>
338 #include <arpa/inet.h>
339 #include <string.h>
340
341 static int inet_aton(const char *cp, struct in_addr *inp) {
342     long addr;
343
344     addr = inet_addr(cp);
345     if (addr == ((long) -1)) return 0;
346
347     memcpy(inp, &addr, sizeof(addr));
348     return 1;
349 }
350 #endif /* HAVE_INET_ATON */
351
352 void interface_parse(char *buf, struct hostdata *hp)
353 /* parse 'interface' specification */
354 {
355         char *cp1, *cp2;
356
357         hp->interface = xstrdup(buf);
358
359         /* find and isolate just the IP address */
360         if (!(cp1 = strchr(buf, '/')))
361         {
362                 (void) report(stderr,
363                               _("missing IP interface address\n"));
364                 exit(PS_SYNTAX);
365         }
366         *cp1++ = '\000';
367
368         /* find and isolate just the netmask */
369         if (!(cp2 = strchr(cp1, '/')))
370                 cp2 = "255.255.255.255";
371         else
372                 *cp2++ = '\000';
373
374         /* convert IP address and netmask */
375         hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s));
376         if (!inet_aton(cp1, &hp->interface_pair->interface_address))
377         {
378                 (void) report(stderr,
379                               _("invalid IP interface address\n"));
380                 exit(PS_SYNTAX);
381         }
382         if (!inet_aton(cp2, &hp->interface_pair->interface_mask))
383         {
384                 (void) report(stderr,
385                               _("invalid IP interface mask\n"));
386                 exit(PS_SYNTAX);
387         }
388         /* apply the mask now to the IP address (range) required */
389         hp->interface_pair->interface_address.s_addr &=
390                 hp->interface_pair->interface_mask.s_addr;
391
392         /* restore original interface string (for configuration dumper) */
393         *--cp1 = '/';
394         return;
395 }
396
397 void interface_note_activity(struct hostdata *hp)
398 /* save interface I/O counts */
399 {
400         ifinfo_t ifinfo;
401         struct query *ctl;
402
403         /* if not monitoring link, all done */
404         if (!hp->monitor)
405                 return;
406
407         /* get the current I/O stats for the monitored link */
408         if (get_ifinfo(hp->monitor, &ifinfo))
409                 /* update this and preceeding host entries using the link
410                    (they were already set during this pass but the I/O
411                    count has now changed and they need to be re-updated)
412                 */
413                 for (ctl = querylist; ctl; ctl = ctl->next) {
414                         if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor))
415                                 ctl->server.monitor_io =
416                                         ifinfo.rx_packets + ifinfo.tx_packets;
417                         /* do NOT update host entries following this one */
418                         if (&ctl->server == hp)
419                                 break;
420                 }
421
422 #ifdef  ACTIVITY_DEBUG
423         (void) report(stdout, 
424                       _("activity on %s -noted- as %d\n"), 
425                       hp->monitor, hp->monitor_io);
426 #endif
427 }
428
429 int interface_approve(struct hostdata *hp)
430 /* return TRUE if OK to poll, FALSE otherwise */
431 {
432         ifinfo_t ifinfo;
433
434         /* check interface IP address (range), if specified */
435         if (hp->interface) {
436                 /* get interface info */
437                 if (!get_ifinfo(hp->interface, &ifinfo)) {
438                         (void) report(stdout, 
439                                       _("skipping poll of %s, %s down\n"),
440                                       hp->pollname, hp->interface);
441                         return(FALSE);
442                 }
443                 /* check the IP address (range) */
444                 if ((ifinfo.addr.s_addr &
445                                 hp->interface_pair->interface_mask.s_addr) !=
446                                 hp->interface_pair->interface_address.s_addr) {
447                         (void) report(stdout,
448                                 _("skipping poll of %s, %s IP address excluded\n"),
449                                 hp->pollname, hp->interface);
450                         return(FALSE);
451                 }
452         }
453
454         /* if not monitoring link, all done */
455         if (!hp->monitor)
456                 return(TRUE);
457
458 #ifdef  ACTIVITY_DEBUG
459         (void) report(stdout, 
460                       _("activity on %s checked as %d\n"), 
461                       hp->monitor, hp->monitor_io);
462 #endif
463         /* if monitoring, check link for activity if it is up */
464         if (get_ifinfo(hp->monitor, &ifinfo) &&
465                         hp->monitor_io == ifinfo.rx_packets +
466                                 ifinfo.tx_packets) {
467                 (void) report(stdout, 
468                               _("skipping poll of %s, %s inactive\n"),
469                               hp->pollname, hp->monitor);
470                 return(FALSE);
471         }
472
473 #ifdef ACTIVITY_DEBUG
474        error(0, 0, _("activity on %s was %d, is %d"),
475              hp->monitor, hp->monitor_io,
476              ifinfo.rx_packets + ifinfo.tx_packets);
477 #endif
478
479         return(TRUE);
480 }
481 #endif /* (defined(linux) && !defined(INET6)) || defined(__FreeBSD__) */