2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2001-2002
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.
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.
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/>.
22 #include "../lib/tsocket/tsocket.h"
24 #include "param/param.h"
25 #include "../lib/messaging/messaging.h"
26 #include "lib/global_contexts.h"
29 #define DBGC_CLASS DBGC_AUTH
33 static struct auth_init_function_entry
*auth_backends
= NULL
;
35 static struct auth_init_function_entry
*auth_find_backend_entry(const char *name
);
37 NTSTATUS
smb_register_auth(int version
, const char *name
, auth_init_function init
)
39 struct auth_init_function_entry
*entry
= NULL
;
41 if (version
!= AUTH_INTERFACE_VERSION
) {
42 DEBUG(0,("Can't register auth_method!\n"
43 "You tried to register an auth module with AUTH_INTERFACE_VERSION %d, while this version of samba uses %d\n",
44 version
,AUTH_INTERFACE_VERSION
));
45 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
49 return NT_STATUS_INVALID_PARAMETER
;
52 DEBUG(5,("Attempting to register auth backend %s\n", name
));
54 if (auth_find_backend_entry(name
)) {
55 DEBUG(0,("There already is an auth method registered with the name %s!\n", name
));
56 return NT_STATUS_OBJECT_NAME_COLLISION
;
59 entry
= SMB_XMALLOC_P(struct auth_init_function_entry
);
60 entry
->name
= smb_xstrdup(name
);
63 DLIST_ADD(auth_backends
, entry
);
64 DEBUG(5,("Successfully added auth method '%s'\n", name
));
68 static struct auth_init_function_entry
*auth_find_backend_entry(const char *name
)
70 struct auth_init_function_entry
*entry
= auth_backends
;
73 if (strcmp(entry
->name
, name
)==0) return entry
;
80 /****************************************************************************
81 Try to get a challenge out of the various authentication modules.
82 Returns a const char of length 8 bytes.
83 ****************************************************************************/
85 NTSTATUS
auth_get_ntlm_challenge(struct auth_context
*auth_context
,
88 if (auth_context
->challenge
.length
) {
89 DBG_INFO("get_ntlm_challenge (auth subsystem): returning "
90 "previous challenge by module %s (normal)\n",
91 auth_context
->challenge_set_by
);
92 memcpy(chal
, auth_context
->challenge
.data
, 8);
96 auth_context
->challenge
= data_blob_talloc(auth_context
, NULL
, 8);
97 if (auth_context
->challenge
.data
== NULL
) {
98 DBG_WARNING("data_blob_talloc failed\n");
99 return NT_STATUS_NO_MEMORY
;
101 generate_random_buffer(
102 auth_context
->challenge
.data
, auth_context
->challenge
.length
);
104 auth_context
->challenge_set_by
= "random";
106 memcpy(chal
, auth_context
->challenge
.data
, 8);
112 * Check user is in correct domain (if required)
114 * @param user Only used to fill in the debug message
116 * @param domain The domain to be verified
118 * @return True if the user can connect with that domain,
122 static bool check_domain_match(const char *user
, const char *domain
)
125 * If we aren't serving to trusted domains, we must make sure that
126 * the validation request comes from an account in the same domain
127 * as the Samba server
130 if (!lp_allow_trusted_domains() &&
131 !(strequal("", domain
) ||
132 strequal(lp_workgroup(), domain
) ||
133 is_myname(domain
))) {
134 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user
, domain
));
142 * Check a user's Plaintext, LM or NTLM password.
144 * Check a user's password, as given in the user_info struct and return various
145 * interesting details in the server_info struct.
147 * This function does NOT need to be in a become_root()/unbecome_root() pair
148 * as it makes the calls itself when needed.
150 * The return value takes precedence over the contents of the server_info
151 * struct. When the return is other than NT_STATUS_OK the contents
152 * of that structure is undefined.
154 * @param user_info Contains the user supplied components, including the passwords.
155 * Must be created with make_user_info() or one of its wrappers.
157 * @param auth_context Supplies the challenges and some other data.
158 * Must be created with make_auth_context(), and the challenges should be
159 * filled in, either at creation or by calling the challenge generation
160 * function auth_get_challenge().
162 * @param pserver_info If successful, contains information about the authentication,
163 * including a struct samu struct describing the user.
165 * @param pauthoritative Indicates if the result should be treated as final
168 * @return An NTSTATUS with NT_STATUS_OK or an appropriate error.
171 NTSTATUS
auth_check_ntlm_password(TALLOC_CTX
*mem_ctx
,
172 const struct auth_context
*auth_context
,
173 const struct auth_usersupplied_info
*user_info
,
174 struct auth_serversupplied_info
**pserver_info
,
175 uint8_t *pauthoritative
)
178 const char *auth_method_name
= "";
179 /* if all the modules say 'not for me' this is reasonable */
180 NTSTATUS nt_status
= NT_STATUS_NOT_IMPLEMENTED
;
181 const char *unix_username
;
182 struct auth_methods
*auth_method
;
183 struct auth_serversupplied_info
*server_info
= NULL
;
184 struct dom_sid sid
= {0};
185 struct imessaging_context
*msg_ctx
= NULL
;
186 struct loadparm_context
*lp_ctx
= NULL
;
188 if (user_info
== NULL
|| auth_context
== NULL
|| pserver_info
== NULL
) {
189 return NT_STATUS_LOGON_FAILURE
;
192 frame
= talloc_stackframe();
194 if (lp_auth_event_notification()) {
195 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
196 msg_ctx
= imessaging_client_init(
197 frame
, lp_ctx
, global_event_context());
202 DBG_NOTICE("check_ntlm_password: Checking password for unmapped user "
203 "[%s]\\[%s]@[%s] with the new password interface\n",
204 user_info
->client
.domain_name
,
205 user_info
->client
.account_name
,
206 user_info
->workstation_name
);
208 DBG_NOTICE("check_ntlm_password: mapped user is: [%s]\\[%s]@[%s]\n",
209 user_info
->mapped
.domain_name
,
210 user_info
->mapped
.account_name
,
211 user_info
->workstation_name
);
213 if (auth_context
->challenge
.length
!= 8) {
214 DEBUG(0, ("check_ntlm_password: Invalid challenge stored for this auth context - cannot continue\n"));
215 nt_status
= NT_STATUS_LOGON_FAILURE
;
219 if (auth_context
->challenge_set_by
)
220 DEBUG(10, ("check_ntlm_password: auth_context challenge created by %s\n",
221 auth_context
->challenge_set_by
));
223 DEBUG(10, ("challenge is: \n"));
224 dump_data(5, auth_context
->challenge
.data
, auth_context
->challenge
.length
);
226 #ifdef DEBUG_PASSWORD
227 DEBUG(100, ("user_info has passwords of length %d and %d\n",
228 (int)user_info
->password
.response
.lanman
.length
, (int)user_info
->password
.response
.nt
.length
));
229 DEBUG(100, ("lm:\n"));
230 dump_data(100, user_info
->password
.response
.lanman
.data
, user_info
->password
.response
.lanman
.length
);
231 DEBUG(100, ("nt:\n"));
232 dump_data(100, user_info
->password
.response
.nt
.data
, user_info
->password
.response
.nt
.length
);
235 /* This needs to be sorted: If it doesn't match, what should we do? */
236 if (!check_domain_match(user_info
->client
.account_name
,
237 user_info
->mapped
.domain_name
)) {
238 nt_status
= NT_STATUS_LOGON_FAILURE
;
242 for (auth_method
= auth_context
->auth_method_list
;auth_method
; auth_method
= auth_method
->next
) {
244 auth_method_name
= auth_method
->name
;
246 nt_status
= auth_method
->auth(auth_context
,
247 auth_method
->private_data
,
252 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOT_IMPLEMENTED
)) {
256 DBG_DEBUG("%s had nothing to say\n", auth_method
->name
);
259 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOT_IMPLEMENTED
)) {
261 nt_status
= NT_STATUS_NO_SUCH_USER
;
264 if (!NT_STATUS_IS_OK(nt_status
)) {
265 DBG_INFO("%s authentication for user [%s] FAILED with "
266 "error %s, authoritative=%u\n",
268 user_info
->client
.account_name
,
269 nt_errstr(nt_status
),
274 DBG_NOTICE("%s authentication for user [%s] succeeded\n",
275 auth_method_name
, user_info
->client
.account_name
);
277 unix_username
= server_info
->unix_name
;
279 /* We skip doing this step if the caller asked us not to */
280 if (!(user_info
->flags
& USER_INFO_INFO3_AND_NO_AUTHZ
)
281 && !(server_info
->guest
)) {
284 if (tsocket_address_is_inet(user_info
->remote_host
, "ip")) {
285 rhost
= tsocket_address_inet_addr_string(
286 user_info
->remote_host
, talloc_tos());
288 nt_status
= NT_STATUS_NO_MEMORY
;
295 /* We might not be root if we are an RPC call */
297 nt_status
= smb_pam_accountcheck(unix_username
, rhost
);
300 if (NT_STATUS_IS_OK(nt_status
)) {
301 DEBUG(5, ("check_ntlm_password: PAM Account for user [%s] "
302 "succeeded\n", unix_username
));
304 DEBUG(3, ("check_ntlm_password: PAM Account for user [%s] "
305 "FAILED with error %s\n",
306 unix_username
, nt_errstr(nt_status
)));
310 if (!NT_STATUS_IS_OK(nt_status
)) {
314 nt_status
= get_user_sid_info3_and_extra(server_info
->info3
,
317 if (!NT_STATUS_IS_OK(nt_status
)) {
318 sid
= (struct dom_sid
) {0};
321 log_authentication_event(msg_ctx
,
323 &auth_context
->start_time
,
326 server_info
->info3
->base
.logon_domain
.string
,
327 server_info
->info3
->base
.account_name
.string
,
329 NULL
/* client_audit_info */,
330 NULL
/* server_audit_info */);
332 DEBUG(server_info
->guest
? 5 : 2,
333 ("check_ntlm_password: %sauthentication for user "
334 "[%s] -> [%s] -> [%s] succeeded\n",
335 server_info
->guest
? "guest " : "",
336 user_info
->client
.account_name
,
337 user_info
->mapped
.account_name
,
340 *pserver_info
= talloc_move(mem_ctx
, &server_info
);
347 /* failed authentication; check for guest lapping */
350 * Please try not to change this string, it is probably in use
351 * in audit logging tools
353 DEBUG(2, ("check_ntlm_password: Authentication for user "
354 "[%s] -> [%s] FAILED with error %s, authoritative=%u\n",
355 user_info
->client
.account_name
, user_info
->mapped
.account_name
,
356 nt_errstr(nt_status
), *pauthoritative
));
358 log_authentication_event(msg_ctx
,
360 &auth_context
->start_time
,
366 NULL
/* client_audit_info */,
367 NULL
/* server_audit_info */);
369 ZERO_STRUCTP(pserver_info
);
376 /***************************************************************************
377 Clear out a auth_context, and destroy the attached TALLOC_CTX
378 ***************************************************************************/
380 static int auth_context_destructor(void *ptr
)
382 struct auth_context
*ctx
= talloc_get_type(ptr
, struct auth_context
);
383 struct auth_methods
*am
;
386 /* Free private data of context's authentication methods */
387 for (am
= ctx
->auth_method_list
; am
; am
= am
->next
) {
388 TALLOC_FREE(am
->private_data
);
394 /***************************************************************************
395 Make a auth_info struct
396 ***************************************************************************/
398 static NTSTATUS
make_auth_context(TALLOC_CTX
*mem_ctx
,
399 struct auth_context
**auth_context
)
401 struct auth_context
*ctx
;
403 ctx
= talloc_zero(mem_ctx
, struct auth_context
);
405 DEBUG(0,("make_auth_context: talloc failed!\n"));
406 return NT_STATUS_NO_MEMORY
;
409 ctx
->start_time
= timeval_current();
411 talloc_set_destructor((TALLOC_CTX
*)ctx
, auth_context_destructor
);
417 static bool load_auth_module(
418 struct auth_context
*auth_context
,
420 struct auth_methods
**ret
)
422 static bool initialised_static_modules
= False
;
424 struct auth_init_function_entry
*entry
;
425 char *module_name
= smb_xstrdup(module
);
426 char *module_params
= NULL
;
430 /* Initialise static modules if not done so yet */
431 if(!initialised_static_modules
) {
432 static_init_auth(NULL
);
433 initialised_static_modules
= True
;
436 DEBUG(5,("load_auth_module: Attempting to find an auth method to match %s\n",
439 p
= strchr(module_name
, ':');
443 trim_char(module_params
, ' ', ' ');
446 trim_char(module_name
, ' ', ' ');
448 entry
= auth_find_backend_entry(module_name
);
451 if (NT_STATUS_IS_OK(smb_probe_module("auth", module_name
))) {
452 entry
= auth_find_backend_entry(module_name
);
457 if (!NT_STATUS_IS_OK(entry
->init(auth_context
, module_params
, ret
))) {
458 DEBUG(0,("load_auth_module: auth method %s did not correctly init\n",
461 DEBUG(5,("load_auth_module: auth method %s has a valid init\n",
466 DEBUG(0,("load_auth_module: can't find auth method %s!\n", module_name
));
469 SAFE_FREE(module_name
);
473 /***************************************************************************
474 Make a auth_info struct for the auth subsystem
475 ***************************************************************************/
477 static NTSTATUS
make_auth_context_text_list(TALLOC_CTX
*mem_ctx
,
478 struct auth_context
**auth_context
,
481 struct auth_methods
*list
= NULL
;
482 struct auth_methods
*t
, *method
= NULL
;
486 DEBUG(2,("make_auth_context_text_list: No auth method list!?\n"));
487 return NT_STATUS_UNSUCCESSFUL
;
490 nt_status
= make_auth_context(mem_ctx
, auth_context
);
492 if (!NT_STATUS_IS_OK(nt_status
)) {
496 for (;*text_list
; text_list
++) {
497 if (load_auth_module(*auth_context
, *text_list
, &t
)) {
498 DLIST_ADD_END(list
, t
);
502 (*auth_context
)->auth_method_list
= list
;
504 /* Look for the first module to provide a prepare_gensec and
505 * make_auth4_context hook, and set that if provided */
506 for (method
= (*auth_context
)->auth_method_list
; method
; method
= method
->next
) {
507 if (method
->prepare_gensec
&& method
->make_auth4_context
) {
508 (*auth_context
)->prepare_gensec
= method
->prepare_gensec
;
509 (*auth_context
)->make_auth4_context
= method
->make_auth4_context
;
516 static NTSTATUS
make_auth_context_specific(TALLOC_CTX
*mem_ctx
,
517 struct auth_context
**auth_context
,
523 method_list
= str_list_make_v3(talloc_tos(), methods
, NULL
);
524 if (method_list
== NULL
) {
525 return NT_STATUS_NO_MEMORY
;
528 status
= make_auth_context_text_list(
529 mem_ctx
, auth_context
, method_list
);
531 TALLOC_FREE(method_list
);
536 /***************************************************************************
537 Make a auth_context struct for the auth subsystem
538 ***************************************************************************/
540 NTSTATUS
make_auth3_context_for_ntlm(TALLOC_CTX
*mem_ctx
,
541 struct auth_context
**auth_context
)
543 const char *methods
= NULL
;
544 const char *role
= NULL
;
546 switch (lp_server_role()) {
547 case ROLE_ACTIVE_DIRECTORY_DC
:
548 role
= "'active directory domain controller'";
551 case ROLE_DOMAIN_MEMBER
:
552 role
= "'domain member'";
553 methods
= "anonymous sam winbind sam_ignoredomain";
555 case ROLE_DOMAIN_BDC
:
556 case ROLE_DOMAIN_PDC
:
559 methods
= "anonymous sam winbind sam_ignoredomain";
561 case ROLE_STANDALONE
:
562 if (lp_encrypt_passwords()) {
563 role
= "'standalone server', encrypt passwords = yes";
564 methods
= "anonymous sam_ignoredomain";
566 role
= "'standalone server', encrypt passwords = no";
567 methods
= "anonymous unix";
571 DEBUG(5,("Unknown auth method!\n"));
572 return NT_STATUS_UNSUCCESSFUL
;
575 DBG_INFO("Making default auth method list for server role = %s\n",
578 return make_auth_context_specific(mem_ctx
, auth_context
, methods
);
581 NTSTATUS
make_auth3_context_for_netlogon(TALLOC_CTX
*mem_ctx
,
582 struct auth_context
**auth_context
)
584 const char *methods
= NULL
;
586 switch (lp_server_role()) {
587 case ROLE_DOMAIN_BDC
:
588 case ROLE_DOMAIN_PDC
:
590 methods
= "sam_netlogon3 winbind";
594 DBG_ERR("Invalid server role!\n");
595 return NT_STATUS_INVALID_SERVER_STATE
;
598 return make_auth_context_specific(mem_ctx
, auth_context
, methods
);
601 NTSTATUS
make_auth3_context_for_winbind(TALLOC_CTX
*mem_ctx
,
602 struct auth_context
**auth_context
)
604 const char *methods
= NULL
;
606 switch (lp_server_role()) {
607 case ROLE_STANDALONE
:
608 case ROLE_DOMAIN_MEMBER
:
609 case ROLE_DOMAIN_BDC
:
610 case ROLE_DOMAIN_PDC
:
614 case ROLE_ACTIVE_DIRECTORY_DC
:
615 methods
= "samba4:sam";
618 DEBUG(5,("Unknown auth method!\n"));
619 return NT_STATUS_UNSUCCESSFUL
;
622 return make_auth_context_specific(mem_ctx
, auth_context
, methods
);
625 bool auth3_context_set_challenge(
626 struct auth_context
*ctx
,
627 const uint8_t chal
[8],
628 const char *challenge_set_by
)
630 ctx
->challenge
= data_blob_talloc(ctx
, chal
, 8);
631 if (ctx
->challenge
.data
== NULL
) {
634 ctx
->challenge_set_by
= talloc_strdup(ctx
, challenge_set_by
);
635 if (ctx
->challenge_set_by
== NULL
) {