/* * interface.c -- implements fetchmail 'interface' and 'monitor' commands * * This module was implemented by George M. Sipe * or and is: * * Copyright (c) 1996,1997 by George M. Sipe * * FreeBSD specific portions written by and Copyright (c) 1999 * Andy Doran . * * For license terms, see the file COPYING in this directory. */ #include "fetchmail.h" #ifdef CAN_MONITOR #include #include #if defined(linux) #include #endif #include #include #if defined(STDC_HEADERS) #include #endif #if defined(HAVE_UNISTD_H) #include #endif #include #include #include #include #include #if defined(__FreeBSD__) #if defined __FreeBSD_USE_KVM #if __FreeBSD_version >= 300001 #include #endif #include #include #include #else #include #include #include #endif #endif #include "socket.h" #include "i18n.h" #include "tunable.h" typedef struct { struct in_addr addr, dstaddr, netmask; int rx_packets, tx_packets; } ifinfo_t; struct interface_pair_s { struct in_addr interface_address; struct in_addr interface_mask; } *interface_pair; /* * Count of packets to see on an interface before monitor considers it up. * Needed because when pppd shuts down the link, the packet counts go up * by two (one rx and one tx?, maybe). A value of 2 seems to do the trick, * but we'll give it some extra. */ #define MONITOR_SLOP 5 #ifdef linux #define have_interface_init static char *netdevfmt; void interface_init(void) /* figure out which /proc/net/dev format to use */ { struct utsname utsname; /* Linux 2.2 -- transmit packet count in 10th field */ netdevfmt = "%d %d %*d %*d %*d %d %*d %*d %*d %d %*d %*d %d"; if (uname(&utsname) < 0) return; else { int major, minor; if (sscanf(utsname.release, "%d.%d.%*d", &major, &minor) >= 2 && (major < 2 || (major == 2 && minor < 2))) /* pre-linux-2.2 format -- transmit packet count in 8th field */ netdevfmt = "%d %d %*d %*d %*d %d %*d %d %*d %*d %*d %*d %d"; } } static int _get_ifinfoGT_(int socket_fd, FILE *stats_file, const char *ifname, ifinfo_t *ifinfo) /* get active network interface information - return non-zero upon success */ { int namelen = strlen(ifname); struct ifreq request; char *cp, buffer[256]; int found = 0; int counts[4]; /* initialize result */ memset((char *) ifinfo, 0, sizeof(ifinfo_t)); /* get the packet I/O counts */ while (fgets(buffer, sizeof(buffer) - 1, stats_file)) { for (cp = buffer; *cp && *cp == ' '; ++cp); if (!strncmp(cp, ifname, namelen) && cp[namelen] == ':') { cp += namelen + 1; if (sscanf(cp, netdevfmt, counts, counts+1, counts+2, counts+3,&found)>4) { /* found = dummy */ /* newer kernel with byte counts */ ifinfo->rx_packets=counts[1]; ifinfo->tx_packets=counts[3]; } else { /* older kernel, no byte counts */ ifinfo->rx_packets=counts[0]; ifinfo->tx_packets=counts[2]; } found = 1; } } if (!found) return (FALSE); /* see if the interface is up */ strcpy(request.ifr_name, ifname); if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0) return(FALSE); if (!(request.ifr_flags & IFF_RUNNING)) return(FALSE); /* get the (local) IP address */ strcpy(request.ifr_name, ifname); if (ioctl(socket_fd, SIOCGIFADDR, &request) < 0) return(FALSE); ifinfo->addr = ((struct sockaddr_in *) (&request.ifr_addr))->sin_addr; /* get the PPP destination (remote) IP address */ ifinfo->dstaddr.s_addr = 0; strcpy(request.ifr_name, ifname); if (ioctl(socket_fd, SIOCGIFDSTADDR, &request) >= 0) ifinfo->dstaddr = ((struct sockaddr_in *) (&request.ifr_dstaddr))->sin_addr; /* get the netmask */ strcpy(request.ifr_name, ifname); if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) { ifinfo->netmask = ((struct sockaddr_in *) (&request.ifr_netmask))->sin_addr; return (TRUE); } return(FALSE); } static int get_ifinfo(const char *ifname, ifinfo_t *ifinfo) { int socket_fd = socket(AF_INET, SOCK_DGRAM, 0); FILE *stats_file = fopen("/proc/net/dev", "r"); int result; if (socket_fd < 0 || !stats_file) result = FALSE; else { char *sp = strchr(ifname, '/'); if (sp) *sp = '\0'; result = _get_ifinfoGT_(socket_fd, stats_file, ifname, ifinfo); if (sp) *sp = '/'; } if (socket_fd >= 0) SockClose(socket_fd); if (stats_file) fclose(stats_file); /* not checking should be safe, mode was "r" */ return(result); } #elif defined __FreeBSD__ #if defined __FreeBSD_USE_KVM static kvm_t *kvmfd; static struct nlist symbols[] = { {"_ifnet"}, {NULL} }; static u_long ifnet_savedaddr; static gid_t if_rgid; static gid_t if_egid; void interface_set_gids(gid_t egid, gid_t rgid) { if_rgid = rgid; if_egid = egid; } static int openkvm(void) { if ((kvmfd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL) return FALSE; if (kvm_nlist(kvmfd, symbols) < 0) return FALSE; if
#------------------------------------------------------------------------------
#
#               GotMail - Statistics Printing Script for GetMail
#
#             1999 by Thomas Nesges <ThomaNesges@TNT-Computer.de>
#
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# GotMail reads a GetMail logfile (/var/log/fetchmail.log) and prints
# statistics from all sessions logged in it, either as normal text on the
# Console, or as an html-file. The parsing is done with the awk-scripts
# gotmail.awk and gotmail.html.awk.
# You can configure its output with a file gotmail.conf either in your home,
# /etc, or in /usr/local/gotmail.
#
# GetMail has to be properly installed. For HTML output the htmllib has to be
# installed in /usr/local/htmllib.
#
# If you have any changes/corrections in the script, please send me email.
#------------------------------------------------------------------------------


