masqmail

view src/spool.c @ 31:0267fe9745d5

we should probably add an always-bcc feature
author meillo@marmaro.de
date Thu, 06 May 2010 13:02:40 +0200
parents f671821d8222
children 90644c204265
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 /* TODO: add support for always_bcc, i.e. a way to deliver
242 each outgoing mail to a configurable address. This
243 can be useful for archiving purposes.
244 address* always_bcc = create_address("always_bcc@localhost", TRUE/FALSE);
245 spool_write_rcpt(out, always_bcc);
246 */
247 DEBUG(6) debugf("after RT\n");
248 fprintf(out, "PR:%s\n", prot_names[msg->received_prot]);
249 if (msg->received_host != NULL)
250 fprintf(out, "RH:%s\n", msg->received_host);
252 if (msg->ident != NULL)
253 fprintf(out, "ID:%s\n", msg->ident);
255 if (msg->data_size >= 0)
256 fprintf(out, "DS: %d\n", msg->data_size);
258 if (msg->received_time > 0)
259 fprintf(out, "TR: %u\n", (int) (msg->received_time));
261 if (msg->warned_time > 0)
262 fprintf(out, "TW: %u\n", (int) (msg->warned_time));
264 DEBUG(6) debugf("after RH\n");
265 fprintf(out, "\n");
267 foreach(msg->hdr_list, node) {
268 header *hdr = (header *) (node->data);
269 fprintf(out, "HD:%s", hdr->header);
270 }
271 if (fflush(out) == EOF)
272 ok = FALSE;
273 else if (fdatasync(fileno(out)) != 0) {
274 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
275 ok = FALSE;
276 }
277 fclose(out);
278 if (ok) {
279 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
280 DEBUG(4) debugf("spool_file = %s\n", spool_file);
281 ok = (rename(tmp_file, spool_file) != -1);
282 g_free(spool_file);
283 }
284 } else {
285 logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno));
286 DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid());
287 ok = FALSE;
288 }
290 g_free(tmp_file);
292 return ok;
293 }
295 gboolean
296 spool_write(message * msg, gboolean do_write_data)
297 {
298 GList *list;
299 gchar *spool_file, *tmp_file;
300 FILE *out;
301 gboolean ok = TRUE;
302 uid_t saved_uid, saved_gid;
303 /* user can read/write, group can read, others cannot do anything: */
304 mode_t saved_mode = saved_mode = umask(026);
306 /* set uid and gid to the mail ids */
307 if (!conf.run_as_user) {
308 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
309 }
311 /* header spool: */
312 ok = spool_write_header(msg);
314 if (ok) {
316 if (do_write_data) {
317 /* data spool: */
318 tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
319 DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
321 if ((out = fopen(tmp_file, "w"))) {
322 fprintf(out, "%s\n", msg->uid);
323 for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
324 fprintf(out, "%s", (gchar *) (list->data));
325 }
327 /* possibly paranoid ;-) */
328 if (fflush(out) == EOF)
329 ok = FALSE;
330 else if (fdatasync(fileno(out)) != 0) {
331 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
332 ok = FALSE;
333 }
334 fclose(out);
335 if (ok) {
336 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
337 DEBUG(4) debugf("spool_file = %s\n", spool_file);
338 ok = (rename(tmp_file, spool_file) != -1);
339 g_free(spool_file);
340 }
341 } else {
342 logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno));
343 ok = FALSE;
344 }
345 g_free(tmp_file);
346 }
347 }
349 /* set uid and gid back */
350 if (!conf.run_as_user) {
351 set_euidgid(saved_uid, saved_gid, NULL, NULL);
352 }
354 umask(saved_mode);
356 return ok;
357 }
359 #define MAX_LOCKAGE 300
361 gboolean
362 spool_lock(gchar * uid)
363 {
364 uid_t saved_uid, saved_gid;
365 gchar *hitch_name;
366 gchar *lock_name;
367 gboolean ok = FALSE;
369 hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid());
370 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
372 /* set uid and gid to the mail ids */
373 if (!conf.run_as_user) {
374 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
375 }
377 ok = dot_lock(lock_name, hitch_name);
378 if (!ok)
379 logwrite(LOG_WARNING, "spool file %s is locked\n", uid);
381 /* set uid and gid back */
382 if (!conf.run_as_user) {
383 set_euidgid(saved_uid, saved_gid, NULL, NULL);
384 }
386 g_free(lock_name);
387 g_free(hitch_name);
389 return ok;
390 }
392 gboolean
393 spool_unlock(gchar * uid)
394 {
395 uid_t saved_uid, saved_gid;
396 gchar *lock_name;
398 /* set uid and gid to the mail ids */
399 if (!conf.run_as_user) {
400 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
401 }
403 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
404 dot_unlock(lock_name);
405 g_free(lock_name);
407 /* set uid and gid back */
408 if (!conf.run_as_user) {
409 set_euidgid(saved_uid, saved_gid, NULL, NULL);
410 }
411 return TRUE;
412 }
414 gboolean
415 spool_delete_all(message * msg)
416 {
417 uid_t saved_uid, saved_gid;
418 gchar *spool_file;
420 /* set uid and gid to the mail ids */
421 if (!conf.run_as_user) {
422 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
423 }
425 /* header spool: */
426 spool_file = g_strdup_printf("%s/input/%s-H", 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 /* data spool: */
432 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
433 if (unlink(spool_file) != 0)
434 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
435 g_free(spool_file);
437 /* set uid and gid back */
438 if (!conf.run_as_user) {
439 set_euidgid(saved_uid, saved_gid, NULL, NULL);
440 }
441 return TRUE;
442 }