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