masqmail

annotate src/deliver.c @ 403:7954b82040b3

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