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