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