xref: /original-bsd/libexec/fingerd/fingerd.c (revision 10ea2def)
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.4 (Berkeley) 11/23/88";
26 #endif /* not lint */
27 
28 #include <stdio.h>
29 
30 main()
31 {
32 	register FILE *fp;
33 	register int ch;
34 	register char *lp;
35 	int p[2];
36 #define	ENTRIES	50
37 	char **ap, *av[ENTRIES + 1], line[1024], *strtok();
38 
39 #ifdef LOGGING					/* unused for now */
40 #include <netinet/in.h>
41 	struct sockaddr_in sin;
42 	int sval;
43 
44 	sval = sizeof(sin);
45 	if (getpeername(0, &sin, &sval) < 0)
46 		fatal("getpeername");
47 #endif
48 
49 	if (!fgets(line, sizeof(line), stdin))
50 		exit(1);
51 
52 	av[0] = "finger";
53 	for (lp = line, ap = &av[1];;) {
54 		*ap = strtok(lp, " \t\r\n");
55 		if (!*ap)
56 			break;
57 		/* RFC742: "/[Ww]" == "-l" */
58 		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w'))
59 			*ap = "-l";
60 		if (++ap == av + ENTRIES)
61 			break;
62 		lp = NULL;
63 	}
64 
65 	if (pipe(p) < 0)
66 		fatal("pipe");
67 
68 	switch(fork()) {
69 	case 0:
70 		(void)close(p[0]);
71 		if (p[1] != 1) {
72 			(void)dup2(p[1], 1);
73 			(void)close(p[1]);
74 		}
75 		execv("/usr/ucb/finger", av);
76 		_exit(1);
77 	case -1:
78 		fatal("fork");
79 	}
80 	(void)close(p[1]);
81 	if (!(fp = fdopen(p[0], "r")))
82 		fatal("fdopen");
83 	while ((ch = getc(fp)) != EOF) {
84 		if (ch == '\n')
85 			putchar('\r');
86 		putchar(ch);
87 	}
88 	exit(0);
89 }
90 
91 fatal(msg)
92 	char *msg;
93 {
94 	extern int errno;
95 	char *strerror();
96 
97 	fprintf(stderr, "fingerd: %s: %s\r\n", msg, strerror(errno));
98 	exit(1);
99 }
100