r26260: Store loadparm context in gensec context.
[jelmer/samba4-debian.git] / source / 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 "libcli/raw/libcliraw.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "libcli/composite/composite.h"
27 #include "auth/gensec/gensec.h"
28 #include "param/param.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                                      global_loadparm);
53         if (!NT_STATUS_IS_OK(status)) {
54                 talloc_free(session);
55                 return NULL;
56         }
57
58         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
59
60         return session;
61 }
62
63 /**
64   send a session setup request
65 */
66 struct smb2_request *smb2_session_setup_send(struct smb2_session *session, 
67                                              struct smb2_session_setup *io)
68 {
69         struct smb2_request *req;
70         NTSTATUS status;
71         
72         req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP, 
73                                 0x18, true, io->in.secblob.length);
74         if (req == NULL) return NULL;
75
76         SBVAL(req->out.hdr,  SMB2_HDR_UID, session->uid);
77         SSVAL(req->out.body, 0x02, io->in._pad); /* pad */
78         SIVAL(req->out.body, 0x04, io->in.unknown2);
79         SIVAL(req->out.body, 0x08, io->in.unknown3);
80
81         req->session = session;
82
83         status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
84         if (!NT_STATUS_IS_OK(status)) {
85                 talloc_free(req);
86                 return NULL;
87         }
88         SBVAL(req->out.body, 0x10, io->in.unknown4);
89
90         smb2_transport_send(req);
91
92         return req;
93 }
94
95
96 /**
97   recv a session setup reply
98 */
99 NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, 
100                                  struct smb2_session_setup *io)
101 {
102         NTSTATUS status;
103
104         if (!smb2_request_receive(req) || 
105             (smb2_request_is_error(req) && 
106              !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
107                 return smb2_request_destroy(req);
108         }
109
110         SMB2_CHECK_PACKET_RECV(req, 0x08, true);
111
112         io->out._pad     = SVAL(req->in.body, 0x02);
113         io->out.uid      = BVAL(req->in.hdr,  SMB2_HDR_UID);
114         
115         status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
116         if (!NT_STATUS_IS_OK(status)) {
117                 smb2_request_destroy(req);
118                 return status;
119         }
120
121         return smb2_request_destroy(req);
122 }
123
124 /*
125   sync session setup request
126 */
127 NTSTATUS smb2_session_setup(struct smb2_session *session, 
128                             TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
129 {
130         struct smb2_request *req = smb2_session_setup_send(session, io);
131         return smb2_session_setup_recv(req, mem_ctx, io);
132 }
133
134
135 struct smb2_session_state {
136         struct smb2_session_setup io;
137         struct smb2_request *req;
138         NTSTATUS gensec_status;
139 };
140
141 /*
142   handle continuations of the spnego session setup
143 */
144 static void session_request_handler(struct smb2_request *req)
145 {
146         struct composite_context *c = talloc_get_type(req->async.private, 
147                                                       struct composite_context);
148         struct smb2_session_state *state = talloc_get_type(c->private_data, 
149                                                            struct smb2_session_state);
150         struct smb2_session *session = req->session;
151
152         c->status = smb2_session_setup_recv(req, c, &state->io);
153         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
154             (NT_STATUS_IS_OK(c->status) && 
155              NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
156                 NTSTATUS session_key_err;
157                 DATA_BLOB session_key;
158                 c->status = gensec_update(session->gensec, c, 
159                                           state->io.out.secblob,
160                                           &state->io.in.secblob);
161                 state->gensec_status = c->status;
162
163                 session_key_err = gensec_session_key(session->gensec, &session_key);
164                 if (NT_STATUS_IS_OK(session_key_err)) {
165                         session->session_key = session_key;
166                 }
167         }
168
169         session->uid = state->io.out.uid;
170
171         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
172                 state->req = smb2_session_setup_send(session, &state->io);
173                 if (state->req == NULL) {
174                         composite_error(c, NT_STATUS_NO_MEMORY);
175                         return;
176                 }
177
178                 state->req->async.fn = session_request_handler;
179                 state->req->async.private = c;
180                 return;
181         }
182
183         if (!NT_STATUS_IS_OK(c->status)) {
184                 composite_error(c, c->status);
185                 return;
186         }
187
188         composite_done(c);
189 }
190
191 /*
192   a composite function that does a full SPNEGO session setup
193  */
194 struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session, 
195                                                          struct cli_credentials *credentials)
196 {
197         struct composite_context *c;
198         struct smb2_session_state *state;
199
200         c = composite_create(session, session->transport->socket->event.ctx);
201         if (c == NULL) return NULL;
202
203         state = talloc(c, struct smb2_session_state);
204         if (composite_nomem(state, c)) return c;
205         c->private_data = state;
206
207         ZERO_STRUCT(state->io);
208         state->io.in._pad = 0x0000;
209         state->io.in.unknown2 = 0x0000000F;
210         state->io.in.unknown3 = 0x00000000;
211         state->io.in.unknown4 = 0; /* uint64_t */
212
213         c->status = gensec_set_credentials(session->gensec, credentials);
214         if (!composite_is_ok(c)) return c;
215
216         c->status = gensec_set_target_hostname(session->gensec, 
217                                                session->transport->socket->hostname);
218         if (!composite_is_ok(c)) return c;
219
220         c->status = gensec_set_target_service(session->gensec, "cifs");
221         if (!composite_is_ok(c)) return c;
222
223         c->status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
224         if (!composite_is_ok(c)) return c;
225
226         c->status = gensec_update(session->gensec, c, 
227                                   session->transport->negotiate.secblob,
228                                   &state->io.in.secblob);
229         if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
230                 composite_error(c, c->status);
231                 return c;
232         }
233         state->gensec_status = c->status;
234                 
235         state->req = smb2_session_setup_send(session, &state->io);
236         composite_continue_smb2(c, state->req, session_request_handler, c);
237         return c;
238 }
239
240 /*
241   receive a composite session setup reply
242 */
243 NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
244 {
245         NTSTATUS status;
246         status = composite_wait(c);
247         talloc_free(c);
248         return status;
249 }
250
251 /*
252   sync version of smb2_session_setup_spnego
253 */
254 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session, 
255                                    struct cli_credentials *credentials)
256 {
257         struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
258         return smb2_session_setup_spnego_recv(c);
259 }