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