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