1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 - 2021 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-only */
13 /****************************************************************
14 * Decode and split the argument of an AUTH command *
15 ****************************************************************/
17 /* If data was supplied on the AUTH command, decode it, and split it up into
18 multiple items at binary zeros. The strings are put into $auth1, $auth2, etc,
19 up to a maximum. To retain backwards compatibility, they are also put int $1,
20 $2, etc. If the data consists of the string "=" it indicates a single, empty
24 auth_read_input(const uschar * data)
26 if (Ustrcmp(data, "=") == 0)
28 auth_vars[0] = expand_nstring[++expand_nmax] = US"";
29 expand_nlength[expand_nmax] = 0;
33 uschar * clear, * end;
36 if ((len = b64decode(data, &clear)) < 0) return BAD64;
37 DEBUG(D_auth) debug_printf("auth input decode:");
38 for (end = clear + len; clear < end && expand_nmax < EXPAND_MAXN; )
40 DEBUG(D_auth) debug_printf(" '%s'", clear);
41 if (expand_nmax < AUTH_VARS) auth_vars[expand_nmax] = clear;
42 expand_nstring[++expand_nmax] = clear;
43 while (*clear != 0) clear++;
44 expand_nlength[expand_nmax] = clear++ - expand_nstring[expand_nmax];
46 DEBUG(D_auth) debug_printf("\n");
54 /*************************************************
55 * Issue a challenge and get a response *
56 *************************************************/
58 /* This function is used by authentication drivers to b64-encode and
59 output a challenge to the SMTP client, and read the response line.
62 aptr set to point to the response (which is in big_buffer)
63 challenge the challenge data (unencoded, may be binary)
64 challen the length of the challenge data, in bytes
66 Returns: OK on success
67 BAD64 if response too large for buffer
68 CANCELLED if response is "*"
72 auth_get_data(uschar ** aptr, const uschar * challenge, int challen)
76 smtp_printf("334 %s\r\n", FALSE, b64encode(challenge, challen));
77 while ((c = receive_getc(GETC_BUFFER_UNLIMITED)) != '\n' && c != EOF)
79 if (p >= big_buffer_size - 1) return BAD64;
82 if (p > 0 && big_buffer[p-1] == '\r') p--;
84 DEBUG(D_receive) debug_printf("SMTP<< %s\n", big_buffer);
85 if (Ustrcmp(big_buffer, "*") == 0) return CANCELLED;
93 auth_prompt(const uschar * challenge)
96 uschar * resp, * clear, * end;
98 if ((rc = auth_get_data(&resp, challenge, Ustrlen(challenge))) != OK)
100 if ((len = b64decode(resp, &clear)) < 0)
104 /* This loop must run at least once, in case the length is zero */
107 if (expand_nmax < AUTH_VARS) auth_vars[expand_nmax] = clear;
108 expand_nstring[++expand_nmax] = clear;
109 while (*clear != 0) clear++;
110 expand_nlength[expand_nmax] = clear++ - expand_nstring[expand_nmax];
112 while (clear < end && expand_nmax < EXPAND_MAXN);
117 /***********************************************
118 * Send an AUTH-negotiation item *
119 ************************************************/
121 /* Expand and send one client auth item and read the response.
122 Include the AUTH command and method if tagged as "first". Use the given buffer
123 for receiving the b6-encoded reply; decode it it return it in the string arg.
127 FAIL_SEND error after writing a command; errno is set
128 FAIL failed after reading a response;
129 either errno is set (for timeouts, I/O failures) or
130 the buffer contains the SMTP response line
131 CANCELLED the client cancelled authentication (often "fail" in expansion)
132 the buffer may contain a message; if not, *buffer = 0
133 ERROR local problem (typically expansion error); message in buffer
134 DEFER more items expected
138 auth_client_item(void * sx, auth_instance * ablock, const uschar ** inout,
139 unsigned flags, int timeout, uschar * buffer, int buffsize)
142 uschar * ss, * clear;
144 ss = US expand_cstring(*inout);
145 if (ss == *inout) ss = string_copy(ss);
147 /* Forced expansion failure is not an error; authentication is abandoned. On
148 all but the first string, we have to abandon the authentication attempt by
149 sending a line containing "*". Save the failed expansion string, because it
150 is in big_buffer, and that gets used by the sending function. */
154 if (!(flags & AUTH_ITEM_FIRST))
156 if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
157 (void) smtp_read_response(sx, US buffer, buffsize, '2', timeout);
159 if (f.expand_string_forcedfail)
161 *buffer = 0; /* No message */
164 string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
165 "authenticator: %s", *inout, ablock->name, expand_string_message);
171 /* The character ^ is used as an escape for a binary zero character, which is
172 needed for the PLAIN mechanism. It must be doubled if really needed.
174 The parsing ambiguity of ^^^ is taken as ^^ -> ^ ; ^ -> NUL - and there is
175 no way to get a leading ^ after a NUL. We would need to intro new syntax to
176 support that (probably preferring to take a more-standard exim list as a source
177 and concat the elements with intervening NULs. Either a magic marker on the
178 source string for client_send, or a new option). */
180 for (int i = 0; i < len; i++)
185 if (--len > i+1) memmove(ss + i + 1, ss + i + 2, len - i);
187 /* The first string is attached to the AUTH command; others are sent
190 if (flags & AUTH_ITEM_FIRST)
192 if (smtp_write_command(sx, SCMD_FLUSH, "AUTH %s%s%s\r\n",
193 ablock->public_name, len == 0 ? "" : " ", b64encode(CUS ss, len)) < 0)
197 if (smtp_write_command(sx, SCMD_FLUSH, "%s\r\n", b64encode(CUS ss, len)) < 0)
200 /* If we receive a success response from the server, authentication
201 has succeeded. There may be more data to send, but is there any point
202 in provoking an error here? */
204 if (smtp_read_response(sx, buffer, buffsize, '2', timeout))
210 /* Not a success response. If errno != 0 there is some kind of transmission
211 error. Otherwise, check the response code in the buffer. If it starts with
212 '3', more data is expected. */
214 if (errno != 0 || buffer[0] != '3') return FAIL;
216 /* If there is no more data to send, we have to cancel the authentication
217 exchange and return ERROR. */
219 if (flags & AUTH_ITEM_LAST)
221 if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
222 (void)smtp_read_response(sx, US buffer, buffsize, '2', timeout);
223 string_format(buffer, buffsize, "Too few items in client_send in %s "
224 "authenticator", ablock->name);
228 /* Now that we know we'll continue, we put the received data into $auth<n>,
229 if possible. First, decode it: buffer+4 skips over the SMTP status code. */
231 clear_len = b64decode(buffer+4, &clear);
233 /* If decoding failed, the default is to terminate the authentication, and
234 return FAIL, with the SMTP response still in the buffer. However, if client_
235 ignore_invalid_base64 is set, we ignore the error, and put an empty string
240 uschar *save_bad = string_copy(buffer);
241 if (!(flags & AUTH_ITEM_IGN64))
243 if (smtp_write_command(sx, SCMD_FLUSH, "*\r\n") >= 0)
244 (void)smtp_read_response(sx, US buffer, buffsize, '2', timeout);
245 string_format(buffer, buffsize, "Invalid base64 string in server "
246 "response \"%s\"", save_bad);
249 DEBUG(D_auth) debug_printf("bad b64 decode for '%s';"
250 " ignoring due to client_ignore_invalid_base64\n", save_bad);
251 clear = string_copy(US"");
260 /* End of get_data.c */