7cb1550f35921dbc856ffe63834e7fef7ba43c2d
[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
44
45 /*************************************************
46 *       Expand string; give error on failure     *
47 *************************************************/
48
49 /* If expansion is forced to fail, set the result NULL and return TRUE.
50 Other failures return FALSE. For a server, an SMTP response is given.
51
52 Arguments:
53   s         the string to expand; if NULL just return TRUE
54   name      name of string being expanded (for error)
55   result    where to put the result
56
57 Returns:    TRUE if OK; result may still be NULL after forced failure
58 */
59
60 static BOOL
61 expand_check(uschar *s, uschar *name, uschar **result)
62 {
63 if (s == NULL) *result = NULL; else
64   {
65   *result = expand_string(s);
66   if (*result == NULL && !expand_string_forcedfail)
67     {
68     log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name,
69       expand_string_message);
70     return FALSE;
71     }
72   }
73 return TRUE;
74 }
75
76
77 /*************************************************
78 *        Many functions are package-specific     *
79 *************************************************/
80
81 #ifdef USE_GNUTLS
82 #include "tls-gnu.c"
83 #else
84 #include "tls-openssl.c"
85 #endif
86
87
88
89 /*************************************************
90 *           TLS version of ungetc                *
91 *************************************************/
92
93 /* Puts a character back in the input buffer. Only ever
94 called once.
95
96 Arguments:
97   ch           the character
98
99 Returns:       the character
100 */
101
102 int
103 tls_ungetc(int ch)
104 {
105 ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
106 return ch;
107 }
108
109
110
111 /*************************************************
112 *           TLS version of feof                  *
113 *************************************************/
114
115 /* Tests for a previous EOF
116
117 Arguments:     none
118 Returns:       non-zero if the eof flag is set
119 */
120
121 int
122 tls_feof(void)
123 {
124 return ssl_xfer_eof;
125 }
126
127
128
129 /*************************************************
130 *              TLS version of ferror             *
131 *************************************************/
132
133 /* Tests for a previous read error, and returns with errno
134 restored to what it was when the error was detected.
135
136 >>>>> Hmm. Errno not handled yet. Where do we get it from?  >>>>>
137
138 Arguments:     none
139 Returns:       non-zero if the error flag is set
140 */
141
142 int
143 tls_ferror(void)
144 {
145 return ssl_xfer_error;
146 }
147
148
149 /*************************************************
150 *           TLS version of smtp_buffered         *
151 *************************************************/
152
153 /* Tests for unused chars in the TLS input buffer.
154
155 Arguments:     none
156 Returns:       TRUE/FALSE
157 */
158
159 BOOL
160 tls_smtp_buffered(void)
161 {
162 return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
163 }
164
165
166 #endif  /* SUPPORT_TLS */
167
168 /* End of tls.c */