fingerd.c revision 12728
1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static char copyright[] = 36"@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)fingerd.c 8.1 (Berkeley) 6/4/93"; 42#endif /* not lint */ 43 44#include <sys/types.h> 45#include <sys/socket.h> 46#include <netinet/in.h> 47#include <arpa/inet.h> 48#include <errno.h> 49 50#include <unistd.h> 51#include <syslog.h> 52#include <netdb.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <strings.h> 56#include "pathnames.h" 57 58void err __P((const char *, ...)); 59 60int 61main(argc, argv) 62 int argc; 63 char *argv[]; 64{ 65 register FILE *fp; 66 register int ch; 67 register char *lp; 68 struct hostent *hp; 69 struct sockaddr_in sin; 70 int p[2], logging, secure, sval; 71#define ENTRIES 50 72 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 73 74 prog = _PATH_FINGER; 75 logging = secure = 0; 76 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 77 opterr = 0; 78 while ((ch = getopt(argc, argv, "slp:")) != EOF) 79 switch (ch) { 80 case 'l': 81 logging = 1; 82 break; 83 case 'p': 84 prog = optarg; 85 break; 86 case 's': 87 secure = 1; 88 break; 89 case '?': 90 default: 91 err("illegal option -- %c", ch); 92 } 93 94 if (logging) { 95 sval = sizeof(sin); 96 if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0) 97 err("getpeername: %s", strerror(errno)); 98 if (hp = gethostbyaddr((char *)&sin.sin_addr.s_addr, 99 sizeof(sin.sin_addr.s_addr), AF_INET)) 100 lp = hp->h_name; 101 else 102 lp = inet_ntoa(sin.sin_addr); 103 syslog(LOG_NOTICE, "query from %s", lp); 104 } 105 106 if (!fgets(line, sizeof(line), stdin)) 107 exit(1); 108 109 comp = &av[1]; 110 av[2] = "--"; 111 for (lp = line, ap = &av[3];;) { 112 *ap = strtok(lp, " \t\r\n"); 113 if (!*ap) { 114 if (secure && ap == &av[3]) { 115 puts("must provide username\r\n"); 116 exit(1); 117 } 118 break; 119 } 120 if (secure && strchr(*ap, '@')) { 121 puts("forwarding service denied\r\n"); 122 exit(1); 123 } 124 125 /* RFC742: "/[Ww]" == "-l" */ 126 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 127 av[1] = "-l"; 128 comp = &av[0]; 129 } 130 else if (++ap == av + ENTRIES) 131 break; 132 lp = NULL; 133 } 134 135 if (lp = strrchr(prog, '/')) 136 *comp = ++lp; 137 else 138 *comp = prog; 139 if (pipe(p) < 0) 140 err("pipe: %s", strerror(errno)); 141 142 switch(vfork()) { 143 case 0: 144 (void)close(p[0]); 145 if (p[1] != 1) { 146 (void)dup2(p[1], 1); 147 (void)close(p[1]); 148 } 149 execv(prog, comp); 150 err("execv: %s: %s", prog, strerror(errno)); 151 _exit(1); 152 case -1: 153 err("fork: %s", strerror(errno)); 154 } 155 (void)close(p[1]); 156 if (!(fp = fdopen(p[0], "r"))) 157 err("fdopen: %s", strerror(errno)); 158 while ((ch = getc(fp)) != EOF) { 159 if (ch == '\n') 160 putchar('\r'); 161 putchar(ch); 162 } 163 exit(0); 164} 165 166#if __STDC__ 167#include <stdarg.h> 168#else 169#include <varargs.h> 170#endif 171 172void 173#if __STDC__ 174err(const char *fmt, ...) 175#else 176err(fmt, va_alist) 177 char *fmt; 178 va_dcl 179#endif 180{ 181 va_list ap; 182#if __STDC__ 183 va_start(ap, fmt); 184#else 185 va_start(ap); 186#endif 187 (void)vsyslog(LOG_ERR, fmt, ap); 188 va_end(ap); 189 exit(1); 190 /* NOTREACHED */ 191} 192