man pages: Add documentation about smbclient command "rename".
[Samba.git] / source / smbd / nttrans.c
blob149e6ecbd9de7f6f8cee323d3ef72cf3ef8e6edb
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;
26 static char *nttrans_realloc(char **ptr, size_t size)
28 if (ptr==NULL) {
29 smb_panic("nttrans_realloc() called with NULL ptr");
32 *ptr = (char *)SMB_REALLOC(*ptr, size);
33 if(*ptr == NULL) {
34 return NULL;
36 memset(*ptr,'\0',size);
37 return *ptr;
40 /****************************************************************************
41 Send the required number of replies back.
42 We assume all fields other than the data fields are
43 set correctly for the type of call.
44 HACK ! Always assumes smb_setup field is zero.
45 ****************************************************************************/
47 void send_nt_replies(connection_struct *conn,
48 struct smb_request *req, NTSTATUS nt_error,
49 char *params, int paramsize,
50 char *pdata, int datasize)
52 int data_to_send = datasize;
53 int params_to_send = paramsize;
54 int useable_space;
55 char *pp = params;
56 char *pd = pdata;
57 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
58 int alignment_offset = 3;
59 int data_alignment_offset = 0;
62 * If there genuinely are no parameters or data to send just send
63 * the empty packet.
66 if(params_to_send == 0 && data_to_send == 0) {
67 reply_outbuf(req, 18, 0);
68 show_msg((char *)req->outbuf);
69 return;
73 * When sending params and data ensure that both are nicely aligned.
74 * Only do this alignment when there is also data to send - else
75 * can cause NT redirector problems.
78 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
79 data_alignment_offset = 4 - (params_to_send % 4);
83 * Space is bufsize minus Netbios over TCP header minus SMB header.
84 * The alignment_offset is to align the param bytes on a four byte
85 * boundary (2 bytes for data len, one byte pad).
86 * NT needs this to work correctly.
89 useable_space = max_send - (smb_size
90 + 2 * 18 /* wct */
91 + alignment_offset
92 + data_alignment_offset);
95 * useable_space can never be more than max_send minus the
96 * alignment offset.
99 useable_space = MIN(useable_space,
100 max_send - (alignment_offset+data_alignment_offset));
103 while (params_to_send || data_to_send) {
106 * Calculate whether we will totally or partially fill this packet.
109 total_sent_thistime = params_to_send + data_to_send +
110 alignment_offset + data_alignment_offset;
113 * We can never send more than useable_space.
116 total_sent_thistime = MIN(total_sent_thistime, useable_space);
118 reply_outbuf(req, 18, total_sent_thistime);
121 * Set total params and data to be sent.
124 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
125 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
128 * Calculate how many parameters and data we can fit into
129 * this packet. Parameters get precedence.
132 params_sent_thistime = MIN(params_to_send,useable_space);
133 data_sent_thistime = useable_space - params_sent_thistime;
134 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
136 SIVAL(req->outbuf, smb_ntr_ParameterCount,
137 params_sent_thistime);
139 if(params_sent_thistime == 0) {
140 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
141 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
142 } else {
144 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
145 * parameter bytes, however the first 4 bytes of outbuf are
146 * the Netbios over TCP header. Thus use smb_base() to subtract
147 * them from the calculation.
150 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
151 ((smb_buf(req->outbuf)+alignment_offset)
152 - smb_base(req->outbuf)));
154 * Absolute displacement of param bytes sent in this packet.
157 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
158 pp - params);
162 * Deal with the data portion.
165 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
167 if(data_sent_thistime == 0) {
168 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
169 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
170 } else {
172 * The offset of the data bytes is the offset of the
173 * parameter bytes plus the number of parameters being sent this time.
176 SIVAL(req->outbuf, smb_ntr_DataOffset,
177 ((smb_buf(req->outbuf)+alignment_offset) -
178 smb_base(req->outbuf))
179 + params_sent_thistime + data_alignment_offset);
180 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
184 * Copy the param bytes into the packet.
187 if(params_sent_thistime) {
188 if (alignment_offset != 0) {
189 memset(smb_buf(req->outbuf), 0,
190 alignment_offset);
192 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
193 params_sent_thistime);
197 * Copy in the data bytes
200 if(data_sent_thistime) {
201 if (data_alignment_offset != 0) {
202 memset((smb_buf(req->outbuf)+alignment_offset+
203 params_sent_thistime), 0,
204 data_alignment_offset);
206 memcpy(smb_buf(req->outbuf)+alignment_offset
207 +params_sent_thistime+data_alignment_offset,
208 pd,data_sent_thistime);
211 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
212 params_sent_thistime, data_sent_thistime, useable_space));
213 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
214 params_to_send, data_to_send, paramsize, datasize));
216 if (NT_STATUS_V(nt_error)) {
217 error_packet_set((char *)req->outbuf,
218 0, 0, nt_error,
219 __LINE__,__FILE__);
222 /* Send the packet */
223 show_msg((char *)req->outbuf);
224 if (!srv_send_smb(smbd_server_fd(),
225 (char *)req->outbuf,
226 IS_CONN_ENCRYPTED(conn))) {
227 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
230 TALLOC_FREE(req->outbuf);
232 pp += params_sent_thistime;
233 pd += data_sent_thistime;
235 params_to_send -= params_sent_thistime;
236 data_to_send -= data_sent_thistime;
239 * Sanity check
242 if(params_to_send < 0 || data_to_send < 0) {
243 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
244 params_to_send, data_to_send));
245 return;
250 /****************************************************************************
251 Is it an NTFS stream name ?
252 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
253 $DATA can be used as both a stream name and a stream type. A missing stream
254 name or type implies $DATA.
255 ****************************************************************************/
257 bool is_ntfs_stream_name(const char *fname)
259 if (lp_posix_pathnames()) {
260 return False;
262 return (strchr_m(fname, ':') != NULL) ? True : False;
265 /****************************************************************************
266 Reply to an NT create and X call on a pipe
267 ****************************************************************************/
269 static void nt_open_pipe(char *fname, connection_struct *conn,
270 struct smb_request *req, int *ppnum)
272 smb_np_struct *p = NULL;
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 p = open_rpc_pipe_p(fname, conn, req->vuid);
290 if (!p) {
291 reply_doserror(req, ERRSRV, ERRnofids);
292 return;
295 /* TODO: Add pipe to db */
297 if ( !store_pipe_opendb( p ) ) {
298 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
301 *ppnum = p->pnum;
302 return;
305 /****************************************************************************
306 Reply to an NT create and X call for pipes.
307 ****************************************************************************/
309 static void do_ntcreate_pipe_open(connection_struct *conn,
310 struct smb_request *req)
312 char *fname = NULL;
313 int pnum = -1;
314 char *p = NULL;
315 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
316 TALLOC_CTX *ctx = talloc_tos();
318 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
319 smb_buf(req->inbuf), STR_TERMINATE);
321 if (!fname) {
322 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
323 ERRDOS, ERRbadpipe);
324 return;
326 nt_open_pipe(fname, conn, req, &pnum);
328 if (req->outbuf) {
329 /* error reply */
330 return;
334 * Deal with pipe return.
337 if (flags & EXTENDED_RESPONSE_REQUIRED) {
338 /* This is very strange. We
339 * return 50 words, but only set
340 * the wcnt to 42 ? It's definately
341 * what happens on the wire....
343 reply_outbuf(req, 50, 0);
344 SCVAL(req->outbuf,smb_wct,42);
345 } else {
346 reply_outbuf(req, 34, 0);
349 p = (char *)req->outbuf + smb_vwv2;
350 p++;
351 SSVAL(p,0,pnum);
352 p += 2;
353 SIVAL(p,0,FILE_WAS_OPENED);
354 p += 4;
355 p += 32;
356 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
357 p += 20;
358 /* File type. */
359 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
360 /* Device state. */
361 SSVAL(p,2, 0x5FF); /* ? */
362 p += 4;
364 if (flags & EXTENDED_RESPONSE_REQUIRED) {
365 p += 25;
366 SIVAL(p,0,FILE_GENERIC_ALL);
368 * For pipes W2K3 seems to return
369 * 0x12019B next.
370 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
372 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
375 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
377 chain_reply(req);
380 /****************************************************************************
381 Reply to an NT create and X call.
382 ****************************************************************************/
384 void reply_ntcreate_and_X(struct smb_request *req)
386 connection_struct *conn = req->conn;
387 char *fname = NULL;
388 uint32 flags;
389 uint32 access_mask;
390 uint32 file_attributes;
391 uint32 share_access;
392 uint32 create_disposition;
393 uint32 create_options;
394 uint16 root_dir_fid;
395 SMB_BIG_UINT allocation_size;
396 /* Breakout the oplock request bits so we can set the
397 reply bits separately. */
398 uint32 fattr=0;
399 SMB_OFF_T file_len = 0;
400 SMB_STRUCT_STAT sbuf;
401 int info = 0;
402 files_struct *fsp = NULL;
403 char *p = NULL;
404 struct timespec c_timespec;
405 struct timespec a_timespec;
406 struct timespec m_timespec;
407 NTSTATUS status;
408 int oplock_request;
409 uint8_t oplock_granted = NO_OPLOCK_RETURN;
410 TALLOC_CTX *ctx = talloc_tos();
412 START_PROFILE(SMBntcreateX);
414 if (req->wct < 24) {
415 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
416 return;
419 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
420 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
421 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
422 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
423 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
424 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
425 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
427 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
428 smb_ntcreate_AllocationSize);
429 #ifdef LARGE_SMB_OFF_T
430 allocation_size |= (((SMB_BIG_UINT)IVAL(
431 req->inbuf,
432 smb_ntcreate_AllocationSize + 4)) << 32);
433 #endif
435 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
436 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
438 if (!NT_STATUS_IS_OK(status)) {
439 reply_nterror(req, status);
440 END_PROFILE(SMBntcreateX);
441 return;
444 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
445 "file_attributes = 0x%x, share_access = 0x%x, "
446 "create_disposition = 0x%x create_options = 0x%x "
447 "root_dir_fid = 0x%x, fname = %s\n",
448 (unsigned int)flags,
449 (unsigned int)access_mask,
450 (unsigned int)file_attributes,
451 (unsigned int)share_access,
452 (unsigned int)create_disposition,
453 (unsigned int)create_options,
454 (unsigned int)root_dir_fid,
455 fname));
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 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
740 TALLOC_FREE(psd);
742 return status;
745 /****************************************************************************
746 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
747 ****************************************************************************/
749 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
751 struct ea_list *ea_list_head = NULL;
752 size_t offset = 0;
754 if (data_size < 4) {
755 return NULL;
758 while (offset + 4 <= data_size) {
759 size_t next_offset = IVAL(pdata,offset);
760 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
762 if (!eal) {
763 return NULL;
766 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
767 if (next_offset == 0) {
768 break;
770 offset += next_offset;
773 return ea_list_head;
776 /****************************************************************************
777 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
778 ****************************************************************************/
780 static void call_nt_transact_create(connection_struct *conn,
781 struct smb_request *req,
782 uint16 **ppsetup, uint32 setup_count,
783 char **ppparams, uint32 parameter_count,
784 char **ppdata, uint32 data_count,
785 uint32 max_data_count)
787 char *fname = NULL;
788 char *params = *ppparams;
789 char *data = *ppdata;
790 /* Breakout the oplock request bits so we can set the reply bits separately. */
791 uint32 fattr=0;
792 SMB_OFF_T file_len = 0;
793 SMB_STRUCT_STAT sbuf;
794 int info = 0;
795 files_struct *fsp = NULL;
796 char *p = NULL;
797 uint32 flags;
798 uint32 access_mask;
799 uint32 file_attributes;
800 uint32 share_access;
801 uint32 create_disposition;
802 uint32 create_options;
803 uint32 sd_len;
804 struct security_descriptor *sd = NULL;
805 uint32 ea_len;
806 uint16 root_dir_fid;
807 struct timespec c_timespec;
808 struct timespec a_timespec;
809 struct timespec m_timespec;
810 struct ea_list *ea_list = NULL;
811 NTSTATUS status;
812 size_t param_len;
813 SMB_BIG_UINT allocation_size;
814 int oplock_request;
815 uint8_t oplock_granted;
816 TALLOC_CTX *ctx = talloc_tos();
818 DEBUG(5,("call_nt_transact_create\n"));
821 * If it's an IPC, use the pipe handler.
824 if (IS_IPC(conn)) {
825 if (lp_nt_pipe_support()) {
826 do_nt_transact_create_pipe(
827 conn, req,
828 ppsetup, setup_count,
829 ppparams, parameter_count,
830 ppdata, data_count);
831 return;
833 reply_doserror(req, ERRDOS, ERRnoaccess);
834 return;
838 * Ensure minimum number of parameters sent.
841 if(parameter_count < 54) {
842 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
843 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
844 return;
847 flags = IVAL(params,0);
848 access_mask = IVAL(params,8);
849 file_attributes = IVAL(params,20);
850 share_access = IVAL(params,24);
851 create_disposition = IVAL(params,28);
852 create_options = IVAL(params,32);
853 sd_len = IVAL(params,36);
854 ea_len = IVAL(params,40);
855 root_dir_fid = (uint16)IVAL(params,4);
856 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
857 #ifdef LARGE_SMB_OFF_T
858 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
859 #endif
861 /* Ensure the data_len is correct for the sd and ea values given. */
862 if ((ea_len + sd_len > data_count)
863 || (ea_len > data_count) || (sd_len > data_count)
864 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
865 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
866 "%u, data_count = %u\n", (unsigned int)ea_len,
867 (unsigned int)sd_len, (unsigned int)data_count));
868 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
869 return;
872 if (sd_len) {
873 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
874 sd_len));
876 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
877 &sd);
878 if (!NT_STATUS_IS_OK(status)) {
879 DEBUG(10, ("call_nt_transact_create: "
880 "unmarshall_sec_desc failed: %s\n",
881 nt_errstr(status)));
882 reply_nterror(req, status);
883 return;
887 if (ea_len) {
888 if (!lp_ea_support(SNUM(conn))) {
889 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
890 "EA's not supported.\n",
891 (unsigned int)ea_len));
892 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
893 return;
896 if (ea_len < 10) {
897 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
898 "too small (should be more than 10)\n",
899 (unsigned int)ea_len ));
900 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
901 return;
904 /* We have already checked that ea_len <= data_count here. */
905 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
906 ea_len);
907 if (ea_list == NULL) {
908 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
909 return;
913 srvstr_get_path(ctx, params, req->flags2, &fname,
914 params+53, parameter_count-53,
915 STR_TERMINATE, &status);
916 if (!NT_STATUS_IS_OK(status)) {
917 reply_nterror(req, status);
918 return;
921 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
922 if (oplock_request) {
923 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
924 ? BATCH_OPLOCK : 0;
927 status = create_file(conn, req, root_dir_fid, fname,
928 access_mask, share_access, create_disposition,
929 create_options, file_attributes, oplock_request,
930 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
932 if(!NT_STATUS_IS_OK(status)) {
933 if (open_was_deferred(req->mid)) {
934 /* We have re-scheduled this call, no error. */
935 return;
937 reply_openerror(req, status);
938 return;
942 * If the caller set the extended oplock request bit
943 * and we granted one (by whatever means) - set the
944 * correct bit for extended oplock reply.
947 if (oplock_request &&
948 (lp_fake_oplocks(SNUM(conn))
949 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
952 * Exclusive oplock granted
955 if (flags & REQUEST_BATCH_OPLOCK) {
956 oplock_granted = BATCH_OPLOCK_RETURN;
957 } else {
958 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
960 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
961 oplock_granted = LEVEL_II_OPLOCK_RETURN;
962 } else {
963 oplock_granted = NO_OPLOCK_RETURN;
966 file_len = sbuf.st_size;
967 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
968 if (fattr == 0) {
969 fattr = FILE_ATTRIBUTE_NORMAL;
972 /* Realloc the size of parameters and data we will return */
973 if (flags & EXTENDED_RESPONSE_REQUIRED) {
974 /* Extended response is 32 more byyes. */
975 param_len = 101;
976 } else {
977 param_len = 69;
979 params = nttrans_realloc(ppparams, param_len);
980 if(params == NULL) {
981 reply_doserror(req, ERRDOS, ERRnomem);
982 return;
985 p = params;
986 SCVAL(p, 0, oplock_granted);
988 p += 2;
989 SSVAL(p,0,fsp->fnum);
990 p += 2;
991 if ((create_disposition == FILE_SUPERSEDE)
992 && (info == FILE_WAS_OVERWRITTEN)) {
993 SIVAL(p,0,FILE_WAS_SUPERSEDED);
994 } else {
995 SIVAL(p,0,info);
997 p += 8;
999 /* Create time. */
1000 c_timespec = get_create_timespec(
1001 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1002 a_timespec = get_atimespec(&sbuf);
1003 m_timespec = get_mtimespec(&sbuf);
1005 if (lp_dos_filetime_resolution(SNUM(conn))) {
1006 dos_filetime_timespec(&c_timespec);
1007 dos_filetime_timespec(&a_timespec);
1008 dos_filetime_timespec(&m_timespec);
1011 put_long_date_timespec(p, c_timespec); /* create time. */
1012 p += 8;
1013 put_long_date_timespec(p, a_timespec); /* access time */
1014 p += 8;
1015 put_long_date_timespec(p, m_timespec); /* write time */
1016 p += 8;
1017 put_long_date_timespec(p, m_timespec); /* change time */
1018 p += 8;
1019 SIVAL(p,0,fattr); /* File Attributes. */
1020 p += 4;
1021 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1022 p += 8;
1023 SOFF_T(p,0,file_len);
1024 p += 8;
1025 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1026 SSVAL(p,2,0x7);
1028 p += 4;
1029 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1031 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1032 uint32 perms = 0;
1033 p += 25;
1034 if (fsp->is_directory
1035 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1036 perms = FILE_GENERIC_ALL;
1037 } else {
1038 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1040 SIVAL(p,0,perms);
1043 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1045 /* Send the required number of replies */
1046 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1048 return;
1051 /****************************************************************************
1052 Reply to a NT CANCEL request.
1053 conn POINTER CAN BE NULL HERE !
1054 ****************************************************************************/
1056 void reply_ntcancel(struct smb_request *req)
1059 * Go through and cancel any pending change notifies.
1062 START_PROFILE(SMBntcancel);
1063 remove_pending_change_notify_requests_by_mid(req->mid);
1064 remove_pending_lock_requests_by_mid(req->mid);
1065 srv_cancel_sign_response(req->mid);
1067 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1069 END_PROFILE(SMBntcancel);
1070 return;
1073 /****************************************************************************
1074 Copy a file.
1075 ****************************************************************************/
1077 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1078 connection_struct *conn,
1079 struct smb_request *req,
1080 const char *oldname_in,
1081 const char *newname_in,
1082 uint32 attrs)
1084 SMB_STRUCT_STAT sbuf1, sbuf2;
1085 char *oldname = NULL;
1086 char *newname = NULL;
1087 char *last_component_oldname = NULL;
1088 char *last_component_newname = NULL;
1089 files_struct *fsp1,*fsp2;
1090 uint32 fattr;
1091 int info;
1092 SMB_OFF_T ret=-1;
1093 NTSTATUS status = NT_STATUS_OK;
1095 ZERO_STRUCT(sbuf1);
1096 ZERO_STRUCT(sbuf2);
1098 if (!CAN_WRITE(conn)) {
1099 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1102 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1103 &last_component_oldname, &sbuf1);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 return status;
1108 status = check_name(conn, oldname);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 return status;
1113 /* Source must already exist. */
1114 if (!VALID_STAT(sbuf1)) {
1115 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1117 /* Ensure attributes match. */
1118 fattr = dos_mode(conn,oldname,&sbuf1);
1119 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1120 return NT_STATUS_NO_SUCH_FILE;
1123 status = unix_convert(ctx, conn, newname_in, False, &newname,
1124 &last_component_newname, &sbuf2);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 return status;
1129 status = check_name(conn, newname);
1130 if (!NT_STATUS_IS_OK(status)) {
1131 return status;
1134 /* Disallow if newname already exists. */
1135 if (VALID_STAT(sbuf2)) {
1136 return NT_STATUS_OBJECT_NAME_COLLISION;
1139 /* No links from a directory. */
1140 if (S_ISDIR(sbuf1.st_mode)) {
1141 return NT_STATUS_FILE_IS_A_DIRECTORY;
1144 /* Ensure this is within the share. */
1145 status = check_reduced_name(conn, oldname);
1146 if (!NT_STATUS_IS_OK(status)) {
1147 return status;
1150 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1151 oldname, newname));
1153 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1154 FILE_READ_DATA, /* Read-only. */
1155 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1156 FILE_OPEN,
1157 0, /* No create options. */
1158 FILE_ATTRIBUTE_NORMAL,
1159 NO_OPLOCK,
1160 &info, &fsp1);
1162 if (!NT_STATUS_IS_OK(status)) {
1163 return status;
1166 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1167 FILE_WRITE_DATA, /* Read-only. */
1168 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1169 FILE_CREATE,
1170 0, /* No create options. */
1171 fattr,
1172 NO_OPLOCK,
1173 &info, &fsp2);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 close_file(fsp1,ERROR_CLOSE);
1177 return status;
1180 if (sbuf1.st_size) {
1181 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1185 * As we are opening fsp1 read-only we only expect
1186 * an error on close on fsp2 if we are out of space.
1187 * Thus we don't look at the error return from the
1188 * close of fsp1.
1190 close_file(fsp1,NORMAL_CLOSE);
1192 /* Ensure the modtime is set correctly on the destination file. */
1193 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1195 status = close_file(fsp2,NORMAL_CLOSE);
1197 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1198 creates the file. This isn't the correct thing to do in the copy
1199 case. JRA */
1200 file_set_dosmode(conn, newname, fattr, &sbuf2,
1201 parent_dirname(newname),false);
1203 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1204 return NT_STATUS_DISK_FULL;
1207 if (!NT_STATUS_IS_OK(status)) {
1208 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1209 nt_errstr(status), oldname, newname));
1211 return status;
1214 /****************************************************************************
1215 Reply to a NT rename request.
1216 ****************************************************************************/
1218 void reply_ntrename(struct smb_request *req)
1220 connection_struct *conn = req->conn;
1221 char *oldname = NULL;
1222 char *newname = NULL;
1223 char *p;
1224 NTSTATUS status;
1225 bool src_has_wcard = False;
1226 bool dest_has_wcard = False;
1227 uint32 attrs;
1228 uint16 rename_type;
1229 TALLOC_CTX *ctx = talloc_tos();
1231 START_PROFILE(SMBntrename);
1233 if (req->wct < 4) {
1234 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1235 END_PROFILE(SMBntrename);
1236 return;
1239 attrs = SVAL(req->inbuf,smb_vwv0);
1240 rename_type = SVAL(req->inbuf,smb_vwv1);
1242 p = smb_buf(req->inbuf) + 1;
1243 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1244 0, STR_TERMINATE, &status,
1245 &src_has_wcard);
1246 if (!NT_STATUS_IS_OK(status)) {
1247 reply_nterror(req, status);
1248 END_PROFILE(SMBntrename);
1249 return;
1252 if( is_ntfs_stream_name(oldname)) {
1253 /* Can't rename a stream. */
1254 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1255 END_PROFILE(SMBntrename);
1256 return;
1259 if (ms_has_wild(oldname)) {
1260 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1261 END_PROFILE(SMBntrename);
1262 return;
1265 p++;
1266 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1267 0, STR_TERMINATE, &status,
1268 &dest_has_wcard);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 reply_nterror(req, status);
1271 END_PROFILE(SMBntrename);
1272 return;
1275 status = resolve_dfspath(ctx, conn,
1276 req->flags2 & FLAGS2_DFS_PATHNAMES,
1277 oldname,
1278 &oldname);
1279 if (!NT_STATUS_IS_OK(status)) {
1280 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1281 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1282 ERRSRV, ERRbadpath);
1283 END_PROFILE(SMBntrename);
1284 return;
1286 reply_nterror(req, status);
1287 END_PROFILE(SMBntrename);
1288 return;
1291 status = resolve_dfspath(ctx, conn,
1292 req->flags2 & FLAGS2_DFS_PATHNAMES,
1293 newname,
1294 &newname);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1297 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1298 ERRSRV, ERRbadpath);
1299 END_PROFILE(SMBntrename);
1300 return;
1302 reply_nterror(req, status);
1303 END_PROFILE(SMBntrename);
1304 return;
1307 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1309 switch(rename_type) {
1310 case RENAME_FLAG_RENAME:
1311 status = rename_internals(ctx, conn, req, oldname,
1312 newname, attrs, False, src_has_wcard,
1313 dest_has_wcard, DELETE_ACCESS);
1314 break;
1315 case RENAME_FLAG_HARD_LINK:
1316 if (src_has_wcard || dest_has_wcard) {
1317 /* No wildcards. */
1318 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1319 } else {
1320 status = hardlink_internals(ctx,
1321 conn,
1322 oldname,
1323 newname);
1325 break;
1326 case RENAME_FLAG_COPY:
1327 if (src_has_wcard || dest_has_wcard) {
1328 /* No wildcards. */
1329 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1330 } else {
1331 status = copy_internals(ctx, conn, req, oldname,
1332 newname, attrs);
1334 break;
1335 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1336 status = NT_STATUS_INVALID_PARAMETER;
1337 break;
1338 default:
1339 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1340 break;
1343 if (!NT_STATUS_IS_OK(status)) {
1344 if (open_was_deferred(req->mid)) {
1345 /* We have re-scheduled this call. */
1346 END_PROFILE(SMBntrename);
1347 return;
1350 reply_nterror(req, status);
1351 END_PROFILE(SMBntrename);
1352 return;
1355 reply_outbuf(req, 0, 0);
1357 END_PROFILE(SMBntrename);
1358 return;
1361 /****************************************************************************
1362 Reply to a notify change - queue the request and
1363 don't allow a directory to be opened.
1364 ****************************************************************************/
1366 static void call_nt_transact_notify_change(connection_struct *conn,
1367 struct smb_request *req,
1368 uint16 **ppsetup,
1369 uint32 setup_count,
1370 char **ppparams,
1371 uint32 parameter_count,
1372 char **ppdata, uint32 data_count,
1373 uint32 max_data_count,
1374 uint32 max_param_count)
1376 uint16 *setup = *ppsetup;
1377 files_struct *fsp;
1378 uint32 filter;
1379 NTSTATUS status;
1380 bool recursive;
1382 if(setup_count < 6) {
1383 reply_doserror(req, ERRDOS, ERRbadfunc);
1384 return;
1387 fsp = file_fsp(SVAL(setup,4));
1388 filter = IVAL(setup, 0);
1389 recursive = (SVAL(setup, 6) != 0) ? True : False;
1391 DEBUG(3,("call_nt_transact_notify_change\n"));
1393 if(!fsp) {
1394 reply_doserror(req, ERRDOS, ERRbadfid);
1395 return;
1399 char *filter_string;
1401 if (!(filter_string = notify_filter_string(NULL, filter))) {
1402 reply_nterror(req,NT_STATUS_NO_MEMORY);
1403 return;
1406 DEBUG(3,("call_nt_transact_notify_change: notify change "
1407 "called on %s, filter = %s, recursive = %d\n",
1408 fsp->fsp_name, filter_string, recursive));
1410 TALLOC_FREE(filter_string);
1413 if((!fsp->is_directory) || (conn != fsp->conn)) {
1414 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1415 return;
1418 if (fsp->notify == NULL) {
1420 status = change_notify_create(fsp, filter, recursive);
1422 if (!NT_STATUS_IS_OK(status)) {
1423 DEBUG(10, ("change_notify_create returned %s\n",
1424 nt_errstr(status)));
1425 reply_nterror(req, status);
1426 return;
1430 if (fsp->notify->num_changes != 0) {
1433 * We've got changes pending, respond immediately
1437 * TODO: write a torture test to check the filtering behaviour
1438 * here.
1441 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1444 * change_notify_reply() above has independently sent its
1445 * results
1447 return;
1451 * No changes pending, queue the request
1454 status = change_notify_add_request(req,
1455 max_param_count,
1456 filter,
1457 recursive, fsp);
1458 if (!NT_STATUS_IS_OK(status)) {
1459 reply_nterror(req, status);
1461 return;
1464 /****************************************************************************
1465 Reply to an NT transact rename command.
1466 ****************************************************************************/
1468 static void call_nt_transact_rename(connection_struct *conn,
1469 struct smb_request *req,
1470 uint16 **ppsetup, uint32 setup_count,
1471 char **ppparams, uint32 parameter_count,
1472 char **ppdata, uint32 data_count,
1473 uint32 max_data_count)
1475 char *params = *ppparams;
1476 char *new_name = NULL;
1477 files_struct *fsp = NULL;
1478 bool dest_has_wcard = False;
1479 NTSTATUS status;
1480 TALLOC_CTX *ctx = talloc_tos();
1482 if(parameter_count < 5) {
1483 reply_doserror(req, ERRDOS, ERRbadfunc);
1484 return;
1487 fsp = file_fsp(SVAL(params, 0));
1488 if (!check_fsp(conn, req, fsp)) {
1489 return;
1491 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1492 parameter_count - 4,
1493 STR_TERMINATE, &status, &dest_has_wcard);
1494 if (!NT_STATUS_IS_OK(status)) {
1495 reply_nterror(req, status);
1496 return;
1500 * W2K3 ignores this request as the RAW-RENAME test
1501 * demonstrates, so we do.
1503 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1505 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1506 fsp->fsp_name, new_name));
1508 return;
1511 /******************************************************************************
1512 Fake up a completely empty SD.
1513 *******************************************************************************/
1515 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1517 size_t sd_size;
1519 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1520 if(!*ppsd) {
1521 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1522 return NT_STATUS_NO_MEMORY;
1525 return NT_STATUS_OK;
1528 /****************************************************************************
1529 Reply to query a security descriptor.
1530 ****************************************************************************/
1532 static void call_nt_transact_query_security_desc(connection_struct *conn,
1533 struct smb_request *req,
1534 uint16 **ppsetup,
1535 uint32 setup_count,
1536 char **ppparams,
1537 uint32 parameter_count,
1538 char **ppdata,
1539 uint32 data_count,
1540 uint32 max_data_count)
1542 char *params = *ppparams;
1543 char *data = *ppdata;
1544 SEC_DESC *psd = NULL;
1545 size_t sd_size;
1546 uint32 security_info_wanted;
1547 files_struct *fsp = NULL;
1548 NTSTATUS status;
1549 DATA_BLOB blob;
1551 if(parameter_count < 8) {
1552 reply_doserror(req, ERRDOS, ERRbadfunc);
1553 return;
1556 fsp = file_fsp(SVAL(params,0));
1557 if(!fsp) {
1558 reply_doserror(req, ERRDOS, ERRbadfid);
1559 return;
1562 security_info_wanted = IVAL(params,4);
1564 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1565 (unsigned int)security_info_wanted ));
1567 params = nttrans_realloc(ppparams, 4);
1568 if(params == NULL) {
1569 reply_doserror(req, ERRDOS, ERRnomem);
1570 return;
1574 * Get the permissions to return.
1577 if (!lp_nt_acl_support(SNUM(conn))) {
1578 status = get_null_nt_acl(talloc_tos(), &psd);
1579 } else {
1580 status = SMB_VFS_FGET_NT_ACL(
1581 fsp, security_info_wanted, &psd);
1584 if (!NT_STATUS_IS_OK(status)) {
1585 reply_nterror(req, status);
1586 return;
1589 sd_size = ndr_size_security_descriptor(psd, 0);
1591 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1593 SIVAL(params,0,(uint32)sd_size);
1595 if (max_data_count < sd_size) {
1596 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1597 params, 4, *ppdata, 0);
1598 return;
1602 * Allocate the data we will point this at.
1605 data = nttrans_realloc(ppdata, sd_size);
1606 if(data == NULL) {
1607 reply_doserror(req, ERRDOS, ERRnomem);
1608 return;
1611 status = marshall_sec_desc(talloc_tos(), psd,
1612 &blob.data, &blob.length);
1614 if (!NT_STATUS_IS_OK(status)) {
1615 reply_nterror(req, status);
1616 return;
1619 SMB_ASSERT(sd_size == blob.length);
1620 memcpy(data, blob.data, sd_size);
1622 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1624 return;
1627 /****************************************************************************
1628 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1629 ****************************************************************************/
1631 static void call_nt_transact_set_security_desc(connection_struct *conn,
1632 struct smb_request *req,
1633 uint16 **ppsetup,
1634 uint32 setup_count,
1635 char **ppparams,
1636 uint32 parameter_count,
1637 char **ppdata,
1638 uint32 data_count,
1639 uint32 max_data_count)
1641 char *params= *ppparams;
1642 char *data = *ppdata;
1643 files_struct *fsp = NULL;
1644 uint32 security_info_sent = 0;
1645 NTSTATUS status;
1647 if(parameter_count < 8) {
1648 reply_doserror(req, ERRDOS, ERRbadfunc);
1649 return;
1652 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1653 reply_doserror(req, ERRDOS, ERRbadfid);
1654 return;
1657 if(!lp_nt_acl_support(SNUM(conn))) {
1658 goto done;
1661 security_info_sent = IVAL(params,4);
1663 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1664 (unsigned int)security_info_sent ));
1666 if (data_count == 0) {
1667 reply_doserror(req, ERRDOS, ERRnoaccess);
1668 return;
1671 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1673 if (!NT_STATUS_IS_OK(status)) {
1674 reply_nterror(req, status);
1675 return;
1678 done:
1679 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1680 return;
1683 /****************************************************************************
1684 Reply to NT IOCTL
1685 ****************************************************************************/
1687 static void call_nt_transact_ioctl(connection_struct *conn,
1688 struct smb_request *req,
1689 uint16 **ppsetup, uint32 setup_count,
1690 char **ppparams, uint32 parameter_count,
1691 char **ppdata, uint32 data_count,
1692 uint32 max_data_count)
1694 uint32 function;
1695 uint16 fidnum;
1696 files_struct *fsp;
1697 uint8 isFSctl;
1698 uint8 compfilter;
1699 static bool logged_message;
1700 char *pdata = *ppdata;
1702 if (setup_count != 8) {
1703 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1704 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1705 return;
1708 function = IVAL(*ppsetup, 0);
1709 fidnum = SVAL(*ppsetup, 4);
1710 isFSctl = CVAL(*ppsetup, 6);
1711 compfilter = CVAL(*ppsetup, 7);
1713 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1714 function, fidnum, isFSctl, compfilter));
1716 fsp=file_fsp(fidnum);
1717 /* this check is done in each implemented function case for now
1718 because I don't want to break anything... --metze
1719 FSP_BELONGS_CONN(fsp,conn);*/
1721 switch (function) {
1722 case FSCTL_SET_SPARSE:
1723 /* pretend this succeeded - tho strictly we should
1724 mark the file sparse (if the local fs supports it)
1725 so we can know if we need to pre-allocate or not */
1727 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1728 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1729 return;
1731 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1733 unsigned char objid[16];
1735 /* This should return the object-id on this file.
1736 * I think I'll make this be the inode+dev. JRA.
1739 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1741 if (!fsp_belongs_conn(conn, req, fsp)) {
1742 return;
1745 data_count = 64;
1746 pdata = nttrans_realloc(ppdata, data_count);
1747 if (pdata == NULL) {
1748 reply_nterror(req, NT_STATUS_NO_MEMORY);
1749 return;
1751 push_file_id_16(pdata, &fsp->file_id);
1752 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1753 push_file_id_16(pdata+32, &fsp->file_id);
1754 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1755 pdata, data_count);
1756 return;
1759 case FSCTL_GET_REPARSE_POINT:
1760 /* pretend this fail - my winXP does it like this
1761 * --metze
1764 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1765 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1766 return;
1768 case FSCTL_SET_REPARSE_POINT:
1769 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1770 * --metze
1773 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1774 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1775 return;
1777 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1780 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1781 * and return their volume names. If max_data_count is 16, then it is just
1782 * asking for the number of volumes and length of the combined names.
1784 * pdata is the data allocated by our caller, but that uses
1785 * total_data_count (which is 0 in our case) rather than max_data_count.
1786 * Allocate the correct amount and return the pointer to let
1787 * it be deallocated when we return.
1789 SHADOW_COPY_DATA *shadow_data = NULL;
1790 TALLOC_CTX *shadow_mem_ctx = NULL;
1791 bool labels = False;
1792 uint32 labels_data_count = 0;
1793 uint32 i;
1794 char *cur_pdata;
1796 if (!fsp_belongs_conn(conn, req, fsp)) {
1797 return;
1800 if (max_data_count < 16) {
1801 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1802 max_data_count));
1803 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1804 return;
1807 if (max_data_count > 16) {
1808 labels = True;
1811 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1812 if (shadow_mem_ctx == NULL) {
1813 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1814 reply_nterror(req, NT_STATUS_NO_MEMORY);
1815 return;
1818 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1819 if (shadow_data == NULL) {
1820 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1821 talloc_destroy(shadow_mem_ctx);
1822 reply_nterror(req, NT_STATUS_NO_MEMORY);
1823 return;
1826 shadow_data->mem_ctx = shadow_mem_ctx;
1829 * Call the VFS routine to actually do the work.
1831 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1832 talloc_destroy(shadow_data->mem_ctx);
1833 if (errno == ENOSYS) {
1834 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1835 conn->connectpath));
1836 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1837 return;
1838 } else {
1839 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1840 conn->connectpath));
1841 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1842 return;
1846 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1848 if (!labels) {
1849 data_count = 16;
1850 } else {
1851 data_count = 12+labels_data_count+4;
1854 if (max_data_count<data_count) {
1855 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1856 max_data_count,data_count));
1857 talloc_destroy(shadow_data->mem_ctx);
1858 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1859 return;
1862 pdata = nttrans_realloc(ppdata, data_count);
1863 if (pdata == NULL) {
1864 talloc_destroy(shadow_data->mem_ctx);
1865 reply_nterror(req, NT_STATUS_NO_MEMORY);
1866 return;
1869 cur_pdata = pdata;
1871 /* num_volumes 4 bytes */
1872 SIVAL(pdata,0,shadow_data->num_volumes);
1874 if (labels) {
1875 /* num_labels 4 bytes */
1876 SIVAL(pdata,4,shadow_data->num_volumes);
1879 /* needed_data_count 4 bytes */
1880 SIVAL(pdata,8,labels_data_count);
1882 cur_pdata+=12;
1884 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1885 shadow_data->num_volumes,fsp->fsp_name));
1886 if (labels && shadow_data->labels) {
1887 for (i=0;i<shadow_data->num_volumes;i++) {
1888 srvstr_push(pdata, req->flags2,
1889 cur_pdata, shadow_data->labels[i],
1890 2*sizeof(SHADOW_COPY_LABEL),
1891 STR_UNICODE|STR_TERMINATE);
1892 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1893 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1897 talloc_destroy(shadow_data->mem_ctx);
1899 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1900 pdata, data_count);
1902 return;
1905 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1907 /* pretend this succeeded -
1909 * we have to send back a list with all files owned by this SID
1911 * but I have to check that --metze
1913 DOM_SID sid;
1914 uid_t uid;
1915 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1917 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1919 if (!fsp_belongs_conn(conn, req, fsp)) {
1920 return;
1923 /* unknown 4 bytes: this is not the length of the sid :-( */
1924 /*unknown = IVAL(pdata,0);*/
1926 sid_parse(pdata+4,sid_len,&sid);
1927 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1929 if (!sid_to_uid(&sid, &uid)) {
1930 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1931 sid_string_dbg(&sid),
1932 (unsigned long)sid_len));
1933 uid = (-1);
1936 /* we can take a look at the find source :-)
1938 * find ./ -uid $uid -name '*' is what we need here
1941 * and send 4bytes len and then NULL terminated unicode strings
1942 * for each file
1944 * but I don't know how to deal with the paged results
1945 * (maybe we can hang the result anywhere in the fsp struct)
1947 * we don't send all files at once
1948 * and at the next we should *not* start from the beginning,
1949 * so we have to cache the result
1951 * --metze
1954 /* this works for now... */
1955 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1956 return;
1958 default:
1959 if (!logged_message) {
1960 logged_message = True; /* Only print this once... */
1961 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1962 function));
1966 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1970 #ifdef HAVE_SYS_QUOTAS
1971 /****************************************************************************
1972 Reply to get user quota
1973 ****************************************************************************/
1975 static void call_nt_transact_get_user_quota(connection_struct *conn,
1976 struct smb_request *req,
1977 uint16 **ppsetup,
1978 uint32 setup_count,
1979 char **ppparams,
1980 uint32 parameter_count,
1981 char **ppdata,
1982 uint32 data_count,
1983 uint32 max_data_count)
1985 NTSTATUS nt_status = NT_STATUS_OK;
1986 char *params = *ppparams;
1987 char *pdata = *ppdata;
1988 char *entry;
1989 int data_len=0,param_len=0;
1990 int qt_len=0;
1991 int entry_len = 0;
1992 files_struct *fsp = NULL;
1993 uint16 level = 0;
1994 size_t sid_len;
1995 DOM_SID sid;
1996 bool start_enum = True;
1997 SMB_NTQUOTA_STRUCT qt;
1998 SMB_NTQUOTA_LIST *tmp_list;
1999 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2001 ZERO_STRUCT(qt);
2003 /* access check */
2004 if (conn->server_info->utok.uid != 0) {
2005 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2006 "[%s]\n", lp_servicename(SNUM(conn)),
2007 conn->server_info->unix_name));
2008 reply_doserror(req, ERRDOS, ERRnoaccess);
2009 return;
2013 * Ensure minimum number of parameters sent.
2016 if (parameter_count < 4) {
2017 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2018 reply_doserror(req, ERRDOS, ERRinvalidparam);
2019 return;
2022 /* maybe we can check the quota_fnum */
2023 fsp = file_fsp(SVAL(params,0));
2024 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2025 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2026 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2027 return;
2030 /* the NULL pointer checking for fsp->fake_file_handle->pd
2031 * is done by CHECK_NTQUOTA_HANDLE_OK()
2033 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2035 level = SVAL(params,2);
2037 /* unknown 12 bytes leading in params */
2039 switch (level) {
2040 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2041 /* seems that we should continue with the enum here --metze */
2043 if (qt_handle->quota_list!=NULL &&
2044 qt_handle->tmp_list==NULL) {
2046 /* free the list */
2047 free_ntquota_list(&(qt_handle->quota_list));
2049 /* Realloc the size of parameters and data we will return */
2050 param_len = 4;
2051 params = nttrans_realloc(ppparams, param_len);
2052 if(params == NULL) {
2053 reply_doserror(req, ERRDOS, ERRnomem);
2054 return;
2057 data_len = 0;
2058 SIVAL(params,0,data_len);
2060 break;
2063 start_enum = False;
2065 case TRANSACT_GET_USER_QUOTA_LIST_START:
2067 if (qt_handle->quota_list==NULL &&
2068 qt_handle->tmp_list==NULL) {
2069 start_enum = True;
2072 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2073 reply_doserror(req, ERRSRV, ERRerror);
2074 return;
2077 /* Realloc the size of parameters and data we will return */
2078 param_len = 4;
2079 params = nttrans_realloc(ppparams, param_len);
2080 if(params == NULL) {
2081 reply_doserror(req, ERRDOS, ERRnomem);
2082 return;
2085 /* we should not trust the value in max_data_count*/
2086 max_data_count = MIN(max_data_count,2048);
2088 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2089 if(pdata == NULL) {
2090 reply_doserror(req, ERRDOS, ERRnomem);
2091 return;
2094 entry = pdata;
2096 /* set params Size of returned Quota Data 4 bytes*/
2097 /* but set it later when we know it */
2099 /* for each entry push the data */
2101 if (start_enum) {
2102 qt_handle->tmp_list = qt_handle->quota_list;
2105 tmp_list = qt_handle->tmp_list;
2107 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2108 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2110 sid_len = ndr_size_dom_sid(
2111 &tmp_list->quotas->sid, 0);
2112 entry_len = 40 + sid_len;
2114 /* nextoffset entry 4 bytes */
2115 SIVAL(entry,0,entry_len);
2117 /* then the len of the SID 4 bytes */
2118 SIVAL(entry,4,sid_len);
2120 /* unknown data 8 bytes SMB_BIG_UINT */
2121 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2123 /* the used disk space 8 bytes SMB_BIG_UINT */
2124 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2126 /* the soft quotas 8 bytes SMB_BIG_UINT */
2127 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2129 /* the hard quotas 8 bytes SMB_BIG_UINT */
2130 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2132 /* and now the SID */
2133 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2136 qt_handle->tmp_list = tmp_list;
2138 /* overwrite the offset of the last entry */
2139 SIVAL(entry-entry_len,0,0);
2141 data_len = 4+qt_len;
2142 /* overwrite the params quota_data_len */
2143 SIVAL(params,0,data_len);
2145 break;
2147 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2149 /* unknown 4 bytes IVAL(pdata,0) */
2151 if (data_count < 8) {
2152 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2153 reply_doserror(req, ERRDOS, ERRunknownlevel);
2154 return;
2157 sid_len = IVAL(pdata,4);
2158 /* Ensure this is less than 1mb. */
2159 if (sid_len > (1024*1024)) {
2160 reply_doserror(req, ERRDOS, ERRnomem);
2161 return;
2164 if (data_count < 8+sid_len) {
2165 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2166 reply_doserror(req, ERRDOS, ERRunknownlevel);
2167 return;
2170 data_len = 4+40+sid_len;
2172 if (max_data_count < data_len) {
2173 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2174 max_data_count, data_len));
2175 param_len = 4;
2176 SIVAL(params,0,data_len);
2177 data_len = 0;
2178 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2179 break;
2182 sid_parse(pdata+8,sid_len,&sid);
2184 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2185 ZERO_STRUCT(qt);
2187 * we have to return zero's in all fields
2188 * instead of returning an error here
2189 * --metze
2193 /* Realloc the size of parameters and data we will return */
2194 param_len = 4;
2195 params = nttrans_realloc(ppparams, param_len);
2196 if(params == NULL) {
2197 reply_doserror(req, ERRDOS, ERRnomem);
2198 return;
2201 pdata = nttrans_realloc(ppdata, data_len);
2202 if(pdata == NULL) {
2203 reply_doserror(req, ERRDOS, ERRnomem);
2204 return;
2207 entry = pdata;
2209 /* set params Size of returned Quota Data 4 bytes*/
2210 SIVAL(params,0,data_len);
2212 /* nextoffset entry 4 bytes */
2213 SIVAL(entry,0,0);
2215 /* then the len of the SID 4 bytes */
2216 SIVAL(entry,4,sid_len);
2218 /* unknown data 8 bytes SMB_BIG_UINT */
2219 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2221 /* the used disk space 8 bytes SMB_BIG_UINT */
2222 SBIG_UINT(entry,16,qt.usedspace);
2224 /* the soft quotas 8 bytes SMB_BIG_UINT */
2225 SBIG_UINT(entry,24,qt.softlim);
2227 /* the hard quotas 8 bytes SMB_BIG_UINT */
2228 SBIG_UINT(entry,32,qt.hardlim);
2230 /* and now the SID */
2231 sid_linearize(entry+40, sid_len, &sid);
2233 break;
2235 default:
2236 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2237 reply_doserror(req, ERRSRV, ERRerror);
2238 return;
2239 break;
2242 send_nt_replies(conn, req, nt_status, params, param_len,
2243 pdata, data_len);
2246 /****************************************************************************
2247 Reply to set user quota
2248 ****************************************************************************/
2250 static void call_nt_transact_set_user_quota(connection_struct *conn,
2251 struct smb_request *req,
2252 uint16 **ppsetup,
2253 uint32 setup_count,
2254 char **ppparams,
2255 uint32 parameter_count,
2256 char **ppdata,
2257 uint32 data_count,
2258 uint32 max_data_count)
2260 char *params = *ppparams;
2261 char *pdata = *ppdata;
2262 int data_len=0,param_len=0;
2263 SMB_NTQUOTA_STRUCT qt;
2264 size_t sid_len;
2265 DOM_SID sid;
2266 files_struct *fsp = NULL;
2268 ZERO_STRUCT(qt);
2270 /* access check */
2271 if (conn->server_info->utok.uid != 0) {
2272 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2273 "[%s]\n", lp_servicename(SNUM(conn)),
2274 conn->server_info->unix_name));
2275 reply_doserror(req, ERRDOS, ERRnoaccess);
2276 return;
2280 * Ensure minimum number of parameters sent.
2283 if (parameter_count < 2) {
2284 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2285 reply_doserror(req, ERRDOS, ERRinvalidparam);
2286 return;
2289 /* maybe we can check the quota_fnum */
2290 fsp = file_fsp(SVAL(params,0));
2291 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2292 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2293 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2294 return;
2297 if (data_count < 40) {
2298 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2299 reply_doserror(req, ERRDOS, ERRunknownlevel);
2300 return;
2303 /* offset to next quota record.
2304 * 4 bytes IVAL(pdata,0)
2305 * unused here...
2308 /* sid len */
2309 sid_len = IVAL(pdata,4);
2311 if (data_count < 40+sid_len) {
2312 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2313 reply_doserror(req, ERRDOS, ERRunknownlevel);
2314 return;
2317 /* unknown 8 bytes in pdata
2318 * maybe its the change time in NTTIME
2321 /* the used space 8 bytes (SMB_BIG_UINT)*/
2322 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2323 #ifdef LARGE_SMB_OFF_T
2324 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2325 #else /* LARGE_SMB_OFF_T */
2326 if ((IVAL(pdata,20) != 0)&&
2327 ((qt.usedspace != 0xFFFFFFFF)||
2328 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2329 /* more than 32 bits? */
2330 reply_doserror(req, ERRDOS, ERRunknownlevel);
2331 return;
2333 #endif /* LARGE_SMB_OFF_T */
2335 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2336 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2337 #ifdef LARGE_SMB_OFF_T
2338 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2339 #else /* LARGE_SMB_OFF_T */
2340 if ((IVAL(pdata,28) != 0)&&
2341 ((qt.softlim != 0xFFFFFFFF)||
2342 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2343 /* more than 32 bits? */
2344 reply_doserror(req, ERRDOS, ERRunknownlevel);
2345 return;
2347 #endif /* LARGE_SMB_OFF_T */
2349 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2350 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2351 #ifdef LARGE_SMB_OFF_T
2352 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2353 #else /* LARGE_SMB_OFF_T */
2354 if ((IVAL(pdata,36) != 0)&&
2355 ((qt.hardlim != 0xFFFFFFFF)||
2356 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2357 /* more than 32 bits? */
2358 reply_doserror(req, ERRDOS, ERRunknownlevel);
2359 return;
2361 #endif /* LARGE_SMB_OFF_T */
2363 sid_parse(pdata+40,sid_len,&sid);
2364 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2366 /* 44 unknown bytes left... */
2368 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2369 reply_doserror(req, ERRSRV, ERRerror);
2370 return;
2373 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2374 pdata, data_len);
2376 #endif /* HAVE_SYS_QUOTAS */
2378 static void handle_nttrans(connection_struct *conn,
2379 struct trans_state *state,
2380 struct smb_request *req)
2382 if (Protocol >= PROTOCOL_NT1) {
2383 req->flags2 |= 0x40; /* IS_LONG_NAME */
2384 SSVAL(req->inbuf,smb_flg2,req->flags2);
2387 /* Now we must call the relevant NT_TRANS function */
2388 switch(state->call) {
2389 case NT_TRANSACT_CREATE:
2391 START_PROFILE(NT_transact_create);
2392 call_nt_transact_create(
2393 conn, req,
2394 &state->setup, state->setup_count,
2395 &state->param, state->total_param,
2396 &state->data, state->total_data,
2397 state->max_data_return);
2398 END_PROFILE(NT_transact_create);
2399 break;
2402 case NT_TRANSACT_IOCTL:
2404 START_PROFILE(NT_transact_ioctl);
2405 call_nt_transact_ioctl(
2406 conn, req,
2407 &state->setup, state->setup_count,
2408 &state->param, state->total_param,
2409 &state->data, state->total_data,
2410 state->max_data_return);
2411 END_PROFILE(NT_transact_ioctl);
2412 break;
2415 case NT_TRANSACT_SET_SECURITY_DESC:
2417 START_PROFILE(NT_transact_set_security_desc);
2418 call_nt_transact_set_security_desc(
2419 conn, req,
2420 &state->setup, state->setup_count,
2421 &state->param, state->total_param,
2422 &state->data, state->total_data,
2423 state->max_data_return);
2424 END_PROFILE(NT_transact_set_security_desc);
2425 break;
2428 case NT_TRANSACT_NOTIFY_CHANGE:
2430 START_PROFILE(NT_transact_notify_change);
2431 call_nt_transact_notify_change(
2432 conn, req,
2433 &state->setup, state->setup_count,
2434 &state->param, state->total_param,
2435 &state->data, state->total_data,
2436 state->max_data_return,
2437 state->max_param_return);
2438 END_PROFILE(NT_transact_notify_change);
2439 break;
2442 case NT_TRANSACT_RENAME:
2444 START_PROFILE(NT_transact_rename);
2445 call_nt_transact_rename(
2446 conn, req,
2447 &state->setup, state->setup_count,
2448 &state->param, state->total_param,
2449 &state->data, state->total_data,
2450 state->max_data_return);
2451 END_PROFILE(NT_transact_rename);
2452 break;
2455 case NT_TRANSACT_QUERY_SECURITY_DESC:
2457 START_PROFILE(NT_transact_query_security_desc);
2458 call_nt_transact_query_security_desc(
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_query_security_desc);
2465 break;
2468 #ifdef HAVE_SYS_QUOTAS
2469 case NT_TRANSACT_GET_USER_QUOTA:
2471 START_PROFILE(NT_transact_get_user_quota);
2472 call_nt_transact_get_user_quota(
2473 conn, req,
2474 &state->setup, state->setup_count,
2475 &state->param, state->total_param,
2476 &state->data, state->total_data,
2477 state->max_data_return);
2478 END_PROFILE(NT_transact_get_user_quota);
2479 break;
2482 case NT_TRANSACT_SET_USER_QUOTA:
2484 START_PROFILE(NT_transact_set_user_quota);
2485 call_nt_transact_set_user_quota(
2486 conn, req,
2487 &state->setup, state->setup_count,
2488 &state->param, state->total_param,
2489 &state->data, state->total_data,
2490 state->max_data_return);
2491 END_PROFILE(NT_transact_set_user_quota);
2492 break;
2494 #endif /* HAVE_SYS_QUOTAS */
2496 default:
2497 /* Error in request */
2498 DEBUG(0,("handle_nttrans: Unknown request %d in "
2499 "nttrans call\n", state->call));
2500 reply_doserror(req, ERRSRV, ERRerror);
2501 return;
2503 return;
2506 /****************************************************************************
2507 Reply to a SMBNTtrans.
2508 ****************************************************************************/
2510 void reply_nttrans(struct smb_request *req)
2512 connection_struct *conn = req->conn;
2513 uint32_t pscnt;
2514 uint32_t psoff;
2515 uint32_t dscnt;
2516 uint32_t dsoff;
2517 uint16 function_code;
2518 NTSTATUS result;
2519 struct trans_state *state;
2520 uint32_t size;
2521 uint32_t av_size;
2523 START_PROFILE(SMBnttrans);
2525 if (req->wct < 19) {
2526 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2527 END_PROFILE(SMBnttrans);
2528 return;
2531 size = smb_len(req->inbuf) + 4;
2532 av_size = smb_len(req->inbuf);
2533 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2534 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2535 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2536 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2537 function_code = SVAL(req->inbuf, smb_nt_Function);
2539 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2540 reply_doserror(req, ERRSRV, ERRaccess);
2541 END_PROFILE(SMBnttrans);
2542 return;
2545 result = allow_new_trans(conn->pending_trans, req->mid);
2546 if (!NT_STATUS_IS_OK(result)) {
2547 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2548 reply_nterror(req, result);
2549 END_PROFILE(SMBnttrans);
2550 return;
2553 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2554 reply_doserror(req, ERRSRV, ERRaccess);
2555 END_PROFILE(SMBnttrans);
2556 return;
2559 state->cmd = SMBnttrans;
2561 state->mid = req->mid;
2562 state->vuid = req->vuid;
2563 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2564 state->data = NULL;
2565 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2566 state->param = NULL;
2567 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2568 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2570 /* setup count is in *words* */
2571 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2572 state->setup = NULL;
2573 state->call = function_code;
2576 * All nttrans messages we handle have smb_wct == 19 +
2577 * state->setup_count. Ensure this is so as a sanity check.
2580 if(req->wct != 19 + (state->setup_count/2)) {
2581 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2582 req->wct, 19 + (state->setup_count/2)));
2583 goto bad_param;
2586 /* Don't allow more than 128mb for each value. */
2587 if ((state->total_data > (1024*1024*128)) ||
2588 (state->total_param > (1024*1024*128))) {
2589 reply_doserror(req, ERRDOS, ERRnomem);
2590 END_PROFILE(SMBnttrans);
2591 return;
2594 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2595 goto bad_param;
2597 if (state->total_data) {
2598 /* Can't use talloc here, the core routines do realloc on the
2599 * params and data. */
2600 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2601 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2602 "bytes !\n", (unsigned int)state->total_data));
2603 TALLOC_FREE(state);
2604 reply_doserror(req, ERRDOS, ERRnomem);
2605 END_PROFILE(SMBnttrans);
2606 return;
2609 if (dscnt > state->total_data ||
2610 dsoff+dscnt < dsoff) {
2611 goto bad_param;
2614 if (dsoff > av_size ||
2615 dscnt > av_size ||
2616 dsoff+dscnt > av_size) {
2617 goto bad_param;
2620 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2623 if (state->total_param) {
2624 /* Can't use talloc here, the core routines do realloc on the
2625 * params and data. */
2626 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2627 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2628 "bytes !\n", (unsigned int)state->total_param));
2629 SAFE_FREE(state->data);
2630 TALLOC_FREE(state);
2631 reply_doserror(req, ERRDOS, ERRnomem);
2632 END_PROFILE(SMBnttrans);
2633 return;
2636 if (pscnt > state->total_param ||
2637 psoff+pscnt < psoff) {
2638 goto bad_param;
2641 if (psoff > av_size ||
2642 pscnt > av_size ||
2643 psoff+pscnt > av_size) {
2644 goto bad_param;
2647 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2650 state->received_data = dscnt;
2651 state->received_param = pscnt;
2653 if(state->setup_count > 0) {
2654 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2655 state->setup_count));
2656 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2657 if (state->setup == NULL) {
2658 DEBUG(0,("reply_nttrans : Out of memory\n"));
2659 SAFE_FREE(state->data);
2660 SAFE_FREE(state->param);
2661 TALLOC_FREE(state);
2662 reply_doserror(req, ERRDOS, ERRnomem);
2663 END_PROFILE(SMBnttrans);
2664 return;
2667 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2668 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2669 goto bad_param;
2671 if (smb_nt_SetupStart + state->setup_count > size) {
2672 goto bad_param;
2675 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2676 state->setup_count);
2677 dump_data(10, (uint8 *)state->setup, state->setup_count);
2680 if ((state->received_data == state->total_data) &&
2681 (state->received_param == state->total_param)) {
2682 handle_nttrans(conn, state, req);
2683 SAFE_FREE(state->param);
2684 SAFE_FREE(state->data);
2685 TALLOC_FREE(state);
2686 END_PROFILE(SMBnttrans);
2687 return;
2690 DLIST_ADD(conn->pending_trans, state);
2692 /* We need to send an interim response then receive the rest
2693 of the parameter/data bytes */
2694 reply_outbuf(req, 0, 0);
2695 show_msg((char *)req->outbuf);
2696 END_PROFILE(SMBnttrans);
2697 return;
2699 bad_param:
2701 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2702 SAFE_FREE(state->data);
2703 SAFE_FREE(state->param);
2704 TALLOC_FREE(state);
2705 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2706 END_PROFILE(SMBnttrans);
2707 return;
2710 /****************************************************************************
2711 Reply to a SMBnttranss
2712 ****************************************************************************/
2714 void reply_nttranss(struct smb_request *req)
2716 connection_struct *conn = req->conn;
2717 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2718 struct trans_state *state;
2719 uint32_t av_size;
2720 uint32_t size;
2722 START_PROFILE(SMBnttranss);
2724 show_msg((char *)req->inbuf);
2726 if (req->wct < 18) {
2727 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2728 END_PROFILE(SMBnttranss);
2729 return;
2732 for (state = conn->pending_trans; state != NULL;
2733 state = state->next) {
2734 if (state->mid == req->mid) {
2735 break;
2739 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2740 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2741 END_PROFILE(SMBnttranss);
2742 return;
2745 /* Revise state->total_param and state->total_data in case they have
2746 changed downwards */
2747 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2748 < state->total_param) {
2749 state->total_param = IVAL(req->inbuf,
2750 smb_nts_TotalParameterCount);
2752 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2753 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2756 size = smb_len(req->inbuf) + 4;
2757 av_size = smb_len(req->inbuf);
2759 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2760 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2761 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2763 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2764 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2765 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2767 state->received_param += pcnt;
2768 state->received_data += dcnt;
2770 if ((state->received_data > state->total_data) ||
2771 (state->received_param > state->total_param))
2772 goto bad_param;
2774 if (pcnt) {
2775 if (pdisp > state->total_param ||
2776 pcnt > state->total_param ||
2777 pdisp+pcnt > state->total_param ||
2778 pdisp+pcnt < pdisp) {
2779 goto bad_param;
2782 if (poff > av_size ||
2783 pcnt > av_size ||
2784 poff+pcnt > av_size ||
2785 poff+pcnt < poff) {
2786 goto bad_param;
2789 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2790 pcnt);
2793 if (dcnt) {
2794 if (ddisp > state->total_data ||
2795 dcnt > state->total_data ||
2796 ddisp+dcnt > state->total_data ||
2797 ddisp+dcnt < ddisp) {
2798 goto bad_param;
2801 if (ddisp > av_size ||
2802 dcnt > av_size ||
2803 ddisp+dcnt > av_size ||
2804 ddisp+dcnt < ddisp) {
2805 goto bad_param;
2808 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2809 dcnt);
2812 if ((state->received_param < state->total_param) ||
2813 (state->received_data < state->total_data)) {
2814 END_PROFILE(SMBnttranss);
2815 return;
2819 * construct_reply_common will copy smb_com from inbuf to
2820 * outbuf. SMBnttranss is wrong here.
2822 SCVAL(req->inbuf,smb_com,SMBnttrans);
2824 handle_nttrans(conn, state, req);
2826 DLIST_REMOVE(conn->pending_trans, state);
2827 SAFE_FREE(state->data);
2828 SAFE_FREE(state->param);
2829 TALLOC_FREE(state);
2830 END_PROFILE(SMBnttranss);
2831 return;
2833 bad_param:
2835 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2836 DLIST_REMOVE(conn->pending_trans, state);
2837 SAFE_FREE(state->data);
2838 SAFE_FREE(state->param);
2839 TALLOC_FREE(state);
2840 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2841 END_PROFILE(SMBnttranss);
2842 return;