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