2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
27 #include "../libcli/auth/spnego.h"
28 #include "../libcli/auth/ntlmssp.h"
30 struct smb2cli_sesssetup_blob_state {
31 struct ntlmssp_state *ntlmssp;
38 static void smb2cli_sesssetup_blob_done(struct tevent_req *subreq);
40 static struct tevent_req *smb2cli_sesssetup_blob_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 struct cli_state *cli,
45 struct tevent_req *req, *subreq;
46 struct smb2cli_sesssetup_blob_state *state;
51 req = tevent_req_create(mem_ctx, &state,
52 struct smb2cli_sesssetup_blob_state);
60 SCVAL(buf, 2, 0); /* VcNumber */
61 SCVAL(buf, 3, 0); /* SecurityMode */
62 SIVAL(buf, 4, 0); /* Capabilities */
63 SIVAL(buf, 8, 0); /* Channel */
64 SSVAL(buf, 12, SMB2_HDR_BODY + 24); /* SecurityBufferOffset */
65 SSVAL(buf, 14, blob->length);
66 SBVAL(buf, 16, 0); /* PreviousSessionId */
68 if (blob->length > 0) {
70 dyn_len = blob->length;
72 dyn = state->dyn_pad;;
73 dyn_len = sizeof(state->dyn_pad);
76 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_SESSSETUP,
81 state->fixed, sizeof(state->fixed),
83 if (tevent_req_nomem(subreq, req)) {
84 return tevent_req_post(req, ev);
86 tevent_req_set_callback(subreq, smb2cli_sesssetup_blob_done, req);
90 static void smb2cli_sesssetup_blob_done(struct tevent_req *subreq)
92 struct tevent_req *req =
93 tevent_req_callback_data(subreq,
95 struct smb2cli_sesssetup_blob_state *state =
97 struct smb2cli_sesssetup_blob_state);
100 uint16_t offset, length;
102 status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 9);
103 if (!NT_STATUS_IS_OK(status) &&
104 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
106 tevent_req_nterror(req, status);
110 offset = SVAL(iov[1].iov_base, 4);
111 length = SVAL(iov[1].iov_base, 6);
113 if ((offset != SMB2_HDR_BODY + 8) || (length > iov[2].iov_len)) {
115 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
118 state->uid = BVAL(iov[0].iov_base, SMB2_HDR_SESSION_ID);
119 state->out.data = (uint8_t *)iov[2].iov_base;
120 state->out.length = length;
121 if (!NT_STATUS_IS_OK(status)) {
122 tevent_req_nterror(req, status);
125 tevent_req_done(req);
128 static NTSTATUS smb2cli_sesssetup_blob_recv(struct tevent_req *req,
129 uint64_t *uid, DATA_BLOB *out)
131 struct smb2cli_sesssetup_blob_state *state =
133 struct smb2cli_sesssetup_blob_state);
134 NTSTATUS status = NT_STATUS_OK;
136 if (tevent_req_is_nterror(req, &status)
137 && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
145 struct smb2cli_sesssetup_state {
146 struct tevent_context *ev;
147 struct cli_state *cli;
148 struct ntlmssp_state *ntlmssp;
155 static void smb2cli_sesssetup_done(struct tevent_req *subreq);
157 struct tevent_req *smb2cli_sesssetup_send(TALLOC_CTX *mem_ctx,
158 struct tevent_context *ev,
159 struct cli_state *cli,
164 struct tevent_req *req, *subreq;
165 struct smb2cli_sesssetup_state *state;
168 const char *OIDs_ntlm[] = {OID_NTLMSSP, NULL};
170 req = tevent_req_create(mem_ctx, &state,
171 struct smb2cli_sesssetup_state);
178 status = ntlmssp_client_start(state,
181 lp_client_ntlmv2_auth(),
183 if (!NT_STATUS_IS_OK(status)) {
186 ntlmssp_want_feature(state->ntlmssp,
187 NTLMSSP_FEATURE_SESSION_KEY);
188 status = ntlmssp_set_username(state->ntlmssp, user);
189 if (!NT_STATUS_IS_OK(status)) {
192 status = ntlmssp_set_domain(state->ntlmssp, domain);
193 if (!NT_STATUS_IS_OK(status)) {
196 status = ntlmssp_set_password(state->ntlmssp, pass);
197 if (!NT_STATUS_IS_OK(status)) {
201 status = ntlmssp_update(state->ntlmssp, data_blob_null, &blob_out);
202 if (!NT_STATUS_IS_OK(status)
203 && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
207 blob_out = spnego_gen_negTokenInit(state, OIDs_ntlm, &blob_out, NULL);
210 subreq = smb2cli_sesssetup_blob_send(
211 state, state->ev, state->cli, &blob_out);
212 if (tevent_req_nomem(subreq, req)) {
213 return tevent_req_post(req, ev);
215 tevent_req_set_callback(subreq, smb2cli_sesssetup_done, req);
218 tevent_req_nterror(req, status);
219 return tevent_req_post(req, ev);
222 static void smb2cli_sesssetup_done(struct tevent_req *subreq)
224 struct tevent_req *req =
225 tevent_req_callback_data(subreq,
227 struct smb2cli_sesssetup_state *state =
229 struct smb2cli_sesssetup_state);
232 DATA_BLOB blob, blob_in, blob_out, spnego_blob;
235 status = smb2cli_sesssetup_blob_recv(subreq, &uid, &blob);
236 if (!NT_STATUS_IS_OK(status)
237 && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
239 tevent_req_nterror(req, status);
243 if (NT_STATUS_IS_OK(status)) {
245 tevent_req_done(req);
249 if (state->turn == 1) {
250 DATA_BLOB tmp_blob = data_blob_null;
251 ret = spnego_parse_challenge(state, blob, &blob_in, &tmp_blob);
252 data_blob_free(&tmp_blob);
254 ret = spnego_parse_auth_response(state, blob, status,
255 OID_NTLMSSP, &blob_in);
259 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
263 status = ntlmssp_update(state->ntlmssp, blob_in, &blob_out);
264 data_blob_free(&blob_in);
267 if (!NT_STATUS_IS_OK(status)) {
268 tevent_req_nterror(req, status);
272 state->cli->smb2.uid = uid;
274 spnego_blob = spnego_gen_auth(state, blob_out);
276 if (tevent_req_nomem(spnego_blob.data, req)) {
280 subreq = smb2cli_sesssetup_blob_send(
281 state, state->ev, state->cli, &spnego_blob);
282 if (tevent_req_nomem(subreq, req)) {
285 tevent_req_set_callback(subreq, smb2cli_sesssetup_done, req);
288 NTSTATUS smb2cli_sesssetup_recv(struct tevent_req *req)
290 return tevent_req_simple_recv_ntstatus(req);
293 NTSTATUS smb2cli_sesssetup(struct cli_state *cli, const char *user,
294 const char *domain, const char *pass)
296 TALLOC_CTX *frame = talloc_stackframe();
297 struct event_context *ev;
298 struct tevent_req *req;
299 NTSTATUS status = NT_STATUS_NO_MEMORY;
301 if (cli_has_async_calls(cli)) {
303 * Can't use sync call while an async call is in flight
305 status = NT_STATUS_INVALID_PARAMETER;
308 ev = event_context_init(frame);
312 req = smb2cli_sesssetup_send(frame, ev, cli, user, domain, pass);
316 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
319 status = smb2cli_sesssetup_recv(req);
325 struct smb2cli_logoff_state {
329 static void smb2cli_logoff_done(struct tevent_req *subreq);
331 struct tevent_req *smb2cli_logoff_send(TALLOC_CTX *mem_ctx,
332 struct tevent_context *ev,
333 struct cli_state *cli)
335 struct tevent_req *req, *subreq;
336 struct smb2cli_logoff_state *state;
338 req = tevent_req_create(mem_ctx, &state,
339 struct smb2cli_logoff_state);
343 SSVAL(state->fixed, 0, 4);
345 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_LOGOFF,
350 state->fixed, sizeof(state->fixed),
352 if (tevent_req_nomem(subreq, req)) {
353 return tevent_req_post(req, ev);
355 tevent_req_set_callback(subreq, smb2cli_logoff_done, req);
359 static void smb2cli_logoff_done(struct tevent_req *subreq)
361 struct tevent_req *req =
362 tevent_req_callback_data(subreq,
367 status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 4);
369 if (tevent_req_nterror(req, status)) {
372 tevent_req_done(req);
375 NTSTATUS smb2cli_logoff_recv(struct tevent_req *req)
377 return tevent_req_simple_recv_ntstatus(req);
380 NTSTATUS smb2cli_logoff(struct cli_state *cli)
382 TALLOC_CTX *frame = talloc_stackframe();
383 struct event_context *ev;
384 struct tevent_req *req;
385 NTSTATUS status = NT_STATUS_NO_MEMORY;
387 if (cli_has_async_calls(cli)) {
389 * Can't use sync call while an async call is in flight
391 status = NT_STATUS_INVALID_PARAMETER;
394 ev = event_context_init(frame);
398 req = smb2cli_logoff_send(frame, ev, cli);
402 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
405 status = smb2cli_logoff_recv(req);