]>
diplodocus.org Git - mdeliver/blob - mdeliver.c
756a07c0413d663cc89d3d067cf8425bde1bf09f
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
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.
18 #include <sys/types.h>
31 #ifndef MDELIVER_EXITCODE
33 #define MDELIVER_EXITCODE EX_TEMPFAIL
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 /* 1 for '\0', 4 for "tmp/", HOST_NAME_MAX for the hostname, and two
48 * LEN_ULONGs for time and pid. */
49 #define TMPNAMLEN 1 + 4 + HOST_NAME_MAX + LEN_ULONG + LEN_ULONG
51 static char rcsid
[] = "$Id$";
53 /* Write all of buf, even if write(2) is interrupted. */
55 full_write(int fd
, const char *buf
, size_t nbytes
)
59 /* Loop until nbytes of buf have been written. */
61 /* Keep trying until write succeeds without interruption. */
63 r
= write(fd
, buf
+ w
, nbytes
- w
);
64 } while (r
< 0 && errno
== EINTR
);
76 /* Find a filename for the message according to the algorithm
77 * documented in the maildir(5) manual page from qmail
78 * (http://www.qmail.org/qmail-manual-html/man5/maildir.html).
81 get_filename(char *fn
)
84 char host
[HOST_NAME_MAX
+ 1];
88 char *buildfn
; /* used to build up the fn */
93 if (gethostname(host
, sizeof(host
)) != 0) {
94 err(MDELIVER_EXITCODE
, "Failed gethostname");
97 for (try = 0; ; try++) {
101 buildfn
= fn
+ sprintf(fn
, "tmp/");
102 buildfn
+= strftime(buildfn
, LEN_ULONG
+ 1, "%s.", t
);
103 buildfn
+= snprintf(buildfn
, LEN_ULONG
+ 1, "%d.", pid
);
104 buildfn
+= sprintf(buildfn
, "%s", host
);
106 if (stat(fn
, &statbuf
) != 0) {
107 if (errno
== ENOENT
) {
108 /* Every failure point in this program relies on errno
109 * describing what went wrong. This is the one
110 * failure we *want*, so it is important to restore
111 * errno to 0 (success) before returning.
119 errx(MDELIVER_EXITCODE
, "Attempted filename exists.");
126 /* Strip the non-RFC2822-compliant 'From ' line some busted-ass
127 * mailers (such as Postfix) add as the first line of input. This bit
128 * of historical gunk originated with Berkeley mbox files, which use
129 * this line to indicate the beginning of a message. Postfix should
130 * only add such a line when it is acting as an MDA and delivering to
131 * a Berkeley mbox; the data written to an external MDA should be the
132 * raw RFC2822 message. >sigh<
134 /* buf is passed only as an (early, evil) space optimization). */
136 strip_from(int fd
, char *buf
, char *fn
)
140 r
= read(STDIN_FILENO
, buf
, 5);
143 err(MDELIVER_EXITCODE
, "Failed read from stdin");
146 if (strncmp(buf
, "From ", 5) == 0) {
147 while ((r
= read(STDIN_FILENO
, buf
, 1)) > 0
148 || (r
== -1 && errno
== EINTR
)) {
149 if (buf
[0] == '\n') {
156 err(MDELIVER_EXITCODE
, "Failed read from stdin");
159 if (full_write(fd
, buf
, r
) != r
) {
161 err(MDELIVER_EXITCODE
, "Failed write(%s)", fn
);
166 /* Copy the message from stdin to fd. */
167 /* buf is passed only as an (early, evil) space optimization). */
169 copy_message(int fd
, char *buf
, char *fn
)
173 while ((r
= read(STDIN_FILENO
, buf
, BUFSIZ
)) > 0
174 || (r
== -1 && errno
== EINTR
)) {
175 if (full_write(fd
, buf
, r
) != r
) {
177 err(MDELIVER_EXITCODE
, "Failed write(%s)", fn
);
183 err(MDELIVER_EXITCODE
, "Failed read from stdin");
188 err(MDELIVER_EXITCODE
, "Failed fsync(%s)", fn
);
193 err(MDELIVER_EXITCODE
, "Failed close(%s)", fn
);
198 deliver(char *maildir
, char *newfn
)
202 char buf
[BUFSIZ
]; /* Allocated here only as an (early,
203 * evil) space optimization. */
205 if (chdir(maildir
) != 0) {
206 err(MDELIVER_EXITCODE
, "Failed chdir(%s)", maildir
);
211 fd
= open(fn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0600);
213 err(MDELIVER_EXITCODE
, "Failed open(%s)", fn
);
216 /* From this point, we have a tmp file, so if someting fails we
217 * must try to unlink it before bombing. */
219 strip_from(fd
, buf
, fn
);
220 copy_message(fd
, buf
, fn
);
223 memcpy(newfn
, "new", 3);
225 if (rename(fn
, newfn
) != 0) {
227 err(MDELIVER_EXITCODE
, "Failed rename(%s, new)", fn
);
232 main(int argc
, char *argv
[])
235 char newfn
[TMPNAMLEN
];
241 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
246 if (!(home
= getenv("HOME"))) {
247 err(MDELIVER_EXITCODE
, "Failed getenv(HOME)");
250 if (chdir(home
) != 0) {
251 err(MDELIVER_EXITCODE
, "Failed chdir(HOME)");
254 deliver("Maildir", newfn
);
257 if ((pid
= fork()) > 0) {
259 } else if (pid
== 0) {
262 execv(argv
[0], argv
);
268 /* If we got here fork or exec failed, so try to write an
269 * error message where later mail processors can find it. We
270 * really can't do anything about it if this fails. */
271 memcpy(newfn
, "err", 3);
273 fd
= open(newfn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0644);
274 errfile
= fdopen(fd
, "w");
276 fprintf(errfile
, "%s:%d\n", fail
, errno
);
280 /* Always return 0 because deliver exits if it fails, and if it
281 * succeeds we cannot return a failure code to the MTA. */