2 Unix SMB/CIFS implementation.
4 SMB2 client session handling
6 Copyright (C) Andrew Tridgell 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/composite/composite.h"
28 #include "auth/gensec/gensec.h"
31 initialise a smb2_session structure
33 struct smb2_session *smb2_session_init(struct smb2_transport *transport,
34 TALLOC_CTX *parent_ctx, BOOL primary)
36 struct smb2_session *session;
39 session = talloc_zero(parent_ctx, struct smb2_session);
44 session->transport = talloc_steal(session, transport);
46 session->transport = talloc_reference(session, transport);
49 /* prepare a gensec context for later use */
50 status = gensec_client_start(session, &session->gensec,
51 session->transport->socket->event.ctx);
52 if (!NT_STATUS_IS_OK(status)) {
57 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
63 send a session setup request
65 struct smb2_request *smb2_session_setup_send(struct smb2_session *session,
66 struct smb2_session_setup *io)
68 struct smb2_request *req;
71 req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP,
72 0x10, io->in.secblob.length);
73 if (req == NULL) return NULL;
75 SBVAL(req->out.hdr, SMB2_HDR_UID, session->uid);
76 SSVAL(req->out.body, 0x02, 0); /* pad */
77 SIVAL(req->out.body, 0x04, io->in.unknown2);
78 SIVAL(req->out.body, 0x08, io->in.unknown3);
80 req->session = session;
82 status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
83 if (!NT_STATUS_IS_OK(status)) {
88 smb2_transport_send(req);
95 recv a session setup reply
97 NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
98 struct smb2_session_setup *io)
102 if (!smb2_request_receive(req) ||
103 (smb2_request_is_error(req) &&
104 !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
105 return smb2_request_destroy(req);
108 SMB2_CHECK_PACKET_RECV(req, 0x08, True);
110 io->out._pad = SVAL(req->in.body, 0x02);
111 io->out.uid = BVAL(req->in.hdr, SMB2_HDR_UID);
113 status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
114 if (!NT_STATUS_IS_OK(status)) {
115 smb2_request_destroy(req);
119 return smb2_request_destroy(req);
123 sync session setup request
125 NTSTATUS smb2_session_setup(struct smb2_session *session,
126 TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
128 struct smb2_request *req = smb2_session_setup_send(session, io);
129 return smb2_session_setup_recv(req, mem_ctx, io);
133 struct smb2_session_state {
134 struct smb2_session_setup io;
135 struct smb2_request *req;
136 NTSTATUS gensec_status;
140 handle continuations of the spnego session setup
142 static void session_request_handler(struct smb2_request *req)
144 struct composite_context *c = talloc_get_type(req->async.private,
145 struct composite_context);
146 struct smb2_session_state *state = talloc_get_type(c->private_data,
147 struct smb2_session_state);
148 struct smb2_session *session = req->session;
150 c->status = smb2_session_setup_recv(req, c, &state->io);
151 if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
152 (NT_STATUS_IS_OK(c->status) &&
153 NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
154 NTSTATUS session_key_err;
155 DATA_BLOB session_key;
156 c->status = gensec_update(session->gensec, c,
157 state->io.out.secblob,
158 &state->io.in.secblob);
159 state->gensec_status = c->status;
161 session_key_err = gensec_session_key(session->gensec, &session_key);
162 if (NT_STATUS_IS_OK(session_key_err)) {
163 session->session_key = session_key;
167 session->uid = state->io.out.uid;
169 if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
170 state->req = smb2_session_setup_send(session, &state->io);
171 if (state->req == NULL) {
172 composite_error(c, NT_STATUS_NO_MEMORY);
175 state->req->async.fn = session_request_handler;
176 state->req->async.private = c;
180 if (!NT_STATUS_IS_OK(c->status)) {
181 composite_error(c, c->status);
189 a composite function that does a full SPNEGO session setup
191 struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session,
192 struct cli_credentials *credentials)
194 struct composite_context *c;
195 struct smb2_session_state *state;
197 c = talloc_zero(session, struct composite_context);
198 if (c == NULL) return NULL;
200 state = talloc(c, struct smb2_session_state);
202 c->status = NT_STATUS_NO_MEMORY;
206 c->state = COMPOSITE_STATE_IN_PROGRESS;
207 c->private_data = state;
208 c->event_ctx = session->transport->socket->event.ctx;
210 ZERO_STRUCT(state->io);
211 state->io.in._pad = 0x0;
212 state->io.in.unknown2 = 0xF;
213 state->io.in.unknown3 = 0x00;
215 c->status = gensec_set_credentials(session->gensec, credentials);
216 if (!NT_STATUS_IS_OK(c->status)) {
220 c->status = gensec_set_target_hostname(session->gensec,
221 session->transport->socket->hostname);
222 if (!NT_STATUS_IS_OK(c->status)) {
226 c->status = gensec_set_target_service(session->gensec, "cifs");
227 if (!NT_STATUS_IS_OK(c->status)) {
231 c->status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
232 if (!NT_STATUS_IS_OK(c->status)) {
236 c->status = gensec_update(session->gensec, c,
237 session->transport->negotiate.secblob,
238 &state->io.in.secblob);
239 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
242 state->gensec_status = c->status;
244 state->req = smb2_session_setup_send(session, &state->io);
245 if (state->req == NULL) {
246 c->status = NT_STATUS_NO_MEMORY;
250 state->req->async.fn = session_request_handler;
251 state->req->async.private = c;
256 composite_trigger_error(c);
261 receive a composite session setup reply
263 NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
266 status = composite_wait(c);
272 sync version of smb2_session_setup_spnego
274 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session,
275 struct cli_credentials *credentials)
277 struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
278 return smb2_session_setup_spnego_recv(c);