fingerd.c revision 1.27
1/* $OpenBSD: fingerd.c,v 1.27 2002/06/02 01:27:15 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.27 2002/06/02 01:27:15 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() 70{ 71 syslog(LOG_ERR, 72 "usage: fingerd [-slumMpS] [-P filename]"); 73 exit(2); 74} 75 76 77int 78main(argc, argv) 79 int argc; 80 char *argv[]; 81{ 82 FILE *fp; 83 int ch, ac = 2; 84 int p[2], logging, secure, user_required, short_list; 85#define ENTRIES 50 86 char **comp, *prog; 87 char **ap, *av[ENTRIES + 1], line[8192], *lp, *hname; 88 char hostbuf[MAXHOSTNAMELEN]; 89 90 prog = _PATH_FINGER; 91 logging = secure = user_required = short_list = 0; 92 openlog("fingerd", LOG_PID, LOG_DAEMON); 93 opterr = 0; 94 while ((ch = getopt(argc, argv, "sluSmMpP:")) != -1) 95 switch (ch) { 96 case 'l': 97 logging = 1; 98 break; 99 case 'P': 100 prog = optarg; 101 break; 102 case 's': 103 secure = 1; 104 break; 105 case 'u': 106 user_required = 1; 107 break; 108 case 'S': 109 if (ac < ENTRIES) { 110 short_list = 1; 111 av[ac++] = "-s"; 112 } 113 break; 114 case 'm': 115 if (ac < ENTRIES) 116 av[ac++] = "-m"; 117 break; 118 case 'M': 119 if (ac < ENTRIES) 120 av[ac++] = "-M"; 121 break; 122 case 'p': 123 if (ac < ENTRIES) 124 av[ac++] = "-p"; 125 break; 126 default: 127 usage(); 128 } 129 130 if (logging) { 131 struct sockaddr_storage ss; 132 struct sockaddr *sa; 133 int sval; 134 135 sval = sizeof(ss); 136 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0) { 137 /* err("getpeername: %s", strerror(errno)); */ 138 exit(1); 139 } 140 sa = (struct sockaddr *)&ss; 141 if (getnameinfo(sa, sa->sa_len, hostbuf, sizeof(hostbuf), 142 NULL, 0, 0) != 0) { 143 strlcpy(hostbuf, "?", sizeof(hostbuf)); 144 } 145 hname = hostbuf; 146 } 147 148 if (fgets(line, sizeof(line), stdin) == NULL) { 149 if (logging) 150 syslog(LOG_NOTICE, "query from %s: %s", hname, 151 feof(stdin) ? "EOF" : strerror(errno)); 152 exit(1); 153 } 154 155 if (logging) 156 syslog(LOG_NOTICE, "query from %s: `%.*s'", hname, 157 (int)strcspn(line, "\r\n"), line); 158 159 /* 160 * Note: we assume that finger(1) will treat "--" as end of 161 * command args (ie: that it uses getopt(3)). 162 */ 163 av[ac++] = "--"; 164 comp = &av[1]; 165 for (lp = line, ap = &av[ac]; ac < ENTRIES;) { 166 if ((*ap = strtok(lp, " \t\r\n")) == NULL) 167 break; 168 lp = NULL; 169 if (secure && strchr(*ap, '@')) { 170 (void) puts("forwarding service denied\r"); 171 exit(1); 172 } 173 174 ch = strlen(*ap); 175 while ((*ap)[ch-1] == '@') 176 (*ap)[--ch] = '\0'; 177 if (**ap == '\0') 178 continue; 179 180 /* RFC1196: "/[Ww]" == "-l" */ 181 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 182 if (!short_list) { 183 av[1] = "-l"; 184 comp = &av[0]; 185 } 186 } else { 187 ap++; 188 ac++; 189 } 190 } 191 av[ENTRIES - 1] = NULL; 192 193 if ((lp = strrchr(prog, '/'))) 194 *comp = ++lp; 195 else 196 *comp = prog; 197 198 if (user_required) { 199 for (ap = comp + 1; strcmp("--", *(ap++)); ) 200 ; 201 if (*ap == NULL) { 202 (void) puts("must provide username\r"); 203 exit(1); 204 } 205 } 206 207 if (pipe(p) < 0) 208 err("pipe: %s", strerror(errno)); 209 210 switch (vfork()) { 211 case 0: 212 (void) close(p[0]); 213 if (p[1] != 1) { 214 (void) dup2(p[1], 1); 215 (void) close(p[1]); 216 } 217 execv(prog, comp); 218 err("execv: %s: %s", prog, strerror(errno)); 219 _exit(1); 220 case -1: 221 err("fork: %s", strerror(errno)); 222 } 223 (void) close(p[1]); 224 if (!(fp = fdopen(p[0], "r"))) 225 err("fdopen: %s", strerror(errno)); 226 while ((ch = getc(fp)) != EOF) { 227 if (ch == '\n') 228 putchar('\r'); 229 putchar(ch); 230 } 231 exit(0); 232} 233 234void 235err(const char *fmt, ...) 236{ 237 va_list ap; 238 239 va_start(ap, fmt); 240 (void) vsyslog(LOG_ERR, fmt, ap); 241 va_end(ap); 242 exit(1); 243 /* NOTREACHED */ 244} 245