2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 a composite API for making handling a generic async session setup
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "auth/auth.h"
32 struct sesssetup_state {
33 union smb_sesssetup setup;
34 NTSTATUS gensec_status;
35 struct smb_composite_sesssetup *io;
36 struct smbcli_request *req;
41 store the user session key for a transport
43 static void set_user_session_key(struct smbcli_session *session,
44 const DATA_BLOB *session_key)
46 session->user_session_key = data_blob_talloc(session,
52 handler for completion of a smbcli_request sub-request
54 static void request_handler(struct smbcli_request *req)
56 struct composite_context *c = req->async.private;
57 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
58 struct smbcli_session *session = req->session;
59 DATA_BLOB session_key = data_blob(NULL, 0);
60 DATA_BLOB null_data_blob = data_blob(NULL, 0);
61 NTSTATUS session_key_err;
63 c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
65 switch (state->setup.old.level) {
66 case RAW_SESSSETUP_OLD:
67 state->io->out.vuid = state->setup.old.out.vuid;
70 case RAW_SESSSETUP_NT1:
71 state->io->out.vuid = state->setup.nt1.out.vuid;
74 case RAW_SESSSETUP_SPNEGO:
75 session->vuid = state->io->out.vuid = state->setup.spnego.out.vuid;
76 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
77 !NT_STATUS_IS_OK(c->status)) {
80 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
82 /* The status value here, from the earlier pass at GENSEC is
83 * vital to the security of the system. Even if the other end
84 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
85 * you must keep feeding it blobs, or else the remote
86 * host/attacker might avoid mutal authentication
89 state->gensec_status = gensec_update(session->gensec, state,
90 state->setup.spnego.out.secblob,
91 &state->setup.spnego.in.secblob);
92 c->status = state->gensec_status;
93 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
94 !NT_STATUS_IS_OK(c->status)) {
98 state->setup.spnego.in.secblob = data_blob(NULL, 0);
101 /* we need to do another round of session setup. We keep going until both sides
103 session_key_err = gensec_session_key(session->gensec, &session_key);
104 if (NT_STATUS_IS_OK(session_key_err)) {
105 set_user_session_key(session, &session_key);
106 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
109 if (state->setup.spnego.in.secblob.length) {
110 state->req = smb_raw_sesssetup_send(session, &state->setup);
111 state->req->async.fn = request_handler;
112 state->req->async.private = c;
117 /* enforce the local signing required flag */
118 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
119 if (!session->transport->negotiate.sign_info.doing_signing
120 && session->transport->negotiate.sign_info.mandatory_signing) {
121 DEBUG(0, ("SMB signing required, but server does not support it\n"));
122 c->status = NT_STATUS_ACCESS_DENIED;
126 if (NT_STATUS_IS_OK(c->status)) {
127 c->state = COMPOSITE_STATE_DONE;
129 c->state = COMPOSITE_STATE_ERROR;
138 send a nt1 style session setup
140 static NTSTATUS session_setup_nt1(struct composite_context *c,
141 struct smbcli_session *session,
142 struct smb_composite_sesssetup *io,
143 struct smbcli_request **req)
146 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
147 const char *password = cli_credentials_get_password(io->in.credentials);
148 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
149 DATA_BLOB session_key;
150 int flags = CLI_CRED_NTLM_AUTH;
151 if (lp_client_lanman_auth()) {
152 flags |= CLI_CRED_LANMAN_AUTH;
155 if (lp_client_ntlmv2_auth()) {
156 flags |= CLI_CRED_NTLMv2_AUTH;
159 state->setup.nt1.level = RAW_SESSSETUP_NT1;
160 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
161 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
162 state->setup.nt1.in.vc_num = 1;
163 state->setup.nt1.in.sesskey = io->in.sesskey;
164 state->setup.nt1.in.capabilities = io->in.capabilities;
165 state->setup.nt1.in.os = "Unix";
166 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
168 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
169 &state->setup.nt1.in.user,
170 &state->setup.nt1.in.domain);
173 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
174 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
176 session->transport->negotiate.secblob,
178 &state->setup.nt1.in.password1,
179 &state->setup.nt1.in.password2,
181 NT_STATUS_NOT_OK_RETURN(nt_status);
183 smbcli_transport_simple_set_signing(session->transport, session_key,
184 state->setup.nt1.in.password2);
185 set_user_session_key(session, &session_key);
187 data_blob_free(&session_key);
188 } else if (lp_client_plaintext_auth()) {
189 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
190 state->setup.nt1.in.password2 = data_blob(NULL, 0);
192 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
193 return NT_STATUS_INVALID_PARAMETER;
196 *req = smb_raw_sesssetup_send(session, &state->setup);
198 return NT_STATUS_NO_MEMORY;
200 return (*req)->status;
205 old style session setup (pre NT1 protocol level)
207 static NTSTATUS session_setup_old(struct composite_context *c,
208 struct smbcli_session *session,
209 struct smb_composite_sesssetup *io,
210 struct smbcli_request **req)
213 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
214 const char *password = cli_credentials_get_password(io->in.credentials);
215 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
216 DATA_BLOB session_key;
218 if (lp_client_lanman_auth()) {
219 flags |= CLI_CRED_LANMAN_AUTH;
222 if (lp_client_ntlmv2_auth()) {
223 flags |= CLI_CRED_NTLMv2_AUTH;
226 state->setup.old.level = RAW_SESSSETUP_OLD;
227 state->setup.old.in.bufsize = session->transport->options.max_xmit;
228 state->setup.old.in.mpx_max = session->transport->options.max_mux;
229 state->setup.old.in.vc_num = 1;
230 state->setup.old.in.sesskey = io->in.sesskey;
231 state->setup.old.in.os = "Unix";
232 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
233 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
234 &state->setup.old.in.user,
235 &state->setup.old.in.domain);
237 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
238 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
240 session->transport->negotiate.secblob,
242 &state->setup.old.in.password,
245 NT_STATUS_NOT_OK_RETURN(nt_status);
246 set_user_session_key(session, &session_key);
248 data_blob_free(&session_key);
249 } else if (lp_client_plaintext_auth()) {
250 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
252 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
253 return NT_STATUS_INVALID_PARAMETER;
256 *req = smb_raw_sesssetup_send(session, &state->setup);
258 return NT_STATUS_NO_MEMORY;
260 return (*req)->status;
265 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
267 static NTSTATUS session_setup_spnego(struct composite_context *c,
268 struct smbcli_session *session,
269 struct smb_composite_sesssetup *io,
270 struct smbcli_request **req)
272 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
273 NTSTATUS status, session_key_err;
274 DATA_BLOB session_key = data_blob(NULL, 0);
275 DATA_BLOB null_data_blob = data_blob(NULL, 0);
276 const char *chosen_oid = NULL;
278 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
279 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
280 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
281 state->setup.spnego.in.vc_num = 1;
282 state->setup.spnego.in.sesskey = io->in.sesskey;
283 state->setup.spnego.in.capabilities = io->in.capabilities;
284 state->setup.spnego.in.os = "Unix";
285 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
286 state->setup.spnego.in.workgroup = io->in.workgroup;
288 state->setup.spnego.out.vuid = session->vuid;
290 smbcli_temp_set_signing(session->transport);
292 status = gensec_client_start(session, &session->gensec, c->event_ctx);
293 if (!NT_STATUS_IS_OK(status)) {
294 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
298 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
300 status = gensec_set_credentials(session->gensec, io->in.credentials);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
307 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
308 if (!NT_STATUS_IS_OK(status)) {
309 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
314 status = gensec_set_target_service(session->gensec, "cifs");
315 if (!NT_STATUS_IS_OK(status)) {
316 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
321 if (session->transport->negotiate.secblob.length) {
322 chosen_oid = GENSEC_OID_SPNEGO;
323 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
326 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
327 chosen_oid = GENSEC_OID_NTLMSSP;
328 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
329 if (!NT_STATUS_IS_OK(status)) {
330 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
331 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
336 /* without a sec blob, means raw NTLMSSP */
337 chosen_oid = GENSEC_OID_NTLMSSP;
338 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
341 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
345 if (chosen_oid == GENSEC_OID_SPNEGO) {
346 status = gensec_update(session->gensec, state,
347 session->transport->negotiate.secblob,
348 &state->setup.spnego.in.secblob);
350 status = gensec_update(session->gensec, state,
352 &state->setup.spnego.in.secblob);
356 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
357 !NT_STATUS_IS_OK(status)) {
358 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
359 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
362 state->gensec_status = status;
364 session_key_err = gensec_session_key(session->gensec, &session_key);
365 if (NT_STATUS_IS_OK(session_key_err)) {
366 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
369 *req = smb_raw_sesssetup_send(session, &state->setup);
371 return NT_STATUS_NO_MEMORY;
373 return (*req)->status;
378 composite session setup function that hides the details of all the
379 different session setup varients, including the multi-pass nature of
382 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
383 struct smb_composite_sesssetup *io)
385 struct composite_context *c;
386 struct sesssetup_state *state;
389 c = talloc_zero(session, struct composite_context);
390 if (c == NULL) return NULL;
392 state = talloc(c, struct sesssetup_state);
400 c->state = COMPOSITE_STATE_IN_PROGRESS;
401 c->private_data = state;
402 c->event_ctx = session->transport->socket->event.ctx;
404 /* no session setup at all in earliest protocol varients */
405 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
406 ZERO_STRUCT(io->out);
407 c->state = COMPOSITE_STATE_DONE;
411 /* see what session setup interface we will use */
412 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
413 status = session_setup_old(c, session, io, &state->req);
414 } else if (!session->transport->options.use_spnego ||
415 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
416 status = session_setup_nt1(c, session, io, &state->req);
418 status = session_setup_spnego(c, session, io, &state->req);
421 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
422 NT_STATUS_IS_OK(status)) {
423 state->req->async.fn = request_handler;
424 state->req->async.private = c;
428 c->state = COMPOSITE_STATE_ERROR;
435 receive a composite session setup reply
437 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
440 status = composite_wait(c);
446 sync version of smb_composite_sesssetup
448 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
450 struct composite_context *c = smb_composite_sesssetup_send(session, io);
451 return smb_composite_sesssetup_recv(c);