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 Ensure primary group SID is always at position 0 in a
31 auth_serversupplied_info struct.
32 ****************************************************************************/
34 static void sort_sid_array_for_smbd(auth_serversupplied_info *result,
35 const DOM_SID *pgroup_sid)
43 if (sid_compare(&result->sids[0], pgroup_sid)==0) {
47 for (i = 1; i < result->num_sids; i++) {
48 if (sid_compare(pgroup_sid,
49 &result->sids[i]) == 0) {
50 sid_copy(&result->sids[i], &result->sids[0]);
51 sid_copy(&result->sids[0], pgroup_sid);
57 /****************************************************************************
58 Create a UNIX user on demand.
59 ****************************************************************************/
61 static int _smb_create_user(const char *domain, const char *unix_username, const char *homedir)
63 TALLOC_CTX *ctx = talloc_tos();
67 add_script = talloc_strdup(ctx, lp_adduser_script());
68 if (!add_script || !*add_script) {
71 add_script = talloc_all_string_sub(ctx,
79 add_script = talloc_all_string_sub(ctx,
88 add_script = talloc_all_string_sub(ctx,
96 ret = smbrun(add_script,NULL);
99 ("smb_create_user: Running the command `%s' gave %d\n",
104 /****************************************************************************
105 Create an auth_usersupplied_data structure
106 ****************************************************************************/
108 static NTSTATUS make_user_info(auth_usersupplied_info **user_info,
109 const char *smb_name,
110 const char *internal_username,
111 const char *client_domain,
113 const char *wksta_name,
114 DATA_BLOB *lm_pwd, DATA_BLOB *nt_pwd,
115 DATA_BLOB *lm_interactive_pwd, DATA_BLOB *nt_interactive_pwd,
116 DATA_BLOB *plaintext,
120 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
122 *user_info = SMB_MALLOC_P(auth_usersupplied_info);
123 if (*user_info == NULL) {
124 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
125 return NT_STATUS_NO_MEMORY;
128 ZERO_STRUCTP(*user_info);
130 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
132 (*user_info)->smb_name = SMB_STRDUP(smb_name);
133 if ((*user_info)->smb_name == NULL) {
134 free_user_info(user_info);
135 return NT_STATUS_NO_MEMORY;
138 (*user_info)->internal_username = SMB_STRDUP(internal_username);
139 if ((*user_info)->internal_username == NULL) {
140 free_user_info(user_info);
141 return NT_STATUS_NO_MEMORY;
144 (*user_info)->domain = SMB_STRDUP(domain);
145 if ((*user_info)->domain == NULL) {
146 free_user_info(user_info);
147 return NT_STATUS_NO_MEMORY;
150 (*user_info)->client_domain = SMB_STRDUP(client_domain);
151 if ((*user_info)->client_domain == NULL) {
152 free_user_info(user_info);
153 return NT_STATUS_NO_MEMORY;
156 (*user_info)->wksta_name = SMB_STRDUP(wksta_name);
157 if ((*user_info)->wksta_name == NULL) {
158 free_user_info(user_info);
159 return NT_STATUS_NO_MEMORY;
162 DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
165 (*user_info)->lm_resp = data_blob(lm_pwd->data, lm_pwd->length);
167 (*user_info)->nt_resp = data_blob(nt_pwd->data, nt_pwd->length);
168 if (lm_interactive_pwd)
169 (*user_info)->lm_interactive_pwd = data_blob(lm_interactive_pwd->data, lm_interactive_pwd->length);
170 if (nt_interactive_pwd)
171 (*user_info)->nt_interactive_pwd = data_blob(nt_interactive_pwd->data, nt_interactive_pwd->length);
174 (*user_info)->plaintext_password = data_blob(plaintext->data, plaintext->length);
176 (*user_info)->encrypted = encrypted;
178 (*user_info)->logon_parameters = 0;
180 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
185 /****************************************************************************
186 Create an auth_usersupplied_data structure after appropriate mapping.
187 ****************************************************************************/
189 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info,
190 const char *smb_name,
191 const char *client_domain,
192 const char *wksta_name,
195 DATA_BLOB *lm_interactive_pwd,
196 DATA_BLOB *nt_interactive_pwd,
197 DATA_BLOB *plaintext,
203 fstring internal_username;
204 fstrcpy(internal_username, smb_name);
205 was_mapped = map_username(internal_username);
207 DEBUG(5, ("Mapping user [%s]\\[%s] from workstation [%s]\n",
208 client_domain, smb_name, wksta_name));
210 /* don't allow "" as a domain, fixes a Win9X bug
211 where it doens't supply a domain for logon script
212 'net use' commands. */
214 if ( *client_domain )
215 domain = client_domain;
217 domain = lp_workgroup();
219 /* If you connect to a Windows domain member using a bogus domain name,
220 * the Windows box will map the BOGUS\user to SAMNAME\user. Thus, if
221 * the Windows box is a DC the name will become DOMAIN\user and be
222 * authenticated against AD, if the Windows box is a member server but
223 * not a DC the name will become WORKSTATION\user. A standalone
224 * non-domain member box will also map to WORKSTATION\user. */
226 if (!is_trusted_domain(domain) &&
227 !strequal(domain, get_global_sam_name()) )
229 if (lp_map_untrusted_to_domain())
230 domain = my_sam_name();
232 domain = get_global_sam_name();
233 DEBUG(5, ("Mapped domain from [%s] to [%s] for user [%s] from "
234 "workstation [%s]\n",
235 client_domain, domain, smb_name, wksta_name));
238 /* We know that the given domain is trusted (and we are allowing them),
239 * it is our global SAM name, or for legacy behavior it is our
240 * primary domain name */
242 result = make_user_info(user_info, smb_name, internal_username,
243 client_domain, domain, wksta_name,
245 lm_interactive_pwd, nt_interactive_pwd,
246 plaintext, encrypted);
247 if (NT_STATUS_IS_OK(result)) {
248 (*user_info)->was_mapped = was_mapped;
253 /****************************************************************************
254 Create an auth_usersupplied_data, making the DATA_BLOBs here.
255 Decrypt and encrypt the passwords.
256 ****************************************************************************/
258 bool make_user_info_netlogon_network(auth_usersupplied_info **user_info,
259 const char *smb_name,
260 const char *client_domain,
261 const char *wksta_name,
262 uint32 logon_parameters,
263 const uchar *lm_network_pwd,
265 const uchar *nt_network_pwd,
270 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
271 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
273 status = make_user_info_map(user_info,
274 smb_name, client_domain,
276 lm_pwd_len ? &lm_blob : NULL,
277 nt_pwd_len ? &nt_blob : NULL,
281 if (NT_STATUS_IS_OK(status)) {
282 (*user_info)->logon_parameters = logon_parameters;
284 ret = NT_STATUS_IS_OK(status) ? True : False;
286 data_blob_free(&lm_blob);
287 data_blob_free(&nt_blob);
291 /****************************************************************************
292 Create an auth_usersupplied_data, making the DATA_BLOBs here.
293 Decrypt and encrypt the passwords.
294 ****************************************************************************/
296 bool make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
297 const char *smb_name,
298 const char *client_domain,
299 const char *wksta_name,
300 uint32 logon_parameters,
302 const uchar lm_interactive_pwd[16],
303 const uchar nt_interactive_pwd[16],
304 const uchar *dc_sess_key)
306 unsigned char lm_pwd[16];
307 unsigned char nt_pwd[16];
308 unsigned char local_lm_response[24];
309 unsigned char local_nt_response[24];
310 unsigned char key[16];
313 memcpy(key, dc_sess_key, 8);
315 if (lm_interactive_pwd)
316 memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
318 if (nt_interactive_pwd)
319 memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
321 #ifdef DEBUG_PASSWORD
323 dump_data(100, key, sizeof(key));
325 DEBUG(100,("lm owf password:"));
326 dump_data(100, lm_pwd, sizeof(lm_pwd));
328 DEBUG(100,("nt owf password:"));
329 dump_data(100, nt_pwd, sizeof(nt_pwd));
332 if (lm_interactive_pwd)
333 SamOEMhash(lm_pwd, key, sizeof(lm_pwd));
335 if (nt_interactive_pwd)
336 SamOEMhash(nt_pwd, key, sizeof(nt_pwd));
338 #ifdef DEBUG_PASSWORD
339 DEBUG(100,("decrypt of lm owf password:"));
340 dump_data(100, lm_pwd, sizeof(lm_pwd));
342 DEBUG(100,("decrypt of nt owf password:"));
343 dump_data(100, nt_pwd, sizeof(nt_pwd));
346 if (lm_interactive_pwd)
347 SMBOWFencrypt(lm_pwd, chal,
350 if (nt_interactive_pwd)
351 SMBOWFencrypt(nt_pwd, chal,
354 /* Password info paranoia */
360 DATA_BLOB local_lm_blob;
361 DATA_BLOB local_nt_blob;
363 DATA_BLOB lm_interactive_blob;
364 DATA_BLOB nt_interactive_blob;
366 if (lm_interactive_pwd) {
367 local_lm_blob = data_blob(local_lm_response,
368 sizeof(local_lm_response));
369 lm_interactive_blob = data_blob(lm_pwd,
374 if (nt_interactive_pwd) {
375 local_nt_blob = data_blob(local_nt_response,
376 sizeof(local_nt_response));
377 nt_interactive_blob = data_blob(nt_pwd,
382 nt_status = make_user_info_map(
384 smb_name, client_domain, wksta_name,
385 lm_interactive_pwd ? &local_lm_blob : NULL,
386 nt_interactive_pwd ? &local_nt_blob : NULL,
387 lm_interactive_pwd ? &lm_interactive_blob : NULL,
388 nt_interactive_pwd ? &nt_interactive_blob : NULL,
391 if (NT_STATUS_IS_OK(nt_status)) {
392 (*user_info)->logon_parameters = logon_parameters;
395 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
396 data_blob_free(&local_lm_blob);
397 data_blob_free(&local_nt_blob);
398 data_blob_free(&lm_interactive_blob);
399 data_blob_free(&nt_interactive_blob);
405 /****************************************************************************
406 Create an auth_usersupplied_data structure
407 ****************************************************************************/
409 bool make_user_info_for_reply(auth_usersupplied_info **user_info,
410 const char *smb_name,
411 const char *client_domain,
413 DATA_BLOB plaintext_password)
416 DATA_BLOB local_lm_blob;
417 DATA_BLOB local_nt_blob;
418 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
421 * Not encrypted - do so.
424 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
427 if (plaintext_password.data) {
428 unsigned char local_lm_response[24];
430 #ifdef DEBUG_PASSWORD
431 DEBUG(10,("Unencrypted password (len %d):\n",
432 (int)plaintext_password.length));
433 dump_data(100, plaintext_password.data,
434 plaintext_password.length);
437 SMBencrypt( (const char *)plaintext_password.data,
438 (const uchar*)chal, local_lm_response);
439 local_lm_blob = data_blob(local_lm_response, 24);
441 /* We can't do an NT hash here, as the password needs to be
443 local_nt_blob = data_blob_null;
446 local_lm_blob = data_blob_null;
447 local_nt_blob = data_blob_null;
450 ret = make_user_info_map(
451 user_info, smb_name, client_domain,
452 get_remote_machine_name(),
453 local_lm_blob.data ? &local_lm_blob : NULL,
454 local_nt_blob.data ? &local_nt_blob : NULL,
456 plaintext_password.data ? &plaintext_password : NULL,
459 data_blob_free(&local_lm_blob);
460 return NT_STATUS_IS_OK(ret) ? True : False;
463 /****************************************************************************
464 Create an auth_usersupplied_data structure
465 ****************************************************************************/
467 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
468 const char *smb_name,
469 const char *client_domain,
470 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
472 return make_user_info_map(user_info, smb_name,
474 get_remote_machine_name(),
475 lm_resp.data ? &lm_resp : NULL,
476 nt_resp.data ? &nt_resp : NULL,
481 /****************************************************************************
482 Create a guest user_info blob, for anonymous authenticaion.
483 ****************************************************************************/
485 bool make_user_info_guest(auth_usersupplied_info **user_info)
489 nt_status = make_user_info(user_info,
498 return NT_STATUS_IS_OK(nt_status) ? True : False;
501 static int server_info_dtor(auth_serversupplied_info *server_info)
503 TALLOC_FREE(server_info->sam_account);
504 ZERO_STRUCTP(server_info);
508 /***************************************************************************
509 Make a server_info struct. Free with TALLOC_FREE().
510 ***************************************************************************/
512 static auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
514 struct auth_serversupplied_info *result;
516 result = TALLOC_ZERO_P(mem_ctx, auth_serversupplied_info);
517 if (result == NULL) {
518 DEBUG(0, ("talloc failed\n"));
522 talloc_set_destructor(result, server_info_dtor);
524 /* Initialise the uid and gid values to something non-zero
525 which may save us from giving away root access if there
526 is a bug in allocating these fields. */
528 result->utok.uid = -1;
529 result->utok.gid = -1;
533 static char *sanitize_username(TALLOC_CTX *mem_ctx, const char *username)
537 alpha_strcpy(tmp, username, ". _-$", sizeof(tmp));
538 return talloc_strdup(mem_ctx, tmp);
541 /***************************************************************************
542 Is the incoming username our own machine account ?
543 If so, the connection is almost certainly from winbindd.
544 ***************************************************************************/
546 static bool is_our_machine_account(const char *username)
549 char *truncname = NULL;
550 size_t ulen = strlen(username);
552 if (ulen == 0 || username[ulen-1] != '$') {
555 truncname = SMB_STRDUP(username);
559 truncname[ulen-1] = '\0';
560 ret = strequal(truncname, global_myname());
561 SAFE_FREE(truncname);
565 /***************************************************************************
566 Make (and fill) a user_info struct from a struct samu
567 ***************************************************************************/
569 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info,
570 struct samu *sampass)
574 auth_serversupplied_info *result;
575 const char *username = pdb_get_username(sampass);
578 if ( !(result = make_server_info(NULL)) ) {
579 return NT_STATUS_NO_MEMORY;
582 if ( !(pwd = getpwnam_alloc(result, username)) ) {
583 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
584 pdb_get_username(sampass)));
586 return NT_STATUS_NO_SUCH_USER;
589 result->sam_account = sampass;
590 result->unix_name = pwd->pw_name;
591 /* Ensure that we keep pwd->pw_name, because we will free pwd below */
592 talloc_steal(result, pwd->pw_name);
593 result->utok.gid = pwd->pw_gid;
594 result->utok.uid = pwd->pw_uid;
598 result->sanitized_username = sanitize_username(result,
600 if (result->sanitized_username == NULL) {
602 return NT_STATUS_NO_MEMORY;
605 if (IS_DC && is_our_machine_account(username)) {
607 * Ensure for a connection from our own
608 * machine account (from winbindd on a DC)
609 * there are no supplementary groups.
610 * Prevents loops in calling gid_to_sid().
614 result->num_sids = 0;
617 * This is a hack of monstrous proportions.
618 * If we know it's winbindd talking to us,
619 * we know we must never recurse into it,
620 * so turn off contacting winbindd for this
621 * entire process. This will get fixed when
622 * winbindd doesn't need to talk to smbd on
628 DEBUG(10, ("make_server_info_sam: our machine account %s "
629 "setting supplementary group list empty and "
630 "turning off winbindd requests.\n",
633 status = pdb_enum_group_memberships(result, sampass,
634 &result->sids, &gids,
637 if (!NT_STATUS_IS_OK(status)) {
638 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
640 result->sam_account = NULL; /* Don't free on error exit. */
646 /* For now we throw away the gids and convert via sid_to_gid
647 * later. This needs fixing, but I'd like to get the code straight and
652 DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
653 pdb_get_username(sampass), result->unix_name));
655 *server_info = result;
656 /* Ensure that the sampass will be freed with the result */
657 talloc_steal(result, sampass);
662 static NTSTATUS log_nt_token(NT_USER_TOKEN *token)
664 TALLOC_CTX *frame = talloc_stackframe();
669 if ((lp_log_nt_token_command() == NULL) ||
670 (strlen(lp_log_nt_token_command()) == 0)) {
675 group_sidstr = talloc_strdup(frame, "");
676 for (i=1; i<token->num_sids; i++) {
677 group_sidstr = talloc_asprintf(
678 frame, "%s %s", group_sidstr,
679 sid_string_talloc(frame, &token->user_sids[i]));
682 command = talloc_string_sub(
683 frame, lp_log_nt_token_command(),
684 "%s", sid_string_talloc(frame, &token->user_sids[0]));
685 command = talloc_string_sub(frame, command, "%t", group_sidstr);
687 if (command == NULL) {
689 return NT_STATUS_NO_MEMORY;
692 DEBUG(8, ("running command: [%s]\n", command));
693 if (smbrun(command, NULL) != 0) {
694 DEBUG(0, ("Could not log NT token\n"));
696 return NT_STATUS_ACCESS_DENIED;
704 * Create the token to use from server_info->sam_account and
705 * server_info->sids (the info3/sam groups). Find the unix gids.
708 NTSTATUS create_local_token(auth_serversupplied_info *server_info)
712 struct dom_sid tmp_sid;
715 * If winbind is not around, we can not make much use of the SIDs the
716 * domain controller provided us with. Likewise if the user name was
717 * mapped to some local unix user.
720 if (((lp_server_role() == ROLE_DOMAIN_MEMBER) && !winbind_ping()) ||
721 (server_info->nss_token)) {
722 status = create_token_from_username(server_info,
723 server_info->unix_name,
725 &server_info->utok.uid,
726 &server_info->utok.gid,
727 &server_info->unix_name,
731 server_info->ptok = create_local_nt_token(
733 pdb_get_user_sid(server_info->sam_account),
735 server_info->num_sids, server_info->sids);
736 status = server_info->ptok ?
737 NT_STATUS_OK : NT_STATUS_NO_SUCH_USER;
740 if (!NT_STATUS_IS_OK(status)) {
744 /* Convert the SIDs to gids. */
746 server_info->utok.ngroups = 0;
747 server_info->utok.groups = NULL;
749 /* Start at index 1, where the groups start. */
751 for (i=1; i<server_info->ptok->num_sids; i++) {
753 DOM_SID *sid = &server_info->ptok->user_sids[i];
755 if (!sid_to_gid(sid, &gid)) {
756 DEBUG(10, ("Could not convert SID %s to gid, "
757 "ignoring it\n", sid_string_dbg(sid)));
760 add_gid_to_array_unique(server_info, gid,
761 &server_info->utok.groups,
762 &server_info->utok.ngroups);
766 * Add the "Unix Group" SID for each gid to catch mapped groups
767 * and their Unix equivalent. This is to solve the backwards
768 * compatibility problem of 'valid users = +ntadmin' where
769 * ntadmin has been paired with "Domain Admins" in the group
770 * mapping table. Otherwise smb.conf would need to be changed
771 * to 'valid user = "Domain Admins"'. --jerry
773 * For consistency we also add the "Unix User" SID,
774 * so that the complete unix token is represented within
778 if (!uid_to_unix_users_sid(server_info->utok.uid, &tmp_sid)) {
779 DEBUG(1,("create_local_token: Failed to create SID "
780 "for uid %d!\n", server_info->utok.uid));
782 add_sid_to_array_unique(server_info->ptok, &tmp_sid,
783 &server_info->ptok->user_sids,
784 &server_info->ptok->num_sids);
786 for ( i=0; i<server_info->utok.ngroups; i++ ) {
787 if (!gid_to_unix_groups_sid( server_info->utok.groups[i], &tmp_sid ) ) {
788 DEBUG(1,("create_local_token: Failed to create SID "
789 "for gid %d!\n", server_info->utok.groups[i]));
792 add_sid_to_array_unique(server_info->ptok, &tmp_sid,
793 &server_info->ptok->user_sids,
794 &server_info->ptok->num_sids);
797 debug_nt_user_token(DBGC_AUTH, 10, server_info->ptok);
798 debug_unix_user_token(DBGC_AUTH, 10,
799 server_info->utok.uid,
800 server_info->utok.gid,
801 server_info->utok.ngroups,
802 server_info->utok.groups);
804 status = log_nt_token(server_info->ptok);
809 * Create an artificial NT token given just a username. (Initially intended
812 * We go through lookup_name() to avoid problems we had with 'winbind use
817 * unmapped unix users: Go directly to nss to find the user's group.
819 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
821 * If the user is provided by winbind, the primary gid is set to "domain
822 * users" of the user's domain. For an explanation why this is necessary, see
823 * the thread starting at
824 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
827 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
829 uid_t *uid, gid_t *gid,
830 char **found_username,
831 struct nt_user_token **token)
833 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
836 enum lsa_SidType type;
839 DOM_SID unix_group_sid;
840 size_t num_group_sids;
844 tmp_ctx = talloc_new(NULL);
845 if (tmp_ctx == NULL) {
846 DEBUG(0, ("talloc_new failed\n"));
847 return NT_STATUS_NO_MEMORY;
850 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
851 NULL, NULL, &user_sid, &type)) {
852 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
856 if (type != SID_NAME_USER) {
857 DEBUG(1, ("%s is a %s, not a user\n", username,
858 sid_type_lookup(type)));
862 if (sid_check_is_in_our_domain(&user_sid)) {
865 /* This is a passdb user, so ask passdb */
867 struct samu *sam_acct = NULL;
869 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
870 result = NT_STATUS_NO_MEMORY;
875 ret = pdb_getsampwsid(sam_acct, &user_sid);
879 DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n",
880 sid_string_dbg(&user_sid), username));
881 DEBUGADD(1, ("Fall back to unix user %s\n", username));
885 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
888 if (!NT_STATUS_IS_OK(result)) {
889 DEBUG(10, ("enum_group_memberships failed for %s\n",
891 DEBUGADD(1, ("Fall back to unix user %s\n", username));
895 /* see the smb_panic() in pdb_default_enum_group_memberships */
896 SMB_ASSERT(num_group_sids > 0);
900 /* Ensure we're returning the found_username on the right context. */
901 *found_username = talloc_strdup(mem_ctx,
902 pdb_get_username(sam_acct));
904 } else if (sid_check_is_in_unix_users(&user_sid)) {
906 /* This is a unix user not in passdb. We need to ask nss
907 * directly, without consulting passdb */
912 * This goto target is used as a fallback for the passdb
913 * case. The concrete bug report is when passdb gave us an
919 if (!sid_to_uid(&user_sid, uid)) {
920 DEBUG(1, ("sid_to_uid for %s (%s) failed\n",
921 username, sid_string_dbg(&user_sid)));
925 uid_to_unix_users_sid(*uid, &user_sid);
927 pass = getpwuid_alloc(tmp_ctx, *uid);
929 DEBUG(1, ("getpwuid(%d) for user %s failed\n",
934 if (!getgroups_unix_user(tmp_ctx, username, pass->pw_gid,
935 &gids, &num_group_sids)) {
936 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
941 if (num_group_sids) {
942 group_sids = TALLOC_ARRAY(tmp_ctx, DOM_SID, num_group_sids);
943 if (group_sids == NULL) {
944 DEBUG(1, ("TALLOC_ARRAY failed\n"));
945 result = NT_STATUS_NO_MEMORY;
952 for (i=0; i<num_group_sids; i++) {
953 gid_to_sid(&group_sids[i], gids[i]);
956 /* In getgroups_unix_user we always set the primary gid */
957 SMB_ASSERT(num_group_sids > 0);
961 /* Ensure we're returning the found_username on the right context. */
962 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
965 /* This user is from winbind, force the primary gid to the
966 * user's "domain users" group. Under certain circumstances
967 * (user comes from NT4), this might be a loss of
968 * information. But we can not rely on winbind getting the
969 * correct info. AD might prohibit winbind looking up that
975 group_sids = TALLOC_ARRAY(tmp_ctx, DOM_SID, num_group_sids);
976 if (group_sids == NULL) {
977 DEBUG(1, ("TALLOC_ARRAY failed\n"));
978 result = NT_STATUS_NO_MEMORY;
982 sid_copy(&group_sids[0], &user_sid);
983 sid_split_rid(&group_sids[0], &dummy);
984 sid_append_rid(&group_sids[0], DOMAIN_GROUP_RID_USERS);
986 if (!sid_to_gid(&group_sids[0], gid)) {
987 DEBUG(1, ("sid_to_gid(%s) failed\n",
988 sid_string_dbg(&group_sids[0])));
994 /* Ensure we're returning the found_username on the right context. */
995 *found_username = talloc_strdup(mem_ctx, username);
998 /* Add the "Unix Group" SID for each gid to catch mapped groups
999 and their Unix equivalent. This is to solve the backwards
1000 compatibility problem of 'valid users = +ntadmin' where
1001 ntadmin has been paired with "Domain Admins" in the group
1002 mapping table. Otherwise smb.conf would need to be changed
1003 to 'valid user = "Domain Admins"'. --jerry */
1005 num_gids = num_group_sids;
1006 for ( i=0; i<num_gids; i++ ) {
1009 /* don't pickup anything managed by Winbind */
1011 if ( lp_idmap_gid(&low, &high) && (gids[i] >= low) && (gids[i] <= high) )
1014 if ( !gid_to_unix_groups_sid( gids[i], &unix_group_sid ) ) {
1015 DEBUG(1,("create_token_from_username: Failed to create SID "
1016 "for gid %d!\n", gids[i]));
1019 result = add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
1020 &group_sids, &num_group_sids);
1021 if (!NT_STATUS_IS_OK(result)) {
1026 /* Ensure we're creating the nt_token on the right context. */
1027 *token = create_local_nt_token(mem_ctx, &user_sid,
1028 is_guest, num_group_sids, group_sids);
1030 if ((*token == NULL) || (*found_username == NULL)) {
1031 result = NT_STATUS_NO_MEMORY;
1035 result = NT_STATUS_OK;
1037 TALLOC_FREE(tmp_ctx);
1041 /***************************************************************************
1042 Build upon create_token_from_username:
1044 Expensive helper function to figure out whether a user given its name is
1045 member of a particular group.
1046 ***************************************************************************/
1048 bool user_in_group_sid(const char *username, const DOM_SID *group_sid)
1053 char *found_username;
1054 struct nt_user_token *token;
1057 TALLOC_CTX *mem_ctx;
1059 mem_ctx = talloc_new(NULL);
1060 if (mem_ctx == NULL) {
1061 DEBUG(0, ("talloc_new failed\n"));
1065 status = create_token_from_username(mem_ctx, username, False,
1066 &uid, &gid, &found_username,
1069 if (!NT_STATUS_IS_OK(status)) {
1070 DEBUG(10, ("could not create token for %s\n", username));
1074 result = nt_token_check_sid(group_sid, token);
1076 TALLOC_FREE(mem_ctx);
1081 bool user_in_group(const char *username, const char *groupname)
1083 TALLOC_CTX *mem_ctx;
1087 mem_ctx = talloc_new(NULL);
1088 if (mem_ctx == NULL) {
1089 DEBUG(0, ("talloc_new failed\n"));
1093 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
1094 NULL, NULL, &group_sid, NULL);
1095 TALLOC_FREE(mem_ctx);
1098 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
1102 return user_in_group_sid(username, &group_sid);
1105 /***************************************************************************
1106 Make (and fill) a server_info struct from a 'struct passwd' by conversion
1108 ***************************************************************************/
1110 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info,
1111 char *unix_username,
1115 struct samu *sampass = NULL;
1117 char *qualified_name = NULL;
1118 TALLOC_CTX *mem_ctx = NULL;
1120 enum lsa_SidType type;
1121 auth_serversupplied_info *result;
1123 if ( !(sampass = samu_new( NULL )) ) {
1124 return NT_STATUS_NO_MEMORY;
1127 status = samu_set_unix( sampass, pwd );
1128 if (!NT_STATUS_IS_OK(status)) {
1132 result = make_server_info(NULL);
1133 if (result == NULL) {
1134 TALLOC_FREE(sampass);
1135 return NT_STATUS_NO_MEMORY;
1138 result->sam_account = sampass;
1140 result->unix_name = talloc_strdup(result, unix_username);
1141 result->sanitized_username = sanitize_username(result, unix_username);
1143 if ((result->unix_name == NULL)
1144 || (result->sanitized_username == NULL)) {
1145 TALLOC_FREE(sampass);
1146 TALLOC_FREE(result);
1147 return NT_STATUS_NO_MEMORY;
1150 result->utok.uid = pwd->pw_uid;
1151 result->utok.gid = pwd->pw_gid;
1153 status = pdb_enum_group_memberships(result, sampass,
1154 &result->sids, &gids,
1157 if (!NT_STATUS_IS_OK(status)) {
1158 DEBUG(10, ("pdb_enum_group_memberships failed: %s\n",
1159 nt_errstr(status)));
1160 TALLOC_FREE(result);
1165 * The SID returned in server_info->sam_account is based
1166 * on our SAM sid even though for a pure UNIX account this should
1167 * not be the case as it doesn't really exist in the SAM db.
1168 * This causes lookups on "[in]valid users" to fail as they
1169 * will lookup this name as a "Unix User" SID to check against
1170 * the user token. Fix this by adding the "Unix User"\unix_username
1171 * SID to the sid array. The correct fix should probably be
1172 * changing the server_info->sam_account user SID to be a
1173 * S-1-22 Unix SID, but this might break old configs where
1174 * plaintext passwords were used with no SAM backend.
1177 mem_ctx = talloc_init("make_server_info_pw_tmp");
1179 TALLOC_FREE(result);
1180 return NT_STATUS_NO_MEMORY;
1183 qualified_name = talloc_asprintf(mem_ctx, "%s\\%s",
1184 unix_users_domain_name(),
1186 if (!qualified_name) {
1187 TALLOC_FREE(result);
1188 TALLOC_FREE(mem_ctx);
1189 return NT_STATUS_NO_MEMORY;
1192 if (!lookup_name(mem_ctx, qualified_name, LOOKUP_NAME_ALL,
1195 TALLOC_FREE(result);
1196 TALLOC_FREE(mem_ctx);
1197 return NT_STATUS_NO_SUCH_USER;
1200 TALLOC_FREE(mem_ctx);
1202 if (type != SID_NAME_USER) {
1203 TALLOC_FREE(result);
1204 return NT_STATUS_NO_SUCH_USER;
1207 status = add_sid_to_array_unique(result, &u_sid,
1210 if (!NT_STATUS_IS_OK(status)) {
1211 TALLOC_FREE(result);
1215 /* For now we throw away the gids and convert via sid_to_gid
1216 * later. This needs fixing, but I'd like to get the code straight and
1220 *server_info = result;
1222 return NT_STATUS_OK;
1225 /***************************************************************************
1226 Make (and fill) a user_info struct for a guest login.
1227 This *must* succeed for smbd to start. If there is no mapping entry for
1228 the guest gid, then create one.
1229 ***************************************************************************/
1231 static NTSTATUS make_new_server_info_guest(auth_serversupplied_info **server_info)
1234 struct samu *sampass = NULL;
1240 if ( !(sampass = samu_new( NULL )) ) {
1241 return NT_STATUS_NO_MEMORY;
1244 sid_copy(&guest_sid, get_global_sam_sid());
1245 sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
1248 ret = pdb_getsampwsid(sampass, &guest_sid);
1252 TALLOC_FREE(sampass);
1253 return NT_STATUS_NO_SUCH_USER;
1256 status = make_server_info_sam(server_info, sampass);
1257 if (!NT_STATUS_IS_OK(status)) {
1258 TALLOC_FREE(sampass);
1262 (*server_info)->guest = True;
1264 status = create_local_token(*server_info);
1265 if (!NT_STATUS_IS_OK(status)) {
1266 DEBUG(10, ("create_local_token failed: %s\n",
1267 nt_errstr(status)));
1271 /* annoying, but the Guest really does have a session key, and it is
1274 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
1275 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
1277 alpha_strcpy(tmp, pdb_get_username(sampass), ". _-$", sizeof(tmp));
1278 (*server_info)->sanitized_username = talloc_strdup(*server_info, tmp);
1280 return NT_STATUS_OK;
1283 /****************************************************************************
1284 Fake a auth_serversupplied_info just from a username
1285 ****************************************************************************/
1287 NTSTATUS make_serverinfo_from_username(TALLOC_CTX *mem_ctx,
1288 const char *username,
1290 struct auth_serversupplied_info **presult)
1292 struct auth_serversupplied_info *result;
1296 pwd = getpwnam_alloc(talloc_tos(), username);
1298 return NT_STATUS_NO_SUCH_USER;
1301 status = make_server_info_pw(&result, pwd->pw_name, pwd);
1305 if (!NT_STATUS_IS_OK(status)) {
1309 result->nss_token = true;
1310 result->guest = is_guest;
1312 status = create_local_token(result);
1314 if (!NT_STATUS_IS_OK(status)) {
1315 TALLOC_FREE(result);
1320 return NT_STATUS_OK;
1324 struct auth_serversupplied_info *copy_serverinfo(TALLOC_CTX *mem_ctx,
1325 const auth_serversupplied_info *src)
1327 auth_serversupplied_info *dst;
1329 dst = make_server_info(mem_ctx);
1334 dst->guest = src->guest;
1335 dst->utok.uid = src->utok.uid;
1336 dst->utok.gid = src->utok.gid;
1337 dst->utok.ngroups = src->utok.ngroups;
1338 if (src->utok.ngroups != 0) {
1339 dst->utok.groups = (gid_t *)TALLOC_MEMDUP(
1340 dst, src->utok.groups,
1341 sizeof(gid_t)*dst->utok.ngroups);
1343 dst->utok.groups = NULL;
1347 dst->ptok = dup_nt_token(dst, src->ptok);
1354 dst->user_session_key = data_blob_talloc( dst, src->user_session_key.data,
1355 src->user_session_key.length);
1357 dst->lm_session_key = data_blob_talloc(dst, src->lm_session_key.data,
1358 src->lm_session_key.length);
1360 dst->sam_account = samu_new(NULL);
1361 if (!dst->sam_account) {
1366 if (!pdb_copy_sam_account(dst->sam_account, src->sam_account)) {
1371 dst->pam_handle = NULL;
1372 dst->unix_name = talloc_strdup(dst, src->unix_name);
1373 if (!dst->unix_name) {
1378 dst->sanitized_username = talloc_strdup(dst, src->sanitized_username);
1379 if (!dst->sanitized_username) {
1388 * Set a new session key. Used in the rpc server where we have to override the
1389 * SMB level session key with SystemLibraryDTC
1392 bool server_info_set_session_key(struct auth_serversupplied_info *info,
1393 DATA_BLOB session_key)
1395 TALLOC_FREE(info->user_session_key.data);
1397 info->user_session_key = data_blob_talloc(
1398 info, session_key.data, session_key.length);
1400 return (info->user_session_key.data != NULL);
1403 static auth_serversupplied_info *guest_info = NULL;
1405 bool init_guest_info(void)
1407 if (guest_info != NULL)
1410 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info));
1413 NTSTATUS make_server_info_guest(TALLOC_CTX *mem_ctx,
1414 auth_serversupplied_info **server_info)
1416 *server_info = copy_serverinfo(mem_ctx, guest_info);
1417 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1420 bool copy_current_user(struct current_user *dst, struct current_user *src)
1423 NT_USER_TOKEN *nt_token;
1425 groups = (gid_t *)memdup(src->ut.groups,
1426 sizeof(gid_t) * src->ut.ngroups);
1427 if ((src->ut.ngroups != 0) && (groups == NULL)) {
1431 nt_token = dup_nt_token(NULL, src->nt_user_token);
1432 if (nt_token == NULL) {
1437 dst->conn = src->conn;
1438 dst->vuid = src->vuid;
1439 dst->ut.uid = src->ut.uid;
1440 dst->ut.gid = src->ut.gid;
1441 dst->ut.ngroups = src->ut.ngroups;
1442 dst->ut.groups = groups;
1443 dst->nt_user_token = nt_token;
1447 /***************************************************************************
1448 Purely internal function for make_server_info_info3
1449 Fill the sam account from getpwnam
1450 ***************************************************************************/
1451 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx,
1453 const char *username,
1454 char **found_username,
1455 uid_t *uid, gid_t *gid,
1456 struct samu *account,
1457 bool *username_was_mapped)
1460 fstring dom_user, lower_username;
1461 fstring real_username;
1462 struct passwd *passwd;
1464 fstrcpy( lower_username, username );
1465 strlower_m( lower_username );
1467 fstr_sprintf(dom_user, "%s%c%s", domain, *lp_winbind_separator(),
1470 /* Get the passwd struct. Try to create the account is necessary. */
1472 *username_was_mapped = map_username( dom_user );
1474 if ( !(passwd = smb_getpwnam( NULL, dom_user, real_username, True )) )
1475 return NT_STATUS_NO_SUCH_USER;
1477 *uid = passwd->pw_uid;
1478 *gid = passwd->pw_gid;
1480 /* This is pointless -- there is no suport for differing
1481 unix and windows names. Make sure to always store the
1482 one we actually looked up and succeeded. Have I mentioned
1483 why I hate the 'winbind use default domain' parameter?
1486 *found_username = talloc_strdup( mem_ctx, real_username );
1488 DEBUG(5,("fill_sam_account: located username was [%s]\n", *found_username));
1490 nt_status = samu_set_unix( account, passwd );
1492 TALLOC_FREE(passwd);
1497 /****************************************************************************
1498 Wrapper to allow the getpwnam() call to strip the domain name and
1499 try again in case a local UNIX user is already there. Also run through
1500 the username if we fallback to the username only.
1501 ****************************************************************************/
1503 struct passwd *smb_getpwnam( TALLOC_CTX *mem_ctx, char *domuser,
1504 fstring save_username, bool create )
1506 struct passwd *pw = NULL;
1510 /* we only save a copy of the username it has been mangled
1511 by winbindd use default domain */
1513 save_username[0] = '\0';
1515 /* don't call map_username() here since it has to be done higher
1516 up the stack so we don't call it mutliple times */
1518 fstrcpy( username, domuser );
1520 p = strchr_m( username, *lp_winbind_separator() );
1522 /* code for a DOMAIN\user string */
1525 fstring strip_username;
1527 pw = Get_Pwnam_alloc( mem_ctx, domuser );
1529 /* make sure we get the case of the username correct */
1530 /* work around 'winbind use default domain = yes' */
1532 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1535 /* split the domain and username into 2 strings */
1539 fstr_sprintf(save_username, "%s%c%s", domain, *lp_winbind_separator(), pw->pw_name);
1542 fstrcpy( save_username, pw->pw_name );
1548 /* setup for lookup of just the username */
1549 /* remember that p and username are overlapping memory */
1552 fstrcpy( strip_username, p );
1553 fstrcpy( username, strip_username );
1556 /* just lookup a plain username */
1558 pw = Get_Pwnam_alloc(mem_ctx, username);
1560 /* Create local user if requested but only if winbindd
1561 is not running. We need to protect against cases
1562 where winbindd is failing and then prematurely
1563 creating users in /etc/passwd */
1565 if ( !pw && create && !winbind_ping() ) {
1566 /* Don't add a machine account. */
1567 if (username[strlen(username)-1] == '$')
1570 _smb_create_user(NULL, username, NULL);
1571 pw = Get_Pwnam_alloc(mem_ctx, username);
1574 /* one last check for a valid passwd struct */
1577 fstrcpy( save_username, pw->pw_name );
1582 /***************************************************************************
1583 Make a server_info struct from the info3 returned by a domain logon
1584 ***************************************************************************/
1586 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1587 const char *sent_nt_username,
1589 auth_serversupplied_info **server_info,
1590 struct netr_SamInfo3 *info3)
1594 NTSTATUS nt_status = NT_STATUS_OK;
1595 char *found_username = NULL;
1596 const char *nt_domain;
1597 const char *nt_username;
1598 struct samu *sam_account = NULL;
1601 bool username_was_mapped;
1603 uid_t uid = (uid_t)-1;
1604 gid_t gid = (gid_t)-1;
1606 auth_serversupplied_info *result;
1609 Here is where we should check the list of
1610 trusted domains, and verify that the SID
1614 sid_copy(&user_sid, info3->base.domain_sid);
1615 if (!sid_append_rid(&user_sid, info3->base.rid)) {
1616 return NT_STATUS_INVALID_PARAMETER;
1619 sid_copy(&group_sid, info3->base.domain_sid);
1620 if (!sid_append_rid(&group_sid, info3->base.primary_gid)) {
1621 return NT_STATUS_INVALID_PARAMETER;
1624 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
1626 /* If the server didn't give us one, just use the one we sent
1628 nt_username = sent_nt_username;
1631 nt_domain = talloc_strdup(mem_ctx, info3->base.domain.string);
1633 /* If the server didn't give us one, just use the one we sent
1638 /* try to fill the SAM account.. If getpwnam() fails, then try the
1639 add user script (2.2.x behavior).
1641 We use the _unmapped_ username here in an attempt to provide
1642 consistent username mapping behavior between kerberos and NTLM[SSP]
1643 authentication in domain mode security. I.E. Username mapping
1644 should be applied to the fully qualified username
1645 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1646 called map_username() unnecessarily in make_user_info_map() but
1647 that is how the current code is designed. Making the change here
1648 is the least disruptive place. -- jerry */
1650 if ( !(sam_account = samu_new( NULL )) ) {
1651 return NT_STATUS_NO_MEMORY;
1654 /* this call will try to create the user if necessary */
1656 nt_status = fill_sam_account(mem_ctx, nt_domain, sent_nt_username,
1657 &found_username, &uid, &gid, sam_account,
1658 &username_was_mapped);
1661 /* if we still don't have a valid unix account check for
1662 'map to guest = bad uid' */
1664 if (!NT_STATUS_IS_OK(nt_status)) {
1665 TALLOC_FREE( sam_account );
1666 if ( lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID ) {
1667 make_server_info_guest(NULL, server_info);
1668 return NT_STATUS_OK;
1673 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1674 TALLOC_FREE(sam_account);
1675 return NT_STATUS_NO_MEMORY;
1678 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1679 TALLOC_FREE(sam_account);
1680 return NT_STATUS_NO_MEMORY;
1683 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1684 TALLOC_FREE(sam_account);
1685 return NT_STATUS_NO_MEMORY;
1688 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1689 TALLOC_FREE(sam_account);
1690 return NT_STATUS_UNSUCCESSFUL;
1693 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1694 TALLOC_FREE(sam_account);
1695 return NT_STATUS_UNSUCCESSFUL;
1698 if (!pdb_set_fullname(sam_account,
1699 info3->base.full_name.string,
1701 TALLOC_FREE(sam_account);
1702 return NT_STATUS_NO_MEMORY;
1705 if (!pdb_set_logon_script(sam_account,
1706 info3->base.logon_script.string,
1708 TALLOC_FREE(sam_account);
1709 return NT_STATUS_NO_MEMORY;
1712 if (!pdb_set_profile_path(sam_account,
1713 info3->base.profile_path.string,
1715 TALLOC_FREE(sam_account);
1716 return NT_STATUS_NO_MEMORY;
1719 if (!pdb_set_homedir(sam_account,
1720 info3->base.home_directory.string,
1722 TALLOC_FREE(sam_account);
1723 return NT_STATUS_NO_MEMORY;
1726 if (!pdb_set_dir_drive(sam_account,
1727 info3->base.home_drive.string,
1729 TALLOC_FREE(sam_account);
1730 return NT_STATUS_NO_MEMORY;
1733 if (!pdb_set_acct_ctrl(sam_account, info3->base.acct_flags, PDB_CHANGED)) {
1734 TALLOC_FREE(sam_account);
1735 return NT_STATUS_NO_MEMORY;
1738 if (!pdb_set_pass_last_set_time(
1740 nt_time_to_unix(info3->base.last_password_change),
1742 TALLOC_FREE(sam_account);
1743 return NT_STATUS_NO_MEMORY;
1746 if (!pdb_set_pass_can_change_time(
1748 nt_time_to_unix(info3->base.allow_password_change),
1750 TALLOC_FREE(sam_account);
1751 return NT_STATUS_NO_MEMORY;
1754 if (!pdb_set_pass_must_change_time(
1756 nt_time_to_unix(info3->base.force_password_change),
1758 TALLOC_FREE(sam_account);
1759 return NT_STATUS_NO_MEMORY;
1762 result = make_server_info(NULL);
1763 if (result == NULL) {
1764 DEBUG(4, ("make_server_info failed!\n"));
1765 TALLOC_FREE(sam_account);
1766 return NT_STATUS_NO_MEMORY;
1769 /* save this here to _net_sam_logon() doesn't fail (it assumes a
1770 valid struct samu) */
1772 result->sam_account = sam_account;
1773 result->unix_name = talloc_strdup(result, found_username);
1775 result->sanitized_username = sanitize_username(result,
1777 if (result->sanitized_username == NULL) {
1778 TALLOC_FREE(result);
1779 return NT_STATUS_NO_MEMORY;
1782 /* Fill in the unix info we found on the way */
1784 result->utok.uid = uid;
1785 result->utok.gid = gid;
1787 /* Create a 'combined' list of all SIDs we might want in the SD */
1789 result->num_sids = 0;
1790 result->sids = NULL;
1792 nt_status = sid_array_from_info3(result, info3,
1796 if (!NT_STATUS_IS_OK(nt_status)) {
1797 TALLOC_FREE(result);
1801 /* Ensure the primary group sid is at position 0. */
1802 sort_sid_array_for_smbd(result, &group_sid);
1804 result->login_server = talloc_strdup(result,
1805 info3->base.logon_server.string);
1807 /* ensure we are never given NULL session keys */
1811 if (memcmp(info3->base.key.key, zeros, sizeof(zeros)) == 0) {
1812 result->user_session_key = data_blob_null;
1814 result->user_session_key = data_blob_talloc(
1815 result, info3->base.key.key,
1816 sizeof(info3->base.key.key));
1819 if (memcmp(info3->base.LMSessKey.key, zeros, 8) == 0) {
1820 result->lm_session_key = data_blob_null;
1822 result->lm_session_key = data_blob_talloc(
1823 result, info3->base.LMSessKey.key,
1824 sizeof(info3->base.LMSessKey.key));
1827 result->nss_token |= username_was_mapped;
1829 *server_info = result;
1831 return NT_STATUS_OK;
1834 /*****************************************************************************
1835 Make a server_info struct from the wbcAuthUserInfo returned by a domain logon
1836 ******************************************************************************/
1838 NTSTATUS make_server_info_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
1839 const char *sent_nt_username,
1841 const struct wbcAuthUserInfo *info,
1842 auth_serversupplied_info **server_info)
1846 NTSTATUS nt_status = NT_STATUS_OK;
1847 char *found_username = NULL;
1848 const char *nt_domain;
1849 const char *nt_username;
1850 struct samu *sam_account = NULL;
1853 bool username_was_mapped;
1856 uid_t uid = (uid_t)-1;
1857 gid_t gid = (gid_t)-1;
1859 auth_serversupplied_info *result;
1861 result = make_server_info(NULL);
1862 if (result == NULL) {
1863 DEBUG(4, ("make_server_info failed!\n"));
1864 return NT_STATUS_NO_MEMORY;
1868 Here is where we should check the list of
1869 trusted domains, and verify that the SID
1873 memcpy(&user_sid, &info->sids[0].sid, sizeof(user_sid));
1874 memcpy(&group_sid, &info->sids[1].sid, sizeof(group_sid));
1876 if (info->account_name) {
1877 nt_username = talloc_strdup(result, info->account_name);
1879 /* If the server didn't give us one, just use the one we sent
1881 nt_username = talloc_strdup(result, sent_nt_username);
1884 TALLOC_FREE(result);
1885 return NT_STATUS_NO_MEMORY;
1888 if (info->domain_name) {
1889 nt_domain = talloc_strdup(result, info->domain_name);
1891 /* If the server didn't give us one, just use the one we sent
1893 nt_domain = talloc_strdup(result, domain);
1896 TALLOC_FREE(result);
1897 return NT_STATUS_NO_MEMORY;
1900 /* try to fill the SAM account.. If getpwnam() fails, then try the
1901 add user script (2.2.x behavior).
1903 We use the _unmapped_ username here in an attempt to provide
1904 consistent username mapping behavior between kerberos and NTLM[SSP]
1905 authentication in domain mode security. I.E. Username mapping
1906 should be applied to the fully qualified username
1907 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1908 called map_username() unnecessarily in make_user_info_map() but
1909 that is how the current code is designed. Making the change here
1910 is the least disruptive place. -- jerry */
1912 if ( !(sam_account = samu_new( result )) ) {
1913 TALLOC_FREE(result);
1914 return NT_STATUS_NO_MEMORY;
1917 /* this call will try to create the user if necessary */
1919 nt_status = fill_sam_account(result, nt_domain, sent_nt_username,
1920 &found_username, &uid, &gid, sam_account,
1921 &username_was_mapped);
1923 /* if we still don't have a valid unix account check for
1924 'map to guest = bad uid' */
1926 if (!NT_STATUS_IS_OK(nt_status)) {
1927 TALLOC_FREE( result );
1928 if ( lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID ) {
1929 make_server_info_guest(NULL, server_info);
1930 return NT_STATUS_OK;
1935 if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1936 TALLOC_FREE(result);
1937 return NT_STATUS_NO_MEMORY;
1940 if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1941 TALLOC_FREE(result);
1942 return NT_STATUS_NO_MEMORY;
1945 if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1946 TALLOC_FREE(result);
1947 return NT_STATUS_NO_MEMORY;
1950 if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1951 TALLOC_FREE(result);
1952 return NT_STATUS_UNSUCCESSFUL;
1955 if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1956 TALLOC_FREE(result);
1957 return NT_STATUS_UNSUCCESSFUL;
1960 if (!pdb_set_fullname(sam_account, info->full_name, PDB_CHANGED)) {
1961 TALLOC_FREE(result);
1962 return NT_STATUS_NO_MEMORY;
1965 if (!pdb_set_logon_script(sam_account, info->logon_script, PDB_CHANGED)) {
1966 TALLOC_FREE(result);
1967 return NT_STATUS_NO_MEMORY;
1970 if (!pdb_set_profile_path(sam_account, info->profile_path, PDB_CHANGED)) {
1971 TALLOC_FREE(result);
1972 return NT_STATUS_NO_MEMORY;
1975 if (!pdb_set_homedir(sam_account, info->home_directory, PDB_CHANGED)) {
1976 TALLOC_FREE(result);
1977 return NT_STATUS_NO_MEMORY;
1980 if (!pdb_set_dir_drive(sam_account, info->home_drive, PDB_CHANGED)) {
1981 TALLOC_FREE(result);
1982 return NT_STATUS_NO_MEMORY;
1985 if (!pdb_set_acct_ctrl(sam_account, info->acct_flags, PDB_CHANGED)) {
1986 TALLOC_FREE(result);
1987 return NT_STATUS_NO_MEMORY;
1990 if (!pdb_set_pass_last_set_time(
1992 nt_time_to_unix(info->pass_last_set_time),
1994 TALLOC_FREE(result);
1995 return NT_STATUS_NO_MEMORY;
1998 if (!pdb_set_pass_can_change_time(
2000 nt_time_to_unix(info->pass_can_change_time),
2002 TALLOC_FREE(result);
2003 return NT_STATUS_NO_MEMORY;
2006 if (!pdb_set_pass_must_change_time(
2008 nt_time_to_unix(info->pass_must_change_time),
2010 TALLOC_FREE(result);
2011 return NT_STATUS_NO_MEMORY;
2014 /* save this here to _net_sam_logon() doesn't fail (it assumes a
2015 valid struct samu) */
2017 result->sam_account = sam_account;
2018 result->unix_name = talloc_strdup(result, found_username);
2020 result->sanitized_username = sanitize_username(result,
2022 result->login_server = talloc_strdup(result, info->logon_server);
2024 if ((result->unix_name == NULL)
2025 || (result->sanitized_username == NULL)
2026 || (result->login_server == NULL)) {
2027 TALLOC_FREE(result);
2028 return NT_STATUS_NO_MEMORY;
2031 /* Fill in the unix info we found on the way */
2033 result->utok.uid = uid;
2034 result->utok.gid = gid;
2036 /* Create a 'combined' list of all SIDs we might want in the SD */
2038 result->num_sids = info->num_sids - 2;
2039 result->sids = talloc_array(result, DOM_SID, result->num_sids);
2040 if (result->sids == NULL) {
2041 TALLOC_FREE(result);
2042 return NT_STATUS_NO_MEMORY;
2045 for (i=0; i < result->num_sids; i++) {
2046 memcpy(&result->sids[i], &info->sids[i+2].sid, sizeof(result->sids[i]));
2049 /* Ensure the primary group sid is at position 0. */
2050 sort_sid_array_for_smbd(result, &group_sid);
2052 /* ensure we are never given NULL session keys */
2056 if (memcmp(info->user_session_key, zeros, sizeof(zeros)) == 0) {
2057 result->user_session_key = data_blob_null;
2059 result->user_session_key = data_blob_talloc(
2060 result, info->user_session_key,
2061 sizeof(info->user_session_key));
2064 if (memcmp(info->lm_session_key, zeros, 8) == 0) {
2065 result->lm_session_key = data_blob_null;
2067 result->lm_session_key = data_blob_talloc(
2068 result, info->lm_session_key,
2069 sizeof(info->lm_session_key));
2072 result->nss_token |= username_was_mapped;
2074 *server_info = result;
2076 return NT_STATUS_OK;
2079 /***************************************************************************
2080 Free a user_info struct
2081 ***************************************************************************/
2083 void free_user_info(auth_usersupplied_info **user_info)
2085 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
2086 if (*user_info != NULL) {
2087 if ((*user_info)->smb_name) {
2088 DEBUG(10,("structure was created for %s\n",
2089 (*user_info)->smb_name));
2091 SAFE_FREE((*user_info)->smb_name);
2092 SAFE_FREE((*user_info)->internal_username);
2093 SAFE_FREE((*user_info)->client_domain);
2094 SAFE_FREE((*user_info)->domain);
2095 SAFE_FREE((*user_info)->wksta_name);
2096 data_blob_free(&(*user_info)->lm_resp);
2097 data_blob_free(&(*user_info)->nt_resp);
2098 data_blob_clear_free(&(*user_info)->lm_interactive_pwd);
2099 data_blob_clear_free(&(*user_info)->nt_interactive_pwd);
2100 data_blob_clear_free(&(*user_info)->plaintext_password);
2101 ZERO_STRUCT(**user_info);
2103 SAFE_FREE(*user_info);
2106 /***************************************************************************
2107 Make an auth_methods struct
2108 ***************************************************************************/
2110 bool make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
2112 if (!auth_context) {
2113 smb_panic("no auth_context supplied to "
2114 "make_auth_methods()!\n");
2118 smb_panic("make_auth_methods: pointer to auth_method pointer "
2122 *auth_method = TALLOC_P(auth_context->mem_ctx, auth_methods);
2123 if (!*auth_method) {
2124 DEBUG(0,("make_auth_method: malloc failed!\n"));
2127 ZERO_STRUCTP(*auth_method);
2133 * Verify whether or not given domain is trusted.
2135 * @param domain_name name of the domain to be verified
2136 * @return true if domain is one of the trusted once or
2137 * false if otherwise
2140 bool is_trusted_domain(const char* dom_name)
2142 DOM_SID trustdom_sid;
2145 /* no trusted domains for a standalone server */
2147 if ( lp_server_role() == ROLE_STANDALONE )
2150 /* if we are a DC, then check for a direct trust relationships */
2154 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
2155 "[%s]\n", dom_name ));
2156 ret = pdb_get_trusteddom_pw(dom_name, NULL, NULL, NULL);
2164 /* If winbind is around, ask it */
2166 result = wb_is_trusted_domain(dom_name);
2168 if (result == WBC_ERR_SUCCESS) {
2172 if (result == WBC_ERR_DOMAIN_NOT_FOUND) {
2173 /* winbind could not find the domain */
2177 /* The only other possible result is that winbind is not up
2178 and running. We need to update the trustdom_cache
2181 update_trustdom_cache();
2184 /* now the trustdom cache should be available a DC could still
2185 * have a transitive trust so fall back to the cache of trusted
2186 * domains (like a domain member would use */
2188 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {