1 |
lefcha |
1.9 |
#ifndef IMAPFILTER_H |
2 |
|
|
#define IMAPFILTER_H |
3 |
|
|
|
4 |
|
|
|
5 |
lefcha |
1.68 |
#include <stdio.h> |
6 |
|
|
#include <sys/types.h> |
7 |
lefcha |
1.80 |
#include <limits.h> |
8 |
lefcha |
1.68 |
|
9 |
lefcha |
1.54 |
#include "config.h" |
10 |
lefcha |
1.65 |
#include "account.h" |
11 |
|
|
#include "filter.h" |
12 |
lefcha |
1.54 |
|
13 |
|
|
#ifdef SSL_TLS |
14 |
|
|
#include <openssl/ssl.h> |
15 |
|
|
#endif |
16 |
|
|
|
17 |
lefcha |
1.43 |
|
18 |
lefcha |
1.11 |
/* Error codes returned by functions. */ |
19 |
lefcha |
1.77 |
#define ERROR_UNDEFINED 1 |
20 |
|
|
#define ERROR_SIGNAL 2 |
21 |
|
|
#define ERROR_TRIVIAL 3 |
22 |
|
|
#define ERROR_FILE_OPEN 4 |
23 |
|
|
#define ERROR_LOCKFILE 5 |
24 |
|
|
#define ERROR_CONFIG_PARSE 6 |
25 |
|
|
#define ERROR_MEMORY_ALLOCATION 7 |
26 |
|
|
#define ERROR_SETUID 8 |
27 |
|
|
#define ERROR_TERMIO 9 |
28 |
|
|
#define ERROR_NETWORK 10 |
29 |
|
|
#define ERROR_SSL 11 |
30 |
|
|
#define ERROR_PASSPHRASE 12 |
31 |
|
|
#define ERROR_ENCRYPT 13 |
32 |
|
|
#define ERROR_DECRYPT 14 |
33 |
|
|
#define ERROR_FORK 15 |
34 |
lefcha |
1.9 |
|
35 |
lefcha |
1.54 |
/* SSL/TLS certificate status for the server. */ |
36 |
|
|
#define SSL_CERT_OK 0 |
37 |
|
|
#define SSL_CERT_NONEXISTENT 1 |
38 |
|
|
#define SSL_CERT_MISMATCH 2 |
39 |
|
|
|
40 |
|
|
/* Action to be applied, concerning the SSL/TLS certificate. */ |
41 |
|
|
#define SSL_CERT_ACTION_ACCEPT 0 |
42 |
|
|
#define SSL_CERT_ACTION_REJECT 1 |
43 |
|
|
#define SSL_CERT_ACTION_CONTINUE 0 |
44 |
|
|
#define SSL_CERT_ACTION_ABORT 1 |
45 |
|
|
|
46 |
lefcha |
1.30 |
/* Other flags. */ |
47 |
lefcha |
1.54 |
#define FLAG_DAEMON_MODE 0x01 |
48 |
|
|
#define FLAG_TTY 0x02 |
49 |
|
|
#define FLAG_TTY_MODIFIED 0x04 |
50 |
|
|
#define FLAG_BLANK_PASSWORD 0x08 |
51 |
lefcha |
1.67 |
#define FLAG_SIGUSR1_RECEIVED 0x10 |
52 |
lefcha |
1.25 |
|
53 |
|
|
/* Capabilities of mail server. */ |
54 |
|
|
#define CAPABILITY_NONE 0x00 |
55 |
|
|
#define CAPABILITY_NAMESPACE 0x01 |
56 |
lefcha |
1.61 |
#define CAPABILITY_AUTH_CRAM_MD5 0x02 |
57 |
lefcha |
1.62 |
#define CAPABILITY_STARTTLS 0x04 |
58 |
lefcha |
1.9 |
|
59 |
lefcha |
1.12 |
/* Flags for logger. */ |
60 |
lefcha |
1.55 |
#define LOG_PREAMBLE 0 |
61 |
lefcha |
1.49 |
#define LOG_ACCOUNT 1 |
62 |
|
|
#define LOG_MAILBOX 2 |
63 |
|
|
#define LOG_FILTER 3 |
64 |
|
|
#define LOG_ACTION 4 |
65 |
|
|
#define LOG_DESTINATION_ACCOUNT 5 |
66 |
lefcha |
1.12 |
#define LOG_DESTINATION_MAILBOX 6 |
67 |
lefcha |
1.55 |
#define LOG_HEADER 7 |
68 |
lefcha |
1.12 |
|
69 |
lefcha |
1.35 |
/* Store IMAP command type may replace,add or remove flags. */ |
70 |
|
|
#define STORE_FLAG_REPLACE 1 |
71 |
|
|
#define STORE_FLAG_ADD 2 |
72 |
|
|
#define STORE_FLAG_REMOVE 3 |
73 |
|
|
|
74 |
lefcha |
1.9 |
/* Buffer size of server's response. */ |
75 |
lefcha |
1.55 |
#define RESPONSE_BUF 4096 |
76 |
lefcha |
1.20 |
#define RESULT_BUF 512 |
77 |
lefcha |
1.9 |
|
78 |
lefcha |
1.16 |
/* Response codes. */ |
79 |
lefcha |
1.44 |
#define RESPONSE_NULLBODY -2 |
80 |
|
|
#define RESPONSE_NONE -1 |
81 |
lefcha |
1.16 |
#define RESPONSE_OK 0 |
82 |
|
|
#define RESPONSE_NO 1 |
83 |
lefcha |
1.44 |
#define RESPONSE_BAD 2 |
84 |
|
|
#define RESPONSE_BYE 3 |
85 |
|
|
#define RESPONSE_PREAUTH 4 |
86 |
|
|
#define RESPONSE_READONLY 5 |
87 |
|
|
#define RESPONSE_TRYCREATE 6 |
88 |
lefcha |
1.16 |
|
89 |
lefcha |
1.26 |
/* Encryption and decryption buffers. */ |
90 |
|
|
#define ENCRYPTION_BUF 1024 |
91 |
|
|
#define DECRYPTION_BUF 4096 |
92 |
|
|
|
93 |
lefcha |
1.35 |
/* Buffer to save flags of the store IMAP command. */ |
94 |
|
|
#define STORE_FLAGS_BUF 64 |
95 |
|
|
|
96 |
lefcha |
1.69 |
/* Length of charset for IMAP SEARCH requests. */ |
97 |
lefcha |
1.47 |
#define CHARSET_LEN 64 |
98 |
|
|
|
99 |
lefcha |
1.65 |
/* String lengths of master password and encrypted passwords. */ |
100 |
|
|
#define PASSPHRASE_LEN 256 |
101 |
|
|
|
102 |
|
|
/* Length of mailbox namespace prefix. */ |
103 |
|
|
#define NAMESPACE_PREFIX_LEN 64 |
104 |
|
|
|
105 |
lefcha |
1.1 |
|
106 |
|
|
#define min(A, B) ((A) < (B) ? (A) : (B)) |
107 |
lefcha |
1.15 |
#define plural(A) ((A) == 1 ? "" : "s") |
108 |
lefcha |
1.1 |
|
109 |
|
|
|
110 |
lefcha |
1.80 |
/* Program's options. */ |
111 |
|
|
typedef struct opts { |
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 |
|
|
} opts_t; |
128 |
|
|
|
129 |
lefcha |
1.64 |
/* IMAP connection. */ |
130 |
|
|
typedef struct conn { |
131 |
|
|
int sock; /* Socket. */ |
132 |
|
|
#ifdef SSL_TLS |
133 |
|
|
SSL *ssl; /* SSL socket. */ |
134 |
|
|
#endif |
135 |
|
|
unsigned int caps; /* Capabilities of the mail server. */ |
136 |
lefcha |
1.65 |
struct { /* Namespace of the mail server's mailboxes. */ |
137 |
|
|
char prefix[NAMESPACE_PREFIX_LEN]; /* Namespace prefix. */ |
138 |
|
|
char delim; /* Namespace delimiter. */ |
139 |
|
|
} nsp; |
140 |
lefcha |
1.64 |
} conn_t; |
141 |
|
|
|
142 |
|
|
|
143 |
lefcha |
1.65 |
/* action.c */ |
144 |
lefcha |
1.79 |
int apply_action(char *mbox, char *mesgs, unsigned int *type, account_t * raccount, char *destmbox, unsigned int *msgflags, char *args); |
145 |
lefcha |
1.65 |
|
146 |
lefcha |
1.61 |
/* auth.c */ |
147 |
lefcha |
1.62 |
#ifdef CRAM_MD5 |
148 |
lefcha |
1.65 |
int auth_cram_md5(conn_t * conn, char *user, char *pass); |
149 |
|
|
#endif |
150 |
lefcha |
1.61 |
|
151 |
lefcha |
1.65 |
/* cert.c */ |
152 |
|
|
#ifdef SSL_TLS |
153 |
|
|
int get_cert(conn_t * conn); |
154 |
lefcha |
1.62 |
#endif |
155 |
|
|
|
156 |
lefcha |
1.65 |
/* destroy.c */ |
157 |
|
|
void destroy_all(void); |
158 |
|
|
void destroy_unneeded(void); |
159 |
lefcha |
1.63 |
|
160 |
lefcha |
1.9 |
/* file.c */ |
161 |
lefcha |
1.25 |
int create_homedir(void); |
162 |
lefcha |
1.26 |
int exists_file(char *fname); |
163 |
|
|
int exists_dir(char *fname); |
164 |
lefcha |
1.25 |
int create_file(char *fname, mode_t mode); |
165 |
|
|
int check_file_perms(char *fname, mode_t mode); |
166 |
lefcha |
1.75 |
int check_dir_perms(char *dname, mode_t mode); |
167 |
lefcha |
1.54 |
|
168 |
lefcha |
1.9 |
/* imap.c */ |
169 |
lefcha |
1.64 |
int imap_noop(conn_t * conn); |
170 |
|
|
int imap_capability(conn_t * conn); |
171 |
|
|
int imap_namespace(conn_t * conn); |
172 |
|
|
int imap_logout(conn_t * conn); |
173 |
lefcha |
1.62 |
#ifdef SSL_TLS |
174 |
lefcha |
1.64 |
int imap_starttls(conn_t * conn); |
175 |
lefcha |
1.62 |
#endif |
176 |
lefcha |
1.64 |
int imap_authenticate(conn_t * conn, char *auth, int cont); |
177 |
|
|
int imap_login(conn_t * conn, char *user, char *pass); |
178 |
|
|
/* int imap_list(conn_t *conn, char *refer, char *mbox); */ |
179 |
|
|
int imap_subscribe(conn_t * conn, char *mbox); |
180 |
|
|
/* int imap_examine(conn_t *conn, char *mbox); */ |
181 |
|
|
int imap_select(conn_t * conn, char *mbox); |
182 |
|
|
int imap_status(conn_t * conn, char *mbox, char *items); |
183 |
|
|
int imap_create(conn_t * conn, char *mbox); |
184 |
|
|
int imap_search(conn_t * conn, char *charset, char *search); |
185 |
|
|
int imap_fetch(conn_t * conn, char *mesg, char *items); |
186 |
|
|
int imap_store(conn_t * conn, char *mesg, unsigned int mode, char *flags); |
187 |
|
|
int imap_copy(conn_t * conn, char *mesg, char *mbox); |
188 |
lefcha |
1.79 |
int imap_append(conn_t * conn, char *mbox, char *flags, char *date, unsigned int size); |
189 |
lefcha |
1.64 |
int imap_close(conn_t * conn); |
190 |
|
|
int imap_expunge(conn_t * conn); |
191 |
lefcha |
1.29 |
|
192 |
|
|
/* lock.c */ |
193 |
|
|
void lockfile_create(void); |
194 |
|
|
void lockfile_check(void); |
195 |
|
|
int lockfile_remove(void); |
196 |
lefcha |
1.30 |
void kill_imapfilter(void); |
197 |
lefcha |
1.9 |
|
198 |
|
|
/* log.c */ |
199 |
lefcha |
1.13 |
void info(const char *info,...); |
200 |
|
|
void verbose(const char *info,...); |
201 |
lefcha |
1.74 |
void debug(const char *debug,...); |
202 |
lefcha |
1.13 |
void error(const char *errmsg,...); |
203 |
|
|
void fatal(unsigned int errnum, const char *fatal,...); |
204 |
lefcha |
1.22 |
void catch_signals(void); |
205 |
lefcha |
1.74 |
int debug_start(void); |
206 |
|
|
int debug_stop(void); |
207 |
|
|
int log_start(void); |
208 |
|
|
int log_stop(void); |
209 |
lefcha |
1.12 |
void log_info(int flag, void *ptr); |
210 |
lefcha |
1.65 |
|
211 |
|
|
/* match.c */ |
212 |
|
|
int apply_filters(char *mbox, filter_t ** filters); |
213 |
lefcha |
1.9 |
|
214 |
lefcha |
1.13 |
/* memory.c */ |
215 |
|
|
void *xmalloc(size_t size); |
216 |
lefcha |
1.19 |
void *xrealloc(void *ptr, size_t size); |
217 |
lefcha |
1.26 |
void xfree(void *ptr); |
218 |
lefcha |
1.13 |
char *xstrdup(const char *s); |
219 |
|
|
|
220 |
lefcha |
1.27 |
void *smalloc(size_t size); |
221 |
|
|
void *srealloc(void *ptr, size_t size); |
222 |
|
|
void sfree(void *ptr); |
223 |
|
|
char *sstrdup(const char *s); |
224 |
|
|
void secmem_clear(void); |
225 |
lefcha |
1.49 |
|
226 |
lefcha |
1.27 |
void corefile_disable(void); |
227 |
|
|
|
228 |
lefcha |
1.16 |
/* misc.c */ |
229 |
|
|
char *strcasestr(const char *haystack, const char *needle); |
230 |
|
|
char *ultostr(unsigned long int num, int base); |
231 |
lefcha |
1.24 |
char *xstrncpy(char *dest, const char *src, size_t size); |
232 |
lefcha |
1.16 |
|
233 |
lefcha |
1.65 |
/* parse.c */ |
234 |
|
|
int read_config(char *cfg); |
235 |
|
|
void reread_config(char *cfg); |
236 |
|
|
int read_passwords(void); |
237 |
|
|
|
238 |
lefcha |
1.25 |
/* passwd.c */ |
239 |
lefcha |
1.26 |
void get_password(char *passwd, size_t pwlen); |
240 |
|
|
#ifdef ENCRYPTED_PASSWORDS |
241 |
lefcha |
1.60 |
int encrypt_passwords(FILE * fd, account_t ** accts); |
242 |
lefcha |
1.34 |
int decrypt_passwords(unsigned char **buf, FILE * fd); |
243 |
lefcha |
1.26 |
void password_editor(void); |
244 |
|
|
#endif |
245 |
lefcha |
1.25 |
|
246 |
lefcha |
1.9 |
/* request.c */ |
247 |
lefcha |
1.64 |
int test(conn_t * conn); |
248 |
|
|
int check_capabilities(conn_t * conn); |
249 |
|
|
int check_namespace(conn_t * conn); |
250 |
lefcha |
1.62 |
#ifdef SSL_TLS |
251 |
lefcha |
1.65 |
int negotiate_tls(conn_t * conn); |
252 |
lefcha |
1.62 |
#endif |
253 |
lefcha |
1.64 |
int login(conn_t * conn, char *user, char *pass); |
254 |
|
|
int check_mailbox(conn_t * conn, char *mbox); |
255 |
|
|
int select_mailbox(conn_t * conn, char *mbox); |
256 |
|
|
int mailbox_status(conn_t * conn, char *mbox); |
257 |
|
|
int close_mailbox(conn_t * conn); |
258 |
|
|
int logout(conn_t * conn); |
259 |
lefcha |
1.9 |
|
260 |
lefcha |
1.65 |
/* response.c */ |
261 |
lefcha |
1.64 |
int server_response(conn_t * conn, unsigned int tag); |
262 |
|
|
int greeting_response(conn_t * conn); |
263 |
|
|
int logout_response(conn_t * conn, unsigned int tag); |
264 |
|
|
int capability_response(conn_t * conn, unsigned int tag); |
265 |
lefcha |
1.79 |
int authenticate_response(conn_t * conn, unsigned int tag, unsigned char **cont); |
266 |
lefcha |
1.64 |
int namespace_response(conn_t * conn, unsigned int tag); |
267 |
|
|
int status_response(conn_t * conn, unsigned int tag, char *mbox); |
268 |
|
|
int select_response(conn_t * conn, unsigned int tag); |
269 |
|
|
int search_response(conn_t * conn, unsigned int tag, char **mesgs); |
270 |
|
|
int fetch_response(conn_t * conn, unsigned int tag, int reset, char *fetch); |
271 |
lefcha |
1.79 |
int fetchfast_response(conn_t * conn, char **flags, char **date, unsigned int *size, unsigned int tag); |
272 |
lefcha |
1.64 |
int append_response(conn_t * conn, unsigned int tag); |
273 |
|
|
int copy_response(conn_t * conn, unsigned int tag); |
274 |
lefcha |
1.25 |
|
275 |
|
|
/* socket.c */ |
276 |
lefcha |
1.80 |
int |
277 |
|
|
init_connection(conn_t * conn, char *serv, unsigned short int port, unsigned int protocol); |
278 |
lefcha |
1.62 |
#ifdef SSL_TLS |
279 |
lefcha |
1.65 |
int init_secure_connection(conn_t * conn, unsigned int protocol); |
280 |
lefcha |
1.62 |
#endif |
281 |
lefcha |
1.64 |
int close_connection(conn_t * conn); |
282 |
|
|
int socket_read(conn_t * conn, char *buf); |
283 |
|
|
int socket_write(conn_t * conn, char *data); |
284 |
lefcha |
1.25 |
|
285 |
|
|
/* tty.c */ |
286 |
|
|
int tty_store(void); |
287 |
|
|
int tty_disable_echo(void); |
288 |
|
|
int tty_restore(void); |
289 |
lefcha |
1.65 |
|
290 |
lefcha |
1.11 |
|
291 |
lefcha |
1.13 |
#endif /* IMAPFILTER_H */ |