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