]>
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.
14 #include <h/signals.h>
18 static int poprint
= 0;
20 char response
[BUFSIZ
];
21 static netsec_context
*nsc
= NULL
;
27 static int command(const char *, ...);
28 static int multiline(void);
30 static int traverse (int (*)(char *), const char *, ...);
31 static int vcommand(const char *, va_list);
32 static int pop_getline (char *, int, netsec_context
*);
33 static int pop_sasl_callback(enum sasl_message_type
, unsigned const char *,
34 unsigned int, unsigned char **, unsigned int *,
38 check_mech(char *server_mechs
, size_t server_mechs_size
)
40 int status
, sasl_capability
= 0;
43 * First off, we're going to send the CAPA command to see if we can
44 * even support the AUTH command, and if we do, then we'll get a
45 * list of mechanisms the server supports. If we don't support
46 * the CAPA command, then it's unlikely that we will support
50 if (command("CAPA") == NOTOK
) {
51 snprintf(response
, sizeof(response
),
52 "The POP CAPA command failed; POP server does not "
57 while ((status
= multiline()) != DONE
)
62 case DONE
: /* Shouldn't be possible, but just in case */
65 if (strncasecmp(response
, "SASL ", 5) == 0) {
67 * We've seen the SASL capability. Grab the mech list
70 strncpy(server_mechs
, response
+ 5, server_mechs_size
);
75 if (!sasl_capability
) {
76 snprintf(response
, sizeof(response
), "POP server does not support "
85 * Split string containing proxy command into an array of arguments
86 * suitable for passing to exec. Returned array must be freed. Shouldn't
87 * be possible to call this with host set to NULL.
90 parse_proxy(char *proxy
, char *host
)
94 int hlen
= strlen(host
);
96 unsigned char *cur
, *pro
;
99 /* skip any initial space */
100 for (pro
= (unsigned char *) proxy
; isspace(*pro
); pro
++)
103 /* calculate required size for argument array */
104 for (cur
= pro
; *cur
; cur
++) {
105 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
107 else if (*cur
== '%' && cur
[1] == 'h') {
110 } else if (!isspace(*cur
))
114 /* put together list of arguments */
115 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
116 c
= *pargv
= mh_xmalloc(plen
* sizeof(char));
117 for (cur
= pro
; *cur
; cur
++) {
118 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
121 } else if (*cur
== '%' && cur
[1] == 'h') {
125 } else if (!isspace(*cur
))
134 pop_init (char *host
, char *port
, char *user
, char *proxy
, int snoop
,
135 int sasl
, char *mech
, int tls
, const char *oauth_svc
)
144 netsec_set_userid(nsc
, user
);
146 netsec_set_hostname(nsc
, host
);
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
);
215 if (tls
& P_INITTLS
) {
216 if (netsec_set_tls(nsc
, 1, tls
& P_NOVERIFY
, &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
, "pop", mech
, pop_sasl_callback
,
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 (!has_prefix(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_retr (int msgno
, int (*action
)(char *))
486 return traverse (action
, "RETR %d", msgno
);
491 traverse (int (*action
)(char *), const char *fmt
, ...)
493 int result
, snoopstate
;
495 char buffer
[sizeof(response
)];
498 result
= vcommand (fmt
, ap
);
503 strncpy (buffer
, response
, sizeof(buffer
));
505 if ((snoopstate
= netsec_get_snoop(nsc
)))
506 netsec_set_snoop(nsc
, 0);
509 switch (multiline ()) {
511 netsec_set_snoop(nsc
, snoopstate
);
515 strncpy (response
, buffer
, sizeof(response
));
516 netsec_set_snoop(nsc
, snoopstate
);
520 (*action
) (response
);
529 return command ("DELE %d", msgno
);
538 i
= command ("QUIT");
549 netsec_shutdown(nsc
, 1);
556 command(const char *fmt
, ...)
562 result
= vcommand(fmt
, ap
);
570 vcommand (const char *fmt
, va_list ap
)
575 if (netsec_vprintf(nsc
, &errstr
, fmt
, ap
) != OK
) {
576 strncpy(response
, errstr
, sizeof(response
));
577 response
[sizeof(response
) - 1] = '\0';
582 if (netsec_printf(nsc
, &errstr
, "\r\n") != OK
) {
583 strncpy(response
, errstr
, sizeof(response
));
584 response
[sizeof(response
) - 1] = '\0';
589 if (netsec_flush(nsc
, &errstr
) != OK
) {
590 strncpy(response
, errstr
, sizeof(response
));
591 response
[sizeof(response
) - 1] = '\0';
596 switch (pop_getline (response
, sizeof response
, nsc
)) {
599 fprintf (stderr
, "<--- %s\n", response
);
600 return (*response
== '+' ? OK
: NOTOK
);
605 fprintf (stderr
, "%s\n", response
);
609 return NOTOK
; /* NOTREACHED */
616 char buffer
[BUFSIZ
+ LEN(TRM
)];
618 if (pop_getline (buffer
, sizeof buffer
, nsc
) != OK
)
620 if (has_prefix(buffer
, TRM
)) {
621 if (buffer
[LEN(TRM
)] == 0)
623 strncpy (response
, buffer
+ LEN(TRM
), sizeof(response
));
626 strncpy (response
, buffer
, sizeof(response
));
632 * This is now just a thin wrapper around netsec_readline().
636 pop_getline (char *s
, int n
, netsec_context
*ns
)
644 p
= netsec_readline(ns
, &len
, &errstr
);
647 strncpy(response
, errstr
, sizeof(response
));
648 response
[sizeof(response
) - 1] = '\0';
654 * If we had an error, it should have been returned already. Since
655 * netsec_readline() strips off the CR-LF ending, just copy the existing
656 * buffer into response now.
658 * We get a length back from netsec_readline, but the rest of the POP
659 * code doesn't handle it; the assumptions are that everything from
660 * the network can be represented as C strings. That should get fixed
664 destlen
= min(len
, (size_t)(n
- 1));
666 memcpy(s
, p
, destlen
);