Remove tiny code duplication
[Samba/gebeck_regimport.git] / source3 / smbd / nttrans.c
blob69772b6becc6148e0d208194d3fa2e8e3377eeb6
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 struct current_user current_user;
27 static const char *known_nt_pipes[] = {
28 "\\LANMAN",
29 "\\srvsvc",
30 "\\samr",
31 "\\wkssvc",
32 "\\NETLOGON",
33 "\\ntlsa",
34 "\\ntsvcs",
35 "\\lsass",
36 "\\lsarpc",
37 "\\winreg",
38 "\\initshutdown",
39 "\\spoolss",
40 "\\netdfs",
41 "\\rpcecho",
42 "\\svcctl",
43 "\\eventlog",
44 "\\unixinfo",
45 NULL
48 static char *nttrans_realloc(char **ptr, size_t size)
50 if (ptr==NULL) {
51 smb_panic("nttrans_realloc() called with NULL ptr");
54 *ptr = (char *)SMB_REALLOC(*ptr, size);
55 if(*ptr == NULL) {
56 return NULL;
58 memset(*ptr,'\0',size);
59 return *ptr;
62 /****************************************************************************
63 Send the required number of replies back.
64 We assume all fields other than the data fields are
65 set correctly for the type of call.
66 HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
69 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
70 char *params, int paramsize,
71 char *pdata, int datasize)
73 int data_to_send = datasize;
74 int params_to_send = paramsize;
75 int useable_space;
76 char *pp = params;
77 char *pd = pdata;
78 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
79 int alignment_offset = 3;
80 int data_alignment_offset = 0;
83 * If there genuinely are no parameters or data to send just send
84 * the empty packet.
87 if(params_to_send == 0 && data_to_send == 0) {
88 reply_outbuf(req, 18, 0);
89 show_msg((char *)req->outbuf);
90 return;
94 * When sending params and data ensure that both are nicely aligned.
95 * Only do this alignment when there is also data to send - else
96 * can cause NT redirector problems.
99 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = max_send - (smb_size
111 + 2 * 18 /* wct */
112 + alignment_offset
113 + data_alignment_offset);
116 * useable_space can never be more than max_send minus the
117 * alignment offset.
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 reply_outbuf(req, 18, total_sent_thistime);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
163 } else {
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
179 pp - params);
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
191 } else {
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
211 alignment_offset);
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
239 0, 0, nt_error,
240 __LINE__,__FILE__);
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
246 exit_server_cleanly("send_nt_replies: send_smb failed.");
249 TALLOC_FREE(req->outbuf);
251 pp += params_sent_thistime;
252 pd += data_sent_thistime;
254 params_to_send -= params_sent_thistime;
255 data_to_send -= data_sent_thistime;
258 * Sanity check
261 if(params_to_send < 0 || data_to_send < 0) {
262 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
263 params_to_send, data_to_send));
264 return;
269 /****************************************************************************
270 Is it an NTFS stream name ?
271 ****************************************************************************/
273 bool is_ntfs_stream_name(const char *fname)
275 if (lp_posix_pathnames()) {
276 return False;
278 return (strchr_m(fname, ':') != NULL) ? True : False;
281 /****************************************************************************
282 Reply to an NT create and X call on a pipe
283 ****************************************************************************/
285 static void nt_open_pipe(char *fname, connection_struct *conn,
286 struct smb_request *req, int *ppnum)
288 smb_np_struct *p = NULL;
289 int i;
291 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
293 /* See if it is one we want to handle. */
295 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
296 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
297 ERRDOS, ERRbadpipe);
298 return;
301 for( i = 0; known_nt_pipes[i]; i++ ) {
302 if( strequal(fname,known_nt_pipes[i])) {
303 break;
307 if ( known_nt_pipes[i] == NULL ) {
308 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
309 ERRDOS, ERRbadpipe);
310 return;
313 /* Strip \\ off the name. */
314 fname++;
316 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
318 p = open_rpc_pipe_p(fname, conn, req->vuid);
319 if (!p) {
320 reply_doserror(req, ERRSRV, ERRnofids);
321 return;
324 /* TODO: Add pipe to db */
326 if ( !store_pipe_opendb( p ) ) {
327 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
330 *ppnum = p->pnum;
331 return;
334 /****************************************************************************
335 Reply to an NT create and X call for pipes.
336 ****************************************************************************/
338 static void do_ntcreate_pipe_open(connection_struct *conn,
339 struct smb_request *req)
341 char *fname = NULL;
342 int pnum = -1;
343 char *p = NULL;
344 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
345 TALLOC_CTX *ctx = talloc_tos();
347 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
348 smb_buf(req->inbuf), STR_TERMINATE);
350 if (!fname) {
351 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
352 ERRDOS, ERRbadpipe);
353 return;
355 nt_open_pipe(fname, conn, req, &pnum);
357 if (req->outbuf) {
358 /* error reply */
359 return;
363 * Deal with pipe return.
366 if (flags & EXTENDED_RESPONSE_REQUIRED) {
367 /* This is very strange. We
368 * return 50 words, but only set
369 * the wcnt to 42 ? It's definately
370 * what happens on the wire....
372 reply_outbuf(req, 50, 0);
373 SCVAL(req->outbuf,smb_wct,42);
374 } else {
375 reply_outbuf(req, 34, 0);
378 p = (char *)req->outbuf + smb_vwv2;
379 p++;
380 SSVAL(p,0,pnum);
381 p += 2;
382 SIVAL(p,0,FILE_WAS_OPENED);
383 p += 4;
384 p += 32;
385 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
386 p += 20;
387 /* File type. */
388 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
389 /* Device state. */
390 SSVAL(p,2, 0x5FF); /* ? */
391 p += 4;
393 if (flags & EXTENDED_RESPONSE_REQUIRED) {
394 p += 25;
395 SIVAL(p,0,FILE_GENERIC_ALL);
397 * For pipes W2K3 seems to return
398 * 0x12019B next.
399 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
401 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
404 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
406 chain_reply(req);
409 /****************************************************************************
410 Reply to an NT create and X call.
411 ****************************************************************************/
413 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
415 char *fname = NULL;
416 uint32 flags;
417 uint32 access_mask;
418 uint32 file_attributes;
419 uint32 share_access;
420 uint32 create_disposition;
421 uint32 create_options;
422 uint16 root_dir_fid;
423 SMB_BIG_UINT allocation_size;
424 /* Breakout the oplock request bits so we can set the
425 reply bits separately. */
426 uint32 fattr=0;
427 SMB_OFF_T file_len = 0;
428 SMB_STRUCT_STAT sbuf;
429 int info = 0;
430 files_struct *fsp = NULL;
431 char *p = NULL;
432 struct timespec c_timespec;
433 struct timespec a_timespec;
434 struct timespec m_timespec;
435 NTSTATUS status;
436 int oplock_request;
437 uint8_t oplock_granted = NO_OPLOCK_RETURN;
438 TALLOC_CTX *ctx = talloc_tos();
440 START_PROFILE(SMBntcreateX);
442 if (req->wct < 24) {
443 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
444 return;
447 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
448 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
449 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
450 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
451 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
452 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
453 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
455 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
456 smb_ntcreate_AllocationSize);
457 #ifdef LARGE_SMB_OFF_T
458 allocation_size |= (((SMB_BIG_UINT)IVAL(
459 req->inbuf,
460 smb_ntcreate_AllocationSize + 4)) << 32);
461 #endif
463 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
464 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
466 if (!NT_STATUS_IS_OK(status)) {
467 reply_nterror(req, status);
468 END_PROFILE(SMBntcreateX);
469 return;
472 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
473 "file_attributes = 0x%x, share_access = 0x%x, "
474 "create_disposition = 0x%x create_options = 0x%x "
475 "root_dir_fid = 0x%x, fname = %s\n",
476 (unsigned int)flags,
477 (unsigned int)access_mask,
478 (unsigned int)file_attributes,
479 (unsigned int)share_access,
480 (unsigned int)create_disposition,
481 (unsigned int)create_options,
482 (unsigned int)root_dir_fid,
483 fname));
486 * If it's an IPC, use the pipe handler.
489 if (IS_IPC(conn)) {
490 if (lp_nt_pipe_support()) {
491 do_ntcreate_pipe_open(conn, req);
492 END_PROFILE(SMBntcreateX);
493 return;
494 } else {
495 reply_doserror(req, ERRDOS, ERRnoaccess);
496 END_PROFILE(SMBntcreateX);
497 return;
501 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
502 if (oplock_request) {
503 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
504 ? BATCH_OPLOCK : 0;
507 status = create_file(conn, req, root_dir_fid, fname,
508 access_mask, share_access, create_disposition,
509 create_options, file_attributes, oplock_request,
510 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
512 if (!NT_STATUS_IS_OK(status)) {
513 if (open_was_deferred(req->mid)) {
514 /* We have re-scheduled this call, no error. */
515 END_PROFILE(SMBntcreateX);
516 return;
518 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
519 reply_botherror(req, status, ERRDOS, ERRfilexists);
521 else {
522 reply_nterror(req, status);
524 END_PROFILE(SMBntcreateX);
525 return;
529 * If the caller set the extended oplock request bit
530 * and we granted one (by whatever means) - set the
531 * correct bit for extended oplock reply.
534 if (oplock_request &&
535 (lp_fake_oplocks(SNUM(conn))
536 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
539 * Exclusive oplock granted
542 if (flags & REQUEST_BATCH_OPLOCK) {
543 oplock_granted = BATCH_OPLOCK_RETURN;
544 } else {
545 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
547 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
548 oplock_granted = LEVEL_II_OPLOCK_RETURN;
549 } else {
550 oplock_granted = NO_OPLOCK_RETURN;
553 file_len = sbuf.st_size;
554 fattr = dos_mode(conn,fname,&sbuf);
555 if (fattr == 0) {
556 fattr = FILE_ATTRIBUTE_NORMAL;
559 if (flags & EXTENDED_RESPONSE_REQUIRED) {
560 /* This is very strange. We
561 * return 50 words, but only set
562 * the wcnt to 42 ? It's definately
563 * what happens on the wire....
565 reply_outbuf(req, 50, 0);
566 SCVAL(req->outbuf,smb_wct,42);
567 } else {
568 reply_outbuf(req, 34, 0);
571 p = (char *)req->outbuf + smb_vwv2;
573 SCVAL(p, 0, oplock_granted);
575 p++;
576 SSVAL(p,0,fsp->fnum);
577 p += 2;
578 if ((create_disposition == FILE_SUPERSEDE)
579 && (info == FILE_WAS_OVERWRITTEN)) {
580 SIVAL(p,0,FILE_WAS_SUPERSEDED);
581 } else {
582 SIVAL(p,0,info);
584 p += 4;
586 /* Create time. */
587 c_timespec = get_create_timespec(
588 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
589 a_timespec = get_atimespec(&sbuf);
590 m_timespec = get_mtimespec(&sbuf);
592 if (lp_dos_filetime_resolution(SNUM(conn))) {
593 dos_filetime_timespec(&c_timespec);
594 dos_filetime_timespec(&a_timespec);
595 dos_filetime_timespec(&m_timespec);
598 put_long_date_timespec(p, c_timespec); /* create time. */
599 p += 8;
600 put_long_date_timespec(p, a_timespec); /* access time */
601 p += 8;
602 put_long_date_timespec(p, m_timespec); /* write time */
603 p += 8;
604 put_long_date_timespec(p, m_timespec); /* change time */
605 p += 8;
606 SIVAL(p,0,fattr); /* File Attributes. */
607 p += 4;
608 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
609 p += 8;
610 SOFF_T(p,0,file_len);
611 p += 8;
612 if (flags & EXTENDED_RESPONSE_REQUIRED) {
613 SSVAL(p,2,0x7);
615 p += 4;
616 SCVAL(p,0,fsp->is_directory ? 1 : 0);
618 if (flags & EXTENDED_RESPONSE_REQUIRED) {
619 uint32 perms = 0;
620 p += 25;
621 if (fsp->is_directory
622 || can_write_to_file(conn, fname, &sbuf)) {
623 perms = FILE_GENERIC_ALL;
624 } else {
625 perms = FILE_GENERIC_READ|FILE_EXECUTE;
627 SIVAL(p,0,perms);
630 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
631 fsp->fnum, fsp->fsp_name));
633 chain_reply(req);
634 END_PROFILE(SMBntcreateX);
635 return;
638 /****************************************************************************
639 Reply to a NT_TRANSACT_CREATE call to open a pipe.
640 ****************************************************************************/
642 static void do_nt_transact_create_pipe(connection_struct *conn,
643 struct smb_request *req,
644 uint16 **ppsetup, uint32 setup_count,
645 char **ppparams, uint32 parameter_count,
646 char **ppdata, uint32 data_count)
648 char *fname = NULL;
649 char *params = *ppparams;
650 int pnum = -1;
651 char *p = NULL;
652 NTSTATUS status;
653 size_t param_len;
654 uint32 flags;
655 TALLOC_CTX *ctx = talloc_tos();
658 * Ensure minimum number of parameters sent.
661 if(parameter_count < 54) {
662 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
663 reply_doserror(req, ERRDOS, ERRnoaccess);
664 return;
667 flags = IVAL(params,0);
669 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
670 parameter_count-53, STR_TERMINATE,
671 &status);
672 if (!NT_STATUS_IS_OK(status)) {
673 reply_nterror(req, status);
674 return;
677 nt_open_pipe(fname, conn, req, &pnum);
679 if (req->outbuf) {
680 /* Error return */
681 return;
684 /* Realloc the size of parameters and data we will return */
685 if (flags & EXTENDED_RESPONSE_REQUIRED) {
686 /* Extended response is 32 more byyes. */
687 param_len = 101;
688 } else {
689 param_len = 69;
691 params = nttrans_realloc(ppparams, param_len);
692 if(params == NULL) {
693 reply_doserror(req, ERRDOS, ERRnomem);
694 return;
697 p = params;
698 SCVAL(p,0,NO_OPLOCK_RETURN);
700 p += 2;
701 SSVAL(p,0,pnum);
702 p += 2;
703 SIVAL(p,0,FILE_WAS_OPENED);
704 p += 8;
706 p += 32;
707 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
708 p += 20;
709 /* File type. */
710 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
711 /* Device state. */
712 SSVAL(p,2, 0x5FF); /* ? */
713 p += 4;
715 if (flags & EXTENDED_RESPONSE_REQUIRED) {
716 p += 25;
717 SIVAL(p,0,FILE_GENERIC_ALL);
719 * For pipes W2K3 seems to return
720 * 0x12019B next.
721 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
723 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
726 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
728 /* Send the required number of replies */
729 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
731 return;
734 /****************************************************************************
735 Internal fn to set security descriptors.
736 ****************************************************************************/
738 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
739 uint32 security_info_sent)
741 SEC_DESC *psd = NULL;
742 NTSTATUS status;
744 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
745 return NT_STATUS_OK;
748 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
750 if (!NT_STATUS_IS_OK(status)) {
751 return status;
754 if (psd->owner_sid==0) {
755 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
757 if (psd->group_sid==0) {
758 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
760 if (psd->sacl==0) {
761 security_info_sent &= ~SACL_SECURITY_INFORMATION;
763 if (psd->dacl==0) {
764 security_info_sent &= ~DACL_SECURITY_INFORMATION;
767 if (fsp->fh->fd != -1) {
768 status = SMB_VFS_FSET_NT_ACL(fsp, fsp->fh->fd,
769 security_info_sent, psd);
771 else {
772 status = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name,
773 security_info_sent, psd);
776 TALLOC_FREE(psd);
778 return status;
781 /****************************************************************************
782 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
783 ****************************************************************************/
785 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
787 struct ea_list *ea_list_head = NULL;
788 size_t offset = 0;
790 if (data_size < 4) {
791 return NULL;
794 while (offset + 4 <= data_size) {
795 size_t next_offset = IVAL(pdata,offset);
796 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
798 if (!eal) {
799 return NULL;
802 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
803 if (next_offset == 0) {
804 break;
806 offset += next_offset;
809 return ea_list_head;
812 /****************************************************************************
813 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
814 ****************************************************************************/
816 static void call_nt_transact_create(connection_struct *conn,
817 struct smb_request *req,
818 uint16 **ppsetup, uint32 setup_count,
819 char **ppparams, uint32 parameter_count,
820 char **ppdata, uint32 data_count,
821 uint32 max_data_count)
823 char *fname = NULL;
824 char *params = *ppparams;
825 char *data = *ppdata;
826 /* Breakout the oplock request bits so we can set the reply bits separately. */
827 uint32 fattr=0;
828 SMB_OFF_T file_len = 0;
829 SMB_STRUCT_STAT sbuf;
830 int info = 0;
831 files_struct *fsp = NULL;
832 char *p = NULL;
833 uint32 flags;
834 uint32 access_mask;
835 uint32 file_attributes;
836 uint32 share_access;
837 uint32 create_disposition;
838 uint32 create_options;
839 uint32 sd_len;
840 struct security_descriptor *sd = NULL;
841 uint32 ea_len;
842 uint16 root_dir_fid;
843 struct timespec c_timespec;
844 struct timespec a_timespec;
845 struct timespec m_timespec;
846 struct ea_list *ea_list = NULL;
847 NTSTATUS status;
848 size_t param_len;
849 SMB_BIG_UINT allocation_size;
850 int oplock_request;
851 uint8_t oplock_granted;
852 TALLOC_CTX *ctx = talloc_tos();
854 DEBUG(5,("call_nt_transact_create\n"));
857 * If it's an IPC, use the pipe handler.
860 if (IS_IPC(conn)) {
861 if (lp_nt_pipe_support()) {
862 do_nt_transact_create_pipe(
863 conn, req,
864 ppsetup, setup_count,
865 ppparams, parameter_count,
866 ppdata, data_count);
867 return;
868 } else {
869 reply_doserror(req, ERRDOS, ERRnoaccess);
870 return;
875 * Ensure minimum number of parameters sent.
878 if(parameter_count < 54) {
879 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
880 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
881 return;
884 flags = IVAL(params,0);
885 access_mask = IVAL(params,8);
886 file_attributes = IVAL(params,20);
887 share_access = IVAL(params,24);
888 create_disposition = IVAL(params,28);
889 create_options = IVAL(params,32);
890 sd_len = IVAL(params,36);
891 ea_len = IVAL(params,40);
892 root_dir_fid = (uint16)IVAL(params,4);
893 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
894 #ifdef LARGE_SMB_OFF_T
895 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
896 #endif
898 /* Ensure the data_len is correct for the sd and ea values given. */
899 if ((ea_len + sd_len > data_count)
900 || (ea_len > data_count) || (sd_len > data_count)
901 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
902 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
903 "%u, data_count = %u\n", (unsigned int)ea_len,
904 (unsigned int)sd_len, (unsigned int)data_count));
905 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
906 return;
909 if (sd_len) {
910 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
911 sd_len));
913 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
914 &sd);
915 if (!NT_STATUS_IS_OK(status)) {
916 DEBUG(10, ("call_nt_transact_create: "
917 "unmarshall_sec_desc failed: %s\n",
918 nt_errstr(status)));
919 reply_nterror(req, status);
920 return;
924 if (ea_len) {
925 if (!lp_ea_support(SNUM(conn))) {
926 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
927 "EA's not supported.\n",
928 (unsigned int)ea_len));
929 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
930 return;
933 if (ea_len < 10) {
934 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
935 "too small (should be more than 10)\n",
936 (unsigned int)ea_len ));
937 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
938 return;
941 /* We have already checked that ea_len <= data_count here. */
942 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
943 ea_len);
944 if (ea_list == NULL) {
945 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
946 return;
950 srvstr_get_path(ctx, params, req->flags2, &fname,
951 params+53, parameter_count-53,
952 STR_TERMINATE, &status);
953 if (!NT_STATUS_IS_OK(status)) {
954 reply_nterror(req, status);
955 return;
958 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
959 if (oplock_request) {
960 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
961 ? BATCH_OPLOCK : 0;
964 status = create_file(conn, req, root_dir_fid, fname,
965 access_mask, share_access, create_disposition,
966 create_options, file_attributes, oplock_request,
967 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
969 if(!NT_STATUS_IS_OK(status)) {
970 if (open_was_deferred(req->mid)) {
971 /* We have re-scheduled this call, no error. */
972 return;
974 reply_openerror(req, status);
975 return;
979 * If the caller set the extended oplock request bit
980 * and we granted one (by whatever means) - set the
981 * correct bit for extended oplock reply.
984 if (oplock_request &&
985 (lp_fake_oplocks(SNUM(conn))
986 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
989 * Exclusive oplock granted
992 if (flags & REQUEST_BATCH_OPLOCK) {
993 oplock_granted = BATCH_OPLOCK_RETURN;
994 } else {
995 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
997 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
998 oplock_granted = LEVEL_II_OPLOCK_RETURN;
999 } else {
1000 oplock_granted = NO_OPLOCK_RETURN;
1003 file_len = sbuf.st_size;
1004 fattr = dos_mode(conn,fname,&sbuf);
1005 if (fattr == 0) {
1006 fattr = FILE_ATTRIBUTE_NORMAL;
1009 /* Realloc the size of parameters and data we will return */
1010 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1011 /* Extended response is 32 more byyes. */
1012 param_len = 101;
1013 } else {
1014 param_len = 69;
1016 params = nttrans_realloc(ppparams, param_len);
1017 if(params == NULL) {
1018 reply_doserror(req, ERRDOS, ERRnomem);
1019 return;
1022 p = params;
1023 SCVAL(p, 0, oplock_granted);
1025 p += 2;
1026 SSVAL(p,0,fsp->fnum);
1027 p += 2;
1028 if ((create_disposition == FILE_SUPERSEDE)
1029 && (info == FILE_WAS_OVERWRITTEN)) {
1030 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1031 } else {
1032 SIVAL(p,0,info);
1034 p += 8;
1036 /* Create time. */
1037 c_timespec = get_create_timespec(
1038 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1039 a_timespec = get_atimespec(&sbuf);
1040 m_timespec = get_mtimespec(&sbuf);
1042 if (lp_dos_filetime_resolution(SNUM(conn))) {
1043 dos_filetime_timespec(&c_timespec);
1044 dos_filetime_timespec(&a_timespec);
1045 dos_filetime_timespec(&m_timespec);
1048 put_long_date_timespec(p, c_timespec); /* create time. */
1049 p += 8;
1050 put_long_date_timespec(p, a_timespec); /* access time */
1051 p += 8;
1052 put_long_date_timespec(p, m_timespec); /* write time */
1053 p += 8;
1054 put_long_date_timespec(p, m_timespec); /* change time */
1055 p += 8;
1056 SIVAL(p,0,fattr); /* File Attributes. */
1057 p += 4;
1058 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1059 p += 8;
1060 SOFF_T(p,0,file_len);
1061 p += 8;
1062 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1063 SSVAL(p,2,0x7);
1065 p += 4;
1066 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1068 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1069 uint32 perms = 0;
1070 p += 25;
1071 if (fsp->is_directory
1072 || can_write_to_file(conn, fname, &sbuf)) {
1073 perms = FILE_GENERIC_ALL;
1074 } else {
1075 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1077 SIVAL(p,0,perms);
1080 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1082 /* Send the required number of replies */
1083 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1085 return;
1088 /****************************************************************************
1089 Reply to a NT CANCEL request.
1090 conn POINTER CAN BE NULL HERE !
1091 ****************************************************************************/
1093 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1096 * Go through and cancel any pending change notifies.
1099 START_PROFILE(SMBntcancel);
1100 remove_pending_change_notify_requests_by_mid(req->mid);
1101 remove_pending_lock_requests_by_mid(req->mid);
1102 srv_cancel_sign_response(req->mid);
1104 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1106 END_PROFILE(SMBntcancel);
1107 return;
1110 /****************************************************************************
1111 Copy a file.
1112 ****************************************************************************/
1114 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1115 connection_struct *conn,
1116 struct smb_request *req,
1117 const char *oldname_in,
1118 const char *newname_in,
1119 uint32 attrs)
1121 SMB_STRUCT_STAT sbuf1, sbuf2;
1122 char *oldname = NULL;
1123 char *newname = NULL;
1124 char *last_component_oldname = NULL;
1125 char *last_component_newname = NULL;
1126 files_struct *fsp1,*fsp2;
1127 uint32 fattr;
1128 int info;
1129 SMB_OFF_T ret=-1;
1130 NTSTATUS status = NT_STATUS_OK;
1132 ZERO_STRUCT(sbuf1);
1133 ZERO_STRUCT(sbuf2);
1135 if (!CAN_WRITE(conn)) {
1136 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1139 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1140 &last_component_oldname, &sbuf1);
1141 if (!NT_STATUS_IS_OK(status)) {
1142 return status;
1145 status = check_name(conn, oldname);
1146 if (!NT_STATUS_IS_OK(status)) {
1147 return status;
1150 /* Source must already exist. */
1151 if (!VALID_STAT(sbuf1)) {
1152 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1154 /* Ensure attributes match. */
1155 fattr = dos_mode(conn,oldname,&sbuf1);
1156 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1157 return NT_STATUS_NO_SUCH_FILE;
1160 status = unix_convert(ctx, conn, newname_in, False, &newname,
1161 &last_component_newname, &sbuf2);
1162 if (!NT_STATUS_IS_OK(status)) {
1163 return status;
1166 status = check_name(conn, newname);
1167 if (!NT_STATUS_IS_OK(status)) {
1168 return status;
1171 /* Disallow if newname already exists. */
1172 if (VALID_STAT(sbuf2)) {
1173 return NT_STATUS_OBJECT_NAME_COLLISION;
1176 /* No links from a directory. */
1177 if (S_ISDIR(sbuf1.st_mode)) {
1178 return NT_STATUS_FILE_IS_A_DIRECTORY;
1181 /* Ensure this is within the share. */
1182 status = check_reduced_name(conn, oldname);
1183 if (!NT_STATUS_IS_OK(status)) {
1184 return status;
1187 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1188 oldname, newname));
1190 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1191 FILE_READ_DATA, /* Read-only. */
1192 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1193 FILE_OPEN,
1194 0, /* No create options. */
1195 FILE_ATTRIBUTE_NORMAL,
1196 NO_OPLOCK,
1197 &info, &fsp1);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 return status;
1203 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1204 FILE_WRITE_DATA, /* Read-only. */
1205 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1206 FILE_CREATE,
1207 0, /* No create options. */
1208 fattr,
1209 NO_OPLOCK,
1210 &info, &fsp2);
1212 if (!NT_STATUS_IS_OK(status)) {
1213 close_file(fsp1,ERROR_CLOSE);
1214 return status;
1217 if (sbuf1.st_size) {
1218 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1222 * As we are opening fsp1 read-only we only expect
1223 * an error on close on fsp2 if we are out of space.
1224 * Thus we don't look at the error return from the
1225 * close of fsp1.
1227 close_file(fsp1,NORMAL_CLOSE);
1229 /* Ensure the modtime is set correctly on the destination file. */
1230 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1232 status = close_file(fsp2,NORMAL_CLOSE);
1234 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1235 creates the file. This isn't the correct thing to do in the copy
1236 case. JRA */
1237 file_set_dosmode(conn, newname, fattr, &sbuf2,
1238 parent_dirname(newname),false);
1240 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1241 return NT_STATUS_DISK_FULL;
1244 if (!NT_STATUS_IS_OK(status)) {
1245 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1246 nt_errstr(status), oldname, newname));
1248 return status;
1251 /****************************************************************************
1252 Reply to a NT rename request.
1253 ****************************************************************************/
1255 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1257 char *oldname = NULL;
1258 char *newname = NULL;
1259 char *p;
1260 NTSTATUS status;
1261 bool src_has_wcard = False;
1262 bool dest_has_wcard = False;
1263 uint32 attrs;
1264 uint16 rename_type;
1265 TALLOC_CTX *ctx = talloc_tos();
1267 START_PROFILE(SMBntrename);
1269 if (req->wct < 4) {
1270 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1271 END_PROFILE(SMBntrename);
1272 return;
1275 attrs = SVAL(req->inbuf,smb_vwv0);
1276 rename_type = SVAL(req->inbuf,smb_vwv1);
1278 p = smb_buf(req->inbuf) + 1;
1279 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1280 0, STR_TERMINATE, &status,
1281 &src_has_wcard);
1282 if (!NT_STATUS_IS_OK(status)) {
1283 reply_nterror(req, status);
1284 END_PROFILE(SMBntrename);
1285 return;
1288 if( is_ntfs_stream_name(oldname)) {
1289 /* Can't rename a stream. */
1290 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1291 END_PROFILE(SMBntrename);
1292 return;
1295 if (ms_has_wild(oldname)) {
1296 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1297 END_PROFILE(SMBntrename);
1298 return;
1301 p++;
1302 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1303 0, STR_TERMINATE, &status,
1304 &dest_has_wcard);
1305 if (!NT_STATUS_IS_OK(status)) {
1306 reply_nterror(req, status);
1307 END_PROFILE(SMBntrename);
1308 return;
1311 status = resolve_dfspath(ctx, conn,
1312 req->flags2 & FLAGS2_DFS_PATHNAMES,
1313 oldname,
1314 &oldname);
1315 if (!NT_STATUS_IS_OK(status)) {
1316 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1317 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1318 ERRSRV, ERRbadpath);
1319 END_PROFILE(SMBntrename);
1320 return;
1322 reply_nterror(req, status);
1323 END_PROFILE(SMBntrename);
1324 return;
1327 status = resolve_dfspath(ctx, conn,
1328 req->flags2 & FLAGS2_DFS_PATHNAMES,
1329 newname,
1330 &newname);
1331 if (!NT_STATUS_IS_OK(status)) {
1332 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1333 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1334 ERRSRV, ERRbadpath);
1335 END_PROFILE(SMBntrename);
1336 return;
1338 reply_nterror(req, status);
1339 END_PROFILE(SMBntrename);
1340 return;
1343 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1345 switch(rename_type) {
1346 case RENAME_FLAG_RENAME:
1347 status = rename_internals(ctx, conn, req, oldname,
1348 newname, attrs, False, src_has_wcard,
1349 dest_has_wcard);
1350 break;
1351 case RENAME_FLAG_HARD_LINK:
1352 if (src_has_wcard || dest_has_wcard) {
1353 /* No wildcards. */
1354 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1355 } else {
1356 status = hardlink_internals(ctx,
1357 conn,
1358 oldname,
1359 newname);
1361 break;
1362 case RENAME_FLAG_COPY:
1363 if (src_has_wcard || dest_has_wcard) {
1364 /* No wildcards. */
1365 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1366 } else {
1367 status = copy_internals(ctx, conn, req, oldname,
1368 newname, attrs);
1370 break;
1371 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1372 status = NT_STATUS_INVALID_PARAMETER;
1373 break;
1374 default:
1375 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1376 break;
1379 if (!NT_STATUS_IS_OK(status)) {
1380 if (open_was_deferred(req->mid)) {
1381 /* We have re-scheduled this call. */
1382 END_PROFILE(SMBntrename);
1383 return;
1386 reply_nterror(req, status);
1387 END_PROFILE(SMBntrename);
1388 return;
1391 reply_outbuf(req, 0, 0);
1393 END_PROFILE(SMBntrename);
1394 return;
1397 /****************************************************************************
1398 Reply to a notify change - queue the request and
1399 don't allow a directory to be opened.
1400 ****************************************************************************/
1402 static void call_nt_transact_notify_change(connection_struct *conn,
1403 struct smb_request *req,
1404 uint16 **ppsetup,
1405 uint32 setup_count,
1406 char **ppparams,
1407 uint32 parameter_count,
1408 char **ppdata, uint32 data_count,
1409 uint32 max_data_count,
1410 uint32 max_param_count)
1412 uint16 *setup = *ppsetup;
1413 files_struct *fsp;
1414 uint32 filter;
1415 NTSTATUS status;
1416 bool recursive;
1418 if(setup_count < 6) {
1419 reply_doserror(req, ERRDOS, ERRbadfunc);
1420 return;
1423 fsp = file_fsp(SVAL(setup,4));
1424 filter = IVAL(setup, 0);
1425 recursive = (SVAL(setup, 6) != 0) ? True : False;
1427 DEBUG(3,("call_nt_transact_notify_change\n"));
1429 if(!fsp) {
1430 reply_doserror(req, ERRDOS, ERRbadfid);
1431 return;
1435 char *filter_string;
1437 if (!(filter_string = notify_filter_string(NULL, filter))) {
1438 reply_nterror(req,NT_STATUS_NO_MEMORY);
1439 return;
1442 DEBUG(3,("call_nt_transact_notify_change: notify change "
1443 "called on %s, filter = %s, recursive = %d\n",
1444 fsp->fsp_name, filter_string, recursive));
1446 TALLOC_FREE(filter_string);
1449 if((!fsp->is_directory) || (conn != fsp->conn)) {
1450 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1451 return;
1454 if (fsp->notify == NULL) {
1456 status = change_notify_create(fsp, filter, recursive);
1458 if (!NT_STATUS_IS_OK(status)) {
1459 DEBUG(10, ("change_notify_create returned %s\n",
1460 nt_errstr(status)));
1461 reply_nterror(req, status);
1462 return;
1466 if (fsp->notify->num_changes != 0) {
1469 * We've got changes pending, respond immediately
1473 * TODO: write a torture test to check the filtering behaviour
1474 * here.
1477 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
1480 * change_notify_reply() above has independently sent its
1481 * results
1483 return;
1487 * No changes pending, queue the request
1490 status = change_notify_add_request(req->inbuf, max_param_count, filter,
1491 recursive, fsp);
1492 if (!NT_STATUS_IS_OK(status)) {
1493 reply_nterror(req, status);
1495 return;
1498 /****************************************************************************
1499 Reply to an NT transact rename command.
1500 ****************************************************************************/
1502 static void call_nt_transact_rename(connection_struct *conn,
1503 struct smb_request *req,
1504 uint16 **ppsetup, uint32 setup_count,
1505 char **ppparams, uint32 parameter_count,
1506 char **ppdata, uint32 data_count,
1507 uint32 max_data_count)
1509 char *params = *ppparams;
1510 char *new_name = NULL;
1511 files_struct *fsp = NULL;
1512 bool replace_if_exists = False;
1513 bool dest_has_wcard = False;
1514 NTSTATUS status;
1515 TALLOC_CTX *ctx = talloc_tos();
1517 if(parameter_count < 5) {
1518 reply_doserror(req, ERRDOS, ERRbadfunc);
1519 return;
1522 fsp = file_fsp(SVAL(params, 0));
1523 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1524 if (!check_fsp(conn, req, fsp, &current_user)) {
1525 return;
1527 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1528 parameter_count - 4,
1529 STR_TERMINATE, &status, &dest_has_wcard);
1530 if (!NT_STATUS_IS_OK(status)) {
1531 reply_nterror(req, status);
1532 return;
1535 status = rename_internals(ctx,
1536 conn,
1537 req,
1538 fsp->fsp_name,
1539 new_name,
1541 replace_if_exists,
1542 False,
1543 dest_has_wcard);
1545 if (!NT_STATUS_IS_OK(status)) {
1546 if (open_was_deferred(req->mid)) {
1547 /* We have re-scheduled this call. */
1548 return;
1550 reply_nterror(req, status);
1551 return;
1555 * Rename was successful.
1557 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1559 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1560 fsp->fsp_name, new_name));
1562 return;
1565 /******************************************************************************
1566 Fake up a completely empty SD.
1567 *******************************************************************************/
1569 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1571 size_t sd_size;
1573 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1574 if(!*ppsd) {
1575 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1576 return NT_STATUS_NO_MEMORY;
1579 return NT_STATUS_OK;
1582 /****************************************************************************
1583 Reply to query a security descriptor.
1584 ****************************************************************************/
1586 static void call_nt_transact_query_security_desc(connection_struct *conn,
1587 struct smb_request *req,
1588 uint16 **ppsetup,
1589 uint32 setup_count,
1590 char **ppparams,
1591 uint32 parameter_count,
1592 char **ppdata,
1593 uint32 data_count,
1594 uint32 max_data_count)
1596 char *params = *ppparams;
1597 char *data = *ppdata;
1598 SEC_DESC *psd = NULL;
1599 size_t sd_size;
1600 uint32 security_info_wanted;
1601 TALLOC_CTX *frame;
1602 files_struct *fsp = NULL;
1603 NTSTATUS status;
1604 DATA_BLOB blob;
1606 if(parameter_count < 8) {
1607 reply_doserror(req, ERRDOS, ERRbadfunc);
1608 return;
1611 fsp = file_fsp(SVAL(params,0));
1612 if(!fsp) {
1613 reply_doserror(req, ERRDOS, ERRbadfid);
1614 return;
1617 security_info_wanted = IVAL(params,4);
1619 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1620 (unsigned int)security_info_wanted ));
1622 params = nttrans_realloc(ppparams, 4);
1623 if(params == NULL) {
1624 reply_doserror(req, ERRDOS, ERRnomem);
1625 return;
1628 frame = talloc_stackframe();
1631 * Get the permissions to return.
1634 if (!lp_nt_acl_support(SNUM(conn))) {
1635 status = get_null_nt_acl(talloc_tos(), &psd);
1636 } else {
1637 if (fsp->fh->fd != -1) {
1638 status = SMB_VFS_FGET_NT_ACL(
1639 fsp, fsp->fh->fd, security_info_wanted, &psd);
1641 else {
1642 status = SMB_VFS_GET_NT_ACL(
1643 conn, fsp->fsp_name, security_info_wanted, &psd);
1647 if (!NT_STATUS_IS_OK(status)) {
1648 TALLOC_FREE(frame);
1649 reply_nterror(req, status);
1650 return;
1653 sd_size = ndr_size_security_descriptor(psd, 0);
1655 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1657 SIVAL(params,0,(uint32)sd_size);
1659 if (max_data_count < sd_size) {
1660 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
1661 params, 4, *ppdata, 0);
1662 TALLOC_FREE(frame);
1663 return;
1667 * Allocate the data we will point this at.
1670 data = nttrans_realloc(ppdata, sd_size);
1671 if(data == NULL) {
1672 TALLOC_FREE(frame);
1673 reply_doserror(req, ERRDOS, ERRnomem);
1674 return;
1677 status = marshall_sec_desc(talloc_tos(), psd,
1678 &blob.data, &blob.length);
1680 if (!NT_STATUS_IS_OK(status)) {
1681 TALLOC_FREE(frame);
1682 reply_nterror(req, status);
1683 return;
1686 SMB_ASSERT(sd_size == blob.length);
1687 memcpy(data, blob.data, sd_size);
1689 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1691 TALLOC_FREE(frame);
1692 return;
1695 /****************************************************************************
1696 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1697 ****************************************************************************/
1699 static void call_nt_transact_set_security_desc(connection_struct *conn,
1700 struct smb_request *req,
1701 uint16 **ppsetup,
1702 uint32 setup_count,
1703 char **ppparams,
1704 uint32 parameter_count,
1705 char **ppdata,
1706 uint32 data_count,
1707 uint32 max_data_count)
1709 char *params= *ppparams;
1710 char *data = *ppdata;
1711 files_struct *fsp = NULL;
1712 uint32 security_info_sent = 0;
1713 NTSTATUS status;
1715 if(parameter_count < 8) {
1716 reply_doserror(req, ERRDOS, ERRbadfunc);
1717 return;
1720 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1721 reply_doserror(req, ERRDOS, ERRbadfid);
1722 return;
1725 if(!lp_nt_acl_support(SNUM(conn))) {
1726 goto done;
1729 security_info_sent = IVAL(params,4);
1731 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1732 (unsigned int)security_info_sent ));
1734 if (data_count == 0) {
1735 reply_doserror(req, ERRDOS, ERRnoaccess);
1736 return;
1739 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1741 if (!NT_STATUS_IS_OK(status)) {
1742 reply_nterror(req, status);
1743 return;
1746 done:
1747 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1748 return;
1751 /****************************************************************************
1752 Reply to NT IOCTL
1753 ****************************************************************************/
1755 static void call_nt_transact_ioctl(connection_struct *conn,
1756 struct smb_request *req,
1757 uint16 **ppsetup, uint32 setup_count,
1758 char **ppparams, uint32 parameter_count,
1759 char **ppdata, uint32 data_count,
1760 uint32 max_data_count)
1762 uint32 function;
1763 uint16 fidnum;
1764 files_struct *fsp;
1765 uint8 isFSctl;
1766 uint8 compfilter;
1767 static bool logged_message;
1768 char *pdata = *ppdata;
1770 if (setup_count != 8) {
1771 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1772 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1773 return;
1776 function = IVAL(*ppsetup, 0);
1777 fidnum = SVAL(*ppsetup, 4);
1778 isFSctl = CVAL(*ppsetup, 6);
1779 compfilter = CVAL(*ppsetup, 7);
1781 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1782 function, fidnum, isFSctl, compfilter));
1784 fsp=file_fsp(fidnum);
1785 /* this check is done in each implemented function case for now
1786 because I don't want to break anything... --metze
1787 FSP_BELONGS_CONN(fsp,conn);*/
1789 switch (function) {
1790 case FSCTL_SET_SPARSE:
1791 /* pretend this succeeded - tho strictly we should
1792 mark the file sparse (if the local fs supports it)
1793 so we can know if we need to pre-allocate or not */
1795 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1796 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1797 return;
1799 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1801 unsigned char objid[16];
1803 /* This should return the object-id on this file.
1804 * I think I'll make this be the inode+dev. JRA.
1807 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1809 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1810 return;
1813 data_count = 64;
1814 pdata = nttrans_realloc(ppdata, data_count);
1815 if (pdata == NULL) {
1816 reply_nterror(req, NT_STATUS_NO_MEMORY);
1817 return;
1819 push_file_id_16(pdata, &fsp->file_id);
1820 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1821 push_file_id_16(pdata+32, &fsp->file_id);
1822 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
1823 pdata, data_count);
1824 return;
1827 case FSCTL_GET_REPARSE_POINT:
1828 /* pretend this fail - my winXP does it like this
1829 * --metze
1832 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1833 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1834 return;
1836 case FSCTL_SET_REPARSE_POINT:
1837 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1838 * --metze
1841 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1842 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1843 return;
1845 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1848 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1849 * and return their volume names. If max_data_count is 16, then it is just
1850 * asking for the number of volumes and length of the combined names.
1852 * pdata is the data allocated by our caller, but that uses
1853 * total_data_count (which is 0 in our case) rather than max_data_count.
1854 * Allocate the correct amount and return the pointer to let
1855 * it be deallocated when we return.
1857 SHADOW_COPY_DATA *shadow_data = NULL;
1858 TALLOC_CTX *shadow_mem_ctx = NULL;
1859 bool labels = False;
1860 uint32 labels_data_count = 0;
1861 uint32 i;
1862 char *cur_pdata;
1864 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1865 return;
1868 if (max_data_count < 16) {
1869 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1870 max_data_count));
1871 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1872 return;
1875 if (max_data_count > 16) {
1876 labels = True;
1879 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1880 if (shadow_mem_ctx == NULL) {
1881 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1882 reply_nterror(req, NT_STATUS_NO_MEMORY);
1883 return;
1886 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1887 if (shadow_data == NULL) {
1888 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1889 talloc_destroy(shadow_mem_ctx);
1890 reply_nterror(req, NT_STATUS_NO_MEMORY);
1891 return;
1894 shadow_data->mem_ctx = shadow_mem_ctx;
1897 * Call the VFS routine to actually do the work.
1899 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1900 talloc_destroy(shadow_data->mem_ctx);
1901 if (errno == ENOSYS) {
1902 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1903 conn->connectpath));
1904 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1905 return;
1906 } else {
1907 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1908 conn->connectpath));
1909 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1910 return;
1914 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1916 if (!labels) {
1917 data_count = 16;
1918 } else {
1919 data_count = 12+labels_data_count+4;
1922 if (max_data_count<data_count) {
1923 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1924 max_data_count,data_count));
1925 talloc_destroy(shadow_data->mem_ctx);
1926 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1927 return;
1930 pdata = nttrans_realloc(ppdata, data_count);
1931 if (pdata == NULL) {
1932 talloc_destroy(shadow_data->mem_ctx);
1933 reply_nterror(req, NT_STATUS_NO_MEMORY);
1934 return;
1937 cur_pdata = pdata;
1939 /* num_volumes 4 bytes */
1940 SIVAL(pdata,0,shadow_data->num_volumes);
1942 if (labels) {
1943 /* num_labels 4 bytes */
1944 SIVAL(pdata,4,shadow_data->num_volumes);
1947 /* needed_data_count 4 bytes */
1948 SIVAL(pdata,8,labels_data_count);
1950 cur_pdata+=12;
1952 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1953 shadow_data->num_volumes,fsp->fsp_name));
1954 if (labels && shadow_data->labels) {
1955 for (i=0;i<shadow_data->num_volumes;i++) {
1956 srvstr_push(pdata, req->flags2,
1957 cur_pdata, shadow_data->labels[i],
1958 2*sizeof(SHADOW_COPY_LABEL),
1959 STR_UNICODE|STR_TERMINATE);
1960 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1961 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1965 talloc_destroy(shadow_data->mem_ctx);
1967 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
1968 pdata, data_count);
1970 return;
1973 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1975 /* pretend this succeeded -
1977 * we have to send back a list with all files owned by this SID
1979 * but I have to check that --metze
1981 DOM_SID sid;
1982 uid_t uid;
1983 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1985 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1987 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
1988 return;
1991 /* unknown 4 bytes: this is not the length of the sid :-( */
1992 /*unknown = IVAL(pdata,0);*/
1994 sid_parse(pdata+4,sid_len,&sid);
1995 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1997 if (!sid_to_uid(&sid, &uid)) {
1998 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1999 sid_string_dbg(&sid),
2000 (unsigned long)sid_len));
2001 uid = (-1);
2004 /* we can take a look at the find source :-)
2006 * find ./ -uid $uid -name '*' is what we need here
2009 * and send 4bytes len and then NULL terminated unicode strings
2010 * for each file
2012 * but I don't know how to deal with the paged results
2013 * (maybe we can hang the result anywhere in the fsp struct)
2015 * we don't send all files at once
2016 * and at the next we should *not* start from the beginning,
2017 * so we have to cache the result
2019 * --metze
2022 /* this works for now... */
2023 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2024 return;
2026 default:
2027 if (!logged_message) {
2028 logged_message = True; /* Only print this once... */
2029 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2030 function));
2034 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2038 #ifdef HAVE_SYS_QUOTAS
2039 /****************************************************************************
2040 Reply to get user quota
2041 ****************************************************************************/
2043 static void call_nt_transact_get_user_quota(connection_struct *conn,
2044 struct smb_request *req,
2045 uint16 **ppsetup,
2046 uint32 setup_count,
2047 char **ppparams,
2048 uint32 parameter_count,
2049 char **ppdata,
2050 uint32 data_count,
2051 uint32 max_data_count)
2053 NTSTATUS nt_status = NT_STATUS_OK;
2054 char *params = *ppparams;
2055 char *pdata = *ppdata;
2056 char *entry;
2057 int data_len=0,param_len=0;
2058 int qt_len=0;
2059 int entry_len = 0;
2060 files_struct *fsp = NULL;
2061 uint16 level = 0;
2062 size_t sid_len;
2063 DOM_SID sid;
2064 bool start_enum = True;
2065 SMB_NTQUOTA_STRUCT qt;
2066 SMB_NTQUOTA_LIST *tmp_list;
2067 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2069 ZERO_STRUCT(qt);
2071 /* access check */
2072 if (current_user.ut.uid != 0) {
2073 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2074 lp_servicename(SNUM(conn)),conn->user));
2075 reply_doserror(req, ERRDOS, ERRnoaccess);
2076 return;
2080 * Ensure minimum number of parameters sent.
2083 if (parameter_count < 4) {
2084 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2085 reply_doserror(req, ERRDOS, ERRinvalidparam);
2086 return;
2089 /* maybe we can check the quota_fnum */
2090 fsp = file_fsp(SVAL(params,0));
2091 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2092 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2093 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2094 return;
2097 /* the NULL pointer checking for fsp->fake_file_handle->pd
2098 * is done by CHECK_NTQUOTA_HANDLE_OK()
2100 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2102 level = SVAL(params,2);
2104 /* unknown 12 bytes leading in params */
2106 switch (level) {
2107 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2108 /* seems that we should continue with the enum here --metze */
2110 if (qt_handle->quota_list!=NULL &&
2111 qt_handle->tmp_list==NULL) {
2113 /* free the list */
2114 free_ntquota_list(&(qt_handle->quota_list));
2116 /* Realloc the size of parameters and data we will return */
2117 param_len = 4;
2118 params = nttrans_realloc(ppparams, param_len);
2119 if(params == NULL) {
2120 reply_doserror(req, ERRDOS, ERRnomem);
2121 return;
2124 data_len = 0;
2125 SIVAL(params,0,data_len);
2127 break;
2130 start_enum = False;
2132 case TRANSACT_GET_USER_QUOTA_LIST_START:
2134 if (qt_handle->quota_list==NULL &&
2135 qt_handle->tmp_list==NULL) {
2136 start_enum = True;
2139 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2140 reply_doserror(req, ERRSRV, ERRerror);
2141 return;
2144 /* Realloc the size of parameters and data we will return */
2145 param_len = 4;
2146 params = nttrans_realloc(ppparams, param_len);
2147 if(params == NULL) {
2148 reply_doserror(req, ERRDOS, ERRnomem);
2149 return;
2152 /* we should not trust the value in max_data_count*/
2153 max_data_count = MIN(max_data_count,2048);
2155 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2156 if(pdata == NULL) {
2157 reply_doserror(req, ERRDOS, ERRnomem);
2158 return;
2161 entry = pdata;
2163 /* set params Size of returned Quota Data 4 bytes*/
2164 /* but set it later when we know it */
2166 /* for each entry push the data */
2168 if (start_enum) {
2169 qt_handle->tmp_list = qt_handle->quota_list;
2172 tmp_list = qt_handle->tmp_list;
2174 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2175 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2177 sid_len = ndr_size_dom_sid(
2178 &tmp_list->quotas->sid, 0);
2179 entry_len = 40 + sid_len;
2181 /* nextoffset entry 4 bytes */
2182 SIVAL(entry,0,entry_len);
2184 /* then the len of the SID 4 bytes */
2185 SIVAL(entry,4,sid_len);
2187 /* unknown data 8 bytes SMB_BIG_UINT */
2188 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2190 /* the used disk space 8 bytes SMB_BIG_UINT */
2191 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2193 /* the soft quotas 8 bytes SMB_BIG_UINT */
2194 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2196 /* the hard quotas 8 bytes SMB_BIG_UINT */
2197 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2199 /* and now the SID */
2200 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2203 qt_handle->tmp_list = tmp_list;
2205 /* overwrite the offset of the last entry */
2206 SIVAL(entry-entry_len,0,0);
2208 data_len = 4+qt_len;
2209 /* overwrite the params quota_data_len */
2210 SIVAL(params,0,data_len);
2212 break;
2214 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2216 /* unknown 4 bytes IVAL(pdata,0) */
2218 if (data_count < 8) {
2219 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2220 reply_doserror(req, ERRDOS, ERRunknownlevel);
2221 return;
2224 sid_len = IVAL(pdata,4);
2225 /* Ensure this is less than 1mb. */
2226 if (sid_len > (1024*1024)) {
2227 reply_doserror(req, ERRDOS, ERRnomem);
2228 return;
2231 if (data_count < 8+sid_len) {
2232 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2233 reply_doserror(req, ERRDOS, ERRunknownlevel);
2234 return;
2237 data_len = 4+40+sid_len;
2239 if (max_data_count < data_len) {
2240 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2241 max_data_count, data_len));
2242 param_len = 4;
2243 SIVAL(params,0,data_len);
2244 data_len = 0;
2245 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2246 break;
2249 sid_parse(pdata+8,sid_len,&sid);
2251 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2252 ZERO_STRUCT(qt);
2254 * we have to return zero's in all fields
2255 * instead of returning an error here
2256 * --metze
2260 /* Realloc the size of parameters and data we will return */
2261 param_len = 4;
2262 params = nttrans_realloc(ppparams, param_len);
2263 if(params == NULL) {
2264 reply_doserror(req, ERRDOS, ERRnomem);
2265 return;
2268 pdata = nttrans_realloc(ppdata, data_len);
2269 if(pdata == NULL) {
2270 reply_doserror(req, ERRDOS, ERRnomem);
2271 return;
2274 entry = pdata;
2276 /* set params Size of returned Quota Data 4 bytes*/
2277 SIVAL(params,0,data_len);
2279 /* nextoffset entry 4 bytes */
2280 SIVAL(entry,0,0);
2282 /* then the len of the SID 4 bytes */
2283 SIVAL(entry,4,sid_len);
2285 /* unknown data 8 bytes SMB_BIG_UINT */
2286 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2288 /* the used disk space 8 bytes SMB_BIG_UINT */
2289 SBIG_UINT(entry,16,qt.usedspace);
2291 /* the soft quotas 8 bytes SMB_BIG_UINT */
2292 SBIG_UINT(entry,24,qt.softlim);
2294 /* the hard quotas 8 bytes SMB_BIG_UINT */
2295 SBIG_UINT(entry,32,qt.hardlim);
2297 /* and now the SID */
2298 sid_linearize(entry+40, sid_len, &sid);
2300 break;
2302 default:
2303 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2304 reply_doserror(req, ERRSRV, ERRerror);
2305 return;
2306 break;
2309 send_nt_replies(req, nt_status, params, param_len,
2310 pdata, data_len);
2313 /****************************************************************************
2314 Reply to set user quota
2315 ****************************************************************************/
2317 static void call_nt_transact_set_user_quota(connection_struct *conn,
2318 struct smb_request *req,
2319 uint16 **ppsetup,
2320 uint32 setup_count,
2321 char **ppparams,
2322 uint32 parameter_count,
2323 char **ppdata,
2324 uint32 data_count,
2325 uint32 max_data_count)
2327 char *params = *ppparams;
2328 char *pdata = *ppdata;
2329 int data_len=0,param_len=0;
2330 SMB_NTQUOTA_STRUCT qt;
2331 size_t sid_len;
2332 DOM_SID sid;
2333 files_struct *fsp = NULL;
2335 ZERO_STRUCT(qt);
2337 /* access check */
2338 if (current_user.ut.uid != 0) {
2339 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2340 lp_servicename(SNUM(conn)),conn->user));
2341 reply_doserror(req, ERRDOS, ERRnoaccess);
2342 return;
2346 * Ensure minimum number of parameters sent.
2349 if (parameter_count < 2) {
2350 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2351 reply_doserror(req, ERRDOS, ERRinvalidparam);
2352 return;
2355 /* maybe we can check the quota_fnum */
2356 fsp = file_fsp(SVAL(params,0));
2357 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2358 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2359 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2360 return;
2363 if (data_count < 40) {
2364 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2365 reply_doserror(req, ERRDOS, ERRunknownlevel);
2366 return;
2369 /* offset to next quota record.
2370 * 4 bytes IVAL(pdata,0)
2371 * unused here...
2374 /* sid len */
2375 sid_len = IVAL(pdata,4);
2377 if (data_count < 40+sid_len) {
2378 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2379 reply_doserror(req, ERRDOS, ERRunknownlevel);
2380 return;
2383 /* unknown 8 bytes in pdata
2384 * maybe its the change time in NTTIME
2387 /* the used space 8 bytes (SMB_BIG_UINT)*/
2388 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2389 #ifdef LARGE_SMB_OFF_T
2390 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2391 #else /* LARGE_SMB_OFF_T */
2392 if ((IVAL(pdata,20) != 0)&&
2393 ((qt.usedspace != 0xFFFFFFFF)||
2394 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2395 /* more than 32 bits? */
2396 reply_doserror(req, ERRDOS, ERRunknownlevel);
2397 return;
2399 #endif /* LARGE_SMB_OFF_T */
2401 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2402 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2403 #ifdef LARGE_SMB_OFF_T
2404 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2405 #else /* LARGE_SMB_OFF_T */
2406 if ((IVAL(pdata,28) != 0)&&
2407 ((qt.softlim != 0xFFFFFFFF)||
2408 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2409 /* more than 32 bits? */
2410 reply_doserror(req, ERRDOS, ERRunknownlevel);
2411 return;
2413 #endif /* LARGE_SMB_OFF_T */
2415 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2416 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2417 #ifdef LARGE_SMB_OFF_T
2418 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2419 #else /* LARGE_SMB_OFF_T */
2420 if ((IVAL(pdata,36) != 0)&&
2421 ((qt.hardlim != 0xFFFFFFFF)||
2422 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2423 /* more than 32 bits? */
2424 reply_doserror(req, ERRDOS, ERRunknownlevel);
2425 return;
2427 #endif /* LARGE_SMB_OFF_T */
2429 sid_parse(pdata+40,sid_len,&sid);
2430 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2432 /* 44 unknown bytes left... */
2434 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2435 reply_doserror(req, ERRSRV, ERRerror);
2436 return;
2439 send_nt_replies(req, NT_STATUS_OK, params, param_len,
2440 pdata, data_len);
2442 #endif /* HAVE_SYS_QUOTAS */
2444 static void handle_nttrans(connection_struct *conn,
2445 struct trans_state *state,
2446 struct smb_request *req)
2448 if (Protocol >= PROTOCOL_NT1) {
2449 req->flags2 |= 0x40; /* IS_LONG_NAME */
2450 SSVAL(req->inbuf,smb_flg2,req->flags2);
2453 /* Now we must call the relevant NT_TRANS function */
2454 switch(state->call) {
2455 case NT_TRANSACT_CREATE:
2457 START_PROFILE(NT_transact_create);
2458 call_nt_transact_create(
2459 conn, req,
2460 &state->setup, state->setup_count,
2461 &state->param, state->total_param,
2462 &state->data, state->total_data,
2463 state->max_data_return);
2464 END_PROFILE(NT_transact_create);
2465 break;
2468 case NT_TRANSACT_IOCTL:
2470 START_PROFILE(NT_transact_ioctl);
2471 call_nt_transact_ioctl(
2472 conn, req,
2473 &state->setup, state->setup_count,
2474 &state->param, state->total_param,
2475 &state->data, state->total_data,
2476 state->max_data_return);
2477 END_PROFILE(NT_transact_ioctl);
2478 break;
2481 case NT_TRANSACT_SET_SECURITY_DESC:
2483 START_PROFILE(NT_transact_set_security_desc);
2484 call_nt_transact_set_security_desc(
2485 conn, req,
2486 &state->setup, state->setup_count,
2487 &state->param, state->total_param,
2488 &state->data, state->total_data,
2489 state->max_data_return);
2490 END_PROFILE(NT_transact_set_security_desc);
2491 break;
2494 case NT_TRANSACT_NOTIFY_CHANGE:
2496 START_PROFILE(NT_transact_notify_change);
2497 call_nt_transact_notify_change(
2498 conn, req,
2499 &state->setup, state->setup_count,
2500 &state->param, state->total_param,
2501 &state->data, state->total_data,
2502 state->max_data_return,
2503 state->max_param_return);
2504 END_PROFILE(NT_transact_notify_change);
2505 break;
2508 case NT_TRANSACT_RENAME:
2510 START_PROFILE(NT_transact_rename);
2511 call_nt_transact_rename(
2512 conn, req,
2513 &state->setup, state->setup_count,
2514 &state->param, state->total_param,
2515 &state->data, state->total_data,
2516 state->max_data_return);
2517 END_PROFILE(NT_transact_rename);
2518 break;
2521 case NT_TRANSACT_QUERY_SECURITY_DESC:
2523 START_PROFILE(NT_transact_query_security_desc);
2524 call_nt_transact_query_security_desc(
2525 conn, req,
2526 &state->setup, state->setup_count,
2527 &state->param, state->total_param,
2528 &state->data, state->total_data,
2529 state->max_data_return);
2530 END_PROFILE(NT_transact_query_security_desc);
2531 break;
2534 #ifdef HAVE_SYS_QUOTAS
2535 case NT_TRANSACT_GET_USER_QUOTA:
2537 START_PROFILE(NT_transact_get_user_quota);
2538 call_nt_transact_get_user_quota(
2539 conn, req,
2540 &state->setup, state->setup_count,
2541 &state->param, state->total_param,
2542 &state->data, state->total_data,
2543 state->max_data_return);
2544 END_PROFILE(NT_transact_get_user_quota);
2545 break;
2548 case NT_TRANSACT_SET_USER_QUOTA:
2550 START_PROFILE(NT_transact_set_user_quota);
2551 call_nt_transact_set_user_quota(
2552 conn, req,
2553 &state->setup, state->setup_count,
2554 &state->param, state->total_param,
2555 &state->data, state->total_data,
2556 state->max_data_return);
2557 END_PROFILE(NT_transact_set_user_quota);
2558 break;
2560 #endif /* HAVE_SYS_QUOTAS */
2562 default:
2563 /* Error in request */
2564 DEBUG(0,("handle_nttrans: Unknown request %d in "
2565 "nttrans call\n", state->call));
2566 reply_doserror(req, ERRSRV, ERRerror);
2567 return;
2569 return;
2572 /****************************************************************************
2573 Reply to a SMBNTtrans.
2574 ****************************************************************************/
2576 void reply_nttrans(connection_struct *conn, struct smb_request *req)
2578 uint32 pscnt;
2579 uint32 psoff;
2580 uint32 dscnt;
2581 uint32 dsoff;
2582 uint16 function_code;
2583 NTSTATUS result;
2584 struct trans_state *state;
2585 int size;
2587 START_PROFILE(SMBnttrans);
2589 if (req->wct < 19) {
2590 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2591 END_PROFILE(SMBnttrans);
2592 return;
2595 size = smb_len(req->inbuf) + 4;
2596 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2597 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2598 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2599 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2600 function_code = SVAL(req->inbuf, smb_nt_Function);
2602 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2603 reply_doserror(req, ERRSRV, ERRaccess);
2604 END_PROFILE(SMBnttrans);
2605 return;
2608 result = allow_new_trans(conn->pending_trans, req->mid);
2609 if (!NT_STATUS_IS_OK(result)) {
2610 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2611 reply_nterror(req, result);
2612 END_PROFILE(SMBnttrans);
2613 return;
2616 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
2617 reply_doserror(req, ERRSRV, ERRaccess);
2618 END_PROFILE(SMBnttrans);
2619 return;
2622 state->cmd = SMBnttrans;
2624 state->mid = req->mid;
2625 state->vuid = req->vuid;
2626 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2627 state->data = NULL;
2628 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2629 state->param = NULL;
2630 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2631 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2633 /* setup count is in *words* */
2634 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2635 state->setup = NULL;
2636 state->call = function_code;
2639 * All nttrans messages we handle have smb_wct == 19 +
2640 * state->setup_count. Ensure this is so as a sanity check.
2643 if(req->wct != 19 + (state->setup_count/2)) {
2644 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2645 req->wct, 19 + (state->setup_count/2)));
2646 goto bad_param;
2649 /* Don't allow more than 128mb for each value. */
2650 if ((state->total_data > (1024*1024*128)) ||
2651 (state->total_param > (1024*1024*128))) {
2652 reply_doserror(req, ERRDOS, ERRnomem);
2653 END_PROFILE(SMBnttrans);
2654 return;
2657 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2658 goto bad_param;
2660 if (state->total_data) {
2661 /* Can't use talloc here, the core routines do realloc on the
2662 * params and data. */
2663 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2664 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2665 "bytes !\n", (unsigned int)state->total_data));
2666 TALLOC_FREE(state);
2667 reply_doserror(req, ERRDOS, ERRnomem);
2668 END_PROFILE(SMBnttrans);
2669 return;
2671 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2672 goto bad_param;
2673 if ((smb_base(req->inbuf)+dsoff+dscnt
2674 > (char *)req->inbuf + size) ||
2675 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
2676 goto bad_param;
2678 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2681 if (state->total_param) {
2682 /* Can't use talloc here, the core routines do realloc on the
2683 * params and data. */
2684 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2685 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2686 "bytes !\n", (unsigned int)state->total_param));
2687 SAFE_FREE(state->data);
2688 TALLOC_FREE(state);
2689 reply_doserror(req, ERRDOS, ERRnomem);
2690 END_PROFILE(SMBnttrans);
2691 return;
2693 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2694 goto bad_param;
2695 if ((smb_base(req->inbuf)+psoff+pscnt
2696 > (char *)req->inbuf + size) ||
2697 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
2698 goto bad_param;
2700 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2703 state->received_data = dscnt;
2704 state->received_param = pscnt;
2706 if(state->setup_count > 0) {
2707 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2708 state->setup_count));
2709 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2710 if (state->setup == NULL) {
2711 DEBUG(0,("reply_nttrans : Out of memory\n"));
2712 SAFE_FREE(state->data);
2713 SAFE_FREE(state->param);
2714 TALLOC_FREE(state);
2715 reply_doserror(req, ERRDOS, ERRnomem);
2716 END_PROFILE(SMBnttrans);
2717 return;
2720 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2721 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2722 goto bad_param;
2724 if (smb_nt_SetupStart + state->setup_count > size) {
2725 goto bad_param;
2728 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2729 state->setup_count);
2730 dump_data(10, (uint8 *)state->setup, state->setup_count);
2733 if ((state->received_data == state->total_data) &&
2734 (state->received_param == state->total_param)) {
2735 handle_nttrans(conn, state, req);
2736 SAFE_FREE(state->param);
2737 SAFE_FREE(state->data);
2738 TALLOC_FREE(state);
2739 END_PROFILE(SMBnttrans);
2740 return;
2743 DLIST_ADD(conn->pending_trans, state);
2745 /* We need to send an interim response then receive the rest
2746 of the parameter/data bytes */
2747 reply_outbuf(req, 0, 0);
2748 show_msg((char *)req->outbuf);
2749 END_PROFILE(SMBnttrans);
2750 return;
2752 bad_param:
2754 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2755 SAFE_FREE(state->data);
2756 SAFE_FREE(state->param);
2757 TALLOC_FREE(state);
2758 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2759 END_PROFILE(SMBnttrans);
2760 return;
2763 /****************************************************************************
2764 Reply to a SMBnttranss
2765 ****************************************************************************/
2767 void reply_nttranss(connection_struct *conn, struct smb_request *req)
2769 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2770 struct trans_state *state;
2772 int size;
2774 START_PROFILE(SMBnttranss);
2776 show_msg((char *)req->inbuf);
2778 if (req->wct < 18) {
2779 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2780 END_PROFILE(SMBnttranss);
2781 return;
2784 for (state = conn->pending_trans; state != NULL;
2785 state = state->next) {
2786 if (state->mid == req->mid) {
2787 break;
2791 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2792 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2793 END_PROFILE(SMBnttranss);
2794 return;
2797 /* Revise state->total_param and state->total_data in case they have
2798 changed downwards */
2799 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2800 < state->total_param) {
2801 state->total_param = IVAL(req->inbuf,
2802 smb_nts_TotalParameterCount);
2804 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2805 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2808 size = smb_len(req->inbuf) + 4;
2810 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2811 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2812 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2814 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2815 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2816 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2818 state->received_param += pcnt;
2819 state->received_data += dcnt;
2821 if ((state->received_data > state->total_data) ||
2822 (state->received_param > state->total_param))
2823 goto bad_param;
2825 if (pcnt) {
2826 if (pdisp+pcnt > state->total_param)
2827 goto bad_param;
2828 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
2829 goto bad_param;
2830 if (pdisp > state->total_param)
2831 goto bad_param;
2832 if ((smb_base(req->inbuf) + poff + pcnt
2833 > (char *)req->inbuf + size) ||
2834 (smb_base(req->inbuf) + poff + pcnt
2835 < smb_base(req->inbuf)))
2836 goto bad_param;
2837 if (state->param + pdisp < state->param)
2838 goto bad_param;
2840 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2841 pcnt);
2844 if (dcnt) {
2845 if (ddisp+dcnt > state->total_data)
2846 goto bad_param;
2847 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
2848 goto bad_param;
2849 if (ddisp > state->total_data)
2850 goto bad_param;
2851 if ((smb_base(req->inbuf) + doff + dcnt
2852 > (char *)req->inbuf + size) ||
2853 (smb_base(req->inbuf) + doff + dcnt
2854 < smb_base(req->inbuf)))
2855 goto bad_param;
2856 if (state->data + ddisp < state->data)
2857 goto bad_param;
2859 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2860 dcnt);
2863 if ((state->received_param < state->total_param) ||
2864 (state->received_data < state->total_data)) {
2865 END_PROFILE(SMBnttranss);
2866 return;
2870 * construct_reply_common will copy smb_com from inbuf to
2871 * outbuf. SMBnttranss is wrong here.
2873 SCVAL(req->inbuf,smb_com,SMBnttrans);
2875 handle_nttrans(conn, state, req);
2877 DLIST_REMOVE(conn->pending_trans, state);
2878 SAFE_FREE(state->data);
2879 SAFE_FREE(state->param);
2880 TALLOC_FREE(state);
2881 END_PROFILE(SMBnttranss);
2882 return;
2884 bad_param:
2886 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2887 DLIST_REMOVE(conn->pending_trans, state);
2888 SAFE_FREE(state->data);
2889 SAFE_FREE(state->param);
2890 TALLOC_FREE(state);
2891 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2892 END_PROFILE(SMBnttranss);
2893 return;