122549Sdist /* 235392Sbostic * Copyright (c) 1983 The Regents of the University of California. 335392Sbostic * All rights reserved. 435392Sbostic * 542819Sbostic * %sccs.include.redist.c% 622549Sdist */ 722549Sdist 86460Swnj #ifndef lint 922549Sdist char copyright[] = 1035392Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 1122549Sdist All rights reserved.\n"; 1235392Sbostic #endif /* not lint */ 136460Swnj 1422549Sdist #ifndef lint 15*44707Skarels static char sccsid[] = "@(#)rwhod.c 5.19 (Berkeley) 06/29/90"; 1635392Sbostic #endif /* not lint */ 1722549Sdist 1842265Skarels #include <sys/param.h> 199215Ssam #include <sys/socket.h> 209215Ssam #include <sys/stat.h> 2137295Sbostic #include <sys/signal.h> 229215Ssam #include <sys/ioctl.h> 2313187Ssam #include <sys/file.h> 249215Ssam 2512236Ssam #include <net/if.h> 269215Ssam #include <netinet/in.h> 279215Ssam 289215Ssam #include <nlist.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> 3537295Sbostic #include <stdio.h> 3637971Sbostic #include <paths.h> 376460Swnj 3815541Sralph /* 3915541Sralph * Alarm interval. Don't forget to change the down time check in ruptime 4015541Sralph * if this is changed. 4115541Sralph */ 4216673Sralph #define AL_INTERVAL (3 * 60) 4315541Sralph 4442265Skarels struct sockaddr_in sin; 456460Swnj 4642265Skarels char myname[MAXHOSTNAMELEN]; 476460Swnj 486460Swnj struct nlist nl[] = { 4938182Smckusick #define NL_BOOTTIME 0 509215Ssam { "_boottime" }, 516460Swnj 0 526460Swnj }; 536460Swnj 5412236Ssam /* 5512236Ssam * We communicate with each neighbor in 5612236Ssam * a list constructed at the time we're 5712236Ssam * started up. Neighbors are currently 5812236Ssam * directly connected via a hardware interface. 5912236Ssam */ 6012236Ssam struct neighbor { 6112236Ssam struct neighbor *n_next; 6212236Ssam char *n_name; /* interface name */ 6312236Ssam char *n_addr; /* who to send to */ 6412236Ssam int n_addrlen; /* size of address */ 6512236Ssam int n_flags; /* should forward?, interface flags */ 6612236Ssam }; 6712236Ssam 6812236Ssam struct neighbor *neighbors; 696460Swnj struct whod mywd; 7012236Ssam struct servent *sp; 716460Swnj int s, utmpf, kmemf = -1; 726460Swnj 7312236Ssam #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we)) 7411834Ssam 7537295Sbostic extern int errno; 766460Swnj int onalrm(); 7732449Sbostic char *strcpy(), *malloc(); 786460Swnj long lseek(); 796460Swnj int getkmem(); 8012236Ssam struct in_addr inet_makeaddr(); 816460Swnj 826460Swnj main() 836460Swnj { 846460Swnj struct sockaddr_in from; 8526016Skarels struct stat st; 866460Swnj char path[64]; 8726484Smckusick int on = 1; 8837295Sbostic char *cp, *index(), *strerror(); 896460Swnj 9016673Sralph if (getuid()) { 9116673Sralph fprintf(stderr, "rwhod: not super user\n"); 9216673Sralph exit(1); 9316673Sralph } 948486Ssam sp = getservbyname("who", "udp"); 958486Ssam if (sp == 0) { 968486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 978486Ssam exit(1); 988486Ssam } 996460Swnj #ifndef DEBUG 100*44707Skarels daemon(1, 0); 1016460Swnj #endif 10237295Sbostic if (chdir(_PATH_RWHODIR) < 0) { 10337295Sbostic (void)fprintf(stderr, "rwhod: %s: %s\n", 10437295Sbostic _PATH_RWHODIR, strerror(errno)); 10526011Smckusick exit(1); 10626011Smckusick } 1076460Swnj (void) signal(SIGHUP, getkmem); 10824853Seric openlog("rwhod", LOG_PID, LOG_DAEMON); 10912236Ssam /* 11012236Ssam * Establish host name as returned by system. 11112236Ssam */ 11212236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 11316673Sralph syslog(LOG_ERR, "gethostname: %m"); 1146460Swnj exit(1); 1156460Swnj } 11624745Sbloom if ((cp = index(myname, '.')) != NULL) 11724745Sbloom *cp = '\0'; 11812236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 11937295Sbostic utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 1206460Swnj if (utmpf < 0) { 12137295Sbostic syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 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 } 13342265Skarels sin.sin_family = AF_INET; 13412236Ssam sin.sin_port = sp->s_port; 13513187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 13616673Sralph syslog(LOG_ERR, "bind: %m"); 1379215Ssam exit(1); 1389215Ssam } 13912236Ssam if (!configure(s)) 14012236Ssam exit(1); 14113027Ssam signal(SIGALRM, onalrm); 1426460Swnj onalrm(); 1436460Swnj for (;;) { 1446460Swnj struct whod wd; 14512236Ssam int cc, whod, len = sizeof (from); 1466460Swnj 14712236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 14812236Ssam &from, &len); 1496460Swnj if (cc <= 0) { 1506460Swnj if (cc < 0 && errno != EINTR) 15116673Sralph syslog(LOG_WARNING, "recv: %m"); 1526460Swnj continue; 1536460Swnj } 1548486Ssam if (from.sin_port != sp->s_port) { 15516673Sralph syslog(LOG_WARNING, "%d: bad from port", 1568486Ssam ntohs(from.sin_port)); 1576460Swnj continue; 1586460Swnj } 15912347Ssam if (wd.wd_vers != WHODVERSION) 16012347Ssam continue; 16112236Ssam if (wd.wd_type != WHODTYPE_STATUS) 16212236Ssam continue; 1638486Ssam if (!verify(wd.wd_hostname)) { 16416673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1658486Ssam from.sin_addr); 1668486Ssam continue; 1678486Ssam } 16826011Smckusick (void) sprintf(path, "whod.%s", wd.wd_hostname); 16926016Skarels /* 17026016Skarels * Rather than truncating and growing the file each time, 17126016Skarels * use ftruncate if size is less than previous size. 17226016Skarels */ 17326016Skarels whod = open(path, O_WRONLY | O_CREAT, 0644); 1746460Swnj if (whod < 0) { 17516673Sralph syslog(LOG_WARNING, "%s: %m", path); 1766460Swnj continue; 1776460Swnj } 17842265Skarels #if ENDIAN != BIG_ENDIAN 17911834Ssam { 18013187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 18111834Ssam struct whoent *we; 18211834Ssam 18311834Ssam /* undo header byte swapping before writing to file */ 18411834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 18511834Ssam for (i = 0; i < 3; i++) 18611834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 18711834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 18811834Ssam we = wd.wd_we; 18911834Ssam for (i = 0; i < n; i++) { 19011834Ssam we->we_idle = ntohl(we->we_idle); 19112872Ssam we->we_utmp.out_time = 19212872Ssam ntohl(we->we_utmp.out_time); 19311834Ssam we++; 19411834Ssam } 19511834Ssam } 19611834Ssam #endif 1976460Swnj (void) time(&wd.wd_recvtime); 1986460Swnj (void) write(whod, (char *)&wd, cc); 19926016Skarels if (fstat(whod, &st) < 0 || st.st_size > cc) 20026016Skarels ftruncate(whod, cc); 2016460Swnj (void) close(whod); 2026460Swnj } 2036460Swnj } 2046460Swnj 2058486Ssam /* 2068486Ssam * Check out host name for unprintables 2078486Ssam * and other funnies before allowing a file 2088486Ssam * to be created. Sorry, but blanks aren't allowed. 2098486Ssam */ 2108486Ssam verify(name) 2118486Ssam register char *name; 2128486Ssam { 2138486Ssam register int size = 0; 2148486Ssam 2158486Ssam while (*name) { 21615214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2178486Ssam return (0); 2188486Ssam name++, size++; 2198486Ssam } 2208486Ssam return (size > 0); 2218486Ssam } 2228486Ssam 2236460Swnj int utmptime; 2246460Swnj int utmpent; 22522546Sbloom int utmpsize = 0; 22622546Sbloom struct utmp *utmp; 2276460Swnj int alarmcount; 2286460Swnj 2296460Swnj onalrm() 2306460Swnj { 23137295Sbostic register struct neighbor *np; 23237295Sbostic register struct whoent *we = mywd.wd_we, *wlast; 2336460Swnj register int i; 2346460Swnj struct stat stb; 2356460Swnj int cc; 2366460Swnj double avenrun[3]; 23737295Sbostic time_t now = time((time_t *)NULL); 23837295Sbostic char *strerror(); 2396460Swnj 2406460Swnj if (alarmcount % 10 == 0) 2416460Swnj getkmem(); 2426460Swnj alarmcount++; 2436460Swnj (void) fstat(utmpf, &stb); 24422546Sbloom if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 24517303Stef utmptime = stb.st_mtime; 24622546Sbloom if (stb.st_size > utmpsize) { 24722546Sbloom utmpsize = stb.st_size + 10 * sizeof(struct utmp); 24822546Sbloom if (utmp) 24922546Sbloom utmp = (struct utmp *)realloc(utmp, utmpsize); 25022546Sbloom else 25122546Sbloom utmp = (struct utmp *)malloc(utmpsize); 25222546Sbloom if (! utmp) { 25322546Sbloom fprintf(stderr, "rwhod: malloc failed\n"); 25422546Sbloom utmpsize = 0; 25522546Sbloom goto done; 25622546Sbloom } 25722546Sbloom } 25813187Ssam (void) lseek(utmpf, (long)0, L_SET); 25922546Sbloom cc = read(utmpf, (char *)utmp, stb.st_size); 2606460Swnj if (cc < 0) { 26137295Sbostic fprintf(stderr, "rwhod: %s: %s\n", 26237295Sbostic _PATH_UTMP, strerror(errno)); 26313469Ssam goto done; 2646460Swnj } 26512236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2666460Swnj utmpent = cc / sizeof (struct utmp); 2676460Swnj for (i = 0; i < utmpent; i++) 2686460Swnj if (utmp[i].ut_name[0]) { 26912807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 27012807Ssam sizeof (utmp[i].ut_line)); 27112807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 27212807Ssam sizeof (utmp[i].ut_name)); 27312807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2746577Ssam if (we >= wlast) 2756577Ssam break; 2766460Swnj we++; 2776460Swnj } 2786460Swnj utmpent = we - mywd.wd_we; 2796460Swnj } 28026484Smckusick 28126484Smckusick /* 28226484Smckusick * The test on utmpent looks silly---after all, if no one is 28326484Smckusick * logged on, why worry about efficiency?---but is useful on 28426484Smckusick * (e.g.) compute servers. 28526484Smckusick */ 28637971Sbostic if (utmpent && chdir(_PATH_DEV)) { 28737971Sbostic syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 28826484Smckusick exit(1); 28926484Smckusick } 2906460Swnj we = mywd.wd_we; 2916460Swnj for (i = 0; i < utmpent; i++) { 29226484Smckusick if (stat(we->we_utmp.out_line, &stb) >= 0) 29312807Ssam we->we_idle = htonl(now - stb.st_atime); 2946460Swnj we++; 2956460Swnj } 29638182Smckusick (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 2976460Swnj for (i = 0; i < 3; i++) 29812873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 2996460Swnj cc = (char *)we - (char *)&mywd; 30012807Ssam mywd.wd_sendtime = htonl(time(0)); 30112236Ssam mywd.wd_vers = WHODVERSION; 30212236Ssam mywd.wd_type = WHODTYPE_STATUS; 30312236Ssam for (np = neighbors; np != NULL; np = np->n_next) 30412236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 30512236Ssam np->n_addr, np->n_addrlen); 30637295Sbostic if (utmpent && chdir(_PATH_RWHODIR)) { 30737295Sbostic syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 30826484Smckusick exit(1); 30926484Smckusick } 31013469Ssam done: 31115541Sralph (void) alarm(AL_INTERVAL); 3126460Swnj } 3136460Swnj 3146460Swnj getkmem() 3156460Swnj { 31616663Ssam static ino_t vmunixino; 31716663Ssam static time_t vmunixctime; 31816663Ssam struct stat sb; 3196460Swnj 32037295Sbostic if (stat(_PATH_UNIX, &sb) < 0) { 32116663Ssam if (vmunixctime) 32216663Ssam return; 32316663Ssam } else { 32416663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 32516663Ssam return; 32616663Ssam vmunixctime = sb.st_ctime; 32716663Ssam vmunixino= sb.st_ino; 32816663Ssam } 3296460Swnj if (kmemf >= 0) 3306460Swnj (void) close(kmemf); 3316460Swnj loop: 33237295Sbostic if (nlist(_PATH_UNIX, nl)) { 33337295Sbostic syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX); 3346460Swnj sleep(300); 3356460Swnj goto loop; 3366460Swnj } 33737295Sbostic kmemf = open(_PATH_KMEM, O_RDONLY, 0); 3386460Swnj if (kmemf < 0) { 33937295Sbostic syslog(LOG_ERR, "%s: %m", _PATH_KMEM); 34016673Sralph exit(1); 3416460Swnj } 34213187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 34313187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 34413187Ssam sizeof (mywd.wd_boottime)); 34512807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3466460Swnj } 34712236Ssam 34812236Ssam /* 34912236Ssam * Figure out device configuration and select 35012236Ssam * networks which deserve status information. 35112236Ssam */ 35212236Ssam configure(s) 35312236Ssam int s; 35412236Ssam { 35544308Ssklower char buf[BUFSIZ], *cp, *cplim; 35612236Ssam struct ifconf ifc; 35712236Ssam struct ifreq ifreq, *ifr; 35812236Ssam struct sockaddr_in *sin; 35912236Ssam register struct neighbor *np; 36012236Ssam 36112236Ssam ifc.ifc_len = sizeof (buf); 36212236Ssam ifc.ifc_buf = buf; 36312236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 36416673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 36512236Ssam return (0); 36612236Ssam } 36712236Ssam ifr = ifc.ifc_req; 36844328Ssklower #ifdef AF_LINK 36944308Ssklower #define max(a, b) (a > b ? a : b) 37044308Ssklower #define size(p) max((p).sa_len, sizeof(p)) 37144308Ssklower #else 37244308Ssklower #define size(p) (sizeof (p)) 37344308Ssklower #endif 37444308Ssklower cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ 37544308Ssklower for (cp = buf; cp < cplim; 37644308Ssklower cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { 37744308Ssklower ifr = (struct ifreq *)cp; 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