1996-07-01 20:54:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1996, Gary J. Palmer
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer,
|
|
|
|
* verbatim and that no modifications are made prior to this
|
|
|
|
* point in the file.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-02-22 16:15:28 +00:00
|
|
|
* $Id$
|
1996-07-01 20:54:11 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
/*
|
|
|
|
* ctm_dequeue: Dequeue queued delta pieces and mail them.
|
|
|
|
*
|
|
|
|
* The pieces have already been packaged up as mail messages by ctm_smail,
|
|
|
|
* and will be simply passed to sendmail in alphabetical order.
|
1996-07-01 20:54:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
1996-11-27 13:06:51 +00:00
|
|
|
#include <sys/wait.h>
|
1996-07-01 20:54:11 +00:00
|
|
|
#include <fts.h>
|
1996-11-27 13:06:51 +00:00
|
|
|
#include <limits.h>
|
1996-07-01 20:54:11 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include "error.h"
|
|
|
|
#include "options.h"
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
#define DEFAULT_NUM 1 /* Default number of pieces mailed per run. */
|
|
|
|
|
1996-07-01 20:54:11 +00:00
|
|
|
int fts_sort(const FTSENT **, const FTSENT **);
|
1996-11-27 13:06:51 +00:00
|
|
|
int run_sendmail(int ifd);
|
1996-07-01 20:54:11 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *log_file = NULL;
|
|
|
|
char *queue_dir = NULL;
|
|
|
|
char *list[2];
|
1996-11-27 13:06:51 +00:00
|
|
|
int num_to_send = DEFAULT_NUM, chunk;
|
|
|
|
int fd;
|
1996-07-01 20:54:11 +00:00
|
|
|
FTS *fts;
|
|
|
|
FTSENT *ftsent;
|
1996-11-27 13:06:51 +00:00
|
|
|
int piece, npieces;
|
|
|
|
char filename[PATH_MAX];
|
1996-07-01 20:54:11 +00:00
|
|
|
|
|
|
|
err_prog_name(argv[0]);
|
|
|
|
|
|
|
|
OPTIONS("[-l log] [-n num] queuedir")
|
|
|
|
NUMBER('n', num_to_send)
|
|
|
|
STRING('l', log_file)
|
1996-11-27 13:06:51 +00:00
|
|
|
ENDOPTS
|
1996-07-01 20:54:11 +00:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
1996-09-07 20:41:09 +00:00
|
|
|
if (log_file)
|
|
|
|
err_set_log(log_file);
|
|
|
|
|
1996-07-01 20:54:11 +00:00
|
|
|
queue_dir = argv[1];
|
|
|
|
list[0] = queue_dir;
|
|
|
|
list[1] = NULL;
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
fts = fts_open(list, FTS_PHYSICAL|FTS_COMFOLLOW, fts_sort);
|
1996-07-01 20:54:11 +00:00
|
|
|
if (fts == NULL)
|
|
|
|
{
|
|
|
|
err("fts failed on `%s'", queue_dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
ftsent = fts_read(fts);
|
|
|
|
if (ftsent == NULL || ftsent->fts_info != FTS_D)
|
1996-07-01 20:54:11 +00:00
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
err("not a directory: %s", queue_dir);
|
|
|
|
exit(1);
|
1996-07-01 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
ftsent = fts_children(fts, 0);
|
|
|
|
if (ftsent == NULL && errno)
|
1996-07-01 20:54:11 +00:00
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
err("*ftschildren failed");
|
1996-07-01 20:54:11 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
for (chunk = 1; ftsent != NULL; ftsent = ftsent->fts_link)
|
1996-07-01 20:54:11 +00:00
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
/*
|
|
|
|
* Skip non-files and ctm_smail tmp files (ones starting with `.')
|
|
|
|
*/
|
|
|
|
if (ftsent->fts_info != FTS_F || ftsent->fts_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sprintf(filename, "%s/%s", queue_dir, ftsent->fts_name);
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
1996-07-01 20:54:11 +00:00
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
err("*open: %s", filename);
|
1996-07-01 20:54:11 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
if (run_sendmail(fd))
|
1996-07-01 20:54:11 +00:00
|
|
|
exit(1);
|
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
close(fd);
|
1996-07-01 20:54:11 +00:00
|
|
|
|
1996-07-12 13:12:46 +00:00
|
|
|
if (unlink(filename) < 0)
|
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
err("*unlink: %s", filename);
|
1996-07-12 13:12:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-07-01 20:54:11 +00:00
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
/*
|
|
|
|
* Deduce the delta, piece number, and number of pieces from
|
|
|
|
* the name of the file in the queue. Ideally, we should be
|
|
|
|
* able to get the mail alias name too.
|
|
|
|
*
|
|
|
|
* NOTE: This depends intimately on the queue name used in ctm_smail.
|
|
|
|
*/
|
|
|
|
npieces = atoi(&ftsent->fts_name[ftsent->fts_namelen-3]);
|
|
|
|
piece = atoi(&ftsent->fts_name[ftsent->fts_namelen-7]);
|
|
|
|
err("%.*s %d/%d sent", ftsent->fts_namelen-8, ftsent->fts_name,
|
|
|
|
piece, npieces);
|
|
|
|
|
|
|
|
if (chunk++ == num_to_send)
|
1996-07-12 13:12:46 +00:00
|
|
|
break;
|
1996-07-01 20:54:11 +00:00
|
|
|
}
|
1996-07-12 13:12:46 +00:00
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
fts_close(fts);
|
|
|
|
|
1996-07-01 20:54:11 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fts_sort(const FTSENT ** a, const FTSENT ** b)
|
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
if ((*a)->fts_info != FTS_F)
|
|
|
|
return(0);
|
|
|
|
if ((*a)->fts_info != FTS_F)
|
|
|
|
return(0);
|
1996-07-01 20:54:11 +00:00
|
|
|
|
1996-11-27 13:06:51 +00:00
|
|
|
return (strcmp((*a)->fts_name, (*b)->fts_name));
|
1996-07-01 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-11-27 13:06:51 +00:00
|
|
|
* Run sendmail with the given file descriptor as standard input.
|
|
|
|
* Sendmail will decode the destination from the message contents.
|
|
|
|
* Returns 0 on success, 1 on failure.
|
1996-07-01 20:54:11 +00:00
|
|
|
*/
|
|
|
|
int
|
1996-11-27 13:06:51 +00:00
|
|
|
run_sendmail(int ifd)
|
1996-07-01 20:54:11 +00:00
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
pid_t child, pid;
|
1996-07-01 20:54:11 +00:00
|
|
|
int status;
|
1996-11-27 13:06:51 +00:00
|
|
|
|
|
|
|
switch (child = fork())
|
1996-07-01 20:54:11 +00:00
|
|
|
{
|
1996-11-27 13:06:51 +00:00
|
|
|
case -1:
|
|
|
|
err("*fork");
|
|
|
|
return(1);
|
|
|
|
|
|
|
|
case 0: /* Child */
|
|
|
|
dup2(ifd, 0);
|
|
|
|
execl(_PATH_SENDMAIL, _PATH_SENDMAIL, "-odq", "-t", NULL);
|
|
|
|
err("*exec: %s", _PATH_SENDMAIL);
|
|
|
|
_exit(1);
|
|
|
|
|
|
|
|
default: /* Parent */
|
|
|
|
while ((pid = wait(&status)) != child)
|
|
|
|
{
|
|
|
|
if (pid == -1 && errno != EINTR)
|
|
|
|
{
|
|
|
|
err("*wait");
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
{
|
|
|
|
err("sendmail failed");
|
|
|
|
return(1);
|
|
|
|
}
|
1996-07-01 20:54:11 +00:00
|
|
|
}
|
1996-11-27 13:06:51 +00:00
|
|
|
|
|
|
|
return(0);
|
1996-07-01 20:54:11 +00:00
|
|
|
}
|