]>
diplodocus.org Git - nmh/blob - uip/popsbr.c
2 * popsbr.c -- POP client subroutines
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
15 #include <h/signals.h>
19 static int poprint
= 0;
21 char response
[BUFSIZ
];
22 static netsec_context
*nsc
= NULL
;
28 static int command(const char *, ...);
29 static int multiline(void);
31 static int traverse (int (*)(char *), const char *, ...);
32 static int vcommand(const char *, va_list);
33 static int pop_getline (char *, int, netsec_context
*);
34 static int pop_sasl_callback(enum sasl_message_type
, unsigned const char *,
35 unsigned int, unsigned char **, unsigned int *,
39 check_mech(char *server_mechs
, size_t server_mechs_size
)
41 int status
, sasl_capability
= 0;
44 * First off, we're going to send the CAPA command to see if we can
45 * even support the AUTH command, and if we do, then we'll get a
46 * list of mechanisms the server supports. If we don't support
47 * the CAPA command, then it's unlikely that we will support
51 if (command("CAPA") == NOTOK
) {
52 snprintf(response
, sizeof(response
),
53 "The POP CAPA command failed; POP server does not "
58 while ((status
= multiline()) != DONE
)
63 case DONE
: /* Shouldn't be possible, but just in case */
66 if (strncasecmp(response
, "SASL ", 5) == 0) {
68 * We've seen the SASL capability. Grab the mech list
71 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
76 if (!sasl_capability
) {
77 snprintf(response
, sizeof(response
), "POP server does not support "
86 * Split string containing proxy command into an array of arguments
87 * suitable for passing to exec. Returned array must be freed. Shouldn't
88 * be possible to call this with host set to NULL.
91 parse_proxy(char *proxy
, char *host
)
95 int hlen
= strlen(host
);
97 unsigned char *cur
, *pro
;
100 /* skip any initial space */
101 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
104 /* calculate required size for argument array */
105 for (cur
= pro
; *cur
; cur
++) {
106 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
108 else if (*cur
== '%' && cur
[1] == 'h') {
111 } else if (!isspace(*cur
))
115 /* put together list of arguments */
116 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
117 c
= *pargv
= mh_xmalloc(plen
* sizeof(char));
118 for (cur
= pro
; *cur
; cur
++) {
119 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
122 } else if (*cur
== '%' && cur
[1] == 'h') {
126 } else if (!isspace(*cur
))
135 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
136 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
145 netsec_set_userid(nsc
, user
);
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
);
215 if (netsec_set_tls(nsc
, 1, &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
, host
, "pop", mech
,
230 pop_sasl_callback
, &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 fprintf (stderr
, "%s\n", response
);
279 netsec_shutdown(nsc
, 1);
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
, char **errstr
)
304 case NETSEC_SASL_START
:
306 * Generate our AUTH message, but there is a wrinkle.
308 * Technically, according to RFC 5034, if your command INCLUDING
309 * an initial response exceeds 255 octets (including CRLF), you
310 * can't issue this all in one go, but have to just issue the
311 * AUTH command, wait for a blank initial response, and then
315 mech
= netsec_get_sasl_mechanism(nsc
);
319 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
320 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
321 b64len
= strlen(b64data
);
323 /* Formula here is AUTH + SP + mech + SP + out + CR + LF */
324 len
= b64len
+ 8 + strlen(mech
);
326 rc
= netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
);
329 if (netsec_flush(nsc
, errstr
) != OK
)
331 line
= netsec_readline(nsc
, &len
, errstr
);
335 * If the protocol is being followed correctly, should just
336 * be a "+ ", nothing else.
338 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
339 netsec_err(errstr
, "Did not get expected blank response "
340 "for initial challenge response");
343 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
344 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
345 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
349 if (netsec_flush(nsc
, errstr
) != OK
)
352 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
354 snoopoffset
= 6 + strlen(mech
);
355 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
358 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
361 if (netsec_flush(nsc
, errstr
) != OK
)
365 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
367 if (netsec_flush(nsc
, errstr
) != OK
)
374 * We should get one line back, with our base64 data. Decode that
375 * and feed it back into the SASL library.
377 case NETSEC_SASL_READ
:
378 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
380 line
= netsec_readline(nsc
, &len
, errstr
);
381 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
385 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
386 netsec_err(errstr
, "Invalid format for SASL response");
394 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
397 netsec_err(errstr
, "Unable to decode base64 response");
404 * Our encoding is pretty simple, so this is easy.
407 case NETSEC_SASL_WRITE
:
408 if (indatalen
== 0) {
409 rc
= netsec_printf(nsc
, errstr
, "\r\n");
411 unsigned char *b64data
;
412 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
413 writeBase64raw(indata
, indatalen
, b64data
);
414 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
415 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
416 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
423 if (netsec_flush(nsc
, errstr
) != OK
)
428 * Finish the protocol; we're looking for an +OK
431 case NETSEC_SASL_FINISH
:
432 line
= netsec_readline(nsc
, &len
, errstr
);
436 if (!HasPrefix(line
, "+OK")) {
437 netsec_err(errstr
, "Authentication failed: %s", line
);
443 * Cancel the SASL exchange in the middle of the commands; for
444 * POP, that's a single "*".
446 * It's unclear to me if I should be returning errors up; I finally
447 * decided the answer should be "yes", and if the upper layer wants to
448 * ignore them that's their choice.
451 case NETSEC_SASL_CANCEL
:
452 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
454 rc
= netsec_flush(nsc
, errstr
);
464 * Find out number of messages available
465 * and their total size.
469 pop_stat (int *nmsgs
, int *nbytes
)
472 if (command ("STAT") == NOTOK
)
475 *nmsgs
= *nbytes
= 0;
476 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
483 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
489 if (command ("LIST %d", msgno
) == NOTOK
)
494 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
497 sscanf (response
, "+OK %d %d", msgs
, bytes
);
501 if (command ("LIST") == NOTOK
)
504 for (i
= 0; i
< *nmsgs
; i
++)
505 switch (multiline ()) {
515 sscanf (response
, "%d %d %d",
516 msgs
++, bytes
++, ids
++);
519 sscanf (response
, "%d %d", msgs
++, bytes
++);
523 switch (multiline ()) {
535 pop_retr (int msgno
, int (*action
)(char *))
537 return traverse (action
, "RETR %d", msgno
);
542 traverse (int (*action
)(char *), const char *fmt
, ...)
544 int result
, snoopstate
;
546 char buffer
[sizeof(response
)];
549 result
= vcommand (fmt
, ap
);
554 strncpy (buffer
, response
, sizeof(buffer
));
556 if ((snoopstate
= netsec_get_snoop(nsc
)))
557 netsec_set_snoop(nsc
, 0);
560 switch (multiline ()) {
562 netsec_set_snoop(nsc
, snoopstate
);
566 strncpy (response
, buffer
, sizeof(response
));
567 netsec_set_snoop(nsc
, snoopstate
);
571 (*action
) (response
);
580 return command ("DELE %d", msgno
);
587 return command ("NOOP");
594 return command ("RSET");
599 pop_top (int msgno
, int lines
, int (*action
)(char *))
601 return traverse (action
, "TOP %d %d", msgno
, lines
);
610 i
= command ("QUIT");
621 netsec_shutdown(nsc
, 1);
628 command(const char *fmt
, ...)
634 result
= vcommand(fmt
, ap
);
642 vcommand (const char *fmt
, va_list ap
)
647 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
648 strncpy(response
, errstr
, sizeof(response
));
649 response
[sizeof(response
) - 1] = '\0';
654 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
655 strncpy(response
, errstr
, sizeof(response
));
656 response
[sizeof(response
) - 1] = '\0';
661 if (netsec_flush(nsc
, &errstr
) != OK
) {
662 strncpy(response
, errstr
, sizeof(response
));
663 response
[sizeof(response
) - 1] = '\0';
668 switch (pop_getline (response
, sizeof response
, nsc
)) {
671 fprintf (stderr
, "<--- %s\n", response
);
672 return (*response
== '+' ? OK
: NOTOK
);
677 fprintf (stderr
, "%s\n", response
);
681 return NOTOK
; /* NOTREACHED */
688 char buffer
[BUFSIZ
+ LEN(TRM
)];
690 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
692 if (HasPrefix(buffer
, TRM
)) {
693 if (buffer
[LEN(TRM
)] == 0)
695 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
698 strncpy (response
, buffer
, sizeof(response
));
704 * This is now just a thin wrapper around netsec_readline().
708 pop_getline (char *s
, int n
, netsec_context
*ns
)
716 p
= netsec_readline(ns
, &len
, &errstr
);
719 strncpy(response
, errstr
, sizeof(response
));
720 response
[sizeof(response
) - 1] = '\0';
726 * If we had an error, it should have been returned already. Since
727 * netsec_readline() strips off the CR-LF ending, just copy the existing
728 * buffer into response now.
730 * We get a length back from netsec_readline, but the rest of the POP
731 * code doesn't handle it; the assumptions are that everything from
732 * the network can be respresented as C strings. That should get fixed
736 destlen
= len
< ((size_t) (n
- 1)) ? len
: (size_t) (n
- 1);
738 memcpy(s
, p
, destlen
);