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 "auth/auth.h"
30 struct sesssetup_state {
31 union smb_sesssetup setup;
32 NTSTATUS session_key_err;
33 struct smb_composite_sesssetup *io;
34 struct smbcli_request *req;
39 form an encrypted lanman password from a plaintext password
40 and the server supplied challenge
42 static DATA_BLOB lanman_blob(TALLOC_CTX *mem_ctx, const char *pass, DATA_BLOB challenge)
44 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, 24);
45 SMBencrypt(pass, challenge.data, blob.data);
50 form an encrypted NT password from a plaintext password
51 and the server supplied challenge
53 static DATA_BLOB nt_blob(TALLOC_CTX *mem_ctx, const char *pass, DATA_BLOB challenge)
55 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, 24);
56 SMBNTencrypt(pass, challenge.data, blob.data);
61 store the user session key for a transport
63 static void set_user_session_key(struct smbcli_session *session,
64 const DATA_BLOB *session_key)
66 session->user_session_key = data_blob_talloc(session,
72 setup signing for a NT1 style session setup
74 static void use_nt1_session_keys(struct smbcli_session *session,
75 const char *password, const DATA_BLOB *nt_response)
77 struct smbcli_transport *transport = session->transport;
79 DATA_BLOB session_key = data_blob_talloc(session, NULL, 16);
81 E_md4hash(password, nt_hash);
82 SMBsesskeygen_ntv1(nt_hash, session_key.data);
84 smbcli_transport_simple_set_signing(transport, session_key, *nt_response);
86 set_user_session_key(session, &session_key);
87 data_blob_free(&session_key);
92 handler for completion of a smbcli_request sub-request
94 static void request_handler(struct smbcli_request *req)
96 struct composite_context *c = req->async.private;
97 struct sesssetup_state *state = talloc_get_type(c->private, struct sesssetup_state);
98 struct smbcli_session *session = req->session;
99 DATA_BLOB session_key = data_blob(NULL, 0);
100 DATA_BLOB null_data_blob = data_blob(NULL, 0);
102 c->status = smb_raw_session_setup_recv(req, state, &state->setup);
104 switch (state->setup.old.level) {
105 case RAW_SESSSETUP_OLD:
106 state->io->out.vuid = state->setup.old.out.vuid;
109 case RAW_SESSSETUP_NT1:
110 state->io->out.vuid = state->setup.nt1.out.vuid;
113 case RAW_SESSSETUP_SPNEGO:
114 session->vuid = state->io->out.vuid = state->setup.spnego.out.vuid;
115 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
116 !NT_STATUS_IS_OK(c->status)) {
119 c->status = gensec_update(session->gensec, state,
120 state->setup.spnego.out.secblob,
121 &state->setup.spnego.in.secblob);
122 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
123 !NT_STATUS_IS_OK(c->status)) {
126 if (state->setup.spnego.in.secblob.length == 0) {
130 /* we need to do another round of session setup. We keep going until both sides
132 state->session_key_err = gensec_session_key(session->gensec, &session_key);
133 if (NT_STATUS_IS_OK(state->session_key_err)) {
134 set_user_session_key(session, &session_key);
135 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
138 state->req = smb_raw_session_setup_send(session, &state->setup);
139 state->req->async.fn = request_handler;
140 state->req->async.private = c;
144 /* enforce the local signing required flag */
145 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
146 if (!session->transport->negotiate.sign_info.doing_signing
147 && session->transport->negotiate.sign_info.mandatory_signing) {
148 DEBUG(0, ("SMB signing required, but server does not support it\n"));
149 c->status = NT_STATUS_ACCESS_DENIED;
153 if (NT_STATUS_IS_OK(c->status)) {
154 c->state = SMBCLI_REQUEST_DONE;
156 c->state = SMBCLI_REQUEST_ERROR;
165 send a nt1 style session setup
167 static struct smbcli_request *session_setup_nt1(struct composite_context *c,
168 struct smbcli_session *session,
169 struct smb_composite_sesssetup *io)
171 struct sesssetup_state *state = talloc_get_type(c->private, struct sesssetup_state);
172 const char *password = cli_credentials_get_password(io->in.credentials);
174 state->setup.nt1.level = RAW_SESSSETUP_NT1;
175 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
176 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
177 state->setup.nt1.in.vc_num = 1;
178 state->setup.nt1.in.sesskey = io->in.sesskey;
179 state->setup.nt1.in.capabilities = io->in.capabilities;
180 state->setup.nt1.in.os = "Unix";
181 state->setup.nt1.in.lanman = "Samba";
182 state->setup.nt1.in.user = cli_credentials_get_username(io->in.credentials);
183 state->setup.nt1.in.domain = cli_credentials_get_domain(io->in.credentials);
186 state->setup.nt1.in.password1 = data_blob(NULL, 0);
187 state->setup.nt1.in.password2 = data_blob(NULL, 0);
188 } else if (session->transport->negotiate.sec_mode &
189 NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
190 state->setup.nt1.in.password1 = lanman_blob(state, password,
191 session->transport->negotiate.secblob);
192 state->setup.nt1.in.password2 = nt_blob(state, password,
193 session->transport->negotiate.secblob);
194 use_nt1_session_keys(session, password, &state->setup.nt1.in.password2);
196 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
197 state->setup.nt1.in.password2 = data_blob(NULL, 0);
200 return smb_raw_session_setup_send(session, &state->setup);
205 old style session setup (pre NT1 protocol level)
207 static struct smbcli_request *session_setup_old(struct composite_context *c,
208 struct smbcli_session *session,
209 struct smb_composite_sesssetup *io)
211 struct sesssetup_state *state = talloc_get_type(c->private, struct sesssetup_state);
212 const char *password = cli_credentials_get_password(io->in.credentials);
214 state->setup.old.level = RAW_SESSSETUP_OLD;
215 state->setup.old.in.bufsize = session->transport->options.max_xmit;
216 state->setup.old.in.mpx_max = session->transport->options.max_mux;
217 state->setup.old.in.vc_num = 1;
218 state->setup.old.in.sesskey = io->in.sesskey;
219 state->setup.old.in.domain = cli_credentials_get_domain(io->in.credentials);
220 state->setup.old.in.user = cli_credentials_get_username(io->in.credentials);
221 state->setup.old.in.os = "Unix";
222 state->setup.old.in.lanman = "Samba";
225 state->setup.old.in.password = data_blob(NULL, 0);
226 } else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
227 state->setup.old.in.password = lanman_blob(state, password,
228 session->transport->negotiate.secblob);
230 state->setup.old.in.password = data_blob_talloc(state,
235 return smb_raw_session_setup_send(session, &state->setup);
240 old style session setup (pre NT1 protocol level)
242 static struct smbcli_request *session_setup_spnego(struct composite_context *c,
243 struct smbcli_session *session,
244 struct smb_composite_sesssetup *io)
246 struct sesssetup_state *state = talloc_get_type(c->private, struct sesssetup_state);
248 DATA_BLOB session_key = data_blob(NULL, 0);
249 DATA_BLOB null_data_blob = data_blob(NULL, 0);
250 const char *chosen_oid = NULL;
252 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
253 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
254 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
255 state->setup.spnego.in.vc_num = 1;
256 state->setup.spnego.in.sesskey = io->in.sesskey;
257 state->setup.spnego.in.capabilities = io->in.capabilities;
258 state->setup.spnego.in.os = "Unix";
259 state->setup.spnego.in.lanman = "Samba";
260 state->setup.spnego.in.workgroup = io->in.workgroup;
262 state->setup.spnego.out.vuid = session->vuid;
264 smbcli_temp_set_signing(session->transport);
266 status = gensec_client_start(session, &session->gensec);
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
272 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
274 status = gensec_set_credentials(session->gensec, io->in.credentials);
275 if (!NT_STATUS_IS_OK(status)) {
276 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n",
281 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
282 if (!NT_STATUS_IS_OK(status)) {
283 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
288 status = gensec_set_target_service(session->gensec, "CIFS");
289 if (!NT_STATUS_IS_OK(status)) {
290 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
295 if (session->transport->negotiate.secblob.length) {
296 chosen_oid = GENSEC_OID_SPNEGO;
298 /* without a sec blob, means raw NTLMSSP */
299 chosen_oid = GENSEC_OID_NTLMSSP;
302 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
303 if (!NT_STATUS_IS_OK(status)) {
304 DEBUG(1, ("Failed to start set GENSEC client SPNEGO mechanism %s: %s\n",
305 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
309 status = gensec_update(session->gensec, state,
310 session->transport->negotiate.secblob,
311 &state->setup.spnego.in.secblob);
312 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
313 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
314 gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
318 state->session_key_err = gensec_session_key(session->gensec, &session_key);
319 if (NT_STATUS_IS_OK(state->session_key_err)) {
320 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
323 return smb_raw_session_setup_send(session, &state->setup);
328 composite session setup function that hides the details of all the
329 different session setup varients, including the multi-pass nature of
332 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
333 struct smb_composite_sesssetup *io)
335 struct composite_context *c;
336 struct sesssetup_state *state;
338 c = talloc_zero(session, struct composite_context);
339 if (c == NULL) goto failed;
341 state = talloc(c, struct sesssetup_state);
342 if (state == NULL) goto failed;
346 c->state = SMBCLI_REQUEST_SEND;
348 c->event_ctx = session->transport->socket->event.ctx;
350 /* no session setup at all in earliest protocol varients */
351 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
352 ZERO_STRUCT(io->out);
353 c->state = SMBCLI_REQUEST_DONE;
357 /* see what session setup interface we will use */
358 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
359 state->req = session_setup_old(c, session, io);
360 } else if (!session->transport->options.use_spnego ||
361 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
362 state->req = session_setup_nt1(c, session, io);
364 state->req = session_setup_spnego(c, session, io);
367 if (state->req == NULL) goto failed;
369 state->req->async.fn = request_handler;
370 state->req->async.private = c;
381 receive a composite session setup reply
383 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
386 status = composite_wait(c);
392 sync version of smb_composite_sesssetup
394 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
396 struct composite_context *c = smb_composite_sesssetup_send(session, io);
397 return smb_composite_sesssetup_recv(c);