xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.fingerd.c (revision 473:9c6ea6da29de)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Finger server.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <sys/socket.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <ctype.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	MAXARGS 10
470Sstevel@tonic-gate 
48*473Sbw void fatal(char *prog, char *s);
49*473Sbw 
50*473Sbw int
main(argc,argv)510Sstevel@tonic-gate main(argc, argv)
52*473Sbw 	int argc;
530Sstevel@tonic-gate 	char *argv[];
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	register char *sp;
560Sstevel@tonic-gate 	char line[512];
570Sstevel@tonic-gate 	struct sockaddr_storage sin;
580Sstevel@tonic-gate 	pid_t pid, w;
590Sstevel@tonic-gate 	int i, p[2], status;
600Sstevel@tonic-gate 	FILE *fp;
610Sstevel@tonic-gate 	char *av[MAXARGS + 1];
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	i = sizeof (sin);
640Sstevel@tonic-gate 	if (getpeername(0, (struct sockaddr *)&sin, &i) < 0)
650Sstevel@tonic-gate 		fatal(argv[0], "getpeername");
660Sstevel@tonic-gate 	line[0] = '\0';
670Sstevel@tonic-gate 	if (fgets(line, sizeof (line), stdin) == NULL)
680Sstevel@tonic-gate 		exit(1);
690Sstevel@tonic-gate 	sp = line;
700Sstevel@tonic-gate 	av[0] = "finger";
710Sstevel@tonic-gate 	i = 1;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/* skip past leading white space */
740Sstevel@tonic-gate 	while (isspace(*sp))
750Sstevel@tonic-gate 		sp++;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * The finger protocol says a "/W" switch means verbose output.
790Sstevel@tonic-gate 	 * We explicitly set either the "long" or "short" output flags
800Sstevel@tonic-gate 	 * to the finger program so that we don't have to know what what
810Sstevel@tonic-gate 	 * the "finger" program's default is.
820Sstevel@tonic-gate 	 */
830Sstevel@tonic-gate 	if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
840Sstevel@tonic-gate 		sp += 2;
850Sstevel@tonic-gate 		av[i++] = "-l";
860Sstevel@tonic-gate 	} else {
870Sstevel@tonic-gate 		av[i++] = "-s";
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/* look for username arguments */
910Sstevel@tonic-gate 	while (i < MAXARGS) {
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		/* skip over leading white space */
940Sstevel@tonic-gate 		while (isspace(*sp))
950Sstevel@tonic-gate 			sp++;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 		/* check for end of "command line" */
980Sstevel@tonic-gate 		if (*sp == '\0')
990Sstevel@tonic-gate 			break;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		/* pick up another name argument */
1020Sstevel@tonic-gate 		av[i++] = sp;
1030Sstevel@tonic-gate 		while ((*sp != '\0') && !isspace(*sp))
1040Sstevel@tonic-gate 			sp++;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		/* check again for end of "command line" */
1070Sstevel@tonic-gate 		if (*sp == '\0')
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 		else
1100Sstevel@tonic-gate 			*sp++ = '\0';
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	av[i] = (char *)0;
1140Sstevel@tonic-gate 	if (pipe(p) < 0)
1150Sstevel@tonic-gate 		fatal(argv[0], "pipe");
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
1180Sstevel@tonic-gate 		close(p[0]);
1190Sstevel@tonic-gate 		if (p[1] != 1) {
1200Sstevel@tonic-gate 			dup2(p[1], 1);
1210Sstevel@tonic-gate 			close(p[1]);
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 		execv("/usr/bin/finger", av);
1240Sstevel@tonic-gate 		printf("No local finger program found\n");
1250Sstevel@tonic-gate 		fflush(stdout);
1260Sstevel@tonic-gate 		_exit(1);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	if (pid == (pid_t)-1)
1290Sstevel@tonic-gate 		fatal(argv[0], "fork");
1300Sstevel@tonic-gate 	close(p[1]);
1310Sstevel@tonic-gate 	if ((fp = fdopen(p[0], "r")) == NULL)
1320Sstevel@tonic-gate 		fatal(argv[0], "fdopen");
1330Sstevel@tonic-gate 	while ((i = getc(fp)) != EOF) {
1340Sstevel@tonic-gate 		if (i == '\n')
1350Sstevel@tonic-gate 			putchar('\r');
1360Sstevel@tonic-gate 		putchar(i);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	fclose(fp);
1390Sstevel@tonic-gate 	while ((w = wait(&status)) != pid && w != (pid_t)-1)
1400Sstevel@tonic-gate 		;
1410Sstevel@tonic-gate 	return (0);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
144*473Sbw void
fatal(prog,s)1450Sstevel@tonic-gate fatal(prog, s)
1460Sstevel@tonic-gate 	char *prog, *s;
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	fprintf(stderr, "%s: ", prog);
1500Sstevel@tonic-gate 	perror(s);
1510Sstevel@tonic-gate 	exit(1);
1520Sstevel@tonic-gate }
153