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;
37 static char hostname[64];
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)))
62 die("Failed to resolve %s", name);
67 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
69 die("Cannot create socket: %m");
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)
75 die("Cannot bind to UDP port %d: %m", NWHO_PORT);
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)
81 die("Cannot connect socket: %m");
84 if (gethostname(hostname, sizeof(hostname)) < 0)
85 die("Unable to get my own host name: %m");
89 scan_utmp(struct nwho_pkt *p, time_t now)
96 char name[UT_NAMESIZE+10];
100 while ((u = getutent()) && cnt < MAX_USERS)
101 if (u->ut_type == USER_PROCESS && u->ut_user[0])
104 memcpy(name, u->ut_user, UT_NAMESIZE);
106 strcpy(h->name, name);
107 h->login_time = htonl(now - u->ut_time);
108 snprintf(h->con, sizeof(h->con), "%s", u->ut_line);
109 snprintf(device, sizeof(device), "/dev/%s", u->ut_line);
110 if (stat(device, &st) < 0)
112 h->mesg_y = !!(S_IWGRP & st.st_mode);
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)
179 int len = nwho_pkt_size(pkt);
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 */
204 struct sockaddr_in sa;
205 socklen_t al = sizeof(sa);
206 int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
211 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));
231 pkt.magic != htonl(NWHO_MAGIC) ||
232 ntohl(pkt.num_users) > MAX_USERS ||
233 r < nwho_pkt_size(&pkt))
235 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
239 for(e=first_host; e; e=e->next)
240 if (e->addr == sa.sin_addr.s_addr)
245 e = malloc(sizeof(struct hostrec));
248 syslog(LOG_ERR, "Out of memory");
251 e->next = first_host;
253 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
256 snprintf(e->name, sizeof(e->name), "%s", h->h_name);
257 for (c=e->name; *c; c++)
263 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
264 && (*c < 'a' || *c > 'z')
265 && (*c < '0' || *c > '9')
270 strcpy(e->name, inet_ntoa(sa.sin_addr));
271 e->addr = sa.sin_addr.s_addr;
275 save_pkt(e->name, &pkt);
284 save_pkt(hostname, &pkt);
292 if (last_local_scan + DEFAULT_SEND_TIME <= now)
294 last_local_scan = now;
297 for(e=first_host; e; e=e->next)
298 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
300 if (unlink(e->name) < 0)
301 syslog(LOG_ERR, "unlink(%s): %m", e->name);
311 die("Fork failed: %m");
318 open("/dev/null", O_RDONLY);
332 static struct sigaction sigact;
335 if (chdir(NWHO_SPOOL_DIR) < 0)
336 die("chdir(" NWHO_SPOOL_DIR "): %m");
339 bzero(&sigact, sizeof(sigact));
340 sigact.sa_handler = tick;
341 sigaction(SIGALRM, &sigact, NULL);
362 if (send(sock, &pkt, nwho_pkt_size(&pkt), 0) < 0)
363 syslog(LOG_ERR, "sendmsg: %m");
364 sleep(DEFAULT_SEND_TIME);
369 main(int argc, char **argv)
371 if (argc == 2 && !strcmp(argv[1], "--version"))
373 printf("nwho " STRINGIFY(VERSION) "\n");
383 fprintf(stderr, "Usage: nwhod [<server>]\n");