r4904: sync up with 3.0 for 3.0.11pre2
[Samba.git] / source / auth / auth_util.c
blob4a23ec8adc5a1e595215d1aef6faf2eb5716d1a6
1 /*
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.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_AUTH
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 *domain, const char *unix_username, const char *homedir)
41 pstring add_script;
42 int ret;
44 pstrcpy(add_script, lp_adduser_script());
45 if (! *add_script)
46 return -1;
47 all_string_sub(add_script, "%u", unix_username, sizeof(pstring));
48 if (domain)
49 all_string_sub(add_script, "%D", domain, sizeof(pstring));
50 if (homedir)
51 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
52 ret = smbrun(add_script,NULL);
53 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
54 return ret;
57 /****************************************************************************
58 Add and Delete UNIX users on demand, based on NTSTATUS codes.
59 We don't care about RID's here so ignore.
60 ****************************************************************************/
62 void auth_add_user_script(const char *domain, const char *username)
64 uint32 rid;
66 * User validated ok against Domain controller.
67 * If the admin wants us to try and create a UNIX
68 * user on the fly, do so.
71 if ( *lp_adduser_script() )
72 smb_create_user(domain, username, NULL);
73 else {
74 DEBUG(10,("auth_add_user_script: no 'add user script'. Asking winbindd\n"));
76 /* should never get here is we a re a domain member running winbindd
77 However, a host set for 'security = server' might run winbindd for
78 account allocation */
80 if ( !winbind_create_user(username, NULL) ) {
81 DEBUG(5,("auth_add_user_script: winbindd_create_user() failed\n"));
82 rid = 0;
87 /****************************************************************************
88 Create a SAM_ACCOUNT - either by looking in the pdb, or by faking it up from
89 unix info.
90 ****************************************************************************/
92 NTSTATUS auth_get_sam_account(const char *user, SAM_ACCOUNT **account)
94 BOOL pdb_ret;
95 NTSTATUS nt_status;
96 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(account))) {
97 return nt_status;
100 become_root();
101 pdb_ret = pdb_getsampwnam(*account, user);
102 unbecome_root();
104 if (!pdb_ret) {
106 struct passwd *pass = Get_Pwnam(user);
107 if (!pass)
108 return NT_STATUS_NO_SUCH_USER;
110 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*account, pass))) {
111 return nt_status;
114 return NT_STATUS_OK;
117 /****************************************************************************
118 Create an auth_usersupplied_data structure
119 ****************************************************************************/
121 static NTSTATUS make_user_info(auth_usersupplied_info **user_info,
122 const char *smb_name,
123 const char *internal_username,
124 const char *client_domain,
125 const char *domain,
126 const char *wksta_name,
127 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
128 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
129 DATA_BLOB *plaintext,
130 BOOL encrypted)
133 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
135 *user_info = SMB_MALLOC_P(auth_usersupplied_info);
136 if (!user_info) {
137 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
138 return NT_STATUS_NO_MEMORY;
141 ZERO_STRUCTP(*user_info);
143 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
145 (*user_info)->smb_name.str = SMB_STRDUP(smb_name);
146 if ((*user_info)->smb_name.str) {
147 (*user_info)->smb_name.len = strlen(smb_name);
148 } else {
149 free_user_info(user_info);
150 return NT_STATUS_NO_MEMORY;
153 (*user_info)->internal_username.str = SMB_STRDUP(internal_username);
154 if ((*user_info)->internal_username.str) {
155 (*user_info)->internal_username.len = strlen(internal_username);
156 } else {
157 free_user_info(user_info);
158 return NT_STATUS_NO_MEMORY;
161 (*user_info)->domain.str = SMB_STRDUP(domain);
162 if ((*user_info)->domain.str) {
163 (*user_info)->domain.len = strlen(domain);
164 } else {
165 free_user_info(user_info);
166 return NT_STATUS_NO_MEMORY;
169 (*user_info)->client_domain.str = SMB_STRDUP(client_domain);
170 if ((*user_info)->client_domain.str) {
171 (*user_info)->client_domain.len = strlen(client_domain);
172 } else {
173 free_user_info(user_info);
174 return NT_STATUS_NO_MEMORY;
177 (*user_info)->wksta_name.str = SMB_STRDUP(wksta_name);
178 if ((*user_info)->wksta_name.str) {
179 (*user_info)->wksta_name.len = strlen(wksta_name);
180 } else {
181 free_user_info(user_info);
182 return NT_STATUS_NO_MEMORY;
185 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
187 if (lm_pwd)
188 (*user_info)->lm_resp = data_blob(lm_pwd->data, lm_pwd->length);
189 if (nt_pwd)
190 (*user_info)->nt_resp = data_blob(nt_pwd->data, nt_pwd->length);
191 if (lm_interactive_pwd)
192 (*user_info)->lm_interactive_pwd = data_blob(lm_interactive_pwd->data, lm_interactive_pwd->length);
193 if (nt_interactive_pwd)
194 (*user_info)->nt_interactive_pwd = data_blob(nt_interactive_pwd->data, nt_interactive_pwd->length);
196 if (plaintext)
197 (*user_info)->plaintext_password = data_blob(plaintext->data, plaintext->length);
199 (*user_info)->encrypted = encrypted;
201 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
203 return NT_STATUS_OK;
206 /****************************************************************************
207 Create an auth_usersupplied_data structure after appropriate mapping.
208 ****************************************************************************/
210 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info,
211 const char *smb_name,
212 const char *client_domain,
213 const char *wksta_name,
214 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
215 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
216 DATA_BLOB *plaintext,
217 BOOL encrypted)
219 const char *domain;
220 fstring internal_username;
221 fstrcpy(internal_username, smb_name);
222 map_username(internal_username);
224 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
225 client_domain, smb_name, wksta_name));
227 /* don't allow "" as a domain, fixes a Win9X bug
228 where it doens't supply a domain for logon script
229 'net use' commands. */
231 if ( *client_domain )
232 domain = client_domain;
233 else
234 domain = lp_workgroup();
236 /* do what win2k does. Always map unknown domains to our own
237 and let the "passdb backend" handle unknown users. */
239 if ( !is_trusted_domain(domain) && !strequal(domain, get_global_sam_name()) )
240 domain = get_default_sam_name();
242 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
244 return make_user_info(user_info, smb_name, internal_username,
245 client_domain, domain, wksta_name,
246 lm_pwd, nt_pwd,
247 lm_interactive_pwd, nt_interactive_pwd,
248 plaintext, encrypted);
251 /****************************************************************************
252 Create an auth_usersupplied_data, making the DATA_BLOBs here.
253 Decrypt and encrypt the passwords.
254 ****************************************************************************/
256 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
257 const char *smb_name,
258 const char *client_domain,
259 const char *wksta_name,
260 const uchar *lm_network_pwd, int lm_pwd_len,
261 const uchar *nt_network_pwd, int nt_pwd_len)
263 BOOL ret;
264 NTSTATUS nt_status;
265 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
266 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
268 nt_status = make_user_info_map(user_info,
269 smb_name, client_domain,
270 wksta_name,
271 lm_pwd_len ? &lm_blob : NULL,
272 nt_pwd_len ? &nt_blob : NULL,
273 NULL, NULL, NULL,
274 True);
276 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
278 data_blob_free(&lm_blob);
279 data_blob_free(&nt_blob);
280 return ret;
283 /****************************************************************************
284 Create an auth_usersupplied_data, making the DATA_BLOBs here.
285 Decrypt and encrypt the passwords.
286 ****************************************************************************/
288 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
289 const char *smb_name,
290 const char *client_domain,
291 const char *wksta_name,
292 const uchar chal[8],
293 const uchar lm_interactive_pwd[16],
294 const uchar nt_interactive_pwd[16],
295 const uchar *dc_sess_key)
297 char lm_pwd[16];
298 char nt_pwd[16];
299 unsigned char local_lm_response[24];
300 unsigned char local_nt_response[24];
301 unsigned char key[16];
303 ZERO_STRUCT(key);
304 memcpy(key, dc_sess_key, 8);
306 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
307 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
309 #ifdef DEBUG_PASSWORD
310 DEBUG(100,("key:"));
311 dump_data(100, (char *)key, sizeof(key));
313 DEBUG(100,("lm owf password:"));
314 dump_data(100, lm_pwd, sizeof(lm_pwd));
316 DEBUG(100,("nt owf password:"));
317 dump_data(100, nt_pwd, sizeof(nt_pwd));
318 #endif
320 if (lm_interactive_pwd)
321 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
323 if (nt_interactive_pwd)
324 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
326 #ifdef DEBUG_PASSWORD
327 DEBUG(100,("decrypt of lm owf password:"));
328 dump_data(100, lm_pwd, sizeof(lm_pwd));
330 DEBUG(100,("decrypt of nt owf password:"));
331 dump_data(100, nt_pwd, sizeof(nt_pwd));
332 #endif
334 if (lm_interactive_pwd)
335 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
337 if (nt_interactive_pwd)
338 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
340 /* Password info paranoia */
341 ZERO_STRUCT(key);
344 BOOL ret;
345 NTSTATUS nt_status;
346 DATA_BLOB local_lm_blob;
347 DATA_BLOB local_nt_blob;
349 DATA_BLOB lm_interactive_blob;
350 DATA_BLOB nt_interactive_blob;
352 if (lm_interactive_pwd) {
353 local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
354 lm_interactive_blob = data_blob(lm_pwd, sizeof(lm_pwd));
355 ZERO_STRUCT(lm_pwd);
358 if (nt_interactive_pwd) {
359 local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
360 nt_interactive_blob = data_blob(nt_pwd, sizeof(nt_pwd));
361 ZERO_STRUCT(nt_pwd);
364 nt_status = make_user_info_map(user_info,
365 smb_name, client_domain,
366 wksta_name,
367 lm_interactive_pwd ? &local_lm_blob : NULL,
368 nt_interactive_pwd ? &local_nt_blob : NULL,
369 lm_interactive_pwd ? &lm_interactive_blob : NULL,
370 nt_interactive_pwd ? &nt_interactive_blob : NULL,
371 NULL,
372 True);
374 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
375 data_blob_free(&local_lm_blob);
376 data_blob_free(&local_nt_blob);
377 data_blob_free(&lm_interactive_blob);
378 data_blob_free(&nt_interactive_blob);
379 return ret;
384 /****************************************************************************
385 Create an auth_usersupplied_data structure
386 ****************************************************************************/
388 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
389 const char *smb_name,
390 const char *client_domain,
391 const uint8 chal[8],
392 DATA_BLOB plaintext_password)
395 DATA_BLOB local_lm_blob;
396 DATA_BLOB local_nt_blob;
397 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
400 * Not encrypted - do so.
403 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
405 if (plaintext_password.data) {
406 unsigned char local_lm_response[24];
408 #ifdef DEBUG_PASSWORD
409 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
410 dump_data(100, plaintext_password.data, plaintext_password.length);
411 #endif
413 SMBencrypt( (const char *)plaintext_password.data, (const uchar*)chal, local_lm_response);
414 local_lm_blob = data_blob(local_lm_response, 24);
416 /* We can't do an NT hash here, as the password needs to be
417 case insensitive */
418 local_nt_blob = data_blob(NULL, 0);
420 } else {
421 local_lm_blob = data_blob(NULL, 0);
422 local_nt_blob = data_blob(NULL, 0);
425 ret = make_user_info_map(user_info, smb_name,
426 client_domain,
427 get_remote_machine_name(),
428 local_lm_blob.data ? &local_lm_blob : NULL,
429 local_nt_blob.data ? &local_nt_blob : NULL,
430 NULL, NULL,
431 plaintext_password.data ? &plaintext_password : NULL,
432 False);
434 data_blob_free(&local_lm_blob);
435 return NT_STATUS_IS_OK(ret) ? True : False;
438 /****************************************************************************
439 Create an auth_usersupplied_data structure
440 ****************************************************************************/
442 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
443 const char *smb_name,
444 const char *client_domain,
445 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
447 return make_user_info_map(user_info, smb_name,
448 client_domain,
449 get_remote_machine_name(),
450 lm_resp.data ? &lm_resp : NULL,
451 nt_resp.data ? &nt_resp : NULL,
452 NULL, NULL, NULL,
453 True);
456 /****************************************************************************
457 Create a guest user_info blob, for anonymous authenticaion.
458 ****************************************************************************/
460 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
462 NTSTATUS nt_status;
464 nt_status = make_user_info(user_info,
465 "","",
466 "","",
467 "",
468 NULL, NULL,
469 NULL, NULL,
470 NULL,
471 True);
473 return NT_STATUS_IS_OK(nt_status) ? True : False;
476 /****************************************************************************
477 prints a NT_USER_TOKEN to debug output.
478 ****************************************************************************/
480 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
482 fstring sid_str;
483 size_t i;
485 if (!token) {
486 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
487 return;
490 DEBUGC(dbg_class, dbg_lev, ("NT user token of user %s\n",
491 sid_to_string(sid_str, &token->user_sids[0]) ));
492 DEBUGADDC(dbg_class, dbg_lev, ("contains %lu SIDs\n", (unsigned long)token->num_sids));
493 for (i = 0; i < token->num_sids; i++)
494 DEBUGADDC(dbg_class, dbg_lev, ("SID[%3lu]: %s\n", (unsigned long)i,
495 sid_to_string(sid_str, &token->user_sids[i])));
497 dump_se_priv( dbg_class, dbg_lev, &token->privileges );
500 /****************************************************************************
501 prints a UNIX 'token' to debug output.
502 ****************************************************************************/
504 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid, int n_groups, gid_t *groups)
506 int i;
507 DEBUGC(dbg_class, dbg_lev, ("UNIX token of user %ld\n", (long int)uid));
509 DEBUGADDC(dbg_class, dbg_lev, ("Primary group is %ld and contains %i supplementary groups\n", (long int)gid, n_groups));
510 for (i = 0; i < n_groups; i++)
511 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
512 (long int)groups[i]));
515 /****************************************************************************
516 Create the SID list for this user.
517 ****************************************************************************/
519 static NTSTATUS create_nt_user_token(const DOM_SID *user_sid, const DOM_SID *group_sid,
520 int n_groupSIDs, DOM_SID *groupSIDs,
521 BOOL is_guest, NT_USER_TOKEN **token)
523 NTSTATUS nt_status = NT_STATUS_OK;
524 NT_USER_TOKEN *ptoken;
525 int i;
526 int sid_ndx;
528 if ((ptoken = SMB_MALLOC_P(NT_USER_TOKEN)) == NULL) {
529 DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
530 nt_status = NT_STATUS_NO_MEMORY;
531 return nt_status;
534 ZERO_STRUCTP(ptoken);
536 ptoken->num_sids = n_groupSIDs + 5;
538 if ((ptoken->user_sids = SMB_MALLOC_ARRAY( DOM_SID, ptoken->num_sids )) == NULL) {
539 DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
540 nt_status = NT_STATUS_NO_MEMORY;
541 return nt_status;
544 memset((char*)ptoken->user_sids,0,sizeof(DOM_SID) * ptoken->num_sids);
547 * Note - user SID *MUST* be first in token !
548 * se_access_check depends on this.
550 * Primary group SID is second in token. Convention.
553 sid_copy(&ptoken->user_sids[PRIMARY_USER_SID_INDEX], user_sid);
554 if (group_sid)
555 sid_copy(&ptoken->user_sids[PRIMARY_GROUP_SID_INDEX], group_sid);
558 * Finally add the "standard" SIDs.
559 * The only difference between guest and "anonymous" (which we
560 * don't really support) is the addition of Authenticated_Users.
563 sid_copy(&ptoken->user_sids[2], &global_sid_World);
564 sid_copy(&ptoken->user_sids[3], &global_sid_Network);
566 if (is_guest)
567 sid_copy(&ptoken->user_sids[4], &global_sid_Builtin_Guests);
568 else
569 sid_copy(&ptoken->user_sids[4], &global_sid_Authenticated_Users);
571 sid_ndx = 5; /* next available spot */
573 for (i = 0; i < n_groupSIDs; i++) {
574 size_t check_sid_idx;
575 for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
576 if (sid_equal(&ptoken->user_sids[check_sid_idx],
577 &groupSIDs[i])) {
578 break;
582 if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
583 sid_copy(&ptoken->user_sids[sid_ndx++], &groupSIDs[i]);
584 } else {
585 ptoken->num_sids--;
589 /* add privileges assigned to this user */
591 get_privileges_for_sids( &ptoken->privileges, ptoken->user_sids, ptoken->num_sids );
593 debug_nt_user_token(DBGC_AUTH, 10, ptoken);
595 *token = ptoken;
597 return nt_status;
600 /****************************************************************************
601 Create the SID list for this user.
602 ****************************************************************************/
604 NT_USER_TOKEN *create_nt_token(uid_t uid, gid_t gid, int ngroups, gid_t *groups, BOOL is_guest)
606 DOM_SID user_sid;
607 DOM_SID group_sid;
608 DOM_SID *group_sids;
609 NT_USER_TOKEN *token;
610 int i;
612 if (!NT_STATUS_IS_OK(uid_to_sid(&user_sid, uid))) {
613 return NULL;
615 if (!NT_STATUS_IS_OK(gid_to_sid(&group_sid, gid))) {
616 return NULL;
619 group_sids = SMB_MALLOC_ARRAY(DOM_SID, ngroups);
620 if (!group_sids) {
621 DEBUG(0, ("create_nt_token: malloc() failed for DOM_SID list!\n"));
622 return NULL;
625 for (i = 0; i < ngroups; i++) {
626 if (!NT_STATUS_IS_OK(gid_to_sid(&(group_sids)[i], (groups)[i]))) {
627 DEBUG(1, ("create_nt_token: failed to convert gid %ld to a sid!\n", (long int)groups[i]));
628 SAFE_FREE(group_sids);
629 return NULL;
633 if (!NT_STATUS_IS_OK(create_nt_user_token(&user_sid, &group_sid,
634 ngroups, group_sids, is_guest, &token))) {
635 SAFE_FREE(group_sids);
636 return NULL;
639 SAFE_FREE(group_sids);
641 return token;
644 /******************************************************************************
645 * this function returns the groups (SIDs) of the local SAM the user is in.
646 * If this samba server is a DC of the domain the user belongs to, it returns
647 * both domain groups and local / builtin groups. If the user is in a trusted
648 * domain, or samba is a member server of a domain, then this function returns
649 * local and builtin groups the user is a member of.
651 * currently this is a hack, as there is no sam implementation that is capable
652 * of groups.
654 * NOTE!! This function will fail if you pass in a winbind user without
655 * the domain --jerry
656 ******************************************************************************/
658 static NTSTATUS get_user_groups(const char *username, uid_t uid, gid_t gid,
659 int *n_groups, DOM_SID **groups, gid_t **unix_groups)
661 int n_unix_groups;
662 int i;
664 *n_groups = 0;
665 *groups = NULL;
667 if (strchr(username, *lp_winbind_separator()) == NULL) {
668 NTSTATUS result;
670 become_root();
671 result = pdb_enum_group_memberships(username, gid, groups,
672 unix_groups, n_groups);
673 unbecome_root();
674 return result;
677 /* We have the separator, this must be winbind */
679 n_unix_groups = winbind_getgroups( username, unix_groups );
681 DEBUG(10,("get_user_groups: winbind_getgroups(%s): result = %s\n",
682 username, n_unix_groups == -1 ? "FAIL" : "SUCCESS"));
684 if ( n_unix_groups == -1 )
685 return NT_STATUS_NO_SUCH_USER; /* what should this return
686 * value be? */
688 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, n_unix_groups, *unix_groups);
690 /* now setup the space for storing the SIDS */
692 if (n_unix_groups > 0) {
694 *groups = SMB_MALLOC_ARRAY(DOM_SID, n_unix_groups);
696 if (!*groups) {
697 DEBUG(0, ("get_user_group: malloc() failed for DOM_SID list!\n"));
698 SAFE_FREE(*unix_groups);
699 return NT_STATUS_NO_MEMORY;
703 *n_groups = n_unix_groups;
705 for (i = 0; i < *n_groups; i++) {
706 if (!NT_STATUS_IS_OK(gid_to_sid(&(*groups)[i], (*unix_groups)[i]))) {
707 DEBUG(1, ("get_user_groups: failed to convert gid %ld to a sid!\n",
708 (long int)(*unix_groups)[i+1]));
709 SAFE_FREE(*groups);
710 SAFE_FREE(*unix_groups);
711 return NT_STATUS_NO_SUCH_USER;
715 return NT_STATUS_OK;
718 /***************************************************************************
719 Make a user_info struct
720 ***************************************************************************/
722 static NTSTATUS make_server_info(auth_serversupplied_info **server_info)
724 *server_info = SMB_MALLOC_P(auth_serversupplied_info);
725 if (!*server_info) {
726 DEBUG(0,("make_server_info: malloc failed!\n"));
727 return NT_STATUS_NO_MEMORY;
729 ZERO_STRUCTP(*server_info);
731 /* Initialise the uid and gid values to something non-zero
732 which may save us from giving away root access if there
733 is a bug in allocating these fields. */
735 (*server_info)->uid = -1;
736 (*server_info)->gid = -1;
738 return NT_STATUS_OK;
741 /***************************************************************************
742 Fill a server_info struct from a SAM_ACCOUNT with their groups
743 ***************************************************************************/
745 static NTSTATUS add_user_groups(auth_serversupplied_info **server_info,
746 const char * unix_username,
747 SAM_ACCOUNT *sampass,
748 uid_t uid, gid_t gid)
750 NTSTATUS nt_status;
751 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
752 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
753 int n_groupSIDs = 0;
754 DOM_SID *groupSIDs = NULL;
755 gid_t *unix_groups = NULL;
756 NT_USER_TOKEN *token;
757 BOOL is_guest;
758 uint32 rid;
760 nt_status = get_user_groups(unix_username, uid, gid,
761 &n_groupSIDs, &groupSIDs, &unix_groups);
763 if (!NT_STATUS_IS_OK(nt_status)) {
764 DEBUG(4,("get_user_groups_from_local_sam failed\n"));
765 free_server_info(server_info);
766 return nt_status;
769 is_guest = (sid_peek_rid(user_sid, &rid) && rid == DOMAIN_USER_RID_GUEST);
771 if (!NT_STATUS_IS_OK(nt_status = create_nt_user_token(user_sid, group_sid,
772 n_groupSIDs, groupSIDs, is_guest,
773 &token)))
775 DEBUG(4,("create_nt_user_token failed\n"));
776 SAFE_FREE(groupSIDs);
777 SAFE_FREE(unix_groups);
778 free_server_info(server_info);
779 return nt_status;
782 SAFE_FREE(groupSIDs);
784 (*server_info)->n_groups = n_groupSIDs;
785 (*server_info)->groups = unix_groups;
786 (*server_info)->ptok = token;
788 return nt_status;
791 /***************************************************************************
792 Make (and fill) a user_info struct from a SAM_ACCOUNT
793 ***************************************************************************/
795 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
796 SAM_ACCOUNT *sampass)
798 NTSTATUS nt_status;
799 struct passwd *pwd;
801 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info)))
802 return nt_status;
804 (*server_info)->sam_account = sampass;
806 if ( !(pwd = getpwnam_alloc(pdb_get_username(sampass))) ) {
807 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
808 pdb_get_username(sampass)));
809 free_server_info(server_info);
810 return NT_STATUS_NO_SUCH_USER;
812 (*server_info)->unix_name = smb_xstrdup(pwd->pw_name);
813 (*server_info)->gid = pwd->pw_gid;
814 (*server_info)->uid = pwd->pw_uid;
816 passwd_free(&pwd);
818 if (!NT_STATUS_IS_OK(nt_status = add_user_groups(server_info, pdb_get_username(sampass),
819 sampass,
820 (*server_info)->uid,
821 (*server_info)->gid)))
823 free_server_info(server_info);
824 return nt_status;
827 (*server_info)->sam_fill_level = SAM_FILL_ALL;
828 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
829 pdb_get_username(sampass),
830 (*server_info)->unix_name));
832 return nt_status;
835 /***************************************************************************
836 Make (and fill) a user_info struct from a 'struct passwd' by conversion
837 to a SAM_ACCOUNT
838 ***************************************************************************/
840 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info,
841 char *unix_username,
842 struct passwd *pwd)
844 NTSTATUS nt_status;
845 SAM_ACCOUNT *sampass = NULL;
846 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(&sampass, pwd))) {
847 return nt_status;
849 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info))) {
850 return nt_status;
853 (*server_info)->sam_account = sampass;
855 if (!NT_STATUS_IS_OK(nt_status = add_user_groups(server_info, unix_username,
856 sampass, pwd->pw_uid, pwd->pw_gid)))
858 return nt_status;
861 (*server_info)->unix_name = smb_xstrdup(unix_username);
863 (*server_info)->sam_fill_level = SAM_FILL_ALL;
864 (*server_info)->uid = pwd->pw_uid;
865 (*server_info)->gid = pwd->pw_gid;
866 return nt_status;
869 /***************************************************************************
870 Make (and fill) a user_info struct for a guest login.
871 ***************************************************************************/
873 static NTSTATUS make_new_server_info_guest(auth_serversupplied_info **server_info)
875 NTSTATUS nt_status;
876 SAM_ACCOUNT *sampass = NULL;
877 DOM_SID guest_sid;
879 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
880 return nt_status;
883 sid_copy(&guest_sid, get_global_sam_sid());
884 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
886 become_root();
887 if (!pdb_getsampwsid(sampass, &guest_sid)) {
888 unbecome_root();
889 return NT_STATUS_NO_SUCH_USER;
891 unbecome_root();
893 nt_status = make_server_info_sam(server_info, sampass);
895 if (NT_STATUS_IS_OK(nt_status)) {
896 static const char zeros[16];
897 (*server_info)->guest = True;
899 /* annoying, but the Guest really does have a session key,
900 and it is all zeros! */
901 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
902 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
905 return nt_status;
908 static auth_serversupplied_info *copy_serverinfo(auth_serversupplied_info *src)
910 auth_serversupplied_info *dst;
912 if (!NT_STATUS_IS_OK(make_server_info(&dst)))
913 return NULL;
915 dst->guest = src->guest;
916 dst->uid = src->uid;
917 dst->gid = src->gid;
918 dst->n_groups = src->n_groups;
919 if (src->n_groups != 0)
920 dst->groups = memdup(src->groups, sizeof(gid_t)*dst->n_groups);
921 else
922 dst->groups = NULL;
923 dst->ptok = dup_nt_token(src->ptok);
924 dst->user_session_key = data_blob(src->user_session_key.data,
925 src->user_session_key.length);
926 dst->lm_session_key = data_blob(src->lm_session_key.data,
927 src->lm_session_key.length);
928 pdb_copy_sam_account(src->sam_account, &dst->sam_account);
929 dst->pam_handle = NULL;
930 dst->unix_name = smb_xstrdup(src->unix_name);
932 return dst;
935 static auth_serversupplied_info *guest_info = NULL;
937 BOOL init_guest_info(void)
939 if (guest_info != NULL)
940 return True;
942 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info));
945 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
947 *server_info = copy_serverinfo(guest_info);
948 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
951 /***************************************************************************
952 Purely internal function for make_server_info_info3
953 Fill the sam account from getpwnam
954 ***************************************************************************/
955 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx,
956 const char *domain,
957 const char *username,
958 char **found_username,
959 uid_t *uid, gid_t *gid,
960 SAM_ACCOUNT **sam_account)
962 fstring dom_user, lower_username;
963 fstring real_username;
964 struct passwd *passwd;
966 fstrcpy( lower_username, username );
967 strlower_m( lower_username );
969 fstr_sprintf(dom_user, "%s%c%s", domain, *lp_winbind_separator(),
970 lower_username);
972 /* get the passwd struct but don't create the user if he/she
973 does not exist. We were explicitly called from a following
974 a winbindd authentication request so we should assume that
975 nss_winbindd is working */
977 map_username( dom_user );
979 if ( !(passwd = smb_getpwnam( dom_user, real_username, True )) )
980 return NT_STATUS_NO_SUCH_USER;
982 *uid = passwd->pw_uid;
983 *gid = passwd->pw_gid;
985 /* This is pointless -- there is no suport for differing
986 unix and windows names. Make sure to always store the
987 one we actually looked up and succeeded. Have I mentioned
988 why I hate the 'winbind use default domain' parameter?
989 --jerry */
991 *found_username = talloc_strdup( mem_ctx, real_username );
993 DEBUG(5,("fill_sam_account: located username was [%s]\n",
994 *found_username));
996 return pdb_init_sam_pw(sam_account, passwd);
999 /****************************************************************************
1000 Wrapper to allow the getpwnam() call to strip the domain name and
1001 try again in case a local UNIX user is already there. Also run through
1002 the username if we fallback to the username only.
1003 ****************************************************************************/
1005 struct passwd *smb_getpwnam( char *domuser, fstring save_username, BOOL create )
1007 struct passwd *pw = NULL;
1008 char *p;
1009 fstring username;
1011 /* we only save a copy of the username it has been mangled
1012 by winbindd use default domain */
1014 save_username[0] = '\0';
1016 /* don't call map_username() here since it has to be done higher
1017 up the stack so we don't call it mutliple times */
1019 fstrcpy( username, domuser );
1021 p = strchr_m( username, *lp_winbind_separator() );
1023 /* code for a DOMAIN\user string */
1025 if ( p ) {
1026 fstring strip_username;
1028 pw = Get_Pwnam( domuser );
1029 if ( pw ) {
1030 /* make sure we get the case of the username correct */
1031 /* work around 'winbind use default domain = yes' */
1033 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1034 char *domain;
1036 /* split the domain and username into 2 strings */
1037 *p = '\0';
1038 domain = username;
1040 fstr_sprintf(save_username, "%s%c%s", domain, *lp_winbind_separator(), pw->pw_name);
1042 else
1043 fstrcpy( save_username, pw->pw_name );
1045 /* whew -- done! */
1046 return pw;
1049 /* setup for lookup of just the username */
1050 /* remember that p and username are overlapping memory */
1052 p++;
1053 fstrcpy( strip_username, p );
1054 fstrcpy( username, strip_username );
1057 /* just lookup a plain username */
1059 pw = Get_Pwnam(username);
1061 /* Create local user if requested. */
1063 if ( !pw && create ) {
1064 /* Don't add a machine account. */
1065 if (username[strlen(username)-1] == '$')
1066 return NULL;
1068 auth_add_user_script(NULL, username);
1069 pw = Get_Pwnam(username);
1072 /* one last check for a valid passwd struct */
1074 if ( pw )
1075 fstrcpy( save_username, pw->pw_name );
1077 return pw;
1080 /***************************************************************************
1081 Make a server_info struct from the info3 returned by a domain logon
1082 ***************************************************************************/
1084 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1085 const char *internal_username,
1086 const char *sent_nt_username,
1087 const char *domain,
1088 auth_serversupplied_info **server_info,
1089 NET_USER_INFO_3 *info3)
1091 static const char zeros[16];
1093 NTSTATUS nt_status = NT_STATUS_OK;
1094 char *found_username;
1095 const char *nt_domain;
1096 const char *nt_username;
1098 SAM_ACCOUNT *sam_account = NULL;
1099 DOM_SID user_sid;
1100 DOM_SID group_sid;
1102 uid_t uid;
1103 gid_t gid;
1105 int n_lgroupSIDs;
1106 DOM_SID *lgroupSIDs = NULL;
1108 gid_t *unix_groups = NULL;
1109 NT_USER_TOKEN *token;
1111 DOM_SID *all_group_SIDs;
1112 size_t i;
1115 Here is where we should check the list of
1116 trusted domains, and verify that the SID
1117 matches.
1120 sid_copy(&user_sid, &info3->dom_sid.sid);
1121 if (!sid_append_rid(&user_sid, info3->user_rid)) {
1122 return NT_STATUS_INVALID_PARAMETER;
1125 sid_copy(&group_sid, &info3->dom_sid.sid);
1126 if (!sid_append_rid(&group_sid, info3->group_rid)) {
1127 return NT_STATUS_INVALID_PARAMETER;
1130 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
1131 /* If the server didn't give us one, just use the one we sent them */
1132 nt_username = sent_nt_username;
1135 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
1136 /* If the server didn't give us one, just use the one we sent them */
1137 nt_domain = domain;
1140 /* try to fill the SAM account.. If getpwnam() fails, then try the
1141 add user script (2.2.x behavior).
1143 We use the _unmapped_ username here in an attempt to provide
1144 consistent username mapping behavior between kerberos and NTLM[SSP]
1145 authentication in domain mode security. I.E. Username mapping should
1146 be applied to the fully qualified username (e.g. DOMAIN\user) and
1147 no just the login name. Yes this mean swe called map_username()
1148 unnecessarily in make_user_info_map() but that is how the current
1149 code is designed. Making the change here is the least disruptive
1150 place. -- jerry */
1152 nt_status = fill_sam_account(mem_ctx, nt_domain, sent_nt_username,
1153 &found_username, &uid, &gid, &sam_account);
1155 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1156 DEBUG(3,("User %s does not exist, trying to add it\n", internal_username));
1157 auth_add_user_script( nt_domain, sent_nt_username );
1158 nt_status = fill_sam_account( mem_ctx, nt_domain, sent_nt_username,
1159 &found_username, &uid, &gid, &sam_account );
1162 if (!NT_STATUS_IS_OK(nt_status)) {
1163 DEBUG(0, ("make_server_info_info3: pdb_init_sam failed!\n"));
1164 return nt_status;
1167 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1168 pdb_free_sam(&sam_account);
1169 return NT_STATUS_NO_MEMORY;
1172 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1173 pdb_free_sam(&sam_account);
1174 return NT_STATUS_NO_MEMORY;
1177 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1178 pdb_free_sam(&sam_account);
1179 return NT_STATUS_NO_MEMORY;
1182 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1183 pdb_free_sam(&sam_account);
1184 return NT_STATUS_UNSUCCESSFUL;
1187 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1188 pdb_free_sam(&sam_account);
1189 return NT_STATUS_UNSUCCESSFUL;
1192 if (!pdb_set_fullname(sam_account, unistr2_static(&(info3->uni_full_name)),
1193 PDB_CHANGED)) {
1194 pdb_free_sam(&sam_account);
1195 return NT_STATUS_NO_MEMORY;
1198 if (!pdb_set_logon_script(sam_account, unistr2_static(&(info3->uni_logon_script)), PDB_CHANGED)) {
1199 pdb_free_sam(&sam_account);
1200 return NT_STATUS_NO_MEMORY;
1203 if (!pdb_set_profile_path(sam_account, unistr2_static(&(info3->uni_profile_path)), PDB_CHANGED)) {
1204 pdb_free_sam(&sam_account);
1205 return NT_STATUS_NO_MEMORY;
1208 if (!pdb_set_homedir(sam_account, unistr2_static(&(info3->uni_home_dir)), PDB_CHANGED)) {
1209 pdb_free_sam(&sam_account);
1210 return NT_STATUS_NO_MEMORY;
1213 if (!pdb_set_dir_drive(sam_account, unistr2_static(&(info3->uni_dir_drive)), PDB_CHANGED)) {
1214 pdb_free_sam(&sam_account);
1215 return NT_STATUS_NO_MEMORY;
1218 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info))) {
1219 DEBUG(4, ("make_server_info failed!\n"));
1220 pdb_free_sam(&sam_account);
1221 return nt_status;
1224 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1225 valid SAM_ACCOUNT) */
1227 (*server_info)->sam_account = sam_account;
1229 (*server_info)->unix_name = smb_xstrdup(found_username);
1231 /* Fill in the unix info we found on the way */
1233 (*server_info)->sam_fill_level = SAM_FILL_ALL;
1234 (*server_info)->uid = uid;
1235 (*server_info)->gid = gid;
1237 /* Store the user group information in the server_info
1238 returned to the caller. */
1240 nt_status = get_user_groups((*server_info)->unix_name,
1241 uid, gid, &n_lgroupSIDs, &lgroupSIDs, &unix_groups);
1243 if ( !NT_STATUS_IS_OK(nt_status) ) {
1244 DEBUG(4,("get_user_groups failed\n"));
1245 return nt_status;
1248 (*server_info)->groups = unix_groups;
1249 (*server_info)->n_groups = n_lgroupSIDs;
1251 /* Create a 'combined' list of all SIDs we might want in the SD */
1253 all_group_SIDs = SMB_MALLOC_ARRAY(DOM_SID,info3->num_groups2 + info3->num_other_sids + n_lgroupSIDs);
1255 if (!all_group_SIDs) {
1256 DEBUG(0, ("malloc() failed for DOM_SID list!\n"));
1257 SAFE_FREE(lgroupSIDs);
1258 free_server_info(server_info);
1259 return NT_STATUS_NO_MEMORY;
1262 /* and create (by appending rids) the 'domain' sids */
1264 for (i = 0; i < info3->num_groups2; i++) {
1266 sid_copy(&all_group_SIDs[i], &(info3->dom_sid.sid));
1268 if (!sid_append_rid(&all_group_SIDs[i], info3->gids[i].g_rid)) {
1270 nt_status = NT_STATUS_INVALID_PARAMETER;
1272 DEBUG(3,("could not append additional group rid 0x%x\n",
1273 info3->gids[i].g_rid));
1275 SAFE_FREE(lgroupSIDs);
1276 SAFE_FREE(all_group_SIDs);
1277 free_server_info(server_info);
1279 return nt_status;
1284 /* Copy 'other' sids. We need to do sid filtering here to
1285 prevent possible elevation of privileges. See:
1287 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1290 for (i = 0; i < info3->num_other_sids; i++) {
1291 sid_copy(&all_group_SIDs[info3->num_groups2 + i],
1292 &info3->other_sids[i].sid);
1296 /* add local alias sids */
1298 for (i = 0; i < n_lgroupSIDs; i++) {
1299 sid_copy(&all_group_SIDs[info3->num_groups2 +
1300 info3->num_other_sids + i],
1301 &lgroupSIDs[i]);
1304 /* Where are the 'global' sids... */
1306 /* can the user be guest? if yes, where is it stored? */
1308 nt_status = create_nt_user_token(&user_sid, &group_sid,
1309 info3->num_groups2 + info3->num_other_sids + n_lgroupSIDs,
1310 all_group_SIDs, False, &token);
1312 if ( !NT_STATUS_IS_OK(nt_status) ) {
1313 DEBUG(4,("create_nt_user_token failed\n"));
1314 SAFE_FREE(lgroupSIDs);
1315 SAFE_FREE(all_group_SIDs);
1316 free_server_info(server_info);
1317 return nt_status;
1320 (*server_info)->ptok = token;
1322 SAFE_FREE(lgroupSIDs);
1323 SAFE_FREE(all_group_SIDs);
1325 /* ensure we are never given NULL session keys */
1327 if (memcmp(info3->user_sess_key, zeros, sizeof(zeros)) == 0) {
1328 (*server_info)->user_session_key = data_blob(NULL, 0);
1329 } else {
1330 (*server_info)->user_session_key = data_blob(info3->user_sess_key, sizeof(info3->user_sess_key));
1333 if (memcmp(info3->lm_sess_key, zeros, 8) == 0) {
1334 (*server_info)->lm_session_key = data_blob(NULL, 0);
1335 } else {
1336 (*server_info)->lm_session_key = data_blob(info3->lm_sess_key, sizeof(info3->lm_sess_key));
1339 return NT_STATUS_OK;
1342 /***************************************************************************
1343 Free a user_info struct
1344 ***************************************************************************/
1346 void free_user_info(auth_usersupplied_info **user_info)
1348 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1349 if (*user_info != NULL) {
1350 if ((*user_info)->smb_name.str) {
1351 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
1353 SAFE_FREE((*user_info)->smb_name.str);
1354 SAFE_FREE((*user_info)->internal_username.str);
1355 SAFE_FREE((*user_info)->client_domain.str);
1356 SAFE_FREE((*user_info)->domain.str);
1357 SAFE_FREE((*user_info)->wksta_name.str);
1358 data_blob_free(&(*user_info)->lm_resp);
1359 data_blob_free(&(*user_info)->nt_resp);
1360 data_blob_clear_free(&(*user_info)->lm_interactive_pwd);
1361 data_blob_clear_free(&(*user_info)->nt_interactive_pwd);
1362 data_blob_clear_free(&(*user_info)->plaintext_password);
1363 ZERO_STRUCT(**user_info);
1365 SAFE_FREE(*user_info);
1368 /***************************************************************************
1369 Clear out a server_info struct that has been allocated
1370 ***************************************************************************/
1372 void free_server_info(auth_serversupplied_info **server_info)
1374 DEBUG(5,("attempting to free (and zero) a server_info structure\n"));
1375 if (*server_info != NULL) {
1376 pdb_free_sam(&(*server_info)->sam_account);
1378 /* call pam_end here, unless we know we are keeping it */
1379 delete_nt_token( &(*server_info)->ptok );
1380 SAFE_FREE((*server_info)->groups);
1381 SAFE_FREE((*server_info)->unix_name);
1382 data_blob_free(&(*server_info)->lm_session_key);
1383 data_blob_free(&(*server_info)->user_session_key);
1384 ZERO_STRUCT(**server_info);
1386 SAFE_FREE(*server_info);
1389 /***************************************************************************
1390 Make an auth_methods struct
1391 ***************************************************************************/
1393 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
1395 if (!auth_context) {
1396 smb_panic("no auth_context supplied to make_auth_methods()!\n");
1399 if (!auth_method) {
1400 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
1403 *auth_method = TALLOC_P(auth_context->mem_ctx, auth_methods);
1404 if (!*auth_method) {
1405 DEBUG(0,("make_auth_method: malloc failed!\n"));
1406 return False;
1408 ZERO_STRUCTP(*auth_method);
1410 return True;
1413 /****************************************************************************
1414 Delete a SID token.
1415 ****************************************************************************/
1417 void delete_nt_token(NT_USER_TOKEN **pptoken)
1419 if (*pptoken) {
1420 NT_USER_TOKEN *ptoken = *pptoken;
1422 SAFE_FREE( ptoken->user_sids );
1423 ZERO_STRUCTP(ptoken);
1425 SAFE_FREE(*pptoken);
1428 /****************************************************************************
1429 Duplicate a SID token.
1430 ****************************************************************************/
1432 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
1434 NT_USER_TOKEN *token;
1436 if (!ptoken)
1437 return NULL;
1439 if ((token = SMB_MALLOC_P(NT_USER_TOKEN)) == NULL)
1440 return NULL;
1442 ZERO_STRUCTP(token);
1444 token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids );
1446 if ( !token ) {
1447 SAFE_FREE(token);
1448 return NULL;
1451 token->num_sids = ptoken->num_sids;
1453 /* copy the privileges; don't consider failure to be critical here */
1455 if ( !se_priv_copy( &token->privileges, &ptoken->privileges ) ) {
1456 DEBUG(0,("dup_nt_token: Failure to copy SE_PRIV!. Continuing with 0 privileges assigned.\n"));
1459 return token;
1462 /****************************************************************************
1463 Check for a SID in an NT_USER_TOKEN
1464 ****************************************************************************/
1466 BOOL nt_token_check_sid ( DOM_SID *sid, NT_USER_TOKEN *token )
1468 int i;
1470 if ( !sid || !token )
1471 return False;
1473 for ( i=0; i<token->num_sids; i++ ) {
1474 if ( sid_equal( sid, &token->user_sids[i] ) )
1475 return True;
1478 return False;
1481 BOOL nt_token_check_domain_rid( NT_USER_TOKEN *token, uint32 rid )
1483 DOM_SID domain_sid;
1485 sid_copy( &domain_sid, get_global_sam_sid() );
1486 sid_append_rid( &domain_sid, rid );
1488 return nt_token_check_sid( &domain_sid, token );\
1492 * Verify whether or not given domain is trusted.
1494 * @param domain_name name of the domain to be verified
1495 * @return true if domain is one of the trusted once or
1496 * false if otherwise
1499 BOOL is_trusted_domain(const char* dom_name)
1501 DOM_SID trustdom_sid;
1502 char *pass = NULL;
1503 time_t lct;
1504 BOOL ret;
1506 /* no trusted domains for a standalone server */
1508 if ( lp_server_role() == ROLE_STANDALONE )
1509 return False;
1511 /* if we are a DC, then check for a direct trust relationships */
1513 if ( IS_DC ) {
1514 become_root();
1515 DEBUG (5,("is_trusted_domain: Checking for domain trust with [%s]\n",
1516 dom_name ));
1517 ret = secrets_fetch_trusted_domain_password(dom_name, &pass, &trustdom_sid, &lct);
1518 unbecome_root();
1519 SAFE_FREE(pass);
1520 if (ret)
1521 return True;
1523 else {
1524 /* if winbindd is not up and we are a domain member) then we need to update the
1525 trustdom_cache ourselves */
1527 if ( !winbind_ping() )
1528 update_trustdom_cache();
1531 /* now the trustdom cache should be available a DC could still
1532 * have a transitive trust so fall back to the cache of trusted
1533 * domains (like a domain member would use */
1535 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {
1536 return True;
1539 return False;