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*24745Sbloom static char sccsid[] = "@(#)rwhod.c 5.4 (Berkeley) 09/15/85"; 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; 8912236Ssam struct hostent *hp; 90*24745Sbloom char *cp; 91*24745Sbloom extern char *index(); 926460Swnj 9316673Sralph if (getuid()) { 9416673Sralph fprintf(stderr, "rwhod: not super user\n"); 9516673Sralph exit(1); 9616673Sralph } 978486Ssam sp = getservbyname("who", "udp"); 988486Ssam if (sp == 0) { 998486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 1008486Ssam exit(1); 1018486Ssam } 1026460Swnj #ifndef DEBUG 1036460Swnj if (fork()) 1046460Swnj exit(0); 1056460Swnj { int s; 1066460Swnj for (s = 0; s < 10; s++) 1076460Swnj (void) close(s); 1086460Swnj (void) open("/", 0); 1096460Swnj (void) dup2(0, 1); 1106460Swnj (void) dup2(0, 2); 1116460Swnj s = open("/dev/tty", 2); 1126460Swnj if (s >= 0) { 1136460Swnj ioctl(s, TIOCNOTTY, 0); 1146460Swnj (void) close(s); 1156460Swnj } 1166460Swnj } 1176460Swnj #endif 1186460Swnj (void) chdir("/dev"); 1196460Swnj (void) signal(SIGHUP, getkmem); 12016673Sralph openlog("rwhod", LOG_PID, 0); 12112236Ssam /* 12212236Ssam * Establish host name as returned by system. 12312236Ssam */ 12412236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 12516673Sralph syslog(LOG_ERR, "gethostname: %m"); 1266460Swnj exit(1); 1276460Swnj } 128*24745Sbloom if ((cp = index(myname, '.')) != NULL) 129*24745Sbloom *cp = '\0'; 13012236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 13113187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1326460Swnj if (utmpf < 0) { 1336460Swnj (void) close(creat("/etc/utmp", 0644)); 13413187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1356460Swnj } 1366460Swnj if (utmpf < 0) { 13716673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 1386460Swnj exit(1); 1396460Swnj } 1406460Swnj getkmem(); 14113187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 14216673Sralph syslog(LOG_ERR, "socket: %m"); 1439215Ssam exit(1); 1446460Swnj } 14517155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 14617055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 14717055Skarels exit(1); 14817055Skarels } 14912236Ssam hp = gethostbyname(myname); 15012236Ssam if (hp == NULL) { 15116673Sralph syslog(LOG_ERR, "%s: don't know my own name", myname); 15212236Ssam exit(1); 15312236Ssam } 15412236Ssam sin.sin_family = hp->h_addrtype; 15512236Ssam sin.sin_port = sp->s_port; 15613187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 15716673Sralph syslog(LOG_ERR, "bind: %m"); 1589215Ssam exit(1); 1599215Ssam } 16012236Ssam if (!configure(s)) 16112236Ssam exit(1); 16213027Ssam signal(SIGALRM, onalrm); 1636460Swnj onalrm(); 1646460Swnj for (;;) { 1656460Swnj struct whod wd; 16612236Ssam int cc, whod, len = sizeof (from); 1676460Swnj 16812236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 16912236Ssam &from, &len); 1706460Swnj if (cc <= 0) { 1716460Swnj if (cc < 0 && errno != EINTR) 17216673Sralph syslog(LOG_WARNING, "recv: %m"); 1736460Swnj continue; 1746460Swnj } 1758486Ssam if (from.sin_port != sp->s_port) { 17616673Sralph syslog(LOG_WARNING, "%d: bad from port", 1778486Ssam ntohs(from.sin_port)); 1786460Swnj continue; 1796460Swnj } 1808486Ssam #ifdef notdef 1818486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 18216673Sralph syslog(LOG_WARNING, "%s: unknown host", 1838486Ssam wd.wd_hostname); 1846460Swnj continue; 1856460Swnj } 1868486Ssam #endif 18712347Ssam if (wd.wd_vers != WHODVERSION) 18812347Ssam continue; 18912236Ssam if (wd.wd_type != WHODTYPE_STATUS) 19012236Ssam continue; 1918486Ssam if (!verify(wd.wd_hostname)) { 19216673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1938486Ssam from.sin_addr); 1948486Ssam continue; 1958486Ssam } 1969914Ssam (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname); 1976460Swnj whod = creat(path, 0666); 1986460Swnj if (whod < 0) { 19916673Sralph syslog(LOG_WARNING, "%s: %m", path); 2006460Swnj continue; 2016460Swnj } 20211834Ssam #if vax || pdp11 20311834Ssam { 20413187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 20511834Ssam struct whoent *we; 20611834Ssam 20711834Ssam /* undo header byte swapping before writing to file */ 20811834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 20911834Ssam for (i = 0; i < 3; i++) 21011834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 21111834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 21211834Ssam we = wd.wd_we; 21311834Ssam for (i = 0; i < n; i++) { 21411834Ssam we->we_idle = ntohl(we->we_idle); 21512872Ssam we->we_utmp.out_time = 21612872Ssam ntohl(we->we_utmp.out_time); 21711834Ssam we++; 21811834Ssam } 21911834Ssam } 22011834Ssam #endif 2216460Swnj (void) time(&wd.wd_recvtime); 2226460Swnj (void) write(whod, (char *)&wd, cc); 2236460Swnj (void) close(whod); 2246460Swnj } 2256460Swnj } 2266460Swnj 2278486Ssam /* 2288486Ssam * Check out host name for unprintables 2298486Ssam * and other funnies before allowing a file 2308486Ssam * to be created. Sorry, but blanks aren't allowed. 2318486Ssam */ 2328486Ssam verify(name) 2338486Ssam register char *name; 2348486Ssam { 2358486Ssam register int size = 0; 2368486Ssam 2378486Ssam while (*name) { 23815214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2398486Ssam return (0); 2408486Ssam name++, size++; 2418486Ssam } 2428486Ssam return (size > 0); 2438486Ssam } 2448486Ssam 2456460Swnj int utmptime; 2466460Swnj int utmpent; 24722546Sbloom int utmpsize = 0; 24822546Sbloom struct utmp *utmp; 2496460Swnj int alarmcount; 2506460Swnj 2516460Swnj onalrm() 2526460Swnj { 2536460Swnj register int i; 2546460Swnj struct stat stb; 2556577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2566460Swnj int cc; 2576460Swnj double avenrun[3]; 2586460Swnj time_t now = time(0); 25912236Ssam register struct neighbor *np; 2606460Swnj 2616460Swnj if (alarmcount % 10 == 0) 2626460Swnj getkmem(); 2636460Swnj alarmcount++; 2646460Swnj (void) fstat(utmpf, &stb); 26522546Sbloom if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 26617303Stef utmptime = stb.st_mtime; 26722546Sbloom if (stb.st_size > utmpsize) { 26822546Sbloom utmpsize = stb.st_size + 10 * sizeof(struct utmp); 26922546Sbloom if (utmp) 27022546Sbloom utmp = (struct utmp *)realloc(utmp, utmpsize); 27122546Sbloom else 27222546Sbloom utmp = (struct utmp *)malloc(utmpsize); 27322546Sbloom if (! utmp) { 27422546Sbloom fprintf(stderr, "rwhod: malloc failed\n"); 27522546Sbloom utmpsize = 0; 27622546Sbloom goto done; 27722546Sbloom } 27822546Sbloom } 27913187Ssam (void) lseek(utmpf, (long)0, L_SET); 28022546Sbloom cc = read(utmpf, (char *)utmp, stb.st_size); 2816460Swnj if (cc < 0) { 2826460Swnj perror("/etc/utmp"); 28313469Ssam goto done; 2846460Swnj } 28512236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2866460Swnj utmpent = cc / sizeof (struct utmp); 2876460Swnj for (i = 0; i < utmpent; i++) 2886460Swnj if (utmp[i].ut_name[0]) { 28912807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 29012807Ssam sizeof (utmp[i].ut_line)); 29112807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 29212807Ssam sizeof (utmp[i].ut_name)); 29312807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2946577Ssam if (we >= wlast) 2956577Ssam break; 2966460Swnj we++; 2976460Swnj } 2986460Swnj utmpent = we - mywd.wd_we; 2996460Swnj } 3006460Swnj we = mywd.wd_we; 3016460Swnj for (i = 0; i < utmpent; i++) { 30212807Ssam if (stat(we->we_utmp.out_line, &stb) >= 0) 30312807Ssam we->we_idle = htonl(now - stb.st_atime); 3046460Swnj we++; 3056460Swnj } 30613187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 3076460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 3086460Swnj for (i = 0; i < 3; i++) 30912873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 3106460Swnj cc = (char *)we - (char *)&mywd; 31112807Ssam mywd.wd_sendtime = htonl(time(0)); 31212236Ssam mywd.wd_vers = WHODVERSION; 31312236Ssam mywd.wd_type = WHODTYPE_STATUS; 31412236Ssam for (np = neighbors; np != NULL; np = np->n_next) 31512236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 31612236Ssam np->n_addr, np->n_addrlen); 31713469Ssam done: 31815541Sralph (void) alarm(AL_INTERVAL); 3196460Swnj } 3206460Swnj 3216460Swnj getkmem() 3226460Swnj { 3236460Swnj struct nlist *nlp; 32416663Ssam static ino_t vmunixino; 32516663Ssam static time_t vmunixctime; 32616663Ssam struct stat sb; 3276460Swnj 32816663Ssam if (stat("/vmunix", &sb) < 0) { 32916663Ssam if (vmunixctime) 33016663Ssam return; 33116663Ssam } else { 33216663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 33316663Ssam return; 33416663Ssam vmunixctime = sb.st_ctime; 33516663Ssam vmunixino= sb.st_ino; 33616663Ssam } 3376460Swnj if (kmemf >= 0) 3386460Swnj (void) close(kmemf); 3396460Swnj loop: 34016673Sralph if (nlist("/vmunix", nl)) { 34116673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3426460Swnj sleep(300); 3436460Swnj goto loop; 3446460Swnj } 34513187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3466460Swnj if (kmemf < 0) { 34716673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 34816673Sralph exit(1); 3496460Swnj } 35013187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 35113187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 35213187Ssam sizeof (mywd.wd_boottime)); 35312807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3546460Swnj } 35512236Ssam 35612236Ssam /* 35712236Ssam * Figure out device configuration and select 35812236Ssam * networks which deserve status information. 35912236Ssam */ 36012236Ssam configure(s) 36112236Ssam int s; 36212236Ssam { 36312236Ssam char buf[BUFSIZ]; 36412236Ssam struct ifconf ifc; 36512236Ssam struct ifreq ifreq, *ifr; 36612236Ssam struct sockaddr_in *sin; 36712236Ssam register struct neighbor *np; 36813187Ssam int n; 36912236Ssam 37012236Ssam ifc.ifc_len = sizeof (buf); 37112236Ssam ifc.ifc_buf = buf; 37212236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 37316673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 37412236Ssam return (0); 37512236Ssam } 37612236Ssam ifr = ifc.ifc_req; 37712236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 37812236Ssam for (np = neighbors; np != NULL; np = np->n_next) 37912236Ssam if (np->n_name && 38012236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 38112236Ssam break; 38212236Ssam if (np != NULL) 38312236Ssam continue; 38412236Ssam ifreq = *ifr; 38512236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 38612236Ssam if (np == NULL) 38712236Ssam continue; 38812236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 38912236Ssam if (np->n_name == NULL) { 39012236Ssam free((char *)np); 39112236Ssam continue; 39212236Ssam } 39312236Ssam strcpy(np->n_name, ifr->ifr_name); 39412236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 39512236Ssam np->n_addr = malloc(np->n_addrlen); 39612236Ssam if (np->n_addr == NULL) { 39712236Ssam free(np->n_name); 39812236Ssam free((char *)np); 39912236Ssam continue; 40012236Ssam } 40112236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 40212236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 40316673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 40412236Ssam free((char *)np); 40512236Ssam continue; 40612236Ssam } 40721852Skarels if ((ifreq.ifr_flags & IFF_UP) == 0 || 40821852Skarels (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 40912236Ssam free((char *)np); 41012236Ssam continue; 41112236Ssam } 41212236Ssam np->n_flags = ifreq.ifr_flags; 41312236Ssam if (np->n_flags & IFF_POINTOPOINT) { 41412236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 41516673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 41612236Ssam free((char *)np); 41712236Ssam continue; 41812236Ssam } 41912236Ssam /* we assume addresses are all the same size */ 42012236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 42112236Ssam np->n_addr, np->n_addrlen); 42212236Ssam } 42312236Ssam if (np->n_flags & IFF_BROADCAST) { 42421852Skarels if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 42521852Skarels syslog(LOG_ERR, "ioctl (get broadaddr)"); 42621852Skarels free((char *)np); 42721852Skarels continue; 42821852Skarels } 42912236Ssam /* we assume addresses are all the same size */ 43021852Skarels bcopy((char *)&ifreq.ifr_broadaddr, 43121852Skarels np->n_addr, np->n_addrlen); 43212236Ssam } 43312236Ssam /* gag, wish we could get rid of Internet dependencies */ 43412236Ssam sin = (struct sockaddr_in *)np->n_addr; 43512236Ssam sin->sin_port = sp->s_port; 43612236Ssam np->n_next = neighbors; 43712236Ssam neighbors = np; 43812236Ssam } 43912236Ssam return (1); 44012236Ssam } 44112807Ssam 44212807Ssam #ifdef DEBUG 44312807Ssam sendto(s, buf, cc, flags, to, tolen) 44412807Ssam int s; 44512807Ssam char *buf; 44612807Ssam int cc, flags; 44712807Ssam char *to; 44812807Ssam int tolen; 44912807Ssam { 45012807Ssam register struct whod *w = (struct whod *)buf; 45112807Ssam register struct whoent *we; 45212807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 45312807Ssam char *interval(); 45412807Ssam 45512807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 45612807Ssam printf("hostname %s %s\n", w->wd_hostname, 45713187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 45812807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 45913187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 46013187Ssam ntohl(w->wd_loadav[2]) / 100.0); 46112807Ssam cc -= WHDRSIZE; 46212807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 46313187Ssam time_t t = ntohl(we->we_utmp.out_time); 46412807Ssam printf("%-8.8s %s:%s %.12s", 46513187Ssam we->we_utmp.out_name, 46613187Ssam w->wd_hostname, we->we_utmp.out_line, 46713187Ssam ctime(&t)+4); 46813187Ssam we->we_idle = ntohl(we->we_idle) / 60; 46912807Ssam if (we->we_idle) { 47012807Ssam if (we->we_idle >= 100*60) 47112807Ssam we->we_idle = 100*60 - 1; 47212807Ssam if (we->we_idle >= 60) 47312807Ssam printf(" %2d", we->we_idle / 60); 47412807Ssam else 47512807Ssam printf(" "); 47612807Ssam printf(":%02d", we->we_idle % 60); 47712807Ssam } 47812807Ssam printf("\n"); 47912807Ssam } 48012807Ssam } 48112807Ssam 48212807Ssam char * 48312807Ssam interval(time, updown) 48412807Ssam int time; 48512807Ssam char *updown; 48612807Ssam { 48712807Ssam static char resbuf[32]; 48812807Ssam int days, hours, minutes; 48912807Ssam 49012807Ssam if (time < 0 || time > 3*30*24*60*60) { 49112807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 49212807Ssam return (resbuf); 49312807Ssam } 49412807Ssam minutes = (time + 59) / 60; /* round to minutes */ 49512807Ssam hours = minutes / 60; minutes %= 60; 49612807Ssam days = hours / 24; hours %= 24; 49712807Ssam if (days) 49812807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 49912807Ssam updown, days, hours, minutes); 50012807Ssam else 50112807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 50212807Ssam updown, hours, minutes); 50312807Ssam return (resbuf); 50412807Ssam } 50512807Ssam #endif 506