1 #include "common.h" 2 #include <ctype.h> 3 #include <plumb.h> 4 #include <libsec.h> 5 #include <auth.h> 6 #include "dat.h" 7 8 #pragma varargck argpos imap4cmd 2 9 #pragma varargck type "Z" char* 10 11 int doublequote(Fmt*); 12 int pipeline = 1; 13 14 static char Eio[] = "i/o error"; 15 16 typedef struct Imap Imap; 17 struct Imap { 18 char *freep; // free this to free the strings below 19 20 char *host; 21 char *user; 22 char *mbox; 23 24 int mustssl; 25 int refreshtime; 26 int debug; 27 28 ulong tag; 29 ulong validity; 30 int nmsg; 31 int size; 32 char *base; 33 char *data; 34 35 vlong *uid; 36 int nuid; 37 int muid; 38 39 Thumbprint *thumb; 40 41 // open network connection 42 Biobuf bin; 43 Biobuf bout; 44 int fd; 45 }; 46 47 static char* 48 removecr(char *s) 49 { 50 char *r, *w; 51 52 for(r=w=s; *r; r++) 53 if(*r != '\r') 54 *w++ = *r; 55 *w = '\0'; 56 return s; 57 } 58 59 // 60 // send imap4 command 61 // 62 static void 63 imap4cmd(Imap *imap, char *fmt, ...) 64 { 65 char buf[128], *p; 66 va_list va; 67 68 va_start(va, fmt); 69 p = buf+sprint(buf, "9X%lud ", imap->tag); 70 vseprint(p, buf+sizeof(buf), fmt, va); 71 va_end(va); 72 73 p = buf+strlen(buf); 74 if(p > (buf+sizeof(buf)-3)) 75 sysfatal("imap4 command too long"); 76 77 if(imap->debug) 78 fprint(2, "-> %s\n", buf); 79 strcpy(p, "\r\n"); 80 Bwrite(&imap->bout, buf, strlen(buf)); 81 Bflush(&imap->bout); 82 } 83 84 enum { 85 OK, 86 NO, 87 BAD, 88 BYE, 89 EXISTS, 90 STATUS, 91 FETCH, 92 UNKNOWN, 93 }; 94 95 static char *verblist[] = { 96 [OK] "OK", 97 [NO] "NO", 98 [BAD] "BAD", 99 [BYE] "BYE", 100 [EXISTS] "EXISTS", 101 [STATUS] "STATUS", 102 [FETCH] "FETCH", 103 }; 104 105 static int 106 verbcode(char *verb) 107 { 108 int i; 109 char *q; 110 111 if(q = strchr(verb, ' ')) 112 *q = '\0'; 113 114 for(i=0; i<nelem(verblist); i++) 115 if(verblist[i] && strcmp(verblist[i], verb)==0){ 116 if(q) 117 *q = ' '; 118 return i; 119 } 120 if(q) 121 *q = ' '; 122 return UNKNOWN; 123 } 124 125 static void 126 strupr(char *s) 127 { 128 for(; *s; s++) 129 if('a' <= *s && *s <= 'z') 130 *s += 'A'-'a'; 131 } 132 133 static void 134 imapgrow(Imap *imap, int n) 135 { 136 int i; 137 138 if(imap->data == nil){ 139 imap->base = emalloc(n+1); 140 imap->data = imap->base; 141 imap->size = n+1; 142 } 143 if(n >= imap->size){ 144 // friggin microsoft - reallocate 145 i = imap->data - imap->base; 146 imap->base = erealloc(imap->base, i+n+1); 147 imap->data = imap->base + i; 148 imap->size = n+1; 149 } 150 } 151 152 153 // 154 // get imap4 response line. there might be various 155 // data or other informational lines mixed in. 156 // 157 static char* 158 imap4resp(Imap *imap) 159 { 160 char *line, *p, *ep, *op, *q, *r, *en, *verb; 161 int i, n; 162 static char error[256]; 163 164 while(p = Brdline(&imap->bin, '\n')){ 165 ep = p+Blinelen(&imap->bin); 166 while(ep > p && (ep[-1]=='\n' || ep[-1]=='\r')) 167 *--ep = '\0'; 168 169 if(imap->debug) 170 fprint(2, "<- %s\n", p); 171 strupr(p); 172 173 switch(p[0]){ 174 case '+': 175 if(imap->tag == 0) 176 fprint(2, "unexpected: %s\n", p); 177 break; 178 179 // ``unsolicited'' information; everything happens here. 180 case '*': 181 if(p[1]!=' ') 182 continue; 183 p += 2; 184 line = p; 185 n = strtol(p, &p, 10); 186 if(*p==' ') 187 p++; 188 verb = p; 189 190 if(p = strchr(verb, ' ')) 191 p++; 192 else 193 p = verb+strlen(verb); 194 195 switch(verbcode(verb)){ 196 case OK: 197 case NO: 198 case BAD: 199 // human readable text at p; 200 break; 201 case BYE: 202 // early disconnect 203 // human readable text at p; 204 break; 205 206 // * 32 EXISTS 207 case EXISTS: 208 imap->nmsg = n; 209 break; 210 211 // * STATUS Inbox (MESSAGES 2 UIDVALIDITY 960164964) 212 case STATUS: 213 if(q = strstr(p, "MESSAGES")) 214 imap->nmsg = atoi(q+8); 215 if(q = strstr(p, "UIDVALIDITY")) 216 imap->validity = strtoul(q+11, 0, 10); 217 break; 218 219 case FETCH: 220 // * 1 FETCH (uid 8889 RFC822.SIZE 3031 body[] {3031} 221 // <3031 bytes of data> 222 // ) 223 if(strstr(p, "RFC822.SIZE") && strstr(p, "BODY[]")){ 224 if((q = strchr(p, '{')) 225 && (n=strtol(q+1, &en, 0), *en=='}')){ 226 if(imap->data == nil || n >= imap->size) 227 imapgrow(imap, n); 228 if((i = Bread(&imap->bin, imap->data, n)) != n){ 229 snprint(error, sizeof error, 230 "short read %d != %d: %r\n", 231 i, n); 232 return error; 233 } 234 if(imap->debug) 235 fprint(2, "<- read %d bytes\n", n); 236 imap->data[n] = '\0'; 237 if(imap->debug) 238 fprint(2, "<- %s\n", imap->data); 239 imap->data += n; 240 imap->size -= n; 241 p = Brdline(&imap->bin, '\n'); 242 if(imap->debug) 243 fprint(2, "<- ignoring %.*s\n", 244 Blinelen(&imap->bin), p); 245 }else if((q = strchr(p, '"')) && (r = strchr(q+1, '"'))){ 246 *r = '\0'; 247 q++; 248 n = r-q; 249 if(imap->data == nil || n >= imap->size) 250 imapgrow(imap, n); 251 memmove(imap->data, q, n); 252 imap->data[n] = '\0'; 253 imap->data += n; 254 imap->size -= n; 255 }else 256 return "confused about FETCH response"; 257 break; 258 } 259 260 // * 1 FETCH (UID 1 RFC822.SIZE 511) 261 if(q=strstr(p, "RFC822.SIZE")){ 262 imap->size = atoi(q+11); 263 break; 264 } 265 266 // * 1 FETCH (UID 1 RFC822.HEADER {496} 267 // <496 bytes of data> 268 // ) 269 // * 1 FETCH (UID 1 RFC822.HEADER "data") 270 if(strstr(p, "RFC822.HEADER") || strstr(p, "RFC822.TEXT")){ 271 if((q = strchr(p, '{')) 272 && (n=strtol(q+1, &en, 0), *en=='}')){ 273 if(imap->data == nil || n >= imap->size) 274 imapgrow(imap, n); 275 if((i = Bread(&imap->bin, imap->data, n)) != n){ 276 snprint(error, sizeof error, 277 "short read %d != %d: %r\n", 278 i, n); 279 return error; 280 } 281 if(imap->debug) 282 fprint(2, "<- read %d bytes\n", n); 283 imap->data[n] = '\0'; 284 if(imap->debug) 285 fprint(2, "<- %s\n", imap->data); 286 imap->data += n; 287 imap->size -= n; 288 p = Brdline(&imap->bin, '\n'); 289 if(imap->debug) 290 fprint(2, "<- ignoring %.*s\n", 291 Blinelen(&imap->bin), p); 292 }else if((q = strchr(p, '"')) && (r = strchr(q+1, '"'))){ 293 *r = '\0'; 294 q++; 295 n = r-q; 296 if(imap->data == nil || n >= imap->size) 297 imapgrow(imap, n); 298 memmove(imap->data, q, n); 299 imap->data[n] = '\0'; 300 imap->data += n; 301 imap->size -= n; 302 }else 303 return "confused about FETCH response"; 304 break; 305 } 306 307 // * 1 FETCH (UID 1) 308 // * 2 FETCH (UID 6) 309 if(q = strstr(p, "UID")){ 310 if(imap->nuid < imap->muid) 311 imap->uid[imap->nuid++] = ((vlong)imap->validity<<32)|strtoul(q+3, nil, 10); 312 break; 313 } 314 } 315 316 if(imap->tag == 0) 317 return line; 318 break; 319 320 case '9': // response to our message 321 op = p; 322 if(p[1]=='X' && strtoul(p+2, &p, 10)==imap->tag){ 323 while(*p==' ') 324 p++; 325 imap->tag++; 326 return p; 327 } 328 fprint(2, "expected %lud; got %s\n", imap->tag, op); 329 break; 330 331 default: 332 if(imap->debug || *p) 333 fprint(2, "unexpected line: %s\n", p); 334 } 335 } 336 snprint(error, sizeof error, "i/o error: %r\n"); 337 return error; 338 } 339 340 static int 341 isokay(char *resp) 342 { 343 return strncmp(resp, "OK", 2)==0; 344 } 345 346 // 347 // log in to IMAP4 server, select mailbox, no SSL at the moment 348 // 349 static char* 350 imap4login(Imap *imap) 351 { 352 char *s; 353 UserPasswd *up; 354 355 imap->tag = 0; 356 s = imap4resp(imap); 357 if(!isokay(s)) 358 return "error in initial IMAP handshake"; 359 360 if(imap->user != nil) 361 up = auth_getuserpasswd(auth_getkey, "proto=pass service=imap server=%q user=%q", imap->host, imap->user); 362 else 363 up = auth_getuserpasswd(auth_getkey, "proto=pass service=imap server=%q", imap->host); 364 if(up == nil) 365 return "cannot find IMAP password"; 366 367 imap->tag = 1; 368 imap4cmd(imap, "LOGIN %Z %Z", up->user, up->passwd); 369 free(up); 370 if(!isokay(s = imap4resp(imap))) 371 return s; 372 373 imap4cmd(imap, "SELECT %Z", imap->mbox); 374 if(!isokay(s = imap4resp(imap))) 375 return s; 376 377 return nil; 378 } 379 380 static char* 381 imaperrstr(char *host, char *port) 382 { 383 /* 384 * make mess big enough to hold a TLS certificate fingerprint 385 * plus quite a bit of slop. 386 */ 387 static char mess[3 * Errlen]; 388 char err[Errlen]; 389 390 err[0] = '\0'; 391 errstr(err, sizeof(err)); 392 snprint(mess, sizeof(mess), "%s/%s:%s", host, port, err); 393 return mess; 394 } 395 396 static int 397 starttls(Imap *imap, TLSconn *connp) 398 { 399 int sfd; 400 uchar digest[SHA1dlen]; 401 402 fmtinstall('H', encodefmt); 403 memset(connp, 0, sizeof *connp); 404 sfd = tlsClient(imap->fd, connp); 405 if(sfd < 0) { 406 werrstr("tlsClient: %r"); 407 return -1; 408 } 409 if(connp->cert==nil || connp->certlen <= 0) { 410 close(sfd); 411 werrstr("server did not provide TLS certificate"); 412 return -1; 413 } 414 sha1(connp->cert, connp->certlen, digest, nil); 415 if(!imap->thumb || !okThumbprint(digest, imap->thumb)){ 416 close(sfd); 417 werrstr("server certificate %.*H not recognized", 418 SHA1dlen, digest); 419 return -1; 420 } 421 close(imap->fd); 422 imap->fd = sfd; 423 return sfd; 424 } 425 426 // 427 // dial and handshake with the imap server 428 // 429 static char* 430 imap4dial(Imap *imap) 431 { 432 char *err, *port; 433 int sfd; 434 TLSconn conn; 435 436 if(imap->fd >= 0){ 437 imap4cmd(imap, "noop"); 438 if(isokay(imap4resp(imap))) 439 return nil; 440 close(imap->fd); 441 imap->fd = -1; 442 } 443 444 if(imap->mustssl) 445 port = "imaps"; 446 else 447 port = "imap"; 448 449 if((imap->fd = dial(netmkaddr(imap->host, "net", port), 0, 0, 0)) < 0) 450 return imaperrstr(imap->host, port); 451 452 if(imap->mustssl){ 453 sfd = starttls(imap, &conn); 454 if (sfd < 0) { 455 free(conn.cert); 456 return imaperrstr(imap->host, port); 457 } 458 if(imap->debug){ 459 char fn[128]; 460 int fd; 461 462 snprint(fn, sizeof fn, "%s/ctl", conn.dir); 463 fd = open(fn, ORDWR); 464 if(fd < 0) 465 fprint(2, "opening ctl: %r\n"); 466 if(fprint(fd, "debug") < 0) 467 fprint(2, "writing ctl: %r\n"); 468 close(fd); 469 } 470 } 471 Binit(&imap->bin, imap->fd, OREAD); 472 Binit(&imap->bout, imap->fd, OWRITE); 473 474 if(err = imap4login(imap)) { 475 close(imap->fd); 476 return err; 477 } 478 479 return nil; 480 } 481 482 // 483 // close connection 484 // 485 static void 486 imap4hangup(Imap *imap) 487 { 488 imap4cmd(imap, "LOGOUT"); 489 imap4resp(imap); 490 close(imap->fd); 491 } 492 493 // 494 // download a single message 495 // 496 static char* 497 imap4fetch(Mailbox *mb, Message *m) 498 { 499 int i; 500 char *p, *s, sdigest[2*SHA1dlen+1]; 501 Imap *imap; 502 503 imap = mb->aux; 504 505 imap->size = 0; 506 507 if(!isokay(s = imap4resp(imap))) 508 return s; 509 510 p = imap->base; 511 if(p == nil) 512 return "did not get message body"; 513 514 removecr(p); 515 free(m->start); 516 m->start = p; 517 m->end = p+strlen(p); 518 m->bend = m->rbend = m->end; 519 m->header = m->start; 520 521 imap->base = nil; 522 imap->data = nil; 523 524 parse(m, 0, mb, 1); 525 526 // digest headers 527 sha1((uchar*)m->start, m->end - m->start, m->digest, nil); 528 for(i = 0; i < SHA1dlen; i++) 529 sprint(sdigest+2*i, "%2.2ux", m->digest[i]); 530 m->sdigest = s_copy(sdigest); 531 532 return nil; 533 } 534 535 // 536 // check for new messages on imap4 server 537 // download new messages, mark deleted messages 538 // 539 static char* 540 imap4read(Imap *imap, Mailbox *mb, int doplumb) 541 { 542 char *s; 543 int i, ignore, nnew, t; 544 Message *m, *next, **l; 545 546 imap4cmd(imap, "STATUS %Z (MESSAGES UIDVALIDITY)", imap->mbox); 547 if(!isokay(s = imap4resp(imap))) 548 return s; 549 550 imap->nuid = 0; 551 imap->uid = erealloc(imap->uid, imap->nmsg*sizeof(imap->uid[0])); 552 imap->muid = imap->nmsg; 553 554 if(imap->nmsg > 0){ 555 imap4cmd(imap, "UID FETCH 1:* UID"); 556 if(!isokay(s = imap4resp(imap))) 557 return s; 558 } 559 560 l = &mb->root->part; 561 for(i=0; i<imap->nuid; i++){ 562 ignore = 0; 563 while(*l != nil){ 564 if((*l)->imapuid == imap->uid[i]){ 565 ignore = 1; 566 l = &(*l)->next; 567 break; 568 }else{ 569 // old mail, we don't have it anymore 570 if(doplumb) 571 mailplumb(mb, *l, 1); 572 (*l)->inmbox = 0; 573 (*l)->deleted = 1; 574 l = &(*l)->next; 575 } 576 } 577 if(ignore) 578 continue; 579 580 // new message 581 m = newmessage(mb->root); 582 m->mallocd = 1; 583 m->inmbox = 1; 584 m->imapuid = imap->uid[i]; 585 586 // add to chain, will download soon 587 *l = m; 588 l = &m->next; 589 } 590 591 // whatever is left at the end of the chain is gone 592 while(*l != nil){ 593 if(doplumb) 594 mailplumb(mb, *l, 1); 595 (*l)->inmbox = 0; 596 (*l)->deleted = 1; 597 l = &(*l)->next; 598 } 599 600 // download new messages 601 t = imap->tag; 602 if(pipeline) 603 switch(rfork(RFPROC|RFMEM)){ 604 case -1: 605 sysfatal("rfork: %r"); 606 default: 607 break; 608 case 0: 609 for(m = mb->root->part; m != nil; m = m->next){ 610 if(m->start != nil) 611 continue; 612 if(imap->debug) 613 fprint(2, "9X%d UID FETCH %lud (UID RFC822.SIZE BODY[])\r\n", 614 t, (ulong)m->imapuid); 615 Bprint(&imap->bout, "9X%d UID FETCH %lud (UID RFC822.SIZE BODY[])\r\n", 616 t++, (ulong)m->imapuid); 617 } 618 Bflush(&imap->bout); 619 _exits(nil); 620 } 621 622 nnew = 0; 623 for(m=mb->root->part; m!=nil; m=next){ 624 next = m->next; 625 if(m->start != nil) 626 continue; 627 628 if(!pipeline){ 629 Bprint(&imap->bout, "9X%lud UID FETCH %lud (UID RFC822.SIZE BODY[])\r\n", 630 (ulong)imap->tag, (ulong)m->imapuid); 631 Bflush(&imap->bout); 632 } 633 634 if(s = imap4fetch(mb, m)){ 635 // message disappeared? unchain 636 fprint(2, "download %lud: %s\n", (ulong)m->imapuid, s); 637 delmessage(mb, m); 638 mb->root->subname--; 639 continue; 640 } 641 nnew++; 642 if(doplumb) 643 mailplumb(mb, m, 0); 644 } 645 if(pipeline) 646 waitpid(); 647 648 if(nnew || mb->vers == 0){ 649 mb->vers++; 650 henter(PATH(0, Qtop), mb->name, 651 (Qid){PATH(mb->id, Qmbox), mb->vers, QTDIR}, nil, mb); 652 } 653 return nil; 654 } 655 656 // 657 // sync mailbox 658 // 659 static void 660 imap4purge(Imap *imap, Mailbox *mb) 661 { 662 int ndel; 663 Message *m, *next; 664 665 ndel = 0; 666 for(m=mb->root->part; m!=nil; m=next){ 667 next = m->next; 668 if(m->deleted && m->refs==0){ 669 if(m->inmbox && (ulong)(m->imapuid>>32)==imap->validity){ 670 imap4cmd(imap, "UID STORE %lud +FLAGS (\\Deleted)", (ulong)m->imapuid); 671 if(isokay(imap4resp(imap))){ 672 ndel++; 673 delmessage(mb, m); 674 } 675 }else 676 delmessage(mb, m); 677 } 678 } 679 680 if(ndel){ 681 imap4cmd(imap, "EXPUNGE"); 682 imap4resp(imap); 683 } 684 } 685 686 // 687 // connect to imap4 server, sync mailbox 688 // 689 static char* 690 imap4sync(Mailbox *mb, int doplumb) 691 { 692 char *err; 693 Imap *imap; 694 695 imap = mb->aux; 696 697 if(err = imap4dial(imap)){ 698 mb->waketime = time(0) + imap->refreshtime; 699 return err; 700 } 701 702 if((err = imap4read(imap, mb, doplumb)) == nil){ 703 imap4purge(imap, mb); 704 mb->d->atime = mb->d->mtime = time(0); 705 } 706 /* 707 * don't hang up; leave connection open for next time. 708 */ 709 // imap4hangup(imap); 710 mb->waketime = time(0) + imap->refreshtime; 711 return err; 712 } 713 714 static char Eimap4ctl[] = "bad imap4 control message"; 715 716 static char* 717 imap4ctl(Mailbox *mb, int argc, char **argv) 718 { 719 int n; 720 Imap *imap; 721 722 imap = mb->aux; 723 if(argc < 1) 724 return Eimap4ctl; 725 726 if(argc==1 && strcmp(argv[0], "debug")==0){ 727 imap->debug = 1; 728 return nil; 729 } 730 731 if(argc==1 && strcmp(argv[0], "nodebug")==0){ 732 imap->debug = 0; 733 return nil; 734 } 735 736 if(argc==1 && strcmp(argv[0], "thumbprint")==0){ 737 if(imap->thumb) 738 freeThumbprints(imap->thumb); 739 imap->thumb = initThumbprints("/sys/lib/tls/mail", "/sys/lib/tls/mail.exclude"); 740 } 741 if(strcmp(argv[0], "refresh")==0){ 742 if(argc==1){ 743 imap->refreshtime = 60; 744 return nil; 745 } 746 if(argc==2){ 747 n = atoi(argv[1]); 748 if(n < 15) 749 return Eimap4ctl; 750 imap->refreshtime = n; 751 return nil; 752 } 753 } 754 755 return Eimap4ctl; 756 } 757 758 // 759 // free extra memory associated with mb 760 // 761 static void 762 imap4close(Mailbox *mb) 763 { 764 Imap *imap; 765 766 imap = mb->aux; 767 free(imap->freep); 768 free(imap->base); 769 free(imap->uid); 770 if(imap->fd >= 0) 771 close(imap->fd); 772 free(imap); 773 } 774 775 // 776 // open mailboxes of the form /imap/host/user 777 // 778 char* 779 imap4mbox(Mailbox *mb, char *path) 780 { 781 char *f[10]; 782 int mustssl, nf; 783 Imap *imap; 784 785 quotefmtinstall(); 786 fmtinstall('Z', doublequote); 787 if(strncmp(path, "/imap/", 6) != 0 && strncmp(path, "/imaps/", 7) != 0) 788 return Enotme; 789 mustssl = (strncmp(path, "/imaps/", 7) == 0); 790 791 path = strdup(path); 792 if(path == nil) 793 return "out of memory"; 794 795 nf = getfields(path, f, 5, 0, "/"); 796 if(nf < 3){ 797 free(path); 798 return "bad imap path syntax /imap[s]/system[/user[/mailbox]]"; 799 } 800 801 imap = emalloc(sizeof(*imap)); 802 imap->fd = -1; 803 imap->debug = debug; 804 imap->freep = path; 805 imap->mustssl = mustssl; 806 imap->host = f[2]; 807 if(nf < 4) 808 imap->user = nil; 809 else 810 imap->user = f[3]; 811 if(nf < 5) 812 imap->mbox = "Inbox"; 813 else 814 imap->mbox = f[4]; 815 imap->thumb = initThumbprints("/sys/lib/tls/mail", "/sys/lib/tls/mail.exclude"); 816 817 mb->aux = imap; 818 mb->sync = imap4sync; 819 mb->close = imap4close; 820 mb->ctl = imap4ctl; 821 mb->d = emalloc(sizeof(*mb->d)); 822 //mb->fetch = imap4fetch; 823 824 return nil; 825 } 826 827 // 828 // Formatter for %" 829 // Use double quotes to protect white space, frogs, \ and " 830 // 831 enum 832 { 833 Qok = 0, 834 Qquote, 835 Qbackslash, 836 }; 837 838 static int 839 needtoquote(Rune r) 840 { 841 if(r >= Runeself) 842 return Qquote; 843 if(r <= ' ') 844 return Qquote; 845 if(r=='\\' || r=='"') 846 return Qbackslash; 847 return Qok; 848 } 849 850 int 851 doublequote(Fmt *f) 852 { 853 char *s, *t; 854 int w, quotes; 855 Rune r; 856 857 s = va_arg(f->args, char*); 858 if(s == nil || *s == '\0') 859 return fmtstrcpy(f, "\"\""); 860 861 quotes = 0; 862 for(t=s; *t; t+=w){ 863 w = chartorune(&r, t); 864 quotes |= needtoquote(r); 865 } 866 if(quotes == 0) 867 return fmtstrcpy(f, s); 868 869 fmtrune(f, '"'); 870 for(t=s; *t; t+=w){ 871 w = chartorune(&r, t); 872 if(needtoquote(r) == Qbackslash) 873 fmtrune(f, '\\'); 874 fmtrune(f, r); 875 } 876 return fmtrune(f, '"'); 877 } 878