1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1996, Gary J. Palmer 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer, 13 * verbatim and that no modifications are made prior to this 14 * point in the file. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34/* 35 * ctm_dequeue: Dequeue queued delta pieces and mail them. 36 * 37 * The pieces have already been packaged up as mail messages by ctm_smail, 38 * and will be simply passed to sendmail in alphabetical order. 39 */ 40 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <unistd.h> 45#include <fcntl.h> 46#include <sys/types.h> 47#include <sys/stat.h> 48#include <sys/wait.h> 49#include <fts.h> 50#include <limits.h> 51#include <errno.h> 52#include <paths.h> 53#include "error.h" 54#include "options.h" 55 56#define DEFAULT_NUM 1 /* Default number of pieces mailed per run. */ 57 58int fts_sort(const FTSENT * const *, const FTSENT * const *); 59int run_sendmail(int ifd); 60 61int 62main(int argc, char **argv) 63{ 64 char *log_file = NULL; 65 char *queue_dir = NULL; 66 char *list[2]; 67 int num_to_send = DEFAULT_NUM, chunk; 68 int fd; 69 FTS *fts; 70 FTSENT *ftsent; 71 int piece, npieces; 72 char filename[PATH_MAX]; 73 74 err_prog_name(argv[0]); 75 76 OPTIONS("[-l log] [-n num] queuedir") 77 NUMBER('n', num_to_send) 78 STRING('l', log_file) 79 ENDOPTS 80 81 if (argc != 2) 82 usage(); 83 84 if (log_file) 85 err_set_log(log_file); 86 87 queue_dir = argv[1]; 88 list[0] = queue_dir; 89 list[1] = NULL; 90 91 fts = fts_open(list, FTS_PHYSICAL|FTS_COMFOLLOW, fts_sort); 92 if (fts == NULL) 93 { 94 err("fts failed on `%s'", queue_dir); 95 exit(1); 96 } 97 98 ftsent = fts_read(fts); 99 if (ftsent == NULL || ftsent->fts_info != FTS_D) 100 { 101 err("not a directory: %s", queue_dir); 102 exit(1); 103 } 104 105 ftsent = fts_children(fts, 0); 106 if (ftsent == NULL && errno) 107 { 108 err("*ftschildren failed"); 109 exit(1); 110 } 111 112 for (chunk = 1; ftsent != NULL; ftsent = ftsent->fts_link) 113 { 114 /* 115 * Skip non-files and ctm_smail tmp files (ones starting with `.') 116 */ 117 if (ftsent->fts_info != FTS_F || ftsent->fts_name[0] == '.') 118 continue; 119 120 snprintf(filename, sizeof(filename), "%s/%s", queue_dir, 121 ftsent->fts_name); 122 fd = open(filename, O_RDONLY); 123 if (fd < 0) 124 { 125 err("*open: %s", filename); 126 exit(1); 127 } 128 129 if (run_sendmail(fd)) 130 exit(1); 131 132 close(fd); 133 134 if (unlink(filename) < 0) 135 { 136 err("*unlink: %s", filename); 137 exit(1); 138 } 139 140 /* 141 * Deduce the delta, piece number, and number of pieces from 142 * the name of the file in the queue. Ideally, we should be 143 * able to get the mail alias name too. 144 * 145 * NOTE: This depends intimately on the queue name used in ctm_smail. 146 */ 147 npieces = atoi(&ftsent->fts_name[ftsent->fts_namelen-3]); 148 piece = atoi(&ftsent->fts_name[ftsent->fts_namelen-7]); 149 err("%.*s %d/%d sent", (int)(ftsent->fts_namelen-8), ftsent->fts_name, 150 piece, npieces); 151 152 if (chunk++ == num_to_send) 153 break; 154 } 155 156 fts_close(fts); 157 158 return(0); 159} 160 161int 162fts_sort(const FTSENT * const * a, const FTSENT * const * b) 163{ 164 if ((*a)->fts_info != FTS_F) 165 return(0); 166 if ((*a)->fts_info != FTS_F) 167 return(0); 168 169 return (strcmp((*a)->fts_name, (*b)->fts_name)); 170} 171 172/* 173 * Run sendmail with the given file descriptor as standard input. 174 * Sendmail will decode the destination from the message contents. 175 * Returns 0 on success, 1 on failure. 176 */ 177int 178run_sendmail(int ifd) 179{ 180 pid_t child, pid; 181 int status; 182 183 switch (child = fork()) 184 { 185 case -1: 186 err("*fork"); 187 return(1); 188 189 case 0: /* Child */ 190 dup2(ifd, 0); 191 execl(_PATH_SENDMAIL, _PATH_SENDMAIL, "-odq", "-t", (char *)NULL); 192 err("*exec: %s", _PATH_SENDMAIL); 193 _exit(1); 194 195 default: /* Parent */ 196 while ((pid = wait(&status)) != child) 197 { 198 if (pid == -1 && errno != EINTR) 199 { 200 err("*wait"); 201 return(1); 202 } 203 } 204 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 205 { 206 err("sendmail failed"); 207 return(1); 208 } 209 } 210 211 return(0); 212} 213