Fix smbclient/tarmode panic on connecting to Windows 2000 clients.
[Samba.git] / source3 / smbd / nttrans.c
blobe87d132c6224dcb07594fdc358cc9db0b16da641
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 Internal fn to set security descriptors.
831 ****************************************************************************/
833 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
834 uint32_t security_info_sent)
836 struct security_descriptor *psd = NULL;
837 NTSTATUS status;
839 if (sd_len == 0) {
840 return NT_STATUS_INVALID_PARAMETER;
843 if (!CAN_WRITE(fsp->conn)) {
844 return NT_STATUS_ACCESS_DENIED;
847 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
848 return NT_STATUS_OK;
851 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
853 if (!NT_STATUS_IS_OK(status)) {
854 return status;
857 if (psd->owner_sid == NULL) {
858 security_info_sent &= ~SECINFO_OWNER;
860 if (psd->group_sid == NULL) {
861 security_info_sent &= ~SECINFO_GROUP;
864 /* Ensure we have at least one thing set. */
865 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
866 if (security_info_sent & SECINFO_LABEL) {
867 /* Only consider SECINFO_LABEL if no other
868 bits are set. Just like W2K3 we don't
869 store this. */
870 return NT_STATUS_OK;
872 return NT_STATUS_INVALID_PARAMETER;
875 /* Ensure we have the rights to do this. */
876 if (security_info_sent & SECINFO_OWNER) {
877 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
878 return NT_STATUS_ACCESS_DENIED;
882 if (security_info_sent & SECINFO_GROUP) {
883 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
884 return NT_STATUS_ACCESS_DENIED;
888 if (security_info_sent & SECINFO_DACL) {
889 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
890 return NT_STATUS_ACCESS_DENIED;
892 /* Convert all the generic bits. */
893 if (psd->dacl) {
894 security_acl_map_generic(psd->dacl, &file_generic_mapping);
898 if (security_info_sent & SECINFO_SACL) {
899 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
900 return NT_STATUS_ACCESS_DENIED;
902 /* Convert all the generic bits. */
903 if (psd->sacl) {
904 security_acl_map_generic(psd->sacl, &file_generic_mapping);
908 if (DEBUGLEVEL >= 10) {
909 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
910 NDR_PRINT_DEBUG(security_descriptor, psd);
913 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
915 TALLOC_FREE(psd);
917 return status;
920 /****************************************************************************
921 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
922 ****************************************************************************/
924 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
926 struct ea_list *ea_list_head = NULL;
927 size_t offset = 0;
929 if (data_size < 4) {
930 return NULL;
933 while (offset + 4 <= data_size) {
934 size_t next_offset = IVAL(pdata,offset);
935 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
937 if (!eal) {
938 return NULL;
941 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
942 if (next_offset == 0) {
943 break;
945 offset += next_offset;
948 return ea_list_head;
951 /****************************************************************************
952 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
953 ****************************************************************************/
955 static void call_nt_transact_create(connection_struct *conn,
956 struct smb_request *req,
957 uint16 **ppsetup, uint32 setup_count,
958 char **ppparams, uint32 parameter_count,
959 char **ppdata, uint32 data_count,
960 uint32 max_data_count)
962 struct smb_filename *smb_fname = NULL;
963 char *fname = NULL;
964 char *params = *ppparams;
965 char *data = *ppdata;
966 /* Breakout the oplock request bits so we can set the reply bits separately. */
967 uint32 fattr=0;
968 SMB_OFF_T file_len = 0;
969 int info = 0;
970 files_struct *fsp = NULL;
971 char *p = NULL;
972 uint32 flags;
973 uint32 access_mask;
974 uint32 file_attributes;
975 uint32 share_access;
976 uint32 create_disposition;
977 uint32 create_options;
978 uint32 sd_len;
979 struct security_descriptor *sd = NULL;
980 uint32 ea_len;
981 uint16 root_dir_fid;
982 struct timespec create_timespec;
983 struct timespec c_timespec;
984 struct timespec a_timespec;
985 struct timespec m_timespec;
986 struct timespec write_time_ts;
987 struct ea_list *ea_list = NULL;
988 NTSTATUS status;
989 size_t param_len;
990 uint64_t allocation_size;
991 int oplock_request;
992 uint8_t oplock_granted;
993 struct case_semantics_state *case_state = NULL;
994 TALLOC_CTX *ctx = talloc_tos();
996 DEBUG(5,("call_nt_transact_create\n"));
999 * If it's an IPC, use the pipe handler.
1002 if (IS_IPC(conn)) {
1003 if (lp_nt_pipe_support()) {
1004 do_nt_transact_create_pipe(
1005 conn, req,
1006 ppsetup, setup_count,
1007 ppparams, parameter_count,
1008 ppdata, data_count);
1009 goto out;
1011 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1012 goto out;
1016 * Ensure minimum number of parameters sent.
1019 if(parameter_count < 54) {
1020 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1021 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1022 goto out;
1025 flags = IVAL(params,0);
1026 access_mask = IVAL(params,8);
1027 file_attributes = IVAL(params,20);
1028 share_access = IVAL(params,24);
1029 create_disposition = IVAL(params,28);
1030 create_options = IVAL(params,32);
1031 sd_len = IVAL(params,36);
1032 ea_len = IVAL(params,40);
1033 root_dir_fid = (uint16)IVAL(params,4);
1034 allocation_size = BVAL(params,12);
1037 * we need to remove ignored bits when they come directly from the client
1038 * because we reuse some of them for internal stuff
1040 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1042 /* Ensure the data_len is correct for the sd and ea values given. */
1043 if ((ea_len + sd_len > data_count)
1044 || (ea_len > data_count) || (sd_len > data_count)
1045 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1046 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1047 "%u, data_count = %u\n", (unsigned int)ea_len,
1048 (unsigned int)sd_len, (unsigned int)data_count));
1049 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1050 goto out;
1053 if (sd_len) {
1054 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1055 sd_len));
1057 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1058 &sd);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 DEBUG(10, ("call_nt_transact_create: "
1061 "unmarshall_sec_desc failed: %s\n",
1062 nt_errstr(status)));
1063 reply_nterror(req, status);
1064 goto out;
1068 if (ea_len) {
1069 if (!lp_ea_support(SNUM(conn))) {
1070 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1071 "EA's not supported.\n",
1072 (unsigned int)ea_len));
1073 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1074 goto out;
1077 if (ea_len < 10) {
1078 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1079 "too small (should be more than 10)\n",
1080 (unsigned int)ea_len ));
1081 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1082 goto out;
1085 /* We have already checked that ea_len <= data_count here. */
1086 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1087 ea_len);
1088 if (ea_list == NULL) {
1089 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1090 goto out;
1094 srvstr_get_path(ctx, params, req->flags2, &fname,
1095 params+53, parameter_count-53,
1096 STR_TERMINATE, &status);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 reply_nterror(req, status);
1099 goto out;
1102 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1103 case_state = set_posix_case_semantics(ctx, conn);
1104 if (!case_state) {
1105 reply_nterror(req, NT_STATUS_NO_MEMORY);
1106 goto out;
1110 status = filename_convert(ctx,
1111 conn,
1112 req->flags2 & FLAGS2_DFS_PATHNAMES,
1113 fname,
1115 NULL,
1116 &smb_fname);
1118 TALLOC_FREE(case_state);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1122 reply_botherror(req,
1123 NT_STATUS_PATH_NOT_COVERED,
1124 ERRSRV, ERRbadpath);
1125 goto out;
1127 reply_nterror(req, status);
1128 goto out;
1131 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1132 if (oplock_request) {
1133 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1134 ? BATCH_OPLOCK : 0;
1138 * Bug #6898 - clients using Windows opens should
1139 * never be able to set this attribute into the
1140 * VFS.
1142 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1144 status = SMB_VFS_CREATE_FILE(
1145 conn, /* conn */
1146 req, /* req */
1147 root_dir_fid, /* root_dir_fid */
1148 smb_fname, /* fname */
1149 access_mask, /* access_mask */
1150 share_access, /* share_access */
1151 create_disposition, /* create_disposition*/
1152 create_options, /* create_options */
1153 file_attributes, /* file_attributes */
1154 oplock_request, /* oplock_request */
1155 allocation_size, /* allocation_size */
1156 0, /* private_flags */
1157 sd, /* sd */
1158 ea_list, /* ea_list */
1159 &fsp, /* result */
1160 &info); /* pinfo */
1162 if(!NT_STATUS_IS_OK(status)) {
1163 if (open_was_deferred(req->mid)) {
1164 /* We have re-scheduled this call, no error. */
1165 return;
1167 reply_openerror(req, status);
1168 goto out;
1171 /* Ensure we're pointing at the correct stat struct. */
1172 TALLOC_FREE(smb_fname);
1173 smb_fname = fsp->fsp_name;
1176 * If the caller set the extended oplock request bit
1177 * and we granted one (by whatever means) - set the
1178 * correct bit for extended oplock reply.
1181 if (oplock_request &&
1182 (lp_fake_oplocks(SNUM(conn))
1183 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1186 * Exclusive oplock granted
1189 if (flags & REQUEST_BATCH_OPLOCK) {
1190 oplock_granted = BATCH_OPLOCK_RETURN;
1191 } else {
1192 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1194 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1195 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1196 } else {
1197 oplock_granted = NO_OPLOCK_RETURN;
1200 file_len = smb_fname->st.st_ex_size;
1202 /* Realloc the size of parameters and data we will return */
1203 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1204 /* Extended response is 32 more byyes. */
1205 param_len = 101;
1206 } else {
1207 param_len = 69;
1209 params = nttrans_realloc(ppparams, param_len);
1210 if(params == NULL) {
1211 reply_nterror(req, NT_STATUS_NO_MEMORY);
1212 goto out;
1215 p = params;
1216 SCVAL(p, 0, oplock_granted);
1218 p += 2;
1219 SSVAL(p,0,fsp->fnum);
1220 p += 2;
1221 if ((create_disposition == FILE_SUPERSEDE)
1222 && (info == FILE_WAS_OVERWRITTEN)) {
1223 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1224 } else {
1225 SIVAL(p,0,info);
1227 p += 8;
1229 fattr = dos_mode(conn, smb_fname);
1230 if (fattr == 0) {
1231 fattr = FILE_ATTRIBUTE_NORMAL;
1234 /* Deal with other possible opens having a modified
1235 write time. JRA. */
1236 ZERO_STRUCT(write_time_ts);
1237 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1238 if (!null_timespec(write_time_ts)) {
1239 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1242 /* Create time. */
1243 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1244 a_timespec = smb_fname->st.st_ex_atime;
1245 m_timespec = smb_fname->st.st_ex_mtime;
1246 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1248 if (lp_dos_filetime_resolution(SNUM(conn))) {
1249 dos_filetime_timespec(&create_timespec);
1250 dos_filetime_timespec(&a_timespec);
1251 dos_filetime_timespec(&m_timespec);
1252 dos_filetime_timespec(&c_timespec);
1255 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1256 p += 8;
1257 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1258 p += 8;
1259 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1260 p += 8;
1261 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1262 p += 8;
1263 SIVAL(p,0,fattr); /* File Attributes. */
1264 p += 4;
1265 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1266 p += 8;
1267 SOFF_T(p,0,file_len);
1268 p += 8;
1269 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1270 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1271 size_t num_names = 0;
1272 unsigned int num_streams = 0;
1273 struct stream_struct *streams = NULL;
1275 /* Do we have any EA's ? */
1276 status = get_ea_names_from_file(ctx, conn, fsp,
1277 smb_fname->base_name, NULL, &num_names);
1278 if (NT_STATUS_IS_OK(status) && num_names) {
1279 file_status &= ~NO_EAS;
1281 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1282 &num_streams, &streams);
1283 /* There is always one stream, ::$DATA. */
1284 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1285 file_status &= ~NO_SUBSTREAMS;
1287 TALLOC_FREE(streams);
1288 SSVAL(p,2,file_status);
1290 p += 4;
1291 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1293 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1294 uint32 perms = 0;
1295 p += 25;
1296 if (fsp->is_directory ||
1297 can_write_to_file(conn, smb_fname)) {
1298 perms = FILE_GENERIC_ALL;
1299 } else {
1300 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1302 SIVAL(p,0,perms);
1305 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1306 smb_fname_str_dbg(smb_fname)));
1308 /* Send the required number of replies */
1309 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1310 out:
1311 return;
1314 /****************************************************************************
1315 Reply to a NT CANCEL request.
1316 conn POINTER CAN BE NULL HERE !
1317 ****************************************************************************/
1319 void reply_ntcancel(struct smb_request *req)
1322 * Go through and cancel any pending change notifies.
1325 START_PROFILE(SMBntcancel);
1326 srv_cancel_sign_response(req->sconn);
1327 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1328 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1330 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1331 (unsigned long long)req->mid));
1333 END_PROFILE(SMBntcancel);
1334 return;
1337 /****************************************************************************
1338 Copy a file.
1339 ****************************************************************************/
1341 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1342 connection_struct *conn,
1343 struct smb_request *req,
1344 struct smb_filename *smb_fname_src,
1345 struct smb_filename *smb_fname_dst,
1346 uint32 attrs)
1348 files_struct *fsp1,*fsp2;
1349 uint32 fattr;
1350 int info;
1351 SMB_OFF_T ret=-1;
1352 NTSTATUS status = NT_STATUS_OK;
1353 char *parent;
1355 if (!CAN_WRITE(conn)) {
1356 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1357 goto out;
1360 /* Source must already exist. */
1361 if (!VALID_STAT(smb_fname_src->st)) {
1362 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1363 goto out;
1366 /* Ensure attributes match. */
1367 fattr = dos_mode(conn, smb_fname_src);
1368 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1369 status = NT_STATUS_NO_SUCH_FILE;
1370 goto out;
1373 /* Disallow if dst file already exists. */
1374 if (VALID_STAT(smb_fname_dst->st)) {
1375 status = NT_STATUS_OBJECT_NAME_COLLISION;
1376 goto out;
1379 /* No links from a directory. */
1380 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1381 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1382 goto out;
1385 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1386 smb_fname_str_dbg(smb_fname_src),
1387 smb_fname_str_dbg(smb_fname_dst)));
1389 status = SMB_VFS_CREATE_FILE(
1390 conn, /* conn */
1391 req, /* req */
1392 0, /* root_dir_fid */
1393 smb_fname_src, /* fname */
1394 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1395 FILE_READ_EA, /* access_mask */
1396 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1397 FILE_SHARE_DELETE),
1398 FILE_OPEN, /* create_disposition*/
1399 0, /* create_options */
1400 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1401 NO_OPLOCK, /* oplock_request */
1402 0, /* allocation_size */
1403 0, /* private_flags */
1404 NULL, /* sd */
1405 NULL, /* ea_list */
1406 &fsp1, /* result */
1407 &info); /* pinfo */
1409 if (!NT_STATUS_IS_OK(status)) {
1410 goto out;
1413 status = SMB_VFS_CREATE_FILE(
1414 conn, /* conn */
1415 req, /* req */
1416 0, /* root_dir_fid */
1417 smb_fname_dst, /* fname */
1418 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1419 FILE_WRITE_EA, /* access_mask */
1420 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1421 FILE_SHARE_DELETE),
1422 FILE_CREATE, /* create_disposition*/
1423 0, /* create_options */
1424 fattr, /* file_attributes */
1425 NO_OPLOCK, /* oplock_request */
1426 0, /* allocation_size */
1427 0, /* private_flags */
1428 NULL, /* sd */
1429 NULL, /* ea_list */
1430 &fsp2, /* result */
1431 &info); /* pinfo */
1433 if (!NT_STATUS_IS_OK(status)) {
1434 close_file(NULL, fsp1, ERROR_CLOSE);
1435 goto out;
1438 if (smb_fname_src->st.st_ex_size) {
1439 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1443 * As we are opening fsp1 read-only we only expect
1444 * an error on close on fsp2 if we are out of space.
1445 * Thus we don't look at the error return from the
1446 * close of fsp1.
1448 close_file(NULL, fsp1, NORMAL_CLOSE);
1450 /* Ensure the modtime is set correctly on the destination file. */
1451 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1453 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1455 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1456 creates the file. This isn't the correct thing to do in the copy
1457 case. JRA */
1458 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1459 NULL)) {
1460 status = NT_STATUS_NO_MEMORY;
1461 goto out;
1463 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1464 TALLOC_FREE(parent);
1466 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1467 status = NT_STATUS_DISK_FULL;
1468 goto out;
1470 out:
1471 if (!NT_STATUS_IS_OK(status)) {
1472 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1473 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1474 smb_fname_str_dbg(smb_fname_dst)));
1477 return status;
1480 /****************************************************************************
1481 Reply to a NT rename request.
1482 ****************************************************************************/
1484 void reply_ntrename(struct smb_request *req)
1486 connection_struct *conn = req->conn;
1487 struct smb_filename *smb_fname_old = NULL;
1488 struct smb_filename *smb_fname_new = NULL;
1489 char *oldname = NULL;
1490 char *newname = NULL;
1491 const char *p;
1492 NTSTATUS status;
1493 bool src_has_wcard = False;
1494 bool dest_has_wcard = False;
1495 uint32 attrs;
1496 uint32_t ucf_flags_src = 0;
1497 uint32_t ucf_flags_dst = 0;
1498 uint16 rename_type;
1499 TALLOC_CTX *ctx = talloc_tos();
1500 bool stream_rename = false;
1502 START_PROFILE(SMBntrename);
1504 if (req->wct < 4) {
1505 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1506 goto out;
1509 attrs = SVAL(req->vwv+0, 0);
1510 rename_type = SVAL(req->vwv+1, 0);
1512 p = (const char *)req->buf + 1;
1513 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1514 &status, &src_has_wcard);
1515 if (!NT_STATUS_IS_OK(status)) {
1516 reply_nterror(req, status);
1517 goto out;
1520 if (ms_has_wild(oldname)) {
1521 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1522 goto out;
1525 p++;
1526 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1527 &status, &dest_has_wcard);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 reply_nterror(req, status);
1530 goto out;
1533 if (!lp_posix_pathnames()) {
1534 /* The newname must begin with a ':' if the
1535 oldname contains a ':'. */
1536 if (strchr_m(oldname, ':')) {
1537 if (newname[0] != ':') {
1538 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1539 goto out;
1541 stream_rename = true;
1546 * If this is a rename operation, allow wildcards and save the
1547 * destination's last component.
1549 if (rename_type == RENAME_FLAG_RENAME) {
1550 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1551 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1554 /* rename_internals() calls unix_convert(), so don't call it here. */
1555 status = filename_convert(ctx, conn,
1556 req->flags2 & FLAGS2_DFS_PATHNAMES,
1557 oldname,
1558 ucf_flags_src,
1559 NULL,
1560 &smb_fname_old);
1561 if (!NT_STATUS_IS_OK(status)) {
1562 if (NT_STATUS_EQUAL(status,
1563 NT_STATUS_PATH_NOT_COVERED)) {
1564 reply_botherror(req,
1565 NT_STATUS_PATH_NOT_COVERED,
1566 ERRSRV, ERRbadpath);
1567 goto out;
1569 reply_nterror(req, status);
1570 goto out;
1573 status = filename_convert(ctx, conn,
1574 req->flags2 & FLAGS2_DFS_PATHNAMES,
1575 newname,
1576 ucf_flags_dst,
1577 &dest_has_wcard,
1578 &smb_fname_new);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 if (NT_STATUS_EQUAL(status,
1581 NT_STATUS_PATH_NOT_COVERED)) {
1582 reply_botherror(req,
1583 NT_STATUS_PATH_NOT_COVERED,
1584 ERRSRV, ERRbadpath);
1585 goto out;
1587 reply_nterror(req, status);
1588 goto out;
1591 if (stream_rename) {
1592 /* smb_fname_new must be the same as smb_fname_old. */
1593 TALLOC_FREE(smb_fname_new->base_name);
1594 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1595 smb_fname_old->base_name);
1596 if (!smb_fname_new->base_name) {
1597 reply_nterror(req, NT_STATUS_NO_MEMORY);
1598 goto out;
1602 DEBUG(3,("reply_ntrename: %s -> %s\n",
1603 smb_fname_str_dbg(smb_fname_old),
1604 smb_fname_str_dbg(smb_fname_new)));
1606 switch(rename_type) {
1607 case RENAME_FLAG_RENAME:
1608 status = rename_internals(ctx, conn, req,
1609 smb_fname_old, smb_fname_new,
1610 attrs, False, src_has_wcard,
1611 dest_has_wcard,
1612 DELETE_ACCESS);
1613 break;
1614 case RENAME_FLAG_HARD_LINK:
1615 if (src_has_wcard || dest_has_wcard) {
1616 /* No wildcards. */
1617 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1618 } else {
1619 status = hardlink_internals(ctx, conn,
1620 req,
1621 false,
1622 smb_fname_old,
1623 smb_fname_new);
1625 break;
1626 case RENAME_FLAG_COPY:
1627 if (src_has_wcard || dest_has_wcard) {
1628 /* No wildcards. */
1629 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1630 } else {
1631 status = copy_internals(ctx, conn, req,
1632 smb_fname_old,
1633 smb_fname_new,
1634 attrs);
1636 break;
1637 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1638 status = NT_STATUS_INVALID_PARAMETER;
1639 break;
1640 default:
1641 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1642 break;
1645 if (!NT_STATUS_IS_OK(status)) {
1646 if (open_was_deferred(req->mid)) {
1647 /* We have re-scheduled this call. */
1648 goto out;
1651 reply_nterror(req, status);
1652 goto out;
1655 reply_outbuf(req, 0, 0);
1656 out:
1657 END_PROFILE(SMBntrename);
1658 return;
1661 /****************************************************************************
1662 Reply to a notify change - queue the request and
1663 don't allow a directory to be opened.
1664 ****************************************************************************/
1666 static void smbd_smb1_notify_reply(struct smb_request *req,
1667 NTSTATUS error_code,
1668 uint8_t *buf, size_t len)
1670 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1673 static void call_nt_transact_notify_change(connection_struct *conn,
1674 struct smb_request *req,
1675 uint16 **ppsetup,
1676 uint32 setup_count,
1677 char **ppparams,
1678 uint32 parameter_count,
1679 char **ppdata, uint32 data_count,
1680 uint32 max_data_count,
1681 uint32 max_param_count)
1683 uint16 *setup = *ppsetup;
1684 files_struct *fsp;
1685 uint32 filter;
1686 NTSTATUS status;
1687 bool recursive;
1689 if(setup_count < 6) {
1690 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1691 return;
1694 fsp = file_fsp(req, SVAL(setup,4));
1695 filter = IVAL(setup, 0);
1696 recursive = (SVAL(setup, 6) != 0) ? True : False;
1698 DEBUG(3,("call_nt_transact_notify_change\n"));
1700 if(!fsp) {
1701 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1702 return;
1706 char *filter_string;
1708 if (!(filter_string = notify_filter_string(NULL, filter))) {
1709 reply_nterror(req,NT_STATUS_NO_MEMORY);
1710 return;
1713 DEBUG(3,("call_nt_transact_notify_change: notify change "
1714 "called on %s, filter = %s, recursive = %d\n",
1715 fsp_str_dbg(fsp), filter_string, recursive));
1717 TALLOC_FREE(filter_string);
1720 if((!fsp->is_directory) || (conn != fsp->conn)) {
1721 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1722 return;
1725 if (fsp->notify == NULL) {
1727 status = change_notify_create(fsp, filter, recursive);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 DEBUG(10, ("change_notify_create returned %s\n",
1731 nt_errstr(status)));
1732 reply_nterror(req, status);
1733 return;
1737 if (fsp->notify->num_changes != 0) {
1740 * We've got changes pending, respond immediately
1744 * TODO: write a torture test to check the filtering behaviour
1745 * here.
1748 change_notify_reply(req,
1749 NT_STATUS_OK,
1750 max_param_count,
1751 fsp->notify,
1752 smbd_smb1_notify_reply);
1755 * change_notify_reply() above has independently sent its
1756 * results
1758 return;
1762 * No changes pending, queue the request
1765 status = change_notify_add_request(req,
1766 max_param_count,
1767 filter,
1768 recursive, fsp,
1769 smbd_smb1_notify_reply);
1770 if (!NT_STATUS_IS_OK(status)) {
1771 reply_nterror(req, status);
1773 return;
1776 /****************************************************************************
1777 Reply to an NT transact rename command.
1778 ****************************************************************************/
1780 static void call_nt_transact_rename(connection_struct *conn,
1781 struct smb_request *req,
1782 uint16 **ppsetup, uint32 setup_count,
1783 char **ppparams, uint32 parameter_count,
1784 char **ppdata, uint32 data_count,
1785 uint32 max_data_count)
1787 char *params = *ppparams;
1788 char *new_name = NULL;
1789 files_struct *fsp = NULL;
1790 bool dest_has_wcard = False;
1791 NTSTATUS status;
1792 TALLOC_CTX *ctx = talloc_tos();
1794 if(parameter_count < 5) {
1795 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1796 return;
1799 fsp = file_fsp(req, SVAL(params, 0));
1800 if (!check_fsp(conn, req, fsp)) {
1801 return;
1803 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1804 parameter_count - 4,
1805 STR_TERMINATE, &status, &dest_has_wcard);
1806 if (!NT_STATUS_IS_OK(status)) {
1807 reply_nterror(req, status);
1808 return;
1812 * W2K3 ignores this request as the RAW-RENAME test
1813 * demonstrates, so we do.
1815 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1817 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1818 fsp_str_dbg(fsp), new_name));
1820 return;
1823 /******************************************************************************
1824 Fake up a completely empty SD.
1825 *******************************************************************************/
1827 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1829 size_t sd_size;
1831 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1832 if(!*ppsd) {
1833 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1834 return NT_STATUS_NO_MEMORY;
1837 return NT_STATUS_OK;
1840 /****************************************************************************
1841 Reply to query a security descriptor.
1842 Callable from SMB2 and SMB2.
1843 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1844 the required size.
1845 ****************************************************************************/
1847 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1848 TALLOC_CTX *mem_ctx,
1849 files_struct *fsp,
1850 uint32_t security_info_wanted,
1851 uint32_t max_data_count,
1852 uint8_t **ppmarshalled_sd,
1853 size_t *psd_size)
1855 NTSTATUS status;
1856 struct security_descriptor *psd = NULL;
1859 * Get the permissions to return.
1862 if ((security_info_wanted & SECINFO_SACL) &&
1863 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1864 return NT_STATUS_ACCESS_DENIED;
1867 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1868 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1869 return NT_STATUS_ACCESS_DENIED;
1872 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1873 SECINFO_GROUP|SECINFO_SACL)) {
1874 /* Don't return SECINFO_LABEL if anything else was
1875 requested. See bug #8458. */
1876 security_info_wanted &= ~SECINFO_LABEL;
1879 if (!lp_nt_acl_support(SNUM(conn))) {
1880 status = get_null_nt_acl(mem_ctx, &psd);
1881 } else if (security_info_wanted & SECINFO_LABEL) {
1882 /* Like W2K3 return a null object. */
1883 status = get_null_nt_acl(mem_ctx, &psd);
1884 } else {
1885 status = SMB_VFS_FGET_NT_ACL(
1886 fsp, security_info_wanted, &psd);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 return status;
1892 if (!(security_info_wanted & SECINFO_OWNER)) {
1893 psd->owner_sid = NULL;
1895 if (!(security_info_wanted & SECINFO_GROUP)) {
1896 psd->group_sid = NULL;
1898 if (!(security_info_wanted & SECINFO_DACL)) {
1899 psd->type &= ~SEC_DESC_DACL_PRESENT;
1900 psd->dacl = NULL;
1902 if (!(security_info_wanted & SECINFO_SACL)) {
1903 psd->type &= ~SEC_DESC_SACL_PRESENT;
1904 psd->sacl = NULL;
1907 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1908 * present in the reply to match Windows behavior */
1909 if (psd->sacl == NULL &&
1910 security_info_wanted & SECINFO_SACL)
1911 psd->type |= SEC_DESC_SACL_PRESENT;
1912 if (psd->dacl == NULL &&
1913 security_info_wanted & SECINFO_DACL)
1914 psd->type |= SEC_DESC_DACL_PRESENT;
1916 if (security_info_wanted & SECINFO_LABEL) {
1917 /* Like W2K3 return a null object. */
1918 psd->owner_sid = NULL;
1919 psd->group_sid = NULL;
1920 psd->dacl = NULL;
1921 psd->sacl = NULL;
1922 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1925 *psd_size = ndr_size_security_descriptor(psd, 0);
1927 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1928 (unsigned long)*psd_size));
1930 if (DEBUGLEVEL >= 10) {
1931 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1932 fsp_str_dbg(fsp)));
1933 NDR_PRINT_DEBUG(security_descriptor, psd);
1936 if (max_data_count < *psd_size) {
1937 TALLOC_FREE(psd);
1938 return NT_STATUS_BUFFER_TOO_SMALL;
1941 status = marshall_sec_desc(mem_ctx, psd,
1942 ppmarshalled_sd, psd_size);
1944 if (!NT_STATUS_IS_OK(status)) {
1945 TALLOC_FREE(psd);
1946 return status;
1949 TALLOC_FREE(psd);
1950 return NT_STATUS_OK;
1953 /****************************************************************************
1954 SMB1 reply to query a security descriptor.
1955 ****************************************************************************/
1957 static void call_nt_transact_query_security_desc(connection_struct *conn,
1958 struct smb_request *req,
1959 uint16 **ppsetup,
1960 uint32 setup_count,
1961 char **ppparams,
1962 uint32 parameter_count,
1963 char **ppdata,
1964 uint32 data_count,
1965 uint32 max_data_count)
1967 char *params = *ppparams;
1968 char *data = *ppdata;
1969 size_t sd_size = 0;
1970 uint32 security_info_wanted;
1971 files_struct *fsp = NULL;
1972 NTSTATUS status;
1973 uint8_t *marshalled_sd = NULL;
1975 if(parameter_count < 8) {
1976 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1977 return;
1980 fsp = file_fsp(req, SVAL(params,0));
1981 if(!fsp) {
1982 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1983 return;
1986 security_info_wanted = IVAL(params,4);
1988 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1989 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1990 (unsigned int)security_info_wanted));
1992 params = nttrans_realloc(ppparams, 4);
1993 if(params == NULL) {
1994 reply_nterror(req, NT_STATUS_NO_MEMORY);
1995 return;
1999 * Get the permissions to return.
2002 status = smbd_do_query_security_desc(conn,
2003 talloc_tos(),
2004 fsp,
2005 security_info_wanted,
2006 max_data_count,
2007 &marshalled_sd,
2008 &sd_size);
2010 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2011 SIVAL(params,0,(uint32_t)sd_size);
2012 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2013 params, 4, NULL, 0);
2014 return;
2017 if (!NT_STATUS_IS_OK(status)) {
2018 reply_nterror(req, status);
2019 return;
2022 SMB_ASSERT(sd_size > 0);
2024 SIVAL(params,0,(uint32_t)sd_size);
2026 if (max_data_count < sd_size) {
2027 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2028 params, 4, NULL, 0);
2029 return;
2033 * Allocate the data we will return.
2036 data = nttrans_realloc(ppdata, sd_size);
2037 if(data == NULL) {
2038 reply_nterror(req, NT_STATUS_NO_MEMORY);
2039 return;
2042 memcpy(data, marshalled_sd, sd_size);
2044 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2046 return;
2049 /****************************************************************************
2050 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2051 ****************************************************************************/
2053 static void call_nt_transact_set_security_desc(connection_struct *conn,
2054 struct smb_request *req,
2055 uint16 **ppsetup,
2056 uint32 setup_count,
2057 char **ppparams,
2058 uint32 parameter_count,
2059 char **ppdata,
2060 uint32 data_count,
2061 uint32 max_data_count)
2063 char *params= *ppparams;
2064 char *data = *ppdata;
2065 files_struct *fsp = NULL;
2066 uint32 security_info_sent = 0;
2067 NTSTATUS status;
2069 if(parameter_count < 8) {
2070 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2071 return;
2074 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2075 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2076 return;
2079 if (!CAN_WRITE(fsp->conn)) {
2080 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2081 return;
2084 if(!lp_nt_acl_support(SNUM(conn))) {
2085 goto done;
2088 security_info_sent = IVAL(params,4);
2090 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2091 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2093 if (data_count == 0) {
2094 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2095 return;
2098 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2100 if (!NT_STATUS_IS_OK(status)) {
2101 reply_nterror(req, status);
2102 return;
2105 done:
2106 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2107 return;
2111 * Implement the default fsctl operation.
2114 static bool vfswrap_logged_ioctl_message = false;
2117 * In 3.6 we do not have a SMB_VFS_FSCTL() function
2118 * it's just faked to make it more look like
2119 * master (4.0)
2121 NTSTATUS smb_fsctl(struct files_struct *fsp,
2122 TALLOC_CTX *ctx,
2123 uint32_t function,
2124 uint16_t req_flags, /* Needed for UNICODE ... */
2125 const uint8_t *_in_data,
2126 uint32_t in_len,
2127 uint8_t **_out_data,
2128 uint32_t max_out_len,
2129 uint32_t *out_len)
2131 const char *in_data = (const char *)_in_data;
2132 char **out_data = (char **)_out_data;
2134 switch (function) {
2135 case FSCTL_SET_SPARSE:
2137 bool set_sparse = true;
2138 NTSTATUS status;
2140 if (in_len >= 1 && in_data[0] == 0) {
2141 set_sparse = false;
2144 status = file_set_sparse(fsp->conn, fsp, set_sparse);
2146 DEBUG(NT_STATUS_IS_OK(status) ? 10 : 9,
2147 ("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2148 smb_fname_str_dbg(fsp->fsp_name), set_sparse,
2149 nt_errstr(status)));
2151 return status;
2154 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2156 unsigned char objid[16];
2157 char *return_data = NULL;
2159 /* This should return the object-id on this file.
2160 * I think I'll make this be the inode+dev. JRA.
2163 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fsp->fnum));
2165 *out_len = (max_out_len >= 64) ? 64 : max_out_len;
2166 /* Hmmm, will this cause problems if less data asked for? */
2167 return_data = talloc_array(ctx, char, 64);
2168 if (return_data == NULL) {
2169 return NT_STATUS_NO_MEMORY;
2172 /* For backwards compatibility only store the dev/inode. */
2173 push_file_id_16(return_data, &fsp->file_id);
2174 memcpy(return_data+16,create_volume_objectid(fsp->conn,objid),16);
2175 push_file_id_16(return_data+32, &fsp->file_id);
2176 *out_data = return_data;
2177 return NT_STATUS_OK;
2180 case FSCTL_GET_REPARSE_POINT:
2182 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
2183 DEBUG(10, ("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X] Status: NOT_IMPLEMENTED\n", fsp->fnum));
2184 return NT_STATUS_NOT_A_REPARSE_POINT;
2187 case FSCTL_SET_REPARSE_POINT:
2189 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
2190 DEBUG(10, ("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X] Status: NOT_IMPLEMENTED\n", fsp->fnum));
2191 return NT_STATUS_NOT_A_REPARSE_POINT;
2194 case FSCTL_GET_SHADOW_COPY_DATA:
2197 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2198 * and return their volume names. If max_data_count is 16, then it is just
2199 * asking for the number of volumes and length of the combined names.
2201 * pdata is the data allocated by our caller, but that uses
2202 * total_data_count (which is 0 in our case) rather than max_data_count.
2203 * Allocate the correct amount and return the pointer to let
2204 * it be deallocated when we return.
2206 struct shadow_copy_data *shadow_data = NULL;
2207 bool labels = False;
2208 uint32 labels_data_count = 0;
2209 uint32 i;
2210 char *cur_pdata = NULL;
2212 if (max_out_len < 16) {
2213 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2214 max_out_len));
2215 return NT_STATUS_INVALID_PARAMETER;
2218 if (max_out_len > 16) {
2219 labels = True;
2222 shadow_data = talloc_zero(ctx, struct shadow_copy_data);
2223 if (shadow_data == NULL) {
2224 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2225 return NT_STATUS_NO_MEMORY;
2229 * Call the VFS routine to actually do the work.
2231 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2232 TALLOC_FREE(shadow_data);
2233 if (errno == ENOSYS) {
2234 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2235 fsp->conn->connectpath));
2236 return NT_STATUS_NOT_SUPPORTED;
2237 } else {
2238 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2239 fsp->conn->connectpath));
2240 return NT_STATUS_UNSUCCESSFUL;
2244 labels_data_count = (shadow_data->num_volumes * 2 *
2245 sizeof(SHADOW_COPY_LABEL)) + 2;
2247 if (!labels) {
2248 *out_len = 16;
2249 } else {
2250 *out_len = 12 + labels_data_count + 4;
2253 if (max_out_len < *out_len) {
2254 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2255 max_out_len, *out_len));
2256 TALLOC_FREE(shadow_data);
2257 return NT_STATUS_BUFFER_TOO_SMALL;
2260 cur_pdata = talloc_array(ctx, char, *out_len);
2261 if (cur_pdata == NULL) {
2262 TALLOC_FREE(shadow_data);
2263 return NT_STATUS_NO_MEMORY;
2266 *out_data = cur_pdata;
2268 /* num_volumes 4 bytes */
2269 SIVAL(cur_pdata, 0, shadow_data->num_volumes);
2271 if (labels) {
2272 /* num_labels 4 bytes */
2273 SIVAL(cur_pdata, 4, shadow_data->num_volumes);
2276 /* needed_data_count 4 bytes */
2277 SIVAL(cur_pdata, 8, labels_data_count + 4);
2279 cur_pdata += 12;
2281 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2282 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2283 if (labels && shadow_data->labels) {
2284 for (i=0; i<shadow_data->num_volumes; i++) {
2285 srvstr_push(cur_pdata, req_flags,
2286 cur_pdata, shadow_data->labels[i],
2287 2 * sizeof(SHADOW_COPY_LABEL),
2288 STR_UNICODE|STR_TERMINATE);
2289 cur_pdata += 2 * sizeof(SHADOW_COPY_LABEL);
2290 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2294 TALLOC_FREE(shadow_data);
2296 return NT_STATUS_OK;
2299 case FSCTL_FIND_FILES_BY_SID:
2301 /* pretend this succeeded -
2303 * we have to send back a list with all files owned by this SID
2305 * but I have to check that --metze
2307 struct dom_sid sid;
2308 uid_t uid;
2309 size_t sid_len;
2311 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n", fsp->fnum));
2313 if (in_len < 8) {
2314 /* NT_STATUS_BUFFER_TOO_SMALL maybe? */
2315 return NT_STATUS_INVALID_PARAMETER;
2318 sid_len = MIN(in_len - 4,SID_MAX_SIZE);
2320 /* unknown 4 bytes: this is not the length of the sid :-( */
2321 /*unknown = IVAL(pdata,0);*/
2323 if (!sid_parse(in_data + 4, sid_len, &sid)) {
2324 return NT_STATUS_INVALID_PARAMETER;
2326 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2328 if (!sid_to_uid(&sid, &uid)) {
2329 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2330 sid_string_dbg(&sid),
2331 (unsigned long)sid_len));
2332 uid = (-1);
2335 /* we can take a look at the find source :-)
2337 * find ./ -uid $uid -name '*' is what we need here
2340 * and send 4bytes len and then NULL terminated unicode strings
2341 * for each file
2343 * but I don't know how to deal with the paged results
2344 * (maybe we can hang the result anywhere in the fsp struct)
2346 * but I don't know how to deal with the paged results
2347 * (maybe we can hang the result anywhere in the fsp struct)
2349 * we don't send all files at once
2350 * and at the next we should *not* start from the beginning,
2351 * so we have to cache the result
2353 * --metze
2356 /* this works for now... */
2357 return NT_STATUS_OK;
2360 case FSCTL_QUERY_ALLOCATED_RANGES:
2362 /* FIXME: This is just a dummy reply, telling that all of the
2363 * file is allocated. MKS cp needs that.
2364 * Adding the real allocated ranges via FIEMAP on Linux
2365 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2366 * this FSCTL correct for sparse files.
2368 NTSTATUS status;
2369 uint64_t offset, length;
2370 char *out_data_tmp = NULL;
2372 if (in_len != 16) {
2373 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2374 in_len));
2375 return NT_STATUS_INVALID_PARAMETER;
2378 if (max_out_len < 16) {
2379 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_out_len (%u) < 16 is invalid!\n",
2380 max_out_len));
2381 return NT_STATUS_INVALID_PARAMETER;
2384 offset = BVAL(in_data,0);
2385 length = BVAL(in_data,8);
2387 if (offset + length < offset) {
2388 /* No 64-bit integer wrap. */
2389 return NT_STATUS_INVALID_PARAMETER;
2392 /* Shouldn't this be SMB_VFS_STAT ... ? */
2393 status = vfs_stat_fsp(fsp);
2394 if (!NT_STATUS_IS_OK(status)) {
2395 return status;
2398 *out_len = 16;
2399 out_data_tmp = talloc_array(ctx, char, *out_len);
2400 if (out_data_tmp == NULL) {
2401 DEBUG(10, ("unable to allocate memory for response\n"));
2402 return NT_STATUS_NO_MEMORY;
2405 if (offset > fsp->fsp_name->st.st_ex_size ||
2406 fsp->fsp_name->st.st_ex_size == 0 ||
2407 length == 0) {
2408 memset(out_data_tmp, 0, *out_len);
2409 } else {
2410 uint64_t end = offset + length;
2411 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2412 SBVAL(out_data_tmp, 0, 0);
2413 SBVAL(out_data_tmp, 8, end);
2416 *out_data = out_data_tmp;
2418 return NT_STATUS_OK;
2421 case FSCTL_IS_VOLUME_DIRTY:
2423 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2424 "(but not implemented)\n", fsp->fnum));
2426 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2427 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2429 return NT_STATUS_INVALID_PARAMETER;
2432 default:
2434 * Only print once ... unfortunately there could be lots of
2435 * different FSCTLs that are called.
2437 if (!vfswrap_logged_ioctl_message) {
2438 vfswrap_logged_ioctl_message = true;
2439 DEBUG(2, ("%s (0x%x): Currently not implemented.\n",
2440 __FUNCTION__, function));
2444 return NT_STATUS_NOT_SUPPORTED;
2447 /****************************************************************************
2448 Reply to NT IOCTL
2449 ****************************************************************************/
2451 static void call_nt_transact_ioctl(connection_struct *conn,
2452 struct smb_request *req,
2453 uint16 **ppsetup, uint32 setup_count,
2454 char **ppparams, uint32 parameter_count,
2455 char **ppdata, uint32 data_count,
2456 uint32 max_data_count)
2458 NTSTATUS status;
2459 uint32 function;
2460 uint16 fidnum;
2461 files_struct *fsp;
2462 uint8 isFSctl;
2463 uint8 compfilter;
2464 char *out_data = NULL;
2465 uint32 out_data_len = 0;
2466 char *pdata = *ppdata;
2467 TALLOC_CTX *ctx = talloc_tos();
2469 if (setup_count != 8) {
2470 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2471 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2472 return;
2475 function = IVAL(*ppsetup, 0);
2476 fidnum = SVAL(*ppsetup, 4);
2477 isFSctl = CVAL(*ppsetup, 6);
2478 compfilter = CVAL(*ppsetup, 7);
2480 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2481 function, fidnum, isFSctl, compfilter));
2483 fsp=file_fsp(req, fidnum);
2486 * We don't really implement IOCTLs, especially on files.
2488 if (!isFSctl) {
2489 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2490 isFSctl));
2491 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2492 return;
2495 /* Has to be for an open file! */
2496 if (!check_fsp_open(conn, req, fsp)) {
2497 return;
2501 * out_data might be allocated by the VFS module, but talloc should be
2502 * used, and should be cleaned up when the request ends.
2504 status = smb_fsctl(fsp,
2505 ctx,
2506 function,
2507 req->flags2,
2508 (uint8_t *)pdata,
2509 data_count,
2510 (uint8_t **)&out_data,
2511 max_data_count,
2512 &out_data_len);
2513 if (!NT_STATUS_IS_OK(status)) {
2514 reply_nterror(req, status);
2515 } else {
2516 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2521 #ifdef HAVE_SYS_QUOTAS
2522 /****************************************************************************
2523 Reply to get user quota
2524 ****************************************************************************/
2526 static void call_nt_transact_get_user_quota(connection_struct *conn,
2527 struct smb_request *req,
2528 uint16 **ppsetup,
2529 uint32 setup_count,
2530 char **ppparams,
2531 uint32 parameter_count,
2532 char **ppdata,
2533 uint32 data_count,
2534 uint32 max_data_count)
2536 NTSTATUS nt_status = NT_STATUS_OK;
2537 char *params = *ppparams;
2538 char *pdata = *ppdata;
2539 char *entry;
2540 int data_len=0,param_len=0;
2541 int qt_len=0;
2542 int entry_len = 0;
2543 files_struct *fsp = NULL;
2544 uint16 level = 0;
2545 size_t sid_len;
2546 struct dom_sid sid;
2547 bool start_enum = True;
2548 SMB_NTQUOTA_STRUCT qt;
2549 SMB_NTQUOTA_LIST *tmp_list;
2550 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2552 ZERO_STRUCT(qt);
2554 /* access check */
2555 if (get_current_uid(conn) != 0) {
2556 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2557 "[%s]\n", lp_servicename(SNUM(conn)),
2558 conn->session_info->unix_name));
2559 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2560 return;
2564 * Ensure minimum number of parameters sent.
2567 if (parameter_count < 4) {
2568 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2569 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2570 return;
2573 /* maybe we can check the quota_fnum */
2574 fsp = file_fsp(req, SVAL(params,0));
2575 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2576 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2577 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2578 return;
2581 /* the NULL pointer checking for fsp->fake_file_handle->pd
2582 * is done by CHECK_NTQUOTA_HANDLE_OK()
2584 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2586 level = SVAL(params,2);
2588 /* unknown 12 bytes leading in params */
2590 switch (level) {
2591 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2592 /* seems that we should continue with the enum here --metze */
2594 if (qt_handle->quota_list!=NULL &&
2595 qt_handle->tmp_list==NULL) {
2597 /* free the list */
2598 free_ntquota_list(&(qt_handle->quota_list));
2600 /* Realloc the size of parameters and data we will return */
2601 param_len = 4;
2602 params = nttrans_realloc(ppparams, param_len);
2603 if(params == NULL) {
2604 reply_nterror(req, NT_STATUS_NO_MEMORY);
2605 return;
2608 data_len = 0;
2609 SIVAL(params,0,data_len);
2611 break;
2614 start_enum = False;
2616 case TRANSACT_GET_USER_QUOTA_LIST_START:
2618 if (qt_handle->quota_list==NULL &&
2619 qt_handle->tmp_list==NULL) {
2620 start_enum = True;
2623 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2624 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2625 return;
2628 /* Realloc the size of parameters and data we will return */
2629 param_len = 4;
2630 params = nttrans_realloc(ppparams, param_len);
2631 if(params == NULL) {
2632 reply_nterror(req, NT_STATUS_NO_MEMORY);
2633 return;
2636 /* we should not trust the value in max_data_count*/
2637 max_data_count = MIN(max_data_count,2048);
2639 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2640 if(pdata == NULL) {
2641 reply_nterror(req, NT_STATUS_NO_MEMORY);
2642 return;
2645 entry = pdata;
2647 /* set params Size of returned Quota Data 4 bytes*/
2648 /* but set it later when we know it */
2650 /* for each entry push the data */
2652 if (start_enum) {
2653 qt_handle->tmp_list = qt_handle->quota_list;
2656 tmp_list = qt_handle->tmp_list;
2658 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2659 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2661 sid_len = ndr_size_dom_sid(
2662 &tmp_list->quotas->sid, 0);
2663 entry_len = 40 + sid_len;
2665 /* nextoffset entry 4 bytes */
2666 SIVAL(entry,0,entry_len);
2668 /* then the len of the SID 4 bytes */
2669 SIVAL(entry,4,sid_len);
2671 /* unknown data 8 bytes uint64_t */
2672 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2674 /* the used disk space 8 bytes uint64_t */
2675 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2677 /* the soft quotas 8 bytes uint64_t */
2678 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2680 /* the hard quotas 8 bytes uint64_t */
2681 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2683 /* and now the SID */
2684 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2687 qt_handle->tmp_list = tmp_list;
2689 /* overwrite the offset of the last entry */
2690 SIVAL(entry-entry_len,0,0);
2692 data_len = 4+qt_len;
2693 /* overwrite the params quota_data_len */
2694 SIVAL(params,0,data_len);
2696 break;
2698 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2700 /* unknown 4 bytes IVAL(pdata,0) */
2702 if (data_count < 8) {
2703 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2704 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2705 return;
2708 sid_len = IVAL(pdata,4);
2709 /* Ensure this is less than 1mb. */
2710 if (sid_len > (1024*1024)) {
2711 reply_nterror(req, NT_STATUS_NO_MEMORY);
2712 return;
2715 if (data_count < 8+sid_len) {
2716 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2717 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2718 return;
2721 data_len = 4+40+sid_len;
2723 if (max_data_count < data_len) {
2724 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2725 max_data_count, data_len));
2726 param_len = 4;
2727 SIVAL(params,0,data_len);
2728 data_len = 0;
2729 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2730 break;
2733 if (!sid_parse(pdata+8,sid_len,&sid)) {
2734 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2735 return;
2738 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2739 ZERO_STRUCT(qt);
2741 * we have to return zero's in all fields
2742 * instead of returning an error here
2743 * --metze
2747 /* Realloc the size of parameters and data we will return */
2748 param_len = 4;
2749 params = nttrans_realloc(ppparams, param_len);
2750 if(params == NULL) {
2751 reply_nterror(req, NT_STATUS_NO_MEMORY);
2752 return;
2755 pdata = nttrans_realloc(ppdata, data_len);
2756 if(pdata == NULL) {
2757 reply_nterror(req, NT_STATUS_NO_MEMORY);
2758 return;
2761 entry = pdata;
2763 /* set params Size of returned Quota Data 4 bytes*/
2764 SIVAL(params,0,data_len);
2766 /* nextoffset entry 4 bytes */
2767 SIVAL(entry,0,0);
2769 /* then the len of the SID 4 bytes */
2770 SIVAL(entry,4,sid_len);
2772 /* unknown data 8 bytes uint64_t */
2773 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2775 /* the used disk space 8 bytes uint64_t */
2776 SBIG_UINT(entry,16,qt.usedspace);
2778 /* the soft quotas 8 bytes uint64_t */
2779 SBIG_UINT(entry,24,qt.softlim);
2781 /* the hard quotas 8 bytes uint64_t */
2782 SBIG_UINT(entry,32,qt.hardlim);
2784 /* and now the SID */
2785 sid_linearize(entry+40, sid_len, &sid);
2787 break;
2789 default:
2790 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2791 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2792 return;
2793 break;
2796 send_nt_replies(conn, req, nt_status, params, param_len,
2797 pdata, data_len);
2800 /****************************************************************************
2801 Reply to set user quota
2802 ****************************************************************************/
2804 static void call_nt_transact_set_user_quota(connection_struct *conn,
2805 struct smb_request *req,
2806 uint16 **ppsetup,
2807 uint32 setup_count,
2808 char **ppparams,
2809 uint32 parameter_count,
2810 char **ppdata,
2811 uint32 data_count,
2812 uint32 max_data_count)
2814 char *params = *ppparams;
2815 char *pdata = *ppdata;
2816 int data_len=0,param_len=0;
2817 SMB_NTQUOTA_STRUCT qt;
2818 size_t sid_len;
2819 struct dom_sid sid;
2820 files_struct *fsp = NULL;
2822 ZERO_STRUCT(qt);
2824 /* access check */
2825 if (get_current_uid(conn) != 0) {
2826 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2827 "[%s]\n", lp_servicename(SNUM(conn)),
2828 conn->session_info->unix_name));
2829 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2830 return;
2834 * Ensure minimum number of parameters sent.
2837 if (parameter_count < 2) {
2838 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2839 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2840 return;
2843 /* maybe we can check the quota_fnum */
2844 fsp = file_fsp(req, SVAL(params,0));
2845 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2846 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2847 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2848 return;
2851 if (data_count < 40) {
2852 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2853 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2854 return;
2857 /* offset to next quota record.
2858 * 4 bytes IVAL(pdata,0)
2859 * unused here...
2862 /* sid len */
2863 sid_len = IVAL(pdata,4);
2865 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2866 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2867 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2868 return;
2871 /* unknown 8 bytes in pdata
2872 * maybe its the change time in NTTIME
2875 /* the used space 8 bytes (uint64_t)*/
2876 qt.usedspace = BVAL(pdata,16);
2878 /* the soft quotas 8 bytes (uint64_t)*/
2879 qt.softlim = BVAL(pdata,24);
2881 /* the hard quotas 8 bytes (uint64_t)*/
2882 qt.hardlim = BVAL(pdata,32);
2884 if (!sid_parse(pdata+40,sid_len,&sid)) {
2885 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2886 return;
2889 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2891 /* 44 unknown bytes left... */
2893 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2894 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2895 return;
2898 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2899 pdata, data_len);
2901 #endif /* HAVE_SYS_QUOTAS */
2903 static void handle_nttrans(connection_struct *conn,
2904 struct trans_state *state,
2905 struct smb_request *req)
2907 if (get_Protocol() >= PROTOCOL_NT1) {
2908 req->flags2 |= 0x40; /* IS_LONG_NAME */
2909 SSVAL(req->inbuf,smb_flg2,req->flags2);
2913 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2915 /* Now we must call the relevant NT_TRANS function */
2916 switch(state->call) {
2917 case NT_TRANSACT_CREATE:
2919 START_PROFILE(NT_transact_create);
2920 call_nt_transact_create(
2921 conn, req,
2922 &state->setup, state->setup_count,
2923 &state->param, state->total_param,
2924 &state->data, state->total_data,
2925 state->max_data_return);
2926 END_PROFILE(NT_transact_create);
2927 break;
2930 case NT_TRANSACT_IOCTL:
2932 START_PROFILE(NT_transact_ioctl);
2933 call_nt_transact_ioctl(
2934 conn, req,
2935 &state->setup, state->setup_count,
2936 &state->param, state->total_param,
2937 &state->data, state->total_data,
2938 state->max_data_return);
2939 END_PROFILE(NT_transact_ioctl);
2940 break;
2943 case NT_TRANSACT_SET_SECURITY_DESC:
2945 START_PROFILE(NT_transact_set_security_desc);
2946 call_nt_transact_set_security_desc(
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_set_security_desc);
2953 break;
2956 case NT_TRANSACT_NOTIFY_CHANGE:
2958 START_PROFILE(NT_transact_notify_change);
2959 call_nt_transact_notify_change(
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 state->max_param_return);
2966 END_PROFILE(NT_transact_notify_change);
2967 break;
2970 case NT_TRANSACT_RENAME:
2972 START_PROFILE(NT_transact_rename);
2973 call_nt_transact_rename(
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_rename);
2980 break;
2983 case NT_TRANSACT_QUERY_SECURITY_DESC:
2985 START_PROFILE(NT_transact_query_security_desc);
2986 call_nt_transact_query_security_desc(
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_query_security_desc);
2993 break;
2996 #ifdef HAVE_SYS_QUOTAS
2997 case NT_TRANSACT_GET_USER_QUOTA:
2999 START_PROFILE(NT_transact_get_user_quota);
3000 call_nt_transact_get_user_quota(
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_get_user_quota);
3007 break;
3010 case NT_TRANSACT_SET_USER_QUOTA:
3012 START_PROFILE(NT_transact_set_user_quota);
3013 call_nt_transact_set_user_quota(
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 END_PROFILE(NT_transact_set_user_quota);
3020 break;
3022 #endif /* HAVE_SYS_QUOTAS */
3024 default:
3025 /* Error in request */
3026 DEBUG(0,("handle_nttrans: Unknown request %d in "
3027 "nttrans call\n", state->call));
3028 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3029 return;
3031 return;
3034 /****************************************************************************
3035 Reply to a SMBNTtrans.
3036 ****************************************************************************/
3038 void reply_nttrans(struct smb_request *req)
3040 connection_struct *conn = req->conn;
3041 uint32_t pscnt;
3042 uint32_t psoff;
3043 uint32_t dscnt;
3044 uint32_t dsoff;
3045 uint16 function_code;
3046 NTSTATUS result;
3047 struct trans_state *state;
3049 START_PROFILE(SMBnttrans);
3051 if (req->wct < 19) {
3052 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3053 END_PROFILE(SMBnttrans);
3054 return;
3057 pscnt = IVAL(req->vwv+9, 1);
3058 psoff = IVAL(req->vwv+11, 1);
3059 dscnt = IVAL(req->vwv+13, 1);
3060 dsoff = IVAL(req->vwv+15, 1);
3061 function_code = SVAL(req->vwv+18, 0);
3063 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3064 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3065 END_PROFILE(SMBnttrans);
3066 return;
3069 result = allow_new_trans(conn->pending_trans, req->mid);
3070 if (!NT_STATUS_IS_OK(result)) {
3071 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3072 reply_nterror(req, result);
3073 END_PROFILE(SMBnttrans);
3074 return;
3077 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3078 reply_nterror(req, NT_STATUS_NO_MEMORY);
3079 END_PROFILE(SMBnttrans);
3080 return;
3083 state->cmd = SMBnttrans;
3085 state->mid = req->mid;
3086 state->vuid = req->vuid;
3087 state->total_data = IVAL(req->vwv+3, 1);
3088 state->data = NULL;
3089 state->total_param = IVAL(req->vwv+1, 1);
3090 state->param = NULL;
3091 state->max_data_return = IVAL(req->vwv+7, 1);
3092 state->max_param_return = IVAL(req->vwv+5, 1);
3094 /* setup count is in *words* */
3095 state->setup_count = 2*CVAL(req->vwv+17, 1);
3096 state->setup = NULL;
3097 state->call = function_code;
3099 DEBUG(10, ("num_setup=%u, "
3100 "param_total=%u, this_param=%u, max_param=%u, "
3101 "data_total=%u, this_data=%u, max_data=%u, "
3102 "param_offset=%u, data_offset=%u\n",
3103 (unsigned)state->setup_count,
3104 (unsigned)state->total_param, (unsigned)pscnt,
3105 (unsigned)state->max_param_return,
3106 (unsigned)state->total_data, (unsigned)dscnt,
3107 (unsigned)state->max_data_return,
3108 (unsigned)psoff, (unsigned)dsoff));
3111 * All nttrans messages we handle have smb_wct == 19 +
3112 * state->setup_count. Ensure this is so as a sanity check.
3115 if(req->wct != 19 + (state->setup_count/2)) {
3116 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3117 req->wct, 19 + (state->setup_count/2)));
3118 goto bad_param;
3121 /* Don't allow more than 128mb for each value. */
3122 if ((state->total_data > (1024*1024*128)) ||
3123 (state->total_param > (1024*1024*128))) {
3124 reply_nterror(req, NT_STATUS_NO_MEMORY);
3125 END_PROFILE(SMBnttrans);
3126 return;
3129 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3130 goto bad_param;
3132 if (state->total_data) {
3134 if (trans_oob(state->total_data, 0, dscnt)
3135 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3136 goto bad_param;
3139 /* Can't use talloc here, the core routines do realloc on the
3140 * params and data. */
3141 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3142 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3143 "bytes !\n", (unsigned int)state->total_data));
3144 TALLOC_FREE(state);
3145 reply_nterror(req, NT_STATUS_NO_MEMORY);
3146 END_PROFILE(SMBnttrans);
3147 return;
3150 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3153 if (state->total_param) {
3155 if (trans_oob(state->total_param, 0, pscnt)
3156 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3157 goto bad_param;
3160 /* Can't use talloc here, the core routines do realloc on the
3161 * params and data. */
3162 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3163 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3164 "bytes !\n", (unsigned int)state->total_param));
3165 SAFE_FREE(state->data);
3166 TALLOC_FREE(state);
3167 reply_nterror(req, NT_STATUS_NO_MEMORY);
3168 END_PROFILE(SMBnttrans);
3169 return;
3172 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3175 state->received_data = dscnt;
3176 state->received_param = pscnt;
3178 if(state->setup_count > 0) {
3179 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3180 state->setup_count));
3183 * No overflow possible here, state->setup_count is an
3184 * unsigned int, being filled by a single byte from
3185 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3186 * below is not necessary, it's here to clarify things. The
3187 * validity of req->vwv and req->wct has been checked in
3188 * init_smb_request already.
3190 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3191 goto bad_param;
3194 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3195 if (state->setup == NULL) {
3196 DEBUG(0,("reply_nttrans : Out of memory\n"));
3197 SAFE_FREE(state->data);
3198 SAFE_FREE(state->param);
3199 TALLOC_FREE(state);
3200 reply_nterror(req, NT_STATUS_NO_MEMORY);
3201 END_PROFILE(SMBnttrans);
3202 return;
3205 memcpy(state->setup, req->vwv+19, state->setup_count);
3206 dump_data(10, (uint8 *)state->setup, state->setup_count);
3209 if ((state->received_data == state->total_data) &&
3210 (state->received_param == state->total_param)) {
3211 handle_nttrans(conn, state, req);
3212 SAFE_FREE(state->param);
3213 SAFE_FREE(state->data);
3214 TALLOC_FREE(state);
3215 END_PROFILE(SMBnttrans);
3216 return;
3219 DLIST_ADD(conn->pending_trans, state);
3221 /* We need to send an interim response then receive the rest
3222 of the parameter/data bytes */
3223 reply_outbuf(req, 0, 0);
3224 show_msg((char *)req->outbuf);
3225 END_PROFILE(SMBnttrans);
3226 return;
3228 bad_param:
3230 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3231 SAFE_FREE(state->data);
3232 SAFE_FREE(state->param);
3233 TALLOC_FREE(state);
3234 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3235 END_PROFILE(SMBnttrans);
3236 return;
3239 /****************************************************************************
3240 Reply to a SMBnttranss
3241 ****************************************************************************/
3243 void reply_nttranss(struct smb_request *req)
3245 connection_struct *conn = req->conn;
3246 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3247 struct trans_state *state;
3249 START_PROFILE(SMBnttranss);
3251 show_msg((char *)req->inbuf);
3253 /* Windows clients expect all replies to
3254 an NT transact secondary (SMBnttranss 0xA1)
3255 to have a command code of NT transact
3256 (SMBnttrans 0xA0). See bug #8989 for details. */
3257 req->cmd = SMBnttrans;
3259 if (req->wct < 18) {
3260 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3261 END_PROFILE(SMBnttranss);
3262 return;
3265 for (state = conn->pending_trans; state != NULL;
3266 state = state->next) {
3267 if (state->mid == req->mid) {
3268 break;
3272 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3273 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3274 END_PROFILE(SMBnttranss);
3275 return;
3278 /* Revise state->total_param and state->total_data in case they have
3279 changed downwards */
3280 if (IVAL(req->vwv+1, 1) < state->total_param) {
3281 state->total_param = IVAL(req->vwv+1, 1);
3283 if (IVAL(req->vwv+3, 1) < state->total_data) {
3284 state->total_data = IVAL(req->vwv+3, 1);
3287 pcnt = IVAL(req->vwv+5, 1);
3288 poff = IVAL(req->vwv+7, 1);
3289 pdisp = IVAL(req->vwv+9, 1);
3291 dcnt = IVAL(req->vwv+11, 1);
3292 doff = IVAL(req->vwv+13, 1);
3293 ddisp = IVAL(req->vwv+15, 1);
3295 state->received_param += pcnt;
3296 state->received_data += dcnt;
3298 if ((state->received_data > state->total_data) ||
3299 (state->received_param > state->total_param))
3300 goto bad_param;
3302 if (pcnt) {
3303 if (trans_oob(state->total_param, pdisp, pcnt)
3304 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3305 goto bad_param;
3307 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3310 if (dcnt) {
3311 if (trans_oob(state->total_data, ddisp, dcnt)
3312 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3313 goto bad_param;
3315 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3318 if ((state->received_param < state->total_param) ||
3319 (state->received_data < state->total_data)) {
3320 END_PROFILE(SMBnttranss);
3321 return;
3324 handle_nttrans(conn, state, req);
3326 DLIST_REMOVE(conn->pending_trans, state);
3327 SAFE_FREE(state->data);
3328 SAFE_FREE(state->param);
3329 TALLOC_FREE(state);
3330 END_PROFILE(SMBnttranss);
3331 return;
3333 bad_param:
3335 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3336 DLIST_REMOVE(conn->pending_trans, state);
3337 SAFE_FREE(state->data);
3338 SAFE_FREE(state->param);
3339 TALLOC_FREE(state);
3340 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3341 END_PROFILE(SMBnttranss);
3342 return;