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