bool{} is false for empty strings
[exim.git] / src / src / tls.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
059ec3d9
PH
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
9based on a patch that was originally contributed by Steve Haslam. It was
10adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is
11based on a patch contributed by Nikos Mavroyanopoulos. Because these packages
12are so very different, the functions for each are kept in separate files. The
13relevant file is #included as required, after any any common functions.
14
15No cryptographic code is included in Exim. All this module does is to call
16functions 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
22build-time configuration. However, some compilers don't like compiling empty
23modules, so keep them happy with a dummy when skipping the rest. Make it
24reference itself to stop picky compilers complaining that it is unused, and put
25in a dummy argument to stop even pickier compilers complaining about infinite
26loops. */
27
28#ifndef SUPPORT_TLS
29static void dummy(int x) { dummy(x-1); }
30#else
31
32/* Static variables that are used for buffering data by both sets of
33functions and the common functions below. */
34
35
36static uschar *ssl_xfer_buffer = NULL;
37static int ssl_xfer_buffer_size = 4096;
38static int ssl_xfer_buffer_lwm = 0;
39static int ssl_xfer_buffer_hwm = 0;
40static int ssl_xfer_eof = 0;
41static 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.
50Other failures return FALSE. For a server, an SMTP response is given.
51
52Arguments:
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
57Returns: TRUE if OK; result may still be NULL after forced failure
58*/
59
60static BOOL
61expand_check(uschar *s, uschar *name, uschar **result)
62{
63if (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 }
73return 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
94called once.
95
96Arguments:
97 ch the character
98
99Returns: the character
100*/
101
102int
103tls_ungetc(int ch)
104{
105ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch;
106return ch;
107}
108
109
110
111/*************************************************
112* TLS version of feof *
113*************************************************/
114
115/* Tests for a previous EOF
116
117Arguments: none
118Returns: non-zero if the eof flag is set
119*/
120
121int
122tls_feof(void)
123{
124return 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
134restored to what it was when the error was detected.
135
136>>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>>
137
138Arguments: none
139Returns: non-zero if the error flag is set
140*/
141
142int
143tls_ferror(void)
144{
145return ssl_xfer_error;
146}
147
58eb016e
PH
148
149/*************************************************
150* TLS version of smtp_buffered *
151*************************************************/
152
153/* Tests for unused chars in the TLS input buffer.
154
155Arguments: none
156Returns: TRUE/FALSE
157*/
158
159BOOL
160tls_smtp_buffered(void)
161{
162return ssl_xfer_buffer_lwm < ssl_xfer_buffer_hwm;
163}
164
165
059ec3d9
PH
166#endif /* SUPPORT_TLS */
167
168/* End of tls.c */