xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/in.fingerd.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 /*
36  * Finger server.
37  */
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
41 #include <stdio.h>
42 #include <ctype.h>
43 
44 #define	MAXARGS 10
45 
46 void fatal(char *prog, char *s);
47 
48 int
main(argc,argv)49 main(argc, argv)
50 	int argc;
51 	char *argv[];
52 {
53 	register char *sp;
54 	char line[512];
55 	struct sockaddr_storage sin;
56 	pid_t pid, w;
57 	int i, p[2], status;
58 	FILE *fp;
59 	char *av[MAXARGS + 1];
60 
61 	i = sizeof (sin);
62 	if (getpeername(0, (struct sockaddr *)&sin, &i) < 0)
63 		fatal(argv[0], "getpeername");
64 	line[0] = '\0';
65 	if (fgets(line, sizeof (line), stdin) == NULL)
66 		exit(1);
67 	sp = line;
68 	av[0] = "finger";
69 	i = 1;
70 
71 	/* skip past leading white space */
72 	while (isspace(*sp))
73 		sp++;
74 
75 	/*
76 	 * The finger protocol says a "/W" switch means verbose output.
77 	 * We explicitly set either the "long" or "short" output flags
78 	 * to the finger program so that we don't have to know what what
79 	 * the "finger" program's default is.
80 	 */
81 	if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
82 		sp += 2;
83 		av[i++] = "-l";
84 	} else {
85 		av[i++] = "-s";
86 	}
87 
88 	/* look for username arguments */
89 	while (i < MAXARGS) {
90 
91 		/* skip over leading white space */
92 		while (isspace(*sp))
93 			sp++;
94 
95 		/* check for end of "command line" */
96 		if (*sp == '\0')
97 			break;
98 
99 		/* pick up another name argument */
100 		av[i++] = sp;
101 		while ((*sp != '\0') && !isspace(*sp))
102 			sp++;
103 
104 		/* check again for end of "command line" */
105 		if (*sp == '\0')
106 			break;
107 		else
108 			*sp++ = '\0';
109 	}
110 
111 	av[i] = (char *)0;
112 	if (pipe(p) < 0)
113 		fatal(argv[0], "pipe");
114 
115 	if ((pid = fork()) == 0) {
116 		close(p[0]);
117 		if (p[1] != 1) {
118 			dup2(p[1], 1);
119 			close(p[1]);
120 		}
121 		execv("/usr/bin/finger", av);
122 		printf("No local finger program found\n");
123 		fflush(stdout);
124 		_exit(1);
125 	}
126 	if (pid == (pid_t)-1)
127 		fatal(argv[0], "fork");
128 	close(p[1]);
129 	if ((fp = fdopen(p[0], "r")) == NULL)
130 		fatal(argv[0], "fdopen");
131 	while ((i = getc(fp)) != EOF) {
132 		if (i == '\n')
133 			putchar('\r');
134 		putchar(i);
135 	}
136 	fclose(fp);
137 	while ((w = wait(&status)) != pid && w != (pid_t)-1)
138 		;
139 	return (0);
140 }
141 
142 void
fatal(prog,s)143 fatal(prog, s)
144 	char *prog, *s;
145 {
146 
147 	fprintf(stderr, "%s: ", prog);
148 	perror(s);
149 	exit(1);
150 }
151