r1460: Avoid a compile warning.
[gd/samba-autobuild/.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                 parms->tconx.out.cnum = SVAL(req->in.hdr, HDR_TID);
125                 if (req->in.wct >= 4) {
126                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
127                 }
128
129                 /* output is actual service name */
130                 p = req->in.data;
131                 if (!p) break;
132
133                 p += cli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
134                                          p, -1, STR_ASCII | STR_TERMINATE);
135                 p += cli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
136                                          p, -1, STR_TERMINATE);
137                 break;
138         }
139
140 failed:
141         return cli_request_destroy(req);
142 }
143
144 /****************************************************************************
145  Send a tconX (sync interface)
146 ****************************************************************************/
147 NTSTATUS smb_tree_connect(struct cli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
148 {
149         struct cli_request *req = smb_tree_connect_send(tree, parms);
150         return smb_tree_connect_recv(req, mem_ctx, parms);
151 }
152
153
154 /****************************************************************************
155  Send a tree disconnect.
156 ****************************************************************************/
157 NTSTATUS smb_tree_disconnect(struct cli_tree *tree)
158 {
159         struct cli_request *req;
160
161         if (!tree) return NT_STATUS_OK;
162         req = cli_request_setup(tree, SMBtdis, 0, 0);
163
164         if (cli_request_send(req)) {
165                 cli_request_receive(req);
166         }
167         return cli_request_destroy(req);
168 }
169
170
171 /*
172   a convenient function to establish a cli_tree from scratch, using reasonable default
173   parameters
174 */
175 NTSTATUS cli_tree_full_connection(struct cli_tree **ret_tree, 
176                                   const char *my_name, 
177                                   const char *dest_host, int port,
178                                   const char *service, const char *service_type,
179                                   const char *user, const char *domain, 
180                                   const char *password)
181 {
182         struct cli_socket *sock;
183         struct cli_transport *transport;
184         struct cli_session *session;
185         struct cli_tree *tree;
186         NTSTATUS status;
187         struct nmb_name calling;
188         struct nmb_name called;
189         union smb_sesssetup setup;
190         union smb_tcon tcon;
191         TALLOC_CTX *mem_ctx;
192         char *in_path = NULL;
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 = transport->negotiate.capabilities;
240         if (!user || !user[0]) {
241                 setup.generic.in.password = NULL;
242                 setup.generic.in.user = "";
243                 setup.generic.in.domain = "";
244                 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
245         } else {
246                 setup.generic.in.password = password;
247                 setup.generic.in.user = user;
248                 setup.generic.in.domain = domain;
249         }
250
251         mem_ctx = talloc_init("tcon");
252         if (!mem_ctx) {
253                 return NT_STATUS_NO_MEMORY;
254         }
255
256         status = smb_raw_session_setup(session, mem_ctx, &setup);
257         if (!NT_STATUS_IS_OK(status)) {
258                 cli_session_close(session);
259                 talloc_destroy(mem_ctx);
260                 return status;
261         }
262
263         session->vuid = setup.generic.out.vuid;
264
265         tree = cli_tree_init(session);
266         if (!tree) {
267                 cli_session_close(session);
268                 talloc_destroy(mem_ctx);
269                 return NT_STATUS_NO_MEMORY;
270         }
271
272         /* connect to a share using a tree connect */
273         tcon.generic.level = RAW_TCON_TCONX;
274         tcon.tconx.in.flags = 0;
275         tcon.tconx.in.password = data_blob(NULL, 0);
276         
277         asprintf(in_path, "\\\\%s\\%s", dest_host, service);
278         tcon.tconx.in.path = in_path;
279         if (!service_type) {
280                 if (strequal(service, "IPC$"))
281                         service_type = "IPC";
282                 else
283                         service_type = "?????";
284         }
285         tcon.tconx.in.device = service_type;
286         
287         status = smb_tree_connect(tree, mem_ctx, &tcon);
288
289         SAFE_FREE(in_path);
290
291         if (!NT_STATUS_IS_OK(status)) {
292                 cli_tree_close(tree);
293                 talloc_destroy(mem_ctx);
294                 return status;
295         }
296
297         tree->tid = tcon.tconx.out.cnum;
298         if (tcon.tconx.out.dev_type) {
299                 tree->device = talloc_strdup(tree->mem_ctx, tcon.tconx.out.dev_type);
300         }
301         if (tcon.tconx.out.fs_type) {
302                 tree->fs_type = talloc_strdup(tree->mem_ctx, tcon.tconx.out.fs_type);
303         }
304
305         talloc_destroy(mem_ctx);
306
307         *ret_tree = tree;
308         return NT_STATUS_OK;
309 }