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