Home | History | Annotate | Line # | Download | only in fingerd
fingerd.c revision 1.5
      1 /*	$NetBSD: fingerd.c,v 1.5 1997/09/09 02:40:43 mrg 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
     37 static 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
     44 static char sccsid[] = "from: @(#)fingerd.c	8.1 (Berkeley) 6/4/93";
     45 #else
     46 static char rcsid[] = "$NetBSD: fingerd.c,v 1.5 1997/09/09 02:40:43 mrg 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 
     64 void err __P((const char *, ...));
     65 
     66 int
     67 main(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, "gsluSmpP:")) != EOF)
     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 'p':
    106 			av[ac++] = "-p";
    107 			break;
    108 		case 'g':
    109 			av[ac++] = "-g";
    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 	av[ac++] = "--";
    132 	comp = &av[1];
    133 	for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
    134 		if ((*ap = strtok(lp, " \t\r\n")) == NULL)
    135 			break;
    136 		lp = NULL;
    137 		if (secure && strchr(*ap, '@')) {
    138 			(void) puts("fowarding service denied\r\n");
    139 			exit(1);
    140 		}
    141 
    142 		ch = strlen(*ap);
    143 		while ((*ap)[ch-1] == '@')
    144 			(*ap)[--ch] = '\0';
    145 		if (**ap == '\0')
    146 			continue;
    147 
    148 		/* RFC1196: "/[Ww]" == "-l" */
    149 		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
    150 			if (!short_list) {
    151 				av[1] = "-l";
    152 				comp = &av[0];
    153 			}
    154 		} else {
    155 			ap++;
    156 			ac++;
    157 		}
    158 	}
    159 	av[ENTRIES - 1] = NULL;
    160 
    161 	if ((lp = strrchr(prog, '/')))
    162 		*comp = ++lp;
    163 	else
    164 		*comp = prog;
    165 
    166 	if (user_required) {
    167 		for (ap = comp + 1; strcmp("--", *(ap++)); );
    168 		if (*ap == NULL) {
    169 			(void) puts("must provide username\r\n");
    170 			exit(1);
    171 		}
    172 	}
    173 
    174 	if (pipe(p) < 0)
    175 		err("pipe: %s", strerror(errno));
    176 
    177 	switch(vfork()) {
    178 	case 0:
    179 		(void) close(p[0]);
    180 		if (p[1] != 1) {
    181 			(void) dup2(p[1], 1);
    182 			(void) close(p[1]);
    183 		}
    184 		execv(prog, comp);
    185 		err("execv: %s: %s", prog, strerror(errno));
    186 		_exit(1);
    187 	case -1:
    188 		err("fork: %s", strerror(errno));
    189 	}
    190 	(void) close(p[1]);
    191 	if (!(fp = fdopen(p[0], "r")))
    192 		err("fdopen: %s", strerror(errno));
    193 	while ((ch = getc(fp)) != EOF) {
    194 		if (ch == '\n')
    195 			putchar('\r');
    196 		putchar(ch);
    197 	}
    198 	exit(0);
    199 }
    200 
    201 #if __STDC__
    202 #include <stdarg.h>
    203 #else
    204 #include <varargs.h>
    205 #endif
    206 
    207 void
    208 #if __STDC__
    209 err(const char *fmt, ...)
    210 #else
    211 err(fmt, va_alist)
    212 	char *fmt;
    213         va_dcl
    214 #endif
    215 {
    216 	va_list ap;
    217 #if __STDC__
    218 	va_start(ap, fmt);
    219 #else
    220 	va_start(ap);
    221 #endif
    222 	(void) vsyslog(LOG_ERR, fmt, ap);
    223 	va_end(ap);
    224 	exit(1);
    225 	/* NOTREACHED */
    226 }
    227