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