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>
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.
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.
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.
24 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
25 req = smbcli_request_setup(tree, cmd, wct, buflen); \
26 if (!req) return NULL; \
30 /****************************************************************************
31 Initialize the tree context
32 ****************************************************************************/
33 struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session)
35 struct smbcli_tree *tree;
36 TALLOC_CTX *mem_ctx = talloc_init("smbcli_tree");
37 if (mem_ctx == NULL) {
41 tree = talloc_zero(mem_ctx, sizeof(*tree));
43 talloc_destroy(mem_ctx);
47 tree->mem_ctx = mem_ctx;
48 tree->session = session;
49 tree->session->reference_count++;
54 /****************************************************************************
55 reduce reference count on a tree and destroy if <= 0
56 ****************************************************************************/
57 void smbcli_tree_close(struct smbcli_tree *tree)
60 tree->reference_count--;
61 if (tree->reference_count <= 0) {
62 smbcli_session_close(tree->session);
63 talloc_destroy(tree->mem_ctx);
68 /****************************************************************************
69 Send a tconX (async send)
70 ****************************************************************************/
71 struct smbcli_request *smb_tree_connect_send(struct smbcli_tree *tree, union smb_tcon *parms)
73 struct smbcli_request *req;
75 switch (parms->tcon.level) {
77 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
78 smbcli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
79 smbcli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
80 smbcli_req_append_ascii4(req, parms->tcon.in.dev, STR_ASCII);
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 smbcli_req_append_blob(req, &parms->tconx.in.password);
90 smbcli_req_append_string(req, parms->tconx.in.path, STR_TERMINATE | STR_UPPER);
91 smbcli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
95 if (!smbcli_request_send(req)) {
96 smbcli_request_destroy(req);
103 /****************************************************************************
104 Send a tconX (async recv)
105 ****************************************************************************/
106 NTSTATUS smb_tree_connect_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
110 if (!smbcli_request_receive(req) ||
111 smbcli_request_is_error(req)) {
115 switch (parms->tcon.level) {
117 SMBCLI_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));
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));
129 /* output is actual service name */
133 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type,
134 p, -1, STR_ASCII | STR_TERMINATE);
135 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type,
136 p, -1, STR_TERMINATE);
141 return smbcli_request_destroy(req);
144 /****************************************************************************
145 Send a tconX (sync interface)
146 ****************************************************************************/
147 NTSTATUS smb_tree_connect(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
149 struct smbcli_request *req = smb_tree_connect_send(tree, parms);
150 return smb_tree_connect_recv(req, mem_ctx, parms);
154 /****************************************************************************
155 Send a tree disconnect.
156 ****************************************************************************/
157 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
159 struct smbcli_request *req;
161 if (!tree) return NT_STATUS_OK;
162 req = smbcli_request_setup(tree, SMBtdis, 0, 0);
164 if (smbcli_request_send(req)) {
165 smbcli_request_receive(req);
167 return smbcli_request_destroy(req);
172 a convenient function to establish a smbcli_tree from scratch, using reasonable default
175 NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
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)
182 struct smbcli_socket *sock;
183 struct smbcli_transport *transport;
184 struct smbcli_session *session;
185 struct smbcli_tree *tree;
187 struct nmb_name calling;
188 struct nmb_name called;
189 union smb_sesssetup setup;
192 char *in_path = NULL;
196 sock = smbcli_sock_init();
198 return NT_STATUS_NO_MEMORY;
201 /* open a TCP socket to the server */
202 if (!smbcli_sock_connect_byname(sock, dest_host, port)) {
203 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
204 return NT_STATUS_UNSUCCESSFUL;
207 transport = smbcli_transport_init(sock);
209 smbcli_sock_close(sock);
210 return NT_STATUS_NO_MEMORY;
213 /* send a NBT session request, if applicable */
214 make_nmb_name(&calling, my_name, 0x0);
215 make_nmb_name(&called, dest_host, 0x20);
217 if (!smbcli_transport_connect(transport, &calling, &called)) {
218 smbcli_transport_close(transport);
219 return NT_STATUS_UNSUCCESSFUL;
223 /* negotiate protocol options with the server */
224 status = smb_raw_negotiate(transport);
225 if (!NT_STATUS_IS_OK(status)) {
226 smbcli_transport_close(transport);
230 session = smbcli_session_init(transport);
232 smbcli_transport_close(transport);
233 return NT_STATUS_NO_MEMORY;
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;
246 setup.generic.in.password = password;
247 setup.generic.in.user = user;
248 setup.generic.in.domain = domain;
251 mem_ctx = talloc_init("tcon");
253 return NT_STATUS_NO_MEMORY;
256 status = smb_raw_session_setup(session, mem_ctx, &setup);
257 if (!NT_STATUS_IS_OK(status)) {
258 smbcli_session_close(session);
259 talloc_destroy(mem_ctx);
263 session->vuid = setup.generic.out.vuid;
265 tree = smbcli_tree_init(session);
267 smbcli_session_close(session);
268 talloc_destroy(mem_ctx);
269 return NT_STATUS_NO_MEMORY;
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 asprintf(&in_path, "\\\\%s\\%s", dest_host, service);
277 tcon.tconx.in.path = in_path;
279 if (strequal(service, "IPC$"))
280 service_type = "IPC";
282 service_type = "?????";
284 tcon.tconx.in.device = service_type;
286 status = smb_tree_connect(tree, mem_ctx, &tcon);
290 if (!NT_STATUS_IS_OK(status)) {
291 smbcli_tree_close(tree);
292 talloc_destroy(mem_ctx);
296 tree->tid = tcon.tconx.out.cnum;
297 if (tcon.tconx.out.dev_type) {
298 tree->device = talloc_strdup(tree->mem_ctx, tcon.tconx.out.dev_type);
300 if (tcon.tconx.out.fs_type) {
301 tree->fs_type = talloc_strdup(tree->mem_ctx, tcon.tconx.out.fs_type);
304 talloc_destroy(mem_ctx);