]>
diplodocus.org Git - nmh/blob - uip/popsbr.c
1 /* popsbr.c -- POP client subroutines
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.
14 #include <h/signals.h>
15 #include "sbr/base64.h"
19 static int poprint
= 0;
21 char response
[BUFSIZ
];
22 static netsec_context
*nsc
= NULL
;
28 static int command(const char *, ...) CHECK_PRINTF(1, 2);
29 static int multiline(void);
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 *,
40 check_mech(char *server_mechs
, size_t server_mechs_size
)
43 bool sasl_capability
= false;
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
53 if (command("CAPA") == NOTOK
) {
54 snprintf(response
, sizeof(response
),
55 "The POP CAPA command failed; POP server does not "
60 while ((status
= multiline()) != DONE
) {
64 if (strncasecmp(response
, "SASL ", 5) == 0) {
65 /* We've seen the SASL capability. Grab the mech list. */
66 sasl_capability
= true;
67 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
71 if (!sasl_capability
) {
72 snprintf(response
, sizeof(response
), "POP server does not support "
81 * Split string containing proxy command into an array of arguments
82 * suitable for passing to exec. Returned array must be freed. Shouldn't
83 * be possible to call this with host set to NULL.
86 parse_proxy(char *proxy
, char *host
)
90 int hlen
= strlen(host
);
92 unsigned char *cur
, *pro
;
95 /* skip any initial space */
96 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
99 /* calculate required size for argument array */
100 for (cur
= pro
; *cur
; cur
++) {
101 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
103 else if (*cur
== '%' && cur
[1] == 'h') {
106 } else if (!isspace(*cur
))
110 /* put together list of arguments */
111 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
112 c
= *pargv
= mh_xmalloc(plen
);
113 for (cur
= pro
; *cur
; cur
++) {
114 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
117 } else if (*cur
== '%' && cur
[1] == 'h') {
121 } else if (!isspace(*cur
))
130 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
131 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
140 netsec_set_userid(nsc
, user
);
142 netsec_set_hostname(nsc
, host
);
144 if (oauth_svc
!= NULL
) {
145 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
146 snprintf(response
, sizeof(response
), "OAuth2 not supported");
151 if (proxy
&& *proxy
) {
153 int inpipe
[2]; /* for reading from the server */
154 int outpipe
[2]; /* for sending to the server */
156 if (pipe(inpipe
) < 0) {
157 adios ("inpipe", "pipe");
159 if (pipe(outpipe
) < 0) {
160 adios ("outpipe", "pipe");
170 dup2(outpipe
[0],0); /* connect read end of connection */
171 dup2(inpipe
[1], 1); /* connect write end of connection */
172 if(inpipe
[0]>1) close(inpipe
[0]);
173 if(inpipe
[1]>1) close(inpipe
[1]);
174 if(outpipe
[0]>1) close(outpipe
[0]);
175 if(outpipe
[1]>1) close(outpipe
[1]);
177 /* run the proxy command */
178 argv
=parse_proxy(proxy
, host
);
179 execvp(argv
[0],argv
);
189 /* okay in the parent we do some stuff */
190 close(inpipe
[1]); /* child uses this */
191 close(outpipe
[0]); /* child uses this */
195 /* and write on fd2 */
199 if ((fd1
= client (host
, port
? port
: "pop3", response
,
200 sizeof(response
), snoop
)) == NOTOK
) {
206 SIGNAL (SIGPIPE
, SIG_IGN
);
208 netsec_set_fd(nsc
, fd1
, fd2
);
209 netsec_set_snoop(nsc
, snoop
);
211 if (tls
& P_INITTLS
) {
212 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &errstr
) != OK
) {
213 snprintf(response
, sizeof(response
), "%s", errstr
);
218 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
219 snprintf(response
, sizeof(response
), "%s", errstr
);
226 if (netsec_set_sasl_params(nsc
, "pop", mech
, pop_sasl_callback
,
227 NULL
, &errstr
) != OK
) {
228 snprintf(response
, sizeof(response
), "%s", errstr
);
234 switch (pop_getline (response
, sizeof response
, nsc
)) {
237 fprintf (stderr
, "<--- %s\n", response
);
238 if (*response
== '+') {
242 char server_mechs
[256];
243 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
245 if (netsec_negotiate_sasl(nsc
, server_mechs
,
247 strncpy(response
, errstr
, sizeof(response
));
248 response
[sizeof(response
) - 1] = '\0';
255 if (!(creds
= nmh_get_credentials(host
, user
)))
257 if (command ("USER %s", nmh_cred_get_user(creds
))
259 if (command("PASS %s", nmh_cred_get_password(creds
))
261 nmh_credentials_free(creds
);
265 nmh_credentials_free(creds
);
267 strncpy (buffer
, response
, sizeof(buffer
));
269 strncpy (response
, buffer
, sizeof(response
));
275 fputs(response
, stderr
);
278 netsec_shutdown(nsc
);
283 return NOTOK
; /* NOTREACHED */
288 * Our SASL callback; we are given SASL tokens and then have to format
289 * them according to the protocol requirements, and then process incoming
290 * messages and feed them back into the SASL library.
294 pop_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
295 unsigned int indatalen
, unsigned char **outdata
,
296 unsigned int *outdatalen
, void *context
, char **errstr
)
304 case NETSEC_SASL_START
:
306 * Generate our AUTH message, but there is a wrinkle.
308 * Technically, according to RFC 5034, if your command INCLUDING
309 * an initial response exceeds 255 octets (including CRLF), you
310 * can't issue this all in one go, but have to just issue the
311 * AUTH command, wait for a blank initial response, and then
315 mech
= netsec_get_sasl_mechanism(nsc
);
319 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
320 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
321 b64len
= strlen(b64data
);
323 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
324 len
= b64len
+ 8 + strlen(mech
);
326 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
329 if (netsec_flush(nsc
, errstr
) != OK
)
331 line
= netsec_readline(nsc
, &len
, errstr
);
335 * If the protocol is being followed correctly, should just
336 * be a "+ ", nothing else.
338 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
339 netsec_err(errstr
, "Did not get expected blank response "
340 "for initial challenge response");
343 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
347 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
348 rc
= netsec_flush(nsc
, errstr
);
349 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
353 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
358 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
360 snoopoffset
= 6 + strlen(mech
);
361 rc
= netsec_flush(nsc
, errstr
);
362 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
367 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
369 if (netsec_flush(nsc
, errstr
) != OK
)
376 * We should get one line back, with our base64 data. Decode that
377 * and feed it back into the SASL library.
379 case NETSEC_SASL_READ
:
380 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
382 line
= netsec_readline(nsc
, &len
, errstr
);
383 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
387 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
388 netsec_err(errstr
, "Invalid format for SASL response");
396 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
399 netsec_err(errstr
, "Unable to decode base64 response");
406 * Our encoding is pretty simple, so this is easy.
409 case NETSEC_SASL_WRITE
:
410 if (indatalen
== 0) {
411 rc
= netsec_printf(nsc
, errstr
, "\r\n");
413 unsigned char *b64data
;
414 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
415 writeBase64raw(indata
, indatalen
, b64data
);
416 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
424 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
425 rc
= netsec_flush(nsc
, errstr
);
426 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
432 * Finish the protocol; we're looking for an +OK
435 case NETSEC_SASL_FINISH
:
436 line
= netsec_readline(nsc
, &len
, errstr
);
440 if (!has_prefix(line
, "+OK")) {
441 netsec_err(errstr
, "Authentication failed: %s", line
);
447 * Cancel the SASL exchange in the middle of the commands; for
448 * POP, that's a single "*".
450 * It's unclear to me if I should be returning errors up; I finally
451 * decided the answer should be "yes", and if the upper layer wants to
452 * ignore them that's their choice.
455 case NETSEC_SASL_CANCEL
:
456 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
458 rc
= netsec_flush(nsc
, errstr
);
468 * Find out number of messages available
469 * and their total size.
473 pop_stat (int *nmsgs
, int *nbytes
)
476 if (command ("STAT") == NOTOK
)
479 *nmsgs
= *nbytes
= 0;
480 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
487 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
489 return traverse (action
, closure
, "RETR %d", msgno
);
494 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
496 int result
, snoopstate
;
498 char buffer
[sizeof(response
)];
501 result
= vcommand (fmt
, ap
);
506 strncpy (buffer
, response
, sizeof(buffer
));
508 if ((snoopstate
= netsec_get_snoop(nsc
)))
509 netsec_set_snoop(nsc
, 0);
512 result
= multiline();
514 result
= (*action
)(closure
, response
);
517 } else if (result
== DONE
) {
518 strncpy(response
, buffer
, sizeof(response
));
524 netsec_set_snoop(nsc
, snoopstate
);
532 return command ("DELE %d", msgno
);
541 i
= command ("QUIT");
552 netsec_shutdown(nsc
);
559 command(const char *fmt
, ...)
565 result
= vcommand(fmt
, ap
);
573 vcommand (const char *fmt
, va_list ap
)
578 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
579 strncpy(response
, errstr
, sizeof(response
));
580 response
[sizeof(response
) - 1] = '\0';
585 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
586 strncpy(response
, errstr
, sizeof(response
));
587 response
[sizeof(response
) - 1] = '\0';
592 if (netsec_flush(nsc
, &errstr
) != OK
) {
593 strncpy(response
, errstr
, sizeof(response
));
594 response
[sizeof(response
) - 1] = '\0';
599 switch (pop_getline (response
, sizeof response
, nsc
)) {
602 fprintf (stderr
, "<--- %s\n", response
);
603 return *response
== '+' ? OK
: NOTOK
;
608 fputs(response
, stderr
);
614 return NOTOK
; /* NOTREACHED */
621 char buffer
[BUFSIZ
+ LEN(TRM
)];
623 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
625 if (has_prefix(buffer
, TRM
)) {
626 if (buffer
[LEN(TRM
)] == 0)
628 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
631 strncpy (response
, buffer
, sizeof(response
));
637 * This is now just a thin wrapper around netsec_readline().
641 pop_getline (char *s
, int n
, netsec_context
*ns
)
649 p
= netsec_readline(ns
, &len
, &errstr
);
652 strncpy(response
, errstr
, sizeof(response
));
653 response
[sizeof(response
) - 1] = '\0';
659 * If we had an error, it should have been returned already. Since
660 * netsec_readline() strips off the CR-LF ending, just copy the existing
661 * buffer into response now.
663 * We get a length back from netsec_readline, but the rest of the POP
664 * code doesn't handle it; the assumptions are that everything from
665 * the network can be represented as C strings. That should get fixed
669 destlen
= min(len
, (size_t)(n
- 1));
671 memcpy(s
, p
, destlen
);