fingerd.c revision 31308
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 const 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 41#if 0 42static char sccsid[] = "@(#)fingerd.c 8.1 (Berkeley) 6/4/93"; 43#endif 44static const char rcsid[] = 45 "$Id$"; 46#endif /* not lint */ 47 48#include <sys/types.h> 49#include <sys/socket.h> 50#include <netinet/in.h> 51#include <netinet/tcp.h> 52#include <arpa/inet.h> 53#include <errno.h> 54 55#include <unistd.h> 56#include <syslog.h> 57#include <netdb.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <strings.h> 61#include "pathnames.h" 62 63void logerr __P((const char *, ...)); 64 65int 66main(argc, argv) 67 int argc; 68 char *argv[]; 69{ 70 register FILE *fp; 71 register int ch; 72 register char *lp; 73 struct hostent *hp; 74 struct sockaddr_in sin; 75 int p[2], logging, secure, sval; 76#define ENTRIES 50 77 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 78 79 prog = _PATH_FINGER; 80 logging = secure = 0; 81 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 82 opterr = 0; 83 while ((ch = getopt(argc, argv, "slp:")) != -1) 84 switch (ch) { 85 case 'l': 86 logging = 1; 87 break; 88 case 'p': 89 prog = optarg; 90 break; 91 case 's': 92 secure = 1; 93 break; 94 case '?': 95 default: 96 logerr("illegal option -- %c", ch); 97 } 98 99 /* 100 * Enable server-side Transaction TCP. 101 */ 102 { 103 int one = 1; 104 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 105 sizeof one) < 0) { 106 logerr("setsockopt(TCP_NOPUSH) failed: %m"); 107 } 108 } 109 110 if (!fgets(line, sizeof(line), stdin)) 111 exit(1); 112 113 if (logging) { 114 char *t; 115 char *end; 116 117 end = memchr(line, 0, sizeof(line)); 118 if (end == NULL) { 119 t = malloc(sizeof(line) + 1); 120 memcpy(t, line, sizeof(line)); 121 t[sizeof(line)] = 0; 122 } else { 123 t = strdup(line); 124 } 125 for (end = t; *end; end++) 126 if (*end == '\n' || *end == '\r') 127 *end = ' '; 128 sval = sizeof(sin); 129 if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0) 130 logerr("getpeername: %s", strerror(errno)); 131 if (hp = gethostbyaddr((char *)&sin.sin_addr.s_addr, 132 sizeof(sin.sin_addr.s_addr), AF_INET)) 133 lp = hp->h_name; 134 else 135 lp = inet_ntoa(sin.sin_addr); 136 syslog(LOG_NOTICE, "query from %s: `%s'", lp, t); 137 } 138 139 comp = &av[1]; 140 av[2] = "--"; 141 for (lp = line, ap = &av[3];;) { 142 *ap = strtok(lp, " \t\r\n"); 143 if (!*ap) { 144 if (secure && ap == &av[3]) { 145 puts("must provide username\r\n"); 146 exit(1); 147 } 148 break; 149 } 150 if (secure && strchr(*ap, '@')) { 151 puts("forwarding service denied\r\n"); 152 exit(1); 153 } 154 155 /* RFC742: "/[Ww]" == "-l" */ 156 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 157 av[1] = "-l"; 158 comp = &av[0]; 159 } 160 else if (++ap == av + ENTRIES) 161 break; 162 lp = NULL; 163 } 164 165 if (lp = strrchr(prog, '/')) 166 *comp = ++lp; 167 else 168 *comp = prog; 169 if (pipe(p) < 0) 170 logerr("pipe: %s", strerror(errno)); 171 172 switch(vfork()) { 173 case 0: 174 (void)close(p[0]); 175 if (p[1] != 1) { 176 (void)dup2(p[1], 1); 177 (void)close(p[1]); 178 } 179 execv(prog, comp); 180 logerr("execv: %s: %s", prog, strerror(errno)); 181 _exit(1); 182 case -1: 183 logerr("fork: %s", strerror(errno)); 184 } 185 (void)close(p[1]); 186 if (!(fp = fdopen(p[0], "r"))) 187 logerr("fdopen: %s", strerror(errno)); 188 while ((ch = getc(fp)) != EOF) { 189 if (ch == '\n') 190 putchar('\r'); 191 putchar(ch); 192 } 193 exit(0); 194} 195 196#if __STDC__ 197#include <stdarg.h> 198#else 199#include <varargs.h> 200#endif 201 202void 203#if __STDC__ 204logerr(const char *fmt, ...) 205#else 206logerr(fmt, va_alist) 207 char *fmt; 208 va_dcl 209#endif 210{ 211 va_list ap; 212#if __STDC__ 213 va_start(ap, fmt); 214#else 215 va_start(ap); 216#endif 217 (void)vsyslog(LOG_ERR, fmt, ap); 218 va_end(ap); 219 exit(1); 220 /* NOTREACHED */ 221} 222