2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 static NTSTATUS
smbd_smb2_session_setup(struct smbd_smb2_request
*req
,
26 uint64_t in_session_id
,
27 uint8_t in_security_mode
,
28 DATA_BLOB in_security_buffer
,
29 uint16_t *out_session_flags
,
30 DATA_BLOB
*out_security_buffer
,
31 uint64_t *out_session_id
);
33 NTSTATUS
smbd_smb2_request_process_sesssetup(struct smbd_smb2_request
*req
)
36 const uint8_t *inbody
;
37 int i
= req
->current_idx
;
41 size_t expected_body_size
= 0x19;
43 uint64_t in_session_id
;
44 uint8_t in_security_mode
;
45 uint16_t in_security_offset
;
46 uint16_t in_security_length
;
47 DATA_BLOB in_security_buffer
;
48 uint16_t out_session_flags
;
49 uint64_t out_session_id
;
50 uint16_t out_security_offset
;
51 DATA_BLOB out_security_buffer
;
54 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
56 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
57 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
60 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
62 body_size
= SVAL(inbody
, 0x00);
63 if (body_size
!= expected_body_size
) {
64 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
67 in_security_offset
= SVAL(inbody
, 0x0C);
68 in_security_length
= SVAL(inbody
, 0x0E);
70 if (in_security_offset
!= (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
71 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
74 if (in_security_length
> req
->in
.vector
[i
+2].iov_len
) {
75 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
78 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
79 in_security_mode
= CVAL(inbody
, 0x03);
80 in_security_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
81 in_security_buffer
.length
= in_security_length
;
83 status
= smbd_smb2_session_setup(req
,
90 if (!NT_STATUS_IS_OK(status
) &&
91 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
92 status
= nt_status_squash(status
);
93 return smbd_smb2_request_error(req
, status
);
96 out_security_offset
= SMB2_HDR_BODY
+ 0x08;
98 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
100 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
101 if (outbody
.data
== NULL
) {
102 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
105 SBVAL(outhdr
, SMB2_HDR_SESSION_ID
, out_session_id
);
107 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
108 SSVAL(outbody
.data
, 0x02,
109 out_session_flags
); /* session flags */
110 SSVAL(outbody
.data
, 0x04,
111 out_security_offset
); /* security buffer offset */
112 SSVAL(outbody
.data
, 0x06,
113 out_security_buffer
.length
); /* security buffer length */
115 outdyn
= out_security_buffer
;
117 return smbd_smb2_request_done_ex(req
, status
, outbody
, &outdyn
,
121 static int smbd_smb2_session_destructor(struct smbd_smb2_session
*session
)
123 if (session
->sconn
== NULL
) {
127 /* first free all tcons */
128 while (session
->tcons
.list
) {
129 talloc_free(session
->tcons
.list
);
132 idr_remove(session
->sconn
->smb2
.sessions
.idtree
, session
->vuid
);
133 DLIST_REMOVE(session
->sconn
->smb2
.sessions
.list
, session
);
134 invalidate_vuid(session
->sconn
, session
->vuid
);
137 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
138 session
->sconn
= NULL
;
143 static NTSTATUS
smbd_smb2_session_setup(struct smbd_smb2_request
*req
,
144 uint64_t in_session_id
,
145 uint8_t in_security_mode
,
146 DATA_BLOB in_security_buffer
,
147 uint16_t *out_session_flags
,
148 DATA_BLOB
*out_security_buffer
,
149 uint64_t *out_session_id
)
151 struct smbd_smb2_session
*session
;
154 *out_session_flags
= 0;
157 if (in_session_id
== 0) {
160 /* create a new session */
161 session
= talloc_zero(req
->sconn
, struct smbd_smb2_session
);
162 if (session
== NULL
) {
163 return NT_STATUS_NO_MEMORY
;
165 session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
166 id
= idr_get_new_random(req
->sconn
->smb2
.sessions
.idtree
,
168 req
->sconn
->smb2
.sessions
.limit
);
170 return NT_STATUS_INSUFFICIENT_RESOURCES
;
174 session
->tcons
.idtree
= idr_init(session
);
175 if (session
->tcons
.idtree
== NULL
) {
176 return NT_STATUS_NO_MEMORY
;
178 session
->tcons
.limit
= 0x0000FFFE;
179 session
->tcons
.list
= NULL
;
181 DLIST_ADD_END(req
->sconn
->smb2
.sessions
.list
, session
,
182 struct smbd_smb2_session
*);
183 session
->sconn
= req
->sconn
;
184 talloc_set_destructor(session
, smbd_smb2_session_destructor
);
188 /* lookup an existing session */
189 p
= idr_find(req
->sconn
->smb2
.sessions
.idtree
, in_session_id
);
191 return NT_STATUS_USER_SESSION_DELETED
;
193 session
= talloc_get_type_abort(p
, struct smbd_smb2_session
);
196 if (NT_STATUS_IS_OK(session
->status
)) {
197 return NT_STATUS_REQUEST_NOT_ACCEPTED
;
200 if (session
->auth_ntlmssp_state
== NULL
) {
201 status
= auth_ntlmssp_start(&session
->auth_ntlmssp_state
);
202 if (!NT_STATUS_IS_OK(status
)) {
203 TALLOC_FREE(session
);
208 if (in_security_buffer
.data
[0] == ASN1_APPLICATION(0)) {
209 DATA_BLOB secblob_in
;
211 char *kerb_mech
= NULL
;
213 status
= parse_spnego_mechanisms(in_security_buffer
,
214 &secblob_in
, &kerb_mech
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 TALLOC_FREE(session
);
217 return nt_status_squash(status
);
220 /* For now, just SPNEGO NTLMSSP - krb5 goes here later.. */
221 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
225 if (!NT_STATUS_IS_OK(status
) &&
226 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
227 auth_ntlmssp_end(&session
->auth_ntlmssp_state
);
228 TALLOC_FREE(session
);
229 return nt_status_squash(status
);
232 *out_security_buffer
= spnego_gen_auth_response(&chal_out
,
233 status
, OID_NTLMSSP
);
235 *out_session_id
= session
->vuid
;
237 } else if (in_security_buffer
.data
[0] == ASN1_CONTEXT(1)) {
238 DATA_BLOB auth
= data_blob_null
;
239 DATA_BLOB auth_out
= data_blob_null
;
241 /* its an auth packet */
242 if (!spnego_parse_auth(in_security_buffer
, &auth
)) {
243 TALLOC_FREE(session
);
244 return NT_STATUS_LOGON_FAILURE
;
246 /* For now, just SPNEGO NTLMSSP - krb5 goes here later.. */
247 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
250 if (!NT_STATUS_IS_OK(status
)) {
251 auth_ntlmssp_end(&session
->auth_ntlmssp_state
);
252 TALLOC_FREE(session
);
253 return nt_status_squash(status
);
256 *out_security_buffer
= spnego_gen_auth_response(&auth_out
,
259 *out_session_id
= session
->vuid
;
260 } else if (strncmp((char *)(in_security_buffer
.data
), "NTLMSSP", 7) == 0) {
263 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
265 out_security_buffer
);
267 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
268 *out_session_id
= session
->vuid
;
271 if (!NT_STATUS_IS_OK(status
)) {
272 auth_ntlmssp_end(&session
->auth_ntlmssp_state
);
273 TALLOC_FREE(session
);
274 return nt_status_squash(status
);
276 *out_session_id
= session
->vuid
;
279 /* TODO: setup session key for signing */
281 if ((in_security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) ||
282 lp_server_signing() == Required
) {
283 session
->do_signing
= true;
286 if (session
->auth_ntlmssp_state
->server_info
->guest
) {
287 /* we map anonymous to guest internally */
288 *out_session_flags
|= SMB2_SESSION_FLAG_IS_GUEST
;
289 *out_session_flags
|= SMB2_SESSION_FLAG_IS_NULL
;
290 /* force no signing */
291 session
->do_signing
= false;
294 session
->server_info
= session
->auth_ntlmssp_state
->server_info
;
295 data_blob_free(&session
->server_info
->user_session_key
);
296 session
->server_info
->user_session_key
=
298 session
->server_info
,
299 session
->auth_ntlmssp_state
->ntlmssp_state
->session_key
.data
,
300 session
->auth_ntlmssp_state
->ntlmssp_state
->session_key
.length
);
301 if (session
->auth_ntlmssp_state
->ntlmssp_state
->session_key
.length
> 0) {
302 if (session
->server_info
->user_session_key
.data
== NULL
) {
303 TALLOC_FREE(session
);
304 return NT_STATUS_NO_MEMORY
;
307 session
->session_key
= session
->server_info
->user_session_key
;
309 session
->compat_vuser
= talloc_zero(session
, user_struct
);
310 if (session
->compat_vuser
== NULL
) {
311 TALLOC_FREE(session
);
312 return NT_STATUS_NO_MEMORY
;
314 session
->compat_vuser
->auth_ntlmssp_state
= session
->auth_ntlmssp_state
;
315 session
->compat_vuser
->homes_snum
= -1;
316 session
->compat_vuser
->server_info
= session
->server_info
;
317 session
->compat_vuser
->session_keystr
= NULL
;
318 session
->compat_vuser
->vuid
= session
->vuid
;
319 DLIST_ADD(session
->sconn
->smb1
.sessions
.validated_users
, session
->compat_vuser
);
321 session
->status
= NT_STATUS_OK
;
324 * we attach the session to the request
325 * so that the response can be signed
327 req
->session
= session
;
328 if (session
->do_signing
) {
329 req
->do_signing
= true;
332 *out_session_id
= session
->vuid
;
336 NTSTATUS
smbd_smb2_request_check_session(struct smbd_smb2_request
*req
)
338 const uint8_t *inhdr
;
339 int i
= req
->current_idx
;
340 uint64_t in_session_id
;
342 struct smbd_smb2_session
*session
;
344 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
346 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
348 /* lookup an existing session */
349 p
= idr_find(req
->sconn
->smb2
.sessions
.idtree
, in_session_id
);
351 return NT_STATUS_USER_SESSION_DELETED
;
353 session
= talloc_get_type_abort(p
, struct smbd_smb2_session
);
355 if (!NT_STATUS_IS_OK(session
->status
)) {
356 return NT_STATUS_ACCESS_DENIED
;
359 set_current_user_info(session
->server_info
->sanitized_username
,
360 session
->server_info
->unix_name
,
361 pdb_get_domain(session
->server_info
->sam_account
));
363 req
->session
= session
;
367 NTSTATUS
smbd_smb2_request_process_logoff(struct smbd_smb2_request
*req
)
369 const uint8_t *inbody
;
370 int i
= req
->current_idx
;
372 size_t expected_body_size
= 0x04;
375 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
376 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
379 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
381 body_size
= SVAL(inbody
, 0x00);
382 if (body_size
!= expected_body_size
) {
383 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
387 * TODO: cancel all outstanding requests on the session
388 * and delete all tree connections.
390 smbd_smb2_session_destructor(req
->session
);
392 * we may need to sign the response, so we need to keep
393 * the session until the response is sent to the wire.
395 talloc_steal(req
, req
->session
);
397 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
398 if (outbody
.data
== NULL
) {
399 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
402 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
403 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
405 return smbd_smb2_request_done(req
, outbody
, NULL
);