Unbreak build on Solaris.
[exim.git] / src / src / tls.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 /* This module provides TLS (aka SSL) support for Exim. The code for OpenSSL is
9 based on a patch that was originally contributed by Steve Haslam. It was
10 adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
11 based on a patch contributed by Nikos Mavroyanopoulos. Because these packages
12 are so very different, the functions for each are kept in separate files. The
13 relevant file is #included as required, after any any common functions.
14
15 No cryptographic code is included in Exim. All this module does is to call
16 functions from the OpenSSL or GNU TLS libraries. */
17
18
19 #include "exim.h"
20
21 /* This module is compiled only when it is specifically requested in the
22 build-time configuration. However, some compilers don't like compiling empty
23 modules, so keep them happy with a dummy when skipping the rest. Make it
24 reference itself to stop picky compilers complaining that it is unused, and put
25 in a dummy argument to stop even pickier compilers complaining about infinite
26 loops. */
27
28 #ifndef SUPPORT_TLS
29 static void dummy(int x) { dummy(x-1); }
30 #else
31
32 /* Static variables that are used for buffering data by both sets of
33 functions and the common functions below. */
34
35
36 static uschar *ssl_xfer_buffer = NULL;
37 static int ssl_xfer_buffer_size = 4096;
38 static int ssl_xfer_buffer_lwm = 0;
39 static int ssl_xfer_buffer_hwm = 0;
40 static int ssl_xfer_eof = 0;
41 static int ssl_xfer_error = 0;
42
43 uschar *tls_channelbinding_b64 = NULL;
44
45
46 /*************************************************
47 * Expand string; give error on failure *
48 *************************************************/
49
50 /* If expansion is forced to fail, set the result NULL and return TRUE.
51 Other failures return FALSE. For a server, an SMTP response is given.
52
53 Arguments:
54 s the string to expand; if NULL just return TRUE
55 name name of string being expanded (for error)
56 result where to put the result
57
58 Returns: TRUE if OK; result may still be NULL after forced failure
59 */
60
61 static BOOL
62 expand_check(uschar *s, uschar *name, uschar **result)
63 {
64 if (s == NULL) *result = NULL; else
65 {
66 *result = expand_string(s);
67 if (*result == NULL && !expand_string_forcedfail)
68 {
69 log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
70 expand_string_message);
71 return FALSE;
72 }
73 }
74 return TRUE;
75 }
76
77
78 /*************************************************
79 * Many functions are package-specific *
80 *************************************************/
81
82 #ifdef USE_GNUTLS
83 #include "tls-gnu.c"
84 #else
85 #include "tls-openssl.c"
86 #endif
87
88
89
90 /*************************************************
91 * TLS version of ungetc *
92 *************************************************/
93
94 /* Puts a character back in the input buffer. Only ever
95 called once.
96
97 Arguments:
98 ch the character
99
100 Returns: the character
101 */
102
103 int
104 tls_ungetc(int ch)
105 {
106 ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
107 return ch;
108 }
109
110
111
112 /*************************************************
113 * TLS version of feof *
114 *************************************************/
115
116 /* Tests for a previous EOF
117
118 Arguments: none
119 Returns: non-zero if the eof flag is set
120 */
121
122 int
123 tls_feof(void)
124 {
125 return ssl_xfer_eof;
126 }
127
128
129
130 /*************************************************
131 * TLS version of ferror *
132 *************************************************/
133
134 /* Tests for a previous read error, and returns with errno
135 restored to what it was when the error was detected.
136
137 >>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
138
139 Arguments: none
140 Returns: non-zero if the error flag is set
141 */
142
143 int
144 tls_ferror(void)
145 {
146 return ssl_xfer_error;
147 }
148
149
150 /*************************************************
151 * TLS version of smtp_buffered *
152 *************************************************/
153
154 /* Tests for unused chars in the TLS input buffer.
155
156 Arguments: none
157 Returns: TRUE/FALSE
158 */
159
160 BOOL
161 tls_smtp_buffered(void)
162 {
163 return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
164 }
165
166
167 #endif /* SUPPORT_TLS */
168
169 /* End of tls.c */