gensec: move event context from gensec_*_init() to gensec_update()
[obnox/samba/samba-obnox.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include <tevent.h>
24 #include "lib/util/tevent_ntstatus.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "auth/gensec/gensec.h"
29
30 #include <unistd.h>
31
32 /**
33   initialise a smb2_session structure
34  */
35 struct smb2_session *smb2_session_init(struct smb2_transport *transport,
36                                        struct gensec_settings *settings,
37                                        TALLOC_CTX *parent_ctx, bool primary)
38 {
39         struct smb2_session *session;
40         NTSTATUS status;
41
42         session = talloc_zero(parent_ctx, struct smb2_session);
43         if (!session) {
44                 return NULL;
45         }
46         if (primary) {
47                 session->transport = talloc_steal(session, transport);
48         } else {
49                 session->transport = talloc_reference(session, transport);
50         }
51
52         session->pid = getpid();
53
54         /* prepare a gensec context for later use */
55         status = gensec_client_start(session, &session->gensec, 
56                                      settings);
57         if (!NT_STATUS_IS_OK(status)) {
58                 talloc_free(session);
59                 return NULL;
60         }
61
62         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
63
64         return session;
65 }
66
67 /**
68   send a session setup request
69 */
70 struct smb2_request *smb2_session_setup_send(struct smb2_session *session, 
71                                              struct smb2_session_setup *io)
72 {
73         struct smb2_request *req;
74         NTSTATUS status;
75         
76         req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP, 
77                                 0x18, true, io->in.secblob.length);
78         if (req == NULL) return NULL;
79
80         SBVAL(req->out.hdr,  SMB2_HDR_SESSION_ID, session->uid);
81         SCVAL(req->out.body, 0x02, io->in.vc_number);
82         SCVAL(req->out.body, 0x03, io->in.security_mode);
83         SIVAL(req->out.body, 0x04, io->in.capabilities);
84         SIVAL(req->out.body, 0x08, io->in.channel);
85         SBVAL(req->out.body, 0x10, io->in.previous_sessionid);
86
87         req->session = session;
88
89         status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
90         if (!NT_STATUS_IS_OK(status)) {
91                 talloc_free(req);
92                 return NULL;
93         }
94
95         smb2_transport_send(req);
96
97         return req;
98 }
99
100
101 /**
102   recv a session setup reply
103 */
104 NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, 
105                                  struct smb2_session_setup *io)
106 {
107         NTSTATUS status;
108
109         if (!smb2_request_receive(req) || 
110             (smb2_request_is_error(req) && 
111              !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
112                 return smb2_request_destroy(req);
113         }
114
115         SMB2_CHECK_PACKET_RECV(req, 0x08, true);
116
117         io->out.session_flags = SVAL(req->in.body, 0x02);
118         io->out.uid           = BVAL(req->in.hdr,  SMB2_HDR_SESSION_ID);
119         
120         status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
121         if (!NT_STATUS_IS_OK(status)) {
122                 smb2_request_destroy(req);
123                 return status;
124         }
125
126         return smb2_request_destroy(req);
127 }
128
129 /*
130   sync session setup request
131 */
132 NTSTATUS smb2_session_setup(struct smb2_session *session, 
133                             TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
134 {
135         struct smb2_request *req = smb2_session_setup_send(session, io);
136         return smb2_session_setup_recv(req, mem_ctx, io);
137 }
138
139 struct smb2_session_setup_spnego_state {
140         struct smb2_session_setup io;
141         struct smb2_request *req;
142         NTSTATUS gensec_status;
143 };
144
145 static void smb2_session_setup_spnego_handler(struct smb2_request *req);
146
147 /*
148   a composite function that does a full SPNEGO session setup
149  */
150 struct tevent_req *smb2_session_setup_spnego_send(TALLOC_CTX *mem_ctx,
151                                                   struct tevent_context *ev,
152                                                   struct smb2_session *session,
153                                                   struct cli_credentials *credentials)
154 {
155         struct tevent_req *req;
156         struct smb2_session_setup_spnego_state *state;
157         const char *chosen_oid;
158         struct smb2_request *subreq;
159         NTSTATUS status;
160
161         req = tevent_req_create(mem_ctx, &state,
162                                 struct smb2_session_setup_spnego_state);
163         if (req == NULL) {
164                 return NULL;
165         }
166
167         ZERO_STRUCT(state->io);
168         state->io.in.vc_number          = 0;
169         if (session->transport->signing_required) {
170                 state->io.in.security_mode =
171                         SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
172         }
173         state->io.in.capabilities       = 0;
174         state->io.in.channel            = 0;
175         state->io.in.previous_sessionid = 0;
176
177         status = gensec_set_credentials(session->gensec, credentials);
178         if (tevent_req_nterror(req, status)) {
179                 return tevent_req_post(req, ev);
180         }
181
182         status = gensec_set_target_hostname(session->gensec,
183                                             session->transport->socket->hostname);
184         if (tevent_req_nterror(req, status)) {
185                 return tevent_req_post(req, ev);
186         }
187
188         status = gensec_set_target_service(session->gensec, "cifs");
189         if (tevent_req_nterror(req, status)) {
190                 return tevent_req_post(req, ev);
191         }
192
193         if (session->transport->negotiate.secblob.length > 0) {
194                 chosen_oid = GENSEC_OID_SPNEGO;
195         } else {
196                 chosen_oid = GENSEC_OID_NTLMSSP;
197         }
198
199         status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
200         if (tevent_req_nterror(req, status)) {
201                 return tevent_req_post(req, ev);
202         }
203
204         status = gensec_update(session->gensec, state,
205                                session->transport->socket->event.ctx,
206                                session->transport->negotiate.secblob,
207                                &state->io.in.secblob);
208         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
209                 tevent_req_nterror(req, status);
210                 return tevent_req_post(req, ev);
211         }
212         state->gensec_status = status;
213
214         subreq = smb2_session_setup_send(session, &state->io);
215         if (tevent_req_nomem(subreq, req)) {
216                 return tevent_req_post(req, ev);
217         }
218         subreq->async.fn = smb2_session_setup_spnego_handler;
219         subreq->async.private_data = req;
220
221         return req;
222 }
223
224 /*
225   handle continuations of the spnego session setup
226 */
227 static void smb2_session_setup_spnego_handler(struct smb2_request *subreq)
228 {
229         struct tevent_req *req =
230                 talloc_get_type_abort(subreq->async.private_data,
231                 struct tevent_req);
232         struct smb2_session_setup_spnego_state *state =
233                 tevent_req_data(req,
234                 struct smb2_session_setup_spnego_state);
235         struct smb2_session *session = subreq->session;
236         NTSTATUS peer_status;
237         NTSTATUS status;
238
239         status = smb2_session_setup_recv(subreq, state, &state->io);
240         peer_status = status;
241         if (NT_STATUS_EQUAL(peer_status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
242             (NT_STATUS_IS_OK(peer_status) &&
243              NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
244                 status = gensec_update(session->gensec, state,
245                                        session->transport->socket->event.ctx,
246                                        state->io.out.secblob,
247                                        &state->io.in.secblob);
248                 state->gensec_status = status;
249                 session->uid = state->io.out.uid;
250         }
251
252         if (!NT_STATUS_IS_OK(status) &&
253             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
254                 tevent_req_nterror(req, status);
255                 return;
256         }
257
258         if (NT_STATUS_EQUAL(peer_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
259                 subreq = smb2_session_setup_send(session, &state->io);
260                 if (tevent_req_nomem(subreq, req)) {
261                         return;
262                 }
263
264                 subreq->async.fn = smb2_session_setup_spnego_handler;
265                 subreq->async.private_data = req;
266                 return;
267         }
268
269         gensec_session_key(session->gensec, session, &session->session_key);
270
271         if (session->transport->signing_required) {
272                 if (session->session_key.length == 0) {
273                         DEBUG(0,("Wrong session key length %u for SMB2 signing\n",
274                                  (unsigned)session->session_key.length));
275                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
276                         return;
277                 }
278                 session->signing_active = true;
279         }
280
281         tevent_req_done(req);
282 }
283
284 /*
285   receive a composite session setup reply
286 */
287 NTSTATUS smb2_session_setup_spnego_recv(struct tevent_req *req)
288 {
289         return tevent_req_simple_recv_ntstatus(req);
290 }
291
292 /*
293   sync version of smb2_session_setup_spnego
294 */
295 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session, 
296                                    struct cli_credentials *credentials)
297 {
298         struct tevent_req *subreq;
299         NTSTATUS status;
300         bool ok;
301         TALLOC_CTX *frame = talloc_stackframe();
302         struct tevent_context *ev = session->transport->socket->event.ctx;
303
304         if (frame == NULL) {
305                 return NT_STATUS_NO_MEMORY;
306         }
307
308         subreq = smb2_session_setup_spnego_send(frame, ev,
309                                                 session, credentials);
310         if (subreq == NULL) {
311                 TALLOC_FREE(frame);
312                 return NT_STATUS_NO_MEMORY;
313         }
314
315         ok = tevent_req_poll(subreq, ev);
316         if (!ok) {
317                 status = map_nt_error_from_unix_common(errno);
318                 TALLOC_FREE(frame);
319                 return status;
320         }
321
322         status = smb2_session_setup_spnego_recv(subreq);
323         TALLOC_FREE(subreq);
324         if (!NT_STATUS_IS_OK(status)) {
325                 TALLOC_FREE(frame);
326                 return status;
327         }
328
329         TALLOC_FREE(frame);
330         return NT_STATUS_OK;
331 }