masqmail-0.2

view src/spool.c @ 12:9fb7ddbaf129

removed noop-code; some beautifying
author meillo@marmaro.de
date Wed, 29 Oct 2008 16:34:13 +0100
parents 08114f7dcc23
children f671821d8222
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 "masqmail.h"
20 #include <sys/stat.h>
21 #include "dotlock.h"
23 static gint
24 read_line(FILE * in, gchar * buf, gint buf_len)
25 {
26 gint p = 0;
27 gint c;
29 while ((c = getc(in)) != '\n' && (c != EOF)) {
30 if (p >= buf_len - 1) {
31 return 0;
32 }
33 buf[p++] = c;
34 }
36 if (c == EOF) {
37 return -1;
38 }
39 if ((p > 0) && (buf[p - 1] == '\r'))
40 p--;
41 buf[p++] = '\n';
42 buf[p] = 0;
44 return p;
45 }
47 static void
48 spool_write_rcpt(FILE * out, address * rcpt)
49 {
50 gchar dlvrd_char = addr_is_delivered(rcpt) ? 'X' : (addr_is_failed(rcpt) ? 'F' : ' ');
52 if (rcpt->local_part[0] != '|') {
53 /* this is a paranoid check, in case it slipped through: */
54 /* if this happens, it is a bug */
55 if (rcpt->domain == NULL) {
56 logwrite(LOG_WARNING, "BUG: null domain for address %s, setting to %s\n", rcpt->local_part, conf.host_name);
57 logwrite(LOG_WARNING, "please report this bug.\n");
58 rcpt->domain = g_strdup(conf.host_name);
59 }
60 fprintf(out, "RT:%c%s\n", dlvrd_char, addr_string(rcpt));
61 } else {
62 fprintf(out, "RT:%c%s\n", dlvrd_char, rcpt->local_part);
63 }
64 }
66 static address*
67 spool_scan_rcpt(gchar * line)
68 {
69 address *rcpt = NULL;
71 if (line[3] != 0) {
72 if (line[4] != '|') {
73 rcpt = create_address(&(line[4]), TRUE);
74 } else {
75 rcpt = create_address_pipe(&(line[4]));
76 }
77 if (line[3] == 'X') {
78 addr_mark_delivered(rcpt);
79 } else if (line[3] == 'F') {
80 addr_mark_failed(rcpt);
81 }
82 }
83 return rcpt;
84 }
86 gboolean
87 spool_read_data(message * msg)
88 {
89 FILE *in;
90 gboolean ok = FALSE;
91 gchar *spool_file;
93 DEBUG(5) debugf("spool_read_data entered\n");
94 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
95 DEBUG(5) debugf("reading data spool file '%s'\n", spool_file);
96 if ((in = fopen(spool_file, "r"))) {
97 char buf[MAX_DATALINE];
98 int len;
100 /* msg uid */
101 read_line(in, buf, MAX_DATALINE);
103 /* data */
104 msg->data_list = NULL;
105 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
106 msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf));
107 }
108 msg->data_list = g_list_reverse(msg->data_list);
109 fclose(in);
110 ok = TRUE;
111 } else
112 logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno));
113 return ok;
114 }
116 gboolean
117 spool_read_header(message * msg)
118 {
119 FILE *in;
120 gboolean ok = FALSE;
121 gchar *spool_file;
123 /* header spool: */
124 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
125 if ((in = fopen(spool_file, "r"))) {
126 header *hdr = NULL;
127 char buf[MAX_DATALINE];
128 int len;
130 /* msg uid */
131 read_line(in, buf, MAX_DATALINE);
133 /* envelope header */
134 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
135 if (buf[0] == '\n')
136 break;
137 else if (strncasecmp(buf, "MF:", 3) == 0) {
138 msg->return_path = create_address(&(buf[3]), TRUE);
139 DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address);
140 } else if (strncasecmp(buf, "RT:", 3) == 0) {
141 address *addr;
142 addr = spool_scan_rcpt(buf);
143 if (!addr_is_delivered(addr) && !addr_is_failed(addr)) {
144 msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
145 } else {
146 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr);
147 }
148 } else if (strncasecmp(buf, "PR:", 3) == 0) {
149 prot_id i;
150 for (i = 0; i < PROT_NUM; i++) {
151 if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) {
152 break;
153 }
154 }
155 msg->received_prot = i;
156 } else if (strncasecmp(buf, "RH:", 3) == 0) {
157 g_strchomp(buf);
158 msg->received_host = g_strdup(&(buf[3]));
159 } else if (strncasecmp(buf, "ID:", 3) == 0) {
160 g_strchomp(buf);
161 msg->ident = g_strdup(&(buf[3]));
162 } else if (strncasecmp(buf, "DS:", 3) == 0) {
163 msg->data_size = atoi(&(buf[3]));
164 } else if (strncasecmp(buf, "TR:", 3) == 0) {
165 msg->received_time = (time_t) (atoi(&(buf[3])));
166 } else if (strncasecmp(buf, "TW:", 3) == 0) {
167 msg->warned_time = (time_t) (atoi(&(buf[3])));
168 }
169 /* so far ignore other tags */
170 }
172 /* mail headers */
173 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
174 if (strncasecmp(buf, "HD:", 3) == 0) {
175 hdr = get_header(&(buf[3]));
176 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
177 } else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) {
178 char *tmp = hdr->header;
179 /* header continuation */
180 hdr->header = g_strconcat(hdr->header, buf, NULL);
181 hdr->value = hdr->header + (hdr->value - tmp);
182 } else
183 break;
184 }
185 fclose(in);
186 ok = TRUE;
187 } else
188 logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno));
189 return ok;
190 }
192 message*
193 msg_spool_read(gchar * uid, gboolean do_readdata)
194 {
195 message *msg;
196 gboolean ok = FALSE;
198 msg = create_message();
199 msg->uid = g_strdup(uid);
201 /* header spool: */
202 ok = spool_read_header(msg);
203 if (ok && do_readdata) {
204 /* data spool: */
205 ok = spool_read_data(msg);
206 }
207 return msg;
208 }
210 /* write header. uid and gid should already be set to the
211 mail ids. Better call spool_write(msg, FALSE).
212 */
213 static gboolean
214 spool_write_header(message * msg)
215 {
216 GList *node;
217 gchar *spool_file, *tmp_file;
218 FILE *out;
219 gboolean ok = TRUE;
221 /* header spool: */
222 tmp_file = g_strdup_printf("%s/input/%d-H.tmp", conf.spool_dir, getpid());
223 DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
225 if ((out = fopen(tmp_file, "w"))) {
226 DEBUG(6) debugf("opened tmp_file %s\n", tmp_file);
228 fprintf(out, "%s\n", msg->uid);
229 fprintf(out, "MF:%s\n", addr_string(msg->return_path));
231 DEBUG(6) debugf("after MF\n");
232 foreach(msg->rcpt_list, node) {
233 address *rcpt = (address *) (node->data);
234 spool_write_rcpt(out, rcpt);
235 }
236 foreach(msg->non_rcpt_list, node) {
237 address *rcpt = (address *) (node->data);
238 spool_write_rcpt(out, rcpt);
239 }
240 DEBUG(6) debugf("after RT\n");
241 fprintf(out, "PR:%s\n", prot_names[msg->received_prot]);
242 if (msg->received_host != NULL)
243 fprintf(out, "RH:%s\n", msg->received_host);
245 if (msg->ident != NULL)
246 fprintf(out, "ID:%s\n", msg->ident);
248 if (msg->data_size >= 0)
249 fprintf(out, "DS: %d\n", msg->data_size);
251 if (msg->received_time > 0)
252 fprintf(out, "TR: %u\n", (int) (msg->received_time));
254 if (msg->warned_time > 0)
255 fprintf(out, "TW: %u\n", (int) (msg->warned_time));
257 DEBUG(6) debugf("after RH\n");
258 fprintf(out, "\n");
260 foreach(msg->hdr_list, node) {
261 header *hdr = (header *) (node->data);
262 fprintf(out, "HD:%s", hdr->header);
263 }
264 if (fflush(out) == EOF)
265 ok = FALSE;
266 else if (fdatasync(fileno(out)) != 0) {
267 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
268 ok = FALSE;
269 }
270 fclose(out);
271 if (ok) {
272 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
273 DEBUG(4) debugf("spool_file = %s\n", spool_file);
274 ok = (rename(tmp_file, spool_file) != -1);
275 g_free(spool_file);
276 }
277 } else {
278 logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno));
279 DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid());
280 ok = FALSE;
281 }
283 g_free(tmp_file);
285 return ok;
286 }
288 gboolean
289 spool_write(message * msg, gboolean do_write_data)
290 {
291 GList *list;
292 gchar *spool_file, *tmp_file;
293 FILE *out;
294 gboolean ok = TRUE;
295 uid_t saved_uid, saved_gid;
296 /* user can read/write, group can read, others cannot do anything: */
297 mode_t saved_mode = saved_mode = umask(026);
299 /* set uid and gid to the mail ids */
300 if (!conf.run_as_user) {
301 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
302 }
304 /* header spool: */
305 ok = spool_write_header(msg);
307 if (ok) {
309 if (do_write_data) {
310 /* data spool: */
311 tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
312 DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
314 if ((out = fopen(tmp_file, "w"))) {
315 fprintf(out, "%s\n", msg->uid);
316 for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
317 fprintf(out, "%s", (gchar *) (list->data));
318 }
320 /* possibly paranoid ;-) */
321 if (fflush(out) == EOF)
322 ok = FALSE;
323 else if (fdatasync(fileno(out)) != 0) {
324 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
325 ok = FALSE;
326 }
327 fclose(out);
328 if (ok) {
329 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
330 DEBUG(4) debugf("spool_file = %s\n", spool_file);
331 ok = (rename(tmp_file, spool_file) != -1);
332 g_free(spool_file);
333 }
334 } else {
335 logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno));
336 ok = FALSE;
337 }
338 g_free(tmp_file);
339 }
340 }
342 /* set uid and gid back */
343 if (!conf.run_as_user) {
344 set_euidgid(saved_uid, saved_gid, NULL, NULL);
345 }
347 umask(saved_mode);
349 return ok;
350 }
352 #define MAX_LOCKAGE 300
354 gboolean
355 spool_lock(gchar * uid)
356 {
357 uid_t saved_uid, saved_gid;
358 gchar *hitch_name;
359 gchar *lock_name;
360 gboolean ok = FALSE;
362 hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid());
363 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
365 /* set uid and gid to the mail ids */
366 if (!conf.run_as_user) {
367 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
368 }
370 ok = dot_lock(lock_name, hitch_name);
371 if (!ok)
372 logwrite(LOG_WARNING, "spool file %s is locked\n", uid);
374 /* set uid and gid back */
375 if (!conf.run_as_user) {
376 set_euidgid(saved_uid, saved_gid, NULL, NULL);
377 }
379 g_free(lock_name);
380 g_free(hitch_name);
382 return ok;
383 }
385 gboolean
386 spool_unlock(gchar * uid)
387 {
388 uid_t saved_uid, saved_gid;
389 gchar *lock_name;
391 /* set uid and gid to the mail ids */
392 if (!conf.run_as_user) {
393 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
394 }
396 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
397 dot_unlock(lock_name);
398 g_free(lock_name);
400 /* set uid and gid back */
401 if (!conf.run_as_user) {
402 set_euidgid(saved_uid, saved_gid, NULL, NULL);
403 }
404 return TRUE;
405 }
407 gboolean
408 spool_delete_all(message * msg)
409 {
410 uid_t saved_uid, saved_gid;
411 gchar *spool_file;
413 /* set uid and gid to the mail ids */
414 if (!conf.run_as_user) {
415 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
416 }
418 /* header spool: */
419 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
420 if (unlink(spool_file) != 0)
421 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
422 g_free(spool_file);
424 /* data spool: */
425 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
426 if (unlink(spool_file) != 0)
427 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
428 g_free(spool_file);
430 /* set uid and gid back */
431 if (!conf.run_as_user) {
432 set_euidgid(saved_uid, saved_gid, NULL, NULL);
433 }
434 return TRUE;
435 }