masqmail

view src/spool.c @ 81:71ce3a1568e9

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