X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=nwhod.c;h=878db6b3844247f7a0b24b8f9e2a64b5fe7ee8a5;hb=dc223bb691e024b60fc8aa8952a6ec31da43a5cc;hp=9b3cb7b4ff8082be3945fbd7591eb3bf5cc786f0;hpb=a1bc744bd23d3bf01f1ec4e7b8ac374201fc792a;p=nwho.git diff --git a/nwhod.c b/nwhod.c index 9b3cb7b..878db6b 100644 --- a/nwhod.c +++ b/nwhod.c @@ -1,16 +1,12 @@ /* - * The Remote User Information Daemon 1.8 + * The Remote User Information Daemon * - * (c) 1997 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU General Public License. See file COPYING in any of the GNU packages. + * (c) 1997--2010 Martin Mares */ #include #include #include -#include #include #include #include @@ -19,81 +15,63 @@ #include #include #include +#include #include #include #include -#include #include -#include "net.h" +#include "nwho.h" struct hostrec { struct hostrec *next; char name[32]; time_t last_rec; /* 0=down */ - __u32 addr; + u32 addr; }; static int sock, port; static struct hostrec *first_host; static time_t now, last_local_scan; - -static void die(char *msg, ...) __attribute__((noreturn)); - -static void -die(char *msg, ...) -{ - va_list args; - - va_start(args, msg); - fprintf(stderr, "nwhod: "); - vfprintf(stderr, msg, args); - fputc('\n', stderr); - exit(1); -} +static char hostname[64]; static void net_init(char *name) { struct hostent *h; struct sockaddr_in sa; - struct servent *se; - int one; if (name) { if (! (h = gethostbyname(name))) - die("%s: %m", name); + die("Failed to resolve %s", name); } else h = NULL; - if (! (se = getservbyname("nwho", "udp"))) - die("Unknown service `nwho/udp'"); - sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) - die("socket: %m"); - - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) - die("setsockopt: %m"); + die("Cannot create socket: %m"); sa.sin_family = AF_INET; - sa.sin_port = port = se->s_port; + sa.sin_port = port = htons(NWHO_PORT); sa.sin_addr.s_addr = INADDR_ANY; if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0) - die("bind: %m"); + die("Cannot bind to UDP port %d: %m", NWHO_PORT); if (h) { memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr)); if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0) - die("connect: %m"); + die("Cannot connect socket: %m"); } + + if (gethostname(hostname, sizeof(hostname)) < 0) + die("Unable to get my own host name: %m"); } static void -scan_utmp(struct rywho_pkt *p, time_t now) +scan_utmp(struct nwho_pkt *p, time_t now) { struct utmp *u; int cnt = 0; @@ -112,31 +90,12 @@ scan_utmp(struct rywho_pkt *p, time_t now) name[9] = 0; strcpy(h->name, name); h->login_time = htonl(now - u->ut_time); - if (u->ut_id[0]) - { - h->con[0] = u->ut_id[0]; - h->con[1] = u->ut_id[1]; - h->con[2] = 0; - } - else - { - char *z = u->ut_line; - if (!strncmp(z, "tty", 3)) - z += 3; - sprintf(h->con, "%.3s", z); - } - sprintf(device, "/dev/%s", u->ut_line); + snprintf(h->con, sizeof(h->con), "%s", u->ut_line); + snprintf(device, sizeof(device), "/dev/%s", u->ut_line); if (stat(device, &st) < 0) - { - fprintf(stderr, "stat(%s): %m", device); - continue; - } + continue; h->mesg_y = !!(S_IWGRP & st.st_mode); last = st.st_atime; - if (st.st_mtime > last) - last = st.st_mtime; - if (st.st_ctime > last) - last = st.st_ctime; if (now < last) last = now; h->idle_time = htonl(now - last); @@ -146,7 +105,7 @@ scan_utmp(struct rywho_pkt *p, time_t now) } static void -scan_load(struct rywho_pkt *p) +scan_load(struct nwho_pkt *p) { int n, i, j[6]; char buf[256]; @@ -175,9 +134,10 @@ scan_load(struct rywho_pkt *p) } static void -make_pkt(struct rywho_pkt *pkt) +make_pkt(struct nwho_pkt *pkt) { bzero(pkt, sizeof(pkt)); + pkt->magic = htonl(NWHO_MAGIC); pkt->local_time = htonl(now); scan_utmp(pkt, now); scan_load(pkt); @@ -199,8 +159,9 @@ cleanup(void) } static void -save_pkt(char *name, struct rywho_pkt *pkt, int len) +save_pkt(char *name, struct nwho_pkt *pkt) { + int len = nwho_pkt_size(pkt); int fd = open(name, O_WRONLY | O_CREAT, 0666); if (fd < 0) { @@ -215,7 +176,7 @@ save_pkt(char *name, struct rywho_pkt *pkt, int len) } static inline int /* Validation checks not implemented yet */ -is_valid(__u32 ipa) +is_valid(u32 ipa) { return 1; } @@ -223,16 +184,16 @@ is_valid(__u32 ipa) static void receive(void) { - int r, al; - struct rywho_pkt pkt; + int r; + struct nwho_pkt pkt; struct sockaddr_in sa; - int n = sizeof(struct rywho_pkt) - MAX_USERS * sizeof(struct userinfo); + socklen_t al = sizeof(sa); + int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo); struct hostrec *e; struct hostent *h; char *c; alarm(DEFAULT_PRUNE_TIME); - al = sizeof(sa); r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al); alarm(0); now = time(NULL); @@ -247,11 +208,14 @@ receive(void) if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port) { - syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port)); + syslog(LOG_WARNING, "Received packet from invalid source %s:%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port)); return; } - if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo)) + if (r < n || + pkt.magic != htonl(NWHO_MAGIC) || + ntohl(pkt.num_users) > MAX_USERS || + r < nwho_pkt_size(&pkt)) { syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr)); return; @@ -260,6 +224,7 @@ receive(void) for(e=first_host; e; e=e->next) if (e->addr == sa.sin_addr.s_addr) break; + if (!e) { e = malloc(sizeof(struct hostrec)); @@ -273,8 +238,8 @@ receive(void) h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET); if (h) { - sprintf(e->name, "%.30s", h->h_name); - for(c=e->name; *c; c++) + snprintf(e->name, sizeof(e->name), "%s", h->h_name); + for (c=e->name; *c; c++) if (*c == '.') { *c = 0; @@ -292,19 +257,16 @@ receive(void) } e->last_rec = now; - save_pkt(e->name, &pkt, r); + save_pkt(e->name, &pkt); } static void local_scan(void) { - struct rywho_pkt pkt; - static char hostname[64]; + struct nwho_pkt pkt; make_pkt(&pkt); - if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0) - die("gethostname: %m"); - save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo)); + save_pkt(hostname, &pkt); } static void @@ -326,6 +288,24 @@ do_tick(void) } } +static void +daemonize(void) +{ + pid_t pid = fork(); + if (pid < 0) + die("Fork failed: %m"); + if (pid) + exit(0); + + close(0); + close(1); + close(2); + open("/dev/null", O_RDONLY); + dup(0); + dup(0); + setsid(); +} + static void tick(int unused) { @@ -337,9 +317,10 @@ server(void) static struct sigaction sigact; net_init(NULL); - if (chdir(YWHO_SPOOL_DIR) < 0) - die("chdir(" YWHO_SPOOL_DIR "): %m"); + if (chdir(NWHO_SPOOL_DIR) < 0) + die("chdir(" NWHO_SPOOL_DIR "): %m"); cleanup(); + daemonize(); bzero(&sigact, sizeof(sigact)); sigact.sa_handler = tick; sigaction(SIGALRM, &sigact, NULL); @@ -354,15 +335,16 @@ server(void) static void client(char *serv) { - struct rywho_pkt pkt; + struct nwho_pkt pkt; net_init(serv); utmpname(UTMP_FILE); + daemonize(); for(;;) { now = time(NULL); make_pkt(&pkt); - if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0) + if (send(sock, &pkt, nwho_pkt_size(&pkt), 0) < 0) syslog(LOG_ERR, "sendmsg: %m"); sleep(DEFAULT_SEND_TIME); } @@ -371,24 +353,20 @@ client(char *serv) int main(int argc, char **argv) { - int pid; - - if (argc != 1 && argc != 2) - { - fprintf(stderr, "Usage: nwhod []\n"); - return 1; - } - pid = fork(); - if (pid < 0) + if (argc == 2 && !strcmp(argv[1], "--version")) { - perror("fork"); - return 1; + printf("nwho " STRINGIFY(VERSION) "\n"); + return 0; } - if (pid) - return 0; + if (argc == 2) client(argv[1]); else if (argc == 1) server(); - return 1; + else + { + fprintf(stderr, "Usage: nwhod []\n"); + return 1; + } + return 0; }