r12006: don't require callers to fill in pad bytes in SMB2 calls
[sfrench/samba-autobuild/.git] / source4 / libcli / smb2 / session.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client session handling
5
6    Copyright (C) Andrew Tridgell 2005
7    
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/composite/composite.h"
28 #include "auth/gensec/gensec.h"
29
30 /*
31   initialise a smb2_session structure
32  */
33 struct smb2_session *smb2_session_init(struct smb2_transport *transport,
34                                        TALLOC_CTX *parent_ctx, BOOL primary)
35 {
36         struct smb2_session *session;
37         NTSTATUS status;
38
39         session = talloc_zero(parent_ctx, struct smb2_session);
40         if (!session) {
41                 return NULL;
42         }
43         if (primary) {
44                 session->transport = talloc_steal(session, transport);
45         } else {
46                 session->transport = talloc_reference(session, transport);
47         }
48
49         /* prepare a gensec context for later use */
50         status = gensec_client_start(session, &session->gensec, 
51                                      session->transport->socket->event.ctx);
52         if (!NT_STATUS_IS_OK(status)) {
53                 talloc_free(session);
54                 return NULL;
55         }
56
57         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
58
59         return session;
60 }
61
62 /*
63   send a session setup request
64 */
65 struct smb2_request *smb2_session_setup_send(struct smb2_session *session, 
66                                              struct smb2_session_setup *io)
67 {
68         struct smb2_request *req;
69         NTSTATUS status;
70         
71         req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP, 
72                                 0x10, io->in.secblob.length);
73         if (req == NULL) return NULL;
74
75         SBVAL(req->out.hdr,  SMB2_HDR_UID, session->uid);
76         SSVAL(req->out.body, 0x02, 0); /* pad */
77         SIVAL(req->out.body, 0x04, io->in.unknown2);
78         SIVAL(req->out.body, 0x08, io->in.unknown3);
79
80         req->session = session;
81
82         status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
83         if (!NT_STATUS_IS_OK(status)) {
84                 talloc_free(req);
85                 return NULL;
86         }
87
88         smb2_transport_send(req);
89
90         return req;
91 }
92
93
94 /*
95   recv a session setup reply
96 */
97 NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, 
98                                  struct smb2_session_setup *io)
99 {
100         NTSTATUS status;
101
102         if (!smb2_request_receive(req) || 
103             (smb2_request_is_error(req) && 
104              !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
105                 return smb2_request_destroy(req);
106         }
107
108         SMB2_CHECK_PACKET_RECV(req, 0x08, True);
109
110         io->out._pad     = SVAL(req->in.body, 0x02);
111         io->out.uid      = BVAL(req->in.hdr,  SMB2_HDR_UID);
112         
113         status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
114         if (!NT_STATUS_IS_OK(status)) {
115                 smb2_request_destroy(req);
116                 return status;
117         }
118
119         return smb2_request_destroy(req);
120 }
121
122 /*
123   sync session setup request
124 */
125 NTSTATUS smb2_session_setup(struct smb2_session *session, 
126                             TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
127 {
128         struct smb2_request *req = smb2_session_setup_send(session, io);
129         return smb2_session_setup_recv(req, mem_ctx, io);
130 }
131
132
133 struct smb2_session_state {
134         struct smb2_session_setup io;
135         struct smb2_request *req;
136         NTSTATUS gensec_status;
137 };
138
139 /*
140   handle continuations of the spnego session setup
141 */
142 static void session_request_handler(struct smb2_request *req)
143 {
144         struct composite_context *c = talloc_get_type(req->async.private, 
145                                                       struct composite_context);
146         struct smb2_session_state *state = talloc_get_type(c->private_data, 
147                                                            struct smb2_session_state);
148         struct smb2_session *session = req->session;
149
150         c->status = smb2_session_setup_recv(req, c, &state->io);
151         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
152             (NT_STATUS_IS_OK(c->status) && 
153              NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
154                 NTSTATUS session_key_err;
155                 DATA_BLOB session_key;
156                 c->status = gensec_update(session->gensec, c, 
157                                           state->io.out.secblob,
158                                           &state->io.in.secblob);
159                 state->gensec_status = c->status;
160
161                 session_key_err = gensec_session_key(session->gensec, &session_key);
162                 if (NT_STATUS_IS_OK(session_key_err)) {
163                         session->session_key = session_key;
164                 }
165         }
166
167         session->uid = state->io.out.uid;
168
169         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
170                 state->req = smb2_session_setup_send(session, &state->io);
171                 if (state->req == NULL) {
172                         composite_error(c, NT_STATUS_NO_MEMORY);
173                 }
174
175                 state->req->async.fn = session_request_handler;
176                 state->req->async.private = c;
177                 return;
178         }
179
180         if (!NT_STATUS_IS_OK(c->status)) {
181                 composite_error(c, c->status);
182                 return;
183         }
184
185         composite_done(c);
186 }
187
188 /*
189   a composite function that does a full SPNEGO session setup
190  */
191 struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session, 
192                                                          struct cli_credentials *credentials)
193 {
194         struct composite_context *c;
195         struct smb2_session_state *state;
196
197         c = talloc_zero(session, struct composite_context);
198         if (c == NULL) return NULL;
199
200         state = talloc(c, struct smb2_session_state);
201         if (state == NULL) {
202                 c->status = NT_STATUS_NO_MEMORY;
203                 goto failed;
204         }
205
206         c->state = COMPOSITE_STATE_IN_PROGRESS;
207         c->private_data = state;
208         c->event_ctx = session->transport->socket->event.ctx;
209
210         ZERO_STRUCT(state->io);
211         state->io.in._pad = 0x0;
212         state->io.in.unknown2 = 0xF;
213         state->io.in.unknown3 = 0x00;
214
215         c->status = gensec_set_credentials(session->gensec, credentials);
216         if (!NT_STATUS_IS_OK(c->status)) {
217                 goto failed;
218         }
219
220         c->status = gensec_set_target_hostname(session->gensec, 
221                                                session->transport->socket->hostname);
222         if (!NT_STATUS_IS_OK(c->status)) {
223                 goto failed;
224         }
225
226         c->status = gensec_set_target_service(session->gensec, "cifs");
227         if (!NT_STATUS_IS_OK(c->status)) {
228                 goto failed;
229         }
230
231         c->status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
232         if (!NT_STATUS_IS_OK(c->status)) {
233                 goto failed;
234         }
235
236         c->status = gensec_update(session->gensec, c, 
237                                   session->transport->negotiate.secblob,
238                                   &state->io.in.secblob);
239         if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
240                 goto failed;
241         }
242         state->gensec_status = c->status;
243                 
244         state->req = smb2_session_setup_send(session, &state->io);
245         if (state->req == NULL) {
246                 c->status = NT_STATUS_NO_MEMORY;
247                 goto failed;
248         }
249
250         state->req->async.fn = session_request_handler;
251         state->req->async.private = c;
252
253         return c;
254
255 failed:
256         composite_trigger_error(c);
257         return c;
258 }
259
260 /*
261   receive a composite session setup reply
262 */
263 NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
264 {
265         NTSTATUS status;
266         status = composite_wait(c);
267         talloc_free(c);
268         return status;
269 }
270
271 /*
272   sync version of smb2_session_setup_spnego
273 */
274 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session, 
275                                    struct cli_credentials *credentials)
276 {
277         struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
278         return smb2_session_setup_spnego_recv(c);
279 }