9 #pragma varargck type "M" uchar*
10 #pragma varargck argpos pop3cmd 2
12 typedef struct Pop Pop;
14 char *freep; /* free this to free the strings below */
29 /* open network connection */
33 char *lastline; /* from Brdstr */
44 errstr(err, sizeof(err));
49 /* get pop3 response line , without worrying */
50 /* about multiline responses; the clients */
51 /* will deal with that. */
56 return s!=nil && strncmp(s, "+OK", 3)==0;
60 pop3cmd(Pop *pop, char *fmt, ...)
66 vseprint(buf, buf+sizeof(buf), fmt, va);
70 if(p > (buf+sizeof(buf)-3))
71 sysfatal("pop3 command too long");
74 fprint(2, "<- %s\n", buf);
76 Bwrite(&pop->bout, buf, strlen(buf));
87 if((s = Brdstr(&pop->bin, '\n', 0)) == nil){
91 return "unexpected eof";
96 while(p >= s && (*p == '\r' || *p == '\n'))
100 fprint(2, "-> %s\n", s);
108 pop3log(char *fmt, ...)
113 syslog(0, "/sys/log/pop3", fmt, ap);
120 pop3pushtls(Pop *pop)
123 uchar digest[SHA1dlen];
126 memset(&conn, 0, sizeof conn);
127 /* conn.trace = pop3log; */
128 fd = tlsClient(pop->fd, &conn);
131 if(conn.cert==nil || conn.certlen <= 0){
133 return "server did not provide TLS certificate";
135 sha1(conn.cert, conn.certlen, digest, nil);
136 if(!pop->thumb || !okThumbprint(digest, pop->thumb)){
137 fmtinstall('H', encodefmt);
140 fprint(2, "upas/fs pop3: server certificate %.*H not recognized\n", SHA1dlen, digest);
141 return "bad server certificate";
147 Binit(&pop->bin, pop->fd, OREAD);
148 Binit(&pop->bout, pop->fd, OWRITE);
153 /* get capability list, possibly start tls */
161 pop3cmd(pop, "CAPA");
162 if(!isokay(pop3resp(pop)))
168 if(strcmp(s, ".") == 0 || strcmp(s, "unexpected eof") == 0)
170 if(strcmp(s, "STLS") == 0)
172 if(strcmp(s, "PIPELINING") == 0)
176 if(hastls && !pop->notls){
177 pop3cmd(pop, "STLS");
178 if(!isokay(s = pop3resp(pop)))
180 if((s = pop3pushtls(pop)) != nil)
187 /* log in using APOP if possible, password if allowed by user */
194 char ubuf[128], user[128];
200 return "error in initial handshake";
203 snprint(ubuf, sizeof ubuf, " user=%q", pop->user);
207 /* look for apop banner */
208 if(pop->ppop==0 && (p = strchr(s, '<')) && (q = strchr(p+1, '>'))) {
210 if((n=auth_respond(p, q-p, user, sizeof user, buf, sizeof buf, auth_getkey, "proto=apop role=client server=%q%s",
211 pop->host, ubuf)) < 0)
212 return "factotum failed";
214 return "factotum did not return a user name";
216 if(s = pop3capa(pop))
219 pop3cmd(pop, "APOP %s %.*s", user, n, buf);
220 if(!isokay(s = pop3resp(pop)))
226 return "no APOP hdr from server";
228 if(s = pop3capa(pop))
231 if(pop->needtls && !pop->encrypted)
232 return "could not negotiate TLS";
234 up = auth_getuserpasswd(auth_getkey, "proto=pass role=client service=pop dom=%q%s",
237 return "no usable keys found";
239 pop3cmd(pop, "USER %s", up->user);
240 if(!isokay(s = pop3resp(pop))){
244 pop3cmd(pop, "PASS %s", up->passwd);
246 if(!isokay(s = pop3resp(pop)))
254 /* dial and handshake with pop server */
261 if((pop->fd = dial(netmkaddr(pop->host, "net", pop->needssl ? "pop3s" : "pop3"), 0, 0, 0)) < 0)
265 if((err = pop3pushtls(pop)) != nil)
268 Binit(&pop->bin, pop->fd, OREAD);
269 Binit(&pop->bout, pop->fd, OWRITE);
272 if(err = pop3login(pop)) {
281 /* close connection */
286 pop3cmd(pop, "QUIT");
292 /* download a single message */
295 pop3download(Pop *pop, Message *m)
297 char *s, *f[3], *wp, *ep;
298 char sdigest[SHA1dlen*2+1];
302 pop3cmd(pop, "LIST %d", m->mesgno);
303 if(!isokay(s = pop3resp(pop)))
306 if(tokenize(s, f, 3) != 3)
307 return "syntax error in LIST response";
309 if(atoi(f[1]) != m->mesgno)
310 return "out of sync with pop3 server";
312 sz = atoi(f[2])+200; /* 200 because the plan9 pop3 server lies */
314 return "invalid size in LIST response";
316 m->start = wp = emalloc(sz+1);
320 pop3cmd(pop, "RETR %d", m->mesgno);
321 if(!isokay(s = pop3resp(pop))) {
330 if(strcmp(s, "unexpected eof") == 0) {
333 return "unexpected end of conversation";
335 if(strcmp(s, ".") == 0)
344 * grow by 10%/200bytes - some servers
345 * lie about message sizes
348 int pos = wp - m->start;
349 sz += ((sz / 10) < 200)? 200: sz/10;
350 m->start = erealloc(m->start, sz+1);
359 if(s == nil || strcmp(s, ".") != 0)
360 return "out of sync with pop3 server";
364 /* make sure there's a trailing null */
365 /* (helps in body searches) */
367 m->bend = m->rbend = m->end;
368 m->header = m->start;
371 sha1((uchar*)m->start, m->end - m->start, m->digest, nil);
372 for(i = 0; i < SHA1dlen; i++)
373 sprint(sdigest+2*i, "%2.2ux", m->digest[i]);
374 m->sdigest = s_copy(sdigest);
380 /* check for new messages on pop server */
381 /* UIDL is not required by RFC 1939, but */
382 /* netscape requires it, so almost every server supports it. */
383 /* we'll use it to make our lives easier. */
386 pop3read(Pop *pop, Mailbox *mb, int doplumb)
388 char *s, *p, *uidl, *f[2];
389 int mesgno, ignore, nnew;
390 Message *m, *next, **l;
392 /* Some POP servers disallow UIDL if the maildrop is empty. */
393 pop3cmd(pop, "STAT");
394 if(!isokay(s = pop3resp(pop)))
397 /* fetch message listing; note messages to grab */
399 if(strncmp(s, "+OK 0 ", 6) != 0) {
400 pop3cmd(pop, "UIDL");
401 if(!isokay(s = pop3resp(pop)))
406 if(strcmp(p, ".") == 0 || strcmp(p, "unexpected eof") == 0)
409 if(tokenize(p, f, 2) != 2)
414 if(strlen(uidl) > 75) /* RFC 1939 says 70 characters max */
419 if(strcmp((*l)->uidl, uidl) == 0) {
420 /* matches mail we already have, note mesgno for deletion */
421 (*l)->mesgno = mesgno;
426 /* old mail no longer in box mark deleted */
428 mailplumb(mb, *l, 1);
437 m = newmessage(mb->root);
441 strcpy(m->uidl, uidl);
443 /* chain in; will fill in message later */
449 /* whatever is left has been removed from the mbox, mark as deleted */
452 mailplumb(mb, *l, 1);
458 /* download new messages */
461 switch(rfork(RFPROC|RFMEM)){
463 fprint(2, "rfork: %r\n");
470 for(m = mb->root->part; m != nil; m = m->next){
473 Bprint(&pop->bout, "LIST %d\r\nRETR %d\r\n", m->mesgno, m->mesgno);
477 /* _exits(nil); jpc */
481 for(m = mb->root->part; m != nil; m = next) {
487 if(s = pop3download(pop, m)) {
488 /* message disappeared? unchain */
489 fprint(2, "download %d: %s\n", m->mesgno, s);
503 if(nnew || mb->vers == 0) {
505 henter(PATH(0, Qtop), mb->name,
506 (Qid){PATH(mb->id, Qmbox), mb->vers, QTDIR}, nil, mb);
513 /* delete marked messages */
516 pop3purge(Pop *pop, Mailbox *mb)
521 switch(rfork(RFPROC|RFMEM)){
523 fprint(2, "rfork: %r\n");
530 for(m = mb->root->part; m != nil; m = next){
532 if(m->deleted && m->refs == 0){
534 Bprint(&pop->bout, "DELE %d\r\n", m->mesgno);
538 /* _exits(nil); jpc */
542 for(m = mb->root->part; m != nil; m = next) {
544 if(m->deleted && m->refs == 0) {
547 pop3cmd(pop, "DELE %d", m->mesgno);
548 if(isokay(pop3resp(pop)))
557 /* connect to pop3 server, sync mailbox */
559 pop3sync(Mailbox *mb, int doplumb)
566 if(err = pop3dial(pop)) {
567 mb->waketime = time(0) + pop->refreshtime;
571 if((err = pop3read(pop, mb, doplumb)) == nil){
573 mb->d->atime = mb->d->mtime = time(0);
576 mb->waketime = time(0) + pop->refreshtime;
580 static char Epop3ctl[] = "bad pop3 control message";
583 pop3ctl(Mailbox *mb, int argc, char **argv)
593 if(argc==1 && strcmp(argv[0], "debug")==0){
598 if(argc==1 && strcmp(argv[0], "nodebug")==0){
603 if(argc==1 && strcmp(argv[0], "thumbprint")==0){
605 freeThumbprints(pop->thumb);
606 /* pop->thumb = initThumbprints("/sys/lib/tls/mail", "/sys/lib/tls/mail.exclude"); jpc */
607 m = unsharp("#9/sys/lib/tls/mail");
608 me = unsharp("#9/sys/lib/tls/mail.exclude");
609 pop->thumb = initThumbprints(m, me);
611 if(strcmp(argv[0], "refresh")==0){
613 pop->refreshtime = 60;
620 pop->refreshtime = n;
628 /* free extra memory associated with mb */
630 pop3close(Mailbox *mb)
640 /* open mailboxes of the form /pop/host/user or /apop/host/user */
643 pop3mbox(Mailbox *mb, char *path)
646 int nf, apop, ppop, popssl, apopssl, apoptls, popnotls, apopnotls, poptls;
651 popssl = strncmp(path, "/pops/", 6) == 0;
652 apopssl = strncmp(path, "/apops/", 7) == 0;
653 poptls = strncmp(path, "/poptls/", 8) == 0;
654 popnotls = strncmp(path, "/popnotls/", 10) == 0;
655 ppop = popssl || poptls || popnotls || strncmp(path, "/pop/", 5) == 0;
656 apoptls = strncmp(path, "/apoptls/", 9) == 0;
657 apopnotls = strncmp(path, "/apopnotls/", 11) == 0;
658 apop = apopssl || apoptls || apopnotls || strncmp(path, "/apop/", 6) == 0;
665 return "out of memory";
667 nf = getfields(path, f, nelem(f), 0, "/");
668 if(nf != 3 && nf != 4) {
670 return "bad pop3 path syntax /[a]pop[tls|ssl]/system[/user]";
673 pop = emalloc(sizeof(*pop));
681 pop->needssl = popssl || apopssl;
682 pop->needtls = poptls || apoptls;
683 pop->refreshtime = 60;
684 pop->notls = popnotls || apopnotls;
685 /* pop->thumb = initThumbprints("/sys/lib/tls/mail", "/sys/lib/tls/mail.exclude"); jpc */
686 m = unsharp("#9/sys/lib/tls/mail");
687 me = unsharp("#9/sys/lib/tls/mail.exclude");
688 pop->thumb = initThumbprints(m, me);
692 mb->close = pop3close;
694 mb->d = emalloc(sizeof(*mb->d));