masqmail
view src/spool.c @ 49:58e8dd8bc90d
Added tag 0.2.22 for changeset f3bdb5b382ec
author | meillo@marmaro.de |
---|---|
date | Thu, 20 May 2010 21:23:19 +0200 |
parents | 0267fe9745d5 |
children | 257a9e6d1a8e |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include <sys/stat.h>
21 #include "masqmail.h"
22 #include "dotlock.h"
24 static gint
25 read_line(FILE * in, gchar * buf, gint buf_len)
26 {
27 gint p = 0;
28 gint c;
30 while ((c = getc(in)) != '\n' && (c != EOF)) {
31 if (p >= buf_len - 1) {
32 return 0;
33 }
34 buf[p++] = c;
35 }
37 if (c == EOF) {
38 return -1;
39 }
40 if ((p > 0) && (buf[p - 1] == '\r'))
41 p--;
42 buf[p++] = '\n';
43 buf[p] = '\0';
45 return p;
46 }
48 static void
49 spool_write_rcpt(FILE * out, address * rcpt)
50 {
51 gchar dlvrd_char = addr_is_delivered(rcpt) ? 'X' : (addr_is_failed(rcpt) ? 'F' : ' ');
53 if (rcpt->local_part[0] != '|') {
54 /* this is a paranoid check, in case it slipped through: */
55 /* if this happens, it is a bug */
56 if (rcpt->domain == NULL) {
57 logwrite(LOG_WARNING, "BUG: null domain for address %s, setting to %s\n", rcpt->local_part, conf.host_name);
58 logwrite(LOG_WARNING, "please report this bug.\n");
59 rcpt->domain = g_strdup(conf.host_name);
60 }
61 fprintf(out, "RT:%c%s\n", dlvrd_char, addr_string(rcpt));
62 } else {
63 fprintf(out, "RT:%c%s\n", dlvrd_char, rcpt->local_part);
64 }
65 }
67 static address*
68 spool_scan_rcpt(gchar * line)
69 {
70 address *rcpt = NULL;
72 if (line[3] != '\0') {
73 if (line[4] != '|') {
74 rcpt = create_address(&(line[4]), TRUE);
75 } else {
76 rcpt = create_address_pipe(&(line[4]));
77 }
78 if (line[3] == 'X') {
79 addr_mark_delivered(rcpt);
80 } else if (line[3] == 'F') {
81 addr_mark_failed(rcpt);
82 }
83 }
84 return rcpt;
85 }
87 gboolean
88 spool_read_data(message * msg)
89 {
90 FILE *in;
91 gboolean ok = FALSE;
92 gchar *spool_file;
94 DEBUG(5) debugf("spool_read_data entered\n");
95 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
96 DEBUG(5) debugf("reading data spool file '%s'\n", spool_file);
97 if ((in = fopen(spool_file, "r"))) {
98 char buf[MAX_DATALINE];
99 int len;
101 /* msg uid */
102 read_line(in, buf, MAX_DATALINE);
104 /* data */
105 msg->data_list = NULL;
106 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
107 msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf));
108 }
109 msg->data_list = g_list_reverse(msg->data_list);
110 fclose(in);
111 ok = TRUE;
112 } else
113 logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno));
114 return ok;
115 }
117 gboolean
118 spool_read_header(message * msg)
119 {
120 FILE *in;
121 gboolean ok = FALSE;
122 gchar *spool_file;
124 /* header spool: */
125 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
126 if ((in = fopen(spool_file, "r"))) {
127 header *hdr = NULL;
128 char buf[MAX_DATALINE];
129 int len;
131 /* msg uid */
132 read_line(in, buf, MAX_DATALINE);
134 /* envelope header */
135 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
136 if (buf[0] == '\n')
137 break;
138 else if (strncasecmp(buf, "MF:", 3) == 0) {
139 msg->return_path = create_address(&(buf[3]), TRUE);
140 DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address);
141 } else if (strncasecmp(buf, "RT:", 3) == 0) {
142 address *addr;
143 addr = spool_scan_rcpt(buf);
144 if (!addr_is_delivered(addr) && !addr_is_failed(addr)) {
145 msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
146 } else {
147 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr);
148 }
149 } else if (strncasecmp(buf, "PR:", 3) == 0) {
150 prot_id i;
151 for (i = 0; i < PROT_NUM; i++) {
152 if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) {
153 break;
154 }
155 }
156 msg->received_prot = i;
157 } else if (strncasecmp(buf, "RH:", 3) == 0) {
158 g_strchomp(buf);
159 msg->received_host = g_strdup(&(buf[3]));
160 } else if (strncasecmp(buf, "ID:", 3) == 0) {
161 g_strchomp(buf);
162 msg->ident = g_strdup(&(buf[3]));
163 } else if (strncasecmp(buf, "DS:", 3) == 0) {
164 msg->data_size = atoi(&(buf[3]));
165 } else if (strncasecmp(buf, "TR:", 3) == 0) {
166 msg->received_time = (time_t) (atoi(&(buf[3])));
167 } else if (strncasecmp(buf, "TW:", 3) == 0) {
168 msg->warned_time = (time_t) (atoi(&(buf[3])));
169 }
170 /* so far ignore other tags */
171 }
173 /* mail headers */
174 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
175 if (strncasecmp(buf, "HD:", 3) == 0) {
176 hdr = get_header(&(buf[3]));
177 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
178 } else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) {
179 char *tmp = hdr->header;
180 /* header continuation */
181 hdr->header = g_strconcat(hdr->header, buf, NULL);
182 hdr->value = hdr->header + (hdr->value - tmp);
183 } else
184 break;
185 }
186 fclose(in);
187 ok = TRUE;
188 } else
189 logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno));
190 return ok;
191 }
193 message*
194 msg_spool_read(gchar * uid, gboolean do_readdata)
195 {
196 message *msg;
197 gboolean ok = FALSE;
199 msg = create_message();
200 msg->uid = g_strdup(uid);
202 /* header spool: */
203 ok = spool_read_header(msg);
204 if (ok && do_readdata) {
205 /* data spool: */
206 ok = spool_read_data(msg);
207 }
208 return msg;
209 }
211 /* write header. uid and gid should already be set to the
212 mail ids. Better call spool_write(msg, FALSE).
213 */
214 static gboolean
215 spool_write_header(message * msg)
216 {
217 GList *node;
218 gchar *spool_file, *tmp_file;
219 FILE *out;
220 gboolean ok = TRUE;
222 /* header spool: */
223 tmp_file = g_strdup_printf("%s/input/%d-H.tmp", conf.spool_dir, getpid());
224 DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
226 if ((out = fopen(tmp_file, "w"))) {
227 DEBUG(6) debugf("opened tmp_file %s\n", tmp_file);
229 fprintf(out, "%s\n", msg->uid);
230 fprintf(out, "MF:%s\n", addr_string(msg->return_path));
232 DEBUG(6) debugf("after MF\n");
233 foreach(msg->rcpt_list, node) {
234 address *rcpt = (address *) (node->data);
235 spool_write_rcpt(out, rcpt);
236 }
237 foreach(msg->non_rcpt_list, node) {
238 address *rcpt = (address *) (node->data);
239 spool_write_rcpt(out, rcpt);
240 }
241 DEBUG(6) debugf("after RT\n");
242 fprintf(out, "PR:%s\n", prot_names[msg->received_prot]);
243 if (msg->received_host != NULL)
244 fprintf(out, "RH:%s\n", msg->received_host);
246 if (msg->ident != NULL)
247 fprintf(out, "ID:%s\n", msg->ident);
249 if (msg->data_size >= 0)
250 fprintf(out, "DS: %d\n", msg->data_size);
252 if (msg->received_time > 0)
253 fprintf(out, "TR: %u\n", (int) (msg->received_time));
255 if (msg->warned_time > 0)
256 fprintf(out, "TW: %u\n", (int) (msg->warned_time));
258 DEBUG(6) debugf("after RH\n");
259 fprintf(out, "\n");
261 foreach(msg->hdr_list, node) {
262 header *hdr = (header *) (node->data);
263 fprintf(out, "HD:%s", hdr->header);
264 }
265 if (fflush(out) == EOF)
266 ok = FALSE;
267 else if (fdatasync(fileno(out)) != 0) {
268 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
269 ok = FALSE;
270 }
271 fclose(out);
272 if (ok) {
273 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
274 DEBUG(4) debugf("spool_file = %s\n", spool_file);
275 ok = (rename(tmp_file, spool_file) != -1);
276 g_free(spool_file);
277 }
278 } else {
279 logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno));
280 DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid());
281 ok = FALSE;
282 }
284 g_free(tmp_file);
286 return ok;
287 }
289 gboolean
290 spool_write(message * msg, gboolean do_write_data)
291 {
292 GList *list;
293 gchar *spool_file, *tmp_file;
294 FILE *out;
295 gboolean ok = TRUE;
296 uid_t saved_uid, saved_gid;
297 /* user can read/write, group can read, others cannot do anything: */
298 mode_t saved_mode = saved_mode = umask(026);
300 /* set uid and gid to the mail ids */
301 if (!conf.run_as_user) {
302 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
303 }
305 /* header spool: */
306 ok = spool_write_header(msg);
308 if (ok) {
310 if (do_write_data) {
311 /* data spool: */
312 tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
313 DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
315 if ((out = fopen(tmp_file, "w"))) {
316 fprintf(out, "%s\n", msg->uid);
317 for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
318 fprintf(out, "%s", (gchar *) (list->data));
319 }
321 /* possibly paranoid ;-) */
322 if (fflush(out) == EOF)
323 ok = FALSE;
324 else if (fdatasync(fileno(out)) != 0) {
325 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
326 ok = FALSE;
327 }
328 fclose(out);
329 if (ok) {
330 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
331 DEBUG(4) debugf("spool_file = %s\n", spool_file);
332 ok = (rename(tmp_file, spool_file) != -1);
333 g_free(spool_file);
334 }
335 } else {
336 logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno));
337 ok = FALSE;
338 }
339 g_free(tmp_file);
340 }
341 }
343 /* set uid and gid back */
344 if (!conf.run_as_user) {
345 set_euidgid(saved_uid, saved_gid, NULL, NULL);
346 }
348 umask(saved_mode);
350 return ok;
351 }
353 #define MAX_LOCKAGE 300
355 gboolean
356 spool_lock(gchar * uid)
357 {
358 uid_t saved_uid, saved_gid;
359 gchar *hitch_name;
360 gchar *lock_name;
361 gboolean ok = FALSE;
363 hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid());
364 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
366 /* set uid and gid to the mail ids */
367 if (!conf.run_as_user) {
368 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
369 }
371 ok = dot_lock(lock_name, hitch_name);
372 if (!ok)
373 logwrite(LOG_WARNING, "spool file %s is locked\n", uid);
375 /* set uid and gid back */
376 if (!conf.run_as_user) {
377 set_euidgid(saved_uid, saved_gid, NULL, NULL);
378 }
380 g_free(lock_name);
381 g_free(hitch_name);
383 return ok;
384 }
386 gboolean
387 spool_unlock(gchar * uid)
388 {
389 uid_t saved_uid, saved_gid;
390 gchar *lock_name;
392 /* set uid and gid to the mail ids */
393 if (!conf.run_as_user) {
394 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
395 }
397 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
398 dot_unlock(lock_name);
399 g_free(lock_name);
401 /* set uid and gid back */
402 if (!conf.run_as_user) {
403 set_euidgid(saved_uid, saved_gid, NULL, NULL);
404 }
405 return TRUE;
406 }
408 gboolean
409 spool_delete_all(message * msg)
410 {
411 uid_t saved_uid, saved_gid;
412 gchar *spool_file;
414 /* set uid and gid to the mail ids */
415 if (!conf.run_as_user) {
416 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
417 }
419 /* header spool: */
420 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
421 if (unlink(spool_file) != 0)
422 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
423 g_free(spool_file);
425 /* data spool: */
426 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
427 if (unlink(spool_file) != 0)
428 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
429 g_free(spool_file);
431 /* set uid and gid back */
432 if (!conf.run_as_user) {
433 set_euidgid(saved_uid, saved_gid, NULL, NULL);
434 }
435 return TRUE;
436 }