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/>.
25 #include "smbd/globals.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../lib/crypto/arcfour.h"
30 #define DBGC_CLASS DBGC_AUTH
32 /****************************************************************************
33 Create a UNIX user on demand.
34 ****************************************************************************/
36 static int _smb_create_user(const char *domain, const char *unix_username, const char *homedir)
38 TALLOC_CTX *ctx = talloc_tos();
42 add_script = talloc_strdup(ctx, lp_adduser_script());
43 if (!add_script || !*add_script) {
46 add_script = talloc_all_string_sub(ctx,
54 add_script = talloc_all_string_sub(ctx,
63 add_script = talloc_all_string_sub(ctx,
71 ret = smbrun(add_script,NULL);
74 ("smb_create_user: Running the command `%s' gave %d\n",
79 /****************************************************************************
80 Create an auth_usersupplied_data structure after appropriate mapping.
81 ****************************************************************************/
83 NTSTATUS make_user_info_map(struct auth_usersupplied_info **user_info,
85 const char *client_domain,
86 const char *workstation_name,
89 DATA_BLOB *lm_interactive_pwd,
90 DATA_BLOB *nt_interactive_pwd,
97 char *internal_username = NULL;
99 was_mapped = map_username(talloc_tos(), smb_name, &internal_username);
100 if (!internal_username) {
101 return NT_STATUS_NO_MEMORY;
104 DEBUG(5, ("Mapping user [%s]\\[%s] from workstation [%s]\n",
105 client_domain, smb_name, workstation_name));
107 domain = client_domain;
109 /* If you connect to a Windows domain member using a bogus domain name,
110 * the Windows box will map the BOGUS\user to SAMNAME\user. Thus, if
111 * the Windows box is a DC the name will become DOMAIN\user and be
112 * authenticated against AD, if the Windows box is a member server but
113 * not a DC the name will become WORKSTATION\user. A standalone
114 * non-domain member box will also map to WORKSTATION\user.
115 * This also deals with the client passing in a "" domain */
117 if (!is_trusted_domain(domain) &&
118 !strequal(domain, my_sam_name()))
120 if (lp_map_untrusted_to_domain())
121 domain = my_sam_name();
123 domain = get_global_sam_name();
124 DEBUG(5, ("Mapped domain from [%s] to [%s] for user [%s] from "
125 "workstation [%s]\n",
126 client_domain, domain, smb_name, workstation_name));
129 /* We know that the given domain is trusted (and we are allowing them),
130 * it is our global SAM name, or for legacy behavior it is our
131 * primary domain name */
133 result = make_user_info(user_info, smb_name, internal_username,
134 client_domain, domain, workstation_name,
136 lm_interactive_pwd, nt_interactive_pwd,
137 plaintext, encrypted);
138 if (NT_STATUS_IS_OK(result)) {
139 (*user_info)->was_mapped = was_mapped;
144 /****************************************************************************
145 Create an auth_usersupplied_data, making the DATA_BLOBs here.
146 Decrypt and encrypt the passwords.
147 ****************************************************************************/
149 bool make_user_info_netlogon_network(struct auth_usersupplied_info **user_info,
150 const char *smb_name,
151 const char *client_domain,
152 const char *workstation_name,
153 uint32 logon_parameters,
154 const uchar *lm_network_pwd,
156 const uchar *nt_network_pwd,
161 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
162 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
164 status = make_user_info_map(user_info,
165 smb_name, client_domain,
167 lm_pwd_len ? &lm_blob : NULL,
168 nt_pwd_len ? &nt_blob : NULL,
172 if (NT_STATUS_IS_OK(status)) {
173 (*user_info)->logon_parameters = logon_parameters;
175 ret = NT_STATUS_IS_OK(status) ? True : False;
177 data_blob_free(&lm_blob);
178 data_blob_free(&nt_blob);
182 /****************************************************************************
183 Create an auth_usersupplied_data, making the DATA_BLOBs here.
184 Decrypt and encrypt the passwords.
185 ****************************************************************************/
187 bool make_user_info_netlogon_interactive(struct auth_usersupplied_info **user_info,
188 const char *smb_name,
189 const char *client_domain,
190 const char *workstation_name,
191 uint32 logon_parameters,
193 const uchar lm_interactive_pwd[16],
194 const uchar nt_interactive_pwd[16],
195 const uchar *dc_sess_key)
197 unsigned char lm_pwd[16];
198 unsigned char nt_pwd[16];
199 unsigned char local_lm_response[24];
200 unsigned char local_nt_response[24];
201 unsigned char key[16];
203 memcpy(key, dc_sess_key, 16);
205 if (lm_interactive_pwd)
206 memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
208 if (nt_interactive_pwd)
209 memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
211 #ifdef DEBUG_PASSWORD
213 dump_data(100, key, sizeof(key));
215 DEBUG(100,("lm owf password:"));
216 dump_data(100, lm_pwd, sizeof(lm_pwd));
218 DEBUG(100,("nt owf password:"));
219 dump_data(100, nt_pwd, sizeof(nt_pwd));
222 if (lm_interactive_pwd)
223 arcfour_crypt(lm_pwd, key, sizeof(lm_pwd));
225 if (nt_interactive_pwd)
226 arcfour_crypt(nt_pwd, key, sizeof(nt_pwd));
228 #ifdef DEBUG_PASSWORD
229 DEBUG(100,("decrypt of lm owf password:"));
230 dump_data(100, lm_pwd, sizeof(lm_pwd));
232 DEBUG(100,("decrypt of nt owf password:"));
233 dump_data(100, nt_pwd, sizeof(nt_pwd));
236 if (lm_interactive_pwd)
237 SMBOWFencrypt(lm_pwd, chal,
240 if (nt_interactive_pwd)
241 SMBOWFencrypt(nt_pwd, chal,
244 /* Password info paranoia */
250 DATA_BLOB local_lm_blob;
251 DATA_BLOB local_nt_blob;
253 DATA_BLOB lm_interactive_blob;
254 DATA_BLOB nt_interactive_blob;
256 if (lm_interactive_pwd) {
257 local_lm_blob = data_blob(local_lm_response,
258 sizeof(local_lm_response));
259 lm_interactive_blob = data_blob(lm_pwd,
264 if (nt_interactive_pwd) {
265 local_nt_blob = data_blob(local_nt_response,
266 sizeof(local_nt_response));
267 nt_interactive_blob = data_blob(nt_pwd,
272 nt_status = make_user_info_map(
274 smb_name, client_domain, workstation_name,
275 lm_interactive_pwd ? &local_lm_blob : NULL,
276 nt_interactive_pwd ? &local_nt_blob : NULL,
277 lm_interactive_pwd ? &lm_interactive_blob : NULL,
278 nt_interactive_pwd ? &nt_interactive_blob : NULL,
281 if (NT_STATUS_IS_OK(nt_status)) {
282 (*user_info)->logon_parameters = logon_parameters;
285 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
286 data_blob_free(&local_lm_blob);
287 data_blob_free(&local_nt_blob);
288 data_blob_free(&lm_interactive_blob);
289 data_blob_free(&nt_interactive_blob);
295 /****************************************************************************
296 Create an auth_usersupplied_data structure
297 ****************************************************************************/
299 bool make_user_info_for_reply(struct auth_usersupplied_info **user_info,
300 const char *smb_name,
301 const char *client_domain,
303 DATA_BLOB plaintext_password)
306 DATA_BLOB local_lm_blob;
307 DATA_BLOB local_nt_blob;
308 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
311 * Not encrypted - do so.
314 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
317 if (plaintext_password.data && plaintext_password.length) {
318 unsigned char local_lm_response[24];
320 #ifdef DEBUG_PASSWORD
321 DEBUG(10,("Unencrypted password (len %d):\n",
322 (int)plaintext_password.length));
323 dump_data(100, plaintext_password.data,
324 plaintext_password.length);
327 SMBencrypt( (const char *)plaintext_password.data,
328 (const uchar*)chal, local_lm_response);
329 local_lm_blob = data_blob(local_lm_response, 24);
331 /* We can't do an NT hash here, as the password needs to be
333 local_nt_blob = data_blob_null;
335 local_lm_blob = data_blob_null;
336 local_nt_blob = data_blob_null;
339 ret = make_user_info_map(
340 user_info, smb_name, client_domain,
341 get_remote_machine_name(),
342 local_lm_blob.data ? &local_lm_blob : NULL,
343 local_nt_blob.data ? &local_nt_blob : NULL,
345 plaintext_password.data && plaintext_password.length ? &plaintext_password : NULL,
348 data_blob_free(&local_lm_blob);
349 return NT_STATUS_IS_OK(ret) ? True : False;
352 /****************************************************************************
353 Create an auth_usersupplied_data structure
354 ****************************************************************************/
356 NTSTATUS make_user_info_for_reply_enc(struct auth_usersupplied_info **user_info,
357 const char *smb_name,
358 const char *client_domain,
359 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
361 return make_user_info_map(user_info, smb_name,
363 get_remote_machine_name(),
364 lm_resp.data && (lm_resp.length > 0) ? &lm_resp : NULL,
365 nt_resp.data && (nt_resp.length > 0) ? &nt_resp : NULL,
370 /****************************************************************************
371 Create a guest user_info blob, for anonymous authenticaion.
372 ****************************************************************************/
374 bool make_user_info_guest(struct auth_usersupplied_info **user_info)
378 nt_status = make_user_info(user_info,
387 return NT_STATUS_IS_OK(nt_status) ? True : False;
390 static NTSTATUS log_nt_token(NT_USER_TOKEN *token)
392 TALLOC_CTX *frame = talloc_stackframe();
397 if ((lp_log_nt_token_command() == NULL) ||
398 (strlen(lp_log_nt_token_command()) == 0)) {
403 group_sidstr = talloc_strdup(frame, "");
404 for (i=1; i<token->num_sids; i++) {
405 group_sidstr = talloc_asprintf(
406 frame, "%s %s", group_sidstr,
407 sid_string_talloc(frame, &token->user_sids[i]));
410 command = talloc_string_sub(
411 frame, lp_log_nt_token_command(),
412 "%s", sid_string_talloc(frame, &token->user_sids[0]));
413 command = talloc_string_sub(frame, command, "%t", group_sidstr);
415 if (command == NULL) {
417 return NT_STATUS_NO_MEMORY;
420 DEBUG(8, ("running command: [%s]\n", command));
421 if (smbrun(command, NULL) != 0) {
422 DEBUG(0, ("Could not log NT token\n"));
424 return NT_STATUS_ACCESS_DENIED;
432 * Create the token to use from server_info->info3 and
433 * server_info->sids (the info3/sam groups). Find the unix gids.
436 NTSTATUS create_local_token(struct auth_serversupplied_info *server_info)
440 struct dom_sid tmp_sid;
443 * If winbind is not around, we can not make much use of the SIDs the
444 * domain controller provided us with. Likewise if the user name was
445 * mapped to some local unix user.
448 if (((lp_server_role() == ROLE_DOMAIN_MEMBER) && !winbind_ping()) ||
449 (server_info->nss_token)) {
450 status = create_token_from_username(server_info,
451 server_info->unix_name,
453 &server_info->utok.uid,
454 &server_info->utok.gid,
455 &server_info->unix_name,
459 status = create_local_nt_token_from_info3(server_info,
466 if (!NT_STATUS_IS_OK(status)) {
470 /* Convert the SIDs to gids. */
472 server_info->utok.ngroups = 0;
473 server_info->utok.groups = NULL;
475 /* Start at index 1, where the groups start. */
477 for (i=1; i<server_info->ptok->num_sids; i++) {
479 struct dom_sid *sid = &server_info->ptok->user_sids[i];
481 if (!sid_to_gid(sid, &gid)) {
482 DEBUG(10, ("Could not convert SID %s to gid, "
483 "ignoring it\n", sid_string_dbg(sid)));
486 add_gid_to_array_unique(server_info, gid,
487 &server_info->utok.groups,
488 &server_info->utok.ngroups);
492 * Add the "Unix Group" SID for each gid to catch mapped groups
493 * and their Unix equivalent. This is to solve the backwards
494 * compatibility problem of 'valid users = +ntadmin' where
495 * ntadmin has been paired with "Domain Admins" in the group
496 * mapping table. Otherwise smb.conf would need to be changed
497 * to 'valid user = "Domain Admins"'. --jerry
499 * For consistency we also add the "Unix User" SID,
500 * so that the complete unix token is represented within
504 uid_to_unix_users_sid(server_info->utok.uid, &tmp_sid);
506 add_sid_to_array_unique(server_info->ptok, &tmp_sid,
507 &server_info->ptok->user_sids,
508 &server_info->ptok->num_sids);
510 for ( i=0; i<server_info->utok.ngroups; i++ ) {
511 gid_to_unix_groups_sid(server_info->utok.groups[i], &tmp_sid);
512 add_sid_to_array_unique(server_info->ptok, &tmp_sid,
513 &server_info->ptok->user_sids,
514 &server_info->ptok->num_sids);
517 debug_nt_user_token(DBGC_AUTH, 10, server_info->ptok);
518 debug_unix_user_token(DBGC_AUTH, 10,
519 server_info->utok.uid,
520 server_info->utok.gid,
521 server_info->utok.ngroups,
522 server_info->utok.groups);
524 status = log_nt_token(server_info->ptok);
528 /***************************************************************************
529 Make (and fill) a server_info struct from a 'struct passwd' by conversion
531 ***************************************************************************/
533 NTSTATUS make_server_info_pw(struct auth_serversupplied_info **server_info,
538 struct samu *sampass = NULL;
539 char *qualified_name = NULL;
540 TALLOC_CTX *mem_ctx = NULL;
541 struct dom_sid u_sid;
542 enum lsa_SidType type;
543 struct auth_serversupplied_info *result;
546 * The SID returned in server_info->sam_account is based
547 * on our SAM sid even though for a pure UNIX account this should
548 * not be the case as it doesn't really exist in the SAM db.
549 * This causes lookups on "[in]valid users" to fail as they
550 * will lookup this name as a "Unix User" SID to check against
551 * the user token. Fix this by adding the "Unix User"\unix_username
552 * SID to the sid array. The correct fix should probably be
553 * changing the server_info->sam_account user SID to be a
554 * S-1-22 Unix SID, but this might break old configs where
555 * plaintext passwords were used with no SAM backend.
558 mem_ctx = talloc_init("make_server_info_pw_tmp");
560 return NT_STATUS_NO_MEMORY;
563 qualified_name = talloc_asprintf(mem_ctx, "%s\\%s",
564 unix_users_domain_name(),
566 if (!qualified_name) {
567 TALLOC_FREE(mem_ctx);
568 return NT_STATUS_NO_MEMORY;
571 if (!lookup_name(mem_ctx, qualified_name, LOOKUP_NAME_ALL,
574 TALLOC_FREE(mem_ctx);
575 return NT_STATUS_NO_SUCH_USER;
578 TALLOC_FREE(mem_ctx);
580 if (type != SID_NAME_USER) {
581 return NT_STATUS_NO_SUCH_USER;
584 if ( !(sampass = samu_new( NULL )) ) {
585 return NT_STATUS_NO_MEMORY;
588 status = samu_set_unix( sampass, pwd );
589 if (!NT_STATUS_IS_OK(status)) {
593 /* In pathological cases the above call can set the account
594 * name to the DOMAIN\username form. Reset the account name
595 * using unix_username */
596 pdb_set_username(sampass, unix_username, PDB_SET);
598 /* set the user sid to be the calculated u_sid */
599 pdb_set_user_sid(sampass, &u_sid, PDB_SET);
601 result = make_server_info(NULL);
602 if (result == NULL) {
603 TALLOC_FREE(sampass);
604 return NT_STATUS_NO_MEMORY;
607 status = samu_to_SamInfo3(result, sampass, global_myname(),
608 &result->info3, &result->extra);
609 TALLOC_FREE(sampass);
610 if (!NT_STATUS_IS_OK(status)) {
611 DEBUG(10, ("Failed to convert samu to info3: %s\n",
617 result->unix_name = talloc_strdup(result, unix_username);
618 result->sanitized_username = sanitize_username(result, unix_username);
620 if ((result->unix_name == NULL)
621 || (result->sanitized_username == NULL)) {
623 return NT_STATUS_NO_MEMORY;
626 result->utok.uid = pwd->pw_uid;
627 result->utok.gid = pwd->pw_gid;
629 *server_info = result;
634 /***************************************************************************
635 Make (and fill) a user_info struct for a guest login.
636 This *must* succeed for smbd to start. If there is no mapping entry for
637 the guest gid, then create one.
638 ***************************************************************************/
640 static NTSTATUS make_new_server_info_guest(struct auth_serversupplied_info **server_info)
643 struct samu *sampass = NULL;
644 struct dom_sid guest_sid;
646 static const char zeros[16] = {0, };
649 if ( !(sampass = samu_new( NULL )) ) {
650 return NT_STATUS_NO_MEMORY;
653 sid_compose(&guest_sid, get_global_sam_sid(), DOMAIN_RID_GUEST);
656 ret = pdb_getsampwsid(sampass, &guest_sid);
660 TALLOC_FREE(sampass);
661 return NT_STATUS_NO_SUCH_USER;
664 status = make_server_info_sam(server_info, sampass);
665 if (!NT_STATUS_IS_OK(status)) {
666 TALLOC_FREE(sampass);
670 TALLOC_FREE(sampass);
672 (*server_info)->guest = True;
674 status = create_local_token(*server_info);
675 if (!NT_STATUS_IS_OK(status)) {
676 DEBUG(10, ("create_local_token failed: %s\n",
681 /* annoying, but the Guest really does have a session key, and it is
683 (*server_info)->user_session_key = data_blob(zeros, sizeof(zeros));
684 (*server_info)->lm_session_key = data_blob(zeros, sizeof(zeros));
686 alpha_strcpy(tmp, (*server_info)->info3->base.account_name.string,
687 ". _-$", sizeof(tmp));
688 (*server_info)->sanitized_username = talloc_strdup(*server_info, tmp);
693 /***************************************************************************
694 Make (and fill) a user_info struct for a system user login.
695 This *must* succeed for smbd to start.
696 ***************************************************************************/
698 static NTSTATUS make_new_server_info_system(TALLOC_CTX *mem_ctx,
699 struct auth_serversupplied_info **server_info)
704 pwd = getpwuid_alloc(mem_ctx, sec_initial_uid());
706 return NT_STATUS_NO_MEMORY;
709 status = make_serverinfo_from_username(mem_ctx,
713 if (!NT_STATUS_IS_OK(status)) {
717 (*server_info)->system = true;
722 /****************************************************************************
723 Fake a auth_serversupplied_info just from a username
724 ****************************************************************************/
726 NTSTATUS make_serverinfo_from_username(TALLOC_CTX *mem_ctx,
727 const char *username,
729 struct auth_serversupplied_info **presult)
731 struct auth_serversupplied_info *result;
735 pwd = Get_Pwnam_alloc(talloc_tos(), username);
737 return NT_STATUS_NO_SUCH_USER;
740 status = make_server_info_pw(&result, pwd->pw_name, pwd);
744 if (!NT_STATUS_IS_OK(status)) {
748 result->nss_token = true;
749 result->guest = is_guest;
751 status = create_local_token(result);
753 if (!NT_STATUS_IS_OK(status)) {
763 struct auth_serversupplied_info *copy_serverinfo(TALLOC_CTX *mem_ctx,
764 const struct auth_serversupplied_info *src)
766 struct auth_serversupplied_info *dst;
768 dst = make_server_info(mem_ctx);
773 dst->guest = src->guest;
774 dst->system = src->system;
775 dst->utok.uid = src->utok.uid;
776 dst->utok.gid = src->utok.gid;
777 dst->utok.ngroups = src->utok.ngroups;
778 if (src->utok.ngroups != 0) {
779 dst->utok.groups = (gid_t *)TALLOC_MEMDUP(
780 dst, src->utok.groups,
781 sizeof(gid_t)*dst->utok.ngroups);
783 dst->utok.groups = NULL;
787 dst->ptok = dup_nt_token(dst, src->ptok);
794 dst->user_session_key = data_blob_talloc( dst, src->user_session_key.data,
795 src->user_session_key.length);
797 dst->lm_session_key = data_blob_talloc(dst, src->lm_session_key.data,
798 src->lm_session_key.length);
800 dst->info3 = copy_netr_SamInfo3(dst, src->info3);
805 dst->extra = src->extra;
807 dst->pam_handle = NULL;
808 dst->unix_name = talloc_strdup(dst, src->unix_name);
809 if (!dst->unix_name) {
814 dst->sanitized_username = talloc_strdup(dst, src->sanitized_username);
815 if (!dst->sanitized_username) {
824 * Set a new session key. Used in the rpc server where we have to override the
825 * SMB level session key with SystemLibraryDTC
828 bool server_info_set_session_key(struct auth_serversupplied_info *info,
829 DATA_BLOB session_key)
831 TALLOC_FREE(info->user_session_key.data);
833 info->user_session_key = data_blob_talloc(
834 info, session_key.data, session_key.length);
836 return (info->user_session_key.data != NULL);
839 static struct auth_serversupplied_info *guest_info = NULL;
841 bool init_guest_info(void)
843 if (guest_info != NULL)
846 return NT_STATUS_IS_OK(make_new_server_info_guest(&guest_info));
849 NTSTATUS make_server_info_guest(TALLOC_CTX *mem_ctx,
850 struct auth_serversupplied_info **server_info)
852 *server_info = copy_serverinfo(mem_ctx, guest_info);
853 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
856 static struct auth_serversupplied_info *system_info = NULL;
858 bool init_system_info(void)
860 if (system_info != NULL)
863 return NT_STATUS_IS_OK(make_new_server_info_system(talloc_autofree_context(), &system_info));
866 NTSTATUS make_server_info_system(TALLOC_CTX *mem_ctx,
867 struct auth_serversupplied_info **server_info)
869 if (system_info == NULL) return NT_STATUS_UNSUCCESSFUL;
870 *server_info = copy_serverinfo(mem_ctx, system_info);
871 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
874 bool copy_current_user(struct current_user *dst, struct current_user *src)
877 NT_USER_TOKEN *nt_token;
879 groups = (gid_t *)memdup(src->ut.groups,
880 sizeof(gid_t) * src->ut.ngroups);
881 if ((src->ut.ngroups != 0) && (groups == NULL)) {
885 nt_token = dup_nt_token(NULL, src->nt_user_token);
886 if (nt_token == NULL) {
891 dst->conn = src->conn;
892 dst->vuid = src->vuid;
893 dst->ut.uid = src->ut.uid;
894 dst->ut.gid = src->ut.gid;
895 dst->ut.ngroups = src->ut.ngroups;
896 dst->ut.groups = groups;
897 dst->nt_user_token = nt_token;
901 /***************************************************************************
902 Purely internal function for make_server_info_info3
903 ***************************************************************************/
905 static NTSTATUS check_account(TALLOC_CTX *mem_ctx, const char *domain,
906 const char *username, char **found_username,
908 bool *username_was_mapped)
910 char *orig_dom_user = NULL;
911 char *dom_user = NULL;
912 char *lower_username = NULL;
913 char *real_username = NULL;
914 struct passwd *passwd;
916 lower_username = talloc_strdup(mem_ctx, username);
917 if (!lower_username) {
918 return NT_STATUS_NO_MEMORY;
920 strlower_m( lower_username );
922 orig_dom_user = talloc_asprintf(mem_ctx,
925 *lp_winbind_separator(),
927 if (!orig_dom_user) {
928 return NT_STATUS_NO_MEMORY;
931 /* Get the passwd struct. Try to create the account if necessary. */
933 *username_was_mapped = map_username(mem_ctx, orig_dom_user, &dom_user);
935 return NT_STATUS_NO_MEMORY;
938 passwd = smb_getpwnam(mem_ctx, dom_user, &real_username, True );
940 DEBUG(3, ("Failed to find authenticated user %s via "
941 "getpwnam(), denying access.\n", dom_user));
942 return NT_STATUS_NO_SUCH_USER;
945 if (!real_username) {
946 return NT_STATUS_NO_MEMORY;
951 /* This is pointless -- there is no suport for differing
952 unix and windows names. Make sure to always store the
953 one we actually looked up and succeeded. Have I mentioned
954 why I hate the 'winbind use default domain' parameter?
957 *found_username = talloc_strdup( mem_ctx, real_username );
962 /****************************************************************************
963 Wrapper to allow the getpwnam() call to strip the domain name and
964 try again in case a local UNIX user is already there. Also run through
965 the username if we fallback to the username only.
966 ****************************************************************************/
968 struct passwd *smb_getpwnam( TALLOC_CTX *mem_ctx, const char *domuser,
969 char **p_save_username, bool create )
971 struct passwd *pw = NULL;
973 char *username = NULL;
975 /* we only save a copy of the username it has been mangled
976 by winbindd use default domain */
978 *p_save_username = NULL;
980 /* don't call map_username() here since it has to be done higher
981 up the stack so we don't call it multiple times */
983 username = talloc_strdup(mem_ctx, domuser);
988 p = strchr_m( username, *lp_winbind_separator() );
990 /* code for a DOMAIN\user string */
993 pw = Get_Pwnam_alloc( mem_ctx, domuser );
995 /* make sure we get the case of the username correct */
996 /* work around 'winbind use default domain = yes' */
998 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1001 /* split the domain and username into 2 strings */
1005 *p_save_username = talloc_asprintf(mem_ctx,
1008 *lp_winbind_separator(),
1010 if (!*p_save_username) {
1015 *p_save_username = talloc_strdup(mem_ctx, pw->pw_name);
1022 /* setup for lookup of just the username */
1023 /* remember that p and username are overlapping memory */
1026 username = talloc_strdup(mem_ctx, p);
1032 /* just lookup a plain username */
1034 pw = Get_Pwnam_alloc(mem_ctx, username);
1036 /* Create local user if requested but only if winbindd
1037 is not running. We need to protect against cases
1038 where winbindd is failing and then prematurely
1039 creating users in /etc/passwd */
1041 if ( !pw && create && !winbind_ping() ) {
1042 /* Don't add a machine account. */
1043 if (username[strlen(username)-1] == '$')
1046 _smb_create_user(NULL, username, NULL);
1047 pw = Get_Pwnam_alloc(mem_ctx, username);
1050 /* one last check for a valid passwd struct */
1053 *p_save_username = talloc_strdup(mem_ctx, pw->pw_name);
1058 /***************************************************************************
1059 Make a server_info struct from the info3 returned by a domain logon
1060 ***************************************************************************/
1062 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1063 const char *sent_nt_username,
1065 struct auth_serversupplied_info **server_info,
1066 struct netr_SamInfo3 *info3)
1068 static const char zeros[16] = {0, };
1070 NTSTATUS nt_status = NT_STATUS_OK;
1071 char *found_username = NULL;
1072 const char *nt_domain;
1073 const char *nt_username;
1074 bool username_was_mapped;
1076 struct auth_serversupplied_info *result;
1077 struct dom_sid *group_sid;
1078 struct netr_SamInfo3 *i3;
1081 Here is where we should check the list of
1082 trusted domains, and verify that the SID
1086 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
1088 /* If the server didn't give us one, just use the one we sent
1090 nt_username = sent_nt_username;
1093 nt_domain = talloc_strdup(mem_ctx, info3->base.domain.string);
1095 /* If the server didn't give us one, just use the one we sent
1100 /* If getpwnam() fails try the add user script (2.2.x behavior).
1102 We use the _unmapped_ username here in an attempt to provide
1103 consistent username mapping behavior between kerberos and NTLM[SSP]
1104 authentication in domain mode security. I.E. Username mapping
1105 should be applied to the fully qualified username
1106 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1107 called map_username() unnecessarily in make_user_info_map() but
1108 that is how the current code is designed. Making the change here
1109 is the least disruptive place. -- jerry */
1111 /* this call will try to create the user if necessary */
1113 nt_status = check_account(mem_ctx, nt_domain, sent_nt_username,
1114 &found_username, &pwd,
1115 &username_was_mapped);
1117 if (!NT_STATUS_IS_OK(nt_status)) {
1121 result = make_server_info(NULL);
1122 if (result == NULL) {
1123 DEBUG(4, ("make_server_info failed!\n"));
1124 return NT_STATUS_NO_MEMORY;
1127 result->unix_name = talloc_strdup(result, found_username);
1129 result->sanitized_username = sanitize_username(result,
1131 if (result->sanitized_username == NULL) {
1132 TALLOC_FREE(result);
1133 return NT_STATUS_NO_MEMORY;
1136 /* copy in the info3 */
1137 result->info3 = i3 = copy_netr_SamInfo3(result, info3);
1138 if (result->info3 == NULL) {
1139 TALLOC_FREE(result);
1140 return NT_STATUS_NO_MEMORY;
1143 /* Fill in the unix info we found on the way */
1144 result->utok.uid = pwd->pw_uid;
1145 result->utok.gid = pwd->pw_gid;
1147 /* We can't just trust that the primary group sid sent us is something
1148 * we can really use. Obtain the useable sid, and store the original
1149 * one as an additional group if it had to be replaced */
1150 nt_status = get_primary_group_sid(mem_ctx, found_username,
1152 if (!NT_STATUS_IS_OK(nt_status)) {
1153 TALLOC_FREE(result);
1157 /* store and check if it is the same we got originally */
1158 sid_peek_rid(group_sid, &i3->base.primary_gid);
1159 if (i3->base.primary_gid != info3->base.primary_gid) {
1160 uint32_t n = i3->base.groups.count;
1161 /* not the same, store the original as an additional group */
1162 i3->base.groups.rids =
1163 talloc_realloc(i3, i3->base.groups.rids,
1164 struct samr_RidWithAttribute, n + 1);
1165 if (i3->base.groups.rids == NULL) {
1166 TALLOC_FREE(result);
1167 return NT_STATUS_NO_MEMORY;
1169 i3->base.groups.rids[n].rid = info3->base.primary_gid;
1170 i3->base.groups.rids[n].attributes = SE_GROUP_ENABLED;
1171 i3->base.groups.count = n + 1;
1174 /* ensure we are never given NULL session keys */
1176 if (memcmp(info3->base.key.key, zeros, sizeof(zeros)) == 0) {
1177 result->user_session_key = data_blob_null;
1179 result->user_session_key = data_blob_talloc(
1180 result, info3->base.key.key,
1181 sizeof(info3->base.key.key));
1184 if (memcmp(info3->base.LMSessKey.key, zeros, 8) == 0) {
1185 result->lm_session_key = data_blob_null;
1187 result->lm_session_key = data_blob_talloc(
1188 result, info3->base.LMSessKey.key,
1189 sizeof(info3->base.LMSessKey.key));
1192 result->nss_token |= username_was_mapped;
1194 *server_info = result;
1196 return NT_STATUS_OK;
1199 /*****************************************************************************
1200 Make a server_info struct from the wbcAuthUserInfo returned by a domain logon
1201 ******************************************************************************/
1203 NTSTATUS make_server_info_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
1204 const char *sent_nt_username,
1206 const struct wbcAuthUserInfo *info,
1207 struct auth_serversupplied_info **server_info)
1209 struct netr_SamInfo3 *info3;
1211 info3 = wbcAuthUserInfo_to_netr_SamInfo3(mem_ctx, info);
1213 return NT_STATUS_NO_MEMORY;
1216 return make_server_info_info3(mem_ctx,
1217 sent_nt_username, domain,
1218 server_info, info3);
1222 * Verify whether or not given domain is trusted.
1224 * @param domain_name name of the domain to be verified
1225 * @return true if domain is one of the trusted ones or
1226 * false if otherwise
1229 bool is_trusted_domain(const char* dom_name)
1231 struct dom_sid trustdom_sid;
1234 /* no trusted domains for a standalone server */
1236 if ( lp_server_role() == ROLE_STANDALONE )
1239 if (dom_name == NULL || dom_name[0] == '\0') {
1243 if (strequal(dom_name, get_global_sam_name())) {
1247 /* if we are a DC, then check for a direct trust relationships */
1251 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
1252 "[%s]\n", dom_name ));
1253 ret = pdb_get_trusteddom_pw(dom_name, NULL, NULL, NULL);
1261 /* If winbind is around, ask it */
1263 result = wb_is_trusted_domain(dom_name);
1265 if (result == WBC_ERR_SUCCESS) {
1269 if (result == WBC_ERR_DOMAIN_NOT_FOUND) {
1270 /* winbind could not find the domain */
1274 /* The only other possible result is that winbind is not up
1275 and running. We need to update the trustdom_cache
1278 update_trustdom_cache();
1281 /* now the trustdom cache should be available a DC could still
1282 * have a transitive trust so fall back to the cache of trusted
1283 * domains (like a domain member would use */
1285 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {