a5217d74b2a1a6eb2c7d62372ad548f1387ab51d
[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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb_composite/smb_composite.h"
26
27 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
28         req = smbcli_request_setup(tree, cmd, wct, buflen); \
29         if (!req) return NULL; \
30 } while (0)
31
32 /****************************************************************************
33  Initialize the tree context
34 ****************************************************************************/
35 struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session,
36                                      TALLOC_CTX *parent_ctx, BOOL primary)
37 {
38         struct smbcli_tree *tree;
39
40         tree = talloc_zero(parent_ctx, struct smbcli_tree);
41         if (!tree) {
42                 return NULL;
43         }
44
45         if (primary) {
46                 tree->session = talloc_steal(tree, session);
47         } else {
48                 tree->session = talloc_reference(tree, session);
49         }
50
51
52         return tree;
53 }
54
55 /****************************************************************************
56  Send a tconX (async send)
57 ****************************************************************************/
58 struct smbcli_request *smb_raw_tcon_send(struct smbcli_tree *tree, 
59                                          union smb_tcon *parms)
60 {
61         struct smbcli_request *req = NULL;
62
63         switch (parms->tcon.level) {
64         case RAW_TCON_TCON:
65                 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
66                 smbcli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
67                 smbcli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
68                 smbcli_req_append_ascii4(req, parms->tcon.in.dev,     STR_ASCII);
69                 break;
70
71         case RAW_TCON_TCONX:
72                 SETUP_REQUEST_TREE(SMBtconX, 4, 0);
73                 SSVAL(req->out.vwv, VWV(0), 0xFF);
74                 SSVAL(req->out.vwv, VWV(1), 0);
75                 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags);
76                 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length);
77                 smbcli_req_append_blob(req, &parms->tconx.in.password);
78                 smbcli_req_append_string(req, parms->tconx.in.path,   STR_TERMINATE | STR_UPPER);
79                 smbcli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
80                 break;
81
82         case RAW_TCON_SMB2:
83                 return NULL;
84         }
85
86         if (!smbcli_request_send(req)) {
87                 smbcli_request_destroy(req);
88                 return NULL;
89         }
90
91         return req;
92 }
93
94 /****************************************************************************
95  Send a tconX (async recv)
96 ****************************************************************************/
97 NTSTATUS smb_raw_tcon_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, 
98                            union smb_tcon *parms)
99 {
100         uint8_t *p;
101
102         if (!smbcli_request_receive(req) ||
103             smbcli_request_is_error(req)) {
104                 goto failed;
105         }
106
107         switch (parms->tcon.level) {
108         case RAW_TCON_TCON:
109                 SMBCLI_CHECK_WCT(req, 2);
110                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
111                 parms->tcon.out.tid = SVAL(req->in.vwv, VWV(1));
112                 break;
113
114         case RAW_TCON_TCONX:
115                 ZERO_STRUCT(parms->tconx.out);
116                 parms->tconx.out.tid = SVAL(req->in.hdr, HDR_TID);
117                 if (req->in.wct >= 4) {
118                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
119                 }
120
121                 /* output is actual service name */
122                 p = req->in.data;
123                 if (!p) break;
124
125                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
126                                          p, -1, STR_ASCII | STR_TERMINATE);
127                 p += smbcli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
128                                          p, -1, STR_TERMINATE);
129                 break;
130
131         case RAW_TCON_SMB2:
132                 req->status = NT_STATUS_INTERNAL_ERROR;
133                 break;
134         }
135
136 failed:
137         return smbcli_request_destroy(req);
138 }
139
140 /****************************************************************************
141  Send a tconX (sync interface)
142 ****************************************************************************/
143 NTSTATUS smb_raw_tcon(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, 
144                       union smb_tcon *parms)
145 {
146         struct smbcli_request *req = smb_raw_tcon_send(tree, parms);
147         return smb_raw_tcon_recv(req, mem_ctx, parms);
148 }
149
150
151 /****************************************************************************
152  Send a tree disconnect.
153 ****************************************************************************/
154 NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
155 {
156         struct smbcli_request *req;
157
158         if (!tree) return NT_STATUS_OK;
159         req = smbcli_request_setup(tree, SMBtdis, 0, 0);
160
161         if (smbcli_request_send(req)) {
162                 (void) smbcli_request_receive(req);
163         }
164         return smbcli_request_destroy(req);
165 }
166
167
168 /*
169   a convenient function to establish a smbcli_tree from scratch
170 */
171 NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx,
172                                      struct smbcli_tree **ret_tree, 
173                                      const char *dest_host, int port,
174                                      const char *service, const char *service_type,
175                                      struct cli_credentials *credentials,
176                                      struct event_context *ev)
177 {
178         struct smb_composite_connect io;
179         NTSTATUS status;
180         TALLOC_CTX *tmp_ctx = talloc_new(parent_ctx);
181         if (!tmp_ctx) {
182                 return NT_STATUS_NO_MEMORY;
183         }
184
185         io.in.dest_host = dest_host;
186         io.in.port = port;
187         io.in.called_name = strupper_talloc(tmp_ctx, dest_host);
188         io.in.service = service;
189         io.in.service_type = service_type;
190         io.in.credentials = credentials;
191         io.in.fallback_to_anonymous = False;
192         io.in.workgroup = lp_workgroup();
193         
194         status = smb_composite_connect(&io, parent_ctx, ev);
195         if (NT_STATUS_IS_OK(status)) {
196                 *ret_tree = io.out.tree;
197         }
198         talloc_free(tmp_ctx);
199         return status;
200 }