]> diplodocus.org Git - mdeliver/blob - mdeliver.c
99eaec1825253392971d89a558ad22291f8abb0a
[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 <sysexits.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 #if !defined(HOST_NAME_MAX)
33 #if defined MAXHOSTNAMELEN
34 #define HOST_NAME_MAX MAXHOSTNAMELEN
35 #else
36 #define HOST_NAME_MAX 64
37 #endif
38 #endif
39
40 /* holds 2^128 - 1 in decimal, plus '\0' */
41 #define LEN_ULONG 40
42
43 /* 1 for '\0', 4 for "tmp/", HOST_NAME_MAX for the hostname, and two
44 * LEN_ULONGs for time and pid. */
45 #define TMPNAMLEN 1 + 4 + HOST_NAME_MAX + LEN_ULONG + LEN_ULONG
46
47 static char rcsid[] = "$Id$";
48
49 /* Write all of buf, even if write(2) is interrupted. */
50 static ssize_t
51 full_write(int fd, const char *buf, size_t nbytes)
52 {
53 ssize_t r, w = 0;
54
55 /* Loop until nbytes of buf have been written. */
56 while (w < nbytes) {
57 /* Keep trying until write succeeds without interruption. */
58 do {
59 r = write(fd, buf + w, nbytes - w);
60 } while (r < 0 && errno == EINTR);
61
62 if (r < 0) {
63 return r;
64 }
65
66 w += r;
67 }
68
69 return w;
70 }
71
72 static void
73 deliver(char *maildir, char *newfn)
74 {
75 char fn[TMPNAMLEN], host[HOST_NAME_MAX + 1];
76 char *buildfn; /* used to build up the fn */
77 time_t now;
78 struct tm *t;
79 pid_t pid;
80 struct stat statbuf;
81 int try;
82 int fd, r;
83 char buf[BUFSIZ];
84
85 if (chdir(maildir) != 0) {
86 err(EX_TEMPFAIL, "Failed chdir(%s)", maildir);
87 }
88
89 pid = getpid();
90 gethostname(host, sizeof(host));
91
92 for (try = 0; ; try++) {
93 now = time(NULL);
94 t = gmtime(&now);
95
96 buildfn = fn + sprintf(fn, "tmp/");
97 buildfn += strftime(buildfn, LEN_ULONG + 1, "%s.", t);
98 buildfn += snprintf(buildfn, LEN_ULONG + 1, "%d.", pid);
99 buildfn += sprintf(buildfn, "%s", host);
100
101 if (stat(fn, &statbuf) != 0) {
102 if (errno == ENOENT) {
103 break;
104 }
105 }
106
107 if (try == 2) {
108 errx(EX_TEMPFAIL, "Attempted filename exists.");
109 }
110
111 sleep(2);
112 }
113
114 fd = open(fn, O_WRONLY | O_EXCL | O_CREAT, 0600);
115 if (fd < 0) {
116 err(EX_TEMPFAIL, "Failed open(%s)", fn);
117 }
118
119 /* From this point, we have a tmp file, so if someting fails we
120 * must try to unlink it before bombing. */
121
122 while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0
123 || (r == -1 && errno == EINTR)) {
124 if (full_write(fd, buf, r) != r) {
125 unlink(fn);
126 err(EX_TEMPFAIL, "Failed write(%s)", fn);
127 }
128 }
129
130 if (r == -1) {
131 unlink(fn);
132 err(EX_TEMPFAIL, "Failed read from stdin");
133 }
134
135 if (fsync(fd) < 0) {
136 unlink(fn);
137 err(EX_TEMPFAIL, "Failed fsync(%s)", fn);
138 }
139 if (close(fd) < 0) {
140 unlink(fn);
141 err(EX_TEMPFAIL, "Failed close(%s)", fn);
142 }
143
144 strcpy(newfn, fn);
145 memcpy(newfn, "new", 3);
146
147 if (rename(fn, newfn) != 0) {
148 unlink(fn);
149 err(EX_TEMPFAIL, "Failed rename(%s, new)", fn);
150 }
151 }
152
153 int
154 main(int argc, char *argv[])
155 {
156 char *home;
157 char newfn[TMPNAMLEN];
158 pid_t pid;
159 FILE *errfile;
160 char *fail;
161 int fd;
162
163 if (argv[1] && argv[1][0] == '-' && argv[1][1] == 'v') {
164 puts(rcsid);
165 return 0;
166 }
167
168 if (!(home = getenv("HOME"))) {
169 err(EX_TEMPFAIL, "Failed getenv(HOME)");
170 }
171
172 if (chdir(home) != 0) {
173 err(EX_TEMPFAIL, "Failed chdir(HOME)");
174 }
175
176 deliver("Maildir", newfn);
177
178 if (argv[1]) {
179 if ((pid = fork()) > 0) {
180 return 0;
181 } else if (pid == 0) {
182 argv[0] = argv[1];
183 argv[1] = newfn;
184 execv(argv[0], argv);
185 fail = "exec";
186 } else {
187 fail = "fork";
188 }
189
190 /* If we got here fork or exec failed, so try to write an
191 * error message where later mail processors can find it. We
192 * really can't do anything about it if this fails. */
193 memcpy(newfn, "err", 3);
194 mkdir("err", 0700);
195 fd = open(newfn, O_WRONLY | O_EXCL | O_CREAT, 0644);
196 errfile = fdopen(fd, "w");
197 if (errfile) {
198 fprintf(errfile, "%s:%d\n", fail, errno);
199 }
200 }
201
202 /* Always return 0 because deliver exits if it fails, and if it
203 * succeeds we cannot return a failure code to the MTA. */
204 return 0;
205 }