1 /* netsec.c -- Network security routines for handling protocols that
2 * require SASL and/or TLS.
4 * This code is Copyright (c) 2016, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
14 #include <sys/select.h>
18 #include <sasl/sasl.h>
19 #include <sasl/saslutil.h>
20 # if SASL_VERSION_FULL < 0x020125
21 /* Cyrus SASL 2.1.25 introduced the sasl_callback_ft prototype,
22 which has an explicit void parameter list, according to best
23 practice. So we need to cast to avoid compile warnings.
24 Provide this prototype for earlier versions. */
25 typedef int (*sasl_callback_ft
)();
26 # endif /* SASL_VERSION_FULL < 0x020125 */
28 static int netsec_get_user(void *context
, int id
, const char **result
,
30 static int netsec_get_password(sasl_conn_t
*conn
, void *context
, int id
,
31 sasl_secret_t
**psecret
);
33 static bool sasl_initialized
;
35 #define SASL_MAXRECVBUF 65536
36 #endif /* CYRUS_SASL */
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
42 static bool tls_initialized
;
43 static SSL_CTX
*sslctx
= NULL
; /* SSL Context */
45 #endif /* TLS_SUPPORT */
47 /* I'm going to hardcode this for now; maybe make it adjustable later? */
48 #define NETSEC_BUFSIZE 65536
51 * Our context structure, which holds all of the relevant information
55 struct _netsec_context
{
56 int ns_readfd
; /* Read descriptor for network connection */
57 int ns_writefd
; /* Write descriptor for network connection */
58 int ns_noclose
; /* Do not close file descriptors if set */
59 int ns_snoop
; /* If true, display network data */
60 netsec_snoop_callback
*ns_snoop_cb
; /* Snoop output callback */
61 void *ns_snoop_context
; /* Context data for snoop function */
62 char *ns_snoop_savebuf
; /* Save buffer for snoop data */
63 int ns_timeout
; /* Network read timeout, in seconds */
64 char *ns_userid
; /* Userid for authentication */
65 char *ns_hostname
; /* Hostname we've connected to */
66 unsigned char *ns_inbuffer
; /* Our read input buffer */
67 unsigned char *ns_inptr
; /* Our read buffer input pointer */
68 unsigned int ns_inbuflen
; /* Length of data in input buffer */
69 unsigned int ns_inbufsize
; /* Size of input buffer */
70 unsigned char *ns_outbuffer
;/* Output buffer */
71 unsigned char *ns_outptr
; /* Output buffer pointer */
72 unsigned int ns_outbuflen
; /* Output buffer data length */
73 unsigned int ns_outbufsize
; /* Output buffer size */
74 char *sasl_mech
; /* User-requested mechanism */
75 char *sasl_chosen_mech
; /* Mechanism chosen by SASL */
76 netsec_sasl_callback sasl_proto_cb
; /* SASL callback we use */
77 void *sasl_proto_context
; /* Context to be used by SASL callback */
79 char *oauth_service
; /* OAuth2 service name */
80 #endif /* OAUTH_SUPPORT */
82 sasl_conn_t
*sasl_conn
; /* SASL connection context */
83 sasl_ssf_t sasl_ssf
; /* SASL Security Strength Factor */
84 sasl_callback_t
*sasl_cbs
; /* Callbacks used by SASL */
85 nmh_creds_t sasl_creds
; /* Credentials (username/password) */
86 sasl_secret_t
*sasl_secret
; /* SASL password structure */
87 int sasl_seclayer
; /* If true, SASL security layer is enabled */
88 char *sasl_tmpbuf
; /* Temporary read buffer for decodes */
89 size_t sasl_maxbufsize
; /* Maximum negotiated SASL buffer size */
90 #endif /* CYRUS_SASL */
92 BIO
*ssl_io
; /* BIO used for connection I/O */
93 int tls_active
; /* If true, TLS is running */
94 #endif /* TLS_SUPPORT */
98 * Function to read data from the actual network socket
101 static int netsec_fillread(netsec_context
*ns_context
, char **errstr
);
104 * Code to check the ASCII content of a byte array.
107 static int checkascii(const unsigned char *byte
, size_t len
);
110 * How this code works, in general.
112 * _If_ we are using no encryption then we buffer the network data
113 * through ns_inbuffer and ns_outbuffer. That should be relatively
116 * If we use encryption, then ns_inbuffer and ns_outbuffer contain the
117 * cleartext data. When it comes time to send the encrypted data on the
118 * (either from a flush or the buffer is full) we either use BIO_write()
119 * for TLS or sasl_encode() (followed by a write() for Cyrus-SASL. For
120 * reads we either use BIO_read() (TLS) or do a network read into a
121 * temporary buffer and use sasl_decode() (Cyrus-SASL). Note that if
122 * negotiate TLS then we disable SASL encryption.
124 * We used to use a buffering BIO for the reads/writes for TLS, but it
125 * ended up being complicated to special-case the buffering for everything
126 * except TLS, so the buffering is now unified, no matter which encryption
127 * method is being used (even none).
129 * For SASL authentication, we make use of (for now) the Cyrus-SASL
130 * library. For some mechanisms, we implement those mechanisms directly
131 * since the Cyrus SASL library doesn't support them (like OAuth).
135 * Allocate and initialize our security context
145 nsc
->ns_writefd
= -1;
148 nsc
->ns_snoop_cb
= NULL
;
149 nsc
->ns_snoop_context
= NULL
;
150 nsc
->ns_snoop_savebuf
= NULL
;
151 nsc
->ns_userid
= NULL
;
152 nsc
->ns_hostname
= NULL
;
153 nsc
->ns_timeout
= 60; /* Our default */
154 nsc
->ns_inbufsize
= NETSEC_BUFSIZE
;
155 nsc
->ns_inbuffer
= mh_xmalloc(nsc
->ns_inbufsize
);
156 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
157 nsc
->ns_inbuflen
= 0;
158 nsc
->ns_outbufsize
= NETSEC_BUFSIZE
;
159 nsc
->ns_outbuffer
= mh_xmalloc(nsc
->ns_outbufsize
);
160 nsc
->ns_outptr
= nsc
->ns_outbuffer
;
161 nsc
->ns_outbuflen
= 0;
162 nsc
->sasl_mech
= NULL
;
163 nsc
->sasl_chosen_mech
= NULL
;
164 nsc
->sasl_proto_cb
= NULL
;
165 nsc
->sasl_proto_context
= NULL
;
167 nsc
->oauth_service
= NULL
;
168 #endif /* OAUTH_SUPPORT */
170 nsc
->sasl_conn
= NULL
;
171 nsc
->sasl_cbs
= NULL
;
172 nsc
->sasl_creds
= NULL
;
173 nsc
->sasl_secret
= NULL
;
175 nsc
->sasl_seclayer
= 0;
176 nsc
->sasl_tmpbuf
= NULL
;
177 nsc
->sasl_maxbufsize
= 0;
178 #endif /* CYRUS_SASL */
182 #endif /* TLS_SUPPORT */
187 * Shutdown the connection completely and free all resources.
191 netsec_shutdown(netsec_context
*nsc
)
193 free(nsc
->ns_userid
);
194 free(nsc
->ns_hostname
);
195 free(nsc
->ns_inbuffer
);
196 free(nsc
->ns_outbuffer
);
197 free(nsc
->sasl_mech
);
198 free(nsc
->sasl_chosen_mech
);
199 free(nsc
->ns_snoop_savebuf
);
201 free(nsc
->oauth_service
);
202 #endif /* OAUTH_SERVICE */
205 sasl_dispose(&nsc
->sasl_conn
);
208 nmh_credentials_free(nsc
->sasl_creds
);
209 if (nsc
->sasl_secret
) {
210 if (nsc
->sasl_secret
->len
> 0) {
211 memset(nsc
->sasl_secret
->data
, 0, nsc
->sasl_secret
->len
);
213 free(nsc
->sasl_secret
);
215 free(nsc
->sasl_tmpbuf
);
216 #endif /* CYRUS_SASL */
220 * I checked; BIO_free_all() will cause SSL_shutdown to be called
221 * on the SSL object in the chain.
223 BIO_free_all(nsc
->ssl_io
);
224 #endif /* TLS_SUPPORT */
226 if (! nsc
->ns_noclose
) {
227 if (nsc
->ns_readfd
!= -1)
228 close(nsc
->ns_readfd
);
229 if (nsc
->ns_writefd
!= -1 && nsc
->ns_writefd
!= nsc
->ns_readfd
)
230 close(nsc
->ns_writefd
);
237 * Set the file descriptor for our context
241 netsec_set_fd(netsec_context
*nsc
, int readfd
, int writefd
)
243 nsc
->ns_readfd
= readfd
;
244 nsc
->ns_writefd
= writefd
;
248 * Set the userid used for authentication for this context
252 netsec_set_userid(netsec_context
*nsc
, const char *userid
)
254 nsc
->ns_userid
= getcpy(userid
);
258 * Set the hostname of the remote host we're connecting to.
262 netsec_set_hostname(netsec_context
*nsc
, const char *hostname
)
264 nsc
->ns_hostname
= mh_xstrdup(hostname
);
268 * Get the snoop flag for this connection
272 netsec_get_snoop(netsec_context
*nsc
)
274 return nsc
->ns_snoop
;
278 * Set the snoop flag for this connection
282 netsec_set_snoop(netsec_context
*nsc
, int snoop
)
284 nsc
->ns_snoop
= snoop
;
288 * Set the snoop callback for this connection.
292 netsec_set_snoop_callback(netsec_context
*nsc
,
293 netsec_snoop_callback callback
, void *context
)
295 nsc
->ns_snoop_cb
= callback
;
296 nsc
->ns_snoop_context
= context
;
300 * A base64-decoding snoop callback
304 netsec_b64_snoop_decoder(netsec_context
*nsc
, const char *string
, size_t len
,
307 unsigned char *decoded
;
312 offset
= context
? *((int *) context
) : 0;
316 * Output non-base64 data first.
318 fprintf(stderr
, "%.*s", offset
, string
);
323 if (decodeBase64(string
, &decoded
, &decodedlen
, 1, NULL
) == OK
) {
325 * Some mechanisms produce large binary tokens, which aren't really
326 * readable. So let's do a simple heuristic. If the token is greater
327 * than 100 characters _and_ the first 100 bytes are more than 50%
328 * non-ASCII, then don't print the decoded buffer, just the
331 if (decodedlen
> 100 && !checkascii(decoded
, 100)) {
332 fprintf(stderr
, "%.*s\n", (int) len
, string
);
335 hexify(decoded
, decodedlen
, &hexified
);
336 fprintf(stderr
, "b64<%s>\n", hexified
);
341 fprintf(stderr
, "%.*s\n", (int) len
, string
);
346 * If the ASCII content is > 50%, return 1
350 checkascii(const unsigned char *bytes
, size_t len
)
352 size_t count
= 0, half
= len
/ 2;
355 if (isascii(*bytes
) && isprint(*bytes
) && ++count
> half
)
358 /* No chance by this point */
359 if (count
+ len
< half
)
367 * Set the read timeout for this connection
371 netsec_set_timeout(netsec_context
*nsc
, int timeout
)
373 nsc
->ns_timeout
= timeout
;
377 * Read data from the network. Basically, return anything in our buffer,
378 * otherwise fill from the network.
382 netsec_read(netsec_context
*nsc
, void *buffer
, size_t size
, char **errstr
)
387 * If our buffer is empty, then we should fill it now
390 if (nsc
->ns_inbuflen
== 0) {
391 if (netsec_fillread(nsc
, errstr
) != OK
)
396 * netsec_fillread only returns if the buffer is full, so we can
397 * assume here that this has something in it.
400 retlen
= min(size
, nsc
->ns_inbuflen
);
402 memcpy(buffer
, nsc
->ns_inptr
, retlen
);
404 if (retlen
== (int) nsc
->ns_inbuflen
) {
406 * We've emptied our buffer, so reset everything.
408 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
409 nsc
->ns_inbuflen
= 0;
411 nsc
->ns_inptr
+= size
;
412 nsc
->ns_inbuflen
-= size
;
419 * Get a "line" (CR/LF) terminated from the network.
421 * Okay, we play some games here, so pay attention:
423 * - Unlike every other function, we return a pointer to the
424 * existing buffer. This pointer is valid until you call another
425 * read function again.
426 * - We NUL-terminate the buffer right at the end, before the CR-LF terminator.
427 * - Technically we look for a LF; if we find a CR right before it, then
429 * - If your data may contain embedded NULs, this won't work. You should
430 * be using netsec_read() in that case.
434 netsec_readline(netsec_context
*nsc
, size_t *len
, char **errstr
)
436 unsigned char *ptr
= nsc
->ns_inptr
;
437 size_t count
= 0, offset
;
441 * Search through our existing buffer for a LF
444 while (count
< nsc
->ns_inbuflen
) {
446 if (*ptr
++ == '\n') {
447 char *sptr
= (char *) nsc
->ns_inptr
;
448 if (count
> 1 && *(ptr
- 2) == '\r')
452 *len
= ptr
- nsc
->ns_inptr
;
453 nsc
->ns_inptr
+= count
;
454 nsc
->ns_inbuflen
-= count
;
457 if (nsc
->sasl_seclayer
)
458 fprintf(stderr
, "(sasl-decrypted) ");
459 #endif /* CYRUS_SASL */
462 fprintf(stderr
, "(tls-decrypted) ");
463 #endif /* TLS_SUPPORT */
464 fprintf(stderr
, "<= ");
465 if (nsc
->ns_snoop_cb
)
466 nsc
->ns_snoop_cb(nsc
, sptr
, strlen(sptr
),
467 nsc
->ns_snoop_context
);
478 * Hm, we didn't find a \n. If we've already searched half of the input
479 * buffer, return an error.
482 if (count
>= nsc
->ns_inbufsize
/ 2) {
483 netsec_err(errstr
, "Unable to find a line terminator after %zu bytes",
489 * Okay, get some more network data. This may move inptr, so regenerate
493 offset
= ptr
- nsc
->ns_inptr
;
495 if (netsec_fillread(nsc
, errstr
) != OK
)
498 ptr
= nsc
->ns_inptr
+ offset
;
502 return NULL
; /* Should never reach this */
506 * Fill our read buffer with some data from the network.
510 netsec_fillread(netsec_context
*nsc
, char **errstr
)
514 size_t readbufsize
, remaining
, startoffset
;
518 * If inbuflen is zero, that means the buffer has been emptied
519 * completely. In that case move inptr back to the start.
522 if (nsc
->ns_inbuflen
== 0) {
523 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
526 #if defined(CYRUS_SASL) || defined(TLS_SUPPORT)
528 #endif /* CYRUS_SASL || TLS_SUPPORT */
530 * If we are using TLS and there's anything pending, then skip the
534 if (!nsc
->tls_active
|| BIO_pending(nsc
->ssl_io
) == 0)
535 #endif /* TLS_SUPPORT */
541 FD_SET(nsc
->ns_readfd
, &rfds
);
543 tv
.tv_sec
= nsc
->ns_timeout
;
546 rc
= select(nsc
->ns_readfd
+ 1, &rfds
, NULL
, NULL
, &tv
);
549 netsec_err(errstr
, "select() while reading failed: %s",
555 netsec_err(errstr
, "read() timed out after %d seconds",
561 * At this point, we know that rc is 1, so there's not even any
562 * point to check to see if our descriptor is set in rfds.
569 * startoffset is the offset from the beginning of the input
570 * buffer to data that is in our input buffer, but has not yet
571 * been consumed. This can be non-zero if functions like
572 * netsec_readline() leave leftover data.
574 * remaining is the remaining amount of unconsumed data in the input
577 * end is a pointer to the end of the valid data + 1; it's where
578 * the next read should go.
581 startoffset
= nsc
->ns_inptr
- nsc
->ns_inbuffer
;
582 remaining
= nsc
->ns_inbufsize
- (startoffset
+ nsc
->ns_inbuflen
);
583 end
= nsc
->ns_inptr
+ nsc
->ns_inbuflen
;
586 * If we're past the halfway point in our read buffers, shuffle everything
587 * back to the beginning.
590 if (startoffset
> nsc
->ns_inbufsize
/ 2) {
591 memmove(nsc
->ns_inbuffer
, nsc
->ns_inptr
, nsc
->ns_inbuflen
);
592 nsc
->ns_inptr
= nsc
->ns_inbuffer
;
594 remaining
= nsc
->ns_inbufsize
- nsc
->ns_inbuflen
;
595 end
= nsc
->ns_inptr
+ nsc
->ns_inbuflen
;
599 * If we are using TLS, then just read via the BIO. But we still
600 * use our local buffer.
603 if (nsc
->tls_active
) {
604 rc
= BIO_read(nsc
->ssl_io
, end
, remaining
);
610 * Check to see if we're supposed to retry; if so,
611 * then go back and read again.
614 if (BIO_should_retry(nsc
->ssl_io
))
618 * Okay, fine. Get the real error out of the SSL context.
621 if (BIO_get_ssl(nsc
->ssl_io
, &ssl
) < 1) {
622 netsec_err(errstr
, "SSL_read() returned 0, but cannot "
623 "retrieve SSL context");
627 errcode
= SSL_get_error(ssl
, rc
);
628 if (errcode
== SSL_ERROR_ZERO_RETURN
) {
629 netsec_err(errstr
, "TLS peer closed remote connection");
631 netsec_err(errstr
, "TLS network read failed: %s",
632 ERR_error_string(ERR_peek_last_error(), NULL
));
635 ERR_print_errors_fp(stderr
);
639 /* Definitely an error */
640 netsec_err(errstr
, "Read on TLS connection failed: %s",
641 ERR_error_string(ERR_get_error(), NULL
));
645 nsc
->ns_inbuflen
+= rc
;
649 #endif /* TLS_SUPPORT */
652 * Okay, time to read some data. Either we're just doing it straight
653 * or we're passing it through sasl_decode() first.
657 if (nsc
->sasl_seclayer
) {
658 readbuf
= nsc
->sasl_tmpbuf
;
659 readbufsize
= nsc
->sasl_maxbufsize
;
661 #endif /* CYRUS_SASL */
663 readbuf
= (char *) end
;
664 readbufsize
= remaining
;
668 * At this point, we should have active data on the connection (see
669 * select() above) so this read SHOULDN'T block. Hopefully.
672 rc
= read(nsc
->ns_readfd
, readbuf
, readbufsize
);
675 netsec_err(errstr
, "Received EOF on network read");
680 netsec_err(errstr
, "Network read failed: %s", strerror(errno
));
685 * Okay, so we've had a successful read. If we are doing SASL security
686 * layers, pass this through sasl_decode(). sasl_decode() can return
687 * 0 bytes decoded; if that happens, jump back to the beginning. Otherwise
688 * we can just update our length pointer.
692 if (nsc
->sasl_seclayer
) {
694 unsigned int tmpoutlen
;
696 rc
= sasl_decode(nsc
->sasl_conn
, nsc
->sasl_tmpbuf
, rc
,
697 &tmpout
, &tmpoutlen
);
700 netsec_err(errstr
, "Unable to decode SASL network data: %s",
701 sasl_errdetail(nsc
->sasl_conn
));
712 if (tmpoutlen
> remaining
) {
713 netsec_err(errstr
, "Internal error: SASL decode buffer overflow!");
717 memcpy(end
, tmpout
, tmpoutlen
);
719 nsc
->ns_inbuflen
+= tmpoutlen
;
721 #endif /* CYRUS_SASL */
722 nsc
->ns_inbuflen
+= rc
;
728 * Write data to our network connection. Really, fill up the buffer as
729 * much as we can, and flush it out if necessary. netsec_flush() does
734 netsec_write(netsec_context
*nsc
, const void *buffer
, size_t size
,
737 const unsigned char *bufptr
= buffer
;
746 * Run a loop copying in data to our local buffer; when we're done with
747 * any buffer overflows then just copy any remaining data in.
750 while ((int) size
>= (remaining
= nsc
->ns_outbufsize
- nsc
->ns_outbuflen
)) {
751 memcpy(nsc
->ns_outptr
, bufptr
, remaining
);
754 * In theory I should increment outptr, but netsec_flush just resets
757 nsc
->ns_outbuflen
= nsc
->ns_outbufsize
;
759 rc
= netsec_flush(nsc
, errstr
);
769 * Copy any leftover data into the buffer.
773 memcpy(nsc
->ns_outptr
, bufptr
, size
);
774 nsc
->ns_outptr
+= size
;
775 nsc
->ns_outbuflen
+= size
;
782 * Our network printf() routine, which really just calls netsec_vprintf().
786 netsec_printf(netsec_context
*nsc
, char **errstr
, const char *format
, ...)
791 va_start(ap
, format
);
792 rc
= netsec_vprintf(nsc
, errstr
, format
, ap
);
799 * Write bytes to the network using printf()-style formatting.
801 * Again, for the most part copy stuff into our buffer to be flushed
806 netsec_vprintf(netsec_context
*nsc
, char **errstr
, const char *format
,
812 * Cheat a little. If we can fit the data into our outgoing buffer,
813 * great! If not, generate a flush and retry once.
817 rc
= vsnprintf((char *) nsc
->ns_outptr
,
818 nsc
->ns_outbufsize
- nsc
->ns_outbuflen
, format
, ap
);
820 if (rc
>= (int) (nsc
->ns_outbufsize
- nsc
->ns_outbuflen
)) {
822 * This means we have an overflow. Note that we don't actually
823 * make use of the terminating NUL, but according to the spec
824 * vsnprintf() won't write to the last byte in the string; that's
825 * why we have to use >= in the comparison above.
827 if (nsc
->ns_outbuffer
== nsc
->ns_outptr
) {
829 * Whoops, if the buffer pointer was the same as the start of the
830 * buffer, that means we overflowed the internal buffer.
831 * At that point, just give up.
833 netsec_err(errstr
, "Internal error: wanted to printf() a total of "
834 "%d bytes, but our buffer size was only %d bytes",
835 rc
, nsc
->ns_outbufsize
);
839 * Generate a flush (which may be inefficient, but hopefully
840 * it isn't) and then try again.
842 if (netsec_flush(nsc
, errstr
) != OK
)
845 * After this, outbuffer should == outptr, so we shouldn't
846 * hit this next time around.
851 nsc
->ns_outptr
+= rc
;
852 nsc
->ns_outbuflen
+= rc
;
858 * Flush out any buffered data in our output buffers. This routine is
859 * actually where the real network writes take place.
863 netsec_flush(netsec_context
*nsc
, char **errstr
)
865 const char *netoutbuf
= (const char *) nsc
->ns_outbuffer
;
866 unsigned int netoutlen
= nsc
->ns_outbuflen
;
877 * If we have snoop turned on, output the data.
879 * Note here; if we don't have a CR or LF at the end, save the data
880 * in ns_snoop_savebuf for later and print it next time.
884 unsigned int snoopoutlen
= netoutlen
;
885 const char *snoopoutbuf
= (const char *) nsc
->ns_outbuffer
;
887 while (snoopoutlen
> 0) {
888 const char *end
= strpbrk(snoopoutbuf
, "\r\n");
892 if (nsc
->ns_snoop_savebuf
) {
893 nsc
->ns_snoop_savebuf
= mh_xrealloc(nsc
->ns_snoop_savebuf
,
894 strlen(nsc
->ns_snoop_savebuf
) +
896 strncat(nsc
->ns_snoop_savebuf
, snoopoutbuf
, snoopoutlen
);
898 nsc
->ns_snoop_savebuf
= mh_xmalloc(snoopoutlen
+ 1);
899 strncpy(nsc
->ns_snoop_savebuf
, snoopoutbuf
, snoopoutlen
);
900 nsc
->ns_snoop_savebuf
[snoopoutlen
] = '\0';
905 outlen
= end
- snoopoutbuf
;
908 if (nsc
->sasl_seclayer
)
909 fprintf(stderr
, "(sasl-encrypted) ");
910 #endif /* CYRUS_SASL */
913 fprintf(stderr
, "(tls-encrypted) ");
914 #endif /* TLS_SUPPORT */
915 fprintf(stderr
, "=> ");
916 if (nsc
->ns_snoop_cb
) {
918 unsigned int cb_len
= outlen
;
920 if (nsc
->ns_snoop_savebuf
) {
921 cb_len
+= strlen(nsc
->ns_snoop_savebuf
);
922 nsc
->ns_snoop_savebuf
= mh_xrealloc(nsc
->ns_snoop_savebuf
,
924 ptr
= nsc
->ns_snoop_savebuf
;
929 nsc
->ns_snoop_cb(nsc
, ptr
, cb_len
, nsc
->ns_snoop_context
);
931 if (nsc
->ns_snoop_savebuf
) {
932 free(nsc
->ns_snoop_savebuf
);
933 nsc
->ns_snoop_savebuf
= NULL
;
936 if (nsc
->ns_snoop_savebuf
) {
937 fprintf(stderr
, "%s", nsc
->ns_snoop_savebuf
);
938 free(nsc
->ns_snoop_savebuf
);
939 nsc
->ns_snoop_savebuf
= NULL
;
941 fprintf(stderr
, "%.*s\n", outlen
, snoopoutbuf
);
945 * Alright, hopefully any previous leftover data is done,
946 * and we have the current line output. Move things past the
950 snoopoutlen
-= outlen
;
951 snoopoutbuf
+= outlen
;
953 if (snoopoutlen
> 0 && *snoopoutbuf
== '\r') {
958 if (snoopoutlen
> 0 && *snoopoutbuf
== '\n') {
966 * If SASL security layers are in effect, run the data through
967 * sasl_encode() first.
970 if (nsc
->sasl_seclayer
) {
971 rc
= sasl_encode(nsc
->sasl_conn
, (const char *) nsc
->ns_outbuffer
,
972 nsc
->ns_outbuflen
, &netoutbuf
, &netoutlen
);
975 netsec_err(errstr
, "SASL data encoding failed: %s",
976 sasl_errdetail(nsc
->sasl_conn
));
981 #endif /* CYRUS_SASL */
984 * If TLS is active, then use those functions to write out the
988 if (nsc
->tls_active
) {
989 if (BIO_write(nsc
->ssl_io
, netoutbuf
, netoutlen
) <= 0) {
990 netsec_err(errstr
, "Error writing to TLS connection: %s",
991 ERR_error_string(ERR_get_error(), NULL
));
995 #endif /* TLS_SUPPORT */
997 rc
= write(nsc
->ns_writefd
, netoutbuf
, netoutlen
);
1000 netsec_err(errstr
, "write() failed: %s", strerror(errno
));
1005 nsc
->ns_outptr
= nsc
->ns_outbuffer
;
1006 nsc
->ns_outbuflen
= 0;
1012 * Set various SASL protocol parameters
1016 netsec_set_sasl_params(netsec_context
*nsc
, const char *service
,
1017 const char *mechanism
, netsec_sasl_callback callback
,
1018 void *context
, char **errstr
)
1021 sasl_callback_t
*sasl_cbs
;
1024 if (!nsc
->ns_hostname
) {
1025 netsec_err(errstr
, "Internal error: ns_hostname is NULL");
1029 if (! sasl_initialized
) {
1030 retval
= sasl_client_init(NULL
);
1031 if (retval
!= SASL_OK
) {
1032 netsec_err(errstr
, "SASL client initialization failed: %s",
1033 sasl_errstring(retval
, NULL
, NULL
));
1036 sasl_initialized
= true;
1040 * Allocate an array of SASL callbacks for this connection.
1041 * Right now we just allocate an array of four callbacks.
1044 sasl_cbs
= mh_xmalloc(sizeof(*sasl_cbs
) * 4);
1046 sasl_cbs
[0].id
= SASL_CB_USER
;
1047 sasl_cbs
[0].proc
= (sasl_callback_ft
) netsec_get_user
;
1048 sasl_cbs
[0].context
= nsc
;
1050 sasl_cbs
[1].id
= SASL_CB_AUTHNAME
;
1051 sasl_cbs
[1].proc
= (sasl_callback_ft
) netsec_get_user
;
1052 sasl_cbs
[1].context
= nsc
;
1054 sasl_cbs
[2].id
= SASL_CB_PASS
;
1055 sasl_cbs
[2].proc
= (sasl_callback_ft
) netsec_get_password
;
1056 sasl_cbs
[2].context
= nsc
;
1058 sasl_cbs
[3].id
= SASL_CB_LIST_END
;
1059 sasl_cbs
[3].proc
= NULL
;
1060 sasl_cbs
[3].context
= NULL
;
1062 nsc
->sasl_cbs
= sasl_cbs
;
1064 retval
= sasl_client_new(service
, nsc
->ns_hostname
, NULL
, NULL
,
1065 nsc
->sasl_cbs
, 0, &nsc
->sasl_conn
);
1068 netsec_err(errstr
, "SASL new client allocation failed: %s",
1069 sasl_errstring(retval
, NULL
, NULL
));
1074 * Set up our credentials
1077 nsc
->sasl_creds
= nmh_get_credentials(nsc
->ns_hostname
, nsc
->ns_userid
);
1079 #else /* CYRUS_SASL */
1080 NMH_UNUSED(service
);
1082 #endif /* CYRUS_SASL */
1085 * According to the RFC, mechanisms can only be uppercase letter, numbers,
1086 * and a hyphen or underscore. So make sure we uppercase any letters
1087 * in case the user passed in lowercase.
1091 nsc
->sasl_mech
= mh_xstrdup(mechanism
);
1092 to_upper(nsc
->sasl_mech
);
1095 nsc
->sasl_proto_cb
= callback
;
1096 nsc
->sasl_proto_context
= context
;
1103 * Our userid callback; return the specified username to the SASL
1104 * library when asked.
1108 netsec_get_user(void *context
, int id
, const char **result
,
1111 netsec_context
*nsc
= (netsec_context
*) context
;
1113 if (! result
|| (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
))
1114 return SASL_BADPARAM
;
1116 *result
= nmh_cred_get_user(nsc
->sasl_creds
);
1119 *len
= strlen(*result
);
1125 * Retrieve a password and return it to SASL
1129 netsec_get_password(sasl_conn_t
*conn
, void *context
, int id
,
1130 sasl_secret_t
**psecret
)
1132 netsec_context
*nsc
= (netsec_context
*) context
;
1133 const char *password
;
1138 if (! psecret
|| id
!= SASL_CB_PASS
)
1139 return SASL_BADPARAM
;
1141 password
= nmh_cred_get_password(nsc
->sasl_creds
);
1143 len
= strlen(password
);
1146 * sasl_secret_t includes 1 bytes for "data" already, so that leaves
1147 * us room for a terminating NUL
1150 *psecret
= malloc(sizeof(sasl_secret_t
) + len
);
1155 (*psecret
)->len
= len
;
1156 strcpy((char *) (*psecret
)->data
, password
);
1158 nsc
->sasl_secret
= *psecret
;
1162 #endif /* CYRUS_SASL */
1165 * Negotiate SASL on this connection
1169 netsec_negotiate_sasl(netsec_context
*nsc
, const char *mechlist
, char **errstr
)
1172 sasl_security_properties_t secprops
;
1173 const char *chosen_mech
;
1174 const unsigned char *saslbuf
;
1175 unsigned char *outbuf
;
1176 unsigned int saslbuflen
, outbuflen
;
1180 #ifdef OAUTH_SUPPORT
1181 unsigned char *xoauth_client_res
;
1182 size_t xoauth_client_res_len
;
1183 #endif /* OAUTH_SUPPORT */
1184 #if defined CYRUS_SASL || defined OAUTH_SUPPORT
1186 #endif /* CYRUS_SASL || OAUTH_SUPPORT */
1189 * If we've been passed a requested mechanism, check our mechanism
1190 * list from the protocol. If it's not supported, return an error.
1193 if (nsc
->sasl_mech
) {
1194 char **str
, *mlist
= getcpy(mechlist
);
1197 str
= brkstring(mlist
, " ", NULL
);
1199 for (i
= 0; str
[i
] != NULL
; i
++) {
1200 if (strcasecmp(nsc
->sasl_mech
, str
[i
]) == 0) {
1205 i
= (str
[i
] == NULL
);
1210 netsec_err(errstr
, "Chosen mechanism %s not supported by server",
1216 #ifdef OAUTH_SUPPORT
1217 if (nsc
->sasl_mech
&& strcasecmp(nsc
->sasl_mech
, "XOAUTH2") == 0) {
1219 * This should be relatively straightforward, but requires some
1220 * help from the plugin. Basically, if XOAUTH2 is a success,
1221 * the callback has to return success, but no output data. If
1222 * there is output data, it will be assumed that it is the JSON
1226 if (! nsc
->oauth_service
) {
1227 netsec_err(errstr
, "Internal error: OAuth2 service name not given");
1231 nsc
->sasl_chosen_mech
= mh_xstrdup(nsc
->sasl_mech
);
1233 if (mh_oauth_do_xoauth(nsc
->ns_userid
, nsc
->oauth_service
,
1234 &xoauth_client_res
, &xoauth_client_res_len
,
1235 nsc
->ns_snoop
? stderr
: NULL
) != OK
) {
1236 netsec_err(errstr
, "Internal error: Unable to get OAuth2 "
1241 rc
= nsc
->sasl_proto_cb(NETSEC_SASL_START
, xoauth_client_res
,
1242 xoauth_client_res_len
, NULL
, 0,
1243 nsc
->sasl_proto_context
, errstr
);
1244 free(xoauth_client_res
);
1250 * Okay, we need to do a NETSEC_SASL_FINISH now. If we return
1251 * success, we indicate that with no output data. But if we
1252 * fail, then send a blank message and get the resulting
1256 rc
= nsc
->sasl_proto_cb(NETSEC_SASL_FINISH
, NULL
, 0, NULL
, 0,
1257 nsc
->sasl_proto_context
, errstr
);
1261 * We're going to assume the error here is a JSON response;
1262 * we ignore it and send a blank message in response. We should
1263 * then get a failure messages with a useful error. We should
1264 * NOT get a success message at this point.
1267 nsc
->sasl_proto_cb(NETSEC_SASL_WRITE
, NULL
, 0, NULL
, 0,
1268 nsc
->sasl_proto_context
, NULL
);
1269 rc
= nsc
->sasl_proto_cb(NETSEC_SASL_FINISH
, NULL
, 0, NULL
, 0,
1270 nsc
->sasl_proto_context
, errstr
);
1272 netsec_err(errstr
, "Unexpected success after OAuth failure!");
1278 #endif /* OAUTH_SUPPORT */
1282 * In netsec_set_sasl_params, we've already done all of our setup with
1283 * sasl_client_init() and sasl_client_new(). So time to set security
1284 * properties, call sasl_client_start(), and generate the protocol
1289 secprops
.maxbufsize
= SASL_MAXRECVBUF
;
1292 * If we're using TLS, do not negotiate a security layer
1297 nsc
->tls_active
? 0 :
1298 #endif /* TLS_SUPPORT */
1301 rc
= sasl_setprop(nsc
->sasl_conn
, SASL_SEC_PROPS
, &secprops
);
1303 if (rc
!= SASL_OK
) {
1304 netsec_err(errstr
, "SASL security property initialization failed: %s",
1305 sasl_errstring(rc
, NULL
, NULL
));
1310 * Start the actual protocol negotiation, and go through the
1311 * sasl_client_step() loop (after sasl_client_start, of course).
1314 rc
= sasl_client_start(nsc
->sasl_conn
,
1315 nsc
->sasl_mech
? nsc
->sasl_mech
: mechlist
, NULL
,
1316 (const char **) &saslbuf
, &saslbuflen
,
1319 if (rc
!= SASL_OK
&& rc
!= SASL_CONTINUE
) {
1320 netsec_err(errstr
, "SASL client start failed: %s",
1321 sasl_errdetail(nsc
->sasl_conn
));
1325 nsc
->sasl_chosen_mech
= getcpy(chosen_mech
);
1327 if (nsc
->sasl_proto_cb(NETSEC_SASL_START
, saslbuf
, saslbuflen
, NULL
, 0,
1328 nsc
->sasl_proto_context
, errstr
) != OK
)
1332 * We've written out our first message; enter in the step loop
1335 while (rc
== SASL_CONTINUE
) {
1337 * Call our SASL callback, which will handle the details of
1338 * reading data from the network.
1341 if (nsc
->sasl_proto_cb(NETSEC_SASL_READ
, NULL
, 0, &outbuf
, &outbuflen
,
1342 nsc
->sasl_proto_context
, errstr
) != OK
) {
1343 nsc
->sasl_proto_cb(NETSEC_SASL_CANCEL
, NULL
, 0, NULL
, 0,
1344 nsc
->sasl_proto_context
, NULL
);
1348 rc
= sasl_client_step(nsc
->sasl_conn
, (char *) outbuf
, outbuflen
, NULL
,
1349 (const char **) &saslbuf
, &saslbuflen
);
1353 if (rc
!= SASL_OK
&& rc
!= SASL_CONTINUE
) {
1354 netsec_err(errstr
, "SASL client negotiation failed: %s",
1355 sasl_errdetail(nsc
->sasl_conn
));
1356 nsc
->sasl_proto_cb(NETSEC_SASL_CANCEL
, NULL
, 0, NULL
, 0,
1357 nsc
->sasl_proto_context
, NULL
);
1361 if (nsc
->sasl_proto_cb(NETSEC_SASL_WRITE
, saslbuf
, saslbuflen
,
1362 NULL
, 0, nsc
->sasl_proto_context
,
1364 nsc
->sasl_proto_cb(NETSEC_SASL_CANCEL
, NULL
, 0, NULL
, 0,
1365 nsc
->sasl_proto_context
, NULL
);
1371 * SASL exchanges should be complete, process the final response message
1375 if (nsc
->sasl_proto_cb(NETSEC_SASL_FINISH
, NULL
, 0, NULL
, 0,
1376 nsc
->sasl_proto_context
, errstr
) != OK
) {
1378 * At this point we can't really send an abort since the SASL dialog
1379 * has completed, so just bubble back up the error message.
1386 * At this point, SASL should be complete. Get a few properties
1387 * from the authentication exchange.
1390 rc
= sasl_getprop(nsc
->sasl_conn
, SASL_SSF
, (const void **) &ssf
);
1392 if (rc
!= SASL_OK
) {
1393 netsec_err(errstr
, "Cannot retrieve SASL negotiated security "
1394 "strength factor: %s", sasl_errstring(rc
, NULL
, NULL
));
1398 nsc
->sasl_ssf
= *ssf
;
1400 if (nsc
->sasl_ssf
> 0) {
1401 rc
= sasl_getprop(nsc
->sasl_conn
, SASL_MAXOUTBUF
,
1402 (const void **) &outbufmax
);
1404 if (rc
!= SASL_OK
) {
1405 netsec_err(errstr
, "Cannot retrieve SASL negotiated output "
1406 "buffer size: %s", sasl_errstring(rc
, NULL
, NULL
));
1411 * If our output buffer isn't the same size as the input buffer,
1412 * reallocate it and set the new size (since we won't encode any
1413 * data larger than that).
1416 nsc
->sasl_maxbufsize
= *outbufmax
;
1418 if (nsc
->ns_outbufsize
!= nsc
->sasl_maxbufsize
) {
1419 nsc
->ns_outbufsize
= nsc
->sasl_maxbufsize
;
1420 nsc
->ns_outbuffer
= mh_xrealloc(nsc
->ns_outbuffer
,
1421 nsc
->ns_outbufsize
);
1423 * There shouldn't be any data in the buffer, but for
1424 * consistency's sake discard it.
1426 nsc
->ns_outptr
= nsc
->ns_outbuffer
;
1427 nsc
->ns_outbuflen
= 0;
1431 * Allocate a buffer to do temporary reads into, before we
1432 * call sasl_decode()
1435 nsc
->sasl_tmpbuf
= mh_xmalloc(nsc
->sasl_maxbufsize
);
1438 * Okay, this is a bit weird. Make sure that the input buffer
1439 * is at least TWICE the size of the max buffer size. That's
1440 * because if we're consuming data but want to extend the current
1441 * buffer, we want to be sure there's room for another full buffer's
1445 if (nsc
->ns_inbufsize
< nsc
->sasl_maxbufsize
* 2) {
1446 size_t offset
= nsc
->ns_inptr
- nsc
->ns_inbuffer
;
1447 nsc
->ns_inbufsize
= nsc
->sasl_maxbufsize
* 2;
1448 nsc
->ns_inbuffer
= mh_xrealloc(nsc
->ns_inbuffer
, nsc
->ns_inbufsize
);
1449 nsc
->ns_inptr
= nsc
->ns_inbuffer
+ offset
;
1452 nsc
->sasl_seclayer
= 1;
1458 * If we're at this point, then either we have NEITHER OAuth2 or
1459 * Cyrus-SASL compiled in, or have OAuth2 but didn't give the XOAUTH2
1460 * mechanism on the command line.
1463 if (! nsc
->sasl_mech
)
1464 netsec_err(errstr
, "SASL library support not available; please "
1465 "specify a SASL mechanism to use");
1467 netsec_err(errstr
, "No support for the %s SASL mechanism",
1471 #endif /* CYRUS_SASL */
1475 * Retrieve our chosen SASL mechanism
1479 netsec_get_sasl_mechanism(netsec_context
*nsc
)
1481 return nsc
->sasl_chosen_mech
;
1485 * Return the negotiated SASL strength security factor (SSF)
1489 netsec_get_sasl_ssf(netsec_context
*nsc
)
1492 return nsc
->sasl_ssf
;
1493 #else /* CYRUS_SASL */
1495 #endif /* CYRUS_SASL */
1499 * Set an OAuth2 service name, if we support it.
1503 netsec_set_oauth_service(netsec_context
*nsc
, const char *service
)
1505 #ifdef OAUTH_SUPPORT
1506 nsc
->oauth_service
= getcpy(service
);
1508 #else /* OAUTH_SUPPORT */
1510 NMH_UNUSED(service
);
1512 #endif /* OAUTH_SUPPORT */
1516 * Initialize (and enable) TLS for this connection
1520 netsec_set_tls(netsec_context
*nsc
, int tls
, int noverify
, char **errstr
)
1525 BIO
*rbio
, *wbio
, *ssl_bio
;
1527 if (! tls_initialized
) {
1529 SSL_load_error_strings();
1532 * Create the SSL context; this has the properties for all
1533 * SSL connections (we are only doing one), though. Make sure
1534 * we only support secure (known as of now) TLS protocols.
1537 sslctx
= SSL_CTX_new(SSLv23_client_method());
1540 netsec_err(errstr
, "Unable to initialize OpenSSL context: %s",
1541 ERR_error_string(ERR_get_error(), NULL
));
1545 SSL_CTX_set_options(sslctx
, SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1548 if (!SSL_CTX_set_default_verify_paths(sslctx
)) {
1549 netsec_err(errstr
, "Unable to set default certificate "
1550 "verification paths: %s",
1551 ERR_error_string(ERR_get_error(), NULL
));
1555 tls_initialized
= true;
1558 if (nsc
->ns_readfd
== -1 || nsc
->ns_writefd
== -1) {
1559 netsec_err(errstr
, "Invalid file descriptor in netsec context");
1564 * Create the SSL structure which holds the data for a single
1568 ssl
= SSL_new(sslctx
);
1571 netsec_err(errstr
, "Unable to create SSL connection: %s",
1572 ERR_error_string(ERR_get_error(), NULL
));
1577 * Never bother us, since we are using blocking sockets.
1580 SSL_set_mode(ssl
, SSL_MODE_AUTO_RETRY
);
1583 * This is a bit weird, so pay attention.
1585 * We create a socket BIO, and bind it to our SSL connection.
1586 * That means reads and writes to the SSL connection will use our
1589 * Then we create an SSL BIO, and assign our current SSL connection
1590 * to it. This is done so our code stays simple if we want to use
1591 * any buffering BIOs (right now we do our own buffering).
1592 * So the chain looks like:
1594 * SSL BIO -> socket BIO.
1597 rbio
= BIO_new_socket(nsc
->ns_readfd
, BIO_CLOSE
);
1600 netsec_err(errstr
, "Unable to create a read socket BIO: %s",
1601 ERR_error_string(ERR_get_error(), NULL
));
1606 wbio
= BIO_new_socket(nsc
->ns_writefd
, BIO_CLOSE
);
1609 netsec_err(errstr
, "Unable to create a write socket BIO: %s",
1610 ERR_error_string(ERR_get_error(), NULL
));
1616 SSL_set_bio(ssl
, rbio
, wbio
);
1617 SSL_set_connect_state(ssl
);
1620 * If noverify is NOT set, then do certificate validation.
1621 * Turning on SSL_VERIFY_PEER will verify the certificate chain
1622 * against locally stored root certificates (the locations are
1623 * set using SSL_CTX_set_default_verify_paths()), and we put
1624 * the hostname in the X509 verification parameters so the OpenSSL
1625 * code will verify that the hostname appears in the server
1630 #ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
1631 X509_VERIFY_PARAM
*param
;
1632 #endif /* HAVE_X509_VERIFY_PARAM_SET1_HOST */
1634 SSL_set_verify(ssl
, SSL_VERIFY_PEER
, NULL
);
1635 if (! nsc
->ns_hostname
) {
1636 netsec_err(errstr
, "Internal error: hostname not set and "
1637 "certification verification enabled");
1642 #ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
1643 param
= SSL_get0_param(ssl
);
1645 if (! X509_VERIFY_PARAM_set1_host(param
, nsc
->ns_hostname
, 0)) {
1646 netsec_err(errstr
, "Unable to add hostname %s to cert "
1647 "verification parameters: %s", nsc
->ns_hostname
,
1648 ERR_error_string(ERR_get_error(), NULL
));
1652 #endif /* HAVE_X509_VERIFY_PARAM_SET1_HOST */
1655 ssl_bio
= BIO_new(BIO_f_ssl());
1658 netsec_err(errstr
, "Unable to create a SSL BIO: %s",
1659 ERR_error_string(ERR_get_error(), NULL
));
1664 BIO_set_ssl(ssl_bio
, ssl
, BIO_CLOSE
);
1665 nsc
->ssl_io
= ssl_bio
;
1668 * Since SSL now owns these file descriptors, have it handle the
1669 * closing of them instead of netsec_shutdown().
1672 nsc
->ns_noclose
= 1;
1676 BIO_free_all(nsc
->ssl_io
);
1679 #else /* TLS_SUPPORT */
1681 NMH_UNUSED(noverify
);
1684 netsec_err(errstr
, "TLS is not supported");
1687 #endif /* TLS_SUPPORT */
1693 * Start TLS negotiation on this connection
1697 netsec_negotiate_tls(netsec_context
*nsc
, char **errstr
)
1700 if (! nsc
->ssl_io
) {
1701 netsec_err(errstr
, "TLS has not been configured for this connection");
1705 if (BIO_do_handshake(nsc
->ssl_io
) < 1) {
1706 unsigned long errcode
= ERR_get_error();
1709 * Print a more detailed message if it was certificate verification
1713 if (ERR_GET_LIB(errcode
) == ERR_LIB_SSL
&&
1714 ERR_GET_REASON(errcode
) == SSL_R_CERTIFICATE_VERIFY_FAILED
) {
1717 if (BIO_get_ssl(nsc
->ssl_io
, &ssl
) < 1) {
1718 netsec_err(errstr
, "Certificate verification failed, but "
1719 "cannot retrieve SSL handle: %s",
1720 ERR_error_string(ERR_get_error(), NULL
));
1722 netsec_err(errstr
, "Server certificate verification failed: %s",
1723 X509_verify_cert_error_string(
1724 SSL_get_verify_result(ssl
)));
1727 netsec_err(errstr
, "TLS negotiation failed: %s",
1728 ERR_error_string(errcode
, NULL
));
1732 * Because negotiation failed, shut down TLS so we don't get any
1733 * garbage on the connection. Because of weirdness with SSL_shutdown,
1734 * we end up calling it twice: once explicitly, once as part of
1738 BIO_ssl_shutdown(nsc
->ssl_io
);
1739 BIO_free_all(nsc
->ssl_io
);
1745 if (nsc
->ns_snoop
) {
1748 if (BIO_get_ssl(nsc
->ssl_io
, &ssl
) < 1) {
1749 fprintf(stderr
, "WARNING: cannot determine SSL ciphers\n");
1751 const SSL_CIPHER
*cipher
= SSL_get_current_cipher(ssl
);
1752 fprintf(stderr
, "TLS negotiation successful: %s(%d) %s\n",
1753 SSL_CIPHER_get_name(cipher
),
1754 SSL_CIPHER_get_bits(cipher
, NULL
),
1755 SSL_CIPHER_get_version(cipher
));
1756 SSL_SESSION_print_fp(stderr
, SSL_get_session(ssl
));
1760 nsc
->tls_active
= 1;
1763 #else /* TLS_SUPPORT */
1765 netsec_err(errstr
, "TLS not supported");
1768 #endif /* TLS_SUPPORT */
1772 * Generate an (allocated) error string
1776 netsec_err(char **errstr
, const char *fmt
, ...)
1780 char *errbuf
= NULL
;
1787 errbufsize
= rc
+ 1;
1788 errbuf
= mh_xrealloc(errbuf
, errbufsize
);
1790 rc
= vsnprintf(errbuf
, errbufsize
, fmt
, ap
);
1792 } while (rc
>= (int) errbufsize
);