|
|
1.1 ! root 1: #ifndef lint ! 2: static char sccsid[] = "@(#)courierd.c 4.4 (Berkeley) 9/17/85"; ! 3: #endif ! 4: ! 5: /* ! 6: * Courier program instantiation server ! 7: * ! 8: * When a connection is made, it reads the program name as a Courier string. ! 9: * The directory /usr/new/lib/courier is used as a database of program names; ! 10: * the executable program must either reside there or have a link there. ! 11: * The program is spawned with user and group ID equal to that of the ! 12: * executable file, and must write a single zero byte back to the caller to ! 13: * indicate successful instantiation. ! 14: * If instantiation was unsuccessful, the daemon will write back a null- ! 15: * terminated error message. ! 16: */ ! 17: #include <sys/types.h> ! 18: #include <sys/socket.h> ! 19: #include <sys/stat.h> ! 20: #include <sys/wait.h> ! 21: ! 22: #include <netinet/in.h> ! 23: ! 24: #include <stdio.h> ! 25: #include <signal.h> ! 26: #include <errno.h> ! 27: #include <netdb.h> ! 28: #include <setjmp.h> ! 29: #include <syslog.h> ! 30: ! 31: #include "../lib/courier.h" ! 32: ! 33: #define LIBCOURIER "/usr/new/lib/courier" ! 34: ! 35: jmp_buf alarmbuf; ! 36: int catch(); ! 37: ! 38: main(argc, argv) ! 39: int argc; ! 40: char **argv; ! 41: { ! 42: struct sockaddr_in sin; ! 43: Cardinal n, nbytes; ! 44: struct stat statbuf; ! 45: char name[200]; ! 46: int len; ! 47: ! 48: len = sizeof (sin); ! 49: if (getpeername(0, &sin, &len) < 0) { ! 50: fprintf(stderr, "%s: ", argv[0]), perror("getpeername"); ! 51: _exit(1); ! 52: } ! 53: openlog("courier", LOG_ODELAY, LOG_DAEMON); ! 54: if (chdir(LIBCOURIER)) { ! 55: syslog(LOG_ERR, "chdir: %s: %m", LIBCOURIER); ! 56: _exit(2); ! 57: } ! 58: setpgrp(0, getpid()); ! 59: signal(SIGHUP, SIG_DFL); ! 60: signal(SIGINT, SIG_DFL); ! 61: signal(SIGQUIT, SIG_DFL); ! 62: signal(SIGALRM, catch); ! 63: ! 64: dup2(0, 1); ! 65: ! 66: alarm(60); ! 67: if (setjmp(alarmbuf) == 0) ! 68: read(0, &n, sizeof(Cardinal)); ! 69: alarm(0); ! 70: ! 71: UnpackCardinal(&nbytes, &n); ! 72: /* ! 73: * Courier strings are always word-aligned, so if the byte count is odd, ! 74: * we must also read the padding byte. ! 75: */ ! 76: read(0, name, nbytes + (nbytes % 2)); ! 77: name[nbytes] = '\0'; ! 78: ! 79: if (name[0] == '/' || name[0] == '.' || stat(name, &statbuf) != 0) ! 80: goto bad; ! 81: setgroups(0, 0); ! 82: setregid(statbuf.st_gid, statbuf.st_gid); ! 83: setreuid(statbuf.st_uid, statbuf.st_uid); ! 84: execl(name, name, 0); ! 85: bad: ! 86: error("Unknown Courier program.\n"); ! 87: syslog(LOG_ERR, "Unknown Courier program"); ! 88: exit(1); ! 89: } ! 90: ! 91: /* ! 92: * Catch alarm clock so read's will timeout. ! 93: */ ! 94: catch() ! 95: { ! 96: ! 97: longjmp(alarmbuf, 1); ! 98: } ! 99: ! 100: /* ! 101: * Write back a null-terminated error message. ! 102: */ ! 103: error(s) ! 104: char *s; ! 105: { ! 106: write(1, s, strlen(s)+1); ! 107: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.