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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 This file handles the parsing of transact2 requests
26 #include "smb_server/smb_server.h"
27 #include "ntvfs/ntvfs.h"
28 #include "libcli/raw/libcliraw.h"
29 #include "librpc/gen_ndr/ndr_security.h"
32 hold the state of a nttrans op while in progress. Needed to allow for async backend
36 struct smb_nttrans *trans;
37 NTSTATUS (*send_fn)(struct nttrans_op *);
42 /* setup a nttrans reply, given the data and params sizes */
43 static NTSTATUS nttrans_setup_reply(struct nttrans_op *op,
44 struct smb_nttrans *trans,
45 uint16_t param_size, uint16_t data_size,
48 trans->out.setup_count = setup_count;
49 if (setup_count != 0) {
50 trans->out.setup = talloc_zero_array(op, uint16_t, setup_count);
51 NT_STATUS_HAVE_NO_MEMORY(trans->out.setup);
53 trans->out.params = data_blob_talloc(op, NULL, param_size);
54 if (param_size != 0) {
55 NT_STATUS_HAVE_NO_MEMORY(trans->out.params.data);
57 trans->out.data = data_blob_talloc(op, NULL, data_size);
59 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
65 send a nttrans create reply
67 static NTSTATUS nttrans_create_send(struct nttrans_op *op)
69 union smb_open *io = talloc_get_type(op->op_info, union smb_open);
72 op->trans->out.setup_count = 0;
73 op->trans->out.setup = NULL;
74 op->trans->out.params = data_blob_talloc(op, NULL, 69);
75 op->trans->out.data = data_blob(NULL, 0);
77 params = op->trans->out.params.data;
78 NT_STATUS_HAVE_NO_MEMORY(params);
80 SSVAL(params, 0, io->ntcreatex.out.oplock_level);
81 SSVAL(params, 2, io->ntcreatex.out.file.fnum);
82 SIVAL(params, 4, io->ntcreatex.out.create_action);
83 SIVAL(params, 8, 0); /* ea error offset */
84 push_nttime(params, 12, io->ntcreatex.out.create_time);
85 push_nttime(params, 20, io->ntcreatex.out.access_time);
86 push_nttime(params, 28, io->ntcreatex.out.write_time);
87 push_nttime(params, 36, io->ntcreatex.out.change_time);
88 SIVAL(params, 44, io->ntcreatex.out.attrib);
89 SBVAL(params, 48, io->ntcreatex.out.alloc_size);
90 SBVAL(params, 56, io->ntcreatex.out.size);
91 SSVAL(params, 64, io->ntcreatex.out.file_type);
92 SSVAL(params, 66, io->ntcreatex.out.ipc_state);
93 SCVAL(params, 68, io->ntcreatex.out.is_directory);
99 parse NTTRANS_CREATE request
101 static NTSTATUS nttrans_create(struct smbsrv_request *req,
102 struct nttrans_op *op)
104 struct smb_nttrans *trans = op->trans;
107 uint32_t sd_length, ea_length;
111 if (trans->in.params.length < 54) {
112 return NT_STATUS_INVALID_PARAMETER;
115 /* parse the request */
116 io = talloc(op, union smb_open);
117 NT_STATUS_HAVE_NO_MEMORY(io);
119 io->ntcreatex.level = RAW_OPEN_NTTRANS_CREATE;
121 params = trans->in.params.data;
123 io->ntcreatex.in.flags = IVAL(params, 0);
124 io->ntcreatex.in.root_fid = IVAL(params, 4);
125 io->ntcreatex.in.access_mask = IVAL(params, 8);
126 io->ntcreatex.in.alloc_size = BVAL(params, 12);
127 io->ntcreatex.in.file_attr = IVAL(params, 20);
128 io->ntcreatex.in.share_access = IVAL(params, 24);
129 io->ntcreatex.in.open_disposition = IVAL(params, 28);
130 io->ntcreatex.in.create_options = IVAL(params, 32);
131 sd_length = IVAL(params, 36);
132 ea_length = IVAL(params, 40);
133 fname_len = IVAL(params, 44);
134 io->ntcreatex.in.impersonation = IVAL(params, 48);
135 io->ntcreatex.in.security_flags = CVAL(params, 52);
136 io->ntcreatex.in.sec_desc = NULL;
137 io->ntcreatex.in.ea_list = NULL;
139 req_pull_string(req, &io->ntcreatex.in.fname,
141 MIN(fname_len+1, trans->in.params.length - 53),
142 STR_NO_RANGE_CHECK | STR_TERMINATE);
143 if (!io->ntcreatex.in.fname) {
144 return NT_STATUS_INVALID_PARAMETER;
147 if (sd_length > trans->in.data.length ||
148 ea_length > trans->in.data.length ||
149 (sd_length+ea_length) > trans->in.data.length) {
150 return NT_STATUS_INVALID_PARAMETER;
153 /* this call has an optional security descriptor */
154 if (sd_length != 0) {
156 blob.data = trans->in.data.data;
157 blob.length = sd_length;
158 io->ntcreatex.in.sec_desc = talloc(io, struct security_descriptor);
159 if (io->ntcreatex.in.sec_desc == NULL) {
160 return NT_STATUS_NO_MEMORY;
162 status = ndr_pull_struct_blob(&blob, io,
163 io->ntcreatex.in.sec_desc,
164 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
165 if (!NT_STATUS_IS_OK(status)) {
170 /* and an optional ea_list */
173 blob.data = trans->in.data.data + sd_length;
174 blob.length = ea_length;
175 io->ntcreatex.in.ea_list = talloc(io, struct smb_ea_list);
176 if (io->ntcreatex.in.ea_list == NULL) {
177 return NT_STATUS_NO_MEMORY;
180 status = ea_pull_list_chained(&blob, io,
181 &io->ntcreatex.in.ea_list->num_eas,
182 &io->ntcreatex.in.ea_list->eas);
183 if (!NT_STATUS_IS_OK(status)) {
188 op->send_fn = nttrans_create_send;
191 return ntvfs_open(req, io);
196 send NTTRANS_QUERY_SEC_DESC reply
198 static NTSTATUS nttrans_query_sec_desc_send(struct nttrans_op *op)
200 struct smb_nttrans *trans = op->trans;
201 union smb_fileinfo *io = talloc_get_type(op->op_info, union smb_fileinfo);
204 trans->out.setup_count = 0;
205 trans->out.setup = NULL;
206 trans->out.params = data_blob_talloc(op, NULL, 4);
207 trans->out.data = data_blob(NULL, 0);
208 NT_STATUS_HAVE_NO_MEMORY(trans->out.params.data);
210 status = ndr_push_struct_blob(&trans->out.data, op,
211 io->query_secdesc.out.sd,
212 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
213 NT_STATUS_NOT_OK_RETURN(status);
215 SIVAL(trans->out.params.data, 0, trans->out.data.length);
221 parse NTTRANS_QUERY_SEC_DESC request
223 static NTSTATUS nttrans_query_sec_desc(struct smbsrv_request *req,
224 struct nttrans_op *op)
226 struct smb_nttrans *trans = op->trans;
227 union smb_fileinfo *io;
229 if (trans->in.params.length < 8) {
230 return NT_STATUS_INVALID_PARAMETER;
233 /* parse the request */
234 io = talloc(op, union smb_fileinfo);
235 NT_STATUS_HAVE_NO_MEMORY(io);
237 io->query_secdesc.level = RAW_FILEINFO_SEC_DESC;
238 io->query_secdesc.in.file.fnum = SVAL(trans->in.params.data, 0);
239 io->query_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
242 op->send_fn = nttrans_query_sec_desc_send;
244 return ntvfs_qfileinfo(req, io);
249 parse NTTRANS_SET_SEC_DESC request
251 static NTSTATUS nttrans_set_sec_desc(struct smbsrv_request *req,
252 struct nttrans_op *op)
254 struct smb_nttrans *trans = op->trans;
255 union smb_setfileinfo *io;
258 if (trans->in.params.length < 8) {
259 return NT_STATUS_INVALID_PARAMETER;
262 /* parse the request */
263 io = talloc(req, union smb_setfileinfo);
264 NT_STATUS_HAVE_NO_MEMORY(io);
266 io->set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
267 io->set_secdesc.in.file.fnum = SVAL(trans->in.params.data, 0);
268 io->set_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
270 io->set_secdesc.in.sd = talloc(io, struct security_descriptor);
271 NT_STATUS_HAVE_NO_MEMORY(io->set_secdesc.in.sd);
273 status = ndr_pull_struct_blob(&trans->in.data, req,
274 io->set_secdesc.in.sd,
275 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
276 NT_STATUS_NOT_OK_RETURN(status);
278 trans->out.setup_count = 0;
279 trans->out.setup = NULL;
280 trans->out.params = data_blob(NULL, 0);
281 trans->out.data = data_blob(NULL, 0);
283 return ntvfs_setfileinfo(req, io);
287 /* parse NTTRANS_RENAME request
289 static NTSTATUS nttrans_rename(struct smbsrv_request *req,
290 struct nttrans_op *op)
292 return NT_STATUS_NOT_IMPLEMENTED;
296 parse NTTRANS_IOCTL request
298 static NTSTATUS nttrans_ioctl(struct smbsrv_request *req,
299 struct nttrans_op *op)
301 struct smb_nttrans *trans = op->trans;
310 /* should have at least 4 setup words */
311 if (trans->in.setup_count != 4) {
312 return NT_STATUS_INVALID_PARAMETER;
315 nt = talloc(op, union smb_ioctl);
316 NT_STATUS_HAVE_NO_MEMORY(nt);
318 function = IVAL(trans->in.setup, 0);
319 fnum = SVAL(trans->in.setup, 4);
320 fsctl = CVAL(trans->in.setup, 6);
321 filter = CVAL(trans->in.setup, 7);
323 blob = &trans->in.data;
325 nt->ntioctl.level = RAW_IOCTL_NTIOCTL;
326 nt->ntioctl.in.file.fnum = fnum;
327 nt->ntioctl.in.function = function;
328 nt->ntioctl.in.fsctl = fsctl;
329 nt->ntioctl.in.filter = filter;
331 status = nttrans_setup_reply(op, trans, 0, 0, 1);
332 NT_STATUS_NOT_OK_RETURN(status);
334 trans->out.setup[0] = 0;
336 return ntvfs_ioctl(req, nt);
341 send NTTRANS_NOTIFY_CHANGE reply
343 static NTSTATUS nttrans_notify_change_send(struct nttrans_op *op)
345 union smb_notify *info = talloc_get_type(op->op_info, union smb_notify);
351 #define MAX_BYTES_PER_CHAR 3
353 /* work out how big the reply buffer could be */
354 for (i=0;i<info->notify.out.num_changes;i++) {
355 size += 12 + 3 + (1+strlen(info->notify.out.changes[i].name.s)) * MAX_BYTES_PER_CHAR;
358 status = nttrans_setup_reply(op, op->trans, size, 0, 0);
359 NT_STATUS_NOT_OK_RETURN(status);
361 p = op->trans->out.params.data;
363 /* construct the changes buffer */
364 for (i=0;i<info->notify.out.num_changes;i++) {
367 SIVAL(p, 4, info->notify.out.changes[i].action);
368 len = push_string(p + 12, info->notify.out.changes[i].name.s,
369 op->trans->out.params.length - (ofs+12), STR_UNICODE);
375 int pad = 4 - (ofs & 3);
376 memset(p+ofs, 0, pad);
382 p = op->trans->out.params.data + ofs;
385 op->trans->out.params.length = ofs;
391 parse NTTRANS_NOTIFY_CHANGE request
393 static NTSTATUS nttrans_notify_change(struct smbsrv_request *req,
394 struct nttrans_op *op)
396 struct smb_nttrans *trans = op->trans;
397 union smb_notify *info;
399 /* should have at least 4 setup words */
400 if (trans->in.setup_count != 4) {
401 return NT_STATUS_INVALID_PARAMETER;
404 info = talloc(op, union smb_notify);
405 NT_STATUS_HAVE_NO_MEMORY(info);
407 info->notify.in.completion_filter = IVAL(trans->in.setup, 0);
408 info->notify.in.file.fnum = SVAL(trans->in.setup, 4);
409 info->notify.in.recursive = SVAL(trans->in.setup, 6);
410 info->notify.in.buffer_size = trans->in.max_param;
413 op->send_fn = nttrans_notify_change_send;
415 return ntvfs_notify(req, info);
419 backend for nttrans requests
421 static NTSTATUS nttrans_backend(struct smbsrv_request *req,
422 struct nttrans_op *op)
424 /* the nttrans command is in function */
425 switch (op->trans->in.function) {
426 case NT_TRANSACT_CREATE:
427 return nttrans_create(req, op);
428 case NT_TRANSACT_IOCTL:
429 return nttrans_ioctl(req, op);
430 case NT_TRANSACT_RENAME:
431 return nttrans_rename(req, op);
432 case NT_TRANSACT_QUERY_SECURITY_DESC:
433 return nttrans_query_sec_desc(req, op);
434 case NT_TRANSACT_SET_SECURITY_DESC:
435 return nttrans_set_sec_desc(req, op);
436 case NT_TRANSACT_NOTIFY_CHANGE:
437 return nttrans_notify_change(req, op);
440 /* an unknown nttrans command */
441 return NT_STATUS_DOS(ERRSRV, ERRerror);
445 static void reply_nttrans_send(struct smbsrv_request *req)
447 uint16_t params_left, data_left;
448 uint8_t *params, *data;
449 struct smb_nttrans *trans;
450 struct nttrans_op *op;
452 if (!NT_STATUS_IS_OK(req->async_states->status)) {
453 smbsrv_send_error(req, req->async_states->status);
457 op = talloc_get_type(req->async_states->private_data, struct nttrans_op);
460 /* if this function needs work to form the nttrans reply buffer, then
462 if (op->send_fn != NULL) {
464 status = op->send_fn(op);
465 if (!NT_STATUS_IS_OK(status)) {
466 smbsrv_send_error(req, status);
471 /* note that we don't check the max_setup count (matching w2k3
474 if (trans->out.params.length > trans->in.max_param) {
475 smbsrv_setup_error(req, NT_STATUS_BUFFER_TOO_SMALL);
476 trans->out.params.length = trans->in.max_param;
478 if (trans->out.data.length > trans->in.max_data) {
479 smbsrv_setup_error(req, NT_STATUS_BUFFER_TOO_SMALL);
480 trans->out.data.length = trans->in.max_data;
483 params_left = trans->out.params.length;
484 data_left = trans->out.data.length;
485 params = trans->out.params.data;
486 data = trans->out.data.data;
488 smbsrv_setup_reply(req, 18 + trans->out.setup_count, 0);
490 /* we need to divide up the reply into chunks that fit into
491 the negotiated buffer size */
493 uint16_t this_data, this_param, max_bytes;
494 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
495 struct smbsrv_request *this_req;
498 max_bytes = req_max_data(req) - (align1 + align2);
500 this_param = params_left;
501 if (this_param > max_bytes) {
502 this_param = max_bytes;
504 max_bytes -= this_param;
506 this_data = data_left;
507 if (this_data > max_bytes) {
508 this_data = max_bytes;
511 /* don't destroy unless this is the last chunk */
512 if (params_left - this_param != 0 ||
513 data_left - this_data != 0) {
514 this_req = smbsrv_setup_secondary_request(req);
519 req_grow_data(req, this_param + this_data + (align1 + align2));
521 SSVAL(this_req->out.vwv, 0, 0); /* reserved */
522 SCVAL(this_req->out.vwv, 2, 0); /* reserved */
523 SIVAL(this_req->out.vwv, 3, trans->out.params.length);
524 SIVAL(this_req->out.vwv, 7, trans->out.data.length);
526 SIVAL(this_req->out.vwv, 11, this_param);
527 SIVAL(this_req->out.vwv, 15, align1 + PTR_DIFF(this_req->out.data, this_req->out.hdr));
528 SIVAL(this_req->out.vwv, 19, PTR_DIFF(params, trans->out.params.data));
530 SIVAL(this_req->out.vwv, 23, this_data);
531 SIVAL(this_req->out.vwv, 27, align1 + align2 +
532 PTR_DIFF(this_req->out.data + this_param, this_req->out.hdr));
533 SIVAL(this_req->out.vwv, 31, PTR_DIFF(data, trans->out.data.data));
535 SCVAL(this_req->out.vwv, 35, trans->out.setup_count);
536 for (i=0;i<trans->out.setup_count;i++) {
537 SSVAL(this_req->out.vwv, VWV(18+i), trans->out.setup[i]);
540 memset(this_req->out.data, 0, align1);
541 if (this_param != 0) {
542 memcpy(this_req->out.data + align1, params, this_param);
544 memset(this_req->out.data+this_param+align1, 0, align2);
545 if (this_data != 0) {
546 memcpy(this_req->out.data+this_param+align1+align2,
550 params_left -= this_param;
551 data_left -= this_data;
552 params += this_param;
555 smbsrv_send_reply(this_req);
556 } while (params_left != 0 || data_left != 0);
560 /****************************************************************************
561 Reply to an SMBnttrans request
562 ****************************************************************************/
563 void smbsrv_reply_nttrans(struct smbsrv_request *req)
565 struct nttrans_op *op;
566 struct smb_nttrans *trans;
568 uint16_t param_ofs, data_ofs;
569 uint16_t param_count, data_count;
570 uint16_t param_total, data_total;
573 if (req->in.wct < 19) {
574 smbsrv_send_error(req, NT_STATUS_FOOBAR);
578 op = talloc(req, struct nttrans_op);
580 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
584 trans = talloc(op, struct smb_nttrans);
586 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
594 trans->in.max_setup = CVAL(req->in.vwv, 0);
595 param_total = IVAL(req->in.vwv, 3);
596 data_total = IVAL(req->in.vwv, 7);
597 trans->in.max_param = IVAL(req->in.vwv, 11);
598 trans->in.max_data = IVAL(req->in.vwv, 15);
599 param_count = IVAL(req->in.vwv, 19);
600 param_ofs = IVAL(req->in.vwv, 23);
601 data_count = IVAL(req->in.vwv, 27);
602 data_ofs = IVAL(req->in.vwv, 31);
603 trans->in.setup_count = CVAL(req->in.vwv, 35);
604 trans->in.function = SVAL(req->in.vwv, 36);
606 if (req->in.wct != 19 + trans->in.setup_count) {
607 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror));
611 /* parse out the setup words */
612 trans->in.setup = talloc_array(req, uint16_t, trans->in.setup_count);
613 if (!trans->in.setup) {
614 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
617 for (i=0;i<trans->in.setup_count;i++) {
618 trans->in.setup[i] = SVAL(req->in.vwv, VWV(19+i));
621 if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans->in.params) ||
622 !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans->in.data)) {
623 smbsrv_send_error(req, NT_STATUS_FOOBAR);
627 /* is it a partial request? if so, then send a 'send more' message */
628 if (param_total > param_count ||
629 data_total > data_count) {
630 DEBUG(0,("REWRITE: not handling partial nttrans requests!\n"));
634 req->async_states->state |= NTVFS_ASYNC_STATE_MAY_ASYNC;
635 req->async_states->send_fn = reply_nttrans_send;
636 req->async_states->private_data = op;
638 /* its a full request, give it to the backend */
639 req->async_states->status = nttrans_backend(req, op);
641 /* if the backend replied synchronously, then send now */
642 if (!(req->async_states->state & NTVFS_ASYNC_STATE_ASYNC)) {
643 req->async_states->send_fn(req);
648 /****************************************************************************
649 Reply to an SMBnttranss request
650 ****************************************************************************/
651 void smbsrv_reply_nttranss(struct smbsrv_request *req)
653 smbsrv_send_error(req, NT_STATUS_FOOBAR);