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/>.
23 #include "system/network.h"
25 #include "lib/util/tevent_ntstatus.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/smb2/smb2.h"
28 #include "libcli/smb2/smb2_calls.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/credentials/credentials.h"
31 #include "../libcli/smb/smbXcli_base.h"
32 #include "../source3/libsmb/smb2cli.h"
35 initialise a smb2_session structure
37 struct smb2_session
*smb2_session_init(struct smb2_transport
*transport
,
38 struct gensec_settings
*settings
,
39 TALLOC_CTX
*parent_ctx
, bool primary
)
41 struct smb2_session
*session
;
44 session
= talloc_zero(parent_ctx
, struct smb2_session
);
49 session
->transport
= talloc_steal(session
, transport
);
51 session
->transport
= talloc_reference(session
, transport
);
54 session
->smbXcli
= smbXcli_session_create(session
, transport
->conn
);
55 if (session
->smbXcli
== NULL
) {
60 /* prepare a gensec context for later use */
61 status
= gensec_client_start(session
, &session
->gensec
,
63 if (!NT_STATUS_IS_OK(status
)) {
68 gensec_want_feature(session
->gensec
, GENSEC_FEATURE_SESSION_KEY
);
73 struct smb2_session_setup_spnego_state
{
74 struct tevent_context
*ev
;
75 struct smb2_session
*session
;
76 struct cli_credentials
*credentials
;
77 uint64_t previous_session_id
;
79 NTSTATUS gensec_status
;
81 DATA_BLOB out_secblob
;
84 static void smb2_session_setup_spnego_done(struct tevent_req
*subreq
);
87 a composite function that does a full SPNEGO session setup
89 struct tevent_req
*smb2_session_setup_spnego_send(
91 struct tevent_context
*ev
,
92 struct smb2_session
*session
,
93 struct cli_credentials
*credentials
,
94 uint64_t previous_session_id
)
96 struct tevent_req
*req
;
97 struct smb2_session_setup_spnego_state
*state
;
98 uint64_t current_session_id
;
99 const char *chosen_oid
;
100 struct tevent_req
*subreq
;
102 const DATA_BLOB
*server_gss_blob
;
103 DATA_BLOB negprot_secblob
= data_blob_null
;
104 uint32_t timeout_msec
;
106 timeout_msec
= session
->transport
->options
.request_timeout
* 1000;
108 req
= tevent_req_create(mem_ctx
, &state
,
109 struct smb2_session_setup_spnego_state
);
114 state
->session
= session
;
115 state
->credentials
= credentials
;
116 state
->previous_session_id
= previous_session_id
;
118 current_session_id
= smb2cli_session_current_id(state
->session
->smbXcli
);
119 if (current_session_id
!= 0) {
120 state
->reauth
= true;
123 server_gss_blob
= smbXcli_conn_server_gss_blob(session
->transport
->conn
);
124 if (server_gss_blob
) {
125 negprot_secblob
= *server_gss_blob
;
128 status
= gensec_set_credentials(session
->gensec
, credentials
);
129 if (tevent_req_nterror(req
, status
)) {
130 return tevent_req_post(req
, ev
);
133 status
= gensec_set_target_hostname(session
->gensec
,
134 smbXcli_conn_remote_name(session
->transport
->conn
));
135 if (tevent_req_nterror(req
, status
)) {
136 return tevent_req_post(req
, ev
);
139 status
= gensec_set_target_service(session
->gensec
, "cifs");
140 if (tevent_req_nterror(req
, status
)) {
141 return tevent_req_post(req
, ev
);
144 if (negprot_secblob
.length
> 0) {
145 chosen_oid
= GENSEC_OID_SPNEGO
;
147 chosen_oid
= GENSEC_OID_NTLMSSP
;
150 status
= gensec_start_mech_by_oid(session
->gensec
, chosen_oid
);
151 if (tevent_req_nterror(req
, status
)) {
152 return tevent_req_post(req
, ev
);
155 status
= gensec_update(session
->gensec
, state
,
159 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
160 tevent_req_nterror(req
, status
);
161 return tevent_req_post(req
, ev
);
163 state
->gensec_status
= status
;
165 subreq
= smb2cli_session_setup_send(state
, state
->ev
,
166 session
->transport
->conn
,
170 0, /* in_capabilities */
172 state
->previous_session_id
,
174 if (tevent_req_nomem(subreq
, req
)) {
175 return tevent_req_post(req
, ev
);
177 tevent_req_set_callback(subreq
, smb2_session_setup_spnego_done
, req
);
183 handle continuations of the spnego session setup
185 static void smb2_session_setup_spnego_done(struct tevent_req
*subreq
)
187 struct tevent_req
*req
=
188 tevent_req_callback_data(subreq
,
190 struct smb2_session_setup_spnego_state
*state
=
192 struct smb2_session_setup_spnego_state
);
193 struct smb2_session
*session
= state
->session
;
194 NTSTATUS peer_status
;
196 struct iovec
*recv_iov
;
197 uint32_t timeout_msec
;
199 timeout_msec
= session
->transport
->options
.request_timeout
* 1000;
201 status
= smb2cli_session_setup_recv(subreq
, state
,
203 &state
->out_secblob
);
204 if (!NT_STATUS_IS_OK(status
) &&
205 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
206 tevent_req_nterror(req
, status
);
209 peer_status
= status
;
211 if (NT_STATUS_EQUAL(state
->gensec_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
212 status
= gensec_update(session
->gensec
, state
,
216 state
->gensec_status
= status
;
219 if (!NT_STATUS_IS_OK(status
) &&
220 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
221 tevent_req_nterror(req
, status
);
225 if (NT_STATUS_IS_OK(peer_status
) && NT_STATUS_IS_OK(state
->gensec_status
)) {
226 DATA_BLOB session_key
;
229 tevent_req_done(req
);
233 if (cli_credentials_is_anonymous(state
->credentials
)) {
235 * Windows server does not set the
236 * SMB2_SESSION_FLAG_IS_GUEST nor
237 * SMB2_SESSION_FLAG_IS_NULL flag.
239 * This fix makes sure we do not try
240 * to verify a signature on the final
241 * session setup response.
243 tevent_req_done(req
);
247 status
= gensec_session_key(session
->gensec
, state
,
249 if (tevent_req_nterror(req
, status
)) {
253 status
= smb2cli_session_set_session_key(session
->smbXcli
,
256 if (tevent_req_nterror(req
, status
)) {
260 tevent_req_done(req
);
264 subreq
= smb2cli_session_setup_send(state
, state
->ev
,
265 session
->transport
->conn
,
269 0, /* in_capabilities */
271 state
->previous_session_id
,
273 if (tevent_req_nomem(subreq
, req
)) {
276 tevent_req_set_callback(subreq
, smb2_session_setup_spnego_done
, req
);
280 receive a composite session setup reply
282 NTSTATUS
smb2_session_setup_spnego_recv(struct tevent_req
*req
)
284 return tevent_req_simple_recv_ntstatus(req
);
288 sync version of smb2_session_setup_spnego
290 NTSTATUS
smb2_session_setup_spnego(struct smb2_session
*session
,
291 struct cli_credentials
*credentials
,
292 uint64_t previous_session_id
)
294 struct tevent_req
*subreq
;
297 TALLOC_CTX
*frame
= talloc_stackframe();
298 struct tevent_context
*ev
= session
->transport
->ev
;
301 return NT_STATUS_NO_MEMORY
;
304 subreq
= smb2_session_setup_spnego_send(frame
, ev
,
305 session
, credentials
,
306 previous_session_id
);
307 if (subreq
== NULL
) {
309 return NT_STATUS_NO_MEMORY
;
312 ok
= tevent_req_poll(subreq
, ev
);
314 status
= map_nt_error_from_unix_common(errno
);
319 status
= smb2_session_setup_spnego_recv(subreq
);
321 if (!NT_STATUS_IS_OK(status
)) {