5e53e11634e11ddbec489bfd77509c1d109794e4
[ira/wip.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27
28 /*
29   initialise a smb2_session structure
30  */
31 struct smb2_tree *smb2_tree_init(struct smb2_session *session,
32                                  TALLOC_CTX *parent_ctx, BOOL primary)
33 {
34         struct smb2_tree *tree;
35
36         tree = talloc_zero(parent_ctx, struct smb2_tree);
37         if (!session) {
38                 return NULL;
39         }
40         if (primary) {
41                 tree->session = talloc_steal(tree, session);
42         } else {
43                 tree->session = talloc_reference(tree, session);
44         }
45         return tree;
46 }
47
48 /*
49   send a tree connect
50 */
51 struct smb2_request *smb2_tree_connect_send(struct smb2_tree *tree, 
52                                             struct smb2_tree_connect *io)
53 {
54         struct smb2_request *req;
55         NTSTATUS status;
56         DATA_BLOB path;
57
58         status = smb2_string_blob(tree, io->in.path, &path);
59         if (!NT_STATUS_IS_OK(status)) {
60                 return NULL;
61         }
62         
63         req = smb2_request_init(tree->session->transport, SMB2_OP_TCON, 
64                                 0x8 + path.length);
65         if (req == NULL) return NULL;
66
67         SBVAL(req->out.hdr,  SMB2_HDR_UID, tree->session->uid);
68         SIVAL(req->out.body, 0x00, io->in.unknown1);
69         status = smb2_push_ofs_blob(&req->out, req->out.body+0x04, path);
70         data_blob_free(&path);
71         if (!NT_STATUS_IS_OK(status)) {
72                 talloc_free(req);
73                 return NULL;
74         }
75
76         smb2_transport_send(req);
77
78         return req;
79 }
80
81
82 /*
83   recv a tree connect reply
84 */
85 NTSTATUS smb2_tree_connect_recv(struct smb2_request *req, struct smb2_tree_connect *io)
86 {
87         if (!smb2_request_receive(req) || 
88             smb2_request_is_error(req)) {
89                 return smb2_request_destroy(req);
90         }
91
92         if (req->in.body_size < 0x10) {
93                 return NT_STATUS_BUFFER_TOO_SMALL;
94         }
95
96         SMB2_CHECK_BUFFER_CODE(req, 0x10);
97
98         io->out.tid      = IVAL(req->in.hdr,  SMB2_HDR_TID);
99
100         io->out.unknown1    = SVAL(req->in.body, 0x02);
101         io->out.unknown2    = IVAL(req->in.body, 0x04);
102         io->out.unknown3    = IVAL(req->in.body, 0x08);
103         io->out.access_mask = IVAL(req->in.body, 0x0C);
104         
105         return smb2_request_destroy(req);
106 }
107
108 /*
109   sync tree connect request
110 */
111 NTSTATUS smb2_tree_connect(struct smb2_tree *tree, struct smb2_tree_connect *io)
112 {
113         struct smb2_request *req = smb2_tree_connect_send(tree, io);
114         return smb2_tree_connect_recv(req, io);
115 }