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*46927Sbostic static char sccsid[] = "@(#)rwhod.c 5.20 (Berkeley) 03/02/91"; 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; 7632449Sbostic char *strcpy(), *malloc(); 776460Swnj long lseek(); 78*46927Sbostic void getkmem(), onalrm(); 7912236Ssam struct in_addr inet_makeaddr(); 806460Swnj 816460Swnj main() 826460Swnj { 836460Swnj struct sockaddr_in from; 8426016Skarels struct stat st; 856460Swnj char path[64]; 8626484Smckusick int on = 1; 8737295Sbostic char *cp, *index(), *strerror(); 886460Swnj 8916673Sralph if (getuid()) { 9016673Sralph fprintf(stderr, "rwhod: not super user\n"); 9116673Sralph exit(1); 9216673Sralph } 938486Ssam sp = getservbyname("who", "udp"); 948486Ssam if (sp == 0) { 958486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 968486Ssam exit(1); 978486Ssam } 986460Swnj #ifndef DEBUG 9944707Skarels daemon(1, 0); 1006460Swnj #endif 10137295Sbostic if (chdir(_PATH_RWHODIR) < 0) { 10237295Sbostic (void)fprintf(stderr, "rwhod: %s: %s\n", 10337295Sbostic _PATH_RWHODIR, strerror(errno)); 10426011Smckusick exit(1); 10526011Smckusick } 1066460Swnj (void) signal(SIGHUP, getkmem); 10724853Seric openlog("rwhod", LOG_PID, LOG_DAEMON); 10812236Ssam /* 10912236Ssam * Establish host name as returned by system. 11012236Ssam */ 11112236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 11216673Sralph syslog(LOG_ERR, "gethostname: %m"); 1136460Swnj exit(1); 1146460Swnj } 11524745Sbloom if ((cp = index(myname, '.')) != NULL) 11624745Sbloom *cp = '\0'; 11712236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 11837295Sbostic utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 1196460Swnj if (utmpf < 0) { 12037295Sbostic syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 1216460Swnj exit(1); 1226460Swnj } 1236460Swnj getkmem(); 12413187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 12516673Sralph syslog(LOG_ERR, "socket: %m"); 1269215Ssam exit(1); 1276460Swnj } 12817155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 12917055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 13017055Skarels exit(1); 13117055Skarels } 13242265Skarels sin.sin_family = AF_INET; 13312236Ssam sin.sin_port = sp->s_port; 134*46927Sbostic if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) { 13516673Sralph syslog(LOG_ERR, "bind: %m"); 1369215Ssam exit(1); 1379215Ssam } 13812236Ssam if (!configure(s)) 13912236Ssam exit(1); 14013027Ssam signal(SIGALRM, onalrm); 1416460Swnj onalrm(); 1426460Swnj for (;;) { 1436460Swnj struct whod wd; 14412236Ssam int cc, whod, len = sizeof (from); 1456460Swnj 14612236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 147*46927Sbostic (struct sockaddr *)&from, &len); 1486460Swnj if (cc <= 0) { 1496460Swnj if (cc < 0 && errno != EINTR) 15016673Sralph syslog(LOG_WARNING, "recv: %m"); 1516460Swnj continue; 1526460Swnj } 1538486Ssam if (from.sin_port != sp->s_port) { 15416673Sralph syslog(LOG_WARNING, "%d: bad from port", 1558486Ssam ntohs(from.sin_port)); 1566460Swnj continue; 1576460Swnj } 15812347Ssam if (wd.wd_vers != WHODVERSION) 15912347Ssam continue; 16012236Ssam if (wd.wd_type != WHODTYPE_STATUS) 16112236Ssam continue; 1628486Ssam if (!verify(wd.wd_hostname)) { 16316673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1648486Ssam from.sin_addr); 1658486Ssam continue; 1668486Ssam } 16726011Smckusick (void) sprintf(path, "whod.%s", wd.wd_hostname); 16826016Skarels /* 16926016Skarels * Rather than truncating and growing the file each time, 17026016Skarels * use ftruncate if size is less than previous size. 17126016Skarels */ 17226016Skarels whod = open(path, O_WRONLY | O_CREAT, 0644); 1736460Swnj if (whod < 0) { 17416673Sralph syslog(LOG_WARNING, "%s: %m", path); 1756460Swnj continue; 1766460Swnj } 17742265Skarels #if ENDIAN != BIG_ENDIAN 17811834Ssam { 17913187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 18011834Ssam struct whoent *we; 18111834Ssam 18211834Ssam /* undo header byte swapping before writing to file */ 18311834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 18411834Ssam for (i = 0; i < 3; i++) 18511834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 18611834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 18711834Ssam we = wd.wd_we; 18811834Ssam for (i = 0; i < n; i++) { 18911834Ssam we->we_idle = ntohl(we->we_idle); 19012872Ssam we->we_utmp.out_time = 19112872Ssam ntohl(we->we_utmp.out_time); 19211834Ssam we++; 19311834Ssam } 19411834Ssam } 19511834Ssam #endif 196*46927Sbostic (void) time((time_t *)&wd.wd_recvtime); 1976460Swnj (void) write(whod, (char *)&wd, cc); 19826016Skarels if (fstat(whod, &st) < 0 || st.st_size > cc) 19926016Skarels ftruncate(whod, cc); 2006460Swnj (void) close(whod); 2016460Swnj } 2026460Swnj } 2036460Swnj 2048486Ssam /* 2058486Ssam * Check out host name for unprintables 2068486Ssam * and other funnies before allowing a file 2078486Ssam * to be created. Sorry, but blanks aren't allowed. 2088486Ssam */ 2098486Ssam verify(name) 2108486Ssam register char *name; 2118486Ssam { 2128486Ssam register int size = 0; 2138486Ssam 2148486Ssam while (*name) { 21515214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2168486Ssam return (0); 2178486Ssam name++, size++; 2188486Ssam } 2198486Ssam return (size > 0); 2208486Ssam } 2218486Ssam 2226460Swnj int utmptime; 2236460Swnj int utmpent; 22422546Sbloom int utmpsize = 0; 22522546Sbloom struct utmp *utmp; 2266460Swnj int alarmcount; 2276460Swnj 228*46927Sbostic void 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, 305*46927Sbostic (struct sockaddr *)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 314*46927Sbostic void 3156460Swnj getkmem() 3166460Swnj { 31716663Ssam static ino_t vmunixino; 31816663Ssam static time_t vmunixctime; 31916663Ssam struct stat sb; 3206460Swnj 32137295Sbostic if (stat(_PATH_UNIX, &sb) < 0) { 32216663Ssam if (vmunixctime) 32316663Ssam return; 32416663Ssam } else { 32516663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 32616663Ssam return; 32716663Ssam vmunixctime = sb.st_ctime; 32816663Ssam vmunixino= sb.st_ino; 32916663Ssam } 3306460Swnj if (kmemf >= 0) 3316460Swnj (void) close(kmemf); 3326460Swnj loop: 33337295Sbostic if (nlist(_PATH_UNIX, nl)) { 33437295Sbostic syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX); 3356460Swnj sleep(300); 3366460Swnj goto loop; 3376460Swnj } 33837295Sbostic kmemf = open(_PATH_KMEM, O_RDONLY, 0); 3396460Swnj if (kmemf < 0) { 34037295Sbostic syslog(LOG_ERR, "%s: %m", _PATH_KMEM); 34116673Sralph exit(1); 3426460Swnj } 34313187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 34413187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 34513187Ssam sizeof (mywd.wd_boottime)); 34612807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3476460Swnj } 34812236Ssam 34912236Ssam /* 35012236Ssam * Figure out device configuration and select 35112236Ssam * networks which deserve status information. 35212236Ssam */ 35312236Ssam configure(s) 35412236Ssam int s; 35512236Ssam { 35644308Ssklower char buf[BUFSIZ], *cp, *cplim; 35712236Ssam struct ifconf ifc; 35812236Ssam struct ifreq ifreq, *ifr; 35912236Ssam struct sockaddr_in *sin; 36012236Ssam register struct neighbor *np; 36112236Ssam 36212236Ssam ifc.ifc_len = sizeof (buf); 36312236Ssam ifc.ifc_buf = buf; 36412236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 36516673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 36612236Ssam return (0); 36712236Ssam } 36812236Ssam ifr = ifc.ifc_req; 36944328Ssklower #ifdef AF_LINK 37044308Ssklower #define max(a, b) (a > b ? a : b) 37144308Ssklower #define size(p) max((p).sa_len, sizeof(p)) 37244308Ssklower #else 37344308Ssklower #define size(p) (sizeof (p)) 37444308Ssklower #endif 37544308Ssklower cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ 37644308Ssklower for (cp = buf; cp < cplim; 37744308Ssklower cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { 37844308Ssklower ifr = (struct ifreq *)cp; 37912236Ssam for (np = neighbors; np != NULL; np = np->n_next) 38012236Ssam if (np->n_name && 38112236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 38212236Ssam break; 38312236Ssam if (np != NULL) 38412236Ssam continue; 38512236Ssam ifreq = *ifr; 38612236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 38712236Ssam if (np == NULL) 38812236Ssam continue; 38912236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 39012236Ssam if (np->n_name == NULL) { 39112236Ssam free((char *)np); 39212236Ssam continue; 39312236Ssam } 39412236Ssam strcpy(np->n_name, ifr->ifr_name); 39512236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 39612236Ssam np->n_addr = malloc(np->n_addrlen); 39712236Ssam if (np->n_addr == NULL) { 39812236Ssam free(np->n_name); 39912236Ssam free((char *)np); 40012236Ssam continue; 40112236Ssam } 40212236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 40312236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 40416673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 40512236Ssam free((char *)np); 40612236Ssam continue; 40712236Ssam } 40821852Skarels if ((ifreq.ifr_flags & IFF_UP) == 0 || 40921852Skarels (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 41012236Ssam free((char *)np); 41112236Ssam continue; 41212236Ssam } 41312236Ssam np->n_flags = ifreq.ifr_flags; 41412236Ssam if (np->n_flags & IFF_POINTOPOINT) { 41512236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 41616673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 41712236Ssam free((char *)np); 41812236Ssam continue; 41912236Ssam } 42012236Ssam /* we assume addresses are all the same size */ 42112236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 42212236Ssam np->n_addr, np->n_addrlen); 42312236Ssam } 42412236Ssam if (np->n_flags & IFF_BROADCAST) { 42521852Skarels if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { 42621852Skarels syslog(LOG_ERR, "ioctl (get broadaddr)"); 42721852Skarels free((char *)np); 42821852Skarels continue; 42921852Skarels } 43012236Ssam /* we assume addresses are all the same size */ 43121852Skarels bcopy((char *)&ifreq.ifr_broadaddr, 43221852Skarels np->n_addr, np->n_addrlen); 43312236Ssam } 43412236Ssam /* gag, wish we could get rid of Internet dependencies */ 43512236Ssam sin = (struct sockaddr_in *)np->n_addr; 43612236Ssam sin->sin_port = sp->s_port; 43712236Ssam np->n_next = neighbors; 43812236Ssam neighbors = np; 43912236Ssam } 44012236Ssam return (1); 44112236Ssam } 44212807Ssam 44312807Ssam #ifdef DEBUG 44412807Ssam sendto(s, buf, cc, flags, to, tolen) 44512807Ssam int s; 44612807Ssam char *buf; 44712807Ssam int cc, flags; 44812807Ssam char *to; 44912807Ssam int tolen; 45012807Ssam { 45112807Ssam register struct whod *w = (struct whod *)buf; 45212807Ssam register struct whoent *we; 45312807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 45412807Ssam char *interval(); 45512807Ssam 45612807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 45712807Ssam printf("hostname %s %s\n", w->wd_hostname, 45813187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 45912807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 46013187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 46113187Ssam ntohl(w->wd_loadav[2]) / 100.0); 46212807Ssam cc -= WHDRSIZE; 46312807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 46413187Ssam time_t t = ntohl(we->we_utmp.out_time); 46512807Ssam printf("%-8.8s %s:%s %.12s", 46613187Ssam we->we_utmp.out_name, 46713187Ssam w->wd_hostname, we->we_utmp.out_line, 46813187Ssam ctime(&t)+4); 46913187Ssam we->we_idle = ntohl(we->we_idle) / 60; 47012807Ssam if (we->we_idle) { 47112807Ssam if (we->we_idle >= 100*60) 47212807Ssam we->we_idle = 100*60 - 1; 47312807Ssam if (we->we_idle >= 60) 47412807Ssam printf(" %2d", we->we_idle / 60); 47512807Ssam else 47612807Ssam printf(" "); 47712807Ssam printf(":%02d", we->we_idle % 60); 47812807Ssam } 47912807Ssam printf("\n"); 48012807Ssam } 48112807Ssam } 48212807Ssam 48312807Ssam char * 48412807Ssam interval(time, updown) 48512807Ssam int time; 48612807Ssam char *updown; 48712807Ssam { 48812807Ssam static char resbuf[32]; 48912807Ssam int days, hours, minutes; 49012807Ssam 49112807Ssam if (time < 0 || time > 3*30*24*60*60) { 49212807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 49312807Ssam return (resbuf); 49412807Ssam } 49512807Ssam minutes = (time + 59) / 60; /* round to minutes */ 49612807Ssam hours = minutes / 60; minutes %= 60; 49712807Ssam days = hours / 24; hours %= 24; 49812807Ssam if (days) 49912807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 50012807Ssam updown, days, hours, minutes); 50112807Ssam else 50212807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 50312807Ssam updown, hours, minutes); 50412807Ssam return (resbuf); 50512807Ssam } 50612807Ssam #endif 507