nsswitch: Fix getting data out of pam_get_data()
[Samba.git] / source3 / smbd / smb1_nttrans.c
blob4a0cfcc113362d930815fd7df7aa96a7b226549b
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, req->seqnum+1,
89 IS_CONN_ENCRYPTED(conn),
90 &req->pcd)) {
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, req->seqnum+1,
253 IS_CONN_ENCRYPTED(conn),
254 &req->pcd)) {
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 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
567 "file_attributes = 0x%x, share_access = 0x%x, "
568 "create_disposition = 0x%x create_options = 0x%x "
569 "root_dir_fid = 0x%x, fname = %s\n",
570 (unsigned int)flags,
571 (unsigned int)access_mask,
572 (unsigned int)file_attributes,
573 (unsigned int)share_access,
574 (unsigned int)create_disposition,
575 (unsigned int)create_options,
576 (unsigned int)root_dir_fid,
577 fname));
580 * we need to remove ignored bits when they come directly from the client
581 * because we reuse some of them for internal stuff
583 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
586 * If it's an IPC, use the pipe handler.
589 if (IS_IPC(conn)) {
590 if (lp_nt_pipe_support()) {
591 do_ntcreate_pipe_open(conn, req);
592 goto out;
594 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
595 goto out;
598 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
599 if (oplock_request) {
600 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
601 ? BATCH_OPLOCK : 0;
604 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
605 case_state = set_posix_case_semantics(ctx, conn);
606 if (!case_state) {
607 reply_nterror(req, NT_STATUS_NO_MEMORY);
608 goto out;
612 if (root_dir_fid != 0) {
613 char *new_fname = NULL;
615 status = get_relative_fid_filename(conn,
616 req,
617 root_dir_fid,
618 fname,
619 &new_fname);
620 if (!NT_STATUS_IS_OK(status)) {
621 reply_nterror(req, status);
622 goto out;
624 fname = new_fname;
627 ucf_flags = filename_create_ucf_flags(req, create_disposition);
628 if (ucf_flags & UCF_GMT_PATHNAME) {
629 extract_snapshot_token(fname, &twrp);
631 status = filename_convert_dirfsp(
632 ctx, conn, fname, ucf_flags, twrp, &dirfsp, &smb_fname);
634 TALLOC_FREE(case_state);
636 if (!NT_STATUS_IS_OK(status)) {
637 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
638 reply_botherror(req,
639 NT_STATUS_PATH_NOT_COVERED,
640 ERRSRV, ERRbadpath);
641 goto out;
643 reply_nterror(req, status);
644 goto out;
648 * Bug #6898 - clients using Windows opens should
649 * never be able to set this attribute into the
650 * VFS.
652 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
654 status = SMB_VFS_CREATE_FILE(
655 conn, /* conn */
656 req, /* req */
657 dirfsp, /* dirfsp */
658 smb_fname, /* fname */
659 access_mask, /* access_mask */
660 share_access, /* share_access */
661 create_disposition, /* create_disposition*/
662 create_options, /* create_options */
663 file_attributes, /* file_attributes */
664 oplock_request, /* oplock_request */
665 NULL, /* lease */
666 allocation_size, /* allocation_size */
667 0, /* private_flags */
668 NULL, /* sd */
669 NULL, /* ea_list */
670 &fsp, /* result */
671 &info, /* pinfo */
672 NULL, NULL); /* create context */
674 if (!NT_STATUS_IS_OK(status)) {
675 if (open_was_deferred(req->xconn, req->mid)) {
676 /* We have re-scheduled this call, no error. */
677 goto out;
679 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
680 bool ok = defer_smb1_sharing_violation(req);
681 if (ok) {
682 goto out;
685 reply_openerror(req, status);
686 goto out;
689 /* Ensure we're pointing at the correct stat struct. */
690 smb_fname = fsp->fsp_name;
693 * If the caller set the extended oplock request bit
694 * and we granted one (by whatever means) - set the
695 * correct bit for extended oplock reply.
698 if (oplock_request &&
699 (lp_fake_oplocks(SNUM(conn))
700 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
703 * Exclusive oplock granted
706 if (flags & REQUEST_BATCH_OPLOCK) {
707 oplock_granted = BATCH_OPLOCK_RETURN;
708 } else {
709 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
711 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
712 oplock_granted = LEVEL_II_OPLOCK_RETURN;
713 } else {
714 oplock_granted = NO_OPLOCK_RETURN;
717 file_len = smb_fname->st.st_ex_size;
719 if (flags & EXTENDED_RESPONSE_REQUIRED) {
720 /* This is very strange. We
721 * return 50 words, but only set
722 * the wcnt to 42 ? It's definitely
723 * what happens on the wire....
725 reply_smb1_outbuf(req, 50, 0);
726 SCVAL(req->outbuf,smb_wct,42);
727 } else {
728 reply_smb1_outbuf(req, 34, 0);
731 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
732 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
734 p = (char *)req->outbuf + smb_vwv2;
736 SCVAL(p, 0, oplock_granted);
738 p++;
739 SSVAL(p,0,fsp->fnum);
740 p += 2;
741 if ((create_disposition == FILE_SUPERSEDE)
742 && (info == FILE_WAS_OVERWRITTEN)) {
743 SIVAL(p,0,FILE_WAS_SUPERSEDED);
744 } else {
745 SIVAL(p,0,info);
747 p += 4;
749 fattr = fdos_mode(fsp);
750 if (fattr == 0) {
751 fattr = FILE_ATTRIBUTE_NORMAL;
754 /* Create time. */
755 create_timespec = get_create_timespec(conn, fsp, smb_fname);
756 a_timespec = smb_fname->st.st_ex_atime;
757 m_timespec = smb_fname->st.st_ex_mtime;
758 c_timespec = get_change_timespec(conn, fsp, smb_fname);
760 if (lp_dos_filetime_resolution(SNUM(conn))) {
761 dos_filetime_timespec(&create_timespec);
762 dos_filetime_timespec(&a_timespec);
763 dos_filetime_timespec(&m_timespec);
764 dos_filetime_timespec(&c_timespec);
767 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
768 p += 8;
769 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
770 p += 8;
771 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
772 p += 8;
773 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
774 p += 8;
775 SIVAL(p,0,fattr); /* File Attributes. */
776 p += 4;
777 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
778 p += 8;
779 SOFF_T(p,0,file_len);
780 p += 8;
781 if (flags & EXTENDED_RESPONSE_REQUIRED) {
782 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
783 unsigned int num_streams = 0;
784 struct stream_struct *streams = NULL;
786 if (lp_ea_support(SNUM(conn))) {
787 size_t num_names = 0;
788 /* Do we have any EA's ? */
789 status = get_ea_names_from_fsp(
790 ctx, smb_fname->fsp, NULL, &num_names);
791 if (NT_STATUS_IS_OK(status) && num_names) {
792 file_status &= ~NO_EAS;
796 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
797 &num_streams, &streams);
798 /* There is always one stream, ::$DATA. */
799 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
800 file_status &= ~NO_SUBSTREAMS;
802 TALLOC_FREE(streams);
803 SSVAL(p,2,file_status);
805 p += 4;
806 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
808 if (flags & EXTENDED_RESPONSE_REQUIRED) {
809 uint32_t perms = 0;
810 p += 25;
811 if (fsp->fsp_flags.is_directory ||
812 fsp->fsp_flags.can_write ||
813 can_write_to_fsp(fsp))
815 perms = FILE_GENERIC_ALL;
816 } else {
817 perms = FILE_GENERIC_READ|FILE_EXECUTE;
819 SIVAL(p,0,perms);
822 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
823 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
825 out:
826 END_PROFILE(SMBntcreateX);
827 return;
830 /****************************************************************************
831 Reply to a NT_TRANSACT_CREATE call to open a pipe.
832 ****************************************************************************/
834 static void do_nt_transact_create_pipe(connection_struct *conn,
835 struct smb_request *req,
836 uint16_t **ppsetup, uint32_t setup_count,
837 char **ppparams, uint32_t parameter_count,
838 char **ppdata, uint32_t data_count)
840 char *fname = NULL;
841 char *params = *ppparams;
842 uint16_t pnum = FNUM_FIELD_INVALID;
843 char *p = NULL;
844 NTSTATUS status;
845 size_t param_len;
846 uint32_t flags;
847 TALLOC_CTX *ctx = talloc_tos();
850 * Ensure minimum number of parameters sent.
853 if(parameter_count < 54) {
854 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
855 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
856 return;
859 flags = IVAL(params,0);
861 if (req->posix_pathnames) {
862 srvstr_get_path_posix(ctx,
863 params,
864 req->flags2,
865 &fname,
866 params+53,
867 parameter_count-53,
868 STR_TERMINATE,
869 &status);
870 } else {
871 srvstr_get_path(ctx,
872 params,
873 req->flags2,
874 &fname,
875 params+53,
876 parameter_count-53,
877 STR_TERMINATE,
878 &status);
880 if (!NT_STATUS_IS_OK(status)) {
881 reply_nterror(req, status);
882 return;
885 nt_open_pipe(fname, conn, req, &pnum);
887 if (req->outbuf) {
888 /* Error return */
889 return;
892 /* Realloc the size of parameters and data we will return */
893 if (flags & EXTENDED_RESPONSE_REQUIRED) {
894 /* Extended response is 32 more byyes. */
895 param_len = 101;
896 } else {
897 param_len = 69;
899 params = nttrans_realloc(ppparams, param_len);
900 if(params == NULL) {
901 reply_nterror(req, NT_STATUS_NO_MEMORY);
902 return;
905 p = params;
906 SCVAL(p,0,NO_OPLOCK_RETURN);
908 p += 2;
909 SSVAL(p,0,pnum);
910 p += 2;
911 SIVAL(p,0,FILE_WAS_OPENED);
912 p += 8;
914 p += 32;
915 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
916 p += 20;
917 /* File type. */
918 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
919 /* Device state. */
920 SSVAL(p,2, 0x5FF); /* ? */
921 p += 4;
923 if (flags & EXTENDED_RESPONSE_REQUIRED) {
924 p += 25;
925 SIVAL(p,0,FILE_GENERIC_ALL);
927 * For pipes W2K3 seems to return
928 * 0x12019B next.
929 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
931 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
934 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
936 /* Send the required number of replies */
937 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
939 return;
942 /****************************************************************************
943 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
944 ****************************************************************************/
946 static void call_nt_transact_create(connection_struct *conn,
947 struct smb_request *req,
948 uint16_t **ppsetup, uint32_t setup_count,
949 char **ppparams, uint32_t parameter_count,
950 char **ppdata, uint32_t data_count,
951 uint32_t max_data_count)
953 struct smb_filename *smb_fname = NULL;
954 char *fname = NULL;
955 char *params = *ppparams;
956 char *data = *ppdata;
957 /* Breakout the oplock request bits so we can set the reply bits separately. */
958 uint32_t fattr=0;
959 off_t file_len = 0;
960 int info = 0;
961 struct files_struct *dirfsp = NULL;
962 files_struct *fsp = NULL;
963 char *p = NULL;
964 uint32_t flags;
965 uint32_t access_mask;
966 uint32_t file_attributes;
967 uint32_t share_access;
968 uint32_t create_disposition;
969 uint32_t create_options;
970 uint32_t sd_len;
971 struct security_descriptor *sd = NULL;
972 uint32_t ea_len;
973 uint16_t root_dir_fid;
974 struct timespec create_timespec;
975 struct timespec c_timespec;
976 struct timespec a_timespec;
977 struct timespec m_timespec;
978 struct ea_list *ea_list = NULL;
979 NTSTATUS status;
980 size_t param_len;
981 uint64_t allocation_size;
982 int oplock_request;
983 uint8_t oplock_granted;
984 struct case_semantics_state *case_state = NULL;
985 uint32_t ucf_flags;
986 NTTIME twrp = 0;
987 TALLOC_CTX *ctx = talloc_tos();
989 DEBUG(5,("call_nt_transact_create\n"));
992 * If it's an IPC, use the pipe handler.
995 if (IS_IPC(conn)) {
996 if (lp_nt_pipe_support()) {
997 do_nt_transact_create_pipe(
998 conn, req,
999 ppsetup, setup_count,
1000 ppparams, parameter_count,
1001 ppdata, data_count);
1002 goto out;
1004 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1005 goto out;
1009 * Ensure minimum number of parameters sent.
1012 if(parameter_count < 54) {
1013 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1014 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1015 goto out;
1018 flags = IVAL(params,0);
1019 access_mask = IVAL(params,8);
1020 file_attributes = IVAL(params,20);
1021 share_access = IVAL(params,24);
1022 create_disposition = IVAL(params,28);
1023 create_options = IVAL(params,32);
1024 sd_len = IVAL(params,36);
1025 ea_len = IVAL(params,40);
1026 root_dir_fid = (uint16_t)IVAL(params,4);
1027 allocation_size = BVAL(params,12);
1030 * we need to remove ignored bits when they come directly from the client
1031 * because we reuse some of them for internal stuff
1033 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1035 if (req->posix_pathnames) {
1036 srvstr_get_path_posix(ctx,
1037 params,
1038 req->flags2,
1039 &fname,
1040 params+53,
1041 parameter_count-53,
1042 STR_TERMINATE,
1043 &status);
1044 } else {
1045 srvstr_get_path(ctx,
1046 params,
1047 req->flags2,
1048 &fname,
1049 params+53,
1050 parameter_count-53,
1051 STR_TERMINATE,
1052 &status);
1054 if (!NT_STATUS_IS_OK(status)) {
1055 reply_nterror(req, status);
1056 goto out;
1059 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1060 case_state = set_posix_case_semantics(ctx, conn);
1061 if (!case_state) {
1062 reply_nterror(req, NT_STATUS_NO_MEMORY);
1063 goto out;
1067 if (root_dir_fid != 0) {
1068 char *new_fname = NULL;
1070 status = get_relative_fid_filename(conn,
1071 req,
1072 root_dir_fid,
1073 fname,
1074 &new_fname);
1075 if (!NT_STATUS_IS_OK(status)) {
1076 reply_nterror(req, status);
1077 goto out;
1079 fname = new_fname;
1082 ucf_flags = filename_create_ucf_flags(req, create_disposition);
1083 if (ucf_flags & UCF_GMT_PATHNAME) {
1084 extract_snapshot_token(fname, &twrp);
1086 status = filename_convert_dirfsp(ctx,
1087 conn,
1088 fname,
1089 ucf_flags,
1090 twrp,
1091 &dirfsp,
1092 &smb_fname);
1094 TALLOC_FREE(case_state);
1096 if (!NT_STATUS_IS_OK(status)) {
1097 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1098 reply_botherror(req,
1099 NT_STATUS_PATH_NOT_COVERED,
1100 ERRSRV, ERRbadpath);
1101 goto out;
1103 reply_nterror(req, status);
1104 goto out;
1107 /* Ensure the data_len is correct for the sd and ea values given. */
1108 if ((ea_len + sd_len > data_count)
1109 || (ea_len > data_count) || (sd_len > data_count)
1110 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1111 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1112 "%u, data_count = %u\n", (unsigned int)ea_len,
1113 (unsigned int)sd_len, (unsigned int)data_count));
1114 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1115 goto out;
1118 if (sd_len) {
1119 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1120 sd_len));
1122 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1123 &sd);
1124 if (!NT_STATUS_IS_OK(status)) {
1125 DEBUG(10, ("call_nt_transact_create: "
1126 "unmarshall_sec_desc failed: %s\n",
1127 nt_errstr(status)));
1128 reply_nterror(req, status);
1129 goto out;
1133 if (ea_len) {
1134 if (!lp_ea_support(SNUM(conn))) {
1135 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1136 "EA's not supported.\n",
1137 (unsigned int)ea_len));
1138 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1139 goto out;
1142 if (ea_len < 10) {
1143 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1144 "too small (should be more than 10)\n",
1145 (unsigned int)ea_len ));
1146 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1147 goto out;
1150 /* We have already checked that ea_len <= data_count here. */
1151 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1152 ea_len);
1153 if (ea_list == NULL) {
1154 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1155 goto out;
1158 if (!req->posix_pathnames &&
1159 ea_list_has_invalid_name(ea_list)) {
1160 /* Realloc the size of parameters and data we will return */
1161 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1162 /* Extended response is 32 more bytes. */
1163 param_len = 101;
1164 } else {
1165 param_len = 69;
1167 params = nttrans_realloc(ppparams, param_len);
1168 if(params == NULL) {
1169 reply_nterror(req, NT_STATUS_NO_MEMORY);
1170 goto out;
1173 memset(params, '\0', param_len);
1174 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1175 params, param_len, NULL, 0);
1176 goto out;
1180 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1181 if (oplock_request) {
1182 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1183 ? BATCH_OPLOCK : 0;
1187 * Bug #6898 - clients using Windows opens should
1188 * never be able to set this attribute into the
1189 * VFS.
1191 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1193 status = SMB_VFS_CREATE_FILE(
1194 conn, /* conn */
1195 req, /* req */
1196 dirfsp, /* dirfsp */
1197 smb_fname, /* fname */
1198 access_mask, /* access_mask */
1199 share_access, /* share_access */
1200 create_disposition, /* create_disposition*/
1201 create_options, /* create_options */
1202 file_attributes, /* file_attributes */
1203 oplock_request, /* oplock_request */
1204 NULL, /* lease */
1205 allocation_size, /* allocation_size */
1206 0, /* private_flags */
1207 sd, /* sd */
1208 ea_list, /* ea_list */
1209 &fsp, /* result */
1210 &info, /* pinfo */
1211 NULL, NULL); /* create context */
1213 if(!NT_STATUS_IS_OK(status)) {
1214 if (open_was_deferred(req->xconn, req->mid)) {
1215 /* We have re-scheduled this call, no error. */
1216 return;
1218 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1219 bool ok = defer_smb1_sharing_violation(req);
1220 if (ok) {
1221 return;
1224 reply_openerror(req, status);
1225 goto out;
1228 /* Ensure we're pointing at the correct stat struct. */
1229 TALLOC_FREE(smb_fname);
1230 smb_fname = fsp->fsp_name;
1233 * If the caller set the extended oplock request bit
1234 * and we granted one (by whatever means) - set the
1235 * correct bit for extended oplock reply.
1238 if (oplock_request &&
1239 (lp_fake_oplocks(SNUM(conn))
1240 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1243 * Exclusive oplock granted
1246 if (flags & REQUEST_BATCH_OPLOCK) {
1247 oplock_granted = BATCH_OPLOCK_RETURN;
1248 } else {
1249 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1251 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1252 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1253 } else {
1254 oplock_granted = NO_OPLOCK_RETURN;
1257 file_len = smb_fname->st.st_ex_size;
1259 /* Realloc the size of parameters and data we will return */
1260 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1261 /* Extended response is 32 more byyes. */
1262 param_len = 101;
1263 } else {
1264 param_len = 69;
1266 params = nttrans_realloc(ppparams, param_len);
1267 if(params == NULL) {
1268 reply_nterror(req, NT_STATUS_NO_MEMORY);
1269 goto out;
1272 p = params;
1273 SCVAL(p, 0, oplock_granted);
1275 p += 2;
1276 SSVAL(p,0,fsp->fnum);
1277 p += 2;
1278 if ((create_disposition == FILE_SUPERSEDE)
1279 && (info == FILE_WAS_OVERWRITTEN)) {
1280 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1281 } else {
1282 SIVAL(p,0,info);
1284 p += 8;
1286 fattr = fdos_mode(fsp);
1287 if (fattr == 0) {
1288 fattr = FILE_ATTRIBUTE_NORMAL;
1291 /* Create time. */
1292 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1293 a_timespec = smb_fname->st.st_ex_atime;
1294 m_timespec = smb_fname->st.st_ex_mtime;
1295 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1297 if (lp_dos_filetime_resolution(SNUM(conn))) {
1298 dos_filetime_timespec(&create_timespec);
1299 dos_filetime_timespec(&a_timespec);
1300 dos_filetime_timespec(&m_timespec);
1301 dos_filetime_timespec(&c_timespec);
1304 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
1305 p += 8;
1306 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
1307 p += 8;
1308 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
1309 p += 8;
1310 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
1311 p += 8;
1312 SIVAL(p,0,fattr); /* File Attributes. */
1313 p += 4;
1314 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1315 p += 8;
1316 SOFF_T(p,0,file_len);
1317 p += 8;
1318 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1319 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1320 unsigned int num_streams = 0;
1321 struct stream_struct *streams = NULL;
1323 if (lp_ea_support(SNUM(conn))) {
1324 size_t num_names = 0;
1325 /* Do we have any EA's ? */
1326 status = get_ea_names_from_fsp(
1327 ctx, smb_fname->fsp, NULL, &num_names);
1328 if (NT_STATUS_IS_OK(status) && num_names) {
1329 file_status &= ~NO_EAS;
1333 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
1334 &num_streams, &streams);
1335 /* There is always one stream, ::$DATA. */
1336 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1337 file_status &= ~NO_SUBSTREAMS;
1339 TALLOC_FREE(streams);
1340 SSVAL(p,2,file_status);
1342 p += 4;
1343 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
1345 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1346 uint32_t perms = 0;
1347 p += 25;
1348 if (fsp->fsp_flags.is_directory ||
1349 fsp->fsp_flags.can_write ||
1350 can_write_to_fsp(fsp))
1352 perms = FILE_GENERIC_ALL;
1353 } else {
1354 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1356 SIVAL(p,0,perms);
1359 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1360 smb_fname_str_dbg(smb_fname)));
1362 /* Send the required number of replies */
1363 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1364 out:
1365 return;
1368 /****************************************************************************
1369 Reply to a NT CANCEL request.
1370 conn POINTER CAN BE NULL HERE !
1371 ****************************************************************************/
1373 void reply_ntcancel(struct smb_request *req)
1375 struct smbXsrv_connection *xconn = req->xconn;
1376 struct smbd_server_connection *sconn = req->sconn;
1377 bool found;
1380 * Go through and cancel any pending change notifies.
1383 START_PROFILE(SMBntcancel);
1384 smb1_srv_cancel_sign_response(xconn);
1385 found = remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1386 if (!found) {
1387 smbd_smb1_brl_finish_by_mid(sconn, req->mid);
1390 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1391 (unsigned long long)req->mid));
1393 END_PROFILE(SMBntcancel);
1394 return;
1397 /****************************************************************************
1398 Reply to a NT rename request.
1399 ****************************************************************************/
1401 void reply_ntrename(struct smb_request *req)
1403 connection_struct *conn = req->conn;
1404 struct files_struct *src_dirfsp = NULL;
1405 struct smb_filename *smb_fname_old = NULL;
1406 struct files_struct *dst_dirfsp = NULL;
1407 struct smb_filename *smb_fname_new = NULL;
1408 char *oldname = NULL;
1409 char *newname = NULL;
1410 const char *dst_original_lcomp = NULL;
1411 const char *p;
1412 NTSTATUS status;
1413 uint32_t attrs;
1414 uint32_t ucf_flags_src = ucf_flags_from_smb_request(req);
1415 NTTIME src_twrp = 0;
1416 uint32_t ucf_flags_dst = ucf_flags_from_smb_request(req);
1417 NTTIME dst_twrp = 0;
1418 uint16_t rename_type;
1419 TALLOC_CTX *ctx = talloc_tos();
1420 bool stream_rename = false;
1422 START_PROFILE(SMBntrename);
1424 if (req->wct < 4) {
1425 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1426 goto out;
1429 attrs = SVAL(req->vwv+0, 0);
1430 rename_type = SVAL(req->vwv+1, 0);
1432 p = (const char *)req->buf + 1;
1433 p += srvstr_get_path_req(ctx, req, &oldname, p, STR_TERMINATE,
1434 &status);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 reply_nterror(req, status);
1437 goto out;
1440 if (!req->posix_pathnames && ms_has_wild(oldname)) {
1441 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1442 goto out;
1445 p++;
1446 p += srvstr_get_path_req(ctx, req, &newname, p, STR_TERMINATE,
1447 &status);
1448 if (!NT_STATUS_IS_OK(status)) {
1449 reply_nterror(req, status);
1450 goto out;
1453 if (!req->posix_pathnames && ms_has_wild(newname)) {
1454 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1455 goto out;
1458 if (!req->posix_pathnames) {
1459 /* The newname must begin with a ':' if the
1460 oldname contains a ':'. */
1461 if (strchr_m(oldname, ':')) {
1462 if (newname[0] != ':') {
1463 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1464 goto out;
1466 stream_rename = true;
1470 if (ucf_flags_src & UCF_GMT_PATHNAME) {
1471 extract_snapshot_token(oldname, &src_twrp);
1473 status = filename_convert_dirfsp(ctx,
1474 conn,
1475 oldname,
1476 ucf_flags_src,
1477 src_twrp,
1478 &src_dirfsp,
1479 &smb_fname_old);
1480 if (!NT_STATUS_IS_OK(status)) {
1481 if (NT_STATUS_EQUAL(status,
1482 NT_STATUS_PATH_NOT_COVERED)) {
1483 reply_botherror(req,
1484 NT_STATUS_PATH_NOT_COVERED,
1485 ERRSRV, ERRbadpath);
1486 goto out;
1488 reply_nterror(req, status);
1489 goto out;
1492 if (stream_rename) {
1494 * No point in calling filename_convert()
1495 * on a raw stream name. It can never find
1496 * the file anyway. Use the same logic as
1497 * SMB2_FILE_RENAME_INFORMATION_INTERNAL
1498 * and generate smb_fname_new directly.
1500 smb_fname_new = synthetic_smb_fname(talloc_tos(),
1501 smb_fname_old->base_name,
1502 newname,
1503 NULL,
1504 smb_fname_old->twrp,
1505 smb_fname_old->flags);
1506 if (smb_fname_new == NULL) {
1507 reply_nterror(req, NT_STATUS_NO_MEMORY);
1508 goto out;
1510 } else {
1511 if (ucf_flags_dst & UCF_GMT_PATHNAME) {
1512 extract_snapshot_token(newname,
1513 &dst_twrp);
1515 status = filename_convert_dirfsp(ctx,
1516 conn,
1517 newname,
1518 ucf_flags_dst,
1519 dst_twrp,
1520 &dst_dirfsp,
1521 &smb_fname_new);
1522 if (!NT_STATUS_IS_OK(status)) {
1523 if (NT_STATUS_EQUAL(status,
1524 NT_STATUS_PATH_NOT_COVERED)) {
1525 reply_botherror(req,
1526 NT_STATUS_PATH_NOT_COVERED,
1527 ERRSRV, ERRbadpath);
1528 goto out;
1530 reply_nterror(req, status);
1531 goto out;
1535 /* Get the last component of the destination for rename_internals(). */
1536 dst_original_lcomp = get_original_lcomp(ctx,
1537 conn,
1538 newname,
1539 ucf_flags_dst);
1540 if (dst_original_lcomp == NULL) {
1541 reply_nterror(req, NT_STATUS_NO_MEMORY);
1542 goto out;
1546 DEBUG(3,("reply_ntrename: %s -> %s\n",
1547 smb_fname_str_dbg(smb_fname_old),
1548 smb_fname_str_dbg(smb_fname_new)));
1550 switch(rename_type) {
1551 case RENAME_FLAG_RENAME:
1552 status = rename_internals(ctx,
1553 conn,
1554 req,
1555 src_dirfsp,
1556 smb_fname_old,
1557 dst_dirfsp,
1558 smb_fname_new,
1559 dst_original_lcomp,
1560 attrs,
1561 false,
1562 DELETE_ACCESS);
1563 break;
1564 case RENAME_FLAG_HARD_LINK:
1565 status = hardlink_internals(ctx,
1566 conn,
1567 req,
1568 false,
1569 src_dirfsp,
1570 smb_fname_old,
1571 dst_dirfsp,
1572 smb_fname_new);
1573 break;
1574 case RENAME_FLAG_COPY:
1575 status = copy_internals(ctx,
1576 conn,
1577 req,
1578 src_dirfsp,
1579 smb_fname_old,
1580 dst_dirfsp,
1581 smb_fname_new,
1582 attrs);
1583 break;
1584 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1585 status = NT_STATUS_INVALID_PARAMETER;
1586 break;
1587 default:
1588 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1589 break;
1592 if (!NT_STATUS_IS_OK(status)) {
1593 if (open_was_deferred(req->xconn, req->mid)) {
1594 /* We have re-scheduled this call. */
1595 goto out;
1597 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1598 bool ok = defer_smb1_sharing_violation(req);
1599 if (ok) {
1600 goto out;
1604 reply_nterror(req, status);
1605 goto out;
1608 reply_smb1_outbuf(req, 0, 0);
1609 out:
1610 END_PROFILE(SMBntrename);
1611 return;
1614 /****************************************************************************
1615 Reply to a notify change - queue the request and
1616 don't allow a directory to be opened.
1617 ****************************************************************************/
1619 static void smbd_smb1_notify_reply(struct smb_request *req,
1620 NTSTATUS error_code,
1621 uint8_t *buf, size_t len)
1623 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1626 static void call_nt_transact_notify_change(connection_struct *conn,
1627 struct smb_request *req,
1628 uint16_t **ppsetup,
1629 uint32_t setup_count,
1630 char **ppparams,
1631 uint32_t parameter_count,
1632 char **ppdata, uint32_t data_count,
1633 uint32_t max_data_count,
1634 uint32_t max_param_count)
1636 uint16_t *setup = *ppsetup;
1637 files_struct *fsp;
1638 uint32_t filter;
1639 NTSTATUS status;
1640 bool recursive;
1642 if(setup_count < 6) {
1643 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1644 return;
1647 fsp = file_fsp(req, SVAL(setup,4));
1648 filter = IVAL(setup, 0);
1649 recursive = (SVAL(setup, 6) != 0) ? True : False;
1651 DEBUG(3,("call_nt_transact_notify_change\n"));
1653 if(!fsp) {
1654 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1655 return;
1659 char *filter_string;
1661 if (!(filter_string = notify_filter_string(NULL, filter))) {
1662 reply_nterror(req,NT_STATUS_NO_MEMORY);
1663 return;
1666 DEBUG(3,("call_nt_transact_notify_change: notify change "
1667 "called on %s, filter = %s, recursive = %d\n",
1668 fsp_str_dbg(fsp), filter_string, recursive));
1670 TALLOC_FREE(filter_string);
1673 if((!fsp->fsp_flags.is_directory) || (conn != fsp->conn)) {
1674 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1675 return;
1678 if (fsp->notify == NULL) {
1680 status = change_notify_create(fsp,
1681 max_param_count,
1682 filter,
1683 recursive);
1684 if (!NT_STATUS_IS_OK(status)) {
1685 DEBUG(10, ("change_notify_create returned %s\n",
1686 nt_errstr(status)));
1687 reply_nterror(req, status);
1688 return;
1692 if (change_notify_fsp_has_changes(fsp)) {
1695 * We've got changes pending, respond immediately
1699 * TODO: write a torture test to check the filtering behaviour
1700 * here.
1703 change_notify_reply(req,
1704 NT_STATUS_OK,
1705 max_param_count,
1706 fsp->notify,
1707 smbd_smb1_notify_reply);
1710 * change_notify_reply() above has independently sent its
1711 * results
1713 return;
1717 * No changes pending, queue the request
1720 status = change_notify_add_request(req,
1721 max_param_count,
1722 filter,
1723 recursive, fsp,
1724 smbd_smb1_notify_reply);
1725 if (!NT_STATUS_IS_OK(status)) {
1726 reply_nterror(req, status);
1728 return;
1731 /****************************************************************************
1732 Reply to an NT transact rename command.
1733 ****************************************************************************/
1735 static void call_nt_transact_rename(connection_struct *conn,
1736 struct smb_request *req,
1737 uint16_t **ppsetup, uint32_t setup_count,
1738 char **ppparams, uint32_t parameter_count,
1739 char **ppdata, uint32_t data_count,
1740 uint32_t max_data_count)
1742 char *params = *ppparams;
1743 char *new_name = NULL;
1744 files_struct *fsp = NULL;
1745 NTSTATUS status;
1746 TALLOC_CTX *ctx = talloc_tos();
1748 if(parameter_count < 5) {
1749 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1750 return;
1753 fsp = file_fsp(req, SVAL(params, 0));
1754 if (!check_fsp(conn, req, fsp)) {
1755 return;
1757 if (req->posix_pathnames) {
1758 srvstr_get_path_posix(ctx,
1759 params,
1760 req->flags2,
1761 &new_name,
1762 params+4,
1763 parameter_count - 4,
1764 STR_TERMINATE,
1765 &status);
1766 } else {
1767 srvstr_get_path(ctx,
1768 params,
1769 req->flags2,
1770 &new_name,
1771 params+4,
1772 parameter_count - 4,
1773 STR_TERMINATE,
1774 &status);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 reply_nterror(req, status);
1779 return;
1783 * W2K3 ignores this request as the RAW-RENAME test
1784 * demonstrates, so we do.
1786 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1788 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1789 fsp_str_dbg(fsp), new_name));
1791 return;
1794 /****************************************************************************
1795 SMB1 reply to query a security descriptor.
1796 ****************************************************************************/
1798 static void call_nt_transact_query_security_desc(connection_struct *conn,
1799 struct smb_request *req,
1800 uint16_t **ppsetup,
1801 uint32_t setup_count,
1802 char **ppparams,
1803 uint32_t parameter_count,
1804 char **ppdata,
1805 uint32_t data_count,
1806 uint32_t max_data_count)
1808 char *params = *ppparams;
1809 char *data = *ppdata;
1810 size_t sd_size = 0;
1811 uint32_t security_info_wanted;
1812 files_struct *fsp = NULL;
1813 NTSTATUS status;
1814 uint8_t *marshalled_sd = NULL;
1816 if(parameter_count < 8) {
1817 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1818 return;
1821 fsp = file_fsp(req, SVAL(params,0));
1822 if(!fsp) {
1823 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1824 return;
1827 security_info_wanted = IVAL(params,4);
1829 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1830 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1831 (unsigned int)security_info_wanted));
1833 params = nttrans_realloc(ppparams, 4);
1834 if(params == NULL) {
1835 reply_nterror(req, NT_STATUS_NO_MEMORY);
1836 return;
1840 * Get the permissions to return.
1843 status = smbd_do_query_security_desc(conn,
1844 talloc_tos(),
1845 fsp,
1846 security_info_wanted &
1847 SMB_SUPPORTED_SECINFO_FLAGS,
1848 max_data_count,
1849 &marshalled_sd,
1850 &sd_size);
1852 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1853 SIVAL(params,0,(uint32_t)sd_size);
1854 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1855 params, 4, NULL, 0);
1856 return;
1859 if (!NT_STATUS_IS_OK(status)) {
1860 reply_nterror(req, status);
1861 return;
1864 SMB_ASSERT(sd_size > 0);
1866 SIVAL(params,0,(uint32_t)sd_size);
1868 if (max_data_count < sd_size) {
1869 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1870 params, 4, NULL, 0);
1871 return;
1875 * Allocate the data we will return.
1878 data = nttrans_realloc(ppdata, sd_size);
1879 if(data == NULL) {
1880 reply_nterror(req, NT_STATUS_NO_MEMORY);
1881 return;
1884 memcpy(data, marshalled_sd, sd_size);
1886 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1888 return;
1891 /****************************************************************************
1892 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1893 ****************************************************************************/
1895 static void call_nt_transact_set_security_desc(connection_struct *conn,
1896 struct smb_request *req,
1897 uint16_t **ppsetup,
1898 uint32_t setup_count,
1899 char **ppparams,
1900 uint32_t parameter_count,
1901 char **ppdata,
1902 uint32_t data_count,
1903 uint32_t max_data_count)
1905 char *params= *ppparams;
1906 char *data = *ppdata;
1907 files_struct *fsp = NULL;
1908 uint32_t security_info_sent = 0;
1909 NTSTATUS status;
1911 if(parameter_count < 8) {
1912 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1913 return;
1916 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1917 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1918 return;
1921 if (!CAN_WRITE(fsp->conn)) {
1922 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1923 return;
1926 if(!lp_nt_acl_support(SNUM(conn))) {
1927 goto done;
1930 security_info_sent = IVAL(params,4);
1932 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1933 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1935 if (data_count == 0) {
1936 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1937 return;
1940 status = set_sd_blob(fsp, (uint8_t *)data, data_count,
1941 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 reply_nterror(req, status);
1944 return;
1947 done:
1948 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1949 return;
1952 /****************************************************************************
1953 Reply to NT IOCTL
1954 ****************************************************************************/
1956 static void call_nt_transact_ioctl(connection_struct *conn,
1957 struct smb_request *req,
1958 uint16_t **ppsetup, uint32_t setup_count,
1959 char **ppparams, uint32_t parameter_count,
1960 char **ppdata, uint32_t data_count,
1961 uint32_t max_data_count)
1963 NTSTATUS status;
1964 uint32_t function;
1965 uint16_t fidnum;
1966 files_struct *fsp;
1967 uint8_t isFSctl;
1968 uint8_t compfilter;
1969 char *out_data = NULL;
1970 uint32_t out_data_len = 0;
1971 char *pdata = *ppdata;
1972 TALLOC_CTX *ctx = talloc_tos();
1974 if (setup_count != 8) {
1975 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1976 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1977 return;
1980 function = IVAL(*ppsetup, 0);
1981 fidnum = SVAL(*ppsetup, 4);
1982 isFSctl = CVAL(*ppsetup, 6);
1983 compfilter = CVAL(*ppsetup, 7);
1985 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1986 function, fidnum, isFSctl, compfilter));
1988 fsp=file_fsp(req, fidnum);
1991 * We don't really implement IOCTLs, especially on files.
1993 if (!isFSctl) {
1994 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
1995 isFSctl));
1996 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1997 return;
2000 /* Has to be for an open file! */
2001 if (!check_fsp_open(conn, req, fsp)) {
2002 return;
2005 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2008 * out_data might be allocated by the VFS module, but talloc should be
2009 * used, and should be cleaned up when the request ends.
2011 status = SMB_VFS_FSCTL(fsp,
2012 ctx,
2013 function,
2014 req->flags2,
2015 (uint8_t *)pdata,
2016 data_count,
2017 (uint8_t **)&out_data,
2018 max_data_count,
2019 &out_data_len);
2020 if (!NT_STATUS_IS_OK(status)) {
2021 reply_nterror(req, status);
2022 } else {
2023 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2028 #ifdef HAVE_SYS_QUOTAS
2029 /****************************************************************************
2030 Reply to get user quota
2031 ****************************************************************************/
2033 static void call_nt_transact_get_user_quota(connection_struct *conn,
2034 struct smb_request *req,
2035 uint16_t **ppsetup,
2036 uint32_t setup_count,
2037 char **ppparams,
2038 uint32_t parameter_count,
2039 char **ppdata,
2040 uint32_t data_count,
2041 uint32_t max_data_count)
2043 const struct loadparm_substitution *lp_sub =
2044 loadparm_s3_global_substitution();
2045 NTSTATUS nt_status = NT_STATUS_OK;
2046 char *params = *ppparams;
2047 char *pdata = *ppdata;
2048 int data_len = 0;
2049 int param_len = 0;
2050 files_struct *fsp = NULL;
2051 DATA_BLOB blob = data_blob_null;
2052 struct nttrans_query_quota_params info = {0};
2053 enum ndr_err_code err;
2054 TALLOC_CTX *tmp_ctx = NULL;
2055 uint32_t resp_len = 0;
2056 uint8_t *resp_data = 0;
2058 tmp_ctx = talloc_init("ntquota_list");
2059 if (!tmp_ctx) {
2060 nt_status = NT_STATUS_NO_MEMORY;
2061 goto error;
2064 /* access check */
2065 if (get_current_uid(conn) != sec_initial_uid()) {
2066 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2067 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2068 conn->session_info->unix_info->unix_name));
2069 nt_status = NT_STATUS_ACCESS_DENIED;
2070 goto error;
2073 blob.data = (uint8_t*)params;
2074 blob.length = parameter_count;
2076 err = ndr_pull_struct_blob(&blob, tmp_ctx, &info,
2077 (ndr_pull_flags_fn_t)ndr_pull_nttrans_query_quota_params);
2079 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2080 DEBUG(0,("TRANSACT_GET_USER_QUOTA: failed to pull "
2081 "query_quota_params."));
2082 nt_status = NT_STATUS_INVALID_PARAMETER;
2083 goto error;
2085 DBG_DEBUG("info.return_single_entry = %u, info.restart_scan = %u, "
2086 "info.sid_list_length = %u, info.start_sid_length = %u, "
2087 "info.start_sid_offset = %u\n",
2088 (unsigned int)info.return_single_entry,
2089 (unsigned int)info.restart_scan,
2090 (unsigned int)info.sid_list_length,
2091 (unsigned int)info.start_sid_length,
2092 (unsigned int)info.start_sid_offset);
2094 /* set blob to point at data for further parsing */
2095 blob.data = (uint8_t*)pdata;
2096 blob.length = data_count;
2098 * Although MS-SMB ref is ambiguous here, a microsoft client will
2099 * only ever send a start sid (as part of a list) with
2100 * sid_list_length & start_sid_offset both set to the actual list
2101 * length. Note: Only a single result is returned in this case
2102 * In the case where either start_sid_offset or start_sid_length
2103 * are set alone or if both set (but have different values) then
2104 * it seems windows will return a number of entries from the start
2105 * of the list of users with quotas set. This behaviour is undocumented
2106 * and windows clients do not send messages of that type. As such we
2107 * currently will reject these requests.
2109 if (info.start_sid_length
2110 || (info.sid_list_length != info.start_sid_offset)) {
2111 DBG_ERR("TRANSACT_GET_USER_QUOTA: unsupported single or "
2112 "compound sid format\n");
2113 nt_status = NT_STATUS_INVALID_PARAMETER;
2114 goto error;
2117 /* maybe we can check the quota_fnum */
2118 fsp = file_fsp(req, info.fid);
2119 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2120 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2121 nt_status = NT_STATUS_INVALID_HANDLE;
2122 goto error;
2124 nt_status = smbd_do_query_getinfo_quota(tmp_ctx,
2125 fsp,
2126 info.restart_scan,
2127 info.return_single_entry,
2128 info.sid_list_length,
2129 &blob,
2130 max_data_count,
2131 &resp_data,
2132 &resp_len);
2133 if (!NT_STATUS_IS_OK(nt_status)) {
2134 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
2135 goto error;
2137 nt_status = NT_STATUS_OK;
2140 param_len = 4;
2141 params = nttrans_realloc(ppparams, param_len);
2142 if(params == NULL) {
2143 nt_status = NT_STATUS_NO_MEMORY;
2144 goto error;
2147 data_len = resp_len;
2148 SIVAL(params, 0, data_len);
2149 pdata = nttrans_realloc(ppdata, data_len);
2150 memcpy(pdata, resp_data, data_len);
2152 TALLOC_FREE(tmp_ctx);
2153 send_nt_replies(conn, req, nt_status, params, param_len,
2154 pdata, data_len);
2155 return;
2156 error:
2157 TALLOC_FREE(tmp_ctx);
2158 reply_nterror(req, nt_status);
2161 /****************************************************************************
2162 Reply to set user quota
2163 ****************************************************************************/
2165 static void call_nt_transact_set_user_quota(connection_struct *conn,
2166 struct smb_request *req,
2167 uint16_t **ppsetup,
2168 uint32_t setup_count,
2169 char **ppparams,
2170 uint32_t parameter_count,
2171 char **ppdata,
2172 uint32_t data_count,
2173 uint32_t max_data_count)
2175 const struct loadparm_substitution *lp_sub =
2176 loadparm_s3_global_substitution();
2177 char *params = *ppparams;
2178 char *pdata = *ppdata;
2179 int data_len=0,param_len=0;
2180 SMB_NTQUOTA_STRUCT qt;
2181 struct file_quota_information info = {0};
2182 enum ndr_err_code err;
2183 struct dom_sid sid;
2184 DATA_BLOB inblob;
2185 files_struct *fsp = NULL;
2186 TALLOC_CTX *ctx = NULL;
2187 NTSTATUS status = NT_STATUS_OK;
2188 ZERO_STRUCT(qt);
2190 /* access check */
2191 if (get_current_uid(conn) != sec_initial_uid()) {
2192 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2193 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2194 conn->session_info->unix_info->unix_name));
2195 status = NT_STATUS_ACCESS_DENIED;
2196 goto error;
2200 * Ensure minimum number of parameters sent.
2203 if (parameter_count < 2) {
2204 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2205 status = NT_STATUS_INVALID_PARAMETER;
2206 goto error;
2209 /* maybe we can check the quota_fnum */
2210 fsp = file_fsp(req, SVAL(params,0));
2211 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2212 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2213 status = NT_STATUS_INVALID_HANDLE;
2214 goto error;
2217 ctx = talloc_init("set_user_quota");
2218 if (!ctx) {
2219 status = NT_STATUS_NO_MEMORY;
2220 goto error;
2222 inblob.data = (uint8_t*)pdata;
2223 inblob.length = data_count;
2225 err = ndr_pull_struct_blob(
2226 &inblob,
2227 ctx,
2228 &info,
2229 (ndr_pull_flags_fn_t)ndr_pull_file_quota_information);
2231 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2232 DEBUG(0,("TRANSACT_SET_USER_QUOTA: failed to pull "
2233 "file_quota_information\n"));
2234 status = NT_STATUS_INVALID_PARAMETER;
2235 goto error;
2237 qt.usedspace = info.quota_used;
2239 qt.softlim = info.quota_threshold;
2241 qt.hardlim = info.quota_limit;
2243 sid = info.sid;
2245 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2246 status = NT_STATUS_INTERNAL_ERROR;
2247 goto error;
2250 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2251 pdata, data_len);
2252 TALLOC_FREE(ctx);
2253 return;
2254 error:
2255 TALLOC_FREE(ctx);
2256 reply_nterror(req, status);
2258 #endif /* HAVE_SYS_QUOTAS */
2260 static void handle_nttrans(connection_struct *conn,
2261 struct trans_state *state,
2262 struct smb_request *req)
2264 if (get_Protocol() >= PROTOCOL_NT1) {
2265 req->flags2 |= 0x40; /* IS_LONG_NAME */
2266 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2270 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2272 /* Now we must call the relevant NT_TRANS function */
2273 switch(state->call) {
2274 case NT_TRANSACT_CREATE:
2276 START_PROFILE(NT_transact_create);
2277 call_nt_transact_create(
2278 conn, req,
2279 &state->setup, state->setup_count,
2280 &state->param, state->total_param,
2281 &state->data, state->total_data,
2282 state->max_data_return);
2283 END_PROFILE(NT_transact_create);
2284 break;
2287 case NT_TRANSACT_IOCTL:
2289 START_PROFILE(NT_transact_ioctl);
2290 call_nt_transact_ioctl(
2291 conn, req,
2292 &state->setup, state->setup_count,
2293 &state->param, state->total_param,
2294 &state->data, state->total_data,
2295 state->max_data_return);
2296 END_PROFILE(NT_transact_ioctl);
2297 break;
2300 case NT_TRANSACT_SET_SECURITY_DESC:
2302 START_PROFILE(NT_transact_set_security_desc);
2303 call_nt_transact_set_security_desc(
2304 conn, req,
2305 &state->setup, state->setup_count,
2306 &state->param, state->total_param,
2307 &state->data, state->total_data,
2308 state->max_data_return);
2309 END_PROFILE(NT_transact_set_security_desc);
2310 break;
2313 case NT_TRANSACT_NOTIFY_CHANGE:
2315 START_PROFILE(NT_transact_notify_change);
2316 call_nt_transact_notify_change(
2317 conn, req,
2318 &state->setup, state->setup_count,
2319 &state->param, state->total_param,
2320 &state->data, state->total_data,
2321 state->max_data_return,
2322 state->max_param_return);
2323 END_PROFILE(NT_transact_notify_change);
2324 break;
2327 case NT_TRANSACT_RENAME:
2329 START_PROFILE(NT_transact_rename);
2330 call_nt_transact_rename(
2331 conn, req,
2332 &state->setup, state->setup_count,
2333 &state->param, state->total_param,
2334 &state->data, state->total_data,
2335 state->max_data_return);
2336 END_PROFILE(NT_transact_rename);
2337 break;
2340 case NT_TRANSACT_QUERY_SECURITY_DESC:
2342 START_PROFILE(NT_transact_query_security_desc);
2343 call_nt_transact_query_security_desc(
2344 conn, req,
2345 &state->setup, state->setup_count,
2346 &state->param, state->total_param,
2347 &state->data, state->total_data,
2348 state->max_data_return);
2349 END_PROFILE(NT_transact_query_security_desc);
2350 break;
2353 #ifdef HAVE_SYS_QUOTAS
2354 case NT_TRANSACT_GET_USER_QUOTA:
2356 START_PROFILE(NT_transact_get_user_quota);
2357 call_nt_transact_get_user_quota(
2358 conn, req,
2359 &state->setup, state->setup_count,
2360 &state->param, state->total_param,
2361 &state->data, state->total_data,
2362 state->max_data_return);
2363 END_PROFILE(NT_transact_get_user_quota);
2364 break;
2367 case NT_TRANSACT_SET_USER_QUOTA:
2369 START_PROFILE(NT_transact_set_user_quota);
2370 call_nt_transact_set_user_quota(
2371 conn, req,
2372 &state->setup, state->setup_count,
2373 &state->param, state->total_param,
2374 &state->data, state->total_data,
2375 state->max_data_return);
2376 END_PROFILE(NT_transact_set_user_quota);
2377 break;
2379 #endif /* HAVE_SYS_QUOTAS */
2381 default:
2382 /* Error in request */
2383 DEBUG(0,("handle_nttrans: Unknown request %d in "
2384 "nttrans call\n", state->call));
2385 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2386 return;
2388 return;
2391 /****************************************************************************
2392 Reply to a SMBNTtrans.
2393 ****************************************************************************/
2395 void reply_nttrans(struct smb_request *req)
2397 connection_struct *conn = req->conn;
2398 uint32_t pscnt;
2399 uint32_t psoff;
2400 uint32_t dscnt;
2401 uint32_t dsoff;
2402 uint16_t function_code;
2403 NTSTATUS result;
2404 struct trans_state *state;
2406 START_PROFILE(SMBnttrans);
2408 if (req->wct < 19) {
2409 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2410 END_PROFILE(SMBnttrans);
2411 return;
2414 pscnt = IVAL(req->vwv+9, 1);
2415 psoff = IVAL(req->vwv+11, 1);
2416 dscnt = IVAL(req->vwv+13, 1);
2417 dsoff = IVAL(req->vwv+15, 1);
2418 function_code = SVAL(req->vwv+18, 0);
2420 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2421 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2422 END_PROFILE(SMBnttrans);
2423 return;
2426 result = allow_new_trans(conn->pending_trans, req->mid);
2427 if (!NT_STATUS_IS_OK(result)) {
2428 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2429 reply_nterror(req, result);
2430 END_PROFILE(SMBnttrans);
2431 return;
2434 if ((state = talloc(conn, struct trans_state)) == NULL) {
2435 reply_nterror(req, NT_STATUS_NO_MEMORY);
2436 END_PROFILE(SMBnttrans);
2437 return;
2440 state->cmd = SMBnttrans;
2442 state->mid = req->mid;
2443 state->vuid = req->vuid;
2444 state->total_data = IVAL(req->vwv+3, 1);
2445 state->data = NULL;
2446 state->total_param = IVAL(req->vwv+1, 1);
2447 state->param = NULL;
2448 state->max_data_return = IVAL(req->vwv+7, 1);
2449 state->max_param_return = IVAL(req->vwv+5, 1);
2451 /* setup count is in *words* */
2452 state->setup_count = 2*CVAL(req->vwv+17, 1);
2453 state->setup = NULL;
2454 state->call = function_code;
2456 DEBUG(10, ("num_setup=%u, "
2457 "param_total=%u, this_param=%u, max_param=%u, "
2458 "data_total=%u, this_data=%u, max_data=%u, "
2459 "param_offset=%u, data_offset=%u\n",
2460 (unsigned)state->setup_count,
2461 (unsigned)state->total_param, (unsigned)pscnt,
2462 (unsigned)state->max_param_return,
2463 (unsigned)state->total_data, (unsigned)dscnt,
2464 (unsigned)state->max_data_return,
2465 (unsigned)psoff, (unsigned)dsoff));
2468 * All nttrans messages we handle have smb_wct == 19 +
2469 * state->setup_count. Ensure this is so as a sanity check.
2472 if(req->wct != 19 + (state->setup_count/2)) {
2473 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2474 req->wct, 19 + (state->setup_count/2)));
2475 goto bad_param;
2478 /* Don't allow more than 128mb for each value. */
2479 if ((state->total_data > (1024*1024*128)) ||
2480 (state->total_param > (1024*1024*128))) {
2481 reply_nterror(req, NT_STATUS_NO_MEMORY);
2482 END_PROFILE(SMBnttrans);
2483 return;
2486 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2487 goto bad_param;
2489 if (state->total_data) {
2491 if (smb_buffer_oob(state->total_data, 0, dscnt)
2492 || smb_buffer_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2493 goto bad_param;
2496 /* Can't use talloc here, the core routines do realloc on the
2497 * params and data. */
2498 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2499 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2500 "bytes !\n", (unsigned int)state->total_data));
2501 TALLOC_FREE(state);
2502 reply_nterror(req, NT_STATUS_NO_MEMORY);
2503 END_PROFILE(SMBnttrans);
2504 return;
2507 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2510 if (state->total_param) {
2512 if (smb_buffer_oob(state->total_param, 0, pscnt)
2513 || smb_buffer_oob(smb_len(req->inbuf), psoff, pscnt)) {
2514 goto bad_param;
2517 /* Can't use talloc here, the core routines do realloc on the
2518 * params and data. */
2519 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2520 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2521 "bytes !\n", (unsigned int)state->total_param));
2522 SAFE_FREE(state->data);
2523 TALLOC_FREE(state);
2524 reply_nterror(req, NT_STATUS_NO_MEMORY);
2525 END_PROFILE(SMBnttrans);
2526 return;
2529 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2532 state->received_data = dscnt;
2533 state->received_param = pscnt;
2535 if(state->setup_count > 0) {
2536 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2537 state->setup_count));
2540 * No overflow possible here, state->setup_count is an
2541 * unsigned int, being filled by a single byte from
2542 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2543 * below is not necessary, it's here to clarify things. The
2544 * validity of req->vwv and req->wct has been checked in
2545 * init_smb1_request already.
2547 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2548 goto bad_param;
2551 state->setup = (uint16_t *)TALLOC(state, state->setup_count);
2552 if (state->setup == NULL) {
2553 DEBUG(0,("reply_nttrans : Out of memory\n"));
2554 SAFE_FREE(state->data);
2555 SAFE_FREE(state->param);
2556 TALLOC_FREE(state);
2557 reply_nterror(req, NT_STATUS_NO_MEMORY);
2558 END_PROFILE(SMBnttrans);
2559 return;
2562 memcpy(state->setup, req->vwv+19, state->setup_count);
2563 dump_data(10, (uint8_t *)state->setup, state->setup_count);
2566 if ((state->received_data == state->total_data) &&
2567 (state->received_param == state->total_param)) {
2568 handle_nttrans(conn, state, req);
2569 SAFE_FREE(state->param);
2570 SAFE_FREE(state->data);
2571 TALLOC_FREE(state);
2572 END_PROFILE(SMBnttrans);
2573 return;
2576 DLIST_ADD(conn->pending_trans, state);
2578 /* We need to send an interim response then receive the rest
2579 of the parameter/data bytes */
2580 reply_smb1_outbuf(req, 0, 0);
2581 show_msg((char *)req->outbuf);
2582 END_PROFILE(SMBnttrans);
2583 return;
2585 bad_param:
2587 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2588 SAFE_FREE(state->data);
2589 SAFE_FREE(state->param);
2590 TALLOC_FREE(state);
2591 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2592 END_PROFILE(SMBnttrans);
2593 return;
2596 /****************************************************************************
2597 Reply to a SMBnttranss
2598 ****************************************************************************/
2600 void reply_nttranss(struct smb_request *req)
2602 connection_struct *conn = req->conn;
2603 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2604 struct trans_state *state;
2606 START_PROFILE(SMBnttranss);
2608 show_msg((const char *)req->inbuf);
2610 /* Windows clients expect all replies to
2611 an NT transact secondary (SMBnttranss 0xA1)
2612 to have a command code of NT transact
2613 (SMBnttrans 0xA0). See bug #8989 for details. */
2614 req->cmd = SMBnttrans;
2616 if (req->wct < 18) {
2617 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2618 END_PROFILE(SMBnttranss);
2619 return;
2622 for (state = conn->pending_trans; state != NULL;
2623 state = state->next) {
2624 if (state->mid == req->mid) {
2625 break;
2629 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2630 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2631 END_PROFILE(SMBnttranss);
2632 return;
2635 /* Revise state->total_param and state->total_data in case they have
2636 changed downwards */
2637 if (IVAL(req->vwv+1, 1) < state->total_param) {
2638 state->total_param = IVAL(req->vwv+1, 1);
2640 if (IVAL(req->vwv+3, 1) < state->total_data) {
2641 state->total_data = IVAL(req->vwv+3, 1);
2644 pcnt = IVAL(req->vwv+5, 1);
2645 poff = IVAL(req->vwv+7, 1);
2646 pdisp = IVAL(req->vwv+9, 1);
2648 dcnt = IVAL(req->vwv+11, 1);
2649 doff = IVAL(req->vwv+13, 1);
2650 ddisp = IVAL(req->vwv+15, 1);
2652 state->received_param += pcnt;
2653 state->received_data += dcnt;
2655 if ((state->received_data > state->total_data) ||
2656 (state->received_param > state->total_param))
2657 goto bad_param;
2659 if (pcnt) {
2660 if (smb_buffer_oob(state->total_param, pdisp, pcnt)
2661 || smb_buffer_oob(smb_len(req->inbuf), poff, pcnt)) {
2662 goto bad_param;
2664 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2667 if (dcnt) {
2668 if (smb_buffer_oob(state->total_data, ddisp, dcnt)
2669 || smb_buffer_oob(smb_len(req->inbuf), doff, dcnt)) {
2670 goto bad_param;
2672 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2675 if ((state->received_param < state->total_param) ||
2676 (state->received_data < state->total_data)) {
2677 END_PROFILE(SMBnttranss);
2678 return;
2681 handle_nttrans(conn, state, req);
2683 DLIST_REMOVE(conn->pending_trans, state);
2684 SAFE_FREE(state->data);
2685 SAFE_FREE(state->param);
2686 TALLOC_FREE(state);
2687 END_PROFILE(SMBnttranss);
2688 return;
2690 bad_param:
2692 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2693 DLIST_REMOVE(conn->pending_trans, state);
2694 SAFE_FREE(state->data);
2695 SAFE_FREE(state->param);
2696 TALLOC_FREE(state);
2697 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2698 END_PROFILE(SMBnttranss);
2699 return;