block-sha1: avoid pointer conversion that violates alignment constraints
[git/raj.git] / imap-send.c
blob3847fd151df6e9f41fdb0b95573882a7d9ac992a
1 /*
2 * git-imap-send - drops patches into an imap Drafts folder
3 * derived from isync/mbsync - mailbox synchronizer
5 * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
6 * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
7 * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
8 * Copyright (C) 2006 Mike McCormack
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "cache.h"
26 #include "exec_cmd.h"
27 #ifdef NO_OPENSSL
28 typedef void *SSL;
29 #endif
31 struct 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;
40 struct string_list {
41 struct string_list *next;
42 char string[1];
45 struct 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 */
56 struct group_conf {
57 struct group_conf *next;
58 char *name;
59 struct string_list *channels;
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 */
67 struct 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;
75 struct store {
76 struct store_conf *conf; /* foreign */
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 */
89 struct msg_data {
90 char *data;
91 int len;
92 unsigned char flags;
93 unsigned int crlf:1;
96 #define DRV_OK 0
97 #define DRV_MSG_BAD -1
98 #define DRV_BOX_BAD -2
99 #define DRV_STORE_BAD -3
101 static int Verbose, Quiet;
103 static void imap_info(const char *, ...);
104 static void imap_warn(const char *, ...);
106 static char *next_arg(char **);
108 static void free_generic_messages(struct message *);
110 static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
112 static int nfvasprintf(char **strp, const char *fmt, va_list ap)
114 int len;
115 char tmp[8192];
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;
126 static void arc4_init(void);
127 static unsigned char arc4_getbyte(void);
129 struct 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 int use_html;
141 struct imap_store_conf {
142 struct store_conf gen;
143 struct imap_server_conf *server;
144 unsigned use_namespace:1;
147 #define NIL (void *)0x1
148 #define LIST (void *)0x2
150 struct imap_list {
151 struct imap_list *next, *child;
152 char *val;
153 int len;
156 struct imap_socket {
157 int fd;
158 SSL *ssl;
161 struct imap_buffer {
162 struct imap_socket sock;
163 int bytes;
164 int offset;
165 char buf[1024];
168 struct imap_cmd;
170 struct imap {
171 int uidnext; /* from SELECT responses */
172 struct imap_list *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
173 unsigned caps, rcaps; /* CAPABILITY results */
174 /* command queue */
175 int nexttag, num_in_progress, literal_pending;
176 struct imap_cmd *in_progress, **in_progress_append;
177 struct imap_buffer buf; /* this is BIG, so put it last */
180 struct imap_store {
181 struct store gen;
182 int uidvalidity;
183 struct imap *imap;
184 const char *prefix;
185 unsigned /*currentnc:1,*/ trashnc:1;
188 struct imap_cmd_cb {
189 int (*cont)(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt);
190 void (*done)(struct imap_store *ctx, struct imap_cmd *cmd, int response);
191 void *ctx;
192 char *data;
193 int dlen;
194 int uid;
195 unsigned create:1, trycreate:1;
198 struct imap_cmd {
199 struct imap_cmd *next;
200 struct imap_cmd_cb cb;
201 char *cmd;
202 int tag;
205 #define CAP(cap) (imap->caps & (1 << (cap)))
207 enum CAPABILITY {
208 NOLOGIN = 0,
209 UIDPLUS,
210 LITERALPLUS,
211 NAMESPACE,
212 STARTTLS,
215 static const char *cap_list[] = {
216 "LOGINDISABLED",
217 "UIDPLUS",
218 "LITERAL+",
219 "NAMESPACE",
220 "STARTTLS",
223 #define RESP_OK 0
224 #define RESP_NO 1
225 #define RESP_BAD 2
227 static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd);
230 static const char *Flags[] = {
231 "Draft",
232 "Flagged",
233 "Answered",
234 "Seen",
235 "Deleted",
238 #ifndef NO_OPENSSL
239 static void ssl_socket_perror(const char *func)
241 fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), NULL));
243 #endif
245 static void socket_perror(const char *func, struct imap_socket *sock, int ret)
247 #ifndef NO_OPENSSL
248 if (sock->ssl) {
249 int sslerr = SSL_get_error(sock->ssl, ret);
250 switch (sslerr) {
251 case SSL_ERROR_NONE:
252 break;
253 case SSL_ERROR_SYSCALL:
254 perror("SSL_connect");
255 break;
256 default:
257 ssl_socket_perror("SSL_connect");
258 break;
260 } else
261 #endif
263 if (ret < 0)
264 perror(func);
265 else
266 fprintf(stderr, "%s: unexpected EOF\n", func);
270 static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
272 #ifdef NO_OPENSSL
273 fprintf(stderr, "SSL requested but SSL support not compiled in\n");
274 return -1;
275 #else
276 SSL_METHOD *meth;
277 SSL_CTX *ctx;
278 int ret;
280 SSL_library_init();
281 SSL_load_error_strings();
283 if (use_tls_only)
284 meth = TLSv1_method();
285 else
286 meth = SSLv23_method();
288 if (!meth) {
289 ssl_socket_perror("SSLv23_method");
290 return -1;
293 ctx = SSL_CTX_new(meth);
295 if (verify)
296 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
298 if (!SSL_CTX_set_default_verify_paths(ctx)) {
299 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
300 return -1;
302 sock->ssl = SSL_new(ctx);
303 if (!sock->ssl) {
304 ssl_socket_perror("SSL_new");
305 return -1;
307 if (!SSL_set_fd(sock->ssl, sock->fd)) {
308 ssl_socket_perror("SSL_set_fd");
309 return -1;
312 ret = SSL_connect(sock->ssl);
313 if (ret <= 0) {
314 socket_perror("SSL_connect", sock, ret);
315 return -1;
318 return 0;
319 #endif
322 static int socket_read(struct imap_socket *sock, char *buf, int len)
324 ssize_t n;
325 #ifndef NO_OPENSSL
326 if (sock->ssl)
327 n = SSL_read(sock->ssl, buf, len);
328 else
329 #endif
330 n = xread(sock->fd, buf, len);
331 if (n <= 0) {
332 socket_perror("read", sock, n);
333 close(sock->fd);
334 sock->fd = -1;
336 return n;
339 static int socket_write(struct imap_socket *sock, const char *buf, int len)
341 int n;
342 #ifndef NO_OPENSSL
343 if (sock->ssl)
344 n = SSL_write(sock->ssl, buf, len);
345 else
346 #endif
347 n = write_in_full(sock->fd, buf, len);
348 if (n != len) {
349 socket_perror("write", sock, n);
350 close(sock->fd);
351 sock->fd = -1;
353 return n;
356 static void socket_shutdown(struct imap_socket *sock)
358 #ifndef NO_OPENSSL
359 if (sock->ssl) {
360 SSL_shutdown(sock->ssl);
361 SSL_free(sock->ssl);
363 #endif
364 close(sock->fd);
367 /* simple line buffering */
368 static int buffer_gets(struct imap_buffer *b, char **s)
370 int n;
371 int start = b->offset;
373 *s = b->buf + start;
375 for (;;) {
376 /* make sure we have enough data to read the \r\n sequence */
377 if (b->offset + 1 >= b->bytes) {
378 if (start) {
379 /* shift down used bytes */
380 *s = b->buf;
382 assert(start <= b->bytes);
383 n = b->bytes - start;
385 if (n)
386 memmove(b->buf, b->buf + start, n);
387 b->offset -= start;
388 b->bytes = n;
389 start = 0;
392 n = socket_read(&b->sock, b->buf + b->bytes,
393 sizeof(b->buf) - b->bytes);
395 if (n <= 0)
396 return -1;
398 b->bytes += n;
401 if (b->buf[b->offset] == '\r') {
402 assert(b->offset + 1 < b->bytes);
403 if (b->buf[b->offset + 1] == '\n') {
404 b->buf[b->offset] = 0; /* terminate the string */
405 b->offset += 2; /* next line */
406 if (Verbose)
407 puts(*s);
408 return 0;
412 b->offset++;
414 /* not reached */
417 static void imap_info(const char *msg, ...)
419 va_list va;
421 if (!Quiet) {
422 va_start(va, msg);
423 vprintf(msg, va);
424 va_end(va);
425 fflush(stdout);
429 static void imap_warn(const char *msg, ...)
431 va_list va;
433 if (Quiet < 2) {
434 va_start(va, msg);
435 vfprintf(stderr, msg, va);
436 va_end(va);
440 static char *next_arg(char **s)
442 char *ret;
444 if (!s || !*s)
445 return NULL;
446 while (isspace((unsigned char) **s))
447 (*s)++;
448 if (!**s) {
449 *s = NULL;
450 return NULL;
452 if (**s == '"') {
453 ++*s;
454 ret = *s;
455 *s = strchr(*s, '"');
456 } else {
457 ret = *s;
458 while (**s && !isspace((unsigned char) **s))
459 (*s)++;
461 if (*s) {
462 if (**s)
463 *(*s)++ = 0;
464 if (!**s)
465 *s = NULL;
467 return ret;
470 static void free_generic_messages(struct message *msgs)
472 struct message *tmsg;
474 for (; msgs; msgs = tmsg) {
475 tmsg = msgs->next;
476 free(msgs);
480 static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
482 int ret;
483 va_list va;
485 va_start(va, fmt);
486 if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
487 die("Fatal: buffer too small. Please report a bug.");
488 va_end(va);
489 return ret;
492 static struct {
493 unsigned char i, j, s[256];
494 } rs;
496 static void arc4_init(void)
498 int i, fd;
499 unsigned char j, si, dat[128];
501 if ((fd = open("/dev/urandom", O_RDONLY)) < 0 && (fd = open("/dev/random", O_RDONLY)) < 0) {
502 fprintf(stderr, "Fatal: no random number source available.\n");
503 exit(3);
505 if (read_in_full(fd, dat, 128) != 128) {
506 fprintf(stderr, "Fatal: cannot read random number source.\n");
507 exit(3);
509 close(fd);
511 for (i = 0; i < 256; i++)
512 rs.s[i] = i;
513 for (i = j = 0; i < 256; i++) {
514 si = rs.s[i];
515 j += si + dat[i & 127];
516 rs.s[i] = rs.s[j];
517 rs.s[j] = si;
519 rs.i = rs.j = 0;
521 for (i = 0; i < 256; i++)
522 arc4_getbyte();
525 static unsigned char arc4_getbyte(void)
527 unsigned char si, sj;
529 rs.i++;
530 si = rs.s[rs.i];
531 rs.j += si;
532 sj = rs.s[rs.j];
533 rs.s[rs.i] = sj;
534 rs.s[rs.j] = si;
535 return rs.s[(si + sj) & 0xff];
538 static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
539 struct imap_cmd_cb *cb,
540 const char *fmt, va_list ap)
542 struct imap *imap = ctx->imap;
543 struct imap_cmd *cmd;
544 int n, bufl;
545 char buf[1024];
547 cmd = xmalloc(sizeof(struct imap_cmd));
548 nfvasprintf(&cmd->cmd, fmt, ap);
549 cmd->tag = ++imap->nexttag;
551 if (cb)
552 cmd->cb = *cb;
553 else
554 memset(&cmd->cb, 0, sizeof(cmd->cb));
556 while (imap->literal_pending)
557 get_cmd_result(ctx, NULL);
559 bufl = nfsnprintf(buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
560 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
561 cmd->tag, cmd->cmd, cmd->cb.dlen);
562 if (Verbose) {
563 if (imap->num_in_progress)
564 printf("(%d in progress) ", imap->num_in_progress);
565 if (memcmp(cmd->cmd, "LOGIN", 5))
566 printf(">>> %s", buf);
567 else
568 printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
570 if (socket_write(&imap->buf.sock, buf, bufl) != bufl) {
571 free(cmd->cmd);
572 free(cmd);
573 if (cb)
574 free(cb->data);
575 return NULL;
577 if (cmd->cb.data) {
578 if (CAP(LITERALPLUS)) {
579 n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);
580 free(cmd->cb.data);
581 if (n != cmd->cb.dlen ||
582 socket_write(&imap->buf.sock, "\r\n", 2) != 2) {
583 free(cmd->cmd);
584 free(cmd);
585 return NULL;
587 cmd->cb.data = NULL;
588 } else
589 imap->literal_pending = 1;
590 } else if (cmd->cb.cont)
591 imap->literal_pending = 1;
592 cmd->next = NULL;
593 *imap->in_progress_append = cmd;
594 imap->in_progress_append = &cmd->next;
595 imap->num_in_progress++;
596 return cmd;
599 static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
600 struct imap_cmd_cb *cb,
601 const char *fmt, ...)
603 struct imap_cmd *ret;
604 va_list ap;
606 va_start(ap, fmt);
607 ret = v_issue_imap_cmd(ctx, cb, fmt, ap);
608 va_end(ap);
609 return ret;
612 static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb,
613 const char *fmt, ...)
615 va_list ap;
616 struct imap_cmd *cmdp;
618 va_start(ap, fmt);
619 cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
620 va_end(ap);
621 if (!cmdp)
622 return RESP_BAD;
624 return get_cmd_result(ctx, cmdp);
627 static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb,
628 const char *fmt, ...)
630 va_list ap;
631 struct imap_cmd *cmdp;
633 va_start(ap, fmt);
634 cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
635 va_end(ap);
636 if (!cmdp)
637 return DRV_STORE_BAD;
639 switch (get_cmd_result(ctx, cmdp)) {
640 case RESP_BAD: return DRV_STORE_BAD;
641 case RESP_NO: return DRV_MSG_BAD;
642 default: return DRV_OK;
646 static int is_atom(struct imap_list *list)
648 return list && list->val && list->val != NIL && list->val != LIST;
651 static int is_list(struct imap_list *list)
653 return list && list->val == LIST;
656 static void free_list(struct imap_list *list)
658 struct imap_list *tmp;
660 for (; list; list = tmp) {
661 tmp = list->next;
662 if (is_list(list))
663 free_list(list->child);
664 else if (is_atom(list))
665 free(list->val);
666 free(list);
670 static int parse_imap_list_l(struct imap *imap, char **sp, struct imap_list **curp, int level)
672 struct imap_list *cur;
673 char *s = *sp, *p;
674 int n, bytes;
676 for (;;) {
677 while (isspace((unsigned char)*s))
678 s++;
679 if (level && *s == ')') {
680 s++;
681 break;
683 *curp = cur = xmalloc(sizeof(*cur));
684 curp = &cur->next;
685 cur->val = NULL; /* for clean bail */
686 if (*s == '(') {
687 /* sublist */
688 s++;
689 cur->val = LIST;
690 if (parse_imap_list_l(imap, &s, &cur->child, level + 1))
691 goto bail;
692 } else if (imap && *s == '{') {
693 /* literal */
694 bytes = cur->len = strtol(s + 1, &s, 10);
695 if (*s != '}')
696 goto bail;
698 s = cur->val = xmalloc(cur->len);
700 /* dump whats left over in the input buffer */
701 n = imap->buf.bytes - imap->buf.offset;
703 if (n > bytes)
704 /* the entire message fit in the buffer */
705 n = bytes;
707 memcpy(s, imap->buf.buf + imap->buf.offset, n);
708 s += n;
709 bytes -= n;
711 /* mark that we used part of the buffer */
712 imap->buf.offset += n;
714 /* now read the rest of the message */
715 while (bytes > 0) {
716 if ((n = socket_read(&imap->buf.sock, s, bytes)) <= 0)
717 goto bail;
718 s += n;
719 bytes -= n;
722 if (buffer_gets(&imap->buf, &s))
723 goto bail;
724 } else if (*s == '"') {
725 /* quoted string */
726 s++;
727 p = s;
728 for (; *s != '"'; s++)
729 if (!*s)
730 goto bail;
731 cur->len = s - p;
732 s++;
733 cur->val = xmemdupz(p, cur->len);
734 } else {
735 /* atom */
736 p = s;
737 for (; *s && !isspace((unsigned char)*s); s++)
738 if (level && *s == ')')
739 break;
740 cur->len = s - p;
741 if (cur->len == 3 && !memcmp("NIL", p, 3))
742 cur->val = NIL;
743 else
744 cur->val = xmemdupz(p, cur->len);
747 if (!level)
748 break;
749 if (!*s)
750 goto bail;
752 *sp = s;
753 *curp = NULL;
754 return 0;
756 bail:
757 *curp = NULL;
758 return -1;
761 static struct imap_list *parse_imap_list(struct imap *imap, char **sp)
763 struct imap_list *head;
765 if (!parse_imap_list_l(imap, sp, &head, 0))
766 return head;
767 free_list(head);
768 return NULL;
771 static struct imap_list *parse_list(char **sp)
773 return parse_imap_list(NULL, sp);
776 static void parse_capability(struct imap *imap, char *cmd)
778 char *arg;
779 unsigned i;
781 imap->caps = 0x80000000;
782 while ((arg = next_arg(&cmd)))
783 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
784 if (!strcmp(cap_list[i], arg))
785 imap->caps |= 1 << i;
786 imap->rcaps = imap->caps;
789 static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
790 char *s)
792 struct imap *imap = ctx->imap;
793 char *arg, *p;
795 if (*s != '[')
796 return RESP_OK; /* no response code */
797 s++;
798 if (!(p = strchr(s, ']'))) {
799 fprintf(stderr, "IMAP error: malformed response code\n");
800 return RESP_BAD;
802 *p++ = 0;
803 arg = next_arg(&s);
804 if (!strcmp("UIDVALIDITY", arg)) {
805 if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg))) {
806 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
807 return RESP_BAD;
809 } else if (!strcmp("UIDNEXT", arg)) {
810 if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
811 fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
812 return RESP_BAD;
814 } else if (!strcmp("CAPABILITY", arg)) {
815 parse_capability(imap, s);
816 } else if (!strcmp("ALERT", arg)) {
817 /* RFC2060 says that these messages MUST be displayed
818 * to the user
820 for (; isspace((unsigned char)*p); p++);
821 fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
822 } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
823 if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg)) ||
824 !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
825 fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
826 return RESP_BAD;
829 return RESP_OK;
832 static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
834 struct imap *imap = ctx->imap;
835 struct imap_cmd *cmdp, **pcmdp, *ncmdp;
836 char *cmd, *arg, *arg1, *p;
837 int n, resp, resp2, tag;
839 for (;;) {
840 if (buffer_gets(&imap->buf, &cmd))
841 return RESP_BAD;
843 arg = next_arg(&cmd);
844 if (*arg == '*') {
845 arg = next_arg(&cmd);
846 if (!arg) {
847 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
848 return RESP_BAD;
851 if (!strcmp("NAMESPACE", arg)) {
852 imap->ns_personal = parse_list(&cmd);
853 imap->ns_other = parse_list(&cmd);
854 imap->ns_shared = parse_list(&cmd);
855 } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
856 !strcmp("NO", arg) || !strcmp("BYE", arg)) {
857 if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
858 return resp;
859 } else if (!strcmp("CAPABILITY", arg))
860 parse_capability(imap, cmd);
861 else if ((arg1 = next_arg(&cmd))) {
862 if (!strcmp("EXISTS", arg1))
863 ctx->gen.count = atoi(arg);
864 else if (!strcmp("RECENT", arg1))
865 ctx->gen.recent = atoi(arg);
866 } else {
867 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
868 return RESP_BAD;
870 } else if (!imap->in_progress) {
871 fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
872 return RESP_BAD;
873 } else if (*arg == '+') {
874 /* This can happen only with the last command underway, as
875 it enforces a round-trip. */
876 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
877 offsetof(struct imap_cmd, next));
878 if (cmdp->cb.data) {
879 n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
880 free(cmdp->cb.data);
881 cmdp->cb.data = NULL;
882 if (n != (int)cmdp->cb.dlen)
883 return RESP_BAD;
884 } else if (cmdp->cb.cont) {
885 if (cmdp->cb.cont(ctx, cmdp, cmd))
886 return RESP_BAD;
887 } else {
888 fprintf(stderr, "IMAP error: unexpected command continuation request\n");
889 return RESP_BAD;
891 if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
892 return RESP_BAD;
893 if (!cmdp->cb.cont)
894 imap->literal_pending = 0;
895 if (!tcmd)
896 return DRV_OK;
897 } else {
898 tag = atoi(arg);
899 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
900 if (cmdp->tag == tag)
901 goto gottag;
902 fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
903 return RESP_BAD;
904 gottag:
905 if (!(*pcmdp = cmdp->next))
906 imap->in_progress_append = pcmdp;
907 imap->num_in_progress--;
908 if (cmdp->cb.cont || cmdp->cb.data)
909 imap->literal_pending = 0;
910 arg = next_arg(&cmd);
911 if (!strcmp("OK", arg))
912 resp = DRV_OK;
913 else {
914 if (!strcmp("NO", arg)) {
915 if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
916 p = strchr(cmdp->cmd, '"');
917 if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", strchr(p + 1, '"') - p + 1, p)) {
918 resp = RESP_BAD;
919 goto normal;
921 /* not waiting here violates the spec, but a server that does not
922 grok this nonetheless violates it too. */
923 cmdp->cb.create = 0;
924 if (!(ncmdp = issue_imap_cmd(ctx, &cmdp->cb, "%s", cmdp->cmd))) {
925 resp = RESP_BAD;
926 goto normal;
928 free(cmdp->cmd);
929 free(cmdp);
930 if (!tcmd)
931 return 0; /* ignored */
932 if (cmdp == tcmd)
933 tcmd = ncmdp;
934 continue;
936 resp = RESP_NO;
937 } else /*if (!strcmp("BAD", arg))*/
938 resp = RESP_BAD;
939 fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
940 memcmp(cmdp->cmd, "LOGIN", 5) ?
941 cmdp->cmd : "LOGIN <user> <pass>",
942 arg, cmd ? cmd : "");
944 if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp)
945 resp = resp2;
946 normal:
947 if (cmdp->cb.done)
948 cmdp->cb.done(ctx, cmdp, resp);
949 free(cmdp->cb.data);
950 free(cmdp->cmd);
951 free(cmdp);
952 if (!tcmd || tcmd == cmdp)
953 return resp;
956 /* not reached */
959 static void imap_close_server(struct imap_store *ictx)
961 struct imap *imap = ictx->imap;
963 if (imap->buf.sock.fd != -1) {
964 imap_exec(ictx, NULL, "LOGOUT");
965 socket_shutdown(&imap->buf.sock);
967 free_list(imap->ns_personal);
968 free_list(imap->ns_other);
969 free_list(imap->ns_shared);
970 free(imap);
973 static void imap_close_store(struct store *ctx)
975 imap_close_server((struct imap_store *)ctx);
976 free_generic_messages(ctx->msgs);
977 free(ctx);
980 static struct store *imap_open_store(struct imap_server_conf *srvc)
982 struct imap_store *ctx;
983 struct imap *imap;
984 char *arg, *rsp;
985 int s = -1, a[2], preauth;
986 pid_t pid;
988 ctx = xcalloc(sizeof(*ctx), 1);
990 ctx->imap = imap = xcalloc(sizeof(*imap), 1);
991 imap->buf.sock.fd = -1;
992 imap->in_progress_append = &imap->in_progress;
994 /* open connection to IMAP server */
996 if (srvc->tunnel) {
997 imap_info("Starting tunnel '%s'... ", srvc->tunnel);
999 if (socketpair(PF_UNIX, SOCK_STREAM, 0, a)) {
1000 perror("socketpair");
1001 exit(1);
1004 pid = fork();
1005 if (pid < 0)
1006 _exit(127);
1007 if (!pid) {
1008 if (dup2(a[0], 0) == -1 || dup2(a[0], 1) == -1)
1009 _exit(127);
1010 close(a[0]);
1011 close(a[1]);
1012 execl("/bin/sh", "sh", "-c", srvc->tunnel, NULL);
1013 _exit(127);
1016 close(a[0]);
1018 imap->buf.sock.fd = a[1];
1020 imap_info("ok\n");
1021 } else {
1022 #ifndef NO_IPV6
1023 struct addrinfo hints, *ai0, *ai;
1024 int gai;
1025 char portstr[6];
1027 snprintf(portstr, sizeof(portstr), "%hu", srvc->port);
1029 memset(&hints, 0, sizeof(hints));
1030 hints.ai_socktype = SOCK_STREAM;
1031 hints.ai_protocol = IPPROTO_TCP;
1033 imap_info("Resolving %s... ", srvc->host);
1034 gai = getaddrinfo(srvc->host, portstr, &hints, &ai);
1035 if (gai) {
1036 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai));
1037 goto bail;
1039 imap_info("ok\n");
1041 for (ai0 = ai; ai; ai = ai->ai_next) {
1042 char addr[NI_MAXHOST];
1044 s = socket(ai->ai_family, ai->ai_socktype,
1045 ai->ai_protocol);
1046 if (s < 0)
1047 continue;
1049 getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1050 sizeof(addr), NULL, 0, NI_NUMERICHOST);
1051 imap_info("Connecting to [%s]:%s... ", addr, portstr);
1053 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1054 close(s);
1055 s = -1;
1056 perror("connect");
1057 continue;
1060 break;
1062 freeaddrinfo(ai0);
1063 #else /* NO_IPV6 */
1064 struct hostent *he;
1065 struct sockaddr_in addr;
1067 memset(&addr, 0, sizeof(addr));
1068 addr.sin_port = htons(srvc->port);
1069 addr.sin_family = AF_INET;
1071 imap_info("Resolving %s... ", srvc->host);
1072 he = gethostbyname(srvc->host);
1073 if (!he) {
1074 perror("gethostbyname");
1075 goto bail;
1077 imap_info("ok\n");
1079 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
1081 s = socket(PF_INET, SOCK_STREAM, 0);
1083 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1084 if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1085 close(s);
1086 s = -1;
1087 perror("connect");
1089 #endif
1090 if (s < 0) {
1091 fputs("Error: unable to connect to server.\n", stderr);
1092 goto bail;
1095 imap->buf.sock.fd = s;
1097 if (srvc->use_ssl &&
1098 ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1099 close(s);
1100 goto bail;
1102 imap_info("ok\n");
1105 /* read the greeting string */
1106 if (buffer_gets(&imap->buf, &rsp)) {
1107 fprintf(stderr, "IMAP error: no greeting response\n");
1108 goto bail;
1110 arg = next_arg(&rsp);
1111 if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) {
1112 fprintf(stderr, "IMAP error: invalid greeting response\n");
1113 goto bail;
1115 preauth = 0;
1116 if (!strcmp("PREAUTH", arg))
1117 preauth = 1;
1118 else if (strcmp("OK", arg) != 0) {
1119 fprintf(stderr, "IMAP error: unknown greeting response\n");
1120 goto bail;
1122 parse_response_code(ctx, NULL, rsp);
1123 if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1124 goto bail;
1126 if (!preauth) {
1127 #ifndef NO_OPENSSL
1128 if (!srvc->use_ssl && CAP(STARTTLS)) {
1129 if (imap_exec(ctx, 0, "STARTTLS") != RESP_OK)
1130 goto bail;
1131 if (ssl_socket_connect(&imap->buf.sock, 1,
1132 srvc->ssl_verify))
1133 goto bail;
1134 /* capabilities may have changed, so get the new capabilities */
1135 if (imap_exec(ctx, 0, "CAPABILITY") != RESP_OK)
1136 goto bail;
1138 #endif
1139 imap_info("Logging in...\n");
1140 if (!srvc->user) {
1141 fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
1142 goto bail;
1144 if (!srvc->pass) {
1145 char prompt[80];
1146 sprintf(prompt, "Password (%s@%s): ", srvc->user, srvc->host);
1147 arg = getpass(prompt);
1148 if (!arg) {
1149 perror("getpass");
1150 exit(1);
1152 if (!*arg) {
1153 fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
1154 goto bail;
1157 * getpass() returns a pointer to a static buffer. make a copy
1158 * for long term storage.
1160 srvc->pass = xstrdup(arg);
1162 if (CAP(NOLOGIN)) {
1163 fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
1164 goto bail;
1166 if (!imap->buf.sock.ssl)
1167 imap_warn("*** IMAP Warning *** Password is being "
1168 "sent in the clear\n");
1169 if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) {
1170 fprintf(stderr, "IMAP error: LOGIN failed\n");
1171 goto bail;
1173 } /* !preauth */
1175 ctx->prefix = "";
1176 ctx->trashnc = 1;
1177 return (struct store *)ctx;
1179 bail:
1180 imap_close_store(&ctx->gen);
1181 return NULL;
1184 static int imap_make_flags(int flags, char *buf)
1186 const char *s;
1187 unsigned i, d;
1189 for (i = d = 0; i < ARRAY_SIZE(Flags); i++)
1190 if (flags & (1 << i)) {
1191 buf[d++] = ' ';
1192 buf[d++] = '\\';
1193 for (s = Flags[i]; *s; s++)
1194 buf[d++] = *s;
1196 buf[0] = '(';
1197 buf[d++] = ')';
1198 return d;
1201 #define TUIDL 8
1203 static int imap_store_msg(struct store *gctx, struct msg_data *data, int *uid)
1205 struct imap_store *ctx = (struct imap_store *)gctx;
1206 struct imap *imap = ctx->imap;
1207 struct imap_cmd_cb cb;
1208 char *fmap, *buf;
1209 const char *prefix, *box;
1210 int ret, i, j, d, len, extra, nocr;
1211 int start, sbreak = 0, ebreak = 0;
1212 char flagstr[128], tuid[TUIDL * 2 + 1];
1214 memset(&cb, 0, sizeof(cb));
1216 fmap = data->data;
1217 len = data->len;
1218 nocr = !data->crlf;
1219 extra = 0, i = 0;
1220 if (!CAP(UIDPLUS) && uid) {
1221 nloop:
1222 start = i;
1223 while (i < len)
1224 if (fmap[i++] == '\n') {
1225 extra += nocr;
1226 if (i - 2 + nocr == start) {
1227 sbreak = ebreak = i - 2 + nocr;
1228 goto mktid;
1230 if (!memcmp(fmap + start, "X-TUID: ", 8)) {
1231 extra -= (ebreak = i) - (sbreak = start) + nocr;
1232 goto mktid;
1234 goto nloop;
1236 /* invalid message */
1237 free(fmap);
1238 return DRV_MSG_BAD;
1239 mktid:
1240 for (j = 0; j < TUIDL; j++)
1241 sprintf(tuid + j * 2, "%02x", arc4_getbyte());
1242 extra += 8 + TUIDL * 2 + 2;
1244 if (nocr)
1245 for (; i < len; i++)
1246 if (fmap[i] == '\n')
1247 extra++;
1249 cb.dlen = len + extra;
1250 buf = cb.data = xmalloc(cb.dlen);
1251 i = 0;
1252 if (!CAP(UIDPLUS) && uid) {
1253 if (nocr) {
1254 for (; i < sbreak; i++)
1255 if (fmap[i] == '\n') {
1256 *buf++ = '\r';
1257 *buf++ = '\n';
1258 } else
1259 *buf++ = fmap[i];
1260 } else {
1261 memcpy(buf, fmap, sbreak);
1262 buf += sbreak;
1264 memcpy(buf, "X-TUID: ", 8);
1265 buf += 8;
1266 memcpy(buf, tuid, TUIDL * 2);
1267 buf += TUIDL * 2;
1268 *buf++ = '\r';
1269 *buf++ = '\n';
1270 i = ebreak;
1272 if (nocr) {
1273 for (; i < len; i++)
1274 if (fmap[i] == '\n') {
1275 *buf++ = '\r';
1276 *buf++ = '\n';
1277 } else
1278 *buf++ = fmap[i];
1279 } else
1280 memcpy(buf, fmap + i, len - i);
1282 free(fmap);
1284 d = 0;
1285 if (data->flags) {
1286 d = imap_make_flags(data->flags, flagstr);
1287 flagstr[d++] = ' ';
1289 flagstr[d] = 0;
1291 if (!uid) {
1292 box = gctx->conf->trash;
1293 prefix = ctx->prefix;
1294 cb.create = 1;
1295 if (ctx->trashnc)
1296 imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
1297 } else {
1298 box = gctx->name;
1299 prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1300 cb.create = 0;
1302 cb.ctx = uid;
1303 ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr);
1304 imap->caps = imap->rcaps;
1305 if (ret != DRV_OK)
1306 return ret;
1307 if (!uid)
1308 ctx->trashnc = 0;
1309 else
1310 gctx->count++;
1312 return DRV_OK;
1315 static void encode_html_chars(struct strbuf *p)
1317 int i;
1318 for (i = 0; i < p->len; i++) {
1319 if (p->buf[i] == '&')
1320 strbuf_splice(p, i, 1, "&amp;", 5);
1321 if (p->buf[i] == '<')
1322 strbuf_splice(p, i, 1, "&lt;", 4);
1323 if (p->buf[i] == '>')
1324 strbuf_splice(p, i, 1, "&gt;", 4);
1325 if (p->buf[i] == '"')
1326 strbuf_splice(p, i, 1, "&quot;", 6);
1329 static void wrap_in_html(struct msg_data *msg)
1331 struct strbuf buf = STRBUF_INIT;
1332 struct strbuf **lines;
1333 struct strbuf **p;
1334 static char *content_type = "Content-Type: text/html;\n";
1335 static char *pre_open = "<pre>\n";
1336 static char *pre_close = "</pre>\n";
1337 int added_header = 0;
1339 strbuf_attach(&buf, msg->data, msg->len, msg->len);
1340 lines = strbuf_split(&buf, '\n');
1341 strbuf_release(&buf);
1342 for (p = lines; *p; p++) {
1343 if (! added_header) {
1344 if ((*p)->len == 1 && *((*p)->buf) == '\n') {
1345 strbuf_addstr(&buf, content_type);
1346 strbuf_addbuf(&buf, *p);
1347 strbuf_addstr(&buf, pre_open);
1348 added_header = 1;
1349 continue;
1352 else
1353 encode_html_chars(*p);
1354 strbuf_addbuf(&buf, *p);
1356 strbuf_addstr(&buf, pre_close);
1357 strbuf_list_free(lines);
1358 msg->len = buf.len;
1359 msg->data = strbuf_detach(&buf, NULL);
1362 #define CHUNKSIZE 0x1000
1364 static int read_message(FILE *f, struct msg_data *msg)
1366 struct strbuf buf = STRBUF_INIT;
1368 memset(msg, 0, sizeof(*msg));
1370 do {
1371 if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
1372 break;
1373 } while (!feof(f));
1375 msg->len = buf.len;
1376 msg->data = strbuf_detach(&buf, NULL);
1377 return msg->len;
1380 static int count_messages(struct msg_data *msg)
1382 int count = 0;
1383 char *p = msg->data;
1385 while (1) {
1386 if (!prefixcmp(p, "From ")) {
1387 count++;
1388 p += 5;
1390 p = strstr(p+5, "\nFrom ");
1391 if (!p)
1392 break;
1393 p++;
1395 return count;
1398 static int split_msg(struct msg_data *all_msgs, struct msg_data *msg, int *ofs)
1400 char *p, *data;
1402 memset(msg, 0, sizeof *msg);
1403 if (*ofs >= all_msgs->len)
1404 return 0;
1406 data = &all_msgs->data[*ofs];
1407 msg->len = all_msgs->len - *ofs;
1409 if (msg->len < 5 || prefixcmp(data, "From "))
1410 return 0;
1412 p = strchr(data, '\n');
1413 if (p) {
1414 p = &p[1];
1415 msg->len -= p-data;
1416 *ofs += p-data;
1417 data = p;
1420 p = strstr(data, "\nFrom ");
1421 if (p)
1422 msg->len = &p[1] - data;
1424 msg->data = xmemdupz(data, msg->len);
1425 *ofs += msg->len;
1426 return 1;
1429 static struct imap_server_conf server = {
1430 NULL, /* name */
1431 NULL, /* tunnel */
1432 NULL, /* host */
1433 0, /* port */
1434 NULL, /* user */
1435 NULL, /* pass */
1436 0, /* use_ssl */
1437 1, /* ssl_verify */
1438 0, /* use_html */
1441 static char *imap_folder;
1443 static int git_imap_config(const char *key, const char *val, void *cb)
1445 char imap_key[] = "imap.";
1447 if (strncmp(key, imap_key, sizeof imap_key - 1))
1448 return 0;
1450 if (!val)
1451 return config_error_nonbool(key);
1453 key += sizeof imap_key - 1;
1455 if (!strcmp("folder", key)) {
1456 imap_folder = xstrdup(val);
1457 } else if (!strcmp("host", key)) {
1458 if (!prefixcmp(val, "imap:"))
1459 val += 5;
1460 else if (!prefixcmp(val, "imaps:")) {
1461 val += 6;
1462 server.use_ssl = 1;
1464 if (!prefixcmp(val, "//"))
1465 val += 2;
1466 server.host = xstrdup(val);
1467 } else if (!strcmp("user", key))
1468 server.user = xstrdup(val);
1469 else if (!strcmp("pass", key))
1470 server.pass = xstrdup(val);
1471 else if (!strcmp("port", key))
1472 server.port = git_config_int(key, val);
1473 else if (!strcmp("tunnel", key))
1474 server.tunnel = xstrdup(val);
1475 else if (!strcmp("sslverify", key))
1476 server.ssl_verify = git_config_bool(key, val);
1477 else if (!strcmp("preformattedHTML", key))
1478 server.use_html = git_config_bool(key, val);
1479 return 0;
1482 int main(int argc, char **argv)
1484 struct msg_data all_msgs, msg;
1485 struct store *ctx = NULL;
1486 int uid = 0;
1487 int ofs = 0;
1488 int r;
1489 int total, n = 0;
1490 int nongit_ok;
1492 git_extract_argv0_path(argv[0]);
1494 /* init the random number generator */
1495 arc4_init();
1497 setup_git_directory_gently(&nongit_ok);
1498 git_config(git_imap_config, NULL);
1500 if (!server.port)
1501 server.port = server.use_ssl ? 993 : 143;
1503 if (!imap_folder) {
1504 fprintf(stderr, "no imap store specified\n");
1505 return 1;
1507 if (!server.host) {
1508 if (!server.tunnel) {
1509 fprintf(stderr, "no imap host specified\n");
1510 return 1;
1512 server.host = "tunnel";
1515 /* read the messages */
1516 if (!read_message(stdin, &all_msgs)) {
1517 fprintf(stderr, "nothing to send\n");
1518 return 1;
1521 total = count_messages(&all_msgs);
1522 if (!total) {
1523 fprintf(stderr, "no messages to send\n");
1524 return 1;
1527 /* write it to the imap server */
1528 ctx = imap_open_store(&server);
1529 if (!ctx) {
1530 fprintf(stderr, "failed to open store\n");
1531 return 1;
1534 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1535 ctx->name = imap_folder;
1536 while (1) {
1537 unsigned percent = n * 100 / total;
1538 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1539 if (!split_msg(&all_msgs, &msg, &ofs))
1540 break;
1541 if (server.use_html)
1542 wrap_in_html(&msg);
1543 r = imap_store_msg(ctx, &msg, &uid);
1544 if (r != DRV_OK)
1545 break;
1546 n++;
1548 fprintf(stderr, "\n");
1550 imap_close_store(ctx);
1552 return 0;