]> diplodocus.org Git - mdeliver/blob - mdeliver.c
6d31a8219af662ef38e62f2858bc9c5d86f7b852
[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 get_filename(char *fn)
74 {
75 pid_t pid;
76 char host[HOST_NAME_MAX + 1];
77 int try;
78 time_t now;
79 struct tm *t;
80 char *buildfn; /* used to build up the fn */
81 struct stat statbuf;
82
83 pid = getpid();
84 gethostname(host, sizeof(host));
85
86 for (try = 0; ; try++) {
87 now = time(NULL);
88 t = gmtime(&now);
89
90 buildfn = fn + sprintf(fn, "tmp/");
91 buildfn += strftime(buildfn, LEN_ULONG + 1, "%s.", t);
92 buildfn += snprintf(buildfn, LEN_ULONG + 1, "%d.", pid);
93 buildfn += sprintf(buildfn, "%s", host);
94
95 if (stat(fn, &statbuf) != 0) {
96 if (errno == ENOENT) {
97 break;
98 }
99 }
100
101 if (try == 2) {
102 errx(EX_TEMPFAIL, "Attempted filename exists.");
103 }
104
105 sleep(2);
106 }
107 }
108
109 /* buf is passed only as an (early, evil) space optimization). */
110 static void
111 strip_from(int fd, char *buf, char *fn)
112 {
113 ssize_t r;
114
115 r = read(STDIN_FILENO, buf, 5);
116 if (r != 5) {
117 unlink(fn);
118 err(EX_TEMPFAIL, "Failed read from stdin");
119 }
120
121 if (strncmp(buf, "From ", 5)) {
122 while ((r = read(STDIN_FILENO, buf, 1)) > 0
123 || (r == -1 && errno == EINTR)) {
124 if (buf[0] == '\n') {
125 break;
126 }
127 }
128
129 if (r == -1) {
130 unlink(fn);
131 err(EX_TEMPFAIL, "Failed read from stdin");
132 }
133 } else {
134 if (full_write(fd, buf, r) != r) {
135 unlink(fn);
136 err(EX_TEMPFAIL, "Failed write(%s)", fn);
137 }
138 }
139 }
140
141 /* buf is passed only as an (early, evil) space optimization). */
142 static void
143 copy_message(int fd, char *buf, char *fn)
144 {
145 ssize_t r;
146
147 while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0
148 || (r == -1 && errno == EINTR)) {
149 if (full_write(fd, buf, r) != r) {
150 unlink(fn);
151 err(EX_TEMPFAIL, "Failed write(%s)", fn);
152 }
153 }
154
155 if (r == -1) {
156 unlink(fn);
157 err(EX_TEMPFAIL, "Failed read from stdin");
158 }
159
160 if (fsync(fd) < 0) {
161 unlink(fn);
162 err(EX_TEMPFAIL, "Failed fsync(%s)", fn);
163 }
164
165 if (close(fd) < 0) {
166 unlink(fn);
167 err(EX_TEMPFAIL, "Failed close(%s)", fn);
168 }
169 }
170
171 static void
172 deliver(char *maildir, char *newfn)
173 {
174 char fn[TMPNAMLEN];
175 int fd;
176 char buf[BUFSIZ]; /* Allocated here only as an (early,
177 * evil) space optimization. */
178
179 if (chdir(maildir) != 0) {
180 err(EX_TEMPFAIL, "Failed chdir(%s)", maildir);
181 }
182
183 get_filename(fn);
184
185 fd = open(fn, O_WRONLY | O_EXCL | O_CREAT, 0600);
186 if (fd < 0) {
187 err(EX_TEMPFAIL, "Failed open(%s)", fn);
188 }
189
190 /* From this point, we have a tmp file, so if someting fails we
191 * must try to unlink it before bombing. */
192
193 strip_from(fd, buf, fn);
194 copy_message(fd, buf, fn);
195
196 strcpy(newfn, fn);
197 memcpy(newfn, "new", 3);
198
199 if (rename(fn, newfn) != 0) {
200 unlink(fn);
201 err(EX_TEMPFAIL, "Failed rename(%s, new)", fn);
202 }
203 }
204
205 int
206 main(int argc, char *argv[])
207 {
208 char *home;
209 char newfn[TMPNAMLEN];
210 pid_t pid;
211 FILE *errfile;
212 char *fail;
213 int fd;
214
215 if (argv[1] && argv[1][0] == '-' && argv[1][1] == 'v') {
216 puts(rcsid);
217 return 0;
218 }
219
220 if (!(home = getenv("HOME"))) {
221 err(EX_TEMPFAIL, "Failed getenv(HOME)");
222 }
223
224 if (chdir(home) != 0) {
225 err(EX_TEMPFAIL, "Failed chdir(HOME)");
226 }
227
228 deliver("Maildir", newfn);
229
230 if (argv[1]) {
231 if ((pid = fork()) > 0) {
232 return 0;
233 } else if (pid == 0) {
234 argv[0] = argv[1];
235 argv[1] = newfn;
236 execv(argv[0], argv);
237 fail = "exec";
238 } else {
239 fail = "fork";
240 }
241
242 /* If we got here fork or exec failed, so try to write an
243 * error message where later mail processors can find it. We
244 * really can't do anything about it if this fails. */
245 memcpy(newfn, "err", 3);
246 mkdir("err", 0700);
247 fd = open(newfn, O_WRONLY | O_EXCL | O_CREAT, 0644);
248 errfile = fdopen(fd, "w");
249 if (errfile) {
250 fprintf(errfile, "%s:%d\n", fail, errno);
251 }
252 }
253
254 /* Always return 0 because deliver exits if it fails, and if it
255 * succeeds we cannot return a failure code to the MTA. */
256 return 0;
257 }