summaryrefslogtreecommitdiff
path: root/source4/libcli/smb_composite/smb2.c
blob: 5c93869b5c82dc06c31590087fe44e5ed216b1ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* 
   Unix SMB/CIFS implementation.

   Copyright (C) Andrew Tridgell 2008
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
  a composite API for making SMB-like calls using SMB2. This is useful
  as SMB2 often requires more than one requests where a single SMB
  request would do. In converting code that uses SMB to use SMB2,
  these routines make life a lot easier
*/


#include "includes.h"
#include <tevent.h>
#include "lib/util/tevent_ntstatus.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/raw/raw_proto.h"
#include "libcli/composite/composite.h"
#include "libcli/smb_composite/smb_composite.h"
#include "libcli/smb2/smb2_calls.h"

/*
  continue after a SMB2 close
 */
static void continue_close(struct smb2_request *req)
{
	struct composite_context *ctx = talloc_get_type(req->async.private_data, 
							struct composite_context);
	NTSTATUS status;
	struct smb2_close close_parm;

	status = smb2_close_recv(req, &close_parm);
	composite_error(ctx, status);	
}

/*
  continue after the create in a composite unlink
 */
static void continue_unlink(struct smb2_request *req)
{
	struct composite_context *ctx = talloc_get_type(req->async.private_data, 
							struct composite_context);
	struct smb2_tree *tree = req->tree;
	struct smb2_create create_parm;
	struct smb2_close close_parm;
	NTSTATUS status;

	status = smb2_create_recv(req, ctx, &create_parm);
	if (!NT_STATUS_IS_OK(status)) {
		composite_error(ctx, status);
		return;
	}

	ZERO_STRUCT(close_parm);
	close_parm.in.file.handle = create_parm.out.file.handle;
	
	req = smb2_close_send(tree, &close_parm);
	composite_continue_smb2(ctx, req, continue_close, ctx);
}

/*
  composite SMB2 unlink call
*/
struct composite_context *smb2_composite_unlink_send(struct smb2_tree *tree, 
						     union smb_unlink *io)
{
	struct composite_context *ctx;
	struct smb2_create create_parm;
	struct smb2_request *req;

	ctx = composite_create(tree, tree->session->transport->socket->event.ctx);
	if (ctx == NULL) return NULL;

	/* check for wildcards - we could support these with a
	   search, but for now they aren't necessary */
	if (strpbrk(io->unlink.in.pattern, "*?<>") != NULL) {
		composite_error(ctx, NT_STATUS_NOT_SUPPORTED);
		return ctx;
	}

	ZERO_STRUCT(create_parm);
	create_parm.in.desired_access     = SEC_STD_DELETE;
	create_parm.in.create_disposition = NTCREATEX_DISP_OPEN;
	create_parm.in.share_access = 
		NTCREATEX_SHARE_ACCESS_DELETE|
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	create_parm.in.create_options = 
		NTCREATEX_OPTIONS_DELETE_ON_CLOSE |
		NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
	create_parm.in.fname = io->unlink.in.pattern;
	if (create_parm.in.fname[0] == '\\') {
		create_parm.in.fname++;
	}

	req = smb2_create_send(tree, &create_parm);

	composite_continue_smb2(ctx, req, continue_unlink, ctx);
	return ctx;
}


/*
  composite unlink call - sync interface
*/
NTSTATUS smb2_composite_unlink(struct smb2_tree *tree, union smb_unlink *io)
{
	struct composite_context *c = smb2_composite_unlink_send(tree, io);
	return composite_wait_free(c);
}




/*
  continue after the create in a composite mkdir
 */
static void continue_mkdir(struct smb2_request *req)
{
	struct composite_context *ctx = talloc_get_type(req->async.private_data, 
							struct composite_context);
	struct smb2_tree *tree = req->tree;
	struct smb2_create create_parm;
	struct smb2_close close_parm;
	NTSTATUS status;

	status = smb2_create_recv(req, ctx, &create_parm);
	if (!NT_STATUS_IS_OK(status)) {
		composite_error(ctx, status);
		return;
	}

	ZERO_STRUCT(close_parm);
	close_parm.in.file.handle = create_parm.out.file.handle;
	
	req = smb2_close_send(tree, &close_parm);
	composite_continue_smb2(ctx, req, continue_close, ctx);
}

