]>
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.
15 #include "h/signals.h"
16 #include "sbr/base64.h"
20 static int poprint
= 0;
22 char response
[BUFSIZ
];
23 static netsec_context
*nsc
= NULL
;
29 static int command(const char *, ...) CHECK_PRINTF(1, 2);
30 static int multiline(void);
32 static int traverse(int (*)(void *, char *), void *closure
,
33 const char *, ...) CHECK_PRINTF(3, 4);
34 static int vcommand(const char *, va_list) CHECK_PRINTF(1, 0);
35 static int pop_getline (char *, int, netsec_context
*);
36 static int pop_sasl_callback(enum sasl_message_type
, unsigned const char *,
37 unsigned int, unsigned char **, unsigned int *,
41 check_mech(char *server_mechs
, size_t server_mechs_size
)
44 bool sasl_capability
= false;
47 * First off, we're going to send the CAPA command to see if we can
48 * even support the AUTH command, and if we do, then we'll get a
49 * list of mechanisms the server supports. If we don't support
50 * the CAPA command, then it's unlikely that we will support
54 if (command("CAPA") == NOTOK
) {
55 snprintf(response
, sizeof(response
),
56 "The POP CAPA command failed; POP server does not "
61 while ((status
= multiline()) != DONE
) {
65 if (strncasecmp(response
, "SASL ", 5) == 0) {
66 /* We've seen the SASL capability. Grab the mech list. */
67 sasl_capability
= true;
68 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
72 if (!sasl_capability
) {
73 snprintf(response
, sizeof(response
), "POP server does not support "
82 * Split string containing proxy command into an array of arguments
83 * suitable for passing to exec. Returned array must be freed. Shouldn't
84 * be possible to call this with host set to NULL.
87 parse_proxy(char *proxy
, char *host
)
91 int hlen
= strlen(host
);
93 unsigned char *cur
, *pro
;
96 /* skip any initial space */
97 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
100 /* calculate required size for argument array */
101 for (cur
= pro
; *cur
; cur
++) {
102 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
104 else if (*cur
== '%' && cur
[1] == 'h') {
107 } else if (!isspace(*cur
))
111 /* put together list of arguments */
112 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
113 c
= *pargv
= mh_xmalloc(plen
);
114 for (cur
= pro
; *cur
; cur
++) {
115 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
118 } else if (*cur
== '%' && cur
[1] == 'h') {
122 } else if (!isspace(*cur
))
131 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
132 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
141 netsec_set_userid(nsc
, user
);
143 netsec_set_hostname(nsc
, host
);
145 if (oauth_svc
!= NULL
) {
146 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
147 snprintf(response
, sizeof(response
), "OAuth2 not supported");
152 if (proxy
&& *proxy
) {
154 int inpipe
[2]; /* for reading from the server */
155 int outpipe
[2]; /* for sending to the server */
157 if (pipe(inpipe
) < 0) {
158 adios ("inpipe", "pipe");
160 if (pipe(outpipe
) < 0) {
161 adios ("outpipe", "pipe");
171 dup2(outpipe
[0],0); /* connect read end of connection */
172 dup2(inpipe
[1], 1); /* connect write end of connection */
173 if(inpipe
[0]>1) close(inpipe
[0]);
174 if(inpipe
[1]>1) close(inpipe
[1]);
175 if(outpipe
[0]>1) close(outpipe
[0]);
176 if(outpipe
[1]>1) close(outpipe
[1]);
178 /* run the proxy command */
179 argv
=parse_proxy(proxy
, host
);
180 execvp(argv
[0],argv
);
190 /* okay in the parent we do some stuff */
191 close(inpipe
[1]); /* child uses this */
192 close(outpipe
[0]); /* child uses this */
196 /* and write on fd2 */
200 if ((fd1
= client (host
, port
? port
: "pop3", response
,
201 sizeof(response
), snoop
)) == NOTOK
) {
207 SIGNAL (SIGPIPE
, SIG_IGN
);
209 netsec_set_fd(nsc
, fd1
, fd2
);
210 netsec_set_snoop(nsc
, snoop
);
212 if (tls
& P_INITTLS
) {
213 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &errstr
) != OK
) {
214 snprintf(response
, sizeof(response
), "%s", errstr
);
219 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
220 snprintf(response
, sizeof(response
), "%s", errstr
);
227 if (netsec_set_sasl_params(nsc
, "pop", mech
, pop_sasl_callback
,
228 NULL
, &errstr
) != OK
) {
229 snprintf(response
, sizeof(response
), "%s", errstr
);
235 switch (pop_getline (response
, sizeof response
, nsc
)) {
238 fprintf (stderr
, "<--- %s\n", response
);
239 if (*response
== '+') {
243 char server_mechs
[256];
244 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
246 if (netsec_negotiate_sasl(nsc
, server_mechs
,
248 strncpy(response
, errstr
, sizeof(response
));
249 response
[sizeof(response
) - 1] = '\0';
256 if (!(creds
= nmh_get_credentials(host
, user
)))
258 if (command ("USER %s", nmh_cred_get_user(creds
))
260 if (command("PASS %s", nmh_cred_get_password(creds
))
262 nmh_credentials_free(creds
);
266 nmh_credentials_free(creds
);
268 strncpy (buffer
, response
, sizeof(buffer
));
270 strncpy (response
, buffer
, sizeof(response
));
276 fputs(response
, stderr
);
279 netsec_shutdown(nsc
);
284 return NOTOK
; /* NOTREACHED */
289 * Our SASL callback; we are given SASL tokens and then have to format
290 * them according to the protocol requirements, and then process incoming
291 * messages and feed them back into the SASL library.
295 pop_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
296 unsigned int indatalen
, unsigned char **outdata
,
297 unsigned int *outdatalen
, void *context
, char **errstr
)
305 case NETSEC_SASL_START
:
307 * Generate our AUTH message, but there is a wrinkle.
309 * Technically, according to RFC 5034, if your command INCLUDING
310 * an initial response exceeds 255 octets (including CRLF), you
311 * can't issue this all in one go, but have to just issue the
312 * AUTH command, wait for a blank initial response, and then
316 mech
= netsec_get_sasl_mechanism(nsc
);
320 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
321 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
322 b64len
= strlen(b64data
);
324 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
325 len
= b64len
+ 8 + strlen(mech
);
327 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
330 if (netsec_flush(nsc
, errstr
) != OK
)
332 line
= netsec_readline(nsc
, &len
, errstr
);
336 * If the protocol is being followed correctly, should just
337 * be a "+ ", nothing else.
339 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
340 netsec_err(errstr
, "Did not get expected blank response "
341 "for initial challenge response");
344 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
348 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
349 rc
= netsec_flush(nsc
, errstr
);
350 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
354 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
359 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
361 snoopoffset
= 6 + strlen(mech
);
362 rc
= netsec_flush(nsc
, errstr
);
363 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
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 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
425 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
426 rc
= netsec_flush(nsc
, errstr
);
427 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
433 * Finish the protocol; we're looking for an +OK
436 case NETSEC_SASL_FINISH
:
437 line
= netsec_readline(nsc
, &len
, errstr
);
441 if (!has_prefix(line
, "+OK")) {
442 netsec_err(errstr
, "Authentication failed: %s", line
);
448 * Cancel the SASL exchange in the middle of the commands; for
449 * POP, that's a single "*".
451 * It's unclear to me if I should be returning errors up; I finally
452 * decided the answer should be "yes", and if the upper layer wants to
453 * ignore them that's their choice.
456 case NETSEC_SASL_CANCEL
:
457 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
459 rc
= netsec_flush(nsc
, errstr
);
469 * Find out number of messages available
470 * and their total size.
474 pop_stat (int *nmsgs
, int *nbytes
)
477 if (command ("STAT") == NOTOK
)
480 *nmsgs
= *nbytes
= 0;
481 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
488 pop_retr (int msgno
, int (*action
)(void *, char *), void *closure
)
490 return traverse (action
, closure
, "RETR %d", msgno
);
495 traverse (int (*action
)(void *, char *), void *closure
, const char *fmt
, ...)
497 int result
, snoopstate
;
499 char buffer
[sizeof(response
)];
502 result
= vcommand (fmt
, ap
);
507 strncpy (buffer
, response
, sizeof(buffer
));
509 if ((snoopstate
= netsec_get_snoop(nsc
)))
510 netsec_set_snoop(nsc
, 0);
513 result
= multiline();
515 result
= (*action
)(closure
, response
);
518 } else if (result
== DONE
) {
519 strncpy(response
, buffer
, sizeof(response
));
525 netsec_set_snoop(nsc
, snoopstate
);
533 return command ("DELE %d", msgno
);
542 i
= command ("QUIT");
553 netsec_shutdown(nsc
);
560 command(const char *fmt
, ...)
566 result
= vcommand(fmt
, ap
);
574 vcommand (const char *fmt
, va_list ap
)
579 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
580 strncpy(response
, errstr
, sizeof(response
));
581 response
[sizeof(response
) - 1] = '\0';
586 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
587 strncpy(response
, errstr
, sizeof(response
));
588 response
[sizeof(response
) - 1] = '\0';
593 if (netsec_flush(nsc
, &errstr
) != OK
) {
594 strncpy(response
, errstr
, sizeof(response
));
595 response
[sizeof(response
) - 1] = '\0';
600 switch (pop_getline (response
, sizeof response
, nsc
)) {
603 fprintf (stderr
, "<--- %s\n", response
);
604 return *response
== '+' ? OK
: NOTOK
;
609 fputs(response
, stderr
);
615 return NOTOK
; /* NOTREACHED */
622 char buffer
[BUFSIZ
+ LEN(TRM
)];
624 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
626 if (has_prefix(buffer
, TRM
)) {
627 if (buffer
[LEN(TRM
)] == 0)
629 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
632 strncpy (response
, buffer
, sizeof(response
));
638 * This is now just a thin wrapper around netsec_readline().
642 pop_getline (char *s
, int n
, netsec_context
*ns
)
650 p
= netsec_readline(ns
, &len
, &errstr
);
653 strncpy(response
, errstr
, sizeof(response
));
654 response
[sizeof(response
) - 1] = '\0';
660 * If we had an error, it should have been returned already. Since
661 * netsec_readline() strips off the CR-LF ending, just copy the existing
662 * buffer into response now.
664 * We get a length back from netsec_readline, but the rest of the POP
665 * code doesn't handle it; the assumptions are that everything from
666 * the network can be represented as C strings. That should get fixed
670 destlen
= min(len
, (size_t)(n
- 1));
672 memcpy(s
, p
, destlen
);