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