Idra, your privileges patch allowed login only with tdbsam. The problem is
[Samba/gebeck_regimport.git] / source / auth / auth_util.c
blob871b399b86dcb1eaeed7af58b910dd1cf03c9681
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 plaintext,
129 uint32 auth_flags, BOOL encrypted)
132 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
134 *user_info = malloc(sizeof(**user_info));
135 if (!user_info) {
136 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
137 return NT_STATUS_NO_MEMORY;
140 ZERO_STRUCTP(*user_info);
142 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
144 (*user_info)->smb_name.str = strdup(smb_name);
145 if ((*user_info)->smb_name.str) {
146 (*user_info)->smb_name.len = strlen(smb_name);
147 } else {
148 free_user_info(user_info);
149 return NT_STATUS_NO_MEMORY;
152 (*user_info)->internal_username.str = strdup(internal_username);
153 if ((*user_info)->internal_username.str) {
154 (*user_info)->internal_username.len = strlen(internal_username);
155 } else {
156 free_user_info(user_info);
157 return NT_STATUS_NO_MEMORY;
160 (*user_info)->domain.str = strdup(domain);
161 if ((*user_info)->domain.str) {
162 (*user_info)->domain.len = strlen(domain);
163 } else {
164 free_user_info(user_info);
165 return NT_STATUS_NO_MEMORY;
168 (*user_info)->client_domain.str = strdup(client_domain);
169 if ((*user_info)->client_domain.str) {
170 (*user_info)->client_domain.len = strlen(client_domain);
171 } else {
172 free_user_info(user_info);
173 return NT_STATUS_NO_MEMORY;
176 (*user_info)->wksta_name.str = strdup(wksta_name);
177 if ((*user_info)->wksta_name.str) {
178 (*user_info)->wksta_name.len = strlen(wksta_name);
179 } else {
180 free_user_info(user_info);
181 return NT_STATUS_NO_MEMORY;
184 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
186 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
187 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
188 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
190 (*user_info)->encrypted = encrypted;
191 (*user_info)->auth_flags = auth_flags;
193 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
195 return NT_STATUS_OK;
198 /****************************************************************************
199 Create an auth_usersupplied_data structure after appropriate mapping.
200 ****************************************************************************/
202 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info,
203 const char *smb_name,
204 const char *client_domain,
205 const char *wksta_name,
206 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
207 DATA_BLOB plaintext,
208 uint32 ntlmssp_flags, BOOL encrypted)
210 const char *domain;
211 fstring internal_username;
212 fstrcpy(internal_username, smb_name);
213 map_username(internal_username);
215 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
216 client_domain, smb_name, wksta_name));
218 /* don't allow "" as a domain, fixes a Win9X bug
219 where it doens't supply a domain for logon script
220 'net use' commands. */
222 if ( *client_domain )
223 domain = client_domain;
224 else
225 domain = lp_workgroup();
227 /* do what win2k does. Always map unknown domains to our own
228 and let the "passdb backend" handle unknown users. */
230 if ( !is_trusted_domain(domain) && !strequal(domain, get_global_sam_name()) )
231 domain = get_default_sam_name();
233 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
235 return make_user_info(user_info, smb_name, internal_username,
236 client_domain, domain, wksta_name, lm_pwd, nt_pwd,
237 plaintext, ntlmssp_flags, encrypted);
240 /****************************************************************************
241 Create an auth_usersupplied_data, making the DATA_BLOBs here.
242 Decrypt and encrypt the passwords.
243 ****************************************************************************/
245 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
246 const char *smb_name,
247 const char *client_domain,
248 const char *wksta_name,
249 const uchar *lm_network_pwd, int lm_pwd_len,
250 const uchar *nt_network_pwd, int nt_pwd_len)
252 BOOL ret;
253 NTSTATUS nt_status;
254 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
255 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
256 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
257 uint32 auth_flags = AUTH_FLAG_NONE;
259 if (lm_pwd_len)
260 auth_flags |= AUTH_FLAG_LM_RESP;
261 if (nt_pwd_len == 24) {
262 auth_flags |= AUTH_FLAG_NTLM_RESP;
263 } else if (nt_pwd_len != 0) {
264 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
267 nt_status = make_user_info_map(user_info,
268 smb_name, client_domain,
269 wksta_name,
270 lm_blob, nt_blob,
271 plaintext_blob,
272 auth_flags, True);
274 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
276 data_blob_free(&lm_blob);
277 data_blob_free(&nt_blob);
278 return ret;
281 /****************************************************************************
282 Create an auth_usersupplied_data, making the DATA_BLOBs here.
283 Decrypt and encrypt the passwords.
284 ****************************************************************************/
286 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
287 const char *smb_name,
288 const char *client_domain,
289 const char *wksta_name,
290 const uchar chal[8],
291 const uchar lm_interactive_pwd[16],
292 const uchar nt_interactive_pwd[16],
293 const uchar *dc_sess_key)
295 char lm_pwd[16];
296 char nt_pwd[16];
297 unsigned char local_lm_response[24];
298 unsigned char local_nt_response[24];
299 unsigned char key[16];
300 uint32 auth_flags = AUTH_FLAG_NONE;
302 ZERO_STRUCT(key);
303 memcpy(key, dc_sess_key, 8);
305 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
306 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
308 #ifdef DEBUG_PASSWORD
309 DEBUG(100,("key:"));
310 dump_data(100, (char *)key, sizeof(key));
312 DEBUG(100,("lm owf password:"));
313 dump_data(100, lm_pwd, sizeof(lm_pwd));
315 DEBUG(100,("nt owf password:"));
316 dump_data(100, nt_pwd, sizeof(nt_pwd));
317 #endif
319 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
320 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
322 #ifdef DEBUG_PASSWORD
323 DEBUG(100,("decrypt of lm owf password:"));
324 dump_data(100, lm_pwd, sizeof(lm_pwd));
326 DEBUG(100,("decrypt of nt owf password:"));
327 dump_data(100, nt_pwd, sizeof(nt_pwd));
328 #endif
330 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
331 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
333 /* Password info paranoia */
334 ZERO_STRUCT(lm_pwd);
335 ZERO_STRUCT(nt_pwd);
336 ZERO_STRUCT(key);
339 BOOL ret;
340 NTSTATUS nt_status;
341 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
342 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
343 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
345 if (lm_interactive_pwd)
346 auth_flags |= AUTH_FLAG_LM_RESP;
347 if (nt_interactive_pwd)
348 auth_flags |= AUTH_FLAG_NTLM_RESP;
350 nt_status = make_user_info_map(user_info,
351 smb_name, client_domain,
352 wksta_name,
353 local_lm_blob,
354 local_nt_blob,
355 plaintext_blob,
356 auth_flags, True);
358 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
359 data_blob_free(&local_lm_blob);
360 data_blob_free(&local_nt_blob);
361 return ret;
366 /****************************************************************************
367 Create an auth_usersupplied_data structure
368 ****************************************************************************/
370 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
371 const char *smb_name,
372 const char *client_domain,
373 const uint8 chal[8],
374 DATA_BLOB plaintext_password)
377 DATA_BLOB local_lm_blob;
378 DATA_BLOB local_nt_blob;
379 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
380 uint32 auth_flags = AUTH_FLAG_NONE;
383 * Not encrypted - do so.
386 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
388 if (plaintext_password.data) {
389 unsigned char local_lm_response[24];
391 #ifdef DEBUG_PASSWORD
392 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
393 dump_data(100, plaintext_password.data, plaintext_password.length);
394 #endif
396 SMBencrypt( (const char *)plaintext_password.data, (const uchar*)chal, local_lm_response);
397 local_lm_blob = data_blob(local_lm_response, 24);
399 /* We can't do an NT hash here, as the password needs to be
400 case insensitive */
401 local_nt_blob = data_blob(NULL, 0);
403 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
404 } else {
405 local_lm_blob = data_blob(NULL, 0);
406 local_nt_blob = data_blob(NULL, 0);
409 ret = make_user_info_map(user_info, smb_name,
410 client_domain,
411 get_remote_machine_name(),
412 local_lm_blob,
413 local_nt_blob,
414 plaintext_password,
415 auth_flags, False);
417 data_blob_free(&local_lm_blob);
418 return NT_STATUS_IS_OK(ret) ? True : False;
421 /****************************************************************************
422 Create an auth_usersupplied_data structure
423 ****************************************************************************/
425 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
426 const char *smb_name,
427 const char *client_domain,
428 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
430 uint32 auth_flags = AUTH_FLAG_NONE;
432 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
434 if (lm_resp.length == 24) {
435 auth_flags |= AUTH_FLAG_LM_RESP;
437 if (nt_resp.length == 0) {
438 } else if (nt_resp.length == 24) {
439 auth_flags |= AUTH_FLAG_NTLM_RESP;
440 } else {
441 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
444 return make_user_info_map(user_info, smb_name,
445 client_domain,
446 get_remote_machine_name(),
447 lm_resp,
448 nt_resp,
449 no_plaintext_blob,
450 auth_flags, True);
453 /****************************************************************************
454 Create a guest user_info blob, for anonymous authenticaion.
455 ****************************************************************************/
457 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
459 DATA_BLOB lm_blob = data_blob(NULL, 0);
460 DATA_BLOB nt_blob = data_blob(NULL, 0);
461 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
462 uint32 auth_flags = AUTH_FLAG_NONE;
463 NTSTATUS nt_status;
465 nt_status = make_user_info(user_info,
466 "","",
467 "","",
468 "",
469 nt_blob, lm_blob,
470 plaintext_blob,
471 auth_flags, 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])));
498 /****************************************************************************
499 prints a UNIX 'token' to debug output.
500 ****************************************************************************/
502 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid, int n_groups, gid_t *groups)
504 int i;
505 DEBUGC(dbg_class, dbg_lev, ("UNIX token of user %ld\n", (long int)uid));
507 DEBUGADDC(dbg_class, dbg_lev, ("Primary group is %ld and contains %i supplementary groups\n", (long int)gid, n_groups));
508 for (i = 0; i < n_groups; i++)
509 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
510 (long int)groups[i]));
513 /****************************************************************************
514 Create the SID list for this user.
515 ****************************************************************************/
517 static NTSTATUS create_nt_user_token(const DOM_SID *user_sid, const DOM_SID *group_sid,
518 int n_groupSIDs, DOM_SID *groupSIDs,
519 BOOL is_guest, NT_USER_TOKEN **token)
521 NTSTATUS nt_status = NT_STATUS_OK;
522 NT_USER_TOKEN *ptoken;
523 int i;
524 int sid_ndx;
526 if ((ptoken = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
527 DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
528 nt_status = NT_STATUS_NO_MEMORY;
529 return nt_status;
532 ZERO_STRUCTP(ptoken);
534 ptoken->num_sids = n_groupSIDs + 5;
536 if ((ptoken->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
537 DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
538 nt_status = NT_STATUS_NO_MEMORY;
539 return nt_status;
542 memset((char*)ptoken->user_sids,0,sizeof(DOM_SID) * ptoken->num_sids);
545 * Note - user SID *MUST* be first in token !
546 * se_access_check depends on this.
548 * Primary group SID is second in token. Convention.
551 sid_copy(&ptoken->user_sids[PRIMARY_USER_SID_INDEX], user_sid);
552 if (group_sid)
553 sid_copy(&ptoken->user_sids[PRIMARY_GROUP_SID_INDEX], group_sid);
556 * Finally add the "standard" SIDs.
557 * The only difference between guest and "anonymous" (which we
558 * don't really support) is the addition of Authenticated_Users.
561 sid_copy(&ptoken->user_sids[2], &global_sid_World);
562 sid_copy(&ptoken->user_sids[3], &global_sid_Network);
564 if (is_guest)
565 sid_copy(&ptoken->user_sids[4], &global_sid_Builtin_Guests);
566 else
567 sid_copy(&ptoken->user_sids[4], &global_sid_Authenticated_Users);
569 sid_ndx = 5; /* next available spot */
571 for (i = 0; i < n_groupSIDs; i++) {
572 size_t check_sid_idx;
573 for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
574 if (sid_equal(&ptoken->user_sids[check_sid_idx],
575 &groupSIDs[i])) {
576 break;
580 if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
581 sid_copy(&ptoken->user_sids[sid_ndx++], &groupSIDs[i]);
582 } else {
583 ptoken->num_sids--;
587 debug_nt_user_token(DBGC_AUTH, 10, ptoken);
589 *token = ptoken;
591 return nt_status;
594 /****************************************************************************
595 Create the SID list for this user.
596 ****************************************************************************/
598 NT_USER_TOKEN *create_nt_token(uid_t uid, gid_t gid, int ngroups, gid_t *groups, BOOL is_guest)
600 DOM_SID user_sid;
601 DOM_SID group_sid;
602 DOM_SID *group_sids;
603 NT_USER_TOKEN *token;
604 int i;
606 if (!NT_STATUS_IS_OK(uid_to_sid(&user_sid, uid))) {
607 return NULL;
609 if (!NT_STATUS_IS_OK(gid_to_sid(&group_sid, gid))) {
610 return NULL;
613 group_sids = malloc(sizeof(DOM_SID) * ngroups);
614 if (!group_sids) {
615 DEBUG(0, ("create_nt_token: malloc() failed for DOM_SID list!\n"));
616 return NULL;
619 for (i = 0; i < ngroups; i++) {
620 if (!NT_STATUS_IS_OK(gid_to_sid(&(group_sids)[i], (groups)[i]))) {
621 DEBUG(1, ("create_nt_token: failed to convert gid %ld to a sid!\n", (long int)groups[i]));
622 SAFE_FREE(group_sids);
623 return NULL;
627 if (!NT_STATUS_IS_OK(create_nt_user_token(&user_sid, &group_sid,
628 ngroups, group_sids, is_guest, &token))) {
629 SAFE_FREE(group_sids);
630 return NULL;
633 SAFE_FREE(group_sids);
635 return token;
638 static void add_foreign_gids(uid_t uid, gid_t gid,
639 gid_t **groups, int *ngroups)
641 int i, dom_groups;
642 DOM_SID sid;
644 if (NT_STATUS_IS_OK(uid_to_sid(&sid, uid)))
645 add_foreign_gids_from_sid(&sid, groups, ngroups);
647 if (NT_STATUS_IS_OK(gid_to_sid(&sid, gid)))
648 add_foreign_gids_from_sid(&sid, groups, ngroups);
650 dom_groups = *ngroups;
652 for (i=0; i<dom_groups; i++) {
654 if (!NT_STATUS_IS_OK(gid_to_sid(&sid, (*groups)[i])))
655 continue;
657 add_foreign_gids_from_sid(&sid, groups, ngroups);
661 /******************************************************************************
662 * this function returns the groups (SIDs) of the local SAM the user is in.
663 * If this samba server is a DC of the domain the user belongs to, it returns
664 * both domain groups and local / builtin groups. If the user is in a trusted
665 * domain, or samba is a member server of a domain, then this function returns
666 * local and builtin groups the user is a member of.
668 * currently this is a hack, as there is no sam implementation that is capable
669 * of groups.
671 * NOTE!! This function will fail if you pass in a winbind user without
672 * the domain --jerry
673 ******************************************************************************/
675 static NTSTATUS get_user_groups(const char *username, uid_t uid, gid_t gid,
676 int *n_groups, DOM_SID **groups, gid_t **unix_groups)
678 int n_unix_groups;
679 int i;
681 *n_groups = 0;
682 *groups = NULL;
684 /* Try winbind first */
686 if ( strchr(username, *lp_winbind_separator()) ) {
687 n_unix_groups = winbind_getgroups( username, unix_groups );
689 DEBUG(10,("get_user_groups: winbind_getgroups(%s): result = %s\n", username,
690 n_unix_groups == -1 ? "FAIL" : "SUCCESS"));
692 if ( n_unix_groups == -1 )
693 return NT_STATUS_NO_SUCH_USER; /* what should this return value be? */
695 else {
696 /* fallback to getgrouplist() */
698 n_unix_groups = groups_max();
700 if ((*unix_groups = malloc( sizeof(gid_t) * n_unix_groups ) ) == NULL) {
701 DEBUG(0, ("get_user_groups: Out of memory allocating unix group list\n"));
702 return NT_STATUS_NO_MEMORY;
705 if (sys_getgrouplist(username, gid, *unix_groups, &n_unix_groups) == -1) {
707 gid_t *groups_tmp;
709 groups_tmp = Realloc(*unix_groups, sizeof(gid_t) * n_unix_groups);
711 if (!groups_tmp) {
712 SAFE_FREE(*unix_groups);
713 return NT_STATUS_NO_MEMORY;
715 *unix_groups = groups_tmp;
717 if (sys_getgrouplist(username, gid, *unix_groups, &n_unix_groups) == -1) {
718 DEBUG(0, ("get_user_groups: failed to get the unix group list\n"));
719 SAFE_FREE(*unix_groups);
720 return NT_STATUS_NO_SUCH_USER; /* what should this return value be? */
725 add_foreign_gids(uid, gid, unix_groups, &n_unix_groups);
727 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, n_unix_groups, *unix_groups);
729 /* now setup the space for storing the SIDS */
731 if (n_unix_groups > 0) {
733 *groups = malloc(sizeof(DOM_SID) * n_unix_groups);
735 if (!*groups) {
736 DEBUG(0, ("get_user_group: malloc() failed for DOM_SID list!\n"));
737 SAFE_FREE(*unix_groups);
738 return NT_STATUS_NO_MEMORY;
742 *n_groups = n_unix_groups;
744 for (i = 0; i < *n_groups; i++) {
745 if (!NT_STATUS_IS_OK(gid_to_sid(&(*groups)[i], (*unix_groups)[i]))) {
746 DEBUG(1, ("get_user_groups: failed to convert gid %ld to a sid!\n",
747 (long int)(*unix_groups)[i+1]));
748 SAFE_FREE(*groups);
749 SAFE_FREE(*unix_groups);
750 return NT_STATUS_NO_SUCH_USER;
754 return NT_STATUS_OK;
757 /***************************************************************************
758 Make a user_info struct
759 ***************************************************************************/
761 static NTSTATUS make_server_info(auth_serversupplied_info **server_info)
763 *server_info = malloc(sizeof(**server_info));
764 if (!*server_info) {
765 DEBUG(0,("make_server_info: malloc failed!\n"));
766 return NT_STATUS_NO_MEMORY;
768 ZERO_STRUCTP(*server_info);
770 /* Initialise the uid and gid values to something non-zero
771 which may save us from giving away root access if there
772 is a bug in allocating these fields. */
774 (*server_info)->uid = -1;
775 (*server_info)->gid = -1;
777 return NT_STATUS_OK;
780 /***************************************************************************
781 Fill a server_info struct from a SAM_ACCOUNT with their groups
782 ***************************************************************************/
784 static NTSTATUS add_user_groups(auth_serversupplied_info **server_info,
785 SAM_ACCOUNT *sampass,
786 uid_t uid, gid_t gid)
788 NTSTATUS nt_status;
789 const DOM_SID *user_sid = pdb_get_user_sid(sampass);
790 const DOM_SID *group_sid = pdb_get_group_sid(sampass);
791 int n_groupSIDs = 0;
792 DOM_SID *groupSIDs = NULL;
793 gid_t *unix_groups = NULL;
794 NT_USER_TOKEN *token;
795 BOOL is_guest;
796 uint32 rid;
798 nt_status = get_user_groups(pdb_get_username(sampass), uid, gid,
799 &n_groupSIDs, &groupSIDs, &unix_groups);
801 if (!NT_STATUS_IS_OK(nt_status)) {
802 DEBUG(4,("get_user_groups_from_local_sam failed\n"));
803 free_server_info(server_info);
804 return nt_status;
807 is_guest = (sid_peek_rid(user_sid, &rid) && rid == DOMAIN_USER_RID_GUEST);
809 if (!NT_STATUS_IS_OK(nt_status = create_nt_user_token(user_sid, group_sid,
810 n_groupSIDs, groupSIDs, is_guest,
811 &token)))
813 DEBUG(4,("create_nt_user_token failed\n"));
814 SAFE_FREE(groupSIDs);
815 SAFE_FREE(unix_groups);
816 free_server_info(server_info);
817 return nt_status;
820 SAFE_FREE(groupSIDs);
822 (*server_info)->n_groups = n_groupSIDs;
823 (*server_info)->groups = unix_groups;
824 (*server_info)->ptok = token;
826 return nt_status;
829 /***************************************************************************
830 Fill a server_info struct from a SAM_ACCOUNT with its privileges
831 ***************************************************************************/
833 static NTSTATUS add_privileges(auth_serversupplied_info **server_info)
835 PRIVILEGE_SET *privs = NULL;
837 init_privilege(&privs);
838 if (!pdb_get_privilege_set((*server_info)->ptok, privs))
839 DEBUG(1, ("Could not add privileges\n"));
841 (*server_info)->privs = privs;
843 return NT_STATUS_OK;
846 /***************************************************************************
847 Make (and fill) a user_info struct from a SAM_ACCOUNT
848 ***************************************************************************/
850 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
851 SAM_ACCOUNT *sampass)
853 NTSTATUS nt_status;
854 struct passwd *pwd;
856 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info)))
857 return nt_status;
859 (*server_info)->sam_account = sampass;
861 if ( !(pwd = getpwnam_alloc(pdb_get_username(sampass))) ) {
862 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
863 pdb_get_username(sampass)));
864 free_server_info(server_info);
865 return NT_STATUS_NO_SUCH_USER;
867 (*server_info)->unix_name = smb_xstrdup(pwd->pw_name);
868 (*server_info)->gid = pwd->pw_gid;
869 (*server_info)->uid = pwd->pw_uid;
871 passwd_free(&pwd);
873 if (!NT_STATUS_IS_OK(nt_status = add_user_groups(server_info, sampass,
874 (*server_info)->uid,
875 (*server_info)->gid))) {
876 free_server_info(server_info);
877 return nt_status;
880 if (!NT_STATUS_IS_OK(nt_status = add_privileges(server_info))) {
881 free_server_info(server_info);
882 return nt_status;
885 (*server_info)->sam_fill_level = SAM_FILL_ALL;
886 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
887 pdb_get_username(sampass),
888 (*server_info)->unix_name));
890 return nt_status;
893 /***************************************************************************
894 Make (and fill) a user_info struct from a 'struct passwd' by conversion
895 to a SAM_ACCOUNT
896 ***************************************************************************/
898 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
900 NTSTATUS nt_status;
901 SAM_ACCOUNT *sampass = NULL;
902 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(&sampass, pwd))) {
903 return nt_status;
905 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info))) {
906 return nt_status;
909 (*server_info)->sam_account = sampass;
911 if (!NT_STATUS_IS_OK(nt_status = add_user_groups(server_info, sampass, pwd->pw_uid, pwd->pw_gid))) {
912 return nt_status;
915 (*server_info)->unix_name = smb_xstrdup(pwd->pw_name);
917 (*server_info)->sam_fill_level = SAM_FILL_ALL;
918 (*server_info)->uid = pwd->pw_uid;
919 (*server_info)->gid = pwd->pw_gid;
920 return nt_status;
923 /***************************************************************************
924 Make (and fill) a user_info struct for a guest login.
925 ***************************************************************************/
927 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
929 NTSTATUS nt_status;
930 SAM_ACCOUNT *sampass = NULL;
931 DOM_SID guest_sid;
933 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
934 return nt_status;
937 sid_copy(&guest_sid, get_global_sam_sid());
938 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
940 become_root();
941 if (!pdb_getsampwsid(sampass, &guest_sid)) {
942 unbecome_root();
943 return NT_STATUS_NO_SUCH_USER;
945 unbecome_root();
947 nt_status = make_server_info_sam(server_info, sampass);
949 if (NT_STATUS_IS_OK(nt_status)) {
950 static const char zeros[16];
951 (*server_info)->guest = True;
953 /* annoying, but the Guest really does have a session key,
954 and it is all zeros! */
955 (*server_info)->nt_session_key = data_blob(zeros, sizeof(zeros));
956 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
959 return nt_status;
962 /***************************************************************************
963 Purely internal function for make_server_info_info3
964 Fill the sam account from getpwnam
965 ***************************************************************************/
966 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx,
967 const char *domain,
968 const char *username,
969 char **found_username,
970 uid_t *uid, gid_t *gid,
971 SAM_ACCOUNT **sam_account)
973 fstring dom_user;
974 struct passwd *passwd;
976 fstr_sprintf(dom_user, "%s%s%s", domain, lp_winbind_separator(),
977 username);
979 passwd = Get_Pwnam(dom_user);
981 if ( passwd ) {
982 char *p;
984 /* make sure we get the case of the username correct */
985 /* work around 'winbind use default domain = yes' */
987 p = strchr( passwd->pw_name, *lp_winbind_separator() );
988 if ( !p )
989 fstr_sprintf(dom_user, "%s%s%s", domain,
990 lp_winbind_separator(), passwd->pw_name);
991 else
992 fstrcpy( dom_user, passwd->pw_name );
994 else {
995 /* if the lookup for DOMAIN\username failed, try again
996 with just 'username'. This is need for accessing the server
997 as a trust user that actually maps to a local account */
999 fstrcpy( dom_user, username );
1000 passwd = Get_Pwnam( dom_user );
1002 /* make sure we get the case of the username correct */
1003 if ( passwd )
1004 fstrcpy( dom_user, passwd->pw_name );
1007 if ( !passwd )
1008 return NT_STATUS_NO_SUCH_USER;
1010 *uid = passwd->pw_uid;
1011 *gid = passwd->pw_gid;
1013 /* This is pointless -- there is no suport for differeing
1014 unix and windows names. Make sure to always store the
1015 one we actually looked up and succeeded. Have I mentioned
1016 why I hate the 'winbind use default domain' parameter?
1017 --jerry */
1019 *found_username = talloc_strdup(mem_ctx, dom_user);
1021 DEBUG(5,("fill_sam_account: located username was [%s]\n",
1022 *found_username));
1024 return pdb_init_sam_pw(sam_account, passwd);
1027 /****************************************************************************
1028 Wrapper to allow the getpwnam() call to strip the domain name and
1029 try again in case a local UNIX user is already there. Also run through
1030 the username if we fallback to the username only.
1031 ****************************************************************************/
1033 struct passwd *smb_getpwnam( char *domuser )
1035 struct passwd *pw = NULL;
1036 char *p;
1037 fstring mapped_username;
1039 pw = Get_Pwnam( domuser );
1040 if ( pw )
1041 return pw;
1043 /* fallback to looking up just the username */
1045 p = strchr( domuser, *lp_winbind_separator() );
1047 if ( p ) {
1048 p += 1;
1049 fstrcpy( mapped_username, p );
1050 map_username( mapped_username );
1051 pw = Get_Pwnam(mapped_username);
1052 if (!pw) {
1053 /* Don't add a machine account. */
1054 if (mapped_username[strlen(mapped_username)-1] == '$')
1055 return NULL;
1057 /* Create local user if requested. */
1058 p = strchr( mapped_username, *lp_winbind_separator() );
1059 if (p)
1060 p += 1;
1061 else
1062 p = mapped_username;
1063 auth_add_user_script(NULL, p);
1064 return Get_Pwnam(p);
1068 return pw;
1071 /***************************************************************************
1072 Make a server_info struct from the info3 returned by a domain logon
1073 ***************************************************************************/
1075 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1076 const char *internal_username,
1077 const char *sent_nt_username,
1078 const char *domain,
1079 auth_serversupplied_info **server_info,
1080 NET_USER_INFO_3 *info3)
1082 static const char zeros[16];
1084 NTSTATUS nt_status = NT_STATUS_OK;
1085 char *found_username;
1086 const char *nt_domain;
1087 const char *nt_username;
1089 SAM_ACCOUNT *sam_account = NULL;
1090 DOM_SID user_sid;
1091 DOM_SID group_sid;
1093 uid_t uid;
1094 gid_t gid;
1096 int n_lgroupSIDs;
1097 DOM_SID *lgroupSIDs = NULL;
1099 gid_t *unix_groups = NULL;
1100 NT_USER_TOKEN *token;
1102 DOM_SID *all_group_SIDs;
1103 size_t i;
1106 Here is where we should check the list of
1107 trusted domains, and verify that the SID
1108 matches.
1111 sid_copy(&user_sid, &info3->dom_sid.sid);
1112 if (!sid_append_rid(&user_sid, info3->user_rid)) {
1113 return NT_STATUS_INVALID_PARAMETER;
1116 sid_copy(&group_sid, &info3->dom_sid.sid);
1117 if (!sid_append_rid(&group_sid, info3->group_rid)) {
1118 return NT_STATUS_INVALID_PARAMETER;
1121 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
1122 /* If the server didn't give us one, just use the one we sent them */
1123 nt_username = sent_nt_username;
1126 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
1127 /* If the server didn't give us one, just use the one we sent them */
1128 nt_domain = domain;
1131 /* try to fill the SAM account.. If getpwnam() fails, then try the
1132 add user script (2.2.x behavior) */
1134 nt_status = fill_sam_account(mem_ctx, nt_domain, internal_username,
1135 &found_username, &uid, &gid, &sam_account);
1137 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1138 DEBUG(3,("User %s does not exist, trying to add it\n",
1139 internal_username));
1140 auth_add_user_script(nt_domain, internal_username);
1141 nt_status = fill_sam_account(mem_ctx, nt_domain,
1142 internal_username, &found_username,
1143 &uid, &gid, &sam_account);
1146 if (!NT_STATUS_IS_OK(nt_status)) {
1147 DEBUG(0, ("make_server_info_info3: pdb_init_sam failed!\n"));
1148 return nt_status;
1151 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1152 pdb_free_sam(&sam_account);
1153 return NT_STATUS_NO_MEMORY;
1156 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1157 pdb_free_sam(&sam_account);
1158 return NT_STATUS_NO_MEMORY;
1161 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1162 pdb_free_sam(&sam_account);
1163 return NT_STATUS_NO_MEMORY;
1166 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1167 pdb_free_sam(&sam_account);
1168 return NT_STATUS_UNSUCCESSFUL;
1171 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1172 pdb_free_sam(&sam_account);
1173 return NT_STATUS_UNSUCCESSFUL;
1176 if (!pdb_set_fullname(sam_account, unistr2_static(&(info3->uni_full_name)),
1177 PDB_CHANGED)) {
1178 pdb_free_sam(&sam_account);
1179 return NT_STATUS_NO_MEMORY;
1182 if (!pdb_set_logon_script(sam_account, unistr2_static(&(info3->uni_logon_script)), PDB_CHANGED)) {
1183 pdb_free_sam(&sam_account);
1184 return NT_STATUS_NO_MEMORY;
1187 if (!pdb_set_profile_path(sam_account, unistr2_static(&(info3->uni_profile_path)), PDB_CHANGED)) {
1188 pdb_free_sam(&sam_account);
1189 return NT_STATUS_NO_MEMORY;
1192 if (!pdb_set_homedir(sam_account, unistr2_static(&(info3->uni_home_dir)), PDB_CHANGED)) {
1193 pdb_free_sam(&sam_account);
1194 return NT_STATUS_NO_MEMORY;
1197 if (!pdb_set_dir_drive(sam_account, unistr2_static(&(info3->uni_dir_drive)), PDB_CHANGED)) {
1198 pdb_free_sam(&sam_account);
1199 return NT_STATUS_NO_MEMORY;
1202 if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info))) {
1203 DEBUG(4, ("make_server_info failed!\n"));
1204 pdb_free_sam(&sam_account);
1205 return nt_status;
1208 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1209 valid SAM_ACCOUNT) */
1211 (*server_info)->sam_account = sam_account;
1213 (*server_info)->unix_name = smb_xstrdup(found_username);
1215 /* Fill in the unix info we found on the way */
1217 (*server_info)->sam_fill_level = SAM_FILL_ALL;
1218 (*server_info)->uid = uid;
1219 (*server_info)->gid = gid;
1221 /* Store the user group information in the server_info
1222 returned to the caller. */
1224 nt_status = get_user_groups((*server_info)->unix_name,
1225 uid, gid, &n_lgroupSIDs, &lgroupSIDs, &unix_groups);
1227 if ( !NT_STATUS_IS_OK(nt_status) ) {
1228 DEBUG(4,("get_user_groups failed\n"));
1229 return nt_status;
1232 (*server_info)->groups = unix_groups;
1233 (*server_info)->n_groups = n_lgroupSIDs;
1235 /* Create a 'combined' list of all SIDs we might want in the SD */
1237 all_group_SIDs = malloc(sizeof(DOM_SID) * (info3->num_groups2 +info3->num_other_sids));
1239 if (!all_group_SIDs) {
1240 DEBUG(0, ("malloc() failed for DOM_SID list!\n"));
1241 SAFE_FREE(lgroupSIDs);
1242 free_server_info(server_info);
1243 return NT_STATUS_NO_MEMORY;
1246 #if 0 /* JERRY -- no such thing as local groups in current code */
1247 /* Copy the 'local' sids */
1248 memcpy(all_group_SIDs, lgroupSIDs, sizeof(DOM_SID) * n_lgroupSIDs);
1249 SAFE_FREE(lgroupSIDs);
1250 #endif
1252 /* and create (by appending rids) the 'domain' sids */
1254 for (i = 0; i < info3->num_groups2; i++) {
1256 sid_copy(&all_group_SIDs[i], &(info3->dom_sid.sid));
1258 if (!sid_append_rid(&all_group_SIDs[i], info3->gids[i].g_rid)) {
1260 nt_status = NT_STATUS_INVALID_PARAMETER;
1262 DEBUG(3,("could not append additional group rid 0x%x\n",
1263 info3->gids[i].g_rid));
1265 SAFE_FREE(lgroupSIDs);
1266 free_server_info(server_info);
1268 return nt_status;
1273 /* Copy 'other' sids. We need to do sid filtering here to
1274 prevent possible elevation of privileges. See:
1276 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1279 for (i = 0; i < info3->num_other_sids; i++) {
1280 sid_copy(&all_group_SIDs[info3->num_groups2 + i],
1281 &info3->other_sids[i].sid);
1284 /* Where are the 'global' sids... */
1286 /* can the user be guest? if yes, where is it stored? */
1288 nt_status = create_nt_user_token(&user_sid, &group_sid,
1289 info3->num_groups2 + info3->num_other_sids,
1290 all_group_SIDs, False, &token);
1292 if ( !NT_STATUS_IS_OK(nt_status) ) {
1293 DEBUG(4,("create_nt_user_token failed\n"));
1294 SAFE_FREE(all_group_SIDs);
1295 free_server_info(server_info);
1296 return nt_status;
1299 (*server_info)->ptok = token;
1301 SAFE_FREE(all_group_SIDs);
1303 /* ensure we are never given NULL session keys */
1305 if (memcmp(info3->user_sess_key, zeros, sizeof(zeros)) == 0) {
1306 (*server_info)->nt_session_key = data_blob(NULL, 0);
1307 } else {
1308 (*server_info)->nt_session_key = data_blob(info3->user_sess_key, sizeof(info3->user_sess_key));
1311 if (memcmp(info3->padding, zeros, sizeof(zeros)) == 0) {
1312 (*server_info)->lm_session_key = data_blob(NULL, 0);
1313 } else {
1314 (*server_info)->lm_session_key = data_blob(info3->padding, 16);
1316 return NT_STATUS_OK;
1319 /***************************************************************************
1320 Free a user_info struct
1321 ***************************************************************************/
1323 void free_user_info(auth_usersupplied_info **user_info)
1325 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1326 if (*user_info != NULL) {
1327 if ((*user_info)->smb_name.str) {
1328 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
1330 SAFE_FREE((*user_info)->smb_name.str);
1331 SAFE_FREE((*user_info)->internal_username.str);
1332 SAFE_FREE((*user_info)->client_domain.str);
1333 SAFE_FREE((*user_info)->domain.str);
1334 SAFE_FREE((*user_info)->wksta_name.str);
1335 data_blob_free(&(*user_info)->lm_resp);
1336 data_blob_free(&(*user_info)->nt_resp);
1337 SAFE_FREE((*user_info)->interactive_password);
1338 data_blob_clear_free(&(*user_info)->plaintext_password);
1339 ZERO_STRUCT(**user_info);
1341 SAFE_FREE(*user_info);
1344 /***************************************************************************
1345 Clear out a server_info struct that has been allocated
1346 ***************************************************************************/
1348 void free_server_info(auth_serversupplied_info **server_info)
1350 DEBUG(5,("attempting to free (and zero) a server_info structure\n"));
1351 if (*server_info != NULL) {
1352 pdb_free_sam(&(*server_info)->sam_account);
1354 /* call pam_end here, unless we know we are keeping it */
1355 delete_nt_token( &(*server_info)->ptok );
1356 SAFE_FREE((*server_info)->groups);
1357 SAFE_FREE((*server_info)->unix_name);
1358 data_blob_free(&(*server_info)->lm_session_key);
1359 data_blob_free(&(*server_info)->nt_session_key);
1360 ZERO_STRUCT(**server_info);
1362 SAFE_FREE(*server_info);
1365 /***************************************************************************
1366 Make an auth_methods struct
1367 ***************************************************************************/
1369 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
1371 if (!auth_context) {
1372 smb_panic("no auth_context supplied to make_auth_methods()!\n");
1375 if (!auth_method) {
1376 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
1379 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
1380 if (!*auth_method) {
1381 DEBUG(0,("make_auth_method: malloc failed!\n"));
1382 return False;
1384 ZERO_STRUCTP(*auth_method);
1386 return True;
1389 /****************************************************************************
1390 Delete a SID token.
1391 ****************************************************************************/
1393 void delete_nt_token(NT_USER_TOKEN **pptoken)
1395 if (*pptoken) {
1396 NT_USER_TOKEN *ptoken = *pptoken;
1397 SAFE_FREE( ptoken->user_sids );
1398 ZERO_STRUCTP(ptoken);
1400 SAFE_FREE(*pptoken);
1403 /****************************************************************************
1404 Duplicate a SID token.
1405 ****************************************************************************/
1407 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
1409 NT_USER_TOKEN *token;
1411 if (!ptoken)
1412 return NULL;
1414 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
1415 return NULL;
1417 ZERO_STRUCTP(token);
1419 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
1420 SAFE_FREE(token);
1421 return NULL;
1424 token->num_sids = ptoken->num_sids;
1426 return token;
1430 * Squash an NT_STATUS in line with security requirements.
1431 * In an attempt to avoid giving the whole game away when users
1432 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
1433 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
1434 * (session setups in particular).
1436 * @param nt_status NTSTATUS input for squashing.
1437 * @return the 'squashed' nt_status
1440 NTSTATUS nt_status_squash(NTSTATUS nt_status)
1442 if NT_STATUS_IS_OK(nt_status) {
1443 return nt_status;
1444 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
1445 /* Match WinXP and don't give the game away */
1446 return NT_STATUS_LOGON_FAILURE;
1448 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
1449 /* Match WinXP and don't give the game away */
1450 return NT_STATUS_LOGON_FAILURE;
1451 } else {
1452 return nt_status;
1458 * Verify whether or not given domain is trusted.
1460 * @param domain_name name of the domain to be verified
1461 * @return true if domain is one of the trusted once or
1462 * false if otherwise
1465 BOOL is_trusted_domain(const char* dom_name)
1467 DOM_SID trustdom_sid;
1468 char *pass = NULL;
1469 time_t lct;
1470 BOOL ret;
1472 /* no trusted domains for a standalone server */
1474 if ( lp_server_role() == ROLE_STANDALONE )
1475 return False;
1477 /* if we are a DC, then check for a direct trust relationships */
1479 if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
1480 become_root();
1481 ret = secrets_fetch_trusted_domain_password(dom_name, &pass, &trustdom_sid, &lct);
1482 unbecome_root();
1483 SAFE_FREE(pass);
1484 if (ret)
1485 return True;
1487 else {
1488 /* if winbindd is not up and we are a domain member) then we need to update the
1489 trustdom_cache ourselves */
1491 if ( !winbind_ping() )
1492 update_trustdom_cache();
1495 /* now the trustdom cache should be available a DC could still
1496 * have a transitive trust so fall back to the cache of trusted
1497 * domains (like a domain member would use */
1499 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {
1500 return True;
1503 return False;