imap-send.con commit hex.c: reduce memory footprint of sha1_to_hex static buffers (0b868f0)
   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, write to the Free Software
  22 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23 */
  24
  25#include "cache.h"
  26#include "exec_cmd.h"
  27#include "run-command.h"
  28#ifdef NO_OPENSSL
  29typedef void *SSL;
  30#endif
  31
  32struct store_conf {
  33        char *name;
  34        const char *path; /* should this be here? its interpretation is driver-specific */
  35        char *map_inbox;
  36        char *trash;
  37        unsigned max_size; /* off_t is overkill */
  38        unsigned trash_remote_new:1, trash_only_new:1;
  39};
  40
  41struct string_list {
  42        struct string_list *next;
  43        char string[1];
  44};
  45
  46struct channel_conf {
  47        struct channel_conf *next;
  48        char *name;
  49        struct store_conf *master, *slave;
  50        char *master_name, *slave_name;
  51        char *sync_state;
  52        struct string_list *patterns;
  53        int mops, sops;
  54        unsigned max_messages; /* for slave only */
  55};
  56
  57struct group_conf {
  58        struct group_conf *next;
  59        char *name;
  60        struct string_list *channels;
  61};
  62
  63/* For message->status */
  64#define M_RECENT       (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
  65#define M_DEAD         (1<<1) /* expunged */
  66#define M_FLAGS        (1<<2) /* flags fetched */
  67
  68struct message {
  69        struct message *next;
  70        /* struct string_list *keywords; */
  71        size_t size; /* zero implies "not fetched" */
  72        int uid;
  73        unsigned char flags, status;
  74};
  75
  76struct store {
  77        struct store_conf *conf; /* foreign */
  78
  79        /* currently open mailbox */
  80        const char *name; /* foreign! maybe preset? */
  81        char *path; /* own */
  82        struct message *msgs; /* own */
  83        int uidvalidity;
  84        unsigned char opts; /* maybe preset? */
  85        /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
  86        int count; /* # of messages */
  87        int recent; /* # of recent messages - don't trust this beyond the initial read */
  88};
  89
  90struct msg_data {
  91        char *data;
  92        int len;
  93        unsigned char flags;
  94        unsigned int crlf:1;
  95};
  96
  97static const char imap_send_usage[] = "git imap-send < <mbox>";
  98
  99#undef DRV_OK
 100#define DRV_OK          0
 101#define DRV_MSG_BAD     -1
 102#define DRV_BOX_BAD     -2
 103#define DRV_STORE_BAD   -3
 104
 105static int Verbose, Quiet;
 106
 107__attribute__((format (printf, 1, 2)))
 108static void imap_info(const char *, ...);
 109__attribute__((format (printf, 1, 2)))
 110static void imap_warn(const char *, ...);
 111
 112static char *next_arg(char **);
 113
 114static void free_generic_messages(struct message *);
 115
 116__attribute__((format (printf, 3, 4)))
 117static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
 118
 119static int nfvasprintf(char **strp, const char *fmt, va_list ap)
 120{
 121        int len;
 122        char tmp[8192];
 123
 124        len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
 125        if (len < 0)
 126                die("Fatal: Out of memory");
 127        if (len >= sizeof(tmp))
 128                die("imap command overflow!");
 129        *strp = xmemdupz(tmp, len);
 130        return len;
 131}
 132
 133struct imap_server_conf {
 134        char *name;
 135        char *tunnel;
 136        char *host;
 137        int port;
 138        char *user;
 139        char *pass;
 140        int use_ssl;
 141        int ssl_verify;
 142        int use_html;
 143};
 144
 145struct imap_store_conf {
 146        struct store_conf gen;
 147        struct imap_server_conf *server;
 148        unsigned use_namespace:1;
 149};
 150
 151#define NIL     (void *)0x1
 152#define LIST    (void *)0x2
 153
 154struct imap_list {
 155        struct imap_list *next, *child;
 156        char *val;
 157        int len;
 158};
 159
 160struct imap_socket {
 161        int fd[2];
 162        SSL *ssl;
 163};
 164
 165struct imap_buffer {
 166        struct imap_socket sock;
 167        int bytes;
 168        int offset;
 169        char buf[1024];
 170};
 171
 172struct imap_cmd;
 173
 174struct imap {
 175        int uidnext; /* from SELECT responses */
 176        struct imap_list *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
 177        unsigned caps, rcaps; /* CAPABILITY results */
 178        /* command queue */
 179        int nexttag, num_in_progress, literal_pending;
 180        struct imap_cmd *in_progress, **in_progress_append;
 181        struct imap_buffer buf; /* this is BIG, so put it last */
 182};
 183
 184struct imap_store {
 185        struct store gen;
 186        int uidvalidity;
 187        struct imap *imap;
 188        const char *prefix;
 189        unsigned /*currentnc:1,*/ trashnc:1;
 190};
 191
 192struct imap_cmd_cb {
 193        int (*cont)(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt);
 194        void (*done)(struct imap_store *ctx, struct imap_cmd *cmd, int response);
 195        void *ctx;
 196        char *data;
 197        int dlen;
 198        int uid;
 199        unsigned create:1, trycreate:1;
 200};
 201
 202struct imap_cmd {
 203        struct imap_cmd *next;
 204        struct imap_cmd_cb cb;
 205        char *cmd;
 206        int tag;
 207};
 208
 209#define CAP(cap) (imap->caps & (1 << (cap)))
 210
 211enum CAPABILITY {
 212        NOLOGIN = 0,
 213        UIDPLUS,
 214        LITERALPLUS,
 215        NAMESPACE,
 216        STARTTLS,
 217};
 218
 219static const char *cap_list[] = {
 220        "LOGINDISABLED",
 221        "UIDPLUS",
 222        "LITERAL+",
 223        "NAMESPACE",
 224        "STARTTLS",
 225};
 226
 227#define RESP_OK    0
 228#define RESP_NO    1
 229#define RESP_BAD   2
 230
 231static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd);
 232
 233
 234static const char *Flags[] = {
 235        "Draft",
 236        "Flagged",
 237        "Answered",
 238        "Seen",
 239        "Deleted",
 240};
 241
 242#ifndef NO_OPENSSL
 243static void ssl_socket_perror(const char *func)
 244{
 245        fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), NULL));
 246}
 247#endif
 248
 249static void socket_perror(const char *func, struct imap_socket *sock, int ret)
 250{
 251#ifndef NO_OPENSSL
 252        if (sock->ssl) {
 253                int sslerr = SSL_get_error(sock->ssl, ret);
 254                switch (sslerr) {
 255                case SSL_ERROR_NONE:
 256                        break;
 257                case SSL_ERROR_SYSCALL:
 258                        perror("SSL_connect");
 259                        break;
 260                default:
 261                        ssl_socket_perror("SSL_connect");
 262                        break;
 263                }
 264        } else
 265#endif
 266        {
 267                if (ret < 0)
 268                        perror(func);
 269                else
 270                        fprintf(stderr, "%s: unexpected EOF\n", func);
 271        }
 272}
 273
 274static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
 275{
 276#ifdef NO_OPENSSL
 277        fprintf(stderr, "SSL requested but SSL support not compiled in\n");
 278        return -1;
 279#else
 280#if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
 281        const SSL_METHOD *meth;
 282#else
 283        SSL_METHOD *meth;
 284#endif
 285        SSL_CTX *ctx;
 286        int ret;
 287
 288        SSL_library_init();
 289        SSL_load_error_strings();
 290
 291        if (use_tls_only)
 292                meth = TLSv1_method();
 293        else
 294                meth = SSLv23_method();
 295
 296        if (!meth) {
 297                ssl_socket_perror("SSLv23_method");
 298                return -1;
 299        }
 300
 301        ctx = SSL_CTX_new(meth);
 302
 303        if (verify)
 304                SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
 305
 306        if (!SSL_CTX_set_default_verify_paths(ctx)) {
 307                ssl_socket_perror("SSL_CTX_set_default_verify_paths");
 308                return -1;
 309        }
 310        sock->ssl = SSL_new(ctx);
 311        if (!sock->ssl) {
 312                ssl_socket_perror("SSL_new");
 313                return -1;
 314        }
 315        if (!SSL_set_rfd(sock->ssl, sock->fd[0])) {
 316                ssl_socket_perror("SSL_set_rfd");
 317                return -1;
 318        }
 319        if (!SSL_set_wfd(sock->ssl, sock->fd[1])) {
 320                ssl_socket_perror("SSL_set_wfd");
 321                return -1;
 322        }
 323
 324        ret = SSL_connect(sock->ssl);
 325        if (ret <= 0) {
 326                socket_perror("SSL_connect", sock, ret);
 327                return -1;
 328        }
 329
 330        return 0;
 331#endif
 332}
 333
 334static int socket_read(struct imap_socket *sock, char *buf, int len)
 335{
 336        ssize_t n;
 337#ifndef NO_OPENSSL
 338        if (sock->ssl)
 339                n = SSL_read(sock->ssl, buf, len);
 340        else
 341#endif
 342                n = xread(sock->fd[0], buf, len);
 343        if (n <= 0) {
 344                socket_perror("read", sock, n);
 345                close(sock->fd[0]);
 346                close(sock->fd[1]);
 347                sock->fd[0] = sock->fd[1] = -1;
 348        }
 349        return n;
 350}
 351
 352static int socket_write(struct imap_socket *sock, const char *buf, int len)
 353{
 354        int n;
 355#ifndef NO_OPENSSL
 356        if (sock->ssl)
 357                n = SSL_write(sock->ssl, buf, len);
 358        else
 359#endif
 360                n = write_in_full(sock->fd[1], buf, len);
 361        if (n != len) {
 362                socket_perror("write", sock, n);
 363                close(sock->fd[0]);
 364                close(sock->fd[1]);
 365                sock->fd[0] = sock->fd[1] = -1;
 366        }
 367        return n;
 368}
 369
 370static void socket_shutdown(struct imap_socket *sock)
 371{
 372#ifndef NO_OPENSSL
 373        if (sock->ssl) {
 374                SSL_shutdown(sock->ssl);
 375                SSL_free(sock->ssl);
 376        }
 377#endif
 378        close(sock->fd[0]);
 379        close(sock->fd[1]);
 380}
 381
 382/* simple line buffering */
 383static int buffer_gets(struct imap_buffer *b, char **s)
 384{
 385        int n;
 386        int start = b->offset;
 387
 388        *s = b->buf + start;
 389
 390        for (;;) {
 391                /* make sure we have enough data to read the \r\n sequence */
 392                if (b->offset + 1 >= b->bytes) {
 393                        if (start) {
 394                                /* shift down used bytes */
 395                                *s = b->buf;
 396
 397                                assert(start <= b->bytes);
 398                                n = b->bytes - start;
 399
 400                                if (n)
 401                                        memmove(b->buf, b->buf + start, n);
 402                                b->offset -= start;
 403                                b->bytes = n;
 404                                start = 0;
 405                        }
 406
 407                        n = socket_read(&b->sock, b->buf + b->bytes,
 408                                         sizeof(b->buf) - b->bytes);
 409
 410                        if (n <= 0)
 411                                return -1;
 412
 413                        b->bytes += n;
 414                }
 415
 416                if (b->buf[b->offset] == '\r') {
 417                        assert(b->offset + 1 < b->bytes);
 418                        if (b->buf[b->offset + 1] == '\n') {
 419                                b->buf[b->offset] = 0;  /* terminate the string */
 420                                b->offset += 2; /* next line */
 421                                if (Verbose)
 422                                        puts(*s);
 423                                return 0;
 424                        }
 425                }
 426
 427                b->offset++;
 428        }
 429        /* not reached */
 430}
 431
 432static void imap_info(const char *msg, ...)
 433{
 434        va_list va;
 435
 436        if (!Quiet) {
 437                va_start(va, msg);
 438                vprintf(msg, va);
 439                va_end(va);
 440                fflush(stdout);
 441        }
 442}
 443
 444static void imap_warn(const char *msg, ...)
 445{
 446        va_list va;
 447
 448        if (Quiet < 2) {
 449                va_start(va, msg);
 450                vfprintf(stderr, msg, va);
 451                va_end(va);
 452        }
 453}
 454
 455static char *next_arg(char **s)
 456{
 457        char *ret;
 458
 459        if (!s || !*s)
 460                return NULL;
 461        while (isspace((unsigned char) **s))
 462                (*s)++;
 463        if (!**s) {
 464                *s = NULL;
 465                return NULL;
 466        }
 467        if (**s == '"') {
 468                ++*s;
 469                ret = *s;
 470                *s = strchr(*s, '"');
 471        } else {
 472                ret = *s;
 473                while (**s && !isspace((unsigned char) **s))
 474                        (*s)++;
 475        }
 476        if (*s) {
 477                if (**s)
 478                        *(*s)++ = 0;
 479                if (!**s)
 480                        *s = NULL;
 481        }
 482        return ret;
 483}
 484
 485static void free_generic_messages(struct message *msgs)
 486{
 487        struct message *tmsg;
 488
 489        for (; msgs; msgs = tmsg) {
 490                tmsg = msgs->next;
 491                free(msgs);
 492        }
 493}
 494
 495static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
 496{
 497        int ret;
 498        va_list va;
 499
 500        va_start(va, fmt);
 501        if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
 502                die("Fatal: buffer too small. Please report a bug.");
 503        va_end(va);
 504        return ret;
 505}
 506
 507static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
 508                                         struct imap_cmd_cb *cb,
 509                                         const char *fmt, va_list ap)
 510{
 511        struct imap *imap = ctx->imap;
 512        struct imap_cmd *cmd;
 513        int n, bufl;
 514        char buf[1024];
 515
 516        cmd = xmalloc(sizeof(struct imap_cmd));
 517        nfvasprintf(&cmd->cmd, fmt, ap);
 518        cmd->tag = ++imap->nexttag;
 519
 520        if (cb)
 521                cmd->cb = *cb;
 522        else
 523                memset(&cmd->cb, 0, sizeof(cmd->cb));
 524
 525        while (imap->literal_pending)
 526                get_cmd_result(ctx, NULL);
 527
 528        bufl = nfsnprintf(buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
 529                           "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
 530                           cmd->tag, cmd->cmd, cmd->cb.dlen);
 531        if (Verbose) {
 532                if (imap->num_in_progress)
 533                        printf("(%d in progress) ", imap->num_in_progress);
 534                if (memcmp(cmd->cmd, "LOGIN", 5))
 535                        printf(">>> %s", buf);
 536                else
 537                        printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
 538        }
 539        if (socket_write(&imap->buf.sock, buf, bufl) != bufl) {
 540                free(cmd->cmd);
 541                free(cmd);
 542                if (cb)
 543                        free(cb->data);
 544                return NULL;
 545        }
 546        if (cmd->cb.data) {
 547                if (CAP(LITERALPLUS)) {
 548                        n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);
 549                        free(cmd->cb.data);
 550                        if (n != cmd->cb.dlen ||
 551                            socket_write(&imap->buf.sock, "\r\n", 2) != 2) {
 552                                free(cmd->cmd);
 553                                free(cmd);
 554                                return NULL;
 555                        }
 556                        cmd->cb.data = NULL;
 557                } else
 558                        imap->literal_pending = 1;
 559        } else if (cmd->cb.cont)
 560                imap->literal_pending = 1;
 561        cmd->next = NULL;
 562        *imap->in_progress_append = cmd;
 563        imap->in_progress_append = &cmd->next;
 564        imap->num_in_progress++;
 565        return cmd;
 566}
 567
 568__attribute__((format (printf, 3, 4)))
 569static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
 570                                       struct imap_cmd_cb *cb,
 571                                       const char *fmt, ...)
 572{
 573        struct imap_cmd *ret;
 574        va_list ap;
 575
 576        va_start(ap, fmt);
 577        ret = v_issue_imap_cmd(ctx, cb, fmt, ap);
 578        va_end(ap);
 579        return ret;
 580}
 581
 582__attribute__((format (printf, 3, 4)))
 583static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb,
 584                     const char *fmt, ...)
 585{
 586        va_list ap;
 587        struct imap_cmd *cmdp;
 588
 589        va_start(ap, fmt);
 590        cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
 591        va_end(ap);
 592        if (!cmdp)
 593                return RESP_BAD;
 594
 595        return get_cmd_result(ctx, cmdp);
 596}
 597
 598__attribute__((format (printf, 3, 4)))
 599static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb,
 600                       const char *fmt, ...)
 601{
 602        va_list ap;
 603        struct imap_cmd *cmdp;
 604
 605        va_start(ap, fmt);
 606        cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
 607        va_end(ap);
 608        if (!cmdp)
 609                return DRV_STORE_BAD;
 610
 611        switch (get_cmd_result(ctx, cmdp)) {
 612        case RESP_BAD: return DRV_STORE_BAD;
 613        case RESP_NO: return DRV_MSG_BAD;
 614        default: return DRV_OK;
 615        }
 616}
 617
 618static int is_atom(struct imap_list *list)
 619{
 620        return list && list->val && list->val != NIL && list->val != LIST;
 621}
 622
 623static int is_list(struct imap_list *list)
 624{
 625        return list && list->val == LIST;
 626}
 627
 628static void free_list(struct imap_list *list)
 629{
 630        struct imap_list *tmp;
 631
 632        for (; list; list = tmp) {
 633                tmp = list->next;
 634                if (is_list(list))
 635                        free_list(list->child);
 636                else if (is_atom(list))
 637                        free(list->val);
 638                free(list);
 639        }
 640}
 641
 642static int parse_imap_list_l(struct imap *imap, char **sp, struct imap_list **curp, int level)
 643{
 644        struct imap_list *cur;
 645        char *s = *sp, *p;
 646        int n, bytes;
 647
 648        for (;;) {
 649                while (isspace((unsigned char)*s))
 650                        s++;
 651                if (level && *s == ')') {
 652                        s++;
 653                        break;
 654                }
 655                *curp = cur = xmalloc(sizeof(*cur));
 656                curp = &cur->next;
 657                cur->val = NULL; /* for clean bail */
 658                if (*s == '(') {
 659                        /* sublist */
 660                        s++;
 661                        cur->val = LIST;
 662                        if (parse_imap_list_l(imap, &s, &cur->child, level + 1))
 663                                goto bail;
 664                } else if (imap && *s == '{') {
 665                        /* literal */
 666                        bytes = cur->len = strtol(s + 1, &s, 10);
 667                        if (*s != '}')
 668                                goto bail;
 669
 670                        s = cur->val = xmalloc(cur->len);
 671
 672                        /* dump whats left over in the input buffer */
 673                        n = imap->buf.bytes - imap->buf.offset;
 674
 675                        if (n > bytes)
 676                                /* the entire message fit in the buffer */
 677                                n = bytes;
 678
 679                        memcpy(s, imap->buf.buf + imap->buf.offset, n);
 680                        s += n;
 681                        bytes -= n;
 682
 683                        /* mark that we used part of the buffer */
 684                        imap->buf.offset += n;
 685
 686                        /* now read the rest of the message */
 687                        while (bytes > 0) {
 688                                if ((n = socket_read(&imap->buf.sock, s, bytes)) <= 0)
 689                                        goto bail;
 690                                s += n;
 691                                bytes -= n;
 692                        }
 693
 694                        if (buffer_gets(&imap->buf, &s))
 695                                goto bail;
 696                } else if (*s == '"') {
 697                        /* quoted string */
 698                        s++;
 699                        p = s;
 700                        for (; *s != '"'; s++)
 701                                if (!*s)
 702                                        goto bail;
 703                        cur->len = s - p;
 704                        s++;
 705                        cur->val = xmemdupz(p, cur->len);
 706                } else {
 707                        /* atom */
 708                        p = s;
 709                        for (; *s && !isspace((unsigned char)*s); s++)
 710                                if (level && *s == ')')
 711                                        break;
 712                        cur->len = s - p;
 713                        if (cur->len == 3 && !memcmp("NIL", p, 3))
 714                                cur->val = NIL;
 715                        else
 716                                cur->val = xmemdupz(p, cur->len);
 717                }
 718
 719                if (!level)
 720                        break;
 721                if (!*s)
 722                        goto bail;
 723        }
 724        *sp = s;
 725        *curp = NULL;
 726        return 0;
 727
 728bail:
 729        *curp = NULL;
 730        return -1;
 731}
 732
 733static struct imap_list *parse_imap_list(struct imap *imap, char **sp)
 734{
 735        struct imap_list *head;
 736
 737        if (!parse_imap_list_l(imap, sp, &head, 0))
 738                return head;
 739        free_list(head);
 740        return NULL;
 741}
 742
 743static struct imap_list *parse_list(char **sp)
 744{
 745        return parse_imap_list(NULL, sp);
 746}
 747
 748static void parse_capability(struct imap *imap, char *cmd)
 749{
 750        char *arg;
 751        unsigned i;
 752
 753        imap->caps = 0x80000000;
 754        while ((arg = next_arg(&cmd)))
 755                for (i = 0; i < ARRAY_SIZE(cap_list); i++)
 756                        if (!strcmp(cap_list[i], arg))
 757                                imap->caps |= 1 << i;
 758        imap->rcaps = imap->caps;
 759}
 760
 761static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
 762                               char *s)
 763{
 764        struct imap *imap = ctx->imap;
 765        char *arg, *p;
 766
 767        if (*s != '[')
 768                return RESP_OK;         /* no response code */
 769        s++;
 770        if (!(p = strchr(s, ']'))) {
 771                fprintf(stderr, "IMAP error: malformed response code\n");
 772                return RESP_BAD;
 773        }
 774        *p++ = 0;
 775        arg = next_arg(&s);
 776        if (!strcmp("UIDVALIDITY", arg)) {
 777                if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg))) {
 778                        fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
 779                        return RESP_BAD;
 780                }
 781        } else if (!strcmp("UIDNEXT", arg)) {
 782                if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
 783                        fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
 784                        return RESP_BAD;
 785                }
 786        } else if (!strcmp("CAPABILITY", arg)) {
 787                parse_capability(imap, s);
 788        } else if (!strcmp("ALERT", arg)) {
 789                /* RFC2060 says that these messages MUST be displayed
 790                 * to the user
 791                 */
 792                for (; isspace((unsigned char)*p); p++);
 793                fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
 794        } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
 795                if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg)) ||
 796                    !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
 797                        fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
 798                        return RESP_BAD;
 799                }
 800        }
 801        return RESP_OK;
 802}
 803
 804static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
 805{
 806        struct imap *imap = ctx->imap;
 807        struct imap_cmd *cmdp, **pcmdp, *ncmdp;
 808        char *cmd, *arg, *arg1, *p;
 809        int n, resp, resp2, tag;
 810
 811        for (;;) {
 812                if (buffer_gets(&imap->buf, &cmd))
 813                        return RESP_BAD;
 814
 815                arg = next_arg(&cmd);
 816                if (*arg == '*') {
 817                        arg = next_arg(&cmd);
 818                        if (!arg) {
 819                                fprintf(stderr, "IMAP error: unable to parse untagged response\n");
 820                                return RESP_BAD;
 821                        }
 822
 823                        if (!strcmp("NAMESPACE", arg)) {
 824                                imap->ns_personal = parse_list(&cmd);
 825                                imap->ns_other = parse_list(&cmd);
 826                                imap->ns_shared = parse_list(&cmd);
 827                        } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
 828                                   !strcmp("NO", arg) || !strcmp("BYE", arg)) {
 829                                if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
 830                                        return resp;
 831                        } else if (!strcmp("CAPABILITY", arg))
 832                                parse_capability(imap, cmd);
 833                        else if ((arg1 = next_arg(&cmd))) {
 834                                if (!strcmp("EXISTS", arg1))
 835                                        ctx->gen.count = atoi(arg);
 836                                else if (!strcmp("RECENT", arg1))
 837                                        ctx->gen.recent = atoi(arg);
 838                        } else {
 839                                fprintf(stderr, "IMAP error: unable to parse untagged response\n");
 840                                return RESP_BAD;
 841                        }
 842                } else if (!imap->in_progress) {
 843                        fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
 844                        return RESP_BAD;
 845                } else if (*arg == '+') {
 846                        /* This can happen only with the last command underway, as
 847                           it enforces a round-trip. */
 848                        cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
 849                               offsetof(struct imap_cmd, next));
 850                        if (cmdp->cb.data) {
 851                                n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
 852                                free(cmdp->cb.data);
 853                                cmdp->cb.data = NULL;
 854                                if (n != (int)cmdp->cb.dlen)
 855                                        return RESP_BAD;
 856                        } else if (cmdp->cb.cont) {
 857                                if (cmdp->cb.cont(ctx, cmdp, cmd))
 858                                        return RESP_BAD;
 859                        } else {
 860                                fprintf(stderr, "IMAP error: unexpected command continuation request\n");
 861                                return RESP_BAD;
 862                        }
 863                        if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
 864                                return RESP_BAD;
 865                        if (!cmdp->cb.cont)
 866                                imap->literal_pending = 0;
 867                        if (!tcmd)
 868                                return DRV_OK;
 869                } else {
 870                        tag = atoi(arg);
 871                        for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
 872                                if (cmdp->tag == tag)
 873                                        goto gottag;
 874                        fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
 875                        return RESP_BAD;
 876                gottag:
 877                        if (!(*pcmdp = cmdp->next))
 878                                imap->in_progress_append = pcmdp;
 879                        imap->num_in_progress--;
 880                        if (cmdp->cb.cont || cmdp->cb.data)
 881                                imap->literal_pending = 0;
 882                        arg = next_arg(&cmd);
 883                        if (!strcmp("OK", arg))
 884                                resp = DRV_OK;
 885                        else {
 886                                if (!strcmp("NO", arg)) {
 887                                        if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
 888                                                p = strchr(cmdp->cmd, '"');
 889                                                if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", (int)(strchr(p + 1, '"') - p + 1), p)) {
 890                                                        resp = RESP_BAD;
 891                                                        goto normal;
 892                                                }
 893                                                /* not waiting here violates the spec, but a server that does not
 894                                                   grok this nonetheless violates it too. */
 895                                                cmdp->cb.create = 0;
 896                                                if (!(ncmdp = issue_imap_cmd(ctx, &cmdp->cb, "%s", cmdp->cmd))) {
 897                                                        resp = RESP_BAD;
 898                                                        goto normal;
 899                                                }
 900                                                free(cmdp->cmd);
 901                                                free(cmdp);
 902                                                if (!tcmd)
 903                                                        return 0;       /* ignored */
 904                                                if (cmdp == tcmd)
 905                                                        tcmd = ncmdp;
 906                                                continue;
 907                                        }
 908                                        resp = RESP_NO;
 909                                } else /*if (!strcmp("BAD", arg))*/
 910                                        resp = RESP_BAD;
 911                                fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
 912                                         memcmp(cmdp->cmd, "LOGIN", 5) ?
 913                                                        cmdp->cmd : "LOGIN <user> <pass>",
 914                                                        arg, cmd ? cmd : "");
 915                        }
 916                        if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp)
 917                                resp = resp2;
 918                normal:
 919                        if (cmdp->cb.done)
 920                                cmdp->cb.done(ctx, cmdp, resp);
 921                        free(cmdp->cb.data);
 922                        free(cmdp->cmd);
 923                        free(cmdp);
 924                        if (!tcmd || tcmd == cmdp)
 925                                return resp;
 926                }
 927        }
 928        /* not reached */
 929}
 930
 931static void imap_close_server(struct imap_store *ictx)
 932{
 933        struct imap *imap = ictx->imap;
 934
 935        if (imap->buf.sock.fd[0] != -1) {
 936                imap_exec(ictx, NULL, "LOGOUT");
 937                socket_shutdown(&imap->buf.sock);
 938        }
 939        free_list(imap->ns_personal);
 940        free_list(imap->ns_other);
 941        free_list(imap->ns_shared);
 942        free(imap);
 943}
 944
 945static void imap_close_store(struct store *ctx)
 946{
 947        imap_close_server((struct imap_store *)ctx);
 948        free_generic_messages(ctx->msgs);
 949        free(ctx);
 950}
 951
 952static struct store *imap_open_store(struct imap_server_conf *srvc)
 953{
 954        struct imap_store *ctx;
 955        struct imap *imap;
 956        char *arg, *rsp;
 957        int s = -1, preauth;
 958
 959        ctx = xcalloc(sizeof(*ctx), 1);
 960
 961        ctx->imap = imap = xcalloc(sizeof(*imap), 1);
 962        imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
 963        imap->in_progress_append = &imap->in_progress;
 964
 965        /* open connection to IMAP server */
 966
 967        if (srvc->tunnel) {
 968                const char *argv[] = { srvc->tunnel, NULL };
 969                struct child_process tunnel = {0};
 970
 971                imap_info("Starting tunnel '%s'... ", srvc->tunnel);
 972
 973                tunnel.argv = argv;
 974                tunnel.use_shell = 1;
 975                tunnel.in = -1;
 976                tunnel.out = -1;
 977                if (start_command(&tunnel))
 978                        die("cannot start proxy %s", argv[0]);
 979
 980                imap->buf.sock.fd[0] = tunnel.out;
 981                imap->buf.sock.fd[1] = tunnel.in;
 982
 983                imap_info("ok\n");
 984        } else {
 985#ifndef NO_IPV6
 986                struct addrinfo hints, *ai0, *ai;
 987                int gai;
 988                char portstr[6];
 989
 990                snprintf(portstr, sizeof(portstr), "%hu", srvc->port);
 991
 992                memset(&hints, 0, sizeof(hints));
 993                hints.ai_socktype = SOCK_STREAM;
 994                hints.ai_protocol = IPPROTO_TCP;
 995
 996                imap_info("Resolving %s... ", srvc->host);
 997                gai = getaddrinfo(srvc->host, portstr, &hints, &ai);
 998                if (gai) {
 999                        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai));
1000                        goto bail;
1001                }
1002                imap_info("ok\n");
1003
1004                for (ai0 = ai; ai; ai = ai->ai_next) {
1005                        char addr[NI_MAXHOST];
1006
1007                        s = socket(ai->ai_family, ai->ai_socktype,
1008                                   ai->ai_protocol);
1009                        if (s < 0)
1010                                continue;
1011
1012                        getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1013                                    sizeof(addr), NULL, 0, NI_NUMERICHOST);
1014                        imap_info("Connecting to [%s]:%s... ", addr, portstr);
1015
1016                        if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1017                                close(s);
1018                                s = -1;
1019                                perror("connect");
1020                                continue;
1021                        }
1022
1023                        break;
1024                }
1025                freeaddrinfo(ai0);
1026#else /* NO_IPV6 */
1027                struct hostent *he;
1028                struct sockaddr_in addr;
1029
1030                memset(&addr, 0, sizeof(addr));
1031                addr.sin_port = htons(srvc->port);
1032                addr.sin_family = AF_INET;
1033
1034                imap_info("Resolving %s... ", srvc->host);
1035                he = gethostbyname(srvc->host);
1036                if (!he) {
1037                        perror("gethostbyname");
1038                        goto bail;
1039                }
1040                imap_info("ok\n");
1041
1042                addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
1043
1044                s = socket(PF_INET, SOCK_STREAM, 0);
1045
1046                imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1047                if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1048                        close(s);
1049                        s = -1;
1050                        perror("connect");
1051                }
1052#endif
1053                if (s < 0) {
1054                        fputs("Error: unable to connect to server.\n", stderr);
1055                        goto bail;
1056                }
1057
1058                imap->buf.sock.fd[0] = s;
1059                imap->buf.sock.fd[1] = dup(s);
1060
1061                if (srvc->use_ssl &&
1062                    ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1063                        close(s);
1064                        goto bail;
1065                }
1066                imap_info("ok\n");
1067        }
1068
1069        /* read the greeting string */
1070        if (buffer_gets(&imap->buf, &rsp)) {
1071                fprintf(stderr, "IMAP error: no greeting response\n");
1072                goto bail;
1073        }
1074        arg = next_arg(&rsp);
1075        if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) {
1076                fprintf(stderr, "IMAP error: invalid greeting response\n");
1077                goto bail;
1078        }
1079        preauth = 0;
1080        if (!strcmp("PREAUTH", arg))
1081                preauth = 1;
1082        else if (strcmp("OK", arg) != 0) {
1083                fprintf(stderr, "IMAP error: unknown greeting response\n");
1084                goto bail;
1085        }
1086        parse_response_code(ctx, NULL, rsp);
1087        if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1088                goto bail;
1089
1090        if (!preauth) {
1091#ifndef NO_OPENSSL
1092                if (!srvc->use_ssl && CAP(STARTTLS)) {
1093                        if (imap_exec(ctx, 0, "STARTTLS") != RESP_OK)
1094                                goto bail;
1095                        if (ssl_socket_connect(&imap->buf.sock, 1,
1096                                               srvc->ssl_verify))
1097                                goto bail;
1098                        /* capabilities may have changed, so get the new capabilities */
1099                        if (imap_exec(ctx, 0, "CAPABILITY") != RESP_OK)
1100                                goto bail;
1101                }
1102#endif
1103                imap_info("Logging in...\n");
1104                if (!srvc->user) {
1105                        fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
1106                        goto bail;
1107                }
1108                if (!srvc->pass) {
1109                        char prompt[80];
1110                        sprintf(prompt, "Password (%s@%s): ", srvc->user, srvc->host);
1111                        arg = getpass(prompt);
1112                        if (!arg) {
1113                                perror("getpass");
1114                                exit(1);
1115                        }
1116                        if (!*arg) {
1117                                fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
1118                                goto bail;
1119                        }
1120                        /*
1121                         * getpass() returns a pointer to a static buffer.  make a copy
1122                         * for long term storage.
1123                         */
1124                        srvc->pass = xstrdup(arg);
1125                }
1126                if (CAP(NOLOGIN)) {
1127                        fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
1128                        goto bail;
1129                }
1130                if (!imap->buf.sock.ssl)
1131                        imap_warn("*** IMAP Warning *** Password is being "
1132                                  "sent in the clear\n");
1133                if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) {
1134                        fprintf(stderr, "IMAP error: LOGIN failed\n");
1135                        goto bail;
1136                }
1137        } /* !preauth */
1138
1139        ctx->prefix = "";
1140        ctx->trashnc = 1;
1141        return (struct store *)ctx;
1142
1143bail:
1144        imap_close_store(&ctx->gen);
1145        return NULL;
1146}
1147
1148static int imap_make_flags(int flags, char *buf)
1149{
1150        const char *s;
1151        unsigned i, d;
1152
1153        for (i = d = 0; i < ARRAY_SIZE(Flags); i++)
1154                if (flags & (1 << i)) {
1155                        buf[d++] = ' ';
1156                        buf[d++] = '\\';
1157                        for (s = Flags[i]; *s; s++)
1158                                buf[d++] = *s;
1159                }
1160        buf[0] = '(';
1161        buf[d++] = ')';
1162        return d;
1163}
1164
1165static int imap_store_msg(struct store *gctx, struct msg_data *data)
1166{
1167        struct imap_store *ctx = (struct imap_store *)gctx;
1168        struct imap *imap = ctx->imap;
1169        struct imap_cmd_cb cb;
1170        const char *prefix, *box;
1171        int ret, d;
1172        char flagstr[128];
1173
1174        memset(&cb, 0, sizeof(cb));
1175
1176        cb.dlen = data->len;
1177        cb.data = xmalloc(cb.dlen);
1178        memcpy(cb.data, data->data, data->len);
1179
1180        d = 0;
1181        if (data->flags) {
1182                d = imap_make_flags(data->flags, flagstr);
1183                flagstr[d++] = ' ';
1184        }
1185        flagstr[d] = 0;
1186
1187        box = gctx->name;
1188        prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1189        cb.create = 0;
1190        ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr);
1191        imap->caps = imap->rcaps;
1192        if (ret != DRV_OK)
1193                return ret;
1194        gctx->count++;
1195
1196        return DRV_OK;
1197}
1198
1199static void encode_html_chars(struct strbuf *p)
1200{
1201        int i;
1202        for (i = 0; i < p->len; i++) {
1203                if (p->buf[i] == '&')
1204                        strbuf_splice(p, i, 1, "&amp;", 5);
1205                if (p->buf[i] == '<')
1206                        strbuf_splice(p, i, 1, "&lt;", 4);
1207                if (p->buf[i] == '>')
1208                        strbuf_splice(p, i, 1, "&gt;", 4);
1209                if (p->buf[i] == '"')
1210                        strbuf_splice(p, i, 1, "&quot;", 6);
1211        }
1212}
1213static void wrap_in_html(struct msg_data *msg)
1214{
1215        struct strbuf buf = STRBUF_INIT;
1216        struct strbuf **lines;
1217        struct strbuf **p;
1218        static char *content_type = "Content-Type: text/html;\n";
1219        static char *pre_open = "<pre>\n";
1220        static char *pre_close = "</pre>\n";
1221        int added_header = 0;
1222
1223        strbuf_attach(&buf, msg->data, msg->len, msg->len);
1224        lines = strbuf_split(&buf, '\n');
1225        strbuf_release(&buf);
1226        for (p = lines; *p; p++) {
1227                if (! added_header) {
1228                        if ((*p)->len == 1 && *((*p)->buf) == '\n') {
1229                                strbuf_addstr(&buf, content_type);
1230                                strbuf_addbuf(&buf, *p);
1231                                strbuf_addstr(&buf, pre_open);
1232                                added_header = 1;
1233                                continue;
1234                        }
1235                }
1236                else
1237                        encode_html_chars(*p);
1238                strbuf_addbuf(&buf, *p);
1239        }
1240        strbuf_addstr(&buf, pre_close);
1241        strbuf_list_free(lines);
1242        msg->len  = buf.len;
1243        msg->data = strbuf_detach(&buf, NULL);
1244}
1245
1246#define CHUNKSIZE 0x1000
1247
1248static int read_message(FILE *f, struct msg_data *msg)
1249{
1250        struct strbuf buf = STRBUF_INIT;
1251
1252        memset(msg, 0, sizeof(*msg));
1253
1254        do {
1255                if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
1256                        break;
1257        } while (!feof(f));
1258
1259        msg->len  = buf.len;
1260        msg->data = strbuf_detach(&buf, NULL);
1261        return msg->len;
1262}
1263
1264static int count_messages(struct msg_data *msg)
1265{
1266        int count = 0;
1267        char *p = msg->data;
1268
1269        while (1) {
1270                if (!prefixcmp(p, "From ")) {
1271                        count++;
1272                        p += 5;
1273                }
1274                p = strstr(p+5, "\nFrom ");
1275                if (!p)
1276                        break;
1277                p++;
1278        }
1279        return count;
1280}
1281
1282static int split_msg(struct msg_data *all_msgs, struct msg_data *msg, int *ofs)
1283{
1284        char *p, *data;
1285
1286        memset(msg, 0, sizeof *msg);
1287        if (*ofs >= all_msgs->len)
1288                return 0;
1289
1290        data = &all_msgs->data[*ofs];
1291        msg->len = all_msgs->len - *ofs;
1292
1293        if (msg->len < 5 || prefixcmp(data, "From "))
1294                return 0;
1295
1296        p = strchr(data, '\n');
1297        if (p) {
1298                p = &p[1];
1299                msg->len -= p-data;
1300                *ofs += p-data;
1301                data = p;
1302        }
1303
1304        p = strstr(data, "\nFrom ");
1305        if (p)
1306                msg->len = &p[1] - data;
1307
1308        msg->data = xmemdupz(data, msg->len);
1309        *ofs += msg->len;
1310        return 1;
1311}
1312
1313static struct imap_server_conf server = {
1314        NULL,   /* name */
1315        NULL,   /* tunnel */
1316        NULL,   /* host */
1317        0,      /* port */
1318        NULL,   /* user */
1319        NULL,   /* pass */
1320        0,      /* use_ssl */
1321        1,      /* ssl_verify */
1322        0,      /* use_html */
1323};
1324
1325static char *imap_folder;
1326
1327static int git_imap_config(const char *key, const char *val, void *cb)
1328{
1329        char imap_key[] = "imap.";
1330
1331        if (strncmp(key, imap_key, sizeof imap_key - 1))
1332                return 0;
1333
1334        if (!val)
1335                return config_error_nonbool(key);
1336
1337        key += sizeof imap_key - 1;
1338
1339        if (!strcmp("folder", key)) {
1340                imap_folder = xstrdup(val);
1341        } else if (!strcmp("host", key)) {
1342                if (!prefixcmp(val, "imap:"))
1343                        val += 5;
1344                else if (!prefixcmp(val, "imaps:")) {
1345                        val += 6;
1346                        server.use_ssl = 1;
1347                }
1348                if (!prefixcmp(val, "//"))
1349                        val += 2;
1350                server.host = xstrdup(val);
1351        } else if (!strcmp("user", key))
1352                server.user = xstrdup(val);
1353        else if (!strcmp("pass", key))
1354                server.pass = xstrdup(val);
1355        else if (!strcmp("port", key))
1356                server.port = git_config_int(key, val);
1357        else if (!strcmp("tunnel", key))
1358                server.tunnel = xstrdup(val);
1359        else if (!strcmp("sslverify", key))
1360                server.ssl_verify = git_config_bool(key, val);
1361        else if (!strcmp("preformattedHTML", key))
1362                server.use_html = git_config_bool(key, val);
1363        return 0;
1364}
1365
1366int main(int argc, char **argv)
1367{
1368        struct msg_data all_msgs, msg;
1369        struct store *ctx = NULL;
1370        int ofs = 0;
1371        int r;
1372        int total, n = 0;
1373        int nongit_ok;
1374
1375        git_extract_argv0_path(argv[0]);
1376
1377        if (argc != 1)
1378                usage(imap_send_usage);
1379
1380        setup_git_directory_gently(&nongit_ok);
1381        git_config(git_imap_config, NULL);
1382
1383        if (!server.port)
1384                server.port = server.use_ssl ? 993 : 143;
1385
1386        if (!imap_folder) {
1387                fprintf(stderr, "no imap store specified\n");
1388                return 1;
1389        }
1390        if (!server.host) {
1391                if (!server.tunnel) {
1392                        fprintf(stderr, "no imap host specified\n");
1393                        return 1;
1394                }
1395                server.host = "tunnel";
1396        }
1397
1398        /* read the messages */
1399        if (!read_message(stdin, &all_msgs)) {
1400                fprintf(stderr, "nothing to send\n");
1401                return 1;
1402        }
1403
1404        total = count_messages(&all_msgs);
1405        if (!total) {
1406                fprintf(stderr, "no messages to send\n");
1407                return 1;
1408        }
1409
1410        /* write it to the imap server */
1411        ctx = imap_open_store(&server);
1412        if (!ctx) {
1413                fprintf(stderr, "failed to open store\n");
1414                return 1;
1415        }
1416
1417        fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1418        ctx->name = imap_folder;
1419        while (1) {
1420                unsigned percent = n * 100 / total;
1421                fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1422                if (!split_msg(&all_msgs, &msg, &ofs))
1423                        break;
1424                if (server.use_html)
1425                        wrap_in_html(&msg);
1426                r = imap_store_msg(ctx, &msg);
1427                if (r != DRV_OK)
1428                        break;
1429                n++;
1430        }
1431        fprintf(stderr, "\n");
1432
1433        imap_close_store(ctx);
1434
1435        return 0;
1436}