/*
  composite SMB2 mkdir call
*/
struct composite_context *smb2_composite_mkdir_send(struct smb2_tree *tree, 
						     union smb_mkdir *io)
{
	struct composite_context *ctx;
	struct smb2_create create_parm;
	struct smb2_request *req;

	ctx = composite_create(tree, tree->session->transport->socket->event.ctx);
	if (ctx == NULL) return NULL;

	ZERO_STRUCT(create_parm);

	create_parm.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
	create_parm.in.share_access = 
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	create_parm.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	create_parm.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
	create_parm.in.create_disposition = NTCREATEX_DISP_CREATE;
	create_parm.in.fname = io->mkdir.in.path;
	if (create_parm.in.fname[0] == '\\') {
		create_parm.in.fname++;
	}

	req = smb2_create_send(tree, &create_parm);

	composite_continue_smb2(ctx, req, continue_mkdir, ctx);

	return ctx;
}


/*
  composite mkdir call - sync interface
*/
NTSTATUS smb2_composite_mkdir(struct smb2_tree *tree, union smb_mkdir *io)
{
	struct composite_context *c = smb2_composite_mkdir_send(tree, io);
	return composite_wait_free(c);
}



/*
  continue after the create in a composite rmdir
 */
static void continue_rmdir(struct smb2_request *req)
{
	struct composite_context *ctx = talloc_get_type(req->async.private_data, 
							struct composite_context);
	struct smb2_tree *tree = req->tree;
	struct smb2_create create_parm;
	struct smb2_close close_parm;
	NTSTATUS status;

	status = smb2_create_recv(req, ctx, &create_parm);
	if (!NT_STATUS_IS_OK(status)) {
		composite_error(ctx, status);
		return;
	}

	ZERO_STRUCT(close_parm);
	close_parm.in.file.handle = create_parm.out.file.handle;
	
	req = smb2_close_send(tree, &close_parm);
	composite_continue_smb2(ctx, req, continue_close, ctx);
}

/*
  composite SMB2 rmdir call
*/
struct composite_context *smb2_composite_rmdir_send(struct smb2_tree *tree, 
						    struct smb_rmdir *io)
{
	struct composite_context *ctx;
	struct smb2_create create_parm;
	struct smb2_request *req;

	ctx = composite_create(tree, tree->session->transport->socket->event.ctx);
	if (ctx == NULL) return NULL;

	ZERO_STRUCT(create_parm);
	create_parm.in.desired_access     = SEC_STD_DELETE;
	create_parm.in.create_disposition = NTCREATEX_DISP_OPEN;
	create_parm.in.share_access = 
		NTCREATEX_SHARE_ACCESS_DELETE|
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	create_parm.in.create_options = 
		NTCREATEX_OPTIONS_DIRECTORY |
		NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
	create_parm.in.fname = io->in.path;
	if (create_parm.in.fname[0] == '\\') {
		create_parm.in.fname++;
	}

	req = smb2_create_send(tree, &create_parm);

	composite_continue_smb2(ctx, req, continue_rmdir, ctx);
	return ctx;
}


/*
  composite rmdir call - sync interface
*/
NTSTATUS smb2_composite_rmdir(struct smb2_tree *tree, struct smb_rmdir *io)
{
	struct composite_context *c = smb2_composite_rmdir_send(tree, io);
	return composite_wait_free(c);
}

struct smb2_composite_setpathinfo_state {
	struct smb2_tree *tree;
	union smb_setfileinfo io;
	NTSTATUS set_status;
	struct smb2_create cr;
	struct smb2_close cl;
};

static void smb2_composite_setpathinfo_create_done(struct smb2_request *smb2req);

