c6b3fa5ad9e951946f5f9b94dac46e4b9ee1230b
[gd/samba-autobuild/.git] / source4 / libcli / raw / clitree.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB client tree context management functions
5
6    Copyright (C) Andrew Tridgell 1994-2005
7    Copyright (C) James Myers 2003 <myersjj@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27
28 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
29         req = smbcli_request_setup(tree, cmd, wct, buflen); \
30         if (!req) return NULL; \
31 } while (0)
32
33 /****************************************************************************
34  Initialize the tree context
35 ****************************************************************************/
36 struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session)
37 {
38         struct smbcli_tree *tree;
39
40         tree = talloc_zero(session, struct smbcli_tree);
41         if (!tree) {
42                 return NULL;
43         }
44
45         tree->session = talloc_reference(tree, session);
46
47         return tree;
48 }
49
50 /****************************************************************************
51  Send a tconX (async send)
52 ****************************************************************************/
53 struct smbcli_request *smb_tree_connect_send(struct smbcli_tree *tree, 
54                                              union smb_tcon *parms)
55 {
56         struct smbcli_request *req = NULL;
57
58         switch (parms->tcon.level) {
59         case RAW_TCON_TCON:
60                 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
61                 smbcli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
62                 smbcli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
63                 smbcli_req_append_ascii4(req, parms->tcon.in.dev,     STR_ASCII);
64                 break;
65
66         case RAW_TCON_TCONX:
67                 SETUP_REQUEST_TREE(SMBtconX, 4, 0);
68                 SSVAL(req->out.vwv, VWV(0), 0xFF);
69                 SSVAL(req->out.vwv, VWV(1), 0);
70                 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags);
71                 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length);
72                 smbcli_req_append_blob(req, &parms->tconx.in.password);
73                 smbcli_req_append_string(req, parms->tconx.in.path,   STR_TERMINATE | STR_UPPER);
74                 smbcli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
75                 break;
76         }
77
78         if (!smbcli_request_send(req)) {
79                 smbcli_request_destroy(req);
80                 return NULL;
81         }
82
83         return req;
84 }
85
86 /****************************************************************************
87  Send a tconX (async recv)
88 ****************************************************************************/
89 NTSTATUS smb_tree_connect_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, 
90                                union smb_tcon *parms)
91 {
92         uint8_t *p;
93
94         if (!smbcli_request_receive(req) ||
95             smbcli_request_is_error(req)) {
96                 goto failed;
97         }
98
99         switch (parms->tcon.level) {
100         case RAW_TCON_TCON:
101                 SMBCLI_CHECK_WCT(req, 2);
102                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
103                 parms->tcon.out.tid = SVAL(req->in.vwv, VWV(1));
104                 break;
105
106         case RAW_TCON_TCONX:
107                 ZERO_STRUCT(parms->tconx.out);
108                 parms->tconx.out.tid = SVAL(req->in.hdr, HDR_TID);
109                 if (req->in.wct >= 4) {
110                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
111                 }
112
113                 /* output is actual service name */
114                 p = req->in.data;
115                 if (!p) break;
116
117                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
118                                          p, -1, STR_ASCII | STR_TERMINATE);
119                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
120                                          p, -1, STR_TERMINATE);
121                 break;
122         }
123
124 failed:
125         return smbcli_request_destroy(req);
126 }
127
128 /****************************************************************************
129  Send a tconX (sync interface)
130 ****************************************************************************/
131 NTSTATUS smb_tree_connect(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, 
132                           union smb_tcon *parms)
133 {
134         struct smbcli_request *req = smb_tree_connect_send(tree, parms);
135         return smb_tree_connect_recv(req, mem_ctx, parms);
136 }
137
138
139 /****************************************************************************
140  Send a tree disconnect.
141 ****************************************************************************/
142 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
143 {
144         struct smbcli_request *req;
145
146         if (!tree) return NT_STATUS_OK;
147         req = smbcli_request_setup(tree, SMBtdis, 0, 0);
148
149         if (smbcli_request_send(req)) {
150                 smbcli_request_receive(req);
151         }
152         return smbcli_request_destroy(req);
153 }
154
155
156 /*
157   a convenient function to establish a smbcli_tree from scratch
158 */
159 NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx,
160                                      struct smbcli_tree **ret_tree, 
161                                      const char *my_name, 
162                                      const char *dest_host, int port,
163                                      const char *service, const char *service_type,
164                                      const char *user, const char *domain, 
165                                      const char *password)
166 {
167         struct smb_composite_connect io;
168         NTSTATUS status;
169
170         io.in.dest_host = dest_host;
171         io.in.port = port;
172         io.in.called_name = dest_host;
173         io.in.calling_name = my_name;
174         io.in.service = service;
175         io.in.service_type = service_type;
176         io.in.user = user;
177         io.in.domain = domain;
178         io.in.password = password;
179         
180         status = smb_composite_connect(&io, parent_ctx);
181         if (NT_STATUS_IS_OK(status)) {
182                 *ret_tree = io.out.tree;
183         }
184
185         return status;
186 }