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.
27 #define DBGC_CLASS DBGC_AUTH
29 /****************************************************************************
30 Create an auth_usersupplied_data structure
31 ****************************************************************************/
32 static NTSTATUS make_user_info(struct auth_usersupplied_info **user_info,
34 const char *internal_username,
35 const char *client_domain,
37 const char *wksta_name,
38 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
39 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
44 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
46 *user_info = malloc(sizeof(**user_info));
48 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
49 return NT_STATUS_NO_MEMORY;
52 ZERO_STRUCTP(*user_info);
54 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
56 (*user_info)->smb_name.str = strdup(smb_name);
57 if ((*user_info)->smb_name.str) {
58 (*user_info)->smb_name.len = strlen(smb_name);
60 free_user_info(user_info);
61 return NT_STATUS_NO_MEMORY;
64 (*user_info)->internal_username.str = strdup(internal_username);
65 if ((*user_info)->internal_username.str) {
66 (*user_info)->internal_username.len = strlen(internal_username);
68 free_user_info(user_info);
69 return NT_STATUS_NO_MEMORY;
72 (*user_info)->domain.str = strdup(domain);
73 if ((*user_info)->domain.str) {
74 (*user_info)->domain.len = strlen(domain);
76 free_user_info(user_info);
77 return NT_STATUS_NO_MEMORY;
80 (*user_info)->client_domain.str = strdup(client_domain);
81 if ((*user_info)->client_domain.str) {
82 (*user_info)->client_domain.len = strlen(client_domain);
84 free_user_info(user_info);
85 return NT_STATUS_NO_MEMORY;
88 (*user_info)->wksta_name.str = strdup(wksta_name);
89 if ((*user_info)->wksta_name.str) {
90 (*user_info)->wksta_name.len = strlen(wksta_name);
92 free_user_info(user_info);
93 return NT_STATUS_NO_MEMORY;
96 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
99 (*user_info)->lm_resp = data_blob(lm_pwd->data, lm_pwd->length);
101 (*user_info)->nt_resp = data_blob(nt_pwd->data, nt_pwd->length);
102 if (lm_interactive_pwd)
103 (*user_info)->lm_interactive_pwd = data_blob(lm_interactive_pwd->data, lm_interactive_pwd->length);
104 if (nt_interactive_pwd)
105 (*user_info)->nt_interactive_pwd = data_blob(nt_interactive_pwd->data, nt_interactive_pwd->length);
108 (*user_info)->plaintext_password = data_blob(plaintext->data, plaintext->length);
110 (*user_info)->encrypted = encrypted;
112 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
117 /****************************************************************************
118 Create an auth_usersupplied_data structure after appropriate mapping.
119 ****************************************************************************/
121 NTSTATUS make_user_info_map(struct auth_usersupplied_info **user_info,
122 const char *smb_name,
123 const char *client_domain,
124 const char *wksta_name,
125 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
126 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
127 DATA_BLOB *plaintext,
131 fstring internal_username;
132 fstrcpy(internal_username, smb_name);
134 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
135 client_domain, smb_name, wksta_name));
137 /* don't allow "" as a domain, fixes a Win9X bug
138 where it doens't supply a domain for logon script
139 'net use' commands. */
141 if ( *client_domain )
142 domain = client_domain;
144 domain = lp_workgroup();
146 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
148 return make_user_info(user_info, smb_name, internal_username,
149 client_domain, domain, wksta_name,
151 lm_interactive_pwd, nt_interactive_pwd,
152 plaintext, encrypted);
155 /****************************************************************************
156 Create an auth_usersupplied_data, making the DATA_BLOBs here.
157 Decrypt and encrypt the passwords.
158 ****************************************************************************/
160 BOOL make_user_info_netlogon_network(struct auth_usersupplied_info **user_info,
161 const char *smb_name,
162 const char *client_domain,
163 const char *wksta_name,
164 const uint8_t *lm_network_pwd, int lm_pwd_len,
165 const uint8_t *nt_network_pwd, int nt_pwd_len)
169 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
170 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
172 nt_status = make_user_info_map(user_info,
173 smb_name, client_domain,
175 lm_pwd_len ? &lm_blob : NULL,
176 nt_pwd_len ? &nt_blob : NULL,
180 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
182 data_blob_free(&lm_blob);
183 data_blob_free(&nt_blob);
187 /****************************************************************************
188 Create an auth_usersupplied_data, making the DATA_BLOBs here.
189 Decrypt and encrypt the passwords.
190 ****************************************************************************/
192 BOOL make_user_info_netlogon_interactive(struct auth_usersupplied_info **user_info,
193 const char *smb_name,
194 const char *client_domain,
195 const char *wksta_name,
196 const uint8_t chal[8],
197 const uint8_t lm_interactive_pwd[16],
198 const uint8_t nt_interactive_pwd[16],
199 const uint8_t *dc_sess_key)
203 uint8_t local_lm_response[24];
204 uint8_t local_nt_response[24];
208 memcpy(key, dc_sess_key, 8);
210 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
211 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
213 #ifdef DEBUG_PASSWORD
215 dump_data(100, (char *)key, sizeof(key));
217 DEBUG(100,("lm owf password:"));
218 dump_data(100, lm_pwd, sizeof(lm_pwd));
220 DEBUG(100,("nt owf password:"));
221 dump_data(100, nt_pwd, sizeof(nt_pwd));
224 if (lm_interactive_pwd)
225 arcfour_crypt((uint8_t *)lm_pwd, key, sizeof(lm_pwd));
227 if (nt_interactive_pwd)
228 arcfour_crypt((uint8_t *)nt_pwd, key, sizeof(nt_pwd));
230 #ifdef DEBUG_PASSWORD
231 DEBUG(100,("decrypt of lm owf password:"));
232 dump_data(100, lm_pwd, sizeof(lm_pwd));
234 DEBUG(100,("decrypt of nt owf password:"));
235 dump_data(100, nt_pwd, sizeof(nt_pwd));
238 if (lm_interactive_pwd)
239 SMBOWFencrypt((const uint8_t *)lm_pwd, chal, local_lm_response);
241 if (nt_interactive_pwd)
242 SMBOWFencrypt((const uint8_t *)nt_pwd, chal, local_nt_response);
244 /* Password info paranoia */
250 DATA_BLOB local_lm_blob;
251 DATA_BLOB local_nt_blob;
253 DATA_BLOB lm_interactive_blob;
254 DATA_BLOB nt_interactive_blob;
256 if (lm_interactive_pwd) {
257 local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
258 lm_interactive_blob = data_blob(lm_pwd, sizeof(lm_pwd));
262 if (nt_interactive_pwd) {
263 local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
264 nt_interactive_blob = data_blob(nt_pwd, sizeof(nt_pwd));
268 nt_status = make_user_info_map(user_info,
269 smb_name, client_domain,
271 lm_interactive_pwd ? &local_lm_blob : NULL,
272 nt_interactive_pwd ? &local_nt_blob : NULL,
273 lm_interactive_pwd ? &lm_interactive_blob : NULL,
274 nt_interactive_pwd ? &nt_interactive_blob : NULL,
278 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
279 data_blob_free(&local_lm_blob);
280 data_blob_free(&local_nt_blob);
281 data_blob_free(&lm_interactive_blob);
282 data_blob_free(&nt_interactive_blob);
288 /****************************************************************************
289 Create an auth_usersupplied_data structure
290 ****************************************************************************/
292 BOOL make_user_info_for_reply(struct auth_usersupplied_info **user_info,
293 const char *smb_name,
294 const char *client_domain,
295 const uint8_t chal[8],
296 DATA_BLOB plaintext_password)
299 DATA_BLOB local_lm_blob;
300 DATA_BLOB local_nt_blob;
301 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
304 * Not encrypted - do so.
307 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
309 if (plaintext_password.data) {
310 uint8_t local_lm_response[24];
312 #ifdef DEBUG_PASSWORD
313 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
314 dump_data(100, plaintext_password.data, plaintext_password.length);
317 SMBencrypt( (const char *)plaintext_password.data, (const uint8_t *)chal, local_lm_response);
318 local_lm_blob = data_blob(local_lm_response, 24);
320 /* We can't do an NT hash here, as the password needs to be
322 local_nt_blob = data_blob(NULL, 0);
325 local_lm_blob = data_blob(NULL, 0);
326 local_nt_blob = data_blob(NULL, 0);
329 ret = make_user_info_map(user_info, smb_name,
331 sub_get_remote_machine(),
332 local_lm_blob.data ? &local_lm_blob : NULL,
333 local_nt_blob.data ? &local_nt_blob : NULL,
335 plaintext_password.data ? &plaintext_password : NULL,
338 data_blob_free(&local_lm_blob);
339 return NT_STATUS_IS_OK(ret) ? True : False;
342 /****************************************************************************
343 Create an auth_usersupplied_data structure
344 ****************************************************************************/
346 NTSTATUS make_user_info_for_reply_enc(struct auth_usersupplied_info **user_info,
347 const char *smb_name,
348 const char *client_domain,
349 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
351 return make_user_info_map(user_info, smb_name,
353 sub_get_remote_machine(),
354 lm_resp.data ? &lm_resp : NULL,
355 nt_resp.data ? &nt_resp : NULL,
360 /****************************************************************************
361 Create a guest user_info blob, for anonymous authenticaion.
362 ****************************************************************************/
364 BOOL make_user_info_guest(struct auth_usersupplied_info **user_info)
368 nt_status = make_user_info(user_info,
377 return NT_STATUS_IS_OK(nt_status) ? True : False;
380 /****************************************************************************
381 prints a NT_USER_TOKEN to debug output.
382 ****************************************************************************/
384 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
391 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
395 mem_ctx = talloc_init("debug_nt_user_token()");
400 DEBUGC(dbg_class, dbg_lev, ("NT user token of user %s\n",
401 dom_sid_string(mem_ctx, token->user_sids[0]) ));
402 DEBUGADDC(dbg_class, dbg_lev, ("contains %lu SIDs\n", (unsigned long)token->num_sids));
403 for (i = 0; i < token->num_sids; i++)
404 DEBUGADDC(dbg_class, dbg_lev, ("SID[%3lu]: %s\n", (unsigned long)i,
405 dom_sid_string(mem_ctx, token->user_sids[i])));
407 talloc_destroy(mem_ctx);
410 /****************************************************************************
411 Create the SID list for this user.
412 ****************************************************************************/
414 NTSTATUS create_nt_user_token(TALLOC_CTX *mem_ctx,
415 struct dom_sid *user_sid, struct dom_sid *group_sid,
416 int n_groupSIDs, struct dom_sid **groupSIDs,
417 BOOL is_guest, struct nt_user_token **token)
419 NTSTATUS nt_status = NT_STATUS_OK;
420 struct nt_user_token *ptoken;
424 if (!(ptoken = talloc_p(mem_ctx, struct nt_user_token))) {
425 DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
426 nt_status = NT_STATUS_NO_MEMORY;
430 ptoken->num_sids = 0;
432 if (!(ptoken->user_sids = talloc_array_p(mem_ctx, struct dom_sid*, n_groupSIDs + 5))) {
433 DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
434 nt_status = NT_STATUS_NO_MEMORY;
439 * Note - user SID *MUST* be first in token !
440 * se_access_check depends on this.
442 * Primary group SID is second in token. Convention.
445 ptoken->user_sids[PRIMARY_USER_SID_INDEX] = user_sid;
447 ptoken->user_sids[PRIMARY_GROUP_SID_INDEX] = group_sid;
451 * Finally add the "standard" SIDs.
452 * The only difference between guest and "anonymous" (which we
453 * don't really support) is the addition of Authenticated_Users.
455 ptoken->user_sids[2] = dom_sid_parse_talloc(mem_ctx, SID_WORLD);
456 ptoken->user_sids[3] = dom_sid_parse_talloc(mem_ctx, SID_NETWORK);
459 ptoken->user_sids[4] = dom_sid_parse_talloc(mem_ctx, SID_BUILTIN_GUESTS);
462 ptoken->user_sids[4] = dom_sid_parse_talloc(mem_ctx, SID_AUTHENTICATED_USERS);
466 sid_ndx = 5; /* next available spot */
468 for (i = 0; i < n_groupSIDs; i++) {
469 size_t check_sid_idx;
470 for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
471 if (sid_equal(ptoken->user_sids[check_sid_idx],
477 if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
478 ptoken->user_sids[sid_ndx++] = groupSIDs[i];
483 debug_nt_user_token(DBGC_AUTH, 10, ptoken);
490 /***************************************************************************
491 Make a user_info struct
492 ***************************************************************************/
494 NTSTATUS make_server_info(struct auth_serversupplied_info **server_info,
495 const char *username)
497 TALLOC_CTX *mem_ctx = talloc_init("auth subsystem: server_info for %s", username);
498 *server_info = talloc_p(mem_ctx, struct auth_serversupplied_info);
500 DEBUG(0,("make_server_info: malloc failed!\n"));
501 talloc_destroy(mem_ctx);
502 return NT_STATUS_NO_MEMORY;
504 (*server_info)->mem_ctx = mem_ctx;
509 /***************************************************************************
510 Make (and fill) a user_info struct for a guest login.
511 ***************************************************************************/
512 NTSTATUS make_server_info_guest(struct auth_serversupplied_info **server_info)
515 static const char zeros[16];
517 nt_status = make_server_info(server_info, "");
519 if (!NT_STATUS_IS_OK(nt_status)) {
523 (*server_info)->guest = True;
525 (*server_info)->user_sid = dom_sid_parse_talloc((*server_info)->mem_ctx, SID_ANONYMOUS);
526 (*server_info)->primary_group_sid = dom_sid_parse_talloc((*server_info)->mem_ctx, SID_BUILTIN_GUESTS);
527 (*server_info)->n_domain_groups = 0;
528 (*server_info)->domain_groups = NULL;
530 /* annoying, but the Guest really does have a session key,
531 and it is all zeros! */
532 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
533 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
538 /***************************************************************************
539 Free a user_info struct
540 ***************************************************************************/
542 void free_user_info(struct auth_usersupplied_info **user_info)
544 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
545 if (*user_info != NULL) {
546 if ((*user_info)->smb_name.str) {
547 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
549 SAFE_FREE((*user_info)->smb_name.str);
550 SAFE_FREE((*user_info)->internal_username.str);
551 SAFE_FREE((*user_info)->client_domain.str);
552 SAFE_FREE((*user_info)->domain.str);
553 SAFE_FREE((*user_info)->wksta_name.str);
554 data_blob_free(&(*user_info)->lm_resp);
555 data_blob_free(&(*user_info)->nt_resp);
556 data_blob_clear_free(&(*user_info)->plaintext_password);
557 ZERO_STRUCT(**user_info);
559 SAFE_FREE(*user_info);
562 /***************************************************************************
563 Clear out a server_info struct that has been allocated
564 ***************************************************************************/
566 void free_server_info(struct auth_serversupplied_info **server_info)
568 DEBUG(5,("attempting to free a server_info structure\n"));
570 talloc_destroy((*server_info)->mem_ctx);
575 /***************************************************************************
576 Make an auth_methods struct
577 ***************************************************************************/
579 BOOL make_auth_methods(struct auth_context *auth_context, struct auth_methods **auth_method)
582 smb_panic("no auth_context supplied to make_auth_methods()!\n");
586 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
589 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
591 DEBUG(0,("make_auth_method: malloc failed!\n"));
594 ZERO_STRUCTP(*auth_method);
599 NTSTATUS make_session_info(struct auth_serversupplied_info *server_info,
600 struct auth_session_info **session_info)
604 *session_info = talloc_p(server_info->mem_ctx, struct auth_session_info);
605 if (!*session_info) {
606 return NT_STATUS_NO_MEMORY;
609 (*session_info)->mem_ctx = server_info->mem_ctx;
610 server_info->mem_ctx = NULL; /* make sure not to accidentily destory it,
611 and this information is now constant */
612 (*session_info)->server_info = server_info;
614 /* unless set otherwise, the session key is the user session
615 * key from the auth subsystem */
617 (*session_info)->session_key = server_info->user_session_key;
619 nt_status = create_nt_user_token((*session_info)->mem_ctx,
620 server_info->user_sid,
621 server_info->primary_group_sid,
622 server_info->n_domain_groups,
623 server_info->domain_groups,
625 &(*session_info)->nt_user_token);
631 * Squash an NT_STATUS in line with security requirements.
632 * In an attempt to avoid giving the whole game away when users
633 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
634 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
635 * (session setups in particular).
637 * @param nt_status NTSTATUS input for squashing.
638 * @return the 'squashed' nt_status
641 NTSTATUS nt_status_squash(NTSTATUS nt_status)
643 if NT_STATUS_IS_OK(nt_status) {
645 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
646 /* Match WinXP and don't give the game away */
647 return NT_STATUS_LOGON_FAILURE;
649 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
650 /* Match WinXP and don't give the game away */
651 return NT_STATUS_LOGON_FAILURE;