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