masqmail

annotate src/spool.c @ 38:ac0233b5f6f8

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