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 "../libcli/smb/smbXcli_base.h"
31 #include "../source3/libsmb/smb2cli.h"
34 initialise a smb2_session structure
36 struct smb2_session
*smb2_session_init(struct smb2_transport
*transport
,
37 struct gensec_settings
*settings
,
38 TALLOC_CTX
*parent_ctx
, bool primary
)
40 struct smb2_session
*session
;
43 session
= talloc_zero(parent_ctx
, struct smb2_session
);
48 session
->transport
= talloc_steal(session
, transport
);
50 session
->transport
= talloc_reference(session
, transport
);
53 session
->pid
= getpid();
55 session
->smbXcli
= smbXcli_session_create(session
, transport
->conn
);
56 if (session
->smbXcli
== NULL
) {
61 /* prepare a gensec context for later use */
62 status
= gensec_client_start(session
, &session
->gensec
,
64 if (!NT_STATUS_IS_OK(status
)) {
69 gensec_want_feature(session
->gensec
, GENSEC_FEATURE_SESSION_KEY
);
74 struct smb2_session_setup_spnego_state
{
75 struct tevent_context
*ev
;
76 struct smb2_session
*session
;
77 struct cli_credentials
*credentials
;
78 NTSTATUS gensec_status
;
80 DATA_BLOB out_secblob
;
83 static void smb2_session_setup_spnego_done(struct tevent_req
*subreq
);
86 a composite function that does a full SPNEGO session setup
88 struct tevent_req
*smb2_session_setup_spnego_send(TALLOC_CTX
*mem_ctx
,
89 struct tevent_context
*ev
,
90 struct smb2_session
*session
,
91 struct cli_credentials
*credentials
)
93 struct tevent_req
*req
;
94 struct smb2_session_setup_spnego_state
*state
;
95 const char *chosen_oid
;
96 struct tevent_req
*subreq
;
98 const DATA_BLOB
*server_gss_blob
;
99 DATA_BLOB negprot_secblob
= data_blob_null
;
100 uint32_t timeout_msec
;
102 timeout_msec
= session
->transport
->options
.request_timeout
* 1000;
104 req
= tevent_req_create(mem_ctx
, &state
,
105 struct smb2_session_setup_spnego_state
);
110 state
->session
= session
;
111 state
->credentials
= credentials
;
113 server_gss_blob
= smbXcli_conn_server_gss_blob(session
->transport
->conn
);
114 if (server_gss_blob
) {
115 negprot_secblob
= *server_gss_blob
;
118 status
= gensec_set_credentials(session
->gensec
, credentials
);
119 if (tevent_req_nterror(req
, status
)) {
120 return tevent_req_post(req
, ev
);
123 status
= gensec_set_target_hostname(session
->gensec
,
124 smbXcli_conn_remote_name(session
->transport
->conn
));
125 if (tevent_req_nterror(req
, status
)) {
126 return tevent_req_post(req
, ev
);
129 status
= gensec_set_target_service(session
->gensec
, "cifs");
130 if (tevent_req_nterror(req
, status
)) {
131 return tevent_req_post(req
, ev
);
134 if (negprot_secblob
.length
> 0) {
135 chosen_oid
= GENSEC_OID_SPNEGO
;
137 chosen_oid
= GENSEC_OID_NTLMSSP
;
140 status
= gensec_start_mech_by_oid(session
->gensec
, chosen_oid
);
141 if (tevent_req_nterror(req
, status
)) {
142 return tevent_req_post(req
, ev
);
145 status
= gensec_update(session
->gensec
, state
,
149 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
150 tevent_req_nterror(req
, status
);
151 return tevent_req_post(req
, ev
);
153 state
->gensec_status
= status
;
155 subreq
= smb2cli_session_setup_send(state
, state
->ev
,
156 session
->transport
->conn
,
160 0, /* in_capabilities */
162 NULL
, /* in_previous_session */
164 if (tevent_req_nomem(subreq
, req
)) {
165 return tevent_req_post(req
, ev
);
167 tevent_req_set_callback(subreq
, smb2_session_setup_spnego_done
, req
);
173 handle continuations of the spnego session setup
175 static void smb2_session_setup_spnego_done(struct tevent_req
*subreq
)
177 struct tevent_req
*req
=
178 tevent_req_callback_data(subreq
,
180 struct smb2_session_setup_spnego_state
*state
=
182 struct smb2_session_setup_spnego_state
);
183 struct smb2_session
*session
= state
->session
;
184 NTSTATUS peer_status
;
186 struct iovec
*recv_iov
;
187 uint32_t timeout_msec
;
189 timeout_msec
= session
->transport
->options
.request_timeout
* 1000;
191 status
= smb2cli_session_setup_recv(subreq
, state
,
193 &state
->out_secblob
);
194 if (!NT_STATUS_IS_OK(status
) &&
195 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
196 tevent_req_nterror(req
, status
);
199 peer_status
= status
;
201 if (NT_STATUS_EQUAL(state
->gensec_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
202 status
= gensec_update(session
->gensec
, state
,
206 state
->gensec_status
= status
;
209 if (!NT_STATUS_IS_OK(status
) &&
210 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
211 tevent_req_nterror(req
, status
);
215 if (NT_STATUS_IS_OK(peer_status
) && NT_STATUS_IS_OK(state
->gensec_status
)) {
216 status
= gensec_session_key(session
->gensec
, session
,
217 &session
->session_key
);
218 if (tevent_req_nterror(req
, status
)) {
222 status
= smb2cli_session_update_session_key(session
->smbXcli
,
223 session
->session_key
,
225 if (tevent_req_nterror(req
, status
)) {
229 tevent_req_done(req
);
233 subreq
= smb2cli_session_setup_send(state
, state
->ev
,
234 session
->transport
->conn
,
238 0, /* in_capabilities */
240 NULL
, /* in_previous_session */
242 if (tevent_req_nomem(subreq
, req
)) {
245 tevent_req_set_callback(subreq
, smb2_session_setup_spnego_done
, req
);
249 receive a composite session setup reply
251 NTSTATUS
smb2_session_setup_spnego_recv(struct tevent_req
*req
)
253 return tevent_req_simple_recv_ntstatus(req
);
257 sync version of smb2_session_setup_spnego
259 NTSTATUS
smb2_session_setup_spnego(struct smb2_session
*session
,
260 struct cli_credentials
*credentials
)
262 struct tevent_req
*subreq
;
265 TALLOC_CTX
*frame
= talloc_stackframe();
266 struct tevent_context
*ev
= session
->transport
->ev
;
269 return NT_STATUS_NO_MEMORY
;
272 subreq
= smb2_session_setup_spnego_send(frame
, ev
,
273 session
, credentials
);
274 if (subreq
== NULL
) {
276 return NT_STATUS_NO_MEMORY
;
279 ok
= tevent_req_poll(subreq
, ev
);
281 status
= map_nt_error_from_unix_common(errno
);
286 status
= smb2_session_setup_spnego_recv(subreq
);
288 if (!NT_STATUS_IS_OK(status
)) {