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