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