]>
diplodocus.org Git - nmh/blob - uip/popsbr.c
c17d9993345b7488c1e715ea3bb1d21c98172e9d
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/client.h"
10 #include "sbr/error.h"
16 #include "h/signals.h"
17 #include "sbr/base64.h"
21 static int poprint
= 0;
23 char response
[BUFSIZ
];
24 static netsec_context
*nsc
= NULL
;
30 static int command(const char *, ...) CHECK_PRINTF(1, 2);
31 static int multiline(void);
33 static int traverse(int (*)(void *, char *), void *closure
,
34 const char *, ...) CHECK_PRINTF(3, 4);
35 static int vcommand(const char *, va_list) CHECK_PRINTF(1, 0);
36 static int pop_getline (char *, int, netsec_context
*);
37 static int pop_sasl_callback(enum sasl_message_type
, unsigned const char *,
38 unsigned int, unsigned char **, unsigned int *,
42 check_mech(char *server_mechs
, size_t server_mechs_size
)
45 bool sasl_capability
= false;
48 * First off, we're going to send the CAPA command to see if we can
49 * even support the AUTH command, and if we do, then we'll get a
50 * list of mechanisms the server supports. If we don't support
51 * the CAPA command, then it's unlikely that we will support
55 if (command("CAPA") == NOTOK
) {
56 snprintf(response
, sizeof(response
),
57 "The POP CAPA command failed; POP server does not "
62 while ((status
= multiline()) != DONE
) {
66 if (strncasecmp(response
, "SASL ", 5) == 0) {
67 /* We've seen the SASL capability. Grab the mech list. */
68 sasl_capability
= true;
69 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
73 if (!sasl_capability
) {
74 snprintf(response
, sizeof(response
), "POP server does not support "
83 * Split string containing proxy command into an array of arguments
84 * suitable for passing to exec. Returned array must be freed. Shouldn't
85 * be possible to call this with host set to NULL.
88 parse_proxy(char *proxy
, char *host
)
92 int hlen
= strlen(host
);
94 unsigned char *cur
, *pro
;
97 /* skip any initial space */
98 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
101 /* calculate required size for argument array */
102 for (cur
= pro
; *cur
; cur
++) {
103 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
105 else if (*cur
== '%' && cur
[1] == 'h') {
108 } else if (!isspace(*cur
))
112 /* put together list of arguments */
113 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
114 c
= *pargv
= mh_xmalloc(plen
);
115 for (cur
= pro
; *cur
; cur
++) {
116 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
119 } else if (*cur
== '%' && cur
[1] == 'h') {
123 } else if (!isspace(*cur
))
132 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
133 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
142 netsec_set_userid(nsc
, user
);
144 netsec_set_hostname(nsc
, host
);
146 if (oauth_svc
!= NULL
) {
147 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
148 snprintf(response
, sizeof(response
), "OAuth2 not supported");
153 if (proxy
&& *proxy
) {
155 int inpipe
[2]; /* for reading from the server */
156 int outpipe
[2]; /* for sending to the server */
158 if (pipe(inpipe
) < 0) {
159 adios ("inpipe", "pipe");
161 if (pipe(outpipe
) < 0) {
162 adios ("outpipe", "pipe");
172 dup2(outpipe
[0],0); /* connect read end of connection */
173 dup2(inpipe
[1], 1); /* connect write end of connection */
174 if(inpipe
[0]>1) close(inpipe
[0]);
175 if(inpipe
[1]>1) close(inpipe
[1]);
176 if(outpipe
[0]>1) close(outpipe
[0]);
177 if(outpipe
[1]>1) close(outpipe
[1]);
179 /* run the proxy command */
180 argv
=parse_proxy(proxy
, host
);
181 execvp(argv
[0],argv
);
191 /* okay in the parent we do some stuff */
192 close(inpipe
[1]); /* child uses this */
193 close(outpipe
[0]); /* child uses this */
197 /* and write on fd2 */
201 if ((fd1
= client (host
, port
? port
: "pop3", response
,
202 sizeof(response
), snoop
)) == NOTOK
) {
208 SIGNAL (SIGPIPE
, SIG_IGN
);
210 netsec_set_fd(nsc
, fd1
, fd2
);
211 netsec_set_snoop(nsc
, snoop
);
213 if (tls
& P_INITTLS
) {
214 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &errstr
) != OK
) {
215 snprintf(response
, sizeof(response
), "%s", errstr
);
220 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
221 snprintf(response
, sizeof(response
), "%s", errstr
);
228 if (netsec_set_sasl_params(nsc
, "pop", mech
, pop_sasl_callback
,
229 NULL
, &errstr
) != OK
) {
230 snprintf(response
, sizeof(response
), "%s", errstr
);
236 switch (pop_getline (response
, sizeof response
, nsc
)) {
239 fprintf (stderr
, "<--- %s\n", response
);
240 if (*response
== '+') {
244 char server_mechs
[256];
245 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
247 if (netsec_negotiate_sasl(nsc
, server_mechs
,
249 strncpy(response
, errstr
, sizeof(response
));
250 response
[sizeof(response
) - 1] = '\0';
257 if (!(creds
= nmh_get_credentials(host
, user
)))
259 if (command ("USER %s", nmh_cred_get_user(creds
))
261 if (command("PASS %s", nmh_cred_get_password(creds
))
263 nmh_credentials_free(creds
);
267 nmh_credentials_free(creds
);
269 strncpy (buffer
, response
, sizeof(buffer
));
271 strncpy (response
, buffer
, sizeof(response
));
277 fputs(response
, stderr
);
280 netsec_shutdown(nsc
);
285 return NOTOK
; /* NOTREACHED */
290 * Our SASL callback; we are given SASL tokens and then have to format
291 * them according to the protocol requirements, and then process incoming
292 * messages and feed them back into the SASL library.
296 pop_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
297 unsigned int indatalen
, unsigned char **outdata
,
298 unsigned int *outdatalen
, void *context
, char **errstr
)
306 case NETSEC_SASL_START
:
308 * Generate our AUTH message, but there is a wrinkle.
310 * Technically, according to RFC 5034, if your command INCLUDING
311 * an initial response exceeds 255 octets (including CRLF), you
312 * can't issue this all in one go, but have to just issue the
313 * AUTH command, wait for a blank initial response, and then
317 mech
= netsec_get_sasl_mechanism(nsc
);
321 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
322 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
323 b64len
= strlen(b64data
);
325 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
326 len
= b64len
+ 8 + strlen(mech
);
328 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
331 if (netsec_flush(nsc
, errstr
) != OK
)
333 line
= netsec_readline(nsc
, &len
, errstr
);
337 * If the protocol is being followed correctly, should just
338 * be a "+ ", nothing else.
340 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
341 netsec_err(errstr
, "Did not get expected blank response "
342 "for initial challenge response");
345 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
349 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
350 rc
= netsec_flush(nsc
, errstr
);
351 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
355 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
360 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
362 snoopoffset
= 6 + strlen(mech
);
363 rc
= netsec_flush(nsc
, errstr
);
364 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
369 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
371 if (netsec_flush(nsc
, errstr
) != OK
)
378 * We should get one line back, with our base64 data. Decode that
379 * and feed it back into the SASL library.
381 case NETSEC_SASL_READ
:
382 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
384 line
= netsec_readline(nsc
, &len
, errstr
);
385 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
389 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
390 netsec_err(errstr
, "Invalid format for SASL response");
398 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
401 netsec_err(errstr
, "Unable to decode base64 response");
408 * Our encoding is pretty simple, so this is easy.
411 case NETSEC_SASL_WRITE
:
412 if (indatalen
== 0) {
413 rc
= netsec_printf(nsc
, errstr
, "\r\n");
415 unsigned char *b64data
;
416 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
417 writeBase64raw(indata
, indatalen
, b64data
);
418 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
426 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
427 rc
= netsec_flush(nsc
, errstr
);
428 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
434 * Finish the protocol; we're looking for an +OK
437 case NETSEC_SASL_FINISH
:
438 line
= netsec_readline(nsc
, &len
, errstr
);
442 if (!has_prefix(line
, "+OK")) {
443 netsec_err(errstr
, "Authentication failed: %s", line
);
449 * Cancel the SASL exchange in the middle of the commands; for
450 * POP, that's a single "*".
452 * It's unclear to me if I should be returning errors up; I finally
453 * decided the answer should be "yes", and if the upper layer wants to
454 * ignore them that's their choice.
457 case NETSEC_SASL_CANCEL
:
458 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
460 rc
= netsec_flush(nsc
, errstr
);
470 * Find out number of messages available
471 * and their total size.
475 pop_stat (int *nmsgs
, int *nbytes
)
478 if (command ("STAT") == NOTOK
)
481 *nmsgs
= *nbytes
= 0;
482 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
489 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
491 return traverse (action
, closure
, "RETR %d", msgno
);
496 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
498 int result
, snoopstate
;
500 char buffer
[sizeof(response
)];
503 result
= vcommand (fmt
, ap
);
508 strncpy (buffer
, response
, sizeof(buffer
));
510 if ((snoopstate
= netsec_get_snoop(nsc
)))
511 netsec_set_snoop(nsc
, 0);
514 result
= multiline();
516 result
= (*action
)(closure
, response
);
519 } else if (result
== DONE
) {
520 strncpy(response
, buffer
, sizeof(response
));
526 netsec_set_snoop(nsc
, snoopstate
);
534 return command ("DELE %d", msgno
);
543 i
= command ("QUIT");
554 netsec_shutdown(nsc
);
561 command(const char *fmt
, ...)
567 result
= vcommand(fmt
, ap
);
575 vcommand (const char *fmt
, va_list ap
)
580 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
581 strncpy(response
, errstr
, sizeof(response
));
582 response
[sizeof(response
) - 1] = '\0';
587 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
588 strncpy(response
, errstr
, sizeof(response
));
589 response
[sizeof(response
) - 1] = '\0';
594 if (netsec_flush(nsc
, &errstr
) != OK
) {
595 strncpy(response
, errstr
, sizeof(response
));
596 response
[sizeof(response
) - 1] = '\0';
601 switch (pop_getline (response
, sizeof response
, nsc
)) {
604 fprintf (stderr
, "<--- %s\n", response
);
605 return *response
== '+' ? OK
: NOTOK
;
610 fputs(response
, stderr
);
616 return NOTOK
; /* NOTREACHED */
623 char buffer
[BUFSIZ
+ LEN(TRM
)];
625 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
627 if (has_prefix(buffer
, TRM
)) {
628 if (buffer
[LEN(TRM
)] == 0)
630 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
633 strncpy (response
, buffer
, sizeof(response
));
639 * This is now just a thin wrapper around netsec_readline().
643 pop_getline (char *s
, int n
, netsec_context
*ns
)
651 p
= netsec_readline(ns
, &len
, &errstr
);
654 strncpy(response
, errstr
, sizeof(response
));
655 response
[sizeof(response
) - 1] = '\0';
661 * If we had an error, it should have been returned already. Since
662 * netsec_readline() strips off the CR-LF ending, just copy the existing
663 * buffer into response now.
665 * We get a length back from netsec_readline, but the rest of the POP
666 * code doesn't handle it; the assumptions are that everything from
667 * the network can be represented as C strings. That should get fixed
671 destlen
= min(len
, (size_t)(n
- 1));
673 memcpy(s
, p
, destlen
);