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 snprintf(h->con, sizeof(h->con), "%s", u->ut_line);
105 snprintf(device, sizeof(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)
175 int len = sizeof(pkt) - (MAX_USERS - ntohl(pkt->num_users))*sizeof(struct userinfo);
176 int fd = open(name, O_WRONLY | O_CREAT, 0666);
179 syslog(LOG_ERR, "open(%s): %m", name);
182 pkt->server_time = htonl(now);
183 if (write(fd, pkt, len) != len)
184 syslog(LOG_ERR, "write: %m");
189 static inline int /* Validation checks not implemented yet */
200 struct sockaddr_in sa;
201 socklen_t al = sizeof(sa);
202 int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
207 alarm(DEFAULT_PRUNE_TIME);
208 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
215 syslog(LOG_ERR, "recvfrom: %m");
220 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
222 syslog(LOG_WARNING, "Received packet from invalid source %s:%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
227 pkt.magic != htonl(NWHO_MAGIC) ||
228 ntohl(pkt.num_users) > MAX_USERS ||
229 r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
231 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
235 for(e=first_host; e; e=e->next)
236 if (e->addr == sa.sin_addr.s_addr)
241 e = malloc(sizeof(struct hostrec));
244 syslog(LOG_ERR, "Out of memory");
247 e->next = first_host;
249 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
252 snprintf(e->name, sizeof(e->name), "%s", h->h_name);
253 for (c=e->name; *c; c++)
259 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
260 && (*c < 'a' || *c > 'z')
261 && (*c < '0' || *c > '9')
266 strcpy(e->name, inet_ntoa(sa.sin_addr));
267 e->addr = sa.sin_addr.s_addr;
271 save_pkt(e->name, &pkt);
278 static char hostname[64];
281 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
282 die("gethostname: %m");
283 save_pkt(hostname, &pkt);
291 if (last_local_scan + DEFAULT_SEND_TIME <= now)
293 last_local_scan = now;
296 for(e=first_host; e; e=e->next)
297 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
299 if (unlink(e->name) < 0)
300 syslog(LOG_ERR, "unlink(%s): %m", e->name);
310 die("Fork failed: %m");
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);
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)
376 fprintf(stderr, "Usage: nwhod [<server>]\n");