Why try to be intelligent about address families too late
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
index 1401d77..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.24 (Berkeley) 84/07/06";
-#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>
@@ -19,7 +31,7 @@ static char sccsid[] = "@(#)rwhod.c   4.24 (Berkeley) 84/07/06";
 #include <ctype.h>
 #include <netdb.h>
 #include <syslog.h>
 #include <ctype.h>
 #include <netdb.h>
 #include <syslog.h>
-#include "rwhod.h"
+#include <protocols/rwhod.h>
 
 /*
  * Alarm interval. Don't forget to change the down time check in ruptime
 
 /*
  * Alarm interval. Don't forget to change the down time check in ruptime
@@ -73,8 +85,9 @@ 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");
 
        if (getuid()) {
                fprintf(stderr, "rwhod: not super user\n");
@@ -103,7 +116,7 @@ main()
 #endif
        (void) chdir("/dev");
        (void) signal(SIGHUP, getkmem);
 #endif
        (void) chdir("/dev");
        (void) signal(SIGHUP, getkmem);
-       openlog("rwhod", LOG_PID, 0);
+       openlog("rwhod", LOG_PID, LOG_DAEMON);
        /*
         * Establish host name as returned by system.
         */
        /*
         * Establish host name as returned by system.
         */
@@ -111,6 +124,8 @@ main()
                syslog(LOG_ERR, "gethostname: %m");
                exit(1);
        }
                syslog(LOG_ERR, "gethostname: %m");
                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) {
@@ -126,12 +141,10 @@ main()
                syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
                syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
-       hp = gethostbyname(myname);
-       if (hp == NULL) {
-               syslog(LOG_ERR, "%s: don't know my own name", 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) {
                syslog(LOG_ERR, "bind: %m");
        sin.sin_port = sp->s_port;
        if (bind(s, &sin, sizeof (sin)) < 0) {
                syslog(LOG_ERR, "bind: %m");
@@ -224,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()
@@ -241,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;
@@ -370,7 +397,8 @@ configure(s)
                        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;
                }
                        free((char *)np);
                        continue;
                }
@@ -386,12 +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((np->n_flags & IFF_LOCAL)?
-                               inet_subnetof(sin->sin_addr) :
-                               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;
@@ -402,32 +432,6 @@ configure(s)
        return (1);
 }
 
        return (1);
 }
 
-/*
- * Return the possible subnetwork number from an internet address.
- * If the address is of the form of a subnet address (most significant
- * bit of the host part is set), believe the subnet exists.
- * Otherwise, return the network number.  Any subnet number is only valid
- * if this is a ``local'' net.
- */
-inet_subnetof(in)
-       struct in_addr in;
-{
-       register u_long i = ntohl(in.s_addr);
-
-       if (IN_CLASSA(i)) {
-               if (IN_SUBNETA(i))
-                       return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT);
-               else
-                       return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
-       } else if (IN_CLASSB(i)) {
-               if (IN_SUBNETB(i))
-                       return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT);
-               else
-                       return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
-       } else
-               return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
-}
-
 #ifdef DEBUG
 sendto(s, buf, cc, flags, to, tolen)
        int s;
 #ifdef DEBUG
 sendto(s, buf, cc, flags, to, tolen)
        int s;