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;
29 extern DOM_SID global_sid_World;
30 extern DOM_SID global_sid_Network;
31 extern DOM_SID global_sid_Builtin_Guests;
32 extern DOM_SID global_sid_Authenticated_Users;
35 /****************************************************************************
36 Create a UNIX user on demand.
37 ****************************************************************************/
39 static int smb_create_user(const char *unix_user, const char *homedir)
44 pstrcpy(add_script, lp_adduser_script());
47 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
49 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
50 ret = smbrun(add_script,NULL);
51 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
55 /****************************************************************************
56 Add and Delete UNIX users on demand, based on NTSTATUS codes.
57 ****************************************************************************/
59 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status)
61 struct passwd *pwd=NULL;
63 if (NT_STATUS_IS_OK(nt_status)) {
65 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
68 * User validated ok against Domain controller.
69 * If the admin wants us to try and create a UNIX
70 * user on the fly, do so.
73 if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
74 smb_create_user(user_info->internal_username.str, NULL);
80 /****************************************************************************
81 Create an auth_usersupplied_data structure
82 ****************************************************************************/
84 static BOOL make_user_info(auth_usersupplied_info **user_info,
86 const char *internal_username,
87 const char *client_domain,
89 const char *wksta_name,
90 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
92 uint32 auth_flags, BOOL encrypted)
95 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
97 *user_info = malloc(sizeof(**user_info));
99 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
103 ZERO_STRUCTP(*user_info);
105 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
107 (*user_info)->smb_name.str = strdup(smb_name);
108 if ((*user_info)->smb_name.str) {
109 (*user_info)->smb_name.len = strlen(smb_name);
111 free_user_info(user_info);
115 (*user_info)->internal_username.str = strdup(internal_username);
116 if ((*user_info)->internal_username.str) {
117 (*user_info)->internal_username.len = strlen(internal_username);
119 free_user_info(user_info);
123 (*user_info)->domain.str = strdup(domain);
124 if ((*user_info)->domain.str) {
125 (*user_info)->domain.len = strlen(domain);
127 free_user_info(user_info);
131 (*user_info)->client_domain.str = strdup(client_domain);
132 if ((*user_info)->client_domain.str) {
133 (*user_info)->client_domain.len = strlen(client_domain);
135 free_user_info(user_info);
139 (*user_info)->wksta_name.str = strdup(wksta_name);
140 if ((*user_info)->wksta_name.str) {
141 (*user_info)->wksta_name.len = strlen(wksta_name);
143 free_user_info(user_info);
147 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
149 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
150 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
151 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
153 (*user_info)->encrypted = encrypted;
154 (*user_info)->auth_flags = auth_flags;
156 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
161 /****************************************************************************
162 Create an auth_usersupplied_data structure after appropriate mapping.
163 ****************************************************************************/
165 BOOL make_user_info_map(auth_usersupplied_info **user_info,
166 const char *smb_name,
167 const char *client_domain,
168 const char *wksta_name,
169 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
171 uint32 ntlmssp_flags, BOOL encrypted)
174 fstring internal_username;
175 fstrcpy(internal_username, smb_name);
176 map_username(internal_username);
178 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
179 client_domain, smb_name, wksta_name));
181 if (lp_allow_trusted_domains() && *client_domain) {
183 /* the client could have given us a workstation name
184 or other crap for the workgroup - we really need a
185 way of telling if this domain name is one of our
188 Also don't allow "" as a domain, fixes a Win9X bug
189 where it doens't supply a domain for logon script
192 The way I do it here is by checking if the fully
193 qualified username exists. This is rather reliant
194 on winbind, but until we have a better method this
198 domain = client_domain;
200 if ((smb_name) && (*smb_name)) { /* Don't do this for guests */
202 if (asprintf(&user, "%s%s%s",
203 client_domain, lp_winbind_separator(),
205 DEBUG(0, ("make_user_info_map: asprintf() failed!\n"));
209 DEBUG(5, ("make_user_info_map: testing for user %s\n", user));
211 if (Get_Pwnam(user) == NULL) {
212 DEBUG(5, ("make_user_info_map: test for user %s failed\n", user));
213 domain = lp_workgroup();
214 DEBUG(5, ("make_user_info_map: trusted domain %s doesn't appear to exist, using %s\n",
215 client_domain, domain));
217 DEBUG(5, ("make_user_info_map: using trusted domain %s\n", domain));
222 domain = lp_workgroup();
225 return make_user_info(user_info,
226 smb_name, internal_username,
227 client_domain, domain,
231 ntlmssp_flags, encrypted);
235 /****************************************************************************
236 Create an auth_usersupplied_data, making the DATA_BLOBs here.
237 Decrypt and encrypt the passwords.
238 ****************************************************************************/
240 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
241 const char *smb_name,
242 const char *client_domain,
243 const char *wksta_name,
244 const uchar *lm_network_pwd, int lm_pwd_len,
245 const uchar *nt_network_pwd, int nt_pwd_len)
248 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
249 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
250 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
251 uint32 auth_flags = AUTH_FLAG_NONE;
254 auth_flags |= AUTH_FLAG_LM_RESP;
255 if (nt_pwd_len == 24) {
256 auth_flags |= AUTH_FLAG_NTLM_RESP;
257 } else if (nt_pwd_len != 0) {
258 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
261 ret = make_user_info_map(user_info,
262 smb_name, client_domain,
268 data_blob_free(&lm_blob);
269 data_blob_free(&nt_blob);
273 /****************************************************************************
274 Create an auth_usersupplied_data, making the DATA_BLOBs here.
275 Decrypt and encrypt the passwords.
276 ****************************************************************************/
278 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
279 const char *smb_name,
280 const char *client_domain,
281 const char *wksta_name,
283 const uchar lm_interactive_pwd[16],
284 const uchar nt_interactive_pwd[16],
285 const uchar *dc_sess_key)
289 unsigned char local_lm_response[24];
290 unsigned char local_nt_response[24];
291 unsigned char key[16];
292 uint32 auth_flags = AUTH_FLAG_NONE;
295 memcpy(key, dc_sess_key, 8);
297 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
298 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
300 #ifdef DEBUG_PASSWORD
302 dump_data(100, (char *)key, sizeof(key));
304 DEBUG(100,("lm owf password:"));
305 dump_data(100, lm_pwd, sizeof(lm_pwd));
307 DEBUG(100,("nt owf password:"));
308 dump_data(100, nt_pwd, sizeof(nt_pwd));
311 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
312 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
314 #ifdef DEBUG_PASSWORD
315 DEBUG(100,("decrypt of lm owf password:"));
316 dump_data(100, lm_pwd, sizeof(lm_pwd));
318 DEBUG(100,("decrypt of nt owf password:"));
319 dump_data(100, nt_pwd, sizeof(nt_pwd));
322 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
323 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
325 /* Password info paranoia */
332 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
333 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
334 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
336 if (lm_interactive_pwd)
337 auth_flags |= AUTH_FLAG_LM_RESP;
338 if (nt_interactive_pwd)
339 auth_flags |= AUTH_FLAG_NTLM_RESP;
341 ret = make_user_info_map(user_info,
342 smb_name, client_domain,
349 data_blob_free(&local_lm_blob);
350 data_blob_free(&local_nt_blob);
356 /****************************************************************************
357 Create an auth_usersupplied_data structure
358 ****************************************************************************/
360 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
361 const char *smb_name,
362 const char *client_domain,
364 DATA_BLOB plaintext_password)
367 DATA_BLOB local_lm_blob;
368 DATA_BLOB local_nt_blob;
370 uint32 auth_flags = AUTH_FLAG_NONE;
373 * Not encrypted - do so.
376 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
378 if (plaintext_password.data) {
379 unsigned char local_lm_response[24];
381 #ifdef DEBUG_PASSWORD
382 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
383 dump_data(100, plaintext_password.data, plaintext_password.length);
386 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
387 local_lm_blob = data_blob(local_lm_response, 24);
389 /* We can't do an NT hash here, as the password needs to be
391 local_nt_blob = data_blob(NULL, 0);
393 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
395 local_lm_blob = data_blob(NULL, 0);
396 local_nt_blob = data_blob(NULL, 0);
399 ret = make_user_info_map(user_info, smb_name,
401 get_remote_machine_name(),
407 data_blob_free(&local_lm_blob);
411 /****************************************************************************
412 Create an auth_usersupplied_data structure
413 ****************************************************************************/
415 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
416 const char *smb_name,
417 const char *client_domain,
418 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
420 uint32 auth_flags = AUTH_FLAG_NONE;
422 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
424 if (lm_resp.length == 24) {
425 auth_flags |= AUTH_FLAG_LM_RESP;
427 if (nt_resp.length == 0) {
428 } else if (nt_resp.length == 24) {
429 auth_flags |= AUTH_FLAG_NTLM_RESP;
431 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
434 return make_user_info_map(user_info, smb_name,
436 get_remote_machine_name(),
443 /****************************************************************************
444 Create a guest user_info blob, for anonymous authenticaion.
445 ****************************************************************************/
447 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
449 DATA_BLOB lm_blob = data_blob(NULL, 0);
450 DATA_BLOB nt_blob = data_blob(NULL, 0);
451 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
452 uint32 auth_flags = AUTH_FLAG_NONE;
454 return make_user_info(user_info,
463 /****************************************************************************
464 prints a NT_USER_TOKEN to debug output.
465 ****************************************************************************/
467 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
472 DEBUGC(dbg_class, dbg_lev, ("NT user token of user %s\n",
473 sid_to_string(sid_str, &token->user_sids[0]) ));
474 DEBUGADDC(dbg_class, dbg_lev, ("contains %i SIDs\n", token->num_sids));
475 for (i = 0; i < token->num_sids; i++)
476 DEBUGADDC(dbg_class, dbg_lev, ("SID[%3i]: %s\n", i,
477 sid_to_string(sid_str, &token->user_sids[i])));
480 /****************************************************************************
481 Create the SID list for this user.
482 ****************************************************************************/
484 static NTSTATUS create_nt_user_token(const DOM_SID *user_sid, const DOM_SID *group_sid,
485 int n_groupSIDs, DOM_SID *groupSIDs,
486 BOOL is_guest, NT_USER_TOKEN **token)
488 NTSTATUS nt_status = NT_STATUS_OK;
489 NT_USER_TOKEN *ptoken;
493 if ((ptoken = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
494 DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
495 nt_status = NT_STATUS_NO_MEMORY;
499 ZERO_STRUCTP(ptoken);
501 ptoken->num_sids = n_groupSIDs + 5;
503 if ((ptoken->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
504 DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
505 nt_status = NT_STATUS_NO_MEMORY;
509 memset((char*)ptoken->user_sids,0,sizeof(DOM_SID) * ptoken->num_sids);
512 * Note - user SID *MUST* be first in token !
513 * se_access_check depends on this.
515 * Primary group SID is second in token. Convention.
518 sid_copy(&ptoken->user_sids[PRIMARY_USER_SID_INDEX], user_sid);
520 sid_copy(&ptoken->user_sids[PRIMARY_GROUP_SID_INDEX], group_sid);
523 * Finally add the "standard" SIDs.
524 * The only difference between guest and "anonymous" (which we
525 * don't really support) is the addition of Authenticated_Users.
528 sid_copy(&ptoken->user_sids[2], &global_sid_World);
529 sid_copy(&ptoken->user_sids[3], &global_sid_Network);
532 sid_copy(&ptoken->user_sids[4], &global_sid_Builtin_Guests);
534 sid_copy(&ptoken->user_sids[4], &global_sid_Authenticated_Users);
536 sid_ndx = 5; /* next available spot */
538 for (i = 0; i < n_groupSIDs; i++) {
540 for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
541 if (sid_equal(&ptoken->user_sids[check_sid_idx],
547 if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
548 sid_copy(&ptoken->user_sids[sid_ndx++], &groupSIDs[i]);
554 debug_nt_user_token(DBGC_AUTH, 10, ptoken);
561 /****************************************************************************
562 Create the SID list for this user.
563 ****************************************************************************/
565 NT_USER_TOKEN *create_nt_token(uid_t uid, gid_t gid, int ngroups, gid_t *groups, BOOL is_guest)
570 NT_USER_TOKEN *token;
573 if (!uid_to_sid(&user_sid, uid)) {
576 if (!gid_to_sid(&group_sid, gid)) {
580 group_sids = malloc(sizeof(DOM_SID) * ngroups);
582 DEBUG(0, ("create_nt_token: malloc() failed for DOM_SID list!\n"));
586 for (i = 0; i < ngroups; i++) {
587 if (!gid_to_sid(&(group_sids)[i], (groups)[i])) {
588 DEBUG(1, ("create_nt_token: failed to convert gid %ld to a sid!\n", (long int)groups[i]));
589 SAFE_FREE(group_sids);
594 if (!NT_STATUS_IS_OK(create_nt_user_token(&user_sid, &group_sid,
595 ngroups, group_sids, is_guest, &token))) {
596 SAFE_FREE(group_sids);
600 SAFE_FREE(group_sids);
605 /******************************************************************************
606 * this function returns the groups (SIDs) of the local SAM the user is in.
607 * If this samba server is a DC of the domain the user belongs to, it returns
608 * both domain groups and local / builtin groups. If the user is in a trusted
609 * domain, or samba is a member server of a domain, then this function returns
610 * local and builtin groups the user is a member of.
612 * currently this is a hack, as there is no sam implementation that is capable
614 ******************************************************************************/
616 static NTSTATUS get_user_groups_from_local_sam(const DOM_SID *user_sid,
617 int *n_groups, DOM_SID **groups, gid_t **unix_groups)
620 enum SID_NAME_USE snu;
629 if (!sid_to_uid(user_sid, &uid, &snu)) {
630 DEBUG(2, ("get_user_groups_from_local_sam: Failed to convert user SID %s to a uid!\n", sid_to_string(str, user_sid)));
631 return NT_STATUS_NO_SUCH_USER;
634 usr = getpwuid_alloc(uid);
636 n_unix_groups = groups_max();
637 if ((*unix_groups = malloc( sizeof(gid_t) * groups_max() ) ) == NULL) {
638 DEBUG(0, ("get_user_groups_from_local_sam: Out of memory allocating unix group list\n"));
640 return NT_STATUS_NO_MEMORY;
643 if (sys_getgrouplist(usr->pw_name, usr->pw_gid, *unix_groups, &n_unix_groups) == -1) {
644 *unix_groups = realloc(unix_groups, sizeof(gid_t) * n_unix_groups);
645 if (sys_getgrouplist(usr->pw_name, usr->pw_gid, *unix_groups, &n_unix_groups) == -1) {
646 DEBUG(0, ("get_user_groups_from_local_sam: failed to get the unix group list\n"));
647 SAFE_FREE(unix_groups);
649 return NT_STATUS_NO_SUCH_USER; /* what should this return value be? */
655 DEBUG(5,("get_user_groups_from_local_sam: user is in the unix following groups\n"));
656 for (i = 0; i < n_unix_groups; i++)
657 DEBUGADD(5,("supplementary group gid:%ld\n",(long int)(*unix_groups)[i]));
659 *groups = malloc(sizeof(DOM_SID) * n_unix_groups);
661 DEBUG(0, ("get_user_group_from_local_sam: malloc() failed for DOM_SID list!\n"));
662 SAFE_FREE(unix_groups);
663 return NT_STATUS_NO_MEMORY;
666 *n_groups = n_unix_groups;
668 for (i = 0; i < *n_groups; i++) {
669 if (!gid_to_sid(&(*groups)[i], (*unix_groups)[i])) {
670 DEBUG(1, ("get_user_groups_from_local_sam: failed to convert gid %ld to a sid!\n", (long int)unix_groups[i+1]));
672 SAFE_FREE(unix_groups);
673 return NT_STATUS_NO_SUCH_USER;
680 /***************************************************************************
681 Make a user_info struct
682 ***************************************************************************/
684 static NTSTATUS make_server_info(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
686 *server_info = malloc(sizeof(**server_info));
688 DEBUG(0,("make_server_info: malloc failed!\n"));
689 return NT_STATUS_NO_MEMORY;
691 ZERO_STRUCTP(*server_info);
693 (*server_info)->sam_fill_level = SAM_FILL_ALL;
694 (*server_info)->sam_account = sampass;
699 /***************************************************************************
700 Make (and fill) a user_info struct from a SAM_ACCOUNT
701 ***************************************************************************/
703 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
704 SAM_ACCOUNT *sampass)
706 NTSTATUS nt_status = NT_STATUS_OK;
707 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
708 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
710 DOM_SID *groupSIDs = NULL;
711 gid_t *unix_groups = NULL;
712 NT_USER_TOKEN *token;
716 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info, sampass))) {
720 if (!NT_STATUS_IS_OK(nt_status
721 = get_user_groups_from_local_sam(pdb_get_user_sid(sampass),
722 &n_groupSIDs, &groupSIDs, &unix_groups)))
724 DEBUG(4,("get_user_groups_from_local_sam failed\n"));
725 free_server_info(server_info);
729 is_guest = (sid_peek_rid(user_sid, &rid) && rid == DOMAIN_USER_RID_GUEST);
731 if (!NT_STATUS_IS_OK(nt_status = create_nt_user_token(user_sid, group_sid,
732 n_groupSIDs, groupSIDs, is_guest,
735 DEBUG(4,("create_nt_user_token failed\n"));
736 SAFE_FREE(groupSIDs);
737 SAFE_FREE(unix_groups);
738 free_server_info(server_info);
742 SAFE_FREE(groupSIDs);
744 (*server_info)->n_groups = n_groupSIDs;
745 (*server_info)->groups = unix_groups;
747 (*server_info)->ptok = token;
749 debug_nt_user_token(DBGC_AUTH, 5, token);
751 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
752 pdb_get_username((*server_info)->sam_account)));
757 /***************************************************************************
758 Make (and fill) a user_info struct from a 'struct passwd' by conversion
760 ***************************************************************************/
762 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
765 SAM_ACCOUNT *sampass = NULL;
766 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(&sampass, pwd))) {
769 return make_server_info_sam(server_info, sampass);
772 /***************************************************************************
773 Make (and fill) a user_info struct for a guest login.
774 ***************************************************************************/
776 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
779 SAM_ACCOUNT *sampass = NULL;
782 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
786 sid_copy(&guest_sid, get_global_sam_sid());
787 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
789 if (!pdb_getsampwsid(sampass, &guest_sid)) {
790 return NT_STATUS_NO_SUCH_USER;
793 nt_status = make_server_info_sam(server_info, sampass);
795 (*server_info)->guest = True;
800 /***************************************************************************
801 Make a server_info struct from the info3 returned by a domain logon
802 ***************************************************************************/
804 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
805 const char *internal_username,
806 const char *sent_nt_username,
808 auth_serversupplied_info **server_info,
809 NET_USER_INFO_3 *info3)
811 NTSTATUS nt_status = NT_STATUS_OK;
813 const char *nt_domain;
814 const char *nt_username;
816 SAM_ACCOUNT *sam_account = NULL;
820 struct passwd *passwd;
826 DOM_SID *lgroupSIDs = NULL;
828 gid_t *unix_groups = NULL;
829 NT_USER_TOKEN *token;
831 DOM_SID *all_group_SIDs;
835 Here is where we should check the list of
836 trusted domains, and verify that the SID
840 sid_copy(&user_sid, &info3->dom_sid.sid);
841 if (!sid_append_rid(&user_sid, info3->user_rid)) {
842 return NT_STATUS_INVALID_PARAMETER;
845 sid_copy(&group_sid, &info3->dom_sid.sid);
846 if (!sid_append_rid(&group_sid, info3->group_rid)) {
847 return NT_STATUS_INVALID_PARAMETER;
850 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
851 /* If the server didn't give us one, just use the one we sent them */
852 nt_username = sent_nt_username;
855 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
856 /* If the server didn't give us one, just use the one we sent them */
860 if (winbind_sid_to_uid(&uid, &user_sid)
861 && winbind_sid_to_gid(&gid, &group_sid)
862 && ((passwd = getpwuid_alloc(uid)))) {
863 nt_status = pdb_init_sam_pw(&sam_account, passwd);
864 passwd_free(&passwd);
867 dom_user = talloc_asprintf(mem_ctx, "%s%s%s",
869 lp_winbind_separator(),
873 DEBUG(0, ("talloc_asprintf failed!\n"));
874 return NT_STATUS_NO_MEMORY;
877 if (!(passwd = Get_Pwnam(dom_user))
878 /* Only lookup local for the local
879 domain, we don't want this for
881 && strequal(nt_domain, lp_workgroup())) {
882 passwd = Get_Pwnam(internal_username);
886 return NT_STATUS_NO_SUCH_USER;
888 nt_status = pdb_init_sam_pw(&sam_account, passwd);
893 if (!NT_STATUS_IS_OK(nt_status)) {
894 DEBUG(0, ("make_server_info_info3: pdb_init_sam failed!\n"));
898 if (!pdb_set_user_sid(sam_account, &user_sid)) {
899 pdb_free_sam(&sam_account);
900 return NT_STATUS_UNSUCCESSFUL;
903 if (!pdb_set_group_sid(sam_account, &group_sid)) {
904 pdb_free_sam(&sam_account);
905 return NT_STATUS_UNSUCCESSFUL;
908 if (!pdb_set_nt_username(sam_account, nt_username)) {
909 pdb_free_sam(&sam_account);
910 return NT_STATUS_NO_MEMORY;
913 if (!pdb_set_domain(sam_account, nt_domain)) {
914 pdb_free_sam(&sam_account);
915 return NT_STATUS_NO_MEMORY;
918 if (!pdb_set_fullname(sam_account, pdb_unistr2_convert(&(info3->uni_full_name)))) {
919 pdb_free_sam(&sam_account);
920 return NT_STATUS_NO_MEMORY;
923 if (!pdb_set_logon_script(sam_account, pdb_unistr2_convert(&(info3->uni_logon_script)), True)) {
924 pdb_free_sam(&sam_account);
925 return NT_STATUS_NO_MEMORY;
928 if (!pdb_set_profile_path(sam_account, pdb_unistr2_convert(&(info3->uni_profile_path)), True)) {
929 pdb_free_sam(&sam_account);
930 return NT_STATUS_NO_MEMORY;
933 if (!pdb_set_homedir(sam_account, pdb_unistr2_convert(&(info3->uni_home_dir)), True)) {
934 pdb_free_sam(&sam_account);
935 return NT_STATUS_NO_MEMORY;
938 if (!pdb_set_dir_drive(sam_account, pdb_unistr2_convert(&(info3->uni_dir_drive)), True)) {
939 pdb_free_sam(&sam_account);
940 return NT_STATUS_NO_MEMORY;
943 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info, sam_account))) {
944 DEBUG(4, ("make_server_info failed!\n"));
945 pdb_free_sam(&sam_account);
949 /* Store the user group information in the server_info
950 returned to the caller. */
952 if (!NT_STATUS_IS_OK(nt_status
953 = get_user_groups_from_local_sam(&user_sid,
958 DEBUG(4,("get_user_groups_from_local_sam failed\n"));
962 (*server_info)->groups = unix_groups;
963 (*server_info)->n_groups = n_lgroupSIDs;
965 /* Create a 'combined' list of all SIDs we might want in the SD */
966 all_group_SIDs = malloc(sizeof(DOM_SID) * (n_lgroupSIDs+info3->num_groups2));
967 if (!all_group_SIDs) {
968 DEBUG(0, ("create_nt_token_info3: malloc() failed for DOM_SID list!\n"));
969 SAFE_FREE(lgroupSIDs);
970 return NT_STATUS_NO_MEMORY;
973 /* Copy the 'local' sids */
974 memcpy(all_group_SIDs, lgroupSIDs, sizeof(DOM_SID) * n_lgroupSIDs);
975 SAFE_FREE(lgroupSIDs);
977 /* and create (by appending rids) the 'domain' sids */
978 for (i = 0; i < info3->num_groups2; i++) {
979 sid_copy(&all_group_SIDs[i+n_lgroupSIDs+1], &(info3->dom_sid.sid));
980 if (!sid_append_rid(&all_group_SIDs[i+n_lgroupSIDs+1], info3->gids[i].g_rid)) {
981 nt_status = NT_STATUS_INVALID_PARAMETER;
982 DEBUG(3,("create_nt_token_info3: could not append additional group rid 0x%x\n",
983 info3->gids[i].g_rid));
984 SAFE_FREE(lgroupSIDs);
989 /* Where are the 'global' sids... */
991 /* can the user be guest? if yes, where is it stored? */
992 if (!NT_STATUS_IS_OK(nt_status = create_nt_user_token(&user_sid, &group_sid,
993 n_lgroupSIDs+info3->num_groups2, all_group_SIDs,
995 DEBUG(4,("create_nt_user_token failed\n"));
996 SAFE_FREE(all_group_SIDs);
1000 (*server_info)->ptok = token;
1002 SAFE_FREE(all_group_SIDs);
1004 debug_nt_user_token(DBGC_AUTH, 5, token);
1006 return NT_STATUS_OK;
1009 /***************************************************************************
1010 Free a user_info struct
1011 ***************************************************************************/
1013 void free_user_info(auth_usersupplied_info **user_info)
1015 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1016 if (*user_info != NULL) {
1017 if ((*user_info)->smb_name.str) {
1018 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
1020 SAFE_FREE((*user_info)->smb_name.str);
1021 SAFE_FREE((*user_info)->internal_username.str);
1022 SAFE_FREE((*user_info)->client_domain.str);
1023 SAFE_FREE((*user_info)->domain.str);
1024 SAFE_FREE((*user_info)->wksta_name.str);
1025 data_blob_free(&(*user_info)->lm_resp);
1026 data_blob_free(&(*user_info)->nt_resp);
1027 SAFE_FREE((*user_info)->interactive_password);
1028 data_blob_clear_free(&(*user_info)->plaintext_password);
1029 ZERO_STRUCT(**user_info);
1031 SAFE_FREE(*user_info);
1034 /***************************************************************************
1035 Clear out a server_info struct that has been allocated
1036 ***************************************************************************/
1038 void free_server_info(auth_serversupplied_info **server_info)
1040 DEBUG(5,("attempting to free (and zero) a server_info structure\n"));
1041 if (*server_info != NULL) {
1042 pdb_free_sam(&(*server_info)->sam_account);
1044 /* call pam_end here, unless we know we are keeping it */
1045 delete_nt_token( &(*server_info)->ptok );
1046 SAFE_FREE((*server_info)->groups);
1047 ZERO_STRUCT(**server_info);
1049 SAFE_FREE(*server_info);
1052 /***************************************************************************
1053 Make an auth_methods struct
1054 ***************************************************************************/
1056 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
1058 if (!auth_context) {
1059 smb_panic("no auth_context supplied to make_auth_methods()!\n");
1063 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
1066 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
1067 if (!*auth_method) {
1068 DEBUG(0,("make_auth_method: malloc failed!\n"));
1071 ZERO_STRUCTP(*auth_method);
1076 /****************************************************************************
1078 ****************************************************************************/
1080 void delete_nt_token(NT_USER_TOKEN **pptoken)
1083 NT_USER_TOKEN *ptoken = *pptoken;
1084 SAFE_FREE( ptoken->user_sids );
1085 ZERO_STRUCTP(ptoken);
1087 SAFE_FREE(*pptoken);
1090 /****************************************************************************
1091 Duplicate a SID token.
1092 ****************************************************************************/
1094 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
1096 NT_USER_TOKEN *token;
1101 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
1104 ZERO_STRUCTP(token);
1106 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
1111 token->num_sids = ptoken->num_sids;
1117 * Squash an NT_STATUS in line with security requirements.
1118 * In an attempt to avoid giving the whole game away when users
1119 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
1120 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
1121 * (session setups in particular).
1123 * @param nt_status NTSTATUS input for squashing.
1124 * @return the 'squashed' nt_status
1127 NTSTATUS nt_status_squash(NTSTATUS nt_status)
1129 if NT_STATUS_IS_OK(nt_status) {
1131 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
1132 /* Match WinXP and don't give the game away */
1133 return NT_STATUS_LOGON_FAILURE;
1135 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
1136 /* Match WinXP and don't give the game away */
1137 return NT_STATUS_LOGON_FAILURE;