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 pstring global_myname;
30 /****************************************************************************
31 Create a UNIX user on demand.
32 ****************************************************************************/
34 static int smb_create_user(const char *unix_user, const char *homedir)
39 pstrcpy(add_script, lp_adduser_script());
42 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
44 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
45 ret = smbrun(add_script,NULL);
46 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
50 /****************************************************************************
51 Add and Delete UNIX users on demand, based on NTSTATUS codes.
52 ****************************************************************************/
54 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status)
56 struct passwd *pwd=NULL;
58 if (NT_STATUS_IS_OK(nt_status)) {
60 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
63 * User validated ok against Domain controller.
64 * If the admin wants us to try and create a UNIX
65 * user on the fly, do so.
68 if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
69 smb_create_user(user_info->internal_username.str, NULL);
75 /****************************************************************************
76 Create an auth_usersupplied_data structure
77 ****************************************************************************/
79 static BOOL make_user_info(auth_usersupplied_info **user_info,
81 const char *internal_username,
82 const char *client_domain,
84 const char *wksta_name,
85 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
87 uint32 auth_flags, BOOL encrypted)
90 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
92 *user_info = malloc(sizeof(**user_info));
94 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
98 ZERO_STRUCTP(*user_info);
100 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
102 (*user_info)->smb_name.str = strdup(smb_name);
103 if ((*user_info)->smb_name.str) {
104 (*user_info)->smb_name.len = strlen(smb_name);
106 free_user_info(user_info);
110 (*user_info)->internal_username.str = strdup(internal_username);
111 if ((*user_info)->internal_username.str) {
112 (*user_info)->internal_username.len = strlen(internal_username);
114 free_user_info(user_info);
118 (*user_info)->domain.str = strdup(domain);
119 if ((*user_info)->domain.str) {
120 (*user_info)->domain.len = strlen(domain);
122 free_user_info(user_info);
126 (*user_info)->client_domain.str = strdup(client_domain);
127 if ((*user_info)->client_domain.str) {
128 (*user_info)->client_domain.len = strlen(client_domain);
130 free_user_info(user_info);
134 (*user_info)->wksta_name.str = strdup(wksta_name);
135 if ((*user_info)->wksta_name.str) {
136 (*user_info)->wksta_name.len = strlen(wksta_name);
138 free_user_info(user_info);
142 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
144 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
145 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
146 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
148 (*user_info)->encrypted = encrypted;
149 (*user_info)->auth_flags = auth_flags;
151 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
156 /****************************************************************************
157 Create an auth_usersupplied_data structure after appropriate mapping.
158 ****************************************************************************/
160 BOOL make_user_info_map(auth_usersupplied_info **user_info,
161 const char *smb_name,
162 const char *client_domain,
163 const char *wksta_name,
164 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
166 uint32 ntlmssp_flags, BOOL encrypted)
169 fstring internal_username;
170 fstrcpy(internal_username, smb_name);
171 map_username(internal_username);
173 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
174 client_domain, smb_name, wksta_name));
176 if (lp_allow_trusted_domains() && *client_domain) {
178 /* the client could have given us a workstation name
179 or other crap for the workgroup - we really need a
180 way of telling if this domain name is one of our
183 Also don't allow "" as a domain, fixes a Win9X bug
184 where it doens't supply a domain for logon script
187 The way I do it here is by checking if the fully
188 qualified username exists. This is rather reliant
189 on winbind, but until we have a better method this
193 domain = client_domain;
195 if ((smb_name) && (*smb_name)) { /* Don't do this for guests */
197 if (asprintf(&user, "%s%s%s",
198 client_domain, lp_winbind_separator(),
200 DEBUG(0, ("make_user_info_map: asprintf() failed!\n"));
204 DEBUG(5, ("make_user_info_map: testing for user %s\n", user));
206 if (Get_Pwnam(user) == NULL) {
207 DEBUG(5, ("make_user_info_map: test for user %s failed\n", user));
208 domain = lp_workgroup();
209 DEBUG(5, ("make_user_info_map: trusted domain %s doesn't appear to exist, using %s\n",
210 client_domain, domain));
212 DEBUG(5, ("make_user_info_map: using trusted domain %s\n", domain));
217 domain = lp_workgroup();
220 return make_user_info(user_info,
221 smb_name, internal_username,
222 client_domain, domain,
226 ntlmssp_flags, encrypted);
230 /****************************************************************************
231 Create an auth_usersupplied_data, making the DATA_BLOBs here.
232 Decrypt and encrypt the passwords.
233 ****************************************************************************/
235 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
236 const char *smb_name,
237 const char *client_domain,
238 const char *wksta_name,
239 const uchar *lm_network_pwd, int lm_pwd_len,
240 const uchar *nt_network_pwd, int nt_pwd_len)
243 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
244 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
245 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
246 uint32 auth_flags = AUTH_FLAG_NONE;
249 auth_flags |= AUTH_FLAG_LM_RESP;
250 if (nt_pwd_len == 24) {
251 auth_flags |= AUTH_FLAG_NTLM_RESP;
252 } else if (nt_pwd_len != 0) {
253 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
256 ret = make_user_info_map(user_info,
257 smb_name, client_domain,
263 data_blob_free(&lm_blob);
264 data_blob_free(&nt_blob);
268 /****************************************************************************
269 Create an auth_usersupplied_data, making the DATA_BLOBs here.
270 Decrypt and encrypt the passwords.
271 ****************************************************************************/
273 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
274 const char *smb_name,
275 const char *client_domain,
276 const char *wksta_name,
278 const uchar lm_interactive_pwd[16],
279 const uchar nt_interactive_pwd[16],
280 const uchar *dc_sess_key)
284 unsigned char local_lm_response[24];
285 unsigned char local_nt_response[24];
286 unsigned char key[16];
287 uint32 auth_flags = AUTH_FLAG_NONE;
290 memcpy(key, dc_sess_key, 8);
292 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
293 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
295 #ifdef DEBUG_PASSWORD
297 dump_data(100, (char *)key, sizeof(key));
299 DEBUG(100,("lm owf password:"));
300 dump_data(100, lm_pwd, sizeof(lm_pwd));
302 DEBUG(100,("nt owf password:"));
303 dump_data(100, nt_pwd, sizeof(nt_pwd));
306 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
307 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
309 #ifdef DEBUG_PASSWORD
310 DEBUG(100,("decrypt of lm owf password:"));
311 dump_data(100, lm_pwd, sizeof(lm_pwd));
313 DEBUG(100,("decrypt of nt owf password:"));
314 dump_data(100, nt_pwd, sizeof(nt_pwd));
317 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
318 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
320 /* Password info paranoia */
327 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
328 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
329 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
331 if (lm_interactive_pwd)
332 auth_flags |= AUTH_FLAG_LM_RESP;
333 if (nt_interactive_pwd)
334 auth_flags |= AUTH_FLAG_NTLM_RESP;
336 ret = make_user_info_map(user_info,
337 smb_name, client_domain,
344 data_blob_free(&local_lm_blob);
345 data_blob_free(&local_nt_blob);
351 /****************************************************************************
352 Create an auth_usersupplied_data structure
353 ****************************************************************************/
355 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
356 const char *smb_name,
357 const char *client_domain,
359 DATA_BLOB plaintext_password)
362 DATA_BLOB local_lm_blob;
363 DATA_BLOB local_nt_blob;
365 uint32 auth_flags = AUTH_FLAG_NONE;
368 * Not encrypted - do so.
371 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
373 if (plaintext_password.data) {
374 unsigned char local_lm_response[24];
376 #ifdef DEBUG_PASSWORD
377 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
378 dump_data(100, plaintext_password.data, plaintext_password.length);
381 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
382 local_lm_blob = data_blob(local_lm_response, 24);
384 /* We can't do an NT hash here, as the password needs to be
386 local_nt_blob = data_blob(NULL, 0);
388 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
390 local_lm_blob = data_blob(NULL, 0);
391 local_nt_blob = data_blob(NULL, 0);
394 ret = make_user_info_map(user_info, smb_name,
396 get_remote_machine_name(),
402 data_blob_free(&local_lm_blob);
406 /****************************************************************************
407 Create an auth_usersupplied_data structure
408 ****************************************************************************/
410 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
411 const char *smb_name,
412 const char *client_domain,
413 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
415 uint32 auth_flags = AUTH_FLAG_NONE;
417 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
419 if (lm_resp.length == 24) {
420 auth_flags |= AUTH_FLAG_LM_RESP;
422 if (nt_resp.length == 0) {
423 } else if (nt_resp.length == 24) {
424 auth_flags |= AUTH_FLAG_NTLM_RESP;
426 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
429 return make_user_info_map(user_info, smb_name,
431 get_remote_machine_name(),
438 /****************************************************************************
439 Create a guest user_info blob, for anonymous authenticaion.
440 ****************************************************************************/
442 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
444 DATA_BLOB lm_blob = data_blob(NULL, 0);
445 DATA_BLOB nt_blob = data_blob(NULL, 0);
446 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
447 uint32 auth_flags = AUTH_FLAG_NONE;
449 return make_user_info(user_info,
458 /***************************************************************************
459 Make a user_info struct
460 ***************************************************************************/
462 static BOOL make_server_info(auth_serversupplied_info **server_info)
464 *server_info = malloc(sizeof(**server_info));
466 DEBUG(0,("make_server_info: malloc failed!\n"));
469 ZERO_STRUCTP(*server_info);
473 /***************************************************************************
474 Make (and fill) a user_info struct from a SAM_ACCOUNT
475 ***************************************************************************/
477 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
479 if (!make_server_info(server_info)) {
483 (*server_info)->sam_fill_level = SAM_FILL_ALL;
484 (*server_info)->sam_account = sampass;
486 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
487 pdb_get_username((*server_info)->sam_account)));
491 /***************************************************************************
492 Make (and fill) a user_info struct from a 'struct passwd' by conversion
494 ***************************************************************************/
496 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
498 SAM_ACCOUNT *sampass = NULL;
499 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {
502 return make_server_info_sam(server_info, sampass);
505 /***************************************************************************
506 Free a user_info struct
507 ***************************************************************************/
509 void free_user_info(auth_usersupplied_info **user_info)
511 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
512 if (*user_info != NULL) {
513 if ((*user_info)->smb_name.str) {
514 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
516 SAFE_FREE((*user_info)->smb_name.str);
517 SAFE_FREE((*user_info)->internal_username.str);
518 SAFE_FREE((*user_info)->client_domain.str);
519 SAFE_FREE((*user_info)->domain.str);
520 SAFE_FREE((*user_info)->wksta_name.str);
521 data_blob_free(&(*user_info)->lm_resp);
522 data_blob_free(&(*user_info)->nt_resp);
523 SAFE_FREE((*user_info)->interactive_password);
524 data_blob_clear_free(&(*user_info)->plaintext_password);
525 ZERO_STRUCT(**user_info);
527 SAFE_FREE(*user_info);
530 /***************************************************************************
531 Clear out a server_info struct that has been allocated
532 ***************************************************************************/
534 void free_server_info(auth_serversupplied_info **server_info)
536 if (*server_info != NULL) {
537 pdb_free_sam(&(*server_info)->sam_account);
539 /* call pam_end here, unless we know we are keeping it */
540 delete_nt_token( &(*server_info)->ptok );
541 ZERO_STRUCT(**server_info);
543 SAFE_FREE(*server_info);
546 /***************************************************************************
547 Make a server_info struct for a guest user
548 ***************************************************************************/
550 BOOL make_server_info_guest(auth_serversupplied_info **server_info)
552 struct passwd *pass = getpwnam_alloc(lp_guestaccount());
555 if (!make_server_info_pw(server_info, pass)) {
559 (*server_info)->guest = True;
563 DEBUG(0,("make_server_info_guest: getpwnam_alloc() failed on guest account!\n"));
567 /***************************************************************************
568 Make a server_info struct from the info3 returned by a domain logon
569 ***************************************************************************/
571 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
572 const char *internal_username,
573 const char *sent_nt_username,
575 auth_serversupplied_info **server_info,
576 NET_USER_INFO_3 *info3)
578 NTSTATUS nt_status = NT_STATUS_OK;
580 const char *nt_domain;
581 const char *nt_username;
583 SAM_ACCOUNT *sam_account = NULL;
587 struct passwd *passwd;
593 Here is where we should check the list of
594 trusted domains, and verify that the SID
598 sid_copy(&user_sid, &info3->dom_sid.sid);
599 if (!sid_append_rid(&user_sid, info3->user_rid)) {
600 return NT_STATUS_INVALID_PARAMETER;
603 sid_copy(&group_sid, &info3->dom_sid.sid);
604 if (!sid_append_rid(&group_sid, info3->group_rid)) {
605 return NT_STATUS_INVALID_PARAMETER;
608 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
609 /* If the server didn't give us one, just use the one we sent them */
610 nt_username = sent_nt_username;
613 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
614 /* If the server didn't give us one, just use the one we sent them */
618 if (winbind_sid_to_uid(&uid, &user_sid)
619 && winbind_sid_to_gid(&gid, &group_sid)
620 && ((passwd = getpwuid_alloc(uid)))) {
621 nt_status = pdb_init_sam_pw(&sam_account, passwd);
622 passwd_free(&passwd);
625 dom_user = talloc_asprintf(mem_ctx, "%s%s%s",
627 lp_winbind_separator(),
631 DEBUG(0, ("talloc_asprintf failed!\n"));
632 return NT_STATUS_NO_MEMORY;
635 if (!(passwd = Get_Pwnam(dom_user))
636 /* Only lookup local for the local
637 domain, we don't want this for
639 && strequal(nt_domain, lp_workgroup())) {
640 passwd = Get_Pwnam(internal_username);
644 return NT_STATUS_NO_SUCH_USER;
646 nt_status = pdb_init_sam_pw(&sam_account, passwd);
651 if (!NT_STATUS_IS_OK(nt_status)) {
652 DEBUG(0, ("make_server_info_info3: pdb_init_sam failed!\n"));
656 if (!pdb_set_user_sid(sam_account, &user_sid)) {
657 pdb_free_sam(&sam_account);
658 return NT_STATUS_UNSUCCESSFUL;
661 if (!pdb_set_group_sid(sam_account, &group_sid)) {
662 pdb_free_sam(&sam_account);
663 return NT_STATUS_UNSUCCESSFUL;
666 if (!pdb_set_nt_username(sam_account, nt_username)) {
667 pdb_free_sam(&sam_account);
668 return NT_STATUS_NO_MEMORY;
671 if (!pdb_set_domain(sam_account, nt_domain)) {
672 pdb_free_sam(&sam_account);
673 return NT_STATUS_NO_MEMORY;
676 if (!pdb_set_fullname(sam_account, pdb_unistr2_convert(&(info3->uni_full_name)))) {
677 pdb_free_sam(&sam_account);
678 return NT_STATUS_NO_MEMORY;
681 if (!pdb_set_logon_script(sam_account, pdb_unistr2_convert(&(info3->uni_logon_script)), True)) {
682 pdb_free_sam(&sam_account);
683 return NT_STATUS_NO_MEMORY;
686 if (!pdb_set_profile_path(sam_account, pdb_unistr2_convert(&(info3->uni_profile_path)), True)) {
687 pdb_free_sam(&sam_account);
688 return NT_STATUS_NO_MEMORY;
691 if (!pdb_set_homedir(sam_account, pdb_unistr2_convert(&(info3->uni_home_dir)), True)) {
692 pdb_free_sam(&sam_account);
693 return NT_STATUS_NO_MEMORY;
696 if (!pdb_set_dir_drive(sam_account, pdb_unistr2_convert(&(info3->uni_dir_drive)), True)) {
697 pdb_free_sam(&sam_account);
698 return NT_STATUS_NO_MEMORY;
701 if (!make_server_info_sam(server_info, sam_account)) {
702 DEBUG(0, ("make_server_info_info3: make_server_info_sam failed!\n"));
703 pdb_free_sam(&sam_account);
704 return NT_STATUS_NO_MEMORY;
707 /* Store the user group information in the server_info
708 returned to the caller. */
710 if (info3->num_groups2 != 0) {
713 auth_serversupplied_info *pserver_info = *server_info;
715 if ((pserver_info->ptok = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
716 DEBUG(0, ("domain_client_validate: out of memory allocating rid group membership\n"));
717 nt_status = NT_STATUS_NO_MEMORY;
718 free_server_info(server_info);
722 ptok = pserver_info->ptok;
723 ptok->num_sids = (size_t)info3->num_groups2;
725 if ((ptok->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptok->num_sids )) == NULL) {
726 DEBUG(0, ("domain_client_validate: Out of memory allocating group SIDS\n"));
727 nt_status = NT_STATUS_NO_MEMORY;
728 free_server_info(server_info);
732 for (i = 0; i < ptok->num_sids; i++) {
733 sid_copy(&ptok->user_sids[i], &(info3->dom_sid.sid));
734 if (!sid_append_rid(&ptok->user_sids[i], info3->gids[i].g_rid)) {
735 nt_status = NT_STATUS_INVALID_PARAMETER;
736 free_server_info(server_info);
744 /***************************************************************************
745 Make an auth_methods struct
746 ***************************************************************************/
748 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
751 smb_panic("no auth_context supplied to make_auth_methods()!\n");
755 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
758 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
760 DEBUG(0,("make_auth_method: malloc failed!\n"));
763 ZERO_STRUCTP(*auth_method);
768 /****************************************************************************
770 ****************************************************************************/
772 void delete_nt_token(NT_USER_TOKEN **pptoken)
775 NT_USER_TOKEN *ptoken = *pptoken;
776 SAFE_FREE( ptoken->user_sids );
777 ZERO_STRUCTP(ptoken);
782 /****************************************************************************
783 Duplicate a SID token.
784 ****************************************************************************/
786 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
788 NT_USER_TOKEN *token;
793 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
798 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
803 token->num_sids = ptoken->num_sids;
809 * Squash an NT_STATUS in line with security requirements.
810 * In an attempt to avoid giving the whole game away when users
811 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
812 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
813 * (session setups in particular).
815 * @param nt_status NTSTATUS input for squashing.
816 * @return the 'squashed' nt_status
819 NTSTATUS nt_status_squash(NTSTATUS nt_status)
821 if NT_STATUS_IS_OK(nt_status) {
823 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
824 /* Match WinXP and don't give the game away */
825 return NT_STATUS_LOGON_FAILURE;
827 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
828 /* Match WinXP and don't give the game away */
829 return NT_STATUS_LOGON_FAILURE;