r14129: Add the group sids from the Kerberos PAC to the user token.
[Samba/nascimento.git] / source / auth / auth_util.c
blob99ce6620c318f6c0ea87d2735b32fcc3fced0265
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
8 Copyright (C) Volker Lendecke 2006
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_AUTH
30 static struct nt_user_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
31 const DOM_SID *user_sid,
32 const DOM_SID *group_sid,
33 BOOL is_guest,
34 int num_groupsids,
35 const DOM_SID *groupsids);
37 /****************************************************************************
38 Create a UNIX user on demand.
39 ****************************************************************************/
41 static int smb_create_user(const char *domain, const char *unix_username, const char *homedir)
43 pstring add_script;
44 int ret;
46 pstrcpy(add_script, lp_adduser_script());
47 if (! *add_script)
48 return -1;
49 all_string_sub(add_script, "%u", unix_username, sizeof(pstring));
50 if (domain)
51 all_string_sub(add_script, "%D", domain, sizeof(pstring));
52 if (homedir)
53 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
54 ret = smbrun(add_script,NULL);
55 flush_pwnam_cache();
56 DEBUG(ret ? 0 : 3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
57 return ret;
60 /****************************************************************************
61 Create an auth_usersupplied_data structure
62 ****************************************************************************/
64 static NTSTATUS make_user_info(auth_usersupplied_info **user_info,
65 const char *smb_name,
66 const char *internal_username,
67 const char *client_domain,
68 const char *domain,
69 const char *wksta_name,
70 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
71 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
72 DATA_BLOB *plaintext,
73 BOOL encrypted)
76 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
78 *user_info = SMB_MALLOC_P(auth_usersupplied_info);
79 if (*user_info == NULL) {
80 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
81 return NT_STATUS_NO_MEMORY;
84 ZERO_STRUCTP(*user_info);
86 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
88 (*user_info)->smb_name = SMB_STRDUP(smb_name);
89 if ((*user_info)->smb_name == NULL) {
90 free_user_info(user_info);
91 return NT_STATUS_NO_MEMORY;
94 (*user_info)->internal_username = SMB_STRDUP(internal_username);
95 if ((*user_info)->internal_username == NULL) {
96 free_user_info(user_info);
97 return NT_STATUS_NO_MEMORY;
100 (*user_info)->domain = SMB_STRDUP(domain);
101 if ((*user_info)->domain == NULL) {
102 free_user_info(user_info);
103 return NT_STATUS_NO_MEMORY;
106 (*user_info)->client_domain = SMB_STRDUP(client_domain);
107 if ((*user_info)->client_domain == NULL) {
108 free_user_info(user_info);
109 return NT_STATUS_NO_MEMORY;
112 (*user_info)->wksta_name = SMB_STRDUP(wksta_name);
113 if ((*user_info)->wksta_name == NULL) {
114 free_user_info(user_info);
115 return NT_STATUS_NO_MEMORY;
118 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
120 if (lm_pwd)
121 (*user_info)->lm_resp = data_blob(lm_pwd->data, lm_pwd->length);
122 if (nt_pwd)
123 (*user_info)->nt_resp = data_blob(nt_pwd->data, nt_pwd->length);
124 if (lm_interactive_pwd)
125 (*user_info)->lm_interactive_pwd = data_blob(lm_interactive_pwd->data, lm_interactive_pwd->length);
126 if (nt_interactive_pwd)
127 (*user_info)->nt_interactive_pwd = data_blob(nt_interactive_pwd->data, nt_interactive_pwd->length);
129 if (plaintext)
130 (*user_info)->plaintext_password = data_blob(plaintext->data, plaintext->length);
132 (*user_info)->encrypted = encrypted;
134 (*user_info)->logon_parameters = 0;
136 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
138 return NT_STATUS_OK;
141 /****************************************************************************
142 Create an auth_usersupplied_data structure after appropriate mapping.
143 ****************************************************************************/
145 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info,
146 const char *smb_name,
147 const char *client_domain,
148 const char *wksta_name,
149 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
150 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
151 DATA_BLOB *plaintext,
152 BOOL encrypted)
154 const char *domain;
155 fstring internal_username;
156 fstrcpy(internal_username, smb_name);
157 map_username(internal_username);
159 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
160 client_domain, smb_name, wksta_name));
162 /* don't allow "" as a domain, fixes a Win9X bug
163 where it doens't supply a domain for logon script
164 'net use' commands. */
166 if ( *client_domain )
167 domain = client_domain;
168 else
169 domain = lp_workgroup();
171 /* do what win2k does. Always map unknown domains to our own
172 and let the "passdb backend" handle unknown users. */
174 if ( !is_trusted_domain(domain) && !strequal(domain, get_global_sam_name()) )
175 domain = my_sam_name();
177 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
179 return make_user_info(user_info, smb_name, internal_username,
180 client_domain, domain, wksta_name,
181 lm_pwd, nt_pwd,
182 lm_interactive_pwd, nt_interactive_pwd,
183 plaintext, encrypted);
186 /****************************************************************************
187 Create an auth_usersupplied_data, making the DATA_BLOBs here.
188 Decrypt and encrypt the passwords.
189 ****************************************************************************/
191 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
192 const char *smb_name,
193 const char *client_domain,
194 const char *wksta_name,
195 uint32 logon_parameters,
196 const uchar *lm_network_pwd,
197 int lm_pwd_len,
198 const uchar *nt_network_pwd,
199 int nt_pwd_len)
201 BOOL ret;
202 NTSTATUS status;
203 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
204 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
206 status = make_user_info_map(user_info,
207 smb_name, client_domain,
208 wksta_name,
209 lm_pwd_len ? &lm_blob : NULL,
210 nt_pwd_len ? &nt_blob : NULL,
211 NULL, NULL, NULL,
212 True);
214 if (NT_STATUS_IS_OK(status)) {
215 (*user_info)->logon_parameters = logon_parameters;
217 ret = NT_STATUS_IS_OK(status) ? True : False;
219 data_blob_free(&lm_blob);
220 data_blob_free(&nt_blob);
221 return ret;
224 /****************************************************************************
225 Create an auth_usersupplied_data, making the DATA_BLOBs here.
226 Decrypt and encrypt the passwords.
227 ****************************************************************************/
229 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
230 const char *smb_name,
231 const char *client_domain,
232 const char *wksta_name,
233 uint32 logon_parameters,
234 const uchar chal[8],
235 const uchar lm_interactive_pwd[16],
236 const uchar nt_interactive_pwd[16],
237 const uchar *dc_sess_key)
239 char lm_pwd[16];
240 char nt_pwd[16];
241 unsigned char local_lm_response[24];
242 unsigned char local_nt_response[24];
243 unsigned char key[16];
245 ZERO_STRUCT(key);
246 memcpy(key, dc_sess_key, 8);
248 if (lm_interactive_pwd)
249 memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
251 if (nt_interactive_pwd)
252 memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
254 #ifdef DEBUG_PASSWORD
255 DEBUG(100,("key:"));
256 dump_data(100, (char *)key, sizeof(key));
258 DEBUG(100,("lm owf password:"));
259 dump_data(100, lm_pwd, sizeof(lm_pwd));
261 DEBUG(100,("nt owf password:"));
262 dump_data(100, nt_pwd, sizeof(nt_pwd));
263 #endif
265 if (lm_interactive_pwd)
266 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
268 if (nt_interactive_pwd)
269 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
271 #ifdef DEBUG_PASSWORD
272 DEBUG(100,("decrypt of lm owf password:"));
273 dump_data(100, lm_pwd, sizeof(lm_pwd));
275 DEBUG(100,("decrypt of nt owf password:"));
276 dump_data(100, nt_pwd, sizeof(nt_pwd));
277 #endif
279 if (lm_interactive_pwd)
280 SMBOWFencrypt((const unsigned char *)lm_pwd, chal,
281 local_lm_response);
283 if (nt_interactive_pwd)
284 SMBOWFencrypt((const unsigned char *)nt_pwd, chal,
285 local_nt_response);
287 /* Password info paranoia */
288 ZERO_STRUCT(key);
291 BOOL ret;
292 NTSTATUS nt_status;
293 DATA_BLOB local_lm_blob;
294 DATA_BLOB local_nt_blob;
296 DATA_BLOB lm_interactive_blob;
297 DATA_BLOB nt_interactive_blob;
299 if (lm_interactive_pwd) {
300 local_lm_blob = data_blob(local_lm_response,
301 sizeof(local_lm_response));
302 lm_interactive_blob = data_blob(lm_pwd,
303 sizeof(lm_pwd));
304 ZERO_STRUCT(lm_pwd);
307 if (nt_interactive_pwd) {
308 local_nt_blob = data_blob(local_nt_response,
309 sizeof(local_nt_response));
310 nt_interactive_blob = data_blob(nt_pwd,
311 sizeof(nt_pwd));
312 ZERO_STRUCT(nt_pwd);
315 nt_status = make_user_info_map(
316 user_info,
317 smb_name, client_domain, wksta_name,
318 lm_interactive_pwd ? &local_lm_blob : NULL,
319 nt_interactive_pwd ? &local_nt_blob : NULL,
320 lm_interactive_pwd ? &lm_interactive_blob : NULL,
321 nt_interactive_pwd ? &nt_interactive_blob : NULL,
322 NULL, True);
324 if (NT_STATUS_IS_OK(nt_status)) {
325 (*user_info)->logon_parameters = logon_parameters;
328 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
329 data_blob_free(&local_lm_blob);
330 data_blob_free(&local_nt_blob);
331 data_blob_free(&lm_interactive_blob);
332 data_blob_free(&nt_interactive_blob);
333 return ret;
338 /****************************************************************************
339 Create an auth_usersupplied_data structure
340 ****************************************************************************/
342 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
343 const char *smb_name,
344 const char *client_domain,
345 const uint8 chal[8],
346 DATA_BLOB plaintext_password)
349 DATA_BLOB local_lm_blob;
350 DATA_BLOB local_nt_blob;
351 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
354 * Not encrypted - do so.
357 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
358 "format.\n"));
360 if (plaintext_password.data) {
361 unsigned char local_lm_response[24];
363 #ifdef DEBUG_PASSWORD
364 DEBUG(10,("Unencrypted password (len %d):\n",
365 (int)plaintext_password.length));
366 dump_data(100, (const char *)plaintext_password.data,
367 plaintext_password.length);
368 #endif
370 SMBencrypt( (const char *)plaintext_password.data,
371 (const uchar*)chal, local_lm_response);
372 local_lm_blob = data_blob(local_lm_response, 24);
374 /* We can't do an NT hash here, as the password needs to be
375 case insensitive */
376 local_nt_blob = data_blob(NULL, 0);
378 } else {
379 local_lm_blob = data_blob(NULL, 0);
380 local_nt_blob = data_blob(NULL, 0);
383 ret = make_user_info_map(
384 user_info, smb_name, client_domain,
385 get_remote_machine_name(),
386 local_lm_blob.data ? &local_lm_blob : NULL,
387 local_nt_blob.data ? &local_nt_blob : NULL,
388 NULL, NULL,
389 plaintext_password.data ? &plaintext_password : NULL,
390 False);
392 data_blob_free(&local_lm_blob);
393 return NT_STATUS_IS_OK(ret) ? True : False;
396 /****************************************************************************
397 Create an auth_usersupplied_data structure
398 ****************************************************************************/
400 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
401 const char *smb_name,
402 const char *client_domain,
403 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
405 return make_user_info_map(user_info, smb_name,
406 client_domain,
407 get_remote_machine_name(),
408 lm_resp.data ? &lm_resp : NULL,
409 nt_resp.data ? &nt_resp : NULL,
410 NULL, NULL, NULL,
411 True);
414 /****************************************************************************
415 Create a guest user_info blob, for anonymous authenticaion.
416 ****************************************************************************/
418 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
420 NTSTATUS nt_status;
422 nt_status = make_user_info(user_info,
423 "","",
424 "","",
425 "",
426 NULL, NULL,
427 NULL, NULL,
428 NULL,
429 True);
431 return NT_STATUS_IS_OK(nt_status) ? True : False;
434 /****************************************************************************
435 prints a NT_USER_TOKEN to debug output.
436 ****************************************************************************/
438 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
440 size_t i;
442 if (!token) {
443 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
444 return;
447 DEBUGC(dbg_class, dbg_lev,
448 ("NT user token of user %s\n",
449 sid_string_static(&token->user_sids[0]) ));
450 DEBUGADDC(dbg_class, dbg_lev,
451 ("contains %lu SIDs\n", (unsigned long)token->num_sids));
452 for (i = 0; i < token->num_sids; i++)
453 DEBUGADDC(dbg_class, dbg_lev,
454 ("SID[%3lu]: %s\n", (unsigned long)i,
455 sid_string_static(&token->user_sids[i])));
457 dump_se_priv( dbg_class, dbg_lev, &token->privileges );
460 /****************************************************************************
461 prints a UNIX 'token' to debug output.
462 ****************************************************************************/
464 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
465 int n_groups, gid_t *groups)
467 int i;
468 DEBUGC(dbg_class, dbg_lev,
469 ("UNIX token of user %ld\n", (long int)uid));
471 DEBUGADDC(dbg_class, dbg_lev,
472 ("Primary group is %ld and contains %i supplementary "
473 "groups\n", (long int)gid, n_groups));
474 for (i = 0; i < n_groups; i++)
475 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
476 (long int)groups[i]));
479 /******************************************************************************
480 Create a token for the root user to be used internally by smbd.
481 This is similar to running under the context of the LOCAL_SYSTEM account
482 in Windows. This is a read-only token. Do not modify it or free() it.
483 Create a copy if your need to change it.
484 ******************************************************************************/
486 NT_USER_TOKEN *get_root_nt_token( void )
488 static NT_USER_TOKEN *token = NULL;
489 DOM_SID u_sid, g_sid;
490 struct passwd *pw;
492 if ( token )
493 return token;
495 if ( !(pw = sys_getpwnam( "root" )) ) {
496 DEBUG(0,("get_root_nt_token: getpwnam\"root\") failed!\n"));
497 return NULL;
500 /* get the user and primary group SIDs; although the
501 BUILTIN\Administrators SId is really the one that matters here */
503 uid_to_sid(&u_sid, pw->pw_uid);
504 gid_to_sid(&g_sid, pw->pw_gid);
506 token = create_local_nt_token(NULL, &u_sid, &g_sid, False,
507 1, &global_sid_Builtin_Administrators);
508 return token;
511 static int server_info_dtor(void *p)
513 auth_serversupplied_info *server_info =
514 talloc_get_type_abort(p, auth_serversupplied_info);
516 if (server_info->sam_account != NULL) {
517 TALLOC_FREE(server_info->sam_account);
520 ZERO_STRUCTP(server_info);
521 return 0;
524 /***************************************************************************
525 Make a server_info struct. Free with TALLOC_FREE().
526 ***************************************************************************/
528 static auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
530 struct auth_serversupplied_info *result;
532 result = TALLOC_ZERO_P(mem_ctx, auth_serversupplied_info);
533 if (result == NULL) {
534 DEBUG(0, ("talloc failed\n"));
535 return NULL;
538 talloc_set_destructor(result, server_info_dtor);
540 /* Initialise the uid and gid values to something non-zero
541 which may save us from giving away root access if there
542 is a bug in allocating these fields. */
544 result->uid = -1;
545 result->gid = -1;
546 return result;
549 /***************************************************************************
550 Make (and fill) a user_info struct from a struct samu
551 ***************************************************************************/
553 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
554 struct samu *sampass)
556 NTSTATUS status;
557 struct passwd *pwd;
558 gid_t *gids;
559 auth_serversupplied_info *result;
561 if ( !(pwd = getpwnam_alloc(NULL, pdb_get_username(sampass))) ) {
562 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
563 pdb_get_username(sampass)));
564 return NT_STATUS_NO_SUCH_USER;
567 if ( !(result = make_server_info(NULL)) ) {
568 TALLOC_FREE(pwd);
569 return NT_STATUS_NO_MEMORY;
572 result->sam_account = sampass;
573 result->unix_name = talloc_strdup(result, pwd->pw_name);
574 result->gid = pwd->pw_gid;
575 result->uid = pwd->pw_uid;
577 TALLOC_FREE(pwd);
579 status = pdb_enum_group_memberships(result, sampass,
580 &result->sids, &gids,
581 &result->num_sids);
583 if (!NT_STATUS_IS_OK(status)) {
584 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
585 nt_errstr(status)));
586 result->sam_account = NULL; /* Don't free on error exit. */
587 TALLOC_FREE(result);
588 return status;
591 /* For now we throw away the gids and convert via sid_to_gid
592 * later. This needs fixing, but I'd like to get the code straight and
593 * simple first. */
594 TALLOC_FREE(gids);
596 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
597 pdb_get_username(sampass), result->unix_name));
599 *server_info = result;
601 return NT_STATUS_OK;
605 * Add alias SIDs from memberships within the partially created token SID list
608 static NTSTATUS add_aliases(TALLOC_CTX *tmp_ctx, const DOM_SID *domain_sid,
609 struct nt_user_token *token)
611 uint32 *aliases;
612 size_t i, num_aliases;
613 NTSTATUS status;
615 aliases = NULL;
616 num_aliases = 0;
618 status = pdb_enum_alias_memberships(tmp_ctx, domain_sid,
619 token->user_sids,
620 token->num_sids,
621 &aliases, &num_aliases);
623 if (!NT_STATUS_IS_OK(status)) {
624 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
625 nt_errstr(status)));
626 return status;
629 for (i=0; i<num_aliases; i++) {
630 DOM_SID alias_sid;
631 sid_compose(&alias_sid, domain_sid, aliases[i]);
632 add_sid_to_array_unique(token, &alias_sid,
633 &token->user_sids,
634 &token->num_sids);
635 if (token->user_sids == NULL) {
636 DEBUG(0, ("add_sid_to_array failed\n"));
637 return NT_STATUS_NO_MEMORY;
641 return NT_STATUS_OK;
644 static NTSTATUS log_nt_token(TALLOC_CTX *tmp_ctx, NT_USER_TOKEN *token)
646 char *command;
647 char *group_sidstr;
648 size_t i;
650 if ((lp_log_nt_token_command() == NULL) ||
651 (strlen(lp_log_nt_token_command()) == 0)) {
652 return NT_STATUS_OK;
655 group_sidstr = talloc_strdup(tmp_ctx, "");
656 for (i=1; i<token->num_sids; i++) {
657 group_sidstr = talloc_asprintf(
658 tmp_ctx, "%s %s", group_sidstr,
659 sid_string_static(&token->user_sids[i]));
662 command = talloc_string_sub(
663 tmp_ctx, lp_log_nt_token_command(),
664 "%s", sid_string_static(&token->user_sids[0]));
665 command = talloc_string_sub(tmp_ctx, command, "%t", group_sidstr);
667 if (command == NULL) {
668 return NT_STATUS_NO_MEMORY;
671 DEBUG(8, ("running command: [%s]\n", command));
672 if (smbrun(command, NULL) != 0) {
673 DEBUG(0, ("Could not log NT token\n"));
674 return NT_STATUS_ACCESS_DENIED;
677 return NT_STATUS_OK;
681 * Create a NT token for the user, expanding local aliases
684 static struct nt_user_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
685 const DOM_SID *user_sid,
686 const DOM_SID *group_sid,
687 BOOL is_guest,
688 int num_groupsids,
689 const DOM_SID *groupsids)
691 TALLOC_CTX *tmp_ctx;
692 struct nt_user_token *result = NULL;
693 int i;
694 NTSTATUS status;
696 tmp_ctx = talloc_new(mem_ctx);
697 if (tmp_ctx == NULL) {
698 DEBUG(0, ("talloc_new failed\n"));
699 return NULL;
702 result = TALLOC_ZERO_P(tmp_ctx, NT_USER_TOKEN);
703 if (result == NULL) {
704 DEBUG(0, ("talloc failed\n"));
705 goto done;
708 /* First create the default SIDs */
710 add_sid_to_array(result, user_sid,
711 &result->user_sids, &result->num_sids);
712 add_sid_to_array(result, group_sid,
713 &result->user_sids, &result->num_sids);
714 add_sid_to_array(result, &global_sid_World,
715 &result->user_sids, &result->num_sids);
716 add_sid_to_array(result, &global_sid_Network,
717 &result->user_sids, &result->num_sids);
719 if (is_guest) {
720 add_sid_to_array(result, &global_sid_Builtin_Guests,
721 &result->user_sids, &result->num_sids);
722 } else {
723 add_sid_to_array(result, &global_sid_Authenticated_Users,
724 &result->user_sids, &result->num_sids);
727 /* Now the SIDs we got from authentication. These are the ones from
728 * the info3 struct or from the pdb_enum_group_memberships, depending
729 * on who authenticated the user. */
731 for (i=0; i<num_groupsids; i++) {
732 add_sid_to_array_unique(result, &groupsids[i],
733 &result->user_sids, &result->num_sids);
736 if (lp_winbind_nested_groups()) {
738 /* Now add the aliases. First the one from our local SAM */
740 status = add_aliases(tmp_ctx, get_global_sam_sid(), result);
742 if (!NT_STATUS_IS_OK(status)) {
743 result = NULL;
744 goto done;
747 /* Finally the builtin ones */
749 status = add_aliases(tmp_ctx, &global_sid_Builtin, result);
751 if (!NT_STATUS_IS_OK(status)) {
752 result = NULL;
753 goto done;
755 } else {
757 /* Play jerry's trick to auto-add local admins if we're a
758 * domain admin. */
760 DOM_SID dom_admins;
761 BOOL domain_mode = False;
763 if (IS_DC) {
764 sid_compose(&dom_admins, get_global_sam_sid(),
765 DOMAIN_GROUP_RID_ADMINS);
766 domain_mode = True;
768 if ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
769 (secrets_fetch_domain_sid(lp_workgroup(), &dom_admins))) {
770 sid_append_rid(&dom_admins, DOMAIN_GROUP_RID_ADMINS);
771 domain_mode = True;
774 if (domain_mode) {
775 for (i=0; i<result->num_sids; i++) {
776 if (sid_equal(&dom_admins,
777 &result->user_sids[i])) {
778 add_sid_to_array_unique(
779 result,
780 &global_sid_Builtin_Administrators,
781 &result->user_sids,
782 &result->num_sids);
783 break;
790 get_privileges_for_sids(&result->privileges, result->user_sids,
791 result->num_sids);
793 talloc_steal(mem_ctx, result);
795 done:
796 TALLOC_FREE(tmp_ctx);
797 return result;
801 * Create the token to use from server_info->sam_account and
802 * server_info->sids (the info3/sam groups). Find the unix gids.
805 NTSTATUS create_local_token(auth_serversupplied_info *server_info)
807 TALLOC_CTX *mem_ctx;
808 NTSTATUS status;
809 size_t i;
812 mem_ctx = talloc_new(NULL);
813 if (mem_ctx == NULL) {
814 DEBUG(0, ("talloc_new failed\n"));
815 return NT_STATUS_NO_MEMORY;
818 server_info->ptok = create_local_nt_token(
819 server_info,
820 pdb_get_user_sid(server_info->sam_account),
821 pdb_get_group_sid(server_info->sam_account),
822 server_info->guest,
823 server_info->num_sids, server_info->sids);
825 if ( !server_info->ptok ) {
826 return NT_STATUS_NO_SUCH_USER;
829 /* Convert the SIDs to gids. */
831 server_info->n_groups = 0;
832 server_info->groups = NULL;
834 /* Start at index 1, where the groups start. */
836 for (i=1; i<server_info->ptok->num_sids; i++) {
837 gid_t gid;
838 DOM_SID *sid = &server_info->ptok->user_sids[i];
840 if (!sid_to_gid(sid, &gid)) {
841 DEBUG(10, ("Could not convert SID %s to gid, "
842 "ignoring it\n", sid_string_static(sid)));
843 continue;
845 add_gid_to_array_unique(server_info, gid, &server_info->groups,
846 &server_info->n_groups);
849 debug_nt_user_token(DBGC_AUTH, 10, server_info->ptok);
851 status = log_nt_token(mem_ctx, server_info->ptok);
853 TALLOC_FREE(mem_ctx);
854 return status;
858 * Create an artificial NT token given just a username. (Initially indended
859 * for force user)
861 * We go through lookup_name() to avoid problems we had with 'winbind use
862 * default domain'.
864 * We have 3 cases:
866 * unmapped unix users: Go directly to nss to find the user's group.
868 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
870 * If the user is provided by winbind, the primary gid is set to "domain
871 * users" of the user's domain. For an explanation why this is necessary, see
872 * the thread starting at
873 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
876 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
877 BOOL is_guest,
878 uid_t *uid, gid_t *gid,
879 char **found_username,
880 struct nt_user_token **token)
882 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
883 TALLOC_CTX *tmp_ctx;
884 DOM_SID user_sid;
885 enum SID_NAME_USE type;
886 gid_t *gids;
887 DOM_SID primary_group_sid;
888 DOM_SID *group_sids;
889 size_t num_group_sids;
891 tmp_ctx = talloc_new(NULL);
892 if (tmp_ctx == NULL) {
893 DEBUG(0, ("talloc_new failed\n"));
894 return NT_STATUS_NO_MEMORY;
897 if (!lookup_name(tmp_ctx, username, LOOKUP_NAME_ALL,
898 NULL, NULL, &user_sid, &type)) {
899 DEBUG(1, ("lookup_name for %s failed\n", username));
900 goto done;
903 if (type != SID_NAME_USER) {
904 DEBUG(1, ("%s is a %s, not a user\n", username,
905 sid_type_lookup(type)));
906 goto done;
909 if (!sid_to_uid(&user_sid, uid)) {
910 DEBUG(1, ("sid_to_uid for %s (%s) failed\n",
911 username, sid_string_static(&user_sid)));
912 goto done;
915 if (sid_check_is_in_unix_users(&user_sid)) {
917 /* This is a unix user not in passdb. We need to ask nss
918 * directly, without consulting passdb */
920 struct passwd *pass;
921 size_t i;
923 pass = getpwuid_alloc(tmp_ctx, *uid);
924 if (pass == NULL) {
925 DEBUG(1, ("getpwuid(%d) for user %s failed\n",
926 *uid, username));
927 goto done;
930 *gid = pass->pw_gid;
931 gid_to_sid(&primary_group_sid, pass->pw_gid);
933 if (!getgroups_unix_user(tmp_ctx, username, pass->pw_gid,
934 &gids, &num_group_sids)) {
935 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
936 username));
937 goto done;
940 group_sids = talloc_array(tmp_ctx, DOM_SID, num_group_sids);
941 if (group_sids == NULL) {
942 DEBUG(1, ("talloc_array failed\n"));
943 result = NT_STATUS_NO_MEMORY;
944 goto done;
947 for (i=0; i<num_group_sids; i++) {
948 gid_to_sid(&group_sids[i], gids[i]);
950 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
952 } else if (sid_check_is_in_our_domain(&user_sid)) {
954 /* This is a passdb user, so ask passdb */
956 struct samu *sam_acct = NULL;
958 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
959 goto done;
962 if (!pdb_getsampwsid(sam_acct, &user_sid)) {
963 DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n",
964 sid_string_static(&user_sid), username));
965 result = NT_STATUS_NO_SUCH_USER;
966 goto done;
969 sid_copy(&primary_group_sid, pdb_get_group_sid(sam_acct));
971 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
972 &group_sids, &gids,
973 &num_group_sids);
974 if (!NT_STATUS_IS_OK(result)) {
975 DEBUG(10, ("enum_group_memberships failed for %s\n",
976 username));
977 goto done;
980 *found_username = talloc_strdup(mem_ctx,
981 pdb_get_username(sam_acct));
983 } else {
985 /* This user is from winbind, force the primary gid to the
986 * user's "domain users" group. Under certain circumstances
987 * (user comes from NT4), this might be a loss of
988 * information. But we can not rely on winbind getting the
989 * correct info. AD might prohibit winbind looking up that
990 * information. */
992 uint32 dummy;
994 sid_copy(&primary_group_sid, &user_sid);
995 sid_split_rid(&primary_group_sid, &dummy);
996 sid_append_rid(&primary_group_sid, DOMAIN_GROUP_RID_USERS);
998 if (!sid_to_gid(&primary_group_sid, gid)) {
999 DEBUG(1, ("sid_to_gid(%s) failed\n",
1000 sid_string_static(&primary_group_sid)));
1001 goto done;
1004 num_group_sids = 0;
1005 group_sids = NULL;
1007 *found_username = talloc_strdup(mem_ctx, username);
1010 *token = create_local_nt_token(mem_ctx, &user_sid, &primary_group_sid,
1011 is_guest, num_group_sids, group_sids);
1013 if ((*token == NULL) || (*found_username == NULL)) {
1014 result = NT_STATUS_NO_MEMORY;
1015 goto done;
1018 result = NT_STATUS_OK;
1019 done:
1020 TALLOC_FREE(tmp_ctx);
1021 return result;
1024 /***************************************************************************
1025 Build upon create_token_from_username:
1027 Expensive helper function to figure out whether a user given its name is
1028 member of a particular group.
1029 ***************************************************************************/
1030 BOOL user_in_group_sid(const char *username, const DOM_SID *group_sid)
1032 NTSTATUS status;
1033 uid_t uid;
1034 gid_t gid;
1035 char *found_username;
1036 struct nt_user_token *token;
1037 BOOL result;
1039 TALLOC_CTX *mem_ctx;
1041 mem_ctx = talloc_new(NULL);
1042 if (mem_ctx == NULL) {
1043 DEBUG(0, ("talloc_new failed\n"));
1044 return False;
1047 status = create_token_from_username(mem_ctx, username, False,
1048 &uid, &gid, &found_username,
1049 &token);
1051 if (!NT_STATUS_IS_OK(status)) {
1052 DEBUG(10, ("could not create token for %s\n", username));
1053 return False;
1056 result = nt_token_check_sid(group_sid, token);
1058 TALLOC_FREE(mem_ctx);
1059 return result;
1063 BOOL user_in_group(const char *username, const char *groupname)
1065 TALLOC_CTX *mem_ctx;
1066 DOM_SID group_sid;
1067 NTSTATUS status;
1068 BOOL ret;
1070 mem_ctx = talloc_new(NULL);
1071 if (mem_ctx == NULL) {
1072 DEBUG(0, ("talloc_new failed\n"));
1073 return False;
1076 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
1077 NULL, NULL, &group_sid, NULL);
1078 TALLOC_FREE(mem_ctx);
1080 if (!ret) {
1081 DEBUG(10, ("lookup_name(%s) failed: %s\n", groupname,
1082 nt_errstr(status)));
1083 return False;
1086 return user_in_group_sid(username, &group_sid);
1090 /***************************************************************************
1091 Make (and fill) a user_info struct from a Kerberos PAC logon_info by
1092 conversion to a struct samu
1093 ***************************************************************************/
1095 NTSTATUS make_server_info_pac(auth_serversupplied_info **server_info,
1096 char *unix_username,
1097 struct passwd *pwd,
1098 PAC_LOGON_INFO *logon_info)
1100 NTSTATUS status;
1101 struct samu *sampass = NULL;
1102 DOM_SID user_sid, group_sid;
1103 fstring dom_name;
1104 auth_serversupplied_info *result;
1105 int i;
1107 if ( !(sampass = samu_new( NULL )) ) {
1108 return NT_STATUS_NO_MEMORY;
1111 status = samu_set_unix( sampass, pwd );
1112 if ( !NT_STATUS_IS_OK(status) ) {
1113 return status;
1116 result = make_server_info(NULL);
1117 if (result == NULL) {
1118 TALLOC_FREE(sampass);
1119 return NT_STATUS_NO_MEMORY;
1122 /* only copy user_sid, group_sid and domain name out of the PAC for
1123 * now, we will benefit from more later - Guenther */
1125 sid_copy(&user_sid, &logon_info->info3.dom_sid.sid);
1126 sid_append_rid(&user_sid, logon_info->info3.user_rid);
1127 pdb_set_user_sid(sampass, &user_sid, PDB_SET);
1129 sid_copy(&group_sid, &logon_info->info3.dom_sid.sid);
1130 sid_append_rid(&group_sid, logon_info->info3.group_rid);
1131 pdb_set_group_sid(sampass, &group_sid, PDB_SET);
1133 unistr2_to_ascii(dom_name, &logon_info->info3.uni_logon_dom, -1);
1134 pdb_set_domain(sampass, dom_name, PDB_SET);
1136 pdb_set_logon_count(sampass, logon_info->info3.logon_count, PDB_SET);
1138 result->sam_account = sampass;
1139 result->unix_name = talloc_strdup(result, unix_username);
1140 result->uid = pwd->pw_uid;
1141 result->gid = pwd->pw_gid;
1143 result->sids = NULL;
1144 result->num_sids = 0;
1146 /* and create (by appending rids) the 'domain' sids */
1148 for (i = 0; i < logon_info->info3.num_groups2; i++) {
1149 DOM_SID sid;
1150 if (!sid_compose(&sid, &logon_info->info3.dom_sid.sid,
1151 logon_info->info3.gids[i].g_rid)) {
1152 DEBUG(3,("could not append additional group rid "
1153 "0x%x\n", logon_info->info3.gids[i].g_rid));
1154 TALLOC_FREE(result);
1155 return NT_STATUS_INVALID_PARAMETER;
1157 add_sid_to_array(result, &sid, &result->sids,
1158 &result->num_sids);
1161 /* Copy 'other' sids. We need to do sid filtering here to
1162 prevent possible elevation of privileges. See:
1164 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1167 for (i = 0; i < logon_info->info3.num_other_sids; i++) {
1168 add_sid_to_array(result, &logon_info->info3.other_sids[i].sid,
1169 &result->sids,
1170 &result->num_sids);
1173 *server_info = result;
1175 return NT_STATUS_OK;
1179 /***************************************************************************
1180 Make (and fill) a user_info struct from a 'struct passwd' by conversion
1181 to a struct samu
1182 ***************************************************************************/
1184 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info,
1185 char *unix_username,
1186 struct passwd *pwd)
1188 NTSTATUS status;
1189 struct samu *sampass = NULL;
1190 gid_t *gids;
1191 auth_serversupplied_info *result;
1193 if ( !(sampass = samu_new( NULL )) ) {
1194 return NT_STATUS_NO_MEMORY;
1197 status = samu_set_unix( sampass, pwd );
1198 if (!NT_STATUS_IS_OK(status)) {
1199 return status;
1202 result = make_server_info(NULL);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 TALLOC_FREE(sampass);
1206 return status;
1209 result->sam_account = sampass;
1210 result->unix_name = talloc_strdup(result, unix_username);
1211 result->uid = pwd->pw_uid;
1212 result->gid = pwd->pw_gid;
1214 status = pdb_enum_group_memberships(result, sampass,
1215 &result->sids, &gids,
1216 &result->num_sids);
1218 if (!NT_STATUS_IS_OK(status)) {
1219 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
1220 nt_errstr(status)));
1221 TALLOC_FREE(result);
1222 return status;
1225 /* For now we throw away the gids and convert via sid_to_gid
1226 * later. This needs fixing, but I'd like to get the code straight and
1227 * simple first. */
1228 TALLOC_FREE(gids);
1230 *server_info = result;
1232 return NT_STATUS_OK;
1235 /***************************************************************************
1236 Make (and fill) a user_info struct for a guest login.
1237 This *must* succeed for smbd to start. If there is no mapping entry for
1238 the guest gid, then create one.
1239 **********************
1240 *****************************************************/
1242 static NTSTATUS make_new_server_info_guest(auth_serversupplied_info **server_info)
1244 NTSTATUS status;
1245 struct samu *sampass = NULL;
1246 DOM_SID guest_sid;
1247 BOOL ret;
1248 static const char zeros[16];
1250 if ( !(sampass = samu_new( NULL )) ) {
1251 return NT_STATUS_NO_MEMORY;
1254 sid_copy(&guest_sid, get_global_sam_sid());
1255 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
1257 become_root();
1258 ret = pdb_getsampwsid(sampass, &guest_sid);
1259 unbecome_root();
1261 if (!ret) {
1262 TALLOC_FREE(sampass);
1263 return NT_STATUS_NO_SUCH_USER;
1266 status = make_server_info_sam(server_info, sampass);
1267 if (!NT_STATUS_IS_OK(status)) {
1268 TALLOC_FREE(sampass);
1269 return status;
1272 (*server_info)->guest = True;
1274 status = create_local_token(*server_info);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 DEBUG(10, ("create_local_token failed: %s\n",
1277 nt_errstr(status)));
1278 return status;
1281 /* annoying, but the Guest really does have a session key, and it is
1282 all zeros! */
1283 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
1284 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
1286 return NT_STATUS_OK;
1289 static auth_serversupplied_info *copy_serverinfo(auth_serversupplied_info *src)
1291 auth_serversupplied_info *dst;
1293 dst = make_server_info(NULL);
1294 if (dst == NULL) {
1295 return NULL;
1298 dst->guest = src->guest;
1299 dst->uid = src->uid;
1300 dst->gid = src->gid;
1301 dst->n_groups = src->n_groups;
1302 if (src->n_groups != 0)
1303 dst->groups = talloc_memdup(dst, src->groups,
1304 sizeof(gid_t)*dst->n_groups);
1305 else
1306 dst->groups = NULL;
1308 dst->ptok = dup_nt_token(dst, src->ptok);
1310 dst->user_session_key = data_blob_talloc( dst, src->user_session_key.data,
1311 src->user_session_key.length);
1313 dst->lm_session_key = data_blob_talloc(dst, src->lm_session_key.data,
1314 src->lm_session_key.length);
1316 if ( (dst->sam_account = samu_new( NULL )) != NULL )
1317 pdb_copy_sam_account(dst->sam_account, src->sam_account);
1319 dst->pam_handle = NULL;
1320 dst->unix_name = talloc_strdup(dst, src->unix_name);
1322 return dst;
1325 static auth_serversupplied_info *guest_info = NULL;
1327 BOOL init_guest_info(void)
1329 if (guest_info != NULL)
1330 return True;
1332 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info));
1335 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
1337 *server_info = copy_serverinfo(guest_info);
1338 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1341 /***************************************************************************
1342 Purely internal function for make_server_info_info3
1343 Fill the sam account from getpwnam
1344 ***************************************************************************/
1345 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx,
1346 const char *domain,
1347 const char *username,
1348 char **found_username,
1349 uid_t *uid, gid_t *gid,
1350 struct samu *account)
1352 NTSTATUS nt_status;
1353 fstring dom_user, lower_username;
1354 fstring real_username;
1355 struct passwd *passwd;
1357 fstrcpy( lower_username, username );
1358 strlower_m( lower_username );
1360 fstr_sprintf(dom_user, "%s%c%s", domain, *lp_winbind_separator(),
1361 lower_username);
1363 /* get the passwd struct but don't create the user if he/she
1364 does not exist. We were explicitly called from a following
1365 a winbindd authentication request so we should assume that
1366 nss_winbindd is working */
1368 map_username( dom_user );
1370 if ( !(passwd = smb_getpwnam( NULL, dom_user, real_username, True )) )
1371 return NT_STATUS_NO_SUCH_USER;
1373 *uid = passwd->pw_uid;
1374 *gid = passwd->pw_gid;
1376 /* This is pointless -- there is no suport for differing
1377 unix and windows names. Make sure to always store the
1378 one we actually looked up and succeeded. Have I mentioned
1379 why I hate the 'winbind use default domain' parameter?
1380 --jerry */
1382 *found_username = talloc_strdup( mem_ctx, real_username );
1384 DEBUG(5,("fill_sam_account: located username was [%s]\n", *found_username));
1386 nt_status = samu_set_unix( account, passwd );
1388 TALLOC_FREE(passwd);
1390 return nt_status;
1393 /****************************************************************************
1394 Wrapper to allow the getpwnam() call to strip the domain name and
1395 try again in case a local UNIX user is already there. Also run through
1396 the username if we fallback to the username only.
1397 ****************************************************************************/
1399 struct passwd *smb_getpwnam( TALLOC_CTX *mem_ctx, char *domuser,
1400 fstring save_username, BOOL create )
1402 struct passwd *pw = NULL;
1403 char *p;
1404 fstring username;
1406 /* we only save a copy of the username it has been mangled
1407 by winbindd use default domain */
1409 save_username[0] = '\0';
1411 /* don't call map_username() here since it has to be done higher
1412 up the stack so we don't call it mutliple times */
1414 fstrcpy( username, domuser );
1416 p = strchr_m( username, *lp_winbind_separator() );
1418 /* code for a DOMAIN\user string */
1420 if ( p ) {
1421 fstring strip_username;
1423 pw = Get_Pwnam_alloc( mem_ctx, domuser );
1424 if ( pw ) {
1425 /* make sure we get the case of the username correct */
1426 /* work around 'winbind use default domain = yes' */
1428 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1429 char *domain;
1431 /* split the domain and username into 2 strings */
1432 *p = '\0';
1433 domain = username;
1435 fstr_sprintf(save_username, "%s%c%s", domain, *lp_winbind_separator(), pw->pw_name);
1437 else
1438 fstrcpy( save_username, pw->pw_name );
1440 /* whew -- done! */
1441 return pw;
1444 /* setup for lookup of just the username */
1445 /* remember that p and username are overlapping memory */
1447 p++;
1448 fstrcpy( strip_username, p );
1449 fstrcpy( username, strip_username );
1452 /* just lookup a plain username */
1454 pw = Get_Pwnam_alloc(mem_ctx, username);
1456 /* Create local user if requested. */
1458 if ( !pw && create ) {
1459 /* Don't add a machine account. */
1460 if (username[strlen(username)-1] == '$')
1461 return NULL;
1463 smb_create_user(NULL, username, NULL);
1464 pw = Get_Pwnam_alloc(mem_ctx, username);
1467 /* one last check for a valid passwd struct */
1469 if ( pw )
1470 fstrcpy( save_username, pw->pw_name );
1472 return pw;
1475 /***************************************************************************
1476 Make a server_info struct from the info3 returned by a domain logon
1477 ***************************************************************************/
1479 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1480 const char *internal_username,
1481 const char *sent_nt_username,
1482 const char *domain,
1483 auth_serversupplied_info **server_info,
1484 NET_USER_INFO_3 *info3)
1486 static const char zeros[16];
1488 NTSTATUS nt_status = NT_STATUS_OK;
1489 char *found_username;
1490 const char *nt_domain;
1491 const char *nt_username;
1492 struct samu *sam_account = NULL;
1493 DOM_SID user_sid;
1494 DOM_SID group_sid;
1496 uid_t uid;
1497 gid_t gid;
1499 size_t i;
1501 auth_serversupplied_info *result;
1504 Here is where we should check the list of
1505 trusted domains, and verify that the SID
1506 matches.
1509 sid_copy(&user_sid, &info3->dom_sid.sid);
1510 if (!sid_append_rid(&user_sid, info3->user_rid)) {
1511 return NT_STATUS_INVALID_PARAMETER;
1514 sid_copy(&group_sid, &info3->dom_sid.sid);
1515 if (!sid_append_rid(&group_sid, info3->group_rid)) {
1516 return NT_STATUS_INVALID_PARAMETER;
1519 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
1520 /* If the server didn't give us one, just use the one we sent
1521 * them */
1522 nt_username = sent_nt_username;
1525 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
1526 /* If the server didn't give us one, just use the one we sent
1527 * them */
1528 nt_domain = domain;
1531 /* try to fill the SAM account.. If getpwnam() fails, then try the
1532 add user script (2.2.x behavior).
1534 We use the _unmapped_ username here in an attempt to provide
1535 consistent username mapping behavior between kerberos and NTLM[SSP]
1536 authentication in domain mode security. I.E. Username mapping
1537 should be applied to the fully qualified username
1538 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1539 called map_username() unnecessarily in make_user_info_map() but
1540 that is how the current code is designed. Making the change here
1541 is the least disruptive place. -- jerry */
1543 if ( !(sam_account = samu_new( NULL )) ) {
1544 return NT_STATUS_NO_MEMORY;
1547 nt_status = fill_sam_account(mem_ctx, nt_domain, sent_nt_username,
1548 &found_username, &uid, &gid, sam_account);
1550 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1551 DEBUG(3,("User %s does not exist, trying to add it\n",
1552 internal_username));
1553 smb_create_user( nt_domain, sent_nt_username, NULL);
1554 nt_status = fill_sam_account( mem_ctx, nt_domain, sent_nt_username,
1555 &found_username, &uid, &gid, sam_account );
1558 /* if we still don't have a valid unix account check for
1559 'map to guest = bad uid' */
1561 if (!NT_STATUS_IS_OK(nt_status)) {
1562 TALLOC_FREE( sam_account );
1563 if ( lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID ) {
1564 make_server_info_guest(server_info);
1565 return NT_STATUS_OK;
1567 return nt_status;
1570 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1571 TALLOC_FREE(sam_account);
1572 return NT_STATUS_NO_MEMORY;
1575 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1576 TALLOC_FREE(sam_account);
1577 return NT_STATUS_NO_MEMORY;
1580 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1581 TALLOC_FREE(sam_account);
1582 return NT_STATUS_NO_MEMORY;
1585 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1586 TALLOC_FREE(sam_account);
1587 return NT_STATUS_UNSUCCESSFUL;
1590 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1591 TALLOC_FREE(sam_account);
1592 return NT_STATUS_UNSUCCESSFUL;
1595 if (!pdb_set_fullname(sam_account,
1596 unistr2_static(&(info3->uni_full_name)),
1597 PDB_CHANGED)) {
1598 TALLOC_FREE(sam_account);
1599 return NT_STATUS_NO_MEMORY;
1602 if (!pdb_set_logon_script(sam_account,
1603 unistr2_static(&(info3->uni_logon_script)),
1604 PDB_CHANGED)) {
1605 TALLOC_FREE(sam_account);
1606 return NT_STATUS_NO_MEMORY;
1609 if (!pdb_set_profile_path(sam_account,
1610 unistr2_static(&(info3->uni_profile_path)),
1611 PDB_CHANGED)) {
1612 TALLOC_FREE(sam_account);
1613 return NT_STATUS_NO_MEMORY;
1616 if (!pdb_set_homedir(sam_account,
1617 unistr2_static(&(info3->uni_home_dir)),
1618 PDB_CHANGED)) {
1619 TALLOC_FREE(sam_account);
1620 return NT_STATUS_NO_MEMORY;
1623 if (!pdb_set_dir_drive(sam_account,
1624 unistr2_static(&(info3->uni_dir_drive)),
1625 PDB_CHANGED)) {
1626 TALLOC_FREE(sam_account);
1627 return NT_STATUS_NO_MEMORY;
1630 if (!pdb_set_acct_ctrl(sam_account, info3->acct_flags, PDB_CHANGED)) {
1631 TALLOC_FREE(sam_account);
1632 return NT_STATUS_NO_MEMORY;
1635 result = make_server_info(NULL);
1636 if (result == NULL) {
1637 DEBUG(4, ("make_server_info failed!\n"));
1638 TALLOC_FREE(sam_account);
1639 return NT_STATUS_NO_MEMORY;
1642 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1643 valid struct samu) */
1645 result->sam_account = sam_account;
1646 result->unix_name = talloc_strdup(result, found_username);
1648 /* Fill in the unix info we found on the way */
1650 result->uid = uid;
1651 result->gid = gid;
1653 /* Create a 'combined' list of all SIDs we might want in the SD */
1655 result->num_sids = 0;
1656 result->sids = NULL;
1658 /* and create (by appending rids) the 'domain' sids */
1660 for (i = 0; i < info3->num_groups2; i++) {
1661 DOM_SID sid;
1662 if (!sid_compose(&sid, &info3->dom_sid.sid,
1663 info3->gids[i].g_rid)) {
1664 DEBUG(3,("could not append additional group rid "
1665 "0x%x\n", info3->gids[i].g_rid));
1666 TALLOC_FREE(result);
1667 return NT_STATUS_INVALID_PARAMETER;
1669 add_sid_to_array(result, &sid, &result->sids,
1670 &result->num_sids);
1673 /* Copy 'other' sids. We need to do sid filtering here to
1674 prevent possible elevation of privileges. See:
1676 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1679 for (i = 0; i < info3->num_other_sids; i++) {
1680 add_sid_to_array(result, &info3->other_sids[i].sid,
1681 &result->sids,
1682 &result->num_sids);
1685 result->login_server = unistr2_tdup(result,
1686 &(info3->uni_logon_srv));
1688 /* ensure we are never given NULL session keys */
1690 if (memcmp(info3->user_sess_key, zeros, sizeof(zeros)) == 0) {
1691 result->user_session_key = data_blob(NULL, 0);
1692 } else {
1693 result->user_session_key = data_blob_talloc(
1694 result, info3->user_sess_key,
1695 sizeof(info3->user_sess_key));
1698 if (memcmp(info3->lm_sess_key, zeros, 8) == 0) {
1699 result->lm_session_key = data_blob(NULL, 0);
1700 } else {
1701 result->lm_session_key = data_blob_talloc(
1702 result, info3->lm_sess_key,
1703 sizeof(info3->lm_sess_key));
1706 *server_info = result;
1708 return NT_STATUS_OK;
1711 /***************************************************************************
1712 Free a user_info struct
1713 ***************************************************************************/
1715 void free_user_info(auth_usersupplied_info **user_info)
1717 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1718 if (*user_info != NULL) {
1719 if ((*user_info)->smb_name) {
1720 DEBUG(10,("structure was created for %s\n",
1721 (*user_info)->smb_name));
1723 SAFE_FREE((*user_info)->smb_name);
1724 SAFE_FREE((*user_info)->internal_username);
1725 SAFE_FREE((*user_info)->client_domain);
1726 SAFE_FREE((*user_info)->domain);
1727 SAFE_FREE((*user_info)->wksta_name);
1728 data_blob_free(&(*user_info)->lm_resp);
1729 data_blob_free(&(*user_info)->nt_resp);
1730 data_blob_clear_free(&(*user_info)->lm_interactive_pwd);
1731 data_blob_clear_free(&(*user_info)->nt_interactive_pwd);
1732 data_blob_clear_free(&(*user_info)->plaintext_password);
1733 ZERO_STRUCT(**user_info);
1735 SAFE_FREE(*user_info);
1738 /***************************************************************************
1739 Make an auth_methods struct
1740 ***************************************************************************/
1742 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
1744 if (!auth_context) {
1745 smb_panic("no auth_context supplied to "
1746 "make_auth_methods()!\n");
1749 if (!auth_method) {
1750 smb_panic("make_auth_methods: pointer to auth_method pointer "
1751 "is NULL!\n");
1754 *auth_method = TALLOC_P(auth_context->mem_ctx, auth_methods);
1755 if (!*auth_method) {
1756 DEBUG(0,("make_auth_method: malloc failed!\n"));
1757 return False;
1759 ZERO_STRUCTP(*auth_method);
1761 return True;
1764 /****************************************************************************
1765 Duplicate a SID token.
1766 ****************************************************************************/
1768 NT_USER_TOKEN *dup_nt_token(TALLOC_CTX *mem_ctx, NT_USER_TOKEN *ptoken)
1770 NT_USER_TOKEN *token;
1772 if (!ptoken)
1773 return NULL;
1775 token = TALLOC_P(mem_ctx, NT_USER_TOKEN);
1776 if (token == NULL) {
1777 DEBUG(0, ("talloc failed\n"));
1778 return NULL;
1781 token->user_sids = talloc_memdup(token, ptoken->user_sids,
1782 sizeof(DOM_SID) * ptoken->num_sids );
1784 if ((ptoken->user_sids != NULL) && (token->user_sids == NULL)) {
1785 DEBUG(0, ("talloc_memdup failed\n"));
1786 TALLOC_FREE(token);
1787 return NULL;
1790 token->num_sids = ptoken->num_sids;
1792 /* copy the privileges; don't consider failure to be critical here */
1794 if ( !se_priv_copy( &token->privileges, &ptoken->privileges ) ) {
1795 DEBUG(0,("dup_nt_token: Failure to copy SE_PRIV!. "
1796 "Continuing with 0 privileges assigned.\n"));
1799 return token;
1802 /****************************************************************************
1803 Check for a SID in an NT_USER_TOKEN
1804 ****************************************************************************/
1806 BOOL nt_token_check_sid ( const DOM_SID *sid, const NT_USER_TOKEN *token )
1808 int i;
1810 if ( !sid || !token )
1811 return False;
1813 for ( i=0; i<token->num_sids; i++ ) {
1814 if ( sid_equal( sid, &token->user_sids[i] ) )
1815 return True;
1818 return False;
1821 BOOL nt_token_check_domain_rid( NT_USER_TOKEN *token, uint32 rid )
1823 DOM_SID domain_sid;
1825 /* if we are a domain member, the get the domain SID, else for
1826 a DC or standalone server, use our own SID */
1828 if ( lp_server_role() == ROLE_DOMAIN_MEMBER ) {
1829 if ( !secrets_fetch_domain_sid( lp_workgroup(),
1830 &domain_sid ) ) {
1831 DEBUG(1,("nt_token_check_domain_rid: Cannot lookup "
1832 "SID for domain [%s]\n", lp_workgroup()));
1833 return False;
1836 else
1837 sid_copy( &domain_sid, get_global_sam_sid() );
1839 sid_append_rid( &domain_sid, rid );
1841 return nt_token_check_sid( &domain_sid, token );\
1845 * Verify whether or not given domain is trusted.
1847 * @param domain_name name of the domain to be verified
1848 * @return true if domain is one of the trusted once or
1849 * false if otherwise
1852 BOOL is_trusted_domain(const char* dom_name)
1854 DOM_SID trustdom_sid;
1855 BOOL ret;
1857 /* no trusted domains for a standalone server */
1859 if ( lp_server_role() == ROLE_STANDALONE )
1860 return False;
1862 /* if we are a DC, then check for a direct trust relationships */
1864 if ( IS_DC ) {
1865 become_root();
1866 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
1867 "[%s]\n", dom_name ));
1868 ret = secrets_fetch_trusted_domain_password(dom_name, NULL,
1869 NULL, NULL);
1870 unbecome_root();
1871 if (ret)
1872 return True;
1874 else {
1875 NSS_STATUS result;
1877 /* If winbind is around, ask it */
1879 result = wb_is_trusted_domain(dom_name);
1881 if (result == NSS_STATUS_SUCCESS) {
1882 return True;
1885 if (result == NSS_STATUS_NOTFOUND) {
1886 /* winbind could not find the domain */
1887 return False;
1890 /* The only other possible result is that winbind is not up
1891 and running. We need to update the trustdom_cache
1892 ourselves */
1894 update_trustdom_cache();
1897 /* now the trustdom cache should be available a DC could still
1898 * have a transitive trust so fall back to the cache of trusted
1899 * domains (like a domain member would use */
1901 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {
1902 return True;
1905 return False;