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.
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
,
34 const DOM_SID
*groupsids
);
36 /****************************************************************************
37 Create a UNIX user on demand.
38 ****************************************************************************/
40 static int smb_create_user(const char *domain
, const char *unix_username
, const char *homedir
)
45 pstrcpy(add_script
, lp_adduser_script());
48 all_string_sub(add_script
, "%u", unix_username
, sizeof(pstring
));
50 all_string_sub(add_script
, "%D", domain
, sizeof(pstring
));
52 all_string_sub(add_script
, "%H", homedir
, sizeof(pstring
));
53 ret
= smbrun(add_script
,NULL
);
55 DEBUG(ret
? 0 : 3,("smb_create_user: Running the command `%s' gave %d\n",add_script
,ret
));
59 /****************************************************************************
60 Create an auth_usersupplied_data structure
61 ****************************************************************************/
63 static NTSTATUS
make_user_info(auth_usersupplied_info
**user_info
,
65 const char *internal_username
,
66 const char *client_domain
,
68 const char *wksta_name
,
69 DATA_BLOB
*lm_pwd
, DATA_BLOB
*nt_pwd
,
70 DATA_BLOB
*lm_interactive_pwd
, DATA_BLOB
*nt_interactive_pwd
,
75 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username
, smb_name
));
77 *user_info
= SMB_MALLOC_P(auth_usersupplied_info
);
78 if (*user_info
== NULL
) {
79 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info
)));
80 return NT_STATUS_NO_MEMORY
;
83 ZERO_STRUCTP(*user_info
);
85 DEBUG(5,("making strings for %s's user_info struct\n", internal_username
));
87 (*user_info
)->smb_name
= SMB_STRDUP(smb_name
);
88 if ((*user_info
)->smb_name
== NULL
) {
89 free_user_info(user_info
);
90 return NT_STATUS_NO_MEMORY
;
93 (*user_info
)->internal_username
= SMB_STRDUP(internal_username
);
94 if ((*user_info
)->internal_username
== NULL
) {
95 free_user_info(user_info
);
96 return NT_STATUS_NO_MEMORY
;
99 (*user_info
)->domain
= SMB_STRDUP(domain
);
100 if ((*user_info
)->domain
== NULL
) {
101 free_user_info(user_info
);
102 return NT_STATUS_NO_MEMORY
;
105 (*user_info
)->client_domain
= SMB_STRDUP(client_domain
);
106 if ((*user_info
)->client_domain
== NULL
) {
107 free_user_info(user_info
);
108 return NT_STATUS_NO_MEMORY
;
111 (*user_info
)->wksta_name
= SMB_STRDUP(wksta_name
);
112 if ((*user_info
)->wksta_name
== NULL
) {
113 free_user_info(user_info
);
114 return NT_STATUS_NO_MEMORY
;
117 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username
));
120 (*user_info
)->lm_resp
= data_blob(lm_pwd
->data
, lm_pwd
->length
);
122 (*user_info
)->nt_resp
= data_blob(nt_pwd
->data
, nt_pwd
->length
);
123 if (lm_interactive_pwd
)
124 (*user_info
)->lm_interactive_pwd
= data_blob(lm_interactive_pwd
->data
, lm_interactive_pwd
->length
);
125 if (nt_interactive_pwd
)
126 (*user_info
)->nt_interactive_pwd
= data_blob(nt_interactive_pwd
->data
, nt_interactive_pwd
->length
);
129 (*user_info
)->plaintext_password
= data_blob(plaintext
->data
, plaintext
->length
);
131 (*user_info
)->encrypted
= encrypted
;
133 (*user_info
)->logon_parameters
= 0;
135 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted
? "":"un" , internal_username
, smb_name
));
140 /****************************************************************************
141 Create an auth_usersupplied_data structure after appropriate mapping.
142 ****************************************************************************/
144 NTSTATUS
make_user_info_map(auth_usersupplied_info
**user_info
,
145 const char *smb_name
,
146 const char *client_domain
,
147 const char *wksta_name
,
148 DATA_BLOB
*lm_pwd
, DATA_BLOB
*nt_pwd
,
149 DATA_BLOB
*lm_interactive_pwd
, DATA_BLOB
*nt_interactive_pwd
,
150 DATA_BLOB
*plaintext
,
156 fstring internal_username
;
157 fstrcpy(internal_username
, smb_name
);
158 was_mapped
= map_username(internal_username
);
160 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
161 client_domain
, smb_name
, wksta_name
));
163 /* don't allow "" as a domain, fixes a Win9X bug
164 where it doens't supply a domain for logon script
165 'net use' commands. */
167 if ( *client_domain
)
168 domain
= client_domain
;
170 domain
= lp_workgroup();
172 /* do what win2k does. Always map unknown domains to our own
173 and let the "passdb backend" handle unknown users. */
175 if ( !is_trusted_domain(domain
) && !strequal(domain
, get_global_sam_name()) )
176 domain
= my_sam_name();
178 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
180 result
= make_user_info(user_info
, smb_name
, internal_username
,
181 client_domain
, domain
, wksta_name
,
183 lm_interactive_pwd
, nt_interactive_pwd
,
184 plaintext
, encrypted
);
185 if (NT_STATUS_IS_OK(result
)) {
186 (*user_info
)->was_mapped
= was_mapped
;
191 /****************************************************************************
192 Create an auth_usersupplied_data, making the DATA_BLOBs here.
193 Decrypt and encrypt the passwords.
194 ****************************************************************************/
196 BOOL
make_user_info_netlogon_network(auth_usersupplied_info
**user_info
,
197 const char *smb_name
,
198 const char *client_domain
,
199 const char *wksta_name
,
200 uint32 logon_parameters
,
201 const uchar
*lm_network_pwd
,
203 const uchar
*nt_network_pwd
,
208 DATA_BLOB lm_blob
= data_blob(lm_network_pwd
, lm_pwd_len
);
209 DATA_BLOB nt_blob
= data_blob(nt_network_pwd
, nt_pwd_len
);
211 status
= make_user_info_map(user_info
,
212 smb_name
, client_domain
,
214 lm_pwd_len
? &lm_blob
: NULL
,
215 nt_pwd_len
? &nt_blob
: NULL
,
219 if (NT_STATUS_IS_OK(status
)) {
220 (*user_info
)->logon_parameters
= logon_parameters
;
222 ret
= NT_STATUS_IS_OK(status
) ? True
: False
;
224 data_blob_free(&lm_blob
);
225 data_blob_free(&nt_blob
);
229 /****************************************************************************
230 Create an auth_usersupplied_data, making the DATA_BLOBs here.
231 Decrypt and encrypt the passwords.
232 ****************************************************************************/
234 BOOL
make_user_info_netlogon_interactive(auth_usersupplied_info
**user_info
,
235 const char *smb_name
,
236 const char *client_domain
,
237 const char *wksta_name
,
238 uint32 logon_parameters
,
240 const uchar lm_interactive_pwd
[16],
241 const uchar nt_interactive_pwd
[16],
242 const uchar
*dc_sess_key
)
246 unsigned char local_lm_response
[24];
247 unsigned char local_nt_response
[24];
248 unsigned char key
[16];
251 memcpy(key
, dc_sess_key
, 8);
253 if (lm_interactive_pwd
)
254 memcpy(lm_pwd
, lm_interactive_pwd
, sizeof(lm_pwd
));
256 if (nt_interactive_pwd
)
257 memcpy(nt_pwd
, nt_interactive_pwd
, sizeof(nt_pwd
));
259 #ifdef DEBUG_PASSWORD
261 dump_data(100, (char *)key
, sizeof(key
));
263 DEBUG(100,("lm owf password:"));
264 dump_data(100, lm_pwd
, sizeof(lm_pwd
));
266 DEBUG(100,("nt owf password:"));
267 dump_data(100, nt_pwd
, sizeof(nt_pwd
));
270 if (lm_interactive_pwd
)
271 SamOEMhash((uchar
*)lm_pwd
, key
, sizeof(lm_pwd
));
273 if (nt_interactive_pwd
)
274 SamOEMhash((uchar
*)nt_pwd
, key
, sizeof(nt_pwd
));
276 #ifdef DEBUG_PASSWORD
277 DEBUG(100,("decrypt of lm owf password:"));
278 dump_data(100, lm_pwd
, sizeof(lm_pwd
));
280 DEBUG(100,("decrypt of nt owf password:"));
281 dump_data(100, nt_pwd
, sizeof(nt_pwd
));
284 if (lm_interactive_pwd
)
285 SMBOWFencrypt((const unsigned char *)lm_pwd
, chal
,
288 if (nt_interactive_pwd
)
289 SMBOWFencrypt((const unsigned char *)nt_pwd
, chal
,
292 /* Password info paranoia */
298 DATA_BLOB local_lm_blob
;
299 DATA_BLOB local_nt_blob
;
301 DATA_BLOB lm_interactive_blob
;
302 DATA_BLOB nt_interactive_blob
;
304 if (lm_interactive_pwd
) {
305 local_lm_blob
= data_blob(local_lm_response
,
306 sizeof(local_lm_response
));
307 lm_interactive_blob
= data_blob(lm_pwd
,
312 if (nt_interactive_pwd
) {
313 local_nt_blob
= data_blob(local_nt_response
,
314 sizeof(local_nt_response
));
315 nt_interactive_blob
= data_blob(nt_pwd
,
320 nt_status
= make_user_info_map(
322 smb_name
, client_domain
, wksta_name
,
323 lm_interactive_pwd
? &local_lm_blob
: NULL
,
324 nt_interactive_pwd
? &local_nt_blob
: NULL
,
325 lm_interactive_pwd
? &lm_interactive_blob
: NULL
,
326 nt_interactive_pwd
? &nt_interactive_blob
: NULL
,
329 if (NT_STATUS_IS_OK(nt_status
)) {
330 (*user_info
)->logon_parameters
= logon_parameters
;
333 ret
= NT_STATUS_IS_OK(nt_status
) ? True
: False
;
334 data_blob_free(&local_lm_blob
);
335 data_blob_free(&local_nt_blob
);
336 data_blob_free(&lm_interactive_blob
);
337 data_blob_free(&nt_interactive_blob
);
343 /****************************************************************************
344 Create an auth_usersupplied_data structure
345 ****************************************************************************/
347 BOOL
make_user_info_for_reply(auth_usersupplied_info
**user_info
,
348 const char *smb_name
,
349 const char *client_domain
,
351 DATA_BLOB plaintext_password
)
354 DATA_BLOB local_lm_blob
;
355 DATA_BLOB local_nt_blob
;
356 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
359 * Not encrypted - do so.
362 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
365 if (plaintext_password
.data
) {
366 unsigned char local_lm_response
[24];
368 #ifdef DEBUG_PASSWORD
369 DEBUG(10,("Unencrypted password (len %d):\n",
370 (int)plaintext_password
.length
));
371 dump_data(100, (const char *)plaintext_password
.data
,
372 plaintext_password
.length
);
375 SMBencrypt( (const char *)plaintext_password
.data
,
376 (const uchar
*)chal
, local_lm_response
);
377 local_lm_blob
= data_blob(local_lm_response
, 24);
379 /* We can't do an NT hash here, as the password needs to be
381 local_nt_blob
= data_blob(NULL
, 0);
384 local_lm_blob
= data_blob(NULL
, 0);
385 local_nt_blob
= data_blob(NULL
, 0);
388 ret
= make_user_info_map(
389 user_info
, smb_name
, client_domain
,
390 get_remote_machine_name(),
391 local_lm_blob
.data
? &local_lm_blob
: NULL
,
392 local_nt_blob
.data
? &local_nt_blob
: NULL
,
394 plaintext_password
.data
? &plaintext_password
: NULL
,
397 data_blob_free(&local_lm_blob
);
398 return NT_STATUS_IS_OK(ret
) ? True
: False
;
401 /****************************************************************************
402 Create an auth_usersupplied_data structure
403 ****************************************************************************/
405 NTSTATUS
make_user_info_for_reply_enc(auth_usersupplied_info
**user_info
,
406 const char *smb_name
,
407 const char *client_domain
,
408 DATA_BLOB lm_resp
, DATA_BLOB nt_resp
)
410 return make_user_info_map(user_info
, smb_name
,
412 get_remote_machine_name(),
413 lm_resp
.data
? &lm_resp
: NULL
,
414 nt_resp
.data
? &nt_resp
: NULL
,
419 /****************************************************************************
420 Create a guest user_info blob, for anonymous authenticaion.
421 ****************************************************************************/
423 BOOL
make_user_info_guest(auth_usersupplied_info
**user_info
)
427 nt_status
= make_user_info(user_info
,
436 return NT_STATUS_IS_OK(nt_status
) ? True
: False
;
439 /****************************************************************************
440 prints a NT_USER_TOKEN to debug output.
441 ****************************************************************************/
443 void debug_nt_user_token(int dbg_class
, int dbg_lev
, NT_USER_TOKEN
*token
)
448 DEBUGC(dbg_class
, dbg_lev
, ("NT user token: (NULL)\n"));
452 DEBUGC(dbg_class
, dbg_lev
,
453 ("NT user token of user %s\n",
454 sid_string_static(&token
->user_sids
[0]) ));
455 DEBUGADDC(dbg_class
, dbg_lev
,
456 ("contains %lu SIDs\n", (unsigned long)token
->num_sids
));
457 for (i
= 0; i
< token
->num_sids
; i
++)
458 DEBUGADDC(dbg_class
, dbg_lev
,
459 ("SID[%3lu]: %s\n", (unsigned long)i
,
460 sid_string_static(&token
->user_sids
[i
])));
462 dump_se_priv( dbg_class
, dbg_lev
, &token
->privileges
);
465 /****************************************************************************
466 prints a UNIX 'token' to debug output.
467 ****************************************************************************/
469 void debug_unix_user_token(int dbg_class
, int dbg_lev
, uid_t uid
, gid_t gid
,
470 int n_groups
, gid_t
*groups
)
473 DEBUGC(dbg_class
, dbg_lev
,
474 ("UNIX token of user %ld\n", (long int)uid
));
476 DEBUGADDC(dbg_class
, dbg_lev
,
477 ("Primary group is %ld and contains %i supplementary "
478 "groups\n", (long int)gid
, n_groups
));
479 for (i
= 0; i
< n_groups
; i
++)
480 DEBUGADDC(dbg_class
, dbg_lev
, ("Group[%3i]: %ld\n", i
,
481 (long int)groups
[i
]));
484 /******************************************************************************
485 Create a token for the root user to be used internally by smbd.
486 This is similar to running under the context of the LOCAL_SYSTEM account
487 in Windows. This is a read-only token. Do not modify it or free() it.
488 Create a copy if your need to change it.
489 ******************************************************************************/
491 NT_USER_TOKEN
*get_root_nt_token( void )
493 static NT_USER_TOKEN
*token
= NULL
;
494 DOM_SID u_sid
, g_sid
;
500 if ( !(pw
= sys_getpwnam( "root" )) ) {
501 DEBUG(0,("get_root_nt_token: getpwnam\"root\") failed!\n"));
505 /* get the user and primary group SIDs; although the
506 BUILTIN\Administrators SId is really the one that matters here */
508 uid_to_sid(&u_sid
, pw
->pw_uid
);
509 gid_to_sid(&g_sid
, pw
->pw_gid
);
511 token
= create_local_nt_token(NULL
, &u_sid
, False
,
512 1, &global_sid_Builtin_Administrators
);
516 static int server_info_dtor(void *p
)
518 auth_serversupplied_info
*server_info
=
519 talloc_get_type_abort(p
, auth_serversupplied_info
);
521 if (server_info
->sam_account
!= NULL
) {
522 TALLOC_FREE(server_info
->sam_account
);
525 ZERO_STRUCTP(server_info
);
529 /***************************************************************************
530 Make a server_info struct. Free with TALLOC_FREE().
531 ***************************************************************************/
533 static auth_serversupplied_info
*make_server_info(TALLOC_CTX
*mem_ctx
)
535 struct auth_serversupplied_info
*result
;
537 result
= TALLOC_ZERO_P(mem_ctx
, auth_serversupplied_info
);
538 if (result
== NULL
) {
539 DEBUG(0, ("talloc failed\n"));
543 talloc_set_destructor(result
, server_info_dtor
);
545 /* Initialise the uid and gid values to something non-zero
546 which may save us from giving away root access if there
547 is a bug in allocating these fields. */
554 /***************************************************************************
555 Make (and fill) a user_info struct from a struct samu
556 ***************************************************************************/
558 NTSTATUS
make_server_info_sam(auth_serversupplied_info
**server_info
,
559 struct samu
*sampass
)
564 auth_serversupplied_info
*result
;
567 DOM_SID unix_group_sid
;
570 if ( !(pwd
= getpwnam_alloc(NULL
, pdb_get_username(sampass
))) ) {
571 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
572 pdb_get_username(sampass
)));
573 return NT_STATUS_NO_SUCH_USER
;
576 if ( !(result
= make_server_info(NULL
)) ) {
578 return NT_STATUS_NO_MEMORY
;
581 result
->sam_account
= sampass
;
582 result
->unix_name
= talloc_strdup(result
, pwd
->pw_name
);
583 result
->gid
= pwd
->pw_gid
;
584 result
->uid
= pwd
->pw_uid
;
588 status
= pdb_enum_group_memberships(result
, sampass
,
589 &result
->sids
, &gids
,
592 if (!NT_STATUS_IS_OK(status
)) {
593 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
595 result
->sam_account
= NULL
; /* Don't free on error exit. */
600 /* Add the "Unix Group" SID for each gid to catch mapped groups
601 and their Unix equivalent. This is to solve the backwards
602 compatibility problem of 'valid users = +ntadmin' where
603 ntadmin has been paired with "Domain Admins" in the group
604 mapping table. Otherwise smb.conf would need to be changed
605 to 'valid user = "Domain Admins"'. --jerry */
607 num_gids
= result
->num_sids
;
608 for ( i
=0; i
<num_gids
; i
++ ) {
609 if ( !gid_to_unix_groups_sid( gids
[i
], &unix_group_sid
) ) {
610 DEBUG(1,("make_server_info_sam: Failed to create SID "
611 "for gid %d!\n", gids
[i
]));
614 add_sid_to_array_unique( result
, &unix_group_sid
,
615 &result
->sids
, &result
->num_sids
);
618 /* For now we throw away the gids and convert via sid_to_gid
619 * later. This needs fixing, but I'd like to get the code straight and
624 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
625 pdb_get_username(sampass
), result
->unix_name
));
627 *server_info
= result
;
633 * Add alias SIDs from memberships within the partially created token SID list
636 static NTSTATUS
add_aliases(TALLOC_CTX
*tmp_ctx
, const DOM_SID
*domain_sid
,
637 struct nt_user_token
*token
)
640 size_t i
, num_aliases
;
646 status
= pdb_enum_alias_memberships(tmp_ctx
, domain_sid
,
649 &aliases
, &num_aliases
);
651 if (!NT_STATUS_IS_OK(status
)) {
652 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
657 for (i
=0; i
<num_aliases
; i
++) {
659 sid_compose(&alias_sid
, domain_sid
, aliases
[i
]);
660 add_sid_to_array_unique(token
, &alias_sid
,
663 if (token
->user_sids
== NULL
) {
664 DEBUG(0, ("add_sid_to_array failed\n"));
665 return NT_STATUS_NO_MEMORY
;
672 static NTSTATUS
log_nt_token(TALLOC_CTX
*tmp_ctx
, NT_USER_TOKEN
*token
)
678 if ((lp_log_nt_token_command() == NULL
) ||
679 (strlen(lp_log_nt_token_command()) == 0)) {
683 group_sidstr
= talloc_strdup(tmp_ctx
, "");
684 for (i
=1; i
<token
->num_sids
; i
++) {
685 group_sidstr
= talloc_asprintf(
686 tmp_ctx
, "%s %s", group_sidstr
,
687 sid_string_static(&token
->user_sids
[i
]));
690 command
= talloc_string_sub(
691 tmp_ctx
, lp_log_nt_token_command(),
692 "%s", sid_string_static(&token
->user_sids
[0]));
693 command
= talloc_string_sub(tmp_ctx
, command
, "%t", group_sidstr
);
695 if (command
== NULL
) {
696 return NT_STATUS_NO_MEMORY
;
699 DEBUG(8, ("running command: [%s]\n", command
));
700 if (smbrun(command
, NULL
) != 0) {
701 DEBUG(0, ("Could not log NT token\n"));
702 return NT_STATUS_ACCESS_DENIED
;
708 /*******************************************************************
709 *******************************************************************/
711 static NTSTATUS
add_builtin_administrators( TALLOC_CTX
*ctx
, struct nt_user_token
*token
)
715 /* nothing to do if we aren't in a domain */
717 if ( !(IS_DC
|| lp_server_role()==ROLE_DOMAIN_MEMBER
) ) {
721 /* Find the Domain Admins SID */
724 sid_copy( &domadm
, get_global_sam_sid() );
726 if ( !secrets_fetch_domain_sid( lp_workgroup(), &domadm
) )
727 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
729 sid_append_rid( &domadm
, DOMAIN_GROUP_RID_ADMINS
);
731 /* Add Administrators if the user beloongs to Domain Admins */
733 if ( nt_token_check_sid( &domadm
, token
) ) {
734 add_sid_to_array(token
, &global_sid_Builtin_Administrators
,
735 &token
->user_sids
, &token
->num_sids
);
741 /*******************************************************************
742 *******************************************************************/
744 static NTSTATUS
create_builtin_users( void )
749 status
= pdb_create_builtin_alias( BUILTIN_ALIAS_RID_USERS
);
750 if ( !NT_STATUS_IS_OK(status
) ) {
751 DEBUG(0,("create_builtin_users: Failed to create Users\n"));
755 /* add domain users */
756 if ((IS_DC
|| (lp_server_role() == ROLE_DOMAIN_MEMBER
))
757 && secrets_fetch_domain_sid(lp_workgroup(), &dom_users
))
759 sid_append_rid(&dom_users
, DOMAIN_GROUP_RID_USERS
);
760 status
= pdb_add_aliasmem( &global_sid_Builtin_Users
, &dom_users
);
761 if ( !NT_STATUS_IS_OK(status
) ) {
762 DEBUG(0,("create_builtin_administrators: Failed to add Domain Users to"
771 /*******************************************************************
772 *******************************************************************/
774 static NTSTATUS
create_builtin_administrators( void )
777 DOM_SID dom_admins
, root_sid
;
779 enum SID_NAME_USE type
;
783 status
= pdb_create_builtin_alias( BUILTIN_ALIAS_RID_ADMINS
);
784 if ( !NT_STATUS_IS_OK(status
) ) {
785 DEBUG(0,("create_builtin_administrators: Failed to create Administrators\n"));
789 /* add domain admins */
790 if ((IS_DC
|| (lp_server_role() == ROLE_DOMAIN_MEMBER
))
791 && secrets_fetch_domain_sid(lp_workgroup(), &dom_admins
))
793 sid_append_rid(&dom_admins
, DOMAIN_GROUP_RID_ADMINS
);
794 status
= pdb_add_aliasmem( &global_sid_Builtin_Administrators
, &dom_admins
);
795 if ( !NT_STATUS_IS_OK(status
) ) {
796 DEBUG(0,("create_builtin_administrators: Failed to add Domain Admins"
797 " Administrators\n"));
803 if ( (ctx
= talloc_init("create_builtin_administrators")) == NULL
) {
804 return NT_STATUS_NO_MEMORY
;
806 fstr_sprintf( root_name
, "%s\\root", get_global_sam_name() );
807 ret
= lookup_name( ctx
, root_name
, 0, NULL
, NULL
, &root_sid
, &type
);
811 status
= pdb_add_aliasmem( &global_sid_Builtin_Administrators
, &root_sid
);
812 if ( !NT_STATUS_IS_OK(status
) ) {
813 DEBUG(0,("create_builtin_administrators: Failed to add root"
814 " Administrators\n"));
822 /*******************************************************************
823 Create a NT token for the user, expanding local aliases
824 *******************************************************************/
826 static struct nt_user_token
*create_local_nt_token(TALLOC_CTX
*mem_ctx
,
827 const DOM_SID
*user_sid
,
830 const DOM_SID
*groupsids
)
833 struct nt_user_token
*result
= NULL
;
838 tmp_ctx
= talloc_new(mem_ctx
);
839 if (tmp_ctx
== NULL
) {
840 DEBUG(0, ("talloc_new failed\n"));
844 result
= TALLOC_ZERO_P(tmp_ctx
, NT_USER_TOKEN
);
845 if (result
== NULL
) {
846 DEBUG(0, ("talloc failed\n"));
850 /* Add the user and primary group sid */
852 add_sid_to_array(result
, user_sid
,
853 &result
->user_sids
, &result
->num_sids
);
855 /* For guest, num_groupsids may be zero. */
857 add_sid_to_array(result
, &groupsids
[0],
858 &result
->user_sids
, &result
->num_sids
);
861 /* Add in BUILTIN sids */
863 add_sid_to_array(result
, &global_sid_World
,
864 &result
->user_sids
, &result
->num_sids
);
865 add_sid_to_array(result
, &global_sid_Network
,
866 &result
->user_sids
, &result
->num_sids
);
869 add_sid_to_array(result
, &global_sid_Builtin_Guests
,
870 &result
->user_sids
, &result
->num_sids
);
872 add_sid_to_array(result
, &global_sid_Authenticated_Users
,
873 &result
->user_sids
, &result
->num_sids
);
876 /* Now the SIDs we got from authentication. These are the ones from
877 * the info3 struct or from the pdb_enum_group_memberships, depending
878 * on who authenticated the user.
879 * Note that we start the for loop at "1" here, we already added the
880 * first group sid as primary above. */
882 for (i
=1; i
<num_groupsids
; i
++) {
883 add_sid_to_array_unique(result
, &groupsids
[i
],
884 &result
->user_sids
, &result
->num_sids
);
887 /* Deal with the BUILTIN\Administrators group. If the SID can
888 be resolved then assume that the add_aliasmem( S-1-5-32 )
891 if ( !sid_to_gid( &global_sid_Builtin_Administrators
, &gid
) ) {
892 /* We can only create a mapping if winbind is running
893 and the nested group functionality has been enabled */
895 if ( lp_winbind_nested_groups() && winbind_ping() ) {
897 status
= create_builtin_administrators( );
898 if ( !NT_STATUS_IS_OK(status
) ) {
899 DEBUG(2,("create_local_nt_token: Failed to create BUILTIN\\Administrators group!\n"));
900 /* don't fail, just log the message */
905 status
= add_builtin_administrators( tmp_ctx
, result
);
906 if ( !NT_STATUS_IS_OK(status
) ) {
907 /* just log a complaint but do not fail */
908 DEBUG(3,("create_local_nt_token: failed to check for local Administrators"
909 " membership (%s)\n", nt_errstr(status
)));
914 /* Deal with the BUILTIN\Users group. If the SID can
915 be resolved then assume that the add_aliasmem( S-1-5-32 )
918 if ( !sid_to_gid( &global_sid_Builtin_Users
, &gid
) ) {
919 /* We can only create a mapping if winbind is running
920 and the nested group functionality has been enabled */
922 if ( lp_winbind_nested_groups() && winbind_ping() ) {
924 status
= create_builtin_users( );
925 if ( !NT_STATUS_IS_OK(status
) ) {
926 DEBUG(2,("create_local_nt_token: Failed to create BUILTIN\\Users group!\n"));
927 /* don't fail, just log the message */
933 /* Deal with local groups */
935 if (lp_winbind_nested_groups()) {
937 /* Now add the aliases. First the one from our local SAM */
939 status
= add_aliases(tmp_ctx
, get_global_sam_sid(), result
);
941 if (!NT_STATUS_IS_OK(status
)) {
946 /* Finally the builtin ones */
948 status
= add_aliases(tmp_ctx
, &global_sid_Builtin
, result
);
950 if (!NT_STATUS_IS_OK(status
)) {
957 get_privileges_for_sids(&result
->privileges
, result
->user_sids
,
960 talloc_steal(mem_ctx
, result
);
963 TALLOC_FREE(tmp_ctx
);
968 * Create the token to use from server_info->sam_account and
969 * server_info->sids (the info3/sam groups). Find the unix gids.
972 NTSTATUS
create_local_token(auth_serversupplied_info
*server_info
)
979 mem_ctx
= talloc_new(NULL
);
980 if (mem_ctx
== NULL
) {
981 DEBUG(0, ("talloc_new failed\n"));
982 return NT_STATUS_NO_MEMORY
;
985 if (((lp_server_role() == ROLE_DOMAIN_MEMBER
) && !winbind_ping()) ||
986 (server_info
->was_mapped
)) {
987 status
= create_token_from_username(server_info
,
988 server_info
->unix_name
,
992 &server_info
->unix_name
,
996 server_info
->ptok
= create_local_nt_token(
998 pdb_get_user_sid(server_info
->sam_account
),
1000 server_info
->num_sids
, server_info
->sids
);
1001 status
= server_info
->ptok
?
1002 NT_STATUS_OK
: NT_STATUS_NO_SUCH_USER
;
1005 if (!NT_STATUS_IS_OK(status
)) {
1006 TALLOC_FREE(mem_ctx
);
1010 /* Convert the SIDs to gids. */
1012 server_info
->n_groups
= 0;
1013 server_info
->groups
= NULL
;
1015 /* Start at index 1, where the groups start. */
1017 for (i
=1; i
<server_info
->ptok
->num_sids
; i
++) {
1019 DOM_SID
*sid
= &server_info
->ptok
->user_sids
[i
];
1021 if (!sid_to_gid(sid
, &gid
)) {
1022 DEBUG(10, ("Could not convert SID %s to gid, "
1023 "ignoring it\n", sid_string_static(sid
)));
1026 add_gid_to_array_unique(server_info
, gid
, &server_info
->groups
,
1027 &server_info
->n_groups
);
1030 debug_nt_user_token(DBGC_AUTH
, 10, server_info
->ptok
);
1032 status
= log_nt_token(mem_ctx
, server_info
->ptok
);
1034 TALLOC_FREE(mem_ctx
);
1039 * Create an artificial NT token given just a username. (Initially indended
1042 * We go through lookup_name() to avoid problems we had with 'winbind use
1047 * unmapped unix users: Go directly to nss to find the user's group.
1049 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
1051 * If the user is provided by winbind, the primary gid is set to "domain
1052 * users" of the user's domain. For an explanation why this is necessary, see
1053 * the thread starting at
1054 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
1057 NTSTATUS
create_token_from_username(TALLOC_CTX
*mem_ctx
, const char *username
,
1059 uid_t
*uid
, gid_t
*gid
,
1060 char **found_username
,
1061 struct nt_user_token
**token
)
1063 NTSTATUS result
= NT_STATUS_NO_SUCH_USER
;
1064 TALLOC_CTX
*tmp_ctx
;
1066 enum SID_NAME_USE type
;
1068 DOM_SID primary_group_sid
;
1069 DOM_SID
*group_sids
;
1070 DOM_SID unix_group_sid
;
1071 size_t num_group_sids
;
1075 tmp_ctx
= talloc_new(NULL
);
1076 if (tmp_ctx
== NULL
) {
1077 DEBUG(0, ("talloc_new failed\n"));
1078 return NT_STATUS_NO_MEMORY
;
1081 if (!lookup_name_smbconf(tmp_ctx
, username
, LOOKUP_NAME_ALL
,
1082 NULL
, NULL
, &user_sid
, &type
)) {
1083 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username
));
1087 if (type
!= SID_NAME_USER
) {
1088 DEBUG(1, ("%s is a %s, not a user\n", username
,
1089 sid_type_lookup(type
)));
1093 if (!sid_to_uid(&user_sid
, uid
)) {
1094 DEBUG(1, ("sid_to_uid for %s (%s) failed\n",
1095 username
, sid_string_static(&user_sid
)));
1099 if (sid_check_is_in_our_domain(&user_sid
)) {
1101 /* This is a passdb user, so ask passdb */
1103 struct samu
*sam_acct
= NULL
;
1105 if ( !(sam_acct
= samu_new( tmp_ctx
)) ) {
1106 result
= NT_STATUS_NO_MEMORY
;
1110 if (!pdb_getsampwsid(sam_acct
, &user_sid
)) {
1111 DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n",
1112 sid_string_static(&user_sid
), username
));
1113 DEBUGADD(1, ("Fall back to unix user %s\n", username
));
1117 result
= pdb_enum_group_memberships(tmp_ctx
, sam_acct
,
1120 if (!NT_STATUS_IS_OK(result
)) {
1121 DEBUG(10, ("enum_group_memberships failed for %s\n",
1123 DEBUGADD(1, ("Fall back to unix user %s\n", username
));
1127 /* see the smb_panic() in pdb_default_enum_group_memberships */
1128 SMB_ASSERT(num_group_sids
> 0);
1131 *found_username
= talloc_strdup(mem_ctx
,
1132 pdb_get_username(sam_acct
));
1134 } else if (sid_check_is_in_unix_users(&user_sid
)) {
1136 /* This is a unix user not in passdb. We need to ask nss
1137 * directly, without consulting passdb */
1139 struct passwd
*pass
;
1142 * This goto target is used as a fallback for the passdb
1143 * case. The concrete bug report is when passdb gave us an
1149 uid_to_unix_users_sid(*uid
, &user_sid
);
1151 pass
= getpwuid_alloc(tmp_ctx
, *uid
);
1153 DEBUG(1, ("getpwuid(%d) for user %s failed\n",
1158 if (!getgroups_unix_user(tmp_ctx
, username
, pass
->pw_gid
,
1159 &gids
, &num_group_sids
)) {
1160 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
1165 group_sids
= talloc_array(tmp_ctx
, DOM_SID
, num_group_sids
);
1166 if (group_sids
== NULL
) {
1167 DEBUG(1, ("talloc_array failed\n"));
1168 result
= NT_STATUS_NO_MEMORY
;
1172 for (i
=0; i
<num_group_sids
; i
++) {
1173 gid_to_sid(&group_sids
[i
], gids
[i
]);
1176 /* In getgroups_unix_user we always set the primary gid */
1177 SMB_ASSERT(num_group_sids
> 0);
1180 *found_username
= talloc_strdup(mem_ctx
, pass
->pw_name
);
1184 /* This user is from winbind, force the primary gid to the
1185 * user's "domain users" group. Under certain circumstances
1186 * (user comes from NT4), this might be a loss of
1187 * information. But we can not rely on winbind getting the
1188 * correct info. AD might prohibit winbind looking up that
1193 sid_copy(&primary_group_sid
, &user_sid
);
1194 sid_split_rid(&primary_group_sid
, &dummy
);
1195 sid_append_rid(&primary_group_sid
, DOMAIN_GROUP_RID_USERS
);
1197 if (!sid_to_gid(&primary_group_sid
, gid
)) {
1198 DEBUG(1, ("sid_to_gid(%s) failed\n",
1199 sid_string_static(&primary_group_sid
)));
1204 group_sids
= &primary_group_sid
;
1207 *found_username
= talloc_strdup(mem_ctx
, username
);
1210 /* Add the "Unix Group" SID for each gid to catch mapped groups
1211 and their Unix equivalent. This is to solve the backwards
1212 compatibility problem of 'valid users = +ntadmin' where
1213 ntadmin has been paired with "Domain Admins" in the group
1214 mapping table. Otherwise smb.conf would need to be changed
1215 to 'valid user = "Domain Admins"'. --jerry */
1217 num_gids
= num_group_sids
;
1218 for ( i
=0; i
<num_gids
; i
++ ) {
1221 /* don't pickup anything managed by Winbind */
1223 if ( lp_idmap_gid(&low
, &high
) && (gids
[i
] >= low
) && (gids
[i
] <= high
) )
1226 if ( !gid_to_unix_groups_sid( gids
[i
], &unix_group_sid
) ) {
1227 DEBUG(1,("create_token_from_username: Failed to create SID "
1228 "for gid %d!\n", gids
[i
]));
1231 add_sid_to_array_unique( mem_ctx
, &unix_group_sid
,
1232 &group_sids
, &num_group_sids
);
1235 *token
= create_local_nt_token(mem_ctx
, &user_sid
,
1236 is_guest
, num_group_sids
, group_sids
);
1238 if ((*token
== NULL
) || (*found_username
== NULL
)) {
1239 result
= NT_STATUS_NO_MEMORY
;
1243 result
= NT_STATUS_OK
;
1245 TALLOC_FREE(tmp_ctx
);
1249 /***************************************************************************
1250 Build upon create_token_from_username:
1252 Expensive helper function to figure out whether a user given its name is
1253 member of a particular group.
1254 ***************************************************************************/
1255 BOOL
user_in_group_sid(const char *username
, const DOM_SID
*group_sid
)
1260 char *found_username
;
1261 struct nt_user_token
*token
;
1264 TALLOC_CTX
*mem_ctx
;
1266 mem_ctx
= talloc_new(NULL
);
1267 if (mem_ctx
== NULL
) {
1268 DEBUG(0, ("talloc_new failed\n"));
1272 status
= create_token_from_username(mem_ctx
, username
, False
,
1273 &uid
, &gid
, &found_username
,
1276 if (!NT_STATUS_IS_OK(status
)) {
1277 DEBUG(10, ("could not create token for %s\n", username
));
1281 result
= nt_token_check_sid(group_sid
, token
);
1283 TALLOC_FREE(mem_ctx
);
1288 BOOL
user_in_group(const char *username
, const char *groupname
)
1290 TALLOC_CTX
*mem_ctx
;
1294 mem_ctx
= talloc_new(NULL
);
1295 if (mem_ctx
== NULL
) {
1296 DEBUG(0, ("talloc_new failed\n"));
1300 ret
= lookup_name(mem_ctx
, groupname
, LOOKUP_NAME_ALL
,
1301 NULL
, NULL
, &group_sid
, NULL
);
1302 TALLOC_FREE(mem_ctx
);
1305 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname
));
1309 return user_in_group_sid(username
, &group_sid
);
1313 /***************************************************************************
1314 Make (and fill) a user_info struct from a 'struct passwd' by conversion
1316 ***************************************************************************/
1318 NTSTATUS
make_server_info_pw(auth_serversupplied_info
**server_info
,
1319 char *unix_username
,
1323 struct samu
*sampass
= NULL
;
1325 auth_serversupplied_info
*result
;
1327 if ( !(sampass
= samu_new( NULL
)) ) {
1328 return NT_STATUS_NO_MEMORY
;
1331 status
= samu_set_unix( sampass
, pwd
);
1332 if (!NT_STATUS_IS_OK(status
)) {
1336 result
= make_server_info(NULL
);
1337 if (result
== NULL
) {
1338 TALLOC_FREE(sampass
);
1339 return NT_STATUS_NO_MEMORY
;
1342 result
->sam_account
= sampass
;
1343 result
->unix_name
= talloc_strdup(result
, unix_username
);
1344 result
->uid
= pwd
->pw_uid
;
1345 result
->gid
= pwd
->pw_gid
;
1347 status
= pdb_enum_group_memberships(result
, sampass
,
1348 &result
->sids
, &gids
,
1351 if (!NT_STATUS_IS_OK(status
)) {
1352 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
1353 nt_errstr(status
)));
1354 TALLOC_FREE(result
);
1358 /* For now we throw away the gids and convert via sid_to_gid
1359 * later. This needs fixing, but I'd like to get the code straight and
1363 *server_info
= result
;
1365 return NT_STATUS_OK
;
1368 /***************************************************************************
1369 Make (and fill) a user_info struct for a guest login.
1370 This *must* succeed for smbd to start. If there is no mapping entry for
1371 the guest gid, then create one.
1372 ***************************************************************************/
1374 static NTSTATUS
make_new_server_info_guest(auth_serversupplied_info
**server_info
)
1377 struct samu
*sampass
= NULL
;
1380 static const char zeros
[16];
1382 if ( !(sampass
= samu_new( NULL
)) ) {
1383 return NT_STATUS_NO_MEMORY
;
1386 sid_copy(&guest_sid
, get_global_sam_sid());
1387 sid_append_rid(&guest_sid
, DOMAIN_USER_RID_GUEST
);
1390 ret
= pdb_getsampwsid(sampass
, &guest_sid
);
1394 TALLOC_FREE(sampass
);
1395 return NT_STATUS_NO_SUCH_USER
;
1398 status
= make_server_info_sam(server_info
, sampass
);
1399 if (!NT_STATUS_IS_OK(status
)) {
1400 TALLOC_FREE(sampass
);
1404 (*server_info
)->guest
= True
;
1406 status
= create_local_token(*server_info
);
1407 if (!NT_STATUS_IS_OK(status
)) {
1408 DEBUG(10, ("create_local_token failed: %s\n",
1409 nt_errstr(status
)));
1413 /* annoying, but the Guest really does have a session key, and it is
1415 (*server_info
)->user_session_key
= data_blob(zeros
, sizeof(zeros
));
1416 (*server_info
)->lm_session_key
= data_blob(zeros
, sizeof(zeros
));
1418 return NT_STATUS_OK
;
1421 static auth_serversupplied_info
*copy_serverinfo(auth_serversupplied_info
*src
)
1423 auth_serversupplied_info
*dst
;
1425 dst
= make_server_info(NULL
);
1430 dst
->guest
= src
->guest
;
1431 dst
->uid
= src
->uid
;
1432 dst
->gid
= src
->gid
;
1433 dst
->n_groups
= src
->n_groups
;
1434 if (src
->n_groups
!= 0) {
1435 dst
->groups
= talloc_memdup(dst
, src
->groups
,
1436 sizeof(gid_t
)*dst
->n_groups
);
1442 dst
->ptok
= dup_nt_token(dst
, src
->ptok
);
1449 dst
->user_session_key
= data_blob_talloc( dst
, src
->user_session_key
.data
,
1450 src
->user_session_key
.length
);
1452 dst
->lm_session_key
= data_blob_talloc(dst
, src
->lm_session_key
.data
,
1453 src
->lm_session_key
.length
);
1455 dst
->sam_account
= samu_new(NULL
);
1456 if (!dst
->sam_account
) {
1461 if (!pdb_copy_sam_account(dst
->sam_account
, src
->sam_account
)) {
1466 dst
->pam_handle
= NULL
;
1467 dst
->unix_name
= talloc_strdup(dst
, src
->unix_name
);
1468 if (!dst
->unix_name
) {
1476 static auth_serversupplied_info
*guest_info
= NULL
;
1478 BOOL
init_guest_info(void)
1480 if (guest_info
!= NULL
)
1483 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info
));
1486 NTSTATUS
make_server_info_guest(auth_serversupplied_info
**server_info
)
1488 *server_info
= copy_serverinfo(guest_info
);
1489 return (*server_info
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
1492 /***************************************************************************
1493 Purely internal function for make_server_info_info3
1494 Fill the sam account from getpwnam
1495 ***************************************************************************/
1496 static NTSTATUS
fill_sam_account(TALLOC_CTX
*mem_ctx
,
1498 const char *username
,
1499 char **found_username
,
1500 uid_t
*uid
, gid_t
*gid
,
1501 struct samu
*account
,
1502 BOOL
*username_was_mapped
)
1505 fstring dom_user
, lower_username
;
1506 fstring real_username
;
1507 struct passwd
*passwd
;
1509 fstrcpy( lower_username
, username
);
1510 strlower_m( lower_username
);
1512 fstr_sprintf(dom_user
, "%s%c%s", domain
, *lp_winbind_separator(),
1515 /* Get the passwd struct. Try to create the account is necessary. */
1517 *username_was_mapped
= map_username( dom_user
);
1519 if ( !(passwd
= smb_getpwnam( NULL
, dom_user
, real_username
, True
)) )
1520 return NT_STATUS_NO_SUCH_USER
;
1522 *uid
= passwd
->pw_uid
;
1523 *gid
= passwd
->pw_gid
;
1525 /* This is pointless -- there is no suport for differing
1526 unix and windows names. Make sure to always store the
1527 one we actually looked up and succeeded. Have I mentioned
1528 why I hate the 'winbind use default domain' parameter?
1531 *found_username
= talloc_strdup( mem_ctx
, real_username
);
1533 DEBUG(5,("fill_sam_account: located username was [%s]\n", *found_username
));
1535 nt_status
= samu_set_unix( account
, passwd
);
1537 TALLOC_FREE(passwd
);
1542 /****************************************************************************
1543 Wrapper to allow the getpwnam() call to strip the domain name and
1544 try again in case a local UNIX user is already there. Also run through
1545 the username if we fallback to the username only.
1546 ****************************************************************************/
1548 struct passwd
*smb_getpwnam( TALLOC_CTX
*mem_ctx
, char *domuser
,
1549 fstring save_username
, BOOL create
)
1551 struct passwd
*pw
= NULL
;
1555 /* we only save a copy of the username it has been mangled
1556 by winbindd use default domain */
1558 save_username
[0] = '\0';
1560 /* don't call map_username() here since it has to be done higher
1561 up the stack so we don't call it mutliple times */
1563 fstrcpy( username
, domuser
);
1565 p
= strchr_m( username
, *lp_winbind_separator() );
1567 /* code for a DOMAIN\user string */
1570 fstring strip_username
;
1572 pw
= Get_Pwnam_alloc( mem_ctx
, domuser
);
1574 /* make sure we get the case of the username correct */
1575 /* work around 'winbind use default domain = yes' */
1577 if ( !strchr_m( pw
->pw_name
, *lp_winbind_separator() ) ) {
1580 /* split the domain and username into 2 strings */
1584 fstr_sprintf(save_username
, "%s%c%s", domain
, *lp_winbind_separator(), pw
->pw_name
);
1587 fstrcpy( save_username
, pw
->pw_name
);
1593 /* setup for lookup of just the username */
1594 /* remember that p and username are overlapping memory */
1597 fstrcpy( strip_username
, p
);
1598 fstrcpy( username
, strip_username
);
1601 /* just lookup a plain username */
1603 pw
= Get_Pwnam_alloc(mem_ctx
, username
);
1605 /* Create local user if requested but only if winbindd
1606 is not running. We need to protect against cases
1607 where winbindd is failing and then prematurely
1608 creating users in /etc/passwd */
1610 if ( !pw
&& create
&& !winbind_ping() ) {
1611 /* Don't add a machine account. */
1612 if (username
[strlen(username
)-1] == '$')
1615 smb_create_user(NULL
, username
, NULL
);
1616 pw
= Get_Pwnam_alloc(mem_ctx
, username
);
1619 /* one last check for a valid passwd struct */
1622 fstrcpy( save_username
, pw
->pw_name
);
1627 /***************************************************************************
1628 Make a server_info struct from the info3 returned by a domain logon
1629 ***************************************************************************/
1631 NTSTATUS
make_server_info_info3(TALLOC_CTX
*mem_ctx
,
1632 const char *sent_nt_username
,
1634 auth_serversupplied_info
**server_info
,
1635 NET_USER_INFO_3
*info3
)
1637 static const char zeros
[16];
1639 NTSTATUS nt_status
= NT_STATUS_OK
;
1640 char *found_username
;
1641 const char *nt_domain
;
1642 const char *nt_username
;
1643 struct samu
*sam_account
= NULL
;
1646 BOOL username_was_mapped
;
1653 auth_serversupplied_info
*result
;
1656 Here is where we should check the list of
1657 trusted domains, and verify that the SID
1661 sid_copy(&user_sid
, &info3
->dom_sid
.sid
);
1662 if (!sid_append_rid(&user_sid
, info3
->user_rid
)) {
1663 return NT_STATUS_INVALID_PARAMETER
;
1666 sid_copy(&group_sid
, &info3
->dom_sid
.sid
);
1667 if (!sid_append_rid(&group_sid
, info3
->group_rid
)) {
1668 return NT_STATUS_INVALID_PARAMETER
;
1671 if (!(nt_username
= unistr2_tdup(mem_ctx
, &(info3
->uni_user_name
)))) {
1672 /* If the server didn't give us one, just use the one we sent
1674 nt_username
= sent_nt_username
;
1677 if (!(nt_domain
= unistr2_tdup(mem_ctx
, &(info3
->uni_logon_dom
)))) {
1678 /* If the server didn't give us one, just use the one we sent
1683 /* try to fill the SAM account.. If getpwnam() fails, then try the
1684 add user script (2.2.x behavior).
1686 We use the _unmapped_ username here in an attempt to provide
1687 consistent username mapping behavior between kerberos and NTLM[SSP]
1688 authentication in domain mode security. I.E. Username mapping
1689 should be applied to the fully qualified username
1690 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1691 called map_username() unnecessarily in make_user_info_map() but
1692 that is how the current code is designed. Making the change here
1693 is the least disruptive place. -- jerry */
1695 if ( !(sam_account
= samu_new( NULL
)) ) {
1696 return NT_STATUS_NO_MEMORY
;
1699 /* this call will try to create the user if necessary */
1701 nt_status
= fill_sam_account(mem_ctx
, nt_domain
, sent_nt_username
,
1702 &found_username
, &uid
, &gid
, sam_account
,
1703 &username_was_mapped
);
1706 /* if we still don't have a valid unix account check for
1707 'map to guest = bad uid' */
1709 if (!NT_STATUS_IS_OK(nt_status
)) {
1710 TALLOC_FREE( sam_account
);
1711 if ( lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID
) {
1712 make_server_info_guest(server_info
);
1713 return NT_STATUS_OK
;
1718 if (!pdb_set_nt_username(sam_account
, nt_username
, PDB_CHANGED
)) {
1719 TALLOC_FREE(sam_account
);
1720 return NT_STATUS_NO_MEMORY
;
1723 if (!pdb_set_username(sam_account
, nt_username
, PDB_CHANGED
)) {
1724 TALLOC_FREE(sam_account
);
1725 return NT_STATUS_NO_MEMORY
;
1728 if (!pdb_set_domain(sam_account
, nt_domain
, PDB_CHANGED
)) {
1729 TALLOC_FREE(sam_account
);
1730 return NT_STATUS_NO_MEMORY
;
1733 if (!pdb_set_user_sid(sam_account
, &user_sid
, PDB_CHANGED
)) {
1734 TALLOC_FREE(sam_account
);
1735 return NT_STATUS_UNSUCCESSFUL
;
1738 if (!pdb_set_group_sid(sam_account
, &group_sid
, PDB_CHANGED
)) {
1739 TALLOC_FREE(sam_account
);
1740 return NT_STATUS_UNSUCCESSFUL
;
1743 if (!pdb_set_fullname(sam_account
,
1744 unistr2_static(&(info3
->uni_full_name
)),
1746 TALLOC_FREE(sam_account
);
1747 return NT_STATUS_NO_MEMORY
;
1750 if (!pdb_set_logon_script(sam_account
,
1751 unistr2_static(&(info3
->uni_logon_script
)),
1753 TALLOC_FREE(sam_account
);
1754 return NT_STATUS_NO_MEMORY
;
1757 if (!pdb_set_profile_path(sam_account
,
1758 unistr2_static(&(info3
->uni_profile_path
)),
1760 TALLOC_FREE(sam_account
);
1761 return NT_STATUS_NO_MEMORY
;
1764 if (!pdb_set_homedir(sam_account
,
1765 unistr2_static(&(info3
->uni_home_dir
)),
1767 TALLOC_FREE(sam_account
);
1768 return NT_STATUS_NO_MEMORY
;
1771 if (!pdb_set_dir_drive(sam_account
,
1772 unistr2_static(&(info3
->uni_dir_drive
)),
1774 TALLOC_FREE(sam_account
);
1775 return NT_STATUS_NO_MEMORY
;
1778 if (!pdb_set_acct_ctrl(sam_account
, info3
->acct_flags
, PDB_CHANGED
)) {
1779 TALLOC_FREE(sam_account
);
1780 return NT_STATUS_NO_MEMORY
;
1783 result
= make_server_info(NULL
);
1784 if (result
== NULL
) {
1785 DEBUG(4, ("make_server_info failed!\n"));
1786 TALLOC_FREE(sam_account
);
1787 return NT_STATUS_NO_MEMORY
;
1790 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1791 valid struct samu) */
1793 result
->sam_account
= sam_account
;
1794 result
->unix_name
= talloc_strdup(result
, found_username
);
1796 /* Fill in the unix info we found on the way */
1801 /* Create a 'combined' list of all SIDs we might want in the SD */
1803 result
->num_sids
= 0;
1804 result
->sids
= NULL
;
1806 /* and create (by appending rids) the 'domain' sids */
1808 for (i
= 0; i
< info3
->num_groups2
; i
++) {
1810 if (!sid_compose(&sid
, &info3
->dom_sid
.sid
,
1811 info3
->gids
[i
].g_rid
)) {
1812 DEBUG(3,("could not append additional group rid "
1813 "0x%x\n", info3
->gids
[i
].g_rid
));
1814 TALLOC_FREE(result
);
1815 return NT_STATUS_INVALID_PARAMETER
;
1817 add_sid_to_array(result
, &sid
, &result
->sids
,
1821 /* Copy 'other' sids. We need to do sid filtering here to
1822 prevent possible elevation of privileges. See:
1824 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1827 for (i
= 0; i
< info3
->num_other_sids
; i
++) {
1828 add_sid_to_array(result
, &info3
->other_sids
[i
].sid
,
1833 result
->login_server
= unistr2_tdup(result
,
1834 &(info3
->uni_logon_srv
));
1836 /* ensure we are never given NULL session keys */
1838 if (memcmp(info3
->user_sess_key
, zeros
, sizeof(zeros
)) == 0) {
1839 result
->user_session_key
= data_blob(NULL
, 0);
1841 result
->user_session_key
= data_blob_talloc(
1842 result
, info3
->user_sess_key
,
1843 sizeof(info3
->user_sess_key
));
1846 if (memcmp(info3
->lm_sess_key
, zeros
, 8) == 0) {
1847 result
->lm_session_key
= data_blob(NULL
, 0);
1849 result
->lm_session_key
= data_blob_talloc(
1850 result
, info3
->lm_sess_key
,
1851 sizeof(info3
->lm_sess_key
));
1854 result
->was_mapped
= username_was_mapped
;
1856 *server_info
= result
;
1858 return NT_STATUS_OK
;
1861 /***************************************************************************
1862 Free a user_info struct
1863 ***************************************************************************/
1865 void free_user_info(auth_usersupplied_info
**user_info
)
1867 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1868 if (*user_info
!= NULL
) {
1869 if ((*user_info
)->smb_name
) {
1870 DEBUG(10,("structure was created for %s\n",
1871 (*user_info
)->smb_name
));
1873 SAFE_FREE((*user_info
)->smb_name
);
1874 SAFE_FREE((*user_info
)->internal_username
);
1875 SAFE_FREE((*user_info
)->client_domain
);
1876 SAFE_FREE((*user_info
)->domain
);
1877 SAFE_FREE((*user_info
)->wksta_name
);
1878 data_blob_free(&(*user_info
)->lm_resp
);
1879 data_blob_free(&(*user_info
)->nt_resp
);
1880 data_blob_clear_free(&(*user_info
)->lm_interactive_pwd
);
1881 data_blob_clear_free(&(*user_info
)->nt_interactive_pwd
);
1882 data_blob_clear_free(&(*user_info
)->plaintext_password
);
1883 ZERO_STRUCT(**user_info
);
1885 SAFE_FREE(*user_info
);
1888 /***************************************************************************
1889 Make an auth_methods struct
1890 ***************************************************************************/
1892 BOOL
make_auth_methods(struct auth_context
*auth_context
, auth_methods
**auth_method
)
1894 if (!auth_context
) {
1895 smb_panic("no auth_context supplied to "
1896 "make_auth_methods()!\n");
1900 smb_panic("make_auth_methods: pointer to auth_method pointer "
1904 *auth_method
= TALLOC_P(auth_context
->mem_ctx
, auth_methods
);
1905 if (!*auth_method
) {
1906 DEBUG(0,("make_auth_method: malloc failed!\n"));
1909 ZERO_STRUCTP(*auth_method
);
1914 /****************************************************************************
1915 Duplicate a SID token.
1916 ****************************************************************************/
1918 NT_USER_TOKEN
*dup_nt_token(TALLOC_CTX
*mem_ctx
, NT_USER_TOKEN
*ptoken
)
1920 NT_USER_TOKEN
*token
;
1925 token
= TALLOC_P(mem_ctx
, NT_USER_TOKEN
);
1926 if (token
== NULL
) {
1927 DEBUG(0, ("talloc failed\n"));
1931 ZERO_STRUCTP(token
);
1933 if (ptoken
->user_sids
&& ptoken
->num_sids
) {
1934 token
->user_sids
= talloc_memdup(token
, ptoken
->user_sids
,
1935 sizeof(DOM_SID
) * ptoken
->num_sids
);
1937 if (token
->user_sids
== NULL
) {
1938 DEBUG(0, ("talloc_memdup failed\n"));
1942 token
->num_sids
= ptoken
->num_sids
;
1945 /* copy the privileges; don't consider failure to be critical here */
1947 if ( !se_priv_copy( &token
->privileges
, &ptoken
->privileges
) ) {
1948 DEBUG(0,("dup_nt_token: Failure to copy SE_PRIV!. "
1949 "Continuing with 0 privileges assigned.\n"));
1955 /****************************************************************************
1956 Check for a SID in an NT_USER_TOKEN
1957 ****************************************************************************/
1959 BOOL
nt_token_check_sid ( const DOM_SID
*sid
, const NT_USER_TOKEN
*token
)
1963 if ( !sid
|| !token
)
1966 for ( i
=0; i
<token
->num_sids
; i
++ ) {
1967 if ( sid_equal( sid
, &token
->user_sids
[i
] ) )
1974 BOOL
nt_token_check_domain_rid( NT_USER_TOKEN
*token
, uint32 rid
)
1978 /* if we are a domain member, the get the domain SID, else for
1979 a DC or standalone server, use our own SID */
1981 if ( lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1982 if ( !secrets_fetch_domain_sid( lp_workgroup(),
1984 DEBUG(1,("nt_token_check_domain_rid: Cannot lookup "
1985 "SID for domain [%s]\n", lp_workgroup()));
1990 sid_copy( &domain_sid
, get_global_sam_sid() );
1992 sid_append_rid( &domain_sid
, rid
);
1994 return nt_token_check_sid( &domain_sid
, token
);\
1998 * Verify whether or not given domain is trusted.
2000 * @param domain_name name of the domain to be verified
2001 * @return true if domain is one of the trusted once or
2002 * false if otherwise
2005 BOOL
is_trusted_domain(const char* dom_name
)
2007 DOM_SID trustdom_sid
;
2010 /* no trusted domains for a standalone server */
2012 if ( lp_server_role() == ROLE_STANDALONE
)
2015 /* if we are a DC, then check for a direct trust relationships */
2019 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
2020 "[%s]\n", dom_name
));
2021 ret
= secrets_fetch_trusted_domain_password(dom_name
, NULL
,
2030 /* If winbind is around, ask it */
2032 result
= wb_is_trusted_domain(dom_name
);
2034 if (result
== NSS_STATUS_SUCCESS
) {
2038 if (result
== NSS_STATUS_NOTFOUND
) {
2039 /* winbind could not find the domain */
2043 /* The only other possible result is that winbind is not up
2044 and running. We need to update the trustdom_cache
2047 update_trustdom_cache();
2050 /* now the trustdom cache should be available a DC could still
2051 * have a transitive trust so fall back to the cache of trusted
2052 * domains (like a domain member would use */
2054 if ( trustdom_cache_fetch(dom_name
, &trustdom_sid
) ) {