1 |
#ifndef IMAPFILTER_H |
2 |
#define IMAPFILTER_H |
3 |
|
4 |
#include <stdio.h> |
5 |
#include <unistd.h> |
6 |
#include <sys/stat.h> |
7 |
#include <sys/types.h> |
8 |
#include <dirent.h> |
9 |
|
10 |
#ifndef DATA_H |
11 |
#include "data.h" |
12 |
#endif |
13 |
|
14 |
/* Error codes returned by functions. */ |
15 |
#define ERROR_SIGNAL 1 |
16 |
#define ERROR_TRIVIAL 2 |
17 |
#define ERROR_FILE_OPEN 3 |
18 |
#define ERROR_LOCKFILE 4 |
19 |
#define ERROR_CONFIG_PARSE 5 |
20 |
#define ERROR_MEMORY_ALLOCATION 6 |
21 |
#define ERROR_SETUID 7 |
22 |
#define ERROR_TERMIO 8 |
23 |
#define ERROR_NETWORK 9 |
24 |
#define ERROR_SSL 10 |
25 |
#define ERROR_PASSPHRASE 11 |
26 |
#define ERROR_ENCRYPT 12 |
27 |
#define ERROR_DECRYPT 13 |
28 |
#define ERROR_FORK 14 |
29 |
#define ERROR_UNDEFINED 15 |
30 |
|
31 |
/* Flags that control the program's execution options. */ |
32 |
#define OPTION_DETAILS_QUIET 0x0001 |
33 |
#define OPTION_DETAILS_NORMAL 0x0002 |
34 |
#define OPTION_DETAILS_VERBOSE 0x0004 |
35 |
#define OPTION_DETAILS_CLEAR 0xfff8 |
36 |
#define OPTION_ERRORS 0x0008 |
37 |
#define OPTION_EXPUNGE 0x0010 |
38 |
#define OPTION_HEADERS 0x0020 |
39 |
#define OPTION_NAMESPACE 0x0040 |
40 |
#define OPTION_SUBSCRIBE 0x0080 |
41 |
#define OPTION_WARNING 0x0100 |
42 |
#define OPTION_PASSWORD_EDITOR 0x0200 |
43 |
#define OPTION_DAEMON_MODE 0x0400 |
44 |
|
45 |
/* Other flags. */ |
46 |
#define FLAG_TTY_MODIFIED 0x01 |
47 |
#define FLAG_BLANK_PASSWORD 0x02 |
48 |
|
49 |
/* Capabilities of mail server. */ |
50 |
#define CAPABILITY_NONE 0x00 |
51 |
#define CAPABILITY_NAMESPACE 0x01 |
52 |
|
53 |
/* Flags for logger. */ |
54 |
#define LOG_WRITE 0 |
55 |
#define LOG_SERVER 1 |
56 |
#define LOG_USERNAME 2 |
57 |
#define LOG_MAILBOX 3 |
58 |
#define LOG_FILTER 4 |
59 |
#define LOG_ACTION 5 |
60 |
#define LOG_DESTINATION_MAILBOX 6 |
61 |
|
62 |
/* Buffer size of the IMAP command. */ |
63 |
#define SMALL_CMD 64 |
64 |
#define MEDIUM_CMD 512 |
65 |
#define BIG_CMD 32768 |
66 |
|
67 |
/* Store IMAP command type may replace,add or remove flags. */ |
68 |
#define STORE_FLAG_REPLACE 1 |
69 |
#define STORE_FLAG_ADD 2 |
70 |
#define STORE_FLAG_REMOVE 3 |
71 |
|
72 |
/* Buffer size of server's response. */ |
73 |
#define RESPONSE_BUF (4096 + 1) |
74 |
#define RESULT_BUF 512 |
75 |
|
76 |
/* Response codes. */ |
77 |
#define RESPONSE_NONE -2 |
78 |
#define RESPONSE_BAD -1 |
79 |
#define RESPONSE_OK 0 |
80 |
#define RESPONSE_NO 1 |
81 |
#define RESPONSE_BYE 2 |
82 |
#define RESPONSE_PREAUTH 3 |
83 |
#define RESPONSE_READONLY 4 |
84 |
#define RESPONSE_TRYCREATE 5 |
85 |
|
86 |
/* Size virtual buffer. */ |
87 |
#define VIRTUAL_BUF 4096 |
88 |
|
89 |
/* Length of mailbox namespace prefix. */ |
90 |
#define NAMESPACE_PREFIX_LEN 64 |
91 |
|
92 |
/* Encryption and decryption buffers. */ |
93 |
#define ENCRYPTION_BUF 1024 |
94 |
#define DECRYPTION_BUF 4096 |
95 |
|
96 |
/* Buffer to save flags of the store IMAP command. */ |
97 |
#define STORE_FLAGS_BUF 64 |
98 |
|
99 |
/* Maximum passwords the password editor can handle. */ |
100 |
#define EDITOR_PASSWORDS_MAX 64 |
101 |
|
102 |
|
103 |
#define min(A, B) ((A) < (B) ? (A) : (B)) |
104 |
#define plural(A) ((A) == 1 ? "" : "s") |
105 |
|
106 |
|
107 |
/* Secure memory information. */ |
108 |
typedef struct secmem { |
109 |
void *buf; /* Allocated memory buffer. */ |
110 |
size_t size; /* Size of the buffer. */ |
111 |
struct secmem *prev, *next; /* Previous/next node of doubly linked list. */ |
112 |
} secmem_t; |
113 |
|
114 |
|
115 |
/* Namespace of IMAP mailboxes. */ |
116 |
typedef struct namesp { |
117 |
char prefix[NAMESPACE_PREFIX_LEN]; |
118 |
char delim; |
119 |
} namesp_t; |
120 |
|
121 |
|
122 |
/* file.c */ |
123 |
int read_config(char *cfg); |
124 |
int parse_config(FILE * fd); |
125 |
void set_options(char *line, regmatch_t * match); |
126 |
|
127 |
int read_passwords(void); |
128 |
int parse_passwords(FILE * fd); |
129 |
int store_passwords(account_t * accts[]); |
130 |
|
131 |
int create_homedir(void); |
132 |
int exists_file(char *fname); |
133 |
int exists_dir(char *fname); |
134 |
int create_file(char *fname, mode_t mode); |
135 |
#ifdef CHECK_PERMISSIONS |
136 |
int check_file_perms(char *fname, mode_t mode); |
137 |
int check_dir_perms(char *dname, mode_t mode); |
138 |
#endif |
139 |
|
140 |
/* imapfilter.c */ |
141 |
void usage(void); |
142 |
|
143 |
/* imap.c */ |
144 |
unsigned int send_command(int *sock, char *cmd); |
145 |
#ifdef DEBUG |
146 |
int imap_noop(int *sock); |
147 |
#endif |
148 |
int imap_capability(int *sock); |
149 |
int imap_namespace(int *sock); |
150 |
int imap_logout(int *sock); |
151 |
int imap_login(int *sock, char *user, char *pass); |
152 |
/* int imap_list(int *sock, char *refer, char *mbox); */ |
153 |
int imap_subscribe(int *sock, char *mbox); |
154 |
int imap_examine(int *sock, char *mbox); |
155 |
int imap_select(int *sock, char *mbox); |
156 |
int imap_status(int *sock, char *mbox, char *items); |
157 |
int imap_create(int *sock, char *mbox); |
158 |
int imap_search(int *sock, char *search); |
159 |
int imap_fetch(int *sock, char *mesg, char *items); |
160 |
int imap_store(int *sock, char *mesg, unsigned int mode, char *flags); |
161 |
int imap_copy(int *sock, char *mesg, char *mbox); |
162 |
int imap_append(int *sock, char *mbox, unsigned int size); |
163 |
int imap_close(int *sock); |
164 |
int imap_expunge(int *sock); |
165 |
|
166 |
/* lock.c */ |
167 |
void lockfile_create(void); |
168 |
void lockfile_check(void); |
169 |
pid_t lockfile_pid(void); |
170 |
int lockfile_remove(void); |
171 |
void kill_imapfilter(void); |
172 |
|
173 |
/* log.c */ |
174 |
void info(const char *info,...); |
175 |
void verbose(const char *info,...); |
176 |
void error(const char *errmsg,...); |
177 |
void fatal(unsigned int errnum, const char *fatal,...); |
178 |
void catch_signals(void); |
179 |
void signal_handler(int sig); |
180 |
int open_logfile(void); |
181 |
int create_logfile(void); |
182 |
int close_logfile(void); |
183 |
void log_info(int flag, void *ptr); |
184 |
char *get_time(void); |
185 |
|
186 |
/* memory.c */ |
187 |
void *xmalloc(size_t size); |
188 |
void *xrealloc(void *ptr, size_t size); |
189 |
void xfree(void *ptr); |
190 |
char *xstrdup(const char *s); |
191 |
|
192 |
void *smalloc(size_t size); |
193 |
void *srealloc(void *ptr, size_t size); |
194 |
void sfree(void *ptr); |
195 |
char *sstrdup(const char *s); |
196 |
void secmem_append(secmem_t * node); |
197 |
secmem_t *secmem_find(void *ptr); |
198 |
void secmem_remove(secmem_t * node); |
199 |
void secmem_clear(void); |
200 |
void secmem_lock(void); |
201 |
|
202 |
void corefile_disable(void); |
203 |
|
204 |
/* misc.c */ |
205 |
char *strcasestr(const char *haystack, const char *needle); |
206 |
char *ultostr(unsigned long int num, int base); |
207 |
char *xstrncpy(char *dest, const char *src, size_t size); |
208 |
|
209 |
/* passwd.c */ |
210 |
void get_password(char *passwd, size_t pwlen); |
211 |
#ifdef ENCRYPTED_PASSWORDS |
212 |
int encrypt_passwords(FILE * fd, account_t * accts[]); |
213 |
int decrypt_passwords(unsigned char **buf, FILE * fd); |
214 |
void password_editor(void); |
215 |
#endif |
216 |
|
217 |
/* request.c */ |
218 |
int test(int *sock); |
219 |
int check_capabilities(int *sock); |
220 |
int check_namespace(int *sock, namesp_t * nsp); |
221 |
int login(int *sock, char *user, char *pass); |
222 |
int check_mailbox(int *sock, char *mbox, namesp_t * nsp); |
223 |
int select_mailbox(int *sock, char *mbox, namesp_t * nsp); |
224 |
int mailbox_status(int *sock, char *mbox, namesp_t * nsp); |
225 |
int close_mailbox(int *sock); |
226 |
int logout(int *sock); |
227 |
|
228 |
int apply_filters(filter_t ** filters); |
229 |
int match_filter(filter_t * filter, char **mesgs); |
230 |
|
231 |
void empty_fifo(mask_t ** mfifo); |
232 |
void queue_fifo(mask_t ** mfifo, mask_t * mask); |
233 |
mask_t *dequeue_fifo(mask_t ** mfifo); |
234 |
|
235 |
char *generate_filter_and(mask_t * mask, unsigned int masknum, |
236 |
unsigned int masklen); |
237 |
char *generate_filter_or(mask_t * mask, unsigned int masknum, |
238 |
unsigned int masklen); |
239 |
|
240 |
int apply_action(char *mesgs, unsigned int *type, account_t * raccount, |
241 |
char *destmbox, unsigned int *msgflags, char *args); |
242 |
int action_delete(char *mesgs, char *args); |
243 |
int action_copy(char *mesgs, char *destmbox, char *args); |
244 |
int action_move(char *mesgs, char *destmbox, char *args); |
245 |
int action_rcopy(char *mesgs, account_t * destacc, char *destmbox, char *args); |
246 |
int action_rmove(char *mesgs, account_t * destacc, char *destmbox, char *args); |
247 |
int action_flag(char *mesgs, unsigned int *type, unsigned int *msgflags, char *args); |
248 |
int action_list(char *mesgs, char *args); |
249 |
|
250 |
unsigned int count_messages(char *mesgs); |
251 |
char *convert_messages(char *mesgs); |
252 |
|
253 |
|
254 |
/* response.c */ |
255 |
void receive_response(int *sock, char *buf); |
256 |
int server_response(int *sock, unsigned int tag); |
257 |
void bye_response(char *buf); |
258 |
int greeting_response(int *sock); |
259 |
int logout_response(int *sock, unsigned int tag); |
260 |
int capability_response(int *sock, unsigned int tag); |
261 |
int namespace_response(int *sock, unsigned int tag, namesp_t * nsp); |
262 |
int status_response(int *sock, unsigned int tag, char *mbox); |
263 |
int select_response(int *sock, unsigned int tag); |
264 |
int search_response(int *sock, unsigned int tag, char **mesgs); |
265 |
int fetch_response(int *sock, int reset, char *fetch, unsigned int tag); |
266 |
int fetchsize_response(int *sock, unsigned int *size, unsigned int tag); |
267 |
int append_response(int *sock, unsigned int tag); |
268 |
int copy_response(int *sock, unsigned int tag); |
269 |
int analyze_response(char *buf); |
270 |
void init_vbuf(void); |
271 |
void reset_vbuf(void); |
272 |
void check_vbuf(size_t n); |
273 |
|
274 |
/* socket.c */ |
275 |
int init_connection(int *sock, char *serv, unsigned short int port, |
276 |
unsigned int protocol); |
277 |
int ssl_init(int *sock, unsigned int protocol); |
278 |
int close_connection(int *sock); |
279 |
int socket_read(int *sock, char *buf); |
280 |
int socket_write(int *sock, char *data); |
281 |
|
282 |
/* tty.c */ |
283 |
int tty_store(void); |
284 |
int tty_disable_echo(void); |
285 |
int tty_restore(void); |
286 |
|
287 |
#endif /* IMAPFILTER_H */ |