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>
30 time_t last_rec; /* 0=down */
34 static int sock, port;
35 static struct hostrec *first_host;
36 static time_t now, last_local_scan;
38 static void die(char *msg, ...) __attribute__((noreturn));
46 fprintf(stderr, "nwhod: ");
47 vfprintf(stderr, msg, args);
56 struct sockaddr_in sa;
60 if (! (h = gethostbyname(name)))
66 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
70 sa.sin_family = AF_INET;
71 sa.sin_port = port = htons(NWHO_PORT);
72 sa.sin_addr.s_addr = INADDR_ANY;
73 if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
78 memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
79 if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
85 scan_utmp(struct nwho_pkt *p, time_t now)
92 char name[UT_NAMESIZE+10];
96 while ((u = getutent()) && cnt < MAX_USERS)
97 if (u->ut_type == USER_PROCESS && u->ut_user[0])
100 memcpy(name, u->ut_user, UT_NAMESIZE);
102 strcpy(h->name, name);
103 h->login_time = htonl(now - u->ut_time);
104 sprintf(h->con, "%.7s", u->ut_line);
105 sprintf(device, "/dev/%s", u->ut_line);
106 if (stat(device, &st) < 0)
108 h->mesg_y = !!(S_IWGRP & st.st_mode);
110 if (st.st_mtime > last)
112 if (st.st_ctime > last)
116 h->idle_time = htonl(now - last);
119 p->num_users = htonl(cnt);
123 scan_load(struct nwho_pkt *p)
128 n = open("/proc/uptime", O_RDONLY);
131 if (read(n, buf, sizeof(buf)) <= 0)
134 if (!sscanf(buf, "%d", &i))
136 p->uptime = htonl(i);
138 n = open("/proc/loadavg", O_RDONLY);
141 if (read(n, buf, sizeof(buf)) <= 0)
144 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
146 p->avl[0] = htonl(j[0]*100 + j[1]);
147 p->avl[1] = htonl(j[2]*100 + j[3]);
148 p->avl[2] = htonl(j[4]*100 + j[5]);
152 make_pkt(struct nwho_pkt *pkt)
154 bzero(pkt, sizeof(pkt));
155 pkt->magic = htonl(NWHO_MAGIC);
156 pkt->local_time = htonl(now);
170 while (e = readdir(d))
171 if (e->d_name[0] != '.')
177 save_pkt(char *name, struct nwho_pkt *pkt, int len)
179 int fd = open(name, O_WRONLY | O_CREAT, 0666);
182 syslog(LOG_ERR, "open(%s): %m", name);
185 pkt->server_time = htonl(now);
186 if (write(fd, pkt, len) != len)
187 syslog(LOG_ERR, "write: %m");
192 static inline int /* Validation checks not implemented yet */
203 struct sockaddr_in sa;
204 socklen_t al = sizeof(sa);
205 int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
210 alarm(DEFAULT_PRUNE_TIME);
211 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
218 syslog(LOG_ERR, "recvfrom: %m");
223 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
225 syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
229 if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
231 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
235 if (pkt.magic != htonl(NWHO_MAGIC))
237 syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr));
241 for(e=first_host; e; e=e->next)
242 if (e->addr == sa.sin_addr.s_addr)
246 e = malloc(sizeof(struct hostrec));
249 syslog(LOG_ERR, "Out of memory");
252 e->next = first_host;
254 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
257 sprintf(e->name, "%.30s", h->h_name);
258 for(c=e->name; *c; c++)
264 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
265 && (*c < 'a' || *c > 'z')
266 && (*c < '0' || *c > '9')
271 strcpy(e->name, inet_ntoa(sa.sin_addr));
272 e->addr = sa.sin_addr.s_addr;
276 save_pkt(e->name, &pkt, r);
283 static char hostname[64];
286 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
287 die("gethostname: %m");
288 save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
296 if (last_local_scan + DEFAULT_SEND_TIME <= now)
298 last_local_scan = now;
301 for(e=first_host; e; e=e->next)
302 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
304 if (unlink(e->name) < 0)
305 syslog(LOG_ERR, "unlink(%s): %m", e->name);
315 die("Fork failed: %m");
322 open("/dev/null", O_RDONLY);
336 static struct sigaction sigact;
339 if (chdir(NWHO_SPOOL_DIR) < 0)
340 die("chdir(" NWHO_SPOOL_DIR "): %m");
343 bzero(&sigact, sizeof(sigact));
344 sigact.sa_handler = tick;
345 sigaction(SIGALRM, &sigact, NULL);
366 if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
367 syslog(LOG_ERR, "sendmsg: %m");
368 sleep(DEFAULT_SEND_TIME);
373 main(int argc, char **argv)
381 fprintf(stderr, "Usage: nwhod [<server>]\n");