16460Swnj #ifndef lint 2*17303Stef static char sccsid[] = "@(#)rwhod.c 4.27 (Berkeley) 84/10/23"; 36460Swnj #endif 46460Swnj 59215Ssam #include <sys/types.h> 69215Ssam #include <sys/socket.h> 79215Ssam #include <sys/stat.h> 89215Ssam #include <sys/ioctl.h> 913187Ssam #include <sys/file.h> 109215Ssam 1112236Ssam #include <net/if.h> 129215Ssam #include <netinet/in.h> 139215Ssam 149215Ssam #include <nlist.h> 156460Swnj #include <stdio.h> 166460Swnj #include <signal.h> 176460Swnj #include <errno.h> 186460Swnj #include <utmp.h> 198486Ssam #include <ctype.h> 208486Ssam #include <netdb.h> 2116673Sralph #include <syslog.h> 2213567Ssam #include "rwhod.h" 236460Swnj 2415541Sralph /* 2515541Sralph * Alarm interval. Don't forget to change the down time check in ruptime 2615541Sralph * if this is changed. 2715541Sralph */ 2816673Sralph #define AL_INTERVAL (3 * 60) 2915541Sralph 308486Ssam struct sockaddr_in sin = { AF_INET }; 316460Swnj 326460Swnj extern errno; 336460Swnj 3412236Ssam char myname[32]; 356460Swnj 366460Swnj struct nlist nl[] = { 376460Swnj #define NL_AVENRUN 0 386460Swnj { "_avenrun" }, 399215Ssam #define NL_BOOTTIME 1 409215Ssam { "_boottime" }, 416460Swnj 0 426460Swnj }; 436460Swnj 4412236Ssam /* 4512236Ssam * We communicate with each neighbor in 4612236Ssam * a list constructed at the time we're 4712236Ssam * started up. Neighbors are currently 4812236Ssam * directly connected via a hardware interface. 4912236Ssam */ 5012236Ssam struct neighbor { 5112236Ssam struct neighbor *n_next; 5212236Ssam char *n_name; /* interface name */ 5312236Ssam char *n_addr; /* who to send to */ 5412236Ssam int n_addrlen; /* size of address */ 5512236Ssam int n_flags; /* should forward?, interface flags */ 5612236Ssam }; 5712236Ssam 5812236Ssam struct neighbor *neighbors; 596460Swnj struct whod mywd; 6012236Ssam struct servent *sp; 616460Swnj int s, utmpf, kmemf = -1; 626460Swnj 6312236Ssam #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we)) 6412236Ssam #define RWHODIR "/usr/spool/rwho" 6511834Ssam 666460Swnj int onalrm(); 6712236Ssam char *strcpy(), *sprintf(), *malloc(); 686460Swnj long lseek(); 696460Swnj int getkmem(); 7012236Ssam struct in_addr inet_makeaddr(); 716460Swnj 726460Swnj main() 736460Swnj { 746460Swnj struct sockaddr_in from; 756460Swnj char path[64]; 7617155Ssam int addr, on = 1; 7712236Ssam struct hostent *hp; 786460Swnj 7916673Sralph if (getuid()) { 8016673Sralph fprintf(stderr, "rwhod: not super user\n"); 8116673Sralph exit(1); 8216673Sralph } 838486Ssam sp = getservbyname("who", "udp"); 848486Ssam if (sp == 0) { 858486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 868486Ssam exit(1); 878486Ssam } 886460Swnj #ifndef DEBUG 896460Swnj if (fork()) 906460Swnj exit(0); 916460Swnj { int s; 926460Swnj for (s = 0; s < 10; s++) 936460Swnj (void) close(s); 946460Swnj (void) open("/", 0); 956460Swnj (void) dup2(0, 1); 966460Swnj (void) dup2(0, 2); 976460Swnj s = open("/dev/tty", 2); 986460Swnj if (s >= 0) { 996460Swnj ioctl(s, TIOCNOTTY, 0); 1006460Swnj (void) close(s); 1016460Swnj } 1026460Swnj } 1036460Swnj #endif 1046460Swnj (void) chdir("/dev"); 1056460Swnj (void) signal(SIGHUP, getkmem); 10616673Sralph openlog("rwhod", LOG_PID, 0); 10712236Ssam /* 10812236Ssam * Establish host name as returned by system. 10912236Ssam */ 11012236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 11116673Sralph syslog(LOG_ERR, "gethostname: %m"); 1126460Swnj exit(1); 1136460Swnj } 11412236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 11513187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1166460Swnj if (utmpf < 0) { 1176460Swnj (void) close(creat("/etc/utmp", 0644)); 11813187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1196460Swnj } 1206460Swnj if (utmpf < 0) { 12116673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 1226460Swnj exit(1); 1236460Swnj } 1246460Swnj getkmem(); 12513187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 12616673Sralph syslog(LOG_ERR, "socket: %m"); 1279215Ssam exit(1); 1286460Swnj } 12917155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 13017055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 13117055Skarels exit(1); 13217055Skarels } 13312236Ssam hp = gethostbyname(myname); 13412236Ssam if (hp == NULL) { 13516673Sralph syslog(LOG_ERR, "%s: don't know my own name", myname); 13612236Ssam exit(1); 13712236Ssam } 13812236Ssam sin.sin_family = hp->h_addrtype; 13912236Ssam sin.sin_port = sp->s_port; 14013187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 14116673Sralph syslog(LOG_ERR, "bind: %m"); 1429215Ssam exit(1); 1439215Ssam } 14412236Ssam if (!configure(s)) 14512236Ssam exit(1); 14613027Ssam signal(SIGALRM, onalrm); 1476460Swnj onalrm(); 1486460Swnj for (;;) { 1496460Swnj struct whod wd; 15012236Ssam int cc, whod, len = sizeof (from); 1516460Swnj 15212236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 15312236Ssam &from, &len); 1546460Swnj if (cc <= 0) { 1556460Swnj if (cc < 0 && errno != EINTR) 15616673Sralph syslog(LOG_WARNING, "recv: %m"); 1576460Swnj continue; 1586460Swnj } 1598486Ssam if (from.sin_port != sp->s_port) { 16016673Sralph syslog(LOG_WARNING, "%d: bad from port", 1618486Ssam ntohs(from.sin_port)); 1626460Swnj continue; 1636460Swnj } 1648486Ssam #ifdef notdef 1658486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 16616673Sralph syslog(LOG_WARNING, "%s: unknown host", 1678486Ssam wd.wd_hostname); 1686460Swnj continue; 1696460Swnj } 1708486Ssam #endif 17112347Ssam if (wd.wd_vers != WHODVERSION) 17212347Ssam continue; 17312236Ssam if (wd.wd_type != WHODTYPE_STATUS) 17412236Ssam continue; 1758486Ssam if (!verify(wd.wd_hostname)) { 17616673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1778486Ssam from.sin_addr); 1788486Ssam continue; 1798486Ssam } 1809914Ssam (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname); 1816460Swnj whod = creat(path, 0666); 1826460Swnj if (whod < 0) { 18316673Sralph syslog(LOG_WARNING, "%s: %m", path); 1846460Swnj continue; 1856460Swnj } 18611834Ssam #if vax || pdp11 18711834Ssam { 18813187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 18911834Ssam struct whoent *we; 19011834Ssam 19111834Ssam /* undo header byte swapping before writing to file */ 19211834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 19311834Ssam for (i = 0; i < 3; i++) 19411834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 19511834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 19611834Ssam we = wd.wd_we; 19711834Ssam for (i = 0; i < n; i++) { 19811834Ssam we->we_idle = ntohl(we->we_idle); 19912872Ssam we->we_utmp.out_time = 20012872Ssam ntohl(we->we_utmp.out_time); 20111834Ssam we++; 20211834Ssam } 20311834Ssam } 20411834Ssam #endif 2056460Swnj (void) time(&wd.wd_recvtime); 2066460Swnj (void) write(whod, (char *)&wd, cc); 2076460Swnj (void) close(whod); 2086460Swnj } 2096460Swnj } 2106460Swnj 2118486Ssam /* 2128486Ssam * Check out host name for unprintables 2138486Ssam * and other funnies before allowing a file 2148486Ssam * to be created. Sorry, but blanks aren't allowed. 2158486Ssam */ 2168486Ssam verify(name) 2178486Ssam register char *name; 2188486Ssam { 2198486Ssam register int size = 0; 2208486Ssam 2218486Ssam while (*name) { 22215214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2238486Ssam return (0); 2248486Ssam name++, size++; 2258486Ssam } 2268486Ssam return (size > 0); 2278486Ssam } 2288486Ssam 2296460Swnj int utmptime; 2306460Swnj int utmpent; 2316577Ssam struct utmp utmp[100]; 2326460Swnj int alarmcount; 2336460Swnj 2346460Swnj onalrm() 2356460Swnj { 2366460Swnj register int i; 2376460Swnj struct stat stb; 2386577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2396460Swnj int cc; 2406460Swnj double avenrun[3]; 2416460Swnj time_t now = time(0); 24212236Ssam register struct neighbor *np; 2436460Swnj 2446460Swnj if (alarmcount % 10 == 0) 2456460Swnj getkmem(); 2466460Swnj alarmcount++; 2476460Swnj (void) fstat(utmpf, &stb); 2486460Swnj if (stb.st_mtime != utmptime) { 249*17303Stef utmptime = stb.st_mtime; 25013187Ssam (void) lseek(utmpf, (long)0, L_SET); 2516460Swnj cc = read(utmpf, (char *)utmp, sizeof (utmp)); 2526460Swnj if (cc < 0) { 2536460Swnj perror("/etc/utmp"); 25413469Ssam goto done; 2556460Swnj } 25612236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2576460Swnj utmpent = cc / sizeof (struct utmp); 2586460Swnj for (i = 0; i < utmpent; i++) 2596460Swnj if (utmp[i].ut_name[0]) { 26012807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 26112807Ssam sizeof (utmp[i].ut_line)); 26212807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 26312807Ssam sizeof (utmp[i].ut_name)); 26412807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2656577Ssam if (we >= wlast) 2666577Ssam break; 2676460Swnj we++; 2686460Swnj } 2696460Swnj utmpent = we - mywd.wd_we; 2706460Swnj } 2716460Swnj we = mywd.wd_we; 2726460Swnj for (i = 0; i < utmpent; i++) { 27312807Ssam if (stat(we->we_utmp.out_line, &stb) >= 0) 27412807Ssam we->we_idle = htonl(now - stb.st_atime); 2756460Swnj we++; 2766460Swnj } 27713187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 2786460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 2796460Swnj for (i = 0; i < 3; i++) 28012873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 2816460Swnj cc = (char *)we - (char *)&mywd; 28212807Ssam mywd.wd_sendtime = htonl(time(0)); 28312236Ssam mywd.wd_vers = WHODVERSION; 28412236Ssam mywd.wd_type = WHODTYPE_STATUS; 28512236Ssam for (np = neighbors; np != NULL; np = np->n_next) 28612236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 28712236Ssam np->n_addr, np->n_addrlen); 28813469Ssam done: 28915541Sralph (void) alarm(AL_INTERVAL); 2906460Swnj } 2916460Swnj 2926460Swnj getkmem() 2936460Swnj { 2946460Swnj struct nlist *nlp; 29516663Ssam static ino_t vmunixino; 29616663Ssam static time_t vmunixctime; 29716663Ssam struct stat sb; 2986460Swnj 29916663Ssam if (stat("/vmunix", &sb) < 0) { 30016663Ssam if (vmunixctime) 30116663Ssam return; 30216663Ssam } else { 30316663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 30416663Ssam return; 30516663Ssam vmunixctime = sb.st_ctime; 30616663Ssam vmunixino= sb.st_ino; 30716663Ssam } 3086460Swnj if (kmemf >= 0) 3096460Swnj (void) close(kmemf); 3106460Swnj loop: 31116673Sralph if (nlist("/vmunix", nl)) { 31216673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3136460Swnj sleep(300); 3146460Swnj goto loop; 3156460Swnj } 31613187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3176460Swnj if (kmemf < 0) { 31816673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 31916673Sralph exit(1); 3206460Swnj } 32113187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 32213187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 32313187Ssam sizeof (mywd.wd_boottime)); 32412807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3256460Swnj } 32612236Ssam 32712236Ssam /* 32812236Ssam * Figure out device configuration and select 32912236Ssam * networks which deserve status information. 33012236Ssam */ 33112236Ssam configure(s) 33212236Ssam int s; 33312236Ssam { 33412236Ssam char buf[BUFSIZ]; 33512236Ssam struct ifconf ifc; 33612236Ssam struct ifreq ifreq, *ifr; 33712236Ssam struct sockaddr_in *sin; 33812236Ssam register struct neighbor *np; 33913187Ssam int n; 34012236Ssam 34112236Ssam ifc.ifc_len = sizeof (buf); 34212236Ssam ifc.ifc_buf = buf; 34312236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 34416673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 34512236Ssam return (0); 34612236Ssam } 34712236Ssam ifr = ifc.ifc_req; 34812236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 34912236Ssam for (np = neighbors; np != NULL; np = np->n_next) 35012236Ssam if (np->n_name && 35112236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 35212236Ssam break; 35312236Ssam if (np != NULL) 35412236Ssam continue; 35512236Ssam ifreq = *ifr; 35612236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 35712236Ssam if (np == NULL) 35812236Ssam continue; 35912236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 36012236Ssam if (np->n_name == NULL) { 36112236Ssam free((char *)np); 36212236Ssam continue; 36312236Ssam } 36412236Ssam strcpy(np->n_name, ifr->ifr_name); 36512236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 36612236Ssam np->n_addr = malloc(np->n_addrlen); 36712236Ssam if (np->n_addr == NULL) { 36812236Ssam free(np->n_name); 36912236Ssam free((char *)np); 37012236Ssam continue; 37112236Ssam } 37212236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 37312236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 37416673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 37512236Ssam free((char *)np); 37612236Ssam continue; 37712236Ssam } 37812236Ssam if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 37912236Ssam free((char *)np); 38012236Ssam continue; 38112236Ssam } 38212236Ssam np->n_flags = ifreq.ifr_flags; 38312236Ssam if (np->n_flags & IFF_POINTOPOINT) { 38412236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 38516673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 38612236Ssam free((char *)np); 38712236Ssam continue; 38812236Ssam } 38912236Ssam /* we assume addresses are all the same size */ 39012236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 39112236Ssam np->n_addr, np->n_addrlen); 39212236Ssam } 39312236Ssam if (np->n_flags & IFF_BROADCAST) { 39412236Ssam /* we assume addresses are all the same size */ 39512236Ssam sin = (struct sockaddr_in *)np->n_addr; 39616468Skarels sin->sin_addr = inet_makeaddr((np->n_flags & IFF_LOCAL)? 39716468Skarels inet_subnetof(sin->sin_addr) : 39816468Skarels inet_netof(sin->sin_addr), 39916468Skarels INADDR_ANY); 40012236Ssam } 40112236Ssam /* gag, wish we could get rid of Internet dependencies */ 40212236Ssam sin = (struct sockaddr_in *)np->n_addr; 40312236Ssam sin->sin_port = sp->s_port; 40412236Ssam np->n_next = neighbors; 40512236Ssam neighbors = np; 40612236Ssam } 40712236Ssam return (1); 40812236Ssam } 40912807Ssam 41016468Skarels /* 41116468Skarels * Return the possible subnetwork number from an internet address. 41216468Skarels * If the address is of the form of a subnet address (most significant 41316468Skarels * bit of the host part is set), believe the subnet exists. 41416468Skarels * Otherwise, return the network number. Any subnet number is only valid 41516468Skarels * if this is a ``local'' net. 41616468Skarels */ 41716468Skarels inet_subnetof(in) 41816468Skarels struct in_addr in; 41916468Skarels { 42016468Skarels register u_long i = ntohl(in.s_addr); 42116468Skarels 42216468Skarels if (IN_CLASSA(i)) { 42316468Skarels if (IN_SUBNETA(i)) 42416468Skarels return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT); 42516468Skarels else 42616468Skarels return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); 42716468Skarels } else if (IN_CLASSB(i)) { 42816468Skarels if (IN_SUBNETB(i)) 42916468Skarels return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT); 43016468Skarels else 43116468Skarels return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); 43216468Skarels } else 43316468Skarels return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); 43416468Skarels } 43516468Skarels 43612807Ssam #ifdef DEBUG 43712807Ssam sendto(s, buf, cc, flags, to, tolen) 43812807Ssam int s; 43912807Ssam char *buf; 44012807Ssam int cc, flags; 44112807Ssam char *to; 44212807Ssam int tolen; 44312807Ssam { 44412807Ssam register struct whod *w = (struct whod *)buf; 44512807Ssam register struct whoent *we; 44612807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 44712807Ssam char *interval(); 44812807Ssam 44912807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 45012807Ssam printf("hostname %s %s\n", w->wd_hostname, 45113187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 45212807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 45313187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 45413187Ssam ntohl(w->wd_loadav[2]) / 100.0); 45512807Ssam cc -= WHDRSIZE; 45612807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 45713187Ssam time_t t = ntohl(we->we_utmp.out_time); 45812807Ssam printf("%-8.8s %s:%s %.12s", 45913187Ssam we->we_utmp.out_name, 46013187Ssam w->wd_hostname, we->we_utmp.out_line, 46113187Ssam ctime(&t)+4); 46213187Ssam we->we_idle = ntohl(we->we_idle) / 60; 46312807Ssam if (we->we_idle) { 46412807Ssam if (we->we_idle >= 100*60) 46512807Ssam we->we_idle = 100*60 - 1; 46612807Ssam if (we->we_idle >= 60) 46712807Ssam printf(" %2d", we->we_idle / 60); 46812807Ssam else 46912807Ssam printf(" "); 47012807Ssam printf(":%02d", we->we_idle % 60); 47112807Ssam } 47212807Ssam printf("\n"); 47312807Ssam } 47412807Ssam } 47512807Ssam 47612807Ssam char * 47712807Ssam interval(time, updown) 47812807Ssam int time; 47912807Ssam char *updown; 48012807Ssam { 48112807Ssam static char resbuf[32]; 48212807Ssam int days, hours, minutes; 48312807Ssam 48412807Ssam if (time < 0 || time > 3*30*24*60*60) { 48512807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 48612807Ssam return (resbuf); 48712807Ssam } 48812807Ssam minutes = (time + 59) / 60; /* round to minutes */ 48912807Ssam hours = minutes / 60; minutes %= 60; 49012807Ssam days = hours / 24; hours %= 24; 49112807Ssam if (days) 49212807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 49312807Ssam updown, days, hours, minutes); 49412807Ssam else 49512807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 49612807Ssam updown, hours, minutes); 49712807Ssam return (resbuf); 49812807Ssam } 49912807Ssam #endif 500