]>
diplodocus.org Git - mdeliver/blob - mdeliver.c
528a348093f59a053cad077c609d304ca25d9c0a
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/", 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
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 ident
[] = "$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()");
112 if (gmtime_r(&now
, &t
) == NULL
) {
113 err(MDELIVER_EXITCODE
, "Failed gmtime_r()");
116 buildfn
= fn
+ sprintf(fn
, "tmp/");
117 buildfn
+= strftime(buildfn
, LEN_ULONG
+ 1, "%s.", &t
);
118 buildfn
+= snprintf(buildfn
, LEN_ULONG
+ 1, "%d.", pid
);
119 buildfn
+= sprintf(buildfn
, "%s", host
);
121 if (stat(fn
, &statbuf
) != 0) {
122 if (errno
== ENOENT
) {
123 /* Every failure point in this program relies on errno
124 * describing what went wrong. This is the one
125 * failure we *want*, so it is important to restore
126 * errno to 0 (success) before returning.
134 errx(MDELIVER_EXITCODE
, "Attempted filename exists.");
141 /* Strip the non-RFC2822-compliant 'From ' line some busted-ass
142 * mailers (such as Postfix) add as the first line of input. This bit
143 * of historical gunk originated with Berkeley mbox files, which use
144 * this line to indicate the beginning of a message. Postfix should
145 * only add such a line when it is acting as an MDA and delivering to
146 * a Berkeley mbox; the data written to an external MDA should be the
147 * raw RFC2822 message. >sigh<
149 /* buf is passed only as an (early, evil) space optimization). */
151 strip_from(int fd
, char *buf
, char *fn
)
155 /* Yes, read(2) can be interrupted and read fewer bytes than
156 * requested. However, i'm only asking for five bytes here. I
157 * have tested this program extensively and it handles large
158 * volumes of mail correctly every day on my systems. This holds
159 * up my original opinion that expecting to be able to get five
160 * bytes safely is not an unreasonable demand. If i can't get
161 * five measly bytes, something is probably wrong, so bomb.
163 r
= read(STDIN_FILENO
, buf
, 5);
165 save_errno( unlink(fn
) );
166 err(MDELIVER_EXITCODE
, "Failed read from stdin");
169 if (strncmp(buf
, "From ", 5) == 0) {
170 while ((r
= read(STDIN_FILENO
, buf
, 1)) > 0
171 || (r
== -1 && errno
== EINTR
)) {
172 if (buf
[0] == '\n') {
178 save_errno( unlink(fn
) );
179 err(MDELIVER_EXITCODE
, "Failed read from stdin");
182 if (full_write(fd
, buf
, r
) != r
) {
183 save_errno( unlink(fn
) );
184 err(MDELIVER_EXITCODE
, "Failed write(%s)", fn
);
189 /* Copy the file from INFD to OUTFD in blocks of BUFSIZE bytes (stored
190 * in BUF). BUF must point to a buffer of at least BUFSIZE bytes;
191 * we're not dealing with null-terminated strings here.
193 * Note that OUTFD is closed before returning; this is necessary to
194 * ensure the file was fully written on some filesystems, such as NFS.
197 copy_file(int infd
, int outfd
, char *buf
, size_t bufsize
, char *fn
)
201 while ((r
= read(infd
, buf
, bufsize
)) > 0
202 || (r
== -1 && errno
== EINTR
)) {
203 if (full_write(outfd
, buf
, r
) != r
) {
204 save_errno( unlink(fn
) );
205 err(MDELIVER_EXITCODE
, "Failed write(%s)", fn
);
210 save_errno( unlink(fn
) );
211 err(MDELIVER_EXITCODE
, "Failed read");
214 if (fsync(outfd
) < 0) {
215 save_errno( unlink(fn
) );
216 err(MDELIVER_EXITCODE
, "Failed fsync(%s)", fn
);
219 if (close(outfd
) < 0) {
220 save_errno( unlink(fn
) );
221 err(MDELIVER_EXITCODE
, "Failed close(%s)", fn
);
226 deliver(char *maildir
, char *newfn
)
230 char buf
[BUFSIZ
]; /* Allocated here only as an (early,
231 * evil) space optimization. */
233 if (chdir(maildir
) != 0) {
234 err(MDELIVER_EXITCODE
, "Failed chdir(%s)", maildir
);
239 fd
= open(fn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0600);
241 err(MDELIVER_EXITCODE
, "Failed open(%s)", fn
);
244 /* From this point, we have a tmp file, so if someting fails we
245 * must try to unlink it before bombing. */
247 strip_from(fd
, buf
, fn
);
248 copy_file(STDIN_FILENO
, fd
, buf
, BUFSIZ
, fn
);
251 memcpy(newfn
, "new", 3);
253 tmpfd
= open(newfn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0600);
255 /* XXX: what error message to use? */
256 err(MDELIVER_EXITCODE
, "Failed open(%s)", newfn
);
260 if (rename(fn
, newfn
) != 0) {
261 save_errno( unlink(newfn
) );
262 save_errno( unlink(fn
) );
263 err(MDELIVER_EXITCODE
, "Failed rename(%s, new)", fn
);
267 /* Run a program to process the new message. */
269 run_processor(char *home
, char *newfn
)
272 char command
[PATH_MAX
];
278 if ((pid
= vfork()) > 0) {
280 } else if (pid
== 0) {
281 snprintf(command
, PATH_MAX
, "%s/%s", home
, ".mdeliver-processor");
282 execl(command
, command
, newfn
, NULL
);
283 if (errno
== ENOENT
) {
284 /* Not having a processor is not an error. */
293 /* If we got here fork or exec failed, so try to write an error
294 * message where later mail processors can find it. We really
295 * can't do anything about it if this fails. */
296 memcpy(newfn
, "err", 3);
298 fd
= open(newfn
, O_WRONLY
| O_EXCL
| O_CREAT
, 0644);
300 errfile
= fdopen(fd
, "w");
302 fprintf(errfile
, "%s:%d\n", fail
, olderrno
);
313 main(int argc
, char *argv
[])
316 char newfn
[TMPNAMLEN
];
318 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
323 if (!(home
= getenv("HOME"))) {
324 err(MDELIVER_EXITCODE
, "Failed getenv(HOME)");
327 if (chdir(home
) != 0) {
328 err(MDELIVER_EXITCODE
, "Failed chdir(HOME)");
331 deliver("Maildir", newfn
);
333 run_processor(home
, newfn
);
335 /* Always return 0 because deliver exits if it fails, and if it
336 * succeeds we cannot return a failure code to the MTA. */