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 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/>.
24 #include "lib/util/tevent_ntstatus.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "auth/gensec/gensec.h"
33 initialise a smb2_session structure
35 struct smb2_session *smb2_session_init(struct smb2_transport *transport,
36 struct gensec_settings *settings,
37 TALLOC_CTX *parent_ctx, bool primary)
39 struct smb2_session *session;
42 session = talloc_zero(parent_ctx, struct smb2_session);
47 session->transport = talloc_steal(session, transport);
49 session->transport = talloc_reference(session, transport);
52 session->pid = getpid();
54 /* prepare a gensec context for later use */
55 status = gensec_client_start(session, &session->gensec,
57 if (!NT_STATUS_IS_OK(status)) {
62 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
68 send a session setup request
70 struct smb2_request *smb2_session_setup_send(struct smb2_session *session,
71 struct smb2_session_setup *io)
73 struct smb2_request *req;
76 req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP,
77 0x18, true, io->in.secblob.length);
78 if (req == NULL) return NULL;
80 SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID, session->uid);
81 SCVAL(req->out.body, 0x02, io->in.vc_number);
82 SCVAL(req->out.body, 0x03, io->in.security_mode);
83 SIVAL(req->out.body, 0x04, io->in.capabilities);
84 SIVAL(req->out.body, 0x08, io->in.channel);
85 SBVAL(req->out.body, 0x10, io->in.previous_sessionid);
87 req->session = session;
89 status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
90 if (!NT_STATUS_IS_OK(status)) {
95 smb2_transport_send(req);
102 recv a session setup reply
104 NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
105 struct smb2_session_setup *io)
109 if (!smb2_request_receive(req) ||
110 (smb2_request_is_error(req) &&
111 !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
112 return smb2_request_destroy(req);
115 SMB2_CHECK_PACKET_RECV(req, 0x08, true);
117 io->out.session_flags = SVAL(req->in.body, 0x02);
118 io->out.uid = BVAL(req->in.hdr, SMB2_HDR_SESSION_ID);
120 status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
121 if (!NT_STATUS_IS_OK(status)) {
122 smb2_request_destroy(req);
126 return smb2_request_destroy(req);
130 sync session setup request
132 NTSTATUS smb2_session_setup(struct smb2_session *session,
133 TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
135 struct smb2_request *req = smb2_session_setup_send(session, io);
136 return smb2_session_setup_recv(req, mem_ctx, io);
139 struct smb2_session_setup_spnego_state {
140 struct smb2_session_setup io;
141 struct smb2_request *req;
142 NTSTATUS gensec_status;
145 static void smb2_session_setup_spnego_handler(struct smb2_request *req);
148 a composite function that does a full SPNEGO session setup
150 struct tevent_req *smb2_session_setup_spnego_send(TALLOC_CTX *mem_ctx,
151 struct tevent_context *ev,
152 struct smb2_session *session,
153 struct cli_credentials *credentials)
155 struct tevent_req *req;
156 struct smb2_session_setup_spnego_state *state;
157 const char *chosen_oid;
158 struct smb2_request *subreq;
161 req = tevent_req_create(mem_ctx, &state,
162 struct smb2_session_setup_spnego_state);
167 ZERO_STRUCT(state->io);
168 state->io.in.vc_number = 0;
169 if (session->transport->signing_required) {
170 state->io.in.security_mode =
171 SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
173 state->io.in.capabilities = 0;
174 state->io.in.channel = 0;
175 state->io.in.previous_sessionid = 0;
177 status = gensec_set_credentials(session->gensec, credentials);
178 if (tevent_req_nterror(req, status)) {
179 return tevent_req_post(req, ev);
182 status = gensec_set_target_hostname(session->gensec,
183 session->transport->socket->hostname);
184 if (tevent_req_nterror(req, status)) {
185 return tevent_req_post(req, ev);
188 status = gensec_set_target_service(session->gensec, "cifs");
189 if (tevent_req_nterror(req, status)) {
190 return tevent_req_post(req, ev);
193 if (session->transport->negotiate.secblob.length > 0) {
194 chosen_oid = GENSEC_OID_SPNEGO;
196 chosen_oid = GENSEC_OID_NTLMSSP;
199 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
200 if (tevent_req_nterror(req, status)) {
201 return tevent_req_post(req, ev);
204 status = gensec_update(session->gensec, state,
205 session->transport->socket->event.ctx,
206 session->transport->negotiate.secblob,
207 &state->io.in.secblob);
208 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
209 tevent_req_nterror(req, status);
210 return tevent_req_post(req, ev);
212 state->gensec_status = status;
214 subreq = smb2_session_setup_send(session, &state->io);
215 if (tevent_req_nomem(subreq, req)) {
216 return tevent_req_post(req, ev);
218 subreq->async.fn = smb2_session_setup_spnego_handler;
219 subreq->async.private_data = req;
225 handle continuations of the spnego session setup
227 static void smb2_session_setup_spnego_handler(struct smb2_request *subreq)
229 struct tevent_req *req =
230 talloc_get_type_abort(subreq->async.private_data,
232 struct smb2_session_setup_spnego_state *state =
234 struct smb2_session_setup_spnego_state);
235 struct smb2_session *session = subreq->session;
236 NTSTATUS peer_status;
239 status = smb2_session_setup_recv(subreq, state, &state->io);
240 peer_status = status;
241 if (NT_STATUS_EQUAL(peer_status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
242 (NT_STATUS_IS_OK(peer_status) &&
243 NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
244 status = gensec_update(session->gensec, state,
245 session->transport->socket->event.ctx,
246 state->io.out.secblob,
247 &state->io.in.secblob);
248 state->gensec_status = status;
249 session->uid = state->io.out.uid;
252 if (!NT_STATUS_IS_OK(status) &&
253 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
254 tevent_req_nterror(req, status);
258 if (NT_STATUS_EQUAL(peer_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
259 subreq = smb2_session_setup_send(session, &state->io);
260 if (tevent_req_nomem(subreq, req)) {
264 subreq->async.fn = smb2_session_setup_spnego_handler;
265 subreq->async.private_data = req;
269 gensec_session_key(session->gensec, session, &session->session_key);
271 if (session->transport->signing_required) {
272 if (session->session_key.length == 0) {
273 DEBUG(0,("Wrong session key length %u for SMB2 signing\n",
274 (unsigned)session->session_key.length));
275 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
278 session->signing_active = true;
281 tevent_req_done(req);
285 receive a composite session setup reply
287 NTSTATUS smb2_session_setup_spnego_recv(struct tevent_req *req)
289 return tevent_req_simple_recv_ntstatus(req);
293 sync version of smb2_session_setup_spnego
295 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session,
296 struct cli_credentials *credentials)
298 struct tevent_req *subreq;
301 TALLOC_CTX *frame = talloc_stackframe();
302 struct tevent_context *ev = session->transport->socket->event.ctx;
305 return NT_STATUS_NO_MEMORY;
308 subreq = smb2_session_setup_spnego_send(frame, ev,
309 session, credentials);
310 if (subreq == NULL) {
312 return NT_STATUS_NO_MEMORY;
315 ok = tevent_req_poll(subreq, ev);
317 status = map_nt_error_from_unix_common(errno);
322 status = smb2_session_setup_spnego_recv(subreq);
324 if (!NT_STATUS_IS_OK(status)) {