traffic_packets: provision request data for packet_drsuapi_13
[Samba.git] / source3 / smbd / nttrans.c
blobca02dbcc3d9969f6de3c46c8983143c0049392a4
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_t 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_t flags;
442 uint32_t access_mask;
443 uint32_t file_attributes;
444 uint32_t share_access;
445 uint32_t create_disposition;
446 uint32_t create_options;
447 uint16_t 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_t 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 uint32_t ucf_flags;
465 TALLOC_CTX *ctx = talloc_tos();
467 START_PROFILE(SMBntcreateX);
469 if (req->wct < 24) {
470 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
471 goto out;
474 flags = IVAL(req->vwv+3, 1);
475 access_mask = IVAL(req->vwv+7, 1);
476 file_attributes = IVAL(req->vwv+13, 1);
477 share_access = IVAL(req->vwv+15, 1);
478 create_disposition = IVAL(req->vwv+17, 1);
479 create_options = IVAL(req->vwv+19, 1);
480 root_dir_fid = (uint16_t)IVAL(req->vwv+5, 1);
482 allocation_size = BVAL(req->vwv+9, 1);
484 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
485 STR_TERMINATE, &status);
487 if (!NT_STATUS_IS_OK(status)) {
488 reply_nterror(req, status);
489 goto out;
492 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
493 "file_attributes = 0x%x, share_access = 0x%x, "
494 "create_disposition = 0x%x create_options = 0x%x "
495 "root_dir_fid = 0x%x, fname = %s\n",
496 (unsigned int)flags,
497 (unsigned int)access_mask,
498 (unsigned int)file_attributes,
499 (unsigned int)share_access,
500 (unsigned int)create_disposition,
501 (unsigned int)create_options,
502 (unsigned int)root_dir_fid,
503 fname));
506 * we need to remove ignored bits when they come directly from the client
507 * because we reuse some of them for internal stuff
509 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
512 * If it's an IPC, use the pipe handler.
515 if (IS_IPC(conn)) {
516 if (lp_nt_pipe_support()) {
517 do_ntcreate_pipe_open(conn, req);
518 goto out;
520 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
521 goto out;
524 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
525 if (oplock_request) {
526 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
527 ? BATCH_OPLOCK : 0;
530 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
531 case_state = set_posix_case_semantics(ctx, conn);
532 if (!case_state) {
533 reply_nterror(req, NT_STATUS_NO_MEMORY);
534 goto out;
538 ucf_flags = filename_create_ucf_flags(req, create_disposition);
539 status = filename_convert(ctx,
540 conn,
541 fname,
542 ucf_flags,
543 NULL,
544 &smb_fname);
546 TALLOC_FREE(case_state);
548 if (!NT_STATUS_IS_OK(status)) {
549 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
550 reply_botherror(req,
551 NT_STATUS_PATH_NOT_COVERED,
552 ERRSRV, ERRbadpath);
553 goto out;
555 reply_nterror(req, status);
556 goto out;
560 * Bug #6898 - clients using Windows opens should
561 * never be able to set this attribute into the
562 * VFS.
564 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
566 status = SMB_VFS_CREATE_FILE(
567 conn, /* conn */
568 req, /* req */
569 root_dir_fid, /* root_dir_fid */
570 smb_fname, /* fname */
571 access_mask, /* access_mask */
572 share_access, /* share_access */
573 create_disposition, /* create_disposition*/
574 create_options, /* create_options */
575 file_attributes, /* file_attributes */
576 oplock_request, /* oplock_request */
577 NULL, /* lease */
578 allocation_size, /* allocation_size */
579 0, /* private_flags */
580 NULL, /* sd */
581 NULL, /* ea_list */
582 &fsp, /* result */
583 &info, /* pinfo */
584 NULL, NULL); /* create context */
586 if (!NT_STATUS_IS_OK(status)) {
587 if (open_was_deferred(req->xconn, req->mid)) {
588 /* We have re-scheduled this call, no error. */
589 goto out;
591 reply_openerror(req, status);
592 goto out;
595 /* Ensure we're pointing at the correct stat struct. */
596 TALLOC_FREE(smb_fname);
597 smb_fname = fsp->fsp_name;
600 * If the caller set the extended oplock request bit
601 * and we granted one (by whatever means) - set the
602 * correct bit for extended oplock reply.
605 if (oplock_request &&
606 (lp_fake_oplocks(SNUM(conn))
607 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
610 * Exclusive oplock granted
613 if (flags & REQUEST_BATCH_OPLOCK) {
614 oplock_granted = BATCH_OPLOCK_RETURN;
615 } else {
616 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
618 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
619 oplock_granted = LEVEL_II_OPLOCK_RETURN;
620 } else {
621 oplock_granted = NO_OPLOCK_RETURN;
624 file_len = smb_fname->st.st_ex_size;
626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
627 /* This is very strange. We
628 * return 50 words, but only set
629 * the wcnt to 42 ? It's definitely
630 * what happens on the wire....
632 reply_outbuf(req, 50, 0);
633 SCVAL(req->outbuf,smb_wct,42);
634 } else {
635 reply_outbuf(req, 34, 0);
638 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
639 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
641 p = (char *)req->outbuf + smb_vwv2;
643 SCVAL(p, 0, oplock_granted);
645 p++;
646 SSVAL(p,0,fsp->fnum);
647 p += 2;
648 if ((create_disposition == FILE_SUPERSEDE)
649 && (info == FILE_WAS_OVERWRITTEN)) {
650 SIVAL(p,0,FILE_WAS_SUPERSEDED);
651 } else {
652 SIVAL(p,0,info);
654 p += 4;
656 fattr = dos_mode(conn, smb_fname);
657 if (fattr == 0) {
658 fattr = FILE_ATTRIBUTE_NORMAL;
661 /* Create time. */
662 create_timespec = get_create_timespec(conn, fsp, smb_fname);
663 a_timespec = smb_fname->st.st_ex_atime;
664 m_timespec = smb_fname->st.st_ex_mtime;
665 c_timespec = get_change_timespec(conn, fsp, smb_fname);
667 if (lp_dos_filetime_resolution(SNUM(conn))) {
668 dos_filetime_timespec(&create_timespec);
669 dos_filetime_timespec(&a_timespec);
670 dos_filetime_timespec(&m_timespec);
671 dos_filetime_timespec(&c_timespec);
674 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
675 p += 8;
676 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
677 p += 8;
678 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
679 p += 8;
680 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
681 p += 8;
682 SIVAL(p,0,fattr); /* File Attributes. */
683 p += 4;
684 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
685 p += 8;
686 SOFF_T(p,0,file_len);
687 p += 8;
688 if (flags & EXTENDED_RESPONSE_REQUIRED) {
689 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
690 unsigned int num_streams = 0;
691 struct stream_struct *streams = NULL;
693 if (lp_ea_support(SNUM(conn))) {
694 size_t num_names = 0;
695 /* Do we have any EA's ? */
696 status = get_ea_names_from_file(
697 ctx, conn, fsp, smb_fname, NULL, &num_names);
698 if (NT_STATUS_IS_OK(status) && num_names) {
699 file_status &= ~NO_EAS;
703 status = vfs_streaminfo(conn, NULL, smb_fname, ctx,
704 &num_streams, &streams);
705 /* There is always one stream, ::$DATA. */
706 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
707 file_status &= ~NO_SUBSTREAMS;
709 TALLOC_FREE(streams);
710 SSVAL(p,2,file_status);
712 p += 4;
713 SCVAL(p,0,fsp->is_directory ? 1 : 0);
715 if (flags & EXTENDED_RESPONSE_REQUIRED) {
716 uint32_t perms = 0;
717 p += 25;
718 if (fsp->is_directory ||
719 fsp->can_write ||
720 can_write_to_file(conn, smb_fname)) {
721 perms = FILE_GENERIC_ALL;
722 } else {
723 perms = FILE_GENERIC_READ|FILE_EXECUTE;
725 SIVAL(p,0,perms);
728 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
729 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
731 out:
732 END_PROFILE(SMBntcreateX);
733 return;
736 /****************************************************************************
737 Reply to a NT_TRANSACT_CREATE call to open a pipe.
738 ****************************************************************************/
740 static void do_nt_transact_create_pipe(connection_struct *conn,
741 struct smb_request *req,
742 uint16_t **ppsetup, uint32_t setup_count,
743 char **ppparams, uint32_t parameter_count,
744 char **ppdata, uint32_t data_count)
746 char *fname = NULL;
747 char *params = *ppparams;
748 uint16_t pnum = FNUM_FIELD_INVALID;
749 char *p = NULL;
750 NTSTATUS status;
751 size_t param_len;
752 uint32_t flags;
753 TALLOC_CTX *ctx = talloc_tos();
756 * Ensure minimum number of parameters sent.
759 if(parameter_count < 54) {
760 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
761 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
762 return;
765 flags = IVAL(params,0);
767 if (req->posix_pathnames) {
768 srvstr_get_path_posix(ctx,
769 params,
770 req->flags2,
771 &fname,
772 params+53,
773 parameter_count-53,
774 STR_TERMINATE,
775 &status);
776 } else {
777 srvstr_get_path(ctx,
778 params,
779 req->flags2,
780 &fname,
781 params+53,
782 parameter_count-53,
783 STR_TERMINATE,
784 &status);
786 if (!NT_STATUS_IS_OK(status)) {
787 reply_nterror(req, status);
788 return;
791 nt_open_pipe(fname, conn, req, &pnum);
793 if (req->outbuf) {
794 /* Error return */
795 return;
798 /* Realloc the size of parameters and data we will return */
799 if (flags & EXTENDED_RESPONSE_REQUIRED) {
800 /* Extended response is 32 more byyes. */
801 param_len = 101;
802 } else {
803 param_len = 69;
805 params = nttrans_realloc(ppparams, param_len);
806 if(params == NULL) {
807 reply_nterror(req, NT_STATUS_NO_MEMORY);
808 return;
811 p = params;
812 SCVAL(p,0,NO_OPLOCK_RETURN);
814 p += 2;
815 SSVAL(p,0,pnum);
816 p += 2;
817 SIVAL(p,0,FILE_WAS_OPENED);
818 p += 8;
820 p += 32;
821 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
822 p += 20;
823 /* File type. */
824 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
825 /* Device state. */
826 SSVAL(p,2, 0x5FF); /* ? */
827 p += 4;
829 if (flags & EXTENDED_RESPONSE_REQUIRED) {
830 p += 25;
831 SIVAL(p,0,FILE_GENERIC_ALL);
833 * For pipes W2K3 seems to return
834 * 0x12019B next.
835 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
837 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
840 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
842 /* Send the required number of replies */
843 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
845 return;
848 /*********************************************************************
849 Windows seems to do canonicalization of inheritance bits. Do the
850 same.
851 *********************************************************************/
853 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
855 bool set_auto_inherited = false;
858 * We need to filter out the
859 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
860 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
861 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
862 * when an ACE is inherited. Otherwise we zero these bits out.
863 * See:
865 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
867 * for details.
870 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
871 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
872 set_auto_inherited = true;
875 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
876 if (set_auto_inherited) {
877 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
881 /****************************************************************************
882 Internal fn to set security descriptors.
883 ****************************************************************************/
885 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
886 uint32_t security_info_sent)
888 NTSTATUS status;
890 if (!CAN_WRITE(fsp->conn)) {
891 return NT_STATUS_ACCESS_DENIED;
894 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
895 return NT_STATUS_OK;
898 if (S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
899 DEBUG(10, ("ACL set on symlink %s denied.\n",
900 fsp_str_dbg(fsp)));
901 return NT_STATUS_ACCESS_DENIED;
904 if (psd->owner_sid == NULL) {
905 security_info_sent &= ~SECINFO_OWNER;
907 if (psd->group_sid == NULL) {
908 security_info_sent &= ~SECINFO_GROUP;
911 /* Ensure we have at least one thing set. */
912 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
913 /* Just like W2K3 */
914 return NT_STATUS_OK;
917 /* Ensure we have the rights to do this. */
918 if (security_info_sent & SECINFO_OWNER) {
919 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
920 return NT_STATUS_ACCESS_DENIED;
924 if (security_info_sent & SECINFO_GROUP) {
925 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
926 return NT_STATUS_ACCESS_DENIED;
930 if (security_info_sent & SECINFO_DACL) {
931 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
932 return NT_STATUS_ACCESS_DENIED;
934 /* Convert all the generic bits. */
935 if (psd->dacl) {
936 security_acl_map_generic(psd->dacl, &file_generic_mapping);
940 if (security_info_sent & SECINFO_SACL) {
941 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
942 return NT_STATUS_ACCESS_DENIED;
944 /* Convert all the generic bits. */
945 if (psd->sacl) {
946 security_acl_map_generic(psd->sacl, &file_generic_mapping);
950 canonicalize_inheritance_bits(psd);
952 if (DEBUGLEVEL >= 10) {
953 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
954 NDR_PRINT_DEBUG(security_descriptor, psd);
957 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
959 TALLOC_FREE(psd);
961 return status;
964 /****************************************************************************
965 Internal fn to set security descriptors from a data blob.
966 ****************************************************************************/
968 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
969 uint32_t security_info_sent)
971 struct security_descriptor *psd = NULL;
972 NTSTATUS status;
974 if (sd_len == 0) {
975 return NT_STATUS_INVALID_PARAMETER;
978 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
980 if (!NT_STATUS_IS_OK(status)) {
981 return status;
984 return set_sd(fsp, psd, security_info_sent);
987 /****************************************************************************
988 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
989 ****************************************************************************/
991 static void call_nt_transact_create(connection_struct *conn,
992 struct smb_request *req,
993 uint16_t **ppsetup, uint32_t setup_count,
994 char **ppparams, uint32_t parameter_count,
995 char **ppdata, uint32_t data_count,
996 uint32_t max_data_count)
998 struct smb_filename *smb_fname = NULL;
999 char *fname = NULL;
1000 char *params = *ppparams;
1001 char *data = *ppdata;
1002 /* Breakout the oplock request bits so we can set the reply bits separately. */
1003 uint32_t fattr=0;
1004 off_t file_len = 0;
1005 int info = 0;
1006 files_struct *fsp = NULL;
1007 char *p = NULL;
1008 uint32_t flags;
1009 uint32_t access_mask;
1010 uint32_t file_attributes;
1011 uint32_t share_access;
1012 uint32_t create_disposition;
1013 uint32_t create_options;
1014 uint32_t sd_len;
1015 struct security_descriptor *sd = NULL;
1016 uint32_t ea_len;
1017 uint16_t root_dir_fid;
1018 struct timespec create_timespec;
1019 struct timespec c_timespec;
1020 struct timespec a_timespec;
1021 struct timespec m_timespec;
1022 struct ea_list *ea_list = NULL;
1023 NTSTATUS status;
1024 size_t param_len;
1025 uint64_t allocation_size;
1026 int oplock_request;
1027 uint8_t oplock_granted;
1028 struct case_semantics_state *case_state = NULL;
1029 uint32_t ucf_flags;
1030 TALLOC_CTX *ctx = talloc_tos();
1032 DEBUG(5,("call_nt_transact_create\n"));
1035 * If it's an IPC, use the pipe handler.
1038 if (IS_IPC(conn)) {
1039 if (lp_nt_pipe_support()) {
1040 do_nt_transact_create_pipe(
1041 conn, req,
1042 ppsetup, setup_count,
1043 ppparams, parameter_count,
1044 ppdata, data_count);
1045 goto out;
1047 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1048 goto out;
1052 * Ensure minimum number of parameters sent.
1055 if(parameter_count < 54) {
1056 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1057 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1058 goto out;
1061 flags = IVAL(params,0);
1062 access_mask = IVAL(params,8);
1063 file_attributes = IVAL(params,20);
1064 share_access = IVAL(params,24);
1065 create_disposition = IVAL(params,28);
1066 create_options = IVAL(params,32);
1067 sd_len = IVAL(params,36);
1068 ea_len = IVAL(params,40);
1069 root_dir_fid = (uint16_t)IVAL(params,4);
1070 allocation_size = BVAL(params,12);
1073 * we need to remove ignored bits when they come directly from the client
1074 * because we reuse some of them for internal stuff
1076 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1078 if (req->posix_pathnames) {
1079 srvstr_get_path_posix(ctx,
1080 params,
1081 req->flags2,
1082 &fname,
1083 params+53,
1084 parameter_count-53,
1085 STR_TERMINATE,
1086 &status);
1087 } else {
1088 srvstr_get_path(ctx,
1089 params,
1090 req->flags2,
1091 &fname,
1092 params+53,
1093 parameter_count-53,
1094 STR_TERMINATE,
1095 &status);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 reply_nterror(req, status);
1099 goto out;
1102 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1103 case_state = set_posix_case_semantics(ctx, conn);
1104 if (!case_state) {
1105 reply_nterror(req, NT_STATUS_NO_MEMORY);
1106 goto out;
1110 ucf_flags = filename_create_ucf_flags(req, create_disposition);
1111 status = filename_convert(ctx,
1112 conn,
1113 fname,
1114 ucf_flags,
1115 NULL,
1116 &smb_fname);
1118 TALLOC_FREE(case_state);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1122 reply_botherror(req,
1123 NT_STATUS_PATH_NOT_COVERED,
1124 ERRSRV, ERRbadpath);
1125 goto out;
1127 reply_nterror(req, status);
1128 goto out;
1131 /* Ensure the data_len is correct for the sd and ea values given. */
1132 if ((ea_len + sd_len > data_count)
1133 || (ea_len > data_count) || (sd_len > data_count)
1134 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1135 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1136 "%u, data_count = %u\n", (unsigned int)ea_len,
1137 (unsigned int)sd_len, (unsigned int)data_count));
1138 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1139 goto out;
1142 if (sd_len) {
1143 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1144 sd_len));
1146 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1147 &sd);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 DEBUG(10, ("call_nt_transact_create: "
1150 "unmarshall_sec_desc failed: %s\n",
1151 nt_errstr(status)));
1152 reply_nterror(req, status);
1153 goto out;
1157 if (ea_len) {
1158 if (!lp_ea_support(SNUM(conn))) {
1159 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1160 "EA's not supported.\n",
1161 (unsigned int)ea_len));
1162 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1163 goto out;
1166 if (ea_len < 10) {
1167 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1168 "too small (should be more than 10)\n",
1169 (unsigned int)ea_len ));
1170 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1171 goto out;
1174 /* We have already checked that ea_len <= data_count here. */
1175 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1176 ea_len);
1177 if (ea_list == NULL) {
1178 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1179 goto out;
1182 if (!req->posix_pathnames &&
1183 ea_list_has_invalid_name(ea_list)) {
1184 /* Realloc the size of parameters and data we will return */
1185 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1186 /* Extended response is 32 more byyes. */
1187 param_len = 101;
1188 } else {
1189 param_len = 69;
1191 params = nttrans_realloc(ppparams, param_len);
1192 if(params == NULL) {
1193 reply_nterror(req, NT_STATUS_NO_MEMORY);
1194 goto out;
1197 memset(params, '\0', param_len);
1198 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1199 params, param_len, NULL, 0);
1200 goto out;
1204 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1205 if (oplock_request) {
1206 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1207 ? BATCH_OPLOCK : 0;
1211 * Bug #6898 - clients using Windows opens should
1212 * never be able to set this attribute into the
1213 * VFS.
1215 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1217 status = SMB_VFS_CREATE_FILE(
1218 conn, /* conn */
1219 req, /* req */
1220 root_dir_fid, /* root_dir_fid */
1221 smb_fname, /* fname */
1222 access_mask, /* access_mask */
1223 share_access, /* share_access */
1224 create_disposition, /* create_disposition*/
1225 create_options, /* create_options */
1226 file_attributes, /* file_attributes */
1227 oplock_request, /* oplock_request */
1228 NULL, /* lease */
1229 allocation_size, /* allocation_size */
1230 0, /* private_flags */
1231 sd, /* sd */
1232 ea_list, /* ea_list */
1233 &fsp, /* result */
1234 &info, /* pinfo */
1235 NULL, NULL); /* create context */
1237 if(!NT_STATUS_IS_OK(status)) {
1238 if (open_was_deferred(req->xconn, req->mid)) {
1239 /* We have re-scheduled this call, no error. */
1240 return;
1242 reply_openerror(req, status);
1243 goto out;
1246 /* Ensure we're pointing at the correct stat struct. */
1247 TALLOC_FREE(smb_fname);
1248 smb_fname = fsp->fsp_name;
1251 * If the caller set the extended oplock request bit
1252 * and we granted one (by whatever means) - set the
1253 * correct bit for extended oplock reply.
1256 if (oplock_request &&
1257 (lp_fake_oplocks(SNUM(conn))
1258 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1261 * Exclusive oplock granted
1264 if (flags & REQUEST_BATCH_OPLOCK) {
1265 oplock_granted = BATCH_OPLOCK_RETURN;
1266 } else {
1267 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1269 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1270 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1271 } else {
1272 oplock_granted = NO_OPLOCK_RETURN;
1275 file_len = smb_fname->st.st_ex_size;
1277 /* Realloc the size of parameters and data we will return */
1278 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1279 /* Extended response is 32 more byyes. */
1280 param_len = 101;
1281 } else {
1282 param_len = 69;
1284 params = nttrans_realloc(ppparams, param_len);
1285 if(params == NULL) {
1286 reply_nterror(req, NT_STATUS_NO_MEMORY);
1287 goto out;
1290 p = params;
1291 SCVAL(p, 0, oplock_granted);
1293 p += 2;
1294 SSVAL(p,0,fsp->fnum);
1295 p += 2;
1296 if ((create_disposition == FILE_SUPERSEDE)
1297 && (info == FILE_WAS_OVERWRITTEN)) {
1298 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1299 } else {
1300 SIVAL(p,0,info);
1302 p += 8;
1304 fattr = dos_mode(conn, smb_fname);
1305 if (fattr == 0) {
1306 fattr = FILE_ATTRIBUTE_NORMAL;
1309 /* Create time. */
1310 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1311 a_timespec = smb_fname->st.st_ex_atime;
1312 m_timespec = smb_fname->st.st_ex_mtime;
1313 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1315 if (lp_dos_filetime_resolution(SNUM(conn))) {
1316 dos_filetime_timespec(&create_timespec);
1317 dos_filetime_timespec(&a_timespec);
1318 dos_filetime_timespec(&m_timespec);
1319 dos_filetime_timespec(&c_timespec);
1322 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1323 p += 8;
1324 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1325 p += 8;
1326 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1327 p += 8;
1328 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1329 p += 8;
1330 SIVAL(p,0,fattr); /* File Attributes. */
1331 p += 4;
1332 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1333 p += 8;
1334 SOFF_T(p,0,file_len);
1335 p += 8;
1336 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1337 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1338 unsigned int num_streams = 0;
1339 struct stream_struct *streams = NULL;
1341 if (lp_ea_support(SNUM(conn))) {
1342 size_t num_names = 0;
1343 /* Do we have any EA's ? */
1344 status = get_ea_names_from_file(
1345 ctx, conn, fsp, smb_fname, NULL, &num_names);
1346 if (NT_STATUS_IS_OK(status) && num_names) {
1347 file_status &= ~NO_EAS;
1351 status = vfs_streaminfo(conn, NULL, smb_fname, ctx,
1352 &num_streams, &streams);
1353 /* There is always one stream, ::$DATA. */
1354 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1355 file_status &= ~NO_SUBSTREAMS;
1357 TALLOC_FREE(streams);
1358 SSVAL(p,2,file_status);
1360 p += 4;
1361 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1363 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1364 uint32_t perms = 0;
1365 p += 25;
1366 if (fsp->is_directory ||
1367 fsp->can_write ||
1368 can_write_to_file(conn, smb_fname)) {
1369 perms = FILE_GENERIC_ALL;
1370 } else {
1371 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1373 SIVAL(p,0,perms);
1376 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1377 smb_fname_str_dbg(smb_fname)));
1379 /* Send the required number of replies */
1380 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1381 out:
1382 return;
1385 /****************************************************************************
1386 Reply to a NT CANCEL request.
1387 conn POINTER CAN BE NULL HERE !
1388 ****************************************************************************/
1390 void reply_ntcancel(struct smb_request *req)
1392 struct smbXsrv_connection *xconn = req->xconn;
1393 struct smbd_server_connection *sconn = req->sconn;
1396 * Go through and cancel any pending change notifies.
1399 START_PROFILE(SMBntcancel);
1400 srv_cancel_sign_response(xconn);
1401 remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1402 remove_pending_lock_requests_by_mid_smb1(sconn, req->mid);
1404 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1405 (unsigned long long)req->mid));
1407 END_PROFILE(SMBntcancel);
1408 return;
1411 /****************************************************************************
1412 Copy a file.
1413 ****************************************************************************/
1415 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1416 connection_struct *conn,
1417 struct smb_request *req,
1418 struct smb_filename *smb_fname_src,
1419 struct smb_filename *smb_fname_dst,
1420 uint32_t attrs)
1422 files_struct *fsp1,*fsp2;
1423 uint32_t fattr;
1424 int info;
1425 off_t ret=-1;
1426 NTSTATUS status = NT_STATUS_OK;
1427 char *parent;
1429 if (!CAN_WRITE(conn)) {
1430 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1431 goto out;
1434 /* Source must already exist. */
1435 if (!VALID_STAT(smb_fname_src->st)) {
1436 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1437 goto out;
1440 /* Ensure attributes match. */
1441 fattr = dos_mode(conn, smb_fname_src);
1442 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1443 status = NT_STATUS_NO_SUCH_FILE;
1444 goto out;
1447 /* Disallow if dst file already exists. */
1448 if (VALID_STAT(smb_fname_dst->st)) {
1449 status = NT_STATUS_OBJECT_NAME_COLLISION;
1450 goto out;
1453 /* No links from a directory. */
1454 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1455 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1456 goto out;
1459 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1460 smb_fname_str_dbg(smb_fname_src),
1461 smb_fname_str_dbg(smb_fname_dst)));
1463 status = SMB_VFS_CREATE_FILE(
1464 conn, /* conn */
1465 req, /* req */
1466 0, /* root_dir_fid */
1467 smb_fname_src, /* fname */
1468 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1469 FILE_READ_EA, /* access_mask */
1470 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1471 FILE_SHARE_DELETE),
1472 FILE_OPEN, /* create_disposition*/
1473 0, /* create_options */
1474 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1475 NO_OPLOCK, /* oplock_request */
1476 NULL, /* lease */
1477 0, /* allocation_size */
1478 0, /* private_flags */
1479 NULL, /* sd */
1480 NULL, /* ea_list */
1481 &fsp1, /* result */
1482 &info, /* pinfo */
1483 NULL, NULL); /* create context */
1485 if (!NT_STATUS_IS_OK(status)) {
1486 goto out;
1489 status = SMB_VFS_CREATE_FILE(
1490 conn, /* conn */
1491 req, /* req */
1492 0, /* root_dir_fid */
1493 smb_fname_dst, /* fname */
1494 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1495 FILE_WRITE_EA, /* access_mask */
1496 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1497 FILE_SHARE_DELETE),
1498 FILE_CREATE, /* create_disposition*/
1499 0, /* create_options */
1500 fattr, /* file_attributes */
1501 NO_OPLOCK, /* oplock_request */
1502 NULL, /* lease */
1503 0, /* allocation_size */
1504 0, /* private_flags */
1505 NULL, /* sd */
1506 NULL, /* ea_list */
1507 &fsp2, /* result */
1508 &info, /* pinfo */
1509 NULL, NULL); /* create context */
1511 if (!NT_STATUS_IS_OK(status)) {
1512 close_file(NULL, fsp1, ERROR_CLOSE);
1513 goto out;
1516 if (smb_fname_src->st.st_ex_size) {
1517 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1521 * As we are opening fsp1 read-only we only expect
1522 * an error on close on fsp2 if we are out of space.
1523 * Thus we don't look at the error return from the
1524 * close of fsp1.
1526 close_file(NULL, fsp1, NORMAL_CLOSE);
1528 /* Ensure the modtime is set correctly on the destination file. */
1529 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1531 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1533 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1534 creates the file. This isn't the correct thing to do in the copy
1535 case. JRA */
1536 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1537 NULL)) {
1538 status = NT_STATUS_NO_MEMORY;
1539 goto out;
1541 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1542 TALLOC_FREE(parent);
1544 if (ret < (off_t)smb_fname_src->st.st_ex_size) {
1545 status = NT_STATUS_DISK_FULL;
1546 goto out;
1548 out:
1549 if (!NT_STATUS_IS_OK(status)) {
1550 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1551 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1552 smb_fname_str_dbg(smb_fname_dst)));
1555 return status;
1558 /****************************************************************************
1559 Reply to a NT rename request.
1560 ****************************************************************************/
1562 void reply_ntrename(struct smb_request *req)
1564 connection_struct *conn = req->conn;
1565 struct smb_filename *smb_fname_old = NULL;
1566 struct smb_filename *smb_fname_new = NULL;
1567 char *oldname = NULL;
1568 char *newname = NULL;
1569 const char *p;
1570 NTSTATUS status;
1571 bool src_has_wcard = False;
1572 bool dest_has_wcard = False;
1573 uint32_t attrs;
1574 uint32_t ucf_flags_src = ucf_flags_from_smb_request(req);
1575 uint32_t ucf_flags_dst = ucf_flags_from_smb_request(req);
1576 uint16_t rename_type;
1577 TALLOC_CTX *ctx = talloc_tos();
1578 bool stream_rename = false;
1580 START_PROFILE(SMBntrename);
1582 if (req->wct < 4) {
1583 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1584 goto out;
1587 attrs = SVAL(req->vwv+0, 0);
1588 rename_type = SVAL(req->vwv+1, 0);
1590 p = (const char *)req->buf + 1;
1591 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1592 &status, &src_has_wcard);
1593 if (!NT_STATUS_IS_OK(status)) {
1594 reply_nterror(req, status);
1595 goto out;
1598 if (!req->posix_pathnames && ms_has_wild(oldname)) {
1599 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1600 goto out;
1603 p++;
1604 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1605 &status, &dest_has_wcard);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 reply_nterror(req, status);
1608 goto out;
1611 if (!req->posix_pathnames) {
1612 /* The newname must begin with a ':' if the
1613 oldname contains a ':'. */
1614 if (strchr_m(oldname, ':')) {
1615 if (newname[0] != ':') {
1616 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1617 goto out;
1619 stream_rename = true;
1624 * If this is a rename operation, allow wildcards and save the
1625 * destination's last component.
1627 if (rename_type == RENAME_FLAG_RENAME) {
1628 ucf_flags_src |= UCF_COND_ALLOW_WCARD_LCOMP;
1629 ucf_flags_dst |= UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1632 /* rename_internals() calls unix_convert(), so don't call it here. */
1633 status = filename_convert(ctx, conn,
1634 oldname,
1635 ucf_flags_src,
1636 NULL,
1637 &smb_fname_old);
1638 if (!NT_STATUS_IS_OK(status)) {
1639 if (NT_STATUS_EQUAL(status,
1640 NT_STATUS_PATH_NOT_COVERED)) {
1641 reply_botherror(req,
1642 NT_STATUS_PATH_NOT_COVERED,
1643 ERRSRV, ERRbadpath);
1644 goto out;
1646 reply_nterror(req, status);
1647 goto out;
1650 status = filename_convert(ctx, conn,
1651 newname,
1652 ucf_flags_dst,
1653 &dest_has_wcard,
1654 &smb_fname_new);
1655 if (!NT_STATUS_IS_OK(status)) {
1656 if (NT_STATUS_EQUAL(status,
1657 NT_STATUS_PATH_NOT_COVERED)) {
1658 reply_botherror(req,
1659 NT_STATUS_PATH_NOT_COVERED,
1660 ERRSRV, ERRbadpath);
1661 goto out;
1663 reply_nterror(req, status);
1664 goto out;
1667 if (stream_rename) {
1668 /* smb_fname_new must be the same as smb_fname_old. */
1669 TALLOC_FREE(smb_fname_new->base_name);
1670 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1671 smb_fname_old->base_name);
1672 if (!smb_fname_new->base_name) {
1673 reply_nterror(req, NT_STATUS_NO_MEMORY);
1674 goto out;
1678 DEBUG(3,("reply_ntrename: %s -> %s\n",
1679 smb_fname_str_dbg(smb_fname_old),
1680 smb_fname_str_dbg(smb_fname_new)));
1682 switch(rename_type) {
1683 case RENAME_FLAG_RENAME:
1684 status = rename_internals(ctx, conn, req,
1685 smb_fname_old, smb_fname_new,
1686 attrs, False, src_has_wcard,
1687 dest_has_wcard,
1688 DELETE_ACCESS);
1689 break;
1690 case RENAME_FLAG_HARD_LINK:
1691 if (src_has_wcard || dest_has_wcard) {
1692 /* No wildcards. */
1693 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1694 } else {
1695 status = hardlink_internals(ctx, conn,
1696 req,
1697 false,
1698 smb_fname_old,
1699 smb_fname_new);
1701 break;
1702 case RENAME_FLAG_COPY:
1703 if (src_has_wcard || dest_has_wcard) {
1704 /* No wildcards. */
1705 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1706 } else {
1707 status = copy_internals(ctx, conn, req,
1708 smb_fname_old,
1709 smb_fname_new,
1710 attrs);
1712 break;
1713 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1714 status = NT_STATUS_INVALID_PARAMETER;
1715 break;
1716 default:
1717 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1718 break;
1721 if (!NT_STATUS_IS_OK(status)) {
1722 if (open_was_deferred(req->xconn, req->mid)) {
1723 /* We have re-scheduled this call. */
1724 goto out;
1727 reply_nterror(req, status);
1728 goto out;
1731 reply_outbuf(req, 0, 0);
1732 out:
1733 END_PROFILE(SMBntrename);
1734 return;
1737 /****************************************************************************
1738 Reply to a notify change - queue the request and
1739 don't allow a directory to be opened.
1740 ****************************************************************************/
1742 static void smbd_smb1_notify_reply(struct smb_request *req,
1743 NTSTATUS error_code,
1744 uint8_t *buf, size_t len)
1746 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1749 static void call_nt_transact_notify_change(connection_struct *conn,
1750 struct smb_request *req,
1751 uint16_t **ppsetup,
1752 uint32_t setup_count,
1753 char **ppparams,
1754 uint32_t parameter_count,
1755 char **ppdata, uint32_t data_count,
1756 uint32_t max_data_count,
1757 uint32_t max_param_count)
1759 uint16_t *setup = *ppsetup;
1760 files_struct *fsp;
1761 uint32_t filter;
1762 NTSTATUS status;
1763 bool recursive;
1765 if(setup_count < 6) {
1766 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1767 return;
1770 fsp = file_fsp(req, SVAL(setup,4));
1771 filter = IVAL(setup, 0);
1772 recursive = (SVAL(setup, 6) != 0) ? True : False;
1774 DEBUG(3,("call_nt_transact_notify_change\n"));
1776 if(!fsp) {
1777 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1778 return;
1782 char *filter_string;
1784 if (!(filter_string = notify_filter_string(NULL, filter))) {
1785 reply_nterror(req,NT_STATUS_NO_MEMORY);
1786 return;
1789 DEBUG(3,("call_nt_transact_notify_change: notify change "
1790 "called on %s, filter = %s, recursive = %d\n",
1791 fsp_str_dbg(fsp), filter_string, recursive));
1793 TALLOC_FREE(filter_string);
1796 if((!fsp->is_directory) || (conn != fsp->conn)) {
1797 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1798 return;
1801 if (fsp->notify == NULL) {
1803 status = change_notify_create(fsp, filter, recursive);
1805 if (!NT_STATUS_IS_OK(status)) {
1806 DEBUG(10, ("change_notify_create returned %s\n",
1807 nt_errstr(status)));
1808 reply_nterror(req, status);
1809 return;
1813 if (change_notify_fsp_has_changes(fsp)) {
1816 * We've got changes pending, respond immediately
1820 * TODO: write a torture test to check the filtering behaviour
1821 * here.
1824 change_notify_reply(req,
1825 NT_STATUS_OK,
1826 max_param_count,
1827 fsp->notify,
1828 smbd_smb1_notify_reply);
1831 * change_notify_reply() above has independently sent its
1832 * results
1834 return;
1838 * No changes pending, queue the request
1841 status = change_notify_add_request(req,
1842 max_param_count,
1843 filter,
1844 recursive, fsp,
1845 smbd_smb1_notify_reply);
1846 if (!NT_STATUS_IS_OK(status)) {
1847 reply_nterror(req, status);
1849 return;
1852 /****************************************************************************
1853 Reply to an NT transact rename command.
1854 ****************************************************************************/
1856 static void call_nt_transact_rename(connection_struct *conn,
1857 struct smb_request *req,
1858 uint16_t **ppsetup, uint32_t setup_count,
1859 char **ppparams, uint32_t parameter_count,
1860 char **ppdata, uint32_t data_count,
1861 uint32_t max_data_count)
1863 char *params = *ppparams;
1864 char *new_name = NULL;
1865 files_struct *fsp = NULL;
1866 bool dest_has_wcard = False;
1867 NTSTATUS status;
1868 TALLOC_CTX *ctx = talloc_tos();
1870 if(parameter_count < 5) {
1871 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1872 return;
1875 fsp = file_fsp(req, SVAL(params, 0));
1876 if (!check_fsp(conn, req, fsp)) {
1877 return;
1879 if (req->posix_pathnames) {
1880 srvstr_get_path_wcard_posix(ctx,
1881 params,
1882 req->flags2,
1883 &new_name,
1884 params+4,
1885 parameter_count - 4,
1886 STR_TERMINATE,
1887 &status,
1888 &dest_has_wcard);
1889 } else {
1890 srvstr_get_path_wcard(ctx,
1891 params,
1892 req->flags2,
1893 &new_name,
1894 params+4,
1895 parameter_count - 4,
1896 STR_TERMINATE,
1897 &status,
1898 &dest_has_wcard);
1901 if (!NT_STATUS_IS_OK(status)) {
1902 reply_nterror(req, status);
1903 return;
1907 * W2K3 ignores this request as the RAW-RENAME test
1908 * demonstrates, so we do.
1910 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1912 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1913 fsp_str_dbg(fsp), new_name));
1915 return;
1918 /******************************************************************************
1919 Fake up a completely empty SD.
1920 *******************************************************************************/
1922 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1924 size_t sd_size;
1926 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1927 if(!*ppsd) {
1928 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1929 return NT_STATUS_NO_MEMORY;
1932 return NT_STATUS_OK;
1935 /****************************************************************************
1936 Reply to query a security descriptor.
1937 Callable from SMB1 and SMB2.
1938 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1939 the required size.
1940 ****************************************************************************/
1942 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1943 TALLOC_CTX *mem_ctx,
1944 files_struct *fsp,
1945 uint32_t security_info_wanted,
1946 uint32_t max_data_count,
1947 uint8_t **ppmarshalled_sd,
1948 size_t *psd_size)
1950 NTSTATUS status;
1951 struct security_descriptor *psd = NULL;
1952 TALLOC_CTX *frame = talloc_stackframe();
1955 * Get the permissions to return.
1958 if ((security_info_wanted & SECINFO_SACL) &&
1959 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1960 DEBUG(10, ("Access to SACL denied.\n"));
1961 TALLOC_FREE(frame);
1962 return NT_STATUS_ACCESS_DENIED;
1965 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1966 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1967 DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
1968 TALLOC_FREE(frame);
1969 return NT_STATUS_ACCESS_DENIED;
1972 if (S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
1973 DEBUG(10, ("ACL get on symlink %s denied.\n",
1974 fsp_str_dbg(fsp)));
1975 TALLOC_FREE(frame);
1976 return NT_STATUS_ACCESS_DENIED;
1979 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1980 SECINFO_GROUP|SECINFO_SACL)) {
1981 /* Don't return SECINFO_LABEL if anything else was
1982 requested. See bug #8458. */
1983 security_info_wanted &= ~SECINFO_LABEL;
1986 if (!lp_nt_acl_support(SNUM(conn))) {
1987 status = get_null_nt_acl(frame, &psd);
1988 } else if (security_info_wanted & SECINFO_LABEL) {
1989 /* Like W2K3 return a null object. */
1990 status = get_null_nt_acl(frame, &psd);
1991 } else {
1992 status = SMB_VFS_FGET_NT_ACL(
1993 fsp, security_info_wanted, frame, &psd);
1995 if (!NT_STATUS_IS_OK(status)) {
1996 TALLOC_FREE(frame);
1997 return status;
2000 if (!(security_info_wanted & SECINFO_OWNER)) {
2001 psd->owner_sid = NULL;
2003 if (!(security_info_wanted & SECINFO_GROUP)) {
2004 psd->group_sid = NULL;
2006 if (!(security_info_wanted & SECINFO_DACL)) {
2007 psd->type &= ~SEC_DESC_DACL_PRESENT;
2008 psd->dacl = NULL;
2010 if (!(security_info_wanted & SECINFO_SACL)) {
2011 psd->type &= ~SEC_DESC_SACL_PRESENT;
2012 psd->sacl = NULL;
2015 /* If the SACL/DACL is NULL, but was requested, we mark that it is
2016 * present in the reply to match Windows behavior */
2017 if (psd->sacl == NULL &&
2018 security_info_wanted & SECINFO_SACL)
2019 psd->type |= SEC_DESC_SACL_PRESENT;
2020 if (psd->dacl == NULL &&
2021 security_info_wanted & SECINFO_DACL)
2022 psd->type |= SEC_DESC_DACL_PRESENT;
2024 if (security_info_wanted & SECINFO_LABEL) {
2025 /* Like W2K3 return a null object. */
2026 psd->owner_sid = NULL;
2027 psd->group_sid = NULL;
2028 psd->dacl = NULL;
2029 psd->sacl = NULL;
2030 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
2033 *psd_size = ndr_size_security_descriptor(psd, 0);
2035 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
2036 (unsigned long)*psd_size));
2038 if (DEBUGLEVEL >= 10) {
2039 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
2040 fsp_str_dbg(fsp)));
2041 NDR_PRINT_DEBUG(security_descriptor, psd);
2044 if (max_data_count < *psd_size) {
2045 TALLOC_FREE(frame);
2046 return NT_STATUS_BUFFER_TOO_SMALL;
2049 status = marshall_sec_desc(mem_ctx, psd,
2050 ppmarshalled_sd, psd_size);
2052 if (!NT_STATUS_IS_OK(status)) {
2053 TALLOC_FREE(frame);
2054 return status;
2057 TALLOC_FREE(frame);
2058 return NT_STATUS_OK;
2061 /****************************************************************************
2062 SMB1 reply to query a security descriptor.
2063 ****************************************************************************/
2065 static void call_nt_transact_query_security_desc(connection_struct *conn,
2066 struct smb_request *req,
2067 uint16_t **ppsetup,
2068 uint32_t setup_count,
2069 char **ppparams,
2070 uint32_t parameter_count,
2071 char **ppdata,
2072 uint32_t data_count,
2073 uint32_t max_data_count)
2075 char *params = *ppparams;
2076 char *data = *ppdata;
2077 size_t sd_size = 0;
2078 uint32_t security_info_wanted;
2079 files_struct *fsp = NULL;
2080 NTSTATUS status;
2081 uint8_t *marshalled_sd = NULL;
2083 if(parameter_count < 8) {
2084 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2085 return;
2088 fsp = file_fsp(req, SVAL(params,0));
2089 if(!fsp) {
2090 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2091 return;
2094 security_info_wanted = IVAL(params,4);
2096 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2097 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2098 (unsigned int)security_info_wanted));
2100 params = nttrans_realloc(ppparams, 4);
2101 if(params == NULL) {
2102 reply_nterror(req, NT_STATUS_NO_MEMORY);
2103 return;
2107 * Get the permissions to return.
2110 status = smbd_do_query_security_desc(conn,
2111 talloc_tos(),
2112 fsp,
2113 security_info_wanted &
2114 SMB_SUPPORTED_SECINFO_FLAGS,
2115 max_data_count,
2116 &marshalled_sd,
2117 &sd_size);
2119 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2120 SIVAL(params,0,(uint32_t)sd_size);
2121 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2122 params, 4, NULL, 0);
2123 return;
2126 if (!NT_STATUS_IS_OK(status)) {
2127 reply_nterror(req, status);
2128 return;
2131 SMB_ASSERT(sd_size > 0);
2133 SIVAL(params,0,(uint32_t)sd_size);
2135 if (max_data_count < sd_size) {
2136 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2137 params, 4, NULL, 0);
2138 return;
2142 * Allocate the data we will return.
2145 data = nttrans_realloc(ppdata, sd_size);
2146 if(data == NULL) {
2147 reply_nterror(req, NT_STATUS_NO_MEMORY);
2148 return;
2151 memcpy(data, marshalled_sd, sd_size);
2153 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2155 return;
2158 /****************************************************************************
2159 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2160 ****************************************************************************/
2162 static void call_nt_transact_set_security_desc(connection_struct *conn,
2163 struct smb_request *req,
2164 uint16_t **ppsetup,
2165 uint32_t setup_count,
2166 char **ppparams,
2167 uint32_t parameter_count,
2168 char **ppdata,
2169 uint32_t data_count,
2170 uint32_t max_data_count)
2172 char *params= *ppparams;
2173 char *data = *ppdata;
2174 files_struct *fsp = NULL;
2175 uint32_t security_info_sent = 0;
2176 NTSTATUS status;
2178 if(parameter_count < 8) {
2179 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2180 return;
2183 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2184 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2185 return;
2188 if (!CAN_WRITE(fsp->conn)) {
2189 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2190 return;
2193 if(!lp_nt_acl_support(SNUM(conn))) {
2194 goto done;
2197 security_info_sent = IVAL(params,4);
2199 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2200 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2202 if (data_count == 0) {
2203 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2204 return;
2207 status = set_sd_blob(fsp, (uint8_t *)data, data_count,
2208 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
2209 if (!NT_STATUS_IS_OK(status)) {
2210 reply_nterror(req, status);
2211 return;
2214 done:
2215 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2216 return;
2219 /****************************************************************************
2220 Reply to NT IOCTL
2221 ****************************************************************************/
2223 static void call_nt_transact_ioctl(connection_struct *conn,
2224 struct smb_request *req,
2225 uint16_t **ppsetup, uint32_t setup_count,
2226 char **ppparams, uint32_t parameter_count,
2227 char **ppdata, uint32_t data_count,
2228 uint32_t max_data_count)
2230 NTSTATUS status;
2231 uint32_t function;
2232 uint16_t fidnum;
2233 files_struct *fsp;
2234 uint8_t isFSctl;
2235 uint8_t compfilter;
2236 char *out_data = NULL;
2237 uint32_t out_data_len = 0;
2238 char *pdata = *ppdata;
2239 TALLOC_CTX *ctx = talloc_tos();
2241 if (setup_count != 8) {
2242 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2243 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2244 return;
2247 function = IVAL(*ppsetup, 0);
2248 fidnum = SVAL(*ppsetup, 4);
2249 isFSctl = CVAL(*ppsetup, 6);
2250 compfilter = CVAL(*ppsetup, 7);
2252 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2253 function, fidnum, isFSctl, compfilter));
2255 fsp=file_fsp(req, fidnum);
2258 * We don't really implement IOCTLs, especially on files.
2260 if (!isFSctl) {
2261 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2262 isFSctl));
2263 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2264 return;
2267 /* Has to be for an open file! */
2268 if (!check_fsp_open(conn, req, fsp)) {
2269 return;
2272 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2275 * out_data might be allocated by the VFS module, but talloc should be
2276 * used, and should be cleaned up when the request ends.
2278 status = SMB_VFS_FSCTL(fsp,
2279 ctx,
2280 function,
2281 req->flags2,
2282 (uint8_t *)pdata,
2283 data_count,
2284 (uint8_t **)&out_data,
2285 max_data_count,
2286 &out_data_len);
2287 if (!NT_STATUS_IS_OK(status)) {
2288 reply_nterror(req, status);
2289 } else {
2290 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2295 #ifdef HAVE_SYS_QUOTAS
2296 /****************************************************************************
2297 Reply to get user quota
2298 ****************************************************************************/
2300 static void call_nt_transact_get_user_quota(connection_struct *conn,
2301 struct smb_request *req,
2302 uint16_t **ppsetup,
2303 uint32_t setup_count,
2304 char **ppparams,
2305 uint32_t parameter_count,
2306 char **ppdata,
2307 uint32_t data_count,
2308 uint32_t max_data_count)
2310 NTSTATUS nt_status = NT_STATUS_OK;
2311 char *params = *ppparams;
2312 char *pdata = *ppdata;
2313 char *entry;
2314 int data_len=0,param_len=0;
2315 int qt_len=0;
2316 int entry_len = 0;
2317 files_struct *fsp = NULL;
2318 uint16_t level = 0;
2319 size_t sid_len;
2320 struct dom_sid sid;
2321 bool start_enum = True;
2322 SMB_NTQUOTA_STRUCT qt;
2323 SMB_NTQUOTA_LIST *tmp_list;
2324 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2326 ZERO_STRUCT(qt);
2328 /* access check */
2329 if (get_current_uid(conn) != sec_initial_uid()) {
2330 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2331 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2332 conn->session_info->unix_info->unix_name));
2333 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2334 return;
2338 * Ensure minimum number of parameters sent.
2341 if (parameter_count < 4) {
2342 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2343 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2344 return;
2347 /* maybe we can check the quota_fnum */
2348 fsp = file_fsp(req, SVAL(params,0));
2349 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2350 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2351 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2352 return;
2355 /* the NULL pointer checking for fsp->fake_file_handle->pd
2356 * is done by CHECK_NTQUOTA_HANDLE_OK()
2358 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2360 level = SVAL(params,2);
2362 /* unknown 12 bytes leading in params */
2364 switch (level) {
2365 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2366 /* seems that we should continue with the enum here --metze */
2368 if (qt_handle->quota_list!=NULL &&
2369 qt_handle->tmp_list==NULL) {
2371 /* free the list */
2372 free_ntquota_list(&(qt_handle->quota_list));
2374 /* Realloc the size of parameters and data we will return */
2375 param_len = 4;
2376 params = nttrans_realloc(ppparams, param_len);
2377 if(params == NULL) {
2378 reply_nterror(req, NT_STATUS_NO_MEMORY);
2379 return;
2382 data_len = 0;
2383 SIVAL(params,0,data_len);
2385 break;
2388 start_enum = False;
2390 FALL_THROUGH;
2391 case TRANSACT_GET_USER_QUOTA_LIST_START:
2393 if (qt_handle->quota_list==NULL &&
2394 qt_handle->tmp_list==NULL) {
2395 start_enum = True;
2398 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2399 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2400 return;
2403 /* Realloc the size of parameters and data we will return */
2404 param_len = 4;
2405 params = nttrans_realloc(ppparams, param_len);
2406 if(params == NULL) {
2407 reply_nterror(req, NT_STATUS_NO_MEMORY);
2408 return;
2411 /* we should not trust the value in max_data_count*/
2412 max_data_count = MIN(max_data_count,2048);
2414 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2415 if(pdata == NULL) {
2416 reply_nterror(req, NT_STATUS_NO_MEMORY);
2417 return;
2420 entry = pdata;
2422 /* set params Size of returned Quota Data 4 bytes*/
2423 /* but set it later when we know it */
2425 /* for each entry push the data */
2427 if (start_enum) {
2428 qt_handle->tmp_list = qt_handle->quota_list;
2431 tmp_list = qt_handle->tmp_list;
2433 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2434 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2436 sid_len = ndr_size_dom_sid(
2437 &tmp_list->quotas->sid, 0);
2438 entry_len = 40 + sid_len;
2440 /* nextoffset entry 4 bytes */
2441 SIVAL(entry,0,entry_len);
2443 /* then the len of the SID 4 bytes */
2444 SIVAL(entry,4,sid_len);
2446 /* unknown data 8 bytes uint64_t */
2447 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2449 /* the used disk space 8 bytes uint64_t */
2450 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2452 /* the soft quotas 8 bytes uint64_t */
2453 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2455 /* the hard quotas 8 bytes uint64_t */
2456 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2458 /* and now the SID */
2459 sid_linearize((uint8_t *)(entry+40), sid_len,
2460 &tmp_list->quotas->sid);
2463 qt_handle->tmp_list = tmp_list;
2465 /* overwrite the offset of the last entry */
2466 SIVAL(entry-entry_len,0,0);
2468 data_len = 4+qt_len;
2469 /* overwrite the params quota_data_len */
2470 SIVAL(params,0,data_len);
2472 break;
2474 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2476 /* unknown 4 bytes IVAL(pdata,0) */
2478 if (data_count < 8) {
2479 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2480 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2481 return;
2484 sid_len = IVAL(pdata,4);
2485 /* Ensure this is less than 1mb. */
2486 if (sid_len > (1024*1024)) {
2487 reply_nterror(req, NT_STATUS_NO_MEMORY);
2488 return;
2491 if (data_count < 8+sid_len) {
2492 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2493 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2494 return;
2497 data_len = 4+40+sid_len;
2499 if (max_data_count < data_len) {
2500 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2501 max_data_count, data_len));
2502 param_len = 4;
2503 SIVAL(params,0,data_len);
2504 data_len = 0;
2505 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2506 break;
2509 if (!sid_parse((const uint8_t *)(pdata+8), sid_len,
2510 &sid)) {
2511 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2512 return;
2515 nt_status = vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE,
2516 &sid, &qt);
2517 if (!NT_STATUS_IS_OK(nt_status)) {
2518 reply_nterror(req, nt_status);
2519 return;
2522 /* Realloc the size of parameters and data we will return */
2523 param_len = 4;
2524 params = nttrans_realloc(ppparams, param_len);
2525 if(params == NULL) {
2526 reply_nterror(req, NT_STATUS_NO_MEMORY);
2527 return;
2530 pdata = nttrans_realloc(ppdata, data_len);
2531 if(pdata == NULL) {
2532 reply_nterror(req, NT_STATUS_NO_MEMORY);
2533 return;
2536 entry = pdata;
2538 /* set params Size of returned Quota Data 4 bytes*/
2539 SIVAL(params,0,data_len);
2541 /* nextoffset entry 4 bytes */
2542 SIVAL(entry,0,0);
2544 /* then the len of the SID 4 bytes */
2545 SIVAL(entry,4,sid_len);
2547 /* unknown data 8 bytes uint64_t */
2548 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2550 /* the used disk space 8 bytes uint64_t */
2551 SBIG_UINT(entry,16,qt.usedspace);
2553 /* the soft quotas 8 bytes uint64_t */
2554 SBIG_UINT(entry,24,qt.softlim);
2556 /* the hard quotas 8 bytes uint64_t */
2557 SBIG_UINT(entry,32,qt.hardlim);
2559 /* and now the SID */
2560 sid_linearize((uint8_t *)(entry+40), sid_len, &sid);
2562 break;
2564 default:
2565 DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
2566 "level 0x%04hX\n",
2567 fsp_fnum_dbg(fsp), level));
2568 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2569 return;
2570 break;
2573 send_nt_replies(conn, req, nt_status, params, param_len,
2574 pdata, data_len);
2577 /****************************************************************************
2578 Reply to set user quota
2579 ****************************************************************************/
2581 static void call_nt_transact_set_user_quota(connection_struct *conn,
2582 struct smb_request *req,
2583 uint16_t **ppsetup,
2584 uint32_t setup_count,
2585 char **ppparams,
2586 uint32_t parameter_count,
2587 char **ppdata,
2588 uint32_t data_count,
2589 uint32_t max_data_count)
2591 char *params = *ppparams;
2592 char *pdata = *ppdata;
2593 int data_len=0,param_len=0;
2594 SMB_NTQUOTA_STRUCT qt;
2595 size_t sid_len;
2596 struct dom_sid sid;
2597 files_struct *fsp = NULL;
2599 ZERO_STRUCT(qt);
2601 /* access check */
2602 if (get_current_uid(conn) != 0) {
2603 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2604 "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
2605 conn->session_info->unix_info->unix_name));
2606 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2607 return;
2611 * Ensure minimum number of parameters sent.
2614 if (parameter_count < 2) {
2615 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2616 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2617 return;
2620 /* maybe we can check the quota_fnum */
2621 fsp = file_fsp(req, SVAL(params,0));
2622 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2623 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2624 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2625 return;
2628 if (data_count < 40) {
2629 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2630 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2631 return;
2634 /* offset to next quota record.
2635 * 4 bytes IVAL(pdata,0)
2636 * unused here...
2639 /* sid len */
2640 sid_len = IVAL(pdata,4);
2642 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2643 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2644 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2645 return;
2648 /* unknown 8 bytes in pdata
2649 * maybe its the change time in NTTIME
2652 /* the used space 8 bytes (uint64_t)*/
2653 qt.usedspace = BVAL(pdata,16);
2655 /* the soft quotas 8 bytes (uint64_t)*/
2656 qt.softlim = BVAL(pdata,24);
2658 /* the hard quotas 8 bytes (uint64_t)*/
2659 qt.hardlim = BVAL(pdata,32);
2661 if (!sid_parse((const uint8_t *)(pdata+40), sid_len, &sid)) {
2662 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2663 return;
2666 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2668 /* 44 unknown bytes left... */
2670 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2671 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2672 return;
2675 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2676 pdata, data_len);
2678 #endif /* HAVE_SYS_QUOTAS */
2680 static void handle_nttrans(connection_struct *conn,
2681 struct trans_state *state,
2682 struct smb_request *req)
2684 if (get_Protocol() >= PROTOCOL_NT1) {
2685 req->flags2 |= 0x40; /* IS_LONG_NAME */
2686 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2690 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2692 /* Now we must call the relevant NT_TRANS function */
2693 switch(state->call) {
2694 case NT_TRANSACT_CREATE:
2696 START_PROFILE(NT_transact_create);
2697 call_nt_transact_create(
2698 conn, req,
2699 &state->setup, state->setup_count,
2700 &state->param, state->total_param,
2701 &state->data, state->total_data,
2702 state->max_data_return);
2703 END_PROFILE(NT_transact_create);
2704 break;
2707 case NT_TRANSACT_IOCTL:
2709 START_PROFILE(NT_transact_ioctl);
2710 call_nt_transact_ioctl(
2711 conn, req,
2712 &state->setup, state->setup_count,
2713 &state->param, state->total_param,
2714 &state->data, state->total_data,
2715 state->max_data_return);
2716 END_PROFILE(NT_transact_ioctl);
2717 break;
2720 case NT_TRANSACT_SET_SECURITY_DESC:
2722 START_PROFILE(NT_transact_set_security_desc);
2723 call_nt_transact_set_security_desc(
2724 conn, req,
2725 &state->setup, state->setup_count,
2726 &state->param, state->total_param,
2727 &state->data, state->total_data,
2728 state->max_data_return);
2729 END_PROFILE(NT_transact_set_security_desc);
2730 break;
2733 case NT_TRANSACT_NOTIFY_CHANGE:
2735 START_PROFILE(NT_transact_notify_change);
2736 call_nt_transact_notify_change(
2737 conn, req,
2738 &state->setup, state->setup_count,
2739 &state->param, state->total_param,
2740 &state->data, state->total_data,
2741 state->max_data_return,
2742 state->max_param_return);
2743 END_PROFILE(NT_transact_notify_change);
2744 break;
2747 case NT_TRANSACT_RENAME:
2749 START_PROFILE(NT_transact_rename);
2750 call_nt_transact_rename(
2751 conn, req,
2752 &state->setup, state->setup_count,
2753 &state->param, state->total_param,
2754 &state->data, state->total_data,
2755 state->max_data_return);
2756 END_PROFILE(NT_transact_rename);
2757 break;
2760 case NT_TRANSACT_QUERY_SECURITY_DESC:
2762 START_PROFILE(NT_transact_query_security_desc);
2763 call_nt_transact_query_security_desc(
2764 conn, req,
2765 &state->setup, state->setup_count,
2766 &state->param, state->total_param,
2767 &state->data, state->total_data,
2768 state->max_data_return);
2769 END_PROFILE(NT_transact_query_security_desc);
2770 break;
2773 #ifdef HAVE_SYS_QUOTAS
2774 case NT_TRANSACT_GET_USER_QUOTA:
2776 START_PROFILE(NT_transact_get_user_quota);
2777 call_nt_transact_get_user_quota(
2778 conn, req,
2779 &state->setup, state->setup_count,
2780 &state->param, state->total_param,
2781 &state->data, state->total_data,
2782 state->max_data_return);
2783 END_PROFILE(NT_transact_get_user_quota);
2784 break;
2787 case NT_TRANSACT_SET_USER_QUOTA:
2789 START_PROFILE(NT_transact_set_user_quota);
2790 call_nt_transact_set_user_quota(
2791 conn, req,
2792 &state->setup, state->setup_count,
2793 &state->param, state->total_param,
2794 &state->data, state->total_data,
2795 state->max_data_return);
2796 END_PROFILE(NT_transact_set_user_quota);
2797 break;
2799 #endif /* HAVE_SYS_QUOTAS */
2801 default:
2802 /* Error in request */
2803 DEBUG(0,("handle_nttrans: Unknown request %d in "
2804 "nttrans call\n", state->call));
2805 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2806 return;
2808 return;
2811 /****************************************************************************
2812 Reply to a SMBNTtrans.
2813 ****************************************************************************/
2815 void reply_nttrans(struct smb_request *req)
2817 connection_struct *conn = req->conn;
2818 uint32_t pscnt;
2819 uint32_t psoff;
2820 uint32_t dscnt;
2821 uint32_t dsoff;
2822 uint16_t function_code;
2823 NTSTATUS result;
2824 struct trans_state *state;
2826 START_PROFILE(SMBnttrans);
2828 if (req->wct < 19) {
2829 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2830 END_PROFILE(SMBnttrans);
2831 return;
2834 pscnt = IVAL(req->vwv+9, 1);
2835 psoff = IVAL(req->vwv+11, 1);
2836 dscnt = IVAL(req->vwv+13, 1);
2837 dsoff = IVAL(req->vwv+15, 1);
2838 function_code = SVAL(req->vwv+18, 0);
2840 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2841 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2842 END_PROFILE(SMBnttrans);
2843 return;
2846 result = allow_new_trans(conn->pending_trans, req->mid);
2847 if (!NT_STATUS_IS_OK(result)) {
2848 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2849 reply_nterror(req, result);
2850 END_PROFILE(SMBnttrans);
2851 return;
2854 if ((state = talloc(conn, struct trans_state)) == NULL) {
2855 reply_nterror(req, NT_STATUS_NO_MEMORY);
2856 END_PROFILE(SMBnttrans);
2857 return;
2860 state->cmd = SMBnttrans;
2862 state->mid = req->mid;
2863 state->vuid = req->vuid;
2864 state->total_data = IVAL(req->vwv+3, 1);
2865 state->data = NULL;
2866 state->total_param = IVAL(req->vwv+1, 1);
2867 state->param = NULL;
2868 state->max_data_return = IVAL(req->vwv+7, 1);
2869 state->max_param_return = IVAL(req->vwv+5, 1);
2871 /* setup count is in *words* */
2872 state->setup_count = 2*CVAL(req->vwv+17, 1);
2873 state->setup = NULL;
2874 state->call = function_code;
2876 DEBUG(10, ("num_setup=%u, "
2877 "param_total=%u, this_param=%u, max_param=%u, "
2878 "data_total=%u, this_data=%u, max_data=%u, "
2879 "param_offset=%u, data_offset=%u\n",
2880 (unsigned)state->setup_count,
2881 (unsigned)state->total_param, (unsigned)pscnt,
2882 (unsigned)state->max_param_return,
2883 (unsigned)state->total_data, (unsigned)dscnt,
2884 (unsigned)state->max_data_return,
2885 (unsigned)psoff, (unsigned)dsoff));
2888 * All nttrans messages we handle have smb_wct == 19 +
2889 * state->setup_count. Ensure this is so as a sanity check.
2892 if(req->wct != 19 + (state->setup_count/2)) {
2893 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2894 req->wct, 19 + (state->setup_count/2)));
2895 goto bad_param;
2898 /* Don't allow more than 128mb for each value. */
2899 if ((state->total_data > (1024*1024*128)) ||
2900 (state->total_param > (1024*1024*128))) {
2901 reply_nterror(req, NT_STATUS_NO_MEMORY);
2902 END_PROFILE(SMBnttrans);
2903 return;
2906 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2907 goto bad_param;
2909 if (state->total_data) {
2911 if (trans_oob(state->total_data, 0, dscnt)
2912 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2913 goto bad_param;
2916 /* Can't use talloc here, the core routines do realloc on the
2917 * params and data. */
2918 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2919 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2920 "bytes !\n", (unsigned int)state->total_data));
2921 TALLOC_FREE(state);
2922 reply_nterror(req, NT_STATUS_NO_MEMORY);
2923 END_PROFILE(SMBnttrans);
2924 return;
2927 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2930 if (state->total_param) {
2932 if (trans_oob(state->total_param, 0, pscnt)
2933 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2934 goto bad_param;
2937 /* Can't use talloc here, the core routines do realloc on the
2938 * params and data. */
2939 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2940 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2941 "bytes !\n", (unsigned int)state->total_param));
2942 SAFE_FREE(state->data);
2943 TALLOC_FREE(state);
2944 reply_nterror(req, NT_STATUS_NO_MEMORY);
2945 END_PROFILE(SMBnttrans);
2946 return;
2949 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2952 state->received_data = dscnt;
2953 state->received_param = pscnt;
2955 if(state->setup_count > 0) {
2956 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2957 state->setup_count));
2960 * No overflow possible here, state->setup_count is an
2961 * unsigned int, being filled by a single byte from
2962 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2963 * below is not necessary, it's here to clarify things. The
2964 * validity of req->vwv and req->wct has been checked in
2965 * init_smb_request already.
2967 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2968 goto bad_param;
2971 state->setup = (uint16_t *)TALLOC(state, state->setup_count);
2972 if (state->setup == NULL) {
2973 DEBUG(0,("reply_nttrans : Out of memory\n"));
2974 SAFE_FREE(state->data);
2975 SAFE_FREE(state->param);
2976 TALLOC_FREE(state);
2977 reply_nterror(req, NT_STATUS_NO_MEMORY);
2978 END_PROFILE(SMBnttrans);
2979 return;
2982 memcpy(state->setup, req->vwv+19, state->setup_count);
2983 dump_data(10, (uint8_t *)state->setup, state->setup_count);
2986 if ((state->received_data == state->total_data) &&
2987 (state->received_param == state->total_param)) {
2988 handle_nttrans(conn, state, req);
2989 SAFE_FREE(state->param);
2990 SAFE_FREE(state->data);
2991 TALLOC_FREE(state);
2992 END_PROFILE(SMBnttrans);
2993 return;
2996 DLIST_ADD(conn->pending_trans, state);
2998 /* We need to send an interim response then receive the rest
2999 of the parameter/data bytes */
3000 reply_outbuf(req, 0, 0);
3001 show_msg((char *)req->outbuf);
3002 END_PROFILE(SMBnttrans);
3003 return;
3005 bad_param:
3007 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3008 SAFE_FREE(state->data);
3009 SAFE_FREE(state->param);
3010 TALLOC_FREE(state);
3011 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3012 END_PROFILE(SMBnttrans);
3013 return;
3016 /****************************************************************************
3017 Reply to a SMBnttranss
3018 ****************************************************************************/
3020 void reply_nttranss(struct smb_request *req)
3022 connection_struct *conn = req->conn;
3023 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3024 struct trans_state *state;
3026 START_PROFILE(SMBnttranss);
3028 show_msg((const char *)req->inbuf);
3030 /* Windows clients expect all replies to
3031 an NT transact secondary (SMBnttranss 0xA1)
3032 to have a command code of NT transact
3033 (SMBnttrans 0xA0). See bug #8989 for details. */
3034 req->cmd = SMBnttrans;
3036 if (req->wct < 18) {
3037 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3038 END_PROFILE(SMBnttranss);
3039 return;
3042 for (state = conn->pending_trans; state != NULL;
3043 state = state->next) {
3044 if (state->mid == req->mid) {
3045 break;
3049 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3050 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3051 END_PROFILE(SMBnttranss);
3052 return;
3055 /* Revise state->total_param and state->total_data in case they have
3056 changed downwards */
3057 if (IVAL(req->vwv+1, 1) < state->total_param) {
3058 state->total_param = IVAL(req->vwv+1, 1);
3060 if (IVAL(req->vwv+3, 1) < state->total_data) {
3061 state->total_data = IVAL(req->vwv+3, 1);
3064 pcnt = IVAL(req->vwv+5, 1);
3065 poff = IVAL(req->vwv+7, 1);
3066 pdisp = IVAL(req->vwv+9, 1);
3068 dcnt = IVAL(req->vwv+11, 1);
3069 doff = IVAL(req->vwv+13, 1);
3070 ddisp = IVAL(req->vwv+15, 1);
3072 state->received_param += pcnt;
3073 state->received_data += dcnt;
3075 if ((state->received_data > state->total_data) ||
3076 (state->received_param > state->total_param))
3077 goto bad_param;
3079 if (pcnt) {
3080 if (trans_oob(state->total_param, pdisp, pcnt)
3081 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3082 goto bad_param;
3084 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3087 if (dcnt) {
3088 if (trans_oob(state->total_data, ddisp, dcnt)
3089 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3090 goto bad_param;
3092 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3095 if ((state->received_param < state->total_param) ||
3096 (state->received_data < state->total_data)) {
3097 END_PROFILE(SMBnttranss);
3098 return;
3101 handle_nttrans(conn, state, req);
3103 DLIST_REMOVE(conn->pending_trans, state);
3104 SAFE_FREE(state->data);
3105 SAFE_FREE(state->param);
3106 TALLOC_FREE(state);
3107 END_PROFILE(SMBnttranss);
3108 return;
3110 bad_param:
3112 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3113 DLIST_REMOVE(conn->pending_trans, state);
3114 SAFE_FREE(state->data);
3115 SAFE_FREE(state->param);
3116 TALLOC_FREE(state);
3117 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3118 END_PROFILE(SMBnttranss);
3119 return;