1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/* DSTART */
/* */
/* maildrop - mail delivery agent with filtering abilities */
/* */
/* Copyright 1998, Double Precision Inc. */
/* */
/* This program is distributed under the terms of the GNU General Public */
/* License. See COPYING for additional information. */
/* DEND */
#include <stdlib.h>
#include <sysexits.h>
#include <sys/types.h>
#include "autoconf.h"
#include "mywait.h"
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#include "dotlock.h"
#include "maildrop.h"
#include "mio.h"
#include "config.h"
#include "setgroupid.h"
static const char rcsid[]="$Id: dotlockmain.C 1.2 1998/06/19 01:54:26 mrsam Exp $";
static void help()
{
merr << "Usage: dotlock\n";
throw EX_TEMPFAIL;
}
static int run(int argc, char *argv[])
{
DotLock dl;
Mio mio;
int use_flock=0;
pid_t pid;
int wait_stat;
int argn;
argn=1;
if (argn < argc)
{
if ( strcmp(argv[argn], "-f") == 0)
{
use_flock=1;
++argn;
}
}
if (argc - argn < 2)
help();
#if RESET_GID
setgroupid(getgid());
#endif
setuid(getuid()); // Drop any setuid privileges.
if (use_flock)
{
if (mio.Open(argv[argn], O_CREAT | O_WRONLY, MAILBOX_MODE) < 0)
throw "Unable to open mailbox.\n";
int flockrc;
#if HAS_LOCKF
while ((flockrc=lockf(mio.fd(),F_LOCK,0)) < 0 && errno == EINTR)
;
#else
#if HAS_FLOCK
while ((flockrc=flock(mio.fd(), LOCK_EX)) < 0 && errno == EINTR)
;
#endif
#endif
if (flockrc < 0)
throw "flock() failed.\n";
}
else
{
dl.Lock(argv[argn]);
}
++argn;
if ( (pid=fork()) < 0)
throw "fork() failed.\n";
if (pid == 0)
{
static char msg[]="maildrop: unable to execute command.\n";
ExitTrap::onfork();
setgroupid(getgid()); // Just in case.
setuid(getuid());
execvp(argv[argn], argv+argn);
write(2, msg, sizeof(msg)-1);
_exit(100);
}
while ( wait(&wait_stat) != pid )
;
mio.Close();
dl.Unlock();
wait_stat= WIFEXITED(wait_stat) ? WEXITSTATUS(wait_stat):-1;
return (wait_stat);
}
int main(int argc, char *argv[])
{
_exit(Maildrop::trap(run, argc, argv));
exit (0);
}
|