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