annotate src/lmtp/lmtp-proxy.c @ 10415:3662241f75f2 HEAD

lmtp proxy fixes.
author Timo Sirainen <tss@iki.fi>
date Mon, 07 Dec 2009 13:34:35 -0500
parents ad3fb3f929fc
children abe02cf3bd47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "array.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "ioloop.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "istream.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "istream-tee.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "ostream.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "lmtp-client.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "lmtp-proxy.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #define LMTP_MAX_LINE_LEN 1024
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
13 #define LMTP_PROXY_DATA_INPUT_TIMEOUT_MSECS (1000*60)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 struct lmtp_proxy_recipient {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 struct lmtp_proxy_connection *conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 const char *address;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 const char *reply;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 unsigned int rcpt_to_failed:1;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 unsigned int data_reply_received:1;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 };
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 struct lmtp_proxy_connection {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 struct lmtp_proxy *proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 struct lmtp_proxy_settings set;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 struct lmtp_client *client;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 struct istream *data_input;
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
30
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
31 unsigned int finished:1;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 unsigned int failed:1;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 };
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 struct lmtp_proxy {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 pool_t pool;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 const char *mail_from, *my_hostname;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 ARRAY_DEFINE(connections, struct lmtp_proxy_connection *);
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
39 ARRAY_DEFINE(rcpt_to, struct lmtp_proxy_recipient *);
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
40 unsigned int next_data_reply_idx;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
42 struct timeout *to, *to_data_idle, *to_finish;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 struct io *io;
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
44 struct istream *data_input, *orig_data_input;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 struct ostream *client_output;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 struct tee_istream *tee_data_input;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
48 unsigned int max_timeout_msecs;
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
49
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
50 lmtp_proxy_finish_callback_t *finish_callback;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 void *finish_context;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 unsigned int finished:1;
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
54 unsigned int input_timeout:1;
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
55 unsigned int handling_data_input:1;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 };
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
58 static void lmtp_conn_finish(void *context);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 static void lmtp_proxy_data_input(struct lmtp_proxy *proxy);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 struct lmtp_proxy *
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 lmtp_proxy_init(const char *my_hostname, struct ostream *client_output)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 struct lmtp_proxy *proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 pool_t pool;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 o_stream_ref(client_output);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 pool = pool_alloconly_create("lmtp proxy", 1024);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 proxy = p_new(pool, struct lmtp_proxy, 1);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 proxy->pool = pool;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 proxy->my_hostname = p_strdup(pool, my_hostname);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 proxy->client_output = client_output;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 i_array_init(&proxy->rcpt_to, 32);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 i_array_init(&proxy->connections, 32);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 return proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 static void lmtp_proxy_connections_deinit(struct lmtp_proxy *proxy)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 struct lmtp_proxy_connection *const *conns;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
83 array_foreach(&proxy->connections, conns) {
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
84 struct lmtp_proxy_connection *conn = *conns;
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
85
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
86 lmtp_client_deinit(&conn->client);
10397
9500cfbe5dbf lmtp proxy: Error handling fix.
Timo Sirainen <tss@iki.fi>
parents: 10396
diff changeset
87 }
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 void lmtp_proxy_deinit(struct lmtp_proxy **_proxy)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 struct lmtp_proxy *proxy = *_proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 *_proxy = NULL;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 lmtp_proxy_connections_deinit(proxy);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 if (proxy->data_input != NULL)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 i_stream_unref(&proxy->data_input);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 if (proxy->client_output != NULL)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 o_stream_unref(&proxy->client_output);
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
101 if (proxy->to_data_idle != NULL)
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
102 timeout_remove(&proxy->to_data_idle);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 if (proxy->to != NULL)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 timeout_remove(&proxy->to);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 if (proxy->io != NULL)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 io_remove(&proxy->io);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 array_free(&proxy->rcpt_to);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 array_free(&proxy->connections);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 pool_unref(&proxy->pool);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 void lmtp_proxy_mail_from(struct lmtp_proxy *proxy, const char *value)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 proxy->mail_from = p_strdup(proxy->pool, value);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 static struct lmtp_proxy_connection *
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 lmtp_proxy_get_connection(struct lmtp_proxy *proxy,
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 const struct lmtp_proxy_settings *set)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 struct lmtp_proxy_connection *const *conns, *conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
123 i_assert(set->timeout_msecs > 0);
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
124
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
125 array_foreach(&proxy->connections, conns) {
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
126 conn = *conns;
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
127
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
128 if (conn->set.port == set->port &&
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
129 strcmp(conn->set.host, set->host) == 0)
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
130 return conn;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 conn = p_new(proxy->pool, struct lmtp_proxy_connection, 1);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 conn->proxy = proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 conn->set.host = p_strdup(proxy->pool, set->host);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 conn->set.port = set->port;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 conn->set.timeout_msecs = set->timeout_msecs;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 array_append(&proxy->connections, &conn, 1);
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
139 conn->client = lmtp_client_init(proxy->mail_from, proxy->my_hostname,
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
140 lmtp_conn_finish, conn);
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
141 if (lmtp_client_connect_tcp(conn->client, set->protocol,
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 conn->set.host, conn->set.port) < 0)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 conn->failed = TRUE;
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
144
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
145 if (proxy->max_timeout_msecs < set->timeout_msecs)
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
146 proxy->max_timeout_msecs = set->timeout_msecs;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 return conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
150 static bool lmtp_proxy_send_data_replies(struct lmtp_proxy *proxy)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 {
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
152 struct lmtp_proxy_recipient *const *rcpt;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153 unsigned int i, count;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 o_stream_cork(proxy->client_output);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 rcpt = array_get(&proxy->rcpt_to, &count);
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
157 for (i = proxy->next_data_reply_idx; i < count; i++) {
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
158 if (!(rcpt[i]->rcpt_to_failed || rcpt[i]->data_reply_received))
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 break;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 o_stream_send_str(proxy->client_output,
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
161 t_strconcat(rcpt[i]->reply, "\r\n", NULL));
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 o_stream_uncork(proxy->client_output);
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
164 proxy->next_data_reply_idx = i;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 return i == count;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
169 static void lmtp_proxy_finish_timeout(struct lmtp_proxy *proxy)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 i_assert(!proxy->finished);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
173 timeout_remove(&proxy->to_finish);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 proxy->finished = TRUE;
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
175 proxy->finish_callback(proxy->input_timeout, proxy->finish_context);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
178 static void lmtp_proxy_finish(struct lmtp_proxy *proxy)
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
179 {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
180 /* do the actual finishing in a timeout handler, since the finish
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
181 callback causes the proxy to be destroyed and the code leading up
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
182 to this function can be called from many different places. it's
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
183 easier this way rather than having all the callers check if the
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
184 proxy was already destroyed. */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
185 if (proxy->to_finish == NULL) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
186 proxy->to_finish = timeout_add(0, lmtp_proxy_finish_timeout,
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
187 proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
188 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
189 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
190
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 static void lmtp_proxy_try_finish(struct lmtp_proxy *proxy)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 {
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
193 if (lmtp_proxy_send_data_replies(proxy) &&
10401
99e069c98709 lmtp proxy: Assert-crashfix to handling DATA input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10400
diff changeset
194 (proxy->data_input->eof || proxy->data_input->stream_errno != 0 ||
99e069c98709 lmtp proxy: Assert-crashfix to handling DATA input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10400
diff changeset
195 proxy->input_timeout))
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 lmtp_proxy_finish(proxy);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
199 static void lmtp_conn_finish(void *context)
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
200 {
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
201 struct lmtp_proxy_connection *conn = context;
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
202
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
203 conn->finished = TRUE;
10399
8f6995923fbd lmtp proxy: Don't crash if all RCPT TOs fail to a remote.
Timo Sirainen <tss@iki.fi>
parents: 10398
diff changeset
204 if (conn->data_input != NULL)
8f6995923fbd lmtp proxy: Don't crash if all RCPT TOs fail to a remote.
Timo Sirainen <tss@iki.fi>
parents: 10398
diff changeset
205 i_stream_unref(&conn->data_input);
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
206 lmtp_proxy_try_finish(conn->proxy);
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
207 }
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
208
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
209 static void lmtp_proxy_fail_all(struct lmtp_proxy *proxy, const char *reason)
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
210 {
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
211 struct lmtp_proxy_connection *const *conns;
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
212 unsigned int i, count;
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
213 const char *line;
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
214
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
215 conns = array_get(&proxy->connections, &count);
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
216 for (i = 0; i < count; i++) {
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
217 line = t_strdup_printf(ERRSTR_TEMP_REMOTE_FAILURE
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
218 " (%s while waiting for reply to %s)", reason,
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
219 lmtp_client_state_to_string(conns[i]->client));
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
220 lmtp_client_fail(conns[i]->client, line);
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
221 }
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
222
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
223 if (proxy->to_finish == NULL) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
224 /* we still have some DATA input to read */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
225 if (proxy->io == NULL) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
226 proxy->io = io_add(i_stream_get_fd(proxy->data_input),
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
227 IO_READ,
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
228 lmtp_proxy_data_input, proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
229 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
230 }
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
231 }
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
232
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
233 static void lmtp_proxy_data_input_timeout(struct lmtp_proxy *proxy)
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
234 {
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
235 struct lmtp_proxy_connection *const *conns;
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
236 unsigned int i, count;
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
237
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
238 proxy->input_timeout = TRUE;
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
239 i_stream_close(proxy->orig_data_input);
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
240
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
241 conns = array_get(&proxy->connections, &count);
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
242 for (i = 0; i < count; i++) {
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
243 lmtp_client_fail(conns[i]->client, ERRSTR_TEMP_REMOTE_FAILURE
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
244 " (timeout in DATA input)");
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
245 }
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
246 if (proxy->to_finish == NULL) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
247 /* we had earlier failed all clients already and were just
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
248 waiting for DATA input to finish, but DATA input also failed
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
249 with a timeout. */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
250 lmtp_proxy_finish(proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
251 }
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
252 }
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
253
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 static void
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 lmtp_proxy_conn_rcpt_to(bool success, const char *reply, void *context)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256 {
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
257 struct lmtp_proxy_recipient *rcpt = context;
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
258 struct lmtp_proxy_connection *conn = rcpt->conn;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
260 i_assert(rcpt->reply == NULL);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
262 rcpt->reply = p_strdup(conn->proxy->pool, reply);
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
263 rcpt->rcpt_to_failed = !success;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266 static void
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 lmtp_proxy_conn_data(bool success ATTR_UNUSED, const char *reply, void *context)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 {
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
269 struct lmtp_proxy_recipient *rcpt = context;
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
270 struct lmtp_proxy_connection *conn = rcpt->conn;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
272 i_assert(!rcpt->rcpt_to_failed);
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
273 i_assert(rcpt->reply != NULL);
10396
6ab1e2f4ca55 lmtp: Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10394
diff changeset
274
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
275 rcpt->reply = p_strdup(conn->proxy->pool, reply);
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
276 rcpt->data_reply_received = TRUE;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
279 int lmtp_proxy_add_rcpt(struct lmtp_proxy *proxy, const char *address,
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280 const struct lmtp_proxy_settings *set)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 struct lmtp_proxy_connection *conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 struct lmtp_proxy_recipient *rcpt;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
284
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
285 conn = lmtp_proxy_get_connection(proxy, set);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 if (conn->failed)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 return -1;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
289 rcpt = p_new(proxy->pool, struct lmtp_proxy_recipient, 1);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 rcpt->conn = conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291 rcpt->address = p_strdup(proxy->pool, address);
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
292 array_append(&proxy->rcpt_to, &rcpt, 1);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 lmtp_client_add_rcpt(conn->client, address, lmtp_proxy_conn_rcpt_to,
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
295 lmtp_proxy_conn_data, rcpt);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 return 0;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
299 static size_t lmtp_proxy_find_lowest_offset(struct lmtp_proxy *proxy)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 {
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
301 struct lmtp_proxy_connection *const *conns;
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
302 uoff_t min_offset = (uoff_t)-1;
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
303
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
304 array_foreach(&proxy->connections, conns) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
305 struct lmtp_proxy_connection *conn = *conns;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
307 if (conn->data_input != NULL &&
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
308 min_offset > conn->data_input->v_offset)
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
309 min_offset = conn->data_input->v_offset;
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
310 }
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
311 return min_offset;
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
312 }
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
313
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
314 static bool lmtp_proxy_disconnect_hanging_output(struct lmtp_proxy *proxy)
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
315 {
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
316 struct lmtp_proxy_connection *const *conns;
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
317 uoff_t min_offset;
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
318
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
319 min_offset = lmtp_proxy_find_lowest_offset(proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
320 if (min_offset == (uoff_t)-1)
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
321 return FALSE;
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
322
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
323 /* disconnect all connections that are keeping us from reading
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
324 more input. */
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
325 array_foreach(&proxy->connections, conns) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
326 struct lmtp_proxy_connection *conn = *conns;
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
327
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
328 if (conn->data_input != NULL &&
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
329 conn->data_input->v_offset == min_offset) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
330 lmtp_client_fail(conn->client,
10396
6ab1e2f4ca55 lmtp: Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10394
diff changeset
331 ERRSTR_TEMP_REMOTE_FAILURE
6ab1e2f4ca55 lmtp: Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10394
diff changeset
332 " (DATA output timeout)");
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
333 }
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
334 }
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
335 return TRUE;
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
336 }
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
337
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
338 static void lmtp_proxy_output_timeout(struct lmtp_proxy *proxy)
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
339 {
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340 timeout_remove(&proxy->to);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
341
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342 /* drop the connection with the most unread data */
10340
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
343 if (lmtp_proxy_disconnect_hanging_output(proxy))
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
344 lmtp_proxy_data_input(proxy);
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
345 else {
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
346 /* no such connection, so we've already sent everything but
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
347 some servers aren't replying to us. disconnect all of
26eb1b52a23f lmtp proxy: If remote hangs without replying to end-of-DATA dot, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 10334
diff changeset
348 them. */
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
349 i_assert(proxy->data_input->eof);
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
350 lmtp_proxy_fail_all(proxy, "timeout");
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 static void lmtp_proxy_wait_for_output(struct lmtp_proxy *proxy)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
356 if (proxy->io != NULL)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 io_remove(&proxy->io);
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
358 if (proxy->to == NULL) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
359 proxy->to = timeout_add(proxy->max_timeout_msecs,
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
360 lmtp_proxy_output_timeout, proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
361 }
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
362 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
364 static bool lmtp_proxy_data_read(struct lmtp_proxy *proxy)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 size_t size;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
368 timeout_reset(proxy->to_data_idle);
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
369
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
370 switch (i_stream_read(proxy->data_input)) {
10393
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
371 case 0:
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
372 if (!tee_i_stream_child_is_waiting(proxy->data_input)) {
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
373 /* nothing new read */
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
374 if (proxy->io != NULL)
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
375 return FALSE;
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
376 proxy->io = io_add(i_stream_get_fd(proxy->data_input),
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
377 IO_READ,
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
378 lmtp_proxy_data_input, proxy);
10394
c5c360a88067 lmtp proxy: Fix to handling "no new input" reads.
Timo Sirainen <tss@iki.fi>
parents: 10393
diff changeset
379 return FALSE;
10393
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
380 }
4eadbf965d1f lmtp proxy: Stalling remote servers weren't detected correctly.
Timo Sirainen <tss@iki.fi>
parents: 10341
diff changeset
381 /* fall through */
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382 case -2:
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
383 /* buffer full. someone's stalling. */
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384 lmtp_proxy_wait_for_output(proxy);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 return FALSE;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386 case -1:
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
387 if (proxy->data_input->stream_errno != 0)
10341
d784be144b4d lmtp proxy: Fixes to error handling.
Timo Sirainen <tss@iki.fi>
parents: 10340
diff changeset
388 lmtp_proxy_fail_all(proxy, "disconnect");
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
389 else {
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
390 /* finished reading data input. now we'll just have to
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
391 wait for replies. */
10278
84a1776dd1c3 lmtp proxy: After all DATA input is read, stop trying to read more of it.
Timo Sirainen <tss@iki.fi>
parents: 10000
diff changeset
392 lmtp_proxy_wait_for_output(proxy);
10285
aa8729e340ba lmtp, lmtp client: More fixes to handling failures.
Timo Sirainen <tss@iki.fi>
parents: 10284
diff changeset
393 /* if all RCPT TOs failed, we can finish now */
aa8729e340ba lmtp, lmtp client: More fixes to handling failures.
Timo Sirainen <tss@iki.fi>
parents: 10284
diff changeset
394 lmtp_proxy_try_finish(proxy);
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
395 }
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
396 return FALSE;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
397 default:
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
398 /* something was read */
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
399 if (proxy->to != NULL)
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
400 timeout_remove(&proxy->to);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
401 (void)i_stream_get_data(proxy->data_input, &size);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
402 i_stream_skip(proxy->data_input, size);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
403 return TRUE;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
404 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
405 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
406
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
407 static void lmtp_proxy_data_input(struct lmtp_proxy *proxy)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
408 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409 struct lmtp_proxy_connection *const *conns;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
410
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
411 i_assert(!proxy->handling_data_input);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
412
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
413 proxy->handling_data_input = TRUE;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414 do {
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
415 array_foreach(&proxy->connections, conns)
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
416 lmtp_client_send_more((*conns)->client);
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
417 } while (lmtp_proxy_data_read(proxy));
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
418 proxy->handling_data_input = FALSE;
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
419 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
420
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
421 static void lmtp_proxy_more_data_sent(void *context)
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
422 {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
423 struct lmtp_proxy *proxy = context;
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
424
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
425 if (proxy->to != NULL && !proxy->handling_data_input) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
426 /* some tee child is blocking others. it might have been this
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
427 one, so see if we can continue. */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
428 lmtp_proxy_data_input(proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
429 }
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
430 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
431
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
432 void lmtp_proxy_start(struct lmtp_proxy *proxy, struct istream *data_input,
10288
c11324abe1a8 lmtp proxy: Add Received: header.
Timo Sirainen <tss@iki.fi>
parents: 10285
diff changeset
433 const char *header,
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
434 lmtp_proxy_finish_callback_t *callback, void *context)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
435 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
436 struct lmtp_proxy_connection *const *conns;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
437
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
438 proxy->finish_callback = callback;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439 proxy->finish_context = context;
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
440 proxy->orig_data_input = data_input;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
441 proxy->tee_data_input = tee_i_stream_create(data_input);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
442 proxy->data_input = tee_i_stream_create_child(proxy->tee_data_input);
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
443 proxy->to_data_idle = timeout_add(LMTP_PROXY_DATA_INPUT_TIMEOUT_MSECS,
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
444 lmtp_proxy_data_input_timeout, proxy);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
445
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
446 array_foreach(&proxy->connections, conns) {
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
447 struct lmtp_proxy_connection *conn = *conns;
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
448
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
449 if (conn->finished) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
450 /* this connection had already failed */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
451 continue;
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
452 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
453
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
454 lmtp_client_set_data_output_callback(conn->client,
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
455 lmtp_proxy_more_data_sent,
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
456 proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
457
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
458 conn->data_input =
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
459 tee_i_stream_create_child(proxy->tee_data_input);
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
460 lmtp_client_set_data_header(conn->client, header);
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
461 lmtp_client_send(conn->client, conn->data_input);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
462 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
463
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
464 lmtp_proxy_data_input(proxy);
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465 }