masqmail

annotate src/deliver.c @ 311:e230bcd0f1c6

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