annotate src/lmtp/lmtp-proxy.c @ 14920:a097ef0a9d6d

Array API changed: ARRAY_DEFINE(name, type) -> ARRAY(type) name Easy way to update your existing code: perl -i -pe 's:ARRAY_DEFINE\(([^,]+), *([^)]+)\);:ARRAY($2) $1;:' **/*.[ch]
author Timo Sirainen <tss@iki.fi>
date Sun, 19 Aug 2012 14:21:37 +0300
parents 6a0954d0ce09
children 6d2a439a10a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14133
ba770cba5598 Updated copyright notices to include year 2012.
Timo Sirainen <tss@iki.fi>
parents: 13945
diff changeset
1 /* Copyright (c) 2009-2012 Dovecot authors, see the included COPYING file */
9832
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 "ostream.h"
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #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
9 #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
10
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #define LMTP_MAX_LINE_LEN 1024
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 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
14 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
15 const char *address;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 const char *reply;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 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
19 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
20 };
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 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
23 struct lmtp_proxy *proxy;
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
24 struct lmtp_proxy_rcpt_settings set;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 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
27 struct istream *data_input;
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
28 struct timeout *to;
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
29
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
30 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
31 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
32 };
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 struct lmtp_proxy {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 pool_t pool;
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
36 const char *mail_from;
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
37 struct lmtp_proxy_settings set;
10825
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
38
14920
a097ef0a9d6d Array API changed: ARRAY_DEFINE(name, type) -> ARRAY(type) name
Timo Sirainen <tss@iki.fi>
parents: 14840
diff changeset
39 ARRAY(struct lmtp_proxy_connection *) connections;
a097ef0a9d6d Array API changed: ARRAY_DEFINE(name, type) -> ARRAY(type) name
Timo Sirainen <tss@iki.fi>
parents: 14840
diff changeset
40 ARRAY(struct lmtp_proxy_recipient *) rcpt_to;
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
41 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
42
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
43 struct timeout *to_finish;
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
44 struct istream *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
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
47 unsigned int max_timeout_msecs;
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
48
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
49 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
50 void *finish_context;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 unsigned int finished:1;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 };
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
55 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
56
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 struct lmtp_proxy *
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
58 lmtp_proxy_init(const struct lmtp_proxy_settings *set,
10825
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
59 struct ostream *client_output)
9832
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 *proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 pool_t pool;
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 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
65
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 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
67 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
68 proxy->pool = pool;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 proxy->client_output = client_output;
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
70 proxy->set.my_hostname = p_strdup(pool, set->my_hostname);
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
71 proxy->set.dns_client_socket_path =
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
72 p_strdup(pool, set->dns_client_socket_path);
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
73 proxy->set.source_ip = set->source_ip;
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
74 proxy->set.source_port = set->source_port;
14162
ba06ea38c722 imap/pop3/lmtp proxy: Implemented detection of proxy loops with TTL.
Timo Sirainen <tss@iki.fi>
parents: 14151
diff changeset
75 proxy->set.proxy_ttl = set->proxy_ttl;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 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
77 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
78 return proxy;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 }
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 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
82 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 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
84
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
85 array_foreach(&proxy->connections, conns) {
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
86 struct lmtp_proxy_connection *conn = *conns;
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
87
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
88 lmtp_client_deinit(&conn->client);
10397
9500cfbe5dbf lmtp proxy: Error handling fix.
Timo Sirainen <tss@iki.fi>
parents: 10396
diff changeset
89 }
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 }
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 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
93 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 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
95
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 *_proxy = NULL;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 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
99 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
100 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
101 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
102 o_stream_unref(&proxy->client_output);
10824
bd28e6f29711 lmtp proxy: Crashfix.
Timo Sirainen <tss@iki.fi>
parents: 10582
diff changeset
103 if (proxy->to_finish != NULL)
bd28e6f29711 lmtp proxy: Crashfix.
Timo Sirainen <tss@iki.fi>
parents: 10582
diff changeset
104 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
105 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
106 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
107 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
108 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 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
111 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 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
113 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 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
116 lmtp_proxy_get_connection(struct lmtp_proxy *proxy,
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
117 const struct lmtp_proxy_rcpt_settings *set)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 struct lmtp_proxy_connection *const *conns, *conn;
10825
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
120 struct lmtp_client_settings client_set;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
122 i_assert(set->timeout_msecs > 0);
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
123
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
124 array_foreach(&proxy->connections, conns) {
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
125 conn = *conns;
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
126
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
127 if (conn->set.port == set->port &&
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
128 strcmp(conn->set.host, set->host) == 0)
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
129 return conn;
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131
10825
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
132 memset(&client_set, 0, sizeof(client_set));
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
133 client_set.mail_from = proxy->mail_from;
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
134 client_set.my_hostname = proxy->set.my_hostname;
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
135 client_set.dns_client_socket_path = proxy->set.dns_client_socket_path;
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
136 client_set.source_ip = proxy->set.source_ip;
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
137 client_set.source_port = proxy->set.source_port;
14162
ba06ea38c722 imap/pop3/lmtp proxy: Implemented detection of proxy loops with TTL.
Timo Sirainen <tss@iki.fi>
parents: 14151
diff changeset
138 client_set.proxy_ttl_plus_1 = proxy->set.proxy_ttl+1;
10825
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
139
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 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
141 conn->proxy = proxy;
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 = 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
143 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
144 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
145 array_append(&proxy->connections, &conn, 1);
10825
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
146
cc3f6adc1e0b lmtp proxy: Added support for DNS lookups.
Timo Sirainen <tss@iki.fi>
parents: 10824
diff changeset
147 conn->client = lmtp_client_init(&client_set, lmtp_conn_finish, conn);
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
148 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
149 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
150 conn->failed = TRUE;
9900
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
151
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
152 if (proxy->max_timeout_msecs < set->timeout_msecs)
22d27318bb18 lmtp client, proxy: Several bugfixes.
Timo Sirainen <tss@iki.fi>
parents: 9832
diff changeset
153 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
154 return conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
157 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
158 {
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
159 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
160 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
161
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 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
163 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
164 for (i = proxy->next_data_reply_idx; i < count; i++) {
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
165 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
166 break;
14681
ca37d1577291 Added o_stream_nsend*() and related functions to make delayed error handling safer.
Timo Sirainen <tss@iki.fi>
parents: 14162
diff changeset
167 o_stream_nsend_str(proxy->client_output,
ca37d1577291 Added o_stream_nsend*() and related functions to make delayed error handling safer.
Timo Sirainen <tss@iki.fi>
parents: 14162
diff changeset
168 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
169 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 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
171 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
172
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 return i == count;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
176 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
177 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 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
179
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
180 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
181 proxy->finished = TRUE;
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
182 proxy->finish_callback(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
183 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
185 static void lmtp_proxy_try_finish(struct lmtp_proxy *proxy)
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
186 {
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
187 if (proxy->finish_callback == NULL) {
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
188 /* DATA command hasn't been sent yet */
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
189 return;
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
190 }
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
191 if (!lmtp_proxy_send_data_replies(proxy)) {
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
192 /* we can't received reply from all clients yet */
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
193 return;
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
194 }
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
195 /* do the actual finishing in a timeout handler, since the finish
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
196 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
197 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
198 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
199 proxy was already destroyed. */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
200 if (proxy->to_finish == NULL) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
201 proxy->to_finish = timeout_add(0, lmtp_proxy_finish_timeout,
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
202 proxy);
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
203 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
204 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
205
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
206 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
207 {
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
208 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
209
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
210 conn->finished = TRUE;
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
211 if (conn->to != NULL)
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
212 timeout_remove(&conn->to);
10399
8f6995923fbd lmtp proxy: Don't crash if all RCPT TOs fail to a remote.
Timo Sirainen <tss@iki.fi>
parents: 10398
diff changeset
213 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
214 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
215 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
216 }
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
217
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 static void
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 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
220 {
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
221 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
222 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
223
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
224 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
225
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
226 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
227 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
228 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
230 static void
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
231 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
232 {
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
233 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
234 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
235
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
236 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
237 i_assert(rcpt->reply != NULL);
10396
6ab1e2f4ca55 lmtp: Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10394
diff changeset
238
14764
27dccff46fe9 lmtp proxy: Reset timeout each time receiving a reply lin for DATA.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
239 /* reset timeout in case there are a lot of RCPT TOs */
14766
569588ff7ef0 lmtp: Fixed previous change to make sure it doesn't crash on error handling.
Timo Sirainen <tss@iki.fi>
parents: 14764
diff changeset
240 if (conn->to != NULL)
569588ff7ef0 lmtp: Fixed previous change to make sure it doesn't crash on error handling.
Timo Sirainen <tss@iki.fi>
parents: 14764
diff changeset
241 timeout_reset(conn->to);
14764
27dccff46fe9 lmtp proxy: Reset timeout each time receiving a reply lin for DATA.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
242
10398
a144e918938c LMTP client API changes. Should be easier to use now.
Timo Sirainen <tss@iki.fi>
parents: 10397
diff changeset
243 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
244 rcpt->data_reply_received = TRUE;
11496
2d6cf78982dc lmtp proxy: Fixed 30 second delay after finishing DATA.
Timo Sirainen <tss@iki.fi>
parents: 10825
diff changeset
245
2d6cf78982dc lmtp proxy: Fixed 30 second delay after finishing DATA.
Timo Sirainen <tss@iki.fi>
parents: 10825
diff changeset
246 lmtp_proxy_try_finish(conn->proxy);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
249 int lmtp_proxy_add_rcpt(struct lmtp_proxy *proxy, const char *address,
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
250 const struct lmtp_proxy_rcpt_settings *set)
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 {
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 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
253 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
254
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 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
256 if (conn->failed)
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 return -1;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
259 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
260 rcpt->conn = conn;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 rcpt->address = p_strdup(proxy->pool, address);
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
262 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
263
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 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
265 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
266 return 0;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 }
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
269 static void lmtp_proxy_conn_timeout(struct lmtp_proxy_connection *conn)
12240
1f784904111b lmtp proxy: Avoid hanging at the end of message input.
Timo Sirainen <tss@iki.fi>
parents: 11496
diff changeset
270 {
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
271 const char *line;
10284
2ad1ad1e1083 lmtp proxy: Added data input timeout.
Timo Sirainen <tss@iki.fi>
parents: 10278
diff changeset
272
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
273 line = t_strdup_printf(ERRSTR_TEMP_REMOTE_FAILURE
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
274 " (timeout while waiting for reply to %s)",
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
275 lmtp_client_state_to_string(conn->client));
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
276 lmtp_client_fail(conn->client, line);
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 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
280 const char *header,
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
281 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
282 {
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_connection *const *conns;
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
284
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
285 i_assert(data_input->seekable);
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
286
10400
96152031f5d9 lmtp proxy: More error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 10399
diff changeset
287 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
288 proxy->finish_context = context;
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
289 proxy->data_input = data_input;
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
290 i_stream_ref(proxy->data_input);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
292 array_foreach(&proxy->connections, conns) {
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
293 struct lmtp_proxy_connection *conn = *conns;
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
294
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
295 if (conn->finished) {
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
296 /* this connection had already failed */
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
297 continue;
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
298 }
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
299
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
300 conn->to = timeout_add(proxy->max_timeout_msecs,
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
301 lmtp_proxy_conn_timeout, conn);
10415
3662241f75f2 lmtp proxy fixes.
Timo Sirainen <tss@iki.fi>
parents: 10406
diff changeset
302
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
303 conn->data_input = i_stream_create_limit(data_input, (uoff_t)-1);
10406
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
304 lmtp_client_set_data_header(conn->client, header);
ad3fb3f929fc Use array_foreach() more.
Timo Sirainen <tss@iki.fi>
parents: 10402
diff changeset
305 lmtp_client_send(conn->client, conn->data_input);
13846
51d87deb5c26 lmtp: Simplify LMTP proxying by first reading the whole input to memory/disk.
Timo Sirainen <tss@iki.fi>
parents: 13477
diff changeset
306 lmtp_client_send_more(conn->client);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 }
14151
1bc56eec3f8e lmtp proxy: Send client's IP/port to destination server via XCLIENT if possible.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
308 lmtp_proxy_try_finish(proxy);
9832
3a16bec9c961 lmtp: Added initial support for proxying mails to other LMTP/SMTP servers.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309 }