sendmail: Update vendor branch to v8.14.4
[dragonfly.git] / contrib / sendmail-8.14 / sendmail / sfsasl.c
blobcad16db1686a265eafe40ad587a73e0d1a486e3d
1 /*
2 * Copyright (c) 1999-2006, 2008 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
9 */
11 #include <sm/gen.h>
12 SM_RCSID("@(#)$Id: sfsasl.c,v 8.118 2008/07/22 15:12:48 ca Exp $")
13 #include <stdlib.h>
14 #include <sendmail.h>
15 #include <sm/time.h>
16 #include <errno.h>
18 /* allow to disable error handling code just in case... */
19 #ifndef DEAL_WITH_ERROR_SSL
20 # define DEAL_WITH_ERROR_SSL 1
21 #endif /* ! DEAL_WITH_ERROR_SSL */
23 #if SASL
24 # include "sfsasl.h"
26 /* Structure used by the "sasl" file type */
27 struct sasl_obj
29 SM_FILE_T *fp;
30 sasl_conn_t *conn;
33 struct sasl_info
35 SM_FILE_T *fp;
36 sasl_conn_t *conn;
40 ** SASL_GETINFO - returns requested information about a "sasl" file
41 ** descriptor.
43 ** Parameters:
44 ** fp -- the file descriptor
45 ** what -- the type of information requested
46 ** valp -- the thang to return the information in
48 ** Returns:
49 ** -1 for unknown requests
50 ** >=0 on success with valp filled in (if possible).
53 static int sasl_getinfo __P((SM_FILE_T *, int, void *));
55 static int
56 sasl_getinfo(fp, what, valp)
57 SM_FILE_T *fp;
58 int what;
59 void *valp;
61 struct sasl_obj *so = (struct sasl_obj *) fp->f_cookie;
63 switch (what)
65 case SM_IO_WHAT_FD:
66 if (so->fp == NULL)
67 return -1;
68 return so->fp->f_file; /* for stdio fileno() compatability */
70 case SM_IO_IS_READABLE:
71 if (so->fp == NULL)
72 return 0;
74 /* get info from underlying file */
75 return sm_io_getinfo(so->fp, what, valp);
77 default:
78 return -1;
83 ** SASL_OPEN -- creates the sasl specific information for opening a
84 ** file of the sasl type.
86 ** Parameters:
87 ** fp -- the file pointer associated with the new open
88 ** info -- contains the sasl connection information pointer and
89 ** the original SM_FILE_T that holds the open
90 ** flags -- ignored
91 ** rpool -- ignored
93 ** Returns:
94 ** 0 on success
97 static int sasl_open __P((SM_FILE_T *, const void *, int, const void *));
99 /* ARGSUSED2 */
100 static int
101 sasl_open(fp, info, flags, rpool)
102 SM_FILE_T *fp;
103 const void *info;
104 int flags;
105 const void *rpool;
107 struct sasl_obj *so;
108 struct sasl_info *si = (struct sasl_info *) info;
110 so = (struct sasl_obj *) sm_malloc(sizeof(struct sasl_obj));
111 if (so == NULL)
113 errno = ENOMEM;
114 return -1;
116 so->fp = si->fp;
117 so->conn = si->conn;
120 ** The underlying 'fp' is set to SM_IO_NOW so that the entire
121 ** encoded string is written in one chunk. Otherwise there is
122 ** the possibility that it may appear illegal, bogus or
123 ** mangled to the other side of the connection.
124 ** We will read or write through 'fp' since it is the opaque
125 ** connection for the communications. We need to treat it this
126 ** way in case the encoded string is to be sent down a TLS
127 ** connection rather than, say, sm_io's stdio.
130 (void) sm_io_setvbuf(so->fp, SM_TIME_DEFAULT, NULL, SM_IO_NOW, 0);
131 fp->f_cookie = so;
132 return 0;
136 ** SASL_CLOSE -- close the sasl specific parts of the sasl file pointer
138 ** Parameters:
139 ** fp -- the file pointer to close
141 ** Returns:
142 ** 0 on success
145 static int sasl_close __P((SM_FILE_T *));
147 static int
148 sasl_close(fp)
149 SM_FILE_T *fp;
151 struct sasl_obj *so;
153 so = (struct sasl_obj *) fp->f_cookie;
154 if (so == NULL)
155 return 0;
156 if (so->fp != NULL)
158 sm_io_close(so->fp, SM_TIME_DEFAULT);
159 so->fp = NULL;
161 sm_free(so);
162 so = NULL;
163 return 0;
166 /* how to deallocate a buffer allocated by SASL */
167 extern void sm_sasl_free __P((void *));
168 # define SASL_DEALLOC(b) sm_sasl_free(b)
171 ** SASL_READ -- read encrypted information and decrypt it for the caller
173 ** Parameters:
174 ** fp -- the file pointer
175 ** buf -- the location to place the decrypted information
176 ** size -- the number of bytes to read after decryption
178 ** Results:
179 ** -1 on error
180 ** otherwise the number of bytes read
183 static ssize_t sasl_read __P((SM_FILE_T *, char *, size_t));
185 static ssize_t
186 sasl_read(fp, buf, size)
187 SM_FILE_T *fp;
188 char *buf;
189 size_t size;
191 int result;
192 ssize_t len;
193 # if SASL >= 20000
194 static const char *outbuf = NULL;
195 # else /* SASL >= 20000 */
196 static char *outbuf = NULL;
197 # endif /* SASL >= 20000 */
198 static unsigned int outlen = 0;
199 static unsigned int offset = 0;
200 struct sasl_obj *so = (struct sasl_obj *) fp->f_cookie;
203 ** sasl_decode() may require more data than a single read() returns.
204 ** Hence we have to put a loop around the decoding.
205 ** This also requires that we may have to split up the returned
206 ** data since it might be larger than the allowed size.
207 ** Therefore we use a static pointer and return portions of it
208 ** if necessary.
209 ** XXX Note: This function is not thread-safe nor can it be used
210 ** on more than one file. A correct implementation would store
211 ** this data in fp->f_cookie.
214 # if SASL >= 20000
215 while (outlen == 0)
216 # else /* SASL >= 20000 */
217 while (outbuf == NULL && outlen == 0)
218 # endif /* SASL >= 20000 */
220 len = sm_io_read(so->fp, SM_TIME_DEFAULT, buf, size);
221 if (len <= 0)
222 return len;
223 result = sasl_decode(so->conn, buf,
224 (unsigned int) len, &outbuf, &outlen);
225 if (result != SASL_OK)
227 if (LogLevel > 7)
228 sm_syslog(LOG_WARNING, NOQID,
229 "AUTH: sasl_decode error=%d", result);
230 outbuf = NULL;
231 offset = 0;
232 outlen = 0;
233 return -1;
237 if (outbuf == NULL)
239 /* be paranoid: outbuf == NULL but outlen != 0 */
240 syserr("@sasl_read failure: outbuf == NULL but outlen != 0");
241 /* NOTREACHED */
243 if (outlen - offset > size)
245 /* return another part of the buffer */
246 (void) memcpy(buf, outbuf + offset, size);
247 offset += size;
248 len = size;
250 else
252 /* return the rest of the buffer */
253 len = outlen - offset;
254 (void) memcpy(buf, outbuf + offset, (size_t) len);
255 # if SASL < 20000
256 SASL_DEALLOC(outbuf);
257 # endif /* SASL < 20000 */
258 outbuf = NULL;
259 offset = 0;
260 outlen = 0;
262 return len;
266 ** SASL_WRITE -- write information out after encrypting it
268 ** Parameters:
269 ** fp -- the file pointer
270 ** buf -- holds the data to be encrypted and written
271 ** size -- the number of bytes to have encrypted and written
273 ** Returns:
274 ** -1 on error
275 ** otherwise number of bytes written
278 static ssize_t sasl_write __P((SM_FILE_T *, const char *, size_t));
280 static ssize_t
281 sasl_write(fp, buf, size)
282 SM_FILE_T *fp;
283 const char *buf;
284 size_t size;
286 int result;
287 # if SASL >= 20000
288 const char *outbuf;
289 # else /* SASL >= 20000 */
290 char *outbuf;
291 # endif /* SASL >= 20000 */
292 unsigned int outlen, *maxencode;
293 size_t ret = 0, total = 0;
294 struct sasl_obj *so = (struct sasl_obj *) fp->f_cookie;
297 ** Fetch the maximum input buffer size for sasl_encode().
298 ** This can be less than the size set in attemptauth()
299 ** due to a negotiation with the other side, e.g.,
300 ** Cyrus IMAP lmtp program sets maxbuf=4096,
301 ** digestmd5 substracts 25 and hence we'll get 4071
302 ** instead of 8192 (MAXOUTLEN).
303 ** Hack (for now): simply reduce the size, callers are (must be)
304 ** able to deal with that and invoke sasl_write() again with
305 ** the rest of the data.
306 ** Note: it would be better to store this value in the context
307 ** after the negotiation.
310 result = sasl_getprop(so->conn, SASL_MAXOUTBUF,
311 (const void **) &maxencode);
312 if (result == SASL_OK && size > *maxencode && *maxencode > 0)
313 size = *maxencode;
315 result = sasl_encode(so->conn, buf,
316 (unsigned int) size, &outbuf, &outlen);
318 if (result != SASL_OK)
320 if (LogLevel > 7)
321 sm_syslog(LOG_WARNING, NOQID,
322 "AUTH: sasl_encode error=%d", result);
323 return -1;
326 if (outbuf != NULL)
328 while (outlen > 0)
330 errno = 0;
331 /* XXX result == 0? */
332 ret = sm_io_write(so->fp, SM_TIME_DEFAULT,
333 &outbuf[total], outlen);
334 if (ret <= 0)
335 return ret;
336 outlen -= ret;
337 total += ret;
339 # if SASL < 20000
340 SASL_DEALLOC(outbuf);
341 # endif /* SASL < 20000 */
343 return size;
347 ** SFDCSASL -- create sasl file type and open in and out file pointers
348 ** for sendmail to read from and write to.
350 ** Parameters:
351 ** fin -- the sm_io file encrypted data to be read from
352 ** fout -- the sm_io file encrypted data to be written to
353 ** conn -- the sasl connection pointer
354 ** tmo -- timeout
356 ** Returns:
357 ** -1 on error
358 ** 0 on success
360 ** Side effects:
361 ** The arguments "fin" and "fout" are replaced with the new
362 ** SM_FILE_T pointers.
366 sfdcsasl(fin, fout, conn, tmo)
367 SM_FILE_T **fin;
368 SM_FILE_T **fout;
369 sasl_conn_t *conn;
370 int tmo;
372 SM_FILE_T *newin, *newout;
373 SM_FILE_T SM_IO_SET_TYPE(sasl_vector, "sasl", sasl_open, sasl_close,
374 sasl_read, sasl_write, NULL, sasl_getinfo, NULL,
375 SM_TIME_DEFAULT);
376 struct sasl_info info;
378 if (conn == NULL)
380 /* no need to do anything */
381 return 0;
384 SM_IO_INIT_TYPE(sasl_vector, "sasl", sasl_open, sasl_close,
385 sasl_read, sasl_write, NULL, sasl_getinfo, NULL,
386 SM_TIME_DEFAULT);
387 info.fp = *fin;
388 info.conn = conn;
389 newin = sm_io_open(&sasl_vector, SM_TIME_DEFAULT, &info,
390 SM_IO_RDONLY_B, NULL);
392 if (newin == NULL)
393 return -1;
395 info.fp = *fout;
396 info.conn = conn;
397 newout = sm_io_open(&sasl_vector, SM_TIME_DEFAULT, &info,
398 SM_IO_WRONLY_B, NULL);
400 if (newout == NULL)
402 (void) sm_io_close(newin, SM_TIME_DEFAULT);
403 return -1;
405 sm_io_automode(newin, newout);
407 sm_io_setinfo(*fin, SM_IO_WHAT_TIMEOUT, &tmo);
408 sm_io_setinfo(*fout, SM_IO_WHAT_TIMEOUT, &tmo);
410 *fin = newin;
411 *fout = newout;
412 return 0;
414 #endif /* SASL */
416 #if STARTTLS
417 # include "sfsasl.h"
418 # include <openssl/err.h>
420 /* Structure used by the "tls" file type */
421 struct tls_obj
423 SM_FILE_T *fp;
424 SSL *con;
427 struct tls_info
429 SM_FILE_T *fp;
430 SSL *con;
434 ** TLS_GETINFO - returns requested information about a "tls" file
435 ** descriptor.
437 ** Parameters:
438 ** fp -- the file descriptor
439 ** what -- the type of information requested
440 ** valp -- the thang to return the information in (unused)
442 ** Returns:
443 ** -1 for unknown requests
444 ** >=0 on success with valp filled in (if possible).
447 static int tls_getinfo __P((SM_FILE_T *, int, void *));
449 /* ARGSUSED2 */
450 static int
451 tls_getinfo(fp, what, valp)
452 SM_FILE_T *fp;
453 int what;
454 void *valp;
456 struct tls_obj *so = (struct tls_obj *) fp->f_cookie;
458 switch (what)
460 case SM_IO_WHAT_FD:
461 if (so->fp == NULL)
462 return -1;
463 return so->fp->f_file; /* for stdio fileno() compatability */
465 case SM_IO_IS_READABLE:
466 return SSL_pending(so->con) > 0;
468 default:
469 return -1;
474 ** TLS_OPEN -- creates the tls specific information for opening a
475 ** file of the tls type.
477 ** Parameters:
478 ** fp -- the file pointer associated with the new open
479 ** info -- the sm_io file pointer holding the open and the
480 ** TLS encryption connection to be read from or written to
481 ** flags -- ignored
482 ** rpool -- ignored
484 ** Returns:
485 ** 0 on success
488 static int tls_open __P((SM_FILE_T *, const void *, int, const void *));
490 /* ARGSUSED2 */
491 static int
492 tls_open(fp, info, flags, rpool)
493 SM_FILE_T *fp;
494 const void *info;
495 int flags;
496 const void *rpool;
498 struct tls_obj *so;
499 struct tls_info *ti = (struct tls_info *) info;
501 so = (struct tls_obj *) sm_malloc(sizeof(struct tls_obj));
502 if (so == NULL)
504 errno = ENOMEM;
505 return -1;
507 so->fp = ti->fp;
508 so->con = ti->con;
511 ** We try to get the "raw" file descriptor that TLS uses to
512 ** do the actual read/write with. This is to allow us control
513 ** over the file descriptor being a blocking or non-blocking type.
514 ** Under the covers TLS handles the change and this allows us
515 ** to do timeouts with sm_io.
518 fp->f_file = sm_io_getinfo(so->fp, SM_IO_WHAT_FD, NULL);
519 (void) sm_io_setvbuf(so->fp, SM_TIME_DEFAULT, NULL, SM_IO_NOW, 0);
520 fp->f_cookie = so;
521 return 0;
525 ** TLS_CLOSE -- close the tls specific parts of the tls file pointer
527 ** Parameters:
528 ** fp -- the file pointer to close
530 ** Returns:
531 ** 0 on success
534 static int tls_close __P((SM_FILE_T *));
536 static int
537 tls_close(fp)
538 SM_FILE_T *fp;
540 struct tls_obj *so;
542 so = (struct tls_obj *) fp->f_cookie;
543 if (so == NULL)
544 return 0;
545 if (so->fp != NULL)
547 sm_io_close(so->fp, SM_TIME_DEFAULT);
548 so->fp = NULL;
550 sm_free(so);
551 so = NULL;
552 return 0;
555 /* maximum number of retries for TLS related I/O due to handshakes */
556 # define MAX_TLS_IOS 4
559 ** TLS_RETRY -- check whether a failed SSL operation can be retried
561 ** Parameters:
562 ** ssl -- TLS structure
563 ** rfd -- read fd
564 ** wfd -- write fd
565 ** tlsstart -- start time of TLS operation
566 ** timeout -- timeout for TLS operation
567 ** err -- SSL error
568 ** where -- description of operation
570 ** Results:
571 ** >0 on success
572 ** 0 on timeout
573 ** <0 on error
577 tls_retry(ssl, rfd, wfd, tlsstart, timeout, err, where)
578 SSL *ssl;
579 int rfd;
580 int wfd;
581 time_t tlsstart;
582 int timeout;
583 int err;
584 const char *where;
586 int ret;
587 time_t left;
588 time_t now = curtime();
589 struct timeval tv;
591 ret = -1;
594 ** For SSL_ERROR_WANT_{READ,WRITE}:
595 ** There is not a complete SSL record available yet
596 ** or there is only a partial SSL record removed from
597 ** the network (socket) buffer into the SSL buffer.
598 ** The SSL_connect will only succeed when a full
599 ** SSL record is available (assuming a "real" error
600 ** doesn't happen). To handle when a "real" error
601 ** does happen the select is set for exceptions too.
602 ** The connection may be re-negotiated during this time
603 ** so both read and write "want errors" need to be handled.
604 ** A select() exception loops back so that a proper SSL
605 ** error message can be gotten.
608 left = timeout - (now - tlsstart);
609 if (left <= 0)
610 return 0; /* timeout */
611 tv.tv_sec = left;
612 tv.tv_usec = 0;
614 if (LogLevel > 14)
616 sm_syslog(LOG_INFO, NOQID,
617 "STARTTLS=%s, info: fds=%d/%d, err=%d",
618 where, rfd, wfd, err);
621 if (FD_SETSIZE > 0 &&
622 ((err == SSL_ERROR_WANT_READ && rfd >= FD_SETSIZE) ||
623 (err == SSL_ERROR_WANT_WRITE && wfd >= FD_SETSIZE)))
625 if (LogLevel > 5)
627 sm_syslog(LOG_ERR, NOQID,
628 "STARTTLS=%s, error: fd %d/%d too large",
629 where, rfd, wfd);
630 if (LogLevel > 8)
631 tlslogerr(where);
633 errno = EINVAL;
635 else if (err == SSL_ERROR_WANT_READ)
637 fd_set ssl_maskr, ssl_maskx;
639 FD_ZERO(&ssl_maskr);
640 FD_SET(rfd, &ssl_maskr);
641 FD_ZERO(&ssl_maskx);
642 FD_SET(rfd, &ssl_maskx);
645 ret = select(rfd + 1, &ssl_maskr, NULL, &ssl_maskx,
646 &tv);
647 } while (ret < 0 && errno == EINTR);
648 if (ret < 0 && errno > 0)
649 ret = -errno;
651 else if (err == SSL_ERROR_WANT_WRITE)
653 fd_set ssl_maskw, ssl_maskx;
655 FD_ZERO(&ssl_maskw);
656 FD_SET(wfd, &ssl_maskw);
657 FD_ZERO(&ssl_maskx);
658 FD_SET(rfd, &ssl_maskx);
661 ret = select(wfd + 1, NULL, &ssl_maskw, &ssl_maskx,
662 &tv);
663 } while (ret < 0 && errno == EINTR);
664 if (ret < 0 && errno > 0)
665 ret = -errno;
667 return ret;
670 /* errno to force refill() etc to stop (see IS_IO_ERROR()) */
671 #ifdef ETIMEDOUT
672 # define SM_ERR_TIMEOUT ETIMEDOUT
673 #else /* ETIMEDOUT */
674 # define SM_ERR_TIMEOUT EIO
675 #endif /* ETIMEDOUT */
678 ** SET_TLS_RD_TMO -- read secured information for the caller
680 ** Parameters:
681 ** rd_tmo -- read timeout
683 ** Results:
684 ** none
685 ** This is a hack: there is no way to pass it in
688 static int tls_rd_tmo = -1;
690 void
691 set_tls_rd_tmo(rd_tmo)
692 int rd_tmo;
694 tls_rd_tmo = rd_tmo;
698 ** TLS_READ -- read secured information for the caller
700 ** Parameters:
701 ** fp -- the file pointer
702 ** buf -- the location to place the data
703 ** size -- the number of bytes to read from connection
705 ** Results:
706 ** -1 on error
707 ** otherwise the number of bytes read
710 static ssize_t tls_read __P((SM_FILE_T *, char *, size_t));
712 static ssize_t
713 tls_read(fp, buf, size)
714 SM_FILE_T *fp;
715 char *buf;
716 size_t size;
718 int r, rfd, wfd, try, ssl_err;
719 struct tls_obj *so = (struct tls_obj *) fp->f_cookie;
720 time_t tlsstart;
721 char *err;
723 try = 99;
724 err = NULL;
725 tlsstart = curtime();
727 retry:
728 r = SSL_read(so->con, (char *) buf, size);
730 if (r > 0)
731 return r;
733 err = NULL;
734 switch (ssl_err = SSL_get_error(so->con, r))
736 case SSL_ERROR_NONE:
737 case SSL_ERROR_ZERO_RETURN:
738 break;
739 case SSL_ERROR_WANT_WRITE:
740 err = "read W BLOCK";
741 /* FALLTHROUGH */
742 case SSL_ERROR_WANT_READ:
743 if (err == NULL)
744 err = "read R BLOCK";
745 rfd = SSL_get_rfd(so->con);
746 wfd = SSL_get_wfd(so->con);
747 try = tls_retry(so->con, rfd, wfd, tlsstart,
748 (tls_rd_tmo < 0) ? TimeOuts.to_datablock
749 : tls_rd_tmo,
750 ssl_err, "read");
751 if (try > 0)
752 goto retry;
753 errno = SM_ERR_TIMEOUT;
754 break;
756 case SSL_ERROR_WANT_X509_LOOKUP:
757 err = "write X BLOCK";
758 break;
759 case SSL_ERROR_SYSCALL:
760 if (r == 0 && errno == 0) /* out of protocol EOF found */
761 break;
762 err = "syscall error";
764 get_last_socket_error());
766 break;
767 case SSL_ERROR_SSL:
768 #if DEAL_WITH_ERROR_SSL
769 if (r == 0 && errno == 0) /* out of protocol EOF found */
770 break;
771 #endif /* DEAL_WITH_ERROR_SSL */
772 err = "generic SSL error";
773 if (LogLevel > 9)
774 tlslogerr("read");
776 #if DEAL_WITH_ERROR_SSL
777 /* avoid repeated calls? */
778 if (r == 0)
779 r = -1;
780 #endif /* DEAL_WITH_ERROR_SSL */
781 break;
783 if (err != NULL)
785 int save_errno;
787 save_errno = (errno == 0) ? EIO : errno;
788 if (try == 0 && save_errno == SM_ERR_TIMEOUT)
790 if (LogLevel > 7)
791 sm_syslog(LOG_WARNING, NOQID,
792 "STARTTLS: read error=timeout");
794 else if (LogLevel > 8)
795 sm_syslog(LOG_WARNING, NOQID,
796 "STARTTLS: read error=%s (%d), errno=%d, get_error=%s, retry=%d, ssl_err=%d",
797 err, r, errno,
798 ERR_error_string(ERR_get_error(), NULL), try,
799 ssl_err);
800 else if (LogLevel > 7)
801 sm_syslog(LOG_WARNING, NOQID,
802 "STARTTLS: read error=%s (%d), retry=%d, ssl_err=%d",
803 err, r, errno, try, ssl_err);
804 errno = save_errno;
806 return r;
810 ** TLS_WRITE -- write information out through secure connection
812 ** Parameters:
813 ** fp -- the file pointer
814 ** buf -- holds the data to be securely written
815 ** size -- the number of bytes to write
817 ** Returns:
818 ** -1 on error
819 ** otherwise number of bytes written
822 static ssize_t tls_write __P((SM_FILE_T *, const char *, size_t));
824 static ssize_t
825 tls_write(fp, buf, size)
826 SM_FILE_T *fp;
827 const char *buf;
828 size_t size;
830 int r, rfd, wfd, try, ssl_err;
831 struct tls_obj *so = (struct tls_obj *) fp->f_cookie;
832 time_t tlsstart;
833 char *err;
835 try = 99;
836 err = NULL;
837 tlsstart = curtime();
839 retry:
840 r = SSL_write(so->con, (char *) buf, size);
842 if (r > 0)
843 return r;
844 err = NULL;
845 switch (ssl_err = SSL_get_error(so->con, r))
847 case SSL_ERROR_NONE:
848 case SSL_ERROR_ZERO_RETURN:
849 break;
850 case SSL_ERROR_WANT_WRITE:
851 err = "read W BLOCK";
852 /* FALLTHROUGH */
853 case SSL_ERROR_WANT_READ:
854 if (err == NULL)
855 err = "read R BLOCK";
856 rfd = SSL_get_rfd(so->con);
857 wfd = SSL_get_wfd(so->con);
858 try = tls_retry(so->con, rfd, wfd, tlsstart,
859 DATA_PROGRESS_TIMEOUT, ssl_err, "write");
860 if (try > 0)
861 goto retry;
862 errno = SM_ERR_TIMEOUT;
863 break;
864 case SSL_ERROR_WANT_X509_LOOKUP:
865 err = "write X BLOCK";
866 break;
867 case SSL_ERROR_SYSCALL:
868 if (r == 0 && errno == 0) /* out of protocol EOF found */
869 break;
870 err = "syscall error";
872 get_last_socket_error());
874 break;
875 case SSL_ERROR_SSL:
876 err = "generic SSL error";
878 ERR_GET_REASON(ERR_peek_error()));
880 if (LogLevel > 9)
881 tlslogerr("write");
883 #if DEAL_WITH_ERROR_SSL
884 /* avoid repeated calls? */
885 if (r == 0)
886 r = -1;
887 #endif /* DEAL_WITH_ERROR_SSL */
888 break;
890 if (err != NULL)
892 int save_errno;
894 save_errno = (errno == 0) ? EIO : errno;
895 if (try == 0 && save_errno == SM_ERR_TIMEOUT)
897 if (LogLevel > 7)
898 sm_syslog(LOG_WARNING, NOQID,
899 "STARTTLS: write error=timeout");
901 else if (LogLevel > 8)
902 sm_syslog(LOG_WARNING, NOQID,
903 "STARTTLS: write error=%s (%d), errno=%d, get_error=%s, retry=%d, ssl_err=%d",
904 err, r, errno,
905 ERR_error_string(ERR_get_error(), NULL), try,
906 ssl_err);
907 else if (LogLevel > 7)
908 sm_syslog(LOG_WARNING, NOQID,
909 "STARTTLS: write error=%s (%d), errno=%d, retry=%d, ssl_err=%d",
910 err, r, errno, try, ssl_err);
911 errno = save_errno;
913 return r;
917 ** SFDCTLS -- create tls file type and open in and out file pointers
918 ** for sendmail to read from and write to.
920 ** Parameters:
921 ** fin -- data input source being replaced
922 ** fout -- data output source being replaced
923 ** con -- the tls connection pointer
925 ** Returns:
926 ** -1 on error
927 ** 0 on success
929 ** Side effects:
930 ** The arguments "fin" and "fout" are replaced with the new
931 ** SM_FILE_T pointers.
932 ** The original "fin" and "fout" are preserved in the tls file
933 ** type but are not actually used because of the design of TLS.
937 sfdctls(fin, fout, con)
938 SM_FILE_T **fin;
939 SM_FILE_T **fout;
940 SSL *con;
942 SM_FILE_T *tlsin, *tlsout;
943 SM_FILE_T SM_IO_SET_TYPE(tls_vector, "tls", tls_open, tls_close,
944 tls_read, tls_write, NULL, tls_getinfo, NULL,
945 SM_TIME_FOREVER);
946 struct tls_info info;
948 SM_ASSERT(con != NULL);
950 SM_IO_INIT_TYPE(tls_vector, "tls", tls_open, tls_close,
951 tls_read, tls_write, NULL, tls_getinfo, NULL,
952 SM_TIME_FOREVER);
953 info.fp = *fin;
954 info.con = con;
955 tlsin = sm_io_open(&tls_vector, SM_TIME_DEFAULT, &info, SM_IO_RDONLY_B,
956 NULL);
957 if (tlsin == NULL)
958 return -1;
960 info.fp = *fout;
961 tlsout = sm_io_open(&tls_vector, SM_TIME_DEFAULT, &info, SM_IO_WRONLY_B,
962 NULL);
963 if (tlsout == NULL)
965 (void) sm_io_close(tlsin, SM_TIME_DEFAULT);
966 return -1;
968 sm_io_automode(tlsin, tlsout);
970 *fin = tlsin;
971 *fout = tlsout;
972 return 0;
974 #endif /* STARTTLS */