]>
diplodocus.org Git - nmh/blob - uip/popsbr.c
de9cafbd3ca02f09c5cb67cc4659658d799bc44d
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
)
64 case DONE
: /* Shouldn't be possible, but just in case */
67 if (strncasecmp(response
, "SASL ", 5) == 0) {
69 * We've seen the SASL capability. Grab the mech list
72 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
77 if (!sasl_capability
) {
78 snprintf(response
, sizeof(response
), "POP server does not support "
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.
92 parse_proxy(char *proxy
, char *host
)
96 int hlen
= strlen(host
);
98 unsigned char *cur
, *pro
;
101 /* skip any initial space */
102 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
105 /* calculate required size for argument array */
106 for (cur
= pro
; *cur
; cur
++) {
107 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
109 else if (*cur
== '%' && cur
[1] == 'h') {
112 } else if (!isspace(*cur
))
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])) {
123 } else if (*cur
== '%' && cur
[1] == 'h') {
127 } else if (!isspace(*cur
))
136 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
137 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
146 netsec_set_userid(nsc
, user
);
148 netsec_set_hostname(nsc
, host
);
150 if (oauth_svc
!= NULL
) {
151 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
152 snprintf(response
, sizeof(response
), "OAuth2 not supported");
157 if (proxy
&& *proxy
) {
159 int inpipe
[2]; /* for reading from the server */
160 int outpipe
[2]; /* for sending to the server */
162 if (pipe(inpipe
) < 0) {
163 adios ("inpipe", "pipe");
165 if (pipe(outpipe
) < 0) {
166 adios ("outpipe", "pipe");
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]);
183 /* run the proxy command */
184 argv
=parse_proxy(proxy
, host
);
185 execvp(argv
[0],argv
);
195 /* okay in the parent we do some stuff */
196 close(inpipe
[1]); /* child uses this */
197 close(outpipe
[0]); /* child uses this */
201 /* and write on fd2 */
205 if ((fd1
= client (host
, port
? port
: "pop3", response
,
206 sizeof(response
), snoop
)) == NOTOK
) {
212 SIGNAL (SIGPIPE
, SIG_IGN
);
214 netsec_set_fd(nsc
, fd1
, fd2
);
215 netsec_set_snoop(nsc
, snoop
);
217 if (tls
& P_INITTLS
) {
218 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &errstr
) != OK
) {
219 snprintf(response
, sizeof(response
), "%s", errstr
);
224 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
225 snprintf(response
, sizeof(response
), "%s", errstr
);
232 if (netsec_set_sasl_params(nsc
, "pop", mech
, pop_sasl_callback
,
234 snprintf(response
, sizeof(response
), "%s", errstr
);
240 switch (pop_getline (response
, sizeof response
, nsc
)) {
243 fprintf (stderr
, "<--- %s\n", response
);
244 if (*response
== '+') {
248 char server_mechs
[256];
249 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
251 if (netsec_negotiate_sasl(nsc
, server_mechs
,
253 strncpy(response
, errstr
, sizeof(response
));
254 response
[sizeof(response
) - 1] = '\0';
261 if (!(creds
= nmh_get_credentials(host
, user
)))
263 if (command ("USER %s", nmh_cred_get_user(creds
))
265 if (command("PASS %s", nmh_cred_get_password(creds
))
267 nmh_credentials_free(creds
);
271 nmh_credentials_free(creds
);
273 strncpy (buffer
, response
, sizeof(buffer
));
275 strncpy (response
, buffer
, sizeof(response
));
281 fprintf (stderr
, "%s\n", response
);
282 netsec_shutdown(nsc
);
287 return NOTOK
; /* NOTREACHED */
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.
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
)
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 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
);
352 if (netsec_flush(nsc
, errstr
) != OK
)
355 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
357 snoopoffset
= 6 + strlen(mech
);
358 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
361 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
364 if (netsec_flush(nsc
, errstr
) != OK
)
368 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
370 if (netsec_flush(nsc
, errstr
) != OK
)
377 * We should get one line back, with our base64 data. Decode that
378 * and feed it back into the SASL library.
380 case NETSEC_SASL_READ
:
381 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
383 line
= netsec_readline(nsc
, &len
, errstr
);
384 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
388 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
389 netsec_err(errstr
, "Invalid format for SASL response");
397 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
400 netsec_err(errstr
, "Unable to decode base64 response");
407 * Our encoding is pretty simple, so this is easy.
410 case NETSEC_SASL_WRITE
:
411 if (indatalen
== 0) {
412 rc
= netsec_printf(nsc
, errstr
, "\r\n");
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
);
426 if (netsec_flush(nsc
, errstr
) != OK
)
431 * Finish the protocol; we're looking for an +OK
434 case NETSEC_SASL_FINISH
:
435 line
= netsec_readline(nsc
, &len
, errstr
);
439 if (!has_prefix(line
, "+OK")) {
440 netsec_err(errstr
, "Authentication failed: %s", line
);
446 * Cancel the SASL exchange in the middle of the commands; for
447 * POP, that's a single "*".
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.
454 case NETSEC_SASL_CANCEL
:
455 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
457 rc
= netsec_flush(nsc
, errstr
);
467 * Find out number of messages available
468 * and their total size.
472 pop_stat (int *nmsgs
, int *nbytes
)
475 if (command ("STAT") == NOTOK
)
478 *nmsgs
= *nbytes
= 0;
479 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
486 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
488 return traverse (action
, closure
, "RETR %d", msgno
);
493 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
495 int result
, snoopstate
;
497 char buffer
[sizeof(response
)];
500 result
= vcommand (fmt
, ap
);
505 strncpy (buffer
, response
, sizeof(buffer
));
507 if ((snoopstate
= netsec_get_snoop(nsc
)))
508 netsec_set_snoop(nsc
, 0);
511 result
= multiline();
513 result
= (*action
)(closure
, response
);
516 } else if (result
== DONE
) {
517 strncpy(response
, buffer
, sizeof(response
));
523 netsec_set_snoop(nsc
, snoopstate
);
531 return command ("DELE %d", msgno
);
540 i
= command ("QUIT");
551 netsec_shutdown(nsc
);
558 command(const char *fmt
, ...)
564 result
= vcommand(fmt
, ap
);
572 vcommand (const char *fmt
, va_list ap
)
577 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
578 strncpy(response
, errstr
, sizeof(response
));
579 response
[sizeof(response
) - 1] = '\0';
584 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
585 strncpy(response
, errstr
, sizeof(response
));
586 response
[sizeof(response
) - 1] = '\0';
591 if (netsec_flush(nsc
, &errstr
) != OK
) {
592 strncpy(response
, errstr
, sizeof(response
));
593 response
[sizeof(response
) - 1] = '\0';
598 switch (pop_getline (response
, sizeof response
, nsc
)) {
601 fprintf (stderr
, "<--- %s\n", response
);
602 return (*response
== '+' ? OK
: NOTOK
);
607 fprintf (stderr
, "%s\n", response
);
611 return NOTOK
; /* NOTREACHED */
618 char buffer
[BUFSIZ
+ LEN(TRM
)];
620 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
622 if (has_prefix(buffer
, TRM
)) {
623 if (buffer
[LEN(TRM
)] == 0)
625 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
628 strncpy (response
, buffer
, sizeof(response
));
634 * This is now just a thin wrapper around netsec_readline().
638 pop_getline (char *s
, int n
, netsec_context
*ns
)
646 p
= netsec_readline(ns
, &len
, &errstr
);
649 strncpy(response
, errstr
, sizeof(response
));
650 response
[sizeof(response
) - 1] = '\0';
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.
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
666 destlen
= min(len
, (size_t)(n
- 1));
668 memcpy(s
, p
, destlen
);