]>
diplodocus.org Git - mdeliver/blob - mdeliver.c
44e7e1641c5f0631b0af9fe47ab53dac17baa2e3
1 /* mdeliver.c -- Deliver a message to a Maildir.
5 * This program is in the public domain.
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
14 #include <sys/types.h>
30 #define EX_TEMPFAIL 111
36 #if !defined(HOST_NAME_MAX)
37 #if defined MAXHOSTNAMELEN
38 #define HOST_NAME_MAX MAXHOSTNAMELEN
40 #define HOST_NAME_MAX 64
44 /* holds 2^128 - 1 in decimal, plus '\0' */
47 /* 4 for "tmp/", 2 for the two dots ('.'), HOST_NAME_MAX for the
48 * hostname, two LEN_ULONGs for time and pid, and 1 for '\0', . */
49 #define LEN_FILENAME 4 + 2 + HOST_NAME_MAX + LEN_ULONG + LEN_ULONG + 1
51 #define save_errno(op) { \
52 int olderrno = errno; \
56 #define try_unlink(fn) save_errno( unlink(fn) )
58 static char ident
[] = "$Id$";
60 #define err(...) _err(true, __LINE__, __VA_ARGS__)
61 #define errx(...) _err(false, __LINE__, __VA_ARGS__)
64 _err(bool want_strerror
, int lineno
, char *format
, ...)
69 fprintf(stderr
, "mdeliver:%d:", lineno
);
72 vfprintf(stderr
, format
, ap
);
77 fputs(strerror(olderrno
), stderr
);
85 full_sleep(unsigned seconds
)
88 seconds
= sleep(seconds
);
92 /* Write all of buf, even if write(2) is interrupted. */
94 full_write(int fd
, const char *buf
, size_t nbytes
)
98 /* Loop until nbytes of buf have been written. */
100 /* Keep trying until write succeeds without interruption. */
102 r
= write(fd
, buf
+ w
, nbytes
- w
);
103 } while (r
< 0 && errno
== EINTR
);
115 /* Find a filename for the message according to the algorithm
116 * documented in the maildir(5) manual page from qmail
117 * (http://www.qmail.org/qmail-manual-html/man5/maildir.html).
120 get_filename(char *fn
)
123 char host
[HOST_NAME_MAX
+ 1];
130 if (gethostname(host
, sizeof(host
)) != 0) {
133 /* http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html
134 * "it is unspecified whether the returned name is null-terminated" */
135 host
[sizeof(host
) - 1] = '\0';
137 for (try = 0; ; try++) {
143 sprintf(fn
, "tmp/%lu.%lu.%s",
144 (long unsigned)now
, (long unsigned)pid
, host
);
146 if (stat(fn
, &st
) != 0) {
147 if (errno
== ENOENT
) {
148 /* Every failure point in this program relies on errno
149 * describing what went wrong. This is the one
150 * failure we *want*, so it is important to restore
151 * errno to 0 (success) before returning.
159 errx("Attempted filename exists.");
167 /* Strip the non-RFC2822-compliant 'From ' line some busted-ass
168 * mailers (such as Postfix) add as the first line of input. This bit
169 * of historical gunk originated with Berkeley mbox files, which use
170 * this line to indicate the beginning of a message. Postfix should
171 * only add such a line when it is acting as an MDA and delivering to
172 * a Berkeley mbox; the data written to an external MDA should be the
173 * raw RFC2822 message. >sigh<
175 /* buf is passed only as an (early, evil) space optimization). */
177 strip_from(int fd
, char *buf
, char *fn
)
181 /* Yes, read(2) can be interrupted and read fewer bytes than
182 * requested. However, i'm only asking for five bytes here. I
183 * have tested this program extensively and it handles large
184 * volumes of mail correctly every day on my systems. This holds
185 * up my original opinion that expecting to be able to get five
186 * bytes safely is not an unreasonable demand. If i can't get
187 * five measly bytes, something is probably wrong, so bomb.
195 if (strncmp(buf
, "From ", 5) == 0) {
196 while ((r
= read(0, buf
, 1)) > 0
197 || (r
== -1 && errno
== EINTR
)) {
198 if (buf
[0] == '\n') {
208 if (full_write(fd
, buf
, r
) != r
) {
210 err("write(%s)", fn
);
216 /* Copy the file from INFD to OUTFD in blocks of BUFSIZE bytes (stored
217 * in BUF). BUF must point to a buffer of at least BUFSIZE bytes;
218 * we're not dealing with null-terminated strings here.
220 * Note that OUTFD is closed before returning; this is necessary to
221 * ensure the file was fully written on some filesystems, such as NFS.
224 copy_file(int infd
, int outfd
, char *buf
, size_t bufsize
, char *fn
)
228 while ((r
= read(infd
, buf
, bufsize
)) > 0
229 || (r
== -1 && errno
== EINTR
)) {
230 if (full_write(outfd
, buf
, r
) != r
) {
232 err("write(%s)", fn
);
238 err("read(%d, %d)", infd
, bufsize
);
241 if (fsync(outfd
) < 0) {
243 err("fsync(%s)", fn
);
246 if (close(outfd
) < 0) {
248 err("close(%s)", fn
);
253 deliver(char *maildir
, char *sub
, char *newfn
)
255 char fn
[LEN_FILENAME
];
257 char buf
[BUFSIZ
]; /* Allocated here only as an (early,
258 * evil) space optimization. */
260 if (chdir(maildir
) != 0) {
261 err("chdir(%s)", maildir
);
266 fd
= open(fn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0600);
271 /* From this point, we have a tmp file, so if someting fails we
272 * must try to unlink it before bombing. */
279 buf
= getenv("RPLINE");
281 if (full_write(fd
, buf
, len
) != len
) {
283 err("write(%s)", fn
);
285 buf
= getenv("DTLINE");
287 if (full_write(fd
, buf
, len
) != len
) {
289 err("write(%s)", fn
);
293 strip_from(fd
, buf
, fn
);
296 copy_file(0, fd
, buf
, BUFSIZ
, fn
);
299 memcpy(newfn
, sub
, 3);
301 if (link(fn
, newfn
) != 0) {
303 err("link(%s, %s)", fn
, newfn
);
308 /* Run a program to process the new message. */
310 run_processor(char *home
, char *newfn
)
313 char command
[PATH_MAX
];
319 if ((pid
= fork()) > 0) {
321 } else if (pid
== 0) {
322 snprintf(command
, sizeof(command
), "%s/%s",
323 home
, ".mdeliver-processor");
324 execl(command
, command
, newfn
, NULL
);
325 if (errno
== ENOENT
) {
326 /* Not having a processor is not an error. */
335 /* If we got here fork or exec failed, so try to write an error
336 * message where later mail processors can find it. We really
337 * can't do anything about it if this fails. */
338 memcpy(newfn
, "err", 3);
340 fd
= open(newfn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0644);
342 errfile
= fdopen(fd
, "w");
344 fprintf(errfile
, "mdeliver:mdeliver.c:%d:%s: %s\n",
345 __LINE__
, fail
, strerror(olderrno
));
352 if (fail
[0] == 'e') {
358 main(int argc
, char *argv
[])
361 char newfn
[LEN_FILENAME
];
364 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
369 if ((home
= getenv("HOME")) == NULL
) {
370 errx("HOME is not set.");
373 if (chdir(home
) != 0) {
374 err("chdir(%s)", home
);
377 if (stat(".mdeliver-processor", &st
) == 0) {
378 deliver("Maildir", "mdp", newfn
);
379 run_processor(home
, newfn
);
381 deliver("Maildir", "new", newfn
);