122549Sdist /* 222549Sdist * Copyright (c) 1983 Regents of the University of California. 322549Sdist * All rights reserved. The Berkeley software License Agreement 422549Sdist * specifies the terms and conditions for redistribution. 522549Sdist */ 622549Sdist 76460Swnj #ifndef lint 822549Sdist char copyright[] = 922549Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 1022549Sdist All rights reserved.\n"; 1122549Sdist #endif not lint 126460Swnj 1322549Sdist #ifndef lint 14*26011Smckusick static char sccsid[] = "@(#)rwhod.c 5.7 (Berkeley) 01/31/86"; 1522549Sdist #endif not lint 1622549Sdist 179215Ssam #include <sys/types.h> 189215Ssam #include <sys/socket.h> 199215Ssam #include <sys/stat.h> 209215Ssam #include <sys/ioctl.h> 2113187Ssam #include <sys/file.h> 229215Ssam 2312236Ssam #include <net/if.h> 249215Ssam #include <netinet/in.h> 259215Ssam 269215Ssam #include <nlist.h> 276460Swnj #include <stdio.h> 286460Swnj #include <signal.h> 296460Swnj #include <errno.h> 306460Swnj #include <utmp.h> 318486Ssam #include <ctype.h> 328486Ssam #include <netdb.h> 3316673Sralph #include <syslog.h> 3423535Smckusick #include <protocols/rwhod.h> 356460Swnj 3615541Sralph /* 3715541Sralph * Alarm interval. Don't forget to change the down time check in ruptime 3815541Sralph * if this is changed. 3915541Sralph */ 4016673Sralph #define AL_INTERVAL (3 * 60) 4115541Sralph 428486Ssam struct sockaddr_in sin = { AF_INET }; 436460Swnj 446460Swnj extern errno; 456460Swnj 4612236Ssam char myname[32]; 476460Swnj 486460Swnj struct nlist nl[] = { 496460Swnj #define NL_AVENRUN 0 506460Swnj { "_avenrun" }, 519215Ssam #define NL_BOOTTIME 1 529215Ssam { "_boottime" }, 536460Swnj 0 546460Swnj }; 556460Swnj 5612236Ssam /* 5712236Ssam * We communicate with each neighbor in 5812236Ssam * a list constructed at the time we're 5912236Ssam * started up. Neighbors are currently 6012236Ssam * directly connected via a hardware interface. 6112236Ssam */ 6212236Ssam struct neighbor { 6312236Ssam struct neighbor *n_next; 6412236Ssam char *n_name; /* interface name */ 6512236Ssam char *n_addr; /* who to send to */ 6612236Ssam int n_addrlen; /* size of address */ 6712236Ssam int n_flags; /* should forward?, interface flags */ 6812236Ssam }; 6912236Ssam 7012236Ssam struct neighbor *neighbors; 716460Swnj struct whod mywd; 7212236Ssam struct servent *sp; 736460Swnj int s, utmpf, kmemf = -1; 746460Swnj 7512236Ssam #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we)) 7612236Ssam #define RWHODIR "/usr/spool/rwho" 7711834Ssam 786460Swnj int onalrm(); 7912236Ssam char *strcpy(), *sprintf(), *malloc(); 806460Swnj long lseek(); 816460Swnj int getkmem(); 8212236Ssam struct in_addr inet_makeaddr(); 836460Swnj 846460Swnj main() 856460Swnj { 866460Swnj struct sockaddr_in from; 876460Swnj char path[64]; 8817155Ssam int addr, on = 1; 8924745Sbloom char *cp; 9024745Sbloom extern char *index(); 916460Swnj 9216673Sralph if (getuid()) { 9316673Sralph fprintf(stderr, "rwhod: not super user\n"); 9416673Sralph exit(1); 9516673Sralph } 968486Ssam sp = getservbyname("who", "udp"); 978486Ssam if (sp == 0) { 988486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 998486Ssam exit(1); 1008486Ssam } 1016460Swnj #ifndef DEBUG 1026460Swnj if (fork()) 1036460Swnj exit(0); 1046460Swnj { int s; 1056460Swnj for (s = 0; s < 10; s++) 1066460Swnj (void) close(s); 1076460Swnj (void) open("/", 0); 1086460Swnj (void) dup2(0, 1); 1096460Swnj (void) dup2(0, 2); 1106460Swnj s = open("/dev/tty", 2); 1116460Swnj if (s >= 0) { 1126460Swnj ioctl(s, TIOCNOTTY, 0); 1136460Swnj (void) close(s); 1146460Swnj } 1156460Swnj } 1166460Swnj #endif 117*26011Smckusick if (chdir(RWHODIR) < 0) { 118*26011Smckusick perror(RWHODIR); 119*26011Smckusick exit(1); 120*26011Smckusick } 1216460Swnj (void) signal(SIGHUP, getkmem); 12224853Seric openlog("rwhod", LOG_PID, LOG_DAEMON); 12312236Ssam /* 12412236Ssam * Establish host name as returned by system. 12512236Ssam */ 12612236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 12716673Sralph syslog(LOG_ERR, "gethostname: %m"); 1286460Swnj exit(1); 1296460Swnj } 13024745Sbloom if ((cp = index(myname, '.')) != NULL) 13124745Sbloom *cp = '\0'; 13212236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 13313187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1346460Swnj if (utmpf < 0) { 1356460Swnj (void) close(creat("/etc/utmp", 0644)); 13613187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1376460Swnj } 1386460Swnj if (utmpf < 0) { 13916673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 1406460Swnj exit(1); 1416460Swnj } 1426460Swnj getkmem(); 14313187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 14416673Sralph syslog(LOG_ERR, "socket: %m"); 1459215Ssam exit(1); 1466460Swnj } 14717155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 14817055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 14917055Skarels exit(1); 15017055Skarels } 15112236Ssam sin.sin_port = sp->s_port; 15213187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 15316673Sralph syslog(LOG_ERR, "bind: %m"); 1549215Ssam exit(1); 1559215Ssam } 15612236Ssam if (!configure(s)) 15712236Ssam exit(1); 15813027Ssam signal(SIGALRM, onalrm); 1596460Swnj onalrm(); 1606460Swnj for (;;) { 1616460Swnj struct whod wd; 16212236Ssam int cc, whod, len = sizeof (from); 1636460Swnj 16412236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 16512236Ssam &from, &len); 1666460Swnj if (cc <= 0) { 1676460Swnj if (cc < 0 && errno != EINTR) 16816673Sralph syslog(LOG_WARNING, "recv: %m"); 1696460Swnj continue; 1706460Swnj } 1718486Ssam if (from.sin_port != sp->s_port) { 17216673Sralph syslog(LOG_WARNING, "%d: bad from port", 1738486Ssam ntohs(from.sin_port)); 1746460Swnj continue; 1756460Swnj } 1768486Ssam #ifdef notdef 1778486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 17816673Sralph syslog(LOG_WARNING, "%s: unknown host", 1798486Ssam wd.wd_hostname); 1806460Swnj continue; 1816460Swnj } 1828486Ssam #endif 18312347Ssam if (wd.wd_vers != WHODVERSION) 18412347Ssam continue; 18512236Ssam if (wd.wd_type != WHODTYPE_STATUS) 18612236Ssam continue; 1878486Ssam if (!verify(wd.wd_hostname)) { 18816673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1898486Ssam from.sin_addr); 1908486Ssam continue; 1918486Ssam } 192*26011Smckusick (void) sprintf(path, "whod.%s", wd.wd_hostname); 193*26011Smckusick whod = open(path, O_WRONLY); /* much faster than creat() */ 194*26011Smckusick { struct stat s; 195*26011Smckusick if (whod < 0 || fstat(whod, &s) < 0 || s.st_size > cc) { 196*26011Smckusick (void) close(whod); 197*26011Smckusick whod = creat(path, 0666); 198*26011Smckusick } 199*26011Smckusick } 2006460Swnj if (whod < 0) { 20116673Sralph syslog(LOG_WARNING, "%s: %m", path); 2026460Swnj continue; 2036460Swnj } 20411834Ssam #if vax || pdp11 20511834Ssam { 20613187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 20711834Ssam struct whoent *we; 20811834Ssam 20911834Ssam /* undo header byte swapping before writing to file */ 21011834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 21111834Ssam for (i = 0; i < 3; i++) 21211834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 21311834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 21411834Ssam we = wd.wd_we; 21511834Ssam for (i = 0; i < n; i++) { 21611834Ssam we->we_idle = ntohl(we->we_idle); 21712872Ssam we->we_utmp.out_time = 21812872Ssam ntohl(we->we_utmp.out_time); 21911834Ssam we++; 22011834Ssam } 22111834Ssam } 22211834Ssam #endif 2236460Swnj (void) time(&wd.wd_recvtime); 2246460Swnj (void) write(whod, (char *)&wd, cc); 2256460Swnj (void) close(whod); 2266460Swnj } 2276460Swnj } 2286460Swnj 2298486Ssam /* 2308486Ssam * Check out host name for unprintables 2318486Ssam * and other funnies before allowing a file 2328486Ssam * to be created. Sorry, but blanks aren't allowed. 2338486Ssam */ 2348486Ssam verify(name) 2358486Ssam register char *name; 2368486Ssam { 2378486Ssam register int size = 0; 2388486Ssam 2398486Ssam while (*name) { 24015214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2418486Ssam return (0); 2428486Ssam name++, size++; 2438486Ssam } 2448486Ssam return (size > 0); 2458486Ssam } 2468486Ssam 2476460Swnj int utmptime; 2486460Swnj int utmpent; 24922546Sbloom int utmpsize = 0; 25022546Sbloom struct utmp *utmp; 2516460Swnj int alarmcount; 2526460Swnj 2536460Swnj onalrm() 2546460Swnj { 2556460Swnj register int i; 2566460Swnj struct stat stb; 2576577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2586460Swnj int cc; 2596460Swnj double avenrun[3]; 2606460Swnj time_t now = time(0); 26112236Ssam register struct neighbor *np; 2626460Swnj 2636460Swnj if (alarmcount % 10 == 0) 2646460Swnj getkmem(); 2656460Swnj alarmcount++; 2666460Swnj (void) fstat(utmpf, &stb); 26722546Sbloom if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 26817303Stef utmptime = stb.st_mtime; 26922546Sbloom if (stb.st_size > utmpsize) { 27022546Sbloom utmpsize = stb.st_size + 10 * sizeof(struct utmp); 27122546Sbloom if (utmp) 27222546Sbloom utmp = (struct utmp *)realloc(utmp, utmpsize); 27322546Sbloom else 27422546Sbloom utmp = (struct utmp *)malloc(utmpsize); 27522546Sbloom if (! utmp) { 27622546Sbloom fprintf(stderr, "rwhod: malloc failed\n"); 27722546Sbloom utmpsize = 0; 27822546Sbloom goto done; 27922546Sbloom } 28022546Sbloom } 28113187Ssam (void) lseek(utmpf, (long)0, L_SET); 28222546Sbloom cc = read(utmpf, (char *)utmp, stb.st_size); 2836460Swnj if (cc < 0) { 2846460Swnj perror("/etc/utmp"); 28513469Ssam goto done; 2866460Swnj } 28712236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2886460Swnj utmpent = cc / sizeof (struct utmp); 2896460Swnj for (i = 0; i < utmpent; i++) 2906460Swnj if (utmp[i].ut_name[0]) { 29112807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 29212807Ssam sizeof (utmp[i].ut_line)); 29312807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 29412807Ssam sizeof (utmp[i].ut_name)); 29512807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2966577Ssam if (we >= wlast) 2976577Ssam break; 2986460Swnj we++; 2996460Swnj } 3006460Swnj utmpent = we - mywd.wd_we; 3016460Swnj } 3026460Swnj we = mywd.wd_we; 3036460Swnj for (i = 0; i < utmpent; i++) { 304*26011Smckusick char path[64]; 305*26011Smckusick (void) sprintf(path, "/dev/%s", we->we_utmp.out_line); 306*26011Smckusick if (stat(path, &stb) >= 0) 30712807Ssam we->we_idle = htonl(now - stb.st_atime); 3086460Swnj we++; 3096460Swnj } 31013187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 3116460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 3126460Swnj for (i = 0; i < 3; i++) 31312873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 3146460Swnj cc = (char *)we - (char *)&mywd; 31512807Ssam mywd.wd_sendtime = htonl(time(0)); 31612236Ssam mywd.wd_vers = WHODVERSION; 31712236Ssam mywd.wd_type = WHODTYPE_STATUS; 31812236Ssam for (np = neighbors; np != NULL; np = np->n_next) 31912236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 32012236Ssam np->n_addr, np->n_addrlen); 32113469Ssam done: 32215541Sralph (void) alarm(AL_INTERVAL); 3236460Swnj } 3246460Swnj 3256460Swnj getkmem() 3266460Swnj { 3276460Swnj struct nlist *nlp; 32816663Ssam static ino_t vmunixino; 32916663Ssam static time_t vmunixctime; 33016663Ssam struct stat sb; 3316460Swnj 33216663Ssam if (stat("/vmunix", &sb) < 0) { 33316663Ssam if (vmunixctime) 33416663Ssam return; 33516663Ssam } else { 33616663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 33716663Ssam return; 33816663Ssam vmunixctime = sb.st_ctime; 33916663Ssam vmunixino= sb.st_ino; 34016663Ssam } 3416460Swnj if (kmemf >= 0) 3426460Swnj (void) close(kmemf); 3436460Swnj loop: 34416673Sralph if (nlist("/vmunix", nl)) { 34516673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3466460Swnj sleep(300); 3476460Swnj goto loop; 3486460Swnj } 34913187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3506460Swnj if (kmemf < 0) { 35116673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 35216673Sralph exit(1); 3536460Swnj } 35413187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 35513187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 35613187Ssam sizeof (mywd.wd_boottime)); 35712807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3586460Swnj } 35912236Ssam 36012236Ssam /* 36112236Ssam * Figure out device configuration and select 36212236Ssam * networks which deserve status information. 36312236Ssam */ 36412236Ssam configure(s) 36512236Ssam int s; 36612236Ssam { 36712236Ssam char buf[BUFSIZ]; 36812236Ssam struct ifconf ifc; 36912236Ssam struct ifreq ifreq, *ifr; 37012236Ssam struct sockaddr_in *sin; 37112236Ssam register struct neighbor *np; 37213187Ssam int n; 37312236Ssam 37412236Ssam ifc.ifc_len = sizeof (buf); 37512236Ssam ifc.ifc_buf = buf; 37612236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 37716673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 37812236Ssam return (0); 37912236Ssam } 38012236Ssam ifr = ifc.ifc_req; 38112236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 38212236Ssam for (np = neighbors; np != NULL; np = np->n_next) 38312236Ssam if (np->n_name && 38412236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 38512236Ssam break; 38612236Ssam if (np != NULL) 38712236Ssam continue; 38812236Ssam ifreq = *ifr; 38912236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 39012236Ssam if (np == NULL) 39112236Ssam continue; 39212236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 39312236Ssam if (np->n_name == NULL) { 39412236Ssam free((char *)np); 39512236Ssam continue; 39612236Ssam } 39712236Ssam strcpy(np->n_name, ifr->ifr_name); 39812236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 39912236Ssam np->n_addr = malloc(np->n_addrlen); 40012236Ssam if (np->n_addr == NULL) { 40112236Ssam free(np->n_name); 40212236Ssam free((char *)np); 40312236Ssam continue; 40412236Ssam } 40512236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 40612236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 40716673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 40812236Ssam free((char *)np); 40912236Ssam continue; 41012236Ssam } 41121852Skarels if ((ifreq.ifr_flags & IFF_UP) == 0 || 41221852Skarels (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 41312236Ssam free((char *)np); 41412236Ssam continue; 41512236Ssam } 41612236Ssam np->n_flags = ifreq.ifr_flags; 41712236Ssam if (np->n_flags & IFF_POINTOPOINT) { 41812236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 41916673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 42012236Ssam free((char *)np); 42112236Ssam continue; 42212236Ssam } 42312236Ssam /* we assume addresses are all the same size */ 42412236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 42512236Ssam np->n_addr, np->n_addrlen); 42612236Ssam } 42712236Ssam if (np->n_flags & IFF_BROADCAST) { 42821852Skarels if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 42921852Skarels syslog(LOG_ERR, "ioctl (get broadaddr)"); 43021852Skarels free((char *)np); 43121852Skarels continue; 43221852Skarels } 43312236Ssam /* we assume addresses are all the same size */ 43421852Skarels bcopy((char *)&ifreq.ifr_broadaddr, 43521852Skarels np->n_addr, np->n_addrlen); 43612236Ssam } 43712236Ssam /* gag, wish we could get rid of Internet dependencies */ 43812236Ssam sin = (struct sockaddr_in *)np->n_addr; 43912236Ssam sin->sin_port = sp->s_port; 44012236Ssam np->n_next = neighbors; 44112236Ssam neighbors = np; 44212236Ssam } 44312236Ssam return (1); 44412236Ssam } 44512807Ssam 44612807Ssam #ifdef DEBUG 44712807Ssam sendto(s, buf, cc, flags, to, tolen) 44812807Ssam int s; 44912807Ssam char *buf; 45012807Ssam int cc, flags; 45112807Ssam char *to; 45212807Ssam int tolen; 45312807Ssam { 45412807Ssam register struct whod *w = (struct whod *)buf; 45512807Ssam register struct whoent *we; 45612807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 45712807Ssam char *interval(); 45812807Ssam 45912807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 46012807Ssam printf("hostname %s %s\n", w->wd_hostname, 46113187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 46212807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 46313187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 46413187Ssam ntohl(w->wd_loadav[2]) / 100.0); 46512807Ssam cc -= WHDRSIZE; 46612807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 46713187Ssam time_t t = ntohl(we->we_utmp.out_time); 46812807Ssam printf("%-8.8s %s:%s %.12s", 46913187Ssam we->we_utmp.out_name, 47013187Ssam w->wd_hostname, we->we_utmp.out_line, 47113187Ssam ctime(&t)+4); 47213187Ssam we->we_idle = ntohl(we->we_idle) / 60; 47312807Ssam if (we->we_idle) { 47412807Ssam if (we->we_idle >= 100*60) 47512807Ssam we->we_idle = 100*60 - 1; 47612807Ssam if (we->we_idle >= 60) 47712807Ssam printf(" %2d", we->we_idle / 60); 47812807Ssam else 47912807Ssam printf(" "); 48012807Ssam printf(":%02d", we->we_idle % 60); 48112807Ssam } 48212807Ssam printf("\n"); 48312807Ssam } 48412807Ssam } 48512807Ssam 48612807Ssam char * 48712807Ssam interval(time, updown) 48812807Ssam int time; 48912807Ssam char *updown; 49012807Ssam { 49112807Ssam static char resbuf[32]; 49212807Ssam int days, hours, minutes; 49312807Ssam 49412807Ssam if (time < 0 || time > 3*30*24*60*60) { 49512807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 49612807Ssam return (resbuf); 49712807Ssam } 49812807Ssam minutes = (time + 59) / 60; /* round to minutes */ 49912807Ssam hours = minutes / 60; minutes %= 60; 50012807Ssam days = hours / 24; hours %= 24; 50112807Ssam if (days) 50212807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 50312807Ssam updown, days, hours, minutes); 50412807Ssam else 50512807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 50612807Ssam updown, hours, minutes); 50712807Ssam return (resbuf); 50812807Ssam } 50912807Ssam #endif 510