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 /****************************************************************************
31 Create a UNIX user on demand.
32 ****************************************************************************/
34 static int smb_create_user(const char *domain, const char *unix_username, const char *homedir)
39 pstrcpy(add_script, lp_adduser_script());
42 all_string_sub(add_script, "%u", unix_username, sizeof(pstring));
44 all_string_sub(add_script, "%D", domain, sizeof(pstring));
46 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
47 ret = smbrun(add_script,NULL);
49 DEBUG(ret ? 0 : 3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
53 /****************************************************************************
54 Create an auth_usersupplied_data structure
55 ****************************************************************************/
57 static NTSTATUS make_user_info(auth_usersupplied_info **user_info,
59 const char *internal_username,
60 const char *client_domain,
62 const char *wksta_name,
63 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
64 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
69 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
71 *user_info = SMB_MALLOC_P(auth_usersupplied_info);
72 if (*user_info == NULL) {
73 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
74 return NT_STATUS_NO_MEMORY;
77 ZERO_STRUCTP(*user_info);
79 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
81 (*user_info)->smb_name = SMB_STRDUP(smb_name);
82 if ((*user_info)->smb_name == NULL) {
83 free_user_info(user_info);
84 return NT_STATUS_NO_MEMORY;
87 (*user_info)->internal_username = SMB_STRDUP(internal_username);
88 if ((*user_info)->internal_username == NULL) {
89 free_user_info(user_info);
90 return NT_STATUS_NO_MEMORY;
93 (*user_info)->domain = SMB_STRDUP(domain);
94 if ((*user_info)->domain == NULL) {
95 free_user_info(user_info);
96 return NT_STATUS_NO_MEMORY;
99 (*user_info)->client_domain = SMB_STRDUP(client_domain);
100 if ((*user_info)->client_domain == NULL) {
101 free_user_info(user_info);
102 return NT_STATUS_NO_MEMORY;
105 (*user_info)->wksta_name = SMB_STRDUP(wksta_name);
106 if ((*user_info)->wksta_name == NULL) {
107 free_user_info(user_info);
108 return NT_STATUS_NO_MEMORY;
111 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
114 (*user_info)->lm_resp = data_blob(lm_pwd->data, lm_pwd->length);
116 (*user_info)->nt_resp = data_blob(nt_pwd->data, nt_pwd->length);
117 if (lm_interactive_pwd)
118 (*user_info)->lm_interactive_pwd = data_blob(lm_interactive_pwd->data, lm_interactive_pwd->length);
119 if (nt_interactive_pwd)
120 (*user_info)->nt_interactive_pwd = data_blob(nt_interactive_pwd->data, nt_interactive_pwd->length);
123 (*user_info)->plaintext_password = data_blob(plaintext->data, plaintext->length);
125 (*user_info)->encrypted = encrypted;
127 (*user_info)->logon_parameters = 0;
129 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
134 /****************************************************************************
135 Create an auth_usersupplied_data structure after appropriate mapping.
136 ****************************************************************************/
138 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info,
139 const char *smb_name,
140 const char *client_domain,
141 const char *wksta_name,
142 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
143 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
144 DATA_BLOB *plaintext,
150 fstring internal_username;
151 fstrcpy(internal_username, smb_name);
152 was_mapped = map_username(internal_username);
154 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
155 client_domain, smb_name, wksta_name));
157 /* don't allow "" as a domain, fixes a Win9X bug
158 where it doens't supply a domain for logon script
159 'net use' commands. */
161 if ( *client_domain )
162 domain = client_domain;
164 domain = lp_workgroup();
166 /* do what win2k does. Always map unknown domains to our own
167 and let the "passdb backend" handle unknown users. */
169 if ( !is_trusted_domain(domain) && !strequal(domain, get_global_sam_name()) )
170 domain = my_sam_name();
172 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
174 result = make_user_info(user_info, smb_name, internal_username,
175 client_domain, domain, wksta_name,
177 lm_interactive_pwd, nt_interactive_pwd,
178 plaintext, encrypted);
179 if (NT_STATUS_IS_OK(result)) {
180 (*user_info)->was_mapped = was_mapped;
185 /****************************************************************************
186 Create an auth_usersupplied_data, making the DATA_BLOBs here.
187 Decrypt and encrypt the passwords.
188 ****************************************************************************/
190 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
191 const char *smb_name,
192 const char *client_domain,
193 const char *wksta_name,
194 uint32 logon_parameters,
195 const uchar *lm_network_pwd,
197 const uchar *nt_network_pwd,
202 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
203 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
205 status = make_user_info_map(user_info,
206 smb_name, client_domain,
208 lm_pwd_len ? &lm_blob : NULL,
209 nt_pwd_len ? &nt_blob : NULL,
213 if (NT_STATUS_IS_OK(status)) {
214 (*user_info)->logon_parameters = logon_parameters;
216 ret = NT_STATUS_IS_OK(status) ? True : False;
218 data_blob_free(&lm_blob);
219 data_blob_free(&nt_blob);
223 /****************************************************************************
224 Create an auth_usersupplied_data, making the DATA_BLOBs here.
225 Decrypt and encrypt the passwords.
226 ****************************************************************************/
228 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
229 const char *smb_name,
230 const char *client_domain,
231 const char *wksta_name,
232 uint32 logon_parameters,
234 const uchar lm_interactive_pwd[16],
235 const uchar nt_interactive_pwd[16],
236 const uchar *dc_sess_key)
238 unsigned char lm_pwd[16];
239 unsigned char nt_pwd[16];
240 unsigned char local_lm_response[24];
241 unsigned char local_nt_response[24];
242 unsigned char key[16];
245 memcpy(key, dc_sess_key, 8);
247 if (lm_interactive_pwd)
248 memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
250 if (nt_interactive_pwd)
251 memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
253 #ifdef DEBUG_PASSWORD
255 dump_data(100, key, sizeof(key));
257 DEBUG(100,("lm owf password:"));
258 dump_data(100, lm_pwd, sizeof(lm_pwd));
260 DEBUG(100,("nt owf password:"));
261 dump_data(100, nt_pwd, sizeof(nt_pwd));
264 if (lm_interactive_pwd)
265 SamOEMhash(lm_pwd, key, sizeof(lm_pwd));
267 if (nt_interactive_pwd)
268 SamOEMhash(nt_pwd, key, sizeof(nt_pwd));
270 #ifdef DEBUG_PASSWORD
271 DEBUG(100,("decrypt of lm owf password:"));
272 dump_data(100, lm_pwd, sizeof(lm_pwd));
274 DEBUG(100,("decrypt of nt owf password:"));
275 dump_data(100, nt_pwd, sizeof(nt_pwd));
278 if (lm_interactive_pwd)
279 SMBOWFencrypt(lm_pwd, chal,
282 if (nt_interactive_pwd)
283 SMBOWFencrypt(nt_pwd, chal,
286 /* Password info paranoia */
292 DATA_BLOB local_lm_blob;
293 DATA_BLOB local_nt_blob;
295 DATA_BLOB lm_interactive_blob;
296 DATA_BLOB nt_interactive_blob;
298 if (lm_interactive_pwd) {
299 local_lm_blob = data_blob(local_lm_response,
300 sizeof(local_lm_response));
301 lm_interactive_blob = data_blob(lm_pwd,
306 if (nt_interactive_pwd) {
307 local_nt_blob = data_blob(local_nt_response,
308 sizeof(local_nt_response));
309 nt_interactive_blob = data_blob(nt_pwd,
314 nt_status = make_user_info_map(
316 smb_name, client_domain, wksta_name,
317 lm_interactive_pwd ? &local_lm_blob : NULL,
318 nt_interactive_pwd ? &local_nt_blob : NULL,
319 lm_interactive_pwd ? &lm_interactive_blob : NULL,
320 nt_interactive_pwd ? &nt_interactive_blob : NULL,
323 if (NT_STATUS_IS_OK(nt_status)) {
324 (*user_info)->logon_parameters = logon_parameters;
327 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
328 data_blob_free(&local_lm_blob);
329 data_blob_free(&local_nt_blob);
330 data_blob_free(&lm_interactive_blob);
331 data_blob_free(&nt_interactive_blob);
337 /****************************************************************************
338 Create an auth_usersupplied_data structure
339 ****************************************************************************/
341 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
342 const char *smb_name,
343 const char *client_domain,
345 DATA_BLOB plaintext_password)
348 DATA_BLOB local_lm_blob;
349 DATA_BLOB local_nt_blob;
350 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
353 * Not encrypted - do so.
356 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
359 if (plaintext_password.data) {
360 unsigned char local_lm_response[24];
362 #ifdef DEBUG_PASSWORD
363 DEBUG(10,("Unencrypted password (len %d):\n",
364 (int)plaintext_password.length));
365 dump_data(100, plaintext_password.data,
366 plaintext_password.length);
369 SMBencrypt( (const char *)plaintext_password.data,
370 (const uchar*)chal, local_lm_response);
371 local_lm_blob = data_blob(local_lm_response, 24);
373 /* We can't do an NT hash here, as the password needs to be
375 local_nt_blob = data_blob(NULL, 0);
378 local_lm_blob = data_blob(NULL, 0);
379 local_nt_blob = data_blob(NULL, 0);
382 ret = make_user_info_map(
383 user_info, smb_name, client_domain,
384 get_remote_machine_name(),
385 local_lm_blob.data ? &local_lm_blob : NULL,
386 local_nt_blob.data ? &local_nt_blob : NULL,
388 plaintext_password.data ? &plaintext_password : NULL,
391 data_blob_free(&local_lm_blob);
392 return NT_STATUS_IS_OK(ret) ? True : False;
395 /****************************************************************************
396 Create an auth_usersupplied_data structure
397 ****************************************************************************/
399 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
400 const char *smb_name,
401 const char *client_domain,
402 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
404 return make_user_info_map(user_info, smb_name,
406 get_remote_machine_name(),
407 lm_resp.data ? &lm_resp : NULL,
408 nt_resp.data ? &nt_resp : NULL,
413 /****************************************************************************
414 Create a guest user_info blob, for anonymous authenticaion.
415 ****************************************************************************/
417 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
421 nt_status = make_user_info(user_info,
430 return NT_STATUS_IS_OK(nt_status) ? True : False;
433 /****************************************************************************
434 prints a NT_USER_TOKEN to debug output.
435 ****************************************************************************/
437 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
442 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
446 DEBUGC(dbg_class, dbg_lev,
447 ("NT user token of user %s\n",
448 sid_string_static(&token->user_sids[0]) ));
449 DEBUGADDC(dbg_class, dbg_lev,
450 ("contains %lu SIDs\n", (unsigned long)token->num_sids));
451 for (i = 0; i < token->num_sids; i++)
452 DEBUGADDC(dbg_class, dbg_lev,
453 ("SID[%3lu]: %s\n", (unsigned long)i,
454 sid_string_static(&token->user_sids[i])));
456 dump_se_priv( dbg_class, dbg_lev, &token->privileges );
459 /****************************************************************************
460 prints a UNIX 'token' to debug output.
461 ****************************************************************************/
463 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
464 int n_groups, gid_t *groups)
467 DEBUGC(dbg_class, dbg_lev,
468 ("UNIX token of user %ld\n", (long int)uid));
470 DEBUGADDC(dbg_class, dbg_lev,
471 ("Primary group is %ld and contains %i supplementary "
472 "groups\n", (long int)gid, n_groups));
473 for (i = 0; i < n_groups; i++)
474 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
475 (long int)groups[i]));
478 static int server_info_dtor(auth_serversupplied_info *server_info)
480 TALLOC_FREE(server_info->sam_account);
481 ZERO_STRUCTP(server_info);
485 /***************************************************************************
486 Make a server_info struct. Free with TALLOC_FREE().
487 ***************************************************************************/
489 static auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
491 struct auth_serversupplied_info *result;
493 result = TALLOC_ZERO_P(mem_ctx, auth_serversupplied_info);
494 if (result == NULL) {
495 DEBUG(0, ("talloc failed\n"));
499 talloc_set_destructor(result, server_info_dtor);
501 /* Initialise the uid and gid values to something non-zero
502 which may save us from giving away root access if there
503 is a bug in allocating these fields. */
510 /***************************************************************************
511 Make (and fill) a user_info struct from a struct samu
512 ***************************************************************************/
514 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
515 struct samu *sampass)
520 auth_serversupplied_info *result;
523 DOM_SID unix_group_sid;
526 if ( !(result = make_server_info(NULL)) ) {
527 return NT_STATUS_NO_MEMORY;
530 if ( !(pwd = getpwnam_alloc(result, pdb_get_username(sampass))) ) {
531 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
532 pdb_get_username(sampass)));
534 return NT_STATUS_NO_SUCH_USER;
537 result->sam_account = sampass;
538 /* Ensure thaat the sampass will be freed with the result */
539 talloc_steal(result, sampass);
540 result->unix_name = pwd->pw_name;
541 /* Ensure that we keep pwd->pw_name, because we will free pwd below */
542 talloc_steal(result, pwd->pw_name);
543 result->gid = pwd->pw_gid;
544 result->uid = pwd->pw_uid;
548 status = pdb_enum_group_memberships(result, sampass,
549 &result->sids, &gids,
552 if (!NT_STATUS_IS_OK(status)) {
553 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
555 result->sam_account = NULL; /* Don't free on error exit. */
560 /* Add the "Unix Group" SID for each gid to catch mapped groups
561 and their Unix equivalent. This is to solve the backwards
562 compatibility problem of 'valid users = +ntadmin' where
563 ntadmin has been paired with "Domain Admins" in the group
564 mapping table. Otherwise smb.conf would need to be changed
565 to 'valid user = "Domain Admins"'. --jerry */
567 num_gids = result->num_sids;
568 for ( i=0; i<num_gids; i++ ) {
569 if ( !gid_to_unix_groups_sid( gids[i], &unix_group_sid ) ) {
570 DEBUG(1,("make_server_info_sam: Failed to create SID "
571 "for gid %d!\n", gids[i]));
574 if (!add_sid_to_array_unique( result, &unix_group_sid,
575 &result->sids, &result->num_sids )) {
576 result->sam_account = NULL; /* Don't free on error exit. */
578 return NT_STATUS_NO_MEMORY;
582 /* For now we throw away the gids and convert via sid_to_gid
583 * later. This needs fixing, but I'd like to get the code straight and
588 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
589 pdb_get_username(sampass), result->unix_name));
591 *server_info = result;
596 static NTSTATUS log_nt_token(TALLOC_CTX *tmp_ctx, NT_USER_TOKEN *token)
602 if ((lp_log_nt_token_command() == NULL) ||
603 (strlen(lp_log_nt_token_command()) == 0)) {
607 group_sidstr = talloc_strdup(tmp_ctx, "");
608 for (i=1; i<token->num_sids; i++) {
609 group_sidstr = talloc_asprintf(
610 tmp_ctx, "%s %s", group_sidstr,
611 sid_string_static(&token->user_sids[i]));
614 command = talloc_string_sub(
615 tmp_ctx, lp_log_nt_token_command(),
616 "%s", sid_string_static(&token->user_sids[0]));
617 command = talloc_string_sub(tmp_ctx, command, "%t", group_sidstr);
619 if (command == NULL) {
620 return NT_STATUS_NO_MEMORY;
623 DEBUG(8, ("running command: [%s]\n", command));
624 if (smbrun(command, NULL) != 0) {
625 DEBUG(0, ("Could not log NT token\n"));
626 return NT_STATUS_ACCESS_DENIED;
633 * Create the token to use from server_info->sam_account and
634 * server_info->sids (the info3/sam groups). Find the unix gids.
637 NTSTATUS create_local_token(auth_serversupplied_info *server_info)
644 mem_ctx = talloc_new(NULL);
645 if (mem_ctx == NULL) {
646 DEBUG(0, ("talloc_new failed\n"));
647 return NT_STATUS_NO_MEMORY;
651 * If winbind is not around, we can not make much use of the SIDs the
652 * domain controller provided us with. Likewise if the user name was
653 * mapped to some local unix user.
656 if (((lp_server_role() == ROLE_DOMAIN_MEMBER) && !winbind_ping()) ||
657 (server_info->was_mapped)) {
658 status = create_token_from_username(server_info,
659 server_info->unix_name,
663 &server_info->unix_name,
667 server_info->ptok = create_local_nt_token(
669 pdb_get_user_sid(server_info->sam_account),
671 server_info->num_sids, server_info->sids);
672 status = server_info->ptok ?
673 NT_STATUS_OK : NT_STATUS_NO_SUCH_USER;
676 if (!NT_STATUS_IS_OK(status)) {
677 TALLOC_FREE(mem_ctx);
681 /* Convert the SIDs to gids. */
683 server_info->n_groups = 0;
684 server_info->groups = NULL;
686 /* Start at index 1, where the groups start. */
688 for (i=1; i<server_info->ptok->num_sids; i++) {
690 DOM_SID *sid = &server_info->ptok->user_sids[i];
692 if (!sid_to_gid(sid, &gid)) {
693 DEBUG(10, ("Could not convert SID %s to gid, "
694 "ignoring it\n", sid_string_static(sid)));
697 add_gid_to_array_unique(server_info, gid, &server_info->groups,
698 &server_info->n_groups);
701 debug_nt_user_token(DBGC_AUTH, 10, server_info->ptok);
703 status = log_nt_token(mem_ctx, server_info->ptok);
705 TALLOC_FREE(mem_ctx);
710 * Create an artificial NT token given just a username. (Initially indended
713 * We go through lookup_name() to avoid problems we had with 'winbind use
718 * unmapped unix users: Go directly to nss to find the user's group.
720 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
722 * If the user is provided by winbind, the primary gid is set to "domain
723 * users" of the user's domain. For an explanation why this is necessary, see
724 * the thread starting at
725 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
728 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
730 uid_t *uid, gid_t *gid,
731 char **found_username,
732 struct nt_user_token **token)
734 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
737 enum lsa_SidType type;
740 DOM_SID unix_group_sid;
741 size_t num_group_sids;
745 tmp_ctx = talloc_new(NULL);
746 if (tmp_ctx == NULL) {
747 DEBUG(0, ("talloc_new failed\n"));
748 return NT_STATUS_NO_MEMORY;
751 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
752 NULL, NULL, &user_sid, &type)) {
753 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
757 if (type != SID_NAME_USER) {
758 DEBUG(1, ("%s is a %s, not a user\n", username,
759 sid_type_lookup(type)));
763 if (!sid_to_uid(&user_sid, uid)) {
764 DEBUG(1, ("sid_to_uid for %s (%s) failed\n",
765 username, sid_string_static(&user_sid)));
769 if (sid_check_is_in_our_domain(&user_sid)) {
771 /* This is a passdb user, so ask passdb */
773 struct samu *sam_acct = NULL;
775 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
776 result = NT_STATUS_NO_MEMORY;
780 if (!pdb_getsampwsid(sam_acct, &user_sid)) {
781 DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n",
782 sid_string_static(&user_sid), username));
783 DEBUGADD(1, ("Fall back to unix user %s\n", username));
787 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
790 if (!NT_STATUS_IS_OK(result)) {
791 DEBUG(10, ("enum_group_memberships failed for %s\n",
793 DEBUGADD(1, ("Fall back to unix user %s\n", username));
797 /* see the smb_panic() in pdb_default_enum_group_memberships */
798 SMB_ASSERT(num_group_sids > 0);
802 /* Ensure we're returning the found_username on the right context. */
803 *found_username = talloc_strdup(mem_ctx,
804 pdb_get_username(sam_acct));
806 } else if (sid_check_is_in_unix_users(&user_sid)) {
808 /* This is a unix user not in passdb. We need to ask nss
809 * directly, without consulting passdb */
814 * This goto target is used as a fallback for the passdb
815 * case. The concrete bug report is when passdb gave us an
821 uid_to_unix_users_sid(*uid, &user_sid);
823 pass = getpwuid_alloc(tmp_ctx, *uid);
825 DEBUG(1, ("getpwuid(%d) for user %s failed\n",
830 if (!getgroups_unix_user(tmp_ctx, username, pass->pw_gid,
831 &gids, &num_group_sids)) {
832 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
837 if (num_group_sids) {
838 group_sids = TALLOC_ARRAY(tmp_ctx, DOM_SID, num_group_sids);
839 if (group_sids == NULL) {
840 DEBUG(1, ("TALLOC_ARRAY failed\n"));
841 result = NT_STATUS_NO_MEMORY;
848 for (i=0; i<num_group_sids; i++) {
849 gid_to_sid(&group_sids[i], gids[i]);
852 /* In getgroups_unix_user we always set the primary gid */
853 SMB_ASSERT(num_group_sids > 0);
857 /* Ensure we're returning the found_username on the right context. */
858 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
861 /* This user is from winbind, force the primary gid to the
862 * user's "domain users" group. Under certain circumstances
863 * (user comes from NT4), this might be a loss of
864 * information. But we can not rely on winbind getting the
865 * correct info. AD might prohibit winbind looking up that
871 group_sids = TALLOC_ARRAY(tmp_ctx, DOM_SID, num_group_sids);
872 if (group_sids == NULL) {
873 DEBUG(1, ("TALLOC_ARRAY failed\n"));
874 result = NT_STATUS_NO_MEMORY;
878 sid_copy(&group_sids[0], &user_sid);
879 sid_split_rid(&group_sids[0], &dummy);
880 sid_append_rid(&group_sids[0], DOMAIN_GROUP_RID_USERS);
882 if (!sid_to_gid(&group_sids[0], gid)) {
883 DEBUG(1, ("sid_to_gid(%s) failed\n",
884 sid_string_static(&group_sids[0])));
890 /* Ensure we're returning the found_username on the right context. */
891 *found_username = talloc_strdup(mem_ctx, username);
894 /* Add the "Unix Group" SID for each gid to catch mapped groups
895 and their Unix equivalent. This is to solve the backwards
896 compatibility problem of 'valid users = +ntadmin' where
897 ntadmin has been paired with "Domain Admins" in the group
898 mapping table. Otherwise smb.conf would need to be changed
899 to 'valid user = "Domain Admins"'. --jerry */
901 num_gids = num_group_sids;
902 for ( i=0; i<num_gids; i++ ) {
905 /* don't pickup anything managed by Winbind */
907 if ( lp_idmap_gid(&low, &high) && (gids[i] >= low) && (gids[i] <= high) )
910 if ( !gid_to_unix_groups_sid( gids[i], &unix_group_sid ) ) {
911 DEBUG(1,("create_token_from_username: Failed to create SID "
912 "for gid %d!\n", gids[i]));
915 if (!add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
916 &group_sids, &num_group_sids )) {
917 result = NT_STATUS_NO_MEMORY;
922 /* Ensure we're creating the nt_token on the right context. */
923 *token = create_local_nt_token(mem_ctx, &user_sid,
924 is_guest, num_group_sids, group_sids);
926 if ((*token == NULL) || (*found_username == NULL)) {
927 result = NT_STATUS_NO_MEMORY;
931 result = NT_STATUS_OK;
933 TALLOC_FREE(tmp_ctx);
937 /***************************************************************************
938 Build upon create_token_from_username:
940 Expensive helper function to figure out whether a user given its name is
941 member of a particular group.
942 ***************************************************************************/
944 BOOL user_in_group_sid(const char *username, const DOM_SID *group_sid)
949 char *found_username;
950 struct nt_user_token *token;
955 mem_ctx = talloc_new(NULL);
956 if (mem_ctx == NULL) {
957 DEBUG(0, ("talloc_new failed\n"));
961 status = create_token_from_username(mem_ctx, username, False,
962 &uid, &gid, &found_username,
965 if (!NT_STATUS_IS_OK(status)) {
966 DEBUG(10, ("could not create token for %s\n", username));
970 result = nt_token_check_sid(group_sid, token);
972 TALLOC_FREE(mem_ctx);
977 BOOL user_in_group(const char *username, const char *groupname)
983 mem_ctx = talloc_new(NULL);
984 if (mem_ctx == NULL) {
985 DEBUG(0, ("talloc_new failed\n"));
989 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
990 NULL, NULL, &group_sid, NULL);
991 TALLOC_FREE(mem_ctx);
994 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
998 return user_in_group_sid(username, &group_sid);
1002 /***************************************************************************
1003 Make (and fill) a user_info struct from a 'struct passwd' by conversion
1005 ***************************************************************************/
1007 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info,
1008 char *unix_username,
1012 struct samu *sampass = NULL;
1014 auth_serversupplied_info *result;
1016 if ( !(sampass = samu_new( NULL )) ) {
1017 return NT_STATUS_NO_MEMORY;
1020 status = samu_set_unix( sampass, pwd );
1021 if (!NT_STATUS_IS_OK(status)) {
1025 result = make_server_info(NULL);
1026 if (result == NULL) {
1027 TALLOC_FREE(sampass);
1028 return NT_STATUS_NO_MEMORY;
1031 result->sam_account = sampass;
1032 result->unix_name = talloc_strdup(result, unix_username);
1033 result->uid = pwd->pw_uid;
1034 result->gid = pwd->pw_gid;
1036 status = pdb_enum_group_memberships(result, sampass,
1037 &result->sids, &gids,
1040 if (!NT_STATUS_IS_OK(status)) {
1041 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
1042 nt_errstr(status)));
1043 TALLOC_FREE(result);
1047 /* For now we throw away the gids and convert via sid_to_gid
1048 * later. This needs fixing, but I'd like to get the code straight and
1052 *server_info = result;
1054 return NT_STATUS_OK;
1057 /***************************************************************************
1058 Make (and fill) a user_info struct for a guest login.
1059 This *must* succeed for smbd to start. If there is no mapping entry for
1060 the guest gid, then create one.
1061 ***************************************************************************/
1063 static NTSTATUS make_new_server_info_guest(auth_serversupplied_info **server_info)
1066 struct samu *sampass = NULL;
1069 static const char zeros[16] = { 0, };
1071 if ( !(sampass = samu_new( NULL )) ) {
1072 return NT_STATUS_NO_MEMORY;
1075 sid_copy(&guest_sid, get_global_sam_sid());
1076 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
1079 ret = pdb_getsampwsid(sampass, &guest_sid);
1083 TALLOC_FREE(sampass);
1084 return NT_STATUS_NO_SUCH_USER;
1087 status = make_server_info_sam(server_info, sampass);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 TALLOC_FREE(sampass);
1093 (*server_info)->guest = True;
1095 status = create_local_token(*server_info);
1096 if (!NT_STATUS_IS_OK(status)) {
1097 DEBUG(10, ("create_local_token failed: %s\n",
1098 nt_errstr(status)));
1102 /* annoying, but the Guest really does have a session key, and it is
1104 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
1105 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
1107 return NT_STATUS_OK;
1110 static auth_serversupplied_info *copy_serverinfo(auth_serversupplied_info *src)
1112 auth_serversupplied_info *dst;
1114 dst = make_server_info(NULL);
1119 dst->guest = src->guest;
1120 dst->uid = src->uid;
1121 dst->gid = src->gid;
1122 dst->n_groups = src->n_groups;
1123 if (src->n_groups != 0) {
1124 dst->groups = (gid_t *)TALLOC_MEMDUP(
1125 dst, src->groups, sizeof(gid_t)*dst->n_groups);
1131 dst->ptok = dup_nt_token(dst, src->ptok);
1138 dst->user_session_key = data_blob_talloc( dst, src->user_session_key.data,
1139 src->user_session_key.length);
1141 dst->lm_session_key = data_blob_talloc(dst, src->lm_session_key.data,
1142 src->lm_session_key.length);
1144 dst->sam_account = samu_new(NULL);
1145 if (!dst->sam_account) {
1150 if (!pdb_copy_sam_account(dst->sam_account, src->sam_account)) {
1155 dst->pam_handle = NULL;
1156 dst->unix_name = talloc_strdup(dst, src->unix_name);
1157 if (!dst->unix_name) {
1165 static auth_serversupplied_info *guest_info = NULL;
1167 BOOL init_guest_info(void)
1169 if (guest_info != NULL)
1172 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info));
1175 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
1177 *server_info = copy_serverinfo(guest_info);
1178 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1181 BOOL copy_current_user(struct current_user *dst, struct current_user *src)
1184 NT_USER_TOKEN *nt_token;
1186 groups = (gid_t *)memdup(src->ut.groups,
1187 sizeof(gid_t) * src->ut.ngroups);
1188 if ((src->ut.ngroups != 0) && (groups == NULL)) {
1192 nt_token = dup_nt_token(NULL, src->nt_user_token);
1193 if (nt_token == NULL) {
1198 dst->conn = src->conn;
1199 dst->vuid = src->vuid;
1200 dst->ut.uid = src->ut.uid;
1201 dst->ut.gid = src->ut.gid;
1202 dst->ut.ngroups = src->ut.ngroups;
1203 dst->ut.groups = groups;
1204 dst->nt_user_token = nt_token;
1208 BOOL set_current_user_guest(struct current_user *dst)
1211 NT_USER_TOKEN *nt_token;
1213 groups = (gid_t *)memdup(guest_info->groups,
1214 sizeof(gid_t) * guest_info->n_groups);
1215 if (groups == NULL) {
1219 nt_token = dup_nt_token(NULL, guest_info->ptok);
1220 if (nt_token == NULL) {
1225 TALLOC_FREE(dst->nt_user_token);
1226 SAFE_FREE(dst->ut.groups);
1228 /* dst->conn is never really dereferenced, it's only tested for
1229 * equality in uid.c */
1232 dst->vuid = UID_FIELD_INVALID;
1233 dst->ut.uid = guest_info->uid;
1234 dst->ut.gid = guest_info->gid;
1235 dst->ut.ngroups = guest_info->n_groups;
1236 dst->ut.groups = groups;
1237 dst->nt_user_token = nt_token;
1241 /***************************************************************************
1242 Purely internal function for make_server_info_info3
1243 Fill the sam account from getpwnam
1244 ***************************************************************************/
1245 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx,
1247 const char *username,
1248 char **found_username,
1249 uid_t *uid, gid_t *gid,
1250 struct samu *account,
1251 BOOL *username_was_mapped)
1254 fstring dom_user, lower_username;
1255 fstring real_username;
1256 struct passwd *passwd;
1258 fstrcpy( lower_username, username );
1259 strlower_m( lower_username );
1261 fstr_sprintf(dom_user, "%s%c%s", domain, *lp_winbind_separator(),
1264 /* Get the passwd struct. Try to create the account is necessary. */
1266 *username_was_mapped = map_username( dom_user );
1268 if ( !(passwd = smb_getpwnam( NULL, dom_user, real_username, True )) )
1269 return NT_STATUS_NO_SUCH_USER;
1271 *uid = passwd->pw_uid;
1272 *gid = passwd->pw_gid;
1274 /* This is pointless -- there is no suport for differing
1275 unix and windows names. Make sure to always store the
1276 one we actually looked up and succeeded. Have I mentioned
1277 why I hate the 'winbind use default domain' parameter?
1280 *found_username = talloc_strdup( mem_ctx, real_username );
1282 DEBUG(5,("fill_sam_account: located username was [%s]\n", *found_username));
1284 nt_status = samu_set_unix( account, passwd );
1286 TALLOC_FREE(passwd);
1291 /****************************************************************************
1292 Wrapper to allow the getpwnam() call to strip the domain name and
1293 try again in case a local UNIX user is already there. Also run through
1294 the username if we fallback to the username only.
1295 ****************************************************************************/
1297 struct passwd *smb_getpwnam( TALLOC_CTX *mem_ctx, char *domuser,
1298 fstring save_username, BOOL create )
1300 struct passwd *pw = NULL;
1304 /* we only save a copy of the username it has been mangled
1305 by winbindd use default domain */
1307 save_username[0] = '\0';
1309 /* don't call map_username() here since it has to be done higher
1310 up the stack so we don't call it mutliple times */
1312 fstrcpy( username, domuser );
1314 p = strchr_m( username, *lp_winbind_separator() );
1316 /* code for a DOMAIN\user string */
1319 fstring strip_username;
1321 pw = Get_Pwnam_alloc( mem_ctx, domuser );
1323 /* make sure we get the case of the username correct */
1324 /* work around 'winbind use default domain = yes' */
1326 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1329 /* split the domain and username into 2 strings */
1333 fstr_sprintf(save_username, "%s%c%s", domain, *lp_winbind_separator(), pw->pw_name);
1336 fstrcpy( save_username, pw->pw_name );
1342 /* setup for lookup of just the username */
1343 /* remember that p and username are overlapping memory */
1346 fstrcpy( strip_username, p );
1347 fstrcpy( username, strip_username );
1350 /* just lookup a plain username */
1352 pw = Get_Pwnam_alloc(mem_ctx, username);
1354 /* Create local user if requested but only if winbindd
1355 is not running. We need to protect against cases
1356 where winbindd is failing and then prematurely
1357 creating users in /etc/passwd */
1359 if ( !pw && create && !winbind_ping() ) {
1360 /* Don't add a machine account. */
1361 if (username[strlen(username)-1] == '$')
1364 smb_create_user(NULL, username, NULL);
1365 pw = Get_Pwnam_alloc(mem_ctx, username);
1368 /* one last check for a valid passwd struct */
1371 fstrcpy( save_username, pw->pw_name );
1376 /***************************************************************************
1377 Make a server_info struct from the info3 returned by a domain logon
1378 ***************************************************************************/
1380 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1381 const char *sent_nt_username,
1383 auth_serversupplied_info **server_info,
1384 NET_USER_INFO_3 *info3)
1386 static const char zeros[16] = { 0, };
1388 NTSTATUS nt_status = NT_STATUS_OK;
1389 char *found_username;
1390 const char *nt_domain;
1391 const char *nt_username;
1392 struct samu *sam_account = NULL;
1395 BOOL username_was_mapped;
1402 auth_serversupplied_info *result;
1405 Here is where we should check the list of
1406 trusted domains, and verify that the SID
1410 sid_copy(&user_sid, &info3->dom_sid.sid);
1411 if (!sid_append_rid(&user_sid, info3->user_rid)) {
1412 return NT_STATUS_INVALID_PARAMETER;
1415 sid_copy(&group_sid, &info3->dom_sid.sid);
1416 if (!sid_append_rid(&group_sid, info3->group_rid)) {
1417 return NT_STATUS_INVALID_PARAMETER;
1420 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
1421 /* If the server didn't give us one, just use the one we sent
1423 nt_username = sent_nt_username;
1426 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
1427 /* If the server didn't give us one, just use the one we sent
1432 /* try to fill the SAM account.. If getpwnam() fails, then try the
1433 add user script (2.2.x behavior).
1435 We use the _unmapped_ username here in an attempt to provide
1436 consistent username mapping behavior between kerberos and NTLM[SSP]
1437 authentication in domain mode security. I.E. Username mapping
1438 should be applied to the fully qualified username
1439 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1440 called map_username() unnecessarily in make_user_info_map() but
1441 that is how the current code is designed. Making the change here
1442 is the least disruptive place. -- jerry */
1444 if ( !(sam_account = samu_new( NULL )) ) {
1445 return NT_STATUS_NO_MEMORY;
1448 /* this call will try to create the user if necessary */
1450 nt_status = fill_sam_account(mem_ctx, nt_domain, sent_nt_username,
1451 &found_username, &uid, &gid, sam_account,
1452 &username_was_mapped);
1455 /* if we still don't have a valid unix account check for
1456 'map to guest = bad uid' */
1458 if (!NT_STATUS_IS_OK(nt_status)) {
1459 TALLOC_FREE( sam_account );
1460 if ( lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID ) {
1461 make_server_info_guest(server_info);
1462 return NT_STATUS_OK;
1467 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1468 TALLOC_FREE(sam_account);
1469 return NT_STATUS_NO_MEMORY;
1472 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1473 TALLOC_FREE(sam_account);
1474 return NT_STATUS_NO_MEMORY;
1477 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1478 TALLOC_FREE(sam_account);
1479 return NT_STATUS_NO_MEMORY;
1482 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1483 TALLOC_FREE(sam_account);
1484 return NT_STATUS_UNSUCCESSFUL;
1487 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1488 TALLOC_FREE(sam_account);
1489 return NT_STATUS_UNSUCCESSFUL;
1492 if (!pdb_set_fullname(sam_account,
1493 unistr2_static(&(info3->uni_full_name)),
1495 TALLOC_FREE(sam_account);
1496 return NT_STATUS_NO_MEMORY;
1499 if (!pdb_set_logon_script(sam_account,
1500 unistr2_static(&(info3->uni_logon_script)),
1502 TALLOC_FREE(sam_account);
1503 return NT_STATUS_NO_MEMORY;
1506 if (!pdb_set_profile_path(sam_account,
1507 unistr2_static(&(info3->uni_profile_path)),
1509 TALLOC_FREE(sam_account);
1510 return NT_STATUS_NO_MEMORY;
1513 if (!pdb_set_homedir(sam_account,
1514 unistr2_static(&(info3->uni_home_dir)),
1516 TALLOC_FREE(sam_account);
1517 return NT_STATUS_NO_MEMORY;
1520 if (!pdb_set_dir_drive(sam_account,
1521 unistr2_static(&(info3->uni_dir_drive)),
1523 TALLOC_FREE(sam_account);
1524 return NT_STATUS_NO_MEMORY;
1527 if (!pdb_set_acct_ctrl(sam_account, info3->acct_flags, PDB_CHANGED)) {
1528 TALLOC_FREE(sam_account);
1529 return NT_STATUS_NO_MEMORY;
1532 result = make_server_info(NULL);
1533 if (result == NULL) {
1534 DEBUG(4, ("make_server_info failed!\n"));
1535 TALLOC_FREE(sam_account);
1536 return NT_STATUS_NO_MEMORY;
1539 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1540 valid struct samu) */
1542 result->sam_account = sam_account;
1543 result->unix_name = talloc_strdup(result, found_username);
1545 /* Fill in the unix info we found on the way */
1550 /* Create a 'combined' list of all SIDs we might want in the SD */
1552 result->num_sids = 0;
1553 result->sids = NULL;
1555 /* and create (by appending rids) the 'domain' sids */
1557 for (i = 0; i < info3->num_groups2; i++) {
1559 if (!sid_compose(&sid, &info3->dom_sid.sid,
1560 info3->gids[i].g_rid)) {
1561 DEBUG(3,("could not append additional group rid "
1562 "0x%x\n", info3->gids[i].g_rid));
1563 TALLOC_FREE(result);
1564 return NT_STATUS_INVALID_PARAMETER;
1566 if (!add_sid_to_array(result, &sid, &result->sids,
1567 &result->num_sids)) {
1568 TALLOC_FREE(result);
1569 return NT_STATUS_NO_MEMORY;
1573 /* Copy 'other' sids. We need to do sid filtering here to
1574 prevent possible elevation of privileges. See:
1576 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1579 for (i = 0; i < info3->num_other_sids; i++) {
1580 if (!add_sid_to_array(result, &info3->other_sids[i].sid,
1582 &result->num_sids)) {
1583 TALLOC_FREE(result);
1584 return NT_STATUS_NO_MEMORY;
1588 result->login_server = unistr2_tdup(result,
1589 &(info3->uni_logon_srv));
1591 /* ensure we are never given NULL session keys */
1593 if (memcmp(info3->user_sess_key, zeros, sizeof(zeros)) == 0) {
1594 result->user_session_key = data_blob(NULL, 0);
1596 result->user_session_key = data_blob_talloc(
1597 result, info3->user_sess_key,
1598 sizeof(info3->user_sess_key));
1601 if (memcmp(info3->lm_sess_key, zeros, 8) == 0) {
1602 result->lm_session_key = data_blob(NULL, 0);
1604 result->lm_session_key = data_blob_talloc(
1605 result, info3->lm_sess_key,
1606 sizeof(info3->lm_sess_key));
1609 result->was_mapped = username_was_mapped;
1611 *server_info = result;
1613 return NT_STATUS_OK;
1616 /***************************************************************************
1617 Free a user_info struct
1618 ***************************************************************************/
1620 void free_user_info(auth_usersupplied_info **user_info)
1622 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1623 if (*user_info != NULL) {
1624 if ((*user_info)->smb_name) {
1625 DEBUG(10,("structure was created for %s\n",
1626 (*user_info)->smb_name));
1628 SAFE_FREE((*user_info)->smb_name);
1629 SAFE_FREE((*user_info)->internal_username);
1630 SAFE_FREE((*user_info)->client_domain);
1631 SAFE_FREE((*user_info)->domain);
1632 SAFE_FREE((*user_info)->wksta_name);
1633 data_blob_free(&(*user_info)->lm_resp);
1634 data_blob_free(&(*user_info)->nt_resp);
1635 data_blob_clear_free(&(*user_info)->lm_interactive_pwd);
1636 data_blob_clear_free(&(*user_info)->nt_interactive_pwd);
1637 data_blob_clear_free(&(*user_info)->plaintext_password);
1638 ZERO_STRUCT(**user_info);
1640 SAFE_FREE(*user_info);
1643 /***************************************************************************
1644 Make an auth_methods struct
1645 ***************************************************************************/
1647 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
1649 if (!auth_context) {
1650 smb_panic("no auth_context supplied to "
1651 "make_auth_methods()!\n");
1655 smb_panic("make_auth_methods: pointer to auth_method pointer "
1659 *auth_method = TALLOC_P(auth_context->mem_ctx, auth_methods);
1660 if (!*auth_method) {
1661 DEBUG(0,("make_auth_method: malloc failed!\n"));
1664 ZERO_STRUCTP(*auth_method);
1670 * Verify whether or not given domain is trusted.
1672 * @param domain_name name of the domain to be verified
1673 * @return true if domain is one of the trusted once or
1674 * false if otherwise
1677 BOOL is_trusted_domain(const char* dom_name)
1679 DOM_SID trustdom_sid;
1682 /* no trusted domains for a standalone server */
1684 if ( lp_server_role() == ROLE_STANDALONE )
1687 /* if we are a DC, then check for a direct trust relationships */
1691 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
1692 "[%s]\n", dom_name ));
1693 ret = pdb_get_trusteddom_pw(dom_name, NULL, NULL, NULL);
1701 /* If winbind is around, ask it */
1703 result = wb_is_trusted_domain(dom_name);
1705 if (result == NSS_STATUS_SUCCESS) {
1709 if (result == NSS_STATUS_NOTFOUND) {
1710 /* winbind could not find the domain */
1714 /* The only other possible result is that winbind is not up
1715 and running. We need to update the trustdom_cache
1718 update_trustdom_cache();
1721 /* now the trustdom cache should be available a DC could still
1722 * have a transitive trust so fall back to the cache of trusted
1723 * domains (like a domain member would use */
1725 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {