2 * popsbr.c -- POP client subroutines
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
13 extern int client(char *args
, char *protocol
, char *service
, int rproto
,
14 char *response
, int len_response
);
16 #if defined(NNTP) && !defined(PSHSBR)
20 #ifdef NNTP /* building pshsbr.o from popsbr.c */
24 #if !defined(NNTP) && defined(APOP)
30 # include <saslutil.h>
31 #endif /* CYRUS_SASL */
34 #include <h/signals.h>
39 #define TRMLEN (sizeof TRM - 1)
41 static int poprint
= 0;
42 static int pophack
= 0;
44 char response
[BUFSIZ
];
51 #if !defined(NNTP) && defined(MPOP)
52 # define command pop_command
53 # define multiline pop_multiline
57 # ifdef BPOP /* stupid */
58 static int xtnd_last
= -1;
59 static int xtnd_first
= 0;
60 static char xtnd_name
[512]; /* INCREDIBLE HACK!! */
65 static sasl_conn_t
*conn
; /* SASL connection state */
66 static int sasl_complete
= 0; /* Has sasl authentication succeeded? */
67 static int maxoutbuf
; /* Maximum output buffer size */
68 static sasl_ssf_t sasl_ssf
= 0; /* Security strength factor */
69 static int sasl_get_user(void *, int, const char **, unsigned *);
70 static int sasl_get_pass(sasl_conn_t
*, void *, int, sasl_secret_t
**);
76 static sasl_callback_t callbacks
[] = {
77 { SASL_CB_USER
, sasl_get_user
, NULL
},
78 #define POP_SASL_CB_N_USER 0
79 { SASL_CB_PASS
, sasl_get_pass
, NULL
},
80 #define POP_SASL_CB_N_PASS 1
81 { SASL_CB_LOG
, NULL
, NULL
},
82 { SASL_CB_LIST_END
, NULL
, NULL
},
84 #else /* CYRUS_SASL */
85 # define sasl_fgetc fgetc
86 #endif /* CYRUS_SASL */
91 #if !defined(NNTP) && defined(APOP)
92 static char *pop_auth (char *, char *);
95 #if defined(NNTP) || !defined(MPOP)
96 /* otherwise they are not static functions */
97 static int command(const char *, ...);
98 static int multiline(void);
102 static int pop_auth_sasl(char *, char *, char *);
103 static int sasl_fgetc(FILE *);
104 #endif /* CYRUS_SASL */
106 static int traverse (int (*)(), const char *, ...);
107 static int vcommand(const char *, va_list);
108 static int getline (char *, int, FILE *);
109 static int putline (char *, FILE *);
112 #if !defined(NNTP) && defined(APOP)
114 pop_auth (char *user
, char *pass
)
118 unsigned char *dp
, *ep
, digest
[16];
120 static char buffer
[BUFSIZ
];
122 if ((cp
= strchr (response
, '<')) == NULL
123 || (lp
= strchr (cp
, '>')) == NULL
) {
124 snprintf (buffer
, sizeof(buffer
), "APOP not available: %s", response
);
125 strncpy (response
, buffer
, sizeof(response
));
130 snprintf (buffer
, sizeof(buffer
), "%s%s", cp
, pass
);
132 MD5Init (&mdContext
);
133 MD5Update (&mdContext
, (unsigned char *) buffer
,
134 (unsigned int) strlen (buffer
));
135 MD5Final (digest
, &mdContext
);
138 buflen
= sizeof(buffer
);
140 snprintf (cp
, buflen
, "%s ", user
);
145 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]); dp
< ep
; ) {
146 snprintf (cp
, buflen
, "%02x", *dp
++ & 0xff);
154 #endif /* !NNTP && APOP */
158 * This function implements the AUTH command for various SASL mechanisms
160 * We do the whole SASL dialog here. If this completes, then we've
161 * authenticated successfully and have (possibly) negotiated a security
166 pop_auth_sasl(char *user
, char *host
, char *mech
)
168 int result
, status
, sasl_capability
= 0;
169 unsigned int buflen
, outlen
;
170 char server_mechs
[256], *buf
, outbuf
[BUFSIZ
];
171 const char *chosen_mech
;
172 sasl_security_properties_t secprops
;
173 struct pass_context p_context
;
178 * First off, we're going to send the CAPA command to see if we can
179 * even support the AUTH command, and if we do, then we'll get a
180 * list of mechanisms the server supports. If we don't support
181 * the CAPA command, then it's unlikely that we will support
185 if (command("CAPA") == NOTOK
) {
186 snprintf(response
, sizeof(response
),
187 "The POP CAPA command failed; POP server does not "
192 while ((status
= multiline()) != DONE
)
197 case DONE
: /* Shouldn't be possible, but just in case */
200 if (strncasecmp(response
, "SASL ", 5) == 0) {
202 * We've seen the SASL capability. Grab the mech list
205 strncpy(server_mechs
, response
+ 5, sizeof(server_mechs
));
210 if (!sasl_capability
) {
211 snprintf(response
, sizeof(response
), "POP server does not support "
217 * If we received a preferred mechanism, see if the server supports it.
220 if (mech
&& stringdex(mech
, server_mechs
) == -1) {
221 snprintf(response
, sizeof(response
), "Requested SASL mech \"%s\" is "
222 "not in list of supported mechanisms:\n%s",
228 * Start the SASL process. First off, initialize the SASL library.
231 callbacks
[POP_SASL_CB_N_USER
].context
= user
;
232 p_context
.user
= user
;
233 p_context
.host
= host
;
234 callbacks
[POP_SASL_CB_N_PASS
].context
= &p_context
;
236 result
= sasl_client_init(callbacks
);
238 if (result
!= SASL_OK
) {
239 snprintf(response
, sizeof(response
), "SASL library initialization "
240 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
244 result
= sasl_client_new("pop", host
, NULL
, NULL
, NULL
, 0, &conn
);
246 if (result
!= SASL_OK
) {
247 snprintf(response
, sizeof(response
), "SASL client initialization "
248 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
253 * Initialize the security properties
256 memset(&secprops
, 0, sizeof(secprops
));
257 secprops
.maxbufsize
= BUFSIZ
;
258 secprops
.max_ssf
= UINT_MAX
;
260 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
262 if (result
!= SASL_OK
) {
263 snprintf(response
, sizeof(response
), "SASL security property "
264 "initialization failed: %s", sasl_errdetail(conn
));
269 * Start the actual protocol. Feed the mech list into the library
270 * and get out a possible initial challenge
273 result
= sasl_client_start(conn
,
274 (const char *) (mech
? mech
: server_mechs
),
275 NULL
, (const char **) &buf
,
276 &buflen
, &chosen_mech
);
278 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
279 snprintf(response
, sizeof(response
), "SASL client start failed: %s",
280 sasl_errdetail(conn
));
285 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
286 if (status
!= SASL_OK
) {
287 snprintf(response
, sizeof(response
), "SASL base64 encode "
288 "failed: %s", sasl_errstring(status
, NULL
, NULL
));
292 status
= command("AUTH %s %s", chosen_mech
, outbuf
);
294 status
= command("AUTH %s", chosen_mech
);
296 while (result
== SASL_CONTINUE
) {
301 * If we get a "+OK" prefix to our response, then we should
302 * exit out of this exchange now (because authenticated should
306 if (strncmp(response
, "+OK", 3) == 0)
310 * Otherwise, make sure the server challenge is correctly formatted
313 if (strncmp(response
, "+ ", 2) != 0) {
315 snprintf(response
, sizeof(response
),
316 "Malformed authentication message from server");
320 result
= sasl_decode64(response
+ 2, strlen(response
+ 2),
321 outbuf
, sizeof(outbuf
), &outlen
);
323 if (result
!= SASL_OK
) {
325 snprintf(response
, sizeof(response
), "SASL base64 decode "
326 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
330 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
,
331 (const char **) &buf
, &buflen
);
333 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
335 snprintf(response
, sizeof(response
), "SASL client negotiaton "
336 "failed: %s", sasl_errdetail(conn
));
340 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
342 if (status
!= SASL_OK
) {
344 snprintf(response
, sizeof(response
), "SASL base64 encode "
345 "failed: %s", sasl_errstring(status
, NULL
, NULL
));
349 status
= command(outbuf
);
353 * If we didn't get a positive final response, then error out
354 * (that probably means we failed an authorization check).
361 * We _should_ be okay now. Get a few properties now that negotiation
365 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (const void **) &moutbuf
);
367 if (result
!= SASL_OK
) {
368 snprintf(response
, sizeof(response
), "Cannot retrieve SASL negotiated "
369 "output buffer size: %s", sasl_errdetail(conn
));
373 maxoutbuf
= *moutbuf
;
375 result
= sasl_getprop(conn
, SASL_SSF
, (const void **) &ssf
);
379 if (result
!= SASL_OK
) {
380 snprintf(response
, sizeof(response
), "Cannot retrieve SASL negotiated "
381 "security strength factor: %s", sasl_errdetail(conn
));
386 * Limit this to what we can deal with.
389 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
398 * Callback to return the userid sent down via the user parameter
402 sasl_get_user(void *context
, int id
, const char **result
, unsigned *len
)
404 char *user
= (char *) context
;
406 if (! result
|| id
!= SASL_CB_USER
)
407 return SASL_BADPARAM
;
417 * Callback to return the password (we call ruserpass, which can get it
422 sasl_get_pass(sasl_conn_t
*conn
, void *context
, int id
, sasl_secret_t
**psecret
)
424 struct pass_context
*p_context
= (struct pass_context
*) context
;
428 if (! psecret
|| id
!= SASL_CB_PASS
)
429 return SASL_BADPARAM
;
431 ruserpass(p_context
->user
, &(p_context
->host
), &pass
);
435 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
440 (*psecret
)->len
= len
;
441 strcpy((char *) (*psecret
)->data
, pass
);
445 #endif /* CYRUS_SASL */
448 pop_init (char *host
, char *user
, char *pass
, int snoop
, int rpop
, int kpop
,
449 int sasl
, char *mech
)
457 if ((apop
= rpop
) < 0)
464 snprintf (buffer
, sizeof(buffer
), "%s/%s", KPOP_PRINCIPAL
, "kpop");
465 if ((fd1
= client (host
, "tcp", buffer
, 0, response
, sizeof(response
))) == NOTOK
) {
470 if ((fd1
= client (host
, "tcp", POPSERVICE
, rpop
, response
, sizeof(response
))) == NOTOK
) {
477 if ((fd1
= client (host
, "tcp", "nntp", rpop
, response
, sizeof(response
))) == NOTOK
)
481 if ((fd2
= dup (fd1
)) == NOTOK
) {
484 if ((s
= strerror(errno
)))
485 snprintf (response
, sizeof(response
),
486 "unable to dup connection descriptor: %s", s
);
488 snprintf (response
, sizeof(response
),
489 "unable to dup connection descriptor: unknown error");
494 if (pop_set (fd1
, fd2
, snoop
) == NOTOK
)
496 if (pop_set (fd1
, fd2
, snoop
, (char *)0) == NOTOK
)
500 SIGNAL (SIGPIPE
, SIG_IGN
);
502 switch (getline (response
, sizeof response
, input
)) {
505 fprintf (stderr
, "<--- %s\n", response
);
507 if (*response
== '+') {
511 char *cp
= pop_auth (user
, pass
);
513 if (cp
&& command ("APOP %s", cp
) != NOTOK
)
520 if (pop_auth_sasl(user
, host
, mech
) != NOTOK
)
523 # endif /* CYRUS_SASL */
524 if (command ("USER %s", user
) != NOTOK
525 && command ("%s %s", rpop
? "RPOP" : (pophack
++, "PASS"),
529 if (command ("USER %s", user
) != NOTOK
530 && command ("PASS %s", pass
) != NOTOK
)
535 if (*response
< CHAR_ERR
) {
536 command ("MODE READER");
540 strncpy (buffer
, response
, sizeof(buffer
));
542 strncpy (response
, buffer
, sizeof(response
));
548 fprintf (stderr
, "%s\n", response
);
554 return NOTOK
; /* NOTREACHED */
559 pop_set (int in
, int out
, int snoop
, char *myname
)
562 pop_set (int in
, int out
, int snoop
)
567 if (myname
&& *myname
) {
568 /* interface from bbc to msh */
569 strncpy (xtnd_name
, myname
, sizeof(xtnd_name
));
573 if ((input
= fdopen (in
, "r")) == NULL
574 || (output
= fdopen (out
, "w")) == NULL
) {
575 strncpy (response
, "fdopen failed on connection descriptor", sizeof(response
));
591 pop_fd (char *in
, int inlen
, char *out
, int outlen
)
593 snprintf (in
, inlen
, "%d", fileno (input
));
594 snprintf (out
, outlen
, "%d", fileno (output
));
600 * Find out number of messages available
601 * and their total size.
605 pop_stat (int *nmsgs
, int *nbytes
)
612 if (command ("STAT") == NOTOK
)
615 *nmsgs
= *nbytes
= 0;
616 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
619 if (xtnd_last
< 0) { /* in msh, xtnd_name is set from myname */
620 if (command("GROUP %s", xtnd_name
) == NOTOK
)
623 ap
= brkstring (response
, " ", "\n"); /* "211 nart first last ggg" */
624 xtnd_first
= atoi (ap
[2]);
625 xtnd_last
= atoi (ap
[3]);
628 /* nmsgs is not the real nart, but an incredible simuation */
630 *nmsgs
= xtnd_last
- xtnd_first
+ 1; /* because of holes... */
633 *nbytes
= xtnd_first
; /* for subtracting offset in msh() */
641 pop_exists (int (*action
)())
643 #ifdef XMSGS /* hacked into NNTP 1.5 */
644 if (traverse (action
, "XMSGS %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
) == OK
)
647 /* provided by INN 1.4 */
648 if (traverse (action
, "LISTGROUP") == OK
)
650 return traverse (action
, "XHDR NONAME %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
);
657 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
, int *ids
)
660 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
670 if (command ("LIST %d", msgno
) == NOTOK
)
675 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
678 sscanf (response
, "+OK %d %d", msgs
, bytes
);
681 if (command ("STAT %d", msgno
) == NOTOK
)
691 if (command ("LIST") == NOTOK
)
694 for (i
= 0; i
< *nmsgs
; i
++)
695 switch (multiline ()) {
705 sscanf (response
, "%d %d %d",
706 msgs
++, bytes
++, ids
++);
709 sscanf (response
, "%d %d", msgs
++, bytes
++);
713 switch (multiline ()) {
728 pop_retr (int msgno
, int (*action
)())
731 return traverse (action
, "RETR %d", (targ_t
) msgno
);
733 return traverse (action
, "ARTICLE %d", (targ_t
) msgno
);
739 traverse (int (*action
)(), const char *fmt
, ...)
743 char buffer
[sizeof(response
)];
746 result
= vcommand (fmt
, ap
);
751 strncpy (buffer
, response
, sizeof(buffer
));
754 switch (multiline ()) {
759 strncpy (response
, buffer
, sizeof(response
));
763 (*action
) (response
);
772 return command ("DELE %d", msgno
);
779 return command ("NOOP");
783 #if defined(MPOP) && !defined(NNTP)
787 return command ("LAST");
795 return command ("RSET");
800 pop_top (int msgno
, int lines
, int (*action
)())
803 return traverse (action
, "TOP %d %d", (targ_t
) msgno
, (targ_t
) lines
);
805 return traverse (action
, "HEAD %d", (targ_t
) msgno
);
812 pop_xtnd (int (*action
)(), char *fmt
, ...)
824 /* needs to be fixed... va_end needs to be added */
825 snprintf (buffer
, sizeof(buffer
), "XTND %s", fmt
);
826 result
= traverse (action
, buffer
, a
, b
, c
, d
);
830 snprintf (buffer
, sizeof(buffer
), fmt
, a
, b
, c
, d
);
831 ap
= brkstring (buffer
, " ", "\n"); /* a hack, i know... */
833 if (!strcasecmp(ap
[0], "x-bboards")) { /* XTND "X-BBOARDS group */
834 /* most of these parameters are meaningless under NNTP.
835 * bbc.c was modified to set AKA and LEADERS as appropriate,
836 * the rest are left blank.
840 if (!strcasecmp (ap
[0], "archive") && ap
[1]) {
841 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
843 xtnd_first
= 1; /* setup to fail in pop_stat */
846 if (!strcasecmp (ap
[0], "bboards")) {
848 if (ap
[1]) { /* XTND "BBOARDS group" */
849 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
850 if (command("GROUP %s", xtnd_name
) == NOTOK
)
853 /* action must ignore extra args */
854 strncpy (buffer
, response
, sizeof(buffer
));
855 ap
= brkstring (response
, " ", "\n");/* "211 nart first last g" */
856 xtnd_first
= atoi (ap
[2]);
857 xtnd_last
= atoi (ap
[3]);
862 } else { /* XTND "BBOARDS" */
863 return traverse (action
, "LIST", a
, b
, c
, d
);
866 return NOTOK
; /* unknown XTND command */
877 i
= command ("QUIT");
890 #endif /* CYRUS_SASL */
898 #if !defined(MPOP) || defined(NNTP)
902 command(const char *fmt
, ...)
908 result
= vcommand(fmt
, ap
);
916 vcommand (const char *fmt
, va_list ap
)
918 char *cp
, buffer
[BUFSIZ
];
920 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
924 fprintf(stderr
, "(encrypted) ");
925 #endif /* CYRUS_SASL */
927 if ((cp
= strchr (buffer
, ' ')))
929 fprintf (stderr
, "---> %s ********\n", buffer
);
935 fprintf (stderr
, "---> %s\n", buffer
);
938 if (putline (buffer
, output
) == NOTOK
)
942 if (poprint
&& sasl_ssf
)
943 fprintf(stderr
, "(decrypted) ");
944 #endif /* CYRUS_SASL */
946 switch (getline (response
, sizeof response
, input
)) {
949 fprintf (stderr
, "<--- %s\n", response
);
951 return (*response
== '+' ? OK
: NOTOK
);
953 return (*response
< CHAR_ERR
? OK
: NOTOK
);
959 fprintf (stderr
, "%s\n", response
);
963 return NOTOK
; /* NOTREACHED */
967 #if defined(MPOP) && !defined(NNTP)
975 char buffer
[BUFSIZ
+ TRMLEN
];
977 if (getline (buffer
, sizeof buffer
, input
) != OK
)
983 fprintf(stderr
, "(decrypted) ");
984 #endif /* CYRUS_SASL */
985 fprintf (stderr
, "<--- %s\n", response
);
988 if (strncmp (buffer
, TRM
, TRMLEN
) == 0) {
989 if (buffer
[TRMLEN
] == 0)
992 strncpy (response
, buffer
+ TRMLEN
, sizeof(response
));
995 strncpy (response
, buffer
, sizeof(response
));
1001 * Note that these functions have been modified to deal with layer encryption
1006 getline (char *s
, int n
, FILE *iop
)
1012 while (--n
> 0 && (c
= sasl_fgetc (iop
)) != EOF
&& c
!= -2)
1013 if ((*p
++ = c
) == '\n')
1017 if (ferror (iop
) && c
!= EOF
) {
1018 strncpy (response
, "error on connection", sizeof(response
));
1021 if (c
== EOF
&& p
== s
) {
1022 strncpy (response
, "connection closed by foreign host", sizeof(response
));
1036 putline (char *s
, FILE *iop
)
1039 char outbuf
[BUFSIZ
], *buf
;
1041 unsigned int buflen
;
1043 if (!sasl_complete
) {
1044 #endif /* CYRUS_SASL */
1045 fprintf (iop
, "%s\r\n", s
);
1049 * Build an output buffer, encrypt it using sasl_encode, and
1050 * squirt out the results.
1052 strncpy(outbuf
, s
, sizeof(outbuf
) - 3);
1053 outbuf
[sizeof(outbuf
) - 3] = '\0'; /* Just in case */
1054 strcat(outbuf
, "\r\n");
1056 result
= sasl_encode(conn
, outbuf
, strlen(outbuf
),
1057 (const char **) &buf
, &buflen
);
1059 if (result
!= SASL_OK
) {
1060 snprintf(response
, sizeof(response
), "SASL encoding error: %s",
1061 sasl_errdetail(conn
));
1065 fwrite(buf
, buflen
, 1, iop
);
1067 #endif /* CYRUS_SASL */
1071 strncpy (response
, "lost connection", sizeof(response
));
1080 * Okay, our little fgetc replacement. Hopefully this is a little more
1081 * efficient than the last one.
1086 static unsigned char *buffer
= NULL
, *ptr
;
1087 static int size
= 0;
1089 unsigned int retbufsize
= 0;
1091 char *retbuf
, tmpbuf
[BUFSIZ
];
1094 * If we have some leftover data, return that
1099 return (int) *ptr
++;
1103 * Otherwise, fill our buffer until we have some data to return.
1106 while (retbufsize
== 0) {
1108 cc
= read(fileno(f
), tmpbuf
, sizeof(tmpbuf
));
1114 snprintf(response
, sizeof(response
), "Error during read from "
1115 "network: %s", strerror(errno
));
1120 * We're not allowed to call sasl_decode until sasl_complete is
1121 * true, so we do these gyrations ...
1124 if (!sasl_complete
) {
1131 result
= sasl_decode(conn
, tmpbuf
, cc
,
1132 (const char **) &retbuf
, &retbufsize
);
1134 if (result
!= SASL_OK
) {
1135 snprintf(response
, sizeof(response
), "Error during SASL "
1136 "decoding: %s", sasl_errdetail(conn
));
1142 if (retbufsize
> size
) {
1143 buffer
= realloc(buffer
, retbufsize
);
1145 snprintf(response
, sizeof(response
), "Error during realloc in "
1146 "read routine: %s", strerror(errno
));
1152 memcpy(buffer
, retbuf
, retbufsize
);
1154 cnt
= retbufsize
- 1;
1156 return (int) buffer
[0];
1158 #endif /* CYRUS_SASL */