xref: /original-bsd/libexec/fingerd/fingerd.c (revision e59fb703)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)fingerd.c	5.6 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include "pathnames.h"
20 
21 main()
22 {
23 	register FILE *fp;
24 	register int ch;
25 	register char *lp;
26 	int p[2];
27 #define	ENTRIES	50
28 	char **ap, *av[ENTRIES + 1], line[1024], *strtok();
29 
30 #ifdef LOGGING					/* unused for now */
31 #include <netinet/in.h>
32 	struct sockaddr_in sin;
33 	int sval;
34 
35 	sval = sizeof(sin);
36 	if (getpeername(0, &sin, &sval) < 0)
37 		fatal("getpeername");
38 #endif
39 
40 	if (!fgets(line, sizeof(line), stdin))
41 		exit(1);
42 
43 	av[0] = "finger";
44 	for (lp = line, ap = &av[1];;) {
45 		*ap = strtok(lp, " \t\r\n");
46 		if (!*ap)
47 			break;
48 		/* RFC742: "/[Ww]" == "-l" */
49 		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w'))
50 			*ap = "-l";
51 		if (++ap == av + ENTRIES)
52 			break;
53 		lp = NULL;
54 	}
55 
56 	if (pipe(p) < 0)
57 		fatal("pipe");
58 
59 	switch(fork()) {
60 	case 0:
61 		(void)close(p[0]);
62 		if (p[1] != 1) {
63 			(void)dup2(p[1], 1);
64 			(void)close(p[1]);
65 		}
66 		execv(_PATH_FINGER, av);
67 		_exit(1);
68 	case -1:
69 		fatal("fork");
70 	}
71 	(void)close(p[1]);
72 	if (!(fp = fdopen(p[0], "r")))
73 		fatal("fdopen");
74 	while ((ch = getc(fp)) != EOF) {
75 		if (ch == '\n')
76 			putchar('\r');
77 		putchar(ch);
78 	}
79 	exit(0);
80 }
81 
82 fatal(msg)
83 	char *msg;
84 {
85 	extern int errno;
86 	char *strerror();
87 
88 	fprintf(stderr, "fingerd: %s: %s\r\n", msg, strerror(errno));
89 	exit(1);
90 }
91