]>
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.
9 #include "sbr/credentials.h"
10 #include "sbr/client.h"
11 #include "sbr/error.h"
17 #include "h/signals.h"
18 #include "sbr/base64.h"
22 static int poprint
= 0;
24 char response
[BUFSIZ
];
25 static netsec_context
*nsc
= NULL
;
31 static int command(const char *, ...) CHECK_PRINTF(1, 2);
32 static int multiline(void);
34 static int traverse(int (*)(void *, char *), void *closure
,
35 const char *, ...) CHECK_PRINTF(3, 4);
36 static int vcommand(const char *, va_list) CHECK_PRINTF(1, 0);
37 static int pop_getline (char *, int, netsec_context
*);
38 static int pop_sasl_callback(enum sasl_message_type
, unsigned const char *,
39 unsigned int, unsigned char **, unsigned int *,
43 check_mech(char *server_mechs
, size_t server_mechs_size
)
46 bool sasl_capability
= false;
49 * First off, we're going to send the CAPA command to see if we can
50 * even support the AUTH command, and if we do, then we'll get a
51 * list of mechanisms the server supports. If we don't support
52 * the CAPA command, then it's unlikely that we will support
56 if (command("CAPA") == NOTOK
) {
57 snprintf(response
, sizeof(response
),
58 "The POP CAPA command failed; POP server does not "
63 while ((status
= multiline()) != DONE
) {
67 if (strncasecmp(response
, "SASL ", 5) == 0) {
68 /* We've seen the SASL capability. Grab the mech list. */
69 sasl_capability
= true;
70 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
74 if (!sasl_capability
) {
75 snprintf(response
, sizeof(response
), "POP server does not support "
84 * Split string containing proxy command into an array of arguments
85 * suitable for passing to exec. Returned array must be freed. Shouldn't
86 * be possible to call this with host set to NULL.
89 parse_proxy(char *proxy
, char *host
)
93 int hlen
= strlen(host
);
95 unsigned char *cur
, *pro
;
98 /* skip any initial space */
99 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
102 /* calculate required size for argument array */
103 for (cur
= pro
; *cur
; cur
++) {
104 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
106 else if (*cur
== '%' && cur
[1] == 'h') {
109 } else if (!isspace(*cur
))
113 /* put together list of arguments */
114 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
115 c
= *pargv
= mh_xmalloc(plen
);
116 for (cur
= pro
; *cur
; cur
++) {
117 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
120 } else if (*cur
== '%' && cur
[1] == 'h') {
124 } else if (!isspace(*cur
))
133 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
134 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
143 netsec_set_userid(nsc
, user
);
145 netsec_set_hostname(nsc
, host
);
147 if (oauth_svc
!= NULL
) {
148 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
149 snprintf(response
, sizeof(response
), "OAuth2 not supported");
154 if (proxy
&& *proxy
) {
156 int inpipe
[2]; /* for reading from the server */
157 int outpipe
[2]; /* for sending to the server */
159 if (pipe(inpipe
) < 0) {
160 adios ("inpipe", "pipe");
162 if (pipe(outpipe
) < 0) {
163 adios ("outpipe", "pipe");
173 dup2(outpipe
[0],0); /* connect read end of connection */
174 dup2(inpipe
[1], 1); /* connect write end of connection */
175 if(inpipe
[0]>1) close(inpipe
[0]);
176 if(inpipe
[1]>1) close(inpipe
[1]);
177 if(outpipe
[0]>1) close(outpipe
[0]);
178 if(outpipe
[1]>1) close(outpipe
[1]);
180 /* run the proxy command */
181 argv
=parse_proxy(proxy
, host
);
182 execvp(argv
[0],argv
);
192 /* okay in the parent we do some stuff */
193 close(inpipe
[1]); /* child uses this */
194 close(outpipe
[0]); /* child uses this */
198 /* and write on fd2 */
202 if ((fd1
= client (host
, port
? port
: "pop3", response
,
203 sizeof(response
), snoop
)) == NOTOK
) {
209 SIGNAL (SIGPIPE
, SIG_IGN
);
211 netsec_set_fd(nsc
, fd1
, fd2
);
212 netsec_set_snoop(nsc
, snoop
);
214 if (tls
& P_INITTLS
) {
215 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &errstr
) != OK
) {
216 snprintf(response
, sizeof(response
), "%s", errstr
);
221 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
222 snprintf(response
, sizeof(response
), "%s", errstr
);
229 if (netsec_set_sasl_params(nsc
, "pop", mech
, pop_sasl_callback
,
230 NULL
, &errstr
) != OK
) {
231 snprintf(response
, sizeof(response
), "%s", errstr
);
237 switch (pop_getline (response
, sizeof response
, nsc
)) {
240 fprintf (stderr
, "<--- %s\n", response
);
241 if (*response
== '+') {
245 char server_mechs
[256];
246 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
248 if (netsec_negotiate_sasl(nsc
, server_mechs
,
250 strncpy(response
, errstr
, sizeof(response
));
251 response
[sizeof(response
) - 1] = '\0';
258 if (!(creds
= nmh_get_credentials(host
, user
)))
260 if (command ("USER %s", nmh_cred_get_user(creds
))
262 if (command("PASS %s", nmh_cred_get_password(creds
))
264 nmh_credentials_free(creds
);
268 nmh_credentials_free(creds
);
270 strncpy (buffer
, response
, sizeof(buffer
));
272 strncpy (response
, buffer
, sizeof(response
));
278 fputs(response
, stderr
);
281 netsec_shutdown(nsc
);
286 return NOTOK
; /* NOTREACHED */
291 * Our SASL callback; we are given SASL tokens and then have to format
292 * them according to the protocol requirements, and then process incoming
293 * messages and feed them back into the SASL library.
297 pop_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
298 unsigned int indatalen
, unsigned char **outdata
,
299 unsigned int *outdatalen
, void *context
, char **errstr
)
307 case NETSEC_SASL_START
:
309 * Generate our AUTH message, but there is a wrinkle.
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
318 mech
= netsec_get_sasl_mechanism(nsc
);
322 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
323 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
324 b64len
= strlen(b64data
);
326 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
327 len
= b64len
+ 8 + strlen(mech
);
329 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
332 if (netsec_flush(nsc
, errstr
) != OK
)
334 line
= netsec_readline(nsc
, &len
, errstr
);
338 * If the protocol is being followed correctly, should just
339 * be a "+ ", nothing else.
341 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
342 netsec_err(errstr
, "Did not get expected blank response "
343 "for initial challenge response");
346 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
350 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
351 rc
= netsec_flush(nsc
, errstr
);
352 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
356 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
361 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
363 snoopoffset
= 6 + strlen(mech
);
364 rc
= netsec_flush(nsc
, errstr
);
365 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
370 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
372 if (netsec_flush(nsc
, errstr
) != OK
)
379 * We should get one line back, with our base64 data. Decode that
380 * and feed it back into the SASL library.
382 case NETSEC_SASL_READ
:
383 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
385 line
= netsec_readline(nsc
, &len
, errstr
);
386 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
390 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
391 netsec_err(errstr
, "Invalid format for SASL response");
399 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
402 netsec_err(errstr
, "Unable to decode base64 response");
409 * Our encoding is pretty simple, so this is easy.
412 case NETSEC_SASL_WRITE
:
413 if (indatalen
== 0) {
414 rc
= netsec_printf(nsc
, errstr
, "\r\n");
416 unsigned char *b64data
;
417 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
418 writeBase64raw(indata
, indatalen
, b64data
);
419 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
427 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
428 rc
= netsec_flush(nsc
, errstr
);
429 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
435 * Finish the protocol; we're looking for an +OK
438 case NETSEC_SASL_FINISH
:
439 line
= netsec_readline(nsc
, &len
, errstr
);
443 if (!has_prefix(line
, "+OK")) {
444 netsec_err(errstr
, "Authentication failed: %s", line
);
450 * Cancel the SASL exchange in the middle of the commands; for
451 * POP, that's a single "*".
453 * It's unclear to me if I should be returning errors up; I finally
454 * decided the answer should be "yes", and if the upper layer wants to
455 * ignore them that's their choice.
458 case NETSEC_SASL_CANCEL
:
459 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
461 rc
= netsec_flush(nsc
, errstr
);
471 * Find out number of messages available
472 * and their total size.
476 pop_stat (int *nmsgs
, int *nbytes
)
479 if (command ("STAT") == NOTOK
)
482 *nmsgs
= *nbytes
= 0;
483 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
490 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
492 return traverse (action
, closure
, "RETR %d", msgno
);
497 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
499 int result
, snoopstate
;
501 char buffer
[sizeof(response
)];
504 result
= vcommand (fmt
, ap
);
509 strncpy (buffer
, response
, sizeof(buffer
));
511 if ((snoopstate
= netsec_get_snoop(nsc
)))
512 netsec_set_snoop(nsc
, 0);
515 result
= multiline();
517 result
= (*action
)(closure
, response
);
520 } else if (result
== DONE
) {
521 strncpy(response
, buffer
, sizeof(response
));
527 netsec_set_snoop(nsc
, snoopstate
);
535 return command ("DELE %d", msgno
);
544 i
= command ("QUIT");
555 netsec_shutdown(nsc
);
562 command(const char *fmt
, ...)
568 result
= vcommand(fmt
, ap
);
576 vcommand (const char *fmt
, va_list ap
)
581 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
582 strncpy(response
, errstr
, sizeof(response
));
583 response
[sizeof(response
) - 1] = '\0';
588 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
589 strncpy(response
, errstr
, sizeof(response
));
590 response
[sizeof(response
) - 1] = '\0';
595 if (netsec_flush(nsc
, &errstr
) != OK
) {
596 strncpy(response
, errstr
, sizeof(response
));
597 response
[sizeof(response
) - 1] = '\0';
602 switch (pop_getline (response
, sizeof response
, nsc
)) {
605 fprintf (stderr
, "<--- %s\n", response
);
606 return *response
== '+' ? OK
: NOTOK
;
611 fputs(response
, stderr
);
617 return NOTOK
; /* NOTREACHED */
624 char buffer
[BUFSIZ
+ LEN(TRM
)];
626 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
628 if (has_prefix(buffer
, TRM
)) {
629 if (buffer
[LEN(TRM
)] == 0)
631 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
634 strncpy (response
, buffer
, sizeof(response
));
640 * This is now just a thin wrapper around netsec_readline().
644 pop_getline (char *s
, int n
, netsec_context
*ns
)
652 p
= netsec_readline(ns
, &len
, &errstr
);
655 strncpy(response
, errstr
, sizeof(response
));
656 response
[sizeof(response
) - 1] = '\0';
662 * If we had an error, it should have been returned already. Since
663 * netsec_readline() strips off the CR-LF ending, just copy the existing
664 * buffer into response now.
666 * We get a length back from netsec_readline, but the rest of the POP
667 * code doesn't handle it; the assumptions are that everything from
668 * the network can be represented as C strings. That should get fixed
672 destlen
= min(len
, (size_t)(n
- 1));
674 memcpy(s
, p
, destlen
);