smbd: Fix a typo
[Samba.git] / source3 / smbd / nttrans.c
blob4423a44cbeabb183d8a5964747f856a6cf7360dd
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 */
583 NULL, NULL); /* create context */
585 if (!NT_STATUS_IS_OK(status)) {
586 if (open_was_deferred(req->xconn, req->mid)) {
587 /* We have re-scheduled this call, no error. */
588 goto out;
590 reply_openerror(req, status);
591 goto out;
594 /* Ensure we're pointing at the correct stat struct. */
595 TALLOC_FREE(smb_fname);
596 smb_fname = fsp->fsp_name;
599 * If the caller set the extended oplock request bit
600 * and we granted one (by whatever means) - set the
601 * correct bit for extended oplock reply.
604 if (oplock_request &&
605 (lp_fake_oplocks(SNUM(conn))
606 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
609 * Exclusive oplock granted
612 if (flags & REQUEST_BATCH_OPLOCK) {
613 oplock_granted = BATCH_OPLOCK_RETURN;
614 } else {
615 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
617 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
618 oplock_granted = LEVEL_II_OPLOCK_RETURN;
619 } else {
620 oplock_granted = NO_OPLOCK_RETURN;
623 file_len = smb_fname->st.st_ex_size;
625 if (flags & EXTENDED_RESPONSE_REQUIRED) {
626 /* This is very strange. We
627 * return 50 words, but only set
628 * the wcnt to 42 ? It's definitely
629 * what happens on the wire....
631 reply_outbuf(req, 50, 0);
632 SCVAL(req->outbuf,smb_wct,42);
633 } else {
634 reply_outbuf(req, 34, 0);
637 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
638 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
640 p = (char *)req->outbuf + smb_vwv2;
642 SCVAL(p, 0, oplock_granted);
644 p++;
645 SSVAL(p,0,fsp->fnum);
646 p += 2;
647 if ((create_disposition == FILE_SUPERSEDE)
648 && (info == FILE_WAS_OVERWRITTEN)) {
649 SIVAL(p,0,FILE_WAS_SUPERSEDED);
650 } else {
651 SIVAL(p,0,info);
653 p += 4;
655 fattr = dos_mode(conn, smb_fname);
656 if (fattr == 0) {
657 fattr = FILE_ATTRIBUTE_NORMAL;
660 /* Create time. */
661 create_timespec = get_create_timespec(conn, fsp, smb_fname);
662 a_timespec = smb_fname->st.st_ex_atime;
663 m_timespec = smb_fname->st.st_ex_mtime;
664 c_timespec = get_change_timespec(conn, fsp, smb_fname);
666 if (lp_dos_filetime_resolution(SNUM(conn))) {
667 dos_filetime_timespec(&create_timespec);
668 dos_filetime_timespec(&a_timespec);
669 dos_filetime_timespec(&m_timespec);
670 dos_filetime_timespec(&c_timespec);
673 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
674 p += 8;
675 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
676 p += 8;
677 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
678 p += 8;
679 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
680 p += 8;
681 SIVAL(p,0,fattr); /* File Attributes. */
682 p += 4;
683 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
684 p += 8;
685 SOFF_T(p,0,file_len);
686 p += 8;
687 if (flags & EXTENDED_RESPONSE_REQUIRED) {
688 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
689 size_t num_names = 0;
690 unsigned int num_streams = 0;
691 struct stream_struct *streams = NULL;
693 /* Do we have any EA's ? */
694 status = get_ea_names_from_file(ctx, conn, fsp,
695 smb_fname->base_name, NULL, &num_names);
696 if (NT_STATUS_IS_OK(status) && num_names) {
697 file_status &= ~NO_EAS;
699 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
700 &num_streams, &streams);
701 /* There is always one stream, ::$DATA. */
702 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
703 file_status &= ~NO_SUBSTREAMS;
705 TALLOC_FREE(streams);
706 SSVAL(p,2,file_status);
708 p += 4;
709 SCVAL(p,0,fsp->is_directory ? 1 : 0);
711 if (flags & EXTENDED_RESPONSE_REQUIRED) {
712 uint32 perms = 0;
713 p += 25;
714 if (fsp->is_directory ||
715 fsp->can_write ||
716 can_write_to_file(conn, smb_fname)) {
717 perms = FILE_GENERIC_ALL;
718 } else {
719 perms = FILE_GENERIC_READ|FILE_EXECUTE;
721 SIVAL(p,0,perms);
724 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
725 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
727 out:
728 END_PROFILE(SMBntcreateX);
729 return;
732 /****************************************************************************
733 Reply to a NT_TRANSACT_CREATE call to open a pipe.
734 ****************************************************************************/
736 static void do_nt_transact_create_pipe(connection_struct *conn,
737 struct smb_request *req,
738 uint16 **ppsetup, uint32 setup_count,
739 char **ppparams, uint32 parameter_count,
740 char **ppdata, uint32 data_count)
742 char *fname = NULL;
743 char *params = *ppparams;
744 uint16_t pnum = FNUM_FIELD_INVALID;
745 char *p = NULL;
746 NTSTATUS status;
747 size_t param_len;
748 uint32 flags;
749 TALLOC_CTX *ctx = talloc_tos();
752 * Ensure minimum number of parameters sent.
755 if(parameter_count < 54) {
756 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
757 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
758 return;
761 flags = IVAL(params,0);
763 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
764 parameter_count-53, STR_TERMINATE,
765 &status);
766 if (!NT_STATUS_IS_OK(status)) {
767 reply_nterror(req, status);
768 return;
771 nt_open_pipe(fname, conn, req, &pnum);
773 if (req->outbuf) {
774 /* Error return */
775 return;
778 /* Realloc the size of parameters and data we will return */
779 if (flags & EXTENDED_RESPONSE_REQUIRED) {
780 /* Extended response is 32 more byyes. */
781 param_len = 101;
782 } else {
783 param_len = 69;
785 params = nttrans_realloc(ppparams, param_len);
786 if(params == NULL) {
787 reply_nterror(req, NT_STATUS_NO_MEMORY);
788 return;
791 p = params;
792 SCVAL(p,0,NO_OPLOCK_RETURN);
794 p += 2;
795 SSVAL(p,0,pnum);
796 p += 2;
797 SIVAL(p,0,FILE_WAS_OPENED);
798 p += 8;
800 p += 32;
801 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
802 p += 20;
803 /* File type. */
804 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
805 /* Device state. */
806 SSVAL(p,2, 0x5FF); /* ? */
807 p += 4;
809 if (flags & EXTENDED_RESPONSE_REQUIRED) {
810 p += 25;
811 SIVAL(p,0,FILE_GENERIC_ALL);
813 * For pipes W2K3 seems to return
814 * 0x12019B next.
815 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
817 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
820 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
822 /* Send the required number of replies */
823 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
825 return;
828 /*********************************************************************
829 Windows seems to do canonicalization of inheritance bits. Do the
830 same.
831 *********************************************************************/
833 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
835 bool set_auto_inherited = false;
838 * We need to filter out the
839 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
840 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
841 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
842 * when an ACE is inherited. Otherwise we zero these bits out.
843 * See:
845 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
847 * for details.
850 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
851 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
852 set_auto_inherited = true;
855 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
856 if (set_auto_inherited) {
857 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
861 /****************************************************************************
862 Internal fn to set security descriptors.
863 ****************************************************************************/
865 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
866 uint32_t security_info_sent)
868 NTSTATUS status;
870 if (!CAN_WRITE(fsp->conn)) {
871 return NT_STATUS_ACCESS_DENIED;
874 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
875 return NT_STATUS_OK;
878 if (psd->owner_sid == NULL) {
879 security_info_sent &= ~SECINFO_OWNER;
881 if (psd->group_sid == NULL) {
882 security_info_sent &= ~SECINFO_GROUP;
885 /* Ensure we have at least one thing set. */
886 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
887 /* Just like W2K3 */
888 return NT_STATUS_OK;
891 /* Ensure we have the rights to do this. */
892 if (security_info_sent & SECINFO_OWNER) {
893 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
894 return NT_STATUS_ACCESS_DENIED;
898 if (security_info_sent & SECINFO_GROUP) {
899 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
900 return NT_STATUS_ACCESS_DENIED;
904 if (security_info_sent & SECINFO_DACL) {
905 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
906 return NT_STATUS_ACCESS_DENIED;
908 /* Convert all the generic bits. */
909 if (psd->dacl) {
910 security_acl_map_generic(psd->dacl, &file_generic_mapping);
914 if (security_info_sent & SECINFO_SACL) {
915 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
916 return NT_STATUS_ACCESS_DENIED;
918 /* Convert all the generic bits. */
919 if (psd->sacl) {
920 security_acl_map_generic(psd->sacl, &file_generic_mapping);
924 canonicalize_inheritance_bits(psd);
926 if (DEBUGLEVEL >= 10) {
927 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
928 NDR_PRINT_DEBUG(security_descriptor, psd);
931 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
933 TALLOC_FREE(psd);
935 return status;
938 /****************************************************************************
939 Internal fn to set security descriptors from a data blob.
940 ****************************************************************************/
942 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
943 uint32_t security_info_sent)
945 struct security_descriptor *psd = NULL;
946 NTSTATUS status;
948 if (sd_len == 0) {
949 return NT_STATUS_INVALID_PARAMETER;
952 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
954 if (!NT_STATUS_IS_OK(status)) {
955 return status;
958 return set_sd(fsp, psd, security_info_sent);
961 /****************************************************************************
962 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
963 ****************************************************************************/
965 static void call_nt_transact_create(connection_struct *conn,
966 struct smb_request *req,
967 uint16 **ppsetup, uint32 setup_count,
968 char **ppparams, uint32 parameter_count,
969 char **ppdata, uint32 data_count,
970 uint32 max_data_count)
972 struct smb_filename *smb_fname = NULL;
973 char *fname = NULL;
974 char *params = *ppparams;
975 char *data = *ppdata;
976 /* Breakout the oplock request bits so we can set the reply bits separately. */
977 uint32 fattr=0;
978 off_t file_len = 0;
979 int info = 0;
980 files_struct *fsp = NULL;
981 char *p = NULL;
982 uint32 flags;
983 uint32 access_mask;
984 uint32 file_attributes;
985 uint32 share_access;
986 uint32 create_disposition;
987 uint32 create_options;
988 uint32 sd_len;
989 struct security_descriptor *sd = NULL;
990 uint32 ea_len;
991 uint16 root_dir_fid;
992 struct timespec create_timespec;
993 struct timespec c_timespec;
994 struct timespec a_timespec;
995 struct timespec m_timespec;
996 struct ea_list *ea_list = NULL;
997 NTSTATUS status;
998 size_t param_len;
999 uint64_t allocation_size;
1000 int oplock_request;
1001 uint8_t oplock_granted;
1002 struct case_semantics_state *case_state = NULL;
1003 TALLOC_CTX *ctx = talloc_tos();
1005 DEBUG(5,("call_nt_transact_create\n"));
1008 * If it's an IPC, use the pipe handler.
1011 if (IS_IPC(conn)) {
1012 if (lp_nt_pipe_support()) {
1013 do_nt_transact_create_pipe(
1014 conn, req,
1015 ppsetup, setup_count,
1016 ppparams, parameter_count,
1017 ppdata, data_count);
1018 goto out;
1020 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1021 goto out;
1025 * Ensure minimum number of parameters sent.
1028 if(parameter_count < 54) {
1029 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1030 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1031 goto out;
1034 flags = IVAL(params,0);
1035 access_mask = IVAL(params,8);
1036 file_attributes = IVAL(params,20);
1037 share_access = IVAL(params,24);
1038 create_disposition = IVAL(params,28);
1039 create_options = IVAL(params,32);
1040 sd_len = IVAL(params,36);
1041 ea_len = IVAL(params,40);
1042 root_dir_fid = (uint16)IVAL(params,4);
1043 allocation_size = BVAL(params,12);
1046 * we need to remove ignored bits when they come directly from the client
1047 * because we reuse some of them for internal stuff
1049 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1051 srvstr_get_path(ctx, params, req->flags2, &fname,
1052 params+53, parameter_count-53,
1053 STR_TERMINATE, &status);
1054 if (!NT_STATUS_IS_OK(status)) {
1055 reply_nterror(req, status);
1056 goto out;
1059 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1060 case_state = set_posix_case_semantics(ctx, conn);
1061 if (!case_state) {
1062 reply_nterror(req, NT_STATUS_NO_MEMORY);
1063 goto out;
1067 status = filename_convert(ctx,
1068 conn,
1069 req->flags2 & FLAGS2_DFS_PATHNAMES,
1070 fname,
1071 UCF_PREP_CREATEFILE,
1072 NULL,
1073 &smb_fname);
1075 TALLOC_FREE(case_state);
1077 if (!NT_STATUS_IS_OK(status)) {
1078 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1079 reply_botherror(req,
1080 NT_STATUS_PATH_NOT_COVERED,
1081 ERRSRV, ERRbadpath);
1082 goto out;
1084 reply_nterror(req, status);
1085 goto out;
1088 /* Ensure the data_len is correct for the sd and ea values given. */
1089 if ((ea_len + sd_len > data_count)
1090 || (ea_len > data_count) || (sd_len > data_count)
1091 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1092 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1093 "%u, data_count = %u\n", (unsigned int)ea_len,
1094 (unsigned int)sd_len, (unsigned int)data_count));
1095 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1096 goto out;
1099 if (sd_len) {
1100 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1101 sd_len));
1103 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1104 &sd);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 DEBUG(10, ("call_nt_transact_create: "
1107 "unmarshall_sec_desc failed: %s\n",
1108 nt_errstr(status)));
1109 reply_nterror(req, status);
1110 goto out;
1114 if (ea_len) {
1115 if (!lp_ea_support(SNUM(conn))) {
1116 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1117 "EA's not supported.\n",
1118 (unsigned int)ea_len));
1119 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1120 goto out;
1123 if (ea_len < 10) {
1124 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1125 "too small (should be more than 10)\n",
1126 (unsigned int)ea_len ));
1127 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1128 goto out;
1131 /* We have already checked that ea_len <= data_count here. */
1132 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1133 ea_len);
1134 if (ea_list == NULL) {
1135 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1136 goto out;
1139 if (ea_list_has_invalid_name(ea_list)) {
1140 /* Realloc the size of parameters and data we will return */
1141 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1142 /* Extended response is 32 more byyes. */
1143 param_len = 101;
1144 } else {
1145 param_len = 69;
1147 params = nttrans_realloc(ppparams, param_len);
1148 if(params == NULL) {
1149 reply_nterror(req, NT_STATUS_NO_MEMORY);
1150 goto out;
1153 memset(params, '\0', param_len);
1154 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1155 params, param_len, NULL, 0);
1156 goto out;
1160 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1161 if (oplock_request) {
1162 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1163 ? BATCH_OPLOCK : 0;
1167 * Bug #6898 - clients using Windows opens should
1168 * never be able to set this attribute into the
1169 * VFS.
1171 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1173 status = SMB_VFS_CREATE_FILE(
1174 conn, /* conn */
1175 req, /* req */
1176 root_dir_fid, /* root_dir_fid */
1177 smb_fname, /* fname */
1178 access_mask, /* access_mask */
1179 share_access, /* share_access */
1180 create_disposition, /* create_disposition*/
1181 create_options, /* create_options */
1182 file_attributes, /* file_attributes */
1183 oplock_request, /* oplock_request */
1184 NULL, /* lease */
1185 allocation_size, /* allocation_size */
1186 0, /* private_flags */
1187 sd, /* sd */
1188 ea_list, /* ea_list */
1189 &fsp, /* result */
1190 &info, /* pinfo */
1191 NULL, NULL); /* create context */
1193 if(!NT_STATUS_IS_OK(status)) {
1194 if (open_was_deferred(req->xconn, req->mid)) {
1195 /* We have re-scheduled this call, no error. */
1196 return;
1198 reply_openerror(req, status);
1199 goto out;
1202 /* Ensure we're pointing at the correct stat struct. */
1203 TALLOC_FREE(smb_fname);
1204 smb_fname = fsp->fsp_name;
1207 * If the caller set the extended oplock request bit
1208 * and we granted one (by whatever means) - set the
1209 * correct bit for extended oplock reply.
1212 if (oplock_request &&
1213 (lp_fake_oplocks(SNUM(conn))
1214 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1217 * Exclusive oplock granted
1220 if (flags & REQUEST_BATCH_OPLOCK) {
1221 oplock_granted = BATCH_OPLOCK_RETURN;
1222 } else {
1223 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1225 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1226 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1227 } else {
1228 oplock_granted = NO_OPLOCK_RETURN;
1231 file_len = smb_fname->st.st_ex_size;
1233 /* Realloc the size of parameters and data we will return */
1234 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1235 /* Extended response is 32 more byyes. */
1236 param_len = 101;
1237 } else {
1238 param_len = 69;
1240 params = nttrans_realloc(ppparams, param_len);
1241 if(params == NULL) {
1242 reply_nterror(req, NT_STATUS_NO_MEMORY);
1243 goto out;
1246 p = params;
1247 SCVAL(p, 0, oplock_granted);
1249 p += 2;
1250 SSVAL(p,0,fsp->fnum);
1251 p += 2;
1252 if ((create_disposition == FILE_SUPERSEDE)
1253 && (info == FILE_WAS_OVERWRITTEN)) {
1254 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1255 } else {
1256 SIVAL(p,0,info);
1258 p += 8;
1260 fattr = dos_mode(conn, smb_fname);
1261 if (fattr == 0) {
1262 fattr = FILE_ATTRIBUTE_NORMAL;
1265 /* Create time. */
1266 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1267 a_timespec = smb_fname->st.st_ex_atime;
1268 m_timespec = smb_fname->st.st_ex_mtime;
1269 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1271 if (lp_dos_filetime_resolution(SNUM(conn))) {
1272 dos_filetime_timespec(&create_timespec);
1273 dos_filetime_timespec(&a_timespec);
1274 dos_filetime_timespec(&m_timespec);
1275 dos_filetime_timespec(&c_timespec);
1278 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1279 p += 8;
1280 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1281 p += 8;
1282 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1283 p += 8;
1284 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1285 p += 8;
1286 SIVAL(p,0,fattr); /* File Attributes. */
1287 p += 4;
1288 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1289 p += 8;
1290 SOFF_T(p,0,file_len);
1291 p += 8;
1292 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1293 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1294 size_t num_names = 0;
1295 unsigned int num_streams = 0;
1296 struct stream_struct *streams = NULL;
1298 /* Do we have any EA's ? */
1299 status = get_ea_names_from_file(ctx, conn, fsp,
1300 smb_fname->base_name, NULL, &num_names);
1301 if (NT_STATUS_IS_OK(status) && num_names) {
1302 file_status &= ~NO_EAS;
1304 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1305 &num_streams, &streams);
1306 /* There is always one stream, ::$DATA. */
1307 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1308 file_status &= ~NO_SUBSTREAMS;
1310 TALLOC_FREE(streams);
1311 SSVAL(p,2,file_status);
1313 p += 4;
1314 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1316 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1317 uint32 perms = 0;
1318 p += 25;
1319 if (fsp->is_directory ||
1320 fsp->can_write ||
1321 can_write_to_file(conn, smb_fname)) {
1322 perms = FILE_GENERIC_ALL;
1323 } else {
1324 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1326 SIVAL(p,0,perms);
1329 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1330 smb_fname_str_dbg(smb_fname)));
1332 /* Send the required number of replies */
1333 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1334 out:
1335 return;
1338 /****************************************************************************
1339 Reply to a NT CANCEL request.
1340 conn POINTER CAN BE NULL HERE !
1341 ****************************************************************************/
1343 void reply_ntcancel(struct smb_request *req)
1345 struct smbXsrv_connection *xconn = req->xconn;
1346 struct smbd_server_connection *sconn = req->sconn;
1349 * Go through and cancel any pending change notifies.
1352 START_PROFILE(SMBntcancel);
1353 srv_cancel_sign_response(xconn);
1354 remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1355 remove_pending_lock_requests_by_mid_smb1(sconn, req->mid);
1357 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1358 (unsigned long long)req->mid));
1360 END_PROFILE(SMBntcancel);
1361 return;
1364 /****************************************************************************
1365 Copy a file.
1366 ****************************************************************************/
1368 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1369 connection_struct *conn,
1370 struct smb_request *req,
1371 struct smb_filename *smb_fname_src,
1372 struct smb_filename *smb_fname_dst,
1373 uint32 attrs)
1375 files_struct *fsp1,*fsp2;
1376 uint32 fattr;
1377 int info;
1378 off_t ret=-1;
1379 NTSTATUS status = NT_STATUS_OK;
1380 char *parent;
1382 if (!CAN_WRITE(conn)) {
1383 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1384 goto out;
1387 /* Source must already exist. */
1388 if (!VALID_STAT(smb_fname_src->st)) {
1389 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1390 goto out;
1393 /* Ensure attributes match. */
1394 fattr = dos_mode(conn, smb_fname_src);
1395 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1396 status = NT_STATUS_NO_SUCH_FILE;
1397 goto out;
1400 /* Disallow if dst file already exists. */
1401 if (VALID_STAT(smb_fname_dst->st)) {
1402 status = NT_STATUS_OBJECT_NAME_COLLISION;
1403 goto out;
1406 /* No links from a directory. */
1407 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1408 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1409 goto out;
1412 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1413 smb_fname_str_dbg(smb_fname_src),
1414 smb_fname_str_dbg(smb_fname_dst)));
1416 status = SMB_VFS_CREATE_FILE(
1417 conn, /* conn */
1418 req, /* req */
1419 0, /* root_dir_fid */
1420 smb_fname_src, /* fname */
1421 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1422 FILE_READ_EA, /* access_mask */
1423 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1424 FILE_SHARE_DELETE),
1425 FILE_OPEN, /* create_disposition*/
1426 0, /* create_options */
1427 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1428 NO_OPLOCK, /* oplock_request */
1429 NULL, /* lease */
1430 0, /* allocation_size */
1431 0, /* private_flags */
1432 NULL, /* sd */
1433 NULL, /* ea_list */
1434 &fsp1, /* result */
1435 &info, /* pinfo */
1436 NULL, NULL); /* create context */
1438 if (!NT_STATUS_IS_OK(status)) {
1439 goto out;
1442 status = SMB_VFS_CREATE_FILE(
1443 conn, /* conn */
1444 req, /* req */
1445 0, /* root_dir_fid */
1446 smb_fname_dst, /* fname */
1447 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1448 FILE_WRITE_EA, /* access_mask */
1449 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1450 FILE_SHARE_DELETE),
1451 FILE_CREATE, /* create_disposition*/
1452 0, /* create_options */
1453 fattr, /* file_attributes */
1454 NO_OPLOCK, /* oplock_request */
1455 NULL, /* lease */
1456 0, /* allocation_size */
1457 0, /* private_flags */
1458 NULL, /* sd */
1459 NULL, /* ea_list */
1460 &fsp2, /* result */
1461 &info, /* pinfo */
1462 NULL, NULL); /* create context */
1464 if (!NT_STATUS_IS_OK(status)) {
1465 close_file(NULL, fsp1, ERROR_CLOSE);
1466 goto out;
1469 if (smb_fname_src->st.st_ex_size) {
1470 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1474 * As we are opening fsp1 read-only we only expect
1475 * an error on close on fsp2 if we are out of space.
1476 * Thus we don't look at the error return from the
1477 * close of fsp1.
1479 close_file(NULL, fsp1, NORMAL_CLOSE);
1481 /* Ensure the modtime is set correctly on the destination file. */
1482 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1484 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1486 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1487 creates the file. This isn't the correct thing to do in the copy
1488 case. JRA */
1489 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1490 NULL)) {
1491 status = NT_STATUS_NO_MEMORY;
1492 goto out;
1494 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1495 TALLOC_FREE(parent);
1497 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1498 status = NT_STATUS_DISK_FULL;
1499 goto out;
1501 out:
1502 if (!NT_STATUS_IS_OK(status)) {
1503 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1504 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1505 smb_fname_str_dbg(smb_fname_dst)));
1508 return status;
1511 /****************************************************************************
1512 Reply to a NT rename request.
1513 ****************************************************************************/
1515 void reply_ntrename(struct smb_request *req)
1517 connection_struct *conn = req->conn;
1518 struct smb_filename *smb_fname_old = NULL;
1519 struct smb_filename *smb_fname_new = NULL;
1520 char *oldname = NULL;
1521 char *newname = NULL;
1522 const char *p;
1523 NTSTATUS status;
1524 bool src_has_wcard = False;
1525 bool dest_has_wcard = False;
1526 uint32 attrs;
1527 uint32_t ucf_flags_src = 0;
1528 uint32_t ucf_flags_dst = 0;
1529 uint16 rename_type;
1530 TALLOC_CTX *ctx = talloc_tos();
1531 bool stream_rename = false;
1533 START_PROFILE(SMBntrename);
1535 if (req->wct < 4) {
1536 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1537 goto out;
1540 attrs = SVAL(req->vwv+0, 0);
1541 rename_type = SVAL(req->vwv+1, 0);
1543 p = (const char *)req->buf + 1;
1544 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1545 &status, &src_has_wcard);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 reply_nterror(req, status);
1548 goto out;
1551 if (ms_has_wild(oldname)) {
1552 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1553 goto out;
1556 p++;
1557 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1558 &status, &dest_has_wcard);
1559 if (!NT_STATUS_IS_OK(status)) {
1560 reply_nterror(req, status);
1561 goto out;
1564 if (!lp_posix_pathnames()) {
1565 /* The newname must begin with a ':' if the
1566 oldname contains a ':'. */
1567 if (strchr_m(oldname, ':')) {
1568 if (newname[0] != ':') {
1569 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1570 goto out;
1572 stream_rename = true;
1577 * If this is a rename operation, allow wildcards and save the
1578 * destination's last component.
1580 if (rename_type == RENAME_FLAG_RENAME) {
1581 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1582 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1585 /* rename_internals() calls unix_convert(), so don't call it here. */
1586 status = filename_convert(ctx, conn,
1587 req->flags2 & FLAGS2_DFS_PATHNAMES,
1588 oldname,
1589 ucf_flags_src,
1590 NULL,
1591 &smb_fname_old);
1592 if (!NT_STATUS_IS_OK(status)) {
1593 if (NT_STATUS_EQUAL(status,
1594 NT_STATUS_PATH_NOT_COVERED)) {
1595 reply_botherror(req,
1596 NT_STATUS_PATH_NOT_COVERED,
1597 ERRSRV, ERRbadpath);
1598 goto out;
1600 reply_nterror(req, status);
1601 goto out;
1604 status = filename_convert(ctx, conn,
1605 req->flags2 & FLAGS2_DFS_PATHNAMES,
1606 newname,
1607 ucf_flags_dst,
1608 &dest_has_wcard,
1609 &smb_fname_new);
1610 if (!NT_STATUS_IS_OK(status)) {
1611 if (NT_STATUS_EQUAL(status,
1612 NT_STATUS_PATH_NOT_COVERED)) {
1613 reply_botherror(req,
1614 NT_STATUS_PATH_NOT_COVERED,
1615 ERRSRV, ERRbadpath);
1616 goto out;
1618 reply_nterror(req, status);
1619 goto out;
1622 if (stream_rename) {
1623 /* smb_fname_new must be the same as smb_fname_old. */
1624 TALLOC_FREE(smb_fname_new->base_name);
1625 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1626 smb_fname_old->base_name);
1627 if (!smb_fname_new->base_name) {
1628 reply_nterror(req, NT_STATUS_NO_MEMORY);
1629 goto out;
1633 DEBUG(3,("reply_ntrename: %s -> %s\n",
1634 smb_fname_str_dbg(smb_fname_old),
1635 smb_fname_str_dbg(smb_fname_new)));
1637 switch(rename_type) {
1638 case RENAME_FLAG_RENAME:
1639 status = rename_internals(ctx, conn, req,
1640 smb_fname_old, smb_fname_new,
1641 attrs, False, src_has_wcard,
1642 dest_has_wcard,
1643 DELETE_ACCESS);
1644 break;
1645 case RENAME_FLAG_HARD_LINK:
1646 if (src_has_wcard || dest_has_wcard) {
1647 /* No wildcards. */
1648 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1649 } else {
1650 status = hardlink_internals(ctx, conn,
1651 req,
1652 false,
1653 smb_fname_old,
1654 smb_fname_new);
1656 break;
1657 case RENAME_FLAG_COPY:
1658 if (src_has_wcard || dest_has_wcard) {
1659 /* No wildcards. */
1660 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1661 } else {
1662 status = copy_internals(ctx, conn, req,
1663 smb_fname_old,
1664 smb_fname_new,
1665 attrs);
1667 break;
1668 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1669 status = NT_STATUS_INVALID_PARAMETER;
1670 break;
1671 default:
1672 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1673 break;
1676 if (!NT_STATUS_IS_OK(status)) {
1677 if (open_was_deferred(req->xconn, req->mid)) {
1678 /* We have re-scheduled this call. */
1679 goto out;
1682 reply_nterror(req, status);
1683 goto out;
1686 reply_outbuf(req, 0, 0);
1687 out:
1688 END_PROFILE(SMBntrename);
1689 return;
1692 /****************************************************************************
1693 Reply to a notify change - queue the request and
1694 don't allow a directory to be opened.
1695 ****************************************************************************/
1697 static void smbd_smb1_notify_reply(struct smb_request *req,
1698 NTSTATUS error_code,
1699 uint8_t *buf, size_t len)
1701 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1704 static void call_nt_transact_notify_change(connection_struct *conn,
1705 struct smb_request *req,
1706 uint16 **ppsetup,
1707 uint32 setup_count,
1708 char **ppparams,
1709 uint32 parameter_count,
1710 char **ppdata, uint32 data_count,
1711 uint32 max_data_count,
1712 uint32 max_param_count)
1714 uint16 *setup = *ppsetup;
1715 files_struct *fsp;
1716 uint32 filter;
1717 NTSTATUS status;
1718 bool recursive;
1720 if(setup_count < 6) {
1721 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1722 return;
1725 fsp = file_fsp(req, SVAL(setup,4));
1726 filter = IVAL(setup, 0);
1727 recursive = (SVAL(setup, 6) != 0) ? True : False;
1729 DEBUG(3,("call_nt_transact_notify_change\n"));
1731 if(!fsp) {
1732 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1733 return;
1737 char *filter_string;
1739 if (!(filter_string = notify_filter_string(NULL, filter))) {
1740 reply_nterror(req,NT_STATUS_NO_MEMORY);
1741 return;
1744 DEBUG(3,("call_nt_transact_notify_change: notify change "
1745 "called on %s, filter = %s, recursive = %d\n",
1746 fsp_str_dbg(fsp), filter_string, recursive));
1748 TALLOC_FREE(filter_string);
1751 if((!fsp->is_directory) || (conn != fsp->conn)) {
1752 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1753 return;
1756 if (fsp->notify == NULL) {
1758 status = change_notify_create(fsp, filter, recursive);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 DEBUG(10, ("change_notify_create returned %s\n",
1762 nt_errstr(status)));
1763 reply_nterror(req, status);
1764 return;
1768 if (change_notify_fsp_has_changes(fsp)) {
1771 * We've got changes pending, respond immediately
1775 * TODO: write a torture test to check the filtering behaviour
1776 * here.
1779 change_notify_reply(req,
1780 NT_STATUS_OK,
1781 max_param_count,
1782 fsp->notify,
1783 smbd_smb1_notify_reply);
1786 * change_notify_reply() above has independently sent its
1787 * results
1789 return;
1793 * No changes pending, queue the request
1796 status = change_notify_add_request(req,
1797 max_param_count,
1798 filter,
1799 recursive, fsp,
1800 smbd_smb1_notify_reply);
1801 if (!NT_STATUS_IS_OK(status)) {
1802 reply_nterror(req, status);
1804 return;
1807 /****************************************************************************
1808 Reply to an NT transact rename command.
1809 ****************************************************************************/
1811 static void call_nt_transact_rename(connection_struct *conn,
1812 struct smb_request *req,
1813 uint16 **ppsetup, uint32 setup_count,
1814 char **ppparams, uint32 parameter_count,
1815 char **ppdata, uint32 data_count,
1816 uint32 max_data_count)
1818 char *params = *ppparams;
1819 char *new_name = NULL;
1820 files_struct *fsp = NULL;
1821 bool dest_has_wcard = False;
1822 NTSTATUS status;
1823 TALLOC_CTX *ctx = talloc_tos();
1825 if(parameter_count < 5) {
1826 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1827 return;
1830 fsp = file_fsp(req, SVAL(params, 0));
1831 if (!check_fsp(conn, req, fsp)) {
1832 return;
1834 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1835 parameter_count - 4,
1836 STR_TERMINATE, &status, &dest_has_wcard);
1837 if (!NT_STATUS_IS_OK(status)) {
1838 reply_nterror(req, status);
1839 return;
1843 * W2K3 ignores this request as the RAW-RENAME test
1844 * demonstrates, so we do.
1846 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1848 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1849 fsp_str_dbg(fsp), new_name));
1851 return;
1854 /******************************************************************************
1855 Fake up a completely empty SD.
1856 *******************************************************************************/
1858 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1860 size_t sd_size;
1862 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1863 if(!*ppsd) {
1864 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1865 return NT_STATUS_NO_MEMORY;
1868 return NT_STATUS_OK;
1871 /****************************************************************************
1872 Reply to query a security descriptor.
1873 Callable from SMB1 and SMB2.
1874 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1875 the required size.
1876 ****************************************************************************/
1878 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1879 TALLOC_CTX *mem_ctx,
1880 files_struct *fsp,
1881 uint32_t security_info_wanted,
1882 uint32_t max_data_count,
1883 uint8_t **ppmarshalled_sd,
1884 size_t *psd_size)
1886 NTSTATUS status;
1887 struct security_descriptor *psd = NULL;
1888 TALLOC_CTX *frame = talloc_stackframe();
1891 * Get the permissions to return.
1894 if ((security_info_wanted & SECINFO_SACL) &&
1895 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1896 DEBUG(10, ("Access to SACL denied.\n"));
1897 TALLOC_FREE(frame);
1898 return NT_STATUS_ACCESS_DENIED;
1901 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1902 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1903 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1904 TALLOC_FREE(frame);
1905 return NT_STATUS_ACCESS_DENIED;
1908 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1909 SECINFO_GROUP|SECINFO_SACL)) {
1910 /* Don't return SECINFO_LABEL if anything else was
1911 requested. See bug #8458. */
1912 security_info_wanted &= ~SECINFO_LABEL;
1915 if (!lp_nt_acl_support(SNUM(conn))) {
1916 status = get_null_nt_acl(frame, &psd);
1917 } else if (security_info_wanted & SECINFO_LABEL) {
1918 /* Like W2K3 return a null object. */
1919 status = get_null_nt_acl(frame, &psd);
1920 } else {
1921 status = SMB_VFS_FGET_NT_ACL(
1922 fsp, security_info_wanted, frame, &psd);
1924 if (!NT_STATUS_IS_OK(status)) {
1925 TALLOC_FREE(frame);
1926 return status;
1929 if (!(security_info_wanted & SECINFO_OWNER)) {
1930 psd->owner_sid = NULL;
1932 if (!(security_info_wanted & SECINFO_GROUP)) {
1933 psd->group_sid = NULL;
1935 if (!(security_info_wanted & SECINFO_DACL)) {
1936 psd->type &= ~SEC_DESC_DACL_PRESENT;
1937 psd->dacl = NULL;
1939 if (!(security_info_wanted & SECINFO_SACL)) {
1940 psd->type &= ~SEC_DESC_SACL_PRESENT;
1941 psd->sacl = NULL;
1944 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1945 * present in the reply to match Windows behavior */
1946 if (psd->sacl == NULL &&
1947 security_info_wanted & SECINFO_SACL)
1948 psd->type |= SEC_DESC_SACL_PRESENT;
1949 if (psd->dacl == NULL &&
1950 security_info_wanted & SECINFO_DACL)
1951 psd->type |= SEC_DESC_DACL_PRESENT;
1953 if (security_info_wanted & SECINFO_LABEL) {
1954 /* Like W2K3 return a null object. */
1955 psd->owner_sid = NULL;
1956 psd->group_sid = NULL;
1957 psd->dacl = NULL;
1958 psd->sacl = NULL;
1959 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1962 *psd_size = ndr_size_security_descriptor(psd, 0);
1964 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1965 (unsigned long)*psd_size));
1967 if (DEBUGLEVEL >= 10) {
1968 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1969 fsp_str_dbg(fsp)));
1970 NDR_PRINT_DEBUG(security_descriptor, psd);
1973 if (max_data_count < *psd_size) {
1974 TALLOC_FREE(frame);
1975 return NT_STATUS_BUFFER_TOO_SMALL;
1978 status = marshall_sec_desc(mem_ctx, psd,
1979 ppmarshalled_sd, psd_size);
1981 if (!NT_STATUS_IS_OK(status)) {
1982 TALLOC_FREE(frame);
1983 return status;
1986 TALLOC_FREE(frame);
1987 return NT_STATUS_OK;
1990 /****************************************************************************
1991 SMB1 reply to query a security descriptor.
1992 ****************************************************************************/
1994 static void call_nt_transact_query_security_desc(connection_struct *conn,
1995 struct smb_request *req,
1996 uint16 **ppsetup,
1997 uint32 setup_count,
1998 char **ppparams,
1999 uint32 parameter_count,
2000 char **ppdata,
2001 uint32 data_count,
2002 uint32 max_data_count)
2004 char *params = *ppparams;
2005 char *data = *ppdata;
2006 size_t sd_size = 0;
2007 uint32 security_info_wanted;
2008 files_struct *fsp = NULL;
2009 NTSTATUS status;
2010 uint8_t *marshalled_sd = NULL;
2012 if(parameter_count < 8) {
2013 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2014 return;
2017 fsp = file_fsp(req, SVAL(params,0));
2018 if(!fsp) {
2019 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2020 return;
2023 security_info_wanted = IVAL(params,4);
2025 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2026 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2027 (unsigned int)security_info_wanted));
2029 params = nttrans_realloc(ppparams, 4);
2030 if(params == NULL) {
2031 reply_nterror(req, NT_STATUS_NO_MEMORY);
2032 return;
2036 * Get the permissions to return.
2039 status = smbd_do_query_security_desc(conn,
2040 talloc_tos(),
2041 fsp,
2042 security_info_wanted &
2043 SMB_SUPPORTED_SECINFO_FLAGS,
2044 max_data_count,
2045 &marshalled_sd,
2046 &sd_size);
2048 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2049 SIVAL(params,0,(uint32_t)sd_size);
2050 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2051 params, 4, NULL, 0);
2052 return;
2055 if (!NT_STATUS_IS_OK(status)) {
2056 reply_nterror(req, status);
2057 return;
2060 SMB_ASSERT(sd_size > 0);
2062 SIVAL(params,0,(uint32_t)sd_size);
2064 if (max_data_count < sd_size) {
2065 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2066 params, 4, NULL, 0);
2067 return;
2071 * Allocate the data we will return.
2074 data = nttrans_realloc(ppdata, sd_size);
2075 if(data == NULL) {
2076 reply_nterror(req, NT_STATUS_NO_MEMORY);
2077 return;
2080 memcpy(data, marshalled_sd, sd_size);
2082 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2084 return;
2087 /****************************************************************************
2088 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2089 ****************************************************************************/
2091 static void call_nt_transact_set_security_desc(connection_struct *conn,
2092 struct smb_request *req,
2093 uint16 **ppsetup,
2094 uint32 setup_count,
2095 char **ppparams,
2096 uint32 parameter_count,
2097 char **ppdata,
2098 uint32 data_count,
2099 uint32 max_data_count)
2101 char *params= *ppparams;
2102 char *data = *ppdata;
2103 files_struct *fsp = NULL;
2104 uint32 security_info_sent = 0;
2105 NTSTATUS status;
2107 if(parameter_count < 8) {
2108 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2109 return;
2112 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2113 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2114 return;
2117 if (!CAN_WRITE(fsp->conn)) {
2118 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2119 return;
2122 if(!lp_nt_acl_support(SNUM(conn))) {
2123 goto done;
2126 security_info_sent = IVAL(params,4);
2128 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2129 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2131 if (data_count == 0) {
2132 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2133 return;
2136 status = set_sd_blob(fsp, (uint8 *)data, data_count,
2137 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
2138 if (!NT_STATUS_IS_OK(status)) {
2139 reply_nterror(req, status);
2140 return;
2143 done:
2144 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2145 return;
2148 /****************************************************************************
2149 Reply to NT IOCTL
2150 ****************************************************************************/
2152 static void call_nt_transact_ioctl(connection_struct *conn,
2153 struct smb_request *req,
2154 uint16 **ppsetup, uint32 setup_count,
2155 char **ppparams, uint32 parameter_count,
2156 char **ppdata, uint32 data_count,
2157 uint32 max_data_count)
2159 NTSTATUS status;
2160 uint32 function;
2161 uint16 fidnum;
2162 files_struct *fsp;
2163 uint8 isFSctl;
2164 uint8 compfilter;
2165 char *out_data = NULL;
2166 uint32 out_data_len = 0;
2167 char *pdata = *ppdata;
2168 TALLOC_CTX *ctx = talloc_tos();
2170 if (setup_count != 8) {
2171 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2172 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2173 return;
2176 function = IVAL(*ppsetup, 0);
2177 fidnum = SVAL(*ppsetup, 4);
2178 isFSctl = CVAL(*ppsetup, 6);
2179 compfilter = CVAL(*ppsetup, 7);
2181 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2182 function, fidnum, isFSctl, compfilter));
2184 fsp=file_fsp(req, fidnum);
2187 * We don't really implement IOCTLs, especially on files.
2189 if (!isFSctl) {
2190 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2191 isFSctl));
2192 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2193 return;
2196 /* Has to be for an open file! */
2197 if (!check_fsp_open(conn, req, fsp)) {
2198 return;
2201 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2204 * out_data might be allocated by the VFS module, but talloc should be
2205 * used, and should be cleaned up when the request ends.
2207 status = SMB_VFS_FSCTL(fsp,
2208 ctx,
2209 function,
2210 req->flags2,
2211 (uint8_t *)pdata,
2212 data_count,
2213 (uint8_t **)&out_data,
2214 max_data_count,
2215 &out_data_len);
2216 if (!NT_STATUS_IS_OK(status)) {
2217 reply_nterror(req, status);
2218 } else {
2219 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2224 #ifdef HAVE_SYS_QUOTAS
2225 /****************************************************************************
2226 Reply to get user quota
2227 ****************************************************************************/
2229 static void call_nt_transact_get_user_quota(connection_struct *conn,
2230 struct smb_request *req,
2231 uint16 **ppsetup,
2232 uint32 setup_count,
2233 char **ppparams,
2234 uint32 parameter_count,
2235 char **ppdata,
2236 uint32 data_count,
2237 uint32 max_data_count)
2239 NTSTATUS nt_status = NT_STATUS_OK;
2240 char *params = *ppparams;
2241 char *pdata = *ppdata;
2242 char *entry;
2243 int data_len=0,param_len=0;
2244 int qt_len=0;
2245 int entry_len = 0;
2246 files_struct *fsp = NULL;
2247 uint16 level = 0;
2248 size_t sid_len;
2249 struct dom_sid sid;
2250 bool start_enum = True;
2251 SMB_NTQUOTA_STRUCT qt;
2252 SMB_NTQUOTA_LIST *tmp_list;
2253 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2255 ZERO_STRUCT(qt);
2257 /* access check */
2258 if (get_current_uid(conn) != 0) {
2259 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2260 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2261 conn->session_info->unix_info->unix_name));
2262 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2263 return;
2267 * Ensure minimum number of parameters sent.
2270 if (parameter_count < 4) {
2271 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2272 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2273 return;
2276 /* maybe we can check the quota_fnum */
2277 fsp = file_fsp(req, SVAL(params,0));
2278 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2279 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2280 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2281 return;
2284 /* the NULL pointer checking for fsp->fake_file_handle->pd
2285 * is done by CHECK_NTQUOTA_HANDLE_OK()
2287 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2289 level = SVAL(params,2);
2291 /* unknown 12 bytes leading in params */
2293 switch (level) {
2294 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2295 /* seems that we should continue with the enum here --metze */
2297 if (qt_handle->quota_list!=NULL &&
2298 qt_handle->tmp_list==NULL) {
2300 /* free the list */
2301 free_ntquota_list(&(qt_handle->quota_list));
2303 /* Realloc the size of parameters and data we will return */
2304 param_len = 4;
2305 params = nttrans_realloc(ppparams, param_len);
2306 if(params == NULL) {
2307 reply_nterror(req, NT_STATUS_NO_MEMORY);
2308 return;
2311 data_len = 0;
2312 SIVAL(params,0,data_len);
2314 break;
2317 start_enum = False;
2319 case TRANSACT_GET_USER_QUOTA_LIST_START:
2321 if (qt_handle->quota_list==NULL &&
2322 qt_handle->tmp_list==NULL) {
2323 start_enum = True;
2326 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2327 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2328 return;
2331 /* Realloc the size of parameters and data we will return */
2332 param_len = 4;
2333 params = nttrans_realloc(ppparams, param_len);
2334 if(params == NULL) {
2335 reply_nterror(req, NT_STATUS_NO_MEMORY);
2336 return;
2339 /* we should not trust the value in max_data_count*/
2340 max_data_count = MIN(max_data_count,2048);
2342 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2343 if(pdata == NULL) {
2344 reply_nterror(req, NT_STATUS_NO_MEMORY);
2345 return;
2348 entry = pdata;
2350 /* set params Size of returned Quota Data 4 bytes*/
2351 /* but set it later when we know it */
2353 /* for each entry push the data */
2355 if (start_enum) {
2356 qt_handle->tmp_list = qt_handle->quota_list;
2359 tmp_list = qt_handle->tmp_list;
2361 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2362 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2364 sid_len = ndr_size_dom_sid(
2365 &tmp_list->quotas->sid, 0);
2366 entry_len = 40 + sid_len;
2368 /* nextoffset entry 4 bytes */
2369 SIVAL(entry,0,entry_len);
2371 /* then the len of the SID 4 bytes */
2372 SIVAL(entry,4,sid_len);
2374 /* unknown data 8 bytes uint64_t */
2375 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2377 /* the used disk space 8 bytes uint64_t */
2378 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2380 /* the soft quotas 8 bytes uint64_t */
2381 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2383 /* the hard quotas 8 bytes uint64_t */
2384 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2386 /* and now the SID */
2387 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2390 qt_handle->tmp_list = tmp_list;
2392 /* overwrite the offset of the last entry */
2393 SIVAL(entry-entry_len,0,0);
2395 data_len = 4+qt_len;
2396 /* overwrite the params quota_data_len */
2397 SIVAL(params,0,data_len);
2399 break;
2401 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2403 /* unknown 4 bytes IVAL(pdata,0) */
2405 if (data_count < 8) {
2406 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2407 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2408 return;
2411 sid_len = IVAL(pdata,4);
2412 /* Ensure this is less than 1mb. */
2413 if (sid_len > (1024*1024)) {
2414 reply_nterror(req, NT_STATUS_NO_MEMORY);
2415 return;
2418 if (data_count < 8+sid_len) {
2419 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2420 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2421 return;
2424 data_len = 4+40+sid_len;
2426 if (max_data_count < data_len) {
2427 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2428 max_data_count, data_len));
2429 param_len = 4;
2430 SIVAL(params,0,data_len);
2431 data_len = 0;
2432 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2433 break;
2436 if (!sid_parse(pdata+8,sid_len,&sid)) {
2437 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2438 return;
2441 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2442 ZERO_STRUCT(qt);
2444 * we have to return zero's in all fields
2445 * instead of returning an error here
2446 * --metze
2450 /* Realloc the size of parameters and data we will return */
2451 param_len = 4;
2452 params = nttrans_realloc(ppparams, param_len);
2453 if(params == NULL) {
2454 reply_nterror(req, NT_STATUS_NO_MEMORY);
2455 return;
2458 pdata = nttrans_realloc(ppdata, data_len);
2459 if(pdata == NULL) {
2460 reply_nterror(req, NT_STATUS_NO_MEMORY);
2461 return;
2464 entry = pdata;
2466 /* set params Size of returned Quota Data 4 bytes*/
2467 SIVAL(params,0,data_len);
2469 /* nextoffset entry 4 bytes */
2470 SIVAL(entry,0,0);
2472 /* then the len of the SID 4 bytes */
2473 SIVAL(entry,4,sid_len);
2475 /* unknown data 8 bytes uint64_t */
2476 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2478 /* the used disk space 8 bytes uint64_t */
2479 SBIG_UINT(entry,16,qt.usedspace);
2481 /* the soft quotas 8 bytes uint64_t */
2482 SBIG_UINT(entry,24,qt.softlim);
2484 /* the hard quotas 8 bytes uint64_t */
2485 SBIG_UINT(entry,32,qt.hardlim);
2487 /* and now the SID */
2488 sid_linearize(entry+40, sid_len, &sid);
2490 break;
2492 default:
2493 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2494 "level 0x%04hX\n",
2495 fsp_fnum_dbg(fsp), level));
2496 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2497 return;
2498 break;
2501 send_nt_replies(conn, req, nt_status, params, param_len,
2502 pdata, data_len);
2505 /****************************************************************************
2506 Reply to set user quota
2507 ****************************************************************************/
2509 static void call_nt_transact_set_user_quota(connection_struct *conn,
2510 struct smb_request *req,
2511 uint16 **ppsetup,
2512 uint32 setup_count,
2513 char **ppparams,
2514 uint32 parameter_count,
2515 char **ppdata,
2516 uint32 data_count,
2517 uint32 max_data_count)
2519 char *params = *ppparams;
2520 char *pdata = *ppdata;
2521 int data_len=0,param_len=0;
2522 SMB_NTQUOTA_STRUCT qt;
2523 size_t sid_len;
2524 struct dom_sid sid;
2525 files_struct *fsp = NULL;
2527 ZERO_STRUCT(qt);
2529 /* access check */
2530 if (get_current_uid(conn) != 0) {
2531 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2532 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2533 conn->session_info->unix_info->unix_name));
2534 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2535 return;
2539 * Ensure minimum number of parameters sent.
2542 if (parameter_count < 2) {
2543 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2544 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2545 return;
2548 /* maybe we can check the quota_fnum */
2549 fsp = file_fsp(req, SVAL(params,0));
2550 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2551 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2552 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2553 return;
2556 if (data_count < 40) {
2557 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2558 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2559 return;
2562 /* offset to next quota record.
2563 * 4 bytes IVAL(pdata,0)
2564 * unused here...
2567 /* sid len */
2568 sid_len = IVAL(pdata,4);
2570 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2571 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2572 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2573 return;
2576 /* unknown 8 bytes in pdata
2577 * maybe its the change time in NTTIME
2580 /* the used space 8 bytes (uint64_t)*/
2581 qt.usedspace = BVAL(pdata,16);
2583 /* the soft quotas 8 bytes (uint64_t)*/
2584 qt.softlim = BVAL(pdata,24);
2586 /* the hard quotas 8 bytes (uint64_t)*/
2587 qt.hardlim = BVAL(pdata,32);
2589 if (!sid_parse(pdata+40,sid_len,&sid)) {
2590 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2591 return;
2594 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2596 /* 44 unknown bytes left... */
2598 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2599 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2600 return;
2603 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2604 pdata, data_len);
2606 #endif /* HAVE_SYS_QUOTAS */
2608 static void handle_nttrans(connection_struct *conn,
2609 struct trans_state *state,
2610 struct smb_request *req)
2612 if (get_Protocol() >= PROTOCOL_NT1) {
2613 req->flags2 |= 0x40; /* IS_LONG_NAME */
2614 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2618 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2620 /* Now we must call the relevant NT_TRANS function */
2621 switch(state->call) {
2622 case NT_TRANSACT_CREATE:
2624 START_PROFILE(NT_transact_create);
2625 call_nt_transact_create(
2626 conn, req,
2627 &state->setup, state->setup_count,
2628 &state->param, state->total_param,
2629 &state->data, state->total_data,
2630 state->max_data_return);
2631 END_PROFILE(NT_transact_create);
2632 break;
2635 case NT_TRANSACT_IOCTL:
2637 START_PROFILE(NT_transact_ioctl);
2638 call_nt_transact_ioctl(
2639 conn, req,
2640 &state->setup, state->setup_count,
2641 &state->param, state->total_param,
2642 &state->data, state->total_data,
2643 state->max_data_return);
2644 END_PROFILE(NT_transact_ioctl);
2645 break;
2648 case NT_TRANSACT_SET_SECURITY_DESC:
2650 START_PROFILE(NT_transact_set_security_desc);
2651 call_nt_transact_set_security_desc(
2652 conn, req,
2653 &state->setup, state->setup_count,
2654 &state->param, state->total_param,
2655 &state->data, state->total_data,
2656 state->max_data_return);
2657 END_PROFILE(NT_transact_set_security_desc);
2658 break;
2661 case NT_TRANSACT_NOTIFY_CHANGE:
2663 START_PROFILE(NT_transact_notify_change);
2664 call_nt_transact_notify_change(
2665 conn, req,
2666 &state->setup, state->setup_count,
2667 &state->param, state->total_param,
2668 &state->data, state->total_data,
2669 state->max_data_return,
2670 state->max_param_return);
2671 END_PROFILE(NT_transact_notify_change);
2672 break;
2675 case NT_TRANSACT_RENAME:
2677 START_PROFILE(NT_transact_rename);
2678 call_nt_transact_rename(
2679 conn, req,
2680 &state->setup, state->setup_count,
2681 &state->param, state->total_param,
2682 &state->data, state->total_data,
2683 state->max_data_return);
2684 END_PROFILE(NT_transact_rename);
2685 break;
2688 case NT_TRANSACT_QUERY_SECURITY_DESC:
2690 START_PROFILE(NT_transact_query_security_desc);
2691 call_nt_transact_query_security_desc(
2692 conn, req,
2693 &state->setup, state->setup_count,
2694 &state->param, state->total_param,
2695 &state->data, state->total_data,
2696 state->max_data_return);
2697 END_PROFILE(NT_transact_query_security_desc);
2698 break;
2701 #ifdef HAVE_SYS_QUOTAS
2702 case NT_TRANSACT_GET_USER_QUOTA:
2704 START_PROFILE(NT_transact_get_user_quota);
2705 call_nt_transact_get_user_quota(
2706 conn, req,
2707 &state->setup, state->setup_count,
2708 &state->param, state->total_param,
2709 &state->data, state->total_data,
2710 state->max_data_return);
2711 END_PROFILE(NT_transact_get_user_quota);
2712 break;
2715 case NT_TRANSACT_SET_USER_QUOTA:
2717 START_PROFILE(NT_transact_set_user_quota);
2718 call_nt_transact_set_user_quota(
2719 conn, req,
2720 &state->setup, state->setup_count,
2721 &state->param, state->total_param,
2722 &state->data, state->total_data,
2723 state->max_data_return);
2724 END_PROFILE(NT_transact_set_user_quota);
2725 break;
2727 #endif /* HAVE_SYS_QUOTAS */
2729 default:
2730 /* Error in request */
2731 DEBUG(0,("handle_nttrans: Unknown request %d in "
2732 "nttrans call\n", state->call));
2733 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2734 return;
2736 return;
2739 /****************************************************************************
2740 Reply to a SMBNTtrans.
2741 ****************************************************************************/
2743 void reply_nttrans(struct smb_request *req)
2745 connection_struct *conn = req->conn;
2746 uint32_t pscnt;
2747 uint32_t psoff;
2748 uint32_t dscnt;
2749 uint32_t dsoff;
2750 uint16 function_code;
2751 NTSTATUS result;
2752 struct trans_state *state;
2754 START_PROFILE(SMBnttrans);
2756 if (req->wct < 19) {
2757 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2758 END_PROFILE(SMBnttrans);
2759 return;
2762 pscnt = IVAL(req->vwv+9, 1);
2763 psoff = IVAL(req->vwv+11, 1);
2764 dscnt = IVAL(req->vwv+13, 1);
2765 dsoff = IVAL(req->vwv+15, 1);
2766 function_code = SVAL(req->vwv+18, 0);
2768 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2769 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2770 END_PROFILE(SMBnttrans);
2771 return;
2774 result = allow_new_trans(conn->pending_trans, req->mid);
2775 if (!NT_STATUS_IS_OK(result)) {
2776 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2777 reply_nterror(req, result);
2778 END_PROFILE(SMBnttrans);
2779 return;
2782 if ((state = talloc(conn, struct trans_state)) == NULL) {
2783 reply_nterror(req, NT_STATUS_NO_MEMORY);
2784 END_PROFILE(SMBnttrans);
2785 return;
2788 state->cmd = SMBnttrans;
2790 state->mid = req->mid;
2791 state->vuid = req->vuid;
2792 state->total_data = IVAL(req->vwv+3, 1);
2793 state->data = NULL;
2794 state->total_param = IVAL(req->vwv+1, 1);
2795 state->param = NULL;
2796 state->max_data_return = IVAL(req->vwv+7, 1);
2797 state->max_param_return = IVAL(req->vwv+5, 1);
2799 /* setup count is in *words* */
2800 state->setup_count = 2*CVAL(req->vwv+17, 1);
2801 state->setup = NULL;
2802 state->call = function_code;
2804 DEBUG(10, ("num_setup=%u, "
2805 "param_total=%u, this_param=%u, max_param=%u, "
2806 "data_total=%u, this_data=%u, max_data=%u, "
2807 "param_offset=%u, data_offset=%u\n",
2808 (unsigned)state->setup_count,
2809 (unsigned)state->total_param, (unsigned)pscnt,
2810 (unsigned)state->max_param_return,
2811 (unsigned)state->total_data, (unsigned)dscnt,
2812 (unsigned)state->max_data_return,
2813 (unsigned)psoff, (unsigned)dsoff));
2816 * All nttrans messages we handle have smb_wct == 19 +
2817 * state->setup_count. Ensure this is so as a sanity check.
2820 if(req->wct != 19 + (state->setup_count/2)) {
2821 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2822 req->wct, 19 + (state->setup_count/2)));
2823 goto bad_param;
2826 /* Don't allow more than 128mb for each value. */
2827 if ((state->total_data > (1024*1024*128)) ||
2828 (state->total_param > (1024*1024*128))) {
2829 reply_nterror(req, NT_STATUS_NO_MEMORY);
2830 END_PROFILE(SMBnttrans);
2831 return;
2834 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2835 goto bad_param;
2837 if (state->total_data) {
2839 if (trans_oob(state->total_data, 0, dscnt)
2840 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2841 goto bad_param;
2844 /* Can't use talloc here, the core routines do realloc on the
2845 * params and data. */
2846 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2847 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2848 "bytes !\n", (unsigned int)state->total_data));
2849 TALLOC_FREE(state);
2850 reply_nterror(req, NT_STATUS_NO_MEMORY);
2851 END_PROFILE(SMBnttrans);
2852 return;
2855 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2858 if (state->total_param) {
2860 if (trans_oob(state->total_param, 0, pscnt)
2861 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2862 goto bad_param;
2865 /* Can't use talloc here, the core routines do realloc on the
2866 * params and data. */
2867 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2868 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2869 "bytes !\n", (unsigned int)state->total_param));
2870 SAFE_FREE(state->data);
2871 TALLOC_FREE(state);
2872 reply_nterror(req, NT_STATUS_NO_MEMORY);
2873 END_PROFILE(SMBnttrans);
2874 return;
2877 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2880 state->received_data = dscnt;
2881 state->received_param = pscnt;
2883 if(state->setup_count > 0) {
2884 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2885 state->setup_count));
2888 * No overflow possible here, state->setup_count is an
2889 * unsigned int, being filled by a single byte from
2890 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2891 * below is not necessary, it's here to clarify things. The
2892 * validity of req->vwv and req->wct has been checked in
2893 * init_smb_request already.
2895 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2896 goto bad_param;
2899 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2900 if (state->setup == NULL) {
2901 DEBUG(0,("reply_nttrans : Out of memory\n"));
2902 SAFE_FREE(state->data);
2903 SAFE_FREE(state->param);
2904 TALLOC_FREE(state);
2905 reply_nterror(req, NT_STATUS_NO_MEMORY);
2906 END_PROFILE(SMBnttrans);
2907 return;
2910 memcpy(state->setup, req->vwv+19, state->setup_count);
2911 dump_data(10, (uint8 *)state->setup, state->setup_count);
2914 if ((state->received_data == state->total_data) &&
2915 (state->received_param == state->total_param)) {
2916 handle_nttrans(conn, state, req);
2917 SAFE_FREE(state->param);
2918 SAFE_FREE(state->data);
2919 TALLOC_FREE(state);
2920 END_PROFILE(SMBnttrans);
2921 return;
2924 DLIST_ADD(conn->pending_trans, state);
2926 /* We need to send an interim response then receive the rest
2927 of the parameter/data bytes */
2928 reply_outbuf(req, 0, 0);
2929 show_msg((char *)req->outbuf);
2930 END_PROFILE(SMBnttrans);
2931 return;
2933 bad_param:
2935 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2936 SAFE_FREE(state->data);
2937 SAFE_FREE(state->param);
2938 TALLOC_FREE(state);
2939 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2940 END_PROFILE(SMBnttrans);
2941 return;
2944 /****************************************************************************
2945 Reply to a SMBnttranss
2946 ****************************************************************************/
2948 void reply_nttranss(struct smb_request *req)
2950 connection_struct *conn = req->conn;
2951 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2952 struct trans_state *state;
2954 START_PROFILE(SMBnttranss);
2956 show_msg((const char *)req->inbuf);
2958 /* Windows clients expect all replies to
2959 an NT transact secondary (SMBnttranss 0xA1)
2960 to have a command code of NT transact
2961 (SMBnttrans 0xA0). See bug #8989 for details. */
2962 req->cmd = SMBnttrans;
2964 if (req->wct < 18) {
2965 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2966 END_PROFILE(SMBnttranss);
2967 return;
2970 for (state = conn->pending_trans; state != NULL;
2971 state = state->next) {
2972 if (state->mid == req->mid) {
2973 break;
2977 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2978 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2979 END_PROFILE(SMBnttranss);
2980 return;
2983 /* Revise state->total_param and state->total_data in case they have
2984 changed downwards */
2985 if (IVAL(req->vwv+1, 1) < state->total_param) {
2986 state->total_param = IVAL(req->vwv+1, 1);
2988 if (IVAL(req->vwv+3, 1) < state->total_data) {
2989 state->total_data = IVAL(req->vwv+3, 1);
2992 pcnt = IVAL(req->vwv+5, 1);
2993 poff = IVAL(req->vwv+7, 1);
2994 pdisp = IVAL(req->vwv+9, 1);
2996 dcnt = IVAL(req->vwv+11, 1);
2997 doff = IVAL(req->vwv+13, 1);
2998 ddisp = IVAL(req->vwv+15, 1);
3000 state->received_param += pcnt;
3001 state->received_data += dcnt;
3003 if ((state->received_data > state->total_data) ||
3004 (state->received_param > state->total_param))
3005 goto bad_param;
3007 if (pcnt) {
3008 if (trans_oob(state->total_param, pdisp, pcnt)
3009 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3010 goto bad_param;
3012 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3015 if (dcnt) {
3016 if (trans_oob(state->total_data, ddisp, dcnt)
3017 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3018 goto bad_param;
3020 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3023 if ((state->received_param < state->total_param) ||
3024 (state->received_data < state->total_data)) {
3025 END_PROFILE(SMBnttranss);
3026 return;
3029 handle_nttrans(conn, state, req);
3031 DLIST_REMOVE(conn->pending_trans, state);
3032 SAFE_FREE(state->data);
3033 SAFE_FREE(state->param);
3034 TALLOC_FREE(state);
3035 END_PROFILE(SMBnttranss);
3036 return;
3038 bad_param:
3040 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3041 DLIST_REMOVE(conn->pending_trans, state);
3042 SAFE_FREE(state->data);
3043 SAFE_FREE(state->param);
3044 TALLOC_FREE(state);
3045 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3046 END_PROFILE(SMBnttranss);
3047 return;