blob: 996651e4f80abd5fa2daf570f9d981934d328aeb [file] [log] [blame]
Mike McCormackf2561fd2006-03-10 14:32:50 +09001/*
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
Todd Zullinger48425792017-11-07 00:39:33 -050021 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Mike McCormackf2561fd2006-03-10 14:32:50 +090022 */
23
Elijah Newren15db4e72023-02-24 00:09:23 +000024#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070025#include "config.h"
Dan Albert791643a2014-04-28 20:00:04 -070026#include "credential.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -070027#include "exec-cmd.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000028#include "gettext.h"
Erik Faye-Lundc94d2dd2009-10-19 17:42:04 +020029#include "run-command.h"
Bernhard Reiterf1a35292014-11-05 15:29:21 +010030#include "parse-options.h"
Elijah Newrene38da482023-03-21 06:26:05 +000031#include "setup.h"
Christian Hesse3307f7d2023-05-17 09:06:32 +020032#include "strbuf.h"
Carlo Marcelo Arenas Belón5b52d9f2022-04-04 21:28:26 -070033#if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
Robert Shearman684ec6c2008-07-09 22:29:00 +010034typedef void *SSL;
35#endif
Bernhard Reiter1e16b252014-11-09 15:55:53 +010036#ifdef USE_CURL_FOR_IMAP_SEND
37#include "http.h"
38#endif
Mike McCormackf2561fd2006-03-10 14:32:50 +090039
Nicolas Morey-Chaisemartindbba42b2017-09-14 09:52:11 +020040#if defined(USE_CURL_FOR_IMAP_SEND)
41/* Always default to curl if it's available. */
Kyle J. McKaydcd01ea2015-03-07 21:13:55 -080042#define USE_CURL_DEFAULT 1
43#else
Nicolas Morey-Chaisemartindbba42b2017-09-14 09:52:11 +020044/* We don't have curl, so continue to use the historical implementation */
Kyle J. McKaydcd01ea2015-03-07 21:13:55 -080045#define USE_CURL_DEFAULT 0
46#endif
47
Bernhard Reiterf1a35292014-11-05 15:29:21 +010048static int verbosity;
Kyle J. McKaydcd01ea2015-03-07 21:13:55 -080049static int use_curl = USE_CURL_DEFAULT;
Bernhard Reiterf1a35292014-11-05 15:29:21 +010050
Bernhard Reiter1e16b252014-11-09 15:55:53 +010051static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] [--[no-]curl] < <mbox>", NULL };
Bernhard Reiterf1a35292014-11-05 15:29:21 +010052
53static struct option imap_send_options[] = {
54 OPT__VERBOSITY(&verbosity),
Bernhard Reiter1e16b252014-11-09 15:55:53 +010055 OPT_BOOL(0, "curl", &use_curl, "use libcurl to communicate with the IMAP server"),
Bernhard Reiterf1a35292014-11-05 15:29:21 +010056 OPT_END()
57};
Jonathan Nieder9a2861e2009-11-09 09:04:51 -060058
Erik Faye-Lundd23b1ec2009-10-19 17:42:05 +020059#undef DRV_OK
Mike McCormackf2561fd2006-03-10 14:32:50 +090060#define DRV_OK 0
61#define DRV_MSG_BAD -1
62#define DRV_BOX_BAD -2
63#define DRV_STORE_BAD -3
64
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080065__attribute__((format (printf, 1, 2)))
Robert Shearman95c53902008-07-09 22:29:01 +010066static void imap_info(const char *, ...);
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080067__attribute__((format (printf, 1, 2)))
Robert Shearman95c53902008-07-09 22:29:01 +010068static void imap_warn(const char *, ...);
Mike McCormackf2561fd2006-03-10 14:32:50 +090069
Robert Shearman95c53902008-07-09 22:29:01 +010070static char *next_arg(char **);
Mike McCormackf2561fd2006-03-10 14:32:50 +090071
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080072__attribute__((format (printf, 3, 4)))
Robert Shearman95c53902008-07-09 22:29:01 +010073static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
Mike McCormackf2561fd2006-03-10 14:32:50 +090074
Pierre Habouzit19247e52007-09-20 10:43:11 +020075static int nfvasprintf(char **strp, const char *fmt, va_list ap)
76{
77 int len;
78 char tmp[8192];
Mike McCormackf2561fd2006-03-10 14:32:50 +090079
Pierre Habouzit19247e52007-09-20 10:43:11 +020080 len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
81 if (len < 0)
Alexander Potashevd7530702009-01-04 21:38:41 +030082 die("Fatal: Out of memory");
Pierre Habouzit19247e52007-09-20 10:43:11 +020083 if (len >= sizeof(tmp))
Alexander Potashevd7530702009-01-04 21:38:41 +030084 die("imap command overflow!");
Pierre Habouzit19247e52007-09-20 10:43:11 +020085 *strp = xmemdupz(tmp, len);
86 return len;
87}
Mike McCormackf2561fd2006-03-10 14:32:50 +090088
Junio C Hamano9f1ad542008-07-09 16:37:38 -070089struct imap_server_conf {
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +010090 const char *name;
91 const char *tunnel;
92 const char *host;
Mike McCormackf2561fd2006-03-10 14:32:50 +090093 int port;
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +010094 const char *folder;
95 const char *user;
96 const char *pass;
Robert Shearman684ec6c2008-07-09 22:29:00 +010097 int use_ssl;
98 int ssl_verify;
Jeremy Whitec64d84f2009-02-12 08:58:12 -060099 int use_html;
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +0100100 const char *auth_method;
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800101};
Mike McCormackf2561fd2006-03-10 14:32:50 +0900102
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800103static struct imap_server_conf server = {
Ævar Arnfjörð Bjarmason518f7052022-02-24 10:32:56 +0100104 .ssl_verify = 1,
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700105};
Mike McCormackf2561fd2006-03-10 14:32:50 +0900106
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700107struct imap_socket {
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200108 int fd[2];
Robert Shearman684ec6c2008-07-09 22:29:00 +0100109 SSL *ssl;
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700110};
Mike McCormackf2561fd2006-03-10 14:32:50 +0900111
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700112struct imap_buffer {
113 struct imap_socket sock;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900114 int bytes;
115 int offset;
116 char buf[1024];
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700117};
Mike McCormackf2561fd2006-03-10 14:32:50 +0900118
119struct imap_cmd;
120
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700121struct imap {
Mike McCormackf2561fd2006-03-10 14:32:50 +0900122 int uidnext; /* from SELECT responses */
Mike McCormackf2561fd2006-03-10 14:32:50 +0900123 unsigned caps, rcaps; /* CAPABILITY results */
124 /* command queue */
125 int nexttag, num_in_progress, literal_pending;
126 struct imap_cmd *in_progress, **in_progress_append;
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700127 struct imap_buffer buf; /* this is BIG, so put it last */
128};
Mike McCormackf2561fd2006-03-10 14:32:50 +0900129
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700130struct imap_store {
Michael Haggerty636fd662013-01-15 09:06:31 +0100131 /* currently open mailbox */
132 const char *name; /* foreign! maybe preset? */
133 int uidvalidity;
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700134 struct imap *imap;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900135 const char *prefix;
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700136};
Mike McCormackf2561fd2006-03-10 14:32:50 +0900137
138struct imap_cmd_cb {
Jeff Kingec9e3582023-07-03 02:34:02 -0400139 int (*cont)(struct imap_store *ctx, const char *prompt);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900140 void *ctx;
141 char *data;
142 int dlen;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900143};
144
145struct imap_cmd {
146 struct imap_cmd *next;
147 struct imap_cmd_cb cb;
148 char *cmd;
149 int tag;
150};
151
152#define CAP(cap) (imap->caps & (1 << (cap)))
153
154enum CAPABILITY {
155 NOLOGIN = 0,
156 UIDPLUS,
157 LITERALPLUS,
158 NAMESPACE,
Robert Shearman684ec6c2008-07-09 22:29:00 +0100159 STARTTLS,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000160 AUTH_CRAM_MD5
Mike McCormackf2561fd2006-03-10 14:32:50 +0900161};
162
163static const char *cap_list[] = {
164 "LOGINDISABLED",
165 "UIDPLUS",
166 "LITERAL+",
167 "NAMESPACE",
Robert Shearman684ec6c2008-07-09 22:29:00 +0100168 "STARTTLS",
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800169 "AUTH=CRAM-MD5",
Mike McCormackf2561fd2006-03-10 14:32:50 +0900170};
171
172#define RESP_OK 0
173#define RESP_NO 1
174#define RESP_BAD 2
175
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700176static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900177
178
Robert Shearman684ec6c2008-07-09 22:29:00 +0100179#ifndef NO_OPENSSL
180static void ssl_socket_perror(const char *func)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900181{
Linus Torvalds2af202b2009-06-18 10:28:43 -0700182 fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), NULL));
Robert Shearman684ec6c2008-07-09 22:29:00 +0100183}
184#endif
185
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700186static void socket_perror(const char *func, struct imap_socket *sock, int ret)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900187{
Robert Shearman684ec6c2008-07-09 22:29:00 +0100188#ifndef NO_OPENSSL
189 if (sock->ssl) {
190 int sslerr = SSL_get_error(sock->ssl, ret);
191 switch (sslerr) {
192 case SSL_ERROR_NONE:
193 break;
194 case SSL_ERROR_SYSCALL:
195 perror("SSL_connect");
196 break;
197 default:
198 ssl_socket_perror("SSL_connect");
199 break;
200 }
201 } else
202#endif
203 {
204 if (ret < 0)
205 perror(func);
206 else
207 fprintf(stderr, "%s: unexpected EOF\n", func);
208 }
Jeff King2c3c3d82023-08-29 19:45:33 -0400209 /* mark as used to appease -Wunused-parameter with NO_OPENSSL */
210 (void)sock;
Robert Shearman684ec6c2008-07-09 22:29:00 +0100211}
212
Junio C Hamano1e1fe522013-02-15 12:32:19 -0800213#ifdef NO_OPENSSL
Jeff King2c3c3d82023-08-29 19:45:33 -0400214static int ssl_socket_connect(struct imap_socket *sock UNUSED,
215 int use_tls_only UNUSED,
216 int verify UNUSED)
Robert Shearman684ec6c2008-07-09 22:29:00 +0100217{
Robert Shearman684ec6c2008-07-09 22:29:00 +0100218 fprintf(stderr, "SSL requested but SSL support not compiled in\n");
219 return -1;
Junio C Hamano1e1fe522013-02-15 12:32:19 -0800220}
221
Robert Shearman684ec6c2008-07-09 22:29:00 +0100222#else
Junio C Hamano1e1fe522013-02-15 12:32:19 -0800223
Oswald Buddenhagenb62fb072013-02-15 12:50:35 -0800224static int host_matches(const char *host, const char *pattern)
225{
226 if (pattern[0] == '*' && pattern[1] == '.') {
227 pattern += 2;
228 if (!(host = strchr(host, '.')))
229 return 0;
230 host++;
231 }
232
233 return *host && *pattern && !strcasecmp(host, pattern);
234}
235
236static int verify_hostname(X509 *cert, const char *hostname)
237{
238 int len;
239 X509_NAME *subj;
240 char cname[1000];
Oswald Buddenhagene1747442013-02-15 12:59:53 -0800241 int i, found;
242 STACK_OF(GENERAL_NAME) *subj_alt_names;
243
244 /* try the DNS subjectAltNames */
245 found = 0;
246 if ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {
247 int num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);
248 for (i = 0; !found && i < num_subj_alt_names; i++) {
249 GENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);
250 if (subj_alt_name->type == GEN_DNS &&
251 strlen((const char *)subj_alt_name->d.ia5->data) == (size_t)subj_alt_name->d.ia5->length &&
252 host_matches(hostname, (const char *)(subj_alt_name->d.ia5->data)))
253 found = 1;
254 }
255 sk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);
256 }
257 if (found)
258 return 0;
Oswald Buddenhagenb62fb072013-02-15 12:50:35 -0800259
260 /* try the common name */
261 if (!(subj = X509_get_subject_name(cert)))
262 return error("cannot get certificate subject");
263 if ((len = X509_NAME_get_text_by_NID(subj, NID_commonName, cname, sizeof(cname))) < 0)
264 return error("cannot get certificate common name");
265 if (strlen(cname) == (size_t)len && host_matches(hostname, cname))
266 return 0;
267 return error("certificate owner '%s' does not match hostname '%s'",
268 cname, hostname);
269}
270
Junio C Hamano1e1fe522013-02-15 12:32:19 -0800271static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
272{
Vietor Liu1e380dd2009-10-31 14:36:03 +0800273#if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
274 const SSL_METHOD *meth;
275#else
Robert Shearman684ec6c2008-07-09 22:29:00 +0100276 SSL_METHOD *meth;
Vietor Liu1e380dd2009-10-31 14:36:03 +0800277#endif
Robert Shearman684ec6c2008-07-09 22:29:00 +0100278 SSL_CTX *ctx;
279 int ret;
Oswald Buddenhagenb62fb072013-02-15 12:50:35 -0800280 X509 *cert;
Robert Shearman684ec6c2008-07-09 22:29:00 +0100281
282 SSL_library_init();
283 SSL_load_error_strings();
284
Kazuki Yamaguchib51c0d42016-04-09 01:22:15 +0900285 meth = SSLv23_method();
Robert Shearman684ec6c2008-07-09 22:29:00 +0100286 if (!meth) {
287 ssl_socket_perror("SSLv23_method");
288 return -1;
289 }
290
291 ctx = SSL_CTX_new(meth);
Kazuki Yamaguchi6738a332016-04-09 01:22:14 +0900292 if (!ctx) {
293 ssl_socket_perror("SSL_CTX_new");
294 return -1;
295 }
Robert Shearman684ec6c2008-07-09 22:29:00 +0100296
Kazuki Yamaguchib51c0d42016-04-09 01:22:15 +0900297 if (use_tls_only)
298 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
Robert Shearman684ec6c2008-07-09 22:29:00 +0100299
300 if (verify)
301 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
302
303 if (!SSL_CTX_set_default_verify_paths(ctx)) {
304 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
305 return -1;
306 }
307 sock->ssl = SSL_new(ctx);
308 if (!sock->ssl) {
309 ssl_socket_perror("SSL_new");
310 return -1;
311 }
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200312 if (!SSL_set_rfd(sock->ssl, sock->fd[0])) {
313 ssl_socket_perror("SSL_set_rfd");
314 return -1;
315 }
316 if (!SSL_set_wfd(sock->ssl, sock->fd[1])) {
317 ssl_socket_perror("SSL_set_wfd");
Robert Shearman684ec6c2008-07-09 22:29:00 +0100318 return -1;
319 }
320
Junio C Hamano698a1ec2013-02-20 16:02:42 -0800321#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
322 /*
323 * SNI (RFC4366)
324 * OpenSSL does not document this function, but the implementation
325 * returns 1 on success, 0 on failure after calling SSLerr().
326 */
327 ret = SSL_set_tlsext_host_name(sock->ssl, server.host);
328 if (ret != 1)
329 warning("SSL_set_tlsext_host_name(%s) failed.", server.host);
330#endif
331
Robert Shearman684ec6c2008-07-09 22:29:00 +0100332 ret = SSL_connect(sock->ssl);
333 if (ret <= 0) {
334 socket_perror("SSL_connect", sock, ret);
335 return -1;
336 }
337
Oswald Buddenhagenb62fb072013-02-15 12:50:35 -0800338 if (verify) {
339 /* make sure the hostname matches that of the certificate */
340 cert = SSL_get_peer_certificate(sock->ssl);
341 if (!cert)
342 return error("unable to get peer certificate.");
343 if (verify_hostname(cert, server.host) < 0)
344 return -1;
345 }
346
Robert Shearman684ec6c2008-07-09 22:29:00 +0100347 return 0;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900348}
Junio C Hamano1e1fe522013-02-15 12:32:19 -0800349#endif
Mike McCormackf2561fd2006-03-10 14:32:50 +0900350
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700351static int socket_read(struct imap_socket *sock, char *buf, int len)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900352{
Robert Shearman684ec6c2008-07-09 22:29:00 +0100353 ssize_t n;
354#ifndef NO_OPENSSL
355 if (sock->ssl)
356 n = SSL_read(sock->ssl, buf, len);
357 else
358#endif
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200359 n = xread(sock->fd[0], buf, len);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900360 if (n <= 0) {
Robert Shearman95c53902008-07-09 22:29:01 +0100361 socket_perror("read", sock, n);
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200362 close(sock->fd[0]);
363 close(sock->fd[1]);
364 sock->fd[0] = sock->fd[1] = -1;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900365 }
366 return n;
367}
368
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700369static int socket_write(struct imap_socket *sock, const char *buf, int len)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900370{
Robert Shearman684ec6c2008-07-09 22:29:00 +0100371 int n;
372#ifndef NO_OPENSSL
373 if (sock->ssl)
374 n = SSL_write(sock->ssl, buf, len);
375 else
376#endif
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200377 n = write_in_full(sock->fd[1], buf, len);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900378 if (n != len) {
Robert Shearman95c53902008-07-09 22:29:01 +0100379 socket_perror("write", sock, n);
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200380 close(sock->fd[0]);
381 close(sock->fd[1]);
382 sock->fd[0] = sock->fd[1] = -1;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900383 }
384 return n;
385}
386
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700387static void socket_shutdown(struct imap_socket *sock)
Robert Shearman684ec6c2008-07-09 22:29:00 +0100388{
389#ifndef NO_OPENSSL
390 if (sock->ssl) {
391 SSL_shutdown(sock->ssl);
392 SSL_free(sock->ssl);
393 }
394#endif
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200395 close(sock->fd[0]);
396 close(sock->fd[1]);
Robert Shearman684ec6c2008-07-09 22:29:00 +0100397}
398
Mike McCormackf2561fd2006-03-10 14:32:50 +0900399/* simple line buffering */
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700400static int buffer_gets(struct imap_buffer *b, char **s)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900401{
402 int n;
403 int start = b->offset;
404
405 *s = b->buf + start;
406
407 for (;;) {
408 /* make sure we have enough data to read the \r\n sequence */
409 if (b->offset + 1 >= b->bytes) {
410 if (start) {
411 /* shift down used bytes */
412 *s = b->buf;
413
Robert Shearman95c53902008-07-09 22:29:01 +0100414 assert(start <= b->bytes);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900415 n = b->bytes - start;
416
417 if (n)
Edgar Toernig173a9cb2006-10-30 17:39:17 -0800418 memmove(b->buf, b->buf + start, n);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900419 b->offset -= start;
420 b->bytes = n;
421 start = 0;
422 }
423
Robert Shearman95c53902008-07-09 22:29:01 +0100424 n = socket_read(&b->sock, b->buf + b->bytes,
425 sizeof(b->buf) - b->bytes);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900426
427 if (n <= 0)
428 return -1;
429
430 b->bytes += n;
431 }
432
433 if (b->buf[b->offset] == '\r') {
Robert Shearman95c53902008-07-09 22:29:01 +0100434 assert(b->offset + 1 < b->bytes);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900435 if (b->buf[b->offset + 1] == '\n') {
436 b->buf[b->offset] = 0; /* terminate the string */
437 b->offset += 2; /* next line */
Bernhard Reiterf1a35292014-11-05 15:29:21 +0100438 if (0 < verbosity)
Robert Shearman95c53902008-07-09 22:29:01 +0100439 puts(*s);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900440 return 0;
441 }
442 }
443
444 b->offset++;
445 }
446 /* not reached */
447}
448
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200449__attribute__((format (printf, 1, 2)))
Robert Shearman95c53902008-07-09 22:29:01 +0100450static void imap_info(const char *msg, ...)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900451{
452 va_list va;
453
Bernhard Reiterf1a35292014-11-05 15:29:21 +0100454 if (0 <= verbosity) {
Robert Shearman95c53902008-07-09 22:29:01 +0100455 va_start(va, msg);
456 vprintf(msg, va);
457 va_end(va);
458 fflush(stdout);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900459 }
460}
461
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200462__attribute__((format (printf, 1, 2)))
Robert Shearman95c53902008-07-09 22:29:01 +0100463static void imap_warn(const char *msg, ...)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900464{
465 va_list va;
466
Bernhard Reiterf1a35292014-11-05 15:29:21 +0100467 if (-2 < verbosity) {
Robert Shearman95c53902008-07-09 22:29:01 +0100468 va_start(va, msg);
469 vfprintf(stderr, msg, va);
470 va_end(va);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900471 }
472}
473
Robert Shearman95c53902008-07-09 22:29:01 +0100474static char *next_arg(char **s)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900475{
476 char *ret;
477
478 if (!s || !*s)
Rene Scharfe5142db62006-04-02 13:13:01 +0200479 return NULL;
Robert Shearman95c53902008-07-09 22:29:01 +0100480 while (isspace((unsigned char) **s))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900481 (*s)++;
482 if (!**s) {
Rene Scharfe5142db62006-04-02 13:13:01 +0200483 *s = NULL;
484 return NULL;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900485 }
486 if (**s == '"') {
487 ++*s;
488 ret = *s;
Robert Shearman95c53902008-07-09 22:29:01 +0100489 *s = strchr(*s, '"');
Mike McCormackf2561fd2006-03-10 14:32:50 +0900490 } else {
491 ret = *s;
Robert Shearman95c53902008-07-09 22:29:01 +0100492 while (**s && !isspace((unsigned char) **s))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900493 (*s)++;
494 }
495 if (*s) {
496 if (**s)
497 *(*s)++ = 0;
498 if (!**s)
Rene Scharfe5142db62006-04-02 13:13:01 +0200499 *s = NULL;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900500 }
501 return ret;
502}
503
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200504__attribute__((format (printf, 3, 4)))
Robert Shearman95c53902008-07-09 22:29:01 +0100505static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900506{
507 int ret;
508 va_list va;
509
Robert Shearman95c53902008-07-09 22:29:01 +0100510 va_start(va, fmt);
511 if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200512 BUG("buffer too small. Please report a bug.");
Robert Shearman95c53902008-07-09 22:29:01 +0100513 va_end(va);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900514 return ret;
515}
516
Tony Finche0d8e302014-08-01 09:15:52 +0100517static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
518 struct imap_cmd_cb *cb,
519 const char *fmt, va_list ap)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900520{
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700521 struct imap *imap = ctx->imap;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900522 struct imap_cmd *cmd;
523 int n, bufl;
524 char buf[1024];
525
Robert Shearman95c53902008-07-09 22:29:01 +0100526 cmd = xmalloc(sizeof(struct imap_cmd));
527 nfvasprintf(&cmd->cmd, fmt, ap);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900528 cmd->tag = ++imap->nexttag;
529
530 if (cb)
531 cmd->cb = *cb;
532 else
Robert Shearman95c53902008-07-09 22:29:01 +0100533 memset(&cmd->cb, 0, sizeof(cmd->cb));
Mike McCormackf2561fd2006-03-10 14:32:50 +0900534
535 while (imap->literal_pending)
Robert Shearman95c53902008-07-09 22:29:01 +0100536 get_cmd_result(ctx, NULL);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900537
Ævar Arnfjörð Bjarmason1702b132010-08-07 18:09:45 -0500538 if (!cmd->cb.data)
539 bufl = nfsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
540 else
541 bufl = nfsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
542 cmd->tag, cmd->cmd, cmd->cb.dlen,
543 CAP(LITERALPLUS) ? "+" : "");
544
Bernhard Reiterf1a35292014-11-05 15:29:21 +0100545 if (0 < verbosity) {
Mike McCormackf2561fd2006-03-10 14:32:50 +0900546 if (imap->num_in_progress)
Robert Shearman95c53902008-07-09 22:29:01 +0100547 printf("(%d in progress) ", imap->num_in_progress);
René Scharfeba9b9e12014-08-30 18:14:24 +0200548 if (!starts_with(cmd->cmd, "LOGIN"))
Robert Shearman95c53902008-07-09 22:29:01 +0100549 printf(">>> %s", buf);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900550 else
Robert Shearman95c53902008-07-09 22:29:01 +0100551 printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900552 }
Robert Shearman95c53902008-07-09 22:29:01 +0100553 if (socket_write(&imap->buf.sock, buf, bufl) != bufl) {
554 free(cmd->cmd);
555 free(cmd);
Jim Meyering8e0f7002008-01-31 18:26:32 +0100556 if (cb)
Robert Shearman95c53902008-07-09 22:29:01 +0100557 free(cb->data);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900558 return NULL;
559 }
560 if (cmd->cb.data) {
561 if (CAP(LITERALPLUS)) {
Robert Shearman95c53902008-07-09 22:29:01 +0100562 n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);
563 free(cmd->cb.data);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900564 if (n != cmd->cb.dlen ||
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100565 socket_write(&imap->buf.sock, "\r\n", 2) != 2) {
Robert Shearman95c53902008-07-09 22:29:01 +0100566 free(cmd->cmd);
567 free(cmd);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900568 return NULL;
569 }
Rene Scharfe5142db62006-04-02 13:13:01 +0200570 cmd->cb.data = NULL;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900571 } else
572 imap->literal_pending = 1;
573 } else if (cmd->cb.cont)
574 imap->literal_pending = 1;
Rene Scharfe5142db62006-04-02 13:13:01 +0200575 cmd->next = NULL;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900576 *imap->in_progress_append = cmd;
577 imap->in_progress_append = &cmd->next;
578 imap->num_in_progress++;
579 return cmd;
580}
581
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -0800582__attribute__((format (printf, 3, 4)))
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700583static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb,
Robert Shearman95c53902008-07-09 22:29:01 +0100584 const char *fmt, ...)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900585{
586 va_list ap;
587 struct imap_cmd *cmdp;
588
Robert Shearman95c53902008-07-09 22:29:01 +0100589 va_start(ap, fmt);
Tony Finche0d8e302014-08-01 09:15:52 +0100590 cmdp = issue_imap_cmd(ctx, cb, fmt, ap);
Robert Shearman95c53902008-07-09 22:29:01 +0100591 va_end(ap);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900592 if (!cmdp)
593 return RESP_BAD;
594
Robert Shearman95c53902008-07-09 22:29:01 +0100595 return get_cmd_result(ctx, cmdp);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900596}
597
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -0800598__attribute__((format (printf, 3, 4)))
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700599static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb,
Robert Shearman95c53902008-07-09 22:29:01 +0100600 const char *fmt, ...)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900601{
602 va_list ap;
603 struct imap_cmd *cmdp;
604
Robert Shearman95c53902008-07-09 22:29:01 +0100605 va_start(ap, fmt);
Tony Finche0d8e302014-08-01 09:15:52 +0100606 cmdp = issue_imap_cmd(ctx, cb, fmt, ap);
Robert Shearman95c53902008-07-09 22:29:01 +0100607 va_end(ap);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900608 if (!cmdp)
609 return DRV_STORE_BAD;
610
Robert Shearman95c53902008-07-09 22:29:01 +0100611 switch (get_cmd_result(ctx, cmdp)) {
Mike McCormackf2561fd2006-03-10 14:32:50 +0900612 case RESP_BAD: return DRV_STORE_BAD;
613 case RESP_NO: return DRV_MSG_BAD;
614 default: return DRV_OK;
615 }
616}
617
Michael Haggerty3648b4d2013-01-15 09:06:27 +0100618static int skip_imap_list_l(char **sp, int level)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900619{
Michael Haggerty3648b4d2013-01-15 09:06:27 +0100620 char *s = *sp;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900621
622 for (;;) {
Robert Shearman95c53902008-07-09 22:29:01 +0100623 while (isspace((unsigned char)*s))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900624 s++;
625 if (level && *s == ')') {
626 s++;
627 break;
628 }
Mike McCormackf2561fd2006-03-10 14:32:50 +0900629 if (*s == '(') {
630 /* sublist */
631 s++;
Michael Haggerty3648b4d2013-01-15 09:06:27 +0100632 if (skip_imap_list_l(&s, level + 1))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900633 goto bail;
634 } else if (*s == '"') {
635 /* quoted string */
636 s++;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900637 for (; *s != '"'; s++)
638 if (!*s)
639 goto bail;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900640 s++;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900641 } else {
642 /* atom */
Robert Shearman95c53902008-07-09 22:29:01 +0100643 for (; *s && !isspace((unsigned char)*s); s++)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900644 if (level && *s == ')')
645 break;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900646 }
647
648 if (!level)
649 break;
650 if (!*s)
651 goto bail;
652 }
653 *sp = s;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900654 return 0;
655
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700656bail:
Mike McCormackf2561fd2006-03-10 14:32:50 +0900657 return -1;
658}
659
Michael Haggerty3648b4d2013-01-15 09:06:27 +0100660static void skip_list(char **sp)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900661{
Michael Haggerty3648b4d2013-01-15 09:06:27 +0100662 skip_imap_list_l(sp, 0);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900663}
664
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700665static void parse_capability(struct imap *imap, char *cmd)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900666{
667 char *arg;
668 unsigned i;
669
670 imap->caps = 0x80000000;
Robert Shearman95c53902008-07-09 22:29:01 +0100671 while ((arg = next_arg(&cmd)))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900672 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
Robert Shearman95c53902008-07-09 22:29:01 +0100673 if (!strcmp(cap_list[i], arg))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900674 imap->caps |= 1 << i;
675 imap->rcaps = imap->caps;
676}
677
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700678static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
Robert Shearman95c53902008-07-09 22:29:01 +0100679 char *s)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900680{
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700681 struct imap *imap = ctx->imap;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900682 char *arg, *p;
683
René Scharfe618ec812017-11-02 18:27:05 +0100684 if (!s || *s != '[')
Mike McCormackf2561fd2006-03-10 14:32:50 +0900685 return RESP_OK; /* no response code */
686 s++;
Robert Shearman95c53902008-07-09 22:29:01 +0100687 if (!(p = strchr(s, ']'))) {
688 fprintf(stderr, "IMAP error: malformed response code\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900689 return RESP_BAD;
690 }
691 *p++ = 0;
Robert Shearman95c53902008-07-09 22:29:01 +0100692 arg = next_arg(&s);
René Scharfef54c5bd2017-11-01 18:03:20 +0100693 if (!arg) {
694 fprintf(stderr, "IMAP error: empty response code\n");
695 return RESP_BAD;
696 }
Robert Shearman95c53902008-07-09 22:29:01 +0100697 if (!strcmp("UIDVALIDITY", arg)) {
Michael Haggerty636fd662013-01-15 09:06:31 +0100698 if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
Robert Shearman95c53902008-07-09 22:29:01 +0100699 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900700 return RESP_BAD;
701 }
Robert Shearman95c53902008-07-09 22:29:01 +0100702 } else if (!strcmp("UIDNEXT", arg)) {
703 if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
704 fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900705 return RESP_BAD;
706 }
Robert Shearman95c53902008-07-09 22:29:01 +0100707 } else if (!strcmp("CAPABILITY", arg)) {
708 parse_capability(imap, s);
709 } else if (!strcmp("ALERT", arg)) {
Mike McCormackf2561fd2006-03-10 14:32:50 +0900710 /* RFC2060 says that these messages MUST be displayed
711 * to the user
712 */
Robert Shearman95c53902008-07-09 22:29:01 +0100713 for (; isspace((unsigned char)*p); p++);
714 fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
715 } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
Michael Haggerty636fd662013-01-15 09:06:31 +0100716 if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) ||
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700717 !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
Robert Shearman95c53902008-07-09 22:29:01 +0100718 fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900719 return RESP_BAD;
720 }
721 }
722 return RESP_OK;
723}
724
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700725static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900726{
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700727 struct imap *imap = ctx->imap;
Tony Finche0d8e302014-08-01 09:15:52 +0100728 struct imap_cmd *cmdp, **pcmdp;
René Scharfef54c5bd2017-11-01 18:03:20 +0100729 char *cmd;
730 const char *arg, *arg1;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900731 int n, resp, resp2, tag;
732
733 for (;;) {
Robert Shearman95c53902008-07-09 22:29:01 +0100734 if (buffer_gets(&imap->buf, &cmd))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900735 return RESP_BAD;
736
Robert Shearman95c53902008-07-09 22:29:01 +0100737 arg = next_arg(&cmd);
René Scharfef54c5bd2017-11-01 18:03:20 +0100738 if (!arg) {
739 fprintf(stderr, "IMAP error: empty response\n");
740 return RESP_BAD;
741 }
Mike McCormackf2561fd2006-03-10 14:32:50 +0900742 if (*arg == '*') {
Robert Shearman95c53902008-07-09 22:29:01 +0100743 arg = next_arg(&cmd);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900744 if (!arg) {
Robert Shearman95c53902008-07-09 22:29:01 +0100745 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900746 return RESP_BAD;
747 }
748
Robert Shearman95c53902008-07-09 22:29:01 +0100749 if (!strcmp("NAMESPACE", arg)) {
Michael Haggerty3648b4d2013-01-15 09:06:27 +0100750 /* rfc2342 NAMESPACE response. */
751 skip_list(&cmd); /* Personal mailboxes */
752 skip_list(&cmd); /* Others' mailboxes */
753 skip_list(&cmd); /* Shared mailboxes */
Robert Shearman95c53902008-07-09 22:29:01 +0100754 } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
755 !strcmp("NO", arg) || !strcmp("BYE", arg)) {
756 if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900757 return resp;
Michael Haggerty1efee7f2013-01-15 09:06:24 +0100758 } else if (!strcmp("CAPABILITY", arg)) {
Robert Shearman95c53902008-07-09 22:29:01 +0100759 parse_capability(imap, cmd);
Michael Haggerty1efee7f2013-01-15 09:06:24 +0100760 } else if ((arg1 = next_arg(&cmd))) {
761 ; /*
762 * Unhandled response-data with at least two words.
763 * Ignore it.
764 *
765 * NEEDSWORK: Previously this case handled '<num> EXISTS'
766 * and '<num> RECENT' but as a probably-unintended side
767 * effect it ignores other unrecognized two-word
768 * responses. imap-send doesn't ever try to read
769 * messages or mailboxes these days, so consider
770 * eliminating this case.
771 */
Mike McCormackf2561fd2006-03-10 14:32:50 +0900772 } else {
Robert Shearman95c53902008-07-09 22:29:01 +0100773 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900774 return RESP_BAD;
775 }
776 } else if (!imap->in_progress) {
Robert Shearman95c53902008-07-09 22:29:01 +0100777 fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900778 return RESP_BAD;
779 } else if (*arg == '+') {
780 /* This can happen only with the last command underway, as
781 it enforces a round-trip. */
782 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
783 offsetof(struct imap_cmd, next));
784 if (cmdp->cb.data) {
Robert Shearman95c53902008-07-09 22:29:01 +0100785 n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000786 FREE_AND_NULL(cmdp->cb.data);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900787 if (n != (int)cmdp->cb.dlen)
788 return RESP_BAD;
789 } else if (cmdp->cb.cont) {
Jeff Kingec9e3582023-07-03 02:34:02 -0400790 if (cmdp->cb.cont(ctx, cmd))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900791 return RESP_BAD;
792 } else {
Robert Shearman95c53902008-07-09 22:29:01 +0100793 fprintf(stderr, "IMAP error: unexpected command continuation request\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900794 return RESP_BAD;
795 }
Robert Shearman95c53902008-07-09 22:29:01 +0100796 if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900797 return RESP_BAD;
798 if (!cmdp->cb.cont)
799 imap->literal_pending = 0;
800 if (!tcmd)
801 return DRV_OK;
802 } else {
Robert Shearman95c53902008-07-09 22:29:01 +0100803 tag = atoi(arg);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900804 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
805 if (cmdp->tag == tag)
806 goto gottag;
Robert Shearman95c53902008-07-09 22:29:01 +0100807 fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900808 return RESP_BAD;
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700809 gottag:
Mike McCormackf2561fd2006-03-10 14:32:50 +0900810 if (!(*pcmdp = cmdp->next))
811 imap->in_progress_append = pcmdp;
812 imap->num_in_progress--;
813 if (cmdp->cb.cont || cmdp->cb.data)
814 imap->literal_pending = 0;
Robert Shearman95c53902008-07-09 22:29:01 +0100815 arg = next_arg(&cmd);
René Scharfef54c5bd2017-11-01 18:03:20 +0100816 if (!arg)
817 arg = "";
Robert Shearman95c53902008-07-09 22:29:01 +0100818 if (!strcmp("OK", arg))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900819 resp = DRV_OK;
820 else {
Tony Finche0d8e302014-08-01 09:15:52 +0100821 if (!strcmp("NO", arg))
Mike McCormackf2561fd2006-03-10 14:32:50 +0900822 resp = RESP_NO;
Tony Finche0d8e302014-08-01 09:15:52 +0100823 else /*if (!strcmp("BAD", arg))*/
Mike McCormackf2561fd2006-03-10 14:32:50 +0900824 resp = RESP_BAD;
Robert Shearman95c53902008-07-09 22:29:01 +0100825 fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
René Scharfeba9b9e12014-08-30 18:14:24 +0200826 !starts_with(cmdp->cmd, "LOGIN") ?
Mike McCormackf2561fd2006-03-10 14:32:50 +0900827 cmdp->cmd : "LOGIN <user> <pass>",
828 arg, cmd ? cmd : "");
829 }
Robert Shearman95c53902008-07-09 22:29:01 +0100830 if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900831 resp = resp2;
Robert Shearman95c53902008-07-09 22:29:01 +0100832 free(cmdp->cb.data);
833 free(cmdp->cmd);
834 free(cmdp);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900835 if (!tcmd || tcmd == cmdp)
836 return resp;
837 }
838 }
839 /* not reached */
840}
841
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700842static void imap_close_server(struct imap_store *ictx)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900843{
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700844 struct imap *imap = ictx->imap;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900845
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200846 if (imap->buf.sock.fd[0] != -1) {
Robert Shearman95c53902008-07-09 22:29:01 +0100847 imap_exec(ictx, NULL, "LOGOUT");
848 socket_shutdown(&imap->buf.sock);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900849 }
Robert Shearman95c53902008-07-09 22:29:01 +0100850 free(imap);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900851}
852
Michael Haggertyfe47e1d2013-01-15 09:06:29 +0100853static void imap_close_store(struct imap_store *ctx)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900854{
Michael Haggertyfe47e1d2013-01-15 09:06:29 +0100855 imap_close_server(ctx);
Robert Shearman95c53902008-07-09 22:29:01 +0100856 free(ctx);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900857}
858
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800859#ifndef NO_OPENSSL
860
861/*
862 * hexchar() and cram() functions are based on the code from the isync
863 * project (http://isync.sf.net/).
864 */
865static char hexchar(unsigned int b)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900866{
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800867 return b < 10 ? '0' + b : 'a' + (b - 10);
868}
869
René Scharfe42c78a22017-07-08 12:35:35 +0200870#define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800871static char *cram(const char *challenge_64, const char *user, const char *pass)
872{
873 int i, resp_len, encoded_len, decoded_len;
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800874 unsigned char hash[16];
875 char hex[33];
876 char *response, *response_64, *challenge;
877
878 /*
879 * length of challenge_64 (i.e. base-64 encoded string) is a good
880 * enough upper bound for challenge (decoded result).
881 */
882 encoded_len = strlen(challenge_64);
883 challenge = xmalloc(encoded_len);
884 decoded_len = EVP_DecodeBlock((unsigned char *)challenge,
885 (unsigned char *)challenge_64, encoded_len);
886 if (decoded_len < 0)
887 die("invalid challenge %s", challenge_64);
Kazuki Yamaguchi1ed2c7b2016-04-09 01:22:13 +0900888 if (!HMAC(EVP_md5(), pass, strlen(pass), (unsigned char *)challenge, decoded_len, hash, NULL))
889 die("HMAC error");
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800890
891 hex[32] = 0;
892 for (i = 0; i < 16; i++) {
893 hex[2 * i] = hexchar((hash[i] >> 4) & 0xf);
894 hex[2 * i + 1] = hexchar(hash[i] & 0xf);
895 }
896
897 /* response: "<user> <digest in hex>" */
Jeff King75faa452015-09-24 17:07:03 -0400898 response = xstrfmt("%s %s", user, hex);
Kazuki Yamaguchieb94ee72016-04-08 23:02:30 +0900899 resp_len = strlen(response);
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800900
Jeff King3733e692016-02-22 17:44:28 -0500901 response_64 = xmallocz(ENCODED_SIZE(resp_len));
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800902 encoded_len = EVP_EncodeBlock((unsigned char *)response_64,
903 (unsigned char *)response, resp_len);
904 if (encoded_len < 0)
905 die("EVP_EncodeBlock error");
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800906 return (char *)response_64;
907}
908
909#else
910
Jeff King2c3c3d82023-08-29 19:45:33 -0400911static char *cram(const char *challenge_64 UNUSED,
912 const char *user UNUSED,
913 const char *pass UNUSED)
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800914{
915 die("If you want to use CRAM-MD5 authenticate method, "
916 "you have to build git-imap-send with OpenSSL library.");
917}
918
919#endif
920
Jeff Kingec9e3582023-07-03 02:34:02 -0400921static int auth_cram_md5(struct imap_store *ctx, const char *prompt)
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800922{
923 int ret;
924 char *response;
925
926 response = cram(prompt, server.user, server.pass);
927
928 ret = socket_write(&ctx->imap->buf.sock, response, strlen(response));
929 if (ret != strlen(response))
Pete Wyckoff82247e92012-04-29 20:28:45 -0400930 return error("IMAP error: sending response failed");
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -0800931
932 free(response);
933
934 return 0;
935}
936
Nicolas Morey-Chaisemartin690307f2017-09-14 09:52:02 +0200937static void server_fill_credential(struct imap_server_conf *srvc, struct credential *cred)
938{
939 if (srvc->user && srvc->pass)
940 return;
941
942 cred->protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
943 cred->host = xstrdup(srvc->host);
944
945 cred->username = xstrdup_or_null(srvc->user);
946 cred->password = xstrdup_or_null(srvc->pass);
947
948 credential_fill(cred);
949
950 if (!srvc->user)
951 srvc->user = xstrdup(cred->username);
952 if (!srvc->pass)
953 srvc->pass = xstrdup(cred->password);
954}
955
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +0100956static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const char *folder)
Mike McCormackf2561fd2006-03-10 14:32:50 +0900957{
Dan Albert791643a2014-04-28 20:00:04 -0700958 struct credential cred = CREDENTIAL_INIT;
Junio C Hamano9f1ad542008-07-09 16:37:38 -0700959 struct imap_store *ctx;
960 struct imap *imap;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900961 char *arg, *rsp;
Erik Faye-Lundc94d2dd2009-10-19 17:42:04 +0200962 int s = -1, preauth;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900963
René Scharfeca56dad2021-03-13 17:17:22 +0100964 CALLOC_ARRAY(ctx, 1);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900965
René Scharfeca56dad2021-03-13 17:17:22 +0100966 ctx->imap = CALLOC_ARRAY(imap, 1);
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +0200967 imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900968 imap->in_progress_append = &imap->in_progress;
969
970 /* open connection to IMAP server */
971
972 if (srvc->tunnel) {
René Scharfed3180272014-08-19 21:09:35 +0200973 struct child_process tunnel = CHILD_PROCESS_INIT;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900974
Robert Shearman95c53902008-07-09 22:29:01 +0100975 imap_info("Starting tunnel '%s'... ", srvc->tunnel);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900976
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400977 strvec_push(&tunnel.args, srvc->tunnel);
Jeff Kingac0ba182009-12-30 05:53:57 -0500978 tunnel.use_shell = 1;
Erik Faye-Lundc94d2dd2009-10-19 17:42:04 +0200979 tunnel.in = -1;
980 tunnel.out = -1;
981 if (start_command(&tunnel))
Bernhard Reiterf9dc5d62014-08-13 19:30:43 +0200982 die("cannot start proxy %s", srvc->tunnel);
Mike McCormackf2561fd2006-03-10 14:32:50 +0900983
Erik Faye-Lundc94d2dd2009-10-19 17:42:04 +0200984 imap->buf.sock.fd[0] = tunnel.out;
985 imap->buf.sock.fd[1] = tunnel.in;
Mike McCormackf2561fd2006-03-10 14:32:50 +0900986
Robert Shearman95c53902008-07-09 22:29:01 +0100987 imap_info("ok\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +0900988 } else {
Benjamin Kramer94ad2432009-05-25 21:13:54 +0200989#ifndef NO_IPV6
990 struct addrinfo hints, *ai0, *ai;
991 int gai;
992 char portstr[6];
Mike McCormackf2561fd2006-03-10 14:32:50 +0900993
Jeff King1a168e52017-03-28 15:46:56 -0400994 xsnprintf(portstr, sizeof(portstr), "%d", srvc->port);
Benjamin Kramer94ad2432009-05-25 21:13:54 +0200995
996 memset(&hints, 0, sizeof(hints));
997 hints.ai_socktype = SOCK_STREAM;
998 hints.ai_protocol = IPPROTO_TCP;
999
1000 imap_info("Resolving %s... ", srvc->host);
1001 gai = getaddrinfo(srvc->host, portstr, &hints, &ai);
1002 if (gai) {
1003 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai));
Mike McCormackf2561fd2006-03-10 14:32:50 +09001004 goto bail;
1005 }
Benjamin Kramer94ad2432009-05-25 21:13:54 +02001006 imap_info("ok\n");
1007
1008 for (ai0 = ai; ai; ai = ai->ai_next) {
1009 char addr[NI_MAXHOST];
1010
1011 s = socket(ai->ai_family, ai->ai_socktype,
1012 ai->ai_protocol);
1013 if (s < 0)
1014 continue;
1015
1016 getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1017 sizeof(addr), NULL, 0, NI_NUMERICHOST);
1018 imap_info("Connecting to [%s]:%s... ", addr, portstr);
1019
1020 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1021 close(s);
1022 s = -1;
1023 perror("connect");
1024 continue;
1025 }
1026
1027 break;
1028 }
1029 freeaddrinfo(ai0);
1030#else /* NO_IPV6 */
1031 struct hostent *he;
1032 struct sockaddr_in addr;
1033
Robert Shearman95c53902008-07-09 22:29:01 +01001034 memset(&addr, 0, sizeof(addr));
1035 addr.sin_port = htons(srvc->port);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001036 addr.sin_family = AF_INET;
1037
Robert Shearman95c53902008-07-09 22:29:01 +01001038 imap_info("Resolving %s... ", srvc->host);
1039 he = gethostbyname(srvc->host);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001040 if (!he) {
Robert Shearman95c53902008-07-09 22:29:01 +01001041 perror("gethostbyname");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001042 goto bail;
1043 }
Robert Shearman95c53902008-07-09 22:29:01 +01001044 imap_info("ok\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001045
1046 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
1047
Robert Shearman95c53902008-07-09 22:29:01 +01001048 s = socket(PF_INET, SOCK_STREAM, 0);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001049
Robert Shearman95c53902008-07-09 22:29:01 +01001050 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1051 if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1052 close(s);
Benjamin Kramer94ad2432009-05-25 21:13:54 +02001053 s = -1;
Robert Shearman95c53902008-07-09 22:29:01 +01001054 perror("connect");
Benjamin Kramer94ad2432009-05-25 21:13:54 +02001055 }
1056#endif
1057 if (s < 0) {
1058 fputs("Error: unable to connect to server.\n", stderr);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001059 goto bail;
1060 }
Mike McCormackf2561fd2006-03-10 14:32:50 +09001061
Erik Faye-Lund7a7796e2009-10-19 17:42:03 +02001062 imap->buf.sock.fd[0] = s;
1063 imap->buf.sock.fd[1] = dup(s);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001064
Robert Shearman684ec6c2008-07-09 22:29:00 +01001065 if (srvc->use_ssl &&
1066 ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1067 close(s);
1068 goto bail;
1069 }
Robert Shearman95c53902008-07-09 22:29:01 +01001070 imap_info("ok\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001071 }
1072
1073 /* read the greeting string */
Robert Shearman95c53902008-07-09 22:29:01 +01001074 if (buffer_gets(&imap->buf, &rsp)) {
1075 fprintf(stderr, "IMAP error: no greeting response\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001076 goto bail;
1077 }
Robert Shearman95c53902008-07-09 22:29:01 +01001078 arg = next_arg(&rsp);
1079 if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) {
1080 fprintf(stderr, "IMAP error: invalid greeting response\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001081 goto bail;
1082 }
1083 preauth = 0;
Robert Shearman95c53902008-07-09 22:29:01 +01001084 if (!strcmp("PREAUTH", arg))
Mike McCormackf2561fd2006-03-10 14:32:50 +09001085 preauth = 1;
Robert Shearman95c53902008-07-09 22:29:01 +01001086 else if (strcmp("OK", arg) != 0) {
1087 fprintf(stderr, "IMAP error: unknown greeting response\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001088 goto bail;
1089 }
Robert Shearman95c53902008-07-09 22:29:01 +01001090 parse_response_code(ctx, NULL, rsp);
1091 if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001092 goto bail;
1093
1094 if (!preauth) {
Robert Shearman684ec6c2008-07-09 22:29:00 +01001095#ifndef NO_OPENSSL
1096 if (!srvc->use_ssl && CAP(STARTTLS)) {
Ramsay Jonesd27da382011-04-07 19:24:57 +01001097 if (imap_exec(ctx, NULL, "STARTTLS") != RESP_OK)
Robert Shearman684ec6c2008-07-09 22:29:00 +01001098 goto bail;
1099 if (ssl_socket_connect(&imap->buf.sock, 1,
1100 srvc->ssl_verify))
1101 goto bail;
1102 /* capabilities may have changed, so get the new capabilities */
Ramsay Jonesd27da382011-04-07 19:24:57 +01001103 if (imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
Robert Shearman684ec6c2008-07-09 22:29:00 +01001104 goto bail;
1105 }
1106#endif
Robert Shearman95c53902008-07-09 22:29:01 +01001107 imap_info("Logging in...\n");
Nicolas Morey-Chaisemartin690307f2017-09-14 09:52:02 +02001108 server_fill_credential(srvc, &cred);
Dan Albert791643a2014-04-28 20:00:04 -07001109
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -08001110 if (srvc->auth_method) {
1111 struct imap_cmd_cb cb;
1112
1113 if (!strcmp(srvc->auth_method, "CRAM-MD5")) {
1114 if (!CAP(AUTH_CRAM_MD5)) {
Nguyễn Thái Ngọc Duy6d1fbf82019-02-11 16:40:11 +07001115 fprintf(stderr, "You specified "
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -08001116 "CRAM-MD5 as authentication method, "
1117 "but %s doesn't support it.\n", srvc->host);
1118 goto bail;
1119 }
1120 /* CRAM-MD5 */
1121
1122 memset(&cb, 0, sizeof(cb));
1123 cb.cont = auth_cram_md5;
1124 if (imap_exec(ctx, &cb, "AUTHENTICATE CRAM-MD5") != RESP_OK) {
1125 fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n");
1126 goto bail;
1127 }
1128 } else {
1129 fprintf(stderr, "Unknown authentication method:%s\n", srvc->host);
1130 goto bail;
1131 }
1132 } else {
Kazuki Yamaguchi6c50a572016-04-08 23:02:24 +09001133 if (CAP(NOLOGIN)) {
1134 fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n",
1135 srvc->user, srvc->host);
1136 goto bail;
1137 }
Chris Webb10439d82010-03-27 15:00:19 +00001138 if (!imap->buf.sock.ssl)
1139 imap_warn("*** IMAP Warning *** Password is being "
1140 "sent in the clear\n");
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -08001141 if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) {
1142 fprintf(stderr, "IMAP error: LOGIN failed\n");
1143 goto bail;
1144 }
Mike McCormackf2561fd2006-03-10 14:32:50 +09001145 }
1146 } /* !preauth */
1147
Dan Albert791643a2014-04-28 20:00:04 -07001148 if (cred.username)
1149 credential_approve(&cred);
1150 credential_clear(&cred);
1151
Tony Finche0d8e302014-08-01 09:15:52 +01001152 /* check the target mailbox exists */
1153 ctx->name = folder;
1154 switch (imap_exec(ctx, NULL, "EXAMINE \"%s\"", ctx->name)) {
1155 case RESP_OK:
1156 /* ok */
1157 break;
1158 case RESP_BAD:
1159 fprintf(stderr, "IMAP error: could not check mailbox\n");
1160 goto out;
1161 case RESP_NO:
1162 if (imap_exec(ctx, NULL, "CREATE \"%s\"", ctx->name) == RESP_OK) {
1163 imap_info("Created missing mailbox\n");
1164 } else {
1165 fprintf(stderr, "IMAP error: could not create missing mailbox\n");
1166 goto out;
1167 }
1168 break;
1169 }
1170
Mike McCormackf2561fd2006-03-10 14:32:50 +09001171 ctx->prefix = "";
Michael Haggertyfe47e1d2013-01-15 09:06:29 +01001172 return ctx;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001173
Junio C Hamano9f1ad542008-07-09 16:37:38 -07001174bail:
Dan Albert791643a2014-04-28 20:00:04 -07001175 if (cred.username)
1176 credential_reject(&cred);
1177 credential_clear(&cred);
1178
Tony Finche0d8e302014-08-01 09:15:52 +01001179 out:
Michael Haggertyfe47e1d2013-01-15 09:06:29 +01001180 imap_close_store(ctx);
Rene Scharfe5142db62006-04-02 13:13:01 +02001181 return NULL;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001182}
1183
Michael Haggerty36910312013-01-15 09:06:32 +01001184/*
1185 * Insert CR characters as necessary in *msg to ensure that every LF
1186 * character in *msg is preceded by a CR.
1187 */
Michael Haggertyf035ab62012-11-25 12:08:39 +01001188static void lf_to_crlf(struct strbuf *msg)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001189{
Brandon Williams59256312018-02-14 10:59:43 -08001190 char *new_msg;
Michael Haggerty36910312013-01-15 09:06:32 +01001191 size_t i, j;
1192 char lastc;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001193
Brandon Williams59256312018-02-14 10:59:43 -08001194 /* First pass: tally, in j, the size of the new_msg string: */
Michael Haggerty36910312013-01-15 09:06:32 +01001195 for (i = j = 0, lastc = '\0'; i < msg->len; i++) {
1196 if (msg->buf[i] == '\n' && lastc != '\r')
1197 j++; /* a CR will need to be added here */
1198 lastc = msg->buf[i];
1199 j++;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001200 }
Mike McCormackf2561fd2006-03-10 14:32:50 +09001201
Brandon Williams59256312018-02-14 10:59:43 -08001202 new_msg = xmallocz(j);
Michael Haggerty36910312013-01-15 09:06:32 +01001203
1204 /*
Brandon Williams59256312018-02-14 10:59:43 -08001205 * Second pass: write the new_msg string. Note that this loop is
Michael Haggerty36910312013-01-15 09:06:32 +01001206 * otherwise identical to the first pass.
1207 */
1208 for (i = j = 0, lastc = '\0'; i < msg->len; i++) {
1209 if (msg->buf[i] == '\n' && lastc != '\r')
Brandon Williams59256312018-02-14 10:59:43 -08001210 new_msg[j++] = '\r';
1211 lastc = new_msg[j++] = msg->buf[i];
Mike McCormackf2561fd2006-03-10 14:32:50 +09001212 }
Brandon Williams59256312018-02-14 10:59:43 -08001213 strbuf_attach(msg, new_msg, j, j + 1);
Hitoshi Mitake67d17632010-02-12 20:36:12 +09001214}
Mike McCormackf2561fd2006-03-10 14:32:50 +09001215
Michael Haggertyf035ab62012-11-25 12:08:39 +01001216/*
1217 * Store msg to IMAP. Also detach and free the data from msg->data,
1218 * leaving msg->data empty.
1219 */
Michael Haggertyfe47e1d2013-01-15 09:06:29 +01001220static int imap_store_msg(struct imap_store *ctx, struct strbuf *msg)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001221{
Junio C Hamano9f1ad542008-07-09 16:37:38 -07001222 struct imap *imap = ctx->imap;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001223 struct imap_cmd_cb cb;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001224 const char *prefix, *box;
Michael Haggerty719125c2013-01-15 09:06:19 +01001225 int ret;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001226
Michael Haggertycbc60762013-01-15 09:06:20 +01001227 lf_to_crlf(msg);
Robert Shearman95c53902008-07-09 22:29:01 +01001228 memset(&cb, 0, sizeof(cb));
Mike McCormackf2561fd2006-03-10 14:32:50 +09001229
Michael Haggertycbc60762013-01-15 09:06:20 +01001230 cb.dlen = msg->len;
1231 cb.data = strbuf_detach(msg, NULL);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001232
Michael Haggerty636fd662013-01-15 09:06:31 +01001233 box = ctx->name;
Jeff King3a7cba92009-10-19 17:42:02 +02001234 prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
Michael Haggerty719125c2013-01-15 09:06:19 +01001235 ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" ", prefix, box);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001236 imap->caps = imap->rcaps;
1237 if (ret != DRV_OK)
1238 return ret;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001239
1240 return DRV_OK;
1241}
1242
Michael Haggertyf035ab62012-11-25 12:08:39 +01001243static void wrap_in_html(struct strbuf *msg)
Jeremy Whitec64d84f2009-02-12 08:58:12 -06001244{
1245 struct strbuf buf = STRBUF_INIT;
Jeremy Whitec64d84f2009-02-12 08:58:12 -06001246 static char *content_type = "Content-Type: text/html;\n";
1247 static char *pre_open = "<pre>\n";
1248 static char *pre_close = "</pre>\n";
Michael Haggerty118a68f2012-11-25 12:08:41 +01001249 const char *body = strstr(msg->buf, "\n\n");
Jeremy Whitec64d84f2009-02-12 08:58:12 -06001250
Michael Haggerty118a68f2012-11-25 12:08:41 +01001251 if (!body)
1252 return; /* Headers but no body; no wrapping needed */
1253
1254 body += 2;
1255
1256 strbuf_add(&buf, msg->buf, body - msg->buf - 1);
1257 strbuf_addstr(&buf, content_type);
1258 strbuf_addch(&buf, '\n');
1259 strbuf_addstr(&buf, pre_open);
1260 strbuf_addstr_xml_quoted(&buf, body);
Jeremy Whitec64d84f2009-02-12 08:58:12 -06001261 strbuf_addstr(&buf, pre_close);
Michael Haggerty118a68f2012-11-25 12:08:41 +01001262
Michael Haggertyf035ab62012-11-25 12:08:39 +01001263 strbuf_release(msg);
1264 *msg = buf;
Jeremy Whitec64d84f2009-02-12 08:58:12 -06001265}
1266
Michael Haggerty3a34e622012-11-25 12:08:37 +01001267static int count_messages(struct strbuf *all_msgs)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001268{
1269 int count = 0;
Michael Haggerty3a34e622012-11-25 12:08:37 +01001270 char *p = all_msgs->buf;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001271
1272 while (1) {
Christian Couder59556542013-11-30 21:55:40 +01001273 if (starts_with(p, "From ")) {
Ramkumar Ramachandra4916c8f2010-03-22 23:37:52 +05301274 p = strstr(p+5, "\nFrom: ");
1275 if (!p) break;
1276 p = strstr(p+7, "\nDate: ");
1277 if (!p) break;
1278 p = strstr(p+7, "\nSubject: ");
1279 if (!p) break;
1280 p += 10;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001281 count++;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001282 }
Robert Shearman95c53902008-07-09 22:29:01 +01001283 p = strstr(p+5, "\nFrom ");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001284 if (!p)
1285 break;
1286 p++;
1287 }
1288 return count;
1289}
1290
Michael Haggertyf035ab62012-11-25 12:08:39 +01001291/*
1292 * Copy the next message from all_msgs, starting at offset *ofs, to
1293 * msg. Update *ofs to the start of the following message. Return
1294 * true iff a message was successfully copied.
1295 */
1296static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001297{
1298 char *p, *data;
Michael Haggertyf035ab62012-11-25 12:08:39 +01001299 size_t len;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001300
Mike McCormackf2561fd2006-03-10 14:32:50 +09001301 if (*ofs >= all_msgs->len)
1302 return 0;
1303
Michael Haggerty3a34e622012-11-25 12:08:37 +01001304 data = &all_msgs->buf[*ofs];
Michael Haggertyf035ab62012-11-25 12:08:39 +01001305 len = all_msgs->len - *ofs;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001306
Christian Couder59556542013-11-30 21:55:40 +01001307 if (len < 5 || !starts_with(data, "From "))
Mike McCormackf2561fd2006-03-10 14:32:50 +09001308 return 0;
1309
Robert Shearman95c53902008-07-09 22:29:01 +01001310 p = strchr(data, '\n');
Markus Amslere0b08302006-10-13 00:19:35 +02001311 if (p) {
Michael Haggertyf035ab62012-11-25 12:08:39 +01001312 p++;
1313 len -= p - data;
1314 *ofs += p - data;
Markus Amslere0b08302006-10-13 00:19:35 +02001315 data = p;
1316 }
1317
Robert Shearman95c53902008-07-09 22:29:01 +01001318 p = strstr(data, "\nFrom ");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001319 if (p)
Michael Haggertyf035ab62012-11-25 12:08:39 +01001320 len = &p[1] - data;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001321
Michael Haggertyf035ab62012-11-25 12:08:39 +01001322 strbuf_add(msg, data, len);
1323 *ofs += len;
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001324 return 1;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001325}
1326
Glen Chooa4e7e312023-06-28 19:26:22 +00001327static int git_imap_config(const char *var, const char *val,
1328 const struct config_context *ctx, void *cb)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001329{
Junio C Hamano3c17c342008-02-11 12:04:00 -08001330
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +01001331 if (!strcmp("imap.sslverify", var))
1332 server.ssl_verify = git_config_bool(var, val);
1333 else if (!strcmp("imap.preformattedhtml", var))
1334 server.use_html = git_config_bool(var, val);
1335 else if (!strcmp("imap.folder", var))
1336 return git_config_string(&server.folder, var, val);
1337 else if (!strcmp("imap.user", var))
1338 return git_config_string(&server.user, var, val);
1339 else if (!strcmp("imap.pass", var))
1340 return git_config_string(&server.pass, var, val);
1341 else if (!strcmp("imap.tunnel", var))
1342 return git_config_string(&server.tunnel, var, val);
1343 else if (!strcmp("imap.authmethod", var))
1344 return git_config_string(&server.auth_method, var, val);
1345 else if (!strcmp("imap.port", var))
Glen Choo8868b1e2023-06-28 19:26:27 +00001346 server.port = git_config_int(var, val, ctx->kvi);
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +01001347 else if (!strcmp("imap.host", var)) {
Tanay Abhraef7e1d02014-08-07 09:21:24 -07001348 if (!val) {
1349 git_die_config("imap.host", "Missing value for 'imap.host'");
1350 } else {
1351 if (starts_with(val, "imap:"))
1352 val += 5;
1353 else if (starts_with(val, "imaps:")) {
1354 val += 6;
1355 server.use_ssl = 1;
1356 }
1357 if (starts_with(val, "//"))
1358 val += 2;
1359 server.host = xstrdup(val);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001360 }
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +01001361 } else
Glen Chooa4e7e312023-06-28 19:26:22 +00001362 return git_default_config(var, val, ctx, cb);
Hitoshi Mitakeae9c6062010-02-15 22:34:07 -08001363
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +01001364 return 0;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001365}
1366
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001367static int append_msgs_to_imap(struct imap_server_conf *server,
1368 struct strbuf* all_msgs, int total)
Mike McCormackf2561fd2006-03-10 14:32:50 +09001369{
Michael Haggertycbc60762013-01-15 09:06:20 +01001370 struct strbuf msg = STRBUF_INIT;
Michael Haggertyfe47e1d2013-01-15 09:06:29 +01001371 struct imap_store *ctx = NULL;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001372 int ofs = 0;
1373 int r;
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001374 int n = 0;
1375
1376 ctx = imap_open_store(server, server->folder);
1377 if (!ctx) {
1378 fprintf(stderr, "failed to open store\n");
1379 return 1;
1380 }
1381 ctx->name = server->folder;
1382
1383 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1384 while (1) {
1385 unsigned percent = n * 100 / total;
1386
1387 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1388
1389 if (!split_msg(all_msgs, &msg, &ofs))
1390 break;
1391 if (server->use_html)
1392 wrap_in_html(&msg);
1393 r = imap_store_msg(ctx, &msg);
1394 if (r != DRV_OK)
1395 break;
1396 n++;
1397 }
1398 fprintf(stderr, "\n");
1399
1400 imap_close_store(ctx);
1401
1402 return 0;
1403}
1404
1405#ifdef USE_CURL_FOR_IMAP_SEND
Nicolas Morey-Chaisemartin19079b32017-09-14 09:52:06 +02001406static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001407{
1408 CURL *curl;
1409 struct strbuf path = STRBUF_INIT;
Nicolas Morey-Chaisemartin77eac3f2017-12-19 00:41:13 +05301410 char *uri_encoded_folder;
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001411
1412 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1413 die("curl_global_init failed");
1414
1415 curl = curl_easy_init();
1416
1417 if (!curl)
1418 die("curl_easy_init failed");
1419
Jeff King84d689a2023-07-03 02:33:30 -04001420 server_fill_credential(srvc, cred);
1421 curl_easy_setopt(curl, CURLOPT_USERNAME, srvc->user);
1422 curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001423
Jeff King84d689a2023-07-03 02:33:30 -04001424 strbuf_addstr(&path, srvc->use_ssl ? "imaps://" : "imap://");
1425 strbuf_addstr(&path, srvc->host);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001426 if (!path.len || path.buf[path.len - 1] != '/')
1427 strbuf_addch(&path, '/');
Nicolas Morey-Chaisemartin77eac3f2017-12-19 00:41:13 +05301428
Jeff King84d689a2023-07-03 02:33:30 -04001429 uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0);
Nicolas Morey-Chaisemartin77eac3f2017-12-19 00:41:13 +05301430 if (!uri_encoded_folder)
1431 die("failed to encode server folder");
1432 strbuf_addstr(&path, uri_encoded_folder);
1433 curl_free(uri_encoded_folder);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001434
1435 curl_easy_setopt(curl, CURLOPT_URL, path.buf);
1436 strbuf_release(&path);
Jeff King84d689a2023-07-03 02:33:30 -04001437 curl_easy_setopt(curl, CURLOPT_PORT, srvc->port);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001438
Jeff King84d689a2023-07-03 02:33:30 -04001439 if (srvc->auth_method) {
Ævar Arnfjörð Bjarmasone4ff3b62021-09-13 16:51:28 +02001440#ifndef GIT_CURL_HAVE_CURLOPT_LOGIN_OPTIONS
Johannes Schindelin71d92572015-10-26 14:14:58 +01001441 warning("No LOGIN_OPTIONS support in this cURL version");
1442#else
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001443 struct strbuf auth = STRBUF_INIT;
1444 strbuf_addstr(&auth, "AUTH=");
Jeff King84d689a2023-07-03 02:33:30 -04001445 strbuf_addstr(&auth, srvc->auth_method);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001446 curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, auth.buf);
1447 strbuf_release(&auth);
Johannes Schindelin71d92572015-10-26 14:14:58 +01001448#endif
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001449 }
1450
Jeff King84d689a2023-07-03 02:33:30 -04001451 if (!srvc->use_ssl)
Kyle J. McKay230c09c2015-01-06 03:20:37 -08001452 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001453
Jeff King84d689a2023-07-03 02:33:30 -04001454 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, srvc->ssl_verify);
1455 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, srvc->ssl_verify);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001456
1457 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
1458
1459 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
1460
Kyle J. McKayd47e55d2015-01-06 03:00:55 -08001461 if (0 < verbosity || getenv("GIT_CURL_VERBOSE"))
Jonathan Tan7167a622020-05-11 10:43:10 -07001462 http_trace_curl_no_data();
Elia Pinto73e57aa2016-05-23 13:44:03 +00001463 setup_curl_trace(curl);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001464
1465 return curl;
1466}
1467
1468static int curl_append_msgs_to_imap(struct imap_server_conf *server,
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001469 struct strbuf* all_msgs, int total)
1470{
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001471 int ofs = 0;
1472 int n = 0;
1473 struct buffer msgbuf = { STRBUF_INIT, 0 };
1474 CURL *curl;
1475 CURLcode res = CURLE_OK;
Nicolas Morey-Chaisemartin19079b32017-09-14 09:52:06 +02001476 struct credential cred = CREDENTIAL_INIT;
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001477
Nicolas Morey-Chaisemartin19079b32017-09-14 09:52:06 +02001478 curl = setup_curl(server, &cred);
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001479 curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
1480
1481 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1482 while (1) {
1483 unsigned percent = n * 100 / total;
1484 int prev_len;
1485
1486 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1487
1488 prev_len = msgbuf.buf.len;
1489 if (!split_msg(all_msgs, &msgbuf.buf, &ofs))
1490 break;
1491 if (server->use_html)
1492 wrap_in_html(&msgbuf.buf);
1493 lf_to_crlf(&msgbuf.buf);
1494
1495 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
1496 (curl_off_t)(msgbuf.buf.len-prev_len));
1497
1498 res = curl_easy_perform(curl);
1499
1500 if(res != CURLE_OK) {
1501 fprintf(stderr, "curl_easy_perform() failed: %s\n",
1502 curl_easy_strerror(res));
1503 break;
1504 }
1505
1506 n++;
1507 }
1508 fprintf(stderr, "\n");
1509
1510 curl_easy_cleanup(curl);
1511 curl_global_cleanup();
1512
Nicolas Morey-Chaisemartin19079b32017-09-14 09:52:06 +02001513 if (cred.username) {
1514 if (res == CURLE_OK)
1515 credential_approve(&cred);
Nicolas Morey-Chaisemartin19079b32017-09-14 09:52:06 +02001516 else if (res == CURLE_LOGIN_DENIED)
Nicolas Morey-Chaisemartin19079b32017-09-14 09:52:06 +02001517 credential_reject(&cred);
1518 }
1519
1520 credential_clear(&cred);
1521
Nicolas Morey-Chaisemartin200bc382017-09-14 09:51:57 +02001522 return res != CURLE_OK;
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001523}
1524#endif
1525
Jeff King3f2e2292016-07-01 01:58:58 -04001526int cmd_main(int argc, const char **argv)
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001527{
1528 struct strbuf all_msgs = STRBUF_INIT;
1529 int total;
Robert Shearmana0406b92008-07-09 22:28:59 +01001530 int nongit_ok;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001531
Robert Shearmana0406b92008-07-09 22:28:59 +01001532 setup_git_directory_gently(&nongit_ok);
Nicolas Morey-Chaisemartin50212362020-12-01 08:38:16 +01001533 git_config(git_imap_config, NULL);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001534
Bernhard Reiterf1a35292014-11-05 15:29:21 +01001535 argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0);
1536
1537 if (argc)
1538 usage_with_options(imap_send_usage, imap_send_options);
1539
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001540#ifndef USE_CURL_FOR_IMAP_SEND
1541 if (use_curl) {
Kyle J. McKaydcd01ea2015-03-07 21:13:55 -08001542 warning("--curl not supported in this build");
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001543 use_curl = 0;
1544 }
Kyle J. McKaydcd01ea2015-03-07 21:13:55 -08001545#elif defined(NO_OPENSSL)
1546 if (!use_curl) {
1547 warning("--no-curl not supported in this build");
1548 use_curl = 1;
1549 }
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001550#endif
1551
Robert Shearman684ec6c2008-07-09 22:29:00 +01001552 if (!server.port)
1553 server.port = server.use_ssl ? 993 : 143;
Mike McCormackf2561fd2006-03-10 14:32:50 +09001554
Bernhard Reiter39180572014-08-19 23:27:11 +02001555 if (!server.folder) {
Robert Shearman95c53902008-07-09 22:29:01 +01001556 fprintf(stderr, "no imap store specified\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001557 return 1;
1558 }
Gerrit Pape5b67b8e2008-03-26 18:05:17 +00001559 if (!server.host) {
Jeff King34b5cd12008-04-22 06:41:47 -04001560 if (!server.tunnel) {
Robert Shearman95c53902008-07-09 22:29:01 +01001561 fprintf(stderr, "no imap host specified\n");
Jeff King34b5cd12008-04-22 06:41:47 -04001562 return 1;
1563 }
1564 server.host = "tunnel";
Gerrit Pape5b67b8e2008-03-26 18:05:17 +00001565 }
Mike McCormackf2561fd2006-03-10 14:32:50 +09001566
1567 /* read the messages */
Ævar Arnfjörð Bjarmason351bca22021-07-07 11:05:24 +02001568 if (strbuf_read(&all_msgs, 0, 0) < 0) {
1569 error_errno(_("could not read from stdin"));
Michael Haggerty6360bee2012-11-25 12:08:38 +01001570 return 1;
1571 }
1572
1573 if (all_msgs.len == 0) {
Junio C Hamano9f1ad542008-07-09 16:37:38 -07001574 fprintf(stderr, "nothing to send\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001575 return 1;
1576 }
1577
Robert Shearman95c53902008-07-09 22:29:01 +01001578 total = count_messages(&all_msgs);
Mike McCormack1cd88cc2006-04-05 23:22:52 +09001579 if (!total) {
Junio C Hamano9f1ad542008-07-09 16:37:38 -07001580 fprintf(stderr, "no messages to send\n");
Mike McCormackf2561fd2006-03-10 14:32:50 +09001581 return 1;
1582 }
1583
1584 /* write it to the imap server */
Mike McCormackf2561fd2006-03-10 14:32:50 +09001585
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001586 if (server.tunnel)
1587 return append_msgs_to_imap(&server, &all_msgs, total);
Michael Haggertyf035ab62012-11-25 12:08:39 +01001588
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001589#ifdef USE_CURL_FOR_IMAP_SEND
1590 if (use_curl)
1591 return curl_append_msgs_to_imap(&server, &all_msgs, total);
1592#endif
Mike McCormackf2561fd2006-03-10 14:32:50 +09001593
Bernhard Reiter1e16b252014-11-09 15:55:53 +01001594 return append_msgs_to_imap(&server, &all_msgs, total);
Mike McCormackf2561fd2006-03-10 14:32:50 +09001595}