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