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.
23 #include "libcli/raw/libcliraw.h"
25 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
26 req = smbcli_request_setup(tree, cmd, wct, buflen); \
27 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;
37 tree = talloc_p(session, struct smbcli_tree);
43 tree->session = talloc_reference(tree, session);
48 /****************************************************************************
49 Send a tconX (async send)
50 ****************************************************************************/
51 struct smbcli_request *smb_tree_connect_send(struct smbcli_tree *tree, union smb_tcon *parms)
53 struct smbcli_request *req = NULL;
55 switch (parms->tcon.level) {
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);
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);
75 if (!smbcli_request_send(req)) {
76 smbcli_request_destroy(req);
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)
90 if (!smbcli_request_receive(req) ||
91 smbcli_request_is_error(req)) {
95 switch (parms->tcon.level) {
97 SMBCLI_CHECK_WCT(req, 2);
98 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
99 parms->tcon.out.cnum = SVAL(req->in.vwv, VWV(1));
103 ZERO_STRUCT(parms->tconx.out);
104 parms->tconx.out.cnum = SVAL(req->in.hdr, HDR_TID);
105 if (req->in.wct >= 4) {
106 parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
109 /* output is actual service name */
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);
121 return smbcli_request_destroy(req);
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)
129 struct smbcli_request *req = smb_tree_connect_send(tree, parms);
130 return smb_tree_connect_recv(req, mem_ctx, parms);
134 /****************************************************************************
135 Send a tree disconnect.
136 ****************************************************************************/
137 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
139 struct smbcli_request *req;
141 if (!tree) return NT_STATUS_OK;
142 req = smbcli_request_setup(tree, SMBtdis, 0, 0);
144 if (smbcli_request_send(req)) {
145 smbcli_request_receive(req);
147 return smbcli_request_destroy(req);
152 a convenient function to establish a smbcli_tree from scratch, using reasonable default
155 NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx,
156 struct smbcli_tree **ret_tree,
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)
163 struct smbcli_socket *sock;
164 struct smbcli_transport *transport;
165 struct smbcli_session *session;
166 struct smbcli_tree *tree;
168 struct nmb_name calling;
169 struct nmb_name called;
170 union smb_sesssetup setup;
173 char *in_path = NULL;
177 sock = smbcli_sock_init(parent_ctx);
179 return NT_STATUS_NO_MEMORY;
182 /* open a TCP socket to the server */
183 if (!smbcli_sock_connect_byname(sock, dest_host, port)) {
185 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
186 return NT_STATUS_UNSUCCESSFUL;
189 transport = smbcli_transport_init(sock);
192 return NT_STATUS_NO_MEMORY;
195 /* send a NBT session request, if applicable */
196 make_nmb_name(&calling, my_name, 0x0);
197 choose_called_name(&called, dest_host, 0x20);
199 if (!smbcli_transport_connect(transport, &calling, &called)) {
200 talloc_free(transport);
201 return NT_STATUS_UNSUCCESSFUL;
205 /* negotiate protocol options with the server */
206 status = smb_raw_negotiate(transport);
207 if (!NT_STATUS_IS_OK(status)) {
208 talloc_free(transport);
212 session = smbcli_session_init(transport);
213 talloc_free(transport);
215 return NT_STATUS_NO_MEMORY;
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 = "";
227 setup.generic.in.password = password;
228 setup.generic.in.user = user;
229 setup.generic.in.domain = domain;
232 mem_ctx = talloc_init("tcon");
234 return NT_STATUS_NO_MEMORY;
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);
244 session->vuid = setup.generic.out.vuid;
246 tree = smbcli_tree_init(session);
247 talloc_free(session);
249 talloc_free(mem_ctx);
250 return NT_STATUS_NO_MEMORY;
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;
260 if (strequal(service, "IPC$"))
261 service_type = "IPC";
263 service_type = "?????";
265 tcon.tconx.in.device = service_type;
267 status = smb_tree_connect(tree, mem_ctx, &tcon);
271 if (!NT_STATUS_IS_OK(status)) {
273 talloc_free(mem_ctx);
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);
281 if (tcon.tconx.out.fs_type) {
282 tree->fs_type = talloc_strdup(tree, tcon.tconx.out.fs_type);
285 talloc_free(mem_ctx);