2 * The Remote User Information Daemon 1.9
4 * (c) 1997--2001 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License. See file COPYING in any of the GNU packages.
16 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
25 #include <asm/types.h>
33 time_t last_rec; /* 0=down */
37 static int sock, port;
38 static struct hostrec *first_host;
39 static time_t now, last_local_scan;
41 static void die(char *msg, ...) __attribute__((noreturn));
49 fprintf(stderr, "nwhod: ");
50 vfprintf(stderr, msg, args);
59 struct sockaddr_in sa;
65 if (! (h = gethostbyname(name)))
71 if (! (se = getservbyname("nwho", "udp")))
72 die("Unknown service `nwho/udp'");
74 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
78 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
79 die("setsockopt: %m");
81 sa.sin_family = AF_INET;
82 sa.sin_port = port = se->s_port;
83 sa.sin_addr.s_addr = INADDR_ANY;
84 if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
89 memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
90 if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
96 scan_utmp(struct rywho_pkt *p, time_t now)
103 char name[UT_NAMESIZE+10];
107 while ((u = getutent()) && cnt < MAX_USERS)
108 if (u->ut_type == USER_PROCESS && u->ut_user[0])
111 memcpy(name, u->ut_user, UT_NAMESIZE);
113 strcpy(h->name, name);
114 h->login_time = htonl(now - u->ut_time);
115 sprintf(h->con, "%.7s", u->ut_line);
116 sprintf(device, "/dev/%s", u->ut_line);
117 if (stat(device, &st) < 0)
119 fprintf(stderr, "stat(%s): %m", device);
122 h->mesg_y = !!(S_IWGRP & st.st_mode);
124 if (st.st_mtime > last)
126 if (st.st_ctime > last)
130 h->idle_time = htonl(now - last);
133 p->num_users = htonl(cnt);
137 scan_load(struct rywho_pkt *p)
142 n = open("/proc/uptime", O_RDONLY);
145 if (read(n, buf, sizeof(buf)) <= 0)
148 if (!sscanf(buf, "%d", &i))
150 p->uptime = htonl(i);
152 n = open("/proc/loadavg", O_RDONLY);
155 if (read(n, buf, sizeof(buf)) <= 0)
158 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
160 p->avl[0] = htonl(j[0]*100 + j[1]);
161 p->avl[1] = htonl(j[2]*100 + j[3]);
162 p->avl[2] = htonl(j[4]*100 + j[5]);
166 make_pkt(struct rywho_pkt *pkt)
168 bzero(pkt, sizeof(pkt));
169 pkt->magic = htonl(NWHO_MAGIC);
170 pkt->local_time = htonl(now);
184 while (e = readdir(d))
185 if (e->d_name[0] != '.')
191 save_pkt(char *name, struct rywho_pkt *pkt, int len)
193 int fd = open(name, O_WRONLY | O_CREAT, 0666);
196 syslog(LOG_ERR, "open(%s): %m", name);
199 pkt->server_time = htonl(now);
200 if (write(fd, pkt, len) != len)
201 syslog(LOG_ERR, "write: %m");
206 static inline int /* Validation checks not implemented yet */
216 struct rywho_pkt pkt;
217 struct sockaddr_in sa;
218 int n = sizeof(struct rywho_pkt) - MAX_USERS * sizeof(struct userinfo);
223 alarm(DEFAULT_PRUNE_TIME);
225 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
232 syslog(LOG_ERR, "recvfrom: %m");
237 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
239 syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
243 if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
245 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
249 if (pkt.magic != htonl(NWHO_MAGIC))
251 syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr));
255 for(e=first_host; e; e=e->next)
256 if (e->addr == sa.sin_addr.s_addr)
260 e = malloc(sizeof(struct hostrec));
263 syslog(LOG_ERR, "Out of memory");
266 e->next = first_host;
268 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
271 sprintf(e->name, "%.30s", h->h_name);
272 for(c=e->name; *c; c++)
278 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
279 && (*c < 'a' || *c > 'z')
280 && (*c < '0' || *c > '9')
285 strcpy(e->name, inet_ntoa(sa.sin_addr));
286 e->addr = sa.sin_addr.s_addr;
290 save_pkt(e->name, &pkt, r);
296 struct rywho_pkt pkt;
297 static char hostname[64];
300 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
301 die("gethostname: %m");
302 save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
310 if (last_local_scan + DEFAULT_SEND_TIME <= now)
312 last_local_scan = now;
315 for(e=first_host; e; e=e->next)
316 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
318 if (unlink(e->name) < 0)
319 syslog(LOG_ERR, "unlink(%s): %m", e->name);
332 static struct sigaction sigact;
335 if (chdir(NWHO_SPOOL_DIR) < 0)
336 die("chdir(" NWHO_SPOOL_DIR "): %m");
338 bzero(&sigact, sizeof(sigact));
339 sigact.sa_handler = tick;
340 sigaction(SIGALRM, &sigact, NULL);
352 struct rywho_pkt pkt;
360 if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
361 syslog(LOG_ERR, "sendmsg: %m");
362 sleep(DEFAULT_SEND_TIME);
367 main(int argc, char **argv)
371 if (argc != 1 && argc != 2)
373 fprintf(stderr, "Usage: nwhod [<server>]\n");