ref: c16300e164508685bfdf69b858f02a1b224926b9
dir: /fingerd.c/
#include <u.h> #include <libc.h> #include <bio.h> #define BUFSIZ 8192 char *usernotfound = "/lib/finger/usernotfound"; char *filenotfound = "/lib/finger/filenotfound"; char *motd = "/lib/finger/motd"; char *req; int puserlist = 1; int vaccess(char *file) { int fd; fd = open(file, OREAD); if (fd < 0){ return 0; } close(fd); return -1; } int cat(char *file, int perr) { int n, fd; char buf[BUFSIZ]; fd = open(file, OREAD); if (fd < 0){ if (perr) print("File %s not Found.\n", file); return 0; } while ((n = read(fd, buf, BUFSIZ)) > 0) if (write(1, buf, n) != n) print("Error writing data!\n"); close(fd); return 1; } char* splitpath(char *req) { char *file; file = strrchr(req, '/'); if (file == nil || strlen(file) == 0) return "finger"; file[0] = 0; return file+1; } void printuserinfo(char *user) { print("%s\n", user); } void printuserlist(void) { Dir *dirbuf; char *path; int fd, n, i; fd = open("/usr", OREAD); if (fd < 0){ print("Can't list users.\n"); return; } n = dirreadall(fd, &dirbuf); close(fd); if (n) print("User list:\n"); else print("No users.\n"); for (i = 0; i < n; i++){ path = smprint("/usr/%s/finger/finger", dirbuf[i].name); if (vaccess(path)){ free(path); printuserinfo(dirbuf[i].name); } } } void printnouser(char *user) { if (!cat(usernotfound, 0)) print("User %s not found or private.\n", user); } void printnofile(char *file) { if (!cat(usernotfound, 0)) print("File %s not found.\n", file); } void main(int argc, char **argv) { int n; Biobuf *bin; char *user, *file, *path; ARGBEGIN{ case 'l': puserlist = 0; break; } ARGEND bin = Bfdopen(0, OREAD); req = Brdline(bin, '\n'); if (req == nil) sysfatal("bad read: %r"); n = Blinelen(bin); req[n-1] = 0; switch(n){ case 1: /* server request */ if (!cat(motd, 0)) print("motd not found.\n"); if (puserlist) printuserlist(); break; default: /* user request */ file = splitpath(req); user = req; path = smprint("/usr/%s/finger/finger", user); if (!vaccess(path)){ printnouser(user); break; } free(path); path = smprint("/usr/%s/finger/%s", user, file); if (!cat(path, OREAD)) printnofile(path); free(path); } }