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