r4726: - use the name tcon and tid instead of conn and cnum
[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 #include "libcli/raw/libcliraw.h"
24
25 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
26         req = smbcli_request_setup(tree, cmd, wct, buflen); \
27         if (!req) return NULL; \
28 } while (0)
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_p(session, struct smbcli_tree);
38         if (!tree) {
39                 return NULL;
40         }
41
42         ZERO_STRUCTP(tree);
43         tree->session = talloc_reference(tree, session);
44
45         return tree;
46 }
47
48 /****************************************************************************
49  Send a tconX (async send)
50 ****************************************************************************/
51 struct smbcli_request *smb_tree_connect_send(struct smbcli_tree *tree, union smb_tcon *parms)
52 {
53         struct smbcli_request *req = NULL;
54
55         switch (parms->tcon.level) {
56         case RAW_TCON_TCON:
57                 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
58                 smbcli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
59                 smbcli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
60                 smbcli_req_append_ascii4(req, parms->tcon.in.dev,     STR_ASCII);
61                 break;
62
63         case RAW_TCON_TCONX:
64                 SETUP_REQUEST_TREE(SMBtconX, 4, 0);
65                 SSVAL(req->out.vwv, VWV(0), 0xFF);
66                 SSVAL(req->out.vwv, VWV(1), 0);
67                 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags);
68                 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length);
69                 smbcli_req_append_blob(req, &parms->tconx.in.password);
70                 smbcli_req_append_string(req, parms->tconx.in.path,   STR_TERMINATE | STR_UPPER);
71                 smbcli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
72                 break;
73         }
74
75         if (!smbcli_request_send(req)) {
76                 smbcli_request_destroy(req);
77                 return NULL;
78         }
79
80         return req;
81 }
82
83 /****************************************************************************
84  Send a tconX (async recv)
85 ****************************************************************************/
86 NTSTATUS smb_tree_connect_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
87 {
88         uint8_t *p;
89
90         if (!smbcli_request_receive(req) ||
91             smbcli_request_is_error(req)) {
92                 goto failed;
93         }
94
95         switch (parms->tcon.level) {
96         case RAW_TCON_TCON:
97                 SMBCLI_CHECK_WCT(req, 2);
98                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
99                 parms->tcon.out.tid = SVAL(req->in.vwv, VWV(1));
100                 break;
101
102         case RAW_TCON_TCONX:
103                 ZERO_STRUCT(parms->tconx.out);
104                 parms->tconx.out.tid = SVAL(req->in.hdr, HDR_TID);
105                 if (req->in.wct >= 4) {
106                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
107                 }
108
109                 /* output is actual service name */
110                 p = req->in.data;
111                 if (!p) break;
112
113                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
114                                          p, -1, STR_ASCII | STR_TERMINATE);
115                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
116                                          p, -1, STR_TERMINATE);
117                 break;
118         }
119
120 failed:
121         return smbcli_request_destroy(req);
122 }
123
124 /****************************************************************************
125  Send a tconX (sync interface)
126 ****************************************************************************/
127 NTSTATUS smb_tree_connect(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
128 {
129         struct smbcli_request *req = smb_tree_connect_send(tree, parms);
130         return smb_tree_connect_recv(req, mem_ctx, parms);
131 }
132
133
134 /****************************************************************************
135  Send a tree disconnect.
136 ****************************************************************************/
137 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
138 {
139         struct smbcli_request *req;
140
141         if (!tree) return NT_STATUS_OK;
142         req = smbcli_request_setup(tree, SMBtdis, 0, 0);
143
144         if (smbcli_request_send(req)) {
145                 smbcli_request_receive(req);
146         }
147         return smbcli_request_destroy(req);
148 }
149
150
151 /*
152   a convenient function to establish a smbcli_tree from scratch, using reasonable default
153   parameters
154 */
155 NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx,
156                                      struct smbcli_tree **ret_tree, 
157                                      const char *my_name, 
158                                      const char *dest_host, int port,
159                                      const char *service, const char *service_type,
160                                      const char *user, const char *domain, 
161                                      const char *password)
162 {
163         struct smbcli_socket *sock;
164         struct smbcli_transport *transport;
165         struct smbcli_session *session;
166         struct smbcli_tree *tree;
167         NTSTATUS status;
168         struct nmb_name calling;
169         struct nmb_name called;
170         union smb_sesssetup setup;
171         union smb_tcon tcon;
172         TALLOC_CTX *mem_ctx;
173         char *in_path = NULL;
174
175         *ret_tree = NULL;
176
177         sock = smbcli_sock_init(parent_ctx);
178         if (!sock) {
179                 return NT_STATUS_NO_MEMORY;
180         }
181
182         /* open a TCP socket to the server */
183         if (!smbcli_sock_connect_byname(sock, dest_host, port)) {
184                 talloc_free(sock);
185                 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
186                 return NT_STATUS_UNSUCCESSFUL;
187         }
188
189         transport = smbcli_transport_init(sock);
190         talloc_free(sock);
191         if (!transport) {
192                 return NT_STATUS_NO_MEMORY;
193         }
194
195         /* send a NBT session request, if applicable */
196         make_nmb_name(&calling, my_name, 0x0);
197         choose_called_name(&called,  dest_host, 0x20);
198
199         if (!smbcli_transport_connect(transport, &calling, &called)) {
200                 talloc_free(transport);
201                 return NT_STATUS_UNSUCCESSFUL;
202         }
203
204
205         /* negotiate protocol options with the server */
206         status = smb_raw_negotiate(transport);
207         if (!NT_STATUS_IS_OK(status)) {
208                 talloc_free(transport);
209                 return status;
210         }
211
212         session = smbcli_session_init(transport);
213         talloc_free(transport);
214         if (!session) {
215                 return NT_STATUS_NO_MEMORY;
216         }
217
218         /* prepare a session setup to establish a security context */
219         setup.generic.level = RAW_SESSSETUP_GENERIC;
220         setup.generic.in.sesskey = transport->negotiate.sesskey;
221         setup.generic.in.capabilities = transport->negotiate.capabilities;
222         if (!user || !user[0]) {
223                 setup.generic.in.password = NULL;
224                 setup.generic.in.user = "";
225                 setup.generic.in.domain = "";
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.tid;
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 }