16460Swnj #ifndef lint 2*16673Sralph static char sccsid[] = "@(#)rwhod.c 4.24 (Berkeley) 84/07/06"; 36460Swnj #endif 46460Swnj 59215Ssam #include <sys/types.h> 69215Ssam #include <sys/socket.h> 79215Ssam #include <sys/stat.h> 89215Ssam #include <sys/ioctl.h> 913187Ssam #include <sys/file.h> 109215Ssam 1112236Ssam #include <net/if.h> 129215Ssam #include <netinet/in.h> 139215Ssam 149215Ssam #include <nlist.h> 156460Swnj #include <stdio.h> 166460Swnj #include <signal.h> 176460Swnj #include <errno.h> 186460Swnj #include <utmp.h> 198486Ssam #include <ctype.h> 208486Ssam #include <netdb.h> 21*16673Sralph #include <syslog.h> 2213567Ssam #include "rwhod.h" 236460Swnj 2415541Sralph /* 2515541Sralph * Alarm interval. Don't forget to change the down time check in ruptime 2615541Sralph * if this is changed. 2715541Sralph */ 28*16673Sralph #define AL_INTERVAL (3 * 60) 2915541Sralph 308486Ssam struct sockaddr_in sin = { AF_INET }; 316460Swnj 326460Swnj extern errno; 336460Swnj 3412236Ssam char myname[32]; 356460Swnj 366460Swnj struct nlist nl[] = { 376460Swnj #define NL_AVENRUN 0 386460Swnj { "_avenrun" }, 399215Ssam #define NL_BOOTTIME 1 409215Ssam { "_boottime" }, 416460Swnj 0 426460Swnj }; 436460Swnj 4412236Ssam /* 4512236Ssam * We communicate with each neighbor in 4612236Ssam * a list constructed at the time we're 4712236Ssam * started up. Neighbors are currently 4812236Ssam * directly connected via a hardware interface. 4912236Ssam */ 5012236Ssam struct neighbor { 5112236Ssam struct neighbor *n_next; 5212236Ssam char *n_name; /* interface name */ 5312236Ssam char *n_addr; /* who to send to */ 5412236Ssam int n_addrlen; /* size of address */ 5512236Ssam int n_flags; /* should forward?, interface flags */ 5612236Ssam }; 5712236Ssam 5812236Ssam struct neighbor *neighbors; 596460Swnj struct whod mywd; 6012236Ssam struct servent *sp; 616460Swnj int s, utmpf, kmemf = -1; 626460Swnj 6312236Ssam #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we)) 6412236Ssam #define RWHODIR "/usr/spool/rwho" 6511834Ssam 666460Swnj int onalrm(); 6712236Ssam char *strcpy(), *sprintf(), *malloc(); 686460Swnj long lseek(); 696460Swnj int getkmem(); 7012236Ssam struct in_addr inet_makeaddr(); 716460Swnj 726460Swnj main() 736460Swnj { 746460Swnj struct sockaddr_in from; 756460Swnj char path[64]; 766460Swnj int addr; 7712236Ssam struct hostent *hp; 786460Swnj 79*16673Sralph if (getuid()) { 80*16673Sralph fprintf(stderr, "rwhod: not super user\n"); 81*16673Sralph exit(1); 82*16673Sralph } 838486Ssam sp = getservbyname("who", "udp"); 848486Ssam if (sp == 0) { 858486Ssam fprintf(stderr, "rwhod: udp/who: unknown service\n"); 868486Ssam exit(1); 878486Ssam } 886460Swnj #ifndef DEBUG 896460Swnj if (fork()) 906460Swnj exit(0); 916460Swnj { int s; 926460Swnj for (s = 0; s < 10; s++) 936460Swnj (void) close(s); 946460Swnj (void) open("/", 0); 956460Swnj (void) dup2(0, 1); 966460Swnj (void) dup2(0, 2); 976460Swnj s = open("/dev/tty", 2); 986460Swnj if (s >= 0) { 996460Swnj ioctl(s, TIOCNOTTY, 0); 1006460Swnj (void) close(s); 1016460Swnj } 1026460Swnj } 1036460Swnj #endif 1046460Swnj (void) chdir("/dev"); 1056460Swnj (void) signal(SIGHUP, getkmem); 106*16673Sralph openlog("rwhod", LOG_PID, 0); 10712236Ssam /* 10812236Ssam * Establish host name as returned by system. 10912236Ssam */ 11012236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 111*16673Sralph syslog(LOG_ERR, "gethostname: %m"); 1126460Swnj exit(1); 1136460Swnj } 11412236Ssam strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 11513187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1166460Swnj if (utmpf < 0) { 1176460Swnj (void) close(creat("/etc/utmp", 0644)); 11813187Ssam utmpf = open("/etc/utmp", O_RDONLY); 1196460Swnj } 1206460Swnj if (utmpf < 0) { 121*16673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 1226460Swnj exit(1); 1236460Swnj } 1246460Swnj getkmem(); 12513187Ssam if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 126*16673Sralph syslog(LOG_ERR, "socket: %m"); 1279215Ssam exit(1); 1286460Swnj } 12912236Ssam hp = gethostbyname(myname); 13012236Ssam if (hp == NULL) { 131*16673Sralph syslog(LOG_ERR, "%s: don't know my own name", myname); 13212236Ssam exit(1); 13312236Ssam } 13412236Ssam sin.sin_family = hp->h_addrtype; 13512236Ssam sin.sin_port = sp->s_port; 13613187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 137*16673Sralph syslog(LOG_ERR, "bind: %m"); 1389215Ssam exit(1); 1399215Ssam } 14012236Ssam if (!configure(s)) 14112236Ssam exit(1); 14213027Ssam signal(SIGALRM, onalrm); 1436460Swnj onalrm(); 1446460Swnj for (;;) { 1456460Swnj struct whod wd; 14612236Ssam int cc, whod, len = sizeof (from); 1476460Swnj 14812236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 14912236Ssam &from, &len); 1506460Swnj if (cc <= 0) { 1516460Swnj if (cc < 0 && errno != EINTR) 152*16673Sralph syslog(LOG_WARNING, "recv: %m"); 1536460Swnj continue; 1546460Swnj } 1558486Ssam if (from.sin_port != sp->s_port) { 156*16673Sralph syslog(LOG_WARNING, "%d: bad from port", 1578486Ssam ntohs(from.sin_port)); 1586460Swnj continue; 1596460Swnj } 1608486Ssam #ifdef notdef 1618486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 162*16673Sralph syslog(LOG_WARNING, "%s: unknown host", 1638486Ssam wd.wd_hostname); 1646460Swnj continue; 1656460Swnj } 1668486Ssam #endif 16712347Ssam if (wd.wd_vers != WHODVERSION) 16812347Ssam continue; 16912236Ssam if (wd.wd_type != WHODTYPE_STATUS) 17012236Ssam continue; 1718486Ssam if (!verify(wd.wd_hostname)) { 172*16673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1738486Ssam from.sin_addr); 1748486Ssam continue; 1758486Ssam } 1769914Ssam (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname); 1776460Swnj whod = creat(path, 0666); 1786460Swnj if (whod < 0) { 179*16673Sralph syslog(LOG_WARNING, "%s: %m", path); 1806460Swnj continue; 1816460Swnj } 18211834Ssam #if vax || pdp11 18311834Ssam { 18413187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 18511834Ssam struct whoent *we; 18611834Ssam 18711834Ssam /* undo header byte swapping before writing to file */ 18811834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 18911834Ssam for (i = 0; i < 3; i++) 19011834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 19111834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 19211834Ssam we = wd.wd_we; 19311834Ssam for (i = 0; i < n; i++) { 19411834Ssam we->we_idle = ntohl(we->we_idle); 19512872Ssam we->we_utmp.out_time = 19612872Ssam ntohl(we->we_utmp.out_time); 19711834Ssam we++; 19811834Ssam } 19911834Ssam } 20011834Ssam #endif 2016460Swnj (void) time(&wd.wd_recvtime); 2026460Swnj (void) write(whod, (char *)&wd, cc); 2036460Swnj (void) close(whod); 2046460Swnj } 2056460Swnj } 2066460Swnj 2078486Ssam /* 2088486Ssam * Check out host name for unprintables 2098486Ssam * and other funnies before allowing a file 2108486Ssam * to be created. Sorry, but blanks aren't allowed. 2118486Ssam */ 2128486Ssam verify(name) 2138486Ssam register char *name; 2148486Ssam { 2158486Ssam register int size = 0; 2168486Ssam 2178486Ssam while (*name) { 21815214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2198486Ssam return (0); 2208486Ssam name++, size++; 2218486Ssam } 2228486Ssam return (size > 0); 2238486Ssam } 2248486Ssam 2256460Swnj int utmptime; 2266460Swnj int utmpent; 2276577Ssam struct utmp utmp[100]; 2286460Swnj int alarmcount; 2296460Swnj 2306460Swnj onalrm() 2316460Swnj { 2326460Swnj register int i; 2336460Swnj struct stat stb; 2346577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2356460Swnj int cc; 2366460Swnj double avenrun[3]; 2376460Swnj time_t now = time(0); 23812236Ssam register struct neighbor *np; 2396460Swnj 2406460Swnj if (alarmcount % 10 == 0) 2416460Swnj getkmem(); 2426460Swnj alarmcount++; 2436460Swnj (void) fstat(utmpf, &stb); 2446460Swnj if (stb.st_mtime != utmptime) { 24513187Ssam (void) lseek(utmpf, (long)0, L_SET); 2466460Swnj cc = read(utmpf, (char *)utmp, sizeof (utmp)); 2476460Swnj if (cc < 0) { 2486460Swnj perror("/etc/utmp"); 24913469Ssam goto done; 2506460Swnj } 25112236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2526460Swnj utmpent = cc / sizeof (struct utmp); 2536460Swnj for (i = 0; i < utmpent; i++) 2546460Swnj if (utmp[i].ut_name[0]) { 25512807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 25612807Ssam sizeof (utmp[i].ut_line)); 25712807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 25812807Ssam sizeof (utmp[i].ut_name)); 25912807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2606577Ssam if (we >= wlast) 2616577Ssam break; 2626460Swnj we++; 2636460Swnj } 2646460Swnj utmpent = we - mywd.wd_we; 2656460Swnj } 2666460Swnj we = mywd.wd_we; 2676460Swnj for (i = 0; i < utmpent; i++) { 26812807Ssam if (stat(we->we_utmp.out_line, &stb) >= 0) 26912807Ssam we->we_idle = htonl(now - stb.st_atime); 2706460Swnj we++; 2716460Swnj } 27213187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 2736460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 2746460Swnj for (i = 0; i < 3; i++) 27512873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 2766460Swnj cc = (char *)we - (char *)&mywd; 27712807Ssam mywd.wd_sendtime = htonl(time(0)); 27812236Ssam mywd.wd_vers = WHODVERSION; 27912236Ssam mywd.wd_type = WHODTYPE_STATUS; 28012236Ssam for (np = neighbors; np != NULL; np = np->n_next) 28112236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 28212236Ssam np->n_addr, np->n_addrlen); 28313469Ssam done: 28415541Sralph (void) alarm(AL_INTERVAL); 2856460Swnj } 2866460Swnj 2876460Swnj getkmem() 2886460Swnj { 2896460Swnj struct nlist *nlp; 29016663Ssam static ino_t vmunixino; 29116663Ssam static time_t vmunixctime; 29216663Ssam struct stat sb; 2936460Swnj 29416663Ssam if (stat("/vmunix", &sb) < 0) { 29516663Ssam if (vmunixctime) 29616663Ssam return; 29716663Ssam } else { 29816663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 29916663Ssam return; 30016663Ssam vmunixctime = sb.st_ctime; 30116663Ssam vmunixino= sb.st_ino; 30216663Ssam } 3036460Swnj if (kmemf >= 0) 3046460Swnj (void) close(kmemf); 3056460Swnj loop: 306*16673Sralph if (nlist("/vmunix", nl)) { 307*16673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3086460Swnj sleep(300); 3096460Swnj goto loop; 3106460Swnj } 31113187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3126460Swnj if (kmemf < 0) { 313*16673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 314*16673Sralph exit(1); 3156460Swnj } 31613187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 31713187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 31813187Ssam sizeof (mywd.wd_boottime)); 31912807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3206460Swnj } 32112236Ssam 32212236Ssam /* 32312236Ssam * Figure out device configuration and select 32412236Ssam * networks which deserve status information. 32512236Ssam */ 32612236Ssam configure(s) 32712236Ssam int s; 32812236Ssam { 32912236Ssam char buf[BUFSIZ]; 33012236Ssam struct ifconf ifc; 33112236Ssam struct ifreq ifreq, *ifr; 33212236Ssam struct sockaddr_in *sin; 33312236Ssam register struct neighbor *np; 33413187Ssam int n; 33512236Ssam 33612236Ssam ifc.ifc_len = sizeof (buf); 33712236Ssam ifc.ifc_buf = buf; 33812236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 339*16673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 34012236Ssam return (0); 34112236Ssam } 34212236Ssam ifr = ifc.ifc_req; 34312236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 34412236Ssam for (np = neighbors; np != NULL; np = np->n_next) 34512236Ssam if (np->n_name && 34612236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 34712236Ssam break; 34812236Ssam if (np != NULL) 34912236Ssam continue; 35012236Ssam ifreq = *ifr; 35112236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 35212236Ssam if (np == NULL) 35312236Ssam continue; 35412236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 35512236Ssam if (np->n_name == NULL) { 35612236Ssam free((char *)np); 35712236Ssam continue; 35812236Ssam } 35912236Ssam strcpy(np->n_name, ifr->ifr_name); 36012236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 36112236Ssam np->n_addr = malloc(np->n_addrlen); 36212236Ssam if (np->n_addr == NULL) { 36312236Ssam free(np->n_name); 36412236Ssam free((char *)np); 36512236Ssam continue; 36612236Ssam } 36712236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 36812236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 369*16673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 37012236Ssam free((char *)np); 37112236Ssam continue; 37212236Ssam } 37312236Ssam if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 37412236Ssam free((char *)np); 37512236Ssam continue; 37612236Ssam } 37712236Ssam np->n_flags = ifreq.ifr_flags; 37812236Ssam if (np->n_flags & IFF_POINTOPOINT) { 37912236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 380*16673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 38112236Ssam free((char *)np); 38212236Ssam continue; 38312236Ssam } 38412236Ssam /* we assume addresses are all the same size */ 38512236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 38612236Ssam np->n_addr, np->n_addrlen); 38712236Ssam } 38812236Ssam if (np->n_flags & IFF_BROADCAST) { 38912236Ssam /* we assume addresses are all the same size */ 39012236Ssam sin = (struct sockaddr_in *)np->n_addr; 39116468Skarels sin->sin_addr = inet_makeaddr((np->n_flags & IFF_LOCAL)? 39216468Skarels inet_subnetof(sin->sin_addr) : 39316468Skarels inet_netof(sin->sin_addr), 39416468Skarels INADDR_ANY); 39512236Ssam } 39612236Ssam /* gag, wish we could get rid of Internet dependencies */ 39712236Ssam sin = (struct sockaddr_in *)np->n_addr; 39812236Ssam sin->sin_port = sp->s_port; 39912236Ssam np->n_next = neighbors; 40012236Ssam neighbors = np; 40112236Ssam } 40212236Ssam return (1); 40312236Ssam } 40412807Ssam 40516468Skarels /* 40616468Skarels * Return the possible subnetwork number from an internet address. 40716468Skarels * If the address is of the form of a subnet address (most significant 40816468Skarels * bit of the host part is set), believe the subnet exists. 40916468Skarels * Otherwise, return the network number. Any subnet number is only valid 41016468Skarels * if this is a ``local'' net. 41116468Skarels */ 41216468Skarels inet_subnetof(in) 41316468Skarels struct in_addr in; 41416468Skarels { 41516468Skarels register u_long i = ntohl(in.s_addr); 41616468Skarels 41716468Skarels if (IN_CLASSA(i)) { 41816468Skarels if (IN_SUBNETA(i)) 41916468Skarels return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT); 42016468Skarels else 42116468Skarels return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); 42216468Skarels } else if (IN_CLASSB(i)) { 42316468Skarels if (IN_SUBNETB(i)) 42416468Skarels return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT); 42516468Skarels else 42616468Skarels return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); 42716468Skarels } else 42816468Skarels return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); 42916468Skarels } 43016468Skarels 43112807Ssam #ifdef DEBUG 43212807Ssam sendto(s, buf, cc, flags, to, tolen) 43312807Ssam int s; 43412807Ssam char *buf; 43512807Ssam int cc, flags; 43612807Ssam char *to; 43712807Ssam int tolen; 43812807Ssam { 43912807Ssam register struct whod *w = (struct whod *)buf; 44012807Ssam register struct whoent *we; 44112807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 44212807Ssam char *interval(); 44312807Ssam 44412807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 44512807Ssam printf("hostname %s %s\n", w->wd_hostname, 44613187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 44712807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 44813187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 44913187Ssam ntohl(w->wd_loadav[2]) / 100.0); 45012807Ssam cc -= WHDRSIZE; 45112807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 45213187Ssam time_t t = ntohl(we->we_utmp.out_time); 45312807Ssam printf("%-8.8s %s:%s %.12s", 45413187Ssam we->we_utmp.out_name, 45513187Ssam w->wd_hostname, we->we_utmp.out_line, 45613187Ssam ctime(&t)+4); 45713187Ssam we->we_idle = ntohl(we->we_idle) / 60; 45812807Ssam if (we->we_idle) { 45912807Ssam if (we->we_idle >= 100*60) 46012807Ssam we->we_idle = 100*60 - 1; 46112807Ssam if (we->we_idle >= 60) 46212807Ssam printf(" %2d", we->we_idle / 60); 46312807Ssam else 46412807Ssam printf(" "); 46512807Ssam printf(":%02d", we->we_idle % 60); 46612807Ssam } 46712807Ssam printf("\n"); 46812807Ssam } 46912807Ssam } 47012807Ssam 47112807Ssam char * 47212807Ssam interval(time, updown) 47312807Ssam int time; 47412807Ssam char *updown; 47512807Ssam { 47612807Ssam static char resbuf[32]; 47712807Ssam int days, hours, minutes; 47812807Ssam 47912807Ssam if (time < 0 || time > 3*30*24*60*60) { 48012807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 48112807Ssam return (resbuf); 48212807Ssam } 48312807Ssam minutes = (time + 59) / 60; /* round to minutes */ 48412807Ssam hours = minutes / 60; minutes %= 60; 48512807Ssam days = hours / 24; hours %= 24; 48612807Ssam if (days) 48712807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 48812807Ssam updown, days, hours, minutes); 48912807Ssam else 49012807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 49112807Ssam updown, hours, minutes); 49212807Ssam return (resbuf); 49312807Ssam } 49412807Ssam #endif 495