masqmail

annotate src/deliver.c @ 394:c8e3d1a79313

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