annotate src/pop3-login/client.c @ 9658:8ba4253adc9b HEAD tip

*-login: SSL connections didn't get closed when the client got destroyed.
author Timo Sirainen <tss@iki.fi>
date Thu, 08 May 2014 16:41:29 +0300
parents 00cd9aacd03c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9532
00cd9aacd03c Updated copyright notices to include year 2010.
Timo Sirainen <tss@iki.fi>
parents: 9218
diff changeset
1 /* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "common.h"
2622
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
4 #include "base64.h"
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "buffer.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "ioloop.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "istream.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "ostream.h"
2622
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
9 #include "randgen.h"
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "process-title.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include "safe-memset.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include "strescape.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 #include "client.h"
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #include "client-authenticate.h"
1702
43815588dd6b Moved client side code for auth process handling to lib-auth. Some other login process cleanups.
Timo Sirainen <tss@iki.fi>
parents: 1499
diff changeset
15 #include "auth-client.h"
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 #include "ssl-proxy.h"
2773
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
17 #include "pop3-proxy.h"
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
18 #include "hostpid.h"
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
20 /* max. size of output buffer. if it gets full, the client is disconnected.
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
21 SASL authentication gives the largest output. */
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
22 #define MAX_OUTBUF_SIZE 4096
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 /* Disconnect client when it sends too many bad commands */
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 #define CLIENT_MAX_BAD_COMMANDS 10
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 /* When max. number of simultaneous connections is reached, few of the
7102
75f4e7ce8151 Minor cleanups
Timo Sirainen <tss@iki.fi>
parents: 7101
diff changeset
28 oldest connections are disconnected. Since we have to go through all of the
75f4e7ce8151 Minor cleanups
Timo Sirainen <tss@iki.fi>
parents: 7101
diff changeset
29 clients, it's faster if we disconnect multiple clients. */
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 #define CLIENT_DESTROY_OLDEST_COUNT 16
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31
9155
1c91fa0d804b login client idle timeout should be larger than auth request timeout, not vice versa.
Timo Sirainen <tss@iki.fi>
parents: 8985
diff changeset
32 #if CLIENT_LOGIN_IDLE_TIMEOUT_MSECS < AUTH_REQUEST_TIMEOUT*1000
1c91fa0d804b login client idle timeout should be larger than auth request timeout, not vice versa.
Timo Sirainen <tss@iki.fi>
parents: 8985
diff changeset
33 # error client idle timeout must be larger than authentication timeout
1083
f6ec28683512 auth: kill login connection if it leaves requests hanging too long.
Timo Sirainen <tss@iki.fi>
parents: 1079
diff changeset
34 #endif
f6ec28683512 auth: kill login connection if it leaves requests hanging too long.
Timo Sirainen <tss@iki.fi>
parents: 1079
diff changeset
35
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3360
diff changeset
36 const char *login_protocol = "POP3";
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3360
diff changeset
37
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 static void client_set_title(struct pop3_client *client)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 {
1486
2f5b3da28dd9 s/host/addr/ in variables
Timo Sirainen <tss@iki.fi>
parents: 1485
diff changeset
40 const char *addr;
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 if (!verbose_proctitle || !process_per_connection)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 return;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44
1486
2f5b3da28dd9 s/host/addr/ in variables
Timo Sirainen <tss@iki.fi>
parents: 1485
diff changeset
45 addr = net_ip2addr(&client->common.ip);
2f5b3da28dd9 s/host/addr/ in variables
Timo Sirainen <tss@iki.fi>
parents: 1485
diff changeset
46 if (addr == NULL)
2f5b3da28dd9 s/host/addr/ in variables
Timo Sirainen <tss@iki.fi>
parents: 1485
diff changeset
47 addr = "??";
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48
2763
ab14dffd0e91 tls/secured variables are in common client structure. Plaintext logins
Timo Sirainen <tss@iki.fi>
parents: 2733
diff changeset
49 process_title_set(t_strdup_printf(client->common.tls ?
ab14dffd0e91 tls/secured variables are in common client structure. Plaintext logins
Timo Sirainen <tss@iki.fi>
parents: 2733
diff changeset
50 "[%s TLS]" : "[%s]", addr));
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52
1079
3aece07f3a1b STARTTLS commands crashed. imap-login STARTTLS also ignored next command.
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
53 static void client_open_streams(struct pop3_client *client, int fd)
3aece07f3a1b STARTTLS commands crashed. imap-login STARTTLS also ignored next command.
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
54 {
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
55 client->common.input =
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
56 i_stream_create_fd(fd, LOGIN_MAX_INBUF_SIZE, FALSE);
6161
c62f7ee79446 Split o_stream_create_file() to _create_fd() and _create_fd_file().
Timo Sirainen <tss@iki.fi>
parents: 6153
diff changeset
57 client->output = o_stream_create_fd(fd, MAX_OUTBUF_SIZE, FALSE);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
58 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
59
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
60 static void client_start_tls(struct pop3_client *client)
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
61 {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
62 int fd_ssl;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
63
4560
507088c0d511 Fixes for handling near-full connection queues.
Timo Sirainen <tss@iki.fi>
parents: 4538
diff changeset
64 client_ref(client);
507088c0d511 Fixes for handling near-full connection queues.
Timo Sirainen <tss@iki.fi>
parents: 4538
diff changeset
65 connection_queue_add(1);
6153
fc7c4af3e870 If STARTTLS failed because max. number of connections for process was
Timo Sirainen <tss@iki.fi>
parents: 6142
diff changeset
66 if (!client_unref(client) || client->destroyed)
4560
507088c0d511 Fixes for handling near-full connection queues.
Timo Sirainen <tss@iki.fi>
parents: 4538
diff changeset
67 return;
507088c0d511 Fixes for handling near-full connection queues.
Timo Sirainen <tss@iki.fi>
parents: 4538
diff changeset
68
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
69 fd_ssl = ssl_proxy_new(client->common.fd, &client->common.ip,
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
70 &client->common.proxy);
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
71 if (fd_ssl == -1) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
72 client_send_line(client, "-ERR TLS initialization failed.");
4538
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
73 client_destroy(client,
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
74 "Disconnected: TLS initialization failed.");
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
75 return;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
76 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
77
2763
ab14dffd0e91 tls/secured variables are in common client structure. Plaintext logins
Timo Sirainen <tss@iki.fi>
parents: 2733
diff changeset
78 client->common.tls = TRUE;
ab14dffd0e91 tls/secured variables are in common client structure. Plaintext logins
Timo Sirainen <tss@iki.fi>
parents: 2733
diff changeset
79 client->common.secured = TRUE;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
80 client_set_title(client);
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
81
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
82 client->common.fd = fd_ssl;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
83
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
84 i_stream_unref(&client->common.input);
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
85 o_stream_unref(&client->output);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
86
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
87 client_open_streams(client, fd_ssl);
2733
9b9d9c164a31 Login process cleanups. Share more authentication code between pop3/imap.
Timo Sirainen <tss@iki.fi>
parents: 2691
diff changeset
88 client->io = io_add(client->common.fd, IO_READ, client_input, client);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
89 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
90
4907
5b4c9b20eba0 Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
91 static int client_output_starttls(struct pop3_client *client)
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
92 {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
93 int ret;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
94
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
95 if ((ret = o_stream_flush(client->output)) < 0) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
96 client_destroy(client, "Disconnected");
2790
02c0b8d532c2 Changed ostream's flush callback to have return value which can tell if
Timo Sirainen <tss@iki.fi>
parents: 2783
diff changeset
97 return 1;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
98 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
99
4125
5071c561c455 Same STARTTLS flush callback fix as with imap-login.
Timo Sirainen <tss@iki.fi>
parents: 3955
diff changeset
100 if (ret > 0) {
4903
204d7edc7cdc Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents: 4845
diff changeset
101 o_stream_unset_flush_callback(client->output);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
102 client_start_tls(client);
4125
5071c561c455 Same STARTTLS flush callback fix as with imap-login.
Timo Sirainen <tss@iki.fi>
parents: 3955
diff changeset
103 }
2790
02c0b8d532c2 Changed ostream's flush callback to have return value which can tell if
Timo Sirainen <tss@iki.fi>
parents: 2783
diff changeset
104 return 1;
1079
3aece07f3a1b STARTTLS commands crashed. imap-login STARTTLS also ignored next command.
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
105 }
3aece07f3a1b STARTTLS commands crashed. imap-login STARTTLS also ignored next command.
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
106
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
107 static bool cmd_stls(struct pop3_client *client)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 {
2763
ab14dffd0e91 tls/secured variables are in common client structure. Plaintext logins
Timo Sirainen <tss@iki.fi>
parents: 2733
diff changeset
109 if (client->common.tls) {
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 client_send_line(client, "-ERR TLS is already active.");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 return TRUE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 if (!ssl_initialized) {
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 client_send_line(client, "-ERR TLS support isn't enabled.");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 return TRUE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
119 /* remove input handler, SSL proxy gives us a new fd. we also have to
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
120 remove it in case we have to wait for buffer to be flushed */
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
121 if (client->io != NULL)
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
122 io_remove(&client->io);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
124 client_send_line(client, "+OK Begin TLS negotiation now.");
3581
cb143fc37b58 Make sure corking is removed before calling ssl_proxy_new()
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
125
cb143fc37b58 Make sure corking is removed before calling ssl_proxy_new()
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
126 /* uncork the old fd */
cb143fc37b58 Make sure corking is removed before calling ssl_proxy_new()
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
127 o_stream_uncork(client->output);
cb143fc37b58 Make sure corking is removed before calling ssl_proxy_new()
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
128
3360
8df4cfb22171 STARTTLS was broken
Timo Sirainen <tss@iki.fi>
parents: 3128
diff changeset
129 if (o_stream_flush(client->output) <= 0) {
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
130 /* the buffer has to be flushed */
3360
8df4cfb22171 STARTTLS was broken
Timo Sirainen <tss@iki.fi>
parents: 3128
diff changeset
131 o_stream_set_flush_pending(client->output, TRUE);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
132 o_stream_set_flush_callback(client->output,
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
133 client_output_starttls, client);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 } else {
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
135 client_start_tls(client);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 return TRUE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
140 static bool cmd_quit(struct pop3_client *client)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 {
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 client_send_line(client, "+OK Logging out");
8302
0db37acdc59f Login process: Log auth failure reasons better in disconnect message.
Timo Sirainen <tss@iki.fi>
parents: 8106
diff changeset
143 client_destroy(client, "Aborted login");
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 return TRUE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
147 static bool client_command_execute(struct pop3_client *client, const char *cmd,
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
148 const char *args)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 {
2058
0a1755f79392 cleanup: str_*case(t_strdup_noconst(str)) -> t_str_*case(str)
Timo Sirainen <tss@iki.fi>
parents: 2027
diff changeset
150 cmd = t_str_ucase(cmd);
1059
d805c2f1d6a9 Support for CAPA command (rfc2449).
Timo Sirainen <tss@iki.fi>
parents: 1054
diff changeset
151 if (strcmp(cmd, "CAPA") == 0)
d805c2f1d6a9 Support for CAPA command (rfc2449).
Timo Sirainen <tss@iki.fi>
parents: 1054
diff changeset
152 return cmd_capa(client, args);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153 if (strcmp(cmd, "USER") == 0)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 return cmd_user(client, args);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 if (strcmp(cmd, "PASS") == 0)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 return cmd_pass(client, args);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157 if (strcmp(cmd, "AUTH") == 0)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 return cmd_auth(client, args);
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
159 if (strcmp(cmd, "APOP") == 0)
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
160 return cmd_apop(client, args);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 if (strcmp(cmd, "STLS") == 0)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 return cmd_stls(client);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 if (strcmp(cmd, "QUIT") == 0)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
164 return cmd_quit(client);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165
1054
cd1ac4101adf compile fix + complain about unknown commands
Timo Sirainen <tss@iki.fi>
parents: 1049
diff changeset
166 client_send_line(client, "-ERR Unknown command.");
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 return FALSE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
170 bool client_read(struct pop3_client *client)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 {
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
172 switch (i_stream_read(client->common.input)) {
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 case -2:
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 /* buffer full */
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 client_send_line(client, "-ERR Input line too long, aborting");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 client_destroy(client, "Disconnected: Input buffer full");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 return FALSE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 case -1:
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 /* disconnected */
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 client_destroy(client, "Disconnected");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 return FALSE;
8577
164569761647 login: Reset idle timeout only when input is actually read from client.
Timo Sirainen <tss@iki.fi>
parents: 8574
diff changeset
182 case 0:
164569761647 login: Reset idle timeout only when input is actually read from client.
Timo Sirainen <tss@iki.fi>
parents: 8574
diff changeset
183 /* nothing new read */
164569761647 login: Reset idle timeout only when input is actually read from client.
Timo Sirainen <tss@iki.fi>
parents: 8574
diff changeset
184 return TRUE;
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 default:
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 /* something was read */
8577
164569761647 login: Reset idle timeout only when input is actually read from client.
Timo Sirainen <tss@iki.fi>
parents: 8574
diff changeset
187 timeout_reset(client->to_idle_disconnect);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 return TRUE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191
4907
5b4c9b20eba0 Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
192 void client_input(struct pop3_client *client)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193 {
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 char *line, *args;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195
8350
1d0cff9ae4cd Minor code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 8302
diff changeset
196 i_assert(!client->common.authenticating);
1d0cff9ae4cd Minor code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 8302
diff changeset
197
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198 if (!client_read(client))
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 return;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200
1714
96dab004a87a fixes. maybe it works now.
Timo Sirainen <tss@iki.fi>
parents: 1702
diff changeset
201 client_ref(client);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 o_stream_cork(client->output);
8350
1d0cff9ae4cd Minor code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 8302
diff changeset
204 /* if a command starts an authentication, stop processing further
1d0cff9ae4cd Minor code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 8302
diff changeset
205 commands until the authentication is finished. */
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
206 while (!client->output->closed && !client->common.authenticating &&
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
207 (line = i_stream_next_line(client->common.input)) != NULL) {
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 args = strchr(line, ' ');
4819
b0f6726975ca More const fixes
Timo Sirainen <tss@iki.fi>
parents: 4790
diff changeset
209 if (args != NULL)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 *args++ = '\0';
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211
4819
b0f6726975ca More const fixes
Timo Sirainen <tss@iki.fi>
parents: 4790
diff changeset
212 if (client_command_execute(client, line,
b0f6726975ca More const fixes
Timo Sirainen <tss@iki.fi>
parents: 4790
diff changeset
213 args != NULL ? args : ""))
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 client->bad_counter = 0;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 else if (++client->bad_counter > CLIENT_MAX_BAD_COMMANDS) {
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216 client_send_line(client, "-ERR Too many bad commands.");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 client_destroy(client,
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 "Disconnected: Too many bad commands");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 if (client_unref(client))
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
223 o_stream_uncork(client->output);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225
4560
507088c0d511 Fixes for handling near-full connection queues.
Timo Sirainen <tss@iki.fi>
parents: 4538
diff changeset
226 void client_destroy_oldest(void)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 {
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
228 struct client *client;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
229 struct pop3_client *destroy_buf[CLIENT_DESTROY_OLDEST_COUNT];
4538
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
230 unsigned int i, destroy_count;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
231
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
232 /* find the oldest clients and put them to destroy-buffer */
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
233 memset(destroy_buf, 0, sizeof(destroy_buf));
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234
4538
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
235 destroy_count = max_connections > CLIENT_DESTROY_OLDEST_COUNT*2 ?
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
236 CLIENT_DESTROY_OLDEST_COUNT : I_MIN(max_connections/2, 1);
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
237 for (client = clients; client != NULL; client = client->next) {
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
238 struct pop3_client *pop3_client = (struct pop3_client *)client;
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239
4538
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
240 for (i = 0; i < destroy_count; i++) {
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
241 if (destroy_buf[i] == NULL ||
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
242 destroy_buf[i]->created > pop3_client->created) {
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
243 /* @UNSAFE */
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
244 memmove(destroy_buf+i+1, destroy_buf+i,
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
245 sizeof(destroy_buf) -
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
246 (i+1) * sizeof(struct pop3_client *));
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
247 destroy_buf[i] = pop3_client;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
248 break;
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
249 }
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 /* then kill them */
4538
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
254 for (i = 0; i < destroy_count; i++) {
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
255 if (destroy_buf[i] == NULL)
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
256 break;
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
258 client_destroy(destroy_buf[i],
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259 "Disconnected: Connection queue full");
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
260 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262
2289
83df1d1bbb05 Fixes, patch by Andrey Panin
Timo Sirainen <tss@iki.fi>
parents: 2268
diff changeset
263 static char *get_apop_challenge(struct pop3_client *client)
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
264 {
2289
83df1d1bbb05 Fixes, patch by Andrey Panin
Timo Sirainen <tss@iki.fi>
parents: 2268
diff changeset
265 struct auth_connect_id *id = &client->auth_id;
2622
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
266 unsigned char buffer[16];
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
267 buffer_t *buf;
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
268
2289
83df1d1bbb05 Fixes, patch by Andrey Panin
Timo Sirainen <tss@iki.fi>
parents: 2268
diff changeset
269 if (!auth_client_reserve_connection(auth_client, "APOP", id))
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
270 return NULL;
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
271
2622
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
272 random_fill(buffer, sizeof(buffer));
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
273 buf = buffer_create_static_hard(pool_datastack_create(),
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
274 MAX_BASE64_ENCODED_SIZE(sizeof(buffer)) + 1);
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
275 base64_encode(buffer, sizeof(buffer), buf);
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
276 buffer_append_c(buf, '\0');
033d2fd1cd38 Replaced timestamp in APOP challenge with 128 bits of randomness.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
277
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6475
diff changeset
278 return i_strdup_printf("<%x.%x.%lx.%s@%s>",
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6475
diff changeset
279 id->server_pid, id->connect_uid,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6475
diff changeset
280 (unsigned long)ioloop_time,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6475
diff changeset
281 (const char *)buf->data, my_hostname);
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
282 }
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
283
2268
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
284 static void client_auth_ready(struct pop3_client *client)
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
285 {
2733
9b9d9c164a31 Login process cleanups. Share more authentication code between pop3/imap.
Timo Sirainen <tss@iki.fi>
parents: 2691
diff changeset
286 client->io = io_add(client->common.fd, IO_READ, client_input, client);
2268
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
287
2289
83df1d1bbb05 Fixes, patch by Andrey Panin
Timo Sirainen <tss@iki.fi>
parents: 2268
diff changeset
288 client->apop_challenge = get_apop_challenge(client);
2674
857f5f7b512b Added login_greeting and login_greeting_capability settings.
Timo Sirainen <tss@iki.fi>
parents: 2622
diff changeset
289 client_send_line(client, t_strconcat("+OK ", greeting,
4161
147d53a049ee Add a space between POP3 greeting and APOP challenge
Timo Sirainen <tss@iki.fi>
parents: 4125
diff changeset
290 client->apop_challenge != NULL ?
147d53a049ee Add a space between POP3 greeting and APOP challenge
Timo Sirainen <tss@iki.fi>
parents: 4125
diff changeset
291 " " : NULL,
2268
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
292 client->apop_challenge, NULL));
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
293 }
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
294
7099
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
295 static void client_idle_disconnect_timeout(struct pop3_client *client)
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
296 {
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
297 client_destroy(client, "Disconnected: Inactivity");
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
298 }
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
299
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
300 struct client *client_create(int fd, bool ssl, const struct ip_addr *local_ip,
2097
4e77cb0aff21 Added %l, %r and %P variables and mail_log_prefix setting.
Timo Sirainen <tss@iki.fi>
parents: 2058
diff changeset
301 const struct ip_addr *ip)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 {
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 struct pop3_client *client;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304
4664
881ed99266a2 New asserts / cleanup
Timo Sirainen <tss@iki.fi>
parents: 4560
diff changeset
305 i_assert(fd != -1);
881ed99266a2 New asserts / cleanup
Timo Sirainen <tss@iki.fi>
parents: 4560
diff changeset
306
4560
507088c0d511 Fixes for handling near-full connection queues.
Timo Sirainen <tss@iki.fi>
parents: 4538
diff changeset
307 connection_queue_add(1);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309 /* always use nonblocking I/O */
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 net_set_nonblock(fd, TRUE);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 client = i_new(struct pop3_client, 1);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
313 client->created = ioloop_time;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314 client->refcount = 1;
1725
cc0690f92d96 disable_plaintext_auth defaults to yes now. ipv4 127.* and ipv6 ::1
Timo Sirainen <tss@iki.fi>
parents: 1714
diff changeset
315
2097
4e77cb0aff21 Added %l, %r and %P variables and mail_log_prefix setting.
Timo Sirainen <tss@iki.fi>
parents: 2058
diff changeset
316 client->common.local_ip = *local_ip;
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
317 client->common.ip = *ip;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 client->common.fd = fd;
7920
3644883cf44e Support transferring original IPs and ports through IMAP proxies.
Timo Sirainen <tss@iki.fi>
parents: 7438
diff changeset
319 client->common.tls = ssl;
3644883cf44e Support transferring original IPs and ports through IMAP proxies.
Timo Sirainen <tss@iki.fi>
parents: 7438
diff changeset
320 client->common.trusted = client_is_trusted(&client->common);
3644883cf44e Support transferring original IPs and ports through IMAP proxies.
Timo Sirainen <tss@iki.fi>
parents: 7438
diff changeset
321 client->common.secured = ssl || client->common.trusted ||
3644883cf44e Support transferring original IPs and ports through IMAP proxies.
Timo Sirainen <tss@iki.fi>
parents: 7438
diff changeset
322 net_ip_compare(ip, local_ip);
3644883cf44e Support transferring original IPs and ports through IMAP proxies.
Timo Sirainen <tss@iki.fi>
parents: 7438
diff changeset
323
1079
3aece07f3a1b STARTTLS commands crashed. imap-login STARTTLS also ignored next command.
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
324 client_open_streams(client, fd);
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
325 client_link(&client->common);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327 main_ref();
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
328
2268
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
329 client->auth_connected = auth_client_is_connected(auth_client);
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
330 if (client->auth_connected)
5bd7fa814d7f If we're not connected to all auth sockets, wait before sending greeting so
Timo Sirainen <tss@iki.fi>
parents: 2267
diff changeset
331 client_auth_ready(client);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 client_set_title(client);
5671
0021765627f3 Check idle timeouts only when there are clients connected.
Timo Sirainen <tss@iki.fi>
parents: 5360
diff changeset
333
7099
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
334 client->to_idle_disconnect =
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
335 timeout_add(CLIENT_LOGIN_IDLE_TIMEOUT_MSECS,
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
336 client_idle_disconnect_timeout, client);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 return &client->common;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339
7438
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
340 void client_destroy_success(struct pop3_client *client, const char *reason)
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
341 {
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
342 client->login_success = TRUE;
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
343 client_destroy(client, reason);
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
344 }
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
345
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
346 void client_destroy(struct pop3_client *client, const char *reason)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 {
1148
b5b172ab0ed9 Make sure calling client_destroy() multiple times doesn't break anything.
Timo Sirainen <tss@iki.fi>
parents: 1117
diff changeset
348 if (client->destroyed)
b5b172ab0ed9 Make sure calling client_destroy() multiple times doesn't break anything.
Timo Sirainen <tss@iki.fi>
parents: 1117
diff changeset
349 return;
b5b172ab0ed9 Make sure calling client_destroy() multiple times doesn't break anything.
Timo Sirainen <tss@iki.fi>
parents: 1117
diff changeset
350 client->destroyed = TRUE;
b5b172ab0ed9 Make sure calling client_destroy() multiple times doesn't break anything.
Timo Sirainen <tss@iki.fi>
parents: 1117
diff changeset
351
7438
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
352 if (!client->login_success && reason != NULL) {
8302
0db37acdc59f Login process: Log auth failure reasons better in disconnect message.
Timo Sirainen <tss@iki.fi>
parents: 8106
diff changeset
353 reason = t_strconcat(reason, " ",
0db37acdc59f Login process: Log auth failure reasons better in disconnect message.
Timo Sirainen <tss@iki.fi>
parents: 8106
diff changeset
354 client_get_extra_disconnect_reason(&client->common),
0db37acdc59f Login process: Log auth failure reasons better in disconnect message.
Timo Sirainen <tss@iki.fi>
parents: 8106
diff changeset
355 NULL);
7438
65fbb6226141 Log clearly with "auth failed, # attempts" if user gets disconnected before
Timo Sirainen <tss@iki.fi>
parents: 7137
diff changeset
356 }
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 if (reason != NULL)
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3360
diff changeset
358 client_syslog(&client->common, reason);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
360 client_unlink(&client->common);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361
9658
8ba4253adc9b *-login: SSL connections didn't get closed when the client got destroyed.
Timo Sirainen <tss@iki.fi>
parents: 9532
diff changeset
362 if (!client->login_success && client->common.proxy != NULL)
8ba4253adc9b *-login: SSL connections didn't get closed when the client got destroyed.
Timo Sirainen <tss@iki.fi>
parents: 9532
diff changeset
363 ssl_proxy_destroy(client->common.proxy);
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
364 if (client->common.input != NULL)
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
365 i_stream_close(client->common.input);
2783
717f8579f1df crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2773
diff changeset
366 if (client->output != NULL)
717f8579f1df crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2773
diff changeset
367 o_stream_close(client->output);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368
8985
f43bebab3dac imap/pop3 proxy: Support SSL/TLS connections to remote servers.
Timo Sirainen <tss@iki.fi>
parents: 8891
diff changeset
369 if (client->common.master_tag != 0) {
f43bebab3dac imap/pop3 proxy: Support SSL/TLS connections to remote servers.
Timo Sirainen <tss@iki.fi>
parents: 8891
diff changeset
370 i_assert(client->common.auth_request == NULL);
f43bebab3dac imap/pop3 proxy: Support SSL/TLS connections to remote servers.
Timo Sirainen <tss@iki.fi>
parents: 8891
diff changeset
371 i_assert(client->common.authenticating);
4790
c6d77f917d12 Fixed potential problems with client disconnecting while master was handling
Timo Sirainen <tss@iki.fi>
parents: 4771
diff changeset
372 master_request_abort(&client->common);
8985
f43bebab3dac imap/pop3 proxy: Support SSL/TLS connections to remote servers.
Timo Sirainen <tss@iki.fi>
parents: 8891
diff changeset
373 } else if (client->common.auth_request != NULL) {
4770
88c29111fcee Crashfixes and more asserts. Mostly related to use of AUTHENTICATE/AUTH
Timo Sirainen <tss@iki.fi>
parents: 4671
diff changeset
374 i_assert(client->common.authenticating);
9218
754234248510 login processes: Auth code cleanups. Custom IMAP auth errors now have [ALERT] prefix.
Timo Sirainen <tss@iki.fi>
parents: 9155
diff changeset
375 sasl_server_auth_abort(&client->common);
4770
88c29111fcee Crashfixes and more asserts. Mostly related to use of AUTHENTICATE/AUTH
Timo Sirainen <tss@iki.fi>
parents: 4671
diff changeset
376 } else {
88c29111fcee Crashfixes and more asserts. Mostly related to use of AUTHENTICATE/AUTH
Timo Sirainen <tss@iki.fi>
parents: 4671
diff changeset
377 i_assert(!client->common.authenticating);
1702
43815588dd6b Moved client side code for auth process handling to lib-auth. Some other login process cleanups.
Timo Sirainen <tss@iki.fi>
parents: 1499
diff changeset
378 }
43815588dd6b Moved client side code for auth process handling to lib-auth. Some other login process cleanups.
Timo Sirainen <tss@iki.fi>
parents: 1499
diff changeset
379
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
380 if (client->io != NULL)
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
381 io_remove(&client->io);
7099
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
382 if (client->to_idle_disconnect != NULL)
3f5b7bebfd82 Use separate per-client timeouts instead of going through all clients in one
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
383 timeout_remove(&client->to_idle_disconnect);
8574
1b744c38bcac Increase failed login's reply delay by 5 seconds for each failure.
Timo Sirainen <tss@iki.fi>
parents: 8350
diff changeset
384 if (client->to_authfail_delay != NULL)
1b744c38bcac Increase failed login's reply delay by 5 seconds for each failure.
Timo Sirainen <tss@iki.fi>
parents: 8350
diff changeset
385 timeout_remove(&client->to_authfail_delay);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386
2773
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
387 if (client->common.fd != -1) {
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
388 net_disconnect(client->common.fd);
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
389 client->common.fd = -1;
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
390 }
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
391
2836
42a86c987c78 crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
392 if (client->proxy_password != NULL) {
2773
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
393 safe_memset(client->proxy_password, 0,
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
394 strlen(client->proxy_password));
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
395 i_free(client->proxy_password);
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
396 client->proxy_password = NULL;
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
397 }
e624a9ad6a30 More smart IMAP and POP3 proxies. Now if remote login fails, it just
Timo Sirainen <tss@iki.fi>
parents: 2768
diff changeset
398
2836
42a86c987c78 crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
399 i_free(client->proxy_user);
42a86c987c78 crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
400 client->proxy_user = NULL;
42a86c987c78 crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
401
8583
2ff2cac3578b imap/pop3-login: Cleaned up proxying code. Don't disconnect client on proxy failures.
Timo Sirainen <tss@iki.fi>
parents: 8577
diff changeset
402 if (client->proxy != NULL)
2ff2cac3578b imap/pop3-login: Cleaned up proxying code. Don't disconnect client on proxy failures.
Timo Sirainen <tss@iki.fi>
parents: 8577
diff changeset
403 login_proxy_free(&client->proxy);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
404
4790
c6d77f917d12 Fixed potential problems with client disconnecting while master was handling
Timo Sirainen <tss@iki.fi>
parents: 4771
diff changeset
405 if (client->common.proxy != NULL) {
2027
dc5d0da1abe9 Added ssl_require_client_cert auth-specific setting. Hide
Timo Sirainen <tss@iki.fi>
parents: 1897
diff changeset
406 ssl_proxy_free(client->common.proxy);
4790
c6d77f917d12 Fixed potential problems with client disconnecting while master was handling
Timo Sirainen <tss@iki.fi>
parents: 4771
diff changeset
407 client->common.proxy = NULL;
c6d77f917d12 Fixed potential problems with client disconnecting while master was handling
Timo Sirainen <tss@iki.fi>
parents: 4771
diff changeset
408 }
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409 client_unref(client);
4538
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
410
9d9e72374164 Fixes to login process handling, especially with
Timo Sirainen <tss@iki.fi>
parents: 4467
diff changeset
411 main_listen_start();
4671
830cee5ef3c0 Reference counting fix
Timo Sirainen <tss@iki.fi>
parents: 4669
diff changeset
412 main_unref();
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
413 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414
2768
d344be0bb70f Added IMAP and POP3 proxying support.
Timo Sirainen <tss@iki.fi>
parents: 2766
diff changeset
415 void client_destroy_internal_failure(struct pop3_client *client)
d344be0bb70f Added IMAP and POP3 proxying support.
Timo Sirainen <tss@iki.fi>
parents: 2766
diff changeset
416 {
d344be0bb70f Added IMAP and POP3 proxying support.
Timo Sirainen <tss@iki.fi>
parents: 2766
diff changeset
417 client_send_line(client, "-ERR [IN-USE] Internal login failure. "
d344be0bb70f Added IMAP and POP3 proxying support.
Timo Sirainen <tss@iki.fi>
parents: 2766
diff changeset
418 "Refer to server log for more information.");
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3360
diff changeset
419 client_destroy(client, "Internal login failure");
2768
d344be0bb70f Added IMAP and POP3 proxying support.
Timo Sirainen <tss@iki.fi>
parents: 2766
diff changeset
420 }
d344be0bb70f Added IMAP and POP3 proxying support.
Timo Sirainen <tss@iki.fi>
parents: 2766
diff changeset
421
1714
96dab004a87a fixes. maybe it works now.
Timo Sirainen <tss@iki.fi>
parents: 1702
diff changeset
422 void client_ref(struct pop3_client *client)
96dab004a87a fixes. maybe it works now.
Timo Sirainen <tss@iki.fi>
parents: 1702
diff changeset
423 {
96dab004a87a fixes. maybe it works now.
Timo Sirainen <tss@iki.fi>
parents: 1702
diff changeset
424 client->refcount++;
96dab004a87a fixes. maybe it works now.
Timo Sirainen <tss@iki.fi>
parents: 1702
diff changeset
425 }
96dab004a87a fixes. maybe it works now.
Timo Sirainen <tss@iki.fi>
parents: 1702
diff changeset
426
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3581
diff changeset
427 bool client_unref(struct pop3_client *client)
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
428 {
3955
295af5c1cce6 If client disconnected while we were trying to send authentication
Timo Sirainen <tss@iki.fi>
parents: 3954
diff changeset
429 i_assert(client->refcount > 0);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
430 if (--client->refcount > 0)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
431 return TRUE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
432
3954
99ed24b66363 Added asserts
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
433 i_assert(client->destroyed);
99ed24b66363 Added asserts
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
434
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
435 if (client->common.input != NULL)
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
436 i_stream_unref(&client->common.input);
2783
717f8579f1df crashfixes
Timo Sirainen <tss@iki.fi>
parents: 2773
diff changeset
437 if (client->output != NULL)
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
438 o_stream_unref(&client->output);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439
2897
90697bb3c368 Username given in USER was was not freed when destroying client.
Timo Sirainen <tss@iki.fi>
parents: 2836
diff changeset
440 i_free(client->last_user);
2267
d2e186f716d8 Added APOP authentication for POP3. Patch by Andrey Panin.
Timo Sirainen <tss@iki.fi>
parents: 2097
diff changeset
441 i_free(client->apop_challenge);
1148
b5b172ab0ed9 Make sure calling client_destroy() multiple times doesn't break anything.
Timo Sirainen <tss@iki.fi>
parents: 1117
diff changeset
442 i_free(client->common.virtual_user);
2766
26a091f3add6 Implemented support for LOGIN-REFERRALS using "referral" and "reason"
Timo Sirainen <tss@iki.fi>
parents: 2763
diff changeset
443 i_free(client->common.auth_mech_name);
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
444 i_free(client);
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
445
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
446 return FALSE;
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
447 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 void client_send_line(struct pop3_client *client, const char *line)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450 {
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
451 struct const_iovec iov[2];
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
452 ssize_t ret;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
453
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
454 iov[0].iov_base = line;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
455 iov[0].iov_len = strlen(line);
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
456 iov[1].iov_base = "\r\n";
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
457 iov[1].iov_len = 2;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2289
diff changeset
458
2901
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
459 ret = o_stream_sendv(client->output, iov, 2);
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
460 if (ret < 0 || (size_t)ret != iov[0].iov_len + iov[1].iov_len) {
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
461 /* either disconnection or buffer full. in either case we
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
462 want this connection destroyed. however destroying it here
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
463 might break things if client is still tried to be accessed
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
464 without being referenced.. */
7927
2351a81ce699 If commands are pipelined after the login command, pass them to the
Timo Sirainen <tss@iki.fi>
parents: 7920
diff changeset
465 i_stream_close(client->common.input);
2901
872172ffd005 Don't destroy client structure immediately when sending line fails. Fixes some writes to freed memory.
Timo Sirainen <tss@iki.fi>
parents: 2897
diff changeset
466 }
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
467 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
468
1702
43815588dd6b Moved client side code for auth process handling to lib-auth. Some other login process cleanups.
Timo Sirainen <tss@iki.fi>
parents: 1499
diff changeset
469 void clients_notify_auth_connected(void)
1275
af685269ead0 login: Wait until we're connected to auth process before executing command
Timo Sirainen <tss@iki.fi>
parents: 1235
diff changeset
470 {
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
471 struct client *client;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
472
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
473 for (client = clients; client != NULL; client = client->next) {
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
474 struct pop3_client *pop3_client = (struct pop3_client *)client;
1275
af685269ead0 login: Wait until we're connected to auth process before executing command
Timo Sirainen <tss@iki.fi>
parents: 1235
diff changeset
475
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
476 if (!pop3_client->auth_connected) {
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
477 pop3_client->auth_connected = TRUE;
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
478 client_auth_ready(pop3_client);
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
479 }
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
480 }
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
481 }
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
482
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
483 void clients_destroy_all(void)
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
484 {
8891
fbb2343b85d9 imap/pop3-login: Don't crash when shutting down and destroying clients.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
485 struct client *client, *next;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
486
8891
fbb2343b85d9 imap/pop3-login: Don't crash when shutting down and destroying clients.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
487 for (client = clients; client != NULL; client = next) {
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
488 struct pop3_client *pop3_client = (struct pop3_client *)client;
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
489
8891
fbb2343b85d9 imap/pop3-login: Don't crash when shutting down and destroying clients.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
490 next = client->next;
7101
09556a64b4e5 Use a linked list to keep track of all clients instead of a hash table.
Timo Sirainen <tss@iki.fi>
parents: 7100
diff changeset
491 client_destroy(pop3_client, "Disconnected: Shutting down");
1897
1e6ed8045f2b Changed hash_foreach() to iterator.
Timo Sirainen <tss@iki.fi>
parents: 1782
diff changeset
492 }
1049
c41787e8c3f4 Moved common login process code to login-common, created pop3-login.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
493 }
7137
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
494
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
495 void clients_init(void)
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
496 {
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
497 /* Nothing to initialize for POP3 */
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
498 }
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
499
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
500 void clients_deinit(void)
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
501 {
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
502 clients_destroy_all();
c33c87781ab4 Added clients_init() and clients_deinit() back (for Managesieve).
Timo Sirainen <tss@iki.fi>
parents: 7102
diff changeset
503 }