masqmail

annotate src/deliver.c @ 370:d86d7b4b8995

Broke long lines.
author markus schnalke <meillo@marmaro.de>
date Tue, 25 Oct 2011 13:50:27 +0200
parents b27f66555ba8
children b0708fac99dd
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@370 276 deliver_msglist_host_pipe(connect_route *route, GList *msgout_list,
meillo@370 277 gchar *host, GList *res_list)
meillo@0 278 {
meillo@10 279 gboolean ok = TRUE;
meillo@10 280 GList *msgout_node;
meillo@0 281
meillo@10 282 DEBUG(5) debugf("deliver_msglist_host_pipe entered\n");
meillo@0 283
meillo@10 284 foreach(msgout_list, msgout_node) {
meillo@10 285 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 286 gboolean flag, ok_fail = FALSE;
meillo@10 287 message *msg = msgout->msg;
meillo@10 288 GList *rcpt_node, *rcpt_list = msgout->rcpt_list;
meillo@0 289
meillo@10 290 DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid);
meillo@0 291
meillo@10 292 flag = (msg->data_list == NULL);
meillo@280 293 if (flag && !spool_read_data(msg)) {
meillo@280 294 logwrite(LOG_ALERT, "could not open data spool file for %s\n", msg->uid);
meillo@280 295 continue;
meillo@10 296 }
meillo@0 297
meillo@10 298 ok = FALSE;
meillo@10 299 foreach(rcpt_list, rcpt_node) {
meillo@10 300 address *rcpt = (address *) (rcpt_node->data);
meillo@10 301 gchar *cmd = g_malloc(256);
meillo@10 302 GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt);
meillo@0 303
meillo@321 304 DEBUG(1) debugf("attempting to deliver %s to %s@%s with pipe\n",
meillo@321 305 msg->uid, rcpt->local_part, rcpt->domain);
meillo@10 306
meillo@280 307 if (!expand(var_table, route->pipe, cmd, 256)) {
meillo@321 308 logwrite(LOG_ALERT, "could not expand string `%s'\n", route->pipe);
meillo@321 309 destroy_table(var_table);
meillo@321 310 continue;
meillo@321 311 }
meillo@321 312
meillo@321 313 if (pipe_out(msg, msg->hdr_list, rcpt, cmd, (route->pipe_fromline ? MSGSTR_FROMLINE : 0)
meillo@321 314 | (route->pipe_fromhack ? MSGSTR_FROMHACK : 0))) {
meillo@321 315 logwrite(LOG_NOTICE, "%s => %s@%s with pipe (cmd = '%s')\n",
meillo@321 316 msg->uid, rcpt->local_part, rcpt->domain, cmd);
meillo@321 317 addr_mark_delivered(rcpt);
meillo@321 318 ok = TRUE;
meillo@280 319 } else {
meillo@321 320 logwrite(LOG_ALERT, "pipe_out '%s' failed\n", route->pipe);
meillo@10 321
meillo@321 322 if (route->connect_error_fail) {
meillo@321 323 addr_mark_failed(rcpt);
meillo@10 324 } else {
meillo@321 325 addr_mark_defered(rcpt);
meillo@10 326 }
meillo@280 327 }
meillo@10 328
meillo@10 329 destroy_table(var_table);
meillo@10 330 }
meillo@10 331 ok_fail = delivery_failures(msg, rcpt_list, "%s", strerror(errno));
meillo@10 332
meillo@280 333 if (flag) {
meillo@10 334 msg_free_data(msg);
meillo@280 335 }
meillo@280 336 if (ok || ok_fail) {
meillo@10 337 deliver_finish(msgout);
meillo@280 338 }
meillo@0 339 }
meillo@0 340
meillo@10 341 return ok;
meillo@0 342 }
meillo@0 343
meillo@367 344 /*
meillo@367 345 ** deliver list of messages to one host and finishes them if the message was
meillo@367 346 ** delivered to at least one rcpt.
meillo@367 347 ** Returns TRUE if at least one msg was delivered to at least one rcpt.
meillo@0 348 */
meillo@10 349 gboolean
meillo@367 350 deliver_msglist_host_smtp(connect_route *route, GList *msgout_list,
meillo@367 351 gchar *host, GList *res_list)
meillo@0 352 {
meillo@10 353 gboolean ok = FALSE;
meillo@10 354 GList *msgout_node;
meillo@10 355 smtp_base *psb;
meillo@178 356 gint port = 25;
meillo@0 357
meillo@10 358 /* paranoid check: */
meillo@280 359 if (!msgout_list) {
meillo@10 360 logwrite(LOG_ALERT, "Ooops: empty list of messages in deliver_msglist_host()\n");
meillo@10 361 return FALSE;
meillo@10 362 }
meillo@10 363
meillo@280 364 if (!host) {
meillo@179 365 /* XXX: what if mail_host isn't set? Is this possible? */
meillo@10 366 host = route->mail_host->address;
meillo@10 367 port = route->mail_host->port;
meillo@178 368 }
meillo@10 369
meillo@280 370 if (route->wrapper) {
meillo@321 371 psb = smtp_out_open_child(route->wrapper, host);
meillo@280 372 } else {
meillo@280 373 psb = smtp_out_open(host, port, res_list);
meillo@280 374 }
meillo@0 375
meillo@280 376 if (!psb) {
meillo@10 377 /* smtp_out_open() failed */
meillo@10 378 foreach(msgout_list, msgout_node) {
meillo@10 379 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 380 GList *rcpt_node;
meillo@10 381
meillo@280 382 for (rcpt_node = g_list_first(msgout->rcpt_list);
meillo@280 383 rcpt_node;
meillo@280 384 rcpt_node = g_list_next(rcpt_node)) {
meillo@10 385 address *rcpt = (address *) (rcpt_node->data);
meillo@280 386 gboolean ret = FALSE;
meillo@10 387
meillo@10 388 addr_unmark_delivered(rcpt);
meillo@10 389 if (route->connect_error_fail) {
meillo@10 390 addr_mark_failed(rcpt);
meillo@10 391 } else {
meillo@10 392 addr_mark_defered(rcpt);
meillo@10 393 }
meillo@280 394 if (route->wrapper) {
meillo@280 395 ret = delivery_failures(msgout->msg, msgout->rcpt_list, "could not open wrapper:\n\t%s", strerror(errno));
meillo@280 396 } else {
meillo@280 397 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 398 }
meillo@280 399 if (ret) {
meillo@10 400 deliver_finish(msgout);
meillo@280 401 }
meillo@10 402 }
meillo@10 403 }
meillo@280 404 return ok;
meillo@0 405 }
meillo@280 406
meillo@280 407 set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name, route->do_correct_helo);
meillo@280 408
meillo@280 409 #ifdef ENABLE_AUTH
meillo@321 410 if (route->auth_name && route->auth_login && route->auth_secret) {
meillo@280 411 set_auth(psb, route->auth_name, route->auth_login, route->auth_secret);
meillo@280 412 }
meillo@280 413 #endif
meillo@280 414 if (!smtp_out_init(psb, route->instant_helo)) {
meillo@280 415 /* smtp_out_init() failed */
meillo@280 416 if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) || (psb->error==smtp_syntax)) {
meillo@280 417 smtp_out_quit(psb);
meillo@280 418
meillo@280 419 foreach(msgout_list, msgout_node) {
meillo@280 420 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 421 smtp_out_mark_rcpts(psb, msgout->rcpt_list);
meillo@280 422
meillo@280 423 if (delivery_failures(msgout->msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer)) {
meillo@280 424 deliver_finish(msgout);
meillo@280 425 }
meillo@280 426 }
meillo@280 427 }
meillo@280 428 destroy_smtpbase(psb);
meillo@280 429 return ok;
meillo@280 430 }
meillo@280 431
meillo@280 432 if (!route->do_pipelining) {
meillo@280 433 psb->use_pipelining = FALSE;
meillo@280 434 }
meillo@280 435
meillo@280 436 foreach(msgout_list, msgout_node) {
meillo@280 437 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 438 gboolean flag, ok_msg = FALSE, ok_fail = FALSE;
meillo@280 439 message *msg = msgout->msg;
meillo@280 440
meillo@280 441 /* we may have to read the data at this point and remember if we did */
meillo@280 442 flag = (msg->data_list == NULL);
meillo@280 443 if (flag && !spool_read_data(msg)) {
meillo@280 444 logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
meillo@280 445 break;
meillo@280 446 }
meillo@280 447
meillo@280 448 smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list, msgout->hdr_list);
meillo@280 449
meillo@280 450 ok_fail = delivery_failures(msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer);
meillo@280 451
meillo@280 452 if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) {
meillo@280 453 /* connection lost */
meillo@280 454 break;
meillo@280 455 } else if (psb->error != smtp_ok) {
meillo@280 456 if (g_list_next(msgout_node) && !smtp_out_rset(psb)) {
meillo@280 457 break;
meillo@280 458 }
meillo@280 459 }
meillo@280 460 ok_msg = (psb->error == smtp_ok);
meillo@280 461
meillo@280 462 if (flag) {
meillo@280 463 msg_free_data(msg);
meillo@280 464 }
meillo@280 465 if (ok_msg) {
meillo@280 466 ok = TRUE;
meillo@280 467 }
meillo@280 468 if (ok_msg || ok_fail) {
meillo@280 469 deliver_finish(msgout);
meillo@280 470 }
meillo@280 471 }
meillo@280 472 if (psb->error == smtp_ok || (psb->error == smtp_fail)
meillo@280 473 || (psb->error == smtp_trylater) || (psb->error == smtp_syntax)) {
meillo@280 474 smtp_out_quit(psb);
meillo@280 475 }
meillo@280 476 destroy_smtpbase(psb);
meillo@10 477 return ok;
meillo@0 478 }
meillo@0 479
meillo@10 480 gboolean
meillo@370 481 deliver_msglist_host(connect_route *route, GList *msgout_list, gchar *host,
meillo@370 482 GList *res_list)
meillo@0 483 {
meillo@0 484
meillo@311 485 if (route->pipe) {
meillo@311 486 DEBUG(5) debugf("with pipe\n");
meillo@10 487 return deliver_msglist_host_pipe(route, msgout_list, host, res_list);
meillo@10 488 } else {
meillo@311 489 DEBUG(5) debugf("with smtp\n");
meillo@10 490 return deliver_msglist_host_smtp(route, msgout_list, host, res_list);
meillo@10 491 }
meillo@0 492 }
meillo@0 493
meillo@0 494 /*
meillo@367 495 ** delivers messages in msgout_list using route
meillo@0 496 */
meillo@10 497 gboolean
meillo@366 498 deliver_route_msgout_list(connect_route *route, GList *msgout_list)
meillo@0 499 {
meillo@10 500 gboolean ok = FALSE;
meillo@280 501 GList *mo_ph_list;
meillo@280 502 GList *mo_ph_node;
meillo@0 503
meillo@15 504 DEBUG(5) debugf("deliver_route_msgout_list entered, route->name = %s\n", route->name);
meillo@0 505
meillo@179 506 if (route->mail_host) {
meillo@179 507 /* this is easy... deliver everything to a smart host for relay */
meillo@370 508 return deliver_msglist_host(route, msgout_list, NULL,
meillo@370 509 route->resolve_list);
meillo@280 510 }
meillo@280 511
meillo@280 512 /* this is not easy... */
meillo@280 513
meillo@280 514 mo_ph_list = route_msgout_list(route, msgout_list);
meillo@280 515 /* okay, now we have ordered our messages by the hosts. */
meillo@280 516 if (!mo_ph_list) {
meillo@280 517 return FALSE;
meillo@280 518 }
meillo@280 519
meillo@367 520 /*
meillo@367 521 ** TODO: It would be nice to be able to fork for each host.
meillo@367 522 ** We cannot do that yet because of complications with finishing the
meillo@367 523 ** messages. Threads could be a solution because they use the same
meillo@367 524 ** memory. But we are not thread safe yet...
meillo@367 525 */
meillo@280 526 foreach(mo_ph_list, mo_ph_node) {
meillo@280 527 msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data);
meillo@370 528 if (deliver_msglist_host(route, mo_ph->msgout_list,
meillo@370 529 mo_ph->host, route->resolve_list)) {
meillo@10 530 ok = TRUE;
meillo@10 531 }
meillo@280 532 destroy_msgout_perhost(mo_ph);
meillo@10 533 }
meillo@280 534 g_list_free(mo_ph_list);
meillo@10 535 return ok;
meillo@0 536 }
meillo@0 537
meillo@0 538 /*
meillo@367 539 ** calls route_prepare_msg()
meillo@367 540 ** delivers messages in msg_list using route by calling
meillo@367 541 ** deliver_route_msgout_list()
meillo@0 542 */
meillo@10 543 gboolean
meillo@366 544 deliver_route_msg_list(connect_route *route, GList *msgout_list)
meillo@0 545 {
meillo@10 546 GList *msgout_list_deliver = NULL;
meillo@10 547 GList *msgout_node;
meillo@10 548 gboolean ok = TRUE;
meillo@0 549
meillo@10 550 DEBUG(6) debugf("deliver_route_msg_list()\n");
meillo@0 551
meillo@10 552 foreach(msgout_list, msgout_node) {
meillo@10 553 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 554 msg_out *msgout_cloned = clone_msg_out(msgout);
meillo@10 555 GList *rcpt_list_non_delivered = NULL;
meillo@10 556 GList *rcpt_node;
meillo@0 557
meillo@367 558 /*
meillo@367 559 ** we have to delete already delivered rcpt's because a
meillo@367 560 ** previous route may have delivered to it
meillo@367 561 */
meillo@10 562 foreach(msgout_cloned->rcpt_list, rcpt_node) {
meillo@10 563 address *rcpt = (address *) (rcpt_node->data);
meillo@367 564 /*
meillo@367 565 ** failed addresses already have been bounced;
meillo@367 566 ** there should be a better way to handle those.
meillo@367 567 */
meillo@10 568 if (!addr_is_delivered(rcpt) && !addr_is_failed(rcpt)
meillo@280 569 && !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)) {
meillo@10 570 rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt);
meillo@280 571 }
meillo@10 572 }
meillo@10 573 g_list_free(msgout_cloned->rcpt_list);
meillo@10 574 msgout_cloned->rcpt_list = rcpt_list_non_delivered;
meillo@0 575
meillo@280 576 if (!msgout_cloned->rcpt_list) {
meillo@280 577 destroy_msg_out(msgout_cloned);
meillo@280 578 continue;
meillo@280 579 }
meillo@0 580
meillo@317 581 /* filter by allowed envelope sender */
meillo@317 582 if (!route_sender_is_allowed(route, msgout->msg->return_path)) {
meillo@280 583 destroy_msg_out(msgout_cloned);
meillo@280 584 continue;
meillo@280 585 }
meillo@0 586
meillo@317 587 /* filter by allowed envelope rcpts */
meillo@366 588 GList *rcpt_list_allowed = NULL;
meillo@366 589 GList *rcpt_list_notallowed = NULL;
meillo@370 590 route_split_rcpts(route, msgout_cloned->rcpt_list,
meillo@370 591 &rcpt_list_allowed, &rcpt_list_notallowed);
meillo@280 592 if (!rcpt_list_allowed) {
meillo@280 593 destroy_msg_out(msgout_cloned);
meillo@280 594 continue;
meillo@280 595 }
meillo@317 596
meillo@370 597 logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid,
meillo@370 598 route->name);
meillo@10 599
meillo@280 600 g_list_free(msgout_cloned->rcpt_list);
meillo@280 601 msgout_cloned->rcpt_list = rcpt_list_allowed;
meillo@280 602
meillo@280 603 if (route->last_route) {
meillo@280 604 GList *rcpt_node;
meillo@280 605 foreach(msgout_cloned->rcpt_list, rcpt_node) {
meillo@280 606 address *rcpt = (address *) (rcpt_node->data);
meillo@280 607 rcpt->flags |= ADDR_FLAG_LAST_ROUTE;
meillo@280 608 }
meillo@280 609 }
meillo@280 610
meillo@280 611 route_prepare_msgout(route, msgout_cloned);
meillo@280 612 msgout_list_deliver = g_list_append(msgout_list_deliver, msgout_cloned);
meillo@10 613 }
meillo@10 614
meillo@280 615 if (msgout_list_deliver) {
meillo@280 616 if (deliver_route_msgout_list(route, msgout_list_deliver)) {
meillo@10 617 ok = TRUE;
meillo@280 618 }
meillo@10 619 destroy_msg_out_list(msgout_list_deliver);
meillo@10 620 }
meillo@10 621 return ok;
meillo@0 622 }
meillo@0 623
meillo@367 624 /*
meillo@367 625 ** copy pointers of delivered addresses to the msg's non_rcpt_list,
meillo@367 626 ** to make sure that they will not be delivered again.
meillo@0 627 */
meillo@10 628 void
meillo@366 629 update_non_rcpt_list(msg_out *msgout)
meillo@0 630 {
meillo@10 631 GList *rcpt_node;
meillo@10 632 message *msg = msgout->msg;
meillo@0 633
meillo@10 634 foreach(msgout->rcpt_list, rcpt_node) {
meillo@10 635 address *rcpt = (address *) (rcpt_node->data);
meillo@280 636 if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) {
meillo@10 637 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, rcpt);
meillo@280 638 }
meillo@10 639 }
meillo@0 640 }
meillo@0 641
meillo@367 642 /*
meillo@367 643 ** after delivery attempts, we check if there are any rcpt addresses left in
meillo@367 644 ** the message. If all addresses have been completed, the spool files will be
meillo@367 645 ** deleted, otherwise the header spool will be written back. We never changed
meillo@367 646 ** the data spool, so there is no need to write that back.
meillo@367 647 **
meillo@367 648 ** returns TRUE if all went well.
meillo@0 649 */
meillo@10 650 gboolean
meillo@366 651 deliver_finish(msg_out *msgout)
meillo@0 652 {
meillo@10 653 GList *rcpt_node;
meillo@10 654 message *msg = msgout->msg;
meillo@10 655 gboolean finished = TRUE;
meillo@0 656
meillo@10 657 update_non_rcpt_list(msgout);
meillo@0 658
meillo@367 659 /*
meillo@367 660 ** we NEVER made copies of the addresses, flags affecting addresses
meillo@367 661 ** were always set on the original address structs
meillo@367 662 */
meillo@10 663 foreach(msg->rcpt_list, rcpt_node) {
meillo@10 664 address *rcpt = (address *) (rcpt_node->data);
meillo@280 665 if (!addr_is_finished_children(rcpt)) {
meillo@10 666 finished = FALSE;
meillo@280 667 } else {
meillo@367 668 /*
meillo@367 669 ** if ALL children have been delivered, mark parent as
meillo@367 670 ** delivered. if there is one or more not delivered,
meillo@367 671 ** it must have failed, we mark the parent as failed
meillo@367 672 ** as well.
meillo@367 673 */
meillo@10 674 if (addr_is_delivered_children(rcpt)) {
meillo@10 675 addr_mark_delivered(rcpt);
meillo@10 676 } else {
meillo@10 677 addr_mark_failed(rcpt);
meillo@10 678 }
meillo@10 679 }
meillo@10 680 }
meillo@10 681
meillo@280 682 if (finished) {
meillo@280 683 if (spool_delete_all(msg)) {
meillo@10 684 logwrite(LOG_NOTICE, "%s completed.\n", msg->uid);
meillo@280 685 return TRUE;
meillo@280 686 }
meillo@280 687 return FALSE;
meillo@10 688 }
meillo@280 689
meillo@280 690 /* one not delivered address was found */
meillo@280 691 if (!spool_write(msg, FALSE)) {
meillo@280 692 logwrite(LOG_ALERT, "could not write back spool header for %s\n", msg->uid);
meillo@280 693 return FALSE;
meillo@280 694 }
meillo@280 695
meillo@280 696 DEBUG(2) debugf("spool header for %s written back.\n", msg->uid);
meillo@280 697 return TRUE;
meillo@0 698 }
meillo@0 699
meillo@354 700 int
meillo@366 701 deliver_remote(GList *remote_msgout_list)
meillo@10 702 {
meillo@354 703 int ok = TRUE;
meillo@354 704 GList *route_list = NULL;
meillo@354 705 GList *route_node;
meillo@10 706 GList *rf_list = NULL;
meillo@280 707 gchar *connect_name = NULL;
meillo@0 708
meillo@354 709 if (!remote_msgout_list) {
meillo@354 710 return FALSE;
meillo@354 711 }
meillo@354 712
meillo@354 713 /* perma routes */
meillo@354 714 if (conf.perma_routes) {
meillo@354 715 DEBUG(5) debugf("processing perma_routes\n");
meillo@354 716
meillo@354 717 route_list = read_route_list(conf.perma_routes, TRUE);
meillo@354 718 foreach(route_list, route_node) {
meillo@354 719 connect_route *route = (connect_route *) (route_node->data);
meillo@354 720 if (!deliver_route_msg_list(route, remote_msgout_list)) {
meillo@354 721 ok = FALSE;
meillo@354 722 }
meillo@354 723 }
meillo@354 724 destroy_route_list(route_list);
meillo@354 725 }
meillo@354 726
meillo@354 727 /* query routes */
meillo@310 728 connect_name = online_query();
meillo@280 729 if (!connect_name) {
meillo@344 730 DEBUG(5) debugf("online query returned false\n");
meillo@280 731 return FALSE;
meillo@10 732 }
meillo@280 733
meillo@280 734 /* we are online! */
meillo@344 735 DEBUG(5) debugf("processing query_routes\n");
meillo@344 736 logwrite(LOG_NOTICE, "detected online configuration `%s'\n", connect_name);
meillo@280 737
meillo@354 738 rf_list = (GList *) table_find(conf.query_routes, connect_name);
meillo@280 739 if (!rf_list) {
meillo@280 740 logwrite(LOG_ALERT, "route list with name '%s' not found.\n", connect_name);
meillo@280 741 return FALSE;
meillo@280 742 }
meillo@280 743
meillo@321 744 route_list = read_route_list(rf_list, FALSE);
meillo@280 745 if (!route_list) {
meillo@280 746 logwrite(LOG_ALERT, "could not read route list '%s'\n", connect_name);
meillo@280 747 return FALSE;
meillo@280 748 }
meillo@280 749
meillo@280 750 foreach(route_list, route_node) {
meillo@280 751 connect_route *route = (connect_route *) (route_node->data);
meillo@280 752 /* TODO: ok gets overwritten */
meillo@354 753 ok = deliver_route_msg_list(route, remote_msgout_list);
meillo@280 754 }
meillo@280 755 destroy_route_list(route_list);
meillo@344 756
meillo@10 757 return ok;
meillo@10 758 }
meillo@0 759
meillo@321 760 /*
meillo@367 761 ** This function splits the list of rcpt addresses
meillo@367 762 ** into local and remote addresses and processes them accordingly.
meillo@321 763 */
meillo@10 764 gboolean
meillo@366 765 deliver_msg_list(GList *msg_list, guint flags)
meillo@10 766 {
meillo@343 767 GList *msgout_list = NULL;
meillo@343 768 GList *msg_node;
meillo@280 769 GList *local_msgout_list = NULL;
meillo@354 770 GList *remote_msgout_list = NULL;
meillo@10 771 GList *msgout_node;
meillo@10 772 GList *alias_table = NULL;
meillo@10 773 gboolean ok = TRUE;
meillo@0 774
meillo@343 775 /* create msgout_list */
meillo@343 776 foreach(msg_list, msg_node) {
meillo@343 777 message *msg = (message *) msg_node->data;
meillo@343 778 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
meillo@343 779 }
meillo@343 780
meillo@10 781 if (conf.alias_file) {
meillo@17 782 alias_table = table_read(conf.alias_file, ':');
meillo@10 783 }
meillo@0 784
meillo@10 785 /* sort messages for different deliveries */
meillo@10 786 foreach(msgout_list, msgout_node) {
meillo@10 787 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 788 GList *rcpt_list;
meillo@10 789 GList *local_rcpt_list = NULL;
meillo@237 790 GList *other_rcpt_list = NULL;
meillo@0 791
meillo@114 792 if (!spool_lock(msgout->msg->uid)) {
meillo@114 793 DEBUG(5) debugf("spool_lock(%s) failed.\n", msgout->msg->uid);
meillo@10 794 continue;
meillo@114 795 }
meillo@114 796 DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid);
meillo@0 797
meillo@10 798 rcpt_list = g_list_copy(msgout->msg->rcpt_list);
meillo@10 799 if (conf.log_user) {
meillo@10 800 address *addr = create_address_qualified(conf.log_user, TRUE, conf.host_name);
meillo@280 801 if (addr) {
meillo@10 802 rcpt_list = g_list_prepend(rcpt_list, addr);
meillo@280 803 } else {
meillo@280 804 logwrite(LOG_ALERT, "invalid log_user address `%s', ignoring\n", conf.log_user);
meillo@280 805 }
meillo@10 806 }
meillo@10 807 if (alias_table) {
meillo@10 808 GList *aliased_rcpt_list;
meillo@10 809 aliased_rcpt_list = alias_expand(alias_table, rcpt_list, msgout->msg->non_rcpt_list);
meillo@10 810 g_list_free(rcpt_list);
meillo@10 811 rcpt_list = aliased_rcpt_list;
meillo@10 812 }
meillo@0 813
meillo@366 814 /* split_rcpts(rcpt_list, NULL, &local_rcpt_list, NULL, &other_rcpt_list); */
meillo@354 815 local_rcpt_list = local_rcpts(rcpt_list);
meillo@354 816 other_rcpt_list = remote_rcpts(rcpt_list);
meillo@237 817 g_list_free(rcpt_list);
meillo@237 818
meillo@10 819 /* local recipients */
meillo@237 820 if ((flags & DLVR_LOCAL) && local_rcpt_list) {
meillo@237 821 msg_out *local_msgout = clone_msg_out(msgout);
meillo@237 822 local_msgout->rcpt_list = local_rcpt_list;
meillo@237 823 local_msgout_list = g_list_append(local_msgout_list, local_msgout);
meillo@10 824 }
meillo@0 825
meillo@354 826 /* remote recipients, requires online delivery */
meillo@237 827 if ((flags & DLVR_ONLINE) && other_rcpt_list) {
meillo@354 828 msg_out *remote_msgout = clone_msg_out(msgout);
meillo@354 829 remote_msgout->rcpt_list = other_rcpt_list;
meillo@354 830 remote_msgout_list = g_list_append(remote_msgout_list, remote_msgout);
meillo@10 831 }
meillo@10 832 }
meillo@0 833
meillo@280 834 if (alias_table) {
meillo@10 835 destroy_table(alias_table);
meillo@280 836 }
meillo@0 837
meillo@354 838 /* process local/remote msgout lists -> delivery */
meillo@237 839
meillo@237 840 if (local_msgout_list) {
meillo@114 841 DEBUG(5) debugf("local_msgout_list\n");
meillo@10 842 foreach(local_msgout_list, msgout_node) {
meillo@10 843 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@280 844 if (!deliver_local(msgout)) {
meillo@10 845 ok = FALSE;
meillo@280 846 }
meillo@10 847 }
meillo@10 848 destroy_msg_out_list(local_msgout_list);
meillo@10 849 }
meillo@0 850
meillo@354 851 if (remote_msgout_list) {
meillo@354 852 DEBUG(5) debugf("remote_msgout_list\n");
meillo@354 853 deliver_remote(remote_msgout_list);
meillo@354 854 destroy_msg_out_list(remote_msgout_list);
meillo@10 855 }
meillo@10 856
meillo@344 857 /* unlock spool files */
meillo@10 858 foreach(msgout_list, msgout_node) {
meillo@10 859 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@114 860 if (spool_unlock(msgout->msg->uid)) {
meillo@114 861 DEBUG(5) debugf("spool_unlock(%s)\n", msgout->msg->uid);
meillo@114 862 } else {
meillo@114 863 DEBUG(5) debugf("spool_unlock(%s) failed.\n", msgout->msg->uid);
meillo@114 864 }
meillo@10 865 }
meillo@10 866 destroy_msg_out_list(msgout_list);
meillo@10 867
meillo@10 868 return ok;
meillo@0 869 }
meillo@0 870
meillo@321 871 /*
meillo@367 872 ** deliver() is called when a message has just been received
meillo@367 873 ** (mode_accept and smtp_in) and should be delivered immediately
meillo@367 874 ** (neither -odq nor do_queue). Only this one message will be tried to
meillo@367 875 ** deliver then.
meillo@0 876 */
meillo@10 877 gboolean
meillo@366 878 deliver(message *msg)
meillo@0 879 {
meillo@10 880 gboolean ok;
meillo@10 881 GList *msg_list = g_list_append(NULL, msg);
meillo@0 882
meillo@10 883 ok = deliver_msg_list(msg_list, DLVR_ALL);
meillo@10 884 g_list_free(msg_list);
meillo@0 885
meillo@10 886 return ok;
meillo@0 887 }