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)))
61 die("Failed to resolve %s", name);
66 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
68 die("Cannot create socket: %m");
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)
74 die("Cannot bind to UDP port %d: %m", NWHO_PORT);
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)
80 die("Cannot connect socket: %m");
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);
112 h->idle_time = htonl(now - last);
115 p->num_users = htonl(cnt);
119 scan_load(struct nwho_pkt *p)
124 n = open("/proc/uptime", O_RDONLY);
127 if (read(n, buf, sizeof(buf)) <= 0)
130 if (!sscanf(buf, "%d", &i))
132 p->uptime = htonl(i);
134 n = open("/proc/loadavg", O_RDONLY);
137 if (read(n, buf, sizeof(buf)) <= 0)
140 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
142 p->avl[0] = htonl(j[0]*100 + j[1]);
143 p->avl[1] = htonl(j[2]*100 + j[3]);
144 p->avl[2] = htonl(j[4]*100 + j[5]);
148 make_pkt(struct nwho_pkt *pkt)
150 bzero(pkt, sizeof(pkt));
151 pkt->magic = htonl(NWHO_MAGIC);
152 pkt->local_time = htonl(now);
166 while (e = readdir(d))
167 if (e->d_name[0] != '.')
173 save_pkt(char *name, struct nwho_pkt *pkt, int len)
175 int fd = open(name, O_WRONLY | O_CREAT, 0666);
178 syslog(LOG_ERR, "open(%s): %m", name);
181 pkt->server_time = htonl(now);
182 if (write(fd, pkt, len) != len)
183 syslog(LOG_ERR, "write: %m");
188 static inline int /* Validation checks not implemented yet */
199 struct sockaddr_in sa;
200 socklen_t al = sizeof(sa);
201 int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
206 alarm(DEFAULT_PRUNE_TIME);
207 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
214 syslog(LOG_ERR, "recvfrom: %m");
219 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
221 syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
225 if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
227 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
231 if (pkt.magic != htonl(NWHO_MAGIC))
233 syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr));
237 for(e=first_host; e; e=e->next)
238 if (e->addr == sa.sin_addr.s_addr)
242 e = malloc(sizeof(struct hostrec));
245 syslog(LOG_ERR, "Out of memory");
248 e->next = first_host;
250 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
253 sprintf(e->name, "%.30s", h->h_name);
254 for(c=e->name; *c; c++)
260 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
261 && (*c < 'a' || *c > 'z')
262 && (*c < '0' || *c > '9')
267 strcpy(e->name, inet_ntoa(sa.sin_addr));
268 e->addr = sa.sin_addr.s_addr;
272 save_pkt(e->name, &pkt, r);
279 static char hostname[64];
282 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
283 die("gethostname: %m");
284 save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
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, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
363 syslog(LOG_ERR, "sendmsg: %m");
364 sleep(DEFAULT_SEND_TIME);
369 main(int argc, char **argv)
377 fprintf(stderr, "Usage: nwhod [<server>]\n");