#!/bin/sh

# Gotmail
# 1999 by Thomas Nesges <ThomasNesges@TNT-Computer.de>

# read the configuration
# the configuration can either be
#  ~/.gotmail.conf
#  /etc/gotmail.conf
#  /usr/local/gotmail/gotmail.conf
if { test -e ~/.gotmail.conf; };
    then { source ~/.gotmail.conf; };
    else { if { test -e /etc/gotmail.conf; }; 
          then { source /etc/gotmail.conf; };
          else { if { test -e /usr/local/gotmail/gotmail.conf; };
          then { source /usr/local/gotmail/gotmail.conf; };
                 else { echo 'Error: gotmail.conf could not be read';
			echo 'gotmail exits now..';
			exit; };
                 fi; };
          fi; };
fi;


# grep the fetchmail.log for relevant messages and save them in
# gotmails tempfile
cat /var/log/fetchmail.log | grep 'message' >> /tmp/gotmail.log.tmp
cat /var/log/fetchmail.log | grep 'Authorization' >> /tmp/gotmail.log.tmp
cat /var/log/fetchmail.log | grep 'fetchmail st' >> /tmp/gotmail.log.tmp


# parse the gotmail tempfile and prints a statistiks-screen
case "$1" in
  html)
    awk -f /usr/local/htmllib/htmllib.awk -f /usr/local/gotmail/gotmail.html.awk /tmp/gotmail.log.tmp > /dev/null
    ;;
  -v)
    echo 'gotmail version: 0.0.1'
    ;;
  *)
    awk -f /usr/local/gotmail/gotmail.awk /tmp/gotmail.log.tmp
    ;;
esac

