r2247: talloc_destroy -> talloc_free
[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 = smbcli_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 smbcli_tree *smbcli_tree_init(struct smbcli_session *session)
34 {
35         struct smbcli_tree *tree;
36
37         tree = talloc_named(NULL, sizeof(*tree), "smbcli_tree");
38         if (!tree) {
39                 return NULL;
40         }
41
42         ZERO_STRUCTP(tree);
43         tree->session = session;
44         tree->session->reference_count++;
45
46         return tree;
47 }
48
49 /****************************************************************************
50 reduce reference count on a tree and destroy if <= 0
51 ****************************************************************************/
52 void smbcli_tree_close(struct smbcli_tree *tree)
53 {
54         if (!tree) return;
55         tree->reference_count--;
56         if (tree->reference_count <= 0) {
57                 smbcli_session_close(tree->session);
58                 talloc_free(tree);
59         }
60 }
61
62
63 /****************************************************************************
64  Send a tconX (async send)
65 ****************************************************************************/
66 struct smbcli_request *smb_tree_connect_send(struct smbcli_tree *tree, union smb_tcon *parms)
67 {
68         struct smbcli_request *req = NULL;
69
70         switch (parms->tcon.level) {
71         case RAW_TCON_TCON:
72                 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
73                 smbcli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
74                 smbcli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
75                 smbcli_req_append_ascii4(req, parms->tcon.in.dev,     STR_ASCII);
76                 break;
77
78         case RAW_TCON_TCONX:
79                 SETUP_REQUEST_TREE(SMBtconX, 4, 0);
80                 SSVAL(req->out.vwv, VWV(0), 0xFF);
81                 SSVAL(req->out.vwv, VWV(1), 0);
82                 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags);
83                 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length);
84                 smbcli_req_append_blob(req, &parms->tconx.in.password);
85                 smbcli_req_append_string(req, parms->tconx.in.path,   STR_TERMINATE | STR_UPPER);
86                 smbcli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
87                 break;
88         }
89
90         if (!smbcli_request_send(req)) {
91                 smbcli_request_destroy(req);
92                 return NULL;
93         }
94
95         return req;
96 }
97
98 /****************************************************************************
99  Send a tconX (async recv)
100 ****************************************************************************/
101 NTSTATUS smb_tree_connect_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
102 {
103         char *p;
104
105         if (!smbcli_request_receive(req) ||
106             smbcli_request_is_error(req)) {
107                 goto failed;
108         }
109
110         switch (parms->tcon.level) {
111         case RAW_TCON_TCON:
112                 SMBCLI_CHECK_WCT(req, 2);
113                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
114                 parms->tcon.out.cnum = SVAL(req->in.vwv, VWV(1));
115                 break;
116
117         case RAW_TCON_TCONX:
118                 ZERO_STRUCT(parms->tconx.out);
119                 parms->tconx.out.cnum = SVAL(req->in.hdr, HDR_TID);
120                 if (req->in.wct >= 4) {
121                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
122                 }
123
124                 /* output is actual service name */
125                 p = req->in.data;
126                 if (!p) break;
127
128                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
129                                          p, -1, STR_ASCII | STR_TERMINATE);
130                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
131                                          p, -1, STR_TERMINATE);
132                 break;
133         }
134
135 failed:
136         return smbcli_request_destroy(req);
137 }
138
139 /****************************************************************************
140  Send a tconX (sync interface)
141 ****************************************************************************/
142 NTSTATUS smb_tree_connect(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
143 {
144         struct smbcli_request *req = smb_tree_connect_send(tree, parms);
145         return smb_tree_connect_recv(req, mem_ctx, parms);
146 }
147
148
149 /****************************************************************************
150  Send a tree disconnect.
151 ****************************************************************************/
152 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
153 {
154         struct smbcli_request *req;
155
156         if (!tree) return NT_STATUS_OK;
157         req = smbcli_request_setup(tree, SMBtdis, 0, 0);
158
159         if (smbcli_request_send(req)) {
160                 smbcli_request_receive(req);
161         }
162         return smbcli_request_destroy(req);
163 }
164
165
166 /*
167   a convenient function to establish a smbcli_tree from scratch, using reasonable default
168   parameters
169 */
170 NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree, 
171                                      const char *my_name, 
172                                      const char *dest_host, int port,
173                                      const char *service, const char *service_type,
174                                      const char *user, const char *domain, 
175                                      const char *password)
176 {
177         struct smbcli_socket *sock;
178         struct smbcli_transport *transport;
179         struct smbcli_session *session;
180         struct smbcli_tree *tree;
181         NTSTATUS status;
182         struct nmb_name calling;
183         struct nmb_name called;
184         union smb_sesssetup setup;
185         union smb_tcon tcon;
186         TALLOC_CTX *mem_ctx;
187         char *in_path = NULL;
188
189         *ret_tree = NULL;
190
191         sock = smbcli_sock_init();
192         if (!sock) {
193                 return NT_STATUS_NO_MEMORY;
194         }
195
196         /* open a TCP socket to the server */
197         if (!smbcli_sock_connect_byname(sock, dest_host, port)) {
198                 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
199                 return NT_STATUS_UNSUCCESSFUL;
200         }
201
202         transport = smbcli_transport_init(sock);
203         if (!transport) {
204                 smbcli_sock_close(sock);
205                 return NT_STATUS_NO_MEMORY;
206         }
207
208         /* send a NBT session request, if applicable */
209         make_nmb_name(&calling, my_name, 0x0);
210         choose_called_name(&called,  dest_host, 0x20);
211
212         if (!smbcli_transport_connect(transport, &calling, &called)) {
213                 smbcli_transport_close(transport);
214                 return NT_STATUS_UNSUCCESSFUL;
215         }
216
217
218         /* negotiate protocol options with the server */
219         status = smb_raw_negotiate(transport);
220         if (!NT_STATUS_IS_OK(status)) {
221                 smbcli_transport_close(transport);
222                 return status;
223         }
224
225         session = smbcli_session_init(transport);
226         if (!session) {
227                 smbcli_transport_close(transport);
228                 return NT_STATUS_NO_MEMORY;
229         }
230
231         /* prepare a session setup to establish a security context */
232         setup.generic.level = RAW_SESSSETUP_GENERIC;
233         setup.generic.in.sesskey = transport->negotiate.sesskey;
234         setup.generic.in.capabilities = transport->negotiate.capabilities;
235         if (!user || !user[0]) {
236                 setup.generic.in.password = NULL;
237                 setup.generic.in.user = "";
238                 setup.generic.in.domain = "";
239                 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
240         } else {
241                 setup.generic.in.password = password;
242                 setup.generic.in.user = user;
243                 setup.generic.in.domain = domain;
244         }
245
246         mem_ctx = talloc_init("tcon");
247         if (!mem_ctx) {
248                 return NT_STATUS_NO_MEMORY;
249         }
250
251         status = smb_raw_session_setup(session, mem_ctx, &setup);
252         if (!NT_STATUS_IS_OK(status)) {
253                 smbcli_session_close(session);
254                 talloc_free(mem_ctx);
255                 return status;
256         }
257
258         session->vuid = setup.generic.out.vuid;
259
260         tree = smbcli_tree_init(session);
261         if (!tree) {
262                 smbcli_session_close(session);
263                 talloc_free(mem_ctx);
264                 return NT_STATUS_NO_MEMORY;
265         }
266
267         /* connect to a share using a tree connect */
268         tcon.generic.level = RAW_TCON_TCONX;
269         tcon.tconx.in.flags = 0;
270         tcon.tconx.in.password = data_blob(NULL, 0);    
271         asprintf(&in_path, "\\\\%s\\%s", dest_host, service);
272         tcon.tconx.in.path = in_path;
273         if (!service_type) {
274                 if (strequal(service, "IPC$"))
275                         service_type = "IPC";
276                 else
277                         service_type = "?????";
278         }
279         tcon.tconx.in.device = service_type;
280         
281         status = smb_tree_connect(tree, mem_ctx, &tcon);
282
283         SAFE_FREE(in_path);
284
285         if (!NT_STATUS_IS_OK(status)) {
286                 smbcli_tree_close(tree);
287                 talloc_free(mem_ctx);
288                 return status;
289         }
290
291         tree->tid = tcon.tconx.out.cnum;
292         if (tcon.tconx.out.dev_type) {
293                 tree->device = talloc_strdup(tree, tcon.tconx.out.dev_type);
294         }
295         if (tcon.tconx.out.fs_type) {
296                 tree->fs_type = talloc_strdup(tree, tcon.tconx.out.fs_type);
297         }
298
299         talloc_free(mem_ctx);
300
301         *ret_tree = tree;
302         return NT_STATUS_OK;
303 }