source: branches/samba-3.5.x/source4/libcli/smb2/session.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 7.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 SMB2 client session handling
5
6 Copyright (C) Andrew Tridgell 2005
7
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.
12
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.
17
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/>.
20*/
21
22#include "includes.h"
23#include "libcli/raw/libcliraw.h"
24#include "libcli/smb2/smb2.h"
25#include "libcli/smb2/smb2_calls.h"
26#include "libcli/composite/composite.h"
27#include "auth/gensec/gensec.h"
28
29/**
30 initialise a smb2_session structure
31 */
32struct smb2_session *smb2_session_init(struct smb2_transport *transport,
33 struct gensec_settings *settings,
34 TALLOC_CTX *parent_ctx, bool primary)
35{
36 struct smb2_session *session;
37 NTSTATUS status;
38
39 session = talloc_zero(parent_ctx, struct smb2_session);
40 if (!session) {
41 return NULL;
42 }
43 if (primary) {
44 session->transport = talloc_steal(session, transport);
45 } else {
46 session->transport = talloc_reference(session, transport);
47 }
48
49 /* prepare a gensec context for later use */
50 status = gensec_client_start(session, &session->gensec,
51 session->transport->socket->event.ctx,
52 settings);
53 if (!NT_STATUS_IS_OK(status)) {
54 talloc_free(session);
55 return NULL;
56 }
57
58 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
59
60 return session;
61}
62
63/**
64 send a session setup request
65*/
66struct smb2_request *smb2_session_setup_send(struct smb2_session *session,
67 struct smb2_session_setup *io)
68{
69 struct smb2_request *req;
70 NTSTATUS status;
71
72 req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP,
73 0x18, true, io->in.secblob.length);
74 if (req == NULL) return NULL;
75
76 SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID, session->uid);
77 SCVAL(req->out.body, 0x02, io->in.vc_number);
78 SCVAL(req->out.body, 0x03, io->in.security_mode);
79 SIVAL(req->out.body, 0x04, io->in.capabilities);
80 SIVAL(req->out.body, 0x08, io->in.channel);
81 SBVAL(req->out.body, 0x10, io->in.previous_sessionid);
82
83 req->session = session;
84
85 status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
86 if (!NT_STATUS_IS_OK(status)) {
87 talloc_free(req);
88 return NULL;
89 }
90
91 smb2_transport_send(req);
92
93 return req;
94}
95
96
97/**
98 recv a session setup reply
99*/
100NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
101 struct smb2_session_setup *io)
102{
103 NTSTATUS status;
104
105 if (!smb2_request_receive(req) ||
106 (smb2_request_is_error(req) &&
107 !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
108 return smb2_request_destroy(req);
109 }
110
111 SMB2_CHECK_PACKET_RECV(req, 0x08, true);
112
113 io->out.session_flags = SVAL(req->in.body, 0x02);
114 io->out.uid = BVAL(req->in.hdr, SMB2_HDR_SESSION_ID);
115
116 status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
117 if (!NT_STATUS_IS_OK(status)) {
118 smb2_request_destroy(req);
119 return status;
120 }
121
122 return smb2_request_destroy(req);
123}
124
125/*
126 sync session setup request
127*/
128NTSTATUS smb2_session_setup(struct smb2_session *session,
129 TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
130{
131 struct smb2_request *req = smb2_session_setup_send(session, io);
132 return smb2_session_setup_recv(req, mem_ctx, io);
133}
134
135
136struct smb2_session_state {
137 struct smb2_session_setup io;
138 struct smb2_request *req;
139 NTSTATUS gensec_status;
140};
141
142/*
143 handle continuations of the spnego session setup
144*/
145static void session_request_handler(struct smb2_request *req)
146{
147 struct composite_context *c = talloc_get_type(req->async.private_data,
148 struct composite_context);
149 struct smb2_session_state *state = talloc_get_type(c->private_data,
150 struct smb2_session_state);
151 struct smb2_session *session = req->session;
152 NTSTATUS session_key_err;
153 DATA_BLOB session_key;
154 NTSTATUS peer_status;
155
156 c->status = smb2_session_setup_recv(req, c, &state->io);
157 peer_status = c->status;
158
159 if (NT_STATUS_EQUAL(peer_status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
160 (NT_STATUS_IS_OK(peer_status) &&
161 NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
162 c->status = gensec_update(session->gensec, c,
163 state->io.out.secblob,
164 &state->io.in.secblob);
165 state->gensec_status = c->status;
166
167 session->uid = state->io.out.uid;
168 }
169
170 if (!NT_STATUS_IS_OK(c->status) &&
171 !NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
172 composite_error(c, c->status);
173 return;
174 }
175
176 if (NT_STATUS_EQUAL(peer_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
177 state->req = smb2_session_setup_send(session, &state->io);
178 if (state->req == NULL) {
179 composite_error(c, NT_STATUS_NO_MEMORY);
180 return;
181 }
182
183 state->req->async.fn = session_request_handler;
184 state->req->async.private_data = c;
185 return;
186 }
187
188 session_key_err = gensec_session_key(session->gensec, &session_key);
189 if (NT_STATUS_IS_OK(session_key_err)) {
190 session->session_key = session_key;
191 }
192
193 if (session->transport->signing_required) {
194 if (session->session_key.length == 0) {
195 DEBUG(0,("Wrong session key length %u for SMB2 signing\n",
196 (unsigned)session->session_key.length));
197 composite_error(c, NT_STATUS_ACCESS_DENIED);
198 return;
199 }
200 session->signing_active = true;
201 }
202
203 composite_done(c);
204}
205
206/*
207 a composite function that does a full SPNEGO session setup
208 */
209struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session,
210 struct cli_credentials *credentials)
211{
212 struct composite_context *c;
213 struct smb2_session_state *state;
214 const char *chosen_oid;
215
216 c = composite_create(session, session->transport->socket->event.ctx);
217 if (c == NULL) return NULL;
218
219 state = talloc(c, struct smb2_session_state);
220 if (composite_nomem(state, c)) return c;
221 c->private_data = state;
222
223 ZERO_STRUCT(state->io);
224 state->io.in.vc_number = 0;
225 if (session->transport->signing_required) {
226 state->io.in.security_mode =
227 SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
228 }
229 state->io.in.capabilities = 0;
230 state->io.in.channel = 0;
231 state->io.in.previous_sessionid = 0;
232
233 c->status = gensec_set_credentials(session->gensec, credentials);
234 if (!composite_is_ok(c)) return c;
235
236 c->status = gensec_set_target_hostname(session->gensec,
237 session->transport->socket->hostname);
238 if (!composite_is_ok(c)) return c;
239
240 c->status = gensec_set_target_service(session->gensec, "cifs");
241 if (!composite_is_ok(c)) return c;
242
243 if (session->transport->negotiate.secblob.length > 0) {
244 chosen_oid = GENSEC_OID_SPNEGO;
245 } else {
246 chosen_oid = GENSEC_OID_NTLMSSP;
247 }
248
249 c->status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
250 if (!composite_is_ok(c)) return c;
251
252 c->status = gensec_update(session->gensec, c,
253 session->transport->negotiate.secblob,
254 &state->io.in.secblob);
255 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
256 composite_error(c, c->status);
257 return c;
258 }
259 state->gensec_status = c->status;
260
261 state->req = smb2_session_setup_send(session, &state->io);
262 composite_continue_smb2(c, state->req, session_request_handler, c);
263 return c;
264}
265
266/*
267 receive a composite session setup reply
268*/
269NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
270{
271 NTSTATUS status;
272 status = composite_wait(c);
273 talloc_free(c);
274 return status;
275}
276
277/*
278 sync version of smb2_session_setup_spnego
279*/
280NTSTATUS smb2_session_setup_spnego(struct smb2_session *session,
281 struct cli_credentials *credentials)
282{
283 struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
284 return smb2_session_setup_spnego_recv(c);
285}
Note: See TracBrowser for help on using the repository browser.