#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <pwd.h>
extern void exit();
char path[] = "PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:/etc:/usr/etc:/usr/sbin";
#define TIME_LIMIT 60
#define INPUT_LENGTH 100000
#define LINE_LENGTH 128
#define FINGER_PROGRAM "finger"
#define UNPRIV_NAME "nobody"
#define UNPRIV_UGID 32767
void perror_exit(char *text) __NORETURN;
int finger_pid;
void cleanup(sig)
int sig;
{
kill(finger_pid, SIGKILL);
exit(0);
}
int
main(argc, argv)
int argc;
char **argv;
{
int c;
int line_length = 0;
int finger_status;
int wait_pid;
int input_count = 0;
struct passwd *pwd;
if (getuid() == 0 || geteuid() == 0) {
if ((pwd = getpwnam(UNPRIV_NAME)) && pwd->pw_uid > 0) {
setgid(pwd->pw_gid);
setuid(pwd->pw_uid);
} else {
setgid(UNPRIV_UGID);
setuid(UNPRIV_UGID);
}
}
if (putenv(path)) {
fprintf(stderr, "%s: putenv: out of memory", argv[0]);
exit(1);
}
argv[0] = FINGER_PROGRAM;
finger_pid = pipe_stdin(argv);
signal(SIGALRM, cleanup);
(void) alarm(TIME_LIMIT);
while ((c = getchar()) != EOF) {
if (input_count++ >= INPUT_LENGTH) {
fclose(stdin);
printf("\n\n Input truncated to %d bytes...\n", input_count - 1);
break;
}
if (c == '\n') {
putchar(c);
line_length = 0;
} else {
if (line_length >= LINE_LENGTH) {
printf("\\\n");
line_length = 0;
}
if (line_length == 0) {
putchar(' ');
line_length++;
}
if (isascii(c) && (isprint(c) || isspace(c))) {
if (c == '\\') {
putchar(c);
line_length++;
}
putchar(c);
line_length++;
} else {
printf("\\%03o", c & 0377);
line_length += 4;
}
}
}
while ((wait_pid = wait(&finger_status)) != -1 && wait_pid != finger_pid)
;
return (wait_pid != finger_pid || finger_status != 0);
}
void
perror_exit(char *text)
{
perror(text);
exit(1);
}
int pipe_stdin(argv)
char **argv;
{
int pipefds[2];
int pid;
int i;
struct stat st;
for (i = 0; i < 3; i++) {
if (fstat(i, &st) == -1 && open("/dev/null", 2) != i)
perror_exit("open /dev/null");
}
if (pipe(pipefds))
perror_exit("pipe");
switch (pid = fork()) {
case -1:
perror_exit("fork");
case 0:
(void) close(pipefds[0]);
(void) close(1);
if (dup(pipefds[1]) != 1)
perror_exit("dup");
(void) close(pipefds[1]);
(void) execvp(argv[0], argv);
perror_exit(argv[0]);
default:
(void) close(pipefds[1]);
(void) close(0);
if (dup(pipefds[0]) != 0)
perror_exit("dup");
(void) close(pipefds[0]);
return (pid);
}
}