r3463: separated out some more headers (asn_1.h, messages.h, dlinklist.h and ioctl.h)
[jelmer/samba4-debian.git] / source / auth / auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett         2001-2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "dlinklist.h"
23 #include "auth/auth.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_AUTH
27
28 /****************************************************************************
29  Try to get a challenge out of the various authentication modules.
30  Returns a const char of length 8 bytes.
31 ****************************************************************************/
32
33 static const uint8_t *get_ntlm_challenge(struct auth_context *auth_context) 
34 {
35         DATA_BLOB challenge = data_blob(NULL, 0);
36         const char *challenge_set_by = NULL;
37         struct auth_methods *auth_method;
38         TALLOC_CTX *mem_ctx;
39
40         if (auth_context->challenge.length) {
41                 DEBUG(5, ("get_ntlm_challenge (auth subsystem): returning previous challenge by module %s (normal)\n", 
42                           auth_context->challenge_set_by));
43                 return auth_context->challenge.data;
44         }
45
46         auth_context->challenge_may_be_modified = False;
47
48         for (auth_method = auth_context->auth_method_list; auth_method; auth_method = auth_method->next) {
49                 if (auth_method->get_chal == NULL) {
50                         DEBUG(5, ("auth_get_challenge: module %s did not want to specify a challenge\n", auth_method->name));
51                         continue;
52                 }
53
54                 DEBUG(5, ("auth_get_challenge: getting challenge from module %s\n", auth_method->name));
55                 if (challenge_set_by != NULL) {
56                         DEBUG(1, ("auth_get_challenge: CONFIGURATION ERROR: authentication method %s has already specified a challenge.  Challenge by %s ignored.\n", 
57                                   challenge_set_by, auth_method->name));
58                         continue;
59                 }
60
61                 mem_ctx = talloc_init("auth_get_challenge for module %s", auth_method->name);
62                 if (!mem_ctx) {
63                         smb_panic("talloc_init() failed!");
64                 }
65                 
66                 challenge = auth_method->get_chal(auth_context, &auth_method->private_data, mem_ctx);
67                 if (!challenge.length) {
68                         DEBUG(3, ("auth_get_challenge: getting challenge from authentication method %s FAILED.\n", 
69                                   auth_method->name));
70                 } else {
71                         DEBUG(5, ("auth_get_challenge: sucessfully got challenge from module %s\n", auth_method->name));
72                         auth_context->challenge = challenge;
73                         challenge_set_by = auth_method->name;
74                         auth_context->challenge_set_method = auth_method;
75                 }
76                 talloc_destroy(mem_ctx);
77         }
78         
79         if (!challenge_set_by) {
80                 uint8_t chal[8];
81                 
82                 generate_random_buffer(chal, sizeof(chal));
83                 auth_context->challenge = data_blob_talloc(auth_context, 
84                                                            chal, sizeof(chal));
85                 
86                 challenge_set_by = "random";
87                 auth_context->challenge_may_be_modified = True;
88         } 
89         
90         DEBUG(5, ("auth_context challenge created by %s\n", challenge_set_by));
91         DEBUG(5, ("challenge is: \n"));
92         dump_data(5, (const char *)auth_context->challenge.data, auth_context->challenge.length);
93         
94         SMB_ASSERT(auth_context->challenge.length == 8);
95
96         auth_context->challenge_set_by=challenge_set_by;
97
98         return auth_context->challenge.data;
99 }
100
101
102 /**
103  * Check user is in correct domain (if required)
104  *
105  * @param user Only used to fill in the debug message
106  * 
107  * @param domain The domain to be verified
108  *
109  * @return True if the user can connect with that domain, 
110  *         False otherwise.
111 **/
112
113 static BOOL check_domain_match(const char *user, const char *domain) 
114 {
115         /*
116          * If we aren't serving to trusted domains, we must make sure that
117          * the validation request comes from an account in the same domain
118          * as the Samba server
119          */
120
121         if (!lp_allow_trusted_domains() &&
122             !(strequal("", domain) || 
123               strequal(lp_workgroup(), domain) || 
124               is_myname(domain))) {
125                 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
126                 return False;
127         } else {
128                 return True;
129         }
130 }
131
132 /**
133  * Check a user's Plaintext, LM or NTLM password.
134  *
135  * Check a user's password, as given in the user_info struct and return various
136  * interesting details in the server_info struct.
137  *
138  * The return value takes precedence over the contents of the server_info 
139  * struct.  When the return is other than NT_STATUS_OK the contents 
140  * of that structure is undefined.
141  *
142  * @param user_info Contains the user supplied components, including the passwords.
143  *                  Must be created with make_user_info() or one of its wrappers.
144  *
145  * @param auth_context Supplies the challenges and some other data. 
146  *                  Must be created with make_auth_context(), and the challenges should be 
147  *                  filled in, either at creation or by calling the challenge geneation 
148  *                  function auth_get_challenge().  
149  *
150  * @param server_info If successful, contains information about the authentication, 
151  *                    including a SAM_ACCOUNT struct describing the user.
152  *
153  * @return An NTSTATUS with NT_STATUS_OK or an appropriate error.
154  *
155  **/
156
157 static NTSTATUS check_ntlm_password(struct auth_context *auth_context,
158                                     const struct auth_usersupplied_info *user_info, 
159                                     TALLOC_CTX *out_mem_ctx, 
160                                     struct auth_serversupplied_info **server_info)
161 {
162         /* if all the modules say 'not for me' this is reasonable */
163         NTSTATUS nt_status = NT_STATUS_NO_SUCH_USER;
164         struct auth_methods *auth_method;
165         TALLOC_CTX *mem_ctx;
166
167         if (!user_info || !auth_context || !server_info)
168                 return NT_STATUS_LOGON_FAILURE;
169
170         DEBUG(3, ("check_ntlm_password:  Checking password for unmapped user [%s]\\[%s]@[%s] with the new password interface\n", 
171                   user_info->client_domain.str, user_info->smb_name.str, user_info->wksta_name.str));
172
173         DEBUG(3, ("check_ntlm_password:  mapped user is: [%s]\\[%s]@[%s]\n", 
174                   user_info->domain.str, user_info->internal_username.str, user_info->wksta_name.str));
175
176         if (auth_context->challenge.length == 0) {
177                 /* get a challenge, if we have not asked for one yet */
178                 get_ntlm_challenge(auth_context);
179         }
180
181         if (auth_context->challenge.length != 8) {
182                 DEBUG(0, ("check_ntlm_password:  Invalid challenge stored for this auth context - cannot continue\n"));
183                 return NT_STATUS_LOGON_FAILURE;
184         }
185
186         if (auth_context->challenge_set_by)
187                 DEBUG(10, ("check_ntlm_password: auth_context challenge created by %s\n",
188                                         auth_context->challenge_set_by));
189
190         DEBUG(10, ("challenge is: \n"));
191         dump_data(5, (const char *)auth_context->challenge.data, auth_context->challenge.length);
192
193 #ifdef DEBUG_PASSWORD
194         DEBUG(100, ("user_info has passwords of length %d and %d\n", 
195                     user_info->lm_resp.length, user_info->nt_resp.length));
196         DEBUG(100, ("lm:\n"));
197         dump_data(100, user_info->lm_resp.data, user_info->lm_resp.length);
198         DEBUG(100, ("nt:\n"));
199         dump_data(100, user_info->nt_resp.data, user_info->nt_resp.length);
200 #endif
201
202         /* This needs to be sorted:  If it doesn't match, what should we do? */
203         if (!check_domain_match(user_info->smb_name.str, user_info->domain.str))
204                 return NT_STATUS_LOGON_FAILURE;
205
206         for (auth_method = auth_context->auth_method_list;auth_method; auth_method = auth_method->next) {
207                 NTSTATUS result;
208                 
209                 mem_ctx = talloc_init("%s authentication for user %s\\%s", auth_method->name, 
210                                             user_info->domain.str, user_info->smb_name.str);
211
212                 result = auth_method->auth(auth_context, auth_method->private_data, mem_ctx, user_info, server_info);
213
214                 /* check if the module did anything */
215                 if ( NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_NOT_IMPLEMENTED) ) {
216                         DEBUG(10,("check_ntlm_password: %s had nothing to say\n", auth_method->name));
217                         talloc_destroy(mem_ctx);
218                         continue;
219                 }
220
221                 nt_status = result;
222
223                 if (NT_STATUS_IS_OK(nt_status)) {
224                         DEBUG(3, ("check_ntlm_password: %s authentication for user [%s] succeeded\n", 
225                                   auth_method->name, user_info->smb_name.str));
226                         
227                         /* Give the server info to the client to hold onto */
228                         talloc_reference(out_mem_ctx, *server_info);
229                 } else {
230                         DEBUG(5, ("check_ntlm_password: %s authentication for user [%s] FAILED with error %s\n", 
231                                   auth_method->name, user_info->smb_name.str, nt_errstr(nt_status)));
232                 }
233
234                 talloc_destroy(mem_ctx);
235
236                 if ( NT_STATUS_IS_OK(nt_status))
237                 {
238                                 break;                  
239                 }
240         }
241
242         if (NT_STATUS_IS_OK(nt_status)) {
243                 if (NT_STATUS_IS_OK(nt_status)) {
244                         DEBUG((*server_info)->guest ? 5 : 2, 
245                               ("check_ntlm_password:  %sauthentication for user [%s] -> [%s] succeeded\n", 
246                                (*server_info)->guest ? "guest " : "", 
247                                user_info->smb_name.str, 
248                                user_info->internal_username.str));
249                 }
250         }
251
252         if (!NT_STATUS_IS_OK(nt_status)) {
253                 DEBUG(2, ("check_ntlm_password:  Authentication for user [%s] -> [%s] FAILED with error %s\n", 
254                           user_info->smb_name.str, user_info->internal_username.str, 
255                           nt_errstr(nt_status)));
256                 ZERO_STRUCTP(server_info);
257         }
258         return nt_status;
259 }
260
261 /***************************************************************************
262  Clear out a auth_context, and destroy the attached TALLOC_CTX
263 ***************************************************************************/
264
265 void free_auth_context(struct auth_context **auth_context)
266 {
267         struct auth_methods *auth_method;
268         
269         if (*auth_context) {
270                 /* Free private data of context's authentication methods */
271                 for (auth_method = (*auth_context)->auth_method_list; auth_method; auth_method = auth_method->next) {
272                         if (auth_method->free_private_data) {
273                                 auth_method->free_private_data (&auth_method->private_data);
274                                 auth_method->private_data = NULL;
275                         }
276                 }
277
278                 talloc_free(*auth_context);
279                 *auth_context = NULL;
280         }
281 }
282
283 /***************************************************************************
284  Make a auth_info struct
285 ***************************************************************************/
286
287 static NTSTATUS make_auth_context(TALLOC_CTX *mem_ctx, struct auth_context **auth_context) 
288 {
289         *auth_context = talloc_p(mem_ctx, struct auth_context);
290         if (!*auth_context) {
291                 DEBUG(0,("make_auth_context: talloc failed!\n"));
292                 return NT_STATUS_NO_MEMORY;
293         }
294         ZERO_STRUCTP(*auth_context);
295
296         (*auth_context)->check_ntlm_password = check_ntlm_password;
297         (*auth_context)->get_ntlm_challenge = get_ntlm_challenge;
298         
299         return NT_STATUS_OK;
300 }
301
302 /***************************************************************************
303  Make a auth_info struct for the auth subsystem
304 ***************************************************************************/
305
306 static NTSTATUS make_auth_context_text_list(TALLOC_CTX *mem_ctx, 
307                                             struct auth_context **auth_context, char **text_list) 
308 {
309         struct auth_methods *list = NULL;
310         struct auth_methods *t = NULL;
311         NTSTATUS nt_status;
312
313         if (!text_list) {
314                 DEBUG(2,("make_auth_context_text_list: No auth method list!?\n"));
315                 return NT_STATUS_UNSUCCESSFUL;
316         }
317         
318         if (!NT_STATUS_IS_OK(nt_status = make_auth_context(mem_ctx, auth_context)))
319                 return nt_status;
320         
321         for (;*text_list; text_list++) {
322                 char *module_name = smb_xstrdup(*text_list);
323                 char *module_params = NULL;
324                 char *p;
325                 const struct auth_operations *ops;
326
327                 DEBUG(5,("make_auth_context_text_list: Attempting to find an auth method to match %s\n",
328                                         *text_list));
329
330                 p = strchr(module_name, ':');
331                 if (p) {
332                         *p = 0;
333                         module_params = p+1;
334                         trim_string(module_params, " ", " ");
335                 }
336
337                 trim_string(module_name, " ", " ");
338
339                 ops = auth_backend_byname(module_name);
340                 if (!ops) {
341                         DEBUG(5,("make_auth_context_text_list: Found auth method %s\n", *text_list));
342                         SAFE_FREE(module_name);
343                         break;
344                 }
345
346                 if (NT_STATUS_IS_OK(ops->init(*auth_context, module_params, &t))) {
347                         DEBUG(5,("make_auth_context_text_list: auth method %s has a valid init\n",
348                                                 *text_list));
349                         DLIST_ADD_END(list, t, struct auth_methods *);
350                 } else {
351                         DEBUG(0,("make_auth_context_text_list: auth method %s did not correctly init\n",
352                                                 *text_list));
353                 }
354                 SAFE_FREE(module_name);
355         }
356         
357         (*auth_context)->auth_method_list = list;
358         
359         return nt_status;
360 }
361
362 /***************************************************************************
363  Make a auth_context struct for the auth subsystem
364 ***************************************************************************/
365
366 NTSTATUS make_auth_context_subsystem(TALLOC_CTX *mem_ctx, struct auth_context **auth_context) 
367 {
368         char **auth_method_list = NULL; 
369         NTSTATUS nt_status;
370
371         if (lp_auth_methods() && !str_list_copy(&auth_method_list, lp_auth_methods())) {
372                 return NT_STATUS_NO_MEMORY;
373         }
374
375         nt_status = make_auth_context_text_list(mem_ctx, auth_context, auth_method_list);
376         if (!NT_STATUS_IS_OK(nt_status)) {
377                 str_list_free(&auth_method_list);
378                 return nt_status;
379         }
380         
381         str_list_free(&auth_method_list);
382         return nt_status;
383 }
384
385 /***************************************************************************
386  Make a auth_info struct with a fixed challenge
387 ***************************************************************************/
388
389 NTSTATUS make_auth_context_fixed(TALLOC_CTX *mem_ctx, 
390                                  struct auth_context **auth_context, uint8_t chal[8]) 
391 {
392         NTSTATUS nt_status;
393         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(mem_ctx, auth_context))) {
394                 return nt_status;
395         }
396         
397         (*auth_context)->challenge = data_blob_talloc(*auth_context, chal, 8);
398         (*auth_context)->challenge_set_by = "fixed";
399         return nt_status;
400 }
401
402 /* the list of currently registered AUTH backends */
403 static struct {
404         const struct auth_operations *ops;
405 } *backends = NULL;
406 static int num_backends;
407
408 /*
409   register a AUTH backend. 
410
411   The 'name' can be later used by other backends to find the operations
412   structure for this backend.
413 */
414 static NTSTATUS auth_register(const void *_ops)
415 {
416         const struct auth_operations *ops = _ops;
417         struct auth_operations *new_ops;
418         
419         if (auth_backend_byname(ops->name) != NULL) {
420                 /* its already registered! */
421                 DEBUG(0,("AUTH backend '%s' already registered\n", 
422                          ops->name));
423                 return NT_STATUS_OBJECT_NAME_COLLISION;
424         }
425
426         backends = Realloc(backends, sizeof(backends[0]) * (num_backends+1));
427         if (!backends) {
428                 smb_panic("out of memory in auth_register");
429         }
430
431         new_ops = smb_xmemdup(ops, sizeof(*ops));
432         new_ops->name = smb_xstrdup(ops->name);
433
434         backends[num_backends].ops = new_ops;
435
436         num_backends++;
437
438         DEBUG(3,("AUTH backend '%s' registered\n", 
439                  ops->name));
440
441         return NT_STATUS_OK;
442 }
443
444 /*
445   return the operations structure for a named backend of the specified type
446 */
447 const struct auth_operations *auth_backend_byname(const char *name)
448 {
449         int i;
450
451         for (i=0;i<num_backends;i++) {
452                 if (strcmp(backends[i].ops->name, name) == 0) {
453                         return backends[i].ops;
454                 }
455         }
456
457         return NULL;
458 }
459
460 /*
461   return the AUTH interface version, and the size of some critical types
462   This can be used by backends to either detect compilation errors, or provide
463   multiple implementations for different smbd compilation options in one module
464 */
465 const struct auth_critical_sizes *auth_interface_version(void)
466 {
467         static const struct auth_critical_sizes critical_sizes = {
468                 AUTH_INTERFACE_VERSION,
469                 sizeof(struct auth_operations),
470                 sizeof(struct auth_methods),
471                 sizeof(struct auth_context),
472                 sizeof(struct auth_usersupplied_info),
473                 sizeof(struct auth_serversupplied_info),
474                 sizeof(struct auth_str),
475         };
476
477         return &critical_sizes;
478 }
479
480 /*
481   initialise the AUTH subsystem
482 */
483 BOOL auth_init(void)
484 {
485         NTSTATUS status;
486         
487         /* ugly cludge, to go away */
488         static BOOL initialised;
489
490         if (initialised) {
491                 return True;
492         }
493
494         status = register_subsystem("auth", auth_register); 
495         if (!NT_STATUS_IS_OK(status)) {
496                 return False;
497         }
498
499         /* FIXME: Perhaps panic if a basic backend, such as SAM, fails to initialise? */
500         static_init_auth;
501
502         initialised = True;
503         DEBUG(3,("AUTH subsystem version %d initialised\n", AUTH_INTERFACE_VERSION));
504         return True;
505 }
506
507 NTSTATUS server_service_auth_init(void)
508 {
509         return NT_STATUS_OK;    
510 }