source: branches/samba-3.5.x/source4/smb_server/smb2/sesssetup.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 SMB2 implementation.
3
4 Copyright (C) Andrew Bartlett 2001-2005
5 Copyright (C) Stefan Metzmacher 2005
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "includes.h"
22#include "auth/gensec/gensec.h"
23#include "auth/auth.h"
24#include "libcli/smb2/smb2.h"
25#include "libcli/smb2/smb2_calls.h"
26#include "smb_server/smb_server.h"
27#include "smb_server/smb2/smb2_server.h"
28#include "smbd/service_stream.h"
29
30static void smb2srv_sesssetup_send(struct smb2srv_request *req, union smb_sesssetup *io)
31{
32 uint16_t credit;
33
34 if (NT_STATUS_IS_OK(req->status)) {
35 credit = 0x0003;
36 } else if (NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
37 credit = 0x0002;
38 } else {
39 smb2srv_send_error(req, req->status);
40 return;
41 }
42
43 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, io->smb2.out.secblob.length));
44
45 SSVAL(req->out.hdr, SMB2_HDR_CREDIT, credit);
46 SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID, io->smb2.out.uid);
47
48 SSVAL(req->out.body, 0x02, io->smb2.out.session_flags);
49 SMB2SRV_CHECK(smb2_push_o16s16_blob(&req->out, 0x04, io->smb2.out.secblob));
50
51 smb2srv_send_reply(req);
52}
53
54struct smb2srv_sesssetup_callback_ctx {
55 struct smb2srv_request *req;
56 union smb_sesssetup *io;
57 struct smbsrv_session *smb_sess;
58};
59
60static void smb2srv_sesssetup_callback(struct gensec_update_request *greq, void *private_data)
61{
62 struct smb2srv_sesssetup_callback_ctx *ctx = talloc_get_type(private_data,
63 struct smb2srv_sesssetup_callback_ctx);
64 struct smb2srv_request *req = ctx->req;
65 union smb_sesssetup *io = ctx->io;
66 struct smbsrv_session *smb_sess = ctx->smb_sess;
67 struct auth_session_info *session_info = NULL;
68 NTSTATUS status;
69
70 status = gensec_update_recv(greq, req, &io->smb2.out.secblob);
71 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
72 goto done;
73 } else if (!NT_STATUS_IS_OK(status)) {
74 goto failed;
75 }
76
77 status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
78 if (!NT_STATUS_IS_OK(status)) {
79 goto failed;
80 }
81
82 /* Ensure this is marked as a 'real' vuid, not one
83 * simply valid for the session setup leg */
84 status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
85 if (!NT_STATUS_IS_OK(status)) {
86 goto failed;
87 }
88 req->session = smb_sess;
89
90 if (smb_sess->smb2_signing.required) {
91 /* activate smb2 signing on the session */
92 smb_sess->smb2_signing.active = true;
93 }
94done:
95 io->smb2.out.uid = smb_sess->vuid;
96failed:
97 req->status = auth_nt_status_squash(status);
98 smb2srv_sesssetup_send(req, io);
99 if (!NT_STATUS_IS_OK(status) && !
100 NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
101 talloc_free(smb_sess);
102 }
103}
104
105static void smb2srv_sesssetup_backend(struct smb2srv_request *req, union smb_sesssetup *io)
106{
107 NTSTATUS status;
108 struct smb2srv_sesssetup_callback_ctx *callback_ctx;
109 struct smbsrv_session *smb_sess = NULL;
110 uint64_t vuid;
111
112 io->smb2.out.session_flags = 0;
113 io->smb2.out.uid = 0;
114 io->smb2.out.secblob = data_blob(NULL, 0);
115
116 vuid = BVAL(req->in.hdr, SMB2_HDR_SESSION_ID);
117
118 /*
119 * only when we got '0' we should allocate a new session
120 */
121 if (vuid == 0) {
122 struct gensec_security *gensec_ctx;
123
124 status = samba_server_gensec_start(req,
125 req->smb_conn->connection->event.ctx,
126 req->smb_conn->connection->msg_ctx,
127 req->smb_conn->lp_ctx,
128 req->smb_conn->negotiate.server_credentials,
129 "cifs",
130 &gensec_ctx);
131 if (!NT_STATUS_IS_OK(status)) {
132 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
133 goto failed;
134 }
135
136 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
137
138 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
139 if (!NT_STATUS_IS_OK(status)) {
140 DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
141 goto failed;
142 }
143
144 /* allocate a new session */
145 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
146 if (!smb_sess) {
147 status = NT_STATUS_INSUFFICIENT_RESOURCES;
148 goto failed;
149 }
150 status = smbsrv_smb2_init_tcons(smb_sess);
151 if (!NT_STATUS_IS_OK(status)) {
152 goto failed;
153 }
154 } else {
155 /* lookup an existing session */
156 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
157 }
158
159 if (!smb_sess) {
160 /* see WSPP test suite - test 11 */
161 status = NT_STATUS_REQUEST_NOT_ACCEPTED;
162 goto failed;
163 }
164
165 if (!smb_sess->gensec_ctx) {
166 status = NT_STATUS_INTERNAL_ERROR;
167 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
168 goto failed;
169 }
170
171 callback_ctx = talloc(req, struct smb2srv_sesssetup_callback_ctx);
172 if (!callback_ctx) goto nomem;
173 callback_ctx->req = req;
174 callback_ctx->io = io;
175 callback_ctx->smb_sess = smb_sess;
176
177 gensec_update_send(smb_sess->gensec_ctx, io->smb2.in.secblob,
178 smb2srv_sesssetup_callback, callback_ctx);
179
180 /* note that we ignore SMB2_NEGOTIATE_SIGNING_ENABLED from the client.
181 This is deliberate as windows does not set it even when it does
182 set SMB2_NEGOTIATE_SIGNING_REQUIRED */
183 if (io->smb2.in.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
184 smb_sess->smb2_signing.required = true;
185 } else if (req->smb_conn->smb2_signing_required) {
186 /*
187 * if required signing was negotiates in SMB2 Negotiate
188 * then the client made an error not using it here
189 */
190 DEBUG(1, ("SMB2 signing required on the connection but not used on session\n"));
191 req->status = NT_STATUS_FOOBAR;
192 goto failed;
193 }
194
195 return;
196nomem:
197 status = NT_STATUS_NO_MEMORY;
198failed:
199 talloc_free(smb_sess);
200 req->status = auth_nt_status_squash(status);
201 smb2srv_sesssetup_send(req, io);
202}
203
204void smb2srv_sesssetup_recv(struct smb2srv_request *req)
205{
206 union smb_sesssetup *io;
207
208 SMB2SRV_CHECK_BODY_SIZE(req, 0x18, true);
209 SMB2SRV_TALLOC_IO_PTR(io, union smb_sesssetup);
210
211 io->smb2.level = RAW_SESSSETUP_SMB2;
212 io->smb2.in.vc_number = CVAL(req->in.body, 0x02);
213 io->smb2.in.security_mode = CVAL(req->in.body, 0x03);
214 io->smb2.in.capabilities = IVAL(req->in.body, 0x04);
215 io->smb2.in.channel = IVAL(req->in.body, 0x08);
216 io->smb2.in.previous_sessionid = BVAL(req->in.body, 0x10);
217 SMB2SRV_CHECK(smb2_pull_o16s16_blob(&req->in, io, req->in.body+0x0C, &io->smb2.in.secblob));
218
219 smb2srv_sesssetup_backend(req, io);
220}
221
222static int smb2srv_cleanup_session_destructor(struct smbsrv_session **session)
223{
224 /* TODO: call ntvfs backends to close file of this session */
225 DEBUG(0,("free session[%p]\n", *session));
226 talloc_free(*session);
227 return 0;
228}
229
230static NTSTATUS smb2srv_logoff_backend(struct smb2srv_request *req)
231{
232 struct smbsrv_session **session_ptr;
233
234 /* we need to destroy the session after sending the reply */
235 session_ptr = talloc(req, struct smbsrv_session *);
236 NT_STATUS_HAVE_NO_MEMORY(session_ptr);
237
238 *session_ptr = req->session;
239 talloc_set_destructor(session_ptr, smb2srv_cleanup_session_destructor);
240
241 return NT_STATUS_OK;
242}
243
244static void smb2srv_logoff_send(struct smb2srv_request *req)
245{
246 if (NT_STATUS_IS_ERR(req->status)) {
247 smb2srv_send_error(req, req->status);
248 return;
249 }
250
251 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
252
253 SSVAL(req->out.body, 0x02, 0);
254
255 smb2srv_send_reply(req);
256}
257
258void smb2srv_logoff_recv(struct smb2srv_request *req)
259{
260 uint16_t _pad;
261
262 SMB2SRV_CHECK_BODY_SIZE(req, 0x04, false);
263
264 _pad = SVAL(req->in.body, 0x02);
265
266 req->status = smb2srv_logoff_backend(req);
267
268 if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
269 talloc_free(req);
270 return;
271 }
272 smb2srv_logoff_send(req);
273}
Note: See TracBrowser for help on using the repository browser.