masqmail

annotate src/deliver.c @ 405:0dd84c8f2524

Broke long lines.
author markus schnalke <meillo@marmaro.de>
date Wed, 29 Feb 2012 10:28:23 +0100
parents 7954b82040b3
children 8fedb93e9b72
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 1999-2002 Oliver Kurth
meillo@367 4 ** Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
meillo@367 5 **
meillo@367 6 ** This program is free software; you can redistribute it and/or modify
meillo@367 7 ** it under the terms of the GNU General Public License as published by
meillo@367 8 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 9 ** (at your option) any later version.
meillo@367 10 **
meillo@367 11 ** This program is distributed in the hope that it will be useful,
meillo@367 12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 14 ** GNU General Public License for more details.
meillo@367 15 **
meillo@367 16 ** You should have received a copy of the GNU General Public License
meillo@367 17 ** along with this program; if not, write to the Free Software
meillo@367 18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 19 */
meillo@0 20
meillo@0 21 #include <fnmatch.h>
meillo@0 22 #include <sysexits.h>
meillo@0 23 #include <netdb.h>
meillo@0 24
meillo@15 25 #include "masqmail.h"
meillo@15 26 #include "smtp_out.h"
meillo@15 27
meillo@367 28 /*
meillo@367 29 ** collect failed/defered rcpts for failure/warning messages
meillo@367 30 ** returns TRUE if either there are no failures or a failure message has
meillo@367 31 ** been successfully sent
meillo@367 32 */
meillo@10 33 gboolean
meillo@366 34 delivery_failures(message *msg, GList *rcpt_list, gchar *err_fmt, ...)
meillo@0 35 {
meillo@10 36 gboolean ok_fail = TRUE, ok_warn = TRUE;
meillo@10 37 time_t now = time(NULL);
meillo@0 38
meillo@10 39 GList *failed_list = NULL, *defered_list = NULL, *rcpt_node;
meillo@10 40 va_list args;
meillo@10 41 va_start(args, err_fmt);
meillo@0 42
meillo@10 43 foreach(rcpt_list, rcpt_node) {
meillo@10 44 address *rcpt = (address *) (rcpt_node->data);
meillo@10 45
meillo@10 46 if (addr_is_defered(rcpt)) {
meillo@403 47 if ((now - msg->received_time) >= conf.max_defer_time){
meillo@10 48 addr_mark_failed(rcpt);
meillo@280 49 } else {
meillo@403 50 defered_list = g_list_prepend(defered_list,
meillo@403 51 rcpt);
meillo@280 52 }
meillo@10 53 }
meillo@280 54 if (addr_is_failed(rcpt)) {
meillo@10 55 failed_list = g_list_prepend(failed_list, rcpt);
meillo@280 56 }
meillo@10 57 }
meillo@280 58 if (failed_list) {
meillo@403 59 ok_fail = fail_msg(msg, conf.errmsg_file, failed_list,
meillo@403 60 err_fmt, args);
meillo@10 61 g_list_free(failed_list);
meillo@10 62 }
meillo@280 63 if (defered_list) {
meillo@403 64 ok_warn = warn_msg(msg, conf.warnmsg_file, defered_list,
meillo@403 65 err_fmt, args);
meillo@10 66 g_list_free(defered_list);
meillo@10 67 }
meillo@10 68 va_end(args);
meillo@10 69 return ok_fail && ok_warn;
meillo@0 70 }
meillo@0 71
meillo@10 72 static gint
meillo@10 73 _g_list_strcasecmp(gconstpointer a, gconstpointer b)
meillo@0 74 {
meillo@10 75 return (gint) strcasecmp(a, b);
meillo@0 76 }
meillo@0 77
meillo@10 78 gboolean
meillo@403 79 deliver_local_mbox(message *msg, GList *hdr_list, address *rcpt,
meillo@403 80 address *env_addr)
meillo@280 81 {
meillo@280 82 DEBUG(1) debugf("attempting to deliver %s with mbox\n", msg->uid);
meillo@280 83 if (append_file(msg, hdr_list, rcpt->local_part)) {
meillo@280 84 if (env_addr != rcpt) {
meillo@403 85 logwrite(LOG_NOTICE, "%s => %s@%s <%s@%s> with mbox\n",
meillo@403 86 msg->uid, rcpt->local_part,
meillo@403 87 rcpt->domain, env_addr->local_part,
meillo@403 88 env_addr->domain);
meillo@280 89 } else {
meillo@403 90 logwrite(LOG_NOTICE, "%s => <%s@%s> with mbox\n",
meillo@403 91 msg->uid, rcpt->local_part,
meillo@403 92 rcpt->domain);
meillo@280 93 }
meillo@280 94 addr_mark_delivered(rcpt);
meillo@280 95 return TRUE;
meillo@280 96 }
meillo@280 97
meillo@280 98 /* prevents 'Resource temporarily unavailable (11)' */
meillo@280 99 if (errno != EAGAIN) {
meillo@280 100 addr_mark_failed(rcpt);
meillo@280 101 } else {
meillo@280 102 addr_mark_defered(rcpt);
meillo@280 103 }
meillo@280 104 return FALSE;
meillo@280 105 }
meillo@280 106
meillo@280 107 gboolean
meillo@403 108 deliver_local_pipe(message *msg, GList *hdr_list, address *rcpt,
meillo@403 109 address *env_addr)
meillo@280 110 {
meillo@403 111 guint flags = 0;
meillo@280 112
meillo@280 113 DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid);
meillo@280 114
meillo@403 115 flags |= (conf.pipe_fromline) ? MSGSTR_FROMLINE : 0;
meillo@280 116 flags |= (conf.pipe_fromhack) ? MSGSTR_FROMHACK : 0;
meillo@280 117 if (pipe_out(msg, hdr_list, rcpt, &(rcpt->local_part[1]), flags)) {
meillo@280 118 logwrite(LOG_NOTICE, "%s => %s <%s@%s> with pipe\n",
meillo@403 119 msg->uid, rcpt->local_part,
meillo@403 120 env_addr->local_part, env_addr->domain);
meillo@280 121 addr_mark_delivered(rcpt);
meillo@280 122 return TRUE;
meillo@280 123 }
meillo@280 124
meillo@280 125 if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) {
meillo@280 126 addr_mark_failed(rcpt);
meillo@280 127 } else {
meillo@280 128 addr_mark_defered(rcpt);
meillo@280 129 /* has no effect yet, except that mail remains in spool */
meillo@280 130 }
meillo@280 131 return FALSE;
meillo@280 132 }
meillo@280 133
meillo@280 134 gboolean
meillo@403 135 deliver_local_mda(message *msg, GList *hdr_list, address *rcpt,
meillo@403 136 address *env_addr)
meillo@280 137 {
meillo@280 138 gboolean ok = FALSE;
meillo@280 139 gchar *cmd = g_malloc(256);
meillo@280 140 GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt);
meillo@403 141 guint flags = 0;
meillo@280 142
meillo@280 143 DEBUG(1) debugf("attempting to deliver %s with mda\n", msg->uid);
meillo@280 144
meillo@280 145 if (!expand(var_table, conf.mda, cmd, 256)) {
meillo@280 146 logwrite(LOG_ALERT, "could not expand string %s\n", conf.mda);
meillo@280 147 destroy_table(var_table);
meillo@280 148 return FALSE;
meillo@280 149 }
meillo@280 150
meillo@403 151 flags |= (conf.mda_fromline) ? MSGSTR_FROMLINE : 0;
meillo@280 152 flags |= (conf.mda_fromhack) ? MSGSTR_FROMHACK : 0;
meillo@280 153 if (pipe_out(msg, hdr_list, rcpt, cmd, flags)) {
meillo@280 154 logwrite(LOG_NOTICE, "%s => %s@%s with mda (cmd = '%s')\n",
meillo@403 155 msg->uid, rcpt->local_part, rcpt->domain, cmd);
meillo@280 156 addr_mark_delivered(rcpt);
meillo@280 157 ok = TRUE;
meillo@280 158 } else if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) {
meillo@280 159 addr_mark_failed(rcpt);
meillo@280 160 } else {
meillo@280 161 addr_mark_defered(rcpt);
meillo@280 162 /* has no effect yet, except that mail remains in spool */
meillo@280 163 }
meillo@280 164
meillo@280 165 destroy_table(var_table);
meillo@280 166 return ok;
meillo@280 167 }
meillo@280 168
meillo@280 169 gboolean
meillo@366 170 deliver_local(msg_out *msgout)
meillo@0 171 {
meillo@10 172 message *msg = msgout->msg;
meillo@10 173 GList *rcpt_list = msgout->rcpt_list;
meillo@10 174 GList *rcpt_node;
meillo@280 175 gboolean ok = FALSE, flag = FALSE, ok_fail = FALSE;
meillo@0 176
meillo@10 177 DEBUG(5) debugf("deliver_local entered\n");
meillo@0 178
meillo@10 179 flag = (msg->data_list == NULL);
meillo@280 180 if (flag && !spool_read_data(msg)) {
meillo@403 181 logwrite(LOG_ALERT, "could not open data spool file for %s\n",
meillo@403 182 msg->uid);
meillo@280 183 return FALSE;
meillo@10 184 }
meillo@0 185
meillo@403 186 for (rcpt_node = g_list_first(rcpt_list); rcpt_node;
meillo@403 187 rcpt_node = g_list_next(rcpt_node)) {
meillo@10 188 GList *hdr_list;
meillo@10 189 address *rcpt = (address *) (rcpt_node->data);
meillo@10 190 address *env_addr = addr_find_ancestor(rcpt);
meillo@10 191 address *ret_path = msg->return_path;
meillo@10 192 header *retpath_hdr, *envto_hdr;
meillo@0 193
meillo@367 194 /*
meillo@367 195 ** we need a private copy of the hdr list because we add
meillo@367 196 ** headers here that belong to the rcpt only. g_list_copy
meillo@367 197 ** copies only the nodes, so it is safe to g_list_free it
meillo@367 198 */
meillo@10 199 hdr_list = g_list_copy(msg->hdr_list);
meillo@403 200 retpath_hdr = create_header(HEAD_ENVELOPE_TO,
meillo@403 201 "Envelope-to: %s\n", addr_string(env_addr));
meillo@403 202 envto_hdr = create_header(HEAD_RETURN_PATH,
meillo@403 203 "Return-path: %s\n", addr_string(ret_path));
meillo@0 204
meillo@10 205 hdr_list = g_list_prepend(hdr_list, envto_hdr);
meillo@10 206 hdr_list = g_list_prepend(hdr_list, retpath_hdr);
meillo@0 207
meillo@403 208 if (*rcpt->local_part == '|') {
meillo@367 209 /*
meillo@367 210 ** probably for expanded aliases, but why not done
meillo@367 211 ** like with the mda? //meillo 2010-12-06
meillo@367 212 */
meillo@403 213 if (deliver_local_pipe(msg, hdr_list, rcpt,
meillo@403 214 env_addr)) {
meillo@10 215 ok = TRUE;
meillo@10 216 }
meillo@10 217 } else {
meillo@403 218 /* figure out which mailbox type should be used
meillo@403 219 ** for this user */
meillo@10 220 gchar *user = rcpt->local_part;
meillo@10 221 gchar *mbox_type = conf.mbox_default;
meillo@0 222
meillo@403 223 if (g_list_find_custom(conf.mbox_users, user,
meillo@403 224 _g_list_strcasecmp)) {
meillo@10 225 mbox_type = "mbox";
meillo@403 226 } else if (g_list_find_custom (conf.mda_users, user,
meillo@403 227 _g_list_strcasecmp)) {
meillo@10 228 mbox_type = "mda";
meillo@280 229 }
meillo@10 230
meillo@403 231 if (strcmp(mbox_type, "mbox")==0) {
meillo@403 232 if (deliver_local_mbox(msg, hdr_list, rcpt,
meillo@403 233 env_addr)) {
meillo@280 234 ok = TRUE;
meillo@280 235 }
meillo@280 236 } else if (strcmp(mbox_type, "mda") == 0) {
meillo@280 237 if (conf.mda) {
meillo@403 238 if (deliver_local_mda(msg, hdr_list,
meillo@403 239 rcpt, env_addr)) {
meillo@280 240 ok = TRUE;
meillo@10 241 }
meillo@10 242 } else {
meillo@403 243 logwrite(LOG_ALERT, "mbox type is "
meillo@403 244 "mda, but no mda "
meillo@403 245 "command given in "
meillo@403 246 "configuration\n");
meillo@10 247 }
meillo@10 248
meillo@280 249 } else {
meillo@403 250 logwrite(LOG_ALERT, "unknown mbox type '%s'\n",
meillo@403 251 mbox_type);
meillo@280 252 }
meillo@10 253 }
meillo@0 254
meillo@10 255 destroy_header(retpath_hdr);
meillo@10 256 destroy_header(envto_hdr);
meillo@0 257
meillo@10 258 g_list_free(hdr_list);
meillo@10 259 }
meillo@403 260 ok_fail = delivery_failures(msg, rcpt_list, "%s (%d)",
meillo@403 261 ext_strerror(errno), errno);
meillo@0 262
meillo@280 263 if (flag) {
meillo@10 264 msg_free_data(msg);
meillo@280 265 }
meillo@280 266 if (ok || ok_fail) {
meillo@10 267 deliver_finish(msgout);
meillo@280 268 }
meillo@0 269
meillo@10 270 return ok;
meillo@0 271 }
meillo@0 272
meillo@367 273 /*
meillo@367 274 ** make a list of rcpt's of a message that are local
meillo@367 275 ** return a new copy of the list
meillo@367 276 */
meillo@10 277 void
meillo@367 278 msg_rcptlist_local(GList *rcpt_list, GList **p_local_list,
meillo@367 279 GList **p_nonlocal_list)
meillo@0 280 {
meillo@10 281 GList *rcpt_node;
meillo@0 282
meillo@10 283 foreach(rcpt_list, rcpt_node) {
meillo@10 284 address *rcpt = (address *) (rcpt_node->data);
meillo@10 285 GList *dom_node;
meillo@0 286
meillo@10 287 DEBUG(5) debugf("checking address %s\n", rcpt->address);
meillo@0 288
meillo@10 289 /* search for local host list: */
meillo@10 290 foreach(conf.local_hosts, dom_node) {
meillo@403 291 if (fnmatch(dom_node->data, rcpt->domain,
meillo@403 292 FNM_CASEFOLD)==0) {
meillo@403 293 *p_local_list = g_list_append(*p_local_list,
meillo@403 294 rcpt);
meillo@403 295 DEBUG(5) debugf("<%s@%s> is local\n",
meillo@403 296 rcpt->local_part,
meillo@403 297 rcpt->domain);
meillo@10 298 break;
meillo@10 299 } else {
meillo@403 300 *p_nonlocal_list = g_list_append(
meillo@403 301 *p_nonlocal_list, rcpt);
meillo@10 302 }
meillo@10 303 }
meillo@10 304 }
meillo@0 305 }
meillo@0 306
meillo@10 307 gboolean
meillo@370 308 deliver_msglist_host_pipe(connect_route *route, GList *msgout_list,
meillo@370 309 gchar *host, GList *res_list)
meillo@0 310 {
meillo@10 311 gboolean ok = TRUE;
meillo@10 312 GList *msgout_node;
meillo@0 313
meillo@10 314 DEBUG(5) debugf("deliver_msglist_host_pipe entered\n");
meillo@0 315
meillo@10 316 foreach(msgout_list, msgout_node) {
meillo@10 317 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 318 gboolean flag, ok_fail = FALSE;
meillo@10 319 message *msg = msgout->msg;
meillo@10 320 GList *rcpt_node, *rcpt_list = msgout->rcpt_list;
meillo@0 321
meillo@403 322 DEBUG(1) debugf("attempting to deliver %s with pipe\n",
meillo@403 323 msg->uid);
meillo@0 324
meillo@10 325 flag = (msg->data_list == NULL);
meillo@280 326 if (flag && !spool_read_data(msg)) {
meillo@403 327 logwrite(LOG_ALERT, "could not open data spool file "
meillo@403 328 "for %s\n", msg->uid);
meillo@280 329 continue;
meillo@10 330 }
meillo@0 331
meillo@10 332 ok = FALSE;
meillo@10 333 foreach(rcpt_list, rcpt_node) {
meillo@10 334 address *rcpt = (address *) (rcpt_node->data);
meillo@10 335 gchar *cmd = g_malloc(256);
meillo@403 336 GList *var_table = var_table_rcpt(var_table_msg(NULL,
meillo@403 337 msg), rcpt);
meillo@0 338
meillo@403 339 DEBUG(1) debugf("attempting to deliver %s to %s@%s "
meillo@403 340 "with pipe\n", msg->uid,
meillo@403 341 rcpt->local_part, rcpt->domain);
meillo@10 342
meillo@280 343 if (!expand(var_table, route->pipe, cmd, 256)) {
meillo@321 344 logwrite(LOG_ALERT, "could not expand string `%s'\n", route->pipe);
meillo@321 345 destroy_table(var_table);
meillo@321 346 continue;
meillo@321 347 }
meillo@321 348
meillo@321 349 if (pipe_out(msg, msg->hdr_list, rcpt, cmd, (route->pipe_fromline ? MSGSTR_FROMLINE : 0)
meillo@321 350 | (route->pipe_fromhack ? MSGSTR_FROMHACK : 0))) {
meillo@321 351 logwrite(LOG_NOTICE, "%s => %s@%s with pipe (cmd = '%s')\n",
meillo@321 352 msg->uid, rcpt->local_part, rcpt->domain, cmd);
meillo@321 353 addr_mark_delivered(rcpt);
meillo@321 354 ok = TRUE;
meillo@280 355 } else {
meillo@321 356 logwrite(LOG_ALERT, "pipe_out '%s' failed\n", route->pipe);
meillo@10 357
meillo@321 358 if (route->connect_error_fail) {
meillo@321 359 addr_mark_failed(rcpt);
meillo@10 360 } else {
meillo@321 361 addr_mark_defered(rcpt);
meillo@10 362 }
meillo@280 363 }
meillo@10 364
meillo@10 365 destroy_table(var_table);
meillo@10 366 }
meillo@10 367 ok_fail = delivery_failures(msg, rcpt_list, "%s", strerror(errno));
meillo@10 368
meillo@280 369 if (flag) {
meillo@10 370 msg_free_data(msg);
meillo@280 371 }
meillo@280 372 if (ok || ok_fail) {
meillo@10 373 deliver_finish(msgout);
meillo@280 374 }
meillo@0 375 }
meillo@0 376
meillo@10 377 return ok;
meillo@0 378 }
meillo@0 379
meillo@367 380 /*
meillo@367 381 ** deliver list of messages to one host and finishes them if the message was
meillo@367 382 ** delivered to at least one rcpt.
meillo@367 383 ** Returns TRUE if at least one msg was delivered to at least one rcpt.
meillo@0 384 */
meillo@10 385 gboolean
meillo@367 386 deliver_msglist_host_smtp(connect_route *route, GList *msgout_list,
meillo@367 387 gchar *host, GList *res_list)
meillo@0 388 {
meillo@10 389 gboolean ok = FALSE;
meillo@10 390 GList *msgout_node;
meillo@10 391 smtp_base *psb;
meillo@178 392 gint port = 25;
meillo@0 393
meillo@10 394 /* paranoid check: */
meillo@280 395 if (!msgout_list) {
meillo@10 396 logwrite(LOG_ALERT, "Ooops: empty list of messages in deliver_msglist_host()\n");
meillo@10 397 return FALSE;
meillo@10 398 }
meillo@10 399
meillo@280 400 if (!host) {
meillo@179 401 /* XXX: what if mail_host isn't set? Is this possible? */
meillo@10 402 host = route->mail_host->address;
meillo@10 403 port = route->mail_host->port;
meillo@178 404 }
meillo@10 405
meillo@280 406 if (route->wrapper) {
meillo@372 407 psb = smtp_out_open_child(route->wrapper);
meillo@372 408 if (psb) {
meillo@372 409 psb->remote_host = host;
meillo@372 410 }
meillo@280 411 } else {
meillo@280 412 psb = smtp_out_open(host, port, res_list);
meillo@280 413 }
meillo@0 414
meillo@280 415 if (!psb) {
meillo@10 416 /* smtp_out_open() failed */
meillo@10 417 foreach(msgout_list, msgout_node) {
meillo@10 418 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 419 GList *rcpt_node;
meillo@10 420
meillo@280 421 for (rcpt_node = g_list_first(msgout->rcpt_list);
meillo@404 422 rcpt_node;
meillo@404 423 rcpt_node = g_list_next(rcpt_node)) {
meillo@10 424 address *rcpt = (address *) (rcpt_node->data);
meillo@280 425 gboolean ret = FALSE;
meillo@10 426
meillo@10 427 addr_unmark_delivered(rcpt);
meillo@10 428 if (route->connect_error_fail) {
meillo@10 429 addr_mark_failed(rcpt);
meillo@10 430 } else {
meillo@10 431 addr_mark_defered(rcpt);
meillo@10 432 }
meillo@280 433 if (route->wrapper) {
meillo@404 434 ret = delivery_failures(msgout->msg,
meillo@404 435 msgout->rcpt_list,
meillo@404 436 "could not open "
meillo@404 437 "wrapper:\n\t%s",
meillo@404 438 strerror(errno));
meillo@280 439 } else {
meillo@404 440 ret = delivery_failures(msgout->msg,
meillo@404 441 msgout->rcpt_list,
meillo@404 442 "could not open "
meillo@404 443 "connection to %s:%d "
meillo@404 444 ":\n\t%s", host, port,
meillo@404 445 h_errno != 0 ?
meillo@404 446 hstrerror(h_errno) :
meillo@404 447 strerror(errno));
meillo@280 448 }
meillo@280 449 if (ret) {
meillo@10 450 deliver_finish(msgout);
meillo@280 451 }
meillo@10 452 }
meillo@10 453 }
meillo@280 454 return ok;
meillo@0 455 }
meillo@280 456
meillo@404 457 set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name,
meillo@404 458 route->do_correct_helo);
meillo@280 459
meillo@280 460 #ifdef ENABLE_AUTH
meillo@321 461 if (route->auth_name && route->auth_login && route->auth_secret) {
meillo@404 462 set_auth(psb, route->auth_name, route->auth_login,
meillo@404 463 route->auth_secret);
meillo@280 464 }
meillo@280 465 #endif
meillo@280 466 if (!smtp_out_init(psb, route->instant_helo)) {
meillo@280 467 /* smtp_out_init() failed */
meillo@404 468 if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) ||
meillo@404 469 (psb->error==smtp_syntax)) {
meillo@280 470 smtp_out_quit(psb);
meillo@280 471
meillo@280 472 foreach(msgout_list, msgout_node) {
meillo@404 473 msg_out *msgout =
meillo@404 474 (msg_out *)(msgout_node->data);
meillo@280 475 smtp_out_mark_rcpts(psb, msgout->rcpt_list);
meillo@280 476
meillo@404 477 if (delivery_failures(msgout->msg,
meillo@404 478 msgout->rcpt_list,
meillo@404 479 "while connected with %s, "
meillo@404 480 "the server replied\n\t%s",
meillo@404 481 (route->wrapper) ?
meillo@404 482 "<wrapper>" : host,
meillo@404 483 psb->buffer)) {
meillo@280 484 deliver_finish(msgout);
meillo@280 485 }
meillo@280 486 }
meillo@280 487 }
meillo@280 488 destroy_smtpbase(psb);
meillo@280 489 return ok;
meillo@280 490 }
meillo@280 491
meillo@280 492 if (!route->do_pipelining) {
meillo@280 493 psb->use_pipelining = FALSE;
meillo@280 494 }
meillo@280 495
meillo@280 496 foreach(msgout_list, msgout_node) {
meillo@280 497 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 498 gboolean flag, ok_msg = FALSE, ok_fail = FALSE;
meillo@280 499 message *msg = msgout->msg;
meillo@280 500
meillo@404 501 /* we may have to read the data at this point
meillo@404 502 ** and remember if we did */
meillo@280 503 flag = (msg->data_list == NULL);
meillo@280 504 if (flag && !spool_read_data(msg)) {
meillo@404 505 logwrite(LOG_ALERT, "could not open data spool "
meillo@404 506 "file %s\n", msg->uid);
meillo@280 507 break;
meillo@280 508 }
meillo@280 509
meillo@404 510 smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list,
meillo@404 511 msgout->hdr_list);
meillo@280 512
meillo@404 513 ok_fail = delivery_failures(msg, msgout->rcpt_list,
meillo@404 514 "while connected with %s, the server "
meillo@404 515 "replied\n\t%s", (route->wrapper) ?
meillo@404 516 "<wrapper>" : host, psb->buffer);
meillo@280 517
meillo@280 518 if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) {
meillo@280 519 /* connection lost */
meillo@280 520 break;
meillo@280 521 } else if (psb->error != smtp_ok) {
meillo@280 522 if (g_list_next(msgout_node) && !smtp_out_rset(psb)) {
meillo@280 523 break;
meillo@280 524 }
meillo@280 525 }
meillo@280 526 ok_msg = (psb->error == smtp_ok);
meillo@280 527
meillo@280 528 if (flag) {
meillo@280 529 msg_free_data(msg);
meillo@280 530 }
meillo@280 531 if (ok_msg) {
meillo@280 532 ok = TRUE;
meillo@280 533 }
meillo@280 534 if (ok_msg || ok_fail) {
meillo@280 535 deliver_finish(msgout);
meillo@280 536 }
meillo@280 537 }
meillo@404 538 if (psb->error == smtp_ok || (psb->error == smtp_fail) ||
meillo@404 539 (psb->error == smtp_trylater) ||
meillo@404 540 (psb->error == smtp_syntax)) {
meillo@280 541 smtp_out_quit(psb);
meillo@280 542 }
meillo@280 543 destroy_smtpbase(psb);
meillo@10 544 return ok;
meillo@0 545 }
meillo@0 546
meillo@10 547 gboolean
meillo@370 548 deliver_msglist_host(connect_route *route, GList *msgout_list, gchar *host,
meillo@370 549 GList *res_list)
meillo@0 550 {
meillo@0 551
meillo@311 552 if (route->pipe) {
meillo@311 553 DEBUG(5) debugf("with pipe\n");
meillo@404 554 return deliver_msglist_host_pipe(route, msgout_list,
meillo@404 555 host, res_list);
meillo@10 556 } else {
meillo@311 557 DEBUG(5) debugf("with smtp\n");
meillo@404 558 return deliver_msglist_host_smtp(route, msgout_list,
meillo@404 559 host, res_list);
meillo@10 560 }
meillo@0 561 }
meillo@0 562
meillo@0 563 /*
meillo@367 564 ** delivers messages in msgout_list using route
meillo@0 565 */
meillo@10 566 gboolean
meillo@366 567 deliver_route_msgout_list(connect_route *route, GList *msgout_list)
meillo@0 568 {
meillo@10 569 gboolean ok = FALSE;
meillo@280 570 GList *mo_ph_list;
meillo@280 571 GList *mo_ph_node;
meillo@0 572
meillo@404 573 DEBUG(5) debugf("deliver_route_msgout_list entered, route->name=%s\n",
meillo@404 574 route->name);
meillo@0 575
meillo@179 576 if (route->mail_host) {
meillo@404 577 /* easy: deliver everything to a smart host for relay */
meillo@370 578 return deliver_msglist_host(route, msgout_list, NULL,
meillo@370 579 route->resolve_list);
meillo@280 580 }
meillo@280 581
meillo@280 582 /* this is not easy... */
meillo@280 583
meillo@280 584 mo_ph_list = route_msgout_list(route, msgout_list);
meillo@280 585 /* okay, now we have ordered our messages by the hosts. */
meillo@280 586 if (!mo_ph_list) {
meillo@280 587 return FALSE;
meillo@280 588 }
meillo@280 589
meillo@367 590 /*
meillo@367 591 ** TODO: It would be nice to be able to fork for each host.
meillo@367 592 ** We cannot do that yet because of complications with finishing the
meillo@367 593 ** messages. Threads could be a solution because they use the same
meillo@367 594 ** memory. But we are not thread safe yet...
meillo@367 595 */
meillo@280 596 foreach(mo_ph_list, mo_ph_node) {
meillo@280 597 msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data);
meillo@370 598 if (deliver_msglist_host(route, mo_ph->msgout_list,
meillo@370 599 mo_ph->host, route->resolve_list)) {
meillo@10 600 ok = TRUE;
meillo@10 601 }
meillo@280 602 destroy_msgout_perhost(mo_ph);
meillo@10 603 }
meillo@280 604 g_list_free(mo_ph_list);
meillo@10 605 return ok;
meillo@0 606 }
meillo@0 607
meillo@0 608 /*
meillo@367 609 ** calls route_prepare_msg()
meillo@367 610 ** delivers messages in msg_list using route by calling
meillo@367 611 ** deliver_route_msgout_list()
meillo@0 612 */
meillo@10 613 gboolean
meillo@366 614 deliver_route_msg_list(connect_route *route, GList *msgout_list)
meillo@0 615 {
meillo@10 616 GList *msgout_list_deliver = NULL;
meillo@10 617 GList *msgout_node;
meillo@10 618 gboolean ok = TRUE;
meillo@0 619
meillo@10 620 DEBUG(6) debugf("deliver_route_msg_list()\n");
meillo@0 621
meillo@10 622 foreach(msgout_list, msgout_node) {
meillo@10 623 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 624 msg_out *msgout_cloned = clone_msg_out(msgout);
meillo@10 625 GList *rcpt_list_non_delivered = NULL;
meillo@10 626 GList *rcpt_node;
meillo@0 627
meillo@367 628 /*
meillo@367 629 ** we have to delete already delivered rcpt's because a
meillo@367 630 ** previous route may have delivered to it
meillo@367 631 */
meillo@10 632 foreach(msgout_cloned->rcpt_list, rcpt_node) {
meillo@10 633 address *rcpt = (address *) (rcpt_node->data);
meillo@367 634 /*
meillo@367 635 ** failed addresses already have been bounced;
meillo@367 636 ** there should be a better way to handle those.
meillo@367 637 */
meillo@404 638 if (!addr_is_delivered(rcpt) &&
meillo@404 639 !addr_is_failed(rcpt) &&
meillo@404 640 !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)){
meillo@10 641 rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt);
meillo@280 642 }
meillo@10 643 }
meillo@10 644 g_list_free(msgout_cloned->rcpt_list);
meillo@10 645 msgout_cloned->rcpt_list = rcpt_list_non_delivered;
meillo@0 646
meillo@280 647 if (!msgout_cloned->rcpt_list) {
meillo@280 648 destroy_msg_out(msgout_cloned);
meillo@280 649 continue;
meillo@280 650 }
meillo@0 651
meillo@317 652 /* filter by allowed envelope sender */
meillo@404 653 if (!route_sender_is_allowed(route, msgout->msg->return_path)){
meillo@404 654 DEBUG(6) debugf("sender `%s' is not allowed for this "
meillo@404 655 "route\n", msgout->msg->return_path);
meillo@280 656 destroy_msg_out(msgout_cloned);
meillo@280 657 continue;
meillo@280 658 }
meillo@0 659
meillo@317 660 /* filter by allowed envelope rcpts */
meillo@366 661 GList *rcpt_list_allowed = NULL;
meillo@366 662 GList *rcpt_list_notallowed = NULL;
meillo@370 663 route_split_rcpts(route, msgout_cloned->rcpt_list,
meillo@370 664 &rcpt_list_allowed, &rcpt_list_notallowed);
meillo@280 665 if (!rcpt_list_allowed) {
meillo@280 666 destroy_msg_out(msgout_cloned);
meillo@280 667 continue;
meillo@280 668 }
meillo@317 669
meillo@370 670 logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid,
meillo@370 671 route->name);
meillo@10 672
meillo@280 673 g_list_free(msgout_cloned->rcpt_list);
meillo@280 674 msgout_cloned->rcpt_list = rcpt_list_allowed;
meillo@280 675
meillo@280 676 if (route->last_route) {
meillo@280 677 GList *rcpt_node;
meillo@280 678 foreach(msgout_cloned->rcpt_list, rcpt_node) {
meillo@280 679 address *rcpt = (address *) (rcpt_node->data);
meillo@280 680 rcpt->flags |= ADDR_FLAG_LAST_ROUTE;
meillo@280 681 }
meillo@280 682 }
meillo@280 683
meillo@280 684 route_prepare_msgout(route, msgout_cloned);
meillo@404 685 msgout_list_deliver = g_list_append(msgout_list_deliver,
meillo@404 686 msgout_cloned);
meillo@10 687 }
meillo@10 688
meillo@280 689 if (msgout_list_deliver) {
meillo@280 690 if (deliver_route_msgout_list(route, msgout_list_deliver)) {
meillo@10 691 ok = TRUE;
meillo@280 692 }
meillo@10 693 destroy_msg_out_list(msgout_list_deliver);
meillo@10 694 }
meillo@10 695 return ok;
meillo@0 696 }
meillo@0 697
meillo@367 698 /*
meillo@367 699 ** copy pointers of delivered addresses to the msg's non_rcpt_list,
meillo@367 700 ** to make sure that they will not be delivered again.
meillo@0 701 */
meillo@10 702 void
meillo@366 703 update_non_rcpt_list(msg_out *msgout)
meillo@0 704 {
meillo@10 705 GList *rcpt_node;
meillo@10 706 message *msg = msgout->msg;
meillo@0 707
meillo@10 708 foreach(msgout->rcpt_list, rcpt_node) {
meillo@10 709 address *rcpt = (address *) (rcpt_node->data);
meillo@280 710 if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) {
meillo@404 711 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list,
meillo@404 712 rcpt);
meillo@280 713 }
meillo@10 714 }
meillo@0 715 }
meillo@0 716
meillo@367 717 /*
meillo@367 718 ** after delivery attempts, we check if there are any rcpt addresses left in
meillo@367 719 ** the message. If all addresses have been completed, the spool files will be
meillo@367 720 ** deleted, otherwise the header spool will be written back. We never changed
meillo@367 721 ** the data spool, so there is no need to write that back.
meillo@367 722 **
meillo@367 723 ** returns TRUE if all went well.
meillo@0 724 */
meillo@10 725 gboolean
meillo@366 726 deliver_finish(msg_out *msgout)
meillo@0 727 {
meillo@10 728 GList *rcpt_node;
meillo@10 729 message *msg = msgout->msg;
meillo@10 730 gboolean finished = TRUE;
meillo@0 731
meillo@10 732 update_non_rcpt_list(msgout);
meillo@0 733
meillo@367 734 /*
meillo@367 735 ** we NEVER made copies of the addresses, flags affecting addresses
meillo@367 736 ** were always set on the original address structs
meillo@367 737 */
meillo@10 738 foreach(msg->rcpt_list, rcpt_node) {
meillo@10 739 address *rcpt = (address *) (rcpt_node->data);
meillo@280 740 if (!addr_is_finished_children(rcpt)) {
meillo@10 741 finished = FALSE;
meillo@280 742 } else {
meillo@367 743 /*
meillo@367 744 ** if ALL children have been delivered, mark parent as
meillo@367 745 ** delivered. if there is one or more not delivered,
meillo@367 746 ** it must have failed, we mark the parent as failed
meillo@367 747 ** as well.
meillo@367 748 */
meillo@10 749 if (addr_is_delivered_children(rcpt)) {
meillo@10 750 addr_mark_delivered(rcpt);
meillo@10 751 } else {
meillo@10 752 addr_mark_failed(rcpt);
meillo@10 753 }
meillo@10 754 }
meillo@10 755 }
meillo@10 756
meillo@280 757 if (finished) {
meillo@280 758 if (spool_delete_all(msg)) {
meillo@10 759 logwrite(LOG_NOTICE, "%s completed.\n", msg->uid);
meillo@280 760 return TRUE;
meillo@280 761 }
meillo@280 762 return FALSE;
meillo@10 763 }
meillo@280 764
meillo@280 765 /* one not delivered address was found */
meillo@280 766 if (!spool_write(msg, FALSE)) {
meillo@404 767 logwrite(LOG_ALERT, "could not write back spool header "
meillo@404 768 "for %s\n", msg->uid);
meillo@280 769 return FALSE;
meillo@280 770 }
meillo@280 771
meillo@280 772 DEBUG(2) debugf("spool header for %s written back.\n", msg->uid);
meillo@280 773 return TRUE;
meillo@0 774 }
meillo@0 775
meillo@354 776 int
meillo@366 777 deliver_remote(GList *remote_msgout_list)
meillo@10 778 {
meillo@354 779 int ok = TRUE;
meillo@354 780 GList *route_list = NULL;
meillo@354 781 GList *route_node;
meillo@10 782 GList *rf_list = NULL;
meillo@280 783 gchar *connect_name = NULL;
meillo@0 784
meillo@354 785 if (!remote_msgout_list) {
meillo@354 786 return FALSE;
meillo@354 787 }
meillo@354 788
meillo@354 789 /* perma routes */
meillo@354 790 if (conf.perma_routes) {
meillo@354 791 DEBUG(5) debugf("processing perma_routes\n");
meillo@354 792
meillo@354 793 route_list = read_route_list(conf.perma_routes, TRUE);
meillo@354 794 foreach(route_list, route_node) {
meillo@404 795 connect_route *route =
meillo@404 796 (connect_route *) (route_node->data);
meillo@404 797 if (!deliver_route_msg_list(route,
meillo@404 798 remote_msgout_list)) {
meillo@354 799 ok = FALSE;
meillo@354 800 }
meillo@354 801 }
meillo@354 802 destroy_route_list(route_list);
meillo@354 803 }
meillo@354 804
meillo@354 805 /* query routes */
meillo@310 806 connect_name = online_query();
meillo@280 807 if (!connect_name) {
meillo@344 808 DEBUG(5) debugf("online query returned false\n");
meillo@280 809 return FALSE;
meillo@10 810 }
meillo@280 811
meillo@280 812 /* we are online! */
meillo@344 813 DEBUG(5) debugf("processing query_routes\n");
meillo@404 814 logwrite(LOG_NOTICE, "detected online configuration `%s'\n",
meillo@404 815 connect_name);
meillo@280 816
meillo@354 817 rf_list = (GList *) table_find(conf.query_routes, connect_name);
meillo@280 818 if (!rf_list) {
meillo@404 819 logwrite(LOG_ALERT, "route list with name '%s' not found.\n",
meillo@404 820 connect_name);
meillo@280 821 return FALSE;
meillo@280 822 }
meillo@280 823
meillo@321 824 route_list = read_route_list(rf_list, FALSE);
meillo@280 825 if (!route_list) {
meillo@404 826 logwrite(LOG_ALERT, "could not read route list '%s'\n",
meillo@404 827 connect_name);
meillo@280 828 return FALSE;
meillo@280 829 }
meillo@280 830
meillo@280 831 foreach(route_list, route_node) {
meillo@280 832 connect_route *route = (connect_route *) (route_node->data);
meillo@280 833 /* TODO: ok gets overwritten */
meillo@354 834 ok = deliver_route_msg_list(route, remote_msgout_list);
meillo@280 835 }
meillo@280 836 destroy_route_list(route_list);
meillo@344 837
meillo@10 838 return ok;
meillo@10 839 }
meillo@0 840
meillo@321 841 /*
meillo@367 842 ** This function splits the list of rcpt addresses
meillo@367 843 ** into local and remote addresses and processes them accordingly.
meillo@321 844 */
meillo@10 845 gboolean
meillo@366 846 deliver_msg_list(GList *msg_list, guint flags)
meillo@10 847 {
meillo@343 848 GList *msgout_list = NULL;
meillo@343 849 GList *msg_node;
meillo@280 850 GList *local_msgout_list = NULL;
meillo@354 851 GList *remote_msgout_list = NULL;
meillo@10 852 GList *msgout_node;
meillo@10 853 GList *alias_table = NULL;
meillo@387 854 GList *globalias_table = NULL;
meillo@10 855 gboolean ok = TRUE;
meillo@0 856
meillo@343 857 /* create msgout_list */
meillo@343 858 foreach(msg_list, msg_node) {
meillo@343 859 message *msg = (message *) msg_node->data;
meillo@343 860 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
meillo@343 861 }
meillo@343 862
meillo@387 863 if (conf.globalias_file) {
meillo@387 864 globalias_table = table_read(conf.globalias_file, ':');
meillo@387 865 }
meillo@10 866 if (conf.alias_file) {
meillo@17 867 alias_table = table_read(conf.alias_file, ':');
meillo@10 868 }
meillo@0 869
meillo@10 870 /* sort messages for different deliveries */
meillo@10 871 foreach(msgout_list, msgout_node) {
meillo@10 872 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 873 GList *rcpt_list;
meillo@10 874 GList *local_rcpt_list = NULL;
meillo@237 875 GList *other_rcpt_list = NULL;
meillo@0 876
meillo@114 877 if (!spool_lock(msgout->msg->uid)) {
meillo@404 878 DEBUG(5) debugf("spool_lock(%s) failed.\n",
meillo@404 879 msgout->msg->uid);
meillo@10 880 continue;
meillo@114 881 }
meillo@114 882 DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid);
meillo@0 883
meillo@10 884 rcpt_list = g_list_copy(msgout->msg->rcpt_list);
meillo@10 885 if (conf.log_user) {
meillo@404 886 address *addr = create_address_qualified(conf.log_user,
meillo@404 887 TRUE, conf.host_name);
meillo@280 888 if (addr) {
meillo@10 889 rcpt_list = g_list_prepend(rcpt_list, addr);
meillo@280 890 } else {
meillo@404 891 logwrite(LOG_ALERT, "invalid log_user "
meillo@404 892 "address `%s', ignoring\n",
meillo@404 893 conf.log_user);
meillo@280 894 }
meillo@10 895 }
meillo@387 896 if (globalias_table) {
meillo@387 897 GList *globaliased_rcpt_list;
meillo@387 898 globaliased_rcpt_list = alias_expand(globalias_table,
meillo@387 899 rcpt_list,
meillo@387 900 msgout->msg->non_rcpt_list, 1);
meillo@387 901 g_list_free(rcpt_list);
meillo@387 902 rcpt_list = globaliased_rcpt_list;
meillo@387 903 }
meillo@10 904 if (alias_table) {
meillo@10 905 GList *aliased_rcpt_list;
meillo@387 906 aliased_rcpt_list = alias_expand(alias_table,
meillo@387 907 rcpt_list,
meillo@387 908 msgout->msg->non_rcpt_list, 0);
meillo@10 909 g_list_free(rcpt_list);
meillo@10 910 rcpt_list = aliased_rcpt_list;
meillo@10 911 }
meillo@0 912
meillo@404 913 /* split_rcpts(rcpt_list, NULL, &local_rcpt_list, NULL,
meillo@404 914 ** &other_rcpt_list); */
meillo@354 915 local_rcpt_list = local_rcpts(rcpt_list);
meillo@354 916 other_rcpt_list = remote_rcpts(rcpt_list);
meillo@237 917 g_list_free(rcpt_list);
meillo@237 918
meillo@10 919 /* local recipients */
meillo@237 920 if ((flags & DLVR_LOCAL) && local_rcpt_list) {
meillo@237 921 msg_out *local_msgout = clone_msg_out(msgout);
meillo@237 922 local_msgout->rcpt_list = local_rcpt_list;
meillo@404 923 local_msgout_list = g_list_append(local_msgout_list,
meillo@404 924 local_msgout);
meillo@10 925 }
meillo@0 926
meillo@354 927 /* remote recipients, requires online delivery */
meillo@237 928 if ((flags & DLVR_ONLINE) && other_rcpt_list) {
meillo@354 929 msg_out *remote_msgout = clone_msg_out(msgout);
meillo@354 930 remote_msgout->rcpt_list = other_rcpt_list;
meillo@404 931 remote_msgout_list = g_list_append(remote_msgout_list,
meillo@404 932 remote_msgout);
meillo@10 933 }
meillo@10 934 }
meillo@0 935
meillo@280 936 if (alias_table) {
meillo@10 937 destroy_table(alias_table);
meillo@280 938 }
meillo@387 939 if (globalias_table) {
meillo@387 940 destroy_table(globalias_table);
meillo@387 941 }
meillo@0 942
meillo@354 943 /* process local/remote msgout lists -> delivery */
meillo@237 944
meillo@237 945 if (local_msgout_list) {
meillo@114 946 DEBUG(5) debugf("local_msgout_list\n");
meillo@10 947 foreach(local_msgout_list, msgout_node) {
meillo@10 948 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 949 if (!deliver_local(msgout)) {
meillo@10 950 ok = FALSE;
meillo@280 951 }
meillo@10 952 }
meillo@10 953 destroy_msg_out_list(local_msgout_list);
meillo@10 954 }
meillo@0 955
meillo@354 956 if (remote_msgout_list) {
meillo@354 957 DEBUG(5) debugf("remote_msgout_list\n");
meillo@354 958 deliver_remote(remote_msgout_list);
meillo@354 959 destroy_msg_out_list(remote_msgout_list);
meillo@10 960 }
meillo@10 961
meillo@344 962 /* unlock spool files */
meillo@10 963 foreach(msgout_list, msgout_node) {
meillo@10 964 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@114 965 if (spool_unlock(msgout->msg->uid)) {
meillo@404 966 DEBUG(5) debugf("spool_unlock(%s)\n",
meillo@404 967 msgout->msg->uid);
meillo@114 968 } else {
meillo@404 969 DEBUG(5) debugf("spool_unlock(%s) failed.\n",
meillo@404 970 msgout->msg->uid);
meillo@114 971 }
meillo@10 972 }
meillo@10 973 destroy_msg_out_list(msgout_list);
meillo@10 974
meillo@10 975 return ok;
meillo@0 976 }
meillo@0 977
meillo@321 978 /*
meillo@367 979 ** deliver() is called when a message has just been received
meillo@367 980 ** (mode_accept and smtp_in) and should be delivered immediately
meillo@367 981 ** (neither -odq nor do_queue). Only this one message will be tried to
meillo@367 982 ** deliver then.
meillo@0 983 */
meillo@10 984 gboolean
meillo@366 985 deliver(message *msg)
meillo@0 986 {
meillo@10 987 gboolean ok;
meillo@10 988 GList *msg_list = g_list_append(NULL, msg);
meillo@0 989
meillo@10 990 ok = deliver_msg_list(msg_list, DLVR_ALL);
meillo@10 991 g_list_free(msg_list);
meillo@0 992
meillo@10 993 return ok;
meillo@0 994 }