1 #ifndef IMAP_LOGIN_CLIENT_H
2 #define IMAP_LOGIN_CLIENT_H
3 
4 #include "net.h"
5 #include "imap-id.h"
6 #include "client-common.h"
7 
8 /* Master prefix is: <1|0><imap tag><NUL> */
9 #define IMAP_TAG_MAX_LEN (LOGIN_MAX_MASTER_PREFIX_LEN-2)
10 
11 /* maximum length for IMAP command line. */
12 #define IMAP_LOGIN_MAX_LINE_LENGTH 8192
13 
14 enum imap_client_id_state {
15 	IMAP_CLIENT_ID_STATE_LIST = 0,
16 	IMAP_CLIENT_ID_STATE_KEY,
17 	IMAP_CLIENT_ID_STATE_VALUE
18 };
19 
20 /* Multiple commands can be sent pipelined, so the sent_state is a bitmask */
21 enum imap_proxy_sent_state {
22 	IMAP_PROXY_SENT_STATE_ID		= 0x01,
23 	IMAP_PROXY_SENT_STATE_STARTTLS		= 0x02,
24 	IMAP_PROXY_SENT_STATE_CAPABILITY	= 0x04,
25 	IMAP_PROXY_SENT_STATE_AUTHENTICATE	= 0x08,
26 	IMAP_PROXY_SENT_STATE_AUTH_CONTINUE	= 0x10,
27 	IMAP_PROXY_SENT_STATE_LOGIN		= 0x20,
28 
29 	IMAP_PROXY_SENT_STATE_COUNT = 6
30 };
31 
32 enum imap_proxy_rcvd_state {
33 	IMAP_PROXY_RCVD_STATE_NONE,
34 	IMAP_PROXY_RCVD_STATE_BANNER,
35 	IMAP_PROXY_RCVD_STATE_ID,
36 	IMAP_PROXY_RCVD_STATE_STARTTLS,
37 	IMAP_PROXY_RCVD_STATE_CAPABILITY,
38 	IMAP_PROXY_RCVD_STATE_AUTH_CONTINUE,
39 	IMAP_PROXY_RCVD_STATE_LOGIN,
40 
41 	IMAP_PROXY_RCVD_STATE_COUNT
42 };
43 
44 struct imap_client_cmd_id {
45 	struct imap_parser *parser;
46 
47 	enum imap_client_id_state state;
48 	char key[IMAP_ID_KEY_MAX_LEN+1];
49 
50 	char **log_keys;
51 	string_t *log_reply;
52 };
53 
54 struct imap_client {
55 	struct client common;
56 
57 	const struct imap_login_settings *set;
58 	struct imap_parser *parser;
59 	char *proxy_backend_capability;
60 
61 	const char *cmd_tag, *cmd_name;
62 	struct imap_client_cmd_id *cmd_id;
63 
64 	enum imap_proxy_sent_state proxy_sent_state;
65 	enum imap_proxy_rcvd_state proxy_rcvd_state;
66 
67 	bool cmd_finished:1;
68 	bool proxy_sasl_ir:1;
69 	bool proxy_logindisabled:1;
70 	bool proxy_seen_banner:1;
71 	bool skip_line:1;
72 	bool id_logged:1;
73 	bool proxy_capability_request_sent:1;
74 	bool client_ignores_capability_resp_code:1;
75 	bool auth_mech_name_parsed:1;
76 };
77 
78 bool client_skip_line(struct imap_client *client);
79 
80 enum imap_cmd_reply {
81 	IMAP_CMD_REPLY_OK,
82 	IMAP_CMD_REPLY_NO,
83 	IMAP_CMD_REPLY_BAD,
84 	IMAP_CMD_REPLY_BYE
85 };
86 
87 void client_send_reply(struct client *client,
88 		       enum imap_cmd_reply reply, const char *text);
89 void client_send_reply_code(struct client *client,
90 			    enum imap_cmd_reply reply, const char *resp_code,
91 			    const char *text) ATTR_NULL(3);
92 bool client_handle_parser_error(struct imap_client *client,
93 				struct imap_parser *parser);
94 
95 int cmd_id(struct imap_client *client);
96 
97 #endif
98