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>
26 #include <asm/types.h>
34 time_t last_rec; /* 0=down */
38 static int sock, port;
39 static struct hostrec *first_host;
40 static time_t now, last_local_scan;
42 static void die(char *msg, ...) __attribute__((noreturn));
50 fprintf(stderr, "nwhod: ");
51 vfprintf(stderr, msg, args);
60 struct sockaddr_in sa;
66 if (! (h = gethostbyname(name)))
72 if (! (se = getservbyname("nwho", "udp")))
73 die("Unknown service `nwho/udp'");
75 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
79 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
80 die("setsockopt: %m");
82 sa.sin_family = AF_INET;
83 sa.sin_port = port = se->s_port;
84 sa.sin_addr.s_addr = INADDR_ANY;
85 if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
90 memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
91 if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
97 scan_utmp(struct rywho_pkt *p, time_t now)
104 char name[UT_NAMESIZE+10];
108 while ((u = getutent()) && cnt < MAX_USERS)
109 if (u->ut_type == USER_PROCESS && u->ut_user[0])
112 memcpy(name, u->ut_user, UT_NAMESIZE);
114 strcpy(h->name, name);
115 h->login_time = htonl(now - u->ut_time);
116 sprintf(h->con, "%.7s", u->ut_line);
117 sprintf(device, "/dev/%s", u->ut_line);
118 if (stat(device, &st) < 0)
120 fprintf(stderr, "stat(%s): %m", device);
123 h->mesg_y = !!(S_IWGRP & st.st_mode);
125 if (st.st_mtime > last)
127 if (st.st_ctime > last)
131 h->idle_time = htonl(now - last);
134 p->num_users = htonl(cnt);
138 scan_load(struct rywho_pkt *p)
143 n = open("/proc/uptime", O_RDONLY);
146 if (read(n, buf, sizeof(buf)) <= 0)
149 if (!sscanf(buf, "%d", &i))
151 p->uptime = htonl(i);
153 n = open("/proc/loadavg", O_RDONLY);
156 if (read(n, buf, sizeof(buf)) <= 0)
159 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
161 p->avl[0] = htonl(j[0]*100 + j[1]);
162 p->avl[1] = htonl(j[2]*100 + j[3]);
163 p->avl[2] = htonl(j[4]*100 + j[5]);
167 make_pkt(struct rywho_pkt *pkt)
169 bzero(pkt, sizeof(pkt));
170 pkt->magic = htonl(NWHO_MAGIC);
171 pkt->local_time = htonl(now);
185 while (e = readdir(d))
186 if (e->d_name[0] != '.')
192 save_pkt(char *name, struct rywho_pkt *pkt, int len)
194 int fd = open(name, O_WRONLY | O_CREAT, 0666);
197 syslog(LOG_ERR, "open(%s): %m", name);
200 pkt->server_time = htonl(now);
201 if (write(fd, pkt, len) != len)
202 syslog(LOG_ERR, "write: %m");
207 static inline int /* Validation checks not implemented yet */
217 struct rywho_pkt pkt;
218 struct sockaddr_in sa;
219 int n = sizeof(struct rywho_pkt) - MAX_USERS * sizeof(struct userinfo);
224 alarm(DEFAULT_PRUNE_TIME);
226 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
233 syslog(LOG_ERR, "recvfrom: %m");
238 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
240 syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
244 if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
246 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
250 if (pkt.magic != htonl(NWHO_MAGIC))
252 syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr));
256 for(e=first_host; e; e=e->next)
257 if (e->addr == sa.sin_addr.s_addr)
261 e = malloc(sizeof(struct hostrec));
264 syslog(LOG_ERR, "Out of memory");
267 e->next = first_host;
269 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
272 sprintf(e->name, "%.30s", h->h_name);
273 for(c=e->name; *c; c++)
279 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
280 && (*c < 'a' || *c > 'z')
281 && (*c < '0' || *c > '9')
286 strcpy(e->name, inet_ntoa(sa.sin_addr));
287 e->addr = sa.sin_addr.s_addr;
291 save_pkt(e->name, &pkt, r);
297 struct rywho_pkt pkt;
298 static char hostname[64];
301 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
302 die("gethostname: %m");
303 save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
311 if (last_local_scan + DEFAULT_SEND_TIME <= now)
313 last_local_scan = now;
316 for(e=first_host; e; e=e->next)
317 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
319 if (unlink(e->name) < 0)
320 syslog(LOG_ERR, "unlink(%s): %m", e->name);
333 static struct sigaction sigact;
336 if (chdir(NWHO_SPOOL_DIR) < 0)
337 die("chdir(" NWHO_SPOOL_DIR "): %m");
339 bzero(&sigact, sizeof(sigact));
340 sigact.sa_handler = tick;
341 sigaction(SIGALRM, &sigact, NULL);
353 struct rywho_pkt pkt;
361 if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
362 syslog(LOG_ERR, "sendmsg: %m");
363 sleep(DEFAULT_SEND_TIME);
368 main(int argc, char **argv)
372 if (argc != 1 && argc != 2)
374 fprintf(stderr, "Usage: nwhod [<server>]\n");