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