auth: Remove the "typedef auth_methods"
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "auth.h"
22 #include "../lib/tsocket/tsocket.h"
23
24 #include "param/param.h"
25 #include "../lib/messaging/messaging.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_AUTH
29
30 static_decl_auth;
31
32 static struct auth_init_function_entry *auth_backends = NULL;
33
34 static struct auth_init_function_entry *auth_find_backend_entry(const char *name);
35
36 NTSTATUS smb_register_auth(int version, const char *name, auth_init_function init)
37 {
38         struct auth_init_function_entry *entry = NULL;
39
40         if (version != AUTH_INTERFACE_VERSION) {
41                 DEBUG(0,("Can't register auth_method!\n"
42                          "You tried to register an auth module with AUTH_INTERFACE_VERSION %d, while this version of samba uses %d\n",
43                          version,AUTH_INTERFACE_VERSION));
44                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
45         }
46
47         if (!name || !init) {
48                 return NT_STATUS_INVALID_PARAMETER;
49         }
50
51         DEBUG(5,("Attempting to register auth backend %s\n", name));
52
53         if (auth_find_backend_entry(name)) {
54                 DEBUG(0,("There already is an auth method registered with the name %s!\n", name));
55                 return NT_STATUS_OBJECT_NAME_COLLISION;
56         }
57
58         entry = SMB_XMALLOC_P(struct auth_init_function_entry);
59         entry->name = smb_xstrdup(name);
60         entry->init = init;
61
62         DLIST_ADD(auth_backends, entry);
63         DEBUG(5,("Successfully added auth method '%s'\n", name));
64         return NT_STATUS_OK;
65 }
66
67 static struct auth_init_function_entry *auth_find_backend_entry(const char *name)
68 {
69         struct auth_init_function_entry *entry = auth_backends;
70
71         while(entry) {
72                 if (strcmp(entry->name, name)==0) return entry;
73                 entry = entry->next;
74         }
75
76         return NULL;
77 }
78
79 /****************************************************************************
80  Try to get a challenge out of the various authentication modules.
81  Returns a const char of length 8 bytes.
82 ****************************************************************************/
83
84 NTSTATUS auth_get_ntlm_challenge(struct auth_context *auth_context,
85                                  uint8_t chal[8])
86 {
87         if (auth_context->challenge.length) {
88                 DEBUG(5, ("get_ntlm_challenge (auth subsystem): returning previous challenge by module %s (normal)\n", 
89                           auth_context->challenge_set_by));
90                 memcpy(chal, auth_context->challenge.data, 8);
91                 return NT_STATUS_OK;
92         }
93
94         auth_context->challenge = data_blob_talloc(auth_context, NULL, 8);
95         if (auth_context->challenge.data == NULL) {
96                 DBG_WARNING("data_blob_talloc failed\n");
97                 return NT_STATUS_NO_MEMORY;
98         }
99         generate_random_buffer(
100                 auth_context->challenge.data, auth_context->challenge.length);
101
102         auth_context->challenge_set_by = "random";
103
104         memcpy(chal, auth_context->challenge.data, 8);
105         return NT_STATUS_OK;
106 }
107
108
109 /**
110  * Check user is in correct domain (if required)
111  *
112  * @param user Only used to fill in the debug message
113  * 
114  * @param domain The domain to be verified
115  *
116  * @return True if the user can connect with that domain, 
117  *         False otherwise.
118 **/
119
120 static bool check_domain_match(const char *user, const char *domain) 
121 {
122         /*
123          * If we aren't serving to trusted domains, we must make sure that
124          * the validation request comes from an account in the same domain
125          * as the Samba server
126          */
127
128         if (!lp_allow_trusted_domains() &&
129             !(strequal("", domain) || 
130               strequal(lp_workgroup(), domain) || 
131               is_myname(domain))) {
132                 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
133                 return False;
134         } else {
135                 return True;
136         }
137 }
138
139 /**
140  * Check a user's Plaintext, LM or NTLM password.
141  *
142  * Check a user's password, as given in the user_info struct and return various
143  * interesting details in the server_info struct.
144  *
145  * This function does NOT need to be in a become_root()/unbecome_root() pair
146  * as it makes the calls itself when needed.
147  *
148  * The return value takes precedence over the contents of the server_info 
149  * struct.  When the return is other than NT_STATUS_OK the contents 
150  * of that structure is undefined.
151  *
152  * @param user_info Contains the user supplied components, including the passwords.
153  *                  Must be created with make_user_info() or one of its wrappers.
154  *
155  * @param auth_context Supplies the challenges and some other data. 
156  *                  Must be created with make_auth_context(), and the challenges should be 
157  *                  filled in, either at creation or by calling the challenge geneation 
158  *                  function auth_get_challenge().  
159  *
160  * @param pserver_info If successful, contains information about the authentication,
161  *                     including a struct samu struct describing the user.
162  *
163  * @param pauthoritative Indicates if the result should be treated as final
164  *                       result.
165  *
166  * @return An NTSTATUS with NT_STATUS_OK or an appropriate error.
167  *
168  **/
169 NTSTATUS auth_check_ntlm_password(TALLOC_CTX *mem_ctx,
170                                   const struct auth_context *auth_context,
171                                   const struct auth_usersupplied_info *user_info,
172                                   struct auth_serversupplied_info **pserver_info,
173                                   uint8_t *pauthoritative)
174 {
175         TALLOC_CTX *frame;
176         const char *auth_method_name = "";
177         /* if all the modules say 'not for me' this is reasonable */
178         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
179         const char *unix_username;
180         struct auth_methods *auth_method;
181         struct auth_serversupplied_info *server_info = NULL;
182         struct dom_sid sid = {0};
183         struct imessaging_context *msg_ctx = NULL;
184         struct loadparm_context *lp_ctx = NULL;
185
186         if (user_info == NULL || auth_context == NULL || pserver_info == NULL) {
187                 return NT_STATUS_LOGON_FAILURE;
188         }
189
190         frame = talloc_stackframe();
191
192         if (lp_auth_event_notification()) {
193                 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
194                 msg_ctx = imessaging_client_init(
195                     frame, lp_ctx, global_event_context());
196         }
197
198         *pauthoritative = 1;
199
200         DEBUG(3, ("check_ntlm_password:  Checking password for unmapped user [%s]\\[%s]@[%s] with the new password interface\n", 
201                   user_info->client.domain_name, user_info->client.account_name, user_info->workstation_name));
202
203         DEBUG(3, ("check_ntlm_password:  mapped user is: [%s]\\[%s]@[%s]\n", 
204                   user_info->mapped.domain_name, user_info->mapped.account_name, user_info->workstation_name));
205
206         if (auth_context->challenge.length != 8) {
207                 DEBUG(0, ("check_ntlm_password:  Invalid challenge stored for this auth context - cannot continue\n"));
208                 nt_status = NT_STATUS_LOGON_FAILURE;
209                 goto fail;
210         }
211
212         if (auth_context->challenge_set_by)
213                 DEBUG(10, ("check_ntlm_password: auth_context challenge created by %s\n",
214                                         auth_context->challenge_set_by));
215
216         DEBUG(10, ("challenge is: \n"));
217         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
218
219 #ifdef DEBUG_PASSWORD
220         DEBUG(100, ("user_info has passwords of length %d and %d\n", 
221                     (int)user_info->password.response.lanman.length, (int)user_info->password.response.nt.length));
222         DEBUG(100, ("lm:\n"));
223         dump_data(100, user_info->password.response.lanman.data, user_info->password.response.lanman.length);
224         DEBUG(100, ("nt:\n"));
225         dump_data(100, user_info->password.response.nt.data, user_info->password.response.nt.length);
226 #endif
227
228         /* This needs to be sorted:  If it doesn't match, what should we do? */
229         if (!check_domain_match(user_info->client.account_name,
230                                 user_info->mapped.domain_name)) {
231                 nt_status = NT_STATUS_LOGON_FAILURE;
232                 goto fail;
233         }
234
235         for (auth_method = auth_context->auth_method_list;auth_method; auth_method = auth_method->next) {
236
237                 auth_method_name = auth_method->name;
238
239                 nt_status = auth_method->auth(auth_context,
240                                               auth_method->private_data,
241                                               talloc_tos(),
242                                               user_info,
243                                               &server_info);
244
245                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOT_IMPLEMENTED)) {
246                         break;
247                 }
248
249                 DBG_DEBUG("%s had nothing to say\n", auth_method->name);
250         }
251
252         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOT_IMPLEMENTED)) {
253                 *pauthoritative = 0;
254                 nt_status = NT_STATUS_NO_SUCH_USER;
255         }
256
257         if (!NT_STATUS_IS_OK(nt_status)) {
258                 DBG_INFO("%s authentication for user [%s] FAILED with "
259                          "error %s, authoritative=%u\n",
260                          auth_method_name,
261                          user_info->client.account_name,
262                          nt_errstr(nt_status),
263                          *pauthoritative);
264                 goto fail;
265         }
266
267         DBG_NOTICE("%s authentication for user [%s] succeeded\n",
268                    auth_method_name, user_info->client.account_name);
269
270         unix_username = server_info->unix_name;
271
272         /* We skip doing this step if the caller asked us not to */
273         if (!(user_info->flags & USER_INFO_INFO3_AND_NO_AUTHZ)
274             && !(server_info->guest)) {
275                 const char *rhost;
276
277                 if (tsocket_address_is_inet(user_info->remote_host, "ip")) {
278                         rhost = tsocket_address_inet_addr_string(
279                                 user_info->remote_host, talloc_tos());
280                         if (rhost == NULL) {
281                                 nt_status = NT_STATUS_NO_MEMORY;
282                                 goto fail;
283                         }
284                 } else {
285                         rhost = "127.0.0.1";
286                 }
287
288                 /* We might not be root if we are an RPC call */
289                 become_root();
290                 nt_status = smb_pam_accountcheck(unix_username, rhost);
291                 unbecome_root();
292
293                 if (NT_STATUS_IS_OK(nt_status)) {
294                         DEBUG(5, ("check_ntlm_password:  PAM Account for user [%s] "
295                                   "succeeded\n", unix_username));
296                 } else {
297                         DEBUG(3, ("check_ntlm_password:  PAM Account for user [%s] "
298                                   "FAILED with error %s\n",
299                                   unix_username, nt_errstr(nt_status)));
300                 }
301         }
302
303         if (!NT_STATUS_IS_OK(nt_status)) {
304                 goto fail;
305         }
306
307         nt_status = get_user_sid_info3_and_extra(server_info->info3,
308                                                  &server_info->extra,
309                                                  &sid);
310         if (!NT_STATUS_IS_OK(nt_status)) {
311                 sid = (struct dom_sid) {0};
312         }
313
314         log_authentication_event(msg_ctx,
315                                  lp_ctx,
316                                  &auth_context->start_time,
317                                  user_info,
318                                  nt_status,
319                                  server_info->info3->base.logon_domain.string,
320                                  server_info->info3->base.account_name.string,
321                                  &sid);
322
323         DEBUG(server_info->guest ? 5 : 2,
324               ("check_ntlm_password:  %sauthentication for user "
325                "[%s] -> [%s] -> [%s] succeeded\n",
326                server_info->guest ? "guest " : "",
327                user_info->client.account_name,
328                user_info->mapped.account_name,
329                unix_username));
330
331         *pserver_info = talloc_move(mem_ctx, &server_info);
332
333         TALLOC_FREE(frame);
334         return NT_STATUS_OK;
335
336 fail:
337
338         /* failed authentication; check for guest lapping */
339
340         /*
341          * Please try not to change this string, it is probably in use
342          * in audit logging tools
343          */
344         DEBUG(2, ("check_ntlm_password:  Authentication for user "
345                   "[%s] -> [%s] FAILED with error %s, authoritative=%u\n",
346                   user_info->client.account_name, user_info->mapped.account_name,
347                   nt_errstr(nt_status), *pauthoritative));
348
349         log_authentication_event(msg_ctx,
350                                  lp_ctx,
351                                  &auth_context->start_time,
352                                  user_info,
353                                  nt_status,
354                                  NULL,
355                                  NULL,
356                                  NULL);
357
358         ZERO_STRUCTP(pserver_info);
359
360         TALLOC_FREE(frame);
361
362         return nt_status;
363 }
364
365 /***************************************************************************
366  Clear out a auth_context, and destroy the attached TALLOC_CTX
367 ***************************************************************************/
368
369 static int auth_context_destructor(void *ptr)
370 {
371         struct auth_context *ctx = talloc_get_type(ptr, struct auth_context);
372         struct auth_methods *am;
373
374
375         /* Free private data of context's authentication methods */
376         for (am = ctx->auth_method_list; am; am = am->next) {
377                 TALLOC_FREE(am->private_data);
378         }
379
380         return 0;
381 }
382
383 /***************************************************************************
384  Make a auth_info struct
385 ***************************************************************************/
386
387 static NTSTATUS make_auth_context(TALLOC_CTX *mem_ctx,
388                                   struct auth_context **auth_context)
389 {
390         struct auth_context *ctx;
391
392         ctx = talloc_zero(mem_ctx, struct auth_context);
393         if (!ctx) {
394                 DEBUG(0,("make_auth_context: talloc failed!\n"));
395                 return NT_STATUS_NO_MEMORY;
396         }
397
398         ctx->start_time = timeval_current();
399
400         talloc_set_destructor((TALLOC_CTX *)ctx, auth_context_destructor);
401
402         *auth_context = ctx;
403         return NT_STATUS_OK;
404 }
405
406 bool load_auth_module(struct auth_context *auth_context, 
407                       const char *module,
408                       struct auth_methods **ret)
409 {
410         static bool initialised_static_modules = False;
411
412         struct auth_init_function_entry *entry;
413         char *module_name = smb_xstrdup(module);
414         char *module_params = NULL;
415         char *p;
416         bool good = False;
417
418         /* Initialise static modules if not done so yet */
419         if(!initialised_static_modules) {
420                 static_init_auth(NULL);
421                 initialised_static_modules = True;
422         }
423
424         DEBUG(5,("load_auth_module: Attempting to find an auth method to match %s\n",
425                  module));
426
427         p = strchr(module_name, ':');
428         if (p) {
429                 *p = 0;
430                 module_params = p+1;
431                 trim_char(module_params, ' ', ' ');
432         }
433
434         trim_char(module_name, ' ', ' ');
435
436         entry = auth_find_backend_entry(module_name);
437
438         if (entry == NULL) {
439                 if (NT_STATUS_IS_OK(smb_probe_module("auth", module_name))) {
440                         entry = auth_find_backend_entry(module_name);
441                 }
442         }
443
444         if (entry != NULL) {
445                 if (!NT_STATUS_IS_OK(entry->init(auth_context, module_params, ret))) {
446                         DEBUG(0,("load_auth_module: auth method %s did not correctly init\n",
447                                  module_name));
448                 } else {
449                         DEBUG(5,("load_auth_module: auth method %s has a valid init\n",
450                                  module_name));
451                         good = True;
452                 }
453         } else {
454                 DEBUG(0,("load_auth_module: can't find auth method %s!\n", module_name));
455         }
456
457         SAFE_FREE(module_name);
458         return good;
459 }
460
461 /***************************************************************************
462  Make a auth_info struct for the auth subsystem
463 ***************************************************************************/
464
465 static NTSTATUS make_auth_context_text_list(TALLOC_CTX *mem_ctx,
466                                             struct auth_context **auth_context,
467                                             char **text_list)
468 {
469         struct auth_methods *list = NULL;
470         struct auth_methods *t, *method = NULL;
471         NTSTATUS nt_status;
472
473         if (!text_list) {
474                 DEBUG(2,("make_auth_context_text_list: No auth method list!?\n"));
475                 return NT_STATUS_UNSUCCESSFUL;
476         }
477
478         nt_status = make_auth_context(mem_ctx, auth_context);
479
480         if (!NT_STATUS_IS_OK(nt_status)) {
481                 return nt_status;
482         }
483
484         for (;*text_list; text_list++) { 
485                 if (load_auth_module(*auth_context, *text_list, &t)) {
486                     DLIST_ADD_END(list, t);
487                 }
488         }
489
490         (*auth_context)->auth_method_list = list;
491
492         /* Look for the first module to provide a prepare_gensec and
493          * make_auth4_context hook, and set that if provided */
494         for (method = (*auth_context)->auth_method_list; method; method = method->next) {
495                 if (method->prepare_gensec && method->make_auth4_context) {
496                         (*auth_context)->prepare_gensec = method->prepare_gensec;
497                         (*auth_context)->make_auth4_context = method->make_auth4_context;
498                         break;
499                 }
500         }
501         return NT_STATUS_OK;
502 }
503
504 static NTSTATUS make_auth_context_specific(TALLOC_CTX *mem_ctx,
505                                            struct auth_context **auth_context,
506                                            const char *methods)
507 {
508         char **method_list;
509         NTSTATUS status;
510
511         method_list = str_list_make_v3(talloc_tos(), methods, NULL);
512         if (method_list == NULL) {
513                 return NT_STATUS_NO_MEMORY;
514         }
515
516         status = make_auth_context_text_list(
517                 mem_ctx, auth_context, method_list);
518
519         TALLOC_FREE(method_list);
520
521         return status;
522 }
523
524 /***************************************************************************
525  Make a auth_context struct for the auth subsystem
526 ***************************************************************************/
527
528 NTSTATUS make_auth3_context_for_ntlm(TALLOC_CTX *mem_ctx,
529                                      struct auth_context **auth_context)
530 {
531         const char *methods = NULL;
532
533         switch (lp_server_role()) {
534         case ROLE_ACTIVE_DIRECTORY_DC:
535                 DEBUG(5,("Making default auth method list for server role = "
536                          "'active directory domain controller'\n"));
537                 methods = "samba4";
538                 break;
539         case ROLE_DOMAIN_MEMBER:
540                 DEBUG(5,("Making default auth method list for server role = 'domain member'\n"));
541                 methods = "anonymous sam winbind sam_ignoredomain";
542                 break;
543         case ROLE_DOMAIN_BDC:
544         case ROLE_DOMAIN_PDC:
545                 DEBUG(5,("Making default auth method list for DC\n"));
546                 methods = "anonymous sam winbind sam_ignoredomain";
547                 break;
548         case ROLE_STANDALONE:
549                 DEBUG(5,("Making default auth method list for server role = 'standalone server', encrypt passwords = yes\n"));
550                 if (lp_encrypt_passwords()) {
551                         methods = "anonymous sam_ignoredomain";
552                 } else {
553                         DEBUG(5,("Making default auth method list for server role = 'standalone server', encrypt passwords = no\n"));
554                         methods = "anonymous unix";
555                 }
556                 break;
557         default:
558                 DEBUG(5,("Unknown auth method!\n"));
559                 return NT_STATUS_UNSUCCESSFUL;
560         }
561
562         return make_auth_context_specific(mem_ctx, auth_context, methods);
563 }
564
565 NTSTATUS make_auth3_context_for_netlogon(TALLOC_CTX *mem_ctx,
566                                          struct auth_context **auth_context)
567 {
568         const char *methods = NULL;
569
570         switch (lp_server_role()) {
571         case ROLE_DOMAIN_BDC:
572         case ROLE_DOMAIN_PDC:
573                 methods = "sam_netlogon3 winbind";
574                 break;
575
576         default:
577                 DBG_ERR("Invalid server role!\n");
578                 return NT_STATUS_INVALID_SERVER_STATE;
579         }
580
581         return make_auth_context_specific(mem_ctx, auth_context, methods);
582 }
583
584 NTSTATUS make_auth3_context_for_winbind(TALLOC_CTX *mem_ctx,
585                                         struct auth_context **auth_context)
586 {
587         const char *methods = NULL;
588
589         switch (lp_server_role()) {
590         case ROLE_STANDALONE:
591         case ROLE_DOMAIN_MEMBER:
592         case ROLE_DOMAIN_BDC:
593         case ROLE_DOMAIN_PDC:
594                 methods = "sam";
595                 break;
596         case ROLE_ACTIVE_DIRECTORY_DC:
597                 methods = "samba4:sam";
598                 break;
599         default:
600                 DEBUG(5,("Unknown auth method!\n"));
601                 return NT_STATUS_UNSUCCESSFUL;
602         }
603
604         return make_auth_context_specific(mem_ctx, auth_context, methods);
605 }
606
607 bool auth3_context_set_challenge(struct auth_context *ctx, uint8_t chal[8],
608                                  const char *challenge_set_by)
609 {
610         ctx->challenge = data_blob_talloc(ctx, chal, 8);
611         if (ctx->challenge.data == NULL) {
612                 return false;
613         }
614         ctx->challenge_set_by = talloc_strdup(ctx, challenge_set_by);
615         if (ctx->challenge_set_by == NULL) {
616                 return false;
617         }
618         return true;
619 }