2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001-2011
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/>.
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../lib/crypto/arcfour.h"
28 #include "rpc_client/init_lsa.h"
29 #include "../libcli/security/security.h"
30 #include "../lib/util/util_pw.h"
31 #include "lib/winbind_util.h"
35 #define DBGC_CLASS DBGC_AUTH
37 static struct auth3_session_info *copy_serverinfo_session_info(TALLOC_CTX *mem_ctx,
38 const struct auth_serversupplied_info *src);
40 /****************************************************************************
41 Create a UNIX user on demand.
42 ****************************************************************************/
44 static int _smb_create_user(const char *domain, const char *unix_username, const char *homedir)
46 TALLOC_CTX *ctx = talloc_tos();
50 add_script = talloc_strdup(ctx, lp_adduser_script());
51 if (!add_script || !*add_script) {
54 add_script = talloc_all_string_sub(ctx,
62 add_script = talloc_all_string_sub(ctx,
71 add_script = talloc_all_string_sub(ctx,
79 ret = smbrun(add_script,NULL);
82 ("smb_create_user: Running the command `%s' gave %d\n",
87 /****************************************************************************
88 Create an auth_usersupplied_data structure after appropriate mapping.
89 ****************************************************************************/
91 NTSTATUS make_user_info_map(struct auth_usersupplied_info **user_info,
93 const char *client_domain,
94 const char *workstation_name,
95 const struct tsocket_address *remote_address,
98 const struct samr_Password *lm_interactive_pwd,
99 const struct samr_Password *nt_interactive_pwd,
100 const char *plaintext,
101 enum auth_password_state password_state)
106 char *internal_username = NULL;
108 was_mapped = map_username(talloc_tos(), smb_name, &internal_username);
109 if (!internal_username) {
110 return NT_STATUS_NO_MEMORY;
113 DEBUG(5, ("Mapping user [%s]\\[%s] from workstation [%s]\n",
114 client_domain, smb_name, workstation_name));
116 domain = client_domain;
118 /* If you connect to a Windows domain member using a bogus domain name,
119 * the Windows box will map the BOGUS\user to SAMNAME\user. Thus, if
120 * the Windows box is a DC the name will become DOMAIN\user and be
121 * authenticated against AD, if the Windows box is a member server but
122 * not a DC the name will become WORKSTATION\user. A standalone
123 * non-domain member box will also map to WORKSTATION\user.
124 * This also deals with the client passing in a "" domain */
126 if (!is_trusted_domain(domain) &&
127 !strequal(domain, my_sam_name()))
129 if (lp_map_untrusted_to_domain())
130 domain = my_sam_name();
132 domain = get_global_sam_name();
133 DEBUG(5, ("Mapped domain from [%s] to [%s] for user [%s] from "
134 "workstation [%s]\n",
135 client_domain, domain, smb_name, workstation_name));
138 /* We know that the given domain is trusted (and we are allowing them),
139 * it is our global SAM name, or for legacy behavior it is our
140 * primary domain name */
142 result = make_user_info(user_info, smb_name, internal_username,
143 client_domain, domain, workstation_name,
144 remote_address, lm_pwd, nt_pwd,
145 lm_interactive_pwd, nt_interactive_pwd,
146 plaintext, password_state);
147 if (NT_STATUS_IS_OK(result)) {
148 /* We have tried mapping */
149 (*user_info)->mapped_state = True;
150 /* did we actually map the user to a different name? */
151 (*user_info)->was_mapped = was_mapped;
156 /****************************************************************************
157 Create an auth_usersupplied_data, making the DATA_BLOBs here.
158 Decrypt and encrypt the passwords.
159 ****************************************************************************/
161 bool make_user_info_netlogon_network(struct auth_usersupplied_info **user_info,
162 const char *smb_name,
163 const char *client_domain,
164 const char *workstation_name,
165 const struct tsocket_address *remote_address,
166 uint32 logon_parameters,
167 const uchar *lm_network_pwd,
169 const uchar *nt_network_pwd,
174 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
175 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
177 status = make_user_info_map(user_info,
178 smb_name, client_domain,
181 lm_pwd_len ? &lm_blob : NULL,
182 nt_pwd_len ? &nt_blob : NULL,
184 AUTH_PASSWORD_RESPONSE);
186 if (NT_STATUS_IS_OK(status)) {
187 (*user_info)->logon_parameters = logon_parameters;
189 ret = NT_STATUS_IS_OK(status) ? True : False;
191 data_blob_free(&lm_blob);
192 data_blob_free(&nt_blob);
196 /****************************************************************************
197 Create an auth_usersupplied_data, making the DATA_BLOBs here.
198 Decrypt and encrypt the passwords.
199 ****************************************************************************/
201 bool make_user_info_netlogon_interactive(struct auth_usersupplied_info **user_info,
202 const char *smb_name,
203 const char *client_domain,
204 const char *workstation_name,
205 const struct tsocket_address *remote_address,
206 uint32 logon_parameters,
208 const uchar lm_interactive_pwd[16],
209 const uchar nt_interactive_pwd[16],
210 const uchar *dc_sess_key)
212 struct samr_Password lm_pwd;
213 struct samr_Password nt_pwd;
214 unsigned char local_lm_response[24];
215 unsigned char local_nt_response[24];
216 unsigned char key[16];
218 memcpy(key, dc_sess_key, 16);
220 if (lm_interactive_pwd)
221 memcpy(lm_pwd.hash, lm_interactive_pwd, sizeof(lm_pwd.hash));
223 if (nt_interactive_pwd)
224 memcpy(nt_pwd.hash, nt_interactive_pwd, sizeof(nt_pwd.hash));
226 #ifdef DEBUG_PASSWORD
228 dump_data(100, key, sizeof(key));
230 DEBUG(100,("lm owf password:"));
231 dump_data(100, lm_pwd.hash, sizeof(lm_pwd.hash));
233 DEBUG(100,("nt owf password:"));
234 dump_data(100, nt_pwd.hash, sizeof(nt_pwd.hash));
237 if (lm_interactive_pwd)
238 arcfour_crypt(lm_pwd.hash, key, sizeof(lm_pwd.hash));
240 if (nt_interactive_pwd)
241 arcfour_crypt(nt_pwd.hash, key, sizeof(nt_pwd.hash));
243 #ifdef DEBUG_PASSWORD
244 DEBUG(100,("decrypt of lm owf password:"));
245 dump_data(100, lm_pwd.hash, sizeof(lm_pwd));
247 DEBUG(100,("decrypt of nt owf password:"));
248 dump_data(100, nt_pwd.hash, sizeof(nt_pwd));
251 if (lm_interactive_pwd)
252 SMBOWFencrypt(lm_pwd.hash, chal,
255 if (nt_interactive_pwd)
256 SMBOWFencrypt(nt_pwd.hash, chal,
259 /* Password info paranoia */
265 DATA_BLOB local_lm_blob;
266 DATA_BLOB local_nt_blob;
268 if (lm_interactive_pwd) {
269 local_lm_blob = data_blob(local_lm_response,
270 sizeof(local_lm_response));
273 if (nt_interactive_pwd) {
274 local_nt_blob = data_blob(local_nt_response,
275 sizeof(local_nt_response));
278 nt_status = make_user_info_map(
280 smb_name, client_domain, workstation_name,
282 lm_interactive_pwd ? &local_lm_blob : NULL,
283 nt_interactive_pwd ? &local_nt_blob : NULL,
284 lm_interactive_pwd ? &lm_pwd : NULL,
285 nt_interactive_pwd ? &nt_pwd : NULL,
286 NULL, AUTH_PASSWORD_HASH);
288 if (NT_STATUS_IS_OK(nt_status)) {
289 (*user_info)->logon_parameters = logon_parameters;
292 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
293 data_blob_free(&local_lm_blob);
294 data_blob_free(&local_nt_blob);
300 /****************************************************************************
301 Create an auth_usersupplied_data structure
302 ****************************************************************************/
304 bool make_user_info_for_reply(struct auth_usersupplied_info **user_info,
305 const char *smb_name,
306 const char *client_domain,
307 const struct tsocket_address *remote_address,
309 DATA_BLOB plaintext_password)
312 DATA_BLOB local_lm_blob;
313 DATA_BLOB local_nt_blob;
314 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
315 char *plaintext_password_string;
317 * Not encrypted - do so.
320 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted "
322 if (plaintext_password.data && plaintext_password.length) {
323 unsigned char local_lm_response[24];
325 #ifdef DEBUG_PASSWORD
326 DEBUG(10,("Unencrypted password (len %d):\n",
327 (int)plaintext_password.length));
328 dump_data(100, plaintext_password.data,
329 plaintext_password.length);
332 SMBencrypt( (const char *)plaintext_password.data,
333 (const uchar*)chal, local_lm_response);
334 local_lm_blob = data_blob(local_lm_response, 24);
336 /* We can't do an NT hash here, as the password needs to be
338 local_nt_blob = data_blob_null;
340 local_lm_blob = data_blob_null;
341 local_nt_blob = data_blob_null;
344 plaintext_password_string = talloc_strndup(talloc_tos(),
345 (const char *)plaintext_password.data,
346 plaintext_password.length);
347 if (!plaintext_password_string) {
351 ret = make_user_info_map(
352 user_info, smb_name, client_domain,
353 get_remote_machine_name(),
355 local_lm_blob.data ? &local_lm_blob : NULL,
356 local_nt_blob.data ? &local_nt_blob : NULL,
358 plaintext_password_string,
359 AUTH_PASSWORD_PLAIN);
361 if (plaintext_password_string) {
362 memset(plaintext_password_string, '\0', strlen(plaintext_password_string));
363 talloc_free(plaintext_password_string);
366 data_blob_free(&local_lm_blob);
367 return NT_STATUS_IS_OK(ret) ? True : False;
370 /****************************************************************************
371 Create an auth_usersupplied_data structure
372 ****************************************************************************/
374 NTSTATUS make_user_info_for_reply_enc(struct auth_usersupplied_info **user_info,
375 const char *smb_name,
376 const char *client_domain,
377 const struct tsocket_address *remote_address,
378 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
380 return make_user_info_map(user_info, smb_name,
382 get_remote_machine_name(),
384 lm_resp.data && (lm_resp.length > 0) ? &lm_resp : NULL,
385 nt_resp.data && (nt_resp.length > 0) ? &nt_resp : NULL,
387 AUTH_PASSWORD_RESPONSE);
390 /****************************************************************************
391 Create a guest user_info blob, for anonymous authenticaion.
392 ****************************************************************************/
394 bool make_user_info_guest(const struct tsocket_address *remote_address,
395 struct auth_usersupplied_info **user_info)
399 nt_status = make_user_info(user_info,
407 AUTH_PASSWORD_RESPONSE);
409 return NT_STATUS_IS_OK(nt_status) ? True : False;
412 static NTSTATUS log_nt_token(struct security_token *token)
414 TALLOC_CTX *frame = talloc_stackframe();
419 if ((lp_log_nt_token_command() == NULL) ||
420 (strlen(lp_log_nt_token_command()) == 0)) {
425 group_sidstr = talloc_strdup(frame, "");
426 for (i=1; i<token->num_sids; i++) {
427 group_sidstr = talloc_asprintf(
428 frame, "%s %s", group_sidstr,
429 sid_string_talloc(frame, &token->sids[i]));
432 command = talloc_string_sub(
433 frame, lp_log_nt_token_command(),
434 "%s", sid_string_talloc(frame, &token->sids[0]));
435 command = talloc_string_sub(frame, command, "%t", group_sidstr);
437 if (command == NULL) {
439 return NT_STATUS_NO_MEMORY;
442 DEBUG(8, ("running command: [%s]\n", command));
443 if (smbrun(command, NULL) != 0) {
444 DEBUG(0, ("Could not log NT token\n"));
446 return NT_STATUS_ACCESS_DENIED;
454 * Create the token to use from server_info->info3 and
455 * server_info->sids (the info3/sam groups). Find the unix gids.
458 NTSTATUS create_local_token(TALLOC_CTX *mem_ctx,
459 const struct auth_serversupplied_info *server_info,
460 DATA_BLOB *session_key,
461 struct auth3_session_info **session_info_out)
463 struct security_token *t;
466 struct dom_sid tmp_sid;
467 struct auth3_session_info *session_info;
468 struct wbcUnixId *ids;
470 /* Ensure we can't possible take a code path leading to a
473 return NT_STATUS_LOGON_FAILURE;
476 session_info = copy_serverinfo_session_info(mem_ctx, server_info);
479 return NT_STATUS_NO_MEMORY;
483 data_blob_free(&session_info->session_key);
484 session_info->session_key = data_blob_talloc(session_info,
486 session_key->length);
487 if (!session_info->session_key.data && session_key->length) {
488 return NT_STATUS_NO_MEMORY;
492 if (session_info->security_token) {
493 /* Just copy the token, it has already been finalised
494 * (nasty hack to support a cached guest session_info,
495 * and a possible strategy for auth_samba4 to pass in
496 * a finalised session) */
497 *session_info_out = session_info;
502 * If winbind is not around, we can not make much use of the SIDs the
503 * domain controller provided us with. Likewise if the user name was
504 * mapped to some local unix user.
507 if (((lp_server_role() == ROLE_DOMAIN_MEMBER) && !winbind_ping()) ||
508 (server_info->nss_token)) {
509 status = create_token_from_username(session_info,
510 session_info->unix_info->unix_name,
511 session_info->unix_info->guest,
512 &session_info->unix_token->uid,
513 &session_info->unix_token->gid,
514 &session_info->unix_info->unix_name,
515 &session_info->security_token);
518 status = create_local_nt_token_from_info3(session_info,
519 session_info->unix_info->guest,
521 &session_info->extra,
522 &session_info->security_token);
525 if (!NT_STATUS_IS_OK(status)) {
529 /* Convert the SIDs to gids. */
531 session_info->unix_token->ngroups = 0;
532 session_info->unix_token->groups = NULL;
534 t = session_info->security_token;
536 ids = talloc_array(talloc_tos(), struct wbcUnixId,
539 return NT_STATUS_NO_MEMORY;
542 if (!sids_to_unix_ids(t->sids, t->num_sids, ids)) {
544 return NT_STATUS_NO_MEMORY;
547 /* Start at index 1, where the groups start. */
549 for (i=1; i<t->num_sids; i++) {
551 if (ids[i].type != WBC_ID_TYPE_GID) {
552 DEBUG(10, ("Could not convert SID %s to gid, "
554 sid_string_dbg(&t->sids[i])));
557 if (!add_gid_to_array_unique(session_info, ids[i].id.gid,
558 &session_info->unix_token->groups,
559 &session_info->unix_token->ngroups)) {
560 return NT_STATUS_NO_MEMORY;
565 * Add the "Unix Group" SID for each gid to catch mapped groups
566 * and their Unix equivalent. This is to solve the backwards
567 * compatibility problem of 'valid users = +ntadmin' where
568 * ntadmin has been paired with "Domain Admins" in the group
569 * mapping table. Otherwise smb.conf would need to be changed
570 * to 'valid user = "Domain Admins"'. --jerry
572 * For consistency we also add the "Unix User" SID,
573 * so that the complete unix token is represented within
577 uid_to_unix_users_sid(session_info->unix_token->uid, &tmp_sid);
579 add_sid_to_array_unique(session_info->security_token, &tmp_sid,
580 &session_info->security_token->sids,
581 &session_info->security_token->num_sids);
583 for ( i=0; i<session_info->unix_token->ngroups; i++ ) {
584 gid_to_unix_groups_sid(session_info->unix_token->groups[i], &tmp_sid);
585 add_sid_to_array_unique(session_info->security_token, &tmp_sid,
586 &session_info->security_token->sids,
587 &session_info->security_token->num_sids);
590 security_token_debug(DBGC_AUTH, 10, session_info->security_token);
591 debug_unix_user_token(DBGC_AUTH, 10,
592 session_info->unix_token->uid,
593 session_info->unix_token->gid,
594 session_info->unix_token->ngroups,
595 session_info->unix_token->groups);
597 status = log_nt_token(session_info->security_token);
598 if (!NT_STATUS_IS_OK(status)) {
602 *session_info_out = session_info;
606 /***************************************************************************
607 Make (and fill) a server_info struct from a 'struct passwd' by conversion
609 ***************************************************************************/
611 NTSTATUS make_server_info_pw(struct auth_serversupplied_info **server_info,
616 struct samu *sampass = NULL;
617 char *qualified_name = NULL;
618 TALLOC_CTX *mem_ctx = NULL;
619 struct dom_sid u_sid;
620 enum lsa_SidType type;
621 struct auth_serversupplied_info *result;
624 * The SID returned in server_info->sam_account is based
625 * on our SAM sid even though for a pure UNIX account this should
626 * not be the case as it doesn't really exist in the SAM db.
627 * This causes lookups on "[in]valid users" to fail as they
628 * will lookup this name as a "Unix User" SID to check against
629 * the user token. Fix this by adding the "Unix User"\unix_username
630 * SID to the sid array. The correct fix should probably be
631 * changing the server_info->sam_account user SID to be a
632 * S-1-22 Unix SID, but this might break old configs where
633 * plaintext passwords were used with no SAM backend.
636 mem_ctx = talloc_init("make_server_info_pw_tmp");
638 return NT_STATUS_NO_MEMORY;
641 qualified_name = talloc_asprintf(mem_ctx, "%s\\%s",
642 unix_users_domain_name(),
644 if (!qualified_name) {
645 TALLOC_FREE(mem_ctx);
646 return NT_STATUS_NO_MEMORY;
649 if (!lookup_name(mem_ctx, qualified_name, LOOKUP_NAME_ALL,
652 TALLOC_FREE(mem_ctx);
653 return NT_STATUS_NO_SUCH_USER;
656 TALLOC_FREE(mem_ctx);
658 if (type != SID_NAME_USER) {
659 return NT_STATUS_NO_SUCH_USER;
662 if ( !(sampass = samu_new( NULL )) ) {
663 return NT_STATUS_NO_MEMORY;
666 status = samu_set_unix( sampass, pwd );
667 if (!NT_STATUS_IS_OK(status)) {
671 /* In pathological cases the above call can set the account
672 * name to the DOMAIN\username form. Reset the account name
673 * using unix_username */
674 pdb_set_username(sampass, unix_username, PDB_SET);
676 /* set the user sid to be the calculated u_sid */
677 pdb_set_user_sid(sampass, &u_sid, PDB_SET);
679 result = make_server_info(NULL);
680 if (result == NULL) {
681 TALLOC_FREE(sampass);
682 return NT_STATUS_NO_MEMORY;
685 status = samu_to_SamInfo3(result, sampass, lp_netbios_name(),
686 &result->info3, &result->extra);
687 TALLOC_FREE(sampass);
688 if (!NT_STATUS_IS_OK(status)) {
689 DEBUG(10, ("Failed to convert samu to info3: %s\n",
695 result->unix_name = talloc_strdup(result, unix_username);
696 result->sanitized_username = sanitize_username(result, unix_username);
698 if ((result->unix_name == NULL)
699 || (result->sanitized_username == NULL)) {
701 return NT_STATUS_NO_MEMORY;
704 result->utok.uid = pwd->pw_uid;
705 result->utok.gid = pwd->pw_gid;
707 *server_info = result;
712 static NTSTATUS get_guest_info3(TALLOC_CTX *mem_ctx,
713 struct netr_SamInfo3 *info3)
715 const char *guest_account = lp_guestaccount();
716 struct dom_sid domain_sid;
720 pwd = Get_Pwnam_alloc(mem_ctx, guest_account);
722 DEBUG(0,("SamInfo3_for_guest: Unable to locate guest "
723 "account [%s]!\n", guest_account));
724 return NT_STATUS_NO_SUCH_USER;
727 /* Set acount name */
728 tmp = talloc_strdup(mem_ctx, pwd->pw_name);
730 return NT_STATUS_NO_MEMORY;
732 init_lsa_String(&info3->base.account_name, tmp);
734 /* Set domain name */
735 tmp = talloc_strdup(mem_ctx, get_global_sam_name());
737 return NT_STATUS_NO_MEMORY;
739 init_lsa_StringLarge(&info3->base.domain, tmp);
742 sid_copy(&domain_sid, get_global_sam_sid());
744 info3->base.domain_sid = dom_sid_dup(mem_ctx, &domain_sid);
745 if (info3->base.domain_sid == NULL) {
746 return NT_STATUS_NO_MEMORY;
750 info3->base.rid = DOMAIN_RID_GUEST;
753 info3->base.primary_gid = BUILTIN_RID_GUESTS;
759 /***************************************************************************
760 Make (and fill) a user_info struct for a guest login.
761 This *must* succeed for smbd to start. If there is no mapping entry for
762 the guest gid, then create one.
764 The resulting structure is a 'session_info' because
765 create_local_token() has already been called on it. This is quite
766 nasty, as the auth subsystem isn't expect this, but the behaviour is
768 ***************************************************************************/
770 static NTSTATUS make_new_session_info_guest(struct auth3_session_info **session_info)
772 struct auth_serversupplied_info *server_info;
773 static const char zeros[16] = {0};
774 const char *guest_account = lp_guestaccount();
775 const char *domain = lp_netbios_name();
776 struct netr_SamInfo3 info3;
781 tmp_ctx = talloc_stackframe();
782 if (tmp_ctx == NULL) {
783 return NT_STATUS_NO_MEMORY;
788 status = get_guest_info3(tmp_ctx, &info3);
789 if (!NT_STATUS_IS_OK(status)) {
790 DEBUG(0, ("get_guest_info3 failed with %s\n",
795 status = make_server_info_info3(tmp_ctx,
800 if (!NT_STATUS_IS_OK(status)) {
801 DEBUG(0, ("make_server_info_info3 failed with %s\n",
806 server_info->guest = True;
808 /* This should not be done here (we should produce a server
809 * info, and later construct a session info from it), but for
810 * now this does not change the previous behaviours */
811 status = create_local_token(tmp_ctx, server_info, NULL, session_info);
812 TALLOC_FREE(server_info);
813 if (!NT_STATUS_IS_OK(status)) {
814 DEBUG(0, ("create_local_token failed: %s\n",
818 talloc_steal(NULL, *session_info);
820 /* annoying, but the Guest really does have a session key, and it is
822 (*session_info)->session_key = data_blob(zeros, sizeof(zeros));
824 alpha_strcpy(tmp, (*session_info)->info3->base.account_name.string,
825 ". _-$", sizeof(tmp));
826 (*session_info)->unix_info->sanitized_username = talloc_strdup(*session_info, tmp);
828 status = NT_STATUS_OK;
830 TALLOC_FREE(tmp_ctx);
834 /***************************************************************************
835 Make (and fill) a auth_session_info struct for a system user login.
836 This *must* succeed for smbd to start.
837 ***************************************************************************/
839 static NTSTATUS make_new_session_info_system(TALLOC_CTX *mem_ctx,
840 struct auth3_session_info **session_info)
845 pwd = getpwuid_alloc(mem_ctx, sec_initial_uid());
847 return NT_STATUS_NO_SUCH_USER;
850 status = make_session_info_from_username(mem_ctx,
855 if (!NT_STATUS_IS_OK(status)) {
859 (*session_info)->unix_info->system = true;
861 status = add_sid_to_array_unique((*session_info)->security_token->sids,
863 &(*session_info)->security_token->sids,
864 &(*session_info)->security_token->num_sids);
865 if (!NT_STATUS_IS_OK(status)) {
866 TALLOC_FREE((*session_info));
873 /****************************************************************************
874 Fake a auth3_session_info just from a username (as a
875 session_info structure, with create_local_token() already called on
877 ****************************************************************************/
879 NTSTATUS make_session_info_from_username(TALLOC_CTX *mem_ctx,
880 const char *username,
882 struct auth3_session_info **session_info)
884 struct auth_serversupplied_info *result;
888 pwd = Get_Pwnam_alloc(talloc_tos(), username);
890 return NT_STATUS_NO_SUCH_USER;
893 status = make_server_info_pw(&result, pwd->pw_name, pwd);
897 if (!NT_STATUS_IS_OK(status)) {
901 result->nss_token = true;
902 result->guest = is_guest;
904 /* Now turn the server_info into a session_info with the full token etc */
905 status = create_local_token(mem_ctx, result, NULL, session_info);
910 /* This function MUST only used to create the cached server_info for guest */
911 static struct auth_serversupplied_info *copy_session_info_serverinfo(TALLOC_CTX *mem_ctx,
912 const struct auth3_session_info *src)
914 struct auth_serversupplied_info *dst;
916 dst = make_server_info(mem_ctx);
921 /* This element must be provided to convert back to an auth_serversupplied_info */
922 SMB_ASSERT(src->unix_info);
924 dst->guest = src->unix_info->guest;
925 dst->system = src->unix_info->system;
927 /* This element must be provided to convert back to an auth_serversupplied_info */
928 SMB_ASSERT(src->unix_token);
929 dst->utok.uid = src->unix_token->uid;
930 dst->utok.gid = src->unix_token->gid;
931 dst->utok.ngroups = src->unix_token->ngroups;
932 if (src->unix_token->ngroups != 0) {
933 dst->utok.groups = (gid_t *)talloc_memdup(
934 dst, src->unix_token->groups,
935 sizeof(gid_t)*dst->utok.ngroups);
937 dst->utok.groups = NULL;
940 if (src->security_token) {
941 dst->security_token = dup_nt_token(dst, src->security_token);
942 if (!dst->security_token) {
948 dst->session_key = data_blob_talloc( dst, src->session_key.data,
949 src->session_key.length);
951 /* This is OK because this functions is only used for the
952 * GUEST account, which has all-zero keys for both values */
953 dst->lm_session_key = data_blob_talloc(dst, src->session_key.data,
954 src->session_key.length);
956 dst->info3 = copy_netr_SamInfo3(dst, src->info3);
961 dst->extra = src->extra;
963 dst->unix_name = talloc_strdup(dst, src->unix_info->unix_name);
964 if (!dst->unix_name) {
969 dst->sanitized_username = talloc_strdup(dst, src->unix_info->sanitized_username);
970 if (!dst->sanitized_username) {
978 static struct auth3_session_info *copy_serverinfo_session_info(TALLOC_CTX *mem_ctx,
979 const struct auth_serversupplied_info *src)
981 struct auth3_session_info *dst;
983 dst = make_auth3_session_info(mem_ctx);
988 dst->unix_token = talloc(dst, struct security_unix_token);
989 if (!dst->unix_token) {
993 dst->unix_token->uid = src->utok.uid;
994 dst->unix_token->gid = src->utok.gid;
995 dst->unix_token->ngroups = src->utok.ngroups;
996 if (src->utok.ngroups != 0) {
997 dst->unix_token->groups = (gid_t *)talloc_memdup(
998 dst->unix_token, src->utok.groups,
999 sizeof(gid_t)*dst->unix_token->ngroups);
1001 dst->unix_token->groups = NULL;
1004 if (src->security_token) {
1005 dst->security_token = dup_nt_token(dst, src->security_token);
1006 if (!dst->security_token) {
1012 dst->session_key = data_blob_talloc( dst, src->session_key.data,
1013 src->session_key.length);
1015 dst->info3 = copy_netr_SamInfo3(dst, src->info3);
1020 dst->extra = src->extra;
1022 dst->unix_info = talloc_zero(dst, struct auth_user_info_unix);
1023 if (!dst->unix_info) {
1028 dst->unix_info->unix_name = talloc_strdup(dst, src->unix_name);
1029 if (!dst->unix_info->unix_name) {
1034 dst->unix_info->sanitized_username = talloc_strdup(dst, src->sanitized_username);
1035 if (!dst->unix_info->sanitized_username) {
1040 dst->unix_info->guest = src->guest;
1041 dst->unix_info->system = src->system;
1046 struct auth3_session_info *copy_session_info(TALLOC_CTX *mem_ctx,
1047 const struct auth3_session_info *src)
1049 struct auth3_session_info *dst;
1051 dst = make_auth3_session_info(mem_ctx);
1056 if (src->unix_token) {
1057 dst->unix_token = talloc(dst, struct security_unix_token);
1058 if (!dst->unix_token) {
1062 dst->unix_token->uid = src->unix_token->uid;
1063 dst->unix_token->gid = src->unix_token->gid;
1064 dst->unix_token->ngroups = src->unix_token->ngroups;
1065 if (src->unix_token->ngroups != 0) {
1066 dst->unix_token->groups = (gid_t *)talloc_memdup(
1067 dst->unix_token, src->unix_token->groups,
1068 sizeof(gid_t)*dst->unix_token->ngroups);
1070 dst->unix_token->groups = NULL;
1073 dst->unix_token = NULL;
1076 if (src->security_token) {
1077 dst->security_token = dup_nt_token(dst, src->security_token);
1078 if (!dst->security_token) {
1084 dst->session_key = data_blob_talloc( dst, src->session_key.data,
1085 src->session_key.length);
1087 dst->info3 = copy_netr_SamInfo3(dst, src->info3);
1092 dst->extra = src->extra;
1094 if (src->unix_info) {
1095 dst->unix_info = talloc_zero(dst, struct auth_user_info_unix);
1096 if (!dst->unix_info) {
1101 dst->unix_info->unix_name = talloc_strdup(dst, src->unix_info->unix_name);
1102 if (!dst->unix_info->unix_name) {
1107 dst->unix_info->sanitized_username = talloc_strdup(dst, src->unix_info->sanitized_username);
1108 if (!dst->unix_info->sanitized_username) {
1113 dst->unix_info->guest = src->unix_info->guest;
1114 dst->unix_info->system = src->unix_info->system;
1121 * Set a new session key. Used in the rpc server where we have to override the
1122 * SMB level session key with SystemLibraryDTC
1125 bool session_info_set_session_key(struct auth3_session_info *info,
1126 DATA_BLOB session_key)
1128 TALLOC_FREE(info->session_key.data);
1130 info->session_key = data_blob_talloc(
1131 info, session_key.data, session_key.length);
1133 return (info->session_key.data != NULL);
1136 static struct auth3_session_info *guest_info = NULL;
1138 bool init_guest_info(void)
1140 if (guest_info != NULL)
1143 return NT_STATUS_IS_OK(make_new_session_info_guest(&guest_info));
1146 NTSTATUS make_server_info_guest(TALLOC_CTX *mem_ctx,
1147 struct auth_serversupplied_info **server_info)
1149 *server_info = copy_session_info_serverinfo(mem_ctx, guest_info);
1150 return (*server_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1153 NTSTATUS make_session_info_guest(TALLOC_CTX *mem_ctx,
1154 struct auth3_session_info **session_info)
1156 *session_info = copy_session_info(mem_ctx, guest_info);
1157 return (*session_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1160 static struct auth3_session_info *system_info = NULL;
1162 NTSTATUS init_system_info(void)
1164 if (system_info != NULL)
1165 return NT_STATUS_OK;
1167 return make_new_session_info_system(NULL, &system_info);
1170 NTSTATUS make_session_info_system(TALLOC_CTX *mem_ctx,
1171 struct auth3_session_info **session_info)
1173 if (system_info == NULL) return NT_STATUS_UNSUCCESSFUL;
1174 *session_info = copy_session_info(mem_ctx, system_info);
1175 return (*session_info != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1178 const struct auth3_session_info *get_session_info_system(void)
1183 bool copy_current_user(struct current_user *dst, struct current_user *src)
1186 struct security_token *nt_token;
1188 groups = (gid_t *)memdup(src->ut.groups,
1189 sizeof(gid_t) * src->ut.ngroups);
1190 if ((src->ut.ngroups != 0) && (groups == NULL)) {
1194 nt_token = dup_nt_token(NULL, src->nt_user_token);
1195 if (nt_token == NULL) {
1200 dst->conn = src->conn;
1201 dst->vuid = src->vuid;
1202 dst->ut.uid = src->ut.uid;
1203 dst->ut.gid = src->ut.gid;
1204 dst->ut.ngroups = src->ut.ngroups;
1205 dst->ut.groups = groups;
1206 dst->nt_user_token = nt_token;
1210 /***************************************************************************
1211 Purely internal function for make_server_info_info3
1212 ***************************************************************************/
1214 static NTSTATUS check_account(TALLOC_CTX *mem_ctx, const char *domain,
1215 const char *username, char **found_username,
1216 struct passwd **pwd,
1217 bool *username_was_mapped)
1219 char *orig_dom_user = NULL;
1220 char *dom_user = NULL;
1221 char *lower_username = NULL;
1222 char *real_username = NULL;
1223 struct passwd *passwd;
1225 lower_username = talloc_strdup(mem_ctx, username);
1226 if (!lower_username) {
1227 return NT_STATUS_NO_MEMORY;
1229 strlower_m( lower_username );
1231 orig_dom_user = talloc_asprintf(mem_ctx,
1234 *lp_winbind_separator(),
1236 if (!orig_dom_user) {
1237 return NT_STATUS_NO_MEMORY;
1240 /* Get the passwd struct. Try to create the account if necessary. */
1242 *username_was_mapped = map_username(mem_ctx, orig_dom_user, &dom_user);
1244 return NT_STATUS_NO_MEMORY;
1247 passwd = smb_getpwnam(mem_ctx, dom_user, &real_username, True );
1249 DEBUG(3, ("Failed to find authenticated user %s via "
1250 "getpwnam(), denying access.\n", dom_user));
1251 return NT_STATUS_NO_SUCH_USER;
1254 if (!real_username) {
1255 return NT_STATUS_NO_MEMORY;
1260 /* This is pointless -- there is no suport for differing
1261 unix and windows names. Make sure to always store the
1262 one we actually looked up and succeeded. Have I mentioned
1263 why I hate the 'winbind use default domain' parameter?
1266 *found_username = talloc_strdup( mem_ctx, real_username );
1268 return NT_STATUS_OK;
1271 /****************************************************************************
1272 Wrapper to allow the getpwnam() call to strip the domain name and
1273 try again in case a local UNIX user is already there. Also run through
1274 the username if we fallback to the username only.
1275 ****************************************************************************/
1277 struct passwd *smb_getpwnam( TALLOC_CTX *mem_ctx, const char *domuser,
1278 char **p_save_username, bool create )
1280 struct passwd *pw = NULL;
1282 char *username = NULL;
1284 /* we only save a copy of the username it has been mangled
1285 by winbindd use default domain */
1286 *p_save_username = NULL;
1288 /* don't call map_username() here since it has to be done higher
1289 up the stack so we don't call it multiple times */
1291 username = talloc_strdup(mem_ctx, domuser);
1296 p = strchr_m( username, *lp_winbind_separator() );
1298 /* code for a DOMAIN\user string */
1301 pw = Get_Pwnam_alloc( mem_ctx, domuser );
1303 /* make sure we get the case of the username correct */
1304 /* work around 'winbind use default domain = yes' */
1306 if ( !strchr_m( pw->pw_name, *lp_winbind_separator() ) ) {
1309 /* split the domain and username into 2 strings */
1313 *p_save_username = talloc_asprintf(mem_ctx,
1316 *lp_winbind_separator(),
1318 if (!*p_save_username) {
1323 *p_save_username = talloc_strdup(mem_ctx, pw->pw_name);
1330 /* setup for lookup of just the username */
1331 /* remember that p and username are overlapping memory */
1334 username = talloc_strdup(mem_ctx, p);
1340 /* just lookup a plain username */
1342 pw = Get_Pwnam_alloc(mem_ctx, username);
1344 /* Create local user if requested but only if winbindd
1345 is not running. We need to protect against cases
1346 where winbindd is failing and then prematurely
1347 creating users in /etc/passwd */
1349 if ( !pw && create && !winbind_ping() ) {
1350 /* Don't add a machine account. */
1351 if (username[strlen(username)-1] == '$')
1354 _smb_create_user(NULL, username, NULL);
1355 pw = Get_Pwnam_alloc(mem_ctx, username);
1358 /* one last check for a valid passwd struct */
1361 *p_save_username = talloc_strdup(mem_ctx, pw->pw_name);
1366 /***************************************************************************
1367 Make a server_info struct from the info3 returned by a domain logon
1368 ***************************************************************************/
1370 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx,
1371 const char *sent_nt_username,
1373 struct auth_serversupplied_info **server_info,
1374 struct netr_SamInfo3 *info3)
1376 static const char zeros[16] = {0, };
1378 NTSTATUS nt_status = NT_STATUS_OK;
1379 char *found_username = NULL;
1380 const char *nt_domain;
1381 const char *nt_username;
1382 bool username_was_mapped;
1384 struct auth_serversupplied_info *result;
1385 struct dom_sid *group_sid;
1386 struct netr_SamInfo3 *i3;
1389 Here is where we should check the list of
1390 trusted domains, and verify that the SID
1394 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
1396 /* If the server didn't give us one, just use the one we sent
1398 nt_username = sent_nt_username;
1401 nt_domain = talloc_strdup(mem_ctx, info3->base.domain.string);
1403 /* If the server didn't give us one, just use the one we sent
1408 /* If getpwnam() fails try the add user script (2.2.x behavior).
1410 We use the _unmapped_ username here in an attempt to provide
1411 consistent username mapping behavior between kerberos and NTLM[SSP]
1412 authentication in domain mode security. I.E. Username mapping
1413 should be applied to the fully qualified username
1414 (e.g. DOMAIN\user) and not just the login name. Yes this means we
1415 called map_username() unnecessarily in make_user_info_map() but
1416 that is how the current code is designed. Making the change here
1417 is the least disruptive place. -- jerry */
1419 /* this call will try to create the user if necessary */
1421 nt_status = check_account(mem_ctx, nt_domain, sent_nt_username,
1422 &found_username, &pwd,
1423 &username_was_mapped);
1425 if (!NT_STATUS_IS_OK(nt_status)) {
1429 result = make_server_info(NULL);
1430 if (result == NULL) {
1431 DEBUG(4, ("make_server_info failed!\n"));
1432 return NT_STATUS_NO_MEMORY;
1435 result->unix_name = talloc_strdup(result, found_username);
1437 result->sanitized_username = sanitize_username(result,
1439 if (result->sanitized_username == NULL) {
1440 TALLOC_FREE(result);
1441 return NT_STATUS_NO_MEMORY;
1444 /* copy in the info3 */
1445 result->info3 = i3 = copy_netr_SamInfo3(result, info3);
1446 if (result->info3 == NULL) {
1447 TALLOC_FREE(result);
1448 return NT_STATUS_NO_MEMORY;
1451 /* Fill in the unix info we found on the way */
1452 result->utok.uid = pwd->pw_uid;
1453 result->utok.gid = pwd->pw_gid;
1455 /* We can't just trust that the primary group sid sent us is something
1456 * we can really use. Obtain the useable sid, and store the original
1457 * one as an additional group if it had to be replaced */
1458 nt_status = get_primary_group_sid(mem_ctx, found_username,
1460 if (!NT_STATUS_IS_OK(nt_status)) {
1461 TALLOC_FREE(result);
1465 /* store and check if it is the same we got originally */
1466 sid_peek_rid(group_sid, &i3->base.primary_gid);
1467 if (i3->base.primary_gid != info3->base.primary_gid) {
1468 uint32_t n = i3->base.groups.count;
1469 /* not the same, store the original as an additional group */
1470 i3->base.groups.rids =
1471 talloc_realloc(i3, i3->base.groups.rids,
1472 struct samr_RidWithAttribute, n + 1);
1473 if (i3->base.groups.rids == NULL) {
1474 TALLOC_FREE(result);
1475 return NT_STATUS_NO_MEMORY;
1477 i3->base.groups.rids[n].rid = info3->base.primary_gid;
1478 i3->base.groups.rids[n].attributes = SE_GROUP_ENABLED;
1479 i3->base.groups.count = n + 1;
1482 /* ensure we are never given NULL session keys */
1484 if (memcmp(info3->base.key.key, zeros, sizeof(zeros)) == 0) {
1485 result->session_key = data_blob_null;
1487 result->session_key = data_blob_talloc(
1488 result, info3->base.key.key,
1489 sizeof(info3->base.key.key));
1492 if (memcmp(info3->base.LMSessKey.key, zeros, 8) == 0) {
1493 result->lm_session_key = data_blob_null;
1495 result->lm_session_key = data_blob_talloc(
1496 result, info3->base.LMSessKey.key,
1497 sizeof(info3->base.LMSessKey.key));
1500 result->nss_token |= username_was_mapped;
1502 result->guest = (info3->base.user_flags & NETLOGON_GUEST);
1504 *server_info = result;
1506 return NT_STATUS_OK;
1509 /*****************************************************************************
1510 Make a server_info struct from the wbcAuthUserInfo returned by a domain logon
1511 ******************************************************************************/
1513 NTSTATUS make_server_info_wbcAuthUserInfo(TALLOC_CTX *mem_ctx,
1514 const char *sent_nt_username,
1516 const struct wbcAuthUserInfo *info,
1517 struct auth_serversupplied_info **server_info)
1519 struct netr_SamInfo3 *info3;
1521 info3 = wbcAuthUserInfo_to_netr_SamInfo3(mem_ctx, info);
1523 return NT_STATUS_NO_MEMORY;
1526 return make_server_info_info3(mem_ctx,
1527 sent_nt_username, domain,
1528 server_info, info3);
1532 * Verify whether or not given domain is trusted.
1534 * @param domain_name name of the domain to be verified
1535 * @return true if domain is one of the trusted ones or
1536 * false if otherwise
1539 bool is_trusted_domain(const char* dom_name)
1541 struct dom_sid trustdom_sid;
1544 /* no trusted domains for a standalone server */
1546 if ( lp_server_role() == ROLE_STANDALONE )
1549 if (dom_name == NULL || dom_name[0] == '\0') {
1553 if (strequal(dom_name, get_global_sam_name())) {
1557 /* if we are a DC, then check for a direct trust relationships */
1561 DEBUG (5,("is_trusted_domain: Checking for domain trust with "
1562 "[%s]\n", dom_name ));
1563 ret = pdb_get_trusteddom_pw(dom_name, NULL, NULL, NULL);
1571 /* If winbind is around, ask it */
1573 result = wb_is_trusted_domain(dom_name);
1575 if (result == WBC_ERR_SUCCESS) {
1579 if (result == WBC_ERR_DOMAIN_NOT_FOUND) {
1580 /* winbind could not find the domain */
1584 /* The only other possible result is that winbind is not up
1585 and running. We need to update the trustdom_cache
1588 update_trustdom_cache();
1591 /* now the trustdom cache should be available a DC could still
1592 * have a transitive trust so fall back to the cache of trusted
1593 * domains (like a domain member would use */
1595 if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {