3f9b3ed55c199e65c8e07b2420a9f04974610a37
[kai/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 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
29 /*
30   initialise a smb2_session structure
31  */
32 struct smb2_session *smb2_session_init(struct smb2_transport *transport,
33                                        TALLOC_CTX *parent_ctx, BOOL primary)
34 {
35         struct smb2_session *session;
36         NTSTATUS status;
37
38         session = talloc_zero(parent_ctx, struct smb2_session);
39         if (!session) {
40                 return NULL;
41         }
42         if (primary) {
43                 session->transport = talloc_steal(session, transport);
44         } else {
45                 session->transport = talloc_reference(session, transport);
46         }
47
48         /* prepare a gensec context for later use */
49         status = gensec_client_start(session, &session->gensec, 
50                                      session->transport->socket->event.ctx);
51         if (!NT_STATUS_IS_OK(status)) {
52                 talloc_free(session);
53                 return NULL;
54         }
55
56         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
57
58         return session;
59 }
60
61 /*
62   send a session setup request
63 */
64 struct smb2_request *smb2_session_setup_send(struct smb2_session *session, 
65                                              struct smb2_session_setup *io)
66 {
67         struct smb2_request *req;
68         NTSTATUS status;
69         
70         req = smb2_request_init(session->transport, SMB2_OP_SESSSETUP, 
71                                 0x18, True, io->in.secblob.length);
72         if (req == NULL) return NULL;
73
74         SBVAL(req->out.hdr,  SMB2_HDR_UID, session->uid);
75         SSVAL(req->out.body, 0x02, io->in._pad); /* pad */
76         SIVAL(req->out.body, 0x04, io->in.unknown2);
77         SIVAL(req->out.body, 0x08, io->in.unknown3);
78
79         req->session = session;
80
81         status = smb2_push_o16s16_blob(&req->out, 0x0C, io->in.secblob);
82         if (!NT_STATUS_IS_OK(status)) {
83                 talloc_free(req);
84                 return NULL;
85         }
86         SBVAL(req->out.body, 0x10, io->in.unknown4);
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                         return;
174                 }
175
176                 state->req->async.fn = session_request_handler;
177                 state->req->async.private = c;
178                 return;
179         }
180
181         if (!NT_STATUS_IS_OK(c->status)) {
182                 composite_error(c, c->status);
183                 return;
184         }
185
186         composite_done(c);
187 }
188
189 /*
190   a composite function that does a full SPNEGO session setup
191  */
192 struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session, 
193                                                          struct cli_credentials *credentials)
194 {
195         struct composite_context *c;
196         struct smb2_session_state *state;
197
198         c = composite_create(session, session->transport->socket->event.ctx);
199         if (c == NULL) return NULL;
200
201         state = talloc(c, struct smb2_session_state);
202         if (composite_nomem(state, c)) return c;
203         c->private_data = state;
204
205         ZERO_STRUCT(state->io);
206         state->io.in._pad = 0x0000;
207         state->io.in.unknown2 = 0x0000000F;
208         state->io.in.unknown3 = 0x00000000;
209         state->io.in.unknown4 = 0; /* uint64_t */
210
211         c->status = gensec_set_credentials(session->gensec, credentials);
212         if (!composite_is_ok(c)) return c;
213
214         c->status = gensec_set_target_hostname(session->gensec, 
215                                                session->transport->socket->hostname);
216         if (!composite_is_ok(c)) return c;
217
218         c->status = gensec_set_target_service(session->gensec, "cifs");
219         if (!composite_is_ok(c)) return c;
220
221         c->status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
222         if (!composite_is_ok(c)) return c;
223
224         c->status = gensec_update(session->gensec, c, 
225                                   session->transport->negotiate.secblob,
226                                   &state->io.in.secblob);
227         if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
228                 composite_error(c, c->status);
229                 return c;
230         }
231         state->gensec_status = c->status;
232                 
233         state->req = smb2_session_setup_send(session, &state->io);
234         composite_continue_smb2(c, state->req, session_request_handler, c);
235         return c;
236 }
237
238 /*
239   receive a composite session setup reply
240 */
241 NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
242 {
243         NTSTATUS status;
244         status = composite_wait(c);
245         talloc_free(c);
246         return status;
247 }
248
249 /*
250   sync version of smb2_session_setup_spnego
251 */
252 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session, 
253                                    struct cli_credentials *credentials)
254 {
255         struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
256         return smb2_session_setup_spnego_recv(c);
257 }