2 * The Remote User Information Daemon
4 * (c) 1997--2010 Martin Mares <mj@ucw.cz>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
29 time_t last_rec; /* 0=down */
33 static int sock, port;
34 static struct hostrec *first_host;
35 static time_t now, last_local_scan;
36 static char hostname[64];
42 struct sockaddr_in sa;
46 if (! (h = gethostbyname(name)))
47 die("Failed to resolve %s", name);
52 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
54 die("Cannot create socket: %m");
56 sa.sin_family = AF_INET;
57 sa.sin_port = port = htons(NWHO_PORT);
58 sa.sin_addr.s_addr = INADDR_ANY;
59 if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
60 die("Cannot bind to UDP port %d: %m", NWHO_PORT);
64 memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
65 if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
66 die("Cannot connect socket: %m");
69 if (gethostname(hostname, sizeof(hostname)) < 0)
70 die("Unable to get my own host name: %m");
74 scan_utmp(struct nwho_pkt *p, time_t now)
81 char name[UT_NAMESIZE+10];
85 while ((u = getutent()) && cnt < MAX_USERS)
86 if (u->ut_type == USER_PROCESS && u->ut_user[0])
89 memcpy(name, u->ut_user, UT_NAMESIZE);
91 strcpy(h->name, name);
92 h->login_time = htonl(now - u->ut_time);
93 snprintf(h->con, sizeof(h->con), "%s", u->ut_line);
94 snprintf(device, sizeof(device), "/dev/%s", u->ut_line);
95 if (stat(device, &st) < 0)
97 h->mesg_y = !!(S_IWGRP & st.st_mode);
101 h->idle_time = htonl(now - last);
104 p->num_users = htonl(cnt);
108 scan_load(struct nwho_pkt *p)
113 n = open("/proc/uptime", O_RDONLY);
116 if (read(n, buf, sizeof(buf)) <= 0)
119 if (!sscanf(buf, "%d", &i))
121 p->uptime = htonl(i);
123 n = open("/proc/loadavg", O_RDONLY);
126 if (read(n, buf, sizeof(buf)) <= 0)
129 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
131 p->avl[0] = htonl(j[0]*100 + j[1]);
132 p->avl[1] = htonl(j[2]*100 + j[3]);
133 p->avl[2] = htonl(j[4]*100 + j[5]);
137 make_pkt(struct nwho_pkt *pkt)
139 bzero(pkt, sizeof(pkt));
140 pkt->magic = htonl(NWHO_MAGIC);
141 pkt->local_time = htonl(now);
155 while (e = readdir(d))
156 if (e->d_name[0] != '.')
162 save_pkt(char *name, struct nwho_pkt *pkt)
164 int len = nwho_pkt_size(pkt);
165 int fd = open(name, O_WRONLY | O_CREAT, 0666);
168 syslog(LOG_ERR, "open(%s): %m", name);
171 pkt->server_time = htonl(now);
172 if (write(fd, pkt, len) != len)
173 syslog(LOG_ERR, "write: %m");
178 static inline int /* Validation checks not implemented yet */
189 struct sockaddr_in sa;
190 socklen_t al = sizeof(sa);
191 int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
196 alarm(DEFAULT_PRUNE_TIME);
197 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
204 syslog(LOG_ERR, "recvfrom: %m");
209 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
211 syslog(LOG_WARNING, "Received packet from invalid source %s:%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
216 pkt.magic != htonl(NWHO_MAGIC) ||
217 ntohl(pkt.num_users) > MAX_USERS ||
218 r < nwho_pkt_size(&pkt))
220 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
224 for(e=first_host; e; e=e->next)
225 if (e->addr == sa.sin_addr.s_addr)
230 e = malloc(sizeof(struct hostrec));
233 syslog(LOG_ERR, "Out of memory");
236 e->next = first_host;
238 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
241 snprintf(e->name, sizeof(e->name), "%s", h->h_name);
242 for (c=e->name; *c; c++)
248 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
249 && (*c < 'a' || *c > 'z')
250 && (*c < '0' || *c > '9')
255 strcpy(e->name, inet_ntoa(sa.sin_addr));
256 e->addr = sa.sin_addr.s_addr;
260 save_pkt(e->name, &pkt);
269 save_pkt(hostname, &pkt);
277 if (last_local_scan + DEFAULT_SEND_TIME <= now)
279 last_local_scan = now;
282 for(e=first_host; e; e=e->next)
283 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
285 if (unlink(e->name) < 0)
286 syslog(LOG_ERR, "unlink(%s): %m", e->name);
296 die("Fork failed: %m");
303 open("/dev/null", O_RDONLY);
317 static struct sigaction sigact;
320 if (chdir(NWHO_SPOOL_DIR) < 0)
321 die("chdir(" NWHO_SPOOL_DIR "): %m");
324 bzero(&sigact, sizeof(sigact));
325 sigact.sa_handler = tick;
326 sigaction(SIGALRM, &sigact, NULL);
347 if (send(sock, &pkt, nwho_pkt_size(&pkt), 0) < 0)
348 syslog(LOG_ERR, "sendmsg: %m");
349 sleep(DEFAULT_SEND_TIME);
354 main(int argc, char **argv)
356 if (argc == 2 && !strcmp(argv[1], "--version"))
358 printf("nwho " STRINGIFY(VERSION) "\n");
368 fprintf(stderr, "Usage: nwhod [<server>]\n");