first public release of samba4 code
[kai/samba.git] / source4 / libcli / raw / clitree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client tree context management functions
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
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
24 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
25         req = cli_request_setup(tree, cmd, wct, buflen); \
26         if (!req) return NULL; \
27 } while (0)
28
29
30 /****************************************************************************
31  Initialize the tree context
32 ****************************************************************************/
33 struct cli_tree *cli_tree_init(struct cli_session *session)
34 {
35         struct cli_tree *tree;
36         TALLOC_CTX *mem_ctx = talloc_init("cli_tree");
37         if (mem_ctx == NULL) {
38                 return NULL;
39         }
40
41         tree = talloc_zero(mem_ctx, sizeof(*tree));
42         if (!tree) {
43                 talloc_destroy(mem_ctx);
44                 return NULL;
45         }
46
47         tree->mem_ctx = mem_ctx;
48         tree->session = session;
49         tree->session->reference_count++;
50
51         return tree;
52 }
53
54 /****************************************************************************
55 reduce reference count on a tree and destroy if <= 0
56 ****************************************************************************/
57 void cli_tree_close(struct cli_tree *tree)
58 {
59         if (!tree) return;
60         tree->reference_count--;
61         if (tree->reference_count <= 0) {
62                 cli_session_close(tree->session);
63                 talloc_destroy(tree->mem_ctx);
64         }
65 }
66
67
68 /****************************************************************************
69  Send a tconX (async send)
70 ****************************************************************************/
71 struct cli_request *smb_tree_connect_send(struct cli_tree *tree, union smb_tcon *parms)
72 {
73         struct cli_request *req;
74
75         switch (parms->tcon.level) {
76         case RAW_TCON_TCON:
77                 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
78                 cli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
79                 cli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
80                 cli_req_append_ascii4(req, parms->tcon.in.dev,     STR_ASCII);
81                 break;
82
83         case RAW_TCON_TCONX:
84                 SETUP_REQUEST_TREE(SMBtconX, 4, 0);
85                 SSVAL(req->out.vwv, VWV(0), 0xFF);
86                 SSVAL(req->out.vwv, VWV(1), 0);
87                 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags);
88                 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length);
89                 cli_req_append_blob(req, &parms->tconx.in.password);
90                 cli_req_append_string(req, parms->tconx.in.path,   STR_TERMINATE | STR_UPPER);
91                 cli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
92                 break;
93         }
94
95         if (!cli_request_send(req)) {
96                 cli_request_destroy(req);
97                 return NULL;
98         }
99
100         return req;
101 }
102
103 /****************************************************************************
104  Send a tconX (async recv)
105 ****************************************************************************/
106 NTSTATUS smb_tree_connect_recv(struct cli_request *req, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
107 {
108         char *p;
109
110         if (!cli_request_receive(req) ||
111             cli_request_is_error(req)) {
112                 goto failed;
113         }
114
115         switch (parms->tcon.level) {
116         case RAW_TCON_TCON:
117                 CLI_CHECK_WCT(req, 2);
118                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
119                 parms->tcon.out.cnum = SVAL(req->in.vwv, VWV(1));
120                 break;
121
122         case RAW_TCON_TCONX:
123                 ZERO_STRUCT(parms->tconx.out);
124                 CLI_CHECK_MIN_WCT(req, 0);  /* this depends on the protocol level */
125                 parms->tconx.out.cnum = SVAL(req->in.hdr, HDR_TID);
126                 if (req->in.wct >= 4) {
127                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
128                 }
129
130                 /* output is actual service name */
131                 p = req->in.data;
132                 if (!p) break;
133
134                 p += cli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
135                                          p, -1, STR_ASCII | STR_TERMINATE);
136                 p += cli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
137                                          p, -1, STR_TERMINATE);
138                 break;
139         }
140
141 failed:
142         return cli_request_destroy(req);
143 }
144
145 /****************************************************************************
146  Send a tconX (sync interface)
147 ****************************************************************************/
148 NTSTATUS smb_tree_connect(struct cli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
149 {
150         struct cli_request *req = smb_tree_connect_send(tree, parms);
151         return smb_tree_connect_recv(req, mem_ctx, parms);
152 }
153
154
155 /****************************************************************************
156  Send a tree disconnect.
157 ****************************************************************************/
158 NTSTATUS smb_tree_disconnect(struct cli_tree *tree)
159 {
160         struct cli_request *req;
161
162         if (!tree) return NT_STATUS_OK;
163         req = cli_request_setup(tree, SMBtdis, 0, 0);
164
165         if (cli_request_send(req)) {
166                 cli_request_receive(req);
167         }
168         return cli_request_destroy(req);
169 }
170
171
172 /*
173   a convenient function to establish a cli_tree from scratch, using reasonable default
174   parameters
175 */
176 NTSTATUS cli_tree_full_connection(struct cli_tree **ret_tree, 
177                                   const char *my_name, 
178                                   const char *dest_host, int port,
179                                   const char *service, const char *service_type,
180                                   const char *user, const char *domain, 
181                                   const char *password)
182 {
183         struct cli_socket *sock;
184         struct cli_transport *transport;
185         struct cli_session *session;
186         struct cli_tree *tree;
187         NTSTATUS status;
188         struct nmb_name calling;
189         struct nmb_name called;
190         union smb_sesssetup setup;
191         union smb_tcon tcon;
192         TALLOC_CTX *mem_ctx;
193
194         *ret_tree = NULL;
195
196         sock = cli_sock_init();
197         if (!sock) {
198                 return NT_STATUS_NO_MEMORY;
199         }
200
201         /* open a TCP socket to the server */
202         if (!cli_sock_connect_byname(sock, dest_host, port)) {
203                 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
204                 return NT_STATUS_UNSUCCESSFUL;
205         }
206
207         transport = cli_transport_init(sock);
208         if (!transport) {
209                 cli_sock_close(sock);
210                 return NT_STATUS_NO_MEMORY;
211         }
212
213         /* send a NBT session request, if applicable */
214         make_nmb_name(&calling, my_name, 0x0);
215         make_nmb_name(&called,  dest_host, 0x20);
216
217         if (!cli_transport_connect(transport, &calling, &called)) {
218                 cli_transport_close(transport);
219                 return NT_STATUS_UNSUCCESSFUL;
220         }
221
222
223         /* negotiate protocol options with the server */
224         status = smb_raw_negotiate(transport);
225         if (!NT_STATUS_IS_OK(status)) {
226                 cli_transport_close(transport);
227                 return status;
228         }
229
230         session = cli_session_init(transport);
231         if (!session) {
232                 cli_transport_close(transport);
233                 return NT_STATUS_NO_MEMORY;
234         }
235
236         /* prepare a session setup to establish a security context */
237         setup.generic.level = RAW_SESSSETUP_GENERIC;
238         setup.generic.in.sesskey = transport->negotiate.sesskey;
239         setup.generic.in.capabilities = CAP_UNICODE | CAP_STATUS32 | 
240                 CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | 
241                 CAP_W2K_SMBS | CAP_LARGE_READX | CAP_LARGE_WRITEX;
242         setup.generic.in.password = password;
243         setup.generic.in.user = user;
244         setup.generic.in.domain = domain;
245
246         mem_ctx = talloc_init("tcon");
247         if (!mem_ctx) {
248                 cli_tree_close(tree);
249                 return NT_STATUS_NO_MEMORY;
250         }
251
252         status = smb_raw_session_setup(session, mem_ctx, &setup);
253         if (!NT_STATUS_IS_OK(status)) {
254                 cli_session_close(session);
255                 talloc_destroy(mem_ctx);
256                 return status;
257         }
258
259         session->vuid = setup.generic.out.vuid;
260
261         tree = cli_tree_init(session);
262         if (!tree) {
263                 cli_session_close(session);
264                 talloc_destroy(mem_ctx);
265                 return NT_STATUS_NO_MEMORY;
266         }
267
268         /* connect to a share using a tree connect */
269         tcon.generic.level = RAW_TCON_TCONX;
270         tcon.tconx.in.flags = 0;
271         tcon.tconx.in.password = data_blob(NULL, 0);
272         tcon.tconx.in.path = service;
273         tcon.tconx.in.device = service_type;
274         
275         status = smb_tree_connect(tree, mem_ctx, &tcon);
276         if (!NT_STATUS_IS_OK(status)) {
277                 cli_tree_close(tree);
278                 talloc_destroy(mem_ctx);
279                 return status;
280         }
281
282         tree->tid = tcon.tconx.out.cnum;
283         tree->device = talloc_strdup(tree->mem_ctx, tcon.tconx.out.dev_type);
284         tree->fs_type = talloc_strdup(tree->mem_ctx, tcon.tconx.out.fs_type);
285
286         talloc_destroy(mem_ctx);
287
288         *ret_tree = tree;
289         return NT_STATUS_OK;
290 }