]>
Commit | Line | Data |
---|---|---|
f2561fda MM |
1 | /* |
2 | * git-imap-send - drops patches into an imap Drafts folder | |
3 | * derived from isync/mbsync - mailbox synchronizer | |
4 | * | |
5 | * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org> | |
6 | * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net> | |
7 | * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu> | |
8 | * Copyright (C) 2006 Mike McCormack | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | */ | |
24 | ||
25 | #include "cache.h" | |
684ec6c6 RS |
26 | #ifdef NO_OPENSSL |
27 | typedef void *SSL; | |
28 | #endif | |
f2561fda | 29 | |
9f1ad541 | 30 | struct store_conf { |
f2561fda MM |
31 | char *name; |
32 | const char *path; /* should this be here? its interpretation is driver-specific */ | |
33 | char *map_inbox; | |
34 | char *trash; | |
35 | unsigned max_size; /* off_t is overkill */ | |
36 | unsigned trash_remote_new:1, trash_only_new:1; | |
9f1ad541 | 37 | }; |
f2561fda | 38 | |
9f1ad541 | 39 | struct string_list { |
f2561fda MM |
40 | struct string_list *next; |
41 | char string[1]; | |
9f1ad541 | 42 | }; |
f2561fda | 43 | |
9f1ad541 | 44 | struct channel_conf { |
f2561fda MM |
45 | struct channel_conf *next; |
46 | char *name; | |
9f1ad541 | 47 | struct store_conf *master, *slave; |
f2561fda MM |
48 | char *master_name, *slave_name; |
49 | char *sync_state; | |
9f1ad541 | 50 | struct string_list *patterns; |
f2561fda MM |
51 | int mops, sops; |
52 | unsigned max_messages; /* for slave only */ | |
9f1ad541 | 53 | }; |
f2561fda | 54 | |
9f1ad541 | 55 | struct group_conf { |
f2561fda MM |
56 | struct group_conf *next; |
57 | char *name; | |
9f1ad541 JH |
58 | struct string_list *channels; |
59 | }; | |
f2561fda MM |
60 | |
61 | /* For message->status */ | |
62 | #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */ | |
63 | #define M_DEAD (1<<1) /* expunged */ | |
64 | #define M_FLAGS (1<<2) /* flags fetched */ | |
65 | ||
9f1ad541 | 66 | struct message { |
f2561fda | 67 | struct message *next; |
9f1ad541 | 68 | /* struct string_list *keywords; */ |
f2561fda MM |
69 | size_t size; /* zero implies "not fetched" */ |
70 | int uid; | |
71 | unsigned char flags, status; | |
9f1ad541 | 72 | }; |
f2561fda | 73 | |
9f1ad541 JH |
74 | struct store { |
75 | struct store_conf *conf; /* foreign */ | |
f2561fda MM |
76 | |
77 | /* currently open mailbox */ | |
78 | const char *name; /* foreign! maybe preset? */ | |
79 | char *path; /* own */ | |
9f1ad541 | 80 | struct message *msgs; /* own */ |
f2561fda MM |
81 | int uidvalidity; |
82 | unsigned char opts; /* maybe preset? */ | |
83 | /* note that the following do _not_ reflect stats from msgs, but mailbox totals */ | |
84 | int count; /* # of messages */ | |
85 | int recent; /* # of recent messages - don't trust this beyond the initial read */ | |
9f1ad541 | 86 | }; |
f2561fda | 87 | |
9f1ad541 | 88 | struct msg_data { |
f2561fda MM |
89 | char *data; |
90 | int len; | |
91 | unsigned char flags; | |
2bda77e0 | 92 | unsigned int crlf:1; |
9f1ad541 | 93 | }; |
f2561fda MM |
94 | |
95 | #define DRV_OK 0 | |
96 | #define DRV_MSG_BAD -1 | |
97 | #define DRV_BOX_BAD -2 | |
98 | #define DRV_STORE_BAD -3 | |
99 | ||
100 | static int Verbose, Quiet; | |
101 | ||
95c53908 RS |
102 | static void imap_info(const char *, ...); |
103 | static void imap_warn(const char *, ...); | |
f2561fda | 104 | |
95c53908 | 105 | static char *next_arg(char **); |
f2561fda | 106 | |
9f1ad541 | 107 | static void free_generic_messages(struct message *); |
f2561fda | 108 | |
95c53908 | 109 | static int nfsnprintf(char *buf, int blen, const char *fmt, ...); |
f2561fda | 110 | |
19247e55 PH |
111 | static int nfvasprintf(char **strp, const char *fmt, va_list ap) |
112 | { | |
113 | int len; | |
114 | char tmp[8192]; | |
115 | ||
116 | len = vsnprintf(tmp, sizeof(tmp), fmt, ap); | |
117 | if (len < 0) | |
118 | die("Fatal: Out of memory\n"); | |
119 | if (len >= sizeof(tmp)) | |
120 | die("imap command overflow !\n"); | |
121 | *strp = xmemdupz(tmp, len); | |
122 | return len; | |
123 | } | |
f2561fda | 124 | |
95c53908 RS |
125 | static void arc4_init(void); |
126 | static unsigned char arc4_getbyte(void); | |
f2561fda | 127 | |
9f1ad541 | 128 | struct imap_server_conf { |
f2561fda MM |
129 | char *name; |
130 | char *tunnel; | |
131 | char *host; | |
132 | int port; | |
133 | char *user; | |
134 | char *pass; | |
684ec6c6 RS |
135 | int use_ssl; |
136 | int ssl_verify; | |
9f1ad541 | 137 | }; |
f2561fda | 138 | |
9f1ad541 JH |
139 | struct imap_store_conf { |
140 | struct store_conf gen; | |
141 | struct imap_server_conf *server; | |
f2561fda | 142 | unsigned use_namespace:1; |
9f1ad541 | 143 | }; |
f2561fda | 144 | |
9f1ad541 JH |
145 | #define NIL (void *)0x1 |
146 | #define LIST (void *)0x2 | |
f2561fda | 147 | |
9f1ad541 JH |
148 | struct imap_list { |
149 | struct imap_list *next, *child; | |
f2561fda MM |
150 | char *val; |
151 | int len; | |
9f1ad541 | 152 | }; |
f2561fda | 153 | |
9f1ad541 | 154 | struct imap_socket { |
f2561fda | 155 | int fd; |
684ec6c6 | 156 | SSL *ssl; |
9f1ad541 | 157 | }; |
f2561fda | 158 | |
9f1ad541 JH |
159 | struct imap_buffer { |
160 | struct imap_socket sock; | |
f2561fda MM |
161 | int bytes; |
162 | int offset; | |
163 | char buf[1024]; | |
9f1ad541 | 164 | }; |
f2561fda MM |
165 | |
166 | struct imap_cmd; | |
167 | ||
9f1ad541 | 168 | struct imap { |
f2561fda | 169 | int uidnext; /* from SELECT responses */ |
9f1ad541 | 170 | struct imap_list *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */ |
f2561fda MM |
171 | unsigned caps, rcaps; /* CAPABILITY results */ |
172 | /* command queue */ | |
173 | int nexttag, num_in_progress, literal_pending; | |
174 | struct imap_cmd *in_progress, **in_progress_append; | |
9f1ad541 JH |
175 | struct imap_buffer buf; /* this is BIG, so put it last */ |
176 | }; | |
f2561fda | 177 | |
9f1ad541 JH |
178 | struct imap_store { |
179 | struct store gen; | |
f2561fda | 180 | int uidvalidity; |
9f1ad541 | 181 | struct imap *imap; |
f2561fda MM |
182 | const char *prefix; |
183 | unsigned /*currentnc:1,*/ trashnc:1; | |
9f1ad541 | 184 | }; |
f2561fda MM |
185 | |
186 | struct imap_cmd_cb { | |
9f1ad541 JH |
187 | int (*cont)(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt); |
188 | void (*done)(struct imap_store *ctx, struct imap_cmd *cmd, int response); | |
f2561fda MM |
189 | void *ctx; |
190 | char *data; | |
191 | int dlen; | |
192 | int uid; | |
193 | unsigned create:1, trycreate:1; | |
194 | }; | |
195 | ||
196 | struct imap_cmd { | |
197 | struct imap_cmd *next; | |
198 | struct imap_cmd_cb cb; | |
199 | char *cmd; | |
200 | int tag; | |
201 | }; | |
202 | ||
203 | #define CAP(cap) (imap->caps & (1 << (cap))) | |
204 | ||
205 | enum CAPABILITY { | |
206 | NOLOGIN = 0, | |
207 | UIDPLUS, | |
208 | LITERALPLUS, | |
209 | NAMESPACE, | |
684ec6c6 | 210 | STARTTLS, |
f2561fda MM |
211 | }; |
212 | ||
213 | static const char *cap_list[] = { | |
214 | "LOGINDISABLED", | |
215 | "UIDPLUS", | |
216 | "LITERAL+", | |
217 | "NAMESPACE", | |
684ec6c6 | 218 | "STARTTLS", |
f2561fda MM |
219 | }; |
220 | ||
221 | #define RESP_OK 0 | |
222 | #define RESP_NO 1 | |
223 | #define RESP_BAD 2 | |
224 | ||
9f1ad541 | 225 | static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd); |
f2561fda MM |
226 | |
227 | ||
228 | static const char *Flags[] = { | |
229 | "Draft", | |
230 | "Flagged", | |
231 | "Answered", | |
232 | "Seen", | |
233 | "Deleted", | |
234 | }; | |
235 | ||
684ec6c6 RS |
236 | #ifndef NO_OPENSSL |
237 | static void ssl_socket_perror(const char *func) | |
238 | { | |
239 | fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), 0)); | |
240 | } | |
241 | #endif | |
242 | ||
9f1ad541 | 243 | static void socket_perror(const char *func, struct imap_socket *sock, int ret) |
f2561fda | 244 | { |
684ec6c6 RS |
245 | #ifndef NO_OPENSSL |
246 | if (sock->ssl) { | |
247 | int sslerr = SSL_get_error(sock->ssl, ret); | |
248 | switch (sslerr) { | |
249 | case SSL_ERROR_NONE: | |
250 | break; | |
251 | case SSL_ERROR_SYSCALL: | |
252 | perror("SSL_connect"); | |
253 | break; | |
254 | default: | |
255 | ssl_socket_perror("SSL_connect"); | |
256 | break; | |
257 | } | |
258 | } else | |
259 | #endif | |
260 | { | |
261 | if (ret < 0) | |
262 | perror(func); | |
263 | else | |
264 | fprintf(stderr, "%s: unexpected EOF\n", func); | |
265 | } | |
266 | } | |
267 | ||
9f1ad541 | 268 | static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify) |
684ec6c6 RS |
269 | { |
270 | #ifdef NO_OPENSSL | |
271 | fprintf(stderr, "SSL requested but SSL support not compiled in\n"); | |
272 | return -1; | |
273 | #else | |
274 | SSL_METHOD *meth; | |
275 | SSL_CTX *ctx; | |
276 | int ret; | |
277 | ||
278 | SSL_library_init(); | |
279 | SSL_load_error_strings(); | |
280 | ||
281 | if (use_tls_only) | |
282 | meth = TLSv1_method(); | |
f2561fda | 283 | else |
684ec6c6 RS |
284 | meth = SSLv23_method(); |
285 | ||
286 | if (!meth) { | |
287 | ssl_socket_perror("SSLv23_method"); | |
288 | return -1; | |
289 | } | |
290 | ||
291 | ctx = SSL_CTX_new(meth); | |
292 | ||
293 | if (verify) | |
294 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); | |
295 | ||
296 | if (!SSL_CTX_set_default_verify_paths(ctx)) { | |
297 | ssl_socket_perror("SSL_CTX_set_default_verify_paths"); | |
298 | return -1; | |
299 | } | |
300 | sock->ssl = SSL_new(ctx); | |
301 | if (!sock->ssl) { | |
302 | ssl_socket_perror("SSL_new"); | |
303 | return -1; | |
304 | } | |
305 | if (!SSL_set_fd(sock->ssl, sock->fd)) { | |
306 | ssl_socket_perror("SSL_set_fd"); | |
307 | return -1; | |
308 | } | |
309 | ||
310 | ret = SSL_connect(sock->ssl); | |
311 | if (ret <= 0) { | |
312 | socket_perror("SSL_connect", sock, ret); | |
313 | return -1; | |
314 | } | |
315 | ||
316 | return 0; | |
317 | #endif | |
f2561fda MM |
318 | } |
319 | ||
9f1ad541 | 320 | static int socket_read(struct imap_socket *sock, char *buf, int len) |
f2561fda | 321 | { |
684ec6c6 RS |
322 | ssize_t n; |
323 | #ifndef NO_OPENSSL | |
324 | if (sock->ssl) | |
325 | n = SSL_read(sock->ssl, buf, len); | |
326 | else | |
327 | #endif | |
95c53908 | 328 | n = xread(sock->fd, buf, len); |
f2561fda | 329 | if (n <= 0) { |
95c53908 RS |
330 | socket_perror("read", sock, n); |
331 | close(sock->fd); | |
f2561fda MM |
332 | sock->fd = -1; |
333 | } | |
334 | return n; | |
335 | } | |
336 | ||
9f1ad541 | 337 | static int socket_write(struct imap_socket *sock, const char *buf, int len) |
f2561fda | 338 | { |
684ec6c6 RS |
339 | int n; |
340 | #ifndef NO_OPENSSL | |
341 | if (sock->ssl) | |
342 | n = SSL_write(sock->ssl, buf, len); | |
343 | else | |
344 | #endif | |
95c53908 | 345 | n = write_in_full(sock->fd, buf, len); |
f2561fda | 346 | if (n != len) { |
95c53908 RS |
347 | socket_perror("write", sock, n); |
348 | close(sock->fd); | |
f2561fda MM |
349 | sock->fd = -1; |
350 | } | |
351 | return n; | |
352 | } | |
353 | ||
9f1ad541 | 354 | static void socket_shutdown(struct imap_socket *sock) |
684ec6c6 RS |
355 | { |
356 | #ifndef NO_OPENSSL | |
357 | if (sock->ssl) { | |
358 | SSL_shutdown(sock->ssl); | |
359 | SSL_free(sock->ssl); | |
360 | } | |
361 | #endif | |
362 | close(sock->fd); | |
363 | } | |
364 | ||
f2561fda | 365 | /* simple line buffering */ |
9f1ad541 | 366 | static int buffer_gets(struct imap_buffer *b, char **s) |
f2561fda MM |
367 | { |
368 | int n; | |
369 | int start = b->offset; | |
370 | ||
371 | *s = b->buf + start; | |
372 | ||
373 | for (;;) { | |
374 | /* make sure we have enough data to read the \r\n sequence */ | |
375 | if (b->offset + 1 >= b->bytes) { | |
376 | if (start) { | |
377 | /* shift down used bytes */ | |
378 | *s = b->buf; | |
379 | ||
95c53908 | 380 | assert(start <= b->bytes); |
f2561fda MM |
381 | n = b->bytes - start; |
382 | ||
383 | if (n) | |
173a9cbe | 384 | memmove(b->buf, b->buf + start, n); |
f2561fda MM |
385 | b->offset -= start; |
386 | b->bytes = n; | |
387 | start = 0; | |
388 | } | |
389 | ||
95c53908 RS |
390 | n = socket_read(&b->sock, b->buf + b->bytes, |
391 | sizeof(b->buf) - b->bytes); | |
f2561fda MM |
392 | |
393 | if (n <= 0) | |
394 | return -1; | |
395 | ||
396 | b->bytes += n; | |
397 | } | |
398 | ||
399 | if (b->buf[b->offset] == '\r') { | |
95c53908 | 400 | assert(b->offset + 1 < b->bytes); |
f2561fda MM |
401 | if (b->buf[b->offset + 1] == '\n') { |
402 | b->buf[b->offset] = 0; /* terminate the string */ | |
403 | b->offset += 2; /* next line */ | |
404 | if (Verbose) | |
95c53908 | 405 | puts(*s); |
f2561fda MM |
406 | return 0; |
407 | } | |
408 | } | |
409 | ||
410 | b->offset++; | |
411 | } | |
412 | /* not reached */ | |
413 | } | |
414 | ||
95c53908 | 415 | static void imap_info(const char *msg, ...) |
f2561fda MM |
416 | { |
417 | va_list va; | |
418 | ||
419 | if (!Quiet) { | |
95c53908 RS |
420 | va_start(va, msg); |
421 | vprintf(msg, va); | |
422 | va_end(va); | |
423 | fflush(stdout); | |
f2561fda MM |
424 | } |
425 | } | |
426 | ||
95c53908 | 427 | static void imap_warn(const char *msg, ...) |
f2561fda MM |
428 | { |
429 | va_list va; | |
430 | ||
431 | if (Quiet < 2) { | |
95c53908 RS |
432 | va_start(va, msg); |
433 | vfprintf(stderr, msg, va); | |
434 | va_end(va); | |
f2561fda MM |
435 | } |
436 | } | |
437 | ||
95c53908 | 438 | static char *next_arg(char **s) |
f2561fda MM |
439 | { |
440 | char *ret; | |
441 | ||
442 | if (!s || !*s) | |
5142db69 | 443 | return NULL; |
95c53908 | 444 | while (isspace((unsigned char) **s)) |
f2561fda MM |
445 | (*s)++; |
446 | if (!**s) { | |
5142db69 RS |
447 | *s = NULL; |
448 | return NULL; | |
f2561fda MM |
449 | } |
450 | if (**s == '"') { | |
451 | ++*s; | |
452 | ret = *s; | |
95c53908 | 453 | *s = strchr(*s, '"'); |
f2561fda MM |
454 | } else { |
455 | ret = *s; | |
95c53908 | 456 | while (**s && !isspace((unsigned char) **s)) |
f2561fda MM |
457 | (*s)++; |
458 | } | |
459 | if (*s) { | |
460 | if (**s) | |
461 | *(*s)++ = 0; | |
462 | if (!**s) | |
5142db69 | 463 | *s = NULL; |
f2561fda MM |
464 | } |
465 | return ret; | |
466 | } | |
467 | ||
9f1ad541 | 468 | static void free_generic_messages(struct message *msgs) |
f2561fda | 469 | { |
9f1ad541 | 470 | struct message *tmsg; |
f2561fda MM |
471 | |
472 | for (; msgs; msgs = tmsg) { | |
473 | tmsg = msgs->next; | |
95c53908 | 474 | free(msgs); |
f2561fda MM |
475 | } |
476 | } | |
477 | ||
95c53908 | 478 | static int nfsnprintf(char *buf, int blen, const char *fmt, ...) |
f2561fda MM |
479 | { |
480 | int ret; | |
481 | va_list va; | |
482 | ||
95c53908 RS |
483 | va_start(va, fmt); |
484 | if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen) | |
485 | die("Fatal: buffer too small. Please report a bug.\n"); | |
486 | va_end(va); | |
f2561fda MM |
487 | return ret; |
488 | } | |
489 | ||
f2561fda MM |
490 | static struct { |
491 | unsigned char i, j, s[256]; | |
492 | } rs; | |
493 | ||
95c53908 | 494 | static void arc4_init(void) |
f2561fda MM |
495 | { |
496 | int i, fd; | |
497 | unsigned char j, si, dat[128]; | |
498 | ||
95c53908 RS |
499 | if ((fd = open("/dev/urandom", O_RDONLY)) < 0 && (fd = open("/dev/random", O_RDONLY)) < 0) { |
500 | fprintf(stderr, "Fatal: no random number source available.\n"); | |
501 | exit(3); | |
f2561fda | 502 | } |
95c53908 RS |
503 | if (read_in_full(fd, dat, 128) != 128) { |
504 | fprintf(stderr, "Fatal: cannot read random number source.\n"); | |
505 | exit(3); | |
f2561fda | 506 | } |
95c53908 | 507 | close(fd); |
f2561fda MM |
508 | |
509 | for (i = 0; i < 256; i++) | |
510 | rs.s[i] = i; | |
511 | for (i = j = 0; i < 256; i++) { | |
512 | si = rs.s[i]; | |
513 | j += si + dat[i & 127]; | |
514 | rs.s[i] = rs.s[j]; | |
515 | rs.s[j] = si; | |
516 | } | |
517 | rs.i = rs.j = 0; | |
518 | ||
519 | for (i = 0; i < 256; i++) | |
520 | arc4_getbyte(); | |
521 | } | |
522 | ||
95c53908 | 523 | static unsigned char arc4_getbyte(void) |
f2561fda MM |
524 | { |
525 | unsigned char si, sj; | |
526 | ||
527 | rs.i++; | |
528 | si = rs.s[rs.i]; | |
529 | rs.j += si; | |
530 | sj = rs.s[rs.j]; | |
531 | rs.s[rs.i] = sj; | |
532 | rs.s[rs.j] = si; | |
533 | return rs.s[(si + sj) & 0xff]; | |
534 | } | |
535 | ||
9f1ad541 | 536 | static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx, |
95c53908 RS |
537 | struct imap_cmd_cb *cb, |
538 | const char *fmt, va_list ap) | |
f2561fda | 539 | { |
9f1ad541 | 540 | struct imap *imap = ctx->imap; |
f2561fda MM |
541 | struct imap_cmd *cmd; |
542 | int n, bufl; | |
543 | char buf[1024]; | |
544 | ||
95c53908 RS |
545 | cmd = xmalloc(sizeof(struct imap_cmd)); |
546 | nfvasprintf(&cmd->cmd, fmt, ap); | |
f2561fda MM |
547 | cmd->tag = ++imap->nexttag; |
548 | ||
549 | if (cb) | |
550 | cmd->cb = *cb; | |
551 | else | |
95c53908 | 552 | memset(&cmd->cb, 0, sizeof(cmd->cb)); |
f2561fda MM |
553 | |
554 | while (imap->literal_pending) | |
95c53908 | 555 | get_cmd_result(ctx, NULL); |
f2561fda | 556 | |
95c53908 | 557 | bufl = nfsnprintf(buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ? |
f2561fda | 558 | "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n", |
95c53908 | 559 | cmd->tag, cmd->cmd, cmd->cb.dlen); |
f2561fda MM |
560 | if (Verbose) { |
561 | if (imap->num_in_progress) | |
95c53908 RS |
562 | printf("(%d in progress) ", imap->num_in_progress); |
563 | if (memcmp(cmd->cmd, "LOGIN", 5)) | |
564 | printf(">>> %s", buf); | |
f2561fda | 565 | else |
95c53908 | 566 | printf(">>> %d LOGIN <user> <pass>\n", cmd->tag); |
f2561fda | 567 | } |
95c53908 RS |
568 | if (socket_write(&imap->buf.sock, buf, bufl) != bufl) { |
569 | free(cmd->cmd); | |
570 | free(cmd); | |
8e0f7003 | 571 | if (cb) |
95c53908 | 572 | free(cb->data); |
f2561fda MM |
573 | return NULL; |
574 | } | |
575 | if (cmd->cb.data) { | |
576 | if (CAP(LITERALPLUS)) { | |
95c53908 RS |
577 | n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen); |
578 | free(cmd->cb.data); | |
f2561fda | 579 | if (n != cmd->cb.dlen || |
9f1ad541 | 580 | (n = socket_write(&imap->buf.sock, "\r\n", 2)) != 2) { |
95c53908 RS |
581 | free(cmd->cmd); |
582 | free(cmd); | |
f2561fda MM |
583 | return NULL; |
584 | } | |
5142db69 | 585 | cmd->cb.data = NULL; |
f2561fda MM |
586 | } else |
587 | imap->literal_pending = 1; | |
588 | } else if (cmd->cb.cont) | |
589 | imap->literal_pending = 1; | |
5142db69 | 590 | cmd->next = NULL; |
f2561fda MM |
591 | *imap->in_progress_append = cmd; |
592 | imap->in_progress_append = &cmd->next; | |
593 | imap->num_in_progress++; | |
594 | return cmd; | |
595 | } | |
596 | ||
9f1ad541 | 597 | static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx, |
95c53908 RS |
598 | struct imap_cmd_cb *cb, |
599 | const char *fmt, ...) | |
f2561fda MM |
600 | { |
601 | struct imap_cmd *ret; | |
602 | va_list ap; | |
603 | ||
95c53908 RS |
604 | va_start(ap, fmt); |
605 | ret = v_issue_imap_cmd(ctx, cb, fmt, ap); | |
606 | va_end(ap); | |
f2561fda MM |
607 | return ret; |
608 | } | |
609 | ||
9f1ad541 | 610 | static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 611 | const char *fmt, ...) |
f2561fda MM |
612 | { |
613 | va_list ap; | |
614 | struct imap_cmd *cmdp; | |
615 | ||
95c53908 RS |
616 | va_start(ap, fmt); |
617 | cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap); | |
618 | va_end(ap); | |
f2561fda MM |
619 | if (!cmdp) |
620 | return RESP_BAD; | |
621 | ||
95c53908 | 622 | return get_cmd_result(ctx, cmdp); |
f2561fda MM |
623 | } |
624 | ||
9f1ad541 | 625 | static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 626 | const char *fmt, ...) |
f2561fda MM |
627 | { |
628 | va_list ap; | |
629 | struct imap_cmd *cmdp; | |
630 | ||
95c53908 RS |
631 | va_start(ap, fmt); |
632 | cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap); | |
633 | va_end(ap); | |
f2561fda MM |
634 | if (!cmdp) |
635 | return DRV_STORE_BAD; | |
636 | ||
95c53908 | 637 | switch (get_cmd_result(ctx, cmdp)) { |
f2561fda MM |
638 | case RESP_BAD: return DRV_STORE_BAD; |
639 | case RESP_NO: return DRV_MSG_BAD; | |
640 | default: return DRV_OK; | |
641 | } | |
642 | } | |
643 | ||
9f1ad541 | 644 | static int is_atom(struct imap_list *list) |
f2561fda MM |
645 | { |
646 | return list && list->val && list->val != NIL && list->val != LIST; | |
647 | } | |
648 | ||
9f1ad541 | 649 | static int is_list(struct imap_list *list) |
f2561fda MM |
650 | { |
651 | return list && list->val == LIST; | |
652 | } | |
653 | ||
9f1ad541 | 654 | static void free_list(struct imap_list *list) |
f2561fda | 655 | { |
9f1ad541 | 656 | struct imap_list *tmp; |
f2561fda MM |
657 | |
658 | for (; list; list = tmp) { | |
659 | tmp = list->next; | |
95c53908 RS |
660 | if (is_list(list)) |
661 | free_list(list->child); | |
662 | else if (is_atom(list)) | |
663 | free(list->val); | |
664 | free(list); | |
f2561fda MM |
665 | } |
666 | } | |
667 | ||
9f1ad541 | 668 | static int parse_imap_list_l(struct imap *imap, char **sp, struct imap_list **curp, int level) |
f2561fda | 669 | { |
9f1ad541 | 670 | struct imap_list *cur; |
f2561fda MM |
671 | char *s = *sp, *p; |
672 | int n, bytes; | |
673 | ||
674 | for (;;) { | |
95c53908 | 675 | while (isspace((unsigned char)*s)) |
f2561fda MM |
676 | s++; |
677 | if (level && *s == ')') { | |
678 | s++; | |
679 | break; | |
680 | } | |
95c53908 | 681 | *curp = cur = xmalloc(sizeof(*cur)); |
f2561fda | 682 | curp = &cur->next; |
5142db69 | 683 | cur->val = NULL; /* for clean bail */ |
f2561fda MM |
684 | if (*s == '(') { |
685 | /* sublist */ | |
686 | s++; | |
687 | cur->val = LIST; | |
95c53908 | 688 | if (parse_imap_list_l(imap, &s, &cur->child, level + 1)) |
f2561fda MM |
689 | goto bail; |
690 | } else if (imap && *s == '{') { | |
691 | /* literal */ | |
95c53908 | 692 | bytes = cur->len = strtol(s + 1, &s, 10); |
f2561fda MM |
693 | if (*s != '}') |
694 | goto bail; | |
695 | ||
95c53908 | 696 | s = cur->val = xmalloc(cur->len); |
f2561fda MM |
697 | |
698 | /* dump whats left over in the input buffer */ | |
699 | n = imap->buf.bytes - imap->buf.offset; | |
700 | ||
701 | if (n > bytes) | |
702 | /* the entire message fit in the buffer */ | |
703 | n = bytes; | |
704 | ||
95c53908 | 705 | memcpy(s, imap->buf.buf + imap->buf.offset, n); |
f2561fda MM |
706 | s += n; |
707 | bytes -= n; | |
708 | ||
709 | /* mark that we used part of the buffer */ | |
710 | imap->buf.offset += n; | |
711 | ||
712 | /* now read the rest of the message */ | |
713 | while (bytes > 0) { | |
95c53908 | 714 | if ((n = socket_read(&imap->buf.sock, s, bytes)) <= 0) |
f2561fda MM |
715 | goto bail; |
716 | s += n; | |
717 | bytes -= n; | |
718 | } | |
719 | ||
95c53908 | 720 | if (buffer_gets(&imap->buf, &s)) |
f2561fda MM |
721 | goto bail; |
722 | } else if (*s == '"') { | |
723 | /* quoted string */ | |
724 | s++; | |
725 | p = s; | |
726 | for (; *s != '"'; s++) | |
727 | if (!*s) | |
728 | goto bail; | |
729 | cur->len = s - p; | |
730 | s++; | |
182af834 | 731 | cur->val = xmemdupz(p, cur->len); |
f2561fda MM |
732 | } else { |
733 | /* atom */ | |
734 | p = s; | |
95c53908 | 735 | for (; *s && !isspace((unsigned char)*s); s++) |
f2561fda MM |
736 | if (level && *s == ')') |
737 | break; | |
738 | cur->len = s - p; | |
9f1ad541 | 739 | if (cur->len == 3 && !memcmp("NIL", p, 3)) |
f2561fda | 740 | cur->val = NIL; |
9f1ad541 | 741 | else |
182af834 | 742 | cur->val = xmemdupz(p, cur->len); |
f2561fda MM |
743 | } |
744 | ||
745 | if (!level) | |
746 | break; | |
747 | if (!*s) | |
748 | goto bail; | |
749 | } | |
750 | *sp = s; | |
5142db69 | 751 | *curp = NULL; |
f2561fda MM |
752 | return 0; |
753 | ||
9f1ad541 | 754 | bail: |
5142db69 | 755 | *curp = NULL; |
f2561fda MM |
756 | return -1; |
757 | } | |
758 | ||
9f1ad541 | 759 | static struct imap_list *parse_imap_list(struct imap *imap, char **sp) |
f2561fda | 760 | { |
9f1ad541 | 761 | struct imap_list *head; |
f2561fda | 762 | |
95c53908 | 763 | if (!parse_imap_list_l(imap, sp, &head, 0)) |
f2561fda | 764 | return head; |
95c53908 | 765 | free_list(head); |
f2561fda MM |
766 | return NULL; |
767 | } | |
768 | ||
9f1ad541 | 769 | static struct imap_list *parse_list(char **sp) |
f2561fda | 770 | { |
95c53908 | 771 | return parse_imap_list(NULL, sp); |
f2561fda MM |
772 | } |
773 | ||
9f1ad541 | 774 | static void parse_capability(struct imap *imap, char *cmd) |
f2561fda MM |
775 | { |
776 | char *arg; | |
777 | unsigned i; | |
778 | ||
779 | imap->caps = 0x80000000; | |
95c53908 | 780 | while ((arg = next_arg(&cmd))) |
f2561fda | 781 | for (i = 0; i < ARRAY_SIZE(cap_list); i++) |
95c53908 | 782 | if (!strcmp(cap_list[i], arg)) |
f2561fda MM |
783 | imap->caps |= 1 << i; |
784 | imap->rcaps = imap->caps; | |
785 | } | |
786 | ||
9f1ad541 | 787 | static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 788 | char *s) |
f2561fda | 789 | { |
9f1ad541 | 790 | struct imap *imap = ctx->imap; |
f2561fda MM |
791 | char *arg, *p; |
792 | ||
793 | if (*s != '[') | |
794 | return RESP_OK; /* no response code */ | |
795 | s++; | |
95c53908 RS |
796 | if (!(p = strchr(s, ']'))) { |
797 | fprintf(stderr, "IMAP error: malformed response code\n"); | |
f2561fda MM |
798 | return RESP_BAD; |
799 | } | |
800 | *p++ = 0; | |
95c53908 RS |
801 | arg = next_arg(&s); |
802 | if (!strcmp("UIDVALIDITY", arg)) { | |
803 | if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg))) { | |
804 | fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n"); | |
f2561fda MM |
805 | return RESP_BAD; |
806 | } | |
95c53908 RS |
807 | } else if (!strcmp("UIDNEXT", arg)) { |
808 | if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) { | |
809 | fprintf(stderr, "IMAP error: malformed NEXTUID status\n"); | |
f2561fda MM |
810 | return RESP_BAD; |
811 | } | |
95c53908 RS |
812 | } else if (!strcmp("CAPABILITY", arg)) { |
813 | parse_capability(imap, s); | |
814 | } else if (!strcmp("ALERT", arg)) { | |
f2561fda MM |
815 | /* RFC2060 says that these messages MUST be displayed |
816 | * to the user | |
817 | */ | |
95c53908 RS |
818 | for (; isspace((unsigned char)*p); p++); |
819 | fprintf(stderr, "*** IMAP ALERT *** %s\n", p); | |
820 | } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) { | |
821 | if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg)) || | |
9f1ad541 | 822 | !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) { |
95c53908 | 823 | fprintf(stderr, "IMAP error: malformed APPENDUID status\n"); |
f2561fda MM |
824 | return RESP_BAD; |
825 | } | |
826 | } | |
827 | return RESP_OK; | |
828 | } | |
829 | ||
9f1ad541 | 830 | static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd) |
f2561fda | 831 | { |
9f1ad541 | 832 | struct imap *imap = ctx->imap; |
f2561fda MM |
833 | struct imap_cmd *cmdp, **pcmdp, *ncmdp; |
834 | char *cmd, *arg, *arg1, *p; | |
835 | int n, resp, resp2, tag; | |
836 | ||
837 | for (;;) { | |
95c53908 | 838 | if (buffer_gets(&imap->buf, &cmd)) |
f2561fda MM |
839 | return RESP_BAD; |
840 | ||
95c53908 | 841 | arg = next_arg(&cmd); |
f2561fda | 842 | if (*arg == '*') { |
95c53908 | 843 | arg = next_arg(&cmd); |
f2561fda | 844 | if (!arg) { |
95c53908 | 845 | fprintf(stderr, "IMAP error: unable to parse untagged response\n"); |
f2561fda MM |
846 | return RESP_BAD; |
847 | } | |
848 | ||
95c53908 RS |
849 | if (!strcmp("NAMESPACE", arg)) { |
850 | imap->ns_personal = parse_list(&cmd); | |
851 | imap->ns_other = parse_list(&cmd); | |
852 | imap->ns_shared = parse_list(&cmd); | |
853 | } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) || | |
854 | !strcmp("NO", arg) || !strcmp("BYE", arg)) { | |
855 | if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK) | |
f2561fda | 856 | return resp; |
95c53908 RS |
857 | } else if (!strcmp("CAPABILITY", arg)) |
858 | parse_capability(imap, cmd); | |
859 | else if ((arg1 = next_arg(&cmd))) { | |
860 | if (!strcmp("EXISTS", arg1)) | |
861 | ctx->gen.count = atoi(arg); | |
862 | else if (!strcmp("RECENT", arg1)) | |
863 | ctx->gen.recent = atoi(arg); | |
f2561fda | 864 | } else { |
95c53908 | 865 | fprintf(stderr, "IMAP error: unable to parse untagged response\n"); |
f2561fda MM |
866 | return RESP_BAD; |
867 | } | |
868 | } else if (!imap->in_progress) { | |
95c53908 | 869 | fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : ""); |
f2561fda MM |
870 | return RESP_BAD; |
871 | } else if (*arg == '+') { | |
872 | /* This can happen only with the last command underway, as | |
873 | it enforces a round-trip. */ | |
874 | cmdp = (struct imap_cmd *)((char *)imap->in_progress_append - | |
875 | offsetof(struct imap_cmd, next)); | |
876 | if (cmdp->cb.data) { | |
95c53908 RS |
877 | n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen); |
878 | free(cmdp->cb.data); | |
5142db69 | 879 | cmdp->cb.data = NULL; |
f2561fda MM |
880 | if (n != (int)cmdp->cb.dlen) |
881 | return RESP_BAD; | |
882 | } else if (cmdp->cb.cont) { | |
95c53908 | 883 | if (cmdp->cb.cont(ctx, cmdp, cmd)) |
f2561fda MM |
884 | return RESP_BAD; |
885 | } else { | |
95c53908 | 886 | fprintf(stderr, "IMAP error: unexpected command continuation request\n"); |
f2561fda MM |
887 | return RESP_BAD; |
888 | } | |
95c53908 | 889 | if (socket_write(&imap->buf.sock, "\r\n", 2) != 2) |
f2561fda MM |
890 | return RESP_BAD; |
891 | if (!cmdp->cb.cont) | |
892 | imap->literal_pending = 0; | |
893 | if (!tcmd) | |
894 | return DRV_OK; | |
895 | } else { | |
95c53908 | 896 | tag = atoi(arg); |
f2561fda MM |
897 | for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next) |
898 | if (cmdp->tag == tag) | |
899 | goto gottag; | |
95c53908 | 900 | fprintf(stderr, "IMAP error: unexpected tag %s\n", arg); |
f2561fda | 901 | return RESP_BAD; |
9f1ad541 | 902 | gottag: |
f2561fda MM |
903 | if (!(*pcmdp = cmdp->next)) |
904 | imap->in_progress_append = pcmdp; | |
905 | imap->num_in_progress--; | |
906 | if (cmdp->cb.cont || cmdp->cb.data) | |
907 | imap->literal_pending = 0; | |
95c53908 RS |
908 | arg = next_arg(&cmd); |
909 | if (!strcmp("OK", arg)) | |
f2561fda MM |
910 | resp = DRV_OK; |
911 | else { | |
95c53908 RS |
912 | if (!strcmp("NO", arg)) { |
913 | if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */ | |
914 | p = strchr(cmdp->cmd, '"'); | |
915 | if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", strchr(p + 1, '"') - p + 1, p)) { | |
f2561fda MM |
916 | resp = RESP_BAD; |
917 | goto normal; | |
918 | } | |
919 | /* not waiting here violates the spec, but a server that does not | |
920 | grok this nonetheless violates it too. */ | |
921 | cmdp->cb.create = 0; | |
95c53908 | 922 | if (!(ncmdp = issue_imap_cmd(ctx, &cmdp->cb, "%s", cmdp->cmd))) { |
f2561fda MM |
923 | resp = RESP_BAD; |
924 | goto normal; | |
925 | } | |
95c53908 RS |
926 | free(cmdp->cmd); |
927 | free(cmdp); | |
f2561fda MM |
928 | if (!tcmd) |
929 | return 0; /* ignored */ | |
930 | if (cmdp == tcmd) | |
931 | tcmd = ncmdp; | |
932 | continue; | |
933 | } | |
934 | resp = RESP_NO; | |
95c53908 | 935 | } else /*if (!strcmp("BAD", arg))*/ |
f2561fda | 936 | resp = RESP_BAD; |
95c53908 RS |
937 | fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n", |
938 | memcmp(cmdp->cmd, "LOGIN", 5) ? | |
f2561fda MM |
939 | cmdp->cmd : "LOGIN <user> <pass>", |
940 | arg, cmd ? cmd : ""); | |
941 | } | |
95c53908 | 942 | if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp) |
f2561fda | 943 | resp = resp2; |
9f1ad541 | 944 | normal: |
f2561fda | 945 | if (cmdp->cb.done) |
95c53908 RS |
946 | cmdp->cb.done(ctx, cmdp, resp); |
947 | free(cmdp->cb.data); | |
948 | free(cmdp->cmd); | |
949 | free(cmdp); | |
f2561fda MM |
950 | if (!tcmd || tcmd == cmdp) |
951 | return resp; | |
952 | } | |
953 | } | |
954 | /* not reached */ | |
955 | } | |
956 | ||
9f1ad541 | 957 | static void imap_close_server(struct imap_store *ictx) |
f2561fda | 958 | { |
9f1ad541 | 959 | struct imap *imap = ictx->imap; |
f2561fda MM |
960 | |
961 | if (imap->buf.sock.fd != -1) { | |
95c53908 RS |
962 | imap_exec(ictx, NULL, "LOGOUT"); |
963 | socket_shutdown(&imap->buf.sock); | |
f2561fda | 964 | } |
95c53908 RS |
965 | free_list(imap->ns_personal); |
966 | free_list(imap->ns_other); | |
967 | free_list(imap->ns_shared); | |
968 | free(imap); | |
f2561fda MM |
969 | } |
970 | ||
9f1ad541 | 971 | static void imap_close_store(struct store *ctx) |
f2561fda | 972 | { |
9f1ad541 | 973 | imap_close_server((struct imap_store *)ctx); |
95c53908 RS |
974 | free_generic_messages(ctx->msgs); |
975 | free(ctx); | |
f2561fda MM |
976 | } |
977 | ||
9f1ad541 | 978 | static struct store *imap_open_store(struct imap_server_conf *srvc) |
f2561fda | 979 | { |
9f1ad541 JH |
980 | struct imap_store *ctx; |
981 | struct imap *imap; | |
f2561fda MM |
982 | char *arg, *rsp; |
983 | struct hostent *he; | |
984 | struct sockaddr_in addr; | |
985 | int s, a[2], preauth; | |
c9bc159d | 986 | pid_t pid; |
f2561fda | 987 | |
95c53908 | 988 | ctx = xcalloc(sizeof(*ctx), 1); |
f2561fda | 989 | |
95c53908 | 990 | ctx->imap = imap = xcalloc(sizeof(*imap), 1); |
f2561fda MM |
991 | imap->buf.sock.fd = -1; |
992 | imap->in_progress_append = &imap->in_progress; | |
993 | ||
994 | /* open connection to IMAP server */ | |
995 | ||
996 | if (srvc->tunnel) { | |
95c53908 | 997 | imap_info("Starting tunnel '%s'... ", srvc->tunnel); |
f2561fda | 998 | |
95c53908 RS |
999 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, a)) { |
1000 | perror("socketpair"); | |
1001 | exit(1); | |
f2561fda MM |
1002 | } |
1003 | ||
c9bc159d PD |
1004 | pid = fork(); |
1005 | if (pid < 0) | |
95c53908 | 1006 | _exit(127); |
c9bc159d | 1007 | if (!pid) { |
95c53908 RS |
1008 | if (dup2(a[0], 0) == -1 || dup2(a[0], 1) == -1) |
1009 | _exit(127); | |
1010 | close(a[0]); | |
1011 | close(a[1]); | |
1012 | execl("/bin/sh", "sh", "-c", srvc->tunnel, NULL); | |
1013 | _exit(127); | |
f2561fda MM |
1014 | } |
1015 | ||
95c53908 | 1016 | close(a[0]); |
f2561fda MM |
1017 | |
1018 | imap->buf.sock.fd = a[1]; | |
1019 | ||
95c53908 | 1020 | imap_info("ok\n"); |
f2561fda | 1021 | } else { |
95c53908 RS |
1022 | memset(&addr, 0, sizeof(addr)); |
1023 | addr.sin_port = htons(srvc->port); | |
f2561fda MM |
1024 | addr.sin_family = AF_INET; |
1025 | ||
95c53908 RS |
1026 | imap_info("Resolving %s... ", srvc->host); |
1027 | he = gethostbyname(srvc->host); | |
f2561fda | 1028 | if (!he) { |
95c53908 | 1029 | perror("gethostbyname"); |
f2561fda MM |
1030 | goto bail; |
1031 | } | |
95c53908 | 1032 | imap_info("ok\n"); |
f2561fda MM |
1033 | |
1034 | addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]); | |
1035 | ||
95c53908 | 1036 | s = socket(PF_INET, SOCK_STREAM, 0); |
f2561fda | 1037 | |
95c53908 RS |
1038 | imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); |
1039 | if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) { | |
1040 | close(s); | |
1041 | perror("connect"); | |
f2561fda MM |
1042 | goto bail; |
1043 | } | |
f2561fda MM |
1044 | |
1045 | imap->buf.sock.fd = s; | |
1046 | ||
684ec6c6 RS |
1047 | if (srvc->use_ssl && |
1048 | ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) { | |
1049 | close(s); | |
1050 | goto bail; | |
1051 | } | |
95c53908 | 1052 | imap_info("ok\n"); |
f2561fda MM |
1053 | } |
1054 | ||
1055 | /* read the greeting string */ | |
95c53908 RS |
1056 | if (buffer_gets(&imap->buf, &rsp)) { |
1057 | fprintf(stderr, "IMAP error: no greeting response\n"); | |
f2561fda MM |
1058 | goto bail; |
1059 | } | |
95c53908 RS |
1060 | arg = next_arg(&rsp); |
1061 | if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) { | |
1062 | fprintf(stderr, "IMAP error: invalid greeting response\n"); | |
f2561fda MM |
1063 | goto bail; |
1064 | } | |
1065 | preauth = 0; | |
95c53908 | 1066 | if (!strcmp("PREAUTH", arg)) |
f2561fda | 1067 | preauth = 1; |
95c53908 RS |
1068 | else if (strcmp("OK", arg) != 0) { |
1069 | fprintf(stderr, "IMAP error: unknown greeting response\n"); | |
f2561fda MM |
1070 | goto bail; |
1071 | } | |
95c53908 RS |
1072 | parse_response_code(ctx, NULL, rsp); |
1073 | if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK) | |
f2561fda MM |
1074 | goto bail; |
1075 | ||
1076 | if (!preauth) { | |
684ec6c6 RS |
1077 | #ifndef NO_OPENSSL |
1078 | if (!srvc->use_ssl && CAP(STARTTLS)) { | |
1079 | if (imap_exec(ctx, 0, "STARTTLS") != RESP_OK) | |
1080 | goto bail; | |
1081 | if (ssl_socket_connect(&imap->buf.sock, 1, | |
1082 | srvc->ssl_verify)) | |
1083 | goto bail; | |
1084 | /* capabilities may have changed, so get the new capabilities */ | |
1085 | if (imap_exec(ctx, 0, "CAPABILITY") != RESP_OK) | |
1086 | goto bail; | |
1087 | } | |
1088 | #endif | |
95c53908 | 1089 | imap_info("Logging in...\n"); |
f2561fda | 1090 | if (!srvc->user) { |
95c53908 | 1091 | fprintf(stderr, "Skipping server %s, no user\n", srvc->host); |
f2561fda MM |
1092 | goto bail; |
1093 | } | |
1094 | if (!srvc->pass) { | |
1095 | char prompt[80]; | |
95c53908 RS |
1096 | sprintf(prompt, "Password (%s@%s): ", srvc->user, srvc->host); |
1097 | arg = getpass(prompt); | |
f2561fda | 1098 | if (!arg) { |
95c53908 RS |
1099 | perror("getpass"); |
1100 | exit(1); | |
f2561fda MM |
1101 | } |
1102 | if (!*arg) { | |
95c53908 | 1103 | fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host); |
f2561fda MM |
1104 | goto bail; |
1105 | } | |
1106 | /* | |
1107 | * getpass() returns a pointer to a static buffer. make a copy | |
1108 | * for long term storage. | |
1109 | */ | |
95c53908 | 1110 | srvc->pass = xstrdup(arg); |
f2561fda MM |
1111 | } |
1112 | if (CAP(NOLOGIN)) { | |
95c53908 | 1113 | fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host); |
f2561fda MM |
1114 | goto bail; |
1115 | } | |
684ec6c6 | 1116 | if (!imap->buf.sock.ssl) |
9f1ad541 JH |
1117 | imap_warn("*** IMAP Warning *** Password is being " |
1118 | "sent in the clear\n"); | |
95c53908 RS |
1119 | if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) { |
1120 | fprintf(stderr, "IMAP error: LOGIN failed\n"); | |
f2561fda MM |
1121 | goto bail; |
1122 | } | |
1123 | } /* !preauth */ | |
1124 | ||
1125 | ctx->prefix = ""; | |
1126 | ctx->trashnc = 1; | |
9f1ad541 | 1127 | return (struct store *)ctx; |
f2561fda | 1128 | |
9f1ad541 | 1129 | bail: |
95c53908 | 1130 | imap_close_store(&ctx->gen); |
5142db69 | 1131 | return NULL; |
f2561fda MM |
1132 | } |
1133 | ||
95c53908 | 1134 | static int imap_make_flags(int flags, char *buf) |
f2561fda MM |
1135 | { |
1136 | const char *s; | |
1137 | unsigned i, d; | |
1138 | ||
1139 | for (i = d = 0; i < ARRAY_SIZE(Flags); i++) | |
1140 | if (flags & (1 << i)) { | |
1141 | buf[d++] = ' '; | |
1142 | buf[d++] = '\\'; | |
1143 | for (s = Flags[i]; *s; s++) | |
1144 | buf[d++] = *s; | |
1145 | } | |
1146 | buf[0] = '('; | |
1147 | buf[d++] = ')'; | |
1148 | return d; | |
1149 | } | |
1150 | ||
1151 | #define TUIDL 8 | |
1152 | ||
9f1ad541 | 1153 | static int imap_store_msg(struct store *gctx, struct msg_data *data, int *uid) |
f2561fda | 1154 | { |
9f1ad541 JH |
1155 | struct imap_store *ctx = (struct imap_store *)gctx; |
1156 | struct imap *imap = ctx->imap; | |
f2561fda MM |
1157 | struct imap_cmd_cb cb; |
1158 | char *fmap, *buf; | |
1159 | const char *prefix, *box; | |
1160 | int ret, i, j, d, len, extra, nocr; | |
1161 | int start, sbreak = 0, ebreak = 0; | |
1162 | char flagstr[128], tuid[TUIDL * 2 + 1]; | |
1163 | ||
95c53908 | 1164 | memset(&cb, 0, sizeof(cb)); |
f2561fda MM |
1165 | |
1166 | fmap = data->data; | |
1167 | len = data->len; | |
1168 | nocr = !data->crlf; | |
1169 | extra = 0, i = 0; | |
1170 | if (!CAP(UIDPLUS) && uid) { | |
9f1ad541 | 1171 | nloop: |
f2561fda MM |
1172 | start = i; |
1173 | while (i < len) | |
1174 | if (fmap[i++] == '\n') { | |
1175 | extra += nocr; | |
1176 | if (i - 2 + nocr == start) { | |
1177 | sbreak = ebreak = i - 2 + nocr; | |
1178 | goto mktid; | |
1179 | } | |
95c53908 | 1180 | if (!memcmp(fmap + start, "X-TUID: ", 8)) { |
f2561fda MM |
1181 | extra -= (ebreak = i) - (sbreak = start) + nocr; |
1182 | goto mktid; | |
1183 | } | |
1184 | goto nloop; | |
1185 | } | |
1186 | /* invalid message */ | |
95c53908 | 1187 | free(fmap); |
f2561fda | 1188 | return DRV_MSG_BAD; |
9f1ad541 | 1189 | mktid: |
f2561fda | 1190 | for (j = 0; j < TUIDL; j++) |
95c53908 | 1191 | sprintf(tuid + j * 2, "%02x", arc4_getbyte()); |
f2561fda MM |
1192 | extra += 8 + TUIDL * 2 + 2; |
1193 | } | |
1194 | if (nocr) | |
1195 | for (; i < len; i++) | |
1196 | if (fmap[i] == '\n') | |
1197 | extra++; | |
1198 | ||
1199 | cb.dlen = len + extra; | |
95c53908 | 1200 | buf = cb.data = xmalloc(cb.dlen); |
f2561fda MM |
1201 | i = 0; |
1202 | if (!CAP(UIDPLUS) && uid) { | |
1203 | if (nocr) { | |
1204 | for (; i < sbreak; i++) | |
1205 | if (fmap[i] == '\n') { | |
1206 | *buf++ = '\r'; | |
1207 | *buf++ = '\n'; | |
1208 | } else | |
1209 | *buf++ = fmap[i]; | |
1210 | } else { | |
95c53908 | 1211 | memcpy(buf, fmap, sbreak); |
f2561fda MM |
1212 | buf += sbreak; |
1213 | } | |
95c53908 | 1214 | memcpy(buf, "X-TUID: ", 8); |
f2561fda | 1215 | buf += 8; |
95c53908 | 1216 | memcpy(buf, tuid, TUIDL * 2); |
f2561fda MM |
1217 | buf += TUIDL * 2; |
1218 | *buf++ = '\r'; | |
1219 | *buf++ = '\n'; | |
1220 | i = ebreak; | |
1221 | } | |
1222 | if (nocr) { | |
1223 | for (; i < len; i++) | |
1224 | if (fmap[i] == '\n') { | |
1225 | *buf++ = '\r'; | |
1226 | *buf++ = '\n'; | |
1227 | } else | |
1228 | *buf++ = fmap[i]; | |
1229 | } else | |
95c53908 | 1230 | memcpy(buf, fmap + i, len - i); |
f2561fda | 1231 | |
95c53908 | 1232 | free(fmap); |
f2561fda MM |
1233 | |
1234 | d = 0; | |
1235 | if (data->flags) { | |
95c53908 | 1236 | d = imap_make_flags(data->flags, flagstr); |
f2561fda MM |
1237 | flagstr[d++] = ' '; |
1238 | } | |
1239 | flagstr[d] = 0; | |
1240 | ||
1241 | if (!uid) { | |
1242 | box = gctx->conf->trash; | |
1243 | prefix = ctx->prefix; | |
1244 | cb.create = 1; | |
1245 | if (ctx->trashnc) | |
1246 | imap->caps = imap->rcaps & ~(1 << LITERALPLUS); | |
1247 | } else { | |
1248 | box = gctx->name; | |
95c53908 | 1249 | prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix; |
f2561fda MM |
1250 | cb.create = 0; |
1251 | } | |
1252 | cb.ctx = uid; | |
95c53908 | 1253 | ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr); |
f2561fda MM |
1254 | imap->caps = imap->rcaps; |
1255 | if (ret != DRV_OK) | |
1256 | return ret; | |
1257 | if (!uid) | |
1258 | ctx->trashnc = 0; | |
1259 | else | |
1260 | gctx->count++; | |
1261 | ||
1262 | return DRV_OK; | |
1263 | } | |
1264 | ||
1265 | #define CHUNKSIZE 0x1000 | |
1266 | ||
9f1ad541 | 1267 | static int read_message(FILE *f, struct msg_data *msg) |
f2561fda | 1268 | { |
f285a2d7 | 1269 | struct strbuf buf = STRBUF_INIT; |
f2561fda | 1270 | |
635d043f | 1271 | memset(msg, 0, sizeof(*msg)); |
635d043f PH |
1272 | |
1273 | do { | |
1274 | if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0) | |
f2561fda | 1275 | break; |
635d043f PH |
1276 | } while (!feof(f)); |
1277 | ||
1278 | msg->len = buf.len; | |
b315c5c0 | 1279 | msg->data = strbuf_detach(&buf, NULL); |
f2561fda MM |
1280 | return msg->len; |
1281 | } | |
1282 | ||
9f1ad541 | 1283 | static int count_messages(struct msg_data *msg) |
f2561fda MM |
1284 | { |
1285 | int count = 0; | |
1286 | char *p = msg->data; | |
1287 | ||
1288 | while (1) { | |
1968d77d | 1289 | if (!prefixcmp(p, "From ")) { |
f2561fda MM |
1290 | count++; |
1291 | p += 5; | |
1292 | } | |
95c53908 | 1293 | p = strstr(p+5, "\nFrom "); |
f2561fda MM |
1294 | if (!p) |
1295 | break; | |
1296 | p++; | |
1297 | } | |
1298 | return count; | |
1299 | } | |
1300 | ||
9f1ad541 | 1301 | static int split_msg(struct msg_data *all_msgs, struct msg_data *msg, int *ofs) |
f2561fda MM |
1302 | { |
1303 | char *p, *data; | |
1304 | ||
95c53908 | 1305 | memset(msg, 0, sizeof *msg); |
f2561fda MM |
1306 | if (*ofs >= all_msgs->len) |
1307 | return 0; | |
1308 | ||
95c53908 | 1309 | data = &all_msgs->data[*ofs]; |
f2561fda MM |
1310 | msg->len = all_msgs->len - *ofs; |
1311 | ||
1968d77d | 1312 | if (msg->len < 5 || prefixcmp(data, "From ")) |
f2561fda MM |
1313 | return 0; |
1314 | ||
95c53908 | 1315 | p = strchr(data, '\n'); |
e0b08307 MA |
1316 | if (p) { |
1317 | p = &p[1]; | |
1318 | msg->len -= p-data; | |
1319 | *ofs += p-data; | |
1320 | data = p; | |
1321 | } | |
1322 | ||
95c53908 | 1323 | p = strstr(data, "\nFrom "); |
f2561fda MM |
1324 | if (p) |
1325 | msg->len = &p[1] - data; | |
1326 | ||
182af834 | 1327 | msg->data = xmemdupz(data, msg->len); |
f2561fda | 1328 | *ofs += msg->len; |
a6080a0a | 1329 | return 1; |
f2561fda MM |
1330 | } |
1331 | ||
9f1ad541 | 1332 | static struct imap_server_conf server = { |
f2561fda MM |
1333 | NULL, /* name */ |
1334 | NULL, /* tunnel */ | |
1335 | NULL, /* host */ | |
1336 | 0, /* port */ | |
1337 | NULL, /* user */ | |
1338 | NULL, /* pass */ | |
684ec6c6 RS |
1339 | 0, /* use_ssl */ |
1340 | 1, /* ssl_verify */ | |
f2561fda MM |
1341 | }; |
1342 | ||
1343 | static char *imap_folder; | |
1344 | ||
95c53908 | 1345 | static int git_imap_config(const char *key, const char *val, void *cb) |
f2561fda MM |
1346 | { |
1347 | char imap_key[] = "imap."; | |
1348 | ||
95c53908 | 1349 | if (strncmp(key, imap_key, sizeof imap_key - 1)) |
f2561fda | 1350 | return 0; |
3c17c34a JH |
1351 | |
1352 | if (!val) | |
1353 | return config_error_nonbool(key); | |
1354 | ||
f2561fda MM |
1355 | key += sizeof imap_key - 1; |
1356 | ||
95c53908 RS |
1357 | if (!strcmp("folder", key)) { |
1358 | imap_folder = xstrdup(val); | |
1359 | } else if (!strcmp("host", key)) { | |
684ec6c6 RS |
1360 | if (!prefixcmp(val, "imap:")) |
1361 | val += 5; | |
1362 | else if (!prefixcmp(val, "imaps:")) { | |
1363 | val += 6; | |
1364 | server.use_ssl = 1; | |
f2561fda | 1365 | } |
1968d77d | 1366 | if (!prefixcmp(val, "//")) |
f2561fda | 1367 | val += 2; |
95c53908 | 1368 | server.host = xstrdup(val); |
9f1ad541 | 1369 | } else if (!strcmp("user", key)) |
95c53908 RS |
1370 | server.user = xstrdup(val); |
1371 | else if (!strcmp("pass", key)) | |
1372 | server.pass = xstrdup(val); | |
1373 | else if (!strcmp("port", key)) | |
1374 | server.port = git_config_int(key, val); | |
1375 | else if (!strcmp("tunnel", key)) | |
1376 | server.tunnel = xstrdup(val); | |
1377 | else if (!strcmp("sslverify", key)) | |
1378 | server.ssl_verify = git_config_bool(key, val); | |
f2561fda MM |
1379 | return 0; |
1380 | } | |
1381 | ||
95c53908 | 1382 | int main(int argc, char **argv) |
f2561fda | 1383 | { |
9f1ad541 JH |
1384 | struct msg_data all_msgs, msg; |
1385 | struct store *ctx = NULL; | |
f2561fda MM |
1386 | int uid = 0; |
1387 | int ofs = 0; | |
1388 | int r; | |
1389 | int total, n = 0; | |
a0406b94 | 1390 | int nongit_ok; |
f2561fda MM |
1391 | |
1392 | /* init the random number generator */ | |
1393 | arc4_init(); | |
1394 | ||
a0406b94 | 1395 | setup_git_directory_gently(&nongit_ok); |
ef90d6d4 | 1396 | git_config(git_imap_config, NULL); |
f2561fda | 1397 | |
684ec6c6 RS |
1398 | if (!server.port) |
1399 | server.port = server.use_ssl ? 993 : 143; | |
1400 | ||
f2561fda | 1401 | if (!imap_folder) { |
95c53908 | 1402 | fprintf(stderr, "no imap store specified\n"); |
f2561fda MM |
1403 | return 1; |
1404 | } | |
5b67b8e2 | 1405 | if (!server.host) { |
34b5cd1f | 1406 | if (!server.tunnel) { |
95c53908 | 1407 | fprintf(stderr, "no imap host specified\n"); |
34b5cd1f JK |
1408 | return 1; |
1409 | } | |
1410 | server.host = "tunnel"; | |
5b67b8e2 | 1411 | } |
f2561fda MM |
1412 | |
1413 | /* read the messages */ | |
95c53908 | 1414 | if (!read_message(stdin, &all_msgs)) { |
9f1ad541 | 1415 | fprintf(stderr, "nothing to send\n"); |
f2561fda MM |
1416 | return 1; |
1417 | } | |
1418 | ||
95c53908 | 1419 | total = count_messages(&all_msgs); |
1cd88cc9 | 1420 | if (!total) { |
9f1ad541 | 1421 | fprintf(stderr, "no messages to send\n"); |
1cd88cc9 MM |
1422 | return 1; |
1423 | } | |
1424 | ||
f2561fda | 1425 | /* write it to the imap server */ |
95c53908 | 1426 | ctx = imap_open_store(&server); |
f2561fda | 1427 | if (!ctx) { |
9f1ad541 | 1428 | fprintf(stderr, "failed to open store\n"); |
f2561fda MM |
1429 | return 1; |
1430 | } | |
1431 | ||
9f1ad541 | 1432 | fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : ""); |
f2561fda MM |
1433 | ctx->name = imap_folder; |
1434 | while (1) { | |
1435 | unsigned percent = n * 100 / total; | |
95c53908 RS |
1436 | fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total); |
1437 | if (!split_msg(&all_msgs, &msg, &ofs)) | |
f2561fda | 1438 | break; |
95c53908 | 1439 | r = imap_store_msg(ctx, &msg, &uid); |
9f1ad541 JH |
1440 | if (r != DRV_OK) |
1441 | break; | |
f2561fda MM |
1442 | n++; |
1443 | } | |
95c53908 | 1444 | fprintf(stderr, "\n"); |
f2561fda | 1445 | |
95c53908 | 1446 | imap_close_store(ctx); |
f2561fda MM |
1447 | |
1448 | return 0; | |
1449 | } |