fingerd.c revision 1.29
1/* $OpenBSD: fingerd.c,v 1.29 2002/09/06 19:43:54 deraadt Exp $ */ 2 3/* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#ifndef lint 37static char copyright[] = 38"@(#) Copyright (c) 1983, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43#if 0 44static char sccsid[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93"; 45#else 46static char rcsid[] = "$OpenBSD: fingerd.c,v 1.29 2002/09/06 19:43:54 deraadt Exp $"; 47#endif 48#endif /* not lint */ 49 50#include <sys/types.h> 51#include <sys/socket.h> 52#include <netinet/in.h> 53#include <arpa/inet.h> 54#include <errno.h> 55 56#include <unistd.h> 57#include <syslog.h> 58#include <netdb.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <stdarg.h> 63#include "pathnames.h" 64 65void err(const char *, ...); 66void usage(void); 67 68void 69usage(void) 70{ 71 syslog(LOG_ERR, 72 "usage: fingerd [-slumMpS] [-P filename]"); 73 exit(2); 74} 75 76 77int 78main(int argc, char *argv[]) 79{ 80 FILE *fp; 81 int ch, ac = 2; 82 int p[2], logging, secure, user_required, short_list; 83#define ENTRIES 50 84 char **comp, *prog; 85 char **ap, *av[ENTRIES + 1], line[8192], *lp, *hname; 86 char hostbuf[MAXHOSTNAMELEN]; 87 88 prog = _PATH_FINGER; 89 logging = secure = user_required = short_list = 0; 90 openlog("fingerd", LOG_PID, LOG_DAEMON); 91 opterr = 0; 92 while ((ch = getopt(argc, argv, "sluSmMpP:")) != -1) 93 switch (ch) { 94 case 'l': 95 logging = 1; 96 break; 97 case 'P': 98 prog = optarg; 99 break; 100 case 's': 101 secure = 1; 102 break; 103 case 'u': 104 user_required = 1; 105 break; 106 case 'S': 107 if (ac < ENTRIES) { 108 short_list = 1; 109 av[ac++] = "-s"; 110 } 111 break; 112 case 'm': 113 if (ac < ENTRIES) 114 av[ac++] = "-m"; 115 break; 116 case 'M': 117 if (ac < ENTRIES) 118 av[ac++] = "-M"; 119 break; 120 case 'p': 121 if (ac < ENTRIES) 122 av[ac++] = "-p"; 123 break; 124 default: 125 usage(); 126 } 127 128 if (logging) { 129 struct sockaddr_storage ss; 130 struct sockaddr *sa; 131 socklen_t sval; 132 133 sval = sizeof(ss); 134 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0) { 135 /* err("getpeername: %s", strerror(errno)); */ 136 exit(1); 137 } 138 sa = (struct sockaddr *)&ss; 139 if (getnameinfo(sa, sa->sa_len, hostbuf, sizeof(hostbuf), 140 NULL, 0, 0) != 0) { 141 strlcpy(hostbuf, "?", sizeof(hostbuf)); 142 } 143 hname = hostbuf; 144 } 145 146 if (fgets(line, sizeof(line), stdin) == NULL) { 147 if (logging) 148 syslog(LOG_NOTICE, "query from %s: %s", hname, 149 feof(stdin) ? "EOF" : strerror(errno)); 150 exit(1); 151 } 152 153 if (logging) 154 syslog(LOG_NOTICE, "query from %s: `%.*s'", hname, 155 (int)strcspn(line, "\r\n"), line); 156 157 /* 158 * Note: we assume that finger(1) will treat "--" as end of 159 * command args (ie: that it uses getopt(3)). 160 */ 161 av[ac++] = "--"; 162 comp = &av[1]; 163 for (lp = line, ap = &av[ac]; ac < ENTRIES;) { 164 if ((*ap = strtok(lp, " \t\r\n")) == NULL) 165 break; 166 lp = NULL; 167 if (secure && strchr(*ap, '@')) { 168 (void) puts("forwarding service denied\r"); 169 exit(1); 170 } 171 172 ch = strlen(*ap); 173 while ((*ap)[ch-1] == '@') 174 (*ap)[--ch] = '\0'; 175 if (**ap == '\0') 176 continue; 177 178 /* RFC1196: "/[Ww]" == "-l" */ 179 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 180 if (!short_list) { 181 av[1] = "-l"; 182 comp = &av[0]; 183 } 184 } else { 185 ap++; 186 ac++; 187 } 188 } 189 av[ENTRIES - 1] = NULL; 190 191 if ((lp = strrchr(prog, '/'))) 192 *comp = ++lp; 193 else 194 *comp = prog; 195 196 if (user_required) { 197 for (ap = comp + 1; strcmp("--", *(ap++)); ) 198 ; 199 if (*ap == NULL) { 200 (void) puts("must provide username\r"); 201 exit(1); 202 } 203 } 204 205 if (pipe(p) < 0) 206 err("pipe: %s", strerror(errno)); 207 208 switch (vfork()) { 209 case 0: 210 (void) close(p[0]); 211 if (p[1] != 1) { 212 (void) dup2(p[1], 1); 213 (void) close(p[1]); 214 } 215 execv(prog, comp); 216 err("execv: %s: %s", prog, strerror(errno)); 217 _exit(1); 218 case -1: 219 err("fork: %s", strerror(errno)); 220 } 221 (void) close(p[1]); 222 if (!(fp = fdopen(p[0], "r"))) 223 err("fdopen: %s", strerror(errno)); 224 while ((ch = getc(fp)) != EOF) { 225 if (ch == '\n') 226 putchar('\r'); 227 putchar(ch); 228 } 229 exit(0); 230} 231 232void 233err(const char *fmt, ...) 234{ 235 va_list ap; 236 237 va_start(ap, fmt); 238 (void) vsyslog(LOG_ERR, fmt, ap); 239 va_end(ap); 240 exit(1); 241 /* NOTREACHED */ 242} 243