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*26484Smckusick static char sccsid[] = "@(#)rwhod.c 5.9 (Berkeley) 03/05/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; 8726016Skarels struct stat st; 886460Swnj char path[64]; 89*26484Smckusick int on = 1; 9024745Sbloom char *cp; 9124745Sbloom 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 11826011Smckusick if (chdir(RWHODIR) < 0) { 11926011Smckusick perror(RWHODIR); 12026011Smckusick exit(1); 12126011Smckusick } 1226460Swnj (void) signal(SIGHUP, getkmem); 12324853Seric openlog("rwhod", LOG_PID, LOG_DAEMON); 12412236Ssam /* 12512236Ssam * Establish host name as returned by system. 12612236Ssam */ 12712236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 12816673Sralph syslog(LOG_ERR, "gethostname: %m"); 1296460Swnj exit(1); 1306460Swnj } 13124745Sbloom if ((cp = index(myname, '.')) != NULL) 13224745Sbloom *cp = '\0'; 13312236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 13413187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1356460Swnj if (utmpf < 0) { 1366460Swnj (void) close(creat("/etc/utmp", 0644)); 13713187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1386460Swnj } 1396460Swnj if (utmpf < 0) { 14016673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 1416460Swnj exit(1); 1426460Swnj } 1436460Swnj getkmem(); 14413187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 14516673Sralph syslog(LOG_ERR, "socket: %m"); 1469215Ssam exit(1); 1476460Swnj } 14817155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 14917055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 15017055Skarels exit(1); 15117055Skarels } 15212236Ssam sin.sin_port = sp->s_port; 15313187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 15416673Sralph syslog(LOG_ERR, "bind: %m"); 1559215Ssam exit(1); 1569215Ssam } 15712236Ssam if (!configure(s)) 15812236Ssam exit(1); 15913027Ssam signal(SIGALRM, onalrm); 1606460Swnj onalrm(); 1616460Swnj for (;;) { 1626460Swnj struct whod wd; 16312236Ssam int cc, whod, len = sizeof (from); 1646460Swnj 16512236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 16612236Ssam &from, &len); 1676460Swnj if (cc <= 0) { 1686460Swnj if (cc < 0 && errno != EINTR) 16916673Sralph syslog(LOG_WARNING, "recv: %m"); 1706460Swnj continue; 1716460Swnj } 1728486Ssam if (from.sin_port != sp->s_port) { 17316673Sralph syslog(LOG_WARNING, "%d: bad from port", 1748486Ssam ntohs(from.sin_port)); 1756460Swnj continue; 1766460Swnj } 1778486Ssam #ifdef notdef 1788486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 17916673Sralph syslog(LOG_WARNING, "%s: unknown host", 1808486Ssam wd.wd_hostname); 1816460Swnj continue; 1826460Swnj } 1838486Ssam #endif 18412347Ssam if (wd.wd_vers != WHODVERSION) 18512347Ssam continue; 18612236Ssam if (wd.wd_type != WHODTYPE_STATUS) 18712236Ssam continue; 1888486Ssam if (!verify(wd.wd_hostname)) { 18916673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1908486Ssam from.sin_addr); 1918486Ssam continue; 1928486Ssam } 19326011Smckusick (void) sprintf(path, "whod.%s", wd.wd_hostname); 19426016Skarels /* 19526016Skarels * Rather than truncating and growing the file each time, 19626016Skarels * use ftruncate if size is less than previous size. 19726016Skarels */ 19826016Skarels whod = open(path, O_WRONLY | O_CREAT, 0644); 1996460Swnj if (whod < 0) { 20016673Sralph syslog(LOG_WARNING, "%s: %m", path); 2016460Swnj continue; 2026460Swnj } 20311834Ssam #if vax || pdp11 20411834Ssam { 20513187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 20611834Ssam struct whoent *we; 20711834Ssam 20811834Ssam /* undo header byte swapping before writing to file */ 20911834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 21011834Ssam for (i = 0; i < 3; i++) 21111834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 21211834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 21311834Ssam we = wd.wd_we; 21411834Ssam for (i = 0; i < n; i++) { 21511834Ssam we->we_idle = ntohl(we->we_idle); 21612872Ssam we->we_utmp.out_time = 21712872Ssam ntohl(we->we_utmp.out_time); 21811834Ssam we++; 21911834Ssam } 22011834Ssam } 22111834Ssam #endif 2226460Swnj (void) time(&wd.wd_recvtime); 2236460Swnj (void) write(whod, (char *)&wd, cc); 22426016Skarels if (fstat(whod, &st) < 0 || st.st_size > cc) 22526016Skarels ftruncate(whod, cc); 2266460Swnj (void) close(whod); 2276460Swnj } 2286460Swnj } 2296460Swnj 2308486Ssam /* 2318486Ssam * Check out host name for unprintables 2328486Ssam * and other funnies before allowing a file 2338486Ssam * to be created. Sorry, but blanks aren't allowed. 2348486Ssam */ 2358486Ssam verify(name) 2368486Ssam register char *name; 2378486Ssam { 2388486Ssam register int size = 0; 2398486Ssam 2408486Ssam while (*name) { 24115214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2428486Ssam return (0); 2438486Ssam name++, size++; 2448486Ssam } 2458486Ssam return (size > 0); 2468486Ssam } 2478486Ssam 2486460Swnj int utmptime; 2496460Swnj int utmpent; 25022546Sbloom int utmpsize = 0; 25122546Sbloom struct utmp *utmp; 2526460Swnj int alarmcount; 2536460Swnj 2546460Swnj onalrm() 2556460Swnj { 2566460Swnj register int i; 2576460Swnj struct stat stb; 2586577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2596460Swnj int cc; 2606460Swnj double avenrun[3]; 2616460Swnj time_t now = time(0); 26212236Ssam register struct neighbor *np; 2636460Swnj 2646460Swnj if (alarmcount % 10 == 0) 2656460Swnj getkmem(); 2666460Swnj alarmcount++; 2676460Swnj (void) fstat(utmpf, &stb); 26822546Sbloom if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 26917303Stef utmptime = stb.st_mtime; 27022546Sbloom if (stb.st_size > utmpsize) { 27122546Sbloom utmpsize = stb.st_size + 10 * sizeof(struct utmp); 27222546Sbloom if (utmp) 27322546Sbloom utmp = (struct utmp *)realloc(utmp, utmpsize); 27422546Sbloom else 27522546Sbloom utmp = (struct utmp *)malloc(utmpsize); 27622546Sbloom if (! utmp) { 27722546Sbloom fprintf(stderr, "rwhod: malloc failed\n"); 27822546Sbloom utmpsize = 0; 27922546Sbloom goto done; 28022546Sbloom } 28122546Sbloom } 28213187Ssam (void) lseek(utmpf, (long)0, L_SET); 28322546Sbloom cc = read(utmpf, (char *)utmp, stb.st_size); 2846460Swnj if (cc < 0) { 2856460Swnj perror("/etc/utmp"); 28613469Ssam goto done; 2876460Swnj } 28812236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2896460Swnj utmpent = cc / sizeof (struct utmp); 2906460Swnj for (i = 0; i < utmpent; i++) 2916460Swnj if (utmp[i].ut_name[0]) { 29212807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 29312807Ssam sizeof (utmp[i].ut_line)); 29412807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 29512807Ssam sizeof (utmp[i].ut_name)); 29612807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2976577Ssam if (we >= wlast) 2986577Ssam break; 2996460Swnj we++; 3006460Swnj } 3016460Swnj utmpent = we - mywd.wd_we; 3026460Swnj } 303*26484Smckusick 304*26484Smckusick /* 305*26484Smckusick * The test on utmpent looks silly---after all, if no one is 306*26484Smckusick * logged on, why worry about efficiency?---but is useful on 307*26484Smckusick * (e.g.) compute servers. 308*26484Smckusick */ 309*26484Smckusick if (utmpent && chdir("/dev")) { 310*26484Smckusick syslog(LOG_ERR, "chdir(/dev): %m"); 311*26484Smckusick exit(1); 312*26484Smckusick } 3136460Swnj we = mywd.wd_we; 3146460Swnj for (i = 0; i < utmpent; i++) { 315*26484Smckusick if (stat(we->we_utmp.out_line, &stb) >= 0) 31612807Ssam we->we_idle = htonl(now - stb.st_atime); 3176460Swnj we++; 3186460Swnj } 31913187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 3206460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 3216460Swnj for (i = 0; i < 3; i++) 32212873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 3236460Swnj cc = (char *)we - (char *)&mywd; 32412807Ssam mywd.wd_sendtime = htonl(time(0)); 32512236Ssam mywd.wd_vers = WHODVERSION; 32612236Ssam mywd.wd_type = WHODTYPE_STATUS; 32712236Ssam for (np = neighbors; np != NULL; np = np->n_next) 32812236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 32912236Ssam np->n_addr, np->n_addrlen); 330*26484Smckusick if (utmpent && chdir(RWHODIR)) { 331*26484Smckusick syslog(LOG_ERR, "chdir(%s): %m", RWHODIR); 332*26484Smckusick exit(1); 333*26484Smckusick } 33413469Ssam done: 33515541Sralph (void) alarm(AL_INTERVAL); 3366460Swnj } 3376460Swnj 3386460Swnj getkmem() 3396460Swnj { 34016663Ssam static ino_t vmunixino; 34116663Ssam static time_t vmunixctime; 34216663Ssam struct stat sb; 3436460Swnj 34416663Ssam if (stat("/vmunix", &sb) < 0) { 34516663Ssam if (vmunixctime) 34616663Ssam return; 34716663Ssam } else { 34816663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 34916663Ssam return; 35016663Ssam vmunixctime = sb.st_ctime; 35116663Ssam vmunixino= sb.st_ino; 35216663Ssam } 3536460Swnj if (kmemf >= 0) 3546460Swnj (void) close(kmemf); 3556460Swnj loop: 35616673Sralph if (nlist("/vmunix", nl)) { 35716673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3586460Swnj sleep(300); 3596460Swnj goto loop; 3606460Swnj } 36113187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3626460Swnj if (kmemf < 0) { 36316673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 36416673Sralph exit(1); 3656460Swnj } 36613187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 36713187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 36813187Ssam sizeof (mywd.wd_boottime)); 36912807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3706460Swnj } 37112236Ssam 37212236Ssam /* 37312236Ssam * Figure out device configuration and select 37412236Ssam * networks which deserve status information. 37512236Ssam */ 37612236Ssam configure(s) 37712236Ssam int s; 37812236Ssam { 37912236Ssam char buf[BUFSIZ]; 38012236Ssam struct ifconf ifc; 38112236Ssam struct ifreq ifreq, *ifr; 38212236Ssam struct sockaddr_in *sin; 38312236Ssam register struct neighbor *np; 38413187Ssam int n; 38512236Ssam 38612236Ssam ifc.ifc_len = sizeof (buf); 38712236Ssam ifc.ifc_buf = buf; 38812236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 38916673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 39012236Ssam return (0); 39112236Ssam } 39212236Ssam ifr = ifc.ifc_req; 39312236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 39412236Ssam for (np = neighbors; np != NULL; np = np->n_next) 39512236Ssam if (np->n_name && 39612236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 39712236Ssam break; 39812236Ssam if (np != NULL) 39912236Ssam continue; 40012236Ssam ifreq = *ifr; 40112236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 40212236Ssam if (np == NULL) 40312236Ssam continue; 40412236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 40512236Ssam if (np->n_name == NULL) { 40612236Ssam free((char *)np); 40712236Ssam continue; 40812236Ssam } 40912236Ssam strcpy(np->n_name, ifr->ifr_name); 41012236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 41112236Ssam np->n_addr = malloc(np->n_addrlen); 41212236Ssam if (np->n_addr == NULL) { 41312236Ssam free(np->n_name); 41412236Ssam free((char *)np); 41512236Ssam continue; 41612236Ssam } 41712236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 41812236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 41916673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 42012236Ssam free((char *)np); 42112236Ssam continue; 42212236Ssam } 42321852Skarels if ((ifreq.ifr_flags & IFF_UP) == 0 || 42421852Skarels (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 42512236Ssam free((char *)np); 42612236Ssam continue; 42712236Ssam } 42812236Ssam np->n_flags = ifreq.ifr_flags; 42912236Ssam if (np->n_flags & IFF_POINTOPOINT) { 43012236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 43116673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 43212236Ssam free((char *)np); 43312236Ssam continue; 43412236Ssam } 43512236Ssam /* we assume addresses are all the same size */ 43612236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 43712236Ssam np->n_addr, np->n_addrlen); 43812236Ssam } 43912236Ssam if (np->n_flags & IFF_BROADCAST) { 44021852Skarels if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 44121852Skarels syslog(LOG_ERR, "ioctl (get broadaddr)"); 44221852Skarels free((char *)np); 44321852Skarels continue; 44421852Skarels } 44512236Ssam /* we assume addresses are all the same size */ 44621852Skarels bcopy((char *)&ifreq.ifr_broadaddr, 44721852Skarels np->n_addr, np->n_addrlen); 44812236Ssam } 44912236Ssam /* gag, wish we could get rid of Internet dependencies */ 45012236Ssam sin = (struct sockaddr_in *)np->n_addr; 45112236Ssam sin->sin_port = sp->s_port; 45212236Ssam np->n_next = neighbors; 45312236Ssam neighbors = np; 45412236Ssam } 45512236Ssam return (1); 45612236Ssam } 45712807Ssam 45812807Ssam #ifdef DEBUG 45912807Ssam sendto(s, buf, cc, flags, to, tolen) 46012807Ssam int s; 46112807Ssam char *buf; 46212807Ssam int cc, flags; 46312807Ssam char *to; 46412807Ssam int tolen; 46512807Ssam { 46612807Ssam register struct whod *w = (struct whod *)buf; 46712807Ssam register struct whoent *we; 46812807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 46912807Ssam char *interval(); 47012807Ssam 47112807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 47212807Ssam printf("hostname %s %s\n", w->wd_hostname, 47313187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 47412807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 47513187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 47613187Ssam ntohl(w->wd_loadav[2]) / 100.0); 47712807Ssam cc -= WHDRSIZE; 47812807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 47913187Ssam time_t t = ntohl(we->we_utmp.out_time); 48012807Ssam printf("%-8.8s %s:%s %.12s", 48113187Ssam we->we_utmp.out_name, 48213187Ssam w->wd_hostname, we->we_utmp.out_line, 48313187Ssam ctime(&t)+4); 48413187Ssam we->we_idle = ntohl(we->we_idle) / 60; 48512807Ssam if (we->we_idle) { 48612807Ssam if (we->we_idle >= 100*60) 48712807Ssam we->we_idle = 100*60 - 1; 48812807Ssam if (we->we_idle >= 60) 48912807Ssam printf(" %2d", we->we_idle / 60); 49012807Ssam else 49112807Ssam printf(" "); 49212807Ssam printf(":%02d", we->we_idle % 60); 49312807Ssam } 49412807Ssam printf("\n"); 49512807Ssam } 49612807Ssam } 49712807Ssam 49812807Ssam char * 49912807Ssam interval(time, updown) 50012807Ssam int time; 50112807Ssam char *updown; 50212807Ssam { 50312807Ssam static char resbuf[32]; 50412807Ssam int days, hours, minutes; 50512807Ssam 50612807Ssam if (time < 0 || time > 3*30*24*60*60) { 50712807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 50812807Ssam return (resbuf); 50912807Ssam } 51012807Ssam minutes = (time + 59) / 60; /* round to minutes */ 51112807Ssam hours = minutes / 60; minutes %= 60; 51212807Ssam days = hours / 24; hours %= 24; 51312807Ssam if (days) 51412807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 51512807Ssam updown, days, hours, minutes); 51612807Ssam else 51712807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 51812807Ssam updown, hours, minutes); 51912807Ssam return (resbuf); 52012807Ssam } 52112807Ssam #endif 522