s3:smbd:smb2: fix an assignment-instead-of-check bug conn_snum_used()
[Samba.git] / source3 / smbd / nttrans.c
blobde508eb0cac95598380540868de0001a12aeadfa
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 "ntioctl.h"
31 #include "smbprofile.h"
32 #include "libsmb/libsmb.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 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 smbd_server_connection *sconn = req->sconn;
71 int max_send = sconn->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(sconn,
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 * We might have had SMBnttranss in req->inbuf, fix that.
149 SCVAL(req->outbuf, smb_com, SMBnttrans);
152 * Set total params and data to be sent.
155 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
156 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
159 * Calculate how many parameters and data we can fit into
160 * this packet. Parameters get precedence.
163 params_sent_thistime = MIN(params_to_send,useable_space);
164 data_sent_thistime = useable_space - params_sent_thistime;
165 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
167 SIVAL(req->outbuf, smb_ntr_ParameterCount,
168 params_sent_thistime);
170 if(params_sent_thistime == 0) {
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
172 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
173 } else {
175 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
176 * parameter bytes, however the first 4 bytes of outbuf are
177 * the Netbios over TCP header. Thus use smb_base() to subtract
178 * them from the calculation.
181 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
182 ((smb_buf(req->outbuf)+alignment_offset)
183 - smb_base(req->outbuf)));
185 * Absolute displacement of param bytes sent in this packet.
188 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
189 pp - params);
193 * Deal with the data portion.
196 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
198 if(data_sent_thistime == 0) {
199 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
200 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
201 } else {
203 * The offset of the data bytes is the offset of the
204 * parameter bytes plus the number of parameters being sent this time.
207 SIVAL(req->outbuf, smb_ntr_DataOffset,
208 ((smb_buf(req->outbuf)+alignment_offset) -
209 smb_base(req->outbuf))
210 + params_sent_thistime + data_alignment_offset);
211 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
215 * Copy the param bytes into the packet.
218 if(params_sent_thistime) {
219 if (alignment_offset != 0) {
220 memset(smb_buf(req->outbuf), 0,
221 alignment_offset);
223 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
224 params_sent_thistime);
228 * Copy in the data bytes
231 if(data_sent_thistime) {
232 if (data_alignment_offset != 0) {
233 memset((smb_buf(req->outbuf)+alignment_offset+
234 params_sent_thistime), 0,
235 data_alignment_offset);
237 memcpy(smb_buf(req->outbuf)+alignment_offset
238 +params_sent_thistime+data_alignment_offset,
239 pd,data_sent_thistime);
242 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
243 params_sent_thistime, data_sent_thistime, useable_space));
244 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
245 params_to_send, data_to_send, paramsize, datasize));
247 if (NT_STATUS_V(nt_error)) {
248 error_packet_set((char *)req->outbuf,
249 0, 0, nt_error,
250 __LINE__,__FILE__);
253 /* Send the packet */
254 show_msg((char *)req->outbuf);
255 if (!srv_send_smb(sconn,
256 (char *)req->outbuf,
257 true, req->seqnum+1,
258 IS_CONN_ENCRYPTED(conn),
259 &req->pcd)) {
260 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
263 TALLOC_FREE(req->outbuf);
265 pp += params_sent_thistime;
266 pd += data_sent_thistime;
268 params_to_send -= params_sent_thistime;
269 data_to_send -= data_sent_thistime;
272 * Sanity check
275 if(params_to_send < 0 || data_to_send < 0) {
276 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
277 params_to_send, data_to_send));
278 exit_server_cleanly("send_nt_replies: internal error");
283 /****************************************************************************
284 Reply to an NT create and X call on a pipe
285 ****************************************************************************/
287 static void nt_open_pipe(char *fname, connection_struct *conn,
288 struct smb_request *req, int *ppnum)
290 files_struct *fsp;
291 NTSTATUS status;
293 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
295 /* Strip \\ off the name. */
296 fname++;
298 status = open_np_file(req, fname, &fsp);
299 if (!NT_STATUS_IS_OK(status)) {
300 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
301 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
302 ERRDOS, ERRbadpipe);
303 return;
305 reply_nterror(req, status);
306 return;
309 *ppnum = fsp->fnum;
310 return;
313 /****************************************************************************
314 Reply to an NT create and X call for pipes.
315 ****************************************************************************/
317 static void do_ntcreate_pipe_open(connection_struct *conn,
318 struct smb_request *req)
320 char *fname = NULL;
321 int pnum = -1;
322 char *p = NULL;
323 uint32 flags = IVAL(req->vwv+3, 1);
324 TALLOC_CTX *ctx = talloc_tos();
326 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
328 if (!fname) {
329 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
330 ERRDOS, ERRbadpipe);
331 return;
333 nt_open_pipe(fname, conn, req, &pnum);
335 if (req->outbuf) {
336 /* error reply */
337 return;
341 * Deal with pipe return.
344 if (flags & EXTENDED_RESPONSE_REQUIRED) {
345 /* This is very strange. We
346 * return 50 words, but only set
347 * the wcnt to 42 ? It's definately
348 * what happens on the wire....
350 reply_outbuf(req, 50, 0);
351 SCVAL(req->outbuf,smb_wct,42);
352 } else {
353 reply_outbuf(req, 34, 0);
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));
384 chain_reply(req);
387 struct case_semantics_state {
388 connection_struct *conn;
389 bool case_sensitive;
390 bool case_preserve;
391 bool short_case_preserve;
394 /****************************************************************************
395 Restore case semantics.
396 ****************************************************************************/
398 static int restore_case_semantics(struct case_semantics_state *state)
400 state->conn->case_sensitive = state->case_sensitive;
401 state->conn->case_preserve = state->case_preserve;
402 state->conn->short_case_preserve = state->short_case_preserve;
403 return 0;
406 /****************************************************************************
407 Save case semantics.
408 ****************************************************************************/
410 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
411 connection_struct *conn)
413 struct case_semantics_state *result;
415 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
416 return NULL;
419 result->conn = conn;
420 result->case_sensitive = conn->case_sensitive;
421 result->case_preserve = conn->case_preserve;
422 result->short_case_preserve = conn->short_case_preserve;
424 /* Set to POSIX. */
425 conn->case_sensitive = True;
426 conn->case_preserve = True;
427 conn->short_case_preserve = True;
429 talloc_set_destructor(result, restore_case_semantics);
431 return result;
434 /****************************************************************************
435 Reply to an NT create and X call.
436 ****************************************************************************/
438 void reply_ntcreate_and_X(struct smb_request *req)
440 connection_struct *conn = req->conn;
441 struct smb_filename *smb_fname = NULL;
442 char *fname = NULL;
443 uint32 flags;
444 uint32 access_mask;
445 uint32 file_attributes;
446 uint32 share_access;
447 uint32 create_disposition;
448 uint32 create_options;
449 uint16 root_dir_fid;
450 uint64_t allocation_size;
451 /* Breakout the oplock request bits so we can set the
452 reply bits separately. */
453 uint32 fattr=0;
454 SMB_OFF_T file_len = 0;
455 int info = 0;
456 files_struct *fsp = NULL;
457 char *p = NULL;
458 struct timespec create_timespec;
459 struct timespec c_timespec;
460 struct timespec a_timespec;
461 struct timespec m_timespec;
462 struct timespec write_time_ts;
463 NTSTATUS status;
464 int oplock_request;
465 uint8_t oplock_granted = NO_OPLOCK_RETURN;
466 struct case_semantics_state *case_state = NULL;
467 TALLOC_CTX *ctx = talloc_tos();
469 START_PROFILE(SMBntcreateX);
471 if (req->wct < 24) {
472 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
473 goto out;
476 flags = IVAL(req->vwv+3, 1);
477 access_mask = IVAL(req->vwv+7, 1);
478 file_attributes = IVAL(req->vwv+13, 1);
479 share_access = IVAL(req->vwv+15, 1);
480 create_disposition = IVAL(req->vwv+17, 1);
481 create_options = IVAL(req->vwv+19, 1);
482 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
484 allocation_size = BVAL(req->vwv+9, 1);
486 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
487 STR_TERMINATE, &status);
489 if (!NT_STATUS_IS_OK(status)) {
490 reply_nterror(req, status);
491 goto out;
494 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
495 "file_attributes = 0x%x, share_access = 0x%x, "
496 "create_disposition = 0x%x create_options = 0x%x "
497 "root_dir_fid = 0x%x, fname = %s\n",
498 (unsigned int)flags,
499 (unsigned int)access_mask,
500 (unsigned int)file_attributes,
501 (unsigned int)share_access,
502 (unsigned int)create_disposition,
503 (unsigned int)create_options,
504 (unsigned int)root_dir_fid,
505 fname));
508 * we need to remove ignored bits when they come directly from the client
509 * because we reuse some of them for internal stuff
511 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
514 * If it's an IPC, use the pipe handler.
517 if (IS_IPC(conn)) {
518 if (lp_nt_pipe_support()) {
519 do_ntcreate_pipe_open(conn, req);
520 goto out;
522 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
523 goto out;
526 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
527 if (oplock_request) {
528 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
529 ? BATCH_OPLOCK : 0;
532 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
533 case_state = set_posix_case_semantics(ctx, conn);
534 if (!case_state) {
535 reply_nterror(req, NT_STATUS_NO_MEMORY);
536 goto out;
540 status = filename_convert(ctx,
541 conn,
542 req->flags2 & FLAGS2_DFS_PATHNAMES,
543 fname,
545 NULL,
546 &smb_fname);
548 TALLOC_FREE(case_state);
550 if (!NT_STATUS_IS_OK(status)) {
551 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
552 reply_botherror(req,
553 NT_STATUS_PATH_NOT_COVERED,
554 ERRSRV, ERRbadpath);
555 goto out;
557 reply_nterror(req, status);
558 goto out;
562 * Bug #6898 - clients using Windows opens should
563 * never be able to set this attribute into the
564 * VFS.
566 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
568 status = SMB_VFS_CREATE_FILE(
569 conn, /* conn */
570 req, /* req */
571 root_dir_fid, /* root_dir_fid */
572 smb_fname, /* fname */
573 access_mask, /* access_mask */
574 share_access, /* share_access */
575 create_disposition, /* create_disposition*/
576 create_options, /* create_options */
577 file_attributes, /* file_attributes */
578 oplock_request, /* oplock_request */
579 allocation_size, /* allocation_size */
580 0, /* private_flags */
581 NULL, /* sd */
582 NULL, /* ea_list */
583 &fsp, /* result */
584 &info); /* pinfo */
586 if (!NT_STATUS_IS_OK(status)) {
587 if (open_was_deferred(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 definately
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 p = (char *)req->outbuf + smb_vwv2;
640 SCVAL(p, 0, oplock_granted);
642 p++;
643 SSVAL(p,0,fsp->fnum);
644 p += 2;
645 if ((create_disposition == FILE_SUPERSEDE)
646 && (info == FILE_WAS_OVERWRITTEN)) {
647 SIVAL(p,0,FILE_WAS_SUPERSEDED);
648 } else {
649 SIVAL(p,0,info);
651 p += 4;
653 fattr = dos_mode(conn, smb_fname);
654 if (fattr == 0) {
655 fattr = FILE_ATTRIBUTE_NORMAL;
658 /* Deal with other possible opens having a modified
659 write time. JRA. */
660 ZERO_STRUCT(write_time_ts);
661 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
662 if (!null_timespec(write_time_ts)) {
663 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
666 /* Create time. */
667 create_timespec = get_create_timespec(conn, fsp, smb_fname);
668 a_timespec = smb_fname->st.st_ex_atime;
669 m_timespec = smb_fname->st.st_ex_mtime;
670 c_timespec = get_change_timespec(conn, fsp, smb_fname);
672 if (lp_dos_filetime_resolution(SNUM(conn))) {
673 dos_filetime_timespec(&create_timespec);
674 dos_filetime_timespec(&a_timespec);
675 dos_filetime_timespec(&m_timespec);
676 dos_filetime_timespec(&c_timespec);
679 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
680 p += 8;
681 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
682 p += 8;
683 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
684 p += 8;
685 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
686 p += 8;
687 SIVAL(p,0,fattr); /* File Attributes. */
688 p += 4;
689 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
690 p += 8;
691 SOFF_T(p,0,file_len);
692 p += 8;
693 if (flags & EXTENDED_RESPONSE_REQUIRED) {
694 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
695 size_t num_names = 0;
696 unsigned int num_streams = 0;
697 struct stream_struct *streams = NULL;
699 /* Do we have any EA's ? */
700 status = get_ea_names_from_file(ctx, conn, fsp,
701 smb_fname->base_name, NULL, &num_names);
702 if (NT_STATUS_IS_OK(status) && num_names) {
703 file_status &= ~NO_EAS;
705 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
706 &num_streams, &streams);
707 /* There is always one stream, ::$DATA. */
708 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
709 file_status &= ~NO_SUBSTREAMS;
711 TALLOC_FREE(streams);
712 SSVAL(p,2,file_status);
714 p += 4;
715 SCVAL(p,0,fsp->is_directory ? 1 : 0);
717 if (flags & EXTENDED_RESPONSE_REQUIRED) {
718 uint32 perms = 0;
719 p += 25;
720 if (fsp->is_directory ||
721 can_write_to_file(conn, smb_fname)) {
722 perms = FILE_GENERIC_ALL;
723 } else {
724 perms = FILE_GENERIC_READ|FILE_EXECUTE;
726 SIVAL(p,0,perms);
729 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
730 fsp->fnum, smb_fname_str_dbg(smb_fname)));
732 chain_reply(req);
733 out:
734 END_PROFILE(SMBntcreateX);
735 return;
738 /****************************************************************************
739 Reply to a NT_TRANSACT_CREATE call to open a pipe.
740 ****************************************************************************/
742 static void do_nt_transact_create_pipe(connection_struct *conn,
743 struct smb_request *req,
744 uint16 **ppsetup, uint32 setup_count,
745 char **ppparams, uint32 parameter_count,
746 char **ppdata, uint32 data_count)
748 char *fname = NULL;
749 char *params = *ppparams;
750 int pnum = -1;
751 char *p = NULL;
752 NTSTATUS status;
753 size_t param_len;
754 uint32 flags;
755 TALLOC_CTX *ctx = talloc_tos();
758 * Ensure minimum number of parameters sent.
761 if(parameter_count < 54) {
762 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
764 return;
767 flags = IVAL(params,0);
769 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
770 parameter_count-53, STR_TERMINATE,
771 &status);
772 if (!NT_STATUS_IS_OK(status)) {
773 reply_nterror(req, status);
774 return;
777 nt_open_pipe(fname, conn, req, &pnum);
779 if (req->outbuf) {
780 /* Error return */
781 return;
784 /* Realloc the size of parameters and data we will return */
785 if (flags & EXTENDED_RESPONSE_REQUIRED) {
786 /* Extended response is 32 more byyes. */
787 param_len = 101;
788 } else {
789 param_len = 69;
791 params = nttrans_realloc(ppparams, param_len);
792 if(params == NULL) {
793 reply_nterror(req, NT_STATUS_NO_MEMORY);
794 return;
797 p = params;
798 SCVAL(p,0,NO_OPLOCK_RETURN);
800 p += 2;
801 SSVAL(p,0,pnum);
802 p += 2;
803 SIVAL(p,0,FILE_WAS_OPENED);
804 p += 8;
806 p += 32;
807 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
808 p += 20;
809 /* File type. */
810 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
811 /* Device state. */
812 SSVAL(p,2, 0x5FF); /* ? */
813 p += 4;
815 if (flags & EXTENDED_RESPONSE_REQUIRED) {
816 p += 25;
817 SIVAL(p,0,FILE_GENERIC_ALL);
819 * For pipes W2K3 seems to return
820 * 0x12019B next.
821 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
823 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
826 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
828 /* Send the required number of replies */
829 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
831 return;
834 /****************************************************************************
835 Internal fn to set security descriptors.
836 ****************************************************************************/
838 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
839 uint32_t security_info_sent)
841 struct security_descriptor *psd = NULL;
842 NTSTATUS status;
844 if (sd_len == 0) {
845 return NT_STATUS_INVALID_PARAMETER;
848 if (!CAN_WRITE(fsp->conn)) {
849 return NT_STATUS_ACCESS_DENIED;
852 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
853 return NT_STATUS_OK;
856 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
858 if (!NT_STATUS_IS_OK(status)) {
859 return status;
862 if (psd->owner_sid == NULL) {
863 security_info_sent &= ~SECINFO_OWNER;
865 if (psd->group_sid == NULL) {
866 security_info_sent &= ~SECINFO_GROUP;
869 /* Ensure we have at least one thing set. */
870 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
871 if (security_info_sent & SECINFO_LABEL) {
872 /* Only consider SECINFO_LABEL if no other
873 bits are set. Just like W2K3 we don't
874 store this. */
875 return NT_STATUS_OK;
877 return NT_STATUS_INVALID_PARAMETER;
880 /* Ensure we have the rights to do this. */
881 if (security_info_sent & SECINFO_OWNER) {
882 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
883 return NT_STATUS_ACCESS_DENIED;
887 if (security_info_sent & SECINFO_GROUP) {
888 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
889 return NT_STATUS_ACCESS_DENIED;
893 if (security_info_sent & SECINFO_DACL) {
894 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
895 return NT_STATUS_ACCESS_DENIED;
897 /* Convert all the generic bits. */
898 if (psd->dacl) {
899 security_acl_map_generic(psd->dacl, &file_generic_mapping);
903 if (security_info_sent & SECINFO_SACL) {
904 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
905 return NT_STATUS_ACCESS_DENIED;
907 /* Convert all the generic bits. */
908 if (psd->sacl) {
909 security_acl_map_generic(psd->sacl, &file_generic_mapping);
913 if (DEBUGLEVEL >= 10) {
914 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
915 NDR_PRINT_DEBUG(security_descriptor, psd);
918 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
920 TALLOC_FREE(psd);
922 return status;
925 /****************************************************************************
926 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
927 ****************************************************************************/
929 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
931 struct ea_list *ea_list_head = NULL;
932 size_t offset = 0;
934 if (data_size < 4) {
935 return NULL;
938 while (offset + 4 <= data_size) {
939 size_t next_offset = IVAL(pdata,offset);
940 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
942 if (!eal) {
943 return NULL;
946 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
947 if (next_offset == 0) {
948 break;
950 offset += next_offset;
953 return ea_list_head;
956 /****************************************************************************
957 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
958 ****************************************************************************/
960 static void call_nt_transact_create(connection_struct *conn,
961 struct smb_request *req,
962 uint16 **ppsetup, uint32 setup_count,
963 char **ppparams, uint32 parameter_count,
964 char **ppdata, uint32 data_count,
965 uint32 max_data_count)
967 struct smb_filename *smb_fname = NULL;
968 char *fname = NULL;
969 char *params = *ppparams;
970 char *data = *ppdata;
971 /* Breakout the oplock request bits so we can set the reply bits separately. */
972 uint32 fattr=0;
973 SMB_OFF_T file_len = 0;
974 int info = 0;
975 files_struct *fsp = NULL;
976 char *p = NULL;
977 uint32 flags;
978 uint32 access_mask;
979 uint32 file_attributes;
980 uint32 share_access;
981 uint32 create_disposition;
982 uint32 create_options;
983 uint32 sd_len;
984 struct security_descriptor *sd = NULL;
985 uint32 ea_len;
986 uint16 root_dir_fid;
987 struct timespec create_timespec;
988 struct timespec c_timespec;
989 struct timespec a_timespec;
990 struct timespec m_timespec;
991 struct timespec write_time_ts;
992 struct ea_list *ea_list = NULL;
993 NTSTATUS status;
994 size_t param_len;
995 uint64_t allocation_size;
996 int oplock_request;
997 uint8_t oplock_granted;
998 struct case_semantics_state *case_state = NULL;
999 TALLOC_CTX *ctx = talloc_tos();
1001 DEBUG(5,("call_nt_transact_create\n"));
1004 * If it's an IPC, use the pipe handler.
1007 if (IS_IPC(conn)) {
1008 if (lp_nt_pipe_support()) {
1009 do_nt_transact_create_pipe(
1010 conn, req,
1011 ppsetup, setup_count,
1012 ppparams, parameter_count,
1013 ppdata, data_count);
1014 goto out;
1016 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1017 goto out;
1021 * Ensure minimum number of parameters sent.
1024 if(parameter_count < 54) {
1025 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1026 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1027 goto out;
1030 flags = IVAL(params,0);
1031 access_mask = IVAL(params,8);
1032 file_attributes = IVAL(params,20);
1033 share_access = IVAL(params,24);
1034 create_disposition = IVAL(params,28);
1035 create_options = IVAL(params,32);
1036 sd_len = IVAL(params,36);
1037 ea_len = IVAL(params,40);
1038 root_dir_fid = (uint16)IVAL(params,4);
1039 allocation_size = BVAL(params,12);
1042 * we need to remove ignored bits when they come directly from the client
1043 * because we reuse some of them for internal stuff
1045 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1047 /* Ensure the data_len is correct for the sd and ea values given. */
1048 if ((ea_len + sd_len > data_count)
1049 || (ea_len > data_count) || (sd_len > data_count)
1050 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1051 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1052 "%u, data_count = %u\n", (unsigned int)ea_len,
1053 (unsigned int)sd_len, (unsigned int)data_count));
1054 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1055 goto out;
1058 if (sd_len) {
1059 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1060 sd_len));
1062 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1063 &sd);
1064 if (!NT_STATUS_IS_OK(status)) {
1065 DEBUG(10, ("call_nt_transact_create: "
1066 "unmarshall_sec_desc failed: %s\n",
1067 nt_errstr(status)));
1068 reply_nterror(req, status);
1069 goto out;
1073 if (ea_len) {
1074 if (!lp_ea_support(SNUM(conn))) {
1075 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1076 "EA's not supported.\n",
1077 (unsigned int)ea_len));
1078 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1079 goto out;
1082 if (ea_len < 10) {
1083 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1084 "too small (should be more than 10)\n",
1085 (unsigned int)ea_len ));
1086 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1087 goto out;
1090 /* We have already checked that ea_len <= data_count here. */
1091 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1092 ea_len);
1093 if (ea_list == NULL) {
1094 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1095 goto out;
1099 srvstr_get_path(ctx, params, req->flags2, &fname,
1100 params+53, parameter_count-53,
1101 STR_TERMINATE, &status);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 reply_nterror(req, status);
1104 goto out;
1107 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1108 case_state = set_posix_case_semantics(ctx, conn);
1109 if (!case_state) {
1110 reply_nterror(req, NT_STATUS_NO_MEMORY);
1111 goto out;
1115 status = filename_convert(ctx,
1116 conn,
1117 req->flags2 & FLAGS2_DFS_PATHNAMES,
1118 fname,
1120 NULL,
1121 &smb_fname);
1123 TALLOC_FREE(case_state);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1127 reply_botherror(req,
1128 NT_STATUS_PATH_NOT_COVERED,
1129 ERRSRV, ERRbadpath);
1130 goto out;
1132 reply_nterror(req, status);
1133 goto out;
1136 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1137 if (oplock_request) {
1138 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1139 ? BATCH_OPLOCK : 0;
1143 * Bug #6898 - clients using Windows opens should
1144 * never be able to set this attribute into the
1145 * VFS.
1147 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1149 status = SMB_VFS_CREATE_FILE(
1150 conn, /* conn */
1151 req, /* req */
1152 root_dir_fid, /* root_dir_fid */
1153 smb_fname, /* fname */
1154 access_mask, /* access_mask */
1155 share_access, /* share_access */
1156 create_disposition, /* create_disposition*/
1157 create_options, /* create_options */
1158 file_attributes, /* file_attributes */
1159 oplock_request, /* oplock_request */
1160 allocation_size, /* allocation_size */
1161 0, /* private_flags */
1162 sd, /* sd */
1163 ea_list, /* ea_list */
1164 &fsp, /* result */
1165 &info); /* pinfo */
1167 if(!NT_STATUS_IS_OK(status)) {
1168 if (open_was_deferred(req->mid)) {
1169 /* We have re-scheduled this call, no error. */
1170 return;
1172 reply_openerror(req, status);
1173 goto out;
1176 /* Ensure we're pointing at the correct stat struct. */
1177 TALLOC_FREE(smb_fname);
1178 smb_fname = fsp->fsp_name;
1181 * If the caller set the extended oplock request bit
1182 * and we granted one (by whatever means) - set the
1183 * correct bit for extended oplock reply.
1186 if (oplock_request &&
1187 (lp_fake_oplocks(SNUM(conn))
1188 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1191 * Exclusive oplock granted
1194 if (flags & REQUEST_BATCH_OPLOCK) {
1195 oplock_granted = BATCH_OPLOCK_RETURN;
1196 } else {
1197 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1199 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1200 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1201 } else {
1202 oplock_granted = NO_OPLOCK_RETURN;
1205 file_len = smb_fname->st.st_ex_size;
1207 /* Realloc the size of parameters and data we will return */
1208 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1209 /* Extended response is 32 more byyes. */
1210 param_len = 101;
1211 } else {
1212 param_len = 69;
1214 params = nttrans_realloc(ppparams, param_len);
1215 if(params == NULL) {
1216 reply_nterror(req, NT_STATUS_NO_MEMORY);
1217 goto out;
1220 p = params;
1221 SCVAL(p, 0, oplock_granted);
1223 p += 2;
1224 SSVAL(p,0,fsp->fnum);
1225 p += 2;
1226 if ((create_disposition == FILE_SUPERSEDE)
1227 && (info == FILE_WAS_OVERWRITTEN)) {
1228 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1229 } else {
1230 SIVAL(p,0,info);
1232 p += 8;
1234 fattr = dos_mode(conn, smb_fname);
1235 if (fattr == 0) {
1236 fattr = FILE_ATTRIBUTE_NORMAL;
1239 /* Deal with other possible opens having a modified
1240 write time. JRA. */
1241 ZERO_STRUCT(write_time_ts);
1242 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1243 if (!null_timespec(write_time_ts)) {
1244 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1247 /* Create time. */
1248 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1249 a_timespec = smb_fname->st.st_ex_atime;
1250 m_timespec = smb_fname->st.st_ex_mtime;
1251 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1253 if (lp_dos_filetime_resolution(SNUM(conn))) {
1254 dos_filetime_timespec(&create_timespec);
1255 dos_filetime_timespec(&a_timespec);
1256 dos_filetime_timespec(&m_timespec);
1257 dos_filetime_timespec(&c_timespec);
1260 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1261 p += 8;
1262 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1263 p += 8;
1264 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1265 p += 8;
1266 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1267 p += 8;
1268 SIVAL(p,0,fattr); /* File Attributes. */
1269 p += 4;
1270 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1271 p += 8;
1272 SOFF_T(p,0,file_len);
1273 p += 8;
1274 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1275 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1276 size_t num_names = 0;
1277 unsigned int num_streams = 0;
1278 struct stream_struct *streams = NULL;
1280 /* Do we have any EA's ? */
1281 status = get_ea_names_from_file(ctx, conn, fsp,
1282 smb_fname->base_name, NULL, &num_names);
1283 if (NT_STATUS_IS_OK(status) && num_names) {
1284 file_status &= ~NO_EAS;
1286 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1287 &num_streams, &streams);
1288 /* There is always one stream, ::$DATA. */
1289 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1290 file_status &= ~NO_SUBSTREAMS;
1292 TALLOC_FREE(streams);
1293 SSVAL(p,2,file_status);
1295 p += 4;
1296 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1298 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1299 uint32 perms = 0;
1300 p += 25;
1301 if (fsp->is_directory ||
1302 can_write_to_file(conn, smb_fname)) {
1303 perms = FILE_GENERIC_ALL;
1304 } else {
1305 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1307 SIVAL(p,0,perms);
1310 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1311 smb_fname_str_dbg(smb_fname)));
1313 /* Send the required number of replies */
1314 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1315 out:
1316 return;
1319 /****************************************************************************
1320 Reply to a NT CANCEL request.
1321 conn POINTER CAN BE NULL HERE !
1322 ****************************************************************************/
1324 void reply_ntcancel(struct smb_request *req)
1327 * Go through and cancel any pending change notifies.
1330 START_PROFILE(SMBntcancel);
1331 srv_cancel_sign_response(req->sconn);
1332 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1333 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1335 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1336 (unsigned long long)req->mid));
1338 END_PROFILE(SMBntcancel);
1339 return;
1342 /****************************************************************************
1343 Copy a file.
1344 ****************************************************************************/
1346 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1347 connection_struct *conn,
1348 struct smb_request *req,
1349 struct smb_filename *smb_fname_src,
1350 struct smb_filename *smb_fname_dst,
1351 uint32 attrs)
1353 files_struct *fsp1,*fsp2;
1354 uint32 fattr;
1355 int info;
1356 SMB_OFF_T ret=-1;
1357 NTSTATUS status = NT_STATUS_OK;
1358 char *parent;
1360 if (!CAN_WRITE(conn)) {
1361 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1362 goto out;
1365 /* Source must already exist. */
1366 if (!VALID_STAT(smb_fname_src->st)) {
1367 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1368 goto out;
1371 /* Ensure attributes match. */
1372 fattr = dos_mode(conn, smb_fname_src);
1373 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1374 status = NT_STATUS_NO_SUCH_FILE;
1375 goto out;
1378 /* Disallow if dst file already exists. */
1379 if (VALID_STAT(smb_fname_dst->st)) {
1380 status = NT_STATUS_OBJECT_NAME_COLLISION;
1381 goto out;
1384 /* No links from a directory. */
1385 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1386 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1387 goto out;
1390 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1391 smb_fname_str_dbg(smb_fname_src),
1392 smb_fname_str_dbg(smb_fname_dst)));
1394 status = SMB_VFS_CREATE_FILE(
1395 conn, /* conn */
1396 req, /* req */
1397 0, /* root_dir_fid */
1398 smb_fname_src, /* fname */
1399 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1400 FILE_READ_EA, /* access_mask */
1401 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1402 FILE_SHARE_DELETE),
1403 FILE_OPEN, /* create_disposition*/
1404 0, /* create_options */
1405 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1406 NO_OPLOCK, /* oplock_request */
1407 0, /* allocation_size */
1408 0, /* private_flags */
1409 NULL, /* sd */
1410 NULL, /* ea_list */
1411 &fsp1, /* result */
1412 &info); /* pinfo */
1414 if (!NT_STATUS_IS_OK(status)) {
1415 goto out;
1418 status = SMB_VFS_CREATE_FILE(
1419 conn, /* conn */
1420 req, /* req */
1421 0, /* root_dir_fid */
1422 smb_fname_dst, /* fname */
1423 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1424 FILE_WRITE_EA, /* access_mask */
1425 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1426 FILE_SHARE_DELETE),
1427 FILE_CREATE, /* create_disposition*/
1428 0, /* create_options */
1429 fattr, /* file_attributes */
1430 NO_OPLOCK, /* oplock_request */
1431 0, /* allocation_size */
1432 0, /* private_flags */
1433 NULL, /* sd */
1434 NULL, /* ea_list */
1435 &fsp2, /* result */
1436 &info); /* pinfo */
1438 if (!NT_STATUS_IS_OK(status)) {
1439 close_file(NULL, fsp1, ERROR_CLOSE);
1440 goto out;
1443 if (smb_fname_src->st.st_ex_size) {
1444 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1448 * As we are opening fsp1 read-only we only expect
1449 * an error on close on fsp2 if we are out of space.
1450 * Thus we don't look at the error return from the
1451 * close of fsp1.
1453 close_file(NULL, fsp1, NORMAL_CLOSE);
1455 /* Ensure the modtime is set correctly on the destination file. */
1456 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1458 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1460 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1461 creates the file. This isn't the correct thing to do in the copy
1462 case. JRA */
1463 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1464 NULL)) {
1465 status = NT_STATUS_NO_MEMORY;
1466 goto out;
1468 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1469 TALLOC_FREE(parent);
1471 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1472 status = NT_STATUS_DISK_FULL;
1473 goto out;
1475 out:
1476 if (!NT_STATUS_IS_OK(status)) {
1477 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1478 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1479 smb_fname_str_dbg(smb_fname_dst)));
1482 return status;
1485 /****************************************************************************
1486 Reply to a NT rename request.
1487 ****************************************************************************/
1489 void reply_ntrename(struct smb_request *req)
1491 connection_struct *conn = req->conn;
1492 struct smb_filename *smb_fname_old = NULL;
1493 struct smb_filename *smb_fname_new = NULL;
1494 char *oldname = NULL;
1495 char *newname = NULL;
1496 const char *p;
1497 NTSTATUS status;
1498 bool src_has_wcard = False;
1499 bool dest_has_wcard = False;
1500 uint32 attrs;
1501 uint32_t ucf_flags_src = 0;
1502 uint32_t ucf_flags_dst = 0;
1503 uint16 rename_type;
1504 TALLOC_CTX *ctx = talloc_tos();
1505 bool stream_rename = false;
1507 START_PROFILE(SMBntrename);
1509 if (req->wct < 4) {
1510 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1511 goto out;
1514 attrs = SVAL(req->vwv+0, 0);
1515 rename_type = SVAL(req->vwv+1, 0);
1517 p = (const char *)req->buf + 1;
1518 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1519 &status, &src_has_wcard);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 reply_nterror(req, status);
1522 goto out;
1525 if (ms_has_wild(oldname)) {
1526 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1527 goto out;
1530 p++;
1531 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1532 &status, &dest_has_wcard);
1533 if (!NT_STATUS_IS_OK(status)) {
1534 reply_nterror(req, status);
1535 goto out;
1538 if (!lp_posix_pathnames()) {
1539 /* The newname must begin with a ':' if the
1540 oldname contains a ':'. */
1541 if (strchr_m(oldname, ':')) {
1542 if (newname[0] != ':') {
1543 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1544 goto out;
1546 stream_rename = true;
1551 * If this is a rename operation, allow wildcards and save the
1552 * destination's last component.
1554 if (rename_type == RENAME_FLAG_RENAME) {
1555 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1556 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1559 /* rename_internals() calls unix_convert(), so don't call it here. */
1560 status = filename_convert(ctx, conn,
1561 req->flags2 & FLAGS2_DFS_PATHNAMES,
1562 oldname,
1563 ucf_flags_src,
1564 NULL,
1565 &smb_fname_old);
1566 if (!NT_STATUS_IS_OK(status)) {
1567 if (NT_STATUS_EQUAL(status,
1568 NT_STATUS_PATH_NOT_COVERED)) {
1569 reply_botherror(req,
1570 NT_STATUS_PATH_NOT_COVERED,
1571 ERRSRV, ERRbadpath);
1572 goto out;
1574 reply_nterror(req, status);
1575 goto out;
1578 status = filename_convert(ctx, conn,
1579 req->flags2 & FLAGS2_DFS_PATHNAMES,
1580 newname,
1581 ucf_flags_dst,
1582 &dest_has_wcard,
1583 &smb_fname_new);
1584 if (!NT_STATUS_IS_OK(status)) {
1585 if (NT_STATUS_EQUAL(status,
1586 NT_STATUS_PATH_NOT_COVERED)) {
1587 reply_botherror(req,
1588 NT_STATUS_PATH_NOT_COVERED,
1589 ERRSRV, ERRbadpath);
1590 goto out;
1592 reply_nterror(req, status);
1593 goto out;
1596 if (stream_rename) {
1597 /* smb_fname_new must be the same as smb_fname_old. */
1598 TALLOC_FREE(smb_fname_new->base_name);
1599 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1600 smb_fname_old->base_name);
1601 if (!smb_fname_new->base_name) {
1602 reply_nterror(req, NT_STATUS_NO_MEMORY);
1603 goto out;
1607 DEBUG(3,("reply_ntrename: %s -> %s\n",
1608 smb_fname_str_dbg(smb_fname_old),
1609 smb_fname_str_dbg(smb_fname_new)));
1611 switch(rename_type) {
1612 case RENAME_FLAG_RENAME:
1613 status = rename_internals(ctx, conn, req,
1614 smb_fname_old, smb_fname_new,
1615 attrs, False, src_has_wcard,
1616 dest_has_wcard,
1617 DELETE_ACCESS);
1618 break;
1619 case RENAME_FLAG_HARD_LINK:
1620 if (src_has_wcard || dest_has_wcard) {
1621 /* No wildcards. */
1622 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1623 } else {
1624 status = hardlink_internals(ctx, conn,
1625 req,
1626 false,
1627 smb_fname_old,
1628 smb_fname_new);
1630 break;
1631 case RENAME_FLAG_COPY:
1632 if (src_has_wcard || dest_has_wcard) {
1633 /* No wildcards. */
1634 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1635 } else {
1636 status = copy_internals(ctx, conn, req,
1637 smb_fname_old,
1638 smb_fname_new,
1639 attrs);
1641 break;
1642 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1643 status = NT_STATUS_INVALID_PARAMETER;
1644 break;
1645 default:
1646 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1647 break;
1650 if (!NT_STATUS_IS_OK(status)) {
1651 if (open_was_deferred(req->mid)) {
1652 /* We have re-scheduled this call. */
1653 goto out;
1656 reply_nterror(req, status);
1657 goto out;
1660 reply_outbuf(req, 0, 0);
1661 out:
1662 END_PROFILE(SMBntrename);
1663 return;
1666 /****************************************************************************
1667 Reply to a notify change - queue the request and
1668 don't allow a directory to be opened.
1669 ****************************************************************************/
1671 static void smbd_smb1_notify_reply(struct smb_request *req,
1672 NTSTATUS error_code,
1673 uint8_t *buf, size_t len)
1675 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1678 static void call_nt_transact_notify_change(connection_struct *conn,
1679 struct smb_request *req,
1680 uint16 **ppsetup,
1681 uint32 setup_count,
1682 char **ppparams,
1683 uint32 parameter_count,
1684 char **ppdata, uint32 data_count,
1685 uint32 max_data_count,
1686 uint32 max_param_count)
1688 uint16 *setup = *ppsetup;
1689 files_struct *fsp;
1690 uint32 filter;
1691 NTSTATUS status;
1692 bool recursive;
1694 if(setup_count < 6) {
1695 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1696 return;
1699 fsp = file_fsp(req, SVAL(setup,4));
1700 filter = IVAL(setup, 0);
1701 recursive = (SVAL(setup, 6) != 0) ? True : False;
1703 DEBUG(3,("call_nt_transact_notify_change\n"));
1705 if(!fsp) {
1706 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1707 return;
1711 char *filter_string;
1713 if (!(filter_string = notify_filter_string(NULL, filter))) {
1714 reply_nterror(req,NT_STATUS_NO_MEMORY);
1715 return;
1718 DEBUG(3,("call_nt_transact_notify_change: notify change "
1719 "called on %s, filter = %s, recursive = %d\n",
1720 fsp_str_dbg(fsp), filter_string, recursive));
1722 TALLOC_FREE(filter_string);
1725 if((!fsp->is_directory) || (conn != fsp->conn)) {
1726 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1727 return;
1730 if (fsp->notify == NULL) {
1732 status = change_notify_create(fsp, filter, recursive);
1734 if (!NT_STATUS_IS_OK(status)) {
1735 DEBUG(10, ("change_notify_create returned %s\n",
1736 nt_errstr(status)));
1737 reply_nterror(req, status);
1738 return;
1742 if (fsp->notify->num_changes != 0) {
1745 * We've got changes pending, respond immediately
1749 * TODO: write a torture test to check the filtering behaviour
1750 * here.
1753 change_notify_reply(req,
1754 NT_STATUS_OK,
1755 max_param_count,
1756 fsp->notify,
1757 smbd_smb1_notify_reply);
1760 * change_notify_reply() above has independently sent its
1761 * results
1763 return;
1767 * No changes pending, queue the request
1770 status = change_notify_add_request(req,
1771 max_param_count,
1772 filter,
1773 recursive, fsp,
1774 smbd_smb1_notify_reply);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 reply_nterror(req, status);
1778 return;
1781 /****************************************************************************
1782 Reply to an NT transact rename command.
1783 ****************************************************************************/
1785 static void call_nt_transact_rename(connection_struct *conn,
1786 struct smb_request *req,
1787 uint16 **ppsetup, uint32 setup_count,
1788 char **ppparams, uint32 parameter_count,
1789 char **ppdata, uint32 data_count,
1790 uint32 max_data_count)
1792 char *params = *ppparams;
1793 char *new_name = NULL;
1794 files_struct *fsp = NULL;
1795 bool dest_has_wcard = False;
1796 NTSTATUS status;
1797 TALLOC_CTX *ctx = talloc_tos();
1799 if(parameter_count < 5) {
1800 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1801 return;
1804 fsp = file_fsp(req, SVAL(params, 0));
1805 if (!check_fsp(conn, req, fsp)) {
1806 return;
1808 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1809 parameter_count - 4,
1810 STR_TERMINATE, &status, &dest_has_wcard);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 reply_nterror(req, status);
1813 return;
1817 * W2K3 ignores this request as the RAW-RENAME test
1818 * demonstrates, so we do.
1820 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1822 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1823 fsp_str_dbg(fsp), new_name));
1825 return;
1828 /******************************************************************************
1829 Fake up a completely empty SD.
1830 *******************************************************************************/
1832 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1834 size_t sd_size;
1836 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1837 if(!*ppsd) {
1838 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1839 return NT_STATUS_NO_MEMORY;
1842 return NT_STATUS_OK;
1845 /****************************************************************************
1846 Reply to query a security descriptor.
1847 Callable from SMB2 and SMB2.
1848 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1849 the required size.
1850 ****************************************************************************/
1852 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1853 TALLOC_CTX *mem_ctx,
1854 files_struct *fsp,
1855 uint32_t security_info_wanted,
1856 uint32_t max_data_count,
1857 uint8_t **ppmarshalled_sd,
1858 size_t *psd_size)
1860 NTSTATUS status;
1861 struct security_descriptor *psd = NULL;
1864 * Get the permissions to return.
1867 if ((security_info_wanted & SECINFO_SACL) &&
1868 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1869 return NT_STATUS_ACCESS_DENIED;
1872 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1873 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1874 return NT_STATUS_ACCESS_DENIED;
1877 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1878 SECINFO_GROUP|SECINFO_SACL)) {
1879 /* Don't return SECINFO_LABEL if anything else was
1880 requested. See bug #8458. */
1881 security_info_wanted &= ~SECINFO_LABEL;
1884 if (!lp_nt_acl_support(SNUM(conn))) {
1885 status = get_null_nt_acl(mem_ctx, &psd);
1886 } else if (security_info_wanted & SECINFO_LABEL) {
1887 /* Like W2K3 return a null object. */
1888 status = get_null_nt_acl(mem_ctx, &psd);
1889 } else {
1890 status = SMB_VFS_FGET_NT_ACL(
1891 fsp, security_info_wanted, &psd);
1893 if (!NT_STATUS_IS_OK(status)) {
1894 return status;
1897 if (!(security_info_wanted & SECINFO_OWNER)) {
1898 psd->owner_sid = NULL;
1900 if (!(security_info_wanted & SECINFO_GROUP)) {
1901 psd->group_sid = NULL;
1903 if (!(security_info_wanted & SECINFO_DACL)) {
1904 psd->type &= ~SEC_DESC_DACL_PRESENT;
1905 psd->dacl = NULL;
1907 if (!(security_info_wanted & SECINFO_SACL)) {
1908 psd->type &= ~SEC_DESC_SACL_PRESENT;
1909 psd->sacl = NULL;
1912 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1913 * present in the reply to match Windows behavior */
1914 if (psd->sacl == NULL &&
1915 security_info_wanted & SECINFO_SACL)
1916 psd->type |= SEC_DESC_SACL_PRESENT;
1917 if (psd->dacl == NULL &&
1918 security_info_wanted & SECINFO_DACL)
1919 psd->type |= SEC_DESC_DACL_PRESENT;
1921 if (security_info_wanted & SECINFO_LABEL) {
1922 /* Like W2K3 return a null object. */
1923 psd->owner_sid = NULL;
1924 psd->group_sid = NULL;
1925 psd->dacl = NULL;
1926 psd->sacl = NULL;
1927 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1930 *psd_size = ndr_size_security_descriptor(psd, 0);
1932 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1933 (unsigned long)*psd_size));
1935 if (DEBUGLEVEL >= 10) {
1936 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1937 fsp_str_dbg(fsp)));
1938 NDR_PRINT_DEBUG(security_descriptor, psd);
1941 if (max_data_count < *psd_size) {
1942 TALLOC_FREE(psd);
1943 return NT_STATUS_BUFFER_TOO_SMALL;
1946 status = marshall_sec_desc(mem_ctx, psd,
1947 ppmarshalled_sd, psd_size);
1949 if (!NT_STATUS_IS_OK(status)) {
1950 TALLOC_FREE(psd);
1951 return status;
1954 TALLOC_FREE(psd);
1955 return NT_STATUS_OK;
1958 /****************************************************************************
1959 SMB1 reply to query a security descriptor.
1960 ****************************************************************************/
1962 static void call_nt_transact_query_security_desc(connection_struct *conn,
1963 struct smb_request *req,
1964 uint16 **ppsetup,
1965 uint32 setup_count,
1966 char **ppparams,
1967 uint32 parameter_count,
1968 char **ppdata,
1969 uint32 data_count,
1970 uint32 max_data_count)
1972 char *params = *ppparams;
1973 char *data = *ppdata;
1974 size_t sd_size = 0;
1975 uint32 security_info_wanted;
1976 files_struct *fsp = NULL;
1977 NTSTATUS status;
1978 uint8_t *marshalled_sd = NULL;
1980 if(parameter_count < 8) {
1981 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1982 return;
1985 fsp = file_fsp(req, SVAL(params,0));
1986 if(!fsp) {
1987 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1988 return;
1991 security_info_wanted = IVAL(params,4);
1993 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1994 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1995 (unsigned int)security_info_wanted));
1997 params = nttrans_realloc(ppparams, 4);
1998 if(params == NULL) {
1999 reply_nterror(req, NT_STATUS_NO_MEMORY);
2000 return;
2004 * Get the permissions to return.
2007 status = smbd_do_query_security_desc(conn,
2008 talloc_tos(),
2009 fsp,
2010 security_info_wanted,
2011 max_data_count,
2012 &marshalled_sd,
2013 &sd_size);
2015 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2016 SIVAL(params,0,(uint32_t)sd_size);
2017 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2018 params, 4, NULL, 0);
2019 return;
2022 if (!NT_STATUS_IS_OK(status)) {
2023 reply_nterror(req, status);
2024 return;
2027 SMB_ASSERT(sd_size > 0);
2029 SIVAL(params,0,(uint32_t)sd_size);
2031 if (max_data_count < sd_size) {
2032 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2033 params, 4, NULL, 0);
2034 return;
2038 * Allocate the data we will return.
2041 data = nttrans_realloc(ppdata, sd_size);
2042 if(data == NULL) {
2043 reply_nterror(req, NT_STATUS_NO_MEMORY);
2044 return;
2047 memcpy(data, marshalled_sd, sd_size);
2049 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2051 return;
2054 /****************************************************************************
2055 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2056 ****************************************************************************/
2058 static void call_nt_transact_set_security_desc(connection_struct *conn,
2059 struct smb_request *req,
2060 uint16 **ppsetup,
2061 uint32 setup_count,
2062 char **ppparams,
2063 uint32 parameter_count,
2064 char **ppdata,
2065 uint32 data_count,
2066 uint32 max_data_count)
2068 char *params= *ppparams;
2069 char *data = *ppdata;
2070 files_struct *fsp = NULL;
2071 uint32 security_info_sent = 0;
2072 NTSTATUS status;
2074 if(parameter_count < 8) {
2075 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2076 return;
2079 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2080 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2081 return;
2084 if (!CAN_WRITE(fsp->conn)) {
2085 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2086 return;
2089 if(!lp_nt_acl_support(SNUM(conn))) {
2090 goto done;
2093 security_info_sent = IVAL(params,4);
2095 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2096 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2098 if (data_count == 0) {
2099 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2100 return;
2103 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 reply_nterror(req, status);
2107 return;
2110 done:
2111 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2112 return;
2115 /****************************************************************************
2116 Reply to NT IOCTL
2117 ****************************************************************************/
2119 static void call_nt_transact_ioctl(connection_struct *conn,
2120 struct smb_request *req,
2121 uint16 **ppsetup, uint32 setup_count,
2122 char **ppparams, uint32 parameter_count,
2123 char **ppdata, uint32 data_count,
2124 uint32 max_data_count)
2126 uint32 function;
2127 uint16 fidnum;
2128 files_struct *fsp;
2129 uint8 isFSctl;
2130 uint8 compfilter;
2131 char *pdata = *ppdata;
2133 if (setup_count != 8) {
2134 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2135 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2136 return;
2139 function = IVAL(*ppsetup, 0);
2140 fidnum = SVAL(*ppsetup, 4);
2141 isFSctl = CVAL(*ppsetup, 6);
2142 compfilter = CVAL(*ppsetup, 7);
2144 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2145 function, fidnum, isFSctl, compfilter));
2147 fsp=file_fsp(req, fidnum);
2148 /* this check is done in each implemented function case for now
2149 because I don't want to break anything... --metze
2150 FSP_BELONGS_CONN(fsp,conn);*/
2152 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2154 switch (function) {
2155 case FSCTL_SET_SPARSE:
2157 bool set_sparse = true;
2158 NTSTATUS status;
2160 if (data_count >= 1 && pdata[0] == 0) {
2161 set_sparse = false;
2164 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
2165 fidnum, set_sparse));
2167 if (!check_fsp_open(conn, req, fsp)) {
2168 return;
2171 status = file_set_sparse(conn, fsp, set_sparse);
2172 if (!NT_STATUS_IS_OK(status)) {
2173 DEBUG(9,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2174 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2175 reply_nterror(req, status);
2176 return;
2179 DEBUG(10,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2180 smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
2181 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2182 return;
2184 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2186 unsigned char objid[16];
2188 /* This should return the object-id on this file.
2189 * I think I'll make this be the inode+dev. JRA.
2192 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2194 if (!check_fsp_open(conn, req, fsp)) {
2195 return;
2198 data_count = 64;
2199 pdata = nttrans_realloc(ppdata, data_count);
2200 if (pdata == NULL) {
2201 reply_nterror(req, NT_STATUS_NO_MEMORY);
2202 return;
2205 /* For backwards compatibility only store the dev/inode. */
2206 push_file_id_16(pdata, &fsp->file_id);
2207 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2208 push_file_id_16(pdata+32, &fsp->file_id);
2209 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2210 pdata, data_count);
2211 return;
2214 case FSCTL_GET_REPARSE_POINT:
2215 /* pretend this fail - my winXP does it like this
2216 * --metze
2219 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2220 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2221 return;
2223 case FSCTL_SET_REPARSE_POINT:
2224 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2225 * --metze
2228 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2229 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2230 return;
2232 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2235 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2236 * and return their volume names. If max_data_count is 16, then it is just
2237 * asking for the number of volumes and length of the combined names.
2239 * pdata is the data allocated by our caller, but that uses
2240 * total_data_count (which is 0 in our case) rather than max_data_count.
2241 * Allocate the correct amount and return the pointer to let
2242 * it be deallocated when we return.
2244 struct shadow_copy_data *shadow_data = NULL;
2245 bool labels = False;
2246 uint32 labels_data_count = 0;
2247 uint32 i;
2248 char *cur_pdata;
2250 if (!check_fsp_open(conn, req, fsp)) {
2251 return;
2254 if (max_data_count < 16) {
2255 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2256 max_data_count));
2257 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2258 return;
2261 if (max_data_count > 16) {
2262 labels = True;
2265 shadow_data = TALLOC_ZERO_P(talloc_tos(),
2266 struct shadow_copy_data);
2267 if (shadow_data == NULL) {
2268 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2269 reply_nterror(req, NT_STATUS_NO_MEMORY);
2270 return;
2274 * Call the VFS routine to actually do the work.
2276 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2277 TALLOC_FREE(shadow_data);
2278 if (errno == ENOSYS) {
2279 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2280 conn->connectpath));
2281 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2282 return;
2283 } else {
2284 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2285 conn->connectpath));
2286 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2287 return;
2291 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2293 if (!labels) {
2294 data_count = 16;
2295 } else {
2296 data_count = 12+labels_data_count+4;
2299 if (max_data_count<data_count) {
2300 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2301 max_data_count,data_count));
2302 TALLOC_FREE(shadow_data);
2303 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2304 return;
2307 pdata = nttrans_realloc(ppdata, data_count);
2308 if (pdata == NULL) {
2309 TALLOC_FREE(shadow_data);
2310 reply_nterror(req, NT_STATUS_NO_MEMORY);
2311 return;
2314 cur_pdata = pdata;
2316 /* num_volumes 4 bytes */
2317 SIVAL(pdata,0,shadow_data->num_volumes);
2319 if (labels) {
2320 /* num_labels 4 bytes */
2321 SIVAL(pdata,4,shadow_data->num_volumes);
2324 /* needed_data_count 4 bytes */
2325 SIVAL(pdata, 8, labels_data_count+4);
2327 cur_pdata+=12;
2329 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2330 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2331 if (labels && shadow_data->labels) {
2332 for (i=0;i<shadow_data->num_volumes;i++) {
2333 srvstr_push(pdata, req->flags2,
2334 cur_pdata, shadow_data->labels[i],
2335 2*sizeof(SHADOW_COPY_LABEL),
2336 STR_UNICODE|STR_TERMINATE);
2337 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2338 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2342 TALLOC_FREE(shadow_data);
2344 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2345 pdata, data_count);
2347 return;
2350 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2352 /* pretend this succeeded -
2354 * we have to send back a list with all files owned by this SID
2356 * but I have to check that --metze
2358 struct dom_sid sid;
2359 uid_t uid;
2360 size_t sid_len;
2362 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2364 if (!check_fsp_open(conn, req, fsp)) {
2365 return;
2368 if (data_count < 8) {
2369 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2370 return;
2373 sid_len = MIN(data_count-4,SID_MAX_SIZE);
2375 /* unknown 4 bytes: this is not the length of the sid :-( */
2376 /*unknown = IVAL(pdata,0);*/
2378 if (!sid_parse(pdata+4,sid_len,&sid)) {
2379 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2380 return;
2382 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2384 if (!sid_to_uid(&sid, &uid)) {
2385 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2386 sid_string_dbg(&sid),
2387 (unsigned long)sid_len));
2388 uid = (-1);
2391 /* we can take a look at the find source :-)
2393 * find ./ -uid $uid -name '*' is what we need here
2396 * and send 4bytes len and then NULL terminated unicode strings
2397 * for each file
2399 * but I don't know how to deal with the paged results
2400 * (maybe we can hang the result anywhere in the fsp struct)
2402 * we don't send all files at once
2403 * and at the next we should *not* start from the beginning,
2404 * so we have to cache the result
2406 * --metze
2409 /* this works for now... */
2410 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2411 return;
2413 case FSCTL_QUERY_ALLOCATED_RANGES:
2415 /* FIXME: This is just a dummy reply, telling that all of the
2416 * file is allocated. MKS cp needs that.
2417 * Adding the real allocated ranges via FIEMAP on Linux
2418 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2419 * this FSCTL correct for sparse files.
2421 NTSTATUS status;
2422 uint64_t offset, length;
2424 if (!check_fsp_open(conn, req, fsp)) {
2425 return;
2428 if (data_count != 16) {
2429 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2430 data_count));
2431 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2432 return;
2435 if (max_data_count < 16) {
2436 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
2437 max_data_count));
2438 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2439 return;
2442 offset = BVAL(pdata,0);
2443 length = BVAL(pdata,8);
2445 if (offset + length < offset) {
2446 /* No 64-bit integer wrap. */
2447 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2448 return;
2451 status = vfs_stat_fsp(fsp);
2452 if (!NT_STATUS_IS_OK(status)) {
2453 reply_nterror(req, status);
2454 return;
2457 if (offset > fsp->fsp_name->st.st_ex_size ||
2458 fsp->fsp_name->st.st_ex_size == 0 ||
2459 length == 0) {
2460 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2461 } else {
2462 uint64_t end = offset + length;
2463 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2464 SBVAL(pdata,0,0);
2465 SBVAL(pdata,8,end);
2466 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2467 pdata, 16);
2469 return;
2471 case FSCTL_IS_VOLUME_DIRTY:
2472 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2473 "(but not implemented)\n", (int)fidnum));
2475 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2476 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2478 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2479 return;
2480 default:
2481 /* Only print this once... */
2482 if (!logged_ioctl_message) {
2483 logged_ioctl_message = true;
2484 DEBUG(2,("call_nt_transact_ioctl(0x%x): "
2485 "Currently not implemented.\n",
2486 function));
2490 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2494 #ifdef HAVE_SYS_QUOTAS
2495 /****************************************************************************
2496 Reply to get user quota
2497 ****************************************************************************/
2499 static void call_nt_transact_get_user_quota(connection_struct *conn,
2500 struct smb_request *req,
2501 uint16 **ppsetup,
2502 uint32 setup_count,
2503 char **ppparams,
2504 uint32 parameter_count,
2505 char **ppdata,
2506 uint32 data_count,
2507 uint32 max_data_count)
2509 NTSTATUS nt_status = NT_STATUS_OK;
2510 char *params = *ppparams;
2511 char *pdata = *ppdata;
2512 char *entry;
2513 int data_len=0,param_len=0;
2514 int qt_len=0;
2515 int entry_len = 0;
2516 files_struct *fsp = NULL;
2517 uint16 level = 0;
2518 size_t sid_len;
2519 struct dom_sid sid;
2520 bool start_enum = True;
2521 SMB_NTQUOTA_STRUCT qt;
2522 SMB_NTQUOTA_LIST *tmp_list;
2523 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2525 ZERO_STRUCT(qt);
2527 /* access check */
2528 if (get_current_uid(conn) != 0) {
2529 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2530 "[%s]\n", lp_servicename(SNUM(conn)),
2531 conn->session_info->unix_name));
2532 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2533 return;
2537 * Ensure minimum number of parameters sent.
2540 if (parameter_count < 4) {
2541 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2542 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2543 return;
2546 /* maybe we can check the quota_fnum */
2547 fsp = file_fsp(req, SVAL(params,0));
2548 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2549 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2550 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2551 return;
2554 /* the NULL pointer checking for fsp->fake_file_handle->pd
2555 * is done by CHECK_NTQUOTA_HANDLE_OK()
2557 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2559 level = SVAL(params,2);
2561 /* unknown 12 bytes leading in params */
2563 switch (level) {
2564 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2565 /* seems that we should continue with the enum here --metze */
2567 if (qt_handle->quota_list!=NULL &&
2568 qt_handle->tmp_list==NULL) {
2570 /* free the list */
2571 free_ntquota_list(&(qt_handle->quota_list));
2573 /* Realloc the size of parameters and data we will return */
2574 param_len = 4;
2575 params = nttrans_realloc(ppparams, param_len);
2576 if(params == NULL) {
2577 reply_nterror(req, NT_STATUS_NO_MEMORY);
2578 return;
2581 data_len = 0;
2582 SIVAL(params,0,data_len);
2584 break;
2587 start_enum = False;
2589 case TRANSACT_GET_USER_QUOTA_LIST_START:
2591 if (qt_handle->quota_list==NULL &&
2592 qt_handle->tmp_list==NULL) {
2593 start_enum = True;
2596 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2597 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2598 return;
2601 /* Realloc the size of parameters and data we will return */
2602 param_len = 4;
2603 params = nttrans_realloc(ppparams, param_len);
2604 if(params == NULL) {
2605 reply_nterror(req, NT_STATUS_NO_MEMORY);
2606 return;
2609 /* we should not trust the value in max_data_count*/
2610 max_data_count = MIN(max_data_count,2048);
2612 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2613 if(pdata == NULL) {
2614 reply_nterror(req, NT_STATUS_NO_MEMORY);
2615 return;
2618 entry = pdata;
2620 /* set params Size of returned Quota Data 4 bytes*/
2621 /* but set it later when we know it */
2623 /* for each entry push the data */
2625 if (start_enum) {
2626 qt_handle->tmp_list = qt_handle->quota_list;
2629 tmp_list = qt_handle->tmp_list;
2631 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2632 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2634 sid_len = ndr_size_dom_sid(
2635 &tmp_list->quotas->sid, 0);
2636 entry_len = 40 + sid_len;
2638 /* nextoffset entry 4 bytes */
2639 SIVAL(entry,0,entry_len);
2641 /* then the len of the SID 4 bytes */
2642 SIVAL(entry,4,sid_len);
2644 /* unknown data 8 bytes uint64_t */
2645 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2647 /* the used disk space 8 bytes uint64_t */
2648 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2650 /* the soft quotas 8 bytes uint64_t */
2651 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2653 /* the hard quotas 8 bytes uint64_t */
2654 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2656 /* and now the SID */
2657 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2660 qt_handle->tmp_list = tmp_list;
2662 /* overwrite the offset of the last entry */
2663 SIVAL(entry-entry_len,0,0);
2665 data_len = 4+qt_len;
2666 /* overwrite the params quota_data_len */
2667 SIVAL(params,0,data_len);
2669 break;
2671 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2673 /* unknown 4 bytes IVAL(pdata,0) */
2675 if (data_count < 8) {
2676 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2677 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2678 return;
2681 sid_len = IVAL(pdata,4);
2682 /* Ensure this is less than 1mb. */
2683 if (sid_len > (1024*1024)) {
2684 reply_nterror(req, NT_STATUS_NO_MEMORY);
2685 return;
2688 if (data_count < 8+sid_len) {
2689 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2690 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2691 return;
2694 data_len = 4+40+sid_len;
2696 if (max_data_count < data_len) {
2697 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2698 max_data_count, data_len));
2699 param_len = 4;
2700 SIVAL(params,0,data_len);
2701 data_len = 0;
2702 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2703 break;
2706 if (!sid_parse(pdata+8,sid_len,&sid)) {
2707 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2708 return;
2711 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2712 ZERO_STRUCT(qt);
2714 * we have to return zero's in all fields
2715 * instead of returning an error here
2716 * --metze
2720 /* Realloc the size of parameters and data we will return */
2721 param_len = 4;
2722 params = nttrans_realloc(ppparams, param_len);
2723 if(params == NULL) {
2724 reply_nterror(req, NT_STATUS_NO_MEMORY);
2725 return;
2728 pdata = nttrans_realloc(ppdata, data_len);
2729 if(pdata == NULL) {
2730 reply_nterror(req, NT_STATUS_NO_MEMORY);
2731 return;
2734 entry = pdata;
2736 /* set params Size of returned Quota Data 4 bytes*/
2737 SIVAL(params,0,data_len);
2739 /* nextoffset entry 4 bytes */
2740 SIVAL(entry,0,0);
2742 /* then the len of the SID 4 bytes */
2743 SIVAL(entry,4,sid_len);
2745 /* unknown data 8 bytes uint64_t */
2746 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2748 /* the used disk space 8 bytes uint64_t */
2749 SBIG_UINT(entry,16,qt.usedspace);
2751 /* the soft quotas 8 bytes uint64_t */
2752 SBIG_UINT(entry,24,qt.softlim);
2754 /* the hard quotas 8 bytes uint64_t */
2755 SBIG_UINT(entry,32,qt.hardlim);
2757 /* and now the SID */
2758 sid_linearize(entry+40, sid_len, &sid);
2760 break;
2762 default:
2763 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2764 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2765 return;
2766 break;
2769 send_nt_replies(conn, req, nt_status, params, param_len,
2770 pdata, data_len);
2773 /****************************************************************************
2774 Reply to set user quota
2775 ****************************************************************************/
2777 static void call_nt_transact_set_user_quota(connection_struct *conn,
2778 struct smb_request *req,
2779 uint16 **ppsetup,
2780 uint32 setup_count,
2781 char **ppparams,
2782 uint32 parameter_count,
2783 char **ppdata,
2784 uint32 data_count,
2785 uint32 max_data_count)
2787 char *params = *ppparams;
2788 char *pdata = *ppdata;
2789 int data_len=0,param_len=0;
2790 SMB_NTQUOTA_STRUCT qt;
2791 size_t sid_len;
2792 struct dom_sid sid;
2793 files_struct *fsp = NULL;
2795 ZERO_STRUCT(qt);
2797 /* access check */
2798 if (get_current_uid(conn) != 0) {
2799 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2800 "[%s]\n", lp_servicename(SNUM(conn)),
2801 conn->session_info->unix_name));
2802 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2803 return;
2807 * Ensure minimum number of parameters sent.
2810 if (parameter_count < 2) {
2811 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2812 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2813 return;
2816 /* maybe we can check the quota_fnum */
2817 fsp = file_fsp(req, SVAL(params,0));
2818 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2819 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2820 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2821 return;
2824 if (data_count < 40) {
2825 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2826 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2827 return;
2830 /* offset to next quota record.
2831 * 4 bytes IVAL(pdata,0)
2832 * unused here...
2835 /* sid len */
2836 sid_len = IVAL(pdata,4);
2838 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2839 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2840 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2841 return;
2844 /* unknown 8 bytes in pdata
2845 * maybe its the change time in NTTIME
2848 /* the used space 8 bytes (uint64_t)*/
2849 qt.usedspace = BVAL(pdata,16);
2851 /* the soft quotas 8 bytes (uint64_t)*/
2852 qt.softlim = BVAL(pdata,24);
2854 /* the hard quotas 8 bytes (uint64_t)*/
2855 qt.hardlim = BVAL(pdata,32);
2857 if (!sid_parse(pdata+40,sid_len,&sid)) {
2858 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2859 return;
2862 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2864 /* 44 unknown bytes left... */
2866 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2867 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2868 return;
2871 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2872 pdata, data_len);
2874 #endif /* HAVE_SYS_QUOTAS */
2876 static void handle_nttrans(connection_struct *conn,
2877 struct trans_state *state,
2878 struct smb_request *req)
2880 if (get_Protocol() >= PROTOCOL_NT1) {
2881 req->flags2 |= 0x40; /* IS_LONG_NAME */
2882 SSVAL(req->inbuf,smb_flg2,req->flags2);
2886 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2888 /* Now we must call the relevant NT_TRANS function */
2889 switch(state->call) {
2890 case NT_TRANSACT_CREATE:
2892 START_PROFILE(NT_transact_create);
2893 call_nt_transact_create(
2894 conn, req,
2895 &state->setup, state->setup_count,
2896 &state->param, state->total_param,
2897 &state->data, state->total_data,
2898 state->max_data_return);
2899 END_PROFILE(NT_transact_create);
2900 break;
2903 case NT_TRANSACT_IOCTL:
2905 START_PROFILE(NT_transact_ioctl);
2906 call_nt_transact_ioctl(
2907 conn, req,
2908 &state->setup, state->setup_count,
2909 &state->param, state->total_param,
2910 &state->data, state->total_data,
2911 state->max_data_return);
2912 END_PROFILE(NT_transact_ioctl);
2913 break;
2916 case NT_TRANSACT_SET_SECURITY_DESC:
2918 START_PROFILE(NT_transact_set_security_desc);
2919 call_nt_transact_set_security_desc(
2920 conn, req,
2921 &state->setup, state->setup_count,
2922 &state->param, state->total_param,
2923 &state->data, state->total_data,
2924 state->max_data_return);
2925 END_PROFILE(NT_transact_set_security_desc);
2926 break;
2929 case NT_TRANSACT_NOTIFY_CHANGE:
2931 START_PROFILE(NT_transact_notify_change);
2932 call_nt_transact_notify_change(
2933 conn, req,
2934 &state->setup, state->setup_count,
2935 &state->param, state->total_param,
2936 &state->data, state->total_data,
2937 state->max_data_return,
2938 state->max_param_return);
2939 END_PROFILE(NT_transact_notify_change);
2940 break;
2943 case NT_TRANSACT_RENAME:
2945 START_PROFILE(NT_transact_rename);
2946 call_nt_transact_rename(
2947 conn, req,
2948 &state->setup, state->setup_count,
2949 &state->param, state->total_param,
2950 &state->data, state->total_data,
2951 state->max_data_return);
2952 END_PROFILE(NT_transact_rename);
2953 break;
2956 case NT_TRANSACT_QUERY_SECURITY_DESC:
2958 START_PROFILE(NT_transact_query_security_desc);
2959 call_nt_transact_query_security_desc(
2960 conn, req,
2961 &state->setup, state->setup_count,
2962 &state->param, state->total_param,
2963 &state->data, state->total_data,
2964 state->max_data_return);
2965 END_PROFILE(NT_transact_query_security_desc);
2966 break;
2969 #ifdef HAVE_SYS_QUOTAS
2970 case NT_TRANSACT_GET_USER_QUOTA:
2972 START_PROFILE(NT_transact_get_user_quota);
2973 call_nt_transact_get_user_quota(
2974 conn, req,
2975 &state->setup, state->setup_count,
2976 &state->param, state->total_param,
2977 &state->data, state->total_data,
2978 state->max_data_return);
2979 END_PROFILE(NT_transact_get_user_quota);
2980 break;
2983 case NT_TRANSACT_SET_USER_QUOTA:
2985 START_PROFILE(NT_transact_set_user_quota);
2986 call_nt_transact_set_user_quota(
2987 conn, req,
2988 &state->setup, state->setup_count,
2989 &state->param, state->total_param,
2990 &state->data, state->total_data,
2991 state->max_data_return);
2992 END_PROFILE(NT_transact_set_user_quota);
2993 break;
2995 #endif /* HAVE_SYS_QUOTAS */
2997 default:
2998 /* Error in request */
2999 DEBUG(0,("handle_nttrans: Unknown request %d in "
3000 "nttrans call\n", state->call));
3001 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3002 return;
3004 return;
3007 /****************************************************************************
3008 Reply to a SMBNTtrans.
3009 ****************************************************************************/
3011 void reply_nttrans(struct smb_request *req)
3013 connection_struct *conn = req->conn;
3014 uint32_t pscnt;
3015 uint32_t psoff;
3016 uint32_t dscnt;
3017 uint32_t dsoff;
3018 uint16 function_code;
3019 NTSTATUS result;
3020 struct trans_state *state;
3022 START_PROFILE(SMBnttrans);
3024 if (req->wct < 19) {
3025 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3026 END_PROFILE(SMBnttrans);
3027 return;
3030 pscnt = IVAL(req->vwv+9, 1);
3031 psoff = IVAL(req->vwv+11, 1);
3032 dscnt = IVAL(req->vwv+13, 1);
3033 dsoff = IVAL(req->vwv+15, 1);
3034 function_code = SVAL(req->vwv+18, 0);
3036 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3037 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3038 END_PROFILE(SMBnttrans);
3039 return;
3042 result = allow_new_trans(conn->pending_trans, req->mid);
3043 if (!NT_STATUS_IS_OK(result)) {
3044 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3045 reply_nterror(req, result);
3046 END_PROFILE(SMBnttrans);
3047 return;
3050 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3051 reply_nterror(req, NT_STATUS_NO_MEMORY);
3052 END_PROFILE(SMBnttrans);
3053 return;
3056 state->cmd = SMBnttrans;
3058 state->mid = req->mid;
3059 state->vuid = req->vuid;
3060 state->total_data = IVAL(req->vwv+3, 1);
3061 state->data = NULL;
3062 state->total_param = IVAL(req->vwv+1, 1);
3063 state->param = NULL;
3064 state->max_data_return = IVAL(req->vwv+7, 1);
3065 state->max_param_return = IVAL(req->vwv+5, 1);
3067 /* setup count is in *words* */
3068 state->setup_count = 2*CVAL(req->vwv+17, 1);
3069 state->setup = NULL;
3070 state->call = function_code;
3072 DEBUG(10, ("num_setup=%u, "
3073 "param_total=%u, this_param=%u, max_param=%u, "
3074 "data_total=%u, this_data=%u, max_data=%u, "
3075 "param_offset=%u, data_offset=%u\n",
3076 (unsigned)state->setup_count,
3077 (unsigned)state->total_param, (unsigned)pscnt,
3078 (unsigned)state->max_param_return,
3079 (unsigned)state->total_data, (unsigned)dscnt,
3080 (unsigned)state->max_data_return,
3081 (unsigned)psoff, (unsigned)dsoff));
3084 * All nttrans messages we handle have smb_wct == 19 +
3085 * state->setup_count. Ensure this is so as a sanity check.
3088 if(req->wct != 19 + (state->setup_count/2)) {
3089 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3090 req->wct, 19 + (state->setup_count/2)));
3091 goto bad_param;
3094 /* Don't allow more than 128mb for each value. */
3095 if ((state->total_data > (1024*1024*128)) ||
3096 (state->total_param > (1024*1024*128))) {
3097 reply_nterror(req, NT_STATUS_NO_MEMORY);
3098 END_PROFILE(SMBnttrans);
3099 return;
3102 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3103 goto bad_param;
3105 if (state->total_data) {
3107 if (trans_oob(state->total_data, 0, dscnt)
3108 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3109 goto bad_param;
3112 /* Can't use talloc here, the core routines do realloc on the
3113 * params and data. */
3114 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3115 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3116 "bytes !\n", (unsigned int)state->total_data));
3117 TALLOC_FREE(state);
3118 reply_nterror(req, NT_STATUS_NO_MEMORY);
3119 END_PROFILE(SMBnttrans);
3120 return;
3123 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3126 if (state->total_param) {
3128 if (trans_oob(state->total_param, 0, pscnt)
3129 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3130 goto bad_param;
3133 /* Can't use talloc here, the core routines do realloc on the
3134 * params and data. */
3135 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3136 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3137 "bytes !\n", (unsigned int)state->total_param));
3138 SAFE_FREE(state->data);
3139 TALLOC_FREE(state);
3140 reply_nterror(req, NT_STATUS_NO_MEMORY);
3141 END_PROFILE(SMBnttrans);
3142 return;
3145 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3148 state->received_data = dscnt;
3149 state->received_param = pscnt;
3151 if(state->setup_count > 0) {
3152 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3153 state->setup_count));
3156 * No overflow possible here, state->setup_count is an
3157 * unsigned int, being filled by a single byte from
3158 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3159 * below is not necessary, it's here to clarify things. The
3160 * validity of req->vwv and req->wct has been checked in
3161 * init_smb_request already.
3163 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3164 goto bad_param;
3167 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3168 if (state->setup == NULL) {
3169 DEBUG(0,("reply_nttrans : Out of memory\n"));
3170 SAFE_FREE(state->data);
3171 SAFE_FREE(state->param);
3172 TALLOC_FREE(state);
3173 reply_nterror(req, NT_STATUS_NO_MEMORY);
3174 END_PROFILE(SMBnttrans);
3175 return;
3178 memcpy(state->setup, req->vwv+19, state->setup_count);
3179 dump_data(10, (uint8 *)state->setup, state->setup_count);
3182 if ((state->received_data == state->total_data) &&
3183 (state->received_param == state->total_param)) {
3184 handle_nttrans(conn, state, req);
3185 SAFE_FREE(state->param);
3186 SAFE_FREE(state->data);
3187 TALLOC_FREE(state);
3188 END_PROFILE(SMBnttrans);
3189 return;
3192 DLIST_ADD(conn->pending_trans, state);
3194 /* We need to send an interim response then receive the rest
3195 of the parameter/data bytes */
3196 reply_outbuf(req, 0, 0);
3197 show_msg((char *)req->outbuf);
3198 END_PROFILE(SMBnttrans);
3199 return;
3201 bad_param:
3203 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3204 SAFE_FREE(state->data);
3205 SAFE_FREE(state->param);
3206 TALLOC_FREE(state);
3207 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3208 END_PROFILE(SMBnttrans);
3209 return;
3212 /****************************************************************************
3213 Reply to a SMBnttranss
3214 ****************************************************************************/
3216 void reply_nttranss(struct smb_request *req)
3218 connection_struct *conn = req->conn;
3219 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3220 struct trans_state *state;
3222 START_PROFILE(SMBnttranss);
3224 show_msg((char *)req->inbuf);
3226 if (req->wct < 18) {
3227 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3228 END_PROFILE(SMBnttranss);
3229 return;
3232 for (state = conn->pending_trans; state != NULL;
3233 state = state->next) {
3234 if (state->mid == req->mid) {
3235 break;
3239 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3240 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3241 END_PROFILE(SMBnttranss);
3242 return;
3245 /* Revise state->total_param and state->total_data in case they have
3246 changed downwards */
3247 if (IVAL(req->vwv+1, 1) < state->total_param) {
3248 state->total_param = IVAL(req->vwv+1, 1);
3250 if (IVAL(req->vwv+3, 1) < state->total_data) {
3251 state->total_data = IVAL(req->vwv+3, 1);
3254 pcnt = IVAL(req->vwv+5, 1);
3255 poff = IVAL(req->vwv+7, 1);
3256 pdisp = IVAL(req->vwv+9, 1);
3258 dcnt = IVAL(req->vwv+11, 1);
3259 doff = IVAL(req->vwv+13, 1);
3260 ddisp = IVAL(req->vwv+15, 1);
3262 state->received_param += pcnt;
3263 state->received_data += dcnt;
3265 if ((state->received_data > state->total_data) ||
3266 (state->received_param > state->total_param))
3267 goto bad_param;
3269 if (pcnt) {
3270 if (trans_oob(state->total_param, pdisp, pcnt)
3271 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3272 goto bad_param;
3274 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3277 if (dcnt) {
3278 if (trans_oob(state->total_data, ddisp, dcnt)
3279 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3280 goto bad_param;
3282 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3285 if ((state->received_param < state->total_param) ||
3286 (state->received_data < state->total_data)) {
3287 END_PROFILE(SMBnttranss);
3288 return;
3291 handle_nttrans(conn, state, req);
3293 DLIST_REMOVE(conn->pending_trans, state);
3294 SAFE_FREE(state->data);
3295 SAFE_FREE(state->param);
3296 TALLOC_FREE(state);
3297 END_PROFILE(SMBnttranss);
3298 return;
3300 bad_param:
3302 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3303 DLIST_REMOVE(conn->pending_trans, state);
3304 SAFE_FREE(state->data);
3305 SAFE_FREE(state->param);
3306 TALLOC_FREE(state);
3307 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3308 END_PROFILE(SMBnttranss);
3309 return;