added server side SMB2 signing
[kai/samba.git] / source4 / 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 #include "param/param.h"
32
33 static void smb2srv_sesssetup_send(struct smb2srv_request *req, union smb_sesssetup *io)
34 {
35         uint16_t credit;
36
37         if (NT_STATUS_IS_OK(req->status)) {
38                 credit = 0x0003;
39         } else if (NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
40                 credit = 0x0002;
41         } else {
42                 smb2srv_send_error(req, req->status);
43                 return;
44         }
45
46         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, io->smb2.out.secblob.length));
47
48         SSVAL(req->out.hdr, SMB2_HDR_CREDIT,    credit);
49         SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID,        io->smb2.out.uid);
50
51         SSVAL(req->out.body, 0x02, io->smb2.out.session_flags);
52         SMB2SRV_CHECK(smb2_push_o16s16_blob(&req->out, 0x04, io->smb2.out.secblob));
53
54         smb2srv_send_reply(req);
55 }
56
57 struct smb2srv_sesssetup_callback_ctx {
58         struct smb2srv_request *req;
59         union smb_sesssetup *io;
60         struct smbsrv_session *smb_sess;
61 };
62
63 static void smb2srv_sesssetup_callback(struct gensec_update_request *greq, void *private_data)
64 {
65         struct smb2srv_sesssetup_callback_ctx *ctx = talloc_get_type(private_data,
66                                                      struct smb2srv_sesssetup_callback_ctx);
67         struct smb2srv_request *req = ctx->req;
68         union smb_sesssetup *io = ctx->io;
69         struct smbsrv_session *smb_sess = ctx->smb_sess;
70         struct auth_session_info *session_info = NULL;
71         NTSTATUS status;
72
73         status = gensec_update_recv(greq, req, &io->smb2.out.secblob);
74         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
75                 goto done;
76         } else if (!NT_STATUS_IS_OK(status)) {
77                 goto failed;
78         }
79
80         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
81         if (!NT_STATUS_IS_OK(status)) {
82                 goto failed;
83         }
84
85         /* Ensure this is marked as a 'real' vuid, not one
86          * simply valid for the session setup leg */
87         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
88         if (!NT_STATUS_IS_OK(status)) {
89                 goto failed;
90         }
91         req->session = smb_sess;
92
93 done:
94         io->smb2.out.uid = smb_sess->vuid;
95 failed:
96         req->status = auth_nt_status_squash(status);
97         smb2srv_sesssetup_send(req, io);
98         if (!NT_STATUS_IS_OK(status) && !
99             NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
100                 talloc_free(smb_sess);
101         }
102 }
103
104 static void smb2srv_sesssetup_backend(struct smb2srv_request *req, union smb_sesssetup *io)
105 {
106         NTSTATUS status;
107         struct smb2srv_sesssetup_callback_ctx *callback_ctx;
108         struct smbsrv_session *smb_sess = NULL;
109         uint64_t vuid;
110
111         io->smb2.out.session_flags = 0;
112         io->smb2.out.uid        = 0;
113         io->smb2.out.secblob = data_blob(NULL, 0);
114
115         vuid = BVAL(req->in.hdr, SMB2_HDR_SESSION_ID);
116
117         /*
118          * only when we got '0' we should allocate a new session
119          */
120         if (vuid == 0) {
121                 struct gensec_security *gensec_ctx;
122
123                 status = gensec_server_start(req,
124                                              req->smb_conn->connection->event.ctx,
125                                              req->smb_conn->lp_ctx,
126                                              req->smb_conn->connection->msg_ctx,
127                                              &gensec_ctx);
128                 if (!NT_STATUS_IS_OK(status)) {
129                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
130                         goto failed;
131                 }
132
133                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
134
135                 gensec_set_target_service(gensec_ctx, "cifs");
136
137                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
138
139                 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
140                 if (!NT_STATUS_IS_OK(status)) {
141                         DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
142                         goto failed;
143                 }
144
145                 /* allocate a new session */
146                 smb_sess = smbsrv_session_new(req->smb_conn, req->smb_conn, gensec_ctx);
147                 if (!smb_sess) {
148                         status = NT_STATUS_INSUFFICIENT_RESOURCES;
149                         goto failed;
150                 }
151                 status = smbsrv_smb2_init_tcons(smb_sess);
152                 if (!NT_STATUS_IS_OK(status)) {
153                         goto failed;
154                 }
155         } else {
156                 /* lookup an existing session */
157                 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
158         }
159
160         if (!smb_sess) {
161                 /* see WSPP test suite - test 11 */
162                 status = NT_STATUS_REQUEST_NOT_ACCEPTED;
163                 goto failed;
164         }
165
166         if (!smb_sess->gensec_ctx) {
167                 status = NT_STATUS_INTERNAL_ERROR;
168                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
169                 goto failed;
170         }
171
172         callback_ctx = talloc(req, struct smb2srv_sesssetup_callback_ctx);
173         if (!callback_ctx) goto nomem;
174         callback_ctx->req       = req;
175         callback_ctx->io        = io;
176         callback_ctx->smb_sess  = smb_sess;
177
178         gensec_update_send(smb_sess->gensec_ctx, io->smb2.in.secblob,
179                            smb2srv_sesssetup_callback, callback_ctx);
180
181         /* note that we ignore SMB2_NEGOTIATE_SIGNING_ENABLED from the client.
182            This is deliberate as windows does not set it even when it does 
183            set SMB2_NEGOTIATE_SIGNING_REQUIRED */
184         if ((io->smb2.in.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
185             lp_server_signing(req->smb_conn->lp_ctx) == SMB_SIGNING_REQUIRED) {
186                 req->smb_conn->doing_signing = true;
187         }
188
189         return;
190 nomem:
191         status = NT_STATUS_NO_MEMORY;
192 failed:
193         talloc_free(smb_sess);
194         req->status = auth_nt_status_squash(status);
195         smb2srv_sesssetup_send(req, io);
196 }
197
198 void smb2srv_sesssetup_recv(struct smb2srv_request *req)
199 {
200         union smb_sesssetup *io;
201
202         SMB2SRV_CHECK_BODY_SIZE(req, 0x18, true);
203         SMB2SRV_TALLOC_IO_PTR(io, union smb_sesssetup);
204
205         io->smb2.level                 = RAW_SESSSETUP_SMB2;
206         io->smb2.in.vc_number          = CVAL(req->in.body, 0x02);
207         io->smb2.in.security_mode      = CVAL(req->in.body, 0x03);
208         io->smb2.in.capabilities       = IVAL(req->in.body, 0x04);
209         io->smb2.in.channel            = IVAL(req->in.body, 0x08);
210         io->smb2.in.previous_sessionid = BVAL(req->in.body, 0x10);
211         SMB2SRV_CHECK(smb2_pull_o16s16_blob(&req->in, io, req->in.body+0x0C, &io->smb2.in.secblob));
212
213         smb2srv_sesssetup_backend(req, io);
214 }
215
216 static NTSTATUS smb2srv_logoff_backend(struct smb2srv_request *req)
217 {
218         /* TODO: call ntvfs backends to close file of this session */
219         talloc_free(req->session);
220         req->session = NULL;
221         return NT_STATUS_OK;
222 }
223
224 static void smb2srv_logoff_send(struct smb2srv_request *req)
225 {
226         if (NT_STATUS_IS_ERR(req->status)) {
227                 smb2srv_send_error(req, req->status);
228                 return;
229         }
230
231         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, false, 0));
232
233         SSVAL(req->out.body, 0x02, 0);
234
235         smb2srv_send_reply(req);
236 }
237
238 void smb2srv_logoff_recv(struct smb2srv_request *req)
239 {
240         uint16_t _pad;
241
242         SMB2SRV_CHECK_BODY_SIZE(req, 0x04, false);
243
244         _pad    = SVAL(req->in.body, 0x02);
245
246         req->status = smb2srv_logoff_backend(req);
247
248         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
249                 talloc_free(req);
250                 return;
251         }
252         smb2srv_logoff_send(req);
253 }