4ffdf13dd6049341c50f4f96f8569afa610426b5
[obnox/samba/samba-obnox.git] / source3 / libsmb / smb2cli_session.c
1 /*
2    Unix SMB/CIFS implementation.
3    smb2 lib
4    Copyright (C) Volker Lendecke 2011
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "client.h"
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
24 #include "smb2cli.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"
29
30 struct smb2cli_sesssetup_blob_state {
31         struct ntlmssp_state *ntlmssp;
32         uint8_t fixed[24];
33         uint8_t dyn_pad[1];
34         uint64_t uid;
35         DATA_BLOB out;
36 };
37
38 static void smb2cli_sesssetup_blob_done(struct tevent_req *subreq);
39
40 static struct tevent_req *smb2cli_sesssetup_blob_send(TALLOC_CTX *mem_ctx,
41                                                 struct tevent_context *ev,
42                                                 struct cli_state *cli,
43                                                 DATA_BLOB *blob)
44 {
45         struct tevent_req *req, *subreq;
46         struct smb2cli_sesssetup_blob_state *state;
47         uint8_t *buf;
48         uint8_t *dyn;
49         size_t dyn_len;
50
51         req = tevent_req_create(mem_ctx, &state,
52                                 struct smb2cli_sesssetup_blob_state);
53         if (req == NULL) {
54                 return NULL;
55         }
56
57         buf = state->fixed;
58
59         SSVAL(buf, 0, 25);
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 */
67
68         if (blob->length > 0) {
69                 dyn = blob->data;
70                 dyn_len = blob->length;
71         } else {
72                 dyn = state->dyn_pad;;
73                 dyn_len = sizeof(state->dyn_pad);
74         }
75
76         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_SESSSETUP,
77                                   0, 0, /* flags */
78                                   cli->smb2.pid,
79                                   0, /* tid */
80                                   cli->smb2.uid,
81                                   state->fixed, sizeof(state->fixed),
82                                   dyn, dyn_len);
83         if (tevent_req_nomem(subreq, req)) {
84                 return tevent_req_post(req, ev);
85         }
86         tevent_req_set_callback(subreq, smb2cli_sesssetup_blob_done, req);
87         return req;
88 }
89
90 static void smb2cli_sesssetup_blob_done(struct tevent_req *subreq)
91 {
92         struct tevent_req *req =
93                 tevent_req_callback_data(subreq,
94                 struct tevent_req);
95         struct smb2cli_sesssetup_blob_state *state =
96                 tevent_req_data(req,
97                 struct smb2cli_sesssetup_blob_state);
98         NTSTATUS status;
99         struct iovec *iov;
100         uint16_t offset, length;
101
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)) {
105                 TALLOC_FREE(subreq);
106                 tevent_req_nterror(req, status);
107                 return;
108         }
109
110         offset = SVAL(iov[1].iov_base, 4);
111         length = SVAL(iov[1].iov_base, 6);
112
113         if ((offset != SMB2_HDR_BODY + 8) || (length > iov[2].iov_len)) {
114                 TALLOC_FREE(subreq);
115                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
116                 return;
117         }
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);
123                 return;
124         }
125         tevent_req_done(req);
126 }
127
128 static NTSTATUS smb2cli_sesssetup_blob_recv(struct tevent_req *req,
129                                             uint64_t *uid, DATA_BLOB *out)
130 {
131         struct smb2cli_sesssetup_blob_state *state =
132                 tevent_req_data(req,
133                 struct smb2cli_sesssetup_blob_state);
134         NTSTATUS status = NT_STATUS_OK;
135
136         if (tevent_req_is_nterror(req, &status)
137             && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
138                 return status;
139         }
140         *uid = state->uid;
141         *out = state->out;
142         return status;
143 }
144
145 struct smb2cli_sesssetup_state {
146         struct tevent_context *ev;
147         struct cli_state *cli;
148         struct ntlmssp_state *ntlmssp;
149         struct iovec iov[2];
150         uint8_t fixed[24];
151         DATA_BLOB msg;
152         int turn;
153 };
154
155 static void smb2cli_sesssetup_done(struct tevent_req *subreq);
156
157 struct tevent_req *smb2cli_sesssetup_send(TALLOC_CTX *mem_ctx,
158                                           struct tevent_context *ev,
159                                           struct cli_state *cli,
160                                           const char *user,
161                                           const char *domain,
162                                           const char *pass)
163 {
164         struct tevent_req *req, *subreq;
165         struct smb2cli_sesssetup_state *state;
166         NTSTATUS status;
167         DATA_BLOB blob_out;
168         const char *OIDs_ntlm[] = {OID_NTLMSSP, NULL};
169
170         req = tevent_req_create(mem_ctx, &state,
171                                 struct smb2cli_sesssetup_state);
172         if (req == NULL) {
173                 return NULL;
174         }
175         state->ev = ev;
176         state->cli = cli;
177
178         status = ntlmssp_client_start(state,
179                                       lp_netbios_name(),
180                                       lp_workgroup(),
181                                       lp_client_ntlmv2_auth(),
182                                       &state->ntlmssp);
183         if (!NT_STATUS_IS_OK(status)) {
184                 goto post_status;
185         }
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)) {
190                 goto post_status;
191         }
192         status = ntlmssp_set_domain(state->ntlmssp, domain);
193         if (!NT_STATUS_IS_OK(status)) {
194                 goto post_status;
195         }
196         status = ntlmssp_set_password(state->ntlmssp, pass);
197         if (!NT_STATUS_IS_OK(status)) {
198                 goto post_status;
199         }
200
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)) {
204                 goto post_status;
205         }
206
207         blob_out = spnego_gen_negTokenInit(state, OIDs_ntlm, &blob_out, NULL);
208         state->turn = 1;
209
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);
214         }
215         tevent_req_set_callback(subreq, smb2cli_sesssetup_done, req);
216         return req;
217 post_status:
218         tevent_req_nterror(req, status);
219         return tevent_req_post(req, ev);
220 }
221
222 static void smb2cli_sesssetup_done(struct tevent_req *subreq)
223 {
224         struct tevent_req *req =
225                 tevent_req_callback_data(subreq,
226                 struct tevent_req);
227         struct smb2cli_sesssetup_state *state =
228                 tevent_req_data(req,
229                 struct smb2cli_sesssetup_state);
230         NTSTATUS status;
231         uint64_t uid = 0;
232         DATA_BLOB blob, blob_in, blob_out, spnego_blob;
233         bool ret;
234
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)) {
238                 TALLOC_FREE(subreq);
239                 tevent_req_nterror(req, status);
240                 return;
241         }
242
243         if (NT_STATUS_IS_OK(status)) {
244                 TALLOC_FREE(subreq);
245                 tevent_req_done(req);
246                 return;
247         }
248
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);
253         } else {
254                 ret = spnego_parse_auth_response(state, blob, status,
255                                                  OID_NTLMSSP, &blob_in);
256         }
257         TALLOC_FREE(subreq);
258         if (!ret) {
259                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
260                 return;
261         }
262
263         status = ntlmssp_update(state->ntlmssp, blob_in, &blob_out);
264         data_blob_free(&blob_in);
265         state->turn += 1;
266
267         if (!NT_STATUS_IS_OK(status)) {
268                 tevent_req_nterror(req, status);
269                 return;
270         }
271
272         state->cli->smb2.uid = uid;
273
274         spnego_blob = spnego_gen_auth(state, blob_out);
275         TALLOC_FREE(subreq);
276         if (tevent_req_nomem(spnego_blob.data, req)) {
277                 return;
278         }
279
280         subreq = smb2cli_sesssetup_blob_send(
281                 state, state->ev, state->cli, &spnego_blob);
282         if (tevent_req_nomem(subreq, req)) {
283                 return;
284         }
285         tevent_req_set_callback(subreq, smb2cli_sesssetup_done, req);
286 }
287
288 NTSTATUS smb2cli_sesssetup_recv(struct tevent_req *req)
289 {
290         return tevent_req_simple_recv_ntstatus(req);
291 }
292
293 NTSTATUS smb2cli_sesssetup(struct cli_state *cli, const char *user,
294                            const char *domain, const char *pass)
295 {
296         TALLOC_CTX *frame = talloc_stackframe();
297         struct event_context *ev;
298         struct tevent_req *req;
299         NTSTATUS status = NT_STATUS_NO_MEMORY;
300
301         if (cli_has_async_calls(cli)) {
302                 /*
303                  * Can't use sync call while an async call is in flight
304                  */
305                 status = NT_STATUS_INVALID_PARAMETER;
306                 goto fail;
307         }
308         ev = event_context_init(frame);
309         if (ev == NULL) {
310                 goto fail;
311         }
312         req = smb2cli_sesssetup_send(frame, ev, cli, user, domain, pass);
313         if (req == NULL) {
314                 goto fail;
315         }
316         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
317                 goto fail;
318         }
319         status = smb2cli_sesssetup_recv(req);
320  fail:
321         TALLOC_FREE(frame);
322         return status;
323 }
324
325 struct smb2cli_logoff_state {
326         uint8_t fixed[4];
327 };
328
329 static void smb2cli_logoff_done(struct tevent_req *subreq);
330
331 struct tevent_req *smb2cli_logoff_send(TALLOC_CTX *mem_ctx,
332                                        struct tevent_context *ev,
333                                        struct cli_state *cli)
334 {
335         struct tevent_req *req, *subreq;
336         struct smb2cli_logoff_state *state;
337
338         req = tevent_req_create(mem_ctx, &state,
339                                 struct smb2cli_logoff_state);
340         if (req == NULL) {
341                 return NULL;
342         }
343         SSVAL(state->fixed, 0, 4);
344
345         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_LOGOFF,
346                                   0, 0, /* flags */
347                                   cli->smb2.pid,
348                                   0, /* tid */
349                                   cli->smb2.uid,
350                                   state->fixed, sizeof(state->fixed),
351                                   NULL, 0);
352         if (tevent_req_nomem(subreq, req)) {
353                 return tevent_req_post(req, ev);
354         }
355         tevent_req_set_callback(subreq, smb2cli_logoff_done, req);
356         return req;
357 }
358
359 static void smb2cli_logoff_done(struct tevent_req *subreq)
360 {
361         struct tevent_req *req =
362                 tevent_req_callback_data(subreq,
363                 struct tevent_req);
364         NTSTATUS status;
365         struct iovec *iov;
366
367         status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 4);
368         TALLOC_FREE(subreq);
369         if (tevent_req_nterror(req, status)) {
370                 return;
371         }
372         tevent_req_done(req);
373 }
374
375 NTSTATUS smb2cli_logoff_recv(struct tevent_req *req)
376 {
377         return tevent_req_simple_recv_ntstatus(req);
378 }
379
380 NTSTATUS smb2cli_logoff(struct cli_state *cli)
381 {
382         TALLOC_CTX *frame = talloc_stackframe();
383         struct event_context *ev;
384         struct tevent_req *req;
385         NTSTATUS status = NT_STATUS_NO_MEMORY;
386
387         if (cli_has_async_calls(cli)) {
388                 /*
389                  * Can't use sync call while an async call is in flight
390                  */
391                 status = NT_STATUS_INVALID_PARAMETER;
392                 goto fail;
393         }
394         ev = event_context_init(frame);
395         if (ev == NULL) {
396                 goto fail;
397         }
398         req = smb2cli_logoff_send(frame, ev, cli);
399         if (req == NULL) {
400                 goto fail;
401         }
402         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
403                 goto fail;
404         }
405         status = smb2cli_logoff_recv(req);
406  fail:
407         TALLOC_FREE(frame);
408         return status;
409 }