s4 dns: Forward questions we can't answer to another server
[Samba/bb.git] / source3 / smbd / nttrans.c
blobfc52ee5be451f50adddc8fe46c30547d03a4e9ee
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"
33 extern const struct generic_mapping file_generic_mapping;
35 static char *nttrans_realloc(char **ptr, size_t size)
37 if (ptr==NULL) {
38 smb_panic("nttrans_realloc() called with NULL ptr");
41 *ptr = (char *)SMB_REALLOC(*ptr, size);
42 if(*ptr == NULL) {
43 return NULL;
45 memset(*ptr,'\0',size);
46 return *ptr;
49 /****************************************************************************
50 Send the required number of replies back.
51 We assume all fields other than the data fields are
52 set correctly for the type of call.
53 HACK ! Always assumes smb_setup field is zero.
54 ****************************************************************************/
56 static void send_nt_replies(connection_struct *conn,
57 struct smb_request *req, NTSTATUS nt_error,
58 char *params, int paramsize,
59 char *pdata, int datasize)
61 int data_to_send = datasize;
62 int params_to_send = paramsize;
63 int useable_space;
64 char *pp = params;
65 char *pd = pdata;
66 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
67 int alignment_offset = 1;
68 int data_alignment_offset = 0;
69 struct smbd_server_connection *sconn = req->sconn;
70 int max_send = sconn->smb1.sessions.max_send;
73 * If there genuinely are no parameters or data to send just send
74 * the empty packet.
77 if(params_to_send == 0 && data_to_send == 0) {
78 reply_outbuf(req, 18, 0);
79 if (NT_STATUS_V(nt_error)) {
80 error_packet_set((char *)req->outbuf,
81 0, 0, nt_error,
82 __LINE__,__FILE__);
84 show_msg((char *)req->outbuf);
85 if (!srv_send_smb(sconn,
86 (char *)req->outbuf,
87 true, req->seqnum+1,
88 IS_CONN_ENCRYPTED(conn),
89 &req->pcd)) {
90 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
92 TALLOC_FREE(req->outbuf);
93 return;
97 * When sending params and data ensure that both are nicely aligned.
98 * Only do this alignment when there is also data to send - else
99 * can cause NT redirector problems.
102 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
103 data_alignment_offset = 4 - (params_to_send % 4);
107 * Space is bufsize minus Netbios over TCP header minus SMB header.
108 * The alignment_offset is to align the param bytes on a four byte
109 * boundary (2 bytes for data len, one byte pad).
110 * NT needs this to work correctly.
113 useable_space = max_send - (smb_size
114 + 2 * 18 /* wct */
115 + alignment_offset
116 + data_alignment_offset);
118 if (useable_space < 0) {
119 char *msg = talloc_asprintf(
120 talloc_tos(),
121 "send_nt_replies failed sanity useable_space = %d!!!",
122 useable_space);
123 DEBUG(0, ("%s\n", msg));
124 exit_server_cleanly(msg);
127 while (params_to_send || data_to_send) {
130 * Calculate whether we will totally or partially fill this packet.
133 total_sent_thistime = params_to_send + data_to_send;
136 * We can never send more than useable_space.
139 total_sent_thistime = MIN(total_sent_thistime, useable_space);
141 reply_outbuf(req, 18,
142 total_sent_thistime + alignment_offset
143 + data_alignment_offset);
146 * We might have had SMBnttranss in req->inbuf, fix that.
148 SCVAL(req->outbuf, smb_com, SMBnttrans);
151 * Set total params and data to be sent.
154 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
155 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
158 * Calculate how many parameters and data we can fit into
159 * this packet. Parameters get precedence.
162 params_sent_thistime = MIN(params_to_send,useable_space);
163 data_sent_thistime = useable_space - params_sent_thistime;
164 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
166 SIVAL(req->outbuf, smb_ntr_ParameterCount,
167 params_sent_thistime);
169 if(params_sent_thistime == 0) {
170 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
171 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
172 } else {
174 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
175 * parameter bytes, however the first 4 bytes of outbuf are
176 * the Netbios over TCP header. Thus use smb_base() to subtract
177 * them from the calculation.
180 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
181 ((smb_buf(req->outbuf)+alignment_offset)
182 - smb_base(req->outbuf)));
184 * Absolute displacement of param bytes sent in this packet.
187 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
188 pp - params);
192 * Deal with the data portion.
195 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
197 if(data_sent_thistime == 0) {
198 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
199 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
200 } else {
202 * The offset of the data bytes is the offset of the
203 * parameter bytes plus the number of parameters being sent this time.
206 SIVAL(req->outbuf, smb_ntr_DataOffset,
207 ((smb_buf(req->outbuf)+alignment_offset) -
208 smb_base(req->outbuf))
209 + params_sent_thistime + data_alignment_offset);
210 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
214 * Copy the param bytes into the packet.
217 if(params_sent_thistime) {
218 if (alignment_offset != 0) {
219 memset(smb_buf(req->outbuf), 0,
220 alignment_offset);
222 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
223 params_sent_thistime);
227 * Copy in the data bytes
230 if(data_sent_thistime) {
231 if (data_alignment_offset != 0) {
232 memset((smb_buf(req->outbuf)+alignment_offset+
233 params_sent_thistime), 0,
234 data_alignment_offset);
236 memcpy(smb_buf(req->outbuf)+alignment_offset
237 +params_sent_thistime+data_alignment_offset,
238 pd,data_sent_thistime);
241 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
242 params_sent_thistime, data_sent_thistime, useable_space));
243 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
244 params_to_send, data_to_send, paramsize, datasize));
246 if (NT_STATUS_V(nt_error)) {
247 error_packet_set((char *)req->outbuf,
248 0, 0, nt_error,
249 __LINE__,__FILE__);
252 /* Send the packet */
253 show_msg((char *)req->outbuf);
254 if (!srv_send_smb(sconn,
255 (char *)req->outbuf,
256 true, req->seqnum+1,
257 IS_CONN_ENCRYPTED(conn),
258 &req->pcd)) {
259 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
262 TALLOC_FREE(req->outbuf);
264 pp += params_sent_thistime;
265 pd += data_sent_thistime;
267 params_to_send -= params_sent_thistime;
268 data_to_send -= data_sent_thistime;
271 * Sanity check
274 if(params_to_send < 0 || data_to_send < 0) {
275 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
276 params_to_send, data_to_send));
277 exit_server_cleanly("send_nt_replies: internal error");
282 /****************************************************************************
283 Reply to an NT create and X call on a pipe
284 ****************************************************************************/
286 static void nt_open_pipe(char *fname, connection_struct *conn,
287 struct smb_request *req, int *ppnum)
289 files_struct *fsp;
290 NTSTATUS status;
292 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
294 /* Strip \\ off the name. */
295 fname++;
297 status = open_np_file(req, fname, &fsp);
298 if (!NT_STATUS_IS_OK(status)) {
299 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
300 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
301 ERRDOS, ERRbadpipe);
302 return;
304 reply_nterror(req, status);
305 return;
308 *ppnum = fsp->fnum;
309 return;
312 /****************************************************************************
313 Reply to an NT create and X call for pipes.
314 ****************************************************************************/
316 static void do_ntcreate_pipe_open(connection_struct *conn,
317 struct smb_request *req)
319 char *fname = NULL;
320 int pnum = -1;
321 char *p = NULL;
322 uint32 flags = IVAL(req->vwv+3, 1);
323 TALLOC_CTX *ctx = talloc_tos();
325 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
327 if (!fname) {
328 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
329 ERRDOS, ERRbadpipe);
330 return;
332 nt_open_pipe(fname, conn, req, &pnum);
334 if (req->outbuf) {
335 /* error reply */
336 return;
340 * Deal with pipe return.
343 if (flags & EXTENDED_RESPONSE_REQUIRED) {
344 /* This is very strange. We
345 * return 50 words, but only set
346 * the wcnt to 42 ? It's definitely
347 * what happens on the wire....
349 reply_outbuf(req, 50, 0);
350 SCVAL(req->outbuf,smb_wct,42);
351 } else {
352 reply_outbuf(req, 34, 0);
355 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
356 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
358 p = (char *)req->outbuf + smb_vwv2;
359 p++;
360 SSVAL(p,0,pnum);
361 p += 2;
362 SIVAL(p,0,FILE_WAS_OPENED);
363 p += 4;
364 p += 32;
365 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
366 p += 20;
367 /* File type. */
368 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
369 /* Device state. */
370 SSVAL(p,2, 0x5FF); /* ? */
371 p += 4;
373 if (flags & EXTENDED_RESPONSE_REQUIRED) {
374 p += 25;
375 SIVAL(p,0,FILE_GENERIC_ALL);
377 * For pipes W2K3 seems to return
378 * 0x12019B next.
379 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
381 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
384 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
387 struct case_semantics_state {
388 connection_struct *conn;
389 bool case_sensitive;
390 bool case_preserve;
391 bool short_case_preserve;
394 /****************************************************************************
395 Restore case semantics.
396 ****************************************************************************/
398 static int restore_case_semantics(struct case_semantics_state *state)
400 state->conn->case_sensitive = state->case_sensitive;
401 state->conn->case_preserve = state->case_preserve;
402 state->conn->short_case_preserve = state->short_case_preserve;
403 return 0;
406 /****************************************************************************
407 Save case semantics.
408 ****************************************************************************/
410 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
411 connection_struct *conn)
413 struct case_semantics_state *result;
415 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
416 return NULL;
419 result->conn = conn;
420 result->case_sensitive = conn->case_sensitive;
421 result->case_preserve = conn->case_preserve;
422 result->short_case_preserve = conn->short_case_preserve;
424 /* Set to POSIX. */
425 conn->case_sensitive = True;
426 conn->case_preserve = True;
427 conn->short_case_preserve = True;
429 talloc_set_destructor(result, restore_case_semantics);
431 return result;
434 /****************************************************************************
435 Reply to an NT create and X call.
436 ****************************************************************************/
438 void reply_ntcreate_and_X(struct smb_request *req)
440 connection_struct *conn = req->conn;
441 struct smb_filename *smb_fname = NULL;
442 char *fname = NULL;
443 uint32 flags;
444 uint32 access_mask;
445 uint32 file_attributes;
446 uint32 share_access;
447 uint32 create_disposition;
448 uint32 create_options;
449 uint16 root_dir_fid;
450 uint64_t allocation_size;
451 /* Breakout the oplock request bits so we can set the
452 reply bits separately. */
453 uint32 fattr=0;
454 SMB_OFF_T file_len = 0;
455 int info = 0;
456 files_struct *fsp = NULL;
457 char *p = NULL;
458 struct timespec create_timespec;
459 struct timespec c_timespec;
460 struct timespec a_timespec;
461 struct timespec m_timespec;
462 struct timespec write_time_ts;
463 NTSTATUS status;
464 int oplock_request;
465 uint8_t oplock_granted = NO_OPLOCK_RETURN;
466 struct case_semantics_state *case_state = NULL;
467 TALLOC_CTX *ctx = talloc_tos();
469 START_PROFILE(SMBntcreateX);
471 if (req->wct < 24) {
472 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
473 goto out;
476 flags = IVAL(req->vwv+3, 1);
477 access_mask = IVAL(req->vwv+7, 1);
478 file_attributes = IVAL(req->vwv+13, 1);
479 share_access = IVAL(req->vwv+15, 1);
480 create_disposition = IVAL(req->vwv+17, 1);
481 create_options = IVAL(req->vwv+19, 1);
482 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
484 allocation_size = BVAL(req->vwv+9, 1);
486 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
487 STR_TERMINATE, &status);
489 if (!NT_STATUS_IS_OK(status)) {
490 reply_nterror(req, status);
491 goto out;
494 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
495 "file_attributes = 0x%x, share_access = 0x%x, "
496 "create_disposition = 0x%x create_options = 0x%x "
497 "root_dir_fid = 0x%x, fname = %s\n",
498 (unsigned int)flags,
499 (unsigned int)access_mask,
500 (unsigned int)file_attributes,
501 (unsigned int)share_access,
502 (unsigned int)create_disposition,
503 (unsigned int)create_options,
504 (unsigned int)root_dir_fid,
505 fname));
508 * we need to remove ignored bits when they come directly from the client
509 * because we reuse some of them for internal stuff
511 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
514 * If it's an IPC, use the pipe handler.
517 if (IS_IPC(conn)) {
518 if (lp_nt_pipe_support()) {
519 do_ntcreate_pipe_open(conn, req);
520 goto out;
522 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
523 goto out;
526 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
527 if (oplock_request) {
528 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
529 ? BATCH_OPLOCK : 0;
532 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
533 case_state = set_posix_case_semantics(ctx, conn);
534 if (!case_state) {
535 reply_nterror(req, NT_STATUS_NO_MEMORY);
536 goto out;
540 status = filename_convert(ctx,
541 conn,
542 req->flags2 & FLAGS2_DFS_PATHNAMES,
543 fname,
545 NULL,
546 &smb_fname);
548 TALLOC_FREE(case_state);
550 if (!NT_STATUS_IS_OK(status)) {
551 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
552 reply_botherror(req,
553 NT_STATUS_PATH_NOT_COVERED,
554 ERRSRV, ERRbadpath);
555 goto out;
557 reply_nterror(req, status);
558 goto out;
562 * Bug #6898 - clients using Windows opens should
563 * never be able to set this attribute into the
564 * VFS.
566 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
568 status = SMB_VFS_CREATE_FILE(
569 conn, /* conn */
570 req, /* req */
571 root_dir_fid, /* root_dir_fid */
572 smb_fname, /* fname */
573 access_mask, /* access_mask */
574 share_access, /* share_access */
575 create_disposition, /* create_disposition*/
576 create_options, /* create_options */
577 file_attributes, /* file_attributes */
578 oplock_request, /* oplock_request */
579 allocation_size, /* allocation_size */
580 0, /* private_flags */
581 NULL, /* sd */
582 NULL, /* ea_list */
583 &fsp, /* result */
584 &info); /* pinfo */
586 if (!NT_STATUS_IS_OK(status)) {
587 if (open_was_deferred(req->sconn, req->mid)) {
588 /* We have re-scheduled this call, no error. */
589 goto out;
591 reply_openerror(req, status);
592 goto out;
595 /* Ensure we're pointing at the correct stat struct. */
596 TALLOC_FREE(smb_fname);
597 smb_fname = fsp->fsp_name;
600 * If the caller set the extended oplock request bit
601 * and we granted one (by whatever means) - set the
602 * correct bit for extended oplock reply.
605 if (oplock_request &&
606 (lp_fake_oplocks(SNUM(conn))
607 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
610 * Exclusive oplock granted
613 if (flags & REQUEST_BATCH_OPLOCK) {
614 oplock_granted = BATCH_OPLOCK_RETURN;
615 } else {
616 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
618 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
619 oplock_granted = LEVEL_II_OPLOCK_RETURN;
620 } else {
621 oplock_granted = NO_OPLOCK_RETURN;
624 file_len = smb_fname->st.st_ex_size;
626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
627 /* This is very strange. We
628 * return 50 words, but only set
629 * the wcnt to 42 ? It's definitely
630 * what happens on the wire....
632 reply_outbuf(req, 50, 0);
633 SCVAL(req->outbuf,smb_wct,42);
634 } else {
635 reply_outbuf(req, 34, 0);
638 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
639 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
641 p = (char *)req->outbuf + smb_vwv2;
643 SCVAL(p, 0, oplock_granted);
645 p++;
646 SSVAL(p,0,fsp->fnum);
647 p += 2;
648 if ((create_disposition == FILE_SUPERSEDE)
649 && (info == FILE_WAS_OVERWRITTEN)) {
650 SIVAL(p,0,FILE_WAS_SUPERSEDED);
651 } else {
652 SIVAL(p,0,info);
654 p += 4;
656 fattr = dos_mode(conn, smb_fname);
657 if (fattr == 0) {
658 fattr = FILE_ATTRIBUTE_NORMAL;
661 /* Deal with other possible opens having a modified
662 write time. JRA. */
663 ZERO_STRUCT(write_time_ts);
664 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
665 if (!null_timespec(write_time_ts)) {
666 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
669 /* Create time. */
670 create_timespec = get_create_timespec(conn, fsp, smb_fname);
671 a_timespec = smb_fname->st.st_ex_atime;
672 m_timespec = smb_fname->st.st_ex_mtime;
673 c_timespec = get_change_timespec(conn, fsp, smb_fname);
675 if (lp_dos_filetime_resolution(SNUM(conn))) {
676 dos_filetime_timespec(&create_timespec);
677 dos_filetime_timespec(&a_timespec);
678 dos_filetime_timespec(&m_timespec);
679 dos_filetime_timespec(&c_timespec);
682 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
683 p += 8;
684 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
685 p += 8;
686 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
687 p += 8;
688 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
689 p += 8;
690 SIVAL(p,0,fattr); /* File Attributes. */
691 p += 4;
692 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
693 p += 8;
694 SOFF_T(p,0,file_len);
695 p += 8;
696 if (flags & EXTENDED_RESPONSE_REQUIRED) {
697 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
698 size_t num_names = 0;
699 unsigned int num_streams = 0;
700 struct stream_struct *streams = NULL;
702 /* Do we have any EA's ? */
703 status = get_ea_names_from_file(ctx, conn, fsp,
704 smb_fname->base_name, NULL, &num_names);
705 if (NT_STATUS_IS_OK(status) && num_names) {
706 file_status &= ~NO_EAS;
708 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
709 &num_streams, &streams);
710 /* There is always one stream, ::$DATA. */
711 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
712 file_status &= ~NO_SUBSTREAMS;
714 TALLOC_FREE(streams);
715 SSVAL(p,2,file_status);
717 p += 4;
718 SCVAL(p,0,fsp->is_directory ? 1 : 0);
720 if (flags & EXTENDED_RESPONSE_REQUIRED) {
721 uint32 perms = 0;
722 p += 25;
723 if (fsp->is_directory ||
724 can_write_to_file(conn, smb_fname)) {
725 perms = FILE_GENERIC_ALL;
726 } else {
727 perms = FILE_GENERIC_READ|FILE_EXECUTE;
729 SIVAL(p,0,perms);
732 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
733 fsp->fnum, smb_fname_str_dbg(smb_fname)));
735 out:
736 END_PROFILE(SMBntcreateX);
737 return;
740 /****************************************************************************
741 Reply to a NT_TRANSACT_CREATE call to open a pipe.
742 ****************************************************************************/
744 static void do_nt_transact_create_pipe(connection_struct *conn,
745 struct smb_request *req,
746 uint16 **ppsetup, uint32 setup_count,
747 char **ppparams, uint32 parameter_count,
748 char **ppdata, uint32 data_count)
750 char *fname = NULL;
751 char *params = *ppparams;
752 int pnum = -1;
753 char *p = NULL;
754 NTSTATUS status;
755 size_t param_len;
756 uint32 flags;
757 TALLOC_CTX *ctx = talloc_tos();
760 * Ensure minimum number of parameters sent.
763 if(parameter_count < 54) {
764 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
765 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
766 return;
769 flags = IVAL(params,0);
771 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
772 parameter_count-53, STR_TERMINATE,
773 &status);
774 if (!NT_STATUS_IS_OK(status)) {
775 reply_nterror(req, status);
776 return;
779 nt_open_pipe(fname, conn, req, &pnum);
781 if (req->outbuf) {
782 /* Error return */
783 return;
786 /* Realloc the size of parameters and data we will return */
787 if (flags & EXTENDED_RESPONSE_REQUIRED) {
788 /* Extended response is 32 more byyes. */
789 param_len = 101;
790 } else {
791 param_len = 69;
793 params = nttrans_realloc(ppparams, param_len);
794 if(params == NULL) {
795 reply_nterror(req, NT_STATUS_NO_MEMORY);
796 return;
799 p = params;
800 SCVAL(p,0,NO_OPLOCK_RETURN);
802 p += 2;
803 SSVAL(p,0,pnum);
804 p += 2;
805 SIVAL(p,0,FILE_WAS_OPENED);
806 p += 8;
808 p += 32;
809 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
810 p += 20;
811 /* File type. */
812 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
813 /* Device state. */
814 SSVAL(p,2, 0x5FF); /* ? */
815 p += 4;
817 if (flags & EXTENDED_RESPONSE_REQUIRED) {
818 p += 25;
819 SIVAL(p,0,FILE_GENERIC_ALL);
821 * For pipes W2K3 seems to return
822 * 0x12019B next.
823 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
825 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
828 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
830 /* Send the required number of replies */
831 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
833 return;
836 /****************************************************************************
837 Internal fn to set security descriptors.
838 ****************************************************************************/
840 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
841 uint32_t security_info_sent)
843 struct security_descriptor *psd = NULL;
844 NTSTATUS status;
846 if (sd_len == 0) {
847 return NT_STATUS_INVALID_PARAMETER;
850 if (!CAN_WRITE(fsp->conn)) {
851 return NT_STATUS_ACCESS_DENIED;
854 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
855 return NT_STATUS_OK;
858 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
860 if (!NT_STATUS_IS_OK(status)) {
861 return status;
864 if (psd->owner_sid == NULL) {
865 security_info_sent &= ~SECINFO_OWNER;
867 if (psd->group_sid == NULL) {
868 security_info_sent &= ~SECINFO_GROUP;
871 /* Ensure we have at least one thing set. */
872 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
873 if (security_info_sent & SECINFO_LABEL) {
874 /* Only consider SECINFO_LABEL if no other
875 bits are set. Just like W2K3 we don't
876 store this. */
877 return NT_STATUS_OK;
879 return NT_STATUS_INVALID_PARAMETER;
882 /* Ensure we have the rights to do this. */
883 if (security_info_sent & SECINFO_OWNER) {
884 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
885 return NT_STATUS_ACCESS_DENIED;
889 if (security_info_sent & SECINFO_GROUP) {
890 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
891 return NT_STATUS_ACCESS_DENIED;
895 if (security_info_sent & SECINFO_DACL) {
896 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
897 return NT_STATUS_ACCESS_DENIED;
899 /* Convert all the generic bits. */
900 if (psd->dacl) {
901 security_acl_map_generic(psd->dacl, &file_generic_mapping);
905 if (security_info_sent & SECINFO_SACL) {
906 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
907 return NT_STATUS_ACCESS_DENIED;
909 /* Convert all the generic bits. */
910 if (psd->sacl) {
911 security_acl_map_generic(psd->sacl, &file_generic_mapping);
915 if (DEBUGLEVEL >= 10) {
916 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
917 NDR_PRINT_DEBUG(security_descriptor, psd);
920 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
922 TALLOC_FREE(psd);
924 return status;
927 /****************************************************************************
928 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
929 ****************************************************************************/
931 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
933 struct ea_list *ea_list_head = NULL;
934 size_t offset = 0;
936 if (data_size < 4) {
937 return NULL;
940 while (offset + 4 <= data_size) {
941 size_t next_offset = IVAL(pdata,offset);
942 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
944 if (!eal) {
945 return NULL;
948 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
949 if (next_offset == 0) {
950 break;
952 offset += next_offset;
955 return ea_list_head;
958 /****************************************************************************
959 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
960 ****************************************************************************/
962 static void call_nt_transact_create(connection_struct *conn,
963 struct smb_request *req,
964 uint16 **ppsetup, uint32 setup_count,
965 char **ppparams, uint32 parameter_count,
966 char **ppdata, uint32 data_count,
967 uint32 max_data_count)
969 struct smb_filename *smb_fname = NULL;
970 char *fname = NULL;
971 char *params = *ppparams;
972 char *data = *ppdata;
973 /* Breakout the oplock request bits so we can set the reply bits separately. */
974 uint32 fattr=0;
975 SMB_OFF_T file_len = 0;
976 int info = 0;
977 files_struct *fsp = NULL;
978 char *p = NULL;
979 uint32 flags;
980 uint32 access_mask;
981 uint32 file_attributes;
982 uint32 share_access;
983 uint32 create_disposition;
984 uint32 create_options;
985 uint32 sd_len;
986 struct security_descriptor *sd = NULL;
987 uint32 ea_len;
988 uint16 root_dir_fid;
989 struct timespec create_timespec;
990 struct timespec c_timespec;
991 struct timespec a_timespec;
992 struct timespec m_timespec;
993 struct timespec write_time_ts;
994 struct ea_list *ea_list = NULL;
995 NTSTATUS status;
996 size_t param_len;
997 uint64_t allocation_size;
998 int oplock_request;
999 uint8_t oplock_granted;
1000 struct case_semantics_state *case_state = NULL;
1001 TALLOC_CTX *ctx = talloc_tos();
1003 DEBUG(5,("call_nt_transact_create\n"));
1006 * If it's an IPC, use the pipe handler.
1009 if (IS_IPC(conn)) {
1010 if (lp_nt_pipe_support()) {
1011 do_nt_transact_create_pipe(
1012 conn, req,
1013 ppsetup, setup_count,
1014 ppparams, parameter_count,
1015 ppdata, data_count);
1016 goto out;
1018 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1019 goto out;
1023 * Ensure minimum number of parameters sent.
1026 if(parameter_count < 54) {
1027 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1028 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1029 goto out;
1032 flags = IVAL(params,0);
1033 access_mask = IVAL(params,8);
1034 file_attributes = IVAL(params,20);
1035 share_access = IVAL(params,24);
1036 create_disposition = IVAL(params,28);
1037 create_options = IVAL(params,32);
1038 sd_len = IVAL(params,36);
1039 ea_len = IVAL(params,40);
1040 root_dir_fid = (uint16)IVAL(params,4);
1041 allocation_size = BVAL(params,12);
1044 * we need to remove ignored bits when they come directly from the client
1045 * because we reuse some of them for internal stuff
1047 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1049 /* Ensure the data_len is correct for the sd and ea values given. */
1050 if ((ea_len + sd_len > data_count)
1051 || (ea_len > data_count) || (sd_len > data_count)
1052 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1053 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1054 "%u, data_count = %u\n", (unsigned int)ea_len,
1055 (unsigned int)sd_len, (unsigned int)data_count));
1056 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1057 goto out;
1060 if (sd_len) {
1061 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1062 sd_len));
1064 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1065 &sd);
1066 if (!NT_STATUS_IS_OK(status)) {
1067 DEBUG(10, ("call_nt_transact_create: "
1068 "unmarshall_sec_desc failed: %s\n",
1069 nt_errstr(status)));
1070 reply_nterror(req, status);
1071 goto out;
1075 if (ea_len) {
1076 if (!lp_ea_support(SNUM(conn))) {
1077 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1078 "EA's not supported.\n",
1079 (unsigned int)ea_len));
1080 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1081 goto out;
1084 if (ea_len < 10) {
1085 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1086 "too small (should be more than 10)\n",
1087 (unsigned int)ea_len ));
1088 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1089 goto out;
1092 /* We have already checked that ea_len <= data_count here. */
1093 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1094 ea_len);
1095 if (ea_list == NULL) {
1096 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1097 goto out;
1101 srvstr_get_path(ctx, params, req->flags2, &fname,
1102 params+53, parameter_count-53,
1103 STR_TERMINATE, &status);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 reply_nterror(req, status);
1106 goto out;
1109 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1110 case_state = set_posix_case_semantics(ctx, conn);
1111 if (!case_state) {
1112 reply_nterror(req, NT_STATUS_NO_MEMORY);
1113 goto out;
1117 status = filename_convert(ctx,
1118 conn,
1119 req->flags2 & FLAGS2_DFS_PATHNAMES,
1120 fname,
1122 NULL,
1123 &smb_fname);
1125 TALLOC_FREE(case_state);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1129 reply_botherror(req,
1130 NT_STATUS_PATH_NOT_COVERED,
1131 ERRSRV, ERRbadpath);
1132 goto out;
1134 reply_nterror(req, status);
1135 goto out;
1138 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1139 if (oplock_request) {
1140 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1141 ? BATCH_OPLOCK : 0;
1145 * Bug #6898 - clients using Windows opens should
1146 * never be able to set this attribute into the
1147 * VFS.
1149 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1151 status = SMB_VFS_CREATE_FILE(
1152 conn, /* conn */
1153 req, /* req */
1154 root_dir_fid, /* root_dir_fid */
1155 smb_fname, /* fname */
1156 access_mask, /* access_mask */
1157 share_access, /* share_access */
1158 create_disposition, /* create_disposition*/
1159 create_options, /* create_options */
1160 file_attributes, /* file_attributes */
1161 oplock_request, /* oplock_request */
1162 allocation_size, /* allocation_size */
1163 0, /* private_flags */
1164 sd, /* sd */
1165 ea_list, /* ea_list */
1166 &fsp, /* result */
1167 &info); /* pinfo */
1169 if(!NT_STATUS_IS_OK(status)) {
1170 if (open_was_deferred(req->sconn, req->mid)) {
1171 /* We have re-scheduled this call, no error. */
1172 return;
1174 reply_openerror(req, status);
1175 goto out;
1178 /* Ensure we're pointing at the correct stat struct. */
1179 TALLOC_FREE(smb_fname);
1180 smb_fname = fsp->fsp_name;
1183 * If the caller set the extended oplock request bit
1184 * and we granted one (by whatever means) - set the
1185 * correct bit for extended oplock reply.
1188 if (oplock_request &&
1189 (lp_fake_oplocks(SNUM(conn))
1190 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1193 * Exclusive oplock granted
1196 if (flags & REQUEST_BATCH_OPLOCK) {
1197 oplock_granted = BATCH_OPLOCK_RETURN;
1198 } else {
1199 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1201 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1202 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1203 } else {
1204 oplock_granted = NO_OPLOCK_RETURN;
1207 file_len = smb_fname->st.st_ex_size;
1209 /* Realloc the size of parameters and data we will return */
1210 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1211 /* Extended response is 32 more byyes. */
1212 param_len = 101;
1213 } else {
1214 param_len = 69;
1216 params = nttrans_realloc(ppparams, param_len);
1217 if(params == NULL) {
1218 reply_nterror(req, NT_STATUS_NO_MEMORY);
1219 goto out;
1222 p = params;
1223 SCVAL(p, 0, oplock_granted);
1225 p += 2;
1226 SSVAL(p,0,fsp->fnum);
1227 p += 2;
1228 if ((create_disposition == FILE_SUPERSEDE)
1229 && (info == FILE_WAS_OVERWRITTEN)) {
1230 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1231 } else {
1232 SIVAL(p,0,info);
1234 p += 8;
1236 fattr = dos_mode(conn, smb_fname);
1237 if (fattr == 0) {
1238 fattr = FILE_ATTRIBUTE_NORMAL;
1241 /* Deal with other possible opens having a modified
1242 write time. JRA. */
1243 ZERO_STRUCT(write_time_ts);
1244 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1245 if (!null_timespec(write_time_ts)) {
1246 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1249 /* Create time. */
1250 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1251 a_timespec = smb_fname->st.st_ex_atime;
1252 m_timespec = smb_fname->st.st_ex_mtime;
1253 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1255 if (lp_dos_filetime_resolution(SNUM(conn))) {
1256 dos_filetime_timespec(&create_timespec);
1257 dos_filetime_timespec(&a_timespec);
1258 dos_filetime_timespec(&m_timespec);
1259 dos_filetime_timespec(&c_timespec);
1262 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1263 p += 8;
1264 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1265 p += 8;
1266 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1267 p += 8;
1268 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1269 p += 8;
1270 SIVAL(p,0,fattr); /* File Attributes. */
1271 p += 4;
1272 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1273 p += 8;
1274 SOFF_T(p,0,file_len);
1275 p += 8;
1276 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1277 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1278 size_t num_names = 0;
1279 unsigned int num_streams = 0;
1280 struct stream_struct *streams = NULL;
1282 /* Do we have any EA's ? */
1283 status = get_ea_names_from_file(ctx, conn, fsp,
1284 smb_fname->base_name, NULL, &num_names);
1285 if (NT_STATUS_IS_OK(status) && num_names) {
1286 file_status &= ~NO_EAS;
1288 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1289 &num_streams, &streams);
1290 /* There is always one stream, ::$DATA. */
1291 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1292 file_status &= ~NO_SUBSTREAMS;
1294 TALLOC_FREE(streams);
1295 SSVAL(p,2,file_status);
1297 p += 4;
1298 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1300 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1301 uint32 perms = 0;
1302 p += 25;
1303 if (fsp->is_directory ||
1304 can_write_to_file(conn, smb_fname)) {
1305 perms = FILE_GENERIC_ALL;
1306 } else {
1307 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1309 SIVAL(p,0,perms);
1312 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1313 smb_fname_str_dbg(smb_fname)));
1315 /* Send the required number of replies */
1316 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1317 out:
1318 return;
1321 /****************************************************************************
1322 Reply to a NT CANCEL request.
1323 conn POINTER CAN BE NULL HERE !
1324 ****************************************************************************/
1326 void reply_ntcancel(struct smb_request *req)
1329 * Go through and cancel any pending change notifies.
1332 START_PROFILE(SMBntcancel);
1333 srv_cancel_sign_response(req->sconn);
1334 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1335 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1337 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1338 (unsigned long long)req->mid));
1340 END_PROFILE(SMBntcancel);
1341 return;
1344 /****************************************************************************
1345 Copy a file.
1346 ****************************************************************************/
1348 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1349 connection_struct *conn,
1350 struct smb_request *req,
1351 struct smb_filename *smb_fname_src,
1352 struct smb_filename *smb_fname_dst,
1353 uint32 attrs)
1355 files_struct *fsp1,*fsp2;
1356 uint32 fattr;
1357 int info;
1358 SMB_OFF_T ret=-1;
1359 NTSTATUS status = NT_STATUS_OK;
1360 char *parent;
1362 if (!CAN_WRITE(conn)) {
1363 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1364 goto out;
1367 /* Source must already exist. */
1368 if (!VALID_STAT(smb_fname_src->st)) {
1369 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1370 goto out;
1373 /* Ensure attributes match. */
1374 fattr = dos_mode(conn, smb_fname_src);
1375 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1376 status = NT_STATUS_NO_SUCH_FILE;
1377 goto out;
1380 /* Disallow if dst file already exists. */
1381 if (VALID_STAT(smb_fname_dst->st)) {
1382 status = NT_STATUS_OBJECT_NAME_COLLISION;
1383 goto out;
1386 /* No links from a directory. */
1387 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1388 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1389 goto out;
1392 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1393 smb_fname_str_dbg(smb_fname_src),
1394 smb_fname_str_dbg(smb_fname_dst)));
1396 status = SMB_VFS_CREATE_FILE(
1397 conn, /* conn */
1398 req, /* req */
1399 0, /* root_dir_fid */
1400 smb_fname_src, /* fname */
1401 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1402 FILE_READ_EA, /* access_mask */
1403 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1404 FILE_SHARE_DELETE),
1405 FILE_OPEN, /* create_disposition*/
1406 0, /* create_options */
1407 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1408 NO_OPLOCK, /* oplock_request */
1409 0, /* allocation_size */
1410 0, /* private_flags */
1411 NULL, /* sd */
1412 NULL, /* ea_list */
1413 &fsp1, /* result */
1414 &info); /* pinfo */
1416 if (!NT_STATUS_IS_OK(status)) {
1417 goto out;
1420 status = SMB_VFS_CREATE_FILE(
1421 conn, /* conn */
1422 req, /* req */
1423 0, /* root_dir_fid */
1424 smb_fname_dst, /* fname */
1425 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1426 FILE_WRITE_EA, /* access_mask */
1427 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1428 FILE_SHARE_DELETE),
1429 FILE_CREATE, /* create_disposition*/
1430 0, /* create_options */
1431 fattr, /* file_attributes */
1432 NO_OPLOCK, /* oplock_request */
1433 0, /* allocation_size */
1434 0, /* private_flags */
1435 NULL, /* sd */
1436 NULL, /* ea_list */
1437 &fsp2, /* result */
1438 &info); /* pinfo */
1440 if (!NT_STATUS_IS_OK(status)) {
1441 close_file(NULL, fsp1, ERROR_CLOSE);
1442 goto out;
1445 if (smb_fname_src->st.st_ex_size) {
1446 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1450 * As we are opening fsp1 read-only we only expect
1451 * an error on close on fsp2 if we are out of space.
1452 * Thus we don't look at the error return from the
1453 * close of fsp1.
1455 close_file(NULL, fsp1, NORMAL_CLOSE);
1457 /* Ensure the modtime is set correctly on the destination file. */
1458 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1460 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1462 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1463 creates the file. This isn't the correct thing to do in the copy
1464 case. JRA */
1465 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1466 NULL)) {
1467 status = NT_STATUS_NO_MEMORY;
1468 goto out;
1470 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1471 TALLOC_FREE(parent);
1473 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1474 status = NT_STATUS_DISK_FULL;
1475 goto out;
1477 out:
1478 if (!NT_STATUS_IS_OK(status)) {
1479 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1480 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1481 smb_fname_str_dbg(smb_fname_dst)));
1484 return status;
1487 /****************************************************************************
1488 Reply to a NT rename request.
1489 ****************************************************************************/
1491 void reply_ntrename(struct smb_request *req)
1493 connection_struct *conn = req->conn;
1494 struct smb_filename *smb_fname_old = NULL;
1495 struct smb_filename *smb_fname_new = NULL;
1496 char *oldname = NULL;
1497 char *newname = NULL;
1498 const char *p;
1499 NTSTATUS status;
1500 bool src_has_wcard = False;
1501 bool dest_has_wcard = False;
1502 uint32 attrs;
1503 uint32_t ucf_flags_src = 0;
1504 uint32_t ucf_flags_dst = 0;
1505 uint16 rename_type;
1506 TALLOC_CTX *ctx = talloc_tos();
1507 bool stream_rename = false;
1509 START_PROFILE(SMBntrename);
1511 if (req->wct < 4) {
1512 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1513 goto out;
1516 attrs = SVAL(req->vwv+0, 0);
1517 rename_type = SVAL(req->vwv+1, 0);
1519 p = (const char *)req->buf + 1;
1520 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1521 &status, &src_has_wcard);
1522 if (!NT_STATUS_IS_OK(status)) {
1523 reply_nterror(req, status);
1524 goto out;
1527 if (ms_has_wild(oldname)) {
1528 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1529 goto out;
1532 p++;
1533 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1534 &status, &dest_has_wcard);
1535 if (!NT_STATUS_IS_OK(status)) {
1536 reply_nterror(req, status);
1537 goto out;
1540 if (!lp_posix_pathnames()) {
1541 /* The newname must begin with a ':' if the
1542 oldname contains a ':'. */
1543 if (strchr_m(oldname, ':')) {
1544 if (newname[0] != ':') {
1545 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1546 goto out;
1548 stream_rename = true;
1553 * If this is a rename operation, allow wildcards and save the
1554 * destination's last component.
1556 if (rename_type == RENAME_FLAG_RENAME) {
1557 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1558 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1561 /* rename_internals() calls unix_convert(), so don't call it here. */
1562 status = filename_convert(ctx, conn,
1563 req->flags2 & FLAGS2_DFS_PATHNAMES,
1564 oldname,
1565 ucf_flags_src,
1566 NULL,
1567 &smb_fname_old);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 if (NT_STATUS_EQUAL(status,
1570 NT_STATUS_PATH_NOT_COVERED)) {
1571 reply_botherror(req,
1572 NT_STATUS_PATH_NOT_COVERED,
1573 ERRSRV, ERRbadpath);
1574 goto out;
1576 reply_nterror(req, status);
1577 goto out;
1580 status = filename_convert(ctx, conn,
1581 req->flags2 & FLAGS2_DFS_PATHNAMES,
1582 newname,
1583 ucf_flags_dst,
1584 &dest_has_wcard,
1585 &smb_fname_new);
1586 if (!NT_STATUS_IS_OK(status)) {
1587 if (NT_STATUS_EQUAL(status,
1588 NT_STATUS_PATH_NOT_COVERED)) {
1589 reply_botherror(req,
1590 NT_STATUS_PATH_NOT_COVERED,
1591 ERRSRV, ERRbadpath);
1592 goto out;
1594 reply_nterror(req, status);
1595 goto out;
1598 if (stream_rename) {
1599 /* smb_fname_new must be the same as smb_fname_old. */
1600 TALLOC_FREE(smb_fname_new->base_name);
1601 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1602 smb_fname_old->base_name);
1603 if (!smb_fname_new->base_name) {
1604 reply_nterror(req, NT_STATUS_NO_MEMORY);
1605 goto out;
1609 DEBUG(3,("reply_ntrename: %s -> %s\n",
1610 smb_fname_str_dbg(smb_fname_old),
1611 smb_fname_str_dbg(smb_fname_new)));
1613 switch(rename_type) {
1614 case RENAME_FLAG_RENAME:
1615 status = rename_internals(ctx, conn, req,
1616 smb_fname_old, smb_fname_new,
1617 attrs, False, src_has_wcard,
1618 dest_has_wcard,
1619 DELETE_ACCESS);
1620 break;
1621 case RENAME_FLAG_HARD_LINK:
1622 if (src_has_wcard || dest_has_wcard) {
1623 /* No wildcards. */
1624 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1625 } else {
1626 status = hardlink_internals(ctx, conn,
1627 req,
1628 false,
1629 smb_fname_old,
1630 smb_fname_new);
1632 break;
1633 case RENAME_FLAG_COPY:
1634 if (src_has_wcard || dest_has_wcard) {
1635 /* No wildcards. */
1636 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1637 } else {
1638 status = copy_internals(ctx, conn, req,
1639 smb_fname_old,
1640 smb_fname_new,
1641 attrs);
1643 break;
1644 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1645 status = NT_STATUS_INVALID_PARAMETER;
1646 break;
1647 default:
1648 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1649 break;
1652 if (!NT_STATUS_IS_OK(status)) {
1653 if (open_was_deferred(req->sconn, req->mid)) {
1654 /* We have re-scheduled this call. */
1655 goto out;
1658 reply_nterror(req, status);
1659 goto out;
1662 reply_outbuf(req, 0, 0);
1663 out:
1664 END_PROFILE(SMBntrename);
1665 return;
1668 /****************************************************************************
1669 Reply to a notify change - queue the request and
1670 don't allow a directory to be opened.
1671 ****************************************************************************/
1673 static void smbd_smb1_notify_reply(struct smb_request *req,
1674 NTSTATUS error_code,
1675 uint8_t *buf, size_t len)
1677 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1680 static void call_nt_transact_notify_change(connection_struct *conn,
1681 struct smb_request *req,
1682 uint16 **ppsetup,
1683 uint32 setup_count,
1684 char **ppparams,
1685 uint32 parameter_count,
1686 char **ppdata, uint32 data_count,
1687 uint32 max_data_count,
1688 uint32 max_param_count)
1690 uint16 *setup = *ppsetup;
1691 files_struct *fsp;
1692 uint32 filter;
1693 NTSTATUS status;
1694 bool recursive;
1696 if(setup_count < 6) {
1697 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1698 return;
1701 fsp = file_fsp(req, SVAL(setup,4));
1702 filter = IVAL(setup, 0);
1703 recursive = (SVAL(setup, 6) != 0) ? True : False;
1705 DEBUG(3,("call_nt_transact_notify_change\n"));
1707 if(!fsp) {
1708 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1709 return;
1713 char *filter_string;
1715 if (!(filter_string = notify_filter_string(NULL, filter))) {
1716 reply_nterror(req,NT_STATUS_NO_MEMORY);
1717 return;
1720 DEBUG(3,("call_nt_transact_notify_change: notify change "
1721 "called on %s, filter = %s, recursive = %d\n",
1722 fsp_str_dbg(fsp), filter_string, recursive));
1724 TALLOC_FREE(filter_string);
1727 if((!fsp->is_directory) || (conn != fsp->conn)) {
1728 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1729 return;
1732 if (fsp->notify == NULL) {
1734 status = change_notify_create(fsp, filter, recursive);
1736 if (!NT_STATUS_IS_OK(status)) {
1737 DEBUG(10, ("change_notify_create returned %s\n",
1738 nt_errstr(status)));
1739 reply_nterror(req, status);
1740 return;
1744 if (fsp->notify->num_changes != 0) {
1747 * We've got changes pending, respond immediately
1751 * TODO: write a torture test to check the filtering behaviour
1752 * here.
1755 change_notify_reply(req,
1756 NT_STATUS_OK,
1757 max_param_count,
1758 fsp->notify,
1759 smbd_smb1_notify_reply);
1762 * change_notify_reply() above has independently sent its
1763 * results
1765 return;
1769 * No changes pending, queue the request
1772 status = change_notify_add_request(req,
1773 max_param_count,
1774 filter,
1775 recursive, fsp,
1776 smbd_smb1_notify_reply);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 reply_nterror(req, status);
1780 return;
1783 /****************************************************************************
1784 Reply to an NT transact rename command.
1785 ****************************************************************************/
1787 static void call_nt_transact_rename(connection_struct *conn,
1788 struct smb_request *req,
1789 uint16 **ppsetup, uint32 setup_count,
1790 char **ppparams, uint32 parameter_count,
1791 char **ppdata, uint32 data_count,
1792 uint32 max_data_count)
1794 char *params = *ppparams;
1795 char *new_name = NULL;
1796 files_struct *fsp = NULL;
1797 bool dest_has_wcard = False;
1798 NTSTATUS status;
1799 TALLOC_CTX *ctx = talloc_tos();
1801 if(parameter_count < 5) {
1802 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1803 return;
1806 fsp = file_fsp(req, SVAL(params, 0));
1807 if (!check_fsp(conn, req, fsp)) {
1808 return;
1810 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1811 parameter_count - 4,
1812 STR_TERMINATE, &status, &dest_has_wcard);
1813 if (!NT_STATUS_IS_OK(status)) {
1814 reply_nterror(req, status);
1815 return;
1819 * W2K3 ignores this request as the RAW-RENAME test
1820 * demonstrates, so we do.
1822 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1824 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1825 fsp_str_dbg(fsp), new_name));
1827 return;
1830 /******************************************************************************
1831 Fake up a completely empty SD.
1832 *******************************************************************************/
1834 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1836 size_t sd_size;
1838 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1839 if(!*ppsd) {
1840 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1841 return NT_STATUS_NO_MEMORY;
1844 return NT_STATUS_OK;
1847 /****************************************************************************
1848 Reply to query a security descriptor.
1849 Callable from SMB2 and SMB2.
1850 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1851 the required size.
1852 ****************************************************************************/
1854 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1855 TALLOC_CTX *mem_ctx,
1856 files_struct *fsp,
1857 uint32_t security_info_wanted,
1858 uint32_t max_data_count,
1859 uint8_t **ppmarshalled_sd,
1860 size_t *psd_size)
1862 NTSTATUS status;
1863 struct security_descriptor *psd = NULL;
1866 * Get the permissions to return.
1869 if ((security_info_wanted & SECINFO_SACL) &&
1870 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1871 return NT_STATUS_ACCESS_DENIED;
1874 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1875 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1876 return NT_STATUS_ACCESS_DENIED;
1879 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1880 SECINFO_GROUP|SECINFO_SACL)) {
1881 /* Don't return SECINFO_LABEL if anything else was
1882 requested. See bug #8458. */
1883 security_info_wanted &= ~SECINFO_LABEL;
1886 if (!lp_nt_acl_support(SNUM(conn))) {
1887 status = get_null_nt_acl(mem_ctx, &psd);
1888 } else if (security_info_wanted & SECINFO_LABEL) {
1889 /* Like W2K3 return a null object. */
1890 status = get_null_nt_acl(mem_ctx, &psd);
1891 } else {
1892 status = SMB_VFS_FGET_NT_ACL(
1893 fsp, security_info_wanted, &psd);
1895 if (!NT_STATUS_IS_OK(status)) {
1896 return status;
1899 if (!(security_info_wanted & SECINFO_OWNER)) {
1900 psd->owner_sid = NULL;
1902 if (!(security_info_wanted & SECINFO_GROUP)) {
1903 psd->group_sid = NULL;
1905 if (!(security_info_wanted & SECINFO_DACL)) {
1906 psd->type &= ~SEC_DESC_DACL_PRESENT;
1907 psd->dacl = NULL;
1909 if (!(security_info_wanted & SECINFO_SACL)) {
1910 psd->type &= ~SEC_DESC_SACL_PRESENT;
1911 psd->sacl = NULL;
1914 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1915 * present in the reply to match Windows behavior */
1916 if (psd->sacl == NULL &&
1917 security_info_wanted & SECINFO_SACL)
1918 psd->type |= SEC_DESC_SACL_PRESENT;
1919 if (psd->dacl == NULL &&
1920 security_info_wanted & SECINFO_DACL)
1921 psd->type |= SEC_DESC_DACL_PRESENT;
1923 if (security_info_wanted & SECINFO_LABEL) {
1924 /* Like W2K3 return a null object. */
1925 psd->owner_sid = NULL;
1926 psd->group_sid = NULL;
1927 psd->dacl = NULL;
1928 psd->sacl = NULL;
1929 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1932 *psd_size = ndr_size_security_descriptor(psd, 0);
1934 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1935 (unsigned long)*psd_size));
1937 if (DEBUGLEVEL >= 10) {
1938 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1939 fsp_str_dbg(fsp)));
1940 NDR_PRINT_DEBUG(security_descriptor, psd);
1943 if (max_data_count < *psd_size) {
1944 TALLOC_FREE(psd);
1945 return NT_STATUS_BUFFER_TOO_SMALL;
1948 status = marshall_sec_desc(mem_ctx, psd,
1949 ppmarshalled_sd, psd_size);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 TALLOC_FREE(psd);
1953 return status;
1956 TALLOC_FREE(psd);
1957 return NT_STATUS_OK;
1960 /****************************************************************************
1961 SMB1 reply to query a security descriptor.
1962 ****************************************************************************/
1964 static void call_nt_transact_query_security_desc(connection_struct *conn,
1965 struct smb_request *req,
1966 uint16 **ppsetup,
1967 uint32 setup_count,
1968 char **ppparams,
1969 uint32 parameter_count,
1970 char **ppdata,
1971 uint32 data_count,
1972 uint32 max_data_count)
1974 char *params = *ppparams;
1975 char *data = *ppdata;
1976 size_t sd_size = 0;
1977 uint32 security_info_wanted;
1978 files_struct *fsp = NULL;
1979 NTSTATUS status;
1980 uint8_t *marshalled_sd = NULL;
1982 if(parameter_count < 8) {
1983 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1984 return;
1987 fsp = file_fsp(req, SVAL(params,0));
1988 if(!fsp) {
1989 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1990 return;
1993 security_info_wanted = IVAL(params,4);
1995 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1996 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1997 (unsigned int)security_info_wanted));
1999 params = nttrans_realloc(ppparams, 4);
2000 if(params == NULL) {
2001 reply_nterror(req, NT_STATUS_NO_MEMORY);
2002 return;
2006 * Get the permissions to return.
2009 status = smbd_do_query_security_desc(conn,
2010 talloc_tos(),
2011 fsp,
2012 security_info_wanted,
2013 max_data_count,
2014 &marshalled_sd,
2015 &sd_size);
2017 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2018 SIVAL(params,0,(uint32_t)sd_size);
2019 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2020 params, 4, NULL, 0);
2021 return;
2024 if (!NT_STATUS_IS_OK(status)) {
2025 reply_nterror(req, status);
2026 return;
2029 SMB_ASSERT(sd_size > 0);
2031 SIVAL(params,0,(uint32_t)sd_size);
2033 if (max_data_count < sd_size) {
2034 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2035 params, 4, NULL, 0);
2036 return;
2040 * Allocate the data we will return.
2043 data = nttrans_realloc(ppdata, sd_size);
2044 if(data == NULL) {
2045 reply_nterror(req, NT_STATUS_NO_MEMORY);
2046 return;
2049 memcpy(data, marshalled_sd, sd_size);
2051 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2053 return;
2056 /****************************************************************************
2057 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2058 ****************************************************************************/
2060 static void call_nt_transact_set_security_desc(connection_struct *conn,
2061 struct smb_request *req,
2062 uint16 **ppsetup,
2063 uint32 setup_count,
2064 char **ppparams,
2065 uint32 parameter_count,
2066 char **ppdata,
2067 uint32 data_count,
2068 uint32 max_data_count)
2070 char *params= *ppparams;
2071 char *data = *ppdata;
2072 files_struct *fsp = NULL;
2073 uint32 security_info_sent = 0;
2074 NTSTATUS status;
2076 if(parameter_count < 8) {
2077 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2078 return;
2081 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2082 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2083 return;
2086 if (!CAN_WRITE(fsp->conn)) {
2087 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2088 return;
2091 if(!lp_nt_acl_support(SNUM(conn))) {
2092 goto done;
2095 security_info_sent = IVAL(params,4);
2097 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2098 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2100 if (data_count == 0) {
2101 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2102 return;
2105 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2107 if (!NT_STATUS_IS_OK(status)) {
2108 reply_nterror(req, status);
2109 return;
2112 done:
2113 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2114 return;
2117 /****************************************************************************
2118 Reply to NT IOCTL
2119 ****************************************************************************/
2121 static void call_nt_transact_ioctl(connection_struct *conn,
2122 struct smb_request *req,
2123 uint16 **ppsetup, uint32 setup_count,
2124 char **ppparams, uint32 parameter_count,
2125 char **ppdata, uint32 data_count,
2126 uint32 max_data_count)
2128 NTSTATUS status;
2129 uint32 function;
2130 uint16 fidnum;
2131 files_struct *fsp;
2132 uint8 isFSctl;
2133 uint8 compfilter;
2134 char *out_data = NULL;
2135 uint32 out_data_len = 0;
2136 char *pdata = *ppdata;
2137 TALLOC_CTX *ctx = talloc_tos();
2139 if (setup_count != 8) {
2140 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2141 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2142 return;
2145 function = IVAL(*ppsetup, 0);
2146 fidnum = SVAL(*ppsetup, 4);
2147 isFSctl = CVAL(*ppsetup, 6);
2148 compfilter = CVAL(*ppsetup, 7);
2150 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2151 function, fidnum, isFSctl, compfilter));
2153 fsp=file_fsp(req, fidnum);
2156 * We don't really implement IOCTLs, especially on files.
2158 if (!isFSctl) {
2159 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2160 isFSctl));
2161 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2162 return;
2165 /* Has to be for an open file! */
2166 if (!check_fsp_open(conn, req, fsp)) {
2167 return;
2170 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2173 * out_data might be allocated by the VFS module, but talloc should be
2174 * used, and should be cleaned up when the request ends.
2176 status = SMB_VFS_FSCTL(fsp,
2177 ctx,
2178 function,
2179 req->flags2,
2180 (uint8_t *)pdata,
2181 data_count,
2182 (uint8_t **)&out_data,
2183 max_data_count,
2184 &out_data_len);
2185 if (!NT_STATUS_IS_OK(status)) {
2186 reply_nterror(req, status);
2187 } else {
2188 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2193 #ifdef HAVE_SYS_QUOTAS
2194 /****************************************************************************
2195 Reply to get user quota
2196 ****************************************************************************/
2198 static void call_nt_transact_get_user_quota(connection_struct *conn,
2199 struct smb_request *req,
2200 uint16 **ppsetup,
2201 uint32 setup_count,
2202 char **ppparams,
2203 uint32 parameter_count,
2204 char **ppdata,
2205 uint32 data_count,
2206 uint32 max_data_count)
2208 NTSTATUS nt_status = NT_STATUS_OK;
2209 char *params = *ppparams;
2210 char *pdata = *ppdata;
2211 char *entry;
2212 int data_len=0,param_len=0;
2213 int qt_len=0;
2214 int entry_len = 0;
2215 files_struct *fsp = NULL;
2216 uint16 level = 0;
2217 size_t sid_len;
2218 struct dom_sid sid;
2219 bool start_enum = True;
2220 SMB_NTQUOTA_STRUCT qt;
2221 SMB_NTQUOTA_LIST *tmp_list;
2222 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2224 ZERO_STRUCT(qt);
2226 /* access check */
2227 if (get_current_uid(conn) != 0) {
2228 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2229 "[%s]\n", lp_servicename(SNUM(conn)),
2230 conn->session_info->unix_info->unix_name));
2231 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2232 return;
2236 * Ensure minimum number of parameters sent.
2239 if (parameter_count < 4) {
2240 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2241 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2242 return;
2245 /* maybe we can check the quota_fnum */
2246 fsp = file_fsp(req, SVAL(params,0));
2247 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2248 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2249 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2250 return;
2253 /* the NULL pointer checking for fsp->fake_file_handle->pd
2254 * is done by CHECK_NTQUOTA_HANDLE_OK()
2256 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2258 level = SVAL(params,2);
2260 /* unknown 12 bytes leading in params */
2262 switch (level) {
2263 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2264 /* seems that we should continue with the enum here --metze */
2266 if (qt_handle->quota_list!=NULL &&
2267 qt_handle->tmp_list==NULL) {
2269 /* free the list */
2270 free_ntquota_list(&(qt_handle->quota_list));
2272 /* Realloc the size of parameters and data we will return */
2273 param_len = 4;
2274 params = nttrans_realloc(ppparams, param_len);
2275 if(params == NULL) {
2276 reply_nterror(req, NT_STATUS_NO_MEMORY);
2277 return;
2280 data_len = 0;
2281 SIVAL(params,0,data_len);
2283 break;
2286 start_enum = False;
2288 case TRANSACT_GET_USER_QUOTA_LIST_START:
2290 if (qt_handle->quota_list==NULL &&
2291 qt_handle->tmp_list==NULL) {
2292 start_enum = True;
2295 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2296 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2297 return;
2300 /* Realloc the size of parameters and data we will return */
2301 param_len = 4;
2302 params = nttrans_realloc(ppparams, param_len);
2303 if(params == NULL) {
2304 reply_nterror(req, NT_STATUS_NO_MEMORY);
2305 return;
2308 /* we should not trust the value in max_data_count*/
2309 max_data_count = MIN(max_data_count,2048);
2311 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2312 if(pdata == NULL) {
2313 reply_nterror(req, NT_STATUS_NO_MEMORY);
2314 return;
2317 entry = pdata;
2319 /* set params Size of returned Quota Data 4 bytes*/
2320 /* but set it later when we know it */
2322 /* for each entry push the data */
2324 if (start_enum) {
2325 qt_handle->tmp_list = qt_handle->quota_list;
2328 tmp_list = qt_handle->tmp_list;
2330 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2331 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2333 sid_len = ndr_size_dom_sid(
2334 &tmp_list->quotas->sid, 0);
2335 entry_len = 40 + sid_len;
2337 /* nextoffset entry 4 bytes */
2338 SIVAL(entry,0,entry_len);
2340 /* then the len of the SID 4 bytes */
2341 SIVAL(entry,4,sid_len);
2343 /* unknown data 8 bytes uint64_t */
2344 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2346 /* the used disk space 8 bytes uint64_t */
2347 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2349 /* the soft quotas 8 bytes uint64_t */
2350 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2352 /* the hard quotas 8 bytes uint64_t */
2353 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2355 /* and now the SID */
2356 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2359 qt_handle->tmp_list = tmp_list;
2361 /* overwrite the offset of the last entry */
2362 SIVAL(entry-entry_len,0,0);
2364 data_len = 4+qt_len;
2365 /* overwrite the params quota_data_len */
2366 SIVAL(params,0,data_len);
2368 break;
2370 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2372 /* unknown 4 bytes IVAL(pdata,0) */
2374 if (data_count < 8) {
2375 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2376 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2377 return;
2380 sid_len = IVAL(pdata,4);
2381 /* Ensure this is less than 1mb. */
2382 if (sid_len > (1024*1024)) {
2383 reply_nterror(req, NT_STATUS_NO_MEMORY);
2384 return;
2387 if (data_count < 8+sid_len) {
2388 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2389 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2390 return;
2393 data_len = 4+40+sid_len;
2395 if (max_data_count < data_len) {
2396 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2397 max_data_count, data_len));
2398 param_len = 4;
2399 SIVAL(params,0,data_len);
2400 data_len = 0;
2401 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2402 break;
2405 if (!sid_parse(pdata+8,sid_len,&sid)) {
2406 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2407 return;
2410 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2411 ZERO_STRUCT(qt);
2413 * we have to return zero's in all fields
2414 * instead of returning an error here
2415 * --metze
2419 /* Realloc the size of parameters and data we will return */
2420 param_len = 4;
2421 params = nttrans_realloc(ppparams, param_len);
2422 if(params == NULL) {
2423 reply_nterror(req, NT_STATUS_NO_MEMORY);
2424 return;
2427 pdata = nttrans_realloc(ppdata, data_len);
2428 if(pdata == NULL) {
2429 reply_nterror(req, NT_STATUS_NO_MEMORY);
2430 return;
2433 entry = pdata;
2435 /* set params Size of returned Quota Data 4 bytes*/
2436 SIVAL(params,0,data_len);
2438 /* nextoffset entry 4 bytes */
2439 SIVAL(entry,0,0);
2441 /* then the len of the SID 4 bytes */
2442 SIVAL(entry,4,sid_len);
2444 /* unknown data 8 bytes uint64_t */
2445 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2447 /* the used disk space 8 bytes uint64_t */
2448 SBIG_UINT(entry,16,qt.usedspace);
2450 /* the soft quotas 8 bytes uint64_t */
2451 SBIG_UINT(entry,24,qt.softlim);
2453 /* the hard quotas 8 bytes uint64_t */
2454 SBIG_UINT(entry,32,qt.hardlim);
2456 /* and now the SID */
2457 sid_linearize(entry+40, sid_len, &sid);
2459 break;
2461 default:
2462 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2463 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2464 return;
2465 break;
2468 send_nt_replies(conn, req, nt_status, params, param_len,
2469 pdata, data_len);
2472 /****************************************************************************
2473 Reply to set user quota
2474 ****************************************************************************/
2476 static void call_nt_transact_set_user_quota(connection_struct *conn,
2477 struct smb_request *req,
2478 uint16 **ppsetup,
2479 uint32 setup_count,
2480 char **ppparams,
2481 uint32 parameter_count,
2482 char **ppdata,
2483 uint32 data_count,
2484 uint32 max_data_count)
2486 char *params = *ppparams;
2487 char *pdata = *ppdata;
2488 int data_len=0,param_len=0;
2489 SMB_NTQUOTA_STRUCT qt;
2490 size_t sid_len;
2491 struct dom_sid sid;
2492 files_struct *fsp = NULL;
2494 ZERO_STRUCT(qt);
2496 /* access check */
2497 if (get_current_uid(conn) != 0) {
2498 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2499 "[%s]\n", lp_servicename(SNUM(conn)),
2500 conn->session_info->unix_info->unix_name));
2501 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2502 return;
2506 * Ensure minimum number of parameters sent.
2509 if (parameter_count < 2) {
2510 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2511 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2512 return;
2515 /* maybe we can check the quota_fnum */
2516 fsp = file_fsp(req, SVAL(params,0));
2517 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2518 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2519 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2520 return;
2523 if (data_count < 40) {
2524 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2525 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2526 return;
2529 /* offset to next quota record.
2530 * 4 bytes IVAL(pdata,0)
2531 * unused here...
2534 /* sid len */
2535 sid_len = IVAL(pdata,4);
2537 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2538 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2539 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2540 return;
2543 /* unknown 8 bytes in pdata
2544 * maybe its the change time in NTTIME
2547 /* the used space 8 bytes (uint64_t)*/
2548 qt.usedspace = BVAL(pdata,16);
2550 /* the soft quotas 8 bytes (uint64_t)*/
2551 qt.softlim = BVAL(pdata,24);
2553 /* the hard quotas 8 bytes (uint64_t)*/
2554 qt.hardlim = BVAL(pdata,32);
2556 if (!sid_parse(pdata+40,sid_len,&sid)) {
2557 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2558 return;
2561 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2563 /* 44 unknown bytes left... */
2565 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2566 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2567 return;
2570 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2571 pdata, data_len);
2573 #endif /* HAVE_SYS_QUOTAS */
2575 static void handle_nttrans(connection_struct *conn,
2576 struct trans_state *state,
2577 struct smb_request *req)
2579 if (get_Protocol() >= PROTOCOL_NT1) {
2580 req->flags2 |= 0x40; /* IS_LONG_NAME */
2581 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2585 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2587 /* Now we must call the relevant NT_TRANS function */
2588 switch(state->call) {
2589 case NT_TRANSACT_CREATE:
2591 START_PROFILE(NT_transact_create);
2592 call_nt_transact_create(
2593 conn, req,
2594 &state->setup, state->setup_count,
2595 &state->param, state->total_param,
2596 &state->data, state->total_data,
2597 state->max_data_return);
2598 END_PROFILE(NT_transact_create);
2599 break;
2602 case NT_TRANSACT_IOCTL:
2604 START_PROFILE(NT_transact_ioctl);
2605 call_nt_transact_ioctl(
2606 conn, req,
2607 &state->setup, state->setup_count,
2608 &state->param, state->total_param,
2609 &state->data, state->total_data,
2610 state->max_data_return);
2611 END_PROFILE(NT_transact_ioctl);
2612 break;
2615 case NT_TRANSACT_SET_SECURITY_DESC:
2617 START_PROFILE(NT_transact_set_security_desc);
2618 call_nt_transact_set_security_desc(
2619 conn, req,
2620 &state->setup, state->setup_count,
2621 &state->param, state->total_param,
2622 &state->data, state->total_data,
2623 state->max_data_return);
2624 END_PROFILE(NT_transact_set_security_desc);
2625 break;
2628 case NT_TRANSACT_NOTIFY_CHANGE:
2630 START_PROFILE(NT_transact_notify_change);
2631 call_nt_transact_notify_change(
2632 conn, req,
2633 &state->setup, state->setup_count,
2634 &state->param, state->total_param,
2635 &state->data, state->total_data,
2636 state->max_data_return,
2637 state->max_param_return);
2638 END_PROFILE(NT_transact_notify_change);
2639 break;
2642 case NT_TRANSACT_RENAME:
2644 START_PROFILE(NT_transact_rename);
2645 call_nt_transact_rename(
2646 conn, req,
2647 &state->setup, state->setup_count,
2648 &state->param, state->total_param,
2649 &state->data, state->total_data,
2650 state->max_data_return);
2651 END_PROFILE(NT_transact_rename);
2652 break;
2655 case NT_TRANSACT_QUERY_SECURITY_DESC:
2657 START_PROFILE(NT_transact_query_security_desc);
2658 call_nt_transact_query_security_desc(
2659 conn, req,
2660 &state->setup, state->setup_count,
2661 &state->param, state->total_param,
2662 &state->data, state->total_data,
2663 state->max_data_return);
2664 END_PROFILE(NT_transact_query_security_desc);
2665 break;
2668 #ifdef HAVE_SYS_QUOTAS
2669 case NT_TRANSACT_GET_USER_QUOTA:
2671 START_PROFILE(NT_transact_get_user_quota);
2672 call_nt_transact_get_user_quota(
2673 conn, req,
2674 &state->setup, state->setup_count,
2675 &state->param, state->total_param,
2676 &state->data, state->total_data,
2677 state->max_data_return);
2678 END_PROFILE(NT_transact_get_user_quota);
2679 break;
2682 case NT_TRANSACT_SET_USER_QUOTA:
2684 START_PROFILE(NT_transact_set_user_quota);
2685 call_nt_transact_set_user_quota(
2686 conn, req,
2687 &state->setup, state->setup_count,
2688 &state->param, state->total_param,
2689 &state->data, state->total_data,
2690 state->max_data_return);
2691 END_PROFILE(NT_transact_set_user_quota);
2692 break;
2694 #endif /* HAVE_SYS_QUOTAS */
2696 default:
2697 /* Error in request */
2698 DEBUG(0,("handle_nttrans: Unknown request %d in "
2699 "nttrans call\n", state->call));
2700 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2701 return;
2703 return;
2706 /****************************************************************************
2707 Reply to a SMBNTtrans.
2708 ****************************************************************************/
2710 void reply_nttrans(struct smb_request *req)
2712 connection_struct *conn = req->conn;
2713 uint32_t pscnt;
2714 uint32_t psoff;
2715 uint32_t dscnt;
2716 uint32_t dsoff;
2717 uint16 function_code;
2718 NTSTATUS result;
2719 struct trans_state *state;
2721 START_PROFILE(SMBnttrans);
2723 if (req->wct < 19) {
2724 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2725 END_PROFILE(SMBnttrans);
2726 return;
2729 pscnt = IVAL(req->vwv+9, 1);
2730 psoff = IVAL(req->vwv+11, 1);
2731 dscnt = IVAL(req->vwv+13, 1);
2732 dsoff = IVAL(req->vwv+15, 1);
2733 function_code = SVAL(req->vwv+18, 0);
2735 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2736 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2737 END_PROFILE(SMBnttrans);
2738 return;
2741 result = allow_new_trans(conn->pending_trans, req->mid);
2742 if (!NT_STATUS_IS_OK(result)) {
2743 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2744 reply_nterror(req, result);
2745 END_PROFILE(SMBnttrans);
2746 return;
2749 if ((state = talloc(conn, struct trans_state)) == NULL) {
2750 reply_nterror(req, NT_STATUS_NO_MEMORY);
2751 END_PROFILE(SMBnttrans);
2752 return;
2755 state->cmd = SMBnttrans;
2757 state->mid = req->mid;
2758 state->vuid = req->vuid;
2759 state->total_data = IVAL(req->vwv+3, 1);
2760 state->data = NULL;
2761 state->total_param = IVAL(req->vwv+1, 1);
2762 state->param = NULL;
2763 state->max_data_return = IVAL(req->vwv+7, 1);
2764 state->max_param_return = IVAL(req->vwv+5, 1);
2766 /* setup count is in *words* */
2767 state->setup_count = 2*CVAL(req->vwv+17, 1);
2768 state->setup = NULL;
2769 state->call = function_code;
2771 DEBUG(10, ("num_setup=%u, "
2772 "param_total=%u, this_param=%u, max_param=%u, "
2773 "data_total=%u, this_data=%u, max_data=%u, "
2774 "param_offset=%u, data_offset=%u\n",
2775 (unsigned)state->setup_count,
2776 (unsigned)state->total_param, (unsigned)pscnt,
2777 (unsigned)state->max_param_return,
2778 (unsigned)state->total_data, (unsigned)dscnt,
2779 (unsigned)state->max_data_return,
2780 (unsigned)psoff, (unsigned)dsoff));
2783 * All nttrans messages we handle have smb_wct == 19 +
2784 * state->setup_count. Ensure this is so as a sanity check.
2787 if(req->wct != 19 + (state->setup_count/2)) {
2788 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2789 req->wct, 19 + (state->setup_count/2)));
2790 goto bad_param;
2793 /* Don't allow more than 128mb for each value. */
2794 if ((state->total_data > (1024*1024*128)) ||
2795 (state->total_param > (1024*1024*128))) {
2796 reply_nterror(req, NT_STATUS_NO_MEMORY);
2797 END_PROFILE(SMBnttrans);
2798 return;
2801 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2802 goto bad_param;
2804 if (state->total_data) {
2806 if (trans_oob(state->total_data, 0, dscnt)
2807 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2808 goto bad_param;
2811 /* Can't use talloc here, the core routines do realloc on the
2812 * params and data. */
2813 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2814 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2815 "bytes !\n", (unsigned int)state->total_data));
2816 TALLOC_FREE(state);
2817 reply_nterror(req, NT_STATUS_NO_MEMORY);
2818 END_PROFILE(SMBnttrans);
2819 return;
2822 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2825 if (state->total_param) {
2827 if (trans_oob(state->total_param, 0, pscnt)
2828 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2829 goto bad_param;
2832 /* Can't use talloc here, the core routines do realloc on the
2833 * params and data. */
2834 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2835 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2836 "bytes !\n", (unsigned int)state->total_param));
2837 SAFE_FREE(state->data);
2838 TALLOC_FREE(state);
2839 reply_nterror(req, NT_STATUS_NO_MEMORY);
2840 END_PROFILE(SMBnttrans);
2841 return;
2844 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2847 state->received_data = dscnt;
2848 state->received_param = pscnt;
2850 if(state->setup_count > 0) {
2851 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2852 state->setup_count));
2855 * No overflow possible here, state->setup_count is an
2856 * unsigned int, being filled by a single byte from
2857 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2858 * below is not necessary, it's here to clarify things. The
2859 * validity of req->vwv and req->wct has been checked in
2860 * init_smb_request already.
2862 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2863 goto bad_param;
2866 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2867 if (state->setup == NULL) {
2868 DEBUG(0,("reply_nttrans : Out of memory\n"));
2869 SAFE_FREE(state->data);
2870 SAFE_FREE(state->param);
2871 TALLOC_FREE(state);
2872 reply_nterror(req, NT_STATUS_NO_MEMORY);
2873 END_PROFILE(SMBnttrans);
2874 return;
2877 memcpy(state->setup, req->vwv+19, state->setup_count);
2878 dump_data(10, (uint8 *)state->setup, state->setup_count);
2881 if ((state->received_data == state->total_data) &&
2882 (state->received_param == state->total_param)) {
2883 handle_nttrans(conn, state, req);
2884 SAFE_FREE(state->param);
2885 SAFE_FREE(state->data);
2886 TALLOC_FREE(state);
2887 END_PROFILE(SMBnttrans);
2888 return;
2891 DLIST_ADD(conn->pending_trans, state);
2893 /* We need to send an interim response then receive the rest
2894 of the parameter/data bytes */
2895 reply_outbuf(req, 0, 0);
2896 show_msg((char *)req->outbuf);
2897 END_PROFILE(SMBnttrans);
2898 return;
2900 bad_param:
2902 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2903 SAFE_FREE(state->data);
2904 SAFE_FREE(state->param);
2905 TALLOC_FREE(state);
2906 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2907 END_PROFILE(SMBnttrans);
2908 return;
2911 /****************************************************************************
2912 Reply to a SMBnttranss
2913 ****************************************************************************/
2915 void reply_nttranss(struct smb_request *req)
2917 connection_struct *conn = req->conn;
2918 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2919 struct trans_state *state;
2921 START_PROFILE(SMBnttranss);
2923 show_msg((const char *)req->inbuf);
2925 if (req->wct < 18) {
2926 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2927 END_PROFILE(SMBnttranss);
2928 return;
2931 for (state = conn->pending_trans; state != NULL;
2932 state = state->next) {
2933 if (state->mid == req->mid) {
2934 break;
2938 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2939 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2940 END_PROFILE(SMBnttranss);
2941 return;
2944 /* Revise state->total_param and state->total_data in case they have
2945 changed downwards */
2946 if (IVAL(req->vwv+1, 1) < state->total_param) {
2947 state->total_param = IVAL(req->vwv+1, 1);
2949 if (IVAL(req->vwv+3, 1) < state->total_data) {
2950 state->total_data = IVAL(req->vwv+3, 1);
2953 pcnt = IVAL(req->vwv+5, 1);
2954 poff = IVAL(req->vwv+7, 1);
2955 pdisp = IVAL(req->vwv+9, 1);
2957 dcnt = IVAL(req->vwv+11, 1);
2958 doff = IVAL(req->vwv+13, 1);
2959 ddisp = IVAL(req->vwv+15, 1);
2961 state->received_param += pcnt;
2962 state->received_data += dcnt;
2964 if ((state->received_data > state->total_data) ||
2965 (state->received_param > state->total_param))
2966 goto bad_param;
2968 if (pcnt) {
2969 if (trans_oob(state->total_param, pdisp, pcnt)
2970 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2971 goto bad_param;
2973 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2976 if (dcnt) {
2977 if (trans_oob(state->total_data, ddisp, dcnt)
2978 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2979 goto bad_param;
2981 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2984 if ((state->received_param < state->total_param) ||
2985 (state->received_data < state->total_data)) {
2986 END_PROFILE(SMBnttranss);
2987 return;
2990 handle_nttrans(conn, state, req);
2992 DLIST_REMOVE(conn->pending_trans, state);
2993 SAFE_FREE(state->data);
2994 SAFE_FREE(state->param);
2995 TALLOC_FREE(state);
2996 END_PROFILE(SMBnttranss);
2997 return;
2999 bad_param:
3001 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3002 DLIST_REMOVE(conn->pending_trans, state);
3003 SAFE_FREE(state->data);
3004 SAFE_FREE(state->param);
3005 TALLOC_FREE(state);
3006 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3007 END_PROFILE(SMBnttranss);
3008 return;