2 * The Remote User Information Daemon
4 * (c) 1997--2010 Martin Mares <mj@ucw.cz>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
23 #include <asm/types.h>
31 time_t last_rec; /* 0=down */
35 static int sock, port;
36 static struct hostrec *first_host;
37 static time_t now, last_local_scan;
39 static void die(char *msg, ...) __attribute__((noreturn));
47 fprintf(stderr, "nwhod: ");
48 vfprintf(stderr, msg, args);
57 struct sockaddr_in sa;
61 if (! (h = gethostbyname(name)))
67 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
71 sa.sin_family = AF_INET;
72 sa.sin_port = port = htons(NWHO_PORT);
73 sa.sin_addr.s_addr = INADDR_ANY;
74 if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
79 memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
80 if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
86 scan_utmp(struct rywho_pkt *p, time_t now)
93 char name[UT_NAMESIZE+10];
97 while ((u = getutent()) && cnt < MAX_USERS)
98 if (u->ut_type == USER_PROCESS && u->ut_user[0])
101 memcpy(name, u->ut_user, UT_NAMESIZE);
103 strcpy(h->name, name);
104 h->login_time = htonl(now - u->ut_time);
105 sprintf(h->con, "%.7s", u->ut_line);
106 sprintf(device, "/dev/%s", u->ut_line);
107 if (stat(device, &st) < 0)
109 h->mesg_y = !!(S_IWGRP & st.st_mode);
111 if (st.st_mtime > last)
113 if (st.st_ctime > last)
117 h->idle_time = htonl(now - last);
120 p->num_users = htonl(cnt);
124 scan_load(struct rywho_pkt *p)
129 n = open("/proc/uptime", O_RDONLY);
132 if (read(n, buf, sizeof(buf)) <= 0)
135 if (!sscanf(buf, "%d", &i))
137 p->uptime = htonl(i);
139 n = open("/proc/loadavg", O_RDONLY);
142 if (read(n, buf, sizeof(buf)) <= 0)
145 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
147 p->avl[0] = htonl(j[0]*100 + j[1]);
148 p->avl[1] = htonl(j[2]*100 + j[3]);
149 p->avl[2] = htonl(j[4]*100 + j[5]);
153 make_pkt(struct rywho_pkt *pkt)
155 bzero(pkt, sizeof(pkt));
156 pkt->magic = htonl(NWHO_MAGIC);
157 pkt->local_time = htonl(now);
171 while (e = readdir(d))
172 if (e->d_name[0] != '.')
178 save_pkt(char *name, struct rywho_pkt *pkt, int len)
180 int fd = open(name, O_WRONLY | O_CREAT, 0666);
183 syslog(LOG_ERR, "open(%s): %m", name);
186 pkt->server_time = htonl(now);
187 if (write(fd, pkt, len) != len)
188 syslog(LOG_ERR, "write: %m");
193 static inline int /* Validation checks not implemented yet */
203 struct rywho_pkt pkt;
204 struct sockaddr_in sa;
205 int n = sizeof(struct rywho_pkt) - MAX_USERS * sizeof(struct userinfo);
210 alarm(DEFAULT_PRUNE_TIME);
212 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
219 syslog(LOG_ERR, "recvfrom: %m");
224 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
226 syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
230 if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
232 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
236 if (pkt.magic != htonl(NWHO_MAGIC))
238 syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr));
242 for(e=first_host; e; e=e->next)
243 if (e->addr == sa.sin_addr.s_addr)
247 e = malloc(sizeof(struct hostrec));
250 syslog(LOG_ERR, "Out of memory");
253 e->next = first_host;
255 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
258 sprintf(e->name, "%.30s", h->h_name);
259 for(c=e->name; *c; c++)
265 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
266 && (*c < 'a' || *c > 'z')
267 && (*c < '0' || *c > '9')
272 strcpy(e->name, inet_ntoa(sa.sin_addr));
273 e->addr = sa.sin_addr.s_addr;
277 save_pkt(e->name, &pkt, r);
283 struct rywho_pkt pkt;
284 static char hostname[64];
287 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
288 die("gethostname: %m");
289 save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
297 if (last_local_scan + DEFAULT_SEND_TIME <= now)
299 last_local_scan = now;
302 for(e=first_host; e; e=e->next)
303 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
305 if (unlink(e->name) < 0)
306 syslog(LOG_ERR, "unlink(%s): %m", e->name);
317 open("/dev/null", O_RDONLY);
331 static struct sigaction sigact;
334 if (chdir(NWHO_SPOOL_DIR) < 0)
335 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;
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");