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