fingerd.c revision 1.9
1/* $OpenBSD: fingerd.c,v 1.9 1997/08/16 20:34:20 millert 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.9 1997/08/16 20:34:20 millert 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 <strings.h> 62#include "pathnames.h" 63 64void err __P((const char *, ...)); 65 66int 67main(argc, argv) 68 int argc; 69 char *argv[]; 70{ 71 register FILE *fp; 72 register int ch, ac = 2; 73 register char *lp; 74 struct hostent *hp; 75 struct sockaddr_in sin; 76 int p[2], logging, secure, user_required, short_list, sval; 77#define ENTRIES 50 78 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 79 80 prog = _PATH_FINGER; 81 logging = secure = user_required = short_list = 0; 82 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 83 opterr = 0; 84 while ((ch = getopt(argc, argv, "sluSmMpP:")) != -1) 85 switch (ch) { 86 case 'l': 87 logging = 1; 88 break; 89 case 'P': 90 prog = optarg; 91 break; 92 case 's': 93 secure = 1; 94 break; 95 case 'u': 96 user_required = 1; 97 break; 98 case 'S': 99 short_list = 1; 100 av[ac++] = "-s"; 101 break; 102 case 'm': 103 av[ac++] = "-m"; 104 break; 105 case 'M': 106 av[ac++] = "-M"; 107 break; 108 case 'p': 109 av[ac++] = "-p"; 110 break; 111 case '?': 112 default: 113 err("illegal option -- %c", ch); 114 } 115 116 if (logging) { 117 sval = sizeof(sin); 118 if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0) 119 err("getpeername: %s", strerror(errno)); 120 if ((hp = gethostbyaddr((char *)&sin.sin_addr.s_addr, 121 sizeof(sin.sin_addr.s_addr), AF_INET))) 122 lp = hp->h_name; 123 else 124 lp = inet_ntoa(sin.sin_addr); 125 syslog(LOG_NOTICE, "query from %s", lp); 126 } 127 128 if (!fgets(line, sizeof(line), stdin)) 129 exit(1); 130 131 /* 132 * Note: we assume that finger(1) will treat "--" as end of 133 * command args (ie: that it uses getopt(3)). 134 */ 135 av[ac++] = "--"; 136 comp = &av[1]; 137 for (lp = line, ap = &av[ac]; ac < ENTRIES;) { 138 if ((*ap = strtok(lp, " \t\r\n")) == NULL) 139 break; 140 lp = NULL; 141 if (secure && strchr(*ap, '@')) { 142 (void) puts("fowarding service denied\r\n"); 143 exit(1); 144 } 145 146 ch = strlen(*ap); 147 while ((*ap)[ch-1] == '@') 148 (*ap)[--ch] = '\0'; 149 if (**ap == '\0') 150 continue; 151 152 /* RFC1196: "/[Ww]" == "-l" */ 153 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 154 if (!short_list) { 155 av[1] = "-l"; 156 comp = &av[0]; 157 } 158 } else { 159 ap++; 160 ac++; 161 } 162 } 163 av[ENTRIES - 1] = NULL; 164 165 if ((lp = strrchr(prog, '/'))) 166 *comp = ++lp; 167 else 168 *comp = prog; 169 170 if (user_required) { 171 for (ap = comp + 1; strcmp("--", *(ap++)); ); 172 if (*ap == NULL) { 173 (void) puts("must provide username\r\n"); 174 exit(1); 175 } 176 } 177 178 if (pipe(p) < 0) 179 err("pipe: %s", strerror(errno)); 180 181 switch(vfork()) { 182 case 0: 183 (void) close(p[0]); 184 if (p[1] != 1) { 185 (void) dup2(p[1], 1); 186 (void) close(p[1]); 187 } 188 execv(prog, comp); 189 err("execv: %s: %s", prog, strerror(errno)); 190 _exit(1); 191 case -1: 192 err("fork: %s", strerror(errno)); 193 } 194 (void) close(p[1]); 195 if (!(fp = fdopen(p[0], "r"))) 196 err("fdopen: %s", strerror(errno)); 197 while ((ch = getc(fp)) != EOF) { 198 if (ch == '\n') 199 putchar('\r'); 200 putchar(ch); 201 } 202 exit(0); 203} 204 205#ifdef __STDC__ 206#include <stdarg.h> 207#else 208#include <varargs.h> 209#endif 210 211void 212#ifdef __STDC__ 213err(const char *fmt, ...) 214#else 215err(fmt, va_alist) 216 char *fmt; 217 va_dcl 218#endif 219{ 220 va_list ap; 221#ifdef __STDC__ 222 va_start(ap, fmt); 223#else 224 va_start(ap); 225#endif 226 (void) vsyslog(LOG_ERR, fmt, ap); 227 va_end(ap); 228 exit(1); 229 /* NOTREACHED */ 230} 231