]>
git.ipfire.org Git - thirdparty/git.git/blob - imap-send.c
f5a656ac71dc2db06e087f4bfce486418724284e
2 * git-imap-send - drops patches into an imap Drafts folder
3 * derived from isync/mbsync - mailbox synchronizer
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
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.
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.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 #define USE_THE_REPOSITORY_VARIABLE
25 #define DISABLE_SIGN_COMPARE_WARNINGS
27 #include "git-compat-util.h"
30 #include "credential.h"
32 #include "run-command.h"
33 #include "parse-options.h"
36 #ifdef USE_CURL_FOR_IMAP_SEND
40 #if defined(USE_CURL_FOR_IMAP_SEND)
41 /* Always default to curl if it's available. */
42 #define USE_CURL_DEFAULT 1
44 /* We don't have curl, so continue to use the historical implementation */
45 #define USE_CURL_DEFAULT 0
49 static int list_folders
;
50 static int use_curl
= USE_CURL_DEFAULT
;
51 static char *opt_folder
;
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",
59 static struct option imap_send_options
[] = {
60 OPT__VERBOSITY(&verbosity
),
61 OPT_BOOL(0, "curl", &use_curl
, "use libcurl to communicate with the IMAP server"),
62 OPT_STRING('f', "folder", &opt_folder
, "folder", "specify the IMAP folder"),
63 OPT_BOOL(0, "list", &list_folders
, "list all folders on the IMAP server"),
69 #define DRV_MSG_BAD -1
70 #define DRV_BOX_BAD -2
71 #define DRV_STORE_BAD -3
73 __attribute__((format (printf
, 1, 2)))
74 static void imap_info(const char *, ...);
75 __attribute__((format (printf
, 1, 2)))
76 static void imap_warn(const char *, ...);
78 static char *next_arg(char **);
80 struct imap_server_conf
{
95 #if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
103 struct imap_socket sock
;
112 int uidnext
; /* from SELECT responses */
113 unsigned caps
, rcaps
; /* CAPABILITY results */
115 int nexttag
, num_in_progress
, literal_pending
;
116 struct imap_cmd
*in_progress
, **in_progress_append
;
117 struct imap_buffer buf
; /* this is BIG, so put it last */
121 const struct imap_server_conf
*cfg
;
122 /* currently open mailbox */
123 const char *name
; /* foreign! maybe preset? */
130 int (*cont
)(struct imap_store
*ctx
, const char *prompt
);
137 struct imap_cmd
*next
;
138 struct imap_cmd_cb cb
;
143 #define CAP(cap) (imap->caps & (1 << (cap)))
157 static const char *cap_list
[] = {
173 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
);
177 static void ssl_socket_perror(const char *func
)
179 fprintf(stderr
, "%s: %s\n", func
, ERR_error_string(ERR_get_error(), NULL
));
183 static void socket_perror(const char *func
, struct imap_socket
*sock
, int ret
)
187 int sslerr
= SSL_get_error(sock
->ssl
, ret
);
191 case SSL_ERROR_SYSCALL
:
192 perror("SSL_connect");
195 ssl_socket_perror("SSL_connect");
204 fprintf(stderr
, "%s: unexpected EOF\n", func
);
206 /* mark as used to appease -Wunused-parameter with NO_OPENSSL */
211 static int ssl_socket_connect(struct imap_socket
*sock UNUSED
,
212 const struct imap_server_conf
*cfg UNUSED
,
213 int use_tls_only UNUSED
)
215 fprintf(stderr
, "SSL requested, but SSL support is not compiled in\n");
221 static int host_matches(const char *host
, const char *pattern
)
223 if (pattern
[0] == '*' && pattern
[1] == '.') {
225 if (!(host
= strchr(host
, '.')))
230 return *host
&& *pattern
&& !strcasecmp(host
, pattern
);
233 static int verify_hostname(X509
*cert
, const char *hostname
)
239 STACK_OF(GENERAL_NAME
) *subj_alt_names
;
241 /* try the DNS subjectAltNames */
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
)))
252 sk_GENERAL_NAME_pop_free(subj_alt_names
, GENERAL_NAME_free
);
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
))
264 return error("certificate owner '%s' does not match hostname '%s'",
268 static int ssl_socket_connect(struct imap_socket
*sock
,
269 const struct imap_server_conf
*cfg
,
272 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
273 const SSL_METHOD
*meth
;
282 SSL_load_error_strings();
284 meth
= SSLv23_method();
286 ssl_socket_perror("SSLv23_method");
290 ctx
= SSL_CTX_new(meth
);
292 ssl_socket_perror("SSL_CTX_new");
297 SSL_CTX_set_options(ctx
, SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
);
300 SSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, NULL
);
302 if (!SSL_CTX_set_default_verify_paths(ctx
)) {
303 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
306 sock
->ssl
= SSL_new(ctx
);
308 ssl_socket_perror("SSL_new");
311 if (!SSL_set_rfd(sock
->ssl
, sock
->fd
[0])) {
312 ssl_socket_perror("SSL_set_rfd");
315 if (!SSL_set_wfd(sock
->ssl
, sock
->fd
[1])) {
316 ssl_socket_perror("SSL_set_wfd");
320 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
323 * OpenSSL does not document this function, but the implementation
324 * returns 1 on success, 0 on failure after calling SSLerr().
326 ret
= SSL_set_tlsext_host_name(sock
->ssl
, cfg
->host
);
328 warning("SSL_set_tlsext_host_name(%s) failed.", cfg
->host
);
331 ret
= SSL_connect(sock
->ssl
);
333 socket_perror("SSL_connect", sock
, ret
);
337 if (cfg
->ssl_verify
) {
338 /* make sure the hostname matches that of the certificate */
339 cert
= SSL_get_peer_certificate(sock
->ssl
);
341 return error("unable to get peer certificate.");
342 if (SSL_get_verify_result(sock
->ssl
) != X509_V_OK
)
343 return error("unable to verify peer certificate");
344 if (verify_hostname(cert
, cfg
->host
) < 0)
352 static int socket_read(struct imap_socket
*sock
, char *buf
, int len
)
357 n
= SSL_read(sock
->ssl
, buf
, len
);
360 n
= xread(sock
->fd
[0], buf
, len
);
362 socket_perror("read", sock
, n
);
365 sock
->fd
[0] = sock
->fd
[1] = -1;
370 static int socket_write(struct imap_socket
*sock
, const char *buf
, int len
)
375 n
= SSL_write(sock
->ssl
, buf
, len
);
378 n
= write_in_full(sock
->fd
[1], buf
, len
);
380 socket_perror("write", sock
, n
);
383 sock
->fd
[0] = sock
->fd
[1] = -1;
388 static void socket_shutdown(struct imap_socket
*sock
)
392 SSL_shutdown(sock
->ssl
);
400 /* simple line buffering */
401 static int buffer_gets(struct imap_buffer
*b
, char **s
)
404 int start
= b
->offset
;
409 /* make sure we have enough data to read the \r\n sequence */
410 if (b
->offset
+ 1 >= b
->bytes
) {
412 /* shift down used bytes */
415 assert(start
<= b
->bytes
);
416 n
= b
->bytes
- start
;
419 memmove(b
->buf
, b
->buf
+ start
, n
);
425 n
= socket_read(&b
->sock
, b
->buf
+ b
->bytes
,
426 sizeof(b
->buf
) - b
->bytes
);
434 if (b
->buf
[b
->offset
] == '\r') {
435 assert(b
->offset
+ 1 < b
->bytes
);
436 if (b
->buf
[b
->offset
+ 1] == '\n') {
437 b
->buf
[b
->offset
] = 0; /* terminate the string */
438 b
->offset
+= 2; /* next line */
439 if ((0 < verbosity
) || (list_folders
&& strstr(*s
, "* LIST")))
450 __attribute__((format (printf
, 1, 2)))
451 static void imap_info(const char *msg
, ...)
455 if (0 <= verbosity
) {
463 __attribute__((format (printf
, 1, 2)))
464 static void imap_warn(const char *msg
, ...)
468 if (-2 < verbosity
) {
470 vfprintf(stderr
, msg
, va
);
475 static char *next_arg(char **s
)
481 while (isspace((unsigned char) **s
))
490 *s
= strchr(*s
, '"');
493 while (**s
&& !isspace((unsigned char) **s
))
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
)
509 struct imap
*imap
= ctx
->imap
;
510 struct imap_cmd
*cmd
;
512 struct strbuf buf
= STRBUF_INIT
;
514 cmd
= xmalloc(sizeof(struct imap_cmd
));
515 cmd
->cmd
= xstrvfmt(fmt
, ap
);
516 cmd
->tag
= ++imap
->nexttag
;
521 memset(&cmd
->cb
, 0, sizeof(cmd
->cb
));
523 while (imap
->literal_pending
)
524 get_cmd_result(ctx
, NULL
);
527 strbuf_addf(&buf
, "%d %s\r\n", cmd
->tag
, cmd
->cmd
);
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!");
535 if (imap
->num_in_progress
)
536 printf("(%d in progress) ", imap
->num_in_progress
);
537 if (!starts_with(cmd
->cmd
, "LOGIN"))
538 printf(">>> %s", buf
.buf
);
540 printf(">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
542 if (socket_write(&imap
->buf
.sock
, buf
.buf
, buf
.len
) != buf
.len
) {
547 strbuf_release(&buf
);
550 strbuf_release(&buf
);
552 if (CAP(LITERALPLUS
)) {
553 n
= socket_write(&imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
555 if (n
!= cmd
->cb
.dlen
||
556 socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2) {
563 imap
->literal_pending
= 1;
564 } else if (cmd
->cb
.cont
)
565 imap
->literal_pending
= 1;
567 *imap
->in_progress_append
= cmd
;
568 imap
->in_progress_append
= &cmd
->next
;
569 imap
->num_in_progress
++;
573 __attribute__((format (printf
, 3, 4)))
574 static int imap_exec(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
575 const char *fmt
, ...)
578 struct imap_cmd
*cmdp
;
581 cmdp
= issue_imap_cmd(ctx
, cb
, fmt
, ap
);
586 return get_cmd_result(ctx
, cmdp
);
589 __attribute__((format (printf
, 3, 4)))
590 static int imap_exec_m(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
591 const char *fmt
, ...)
594 struct imap_cmd
*cmdp
;
597 cmdp
= issue_imap_cmd(ctx
, cb
, fmt
, ap
);
600 return DRV_STORE_BAD
;
602 switch (get_cmd_result(ctx
, cmdp
)) {
603 case RESP_BAD
: return DRV_STORE_BAD
;
604 case RESP_NO
: return DRV_MSG_BAD
;
605 default: return DRV_OK
;
609 static int skip_imap_list_l(char **sp
, int level
)
614 while (isspace((unsigned char)*s
))
616 if (level
&& *s
== ')') {
623 if (skip_imap_list_l(&s
, level
+ 1))
625 } else if (*s
== '"') {
628 for (; *s
!= '"'; s
++)
634 for (; *s
&& !isspace((unsigned char)*s
); s
++)
635 if (level
&& *s
== ')')
651 static void skip_list(char **sp
)
653 skip_imap_list_l(sp
, 0);
656 static void parse_capability(struct imap
*imap
, char *cmd
)
661 imap
->caps
= 0x80000000;
662 while ((arg
= next_arg(&cmd
)))
663 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
664 if (!strcmp(cap_list
[i
], arg
))
665 imap
->caps
|= 1 << i
;
666 imap
->rcaps
= imap
->caps
;
669 static int parse_response_code(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
672 struct imap
*imap
= ctx
->imap
;
676 return RESP_OK
; /* no response code */
678 if (!(p
= strchr(s
, ']'))) {
679 fprintf(stderr
, "IMAP error: malformed response code\n");
685 fprintf(stderr
, "IMAP error: empty response code\n");
688 if (!strcmp("UIDVALIDITY", arg
)) {
689 if (!(arg
= next_arg(&s
)) || strtol_i(arg
, 10, &ctx
->uidvalidity
) || !ctx
->uidvalidity
) {
690 fprintf(stderr
, "IMAP error: malformed UIDVALIDITY status\n");
693 } else if (!strcmp("UIDNEXT", arg
)) {
694 if (!(arg
= next_arg(&s
)) || strtol_i(arg
, 10, &imap
->uidnext
) || !imap
->uidnext
) {
695 fprintf(stderr
, "IMAP error: malformed NEXTUID status\n");
698 } else if (!strcmp("CAPABILITY", arg
)) {
699 parse_capability(imap
, s
);
700 } else if (!strcmp("ALERT", arg
)) {
701 /* RFC2060 says that these messages MUST be displayed
704 for (; isspace((unsigned char)*p
); p
++);
705 fprintf(stderr
, "*** IMAP ALERT *** %s\n", p
);
706 } else if (cb
&& cb
->ctx
&& !strcmp("APPENDUID", arg
)) {
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
) {
709 fprintf(stderr
, "IMAP error: malformed APPENDUID status\n");
716 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
)
718 struct imap
*imap
= ctx
->imap
;
719 struct imap_cmd
*cmdp
, **pcmdp
;
721 const char *arg
, *arg1
;
722 int n
, resp
, resp2
, tag
;
725 if (buffer_gets(&imap
->buf
, &cmd
))
728 arg
= next_arg(&cmd
);
730 fprintf(stderr
, "IMAP error: empty response\n");
734 arg
= next_arg(&cmd
);
736 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
740 if (!strcmp("NAMESPACE", arg
)) {
741 /* rfc2342 NAMESPACE response. */
742 skip_list(&cmd
); /* Personal mailboxes */
743 skip_list(&cmd
); /* Others' mailboxes */
744 skip_list(&cmd
); /* Shared mailboxes */
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
)
749 } else if (!strcmp("CAPABILITY", arg
)) {
750 parse_capability(imap
, cmd
);
751 } else if ((arg1
= next_arg(&cmd
))) {
753 * Unhandled response-data with at least two words.
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.
764 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
767 } else if (!imap
->in_progress
) {
768 fprintf(stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "");
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
));
776 n
= socket_write(&imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
777 FREE_AND_NULL(cmdp
->cb
.data
);
778 if (n
!= (int)cmdp
->cb
.dlen
)
780 } else if (cmdp
->cb
.cont
) {
781 if (cmdp
->cb
.cont(ctx
, cmd
))
784 fprintf(stderr
, "IMAP error: unexpected command continuation request\n");
787 if (socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2)
790 imap
->literal_pending
= 0;
794 if (strtol_i(arg
, 10, &tag
)) {
795 fprintf(stderr
, "IMAP error: malformed tag %s\n", arg
);
798 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
799 if (cmdp
->tag
== tag
)
801 fprintf(stderr
, "IMAP error: unexpected tag %s\n", arg
);
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;
809 arg
= next_arg(&cmd
);
812 if (!strcmp("OK", arg
))
815 if (!strcmp("NO", arg
))
817 else /*if (!strcmp("BAD", arg))*/
819 fprintf(stderr
, "IMAP command '%s' returned response (%s) - %s\n",
820 !starts_with(cmdp
->cmd
, "LOGIN") ?
821 cmdp
->cmd
: "LOGIN <user> <pass>",
822 arg
, cmd
? cmd
: "");
824 if ((resp2
= parse_response_code(ctx
, &cmdp
->cb
, cmd
)) > resp
)
829 if (!tcmd
|| tcmd
== cmdp
)
836 static void imap_close_server(struct imap_store
*ictx
)
838 struct imap
*imap
= ictx
->imap
;
840 if (imap
->buf
.sock
.fd
[0] != -1) {
841 imap_exec(ictx
, NULL
, "LOGOUT");
842 socket_shutdown(&imap
->buf
.sock
);
847 static void imap_close_store(struct imap_store
*ctx
)
849 imap_close_server(ctx
);
856 * hexchar() and cram() functions are based on the code from the isync
857 * project (https://isync.sourceforge.io/).
859 static char hexchar(unsigned int b
)
861 return b
< 10 ? '0' + b
: 'a' + (b
- 10);
864 #define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
865 static char *plain_base64(const char *user
, const char *pass
)
867 struct strbuf raw
= STRBUF_INIT
;
872 * Compose the PLAIN string
874 * The username and password are combined to one string and base64 encoded.
877 * The method has been described in RFC4616.
879 * https://datatracker.ietf.org/doc/html/rfc4616
881 strbuf_addch(&raw
, '\0');
882 strbuf_addstr(&raw
, user
);
883 strbuf_addch(&raw
, '\0');
884 strbuf_addstr(&raw
, pass
);
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
);
897 static char *cram(const char *challenge_64
, const char *user
, const char *pass
)
899 int i
, resp_len
, encoded_len
, decoded_len
;
900 unsigned char hash
[16];
902 char *response
, *response_64
, *challenge
;
905 * length of challenge_64 (i.e. base-64 encoded string) is a good
906 * enough upper bound for challenge (decoded result).
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
);
913 die("invalid challenge %s", challenge_64
);
914 if (!HMAC(EVP_md5(), pass
, strlen(pass
), (unsigned char *)challenge
, decoded_len
, hash
, NULL
))
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);
923 /* response: "<user> <digest in hex>" */
924 response
= xstrfmt("%s %s", user
, hex
);
925 resp_len
= strlen(response
);
927 response_64
= xmallocz(ENCODED_SIZE(resp_len
));
928 encoded_len
= EVP_EncodeBlock((unsigned char *)response_64
,
929 (unsigned char *)response
, resp_len
);
931 die("EVP_EncodeBlock error");
932 return (char *)response_64
;
935 static char *oauthbearer_base64(const char *user
, const char *access_token
)
941 * Compose the OAUTHBEARER string
943 * "n,a=" {User} ",^Ahost=" {Host} "^Aport=" {Port} "^Aauth=Bearer " {Access Token} "^A^A
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} "`
949 * The second part are key value pairs containing host, port and auth as
950 * described in RFC7628.
952 * https://datatracker.ietf.org/doc/html/rfc5801
953 * https://datatracker.ietf.org/doc/html/rfc7628
955 raw
= xstrfmt("n,a=%s,\001auth=Bearer %s\001\001", user
, access_token
);
958 b64
= xmallocz(ENCODED_SIZE(strlen(raw
)));
959 b64_len
= EVP_EncodeBlock((unsigned char *)b64
, (unsigned char *)raw
, strlen(raw
));
969 static char *xoauth2_base64(const char *user
, const char *access_token
)
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
979 raw
= xstrfmt("user=%s\001auth=Bearer %s\001\001", user
, access_token
);
982 b64
= xmallocz(ENCODED_SIZE(strlen(raw
)));
983 b64_len
= EVP_EncodeBlock((unsigned char *)b64
, (unsigned char *)raw
, strlen(raw
));
993 static int auth_plain(struct imap_store
*ctx
, const char *prompt UNUSED
)
998 b64
= plain_base64(ctx
->cfg
->user
, ctx
->cfg
->pass
);
1000 return error("PLAIN: base64 encoding failed");
1002 /* Send the base64-encoded response */
1003 ret
= socket_write(&ctx
->imap
->buf
.sock
, b64
, strlen(b64
));
1004 if (ret
!= (int)strlen(b64
)) {
1006 return error("IMAP error: sending PLAIN response failed");
1013 static int auth_cram_md5(struct imap_store
*ctx
, const char *prompt
)
1018 response
= cram(prompt
, ctx
->cfg
->user
, ctx
->cfg
->pass
);
1020 ret
= socket_write(&ctx
->imap
->buf
.sock
, response
, strlen(response
));
1021 if (ret
!= strlen(response
)) {
1023 return error("IMAP error: sending CRAM-MD5 response failed");
1031 static int auth_oauthbearer(struct imap_store
*ctx
, const char *prompt UNUSED
)
1036 b64
= oauthbearer_base64(ctx
->cfg
->user
, ctx
->cfg
->pass
);
1038 return error("OAUTHBEARER: base64 encoding failed");
1040 /* Send the base64-encoded response */
1041 ret
= socket_write(&ctx
->imap
->buf
.sock
, b64
, strlen(b64
));
1042 if (ret
!= (int)strlen(b64
)) {
1044 return error("IMAP error: sending OAUTHBEARER response failed");
1051 static int auth_xoauth2(struct imap_store
*ctx
, const char *prompt UNUSED
)
1056 b64
= xoauth2_base64(ctx
->cfg
->user
, ctx
->cfg
->pass
);
1058 return error("XOAUTH2: base64 encoding failed");
1060 /* Send the base64-encoded response */
1061 ret
= socket_write(&ctx
->imap
->buf
.sock
, b64
, strlen(b64
));
1062 if (ret
!= (int)strlen(b64
)) {
1064 return error("IMAP error: sending XOAUTH2 response failed");
1073 #define auth_plain NULL
1074 #define auth_cram_md5 NULL
1075 #define auth_oauthbearer NULL
1076 #define auth_xoauth2 NULL
1080 static void server_fill_credential(struct imap_server_conf
*srvc
, struct credential
*cred
)
1082 if (srvc
->user
&& srvc
->pass
)
1085 cred
->protocol
= xstrdup(srvc
->use_ssl
? "imaps" : "imap");
1086 cred
->host
= xstrfmt("%s:%d", srvc
->host
, srvc
->port
);
1088 cred
->username
= xstrdup_or_null(srvc
->user
);
1089 cred
->password
= xstrdup_or_null(srvc
->pass
);
1091 credential_fill(the_repository
, cred
, 1);
1094 srvc
->user
= xstrdup(cred
->username
);
1096 srvc
->pass
= xstrdup(cred
->password
);
1099 static int try_auth_method(struct imap_server_conf
*srvc
,
1100 struct imap_store
*ctx
,
1102 const char *auth_method
,
1103 enum CAPABILITY cap
,
1104 int (*fn
)(struct imap_store
*, const char *))
1106 struct imap_cmd_cb cb
= {0};
1109 fprintf(stderr
, "You specified "
1110 "%s as authentication method, "
1111 "but %s doesn't support it.\n",
1112 auth_method
, srvc
->host
);
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.",
1123 if (imap_exec(ctx
, &cb
, "AUTHENTICATE %s", auth_method
) != RESP_OK
) {
1124 fprintf(stderr
, "IMAP error: AUTHENTICATE %s failed\n",
1131 static struct imap_store
*imap_open_store(struct imap_server_conf
*srvc
, const char *folder
)
1133 struct credential cred
= CREDENTIAL_INIT
;
1134 struct imap_store
*ctx
;
1137 int s
= -1, preauth
;
1139 CALLOC_ARRAY(ctx
, 1);
1142 ctx
->imap
= CALLOC_ARRAY(imap
, 1);
1143 imap
->buf
.sock
.fd
[0] = imap
->buf
.sock
.fd
[1] = -1;
1144 imap
->in_progress_append
= &imap
->in_progress
;
1146 /* open connection to IMAP server */
1149 struct child_process tunnel
= CHILD_PROCESS_INIT
;
1151 imap_info("Starting tunnel '%s'... ", srvc
->tunnel
);
1153 strvec_push(&tunnel
.args
, srvc
->tunnel
);
1154 tunnel
.use_shell
= 1;
1157 if (start_command(&tunnel
))
1158 die("cannot start proxy %s", srvc
->tunnel
);
1160 imap
->buf
.sock
.fd
[0] = tunnel
.out
;
1161 imap
->buf
.sock
.fd
[1] = tunnel
.in
;
1166 struct addrinfo hints
, *ai0
, *ai
;
1170 xsnprintf(portstr
, sizeof(portstr
), "%d", srvc
->port
);
1172 memset(&hints
, 0, sizeof(hints
));
1173 hints
.ai_socktype
= SOCK_STREAM
;
1174 hints
.ai_protocol
= IPPROTO_TCP
;
1176 imap_info("Resolving %s... ", srvc
->host
);
1177 gai
= getaddrinfo(srvc
->host
, portstr
, &hints
, &ai
);
1179 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(gai
));
1184 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
1185 char addr
[NI_MAXHOST
];
1187 s
= socket(ai
->ai_family
, ai
->ai_socktype
,
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
);
1196 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
1208 struct sockaddr_in addr
;
1210 memset(&addr
, 0, sizeof(addr
));
1211 addr
.sin_port
= htons(srvc
->port
);
1212 addr
.sin_family
= AF_INET
;
1214 imap_info("Resolving %s... ", srvc
->host
);
1215 he
= gethostbyname(srvc
->host
);
1217 perror("gethostbyname");
1222 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
1224 s
= socket(PF_INET
, SOCK_STREAM
, 0);
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
))) {
1234 fputs("error: unable to connect to server\n", stderr
);
1238 imap
->buf
.sock
.fd
[0] = s
;
1239 imap
->buf
.sock
.fd
[1] = dup(s
);
1241 if (srvc
->use_ssl
&&
1242 ssl_socket_connect(&imap
->buf
.sock
, srvc
, 0)) {
1249 /* read the greeting string */
1250 if (buffer_gets(&imap
->buf
, &rsp
)) {
1251 fprintf(stderr
, "IMAP error: no greeting response\n");
1254 arg
= next_arg(&rsp
);
1255 if (!arg
|| *arg
!= '*' || (arg
= next_arg(&rsp
)) == NULL
) {
1256 fprintf(stderr
, "IMAP error: invalid greeting response\n");
1260 if (!strcmp("PREAUTH", arg
))
1262 else if (strcmp("OK", arg
) != 0) {
1263 fprintf(stderr
, "IMAP error: unknown greeting response\n");
1266 parse_response_code(ctx
, NULL
, rsp
);
1267 if (!imap
->caps
&& imap_exec(ctx
, NULL
, "CAPABILITY") != RESP_OK
)
1272 if (!srvc
->use_ssl
&& CAP(STARTTLS
)) {
1273 if (imap_exec(ctx
, NULL
, "STARTTLS") != RESP_OK
)
1275 if (ssl_socket_connect(&imap
->buf
.sock
, srvc
, 1))
1277 /* capabilities may have changed, so get the new capabilities */
1278 if (imap_exec(ctx
, NULL
, "CAPABILITY") != RESP_OK
)
1282 imap_info("Logging in...\n");
1283 server_fill_credential(srvc
, &cred
);
1285 if (srvc
->auth_method
) {
1286 if (!strcmp(srvc
->auth_method
, "PLAIN")) {
1287 if (try_auth_method(srvc
, ctx
, imap
, "PLAIN", AUTH_PLAIN
, auth_plain
))
1289 } else if (!strcmp(srvc
->auth_method
, "CRAM-MD5")) {
1290 if (try_auth_method(srvc
, ctx
, imap
, "CRAM-MD5", AUTH_CRAM_MD5
, auth_cram_md5
))
1292 } else if (!strcmp(srvc
->auth_method
, "OAUTHBEARER")) {
1293 if (try_auth_method(srvc
, ctx
, imap
, "OAUTHBEARER", AUTH_OAUTHBEARER
, auth_oauthbearer
))
1295 } else if (!strcmp(srvc
->auth_method
, "XOAUTH2")) {
1296 if (try_auth_method(srvc
, ctx
, imap
, "XOAUTH2", AUTH_XOAUTH2
, auth_xoauth2
))
1299 fprintf(stderr
, "unknown authentication mechanism: %s\n", srvc
->auth_method
);
1304 fprintf(stderr
, "skipping account %s@%s, server forbids LOGIN\n",
1305 srvc
->user
, srvc
->host
);
1308 if (!imap
->buf
.sock
.ssl
)
1309 imap_warn("*** IMAP Warning *** Password is being "
1310 "sent in the clear\n");
1311 if (imap_exec(ctx
, NULL
, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1312 fprintf(stderr
, "IMAP error: LOGIN failed\n");
1319 credential_approve(the_repository
, &cred
);
1320 credential_clear(&cred
);
1322 /* check the target mailbox exists */
1324 switch (imap_exec(ctx
, NULL
, "EXAMINE \"%s\"", ctx
->name
)) {
1329 fprintf(stderr
, "IMAP error: could not check mailbox\n");
1332 if (imap_exec(ctx
, NULL
, "CREATE \"%s\"", ctx
->name
) == RESP_OK
) {
1333 imap_info("Created missing mailbox\n");
1335 fprintf(stderr
, "IMAP error: could not create missing mailbox\n");
1346 credential_reject(the_repository
, &cred
);
1347 credential_clear(&cred
);
1350 imap_close_store(ctx
);
1355 * Insert CR characters as necessary in *msg to ensure that every LF
1356 * character in *msg is preceded by a CR.
1358 static void lf_to_crlf(struct strbuf
*msg
)
1364 /* First pass: tally, in j, the size of the new_msg string: */
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
];
1372 new_msg
= xmallocz(j
);
1375 * Second pass: write the new_msg string. Note that this loop is
1376 * otherwise identical to the first pass.
1378 for (i
= j
= 0, lastc
= '\0'; i
< msg
->len
; i
++) {
1379 if (msg
->buf
[i
] == '\n' && lastc
!= '\r')
1380 new_msg
[j
++] = '\r';
1381 lastc
= new_msg
[j
++] = msg
->buf
[i
];
1383 strbuf_attach(msg
, new_msg
, j
, j
+ 1);
1387 * Store msg to IMAP. Also detach and free the data from msg->data,
1388 * leaving msg->data empty.
1390 static int imap_store_msg(struct imap_store
*ctx
, struct strbuf
*msg
)
1392 struct imap
*imap
= ctx
->imap
;
1393 struct imap_cmd_cb cb
;
1394 const char *prefix
, *box
;
1398 memset(&cb
, 0, sizeof(cb
));
1401 cb
.data
= strbuf_detach(msg
, NULL
);
1404 prefix
= !strcmp(box
, "INBOX") ? "" : ctx
->prefix
;
1405 ret
= imap_exec_m(ctx
, &cb
, "APPEND \"%s%s\" ", prefix
, box
);
1406 imap
->caps
= imap
->rcaps
;
1413 static void wrap_in_html(struct strbuf
*msg
)
1415 struct strbuf buf
= STRBUF_INIT
;
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";
1419 const char *body
= strstr(msg
->buf
, "\n\n");
1422 return; /* Headers but no body; no wrapping needed */
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
);
1431 strbuf_addstr(&buf
, pre_close
);
1433 strbuf_release(msg
);
1437 static int count_messages(struct strbuf
*all_msgs
)
1440 char *p
= all_msgs
->buf
;
1443 if (starts_with(p
, "From ")) {
1444 p
= strstr(p
+5, "\nFrom: ");
1446 p
= strstr(p
+7, "\nDate: ");
1448 p
= strstr(p
+7, "\nSubject: ");
1453 p
= strstr(p
+5, "\nFrom ");
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.
1466 static int split_msg(struct strbuf
*all_msgs
, struct strbuf
*msg
, int *ofs
)
1471 if (*ofs
>= all_msgs
->len
)
1474 data
= &all_msgs
->buf
[*ofs
];
1475 len
= all_msgs
->len
- *ofs
;
1477 if (len
< 5 || !starts_with(data
, "From "))
1480 p
= strchr(data
, '\n');
1488 p
= strstr(data
, "\nFrom ");
1492 strbuf_add(msg
, data
, len
);
1497 static int git_imap_config(const char *var
, const char *val
,
1498 const struct config_context
*ctx
, void *cb
)
1500 struct imap_server_conf
*cfg
= cb
;
1502 if (!strcmp("imap.sslverify", var
)) {
1503 cfg
->ssl_verify
= git_config_bool(var
, val
);
1504 } else if (!strcmp("imap.preformattedhtml", var
)) {
1505 cfg
->use_html
= git_config_bool(var
, val
);
1506 } else if (!strcmp("imap.folder", var
)) {
1507 FREE_AND_NULL(cfg
->folder
);
1508 return git_config_string(&cfg
->folder
, var
, val
);
1509 } else if (!strcmp("imap.user", var
)) {
1510 FREE_AND_NULL(cfg
->user
);
1511 return git_config_string(&cfg
->user
, var
, val
);
1512 } else if (!strcmp("imap.pass", var
)) {
1513 FREE_AND_NULL(cfg
->pass
);
1514 return git_config_string(&cfg
->pass
, var
, val
);
1515 } else if (!strcmp("imap.tunnel", var
)) {
1516 FREE_AND_NULL(cfg
->tunnel
);
1517 return git_config_string(&cfg
->tunnel
, var
, val
);
1518 } else if (!strcmp("imap.authmethod", var
)) {
1519 FREE_AND_NULL(cfg
->auth_method
);
1520 return git_config_string(&cfg
->auth_method
, var
, val
);
1521 } else if (!strcmp("imap.port", var
)) {
1522 cfg
->port
= git_config_int(var
, val
, ctx
->kvi
);
1523 } else if (!strcmp("imap.host", var
)) {
1525 return config_error_nonbool(var
);
1527 if (starts_with(val
, "imap:"))
1529 else if (starts_with(val
, "imaps:")) {
1533 if (starts_with(val
, "//"))
1535 cfg
->host
= xstrdup(val
);
1538 return git_default_config(var
, val
, ctx
, cb
);
1544 static int append_msgs_to_imap(struct imap_server_conf
*server
,
1545 struct strbuf
* all_msgs
, int total
)
1547 struct strbuf msg
= STRBUF_INIT
;
1548 struct imap_store
*ctx
= NULL
;
1553 ctx
= imap_open_store(server
, server
->folder
);
1555 fprintf(stderr
, "failed to open store\n");
1558 ctx
->name
= server
->folder
;
1560 fprintf(stderr
, "Sending %d message%s to %s folder...\n",
1561 total
, (total
!= 1) ? "s" : "", server
->folder
);
1563 unsigned percent
= n
* 100 / total
;
1565 fprintf(stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1567 if (!split_msg(all_msgs
, &msg
, &ofs
))
1569 if (server
->use_html
)
1571 r
= imap_store_msg(ctx
, &msg
);
1576 fprintf(stderr
, "\n");
1578 imap_close_store(ctx
);
1583 static int list_imap_folders(struct imap_server_conf
*server
)
1585 struct imap_store
*ctx
= imap_open_store(server
, "INBOX");
1587 fprintf(stderr
, "failed to connect to IMAP server\n");
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
);
1599 imap_close_store(ctx
);
1603 #ifdef USE_CURL_FOR_IMAP_SEND
1604 static CURL
*setup_curl(struct imap_server_conf
*srvc
, struct credential
*cred
)
1607 struct strbuf path
= STRBUF_INIT
;
1608 char *uri_encoded_folder
;
1610 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
1611 die("curl_global_init failed");
1613 curl
= curl_easy_init();
1616 die("curl_easy_init failed");
1618 server_fill_credential(srvc
, cred
);
1619 curl_easy_setopt(curl
, CURLOPT_USERNAME
, srvc
->user
);
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.
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
);
1631 strbuf_addstr(&path
, srvc
->use_ssl
? "imaps://" : "imap://");
1632 strbuf_addstr(&path
, srvc
->host
);
1633 if (!path
.len
|| path
.buf
[path
.len
- 1] != '/')
1634 strbuf_addch(&path
, '/');
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
);
1644 curl_easy_setopt(curl
, CURLOPT_URL
, path
.buf
);
1645 strbuf_release(&path
);
1646 curl_easy_setopt(curl
, CURLOPT_PORT
, (long)srvc
->port
);
1648 if (srvc
->auth_method
) {
1649 if (!strcmp(srvc
->auth_method
, "XOAUTH2") ||
1650 !strcmp(srvc
->auth_method
, "OAUTHBEARER")) {
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.
1657 curl_easy_setopt(curl
, CURLOPT_XOAUTH2_BEARER
, srvc
->pass
);
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
);
1668 curl_easy_setopt(curl
, CURLOPT_USE_SSL
, (long)CURLUSESSL_TRY
);
1670 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, (long)srvc
->ssl_verify
);
1671 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, (long)srvc
->ssl_verify
);
1673 if (0 < verbosity
|| getenv("GIT_CURL_VERBOSE"))
1674 http_trace_curl_no_data();
1675 setup_curl_trace(curl
);
1680 static int curl_append_msgs_to_imap(struct imap_server_conf
*server
,
1681 struct strbuf
* all_msgs
, int total
)
1685 struct buffer msgbuf
= { STRBUF_INIT
, 0 };
1687 CURLcode res
= CURLE_OK
;
1688 struct credential cred
= CREDENTIAL_INIT
;
1690 curl
= setup_curl(server
, &cred
);
1692 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1693 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
1695 curl_easy_setopt(curl
, CURLOPT_READDATA
, &msgbuf
);
1697 fprintf(stderr
, "Sending %d message%s to %s folder...\n",
1698 total
, (total
!= 1) ? "s" : "", server
->folder
);
1700 unsigned percent
= n
* 100 / total
;
1703 fprintf(stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1705 prev_len
= msgbuf
.buf
.len
;
1706 if (!split_msg(all_msgs
, &msgbuf
.buf
, &ofs
))
1708 if (server
->use_html
)
1709 wrap_in_html(&msgbuf
.buf
);
1710 lf_to_crlf(&msgbuf
.buf
);
1712 curl_easy_setopt(curl
, CURLOPT_INFILESIZE_LARGE
,
1713 (curl_off_t
)(msgbuf
.buf
.len
-prev_len
));
1715 res
= curl_easy_perform(curl
);
1717 if(res
!= CURLE_OK
) {
1718 fprintf(stderr
, "curl_easy_perform() failed: %s\n",
1719 curl_easy_strerror(res
));
1725 fprintf(stderr
, "\n");
1727 curl_easy_cleanup(curl
);
1728 curl_global_cleanup();
1730 if (cred
.username
) {
1731 if (res
== CURLE_OK
)
1732 credential_approve(the_repository
, &cred
);
1733 else if (res
== CURLE_LOGIN_DENIED
)
1734 credential_reject(the_repository
, &cred
);
1737 credential_clear(&cred
);
1739 return res
!= CURLE_OK
;
1742 static int curl_list_imap_folders(struct imap_server_conf
*server
)
1745 CURLcode res
= CURLE_OK
;
1746 struct credential cred
= CREDENTIAL_INIT
;
1748 fprintf(stderr
, "Fetching the list of available folders...\n");
1749 curl
= setup_curl(server
, &cred
);
1750 res
= curl_easy_perform(curl
);
1752 curl_easy_cleanup(curl
);
1753 curl_global_cleanup();
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
);
1762 credential_clear(&cred
);
1764 return res
!= CURLE_OK
;
1768 int cmd_main(int argc
, const char **argv
)
1770 struct imap_server_conf server
= {
1773 struct strbuf all_msgs
= STRBUF_INIT
;
1778 setup_git_directory_gently(&nongit_ok
);
1779 git_config(git_imap_config
, &server
);
1781 argc
= parse_options(argc
, (const char **)argv
, "", imap_send_options
, imap_send_usage
, 0);
1784 free(server
.folder
);
1785 server
.folder
= xstrdup(opt_folder
);
1789 usage_with_options(imap_send_usage
, imap_send_options
);
1791 #ifndef USE_CURL_FOR_IMAP_SEND
1793 warning("--curl not supported in this build");
1796 #elif defined(NO_OPENSSL)
1798 warning("--no-curl not supported in this build");
1804 server
.port
= server
.use_ssl
? 993 : 143;
1807 if (!server
.tunnel
) {
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')"));
1814 server
.host
= xstrdup("tunnel");
1819 ret
= list_imap_folders(&server
);
1820 #ifdef USE_CURL_FOR_IMAP_SEND
1822 ret
= curl_list_imap_folders(&server
);
1825 ret
= list_imap_folders(&server
);
1829 if (!server
.folder
) {
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')"));
1837 /* read the messages */
1838 if (strbuf_read(&all_msgs
, 0, 0) < 0) {
1839 error_errno(_("could not read from stdin"));
1844 if (all_msgs
.len
== 0) {
1845 fprintf(stderr
, "nothing to send\n");
1850 total
= count_messages(&all_msgs
);
1852 fprintf(stderr
, "no messages found to send\n");
1857 /* write it to the imap server */
1860 ret
= append_msgs_to_imap(&server
, &all_msgs
, total
);
1861 #ifdef USE_CURL_FOR_IMAP_SEND
1863 ret
= curl_append_msgs_to_imap(&server
, &all_msgs
, total
);
1866 ret
= append_msgs_to_imap(&server
, &all_msgs
, total
);
1869 free(server
.tunnel
);
1871 free(server
.folder
);
1874 free(server
.auth_method
);
1875 strbuf_release(&all_msgs
);