xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/in.fingerd.c (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * Finger server.
39  */
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 
46 #define	MAXARGS 10
47 
48 void fatal(char *prog, char *s);
49 
50 int
51 main(argc, argv)
52 	int argc;
53 	char *argv[];
54 {
55 	register char *sp;
56 	char line[512];
57 	struct sockaddr_storage sin;
58 	pid_t pid, w;
59 	int i, p[2], status;
60 	FILE *fp;
61 	char *av[MAXARGS + 1];
62 
63 	i = sizeof (sin);
64 	if (getpeername(0, (struct sockaddr *)&sin, &i) < 0)
65 		fatal(argv[0], "getpeername");
66 	line[0] = '\0';
67 	if (fgets(line, sizeof (line), stdin) == NULL)
68 		exit(1);
69 	sp = line;
70 	av[0] = "finger";
71 	i = 1;
72 
73 	/* skip past leading white space */
74 	while (isspace(*sp))
75 		sp++;
76 
77 	/*
78 	 * The finger protocol says a "/W" switch means verbose output.
79 	 * We explicitly set either the "long" or "short" output flags
80 	 * to the finger program so that we don't have to know what what
81 	 * the "finger" program's default is.
82 	 */
83 	if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
84 		sp += 2;
85 		av[i++] = "-l";
86 	} else {
87 		av[i++] = "-s";
88 	}
89 
90 	/* look for username arguments */
91 	while (i < MAXARGS) {
92 
93 		/* skip over leading white space */
94 		while (isspace(*sp))
95 			sp++;
96 
97 		/* check for end of "command line" */
98 		if (*sp == '\0')
99 			break;
100 
101 		/* pick up another name argument */
102 		av[i++] = sp;
103 		while ((*sp != '\0') && !isspace(*sp))
104 			sp++;
105 
106 		/* check again for end of "command line" */
107 		if (*sp == '\0')
108 			break;
109 		else
110 			*sp++ = '\0';
111 	}
112 
113 	av[i] = (char *)0;
114 	if (pipe(p) < 0)
115 		fatal(argv[0], "pipe");
116 
117 	if ((pid = fork()) == 0) {
118 		close(p[0]);
119 		if (p[1] != 1) {
120 			dup2(p[1], 1);
121 			close(p[1]);
122 		}
123 		execv("/usr/bin/finger", av);
124 		printf("No local finger program found\n");
125 		fflush(stdout);
126 		_exit(1);
127 	}
128 	if (pid == (pid_t)-1)
129 		fatal(argv[0], "fork");
130 	close(p[1]);
131 	if ((fp = fdopen(p[0], "r")) == NULL)
132 		fatal(argv[0], "fdopen");
133 	while ((i = getc(fp)) != EOF) {
134 		if (i == '\n')
135 			putchar('\r');
136 		putchar(i);
137 	}
138 	fclose(fp);
139 	while ((w = wait(&status)) != pid && w != (pid_t)-1)
140 		;
141 	return (0);
142 }
143 
144 void
145 fatal(prog, s)
146 	char *prog, *s;
147 {
148 
149 	fprintf(stderr, "%s: ", prog);
150 	perror(s);
151 	exit(1);
152 }
153