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