4 |
|
|
5 |
#include <stdio.h> |
#include <stdio.h> |
6 |
#include <sys/types.h> |
#include <sys/types.h> |
7 |
|
#include <limits.h> |
8 |
|
|
9 |
#include "config.h" |
#include "config.h" |
10 |
#include "account.h" |
#include "account.h" |
16 |
|
|
17 |
|
|
18 |
/* Error codes returned by functions. */ |
/* Error codes returned by functions. */ |
19 |
#define ERROR_SIGNAL 1 |
#define ERROR_UNDEFINED 1 |
20 |
#define ERROR_TRIVIAL 2 |
#define ERROR_TRIVIAL 2 |
21 |
#define ERROR_FILE_OPEN 3 |
#define ERROR_SIGNAL 3 |
22 |
#define ERROR_LOCKFILE 4 |
#define ERROR_FILEOPEN 4 |
23 |
#define ERROR_CONFIG_PARSE 5 |
#define ERROR_LOCKFILE 5 |
24 |
#define ERROR_MEMORY_ALLOCATION 6 |
#define ERROR_PARSER 6 |
25 |
#define ERROR_SETUID 7 |
#define ERROR_MEMALLOC 7 |
26 |
#define ERROR_TERMIO 8 |
#define ERROR_SETUID 8 |
27 |
#define ERROR_NETWORK 9 |
#define ERROR_TERMIO 9 |
28 |
#define ERROR_SSL 10 |
#define ERROR_NETWORK 10 |
29 |
#define ERROR_PASSPHRASE 11 |
#define ERROR_SSL 11 |
30 |
#define ERROR_ENCRYPT 12 |
#define ERROR_PASSPHRASE 12 |
31 |
#define ERROR_DECRYPT 13 |
#define ERROR_ENCRYPT 13 |
32 |
#define ERROR_FORK 14 |
#define ERROR_DECRYPT 14 |
33 |
#define ERROR_UNDEFINED 15 |
#define ERROR_FORK 15 |
34 |
|
|
35 |
/* SSL/TLS certificate status for the server. */ |
/* SSL/TLS certificate status for the server. */ |
36 |
#define SSL_CERT_OK 0 |
#define CERT_OK 0 |
37 |
#define SSL_CERT_NONEXISTENT 1 |
#define CERT_NONE 1 |
38 |
#define SSL_CERT_MISMATCH 2 |
#define CERT_MISMATCH 2 |
39 |
|
|
40 |
/* Action to be applied, concerning the SSL/TLS certificate. */ |
/* Action to be applied, concerning the SSL/TLS certificate. */ |
41 |
#define SSL_CERT_ACTION_ACCEPT 0 |
#define CERT_ACTION_ACCEPT 0 |
42 |
#define SSL_CERT_ACTION_REJECT 1 |
#define CERT_ACTION_REJECT 1 |
43 |
#define SSL_CERT_ACTION_CONTINUE 0 |
#define CERT_ACTION_CONTINUE 0 |
44 |
#define SSL_CERT_ACTION_ABORT 1 |
#define CERT_ACTION_ABORT 1 |
|
|
|
|
/* Flags that control the program's execution options. */ |
|
|
#define OPTION_DETAILS_QUIET 0x0001 |
|
|
#define OPTION_DETAILS_NORMAL 0x0002 |
|
|
#define OPTION_DETAILS_VERBOSE 0x0004 |
|
|
#define OPTION_ERRORS 0x0008 |
|
|
#define OPTION_EXPUNGE 0x0010 |
|
|
#define OPTION_HEADERS 0x0020 |
|
|
#define OPTION_NAMESPACE 0x0040 |
|
|
#define OPTION_SUBSCRIBE 0x0080 |
|
|
#define OPTION_PASSWORD_EDITOR 0x0100 |
|
|
#define OPTION_DAEMON_MODE 0x0200 |
|
|
#define OPTION_PEEK 0x0400 |
|
45 |
|
|
46 |
/* Other flags. */ |
/* Other flags. */ |
47 |
#define FLAG_DAEMON_MODE 0x01 |
#define FLAG_DAEMON 0x01 |
48 |
#define FLAG_TTY 0x02 |
#define FLAG_TTY 0x02 |
49 |
#define FLAG_TTY_MODIFIED 0x04 |
#define FLAG_TTYMOD 0x04 |
50 |
#define FLAG_BLANK_PASSWORD 0x08 |
#define FLAG_BLANKPASS 0x08 |
51 |
#define FLAG_SIGUSR1_RECEIVED 0x10 |
#define FLAG_SIGUSR1 0x10 |
52 |
|
|
53 |
|
/* IMAP protocol supported by the server. */ |
54 |
|
#define PROTOCOL_NONE 0 |
55 |
|
#define PROTOCOL_IMAP4REV1 1 |
56 |
|
#define PROTOCOL_IMAP4 2 |
57 |
|
|
58 |
/* Capabilities of mail server. */ |
/* Capabilities of mail server. */ |
59 |
#define CAPABILITY_NONE 0x00 |
#define CAPABILITY_NONE 0x00 |
60 |
#define CAPABILITY_NAMESPACE 0x01 |
#define CAPABILITY_NAMESPACE 0x01 |
61 |
#define CAPABILITY_AUTH_CRAM_MD5 0x02 |
#define CAPABILITY_CRAMMD5 0x02 |
62 |
#define CAPABILITY_STARTTLS 0x04 |
#define CAPABILITY_STARTTLS 0x04 |
63 |
|
|
64 |
/* Flags for logger. */ |
/* Flags for logger. */ |
65 |
#define LOG_PREAMBLE 0 |
#define LOG_PREAMBLE 0 |
66 |
#define LOG_ACCOUNT 1 |
#define LOG_ACCOUNT 1 |
67 |
#define LOG_MAILBOX 2 |
#define LOG_MBOX 2 |
68 |
#define LOG_FILTER 3 |
#define LOG_FILTER 3 |
69 |
#define LOG_ACTION 4 |
#define LOG_ACTION 4 |
70 |
#define LOG_DESTINATION_ACCOUNT 5 |
#define LOG_DEST_ACCOUNT 5 |
71 |
#define LOG_DESTINATION_MAILBOX 6 |
#define LOG_DEST_MBOX 6 |
72 |
#define LOG_HEADER 7 |
#define LOG_HEADER 7 |
|
|
|
|
/* Store IMAP command type may replace,add or remove flags. */ |
|
|
#define STORE_FLAG_REPLACE 1 |
|
|
#define STORE_FLAG_ADD 2 |
|
|
#define STORE_FLAG_REMOVE 3 |
|
|
|
|
|
/* Buffer size of server's response. */ |
|
|
#define RESPONSE_BUF 4096 |
|
|
#define RESULT_BUF 512 |
|
73 |
|
|
74 |
/* Response codes. */ |
/* Response codes. */ |
75 |
#define RESPONSE_NULLBODY -2 |
#define RESPONSE_NULLBODY -2 |
76 |
#define RESPONSE_NONE -1 |
#define RESPONSE_NONE -1 |
77 |
#define RESPONSE_OK 0 |
#define RESPONSE_OK 0 |
78 |
#define RESPONSE_NO 1 |
#define RESPONSE_NO 1 |
79 |
#define RESPONSE_BAD 2 |
#define RESPONSE_BAD 2 |
80 |
#define RESPONSE_BYE 3 |
#define RESPONSE_BYE 3 |
81 |
#define RESPONSE_PREAUTH 4 |
#define RESPONSE_PREAUTH 4 |
82 |
#define RESPONSE_READONLY 5 |
#define RESPONSE_READONLY 5 |
83 |
#define RESPONSE_TRYCREATE 6 |
#define RESPONSE_TRYCREATE 6 |
84 |
|
|
85 |
|
/* Buffer size of server's response. */ |
86 |
|
#define RESPONSE_BUF 4096 |
87 |
|
#define RESULT_BUF 512 |
88 |
|
|
89 |
/* Encryption and decryption buffers. */ |
/* Encryption and decryption buffers. */ |
90 |
#define ENCRYPTION_BUF 1024 |
#define ENCRYPT_BUF 1024 |
91 |
#define DECRYPTION_BUF 4096 |
#define DECRYPT_BUF 4096 |
92 |
|
|
93 |
/* Buffer to save flags of the store IMAP command. */ |
/* Buffer to save flags of the store IMAP command. */ |
94 |
#define STORE_FLAGS_BUF 64 |
#define STORE_FLAGS_BUF 64 |
95 |
|
|
96 |
/* Length of charset for IMAP SEARCH requests. */ |
/* Length of charset for IMAP SEARCH requests. */ |
97 |
#define CHARSET_LEN 64 |
#define CHARSET_LEN 64 |
98 |
|
|
99 |
/* String lengths of master password and encrypted passwords. */ |
/* String lengths of master password and encrypted passwords. */ |
100 |
#define PASSPHRASE_LEN 256 |
#define PASSPHRASE_LEN 256 |
101 |
|
|
102 |
/* Length of mailbox namespace prefix. */ |
/* Length of mailbox namespace prefix. */ |
103 |
#define NAMESPACE_PREFIX_LEN 64 |
#define NAMESPACE_PREFIX_LEN 64 |
104 |
|
|
105 |
|
|
106 |
#define min(A, B) ((A) < (B) ? (A) : (B)) |
#define min(A, B) ((A) < (B) ? (A) : (B)) |
107 |
#define plural(A) ((A) == 1 ? "" : "s") |
#define plural(A) ((A) == 1 ? "" : "s") |
108 |
|
|
109 |
|
|
110 |
|
/* Program's options. */ |
111 |
|
typedef struct options { |
112 |
|
int debug; /* Debugging level (1..2). */ |
113 |
|
int verbosity; /* Verbosity level (-2..2). */ |
114 |
|
int timeout; /* Server non-response timeout in seconds. */ |
115 |
|
int daemon; /* Daemon mode interval. */ |
116 |
|
int headers; /* Print messages headers to stdout. */ |
117 |
|
int errors; /* Errors appended to logfile. */ |
118 |
|
int namespace; /* Apply mailbox namespace automatically. */ |
119 |
|
int expunge; /* Expunge messages after they are marked |
120 |
|
* deleted. */ |
121 |
|
int subscribe; /* Subscribe newly created mailboxes. */ |
122 |
|
int peek; /* Do not mark messages seen when their |
123 |
|
* headers are received. */ |
124 |
|
int passwd_editor; /* Enter interactive password editor. */ |
125 |
|
char charset[CHARSET_LEN]; /* Search criteria character set. */ |
126 |
|
char logfile[PATH_MAX]; /* Log file for filtering information. */ |
127 |
|
int force_protocol; /* Protocol version to assume. */ |
128 |
|
} options_t; |
129 |
|
|
130 |
/* IMAP connection. */ |
/* IMAP connection. */ |
131 |
typedef struct conn { |
typedef struct connection { |
132 |
int sock; /* Socket. */ |
int sock; /* Socket. */ |
133 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
134 |
SSL *ssl; /* SSL socket. */ |
SSL *ssl; /* SSL socket. */ |
135 |
#endif |
#endif |
136 |
|
unsigned int proto; /* IMAP protocol. Currently IMAP4rev1 and |
137 |
|
* IMAP4 are supported. */ |
138 |
unsigned int caps; /* Capabilities of the mail server. */ |
unsigned int caps; /* Capabilities of the mail server. */ |
139 |
struct { /* Namespace of the mail server's mailboxes. */ |
struct { /* Namespace of the mail server's mailboxes. */ |
140 |
char prefix[NAMESPACE_PREFIX_LEN]; /* Namespace prefix. */ |
char prefix[NAMESPACE_PREFIX_LEN]; /* Namespace prefix. */ |
141 |
char delim; /* Namespace delimiter. */ |
char delim; /* Namespace delimiter. */ |
142 |
} nsp; |
} ns; |
143 |
} conn_t; |
} connection_t; |
144 |
|
|
145 |
|
|
146 |
/* action.c */ |
/* action.c */ |
147 |
int apply_action(char *mbox, char *mesgs, unsigned int *type, account_t * raccount, char *destmbox, unsigned int *msgflags, char *args); |
int apply_action(char *mbox, char *mesgs, unsigned int *type, account_t * destacct, char *destmbox, unsigned int *msgflags, char *args); |
148 |
|
|
149 |
/* auth.c */ |
/* auth.c */ |
150 |
#ifdef CRAM_MD5 |
#ifdef CRAM_MD5 |
151 |
int auth_cram_md5(conn_t * conn, char *user, char *pass); |
int auth_cram_md5(connection_t * conn, char *user, char *pass); |
152 |
#endif |
#endif |
153 |
|
|
154 |
/* cert.c */ |
/* cert.c */ |
155 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
156 |
int get_cert(conn_t * conn); |
int get_cert(connection_t * conn); |
157 |
#endif |
#endif |
158 |
|
|
159 |
/* destroy.c */ |
/* destroy.c */ |
165 |
int exists_file(char *fname); |
int exists_file(char *fname); |
166 |
int exists_dir(char *fname); |
int exists_dir(char *fname); |
167 |
int create_file(char *fname, mode_t mode); |
int create_file(char *fname, mode_t mode); |
|
#ifdef CHECK_PERMISSIONS |
|
168 |
int check_file_perms(char *fname, mode_t mode); |
int check_file_perms(char *fname, mode_t mode); |
169 |
int check_dir_perms(char *dname, mode_t mode); |
int check_dir_perms(char *dname, mode_t mode); |
|
#endif |
|
170 |
|
|
171 |
/* imap.c */ |
/* imap.c */ |
172 |
#ifdef DEBUG |
int imap_noop(connection_t * conn); |
173 |
int imap_noop(conn_t * conn); |
int imap_capability(connection_t * conn); |
174 |
#endif |
int imap_namespace(connection_t * conn); |
175 |
int imap_capability(conn_t * conn); |
int imap_logout(connection_t * conn); |
|
int imap_namespace(conn_t * conn); |
|
|
int imap_logout(conn_t * conn); |
|
176 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
177 |
int imap_starttls(conn_t * conn); |
int imap_starttls(connection_t * conn); |
178 |
#endif |
#endif |
179 |
int imap_authenticate(conn_t * conn, char *auth, int cont); |
int imap_authenticate(connection_t * conn, char *auth, int cont); |
180 |
int imap_login(conn_t * conn, char *user, char *pass); |
int imap_login(connection_t * conn, char *user, char *pass); |
181 |
/* int imap_list(conn_t *conn, char *refer, char *mbox); */ |
/* int imap_list(connection_t *conn, char *refer, char *mbox); */ |
182 |
int imap_subscribe(conn_t * conn, char *mbox); |
int imap_subscribe(connection_t * conn, char *mbox); |
183 |
/* int imap_examine(conn_t *conn, char *mbox); */ |
int imap_examine(connection_t * conn, char *mbox); |
184 |
int imap_select(conn_t * conn, char *mbox); |
int imap_select(connection_t * conn, char *mbox); |
185 |
int imap_status(conn_t * conn, char *mbox, char *items); |
int imap_status(connection_t * conn, char *mbox, char *items); |
186 |
int imap_create(conn_t * conn, char *mbox); |
int imap_create(connection_t * conn, char *mbox); |
187 |
int imap_search(conn_t * conn, char *charset, char *search); |
int imap_search(connection_t * conn, char *charset, char *search); |
188 |
int imap_fetch(conn_t * conn, char *mesg, char *items); |
int imap_fetch(connection_t * conn, char *mesg, char *items); |
189 |
int imap_store(conn_t * conn, char *mesg, unsigned int mode, char *flags); |
int imap_store(connection_t * conn, char *mesg, unsigned int mode, char *flags); |
190 |
int imap_copy(conn_t * conn, char *mesg, char *mbox); |
int imap_copy(connection_t * conn, char *mesg, char *mbox); |
191 |
int imap_append(conn_t * conn, char *mbox, char *flags, char *date, unsigned int size); |
int imap_append(connection_t * conn, char *mbox, char *flags, char *date, unsigned int size); |
192 |
int imap_close(conn_t * conn); |
int imap_close(connection_t * conn); |
193 |
int imap_expunge(conn_t * conn); |
int imap_expunge(connection_t * conn); |
194 |
|
|
195 |
/* lock.c */ |
/* lock.c */ |
196 |
void lockfile_create(void); |
void lockfile_create(void); |
201 |
/* log.c */ |
/* log.c */ |
202 |
void info(const char *info,...); |
void info(const char *info,...); |
203 |
void verbose(const char *info,...); |
void verbose(const char *info,...); |
204 |
|
void debug(const char *debug,...); |
205 |
void error(const char *errmsg,...); |
void error(const char *errmsg,...); |
206 |
void fatal(unsigned int errnum, const char *fatal,...); |
void fatal(unsigned int errnum, const char *fatal,...); |
207 |
void catch_signals(void); |
void catch_signals(void); |
208 |
int open_logfile(void); |
int debug_start(void); |
209 |
int close_logfile(void); |
int debug_stop(void); |
210 |
|
int log_start(void); |
211 |
|
int log_stop(void); |
212 |
void log_info(int flag, void *ptr); |
void log_info(int flag, void *ptr); |
213 |
|
|
214 |
/* match.c */ |
/* match.c */ |
224 |
void *srealloc(void *ptr, size_t size); |
void *srealloc(void *ptr, size_t size); |
225 |
void sfree(void *ptr); |
void sfree(void *ptr); |
226 |
char *sstrdup(const char *s); |
char *sstrdup(const char *s); |
227 |
|
|
228 |
void secmem_clear(void); |
void secmem_clear(void); |
229 |
|
|
230 |
void corefile_disable(void); |
void corefile_disable(void); |
248 |
#endif |
#endif |
249 |
|
|
250 |
/* request.c */ |
/* request.c */ |
251 |
int test(conn_t * conn); |
int test(connection_t * conn); |
252 |
int check_capabilities(conn_t * conn); |
int check_capabilities(connection_t * conn); |
253 |
int check_namespace(conn_t * conn); |
int check_namespace(connection_t * conn); |
254 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
255 |
int negotiate_tls(conn_t * conn); |
int negotiate_tls(connection_t * conn); |
256 |
#endif |
#endif |
257 |
int login(conn_t * conn, char *user, char *pass); |
int login(connection_t * conn, char *user, char *pass); |
258 |
int check_mailbox(conn_t * conn, char *mbox); |
int check_mailbox(connection_t * conn, char *mbox); |
259 |
int select_mailbox(conn_t * conn, char *mbox); |
int select_mailbox(connection_t * conn, char *mbox); |
260 |
int mailbox_status(conn_t * conn, char *mbox); |
int mailbox_status(connection_t * conn, char *mbox); |
261 |
int close_mailbox(conn_t * conn); |
int close_mailbox(connection_t * conn); |
262 |
int logout(conn_t * conn); |
int logout(connection_t * conn); |
263 |
|
|
264 |
/* response.c */ |
/* response.c */ |
265 |
int server_response(conn_t * conn, unsigned int tag); |
int response_generic(connection_t * conn, unsigned int tag); |
266 |
int greeting_response(conn_t * conn); |
int response_greeting(connection_t * conn); |
267 |
int logout_response(conn_t * conn, unsigned int tag); |
int response_logout(connection_t * conn, unsigned int tag); |
268 |
int capability_response(conn_t * conn, unsigned int tag); |
int response_capability(connection_t * conn, unsigned int tag); |
269 |
int authenticate_response(conn_t * conn, unsigned int tag, unsigned char **cont); |
int response_authenticate(connection_t * conn, unsigned int tag, unsigned char **cont); |
270 |
int namespace_response(conn_t * conn, unsigned int tag); |
int response_namespace(connection_t * conn, unsigned int tag); |
271 |
int status_response(conn_t * conn, unsigned int tag, char *mbox); |
int response_status(connection_t * conn, unsigned int tag, char *mbox); |
272 |
int select_response(conn_t * conn, unsigned int tag); |
int response_examine(connection_t * conn, unsigned int tag, char *mbox); |
273 |
int search_response(conn_t * conn, unsigned int tag, char **mesgs); |
int response_select(connection_t * conn, unsigned int tag); |
274 |
int fetch_response(conn_t * conn, unsigned int tag, int reset, char *fetch); |
int response_search(connection_t * conn, unsigned int tag, char **mesgs); |
275 |
int fetchfast_response(conn_t * conn, char **flags, char **date, unsigned int *size, unsigned int tag); |
int response_fetch(connection_t * conn, unsigned int tag, int reset, char *fetch); |
276 |
int append_response(conn_t * conn, unsigned int tag); |
int response_fetchfast(connection_t * conn, char **flags, char **date, unsigned int *size, unsigned int tag); |
277 |
int copy_response(conn_t * conn, unsigned int tag); |
int response_append(connection_t * conn, unsigned int tag); |
278 |
|
int response_copy(connection_t * conn, unsigned int tag); |
279 |
|
|
280 |
/* socket.c */ |
/* socket.c */ |
281 |
int init_connection(conn_t * conn, char *serv, unsigned short int port, unsigned int protocol); |
int |
282 |
|
init_connection(connection_t * conn, char *serv, unsigned short int port, unsigned int protocol); |
283 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
284 |
int init_secure_connection(conn_t * conn, unsigned int protocol); |
int init_secure_connection(connection_t * conn, unsigned int protocol); |
285 |
#endif |
#endif |
286 |
int close_connection(conn_t * conn); |
int close_connection(connection_t * conn); |
287 |
int socket_read(conn_t * conn, char *buf); |
int socket_read(connection_t * conn, char *buf); |
288 |
int socket_write(conn_t * conn, char *data); |
int socket_write(connection_t * conn, char *data); |
289 |
|
|
290 |
/* tty.c */ |
/* tty.c */ |
291 |
int tty_store(void); |
int tty_store(void); |
292 |
int tty_disable_echo(void); |
int tty_noecho(void); |
293 |
int tty_restore(void); |
int tty_restore(void); |
294 |
|
|
295 |
|
|