Pass session options around; saves another use of global_loadparm.
[kai/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 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/raw/raw_proto.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "param/param.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 _PUBLIC_ 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         case RAW_TCON_SMB2:
85                 return NULL;
86         }
87
88         if (!smbcli_request_send(req)) {
89                 smbcli_request_destroy(req);
90                 return NULL;
91         }
92
93         return req;
94 }
95
96 /****************************************************************************
97  Send a tconX (async recv)
98 ****************************************************************************/
99 NTSTATUS smb_raw_tcon_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, 
100                            union smb_tcon *parms)
101 {
102         uint8_t *p;
103
104         if (!smbcli_request_receive(req) ||
105             smbcli_request_is_error(req)) {
106                 goto failed;
107         }
108
109         switch (parms->tcon.level) {
110         case RAW_TCON_TCON:
111                 SMBCLI_CHECK_WCT(req, 2);
112                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
113                 parms->tcon.out.tid = SVAL(req->in.vwv, VWV(1));
114                 break;
115
116         case RAW_TCON_TCONX:
117                 ZERO_STRUCT(parms->tconx.out);
118                 parms->tconx.out.tid = SVAL(req->in.hdr, HDR_TID);
119                 if (req->in.wct >= 4) {
120                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
121                 }
122
123                 /* output is actual service name */
124                 p = req->in.data;
125                 if (!p) break;
126
127                 p += smbcli_req_pull_string(&req->in.bufinfo, mem_ctx, &parms->tconx.out.dev_type, 
128                                             p, -1, STR_ASCII | STR_TERMINATE);
129                 p += smbcli_req_pull_string(&req->in.bufinfo, mem_ctx, &parms->tconx.out.fs_type, 
130                                          p, -1, STR_TERMINATE);
131                 break;
132
133         case RAW_TCON_SMB2:
134                 req->status = NT_STATUS_INTERNAL_ERROR;
135                 break;
136         }
137
138 failed:
139         return smbcli_request_destroy(req);
140 }
141
142 /****************************************************************************
143  Send a tconX (sync interface)
144 ****************************************************************************/
145 _PUBLIC_ NTSTATUS smb_raw_tcon(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, 
146                       union smb_tcon *parms)
147 {
148         struct smbcli_request *req = smb_raw_tcon_send(tree, parms);
149         return smb_raw_tcon_recv(req, mem_ctx, parms);
150 }
151
152
153 /****************************************************************************
154  Send a tree disconnect.
155 ****************************************************************************/
156 _PUBLIC_ NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree)
157 {
158         struct smbcli_request *req;
159
160         if (!tree) return NT_STATUS_OK;
161         req = smbcli_request_setup(tree, SMBtdis, 0, 0);
162
163         if (smbcli_request_send(req)) {
164                 (void) smbcli_request_receive(req);
165         }
166         return smbcli_request_destroy(req);
167 }
168
169
170 /*
171   a convenient function to establish a smbcli_tree from scratch
172 */
173 NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx,
174                                      struct smbcli_tree **ret_tree, 
175                                      const char *dest_host, const char **dest_ports,
176                                      const char *service, const char *service_type,
177                                      struct cli_credentials *credentials,
178                                      struct resolve_context *resolve_ctx,
179                                      struct event_context *ev,
180                                      struct smbcli_options *options,
181                                      struct smbcli_session_options *session_options)
182 {
183         struct smb_composite_connect io;
184         NTSTATUS status;
185         TALLOC_CTX *tmp_ctx = talloc_new(parent_ctx);
186         if (!tmp_ctx) {
187                 return NT_STATUS_NO_MEMORY;
188         }
189
190         io.in.dest_host = dest_host;
191         io.in.dest_ports = dest_ports;
192         io.in.called_name = strupper_talloc(tmp_ctx, dest_host);
193         io.in.service = service;
194         io.in.service_type = service_type;
195         io.in.credentials = credentials;
196         io.in.fallback_to_anonymous = false;
197
198         /* This workgroup gets sent out by the SPNEGO session setup.
199          * I don't know of any servers that look at it, so we might
200          * hardcode it to "" some day, when the war on global_loadparm
201          * is complete -- abartlet 2008-04-28 */
202         io.in.workgroup = lp_workgroup(global_loadparm);
203         io.in.options = *options;
204         io.in.session_options = *session_options;
205         
206         status = smb_composite_connect(&io, parent_ctx, resolve_ctx, ev);
207         if (NT_STATUS_IS_OK(status)) {
208                 *ret_tree = io.out.tree;
209         }
210         talloc_free(tmp_ctx);
211         return status;
212 }