2 Unix SMB/CIFS implementation.
3 NT transaction handling
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) James J Myers 2003 <myersjj@samba.org>
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 This file handles the parsing of transact2 requests
25 #include "smb_server/smb_server.h"
26 #include "ntvfs/ntvfs.h"
27 #include "libcli/raw/libcliraw.h"
28 #include "librpc/gen_ndr/ndr_security.h"
31 hold the state of a nttrans op while in progress. Needed to allow for async backend
35 struct smb_nttrans *trans;
36 NTSTATUS (*send_fn)(struct nttrans_op *);
41 /* setup a nttrans reply, given the data and params sizes */
42 static NTSTATUS nttrans_setup_reply(struct nttrans_op *op,
43 struct smb_nttrans *trans,
44 uint32_t param_size, uint32_t data_size,
47 trans->out.setup_count = setup_count;
48 if (setup_count != 0) {
49 trans->out.setup = talloc_zero_array(op, uint8_t, setup_count*2);
50 NT_STATUS_HAVE_NO_MEMORY(trans->out.setup);
52 trans->out.params = data_blob_talloc(op, NULL, param_size);
53 if (param_size != 0) {
54 NT_STATUS_HAVE_NO_MEMORY(trans->out.params.data);
56 trans->out.data = data_blob_talloc(op, NULL, data_size);
58 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
64 send a nttrans create reply
66 static NTSTATUS nttrans_create_send(struct nttrans_op *op)
68 union smb_open *io = talloc_get_type(op->op_info, union smb_open);
72 status = nttrans_setup_reply(op, op->trans, 69, 0, 0);
73 NT_STATUS_NOT_OK_RETURN(status);
74 params = op->trans->out.params.data;
76 SSVAL(params, 0, io->ntcreatex.out.oplock_level);
77 smbsrv_push_fnum(params, 2, io->ntcreatex.out.file.ntvfs);
78 SIVAL(params, 4, io->ntcreatex.out.create_action);
79 SIVAL(params, 8, 0); /* ea error offset */
80 push_nttime(params, 12, io->ntcreatex.out.create_time);
81 push_nttime(params, 20, io->ntcreatex.out.access_time);
82 push_nttime(params, 28, io->ntcreatex.out.write_time);
83 push_nttime(params, 36, io->ntcreatex.out.change_time);
84 SIVAL(params, 44, io->ntcreatex.out.attrib);
85 SBVAL(params, 48, io->ntcreatex.out.alloc_size);
86 SBVAL(params, 56, io->ntcreatex.out.size);
87 SSVAL(params, 64, io->ntcreatex.out.file_type);
88 SSVAL(params, 66, io->ntcreatex.out.ipc_state);
89 SCVAL(params, 68, io->ntcreatex.out.is_directory);
95 parse NTTRANS_CREATE request
97 static NTSTATUS nttrans_create(struct smbsrv_request *req,
98 struct nttrans_op *op)
100 struct smb_nttrans *trans = op->trans;
103 uint32_t sd_length, ea_length;
107 if (trans->in.params.length < 54) {
108 return NT_STATUS_INVALID_PARAMETER;
111 /* parse the request */
112 io = talloc(op, union smb_open);
113 NT_STATUS_HAVE_NO_MEMORY(io);
115 io->ntcreatex.level = RAW_OPEN_NTTRANS_CREATE;
117 params = trans->in.params.data;
119 io->ntcreatex.in.flags = IVAL(params, 0);
120 io->ntcreatex.in.root_fid = IVAL(params, 4);
121 io->ntcreatex.in.access_mask = IVAL(params, 8);
122 io->ntcreatex.in.alloc_size = BVAL(params, 12);
123 io->ntcreatex.in.file_attr = IVAL(params, 20);
124 io->ntcreatex.in.share_access = IVAL(params, 24);
125 io->ntcreatex.in.open_disposition = IVAL(params, 28);
126 io->ntcreatex.in.create_options = IVAL(params, 32);
127 sd_length = IVAL(params, 36);
128 ea_length = IVAL(params, 40);
129 fname_len = IVAL(params, 44);
130 io->ntcreatex.in.impersonation = IVAL(params, 48);
131 io->ntcreatex.in.security_flags = CVAL(params, 52);
132 io->ntcreatex.in.sec_desc = NULL;
133 io->ntcreatex.in.ea_list = NULL;
135 req_pull_string(req, &io->ntcreatex.in.fname,
137 MIN(fname_len+1, trans->in.params.length - 53),
138 STR_NO_RANGE_CHECK | STR_TERMINATE);
139 if (!io->ntcreatex.in.fname) {
140 return NT_STATUS_INVALID_PARAMETER;
143 if (sd_length > trans->in.data.length ||
144 ea_length > trans->in.data.length ||
145 (sd_length+ea_length) > trans->in.data.length) {
146 return NT_STATUS_INVALID_PARAMETER;
149 /* this call has an optional security descriptor */
150 if (sd_length != 0) {
152 blob.data = trans->in.data.data;
153 blob.length = sd_length;
154 io->ntcreatex.in.sec_desc = talloc(io, struct security_descriptor);
155 if (io->ntcreatex.in.sec_desc == NULL) {
156 return NT_STATUS_NO_MEMORY;
158 status = ndr_pull_struct_blob(&blob, io,
159 io->ntcreatex.in.sec_desc,
160 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
161 if (!NT_STATUS_IS_OK(status)) {
166 /* and an optional ea_list */
169 blob.data = trans->in.data.data + sd_length;
170 blob.length = ea_length;
171 io->ntcreatex.in.ea_list = talloc(io, struct smb_ea_list);
172 if (io->ntcreatex.in.ea_list == NULL) {
173 return NT_STATUS_NO_MEMORY;
176 status = ea_pull_list_chained(&blob, io,
177 &io->ntcreatex.in.ea_list->num_eas,
178 &io->ntcreatex.in.ea_list->eas);
179 if (!NT_STATUS_IS_OK(status)) {
184 op->send_fn = nttrans_create_send;
187 return ntvfs_open(req->ntvfs, io);
192 send NTTRANS_QUERY_SEC_DESC reply
194 static NTSTATUS nttrans_query_sec_desc_send(struct nttrans_op *op)
196 union smb_fileinfo *io = talloc_get_type(op->op_info, union smb_fileinfo);
200 status = nttrans_setup_reply(op, op->trans, 4, 0, 0);
201 NT_STATUS_NOT_OK_RETURN(status);
202 params = op->trans->out.params.data;
204 status = ndr_push_struct_blob(&op->trans->out.data, op,
205 io->query_secdesc.out.sd,
206 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
207 NT_STATUS_NOT_OK_RETURN(status);
209 SIVAL(params, 0, op->trans->out.data.length);
215 parse NTTRANS_QUERY_SEC_DESC request
217 static NTSTATUS nttrans_query_sec_desc(struct smbsrv_request *req,
218 struct nttrans_op *op)
220 struct smb_nttrans *trans = op->trans;
221 union smb_fileinfo *io;
223 if (trans->in.params.length < 8) {
224 return NT_STATUS_INVALID_PARAMETER;
227 /* parse the request */
228 io = talloc(op, union smb_fileinfo);
229 NT_STATUS_HAVE_NO_MEMORY(io);
231 io->query_secdesc.level = RAW_FILEINFO_SEC_DESC;
232 io->query_secdesc.in.file.ntvfs = smbsrv_pull_fnum(req, trans->in.params.data, 0);
233 io->query_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
236 op->send_fn = nttrans_query_sec_desc_send;
238 SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(io->query_secdesc.in.file.ntvfs);
239 return ntvfs_qfileinfo(req->ntvfs, io);
244 parse NTTRANS_SET_SEC_DESC request
246 static NTSTATUS nttrans_set_sec_desc(struct smbsrv_request *req,
247 struct nttrans_op *op)
249 struct smb_nttrans *trans = op->trans;
250 union smb_setfileinfo *io;
253 if (trans->in.params.length < 8) {
254 return NT_STATUS_INVALID_PARAMETER;
257 /* parse the request */
258 io = talloc(req, union smb_setfileinfo);
259 NT_STATUS_HAVE_NO_MEMORY(io);
261 io->set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
262 io->set_secdesc.in.file.ntvfs = smbsrv_pull_fnum(req, trans->in.params.data, 0);
263 io->set_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
265 io->set_secdesc.in.sd = talloc(io, struct security_descriptor);
266 NT_STATUS_HAVE_NO_MEMORY(io->set_secdesc.in.sd);
268 status = ndr_pull_struct_blob(&trans->in.data, req,
269 io->set_secdesc.in.sd,
270 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
271 NT_STATUS_NOT_OK_RETURN(status);
273 SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(io->set_secdesc.in.file.ntvfs);
274 return ntvfs_setfileinfo(req->ntvfs, io);
278 /* parse NTTRANS_RENAME request
280 static NTSTATUS nttrans_rename(struct smbsrv_request *req,
281 struct nttrans_op *op)
283 return NT_STATUS_NOT_IMPLEMENTED;
287 parse NTTRANS_IOCTL send
289 static NTSTATUS nttrans_ioctl_send(struct nttrans_op *op)
291 union smb_ioctl *info = talloc_get_type(op->op_info, union smb_ioctl);
295 * we pass 0 as data_count here,
296 * because we reuse the DATA_BLOB from the smb_ioctl
299 status = nttrans_setup_reply(op, op->trans, 0, 0, 1);
300 NT_STATUS_NOT_OK_RETURN(status);
302 op->trans->out.setup[0] = 0;
303 op->trans->out.data = info->ntioctl.out.blob;
310 parse NTTRANS_IOCTL request
312 static NTSTATUS nttrans_ioctl(struct smbsrv_request *req,
313 struct nttrans_op *op)
315 struct smb_nttrans *trans = op->trans;
318 /* should have at least 4 setup words */
319 if (trans->in.setup_count != 4) {
320 return NT_STATUS_INVALID_PARAMETER;
323 nt = talloc(op, union smb_ioctl);
324 NT_STATUS_HAVE_NO_MEMORY(nt);
326 nt->ntioctl.level = RAW_IOCTL_NTIOCTL;
327 nt->ntioctl.in.function = IVAL(trans->in.setup, 0);
328 nt->ntioctl.in.file.ntvfs = smbsrv_pull_fnum(req, (uint8_t *)trans->in.setup, 4);
329 nt->ntioctl.in.fsctl = CVAL(trans->in.setup, 6);
330 nt->ntioctl.in.filter = CVAL(trans->in.setup, 7);
331 nt->ntioctl.in.max_data = trans->in.max_data;
332 nt->ntioctl.in.blob = trans->in.data;
335 op->send_fn = nttrans_ioctl_send;
337 SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(nt->ntioctl.in.file.ntvfs);
338 return ntvfs_ioctl(req->ntvfs, nt);
343 send NTTRANS_NOTIFY_CHANGE reply
345 static NTSTATUS nttrans_notify_change_send(struct nttrans_op *op)
347 union smb_notify *info = talloc_get_type(op->op_info, union smb_notify);
352 #define MAX_BYTES_PER_CHAR 3
354 /* work out how big the reply buffer could be */
355 for (i=0;i<info->nttrans.out.num_changes;i++) {
356 size += 12 + 3 + (1+strlen(info->nttrans.out.changes[i].name.s)) * MAX_BYTES_PER_CHAR;
359 status = nttrans_setup_reply(op, op->trans, size, 0, 0);
360 NT_STATUS_NOT_OK_RETURN(status);
361 p = op->trans->out.params.data;
363 /* construct the changes buffer */
364 for (i=0;i<info->nttrans.out.num_changes;i++) {
368 SIVAL(p, 4, info->nttrans.out.changes[i].action);
369 len = push_string(p + 12, info->nttrans.out.changes[i].name.s,
370 op->trans->out.params.length -
371 (p+12 - op->trans->out.params.data), STR_UNICODE);
377 int pad = 4 - (ofs & 3);
378 memset(p+ofs, 0, pad);
382 if (i == info->nttrans.out.num_changes-1) {
391 op->trans->out.params.length = p - op->trans->out.params.data;
397 parse NTTRANS_NOTIFY_CHANGE request
399 static NTSTATUS nttrans_notify_change(struct smbsrv_request *req,
400 struct nttrans_op *op)
402 struct smb_nttrans *trans = op->trans;
403 union smb_notify *info;
405 /* should have at least 4 setup words */
406 if (trans->in.setup_count != 4) {
407 return NT_STATUS_INVALID_PARAMETER;
410 info = talloc(op, union smb_notify);
411 NT_STATUS_HAVE_NO_MEMORY(info);
413 info->nttrans.level = RAW_NOTIFY_NTTRANS;
414 info->nttrans.in.completion_filter = IVAL(trans->in.setup, 0);
415 info->nttrans.in.file.ntvfs = smbsrv_pull_fnum(req, (uint8_t *)trans->in.setup, 4);
416 info->nttrans.in.recursive = SVAL(trans->in.setup, 6);
417 info->nttrans.in.buffer_size = trans->in.max_param;
420 op->send_fn = nttrans_notify_change_send;
422 SMBSRV_CHECK_FILE_HANDLE_NTSTATUS(info->nttrans.in.file.ntvfs);
423 return ntvfs_notify(req->ntvfs, info);
427 backend for nttrans requests
429 static NTSTATUS nttrans_backend(struct smbsrv_request *req,
430 struct nttrans_op *op)
432 /* the nttrans command is in function */
433 switch (op->trans->in.function) {
434 case NT_TRANSACT_CREATE:
435 return nttrans_create(req, op);
436 case NT_TRANSACT_IOCTL:
437 return nttrans_ioctl(req, op);
438 case NT_TRANSACT_RENAME:
439 return nttrans_rename(req, op);
440 case NT_TRANSACT_QUERY_SECURITY_DESC:
441 return nttrans_query_sec_desc(req, op);
442 case NT_TRANSACT_SET_SECURITY_DESC:
443 return nttrans_set_sec_desc(req, op);
444 case NT_TRANSACT_NOTIFY_CHANGE:
445 return nttrans_notify_change(req, op);
448 /* an unknown nttrans command */
449 return NT_STATUS_DOS(ERRSRV, ERRerror);
453 static void reply_nttrans_send(struct ntvfs_request *ntvfs)
455 struct smbsrv_request *req;
456 uint16_t params_left, data_left;
457 uint8_t *params, *data;
458 struct smb_nttrans *trans;
459 struct nttrans_op *op;
461 SMBSRV_CHECK_ASYNC_STATUS(op, struct nttrans_op);
465 /* if this function needs work to form the nttrans reply buffer, then
467 if (op->send_fn != NULL) {
469 status = op->send_fn(op);
470 if (!NT_STATUS_IS_OK(status)) {
471 smbsrv_send_error(req, status);
476 smbsrv_setup_reply(req, 18 + trans->out.setup_count, 0);
478 /* note that we don't check the max_setup count (matching w2k3
481 if (trans->out.params.length > trans->in.max_param) {
482 smbsrv_setup_error(req, NT_STATUS_BUFFER_TOO_SMALL);
483 trans->out.params.length = trans->in.max_param;
485 if (trans->out.data.length > trans->in.max_data) {
486 smbsrv_setup_error(req, NT_STATUS_BUFFER_TOO_SMALL);
487 trans->out.data.length = trans->in.max_data;
490 params_left = trans->out.params.length;
491 data_left = trans->out.data.length;
492 params = trans->out.params.data;
493 data = trans->out.data.data;
495 /* we need to divide up the reply into chunks that fit into
496 the negotiated buffer size */
498 uint16_t this_data, this_param, max_bytes;
499 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
500 struct smbsrv_request *this_req;
502 max_bytes = req_max_data(req) - (align1 + align2);
504 this_param = params_left;
505 if (this_param > max_bytes) {
506 this_param = max_bytes;
508 max_bytes -= this_param;
510 this_data = data_left;
511 if (this_data > max_bytes) {
512 this_data = max_bytes;
515 /* don't destroy unless this is the last chunk */
516 if (params_left - this_param != 0 ||
517 data_left - this_data != 0) {
518 this_req = smbsrv_setup_secondary_request(req);
523 req_grow_data(req, this_param + this_data + (align1 + align2));
525 SSVAL(this_req->out.vwv, 0, 0); /* reserved */
526 SCVAL(this_req->out.vwv, 2, 0); /* reserved */
527 SIVAL(this_req->out.vwv, 3, trans->out.params.length);
528 SIVAL(this_req->out.vwv, 7, trans->out.data.length);
530 SIVAL(this_req->out.vwv, 11, this_param);
531 SIVAL(this_req->out.vwv, 15, align1 + PTR_DIFF(this_req->out.data, this_req->out.hdr));
532 SIVAL(this_req->out.vwv, 19, PTR_DIFF(params, trans->out.params.data));
534 SIVAL(this_req->out.vwv, 23, this_data);
535 SIVAL(this_req->out.vwv, 27, align1 + align2 +
536 PTR_DIFF(this_req->out.data + this_param, this_req->out.hdr));
537 SIVAL(this_req->out.vwv, 31, PTR_DIFF(data, trans->out.data.data));
539 SCVAL(this_req->out.vwv, 35, trans->out.setup_count);
540 memcpy((char *)(this_req->out.vwv) + VWV(18), trans->out.setup,
541 sizeof(uint16_t) * trans->out.setup_count);
542 memset(this_req->out.data, 0, align1);
543 if (this_param != 0) {
544 memcpy(this_req->out.data + align1, params, this_param);
546 memset(this_req->out.data+this_param+align1, 0, align2);
547 if (this_data != 0) {
548 memcpy(this_req->out.data+this_param+align1+align2,
552 params_left -= this_param;
553 data_left -= this_data;
554 params += this_param;
557 smbsrv_send_reply(this_req);
558 } while (params_left != 0 || data_left != 0);
562 /****************************************************************************
563 Reply to an SMBnttrans request
564 ****************************************************************************/
565 void smbsrv_reply_nttrans(struct smbsrv_request *req)
567 struct nttrans_op *op;
568 struct smb_nttrans *trans;
569 uint16_t param_ofs, data_ofs;
570 uint16_t param_count, data_count;
571 uint16_t param_total, data_total;
574 if (req->in.wct < 19) {
575 smbsrv_send_error(req, NT_STATUS_FOOBAR);
579 SMBSRV_TALLOC_IO_PTR(op, struct nttrans_op);
580 SMBSRV_SETUP_NTVFS_REQUEST(reply_nttrans_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
582 trans = talloc(op, struct smb_nttrans);
584 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
592 trans->in.max_setup = CVAL(req->in.vwv, 0);
593 param_total = IVAL(req->in.vwv, 3);
594 data_total = IVAL(req->in.vwv, 7);
595 trans->in.max_param = IVAL(req->in.vwv, 11);
596 trans->in.max_data = IVAL(req->in.vwv, 15);
597 param_count = IVAL(req->in.vwv, 19);
598 param_ofs = IVAL(req->in.vwv, 23);
599 data_count = IVAL(req->in.vwv, 27);
600 data_ofs = IVAL(req->in.vwv, 31);
601 trans->in.setup_count= CVAL(req->in.vwv, 35);
602 trans->in.function = SVAL(req->in.vwv, 36);
604 if (req->in.wct != 19 + trans->in.setup_count) {
605 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror));
609 /* parse out the setup words */
610 trans->in.setup = talloc_array(req, uint8_t, trans->in.setup_count*2);
611 if (!trans->in.setup) {
612 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
615 memcpy(trans->in.setup, (char *)(req->in.vwv) + VWV(19),
616 sizeof(uint16_t) * trans->in.setup_count);
618 if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans->in.params) ||
619 !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans->in.data)) {
620 smbsrv_send_error(req, NT_STATUS_FOOBAR);
624 /* is it a partial request? if so, then send a 'send more' message */
625 if (param_total > param_count ||
626 data_total > data_count) {
627 DEBUG(0,("REWRITE: not handling partial nttrans requests!\n"));
628 smbsrv_send_error(req, NT_STATUS_FOOBAR);
632 ZERO_STRUCT(trans->out);
633 SMBSRV_CALL_NTVFS_BACKEND(nttrans_backend(req, op));
637 /****************************************************************************
638 Reply to an SMBnttranss request
639 ****************************************************************************/
640 void smbsrv_reply_nttranss(struct smbsrv_request *req)
642 smbsrv_send_error(req, NT_STATUS_FOOBAR);