fingerd.c revision 45393
11592Srgrimes/* 21592Srgrimes * Copyright (c) 1983, 1993 31592Srgrimes * The Regents of the University of California. All rights reserved. 41592Srgrimes * 51592Srgrimes * Redistribution and use in source and binary forms, with or without 61592Srgrimes * modification, are permitted provided that the following conditions 71592Srgrimes * are met: 81592Srgrimes * 1. Redistributions of source code must retain the above copyright 91592Srgrimes * notice, this list of conditions and the following disclaimer. 101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111592Srgrimes * notice, this list of conditions and the following disclaimer in the 121592Srgrimes * documentation and/or other materials provided with the distribution. 131592Srgrimes * 3. All advertising materials mentioning features or use of this software 141592Srgrimes * must display the following acknowledgement: 151592Srgrimes * This product includes software developed by the University of 161592Srgrimes * California, Berkeley and its contributors. 171592Srgrimes * 4. Neither the name of the University nor the names of its contributors 181592Srgrimes * may be used to endorse or promote products derived from this software 191592Srgrimes * without specific prior written permission. 201592Srgrimes * 211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241592Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311592Srgrimes * SUCH DAMAGE. 321592Srgrimes */ 331592Srgrimes 341592Srgrimes#ifndef lint 3531308Scharnierstatic const char copyright[] = 361592Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 371592Srgrimes The Regents of the University of California. All rights reserved.\n"; 381592Srgrimes#endif /* not lint */ 391592Srgrimes 401592Srgrimes#ifndef lint 4131308Scharnier#if 0 421592Srgrimesstatic char sccsid[] = "@(#)fingerd.c 8.1 (Berkeley) 6/4/93"; 4331308Scharnier#endif 4412908Swollmanstatic const char rcsid[] = 4545393Sbrian "$Id: fingerd.c,v 1.11 1998/05/15 03:23:28 jb Exp $"; 461592Srgrimes#endif /* not lint */ 471592Srgrimes 4845393Sbrian#include <sys/param.h> 491592Srgrimes#include <sys/socket.h> 501592Srgrimes#include <netinet/in.h> 5112908Swollman#include <netinet/tcp.h> 521592Srgrimes#include <arpa/inet.h> 531592Srgrimes#include <errno.h> 541592Srgrimes 551592Srgrimes#include <unistd.h> 561592Srgrimes#include <syslog.h> 571592Srgrimes#include <netdb.h> 581592Srgrimes#include <stdio.h> 591592Srgrimes#include <stdlib.h> 601592Srgrimes#include <strings.h> 611592Srgrimes#include "pathnames.h" 621592Srgrimes 6312908Swollmanvoid logerr __P((const char *, ...)); 641592Srgrimes 651592Srgrimesint 661592Srgrimesmain(argc, argv) 671592Srgrimes int argc; 681592Srgrimes char *argv[]; 691592Srgrimes{ 701592Srgrimes register FILE *fp; 711592Srgrimes register int ch; 721592Srgrimes register char *lp; 731592Srgrimes struct sockaddr_in sin; 741592Srgrimes int p[2], logging, secure, sval; 751592Srgrimes#define ENTRIES 50 761592Srgrimes char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 7745393Sbrian char rhost[MAXHOSTNAMELEN + 1]; 781592Srgrimes 791592Srgrimes prog = _PATH_FINGER; 801592Srgrimes logging = secure = 0; 811592Srgrimes openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 821592Srgrimes opterr = 0; 8324349Simp while ((ch = getopt(argc, argv, "slp:")) != -1) 841592Srgrimes switch (ch) { 851592Srgrimes case 'l': 861592Srgrimes logging = 1; 871592Srgrimes break; 881592Srgrimes case 'p': 891592Srgrimes prog = optarg; 901592Srgrimes break; 911592Srgrimes case 's': 921592Srgrimes secure = 1; 931592Srgrimes break; 941592Srgrimes case '?': 951592Srgrimes default: 9612908Swollman logerr("illegal option -- %c", ch); 971592Srgrimes } 981592Srgrimes 9912908Swollman /* 10012908Swollman * Enable server-side Transaction TCP. 10112908Swollman */ 10212908Swollman { 10336045Sjb#if !defined(__alpha__) /* XXX FIXME */ 10412908Swollman int one = 1; 10512908Swollman if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 10612908Swollman sizeof one) < 0) { 10712908Swollman logerr("setsockopt(TCP_NOPUSH) failed: %m"); 10812908Swollman } 10936045Sjb#endif 11012908Swollman } 11112908Swollman 1121592Srgrimes if (!fgets(line, sizeof(line), stdin)) 1131592Srgrimes exit(1); 1148870Srgrimes 11531168Ssef if (logging) { 11631168Ssef char *t; 11731168Ssef char *end; 11831168Ssef 11931168Ssef end = memchr(line, 0, sizeof(line)); 12031168Ssef if (end == NULL) { 12131168Ssef t = malloc(sizeof(line) + 1); 12231168Ssef memcpy(t, line, sizeof(line)); 12331168Ssef t[sizeof(line)] = 0; 12431168Ssef } else { 12531168Ssef t = strdup(line); 12631168Ssef } 12731168Ssef for (end = t; *end; end++) 12831168Ssef if (*end == '\n' || *end == '\r') 12931168Ssef *end = ' '; 13031168Ssef sval = sizeof(sin); 13131168Ssef if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0) 13231168Ssef logerr("getpeername: %s", strerror(errno)); 13345393Sbrian realhostname(rhost, sizeof rhost - 1, &sin.sin_addr); 13445393Sbrian syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t); 13531168Ssef } 13631168Ssef 1371592Srgrimes comp = &av[1]; 13812728Speter av[2] = "--"; 13912728Speter for (lp = line, ap = &av[3];;) { 1401592Srgrimes *ap = strtok(lp, " \t\r\n"); 1411592Srgrimes if (!*ap) { 14212728Speter if (secure && ap == &av[3]) { 1431592Srgrimes puts("must provide username\r\n"); 1441592Srgrimes exit(1); 1451592Srgrimes } 1461592Srgrimes break; 1471592Srgrimes } 1481592Srgrimes if (secure && strchr(*ap, '@')) { 1496180Sphk puts("forwarding service denied\r\n"); 1501592Srgrimes exit(1); 1511592Srgrimes } 1521592Srgrimes 1531592Srgrimes /* RFC742: "/[Ww]" == "-l" */ 1541592Srgrimes if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 1551592Srgrimes av[1] = "-l"; 1561592Srgrimes comp = &av[0]; 1571592Srgrimes } 1581592Srgrimes else if (++ap == av + ENTRIES) 1591592Srgrimes break; 1601592Srgrimes lp = NULL; 1611592Srgrimes } 1621592Srgrimes 1631592Srgrimes if (lp = strrchr(prog, '/')) 1641592Srgrimes *comp = ++lp; 1651592Srgrimes else 1661592Srgrimes *comp = prog; 1671592Srgrimes if (pipe(p) < 0) 16812908Swollman logerr("pipe: %s", strerror(errno)); 1691592Srgrimes 1701592Srgrimes switch(vfork()) { 1711592Srgrimes case 0: 1721592Srgrimes (void)close(p[0]); 1731592Srgrimes if (p[1] != 1) { 1741592Srgrimes (void)dup2(p[1], 1); 1751592Srgrimes (void)close(p[1]); 1761592Srgrimes } 1771592Srgrimes execv(prog, comp); 17812908Swollman logerr("execv: %s: %s", prog, strerror(errno)); 1791592Srgrimes _exit(1); 1801592Srgrimes case -1: 18112908Swollman logerr("fork: %s", strerror(errno)); 1821592Srgrimes } 1831592Srgrimes (void)close(p[1]); 1841592Srgrimes if (!(fp = fdopen(p[0], "r"))) 18512908Swollman logerr("fdopen: %s", strerror(errno)); 1861592Srgrimes while ((ch = getc(fp)) != EOF) { 1871592Srgrimes if (ch == '\n') 1881592Srgrimes putchar('\r'); 1891592Srgrimes putchar(ch); 1901592Srgrimes } 1911592Srgrimes exit(0); 1921592Srgrimes} 1931592Srgrimes 1941592Srgrimes#if __STDC__ 1951592Srgrimes#include <stdarg.h> 1961592Srgrimes#else 1971592Srgrimes#include <varargs.h> 1981592Srgrimes#endif 1991592Srgrimes 2001592Srgrimesvoid 2011592Srgrimes#if __STDC__ 20212908Swollmanlogerr(const char *fmt, ...) 2031592Srgrimes#else 20412908Swollmanlogerr(fmt, va_alist) 2051592Srgrimes char *fmt; 2061592Srgrimes va_dcl 2071592Srgrimes#endif 2081592Srgrimes{ 2091592Srgrimes va_list ap; 2101592Srgrimes#if __STDC__ 2111592Srgrimes va_start(ap, fmt); 2121592Srgrimes#else 2131592Srgrimes va_start(ap); 2141592Srgrimes#endif 2151592Srgrimes (void)vsyslog(LOG_ERR, fmt, ap); 2161592Srgrimes va_end(ap); 2171592Srgrimes exit(1); 2181592Srgrimes /* NOTREACHED */ 2191592Srgrimes} 220