r23792: convert Samba4 to GPLv3
[gd/samba-autobuild/.git] / source4 / libcli / smb2 / tcon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client tree 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 "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25
26 /*
27   initialise a smb2_session structure
28  */
29 struct smb2_tree *smb2_tree_init(struct smb2_session *session,
30                                  TALLOC_CTX *parent_ctx, BOOL primary)
31 {
32         struct smb2_tree *tree;
33
34         tree = talloc_zero(parent_ctx, struct smb2_tree);
35         if (!session) {
36                 return NULL;
37         }
38         if (primary) {
39                 tree->session = talloc_steal(tree, session);
40         } else {
41                 tree->session = talloc_reference(tree, session);
42         }
43         return tree;
44 }
45
46 /*
47   send a tree connect
48 */
49 struct smb2_request *smb2_tree_connect_send(struct smb2_tree *tree, 
50                                             struct smb2_tree_connect *io)
51 {
52         struct smb2_request *req;
53         NTSTATUS status;
54
55         req = smb2_request_init(tree->session->transport, SMB2_OP_TCON, 
56                                 0x08, True, 0);
57         if (req == NULL) return NULL;
58
59         SBVAL(req->out.hdr,  SMB2_HDR_UID, tree->session->uid);
60
61         SSVAL(req->out.body, 0x02, io->in.unknown1);
62         status = smb2_push_o16s16_string(&req->out, 0x04, io->in.path);
63         if (!NT_STATUS_IS_OK(status)) {
64                 talloc_free(req);
65                 return NULL;
66         }
67
68         smb2_transport_send(req);
69
70         return req;
71 }
72
73
74 /*
75   recv a tree connect reply
76 */
77 NTSTATUS smb2_tree_connect_recv(struct smb2_request *req, struct smb2_tree_connect *io)
78 {
79         if (!smb2_request_receive(req) || 
80             smb2_request_is_error(req)) {
81                 return smb2_request_destroy(req);
82         }
83
84         SMB2_CHECK_PACKET_RECV(req, 0x10, False);
85
86         io->out.tid      = IVAL(req->in.hdr,  SMB2_HDR_TID);
87
88         io->out.unknown1    = SVAL(req->in.body, 0x02);
89         io->out.unknown2    = IVAL(req->in.body, 0x04);
90         io->out.unknown3    = IVAL(req->in.body, 0x08);
91         io->out.access_mask = IVAL(req->in.body, 0x0C);
92         
93         return smb2_request_destroy(req);
94 }
95
96 /*
97   sync tree connect request
98 */
99 NTSTATUS smb2_tree_connect(struct smb2_tree *tree, struct smb2_tree_connect *io)
100 {
101         struct smb2_request *req = smb2_tree_connect_send(tree, io);
102         return smb2_tree_connect_recv(req, io);
103 }