masqmail

annotate src/deliver.c @ 12:9fb7ddbaf129

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