2 Unix SMB/CIFS implementation.
3 Authenticate by using a remote server
4 Copyright (C) Andrew Bartlett 2001-2002, 2008
5 Copyright (C) Jelmer Vernooij 2002
6 Copyright (C) Stefan Metzmacher 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "auth/auth.h"
24 #include "auth/credentials/credentials.h"
25 #include "libcli/security/security.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "param/param.h"
28 #include "libcli/resolve/resolve.h"
30 _PUBLIC_ NTSTATUS
auth4_server_init(void);
32 /* This version of 'security=server' rewirtten from scratch for Samba4
33 * libraries in 2008 */
36 static NTSTATUS
server_want_check(struct auth_method_context
*ctx
,
38 const struct auth_usersupplied_info
*user_info
)
43 * The challenge from the target server, when operating in security=server
45 static NTSTATUS
server_get_challenge(struct auth_method_context
*ctx
, TALLOC_CTX
*mem_ctx
, uint8_t chal
[8])
47 struct smb_composite_connect io
;
48 struct smbcli_options smb_options
;
49 const char **host_list
;
52 /* Make a connection to the target server, found by 'password server' in smb.conf */
54 lpcfg_smbcli_options(ctx
->auth_ctx
->lp_ctx
, &smb_options
);
56 /* Make a negprot, WITHOUT SPNEGO, so we get a challenge nice an easy */
57 io
.in
.options
.use_spnego
= false;
59 /* Hope we don't get * (the default), as this won't work... */
60 host_list
= lpcfg_passwordserver(ctx
->auth_ctx
->lp_ctx
);
62 return NT_STATUS_INTERNAL_ERROR
;
64 io
.in
.dest_host
= host_list
[0];
65 if (strequal(io
.in
.dest_host
, "*")) {
66 return NT_STATUS_INTERNAL_ERROR
;
68 io
.in
.dest_ports
= lpcfg_smb_ports(ctx
->auth_ctx
->lp_ctx
);
69 io
.in
.socket_options
= lpcfg_socket_options(ctx
->auth_ctx
->lp_ctx
);
70 io
.in
.gensec_settings
= lpcfg_gensec_settings(mem_ctx
, ctx
->auth_ctx
->lp_ctx
);
72 io
.in
.called_name
= strupper_talloc(mem_ctx
, io
.in
.dest_host
);
74 /* We don't want to get as far as the session setup */
75 io
.in
.credentials
= cli_credentials_init_anon(mem_ctx
);
76 cli_credentials_set_workstation(io
.in
.credentials
,
77 lpcfg_netbios_name(ctx
->auth_ctx
->lp_ctx
),
82 io
.in
.workgroup
= ""; /* only used with SPNEGO, disabled above */
84 io
.in
.options
= smb_options
;
86 lpcfg_smbcli_session_options(ctx
->auth_ctx
->lp_ctx
, &io
.in
.session_options
);
88 status
= smb_composite_connect(&io
, mem_ctx
, lpcfg_resolve_context(ctx
->auth_ctx
->lp_ctx
),
89 ctx
->auth_ctx
->event_ctx
);
90 NT_STATUS_NOT_OK_RETURN(status
);
92 if (io
.out
.tree
->session
->transport
->negotiate
.secblob
.length
!= 8) {
93 return NT_STATUS_INTERNAL_ERROR
;
95 memcpy(chal
, io
.out
.tree
->session
->transport
->negotiate
.secblob
.data
, 8);
96 ctx
->private_data
= talloc_steal(ctx
, io
.out
.tree
->session
);
101 * Return an error based on username
103 * This function allows the testing of obsure errors, as well as the generation
104 * of NT_STATUS -> DOS error mapping tables.
106 * This module is of no value to end-users.
108 * The password is ignored.
110 * @return An NTSTATUS value based on the username
113 static NTSTATUS
server_check_password(struct auth_method_context
*ctx
,
115 const struct auth_usersupplied_info
*user_info
,
116 struct auth_user_info_dc
**_user_info_dc
)
119 struct auth_user_info_dc
*user_info_dc
;
120 struct auth_user_info
*info
;
121 struct cli_credentials
*creds
;
122 struct smb_composite_sesssetup session_setup
;
124 struct smbcli_session
*session
= talloc_get_type(ctx
->private_data
, struct smbcli_session
);
126 creds
= cli_credentials_init(mem_ctx
);
128 NT_STATUS_HAVE_NO_MEMORY(creds
);
130 cli_credentials_set_username(creds
, user_info
->client
.account_name
, CRED_SPECIFIED
);
131 cli_credentials_set_domain(creds
, user_info
->client
.domain_name
, CRED_SPECIFIED
);
133 switch (user_info
->password_state
) {
134 case AUTH_PASSWORD_PLAIN
:
135 cli_credentials_set_password(creds
, user_info
->password
.plaintext
,
138 case AUTH_PASSWORD_HASH
:
139 cli_credentials_set_nt_hash(creds
, user_info
->password
.hash
.nt
,
143 case AUTH_PASSWORD_RESPONSE
:
144 cli_credentials_set_ntlm_response(creds
, &user_info
->password
.response
.lanman
, &user_info
->password
.response
.nt
, CRED_SPECIFIED
);
148 session_setup
.in
.sesskey
= session
->transport
->negotiate
.sesskey
;
149 session_setup
.in
.capabilities
= session
->transport
->negotiate
.capabilities
;
151 session_setup
.in
.credentials
= creds
;
152 session_setup
.in
.workgroup
= ""; /* Only used with SPNEGO, which we are not doing */
153 session_setup
.in
.gensec_settings
= lpcfg_gensec_settings(session
, ctx
->auth_ctx
->lp_ctx
);
155 /* Check password with remove server - this should be async some day */
156 nt_status
= smb_composite_sesssetup(session
, &session_setup
);
158 if (!NT_STATUS_IS_OK(nt_status
)) {
162 user_info_dc
= talloc(mem_ctx
, struct auth_user_info_dc
);
163 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
);
165 user_info_dc
->num_sids
= 1;
167 /* This returns a pointer to a struct dom_sid, which is the
168 * same as a 1 element list of struct dom_sid */
169 user_info_dc
->sids
= dom_sid_parse_talloc(user_info_dc
, SID_NT_ANONYMOUS
);
170 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
->sids
);
172 /* annoying, but the Anonymous really does have a session key,
173 and it is all zeros! */
174 user_info_dc
->user_session_key
= data_blob(NULL
, 0);
175 user_info_dc
->lm_session_key
= data_blob(NULL
, 0);
177 user_info_dc
->info
= info
= talloc_zero(user_info_dc
, struct auth_user_info
);
178 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
->info
);
180 info
->account_name
= talloc_strdup(user_info_dc
, user_info
->client
.account_name
);
181 NT_STATUS_HAVE_NO_MEMORY(info
->account_name
);
183 info
->domain_name
= talloc_strdup(user_info_dc
, user_info
->client
.domain_name
);
184 NT_STATUS_HAVE_NO_MEMORY(info
->domain_name
);
186 info
->full_name
= NULL
;
188 info
->logon_script
= talloc_strdup(user_info_dc
, "");
189 NT_STATUS_HAVE_NO_MEMORY(info
->logon_script
);
191 info
->profile_path
= talloc_strdup(user_info_dc
, "");
192 NT_STATUS_HAVE_NO_MEMORY(info
->profile_path
);
194 info
->home_directory
= talloc_strdup(user_info_dc
, "");
195 NT_STATUS_HAVE_NO_MEMORY(info
->home_directory
);
197 info
->home_drive
= talloc_strdup(user_info_dc
, "");
198 NT_STATUS_HAVE_NO_MEMORY(info
->home_drive
);
200 info
->last_logon
= 0;
201 info
->last_logoff
= 0;
202 info
->acct_expiry
= 0;
203 info
->last_password_change
= 0;
204 info
->allow_password_change
= 0;
205 info
->force_password_change
= 0;
207 info
->logon_count
= 0;
208 info
->bad_password_count
= 0;
210 info
->acct_flags
= ACB_NORMAL
;
212 info
->authenticated
= false;
214 *_user_info_dc
= user_info_dc
;
219 static const struct auth_operations server_auth_ops
= {
221 .get_challenge
= server_get_challenge
,
222 .want_check
= server_want_check
,
223 .check_password
= server_check_password
226 _PUBLIC_ NTSTATUS
auth4_server_init(void)
230 ret
= auth_register(&server_auth_ops
);
231 if (!NT_STATUS_IS_OK(ret
)) {
232 DEBUG(0,("Failed to register 'server' auth backend!\n"));