masqmail

annotate src/deliver.c @ 367:b27f66555ba8

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