masqmail

annotate src/spool.c @ 54:907fee7c081a

changes probably introduced by a newer version of automake or thelike
author meillo@marmaro.de
date Sat, 29 May 2010 14:23:07 +0200
parents 0267fe9745d5
children 257a9e6d1a8e
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@10 241 DEBUG(6) debugf("after RT\n");
meillo@10 242 fprintf(out, "PR:%s\n", prot_names[msg->received_prot]);
meillo@10 243 if (msg->received_host != NULL)
meillo@10 244 fprintf(out, "RH:%s\n", msg->received_host);
meillo@0 245
meillo@10 246 if (msg->ident != NULL)
meillo@10 247 fprintf(out, "ID:%s\n", msg->ident);
meillo@0 248
meillo@10 249 if (msg->data_size >= 0)
meillo@10 250 fprintf(out, "DS: %d\n", msg->data_size);
meillo@0 251
meillo@10 252 if (msg->received_time > 0)
meillo@10 253 fprintf(out, "TR: %u\n", (int) (msg->received_time));
meillo@0 254
meillo@10 255 if (msg->warned_time > 0)
meillo@10 256 fprintf(out, "TW: %u\n", (int) (msg->warned_time));
meillo@0 257
meillo@10 258 DEBUG(6) debugf("after RH\n");
meillo@10 259 fprintf(out, "\n");
meillo@0 260
meillo@10 261 foreach(msg->hdr_list, node) {
meillo@10 262 header *hdr = (header *) (node->data);
meillo@10 263 fprintf(out, "HD:%s", hdr->header);
meillo@10 264 }
meillo@10 265 if (fflush(out) == EOF)
meillo@10 266 ok = FALSE;
meillo@10 267 else if (fdatasync(fileno(out)) != 0) {
meillo@10 268 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
meillo@10 269 ok = FALSE;
meillo@10 270 }
meillo@10 271 fclose(out);
meillo@10 272 if (ok) {
meillo@10 273 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
meillo@10 274 DEBUG(4) debugf("spool_file = %s\n", spool_file);
meillo@10 275 ok = (rename(tmp_file, spool_file) != -1);
meillo@10 276 g_free(spool_file);
meillo@10 277 }
meillo@10 278 } else {
meillo@10 279 logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno));
meillo@10 280 DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid());
meillo@10 281 ok = FALSE;
meillo@10 282 }
meillo@0 283
meillo@10 284 g_free(tmp_file);
meillo@0 285
meillo@10 286 return ok;
meillo@0 287 }
meillo@0 288
meillo@10 289 gboolean
meillo@10 290 spool_write(message * msg, gboolean do_write_data)
meillo@0 291 {
meillo@10 292 GList *list;
meillo@10 293 gchar *spool_file, *tmp_file;
meillo@10 294 FILE *out;
meillo@10 295 gboolean ok = TRUE;
meillo@10 296 uid_t saved_uid, saved_gid;
meillo@10 297 /* user can read/write, group can read, others cannot do anything: */
meillo@10 298 mode_t saved_mode = saved_mode = umask(026);
meillo@0 299
meillo@10 300 /* set uid and gid to the mail ids */
meillo@10 301 if (!conf.run_as_user) {
meillo@10 302 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
meillo@0 303 }
meillo@0 304
meillo@10 305 /* header spool: */
meillo@10 306 ok = spool_write_header(msg);
meillo@10 307
meillo@10 308 if (ok) {
meillo@10 309
meillo@10 310 if (do_write_data) {
meillo@10 311 /* data spool: */
meillo@10 312 tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
meillo@10 313 DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
meillo@10 314
meillo@10 315 if ((out = fopen(tmp_file, "w"))) {
meillo@10 316 fprintf(out, "%s\n", msg->uid);
meillo@10 317 for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
meillo@10 318 fprintf(out, "%s", (gchar *) (list->data));
meillo@10 319 }
meillo@10 320
meillo@10 321 /* possibly paranoid ;-) */
meillo@10 322 if (fflush(out) == EOF)
meillo@10 323 ok = FALSE;
meillo@10 324 else if (fdatasync(fileno(out)) != 0) {
meillo@10 325 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
meillo@10 326 ok = FALSE;
meillo@10 327 }
meillo@10 328 fclose(out);
meillo@10 329 if (ok) {
meillo@10 330 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
meillo@10 331 DEBUG(4) debugf("spool_file = %s\n", spool_file);
meillo@10 332 ok = (rename(tmp_file, spool_file) != -1);
meillo@10 333 g_free(spool_file);
meillo@10 334 }
meillo@10 335 } else {
meillo@10 336 logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno));
meillo@10 337 ok = FALSE;
meillo@10 338 }
meillo@10 339 g_free(tmp_file);
meillo@10 340 }
meillo@0 341 }
meillo@10 342
meillo@10 343 /* set uid and gid back */
meillo@10 344 if (!conf.run_as_user) {
meillo@10 345 set_euidgid(saved_uid, saved_gid, NULL, NULL);
meillo@0 346 }
meillo@0 347
meillo@10 348 umask(saved_mode);
meillo@0 349
meillo@10 350 return ok;
meillo@0 351 }
meillo@0 352
meillo@0 353 #define MAX_LOCKAGE 300
meillo@0 354
meillo@10 355 gboolean
meillo@10 356 spool_lock(gchar * uid)
meillo@0 357 {
meillo@10 358 uid_t saved_uid, saved_gid;
meillo@10 359 gchar *hitch_name;
meillo@10 360 gchar *lock_name;
meillo@10 361 gboolean ok = FALSE;
meillo@0 362
meillo@10 363 hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid());
meillo@10 364 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
meillo@0 365
meillo@10 366 /* set uid and gid to the mail ids */
meillo@10 367 if (!conf.run_as_user) {
meillo@10 368 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
meillo@10 369 }
meillo@0 370
meillo@10 371 ok = dot_lock(lock_name, hitch_name);
meillo@10 372 if (!ok)
meillo@10 373 logwrite(LOG_WARNING, "spool file %s is locked\n", uid);
meillo@0 374
meillo@10 375 /* set uid and gid back */
meillo@10 376 if (!conf.run_as_user) {
meillo@10 377 set_euidgid(saved_uid, saved_gid, NULL, NULL);
meillo@10 378 }
meillo@0 379
meillo@10 380 g_free(lock_name);
meillo@10 381 g_free(hitch_name);
meillo@0 382
meillo@10 383 return ok;
meillo@0 384 }
meillo@0 385
meillo@10 386 gboolean
meillo@10 387 spool_unlock(gchar * uid)
meillo@0 388 {
meillo@10 389 uid_t saved_uid, saved_gid;
meillo@10 390 gchar *lock_name;
meillo@0 391
meillo@10 392 /* set uid and gid to the mail ids */
meillo@10 393 if (!conf.run_as_user) {
meillo@10 394 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
meillo@10 395 }
meillo@0 396
meillo@10 397 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid);
meillo@10 398 dot_unlock(lock_name);
meillo@10 399 g_free(lock_name);
meillo@0 400
meillo@10 401 /* set uid and gid back */
meillo@10 402 if (!conf.run_as_user) {
meillo@10 403 set_euidgid(saved_uid, saved_gid, NULL, NULL);
meillo@10 404 }
meillo@10 405 return TRUE;
meillo@0 406 }
meillo@0 407
meillo@10 408 gboolean
meillo@10 409 spool_delete_all(message * msg)
meillo@0 410 {
meillo@10 411 uid_t saved_uid, saved_gid;
meillo@10 412 gchar *spool_file;
meillo@0 413
meillo@10 414 /* set uid and gid to the mail ids */
meillo@10 415 if (!conf.run_as_user) {
meillo@10 416 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
meillo@10 417 }
meillo@0 418
meillo@10 419 /* header spool: */
meillo@10 420 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
meillo@10 421 if (unlink(spool_file) != 0)
meillo@10 422 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
meillo@10 423 g_free(spool_file);
meillo@0 424
meillo@10 425 /* data spool: */
meillo@10 426 spool_file = g_strdup_printf("%s/input/%s-D", 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 /* set uid and gid back */
meillo@10 432 if (!conf.run_as_user) {
meillo@10 433 set_euidgid(saved_uid, saved_gid, NULL, NULL);
meillo@10 434 }
meillo@10 435 return TRUE;
meillo@0 436 }