Back to index

courier  0.68.2
sendmail-esmtpd-wrapper.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2006 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "courier.h"
00007 #include      "bindir.h"
00008 #include      "sbindir.h"
00009 #include      "maxlongsize.h"
00010 #include      "numlib/numlib.h"
00011 #include      <sys/types.h>
00012 #include      <pwd.h>
00013 #if    HAVE_UNISTD_H
00014 #include      <unistd.h>
00015 #endif
00016 #include      <stdlib.h>
00017 #if    HAVE_FCNTL_H
00018 #include      <fcntl.h>
00019 #endif
00020 #if     HAVE_SYSEXITS_H
00021 #include        <sysexits.h>
00022 #else
00023 #define EX_NOUSER       67
00024 #define EX_TEMPFAIL     75
00025 #define EX_NOPERM       77
00026 #endif
00027 
00028 
00029 /*
00030        -bs flag to sendmail should invoke courieresmtpd, however courieresmtpd
00031        does not have world execute permissions (for a good reason).
00032 
00033        Therefore, we install sendmail-esmtpd-wrapper as a setuid root
00034        program.  sendmail-esmtpd-wrapper makes a note of who's running
00035        it, then sets its uid and gid to courier, then execs bin/courieresmtpd
00036        with a fresh environment.
00037 
00038        The fresh environment will have fake TCPREMOTEHOST and TCPREMOTEIP.
00039        Furthermore, TCPREMOTEINFO will be set to the identity of the
00040        invoking user.  This way, we can read the sender's identity in the
00041        headers.
00042 
00043        Also RELAYCLIENT is set, so we can relay :-)
00044 
00045        Additional, we run addcr to automatically add the CRs before the NLs,
00046        which courieresmtpd expects (!).
00047 */
00048 
00049 void   esmtpd()
00050 {
00051 uid_t orig_uid=getuid();
00052 char   ubuf[NUMBUFSIZE];
00053 char   remoteinfobuf[NUMBUFSIZE+30];
00054 char   *environ[6];
00055 char   *argv[2];
00056 int    pipefd[2];
00057 pid_t  pid;
00058 
00059        sprintf(remoteinfobuf, "TCPREMOTEINFO=uid %s",
00060               libmail_str_uid_t(orig_uid, ubuf));
00061 
00062        if (pipe(pipefd) < 0)
00063        {
00064               perror("pipe");
00065               exit(EX_TEMPFAIL);
00066        }
00067 
00068        if (chdir(courierdir()) < 0)
00069        {
00070               perror("chdir");
00071               exit(EX_TEMPFAIL);
00072        }
00073 
00074        pid=fork();
00075        if (pid < 0)
00076        {
00077               perror("fork");
00078               exit(EX_TEMPFAIL);
00079        }
00080        if (pid == 0)
00081        {
00082               dup2(pipefd[1], 1);
00083               close(pipefd[0]);
00084               close(pipefd[1]);
00085               execl(BINDIR "/addcr", "addcr", (char *)0);
00086               perror("exec");
00087               exit(EX_TEMPFAIL);
00088        }
00089        dup2(pipefd[0], 0);
00090        close(pipefd[0]);
00091        close(pipefd[1]);
00092        close(2); open("/dev/null", O_WRONLY); /* Pine temper tantrum */
00093 
00094        environ[0]="TCPREMOTEIP=127.0.0.1";
00095        environ[1]="TCPREMOTEHOST=localhost";
00096        environ[2]=remoteinfobuf;
00097        environ[3]="RELAYCLIENT=";
00098        environ[4]="FAXRELAYCLIENT=";
00099        environ[5]=0;
00100        argv[0]="sendmail";
00101        argv[1]=0;
00102        execve(SBINDIR "/courieresmtpd", argv, environ);
00103        perror("exec");
00104        exit(EX_TEMPFAIL);
00105 }