s3:smbd: use req->sconn in reply_ntcancel()
[Samba.git] / source3 / smbd / nttrans.c
blob51adcf6c7084b45b662298a6c201590854d76810
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
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 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
29 #include "auth.h"
30 #include "smbprofile.h"
31 #include "libsmb/libsmb.h"
32 #include "lib/util_ea.h"
34 extern const struct generic_mapping file_generic_mapping;
36 static char *nttrans_realloc(char **ptr, size_t size)
38 if (ptr==NULL) {
39 smb_panic("nttrans_realloc() called with NULL ptr");
42 *ptr = (char *)SMB_REALLOC(*ptr, size);
43 if(*ptr == NULL) {
44 return NULL;
46 memset(*ptr,'\0',size);
47 return *ptr;
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static void send_nt_replies(connection_struct *conn,
58 struct smb_request *req, NTSTATUS nt_error,
59 char *params, int paramsize,
60 char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
64 int useable_space;
65 char *pp = params;
66 char *pd = pdata;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 1;
69 int data_alignment_offset = 0;
70 struct smbXsrv_connection *xconn = req->xconn;
71 int max_send = xconn->smb1.sessions.max_send;
74 * If there genuinely are no parameters or data to send just send
75 * the empty packet.
78 if(params_to_send == 0 && data_to_send == 0) {
79 reply_outbuf(req, 18, 0);
80 if (NT_STATUS_V(nt_error)) {
81 error_packet_set((char *)req->outbuf,
82 0, 0, nt_error,
83 __LINE__,__FILE__);
85 show_msg((char *)req->outbuf);
86 if (!srv_send_smb(xconn,
87 (char *)req->outbuf,
88 true, req->seqnum+1,
89 IS_CONN_ENCRYPTED(conn),
90 &req->pcd)) {
91 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
93 TALLOC_FREE(req->outbuf);
94 return;
98 * When sending params and data ensure that both are nicely aligned.
99 * Only do this alignment when there is also data to send - else
100 * can cause NT redirector problems.
103 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
104 data_alignment_offset = 4 - (params_to_send % 4);
108 * Space is bufsize minus Netbios over TCP header minus SMB header.
109 * The alignment_offset is to align the param bytes on a four byte
110 * boundary (2 bytes for data len, one byte pad).
111 * NT needs this to work correctly.
114 useable_space = max_send - (smb_size
115 + 2 * 18 /* wct */
116 + alignment_offset
117 + data_alignment_offset);
119 if (useable_space < 0) {
120 char *msg = talloc_asprintf(
121 talloc_tos(),
122 "send_nt_replies failed sanity useable_space = %d!!!",
123 useable_space);
124 DEBUG(0, ("%s\n", msg));
125 exit_server_cleanly(msg);
128 while (params_to_send || data_to_send) {
131 * Calculate whether we will totally or partially fill this packet.
134 total_sent_thistime = params_to_send + data_to_send;
137 * We can never send more than useable_space.
140 total_sent_thistime = MIN(total_sent_thistime, useable_space);
142 reply_outbuf(req, 18,
143 total_sent_thistime + alignment_offset
144 + data_alignment_offset);
147 * Set total params and data to be sent.
150 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
151 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
154 * Calculate how many parameters and data we can fit into
155 * this packet. Parameters get precedence.
158 params_sent_thistime = MIN(params_to_send,useable_space);
159 data_sent_thistime = useable_space - params_sent_thistime;
160 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
162 SIVAL(req->outbuf, smb_ntr_ParameterCount,
163 params_sent_thistime);
165 if(params_sent_thistime == 0) {
166 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
167 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
168 } else {
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
177 ((smb_buf(req->outbuf)+alignment_offset)
178 - smb_base(req->outbuf)));
180 * Absolute displacement of param bytes sent in this packet.
183 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
184 pp - params);
188 * Deal with the data portion.
191 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
193 if(data_sent_thistime == 0) {
194 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
195 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
196 } else {
198 * The offset of the data bytes is the offset of the
199 * parameter bytes plus the number of parameters being sent this time.
202 SIVAL(req->outbuf, smb_ntr_DataOffset,
203 ((smb_buf(req->outbuf)+alignment_offset) -
204 smb_base(req->outbuf))
205 + params_sent_thistime + data_alignment_offset);
206 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
210 * Copy the param bytes into the packet.
213 if(params_sent_thistime) {
214 if (alignment_offset != 0) {
215 memset(smb_buf(req->outbuf), 0,
216 alignment_offset);
218 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
219 params_sent_thistime);
223 * Copy in the data bytes
226 if(data_sent_thistime) {
227 if (data_alignment_offset != 0) {
228 memset((smb_buf(req->outbuf)+alignment_offset+
229 params_sent_thistime), 0,
230 data_alignment_offset);
232 memcpy(smb_buf(req->outbuf)+alignment_offset
233 +params_sent_thistime+data_alignment_offset,
234 pd,data_sent_thistime);
237 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
238 params_sent_thistime, data_sent_thistime, useable_space));
239 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
240 params_to_send, data_to_send, paramsize, datasize));
242 if (NT_STATUS_V(nt_error)) {
243 error_packet_set((char *)req->outbuf,
244 0, 0, nt_error,
245 __LINE__,__FILE__);
248 /* Send the packet */
249 show_msg((char *)req->outbuf);
250 if (!srv_send_smb(xconn,
251 (char *)req->outbuf,
252 true, req->seqnum+1,
253 IS_CONN_ENCRYPTED(conn),
254 &req->pcd)) {
255 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
258 TALLOC_FREE(req->outbuf);
260 pp += params_sent_thistime;
261 pd += data_sent_thistime;
263 params_to_send -= params_sent_thistime;
264 data_to_send -= data_sent_thistime;
267 * Sanity check
270 if(params_to_send < 0 || data_to_send < 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send, data_to_send));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname, connection_struct *conn,
283 struct smb_request *req, uint16_t *ppnum)
285 files_struct *fsp;
286 NTSTATUS status;
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name if present. */
291 while (fname[0] == '\\') {
292 fname++;
295 status = open_np_file(req, fname, &fsp);
296 if (!NT_STATUS_IS_OK(status)) {
297 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
298 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
299 ERRDOS, ERRbadpipe);
300 return;
302 reply_nterror(req, status);
303 return;
306 *ppnum = fsp->fnum;
307 return;
310 /****************************************************************************
311 Reply to an NT create and X call for pipes.
312 ****************************************************************************/
314 static void do_ntcreate_pipe_open(connection_struct *conn,
315 struct smb_request *req)
317 char *fname = NULL;
318 uint16_t pnum = FNUM_FIELD_INVALID;
319 char *p = NULL;
320 uint32 flags = IVAL(req->vwv+3, 1);
321 TALLOC_CTX *ctx = talloc_tos();
323 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
325 if (!fname) {
326 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
327 ERRDOS, ERRbadpipe);
328 return;
330 nt_open_pipe(fname, conn, req, &pnum);
332 if (req->outbuf) {
333 /* error reply */
334 return;
338 * Deal with pipe return.
341 if (flags & EXTENDED_RESPONSE_REQUIRED) {
342 /* This is very strange. We
343 * return 50 words, but only set
344 * the wcnt to 42 ? It's definitely
345 * what happens on the wire....
347 reply_outbuf(req, 50, 0);
348 SCVAL(req->outbuf,smb_wct,42);
349 } else {
350 reply_outbuf(req, 34, 0);
353 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
354 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
356 p = (char *)req->outbuf + smb_vwv2;
357 p++;
358 SSVAL(p,0,pnum);
359 p += 2;
360 SIVAL(p,0,FILE_WAS_OPENED);
361 p += 4;
362 p += 32;
363 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
364 p += 20;
365 /* File type. */
366 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
367 /* Device state. */
368 SSVAL(p,2, 0x5FF); /* ? */
369 p += 4;
371 if (flags & EXTENDED_RESPONSE_REQUIRED) {
372 p += 25;
373 SIVAL(p,0,FILE_GENERIC_ALL);
375 * For pipes W2K3 seems to return
376 * 0x12019B next.
377 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
379 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
382 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
385 struct case_semantics_state {
386 connection_struct *conn;
387 bool case_sensitive;
388 bool case_preserve;
389 bool short_case_preserve;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state *state)
398 state->conn->case_sensitive = state->case_sensitive;
399 state->conn->case_preserve = state->case_preserve;
400 state->conn->short_case_preserve = state->short_case_preserve;
401 return 0;
404 /****************************************************************************
405 Save case semantics.
406 ****************************************************************************/
408 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
409 connection_struct *conn)
411 struct case_semantics_state *result;
413 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
414 return NULL;
417 result->conn = conn;
418 result->case_sensitive = conn->case_sensitive;
419 result->case_preserve = conn->case_preserve;
420 result->short_case_preserve = conn->short_case_preserve;
422 /* Set to POSIX. */
423 conn->case_sensitive = True;
424 conn->case_preserve = True;
425 conn->short_case_preserve = True;
427 talloc_set_destructor(result, restore_case_semantics);
429 return result;
432 /****************************************************************************
433 Reply to an NT create and X call.
434 ****************************************************************************/
436 void reply_ntcreate_and_X(struct smb_request *req)
438 connection_struct *conn = req->conn;
439 struct smb_filename *smb_fname = NULL;
440 char *fname = NULL;
441 uint32 flags;
442 uint32 access_mask;
443 uint32 file_attributes;
444 uint32 share_access;
445 uint32 create_disposition;
446 uint32 create_options;
447 uint16 root_dir_fid;
448 uint64_t allocation_size;
449 /* Breakout the oplock request bits so we can set the
450 reply bits separately. */
451 uint32 fattr=0;
452 off_t file_len = 0;
453 int info = 0;
454 files_struct *fsp = NULL;
455 char *p = NULL;
456 struct timespec create_timespec;
457 struct timespec c_timespec;
458 struct timespec a_timespec;
459 struct timespec m_timespec;
460 NTSTATUS status;
461 int oplock_request;
462 uint8_t oplock_granted = NO_OPLOCK_RETURN;
463 struct case_semantics_state *case_state = NULL;
464 TALLOC_CTX *ctx = talloc_tos();
466 START_PROFILE(SMBntcreateX);
468 if (req->wct < 24) {
469 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
470 goto out;
473 flags = IVAL(req->vwv+3, 1);
474 access_mask = IVAL(req->vwv+7, 1);
475 file_attributes = IVAL(req->vwv+13, 1);
476 share_access = IVAL(req->vwv+15, 1);
477 create_disposition = IVAL(req->vwv+17, 1);
478 create_options = IVAL(req->vwv+19, 1);
479 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
481 allocation_size = BVAL(req->vwv+9, 1);
483 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
484 STR_TERMINATE, &status);
486 if (!NT_STATUS_IS_OK(status)) {
487 reply_nterror(req, status);
488 goto out;
491 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
492 "file_attributes = 0x%x, share_access = 0x%x, "
493 "create_disposition = 0x%x create_options = 0x%x "
494 "root_dir_fid = 0x%x, fname = %s\n",
495 (unsigned int)flags,
496 (unsigned int)access_mask,
497 (unsigned int)file_attributes,
498 (unsigned int)share_access,
499 (unsigned int)create_disposition,
500 (unsigned int)create_options,
501 (unsigned int)root_dir_fid,
502 fname));
505 * we need to remove ignored bits when they come directly from the client
506 * because we reuse some of them for internal stuff
508 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
511 * If it's an IPC, use the pipe handler.
514 if (IS_IPC(conn)) {
515 if (lp_nt_pipe_support()) {
516 do_ntcreate_pipe_open(conn, req);
517 goto out;
519 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
520 goto out;
523 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
524 if (oplock_request) {
525 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
526 ? BATCH_OPLOCK : 0;
529 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
530 case_state = set_posix_case_semantics(ctx, conn);
531 if (!case_state) {
532 reply_nterror(req, NT_STATUS_NO_MEMORY);
533 goto out;
537 status = filename_convert(ctx,
538 conn,
539 req->flags2 & FLAGS2_DFS_PATHNAMES,
540 fname,
541 UCF_PREP_CREATEFILE,
542 NULL,
543 &smb_fname);
545 TALLOC_FREE(case_state);
547 if (!NT_STATUS_IS_OK(status)) {
548 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
549 reply_botherror(req,
550 NT_STATUS_PATH_NOT_COVERED,
551 ERRSRV, ERRbadpath);
552 goto out;
554 reply_nterror(req, status);
555 goto out;
559 * Bug #6898 - clients using Windows opens should
560 * never be able to set this attribute into the
561 * VFS.
563 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
565 status = SMB_VFS_CREATE_FILE(
566 conn, /* conn */
567 req, /* req */
568 root_dir_fid, /* root_dir_fid */
569 smb_fname, /* fname */
570 access_mask, /* access_mask */
571 share_access, /* share_access */
572 create_disposition, /* create_disposition*/
573 create_options, /* create_options */
574 file_attributes, /* file_attributes */
575 oplock_request, /* oplock_request */
576 NULL, /* lease */
577 allocation_size, /* allocation_size */
578 0, /* private_flags */
579 NULL, /* sd */
580 NULL, /* ea_list */
581 &fsp, /* result */
582 &info); /* pinfo */
584 if (!NT_STATUS_IS_OK(status)) {
585 if (open_was_deferred(req->sconn, req->mid)) {
586 /* We have re-scheduled this call, no error. */
587 goto out;
589 reply_openerror(req, status);
590 goto out;
593 /* Ensure we're pointing at the correct stat struct. */
594 TALLOC_FREE(smb_fname);
595 smb_fname = fsp->fsp_name;
598 * If the caller set the extended oplock request bit
599 * and we granted one (by whatever means) - set the
600 * correct bit for extended oplock reply.
603 if (oplock_request &&
604 (lp_fake_oplocks(SNUM(conn))
605 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
608 * Exclusive oplock granted
611 if (flags & REQUEST_BATCH_OPLOCK) {
612 oplock_granted = BATCH_OPLOCK_RETURN;
613 } else {
614 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
616 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
617 oplock_granted = LEVEL_II_OPLOCK_RETURN;
618 } else {
619 oplock_granted = NO_OPLOCK_RETURN;
622 file_len = smb_fname->st.st_ex_size;
624 if (flags & EXTENDED_RESPONSE_REQUIRED) {
625 /* This is very strange. We
626 * return 50 words, but only set
627 * the wcnt to 42 ? It's definitely
628 * what happens on the wire....
630 reply_outbuf(req, 50, 0);
631 SCVAL(req->outbuf,smb_wct,42);
632 } else {
633 reply_outbuf(req, 34, 0);
636 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
637 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
639 p = (char *)req->outbuf + smb_vwv2;
641 SCVAL(p, 0, oplock_granted);
643 p++;
644 SSVAL(p,0,fsp->fnum);
645 p += 2;
646 if ((create_disposition == FILE_SUPERSEDE)
647 && (info == FILE_WAS_OVERWRITTEN)) {
648 SIVAL(p,0,FILE_WAS_SUPERSEDED);
649 } else {
650 SIVAL(p,0,info);
652 p += 4;
654 fattr = dos_mode(conn, smb_fname);
655 if (fattr == 0) {
656 fattr = FILE_ATTRIBUTE_NORMAL;
659 /* Create time. */
660 create_timespec = get_create_timespec(conn, fsp, smb_fname);
661 a_timespec = smb_fname->st.st_ex_atime;
662 m_timespec = smb_fname->st.st_ex_mtime;
663 c_timespec = get_change_timespec(conn, fsp, smb_fname);
665 if (lp_dos_filetime_resolution(SNUM(conn))) {
666 dos_filetime_timespec(&create_timespec);
667 dos_filetime_timespec(&a_timespec);
668 dos_filetime_timespec(&m_timespec);
669 dos_filetime_timespec(&c_timespec);
672 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
673 p += 8;
674 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
675 p += 8;
676 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
677 p += 8;
678 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
679 p += 8;
680 SIVAL(p,0,fattr); /* File Attributes. */
681 p += 4;
682 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
683 p += 8;
684 SOFF_T(p,0,file_len);
685 p += 8;
686 if (flags & EXTENDED_RESPONSE_REQUIRED) {
687 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
688 size_t num_names = 0;
689 unsigned int num_streams = 0;
690 struct stream_struct *streams = NULL;
692 /* Do we have any EA's ? */
693 status = get_ea_names_from_file(ctx, conn, fsp,
694 smb_fname->base_name, NULL, &num_names);
695 if (NT_STATUS_IS_OK(status) && num_names) {
696 file_status &= ~NO_EAS;
698 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
699 &num_streams, &streams);
700 /* There is always one stream, ::$DATA. */
701 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
702 file_status &= ~NO_SUBSTREAMS;
704 TALLOC_FREE(streams);
705 SSVAL(p,2,file_status);
707 p += 4;
708 SCVAL(p,0,fsp->is_directory ? 1 : 0);
710 if (flags & EXTENDED_RESPONSE_REQUIRED) {
711 uint32 perms = 0;
712 p += 25;
713 if (fsp->is_directory ||
714 fsp->can_write ||
715 can_write_to_file(conn, smb_fname)) {
716 perms = FILE_GENERIC_ALL;
717 } else {
718 perms = FILE_GENERIC_READ|FILE_EXECUTE;
720 SIVAL(p,0,perms);
723 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
724 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
726 out:
727 END_PROFILE(SMBntcreateX);
728 return;
731 /****************************************************************************
732 Reply to a NT_TRANSACT_CREATE call to open a pipe.
733 ****************************************************************************/
735 static void do_nt_transact_create_pipe(connection_struct *conn,
736 struct smb_request *req,
737 uint16 **ppsetup, uint32 setup_count,
738 char **ppparams, uint32 parameter_count,
739 char **ppdata, uint32 data_count)
741 char *fname = NULL;
742 char *params = *ppparams;
743 uint16_t pnum = FNUM_FIELD_INVALID;
744 char *p = NULL;
745 NTSTATUS status;
746 size_t param_len;
747 uint32 flags;
748 TALLOC_CTX *ctx = talloc_tos();
751 * Ensure minimum number of parameters sent.
754 if(parameter_count < 54) {
755 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
756 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
757 return;
760 flags = IVAL(params,0);
762 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
763 parameter_count-53, STR_TERMINATE,
764 &status);
765 if (!NT_STATUS_IS_OK(status)) {
766 reply_nterror(req, status);
767 return;
770 nt_open_pipe(fname, conn, req, &pnum);
772 if (req->outbuf) {
773 /* Error return */
774 return;
777 /* Realloc the size of parameters and data we will return */
778 if (flags & EXTENDED_RESPONSE_REQUIRED) {
779 /* Extended response is 32 more byyes. */
780 param_len = 101;
781 } else {
782 param_len = 69;
784 params = nttrans_realloc(ppparams, param_len);
785 if(params == NULL) {
786 reply_nterror(req, NT_STATUS_NO_MEMORY);
787 return;
790 p = params;
791 SCVAL(p,0,NO_OPLOCK_RETURN);
793 p += 2;
794 SSVAL(p,0,pnum);
795 p += 2;
796 SIVAL(p,0,FILE_WAS_OPENED);
797 p += 8;
799 p += 32;
800 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
801 p += 20;
802 /* File type. */
803 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
804 /* Device state. */
805 SSVAL(p,2, 0x5FF); /* ? */
806 p += 4;
808 if (flags & EXTENDED_RESPONSE_REQUIRED) {
809 p += 25;
810 SIVAL(p,0,FILE_GENERIC_ALL);
812 * For pipes W2K3 seems to return
813 * 0x12019B next.
814 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
816 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
819 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
821 /* Send the required number of replies */
822 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
824 return;
827 /*********************************************************************
828 Windows seems to do canonicalization of inheritance bits. Do the
829 same.
830 *********************************************************************/
832 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
834 bool set_auto_inherited = false;
837 * We need to filter out the
838 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
839 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
840 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
841 * when an ACE is inherited. Otherwise we zero these bits out.
842 * See:
844 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
846 * for details.
849 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
850 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
851 set_auto_inherited = true;
854 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
855 if (set_auto_inherited) {
856 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
860 /****************************************************************************
861 Internal fn to set security descriptors.
862 ****************************************************************************/
864 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
865 uint32_t security_info_sent)
867 NTSTATUS status;
869 if (!CAN_WRITE(fsp->conn)) {
870 return NT_STATUS_ACCESS_DENIED;
873 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
874 return NT_STATUS_OK;
877 if (psd->owner_sid == NULL) {
878 security_info_sent &= ~SECINFO_OWNER;
880 if (psd->group_sid == NULL) {
881 security_info_sent &= ~SECINFO_GROUP;
884 /* Ensure we have at least one thing set. */
885 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
886 /* Just like W2K3 */
887 return NT_STATUS_OK;
890 /* Ensure we have the rights to do this. */
891 if (security_info_sent & SECINFO_OWNER) {
892 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
893 return NT_STATUS_ACCESS_DENIED;
897 if (security_info_sent & SECINFO_GROUP) {
898 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
899 return NT_STATUS_ACCESS_DENIED;
903 if (security_info_sent & SECINFO_DACL) {
904 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
905 return NT_STATUS_ACCESS_DENIED;
907 /* Convert all the generic bits. */
908 if (psd->dacl) {
909 security_acl_map_generic(psd->dacl, &file_generic_mapping);
913 if (security_info_sent & SECINFO_SACL) {
914 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
915 return NT_STATUS_ACCESS_DENIED;
917 /* Convert all the generic bits. */
918 if (psd->sacl) {
919 security_acl_map_generic(psd->sacl, &file_generic_mapping);
923 canonicalize_inheritance_bits(psd);
925 if (DEBUGLEVEL >= 10) {
926 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
927 NDR_PRINT_DEBUG(security_descriptor, psd);
930 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
932 TALLOC_FREE(psd);
934 return status;
937 /****************************************************************************
938 Internal fn to set security descriptors from a data blob.
939 ****************************************************************************/
941 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
942 uint32_t security_info_sent)
944 struct security_descriptor *psd = NULL;
945 NTSTATUS status;
947 if (sd_len == 0) {
948 return NT_STATUS_INVALID_PARAMETER;
951 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
953 if (!NT_STATUS_IS_OK(status)) {
954 return status;
957 return set_sd(fsp, psd, security_info_sent);
960 /****************************************************************************
961 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
962 ****************************************************************************/
964 static void call_nt_transact_create(connection_struct *conn,
965 struct smb_request *req,
966 uint16 **ppsetup, uint32 setup_count,
967 char **ppparams, uint32 parameter_count,
968 char **ppdata, uint32 data_count,
969 uint32 max_data_count)
971 struct smb_filename *smb_fname = NULL;
972 char *fname = NULL;
973 char *params = *ppparams;
974 char *data = *ppdata;
975 /* Breakout the oplock request bits so we can set the reply bits separately. */
976 uint32 fattr=0;
977 off_t file_len = 0;
978 int info = 0;
979 files_struct *fsp = NULL;
980 char *p = NULL;
981 uint32 flags;
982 uint32 access_mask;
983 uint32 file_attributes;
984 uint32 share_access;
985 uint32 create_disposition;
986 uint32 create_options;
987 uint32 sd_len;
988 struct security_descriptor *sd = NULL;
989 uint32 ea_len;
990 uint16 root_dir_fid;
991 struct timespec create_timespec;
992 struct timespec c_timespec;
993 struct timespec a_timespec;
994 struct timespec m_timespec;
995 struct ea_list *ea_list = NULL;
996 NTSTATUS status;
997 size_t param_len;
998 uint64_t allocation_size;
999 int oplock_request;
1000 uint8_t oplock_granted;
1001 struct case_semantics_state *case_state = NULL;
1002 TALLOC_CTX *ctx = talloc_tos();
1004 DEBUG(5,("call_nt_transact_create\n"));
1007 * If it's an IPC, use the pipe handler.
1010 if (IS_IPC(conn)) {
1011 if (lp_nt_pipe_support()) {
1012 do_nt_transact_create_pipe(
1013 conn, req,
1014 ppsetup, setup_count,
1015 ppparams, parameter_count,
1016 ppdata, data_count);
1017 goto out;
1019 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1020 goto out;
1024 * Ensure minimum number of parameters sent.
1027 if(parameter_count < 54) {
1028 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1029 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1030 goto out;
1033 flags = IVAL(params,0);
1034 access_mask = IVAL(params,8);
1035 file_attributes = IVAL(params,20);
1036 share_access = IVAL(params,24);
1037 create_disposition = IVAL(params,28);
1038 create_options = IVAL(params,32);
1039 sd_len = IVAL(params,36);
1040 ea_len = IVAL(params,40);
1041 root_dir_fid = (uint16)IVAL(params,4);
1042 allocation_size = BVAL(params,12);
1045 * we need to remove ignored bits when they come directly from the client
1046 * because we reuse some of them for internal stuff
1048 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1050 srvstr_get_path(ctx, params, req->flags2, &fname,
1051 params+53, parameter_count-53,
1052 STR_TERMINATE, &status);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 reply_nterror(req, status);
1055 goto out;
1058 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1059 case_state = set_posix_case_semantics(ctx, conn);
1060 if (!case_state) {
1061 reply_nterror(req, NT_STATUS_NO_MEMORY);
1062 goto out;
1066 status = filename_convert(ctx,
1067 conn,
1068 req->flags2 & FLAGS2_DFS_PATHNAMES,
1069 fname,
1070 UCF_PREP_CREATEFILE,
1071 NULL,
1072 &smb_fname);
1074 TALLOC_FREE(case_state);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1078 reply_botherror(req,
1079 NT_STATUS_PATH_NOT_COVERED,
1080 ERRSRV, ERRbadpath);
1081 goto out;
1083 reply_nterror(req, status);
1084 goto out;
1087 /* Ensure the data_len is correct for the sd and ea values given. */
1088 if ((ea_len + sd_len > data_count)
1089 || (ea_len > data_count) || (sd_len > data_count)
1090 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1091 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1092 "%u, data_count = %u\n", (unsigned int)ea_len,
1093 (unsigned int)sd_len, (unsigned int)data_count));
1094 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1095 goto out;
1098 if (sd_len) {
1099 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1100 sd_len));
1102 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1103 &sd);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 DEBUG(10, ("call_nt_transact_create: "
1106 "unmarshall_sec_desc failed: %s\n",
1107 nt_errstr(status)));
1108 reply_nterror(req, status);
1109 goto out;
1113 if (ea_len) {
1114 if (!lp_ea_support(SNUM(conn))) {
1115 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1116 "EA's not supported.\n",
1117 (unsigned int)ea_len));
1118 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1119 goto out;
1122 if (ea_len < 10) {
1123 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1124 "too small (should be more than 10)\n",
1125 (unsigned int)ea_len ));
1126 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1127 goto out;
1130 /* We have already checked that ea_len <= data_count here. */
1131 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1132 ea_len);
1133 if (ea_list == NULL) {
1134 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1135 goto out;
1138 if (ea_list_has_invalid_name(ea_list)) {
1139 /* Realloc the size of parameters and data we will return */
1140 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1141 /* Extended response is 32 more byyes. */
1142 param_len = 101;
1143 } else {
1144 param_len = 69;
1146 params = nttrans_realloc(ppparams, param_len);
1147 if(params == NULL) {
1148 reply_nterror(req, NT_STATUS_NO_MEMORY);
1149 goto out;
1152 memset(params, '\0', param_len);
1153 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1154 params, param_len, NULL, 0);
1155 goto out;
1159 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1160 if (oplock_request) {
1161 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1162 ? BATCH_OPLOCK : 0;
1166 * Bug #6898 - clients using Windows opens should
1167 * never be able to set this attribute into the
1168 * VFS.
1170 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1172 status = SMB_VFS_CREATE_FILE(
1173 conn, /* conn */
1174 req, /* req */
1175 root_dir_fid, /* root_dir_fid */
1176 smb_fname, /* fname */
1177 access_mask, /* access_mask */
1178 share_access, /* share_access */
1179 create_disposition, /* create_disposition*/
1180 create_options, /* create_options */
1181 file_attributes, /* file_attributes */
1182 oplock_request, /* oplock_request */
1183 NULL, /* lease */
1184 allocation_size, /* allocation_size */
1185 0, /* private_flags */
1186 sd, /* sd */
1187 ea_list, /* ea_list */
1188 &fsp, /* result */
1189 &info); /* pinfo */
1191 if(!NT_STATUS_IS_OK(status)) {
1192 if (open_was_deferred(req->sconn, req->mid)) {
1193 /* We have re-scheduled this call, no error. */
1194 return;
1196 reply_openerror(req, status);
1197 goto out;
1200 /* Ensure we're pointing at the correct stat struct. */
1201 TALLOC_FREE(smb_fname);
1202 smb_fname = fsp->fsp_name;
1205 * If the caller set the extended oplock request bit
1206 * and we granted one (by whatever means) - set the
1207 * correct bit for extended oplock reply.
1210 if (oplock_request &&
1211 (lp_fake_oplocks(SNUM(conn))
1212 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1215 * Exclusive oplock granted
1218 if (flags & REQUEST_BATCH_OPLOCK) {
1219 oplock_granted = BATCH_OPLOCK_RETURN;
1220 } else {
1221 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1223 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1224 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1225 } else {
1226 oplock_granted = NO_OPLOCK_RETURN;
1229 file_len = smb_fname->st.st_ex_size;
1231 /* Realloc the size of parameters and data we will return */
1232 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1233 /* Extended response is 32 more byyes. */
1234 param_len = 101;
1235 } else {
1236 param_len = 69;
1238 params = nttrans_realloc(ppparams, param_len);
1239 if(params == NULL) {
1240 reply_nterror(req, NT_STATUS_NO_MEMORY);
1241 goto out;
1244 p = params;
1245 SCVAL(p, 0, oplock_granted);
1247 p += 2;
1248 SSVAL(p,0,fsp->fnum);
1249 p += 2;
1250 if ((create_disposition == FILE_SUPERSEDE)
1251 && (info == FILE_WAS_OVERWRITTEN)) {
1252 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1253 } else {
1254 SIVAL(p,0,info);
1256 p += 8;
1258 fattr = dos_mode(conn, smb_fname);
1259 if (fattr == 0) {
1260 fattr = FILE_ATTRIBUTE_NORMAL;
1263 /* Create time. */
1264 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1265 a_timespec = smb_fname->st.st_ex_atime;
1266 m_timespec = smb_fname->st.st_ex_mtime;
1267 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1269 if (lp_dos_filetime_resolution(SNUM(conn))) {
1270 dos_filetime_timespec(&create_timespec);
1271 dos_filetime_timespec(&a_timespec);
1272 dos_filetime_timespec(&m_timespec);
1273 dos_filetime_timespec(&c_timespec);
1276 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1277 p += 8;
1278 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1279 p += 8;
1280 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1281 p += 8;
1282 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1283 p += 8;
1284 SIVAL(p,0,fattr); /* File Attributes. */
1285 p += 4;
1286 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1287 p += 8;
1288 SOFF_T(p,0,file_len);
1289 p += 8;
1290 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1291 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1292 size_t num_names = 0;
1293 unsigned int num_streams = 0;
1294 struct stream_struct *streams = NULL;
1296 /* Do we have any EA's ? */
1297 status = get_ea_names_from_file(ctx, conn, fsp,
1298 smb_fname->base_name, NULL, &num_names);
1299 if (NT_STATUS_IS_OK(status) && num_names) {
1300 file_status &= ~NO_EAS;
1302 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1303 &num_streams, &streams);
1304 /* There is always one stream, ::$DATA. */
1305 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1306 file_status &= ~NO_SUBSTREAMS;
1308 TALLOC_FREE(streams);
1309 SSVAL(p,2,file_status);
1311 p += 4;
1312 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1314 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1315 uint32 perms = 0;
1316 p += 25;
1317 if (fsp->is_directory ||
1318 fsp->can_write ||
1319 can_write_to_file(conn, smb_fname)) {
1320 perms = FILE_GENERIC_ALL;
1321 } else {
1322 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1324 SIVAL(p,0,perms);
1327 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1328 smb_fname_str_dbg(smb_fname)));
1330 /* Send the required number of replies */
1331 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1332 out:
1333 return;
1336 /****************************************************************************
1337 Reply to a NT CANCEL request.
1338 conn POINTER CAN BE NULL HERE !
1339 ****************************************************************************/
1341 void reply_ntcancel(struct smb_request *req)
1343 struct smbXsrv_connection *xconn = req->xconn;
1344 struct smbd_server_connection *sconn = req->sconn;
1347 * Go through and cancel any pending change notifies.
1350 START_PROFILE(SMBntcancel);
1351 srv_cancel_sign_response(xconn);
1352 remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1353 remove_pending_lock_requests_by_mid_smb1(sconn, req->mid);
1355 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1356 (unsigned long long)req->mid));
1358 END_PROFILE(SMBntcancel);
1359 return;
1362 /****************************************************************************
1363 Copy a file.
1364 ****************************************************************************/
1366 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1367 connection_struct *conn,
1368 struct smb_request *req,
1369 struct smb_filename *smb_fname_src,
1370 struct smb_filename *smb_fname_dst,
1371 uint32 attrs)
1373 files_struct *fsp1,*fsp2;
1374 uint32 fattr;
1375 int info;
1376 off_t ret=-1;
1377 NTSTATUS status = NT_STATUS_OK;
1378 char *parent;
1380 if (!CAN_WRITE(conn)) {
1381 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1382 goto out;
1385 /* Source must already exist. */
1386 if (!VALID_STAT(smb_fname_src->st)) {
1387 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1388 goto out;
1391 /* Ensure attributes match. */
1392 fattr = dos_mode(conn, smb_fname_src);
1393 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1394 status = NT_STATUS_NO_SUCH_FILE;
1395 goto out;
1398 /* Disallow if dst file already exists. */
1399 if (VALID_STAT(smb_fname_dst->st)) {
1400 status = NT_STATUS_OBJECT_NAME_COLLISION;
1401 goto out;
1404 /* No links from a directory. */
1405 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1406 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1407 goto out;
1410 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1411 smb_fname_str_dbg(smb_fname_src),
1412 smb_fname_str_dbg(smb_fname_dst)));
1414 status = SMB_VFS_CREATE_FILE(
1415 conn, /* conn */
1416 req, /* req */
1417 0, /* root_dir_fid */
1418 smb_fname_src, /* fname */
1419 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1420 FILE_READ_EA, /* access_mask */
1421 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1422 FILE_SHARE_DELETE),
1423 FILE_OPEN, /* create_disposition*/
1424 0, /* create_options */
1425 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1426 NO_OPLOCK, /* oplock_request */
1427 NULL, /* lease */
1428 0, /* allocation_size */
1429 0, /* private_flags */
1430 NULL, /* sd */
1431 NULL, /* ea_list */
1432 &fsp1, /* result */
1433 &info); /* pinfo */
1435 if (!NT_STATUS_IS_OK(status)) {
1436 goto out;
1439 status = SMB_VFS_CREATE_FILE(
1440 conn, /* conn */
1441 req, /* req */
1442 0, /* root_dir_fid */
1443 smb_fname_dst, /* fname */
1444 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1445 FILE_WRITE_EA, /* access_mask */
1446 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1447 FILE_SHARE_DELETE),
1448 FILE_CREATE, /* create_disposition*/
1449 0, /* create_options */
1450 fattr, /* file_attributes */
1451 NO_OPLOCK, /* oplock_request */
1452 NULL, /* lease */
1453 0, /* allocation_size */
1454 0, /* private_flags */
1455 NULL, /* sd */
1456 NULL, /* ea_list */
1457 &fsp2, /* result */
1458 &info); /* pinfo */
1460 if (!NT_STATUS_IS_OK(status)) {
1461 close_file(NULL, fsp1, ERROR_CLOSE);
1462 goto out;
1465 if (smb_fname_src->st.st_ex_size) {
1466 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1470 * As we are opening fsp1 read-only we only expect
1471 * an error on close on fsp2 if we are out of space.
1472 * Thus we don't look at the error return from the
1473 * close of fsp1.
1475 close_file(NULL, fsp1, NORMAL_CLOSE);
1477 /* Ensure the modtime is set correctly on the destination file. */
1478 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1480 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1482 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1483 creates the file. This isn't the correct thing to do in the copy
1484 case. JRA */
1485 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1486 NULL)) {
1487 status = NT_STATUS_NO_MEMORY;
1488 goto out;
1490 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1491 TALLOC_FREE(parent);
1493 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1494 status = NT_STATUS_DISK_FULL;
1495 goto out;
1497 out:
1498 if (!NT_STATUS_IS_OK(status)) {
1499 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1500 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1501 smb_fname_str_dbg(smb_fname_dst)));
1504 return status;
1507 /****************************************************************************
1508 Reply to a NT rename request.
1509 ****************************************************************************/
1511 void reply_ntrename(struct smb_request *req)
1513 connection_struct *conn = req->conn;
1514 struct smb_filename *smb_fname_old = NULL;
1515 struct smb_filename *smb_fname_new = NULL;
1516 char *oldname = NULL;
1517 char *newname = NULL;
1518 const char *p;
1519 NTSTATUS status;
1520 bool src_has_wcard = False;
1521 bool dest_has_wcard = False;
1522 uint32 attrs;
1523 uint32_t ucf_flags_src = 0;
1524 uint32_t ucf_flags_dst = 0;
1525 uint16 rename_type;
1526 TALLOC_CTX *ctx = talloc_tos();
1527 bool stream_rename = false;
1529 START_PROFILE(SMBntrename);
1531 if (req->wct < 4) {
1532 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1533 goto out;
1536 attrs = SVAL(req->vwv+0, 0);
1537 rename_type = SVAL(req->vwv+1, 0);
1539 p = (const char *)req->buf + 1;
1540 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1541 &status, &src_has_wcard);
1542 if (!NT_STATUS_IS_OK(status)) {
1543 reply_nterror(req, status);
1544 goto out;
1547 if (ms_has_wild(oldname)) {
1548 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1549 goto out;
1552 p++;
1553 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1554 &status, &dest_has_wcard);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 reply_nterror(req, status);
1557 goto out;
1560 if (!lp_posix_pathnames()) {
1561 /* The newname must begin with a ':' if the
1562 oldname contains a ':'. */
1563 if (strchr_m(oldname, ':')) {
1564 if (newname[0] != ':') {
1565 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1566 goto out;
1568 stream_rename = true;
1573 * If this is a rename operation, allow wildcards and save the
1574 * destination's last component.
1576 if (rename_type == RENAME_FLAG_RENAME) {
1577 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1578 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1581 /* rename_internals() calls unix_convert(), so don't call it here. */
1582 status = filename_convert(ctx, conn,
1583 req->flags2 & FLAGS2_DFS_PATHNAMES,
1584 oldname,
1585 ucf_flags_src,
1586 NULL,
1587 &smb_fname_old);
1588 if (!NT_STATUS_IS_OK(status)) {
1589 if (NT_STATUS_EQUAL(status,
1590 NT_STATUS_PATH_NOT_COVERED)) {
1591 reply_botherror(req,
1592 NT_STATUS_PATH_NOT_COVERED,
1593 ERRSRV, ERRbadpath);
1594 goto out;
1596 reply_nterror(req, status);
1597 goto out;
1600 status = filename_convert(ctx, conn,
1601 req->flags2 & FLAGS2_DFS_PATHNAMES,
1602 newname,
1603 ucf_flags_dst,
1604 &dest_has_wcard,
1605 &smb_fname_new);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 if (NT_STATUS_EQUAL(status,
1608 NT_STATUS_PATH_NOT_COVERED)) {
1609 reply_botherror(req,
1610 NT_STATUS_PATH_NOT_COVERED,
1611 ERRSRV, ERRbadpath);
1612 goto out;
1614 reply_nterror(req, status);
1615 goto out;
1618 if (stream_rename) {
1619 /* smb_fname_new must be the same as smb_fname_old. */
1620 TALLOC_FREE(smb_fname_new->base_name);
1621 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1622 smb_fname_old->base_name);
1623 if (!smb_fname_new->base_name) {
1624 reply_nterror(req, NT_STATUS_NO_MEMORY);
1625 goto out;
1629 DEBUG(3,("reply_ntrename: %s -> %s\n",
1630 smb_fname_str_dbg(smb_fname_old),
1631 smb_fname_str_dbg(smb_fname_new)));
1633 switch(rename_type) {
1634 case RENAME_FLAG_RENAME:
1635 status = rename_internals(ctx, conn, req,
1636 smb_fname_old, smb_fname_new,
1637 attrs, False, src_has_wcard,
1638 dest_has_wcard,
1639 DELETE_ACCESS);
1640 break;
1641 case RENAME_FLAG_HARD_LINK:
1642 if (src_has_wcard || dest_has_wcard) {
1643 /* No wildcards. */
1644 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1645 } else {
1646 status = hardlink_internals(ctx, conn,
1647 req,
1648 false,
1649 smb_fname_old,
1650 smb_fname_new);
1652 break;
1653 case RENAME_FLAG_COPY:
1654 if (src_has_wcard || dest_has_wcard) {
1655 /* No wildcards. */
1656 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1657 } else {
1658 status = copy_internals(ctx, conn, req,
1659 smb_fname_old,
1660 smb_fname_new,
1661 attrs);
1663 break;
1664 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1665 status = NT_STATUS_INVALID_PARAMETER;
1666 break;
1667 default:
1668 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1669 break;
1672 if (!NT_STATUS_IS_OK(status)) {
1673 if (open_was_deferred(req->sconn, req->mid)) {
1674 /* We have re-scheduled this call. */
1675 goto out;
1678 reply_nterror(req, status);
1679 goto out;
1682 reply_outbuf(req, 0, 0);
1683 out:
1684 END_PROFILE(SMBntrename);
1685 return;
1688 /****************************************************************************
1689 Reply to a notify change - queue the request and
1690 don't allow a directory to be opened.
1691 ****************************************************************************/
1693 static void smbd_smb1_notify_reply(struct smb_request *req,
1694 NTSTATUS error_code,
1695 uint8_t *buf, size_t len)
1697 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1700 static void call_nt_transact_notify_change(connection_struct *conn,
1701 struct smb_request *req,
1702 uint16 **ppsetup,
1703 uint32 setup_count,
1704 char **ppparams,
1705 uint32 parameter_count,
1706 char **ppdata, uint32 data_count,
1707 uint32 max_data_count,
1708 uint32 max_param_count)
1710 uint16 *setup = *ppsetup;
1711 files_struct *fsp;
1712 uint32 filter;
1713 NTSTATUS status;
1714 bool recursive;
1716 if(setup_count < 6) {
1717 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1718 return;
1721 fsp = file_fsp(req, SVAL(setup,4));
1722 filter = IVAL(setup, 0);
1723 recursive = (SVAL(setup, 6) != 0) ? True : False;
1725 DEBUG(3,("call_nt_transact_notify_change\n"));
1727 if(!fsp) {
1728 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1729 return;
1733 char *filter_string;
1735 if (!(filter_string = notify_filter_string(NULL, filter))) {
1736 reply_nterror(req,NT_STATUS_NO_MEMORY);
1737 return;
1740 DEBUG(3,("call_nt_transact_notify_change: notify change "
1741 "called on %s, filter = %s, recursive = %d\n",
1742 fsp_str_dbg(fsp), filter_string, recursive));
1744 TALLOC_FREE(filter_string);
1747 if((!fsp->is_directory) || (conn != fsp->conn)) {
1748 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1749 return;
1752 if (fsp->notify == NULL) {
1754 status = change_notify_create(fsp, filter, recursive);
1756 if (!NT_STATUS_IS_OK(status)) {
1757 DEBUG(10, ("change_notify_create returned %s\n",
1758 nt_errstr(status)));
1759 reply_nterror(req, status);
1760 return;
1764 if (change_notify_fsp_has_changes(fsp)) {
1767 * We've got changes pending, respond immediately
1771 * TODO: write a torture test to check the filtering behaviour
1772 * here.
1775 change_notify_reply(req,
1776 NT_STATUS_OK,
1777 max_param_count,
1778 fsp->notify,
1779 smbd_smb1_notify_reply);
1782 * change_notify_reply() above has independently sent its
1783 * results
1785 return;
1789 * No changes pending, queue the request
1792 status = change_notify_add_request(req,
1793 max_param_count,
1794 filter,
1795 recursive, fsp,
1796 smbd_smb1_notify_reply);
1797 if (!NT_STATUS_IS_OK(status)) {
1798 reply_nterror(req, status);
1800 return;
1803 /****************************************************************************
1804 Reply to an NT transact rename command.
1805 ****************************************************************************/
1807 static void call_nt_transact_rename(connection_struct *conn,
1808 struct smb_request *req,
1809 uint16 **ppsetup, uint32 setup_count,
1810 char **ppparams, uint32 parameter_count,
1811 char **ppdata, uint32 data_count,
1812 uint32 max_data_count)
1814 char *params = *ppparams;
1815 char *new_name = NULL;
1816 files_struct *fsp = NULL;
1817 bool dest_has_wcard = False;
1818 NTSTATUS status;
1819 TALLOC_CTX *ctx = talloc_tos();
1821 if(parameter_count < 5) {
1822 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1823 return;
1826 fsp = file_fsp(req, SVAL(params, 0));
1827 if (!check_fsp(conn, req, fsp)) {
1828 return;
1830 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1831 parameter_count - 4,
1832 STR_TERMINATE, &status, &dest_has_wcard);
1833 if (!NT_STATUS_IS_OK(status)) {
1834 reply_nterror(req, status);
1835 return;
1839 * W2K3 ignores this request as the RAW-RENAME test
1840 * demonstrates, so we do.
1842 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1844 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1845 fsp_str_dbg(fsp), new_name));
1847 return;
1850 /******************************************************************************
1851 Fake up a completely empty SD.
1852 *******************************************************************************/
1854 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1856 size_t sd_size;
1858 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1859 if(!*ppsd) {
1860 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1861 return NT_STATUS_NO_MEMORY;
1864 return NT_STATUS_OK;
1867 /****************************************************************************
1868 Reply to query a security descriptor.
1869 Callable from SMB1 and SMB2.
1870 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1871 the required size.
1872 ****************************************************************************/
1874 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1875 TALLOC_CTX *mem_ctx,
1876 files_struct *fsp,
1877 uint32_t security_info_wanted,
1878 uint32_t max_data_count,
1879 uint8_t **ppmarshalled_sd,
1880 size_t *psd_size)
1882 NTSTATUS status;
1883 struct security_descriptor *psd = NULL;
1884 TALLOC_CTX *frame = talloc_stackframe();
1887 * Get the permissions to return.
1890 if ((security_info_wanted & SECINFO_SACL) &&
1891 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1892 DEBUG(10, ("Access to SACL denied.\n"));
1893 TALLOC_FREE(frame);
1894 return NT_STATUS_ACCESS_DENIED;
1897 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1898 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1899 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1900 TALLOC_FREE(frame);
1901 return NT_STATUS_ACCESS_DENIED;
1904 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1905 SECINFO_GROUP|SECINFO_SACL)) {
1906 /* Don't return SECINFO_LABEL if anything else was
1907 requested. See bug #8458. */
1908 security_info_wanted &= ~SECINFO_LABEL;
1911 if (!lp_nt_acl_support(SNUM(conn))) {
1912 status = get_null_nt_acl(frame, &psd);
1913 } else if (security_info_wanted & SECINFO_LABEL) {
1914 /* Like W2K3 return a null object. */
1915 status = get_null_nt_acl(frame, &psd);
1916 } else {
1917 status = SMB_VFS_FGET_NT_ACL(
1918 fsp, security_info_wanted, frame, &psd);
1920 if (!NT_STATUS_IS_OK(status)) {
1921 TALLOC_FREE(frame);
1922 return status;
1925 if (!(security_info_wanted & SECINFO_OWNER)) {
1926 psd->owner_sid = NULL;
1928 if (!(security_info_wanted & SECINFO_GROUP)) {
1929 psd->group_sid = NULL;
1931 if (!(security_info_wanted & SECINFO_DACL)) {
1932 psd->type &= ~SEC_DESC_DACL_PRESENT;
1933 psd->dacl = NULL;
1935 if (!(security_info_wanted & SECINFO_SACL)) {
1936 psd->type &= ~SEC_DESC_SACL_PRESENT;
1937 psd->sacl = NULL;
1940 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1941 * present in the reply to match Windows behavior */
1942 if (psd->sacl == NULL &&
1943 security_info_wanted & SECINFO_SACL)
1944 psd->type |= SEC_DESC_SACL_PRESENT;
1945 if (psd->dacl == NULL &&
1946 security_info_wanted & SECINFO_DACL)
1947 psd->type |= SEC_DESC_DACL_PRESENT;
1949 if (security_info_wanted & SECINFO_LABEL) {
1950 /* Like W2K3 return a null object. */
1951 psd->owner_sid = NULL;
1952 psd->group_sid = NULL;
1953 psd->dacl = NULL;
1954 psd->sacl = NULL;
1955 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1958 *psd_size = ndr_size_security_descriptor(psd, 0);
1960 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1961 (unsigned long)*psd_size));
1963 if (DEBUGLEVEL >= 10) {
1964 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1965 fsp_str_dbg(fsp)));
1966 NDR_PRINT_DEBUG(security_descriptor, psd);
1969 if (max_data_count < *psd_size) {
1970 TALLOC_FREE(frame);
1971 return NT_STATUS_BUFFER_TOO_SMALL;
1974 status = marshall_sec_desc(mem_ctx, psd,
1975 ppmarshalled_sd, psd_size);
1977 if (!NT_STATUS_IS_OK(status)) {
1978 TALLOC_FREE(frame);
1979 return status;
1982 TALLOC_FREE(frame);
1983 return NT_STATUS_OK;
1986 /****************************************************************************
1987 SMB1 reply to query a security descriptor.
1988 ****************************************************************************/
1990 static void call_nt_transact_query_security_desc(connection_struct *conn,
1991 struct smb_request *req,
1992 uint16 **ppsetup,
1993 uint32 setup_count,
1994 char **ppparams,
1995 uint32 parameter_count,
1996 char **ppdata,
1997 uint32 data_count,
1998 uint32 max_data_count)
2000 char *params = *ppparams;
2001 char *data = *ppdata;
2002 size_t sd_size = 0;
2003 uint32 security_info_wanted;
2004 files_struct *fsp = NULL;
2005 NTSTATUS status;
2006 uint8_t *marshalled_sd = NULL;
2008 if(parameter_count < 8) {
2009 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2010 return;
2013 fsp = file_fsp(req, SVAL(params,0));
2014 if(!fsp) {
2015 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2016 return;
2019 security_info_wanted = IVAL(params,4);
2021 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2022 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2023 (unsigned int)security_info_wanted));
2025 params = nttrans_realloc(ppparams, 4);
2026 if(params == NULL) {
2027 reply_nterror(req, NT_STATUS_NO_MEMORY);
2028 return;
2032 * Get the permissions to return.
2035 status = smbd_do_query_security_desc(conn,
2036 talloc_tos(),
2037 fsp,
2038 security_info_wanted &
2039 SMB_SUPPORTED_SECINFO_FLAGS,
2040 max_data_count,
2041 &marshalled_sd,
2042 &sd_size);
2044 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2045 SIVAL(params,0,(uint32_t)sd_size);
2046 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2047 params, 4, NULL, 0);
2048 return;
2051 if (!NT_STATUS_IS_OK(status)) {
2052 reply_nterror(req, status);
2053 return;
2056 SMB_ASSERT(sd_size > 0);
2058 SIVAL(params,0,(uint32_t)sd_size);
2060 if (max_data_count < sd_size) {
2061 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2062 params, 4, NULL, 0);
2063 return;
2067 * Allocate the data we will return.
2070 data = nttrans_realloc(ppdata, sd_size);
2071 if(data == NULL) {
2072 reply_nterror(req, NT_STATUS_NO_MEMORY);
2073 return;
2076 memcpy(data, marshalled_sd, sd_size);
2078 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2080 return;
2083 /****************************************************************************
2084 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2085 ****************************************************************************/
2087 static void call_nt_transact_set_security_desc(connection_struct *conn,
2088 struct smb_request *req,
2089 uint16 **ppsetup,
2090 uint32 setup_count,
2091 char **ppparams,
2092 uint32 parameter_count,
2093 char **ppdata,
2094 uint32 data_count,
2095 uint32 max_data_count)
2097 char *params= *ppparams;
2098 char *data = *ppdata;
2099 files_struct *fsp = NULL;
2100 uint32 security_info_sent = 0;
2101 NTSTATUS status;
2103 if(parameter_count < 8) {
2104 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2105 return;
2108 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2109 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2110 return;
2113 if (!CAN_WRITE(fsp->conn)) {
2114 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2115 return;
2118 if(!lp_nt_acl_support(SNUM(conn))) {
2119 goto done;
2122 security_info_sent = IVAL(params,4);
2124 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2125 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2127 if (data_count == 0) {
2128 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2129 return;
2132 status = set_sd_blob(fsp, (uint8 *)data, data_count,
2133 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
2134 if (!NT_STATUS_IS_OK(status)) {
2135 reply_nterror(req, status);
2136 return;
2139 done:
2140 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2141 return;
2144 /****************************************************************************
2145 Reply to NT IOCTL
2146 ****************************************************************************/
2148 static void call_nt_transact_ioctl(connection_struct *conn,
2149 struct smb_request *req,
2150 uint16 **ppsetup, uint32 setup_count,
2151 char **ppparams, uint32 parameter_count,
2152 char **ppdata, uint32 data_count,
2153 uint32 max_data_count)
2155 NTSTATUS status;
2156 uint32 function;
2157 uint16 fidnum;
2158 files_struct *fsp;
2159 uint8 isFSctl;
2160 uint8 compfilter;
2161 char *out_data = NULL;
2162 uint32 out_data_len = 0;
2163 char *pdata = *ppdata;
2164 TALLOC_CTX *ctx = talloc_tos();
2166 if (setup_count != 8) {
2167 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2168 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2169 return;
2172 function = IVAL(*ppsetup, 0);
2173 fidnum = SVAL(*ppsetup, 4);
2174 isFSctl = CVAL(*ppsetup, 6);
2175 compfilter = CVAL(*ppsetup, 7);
2177 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2178 function, fidnum, isFSctl, compfilter));
2180 fsp=file_fsp(req, fidnum);
2183 * We don't really implement IOCTLs, especially on files.
2185 if (!isFSctl) {
2186 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2187 isFSctl));
2188 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2189 return;
2192 /* Has to be for an open file! */
2193 if (!check_fsp_open(conn, req, fsp)) {
2194 return;
2197 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2200 * out_data might be allocated by the VFS module, but talloc should be
2201 * used, and should be cleaned up when the request ends.
2203 status = SMB_VFS_FSCTL(fsp,
2204 ctx,
2205 function,
2206 req->flags2,
2207 (uint8_t *)pdata,
2208 data_count,
2209 (uint8_t **)&out_data,
2210 max_data_count,
2211 &out_data_len);
2212 if (!NT_STATUS_IS_OK(status)) {
2213 reply_nterror(req, status);
2214 } else {
2215 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2220 #ifdef HAVE_SYS_QUOTAS
2221 /****************************************************************************
2222 Reply to get user quota
2223 ****************************************************************************/
2225 static void call_nt_transact_get_user_quota(connection_struct *conn,
2226 struct smb_request *req,
2227 uint16 **ppsetup,
2228 uint32 setup_count,
2229 char **ppparams,
2230 uint32 parameter_count,
2231 char **ppdata,
2232 uint32 data_count,
2233 uint32 max_data_count)
2235 NTSTATUS nt_status = NT_STATUS_OK;
2236 char *params = *ppparams;
2237 char *pdata = *ppdata;
2238 char *entry;
2239 int data_len=0,param_len=0;
2240 int qt_len=0;
2241 int entry_len = 0;
2242 files_struct *fsp = NULL;
2243 uint16 level = 0;
2244 size_t sid_len;
2245 struct dom_sid sid;
2246 bool start_enum = True;
2247 SMB_NTQUOTA_STRUCT qt;
2248 SMB_NTQUOTA_LIST *tmp_list;
2249 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2251 ZERO_STRUCT(qt);
2253 /* access check */
2254 if (get_current_uid(conn) != 0) {
2255 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2256 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2257 conn->session_info->unix_info->unix_name));
2258 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2259 return;
2263 * Ensure minimum number of parameters sent.
2266 if (parameter_count < 4) {
2267 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2268 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2269 return;
2272 /* maybe we can check the quota_fnum */
2273 fsp = file_fsp(req, SVAL(params,0));
2274 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2275 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2276 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2277 return;
2280 /* the NULL pointer checking for fsp->fake_file_handle->pd
2281 * is done by CHECK_NTQUOTA_HANDLE_OK()
2283 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2285 level = SVAL(params,2);
2287 /* unknown 12 bytes leading in params */
2289 switch (level) {
2290 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2291 /* seems that we should continue with the enum here --metze */
2293 if (qt_handle->quota_list!=NULL &&
2294 qt_handle->tmp_list==NULL) {
2296 /* free the list */
2297 free_ntquota_list(&(qt_handle->quota_list));
2299 /* Realloc the size of parameters and data we will return */
2300 param_len = 4;
2301 params = nttrans_realloc(ppparams, param_len);
2302 if(params == NULL) {
2303 reply_nterror(req, NT_STATUS_NO_MEMORY);
2304 return;
2307 data_len = 0;
2308 SIVAL(params,0,data_len);
2310 break;
2313 start_enum = False;
2315 case TRANSACT_GET_USER_QUOTA_LIST_START:
2317 if (qt_handle->quota_list==NULL &&
2318 qt_handle->tmp_list==NULL) {
2319 start_enum = True;
2322 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2323 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2324 return;
2327 /* Realloc the size of parameters and data we will return */
2328 param_len = 4;
2329 params = nttrans_realloc(ppparams, param_len);
2330 if(params == NULL) {
2331 reply_nterror(req, NT_STATUS_NO_MEMORY);
2332 return;
2335 /* we should not trust the value in max_data_count*/
2336 max_data_count = MIN(max_data_count,2048);
2338 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2339 if(pdata == NULL) {
2340 reply_nterror(req, NT_STATUS_NO_MEMORY);
2341 return;
2344 entry = pdata;
2346 /* set params Size of returned Quota Data 4 bytes*/
2347 /* but set it later when we know it */
2349 /* for each entry push the data */
2351 if (start_enum) {
2352 qt_handle->tmp_list = qt_handle->quota_list;
2355 tmp_list = qt_handle->tmp_list;
2357 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2358 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2360 sid_len = ndr_size_dom_sid(
2361 &tmp_list->quotas->sid, 0);
2362 entry_len = 40 + sid_len;
2364 /* nextoffset entry 4 bytes */
2365 SIVAL(entry,0,entry_len);
2367 /* then the len of the SID 4 bytes */
2368 SIVAL(entry,4,sid_len);
2370 /* unknown data 8 bytes uint64_t */
2371 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2373 /* the used disk space 8 bytes uint64_t */
2374 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2376 /* the soft quotas 8 bytes uint64_t */
2377 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2379 /* the hard quotas 8 bytes uint64_t */
2380 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2382 /* and now the SID */
2383 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2386 qt_handle->tmp_list = tmp_list;
2388 /* overwrite the offset of the last entry */
2389 SIVAL(entry-entry_len,0,0);
2391 data_len = 4+qt_len;
2392 /* overwrite the params quota_data_len */
2393 SIVAL(params,0,data_len);
2395 break;
2397 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2399 /* unknown 4 bytes IVAL(pdata,0) */
2401 if (data_count < 8) {
2402 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2403 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2404 return;
2407 sid_len = IVAL(pdata,4);
2408 /* Ensure this is less than 1mb. */
2409 if (sid_len > (1024*1024)) {
2410 reply_nterror(req, NT_STATUS_NO_MEMORY);
2411 return;
2414 if (data_count < 8+sid_len) {
2415 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2416 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2417 return;
2420 data_len = 4+40+sid_len;
2422 if (max_data_count < data_len) {
2423 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2424 max_data_count, data_len));
2425 param_len = 4;
2426 SIVAL(params,0,data_len);
2427 data_len = 0;
2428 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2429 break;
2432 if (!sid_parse(pdata+8,sid_len,&sid)) {
2433 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2434 return;
2437 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2438 ZERO_STRUCT(qt);
2440 * we have to return zero's in all fields
2441 * instead of returning an error here
2442 * --metze
2446 /* Realloc the size of parameters and data we will return */
2447 param_len = 4;
2448 params = nttrans_realloc(ppparams, param_len);
2449 if(params == NULL) {
2450 reply_nterror(req, NT_STATUS_NO_MEMORY);
2451 return;
2454 pdata = nttrans_realloc(ppdata, data_len);
2455 if(pdata == NULL) {
2456 reply_nterror(req, NT_STATUS_NO_MEMORY);
2457 return;
2460 entry = pdata;
2462 /* set params Size of returned Quota Data 4 bytes*/
2463 SIVAL(params,0,data_len);
2465 /* nextoffset entry 4 bytes */
2466 SIVAL(entry,0,0);
2468 /* then the len of the SID 4 bytes */
2469 SIVAL(entry,4,sid_len);
2471 /* unknown data 8 bytes uint64_t */
2472 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2474 /* the used disk space 8 bytes uint64_t */
2475 SBIG_UINT(entry,16,qt.usedspace);
2477 /* the soft quotas 8 bytes uint64_t */
2478 SBIG_UINT(entry,24,qt.softlim);
2480 /* the hard quotas 8 bytes uint64_t */
2481 SBIG_UINT(entry,32,qt.hardlim);
2483 /* and now the SID */
2484 sid_linearize(entry+40, sid_len, &sid);
2486 break;
2488 default:
2489 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2490 "level 0x%04hX\n",
2491 fsp_fnum_dbg(fsp), level));
2492 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2493 return;
2494 break;
2497 send_nt_replies(conn, req, nt_status, params, param_len,
2498 pdata, data_len);
2501 /****************************************************************************
2502 Reply to set user quota
2503 ****************************************************************************/
2505 static void call_nt_transact_set_user_quota(connection_struct *conn,
2506 struct smb_request *req,
2507 uint16 **ppsetup,
2508 uint32 setup_count,
2509 char **ppparams,
2510 uint32 parameter_count,
2511 char **ppdata,
2512 uint32 data_count,
2513 uint32 max_data_count)
2515 char *params = *ppparams;
2516 char *pdata = *ppdata;
2517 int data_len=0,param_len=0;
2518 SMB_NTQUOTA_STRUCT qt;
2519 size_t sid_len;
2520 struct dom_sid sid;
2521 files_struct *fsp = NULL;
2523 ZERO_STRUCT(qt);
2525 /* access check */
2526 if (get_current_uid(conn) != 0) {
2527 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2528 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2529 conn->session_info->unix_info->unix_name));
2530 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2531 return;
2535 * Ensure minimum number of parameters sent.
2538 if (parameter_count < 2) {
2539 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2540 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2541 return;
2544 /* maybe we can check the quota_fnum */
2545 fsp = file_fsp(req, SVAL(params,0));
2546 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2547 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2548 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2549 return;
2552 if (data_count < 40) {
2553 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2554 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2555 return;
2558 /* offset to next quota record.
2559 * 4 bytes IVAL(pdata,0)
2560 * unused here...
2563 /* sid len */
2564 sid_len = IVAL(pdata,4);
2566 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2567 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2568 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2569 return;
2572 /* unknown 8 bytes in pdata
2573 * maybe its the change time in NTTIME
2576 /* the used space 8 bytes (uint64_t)*/
2577 qt.usedspace = BVAL(pdata,16);
2579 /* the soft quotas 8 bytes (uint64_t)*/
2580 qt.softlim = BVAL(pdata,24);
2582 /* the hard quotas 8 bytes (uint64_t)*/
2583 qt.hardlim = BVAL(pdata,32);
2585 if (!sid_parse(pdata+40,sid_len,&sid)) {
2586 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2587 return;
2590 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2592 /* 44 unknown bytes left... */
2594 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2595 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2596 return;
2599 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2600 pdata, data_len);
2602 #endif /* HAVE_SYS_QUOTAS */
2604 static void handle_nttrans(connection_struct *conn,
2605 struct trans_state *state,
2606 struct smb_request *req)
2608 if (get_Protocol() >= PROTOCOL_NT1) {
2609 req->flags2 |= 0x40; /* IS_LONG_NAME */
2610 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2614 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2616 /* Now we must call the relevant NT_TRANS function */
2617 switch(state->call) {
2618 case NT_TRANSACT_CREATE:
2620 START_PROFILE(NT_transact_create);
2621 call_nt_transact_create(
2622 conn, req,
2623 &state->setup, state->setup_count,
2624 &state->param, state->total_param,
2625 &state->data, state->total_data,
2626 state->max_data_return);
2627 END_PROFILE(NT_transact_create);
2628 break;
2631 case NT_TRANSACT_IOCTL:
2633 START_PROFILE(NT_transact_ioctl);
2634 call_nt_transact_ioctl(
2635 conn, req,
2636 &state->setup, state->setup_count,
2637 &state->param, state->total_param,
2638 &state->data, state->total_data,
2639 state->max_data_return);
2640 END_PROFILE(NT_transact_ioctl);
2641 break;
2644 case NT_TRANSACT_SET_SECURITY_DESC:
2646 START_PROFILE(NT_transact_set_security_desc);
2647 call_nt_transact_set_security_desc(
2648 conn, req,
2649 &state->setup, state->setup_count,
2650 &state->param, state->total_param,
2651 &state->data, state->total_data,
2652 state->max_data_return);
2653 END_PROFILE(NT_transact_set_security_desc);
2654 break;
2657 case NT_TRANSACT_NOTIFY_CHANGE:
2659 START_PROFILE(NT_transact_notify_change);
2660 call_nt_transact_notify_change(
2661 conn, req,
2662 &state->setup, state->setup_count,
2663 &state->param, state->total_param,
2664 &state->data, state->total_data,
2665 state->max_data_return,
2666 state->max_param_return);
2667 END_PROFILE(NT_transact_notify_change);
2668 break;
2671 case NT_TRANSACT_RENAME:
2673 START_PROFILE(NT_transact_rename);
2674 call_nt_transact_rename(
2675 conn, req,
2676 &state->setup, state->setup_count,
2677 &state->param, state->total_param,
2678 &state->data, state->total_data,
2679 state->max_data_return);
2680 END_PROFILE(NT_transact_rename);
2681 break;
2684 case NT_TRANSACT_QUERY_SECURITY_DESC:
2686 START_PROFILE(NT_transact_query_security_desc);
2687 call_nt_transact_query_security_desc(
2688 conn, req,
2689 &state->setup, state->setup_count,
2690 &state->param, state->total_param,
2691 &state->data, state->total_data,
2692 state->max_data_return);
2693 END_PROFILE(NT_transact_query_security_desc);
2694 break;
2697 #ifdef HAVE_SYS_QUOTAS
2698 case NT_TRANSACT_GET_USER_QUOTA:
2700 START_PROFILE(NT_transact_get_user_quota);
2701 call_nt_transact_get_user_quota(
2702 conn, req,
2703 &state->setup, state->setup_count,
2704 &state->param, state->total_param,
2705 &state->data, state->total_data,
2706 state->max_data_return);
2707 END_PROFILE(NT_transact_get_user_quota);
2708 break;
2711 case NT_TRANSACT_SET_USER_QUOTA:
2713 START_PROFILE(NT_transact_set_user_quota);
2714 call_nt_transact_set_user_quota(
2715 conn, req,
2716 &state->setup, state->setup_count,
2717 &state->param, state->total_param,
2718 &state->data, state->total_data,
2719 state->max_data_return);
2720 END_PROFILE(NT_transact_set_user_quota);
2721 break;
2723 #endif /* HAVE_SYS_QUOTAS */
2725 default:
2726 /* Error in request */
2727 DEBUG(0,("handle_nttrans: Unknown request %d in "
2728 "nttrans call\n", state->call));
2729 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2730 return;
2732 return;
2735 /****************************************************************************
2736 Reply to a SMBNTtrans.
2737 ****************************************************************************/
2739 void reply_nttrans(struct smb_request *req)
2741 connection_struct *conn = req->conn;
2742 uint32_t pscnt;
2743 uint32_t psoff;
2744 uint32_t dscnt;
2745 uint32_t dsoff;
2746 uint16 function_code;
2747 NTSTATUS result;
2748 struct trans_state *state;
2750 START_PROFILE(SMBnttrans);
2752 if (req->wct < 19) {
2753 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2754 END_PROFILE(SMBnttrans);
2755 return;
2758 pscnt = IVAL(req->vwv+9, 1);
2759 psoff = IVAL(req->vwv+11, 1);
2760 dscnt = IVAL(req->vwv+13, 1);
2761 dsoff = IVAL(req->vwv+15, 1);
2762 function_code = SVAL(req->vwv+18, 0);
2764 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2765 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2766 END_PROFILE(SMBnttrans);
2767 return;
2770 result = allow_new_trans(conn->pending_trans, req->mid);
2771 if (!NT_STATUS_IS_OK(result)) {
2772 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2773 reply_nterror(req, result);
2774 END_PROFILE(SMBnttrans);
2775 return;
2778 if ((state = talloc(conn, struct trans_state)) == NULL) {
2779 reply_nterror(req, NT_STATUS_NO_MEMORY);
2780 END_PROFILE(SMBnttrans);
2781 return;
2784 state->cmd = SMBnttrans;
2786 state->mid = req->mid;
2787 state->vuid = req->vuid;
2788 state->total_data = IVAL(req->vwv+3, 1);
2789 state->data = NULL;
2790 state->total_param = IVAL(req->vwv+1, 1);
2791 state->param = NULL;
2792 state->max_data_return = IVAL(req->vwv+7, 1);
2793 state->max_param_return = IVAL(req->vwv+5, 1);
2795 /* setup count is in *words* */
2796 state->setup_count = 2*CVAL(req->vwv+17, 1);
2797 state->setup = NULL;
2798 state->call = function_code;
2800 DEBUG(10, ("num_setup=%u, "
2801 "param_total=%u, this_param=%u, max_param=%u, "
2802 "data_total=%u, this_data=%u, max_data=%u, "
2803 "param_offset=%u, data_offset=%u\n",
2804 (unsigned)state->setup_count,
2805 (unsigned)state->total_param, (unsigned)pscnt,
2806 (unsigned)state->max_param_return,
2807 (unsigned)state->total_data, (unsigned)dscnt,
2808 (unsigned)state->max_data_return,
2809 (unsigned)psoff, (unsigned)dsoff));
2812 * All nttrans messages we handle have smb_wct == 19 +
2813 * state->setup_count. Ensure this is so as a sanity check.
2816 if(req->wct != 19 + (state->setup_count/2)) {
2817 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2818 req->wct, 19 + (state->setup_count/2)));
2819 goto bad_param;
2822 /* Don't allow more than 128mb for each value. */
2823 if ((state->total_data > (1024*1024*128)) ||
2824 (state->total_param > (1024*1024*128))) {
2825 reply_nterror(req, NT_STATUS_NO_MEMORY);
2826 END_PROFILE(SMBnttrans);
2827 return;
2830 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2831 goto bad_param;
2833 if (state->total_data) {
2835 if (trans_oob(state->total_data, 0, dscnt)
2836 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2837 goto bad_param;
2840 /* Can't use talloc here, the core routines do realloc on the
2841 * params and data. */
2842 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2843 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2844 "bytes !\n", (unsigned int)state->total_data));
2845 TALLOC_FREE(state);
2846 reply_nterror(req, NT_STATUS_NO_MEMORY);
2847 END_PROFILE(SMBnttrans);
2848 return;
2851 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2854 if (state->total_param) {
2856 if (trans_oob(state->total_param, 0, pscnt)
2857 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2858 goto bad_param;
2861 /* Can't use talloc here, the core routines do realloc on the
2862 * params and data. */
2863 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2864 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2865 "bytes !\n", (unsigned int)state->total_param));
2866 SAFE_FREE(state->data);
2867 TALLOC_FREE(state);
2868 reply_nterror(req, NT_STATUS_NO_MEMORY);
2869 END_PROFILE(SMBnttrans);
2870 return;
2873 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2876 state->received_data = dscnt;
2877 state->received_param = pscnt;
2879 if(state->setup_count > 0) {
2880 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2881 state->setup_count));
2884 * No overflow possible here, state->setup_count is an
2885 * unsigned int, being filled by a single byte from
2886 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2887 * below is not necessary, it's here to clarify things. The
2888 * validity of req->vwv and req->wct has been checked in
2889 * init_smb_request already.
2891 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2892 goto bad_param;
2895 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2896 if (state->setup == NULL) {
2897 DEBUG(0,("reply_nttrans : Out of memory\n"));
2898 SAFE_FREE(state->data);
2899 SAFE_FREE(state->param);
2900 TALLOC_FREE(state);
2901 reply_nterror(req, NT_STATUS_NO_MEMORY);
2902 END_PROFILE(SMBnttrans);
2903 return;
2906 memcpy(state->setup, req->vwv+19, state->setup_count);
2907 dump_data(10, (uint8 *)state->setup, state->setup_count);
2910 if ((state->received_data == state->total_data) &&
2911 (state->received_param == state->total_param)) {
2912 handle_nttrans(conn, state, req);
2913 SAFE_FREE(state->param);
2914 SAFE_FREE(state->data);
2915 TALLOC_FREE(state);
2916 END_PROFILE(SMBnttrans);
2917 return;
2920 DLIST_ADD(conn->pending_trans, state);
2922 /* We need to send an interim response then receive the rest
2923 of the parameter/data bytes */
2924 reply_outbuf(req, 0, 0);
2925 show_msg((char *)req->outbuf);
2926 END_PROFILE(SMBnttrans);
2927 return;
2929 bad_param:
2931 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2932 SAFE_FREE(state->data);
2933 SAFE_FREE(state->param);
2934 TALLOC_FREE(state);
2935 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2936 END_PROFILE(SMBnttrans);
2937 return;
2940 /****************************************************************************
2941 Reply to a SMBnttranss
2942 ****************************************************************************/
2944 void reply_nttranss(struct smb_request *req)
2946 connection_struct *conn = req->conn;
2947 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2948 struct trans_state *state;
2950 START_PROFILE(SMBnttranss);
2952 show_msg((const char *)req->inbuf);
2954 /* Windows clients expect all replies to
2955 an NT transact secondary (SMBnttranss 0xA1)
2956 to have a command code of NT transact
2957 (SMBnttrans 0xA0). See bug #8989 for details. */
2958 req->cmd = SMBnttrans;
2960 if (req->wct < 18) {
2961 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2962 END_PROFILE(SMBnttranss);
2963 return;
2966 for (state = conn->pending_trans; state != NULL;
2967 state = state->next) {
2968 if (state->mid == req->mid) {
2969 break;
2973 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2974 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2975 END_PROFILE(SMBnttranss);
2976 return;
2979 /* Revise state->total_param and state->total_data in case they have
2980 changed downwards */
2981 if (IVAL(req->vwv+1, 1) < state->total_param) {
2982 state->total_param = IVAL(req->vwv+1, 1);
2984 if (IVAL(req->vwv+3, 1) < state->total_data) {
2985 state->total_data = IVAL(req->vwv+3, 1);
2988 pcnt = IVAL(req->vwv+5, 1);
2989 poff = IVAL(req->vwv+7, 1);
2990 pdisp = IVAL(req->vwv+9, 1);
2992 dcnt = IVAL(req->vwv+11, 1);
2993 doff = IVAL(req->vwv+13, 1);
2994 ddisp = IVAL(req->vwv+15, 1);
2996 state->received_param += pcnt;
2997 state->received_data += dcnt;
2999 if ((state->received_data > state->total_data) ||
3000 (state->received_param > state->total_param))
3001 goto bad_param;
3003 if (pcnt) {
3004 if (trans_oob(state->total_param, pdisp, pcnt)
3005 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3006 goto bad_param;
3008 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3011 if (dcnt) {
3012 if (trans_oob(state->total_data, ddisp, dcnt)
3013 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3014 goto bad_param;
3016 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3019 if ((state->received_param < state->total_param) ||
3020 (state->received_data < state->total_data)) {
3021 END_PROFILE(SMBnttranss);
3022 return;
3025 handle_nttrans(conn, state, req);
3027 DLIST_REMOVE(conn->pending_trans, state);
3028 SAFE_FREE(state->data);
3029 SAFE_FREE(state->param);
3030 TALLOC_FREE(state);
3031 END_PROFILE(SMBnttranss);
3032 return;
3034 bad_param:
3036 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3037 DLIST_REMOVE(conn->pending_trans, state);
3038 SAFE_FREE(state->data);
3039 SAFE_FREE(state->param);
3040 TALLOC_FREE(state);
3041 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3042 END_PROFILE(SMBnttranss);
3043 return;