docs: Fix variable list in man vfs_crossrename.
[Samba.git] / source3 / smbd / nttrans.c
bloba884b2f38f424b070c4c7aaf5e2763ea7f8be275
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 * 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(sconn,
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, int *ppnum)
285 files_struct *fsp;
286 NTSTATUS status;
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name. */
291 fname++;
293 status = open_np_file(req, fname, &fsp);
294 if (!NT_STATUS_IS_OK(status)) {
295 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
296 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
297 ERRDOS, ERRbadpipe);
298 return;
300 reply_nterror(req, status);
301 return;
304 *ppnum = fsp->fnum;
305 return;
308 /****************************************************************************
309 Reply to an NT create and X call for pipes.
310 ****************************************************************************/
312 static void do_ntcreate_pipe_open(connection_struct *conn,
313 struct smb_request *req)
315 char *fname = NULL;
316 int pnum = -1;
317 char *p = NULL;
318 uint32 flags = IVAL(req->vwv+3, 1);
319 TALLOC_CTX *ctx = talloc_tos();
321 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
323 if (!fname) {
324 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
325 ERRDOS, ERRbadpipe);
326 return;
328 nt_open_pipe(fname, conn, req, &pnum);
330 if (req->outbuf) {
331 /* error reply */
332 return;
336 * Deal with pipe return.
339 if (flags & EXTENDED_RESPONSE_REQUIRED) {
340 /* This is very strange. We
341 * return 50 words, but only set
342 * the wcnt to 42 ? It's definately
343 * what happens on the wire....
345 reply_outbuf(req, 50, 0);
346 SCVAL(req->outbuf,smb_wct,42);
347 } else {
348 reply_outbuf(req, 34, 0);
351 p = (char *)req->outbuf + smb_vwv2;
352 p++;
353 SSVAL(p,0,pnum);
354 p += 2;
355 SIVAL(p,0,FILE_WAS_OPENED);
356 p += 4;
357 p += 32;
358 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
359 p += 20;
360 /* File type. */
361 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
362 /* Device state. */
363 SSVAL(p,2, 0x5FF); /* ? */
364 p += 4;
366 if (flags & EXTENDED_RESPONSE_REQUIRED) {
367 p += 25;
368 SIVAL(p,0,FILE_GENERIC_ALL);
370 * For pipes W2K3 seems to return
371 * 0x12019B next.
372 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
374 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
377 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
379 chain_reply(req);
382 struct case_semantics_state {
383 connection_struct *conn;
384 bool case_sensitive;
385 bool case_preserve;
386 bool short_case_preserve;
389 /****************************************************************************
390 Restore case semantics.
391 ****************************************************************************/
393 static int restore_case_semantics(struct case_semantics_state *state)
395 state->conn->case_sensitive = state->case_sensitive;
396 state->conn->case_preserve = state->case_preserve;
397 state->conn->short_case_preserve = state->short_case_preserve;
398 return 0;
401 /****************************************************************************
402 Save case semantics.
403 ****************************************************************************/
405 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
406 connection_struct *conn)
408 struct case_semantics_state *result;
410 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
411 return NULL;
414 result->conn = conn;
415 result->case_sensitive = conn->case_sensitive;
416 result->case_preserve = conn->case_preserve;
417 result->short_case_preserve = conn->short_case_preserve;
419 /* Set to POSIX. */
420 conn->case_sensitive = True;
421 conn->case_preserve = True;
422 conn->short_case_preserve = True;
424 talloc_set_destructor(result, restore_case_semantics);
426 return result;
429 /****************************************************************************
430 Reply to an NT create and X call.
431 ****************************************************************************/
433 void reply_ntcreate_and_X(struct smb_request *req)
435 connection_struct *conn = req->conn;
436 struct smb_filename *smb_fname = NULL;
437 char *fname = NULL;
438 uint32 flags;
439 uint32 access_mask;
440 uint32 file_attributes;
441 uint32 share_access;
442 uint32 create_disposition;
443 uint32 create_options;
444 uint16 root_dir_fid;
445 uint64_t allocation_size;
446 /* Breakout the oplock request bits so we can set the
447 reply bits separately. */
448 uint32 fattr=0;
449 SMB_OFF_T file_len = 0;
450 int info = 0;
451 files_struct *fsp = NULL;
452 char *p = NULL;
453 struct timespec create_timespec;
454 struct timespec c_timespec;
455 struct timespec a_timespec;
456 struct timespec m_timespec;
457 struct timespec write_time_ts;
458 NTSTATUS status;
459 int oplock_request;
460 uint8_t oplock_granted = NO_OPLOCK_RETURN;
461 struct case_semantics_state *case_state = NULL;
462 TALLOC_CTX *ctx = talloc_tos();
464 START_PROFILE(SMBntcreateX);
466 if (req->wct < 24) {
467 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
468 goto out;
471 flags = IVAL(req->vwv+3, 1);
472 access_mask = IVAL(req->vwv+7, 1);
473 file_attributes = IVAL(req->vwv+13, 1);
474 share_access = IVAL(req->vwv+15, 1);
475 create_disposition = IVAL(req->vwv+17, 1);
476 create_options = IVAL(req->vwv+19, 1);
477 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
479 allocation_size = BVAL(req->vwv+9, 1);
481 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
482 STR_TERMINATE, &status);
484 if (!NT_STATUS_IS_OK(status)) {
485 reply_nterror(req, status);
486 goto out;
489 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
490 "file_attributes = 0x%x, share_access = 0x%x, "
491 "create_disposition = 0x%x create_options = 0x%x "
492 "root_dir_fid = 0x%x, fname = %s\n",
493 (unsigned int)flags,
494 (unsigned int)access_mask,
495 (unsigned int)file_attributes,
496 (unsigned int)share_access,
497 (unsigned int)create_disposition,
498 (unsigned int)create_options,
499 (unsigned int)root_dir_fid,
500 fname));
503 * we need to remove ignored bits when they come directly from the client
504 * because we reuse some of them for internal stuff
506 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
509 * If it's an IPC, use the pipe handler.
512 if (IS_IPC(conn)) {
513 if (lp_nt_pipe_support()) {
514 do_ntcreate_pipe_open(conn, req);
515 goto out;
517 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
518 goto out;
521 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
522 if (oplock_request) {
523 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
524 ? BATCH_OPLOCK : 0;
527 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
528 case_state = set_posix_case_semantics(ctx, conn);
529 if (!case_state) {
530 reply_nterror(req, NT_STATUS_NO_MEMORY);
531 goto out;
535 status = filename_convert(ctx,
536 conn,
537 req->flags2 & FLAGS2_DFS_PATHNAMES,
538 fname,
540 NULL,
541 &smb_fname);
543 TALLOC_FREE(case_state);
545 if (!NT_STATUS_IS_OK(status)) {
546 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
547 reply_botherror(req,
548 NT_STATUS_PATH_NOT_COVERED,
549 ERRSRV, ERRbadpath);
550 goto out;
552 reply_nterror(req, status);
553 goto out;
557 * Bug #6898 - clients using Windows opens should
558 * never be able to set this attribute into the
559 * VFS.
561 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
563 status = SMB_VFS_CREATE_FILE(
564 conn, /* conn */
565 req, /* req */
566 root_dir_fid, /* root_dir_fid */
567 smb_fname, /* fname */
568 access_mask, /* access_mask */
569 share_access, /* share_access */
570 create_disposition, /* create_disposition*/
571 create_options, /* create_options */
572 file_attributes, /* file_attributes */
573 oplock_request, /* oplock_request */
574 allocation_size, /* allocation_size */
575 0, /* private_flags */
576 NULL, /* sd */
577 NULL, /* ea_list */
578 &fsp, /* result */
579 &info); /* pinfo */
581 if (!NT_STATUS_IS_OK(status)) {
582 if (open_was_deferred(req->mid)) {
583 /* We have re-scheduled this call, no error. */
584 goto out;
586 reply_openerror(req, status);
587 goto out;
590 /* Ensure we're pointing at the correct stat struct. */
591 TALLOC_FREE(smb_fname);
592 smb_fname = fsp->fsp_name;
595 * If the caller set the extended oplock request bit
596 * and we granted one (by whatever means) - set the
597 * correct bit for extended oplock reply.
600 if (oplock_request &&
601 (lp_fake_oplocks(SNUM(conn))
602 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
605 * Exclusive oplock granted
608 if (flags & REQUEST_BATCH_OPLOCK) {
609 oplock_granted = BATCH_OPLOCK_RETURN;
610 } else {
611 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
613 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
614 oplock_granted = LEVEL_II_OPLOCK_RETURN;
615 } else {
616 oplock_granted = NO_OPLOCK_RETURN;
619 file_len = smb_fname->st.st_ex_size;
621 if (flags & EXTENDED_RESPONSE_REQUIRED) {
622 /* This is very strange. We
623 * return 50 words, but only set
624 * the wcnt to 42 ? It's definately
625 * what happens on the wire....
627 reply_outbuf(req, 50, 0);
628 SCVAL(req->outbuf,smb_wct,42);
629 } else {
630 reply_outbuf(req, 34, 0);
633 p = (char *)req->outbuf + smb_vwv2;
635 SCVAL(p, 0, oplock_granted);
637 p++;
638 SSVAL(p,0,fsp->fnum);
639 p += 2;
640 if ((create_disposition == FILE_SUPERSEDE)
641 && (info == FILE_WAS_OVERWRITTEN)) {
642 SIVAL(p,0,FILE_WAS_SUPERSEDED);
643 } else {
644 SIVAL(p,0,info);
646 p += 4;
648 fattr = dos_mode(conn, smb_fname);
649 if (fattr == 0) {
650 fattr = FILE_ATTRIBUTE_NORMAL;
653 /* Deal with other possible opens having a modified
654 write time. JRA. */
655 ZERO_STRUCT(write_time_ts);
656 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
657 if (!null_timespec(write_time_ts)) {
658 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
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 size_t num_names = 0;
691 unsigned int num_streams = 0;
692 struct stream_struct *streams = NULL;
694 /* Do we have any EA's ? */
695 status = get_ea_names_from_file(ctx, conn, fsp,
696 smb_fname->base_name, NULL, &num_names);
697 if (NT_STATUS_IS_OK(status) && num_names) {
698 file_status &= ~NO_EAS;
700 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
701 &num_streams, &streams);
702 /* There is always one stream, ::$DATA. */
703 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
704 file_status &= ~NO_SUBSTREAMS;
706 TALLOC_FREE(streams);
707 SSVAL(p,2,file_status);
709 p += 4;
710 SCVAL(p,0,fsp->is_directory ? 1 : 0);
712 if (flags & EXTENDED_RESPONSE_REQUIRED) {
713 uint32 perms = 0;
714 p += 25;
715 if (fsp->is_directory ||
716 can_write_to_file(conn, smb_fname)) {
717 perms = FILE_GENERIC_ALL;
718 } else {
719 perms = FILE_GENERIC_READ|FILE_EXECUTE;
721 SIVAL(p,0,perms);
724 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
725 fsp->fnum, smb_fname_str_dbg(smb_fname)));
727 chain_reply(req);
728 out:
729 END_PROFILE(SMBntcreateX);
730 return;
733 /****************************************************************************
734 Reply to a NT_TRANSACT_CREATE call to open a pipe.
735 ****************************************************************************/
737 static void do_nt_transact_create_pipe(connection_struct *conn,
738 struct smb_request *req,
739 uint16 **ppsetup, uint32 setup_count,
740 char **ppparams, uint32 parameter_count,
741 char **ppdata, uint32 data_count)
743 char *fname = NULL;
744 char *params = *ppparams;
745 int pnum = -1;
746 char *p = NULL;
747 NTSTATUS status;
748 size_t param_len;
749 uint32 flags;
750 TALLOC_CTX *ctx = talloc_tos();
753 * Ensure minimum number of parameters sent.
756 if(parameter_count < 54) {
757 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
758 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
759 return;
762 flags = IVAL(params,0);
764 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
765 parameter_count-53, STR_TERMINATE,
766 &status);
767 if (!NT_STATUS_IS_OK(status)) {
768 reply_nterror(req, status);
769 return;
772 nt_open_pipe(fname, conn, req, &pnum);
774 if (req->outbuf) {
775 /* Error return */
776 return;
779 /* Realloc the size of parameters and data we will return */
780 if (flags & EXTENDED_RESPONSE_REQUIRED) {
781 /* Extended response is 32 more byyes. */
782 param_len = 101;
783 } else {
784 param_len = 69;
786 params = nttrans_realloc(ppparams, param_len);
787 if(params == NULL) {
788 reply_nterror(req, NT_STATUS_NO_MEMORY);
789 return;
792 p = params;
793 SCVAL(p,0,NO_OPLOCK_RETURN);
795 p += 2;
796 SSVAL(p,0,pnum);
797 p += 2;
798 SIVAL(p,0,FILE_WAS_OPENED);
799 p += 8;
801 p += 32;
802 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
803 p += 20;
804 /* File type. */
805 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
806 /* Device state. */
807 SSVAL(p,2, 0x5FF); /* ? */
808 p += 4;
810 if (flags & EXTENDED_RESPONSE_REQUIRED) {
811 p += 25;
812 SIVAL(p,0,FILE_GENERIC_ALL);
814 * For pipes W2K3 seems to return
815 * 0x12019B next.
816 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
818 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
821 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
823 /* Send the required number of replies */
824 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
826 return;
829 /*********************************************************************
830 Windows seems to do canonicalization of inheritance bits. Do the
831 same.
832 *********************************************************************/
834 static void canonicalize_inheritance_bits(struct security_descriptor *psd)
836 bool set_auto_inherited = false;
839 * We need to filter out the
840 * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
841 * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
842 * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
843 * when an ACE is inherited. Otherwise we zero these bits out.
844 * See:
846 * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
848 * for details.
851 if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
852 == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
853 set_auto_inherited = true;
856 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
857 if (set_auto_inherited) {
858 psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
862 /****************************************************************************
863 Internal fn to set security descriptors.
864 ****************************************************************************/
866 NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
867 uint32_t security_info_sent)
869 NTSTATUS status;
871 if (!CAN_WRITE(fsp->conn)) {
872 return NT_STATUS_ACCESS_DENIED;
875 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
876 return NT_STATUS_OK;
879 if (psd->owner_sid == NULL) {
880 security_info_sent &= ~SECINFO_OWNER;
882 if (psd->group_sid == NULL) {
883 security_info_sent &= ~SECINFO_GROUP;
886 /* Ensure we have at least one thing set. */
887 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
888 /* Just like W2K3 */
889 return NT_STATUS_OK;
892 /* Ensure we have the rights to do this. */
893 if (security_info_sent & SECINFO_OWNER) {
894 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
895 return NT_STATUS_ACCESS_DENIED;
899 if (security_info_sent & SECINFO_GROUP) {
900 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
901 return NT_STATUS_ACCESS_DENIED;
905 if (security_info_sent & SECINFO_DACL) {
906 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
907 return NT_STATUS_ACCESS_DENIED;
909 /* Convert all the generic bits. */
910 if (psd->dacl) {
911 security_acl_map_generic(psd->dacl, &file_generic_mapping);
915 if (security_info_sent & SECINFO_SACL) {
916 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
917 return NT_STATUS_ACCESS_DENIED;
919 /* Convert all the generic bits. */
920 if (psd->sacl) {
921 security_acl_map_generic(psd->sacl, &file_generic_mapping);
925 canonicalize_inheritance_bits(psd);
927 if (DEBUGLEVEL >= 10) {
928 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
929 NDR_PRINT_DEBUG(security_descriptor, psd);
932 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
934 TALLOC_FREE(psd);
936 return status;
939 /****************************************************************************
940 Internal fn to set security descriptors from a data blob.
941 ****************************************************************************/
943 NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
944 uint32_t security_info_sent)
946 struct security_descriptor *psd = NULL;
947 NTSTATUS status;
949 if (sd_len == 0) {
950 return NT_STATUS_INVALID_PARAMETER;
953 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
955 if (!NT_STATUS_IS_OK(status)) {
956 return status;
959 return set_sd(fsp, psd, security_info_sent);
962 /****************************************************************************
963 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
964 ****************************************************************************/
966 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
968 struct ea_list *ea_list_head = NULL;
969 size_t offset = 0;
971 if (data_size < 4) {
972 return NULL;
975 while (offset + 4 <= data_size) {
976 size_t next_offset = IVAL(pdata,offset);
977 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
979 if (!eal) {
980 return NULL;
983 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
984 if (next_offset == 0) {
985 break;
988 /* Integer wrap protection for the increment. */
989 if (offset + next_offset < offset) {
990 break;
993 offset += next_offset;
995 /* Integer wrap protection for while loop. */
996 if (offset + 4 < offset) {
997 break;
1002 return ea_list_head;
1005 /****************************************************************************
1006 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1007 ****************************************************************************/
1009 static void call_nt_transact_create(connection_struct *conn,
1010 struct smb_request *req,
1011 uint16 **ppsetup, uint32 setup_count,
1012 char **ppparams, uint32 parameter_count,
1013 char **ppdata, uint32 data_count,
1014 uint32 max_data_count)
1016 struct smb_filename *smb_fname = NULL;
1017 char *fname = NULL;
1018 char *params = *ppparams;
1019 char *data = *ppdata;
1020 /* Breakout the oplock request bits so we can set the reply bits separately. */
1021 uint32 fattr=0;
1022 SMB_OFF_T file_len = 0;
1023 int info = 0;
1024 files_struct *fsp = NULL;
1025 char *p = NULL;
1026 uint32 flags;
1027 uint32 access_mask;
1028 uint32 file_attributes;
1029 uint32 share_access;
1030 uint32 create_disposition;
1031 uint32 create_options;
1032 uint32 sd_len;
1033 struct security_descriptor *sd = NULL;
1034 uint32 ea_len;
1035 uint16 root_dir_fid;
1036 struct timespec create_timespec;
1037 struct timespec c_timespec;
1038 struct timespec a_timespec;
1039 struct timespec m_timespec;
1040 struct timespec write_time_ts;
1041 struct ea_list *ea_list = NULL;
1042 NTSTATUS status;
1043 size_t param_len;
1044 uint64_t allocation_size;
1045 int oplock_request;
1046 uint8_t oplock_granted;
1047 struct case_semantics_state *case_state = NULL;
1048 TALLOC_CTX *ctx = talloc_tos();
1050 DEBUG(5,("call_nt_transact_create\n"));
1053 * If it's an IPC, use the pipe handler.
1056 if (IS_IPC(conn)) {
1057 if (lp_nt_pipe_support()) {
1058 do_nt_transact_create_pipe(
1059 conn, req,
1060 ppsetup, setup_count,
1061 ppparams, parameter_count,
1062 ppdata, data_count);
1063 goto out;
1065 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1066 goto out;
1070 * Ensure minimum number of parameters sent.
1073 if(parameter_count < 54) {
1074 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1075 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1076 goto out;
1079 flags = IVAL(params,0);
1080 access_mask = IVAL(params,8);
1081 file_attributes = IVAL(params,20);
1082 share_access = IVAL(params,24);
1083 create_disposition = IVAL(params,28);
1084 create_options = IVAL(params,32);
1085 sd_len = IVAL(params,36);
1086 ea_len = IVAL(params,40);
1087 root_dir_fid = (uint16)IVAL(params,4);
1088 allocation_size = BVAL(params,12);
1091 * we need to remove ignored bits when they come directly from the client
1092 * because we reuse some of them for internal stuff
1094 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1096 /* Ensure the data_len is correct for the sd and ea values given. */
1097 if ((ea_len + sd_len > data_count)
1098 || (ea_len > data_count) || (sd_len > data_count)
1099 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1100 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1101 "%u, data_count = %u\n", (unsigned int)ea_len,
1102 (unsigned int)sd_len, (unsigned int)data_count));
1103 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1104 goto out;
1107 if (sd_len) {
1108 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1109 sd_len));
1111 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1112 &sd);
1113 if (!NT_STATUS_IS_OK(status)) {
1114 DEBUG(10, ("call_nt_transact_create: "
1115 "unmarshall_sec_desc failed: %s\n",
1116 nt_errstr(status)));
1117 reply_nterror(req, status);
1118 goto out;
1122 if (ea_len) {
1123 if (!lp_ea_support(SNUM(conn))) {
1124 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1125 "EA's not supported.\n",
1126 (unsigned int)ea_len));
1127 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1128 goto out;
1131 if (ea_len < 10) {
1132 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1133 "too small (should be more than 10)\n",
1134 (unsigned int)ea_len ));
1135 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1136 goto out;
1139 /* We have already checked that ea_len <= data_count here. */
1140 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1141 ea_len);
1142 if (ea_list == NULL) {
1143 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1144 goto out;
1148 srvstr_get_path(ctx, params, req->flags2, &fname,
1149 params+53, parameter_count-53,
1150 STR_TERMINATE, &status);
1151 if (!NT_STATUS_IS_OK(status)) {
1152 reply_nterror(req, status);
1153 goto out;
1156 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1157 case_state = set_posix_case_semantics(ctx, conn);
1158 if (!case_state) {
1159 reply_nterror(req, NT_STATUS_NO_MEMORY);
1160 goto out;
1164 status = filename_convert(ctx,
1165 conn,
1166 req->flags2 & FLAGS2_DFS_PATHNAMES,
1167 fname,
1169 NULL,
1170 &smb_fname);
1172 TALLOC_FREE(case_state);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1176 reply_botherror(req,
1177 NT_STATUS_PATH_NOT_COVERED,
1178 ERRSRV, ERRbadpath);
1179 goto out;
1181 reply_nterror(req, status);
1182 goto out;
1185 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1186 if (oplock_request) {
1187 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1188 ? BATCH_OPLOCK : 0;
1192 * Bug #6898 - clients using Windows opens should
1193 * never be able to set this attribute into the
1194 * VFS.
1196 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1198 status = SMB_VFS_CREATE_FILE(
1199 conn, /* conn */
1200 req, /* req */
1201 root_dir_fid, /* root_dir_fid */
1202 smb_fname, /* fname */
1203 access_mask, /* access_mask */
1204 share_access, /* share_access */
1205 create_disposition, /* create_disposition*/
1206 create_options, /* create_options */
1207 file_attributes, /* file_attributes */
1208 oplock_request, /* oplock_request */
1209 allocation_size, /* allocation_size */
1210 0, /* private_flags */
1211 sd, /* sd */
1212 ea_list, /* ea_list */
1213 &fsp, /* result */
1214 &info); /* pinfo */
1216 if(!NT_STATUS_IS_OK(status)) {
1217 if (open_was_deferred(req->mid)) {
1218 /* We have re-scheduled this call, no error. */
1219 return;
1221 reply_openerror(req, status);
1222 goto out;
1225 /* Ensure we're pointing at the correct stat struct. */
1226 TALLOC_FREE(smb_fname);
1227 smb_fname = fsp->fsp_name;
1230 * If the caller set the extended oplock request bit
1231 * and we granted one (by whatever means) - set the
1232 * correct bit for extended oplock reply.
1235 if (oplock_request &&
1236 (lp_fake_oplocks(SNUM(conn))
1237 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1240 * Exclusive oplock granted
1243 if (flags & REQUEST_BATCH_OPLOCK) {
1244 oplock_granted = BATCH_OPLOCK_RETURN;
1245 } else {
1246 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1248 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1249 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1250 } else {
1251 oplock_granted = NO_OPLOCK_RETURN;
1254 file_len = smb_fname->st.st_ex_size;
1256 /* Realloc the size of parameters and data we will return */
1257 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1258 /* Extended response is 32 more byyes. */
1259 param_len = 101;
1260 } else {
1261 param_len = 69;
1263 params = nttrans_realloc(ppparams, param_len);
1264 if(params == NULL) {
1265 reply_nterror(req, NT_STATUS_NO_MEMORY);
1266 goto out;
1269 p = params;
1270 SCVAL(p, 0, oplock_granted);
1272 p += 2;
1273 SSVAL(p,0,fsp->fnum);
1274 p += 2;
1275 if ((create_disposition == FILE_SUPERSEDE)
1276 && (info == FILE_WAS_OVERWRITTEN)) {
1277 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1278 } else {
1279 SIVAL(p,0,info);
1281 p += 8;
1283 fattr = dos_mode(conn, smb_fname);
1284 if (fattr == 0) {
1285 fattr = FILE_ATTRIBUTE_NORMAL;
1288 /* Deal with other possible opens having a modified
1289 write time. JRA. */
1290 ZERO_STRUCT(write_time_ts);
1291 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1292 if (!null_timespec(write_time_ts)) {
1293 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1296 /* Create time. */
1297 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1298 a_timespec = smb_fname->st.st_ex_atime;
1299 m_timespec = smb_fname->st.st_ex_mtime;
1300 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1302 if (lp_dos_filetime_resolution(SNUM(conn))) {
1303 dos_filetime_timespec(&create_timespec);
1304 dos_filetime_timespec(&a_timespec);
1305 dos_filetime_timespec(&m_timespec);
1306 dos_filetime_timespec(&c_timespec);
1309 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1310 p += 8;
1311 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1312 p += 8;
1313 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1314 p += 8;
1315 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1316 p += 8;
1317 SIVAL(p,0,fattr); /* File Attributes. */
1318 p += 4;
1319 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1320 p += 8;
1321 SOFF_T(p,0,file_len);
1322 p += 8;
1323 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1324 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1325 size_t num_names = 0;
1326 unsigned int num_streams = 0;
1327 struct stream_struct *streams = NULL;
1329 /* Do we have any EA's ? */
1330 status = get_ea_names_from_file(ctx, conn, fsp,
1331 smb_fname->base_name, NULL, &num_names);
1332 if (NT_STATUS_IS_OK(status) && num_names) {
1333 file_status &= ~NO_EAS;
1335 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1336 &num_streams, &streams);
1337 /* There is always one stream, ::$DATA. */
1338 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1339 file_status &= ~NO_SUBSTREAMS;
1341 TALLOC_FREE(streams);
1342 SSVAL(p,2,file_status);
1344 p += 4;
1345 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1347 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1348 uint32 perms = 0;
1349 p += 25;
1350 if (fsp->is_directory ||
1351 can_write_to_file(conn, smb_fname)) {
1352 perms = FILE_GENERIC_ALL;
1353 } else {
1354 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1356 SIVAL(p,0,perms);
1359 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1360 smb_fname_str_dbg(smb_fname)));
1362 /* Send the required number of replies */
1363 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1364 out:
1365 return;
1368 /****************************************************************************
1369 Reply to a NT CANCEL request.
1370 conn POINTER CAN BE NULL HERE !
1371 ****************************************************************************/
1373 void reply_ntcancel(struct smb_request *req)
1376 * Go through and cancel any pending change notifies.
1379 START_PROFILE(SMBntcancel);
1380 srv_cancel_sign_response(req->sconn);
1381 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1382 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1384 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1385 (unsigned long long)req->mid));
1387 END_PROFILE(SMBntcancel);
1388 return;
1391 /****************************************************************************
1392 Copy a file.
1393 ****************************************************************************/
1395 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1396 connection_struct *conn,
1397 struct smb_request *req,
1398 struct smb_filename *smb_fname_src,
1399 struct smb_filename *smb_fname_dst,
1400 uint32 attrs)
1402 files_struct *fsp1,*fsp2;
1403 uint32 fattr;
1404 int info;
1405 SMB_OFF_T ret=-1;
1406 NTSTATUS status = NT_STATUS_OK;
1407 char *parent;
1409 if (!CAN_WRITE(conn)) {
1410 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1411 goto out;
1414 /* Source must already exist. */
1415 if (!VALID_STAT(smb_fname_src->st)) {
1416 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1417 goto out;
1420 /* Ensure attributes match. */
1421 fattr = dos_mode(conn, smb_fname_src);
1422 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1423 status = NT_STATUS_NO_SUCH_FILE;
1424 goto out;
1427 /* Disallow if dst file already exists. */
1428 if (VALID_STAT(smb_fname_dst->st)) {
1429 status = NT_STATUS_OBJECT_NAME_COLLISION;
1430 goto out;
1433 /* No links from a directory. */
1434 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1435 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1436 goto out;
1439 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1440 smb_fname_str_dbg(smb_fname_src),
1441 smb_fname_str_dbg(smb_fname_dst)));
1443 status = SMB_VFS_CREATE_FILE(
1444 conn, /* conn */
1445 req, /* req */
1446 0, /* root_dir_fid */
1447 smb_fname_src, /* fname */
1448 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1449 FILE_READ_EA, /* access_mask */
1450 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1451 FILE_SHARE_DELETE),
1452 FILE_OPEN, /* create_disposition*/
1453 0, /* create_options */
1454 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1455 NO_OPLOCK, /* oplock_request */
1456 0, /* allocation_size */
1457 0, /* private_flags */
1458 NULL, /* sd */
1459 NULL, /* ea_list */
1460 &fsp1, /* result */
1461 &info); /* pinfo */
1463 if (!NT_STATUS_IS_OK(status)) {
1464 goto out;
1467 status = SMB_VFS_CREATE_FILE(
1468 conn, /* conn */
1469 req, /* req */
1470 0, /* root_dir_fid */
1471 smb_fname_dst, /* fname */
1472 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1473 FILE_WRITE_EA, /* access_mask */
1474 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1475 FILE_SHARE_DELETE),
1476 FILE_CREATE, /* create_disposition*/
1477 0, /* create_options */
1478 fattr, /* file_attributes */
1479 NO_OPLOCK, /* oplock_request */
1480 0, /* allocation_size */
1481 0, /* private_flags */
1482 NULL, /* sd */
1483 NULL, /* ea_list */
1484 &fsp2, /* result */
1485 &info); /* pinfo */
1487 if (!NT_STATUS_IS_OK(status)) {
1488 close_file(NULL, fsp1, ERROR_CLOSE);
1489 goto out;
1492 if (smb_fname_src->st.st_ex_size) {
1493 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1497 * As we are opening fsp1 read-only we only expect
1498 * an error on close on fsp2 if we are out of space.
1499 * Thus we don't look at the error return from the
1500 * close of fsp1.
1502 close_file(NULL, fsp1, NORMAL_CLOSE);
1504 /* Ensure the modtime is set correctly on the destination file. */
1505 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1507 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1509 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1510 creates the file. This isn't the correct thing to do in the copy
1511 case. JRA */
1512 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1513 NULL)) {
1514 status = NT_STATUS_NO_MEMORY;
1515 goto out;
1517 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1518 TALLOC_FREE(parent);
1520 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1521 status = NT_STATUS_DISK_FULL;
1522 goto out;
1524 out:
1525 if (!NT_STATUS_IS_OK(status)) {
1526 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1527 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1528 smb_fname_str_dbg(smb_fname_dst)));
1531 return status;
1534 /****************************************************************************
1535 Reply to a NT rename request.
1536 ****************************************************************************/
1538 void reply_ntrename(struct smb_request *req)
1540 connection_struct *conn = req->conn;
1541 struct smb_filename *smb_fname_old = NULL;
1542 struct smb_filename *smb_fname_new = NULL;
1543 char *oldname = NULL;
1544 char *newname = NULL;
1545 const char *p;
1546 NTSTATUS status;
1547 bool src_has_wcard = False;
1548 bool dest_has_wcard = False;
1549 uint32 attrs;
1550 uint32_t ucf_flags_src = 0;
1551 uint32_t ucf_flags_dst = 0;
1552 uint16 rename_type;
1553 TALLOC_CTX *ctx = talloc_tos();
1554 bool stream_rename = false;
1556 START_PROFILE(SMBntrename);
1558 if (req->wct < 4) {
1559 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1560 goto out;
1563 attrs = SVAL(req->vwv+0, 0);
1564 rename_type = SVAL(req->vwv+1, 0);
1566 p = (const char *)req->buf + 1;
1567 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1568 &status, &src_has_wcard);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 reply_nterror(req, status);
1571 goto out;
1574 if (ms_has_wild(oldname)) {
1575 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1576 goto out;
1579 p++;
1580 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1581 &status, &dest_has_wcard);
1582 if (!NT_STATUS_IS_OK(status)) {
1583 reply_nterror(req, status);
1584 goto out;
1587 if (!lp_posix_pathnames()) {
1588 /* The newname must begin with a ':' if the
1589 oldname contains a ':'. */
1590 if (strchr_m(oldname, ':')) {
1591 if (newname[0] != ':') {
1592 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1593 goto out;
1595 stream_rename = true;
1600 * If this is a rename operation, allow wildcards and save the
1601 * destination's last component.
1603 if (rename_type == RENAME_FLAG_RENAME) {
1604 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1605 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1608 /* rename_internals() calls unix_convert(), so don't call it here. */
1609 status = filename_convert(ctx, conn,
1610 req->flags2 & FLAGS2_DFS_PATHNAMES,
1611 oldname,
1612 ucf_flags_src,
1613 NULL,
1614 &smb_fname_old);
1615 if (!NT_STATUS_IS_OK(status)) {
1616 if (NT_STATUS_EQUAL(status,
1617 NT_STATUS_PATH_NOT_COVERED)) {
1618 reply_botherror(req,
1619 NT_STATUS_PATH_NOT_COVERED,
1620 ERRSRV, ERRbadpath);
1621 goto out;
1623 reply_nterror(req, status);
1624 goto out;
1627 status = filename_convert(ctx, conn,
1628 req->flags2 & FLAGS2_DFS_PATHNAMES,
1629 newname,
1630 ucf_flags_dst,
1631 &dest_has_wcard,
1632 &smb_fname_new);
1633 if (!NT_STATUS_IS_OK(status)) {
1634 if (NT_STATUS_EQUAL(status,
1635 NT_STATUS_PATH_NOT_COVERED)) {
1636 reply_botherror(req,
1637 NT_STATUS_PATH_NOT_COVERED,
1638 ERRSRV, ERRbadpath);
1639 goto out;
1641 reply_nterror(req, status);
1642 goto out;
1645 if (stream_rename) {
1646 /* smb_fname_new must be the same as smb_fname_old. */
1647 TALLOC_FREE(smb_fname_new->base_name);
1648 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1649 smb_fname_old->base_name);
1650 if (!smb_fname_new->base_name) {
1651 reply_nterror(req, NT_STATUS_NO_MEMORY);
1652 goto out;
1656 DEBUG(3,("reply_ntrename: %s -> %s\n",
1657 smb_fname_str_dbg(smb_fname_old),
1658 smb_fname_str_dbg(smb_fname_new)));
1660 switch(rename_type) {
1661 case RENAME_FLAG_RENAME:
1662 status = rename_internals(ctx, conn, req,
1663 smb_fname_old, smb_fname_new,
1664 attrs, False, src_has_wcard,
1665 dest_has_wcard,
1666 DELETE_ACCESS);
1667 break;
1668 case RENAME_FLAG_HARD_LINK:
1669 if (src_has_wcard || dest_has_wcard) {
1670 /* No wildcards. */
1671 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1672 } else {
1673 status = hardlink_internals(ctx, conn,
1674 req,
1675 false,
1676 smb_fname_old,
1677 smb_fname_new);
1679 break;
1680 case RENAME_FLAG_COPY:
1681 if (src_has_wcard || dest_has_wcard) {
1682 /* No wildcards. */
1683 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1684 } else {
1685 status = copy_internals(ctx, conn, req,
1686 smb_fname_old,
1687 smb_fname_new,
1688 attrs);
1690 break;
1691 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1692 status = NT_STATUS_INVALID_PARAMETER;
1693 break;
1694 default:
1695 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1696 break;
1699 if (!NT_STATUS_IS_OK(status)) {
1700 if (open_was_deferred(req->mid)) {
1701 /* We have re-scheduled this call. */
1702 goto out;
1705 reply_nterror(req, status);
1706 goto out;
1709 reply_outbuf(req, 0, 0);
1710 out:
1711 END_PROFILE(SMBntrename);
1712 return;
1715 /****************************************************************************
1716 Reply to a notify change - queue the request and
1717 don't allow a directory to be opened.
1718 ****************************************************************************/
1720 static void smbd_smb1_notify_reply(struct smb_request *req,
1721 NTSTATUS error_code,
1722 uint8_t *buf, size_t len)
1724 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1727 static void call_nt_transact_notify_change(connection_struct *conn,
1728 struct smb_request *req,
1729 uint16 **ppsetup,
1730 uint32 setup_count,
1731 char **ppparams,
1732 uint32 parameter_count,
1733 char **ppdata, uint32 data_count,
1734 uint32 max_data_count,
1735 uint32 max_param_count)
1737 uint16 *setup = *ppsetup;
1738 files_struct *fsp;
1739 uint32 filter;
1740 NTSTATUS status;
1741 bool recursive;
1743 if(setup_count < 6) {
1744 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1745 return;
1748 fsp = file_fsp(req, SVAL(setup,4));
1749 filter = IVAL(setup, 0);
1750 recursive = (SVAL(setup, 6) != 0) ? True : False;
1752 DEBUG(3,("call_nt_transact_notify_change\n"));
1754 if(!fsp) {
1755 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1756 return;
1760 char *filter_string;
1762 if (!(filter_string = notify_filter_string(NULL, filter))) {
1763 reply_nterror(req,NT_STATUS_NO_MEMORY);
1764 return;
1767 DEBUG(3,("call_nt_transact_notify_change: notify change "
1768 "called on %s, filter = %s, recursive = %d\n",
1769 fsp_str_dbg(fsp), filter_string, recursive));
1771 TALLOC_FREE(filter_string);
1774 if((!fsp->is_directory) || (conn != fsp->conn)) {
1775 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1776 return;
1779 if (fsp->notify == NULL) {
1781 status = change_notify_create(fsp, filter, recursive);
1783 if (!NT_STATUS_IS_OK(status)) {
1784 DEBUG(10, ("change_notify_create returned %s\n",
1785 nt_errstr(status)));
1786 reply_nterror(req, status);
1787 return;
1791 if (fsp->notify->num_changes != 0) {
1794 * We've got changes pending, respond immediately
1798 * TODO: write a torture test to check the filtering behaviour
1799 * here.
1802 change_notify_reply(req,
1803 NT_STATUS_OK,
1804 max_param_count,
1805 fsp->notify,
1806 smbd_smb1_notify_reply);
1809 * change_notify_reply() above has independently sent its
1810 * results
1812 return;
1816 * No changes pending, queue the request
1819 status = change_notify_add_request(req,
1820 max_param_count,
1821 filter,
1822 recursive, fsp,
1823 smbd_smb1_notify_reply);
1824 if (!NT_STATUS_IS_OK(status)) {
1825 reply_nterror(req, status);
1827 return;
1830 /****************************************************************************
1831 Reply to an NT transact rename command.
1832 ****************************************************************************/
1834 static void call_nt_transact_rename(connection_struct *conn,
1835 struct smb_request *req,
1836 uint16 **ppsetup, uint32 setup_count,
1837 char **ppparams, uint32 parameter_count,
1838 char **ppdata, uint32 data_count,
1839 uint32 max_data_count)
1841 char *params = *ppparams;
1842 char *new_name = NULL;
1843 files_struct *fsp = NULL;
1844 bool dest_has_wcard = False;
1845 NTSTATUS status;
1846 TALLOC_CTX *ctx = talloc_tos();
1848 if(parameter_count < 5) {
1849 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1850 return;
1853 fsp = file_fsp(req, SVAL(params, 0));
1854 if (!check_fsp(conn, req, fsp)) {
1855 return;
1857 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1858 parameter_count - 4,
1859 STR_TERMINATE, &status, &dest_has_wcard);
1860 if (!NT_STATUS_IS_OK(status)) {
1861 reply_nterror(req, status);
1862 return;
1866 * W2K3 ignores this request as the RAW-RENAME test
1867 * demonstrates, so we do.
1869 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1871 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1872 fsp_str_dbg(fsp), new_name));
1874 return;
1877 /******************************************************************************
1878 Fake up a completely empty SD.
1879 *******************************************************************************/
1881 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1883 size_t sd_size;
1885 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1886 if(!*ppsd) {
1887 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1888 return NT_STATUS_NO_MEMORY;
1891 return NT_STATUS_OK;
1894 /****************************************************************************
1895 Reply to query a security descriptor.
1896 Callable from SMB2 and SMB2.
1897 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1898 the required size.
1899 ****************************************************************************/
1901 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1902 TALLOC_CTX *mem_ctx,
1903 files_struct *fsp,
1904 uint32_t security_info_wanted,
1905 uint32_t max_data_count,
1906 uint8_t **ppmarshalled_sd,
1907 size_t *psd_size)
1909 NTSTATUS status;
1910 struct security_descriptor *psd = NULL;
1913 * Get the permissions to return.
1916 if ((security_info_wanted & SECINFO_SACL) &&
1917 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1918 return NT_STATUS_ACCESS_DENIED;
1921 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1922 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1923 return NT_STATUS_ACCESS_DENIED;
1926 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1927 SECINFO_GROUP|SECINFO_SACL)) {
1928 /* Don't return SECINFO_LABEL if anything else was
1929 requested. See bug #8458. */
1930 security_info_wanted &= ~SECINFO_LABEL;
1933 if (!lp_nt_acl_support(SNUM(conn))) {
1934 status = get_null_nt_acl(mem_ctx, &psd);
1935 } else if (security_info_wanted & SECINFO_LABEL) {
1936 /* Like W2K3 return a null object. */
1937 status = get_null_nt_acl(mem_ctx, &psd);
1938 } else {
1939 status = SMB_VFS_FGET_NT_ACL(
1940 fsp, security_info_wanted, &psd);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 return status;
1946 if (!(security_info_wanted & SECINFO_OWNER)) {
1947 psd->owner_sid = NULL;
1949 if (!(security_info_wanted & SECINFO_GROUP)) {
1950 psd->group_sid = NULL;
1952 if (!(security_info_wanted & SECINFO_DACL)) {
1953 psd->type &= ~SEC_DESC_DACL_PRESENT;
1954 psd->dacl = NULL;
1956 if (!(security_info_wanted & SECINFO_SACL)) {
1957 psd->type &= ~SEC_DESC_SACL_PRESENT;
1958 psd->sacl = NULL;
1961 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1962 * present in the reply to match Windows behavior */
1963 if (psd->sacl == NULL &&
1964 security_info_wanted & SECINFO_SACL)
1965 psd->type |= SEC_DESC_SACL_PRESENT;
1966 if (psd->dacl == NULL &&
1967 security_info_wanted & SECINFO_DACL)
1968 psd->type |= SEC_DESC_DACL_PRESENT;
1970 if (security_info_wanted & SECINFO_LABEL) {
1971 /* Like W2K3 return a null object. */
1972 psd->owner_sid = NULL;
1973 psd->group_sid = NULL;
1974 psd->dacl = NULL;
1975 psd->sacl = NULL;
1976 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1979 *psd_size = ndr_size_security_descriptor(psd, 0);
1981 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1982 (unsigned long)*psd_size));
1984 if (DEBUGLEVEL >= 10) {
1985 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1986 fsp_str_dbg(fsp)));
1987 NDR_PRINT_DEBUG(security_descriptor, psd);
1990 if (max_data_count < *psd_size) {
1991 TALLOC_FREE(psd);
1992 return NT_STATUS_BUFFER_TOO_SMALL;
1995 status = marshall_sec_desc(mem_ctx, psd,
1996 ppmarshalled_sd, psd_size);
1998 if (!NT_STATUS_IS_OK(status)) {
1999 TALLOC_FREE(psd);
2000 return status;
2003 TALLOC_FREE(psd);
2004 return NT_STATUS_OK;
2007 /****************************************************************************
2008 SMB1 reply to query a security descriptor.
2009 ****************************************************************************/
2011 static void call_nt_transact_query_security_desc(connection_struct *conn,
2012 struct smb_request *req,
2013 uint16 **ppsetup,
2014 uint32 setup_count,
2015 char **ppparams,
2016 uint32 parameter_count,
2017 char **ppdata,
2018 uint32 data_count,
2019 uint32 max_data_count)
2021 char *params = *ppparams;
2022 char *data = *ppdata;
2023 size_t sd_size = 0;
2024 uint32 security_info_wanted;
2025 files_struct *fsp = NULL;
2026 NTSTATUS status;
2027 uint8_t *marshalled_sd = NULL;
2029 if(parameter_count < 8) {
2030 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2031 return;
2034 fsp = file_fsp(req, SVAL(params,0));
2035 if(!fsp) {
2036 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2037 return;
2040 security_info_wanted = IVAL(params,4);
2042 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
2043 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
2044 (unsigned int)security_info_wanted));
2046 params = nttrans_realloc(ppparams, 4);
2047 if(params == NULL) {
2048 reply_nterror(req, NT_STATUS_NO_MEMORY);
2049 return;
2053 * Get the permissions to return.
2056 status = smbd_do_query_security_desc(conn,
2057 talloc_tos(),
2058 fsp,
2059 security_info_wanted,
2060 max_data_count,
2061 &marshalled_sd,
2062 &sd_size);
2064 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2065 SIVAL(params,0,(uint32_t)sd_size);
2066 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2067 params, 4, NULL, 0);
2068 return;
2071 if (!NT_STATUS_IS_OK(status)) {
2072 reply_nterror(req, status);
2073 return;
2076 SMB_ASSERT(sd_size > 0);
2078 SIVAL(params,0,(uint32_t)sd_size);
2080 if (max_data_count < sd_size) {
2081 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2082 params, 4, NULL, 0);
2083 return;
2087 * Allocate the data we will return.
2090 data = nttrans_realloc(ppdata, sd_size);
2091 if(data == NULL) {
2092 reply_nterror(req, NT_STATUS_NO_MEMORY);
2093 return;
2096 memcpy(data, marshalled_sd, sd_size);
2098 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2100 return;
2103 /****************************************************************************
2104 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2105 ****************************************************************************/
2107 static void call_nt_transact_set_security_desc(connection_struct *conn,
2108 struct smb_request *req,
2109 uint16 **ppsetup,
2110 uint32 setup_count,
2111 char **ppparams,
2112 uint32 parameter_count,
2113 char **ppdata,
2114 uint32 data_count,
2115 uint32 max_data_count)
2117 char *params= *ppparams;
2118 char *data = *ppdata;
2119 files_struct *fsp = NULL;
2120 uint32 security_info_sent = 0;
2121 NTSTATUS status;
2123 if(parameter_count < 8) {
2124 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2125 return;
2128 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2129 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2130 return;
2133 if (!CAN_WRITE(fsp->conn)) {
2134 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2135 return;
2138 if(!lp_nt_acl_support(SNUM(conn))) {
2139 goto done;
2142 security_info_sent = IVAL(params,4);
2144 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2145 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2147 if (data_count == 0) {
2148 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2149 return;
2152 status = set_sd_blob(fsp, (uint8 *)data, data_count, security_info_sent);
2154 if (!NT_STATUS_IS_OK(status)) {
2155 reply_nterror(req, status);
2156 return;
2159 done:
2160 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2161 return;
2165 * Implement the default fsctl operation.
2168 static bool vfswrap_logged_ioctl_message = false;
2171 * In 3.6 we do not have a SMB_VFS_FSCTL() function
2172 * it's just faked to make it more look like
2173 * master (4.0)
2175 NTSTATUS smb_fsctl(struct files_struct *fsp,
2176 TALLOC_CTX *ctx,
2177 uint32_t function,
2178 uint16_t req_flags, /* Needed for UNICODE ... */
2179 const uint8_t *_in_data,
2180 uint32_t in_len,
2181 uint8_t **_out_data,
2182 uint32_t max_out_len,
2183 uint32_t *out_len)
2185 const char *in_data = (const char *)_in_data;
2186 char **out_data = (char **)_out_data;
2188 switch (function) {
2189 case FSCTL_SET_SPARSE:
2191 bool set_sparse = true;
2192 NTSTATUS status;
2194 if (in_len >= 1 && in_data[0] == 0) {
2195 set_sparse = false;
2198 status = file_set_sparse(fsp->conn, fsp, set_sparse);
2200 DEBUG(NT_STATUS_IS_OK(status) ? 10 : 9,
2201 ("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2202 smb_fname_str_dbg(fsp->fsp_name), set_sparse,
2203 nt_errstr(status)));
2205 return status;
2208 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2210 unsigned char objid[16];
2211 char *return_data = NULL;
2213 /* This should return the object-id on this file.
2214 * I think I'll make this be the inode+dev. JRA.
2217 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fsp->fnum));
2219 *out_len = (max_out_len >= 64) ? 64 : max_out_len;
2220 /* Hmmm, will this cause problems if less data asked for? */
2221 return_data = talloc_array(ctx, char, 64);
2222 if (return_data == NULL) {
2223 return NT_STATUS_NO_MEMORY;
2226 /* For backwards compatibility only store the dev/inode. */
2227 push_file_id_16(return_data, &fsp->file_id);
2228 memcpy(return_data+16,create_volume_objectid(fsp->conn,objid),16);
2229 push_file_id_16(return_data+32, &fsp->file_id);
2230 *out_data = return_data;
2231 return NT_STATUS_OK;
2234 case FSCTL_GET_REPARSE_POINT:
2236 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
2237 DEBUG(10, ("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X] Status: NOT_IMPLEMENTED\n", fsp->fnum));
2238 return NT_STATUS_NOT_A_REPARSE_POINT;
2241 case FSCTL_SET_REPARSE_POINT:
2243 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
2244 DEBUG(10, ("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X] Status: NOT_IMPLEMENTED\n", fsp->fnum));
2245 return NT_STATUS_NOT_A_REPARSE_POINT;
2248 case FSCTL_GET_SHADOW_COPY_DATA:
2251 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2252 * and return their volume names. If max_data_count is 16, then it is just
2253 * asking for the number of volumes and length of the combined names.
2255 * pdata is the data allocated by our caller, but that uses
2256 * total_data_count (which is 0 in our case) rather than max_data_count.
2257 * Allocate the correct amount and return the pointer to let
2258 * it be deallocated when we return.
2260 struct shadow_copy_data *shadow_data = NULL;
2261 bool labels = False;
2262 uint32 labels_data_count = 0;
2263 uint32 i;
2264 char *cur_pdata = NULL;
2266 if (max_out_len < 16) {
2267 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2268 max_out_len));
2269 return NT_STATUS_INVALID_PARAMETER;
2272 if (max_out_len > 16) {
2273 labels = True;
2276 shadow_data = talloc_zero(ctx, struct shadow_copy_data);
2277 if (shadow_data == NULL) {
2278 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2279 return NT_STATUS_NO_MEMORY;
2283 * Call the VFS routine to actually do the work.
2285 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2286 TALLOC_FREE(shadow_data);
2287 if (errno == ENOSYS) {
2288 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2289 fsp->conn->connectpath));
2290 return NT_STATUS_NOT_SUPPORTED;
2291 } else {
2292 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2293 fsp->conn->connectpath));
2294 return NT_STATUS_UNSUCCESSFUL;
2298 labels_data_count = (shadow_data->num_volumes * 2 *
2299 sizeof(SHADOW_COPY_LABEL)) + 2;
2301 if (!labels) {
2302 *out_len = 16;
2303 } else {
2304 *out_len = 12 + labels_data_count + 4;
2307 if (max_out_len < *out_len) {
2308 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2309 max_out_len, *out_len));
2310 TALLOC_FREE(shadow_data);
2311 return NT_STATUS_BUFFER_TOO_SMALL;
2314 cur_pdata = talloc_array(ctx, char, *out_len);
2315 if (cur_pdata == NULL) {
2316 TALLOC_FREE(shadow_data);
2317 return NT_STATUS_NO_MEMORY;
2320 *out_data = cur_pdata;
2322 /* num_volumes 4 bytes */
2323 SIVAL(cur_pdata, 0, shadow_data->num_volumes);
2325 if (labels) {
2326 /* num_labels 4 bytes */
2327 SIVAL(cur_pdata, 4, shadow_data->num_volumes);
2330 /* needed_data_count 4 bytes */
2331 SIVAL(cur_pdata, 8, labels_data_count + 4);
2333 cur_pdata += 12;
2335 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2336 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2337 if (labels && shadow_data->labels) {
2338 for (i=0; i<shadow_data->num_volumes; i++) {
2339 srvstr_push(cur_pdata, req_flags,
2340 cur_pdata, shadow_data->labels[i],
2341 2 * sizeof(SHADOW_COPY_LABEL),
2342 STR_UNICODE|STR_TERMINATE);
2343 cur_pdata += 2 * sizeof(SHADOW_COPY_LABEL);
2344 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2348 TALLOC_FREE(shadow_data);
2350 return NT_STATUS_OK;
2353 case FSCTL_FIND_FILES_BY_SID:
2355 /* pretend this succeeded -
2357 * we have to send back a list with all files owned by this SID
2359 * but I have to check that --metze
2361 struct dom_sid sid;
2362 uid_t uid;
2363 size_t sid_len;
2365 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n", fsp->fnum));
2367 if (in_len < 8) {
2368 /* NT_STATUS_BUFFER_TOO_SMALL maybe? */
2369 return NT_STATUS_INVALID_PARAMETER;
2372 sid_len = MIN(in_len - 4,SID_MAX_SIZE);
2374 /* unknown 4 bytes: this is not the length of the sid :-( */
2375 /*unknown = IVAL(pdata,0);*/
2377 if (!sid_parse(in_data + 4, sid_len, &sid)) {
2378 return NT_STATUS_INVALID_PARAMETER;
2380 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2382 if (!sid_to_uid(&sid, &uid)) {
2383 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2384 sid_string_dbg(&sid),
2385 (unsigned long)sid_len));
2386 uid = (-1);
2389 /* we can take a look at the find source :-)
2391 * find ./ -uid $uid -name '*' is what we need here
2394 * and send 4bytes len and then NULL terminated unicode strings
2395 * for each file
2397 * but I don't know how to deal with the paged results
2398 * (maybe we can hang the result anywhere in the fsp struct)
2400 * but I don't know how to deal with the paged results
2401 * (maybe we can hang the result anywhere in the fsp struct)
2403 * we don't send all files at once
2404 * and at the next we should *not* start from the beginning,
2405 * so we have to cache the result
2407 * --metze
2410 /* this works for now... */
2411 return NT_STATUS_OK;
2414 case FSCTL_QUERY_ALLOCATED_RANGES:
2416 /* FIXME: This is just a dummy reply, telling that all of the
2417 * file is allocated. MKS cp needs that.
2418 * Adding the real allocated ranges via FIEMAP on Linux
2419 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2420 * this FSCTL correct for sparse files.
2422 NTSTATUS status;
2423 uint64_t offset, length;
2424 char *out_data_tmp = NULL;
2426 if (in_len != 16) {
2427 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2428 in_len));
2429 return NT_STATUS_INVALID_PARAMETER;
2432 if (max_out_len < 16) {
2433 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_out_len (%u) < 16 is invalid!\n",
2434 max_out_len));
2435 return NT_STATUS_INVALID_PARAMETER;
2438 offset = BVAL(in_data,0);
2439 length = BVAL(in_data,8);
2441 if (offset + length < offset) {
2442 /* No 64-bit integer wrap. */
2443 return NT_STATUS_INVALID_PARAMETER;
2446 /* Shouldn't this be SMB_VFS_STAT ... ? */
2447 status = vfs_stat_fsp(fsp);
2448 if (!NT_STATUS_IS_OK(status)) {
2449 return status;
2452 *out_len = 16;
2453 out_data_tmp = talloc_array(ctx, char, *out_len);
2454 if (out_data_tmp == NULL) {
2455 DEBUG(10, ("unable to allocate memory for response\n"));
2456 return NT_STATUS_NO_MEMORY;
2459 if (offset > fsp->fsp_name->st.st_ex_size ||
2460 fsp->fsp_name->st.st_ex_size == 0 ||
2461 length == 0) {
2462 memset(out_data_tmp, 0, *out_len);
2463 } else {
2464 uint64_t end = offset + length;
2465 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2466 SBVAL(out_data_tmp, 0, 0);
2467 SBVAL(out_data_tmp, 8, end);
2470 *out_data = out_data_tmp;
2472 return NT_STATUS_OK;
2475 case FSCTL_IS_VOLUME_DIRTY:
2477 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2478 "(but not implemented)\n", fsp->fnum));
2480 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2481 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2483 return NT_STATUS_INVALID_PARAMETER;
2486 default:
2488 * Only print once ... unfortunately there could be lots of
2489 * different FSCTLs that are called.
2491 if (!vfswrap_logged_ioctl_message) {
2492 vfswrap_logged_ioctl_message = true;
2493 DEBUG(2, ("%s (0x%x): Currently not implemented.\n",
2494 __FUNCTION__, function));
2498 return NT_STATUS_NOT_SUPPORTED;
2501 /****************************************************************************
2502 Reply to NT IOCTL
2503 ****************************************************************************/
2505 static void call_nt_transact_ioctl(connection_struct *conn,
2506 struct smb_request *req,
2507 uint16 **ppsetup, uint32 setup_count,
2508 char **ppparams, uint32 parameter_count,
2509 char **ppdata, uint32 data_count,
2510 uint32 max_data_count)
2512 NTSTATUS status;
2513 uint32 function;
2514 uint16 fidnum;
2515 files_struct *fsp;
2516 uint8 isFSctl;
2517 uint8 compfilter;
2518 char *out_data = NULL;
2519 uint32 out_data_len = 0;
2520 char *pdata = *ppdata;
2521 TALLOC_CTX *ctx = talloc_tos();
2523 if (setup_count != 8) {
2524 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2525 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2526 return;
2529 function = IVAL(*ppsetup, 0);
2530 fidnum = SVAL(*ppsetup, 4);
2531 isFSctl = CVAL(*ppsetup, 6);
2532 compfilter = CVAL(*ppsetup, 7);
2534 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2535 function, fidnum, isFSctl, compfilter));
2537 fsp=file_fsp(req, fidnum);
2540 * We don't really implement IOCTLs, especially on files.
2542 if (!isFSctl) {
2543 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2544 isFSctl));
2545 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2546 return;
2549 /* Has to be for an open file! */
2550 if (!check_fsp_open(conn, req, fsp)) {
2551 return;
2555 * out_data might be allocated by the VFS module, but talloc should be
2556 * used, and should be cleaned up when the request ends.
2558 status = smb_fsctl(fsp,
2559 ctx,
2560 function,
2561 req->flags2,
2562 (uint8_t *)pdata,
2563 data_count,
2564 (uint8_t **)&out_data,
2565 max_data_count,
2566 &out_data_len);
2567 if (!NT_STATUS_IS_OK(status)) {
2568 reply_nterror(req, status);
2569 } else {
2570 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2575 #ifdef HAVE_SYS_QUOTAS
2576 /****************************************************************************
2577 Reply to get user quota
2578 ****************************************************************************/
2580 static void call_nt_transact_get_user_quota(connection_struct *conn,
2581 struct smb_request *req,
2582 uint16 **ppsetup,
2583 uint32 setup_count,
2584 char **ppparams,
2585 uint32 parameter_count,
2586 char **ppdata,
2587 uint32 data_count,
2588 uint32 max_data_count)
2590 NTSTATUS nt_status = NT_STATUS_OK;
2591 char *params = *ppparams;
2592 char *pdata = *ppdata;
2593 char *entry;
2594 int data_len=0,param_len=0;
2595 int qt_len=0;
2596 int entry_len = 0;
2597 files_struct *fsp = NULL;
2598 uint16 level = 0;
2599 size_t sid_len;
2600 struct dom_sid sid;
2601 bool start_enum = True;
2602 SMB_NTQUOTA_STRUCT qt;
2603 SMB_NTQUOTA_LIST *tmp_list;
2604 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2606 ZERO_STRUCT(qt);
2608 /* access check */
2609 if (get_current_uid(conn) != 0) {
2610 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2611 "[%s]\n", lp_servicename(SNUM(conn)),
2612 conn->session_info->unix_name));
2613 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2614 return;
2618 * Ensure minimum number of parameters sent.
2621 if (parameter_count < 4) {
2622 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2623 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2624 return;
2627 /* maybe we can check the quota_fnum */
2628 fsp = file_fsp(req, SVAL(params,0));
2629 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2630 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2631 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2632 return;
2635 /* the NULL pointer checking for fsp->fake_file_handle->pd
2636 * is done by CHECK_NTQUOTA_HANDLE_OK()
2638 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2640 level = SVAL(params,2);
2642 /* unknown 12 bytes leading in params */
2644 switch (level) {
2645 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2646 /* seems that we should continue with the enum here --metze */
2648 if (qt_handle->quota_list!=NULL &&
2649 qt_handle->tmp_list==NULL) {
2651 /* free the list */
2652 free_ntquota_list(&(qt_handle->quota_list));
2654 /* Realloc the size of parameters and data we will return */
2655 param_len = 4;
2656 params = nttrans_realloc(ppparams, param_len);
2657 if(params == NULL) {
2658 reply_nterror(req, NT_STATUS_NO_MEMORY);
2659 return;
2662 data_len = 0;
2663 SIVAL(params,0,data_len);
2665 break;
2668 start_enum = False;
2670 case TRANSACT_GET_USER_QUOTA_LIST_START:
2672 if (qt_handle->quota_list==NULL &&
2673 qt_handle->tmp_list==NULL) {
2674 start_enum = True;
2677 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2678 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2679 return;
2682 /* Realloc the size of parameters and data we will return */
2683 param_len = 4;
2684 params = nttrans_realloc(ppparams, param_len);
2685 if(params == NULL) {
2686 reply_nterror(req, NT_STATUS_NO_MEMORY);
2687 return;
2690 /* we should not trust the value in max_data_count*/
2691 max_data_count = MIN(max_data_count,2048);
2693 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2694 if(pdata == NULL) {
2695 reply_nterror(req, NT_STATUS_NO_MEMORY);
2696 return;
2699 entry = pdata;
2701 /* set params Size of returned Quota Data 4 bytes*/
2702 /* but set it later when we know it */
2704 /* for each entry push the data */
2706 if (start_enum) {
2707 qt_handle->tmp_list = qt_handle->quota_list;
2710 tmp_list = qt_handle->tmp_list;
2712 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2713 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2715 sid_len = ndr_size_dom_sid(
2716 &tmp_list->quotas->sid, 0);
2717 entry_len = 40 + sid_len;
2719 /* nextoffset entry 4 bytes */
2720 SIVAL(entry,0,entry_len);
2722 /* then the len of the SID 4 bytes */
2723 SIVAL(entry,4,sid_len);
2725 /* unknown data 8 bytes uint64_t */
2726 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2728 /* the used disk space 8 bytes uint64_t */
2729 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2731 /* the soft quotas 8 bytes uint64_t */
2732 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2734 /* the hard quotas 8 bytes uint64_t */
2735 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2737 /* and now the SID */
2738 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2741 qt_handle->tmp_list = tmp_list;
2743 /* overwrite the offset of the last entry */
2744 SIVAL(entry-entry_len,0,0);
2746 data_len = 4+qt_len;
2747 /* overwrite the params quota_data_len */
2748 SIVAL(params,0,data_len);
2750 break;
2752 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2754 /* unknown 4 bytes IVAL(pdata,0) */
2756 if (data_count < 8) {
2757 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2758 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2759 return;
2762 sid_len = IVAL(pdata,4);
2763 /* Ensure this is less than 1mb. */
2764 if (sid_len > (1024*1024)) {
2765 reply_nterror(req, NT_STATUS_NO_MEMORY);
2766 return;
2769 if (data_count < 8+sid_len) {
2770 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2771 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2772 return;
2775 data_len = 4+40+sid_len;
2777 if (max_data_count < data_len) {
2778 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2779 max_data_count, data_len));
2780 param_len = 4;
2781 SIVAL(params,0,data_len);
2782 data_len = 0;
2783 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2784 break;
2787 if (!sid_parse(pdata+8,sid_len,&sid)) {
2788 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2789 return;
2792 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2793 ZERO_STRUCT(qt);
2795 * we have to return zero's in all fields
2796 * instead of returning an error here
2797 * --metze
2801 /* Realloc the size of parameters and data we will return */
2802 param_len = 4;
2803 params = nttrans_realloc(ppparams, param_len);
2804 if(params == NULL) {
2805 reply_nterror(req, NT_STATUS_NO_MEMORY);
2806 return;
2809 pdata = nttrans_realloc(ppdata, data_len);
2810 if(pdata == NULL) {
2811 reply_nterror(req, NT_STATUS_NO_MEMORY);
2812 return;
2815 entry = pdata;
2817 /* set params Size of returned Quota Data 4 bytes*/
2818 SIVAL(params,0,data_len);
2820 /* nextoffset entry 4 bytes */
2821 SIVAL(entry,0,0);
2823 /* then the len of the SID 4 bytes */
2824 SIVAL(entry,4,sid_len);
2826 /* unknown data 8 bytes uint64_t */
2827 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2829 /* the used disk space 8 bytes uint64_t */
2830 SBIG_UINT(entry,16,qt.usedspace);
2832 /* the soft quotas 8 bytes uint64_t */
2833 SBIG_UINT(entry,24,qt.softlim);
2835 /* the hard quotas 8 bytes uint64_t */
2836 SBIG_UINT(entry,32,qt.hardlim);
2838 /* and now the SID */
2839 sid_linearize(entry+40, sid_len, &sid);
2841 break;
2843 default:
2844 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2845 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2846 return;
2847 break;
2850 send_nt_replies(conn, req, nt_status, params, param_len,
2851 pdata, data_len);
2854 /****************************************************************************
2855 Reply to set user quota
2856 ****************************************************************************/
2858 static void call_nt_transact_set_user_quota(connection_struct *conn,
2859 struct smb_request *req,
2860 uint16 **ppsetup,
2861 uint32 setup_count,
2862 char **ppparams,
2863 uint32 parameter_count,
2864 char **ppdata,
2865 uint32 data_count,
2866 uint32 max_data_count)
2868 char *params = *ppparams;
2869 char *pdata = *ppdata;
2870 int data_len=0,param_len=0;
2871 SMB_NTQUOTA_STRUCT qt;
2872 size_t sid_len;
2873 struct dom_sid sid;
2874 files_struct *fsp = NULL;
2876 ZERO_STRUCT(qt);
2878 /* access check */
2879 if (get_current_uid(conn) != 0) {
2880 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2881 "[%s]\n", lp_servicename(SNUM(conn)),
2882 conn->session_info->unix_name));
2883 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2884 return;
2888 * Ensure minimum number of parameters sent.
2891 if (parameter_count < 2) {
2892 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2893 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2894 return;
2897 /* maybe we can check the quota_fnum */
2898 fsp = file_fsp(req, SVAL(params,0));
2899 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2900 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2901 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2902 return;
2905 if (data_count < 40) {
2906 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2907 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2908 return;
2911 /* offset to next quota record.
2912 * 4 bytes IVAL(pdata,0)
2913 * unused here...
2916 /* sid len */
2917 sid_len = IVAL(pdata,4);
2919 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2920 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2921 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2922 return;
2925 /* unknown 8 bytes in pdata
2926 * maybe its the change time in NTTIME
2929 /* the used space 8 bytes (uint64_t)*/
2930 qt.usedspace = BVAL(pdata,16);
2932 /* the soft quotas 8 bytes (uint64_t)*/
2933 qt.softlim = BVAL(pdata,24);
2935 /* the hard quotas 8 bytes (uint64_t)*/
2936 qt.hardlim = BVAL(pdata,32);
2938 if (!sid_parse(pdata+40,sid_len,&sid)) {
2939 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2940 return;
2943 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2945 /* 44 unknown bytes left... */
2947 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2948 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2949 return;
2952 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2953 pdata, data_len);
2955 #endif /* HAVE_SYS_QUOTAS */
2957 static void handle_nttrans(connection_struct *conn,
2958 struct trans_state *state,
2959 struct smb_request *req)
2961 if (get_Protocol() >= PROTOCOL_NT1) {
2962 req->flags2 |= 0x40; /* IS_LONG_NAME */
2963 SSVAL(req->inbuf,smb_flg2,req->flags2);
2967 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2969 /* Now we must call the relevant NT_TRANS function */
2970 switch(state->call) {
2971 case NT_TRANSACT_CREATE:
2973 START_PROFILE(NT_transact_create);
2974 call_nt_transact_create(
2975 conn, req,
2976 &state->setup, state->setup_count,
2977 &state->param, state->total_param,
2978 &state->data, state->total_data,
2979 state->max_data_return);
2980 END_PROFILE(NT_transact_create);
2981 break;
2984 case NT_TRANSACT_IOCTL:
2986 START_PROFILE(NT_transact_ioctl);
2987 call_nt_transact_ioctl(
2988 conn, req,
2989 &state->setup, state->setup_count,
2990 &state->param, state->total_param,
2991 &state->data, state->total_data,
2992 state->max_data_return);
2993 END_PROFILE(NT_transact_ioctl);
2994 break;
2997 case NT_TRANSACT_SET_SECURITY_DESC:
2999 START_PROFILE(NT_transact_set_security_desc);
3000 call_nt_transact_set_security_desc(
3001 conn, req,
3002 &state->setup, state->setup_count,
3003 &state->param, state->total_param,
3004 &state->data, state->total_data,
3005 state->max_data_return);
3006 END_PROFILE(NT_transact_set_security_desc);
3007 break;
3010 case NT_TRANSACT_NOTIFY_CHANGE:
3012 START_PROFILE(NT_transact_notify_change);
3013 call_nt_transact_notify_change(
3014 conn, req,
3015 &state->setup, state->setup_count,
3016 &state->param, state->total_param,
3017 &state->data, state->total_data,
3018 state->max_data_return,
3019 state->max_param_return);
3020 END_PROFILE(NT_transact_notify_change);
3021 break;
3024 case NT_TRANSACT_RENAME:
3026 START_PROFILE(NT_transact_rename);
3027 call_nt_transact_rename(
3028 conn, req,
3029 &state->setup, state->setup_count,
3030 &state->param, state->total_param,
3031 &state->data, state->total_data,
3032 state->max_data_return);
3033 END_PROFILE(NT_transact_rename);
3034 break;
3037 case NT_TRANSACT_QUERY_SECURITY_DESC:
3039 START_PROFILE(NT_transact_query_security_desc);
3040 call_nt_transact_query_security_desc(
3041 conn, req,
3042 &state->setup, state->setup_count,
3043 &state->param, state->total_param,
3044 &state->data, state->total_data,
3045 state->max_data_return);
3046 END_PROFILE(NT_transact_query_security_desc);
3047 break;
3050 #ifdef HAVE_SYS_QUOTAS
3051 case NT_TRANSACT_GET_USER_QUOTA:
3053 START_PROFILE(NT_transact_get_user_quota);
3054 call_nt_transact_get_user_quota(
3055 conn, req,
3056 &state->setup, state->setup_count,
3057 &state->param, state->total_param,
3058 &state->data, state->total_data,
3059 state->max_data_return);
3060 END_PROFILE(NT_transact_get_user_quota);
3061 break;
3064 case NT_TRANSACT_SET_USER_QUOTA:
3066 START_PROFILE(NT_transact_set_user_quota);
3067 call_nt_transact_set_user_quota(
3068 conn, req,
3069 &state->setup, state->setup_count,
3070 &state->param, state->total_param,
3071 &state->data, state->total_data,
3072 state->max_data_return);
3073 END_PROFILE(NT_transact_set_user_quota);
3074 break;
3076 #endif /* HAVE_SYS_QUOTAS */
3078 default:
3079 /* Error in request */
3080 DEBUG(0,("handle_nttrans: Unknown request %d in "
3081 "nttrans call\n", state->call));
3082 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3083 return;
3085 return;
3088 /****************************************************************************
3089 Reply to a SMBNTtrans.
3090 ****************************************************************************/
3092 void reply_nttrans(struct smb_request *req)
3094 connection_struct *conn = req->conn;
3095 uint32_t pscnt;
3096 uint32_t psoff;
3097 uint32_t dscnt;
3098 uint32_t dsoff;
3099 uint16 function_code;
3100 NTSTATUS result;
3101 struct trans_state *state;
3103 START_PROFILE(SMBnttrans);
3105 if (req->wct < 19) {
3106 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3107 END_PROFILE(SMBnttrans);
3108 return;
3111 pscnt = IVAL(req->vwv+9, 1);
3112 psoff = IVAL(req->vwv+11, 1);
3113 dscnt = IVAL(req->vwv+13, 1);
3114 dsoff = IVAL(req->vwv+15, 1);
3115 function_code = SVAL(req->vwv+18, 0);
3117 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3118 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3119 END_PROFILE(SMBnttrans);
3120 return;
3123 result = allow_new_trans(conn->pending_trans, req->mid);
3124 if (!NT_STATUS_IS_OK(result)) {
3125 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3126 reply_nterror(req, result);
3127 END_PROFILE(SMBnttrans);
3128 return;
3131 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3132 reply_nterror(req, NT_STATUS_NO_MEMORY);
3133 END_PROFILE(SMBnttrans);
3134 return;
3137 state->cmd = SMBnttrans;
3139 state->mid = req->mid;
3140 state->vuid = req->vuid;
3141 state->total_data = IVAL(req->vwv+3, 1);
3142 state->data = NULL;
3143 state->total_param = IVAL(req->vwv+1, 1);
3144 state->param = NULL;
3145 state->max_data_return = IVAL(req->vwv+7, 1);
3146 state->max_param_return = IVAL(req->vwv+5, 1);
3148 /* setup count is in *words* */
3149 state->setup_count = 2*CVAL(req->vwv+17, 1);
3150 state->setup = NULL;
3151 state->call = function_code;
3153 DEBUG(10, ("num_setup=%u, "
3154 "param_total=%u, this_param=%u, max_param=%u, "
3155 "data_total=%u, this_data=%u, max_data=%u, "
3156 "param_offset=%u, data_offset=%u\n",
3157 (unsigned)state->setup_count,
3158 (unsigned)state->total_param, (unsigned)pscnt,
3159 (unsigned)state->max_param_return,
3160 (unsigned)state->total_data, (unsigned)dscnt,
3161 (unsigned)state->max_data_return,
3162 (unsigned)psoff, (unsigned)dsoff));
3165 * All nttrans messages we handle have smb_wct == 19 +
3166 * state->setup_count. Ensure this is so as a sanity check.
3169 if(req->wct != 19 + (state->setup_count/2)) {
3170 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3171 req->wct, 19 + (state->setup_count/2)));
3172 goto bad_param;
3175 /* Don't allow more than 128mb for each value. */
3176 if ((state->total_data > (1024*1024*128)) ||
3177 (state->total_param > (1024*1024*128))) {
3178 reply_nterror(req, NT_STATUS_NO_MEMORY);
3179 END_PROFILE(SMBnttrans);
3180 return;
3183 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3184 goto bad_param;
3186 if (state->total_data) {
3188 if (trans_oob(state->total_data, 0, dscnt)
3189 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3190 goto bad_param;
3193 /* Can't use talloc here, the core routines do realloc on the
3194 * params and data. */
3195 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3196 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3197 "bytes !\n", (unsigned int)state->total_data));
3198 TALLOC_FREE(state);
3199 reply_nterror(req, NT_STATUS_NO_MEMORY);
3200 END_PROFILE(SMBnttrans);
3201 return;
3204 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3207 if (state->total_param) {
3209 if (trans_oob(state->total_param, 0, pscnt)
3210 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3211 goto bad_param;
3214 /* Can't use talloc here, the core routines do realloc on the
3215 * params and data. */
3216 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3217 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3218 "bytes !\n", (unsigned int)state->total_param));
3219 SAFE_FREE(state->data);
3220 TALLOC_FREE(state);
3221 reply_nterror(req, NT_STATUS_NO_MEMORY);
3222 END_PROFILE(SMBnttrans);
3223 return;
3226 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3229 state->received_data = dscnt;
3230 state->received_param = pscnt;
3232 if(state->setup_count > 0) {
3233 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3234 state->setup_count));
3237 * No overflow possible here, state->setup_count is an
3238 * unsigned int, being filled by a single byte from
3239 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3240 * below is not necessary, it's here to clarify things. The
3241 * validity of req->vwv and req->wct has been checked in
3242 * init_smb_request already.
3244 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3245 goto bad_param;
3248 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3249 if (state->setup == NULL) {
3250 DEBUG(0,("reply_nttrans : Out of memory\n"));
3251 SAFE_FREE(state->data);
3252 SAFE_FREE(state->param);
3253 TALLOC_FREE(state);
3254 reply_nterror(req, NT_STATUS_NO_MEMORY);
3255 END_PROFILE(SMBnttrans);
3256 return;
3259 memcpy(state->setup, req->vwv+19, state->setup_count);
3260 dump_data(10, (uint8 *)state->setup, state->setup_count);
3263 if ((state->received_data == state->total_data) &&
3264 (state->received_param == state->total_param)) {
3265 handle_nttrans(conn, state, req);
3266 SAFE_FREE(state->param);
3267 SAFE_FREE(state->data);
3268 TALLOC_FREE(state);
3269 END_PROFILE(SMBnttrans);
3270 return;
3273 DLIST_ADD(conn->pending_trans, state);
3275 /* We need to send an interim response then receive the rest
3276 of the parameter/data bytes */
3277 reply_outbuf(req, 0, 0);
3278 show_msg((char *)req->outbuf);
3279 END_PROFILE(SMBnttrans);
3280 return;
3282 bad_param:
3284 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3285 SAFE_FREE(state->data);
3286 SAFE_FREE(state->param);
3287 TALLOC_FREE(state);
3288 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3289 END_PROFILE(SMBnttrans);
3290 return;
3293 /****************************************************************************
3294 Reply to a SMBnttranss
3295 ****************************************************************************/
3297 void reply_nttranss(struct smb_request *req)
3299 connection_struct *conn = req->conn;
3300 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3301 struct trans_state *state;
3303 START_PROFILE(SMBnttranss);
3305 show_msg((char *)req->inbuf);
3307 /* Windows clients expect all replies to
3308 an NT transact secondary (SMBnttranss 0xA1)
3309 to have a command code of NT transact
3310 (SMBnttrans 0xA0). See bug #8989 for details. */
3311 req->cmd = SMBnttrans;
3313 if (req->wct < 18) {
3314 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3315 END_PROFILE(SMBnttranss);
3316 return;
3319 for (state = conn->pending_trans; state != NULL;
3320 state = state->next) {
3321 if (state->mid == req->mid) {
3322 break;
3326 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3327 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3328 END_PROFILE(SMBnttranss);
3329 return;
3332 /* Revise state->total_param and state->total_data in case they have
3333 changed downwards */
3334 if (IVAL(req->vwv+1, 1) < state->total_param) {
3335 state->total_param = IVAL(req->vwv+1, 1);
3337 if (IVAL(req->vwv+3, 1) < state->total_data) {
3338 state->total_data = IVAL(req->vwv+3, 1);
3341 pcnt = IVAL(req->vwv+5, 1);
3342 poff = IVAL(req->vwv+7, 1);
3343 pdisp = IVAL(req->vwv+9, 1);
3345 dcnt = IVAL(req->vwv+11, 1);
3346 doff = IVAL(req->vwv+13, 1);
3347 ddisp = IVAL(req->vwv+15, 1);
3349 state->received_param += pcnt;
3350 state->received_data += dcnt;
3352 if ((state->received_data > state->total_data) ||
3353 (state->received_param > state->total_param))
3354 goto bad_param;
3356 if (pcnt) {
3357 if (trans_oob(state->total_param, pdisp, pcnt)
3358 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3359 goto bad_param;
3361 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3364 if (dcnt) {
3365 if (trans_oob(state->total_data, ddisp, dcnt)
3366 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3367 goto bad_param;
3369 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3372 if ((state->received_param < state->total_param) ||
3373 (state->received_data < state->total_data)) {
3374 END_PROFILE(SMBnttranss);
3375 return;
3378 handle_nttrans(conn, state, req);
3380 DLIST_REMOVE(conn->pending_trans, state);
3381 SAFE_FREE(state->data);
3382 SAFE_FREE(state->param);
3383 TALLOC_FREE(state);
3384 END_PROFILE(SMBnttranss);
3385 return;
3387 bad_param:
3389 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3390 DLIST_REMOVE(conn->pending_trans, state);
3391 SAFE_FREE(state->data);
3392 SAFE_FREE(state->param);
3393 TALLOC_FREE(state);
3394 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3395 END_PROFILE(SMBnttranss);
3396 return;