Why try to be intelligent about address families too late
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
index 3cc0ea8..e4d7a8f 100644 (file)
@@ -1,6 +1,18 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rwhod.c    4.21 (Berkeley) 83/11/14";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)rwhod.c    5.6 (Berkeley) %G%";
+#endif not lint
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -18,13 +30,14 @@ static char sccsid[] = "@(#)rwhod.c 4.21 (Berkeley) 83/11/14";
 #include <utmp.h>
 #include <ctype.h>
 #include <netdb.h>
 #include <utmp.h>
 #include <ctype.h>
 #include <netdb.h>
-#include "rwhod.h"
+#include <syslog.h>
+#include <protocols/rwhod.h>
 
 /*
  * Alarm interval. Don't forget to change the down time check in ruptime
  * if this is changed.
  */
 
 /*
  * Alarm interval. Don't forget to change the down time check in ruptime
  * if this is changed.
  */
-#define AL_INTERVAL (5 * 60)
+#define AL_INTERVAL (3 * 60)
 
 struct sockaddr_in sin = { AF_INET };
 
 
 struct sockaddr_in sin = { AF_INET };
 
@@ -72,9 +85,14 @@ main()
 {
        struct sockaddr_in from;
        char path[64];
 {
        struct sockaddr_in from;
        char path[64];
-       int addr;
-       struct hostent *hp;
+       int addr, on = 1;
+       char *cp;
+       extern char *index();
 
 
+       if (getuid()) {
+               fprintf(stderr, "rwhod: not super user\n");
+               exit(1);
+       }
        sp = getservbyname("who", "udp");
        if (sp == 0) {
                fprintf(stderr, "rwhod: udp/who: unknown service\n");
        sp = getservbyname("who", "udp");
        if (sp == 0) {
                fprintf(stderr, "rwhod: udp/who: unknown service\n");
@@ -98,17 +116,16 @@ main()
 #endif
        (void) chdir("/dev");
        (void) signal(SIGHUP, getkmem);
 #endif
        (void) chdir("/dev");
        (void) signal(SIGHUP, getkmem);
-       if (getuid()) {
-               fprintf(stderr, "rwhod: not super user\n");
-               exit(1);
-       }
+       openlog("rwhod", LOG_PID, LOG_DAEMON);
        /*
         * Establish host name as returned by system.
         */
        if (gethostname(myname, sizeof (myname) - 1) < 0) {
        /*
         * Establish host name as returned by system.
         */
        if (gethostname(myname, sizeof (myname) - 1) < 0) {
-               perror("gethostname");
+               syslog(LOG_ERR, "gethostname: %m");
                exit(1);
        }
                exit(1);
        }
+       if ((cp = index(myname, '.')) != NULL)
+               *cp = '\0';
        strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
        utmpf = open("/etc/utmp", O_RDONLY);
        if (utmpf < 0) {
        strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
        utmpf = open("/etc/utmp", O_RDONLY);
        if (utmpf < 0) {
@@ -116,23 +133,21 @@ main()
                utmpf = open("/etc/utmp", O_RDONLY);
        }
        if (utmpf < 0) {
                utmpf = open("/etc/utmp", O_RDONLY);
        }
        if (utmpf < 0) {
-               perror("rwhod: /etc/utmp");
+               syslog(LOG_ERR, "/etc/utmp: %m");
                exit(1);
        }
        getkmem();
        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                exit(1);
        }
        getkmem();
        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-               perror("rwhod: socket");
+               syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
                exit(1);
        }
-       hp = gethostbyname(myname);
-       if (hp == NULL) {
-               fprintf(stderr, "%s: don't know my own name\n", myname);
+       if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
+               syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
                exit(1);
        }
                exit(1);
        }
-       sin.sin_family = hp->h_addrtype;
        sin.sin_port = sp->s_port;
        if (bind(s, &sin, sizeof (sin)) < 0) {
        sin.sin_port = sp->s_port;
        if (bind(s, &sin, sizeof (sin)) < 0) {
-               perror("rwhod: bind");
+               syslog(LOG_ERR, "bind: %m");
                exit(1);
        }
        if (!configure(s))
                exit(1);
        }
        if (!configure(s))
@@ -147,17 +162,17 @@ main()
                        &from, &len);
                if (cc <= 0) {
                        if (cc < 0 && errno != EINTR)
                        &from, &len);
                if (cc <= 0) {
                        if (cc < 0 && errno != EINTR)
-                               perror("rwhod: recv");
+                               syslog(LOG_WARNING, "recv: %m");
                        continue;
                }
                if (from.sin_port != sp->s_port) {
                        continue;
                }
                if (from.sin_port != sp->s_port) {
-                       fprintf(stderr, "rwhod: %d: bad from port\n",
+                       syslog(LOG_WARNING, "%d: bad from port",
                                ntohs(from.sin_port));
                        continue;
                }
 #ifdef notdef
                if (gethostbyname(wd.wd_hostname) == 0) {
                                ntohs(from.sin_port));
                        continue;
                }
 #ifdef notdef
                if (gethostbyname(wd.wd_hostname) == 0) {
-                       fprintf(stderr, "rwhod: %s: unknown host\n",
+                       syslog(LOG_WARNING, "%s: unknown host",
                                wd.wd_hostname);
                        continue;
                }
                                wd.wd_hostname);
                        continue;
                }
