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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
** Copyright 2002-2006 Double Precision, Inc. See COPYING for
** distribution information.
*/
#include "config.h"
#include "liblock.h"
#include "mail.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifndef WEXITSTATUS
#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif
#ifndef WIFEXITED
#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif
#if HAVE_SYSEXITS_H
#include <sysexits.h>
#endif
#ifndef EX_TEMPFAIL
#define EX_TEMPFAIL 75
#endif
#define NTRIES_DEFAULT 60
#define DELAY 5
static int sig=0;
static int catch(int signum)
{
sig=1;
signal(SIGHUP, (void (*)(int))catch);
signal(SIGTERM, (void (*)(int))catch);
signal(SIGINT, (void (*)(int))catch);
return 0;
}
static int caught()
{
signal(SIGHUP, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGINT, SIG_DFL);
return sig;
}
int main(int argc, char **argv)
{
char **argvec;
int n;
int fd;
pid_t pid, pid2;
int waitstat;
struct ll_mail *p;
int ntries=NTRIES_DEFAULT;
int readonly=0;
int optchar;
while ((optchar=getopt(argc, argv, "+rt:")) != -1)
switch (optchar) {
case 'r':
readonly=1;
break;
case 't':
ntries=atoi(optarg);
if (ntries < 0)
{
fprintf(stderr, "%s: invalid argument to -t\n",
argv[0]);
exit(EX_TEMPFAIL);
}
ntries= (ntries / DELAY) + 1;
break;
default:
exit(1);
}
if (argc - optind < 2)
{
fprintf(stderr, "Usage: %s [-r] [-t time] mailfile program [args]...\n",
argv[0]);
exit(1);
}
if ((argvec=malloc(sizeof(char *) * (argc - optind + 1))) == NULL)
{
perror("malloc");
exit (1);
}
for (n=optind+1; n<argc; n++)
argvec[n-optind - 1]=argv[n];
argvec[n-optind-1]=NULL;
/* Create the mail file, if it doesn't exist */
if ((n=open(argv[optind], O_RDWR|O_CREAT, 0600)) >= 0)
close(n);
signal(SIGUSR2, SIG_IGN);
signal(SIGCHLD, SIG_DFL);
p=NULL;
for (n=0; n<ntries; sleep(DELAY), n++)
{
if (p)
ll_mail_free(p);
if ((p=ll_mail_alloc(argv[optind])) == NULL)
{
perror("malloc");
exit(1);
}
signal(SIGHUP, (void (*)(int))catch);
signal(SIGTERM, (void (*)(int))catch);
signal(SIGINT, (void (*)(int))catch);
if (ll_mail_lock(p) < 0)
{
if (errno == ENOENT)
break; /* Mail file gone? */
if (caught())
break;
continue;
}
if ((fd=ll_mail_open(p)) < 0)
{
if (!readonly || (fd=ll_mail_open_ro(p)) < 0)
{
if (caught())
break;
continue;
}
}
if ((pid=fork()) < 0)
{
perror("fork");
exit(1);
}
if (pid == 0)
{
if (setgid(getgid()) < 0 ||
setuid(getuid()) < 0)
{
perror("setuid/setgid");
exit(1);
}
(void)caught();
execvp(argvec[0], argvec);
perror(argvec[0]);
exit(1);
}
while ((pid2=wait(&waitstat)) != pid)
;
ll_mail_free(p);
if (WIFEXITED(waitstat))
exit(WEXITSTATUS(waitstat));
exit(EX_TEMPFAIL);
}
if (p)
ll_mail_free(p);
exit(EX_TEMPFAIL);
return (0);
}
|