masqmail

annotate src/deliver.c @ 344:0410c8aabac2

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