1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1983, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)fingerd.c 8.1 (Berkeley) 06/04/93"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <netinet/in.h> 21 #include <arpa/inet.h> 22 #include <errno.h> 23 24 #include <unistd.h> 25 #include <syslog.h> 26 #include <netdb.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <strings.h> 30 #include "pathnames.h" 31 32 void err __P((const char *, ...)); 33 34 int 35 main(argc, argv) 36 int argc; 37 char *argv[]; 38 { 39 register FILE *fp; 40 register int ch; 41 register char *lp; 42 struct hostent *hp; 43 struct sockaddr_in sin; 44 int p[2], logging, secure, sval; 45 #define ENTRIES 50 46 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 47 48 prog = _PATH_FINGER; 49 logging = secure = 0; 50 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 51 opterr = 0; 52 while ((ch = getopt(argc, argv, "slp:")) != EOF) 53 switch (ch) { 54 case 'l': 55 logging = 1; 56 break; 57 case 'p': 58 prog = optarg; 59 break; 60 case 's': 61 secure = 1; 62 break; 63 case '?': 64 default: 65 err("illegal option -- %c", ch); 66 } 67 68 if (logging) { 69 sval = sizeof(sin); 70 if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0) 71 err("getpeername: %s", strerror(errno)); 72 if (hp = gethostbyaddr((char *)&sin.sin_addr.s_addr, 73 sizeof(sin.sin_addr.s_addr), AF_INET)) 74 lp = hp->h_name; 75 else 76 lp = inet_ntoa(sin.sin_addr); 77 syslog(LOG_NOTICE, "query from %s", lp); 78 } 79 80 if (!fgets(line, sizeof(line), stdin)) 81 exit(1); 82 83 comp = &av[1]; 84 for (lp = line, ap = &av[2];;) { 85 *ap = strtok(lp, " \t\r\n"); 86 if (!*ap) { 87 if (secure && ap == &av[2]) { 88 puts("must provide username\r\n"); 89 exit(1); 90 } 91 break; 92 } 93 if (secure && strchr(*ap, '@')) { 94 puts("fowarding service denied\r\n"); 95 exit(1); 96 } 97 98 /* RFC742: "/[Ww]" == "-l" */ 99 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 100 av[1] = "-l"; 101 comp = &av[0]; 102 } 103 else if (++ap == av + ENTRIES) 104 break; 105 lp = NULL; 106 } 107 108 if (lp = strrchr(prog, '/')) 109 *comp = ++lp; 110 else 111 *comp = prog; 112 if (pipe(p) < 0) 113 err("pipe: %s", strerror(errno)); 114 115 switch(vfork()) { 116 case 0: 117 (void)close(p[0]); 118 if (p[1] != 1) { 119 (void)dup2(p[1], 1); 120 (void)close(p[1]); 121 } 122 execv(prog, comp); 123 err("execv: %s: %s", prog, strerror(errno)); 124 _exit(1); 125 case -1: 126 err("fork: %s", strerror(errno)); 127 } 128 (void)close(p[1]); 129 if (!(fp = fdopen(p[0], "r"))) 130 err("fdopen: %s", strerror(errno)); 131 while ((ch = getc(fp)) != EOF) { 132 if (ch == '\n') 133 putchar('\r'); 134 putchar(ch); 135 } 136 exit(0); 137 } 138 139 #if __STDC__ 140 #include <stdarg.h> 141 #else 142 #include <varargs.h> 143 #endif 144 145 void 146 #if __STDC__ 147 err(const char *fmt, ...) 148 #else 149 err(fmt, va_alist) 150 char *fmt; 151 va_dcl 152 #endif 153 { 154 va_list ap; 155 #if __STDC__ 156 va_start(ap, fmt); 157 #else 158 va_start(ap); 159 #endif 160 (void)vsyslog(LOG_ERR, fmt, ap); 161 va_end(ap); 162 exit(1); 163 /* NOTREACHED */ 164 } 165