xref: /dragonfly/libexec/dma/local.c (revision 25a2db75)
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <paths.h>
43 #include <signal.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <syslog.h>
47 #include <unistd.h>
48 
49 #include "dma.h"
50 
51 static int
52 create_mbox(const char *name)
53 {
54 	struct sigaction sa, osa;
55 	pid_t child, waitchild;
56 	int status;
57 	int i;
58 	long maxfd;
59 	int e;
60 	int r = -1;
61 
62 	/*
63 	 * We need to enable SIGCHLD temporarily so that waitpid works.
64 	 */
65 	bzero(&sa, sizeof(sa));
66 	sa.sa_handler = SIG_DFL;
67 	sigaction(SIGCHLD, &sa, &osa);
68 
69 	do_timeout(100, 0);
70 
71 	child = fork();
72 	switch (child) {
73 	case 0:
74 		/* child */
75 		maxfd = sysconf(_SC_OPEN_MAX);
76 		if (maxfd == -1)
77 			maxfd = 1024;	/* what can we do... */
78 
79 		for (i = 3; i <= maxfd; ++i)
80 			close(i);
81 
82 		execl(LIBEXEC_PATH "/dma-mbox-create", "dma-mbox-create", name, NULL);
83 		syslog(LOG_ERR, "cannot execute "LIBEXEC_PATH"/dma-mbox-create: %m");
84 		exit(1);
85 
86 	default:
87 		/* parent */
88 		waitchild = waitpid(child, &status, 0);
89 
90 		e = errno;
91 
92 		do_timeout(0, 0);
93 
94 		if (waitchild == -1 && e == EINTR) {
95 			syslog(LOG_ERR, "hung child while creating mbox `%s': %m", name);
96 			break;
97 		}
98 
99 		if (waitchild == -1) {
100 			syslog(LOG_ERR, "child disappeared while creating mbox `%s': %m", name);
101 			break;
102 		}
103 
104 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
105 			syslog(LOG_ERR, "error creating mbox `%s'", name);
106 			break;
107 		}
108 
109 		/* success */
110 		r = 0;
111 		break;
112 
113 	case -1:
114 		/* error */
115 		syslog(LOG_ERR, "error creating mbox");
116 		break;
117 	}
118 
119 	sigaction(SIGCHLD, &osa, NULL);
120 
121 	return (r);
122 }
123 
124 int
125 deliver_local(struct qitem *it)
126 {
127 	char fn[PATH_MAX+1];
128 	char line[1000];
129 	const char *sender;
130 	const char *newline = "\n";
131 	size_t linelen;
132 	int tries = 0;
133 	int mbox;
134 	int error;
135 	int hadnl = 0;
136 	off_t mboxlen;
137 	time_t now = time(NULL);
138 
139 	error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, it->addr);
140 	if (error < 0 || (size_t)error >= sizeof(fn)) {
141 		syslog(LOG_NOTICE, "local delivery deferred: %m");
142 		return (1);
143 	}
144 
145 retry:
146 	/* wait for a maximum of 100s to get the lock to the file */
147 	do_timeout(100, 0);
148 
149 	/* don't use O_CREAT here, because we might be running as the wrong user. */
150 	mbox = open_locked(fn, O_WRONLY|O_APPEND);
151 	if (mbox < 0) {
152 		int e = errno;
153 
154 		do_timeout(0, 0);
155 
156 		switch (e) {
157 		case EACCES:
158 		case ENOENT:
159 			/*
160 			 * The file does not exist or we can't access it.
161 			 * Call dma-mbox-create to create it and fix permissions.
162 			 */
163 			if (tries > 0 || create_mbox(it->addr) != 0) {
164 				syslog(LOG_ERR, "local delivery deferred: can not create `%s'", fn);
165 				return (1);
166 			}
167 			++tries;
168 			goto retry;
169 
170 		case EINTR:
171 			syslog(LOG_NOTICE, "local delivery deferred: can not lock `%s'", fn);
172 			break;
173 
174 		default:
175 			syslog(LOG_NOTICE, "local delivery deferred: can not open `%s': %m", fn);
176 			break;
177 		}
178 		return (1);
179 	}
180 	do_timeout(0, 0);
181 
182 	mboxlen = lseek(mbox, 0, SEEK_END);
183 
184 	/* New mails start with \nFrom ...., unless we're at the beginning of the mbox */
185 	if (mboxlen == 0)
186 		newline = "";
187 
188 	/* If we're bouncing a message, claim it comes from MAILER-DAEMON */
189 	sender = it->sender;
190 	if (strcmp(sender, "") == 0)
191 		sender = "MAILER-DAEMON";
192 
193 	if (fseek(it->mailf, 0, SEEK_SET) != 0) {
194 		syslog(LOG_NOTICE, "local delivery deferred: can not seek: %m");
195 		goto out;
196 	}
197 
198 	error = snprintf(line, sizeof(line), "%sFrom %s\t%s", newline, sender, ctime(&now));
199 	if (error < 0 || (size_t)error >= sizeof(line)) {
200 		syslog(LOG_NOTICE, "local delivery deferred: can not write header: %m");
201 		goto out;
202 	}
203 	if (write(mbox, line, error) != error)
204 		goto wrerror;
205 
206 	while (!feof(it->mailf)) {
207 		if (fgets(line, sizeof(line), it->mailf) == NULL)
208 			break;
209 		linelen = strlen(line);
210 		if (linelen == 0 || line[linelen - 1] != '\n') {
211 			syslog(LOG_CRIT, "local delivery failed: corrupted queue file");
212 			snprintf(errmsg, sizeof(errmsg), "corrupted queue file");
213 			error = -1;
214 			goto chop;
215 		}
216 
217 		/*
218 		 * mboxro processing:
219 		 * - escape lines that start with "From " with a > sign.
220 		 * - be reversable by escaping lines that contain an arbitrary
221 		 *   number of > signs, followed by "From ", i.e. />*From / in regexp.
222 		 * - strict mbox processing only requires escaping after empty lines,
223 		 *   yet most MUAs seem to relax this requirement and will treat any
224 		 *   line starting with "From " as the beginning of a new mail.
225 		 */
226 		if ((!MBOX_STRICT || hadnl) &&
227 		    strncmp(&line[strspn(line, ">")], "From ", 5) == 0) {
228 			const char *gt = ">";
229 
230 			if (write(mbox, gt, 1) != 1)
231 				goto wrerror;
232 			hadnl = 0;
233 		} else if (strcmp(line, "\n") == 0) {
234 			hadnl = 1;
235 		} else {
236 			hadnl = 0;
237 		}
238 		if ((size_t)write(mbox, line, linelen) != linelen)
239 			goto wrerror;
240 	}
241 	close(mbox);
242 	return (0);
243 
244 wrerror:
245 	syslog(LOG_ERR, "local delivery failed: write error: %m");
246 	error = 1;
247 chop:
248 	if (ftruncate(mbox, mboxlen) != 0)
249 		syslog(LOG_WARNING, "error recovering mbox `%s': %m", fn);
250 out:
251 	close(mbox);
252 	return (error);
253 }
254