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 3 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, see <http://www.gnu.org/licenses/>.
27 #define DBGC_CLASS DBGC_AUTH
29 /****************************************************************************
30 Create a UNIX user on demand.
31 ****************************************************************************/
33 static int smb_create_user(const char *domain, const char *unix_username, const char *homedir)
38 pstrcpy(add_script, lp_adduser_script());
41 all_string_sub(add_script, "%u", unix_username, sizeof(pstring));
43 all_string_sub(add_script, "%D", domain, sizeof(pstring));
45 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
46 ret = smbrun(add_script,NULL);
48 DEBUG(ret ? 0 : 3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
52 /****************************************************************************
53 Create an auth_usersupplied_data structure
54 ****************************************************************************/
56 static NTSTATUS make_user_info(auth_usersupplied_info **user_info,
58 const char *internal_username,
59 const char *client_domain,
61 const char *wksta_name,
62 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
63 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
68 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
70 *user_info = SMB_MALLOC_P(auth_usersupplied_info);
71 if (*user_info == NULL) {
72 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
73 return NT_STATUS_NO_MEMORY;
76 ZERO_STRUCTP(*user_info);
78 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
80 (*user_info)->smb_name = SMB_STRDUP(smb_name);
81 if ((*user_info)->smb_name == NULL) {
82 free_user_info(user_info);
83 return NT_STATUS_NO_MEMORY;
86 (*user_info)->internal_username = SMB_STRDUP(internal_username);
87 if ((*user_info)->internal_username == NULL) {
88 free_user_info(user_info);
89 return NT_STATUS_NO_MEMORY;
92 (*user_info)->domain = SMB_STRDUP(domain);
93 if ((*user_info)->domain == NULL) {
94 free_user_info(user_info);
95 return NT_STATUS_NO_MEMORY;
98 (*user_info)->client_domain = SMB_STRDUP(client_domain);
99 if ((*user_info)->client_domain == NULL) {
100 free_user_info(user_info);
101 return NT_STATUS_NO_MEMORY;
104 (*user_info)->wksta_name = SMB_STRDUP(wksta_name);
105 if ((*user_info)->wksta_name == NULL) {
106 free_user_info(user_info);
107 return NT_STATUS_NO_MEMORY;
110 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
113 (*user_info)->lm_resp = data_blob(lm_pwd->data, lm_pwd->length);
115 (*user_info)->nt_resp = data_blob(nt_pwd->data, nt_pwd->length);
116 if (lm_interactive_pwd)
117 (*user_info)->lm_interactive_pwd = data_blob(lm_interactive_pwd->data, lm_interactive_pwd->length);
118 if (nt_interactive_pwd)
119 (*user_info)->nt_interactive_pwd = data_blob(nt_interactive_pwd->data, nt_interactive_pwd->length);
122 (*user_info)->plaintext_password = data_blob(plaintext->data, plaintext->length);
124 (*user_info)->encrypted = encrypted;
126 (*user_info)->logon_parameters = 0;
128 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
133 /****************************************************************************
134 Create an auth_usersupplied_data structure after appropriate mapping.
135 ****************************************************************************/
137 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info,
138 const char *smb_name,
139 const char *client_domain,
140 const char *wksta_name,
141 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
142 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
143 DATA_BLOB *plaintext,
149 fstring internal_username;
150 fstrcpy(internal_username, smb_name);
151 was_mapped = map_username(internal_username);
153 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
154 client_domain, smb_name, wksta_name));
156 /* don't allow "" as a domain, fixes a Win9X bug
157 where it doens't supply a domain for logon script
158 'net use' commands. */
160 if ( *client_domain )
161 domain = client_domain;
163 domain = lp_workgroup();
165 /* do what win2k does. Always map unknown domains to our own
166 and let the "passdb backend" handle unknown users. */
168 if ( !is_trusted_domain(domain) && !strequal(domain, get_global_sam_name()) )
169 domain = my_sam_name();
171 /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
173 result = make_user_info(user_info, smb_name, internal_username,
174 client_domain, domain, wksta_name,
176 lm_interactive_pwd, nt_interactive_pwd,
177 plaintext, encrypted);
178 if (NT_STATUS_IS_OK(result)) {
179 (*user_info)->was_mapped = was_mapped;
184 /****************************************************************************
185 Create an auth_usersupplied_data, making the DATA_BLOBs here.
186 Decrypt and encrypt the passwords.
187 ****************************************************************************/
189 bool make_user_info_netlogon_network(auth_usersupplied_info **user_info,
190 const char *smb_name,
191 const char *client_domain,
192 const char *wksta_name,
193 uint32 logon_parameters,
194 const uchar *lm_network_pwd,
196 const uchar *nt_network_pwd,
201 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
202 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
204 status = make_user_info_map(user_info,
205 smb_name, client_domain,
207 lm_pwd_len ? &lm_blob : NULL,
208 nt_pwd_len ? &nt_blob : NULL,
212 if (NT_STATUS_IS_OK(status)) {
213 (*user_info)->logon_parameters = logon_parameters;
215 ret = NT_STATUS_IS_OK(status) ? True : False;
217 data_blob_free(&lm_blob);
218 data_blob_free(&nt_blob);
222 /****************************************************************************
223 Create an auth_usersupplied_data, making the DATA_BLOBs here.
224 Decrypt and encrypt the passwords.
225 ****************************************************************************/
227 bool make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
228 const char *smb_name,
229 const char *client_domain,
230 const char *wksta_name,
231 uint32 logon_parameters,
233 const uchar lm_interactive_pwd[16],
234 const uchar nt_interactive_pwd[16],
235 const uchar *dc_sess_key)
237 unsigned char lm_pwd[16];
238 unsigned char nt_pwd[16];
239 unsigned char local_lm_response[24];
240 unsigned char local_nt_response[24];
241 unsigned char key[16];
244 memcpy(key, dc_sess_key, 8);
246 if (lm_interactive_pwd)
247 memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
249 if (nt_interactive_pwd)
250 memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
252 #ifdef DEBUG_PASSWORD
254 dump_data(100, key, sizeof(key));
256 DEBUG(100,("lm owf password:"));
257 dump_data(100, lm_pwd, sizeof(lm_pwd));
259 DEBUG(100,("nt owf password:"));
260 dump_data(100, nt_pwd, sizeof(nt_pwd));
263 if (lm_interactive_pwd)
264 SamOEMhash(lm_pwd, key, sizeof(lm_pwd));
266 if (nt_interactive_pwd)
267 SamOEMhash(nt_pwd, key, sizeof(nt_pwd));
269 #ifdef DEBUG_PASSWORD
270 DEBUG(100,("decrypt of lm owf password:"));
271 dump_data(100, lm_pwd, sizeof(lm_pwd));
273 DEBUG(100,("decrypt of nt owf password:"));
274 dump_data(100, nt_pwd, sizeof(nt_pwd));
277 if (lm_interactive_pwd)
278 SMBOWFencrypt(lm_pwd, chal,
281 if (nt_interactive_pwd)
282 SMBOWFencrypt(nt_pwd, chal,
285 /* Password info paranoia */
291 DATA_BLOB local_lm_blob;
292 DATA_BLOB local_nt_blob;
294 DATA_BLOB lm_interactive_blob;
295 DATA_BLOB nt_interactive_blob;
297 if (lm_interactive_pwd) {
298 local_lm_blob = data_blob(local_lm_response,
299 sizeof(local_lm_response));
300 lm_interactive_blob = data_blob(lm_pwd,
305 if (nt_interactive_pwd) {
306 local_nt_blob = data_blob(local_nt_response,
307 sizeof(local_nt_response));
308 nt_interactive_blob = data_blob(nt_pwd,
313 nt_status = make_user_info_map(
315 smb_name, client_domain, wksta_name,
316 lm_interactive_pwd ? &local_lm_blob : NULL,
317 nt_interactive_pwd ? &local_nt_blob : NULL,
318 lm_interactive_pwd ? &lm_interactive_blob : NULL,
319 nt_interactive_pwd ? &nt_interactive_blob : NULL,
322 if (NT_STATUS_IS_OK(nt_status)) {
323 (*user_info)->logon_parameters = logon_parameters;
326 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
327 data_blob_free(&local_lm_blob);
328 data_blob_free(&local_nt_blob);
329 data_blob_free(&lm_interactive_blob);
330 data_blob_free(&nt_interactive_blob);
336 /****************************************************************************
337 Create an auth_usersupplied_data structure
338 ****************************************************************************/
340 bool make_user_info_for_reply(auth_usersupplied_info **user_info,
341 const char *smb_name,
342 const char *client_domain,
344 DATA_BLOB plaintext_password)
347 DATA_BLOB local_lm_blob;
348 DATA_BLOB local_nt_blob;
349 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
352 * Not encrypted - do so.
355 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
358 if (plaintext_password.data) {
359 unsigned char local_lm_response[24];
361 #ifdef DEBUG_PASSWORD
362 DEBUG(10,("Unencrypted password (len %d):\n",
363 (int)plaintext_password.length));
364 dump_data(100, plaintext_password.data,
365 plaintext_password.length);
368 SMBencrypt( (const char *)plaintext_password.data,
369 (const uchar*)chal, local_lm_response);
370 local_lm_blob = data_blob(local_lm_response, 24);
372 /* We can't do an NT hash here, as the password needs to be
374 local_nt_blob = data_blob_null;
377 local_lm_blob = data_blob_null;
378 local_nt_blob = data_blob_null;
381 ret = make_user_info_map(
382 user_info, smb_name, client_domain,
383 get_remote_machine_name(),
384 local_lm_blob.data ? &local_lm_blob : NULL,
385 local_nt_blob.data ? &local_nt_blob : NULL,
387 plaintext_password.data ? &plaintext_password : NULL,
390 data_blob_free(&local_lm_blob);
391 return NT_STATUS_IS_OK(ret) ? True : False;
394 /****************************************************************************
395 Create an auth_usersupplied_data structure
396 ****************************************************************************/
398 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
399 const char *smb_name,
400 const char *client_domain,
401 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
403 return make_user_info_map(user_info, smb_name,
405 get_remote_machine_name(),
406 lm_resp.data ? &lm_resp : NULL,
407 nt_resp.data ? &nt_resp : NULL,
412 /****************************************************************************
413 Create a guest user_info blob, for anonymous authenticaion.
414 ****************************************************************************/
416 bool make_user_info_guest(auth_usersupplied_info **user_info)
420 nt_status = make_user_info(user_info,
429 return NT_STATUS_IS_OK(nt_status) ? True : False;
432 static int server_info_dtor(auth_serversupplied_info *server_info)
434 TALLOC_FREE(server_info->sam_account);
435 ZERO_STRUCTP(server_info);
439 /***************************************************************************
440 Make a server_info struct. Free with TALLOC_FREE().
441 ***************************************************************************/
443 static auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
445 struct auth_serversupplied_info *result;
447 result = TALLOC_ZERO_P(mem_ctx, auth_serversupplied_info);
448 if (result == NULL) {
449 DEBUG(0, ("talloc failed\n"));
453 talloc_set_destructor(result, server_info_dtor);
455 /* Initialise the uid and gid values to something non-zero
456 which may save us from giving away root access if there
457 is a bug in allocating these fields. */
464 /***************************************************************************
465 Make (and fill) a user_info struct from a struct samu
466 ***************************************************************************/
468 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
469 struct samu *sampass)
474 auth_serversupplied_info *result;
477 DOM_SID unix_group_sid;
480 if ( !(result = make_server_info(NULL)) ) {
481 return NT_STATUS_NO_MEMORY;
484 if ( !(pwd = getpwnam_alloc(result, pdb_get_username(sampass))) ) {
485 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
486 pdb_get_username(sampass)));
488 return NT_STATUS_NO_SUCH_USER;
491 result->sam_account = sampass;
492 /* Ensure thaat the sampass will be freed with the result */
493 talloc_steal(result, sampass);
494 result->unix_name = pwd->pw_name;
495 /* Ensure that we keep pwd->pw_name, because we will free pwd below */
496 talloc_steal(result, pwd->pw_name);
497 result->gid = pwd->pw_gid;
498 result->uid = pwd->pw_uid;
502 status = pdb_enum_group_memberships(result, sampass,
503 &result->sids, &gids,
506 if (!NT_STATUS_IS_OK(status)) {
507 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
509 result->sam_account = NULL; /* Don't free on error exit. */
514 /* Add the "Unix Group" SID for each gid to catch mapped groups
515 and their Unix equivalent. This is to solve the backwards
516 compatibility problem of 'valid users = +ntadmin' where
517 ntadmin has been paired with "Domain Admins" in the group
518 mapping table. Otherwise smb.conf would need to be changed
519 to 'valid user = "Domain Admins"'. --jerry */
521 num_gids = result->num_sids;
522 for ( i=0; i<num_gids; i++ ) {
523 if ( !gid_to_unix_groups_sid( gids[i], &unix_group_sid ) ) {
524 DEBUG(1,("make_server_info_sam: Failed to create SID "
525 "for gid %d!\n", gids[i]));
528 if (!add_sid_to_array_unique( result, &unix_group_sid,
529 &result->sids, &result->num_sids )) {
530 result->sam_account = NULL; /* Don't free on error exit. */
532 return NT_STATUS_NO_MEMORY;
536 /* For now we throw away the gids and convert via sid_to_gid
537 * later. This needs fixing, but I'd like to get the code straight and
542 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
543 pdb_get_username(sampass), result->unix_name));
545 *server_info = result;
550 static NTSTATUS log_nt_token(TALLOC_CTX *tmp_ctx, NT_USER_TOKEN *token)
556 if ((lp_log_nt_token_command() == NULL) ||
557 (strlen(lp_log_nt_token_command()) == 0)) {
561 group_sidstr = talloc_strdup(tmp_ctx, "");
562 for (i=1; i<token->num_sids; i++) {
563 group_sidstr = talloc_asprintf(
564 tmp_ctx, "%s %s", group_sidstr,
565 sid_string_static(&token->user_sids[i]));
568 command = talloc_string_sub(
569 tmp_ctx, lp_log_nt_token_command(),
570 "%s", sid_string_static(&token->user_sids[0]));
571 command = talloc_string_sub(tmp_ctx, command, "%t", group_sidstr);
573 if (command == NULL) {
574 return NT_STATUS_NO_MEMORY;
577 DEBUG(8, ("running command: [%s]\n", command));
578 if (smbrun(command, NULL) != 0) {
579 DEBUG(0, ("Could not log NT token\n"));
580 return NT_STATUS_ACCESS_DENIED;
587 * Create the token to use from server_info->sam_account and
588 * server_info->sids (the info3/sam groups). Find the unix gids.
591 NTSTATUS create_local_token(auth_serversupplied_info *server_info)
598 mem_ctx = talloc_new(NULL);
599 if (mem_ctx == NULL) {
600 DEBUG(0, ("talloc_new failed\n"));
601 return NT_STATUS_NO_MEMORY;
605 * If winbind is not around, we can not make much use of the SIDs the
606 * domain controller provided us with. Likewise if the user name was
607 * mapped to some local unix user.
610 if (((lp_server_role() == ROLE_DOMAIN_MEMBER) && !winbind_ping()) ||
611 (server_info->was_mapped)) {
612 status = create_token_from_username(server_info,
613 server_info->unix_name,
617 &server_info->unix_name,
621 server_info->ptok = create_local_nt_token(
623 pdb_get_user_sid(server_info->sam_account),
625 server_info->num_sids, server_info->sids);
626 status = server_info->ptok ?
627 NT_STATUS_OK : NT_STATUS_NO_SUCH_USER;
630 if (!NT_STATUS_IS_OK(status)) {
631 TALLOC_FREE(mem_ctx);
635 /* Convert the SIDs to gids. */
637 server_info->n_groups = 0;
638 server_info->groups = NULL;
640 /* Start at index 1, where the groups start. */
642 for (i=1; i<server_info->ptok->num_sids; i++) {
644 DOM_SID *sid = &server_info->ptok->user_sids[i];
646 if (!sid_to_gid(sid, &gid)) {
647 DEBUG(10, ("Could not convert SID %s to gid, "
648 "ignoring it\n", sid_string_static(sid)));
651 add_gid_to_array_unique(server_info, gid, &server_info->groups,
652 &server_info->n_groups);
655 debug_nt_user_token(DBGC_AUTH, 10, server_info->ptok);
657 status = log_nt_token(mem_ctx, server_info->ptok);
659 TALLOC_FREE(mem_ctx);
664 * Create an artificial NT token given just a username. (Initially indended
667 * We go through lookup_name() to avoid problems we had with 'winbind use
672 * unmapped unix users: Go directly to nss to find the user's group.
674 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
676 * If the user is provided by winbind, the primary gid is set to "domain
677 * users" of the user's domain. For an explanation why this is necessary, see
678 * the thread starting at
679 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
682 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
684 uid_t *uid, gid_t *gid,
685 char **found_username,
686 struct nt_user_token **token)
688 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
691 enum lsa_SidType type;
694 DOM_SID unix_group_sid;
695 size_t num_group_sids;
699 tmp_ctx = talloc_new(NULL);
700 if (tmp_ctx == NULL) {
701 DEBUG(0, ("talloc_new failed\n"));
702 return NT_STATUS_NO_MEMORY;
705 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
706 NULL, NULL, &user_sid, &type)) {
707 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
711 if (type != SID_NAME_USER) {
712 DEBUG(1, ("%s is a %s, not a user\n", username,
713 sid_type_lookup(type)));
717 if (!sid_to_uid(&user_sid, uid)) {
718 DEBUG(1, ("sid_to_uid for %s (%s) failed\n",
719 username, sid_string_static(&user_sid)));
723 if (sid_check_is_in_our_domain(&user_sid)) {
726 /* This is a passdb user, so ask passdb */
728 struct samu *sam_acct = NULL;
730 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
731 result = NT_STATUS_NO_MEMORY;
736 ret = pdb_getsampwsid(sam_acct, &user_sid);
740 DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n",
741 sid_string_static(&user_sid), username));
742 DEBUGADD(1, ("Fall back to unix user %s\n", username));
746 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
749 if (!NT_STATUS_IS_OK(result)) {
750 DEBUG(10, ("enum_group_memberships failed for %s\n",
752 DEBUGADD(1, ("Fall back to unix user %s\n", username));
756 /* see the smb_panic() in pdb_default_enum_group_memberships */
757 SMB_ASSERT(num_group_sids > 0);
761 /* Ensure we're returning the found_username on the right context. */
762 *found_username = talloc_strdup(mem_ctx,
763 pdb_get_username(sam_acct));
765 } else if (sid_check_is_in_unix_users(&user_sid)) {
767 /* This is a unix user not in passdb. We need to ask nss
768 * directly, without consulting passdb */
773 * This goto target is used as a fallback for the passdb
774 * case. The concrete bug report is when passdb gave us an
780 uid_to_unix_users_sid(*uid, &user_sid);
782 pass = getpwuid_alloc(tmp_ctx, *uid);
784 DEBUG(1, ("getpwuid(%d) for user %s failed\n",
789 if (!getgroups_unix_user(tmp_ctx, username, pass->pw_gid,
790 &gids, &num_group_sids)) {
791 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
796 if (num_group_sids) {
797 group_sids = TALLOC_ARRAY(tmp_ctx, DOM_SID, num_group_sids);
798 if (group_sids == NULL) {
799 DEBUG(1, ("TALLOC_ARRAY failed\n"));
800 result = NT_STATUS_NO_MEMORY;
807 for (i=0; i<num_group_sids; i++) {
808 gid_to_sid(&group_sids[i], gids[i]);
811 /* In getgroups_unix_user we always set the primary gid */
812 SMB_ASSERT(num_group_sids > 0);
816 /* Ensure we're returning the found_username on the right context. */
817 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
820 /* This user is from winbind, force the primary gid to the
821 * user's "domain users" group. Under certain circumstances
822 * (user comes from NT4), this might be a loss of
823 * information. But we can not rely on winbind getting the
824 * correct info. AD might prohibit winbind looking up that
830 group_sids = TALLOC_ARRAY(tmp_ctx, DOM_SID, num_group_sids);
831 if (group_sids == NULL) {
832 DEBUG(1, ("TALLOC_ARRAY failed\n"));
833 result = NT_STATUS_NO_MEMORY;
837 sid_copy(&group_sids[0], &user_sid);
838 sid_split_rid(&group_sids[0], &dummy);
839 sid_append_rid(&group_sids[0], DOMAIN_GROUP_RID_USERS);
841 if (!sid_to_gid(&group_sids[0], gid)) {
842 DEBUG(1, ("sid_to_gid(%s) failed\n",
843 sid_string_static(&group_sids[0])));
849 /* Ensure we're returning the found_username on the right context. */
850 *found_username = talloc_strdup(mem_ctx, username);
853 /* Add the "Unix Group" SID for each gid to catch mapped groups
854 and their Unix equivalent. This is to solve the backwards
855 compatibility problem of 'valid users = +ntadmin' where
856 ntadmin has been paired with "Domain Admins" in the group
857 mapping table. Otherwise smb.conf would need to be changed
858 to 'valid user = "Domain Admins"'. --jerry */
860 num_gids = num_group_sids;
861 for ( i=0; i<num_gids; i++ ) {
864 /* don't pickup anything managed by Winbind */
866 if ( lp_idmap_gid(&low, &high) && (gids[i] >= low) && (gids[i] <= high) )
869 if ( !gid_to_unix_groups_sid( gids[i], &unix_group_sid ) ) {
870 DEBUG(1,("create_token_from_username: Failed to create SID "
871 "for gid %d!\n", gids[i]));
874 if (!add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
875 &group_sids, &num_group_sids )) {
876 result = NT_STATUS_NO_MEMORY;
881 /* Ensure we're creating the nt_token on the right context. */
882 *token = create_local_nt_token(mem_ctx, &user_sid,
883 is_guest, num_group_sids, group_sids);
885 if ((*token == NULL) || (*found_username == NULL)) {
886 result = NT_STATUS_NO_MEMORY;
890 result = NT_STATUS_OK;
892 TALLOC_FREE(tmp_ctx);
896 /***************************************************************************
897 Build upon create_token_from_username:
899 Expensive helper function to figure out whether a user given its name is
900 member of a particular group.
901 ***************************************************************************/
903 bool user_in_group_sid(const char *username, const DOM_SID *group_sid)
908 char *found_username;
909 struct nt_user_token *token;
914 mem_ctx = talloc_new(NULL);
915 if (mem_ctx == NULL) {
916 DEBUG(0, ("talloc_new failed\n"));
920 status = create_token_from_username(mem_ctx, username, False,
921 &uid, &gid, &found_username,
924 if (!NT_STATUS_IS_OK(status)) {
925 DEBUG(10, ("could not create token for %s\n", username));
929 result = nt_token_check_sid(group_sid, token);
931 TALLOC_FREE(mem_ctx);
936 bool user_in_group(const char *username, const char *groupname)
942 mem_ctx = talloc_new(NULL);
943 if (mem_ctx == NULL) {
944 DEBUG(0, ("talloc_new failed\n"));
948 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
949 NULL, NULL, &group_sid, NULL);
950 TALLOC_FREE(mem_ctx);
953 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
957 return user_in_group_sid(username, &group_sid);
961 /***************************************************************************
962 Make (and fill) a user_info struct from a 'struct passwd' by conversion
964 ***************************************************************************/
966 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info,
971 struct samu *sampass = NULL;
973 char *qualified_name = NULL;
974 TALLOC_CTX *mem_ctx = NULL;
976 enum lsa_SidType type;
977 auth_serversupplied_info *result;
979 if ( !(sampass = samu_new( NULL )) ) {
980 return NT_STATUS_NO_MEMORY;
983 status = samu_set_unix( sampass, pwd );
984 if (!NT_STATUS_IS_OK(status)) {
988 result = make_server_info(NULL);
989 if (result == NULL) {
990 TALLOC_FREE(sampass);
991 return NT_STATUS_NO_MEMORY;
994 result->sam_account = sampass;
995 result->unix_name = talloc_strdup(result, unix_username);
996 result->uid = pwd->pw_uid;
997 result->gid = pwd->pw_gid;
999 status = pdb_enum_group_memberships(result, sampass,
1000 &result->sids, &gids,
1003 if (!NT_STATUS_IS_OK(status)) {
1004 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
1005 nt_errstr(status)));
1006 TALLOC_FREE(result);
1011 * The SID returned in server_info->sam_account is based
1012 * on our SAM sid even though for a pure UNIX account this should
1013 * not be the case as it doesn't really exist in the SAM db.
1014 * This causes lookups on "[in]valid users" to fail as they
1015 * will lookup this name as a "Unix User" SID to check against
1016 * the user token. Fix this by adding the "Unix User"\unix_username
1017 * SID to the sid array. The correct fix should probably be
1018 * changing the server_info->sam_account user SID to be a
1019 * S-1-22 Unix SID, but this might break old configs where
1020 * plaintext passwords were used with no SAM backend.
1023 mem_ctx = talloc_init("make_server_info_pw_tmp");
1025 TALLOC_FREE(result);
1026 return NT_STATUS_NO_MEMORY;
1029 qualified_name = talloc_asprintf(mem_ctx, "%s\\%s",
1030 unix_users_domain_name(),
1032 if (!qualified_name) {
1033 TALLOC_FREE(result);
1034 TALLOC_FREE(mem_ctx);
1035 return NT_STATUS_NO_MEMORY;
1038 if (!lookup_name(mem_ctx, qualified_name, LOOKUP_NAME_ALL,
1041 TALLOC_FREE(result);
1042 TALLOC_FREE(mem_ctx);
1043 return NT_STATUS_NO_SUCH_USER;
1046 TALLOC_FREE(mem_ctx);
1048 if (type != SID_NAME_USER) {
1049 TALLOC_FREE(result);
1050 return NT_STATUS_NO_SUCH_USER;
1053 if (!add_sid_to_array_unique(result, &u_sid,
1055 &result->num_sids)) {
1056 TALLOC_FREE(result);
1057 return NT_STATUS_NO_MEMORY;
1060 /* For now we throw away the gids and convert via sid_to_gid
1061 * later. This needs fixing, but I'd like to get the code straight and
1065 *server_info = result;
1067 return NT_STATUS_OK;
1070 /***************************************************************************
1071 Make (and fill) a user_info struct for a guest login.
1072 This *must* succeed for smbd to start. If there is no mapping entry for
1073 the guest gid, then create one.
1074 ***************************************************************************/
1076 static NTSTATUS make_new_server_info_guest(auth_serversupplied_info **server_info)
1079 struct samu *sampass = NULL;
1082 static const char zeros[16] = { 0, };
1084 if ( !(sampass = samu_new( NULL )) ) {
1085 return NT_STATUS_NO_MEMORY;
1088 sid_copy(&guest_sid, get_global_sam_sid());
1089 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
1092 ret = pdb_getsampwsid(sampass, &guest_sid);
1096 TALLOC_FREE(sampass);
1097 return NT_STATUS_NO_SUCH_USER;
1100 status = make_server_info_sam(server_info, sampass);
1101 if (!NT_STATUS_IS_OK(status)) {
1102 TALLOC_FREE(sampass);
1106 (*server_info)->guest = True;
1108 status = create_local_token(*server_info);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 DEBUG(10, ("create_local_token failed: %s\n",
1111 nt_errstr(status)));
1115 /* annoying, but the Guest really does have a session key, and it is
1117 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
1118 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
1120 return NT_STATUS_OK;
1123 static auth_serversupplied_info *copy_serverinfo(auth_serversupplied_info *src)
1125 auth_serversupplied_info *dst;
1127 dst = make_server_info(NULL);
1132 dst->guest = src->guest;
1133 dst->uid = src->uid;
1134 dst->gid = src->gid;
1135 dst->n_groups = src->n_groups;
1136 if (src->n_groups != 0) {
1137 dst->groups = (gid_t *)TALLOC_MEMDUP(
1138 dst, src->groups, sizeof(gid_t)*dst->n_groups);
1144 dst->ptok = dup_nt_token(dst, src->ptok);
1151 dst->user_session_key = data_blob_talloc( dst, src->user_session_key.data,
1152 src->user_session_key.length);
1154 dst->lm_session_key = data_blob_talloc(dst, src->lm_session_key.data,
1155 src->lm_session_key.length);
1157 dst->sam_account = samu_new(NULL);
1158 if (!dst->sam_account) {
1163 if (!pdb_copy_sam_account(dst->sam_account, src->sam_account)) {
1168 dst->pam_handle = NULL;
1169 dst->unix_name = talloc_strdup(dst, src->unix_name);
1170 if (!dst->unix_name) {
1178 static auth_serversupplied_info *guest_info = NULL;
1180 bool init_guest_info(void)
1182 if (guest_info != NULL)
1185 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info));
1188 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
1190 *server_info = copy_serverinfo(guest_info);
1191 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1194 bool copy_current_user(struct current_user *dst, struct current_user *src)
1197 NT_USER_TOKEN *nt_token;
1199 groups = (gid_t *)memdup(src->ut.groups,
1200 sizeof(gid_t) * src->ut.ngroups);
1201 if ((src->ut.ngroups != 0) && (groups == NULL)) {
1205 nt_token = dup_nt_token(NULL, src->nt_user_token);
1206 if (nt_token == NULL) {
1211 dst->conn = src->conn;
1212 dst->vuid = src->vuid;
1213 dst->ut.uid = src->ut.uid;
1214 dst->ut.gid = src->ut.gid;
1215 dst->ut.ngroups = src->ut.ngroups;
1216 dst->ut.groups = groups;
1217 dst->nt_user_token = nt_token;
1221 bool set_current_user_guest(struct current_user *dst)
1224 NT_USER_TOKEN *nt_token;
1226 groups = (gid_t *)memdup(guest_info->groups,
1227 sizeof(gid_t) * guest_info->n_groups);
1228 if (groups == NULL) {
1232 nt_token = dup_nt_token(NULL, guest_info->ptok);
1233 if (nt_token == NULL) {
1238 TALLOC_FREE(dst->nt_user_token);
1239 SAFE_FREE(dst->ut.groups);
1241 /* dst->conn is never really dereferenced, it's only tested for
1242 * equality in uid.c */
1245 dst->vuid = UID_FIELD_INVALID;
1246 dst->ut.uid = guest_info->uid;
1247 dst->ut.gid = guest_info->gid;
1248 dst->ut.ngroups = guest_info->n_groups;
1249 dst->ut.groups = groups;
1250 dst->nt_user_token = nt_token;
1254 /***************************************************************************
1255 Purely internal function for make_server_info_info3
1256 Fill the sam account from getpwnam
1257 ***************************************************************************/
1258 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx,
1260 const char *username,
1261 char **found_username,
1262 uid_t *uid, gid_t *gid,
1263 struct samu *account,
1264 bool *username_was_mapped)
1267 fstring dom_user, lower_username;
1268 fstring real_username;
1269 struct passwd *passwd;
1271 fstrcpy( lower_username, username );
1272 strlower_m( lower_username );
1274 fstr_sprintf(dom_user, "%s%c%s", domain, *lp_winbind_separator(),
1277 /* Get the passwd struct. Try to create the account is necessary. */
1279 *username_was_mapped = map_username( dom_user );
1281 if ( !(passwd = smb_getpwnam( NULL, dom_user, real_username, True )) )
1282 return NT_STATUS_NO_SUCH_USER;
1284 *uid = passwd->pw_uid;
1285 *gid = passwd->pw_gid;
1287 /* This is pointless -- there is no suport for differing
1288 unix and windows names. Make sure to always store the
1289 one we actually looked up and succeeded. Have I mentioned
1290 why I hate the 'winbind use default domain' parameter?
1293 *found_username = talloc_strdup( mem_ctx, real_username );
1295 DEBUG(5,("fill_sam_account: located username was [%s]\n", *found_username));
1297 nt_status = samu_set_unix( account, passwd );
1299 TALLOC_FREE(passwd);
1304 /****************************************************************************
1305 Wrapper to allow the getpwnam() call to strip the domain name and
1306 try again in case a local UNIX user is already there. Also run through
1307 the username if we fallback to the username only.
1308 ****************************************************************************/
1310 struct passwd *smb_getpwnam( TALLOC_CTX *mem_ctx, char *domuser,
1311 fstring save_username, bool create )
1313 struct passwd *pw = NULL;
1317 /* we only save a copy of the username it has been mangled
1318 by winbindd use default domain */
1320 save_username[0] = '\0';
1322 /* don't call map_username() here since it has to be done higher
1323 up the stack so we don't call it mutliple times */
1325 fstrcpy( username, domuser );
1327 p = strchr_m( username, *lp_winbind_separator() );
1329 /* code for a DOMAIN\user string */
1332 fstring strip_username;
1334 pw = Get_Pwnam_alloc( mem_ctx, domuser );
1336 /* make sure we get the case of the username correct */
1337 /* work around 'winbind use default domain = yes' */
1339 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1342 /* split the domain and username into 2 strings */
1346 fstr_sprintf(save_username, "%s%c%s", domain, *lp_winbind_separator(), pw->pw_name);
1349 fstrcpy( save_username, pw->pw_name );
1355 /* setup for lookup of just the username */
1356 /* remember that p and username are overlapping memory */
1359 fstrcpy( strip_username, p );
1360 fstrcpy( username, strip_username );
1363 /* just lookup a plain username */
1365 pw = Get_Pwnam_alloc(mem_ctx, username);
1367 /* Create local user if requested but only if winbindd
1368 is not running. We need to protect against cases
1369 where winbindd is failing and then prematurely
1370 creating users in /etc/passwd */
1372 if ( !pw && create && !winbind_ping() ) {
1373 /* Don't add a machine account. */
1374 if (username[strlen(username)-1] == '$')
1377 smb_create_user(NULL, username, NULL);
1378 pw = Get_Pwnam_alloc(mem_ctx, username);
1381 /* one last check for a valid passwd struct */
1384 fstrcpy( save_username, pw->pw_name );
1389 /***************************************************************************
1390 Make a server_info struct from the info3 returned by a domain logon
1391 ***************************************************************************/
1393 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1394 const char *sent_nt_username,
1396 auth_serversupplied_info **server_info,
1397 NET_USER_INFO_3 *info3)
1399 static const char zeros[16] = { 0, };
1401 NTSTATUS nt_status = NT_STATUS_OK;
1402 char *found_username;
1403 const char *nt_domain;
1404 const char *nt_username;
1405 struct samu *sam_account = NULL;
1408 bool username_was_mapped;
1413 auth_serversupplied_info *result;
1416 Here is where we should check the list of
1417 trusted domains, and verify that the SID
1421 sid_copy(&user_sid, &info3->dom_sid.sid);
1422 if (!sid_append_rid(&user_sid, info3->user_rid)) {
1423 return NT_STATUS_INVALID_PARAMETER;
1426 sid_copy(&group_sid, &info3->dom_sid.sid);
1427 if (!sid_append_rid(&group_sid, info3->group_rid)) {
1428 return NT_STATUS_INVALID_PARAMETER;
1431 if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
1432 /* If the server didn't give us one, just use the one we sent
1434 nt_username = sent_nt_username;
1437 if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
1438 /* If the server didn't give us one, just use the one we sent
1443 /* try to fill the SAM account.. If getpwnam() fails, then try the
1444 add user script (2.2.x behavior).
1446 We use the _unmapped_ username here in an attempt to provide
1447 consistent username mapping behavior between kerberos and NTLM[SSP]
1448 authentication in domain mode security. I.E. Username mapping
1449 should be applied to the fully qualified username
1450 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1451 called map_username() unnecessarily in make_user_info_map() but
1452 that is how the current code is designed. Making the change here
1453 is the least disruptive place. -- jerry */
1455 if ( !(sam_account = samu_new( NULL )) ) {
1456 return NT_STATUS_NO_MEMORY;
1459 /* this call will try to create the user if necessary */
1461 nt_status = fill_sam_account(mem_ctx, nt_domain, sent_nt_username,
1462 &found_username, &uid, &gid, sam_account,
1463 &username_was_mapped);
1466 /* if we still don't have a valid unix account check for
1467 'map to guest = bad uid' */
1469 if (!NT_STATUS_IS_OK(nt_status)) {
1470 TALLOC_FREE( sam_account );
1471 if ( lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID ) {
1472 make_server_info_guest(server_info);
1473 return NT_STATUS_OK;
1478 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1479 TALLOC_FREE(sam_account);
1480 return NT_STATUS_NO_MEMORY;
1483 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1484 TALLOC_FREE(sam_account);
1485 return NT_STATUS_NO_MEMORY;
1488 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1489 TALLOC_FREE(sam_account);
1490 return NT_STATUS_NO_MEMORY;
1493 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1494 TALLOC_FREE(sam_account);
1495 return NT_STATUS_UNSUCCESSFUL;
1498 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1499 TALLOC_FREE(sam_account);
1500 return NT_STATUS_UNSUCCESSFUL;
1503 if (!pdb_set_fullname(sam_account,
1504 unistr2_static(&(info3->uni_full_name)),
1506 TALLOC_FREE(sam_account);
1507 return NT_STATUS_NO_MEMORY;
1510 if (!pdb_set_logon_script(sam_account,
1511 unistr2_static(&(info3->uni_logon_script)),
1513 TALLOC_FREE(sam_account);
1514 return NT_STATUS_NO_MEMORY;
1517 if (!pdb_set_profile_path(sam_account,
1518 unistr2_static(&(info3->uni_profile_path)),
1520 TALLOC_FREE(sam_account);
1521 return NT_STATUS_NO_MEMORY;
1524 if (!pdb_set_homedir(sam_account,
1525 unistr2_static(&(info3->uni_home_dir)),
1527 TALLOC_FREE(sam_account);
1528 return NT_STATUS_NO_MEMORY;
1531 if (!pdb_set_dir_drive(sam_account,
1532 unistr2_static(&(info3->uni_dir_drive)),
1534 TALLOC_FREE(sam_account);
1535 return NT_STATUS_NO_MEMORY;
1538 if (!pdb_set_acct_ctrl(sam_account, info3->acct_flags, PDB_CHANGED)) {
1539 TALLOC_FREE(sam_account);
1540 return NT_STATUS_NO_MEMORY;
1543 if (!pdb_set_pass_last_set_time(
1545 nt_time_to_unix(info3->pass_last_set_time),
1547 TALLOC_FREE(sam_account);
1548 return NT_STATUS_NO_MEMORY;
1551 if (!pdb_set_pass_can_change_time(
1553 nt_time_to_unix(info3->pass_can_change_time),
1555 TALLOC_FREE(sam_account);
1556 return NT_STATUS_NO_MEMORY;
1559 if (!pdb_set_pass_must_change_time(
1561 nt_time_to_unix(info3->pass_must_change_time),
1563 TALLOC_FREE(sam_account);
1564 return NT_STATUS_NO_MEMORY;
1567 result = make_server_info(NULL);
1568 if (result == NULL) {
1569 DEBUG(4, ("make_server_info failed!\n"));
1570 TALLOC_FREE(sam_account);
1571 return NT_STATUS_NO_MEMORY;
1574 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1575 valid struct samu) */
1577 result->sam_account = sam_account;
1578 result->unix_name = talloc_strdup(result, found_username);
1580 /* Fill in the unix info we found on the way */
1585 /* Create a 'combined' list of all SIDs we might want in the SD */
1587 result->num_sids = 0;
1588 result->sids = NULL;
1590 nt_status = sid_array_from_info3(result, info3,
1594 if (!NT_STATUS_IS_OK(nt_status)) {
1595 TALLOC_FREE(result);
1599 result->login_server = unistr2_tdup(result,
1600 &(info3->uni_logon_srv));
1602 /* ensure we are never given NULL session keys */
1604 if (memcmp(info3->user_sess_key, zeros, sizeof(zeros)) == 0) {
1605 result->user_session_key = data_blob_null;
1607 result->user_session_key = data_blob_talloc(
1608 result, info3->user_sess_key,
1609 sizeof(info3->user_sess_key));
1612 if (memcmp(info3->lm_sess_key, zeros, 8) == 0) {
1613 result->lm_session_key = data_blob_null;
1615 result->lm_session_key = data_blob_talloc(
1616 result, info3->lm_sess_key,
1617 sizeof(info3->lm_sess_key));
1620 result->was_mapped = username_was_mapped;
1622 *server_info = result;
1624 return NT_STATUS_OK;
1627 /***************************************************************************
1628 Free a user_info struct
1629 ***************************************************************************/
1631 void free_user_info(auth_usersupplied_info **user_info)
1633 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1634 if (*user_info != NULL) {
1635 if ((*user_info)->smb_name) {
1636 DEBUG(10,("structure was created for %s\n",
1637 (*user_info)->smb_name));
1639 SAFE_FREE((*user_info)->smb_name);
1640 SAFE_FREE((*user_info)->internal_username);
1641 SAFE_FREE((*user_info)->client_domain);
1642 SAFE_FREE((*user_info)->domain);
1643 SAFE_FREE((*user_info)->wksta_name);
1644 data_blob_free(&(*user_info)->lm_resp);
1645 data_blob_free(&(*user_info)->nt_resp);
1646 data_blob_clear_free(&(*user_info)->lm_interactive_pwd);
1647 data_blob_clear_free(&(*user_info)->nt_interactive_pwd);
1648 data_blob_clear_free(&(*user_info)->plaintext_password);
1649 ZERO_STRUCT(**user_info);
1651 SAFE_FREE(*user_info);
1654 /***************************************************************************
1655 Make an auth_methods struct
1656 ***************************************************************************/
1658 bool make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
1660 if (!auth_context) {
1661 smb_panic("no auth_context supplied to "
1662 "make_auth_methods()!\n");
1666 smb_panic("make_auth_methods: pointer to auth_method pointer "
1670 *auth_method = TALLOC_P(auth_context->mem_ctx, auth_methods);
1671 if (!*auth_method) {
1672 DEBUG(0,("make_auth_method: malloc failed!\n"));
1675 ZERO_STRUCTP(*auth_method);
1681 * Verify whether or not given domain is trusted.
1683 * @param domain_name name of the domain to be verified
1684 * @return true if domain is one of the trusted once or
1685 * false if otherwise
1688 bool is_trusted_domain(const char* dom_name)
1690 DOM_SID trustdom_sid;
1693 /* no trusted domains for a standalone server */
1695 if ( lp_server_role() == ROLE_STANDALONE )
1698 /* if we are a DC, then check for a direct trust relationships */
1702 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
1703 "[%s]\n", dom_name ));
1704 ret = pdb_get_trusteddom_pw(dom_name, NULL, NULL, NULL);
1712 /* If winbind is around, ask it */
1714 result = wb_is_trusted_domain(dom_name);
1716 if (result == NSS_STATUS_SUCCESS) {
1720 if (result == NSS_STATUS_NOTFOUND) {
1721 /* winbind could not find the domain */
1725 /* The only other possible result is that winbind is not up
1726 and running. We need to update the trustdom_cache
1729 update_trustdom_cache();
1732 /* now the trustdom cache should be available a DC could still
1733 * have a transitive trust so fall back to the cache of trusted
1734 * domains (like a domain member would use */
1736 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {