r25035: Fix some more warnings, use service pointer rather than service number in...
[kai/samba-autobuild/.git] / source4 / smb_server / smb2 / tcon.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Stefan Metzmacher      2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libcli/smb2/smb2.h"
22 #include "libcli/smb2/smb2_calls.h"
23 #include "smb_server/smb_server.h"
24 #include "smb_server/service_smb_proto.h"
25 #include "smb_server/smb2/smb2_server.h"
26 #include "librpc/gen_ndr/security.h"
27 #include "smbd/service_stream.h"
28 #include "ntvfs/ntvfs.h"
29
30 /*
31   send an oplock break request to a client
32 */
33 static NTSTATUS smb2srv_send_oplock_break(void *p, struct ntvfs_handle *h, uint8_t level)
34 {
35         struct smbsrv_handle *handle = talloc_get_type(h->frontend_data.private_data,
36                                                        struct smbsrv_handle);
37         struct smb2srv_request *req;
38         NTSTATUS status;
39
40         /* setup a dummy request structure */
41         req = smb2srv_init_request(handle->tcon->smb_conn);
42         NT_STATUS_HAVE_NO_MEMORY(req);
43
44         req->in.buffer          = talloc_array(req, uint8_t, 
45                                                NBT_HDR_SIZE + SMB2_MIN_SIZE);
46         NT_STATUS_HAVE_NO_MEMORY(req->in.buffer);
47         req->in.size            = NBT_HDR_SIZE + SMB2_MIN_SIZE;
48         req->in.allocated       = req->in.size;
49
50         req->in.hdr             = req->in.buffer+ NBT_HDR_SIZE;
51         req->in.body            = req->in.hdr   + SMB2_HDR_BODY;
52         req->in.body_size       = req->in.size  - (SMB2_HDR_BODY+NBT_HDR_SIZE);
53         req->in.dynamic         = NULL;
54
55         req->seqnum             = UINT64_MAX;
56
57         SIVAL(req->in.hdr, 0,                           SMB2_MAGIC);
58         SSVAL(req->in.hdr, SMB2_HDR_LENGTH,             SMB2_HDR_BODY);
59         SSVAL(req->in.hdr, SMB2_HDR_PAD1,               0);
60         SIVAL(req->in.hdr, SMB2_HDR_STATUS,             0);
61         SSVAL(req->in.hdr, SMB2_HDR_OPCODE,             SMB2_OP_BREAK);
62         SSVAL(req->in.hdr, SMB2_HDR_UNKNOWN1,           0);
63         SIVAL(req->in.hdr, SMB2_HDR_FLAGS,              0);
64         SIVAL(req->in.hdr, SMB2_HDR_CHAIN_OFFSET,       0);
65         SBVAL(req->in.hdr, SMB2_HDR_SEQNUM,             0);
66         SIVAL(req->in.hdr, SMB2_HDR_PID,                0);
67         SIVAL(req->in.hdr, SMB2_HDR_TID,                0);
68         SBVAL(req->in.hdr, SMB2_HDR_UID,                0);
69         memset(req->in.hdr+SMB2_HDR_SIG, 0, 16);
70
71         SSVAL(req->in.body, 0, 2);
72
73         status = smb2srv_setup_reply(req, 0x18, False, 0);
74         NT_STATUS_NOT_OK_RETURN(status);
75
76         SSVAL(req->out.hdr, SMB2_HDR_UNKNOWN1,  0x0000);
77
78         SSVAL(req->out.body, 0x02, 0x0001);
79         SIVAL(req->out.body, 0x04, 0x00000000);
80         smb2srv_push_handle(req->out.body, 0x08, h);
81
82         smb2srv_send_reply(req);
83
84         return NT_STATUS_OK;
85 }
86
87 struct ntvfs_handle *smb2srv_pull_handle(struct smb2srv_request *req, const uint8_t *base, uint_t offset)
88 {
89         struct smbsrv_tcon *tcon;
90         struct smbsrv_handle *handle;
91         uint32_t hid;
92         uint32_t tid;
93         uint64_t uid;
94
95         /*
96          * if there're chained requests used the cached handle
97          *
98          * TODO: check if this also correct when the given handle
99          *       isn't all 0xFF.
100          */
101         if (req->chained_file_handle) {
102                 base = req->chained_file_handle;
103                 offset = 0;
104         }
105
106         hid = IVAL(base, offset);
107         tid = IVAL(base, offset + 4);
108         uid = BVAL(base, offset + 8);
109
110         /* if it's the wildcard handle, don't waste time to search it... */
111         if (hid == UINT32_MAX && tid == UINT32_MAX && uid == UINT64_MAX) {
112                 return NULL;
113         }
114
115         /*
116          * if the (v)uid part doesn't match the given session the handle isn't
117          * valid
118          */
119         if (uid != req->session->vuid) {
120                 return NULL;
121         }
122
123         /*
124          * the handle can belong to a different tcon
125          * as that TID in the SMB2 header says, but
126          * the request should succeed nevertheless!
127          *
128          * because of this we put the 32 bit TID into the
129          * 128 bit handle, so that we can extract the tcon from the
130          * handle
131          */
132         tcon = req->tcon;
133         if (tid != req->tcon->tid) {
134                 tcon = smbsrv_smb2_tcon_find(req->session, tid, req->request_time);
135                 if (!tcon) {
136                         return NULL;
137                 }
138         }
139
140         handle = smbsrv_smb2_handle_find(tcon, hid, req->request_time);
141         if (!handle) {
142                 return NULL;
143         }
144
145         /*
146          * as the smb2srv_tcon is a child object of the smb2srv_session
147          * the handle belongs to the correct session!
148          *
149          * Note: no check is needed here for SMB2
150          */
151
152         /*
153          * as the handle may have overwritten the tcon
154          * we need to set it on the request so that the
155          * correct ntvfs context will be used for the ntvfs_*() request
156          *
157          * TODO: check if that's correct for chained requests as well!
158          */
159         req->tcon = tcon;
160         return handle->ntvfs;
161 }
162
163 void smb2srv_push_handle(uint8_t *base, uint_t offset, struct ntvfs_handle *ntvfs)
164 {
165         struct smbsrv_handle *handle = talloc_get_type(ntvfs->frontend_data.private_data,
166                                        struct smbsrv_handle);
167
168         /* 
169          * the handle is 128 bit on the wire
170          */
171         SIVAL(base, offset,     handle->hid);
172         SIVAL(base, offset + 4, handle->tcon->tid);
173         SBVAL(base, offset + 8, handle->session->vuid);
174 }
175
176 static NTSTATUS smb2srv_handle_create_new(void *private_data, struct ntvfs_request *ntvfs, struct ntvfs_handle **_h)
177 {
178         struct smb2srv_request *req = talloc_get_type(ntvfs->frontend_data.private_data,
179                                       struct smb2srv_request);
180         struct smbsrv_handle *handle;
181         struct ntvfs_handle *h;
182
183         handle = smbsrv_handle_new(req->session, req->tcon, req, req->request_time);
184         if (!handle) return NT_STATUS_INSUFFICIENT_RESOURCES;
185
186         h = talloc_zero(handle, struct ntvfs_handle);
187         if (!h) goto nomem;
188
189         /* 
190          * note: we don't set handle->ntvfs yet,
191          *       this will be done by smbsrv_handle_make_valid()
192          *       this makes sure the handle is invalid for clients
193          *       until the ntvfs subsystem has made it valid
194          */
195         h->ctx          = ntvfs->ctx;
196         h->session_info = ntvfs->session_info;
197         h->smbpid       = ntvfs->smbpid;
198
199         h->frontend_data.private_data = handle;
200
201         *_h = h;
202         return NT_STATUS_OK;
203 nomem:
204         talloc_free(handle);
205         return NT_STATUS_NO_MEMORY;
206 }
207
208 static NTSTATUS smb2srv_handle_make_valid(void *private_data, struct ntvfs_handle *h)
209 {
210         struct smbsrv_tcon *tcon = talloc_get_type(private_data, struct smbsrv_tcon);
211         struct smbsrv_handle *handle = talloc_get_type(h->frontend_data.private_data,
212                                                        struct smbsrv_handle);
213         /* this tells the frontend that the handle is valid */
214         handle->ntvfs = h;
215         /* this moves the smbsrv_request to the smbsrv_tcon memory context */
216         talloc_steal(tcon, handle);
217         return NT_STATUS_OK;
218 }
219
220 static void smb2srv_handle_destroy(void *private_data, struct ntvfs_handle *h)
221 {
222         struct smbsrv_handle *handle = talloc_get_type(h->frontend_data.private_data,
223                                                        struct smbsrv_handle);
224         talloc_free(handle);
225 }
226
227 static struct ntvfs_handle *smb2srv_handle_search_by_wire_key(void *private_data, struct ntvfs_request *ntvfs, const DATA_BLOB *key)
228 {
229         return NULL;
230 }
231
232 static DATA_BLOB smb2srv_handle_get_wire_key(void *private_data, struct ntvfs_handle *handle, TALLOC_CTX *mem_ctx)
233 {
234         return data_blob(NULL, 0);
235 }
236
237 static NTSTATUS smb2srv_tcon_backend(struct smb2srv_request *req, union smb_tcon *io)
238 {
239         struct smbsrv_tcon *tcon;
240         NTSTATUS status;
241         enum ntvfs_type type;
242         uint16_t type_smb2;
243         uint32_t unknown2;
244         const char *service = io->smb2.in.path;
245         struct share_config *scfg;
246         const char *sharetype;
247
248         if (strncmp(service, "\\\\", 2) == 0) {
249                 const char *p = strchr(service+2, '\\');
250                 if (p) {
251                         service = p + 1;
252                 }
253         }
254
255         status = share_get_config(req, req->smb_conn->share_context, service, &scfg);
256         if (!NT_STATUS_IS_OK(status)) {
257                 DEBUG(0,("smb2srv_tcon_backend: couldn't find service %s\n", service));
258                 return NT_STATUS_BAD_NETWORK_NAME;
259         }
260
261         if (!socket_check_access(req->smb_conn->connection->socket, 
262                                  scfg->name, 
263                                  share_string_list_option(req, scfg, SHARE_HOSTS_ALLOW), 
264                                  share_string_list_option(req, scfg, SHARE_HOSTS_DENY))) {
265                 return NT_STATUS_ACCESS_DENIED;
266         }
267
268         /* work out what sort of connection this is */
269         sharetype = share_string_option(scfg, SHARE_TYPE, "DISK");
270         if (sharetype && strcmp(sharetype, "IPC") == 0) {
271                 type = NTVFS_IPC;
272                 type_smb2 = 0x0002;
273                 unknown2 = 0x00000030;
274         } else if (sharetype && strcmp(sharetype, "PRINTER") == 0) {
275                 type = NTVFS_PRINT;
276                 type_smb2 = 0x0003;
277                 unknown2 = 0x00000000;
278         } else {
279                 type = NTVFS_DISK;
280                 type_smb2 = 0x0001;
281                 unknown2 = 0x00000800;
282         }
283
284         tcon = smbsrv_smb2_tcon_new(req->session, scfg->name);
285         if (!tcon) {
286                 DEBUG(0,("smb2srv_tcon_backend: Couldn't find free connection.\n"));
287                 return NT_STATUS_INSUFFICIENT_RESOURCES;
288         }
289         req->tcon = tcon;
290
291         /* init ntvfs function pointers */
292         status = ntvfs_init_connection(tcon, scfg, type,
293                                        req->smb_conn->negotiate.protocol,
294                                        req->smb_conn->connection->event.ctx,
295                                        req->smb_conn->connection->msg_ctx,
296                                        req->smb_conn->connection->server_id,
297                                        &tcon->ntvfs);
298         if (!NT_STATUS_IS_OK(status)) {
299                 DEBUG(0, ("smb2srv_tcon_backend: ntvfs_init_connection failed for service %s\n", 
300                           scfg->name));
301                 goto failed;
302         }
303
304         status = ntvfs_set_oplock_handler(tcon->ntvfs, smb2srv_send_oplock_break, tcon);
305         if (!NT_STATUS_IS_OK(status)) {
306                 DEBUG(0,("smb2srv_tcon_backend: NTVFS failed to set the oplock handler!\n"));
307                 goto failed;
308         }
309
310         status = ntvfs_set_addr_callbacks(tcon->ntvfs, smbsrv_get_my_addr, smbsrv_get_peer_addr, req->smb_conn);
311         if (!NT_STATUS_IS_OK(status)) {
312                 DEBUG(0,("smb2srv_tcon_backend: NTVFS failed to set the addr callbacks!\n"));
313                 goto failed;
314         }
315
316         status = ntvfs_set_handle_callbacks(tcon->ntvfs,
317                                             smb2srv_handle_create_new,
318                                             smb2srv_handle_make_valid,
319                                             smb2srv_handle_destroy,
320                                             smb2srv_handle_search_by_wire_key,
321                                             smb2srv_handle_get_wire_key,
322                                             tcon);
323         if (!NT_STATUS_IS_OK(status)) {
324                 DEBUG(0,("smb2srv_tcon_backend: NTVFS failed to set the handle callbacks!\n"));
325                 goto failed;
326         }
327
328         req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req,
329                                           req->session->session_info,
330                                           0, /* TODO: fill in PID */
331                                           req->request_time,
332                                           req, NULL, 0);
333         if (!req->ntvfs) {
334                 status = NT_STATUS_NO_MEMORY;
335                 goto failed;
336         }
337
338         /* Invoke NTVFS connection hook */
339         status = ntvfs_connect(req->ntvfs, scfg->name);
340         if (!NT_STATUS_IS_OK(status)) {
341                 DEBUG(0,("smb2srv_tcon_backend: NTVFS ntvfs_connect() failed!\n"));
342                 goto failed;
343         }
344
345         io->smb2.out.unknown1   = type_smb2; /* 1 - DISK, 2 - Print, 3 - IPC */
346         io->smb2.out.unknown2   = unknown2;
347         io->smb2.out.unknown3   = 0x00000000;
348         io->smb2.out.access_mask= SEC_RIGHTS_FILE_ALL;
349
350         io->smb2.out.tid        = tcon->tid;
351
352         return NT_STATUS_OK;
353
354 failed:
355         req->tcon = NULL;
356         talloc_free(tcon);
357         return status;
358 }
359
360 static void smb2srv_tcon_send(struct smb2srv_request *req, union smb_tcon *io)
361 {
362         uint16_t unknown1;
363
364         if (!NT_STATUS_IS_OK(req->status)) {
365                 smb2srv_send_error(req, req->status);
366                 return;
367         }
368         if (io->smb2.out.unknown1 == 0x0002) {
369                 /* if it's an IPC share vista returns 0x0005 */
370                 unknown1 = 0x0005;
371         } else {
372                 unknown1 = 0x0001;
373         }
374
375         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x10, False, 0));
376
377         SIVAL(req->out.hdr,     SMB2_HDR_TID,   io->smb2.out.tid);
378         SSVAL(req->out.hdr,     SMB2_HDR_UNKNOWN1,unknown1);
379
380         SSVAL(req->out.body,    0x02,           io->smb2.out.unknown1);
381         SIVAL(req->out.body,    0x04,           io->smb2.out.unknown2);
382         SIVAL(req->out.body,    0x08,           io->smb2.out.unknown3);
383         SIVAL(req->out.body,    0x0C,           io->smb2.out.access_mask);
384
385         smb2srv_send_reply(req);
386 }
387
388 void smb2srv_tcon_recv(struct smb2srv_request *req)
389 {
390         union smb_tcon *io;
391
392         SMB2SRV_CHECK_BODY_SIZE(req, 0x08, True);
393         SMB2SRV_TALLOC_IO_PTR(io, union smb_tcon);
394
395         io->smb2.level          = RAW_TCON_SMB2;
396         io->smb2.in.unknown1    = SVAL(req->in.body, 0x02);
397         SMB2SRV_CHECK(smb2_pull_o16s16_string(&req->in, io, req->in.body+0x04, &io->smb2.in.path));
398
399         req->status = smb2srv_tcon_backend(req, io);
400
401         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
402                 talloc_free(req);
403                 return;
404         }
405         smb2srv_tcon_send(req, io);
406 }
407
408 static NTSTATUS smb2srv_tdis_backend(struct smb2srv_request *req)
409 {
410         /* TODO: call ntvfs backends to close file of this tcon */
411         talloc_free(req->tcon);
412         req->tcon = NULL;
413         return NT_STATUS_OK;
414 }
415
416 static void smb2srv_tdis_send(struct smb2srv_request *req)
417 {
418         NTSTATUS status;
419
420         if (NT_STATUS_IS_ERR(req->status)) {
421                 smb2srv_send_error(req, req->status);
422                 return;
423         }
424
425         status = smb2srv_setup_reply(req, 0x04, False, 0);
426         if (!NT_STATUS_IS_OK(status)) {
427                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
428                 talloc_free(req);
429                 return;
430         }
431
432         SSVAL(req->out.body, 0x02, 0);
433
434         smb2srv_send_reply(req);
435 }
436
437 void smb2srv_tdis_recv(struct smb2srv_request *req)
438 {
439         uint16_t _pad;
440
441         SMB2SRV_CHECK_BODY_SIZE(req, 0x04, False);
442
443         _pad    = SVAL(req->in.body, 0x02);
444
445         req->status = smb2srv_tdis_backend(req);
446
447         if (req->control_flags & SMB2SRV_REQ_CTRL_FLAG_NOT_REPLY) {
448                 talloc_free(req);
449                 return;
450         }
451         smb2srv_tdis_send(req);
452 }