# remove the gotmail tempfile
rm /tmp/gotmail.log.tmp
(!(cp2 = strchr(cp1, '/'))) cp2 = "255.255.255.255"; else *cp2++ = '\000'; /* convert IP address and netmask */ hp->interface_pair = (struct interface_pair_s *)xmalloc(sizeof(struct interface_pair_s)); if (!inet_aton(cp1, &hp->interface_pair->interface_address)) { (void) report(stderr, GT_("invalid IP interface address\n")); exit(PS_SYNTAX); } if (!inet_aton(cp2, &hp->interface_pair->interface_mask)) { (void) report(stderr, GT_("invalid IP interface mask\n")); exit(PS_SYNTAX); } /* apply the mask now to the IP address (range) required */ hp->interface_pair->interface_address.s_addr &= hp->interface_pair->interface_mask.s_addr; /* restore original interface string (for configuration dumper) */ *--cp1 = '/'; return; } void interface_note_activity(struct hostdata *hp) /* save interface I/O counts */ { ifinfo_t ifinfo; struct query *ctl; /* if not monitoring link, all done */ if (!hp->monitor) return; /* get the current I/O stats for the monitored link */ if (get_ifinfo(hp->monitor, &ifinfo)) /* update this and preceeding host entries using the link (they were already set during this pass but the I/O count has now changed and they need to be re-updated) */ for (ctl = querylist; ctl; ctl = ctl->next) { if (ctl->server.monitor && !strcmp(hp->monitor, ctl->server.monitor)) ctl->server.monitor_io = ifinfo.rx_packets + ifinfo.tx_packets; /* do NOT update host entries following this one */ if (&ctl->server == hp) break; } #ifdef ACTIVITY_DEBUG (void) report(stdout, GT_("activity on %s -noted- as %d\n"), hp->monitor, hp->monitor_io); #endif } int interface_approve(struct hostdata *hp, flag domonitor) /* return TRUE if OK to poll, FALSE otherwise */ { ifinfo_t ifinfo; /* check interface IP address (range), if specified */ if (hp->interface) { /* get interface info */ if (!get_ifinfo(hp->interface, &ifinfo)) { (void) report(stdout, GT_("skipping poll of %s, %s down\n"), hp->pollname, hp->interface); return(FALSE); } /* check the IP addresses (range) */ if (!( /* check remote IP address */ ((ifinfo.dstaddr.s_addr != 0) && (ifinfo.dstaddr.s_addr & hp->interface_pair->interface_mask.s_addr) == hp->interface_pair->interface_address.s_addr) || /* check local IP address */ ((ifinfo.addr.s_addr & hp->interface_pair->interface_mask.s_addr) == hp->interface_pair->interface_address.s_addr) ) ) { (void) report(stdout, GT_("skipping poll of %s, %s IP address excluded\n"), hp->pollname, hp->interface); return(FALSE); } } /* if not monitoring link, all done */ if (!domonitor || !hp->monitor) return(TRUE); #ifdef ACTIVITY_DEBUG (void) report(stdout, GT_("activity on %s checked as %d\n"), hp->monitor, hp->monitor_io); #endif /* if monitoring, check link for activity if it is up */ if (get_ifinfo(hp->monitor, &ifinfo)) { int diff = (ifinfo.rx_packets + ifinfo.tx_packets) - hp->monitor_io; /* * There are three cases here: * * (a) If the new packet count is less than the recorded one, * probably pppd was restarted while fetchmail was running. * Don't skip. * * (b) newpacket count is greater than the old packet count, * but the difference is small and may just reflect the overhead * of a link shutdown. Skip. * * (c) newpacket count is greater than the old packet count, * and the difference is large. Connection is live. Don't skip. */ if (diff >= 0 && diff <= MONITOR_SLOP) { (void) report(stdout, GT_("skipping poll of %s, %s inactive\n"), hp->pollname, hp->monitor); return(FALSE); } } #ifdef ACTIVITY_DEBUG report(stdout, GT_("activity on %s was %d, is %d\n"), hp->monitor, hp->monitor_io, ifinfo.rx_packets + ifinfo.tx_packets); #endif return(TRUE); } #endif /* CAN_MONITOR */ #ifndef have_interface_init void interface_init(void) {}; #endif