]> diplodocus.org Git - mdeliver/blob - mdeliver.c
f36d682048d1b09332e1f4a68e7cb1c0a71a4e22
[mdeliver] / mdeliver.c
1 /* mdeliver.c -- Deliver a message to a Maildir.
2 *
3 * $Id$
4 *
5 * This program is in the public domain.
6 *
7 * mdeliver is a simple, robust MDA. It delivers a single message
8 * from stdin to the user's Maildir and executes (in the background) a
9 * single program with the message filename as the first argument.
10 * Additional arguments may be specified; see main() below for
11 * details.
12 *
13 * This program does not conform strictly to POSIX. However, the two
14 * nonstandard interfaces it relies upon (<err.h> and <sysexits.h>)
15 * are widespread and easily enough replaced if necessary.
16 */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20
21 #include <err.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #ifndef MDELIVER_EXITCODE
32 #include <sysexits.h>
33 #define MDELIVER_EXITCODE EX_TEMPFAIL
34 #endif
35
36 #if !defined(HOST_NAME_MAX)
37 #if defined MAXHOSTNAMELEN
38 #define HOST_NAME_MAX MAXHOSTNAMELEN
39 #else
40 #define HOST_NAME_MAX 64
41 #endif
42 #endif
43
44 /* holds 2^128 - 1 in decimal, plus '\0' */
45 #define LEN_ULONG 40
46
47 /* 1 for '\0', 4 for "tmp/", 2 for the two dots ('.'), HOST_NAME_MAX
48 * for the hostname, and two LEN_ULONGs for time and pid. */
49 #define TMPNAMLEN 1 + 4 + 2 + HOST_NAME_MAX + LEN_ULONG + LEN_ULONG
50
51 /* Use this macro to perform some operation and saving the value errno
52 * had before that operation.
53 */
54 #define save_errno(op) { \
55 int olderrno = errno; \
56 op; \
57 errno = olderrno; \
58 }
59
60 static char rcsid[] = "$Id$";
61
62 /* Write all of buf, even if write(2) is interrupted. */
63 static ssize_t
64 full_write(int fd, const char *buf, size_t nbytes)
65 {
66 ssize_t r, w = 0;
67
68 /* Loop until nbytes of buf have been written. */
69 while (w < nbytes) {
70 /* Keep trying until write succeeds without interruption. */
71 do {
72 r = write(fd, buf + w, nbytes - w);
73 } while (r < 0 && errno == EINTR);
74
75 if (r < 0) {
76 return r;
77 }
78
79 w += r;
80 }
81
82 return w;
83 }
84
85 /* Find a filename for the message according to the algorithm
86 * documented in the maildir(5) manual page from qmail
87 * (http://www.qmail.org/qmail-manual-html/man5/maildir.html).
88 */
89 static void
90 get_filename(char *fn)
91 {
92 pid_t pid;
93 char host[HOST_NAME_MAX + 1];
94 int try;
95 time_t now;
96 struct tm *t;
97 char *buildfn; /* used to build up the fn */
98 struct stat statbuf;
99
100 pid = getpid();
101
102 if (gethostname(host, sizeof(host)) != 0) {
103 err(MDELIVER_EXITCODE, "Failed gethostname");
104 }
105
106 for (try = 0; ; try++) {
107 now = time(NULL);
108 if (now == -1) {
109 err(MDELIVER_EXITCODE, "Failed time()");
110 }
111
112 t = gmtime(&now);
113 if (!t) {
114 err(MDELIVER_EXITCODE, "Failed gmtime()");
115 }
116
117 buildfn = fn + sprintf(fn, "tmp/");
118 buildfn += strftime(buildfn, LEN_ULONG + 1, "%s.", t);
119 buildfn += snprintf(buildfn, LEN_ULONG + 1, "%d.", pid);
120 buildfn += sprintf(buildfn, "%s", host);
121
122 if (stat(fn, &statbuf) != 0) {
123 if (errno == ENOENT) {
124 /* Every failure point in this program relies on errno
125 * describing what went wrong. This is the one
126 * failure we *want*, so it is important to restore
127 * errno to 0 (success) before returning.
128 */
129 errno = 0;
130 break;
131 }
132 }
133
134 if (try == 2) {
135 errx(MDELIVER_EXITCODE, "Attempted filename exists.");
136 }
137
138 sleep(2);
139 }
140 }
141
142 /* Strip the non-RFC2822-compliant 'From ' line some busted-ass
143 * mailers (such as Postfix) add as the first line of input. This bit
144 * of historical gunk originated with Berkeley mbox files, which use
145 * this line to indicate the beginning of a message. Postfix should
146 * only add such a line when it is acting as an MDA and delivering to
147 * a Berkeley mbox; the data written to an external MDA should be the
148 * raw RFC2822 message. >sigh<
149 */
150 /* buf is passed only as an (early, evil) space optimization). */
151 static void
152 strip_from(int fd, char *buf, char *fn)
153 {
154 ssize_t r;
155
156 r = read(STDIN_FILENO, buf, 5);
157 if (r != 5) {
158 save_errno( unlink(fn) );
159 err(MDELIVER_EXITCODE, "Failed read from stdin");
160 }
161
162 if (strncmp(buf, "From ", 5) == 0) {
163 while ((r = read(STDIN_FILENO, buf, 1)) > 0
164 || (r == -1 && errno == EINTR)) {
165 if (buf[0] == '\n') {
166 break;
167 }
168 }
169
170 if (r == -1) {
171 save_errno( unlink(fn) );
172 err(MDELIVER_EXITCODE, "Failed read from stdin");
173 }
174 } else {
175 if (full_write(fd, buf, r) != r) {
176 save_errno( unlink(fn) );
177 err(MDELIVER_EXITCODE, "Failed write(%s)", fn);
178 }
179 }
180 }
181
182 /* Copy the file from INFD to OUTFD in blocks of BUFSIZE bytes (stored
183 * in BUF). BUF must point to a buffer of at least BUFSIZE bytes;
184 * we're not dealing with null-terminated strings here.
185 *
186 * Note that OUTFD is closed before returning; this is necessary to
187 * ensure the file was fully written on some filesystems, such as NFS.
188 */
189 static void
190 copy_file(int infd, int outfd, char *buf, size_t bufsize, char *fn)
191 {
192 ssize_t r;
193
194 while ((r = read(infd, buf, bufsize)) > 0
195 || (r == -1 && errno == EINTR)) {
196 if (full_write(outfd, buf, r) != r) {
197 save_errno( unlink(fn) );
198 err(MDELIVER_EXITCODE, "Failed write(%s)", fn);
199 }
200 }
201
202 if (r == -1) {
203 save_errno( unlink(fn) );
204 err(MDELIVER_EXITCODE, "Failed read");
205 }
206
207 if (fsync(outfd) < 0) {
208 save_errno( unlink(fn) );
209 err(MDELIVER_EXITCODE, "Failed fsync(%s)", fn);
210 }
211
212 if (close(outfd) < 0) {
213 save_errno( unlink(fn) );
214 err(MDELIVER_EXITCODE, "Failed close(%s)", fn);
215 }
216 }
217
218 static void
219 deliver(char *maildir, char *newfn)
220 {
221 char fn[TMPNAMLEN];
222 int fd;
223 char buf[BUFSIZ]; /* Allocated here only as an (early,
224 * evil) space optimization. */
225
226 if (chdir(maildir) != 0) {
227 err(MDELIVER_EXITCODE, "Failed chdir(%s)", maildir);
228 }
229
230 get_filename(fn);
231
232 fd = open(fn, O_WRONLY | O_EXCL | O_CREAT, 0600);
233 if (fd < 0) {
234 err(MDELIVER_EXITCODE, "Failed open(%s)", fn);
235 }
236
237 /* From this point, we have a tmp file, so if someting fails we
238 * must try to unlink it before bombing. */
239
240 strip_from(fd, buf, fn);
241 copy_file(STDIN_FILENO, fd, buf, BUFSIZ, fn);
242
243 strcpy(newfn, fn);
244 memcpy(newfn, "new", 3);
245
246 if (rename(fn, newfn) != 0) {
247 save_errno( unlink(fn) );
248 err(MDELIVER_EXITCODE, "Failed rename(%s, new)", fn);
249 }
250 }
251
252 /* Run a program to process the new message. */
253 static void
254 run_processor(char *home, char *newfn)
255 {
256 pid_t pid;
257 char command[PATH_MAX];
258 FILE *errfile;
259 char *fail;
260 int olderrno;
261 int fd;
262
263 if ((pid = vfork()) > 0) {
264 return;
265 } else if (pid == 0) {
266 snprintf(command, PATH_MAX, "%s/%s", home, ".mdeliver-processor");
267 execl(command, command, newfn, NULL);
268 if (errno == ENOENT) {
269 /* Not having a processor is not an error. */
270 return;
271 }
272 fail = "exec";
273 } else {
274 fail = "fork";
275 }
276 olderrno = errno;
277
278 /* If we got here fork or exec failed, so try to write an error
279 * message where later mail processors can find it. We really
280 * can't do anything about it if this fails. */
281 memcpy(newfn, "err", 3);
282 mkdir("err", 0700);
283 fd = open(newfn, O_WRONLY | O_EXCL | O_CREAT, 0644);
284 if (fd >= 0) {
285 errfile = fdopen(fd, "w");
286 if (errfile) {
287 fprintf(errfile, "%s:%d\n", fail, olderrno);
288 fflush(errfile);
289 }
290 fsync(fd);
291 close(fd);
292 }
293 }
294
295 int
296 main(int argc, char *argv[])
297 {
298 char *home;
299 char newfn[TMPNAMLEN];
300
301 if (argv[1] && argv[1][0] == '-' && argv[1][1] == 'v') {
302 puts(rcsid);
303 return 0;
304 }
305
306 if (!(home = getenv("HOME"))) {
307 err(MDELIVER_EXITCODE, "Failed getenv(HOME)");
308 }
309
310 if (chdir(home) != 0) {
311 err(MDELIVER_EXITCODE, "Failed chdir(HOME)");
312 }
313
314 deliver("Maildir", newfn);
315
316 run_processor(home, newfn);
317
318 /* Always return 0 because deliver exits if it fails, and if it
319 * succeeds we cannot return a failure code to the MTA. */
320 return 0;
321 }