1 /*
2  * safe_finger - finger client wrapper that protects against nasty stuff
3  * from finger servers. Use this program for automatic reverse finger
4  * probes, not the raw finger command.
5  *
6  * Build with: cc -o safe_finger safe_finger.c
7  *
8  * The problem: some programs may react to stuff in the first column. Other
9  * programs may get upset by thrash anywhere on a line. File systems may
10  * fill up as the finger server keeps sending data. Text editors may bomb
11  * out on extremely long lines. The finger server may take forever because
12  * it is somehow wedged. The code below takes care of all this badness.
13  *
14  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
15  */
16
17#ifndef lint
18static char sccsid[] = "@(#) safe_finger.c 1.4 94/12/28 17:42:41";
19#endif
20
21/* System libraries */
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <signal.h>
26#include <stdio.h>
27#include <ctype.h>
28#include <pwd.h>
29
30extern void exit();
31
32/* Local stuff */
33
34char    path[] = "PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:/etc:/usr/etc:/usr/sbin";
35
36#define	TIME_LIMIT	60		/* Do not keep listinging forever */
37#define	INPUT_LENGTH	100000		/* Do not keep listinging forever */
38#define	LINE_LENGTH	128		/* Editors can choke on long lines */
39#define	FINGER_PROGRAM	"finger"	/* Most, if not all, UNIX systems */
40#define	UNPRIV_NAME	"nobody"	/* Preferred privilege level */
41#define	UNPRIV_UGID	32767		/* Default uid and gid */
42
43int     finger_pid;
44
45void    cleanup(sig)
46int     sig;
47{
48    kill(finger_pid, SIGKILL);
49    exit(0);
50}
51
52main(int argc, char **argv)
53{
54    int     c;
55    int     line_length = 0;
56    int     finger_status;
57    int     wait_pid;
58    int     input_count = 0;
59    struct passwd *pwd;
60
61    /*
62     * First of all, let's don't run with superuser privileges.
63     */
64    if (getuid() == 0 || geteuid() == 0) {
65	if ((pwd = getpwnam(UNPRIV_NAME)) && pwd->pw_uid > 0) {
66	    setgid(pwd->pw_gid);
67	    setuid(pwd->pw_uid);
68	} else {
69	    setgid(UNPRIV_UGID);
70	    setuid(UNPRIV_UGID);
71	}
72    }
73
74    /*
75     * Redirect our standard input through the raw finger command.
76     */
77    if (putenv(path)) {
78	fprintf(stderr, "%s: putenv: out of memory", argv[0]);
79	exit(1);
80    }
81    argv[0] = FINGER_PROGRAM;
82    finger_pid = pipe_stdin(argv);
83
84    /*
85     * Don't wait forever (Peter Wemm <peter@gecko.DIALix.oz.au>).
86     */
87    signal(SIGALRM, cleanup);
88    (void) alarm(TIME_LIMIT);
89
90    /*
91     * Main filter loop.
92     */
93    while ((c = getchar()) != EOF) {
94	if (input_count++ >= INPUT_LENGTH) {	/* don't listen forever */
95	    fclose(stdin);
96	    printf("\n\n Input truncated to %d bytes...\n", input_count - 1);
97	    break;
98	}
99	if (c == '\n') {			/* good: end of line */
100	    putchar(c);
101	    line_length = 0;
102	} else {
103	    if (line_length >= LINE_LENGTH) {	/* force end of line */
104		printf("\\\n");
105		line_length = 0;
106	    }
107	    if (line_length == 0) {		/* protect left margin */
108		putchar(' ');
109		line_length++;
110	    }
111	    if (isascii(c) && (isprint(c) || isspace(c))) {	/* text */
112		if (c == '\\') {
113		    putchar(c);
114		    line_length++;
115		}
116		putchar(c);
117		line_length++;
118	    } else {				/* quote all other thash */
119		printf("\\%03o", c & 0377);
120		line_length += 4;
121	    }
122	}
123    }
124
125    /*
126     * Wait until the finger child process has terminated and account for its
127     * exit status. Which will always be zero on most systems.
128     */
129    while ((wait_pid = wait(&finger_status)) != -1 && wait_pid != finger_pid)
130	 /* void */ ;
131    return (wait_pid != finger_pid || finger_status != 0);
132}
133
134/* perror_exit - report system error text and terminate */
135
136void    perror_exit(char *text)
137{
138    perror(text);
139    exit(1);
140}
141
142/* pipe_stdin - pipe stdin through program (from my ANSI to OLD C converter) */
143
144int     pipe_stdin(char **argv)
145{
146    int     pipefds[2];
147    int     pid;
148    int     i;
149    struct stat st;
150
151    /*
152     * The code that sets up the pipe requires that file descriptors 0,1,2
153     * are already open. All kinds of mysterious things will happen if that
154     * is not the case. The following loops makes sure that descriptors 0,1,2
155     * are set up properly.
156     */
157
158    for (i = 0; i < 3; i++) {
159	if (fstat(i, &st) == -1 && open("/dev/null", 2) != i)
160	    perror_exit("open /dev/null");
161    }
162
163    /*
164     * Set up the pipe that interposes the command into our standard input
165     * stream.
166     */
167
168    if (pipe(pipefds))
169	perror_exit("pipe");
170
171    switch (pid = fork()) {
172    case -1:					/* error */
173	perror_exit("fork");
174	/* NOTREACHED */
175    case 0:					/* child */
176	(void) close(pipefds[0]);		/* close reading end */
177	(void) close(1);			/* connect stdout to pipe */
178	if (dup(pipefds[1]) != 1)
179	    perror_exit("dup");
180	(void) close(pipefds[1]);		/* close redundant fd */
181	(void) execvp(argv[0], argv);
182	perror_exit(argv[0]);
183	/* NOTREACHED */
184    default:					/* parent */
185	(void) close(pipefds[1]);		/* close writing end */
186	(void) close(0);			/* connect stdin to pipe */
187	if (dup(pipefds[0]) != 0)
188	    perror_exit("dup");
189	(void) close(pipefds[0]);		/* close redundant fd */
190	return (pid);
191    }
192}
193