From: CSRG Date: Tue, 1 Mar 1983 09:19:39 +0000 (-0800) Subject: BSD 4_1c_2 development X-Git-Tag: BSD-4_1c_2~321 X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/commitdiff_plain/bea246bcf89f3c808ff48d87c1667d7e0807f206 BSD 4_1c_2 development Work on file usr/src/new/courier/daemon/RCS/courierd.c,v Work on file usr/src/new/courier/daemon/RCS/Makefile,v Work on file usr/src/new/courier/daemon/Makefile Work on file usr/src/new/courier/daemon/courierd.c Synthesized-from: CSRG/cd1/4.1c.2 --- diff --git a/usr/src/new/courier/daemon/Makefile b/usr/src/new/courier/daemon/Makefile new file mode 100644 index 0000000000..365f517751 --- /dev/null +++ b/usr/src/new/courier/daemon/Makefile @@ -0,0 +1,10 @@ +OBJS = courierd.o +CFLAGS = -O +LIBS = -ljobs +DESTDIR = /etc + +courierd: $(OBJS) + $(CC) $(CFLAGS) -o courierd $(OBJS) $(LIBS) + +install: courierd + install -s courierd $(DESTDIR) diff --git a/usr/src/new/courier/daemon/RCS/Makefile,v b/usr/src/new/courier/daemon/RCS/Makefile,v new file mode 100644 index 0000000000..b194d11c75 --- /dev/null +++ b/usr/src/new/courier/daemon/RCS/Makefile,v @@ -0,0 +1,34 @@ +head 1.1; +access ; +symbols ; +locks ; +comment @@; + + +1.1 +date 83.03.01.16.19.00; author cooper; state Exp; +branches ; +next ; + + +desc +@Makefile for Courier daemon. +@ + + +1.1 +log +@Initial revision +@ +text +@OBJS = courierd.o +CFLAGS = -O +LIBS = -ljobs +DESTDIR = /etc + +courierd: $(OBJS) + $(CC) $(CFLAGS) -o courierd $(OBJS) $(LIBS) + +install: courierd + install -s courierd $(DESTDIR) +@ diff --git a/usr/src/new/courier/daemon/RCS/courierd.c,v b/usr/src/new/courier/daemon/RCS/courierd.c,v new file mode 100644 index 0000000000..cd7dd82277 --- /dev/null +++ b/usr/src/new/courier/daemon/RCS/courierd.c,v @@ -0,0 +1,149 @@ +head 1.1; +access ; +symbols ; +locks ; +comment @ * @; + + +1.1 +date 83.03.01.16.19.08; author cooper; state Exp; +branches ; +next ; + + +desc +@Courier daemon. +@ + + +1.1 +log +@Initial revision +@ +text +@/* + * Courier program instantiation server + * + * Listens on the well-known socket for Courier, as defined in /etc/services. + * When a connection is made, it reads the program name as a Courier string. + * The directory /usr/lib/courier is used as a database of program names; + * the executable program must either reside there or have a link there. + * The program is spawned with user and group ID equal to that of the + * executable file, and must write a single zero byte back to the caller to + * indicate successful instantiation. + * If instantiation was unsuccessful, the daemon will write back a null- + * terminated error message. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +main(argc, argv) + int argc; + char **argv; +{ + int f, s, pid, len; + struct sockaddr_in sin, from; + struct servent *srvp; + extern int errno; + + if (argc != 1) + fprintf(stderr, "Courier daemon: arguments ignored\n"); + srvp = getservbyname("courier", "tcp"); + if (srvp == 0) { + fprintf(stderr, "tcp/courier: unknown service\n"); + exit(1); + } + if (chdir("/usr/lib/courier")) { + perror("/usr/lib/courier"); + exit(1); + } + sin.sin_port = srvp->s_port; + f = socket(AF_INET, SOCK_STREAM, 0, 0); + if (f < 0) { + perror("Courier daemon: socket"); + exit(1); + } + sin.sin_family = AF_INET; + if (bind(f, &sin, sizeof(struct sockaddr_in), 0) < 0) { + perror("Courier daemon: bind"); + exit(1); + } + listen(f, 10); + for (;;) { + len = sizeof(from); + s = accept(f, &from, &len, 0); + if (s < 0) { + if (errno != EINTR) { + perror("Courier daemon: accept"); + sleep(1); + } + continue; + } + if ((pid = fork()) == 0) { + close(f); + instantiate(s); + } + close(s); + if (pid == -1) + error("Try again.\n"); + while(wait3(0, WNOHANG, 0) > 0) + continue; + } +} + +instantiate(f) + int f; +{ + Cardinal n, nbytes; + struct stat statbuf; + char name[200]; + + setpgrp(0, getpid()); + sigset(SIGHUP, SIG_DFL); + sigset(SIGINT, SIG_DFL); + sigset(SIGQUIT, SIG_DFL); + + dup2(f, 0); + dup2(f, 1); + close(f); + + alarm(60); + read(0, &n, sizeof(Cardinal)); + alarm(0); + + UnpackCardinal(&nbytes, &n); + /* + * Courier strings are always word-aligned, so if the byte count is odd, + * we must also read the padding byte. + */ + read(0, name, nbytes + (nbytes % 2)); + name[nbytes] = '\0'; + + if (name[0] == '/' || name[0] == '.' || stat(name, &statbuf) != 0) + goto bad; + setgroups(0, 0); + setregid(statbuf.st_gid, statbuf.st_gid); + setreuid(statbuf.st_uid, statbuf.st_uid); + execl(name, name, 0); +bad: + error("Unknown Courier program.\n"); + exit(1); +} + +/* + * Write back a null-terminated error message. + */ +error(s) + char *s; +{ + write(1, s, strlen(s)+1); +} +@ diff --git a/usr/src/new/courier/daemon/courierd.c b/usr/src/new/courier/daemon/courierd.c new file mode 100644 index 0000000000..f9f8d986dd --- /dev/null +++ b/usr/src/new/courier/daemon/courierd.c @@ -0,0 +1,125 @@ +/* + * Courier program instantiation server + * + * Listens on the well-known socket for Courier, as defined in /etc/services. + * When a connection is made, it reads the program name as a Courier string. + * The directory /usr/lib/courier is used as a database of program names; + * the executable program must either reside there or have a link there. + * The program is spawned with user and group ID equal to that of the + * executable file, and must write a single zero byte back to the caller to + * indicate successful instantiation. + * If instantiation was unsuccessful, the daemon will write back a null- + * terminated error message. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +main(argc, argv) + int argc; + char **argv; +{ + int f, s, pid, len; + struct sockaddr_in sin, from; + struct servent *srvp; + extern int errno; + + if (argc != 1) + fprintf(stderr, "Courier daemon: arguments ignored\n"); + srvp = getservbyname("courier", "tcp"); + if (srvp == 0) { + fprintf(stderr, "tcp/courier: unknown service\n"); + exit(1); + } + if (chdir("/usr/lib/courier")) { + perror("/usr/lib/courier"); + exit(1); + } + sin.sin_port = srvp->s_port; + f = socket(AF_INET, SOCK_STREAM, 0, 0); + if (f < 0) { + perror("Courier daemon: socket"); + exit(1); + } + sin.sin_family = AF_INET; + if (bind(f, &sin, sizeof(struct sockaddr_in), 0) < 0) { + perror("Courier daemon: bind"); + exit(1); + } + listen(f, 10); + for (;;) { + len = sizeof(from); + s = accept(f, &from, &len, 0); + if (s < 0) { + if (errno != EINTR) { + perror("Courier daemon: accept"); + sleep(1); + } + continue; + } + if ((pid = fork()) == 0) { + close(f); + instantiate(s); + } + close(s); + if (pid == -1) + error("Try again.\n"); + while(wait3(0, WNOHANG, 0) > 0) + continue; + } +} + +instantiate(f) + int f; +{ + Cardinal n, nbytes; + struct stat statbuf; + char name[200]; + + setpgrp(0, getpid()); + sigset(SIGHUP, SIG_DFL); + sigset(SIGINT, SIG_DFL); + sigset(SIGQUIT, SIG_DFL); + + dup2(f, 0); + dup2(f, 1); + close(f); + + alarm(60); + read(0, &n, sizeof(Cardinal)); + alarm(0); + + UnpackCardinal(&nbytes, &n); + /* + * Courier strings are always word-aligned, so if the byte count is odd, + * we must also read the padding byte. + */ + read(0, name, nbytes + (nbytes % 2)); + name[nbytes] = '\0'; + + if (name[0] == '/' || name[0] == '.' || stat(name, &statbuf) != 0) + goto bad; + setgroups(0, 0); + setregid(statbuf.st_gid, statbuf.st_gid); + setreuid(statbuf.st_uid, statbuf.st_uid); + execl(name, name, 0); +bad: + error("Unknown Courier program.\n"); + exit(1); +} + +/* + * Write back a null-terminated error message. + */ +error(s) + char *s; +{ + write(1, s, strlen(s)+1); +}