]> diplodocus.org Git - nmh/blob - uip/popsbr.c
70d710446a40deca875817fa080edd5b2eed7722
[nmh] / uip / popsbr.c
1 /*
2 * popsbr.c -- POP client subroutines
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <h/oauth.h>
12 #include <h/netsec.h>
13
14 #include <h/popsbr.h>
15 #include <h/signals.h>
16
17 #define TRM "."
18 #define TRMLEN (sizeof TRM - 1)
19
20 static int poprint = 0;
21 static int pophack = 0;
22
23 char response[BUFSIZ];
24 static netsec_context *nsc = NULL;
25
26 /*
27 * static prototypes
28 */
29
30 static int command(const char *, ...);
31 static int multiline(void);
32
33 static int traverse (int (*)(char *), const char *, ...);
34 static int vcommand(const char *, va_list);
35 static int pop_getline (char *, int, netsec_context *);
36 static int pop_sasl_callback(enum sasl_message_type, unsigned const char *,
37 unsigned int, unsigned char **, unsigned int *,
38 char **);
39
40 static int
41 check_mech(char *server_mechs, size_t server_mechs_size)
42 {
43 int status, sasl_capability = 0;
44
45 /*
46 * First off, we're going to send the CAPA command to see if we can
47 * even support the AUTH command, and if we do, then we'll get a
48 * list of mechanisms the server supports. If we don't support
49 * the CAPA command, then it's unlikely that we will support
50 * SASL
51 */
52
53 if (command("CAPA") == NOTOK) {
54 snprintf(response, sizeof(response),
55 "The POP CAPA command failed; POP server does not "
56 "support SASL");
57 return NOTOK;
58 }
59
60 while ((status = multiline()) != DONE)
61 switch (status) {
62 case NOTOK:
63 return NOTOK;
64 break;
65 case DONE: /* Shouldn't be possible, but just in case */
66 break;
67 case OK:
68 if (strncasecmp(response, "SASL ", 5) == 0) {
69 /*
70 * We've seen the SASL capability. Grab the mech list
71 */
72 sasl_capability++;
73 strncpy(server_mechs, response + 5, server_mechs_size);
74 }
75 break;
76 }
77
78 if (!sasl_capability) {
79 snprintf(response, sizeof(response), "POP server does not support "
80 "SASL");
81 return NOTOK;
82 }
83
84 return OK;
85 }
86
87 /*
88 * Split string containing proxy command into an array of arguments
89 * suitable for passing to exec. Returned array must be freed. Shouldn't
90 * be possible to call this with host set to NULL.
91 */
92 char **
93 parse_proxy(char *proxy, char *host)
94 {
95 char **pargv, **p;
96 int pargc = 2;
97 int hlen = strlen(host);
98 int plen = 1;
99 unsigned char *cur, *pro;
100 char *c;
101
102 /* skip any initial space */
103 for (pro = (unsigned char *) proxy; isspace(*pro); pro++)
104 continue;
105
106 /* calculate required size for argument array */
107 for (cur = pro; *cur; cur++) {
108 if (isspace(*cur) && cur[1] && !isspace(cur[1]))
109 plen++, pargc++;
110 else if (*cur == '%' && cur[1] == 'h') {
111 plen += hlen;
112 cur++;
113 } else if (!isspace(*cur))
114 plen++;
115 }
116
117 /* put together list of arguments */
118 p = pargv = mh_xmalloc(pargc * sizeof(char *));
119 c = *pargv = mh_xmalloc(plen * sizeof(char));
120 for (cur = pro; *cur; cur++) {
121 if (isspace(*cur) && cur[1] && !isspace(cur[1])) {
122 *c++ = '\0';
123 *++p = c;
124 } else if (*cur == '%' && cur[1] == 'h') {
125 strcpy (c, host);
126 c += hlen;
127 cur++;
128 } else if (!isspace(*cur))
129 *c++ = *cur;
130 }
131 *c = '\0';
132 *++p = NULL;
133 return pargv;
134 }
135
136 int
137 pop_init (char *host, char *port, char *user, char *pass, char *proxy,
138 int snoop, int sasl, char *mech, int tls, const char *oauth_svc)
139 {
140 int fd1, fd2;
141 char buffer[BUFSIZ];
142 char *errstr;
143
144 nsc = netsec_init();
145
146 if (user)
147 netsec_set_userid(nsc, user);
148
149 if (oauth_svc != NULL) {
150 if (netsec_set_oauth_service(nsc, oauth_svc) != OK) {
151 snprintf(response, sizeof(response), "OAuth2 not supported");
152 return NOTOK;
153 }
154 }
155
156 if (proxy && *proxy) {
157 int pid;
158 int inpipe[2]; /* for reading from the server */
159 int outpipe[2]; /* for sending to the server */
160
161 if (pipe(inpipe) < 0) {
162 adios ("inpipe", "pipe");
163 }
164 if (pipe(outpipe) < 0) {
165 adios ("outpipe", "pipe");
166 }
167
168 pid=fork();
169 if (pid==0) {
170 char **argv;
171
172 /* in child */
173 close(0);
174 close(1);
175 dup2(outpipe[0],0); /* connect read end of connection */
176 dup2(inpipe[1], 1); /* connect write end of connection */
177 if(inpipe[0]>1) close(inpipe[0]);
178 if(inpipe[1]>1) close(inpipe[1]);
179 if(outpipe[0]>1) close(outpipe[0]);
180 if(outpipe[1]>1) close(outpipe[1]);
181
182 /* run the proxy command */
183 argv=parse_proxy(proxy, host);
184 execvp(argv[0],argv);
185
186 perror(argv[0]);
187 close(0);
188 close(1);
189 free(*argv);
190 free(argv);
191 exit(10);
192 }
193
194 /* okay in the parent we do some stuff */
195 close(inpipe[1]); /* child uses this */
196 close(outpipe[0]); /* child uses this */
197
198 /* we read on fd1 */
199 fd1=inpipe[0];
200 /* and write on fd2 */
201 fd2=outpipe[1];
202
203 } else {
204 if ((fd1 = client (host, port ? port : "pop3", response,
205 sizeof(response), snoop)) == NOTOK) {
206 return NOTOK;
207 }
208 fd2 = fd1;
209 }
210
211 SIGNAL (SIGPIPE, SIG_IGN);
212
213 netsec_set_fd(nsc, fd1, fd2);
214 netsec_set_snoop(nsc, snoop);
215
216 if (tls) {
217 if (netsec_set_tls(nsc, 1, &errstr) != OK) {
218 snprintf(response, sizeof(response), "%s", errstr);
219 free(errstr);
220 return NOTOK;
221 }
222
223 if (netsec_negotiate_tls(nsc, &errstr) != OK) {
224 snprintf(response, sizeof(response), "%s", errstr);
225 free(errstr);
226 return NOTOK;
227 }
228 }
229
230 if (sasl) {
231 if (netsec_set_sasl_params(nsc, host, "pop", mech,
232 pop_sasl_callback, &errstr) != OK) {
233 snprintf(response, sizeof(response), "%s", errstr);
234 free(errstr);
235 return NOTOK;
236 }
237 }
238
239 switch (pop_getline (response, sizeof response, nsc)) {
240 case OK:
241 if (poprint)
242 fprintf (stderr, "<--- %s\n", response);
243 if (*response == '+') {
244 if (sasl) {
245 char server_mechs[256];
246 if (check_mech(server_mechs, sizeof(server_mechs)) != OK)
247 return NOTOK;
248 if (netsec_negotiate_sasl(nsc, server_mechs,
249 &errstr) != OK) {
250 strncpy(response, errstr, sizeof(response));
251 response[sizeof(response) - 1] = '\0';
252 free(errstr);
253 return NOTOK;
254 }
255 return OK;
256 } else
257 if (command ("USER %s", user) != NOTOK
258 && command ("%s %s", (pophack++, "PASS"),
259 pass) != NOTOK)
260 return OK;
261 }
262 strncpy (buffer, response, sizeof(buffer));
263 command ("QUIT");
264 strncpy (response, buffer, sizeof(response));
265 /* and fall */
266
267 case NOTOK:
268 case DONE:
269 if (poprint)
270 fprintf (stderr, "%s\n", response);
271 netsec_shutdown(nsc, 1);
272 nsc = NULL;
273 return NOTOK;
274 }
275
276 return NOTOK; /* NOTREACHED */
277 }
278
279
280 /*
281 * Our SASL callback; we are given SASL tokens and then have to format
282 * them according to the protocol requirements, and then process incoming
283 * messages and feed them back into the SASL library.
284 */
285
286 static int
287 pop_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
288 unsigned int indatalen, unsigned char **outdata,
289 unsigned int *outdatalen, char **errstr)
290 {
291 int rc, snoopoffset;
292 char *mech, *line;
293 size_t len, b64len;
294
295 switch (mtype) {
296 case NETSEC_SASL_START:
297 /*
298 * Generate our AUTH message, but there is a wrinkle.
299 *
300 * Technically, according to RFC 5034, if your command INCLUDING
301 * an initial response exceeds 255 octets (including CRLF), you
302 * can't issue this all in one go, but have to just issue the
303 * AUTH command, wait for a blank initial response, and then
304 * send your data.
305 */
306
307 mech = netsec_get_sasl_mechanism(nsc);
308
309 if (indatalen) {
310 char *b64data;
311 b64data = mh_xmalloc(BASE64SIZE(indatalen));
312 writeBase64raw(indata, indatalen, (unsigned char *) b64data);
313 b64len = strlen(b64data);
314
315 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
316 len = b64len + 8 + strlen(mech);
317 if (len > 255) {
318 rc = netsec_printf(nsc, errstr, "AUTH %s\r\n", mech);
319 if (rc)
320 return NOTOK;
321 if (netsec_flush(nsc, errstr) != OK)
322 return NOTOK;
323 line = netsec_readline(nsc, &len, errstr);
324 if (! line)
325 return NOTOK;
326 /*
327 * If the protocol is being followed correctly, should just
328 * be a "+ ", nothing else.
329 */
330 if (len != 2 || strcmp(line, "+ ") != 0) {
331 netsec_err(errstr, "Did not get expected blank response "
332 "for initial challenge response");
333 return NOTOK;
334 }
335 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
336 rc = netsec_printf(nsc, errstr, "%s\r\n", b64data);
337 netsec_set_snoop_callback(nsc, NULL, NULL);
338 free(b64data);
339 if (rc != OK)
340 return NOTOK;
341 if (netsec_flush(nsc, errstr) != OK)
342 return NOTOK;
343 } else {
344 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder,
345 &snoopoffset);
346 snoopoffset = 6 + strlen(mech);
347 rc = netsec_printf(nsc, errstr, "AUTH %s %s\r\n", mech,
348 b64data);
349 free(b64data);
350 if (rc != OK)
351 return NOTOK;
352 if (netsec_flush(nsc, errstr) != OK)
353 return NOTOK;
354 }
355 } else {
356 if (netsec_printf(nsc, errstr, "AUTH %s\r\n", mech) != OK)
357 return NOTOK;
358 if (netsec_flush(nsc, errstr) != OK)
359 return NOTOK;
360 }
361
362 break;
363
364 /*
365 * We should get one line back, with our base64 data. Decode that
366 * and feed it back into the SASL library.
367 */
368 case NETSEC_SASL_READ:
369 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, &snoopoffset);
370 snoopoffset = 2;
371 line = netsec_readline(nsc, &len, errstr);
372 netsec_set_snoop_callback(nsc, NULL, NULL);
373
374 if (line == NULL)
375 return NOTOK;
376 if (len < 2 || (len == 2 && strcmp(line, "+ ") != 0)) {
377 netsec_err(errstr, "Invalid format for SASL response");
378 return NOTOK;
379 }
380
381 if (len == 2) {
382 *outdata = NULL;
383 *outdatalen = 0;
384 } else {
385 rc = decodeBase64(line + 2, (const char **) outdata, &len, 0, NULL);
386 *outdatalen = len;
387 if (rc != OK)
388 return NOTOK;
389 }
390 break;
391
392 /*
393 * Our encoding is pretty simple, so this is easy.
394 */
395
396 case NETSEC_SASL_WRITE:
397 if (indatalen == 0) {
398 rc = netsec_printf(nsc, errstr, "\r\n");
399 } else {
400 unsigned char *b64data;
401 b64data = mh_xmalloc(BASE64SIZE(indatalen));
402 writeBase64raw(indata, indatalen, b64data);
403 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
404 rc = netsec_printf(nsc, errstr, "%s\r\n", b64data);
405 netsec_set_snoop_callback(nsc, NULL, NULL);
406 free(b64data);
407 }
408
409 if (rc != OK)
410 return NOTOK;
411
412 if (netsec_flush(nsc, errstr) != OK)
413 return NOTOK;
414
415 return OK;
416 break;
417
418 /*
419 * Finish the protocol; we're looking for an +OK
420 */
421
422 case NETSEC_SASL_FINISH:
423 line = netsec_readline(nsc, &len, errstr);
424 if (line == NULL)
425 return NOTOK;
426
427 if (strncmp(line, "+OK", 3) != 0) {
428 netsec_err(errstr, "Authentication failed: %s", line);
429 return NOTOK;
430 }
431 break;
432
433 /*
434 * Cancel the SASL exchange in the middle of the commands; for
435 * POP, that's a single "*".
436 *
437 * It's unclear to me if I should be returning errors up; I finally
438 * decided the answer should be "yes", and if the upper layer wants to
439 * ignore them that's their choice.
440 */
441
442 case NETSEC_SASL_CANCEL:
443 rc = netsec_printf(nsc, errstr, "*\r\n");
444 if (rc == OK)
445 rc = netsec_flush(nsc, errstr);
446 if (rc != OK)
447 return NOTOK;
448 break;
449 }
450 return OK;
451 }
452
453 /*
454 * Find out number of messages available
455 * and their total size.
456 */
457
458 int
459 pop_stat (int *nmsgs, int *nbytes)
460 {
461
462 if (command ("STAT") == NOTOK)
463 return NOTOK;
464
465 *nmsgs = *nbytes = 0;
466 sscanf (response, "+OK %d %d", nmsgs, nbytes);
467
468 return OK;
469 }
470
471
472 int
473 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes)
474 {
475 int i;
476 int *ids = NULL;
477
478 if (msgno) {
479 if (command ("LIST %d", msgno) == NOTOK)
480 return NOTOK;
481 *msgs = *bytes = 0;
482 if (ids) {
483 *ids = 0;
484 sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
485 }
486 else
487 sscanf (response, "+OK %d %d", msgs, bytes);
488 return OK;
489 }
490
491 if (command ("LIST") == NOTOK)
492 return NOTOK;
493
494 for (i = 0; i < *nmsgs; i++)
495 switch (multiline ()) {
496 case NOTOK:
497 return NOTOK;
498 case DONE:
499 *nmsgs = ++i;
500 return OK;
501 case OK:
502 *msgs = *bytes = 0;
503 if (ids) {
504 *ids = 0;
505 sscanf (response, "%d %d %d",
506 msgs++, bytes++, ids++);
507 }
508 else
509 sscanf (response, "%d %d", msgs++, bytes++);
510 break;
511 }
512 for (;;)
513 switch (multiline ()) {
514 case NOTOK:
515 return NOTOK;
516 case DONE:
517 return OK;
518 case OK:
519 break;
520 }
521 }
522
523
524 int
525 pop_retr (int msgno, int (*action)(char *))
526 {
527 return traverse (action, "RETR %d", msgno);
528 }
529
530
531 static int
532 traverse (int (*action)(char *), const char *fmt, ...)
533 {
534 int result;
535 va_list ap;
536 char buffer[sizeof(response)];
537
538 va_start(ap, fmt);
539 result = vcommand (fmt, ap);
540 va_end(ap);
541
542 if (result == NOTOK)
543 return NOTOK;
544 strncpy (buffer, response, sizeof(buffer));
545
546 for (;;)
547 switch (multiline ()) {
548 case NOTOK:
549 return NOTOK;
550
551 case DONE:
552 strncpy (response, buffer, sizeof(response));
553 return OK;
554
555 case OK:
556 (*action) (response);
557 break;
558 }
559 }
560
561
562 int
563 pop_dele (int msgno)
564 {
565 return command ("DELE %d", msgno);
566 }
567
568
569 int
570 pop_noop (void)
571 {
572 return command ("NOOP");
573 }
574
575
576 int
577 pop_rset (void)
578 {
579 return command ("RSET");
580 }
581
582
583 int
584 pop_top (int msgno, int lines, int (*action)(char *))
585 {
586 return traverse (action, "TOP %d %d", msgno, lines);
587 }
588
589
590 int
591 pop_quit (void)
592 {
593 int i;
594
595 i = command ("QUIT");
596 pop_done ();
597
598 return i;
599 }
600
601
602 int
603 pop_done (void)
604 {
605 if (nsc)
606 netsec_shutdown(nsc, 1);
607
608 return OK;
609 }
610
611
612 int
613 command(const char *fmt, ...)
614 {
615 va_list ap;
616 int result;
617
618 va_start(ap, fmt);
619 result = vcommand(fmt, ap);
620 va_end(ap);
621
622 return result;
623 }
624
625
626 static int
627 vcommand (const char *fmt, va_list ap)
628 {
629 /* char *cp; */
630 char *errstr;
631
632 if (netsec_vprintf(nsc, &errstr, fmt, ap) != OK) {
633 strncpy(response, errstr, sizeof(response));
634 response[sizeof(response) - 1] = '\0';
635 free(errstr);
636 return NOTOK;
637 }
638
639 if (netsec_printf(nsc, &errstr, "\r\n") != OK) {
640 strncpy(response, errstr, sizeof(response));
641 response[sizeof(response) - 1] = '\0';
642 free(errstr);
643 return NOTOK;
644 }
645
646 if (netsec_flush(nsc, &errstr) != OK) {
647 strncpy(response, errstr, sizeof(response));
648 response[sizeof(response) - 1] = '\0';
649 free(errstr);
650 return NOTOK;
651 }
652
653 switch (pop_getline (response, sizeof response, nsc)) {
654 case OK:
655 if (poprint)
656 fprintf (stderr, "<--- %s\n", response);
657 return (*response == '+' ? OK : NOTOK);
658
659 case NOTOK:
660 case DONE:
661 if (poprint)
662 fprintf (stderr, "%s\n", response);
663 return NOTOK;
664 }
665
666 return NOTOK; /* NOTREACHED */
667 }
668
669
670 int
671 multiline (void)
672 {
673 char buffer[BUFSIZ + TRMLEN];
674
675 if (pop_getline (buffer, sizeof buffer, nsc) != OK)
676 return NOTOK;
677 if (strncmp (buffer, TRM, TRMLEN) == 0) {
678 if (buffer[TRMLEN] == 0)
679 return DONE;
680 else
681 strncpy (response, buffer + TRMLEN, sizeof(response));
682 }
683 else
684 strncpy (response, buffer, sizeof(response));
685
686 return OK;
687 }
688
689 /*
690 * This is now just a thin wrapper around netsec_readline().
691 */
692
693 static int
694 pop_getline (char *s, int n, netsec_context *ns)
695 {
696 /* int c = -2; */
697 char *p;
698 size_t len, destlen;
699 /* int rc; */
700 char *errstr;
701
702 p = netsec_readline(ns, &len, &errstr);
703
704 if (p == NULL) {
705 strncpy(response, errstr, sizeof(response));
706 response[sizeof(response) - 1] = '\0';
707 free(errstr);
708 return NOTOK;
709 }
710
711 /*
712 * If we had an error, it should have been returned already. Since
713 * netsec_readline() strips off the CR-LF ending, just copy the existing
714 * buffer into response now.
715 *
716 * We get a length back from netsec_readline, but the rest of the POP
717 * code doesn't handle it; the assumptions are that everything from
718 * the network can be respresented as C strings. That should get fixed
719 * someday.
720 */
721
722 destlen = len < ((size_t) (n - 1)) ? len : n - 1;
723
724 memcpy(s, p, destlen);
725 s[destlen] = '\0';
726
727 return OK;
728 }