Unleashed v1.4
[unleashed.git] / usr / src / cmd / cmd-inet / usr.sbin / in.fingerd.c
blob58c16a66f9805480debb6d9bd8d3741ae68cd22b
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * Finger server.
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <unistd.h>
49 #define MAXARGS 10
51 void fatal(char *prog, char *s);
53 int
54 main(argc, argv)
55 int argc;
56 char *argv[];
58 register char *sp;
59 char line[512];
60 struct sockaddr_storage sin;
61 pid_t pid, w;
62 int i, p[2], status;
63 FILE *fp;
64 char *av[MAXARGS + 1];
65 socklen_t len = sizeof (sin);
67 if (getpeername(0, (struct sockaddr *)&sin, &len) < 0)
68 fatal(argv[0], "getpeername");
69 line[0] = '\0';
70 if (fgets(line, sizeof (line), stdin) == NULL)
71 exit(1);
72 sp = line;
73 av[0] = "finger";
74 i = 1;
76 /* skip past leading white space */
77 while (isspace(*sp))
78 sp++;
81 * The finger protocol says a "/W" switch means verbose output.
82 * We explicitly set either the "long" or "short" output flags
83 * to the finger program so that we don't have to know what what
84 * the "finger" program's default is.
86 if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
87 sp += 2;
88 av[i++] = "-l";
89 } else {
90 av[i++] = "-s";
93 /* look for username arguments */
94 while (i < MAXARGS) {
96 /* skip over leading white space */
97 while (isspace(*sp))
98 sp++;
100 /* check for end of "command line" */
101 if (*sp == '\0')
102 break;
104 /* pick up another name argument */
105 av[i++] = sp;
106 while ((*sp != '\0') && !isspace(*sp))
107 sp++;
109 /* check again for end of "command line" */
110 if (*sp == '\0')
111 break;
112 else
113 *sp++ = '\0';
116 av[i] = NULL;
117 if (pipe(p) < 0)
118 fatal(argv[0], "pipe");
120 if ((pid = fork()) == 0) {
121 close(p[0]);
122 if (p[1] != 1) {
123 dup2(p[1], 1);
124 close(p[1]);
126 execv("/usr/bin/finger", av);
127 printf("No local finger program found\n");
128 fflush(stdout);
129 _exit(1);
131 if (pid == (pid_t)-1)
132 fatal(argv[0], "fork");
133 close(p[1]);
134 if ((fp = fdopen(p[0], "r")) == NULL)
135 fatal(argv[0], "fdopen");
136 while ((i = getc(fp)) != EOF) {
137 if (i == '\n')
138 putchar('\r');
139 putchar(i);
141 fclose(fp);
142 while ((w = wait(&status)) != pid && w != (pid_t)-1)
144 return (0);
147 void
148 fatal(prog, s)
149 char *prog, *s;
152 fprintf(stderr, "%s: ", prog);
153 perror(s);
154 exit(1);