8ebdc93bd426feab546b74bfd6839cd13737bc32
[samba.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                                 0x18, True, 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, io->in._pad); /* 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         SBVAL(req->out.body, 0x10, io->in.unknown4);
88
89         smb2_transport_send(req);
90
91         return req;
92 }
93
94
95 /*
96   recv a session setup reply
97 */
98 NTSTATUS smb2_session_setup_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, 
99                                  struct smb2_session_setup *io)
100 {
101         NTSTATUS status;
102
103         if (!smb2_request_receive(req) || 
104             (smb2_request_is_error(req) && 
105              !NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
106                 return smb2_request_destroy(req);
107         }
108
109         SMB2_CHECK_PACKET_RECV(req, 0x08, True);
110
111         io->out._pad     = SVAL(req->in.body, 0x02);
112         io->out.uid      = BVAL(req->in.hdr,  SMB2_HDR_UID);
113         
114         status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x04, &io->out.secblob);
115         if (!NT_STATUS_IS_OK(status)) {
116                 smb2_request_destroy(req);
117                 return status;
118         }
119
120         return smb2_request_destroy(req);
121 }
122
123 /*
124   sync session setup request
125 */
126 NTSTATUS smb2_session_setup(struct smb2_session *session, 
127                             TALLOC_CTX *mem_ctx, struct smb2_session_setup *io)
128 {
129         struct smb2_request *req = smb2_session_setup_send(session, io);
130         return smb2_session_setup_recv(req, mem_ctx, io);
131 }
132
133
134 struct smb2_session_state {
135         struct smb2_session_setup io;
136         struct smb2_request *req;
137         NTSTATUS gensec_status;
138 };
139
140 /*
141   handle continuations of the spnego session setup
142 */
143 static void session_request_handler(struct smb2_request *req)
144 {
145         struct composite_context *c = talloc_get_type(req->async.private, 
146                                                       struct composite_context);
147         struct smb2_session_state *state = talloc_get_type(c->private_data, 
148                                                            struct smb2_session_state);
149         struct smb2_session *session = req->session;
150
151         c->status = smb2_session_setup_recv(req, c, &state->io);
152         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
153             (NT_STATUS_IS_OK(c->status) && 
154              NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED))) {
155                 NTSTATUS session_key_err;
156                 DATA_BLOB session_key;
157                 c->status = gensec_update(session->gensec, c, 
158                                           state->io.out.secblob,
159                                           &state->io.in.secblob);
160                 state->gensec_status = c->status;
161
162                 session_key_err = gensec_session_key(session->gensec, &session_key);
163                 if (NT_STATUS_IS_OK(session_key_err)) {
164                         session->session_key = session_key;
165                 }
166         }
167
168         session->uid = state->io.out.uid;
169
170         if (NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
171                 state->req = smb2_session_setup_send(session, &state->io);
172                 if (state->req == NULL) {
173                         composite_error(c, NT_STATUS_NO_MEMORY);
174                         return;
175                 }
176
177                 state->req->async.fn = session_request_handler;
178                 state->req->async.private = c;
179                 return;
180         }
181
182         if (!NT_STATUS_IS_OK(c->status)) {
183                 composite_error(c, c->status);
184                 return;
185         }
186
187         composite_done(c);
188 }
189
190 /*
191   a composite function that does a full SPNEGO session setup
192  */
193 struct composite_context *smb2_session_setup_spnego_send(struct smb2_session *session, 
194                                                          struct cli_credentials *credentials)
195 {
196         struct composite_context *c;
197         struct smb2_session_state *state;
198
199         c = composite_create(session, session->transport->socket->event.ctx);
200         if (c == NULL) return NULL;
201
202         state = talloc(c, struct smb2_session_state);
203         if (composite_nomem(state, c)) return c;
204         c->private_data = state;
205
206         ZERO_STRUCT(state->io);
207         state->io.in._pad = 0x0000;
208         state->io.in.unknown2 = 0x0000000F;
209         state->io.in.unknown3 = 0x00000000;
210         state->io.in.unknown4 = 0; /* uint64_t */
211
212         c->status = gensec_set_credentials(session->gensec, credentials);
213         if (!composite_is_ok(c)) return c;
214
215         c->status = gensec_set_target_hostname(session->gensec, 
216                                                session->transport->socket->hostname);
217         if (!composite_is_ok(c)) return c;
218
219         c->status = gensec_set_target_service(session->gensec, "cifs");
220         if (!composite_is_ok(c)) return c;
221
222         c->status = gensec_start_mech_by_oid(session->gensec, GENSEC_OID_SPNEGO);
223         if (!composite_is_ok(c)) return c;
224
225         c->status = gensec_update(session->gensec, c, 
226                                   session->transport->negotiate.secblob,
227                                   &state->io.in.secblob);
228         if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
229                 composite_error(c, c->status);
230                 return c;
231         }
232         state->gensec_status = c->status;
233                 
234         state->req = smb2_session_setup_send(session, &state->io);
235         composite_continue_smb2(c, state->req, session_request_handler, c);
236         return c;
237 }
238
239 /*
240   receive a composite session setup reply
241 */
242 NTSTATUS smb2_session_setup_spnego_recv(struct composite_context *c)
243 {
244         NTSTATUS status;
245         status = composite_wait(c);
246         talloc_free(c);
247         return status;
248 }
249
250 /*
251   sync version of smb2_session_setup_spnego
252 */
253 NTSTATUS smb2_session_setup_spnego(struct smb2_session *session, 
254                                    struct cli_credentials *credentials)
255 {
256         struct composite_context *c = smb2_session_setup_spnego_send(session, credentials);
257         return smb2_session_setup_spnego_recv(c);
258 }