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.
14 extern int client(char *args
, char *protocol
, char *service
, int rproto
,
15 char *response
, int len_response
);
17 #if defined(NNTP) && !defined(PSHSBR)
21 #ifdef NNTP /* building pshsbr.o from popsbr.c */
25 #if !defined(NNTP) && defined(APOP)
30 # include <sasl/sasl.h>
31 # include <sasl/saslutil.h>
32 #endif /* CYRUS_SASL */
35 #include <h/signals.h>
40 #define TRMLEN (sizeof TRM - 1)
42 static int poprint
= 0;
43 static int pophack
= 0;
45 char response
[BUFSIZ
];
52 #if !defined(NNTP) && defined(MPOP)
53 # define command pop_command
54 # define multiline pop_multiline
58 # ifdef BPOP /* stupid */
59 static int xtnd_last
= -1;
60 static int xtnd_first
= 0;
61 static char xtnd_name
[512]; /* INCREDIBLE HACK!! */
66 static sasl_conn_t
*conn
; /* SASL connection state */
67 static int sasl_complete
= 0; /* Has sasl authentication succeeded? */
68 static int maxoutbuf
; /* Maximum output buffer size */
69 static sasl_ssf_t sasl_ssf
= 0; /* Security strength factor */
70 static int sasl_get_user(void *, int, const char **, unsigned *);
71 static int sasl_get_pass(sasl_conn_t
*, void *, int, sasl_secret_t
**);
77 static sasl_callback_t callbacks
[] = {
78 { SASL_CB_USER
, sasl_get_user
, NULL
},
79 #define POP_SASL_CB_N_USER 0
80 { SASL_CB_PASS
, sasl_get_pass
, NULL
},
81 #define POP_SASL_CB_N_PASS 1
82 { SASL_CB_LOG
, NULL
, NULL
},
83 { SASL_CB_LIST_END
, NULL
, NULL
},
85 #else /* CYRUS_SASL */
86 # define sasl_fgetc fgetc
87 #endif /* CYRUS_SASL */
92 #if !defined(NNTP) && defined(APOP)
93 static char *pop_auth (char *, char *);
96 #if defined(NNTP) || !defined(MPOP)
97 /* otherwise they are not static functions */
98 static int command(const char *, ...);
99 static int multiline(void);
103 static int pop_auth_sasl(char *, char *, char *);
104 static int sasl_fgetc(FILE *);
105 #endif /* CYRUS_SASL */
107 static int traverse (int (*)(), const char *, ...);
108 static int vcommand(const char *, va_list);
109 static int getline (char *, int, FILE *);
110 static int putline (char *, FILE *);
113 #if !defined(NNTP) && defined(APOP)
115 pop_auth (char *user
, char *pass
)
119 unsigned char *dp
, *ep
, digest
[16];
121 static char buffer
[BUFSIZ
];
123 if ((cp
= strchr (response
, '<')) == NULL
124 || (lp
= strchr (cp
, '>')) == NULL
) {
125 snprintf (buffer
, sizeof(buffer
), "APOP not available: %s", response
);
126 strncpy (response
, buffer
, sizeof(response
));
131 snprintf (buffer
, sizeof(buffer
), "%s%s", cp
, pass
);
133 MD5Init (&mdContext
);
134 MD5Update (&mdContext
, (unsigned char *) buffer
,
135 (unsigned int) strlen (buffer
));
136 MD5Final (digest
, &mdContext
);
139 buflen
= sizeof(buffer
);
141 snprintf (cp
, buflen
, "%s ", user
);
146 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]); dp
< ep
; ) {
147 snprintf (cp
, buflen
, "%02x", *dp
++ & 0xff);
155 #endif /* !NNTP && APOP */
159 * This function implements the AUTH command for various SASL mechanisms
161 * We do the whole SASL dialog here. If this completes, then we've
162 * authenticated successfully and have (possibly) negotiated a security
167 pop_auth_sasl(char *user
, char *host
, char *mech
)
169 int result
, status
, sasl_capability
= 0;
170 unsigned int buflen
, outlen
;
171 char server_mechs
[256], *buf
, outbuf
[BUFSIZ
];
172 const char *chosen_mech
;
173 sasl_security_properties_t secprops
;
174 struct pass_context p_context
;
179 * First off, we're going to send the CAPA command to see if we can
180 * even support the AUTH command, and if we do, then we'll get a
181 * list of mechanisms the server supports. If we don't support
182 * the CAPA command, then it's unlikely that we will support
186 if (command("CAPA") == NOTOK
) {
187 snprintf(response
, sizeof(response
),
188 "The POP CAPA command failed; POP server does not "
193 while ((status
= multiline()) != DONE
)
198 case DONE
: /* Shouldn't be possible, but just in case */
201 if (strncasecmp(response
, "SASL ", 5) == 0) {
203 * We've seen the SASL capability. Grab the mech list
206 strncpy(server_mechs
, response
+ 5, sizeof(server_mechs
));
211 if (!sasl_capability
) {
212 snprintf(response
, sizeof(response
), "POP server does not support "
218 * If we received a preferred mechanism, see if the server supports it.
221 if (mech
&& stringdex(mech
, server_mechs
) == -1) {
222 snprintf(response
, sizeof(response
), "Requested SASL mech \"%s\" is "
223 "not in list of supported mechanisms:\n%s",
229 * Start the SASL process. First off, initialize the SASL library.
232 callbacks
[POP_SASL_CB_N_USER
].context
= user
;
233 p_context
.user
= user
;
234 p_context
.host
= host
;
235 callbacks
[POP_SASL_CB_N_PASS
].context
= &p_context
;
237 result
= sasl_client_init(callbacks
);
239 if (result
!= SASL_OK
) {
240 snprintf(response
, sizeof(response
), "SASL library initialization "
241 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
245 result
= sasl_client_new("pop", host
, NULL
, NULL
, NULL
, 0, &conn
);
247 if (result
!= SASL_OK
) {
248 snprintf(response
, sizeof(response
), "SASL client initialization "
249 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
254 * Initialize the security properties
257 memset(&secprops
, 0, sizeof(secprops
));
258 secprops
.maxbufsize
= BUFSIZ
;
259 secprops
.max_ssf
= UINT_MAX
;
261 result
= sasl_setprop(conn
, SASL_SEC_PROPS
, &secprops
);
263 if (result
!= SASL_OK
) {
264 snprintf(response
, sizeof(response
), "SASL security property "
265 "initialization failed: %s", sasl_errdetail(conn
));
270 * Start the actual protocol. Feed the mech list into the library
271 * and get out a possible initial challenge
274 result
= sasl_client_start(conn
,
275 (const char *) (mech
? mech
: server_mechs
),
276 NULL
, (const char **) &buf
,
277 &buflen
, &chosen_mech
);
279 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
280 snprintf(response
, sizeof(response
), "SASL client start failed: %s",
281 sasl_errdetail(conn
));
286 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
287 if (status
!= SASL_OK
) {
288 snprintf(response
, sizeof(response
), "SASL base64 encode "
289 "failed: %s", sasl_errstring(status
, NULL
, NULL
));
293 status
= command("AUTH %s %s", chosen_mech
, outbuf
);
295 status
= command("AUTH %s", chosen_mech
);
297 while (result
== SASL_CONTINUE
) {
302 * If we get a "+OK" prefix to our response, then we should
303 * exit out of this exchange now (because authenticated should
307 if (strncmp(response
, "+OK", 3) == 0)
311 * Otherwise, make sure the server challenge is correctly formatted
314 if (strncmp(response
, "+ ", 2) != 0) {
316 snprintf(response
, sizeof(response
),
317 "Malformed authentication message from server");
321 result
= sasl_decode64(response
+ 2, strlen(response
+ 2),
322 outbuf
, sizeof(outbuf
), &outlen
);
324 if (result
!= SASL_OK
) {
326 snprintf(response
, sizeof(response
), "SASL base64 decode "
327 "failed: %s", sasl_errstring(result
, NULL
, NULL
));
331 result
= sasl_client_step(conn
, outbuf
, outlen
, NULL
,
332 (const char **) &buf
, &buflen
);
334 if (result
!= SASL_OK
&& result
!= SASL_CONTINUE
) {
336 snprintf(response
, sizeof(response
), "SASL client negotiaton "
337 "failed: %s", sasl_errdetail(conn
));
341 status
= sasl_encode64(buf
, buflen
, outbuf
, sizeof(outbuf
), NULL
);
343 if (status
!= SASL_OK
) {
345 snprintf(response
, sizeof(response
), "SASL base64 encode "
346 "failed: %s", sasl_errstring(status
, NULL
, NULL
));
350 status
= command(outbuf
);
354 * If we didn't get a positive final response, then error out
355 * (that probably means we failed an authorization check).
362 * We _should_ be okay now. Get a few properties now that negotiation
366 result
= sasl_getprop(conn
, SASL_MAXOUTBUF
, (const void **) &moutbuf
);
368 if (result
!= SASL_OK
) {
369 snprintf(response
, sizeof(response
), "Cannot retrieve SASL negotiated "
370 "output buffer size: %s", sasl_errdetail(conn
));
374 maxoutbuf
= *moutbuf
;
376 result
= sasl_getprop(conn
, SASL_SSF
, (const void **) &ssf
);
380 if (result
!= SASL_OK
) {
381 snprintf(response
, sizeof(response
), "Cannot retrieve SASL negotiated "
382 "security strength factor: %s", sasl_errdetail(conn
));
387 * Limit this to what we can deal with.
390 if (maxoutbuf
== 0 || maxoutbuf
> BUFSIZ
)
399 * Callback to return the userid sent down via the user parameter
403 sasl_get_user(void *context
, int id
, const char **result
, unsigned *len
)
405 char *user
= (char *) context
;
407 if (! result
|| id
!= SASL_CB_USER
)
408 return SASL_BADPARAM
;
418 * Callback to return the password (we call ruserpass, which can get it
423 sasl_get_pass(sasl_conn_t
*conn
, void *context
, int id
, sasl_secret_t
**psecret
)
425 struct pass_context
*p_context
= (struct pass_context
*) context
;
429 if (! psecret
|| id
!= SASL_CB_PASS
)
430 return SASL_BADPARAM
;
432 ruserpass(p_context
->user
, &(p_context
->host
), &pass
);
436 *psecret
= (sasl_secret_t
*) mh_xmalloc(sizeof(sasl_secret_t
) + len
);
438 (*psecret
)->len
= len
;
439 strcpy((char *) (*psecret
)->data
, pass
);
443 #endif /* CYRUS_SASL */
447 * Split string containing proxy command into an array of arguments
448 * suitable for passing to exec. Returned array must be freed. Shouldn't
449 * be possible to call this with host set to NULL.
452 parse_proxy(char *proxy
, char *host
)
456 int hlen
= strlen(host
);
461 /* skip any initial space */
462 for (pro
= proxy
; isspace(*pro
); pro
++)
465 /* calculate required size for argument array */
466 for (cur
= pro
; *cur
; cur
++) {
467 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1]))
469 else if (*cur
== '%' && cur
[1] == 'h') {
472 } else if (!isspace(*cur
))
476 /* put together list of arguments */
477 p
= pargv
= mh_xmalloc(pargc
* sizeof(char *));
478 c
= *pargv
= mh_xmalloc(plen
* sizeof(char));
479 for (cur
= pro
; *cur
; cur
++) {
480 if (isspace(*cur
) && cur
[1] && !isspace(cur
[1])) {
483 } else if (*cur
== '%' && cur
[1] == 'h') {
487 } else if (!isspace(*cur
))
495 pop_init (char *host
, char *user
, char *pass
, char *proxy
, int snoop
,
496 int rpop
, int kpop
, int sasl
, char *mech
)
504 if ((apop
= rpop
) < 0)
508 if (proxy
&& *proxy
) {
510 int inpipe
[2]; /* for reading from the server */
511 int outpipe
[2]; /* for sending to the server */
513 /* first give up any root priviledges we may have for rpop */
526 dup2(outpipe
[0],0); /* connect read end of connection */
527 dup2(inpipe
[1], 1); /* connect write end of connection */
528 if(inpipe
[0]>1) close(inpipe
[0]);
529 if(inpipe
[1]>1) close(inpipe
[1]);
530 if(outpipe
[0]>1) close(outpipe
[0]);
531 if(outpipe
[1]>1) close(outpipe
[1]);
533 /* run the proxy command */
534 argv
=parse_proxy(proxy
, host
);
535 execvp(argv
[0],argv
);
545 /* okay in the parent we do some stuff */
546 close(inpipe
[1]); /* child uses this */
547 close(outpipe
[0]); /* child uses this */
552 /* and write on fd2 */
560 snprintf (buffer
, sizeof(buffer
), "%s/%s", KPOP_PRINCIPAL
, "kpop");
561 if ((fd1
= client (host
, "tcp", buffer
, 0, response
, sizeof(response
))) == NOTOK
) {
565 snprintf (response
, sizeof(response
), "this version of nmh compiled without KPOP support");
569 if ((fd1
= client (host
, "tcp", POPSERVICE
, rpop
, response
, sizeof(response
))) == NOTOK
) {
574 if ((fd1
= client (host
, "tcp", "nntp", rpop
, response
, sizeof(response
))) == NOTOK
)
578 if ((fd2
= dup (fd1
)) == NOTOK
) {
581 if ((s
= strerror(errno
)))
582 snprintf (response
, sizeof(response
),
583 "unable to dup connection descriptor: %s", s
);
585 snprintf (response
, sizeof(response
),
586 "unable to dup connection descriptor: unknown error");
592 if (pop_set (fd1
, fd2
, snoop
) == NOTOK
)
594 if (pop_set (fd1
, fd2
, snoop
, (char *)0) == NOTOK
)
598 SIGNAL (SIGPIPE
, SIG_IGN
);
600 switch (getline (response
, sizeof response
, input
)) {
603 fprintf (stderr
, "<--- %s\n", response
);
605 if (*response
== '+') {
609 char *cp
= pop_auth (user
, pass
);
611 if (cp
&& command ("APOP %s", cp
) != NOTOK
)
618 if (pop_auth_sasl(user
, host
, mech
) != NOTOK
)
621 # endif /* CYRUS_SASL */
622 if (command ("USER %s", user
) != NOTOK
623 && command ("%s %s", rpop
? "RPOP" : (pophack
++, "PASS"),
627 if (command ("USER %s", user
) != NOTOK
628 && command ("PASS %s", pass
) != NOTOK
)
633 if (*response
< CHAR_ERR
) {
634 command ("MODE READER");
638 strncpy (buffer
, response
, sizeof(buffer
));
640 strncpy (response
, buffer
, sizeof(response
));
646 fprintf (stderr
, "%s\n", response
);
652 return NOTOK
; /* NOTREACHED */
657 pop_set (int in
, int out
, int snoop
, char *myname
)
660 pop_set (int in
, int out
, int snoop
)
665 if (myname
&& *myname
) {
666 /* interface from bbc to msh */
667 strncpy (xtnd_name
, myname
, sizeof(xtnd_name
));
671 if ((input
= fdopen (in
, "r")) == NULL
672 || (output
= fdopen (out
, "w")) == NULL
) {
673 strncpy (response
, "fdopen failed on connection descriptor", sizeof(response
));
689 pop_fd (char *in
, int inlen
, char *out
, int outlen
)
691 snprintf (in
, inlen
, "%d", fileno (input
));
692 snprintf (out
, outlen
, "%d", fileno (output
));
698 * Find out number of messages available
699 * and their total size.
703 pop_stat (int *nmsgs
, int *nbytes
)
710 if (command ("STAT") == NOTOK
)
713 *nmsgs
= *nbytes
= 0;
714 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
717 if (xtnd_last
< 0) { /* in msh, xtnd_name is set from myname */
718 if (command("GROUP %s", xtnd_name
) == NOTOK
)
721 ap
= brkstring (response
, " ", "\n"); /* "211 nart first last ggg" */
722 xtnd_first
= atoi (ap
[2]);
723 xtnd_last
= atoi (ap
[3]);
726 /* nmsgs is not the real nart, but an incredible simuation */
728 *nmsgs
= xtnd_last
- xtnd_first
+ 1; /* because of holes... */
731 *nbytes
= xtnd_first
; /* for subtracting offset in msh() */
739 pop_exists (int (*action
)())
741 #ifdef XMSGS /* hacked into NNTP 1.5 */
742 if (traverse (action
, "XMSGS %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
) == OK
)
745 /* provided by INN 1.4 */
746 if (traverse (action
, "LISTGROUP") == OK
)
748 return traverse (action
, "XHDR NONAME %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
);
755 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
, int *ids
)
758 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
768 if (command ("LIST %d", msgno
) == NOTOK
)
773 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
776 sscanf (response
, "+OK %d %d", msgs
, bytes
);
779 if (command ("STAT %d", msgno
) == NOTOK
)
789 if (command ("LIST") == NOTOK
)
792 for (i
= 0; i
< *nmsgs
; i
++)
793 switch (multiline ()) {
803 sscanf (response
, "%d %d %d",
804 msgs
++, bytes
++, ids
++);
807 sscanf (response
, "%d %d", msgs
++, bytes
++);
811 switch (multiline ()) {
826 pop_retr (int msgno
, int (*action
)())
829 return traverse (action
, "RETR %d", (targ_t
) msgno
);
831 return traverse (action
, "ARTICLE %d", (targ_t
) msgno
);
837 traverse (int (*action
)(), const char *fmt
, ...)
841 char buffer
[sizeof(response
)];
844 result
= vcommand (fmt
, ap
);
849 strncpy (buffer
, response
, sizeof(buffer
));
852 switch (multiline ()) {
857 strncpy (response
, buffer
, sizeof(response
));
861 (*action
) (response
);
870 return command ("DELE %d", msgno
);
877 return command ("NOOP");
881 #if defined(MPOP) && !defined(NNTP)
885 return command ("LAST");
893 return command ("RSET");
898 pop_top (int msgno
, int lines
, int (*action
)())
901 return traverse (action
, "TOP %d %d", (targ_t
) msgno
, (targ_t
) lines
);
903 return traverse (action
, "HEAD %d", (targ_t
) msgno
);
910 pop_xtnd (int (*action
)(), char *fmt
, ...)
922 /* needs to be fixed... va_end needs to be added */
923 snprintf (buffer
, sizeof(buffer
), "XTND %s", fmt
);
924 result
= traverse (action
, buffer
, a
, b
, c
, d
);
928 snprintf (buffer
, sizeof(buffer
), fmt
, a
, b
, c
, d
);
929 ap
= brkstring (buffer
, " ", "\n"); /* a hack, i know... */
931 if (!mh_strcasecmp(ap
[0], "x-bboards")) { /* XTND "X-BBOARDS group */
932 /* most of these parameters are meaningless under NNTP.
933 * bbc.c was modified to set AKA and LEADERS as appropriate,
934 * the rest are left blank.
938 if (!mh_strcasecmp (ap
[0], "archive") && ap
[1]) {
939 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
941 xtnd_first
= 1; /* setup to fail in pop_stat */
944 if (!mh_strcasecmp (ap
[0], "bboards")) {
946 if (ap
[1]) { /* XTND "BBOARDS group" */
947 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
948 if (command("GROUP %s", xtnd_name
) == NOTOK
)
951 /* action must ignore extra args */
952 strncpy (buffer
, response
, sizeof(buffer
));
953 ap
= brkstring (response
, " ", "\n");/* "211 nart first last g" */
954 xtnd_first
= atoi (ap
[2]);
955 xtnd_last
= atoi (ap
[3]);
960 } else { /* XTND "BBOARDS" */
961 return traverse (action
, "LIST", a
, b
, c
, d
);
964 return NOTOK
; /* unknown XTND command */
975 i
= command ("QUIT");
988 #endif /* CYRUS_SASL */
996 #if !defined(MPOP) || defined(NNTP)
1000 command(const char *fmt
, ...)
1006 result
= vcommand(fmt
, ap
);
1014 vcommand (const char *fmt
, va_list ap
)
1016 char *cp
, buffer
[BUFSIZ
];
1018 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
1022 fprintf(stderr
, "(encrypted) ");
1023 #endif /* CYRUS_SASL */
1025 if ((cp
= strchr (buffer
, ' ')))
1027 fprintf (stderr
, "---> %s ********\n", buffer
);
1033 fprintf (stderr
, "---> %s\n", buffer
);
1036 if (putline (buffer
, output
) == NOTOK
)
1040 if (poprint
&& sasl_ssf
)
1041 fprintf(stderr
, "(decrypted) ");
1042 #endif /* CYRUS_SASL */
1044 switch (getline (response
, sizeof response
, input
)) {
1047 fprintf (stderr
, "<--- %s\n", response
);
1049 return (*response
== '+' ? OK
: NOTOK
);
1051 return (*response
< CHAR_ERR
? OK
: NOTOK
);
1057 fprintf (stderr
, "%s\n", response
);
1061 return NOTOK
; /* NOTREACHED */
1065 #if defined(MPOP) && !defined(NNTP)
1073 char buffer
[BUFSIZ
+ TRMLEN
];
1075 if (getline (buffer
, sizeof buffer
, input
) != OK
)
1081 fprintf(stderr
, "(decrypted) ");
1082 #endif /* CYRUS_SASL */
1083 fprintf (stderr
, "<--- %s\n", response
);
1086 if (strncmp (buffer
, TRM
, TRMLEN
) == 0) {
1087 if (buffer
[TRMLEN
] == 0)
1090 strncpy (response
, buffer
+ TRMLEN
, sizeof(response
));
1093 strncpy (response
, buffer
, sizeof(response
));
1099 * Note that these functions have been modified to deal with layer encryption
1104 getline (char *s
, int n
, FILE *iop
)
1110 while (--n
> 0 && (c
= sasl_fgetc (iop
)) != EOF
&& c
!= -2)
1111 if ((*p
++ = c
) == '\n')
1115 if (ferror (iop
) && c
!= EOF
) {
1116 strncpy (response
, "error on connection", sizeof(response
));
1119 if (c
== EOF
&& p
== s
) {
1120 strncpy (response
, "connection closed by foreign host", sizeof(response
));
1134 putline (char *s
, FILE *iop
)
1137 char outbuf
[BUFSIZ
], *buf
;
1139 unsigned int buflen
;
1141 if (!sasl_complete
) {
1142 #endif /* CYRUS_SASL */
1143 fprintf (iop
, "%s\r\n", s
);
1147 * Build an output buffer, encrypt it using sasl_encode, and
1148 * squirt out the results.
1150 strncpy(outbuf
, s
, sizeof(outbuf
) - 3);
1151 outbuf
[sizeof(outbuf
) - 3] = '\0'; /* Just in case */
1152 strcat(outbuf
, "\r\n");
1154 result
= sasl_encode(conn
, outbuf
, strlen(outbuf
),
1155 (const char **) &buf
, &buflen
);
1157 if (result
!= SASL_OK
) {
1158 snprintf(response
, sizeof(response
), "SASL encoding error: %s",
1159 sasl_errdetail(conn
));
1163 fwrite(buf
, buflen
, 1, iop
);
1165 #endif /* CYRUS_SASL */
1169 strncpy (response
, "lost connection", sizeof(response
));
1178 * Okay, our little fgetc replacement. Hopefully this is a little more
1179 * efficient than the last one.
1184 static unsigned char *buffer
= NULL
, *ptr
;
1185 static int size
= 0;
1187 unsigned int retbufsize
= 0;
1189 char *retbuf
, tmpbuf
[BUFSIZ
];
1192 * If we have some leftover data, return that
1197 return (int) *ptr
++;
1201 * Otherwise, fill our buffer until we have some data to return.
1204 while (retbufsize
== 0) {
1206 cc
= read(fileno(f
), tmpbuf
, sizeof(tmpbuf
));
1212 snprintf(response
, sizeof(response
), "Error during read from "
1213 "network: %s", strerror(errno
));
1218 * We're not allowed to call sasl_decode until sasl_complete is
1219 * true, so we do these gyrations ...
1222 if (!sasl_complete
) {
1229 result
= sasl_decode(conn
, tmpbuf
, cc
,
1230 (const char **) &retbuf
, &retbufsize
);
1232 if (result
!= SASL_OK
) {
1233 snprintf(response
, sizeof(response
), "Error during SASL "
1234 "decoding: %s", sasl_errdetail(conn
));
1240 if (retbufsize
> size
) {
1241 buffer
= mh_xrealloc(buffer
, retbufsize
);
1245 memcpy(buffer
, retbuf
, retbufsize
);
1247 cnt
= retbufsize
- 1;
1249 return (int) buffer
[0];
1251 #endif /* CYRUS_SASL */