@@ -167,15 +182,14 @@ main()
                if (wd.wd_type != WHODTYPE_STATUS)
                        continue;
                if (!verify(wd.wd_hostname)) {
                if (wd.wd_type != WHODTYPE_STATUS)
                        continue;
                if (!verify(wd.wd_hostname)) {
-                       fprintf(stderr, "rwhod: malformed host name from %x\n",
+                       syslog(LOG_WARNING, "malformed host name from %x",
                                from.sin_addr);
                        continue;
                }
                (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
                whod = creat(path, 0666);
                if (whod < 0) {
                                from.sin_addr);
                        continue;
                }
                (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
                whod = creat(path, 0666);
                if (whod < 0) {
-                       fprintf(stderr, "rwhod: ");
-                       perror(path);
+                       syslog(LOG_WARNING, "%s: %m", path);
                        continue;
                }
 #if vax || pdp11
                        continue;
                }
 #if vax || pdp11
@@ -223,7 +237,8 @@ verify(name)
 
 int    utmptime;
 int    utmpent;
 
 int    utmptime;
 int    utmpent;
-struct utmp utmp[100];
+int    utmpsize = 0;
+struct utmp *utmp;
 int    alarmcount;
 
 onalrm()
 int    alarmcount;
 
 onalrm()
@@ -240,9 +255,22 @@ onalrm()
                getkmem();
        alarmcount++;
        (void) fstat(utmpf, &stb);
                getkmem();
        alarmcount++;
        (void) fstat(utmpf, &stb);
-       if (stb.st_mtime != utmptime) {
+       if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
+               utmptime = stb.st_mtime;
+               if (stb.st_size > utmpsize) {
+                       utmpsize = stb.st_size + 10 * sizeof(struct utmp);
+                       if (utmp)
+                               utmp = (struct utmp *)realloc(utmp, utmpsize);
+                       else
+                               utmp = (struct utmp *)malloc(utmpsize);
+                       if (! utmp) {
+                               fprintf(stderr, "rwhod: malloc failed\n");
+                               utmpsize = 0;
+                               goto done;
+                       }
+               }
                (void) lseek(utmpf, (long)0, L_SET);
                (void) lseek(utmpf, (long)0, L_SET);
-               cc = read(utmpf, (char *)utmp, sizeof (utmp));
+               cc = read(utmpf, (char *)utmp, stb.st_size);
                if (cc < 0) {
                        perror("/etc/utmp");
                        goto done;
                if (cc < 0) {
                        perror("/etc/utmp");
                        goto done;
@@ -286,25 +314,31 @@ done:
 getkmem()
 {
        struct nlist *nlp;
 getkmem()
 {
        struct nlist *nlp;
+       static ino_t vmunixino;
+       static time_t vmunixctime;
+       struct stat sb;
 
 
+       if (stat("/vmunix", &sb) < 0) {
+               if (vmunixctime)
+                       return;
+       } else {
+               if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
+                       return;
+               vmunixctime = sb.st_ctime;
+               vmunixino= sb.st_ino;
+       }
        if (kmemf >= 0)
                (void) close(kmemf);
 loop:
        if (kmemf >= 0)
                (void) close(kmemf);
 loop:
-       for (nlp = &nl[sizeof (nl) / sizeof (nl[0])]; --nlp >= nl; ) {
-               nlp->n_value = 0;
-               nlp->n_type = 0;
-       }
-       nlist("/vmunix", nl);
-       if (nl[0].n_value == 0) {
-               fprintf(stderr, "/vmunix namelist botch\n");
+       if (nlist("/vmunix", nl)) {
+               syslog(LOG_WARNING, "/vmunix namelist botch");
                sleep(300);
                goto loop;
        }
        kmemf = open("/dev/kmem", O_RDONLY);
        if (kmemf < 0) {
                sleep(300);
                goto loop;
        }
        kmemf = open("/dev/kmem", O_RDONLY);
        if (kmemf < 0) {
-               perror("/dev/kmem");
-               sleep(300);
-               goto loop;
+               syslog(LOG_ERR, "/dev/kmem: %m");
+               exit(1);
        }
        (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
        (void) read(kmemf, (char *)&mywd.wd_boottime,
        }
        (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
        (void) read(kmemf, (char *)&mywd.wd_boottime,
@@ -329,7 +363,7 @@ configure(s)
        ifc.ifc_len = sizeof (buf);
        ifc.ifc_buf = buf;
        if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
        ifc.ifc_len = sizeof (buf);
        ifc.ifc_buf = buf;
        if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
-               perror("rwhod: ioctl (get interface configuration)");
+               syslog(LOG_ERR, "ioctl (get interface configuration)");
                return (0);
        }
        ifr = ifc.ifc_req;
                return (0);
        }
        ifr = ifc.ifc_req;
@@ -359,18 +393,19 @@ configure(s)
                }
                bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
                if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
                }
                bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
                if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
-                       perror("rwhod: ioctl (get interface flags)");
+                       syslog(LOG_ERR, "ioctl (get interface flags)");
                        free((char *)np);
                        continue;
                }
                        free((char *)np);
                        continue;
                }
-               if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
+               if ((ifreq.ifr_flags & IFF_UP) == 0 ||
+                   (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
                        free((char *)np);
                        continue;
                }
                np->n_flags = ifreq.ifr_flags;
                if (np->n_flags & IFF_POINTOPOINT) {
                        if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
                        free((char *)np);
                        continue;
                }
                np->n_flags = ifreq.ifr_flags;
                if (np->n_flags & IFF_POINTOPOINT) {
                        if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
-                               perror("rwhod: ioctl (get dstaddr)");
+                               syslog(LOG_ERR, "ioctl (get dstaddr)");
                                free((char *)np);
                                continue;
                        }
                                free((char *)np);
                                continue;
                        }
@@ -379,10 +414,14 @@ configure(s)
                          np->n_addr, np->n_addrlen);
                }
                if (np->n_flags & IFF_BROADCAST) {
                          np->n_addr, np->n_addrlen);
                }
                if (np->n_flags & IFF_BROADCAST) {
+                       if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
+                               syslog(LOG_ERR, "ioctl (get broadaddr)");
+                               free((char *)np);
+                               continue;
+                       }
                        /* we assume addresses are all the same size */
                        /* we assume addresses are all the same size */
-                       sin = (struct sockaddr_in *)np->n_addr;
-                       sin->sin_addr =
-                         inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
+                       bcopy((char *)&ifreq.ifr_broadaddr,
+                         np->n_addr, np->n_addrlen);
                }
                /* gag, wish we could get rid of Internet dependencies */
                sin = (struct sockaddr_in *)np->n_addr;
                }
                /* gag, wish we could get rid of Internet dependencies */
                sin = (struct sockaddr_in *)np->n_addr;