Merge commit 'release-4-0-0alpha1' into v4-0-test
[kai/samba.git] / source / smb_server / smb2 / sesssetup.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Andrew Bartlett        2001-2005
5    Copyright (C) Stefan Metzmacher      2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "auth/credentials/credentials.h"
23 #include "auth/gensec/gensec.h"
24 #include "auth/auth.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "smb_server/smb_server.h"
28 #include "smb_server/service_smb_proto.h"
29 #include "smb_server/smb2/smb2_server.h"
30 #include "smbd/service_stream.h"
31
32 static void smb2srv_sesssetup_send(struct smb2srv_request *req, union smb_sesssetup *io)
33 {
34         uint16_t unknown1;
35
36         if (NT_STATUS_IS_OK(req->status)) {
37                 unknown1 = 0x0003;
38         } else if (NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
39                 unknown1 = 0x0002;
40         } else {
41                 smb2srv_send_error(req, req->status);
42                 return;
43         }
44
45         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, io->smb2.out.secblob.length));
46
47         SSVAL(req->out.hdr, SMB2_HDR_UNKNOWN1,  unknown1);
48         SBVAL(req->out.hdr, SMB2_HDR_UID,       io->smb2.out.uid);
49
50         SSVAL(req->out.body, 0x02, io->smb2.out._pad);
51         SMB2SRV_CHECK(smb2_push_o16s16_blob(&req->out, 0x04, io->smb2.out.secblob));
52
53         smb2srv_send_reply(req);
54 }
55
56 struct smb2srv_sesssetup_callback_ctx {
57         struct smb2srv_request *req;
58         union smb_sesssetup *io;
59         struct smbsrv_session *smb_sess;
60 };
61
62 static void smb2srv_sesssetup_callback(struct gensec_update_request *greq, void *private_data)
63 {
64         struct smb2srv_sesssetup_callback_ctx *ctx = talloc_get_type(private_data,
65                                                      struct smb2srv_sesssetup_callback_ctx);
66         struct smb2srv_request *req = ctx->req;
67         union smb_sesssetup *io = ctx->io;
68         struct smbsrv_session *smb_sess = ctx->smb_sess;
69         struct auth_session_info *session_info = NULL;
70         NTSTATUS status;
71
72         status = gensec_update_recv(greq, req, &io->smb2.out.secblob);
73         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
74                 goto done;
75         } else if (!NT_STATUS_IS_OK(status)) {
76                 goto failed;
77         }
78
79         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
80         if (!NT_STATUS_IS_OK(status)) {
81                 goto failed;
82         }
83
84         /* Ensure this is marked as a 'real' vuid, not one
85          * simply valid for the session setup leg */
86         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
87         if (!NT_STATUS_IS_OK(status)) {
88                 goto failed;
89         }
90         req->session = smb_sess;
91
92 done:
93         io->smb2.out.uid = smb_sess->vuid;
94 failed:
95         req->status = auth_nt_status_squash(status);
96         smb2srv_sesssetup_send(req, io);
97         if (!NT_STATUS_IS_OK(status) && !
98             NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
99                 talloc_free(smb_sess);
100         }
101 }
102
103 static void smb2srv_sesssetup_backend(struct smb2srv_request *req, union smb_sesssetup *io)
104 {
105         NTSTATUS status;
106         struct smb2srv_sesssetup_callback_ctx *callback_ctx;
107         struct smbsrv_session *smb_sess = NULL;
108         uint64_t vuid;
109
110         io->smb2.out._pad       = 0;
111         io->smb2.out.uid        = 0;
112         io->smb2.out.secblob = data_blob(NULL, 0);
113
114         vuid = BVAL(req->in.hdr, SMB2_HDR_UID);
115
116         /*
117          * only when we got '0' we should allocate a new session
118          */
119         if (vuid == 0) {
120                 struct gensec_security *gensec_ctx;
121
122                 status = gensec_server_start(req,
123                                              req->smb_conn->connection->event.ctx,
124                                              req->smb_conn->connection->msg_ctx,
125                                              &gensec_ctx);
126                 if (!NT_STATUS_IS_OK(status)) {
127                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
128                         goto failed;
129                 }
130
131                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
132
133                 gensec_set_target_service(gensec_ctx, "cifs");
134
135                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
136
137                 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
138                 if (!NT_STATUS_IS_OK(status)) {
139                         DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
140                         goto failed;
141                 }
142
143                 /* allocate a new session */
144                 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
145                 if (!smb_sess) {
146                         status = NT_STATUS_INSUFFICIENT_RESOURCES;
147                         goto failed;
148                 }
149                 status = smbsrv_smb2_init_tcons(smb_sess);
150                 if (!NT_STATUS_IS_OK(status)) {
151                         goto failed;
152                 }
153         } else {
154                 /* lookup an existing session */
155                 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
156         }
157
158         if (!smb_sess) {
159                 status = NT_STATUS_USER_SESSION_DELETED;
160                 goto failed;
161         }
162
163         if (!smb_sess->gensec_ctx) {
164                 status = NT_STATUS_INTERNAL_ERROR;
165                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
166                 goto failed;
167         }
168
169         callback_ctx = talloc(req, struct smb2srv_sesssetup_callback_ctx);
170         if (!callback_ctx) goto nomem;
171         callback_ctx->req       = req;
172         callback_ctx->io        = io;
173         callback_ctx->smb_sess  = smb_sess;
174
175         gensec_update_send(smb_sess->gensec_ctx, io->smb2.in.secblob,
176                            smb2srv_sesssetup_callback, callback_ctx);
177         return;
178 nomem:
179         status = NT_STATUS_NO_MEMORY;
180 failed:
181         talloc_free(smb_sess);
182         req->status = auth_nt_status_squash(status);
183         smb2srv_sesssetup_send(req, io);
184 }
185
186 void smb2srv_sesssetup_recv(struct smb2srv_request *req)
187 {
188         union smb_sesssetup *io;
189
190         SMB2SRV_CHECK_BODY_SIZE(req, 0x18, true);
191         SMB2SRV_TALLOC_IO_PTR(io, union smb_sesssetup);
192
193         io->smb2.level          = RAW_SESSSETUP_SMB2;
194         io->smb2.in._pad        = SVAL(req->in.body, 0x02);
195         io->smb2.in.unknown2    = IVAL(req->in.body, 0x04);
196         io->smb2.in.unknown3    = IVAL(req->in.body, 0x08);
197         SMB2SRV_CHECK(smb2_pull_o16s16_blob(&req->in, io, req->in.body+0x0C, &io->smb2.in.secblob));
198         io->smb2.in.unknown4    = BVAL(req->in.body, 0x10);
199
200         smb2srv_sesssetup_backend(req, io);
201 }
202
203 static NTSTATUS smb2srv_logoff_backend(struct smb2srv_request *req)
204 {
205         /* TODO: call ntvfs backends to close file of this session */
206         talloc_free(req->session);
207         req->session = NULL;
208         return NT_STATUS_OK;
209 }
210
211 static void smb2srv_logoff_send(struct smb2srv_request *req)
212 {
213         if (NT_STATUS_IS_ERR(req->status)) {
214                 smb2srv_send_error(req, req->status);
215                 return;
216         }
217
218         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
219
220         SSVAL(req->out.body, 0x02, 0);
221
222         smb2srv_send_reply(req);
223 }
224
225 void smb2srv_logoff_recv(struct smb2srv_request *req)
226 {
227         uint16_t _pad;
228
229         SMB2SRV_CHECK_BODY_SIZE(req, 0x04, false);
230
231         _pad    = SVAL(req->in.body, 0x02);
232
233         req->status = smb2srv_logoff_backend(req);
234
235         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
236                 talloc_free(req);
237                 return;
238         }
239         smb2srv_logoff_send(req);
240 }