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"
24 #include "../libcli/auth/spnego.h"
27 static NTSTATUS
smbd_smb2_session_setup(struct smbd_smb2_request
*req
,
28 uint64_t in_session_id
,
29 uint8_t in_security_mode
,
30 DATA_BLOB in_security_buffer
,
31 uint16_t *out_session_flags
,
32 DATA_BLOB
*out_security_buffer
,
33 uint64_t *out_session_id
);
35 NTSTATUS
smbd_smb2_request_process_sesssetup(struct smbd_smb2_request
*req
)
38 const uint8_t *inbody
;
39 int i
= req
->current_idx
;
43 size_t expected_body_size
= 0x19;
45 uint64_t in_session_id
;
46 uint8_t in_security_mode
;
47 uint16_t in_security_offset
;
48 uint16_t in_security_length
;
49 DATA_BLOB in_security_buffer
;
50 uint16_t out_session_flags
;
51 uint64_t out_session_id
;
52 uint16_t out_security_offset
;
53 DATA_BLOB out_security_buffer
;
56 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
58 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
59 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
62 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
64 body_size
= SVAL(inbody
, 0x00);
65 if (body_size
!= expected_body_size
) {
66 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
69 in_security_offset
= SVAL(inbody
, 0x0C);
70 in_security_length
= SVAL(inbody
, 0x0E);
72 if (in_security_offset
!= (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
73 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
76 if (in_security_length
> req
->in
.vector
[i
+2].iov_len
) {
77 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
80 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
81 in_security_mode
= CVAL(inbody
, 0x03);
82 in_security_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
83 in_security_buffer
.length
= in_security_length
;
85 status
= smbd_smb2_session_setup(req
,
92 if (!NT_STATUS_IS_OK(status
) &&
93 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
94 status
= nt_status_squash(status
);
95 return smbd_smb2_request_error(req
, status
);
98 out_security_offset
= SMB2_HDR_BODY
+ 0x08;
100 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
102 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
103 if (outbody
.data
== NULL
) {
104 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
107 SBVAL(outhdr
, SMB2_HDR_SESSION_ID
, out_session_id
);
109 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
110 SSVAL(outbody
.data
, 0x02,
111 out_session_flags
); /* session flags */
112 SSVAL(outbody
.data
, 0x04,
113 out_security_offset
); /* security buffer offset */
114 SSVAL(outbody
.data
, 0x06,
115 out_security_buffer
.length
); /* security buffer length */
117 outdyn
= out_security_buffer
;
119 return smbd_smb2_request_done_ex(req
, status
, outbody
, &outdyn
,
123 static int smbd_smb2_session_destructor(struct smbd_smb2_session
*session
)
125 if (session
->sconn
== NULL
) {
129 /* first free all tcons */
130 while (session
->tcons
.list
) {
131 talloc_free(session
->tcons
.list
);
134 idr_remove(session
->sconn
->smb2
.sessions
.idtree
, session
->vuid
);
135 DLIST_REMOVE(session
->sconn
->smb2
.sessions
.list
, session
);
136 invalidate_vuid(session
->sconn
, session
->vuid
);
139 session
->status
= NT_STATUS_USER_SESSION_DELETED
;
140 session
->sconn
= NULL
;
145 static NTSTATUS
smbd_smb2_session_setup(struct smbd_smb2_request
*req
,
146 uint64_t in_session_id
,
147 uint8_t in_security_mode
,
148 DATA_BLOB in_security_buffer
,
149 uint16_t *out_session_flags
,
150 DATA_BLOB
*out_security_buffer
,
151 uint64_t *out_session_id
)
153 struct smbd_smb2_session
*session
;
156 *out_session_flags
= 0;
159 if (in_session_id
== 0) {
162 /* create a new session */
163 session
= talloc_zero(req
->sconn
, struct smbd_smb2_session
);
164 if (session
== NULL
) {
165 return NT_STATUS_NO_MEMORY
;
167 session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
168 id
= idr_get_new_random(req
->sconn
->smb2
.sessions
.idtree
,
170 req
->sconn
->smb2
.sessions
.limit
);
172 return NT_STATUS_INSUFFICIENT_RESOURCES
;
176 session
->tcons
.idtree
= idr_init(session
);
177 if (session
->tcons
.idtree
== NULL
) {
178 return NT_STATUS_NO_MEMORY
;
180 session
->tcons
.limit
= 0x0000FFFE;
181 session
->tcons
.list
= NULL
;
183 DLIST_ADD_END(req
->sconn
->smb2
.sessions
.list
, session
,
184 struct smbd_smb2_session
*);
185 session
->sconn
= req
->sconn
;
186 talloc_set_destructor(session
, smbd_smb2_session_destructor
);
190 /* lookup an existing session */
191 p
= idr_find(req
->sconn
->smb2
.sessions
.idtree
, in_session_id
);
193 return NT_STATUS_USER_SESSION_DELETED
;
195 session
= talloc_get_type_abort(p
, struct smbd_smb2_session
);
198 if (NT_STATUS_IS_OK(session
->status
)) {
199 return NT_STATUS_REQUEST_NOT_ACCEPTED
;
202 if (session
->auth_ntlmssp_state
== NULL
) {
203 status
= auth_ntlmssp_start(&session
->auth_ntlmssp_state
);
204 if (!NT_STATUS_IS_OK(status
)) {
205 TALLOC_FREE(session
);
210 if (in_security_buffer
.data
[0] == ASN1_APPLICATION(0)) {
211 DATA_BLOB secblob_in
;
213 char *kerb_mech
= NULL
;
215 status
= parse_spnego_mechanisms(in_security_buffer
,
216 &secblob_in
, &kerb_mech
);
217 if (!NT_STATUS_IS_OK(status
)) {
218 TALLOC_FREE(session
);
219 return nt_status_squash(status
);
222 /* For now, just SPNEGO NTLMSSP - krb5 goes here later.. */
223 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
227 if (!NT_STATUS_IS_OK(status
) &&
228 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
229 auth_ntlmssp_end(&session
->auth_ntlmssp_state
);
230 TALLOC_FREE(session
);
231 return nt_status_squash(status
);
234 *out_security_buffer
= spnego_gen_auth_response(&chal_out
,
235 status
, OID_NTLMSSP
);
237 *out_session_id
= session
->vuid
;
239 } else if (in_security_buffer
.data
[0] == ASN1_CONTEXT(1)) {
240 DATA_BLOB auth
= data_blob_null
;
241 DATA_BLOB auth_out
= data_blob_null
;
243 /* its an auth packet */
244 if (!spnego_parse_auth(in_security_buffer
, &auth
)) {
245 TALLOC_FREE(session
);
246 return NT_STATUS_LOGON_FAILURE
;
248 /* For now, just SPNEGO NTLMSSP - krb5 goes here later.. */
249 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
252 if (!NT_STATUS_IS_OK(status
)) {
253 auth_ntlmssp_end(&session
->auth_ntlmssp_state
);
254 TALLOC_FREE(session
);
255 return nt_status_squash(status
);
258 *out_security_buffer
= spnego_gen_auth_response(&auth_out
,
261 *out_session_id
= session
->vuid
;
262 } else if (strncmp((char *)(in_security_buffer
.data
), "NTLMSSP", 7) == 0) {
265 status
= auth_ntlmssp_update(session
->auth_ntlmssp_state
,
267 out_security_buffer
);
269 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
270 *out_session_id
= session
->vuid
;
273 if (!NT_STATUS_IS_OK(status
)) {
274 auth_ntlmssp_end(&session
->auth_ntlmssp_state
);
275 TALLOC_FREE(session
);
276 return nt_status_squash(status
);
278 *out_session_id
= session
->vuid
;
281 /* TODO: setup session key for signing */
283 if ((in_security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) ||
284 lp_server_signing() == Required
) {
285 session
->do_signing
= true;
288 if (session
->auth_ntlmssp_state
->server_info
->guest
) {
289 /* we map anonymous to guest internally */
290 *out_session_flags
|= SMB2_SESSION_FLAG_IS_GUEST
;
291 *out_session_flags
|= SMB2_SESSION_FLAG_IS_NULL
;
292 /* force no signing */
293 session
->do_signing
= false;
296 session
->server_info
= session
->auth_ntlmssp_state
->server_info
;
297 data_blob_free(&session
->server_info
->user_session_key
);
298 session
->server_info
->user_session_key
=
300 session
->server_info
,
301 session
->auth_ntlmssp_state
->ntlmssp_state
->session_key
.data
,
302 session
->auth_ntlmssp_state
->ntlmssp_state
->session_key
.length
);
303 if (session
->auth_ntlmssp_state
->ntlmssp_state
->session_key
.length
> 0) {
304 if (session
->server_info
->user_session_key
.data
== NULL
) {
305 TALLOC_FREE(session
);
306 return NT_STATUS_NO_MEMORY
;
309 session
->session_key
= session
->server_info
->user_session_key
;
311 session
->compat_vuser
= talloc_zero(session
, user_struct
);
312 if (session
->compat_vuser
== NULL
) {
313 TALLOC_FREE(session
);
314 return NT_STATUS_NO_MEMORY
;
316 session
->compat_vuser
->auth_ntlmssp_state
= session
->auth_ntlmssp_state
;
317 session
->compat_vuser
->homes_snum
= -1;
318 session
->compat_vuser
->server_info
= session
->server_info
;
319 session
->compat_vuser
->session_keystr
= NULL
;
320 session
->compat_vuser
->vuid
= session
->vuid
;
321 DLIST_ADD(session
->sconn
->smb1
.sessions
.validated_users
, session
->compat_vuser
);
323 session
->status
= NT_STATUS_OK
;
326 * we attach the session to the request
327 * so that the response can be signed
329 req
->session
= session
;
330 if (session
->do_signing
) {
331 req
->do_signing
= true;
334 *out_session_id
= session
->vuid
;
338 NTSTATUS
smbd_smb2_request_check_session(struct smbd_smb2_request
*req
)
340 const uint8_t *inhdr
;
341 int i
= req
->current_idx
;
342 uint64_t in_session_id
;
344 struct smbd_smb2_session
*session
;
346 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
348 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
350 /* lookup an existing session */
351 p
= idr_find(req
->sconn
->smb2
.sessions
.idtree
, in_session_id
);
353 return NT_STATUS_USER_SESSION_DELETED
;
355 session
= talloc_get_type_abort(p
, struct smbd_smb2_session
);
357 if (!NT_STATUS_IS_OK(session
->status
)) {
358 return NT_STATUS_ACCESS_DENIED
;
361 set_current_user_info(session
->server_info
->sanitized_username
,
362 session
->server_info
->unix_name
,
363 pdb_get_domain(session
->server_info
->sam_account
));
365 req
->session
= session
;
369 NTSTATUS
smbd_smb2_request_process_logoff(struct smbd_smb2_request
*req
)
371 const uint8_t *inbody
;
372 int i
= req
->current_idx
;
374 size_t expected_body_size
= 0x04;
377 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
378 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
381 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
383 body_size
= SVAL(inbody
, 0x00);
384 if (body_size
!= expected_body_size
) {
385 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
389 * TODO: cancel all outstanding requests on the session
390 * and delete all tree connections.
392 smbd_smb2_session_destructor(req
->session
);
394 * we may need to sign the response, so we need to keep
395 * the session until the response is sent to the wire.
397 talloc_steal(req
, req
->session
);
399 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
400 if (outbody
.data
== NULL
) {
401 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
404 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
405 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
407 return smbd_smb2_request_done(req
, outbody
, NULL
);