Add server_condition to all authenticators, to allow for additional
[exim.git] / src / src / auths / spa.c
CommitLineData
16ff981e 1/* $Cambridge: exim/src/src/auths/spa.c,v 1.8 2006/10/16 15:44:36 ph10 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
d7d7b7b9 7/* Copyright (c) University of Cambridge 1995 - 2006 */
0756eb3c
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10/* This file, which provides support for Microsoft's Secure Password
11Authentication, was contributed by Marc Prud'hommeaux. Tom Kistner added SPA
12server support. I (PH) have only modified it in very trivial ways.
13
14References:
15 http://www.innovation.ch/java/ntlm.html
16 http://www.kuro5hin.org/story/2002/4/28/1436/66154
17
18 * It seems that some systems have existing but different definitions of some
19 * of the following types. I received a complaint about "int16" causing
20 * compilation problems. So I (PH) have renamed them all, to be on the safe
21 * side, by adding 'x' on the end. See auths/auth-spa.h.
22
23 * typedef signed short int16;
24 * typedef unsigned short uint16;
25 * typedef unsigned uint32;
26 * typedef unsigned char uint8;
27
16ff981e
PH
2807-August-2003: PH: Patched up the code to avoid assert bombouts for stupid
29 input data. Find appropriate comment by grepping for "PH".
3016-October-2006: PH: Added a call to auth_check_serv_cond() at the end
0756eb3c
PH
31*/
32
33
34#include "../exim.h"
35#include "spa.h"
36
37/* #define DEBUG_SPA */
38
39#ifdef DEBUG_SPA
40#define DSPA(x,y,z) debug_printf(x,y,z)
41#else
42#define DSPA(x,y,z)
43#endif
44
45/* Options specific to the spa authentication mechanism. */
46
47optionlist auth_spa_options[] = {
48 { "client_domain", opt_stringptr,
49 (void *)(offsetof(auth_spa_options_block, spa_domain)) },
50 { "client_password", opt_stringptr,
51 (void *)(offsetof(auth_spa_options_block, spa_password)) },
52 { "client_username", opt_stringptr,
53 (void *)(offsetof(auth_spa_options_block, spa_username)) },
54 { "server_password", opt_stringptr,
55 (void *)(offsetof(auth_spa_options_block, spa_serverpassword)) }
56};
57
58/* Size of the options list. An extern variable has to be used so that its
59address can appear in the tables drtables.c. */
60
61int auth_spa_options_count =
62 sizeof(auth_spa_options)/sizeof(optionlist);
63
64/* Default private options block for the contidion authentication method. */
65
66auth_spa_options_block auth_spa_option_defaults = {
67 NULL, /* spa_password */
68 NULL, /* spa_username */
69 NULL, /* spa_domain */
70 NULL /* spa_serverpassword (for server side use) */
71};
72
73
74/*************************************************
75* Initialization entry point *
76*************************************************/
77
78/* Called for each instance, after its options have been read, to
79enable consistency checks to be done, or anything else that needs
80to be set up. */
81
82void
83auth_spa_init(auth_instance *ablock)
84{
85auth_spa_options_block *ob =
86 (auth_spa_options_block *)(ablock->options_block);
87
88/* The public name defaults to the authenticator name */
89
90if (ablock->public_name == NULL) ablock->public_name = ablock->name;
91
92/* Both username and password must be set for a client */
93
94if ((ob->spa_username == NULL) != (ob->spa_password == NULL))
95 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator:\n "
96 "one of client_username and client_password cannot be set without "
97 "the other", ablock->name);
98ablock->client = ob->spa_username != NULL;
99
100/* For a server we have just one option */
101
102ablock->server = ob->spa_serverpassword != NULL;
103}
104
105
106
107/*************************************************
108* Server entry point *
109*************************************************/
110
111/* For interface, see auths/README */
112
113#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
114#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
115#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
116#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
117
118int
119auth_spa_server(auth_instance *ablock, uschar *data)
120{
121auth_spa_options_block *ob = (auth_spa_options_block *)(ablock->options_block);
122uint8x lmRespData[24];
123uint8x ntRespData[24];
124SPAAuthRequest request;
125SPAAuthChallenge challenge;
126SPAAuthResponse response;
127SPAAuthResponse *responseptr = &response;
128uschar msgbuf[2048];
129uschar *clearpass;
130
131/* send a 334, MS Exchange style, and grab the client's request */
132
133if (auth_get_no64_data(&data, US"NTLM supported") != OK)
134 {
135 /* something borked */
136 return FAIL;
137 }
138
85b87bc2 139if (spa_base64_to_bits((char *)(&request), sizeof(request), (const char *)(data)) < 0)
0756eb3c
PH
140 {
141 DEBUG(D_auth) debug_printf("auth_spa_server(): bad base64 data in "
142 "request: %s\n", data);
143 return FAIL;
144 }
145
146/* create a challenge and send it back */
147
148spa_build_auth_challenge(&request,&challenge);
149spa_bits_to_base64 (msgbuf, (unsigned char*)&challenge,
150 spa_request_length(&challenge));
151
152if (auth_get_no64_data(&data, msgbuf) != OK)
153 {
154 /* something borked */
155 return FAIL;
156 }
157
158/* dump client response */
85b87bc2 159if (spa_base64_to_bits((char *)(&response), sizeof(response), (const char *)(data)) < 0)
0756eb3c
PH
160 {
161 DEBUG(D_auth) debug_printf("auth_spa_server(): bad base64 data in "
162 "response: %s\n", data);
163 return FAIL;
164 }
165
0756eb3c
PH
166/***************************************************************
167PH 07-Aug-2003: The original code here was this:
168
169Ustrcpy(msgbuf, unicodeToString(((char*)responseptr) +
170 IVAL(&responseptr->uUser.offset,0),
171 SVAL(&responseptr->uUser.len,0)/2) );
172
173However, if the response data is too long, unicodeToString bombs out on
174an assertion failure. It uses a 1024 fixed buffer. Bombing out is not a good
175idea. It's too messy to try to rework that function to return an error because
176it is called from a number of other places in the auth-spa.c module. Instead,
177since it is a very small function, I reproduce its code here, with a size check
178that causes failure if the size of msgbuf is exceeded. ****/
179
180 {
181 int i;
182 char *p = ((char*)responseptr) + IVAL(&responseptr->uUser.offset,0);
183 int len = SVAL(&responseptr->uUser.len,0)/2;
184
185 if (len + 1 >= sizeof(msgbuf)) return FAIL;
186 for (i = 0; i < len; ++i)
187 {
188 msgbuf[i] = *p & 0x7f;
189 p += 2;
190 }
191 msgbuf[i] = 0;
192 }
193
194/***************************************************************/
195
f78eb7c6
PH
196/* Put the username in $auth1 and $1. The former is now the preferred variable;
197the latter is the original variable. */
198
199auth_vars[0] = expand_nstring[1] = msgbuf;
0756eb3c
PH
200expand_nlength[1] = Ustrlen(msgbuf);
201expand_nmax = 1;
202
f78eb7c6
PH
203debug_print_string(ablock->server_debug_string); /* customized debug */
204
0756eb3c
PH
205/* look up password */
206
207clearpass = expand_string(ob->spa_serverpassword);
208if (clearpass == NULL)
209 {
210 if (expand_string_forcedfail)
211 {
212 DEBUG(D_auth) debug_printf("auth_spa_server(): forced failure while "
213 "expanding spa_serverpassword\n");
214 return FAIL;
215 }
216 else
217 {
218 DEBUG(D_auth) debug_printf("auth_spa_server(): error while expanding "
219 "spa_serverpassword: %s\n", expand_string_message);
220 return DEFER;
221 }
222 }
223
224/* create local hash copy */
225
226spa_smb_encrypt (clearpass, challenge.challengeData, lmRespData);
227spa_smb_nt_encrypt (clearpass, challenge.challengeData, ntRespData);
228
229/* compare NT hash (LM may not be available) */
230
231if (memcmp(ntRespData,
232 ((unsigned char*)responseptr)+IVAL(&responseptr->ntResponse.offset,0),
233 24) == 0)
234 /* success. we have a winner. */
16ff981e
PH
235
236 /* Expand server_condition as an authorization check (PH) */
237 return auth_check_serv_cond(ablock);
0756eb3c
PH
238
239return FAIL;
240}
241
242
243/*************************************************
244* Client entry point *
245*************************************************/
246
247/* For interface, see auths/README */
248
249int
250auth_spa_client(
251 auth_instance *ablock, /* authenticator block */
252 smtp_inblock *inblock, /* connection inblock */
253 smtp_outblock *outblock, /* connection outblock */
254 int timeout, /* command timeout */
255 uschar *buffer, /* buffer for reading response */
256 int buffsize) /* size of buffer */
257{
258 auth_spa_options_block *ob =
259 (auth_spa_options_block *)(ablock->options_block);
260 SPAAuthRequest request;
261 SPAAuthChallenge challenge;
262 SPAAuthResponse response;
263 char msgbuf[2048];
264 char *domain = NULL;
265 char *username, *password;
266
0756eb3c
PH
267 /* Code added by PH to expand the options */
268
4730f942
PH
269 *buffer = 0; /* Default no message when cancelled */
270
0756eb3c
PH
271 username = CS expand_string(ob->spa_username);
272 if (username == NULL)
273 {
b1206957 274 if (expand_string_forcedfail) return CANCELLED;
0756eb3c
PH
275 string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
276 "authenticator: %s", ob->spa_username, ablock->name,
277 expand_string_message);
278 return ERROR;
279 }
280
281 password = CS expand_string(ob->spa_password);
282 if (password == NULL)
283 {
b1206957 284 if (expand_string_forcedfail) return CANCELLED;
0756eb3c
PH
285 string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
286 "authenticator: %s", ob->spa_password, ablock->name,
287 expand_string_message);
288 return ERROR;
289 }
290
291 if (ob->spa_domain != NULL)
292 {
293 domain = CS expand_string(ob->spa_domain);
294 if (domain == NULL)
295 {
b1206957 296 if (expand_string_forcedfail) return CANCELLED;
0756eb3c
PH
297 string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
298 "authenticator: %s", ob->spa_domain, ablock->name,
299 expand_string_message);
300 return ERROR;
301 }
302 }
303
304 /* Original code */
305
b1206957
PH
306 if (smtp_write_command(outblock, FALSE, "AUTH %s\r\n",
307 ablock->public_name) < 0)
308 return FAIL_SEND;
309
310 /* wait for the 3XX OK message */
311 if (!smtp_read_response(inblock, (uschar *)buffer, buffsize, '3', timeout))
312 return FAIL;
313
0756eb3c
PH
314 DSPA("\n\n%s authenticator: using domain %s\n\n",
315 ablock->name, domain);
316
317 spa_build_auth_request (&request, CS username, domain);
318 spa_bits_to_base64 (US msgbuf, (unsigned char*)&request,
319 spa_request_length(&request));
320
321 DSPA("\n\n%s authenticator: sending request (%s)\n\n", ablock->name,
322 msgbuf);
323
324 /* send the encrypted password */
325 if (smtp_write_command(outblock, FALSE, "%s\r\n", msgbuf) < 0)
326 return FAIL_SEND;
327
328 /* wait for the auth challenge */
329 if (!smtp_read_response(inblock, (uschar *)buffer, buffsize, '3', timeout))
330 return FAIL;
331
332 /* convert the challenge into the challenge struct */
333 DSPA("\n\n%s authenticator: challenge (%s)\n\n",
334 ablock->name, buffer + 4);
85b87bc2 335 spa_base64_to_bits ((char *)(&challenge), sizeof(challenge), (const char *)(buffer + 4));
0756eb3c
PH
336
337 spa_build_auth_response (&challenge, &response,
338 CS username, CS password);
339 spa_bits_to_base64 (US msgbuf, (unsigned char*)&response,
340 spa_request_length(&response));
341 DSPA("\n\n%s authenticator: challenge response (%s)\n\n", ablock->name,
342 msgbuf);
343
344 /* send the challenge response */
345 if (smtp_write_command(outblock, FALSE, "%s\r\n", msgbuf) < 0)
346 return FAIL_SEND;
347
348 /* If we receive a success response from the server, authentication
349 has succeeded. There may be more data to send, but is there any point
350 in provoking an error here? */
351 if (smtp_read_response(inblock, US buffer, buffsize, '2', timeout))
352 return OK;
353
354 /* Not a success response. If errno != 0 there is some kind of transmission
355 error. Otherwise, check the response code in the buffer. If it starts with
356 '3', more data is expected. */
357 if (errno != 0 || buffer[0] != '3')
358 return FAIL;
359
360 return FAIL;
361}
362
363/* End of spa.c */