2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jeremy Allison 2000-2001
7 Copyright (C) Rafal Szczesniak 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "librpc/gen_ndr/ndr_samr.h"
26 #include "librpc/gen_ndr/ndr_netlogon.h"
29 #define DBGC_CLASS DBGC_AUTH
31 /****************************************************************************
32 Create an auth_usersupplied_data structure
33 ****************************************************************************/
34 static NTSTATUS make_user_info(TALLOC_CTX *mem_ctx,
35 struct auth_usersupplied_info **user_info,
37 const char *internal_username,
38 const char *client_domain,
40 const char *wksta_name,
41 DATA_BLOB *lm_password, DATA_BLOB *nt_password,
42 DATA_BLOB *lm_interactive_password, DATA_BLOB *nt_interactive_password,
47 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
49 *user_info = talloc_p(mem_ctx, struct auth_usersupplied_info);
51 return NT_STATUS_NO_MEMORY;
54 ZERO_STRUCTP(*user_info);
56 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
58 (*user_info)->smb_name.str = talloc_strdup(*user_info, smb_name);
59 if ((*user_info)->smb_name.str) {
60 (*user_info)->smb_name.len = strlen(smb_name);
62 free_user_info(user_info);
63 return NT_STATUS_NO_MEMORY;
66 (*user_info)->internal_username.str = talloc_strdup(*user_info, internal_username);
67 if ((*user_info)->internal_username.str) {
68 (*user_info)->internal_username.len = strlen(internal_username);
70 free_user_info(user_info);
71 return NT_STATUS_NO_MEMORY;
74 (*user_info)->domain.str = talloc_strdup(*user_info, domain);
75 if ((*user_info)->domain.str) {
76 (*user_info)->domain.len = strlen(domain);
78 free_user_info(user_info);
79 return NT_STATUS_NO_MEMORY;
82 (*user_info)->client_domain.str = talloc_strdup(*user_info, client_domain);
83 if ((*user_info)->client_domain.str) {
84 (*user_info)->client_domain.len = strlen(client_domain);
86 free_user_info(user_info);
87 return NT_STATUS_NO_MEMORY;
90 (*user_info)->wksta_name.str = talloc_strdup(*user_info, wksta_name);
91 if ((*user_info)->wksta_name.str) {
92 (*user_info)->wksta_name.len = strlen(wksta_name);
94 free_user_info(user_info);
95 return NT_STATUS_NO_MEMORY;
98 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
101 (*user_info)->lm_resp = data_blob_talloc(*user_info,
103 lm_password->length);
105 (*user_info)->nt_resp = data_blob_talloc(*user_info,
107 nt_password->length);
108 if (lm_interactive_password)
109 (*user_info)->lm_interactive_password =
110 data_blob_talloc(*user_info,
111 lm_interactive_password->data,
112 lm_interactive_password->length);
113 if (nt_interactive_password)
114 (*user_info)->nt_interactive_password =
115 data_blob_talloc(*user_info,
116 nt_interactive_password->data,
117 nt_interactive_password->length);
120 (*user_info)->plaintext_password =
121 data_blob_talloc(*user_info,
125 (*user_info)->encrypted = encrypted;
127 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
132 /****************************************************************************
133 Create an auth_usersupplied_data structure after appropriate mapping.
134 ****************************************************************************/
136 NTSTATUS make_user_info_map(TALLOC_CTX *mem_ctx,
137 struct auth_usersupplied_info **user_info,
138 const char *smb_name,
139 const char *client_domain,
140 const char *wksta_name,
141 DATA_BLOB *lm_password, DATA_BLOB *nt_password,
142 DATA_BLOB *lm_interactive_password, DATA_BLOB *nt_interactive_password,
143 DATA_BLOB *plaintext,
148 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
149 client_domain, smb_name, wksta_name));
151 /* don't allow "" as a domain, fixes a Win9X bug
152 where it doens't supply a domain for logon script
153 'net use' commands. */
155 if ( *client_domain )
156 domain = client_domain;
158 domain = lp_workgroup();
160 /* we know that it is a trusted domain (and we are allowing
161 them) or it is our domain */
163 return make_user_info(mem_ctx,
164 user_info, smb_name, smb_name,
165 client_domain, domain, wksta_name,
166 lm_password, nt_password,
167 lm_interactive_password, nt_interactive_password,
168 plaintext, encrypted);
171 /****************************************************************************
172 Create an auth_usersupplied_data, making the DATA_BLOBs here.
173 Decrypt and encrypt the passwords.
174 ****************************************************************************/
176 NTSTATUS make_user_info_netlogon_network(TALLOC_CTX *mem_ctx,
177 struct auth_usersupplied_info **user_info,
178 const char *smb_name,
179 const char *client_domain,
180 const char *wksta_name,
181 const uint8_t *lm_network_password, int lm_password_len,
182 const uint8_t *nt_network_password, int nt_password_len)
185 DATA_BLOB lm_blob = data_blob(lm_network_password, lm_password_len);
186 DATA_BLOB nt_blob = data_blob(nt_network_password, nt_password_len);
188 nt_status = make_user_info_map(mem_ctx,
190 smb_name, client_domain,
192 lm_password_len ? &lm_blob : NULL,
193 nt_password_len ? &nt_blob : NULL,
197 data_blob_free(&lm_blob);
198 data_blob_free(&nt_blob);
202 /****************************************************************************
203 Create an auth_usersupplied_data, making the DATA_BLOBs here.
204 Decrypt and encrypt the passwords.
205 ****************************************************************************/
207 NTSTATUS make_user_info_netlogon_interactive(TALLOC_CTX *mem_ctx,
208 struct auth_usersupplied_info **user_info,
209 const char *smb_name,
210 const char *client_domain,
211 const char *wksta_name,
212 const uint8_t chal[8],
213 const struct samr_Password *lm_interactive_password,
214 const struct samr_Password *nt_interactive_password)
217 DATA_BLOB local_lm_blob;
218 DATA_BLOB local_nt_blob;
220 DATA_BLOB lm_interactive_blob;
221 DATA_BLOB nt_interactive_blob;
222 uint8_t local_lm_response[24];
223 uint8_t local_nt_response[24];
225 SMBOWFencrypt(lm_interactive_password->hash, chal, local_lm_response);
226 SMBOWFencrypt(nt_interactive_password->hash, chal, local_nt_response);
228 local_lm_blob = data_blob(local_lm_response,
229 sizeof(local_lm_response));
230 lm_interactive_blob = data_blob(lm_interactive_password->hash,
231 sizeof(lm_interactive_password->hash));
233 local_nt_blob = data_blob(local_nt_response,
234 sizeof(local_nt_response));
235 nt_interactive_blob = data_blob(nt_interactive_password->hash,
236 sizeof(nt_interactive_password->hash));
238 nt_status = make_user_info_map(mem_ctx,
240 smb_name, client_domain,
244 &lm_interactive_blob,
245 &nt_interactive_blob,
249 data_blob_free(&local_lm_blob);
250 data_blob_free(&local_nt_blob);
251 data_blob_free(&lm_interactive_blob);
252 data_blob_free(&nt_interactive_blob);
255 /****************************************************************************
256 Create an auth_usersupplied_data structure
257 ****************************************************************************/
259 NTSTATUS make_user_info_for_reply_enc(TALLOC_CTX *mem_ctx,
260 struct auth_usersupplied_info **user_info,
261 const char *smb_name,
262 const char *client_domain,
263 const char *remote_machine,
264 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
266 return make_user_info_map(mem_ctx,
270 lm_resp.data ? &lm_resp : NULL,
271 nt_resp.data ? &nt_resp : NULL,
276 /****************************************************************************
277 Create a guest user_info blob, for anonymous authenticaion.
278 ****************************************************************************/
280 BOOL make_user_info_guest(TALLOC_CTX *mem_ctx,
281 struct auth_usersupplied_info **user_info)
285 nt_status = make_user_info(mem_ctx,
295 return NT_STATUS_IS_OK(nt_status) ? True : False;
298 /****************************************************************************
299 prints a NT_USER_TOKEN to debug output.
300 ****************************************************************************/
302 void debug_nt_user_token(int dbg_class, int dbg_lev, const NT_USER_TOKEN *token)
309 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
313 mem_ctx = talloc_init("debug_nt_user_token()");
318 DEBUGC(dbg_class, dbg_lev, ("NT user token of user %s\n",
319 dom_sid_string(mem_ctx, token->user_sids[0]) ));
320 DEBUGADDC(dbg_class, dbg_lev, ("contains %lu SIDs\n", (unsigned long)token->num_sids));
321 for (i = 0; i < token->num_sids; i++)
322 DEBUGADDC(dbg_class, dbg_lev, ("SID[%3lu]: %s\n", (unsigned long)i,
323 dom_sid_string(mem_ctx, token->user_sids[i])));
325 talloc_destroy(mem_ctx);
328 /****************************************************************************
329 prints a NT_USER_TOKEN to debug output.
330 ****************************************************************************/
332 void debug_session_info(int dbg_class, int dbg_lev, const struct auth_session_info *session_info)
335 DEBUGC(dbg_class, dbg_lev, ("Session Info: (NULL)\n"));
339 debug_nt_user_token(dbg_class, dbg_lev, session_info->nt_user_token);
342 /****************************************************************************
343 Create the SID list for this user.
344 ****************************************************************************/
346 NTSTATUS create_nt_user_token(TALLOC_CTX *mem_ctx,
347 struct dom_sid *user_sid, struct dom_sid *group_sid,
348 int n_groupSIDs, struct dom_sid **groupSIDs,
349 BOOL is_guest, struct nt_user_token **token)
351 NTSTATUS nt_status = NT_STATUS_OK;
352 struct nt_user_token *ptoken;
356 if (!(ptoken = talloc_p(mem_ctx, struct nt_user_token))) {
357 DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
358 nt_status = NT_STATUS_NO_MEMORY;
362 ptoken->num_sids = 0;
364 if (!(ptoken->user_sids = talloc_array_p(mem_ctx, struct dom_sid*, n_groupSIDs + 5))) {
365 DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
366 nt_status = NT_STATUS_NO_MEMORY;
371 * Note - user SID *MUST* be first in token !
372 * se_access_check depends on this.
374 * Primary group SID is second in token. Convention.
377 ptoken->user_sids[PRIMARY_USER_SID_INDEX] = user_sid;
379 ptoken->user_sids[PRIMARY_GROUP_SID_INDEX] = group_sid;
383 * Finally add the "standard" SIDs.
384 * The only difference between guest and "anonymous" (which we
385 * don't really support) is the addition of Authenticated_Users.
387 ptoken->user_sids[2] = dom_sid_parse_talloc(mem_ctx, SID_WORLD);
388 ptoken->user_sids[3] = dom_sid_parse_talloc(mem_ctx, SID_NETWORK);
391 ptoken->user_sids[4] = dom_sid_parse_talloc(mem_ctx, SID_BUILTIN_GUESTS);
394 ptoken->user_sids[4] = dom_sid_parse_talloc(mem_ctx, SID_AUTHENTICATED_USERS);
398 sid_ndx = 5; /* next available spot */
400 for (i = 0; i < n_groupSIDs; i++) {
401 size_t check_sid_idx;
402 for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
403 if (sid_equal(ptoken->user_sids[check_sid_idx],
409 if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
410 ptoken->user_sids[sid_ndx++] = groupSIDs[i];
415 debug_nt_user_token(DBGC_AUTH, 10, ptoken);
422 /***************************************************************************
423 Make a user_info struct
424 ***************************************************************************/
426 NTSTATUS make_server_info(const TALLOC_CTX *mem_ctx,
427 struct auth_serversupplied_info **server_info,
428 const char *username)
430 *server_info = talloc_p(mem_ctx, struct auth_serversupplied_info);
432 return NT_STATUS_NO_MEMORY;
434 ZERO_STRUCTP(*server_info);
439 /***************************************************************************
440 Make (and fill) a user_info struct for a guest login.
441 ***************************************************************************/
442 NTSTATUS make_server_info_guest(TALLOC_CTX *mem_ctx, struct auth_serversupplied_info **server_info)
446 nt_status = make_server_info(mem_ctx, server_info, "");
448 if (!NT_STATUS_IS_OK(nt_status)) {
452 (*server_info)->guest = True;
454 (*server_info)->user_sid = dom_sid_parse_talloc((*server_info), SID_ANONYMOUS);
455 (*server_info)->primary_group_sid = dom_sid_parse_talloc((*server_info), SID_BUILTIN_GUESTS);
456 (*server_info)->n_domain_groups = 0;
457 (*server_info)->domain_groups = NULL;
459 /* annoying, but the Guest really does have a session key,
460 and it is all zeros! */
461 (*server_info)->user_session_key = data_blob_talloc(*server_info, NULL, 16);
462 (*server_info)->lm_session_key = data_blob_talloc(*server_info, NULL, 16);
464 data_blob_clear(&(*server_info)->user_session_key);
465 data_blob_clear(&(*server_info)->lm_session_key);
467 (*server_info)->account_name = "";
468 (*server_info)->domain = "";
469 (*server_info)->full_name = "Anonymous";
470 (*server_info)->logon_script = "";
471 (*server_info)->profile_path = "";
472 (*server_info)->home_directory = "";
473 (*server_info)->home_drive = "";
475 (*server_info)->last_logon = 0;
476 (*server_info)->last_logoff = 0;
477 (*server_info)->acct_expiry = 0;
478 (*server_info)->last_password_change = 0;
479 (*server_info)->allow_password_change = 0;
480 (*server_info)->force_password_change = 0;
482 (*server_info)->logon_count = 0;
483 (*server_info)->bad_password_count = 0;
485 (*server_info)->acct_flags = ACB_NORMAL;
490 /***************************************************************************
491 Make a server_info struct from the info3 returned by a domain logon
492 ***************************************************************************/
494 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
495 const char *internal_username,
496 struct auth_serversupplied_info **server_info,
497 struct netr_SamInfo3 *info3)
501 nt_status = make_server_info(mem_ctx, server_info, internal_username);
503 if (!NT_STATUS_IS_OK(nt_status)) {
507 (*server_info)->guest = False;
510 Here is where we should check the list of
511 trusted domains, and verify that the SID
515 (*server_info)->user_sid = dom_sid_add_rid(*server_info, dom_sid_dup(*server_info, info3->base.domain_sid), info3->base.rid);
516 (*server_info)->primary_group_sid = dom_sid_add_rid(*server_info, dom_sid_dup(*server_info, info3->base.domain_sid), info3->base.primary_gid);
518 /* TODO: pull in other groups: */
521 (*server_info)->domain_groups = talloc_array_p((*server_info), struct dom_sid*, info3->base.group_count);
522 if (!(*server_info)->domain_groups) {
523 return NT_STATUS_NO_MEMORY;
526 for ((*server_info)->n_domain_groups = 0;
527 (*server_info)->n_domain_groups < info3->base.group_count;
528 (*server_info)->n_domain_groups++) {
530 sid = dom_sid_dup(*server_info, info3->base.domain_sid);
532 return NT_STATUS_NO_MEMORY;
534 (*server_info)->domain_groups[(*server_info)->n_domain_groups]
535 = dom_sid_add_rid(*server_info, sid,
536 info3->base.groupids[(*server_info)->n_domain_groups].rid);
537 if (!(*server_info)->domain_groups[(*server_info)->n_domain_groups]) {
538 return NT_STATUS_NO_MEMORY;
542 if (info3->base.account_name.string) {
543 (*server_info)->account_name = talloc_reference(*server_info, info3->base.account_name.string);
545 (*server_info)->account_name = talloc_strdup(*server_info, internal_username);
548 if (info3->base.domain.string) {
549 (*server_info)->domain = talloc_reference(*server_info, info3->base.domain.string);
551 (*server_info)->domain = NULL;
554 if (info3->base.full_name.string) {
555 (*server_info)->full_name = talloc_reference(*server_info, info3->base.full_name.string);
557 (*server_info)->full_name = NULL;
560 if (info3->base.logon_script.string) {
561 (*server_info)->logon_script = talloc_reference(*server_info, info3->base.logon_script.string);
563 (*server_info)->logon_script = NULL;
566 if (info3->base.profile_path.string) {
567 (*server_info)->profile_path = talloc_reference(*server_info, info3->base.profile_path.string);
569 (*server_info)->profile_path = NULL;
572 if (info3->base.home_directory.string) {
573 (*server_info)->home_directory = talloc_reference(*server_info, info3->base.home_directory.string);
575 (*server_info)->home_directory = NULL;
578 if (info3->base.home_drive.string) {
579 (*server_info)->home_drive = talloc_reference(*server_info, info3->base.home_drive.string);
581 (*server_info)->home_drive = NULL;
583 (*server_info)->last_logon = info3->base.last_logon;
584 (*server_info)->last_logoff = info3->base.last_logoff;
585 (*server_info)->acct_expiry = info3->base.acct_expiry;
586 (*server_info)->last_password_change = info3->base.last_password_change;
587 (*server_info)->allow_password_change = info3->base.allow_password_change;
588 (*server_info)->force_password_change = info3->base.force_password_change;
590 (*server_info)->logon_count = info3->base.logon_count;
591 (*server_info)->bad_password_count = info3->base.bad_password_count;
593 (*server_info)->acct_flags = info3->base.acct_flags;
595 /* ensure we are never given NULL session keys */
597 if (all_zero(info3->base.key.key, sizeof(info3->base.key.key))) {
598 (*server_info)->user_session_key = data_blob(NULL, 0);
600 (*server_info)->user_session_key = data_blob_talloc((*server_info), info3->base.key.key, sizeof(info3->base.key.key));
603 if (all_zero(info3->base.LMSessKey.key, sizeof(info3->base.LMSessKey.key))) {
604 (*server_info)->lm_session_key = data_blob(NULL, 0);
606 (*server_info)->lm_session_key = data_blob_talloc((*server_info), info3->base.LMSessKey.key, sizeof(info3->base.LMSessKey.key));
611 /***************************************************************************
612 Free a user_info struct
613 ***************************************************************************/
615 void free_user_info(struct auth_usersupplied_info **user_info)
617 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
619 data_blob_clear(&(*user_info)->plaintext_password);
622 talloc_free(*user_info);
626 /***************************************************************************
627 Clear out a server_info struct that has been allocated
628 ***************************************************************************/
630 void free_server_info(struct auth_serversupplied_info **server_info)
632 DEBUG(5,("attempting to free a server_info structure\n"));
633 talloc_free(*server_info);
637 /***************************************************************************
638 Make an auth_methods struct
639 ***************************************************************************/
641 BOOL make_auth_methods(struct auth_context *auth_context, struct auth_methods **auth_method)
644 smb_panic("no auth_context supplied to make_auth_methods()!\n");
648 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
651 *auth_method = talloc_p(auth_context, struct auth_methods);
655 ZERO_STRUCTP(*auth_method);
660 NTSTATUS make_session_info(TALLOC_CTX *mem_ctx,
661 struct auth_serversupplied_info *server_info,
662 struct auth_session_info **session_info)
666 *session_info = talloc_p(mem_ctx, struct auth_session_info);
667 if (!*session_info) {
668 return NT_STATUS_NO_MEMORY;
671 (*session_info)->server_info = server_info;
672 talloc_reference(*session_info, (*session_info)->server_info);
674 /* unless set otherwise, the session key is the user session
675 * key from the auth subsystem */
677 (*session_info)->session_key = server_info->user_session_key;
679 nt_status = create_nt_user_token((*session_info),
680 server_info->user_sid,
681 server_info->primary_group_sid,
682 server_info->n_domain_groups,
683 server_info->domain_groups,
685 &(*session_info)->nt_user_token);
690 /***************************************************************************
691 Clear out a server_info struct that has been allocated
692 ***************************************************************************/
694 void free_session_info(struct auth_session_info **session_info)
696 DEBUG(5,("attempting to free a session_info structure\n"));
697 talloc_free((*session_info));
698 *session_info = NULL;
702 * Squash an NT_STATUS in line with security requirements.
703 * In an attempt to avoid giving the whole game away when users
704 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
705 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
706 * (session setups in particular).
708 * @param nt_status NTSTATUS input for squashing.
709 * @return the 'squashed' nt_status
712 NTSTATUS nt_status_squash(NTSTATUS nt_status)
714 if NT_STATUS_IS_OK(nt_status) {
716 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
717 /* Match WinXP and don't give the game away */
718 return NT_STATUS_LOGON_FAILURE;
720 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
721 /* Match WinXP and don't give the game away */
722 return NT_STATUS_LOGON_FAILURE;