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