updated the 3.0 branch from the head branch - ready for alpha18
[ira/wip.git] / source3 / 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
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_AUTH
25
26 /** List of various built-in authentication modules */
27
28 const struct auth_init_function_entry builtin_auth_init_functions[] = {
29         { "guest", auth_init_guest },
30         { "rhosts", auth_init_rhosts },
31         { "hostsequiv", auth_init_hostsequiv },
32         { "sam", auth_init_sam },       
33         { "samstrict", auth_init_samstrict },
34         { "unix", auth_init_unix },
35         { "smbserver", auth_init_smbserver },
36         { "ntdomain", auth_init_ntdomain },
37         { "trustdomain", auth_init_trustdomain },
38         { "winbind", auth_init_winbind },
39 #ifdef DEVELOPER
40         { "name_to_ntstatus", auth_init_name_to_ntstatus },
41         { "fixed_challenge", auth_init_fixed_challenge },
42 #endif
43         { "plugin", auth_init_plugin },
44         { NULL, NULL}
45 };
46
47 /****************************************************************************
48  Try to get a challenge out of the various authentication modules.
49  Returns a const char of length 8 bytes.
50 ****************************************************************************/
51
52 static const uint8 *get_ntlm_challenge(struct auth_context *auth_context) 
53 {
54         DATA_BLOB challenge = data_blob(NULL, 0);
55         char *challenge_set_by = NULL;
56         auth_methods *auth_method;
57         TALLOC_CTX *mem_ctx;
58
59         if (auth_context->challenge.length) {
60                 DEBUG(5, ("get_ntlm_challenge (auth subsystem): returning previous challenge (normal)\n"));
61                 return auth_context->challenge.data;
62         }
63
64         for (auth_method = auth_context->auth_method_list; auth_method; auth_method = auth_method->next)
65         {
66                 if (auth_method->get_chal == NULL) {
67                         DEBUG(5, ("auth_get_challenge: module %s did not want to specify a challenge\n", auth_method->name));
68                         continue;
69                 }
70
71                 DEBUG(5, ("auth_get_challenge: getting challenge from module %s\n", auth_method->name));
72                 if (challenge_set_by != NULL) {
73                         DEBUG(1, ("auth_get_challenge: CONFIGURATION ERROR: authentication method %s has already specified a challenge.  Challenge by %s ignored.\n", 
74                                   challenge_set_by, auth_method->name));
75                         continue;
76                 }
77
78                 mem_ctx = talloc_init_named("auth_get_challenge for module %s", auth_method->name);
79                 if (!mem_ctx) {
80                         smb_panic("talloc_init_named() failed!");
81                 }
82                 
83                 challenge = auth_method->get_chal(auth_context, &auth_method->private_data, mem_ctx);
84                 if (!challenge.length) {
85                         DEBUG(3, ("auth_get_challenge: getting challenge from authentication method %s FAILED.\n", 
86                                   auth_method->name));
87                 } else {
88                         DEBUG(5, ("auth_get_challenge: sucessfully got challenge from module %s\n", auth_method->name));
89                         auth_context->challenge = challenge;
90                         challenge_set_by = auth_method->name;
91                         auth_context->challenge_set_method = auth_method;
92                 }
93                 talloc_destroy(mem_ctx);
94         }
95         
96         if (!challenge_set_by) {
97                 uchar chal[8];
98                 
99                 generate_random_buffer(chal, sizeof(chal), False);
100                 auth_context->challenge = data_blob_talloc(auth_context->mem_ctx, 
101                                                            chal, sizeof(chal));
102                 
103                 challenge_set_by = "random";
104         } 
105         
106         DEBUG(5, ("auth_context challenge created by %s\n", challenge_set_by));
107         DEBUG(5, ("challenge is: \n"));
108         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
109         
110         SMB_ASSERT(auth_context->challenge.length == 8);
111
112         auth_context->challenge_set_by=challenge_set_by;
113
114         return auth_context->challenge.data;
115 }
116
117
118 /**
119  * Check user is in correct domain (if required)
120  *
121  * @param user Only used to fill in the debug message
122  * 
123  * @param domain The domain to be verified
124  *
125  * @return True if the user can connect with that domain, 
126  *         False otherwise.
127 **/
128
129 static BOOL check_domain_match(const char *user, const char *domain) 
130 {
131         /*
132          * If we aren't serving to trusted domains, we must make sure that
133          * the validation request comes from an account in the same domain
134          * as the Samba server
135          */
136
137         if (!lp_allow_trusted_domains() &&
138             !(strequal("", domain) || 
139               strequal(lp_workgroup(), domain) || 
140               is_netbios_alias_or_name(domain))) {
141                 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
142                 return False;
143         } else {
144                 return True;
145         }
146 }
147
148 /**
149  * Check a user's Plaintext, LM or NTLM password.
150  *
151  * Check a user's password, as given in the user_info struct and return various
152  * interesting details in the server_info struct.
153  *
154  * This function does NOT need to be in a become_root()/unbecome_root() pair
155  * as it makes the calls itself when needed.
156  *
157  * The return value takes precedence over the contents of the server_info 
158  * struct.  When the return is other than NT_STATUS_OK the contents 
159  * of that structure is undefined.
160  *
161  * @param user_info Contains the user supplied components, including the passwords.
162  *                  Must be created with make_user_info() or one of its wrappers.
163  *
164  * @param auth_info Supplies the challenges and some other data. 
165  *                  Must be created with make_auth_info(), and the challenges should be 
166  *                  filled in, either at creation or by calling the challenge geneation 
167  *                  function auth_get_challenge().  
168  *
169  * @param server_info If successful, contains information about the authentication, 
170  *                    including a SAM_ACCOUNT struct describing the user.
171  *
172  * @return An NTSTATUS with NT_STATUS_OK or an appropriate error.
173  *
174  **/
175
176 static NTSTATUS check_ntlm_password(const struct auth_context *auth_context,
177                                     const struct auth_usersupplied_info *user_info, 
178                                     struct auth_serversupplied_info **server_info)
179 {
180         
181         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
182         const char *pdb_username;
183         auth_methods *auth_method;
184         TALLOC_CTX *mem_ctx;
185
186         if (!user_info || !auth_context || !server_info) {
187                 return NT_STATUS_LOGON_FAILURE;
188         }
189
190         DEBUG(3, ("check_password:  Checking password for unmapped user [%s]\\[%s]@[%s] with the new password interface\n", 
191                   user_info->client_domain.str, user_info->smb_name.str, user_info->wksta_name.str));
192
193         DEBUG(3, ("check_password:  mapped user is: [%s]\\[%s]@[%s]\n", 
194                   user_info->domain.str, user_info->internal_username.str, user_info->wksta_name.str));
195         if (auth_context->challenge_set_by) {
196                 DEBUG(10, ("auth_context challenge created by %s\n", auth_context->challenge_set_by));
197         }
198         DEBUG(10, ("challenge is: \n"));
199         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
200
201 #ifdef DEBUG_PASSWORD
202         DEBUG(100, ("user_info has passwords of length %d and %d\n", 
203                     user_info->lm_resp.length, user_info->nt_resp.length));
204         DEBUG(100, ("lm:\n"));
205         dump_data(100, user_info->lm_resp.data, user_info->lm_resp.length);
206         DEBUG(100, ("nt:\n"));
207         dump_data(100, user_info->nt_resp.data, user_info->nt_resp.length);
208 #endif
209
210         /* This needs to be sorted:  If it doesn't match, what should we do? */
211         if (!check_domain_match(user_info->smb_name.str, user_info->domain.str)) {
212                 return NT_STATUS_LOGON_FAILURE;
213         }
214
215         for (auth_method = auth_context->auth_method_list;auth_method; auth_method = auth_method->next)
216         {
217                 mem_ctx = talloc_init_named("%s authentication for user %s\\%s", auth_method->name, 
218                                             user_info->domain.str, user_info->smb_name.str);
219
220                 nt_status = auth_method->auth(auth_context, auth_method->private_data, mem_ctx, user_info, server_info);
221                 if (NT_STATUS_IS_OK(nt_status)) {
222                         DEBUG(3, ("check_password: %s authentication for user [%s] suceeded\n", 
223                                   auth_method->name, user_info->smb_name.str));
224                 } else {
225                         DEBUG(5, ("check_password: %s authentication for user [%s] FAILED with error %s\n", 
226                                   auth_method->name, user_info->smb_name.str, nt_errstr(nt_status)));
227                 }
228
229                 talloc_destroy(mem_ctx);
230
231                 if (NT_STATUS_IS_OK(nt_status)) {
232                         break;
233                 }
234         }
235
236         /* This is one of the few places the *relies* (rather than just sets defaults
237            on the value of lp_security().  This needs to change.  A new paramater 
238            perhaps? */
239         if (lp_security() >= SEC_SERVER) {
240                 smb_user_control(user_info, *server_info, nt_status);
241         }
242
243         if (NT_STATUS_IS_OK(nt_status)) {
244                 pdb_username = pdb_get_username((*server_info)->sam_account);
245                 if (!(*server_info)->guest) {
246                         /* We might not be root if we are an RPC call */
247                         become_root();
248                         nt_status = smb_pam_accountcheck(pdb_username);
249                         unbecome_root();
250                         
251                         if (NT_STATUS_IS_OK(nt_status)) {
252                                 DEBUG(5, ("check_password:  PAM Account for user [%s] suceeded\n", 
253                                           pdb_username));
254                         } else {
255                                 DEBUG(3, ("check_password:  PAM Account for user [%s] FAILED with error %s\n", 
256                                           pdb_username, nt_errstr(nt_status)));
257                         } 
258                 }
259                 
260                 if (NT_STATUS_IS_OK(nt_status)) {
261                         DEBUG((*server_info)->guest ? 5 : 2, 
262                               ("check_password:  %sauthentication for user [%s] -> [%s] -> [%s] suceeded\n", 
263                                (*server_info)->guest ? "guest " : "", 
264                                user_info->smb_name.str, 
265                                user_info->internal_username.str, 
266                                pdb_username));
267                 }
268         }
269
270         if (!NT_STATUS_IS_OK(nt_status)) {
271                 DEBUG(2, ("check_password:  Authentication for user [%s] -> [%s] FAILED with error %s\n", 
272                           user_info->smb_name.str, user_info->internal_username.str, 
273                           nt_errstr(nt_status)));
274                 ZERO_STRUCTP(server_info);
275         }
276         return nt_status;
277 }
278
279 /***************************************************************************
280  Clear out a auth_context, and destroy the attached TALLOC_CTX
281 ***************************************************************************/
282
283 static void free_auth_context(struct auth_context **auth_context)
284 {
285         if (*auth_context != NULL) {
286                 talloc_destroy((*auth_context)->mem_ctx);
287         }
288         *auth_context = NULL;
289 }
290
291 /***************************************************************************
292  Make a auth_info struct
293 ***************************************************************************/
294
295 static NTSTATUS make_auth_context(struct auth_context **auth_context) 
296 {
297         TALLOC_CTX *mem_ctx;
298
299         mem_ctx = talloc_init_named("authentication context");
300         
301         *auth_context = talloc(mem_ctx, sizeof(**auth_context));
302         if (!*auth_context) {
303                 DEBUG(0,("make_auth_context: talloc failed!\n"));
304                 talloc_destroy(mem_ctx);
305                 return NT_STATUS_NO_MEMORY;
306         }
307         ZERO_STRUCTP(*auth_context);
308
309         (*auth_context)->mem_ctx = mem_ctx;
310         (*auth_context)->check_ntlm_password = check_ntlm_password;
311         (*auth_context)->get_ntlm_challenge = get_ntlm_challenge;
312         (*auth_context)->free = free_auth_context;
313         
314         return NT_STATUS_OK;
315 }
316
317 /***************************************************************************
318  Make a auth_info struct for the auth subsystem
319 ***************************************************************************/
320
321 static NTSTATUS make_auth_context_text_list(struct auth_context **auth_context, char **text_list) 
322 {
323         auth_methods *list = NULL;
324         auth_methods *t = NULL;
325         auth_methods *tmp;
326         int i;
327         NTSTATUS nt_status;
328
329         if (!text_list) {
330                 DEBUG(2,("No auth method list!?\n"));
331                 return NT_STATUS_UNSUCCESSFUL;
332         }
333         
334         if (!NT_STATUS_IS_OK(nt_status = make_auth_context(auth_context))) {
335                 return nt_status;
336         }
337         
338         for (;*text_list; text_list++)
339         { 
340                 DEBUG(5,("Attempting to find an auth method to match %s\n", *text_list));
341                 for (i = 0; builtin_auth_init_functions[i].name; i++)
342                 {
343                         if (strequal(builtin_auth_init_functions[i].name, *text_list))
344                         {
345                                 
346                                 char *module_name = smb_xstrdup(*text_list);
347                                 char *module_params = NULL;
348                                 char *p;
349                                 
350                                 p = strchr(module_name, ':');
351                                 
352                                 if (p) {
353                                         *p = 0;
354                                         
355                                         module_params = p+1;
356                                         
357                                         trim_string(module_params, " ", " ");
358                                 }
359                                 
360                                 trim_string(module_name, " ", " ");
361
362                                 DEBUG(5,("Found auth method %s (at pos %d)\n", *text_list, i));
363                                 if (NT_STATUS_IS_OK(builtin_auth_init_functions[i].init(*auth_context, module_params, &t))) {
364                                         DEBUG(5,("auth method %s has a valid init\n", *text_list));
365                                         DLIST_ADD_END(list, t, tmp);
366                                 } else {
367                                         DEBUG(0,("auth method %s did not correctly init\n", *text_list));
368                                 }
369                                 SAFE_FREE(module_name);
370                                 break;
371                         }
372                 }
373         }
374         
375         (*auth_context)->auth_method_list = list;
376         
377         return nt_status;
378 }
379
380 /***************************************************************************
381  Make a auth_context struct for the auth subsystem
382 ***************************************************************************/
383
384 NTSTATUS make_auth_context_subsystem(struct auth_context **auth_context) 
385 {
386         char **auth_method_list = NULL; 
387         NTSTATUS nt_status;
388
389         if (lp_auth_methods() && !str_list_copy(&auth_method_list, lp_auth_methods())) {
390                 return NT_STATUS_NO_MEMORY;
391         }
392
393         if (auth_method_list == NULL) {
394                 switch (lp_security()) 
395                 {
396                 case SEC_DOMAIN:
397                         DEBUG(5,("Making default auth method list for security=domain\n"));
398                         auth_method_list = str_list_make("guest sam ntdomain");
399                         break;
400                 case SEC_SERVER:
401                         DEBUG(5,("Making default auth method list for security=server\n"));
402                         auth_method_list = str_list_make("guest sam smbserver");
403                         break;
404                 case SEC_USER:
405                         if (lp_encrypted_passwords()) { 
406                                 DEBUG(5,("Making default auth method list for security=user, encrypt passwords = yes\n"));
407                                 auth_method_list = str_list_make("guest sam");
408                         } else {
409                                 DEBUG(5,("Making default auth method list for security=user, encrypt passwords = no\n"));
410                                 auth_method_list = str_list_make("guest unix");
411                         }
412                         break;
413                 case SEC_SHARE:
414                         if (lp_encrypted_passwords()) {
415                                 DEBUG(5,("Making default auth method list for security=share, encrypt passwords = yes\n"));
416                                 auth_method_list = str_list_make("guest sam");
417                         } else {
418                                 DEBUG(5,("Making default auth method list for security=share, encrypt passwords = no\n"));
419                                 auth_method_list = str_list_make("guest unix");
420                         }
421                         break;
422                 case SEC_ADS:
423                         DEBUG(5,("Making default auth method list for security=ADS\n"));
424                         auth_method_list = str_list_make("guest sam ads ntdomain");
425                         break;
426                 default:
427                         DEBUG(5,("Unknown auth method!\n"));
428                         return NT_STATUS_UNSUCCESSFUL;
429                 }
430         } else {
431                 DEBUG(5,("Using specified auth order\n"));
432         }
433         
434         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_text_list(auth_context, auth_method_list))) {
435                 str_list_free(&auth_method_list);
436                 return nt_status;
437         }
438         
439         str_list_free(&auth_method_list);
440         return nt_status;
441 }
442
443 /***************************************************************************
444  Make a auth_info struct with a fixed challenge
445 ***************************************************************************/
446
447 NTSTATUS make_auth_context_fixed(struct auth_context **auth_context, uchar chal[8]) 
448 {
449         NTSTATUS nt_status;
450         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(auth_context))) {
451                 return nt_status;
452         }
453         
454         (*auth_context)->challenge = data_blob(chal, 8);
455         return nt_status;
456 }
457
458