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 #endif /* CYRUS_SASL */
33 #include <h/signals.h>
38 #define TRMLEN (sizeof TRM - 1)
40 static int poprint
= 0;
41 static int pophack
= 0;
43 char response
[BUFSIZ
];
50 #if !defined(NNTP) && defined(MPOP)
51 # define command pop_command
52 # define multiline pop_multiline
56 # ifdef BPOP /* stupid */
57 static int xtnd_last
= -1;
58 static int xtnd_first
= 0;
59 static char xtnd_name
[512]; /* INCREDIBLE HACK!! */
64 static sasl_conn_t
*conn
; /* SASL connection state */
65 static int sasl_complete
= 0; /* Has sasl authentication succeeded? */
66 static int maxoutbuf
; /* Maximum output buffer size */
67 static sasl_ssf_t sasl_ssf
= 0; /* Security strength factor */
68 static int sasl_get_user(void *, int, const char **, unsigned *);
69 static int sasl_get_pass(sasl_conn_t
*, void *, int, sasl_secret_t
**);
75 static sasl_callback_t callbacks
[] = {
76 { SASL_CB_USER
, sasl_get_user
, NULL
},
77 #define POP_SASL_CB_N_USER 0
78 { SASL_CB_PASS
, sasl_get_pass
, NULL
},
79 #define POP_SASL_CB_N_PASS 1
80 { SASL_CB_LIST_END
, NULL
, NULL
},
82 #else /* CYRUS_SASL */
83 # define sasl_fgetc fgetc
84 #endif /* CYRUS_SASL */
89 #if !defined(NNTP) && defined(APOP)
90 static char *pop_auth (char *, char *);
93 #if defined(NNTP) || !defined(MPOP)
94 /* otherwise they are not static functions */
95 static int command(const char *, ...);
96 static int multiline(void);
100 static int pop_auth_sasl(char *, char *, char *);
101 static int sasl_fgetc(FILE *);
102 #endif /* CYRUS_SASL */
104 static int traverse (int (*)(), const char *, ...);
105 static int vcommand(const char *, va_list);
106 static int getline (char *, int, FILE *);
107 static int putline (char *, FILE *);
110 #if !defined(NNTP) && defined(APOP)
112 pop_auth (char *user
, char *pass
)
116 unsigned char *dp
, *ep
, digest
[16];
118 static char buffer
[BUFSIZ
];
120 if ((cp
= strchr (response
, '<')) == NULL
121 || (lp
= strchr (cp
, '>')) == NULL
) {
122 snprintf (buffer
, sizeof(buffer
), "APOP not available: %s", response
);
123 strncpy (response
, buffer
, sizeof(response
));
128 snprintf (buffer
, sizeof(buffer
), "%s%s", cp
, pass
);
130 MD5Init (&mdContext
);
131 MD5Update (&mdContext
, (unsigned char *) buffer
,
132 (unsigned int) strlen (buffer
));
133 MD5Final (digest
, &mdContext
);
136 buflen
= sizeof(buffer
);
138 snprintf (cp
, buflen
, "%s ", user
);
143 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]); dp
< ep
; ) {
144 snprintf (cp
, buflen
, "%02x", *dp
++ & 0xff);
152 #endif /* !NNTP && APOP */
156 * This function implements the AUTH command for various SASL mechanisms
158 * We do the whole SASL dialog here. If this completes, then we've
159 * authenticated successfully and have (possibly) negotiated a security
164 pop_auth_sasl(char *user
, char *host
, char *mech
)
166 int result
, status
, sasl_capability
= 0, outlen
;
168 char server_mechs
[256], *buf
, outbuf
[BUFSIZ
];
169 const char *chosen_mech
;
170 sasl_security_properties_t secprops
;
171 sasl_external_properties_t extprops
;
172 struct pass_context p_context
;
177 * First off, we're going to send the CAPA command to see if we can
178 * even support the AUTH command, and if we do, then we'll get a
179 * list of mechanisms the server supports. If we don't support
180 * the CAPA command, then it's unlikely that we will support
184 if (command("CAPA") == NOTOK
) {
185 snprintf(response
, sizeof(response
),
186 "The POP CAPA command failed; POP server does not "
191 while ((status
= multiline()) != DONE
)
196 case DONE
: /* Shouldn't be possible, but just in case */
199 if (strncasecmp(response
, "SASL ", 5) == 0) {
201 * We've seen the SASL capability. Grab the mech list
204 strncpy(server_mechs
, response
+ 5, sizeof(server_mechs
));
209 if (!sasl_capability
) {
210 snprintf(response
, sizeof(response
), "POP server does not support "
216 * If we received a preferred mechanism, see if the server supports it.
219 if (mech
&& stringdex(mech
, server_mechs
) == -1) {
220 snprintf(response
, sizeof(response
), "Requested SASL mech \"%s\" is "
221 "not in list of supported mechanisms:\n%s",
227 * Start the SASL process. First off, initialize the SASL library.
230 callbacks
[POP_SASL_CB_N_USER
].context
= user
;
231 p_context
.user
= user
;
232 p_context
.host
= host
;
233 callbacks
[POP_SASL_CB_N_PASS
].context
= &p_context
;
235 result
= sasl_client_init(callbacks
);
237 if (result
!= SASL_OK
) {
238 snprintf(response
, sizeof(response
), "SASL library initialization "
239 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
243 result
= sasl_client_new("pop", host
, NULL
, SASL_SECURITY_LAYER
, &conn
);
245 if (result
!= SASL_OK
) {
246 snprintf(response
, sizeof(response
), "SASL client initialization "
247 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
252 * Initialize the security properties
255 memset(&secprops
, 0, sizeof(secprops
));
256 secprops
.maxbufsize
= BUFSIZ
;
257 secprops
.max_ssf
= UINT_MAX
;
258 memset(&extprops
, 0, sizeof(extprops
));
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",
265 sasl_errstring(result
, NULL
, NULL
));
269 result
= sasl_setprop(conn
, SASL_SSF_EXTERNAL
, &extprops
);
271 if (result
!= SASL_OK
) {
272 snprintf(response
, sizeof(response
), "SASL external property "
273 "initialization failed: %s",
274 sasl_errstring(result
, NULL
, NULL
));
279 * Start the actual protocol. Feed the mech list into the library
280 * and get out a possible initial challenge
283 result
= sasl_client_start(conn
, mech
? mech
: server_mechs
,
284 NULL
, NULL
, &buf
, &buflen
, &chosen_mech
);
286 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
287 snprintf(response
, sizeof(response
), "SASL client start failed: %s",
288 sasl_errstring(result
, NULL
, NULL
));
293 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
295 if (status
!= SASL_OK
) {
296 snprintf(response
, sizeof(response
), "SASL base64 encode "
297 "failed: %s", sasl_errstring(status
, NULL
, NULL
));
301 status
= command("AUTH %s %s", chosen_mech
, outbuf
);
303 status
= command("AUTH %s", chosen_mech
);
305 while (result
== SASL_CONTINUE
) {
310 * If we get a "+OK" prefix to our response, then we should
311 * exit out of this exchange now (because authenticated should
315 if (strncmp(response
, "+OK", 3) == 0)
319 * Otherwise, make sure the server challenge is correctly formatted
322 if (strncmp(response
, "+ ", 2) != 0) {
324 snprintf(response
, sizeof(response
),
325 "Malformed authentication message from server");
329 result
= sasl_decode64(response
+ 2, strlen(response
+ 2),
332 if (result
!= SASL_OK
) {
334 snprintf(response
, sizeof(response
), "SASL base64 decode "
335 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
339 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
, &buf
, &buflen
);
341 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
343 snprintf(response
, sizeof(response
), "SASL client negotiaton "
344 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
348 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
351 if (status
!= SASL_OK
) {
353 snprintf(response
, sizeof(response
), "SASL base64 encode "
354 "failed: %s", sasl_errstring(status
, NULL
, NULL
));
358 status
= command(outbuf
);
362 * If we didn't get a positive final response, then error out
363 * (that probably means we failed an authorization check).
370 * Depending on the mechanism, we might need to call sasl_client_step()
371 * one more time. Do that now.
374 result
= sasl_client_step(conn
, NULL
, 0, NULL
, &buf
, &buflen
);
376 if (result
!= SASL_OK
) {
377 snprintf(response
, sizeof(response
), "SASL final client negotiaton "
378 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
383 * We _should_ be okay now. Get a few properties now that negotiation
387 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (void **) &moutbuf
);
389 if (result
!= SASL_OK
) {
390 snprintf(response
, sizeof(response
), "Cannot retrieve SASL negotiated "
391 "output buffer size: %s", sasl_errstring(result
, NULL
, NULL
));
395 maxoutbuf
= *moutbuf
;
397 result
= sasl_getprop(conn
, SASL_SSF
, (void **) &ssf
);
401 if (result
!= SASL_OK
) {
402 snprintf(response
, sizeof(response
), "Cannot retrieve SASL negotiated "
403 "security strength factor: %s",
404 sasl_errstring(result
, NULL
, NULL
));
409 * Limit this to what we can deal with.
412 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
421 * Callback to return the userid sent down via the user parameter
425 sasl_get_user(void *context
, int id
, const char **result
, unsigned *len
)
427 char *user
= (char *) context
;
429 if (! result
|| id
!= SASL_CB_USER
)
430 return SASL_BADPARAM
;
440 * Callback to return the password (we call ruserpass, which can get it
445 sasl_get_pass(sasl_conn_t
*conn
, void *context
, int id
, sasl_secret_t
**psecret
)
447 struct pass_context
*p_context
= (struct pass_context
*) context
;
451 if (! psecret
|| id
!= SASL_CB_PASS
)
452 return SASL_BADPARAM
;
454 ruserpass(p_context
->user
, &(p_context
->host
), &pass
);
458 *psecret
= (sasl_secret_t
*) malloc(sizeof(sasl_secret_t
) + len
);
463 (*psecret
)->len
= len
;
464 strcpy((*psecret
)->data
, pass
);
468 #endif /* CYRUS_SASL */
471 pop_init (char *host
, char *user
, char *pass
, int snoop
, int rpop
, int kpop
,
472 int sasl
, char *mech
)
480 if ((apop
= rpop
) < 0)
487 snprintf (buffer
, sizeof(buffer
), "%s/%s", KPOP_PRINCIPAL
, "kpop");
488 if ((fd1
= client (host
, "tcp", buffer
, 0, response
, sizeof(response
))) == NOTOK
) {
493 if ((fd1
= client (host
, "tcp", POPSERVICE
, rpop
, response
, sizeof(response
))) == NOTOK
) {
500 if ((fd1
= client (host
, "tcp", "nntp", rpop
, response
, sizeof(response
))) == NOTOK
)
504 if ((fd2
= dup (fd1
)) == NOTOK
) {
507 if ((s
= strerror(errno
)))
508 snprintf (response
, sizeof(response
),
509 "unable to dup connection descriptor: %s", s
);
511 snprintf (response
, sizeof(response
),
512 "unable to dup connection descriptor: unknown error");
517 if (pop_set (fd1
, fd2
, snoop
) == NOTOK
)
519 if (pop_set (fd1
, fd2
, snoop
, (char *)0) == NOTOK
)
523 SIGNAL (SIGPIPE
, SIG_IGN
);
525 switch (getline (response
, sizeof response
, input
)) {
528 fprintf (stderr
, "<--- %s\n", response
);
530 if (*response
== '+') {
534 char *cp
= pop_auth (user
, pass
);
536 if (cp
&& command ("APOP %s", cp
) != NOTOK
)
543 if (pop_auth_sasl(user
, host
, mech
) != NOTOK
)
546 # endif /* CYRUS_SASL */
547 if (command ("USER %s", user
) != NOTOK
548 && command ("%s %s", rpop
? "RPOP" : (pophack
++, "PASS"),
552 if (command ("USER %s", user
) != NOTOK
553 && command ("PASS %s", pass
) != NOTOK
)
558 if (*response
< CHAR_ERR
) {
559 command ("MODE READER");
563 strncpy (buffer
, response
, sizeof(buffer
));
565 strncpy (response
, buffer
, sizeof(response
));
571 fprintf (stderr
, "%s\n", response
);
577 return NOTOK
; /* NOTREACHED */
582 pop_set (int in
, int out
, int snoop
, char *myname
)
585 pop_set (int in
, int out
, int snoop
)
590 if (myname
&& *myname
) {
591 /* interface from bbc to msh */
592 strncpy (xtnd_name
, myname
, sizeof(xtnd_name
));
596 if ((input
= fdopen (in
, "r")) == NULL
597 || (output
= fdopen (out
, "w")) == NULL
) {
598 strncpy (response
, "fdopen failed on connection descriptor", sizeof(response
));
614 pop_fd (char *in
, int inlen
, char *out
, int outlen
)
616 snprintf (in
, inlen
, "%d", fileno (input
));
617 snprintf (out
, outlen
, "%d", fileno (output
));
623 * Find out number of messages available
624 * and their total size.
628 pop_stat (int *nmsgs
, int *nbytes
)
635 if (command ("STAT") == NOTOK
)
638 *nmsgs
= *nbytes
= 0;
639 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
642 if (xtnd_last
< 0) { /* in msh, xtnd_name is set from myname */
643 if (command("GROUP %s", xtnd_name
) == NOTOK
)
646 ap
= brkstring (response
, " ", "\n"); /* "211 nart first last ggg" */
647 xtnd_first
= atoi (ap
[2]);
648 xtnd_last
= atoi (ap
[3]);
651 /* nmsgs is not the real nart, but an incredible simuation */
653 *nmsgs
= xtnd_last
- xtnd_first
+ 1; /* because of holes... */
656 *nbytes
= xtnd_first
; /* for subtracting offset in msh() */
664 pop_exists (int (*action
)())
666 #ifdef XMSGS /* hacked into NNTP 1.5 */
667 if (traverse (action
, "XMSGS %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
) == OK
)
670 /* provided by INN 1.4 */
671 if (traverse (action
, "LISTGROUP") == OK
)
673 return traverse (action
, "XHDR NONAME %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
);
680 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
, int *ids
)
683 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
693 if (command ("LIST %d", msgno
) == NOTOK
)
698 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
701 sscanf (response
, "+OK %d %d", msgs
, bytes
);
704 if (command ("STAT %d", msgno
) == NOTOK
)
714 if (command ("LIST") == NOTOK
)
717 for (i
= 0; i
< *nmsgs
; i
++)
718 switch (multiline ()) {
728 sscanf (response
, "%d %d %d",
729 msgs
++, bytes
++, ids
++);
732 sscanf (response
, "%d %d", msgs
++, bytes
++);
736 switch (multiline ()) {
751 pop_retr (int msgno
, int (*action
)())
754 return traverse (action
, "RETR %d", (targ_t
) msgno
);
756 return traverse (action
, "ARTICLE %d", (targ_t
) msgno
);
762 traverse (int (*action
)(), const char *fmt
, ...)
766 char buffer
[sizeof(response
)];
769 result
= vcommand (fmt
, ap
);
774 strncpy (buffer
, response
, sizeof(buffer
));
777 switch (multiline ()) {
782 strncpy (response
, buffer
, sizeof(response
));
786 (*action
) (response
);
795 return command ("DELE %d", msgno
);
802 return command ("NOOP");
806 #if defined(MPOP) && !defined(NNTP)
810 return command ("LAST");
818 return command ("RSET");
823 pop_top (int msgno
, int lines
, int (*action
)())
826 return traverse (action
, "TOP %d %d", (targ_t
) msgno
, (targ_t
) lines
);
828 return traverse (action
, "HEAD %d", (targ_t
) msgno
);
835 pop_xtnd (int (*action
)(), char *fmt
, ...)
847 /* needs to be fixed... va_end needs to be added */
848 snprintf (buffer
, sizeof(buffer
), "XTND %s", fmt
);
849 result
= traverse (action
, buffer
, a
, b
, c
, d
);
853 snprintf (buffer
, sizeof(buffer
), fmt
, a
, b
, c
, d
);
854 ap
= brkstring (buffer
, " ", "\n"); /* a hack, i know... */
856 if (!strcasecmp(ap
[0], "x-bboards")) { /* XTND "X-BBOARDS group */
857 /* most of these parameters are meaningless under NNTP.
858 * bbc.c was modified to set AKA and LEADERS as appropriate,
859 * the rest are left blank.
863 if (!strcasecmp (ap
[0], "archive") && ap
[1]) {
864 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
866 xtnd_first
= 1; /* setup to fail in pop_stat */
869 if (!strcasecmp (ap
[0], "bboards")) {
871 if (ap
[1]) { /* XTND "BBOARDS group" */
872 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
873 if (command("GROUP %s", xtnd_name
) == NOTOK
)
876 /* action must ignore extra args */
877 strncpy (buffer
, response
, sizeof(buffer
));
878 ap
= brkstring (response
, " ", "\n");/* "211 nart first last g" */
879 xtnd_first
= atoi (ap
[2]);
880 xtnd_last
= atoi (ap
[3]);
885 } else { /* XTND "BBOARDS" */
886 return traverse (action
, "LIST", a
, b
, c
, d
);
889 return NOTOK
; /* unknown XTND command */
900 i
= command ("QUIT");
913 #endif /* CYRUS_SASL */
921 #if !defined(MPOP) || defined(NNTP)
925 command(const char *fmt
, ...)
931 result
= vcommand(fmt
, ap
);
939 vcommand (const char *fmt
, va_list ap
)
941 char *cp
, buffer
[BUFSIZ
];
943 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
947 fprintf(stderr
, "(encrypted) ");
948 #endif /* CYRUS_SASL */
950 if ((cp
= strchr (buffer
, ' ')))
952 fprintf (stderr
, "---> %s ********\n", buffer
);
958 fprintf (stderr
, "---> %s\n", buffer
);
961 if (putline (buffer
, output
) == NOTOK
)
965 if (poprint
&& sasl_ssf
)
966 fprintf(stderr
, "(decrypted) ");
967 #endif /* CYRUS_SASL */
969 switch (getline (response
, sizeof response
, input
)) {
972 fprintf (stderr
, "<--- %s\n", response
);
974 return (*response
== '+' ? OK
: NOTOK
);
976 return (*response
< CHAR_ERR
? OK
: NOTOK
);
982 fprintf (stderr
, "%s\n", response
);
986 return NOTOK
; /* NOTREACHED */
990 #if defined(MPOP) && !defined(NNTP)
998 char buffer
[BUFSIZ
+ TRMLEN
];
1000 if (getline (buffer
, sizeof buffer
, input
) != OK
)
1006 fprintf(stderr
, "(decrypted) ");
1007 #endif /* CYRUS_SASL */
1008 fprintf (stderr
, "<--- %s\n", response
);
1011 if (strncmp (buffer
, TRM
, TRMLEN
) == 0) {
1012 if (buffer
[TRMLEN
] == 0)
1015 strncpy (response
, buffer
+ TRMLEN
, sizeof(response
));
1018 strncpy (response
, buffer
, sizeof(response
));
1024 * Note that these functions have been modified to deal with layer encryption
1029 getline (char *s
, int n
, FILE *iop
)
1035 while (--n
> 0 && (c
= sasl_fgetc (iop
)) != EOF
&& c
!= -2)
1036 if ((*p
++ = c
) == '\n')
1040 if (ferror (iop
) && c
!= EOF
) {
1041 strncpy (response
, "error on connection", sizeof(response
));
1044 if (c
== EOF
&& p
== s
) {
1045 strncpy (response
, "connection closed by foreign host", sizeof(response
));
1059 putline (char *s
, FILE *iop
)
1062 char outbuf
[BUFSIZ
], *buf
;
1064 unsigned int buflen
;
1066 if (!sasl_complete
) {
1067 #endif /* CYRUS_SASL */
1068 fprintf (iop
, "%s\r\n", s
);
1072 * Build an output buffer, encrypt it using sasl_encode, and
1073 * squirt out the results.
1075 strncpy(outbuf
, s
, sizeof(outbuf
) - 3);
1076 outbuf
[sizeof(outbuf
) - 3] = '\0'; /* Just in case */
1077 strcat(outbuf
, "\r\n");
1079 result
= sasl_encode(conn
, outbuf
, strlen(outbuf
), &buf
, &buflen
);
1081 if (result
!= SASL_OK
) {
1082 snprintf(response
, sizeof(response
), "SASL encoding error: %s",
1083 sasl_errstring(result
, NULL
, NULL
));
1087 fwrite(buf
, buflen
, 1, iop
);
1090 #endif /* CYRUS_SASL */
1094 strncpy (response
, "lost connection", sizeof(response
));
1103 * Okay, our little fgetc replacement. Hopefully this is a little more
1104 * efficient than the last one.
1109 static unsigned char *buffer
= NULL
, *ptr
;
1110 static int size
= 0;
1112 unsigned int retbufsize
= 0;
1114 char *retbuf
, tmpbuf
[BUFSIZ
];
1117 * If we have some leftover data, return that
1122 return (int) *ptr
++;
1126 * Otherwise, fill our buffer until we have some data to return.
1129 while (retbufsize
== 0) {
1131 cc
= read(fileno(f
), tmpbuf
, sizeof(tmpbuf
));
1137 snprintf(response
, sizeof(response
), "Error during read from "
1138 "network: %s", strerror(errno
));
1143 * We're not allowed to call sasl_decode until sasl_complete is
1144 * true, so we do these gyrations ...
1147 if (!sasl_complete
) {
1154 result
= sasl_decode(conn
, tmpbuf
, cc
, &retbuf
, &retbufsize
);
1156 if (result
!= SASL_OK
) {
1157 snprintf(response
, sizeof(response
), "Error during SASL "
1158 "decoding: %s", sasl_errstring(result
, NULL
, NULL
));
1164 if (retbufsize
> size
) {
1165 buffer
= realloc(buffer
, retbufsize
);
1167 snprintf(response
, sizeof(response
), "Error during realloc in "
1168 "read routine: %s", strerror(errno
));
1174 memcpy(buffer
, retbuf
, retbufsize
);
1176 cnt
= retbufsize
- 1;
1180 return (int) buffer
[0];
1182 #endif /* CYRUS_SASL */