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"
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 uint16_t param_size, uint16_t data_size,
47 trans->out.setup_count = setup_count;
48 if (setup_count != 0) {
49 trans->out.setup = talloc_zero_array(op, uint16_t, setup_count);
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);
71 op->trans->out.setup_count = 0;
72 op->trans->out.setup = NULL;
73 op->trans->out.params = data_blob_talloc(op, NULL, 69);
74 op->trans->out.data = data_blob(NULL, 0);
76 params = op->trans->out.params.data;
77 NT_STATUS_HAVE_NO_MEMORY(params);
79 SSVAL(params, 0, io->ntcreatex.out.oplock_level);
80 SSVAL(params, 2, io->ntcreatex.file.fnum);
81 SIVAL(params, 4, io->ntcreatex.out.create_action);
82 SIVAL(params, 8, 0); /* ea error offset */
83 push_nttime(params, 12, io->ntcreatex.out.create_time);
84 push_nttime(params, 20, io->ntcreatex.out.access_time);
85 push_nttime(params, 28, io->ntcreatex.out.write_time);
86 push_nttime(params, 36, io->ntcreatex.out.change_time);
87 SIVAL(params, 44, io->ntcreatex.out.attrib);
88 SBVAL(params, 48, io->ntcreatex.out.alloc_size);
89 SBVAL(params, 56, io->ntcreatex.out.size);
90 SSVAL(params, 64, io->ntcreatex.out.file_type);
91 SSVAL(params, 66, io->ntcreatex.out.ipc_state);
92 SCVAL(params, 68, io->ntcreatex.out.is_directory);
98 parse NTTRANS_CREATE request
100 static NTSTATUS nttrans_create(struct smbsrv_request *req,
101 struct nttrans_op *op)
103 struct smb_nttrans *trans = op->trans;
106 uint32_t sd_length, ea_length;
110 if (trans->in.params.length < 54) {
111 return NT_STATUS_INVALID_PARAMETER;
114 /* parse the request */
115 io = talloc(op, union smb_open);
116 NT_STATUS_HAVE_NO_MEMORY(io);
118 io->ntcreatex.level = RAW_OPEN_NTTRANS_CREATE;
120 params = trans->in.params.data;
122 io->ntcreatex.in.flags = IVAL(params, 0);
123 io->ntcreatex.in.root_fid = IVAL(params, 4);
124 io->ntcreatex.in.access_mask = IVAL(params, 8);
125 io->ntcreatex.in.alloc_size = BVAL(params, 12);
126 io->ntcreatex.in.file_attr = IVAL(params, 20);
127 io->ntcreatex.in.share_access = IVAL(params, 24);
128 io->ntcreatex.in.open_disposition = IVAL(params, 28);
129 io->ntcreatex.in.create_options = IVAL(params, 32);
130 sd_length = IVAL(params, 36);
131 ea_length = IVAL(params, 40);
132 fname_len = IVAL(params, 44);
133 io->ntcreatex.in.impersonation = IVAL(params, 48);
134 io->ntcreatex.in.security_flags = CVAL(params, 52);
135 io->ntcreatex.in.sec_desc = NULL;
136 io->ntcreatex.in.ea_list = NULL;
138 req_pull_string(req, &io->ntcreatex.in.fname,
140 MIN(fname_len+1, trans->in.params.length - 53),
141 STR_NO_RANGE_CHECK | STR_TERMINATE);
142 if (!io->ntcreatex.in.fname) {
143 return NT_STATUS_INVALID_PARAMETER;
146 if (sd_length > trans->in.data.length ||
147 ea_length > trans->in.data.length ||
148 (sd_length+ea_length) > trans->in.data.length) {
149 return NT_STATUS_INVALID_PARAMETER;
152 /* this call has an optional security descriptor */
153 if (sd_length != 0) {
155 blob.data = trans->in.data.data;
156 blob.length = sd_length;
157 io->ntcreatex.in.sec_desc = talloc(io, struct security_descriptor);
158 if (io->ntcreatex.in.sec_desc == NULL) {
159 return NT_STATUS_NO_MEMORY;
161 status = ndr_pull_struct_blob(&blob, io,
162 io->ntcreatex.in.sec_desc,
163 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
164 if (!NT_STATUS_IS_OK(status)) {
169 /* and an optional ea_list */
172 blob.data = trans->in.data.data + sd_length;
173 blob.length = ea_length;
174 io->ntcreatex.in.ea_list = talloc(io, struct smb_ea_list);
175 if (io->ntcreatex.in.ea_list == NULL) {
176 return NT_STATUS_NO_MEMORY;
179 status = ea_pull_list_chained(&blob, io,
180 &io->ntcreatex.in.ea_list->num_eas,
181 &io->ntcreatex.in.ea_list->eas);
182 if (!NT_STATUS_IS_OK(status)) {
187 op->send_fn = nttrans_create_send;
190 return ntvfs_open(req, io);
195 send NTTRANS_QUERY_SEC_DESC reply
197 static NTSTATUS nttrans_query_sec_desc_send(struct nttrans_op *op)
199 struct smb_nttrans *trans = op->trans;
200 union smb_fileinfo *io = talloc_get_type(op->op_info, union smb_fileinfo);
203 trans->out.setup_count = 0;
204 trans->out.setup = NULL;
205 trans->out.params = data_blob_talloc(op, NULL, 4);
206 trans->out.data = data_blob(NULL, 0);
207 NT_STATUS_HAVE_NO_MEMORY(trans->out.params.data);
209 status = ndr_push_struct_blob(&trans->out.data, op,
210 io->query_secdesc.out.sd,
211 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
212 NT_STATUS_NOT_OK_RETURN(status);
214 SIVAL(trans->out.params.data, 0, trans->out.data.length);
220 parse NTTRANS_QUERY_SEC_DESC request
222 static NTSTATUS nttrans_query_sec_desc(struct smbsrv_request *req,
223 struct nttrans_op *op)
225 struct smb_nttrans *trans = op->trans;
226 union smb_fileinfo *io;
228 if (trans->in.params.length < 8) {
229 return NT_STATUS_INVALID_PARAMETER;
232 /* parse the request */
233 io = talloc(op, union smb_fileinfo);
234 NT_STATUS_HAVE_NO_MEMORY(io);
236 io->query_secdesc.level = RAW_FILEINFO_SEC_DESC;
237 io->query_secdesc.file.fnum = SVAL(trans->in.params.data, 0);
238 io->query_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
241 op->send_fn = nttrans_query_sec_desc_send;
243 return ntvfs_qfileinfo(req, io);
248 parse NTTRANS_SET_SEC_DESC request
250 static NTSTATUS nttrans_set_sec_desc(struct smbsrv_request *req,
251 struct nttrans_op *op)
253 struct smb_nttrans *trans = op->trans;
254 union smb_setfileinfo *io;
257 if (trans->in.params.length < 8) {
258 return NT_STATUS_INVALID_PARAMETER;
261 /* parse the request */
262 io = talloc(req, union smb_setfileinfo);
263 NT_STATUS_HAVE_NO_MEMORY(io);
265 io->set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
266 io->set_secdesc.file.fnum = SVAL(trans->in.params.data, 0);
267 io->set_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
269 io->set_secdesc.in.sd = talloc(io, struct security_descriptor);
270 NT_STATUS_HAVE_NO_MEMORY(io->set_secdesc.in.sd);
272 status = ndr_pull_struct_blob(&trans->in.data, req,
273 io->set_secdesc.in.sd,
274 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
275 NT_STATUS_NOT_OK_RETURN(status);
277 trans->out.setup_count = 0;
278 trans->out.setup = NULL;
279 trans->out.params = data_blob(NULL, 0);
280 trans->out.data = data_blob(NULL, 0);
282 return ntvfs_setfileinfo(req, io);
286 /* parse NTTRANS_RENAME request
288 static NTSTATUS nttrans_rename(struct smbsrv_request *req,
289 struct nttrans_op *op)
291 return NT_STATUS_NOT_IMPLEMENTED;
295 parse NTTRANS_IOCTL request
297 static NTSTATUS nttrans_ioctl(struct smbsrv_request *req,
298 struct nttrans_op *op)
300 struct smb_nttrans *trans = op->trans;
309 /* should have at least 4 setup words */
310 if (trans->in.setup_count != 4) {
311 return NT_STATUS_INVALID_PARAMETER;
314 nt = talloc(op, union smb_ioctl);
315 NT_STATUS_HAVE_NO_MEMORY(nt);
317 function = IVAL(trans->in.setup, 0);
318 fnum = SVAL(trans->in.setup, 4);
319 fsctl = CVAL(trans->in.setup, 6);
320 filter = CVAL(trans->in.setup, 7);
322 blob = &trans->in.data;
324 nt->ntioctl.level = RAW_IOCTL_NTIOCTL;
325 nt->ntioctl.file.fnum = fnum;
326 nt->ntioctl.in.function = function;
327 nt->ntioctl.in.fsctl = fsctl;
328 nt->ntioctl.in.filter = filter;
330 status = nttrans_setup_reply(op, trans, 0, 0, 1);
331 NT_STATUS_NOT_OK_RETURN(status);
333 trans->out.setup[0] = 0;
335 return ntvfs_ioctl(req, nt);
340 send NTTRANS_NOTIFY_CHANGE reply
342 static NTSTATUS nttrans_notify_change_send(struct nttrans_op *op)
344 union smb_notify *info = talloc_get_type(op->op_info, union smb_notify);
350 #define MAX_BYTES_PER_CHAR 3
352 /* work out how big the reply buffer could be */
353 for (i=0;i<info->notify.out.num_changes;i++) {
354 size += 12 + 3 + (1+strlen(info->notify.out.changes[i].name.s)) * MAX_BYTES_PER_CHAR;
357 status = nttrans_setup_reply(op, op->trans, size, 0, 0);
358 NT_STATUS_NOT_OK_RETURN(status);
360 p = op->trans->out.params.data;
362 /* construct the changes buffer */
363 for (i=0;i<info->notify.out.num_changes;i++) {
366 SIVAL(p, 4, info->notify.out.changes[i].action);
367 len = push_string(p + 12, info->notify.out.changes[i].name.s,
368 op->trans->out.params.length - (ofs+12), STR_UNICODE);
374 int pad = 4 - (ofs & 3);
375 memset(p+ofs, 0, pad);
381 p = op->trans->out.params.data + ofs;
384 op->trans->out.params.length = ofs;
390 parse NTTRANS_NOTIFY_CHANGE request
392 static NTSTATUS nttrans_notify_change(struct smbsrv_request *req,
393 struct nttrans_op *op)
395 struct smb_nttrans *trans = op->trans;
396 union smb_notify *info;
398 /* should have at least 4 setup words */
399 if (trans->in.setup_count != 4) {
400 return NT_STATUS_INVALID_PARAMETER;
403 info = talloc(op, union smb_notify);
404 NT_STATUS_HAVE_NO_MEMORY(info);
406 info->notify.in.completion_filter = IVAL(trans->in.setup, 0);
407 info->notify.file.fnum = SVAL(trans->in.setup, 4);
408 info->notify.in.recursive = SVAL(trans->in.setup, 6);
409 info->notify.in.buffer_size = trans->in.max_param;
412 op->send_fn = nttrans_notify_change_send;
414 return ntvfs_notify(req, info);
418 backend for nttrans requests
420 static NTSTATUS nttrans_backend(struct smbsrv_request *req,
421 struct nttrans_op *op)
423 /* the nttrans command is in function */
424 switch (op->trans->in.function) {
425 case NT_TRANSACT_CREATE:
426 return nttrans_create(req, op);
427 case NT_TRANSACT_IOCTL:
428 return nttrans_ioctl(req, op);
429 case NT_TRANSACT_RENAME:
430 return nttrans_rename(req, op);
431 case NT_TRANSACT_QUERY_SECURITY_DESC:
432 return nttrans_query_sec_desc(req, op);
433 case NT_TRANSACT_SET_SECURITY_DESC:
434 return nttrans_set_sec_desc(req, op);
435 case NT_TRANSACT_NOTIFY_CHANGE:
436 return nttrans_notify_change(req, op);
439 /* an unknown nttrans command */
440 return NT_STATUS_DOS(ERRSRV, ERRerror);
444 static void reply_nttrans_send(struct smbsrv_request *req)
446 uint16_t params_left, data_left;
447 uint8_t *params, *data;
448 struct smb_nttrans *trans;
449 struct nttrans_op *op;
451 if (!NT_STATUS_IS_OK(req->async_states->status)) {
452 smbsrv_send_error(req, req->async_states->status);
456 op = talloc_get_type(req->async_states->private_data, struct nttrans_op);
459 /* if this function needs work to form the nttrans reply buffer, then
461 if (op->send_fn != NULL) {
463 status = op->send_fn(op);
464 if (!NT_STATUS_IS_OK(status)) {
465 smbsrv_send_error(req, status);
470 /* note that we don't check the max_setup count (matching w2k3
473 if (trans->out.params.length > trans->in.max_param) {
474 smbsrv_setup_error(req, NT_STATUS_BUFFER_TOO_SMALL);
475 trans->out.params.length = trans->in.max_param;
477 if (trans->out.data.length > trans->in.max_data) {
478 smbsrv_setup_error(req, NT_STATUS_BUFFER_TOO_SMALL);
479 trans->out.data.length = trans->in.max_data;
482 params_left = trans->out.params.length;
483 data_left = trans->out.data.length;
484 params = trans->out.params.data;
485 data = trans->out.data.data;
487 smbsrv_setup_reply(req, 18 + trans->out.setup_count, 0);
489 /* we need to divide up the reply into chunks that fit into
490 the negotiated buffer size */
492 uint16_t this_data, this_param, max_bytes;
493 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
494 struct smbsrv_request *this_req;
497 max_bytes = req_max_data(req) - (align1 + align2);
499 this_param = params_left;
500 if (this_param > max_bytes) {
501 this_param = max_bytes;
503 max_bytes -= this_param;
505 this_data = data_left;
506 if (this_data > max_bytes) {
507 this_data = max_bytes;
510 /* don't destroy unless this is the last chunk */
511 if (params_left - this_param != 0 ||
512 data_left - this_data != 0) {
513 this_req = smbsrv_setup_secondary_request(req);
518 req_grow_data(req, this_param + this_data + (align1 + align2));
520 SSVAL(this_req->out.vwv, 0, 0); /* reserved */
521 SCVAL(this_req->out.vwv, 2, 0); /* reserved */
522 SIVAL(this_req->out.vwv, 3, trans->out.params.length);
523 SIVAL(this_req->out.vwv, 7, trans->out.data.length);
525 SIVAL(this_req->out.vwv, 11, this_param);
526 SIVAL(this_req->out.vwv, 15, align1 + PTR_DIFF(this_req->out.data, this_req->out.hdr));
527 SIVAL(this_req->out.vwv, 19, PTR_DIFF(params, trans->out.params.data));
529 SIVAL(this_req->out.vwv, 23, this_data);
530 SIVAL(this_req->out.vwv, 27, align1 + align2 +
531 PTR_DIFF(this_req->out.data + this_param, this_req->out.hdr));
532 SIVAL(this_req->out.vwv, 31, PTR_DIFF(data, trans->out.data.data));
534 SCVAL(this_req->out.vwv, 35, trans->out.setup_count);
535 for (i=0;i<trans->out.setup_count;i++) {
536 SSVAL(this_req->out.vwv, VWV(18+i), trans->out.setup[i]);
539 memset(this_req->out.data, 0, align1);
540 if (this_param != 0) {
541 memcpy(this_req->out.data + align1, params, this_param);
543 memset(this_req->out.data+this_param+align1, 0, align2);
544 if (this_data != 0) {
545 memcpy(this_req->out.data+this_param+align1+align2,
549 params_left -= this_param;
550 data_left -= this_data;
551 params += this_param;
554 smbsrv_send_reply(this_req);
555 } while (params_left != 0 || data_left != 0);
559 /****************************************************************************
560 Reply to an SMBnttrans request
561 ****************************************************************************/
562 void smbsrv_reply_nttrans(struct smbsrv_request *req)
564 struct nttrans_op *op;
565 struct smb_nttrans *trans;
567 uint16_t param_ofs, data_ofs;
568 uint16_t param_count, data_count;
569 uint16_t param_total, data_total;
572 if (req->in.wct < 19) {
573 smbsrv_send_error(req, NT_STATUS_FOOBAR);
577 op = talloc(req, struct nttrans_op);
579 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
583 trans = talloc(op, struct smb_nttrans);
585 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
593 trans->in.max_setup = CVAL(req->in.vwv, 0);
594 param_total = IVAL(req->in.vwv, 3);
595 data_total = IVAL(req->in.vwv, 7);
596 trans->in.max_param = IVAL(req->in.vwv, 11);
597 trans->in.max_data = IVAL(req->in.vwv, 15);
598 param_count = IVAL(req->in.vwv, 19);
599 param_ofs = IVAL(req->in.vwv, 23);
600 data_count = IVAL(req->in.vwv, 27);
601 data_ofs = IVAL(req->in.vwv, 31);
602 trans->in.setup_count = CVAL(req->in.vwv, 35);
603 trans->in.function = SVAL(req->in.vwv, 36);
605 if (req->in.wct != 19 + trans->in.setup_count) {
606 smbsrv_send_error(req, NT_STATUS_DOS(ERRSRV, ERRerror));
610 /* parse out the setup words */
611 trans->in.setup = talloc_array(req, uint16_t, trans->in.setup_count);
612 if (!trans->in.setup) {
613 smbsrv_send_error(req, NT_STATUS_NO_MEMORY);
616 for (i=0;i<trans->in.setup_count;i++) {
617 trans->in.setup[i] = SVAL(req->in.vwv, VWV(19+i));
620 if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans->in.params) ||
621 !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans->in.data)) {
622 smbsrv_send_error(req, NT_STATUS_FOOBAR);
626 /* is it a partial request? if so, then send a 'send more' message */
627 if (param_total > param_count ||
628 data_total > data_count) {
629 DEBUG(0,("REWRITE: not handling partial nttrans requests!\n"));
633 req->async_states->state |= NTVFS_ASYNC_STATE_MAY_ASYNC;
634 req->async_states->send_fn = reply_nttrans_send;
635 req->async_states->private_data = op;
637 /* its a full request, give it to the backend */
638 req->async_states->status = nttrans_backend(req, op);
640 /* if the backend replied synchronously, then send now */
641 if (!(req->async_states->state & NTVFS_ASYNC_STATE_ASYNC)) {
642 req->async_states->send_fn(req);
647 /****************************************************************************
648 Reply to an SMBnttranss request
649 ****************************************************************************/
650 void smbsrv_reply_nttranss(struct smbsrv_request *req)
652 smbsrv_send_error(req, NT_STATUS_FOOBAR);