]>
diplodocus.org Git - mdeliver/blob - mdeliver.c
7706aa5815e79f4a764bdbc78b1f1b27ff465cb3
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 /* Use this macro to perform some operation and saving the value errno
52 * had before that operation.
54 #define save_errno(op) { \
55 int olderrno = errno; \
60 static char rcsid
[] = "$Id$";
62 /* Write all of buf, even if write(2) is interrupted. */
64 full_write(int fd
, const char *buf
, size_t nbytes
)
68 /* Loop until nbytes of buf have been written. */
70 /* Keep trying until write succeeds without interruption. */
72 r
= write(fd
, buf
+ w
, nbytes
- w
);
73 } while (r
< 0 && errno
== EINTR
);
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).
90 get_filename(char *fn
)
93 char host
[HOST_NAME_MAX
+ 1];
97 char *buildfn
; /* used to build up the fn */
102 if (gethostname(host
, sizeof(host
)) != 0) {
103 err(MDELIVER_EXITCODE
, "Failed gethostname");
106 for (try = 0; ; try++) {
109 err(MDELIVER_EXITCODE
, "Failed time()");
114 err(MDELIVER_EXITCODE
, "Failed gmtime()");
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
);
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.
135 errx(MDELIVER_EXITCODE
, "Attempted filename exists.");
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<
150 /* buf is passed only as an (early, evil) space optimization). */
152 strip_from(int fd
, char *buf
, char *fn
)
156 r
= read(STDIN_FILENO
, buf
, 5);
158 save_errno( unlink(fn
) );
159 err(MDELIVER_EXITCODE
, "Failed read from stdin");
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') {
171 save_errno( unlink(fn
) );
172 err(MDELIVER_EXITCODE
, "Failed read from stdin");
175 if (full_write(fd
, buf
, r
) != r
) {
176 save_errno( unlink(fn
) );
177 err(MDELIVER_EXITCODE
, "Failed write(%s)", fn
);
182 /* Copy the message from stdin to fd. */
183 /* buf is passed only as an (early, evil) space optimization). */
185 copy_message(int fd
, char *buf
, char *fn
)
189 while ((r
= read(STDIN_FILENO
, buf
, BUFSIZ
)) > 0
190 || (r
== -1 && errno
== EINTR
)) {
191 if (full_write(fd
, buf
, r
) != r
) {
192 save_errno( unlink(fn
) );
193 err(MDELIVER_EXITCODE
, "Failed write(%s)", fn
);
198 save_errno( unlink(fn
) );
199 err(MDELIVER_EXITCODE
, "Failed read from stdin");
203 save_errno( unlink(fn
) );
204 err(MDELIVER_EXITCODE
, "Failed fsync(%s)", fn
);
208 save_errno( unlink(fn
) );
209 err(MDELIVER_EXITCODE
, "Failed close(%s)", fn
);
214 deliver(char *maildir
, char *newfn
)
218 char buf
[BUFSIZ
]; /* Allocated here only as an (early,
219 * evil) space optimization. */
221 if (chdir(maildir
) != 0) {
222 err(MDELIVER_EXITCODE
, "Failed chdir(%s)", maildir
);
227 fd
= open(fn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0600);
229 err(MDELIVER_EXITCODE
, "Failed open(%s)", fn
);
232 /* From this point, we have a tmp file, so if someting fails we
233 * must try to unlink it before bombing. */
235 strip_from(fd
, buf
, fn
);
236 copy_message(fd
, buf
, fn
);
239 memcpy(newfn
, "new", 3);
241 if (rename(fn
, newfn
) != 0) {
242 save_errno( unlink(fn
) );
243 err(MDELIVER_EXITCODE
, "Failed rename(%s, new)", fn
);
248 main(int argc
, char *argv
[])
251 char newfn
[TMPNAMLEN
];
258 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
263 if (!(home
= getenv("HOME"))) {
264 err(MDELIVER_EXITCODE
, "Failed getenv(HOME)");
267 if (chdir(home
) != 0) {
268 err(MDELIVER_EXITCODE
, "Failed chdir(HOME)");
271 deliver("Maildir", newfn
);
274 if ((pid
= fork()) > 0) {
276 } else if (pid
== 0) {
279 execv(argv
[0], argv
);
286 /* If we got here fork or exec failed, so try to write an
287 * error message where later mail processors can find it. We
288 * really can't do anything about it if this fails. */
289 memcpy(newfn
, "err", 3);
291 fd
= open(newfn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0644);
292 errfile
= fdopen(fd
, "w");
294 fprintf(errfile
, "%s:%d\n", fail
, olderrno
);
298 /* Always return 0 because deliver exits if it fails, and if it
299 * succeeds we cannot return a failure code to the MTA. */