2615eed6833a701d13c28f662a904fa2b0fb7f63
[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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "auth/auth.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "smb_server/smb_server.h"
27 #include "smb_server/service_smb_proto.h"
28 #include "smb_server/smb2/smb2_server.h"
29 #include "smbd/service_stream.h"
30
31 static void smb2srv_sesssetup_send(struct smb2srv_request *req, union smb_sesssetup *io)
32 {
33         uint16_t unknown1;
34
35         if (NT_STATUS_IS_OK(req->status)) {
36                 unknown1 = 0x0003;
37         } else if (NT_STATUS_EQUAL(req->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
38                 unknown1 = 0x0002;
39         } else {
40                 smb2srv_send_error(req, req->status);
41                 return;
42         }
43
44         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, True, io->smb2.out.secblob.length));
45
46         SSVAL(req->out.hdr, SMB2_HDR_UNKNOWN1,  unknown1);
47         SBVAL(req->out.hdr, SMB2_HDR_UID,       io->smb2.out.uid);
48
49         SSVAL(req->out.body, 0x02, io->smb2.out._pad);
50         SMB2SRV_CHECK(smb2_push_o16s16_blob(&req->out, 0x04, io->smb2.out.secblob));
51
52         smb2srv_send_reply(req);
53 }
54
55 struct smb2srv_sesssetup_callback_ctx {
56         struct smb2srv_request *req;
57         union smb_sesssetup *io;
58         struct smbsrv_session *smb_sess;
59 };
60
61 static void smb2srv_sesssetup_callback(struct gensec_update_request *greq, void *private_data)
62 {
63         struct smb2srv_sesssetup_callback_ctx *ctx = talloc_get_type(private_data,
64                                                      struct smb2srv_sesssetup_callback_ctx);
65         struct smb2srv_request *req = ctx->req;
66         union smb_sesssetup *io = ctx->io;
67         struct smbsrv_session *smb_sess = ctx->smb_sess;
68         struct auth_session_info *session_info = NULL;
69         NTSTATUS status;
70
71         status = gensec_update_recv(greq, req, &io->smb2.out.secblob);
72         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
73                 goto done;
74         } else if (!NT_STATUS_IS_OK(status)) {
75                 goto failed;
76         }
77
78         status = gensec_session_info(smb_sess->gensec_ctx, &session_info);
79         if (!NT_STATUS_IS_OK(status)) {
80                 goto failed;
81         }
82
83         /* Ensure this is marked as a 'real' vuid, not one
84          * simply valid for the session setup leg */
85         status = smbsrv_session_sesssetup_finished(smb_sess, session_info);
86         if (!NT_STATUS_IS_OK(status)) {
87                 goto failed;
88         }
89         req->session = smb_sess;
90
91 done:
92         io->smb2.out.uid = smb_sess->vuid;
93 failed:
94         req->status = auth_nt_status_squash(status);
95         smb2srv_sesssetup_send(req, io);
96 }
97
98 static void smb2srv_sesssetup_backend(struct smb2srv_request *req, union smb_sesssetup *io)
99 {
100         NTSTATUS status;
101         struct smb2srv_sesssetup_callback_ctx *callback_ctx;
102         struct smbsrv_session *smb_sess = NULL;
103         uint64_t vuid;
104
105         io->smb2.out._pad       = 0;
106         io->smb2.out.uid        = 0;
107         io->smb2.out.secblob = data_blob(NULL, 0);
108
109         vuid = BVAL(req->in.hdr, SMB2_HDR_UID);
110
111         /*
112          * only when we got '0' we should allocate a new session
113          */
114         if (vuid == 0) {
115                 struct gensec_security *gensec_ctx;
116
117                 status = gensec_server_start(req,
118                                              req->smb_conn->connection->event.ctx,
119                                              req->smb_conn->connection->msg_ctx,
120                                              &gensec_ctx);
121                 if (!NT_STATUS_IS_OK(status)) {
122                         DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
123                         goto failed;
124                 }
125
126                 gensec_set_credentials(gensec_ctx, req->smb_conn->negotiate.server_credentials);
127
128                 gensec_set_target_service(gensec_ctx, "cifs");
129
130                 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
131
132                 status = gensec_start_mech_by_oid(gensec_ctx, GENSEC_OID_SPNEGO);
133                 if (!NT_STATUS_IS_OK(status)) {
134                         DEBUG(1, ("Failed to start GENSEC SPNEGO server code: %s\n", nt_errstr(status)));
135                         goto failed;
136                 }
137
138                 /* allocate a new session */
139                 smb_sess = smbsrv_session_new(req->smb_conn, gensec_ctx);
140                 if (!smb_sess) {
141                         status = NT_STATUS_INSUFFICIENT_RESOURCES;
142                         goto failed;
143                 }
144                 status = smbsrv_smb2_init_tcons(smb_sess);
145                 if (!NT_STATUS_IS_OK(status)) {
146                         goto failed;
147                 }
148         } else {
149                 /* lookup an existing session */
150                 smb_sess = smbsrv_session_find_sesssetup(req->smb_conn, vuid);
151         }
152
153         if (!smb_sess) {
154                 status = NT_STATUS_USER_SESSION_DELETED;
155                 goto failed;
156         }
157
158         if (!smb_sess->gensec_ctx) {
159                 status = NT_STATUS_INTERNAL_ERROR;
160                 DEBUG(1, ("Internal ERROR: no gensec_ctx on session: %s\n", nt_errstr(status)));
161                 goto failed;
162         }
163
164         callback_ctx = talloc(req, struct smb2srv_sesssetup_callback_ctx);
165         if (!callback_ctx) goto nomem;
166         callback_ctx->req       = req;
167         callback_ctx->io        = io;
168         callback_ctx->smb_sess  = smb_sess;
169
170         gensec_update_send(smb_sess->gensec_ctx, io->smb2.in.secblob,
171                            smb2srv_sesssetup_callback, callback_ctx);
172         return;
173 nomem:
174         status = NT_STATUS_NO_MEMORY;
175 failed:
176         talloc_free(smb_sess);
177         req->status = auth_nt_status_squash(status);
178         smb2srv_sesssetup_send(req, io);
179 }
180
181 void smb2srv_sesssetup_recv(struct smb2srv_request *req)
182 {
183         union smb_sesssetup *io;
184
185         SMB2SRV_CHECK_BODY_SIZE(req, 0x18, True);
186         SMB2SRV_TALLOC_IO_PTR(io, union smb_sesssetup);
187
188         io->smb2.level          = RAW_SESSSETUP_SMB2;
189         io->smb2.in._pad        = SVAL(req->in.body, 0x02);
190         io->smb2.in.unknown2    = IVAL(req->in.body, 0x04);
191         io->smb2.in.unknown3    = IVAL(req->in.body, 0x08);
192         SMB2SRV_CHECK(smb2_pull_o16s16_blob(&req->in, io, req->in.body+0x0C, &io->smb2.in.secblob));
193         io->smb2.in.unknown4    = BVAL(req->in.body, 0x10);
194
195         smb2srv_sesssetup_backend(req, io);
196 }
197
198 static NTSTATUS smb2srv_logoff_backend(struct smb2srv_request *req)
199 {
200         /* TODO: call ntvfs backends to close file of this session */
201         talloc_free(req->session);
202         req->session = NULL;
203         return NT_STATUS_OK;
204 }
205
206 static void smb2srv_logoff_send(struct smb2srv_request *req)
207 {
208         if (NT_STATUS_IS_ERR(req->status)) {
209                 smb2srv_send_error(req, req->status);
210                 return;
211         }
212
213         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x04, False, 0));
214
215         SSVAL(req->out.body, 0x02, 0);
216
217         smb2srv_send_reply(req);
218 }
219
220 void smb2srv_logoff_recv(struct smb2srv_request *req)
221 {
222         uint16_t _pad;
223
224         SMB2SRV_CHECK_BODY_SIZE(req, 0x04, False);
225
226         _pad    = SVAL(req->in.body, 0x02);
227
228         req->status = smb2srv_logoff_backend(req);
229
230         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
231                 talloc_free(req);
232                 return;
233         }
234         smb2srv_logoff_send(req);
235 }