1 /* $Cambridge: exim/src/src/auths/plaintext.c,v 1.8 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "plaintext.h"
14 /* Options specific to the plaintext authentication mechanism. */
16 optionlist auth_plaintext_options[] = {
17 { "client_ignore_invalid_base64", opt_bool,
18 (void *)(offsetof(auth_plaintext_options_block, client_ignore_invalid_base64)) },
19 { "client_send", opt_stringptr,
20 (void *)(offsetof(auth_plaintext_options_block, client_send)) },
21 { "server_prompts", opt_stringptr,
22 (void *)(offsetof(auth_plaintext_options_block, server_prompts)) }
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. */
28 int auth_plaintext_options_count =
29 sizeof(auth_plaintext_options)/sizeof(optionlist);
31 /* Default private options block for the plaintext authentication method. */
33 auth_plaintext_options_block auth_plaintext_option_defaults = {
34 NULL, /* server_prompts */
35 NULL, /* client_send */
36 FALSE /* client_ignore_invalid_base64 */
40 /*************************************************
41 * Initialization entry point *
42 *************************************************/
44 /* Called for each instance, after its options have been read, to
45 enable consistency checks to be done, or anything else that needs
49 auth_plaintext_init(auth_instance *ablock)
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 (ablock->server_condition != NULL) ablock->server = TRUE;
55 if (ob->client_send != NULL) ablock->client = TRUE;
60 /*************************************************
61 * Server entry point *
62 *************************************************/
64 /* For interface, see auths/README */
67 auth_plaintext_server(auth_instance *ablock, uschar *data)
69 auth_plaintext_options_block *ob =
70 (auth_plaintext_options_block *)(ablock->options_block);
71 uschar *prompts = ob->server_prompts;
72 uschar *clear, *end, *s;
77 /* Expand a non-empty list of prompt strings */
81 prompts = expand_string(prompts);
84 auth_defer_msg = expand_string_message;
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
97 if (Ustrcmp(data, "=") == 0)
99 auth_vars[0] = expand_nstring[++expand_nmax] = US"";
100 expand_nlength[expand_nmax] = 0;
104 if ((len = auth_b64decode(data, &clear)) < 0) return BAD64;
106 while (clear < end && expand_nmax < EXPAND_MAXN)
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];
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. */
121 while ((s = string_nextinlist(&prompts, &sep, big_buffer, big_buffer_size))
122 != NULL && expand_nmax < EXPAND_MAXN)
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;
129 /* This loop must run at least once, in case the length is zero */
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];
137 while (clear < end && expand_nmax < EXPAND_MAXN);
140 /* We now have a number of items of data in $auth1, $auth2, etc (and also, for
141 compatibility, in $1, $2, etc). Authentication and authorization are handled
142 together for this authenticator by expanding the server_condition option. Note
143 that ablock->server_condition is always non-NULL because that's what configures
144 this authenticator as a server. */
146 return auth_check_serv_cond(ablock);
151 /*************************************************
152 * Client entry point *
153 *************************************************/
155 /* For interface, see auths/README */
158 auth_plaintext_client(
159 auth_instance *ablock, /* authenticator block */
160 smtp_inblock *inblock, /* connection inblock */
161 smtp_outblock *outblock, /* connection outblock */
162 int timeout, /* command timeout */
163 uschar *buffer, /* buffer for reading response */
164 int buffsize) /* size of buffer */
166 auth_plaintext_options_block *ob =
167 (auth_plaintext_options_block *)(ablock->options_block);
168 uschar *text = ob->client_send;
172 int auth_var_idx = 0;
174 /* The text is broken up into a number of different data items, which are
175 sent one by one. The first one is sent with the AUTH command; the remainder are
176 sent in response to subsequent prompts. Each is expanded before being sent. */
178 while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)) != NULL)
180 int i, len, clear_len;
181 uschar *ss = expand_string(s);
184 /* Forced expansion failure is not an error; authentication is abandoned. On
185 all but the first string, we have to abandon the authentication attempt by
186 sending a line containing "*". Save the failed expansion string, because it
187 is in big_buffer, and that gets used by the sending function. */
191 uschar *ssave = string_copy(s);
194 if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
195 (void) smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
197 if (expand_string_forcedfail)
199 *buffer = 0; /* No message */
202 string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
203 "authenticator: %s", ssave, ablock->name, expand_string_message);
209 /* The character ^ is used as an escape for a binary zero character, which is
210 needed for the PLAIN mechanism. It must be doubled if really needed. */
212 for (i = 0; i < len; i++)
216 if (ss[i+1] != '^') ss[i] = 0; else
220 memmove(ss + i, ss + i + 1, len - i);
225 /* The first string is attached to the AUTH command; others are sent
231 if (smtp_write_command(outblock, FALSE, "AUTH %s%s%s\r\n",
232 ablock->public_name, (len == 0)? "" : " ",
233 auth_b64encode(ss, len)) < 0)
238 if (smtp_write_command(outblock, FALSE, "%s\r\n",
239 auth_b64encode(ss, len)) < 0)
243 /* If we receive a success response from the server, authentication
244 has succeeded. There may be more data to send, but is there any point
245 in provoking an error here? */
247 if (smtp_read_response(inblock, US buffer, buffsize, '2', timeout)) return OK;
249 /* Not a success response. If errno != 0 there is some kind of transmission
250 error. Otherwise, check the response code in the buffer. If it starts with
251 '3', more data is expected. */
253 if (errno != 0 || buffer[0] != '3') return FAIL;
255 /* If there is no more data to send, we have to cancel the authentication
256 exchange and return ERROR. */
260 if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
261 (void)smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
262 string_format(buffer, buffsize, "Too few items in client_send in %s "
263 "authenticator", ablock->name);
267 /* Now that we know we'll continue, we put the received data into $auth<n>,
268 if possible. First, decode it: buffer+4 skips over the SMTP status code. */
270 clear_len = auth_b64decode(buffer+4, &clear);
272 /* If decoding failed, the default is to terminate the authentication, and
273 return FAIL, with the SMTP response still in the buffer. However, if client_
274 ignore_invalid_base64 is set, we ignore the error, and put an empty string
279 uschar *save_bad = string_copy(buffer);
280 if (!ob->client_ignore_invalid_base64)
282 if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
283 (void)smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
284 string_format(buffer, buffsize, "Invalid base64 string in server "
285 "response \"%s\"", save_bad);
292 if (auth_var_idx < AUTH_VARS)
293 auth_vars[auth_var_idx++] = string_copy(clear);
296 /* Control should never actually get here. */
301 /* End of plaintext.c */