xref: /original-bsd/libexec/fingerd/fingerd.c (revision 6131e5cb)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)fingerd.c	5.2 (Berkeley) 09/19/88";
26 #endif /* not lint */
27 
28 /*
29  * Finger server.
30  */
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 
34 #include <stdio.h>
35 #include <ctype.h>
36 
37 main(argc, argv)
38 	int argc;
39 	char *argv[];
40 {
41 	register char *sp;
42 	char line[512];
43 	struct sockaddr_in sin;
44 	int i, p[2], pid, status;
45 	FILE *fp;
46 	char *av[4];
47 
48 	i = sizeof (sin);
49 	if (getpeername(0, &sin, &i) < 0)
50 		fatal(argv[0], "getpeername");
51 	line[0] = '\0';
52 	gets(line);
53 	sp = line;
54 	av[0] = "finger";
55 	i = 1;
56 	while (1) {
57 		while (isspace(*sp))
58 			sp++;
59 		if (!*sp)
60 			break;
61 		if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
62 			sp += 2;
63 			av[i++] = "-l";
64 		}
65 		if (*sp && !isspace(*sp)) {
66 			av[i++] = sp;
67 			while (*sp && !isspace(*sp))
68 				sp++;
69 			*sp = '\0';
70 		}
71 	}
72 	av[i] = 0;
73 	if (pipe(p) < 0)
74 		fatal(argv[0], "pipe");
75 	if ((pid = fork()) == 0) {
76 		close(p[0]);
77 		if (p[1] != 1) {
78 			dup2(p[1], 1);
79 			close(p[1]);
80 		}
81 		execv("/usr/ucb/finger", av);
82 		_exit(1);
83 	}
84 	if (pid == -1)
85 		fatal(argv[0], "fork");
86 	close(p[1]);
87 	if ((fp = fdopen(p[0], "r")) == NULL)
88 		fatal(argv[0], "fdopen");
89 	while ((i = getc(fp)) != EOF) {
90 		if (i == '\n')
91 			putchar('\r');
92 		putchar(i);
93 	}
94 	fclose(fp);
95 	while ((i = wait(&status)) != pid && i != -1)
96 		;
97 	return(0);
98 }
99 
100 fatal(prog, s)
101 	char *prog, *s;
102 {
103 
104 	fprintf(stderr, "%s: ", prog);
105 	perror(s);
106 	exit(1);
107 }
108