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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DBGC_CLASS DBGC_AUTH
28 extern fstring remote_machine;
29 extern pstring global_myname;
31 /****************************************************************************
32 Create a UNIX user on demand.
33 ****************************************************************************/
35 static int smb_create_user(const char *unix_user, const char *homedir)
40 pstrcpy(add_script, lp_adduser_script());
43 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
45 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
46 ret = smbrun(add_script,NULL);
47 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
51 /****************************************************************************
52 Add and Delete UNIX users on demand, based on NTSTATUS codes.
53 ****************************************************************************/
55 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status)
57 struct passwd *pwd=NULL;
59 if (NT_STATUS_IS_OK(nt_status)) {
61 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
64 * User validated ok against Domain controller.
65 * If the admin wants us to try and create a UNIX
66 * user on the fly, do so.
69 if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
70 smb_create_user(user_info->internal_username.str, NULL);
76 /****************************************************************************
77 Create an auth_usersupplied_data structure
78 ****************************************************************************/
80 static BOOL make_user_info(auth_usersupplied_info **user_info,
82 const char *internal_username,
83 const char *client_domain,
85 const char *wksta_name,
86 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
88 uint32 auth_flags, BOOL encrypted)
91 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
93 *user_info = malloc(sizeof(**user_info));
95 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
99 ZERO_STRUCTP(*user_info);
101 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
103 (*user_info)->smb_name.str = strdup(smb_name);
104 if ((*user_info)->smb_name.str) {
105 (*user_info)->smb_name.len = strlen(smb_name);
107 free_user_info(user_info);
111 (*user_info)->internal_username.str = strdup(internal_username);
112 if ((*user_info)->internal_username.str) {
113 (*user_info)->internal_username.len = strlen(internal_username);
115 free_user_info(user_info);
119 (*user_info)->domain.str = strdup(domain);
120 if ((*user_info)->domain.str) {
121 (*user_info)->domain.len = strlen(domain);
123 free_user_info(user_info);
127 (*user_info)->client_domain.str = strdup(client_domain);
128 if ((*user_info)->client_domain.str) {
129 (*user_info)->client_domain.len = strlen(client_domain);
131 free_user_info(user_info);
135 (*user_info)->wksta_name.str = strdup(wksta_name);
136 if ((*user_info)->wksta_name.str) {
137 (*user_info)->wksta_name.len = strlen(wksta_name);
139 free_user_info(user_info);
143 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
145 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
146 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
147 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
149 (*user_info)->encrypted = encrypted;
150 (*user_info)->auth_flags = auth_flags;
152 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
157 /****************************************************************************
158 Create an auth_usersupplied_data structure after appropriate mapping.
159 ****************************************************************************/
161 BOOL make_user_info_map(auth_usersupplied_info **user_info,
162 const char *smb_name,
163 const char *client_domain,
164 const char *wksta_name,
165 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
167 uint32 ntlmssp_flags, BOOL encrypted)
170 fstring internal_username;
171 fstrcpy(internal_username, smb_name);
172 map_username(internal_username);
174 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
175 client_domain, smb_name, wksta_name));
177 if (lp_allow_trusted_domains() && *client_domain) {
179 /* the client could have given us a workstation name
180 or other crap for the workgroup - we really need a
181 way of telling if this domain name is one of our
184 Also don't allow "" as a domain, fixes a Win9X bug
185 where it doens't supply a domain for logon script
188 The way I do it here is by checking if the fully
189 qualified username exists. This is rather reliant
190 on winbind, but until we have a better method this
194 domain = client_domain;
196 if ((smb_name) && (*smb_name)) { /* Don't do this for guests */
198 if (asprintf(&user, "%s%s%s",
199 client_domain, lp_winbind_separator(),
201 DEBUG(0, ("make_user_info_map: asprintf() failed!\n"));
205 DEBUG(5, ("make_user_info_map: testing for user %s\n", user));
207 if (Get_Pwnam(user) == NULL) {
208 DEBUG(5, ("make_user_info_map: test for user %s failed\n", user));
209 domain = lp_workgroup();
210 DEBUG(5, ("make_user_info_map: trusted domain %s doesn't appear to exist, using %s\n",
211 client_domain, domain));
213 DEBUG(5, ("make_user_info_map: using trusted domain %s\n", domain));
218 domain = lp_workgroup();
221 return make_user_info(user_info,
222 smb_name, internal_username,
223 client_domain, domain,
227 ntlmssp_flags, encrypted);
231 /****************************************************************************
232 Create an auth_usersupplied_data, making the DATA_BLOBs here.
233 Decrypt and encrypt the passwords.
234 ****************************************************************************/
236 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
237 const char *smb_name,
238 const char *client_domain,
239 const char *wksta_name,
240 const uchar *lm_network_pwd, int lm_pwd_len,
241 const uchar *nt_network_pwd, int nt_pwd_len)
244 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
245 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
246 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
247 uint32 auth_flags = AUTH_FLAG_NONE;
250 auth_flags |= AUTH_FLAG_LM_RESP;
251 if (nt_pwd_len == 24) {
252 auth_flags |= AUTH_FLAG_NTLM_RESP;
253 } else if (nt_pwd_len != 0) {
254 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
257 ret = make_user_info_map(user_info,
258 smb_name, client_domain,
264 data_blob_free(&lm_blob);
265 data_blob_free(&nt_blob);
269 /****************************************************************************
270 Create an auth_usersupplied_data, making the DATA_BLOBs here.
271 Decrypt and encrypt the passwords.
272 ****************************************************************************/
274 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
275 const char *smb_name,
276 const char *client_domain,
277 const char *wksta_name,
279 const uchar lm_interactive_pwd[16],
280 const uchar nt_interactive_pwd[16],
281 const uchar *dc_sess_key)
285 unsigned char local_lm_response[24];
286 unsigned char local_nt_response[24];
287 unsigned char key[16];
288 uint32 auth_flags = AUTH_FLAG_NONE;
291 memcpy(key, dc_sess_key, 8);
293 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
294 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
296 #ifdef DEBUG_PASSWORD
298 dump_data(100, (char *)key, sizeof(key));
300 DEBUG(100,("lm owf password:"));
301 dump_data(100, lm_pwd, sizeof(lm_pwd));
303 DEBUG(100,("nt owf password:"));
304 dump_data(100, nt_pwd, sizeof(nt_pwd));
307 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
308 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
310 #ifdef DEBUG_PASSWORD
311 DEBUG(100,("decrypt of lm owf password:"));
312 dump_data(100, lm_pwd, sizeof(lm_pwd));
314 DEBUG(100,("decrypt of nt owf password:"));
315 dump_data(100, nt_pwd, sizeof(nt_pwd));
318 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
319 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
321 /* Password info paranoia */
328 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
329 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
330 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
332 if (lm_interactive_pwd)
333 auth_flags |= AUTH_FLAG_LM_RESP;
334 if (nt_interactive_pwd)
335 auth_flags |= AUTH_FLAG_NTLM_RESP;
337 ret = make_user_info_map(user_info,
338 smb_name, client_domain,
345 data_blob_free(&local_lm_blob);
346 data_blob_free(&local_nt_blob);
352 /****************************************************************************
353 Create an auth_usersupplied_data structure
354 ****************************************************************************/
356 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
357 const char *smb_name,
358 const char *client_domain,
360 DATA_BLOB plaintext_password)
363 DATA_BLOB local_lm_blob;
364 DATA_BLOB local_nt_blob;
366 uint32 auth_flags = AUTH_FLAG_NONE;
369 * Not encrypted - do so.
372 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
374 if (plaintext_password.data) {
375 unsigned char local_lm_response[24];
377 #ifdef DEBUG_PASSWORD
378 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
379 dump_data(100, plaintext_password.data, plaintext_password.length);
382 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
383 local_lm_blob = data_blob(local_lm_response, 24);
385 /* We can't do an NT hash here, as the password needs to be
387 local_nt_blob = data_blob(NULL, 0);
389 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
391 local_lm_blob = data_blob(NULL, 0);
392 local_nt_blob = data_blob(NULL, 0);
395 ret = make_user_info_map(user_info, smb_name,
403 data_blob_free(&local_lm_blob);
407 /****************************************************************************
408 Create an auth_usersupplied_data structure
409 ****************************************************************************/
411 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
412 const char *smb_name,
413 const char *client_domain,
414 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
416 uint32 auth_flags = AUTH_FLAG_NONE;
418 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
420 if (lm_resp.length == 24) {
421 auth_flags |= AUTH_FLAG_LM_RESP;
423 if (nt_resp.length == 0) {
424 } else if (nt_resp.length == 24) {
425 auth_flags |= AUTH_FLAG_NTLM_RESP;
427 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
430 return make_user_info_map(user_info, smb_name,
439 /****************************************************************************
440 Create a guest user_info blob, for anonymous authenticaion.
441 ****************************************************************************/
443 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
445 DATA_BLOB lm_blob = data_blob(NULL, 0);
446 DATA_BLOB nt_blob = data_blob(NULL, 0);
447 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
448 uint32 auth_flags = AUTH_FLAG_NONE;
450 return make_user_info(user_info,
459 /***************************************************************************
460 Make a user_info struct
461 ***************************************************************************/
463 static BOOL make_server_info(auth_serversupplied_info **server_info)
465 *server_info = malloc(sizeof(**server_info));
467 DEBUG(0,("make_server_info: malloc failed!\n"));
470 ZERO_STRUCTP(*server_info);
474 /***************************************************************************
475 Make (and fill) a user_info struct from a SAM_ACCOUNT
476 ***************************************************************************/
478 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
480 if (!make_server_info(server_info)) {
484 (*server_info)->sam_fill_level = SAM_FILL_ALL;
485 (*server_info)->sam_account = sampass;
487 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
488 pdb_get_username((*server_info)->sam_account)));
492 /***************************************************************************
493 Make (and fill) a user_info struct from a 'struct passwd' by conversion
495 ***************************************************************************/
497 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
499 SAM_ACCOUNT *sampass = NULL;
500 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {
503 return make_server_info_sam(server_info, sampass);
506 /***************************************************************************
507 Free a user_info struct
508 ***************************************************************************/
510 void free_user_info(auth_usersupplied_info **user_info)
512 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
513 if (*user_info != NULL) {
514 if ((*user_info)->smb_name.str) {
515 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
517 SAFE_FREE((*user_info)->smb_name.str);
518 SAFE_FREE((*user_info)->internal_username.str);
519 SAFE_FREE((*user_info)->client_domain.str);
520 SAFE_FREE((*user_info)->domain.str);
521 SAFE_FREE((*user_info)->wksta_name.str);
522 data_blob_free(&(*user_info)->lm_resp);
523 data_blob_free(&(*user_info)->nt_resp);
524 SAFE_FREE((*user_info)->interactive_password);
525 data_blob_clear_free(&(*user_info)->plaintext_password);
526 ZERO_STRUCT(**user_info);
528 SAFE_FREE(*user_info);
531 /***************************************************************************
532 Clear out a server_info struct that has been allocated
533 ***************************************************************************/
535 void free_server_info(auth_serversupplied_info **server_info)
537 if (*server_info != NULL) {
538 pdb_free_sam(&(*server_info)->sam_account);
540 /* call pam_end here, unless we know we are keeping it */
541 delete_nt_token( &(*server_info)->ptok );
542 ZERO_STRUCT(**server_info);
544 SAFE_FREE(*server_info);
547 /***************************************************************************
548 Make a server_info struct for a guest user
549 ***************************************************************************/
551 BOOL make_server_info_guest(auth_serversupplied_info **server_info)
553 struct passwd *pass = getpwnam_alloc(lp_guestaccount());
556 if (!make_server_info_pw(server_info, pass)) {
560 (*server_info)->guest = True;
564 DEBUG(0,("make_server_info_guest: getpwnam_alloc() failed on guest account!\n"));
568 /***************************************************************************
569 Make a server_info struct from the info3 returned by a domain logon
570 ***************************************************************************/
572 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
574 auth_serversupplied_info **server_info,
575 NET_USER_INFO_3 *info3)
577 NTSTATUS nt_status = NT_STATUS_OK;
582 SAM_ACCOUNT *sam_account = NULL;
586 struct passwd *passwd;
592 Here is where we should check the list of
593 trusted domains, and verify that the SID
597 sid_copy(&user_sid, &info3->dom_sid.sid);
598 if (!sid_append_rid(&user_sid, info3->user_rid)) {
599 return NT_STATUS_INVALID_PARAMETER;
602 sid_copy(&group_sid, &info3->dom_sid.sid);
603 if (!sid_append_rid(&group_sid, info3->group_rid)) {
604 return NT_STATUS_INVALID_PARAMETER;
607 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
608 return NT_STATUS_NO_MEMORY;
611 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
612 return NT_STATUS_NO_MEMORY;
615 if (winbind_sid_to_uid(&uid, &user_sid)
616 && winbind_sid_to_gid(&gid, &group_sid)
617 && ((passwd = getpwuid_alloc(uid)))) {
618 nt_status = pdb_init_sam_pw(&sam_account, passwd);
619 passwd_free(&passwd);
622 dom_user = talloc_asprintf(mem_ctx, "%s%s%s",
624 lp_winbind_separator(),
628 DEBUG(0, ("talloc_asprintf failed!\n"));
629 return NT_STATUS_NO_MEMORY;
632 if (!(passwd = Get_Pwnam(dom_user))
633 /* Only lookup local for the local
634 domain, we don't want this for
636 && strequal(nt_domain, lp_workgroup())) {
637 passwd = Get_Pwnam(nt_username);
641 return NT_STATUS_NO_SUCH_USER;
643 nt_status = pdb_init_sam_pw(&sam_account, passwd);
648 if (!NT_STATUS_IS_OK(nt_status)) {
649 DEBUG(0, ("make_server_info_info3: pdb_init_sam failed!\n"));
653 if (!pdb_set_user_sid(sam_account, &user_sid)) {
654 pdb_free_sam(&sam_account);
655 return NT_STATUS_UNSUCCESSFUL;
658 if (!pdb_set_group_sid(sam_account, &group_sid)) {
659 pdb_free_sam(&sam_account);
660 return NT_STATUS_UNSUCCESSFUL;
663 if (!pdb_set_nt_username(sam_account, nt_username)) {
664 pdb_free_sam(&sam_account);
665 return NT_STATUS_NO_MEMORY;
668 if (!pdb_set_domain(sam_account, nt_domain)) {
669 pdb_free_sam(&sam_account);
670 return NT_STATUS_NO_MEMORY;
673 if (!pdb_set_fullname(sam_account, pdb_unistr2_convert(&(info3->uni_full_name)))) {
674 pdb_free_sam(&sam_account);
675 return NT_STATUS_NO_MEMORY;
678 if (!pdb_set_logon_script(sam_account, pdb_unistr2_convert(&(info3->uni_logon_script)), True)) {
679 pdb_free_sam(&sam_account);
680 return NT_STATUS_NO_MEMORY;
683 if (!pdb_set_profile_path(sam_account, pdb_unistr2_convert(&(info3->uni_profile_path)), True)) {
684 pdb_free_sam(&sam_account);
685 return NT_STATUS_NO_MEMORY;
688 if (!pdb_set_homedir(sam_account, pdb_unistr2_convert(&(info3->uni_home_dir)), True)) {
689 pdb_free_sam(&sam_account);
690 return NT_STATUS_NO_MEMORY;
693 if (!pdb_set_dir_drive(sam_account, pdb_unistr2_convert(&(info3->uni_dir_drive)), True)) {
694 pdb_free_sam(&sam_account);
695 return NT_STATUS_NO_MEMORY;
698 if (!make_server_info_sam(server_info, sam_account)) {
699 DEBUG(0, ("make_server_info_info3: make_server_info_sam failed!\n"));
700 pdb_free_sam(&sam_account);
701 return NT_STATUS_NO_MEMORY;
704 /* Store the user group information in the server_info
705 returned to the caller. */
707 if (info3->num_groups2 != 0) {
710 auth_serversupplied_info *pserver_info = *server_info;
712 if ((pserver_info->ptok = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
713 DEBUG(0, ("domain_client_validate: out of memory allocating rid group membership\n"));
714 nt_status = NT_STATUS_NO_MEMORY;
715 free_server_info(server_info);
719 ptok = pserver_info->ptok;
720 ptok->num_sids = (size_t)info3->num_groups2;
722 if ((ptok->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptok->num_sids )) == NULL) {
723 DEBUG(0, ("domain_client_validate: Out of memory allocating group SIDS\n"));
724 nt_status = NT_STATUS_NO_MEMORY;
725 free_server_info(server_info);
729 for (i = 0; i < ptok->num_sids; i++) {
730 sid_copy(&ptok->user_sids[i], &(info3->dom_sid.sid));
731 if (!sid_append_rid(&ptok->user_sids[i], info3->gids[i].g_rid)) {
732 nt_status = NT_STATUS_INVALID_PARAMETER;
733 free_server_info(server_info);
741 /***************************************************************************
742 Make an auth_methods struct
743 ***************************************************************************/
745 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
748 smb_panic("no auth_context supplied to make_auth_methods()!\n");
752 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
755 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
757 DEBUG(0,("make_auth_method: malloc failed!\n"));
760 ZERO_STRUCTP(*auth_method);
765 /****************************************************************************
767 ****************************************************************************/
769 void delete_nt_token(NT_USER_TOKEN **pptoken)
772 NT_USER_TOKEN *ptoken = *pptoken;
773 SAFE_FREE( ptoken->user_sids );
774 ZERO_STRUCTP(ptoken);
779 /****************************************************************************
780 Duplicate a SID token.
781 ****************************************************************************/
783 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
785 NT_USER_TOKEN *token;
790 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
795 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
800 token->num_sids = ptoken->num_sids;
806 * Squash an NT_STATUS in line with security requirements.
807 * In an attempt to avoid giving the whole game away when users
808 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
809 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
810 * (session setups in particular).
812 * @param nt_status NTSTATUS input for squashing.
813 * @return the 'squashed' nt_status
816 NTSTATUS nt_status_squash(NTSTATUS nt_status)
818 if NT_STATUS_IS_OK(nt_status) {
820 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
821 /* Match WinXP and don't give the game away */
822 return NT_STATUS_LOGON_FAILURE;
824 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
825 /* Match WinXP and don't give the game away */
826 return NT_STATUS_LOGON_FAILURE;