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