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