Use "struct files_struct" for pipes instead of smb_np_struct
[Samba/gebeck_regimport.git] / source3 / smbd / nttrans.c
blobc5e4dc3dfbf8350776d0cdc2f8f578cef2a95e4d
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"
23 extern int max_send;
24 extern enum protocol_types Protocol;
25 extern const struct generic_mapping file_generic_mapping;
27 static char *nttrans_realloc(char **ptr, size_t size)
29 if (ptr==NULL) {
30 smb_panic("nttrans_realloc() called with NULL ptr");
33 *ptr = (char *)SMB_REALLOC(*ptr, size);
34 if(*ptr == NULL) {
35 return NULL;
37 memset(*ptr,'\0',size);
38 return *ptr;
41 /****************************************************************************
42 Send the required number of replies back.
43 We assume all fields other than the data fields are
44 set correctly for the type of call.
45 HACK ! Always assumes smb_setup field is zero.
46 ****************************************************************************/
48 void send_nt_replies(connection_struct *conn,
49 struct smb_request *req, NTSTATUS nt_error,
50 char *params, int paramsize,
51 char *pdata, int datasize)
53 int data_to_send = datasize;
54 int params_to_send = paramsize;
55 int useable_space;
56 char *pp = params;
57 char *pd = pdata;
58 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
59 int alignment_offset = 3;
60 int data_alignment_offset = 0;
63 * If there genuinely are no parameters or data to send just send
64 * the empty packet.
67 if(params_to_send == 0 && data_to_send == 0) {
68 reply_outbuf(req, 18, 0);
69 show_msg((char *)req->outbuf);
70 return;
74 * When sending params and data ensure that both are nicely aligned.
75 * Only do this alignment when there is also data to send - else
76 * can cause NT redirector problems.
79 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
80 data_alignment_offset = 4 - (params_to_send % 4);
84 * Space is bufsize minus Netbios over TCP header minus SMB header.
85 * The alignment_offset is to align the param bytes on a four byte
86 * boundary (2 bytes for data len, one byte pad).
87 * NT needs this to work correctly.
90 useable_space = max_send - (smb_size
91 + 2 * 18 /* wct */
92 + alignment_offset
93 + data_alignment_offset);
95 if (useable_space < 0) {
96 DEBUG(0, ("send_nt_replies failed sanity useable_space "
97 "= %d!!!", useable_space));
98 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
101 while (params_to_send || data_to_send) {
104 * Calculate whether we will totally or partially fill this packet.
107 total_sent_thistime = params_to_send + data_to_send;
110 * We can never send more than useable_space.
113 total_sent_thistime = MIN(total_sent_thistime, useable_space);
115 reply_outbuf(req, 18,
116 total_sent_thistime + alignment_offset
117 + data_alignment_offset);
120 * Set total params and data to be sent.
123 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
124 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
127 * Calculate how many parameters and data we can fit into
128 * this packet. Parameters get precedence.
131 params_sent_thistime = MIN(params_to_send,useable_space);
132 data_sent_thistime = useable_space - params_sent_thistime;
133 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
135 SIVAL(req->outbuf, smb_ntr_ParameterCount,
136 params_sent_thistime);
138 if(params_sent_thistime == 0) {
139 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
140 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
141 } else {
143 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
144 * parameter bytes, however the first 4 bytes of outbuf are
145 * the Netbios over TCP header. Thus use smb_base() to subtract
146 * them from the calculation.
149 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
150 ((smb_buf(req->outbuf)+alignment_offset)
151 - smb_base(req->outbuf)));
153 * Absolute displacement of param bytes sent in this packet.
156 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
157 pp - params);
161 * Deal with the data portion.
164 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
166 if(data_sent_thistime == 0) {
167 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
168 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
169 } else {
171 * The offset of the data bytes is the offset of the
172 * parameter bytes plus the number of parameters being sent this time.
175 SIVAL(req->outbuf, smb_ntr_DataOffset,
176 ((smb_buf(req->outbuf)+alignment_offset) -
177 smb_base(req->outbuf))
178 + params_sent_thistime + data_alignment_offset);
179 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
183 * Copy the param bytes into the packet.
186 if(params_sent_thistime) {
187 if (alignment_offset != 0) {
188 memset(smb_buf(req->outbuf), 0,
189 alignment_offset);
191 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
192 params_sent_thistime);
196 * Copy in the data bytes
199 if(data_sent_thistime) {
200 if (data_alignment_offset != 0) {
201 memset((smb_buf(req->outbuf)+alignment_offset+
202 params_sent_thistime), 0,
203 data_alignment_offset);
205 memcpy(smb_buf(req->outbuf)+alignment_offset
206 +params_sent_thistime+data_alignment_offset,
207 pd,data_sent_thistime);
210 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
211 params_sent_thistime, data_sent_thistime, useable_space));
212 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
213 params_to_send, data_to_send, paramsize, datasize));
215 if (NT_STATUS_V(nt_error)) {
216 error_packet_set((char *)req->outbuf,
217 0, 0, nt_error,
218 __LINE__,__FILE__);
221 /* Send the packet */
222 show_msg((char *)req->outbuf);
223 if (!srv_send_smb(smbd_server_fd(),
224 (char *)req->outbuf,
225 IS_CONN_ENCRYPTED(conn))) {
226 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
229 TALLOC_FREE(req->outbuf);
231 pp += params_sent_thistime;
232 pd += data_sent_thistime;
234 params_to_send -= params_sent_thistime;
235 data_to_send -= data_sent_thistime;
238 * Sanity check
241 if(params_to_send < 0 || data_to_send < 0) {
242 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
243 params_to_send, data_to_send));
244 exit_server_cleanly("send_nt_replies: internal error");
249 /****************************************************************************
250 Is it an NTFS stream name ?
251 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
252 $DATA can be used as both a stream name and a stream type. A missing stream
253 name or type implies $DATA.
254 ****************************************************************************/
256 bool is_ntfs_stream_name(const char *fname)
258 if (lp_posix_pathnames()) {
259 return False;
261 return (strchr_m(fname, ':') != NULL) ? True : False;
264 /****************************************************************************
265 Reply to an NT create and X call on a pipe
266 ****************************************************************************/
268 static void nt_open_pipe(char *fname, connection_struct *conn,
269 struct smb_request *req, int *ppnum)
271 files_struct *fsp;
272 NTSTATUS status;
274 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
276 /* See if it is one we want to handle. */
278 if (!is_known_pipename(fname)) {
279 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
280 ERRDOS, ERRbadpipe);
281 return;
284 /* Strip \\ off the name. */
285 fname++;
287 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
289 status = np_open(req, conn, fname, &fsp);
290 if (!NT_STATUS_IS_OK(status)) {
291 reply_nterror(req, status);
292 return;
295 *ppnum = fsp->fnum;
296 return;
299 /****************************************************************************
300 Reply to an NT create and X call for pipes.
301 ****************************************************************************/
303 static void do_ntcreate_pipe_open(connection_struct *conn,
304 struct smb_request *req)
306 char *fname = NULL;
307 int pnum = -1;
308 char *p = NULL;
309 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
310 TALLOC_CTX *ctx = talloc_tos();
312 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
313 smb_buf(req->inbuf), STR_TERMINATE);
315 if (!fname) {
316 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
317 ERRDOS, ERRbadpipe);
318 return;
320 nt_open_pipe(fname, conn, req, &pnum);
322 if (req->outbuf) {
323 /* error reply */
324 return;
328 * Deal with pipe return.
331 if (flags & EXTENDED_RESPONSE_REQUIRED) {
332 /* This is very strange. We
333 * return 50 words, but only set
334 * the wcnt to 42 ? It's definately
335 * what happens on the wire....
337 reply_outbuf(req, 50, 0);
338 SCVAL(req->outbuf,smb_wct,42);
339 } else {
340 reply_outbuf(req, 34, 0);
343 p = (char *)req->outbuf + smb_vwv2;
344 p++;
345 SSVAL(p,0,pnum);
346 p += 2;
347 SIVAL(p,0,FILE_WAS_OPENED);
348 p += 4;
349 p += 32;
350 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
351 p += 20;
352 /* File type. */
353 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
354 /* Device state. */
355 SSVAL(p,2, 0x5FF); /* ? */
356 p += 4;
358 if (flags & EXTENDED_RESPONSE_REQUIRED) {
359 p += 25;
360 SIVAL(p,0,FILE_GENERIC_ALL);
362 * For pipes W2K3 seems to return
363 * 0x12019B next.
364 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
366 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
369 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
371 chain_reply(req);
374 /****************************************************************************
375 Reply to an NT create and X call.
376 ****************************************************************************/
378 void reply_ntcreate_and_X(struct smb_request *req)
380 connection_struct *conn = req->conn;
381 char *fname = NULL;
382 uint32 flags;
383 uint32 access_mask;
384 uint32 file_attributes;
385 uint32 share_access;
386 uint32 create_disposition;
387 uint32 create_options;
388 uint16 root_dir_fid;
389 SMB_BIG_UINT allocation_size;
390 /* Breakout the oplock request bits so we can set the
391 reply bits separately. */
392 uint32 fattr=0;
393 SMB_OFF_T file_len = 0;
394 SMB_STRUCT_STAT sbuf;
395 int info = 0;
396 files_struct *fsp = NULL;
397 char *p = NULL;
398 struct timespec c_timespec;
399 struct timespec a_timespec;
400 struct timespec m_timespec;
401 NTSTATUS status;
402 int oplock_request;
403 uint8_t oplock_granted = NO_OPLOCK_RETURN;
404 TALLOC_CTX *ctx = talloc_tos();
406 START_PROFILE(SMBntcreateX);
408 if (req->wct < 24) {
409 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
410 return;
413 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
414 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
415 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
416 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
417 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
418 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
419 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
421 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
422 smb_ntcreate_AllocationSize);
423 #ifdef LARGE_SMB_OFF_T
424 allocation_size |= (((SMB_BIG_UINT)IVAL(
425 req->inbuf,
426 smb_ntcreate_AllocationSize + 4)) << 32);
427 #endif
429 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
430 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
432 if (!NT_STATUS_IS_OK(status)) {
433 reply_nterror(req, status);
434 END_PROFILE(SMBntcreateX);
435 return;
438 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
439 "file_attributes = 0x%x, share_access = 0x%x, "
440 "create_disposition = 0x%x create_options = 0x%x "
441 "root_dir_fid = 0x%x, fname = %s\n",
442 (unsigned int)flags,
443 (unsigned int)access_mask,
444 (unsigned int)file_attributes,
445 (unsigned int)share_access,
446 (unsigned int)create_disposition,
447 (unsigned int)create_options,
448 (unsigned int)root_dir_fid,
449 fname));
452 * we need to remove ignored bits when they come directly from the client
453 * because we reuse some of them for internal stuff
455 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
458 * If it's an IPC, use the pipe handler.
461 if (IS_IPC(conn)) {
462 if (lp_nt_pipe_support()) {
463 do_ntcreate_pipe_open(conn, req);
464 END_PROFILE(SMBntcreateX);
465 return;
467 reply_doserror(req, ERRDOS, ERRnoaccess);
468 END_PROFILE(SMBntcreateX);
469 return;
472 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
473 if (oplock_request) {
474 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
475 ? BATCH_OPLOCK : 0;
478 status = create_file(conn, req, root_dir_fid, fname,
479 access_mask, share_access, create_disposition,
480 create_options, file_attributes, oplock_request,
481 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
483 if (!NT_STATUS_IS_OK(status)) {
484 if (open_was_deferred(req->mid)) {
485 /* We have re-scheduled this call, no error. */
486 END_PROFILE(SMBntcreateX);
487 return;
489 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
490 reply_botherror(req, status, ERRDOS, ERRfilexists);
492 else {
493 reply_nterror(req, status);
495 END_PROFILE(SMBntcreateX);
496 return;
500 * If the caller set the extended oplock request bit
501 * and we granted one (by whatever means) - set the
502 * correct bit for extended oplock reply.
505 if (oplock_request &&
506 (lp_fake_oplocks(SNUM(conn))
507 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
510 * Exclusive oplock granted
513 if (flags & REQUEST_BATCH_OPLOCK) {
514 oplock_granted = BATCH_OPLOCK_RETURN;
515 } else {
516 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
518 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
519 oplock_granted = LEVEL_II_OPLOCK_RETURN;
520 } else {
521 oplock_granted = NO_OPLOCK_RETURN;
524 file_len = sbuf.st_size;
525 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
526 if (fattr == 0) {
527 fattr = FILE_ATTRIBUTE_NORMAL;
530 if (flags & EXTENDED_RESPONSE_REQUIRED) {
531 /* This is very strange. We
532 * return 50 words, but only set
533 * the wcnt to 42 ? It's definately
534 * what happens on the wire....
536 reply_outbuf(req, 50, 0);
537 SCVAL(req->outbuf,smb_wct,42);
538 } else {
539 reply_outbuf(req, 34, 0);
542 p = (char *)req->outbuf + smb_vwv2;
544 SCVAL(p, 0, oplock_granted);
546 p++;
547 SSVAL(p,0,fsp->fnum);
548 p += 2;
549 if ((create_disposition == FILE_SUPERSEDE)
550 && (info == FILE_WAS_OVERWRITTEN)) {
551 SIVAL(p,0,FILE_WAS_SUPERSEDED);
552 } else {
553 SIVAL(p,0,info);
555 p += 4;
557 /* Create time. */
558 c_timespec = get_create_timespec(
559 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
560 a_timespec = get_atimespec(&sbuf);
561 m_timespec = get_mtimespec(&sbuf);
563 if (lp_dos_filetime_resolution(SNUM(conn))) {
564 dos_filetime_timespec(&c_timespec);
565 dos_filetime_timespec(&a_timespec);
566 dos_filetime_timespec(&m_timespec);
569 put_long_date_timespec(p, c_timespec); /* create time. */
570 p += 8;
571 put_long_date_timespec(p, a_timespec); /* access time */
572 p += 8;
573 put_long_date_timespec(p, m_timespec); /* write time */
574 p += 8;
575 put_long_date_timespec(p, m_timespec); /* change time */
576 p += 8;
577 SIVAL(p,0,fattr); /* File Attributes. */
578 p += 4;
579 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
580 p += 8;
581 SOFF_T(p,0,file_len);
582 p += 8;
583 if (flags & EXTENDED_RESPONSE_REQUIRED) {
584 SSVAL(p,2,0x7);
586 p += 4;
587 SCVAL(p,0,fsp->is_directory ? 1 : 0);
589 if (flags & EXTENDED_RESPONSE_REQUIRED) {
590 uint32 perms = 0;
591 p += 25;
592 if (fsp->is_directory
593 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
594 perms = FILE_GENERIC_ALL;
595 } else {
596 perms = FILE_GENERIC_READ|FILE_EXECUTE;
598 SIVAL(p,0,perms);
601 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
602 fsp->fnum, fsp->fsp_name));
604 chain_reply(req);
605 END_PROFILE(SMBntcreateX);
606 return;
609 /****************************************************************************
610 Reply to a NT_TRANSACT_CREATE call to open a pipe.
611 ****************************************************************************/
613 static void do_nt_transact_create_pipe(connection_struct *conn,
614 struct smb_request *req,
615 uint16 **ppsetup, uint32 setup_count,
616 char **ppparams, uint32 parameter_count,
617 char **ppdata, uint32 data_count)
619 char *fname = NULL;
620 char *params = *ppparams;
621 int pnum = -1;
622 char *p = NULL;
623 NTSTATUS status;
624 size_t param_len;
625 uint32 flags;
626 TALLOC_CTX *ctx = talloc_tos();
629 * Ensure minimum number of parameters sent.
632 if(parameter_count < 54) {
633 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
634 reply_doserror(req, ERRDOS, ERRnoaccess);
635 return;
638 flags = IVAL(params,0);
640 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
641 parameter_count-53, STR_TERMINATE,
642 &status);
643 if (!NT_STATUS_IS_OK(status)) {
644 reply_nterror(req, status);
645 return;
648 nt_open_pipe(fname, conn, req, &pnum);
650 if (req->outbuf) {
651 /* Error return */
652 return;
655 /* Realloc the size of parameters and data we will return */
656 if (flags & EXTENDED_RESPONSE_REQUIRED) {
657 /* Extended response is 32 more byyes. */
658 param_len = 101;
659 } else {
660 param_len = 69;
662 params = nttrans_realloc(ppparams, param_len);
663 if(params == NULL) {
664 reply_doserror(req, ERRDOS, ERRnomem);
665 return;
668 p = params;
669 SCVAL(p,0,NO_OPLOCK_RETURN);
671 p += 2;
672 SSVAL(p,0,pnum);
673 p += 2;
674 SIVAL(p,0,FILE_WAS_OPENED);
675 p += 8;
677 p += 32;
678 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
679 p += 20;
680 /* File type. */
681 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
682 /* Device state. */
683 SSVAL(p,2, 0x5FF); /* ? */
684 p += 4;
686 if (flags & EXTENDED_RESPONSE_REQUIRED) {
687 p += 25;
688 SIVAL(p,0,FILE_GENERIC_ALL);
690 * For pipes W2K3 seems to return
691 * 0x12019B next.
692 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
694 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
697 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
699 /* Send the required number of replies */
700 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
702 return;
705 /****************************************************************************
706 Internal fn to set security descriptors.
707 ****************************************************************************/
709 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
710 uint32 security_info_sent)
712 SEC_DESC *psd = NULL;
713 NTSTATUS status;
715 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
716 return NT_STATUS_OK;
719 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
721 if (!NT_STATUS_IS_OK(status)) {
722 return status;
725 if (psd->owner_sid==0) {
726 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
728 if (psd->group_sid==0) {
729 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
731 if (psd->sacl==0) {
732 security_info_sent &= ~SACL_SECURITY_INFORMATION;
734 if (psd->dacl==0) {
735 security_info_sent &= ~DACL_SECURITY_INFORMATION;
738 /* Convert all the generic bits. */
739 security_acl_map_generic(psd->dacl, &file_generic_mapping);
740 security_acl_map_generic(psd->sacl, &file_generic_mapping);
742 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
744 TALLOC_FREE(psd);
746 return status;
749 /****************************************************************************
750 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
751 ****************************************************************************/
753 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
755 struct ea_list *ea_list_head = NULL;
756 size_t offset = 0;
758 if (data_size < 4) {
759 return NULL;
762 while (offset + 4 <= data_size) {
763 size_t next_offset = IVAL(pdata,offset);
764 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
766 if (!eal) {
767 return NULL;
770 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
771 if (next_offset == 0) {
772 break;
774 offset += next_offset;
777 return ea_list_head;
780 /****************************************************************************
781 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
782 ****************************************************************************/
784 static void call_nt_transact_create(connection_struct *conn,
785 struct smb_request *req,
786 uint16 **ppsetup, uint32 setup_count,
787 char **ppparams, uint32 parameter_count,
788 char **ppdata, uint32 data_count,
789 uint32 max_data_count)
791 char *fname = NULL;
792 char *params = *ppparams;
793 char *data = *ppdata;
794 /* Breakout the oplock request bits so we can set the reply bits separately. */
795 uint32 fattr=0;
796 SMB_OFF_T file_len = 0;
797 SMB_STRUCT_STAT sbuf;
798 int info = 0;
799 files_struct *fsp = NULL;
800 char *p = NULL;
801 uint32 flags;
802 uint32 access_mask;
803 uint32 file_attributes;
804 uint32 share_access;
805 uint32 create_disposition;
806 uint32 create_options;
807 uint32 sd_len;
808 struct security_descriptor *sd = NULL;
809 uint32 ea_len;
810 uint16 root_dir_fid;
811 struct timespec c_timespec;
812 struct timespec a_timespec;
813 struct timespec m_timespec;
814 struct ea_list *ea_list = NULL;
815 NTSTATUS status;
816 size_t param_len;
817 SMB_BIG_UINT allocation_size;
818 int oplock_request;
819 uint8_t oplock_granted;
820 TALLOC_CTX *ctx = talloc_tos();
822 DEBUG(5,("call_nt_transact_create\n"));
825 * If it's an IPC, use the pipe handler.
828 if (IS_IPC(conn)) {
829 if (lp_nt_pipe_support()) {
830 do_nt_transact_create_pipe(
831 conn, req,
832 ppsetup, setup_count,
833 ppparams, parameter_count,
834 ppdata, data_count);
835 return;
837 reply_doserror(req, ERRDOS, ERRnoaccess);
838 return;
842 * Ensure minimum number of parameters sent.
845 if(parameter_count < 54) {
846 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
847 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
848 return;
851 flags = IVAL(params,0);
852 access_mask = IVAL(params,8);
853 file_attributes = IVAL(params,20);
854 share_access = IVAL(params,24);
855 create_disposition = IVAL(params,28);
856 create_options = IVAL(params,32);
857 sd_len = IVAL(params,36);
858 ea_len = IVAL(params,40);
859 root_dir_fid = (uint16)IVAL(params,4);
860 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
861 #ifdef LARGE_SMB_OFF_T
862 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
863 #endif
866 * we need to remove ignored bits when they come directly from the client
867 * because we reuse some of them for internal stuff
869 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
871 /* Ensure the data_len is correct for the sd and ea values given. */
872 if ((ea_len + sd_len > data_count)
873 || (ea_len > data_count) || (sd_len > data_count)
874 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
875 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
876 "%u, data_count = %u\n", (unsigned int)ea_len,
877 (unsigned int)sd_len, (unsigned int)data_count));
878 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
879 return;
882 if (sd_len) {
883 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
884 sd_len));
886 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
887 &sd);
888 if (!NT_STATUS_IS_OK(status)) {
889 DEBUG(10, ("call_nt_transact_create: "
890 "unmarshall_sec_desc failed: %s\n",
891 nt_errstr(status)));
892 reply_nterror(req, status);
893 return;
897 if (ea_len) {
898 if (!lp_ea_support(SNUM(conn))) {
899 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
900 "EA's not supported.\n",
901 (unsigned int)ea_len));
902 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
903 return;
906 if (ea_len < 10) {
907 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
908 "too small (should be more than 10)\n",
909 (unsigned int)ea_len ));
910 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
911 return;
914 /* We have already checked that ea_len <= data_count here. */
915 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
916 ea_len);
917 if (ea_list == NULL) {
918 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
919 return;
923 srvstr_get_path(ctx, params, req->flags2, &fname,
924 params+53, parameter_count-53,
925 STR_TERMINATE, &status);
926 if (!NT_STATUS_IS_OK(status)) {
927 reply_nterror(req, status);
928 return;
931 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
932 if (oplock_request) {
933 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
934 ? BATCH_OPLOCK : 0;
937 status = create_file(conn, req, root_dir_fid, fname,
938 access_mask, share_access, create_disposition,
939 create_options, file_attributes, oplock_request,
940 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
942 if(!NT_STATUS_IS_OK(status)) {
943 if (open_was_deferred(req->mid)) {
944 /* We have re-scheduled this call, no error. */
945 return;
947 reply_openerror(req, status);
948 return;
952 * If the caller set the extended oplock request bit
953 * and we granted one (by whatever means) - set the
954 * correct bit for extended oplock reply.
957 if (oplock_request &&
958 (lp_fake_oplocks(SNUM(conn))
959 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
962 * Exclusive oplock granted
965 if (flags & REQUEST_BATCH_OPLOCK) {
966 oplock_granted = BATCH_OPLOCK_RETURN;
967 } else {
968 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
970 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
971 oplock_granted = LEVEL_II_OPLOCK_RETURN;
972 } else {
973 oplock_granted = NO_OPLOCK_RETURN;
976 file_len = sbuf.st_size;
977 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
978 if (fattr == 0) {
979 fattr = FILE_ATTRIBUTE_NORMAL;
982 /* Realloc the size of parameters and data we will return */
983 if (flags & EXTENDED_RESPONSE_REQUIRED) {
984 /* Extended response is 32 more byyes. */
985 param_len = 101;
986 } else {
987 param_len = 69;
989 params = nttrans_realloc(ppparams, param_len);
990 if(params == NULL) {
991 reply_doserror(req, ERRDOS, ERRnomem);
992 return;
995 p = params;
996 SCVAL(p, 0, oplock_granted);
998 p += 2;
999 SSVAL(p,0,fsp->fnum);
1000 p += 2;
1001 if ((create_disposition == FILE_SUPERSEDE)
1002 && (info == FILE_WAS_OVERWRITTEN)) {
1003 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1004 } else {
1005 SIVAL(p,0,info);
1007 p += 8;
1009 /* Create time. */
1010 c_timespec = get_create_timespec(
1011 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1012 a_timespec = get_atimespec(&sbuf);
1013 m_timespec = get_mtimespec(&sbuf);
1015 if (lp_dos_filetime_resolution(SNUM(conn))) {
1016 dos_filetime_timespec(&c_timespec);
1017 dos_filetime_timespec(&a_timespec);
1018 dos_filetime_timespec(&m_timespec);
1021 put_long_date_timespec(p, c_timespec); /* create time. */
1022 p += 8;
1023 put_long_date_timespec(p, a_timespec); /* access time */
1024 p += 8;
1025 put_long_date_timespec(p, m_timespec); /* write time */
1026 p += 8;
1027 put_long_date_timespec(p, m_timespec); /* change time */
1028 p += 8;
1029 SIVAL(p,0,fattr); /* File Attributes. */
1030 p += 4;
1031 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1032 p += 8;
1033 SOFF_T(p,0,file_len);
1034 p += 8;
1035 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1036 SSVAL(p,2,0x7);
1038 p += 4;
1039 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1041 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1042 uint32 perms = 0;
1043 p += 25;
1044 if (fsp->is_directory
1045 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1046 perms = FILE_GENERIC_ALL;
1047 } else {
1048 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1050 SIVAL(p,0,perms);
1053 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1055 /* Send the required number of replies */
1056 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1058 return;
1061 /****************************************************************************
1062 Reply to a NT CANCEL request.
1063 conn POINTER CAN BE NULL HERE !
1064 ****************************************************************************/
1066 void reply_ntcancel(struct smb_request *req)
1069 * Go through and cancel any pending change notifies.
1072 START_PROFILE(SMBntcancel);
1073 remove_pending_change_notify_requests_by_mid(req->mid);
1074 remove_pending_lock_requests_by_mid(req->mid);
1075 srv_cancel_sign_response(req->mid);
1077 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1079 END_PROFILE(SMBntcancel);
1080 return;
1083 /****************************************************************************
1084 Copy a file.
1085 ****************************************************************************/
1087 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1088 connection_struct *conn,
1089 struct smb_request *req,
1090 const char *oldname_in,
1091 const char *newname_in,
1092 uint32 attrs)
1094 SMB_STRUCT_STAT sbuf1, sbuf2;
1095 char *oldname = NULL;
1096 char *newname = NULL;
1097 char *last_component_oldname = NULL;
1098 char *last_component_newname = NULL;
1099 files_struct *fsp1,*fsp2;
1100 uint32 fattr;
1101 int info;
1102 SMB_OFF_T ret=-1;
1103 NTSTATUS status = NT_STATUS_OK;
1105 ZERO_STRUCT(sbuf1);
1106 ZERO_STRUCT(sbuf2);
1108 if (!CAN_WRITE(conn)) {
1109 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1112 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1113 &last_component_oldname, &sbuf1);
1114 if (!NT_STATUS_IS_OK(status)) {
1115 return status;
1118 status = check_name(conn, oldname);
1119 if (!NT_STATUS_IS_OK(status)) {
1120 return status;
1123 /* Source must already exist. */
1124 if (!VALID_STAT(sbuf1)) {
1125 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1127 /* Ensure attributes match. */
1128 fattr = dos_mode(conn,oldname,&sbuf1);
1129 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1130 return NT_STATUS_NO_SUCH_FILE;
1133 status = unix_convert(ctx, conn, newname_in, False, &newname,
1134 &last_component_newname, &sbuf2);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 return status;
1139 status = check_name(conn, newname);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 return status;
1144 /* Disallow if newname already exists. */
1145 if (VALID_STAT(sbuf2)) {
1146 return NT_STATUS_OBJECT_NAME_COLLISION;
1149 /* No links from a directory. */
1150 if (S_ISDIR(sbuf1.st_mode)) {
1151 return NT_STATUS_FILE_IS_A_DIRECTORY;
1154 /* Ensure this is within the share. */
1155 status = check_reduced_name(conn, oldname);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 return status;
1160 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1161 oldname, newname));
1163 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1164 FILE_READ_DATA, /* Read-only. */
1165 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1166 FILE_OPEN,
1167 0, /* No create options. */
1168 FILE_ATTRIBUTE_NORMAL,
1169 NO_OPLOCK,
1170 &info, &fsp1);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 return status;
1176 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1177 FILE_WRITE_DATA, /* Read-only. */
1178 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1179 FILE_CREATE,
1180 0, /* No create options. */
1181 fattr,
1182 NO_OPLOCK,
1183 &info, &fsp2);
1185 if (!NT_STATUS_IS_OK(status)) {
1186 close_file(NULL, fsp1, ERROR_CLOSE);
1187 return status;
1190 if (sbuf1.st_size) {
1191 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1195 * As we are opening fsp1 read-only we only expect
1196 * an error on close on fsp2 if we are out of space.
1197 * Thus we don't look at the error return from the
1198 * close of fsp1.
1200 close_file(NULL, fsp1, NORMAL_CLOSE);
1202 /* Ensure the modtime is set correctly on the destination file. */
1203 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1205 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1207 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1208 creates the file. This isn't the correct thing to do in the copy
1209 case. JRA */
1210 file_set_dosmode(conn, newname, fattr, &sbuf2,
1211 parent_dirname(newname),false);
1213 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1214 return NT_STATUS_DISK_FULL;
1217 if (!NT_STATUS_IS_OK(status)) {
1218 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1219 nt_errstr(status), oldname, newname));
1221 return status;
1224 /****************************************************************************
1225 Reply to a NT rename request.
1226 ****************************************************************************/
1228 void reply_ntrename(struct smb_request *req)
1230 connection_struct *conn = req->conn;
1231 char *oldname = NULL;
1232 char *newname = NULL;
1233 char *p;
1234 NTSTATUS status;
1235 bool src_has_wcard = False;
1236 bool dest_has_wcard = False;
1237 uint32 attrs;
1238 uint16 rename_type;
1239 TALLOC_CTX *ctx = talloc_tos();
1241 START_PROFILE(SMBntrename);
1243 if (req->wct < 4) {
1244 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1245 END_PROFILE(SMBntrename);
1246 return;
1249 attrs = SVAL(req->inbuf,smb_vwv0);
1250 rename_type = SVAL(req->inbuf,smb_vwv1);
1252 p = smb_buf(req->inbuf) + 1;
1253 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1254 0, STR_TERMINATE, &status,
1255 &src_has_wcard);
1256 if (!NT_STATUS_IS_OK(status)) {
1257 reply_nterror(req, status);
1258 END_PROFILE(SMBntrename);
1259 return;
1262 if( is_ntfs_stream_name(oldname)) {
1263 /* Can't rename a stream. */
1264 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1265 END_PROFILE(SMBntrename);
1266 return;
1269 if (ms_has_wild(oldname)) {
1270 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1271 END_PROFILE(SMBntrename);
1272 return;
1275 p++;
1276 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1277 0, STR_TERMINATE, &status,
1278 &dest_has_wcard);
1279 if (!NT_STATUS_IS_OK(status)) {
1280 reply_nterror(req, status);
1281 END_PROFILE(SMBntrename);
1282 return;
1285 status = resolve_dfspath(ctx, conn,
1286 req->flags2 & FLAGS2_DFS_PATHNAMES,
1287 oldname,
1288 &oldname);
1289 if (!NT_STATUS_IS_OK(status)) {
1290 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1291 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1292 ERRSRV, ERRbadpath);
1293 END_PROFILE(SMBntrename);
1294 return;
1296 reply_nterror(req, status);
1297 END_PROFILE(SMBntrename);
1298 return;
1301 status = resolve_dfspath(ctx, conn,
1302 req->flags2 & FLAGS2_DFS_PATHNAMES,
1303 newname,
1304 &newname);
1305 if (!NT_STATUS_IS_OK(status)) {
1306 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1307 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1308 ERRSRV, ERRbadpath);
1309 END_PROFILE(SMBntrename);
1310 return;
1312 reply_nterror(req, status);
1313 END_PROFILE(SMBntrename);
1314 return;
1317 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1319 switch(rename_type) {
1320 case RENAME_FLAG_RENAME:
1321 status = rename_internals(ctx, conn, req, oldname,
1322 newname, attrs, False, src_has_wcard,
1323 dest_has_wcard, DELETE_ACCESS);
1324 break;
1325 case RENAME_FLAG_HARD_LINK:
1326 if (src_has_wcard || dest_has_wcard) {
1327 /* No wildcards. */
1328 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1329 } else {
1330 status = hardlink_internals(ctx,
1331 conn,
1332 oldname,
1333 newname);
1335 break;
1336 case RENAME_FLAG_COPY:
1337 if (src_has_wcard || dest_has_wcard) {
1338 /* No wildcards. */
1339 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1340 } else {
1341 status = copy_internals(ctx, conn, req, oldname,
1342 newname, attrs);
1344 break;
1345 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1346 status = NT_STATUS_INVALID_PARAMETER;
1347 break;
1348 default:
1349 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1350 break;
1353 if (!NT_STATUS_IS_OK(status)) {
1354 if (open_was_deferred(req->mid)) {
1355 /* We have re-scheduled this call. */
1356 END_PROFILE(SMBntrename);
1357 return;
1360 reply_nterror(req, status);
1361 END_PROFILE(SMBntrename);
1362 return;
1365 reply_outbuf(req, 0, 0);
1367 END_PROFILE(SMBntrename);
1368 return;
1371 /****************************************************************************
1372 Reply to a notify change - queue the request and
1373 don't allow a directory to be opened.
1374 ****************************************************************************/
1376 static void call_nt_transact_notify_change(connection_struct *conn,
1377 struct smb_request *req,
1378 uint16 **ppsetup,
1379 uint32 setup_count,
1380 char **ppparams,
1381 uint32 parameter_count,
1382 char **ppdata, uint32 data_count,
1383 uint32 max_data_count,
1384 uint32 max_param_count)
1386 uint16 *setup = *ppsetup;
1387 files_struct *fsp;
1388 uint32 filter;
1389 NTSTATUS status;
1390 bool recursive;
1392 if(setup_count < 6) {
1393 reply_doserror(req, ERRDOS, ERRbadfunc);
1394 return;
1397 fsp = file_fsp(req, SVAL(setup,4));
1398 filter = IVAL(setup, 0);
1399 recursive = (SVAL(setup, 6) != 0) ? True : False;
1401 DEBUG(3,("call_nt_transact_notify_change\n"));
1403 if(!fsp) {
1404 reply_doserror(req, ERRDOS, ERRbadfid);
1405 return;
1409 char *filter_string;
1411 if (!(filter_string = notify_filter_string(NULL, filter))) {
1412 reply_nterror(req,NT_STATUS_NO_MEMORY);
1413 return;
1416 DEBUG(3,("call_nt_transact_notify_change: notify change "
1417 "called on %s, filter = %s, recursive = %d\n",
1418 fsp->fsp_name, filter_string, recursive));
1420 TALLOC_FREE(filter_string);
1423 if((!fsp->is_directory) || (conn != fsp->conn)) {
1424 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1425 return;
1428 if (fsp->notify == NULL) {
1430 status = change_notify_create(fsp, filter, recursive);
1432 if (!NT_STATUS_IS_OK(status)) {
1433 DEBUG(10, ("change_notify_create returned %s\n",
1434 nt_errstr(status)));
1435 reply_nterror(req, status);
1436 return;
1440 if (fsp->notify->num_changes != 0) {
1443 * We've got changes pending, respond immediately
1447 * TODO: write a torture test to check the filtering behaviour
1448 * here.
1451 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1454 * change_notify_reply() above has independently sent its
1455 * results
1457 return;
1461 * No changes pending, queue the request
1464 status = change_notify_add_request(req,
1465 max_param_count,
1466 filter,
1467 recursive, fsp);
1468 if (!NT_STATUS_IS_OK(status)) {
1469 reply_nterror(req, status);
1471 return;
1474 /****************************************************************************
1475 Reply to an NT transact rename command.
1476 ****************************************************************************/
1478 static void call_nt_transact_rename(connection_struct *conn,
1479 struct smb_request *req,
1480 uint16 **ppsetup, uint32 setup_count,
1481 char **ppparams, uint32 parameter_count,
1482 char **ppdata, uint32 data_count,
1483 uint32 max_data_count)
1485 char *params = *ppparams;
1486 char *new_name = NULL;
1487 files_struct *fsp = NULL;
1488 bool dest_has_wcard = False;
1489 NTSTATUS status;
1490 TALLOC_CTX *ctx = talloc_tos();
1492 if(parameter_count < 5) {
1493 reply_doserror(req, ERRDOS, ERRbadfunc);
1494 return;
1497 fsp = file_fsp(req, SVAL(params, 0));
1498 if (!check_fsp(conn, req, fsp)) {
1499 return;
1501 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1502 parameter_count - 4,
1503 STR_TERMINATE, &status, &dest_has_wcard);
1504 if (!NT_STATUS_IS_OK(status)) {
1505 reply_nterror(req, status);
1506 return;
1510 * W2K3 ignores this request as the RAW-RENAME test
1511 * demonstrates, so we do.
1513 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1515 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1516 fsp->fsp_name, new_name));
1518 return;
1521 /******************************************************************************
1522 Fake up a completely empty SD.
1523 *******************************************************************************/
1525 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1527 size_t sd_size;
1529 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1530 if(!*ppsd) {
1531 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1532 return NT_STATUS_NO_MEMORY;
1535 return NT_STATUS_OK;
1538 /****************************************************************************
1539 Reply to query a security descriptor.
1540 ****************************************************************************/
1542 static void call_nt_transact_query_security_desc(connection_struct *conn,
1543 struct smb_request *req,
1544 uint16 **ppsetup,
1545 uint32 setup_count,
1546 char **ppparams,
1547 uint32 parameter_count,
1548 char **ppdata,
1549 uint32 data_count,
1550 uint32 max_data_count)
1552 char *params = *ppparams;
1553 char *data = *ppdata;
1554 SEC_DESC *psd = NULL;
1555 size_t sd_size;
1556 uint32 security_info_wanted;
1557 files_struct *fsp = NULL;
1558 NTSTATUS status;
1559 DATA_BLOB blob;
1561 if(parameter_count < 8) {
1562 reply_doserror(req, ERRDOS, ERRbadfunc);
1563 return;
1566 fsp = file_fsp(req, SVAL(params,0));
1567 if(!fsp) {
1568 reply_doserror(req, ERRDOS, ERRbadfid);
1569 return;
1572 security_info_wanted = IVAL(params,4);
1574 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1575 (unsigned int)security_info_wanted ));
1577 params = nttrans_realloc(ppparams, 4);
1578 if(params == NULL) {
1579 reply_doserror(req, ERRDOS, ERRnomem);
1580 return;
1584 * Get the permissions to return.
1587 if (!lp_nt_acl_support(SNUM(conn))) {
1588 status = get_null_nt_acl(talloc_tos(), &psd);
1589 } else {
1590 status = SMB_VFS_FGET_NT_ACL(
1591 fsp, security_info_wanted, &psd);
1594 if (!NT_STATUS_IS_OK(status)) {
1595 reply_nterror(req, status);
1596 return;
1599 sd_size = ndr_size_security_descriptor(psd, 0);
1601 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1603 SIVAL(params,0,(uint32)sd_size);
1605 if (max_data_count < sd_size) {
1606 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1607 params, 4, *ppdata, 0);
1608 return;
1612 * Allocate the data we will point this at.
1615 data = nttrans_realloc(ppdata, sd_size);
1616 if(data == NULL) {
1617 reply_doserror(req, ERRDOS, ERRnomem);
1618 return;
1621 status = marshall_sec_desc(talloc_tos(), psd,
1622 &blob.data, &blob.length);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 reply_nterror(req, status);
1626 return;
1629 SMB_ASSERT(sd_size == blob.length);
1630 memcpy(data, blob.data, sd_size);
1632 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1634 return;
1637 /****************************************************************************
1638 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1639 ****************************************************************************/
1641 static void call_nt_transact_set_security_desc(connection_struct *conn,
1642 struct smb_request *req,
1643 uint16 **ppsetup,
1644 uint32 setup_count,
1645 char **ppparams,
1646 uint32 parameter_count,
1647 char **ppdata,
1648 uint32 data_count,
1649 uint32 max_data_count)
1651 char *params= *ppparams;
1652 char *data = *ppdata;
1653 files_struct *fsp = NULL;
1654 uint32 security_info_sent = 0;
1655 NTSTATUS status;
1657 if(parameter_count < 8) {
1658 reply_doserror(req, ERRDOS, ERRbadfunc);
1659 return;
1662 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1663 reply_doserror(req, ERRDOS, ERRbadfid);
1664 return;
1667 if(!lp_nt_acl_support(SNUM(conn))) {
1668 goto done;
1671 security_info_sent = IVAL(params,4);
1673 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1674 (unsigned int)security_info_sent ));
1676 if (data_count == 0) {
1677 reply_doserror(req, ERRDOS, ERRnoaccess);
1678 return;
1681 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1683 if (!NT_STATUS_IS_OK(status)) {
1684 reply_nterror(req, status);
1685 return;
1688 done:
1689 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1690 return;
1693 /****************************************************************************
1694 Reply to NT IOCTL
1695 ****************************************************************************/
1697 static void call_nt_transact_ioctl(connection_struct *conn,
1698 struct smb_request *req,
1699 uint16 **ppsetup, uint32 setup_count,
1700 char **ppparams, uint32 parameter_count,
1701 char **ppdata, uint32 data_count,
1702 uint32 max_data_count)
1704 uint32 function;
1705 uint16 fidnum;
1706 files_struct *fsp;
1707 uint8 isFSctl;
1708 uint8 compfilter;
1709 static bool logged_message;
1710 char *pdata = *ppdata;
1712 if (setup_count != 8) {
1713 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1714 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1715 return;
1718 function = IVAL(*ppsetup, 0);
1719 fidnum = SVAL(*ppsetup, 4);
1720 isFSctl = CVAL(*ppsetup, 6);
1721 compfilter = CVAL(*ppsetup, 7);
1723 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1724 function, fidnum, isFSctl, compfilter));
1726 fsp=file_fsp(req, fidnum);
1727 /* this check is done in each implemented function case for now
1728 because I don't want to break anything... --metze
1729 FSP_BELONGS_CONN(fsp,conn);*/
1731 switch (function) {
1732 case FSCTL_SET_SPARSE:
1733 /* pretend this succeeded - tho strictly we should
1734 mark the file sparse (if the local fs supports it)
1735 so we can know if we need to pre-allocate or not */
1737 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1738 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1739 return;
1741 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1743 unsigned char objid[16];
1745 /* This should return the object-id on this file.
1746 * I think I'll make this be the inode+dev. JRA.
1749 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1751 if (!fsp_belongs_conn(conn, req, fsp)) {
1752 return;
1755 data_count = 64;
1756 pdata = nttrans_realloc(ppdata, data_count);
1757 if (pdata == NULL) {
1758 reply_nterror(req, NT_STATUS_NO_MEMORY);
1759 return;
1761 push_file_id_16(pdata, &fsp->file_id);
1762 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1763 push_file_id_16(pdata+32, &fsp->file_id);
1764 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1765 pdata, data_count);
1766 return;
1769 case FSCTL_GET_REPARSE_POINT:
1770 /* pretend this fail - my winXP does it like this
1771 * --metze
1774 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1775 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1776 return;
1778 case FSCTL_SET_REPARSE_POINT:
1779 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1780 * --metze
1783 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1784 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1785 return;
1787 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1790 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1791 * and return their volume names. If max_data_count is 16, then it is just
1792 * asking for the number of volumes and length of the combined names.
1794 * pdata is the data allocated by our caller, but that uses
1795 * total_data_count (which is 0 in our case) rather than max_data_count.
1796 * Allocate the correct amount and return the pointer to let
1797 * it be deallocated when we return.
1799 SHADOW_COPY_DATA *shadow_data = NULL;
1800 TALLOC_CTX *shadow_mem_ctx = NULL;
1801 bool labels = False;
1802 uint32 labels_data_count = 0;
1803 uint32 i;
1804 char *cur_pdata;
1806 if (!fsp_belongs_conn(conn, req, fsp)) {
1807 return;
1810 if (max_data_count < 16) {
1811 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1812 max_data_count));
1813 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1814 return;
1817 if (max_data_count > 16) {
1818 labels = True;
1821 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1822 if (shadow_mem_ctx == NULL) {
1823 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1824 reply_nterror(req, NT_STATUS_NO_MEMORY);
1825 return;
1828 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1829 if (shadow_data == NULL) {
1830 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1831 talloc_destroy(shadow_mem_ctx);
1832 reply_nterror(req, NT_STATUS_NO_MEMORY);
1833 return;
1836 shadow_data->mem_ctx = shadow_mem_ctx;
1839 * Call the VFS routine to actually do the work.
1841 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1842 talloc_destroy(shadow_data->mem_ctx);
1843 if (errno == ENOSYS) {
1844 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1845 conn->connectpath));
1846 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1847 return;
1848 } else {
1849 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1850 conn->connectpath));
1851 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1852 return;
1856 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1858 if (!labels) {
1859 data_count = 16;
1860 } else {
1861 data_count = 12+labels_data_count+4;
1864 if (max_data_count<data_count) {
1865 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1866 max_data_count,data_count));
1867 talloc_destroy(shadow_data->mem_ctx);
1868 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1869 return;
1872 pdata = nttrans_realloc(ppdata, data_count);
1873 if (pdata == NULL) {
1874 talloc_destroy(shadow_data->mem_ctx);
1875 reply_nterror(req, NT_STATUS_NO_MEMORY);
1876 return;
1879 cur_pdata = pdata;
1881 /* num_volumes 4 bytes */
1882 SIVAL(pdata,0,shadow_data->num_volumes);
1884 if (labels) {
1885 /* num_labels 4 bytes */
1886 SIVAL(pdata,4,shadow_data->num_volumes);
1889 /* needed_data_count 4 bytes */
1890 SIVAL(pdata,8,labels_data_count);
1892 cur_pdata+=12;
1894 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1895 shadow_data->num_volumes,fsp->fsp_name));
1896 if (labels && shadow_data->labels) {
1897 for (i=0;i<shadow_data->num_volumes;i++) {
1898 srvstr_push(pdata, req->flags2,
1899 cur_pdata, shadow_data->labels[i],
1900 2*sizeof(SHADOW_COPY_LABEL),
1901 STR_UNICODE|STR_TERMINATE);
1902 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1903 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1907 talloc_destroy(shadow_data->mem_ctx);
1909 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1910 pdata, data_count);
1912 return;
1915 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1917 /* pretend this succeeded -
1919 * we have to send back a list with all files owned by this SID
1921 * but I have to check that --metze
1923 DOM_SID sid;
1924 uid_t uid;
1925 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1927 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1929 if (!fsp_belongs_conn(conn, req, fsp)) {
1930 return;
1933 /* unknown 4 bytes: this is not the length of the sid :-( */
1934 /*unknown = IVAL(pdata,0);*/
1936 sid_parse(pdata+4,sid_len,&sid);
1937 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1939 if (!sid_to_uid(&sid, &uid)) {
1940 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1941 sid_string_dbg(&sid),
1942 (unsigned long)sid_len));
1943 uid = (-1);
1946 /* we can take a look at the find source :-)
1948 * find ./ -uid $uid -name '*' is what we need here
1951 * and send 4bytes len and then NULL terminated unicode strings
1952 * for each file
1954 * but I don't know how to deal with the paged results
1955 * (maybe we can hang the result anywhere in the fsp struct)
1957 * we don't send all files at once
1958 * and at the next we should *not* start from the beginning,
1959 * so we have to cache the result
1961 * --metze
1964 /* this works for now... */
1965 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1966 return;
1968 default:
1969 if (!logged_message) {
1970 logged_message = True; /* Only print this once... */
1971 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1972 function));
1976 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1980 #ifdef HAVE_SYS_QUOTAS
1981 /****************************************************************************
1982 Reply to get user quota
1983 ****************************************************************************/
1985 static void call_nt_transact_get_user_quota(connection_struct *conn,
1986 struct smb_request *req,
1987 uint16 **ppsetup,
1988 uint32 setup_count,
1989 char **ppparams,
1990 uint32 parameter_count,
1991 char **ppdata,
1992 uint32 data_count,
1993 uint32 max_data_count)
1995 NTSTATUS nt_status = NT_STATUS_OK;
1996 char *params = *ppparams;
1997 char *pdata = *ppdata;
1998 char *entry;
1999 int data_len=0,param_len=0;
2000 int qt_len=0;
2001 int entry_len = 0;
2002 files_struct *fsp = NULL;
2003 uint16 level = 0;
2004 size_t sid_len;
2005 DOM_SID sid;
2006 bool start_enum = True;
2007 SMB_NTQUOTA_STRUCT qt;
2008 SMB_NTQUOTA_LIST *tmp_list;
2009 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2011 ZERO_STRUCT(qt);
2013 /* access check */
2014 if (conn->server_info->utok.uid != 0) {
2015 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2016 "[%s]\n", lp_servicename(SNUM(conn)),
2017 conn->server_info->unix_name));
2018 reply_doserror(req, ERRDOS, ERRnoaccess);
2019 return;
2023 * Ensure minimum number of parameters sent.
2026 if (parameter_count < 4) {
2027 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2028 reply_doserror(req, ERRDOS, ERRinvalidparam);
2029 return;
2032 /* maybe we can check the quota_fnum */
2033 fsp = file_fsp(req, SVAL(params,0));
2034 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2035 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2036 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2037 return;
2040 /* the NULL pointer checking for fsp->fake_file_handle->pd
2041 * is done by CHECK_NTQUOTA_HANDLE_OK()
2043 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2045 level = SVAL(params,2);
2047 /* unknown 12 bytes leading in params */
2049 switch (level) {
2050 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2051 /* seems that we should continue with the enum here --metze */
2053 if (qt_handle->quota_list!=NULL &&
2054 qt_handle->tmp_list==NULL) {
2056 /* free the list */
2057 free_ntquota_list(&(qt_handle->quota_list));
2059 /* Realloc the size of parameters and data we will return */
2060 param_len = 4;
2061 params = nttrans_realloc(ppparams, param_len);
2062 if(params == NULL) {
2063 reply_doserror(req, ERRDOS, ERRnomem);
2064 return;
2067 data_len = 0;
2068 SIVAL(params,0,data_len);
2070 break;
2073 start_enum = False;
2075 case TRANSACT_GET_USER_QUOTA_LIST_START:
2077 if (qt_handle->quota_list==NULL &&
2078 qt_handle->tmp_list==NULL) {
2079 start_enum = True;
2082 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2083 reply_doserror(req, ERRSRV, ERRerror);
2084 return;
2087 /* Realloc the size of parameters and data we will return */
2088 param_len = 4;
2089 params = nttrans_realloc(ppparams, param_len);
2090 if(params == NULL) {
2091 reply_doserror(req, ERRDOS, ERRnomem);
2092 return;
2095 /* we should not trust the value in max_data_count*/
2096 max_data_count = MIN(max_data_count,2048);
2098 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2099 if(pdata == NULL) {
2100 reply_doserror(req, ERRDOS, ERRnomem);
2101 return;
2104 entry = pdata;
2106 /* set params Size of returned Quota Data 4 bytes*/
2107 /* but set it later when we know it */
2109 /* for each entry push the data */
2111 if (start_enum) {
2112 qt_handle->tmp_list = qt_handle->quota_list;
2115 tmp_list = qt_handle->tmp_list;
2117 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2118 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2120 sid_len = ndr_size_dom_sid(
2121 &tmp_list->quotas->sid, 0);
2122 entry_len = 40 + sid_len;
2124 /* nextoffset entry 4 bytes */
2125 SIVAL(entry,0,entry_len);
2127 /* then the len of the SID 4 bytes */
2128 SIVAL(entry,4,sid_len);
2130 /* unknown data 8 bytes SMB_BIG_UINT */
2131 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2133 /* the used disk space 8 bytes SMB_BIG_UINT */
2134 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2136 /* the soft quotas 8 bytes SMB_BIG_UINT */
2137 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2139 /* the hard quotas 8 bytes SMB_BIG_UINT */
2140 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2142 /* and now the SID */
2143 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2146 qt_handle->tmp_list = tmp_list;
2148 /* overwrite the offset of the last entry */
2149 SIVAL(entry-entry_len,0,0);
2151 data_len = 4+qt_len;
2152 /* overwrite the params quota_data_len */
2153 SIVAL(params,0,data_len);
2155 break;
2157 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2159 /* unknown 4 bytes IVAL(pdata,0) */
2161 if (data_count < 8) {
2162 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2163 reply_doserror(req, ERRDOS, ERRunknownlevel);
2164 return;
2167 sid_len = IVAL(pdata,4);
2168 /* Ensure this is less than 1mb. */
2169 if (sid_len > (1024*1024)) {
2170 reply_doserror(req, ERRDOS, ERRnomem);
2171 return;
2174 if (data_count < 8+sid_len) {
2175 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2176 reply_doserror(req, ERRDOS, ERRunknownlevel);
2177 return;
2180 data_len = 4+40+sid_len;
2182 if (max_data_count < data_len) {
2183 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2184 max_data_count, data_len));
2185 param_len = 4;
2186 SIVAL(params,0,data_len);
2187 data_len = 0;
2188 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2189 break;
2192 sid_parse(pdata+8,sid_len,&sid);
2194 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2195 ZERO_STRUCT(qt);
2197 * we have to return zero's in all fields
2198 * instead of returning an error here
2199 * --metze
2203 /* Realloc the size of parameters and data we will return */
2204 param_len = 4;
2205 params = nttrans_realloc(ppparams, param_len);
2206 if(params == NULL) {
2207 reply_doserror(req, ERRDOS, ERRnomem);
2208 return;
2211 pdata = nttrans_realloc(ppdata, data_len);
2212 if(pdata == NULL) {
2213 reply_doserror(req, ERRDOS, ERRnomem);
2214 return;
2217 entry = pdata;
2219 /* set params Size of returned Quota Data 4 bytes*/
2220 SIVAL(params,0,data_len);
2222 /* nextoffset entry 4 bytes */
2223 SIVAL(entry,0,0);
2225 /* then the len of the SID 4 bytes */
2226 SIVAL(entry,4,sid_len);
2228 /* unknown data 8 bytes SMB_BIG_UINT */
2229 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2231 /* the used disk space 8 bytes SMB_BIG_UINT */
2232 SBIG_UINT(entry,16,qt.usedspace);
2234 /* the soft quotas 8 bytes SMB_BIG_UINT */
2235 SBIG_UINT(entry,24,qt.softlim);
2237 /* the hard quotas 8 bytes SMB_BIG_UINT */
2238 SBIG_UINT(entry,32,qt.hardlim);
2240 /* and now the SID */
2241 sid_linearize(entry+40, sid_len, &sid);
2243 break;
2245 default:
2246 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2247 reply_doserror(req, ERRSRV, ERRerror);
2248 return;
2249 break;
2252 send_nt_replies(conn, req, nt_status, params, param_len,
2253 pdata, data_len);
2256 /****************************************************************************
2257 Reply to set user quota
2258 ****************************************************************************/
2260 static void call_nt_transact_set_user_quota(connection_struct *conn,
2261 struct smb_request *req,
2262 uint16 **ppsetup,
2263 uint32 setup_count,
2264 char **ppparams,
2265 uint32 parameter_count,
2266 char **ppdata,
2267 uint32 data_count,
2268 uint32 max_data_count)
2270 char *params = *ppparams;
2271 char *pdata = *ppdata;
2272 int data_len=0,param_len=0;
2273 SMB_NTQUOTA_STRUCT qt;
2274 size_t sid_len;
2275 DOM_SID sid;
2276 files_struct *fsp = NULL;
2278 ZERO_STRUCT(qt);
2280 /* access check */
2281 if (conn->server_info->utok.uid != 0) {
2282 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2283 "[%s]\n", lp_servicename(SNUM(conn)),
2284 conn->server_info->unix_name));
2285 reply_doserror(req, ERRDOS, ERRnoaccess);
2286 return;
2290 * Ensure minimum number of parameters sent.
2293 if (parameter_count < 2) {
2294 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2295 reply_doserror(req, ERRDOS, ERRinvalidparam);
2296 return;
2299 /* maybe we can check the quota_fnum */
2300 fsp = file_fsp(req, SVAL(params,0));
2301 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2302 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2303 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2304 return;
2307 if (data_count < 40) {
2308 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2309 reply_doserror(req, ERRDOS, ERRunknownlevel);
2310 return;
2313 /* offset to next quota record.
2314 * 4 bytes IVAL(pdata,0)
2315 * unused here...
2318 /* sid len */
2319 sid_len = IVAL(pdata,4);
2321 if (data_count < 40+sid_len) {
2322 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2323 reply_doserror(req, ERRDOS, ERRunknownlevel);
2324 return;
2327 /* unknown 8 bytes in pdata
2328 * maybe its the change time in NTTIME
2331 /* the used space 8 bytes (SMB_BIG_UINT)*/
2332 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2333 #ifdef LARGE_SMB_OFF_T
2334 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2335 #else /* LARGE_SMB_OFF_T */
2336 if ((IVAL(pdata,20) != 0)&&
2337 ((qt.usedspace != 0xFFFFFFFF)||
2338 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2339 /* more than 32 bits? */
2340 reply_doserror(req, ERRDOS, ERRunknownlevel);
2341 return;
2343 #endif /* LARGE_SMB_OFF_T */
2345 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2346 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2347 #ifdef LARGE_SMB_OFF_T
2348 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2349 #else /* LARGE_SMB_OFF_T */
2350 if ((IVAL(pdata,28) != 0)&&
2351 ((qt.softlim != 0xFFFFFFFF)||
2352 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2353 /* more than 32 bits? */
2354 reply_doserror(req, ERRDOS, ERRunknownlevel);
2355 return;
2357 #endif /* LARGE_SMB_OFF_T */
2359 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2360 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2361 #ifdef LARGE_SMB_OFF_T
2362 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2363 #else /* LARGE_SMB_OFF_T */
2364 if ((IVAL(pdata,36) != 0)&&
2365 ((qt.hardlim != 0xFFFFFFFF)||
2366 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2367 /* more than 32 bits? */
2368 reply_doserror(req, ERRDOS, ERRunknownlevel);
2369 return;
2371 #endif /* LARGE_SMB_OFF_T */
2373 sid_parse(pdata+40,sid_len,&sid);
2374 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2376 /* 44 unknown bytes left... */
2378 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2379 reply_doserror(req, ERRSRV, ERRerror);
2380 return;
2383 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2384 pdata, data_len);
2386 #endif /* HAVE_SYS_QUOTAS */
2388 static void handle_nttrans(connection_struct *conn,
2389 struct trans_state *state,
2390 struct smb_request *req)
2392 if (Protocol >= PROTOCOL_NT1) {
2393 req->flags2 |= 0x40; /* IS_LONG_NAME */
2394 SSVAL(req->inbuf,smb_flg2,req->flags2);
2397 /* Now we must call the relevant NT_TRANS function */
2398 switch(state->call) {
2399 case NT_TRANSACT_CREATE:
2401 START_PROFILE(NT_transact_create);
2402 call_nt_transact_create(
2403 conn, req,
2404 &state->setup, state->setup_count,
2405 &state->param, state->total_param,
2406 &state->data, state->total_data,
2407 state->max_data_return);
2408 END_PROFILE(NT_transact_create);
2409 break;
2412 case NT_TRANSACT_IOCTL:
2414 START_PROFILE(NT_transact_ioctl);
2415 call_nt_transact_ioctl(
2416 conn, req,
2417 &state->setup, state->setup_count,
2418 &state->param, state->total_param,
2419 &state->data, state->total_data,
2420 state->max_data_return);
2421 END_PROFILE(NT_transact_ioctl);
2422 break;
2425 case NT_TRANSACT_SET_SECURITY_DESC:
2427 START_PROFILE(NT_transact_set_security_desc);
2428 call_nt_transact_set_security_desc(
2429 conn, req,
2430 &state->setup, state->setup_count,
2431 &state->param, state->total_param,
2432 &state->data, state->total_data,
2433 state->max_data_return);
2434 END_PROFILE(NT_transact_set_security_desc);
2435 break;
2438 case NT_TRANSACT_NOTIFY_CHANGE:
2440 START_PROFILE(NT_transact_notify_change);
2441 call_nt_transact_notify_change(
2442 conn, req,
2443 &state->setup, state->setup_count,
2444 &state->param, state->total_param,
2445 &state->data, state->total_data,
2446 state->max_data_return,
2447 state->max_param_return);
2448 END_PROFILE(NT_transact_notify_change);
2449 break;
2452 case NT_TRANSACT_RENAME:
2454 START_PROFILE(NT_transact_rename);
2455 call_nt_transact_rename(
2456 conn, req,
2457 &state->setup, state->setup_count,
2458 &state->param, state->total_param,
2459 &state->data, state->total_data,
2460 state->max_data_return);
2461 END_PROFILE(NT_transact_rename);
2462 break;
2465 case NT_TRANSACT_QUERY_SECURITY_DESC:
2467 START_PROFILE(NT_transact_query_security_desc);
2468 call_nt_transact_query_security_desc(
2469 conn, req,
2470 &state->setup, state->setup_count,
2471 &state->param, state->total_param,
2472 &state->data, state->total_data,
2473 state->max_data_return);
2474 END_PROFILE(NT_transact_query_security_desc);
2475 break;
2478 #ifdef HAVE_SYS_QUOTAS
2479 case NT_TRANSACT_GET_USER_QUOTA:
2481 START_PROFILE(NT_transact_get_user_quota);
2482 call_nt_transact_get_user_quota(
2483 conn, req,
2484 &state->setup, state->setup_count,
2485 &state->param, state->total_param,
2486 &state->data, state->total_data,
2487 state->max_data_return);
2488 END_PROFILE(NT_transact_get_user_quota);
2489 break;
2492 case NT_TRANSACT_SET_USER_QUOTA:
2494 START_PROFILE(NT_transact_set_user_quota);
2495 call_nt_transact_set_user_quota(
2496 conn, req,
2497 &state->setup, state->setup_count,
2498 &state->param, state->total_param,
2499 &state->data, state->total_data,
2500 state->max_data_return);
2501 END_PROFILE(NT_transact_set_user_quota);
2502 break;
2504 #endif /* HAVE_SYS_QUOTAS */
2506 default:
2507 /* Error in request */
2508 DEBUG(0,("handle_nttrans: Unknown request %d in "
2509 "nttrans call\n", state->call));
2510 reply_doserror(req, ERRSRV, ERRerror);
2511 return;
2513 return;
2516 /****************************************************************************
2517 Reply to a SMBNTtrans.
2518 ****************************************************************************/
2520 void reply_nttrans(struct smb_request *req)
2522 connection_struct *conn = req->conn;
2523 uint32_t pscnt;
2524 uint32_t psoff;
2525 uint32_t dscnt;
2526 uint32_t dsoff;
2527 uint16 function_code;
2528 NTSTATUS result;
2529 struct trans_state *state;
2530 uint32_t size;
2531 uint32_t av_size;
2533 START_PROFILE(SMBnttrans);
2535 if (req->wct < 19) {
2536 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2537 END_PROFILE(SMBnttrans);
2538 return;
2541 size = smb_len(req->inbuf) + 4;
2542 av_size = smb_len(req->inbuf);
2543 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2544 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2545 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2546 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2547 function_code = SVAL(req->inbuf, smb_nt_Function);
2549 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2550 reply_doserror(req, ERRSRV, ERRaccess);
2551 END_PROFILE(SMBnttrans);
2552 return;
2555 result = allow_new_trans(conn->pending_trans, req->mid);
2556 if (!NT_STATUS_IS_OK(result)) {
2557 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2558 reply_nterror(req, result);
2559 END_PROFILE(SMBnttrans);
2560 return;
2563 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2564 reply_doserror(req, ERRSRV, ERRaccess);
2565 END_PROFILE(SMBnttrans);
2566 return;
2569 state->cmd = SMBnttrans;
2571 state->mid = req->mid;
2572 state->vuid = req->vuid;
2573 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2574 state->data = NULL;
2575 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2576 state->param = NULL;
2577 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2578 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2580 /* setup count is in *words* */
2581 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2582 state->setup = NULL;
2583 state->call = function_code;
2585 DEBUG(10, ("num_setup=%u, "
2586 "param_total=%u, this_param=%u, max_param=%u, "
2587 "data_total=%u, this_data=%u, max_data=%u, "
2588 "param_offset=%u, data_offset=%u\n",
2589 (unsigned)state->setup_count,
2590 (unsigned)state->total_param, (unsigned)pscnt,
2591 (unsigned)state->max_param_return,
2592 (unsigned)state->total_data, (unsigned)dscnt,
2593 (unsigned)state->max_data_return,
2594 (unsigned)psoff, (unsigned)dsoff));
2597 * All nttrans messages we handle have smb_wct == 19 +
2598 * state->setup_count. Ensure this is so as a sanity check.
2601 if(req->wct != 19 + (state->setup_count/2)) {
2602 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2603 req->wct, 19 + (state->setup_count/2)));
2604 goto bad_param;
2607 /* Don't allow more than 128mb for each value. */
2608 if ((state->total_data > (1024*1024*128)) ||
2609 (state->total_param > (1024*1024*128))) {
2610 reply_doserror(req, ERRDOS, ERRnomem);
2611 END_PROFILE(SMBnttrans);
2612 return;
2615 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2616 goto bad_param;
2618 if (state->total_data) {
2619 /* Can't use talloc here, the core routines do realloc on the
2620 * params and data. */
2621 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2622 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2623 "bytes !\n", (unsigned int)state->total_data));
2624 TALLOC_FREE(state);
2625 reply_doserror(req, ERRDOS, ERRnomem);
2626 END_PROFILE(SMBnttrans);
2627 return;
2630 if (dscnt > state->total_data ||
2631 dsoff+dscnt < dsoff) {
2632 goto bad_param;
2635 if (dsoff > av_size ||
2636 dscnt > av_size ||
2637 dsoff+dscnt > av_size) {
2638 goto bad_param;
2641 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2644 if (state->total_param) {
2645 /* Can't use talloc here, the core routines do realloc on the
2646 * params and data. */
2647 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2648 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2649 "bytes !\n", (unsigned int)state->total_param));
2650 SAFE_FREE(state->data);
2651 TALLOC_FREE(state);
2652 reply_doserror(req, ERRDOS, ERRnomem);
2653 END_PROFILE(SMBnttrans);
2654 return;
2657 if (pscnt > state->total_param ||
2658 psoff+pscnt < psoff) {
2659 goto bad_param;
2662 if (psoff > av_size ||
2663 pscnt > av_size ||
2664 psoff+pscnt > av_size) {
2665 goto bad_param;
2668 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2671 state->received_data = dscnt;
2672 state->received_param = pscnt;
2674 if(state->setup_count > 0) {
2675 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2676 state->setup_count));
2677 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2678 if (state->setup == NULL) {
2679 DEBUG(0,("reply_nttrans : Out of memory\n"));
2680 SAFE_FREE(state->data);
2681 SAFE_FREE(state->param);
2682 TALLOC_FREE(state);
2683 reply_doserror(req, ERRDOS, ERRnomem);
2684 END_PROFILE(SMBnttrans);
2685 return;
2688 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2689 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2690 goto bad_param;
2692 if (smb_nt_SetupStart + state->setup_count > size) {
2693 goto bad_param;
2696 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2697 state->setup_count);
2698 dump_data(10, (uint8 *)state->setup, state->setup_count);
2701 if ((state->received_data == state->total_data) &&
2702 (state->received_param == state->total_param)) {
2703 handle_nttrans(conn, state, req);
2704 SAFE_FREE(state->param);
2705 SAFE_FREE(state->data);
2706 TALLOC_FREE(state);
2707 END_PROFILE(SMBnttrans);
2708 return;
2711 DLIST_ADD(conn->pending_trans, state);
2713 /* We need to send an interim response then receive the rest
2714 of the parameter/data bytes */
2715 reply_outbuf(req, 0, 0);
2716 show_msg((char *)req->outbuf);
2717 END_PROFILE(SMBnttrans);
2718 return;
2720 bad_param:
2722 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2723 SAFE_FREE(state->data);
2724 SAFE_FREE(state->param);
2725 TALLOC_FREE(state);
2726 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2727 END_PROFILE(SMBnttrans);
2728 return;
2731 /****************************************************************************
2732 Reply to a SMBnttranss
2733 ****************************************************************************/
2735 void reply_nttranss(struct smb_request *req)
2737 connection_struct *conn = req->conn;
2738 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2739 struct trans_state *state;
2740 uint32_t av_size;
2741 uint32_t size;
2743 START_PROFILE(SMBnttranss);
2745 show_msg((char *)req->inbuf);
2747 if (req->wct < 18) {
2748 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2749 END_PROFILE(SMBnttranss);
2750 return;
2753 for (state = conn->pending_trans; state != NULL;
2754 state = state->next) {
2755 if (state->mid == req->mid) {
2756 break;
2760 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2761 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2762 END_PROFILE(SMBnttranss);
2763 return;
2766 /* Revise state->total_param and state->total_data in case they have
2767 changed downwards */
2768 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2769 < state->total_param) {
2770 state->total_param = IVAL(req->inbuf,
2771 smb_nts_TotalParameterCount);
2773 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2774 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2777 size = smb_len(req->inbuf) + 4;
2778 av_size = smb_len(req->inbuf);
2780 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2781 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2782 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2784 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2785 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2786 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2788 state->received_param += pcnt;
2789 state->received_data += dcnt;
2791 if ((state->received_data > state->total_data) ||
2792 (state->received_param > state->total_param))
2793 goto bad_param;
2795 if (pcnt) {
2796 if (pdisp > state->total_param ||
2797 pcnt > state->total_param ||
2798 pdisp+pcnt > state->total_param ||
2799 pdisp+pcnt < pdisp) {
2800 goto bad_param;
2803 if (poff > av_size ||
2804 pcnt > av_size ||
2805 poff+pcnt > av_size ||
2806 poff+pcnt < poff) {
2807 goto bad_param;
2810 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2811 pcnt);
2814 if (dcnt) {
2815 if (ddisp > state->total_data ||
2816 dcnt > state->total_data ||
2817 ddisp+dcnt > state->total_data ||
2818 ddisp+dcnt < ddisp) {
2819 goto bad_param;
2822 if (ddisp > av_size ||
2823 dcnt > av_size ||
2824 ddisp+dcnt > av_size ||
2825 ddisp+dcnt < ddisp) {
2826 goto bad_param;
2829 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2830 dcnt);
2833 if ((state->received_param < state->total_param) ||
2834 (state->received_data < state->total_data)) {
2835 END_PROFILE(SMBnttranss);
2836 return;
2840 * construct_reply_common will copy smb_com from inbuf to
2841 * outbuf. SMBnttranss is wrong here.
2843 SCVAL(req->inbuf,smb_com,SMBnttrans);
2845 handle_nttrans(conn, state, req);
2847 DLIST_REMOVE(conn->pending_trans, state);
2848 SAFE_FREE(state->data);
2849 SAFE_FREE(state->param);
2850 TALLOC_FREE(state);
2851 END_PROFILE(SMBnttranss);
2852 return;
2854 bad_param:
2856 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2857 DLIST_REMOVE(conn->pending_trans, state);
2858 SAFE_FREE(state->data);
2859 SAFE_FREE(state->param);
2860 TALLOC_FREE(state);
2861 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2862 END_PROFILE(SMBnttranss);
2863 return;