]>
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 | |
d05b08cd | 21 | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
f2561fda MM |
22 | */ |
23 | ||
15db4e7f | 24 | #include "git-compat-util.h" |
b2141fc1 | 25 | #include "config.h" |
791643a8 | 26 | #include "credential.h" |
f394e093 | 27 | #include "gettext.h" |
c94d2dd0 | 28 | #include "run-command.h" |
f1a35295 | 29 | #include "parse-options.h" |
e38da487 | 30 | #include "setup.h" |
30bced3a | 31 | #include "strbuf.h" |
5b52d9f1 | 32 | #if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG) |
684ec6c6 RS |
33 | typedef void *SSL; |
34 | #endif | |
1e16b255 BR |
35 | #ifdef USE_CURL_FOR_IMAP_SEND |
36 | #include "http.h" | |
37 | #endif | |
f2561fda | 38 | |
dbba42bb NMC |
39 | #if defined(USE_CURL_FOR_IMAP_SEND) |
40 | /* Always default to curl if it's available. */ | |
dcd01ea1 KM |
41 | #define USE_CURL_DEFAULT 1 |
42 | #else | |
dbba42bb | 43 | /* We don't have curl, so continue to use the historical implementation */ |
dcd01ea1 KM |
44 | #define USE_CURL_DEFAULT 0 |
45 | #endif | |
46 | ||
f1a35295 | 47 | static int verbosity; |
dcd01ea1 | 48 | static int use_curl = USE_CURL_DEFAULT; |
f1a35295 | 49 | |
1e16b255 | 50 | static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] [--[no-]curl] < <mbox>", NULL }; |
f1a35295 BR |
51 | |
52 | static struct option imap_send_options[] = { | |
53 | OPT__VERBOSITY(&verbosity), | |
1e16b255 | 54 | OPT_BOOL(0, "curl", &use_curl, "use libcurl to communicate with the IMAP server"), |
f1a35295 BR |
55 | OPT_END() |
56 | }; | |
f2561fda | 57 | |
d23b1ecf | 58 | #undef DRV_OK |
f2561fda MM |
59 | #define DRV_OK 0 |
60 | #define DRV_MSG_BAD -1 | |
61 | #define DRV_BOX_BAD -2 | |
62 | #define DRV_STORE_BAD -3 | |
63 | ||
28bea9e5 | 64 | __attribute__((format (printf, 1, 2))) |
95c53908 | 65 | static void imap_info(const char *, ...); |
28bea9e5 | 66 | __attribute__((format (printf, 1, 2))) |
95c53908 | 67 | static void imap_warn(const char *, ...); |
f2561fda | 68 | |
95c53908 | 69 | static char *next_arg(char **); |
f2561fda | 70 | |
19247e55 PH |
71 | static int nfvasprintf(char **strp, const char *fmt, va_list ap) |
72 | { | |
73 | int len; | |
74 | char tmp[8192]; | |
75 | ||
76 | len = vsnprintf(tmp, sizeof(tmp), fmt, ap); | |
77 | if (len < 0) | |
d7530708 | 78 | die("Fatal: Out of memory"); |
19247e55 | 79 | if (len >= sizeof(tmp)) |
d7530708 | 80 | die("imap command overflow!"); |
19247e55 PH |
81 | *strp = xmemdupz(tmp, len); |
82 | return len; | |
83 | } | |
f2561fda | 84 | |
9f1ad541 | 85 | struct imap_server_conf { |
50212361 NMC |
86 | const char *name; |
87 | const char *tunnel; | |
88 | const char *host; | |
f2561fda | 89 | int port; |
50212361 NMC |
90 | const char *folder; |
91 | const char *user; | |
92 | const char *pass; | |
684ec6c6 RS |
93 | int use_ssl; |
94 | int ssl_verify; | |
c64d84f1 | 95 | int use_html; |
50212361 | 96 | const char *auth_method; |
ae9c606e HM |
97 | }; |
98 | ||
99 | static struct imap_server_conf server = { | |
518f7059 | 100 | .ssl_verify = 1, |
9f1ad541 | 101 | }; |
f2561fda | 102 | |
9f1ad541 | 103 | struct imap_socket { |
7a7796e9 | 104 | int fd[2]; |
684ec6c6 | 105 | SSL *ssl; |
9f1ad541 | 106 | }; |
f2561fda | 107 | |
9f1ad541 JH |
108 | struct imap_buffer { |
109 | struct imap_socket sock; | |
f2561fda MM |
110 | int bytes; |
111 | int offset; | |
112 | char buf[1024]; | |
9f1ad541 | 113 | }; |
f2561fda MM |
114 | |
115 | struct imap_cmd; | |
116 | ||
9f1ad541 | 117 | struct imap { |
f2561fda | 118 | int uidnext; /* from SELECT responses */ |
f2561fda MM |
119 | unsigned caps, rcaps; /* CAPABILITY results */ |
120 | /* command queue */ | |
121 | int nexttag, num_in_progress, literal_pending; | |
122 | struct imap_cmd *in_progress, **in_progress_append; | |
9f1ad541 JH |
123 | struct imap_buffer buf; /* this is BIG, so put it last */ |
124 | }; | |
f2561fda | 125 | |
9f1ad541 | 126 | struct imap_store { |
636fd66b MH |
127 | /* currently open mailbox */ |
128 | const char *name; /* foreign! maybe preset? */ | |
129 | int uidvalidity; | |
9f1ad541 | 130 | struct imap *imap; |
f2561fda | 131 | const char *prefix; |
9f1ad541 | 132 | }; |
f2561fda MM |
133 | |
134 | struct imap_cmd_cb { | |
ec9e358a | 135 | int (*cont)(struct imap_store *ctx, const char *prompt); |
f2561fda MM |
136 | void *ctx; |
137 | char *data; | |
138 | int dlen; | |
f2561fda MM |
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 | } | |
2c3c3d88 JK |
205 | /* mark as used to appease -Wunused-parameter with NO_OPENSSL */ |
206 | (void)sock; | |
684ec6c6 RS |
207 | } |
208 | ||
1e1fe529 | 209 | #ifdef NO_OPENSSL |
2c3c3d88 JK |
210 | static int ssl_socket_connect(struct imap_socket *sock UNUSED, |
211 | int use_tls_only UNUSED, | |
212 | int verify UNUSED) | |
684ec6c6 | 213 | { |
684ec6c6 RS |
214 | fprintf(stderr, "SSL requested but SSL support not compiled in\n"); |
215 | return -1; | |
1e1fe529 JH |
216 | } |
217 | ||
1e380ddc | 218 | #else |
1e1fe529 | 219 | |
b62fb077 OB |
220 | static int host_matches(const char *host, const char *pattern) |
221 | { | |
222 | if (pattern[0] == '*' && pattern[1] == '.') { | |
223 | pattern += 2; | |
224 | if (!(host = strchr(host, '.'))) | |
225 | return 0; | |
226 | host++; | |
227 | } | |
228 | ||
229 | return *host && *pattern && !strcasecmp(host, pattern); | |
230 | } | |
231 | ||
232 | static int verify_hostname(X509 *cert, const char *hostname) | |
233 | { | |
234 | int len; | |
235 | X509_NAME *subj; | |
236 | char cname[1000]; | |
e174744a OB |
237 | int i, found; |
238 | STACK_OF(GENERAL_NAME) *subj_alt_names; | |
239 | ||
240 | /* try the DNS subjectAltNames */ | |
241 | found = 0; | |
242 | if ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) { | |
243 | int num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names); | |
244 | for (i = 0; !found && i < num_subj_alt_names; i++) { | |
245 | GENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i); | |
246 | if (subj_alt_name->type == GEN_DNS && | |
247 | strlen((const char *)subj_alt_name->d.ia5->data) == (size_t)subj_alt_name->d.ia5->length && | |
248 | host_matches(hostname, (const char *)(subj_alt_name->d.ia5->data))) | |
249 | found = 1; | |
250 | } | |
251 | sk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free); | |
252 | } | |
253 | if (found) | |
254 | return 0; | |
b62fb077 OB |
255 | |
256 | /* try the common name */ | |
257 | if (!(subj = X509_get_subject_name(cert))) | |
258 | return error("cannot get certificate subject"); | |
259 | if ((len = X509_NAME_get_text_by_NID(subj, NID_commonName, cname, sizeof(cname))) < 0) | |
260 | return error("cannot get certificate common name"); | |
261 | if (strlen(cname) == (size_t)len && host_matches(hostname, cname)) | |
262 | return 0; | |
263 | return error("certificate owner '%s' does not match hostname '%s'", | |
264 | cname, hostname); | |
265 | } | |
266 | ||
1e1fe529 JH |
267 | static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify) |
268 | { | |
1e380ddc VL |
269 | #if (OPENSSL_VERSION_NUMBER >= 0x10000000L) |
270 | const SSL_METHOD *meth; | |
684ec6c6 RS |
271 | #else |
272 | SSL_METHOD *meth; | |
1e380ddc | 273 | #endif |
684ec6c6 RS |
274 | SSL_CTX *ctx; |
275 | int ret; | |
b62fb077 | 276 | X509 *cert; |
684ec6c6 RS |
277 | |
278 | SSL_library_init(); | |
279 | SSL_load_error_strings(); | |
280 | ||
b51c0d4b | 281 | meth = SSLv23_method(); |
684ec6c6 RS |
282 | if (!meth) { |
283 | ssl_socket_perror("SSLv23_method"); | |
284 | return -1; | |
285 | } | |
286 | ||
287 | ctx = SSL_CTX_new(meth); | |
6738a33b KY |
288 | if (!ctx) { |
289 | ssl_socket_perror("SSL_CTX_new"); | |
290 | return -1; | |
291 | } | |
684ec6c6 | 292 | |
b51c0d4b KY |
293 | if (use_tls_only) |
294 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); | |
684ec6c6 RS |
295 | |
296 | if (verify) | |
297 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); | |
298 | ||
299 | if (!SSL_CTX_set_default_verify_paths(ctx)) { | |
300 | ssl_socket_perror("SSL_CTX_set_default_verify_paths"); | |
301 | return -1; | |
302 | } | |
303 | sock->ssl = SSL_new(ctx); | |
304 | if (!sock->ssl) { | |
305 | ssl_socket_perror("SSL_new"); | |
306 | return -1; | |
307 | } | |
7a7796e9 EFL |
308 | if (!SSL_set_rfd(sock->ssl, sock->fd[0])) { |
309 | ssl_socket_perror("SSL_set_rfd"); | |
310 | return -1; | |
311 | } | |
312 | if (!SSL_set_wfd(sock->ssl, sock->fd[1])) { | |
313 | ssl_socket_perror("SSL_set_wfd"); | |
684ec6c6 RS |
314 | return -1; |
315 | } | |
316 | ||
698a1ec4 JH |
317 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
318 | /* | |
319 | * SNI (RFC4366) | |
320 | * OpenSSL does not document this function, but the implementation | |
321 | * returns 1 on success, 0 on failure after calling SSLerr(). | |
322 | */ | |
323 | ret = SSL_set_tlsext_host_name(sock->ssl, server.host); | |
324 | if (ret != 1) | |
325 | warning("SSL_set_tlsext_host_name(%s) failed.", server.host); | |
326 | #endif | |
327 | ||
684ec6c6 RS |
328 | ret = SSL_connect(sock->ssl); |
329 | if (ret <= 0) { | |
330 | socket_perror("SSL_connect", sock, ret); | |
331 | return -1; | |
332 | } | |
333 | ||
b62fb077 OB |
334 | if (verify) { |
335 | /* make sure the hostname matches that of the certificate */ | |
336 | cert = SSL_get_peer_certificate(sock->ssl); | |
337 | if (!cert) | |
338 | return error("unable to get peer certificate."); | |
339 | if (verify_hostname(cert, server.host) < 0) | |
340 | return -1; | |
341 | } | |
342 | ||
684ec6c6 | 343 | return 0; |
f2561fda | 344 | } |
1e1fe529 | 345 | #endif |
f2561fda | 346 | |
9f1ad541 | 347 | static int socket_read(struct imap_socket *sock, char *buf, int len) |
f2561fda | 348 | { |
684ec6c6 RS |
349 | ssize_t n; |
350 | #ifndef NO_OPENSSL | |
351 | if (sock->ssl) | |
352 | n = SSL_read(sock->ssl, buf, len); | |
353 | else | |
354 | #endif | |
7a7796e9 | 355 | n = xread(sock->fd[0], buf, len); |
f2561fda | 356 | if (n <= 0) { |
95c53908 | 357 | socket_perror("read", sock, n); |
7a7796e9 EFL |
358 | close(sock->fd[0]); |
359 | close(sock->fd[1]); | |
360 | sock->fd[0] = sock->fd[1] = -1; | |
f2561fda MM |
361 | } |
362 | return n; | |
363 | } | |
364 | ||
9f1ad541 | 365 | static int socket_write(struct imap_socket *sock, const char *buf, int len) |
f2561fda | 366 | { |
684ec6c6 RS |
367 | int n; |
368 | #ifndef NO_OPENSSL | |
369 | if (sock->ssl) | |
370 | n = SSL_write(sock->ssl, buf, len); | |
371 | else | |
372 | #endif | |
7a7796e9 | 373 | n = write_in_full(sock->fd[1], buf, len); |
f2561fda | 374 | if (n != len) { |
95c53908 | 375 | socket_perror("write", sock, n); |
7a7796e9 EFL |
376 | close(sock->fd[0]); |
377 | close(sock->fd[1]); | |
378 | sock->fd[0] = sock->fd[1] = -1; | |
f2561fda MM |
379 | } |
380 | return n; | |
381 | } | |
382 | ||
9f1ad541 | 383 | static void socket_shutdown(struct imap_socket *sock) |
684ec6c6 RS |
384 | { |
385 | #ifndef NO_OPENSSL | |
386 | if (sock->ssl) { | |
387 | SSL_shutdown(sock->ssl); | |
388 | SSL_free(sock->ssl); | |
389 | } | |
390 | #endif | |
7a7796e9 EFL |
391 | close(sock->fd[0]); |
392 | close(sock->fd[1]); | |
684ec6c6 RS |
393 | } |
394 | ||
f2561fda | 395 | /* simple line buffering */ |
9f1ad541 | 396 | static int buffer_gets(struct imap_buffer *b, char **s) |
f2561fda MM |
397 | { |
398 | int n; | |
399 | int start = b->offset; | |
400 | ||
401 | *s = b->buf + start; | |
402 | ||
403 | for (;;) { | |
404 | /* make sure we have enough data to read the \r\n sequence */ | |
405 | if (b->offset + 1 >= b->bytes) { | |
406 | if (start) { | |
407 | /* shift down used bytes */ | |
408 | *s = b->buf; | |
409 | ||
95c53908 | 410 | assert(start <= b->bytes); |
f2561fda MM |
411 | n = b->bytes - start; |
412 | ||
413 | if (n) | |
173a9cbe | 414 | memmove(b->buf, b->buf + start, n); |
f2561fda MM |
415 | b->offset -= start; |
416 | b->bytes = n; | |
417 | start = 0; | |
418 | } | |
419 | ||
95c53908 RS |
420 | n = socket_read(&b->sock, b->buf + b->bytes, |
421 | sizeof(b->buf) - b->bytes); | |
f2561fda MM |
422 | |
423 | if (n <= 0) | |
424 | return -1; | |
425 | ||
426 | b->bytes += n; | |
427 | } | |
428 | ||
429 | if (b->buf[b->offset] == '\r') { | |
95c53908 | 430 | assert(b->offset + 1 < b->bytes); |
f2561fda MM |
431 | if (b->buf[b->offset + 1] == '\n') { |
432 | b->buf[b->offset] = 0; /* terminate the string */ | |
433 | b->offset += 2; /* next line */ | |
f1a35295 | 434 | if (0 < verbosity) |
95c53908 | 435 | puts(*s); |
f2561fda MM |
436 | return 0; |
437 | } | |
438 | } | |
439 | ||
440 | b->offset++; | |
441 | } | |
442 | /* not reached */ | |
443 | } | |
444 | ||
48ca53ca | 445 | __attribute__((format (printf, 1, 2))) |
95c53908 | 446 | static void imap_info(const char *msg, ...) |
f2561fda MM |
447 | { |
448 | va_list va; | |
449 | ||
f1a35295 | 450 | if (0 <= verbosity) { |
95c53908 RS |
451 | va_start(va, msg); |
452 | vprintf(msg, va); | |
453 | va_end(va); | |
454 | fflush(stdout); | |
f2561fda MM |
455 | } |
456 | } | |
457 | ||
48ca53ca | 458 | __attribute__((format (printf, 1, 2))) |
95c53908 | 459 | static void imap_warn(const char *msg, ...) |
f2561fda MM |
460 | { |
461 | va_list va; | |
462 | ||
f1a35295 | 463 | if (-2 < verbosity) { |
95c53908 RS |
464 | va_start(va, msg); |
465 | vfprintf(stderr, msg, va); | |
466 | va_end(va); | |
f2561fda MM |
467 | } |
468 | } | |
469 | ||
95c53908 | 470 | static char *next_arg(char **s) |
f2561fda MM |
471 | { |
472 | char *ret; | |
473 | ||
474 | if (!s || !*s) | |
5142db69 | 475 | return NULL; |
95c53908 | 476 | while (isspace((unsigned char) **s)) |
f2561fda MM |
477 | (*s)++; |
478 | if (!**s) { | |
5142db69 RS |
479 | *s = NULL; |
480 | return NULL; | |
f2561fda MM |
481 | } |
482 | if (**s == '"') { | |
483 | ++*s; | |
484 | ret = *s; | |
95c53908 | 485 | *s = strchr(*s, '"'); |
f2561fda MM |
486 | } else { |
487 | ret = *s; | |
95c53908 | 488 | while (**s && !isspace((unsigned char) **s)) |
f2561fda MM |
489 | (*s)++; |
490 | } | |
491 | if (*s) { | |
492 | if (**s) | |
493 | *(*s)++ = 0; | |
494 | if (!**s) | |
5142db69 | 495 | *s = NULL; |
f2561fda MM |
496 | } |
497 | return ret; | |
498 | } | |
499 | ||
e0d8e308 TF |
500 | static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx, |
501 | struct imap_cmd_cb *cb, | |
502 | const char *fmt, va_list ap) | |
f2561fda | 503 | { |
9f1ad541 | 504 | struct imap *imap = ctx->imap; |
f2561fda MM |
505 | struct imap_cmd *cmd; |
506 | int n, bufl; | |
507 | char buf[1024]; | |
508 | ||
95c53908 RS |
509 | cmd = xmalloc(sizeof(struct imap_cmd)); |
510 | nfvasprintf(&cmd->cmd, fmt, ap); | |
f2561fda MM |
511 | cmd->tag = ++imap->nexttag; |
512 | ||
513 | if (cb) | |
514 | cmd->cb = *cb; | |
515 | else | |
95c53908 | 516 | memset(&cmd->cb, 0, sizeof(cmd->cb)); |
f2561fda MM |
517 | |
518 | while (imap->literal_pending) | |
95c53908 | 519 | get_cmd_result(ctx, NULL); |
f2561fda | 520 | |
1702b138 | 521 | if (!cmd->cb.data) |
39bb6921 | 522 | bufl = xsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd); |
1702b138 | 523 | else |
39bb6921 RS |
524 | bufl = xsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n", |
525 | cmd->tag, cmd->cmd, cmd->cb.dlen, | |
526 | CAP(LITERALPLUS) ? "+" : ""); | |
f2561fda | 527 | |
f1a35295 | 528 | if (0 < verbosity) { |
f2561fda | 529 | if (imap->num_in_progress) |
95c53908 | 530 | printf("(%d in progress) ", imap->num_in_progress); |
ba9b9e12 | 531 | if (!starts_with(cmd->cmd, "LOGIN")) |
95c53908 | 532 | printf(">>> %s", buf); |
f2561fda | 533 | else |
95c53908 | 534 | printf(">>> %d LOGIN <user> <pass>\n", cmd->tag); |
f2561fda | 535 | } |
95c53908 RS |
536 | if (socket_write(&imap->buf.sock, buf, bufl) != bufl) { |
537 | free(cmd->cmd); | |
538 | free(cmd); | |
8e0f7003 | 539 | if (cb) |
95c53908 | 540 | free(cb->data); |
f2561fda MM |
541 | return NULL; |
542 | } | |
543 | if (cmd->cb.data) { | |
544 | if (CAP(LITERALPLUS)) { | |
95c53908 RS |
545 | n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen); |
546 | free(cmd->cb.data); | |
f2561fda | 547 | if (n != cmd->cb.dlen || |
8e76bf3f | 548 | socket_write(&imap->buf.sock, "\r\n", 2) != 2) { |
95c53908 RS |
549 | free(cmd->cmd); |
550 | free(cmd); | |
f2561fda MM |
551 | return NULL; |
552 | } | |
5142db69 | 553 | cmd->cb.data = NULL; |
f2561fda MM |
554 | } else |
555 | imap->literal_pending = 1; | |
556 | } else if (cmd->cb.cont) | |
557 | imap->literal_pending = 1; | |
5142db69 | 558 | cmd->next = NULL; |
f2561fda MM |
559 | *imap->in_progress_append = cmd; |
560 | imap->in_progress_append = &cmd->next; | |
561 | imap->num_in_progress++; | |
562 | return cmd; | |
563 | } | |
564 | ||
28bea9e5 | 565 | __attribute__((format (printf, 3, 4))) |
9f1ad541 | 566 | static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 567 | const char *fmt, ...) |
f2561fda MM |
568 | { |
569 | va_list ap; | |
570 | struct imap_cmd *cmdp; | |
571 | ||
95c53908 | 572 | va_start(ap, fmt); |
e0d8e308 | 573 | cmdp = issue_imap_cmd(ctx, cb, fmt, ap); |
95c53908 | 574 | va_end(ap); |
f2561fda MM |
575 | if (!cmdp) |
576 | return RESP_BAD; | |
577 | ||
95c53908 | 578 | return get_cmd_result(ctx, cmdp); |
f2561fda MM |
579 | } |
580 | ||
28bea9e5 | 581 | __attribute__((format (printf, 3, 4))) |
9f1ad541 | 582 | static int imap_exec_m(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 | 588 | va_start(ap, fmt); |
e0d8e308 | 589 | cmdp = issue_imap_cmd(ctx, cb, fmt, ap); |
95c53908 | 590 | va_end(ap); |
f2561fda MM |
591 | if (!cmdp) |
592 | return DRV_STORE_BAD; | |
593 | ||
95c53908 | 594 | switch (get_cmd_result(ctx, cmdp)) { |
f2561fda MM |
595 | case RESP_BAD: return DRV_STORE_BAD; |
596 | case RESP_NO: return DRV_MSG_BAD; | |
597 | default: return DRV_OK; | |
598 | } | |
599 | } | |
600 | ||
3648b4d9 | 601 | static int skip_imap_list_l(char **sp, int level) |
f2561fda | 602 | { |
3648b4d9 | 603 | char *s = *sp; |
f2561fda MM |
604 | |
605 | for (;;) { | |
95c53908 | 606 | while (isspace((unsigned char)*s)) |
f2561fda MM |
607 | s++; |
608 | if (level && *s == ')') { | |
609 | s++; | |
610 | break; | |
611 | } | |
f2561fda MM |
612 | if (*s == '(') { |
613 | /* sublist */ | |
614 | s++; | |
3648b4d9 | 615 | if (skip_imap_list_l(&s, level + 1)) |
f2561fda MM |
616 | goto bail; |
617 | } else if (*s == '"') { | |
618 | /* quoted string */ | |
619 | s++; | |
f2561fda MM |
620 | for (; *s != '"'; s++) |
621 | if (!*s) | |
622 | goto bail; | |
f2561fda | 623 | s++; |
f2561fda MM |
624 | } else { |
625 | /* atom */ | |
95c53908 | 626 | for (; *s && !isspace((unsigned char)*s); s++) |
f2561fda MM |
627 | if (level && *s == ')') |
628 | break; | |
f2561fda MM |
629 | } |
630 | ||
631 | if (!level) | |
632 | break; | |
633 | if (!*s) | |
634 | goto bail; | |
635 | } | |
636 | *sp = s; | |
f2561fda MM |
637 | return 0; |
638 | ||
9f1ad541 | 639 | bail: |
f2561fda MM |
640 | return -1; |
641 | } | |
642 | ||
3648b4d9 | 643 | static void skip_list(char **sp) |
f2561fda | 644 | { |
3648b4d9 | 645 | skip_imap_list_l(sp, 0); |
f2561fda MM |
646 | } |
647 | ||
9f1ad541 | 648 | static void parse_capability(struct imap *imap, char *cmd) |
f2561fda MM |
649 | { |
650 | char *arg; | |
651 | unsigned i; | |
652 | ||
653 | imap->caps = 0x80000000; | |
95c53908 | 654 | while ((arg = next_arg(&cmd))) |
f2561fda | 655 | for (i = 0; i < ARRAY_SIZE(cap_list); i++) |
95c53908 | 656 | if (!strcmp(cap_list[i], arg)) |
f2561fda MM |
657 | imap->caps |= 1 << i; |
658 | imap->rcaps = imap->caps; | |
659 | } | |
660 | ||
9f1ad541 | 661 | static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb, |
95c53908 | 662 | char *s) |
f2561fda | 663 | { |
9f1ad541 | 664 | struct imap *imap = ctx->imap; |
f2561fda MM |
665 | char *arg, *p; |
666 | ||
618ec81a | 667 | if (!s || *s != '[') |
f2561fda MM |
668 | return RESP_OK; /* no response code */ |
669 | s++; | |
95c53908 RS |
670 | if (!(p = strchr(s, ']'))) { |
671 | fprintf(stderr, "IMAP error: malformed response code\n"); | |
f2561fda MM |
672 | return RESP_BAD; |
673 | } | |
674 | *p++ = 0; | |
95c53908 | 675 | arg = next_arg(&s); |
f54c5bd4 RS |
676 | if (!arg) { |
677 | fprintf(stderr, "IMAP error: empty response code\n"); | |
678 | return RESP_BAD; | |
679 | } | |
95c53908 | 680 | if (!strcmp("UIDVALIDITY", arg)) { |
636fd66b | 681 | if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) { |
95c53908 | 682 | fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n"); |
f2561fda MM |
683 | return RESP_BAD; |
684 | } | |
95c53908 RS |
685 | } else if (!strcmp("UIDNEXT", arg)) { |
686 | if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) { | |
687 | fprintf(stderr, "IMAP error: malformed NEXTUID status\n"); | |
f2561fda MM |
688 | return RESP_BAD; |
689 | } | |
95c53908 RS |
690 | } else if (!strcmp("CAPABILITY", arg)) { |
691 | parse_capability(imap, s); | |
692 | } else if (!strcmp("ALERT", arg)) { | |
f2561fda MM |
693 | /* RFC2060 says that these messages MUST be displayed |
694 | * to the user | |
695 | */ | |
95c53908 RS |
696 | for (; isspace((unsigned char)*p); p++); |
697 | fprintf(stderr, "*** IMAP ALERT *** %s\n", p); | |
698 | } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) { | |
636fd66b | 699 | if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) || |
9f1ad541 | 700 | !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) { |
95c53908 | 701 | fprintf(stderr, "IMAP error: malformed APPENDUID status\n"); |
f2561fda MM |
702 | return RESP_BAD; |
703 | } | |
704 | } | |
705 | return RESP_OK; | |
706 | } | |
707 | ||
9f1ad541 | 708 | static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd) |
f2561fda | 709 | { |
9f1ad541 | 710 | struct imap *imap = ctx->imap; |
e0d8e308 | 711 | struct imap_cmd *cmdp, **pcmdp; |
f54c5bd4 RS |
712 | char *cmd; |
713 | const char *arg, *arg1; | |
f2561fda MM |
714 | int n, resp, resp2, tag; |
715 | ||
716 | for (;;) { | |
95c53908 | 717 | if (buffer_gets(&imap->buf, &cmd)) |
f2561fda MM |
718 | return RESP_BAD; |
719 | ||
95c53908 | 720 | arg = next_arg(&cmd); |
f54c5bd4 RS |
721 | if (!arg) { |
722 | fprintf(stderr, "IMAP error: empty response\n"); | |
723 | return RESP_BAD; | |
724 | } | |
f2561fda | 725 | if (*arg == '*') { |
95c53908 | 726 | arg = next_arg(&cmd); |
f2561fda | 727 | if (!arg) { |
95c53908 | 728 | fprintf(stderr, "IMAP error: unable to parse untagged response\n"); |
f2561fda MM |
729 | return RESP_BAD; |
730 | } | |
731 | ||
95c53908 | 732 | if (!strcmp("NAMESPACE", arg)) { |
3648b4d9 MH |
733 | /* rfc2342 NAMESPACE response. */ |
734 | skip_list(&cmd); /* Personal mailboxes */ | |
735 | skip_list(&cmd); /* Others' mailboxes */ | |
736 | skip_list(&cmd); /* Shared mailboxes */ | |
95c53908 RS |
737 | } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) || |
738 | !strcmp("NO", arg) || !strcmp("BYE", arg)) { | |
739 | if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK) | |
f2561fda | 740 | return resp; |
1efee7ff | 741 | } else if (!strcmp("CAPABILITY", arg)) { |
95c53908 | 742 | parse_capability(imap, cmd); |
1efee7ff MH |
743 | } else if ((arg1 = next_arg(&cmd))) { |
744 | ; /* | |
745 | * Unhandled response-data with at least two words. | |
746 | * Ignore it. | |
747 | * | |
748 | * NEEDSWORK: Previously this case handled '<num> EXISTS' | |
749 | * and '<num> RECENT' but as a probably-unintended side | |
750 | * effect it ignores other unrecognized two-word | |
751 | * responses. imap-send doesn't ever try to read | |
752 | * messages or mailboxes these days, so consider | |
753 | * eliminating this case. | |
754 | */ | |
f2561fda | 755 | } else { |
95c53908 | 756 | fprintf(stderr, "IMAP error: unable to parse untagged response\n"); |
f2561fda MM |
757 | return RESP_BAD; |
758 | } | |
759 | } else if (!imap->in_progress) { | |
95c53908 | 760 | fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : ""); |
f2561fda MM |
761 | return RESP_BAD; |
762 | } else if (*arg == '+') { | |
763 | /* This can happen only with the last command underway, as | |
764 | it enforces a round-trip. */ | |
765 | cmdp = (struct imap_cmd *)((char *)imap->in_progress_append - | |
766 | offsetof(struct imap_cmd, next)); | |
767 | if (cmdp->cb.data) { | |
95c53908 | 768 | n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen); |
6a83d902 | 769 | FREE_AND_NULL(cmdp->cb.data); |
f2561fda MM |
770 | if (n != (int)cmdp->cb.dlen) |
771 | return RESP_BAD; | |
772 | } else if (cmdp->cb.cont) { | |
ec9e358a | 773 | if (cmdp->cb.cont(ctx, cmd)) |
f2561fda MM |
774 | return RESP_BAD; |
775 | } else { | |
95c53908 | 776 | fprintf(stderr, "IMAP error: unexpected command continuation request\n"); |
f2561fda MM |
777 | return RESP_BAD; |
778 | } | |
95c53908 | 779 | if (socket_write(&imap->buf.sock, "\r\n", 2) != 2) |
f2561fda MM |
780 | return RESP_BAD; |
781 | if (!cmdp->cb.cont) | |
782 | imap->literal_pending = 0; | |
783 | if (!tcmd) | |
784 | return DRV_OK; | |
785 | } else { | |
95c53908 | 786 | tag = atoi(arg); |
f2561fda MM |
787 | for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next) |
788 | if (cmdp->tag == tag) | |
789 | goto gottag; | |
95c53908 | 790 | fprintf(stderr, "IMAP error: unexpected tag %s\n", arg); |
f2561fda | 791 | return RESP_BAD; |
9f1ad541 | 792 | gottag: |
f2561fda MM |
793 | if (!(*pcmdp = cmdp->next)) |
794 | imap->in_progress_append = pcmdp; | |
795 | imap->num_in_progress--; | |
796 | if (cmdp->cb.cont || cmdp->cb.data) | |
797 | imap->literal_pending = 0; | |
95c53908 | 798 | arg = next_arg(&cmd); |
f54c5bd4 RS |
799 | if (!arg) |
800 | arg = ""; | |
95c53908 | 801 | if (!strcmp("OK", arg)) |
f2561fda MM |
802 | resp = DRV_OK; |
803 | else { | |
e0d8e308 | 804 | if (!strcmp("NO", arg)) |
f2561fda | 805 | resp = RESP_NO; |
e0d8e308 | 806 | else /*if (!strcmp("BAD", arg))*/ |
f2561fda | 807 | resp = RESP_BAD; |
95c53908 | 808 | fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n", |
ba9b9e12 | 809 | !starts_with(cmdp->cmd, "LOGIN") ? |
f2561fda MM |
810 | cmdp->cmd : "LOGIN <user> <pass>", |
811 | arg, cmd ? cmd : ""); | |
812 | } | |
95c53908 | 813 | if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp) |
f2561fda | 814 | resp = resp2; |
95c53908 RS |
815 | free(cmdp->cb.data); |
816 | free(cmdp->cmd); | |
817 | free(cmdp); | |
f2561fda MM |
818 | if (!tcmd || tcmd == cmdp) |
819 | return resp; | |
820 | } | |
821 | } | |
822 | /* not reached */ | |
823 | } | |
824 | ||
9f1ad541 | 825 | static void imap_close_server(struct imap_store *ictx) |
f2561fda | 826 | { |
9f1ad541 | 827 | struct imap *imap = ictx->imap; |
f2561fda | 828 | |
7a7796e9 | 829 | if (imap->buf.sock.fd[0] != -1) { |
95c53908 RS |
830 | imap_exec(ictx, NULL, "LOGOUT"); |
831 | socket_shutdown(&imap->buf.sock); | |
f2561fda | 832 | } |
95c53908 | 833 | free(imap); |
f2561fda MM |
834 | } |
835 | ||
fe47e1df | 836 | static void imap_close_store(struct imap_store *ctx) |
f2561fda | 837 | { |
fe47e1df | 838 | imap_close_server(ctx); |
95c53908 | 839 | free(ctx); |
f2561fda MM |
840 | } |
841 | ||
ae9c606e HM |
842 | #ifndef NO_OPENSSL |
843 | ||
844 | /* | |
845 | * hexchar() and cram() functions are based on the code from the isync | |
65175d9e | 846 | * project (https://isync.sourceforge.io/). |
ae9c606e HM |
847 | */ |
848 | static char hexchar(unsigned int b) | |
f2561fda | 849 | { |
ae9c606e | 850 | return b < 10 ? '0' + b : 'a' + (b - 10); |
f2561fda MM |
851 | } |
852 | ||
42c78a21 | 853 | #define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3)) |
ae9c606e | 854 | static char *cram(const char *challenge_64, const char *user, const char *pass) |
f2561fda | 855 | { |
ae9c606e | 856 | int i, resp_len, encoded_len, decoded_len; |
ae9c606e HM |
857 | unsigned char hash[16]; |
858 | char hex[33]; | |
859 | char *response, *response_64, *challenge; | |
860 | ||
861 | /* | |
862 | * length of challenge_64 (i.e. base-64 encoded string) is a good | |
863 | * enough upper bound for challenge (decoded result). | |
864 | */ | |
865 | encoded_len = strlen(challenge_64); | |
866 | challenge = xmalloc(encoded_len); | |
867 | decoded_len = EVP_DecodeBlock((unsigned char *)challenge, | |
868 | (unsigned char *)challenge_64, encoded_len); | |
869 | if (decoded_len < 0) | |
870 | die("invalid challenge %s", challenge_64); | |
1ed2c7b1 KY |
871 | if (!HMAC(EVP_md5(), pass, strlen(pass), (unsigned char *)challenge, decoded_len, hash, NULL)) |
872 | die("HMAC error"); | |
ae9c606e HM |
873 | |
874 | hex[32] = 0; | |
875 | for (i = 0; i < 16; i++) { | |
876 | hex[2 * i] = hexchar((hash[i] >> 4) & 0xf); | |
877 | hex[2 * i + 1] = hexchar(hash[i] & 0xf); | |
878 | } | |
879 | ||
880 | /* response: "<user> <digest in hex>" */ | |
75faa45a | 881 | response = xstrfmt("%s %s", user, hex); |
eb94ee7f | 882 | resp_len = strlen(response); |
ae9c606e | 883 | |
3733e694 | 884 | response_64 = xmallocz(ENCODED_SIZE(resp_len)); |
ae9c606e HM |
885 | encoded_len = EVP_EncodeBlock((unsigned char *)response_64, |
886 | (unsigned char *)response, resp_len); | |
887 | if (encoded_len < 0) | |
888 | die("EVP_EncodeBlock error"); | |
ae9c606e HM |
889 | return (char *)response_64; |
890 | } | |
891 | ||
892 | #else | |
893 | ||
2c3c3d88 JK |
894 | static char *cram(const char *challenge_64 UNUSED, |
895 | const char *user UNUSED, | |
896 | const char *pass UNUSED) | |
ae9c606e HM |
897 | { |
898 | die("If you want to use CRAM-MD5 authenticate method, " | |
899 | "you have to build git-imap-send with OpenSSL library."); | |
900 | } | |
901 | ||
902 | #endif | |
903 | ||
ec9e358a | 904 | static int auth_cram_md5(struct imap_store *ctx, const char *prompt) |
ae9c606e HM |
905 | { |
906 | int ret; | |
907 | char *response; | |
908 | ||
909 | response = cram(prompt, server.user, server.pass); | |
910 | ||
911 | ret = socket_write(&ctx->imap->buf.sock, response, strlen(response)); | |
912 | if (ret != strlen(response)) | |
82247e9b | 913 | return error("IMAP error: sending response failed"); |
ae9c606e HM |
914 | |
915 | free(response); | |
916 | ||
917 | return 0; | |
918 | } | |
919 | ||
690307f3 NMC |
920 | static void server_fill_credential(struct imap_server_conf *srvc, struct credential *cred) |
921 | { | |
922 | if (srvc->user && srvc->pass) | |
923 | return; | |
924 | ||
925 | cred->protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap"); | |
926 | cred->host = xstrdup(srvc->host); | |
927 | ||
928 | cred->username = xstrdup_or_null(srvc->user); | |
929 | cred->password = xstrdup_or_null(srvc->pass); | |
930 | ||
931 | credential_fill(cred); | |
932 | ||
933 | if (!srvc->user) | |
934 | srvc->user = xstrdup(cred->username); | |
935 | if (!srvc->pass) | |
936 | srvc->pass = xstrdup(cred->password); | |
937 | } | |
938 | ||
50212361 | 939 | static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const char *folder) |
f2561fda | 940 | { |
791643a8 | 941 | struct credential cred = CREDENTIAL_INIT; |
9f1ad541 JH |
942 | struct imap_store *ctx; |
943 | struct imap *imap; | |
f2561fda | 944 | char *arg, *rsp; |
c94d2dd0 | 945 | int s = -1, preauth; |
f2561fda | 946 | |
ca56dadb | 947 | CALLOC_ARRAY(ctx, 1); |
f2561fda | 948 | |
ca56dadb | 949 | ctx->imap = CALLOC_ARRAY(imap, 1); |
7a7796e9 | 950 | imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1; |
f2561fda MM |
951 | imap->in_progress_append = &imap->in_progress; |
952 | ||
953 | /* open connection to IMAP server */ | |
954 | ||
955 | if (srvc->tunnel) { | |
d3180279 | 956 | struct child_process tunnel = CHILD_PROCESS_INIT; |
f2561fda | 957 | |
c94d2dd0 | 958 | imap_info("Starting tunnel '%s'... ", srvc->tunnel); |
f2561fda | 959 | |
ef8d7ac4 | 960 | strvec_push(&tunnel.args, srvc->tunnel); |
ac0ba18d | 961 | tunnel.use_shell = 1; |
c94d2dd0 EFL |
962 | tunnel.in = -1; |
963 | tunnel.out = -1; | |
964 | if (start_command(&tunnel)) | |
f9dc5d65 | 965 | die("cannot start proxy %s", srvc->tunnel); |
f2561fda | 966 | |
c94d2dd0 EFL |
967 | imap->buf.sock.fd[0] = tunnel.out; |
968 | imap->buf.sock.fd[1] = tunnel.in; | |
f2561fda | 969 | |
95c53908 | 970 | imap_info("ok\n"); |
f2561fda | 971 | } else { |
94ad2437 BK |
972 | #ifndef NO_IPV6 |
973 | struct addrinfo hints, *ai0, *ai; | |
974 | int gai; | |
975 | char portstr[6]; | |
976 | ||
1a168e5c | 977 | xsnprintf(portstr, sizeof(portstr), "%d", srvc->port); |
94ad2437 BK |
978 | |
979 | memset(&hints, 0, sizeof(hints)); | |
980 | hints.ai_socktype = SOCK_STREAM; | |
981 | hints.ai_protocol = IPPROTO_TCP; | |
f2561fda | 982 | |
94ad2437 BK |
983 | imap_info("Resolving %s... ", srvc->host); |
984 | gai = getaddrinfo(srvc->host, portstr, &hints, &ai); | |
985 | if (gai) { | |
986 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai)); | |
987 | goto bail; | |
f2561fda | 988 | } |
94ad2437 | 989 | imap_info("ok\n"); |
f2561fda | 990 | |
94ad2437 BK |
991 | for (ai0 = ai; ai; ai = ai->ai_next) { |
992 | char addr[NI_MAXHOST]; | |
f2561fda | 993 | |
94ad2437 BK |
994 | s = socket(ai->ai_family, ai->ai_socktype, |
995 | ai->ai_protocol); | |
996 | if (s < 0) | |
997 | continue; | |
f2561fda | 998 | |
94ad2437 BK |
999 | getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, |
1000 | sizeof(addr), NULL, 0, NI_NUMERICHOST); | |
1001 | imap_info("Connecting to [%s]:%s... ", addr, portstr); | |
1002 | ||
1003 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { | |
1004 | close(s); | |
1005 | s = -1; | |
1006 | perror("connect"); | |
1007 | continue; | |
1008 | } | |
1009 | ||
1010 | break; | |
1011 | } | |
1012 | freeaddrinfo(ai0); | |
1013 | #else /* NO_IPV6 */ | |
1014 | struct hostent *he; | |
1015 | struct sockaddr_in addr; | |
1016 | ||
95c53908 RS |
1017 | memset(&addr, 0, sizeof(addr)); |
1018 | addr.sin_port = htons(srvc->port); | |
f2561fda MM |
1019 | addr.sin_family = AF_INET; |
1020 | ||
95c53908 RS |
1021 | imap_info("Resolving %s... ", srvc->host); |
1022 | he = gethostbyname(srvc->host); | |
f2561fda | 1023 | if (!he) { |
95c53908 | 1024 | perror("gethostbyname"); |
f2561fda MM |
1025 | goto bail; |
1026 | } | |
95c53908 | 1027 | imap_info("ok\n"); |
f2561fda MM |
1028 | |
1029 | addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]); | |
1030 | ||
95c53908 | 1031 | s = socket(PF_INET, SOCK_STREAM, 0); |
f2561fda | 1032 | |
95c53908 RS |
1033 | imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); |
1034 | if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) { | |
1035 | close(s); | |
94ad2437 | 1036 | s = -1; |
95c53908 | 1037 | perror("connect"); |
94ad2437 BK |
1038 | } |
1039 | #endif | |
1040 | if (s < 0) { | |
1041 | fputs("Error: unable to connect to server.\n", stderr); | |
f2561fda MM |
1042 | goto bail; |
1043 | } | |
f2561fda | 1044 | |
7a7796e9 EFL |
1045 | imap->buf.sock.fd[0] = s; |
1046 | imap->buf.sock.fd[1] = dup(s); | |
f2561fda | 1047 | |
684ec6c6 RS |
1048 | if (srvc->use_ssl && |
1049 | ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) { | |
1050 | close(s); | |
1051 | goto bail; | |
1052 | } | |
95c53908 | 1053 | imap_info("ok\n"); |
f2561fda MM |
1054 | } |
1055 | ||
1056 | /* read the greeting string */ | |
95c53908 RS |
1057 | if (buffer_gets(&imap->buf, &rsp)) { |
1058 | fprintf(stderr, "IMAP error: no greeting response\n"); | |
f2561fda MM |
1059 | goto bail; |
1060 | } | |
95c53908 RS |
1061 | arg = next_arg(&rsp); |
1062 | if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) { | |
1063 | fprintf(stderr, "IMAP error: invalid greeting response\n"); | |
f2561fda MM |
1064 | goto bail; |
1065 | } | |
1066 | preauth = 0; | |
95c53908 | 1067 | if (!strcmp("PREAUTH", arg)) |
f2561fda | 1068 | preauth = 1; |
95c53908 RS |
1069 | else if (strcmp("OK", arg) != 0) { |
1070 | fprintf(stderr, "IMAP error: unknown greeting response\n"); | |
f2561fda MM |
1071 | goto bail; |
1072 | } | |
95c53908 RS |
1073 | parse_response_code(ctx, NULL, rsp); |
1074 | if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK) | |
f2561fda MM |
1075 | goto bail; |
1076 | ||
1077 | if (!preauth) { | |
684ec6c6 RS |
1078 | #ifndef NO_OPENSSL |
1079 | if (!srvc->use_ssl && CAP(STARTTLS)) { | |
d27da38a | 1080 | if (imap_exec(ctx, NULL, "STARTTLS") != RESP_OK) |
684ec6c6 RS |
1081 | goto bail; |
1082 | if (ssl_socket_connect(&imap->buf.sock, 1, | |
1083 | srvc->ssl_verify)) | |
1084 | goto bail; | |
1085 | /* capabilities may have changed, so get the new capabilities */ | |
d27da38a | 1086 | if (imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK) |
684ec6c6 RS |
1087 | goto bail; |
1088 | } | |
1089 | #endif | |
95c53908 | 1090 | imap_info("Logging in...\n"); |
690307f3 | 1091 | server_fill_credential(srvc, &cred); |
791643a8 | 1092 | |
ae9c606e HM |
1093 | if (srvc->auth_method) { |
1094 | struct imap_cmd_cb cb; | |
1095 | ||
1096 | if (!strcmp(srvc->auth_method, "CRAM-MD5")) { | |
1097 | if (!CAP(AUTH_CRAM_MD5)) { | |
6d1fbf88 | 1098 | fprintf(stderr, "You specified " |
ae9c606e HM |
1099 | "CRAM-MD5 as authentication method, " |
1100 | "but %s doesn't support it.\n", srvc->host); | |
1101 | goto bail; | |
1102 | } | |
1103 | /* CRAM-MD5 */ | |
1104 | ||
1105 | memset(&cb, 0, sizeof(cb)); | |
1106 | cb.cont = auth_cram_md5; | |
1107 | if (imap_exec(ctx, &cb, "AUTHENTICATE CRAM-MD5") != RESP_OK) { | |
1108 | fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n"); | |
1109 | goto bail; | |
1110 | } | |
1111 | } else { | |
1112 | fprintf(stderr, "Unknown authentication method:%s\n", srvc->host); | |
1113 | goto bail; | |
1114 | } | |
1115 | } else { | |
6c50a575 KY |
1116 | if (CAP(NOLOGIN)) { |
1117 | fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", | |
1118 | srvc->user, srvc->host); | |
1119 | goto bail; | |
1120 | } | |
10439d89 CW |
1121 | if (!imap->buf.sock.ssl) |
1122 | imap_warn("*** IMAP Warning *** Password is being " | |
1123 | "sent in the clear\n"); | |
ae9c606e HM |
1124 | if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) { |
1125 | fprintf(stderr, "IMAP error: LOGIN failed\n"); | |
1126 | goto bail; | |
1127 | } | |
f2561fda MM |
1128 | } |
1129 | } /* !preauth */ | |
1130 | ||
791643a8 DA |
1131 | if (cred.username) |
1132 | credential_approve(&cred); | |
1133 | credential_clear(&cred); | |
1134 | ||
e0d8e308 TF |
1135 | /* check the target mailbox exists */ |
1136 | ctx->name = folder; | |
1137 | switch (imap_exec(ctx, NULL, "EXAMINE \"%s\"", ctx->name)) { | |
1138 | case RESP_OK: | |
1139 | /* ok */ | |
1140 | break; | |
1141 | case RESP_BAD: | |
1142 | fprintf(stderr, "IMAP error: could not check mailbox\n"); | |
1143 | goto out; | |
1144 | case RESP_NO: | |
1145 | if (imap_exec(ctx, NULL, "CREATE \"%s\"", ctx->name) == RESP_OK) { | |
1146 | imap_info("Created missing mailbox\n"); | |
1147 | } else { | |
1148 | fprintf(stderr, "IMAP error: could not create missing mailbox\n"); | |
1149 | goto out; | |
1150 | } | |
1151 | break; | |
1152 | } | |
1153 | ||
f2561fda | 1154 | ctx->prefix = ""; |
fe47e1df | 1155 | return ctx; |
f2561fda | 1156 | |
9f1ad541 | 1157 | bail: |
791643a8 DA |
1158 | if (cred.username) |
1159 | credential_reject(&cred); | |
1160 | credential_clear(&cred); | |
1161 | ||
e0d8e308 | 1162 | out: |
fe47e1df | 1163 | imap_close_store(ctx); |
5142db69 | 1164 | return NULL; |
f2561fda MM |
1165 | } |
1166 | ||
3691031c MH |
1167 | /* |
1168 | * Insert CR characters as necessary in *msg to ensure that every LF | |
1169 | * character in *msg is preceded by a CR. | |
1170 | */ | |
f035ab62 | 1171 | static void lf_to_crlf(struct strbuf *msg) |
f2561fda | 1172 | { |
59256315 | 1173 | char *new_msg; |
3691031c MH |
1174 | size_t i, j; |
1175 | char lastc; | |
1176 | ||
59256315 | 1177 | /* First pass: tally, in j, the size of the new_msg string: */ |
3691031c MH |
1178 | for (i = j = 0, lastc = '\0'; i < msg->len; i++) { |
1179 | if (msg->buf[i] == '\n' && lastc != '\r') | |
1180 | j++; /* a CR will need to be added here */ | |
1181 | lastc = msg->buf[i]; | |
1182 | j++; | |
f2561fda | 1183 | } |
67d17630 | 1184 | |
59256315 | 1185 | new_msg = xmallocz(j); |
3691031c MH |
1186 | |
1187 | /* | |
59256315 | 1188 | * Second pass: write the new_msg string. Note that this loop is |
3691031c MH |
1189 | * otherwise identical to the first pass. |
1190 | */ | |
1191 | for (i = j = 0, lastc = '\0'; i < msg->len; i++) { | |
1192 | if (msg->buf[i] == '\n' && lastc != '\r') | |
59256315 BW |
1193 | new_msg[j++] = '\r'; |
1194 | lastc = new_msg[j++] = msg->buf[i]; | |
f2561fda | 1195 | } |
59256315 | 1196 | strbuf_attach(msg, new_msg, j, j + 1); |
67d17630 | 1197 | } |
f2561fda | 1198 | |
f035ab62 MH |
1199 | /* |
1200 | * Store msg to IMAP. Also detach and free the data from msg->data, | |
1201 | * leaving msg->data empty. | |
1202 | */ | |
fe47e1df | 1203 | static int imap_store_msg(struct imap_store *ctx, struct strbuf *msg) |
f2561fda | 1204 | { |
9f1ad541 | 1205 | struct imap *imap = ctx->imap; |
f2561fda | 1206 | struct imap_cmd_cb cb; |
f2561fda | 1207 | const char *prefix, *box; |
719125c5 | 1208 | int ret; |
f2561fda | 1209 | |
cbc60761 | 1210 | lf_to_crlf(msg); |
95c53908 | 1211 | memset(&cb, 0, sizeof(cb)); |
f2561fda | 1212 | |
cbc60761 MH |
1213 | cb.dlen = msg->len; |
1214 | cb.data = strbuf_detach(msg, NULL); | |
f2561fda | 1215 | |
636fd66b | 1216 | box = ctx->name; |
3a7cba95 | 1217 | prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix; |
719125c5 | 1218 | ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" ", prefix, box); |
f2561fda MM |
1219 | imap->caps = imap->rcaps; |
1220 | if (ret != DRV_OK) | |
1221 | return ret; | |
f2561fda MM |
1222 | |
1223 | return DRV_OK; | |
1224 | } | |
1225 | ||
f035ab62 | 1226 | static void wrap_in_html(struct strbuf *msg) |
c64d84f1 JW |
1227 | { |
1228 | struct strbuf buf = STRBUF_INIT; | |
c64d84f1 JW |
1229 | static char *content_type = "Content-Type: text/html;\n"; |
1230 | static char *pre_open = "<pre>\n"; | |
1231 | static char *pre_close = "</pre>\n"; | |
118a68f9 MH |
1232 | const char *body = strstr(msg->buf, "\n\n"); |
1233 | ||
1234 | if (!body) | |
1235 | return; /* Headers but no body; no wrapping needed */ | |
1236 | ||
1237 | body += 2; | |
1238 | ||
1239 | strbuf_add(&buf, msg->buf, body - msg->buf - 1); | |
1240 | strbuf_addstr(&buf, content_type); | |
1241 | strbuf_addch(&buf, '\n'); | |
1242 | strbuf_addstr(&buf, pre_open); | |
1243 | strbuf_addstr_xml_quoted(&buf, body); | |
c64d84f1 | 1244 | strbuf_addstr(&buf, pre_close); |
118a68f9 | 1245 | |
f035ab62 MH |
1246 | strbuf_release(msg); |
1247 | *msg = buf; | |
c64d84f1 JW |
1248 | } |
1249 | ||
3a34e626 | 1250 | static int count_messages(struct strbuf *all_msgs) |
f2561fda MM |
1251 | { |
1252 | int count = 0; | |
3a34e626 | 1253 | char *p = all_msgs->buf; |
f2561fda MM |
1254 | |
1255 | while (1) { | |
59556548 | 1256 | if (starts_with(p, "From ")) { |
4916c8f9 RR |
1257 | p = strstr(p+5, "\nFrom: "); |
1258 | if (!p) break; | |
1259 | p = strstr(p+7, "\nDate: "); | |
1260 | if (!p) break; | |
1261 | p = strstr(p+7, "\nSubject: "); | |
1262 | if (!p) break; | |
1263 | p += 10; | |
f2561fda | 1264 | count++; |
f2561fda | 1265 | } |
95c53908 | 1266 | p = strstr(p+5, "\nFrom "); |
f2561fda MM |
1267 | if (!p) |
1268 | break; | |
1269 | p++; | |
1270 | } | |
1271 | return count; | |
1272 | } | |
1273 | ||
f035ab62 MH |
1274 | /* |
1275 | * Copy the next message from all_msgs, starting at offset *ofs, to | |
1276 | * msg. Update *ofs to the start of the following message. Return | |
1277 | * true iff a message was successfully copied. | |
1278 | */ | |
1279 | static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs) | |
f2561fda MM |
1280 | { |
1281 | char *p, *data; | |
f035ab62 | 1282 | size_t len; |
f2561fda | 1283 | |
f2561fda MM |
1284 | if (*ofs >= all_msgs->len) |
1285 | return 0; | |
1286 | ||
3a34e626 | 1287 | data = &all_msgs->buf[*ofs]; |
f035ab62 | 1288 | len = all_msgs->len - *ofs; |
f2561fda | 1289 | |
59556548 | 1290 | if (len < 5 || !starts_with(data, "From ")) |
f2561fda MM |
1291 | return 0; |
1292 | ||
95c53908 | 1293 | p = strchr(data, '\n'); |
e0b08307 | 1294 | if (p) { |
f035ab62 MH |
1295 | p++; |
1296 | len -= p - data; | |
1297 | *ofs += p - data; | |
e0b08307 MA |
1298 | data = p; |
1299 | } | |
1300 | ||
95c53908 | 1301 | p = strstr(data, "\nFrom "); |
f2561fda | 1302 | if (p) |
f035ab62 | 1303 | len = &p[1] - data; |
f2561fda | 1304 | |
f035ab62 MH |
1305 | strbuf_add(msg, data, len); |
1306 | *ofs += len; | |
a6080a0a | 1307 | return 1; |
f2561fda MM |
1308 | } |
1309 | ||
a4e7e317 GC |
1310 | static int git_imap_config(const char *var, const char *val, |
1311 | const struct config_context *ctx, void *cb) | |
f2561fda | 1312 | { |
ef7e1d0c | 1313 | |
50212361 NMC |
1314 | if (!strcmp("imap.sslverify", var)) |
1315 | server.ssl_verify = git_config_bool(var, val); | |
1316 | else if (!strcmp("imap.preformattedhtml", var)) | |
1317 | server.use_html = git_config_bool(var, val); | |
1318 | else if (!strcmp("imap.folder", var)) | |
1319 | return git_config_string(&server.folder, var, val); | |
1320 | else if (!strcmp("imap.user", var)) | |
1321 | return git_config_string(&server.user, var, val); | |
1322 | else if (!strcmp("imap.pass", var)) | |
1323 | return git_config_string(&server.pass, var, val); | |
1324 | else if (!strcmp("imap.tunnel", var)) | |
1325 | return git_config_string(&server.tunnel, var, val); | |
1326 | else if (!strcmp("imap.authmethod", var)) | |
1327 | return git_config_string(&server.auth_method, var, val); | |
1328 | else if (!strcmp("imap.port", var)) | |
8868b1eb | 1329 | server.port = git_config_int(var, val, ctx->kvi); |
50212361 | 1330 | else if (!strcmp("imap.host", var)) { |
ef7e1d0c | 1331 | if (!val) { |
92cecce0 | 1332 | return config_error_nonbool(var); |
ef7e1d0c TA |
1333 | } else { |
1334 | if (starts_with(val, "imap:")) | |
1335 | val += 5; | |
1336 | else if (starts_with(val, "imaps:")) { | |
1337 | val += 6; | |
1338 | server.use_ssl = 1; | |
1339 | } | |
1340 | if (starts_with(val, "//")) | |
1341 | val += 2; | |
1342 | server.host = xstrdup(val); | |
f2561fda | 1343 | } |
50212361 | 1344 | } else |
a4e7e317 | 1345 | return git_default_config(var, val, ctx, cb); |
ae9c606e | 1346 | |
50212361 | 1347 | return 0; |
f2561fda MM |
1348 | } |
1349 | ||
1e16b255 BR |
1350 | static int append_msgs_to_imap(struct imap_server_conf *server, |
1351 | struct strbuf* all_msgs, int total) | |
f2561fda | 1352 | { |
cbc60761 | 1353 | struct strbuf msg = STRBUF_INIT; |
fe47e1df | 1354 | struct imap_store *ctx = NULL; |
f2561fda MM |
1355 | int ofs = 0; |
1356 | int r; | |
1e16b255 BR |
1357 | int n = 0; |
1358 | ||
1359 | ctx = imap_open_store(server, server->folder); | |
1360 | if (!ctx) { | |
1361 | fprintf(stderr, "failed to open store\n"); | |
1362 | return 1; | |
1363 | } | |
1364 | ctx->name = server->folder; | |
1365 | ||
1366 | fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : ""); | |
1367 | while (1) { | |
1368 | unsigned percent = n * 100 / total; | |
1369 | ||
1370 | fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total); | |
1371 | ||
1372 | if (!split_msg(all_msgs, &msg, &ofs)) | |
1373 | break; | |
1374 | if (server->use_html) | |
1375 | wrap_in_html(&msg); | |
1376 | r = imap_store_msg(ctx, &msg); | |
1377 | if (r != DRV_OK) | |
1378 | break; | |
1379 | n++; | |
1380 | } | |
1381 | fprintf(stderr, "\n"); | |
1382 | ||
1383 | imap_close_store(ctx); | |
1384 | ||
1385 | return 0; | |
1386 | } | |
1387 | ||
1388 | #ifdef USE_CURL_FOR_IMAP_SEND | |
19079b3e | 1389 | static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred) |
1e16b255 BR |
1390 | { |
1391 | CURL *curl; | |
1392 | struct strbuf path = STRBUF_INIT; | |
77eac3f8 | 1393 | char *uri_encoded_folder; |
1e16b255 BR |
1394 | |
1395 | if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) | |
1396 | die("curl_global_init failed"); | |
1397 | ||
1398 | curl = curl_easy_init(); | |
1399 | ||
1400 | if (!curl) | |
1401 | die("curl_easy_init failed"); | |
1402 | ||
84d689a8 JK |
1403 | server_fill_credential(srvc, cred); |
1404 | curl_easy_setopt(curl, CURLOPT_USERNAME, srvc->user); | |
1405 | curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass); | |
1e16b255 | 1406 | |
84d689a8 JK |
1407 | strbuf_addstr(&path, srvc->use_ssl ? "imaps://" : "imap://"); |
1408 | strbuf_addstr(&path, srvc->host); | |
1e16b255 BR |
1409 | if (!path.len || path.buf[path.len - 1] != '/') |
1410 | strbuf_addch(&path, '/'); | |
77eac3f8 | 1411 | |
84d689a8 | 1412 | uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0); |
77eac3f8 NMC |
1413 | if (!uri_encoded_folder) |
1414 | die("failed to encode server folder"); | |
1415 | strbuf_addstr(&path, uri_encoded_folder); | |
1416 | curl_free(uri_encoded_folder); | |
1e16b255 BR |
1417 | |
1418 | curl_easy_setopt(curl, CURLOPT_URL, path.buf); | |
1419 | strbuf_release(&path); | |
84d689a8 | 1420 | curl_easy_setopt(curl, CURLOPT_PORT, srvc->port); |
1e16b255 | 1421 | |
84d689a8 | 1422 | if (srvc->auth_method) { |
e4ff3b67 | 1423 | #ifndef GIT_CURL_HAVE_CURLOPT_LOGIN_OPTIONS |
71d92575 JS |
1424 | warning("No LOGIN_OPTIONS support in this cURL version"); |
1425 | #else | |
1e16b255 BR |
1426 | struct strbuf auth = STRBUF_INIT; |
1427 | strbuf_addstr(&auth, "AUTH="); | |
84d689a8 | 1428 | strbuf_addstr(&auth, srvc->auth_method); |
1e16b255 BR |
1429 | curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, auth.buf); |
1430 | strbuf_release(&auth); | |
71d92575 | 1431 | #endif |
1e16b255 BR |
1432 | } |
1433 | ||
84d689a8 | 1434 | if (!srvc->use_ssl) |
230c09c0 | 1435 | curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY); |
1e16b255 | 1436 | |
84d689a8 JK |
1437 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, srvc->ssl_verify); |
1438 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, srvc->ssl_verify); | |
1e16b255 BR |
1439 | |
1440 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer); | |
1441 | ||
1442 | curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | |
1443 | ||
d47e55da | 1444 | if (0 < verbosity || getenv("GIT_CURL_VERBOSE")) |
7167a62b | 1445 | http_trace_curl_no_data(); |
73e57aaf | 1446 | setup_curl_trace(curl); |
1e16b255 BR |
1447 | |
1448 | return curl; | |
1449 | } | |
1450 | ||
1451 | static int curl_append_msgs_to_imap(struct imap_server_conf *server, | |
3b335762 NTND |
1452 | struct strbuf* all_msgs, int total) |
1453 | { | |
1e16b255 BR |
1454 | int ofs = 0; |
1455 | int n = 0; | |
1456 | struct buffer msgbuf = { STRBUF_INIT, 0 }; | |
1457 | CURL *curl; | |
1458 | CURLcode res = CURLE_OK; | |
19079b3e | 1459 | struct credential cred = CREDENTIAL_INIT; |
1e16b255 | 1460 | |
19079b3e | 1461 | curl = setup_curl(server, &cred); |
1e16b255 BR |
1462 | curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf); |
1463 | ||
1464 | fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : ""); | |
1465 | while (1) { | |
1466 | unsigned percent = n * 100 / total; | |
1467 | int prev_len; | |
1468 | ||
1469 | fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total); | |
1470 | ||
1471 | prev_len = msgbuf.buf.len; | |
1472 | if (!split_msg(all_msgs, &msgbuf.buf, &ofs)) | |
1473 | break; | |
1474 | if (server->use_html) | |
1475 | wrap_in_html(&msgbuf.buf); | |
1476 | lf_to_crlf(&msgbuf.buf); | |
1477 | ||
1478 | curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, | |
1479 | (curl_off_t)(msgbuf.buf.len-prev_len)); | |
1480 | ||
1481 | res = curl_easy_perform(curl); | |
1482 | ||
1483 | if(res != CURLE_OK) { | |
1484 | fprintf(stderr, "curl_easy_perform() failed: %s\n", | |
1485 | curl_easy_strerror(res)); | |
1486 | break; | |
1487 | } | |
1488 | ||
1489 | n++; | |
1490 | } | |
1491 | fprintf(stderr, "\n"); | |
1492 | ||
1493 | curl_easy_cleanup(curl); | |
1494 | curl_global_cleanup(); | |
1495 | ||
19079b3e NMC |
1496 | if (cred.username) { |
1497 | if (res == CURLE_OK) | |
1498 | credential_approve(&cred); | |
19079b3e | 1499 | else if (res == CURLE_LOGIN_DENIED) |
19079b3e NMC |
1500 | credential_reject(&cred); |
1501 | } | |
1502 | ||
1503 | credential_clear(&cred); | |
1504 | ||
200bc38b | 1505 | return res != CURLE_OK; |
1e16b255 BR |
1506 | } |
1507 | #endif | |
1508 | ||
3f2e2297 | 1509 | int cmd_main(int argc, const char **argv) |
1e16b255 BR |
1510 | { |
1511 | struct strbuf all_msgs = STRBUF_INIT; | |
1512 | int total; | |
a0406b94 | 1513 | int nongit_ok; |
f2561fda | 1514 | |
a0406b94 | 1515 | setup_git_directory_gently(&nongit_ok); |
50212361 | 1516 | git_config(git_imap_config, NULL); |
f2561fda | 1517 | |
f1a35295 BR |
1518 | argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0); |
1519 | ||
1520 | if (argc) | |
1521 | usage_with_options(imap_send_usage, imap_send_options); | |
1522 | ||
1e16b255 BR |
1523 | #ifndef USE_CURL_FOR_IMAP_SEND |
1524 | if (use_curl) { | |
dcd01ea1 | 1525 | warning("--curl not supported in this build"); |
1e16b255 BR |
1526 | use_curl = 0; |
1527 | } | |
dcd01ea1 KM |
1528 | #elif defined(NO_OPENSSL) |
1529 | if (!use_curl) { | |
1530 | warning("--no-curl not supported in this build"); | |
1531 | use_curl = 1; | |
1532 | } | |
1e16b255 BR |
1533 | #endif |
1534 | ||
684ec6c6 RS |
1535 | if (!server.port) |
1536 | server.port = server.use_ssl ? 993 : 143; | |
f2561fda | 1537 | |
39180571 | 1538 | if (!server.folder) { |
95c53908 | 1539 | fprintf(stderr, "no imap store specified\n"); |
f2561fda MM |
1540 | return 1; |
1541 | } | |
5b67b8e2 | 1542 | if (!server.host) { |
34b5cd1f | 1543 | if (!server.tunnel) { |
95c53908 | 1544 | fprintf(stderr, "no imap host specified\n"); |
34b5cd1f JK |
1545 | return 1; |
1546 | } | |
1547 | server.host = "tunnel"; | |
5b67b8e2 | 1548 | } |
f2561fda MM |
1549 | |
1550 | /* read the messages */ | |
351bca2d ÆAB |
1551 | if (strbuf_read(&all_msgs, 0, 0) < 0) { |
1552 | error_errno(_("could not read from stdin")); | |
6360bee4 MH |
1553 | return 1; |
1554 | } | |
1555 | ||
1556 | if (all_msgs.len == 0) { | |
9f1ad541 | 1557 | fprintf(stderr, "nothing to send\n"); |
f2561fda MM |
1558 | return 1; |
1559 | } | |
1560 | ||
95c53908 | 1561 | total = count_messages(&all_msgs); |
1cd88cc9 | 1562 | if (!total) { |
9f1ad541 | 1563 | fprintf(stderr, "no messages to send\n"); |
f2561fda MM |
1564 | return 1; |
1565 | } | |
1566 | ||
1567 | /* write it to the imap server */ | |
f035ab62 | 1568 | |
1e16b255 BR |
1569 | if (server.tunnel) |
1570 | return append_msgs_to_imap(&server, &all_msgs, total); | |
f2561fda | 1571 | |
1e16b255 BR |
1572 | #ifdef USE_CURL_FOR_IMAP_SEND |
1573 | if (use_curl) | |
1574 | return curl_append_msgs_to_imap(&server, &all_msgs, total); | |
1575 | #endif | |
f2561fda | 1576 | |
1e16b255 | 1577 | return append_msgs_to_imap(&server, &all_msgs, total); |
f2561fda | 1578 | } |