utils: Fix up 14a533680245
[samba4-gss.git] / source3 / smbd / smb1_nttrans.c
blob001e3be6df99daaf9e94e09dd4e3fc6edc4efe5a
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
29 #include "auth.h"
30 #include "smbprofile.h"
31 #include "libsmb/libsmb.h"
32 #include "lib/util_ea.h"
33 #include "librpc/gen_ndr/ndr_quota.h"
34 #include "librpc/gen_ndr/ndr_security.h"
36 static char *nttrans_realloc(char **ptr, size_t size)
38 if (ptr==NULL) {
39 smb_panic("nttrans_realloc() called with NULL ptr");
42 *ptr = (char *)SMB_REALLOC(*ptr, size);
43 if(*ptr == NULL) {
44 return NULL;
46 memset(*ptr,'\0',size);
47 return *ptr;
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static void send_nt_replies(connection_struct *conn,
58 struct smb_request *req, NTSTATUS nt_error,
59 char *params, int paramsize,
60 char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
64 int useable_space;
65 char *pp = params;
66 char *pd = pdata;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 1;
69 int data_alignment_offset = 0;
70 struct smbXsrv_connection *xconn = req->xconn;
71 int max_send = xconn->smb1.sessions.max_send;
74 * If there genuinely are no parameters or data to send just send
75 * the empty packet.
78 if(params_to_send == 0 && data_to_send == 0) {
79 reply_smb1_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 (!smb1_srv_send(xconn,
87 (char *)req->outbuf,
88 true,
89 req->seqnum + 1,
90 IS_CONN_ENCRYPTED(conn))) {
91 exit_server_cleanly("send_nt_replies: smb1_srv_send 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_smb1_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 (!smb1_srv_send(xconn,
251 (char *)req->outbuf,
252 true,
253 req->seqnum + 1,
254 IS_CONN_ENCRYPTED(conn))) {
255 exit_server_cleanly("send_nt_replies: smb1_srv_send failed.");
258 TALLOC_FREE(req->outbuf);
260 pp += params_sent_thistime;
261 pd += data_sent_thistime;
263 params_to_send -= params_sent_thistime;
264 data_to_send -= data_sent_thistime;
267 * Sanity check
270 if(params_to_send < 0 || data_to_send < 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send, data_to_send));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname, connection_struct *conn,
283 struct smb_request *req, uint16_t *ppnum)
285 files_struct *fsp;
286 NTSTATUS status;
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name if present. */
291 while (fname[0] == '\\') {
292 fname++;
295 status = open_np_file(req, fname, &fsp);
296 if (!NT_STATUS_IS_OK(status)) {
297 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
298 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
299 ERRDOS, ERRbadpipe);
300 return;
302 reply_nterror(req, status);
303 return;
306 *ppnum = fsp->fnum;
307 return;
310 /****************************************************************************
311 Reply to an NT create and X call for pipes.
312 ****************************************************************************/
314 static void do_ntcreate_pipe_open(connection_struct *conn,
315 struct smb_request *req)
317 char *fname = NULL;
318 uint16_t pnum = FNUM_FIELD_INVALID;
319 char *p = NULL;
320 uint32_t flags = IVAL(req->vwv+3, 1);
321 TALLOC_CTX *ctx = talloc_tos();
323 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
325 if (!fname) {
326 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
327 ERRDOS, ERRbadpipe);
328 return;
330 nt_open_pipe(fname, conn, req, &pnum);
332 if (req->outbuf) {
333 /* error reply */
334 return;
338 * Deal with pipe return.
341 if (flags & EXTENDED_RESPONSE_REQUIRED) {
342 /* This is very strange. We
343 * return 50 words, but only set
344 * the wcnt to 42 ? It's definitely
345 * what happens on the wire....
347 reply_smb1_outbuf(req, 50, 0);
348 SCVAL(req->outbuf,smb_wct,42);
349 } else {
350 reply_smb1_outbuf(req, 34, 0);
353 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
354 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
356 p = (char *)req->outbuf + smb_vwv2;
357 p++;
358 SSVAL(p,0,pnum);
359 p += 2;
360 SIVAL(p,0,FILE_WAS_OPENED);
361 p += 4;
362 p += 32;
363 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
364 p += 20;
365 /* File type. */
366 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
367 /* Device state. */
368 SSVAL(p,2, 0x5FF); /* ? */
369 p += 4;
371 if (flags & EXTENDED_RESPONSE_REQUIRED) {
372 p += 25;
373 SIVAL(p,0,FILE_GENERIC_ALL);
375 * For pipes W2K3 seems to return
376 * 0x12019B next.
377 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
379 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
382 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
385 struct case_semantics_state {
386 connection_struct *conn;
387 bool case_sensitive;
388 bool case_preserve;
389 bool short_case_preserve;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state *state)
398 state->conn->case_sensitive = state->case_sensitive;
399 state->conn->case_preserve = state->case_preserve;
400 state->conn->short_case_preserve = state->short_case_preserve;
401 return 0;
404 /****************************************************************************
405 Save case semantics.
406 ****************************************************************************/
408 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
409 connection_struct *conn)
411 struct case_semantics_state *result;
413 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
414 return NULL;
417 result->conn = conn;
418 result->case_sensitive = conn->case_sensitive;
419 result->case_preserve = conn->case_preserve;
420 result->short_case_preserve = conn->short_case_preserve;
422 /* Set to POSIX. */
423 conn->case_sensitive = True;
424 conn->case_preserve = True;
425 conn->short_case_preserve = True;
427 talloc_set_destructor(result, restore_case_semantics);
429 return result;
433 * Calculate the full path name given a relative fid.
435 static NTSTATUS get_relative_fid_filename(connection_struct *conn,
436 struct smb_request *req,
437 uint16_t root_dir_fid,
438 char *path,
439 char **path_out)
441 struct files_struct *dir_fsp = NULL;
442 char *new_path = NULL;
444 if (root_dir_fid == 0 || path == NULL) {
445 return NT_STATUS_INTERNAL_ERROR;
448 dir_fsp = file_fsp(req, root_dir_fid);
449 if (dir_fsp == NULL) {
450 return NT_STATUS_INVALID_HANDLE;
453 if (fsp_is_alternate_stream(dir_fsp)) {
454 return NT_STATUS_INVALID_HANDLE;
457 if (!dir_fsp->fsp_flags.is_directory) {
459 * Check to see if this is a mac fork of some kind.
461 if (conn->fs_capabilities & FILE_NAMED_STREAMS) {
462 char *stream = NULL;
464 stream = strchr_m(path, ':');
465 if (stream != NULL) {
466 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
471 * We need to handle the case when we get a relative open
472 * relative to a file and the pathname is blank - this is a
473 * reopen! (hint from demyn plantenberg)
475 return NT_STATUS_INVALID_HANDLE;
478 if (ISDOT(dir_fsp->fsp_name->base_name)) {
480 * We're at the toplevel dir, the final file name
481 * must not contain ./, as this is filtered out
482 * normally by srvstr_get_path and unix_convert
483 * explicitly rejects paths containing ./.
485 new_path = talloc_strdup(talloc_tos(), path);
486 } else {
488 * Copy in the base directory name.
491 new_path = talloc_asprintf(talloc_tos(),
492 "%s/%s",
493 dir_fsp->fsp_name->base_name,
494 path);
496 if (new_path == NULL) {
497 return NT_STATUS_NO_MEMORY;
500 *path_out = new_path;
501 return NT_STATUS_OK;
504 /****************************************************************************
505 Reply to an NT create and X call.
506 ****************************************************************************/
508 void reply_ntcreate_and_X(struct smb_request *req)
510 connection_struct *conn = req->conn;
511 struct files_struct *dirfsp = NULL;
512 struct smb_filename *smb_fname = NULL;
513 char *fname = NULL;
514 uint32_t flags;
515 uint32_t access_mask;
516 uint32_t file_attributes;
517 uint32_t share_access;
518 uint32_t create_disposition;
519 uint32_t create_options;
520 uint16_t root_dir_fid;
521 uint64_t allocation_size;
522 /* Breakout the oplock request bits so we can set the
523 reply bits separately. */
524 uint32_t fattr=0;
525 off_t file_len = 0;
526 int info = 0;
527 files_struct *fsp = NULL;
528 char *p = NULL;
529 struct timespec create_timespec;
530 struct timespec c_timespec;
531 struct timespec a_timespec;
532 struct timespec m_timespec;
533 NTSTATUS status;
534 int oplock_request;
535 uint8_t oplock_granted = NO_OPLOCK_RETURN;
536 struct case_semantics_state *case_state = NULL;
537 uint32_t ucf_flags;
538 NTTIME twrp = 0;
539 TALLOC_CTX *ctx = talloc_tos();
541 START_PROFILE(SMBntcreateX);
543 if (req->wct < 24) {
544 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
545 goto out;
548 flags = IVAL(req->vwv+3, 1);
549 access_mask = IVAL(req->vwv+7, 1);
550 file_attributes = IVAL(req->vwv+13, 1);
551 share_access = IVAL(req->vwv+15, 1);
552 create_disposition = IVAL(req->vwv+17, 1);
553 create_options = IVAL(req->vwv+19, 1);
554 root_dir_fid = (uint16_t)IVAL(req->vwv+5, 1);
556 allocation_size = BVAL(req->vwv+9, 1);
558 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
559 STR_TERMINATE, &status);
561 if (!NT_STATUS_IS_OK(status)) {
562 reply_nterror(req, status);
563 goto out;
566 DBG_DEBUG("flags = 0x%" PRIx32 ", access_mask = 0x%" PRIx32
567 ", file_attributes = 0x%" PRIx32
568 ", share_access = 0x%" PRIx32
569 ", create_disposition = 0x%" PRIx32
570 ", create_options = 0x%" PRIx32 ", root_dir_fid = 0x%" PRIx32
571 ", fname = %s\n",
572 flags,
573 access_mask,
574 file_attributes,
575 share_access,
576 create_disposition,
577 create_options,
578 root_dir_fid,
579 fname);
582 * we need to remove ignored bits when they come directly from the client
583 * because we reuse some of them for internal stuff
585 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
588 * If it's an IPC, use the pipe handler.
591 if (IS_IPC(conn)) {
592 if (lp_nt_pipe_support()) {
593 do_ntcreate_pipe_open(conn, req);
594 goto out;
596 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
597 goto out;
600 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
601 if (oplock_request) {
602 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
603 ? BATCH_OPLOCK : 0;
606 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
607 case_state = set_posix_case_semantics(ctx, conn);
608 if (!case_state) {
609 reply_nterror(req, NT_STATUS_NO_MEMORY);
610 goto out;
614 if (root_dir_fid != 0) {
615 char *new_fname = NULL;
617 status = get_relative_fid_filename(conn,
618 req,
619 root_dir_fid,
620 fname,
621 &new_fname);
622 if (!NT_STATUS_IS_OK(status)) {
623 reply_nterror(req, status);
624 goto out;
626 fname = new_fname;
629 ucf_flags = filename_create_ucf_flags(req, create_disposition);
630 if (ucf_flags & UCF_GMT_PATHNAME) {
631 extract_snapshot_token(fname, &twrp);
633 status = smb1_strip_dfs_path(ctx, &ucf_flags, &fname);
634 if (!NT_STATUS_IS_OK(status)) {
635 reply_nterror(req, status);
636 goto out;
639 status = filename_convert_dirfsp(
640 ctx, conn, fname, ucf_flags, twrp, &dirfsp, &smb_fname);
642 TALLOC_FREE(case_state);
644 if (!NT_STATUS_IS_OK(status)) {
645 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
646 reply_botherror(req,
647 NT_STATUS_PATH_NOT_COVERED,
648 ERRSRV, ERRbadpath);
649 goto out;
651 reply_nterror(req, status);
652 goto out;
656 * Bug #6898 - clients using Windows opens should
657 * never be able to set this attribute into the
658 * VFS.
660 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
662 status = SMB_VFS_CREATE_FILE(
663 conn, /* conn */
664 req, /* req */
665 dirfsp, /* dirfsp */
666 smb_fname, /* fname */
667 access_mask, /* access_mask */
668 share_access, /* share_access */
669 create_disposition, /* create_disposition*/
670 create_options, /* create_options */
671 file_attributes, /* file_attributes */
672 oplock_request, /* oplock_request */
673 NULL, /* lease */
674 allocation_size, /* allocation_size */
675 0, /* private_flags */
676 NULL, /* sd */
677 NULL, /* ea_list */
678 &fsp, /* result */
679 &info, /* pinfo */
680 NULL, NULL); /* create context */
682 if (!NT_STATUS_IS_OK(status)) {
683 if (open_was_deferred(req->xconn, req->mid)) {
684 /* We have re-scheduled this call, no error. */
685 goto out;
687 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
688 bool ok = defer_smb1_sharing_violation(req);
689 if (ok) {
690 goto out;
693 reply_openerror(req, status);
694 goto out;
697 /* Ensure we're pointing at the correct stat struct. */
698 smb_fname = fsp->fsp_name;
701 * If the caller set the extended oplock request bit
702 * and we granted one (by whatever means) - set the
703 * correct bit for extended oplock reply.
706 if (oplock_request &&
707 (lp_fake_oplocks(SNUM(conn))
708 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
711 * Exclusive oplock granted
714 if (flags & REQUEST_BATCH_OPLOCK) {
715 oplock_granted = BATCH_OPLOCK_RETURN;
716 } else {
717 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
719 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
720 oplock_granted = LEVEL_II_OPLOCK_RETURN;
721 } else {
722 oplock_granted = NO_OPLOCK_RETURN;
725 file_len = smb_fname->st.st_ex_size;
727 if (flags & EXTENDED_RESPONSE_REQUIRED) {
728 /* This is very strange. We
729 * return 50 words, but only set
730 * the wcnt to 42 ? It's definitely
731 * what happens on the wire....
733 reply_smb1_outbuf(req, 50, 0);
734 SCVAL(req->outbuf,smb_wct,42);
735 } else {
736 reply_smb1_outbuf(req, 34, 0);
739 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
740 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
742 p = (char *)req->outbuf + smb_vwv2;
744 SCVAL(p, 0, oplock_granted);
746 p++;
747 SSVAL(p,0,fsp->fnum);
748 p += 2;
749 if ((create_disposition == FILE_SUPERSEDE)
750 && (info == FILE_WAS_OVERWRITTEN)) {
751 SIVAL(p,0,FILE_WAS_SUPERSEDED);
752 } else {
753 SIVAL(p,0,info);
755 p += 4;
757 fattr = fdos_mode(fsp);
758 if (fattr == 0) {
759 fattr = FILE_ATTRIBUTE_NORMAL;
762 /* Create time. */
763 create_timespec = get_create_timespec(conn, fsp, smb_fname);
764 a_timespec = smb_fname->st.st_ex_atime;
765 m_timespec = smb_fname->st.st_ex_mtime;
766 c_timespec = get_change_timespec(conn, fsp, smb_fname);
768 if (lp_dos_filetime_resolution(SNUM(conn))) {
769 dos_filetime_timespec(&create_timespec);
770 dos_filetime_timespec(&a_timespec);
771 dos_filetime_timespec(&m_timespec);
772 dos_filetime_timespec(&c_timespec);
775 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
776 p += 8;
777 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
778 p += 8;
779 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
780 p += 8;
781 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
782 p += 8;
783 SIVAL(p,0,fattr); /* File Attributes. */
784 p += 4;
785 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
786 p += 8;
787 SOFF_T(p,0,file_len);
788 p += 8;
789 if (flags & EXTENDED_RESPONSE_REQUIRED) {
790 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
791 unsigned int num_streams = 0;
792 struct stream_struct *streams = NULL;
794 if (lp_ea_support(SNUM(conn))) {
795 size_t num_names = 0;
796 /* Do we have any EA's ? */
797 status = get_ea_names_from_fsp(
798 ctx, smb_fname->fsp, NULL, &num_names);
799 if (NT_STATUS_IS_OK(status) && num_names) {
800 file_status &= ~NO_EAS;
804 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
805 &num_streams, &streams);
806 /* There is always one stream, ::$DATA. */
807 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
808 file_status &= ~NO_SUBSTREAMS;
810 TALLOC_FREE(streams);
811 SSVAL(p,2,file_status);
813 p += 4;
814 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
816 if (flags & EXTENDED_RESPONSE_REQUIRED) {
817 uint32_t perms = 0;
818 p += 25;
819 if (fsp->fsp_flags.is_directory ||
820 fsp->fsp_flags.can_write ||
821 can_write_to_fsp(fsp))
823 perms = FILE_GENERIC_ALL;
824 } else {
825 perms = FILE_GENERIC_READ|FILE_EXECUTE;
827 SIVAL(p,0,perms);
830 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
831 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
833 out:
834 END_PROFILE(SMBntcreateX);
835 return;
838 /****************************************************************************
839 Reply to a NT_TRANSACT_CREATE call to open a pipe.
840 ****************************************************************************/
842 static void do_nt_transact_create_pipe(connection_struct *conn,
843 struct smb_request *req,
844 uint16_t **ppsetup, uint32_t setup_count,
845 char **ppparams, uint32_t parameter_count,
846 char **ppdata, uint32_t data_count)
848 char *fname = NULL;
849 char *params = *ppparams;
850 uint16_t pnum = FNUM_FIELD_INVALID;
851 char *p = NULL;
852 NTSTATUS status;
853 size_t param_len;
854 uint32_t flags;
855 TALLOC_CTX *ctx = talloc_tos();
858 * Ensure minimum number of parameters sent.
861 if(parameter_count < 54) {
862 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
863 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
864 return;
867 flags = IVAL(params,0);
869 if (req->posix_pathnames) {
870 srvstr_get_path_posix(ctx,
871 params,
872 req->flags2,
873 &fname,
874 params+53,
875 parameter_count-53,
876 STR_TERMINATE,
877 &status);
878 } else {
879 srvstr_get_path(ctx,
880 params,
881 req->flags2,
882 &fname,
883 params+53,
884 parameter_count-53,
885 STR_TERMINATE,
886 &status);
888 if (!NT_STATUS_IS_OK(status)) {
889 reply_nterror(req, status);
890 return;
893 nt_open_pipe(fname, conn, req, &pnum);
895 if (req->outbuf) {
896 /* Error return */
897 return;
900 /* Realloc the size of parameters and data we will return */
901 if (flags & EXTENDED_RESPONSE_REQUIRED) {
902 /* Extended response is 32 more byyes. */
903 param_len = 101;
904 } else {
905 param_len = 69;
907 params = nttrans_realloc(ppparams, param_len);
908 if(params == NULL) {
909 reply_nterror(req, NT_STATUS_NO_MEMORY);
910 return;
913 p = params;
914 SCVAL(p,0,NO_OPLOCK_RETURN);
916 p += 2;
917 SSVAL(p,0,pnum);
918 p += 2;
919 SIVAL(p,0,FILE_WAS_OPENED);
920 p += 8;
922 p += 32;
923 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
924 p += 20;
925 /* File type. */
926 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
927 /* Device state. */
928 SSVAL(p,2, 0x5FF); /* ? */
929 p += 4;
931 if (flags & EXTENDED_RESPONSE_REQUIRED) {
932 p += 25;
933 SIVAL(p,0,FILE_GENERIC_ALL);
935 * For pipes W2K3 seems to return
936 * 0x12019B next.
937 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
939 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
942 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
944 /* Send the required number of replies */
945 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
947 return;
950 /****************************************************************************
951 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
952 ****************************************************************************/
954 static void call_nt_transact_create(connection_struct *conn,
955 struct smb_request *req,
956 uint16_t **ppsetup, uint32_t setup_count,
957 char **ppparams, uint32_t parameter_count,
958 char **ppdata, uint32_t data_count,
959 uint32_t max_data_count)
961 struct smb_filename *smb_fname = NULL;
962 char *fname = NULL;
963 char *params = *ppparams;
964 char *data = *ppdata;
965 /* Breakout the oplock request bits so we can set the reply bits separately. */
966 uint32_t fattr=0;
967 off_t file_len = 0;
968 int info = 0;
969 struct files_struct *dirfsp = NULL;
970 files_struct *fsp = NULL;
971 char *p = NULL;
972 uint32_t flags;
973 uint32_t access_mask;
974 uint32_t file_attributes;
975 uint32_t share_access;
976 uint32_t create_disposition;
977 uint32_t create_options;
978 uint32_t sd_len;
979 struct security_descriptor *sd = NULL;
980 uint32_t ea_len;
981 uint16_t 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 ea_list *ea_list = NULL;
987 NTSTATUS status;
988 size_t param_len;
989 uint64_t allocation_size;
990 int oplock_request;
991 uint8_t oplock_granted;
992 struct case_semantics_state *case_state = NULL;
993 uint32_t ucf_flags;
994 NTTIME twrp = 0;
995 TALLOC_CTX *ctx = talloc_tos();
997 DEBUG(5,("call_nt_transact_create\n"));
1000 * If it's an IPC, use the pipe handler.
1003 if (IS_IPC(conn)) {
1004 if (lp_nt_pipe_support()) {
1005 do_nt_transact_create_pipe(
1006 conn, req,
1007 ppsetup, setup_count,
1008 ppparams, parameter_count,
1009 ppdata, data_count);
1010 goto out;
1012 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1013 goto out;
1017 * Ensure minimum number of parameters sent.
1020 if(parameter_count < 54) {
1021 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1022 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1023 goto out;
1026 flags = IVAL(params,0);
1027 access_mask = IVAL(params,8);
1028 file_attributes = IVAL(params,20);
1029 share_access = IVAL(params,24);
1030 create_disposition = IVAL(params,28);
1031 create_options = IVAL(params,32);
1032 sd_len = IVAL(params,36);
1033 ea_len = IVAL(params,40);
1034 root_dir_fid = (uint16_t)IVAL(params,4);
1035 allocation_size = BVAL(params,12);
1038 * we need to remove ignored bits when they come directly from the client
1039 * because we reuse some of them for internal stuff
1041 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1043 if (req->posix_pathnames) {
1044 srvstr_get_path_posix(ctx,
1045 params,
1046 req->flags2,
1047 &fname,
1048 params+53,
1049 parameter_count-53,
1050 STR_TERMINATE,
1051 &status);
1052 } else {
1053 srvstr_get_path(ctx,
1054 params,
1055 req->flags2,
1056 &fname,
1057 params+53,
1058 parameter_count-53,
1059 STR_TERMINATE,
1060 &status);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 reply_nterror(req, status);
1064 goto out;
1067 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1068 case_state = set_posix_case_semantics(ctx, conn);
1069 if (!case_state) {
1070 reply_nterror(req, NT_STATUS_NO_MEMORY);
1071 goto out;
1075 if (root_dir_fid != 0) {
1076 char *new_fname = NULL;
1078 status = get_relative_fid_filename(conn,
1079 req,
1080 root_dir_fid,
1081 fname,
1082 &new_fname);
1083 if (!NT_STATUS_IS_OK(status)) {
1084 reply_nterror(req, status);
1085 goto out;
1087 fname = new_fname;
1090 ucf_flags = filename_create_ucf_flags(req, create_disposition);
1091 if (ucf_flags & UCF_GMT_PATHNAME) {
1092 extract_snapshot_token(fname, &twrp);
1094 status = smb1_strip_dfs_path(ctx, &ucf_flags, &fname);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 reply_nterror(req, status);
1097 goto out;
1100 status = filename_convert_dirfsp(ctx,
1101 conn,
1102 fname,
1103 ucf_flags,
1104 twrp,
1105 &dirfsp,
1106 &smb_fname);
1108 TALLOC_FREE(case_state);
1110 if (!NT_STATUS_IS_OK(status)) {
1111 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1112 reply_botherror(req,
1113 NT_STATUS_PATH_NOT_COVERED,
1114 ERRSRV, ERRbadpath);
1115 goto out;
1117 reply_nterror(req, status);
1118 goto out;
1121 /* Ensure the data_len is correct for the sd and ea values given. */
1122 if ((ea_len + sd_len > data_count)
1123 || (ea_len > data_count) || (sd_len > data_count)
1124 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1125 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1126 "%u, data_count = %u\n", (unsigned int)ea_len,
1127 (unsigned int)sd_len, (unsigned int)data_count));
1128 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1129 goto out;
1132 if (sd_len) {
1133 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1134 sd_len));
1136 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1137 &sd);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 DEBUG(10, ("call_nt_transact_create: "
1140 "unmarshall_sec_desc failed: %s\n",
1141 nt_errstr(status)));
1142 reply_nterror(req, status);
1143 goto out;
1147 if (ea_len) {
1148 if (!lp_ea_support(SNUM(conn))) {
1149 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1150 "EA's not supported.\n",
1151 (unsigned int)ea_len));
1152 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1153 goto out;
1156 if (ea_len < 10) {
1157 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1158 "too small (should be more than 10)\n",
1159 (unsigned int)ea_len ));
1160 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1161 goto out;
1164 /* We have already checked that ea_len <= data_count here. */
1165 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1166 ea_len);
1167 if (ea_list == NULL) {
1168 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1169 goto out;
1172 if (!req->posix_pathnames &&
1173 ea_list_has_invalid_name(ea_list)) {
1174 /* Realloc the size of parameters and data we will return */
1175 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1176 /* Extended response is 32 more bytes. */
1177 param_len = 101;
1178 } else {
1179 param_len = 69;
1181 params = nttrans_realloc(ppparams, param_len);
1182 if(params == NULL) {
1183 reply_nterror(req, NT_STATUS_NO_MEMORY);
1184 goto out;
1187 memset(params, '\0', param_len);
1188 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1189 params, param_len, NULL, 0);
1190 goto out;
1194 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1195 if (oplock_request) {
1196 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1197 ? BATCH_OPLOCK : 0;
1201 * Bug #6898 - clients using Windows opens should
1202 * never be able to set this attribute into the
1203 * VFS.
1205 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1207 status = SMB_VFS_CREATE_FILE(
1208 conn, /* conn */
1209 req, /* req */
1210 dirfsp, /* dirfsp */
1211 smb_fname, /* fname */
1212 access_mask, /* access_mask */
1213 share_access, /* share_access */
1214 create_disposition, /* create_disposition*/
1215 create_options, /* create_options */
1216 file_attributes, /* file_attributes */
1217 oplock_request, /* oplock_request */
1218 NULL, /* lease */
1219 allocation_size, /* allocation_size */
1220 0, /* private_flags */
1221 sd, /* sd */
1222 ea_list, /* ea_list */
1223 &fsp, /* result */
1224 &info, /* pinfo */
1225 NULL, NULL); /* create context */
1227 if(!NT_STATUS_IS_OK(status)) {
1228 if (open_was_deferred(req->xconn, req->mid)) {
1229 /* We have re-scheduled this call, no error. */
1230 return;
1232 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1233 bool ok = defer_smb1_sharing_violation(req);
1234 if (ok) {
1235 return;
1238 reply_openerror(req, status);
1239 goto out;
1242 /* Ensure we're pointing at the correct stat struct. */
1243 TALLOC_FREE(smb_fname);
1244 smb_fname = fsp->fsp_name;
1247 * If the caller set the extended oplock request bit
1248 * and we granted one (by whatever means) - set the
1249 * correct bit for extended oplock reply.
1252 if (oplock_request &&
1253 (lp_fake_oplocks(SNUM(conn))
1254 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1257 * Exclusive oplock granted
1260 if (flags & REQUEST_BATCH_OPLOCK) {
1261 oplock_granted = BATCH_OPLOCK_RETURN;
1262 } else {
1263 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1265 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1266 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1267 } else {
1268 oplock_granted = NO_OPLOCK_RETURN;
1271 file_len = smb_fname->st.st_ex_size;
1273 /* Realloc the size of parameters and data we will return */
1274 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1275 /* Extended response is 32 more byyes. */
1276 param_len = 101;
1277 } else {
1278 param_len = 69;
1280 params = nttrans_realloc(ppparams, param_len);
1281 if(params == NULL) {
1282 reply_nterror(req, NT_STATUS_NO_MEMORY);
1283 goto out;
1286 p = params;
1287 SCVAL(p, 0, oplock_granted);
1289 p += 2;
1290 SSVAL(p,0,fsp->fnum);
1291 p += 2;
1292 if ((create_disposition == FILE_SUPERSEDE)
1293 && (info == FILE_WAS_OVERWRITTEN)) {
1294 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1295 } else {
1296 SIVAL(p,0,info);
1298 p += 8;
1300 fattr = fdos_mode(fsp);
1301 if (fattr == 0) {
1302 fattr = FILE_ATTRIBUTE_NORMAL;
1305 /* Create time. */
1306 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1307 a_timespec = smb_fname->st.st_ex_atime;
1308 m_timespec = smb_fname->st.st_ex_mtime;
1309 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1311 if (lp_dos_filetime_resolution(SNUM(conn))) {
1312 dos_filetime_timespec(&create_timespec);
1313 dos_filetime_timespec(&a_timespec);
1314 dos_filetime_timespec(&m_timespec);
1315 dos_filetime_timespec(&c_timespec);
1318 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
1319 p += 8;
1320 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
1321 p += 8;
1322 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
1323 p += 8;
1324 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
1325 p += 8;
1326 SIVAL(p,0,fattr); /* File Attributes. */
1327 p += 4;
1328 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1329 p += 8;
1330 SOFF_T(p,0,file_len);
1331 p += 8;
1332 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1333 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1334 unsigned int num_streams = 0;
1335 struct stream_struct *streams = NULL;
1337 if (lp_ea_support(SNUM(conn))) {
1338 size_t num_names = 0;
1339 /* Do we have any EA's ? */
1340 status = get_ea_names_from_fsp(
1341 ctx, smb_fname->fsp, NULL, &num_names);
1342 if (NT_STATUS_IS_OK(status) && num_names) {
1343 file_status &= ~NO_EAS;
1347 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
1348 &num_streams, &streams);
1349 /* There is always one stream, ::$DATA. */
1350 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1351 file_status &= ~NO_SUBSTREAMS;
1353 TALLOC_FREE(streams);
1354 SSVAL(p,2,file_status);
1356 p += 4;
1357 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
1359 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1360 uint32_t perms = 0;
1361 p += 25;
1362 if (fsp->fsp_flags.is_directory ||
1363 fsp->fsp_flags.can_write ||
1364 can_write_to_fsp(fsp))
1366 perms = FILE_GENERIC_ALL;
1367 } else {
1368 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1370 SIVAL(p,0,perms);
1373 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1374 smb_fname_str_dbg(smb_fname)));
1376 /* Send the required number of replies */
1377 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1378 out:
1379 return;
1382 /****************************************************************************
1383 Reply to a NT CANCEL request.
1384 conn POINTER CAN BE NULL HERE !
1385 ****************************************************************************/
1387 void reply_ntcancel(struct smb_request *req)
1389 struct smbXsrv_connection *xconn = req->xconn;
1390 struct smbd_server_connection *sconn = req->sconn;
1391 bool found;
1394 * Go through and cancel any pending change notifies.
1397 START_PROFILE(SMBntcancel);
1398 smb1_srv_cancel_sign_response(xconn);
1399 found = remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1400 if (!found) {
1401 smbd_smb1_brl_finish_by_mid(sconn, req->mid);
1404 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1405 (unsigned long long)req->mid));
1407 END_PROFILE(SMBntcancel);
1408 return;
1411 /****************************************************************************
1412 Reply to a NT rename request.
1413 ****************************************************************************/
1415 void reply_ntrename(struct smb_request *req)
1417 connection_struct *conn = req->conn;
1418 struct files_struct *src_dirfsp = NULL;
1419 struct smb_filename *smb_fname_old = NULL;
1420 struct files_struct *dst_dirfsp = NULL;
1421 struct smb_filename *smb_fname_new = NULL;
1422 char *oldname = NULL;
1423 char *newname = NULL;
1424 const char *dst_original_lcomp = NULL;
1425 const char *p;
1426 NTSTATUS status;
1427 uint32_t attrs;
1428 uint32_t ucf_flags_src = ucf_flags_from_smb_request(req);
1429 NTTIME src_twrp = 0;
1430 uint32_t ucf_flags_dst = ucf_flags_from_smb_request(req);
1431 NTTIME dst_twrp = 0;
1432 uint16_t rename_type;
1433 TALLOC_CTX *ctx = talloc_tos();
1434 bool stream_rename = false;
1436 START_PROFILE(SMBntrename);
1438 if (req->wct < 4) {
1439 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1440 goto out;
1443 attrs = SVAL(req->vwv+0, 0);
1444 rename_type = SVAL(req->vwv+1, 0);
1446 p = (const char *)req->buf + 1;
1447 p += srvstr_get_path_req(ctx, req, &oldname, p, STR_TERMINATE,
1448 &status);
1449 if (!NT_STATUS_IS_OK(status)) {
1450 reply_nterror(req, status);
1451 goto out;
1454 if (!req->posix_pathnames && ms_has_wild(oldname)) {
1455 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1456 goto out;
1459 p++;
1460 p += srvstr_get_path_req(ctx, req, &newname, p, STR_TERMINATE,
1461 &status);
1462 if (!NT_STATUS_IS_OK(status)) {
1463 reply_nterror(req, status);
1464 goto out;
1467 if (!req->posix_pathnames && ms_has_wild(newname)) {
1468 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1469 goto out;
1472 if (!req->posix_pathnames) {
1473 /* The newname must begin with a ':' if the
1474 oldname contains a ':'. */
1475 if (strchr_m(oldname, ':')) {
1476 if (newname[0] != ':') {
1477 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1478 goto out;
1480 stream_rename = true;
1484 if (ucf_flags_src & UCF_GMT_PATHNAME) {
1485 extract_snapshot_token(oldname, &src_twrp);
1487 status = smb1_strip_dfs_path(ctx, &ucf_flags_src, &oldname);
1488 if (!NT_STATUS_IS_OK(status)) {
1489 reply_nterror(req, status);
1490 goto out;
1493 status = filename_convert_dirfsp(ctx,
1494 conn,
1495 oldname,
1496 ucf_flags_src,
1497 src_twrp,
1498 &src_dirfsp,
1499 &smb_fname_old);
1500 if (!NT_STATUS_IS_OK(status)) {
1501 if (NT_STATUS_EQUAL(status,
1502 NT_STATUS_PATH_NOT_COVERED)) {
1503 reply_botherror(req,
1504 NT_STATUS_PATH_NOT_COVERED,
1505 ERRSRV, ERRbadpath);
1506 goto out;
1508 reply_nterror(req, status);
1509 goto out;
1512 if (stream_rename) {
1514 * No point in calling filename_convert()
1515 * on a raw stream name. It can never find
1516 * the file anyway. Use the same logic as
1517 * SMB2_FILE_RENAME_INFORMATION_INTERNAL
1518 * and generate smb_fname_new directly.
1520 smb_fname_new = synthetic_smb_fname(talloc_tos(),
1521 smb_fname_old->base_name,
1522 newname,
1523 NULL,
1524 smb_fname_old->twrp,
1525 smb_fname_old->flags);
1526 if (smb_fname_new == NULL) {
1527 reply_nterror(req, NT_STATUS_NO_MEMORY);
1528 goto out;
1530 } else {
1531 if (ucf_flags_dst & UCF_GMT_PATHNAME) {
1532 extract_snapshot_token(newname,
1533 &dst_twrp);
1535 status = smb1_strip_dfs_path(ctx, &ucf_flags_dst, &newname);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 reply_nterror(req, status);
1538 goto out;
1540 status = filename_convert_dirfsp(ctx,
1541 conn,
1542 newname,
1543 ucf_flags_dst,
1544 dst_twrp,
1545 &dst_dirfsp,
1546 &smb_fname_new);
1547 if (!NT_STATUS_IS_OK(status)) {
1548 if (NT_STATUS_EQUAL(status,
1549 NT_STATUS_PATH_NOT_COVERED)) {
1550 reply_botherror(req,
1551 NT_STATUS_PATH_NOT_COVERED,
1552 ERRSRV, ERRbadpath);
1553 goto out;
1555 reply_nterror(req, status);
1556 goto out;
1560 /* Get the last component of the destination for rename_internals(). */
1561 dst_original_lcomp = get_original_lcomp(ctx,
1562 conn,
1563 newname,
1564 ucf_flags_dst);
1565 if (dst_original_lcomp == NULL) {
1566 reply_nterror(req, NT_STATUS_NO_MEMORY);
1567 goto out;
1571 DEBUG(3,("reply_ntrename: %s -> %s\n",
1572 smb_fname_str_dbg(smb_fname_old),
1573 smb_fname_str_dbg(smb_fname_new)));
1575 switch(rename_type) {
1576 case RENAME_FLAG_RENAME:
1577 status = rename_internals(ctx,
1578 conn,
1579 req,
1580 src_dirfsp,
1581 smb_fname_old,
1582 smb_fname_new,
1583 dst_original_lcomp,
1584 attrs,
1585 false,
1586 DELETE_ACCESS);
1587 break;
1588 case RENAME_FLAG_HARD_LINK:
1589 status = hardlink_internals(ctx,
1590 conn,
1591 req,
1592 false,
1593 smb_fname_old,
1594 smb_fname_new);
1595 break;
1596 case RENAME_FLAG_COPY:
1597 status = copy_internals(ctx,
1598 conn,
1599 req,
1600 src_dirfsp,
1601 smb_fname_old,
1602 dst_dirfsp,
1603 smb_fname_new,
1604 attrs);
1605 break;
1606 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1607 status = NT_STATUS_INVALID_PARAMETER;
1608 break;
1609 default:
1610 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1611 break;
1614 if (!NT_STATUS_IS_OK(status)) {
1615 if (open_was_deferred(req->xconn, req->mid)) {
1616 /* We have re-scheduled this call. */
1617 goto out;
1619 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1620 bool ok = defer_smb1_sharing_violation(req);
1621 if (ok) {
1622 goto out;
1626 reply_nterror(req, status);
1627 goto out;
1630 reply_smb1_outbuf(req, 0, 0);
1631 out:
1632 END_PROFILE(SMBntrename);
1633 return;
1636 /****************************************************************************
1637 Reply to a notify change - queue the request and
1638 don't allow a directory to be opened.
1639 ****************************************************************************/
1641 static void smbd_smb1_notify_reply(struct smb_request *req,
1642 NTSTATUS error_code,
1643 uint8_t *buf, size_t len)
1645 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1648 static void call_nt_transact_notify_change(connection_struct *conn,
1649 struct smb_request *req,
1650 uint16_t **ppsetup,
1651 uint32_t setup_count,
1652 char **ppparams,
1653 uint32_t parameter_count,
1654 char **ppdata, uint32_t data_count,
1655 uint32_t max_data_count,
1656 uint32_t max_param_count)
1658 uint16_t *setup = *ppsetup;
1659 files_struct *fsp;
1660 uint32_t filter;
1661 NTSTATUS status;
1662 bool recursive;
1664 if(setup_count < 6) {
1665 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1666 return;
1669 fsp = file_fsp(req, SVAL(setup,4));
1670 filter = IVAL(setup, 0);
1671 recursive = (SVAL(setup, 6) != 0) ? True : False;
1673 DEBUG(3,("call_nt_transact_notify_change\n"));
1675 if(!fsp) {
1676 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1677 return;
1680 DBG_NOTICE("notify change called on %s, filter = %s, recursive = %d\n",
1681 fsp_str_dbg(fsp),
1682 notify_filter_string(talloc_tos(), filter),
1683 recursive);
1685 if((!fsp->fsp_flags.is_directory) || (conn != fsp->conn)) {
1686 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1687 return;
1690 if (fsp->notify == NULL) {
1692 status = change_notify_create(fsp,
1693 max_param_count,
1694 filter,
1695 recursive);
1696 if (!NT_STATUS_IS_OK(status)) {
1697 DEBUG(10, ("change_notify_create returned %s\n",
1698 nt_errstr(status)));
1699 reply_nterror(req, status);
1700 return;
1704 if (change_notify_fsp_has_changes(fsp)) {
1707 * We've got changes pending, respond immediately
1711 * TODO: write a torture test to check the filtering behaviour
1712 * here.
1715 change_notify_reply(req,
1716 NT_STATUS_OK,
1717 max_param_count,
1718 fsp->notify,
1719 smbd_smb1_notify_reply);
1722 * change_notify_reply() above has independently sent its
1723 * results
1725 return;
1729 * No changes pending, queue the request
1732 status = change_notify_add_request(req,
1733 max_param_count,
1734 filter,
1735 recursive, fsp,
1736 smbd_smb1_notify_reply);
1737 if (!NT_STATUS_IS_OK(status)) {
1738 reply_nterror(req, status);
1740 return;
1743 /****************************************************************************
1744 Reply to an NT transact rename command.
1745 ****************************************************************************/
1747 static void call_nt_transact_rename(connection_struct *conn,
1748 struct smb_request *req,
1749 uint16_t **ppsetup, uint32_t setup_count,
1750 char **ppparams, uint32_t parameter_count,
1751 char **ppdata, uint32_t data_count,
1752 uint32_t max_data_count)
1754 char *params = *ppparams;
1755 char *new_name = NULL;
1756 files_struct *fsp = NULL;
1757 NTSTATUS status;
1758 TALLOC_CTX *ctx = talloc_tos();
1760 if(parameter_count < 5) {
1761 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1762 return;
1765 fsp = file_fsp(req, SVAL(params, 0));
1766 if (!check_fsp(conn, req, fsp)) {
1767 return;
1769 if (req->posix_pathnames) {
1770 srvstr_get_path_posix(ctx,
1771 params,
1772 req->flags2,
1773 &new_name,
1774 params+4,
1775 parameter_count - 4,
1776 STR_TERMINATE,
1777 &status);
1778 } else {
1779 srvstr_get_path(ctx,
1780 params,
1781 req->flags2,
1782 &new_name,
1783 params+4,
1784 parameter_count - 4,
1785 STR_TERMINATE,
1786 &status);
1789 if (!NT_STATUS_IS_OK(status)) {
1790 reply_nterror(req, status);
1791 return;
1795 * W2K3 ignores this request as the RAW-RENAME test
1796 * demonstrates, so we do.
1798 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1800 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1801 fsp_str_dbg(fsp), new_name));
1803 return;
1806 /****************************************************************************
1807 SMB1 reply to query a security descriptor.
1808 ****************************************************************************/
1810 static void call_nt_transact_query_security_desc(connection_struct *conn,
1811 struct smb_request *req,
1812 uint16_t **ppsetup,
1813 uint32_t setup_count,
1814 char **ppparams,
1815 uint32_t parameter_count,
1816 char **ppdata,
1817 uint32_t data_count,
1818 uint32_t max_data_count)
1820 char *params = *ppparams;
1821 char *data = *ppdata;
1822 size_t sd_size = 0;
1823 uint32_t security_info_wanted;
1824 files_struct *fsp = NULL;
1825 NTSTATUS status;
1826 uint8_t *marshalled_sd = NULL;
1828 if(parameter_count < 8) {
1829 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1830 return;
1833 fsp = file_fsp(req, SVAL(params,0));
1834 if(!fsp) {
1835 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1836 return;
1839 security_info_wanted = IVAL(params,4);
1841 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1842 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1843 (unsigned int)security_info_wanted));
1845 params = nttrans_realloc(ppparams, 4);
1846 if(params == NULL) {
1847 reply_nterror(req, NT_STATUS_NO_MEMORY);
1848 return;
1852 * Get the permissions to return.
1855 status = smbd_do_query_security_desc(conn,
1856 talloc_tos(),
1857 fsp,
1858 security_info_wanted &
1859 SMB_SUPPORTED_SECINFO_FLAGS,
1860 max_data_count,
1861 &marshalled_sd,
1862 &sd_size);
1864 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1865 SIVAL(params,0,(uint32_t)sd_size);
1866 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1867 params, 4, NULL, 0);
1868 return;
1871 if (!NT_STATUS_IS_OK(status)) {
1872 reply_nterror(req, status);
1873 return;
1876 SMB_ASSERT(sd_size > 0);
1878 SIVAL(params,0,(uint32_t)sd_size);
1880 if (max_data_count < sd_size) {
1881 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1882 params, 4, NULL, 0);
1883 return;
1887 * Allocate the data we will return.
1890 data = nttrans_realloc(ppdata, sd_size);
1891 if(data == NULL) {
1892 reply_nterror(req, NT_STATUS_NO_MEMORY);
1893 return;
1896 memcpy(data, marshalled_sd, sd_size);
1898 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1900 return;
1903 /****************************************************************************
1904 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1905 ****************************************************************************/
1907 static void call_nt_transact_set_security_desc(connection_struct *conn,
1908 struct smb_request *req,
1909 uint16_t **ppsetup,
1910 uint32_t setup_count,
1911 char **ppparams,
1912 uint32_t parameter_count,
1913 char **ppdata,
1914 uint32_t data_count,
1915 uint32_t max_data_count)
1917 char *params= *ppparams;
1918 char *data = *ppdata;
1919 files_struct *fsp = NULL;
1920 uint32_t security_info_sent = 0;
1921 NTSTATUS status;
1923 if(parameter_count < 8) {
1924 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1925 return;
1928 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1929 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1930 return;
1933 if (!CAN_WRITE(fsp->conn)) {
1934 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1935 return;
1938 if(!lp_nt_acl_support(SNUM(conn))) {
1939 goto done;
1942 security_info_sent = IVAL(params,4);
1944 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1945 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1947 if (data_count == 0) {
1948 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1949 return;
1952 status = set_sd_blob(fsp, (uint8_t *)data, data_count,
1953 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
1954 if (!NT_STATUS_IS_OK(status)) {
1955 reply_nterror(req, status);
1956 return;
1959 done:
1960 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1961 return;
1964 /****************************************************************************
1965 Reply to NT IOCTL
1966 ****************************************************************************/
1968 static void call_nt_transact_ioctl(connection_struct *conn,
1969 struct smb_request *req,
1970 uint16_t **ppsetup, uint32_t setup_count,
1971 char **ppparams, uint32_t parameter_count,
1972 char **ppdata, uint32_t data_count,
1973 uint32_t max_data_count)
1975 NTSTATUS status;
1976 uint32_t function;
1977 uint16_t fidnum;
1978 files_struct *fsp;
1979 uint8_t isFSctl;
1980 uint8_t compfilter;
1981 char *out_data = NULL;
1982 uint32_t out_data_len = 0;
1983 char *pdata = *ppdata;
1984 TALLOC_CTX *ctx = talloc_tos();
1986 if (setup_count != 8) {
1987 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1988 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1989 return;
1992 function = IVAL(*ppsetup, 0);
1993 fidnum = SVAL(*ppsetup, 4);
1994 isFSctl = CVAL(*ppsetup, 6);
1995 compfilter = CVAL(*ppsetup, 7);
1997 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1998 function, fidnum, isFSctl, compfilter));
2000 fsp=file_fsp(req, fidnum);
2003 * We don't really implement IOCTLs, especially on files.
2005 if (!isFSctl) {
2006 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2007 isFSctl));
2008 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2009 return;
2012 /* Has to be for an open file! */
2013 if (!check_fsp_open(conn, req, fsp)) {
2014 return;
2018 * out_data might be allocated by the VFS module, but talloc should be
2019 * used, and should be cleaned up when the request ends.
2021 status = SMB_VFS_FSCTL(fsp,
2022 ctx,
2023 function,
2024 req->flags2,
2025 (uint8_t *)pdata,
2026 data_count,
2027 (uint8_t **)&out_data,
2028 max_data_count,
2029 &out_data_len);
2030 if (!NT_STATUS_IS_OK(status)) {
2031 reply_nterror(req, status);
2032 } else {
2033 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2038 #ifdef HAVE_SYS_QUOTAS
2039 /****************************************************************************
2040 Reply to get user quota
2041 ****************************************************************************/
2043 static void call_nt_transact_get_user_quota(connection_struct *conn,
2044 struct smb_request *req,
2045 uint16_t **ppsetup,
2046 uint32_t setup_count,
2047 char **ppparams,
2048 uint32_t parameter_count,
2049 char **ppdata,
2050 uint32_t data_count,
2051 uint32_t max_data_count)
2053 const struct loadparm_substitution *lp_sub =
2054 loadparm_s3_global_substitution();
2055 NTSTATUS nt_status = NT_STATUS_OK;
2056 char *params = *ppparams;
2057 char *pdata = *ppdata;
2058 int data_len = 0;
2059 int param_len = 0;
2060 files_struct *fsp = NULL;
2061 DATA_BLOB blob = data_blob_null;
2062 struct nttrans_query_quota_params info = {0};
2063 enum ndr_err_code err;
2064 TALLOC_CTX *tmp_ctx = NULL;
2065 uint32_t resp_len = 0;
2066 uint8_t *resp_data = 0;
2068 tmp_ctx = talloc_init("ntquota_list");
2069 if (!tmp_ctx) {
2070 nt_status = NT_STATUS_NO_MEMORY;
2071 goto error;
2074 /* access check */
2075 if (get_current_uid(conn) != sec_initial_uid()) {
2076 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2077 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2078 conn->session_info->unix_info->unix_name));
2079 nt_status = NT_STATUS_ACCESS_DENIED;
2080 goto error;
2083 blob.data = (uint8_t*)params;
2084 blob.length = parameter_count;
2086 err = ndr_pull_struct_blob(&blob, tmp_ctx, &info,
2087 (ndr_pull_flags_fn_t)ndr_pull_nttrans_query_quota_params);
2089 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2090 DEBUG(0,("TRANSACT_GET_USER_QUOTA: failed to pull "
2091 "query_quota_params.\n"));
2092 nt_status = NT_STATUS_INVALID_PARAMETER;
2093 goto error;
2095 DBG_DEBUG("info.return_single_entry = %u, info.restart_scan = %u, "
2096 "info.sid_list_length = %u, info.start_sid_length = %u, "
2097 "info.start_sid_offset = %u\n",
2098 (unsigned int)info.return_single_entry,
2099 (unsigned int)info.restart_scan,
2100 (unsigned int)info.sid_list_length,
2101 (unsigned int)info.start_sid_length,
2102 (unsigned int)info.start_sid_offset);
2104 /* set blob to point at data for further parsing */
2105 blob.data = (uint8_t*)pdata;
2106 blob.length = data_count;
2108 * Although MS-SMB ref is ambiguous here, a microsoft client will
2109 * only ever send a start sid (as part of a list) with
2110 * sid_list_length & start_sid_offset both set to the actual list
2111 * length. Note: Only a single result is returned in this case
2112 * In the case where either start_sid_offset or start_sid_length
2113 * are set alone or if both set (but have different values) then
2114 * it seems windows will return a number of entries from the start
2115 * of the list of users with quotas set. This behaviour is undocumented
2116 * and windows clients do not send messages of that type. As such we
2117 * currently will reject these requests.
2119 if (info.start_sid_length
2120 || (info.sid_list_length != info.start_sid_offset)) {
2121 DBG_ERR("TRANSACT_GET_USER_QUOTA: unsupported single or "
2122 "compound sid format\n");
2123 nt_status = NT_STATUS_INVALID_PARAMETER;
2124 goto error;
2127 /* maybe we can check the quota_fnum */
2128 fsp = file_fsp(req, info.fid);
2129 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2130 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2131 nt_status = NT_STATUS_INVALID_HANDLE;
2132 goto error;
2134 nt_status = smbd_do_query_getinfo_quota(tmp_ctx,
2135 fsp,
2136 info.restart_scan,
2137 info.return_single_entry,
2138 info.sid_list_length,
2139 &blob,
2140 max_data_count,
2141 &resp_data,
2142 &resp_len);
2143 if (!NT_STATUS_IS_OK(nt_status)) {
2144 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
2145 goto error;
2147 nt_status = NT_STATUS_OK;
2150 param_len = 4;
2151 params = nttrans_realloc(ppparams, param_len);
2152 if(params == NULL) {
2153 nt_status = NT_STATUS_NO_MEMORY;
2154 goto error;
2157 data_len = resp_len;
2158 SIVAL(params, 0, data_len);
2159 pdata = nttrans_realloc(ppdata, data_len);
2160 memcpy(pdata, resp_data, data_len);
2162 TALLOC_FREE(tmp_ctx);
2163 send_nt_replies(conn, req, nt_status, params, param_len,
2164 pdata, data_len);
2165 return;
2166 error:
2167 TALLOC_FREE(tmp_ctx);
2168 reply_nterror(req, nt_status);
2171 /****************************************************************************
2172 Reply to set user quota
2173 ****************************************************************************/
2175 static void call_nt_transact_set_user_quota(connection_struct *conn,
2176 struct smb_request *req,
2177 uint16_t **ppsetup,
2178 uint32_t setup_count,
2179 char **ppparams,
2180 uint32_t parameter_count,
2181 char **ppdata,
2182 uint32_t data_count,
2183 uint32_t max_data_count)
2185 const struct loadparm_substitution *lp_sub =
2186 loadparm_s3_global_substitution();
2187 char *params = *ppparams;
2188 char *pdata = *ppdata;
2189 int data_len=0,param_len=0;
2190 SMB_NTQUOTA_STRUCT qt;
2191 struct file_quota_information info = {0};
2192 enum ndr_err_code err;
2193 struct dom_sid sid;
2194 DATA_BLOB inblob;
2195 files_struct *fsp = NULL;
2196 TALLOC_CTX *ctx = NULL;
2197 NTSTATUS status = NT_STATUS_OK;
2198 ZERO_STRUCT(qt);
2200 /* access check */
2201 if (get_current_uid(conn) != sec_initial_uid()) {
2202 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2203 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2204 conn->session_info->unix_info->unix_name));
2205 status = NT_STATUS_ACCESS_DENIED;
2206 goto error;
2210 * Ensure minimum number of parameters sent.
2213 if (parameter_count < 2) {
2214 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2215 status = NT_STATUS_INVALID_PARAMETER;
2216 goto error;
2219 /* maybe we can check the quota_fnum */
2220 fsp = file_fsp(req, SVAL(params,0));
2221 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2222 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2223 status = NT_STATUS_INVALID_HANDLE;
2224 goto error;
2227 ctx = talloc_init("set_user_quota");
2228 if (!ctx) {
2229 status = NT_STATUS_NO_MEMORY;
2230 goto error;
2232 inblob.data = (uint8_t*)pdata;
2233 inblob.length = data_count;
2235 err = ndr_pull_struct_blob(
2236 &inblob,
2237 ctx,
2238 &info,
2239 (ndr_pull_flags_fn_t)ndr_pull_file_quota_information);
2241 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2242 DEBUG(0,("TRANSACT_SET_USER_QUOTA: failed to pull "
2243 "file_quota_information\n"));
2244 status = NT_STATUS_INVALID_PARAMETER;
2245 goto error;
2247 qt.usedspace = info.quota_used;
2249 qt.softlim = info.quota_threshold;
2251 qt.hardlim = info.quota_limit;
2253 sid = info.sid;
2255 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2256 status = NT_STATUS_INTERNAL_ERROR;
2257 goto error;
2260 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2261 pdata, data_len);
2262 TALLOC_FREE(ctx);
2263 return;
2264 error:
2265 TALLOC_FREE(ctx);
2266 reply_nterror(req, status);
2268 #endif /* HAVE_SYS_QUOTAS */
2270 static void handle_nttrans(connection_struct *conn,
2271 struct trans_state *state,
2272 struct smb_request *req)
2274 struct smbXsrv_connection *xconn = req->xconn;
2276 if (xconn->protocol >= PROTOCOL_NT1) {
2277 req->flags2 |= 0x40; /* IS_LONG_NAME */
2278 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2282 /* Now we must call the relevant NT_TRANS function */
2283 switch(state->call) {
2284 case NT_TRANSACT_CREATE:
2286 START_PROFILE(NT_transact_create);
2287 call_nt_transact_create(
2288 conn, req,
2289 &state->setup, state->setup_count,
2290 &state->param, state->total_param,
2291 &state->data, state->total_data,
2292 state->max_data_return);
2293 END_PROFILE(NT_transact_create);
2294 break;
2297 case NT_TRANSACT_IOCTL:
2299 START_PROFILE(NT_transact_ioctl);
2300 call_nt_transact_ioctl(
2301 conn, req,
2302 &state->setup, state->setup_count,
2303 &state->param, state->total_param,
2304 &state->data, state->total_data,
2305 state->max_data_return);
2306 END_PROFILE(NT_transact_ioctl);
2307 break;
2310 case NT_TRANSACT_SET_SECURITY_DESC:
2312 START_PROFILE(NT_transact_set_security_desc);
2313 call_nt_transact_set_security_desc(
2314 conn, req,
2315 &state->setup, state->setup_count,
2316 &state->param, state->total_param,
2317 &state->data, state->total_data,
2318 state->max_data_return);
2319 END_PROFILE(NT_transact_set_security_desc);
2320 break;
2323 case NT_TRANSACT_NOTIFY_CHANGE:
2325 START_PROFILE(NT_transact_notify_change);
2326 call_nt_transact_notify_change(
2327 conn, req,
2328 &state->setup, state->setup_count,
2329 &state->param, state->total_param,
2330 &state->data, state->total_data,
2331 state->max_data_return,
2332 state->max_param_return);
2333 END_PROFILE(NT_transact_notify_change);
2334 break;
2337 case NT_TRANSACT_RENAME:
2339 START_PROFILE(NT_transact_rename);
2340 call_nt_transact_rename(
2341 conn, req,
2342 &state->setup, state->setup_count,
2343 &state->param, state->total_param,
2344 &state->data, state->total_data,
2345 state->max_data_return);
2346 END_PROFILE(NT_transact_rename);
2347 break;
2350 case NT_TRANSACT_QUERY_SECURITY_DESC:
2352 START_PROFILE(NT_transact_query_security_desc);
2353 call_nt_transact_query_security_desc(
2354 conn, req,
2355 &state->setup, state->setup_count,
2356 &state->param, state->total_param,
2357 &state->data, state->total_data,
2358 state->max_data_return);
2359 END_PROFILE(NT_transact_query_security_desc);
2360 break;
2363 #ifdef HAVE_SYS_QUOTAS
2364 case NT_TRANSACT_GET_USER_QUOTA:
2366 START_PROFILE(NT_transact_get_user_quota);
2367 call_nt_transact_get_user_quota(
2368 conn, req,
2369 &state->setup, state->setup_count,
2370 &state->param, state->total_param,
2371 &state->data, state->total_data,
2372 state->max_data_return);
2373 END_PROFILE(NT_transact_get_user_quota);
2374 break;
2377 case NT_TRANSACT_SET_USER_QUOTA:
2379 START_PROFILE(NT_transact_set_user_quota);
2380 call_nt_transact_set_user_quota(
2381 conn, req,
2382 &state->setup, state->setup_count,
2383 &state->param, state->total_param,
2384 &state->data, state->total_data,
2385 state->max_data_return);
2386 END_PROFILE(NT_transact_set_user_quota);
2387 break;
2389 #endif /* HAVE_SYS_QUOTAS */
2391 default:
2392 /* Error in request */
2393 DEBUG(0,("handle_nttrans: Unknown request %d in "
2394 "nttrans call\n", state->call));
2395 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2396 return;
2398 return;
2401 /****************************************************************************
2402 Reply to a SMBNTtrans.
2403 ****************************************************************************/
2405 void reply_nttrans(struct smb_request *req)
2407 connection_struct *conn = req->conn;
2408 uint32_t pscnt;
2409 uint32_t psoff;
2410 uint32_t dscnt;
2411 uint32_t dsoff;
2412 uint16_t function_code;
2413 NTSTATUS result;
2414 struct trans_state *state;
2416 START_PROFILE(SMBnttrans);
2418 if (req->wct < 19) {
2419 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2420 END_PROFILE(SMBnttrans);
2421 return;
2424 pscnt = IVAL(req->vwv+9, 1);
2425 psoff = IVAL(req->vwv+11, 1);
2426 dscnt = IVAL(req->vwv+13, 1);
2427 dsoff = IVAL(req->vwv+15, 1);
2428 function_code = SVAL(req->vwv+18, 0);
2430 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2431 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2432 END_PROFILE(SMBnttrans);
2433 return;
2436 result = allow_new_trans(conn->pending_trans, req->mid);
2437 if (!NT_STATUS_IS_OK(result)) {
2438 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2439 reply_nterror(req, result);
2440 END_PROFILE(SMBnttrans);
2441 return;
2444 if ((state = talloc(conn, struct trans_state)) == NULL) {
2445 reply_nterror(req, NT_STATUS_NO_MEMORY);
2446 END_PROFILE(SMBnttrans);
2447 return;
2450 state->cmd = SMBnttrans;
2452 state->mid = req->mid;
2453 state->vuid = req->vuid;
2454 state->total_data = IVAL(req->vwv+3, 1);
2455 state->data = NULL;
2456 state->total_param = IVAL(req->vwv+1, 1);
2457 state->param = NULL;
2458 state->max_data_return = IVAL(req->vwv+7, 1);
2459 state->max_param_return = IVAL(req->vwv+5, 1);
2461 /* setup count is in *words* */
2462 state->setup_count = 2*CVAL(req->vwv+17, 1);
2463 state->setup = NULL;
2464 state->call = function_code;
2466 DEBUG(10, ("num_setup=%u, "
2467 "param_total=%u, this_param=%u, max_param=%u, "
2468 "data_total=%u, this_data=%u, max_data=%u, "
2469 "param_offset=%u, data_offset=%u\n",
2470 (unsigned)state->setup_count,
2471 (unsigned)state->total_param, (unsigned)pscnt,
2472 (unsigned)state->max_param_return,
2473 (unsigned)state->total_data, (unsigned)dscnt,
2474 (unsigned)state->max_data_return,
2475 (unsigned)psoff, (unsigned)dsoff));
2478 * All nttrans messages we handle have smb_wct == 19 +
2479 * state->setup_count. Ensure this is so as a sanity check.
2482 if(req->wct != 19 + (state->setup_count/2)) {
2483 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2484 req->wct, 19 + (state->setup_count/2)));
2485 goto bad_param;
2488 /* Don't allow more than 128mb for each value. */
2489 if ((state->total_data > (1024*1024*128)) ||
2490 (state->total_param > (1024*1024*128))) {
2491 reply_nterror(req, NT_STATUS_NO_MEMORY);
2492 END_PROFILE(SMBnttrans);
2493 return;
2496 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2497 goto bad_param;
2499 if (state->total_data) {
2501 if (smb_buffer_oob(state->total_data, 0, dscnt)
2502 || smb_buffer_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2503 goto bad_param;
2506 /* Can't use talloc here, the core routines do realloc on the
2507 * params and data. */
2508 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2509 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2510 "bytes !\n", (unsigned int)state->total_data));
2511 TALLOC_FREE(state);
2512 reply_nterror(req, NT_STATUS_NO_MEMORY);
2513 END_PROFILE(SMBnttrans);
2514 return;
2517 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2520 if (state->total_param) {
2522 if (smb_buffer_oob(state->total_param, 0, pscnt)
2523 || smb_buffer_oob(smb_len(req->inbuf), psoff, pscnt)) {
2524 goto bad_param;
2527 /* Can't use talloc here, the core routines do realloc on the
2528 * params and data. */
2529 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2530 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2531 "bytes !\n", (unsigned int)state->total_param));
2532 SAFE_FREE(state->data);
2533 TALLOC_FREE(state);
2534 reply_nterror(req, NT_STATUS_NO_MEMORY);
2535 END_PROFILE(SMBnttrans);
2536 return;
2539 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2542 state->received_data = dscnt;
2543 state->received_param = pscnt;
2545 if(state->setup_count > 0) {
2546 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2547 state->setup_count));
2550 * No overflow possible here, state->setup_count is an
2551 * unsigned int, being filled by a single byte from
2552 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2553 * below is not necessary, it's here to clarify things. The
2554 * validity of req->vwv and req->wct has been checked in
2555 * init_smb1_request already.
2557 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2558 goto bad_param;
2561 state->setup = (uint16_t *)TALLOC(state, state->setup_count);
2562 if (state->setup == NULL) {
2563 DEBUG(0,("reply_nttrans : Out of memory\n"));
2564 SAFE_FREE(state->data);
2565 SAFE_FREE(state->param);
2566 TALLOC_FREE(state);
2567 reply_nterror(req, NT_STATUS_NO_MEMORY);
2568 END_PROFILE(SMBnttrans);
2569 return;
2572 memcpy(state->setup, req->vwv+19, state->setup_count);
2573 dump_data(10, (uint8_t *)state->setup, state->setup_count);
2576 if ((state->received_data == state->total_data) &&
2577 (state->received_param == state->total_param)) {
2578 handle_nttrans(conn, state, req);
2579 SAFE_FREE(state->param);
2580 SAFE_FREE(state->data);
2581 TALLOC_FREE(state);
2582 END_PROFILE(SMBnttrans);
2583 return;
2586 DLIST_ADD(conn->pending_trans, state);
2588 /* We need to send an interim response then receive the rest
2589 of the parameter/data bytes */
2590 reply_smb1_outbuf(req, 0, 0);
2591 show_msg((char *)req->outbuf);
2592 END_PROFILE(SMBnttrans);
2593 return;
2595 bad_param:
2597 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2598 SAFE_FREE(state->data);
2599 SAFE_FREE(state->param);
2600 TALLOC_FREE(state);
2601 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2602 END_PROFILE(SMBnttrans);
2603 return;
2606 /****************************************************************************
2607 Reply to a SMBnttranss
2608 ****************************************************************************/
2610 void reply_nttranss(struct smb_request *req)
2612 connection_struct *conn = req->conn;
2613 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2614 struct trans_state *state;
2616 START_PROFILE(SMBnttranss);
2618 show_msg((const char *)req->inbuf);
2620 /* Windows clients expect all replies to
2621 an NT transact secondary (SMBnttranss 0xA1)
2622 to have a command code of NT transact
2623 (SMBnttrans 0xA0). See bug #8989 for details. */
2624 req->cmd = SMBnttrans;
2626 if (req->wct < 18) {
2627 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2628 END_PROFILE(SMBnttranss);
2629 return;
2632 for (state = conn->pending_trans; state != NULL;
2633 state = state->next) {
2634 if (state->mid == req->mid) {
2635 break;
2639 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2640 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2641 END_PROFILE(SMBnttranss);
2642 return;
2645 /* Revise state->total_param and state->total_data in case they have
2646 changed downwards */
2647 if (IVAL(req->vwv+1, 1) < state->total_param) {
2648 state->total_param = IVAL(req->vwv+1, 1);
2650 if (IVAL(req->vwv+3, 1) < state->total_data) {
2651 state->total_data = IVAL(req->vwv+3, 1);
2654 pcnt = IVAL(req->vwv+5, 1);
2655 poff = IVAL(req->vwv+7, 1);
2656 pdisp = IVAL(req->vwv+9, 1);
2658 dcnt = IVAL(req->vwv+11, 1);
2659 doff = IVAL(req->vwv+13, 1);
2660 ddisp = IVAL(req->vwv+15, 1);
2662 state->received_param += pcnt;
2663 state->received_data += dcnt;
2665 if ((state->received_data > state->total_data) ||
2666 (state->received_param > state->total_param))
2667 goto bad_param;
2669 if (pcnt) {
2670 if (smb_buffer_oob(state->total_param, pdisp, pcnt)
2671 || smb_buffer_oob(smb_len(req->inbuf), poff, pcnt)) {
2672 goto bad_param;
2674 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2677 if (dcnt) {
2678 if (smb_buffer_oob(state->total_data, ddisp, dcnt)
2679 || smb_buffer_oob(smb_len(req->inbuf), doff, dcnt)) {
2680 goto bad_param;
2682 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2685 if ((state->received_param < state->total_param) ||
2686 (state->received_data < state->total_data)) {
2687 END_PROFILE(SMBnttranss);
2688 return;
2691 handle_nttrans(conn, state, req);
2693 DLIST_REMOVE(conn->pending_trans, state);
2694 SAFE_FREE(state->data);
2695 SAFE_FREE(state->param);
2696 TALLOC_FREE(state);
2697 END_PROFILE(SMBnttranss);
2698 return;
2700 bad_param:
2702 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2703 DLIST_REMOVE(conn->pending_trans, state);
2704 SAFE_FREE(state->data);
2705 SAFE_FREE(state->param);
2706 TALLOC_FREE(state);
2707 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2708 END_PROFILE(SMBnttranss);
2709 return;