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 = cli_request_setup(tree, cmd, wct, buflen); \
26 if (!req) return NULL; \
30 /****************************************************************************
31 Initialize the tree context
32 ****************************************************************************/
33 struct cli_tree *cli_tree_init(struct cli_session *session)
35 struct cli_tree *tree;
36 TALLOC_CTX *mem_ctx = talloc_init("cli_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 cli_tree_close(struct cli_tree *tree)
60 tree->reference_count--;
61 if (tree->reference_count <= 0) {
62 cli_session_close(tree->session);
63 talloc_destroy(tree->mem_ctx);
68 /****************************************************************************
69 Send a tconX (async send)
70 ****************************************************************************/
71 struct cli_request *smb_tree_connect_send(struct cli_tree *tree, union smb_tcon *parms)
73 struct cli_request *req;
75 switch (parms->tcon.level) {
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);
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);
95 if (!cli_request_send(req)) {
96 cli_request_destroy(req);
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)
110 if (!cli_request_receive(req) ||
111 cli_request_is_error(req)) {
115 switch (parms->tcon.level) {
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));
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 += 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);
141 return cli_request_destroy(req);
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)
149 struct cli_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 cli_tree *tree)
159 struct cli_request *req;
161 if (!tree) return NT_STATUS_OK;
162 req = cli_request_setup(tree, SMBtdis, 0, 0);
164 if (cli_request_send(req)) {
165 cli_request_receive(req);
167 return cli_request_destroy(req);
172 a convenient function to establish a cli_tree from scratch, using reasonable default
175 NTSTATUS cli_tree_full_connection(struct cli_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 cli_socket *sock;
183 struct cli_transport *transport;
184 struct cli_session *session;
185 struct cli_tree *tree;
187 struct nmb_name calling;
188 struct nmb_name called;
189 union smb_sesssetup setup;
195 sock = cli_sock_init();
197 return NT_STATUS_NO_MEMORY;
200 /* open a TCP socket to the server */
201 if (!cli_sock_connect_byname(sock, dest_host, port)) {
202 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
203 return NT_STATUS_UNSUCCESSFUL;
206 transport = cli_transport_init(sock);
208 cli_sock_close(sock);
209 return NT_STATUS_NO_MEMORY;
212 /* send a NBT session request, if applicable */
213 make_nmb_name(&calling, my_name, 0x0);
214 make_nmb_name(&called, dest_host, 0x20);
216 if (!cli_transport_connect(transport, &calling, &called)) {
217 cli_transport_close(transport);
218 return NT_STATUS_UNSUCCESSFUL;
222 /* negotiate protocol options with the server */
223 status = smb_raw_negotiate(transport);
224 if (!NT_STATUS_IS_OK(status)) {
225 cli_transport_close(transport);
229 session = cli_session_init(transport);
231 cli_transport_close(transport);
232 return NT_STATUS_NO_MEMORY;
235 /* prepare a session setup to establish a security context */
236 setup.generic.level = RAW_SESSSETUP_GENERIC;
237 setup.generic.in.sesskey = transport->negotiate.sesskey;
238 setup.generic.in.capabilities = transport->negotiate.capabilities;
239 if (!user || !user[0]) {
240 setup.generic.in.password = NULL;
241 setup.generic.in.user = "";
242 setup.generic.in.domain = "";
244 setup.generic.in.password = password;
245 setup.generic.in.user = user;
246 setup.generic.in.domain = domain;
249 mem_ctx = talloc_init("tcon");
251 return NT_STATUS_NO_MEMORY;
254 status = smb_raw_session_setup(session, mem_ctx, &setup);
255 if (!NT_STATUS_IS_OK(status)) {
256 cli_session_close(session);
257 talloc_destroy(mem_ctx);
261 session->vuid = setup.generic.out.vuid;
263 tree = cli_tree_init(session);
265 cli_session_close(session);
266 talloc_destroy(mem_ctx);
267 return NT_STATUS_NO_MEMORY;
270 /* connect to a share using a tree connect */
271 tcon.generic.level = RAW_TCON_TCONX;
272 tcon.tconx.in.flags = 0;
273 tcon.tconx.in.password = data_blob(NULL, 0);
274 asprintf(&tcon.tconx.in.path, "\\\\%s\\%s", dest_host, service);
276 if (strequal(service, "IPC$"))
277 service_type = "IPC";
279 service_type = "?????";
281 tcon.tconx.in.device = service_type;
283 status = smb_tree_connect(tree, mem_ctx, &tcon);
285 free(tcon.tconx.in.path);
287 if (!NT_STATUS_IS_OK(status)) {
288 cli_tree_close(tree);
289 talloc_destroy(mem_ctx);
293 tree->tid = tcon.tconx.out.cnum;
294 if (tcon.tconx.out.dev_type) {
295 tree->device = talloc_strdup(tree->mem_ctx, tcon.tconx.out.dev_type);
297 if (tcon.tconx.out.fs_type) {
298 tree->fs_type = talloc_strdup(tree->mem_ctx, tcon.tconx.out.fs_type);
301 talloc_destroy(mem_ctx);