libcli/util Rename common map_nt_error_from_unix to avoid duplicate symbol
[kai/samba.git] / source4 / libcli / smb_composite / smb2.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2008
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20   a composite API for making SMB-like calls using SMB2. This is useful
21   as SMB2 often requires more than one requests where a single SMB
22   request would do. In converting code that uses SMB to use SMB2,
23   these routines make life a lot easier
24 */
25
26
27 #include "includes.h"
28 #include <tevent.h>
29 #include "lib/util/tevent_ntstatus.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "libcli/raw/raw_proto.h"
32 #include "libcli/composite/composite.h"
33 #include "libcli/smb_composite/smb_composite.h"
34 #include "libcli/smb2/smb2_calls.h"
35
36 /*
37   continue after a SMB2 close
38  */
39 static void continue_close(struct smb2_request *req)
40 {
41         struct composite_context *ctx = talloc_get_type(req->async.private_data, 
42                                                         struct composite_context);
43         NTSTATUS status;
44         struct smb2_close close_parm;
45
46         status = smb2_close_recv(req, &close_parm);
47         composite_error(ctx, status);   
48 }
49
50 /*
51   continue after the create in a composite unlink
52  */
53 static void continue_unlink(struct smb2_request *req)
54 {
55         struct composite_context *ctx = talloc_get_type(req->async.private_data, 
56                                                         struct composite_context);
57         struct smb2_tree *tree = req->tree;
58         struct smb2_create create_parm;
59         struct smb2_close close_parm;
60         NTSTATUS status;
61
62         status = smb2_create_recv(req, ctx, &create_parm);
63         if (!NT_STATUS_IS_OK(status)) {
64                 composite_error(ctx, status);
65                 return;
66         }
67
68         ZERO_STRUCT(close_parm);
69         close_parm.in.file.handle = create_parm.out.file.handle;
70         
71         req = smb2_close_send(tree, &close_parm);
72         composite_continue_smb2(ctx, req, continue_close, ctx);
73 }
74
75 /*
76   composite SMB2 unlink call
77 */
78 struct composite_context *smb2_composite_unlink_send(struct smb2_tree *tree, 
79                                                      union smb_unlink *io)
80 {
81         struct composite_context *ctx;
82         struct smb2_create create_parm;
83         struct smb2_request *req;
84
85         ctx = composite_create(tree, tree->session->transport->socket->event.ctx);
86         if (ctx == NULL) return NULL;
87
88         /* check for wildcards - we could support these with a
89            search, but for now they aren't necessary */
90         if (strpbrk(io->unlink.in.pattern, "*?<>") != NULL) {
91                 composite_error(ctx, NT_STATUS_NOT_SUPPORTED);
92                 return ctx;
93         }
94
95         ZERO_STRUCT(create_parm);
96         create_parm.in.desired_access     = SEC_STD_DELETE;
97         create_parm.in.create_disposition = NTCREATEX_DISP_OPEN;
98         create_parm.in.share_access = 
99                 NTCREATEX_SHARE_ACCESS_DELETE|
100                 NTCREATEX_SHARE_ACCESS_READ|
101                 NTCREATEX_SHARE_ACCESS_WRITE;
102         create_parm.in.create_options = 
103                 NTCREATEX_OPTIONS_DELETE_ON_CLOSE |
104                 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
105         create_parm.in.fname = io->unlink.in.pattern;
106         if (create_parm.in.fname[0] == '\\') {
107                 create_parm.in.fname++;
108         }
109
110         req = smb2_create_send(tree, &create_parm);
111
112         composite_continue_smb2(ctx, req, continue_unlink, ctx);
113         return ctx;
114 }
115
116
117 /*
118   composite unlink call - sync interface
119 */
120 NTSTATUS smb2_composite_unlink(struct smb2_tree *tree, union smb_unlink *io)
121 {
122         struct composite_context *c = smb2_composite_unlink_send(tree, io);
123         return composite_wait_free(c);
124 }
125
126
127
128
129 /*
130   continue after the create in a composite mkdir
131  */
132 static void continue_mkdir(struct smb2_request *req)
133 {
134         struct composite_context *ctx = talloc_get_type(req->async.private_data, 
135                                                         struct composite_context);
136         struct smb2_tree *tree = req->tree;
137         struct smb2_create create_parm;
138         struct smb2_close close_parm;
139         NTSTATUS status;
140
141         status = smb2_create_recv(req, ctx, &create_parm);
142         if (!NT_STATUS_IS_OK(status)) {
143                 composite_error(ctx, status);
144                 return;
145         }
146
147         ZERO_STRUCT(close_parm);
148         close_parm.in.file.handle = create_parm.out.file.handle;
149         
150         req = smb2_close_send(tree, &close_parm);
151         composite_continue_smb2(ctx, req, continue_close, ctx);
152 }
153
154 /*
155   composite SMB2 mkdir call
156 */
157 struct composite_context *smb2_composite_mkdir_send(struct smb2_tree *tree, 
158                                                      union smb_mkdir *io)
159 {
160         struct composite_context *ctx;
161         struct smb2_create create_parm;
162         struct smb2_request *req;
163
164         ctx = composite_create(tree, tree->session->transport->socket->event.ctx);
165         if (ctx == NULL) return NULL;
166
167         ZERO_STRUCT(create_parm);
168
169         create_parm.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
170         create_parm.in.share_access = 
171                 NTCREATEX_SHARE_ACCESS_READ|
172                 NTCREATEX_SHARE_ACCESS_WRITE;
173         create_parm.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
174         create_parm.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
175         create_parm.in.create_disposition = NTCREATEX_DISP_CREATE;
176         create_parm.in.fname = io->mkdir.in.path;
177         if (create_parm.in.fname[0] == '\\') {
178                 create_parm.in.fname++;
179         }
180
181         req = smb2_create_send(tree, &create_parm);
182
183         composite_continue_smb2(ctx, req, continue_mkdir, ctx);
184
185         return ctx;
186 }
187
188
189 /*
190   composite mkdir call - sync interface
191 */
192 NTSTATUS smb2_composite_mkdir(struct smb2_tree *tree, union smb_mkdir *io)
193 {
194         struct composite_context *c = smb2_composite_mkdir_send(tree, io);
195         return composite_wait_free(c);
196 }
197
198
199
200 /*
201   continue after the create in a composite rmdir
202  */
203 static void continue_rmdir(struct smb2_request *req)
204 {
205         struct composite_context *ctx = talloc_get_type(req->async.private_data, 
206                                                         struct composite_context);
207         struct smb2_tree *tree = req->tree;
208         struct smb2_create create_parm;
209         struct smb2_close close_parm;
210         NTSTATUS status;
211
212         status = smb2_create_recv(req, ctx, &create_parm);
213         if (!NT_STATUS_IS_OK(status)) {
214                 composite_error(ctx, status);
215                 return;
216         }
217
218         ZERO_STRUCT(close_parm);
219         close_parm.in.file.handle = create_parm.out.file.handle;
220         
221         req = smb2_close_send(tree, &close_parm);
222         composite_continue_smb2(ctx, req, continue_close, ctx);
223 }
224
225 /*
226   composite SMB2 rmdir call
227 */
228 struct composite_context *smb2_composite_rmdir_send(struct smb2_tree *tree, 
229                                                     struct smb_rmdir *io)
230 {
231         struct composite_context *ctx;
232         struct smb2_create create_parm;
233         struct smb2_request *req;
234
235         ctx = composite_create(tree, tree->session->transport->socket->event.ctx);
236         if (ctx == NULL) return NULL;
237
238         ZERO_STRUCT(create_parm);
239         create_parm.in.desired_access     = SEC_STD_DELETE;
240         create_parm.in.create_disposition = NTCREATEX_DISP_OPEN;
241         create_parm.in.share_access = 
242                 NTCREATEX_SHARE_ACCESS_DELETE|
243                 NTCREATEX_SHARE_ACCESS_READ|
244                 NTCREATEX_SHARE_ACCESS_WRITE;
245         create_parm.in.create_options = 
246                 NTCREATEX_OPTIONS_DIRECTORY |
247                 NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
248         create_parm.in.fname = io->in.path;
249         if (create_parm.in.fname[0] == '\\') {
250                 create_parm.in.fname++;
251         }
252
253         req = smb2_create_send(tree, &create_parm);
254
255         composite_continue_smb2(ctx, req, continue_rmdir, ctx);
256         return ctx;
257 }
258
259
260 /*
261   composite rmdir call - sync interface
262 */
263 NTSTATUS smb2_composite_rmdir(struct smb2_tree *tree, struct smb_rmdir *io)
264 {
265         struct composite_context *c = smb2_composite_rmdir_send(tree, io);
266         return composite_wait_free(c);
267 }
268
269 struct smb2_composite_setpathinfo_state {
270         struct smb2_tree *tree;
271         union smb_setfileinfo io;
272         NTSTATUS set_status;
273         struct smb2_create cr;
274         struct smb2_close cl;
275 };
276
277 static void smb2_composite_setpathinfo_create_done(struct smb2_request *smb2req);
278
279 /*
280   composite SMB2 setpathinfo call
281 */
282 struct tevent_req *smb2_composite_setpathinfo_send(TALLOC_CTX *mem_ctx,
283                                                    struct tevent_context *ev,
284                                                    struct smb2_tree *tree,
285                                                    const union smb_setfileinfo *io)
286 {
287         struct tevent_req *req;
288         struct smb2_composite_setpathinfo_state *state;
289         struct smb2_request *smb2req;
290
291         req = tevent_req_create(mem_ctx, &state,
292                                 struct smb2_composite_setpathinfo_state);
293         if (req == NULL) {
294                 return NULL;
295         }
296
297         state->tree = tree;
298         state->io = *io;
299
300         state->cr.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
301         state->cr.in.create_disposition = NTCREATEX_DISP_OPEN;
302         state->cr.in.share_access =
303                 NTCREATEX_SHARE_ACCESS_DELETE|
304                 NTCREATEX_SHARE_ACCESS_READ|
305                 NTCREATEX_SHARE_ACCESS_WRITE;
306         state->cr.in.create_options = 0;
307         state->cr.in.fname = state->io.generic.in.file.path;
308         if (state->cr.in.fname[0] == '\\') {
309                 state->cr.in.fname++;
310         }
311
312         smb2req = smb2_create_send(tree, &state->cr);
313         if (tevent_req_nomem(smb2req, req)) {
314                 return tevent_req_post(req, ev);
315         }
316         smb2req->async.fn = smb2_composite_setpathinfo_create_done;
317         smb2req->async.private_data = req;
318
319         return req;
320 }
321
322 static void smb2_composite_setpathinfo_setinfo_done(struct smb2_request *smb2req);
323
324 static void smb2_composite_setpathinfo_create_done(struct smb2_request *smb2req)
325 {
326         struct tevent_req *req =
327                 talloc_get_type_abort(smb2req->async.private_data,
328                 struct tevent_req);
329         struct smb2_composite_setpathinfo_state *state =
330                 tevent_req_data(req,
331                 struct smb2_composite_setpathinfo_state);
332         NTSTATUS status;
333
334         status = smb2_create_recv(smb2req, state, &state->cr);
335         if (tevent_req_nterror(req, status)) {
336                 return;
337         }
338
339         state->io.generic.in.file.handle = state->cr.out.file.handle;
340
341         smb2req = smb2_setinfo_file_send(state->tree, &state->io);
342         if (tevent_req_nomem(smb2req, req)) {
343                 return;
344         }
345         smb2req->async.fn = smb2_composite_setpathinfo_setinfo_done;
346         smb2req->async.private_data = req;
347 }
348
349 static void smb2_composite_setpathinfo_close_done(struct smb2_request *smb2req);
350
351 static void smb2_composite_setpathinfo_setinfo_done(struct smb2_request *smb2req)
352 {
353         struct tevent_req *req =
354                 talloc_get_type_abort(smb2req->async.private_data,
355                 struct tevent_req);
356         struct smb2_composite_setpathinfo_state *state =
357                 tevent_req_data(req,
358                 struct smb2_composite_setpathinfo_state);
359         NTSTATUS status;
360
361         status = smb2_setinfo_recv(smb2req);
362         state->set_status = status;
363
364         state->cl.in.file.handle = state->io.generic.in.file.handle;
365
366         smb2req = smb2_close_send(state->tree, &state->cl);
367         if (tevent_req_nomem(smb2req, req)) {
368                 return;
369         }
370         smb2req->async.fn = smb2_composite_setpathinfo_close_done;
371         smb2req->async.private_data = req;
372 }
373
374 static void smb2_composite_setpathinfo_close_done(struct smb2_request *smb2req)
375 {
376         struct tevent_req *req =
377                 talloc_get_type_abort(smb2req->async.private_data,
378                 struct tevent_req);
379         struct smb2_composite_setpathinfo_state *state =
380                 tevent_req_data(req,
381                 struct smb2_composite_setpathinfo_state);
382         NTSTATUS status;
383
384         status = smb2_close_recv(smb2req, &state->cl);
385
386         if (tevent_req_nterror(req, state->set_status)) {
387                 return;
388         }
389
390         if (tevent_req_nterror(req, status)) {
391                 return;
392         }
393
394         tevent_req_done(req);
395 }
396
397 NTSTATUS smb2_composite_setpathinfo_recv(struct tevent_req *req)
398 {
399         NTSTATUS status;
400
401         if (tevent_req_is_nterror(req, &status)) {
402                 tevent_req_received(req);
403                 return status;
404         }
405
406         tevent_req_received(req);
407         return NT_STATUS_OK;
408 }
409
410 /*
411   composite setpathinfo call
412  */
413 NTSTATUS smb2_composite_setpathinfo(struct smb2_tree *tree, union smb_setfileinfo *io)
414 {
415         struct tevent_req *subreq;
416         NTSTATUS status;
417         bool ok;
418         TALLOC_CTX *frame = talloc_stackframe();
419         struct tevent_context *ev = tree->session->transport->socket->event.ctx;
420
421         if (frame == NULL) {
422                 return NT_STATUS_NO_MEMORY;
423         }
424
425         subreq = smb2_composite_setpathinfo_send(frame, ev, tree, io);
426         if (subreq == NULL) {
427                 TALLOC_FREE(frame);
428                 return NT_STATUS_NO_MEMORY;
429         }
430
431         ok = tevent_req_poll(subreq, ev);
432         if (!ok) {
433                 status = map_nt_error_from_unix_common(errno);
434                 TALLOC_FREE(frame);
435                 return status;
436         }
437
438         status = smb2_composite_setpathinfo_recv(subreq);
439         TALLOC_FREE(subreq);
440         if (!NT_STATUS_IS_OK(status)) {
441                 TALLOC_FREE(frame);
442                 return status;
443         }
444
445         TALLOC_FREE(frame);
446         return NT_STATUS_OK;
447 }