masqmail

annotate src/deliver.c @ 317:55b7bde95d37

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