masqmail

annotate src/spool.c @ 10:26e34ae9a3e3

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