Use C99 initialisations for iterators
[exim.git] / src / src / auths / plaintext.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
f9ba5e22 5/* Copyright (c) University of Cambridge 1995 - 2018 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "plaintext.h"
10
11
12/* Options specific to the plaintext authentication mechanism. */
13
14optionlist auth_plaintext_options[] = {
4730f942
PH
15 { "client_ignore_invalid_base64", opt_bool,
16 (void *)(offsetof(auth_plaintext_options_block, client_ignore_invalid_base64)) },
0756eb3c
PH
17 { "client_send", opt_stringptr,
18 (void *)(offsetof(auth_plaintext_options_block, client_send)) },
0756eb3c
PH
19 { "server_prompts", opt_stringptr,
20 (void *)(offsetof(auth_plaintext_options_block, server_prompts)) }
21};
22
23/* Size of the options list. An extern variable has to be used so that its
24address can appear in the tables drtables.c. */
25
26int auth_plaintext_options_count =
27 sizeof(auth_plaintext_options)/sizeof(optionlist);
28
29/* Default private options block for the plaintext authentication method. */
30
31auth_plaintext_options_block auth_plaintext_option_defaults = {
0756eb3c 32 NULL, /* server_prompts */
4730f942
PH
33 NULL, /* client_send */
34 FALSE /* client_ignore_invalid_base64 */
0756eb3c
PH
35};
36
37
d185889f
JH
38#ifdef MACRO_PREDEF
39
40/* Dummy values */
41void auth_plaintext_init(auth_instance *ablock) {}
42int auth_plaintext_server(auth_instance *ablock, uschar *data) {return 0;}
251b9eb4
JH
43int auth_plaintext_client(auth_instance *ablock, void * sx, int timeout,
44 uschar *buffer, int buffsize) {return 0;}
d185889f
JH
45
46#else /*!MACRO_PREDEF*/
47
48
49
0756eb3c
PH
50/*************************************************
51* Initialization entry point *
52*************************************************/
53
54/* Called for each instance, after its options have been read, to
55enable consistency checks to be done, or anything else that needs
56to be set up. */
57
58void
59auth_plaintext_init(auth_instance *ablock)
60{
61auth_plaintext_options_block *ob =
62 (auth_plaintext_options_block *)(ablock->options_block);
63if (ablock->public_name == NULL) ablock->public_name = ablock->name;
16ff981e 64if (ablock->server_condition != NULL) ablock->server = TRUE;
0756eb3c
PH
65if (ob->client_send != NULL) ablock->client = TRUE;
66}
67
68
69
70/*************************************************
71* Server entry point *
72*************************************************/
73
74/* For interface, see auths/README */
75
76int
77auth_plaintext_server(auth_instance *ablock, uschar *data)
78{
79auth_plaintext_options_block *ob =
80 (auth_plaintext_options_block *)(ablock->options_block);
55414b25 81const uschar *prompts = ob->server_prompts;
16ff981e 82uschar *clear, *end, *s;
0756eb3c
PH
83int number = 1;
84int len, rc;
85int sep = 0;
86
87/* Expand a non-empty list of prompt strings */
88
89if (prompts != NULL)
90 {
55414b25 91 prompts = expand_cstring(prompts);
0756eb3c
PH
92 if (prompts == NULL)
93 {
94 auth_defer_msg = expand_string_message;
95 return DEFER;
96 }
97 }
98
99/* If data was supplied on the AUTH command, decode it, and split it up into
f78eb7c6
PH
100multiple items at binary zeros. The strings are put into $auth1, $auth2, etc,
101up to a maximum. To retain backwards compatibility, they are also put int $1,
102$2, etc. If the data consists of the string "=" it indicates a single, empty
103string. */
0756eb3c
PH
104
105if (*data != 0)
106 {
107 if (Ustrcmp(data, "=") == 0)
108 {
f78eb7c6 109 auth_vars[0] = expand_nstring[++expand_nmax] = US"";
0756eb3c
PH
110 expand_nlength[expand_nmax] = 0;
111 }
112 else
113 {
f4d091fb 114 if ((len = b64decode(data, &clear)) < 0) return BAD64;
0756eb3c
PH
115 end = clear + len;
116 while (clear < end && expand_nmax < EXPAND_MAXN)
117 {
f78eb7c6 118 if (expand_nmax < AUTH_VARS) auth_vars[expand_nmax] = clear;
0756eb3c
PH
119 expand_nstring[++expand_nmax] = clear;
120 while (*clear != 0) clear++;
121 expand_nlength[expand_nmax] = clear++ - expand_nstring[expand_nmax];
122 }
123 }
124 }
125
126/* Now go through the list of prompt strings. Skip over any whose data has
127already been provided as part of the AUTH command. For the rest, send them
128out as prompts, and get a data item back. If the data item is "*", abandon the
129authentication attempt. Otherwise, split it into items as above. */
130
131while ((s = string_nextinlist(&prompts, &sep, big_buffer, big_buffer_size))
132 != NULL && expand_nmax < EXPAND_MAXN)
133 {
134 if (number++ <= expand_nmax) continue;
135 if ((rc = auth_get_data(&data, s, Ustrlen(s))) != OK) return rc;
f4d091fb 136 if ((len = b64decode(data, &clear)) < 0) return BAD64;
0756eb3c
PH
137 end = clear + len;
138
139 /* This loop must run at least once, in case the length is zero */
140 do
141 {
f78eb7c6 142 if (expand_nmax < AUTH_VARS) auth_vars[expand_nmax] = clear;
0756eb3c
PH
143 expand_nstring[++expand_nmax] = clear;
144 while (*clear != 0) clear++;
145 expand_nlength[expand_nmax] = clear++ - expand_nstring[expand_nmax];
146 }
147 while (clear < end && expand_nmax < EXPAND_MAXN);
148 }
149
f78eb7c6 150/* We now have a number of items of data in $auth1, $auth2, etc (and also, for
16ff981e
PH
151compatibility, in $1, $2, etc). Authentication and authorization are handled
152together for this authenticator by expanding the server_condition option. Note
153that ablock->server_condition is always non-NULL because that's what configures
154this authenticator as a server. */
0756eb3c 155
16ff981e 156return auth_check_serv_cond(ablock);
0756eb3c
PH
157}
158
159
160
161/*************************************************
162* Client entry point *
163*************************************************/
164
165/* For interface, see auths/README */
166
167int
168auth_plaintext_client(
169 auth_instance *ablock, /* authenticator block */
251b9eb4 170 void * sx, /* smtp connextion */
0756eb3c
PH
171 int timeout, /* command timeout */
172 uschar *buffer, /* buffer for reading response */
173 int buffsize) /* size of buffer */
174{
175auth_plaintext_options_block *ob =
176 (auth_plaintext_options_block *)(ablock->options_block);
55414b25 177const uschar *text = ob->client_send;
0756eb3c
PH
178uschar *s;
179BOOL first = TRUE;
180int sep = 0;
4730f942 181int auth_var_idx = 0;
0756eb3c
PH
182
183/* The text is broken up into a number of different data items, which are
184sent one by one. The first one is sent with the AUTH command; the remainder are
185sent in response to subsequent prompts. Each is expanded before being sent. */
186
4e910c01 187while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)))
0756eb3c 188 {
db3f7b69 189 int len, clear_len;
0756eb3c 190 uschar *ss = expand_string(s);
4730f942 191 uschar *clear;
0756eb3c
PH
192
193 /* Forced expansion failure is not an error; authentication is abandoned. On
194 all but the first string, we have to abandon the authentication attempt by
195 sending a line containing "*". Save the failed expansion string, because it
196 is in big_buffer, and that gets used by the sending function. */
197
4e910c01 198 if (!ss)
0756eb3c
PH
199 {
200 uschar *ssave = string_copy(s);
201 if (!first)
202 {
251b9eb4
JH
203 if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
204 (void) smtp_read_response(sx, US buffer, buffsize, '2', timeout);
0756eb3c 205 }
8768d548 206 if (f.expand_string_forcedfail)
4730f942
PH
207 {
208 *buffer = 0; /* No message */
209 return CANCELLED;
210 }
0756eb3c
PH
211 string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
212 "authenticator: %s", ssave, ablock->name, expand_string_message);
213 return ERROR;
214 }
215
216 len = Ustrlen(ss);
217
218 /* The character ^ is used as an escape for a binary zero character, which is
219 needed for the PLAIN mechanism. It must be doubled if really needed. */
220
db3f7b69 221 for (int i = 0; i < len; i++)
0756eb3c 222 if (ss[i] == '^')
4e910c01
JH
223 if (ss[i+1] != '^')
224 ss[i] = 0;
225 else
0756eb3c
PH
226 {
227 i++;
228 len--;
229 memmove(ss + i, ss + i + 1, len - i);
230 }
0756eb3c
PH
231
232 /* The first string is attached to the AUTH command; others are sent
4c04137d 233 unembellished. */
0756eb3c
PH
234
235 if (first)
236 {
237 first = FALSE;
251b9eb4
JH
238 if (smtp_write_command(sx, SCMD_FLUSH, "AUTH %s%s%s\r\n",
239 ablock->public_name, len == 0 ? "" : " ", b64encode(ss, len)) < 0)
0756eb3c
PH
240 return FAIL_SEND;
241 }
242 else
243 {
251b9eb4 244 if (smtp_write_command(sx, SCMD_FLUSH, "%s\r\n", b64encode(ss, len)) < 0)
0756eb3c
PH
245 return FAIL_SEND;
246 }
247
248 /* If we receive a success response from the server, authentication
249 has succeeded. There may be more data to send, but is there any point
250 in provoking an error here? */
251
251b9eb4 252 if (smtp_read_response(sx, US buffer, buffsize, '2', timeout)) return OK;
0756eb3c
PH
253
254 /* Not a success response. If errno != 0 there is some kind of transmission
255 error. Otherwise, check the response code in the buffer. If it starts with
256 '3', more data is expected. */
257
258 if (errno != 0 || buffer[0] != '3') return FAIL;
259
260 /* If there is no more data to send, we have to cancel the authentication
261 exchange and return ERROR. */
262
251b9eb4 263 if (!text)
0756eb3c 264 {
251b9eb4
JH
265 if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
266 (void)smtp_read_response(sx, US buffer, buffsize, '2', timeout);
0756eb3c
PH
267 string_format(buffer, buffsize, "Too few items in client_send in %s "
268 "authenticator", ablock->name);
269 return ERROR;
270 }
4730f942
PH
271
272 /* Now that we know we'll continue, we put the received data into $auth<n>,
273 if possible. First, decode it: buffer+4 skips over the SMTP status code. */
274
f4d091fb 275 clear_len = b64decode(buffer+4, &clear);
4730f942
PH
276
277 /* If decoding failed, the default is to terminate the authentication, and
278 return FAIL, with the SMTP response still in the buffer. However, if client_
279 ignore_invalid_base64 is set, we ignore the error, and put an empty string
280 into $auth<n>. */
281
282 if (clear_len < 0)
283 {
284 uschar *save_bad = string_copy(buffer);
285 if (!ob->client_ignore_invalid_base64)
286 {
251b9eb4
JH
287 if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
288 (void)smtp_read_response(sx, US buffer, buffsize, '2', timeout);
4730f942
PH
289 string_format(buffer, buffsize, "Invalid base64 string in server "
290 "response \"%s\"", save_bad);
291 return CANCELLED;
292 }
293 clear = US"";
294 clear_len = 0;
295 }
296
297 if (auth_var_idx < AUTH_VARS)
298 auth_vars[auth_var_idx++] = string_copy(clear);
0756eb3c
PH
299 }
300
301/* Control should never actually get here. */
302
303return FAIL;
304}
305
d185889f 306#endif /*!MACRO_PREDEF*/
0756eb3c 307/* End of plaintext.c */