r1345: add extended security spnego support to the smb client
[gd/samba-autobuild/.git] / source4 / libcli / raw / clitree.c
1 /* 
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>
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 #define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \
25         req = cli_request_setup(tree, cmd, wct, buflen); \
26         if (!req) return NULL; \
27 } while (0)
28
29
30 /****************************************************************************
31  Initialize the tree context
32 ****************************************************************************/
33 struct cli_tree *cli_tree_init(struct cli_session *session)
34 {
35         struct cli_tree *tree;
36         TALLOC_CTX *mem_ctx = talloc_init("cli_tree");
37         if (mem_ctx == NULL) {
38                 return NULL;
39         }
40
41         tree = talloc_zero(mem_ctx, sizeof(*tree));
42         if (!tree) {
43                 talloc_destroy(mem_ctx);
44                 return NULL;
45         }
46
47         tree->mem_ctx = mem_ctx;
48         tree->session = session;
49         tree->session->reference_count++;
50
51         return tree;
52 }
53
54 /****************************************************************************
55 reduce reference count on a tree and destroy if <= 0
56 ****************************************************************************/
57 void cli_tree_close(struct cli_tree *tree)
58 {
59         if (!tree) return;
60         tree->reference_count--;
61         if (tree->reference_count <= 0) {
62                 cli_session_close(tree->session);
63                 talloc_destroy(tree->mem_ctx);
64         }
65 }
66
67
68 /****************************************************************************
69  Send a tconX (async send)
70 ****************************************************************************/
71 struct cli_request *smb_tree_connect_send(struct cli_tree *tree, union smb_tcon *parms)
72 {
73         struct cli_request *req;
74
75         switch (parms->tcon.level) {
76         case RAW_TCON_TCON:
77                 SETUP_REQUEST_TREE(SMBtcon, 0, 0);
78                 cli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII);
79                 cli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII);
80                 cli_req_append_ascii4(req, parms->tcon.in.dev,     STR_ASCII);
81                 break;
82
83         case RAW_TCON_TCONX:
84                 SETUP_REQUEST_TREE(SMBtconX, 4, 0);
85                 SSVAL(req->out.vwv, VWV(0), 0xFF);
86                 SSVAL(req->out.vwv, VWV(1), 0);
87                 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags);
88                 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length);
89                 cli_req_append_blob(req, &parms->tconx.in.password);
90                 cli_req_append_string(req, parms->tconx.in.path,   STR_TERMINATE | STR_UPPER);
91                 cli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII);
92                 break;
93         }
94
95         if (!cli_request_send(req)) {
96                 cli_request_destroy(req);
97                 return NULL;
98         }
99
100         return req;
101 }
102
103 /****************************************************************************
104  Send a tconX (async recv)
105 ****************************************************************************/
106 NTSTATUS smb_tree_connect_recv(struct cli_request *req, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
107 {
108         char *p;
109
110         if (!cli_request_receive(req) ||
111             cli_request_is_error(req)) {
112                 goto failed;
113         }
114
115         switch (parms->tcon.level) {
116         case RAW_TCON_TCON:
117                 CLI_CHECK_WCT(req, 2);
118                 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0));
119                 parms->tcon.out.cnum = SVAL(req->in.vwv, VWV(1));
120                 break;
121
122         case RAW_TCON_TCONX:
123                 ZERO_STRUCT(parms->tconx.out);
124                 parms->tconx.out.cnum = SVAL(req->in.hdr, HDR_TID);
125                 if (req->in.wct >= 4) {
126                         parms->tconx.out.options = SVAL(req->in.vwv, VWV(3));
127                 }
128
129                 /* output is actual service name */
130                 p = req->in.data;
131                 if (!p) break;
132
133                 p += cli_req_pull_string(req, mem_ctx, &parms->tconx.out.dev_type, 
134                                          p, -1, STR_ASCII | STR_TERMINATE);
135                 p += cli_req_pull_string(req, mem_ctx, &parms->tconx.out.fs_type, 
136                                          p, -1, STR_TERMINATE);
137                 break;
138         }
139
140 failed:
141         return cli_request_destroy(req);
142 }
143
144 /****************************************************************************
145  Send a tconX (sync interface)
146 ****************************************************************************/
147 NTSTATUS smb_tree_connect(struct cli_tree *tree, TALLOC_CTX *mem_ctx, union smb_tcon *parms)
148 {
149         struct cli_request *req = smb_tree_connect_send(tree, parms);
150         return smb_tree_connect_recv(req, mem_ctx, parms);
151 }
152
153
154 /****************************************************************************
155  Send a tree disconnect.
156 ****************************************************************************/
157 NTSTATUS smb_tree_disconnect(struct cli_tree *tree)
158 {
159         struct cli_request *req;
160
161         if (!tree) return NT_STATUS_OK;
162         req = cli_request_setup(tree, SMBtdis, 0, 0);
163
164         if (cli_request_send(req)) {
165                 cli_request_receive(req);
166         }
167         return cli_request_destroy(req);
168 }
169
170
171 /*
172   a convenient function to establish a cli_tree from scratch, using reasonable default
173   parameters
174 */
175 NTSTATUS cli_tree_full_connection(struct cli_tree **ret_tree, 
176                                   const char *my_name, 
177                                   const char *dest_host, int port,
178                                   const char *service, const char *service_type,
179                                   const char *user, const char *domain, 
180                                   const char *password)
181 {
182         struct cli_socket *sock;
183         struct cli_transport *transport;
184         struct cli_session *session;
185         struct cli_tree *tree;
186         NTSTATUS status;
187         struct nmb_name calling;
188         struct nmb_name called;
189         union smb_sesssetup setup;
190         union smb_tcon tcon;
191         TALLOC_CTX *mem_ctx;
192
193         *ret_tree = NULL;
194
195         sock = cli_sock_init();
196         if (!sock) {
197                 return NT_STATUS_NO_MEMORY;
198         }
199
200         /* open a TCP socket to the server */
201         if (!cli_sock_connect_byname(sock, dest_host, port)) {
202                 DEBUG(2,("Failed to establish socket connection - %s\n", strerror(errno)));
203                 return NT_STATUS_UNSUCCESSFUL;
204         }
205
206         transport = cli_transport_init(sock);
207         if (!transport) {
208                 cli_sock_close(sock);
209                 return NT_STATUS_NO_MEMORY;
210         }
211
212         /* send a NBT session request, if applicable */
213         make_nmb_name(&calling, my_name, 0x0);
214         make_nmb_name(&called,  dest_host, 0x20);
215
216         if (!cli_transport_connect(transport, &calling, &called)) {
217                 cli_transport_close(transport);
218                 return NT_STATUS_UNSUCCESSFUL;
219         }
220
221
222         /* negotiate protocol options with the server */
223         status = smb_raw_negotiate(transport);
224         if (!NT_STATUS_IS_OK(status)) {
225                 cli_transport_close(transport);
226                 return status;
227         }
228
229         session = cli_session_init(transport);
230         if (!session) {
231                 cli_transport_close(transport);
232                 return NT_STATUS_NO_MEMORY;
233         }
234
235         /* prepare a session setup to establish a security context */
236         setup.generic.level = RAW_SESSSETUP_GENERIC;
237         setup.generic.in.sesskey = transport->negotiate.sesskey;
238         setup.generic.in.capabilities = transport->negotiate.capabilities;
239         if (!user || !user[0]) {
240                 setup.generic.in.password = NULL;
241                 setup.generic.in.user = "";
242                 setup.generic.in.domain = "";
243         } else {
244                 setup.generic.in.password = password;
245                 setup.generic.in.user = user;
246                 setup.generic.in.domain = domain;
247         }
248
249         mem_ctx = talloc_init("tcon");
250         if (!mem_ctx) {
251                 return NT_STATUS_NO_MEMORY;
252         }
253
254         status = smb_raw_session_setup(session, mem_ctx, &setup);
255         if (!NT_STATUS_IS_OK(status)) {
256                 cli_session_close(session);
257                 talloc_destroy(mem_ctx);
258                 return status;
259         }
260
261         session->vuid = setup.generic.out.vuid;
262
263         tree = cli_tree_init(session);
264         if (!tree) {
265                 cli_session_close(session);
266                 talloc_destroy(mem_ctx);
267                 return NT_STATUS_NO_MEMORY;
268         }
269
270         /* connect to a share using a tree connect */
271         tcon.generic.level = RAW_TCON_TCONX;
272         tcon.tconx.in.flags = 0;
273         tcon.tconx.in.password = data_blob(NULL, 0);
274         asprintf(&tcon.tconx.in.path, "\\\\%s\\%s", dest_host, service);
275         if (!service_type) {
276                 if (strequal(service, "IPC$"))
277                         service_type = "IPC";
278                 else
279                         service_type = "?????";
280         }
281         tcon.tconx.in.device = service_type;
282         
283         status = smb_tree_connect(tree, mem_ctx, &tcon);
284
285         free(tcon.tconx.in.path);
286
287         if (!NT_STATUS_IS_OK(status)) {
288                 cli_tree_close(tree);
289                 talloc_destroy(mem_ctx);
290                 return status;
291         }
292
293         tree->tid = tcon.tconx.out.cnum;
294         if (tcon.tconx.out.dev_type) {
295                 tree->device = talloc_strdup(tree->mem_ctx, tcon.tconx.out.dev_type);
296         }
297         if (tcon.tconx.out.fs_type) {
298                 tree->fs_type = talloc_strdup(tree->mem_ctx, tcon.tconx.out.fs_type);
299         }
300
301         talloc_destroy(mem_ctx);
302
303         *ret_tree = tree;
304         return NT_STATUS_OK;
305 }