]>
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
)
42 int status
, sasl_capability
= 0;
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
52 if (command("CAPA") == NOTOK
) {
53 snprintf(response
, sizeof(response
),
54 "The POP CAPA command failed; POP server does not "
59 while ((status
= multiline()) != DONE
) {
63 if (strncasecmp(response
, "SASL ", 5) == 0) {
64 /* We've seen the SASL capability. Grab the mech list. */
66 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
70 if (!sasl_capability
) {
71 snprintf(response
, sizeof(response
), "POP server does not support "
80 * Split string containing proxy command into an array of arguments
81 * suitable for passing to exec. Returned array must be freed. Shouldn't
82 * be possible to call this with host set to NULL.
85 parse_proxy(char *proxy
, char *host
)
89 int hlen
= strlen(host
);
91 unsigned char *cur
, *pro
;
94 /* skip any initial space */
95 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
98 /* calculate required size for argument array */
99 for (cur
= pro
; *cur
; cur
++) {
100 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
102 else if (*cur
== '%' && cur
[1] == 'h') {
105 } else if (!isspace(*cur
))
109 /* put together list of arguments */
110 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
111 c
= *pargv
= mh_xmalloc(plen
* sizeof(char));
112 for (cur
= pro
; *cur
; cur
++) {
113 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
116 } else if (*cur
== '%' && cur
[1] == 'h') {
120 } else if (!isspace(*cur
))
129 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
130 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
139 netsec_set_userid(nsc
, user
);
141 netsec_set_hostname(nsc
, host
);
143 if (oauth_svc
!= NULL
) {
144 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
145 snprintf(response
, sizeof(response
), "OAuth2 not supported");
150 if (proxy
&& *proxy
) {
152 int inpipe
[2]; /* for reading from the server */
153 int outpipe
[2]; /* for sending to the server */
155 if (pipe(inpipe
) < 0) {
156 adios ("inpipe", "pipe");
158 if (pipe(outpipe
) < 0) {
159 adios ("outpipe", "pipe");
169 dup2(outpipe
[0],0); /* connect read end of connection */
170 dup2(inpipe
[1], 1); /* connect write end of connection */
171 if(inpipe
[0]>1) close(inpipe
[0]);
172 if(inpipe
[1]>1) close(inpipe
[1]);
173 if(outpipe
[0]>1) close(outpipe
[0]);
174 if(outpipe
[1]>1) close(outpipe
[1]);
176 /* run the proxy command */
177 argv
=parse_proxy(proxy
, host
);
178 execvp(argv
[0],argv
);
188 /* okay in the parent we do some stuff */
189 close(inpipe
[1]); /* child uses this */
190 close(outpipe
[0]); /* child uses this */
194 /* and write on fd2 */
198 if ((fd1
= client (host
, port
? port
: "pop3", response
,
199 sizeof(response
), snoop
)) == NOTOK
) {
205 SIGNAL (SIGPIPE
, SIG_IGN
);
207 netsec_set_fd(nsc
, fd1
, fd2
);
208 netsec_set_snoop(nsc
, snoop
);
210 if (tls
& P_INITTLS
) {
211 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &errstr
) != OK
) {
212 snprintf(response
, sizeof(response
), "%s", errstr
);
217 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
218 snprintf(response
, sizeof(response
), "%s", errstr
);
225 if (netsec_set_sasl_params(nsc
, "pop", mech
, pop_sasl_callback
,
227 snprintf(response
, sizeof(response
), "%s", errstr
);
233 switch (pop_getline (response
, sizeof response
, nsc
)) {
236 fprintf (stderr
, "<--- %s\n", response
);
237 if (*response
== '+') {
241 char server_mechs
[256];
242 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
244 if (netsec_negotiate_sasl(nsc
, server_mechs
,
246 strncpy(response
, errstr
, sizeof(response
));
247 response
[sizeof(response
) - 1] = '\0';
254 if (!(creds
= nmh_get_credentials(host
, user
)))
256 if (command ("USER %s", nmh_cred_get_user(creds
))
258 if (command("PASS %s", nmh_cred_get_password(creds
))
260 nmh_credentials_free(creds
);
264 nmh_credentials_free(creds
);
266 strncpy (buffer
, response
, sizeof(buffer
));
268 strncpy (response
, buffer
, sizeof(response
));
274 fprintf (stderr
, "%s\n", response
);
275 netsec_shutdown(nsc
);
280 return NOTOK
; /* NOTREACHED */
285 * Our SASL callback; we are given SASL tokens and then have to format
286 * them according to the protocol requirements, and then process incoming
287 * messages and feed them back into the SASL library.
291 pop_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
292 unsigned int indatalen
, unsigned char **outdata
,
293 unsigned int *outdatalen
, char **errstr
)
300 case NETSEC_SASL_START
:
302 * Generate our AUTH message, but there is a wrinkle.
304 * Technically, according to RFC 5034, if your command INCLUDING
305 * an initial response exceeds 255 octets (including CRLF), you
306 * can't issue this all in one go, but have to just issue the
307 * AUTH command, wait for a blank initial response, and then
311 mech
= netsec_get_sasl_mechanism(nsc
);
315 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
316 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
317 b64len
= strlen(b64data
);
319 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
320 len
= b64len
+ 8 + strlen(mech
);
322 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
325 if (netsec_flush(nsc
, errstr
) != OK
)
327 line
= netsec_readline(nsc
, &len
, errstr
);
331 * If the protocol is being followed correctly, should just
332 * be a "+ ", nothing else.
334 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
335 netsec_err(errstr
, "Did not get expected blank response "
336 "for initial challenge response");
339 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
340 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
341 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
345 if (netsec_flush(nsc
, errstr
) != OK
)
348 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
350 snoopoffset
= 6 + strlen(mech
);
351 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
354 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
357 if (netsec_flush(nsc
, errstr
) != OK
)
361 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
363 if (netsec_flush(nsc
, errstr
) != OK
)
370 * We should get one line back, with our base64 data. Decode that
371 * and feed it back into the SASL library.
373 case NETSEC_SASL_READ
:
374 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
376 line
= netsec_readline(nsc
, &len
, errstr
);
377 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
381 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
382 netsec_err(errstr
, "Invalid format for SASL response");
390 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
393 netsec_err(errstr
, "Unable to decode base64 response");
400 * Our encoding is pretty simple, so this is easy.
403 case NETSEC_SASL_WRITE
:
404 if (indatalen
== 0) {
405 rc
= netsec_printf(nsc
, errstr
, "\r\n");
407 unsigned char *b64data
;
408 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
409 writeBase64raw(indata
, indatalen
, b64data
);
410 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
411 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
412 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
419 if (netsec_flush(nsc
, errstr
) != OK
)
424 * Finish the protocol; we're looking for an +OK
427 case NETSEC_SASL_FINISH
:
428 line
= netsec_readline(nsc
, &len
, errstr
);
432 if (!has_prefix(line
, "+OK")) {
433 netsec_err(errstr
, "Authentication failed: %s", line
);
439 * Cancel the SASL exchange in the middle of the commands; for
440 * POP, that's a single "*".
442 * It's unclear to me if I should be returning errors up; I finally
443 * decided the answer should be "yes", and if the upper layer wants to
444 * ignore them that's their choice.
447 case NETSEC_SASL_CANCEL
:
448 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
450 rc
= netsec_flush(nsc
, errstr
);
460 * Find out number of messages available
461 * and their total size.
465 pop_stat (int *nmsgs
, int *nbytes
)
468 if (command ("STAT") == NOTOK
)
471 *nmsgs
= *nbytes
= 0;
472 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
479 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
481 return traverse (action
, closure
, "RETR %d", msgno
);
486 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
488 int result
, snoopstate
;
490 char buffer
[sizeof(response
)];
493 result
= vcommand (fmt
, ap
);
498 strncpy (buffer
, response
, sizeof(buffer
));
500 if ((snoopstate
= netsec_get_snoop(nsc
)))
501 netsec_set_snoop(nsc
, 0);
504 result
= multiline();
506 result
= (*action
)(closure
, response
);
509 } else if (result
== DONE
) {
510 strncpy(response
, buffer
, sizeof(response
));
516 netsec_set_snoop(nsc
, snoopstate
);
524 return command ("DELE %d", msgno
);
533 i
= command ("QUIT");
544 netsec_shutdown(nsc
);
551 command(const char *fmt
, ...)
557 result
= vcommand(fmt
, ap
);
565 vcommand (const char *fmt
, va_list ap
)
570 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
571 strncpy(response
, errstr
, sizeof(response
));
572 response
[sizeof(response
) - 1] = '\0';
577 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
578 strncpy(response
, errstr
, sizeof(response
));
579 response
[sizeof(response
) - 1] = '\0';
584 if (netsec_flush(nsc
, &errstr
) != OK
) {
585 strncpy(response
, errstr
, sizeof(response
));
586 response
[sizeof(response
) - 1] = '\0';
591 switch (pop_getline (response
, sizeof response
, nsc
)) {
594 fprintf (stderr
, "<--- %s\n", response
);
595 return (*response
== '+' ? OK
: NOTOK
);
600 fprintf (stderr
, "%s\n", response
);
604 return NOTOK
; /* NOTREACHED */
611 char buffer
[BUFSIZ
+ LEN(TRM
)];
613 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
615 if (has_prefix(buffer
, TRM
)) {
616 if (buffer
[LEN(TRM
)] == 0)
618 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
621 strncpy (response
, buffer
, sizeof(response
));
627 * This is now just a thin wrapper around netsec_readline().
631 pop_getline (char *s
, int n
, netsec_context
*ns
)
639 p
= netsec_readline(ns
, &len
, &errstr
);
642 strncpy(response
, errstr
, sizeof(response
));
643 response
[sizeof(response
) - 1] = '\0';
649 * If we had an error, it should have been returned already. Since
650 * netsec_readline() strips off the CR-LF ending, just copy the existing
651 * buffer into response now.
653 * We get a length back from netsec_readline, but the rest of the POP
654 * code doesn't handle it; the assumptions are that everything from
655 * the network can be represented as C strings. That should get fixed
659 destlen
= min(len
, (size_t)(n
- 1));
661 memcpy(s
, p
, destlen
);