16460Swnj #ifndef lint 2*17155Ssam static char sccsid[] = "@(#)rwhod.c 4.26 (Berkeley) 84/09/04"; 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> 2116673Sralph #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 */ 2816673Sralph #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]; 76*17155Ssam int addr, on = 1; 7712236Ssam struct hostent *hp; 786460Swnj 7916673Sralph if (getuid()) { 8016673Sralph fprintf(stderr, "rwhod: not super user\n"); 8116673Sralph exit(1); 8216673Sralph } 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); 10616673Sralph openlog("rwhod", LOG_PID, 0); 10712236Ssam /* 10812236Ssam * Establish host name as returned by system. 10912236Ssam */ 11012236Ssam if (gethostname(myname, sizeof (myname) - 1) < 0) { 11116673Sralph 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) { 12116673Sralph syslog(LOG_ERR, "/etc/utmp: %m"); 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 } 129*17155Ssam if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 13017055Skarels syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 13117055Skarels exit(1); 13217055Skarels } 13312236Ssam hp = gethostbyname(myname); 13412236Ssam if (hp == NULL) { 13516673Sralph syslog(LOG_ERR, "%s: don't know my own name", myname); 13612236Ssam exit(1); 13712236Ssam } 13812236Ssam sin.sin_family = hp->h_addrtype; 13912236Ssam sin.sin_port = sp->s_port; 14013187Ssam if (bind(s, &sin, sizeof (sin)) < 0) { 14116673Sralph syslog(LOG_ERR, "bind: %m"); 1429215Ssam exit(1); 1439215Ssam } 14412236Ssam if (!configure(s)) 14512236Ssam exit(1); 14613027Ssam signal(SIGALRM, onalrm); 1476460Swnj onalrm(); 1486460Swnj for (;;) { 1496460Swnj struct whod wd; 15012236Ssam int cc, whod, len = sizeof (from); 1516460Swnj 15212236Ssam cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 15312236Ssam &from, &len); 1546460Swnj if (cc <= 0) { 1556460Swnj if (cc < 0 && errno != EINTR) 15616673Sralph syslog(LOG_WARNING, "recv: %m"); 1576460Swnj continue; 1586460Swnj } 1598486Ssam if (from.sin_port != sp->s_port) { 16016673Sralph syslog(LOG_WARNING, "%d: bad from port", 1618486Ssam ntohs(from.sin_port)); 1626460Swnj continue; 1636460Swnj } 1648486Ssam #ifdef notdef 1658486Ssam if (gethostbyname(wd.wd_hostname) == 0) { 16616673Sralph syslog(LOG_WARNING, "%s: unknown host", 1678486Ssam wd.wd_hostname); 1686460Swnj continue; 1696460Swnj } 1708486Ssam #endif 17112347Ssam if (wd.wd_vers != WHODVERSION) 17212347Ssam continue; 17312236Ssam if (wd.wd_type != WHODTYPE_STATUS) 17412236Ssam continue; 1758486Ssam if (!verify(wd.wd_hostname)) { 17616673Sralph syslog(LOG_WARNING, "malformed host name from %x", 1778486Ssam from.sin_addr); 1788486Ssam continue; 1798486Ssam } 1809914Ssam (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname); 1816460Swnj whod = creat(path, 0666); 1826460Swnj if (whod < 0) { 18316673Sralph syslog(LOG_WARNING, "%s: %m", path); 1846460Swnj continue; 1856460Swnj } 18611834Ssam #if vax || pdp11 18711834Ssam { 18813187Ssam int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 18911834Ssam struct whoent *we; 19011834Ssam 19111834Ssam /* undo header byte swapping before writing to file */ 19211834Ssam wd.wd_sendtime = ntohl(wd.wd_sendtime); 19311834Ssam for (i = 0; i < 3; i++) 19411834Ssam wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 19511834Ssam wd.wd_boottime = ntohl(wd.wd_boottime); 19611834Ssam we = wd.wd_we; 19711834Ssam for (i = 0; i < n; i++) { 19811834Ssam we->we_idle = ntohl(we->we_idle); 19912872Ssam we->we_utmp.out_time = 20012872Ssam ntohl(we->we_utmp.out_time); 20111834Ssam we++; 20211834Ssam } 20311834Ssam } 20411834Ssam #endif 2056460Swnj (void) time(&wd.wd_recvtime); 2066460Swnj (void) write(whod, (char *)&wd, cc); 2076460Swnj (void) close(whod); 2086460Swnj } 2096460Swnj } 2106460Swnj 2118486Ssam /* 2128486Ssam * Check out host name for unprintables 2138486Ssam * and other funnies before allowing a file 2148486Ssam * to be created. Sorry, but blanks aren't allowed. 2158486Ssam */ 2168486Ssam verify(name) 2178486Ssam register char *name; 2188486Ssam { 2198486Ssam register int size = 0; 2208486Ssam 2218486Ssam while (*name) { 22215214Sleres if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 2238486Ssam return (0); 2248486Ssam name++, size++; 2258486Ssam } 2268486Ssam return (size > 0); 2278486Ssam } 2288486Ssam 2296460Swnj int utmptime; 2306460Swnj int utmpent; 2316577Ssam struct utmp utmp[100]; 2326460Swnj int alarmcount; 2336460Swnj 2346460Swnj onalrm() 2356460Swnj { 2366460Swnj register int i; 2376460Swnj struct stat stb; 2386577Ssam register struct whoent *we = mywd.wd_we, *wlast; 2396460Swnj int cc; 2406460Swnj double avenrun[3]; 2416460Swnj time_t now = time(0); 24212236Ssam register struct neighbor *np; 2436460Swnj 2446460Swnj if (alarmcount % 10 == 0) 2456460Swnj getkmem(); 2466460Swnj alarmcount++; 2476460Swnj (void) fstat(utmpf, &stb); 2486460Swnj if (stb.st_mtime != utmptime) { 24913187Ssam (void) lseek(utmpf, (long)0, L_SET); 2506460Swnj cc = read(utmpf, (char *)utmp, sizeof (utmp)); 2516460Swnj if (cc < 0) { 2526460Swnj perror("/etc/utmp"); 25313469Ssam goto done; 2546460Swnj } 25512236Ssam wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 2566460Swnj utmpent = cc / sizeof (struct utmp); 2576460Swnj for (i = 0; i < utmpent; i++) 2586460Swnj if (utmp[i].ut_name[0]) { 25912807Ssam bcopy(utmp[i].ut_line, we->we_utmp.out_line, 26012807Ssam sizeof (utmp[i].ut_line)); 26112807Ssam bcopy(utmp[i].ut_name, we->we_utmp.out_name, 26212807Ssam sizeof (utmp[i].ut_name)); 26312807Ssam we->we_utmp.out_time = htonl(utmp[i].ut_time); 2646577Ssam if (we >= wlast) 2656577Ssam break; 2666460Swnj we++; 2676460Swnj } 2686460Swnj utmpent = we - mywd.wd_we; 2696460Swnj } 2706460Swnj we = mywd.wd_we; 2716460Swnj for (i = 0; i < utmpent; i++) { 27212807Ssam if (stat(we->we_utmp.out_line, &stb) >= 0) 27312807Ssam we->we_idle = htonl(now - stb.st_atime); 2746460Swnj we++; 2756460Swnj } 27613187Ssam (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET); 2776460Swnj (void) read(kmemf, (char *)avenrun, sizeof (avenrun)); 2786460Swnj for (i = 0; i < 3; i++) 27912873Ssam mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 2806460Swnj cc = (char *)we - (char *)&mywd; 28112807Ssam mywd.wd_sendtime = htonl(time(0)); 28212236Ssam mywd.wd_vers = WHODVERSION; 28312236Ssam mywd.wd_type = WHODTYPE_STATUS; 28412236Ssam for (np = neighbors; np != NULL; np = np->n_next) 28512236Ssam (void) sendto(s, (char *)&mywd, cc, 0, 28612236Ssam np->n_addr, np->n_addrlen); 28713469Ssam done: 28815541Sralph (void) alarm(AL_INTERVAL); 2896460Swnj } 2906460Swnj 2916460Swnj getkmem() 2926460Swnj { 2936460Swnj struct nlist *nlp; 29416663Ssam static ino_t vmunixino; 29516663Ssam static time_t vmunixctime; 29616663Ssam struct stat sb; 2976460Swnj 29816663Ssam if (stat("/vmunix", &sb) < 0) { 29916663Ssam if (vmunixctime) 30016663Ssam return; 30116663Ssam } else { 30216663Ssam if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 30316663Ssam return; 30416663Ssam vmunixctime = sb.st_ctime; 30516663Ssam vmunixino= sb.st_ino; 30616663Ssam } 3076460Swnj if (kmemf >= 0) 3086460Swnj (void) close(kmemf); 3096460Swnj loop: 31016673Sralph if (nlist("/vmunix", nl)) { 31116673Sralph syslog(LOG_WARNING, "/vmunix namelist botch"); 3126460Swnj sleep(300); 3136460Swnj goto loop; 3146460Swnj } 31513187Ssam kmemf = open("/dev/kmem", O_RDONLY); 3166460Swnj if (kmemf < 0) { 31716673Sralph syslog(LOG_ERR, "/dev/kmem: %m"); 31816673Sralph exit(1); 3196460Swnj } 32013187Ssam (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 32113187Ssam (void) read(kmemf, (char *)&mywd.wd_boottime, 32213187Ssam sizeof (mywd.wd_boottime)); 32312807Ssam mywd.wd_boottime = htonl(mywd.wd_boottime); 3246460Swnj } 32512236Ssam 32612236Ssam /* 32712236Ssam * Figure out device configuration and select 32812236Ssam * networks which deserve status information. 32912236Ssam */ 33012236Ssam configure(s) 33112236Ssam int s; 33212236Ssam { 33312236Ssam char buf[BUFSIZ]; 33412236Ssam struct ifconf ifc; 33512236Ssam struct ifreq ifreq, *ifr; 33612236Ssam struct sockaddr_in *sin; 33712236Ssam register struct neighbor *np; 33813187Ssam int n; 33912236Ssam 34012236Ssam ifc.ifc_len = sizeof (buf); 34112236Ssam ifc.ifc_buf = buf; 34212236Ssam if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { 34316673Sralph syslog(LOG_ERR, "ioctl (get interface configuration)"); 34412236Ssam return (0); 34512236Ssam } 34612236Ssam ifr = ifc.ifc_req; 34712236Ssam for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 34812236Ssam for (np = neighbors; np != NULL; np = np->n_next) 34912236Ssam if (np->n_name && 35012236Ssam strcmp(ifr->ifr_name, np->n_name) == 0) 35112236Ssam break; 35212236Ssam if (np != NULL) 35312236Ssam continue; 35412236Ssam ifreq = *ifr; 35512236Ssam np = (struct neighbor *)malloc(sizeof (*np)); 35612236Ssam if (np == NULL) 35712236Ssam continue; 35812236Ssam np->n_name = malloc(strlen(ifr->ifr_name) + 1); 35912236Ssam if (np->n_name == NULL) { 36012236Ssam free((char *)np); 36112236Ssam continue; 36212236Ssam } 36312236Ssam strcpy(np->n_name, ifr->ifr_name); 36412236Ssam np->n_addrlen = sizeof (ifr->ifr_addr); 36512236Ssam np->n_addr = malloc(np->n_addrlen); 36612236Ssam if (np->n_addr == NULL) { 36712236Ssam free(np->n_name); 36812236Ssam free((char *)np); 36912236Ssam continue; 37012236Ssam } 37112236Ssam bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen); 37212236Ssam if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { 37316673Sralph syslog(LOG_ERR, "ioctl (get interface flags)"); 37412236Ssam free((char *)np); 37512236Ssam continue; 37612236Ssam } 37712236Ssam if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) { 37812236Ssam free((char *)np); 37912236Ssam continue; 38012236Ssam } 38112236Ssam np->n_flags = ifreq.ifr_flags; 38212236Ssam if (np->n_flags & IFF_POINTOPOINT) { 38312236Ssam if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) { 38416673Sralph syslog(LOG_ERR, "ioctl (get dstaddr)"); 38512236Ssam free((char *)np); 38612236Ssam continue; 38712236Ssam } 38812236Ssam /* we assume addresses are all the same size */ 38912236Ssam bcopy((char *)&ifreq.ifr_dstaddr, 39012236Ssam np->n_addr, np->n_addrlen); 39112236Ssam } 39212236Ssam if (np->n_flags & IFF_BROADCAST) { 39312236Ssam /* we assume addresses are all the same size */ 39412236Ssam sin = (struct sockaddr_in *)np->n_addr; 39516468Skarels sin->sin_addr = inet_makeaddr((np->n_flags & IFF_LOCAL)? 39616468Skarels inet_subnetof(sin->sin_addr) : 39716468Skarels inet_netof(sin->sin_addr), 39816468Skarels INADDR_ANY); 39912236Ssam } 40012236Ssam /* gag, wish we could get rid of Internet dependencies */ 40112236Ssam sin = (struct sockaddr_in *)np->n_addr; 40212236Ssam sin->sin_port = sp->s_port; 40312236Ssam np->n_next = neighbors; 40412236Ssam neighbors = np; 40512236Ssam } 40612236Ssam return (1); 40712236Ssam } 40812807Ssam 40916468Skarels /* 41016468Skarels * Return the possible subnetwork number from an internet address. 41116468Skarels * If the address is of the form of a subnet address (most significant 41216468Skarels * bit of the host part is set), believe the subnet exists. 41316468Skarels * Otherwise, return the network number. Any subnet number is only valid 41416468Skarels * if this is a ``local'' net. 41516468Skarels */ 41616468Skarels inet_subnetof(in) 41716468Skarels struct in_addr in; 41816468Skarels { 41916468Skarels register u_long i = ntohl(in.s_addr); 42016468Skarels 42116468Skarels if (IN_CLASSA(i)) { 42216468Skarels if (IN_SUBNETA(i)) 42316468Skarels return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT); 42416468Skarels else 42516468Skarels return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); 42616468Skarels } else if (IN_CLASSB(i)) { 42716468Skarels if (IN_SUBNETB(i)) 42816468Skarels return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT); 42916468Skarels else 43016468Skarels return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); 43116468Skarels } else 43216468Skarels return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); 43316468Skarels } 43416468Skarels 43512807Ssam #ifdef DEBUG 43612807Ssam sendto(s, buf, cc, flags, to, tolen) 43712807Ssam int s; 43812807Ssam char *buf; 43912807Ssam int cc, flags; 44012807Ssam char *to; 44112807Ssam int tolen; 44212807Ssam { 44312807Ssam register struct whod *w = (struct whod *)buf; 44412807Ssam register struct whoent *we; 44512807Ssam struct sockaddr_in *sin = (struct sockaddr_in *)to; 44612807Ssam char *interval(); 44712807Ssam 44812807Ssam printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 44912807Ssam printf("hostname %s %s\n", w->wd_hostname, 45013187Ssam interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 45112807Ssam printf("load %4.2f, %4.2f, %4.2f\n", 45213187Ssam ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 45313187Ssam ntohl(w->wd_loadav[2]) / 100.0); 45412807Ssam cc -= WHDRSIZE; 45512807Ssam for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 45613187Ssam time_t t = ntohl(we->we_utmp.out_time); 45712807Ssam printf("%-8.8s %s:%s %.12s", 45813187Ssam we->we_utmp.out_name, 45913187Ssam w->wd_hostname, we->we_utmp.out_line, 46013187Ssam ctime(&t)+4); 46113187Ssam we->we_idle = ntohl(we->we_idle) / 60; 46212807Ssam if (we->we_idle) { 46312807Ssam if (we->we_idle >= 100*60) 46412807Ssam we->we_idle = 100*60 - 1; 46512807Ssam if (we->we_idle >= 60) 46612807Ssam printf(" %2d", we->we_idle / 60); 46712807Ssam else 46812807Ssam printf(" "); 46912807Ssam printf(":%02d", we->we_idle % 60); 47012807Ssam } 47112807Ssam printf("\n"); 47212807Ssam } 47312807Ssam } 47412807Ssam 47512807Ssam char * 47612807Ssam interval(time, updown) 47712807Ssam int time; 47812807Ssam char *updown; 47912807Ssam { 48012807Ssam static char resbuf[32]; 48112807Ssam int days, hours, minutes; 48212807Ssam 48312807Ssam if (time < 0 || time > 3*30*24*60*60) { 48412807Ssam (void) sprintf(resbuf, " %s ??:??", updown); 48512807Ssam return (resbuf); 48612807Ssam } 48712807Ssam minutes = (time + 59) / 60; /* round to minutes */ 48812807Ssam hours = minutes / 60; minutes %= 60; 48912807Ssam days = hours / 24; hours %= 24; 49012807Ssam if (days) 49112807Ssam (void) sprintf(resbuf, "%s %2d+%02d:%02d", 49212807Ssam updown, days, hours, minutes); 49312807Ssam else 49412807Ssam (void) sprintf(resbuf, "%s %2d:%02d", 49512807Ssam updown, hours, minutes); 49612807Ssam return (resbuf); 49712807Ssam } 49812807Ssam #endif 499