2 * The Remote User Information Daemon
4 * (c) 1997--2002 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License. See file COPYING in any of the GNU packages.
16 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
26 #include <asm/types.h>
34 time_t last_rec; /* 0=down */
38 static int sock, port;
39 static struct hostrec *first_host;
40 static time_t now, last_local_scan;
42 static void die(char *msg, ...) __attribute__((noreturn));
50 fprintf(stderr, "nwhod: ");
51 vfprintf(stderr, msg, args);
60 struct sockaddr_in sa;
64 if (! (h = gethostbyname(name)))
70 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
74 sa.sin_family = AF_INET;
75 sa.sin_port = port = htons(NWHO_PORT);
76 sa.sin_addr.s_addr = INADDR_ANY;
77 if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
82 memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
83 if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
89 scan_utmp(struct rywho_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 sprintf(h->con, "%.7s", u->ut_line);
109 sprintf(device, "/dev/%s", u->ut_line);
110 if (stat(device, &st) < 0)
112 h->mesg_y = !!(S_IWGRP & st.st_mode);
114 if (st.st_mtime > last)
116 if (st.st_ctime > last)
120 h->idle_time = htonl(now - last);
123 p->num_users = htonl(cnt);
127 scan_load(struct rywho_pkt *p)
132 n = open("/proc/uptime", O_RDONLY);
135 if (read(n, buf, sizeof(buf)) <= 0)
138 if (!sscanf(buf, "%d", &i))
140 p->uptime = htonl(i);
142 n = open("/proc/loadavg", O_RDONLY);
145 if (read(n, buf, sizeof(buf)) <= 0)
148 if (sscanf(buf, "%d.%d%d.%d%d.%d", &j[0], &j[1], &j[2], &j[3], &j[4], &j[5]) != 6)
150 p->avl[0] = htonl(j[0]*100 + j[1]);
151 p->avl[1] = htonl(j[2]*100 + j[3]);
152 p->avl[2] = htonl(j[4]*100 + j[5]);
156 make_pkt(struct rywho_pkt *pkt)
158 bzero(pkt, sizeof(pkt));
159 pkt->magic = htonl(NWHO_MAGIC);
160 pkt->local_time = htonl(now);
174 while (e = readdir(d))
175 if (e->d_name[0] != '.')
181 save_pkt(char *name, struct rywho_pkt *pkt, int len)
183 int fd = open(name, O_WRONLY | O_CREAT, 0666);
186 syslog(LOG_ERR, "open(%s): %m", name);
189 pkt->server_time = htonl(now);
190 if (write(fd, pkt, len) != len)
191 syslog(LOG_ERR, "write: %m");
196 static inline int /* Validation checks not implemented yet */
206 struct rywho_pkt pkt;
207 struct sockaddr_in sa;
208 int n = sizeof(struct rywho_pkt) - MAX_USERS * sizeof(struct userinfo);
213 alarm(DEFAULT_PRUNE_TIME);
215 r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
222 syslog(LOG_ERR, "recvfrom: %m");
227 if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port)
229 syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
233 if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
235 syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
239 if (pkt.magic != htonl(NWHO_MAGIC))
241 syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr));
245 for(e=first_host; e; e=e->next)
246 if (e->addr == sa.sin_addr.s_addr)
250 e = malloc(sizeof(struct hostrec));
253 syslog(LOG_ERR, "Out of memory");
256 e->next = first_host;
258 h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
261 sprintf(e->name, "%.30s", h->h_name);
262 for(c=e->name; *c; c++)
268 else if ((*c < 'A' || *c > 'Z') /* Filter out malicious characters */
269 && (*c < 'a' || *c > 'z')
270 && (*c < '0' || *c > '9')
275 strcpy(e->name, inet_ntoa(sa.sin_addr));
276 e->addr = sa.sin_addr.s_addr;
280 save_pkt(e->name, &pkt, r);
286 struct rywho_pkt pkt;
287 static char hostname[64];
290 if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
291 die("gethostname: %m");
292 save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
300 if (last_local_scan + DEFAULT_SEND_TIME <= now)
302 last_local_scan = now;
305 for(e=first_host; e; e=e->next)
306 if (e->last_rec && e->last_rec + DEFAULT_DEAD_TIME < now)
308 if (unlink(e->name) < 0)
309 syslog(LOG_ERR, "unlink(%s): %m", e->name);
320 open("/dev/null", O_RDONLY);
334 static struct sigaction sigact;
337 if (chdir(NWHO_SPOOL_DIR) < 0)
338 die("chdir(" NWHO_SPOOL_DIR "): %m");
341 bzero(&sigact, sizeof(sigact));
342 sigact.sa_handler = tick;
343 sigaction(SIGALRM, &sigact, NULL);
355 struct rywho_pkt pkt;
364 if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
365 syslog(LOG_ERR, "sendmsg: %m");
366 sleep(DEFAULT_SEND_TIME);
371 main(int argc, char **argv)
375 if (argc != 1 && argc != 2)
377 fprintf(stderr, "Usage: nwhod [<server>]\n");