]>
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 fputs(response
, stderr
);
277 netsec_shutdown(nsc
);
282 return NOTOK
; /* NOTREACHED */
287 * Our SASL callback; we are given SASL tokens and then have to format
288 * them according to the protocol requirements, and then process incoming
289 * messages and feed them back into the SASL library.
293 pop_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
294 unsigned int indatalen
, unsigned char **outdata
,
295 unsigned int *outdatalen
, char **errstr
)
302 case NETSEC_SASL_START
:
304 * Generate our AUTH message, but there is a wrinkle.
306 * Technically, according to RFC 5034, if your command INCLUDING
307 * an initial response exceeds 255 octets (including CRLF), you
308 * can't issue this all in one go, but have to just issue the
309 * AUTH command, wait for a blank initial response, and then
313 mech
= netsec_get_sasl_mechanism(nsc
);
317 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
318 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
319 b64len
= strlen(b64data
);
321 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
322 len
= b64len
+ 8 + strlen(mech
);
324 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
327 if (netsec_flush(nsc
, errstr
) != OK
)
329 line
= netsec_readline(nsc
, &len
, errstr
);
333 * If the protocol is being followed correctly, should just
334 * be a "+ ", nothing else.
336 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
337 netsec_err(errstr
, "Did not get expected blank response "
338 "for initial challenge response");
341 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
342 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
343 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
347 if (netsec_flush(nsc
, errstr
) != OK
)
350 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
352 snoopoffset
= 6 + strlen(mech
);
353 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
356 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
359 if (netsec_flush(nsc
, errstr
) != OK
)
363 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
365 if (netsec_flush(nsc
, errstr
) != OK
)
372 * We should get one line back, with our base64 data. Decode that
373 * and feed it back into the SASL library.
375 case NETSEC_SASL_READ
:
376 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
378 line
= netsec_readline(nsc
, &len
, errstr
);
379 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
383 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
384 netsec_err(errstr
, "Invalid format for SASL response");
392 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
395 netsec_err(errstr
, "Unable to decode base64 response");
402 * Our encoding is pretty simple, so this is easy.
405 case NETSEC_SASL_WRITE
:
406 if (indatalen
== 0) {
407 rc
= netsec_printf(nsc
, errstr
, "\r\n");
409 unsigned char *b64data
;
410 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
411 writeBase64raw(indata
, indatalen
, b64data
);
412 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
413 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
414 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
421 if (netsec_flush(nsc
, errstr
) != OK
)
426 * Finish the protocol; we're looking for an +OK
429 case NETSEC_SASL_FINISH
:
430 line
= netsec_readline(nsc
, &len
, errstr
);
434 if (!has_prefix(line
, "+OK")) {
435 netsec_err(errstr
, "Authentication failed: %s", line
);
441 * Cancel the SASL exchange in the middle of the commands; for
442 * POP, that's a single "*".
444 * It's unclear to me if I should be returning errors up; I finally
445 * decided the answer should be "yes", and if the upper layer wants to
446 * ignore them that's their choice.
449 case NETSEC_SASL_CANCEL
:
450 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
452 rc
= netsec_flush(nsc
, errstr
);
462 * Find out number of messages available
463 * and their total size.
467 pop_stat (int *nmsgs
, int *nbytes
)
470 if (command ("STAT") == NOTOK
)
473 *nmsgs
= *nbytes
= 0;
474 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
481 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
483 return traverse (action
, closure
, "RETR %d", msgno
);
488 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
490 int result
, snoopstate
;
492 char buffer
[sizeof(response
)];
495 result
= vcommand (fmt
, ap
);
500 strncpy (buffer
, response
, sizeof(buffer
));
502 if ((snoopstate
= netsec_get_snoop(nsc
)))
503 netsec_set_snoop(nsc
, 0);
506 result
= multiline();
508 result
= (*action
)(closure
, response
);
511 } else if (result
== DONE
) {
512 strncpy(response
, buffer
, sizeof(response
));
518 netsec_set_snoop(nsc
, snoopstate
);
526 return command ("DELE %d", msgno
);
535 i
= command ("QUIT");
546 netsec_shutdown(nsc
);
553 command(const char *fmt
, ...)
559 result
= vcommand(fmt
, ap
);
567 vcommand (const char *fmt
, va_list ap
)
572 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
573 strncpy(response
, errstr
, sizeof(response
));
574 response
[sizeof(response
) - 1] = '\0';
579 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
580 strncpy(response
, errstr
, sizeof(response
));
581 response
[sizeof(response
) - 1] = '\0';
586 if (netsec_flush(nsc
, &errstr
) != OK
) {
587 strncpy(response
, errstr
, sizeof(response
));
588 response
[sizeof(response
) - 1] = '\0';
593 switch (pop_getline (response
, sizeof response
, nsc
)) {
596 fprintf (stderr
, "<--- %s\n", response
);
597 return (*response
== '+' ? OK
: NOTOK
);
602 fputs(response
, stderr
);
608 return NOTOK
; /* NOTREACHED */
615 char buffer
[BUFSIZ
+ LEN(TRM
)];
617 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
619 if (has_prefix(buffer
, TRM
)) {
620 if (buffer
[LEN(TRM
)] == 0)
622 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
625 strncpy (response
, buffer
, sizeof(response
));
631 * This is now just a thin wrapper around netsec_readline().
635 pop_getline (char *s
, int n
, netsec_context
*ns
)
643 p
= netsec_readline(ns
, &len
, &errstr
);
646 strncpy(response
, errstr
, sizeof(response
));
647 response
[sizeof(response
) - 1] = '\0';
653 * If we had an error, it should have been returned already. Since
654 * netsec_readline() strips off the CR-LF ending, just copy the existing
655 * buffer into response now.
657 * We get a length back from netsec_readline, but the rest of the POP
658 * code doesn't handle it; the assumptions are that everything from
659 * the network can be represented as C strings. That should get fixed
663 destlen
= min(len
, (size_t)(n
- 1));
665 memcpy(s
, p
, destlen
);