]>
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>
18 #define TRMLEN (sizeof TRM - 1)
20 static int poprint
= 0;
22 char response
[BUFSIZ
];
23 static netsec_context
*nsc
= NULL
;
29 static int command(const char *, ...);
30 static int multiline(void);
32 static int traverse (int (*)(char *), const char *, ...);
33 static int vcommand(const char *, va_list);
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 if (oauth_svc
!= NULL
) {
149 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
150 snprintf(response
, sizeof(response
), "OAuth2 not supported");
155 if (proxy
&& *proxy
) {
157 int inpipe
[2]; /* for reading from the server */
158 int outpipe
[2]; /* for sending to the server */
160 if (pipe(inpipe
) < 0) {
161 adios ("inpipe", "pipe");
163 if (pipe(outpipe
) < 0) {
164 adios ("outpipe", "pipe");
174 dup2(outpipe
[0],0); /* connect read end of connection */
175 dup2(inpipe
[1], 1); /* connect write end of connection */
176 if(inpipe
[0]>1) close(inpipe
[0]);
177 if(inpipe
[1]>1) close(inpipe
[1]);
178 if(outpipe
[0]>1) close(outpipe
[0]);
179 if(outpipe
[1]>1) close(outpipe
[1]);
181 /* run the proxy command */
182 argv
=parse_proxy(proxy
, host
);
183 execvp(argv
[0],argv
);
193 /* okay in the parent we do some stuff */
194 close(inpipe
[1]); /* child uses this */
195 close(outpipe
[0]); /* child uses this */
199 /* and write on fd2 */
203 if ((fd1
= client (host
, port
? port
: "pop3", response
,
204 sizeof(response
), snoop
)) == NOTOK
) {
210 SIGNAL (SIGPIPE
, SIG_IGN
);
212 netsec_set_fd(nsc
, fd1
, fd2
);
213 netsec_set_snoop(nsc
, snoop
);
216 if (netsec_set_tls(nsc
, 1, &errstr
) != OK
) {
217 snprintf(response
, sizeof(response
), "%s", errstr
);
222 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
) {
223 snprintf(response
, sizeof(response
), "%s", errstr
);
230 if (netsec_set_sasl_params(nsc
, host
, "pop", mech
,
231 pop_sasl_callback
, &errstr
) != OK
) {
232 snprintf(response
, sizeof(response
), "%s", errstr
);
238 switch (pop_getline (response
, sizeof response
, nsc
)) {
241 fprintf (stderr
, "<--- %s\n", response
);
242 if (*response
== '+') {
246 char server_mechs
[256];
247 if (check_mech(server_mechs
, sizeof(server_mechs
)) != OK
)
249 if (netsec_negotiate_sasl(nsc
, server_mechs
,
251 strncpy(response
, errstr
, sizeof(response
));
252 response
[sizeof(response
) - 1] = '\0';
259 if (!(creds
= nmh_get_credentials(host
, user
)))
261 if (command ("USER %s", nmh_cred_get_user(creds
))
263 if (command("PASS %s", nmh_cred_get_password(creds
))
265 nmh_credentials_free(creds
);
269 nmh_credentials_free(creds
);
271 strncpy (buffer
, response
, sizeof(buffer
));
273 strncpy (response
, buffer
, sizeof(response
));
279 fprintf (stderr
, "%s\n", response
);
280 netsec_shutdown(nsc
, 1);
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
, 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 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
345 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
346 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
350 if (netsec_flush(nsc
, errstr
) != OK
)
353 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
355 snoopoffset
= 6 + strlen(mech
);
356 rc
= netsec_printf(nsc
, errstr
, "AUTH %s %s\r\n", mech
,
359 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
362 if (netsec_flush(nsc
, errstr
) != OK
)
366 if (netsec_printf(nsc
, errstr
, "AUTH %s\r\n", mech
) != OK
)
368 if (netsec_flush(nsc
, errstr
) != OK
)
375 * We should get one line back, with our base64 data. Decode that
376 * and feed it back into the SASL library.
378 case NETSEC_SASL_READ
:
379 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
381 line
= netsec_readline(nsc
, &len
, errstr
);
382 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
386 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
387 netsec_err(errstr
, "Invalid format for SASL response");
395 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
398 netsec_err(errstr
, "Unable to decode base64 response");
405 * Our encoding is pretty simple, so this is easy.
408 case NETSEC_SASL_WRITE
:
409 if (indatalen
== 0) {
410 rc
= netsec_printf(nsc
, errstr
, "\r\n");
412 unsigned char *b64data
;
413 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
414 writeBase64raw(indata
, indatalen
, b64data
);
415 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
416 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
417 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
424 if (netsec_flush(nsc
, errstr
) != OK
)
429 * Finish the protocol; we're looking for an +OK
432 case NETSEC_SASL_FINISH
:
433 line
= netsec_readline(nsc
, &len
, errstr
);
437 if (!HasPrefix(line
, "+OK")) {
438 netsec_err(errstr
, "Authentication failed: %s", line
);
444 * Cancel the SASL exchange in the middle of the commands; for
445 * POP, that's a single "*".
447 * It's unclear to me if I should be returning errors up; I finally
448 * decided the answer should be "yes", and if the upper layer wants to
449 * ignore them that's their choice.
452 case NETSEC_SASL_CANCEL
:
453 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
455 rc
= netsec_flush(nsc
, errstr
);
465 * Find out number of messages available
466 * and their total size.
470 pop_stat (int *nmsgs
, int *nbytes
)
473 if (command ("STAT") == NOTOK
)
476 *nmsgs
= *nbytes
= 0;
477 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
484 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
490 if (command ("LIST %d", msgno
) == NOTOK
)
495 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
498 sscanf (response
, "+OK %d %d", msgs
, bytes
);
502 if (command ("LIST") == NOTOK
)
505 for (i
= 0; i
< *nmsgs
; i
++)
506 switch (multiline ()) {
516 sscanf (response
, "%d %d %d",
517 msgs
++, bytes
++, ids
++);
520 sscanf (response
, "%d %d", msgs
++, bytes
++);
524 switch (multiline ()) {
536 pop_retr (int msgno
, int (*action
)(char *))
538 return traverse (action
, "RETR %d", msgno
);
543 traverse (int (*action
)(char *), const char *fmt
, ...)
545 int result
, snoopstate
;
547 char buffer
[sizeof(response
)];
550 result
= vcommand (fmt
, ap
);
555 strncpy (buffer
, response
, sizeof(buffer
));
557 if ((snoopstate
= netsec_get_snoop(nsc
)))
558 netsec_set_snoop(nsc
, 0);
561 switch (multiline ()) {
563 netsec_set_snoop(nsc
, snoopstate
);
567 strncpy (response
, buffer
, sizeof(response
));
568 netsec_set_snoop(nsc
, snoopstate
);
572 (*action
) (response
);
581 return command ("DELE %d", msgno
);
588 return command ("NOOP");
595 return command ("RSET");
600 pop_top (int msgno
, int lines
, int (*action
)(char *))
602 return traverse (action
, "TOP %d %d", msgno
, lines
);
611 i
= command ("QUIT");
622 netsec_shutdown(nsc
, 1);
629 command(const char *fmt
, ...)
635 result
= vcommand(fmt
, ap
);
643 vcommand (const char *fmt
, va_list ap
)
648 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
649 strncpy(response
, errstr
, sizeof(response
));
650 response
[sizeof(response
) - 1] = '\0';
655 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
656 strncpy(response
, errstr
, sizeof(response
));
657 response
[sizeof(response
) - 1] = '\0';
662 if (netsec_flush(nsc
, &errstr
) != OK
) {
663 strncpy(response
, errstr
, sizeof(response
));
664 response
[sizeof(response
) - 1] = '\0';
669 switch (pop_getline (response
, sizeof response
, nsc
)) {
672 fprintf (stderr
, "<--- %s\n", response
);
673 return (*response
== '+' ? OK
: NOTOK
);
678 fprintf (stderr
, "%s\n", response
);
682 return NOTOK
; /* NOTREACHED */
689 char buffer
[BUFSIZ
+ TRMLEN
];
691 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
693 if (HasPrefix(buffer
, TRM
)) {
694 if (buffer
[TRMLEN
] == 0)
696 strncpy (response
, buffer
+ TRMLEN
, sizeof(response
));
699 strncpy (response
, buffer
, sizeof(response
));
705 * This is now just a thin wrapper around netsec_readline().
709 pop_getline (char *s
, int n
, netsec_context
*ns
)
717 p
= netsec_readline(ns
, &len
, &errstr
);
720 strncpy(response
, errstr
, sizeof(response
));
721 response
[sizeof(response
) - 1] = '\0';
727 * If we had an error, it should have been returned already. Since
728 * netsec_readline() strips off the CR-LF ending, just copy the existing
729 * buffer into response now.
731 * We get a length back from netsec_readline, but the rest of the POP
732 * code doesn't handle it; the assumptions are that everything from
733 * the network can be respresented as C strings. That should get fixed
737 destlen
= len
< ((size_t) (n
- 1)) ? len
: (size_t) (n
- 1);
739 memcpy(s
, p
, destlen
);