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