/*
  composite SMB2 setpathinfo call
*/
struct tevent_req *smb2_composite_setpathinfo_send(TALLOC_CTX *mem_ctx,
						   struct tevent_context *ev,
						   struct smb2_tree *tree,
						   const union smb_setfileinfo *io)
{
	struct tevent_req *req;
	struct smb2_composite_setpathinfo_state *state;
	struct smb2_request *smb2req;

	req = tevent_req_create(mem_ctx, &state,
				struct smb2_composite_setpathinfo_state);
	if (req == NULL) {
		return NULL;
	}

	state->tree = tree;
	state->io = *io;

	state->cr.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
	state->cr.in.create_disposition = NTCREATEX_DISP_OPEN;
	state->cr.in.share_access =
		NTCREATEX_SHARE_ACCESS_DELETE|
		NTCREATEX_SHARE_ACCESS_READ|
		NTCREATEX_SHARE_ACCESS_WRITE;
	state->cr.in.create_options = 0;
	state->cr.in.fname = state->io.generic.in.file.path;
	if (state->cr.in.fname[0] == '\\') {
		state->cr.in.fname++;
	}

	smb2req = smb2_create_send(tree, &state->cr);
	if (tevent_req_nomem(smb2req, req)) {
		return tevent_req_post(req, ev);
	}
	smb2req->async.fn = smb2_composite_setpathinfo_create_done;
	smb2req->async.private_data = req;

	return req;
}

static void smb2_composite_setpathinfo_setinfo_done(struct smb2_request *smb2req);

static void smb2_composite_setpathinfo_create_done(struct smb2_request *smb2req)
{
	struct tevent_req *req =
		talloc_get_type_abort(smb2req->async.private_data,
		struct tevent_req);
	struct smb2_composite_setpathinfo_state *state =
		tevent_req_data(req,
		struct smb2_composite_setpathinfo_state);
	NTSTATUS status;

	status = smb2_create_recv(smb2req, state, &state->cr);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	state->io.generic.in.file.handle = state->cr.out.file.handle;

	smb2req = smb2_setinfo_file_send(state->tree, &state->io);
	if (tevent_req_nomem(smb2req, req)) {
		return;
	}
	smb2req->async.fn = smb2_composite_setpathinfo_setinfo_done;
	smb2req->async.private_data = req;
}

static void smb2_composite_setpathinfo_close_done(struct smb2_request *smb2req);

static void smb2_composite_setpathinfo_setinfo_done(struct smb2_request *smb2req)
{
	struct tevent_req *req =
		talloc_get_type_abort(smb2req->async.private_data,
		struct tevent_req);
	struct smb2_composite_setpathinfo_state *state =
		tevent_req_data(req,
		struct smb2_composite_setpathinfo_state);
	NTSTATUS status;

	status = smb2_setinfo_recv(smb2req);
	state->set_status = status;

	state->cl.in.file.handle = state->io.generic.in.file.handle;

	smb2req = smb2_close_send(state->tree, &state->cl);
	if (tevent_req_nomem(smb2req, req)) {
		return;
	}
	smb2req->async.fn = smb2_composite_setpathinfo_close_done;
	smb2req->async.private_data = req;
}

static void smb2_composite_setpathinfo_close_done(struct smb2_request *smb2req)
{
	struct tevent_req *req =
		talloc_get_type_abort(smb2req->async.private_data,
		struct tevent_req);
	struct smb2_composite_setpathinfo_state *state =
		tevent_req_data(req,
		struct smb2_composite_setpathinfo_state);
	NTSTATUS status;

	status = smb2_close_recv(smb2req, &state->cl);

	if (tevent_req_nterror(req, state->set_status)) {
		return;
	}

	if (tevent_req_nterror(req, status)) {
		return;
	}

	tevent_req_done(req);
}

NTSTATUS smb2_composite_setpathinfo_recv(struct tevent_req *req)
{
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		tevent_req_received(req);
		return status;
	}

	tevent_req_received(req);
	return NT_STATUS_OK;
}

/*
  composite setpathinfo call
 */
NTSTATUS smb2_composite_setpathinfo(struct smb2_tree *tree, union smb_setfileinfo *io)
{
	struct tevent_req *subreq;
	NTSTATUS status;
	bool ok;
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev = tree->session->transport->socket->event.ctx;

	if (frame == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	subreq = smb2_composite_setpathinfo_send(frame, ev, tree, io);
	if (subreq == NULL) {
		TALLOC_FREE(frame);
		return NT_STATUS_NO_MEMORY;
	}

	ok = tevent_req_poll(subreq, ev);
	if (!ok) {
		status = map_nt_error_from_unix_common(errno);
		TALLOC_FREE(frame);
		return status;
	}

	status = smb2_composite_setpathinfo_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return status;
	}

	TALLOC_FREE(frame);
	return NT_STATUS_OK;
}