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