The compatibility function also should have a const buffer pointer
[Samba/gbeck.git] / source3 / smbd / nttrans.c
blob12f4da627bec1992b5d40f5c16afdaf29bbb1c03
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 const char *known_nt_pipes[] = {
27 "\\LANMAN",
28 "\\srvsvc",
29 "\\samr",
30 "\\wkssvc",
31 "\\NETLOGON",
32 "\\ntlsa",
33 "\\ntsvcs",
34 "\\lsass",
35 "\\lsarpc",
36 "\\winreg",
37 "\\initshutdown",
38 "\\spoolss",
39 "\\netdfs",
40 "\\rpcecho",
41 "\\svcctl",
42 "\\eventlog",
43 "\\unixinfo",
44 NULL
47 static char *nttrans_realloc(char **ptr, size_t size)
49 if (ptr==NULL) {
50 smb_panic("nttrans_realloc() called with NULL ptr");
53 *ptr = (char *)SMB_REALLOC(*ptr, size);
54 if(*ptr == NULL) {
55 return NULL;
57 memset(*ptr,'\0',size);
58 return *ptr;
61 /****************************************************************************
62 Send the required number of replies back.
63 We assume all fields other than the data fields are
64 set correctly for the type of call.
65 HACK ! Always assumes smb_setup field is zero.
66 ****************************************************************************/
68 void send_nt_replies(connection_struct *conn,
69 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 (!srv_send_smb(smbd_server_fd(),
246 (char *)req->outbuf,
247 IS_CONN_ENCRYPTED(conn))) {
248 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
251 TALLOC_FREE(req->outbuf);
253 pp += params_sent_thistime;
254 pd += data_sent_thistime;
256 params_to_send -= params_sent_thistime;
257 data_to_send -= data_sent_thistime;
260 * Sanity check
263 if(params_to_send < 0 || data_to_send < 0) {
264 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
265 params_to_send, data_to_send));
266 return;
271 /****************************************************************************
272 Is it an NTFS stream name ?
273 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
274 $DATA can be used as both a stream name and a stream type. A missing stream
275 name or type implies $DATA.
276 ****************************************************************************/
278 bool is_ntfs_stream_name(const char *fname)
280 if (lp_posix_pathnames()) {
281 return False;
283 return (strchr_m(fname, ':') != NULL) ? True : False;
286 /****************************************************************************
287 Reply to an NT create and X call on a pipe
288 ****************************************************************************/
290 static void nt_open_pipe(char *fname, connection_struct *conn,
291 struct smb_request *req, int *ppnum)
293 smb_np_struct *p = NULL;
294 int i;
296 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
298 /* See if it is one we want to handle. */
300 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
301 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
302 ERRDOS, ERRbadpipe);
303 return;
306 for( i = 0; known_nt_pipes[i]; i++ ) {
307 if( strequal(fname,known_nt_pipes[i])) {
308 break;
312 if ( known_nt_pipes[i] == NULL ) {
313 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
314 ERRDOS, ERRbadpipe);
315 return;
318 /* Strip \\ off the name. */
319 fname++;
321 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
323 p = open_rpc_pipe_p(fname, conn, req->vuid);
324 if (!p) {
325 reply_doserror(req, ERRSRV, ERRnofids);
326 return;
329 /* TODO: Add pipe to db */
331 if ( !store_pipe_opendb( p ) ) {
332 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
335 *ppnum = p->pnum;
336 return;
339 /****************************************************************************
340 Reply to an NT create and X call for pipes.
341 ****************************************************************************/
343 static void do_ntcreate_pipe_open(connection_struct *conn,
344 struct smb_request *req)
346 char *fname = NULL;
347 int pnum = -1;
348 char *p = NULL;
349 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
350 TALLOC_CTX *ctx = talloc_tos();
352 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
353 smb_buf(req->inbuf), STR_TERMINATE);
355 if (!fname) {
356 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
357 ERRDOS, ERRbadpipe);
358 return;
360 nt_open_pipe(fname, conn, req, &pnum);
362 if (req->outbuf) {
363 /* error reply */
364 return;
368 * Deal with pipe return.
371 if (flags & EXTENDED_RESPONSE_REQUIRED) {
372 /* This is very strange. We
373 * return 50 words, but only set
374 * the wcnt to 42 ? It's definately
375 * what happens on the wire....
377 reply_outbuf(req, 50, 0);
378 SCVAL(req->outbuf,smb_wct,42);
379 } else {
380 reply_outbuf(req, 34, 0);
383 p = (char *)req->outbuf + smb_vwv2;
384 p++;
385 SSVAL(p,0,pnum);
386 p += 2;
387 SIVAL(p,0,FILE_WAS_OPENED);
388 p += 4;
389 p += 32;
390 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
391 p += 20;
392 /* File type. */
393 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
394 /* Device state. */
395 SSVAL(p,2, 0x5FF); /* ? */
396 p += 4;
398 if (flags & EXTENDED_RESPONSE_REQUIRED) {
399 p += 25;
400 SIVAL(p,0,FILE_GENERIC_ALL);
402 * For pipes W2K3 seems to return
403 * 0x12019B next.
404 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
406 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
409 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
411 chain_reply(req);
414 /****************************************************************************
415 Reply to an NT create and X call.
416 ****************************************************************************/
418 void reply_ntcreate_and_X(struct smb_request *req)
420 connection_struct *conn = req->conn;
421 char *fname = NULL;
422 uint32 flags;
423 uint32 access_mask;
424 uint32 file_attributes;
425 uint32 share_access;
426 uint32 create_disposition;
427 uint32 create_options;
428 uint16 root_dir_fid;
429 SMB_BIG_UINT allocation_size;
430 /* Breakout the oplock request bits so we can set the
431 reply bits separately. */
432 uint32 fattr=0;
433 SMB_OFF_T file_len = 0;
434 SMB_STRUCT_STAT sbuf;
435 int info = 0;
436 files_struct *fsp = NULL;
437 char *p = NULL;
438 struct timespec c_timespec;
439 struct timespec a_timespec;
440 struct timespec m_timespec;
441 NTSTATUS status;
442 int oplock_request;
443 uint8_t oplock_granted = NO_OPLOCK_RETURN;
444 TALLOC_CTX *ctx = talloc_tos();
446 START_PROFILE(SMBntcreateX);
448 if (req->wct < 24) {
449 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
450 return;
453 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
454 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
455 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
456 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
457 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
458 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
459 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
461 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
462 smb_ntcreate_AllocationSize);
463 #ifdef LARGE_SMB_OFF_T
464 allocation_size |= (((SMB_BIG_UINT)IVAL(
465 req->inbuf,
466 smb_ntcreate_AllocationSize + 4)) << 32);
467 #endif
469 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
470 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
472 if (!NT_STATUS_IS_OK(status)) {
473 reply_nterror(req, status);
474 END_PROFILE(SMBntcreateX);
475 return;
478 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
479 "file_attributes = 0x%x, share_access = 0x%x, "
480 "create_disposition = 0x%x create_options = 0x%x "
481 "root_dir_fid = 0x%x, fname = %s\n",
482 (unsigned int)flags,
483 (unsigned int)access_mask,
484 (unsigned int)file_attributes,
485 (unsigned int)share_access,
486 (unsigned int)create_disposition,
487 (unsigned int)create_options,
488 (unsigned int)root_dir_fid,
489 fname));
492 * If it's an IPC, use the pipe handler.
495 if (IS_IPC(conn)) {
496 if (lp_nt_pipe_support()) {
497 do_ntcreate_pipe_open(conn, req);
498 END_PROFILE(SMBntcreateX);
499 return;
501 reply_doserror(req, ERRDOS, ERRnoaccess);
502 END_PROFILE(SMBntcreateX);
503 return;
506 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
507 if (oplock_request) {
508 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
509 ? BATCH_OPLOCK : 0;
512 status = create_file(conn, req, root_dir_fid, fname,
513 access_mask, share_access, create_disposition,
514 create_options, file_attributes, oplock_request,
515 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
517 if (!NT_STATUS_IS_OK(status)) {
518 if (open_was_deferred(req->mid)) {
519 /* We have re-scheduled this call, no error. */
520 END_PROFILE(SMBntcreateX);
521 return;
523 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
524 reply_botherror(req, status, ERRDOS, ERRfilexists);
526 else {
527 reply_nterror(req, status);
529 END_PROFILE(SMBntcreateX);
530 return;
534 * If the caller set the extended oplock request bit
535 * and we granted one (by whatever means) - set the
536 * correct bit for extended oplock reply.
539 if (oplock_request &&
540 (lp_fake_oplocks(SNUM(conn))
541 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
544 * Exclusive oplock granted
547 if (flags & REQUEST_BATCH_OPLOCK) {
548 oplock_granted = BATCH_OPLOCK_RETURN;
549 } else {
550 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
552 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
553 oplock_granted = LEVEL_II_OPLOCK_RETURN;
554 } else {
555 oplock_granted = NO_OPLOCK_RETURN;
558 file_len = sbuf.st_size;
559 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
560 if (fattr == 0) {
561 fattr = FILE_ATTRIBUTE_NORMAL;
564 if (flags & EXTENDED_RESPONSE_REQUIRED) {
565 /* This is very strange. We
566 * return 50 words, but only set
567 * the wcnt to 42 ? It's definately
568 * what happens on the wire....
570 reply_outbuf(req, 50, 0);
571 SCVAL(req->outbuf,smb_wct,42);
572 } else {
573 reply_outbuf(req, 34, 0);
576 p = (char *)req->outbuf + smb_vwv2;
578 SCVAL(p, 0, oplock_granted);
580 p++;
581 SSVAL(p,0,fsp->fnum);
582 p += 2;
583 if ((create_disposition == FILE_SUPERSEDE)
584 && (info == FILE_WAS_OVERWRITTEN)) {
585 SIVAL(p,0,FILE_WAS_SUPERSEDED);
586 } else {
587 SIVAL(p,0,info);
589 p += 4;
591 /* Create time. */
592 c_timespec = get_create_timespec(
593 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
594 a_timespec = get_atimespec(&sbuf);
595 m_timespec = get_mtimespec(&sbuf);
597 if (lp_dos_filetime_resolution(SNUM(conn))) {
598 dos_filetime_timespec(&c_timespec);
599 dos_filetime_timespec(&a_timespec);
600 dos_filetime_timespec(&m_timespec);
603 put_long_date_timespec(p, c_timespec); /* create time. */
604 p += 8;
605 put_long_date_timespec(p, a_timespec); /* access time */
606 p += 8;
607 put_long_date_timespec(p, m_timespec); /* write time */
608 p += 8;
609 put_long_date_timespec(p, m_timespec); /* change time */
610 p += 8;
611 SIVAL(p,0,fattr); /* File Attributes. */
612 p += 4;
613 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
614 p += 8;
615 SOFF_T(p,0,file_len);
616 p += 8;
617 if (flags & EXTENDED_RESPONSE_REQUIRED) {
618 SSVAL(p,2,0x7);
620 p += 4;
621 SCVAL(p,0,fsp->is_directory ? 1 : 0);
623 if (flags & EXTENDED_RESPONSE_REQUIRED) {
624 uint32 perms = 0;
625 p += 25;
626 if (fsp->is_directory
627 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
628 perms = FILE_GENERIC_ALL;
629 } else {
630 perms = FILE_GENERIC_READ|FILE_EXECUTE;
632 SIVAL(p,0,perms);
635 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
636 fsp->fnum, fsp->fsp_name));
638 chain_reply(req);
639 END_PROFILE(SMBntcreateX);
640 return;
643 /****************************************************************************
644 Reply to a NT_TRANSACT_CREATE call to open a pipe.
645 ****************************************************************************/
647 static void do_nt_transact_create_pipe(connection_struct *conn,
648 struct smb_request *req,
649 uint16 **ppsetup, uint32 setup_count,
650 char **ppparams, uint32 parameter_count,
651 char **ppdata, uint32 data_count)
653 char *fname = NULL;
654 char *params = *ppparams;
655 int pnum = -1;
656 char *p = NULL;
657 NTSTATUS status;
658 size_t param_len;
659 uint32 flags;
660 TALLOC_CTX *ctx = talloc_tos();
663 * Ensure minimum number of parameters sent.
666 if(parameter_count < 54) {
667 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
668 reply_doserror(req, ERRDOS, ERRnoaccess);
669 return;
672 flags = IVAL(params,0);
674 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
675 parameter_count-53, STR_TERMINATE,
676 &status);
677 if (!NT_STATUS_IS_OK(status)) {
678 reply_nterror(req, status);
679 return;
682 nt_open_pipe(fname, conn, req, &pnum);
684 if (req->outbuf) {
685 /* Error return */
686 return;
689 /* Realloc the size of parameters and data we will return */
690 if (flags & EXTENDED_RESPONSE_REQUIRED) {
691 /* Extended response is 32 more byyes. */
692 param_len = 101;
693 } else {
694 param_len = 69;
696 params = nttrans_realloc(ppparams, param_len);
697 if(params == NULL) {
698 reply_doserror(req, ERRDOS, ERRnomem);
699 return;
702 p = params;
703 SCVAL(p,0,NO_OPLOCK_RETURN);
705 p += 2;
706 SSVAL(p,0,pnum);
707 p += 2;
708 SIVAL(p,0,FILE_WAS_OPENED);
709 p += 8;
711 p += 32;
712 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
713 p += 20;
714 /* File type. */
715 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
716 /* Device state. */
717 SSVAL(p,2, 0x5FF); /* ? */
718 p += 4;
720 if (flags & EXTENDED_RESPONSE_REQUIRED) {
721 p += 25;
722 SIVAL(p,0,FILE_GENERIC_ALL);
724 * For pipes W2K3 seems to return
725 * 0x12019B next.
726 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
728 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
731 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
733 /* Send the required number of replies */
734 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
736 return;
739 /****************************************************************************
740 Internal fn to set security descriptors.
741 ****************************************************************************/
743 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
744 uint32 security_info_sent)
746 SEC_DESC *psd = NULL;
747 NTSTATUS status;
749 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
750 return NT_STATUS_OK;
753 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
755 if (!NT_STATUS_IS_OK(status)) {
756 return status;
759 if (psd->owner_sid==0) {
760 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
762 if (psd->group_sid==0) {
763 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
765 if (psd->sacl==0) {
766 security_info_sent &= ~SACL_SECURITY_INFORMATION;
768 if (psd->dacl==0) {
769 security_info_sent &= ~DACL_SECURITY_INFORMATION;
772 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
774 TALLOC_FREE(psd);
776 return status;
779 /****************************************************************************
780 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
781 ****************************************************************************/
783 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
785 struct ea_list *ea_list_head = NULL;
786 size_t offset = 0;
788 if (data_size < 4) {
789 return NULL;
792 while (offset + 4 <= data_size) {
793 size_t next_offset = IVAL(pdata,offset);
794 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
796 if (!eal) {
797 return NULL;
800 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
801 if (next_offset == 0) {
802 break;
804 offset += next_offset;
807 return ea_list_head;
810 /****************************************************************************
811 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
812 ****************************************************************************/
814 static void call_nt_transact_create(connection_struct *conn,
815 struct smb_request *req,
816 uint16 **ppsetup, uint32 setup_count,
817 char **ppparams, uint32 parameter_count,
818 char **ppdata, uint32 data_count,
819 uint32 max_data_count)
821 char *fname = NULL;
822 char *params = *ppparams;
823 char *data = *ppdata;
824 /* Breakout the oplock request bits so we can set the reply bits separately. */
825 uint32 fattr=0;
826 SMB_OFF_T file_len = 0;
827 SMB_STRUCT_STAT sbuf;
828 int info = 0;
829 files_struct *fsp = NULL;
830 char *p = NULL;
831 uint32 flags;
832 uint32 access_mask;
833 uint32 file_attributes;
834 uint32 share_access;
835 uint32 create_disposition;
836 uint32 create_options;
837 uint32 sd_len;
838 struct security_descriptor *sd = NULL;
839 uint32 ea_len;
840 uint16 root_dir_fid;
841 struct timespec c_timespec;
842 struct timespec a_timespec;
843 struct timespec m_timespec;
844 struct ea_list *ea_list = NULL;
845 NTSTATUS status;
846 size_t param_len;
847 SMB_BIG_UINT allocation_size;
848 int oplock_request;
849 uint8_t oplock_granted;
850 TALLOC_CTX *ctx = talloc_tos();
852 DEBUG(5,("call_nt_transact_create\n"));
855 * If it's an IPC, use the pipe handler.
858 if (IS_IPC(conn)) {
859 if (lp_nt_pipe_support()) {
860 do_nt_transact_create_pipe(
861 conn, req,
862 ppsetup, setup_count,
863 ppparams, parameter_count,
864 ppdata, data_count);
865 return;
867 reply_doserror(req, ERRDOS, ERRnoaccess);
868 return;
872 * Ensure minimum number of parameters sent.
875 if(parameter_count < 54) {
876 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
877 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
878 return;
881 flags = IVAL(params,0);
882 access_mask = IVAL(params,8);
883 file_attributes = IVAL(params,20);
884 share_access = IVAL(params,24);
885 create_disposition = IVAL(params,28);
886 create_options = IVAL(params,32);
887 sd_len = IVAL(params,36);
888 ea_len = IVAL(params,40);
889 root_dir_fid = (uint16)IVAL(params,4);
890 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
891 #ifdef LARGE_SMB_OFF_T
892 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
893 #endif
895 /* Ensure the data_len is correct for the sd and ea values given. */
896 if ((ea_len + sd_len > data_count)
897 || (ea_len > data_count) || (sd_len > data_count)
898 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
899 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
900 "%u, data_count = %u\n", (unsigned int)ea_len,
901 (unsigned int)sd_len, (unsigned int)data_count));
902 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
903 return;
906 if (sd_len) {
907 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
908 sd_len));
910 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
911 &sd);
912 if (!NT_STATUS_IS_OK(status)) {
913 DEBUG(10, ("call_nt_transact_create: "
914 "unmarshall_sec_desc failed: %s\n",
915 nt_errstr(status)));
916 reply_nterror(req, status);
917 return;
921 if (ea_len) {
922 if (!lp_ea_support(SNUM(conn))) {
923 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
924 "EA's not supported.\n",
925 (unsigned int)ea_len));
926 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
927 return;
930 if (ea_len < 10) {
931 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
932 "too small (should be more than 10)\n",
933 (unsigned int)ea_len ));
934 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
935 return;
938 /* We have already checked that ea_len <= data_count here. */
939 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
940 ea_len);
941 if (ea_list == NULL) {
942 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
943 return;
947 srvstr_get_path(ctx, params, req->flags2, &fname,
948 params+53, parameter_count-53,
949 STR_TERMINATE, &status);
950 if (!NT_STATUS_IS_OK(status)) {
951 reply_nterror(req, status);
952 return;
955 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
956 if (oplock_request) {
957 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
958 ? BATCH_OPLOCK : 0;
961 status = create_file(conn, req, root_dir_fid, fname,
962 access_mask, share_access, create_disposition,
963 create_options, file_attributes, oplock_request,
964 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
966 if(!NT_STATUS_IS_OK(status)) {
967 if (open_was_deferred(req->mid)) {
968 /* We have re-scheduled this call, no error. */
969 return;
971 reply_openerror(req, status);
972 return;
976 * If the caller set the extended oplock request bit
977 * and we granted one (by whatever means) - set the
978 * correct bit for extended oplock reply.
981 if (oplock_request &&
982 (lp_fake_oplocks(SNUM(conn))
983 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
986 * Exclusive oplock granted
989 if (flags & REQUEST_BATCH_OPLOCK) {
990 oplock_granted = BATCH_OPLOCK_RETURN;
991 } else {
992 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
994 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
995 oplock_granted = LEVEL_II_OPLOCK_RETURN;
996 } else {
997 oplock_granted = NO_OPLOCK_RETURN;
1000 file_len = sbuf.st_size;
1001 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1002 if (fattr == 0) {
1003 fattr = FILE_ATTRIBUTE_NORMAL;
1006 /* Realloc the size of parameters and data we will return */
1007 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1008 /* Extended response is 32 more byyes. */
1009 param_len = 101;
1010 } else {
1011 param_len = 69;
1013 params = nttrans_realloc(ppparams, param_len);
1014 if(params == NULL) {
1015 reply_doserror(req, ERRDOS, ERRnomem);
1016 return;
1019 p = params;
1020 SCVAL(p, 0, oplock_granted);
1022 p += 2;
1023 SSVAL(p,0,fsp->fnum);
1024 p += 2;
1025 if ((create_disposition == FILE_SUPERSEDE)
1026 && (info == FILE_WAS_OVERWRITTEN)) {
1027 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1028 } else {
1029 SIVAL(p,0,info);
1031 p += 8;
1033 /* Create time. */
1034 c_timespec = get_create_timespec(
1035 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1036 a_timespec = get_atimespec(&sbuf);
1037 m_timespec = get_mtimespec(&sbuf);
1039 if (lp_dos_filetime_resolution(SNUM(conn))) {
1040 dos_filetime_timespec(&c_timespec);
1041 dos_filetime_timespec(&a_timespec);
1042 dos_filetime_timespec(&m_timespec);
1045 put_long_date_timespec(p, c_timespec); /* create time. */
1046 p += 8;
1047 put_long_date_timespec(p, a_timespec); /* access time */
1048 p += 8;
1049 put_long_date_timespec(p, m_timespec); /* write time */
1050 p += 8;
1051 put_long_date_timespec(p, m_timespec); /* change time */
1052 p += 8;
1053 SIVAL(p,0,fattr); /* File Attributes. */
1054 p += 4;
1055 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1056 p += 8;
1057 SOFF_T(p,0,file_len);
1058 p += 8;
1059 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1060 SSVAL(p,2,0x7);
1062 p += 4;
1063 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1065 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1066 uint32 perms = 0;
1067 p += 25;
1068 if (fsp->is_directory
1069 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1070 perms = FILE_GENERIC_ALL;
1071 } else {
1072 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1074 SIVAL(p,0,perms);
1077 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1079 /* Send the required number of replies */
1080 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1082 return;
1085 /****************************************************************************
1086 Reply to a NT CANCEL request.
1087 conn POINTER CAN BE NULL HERE !
1088 ****************************************************************************/
1090 void reply_ntcancel(struct smb_request *req)
1093 * Go through and cancel any pending change notifies.
1096 START_PROFILE(SMBntcancel);
1097 remove_pending_change_notify_requests_by_mid(req->mid);
1098 remove_pending_lock_requests_by_mid(req->mid);
1099 srv_cancel_sign_response(req->mid);
1101 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1103 END_PROFILE(SMBntcancel);
1104 return;
1107 /****************************************************************************
1108 Copy a file.
1109 ****************************************************************************/
1111 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1112 connection_struct *conn,
1113 struct smb_request *req,
1114 const char *oldname_in,
1115 const char *newname_in,
1116 uint32 attrs)
1118 SMB_STRUCT_STAT sbuf1, sbuf2;
1119 char *oldname = NULL;
1120 char *newname = NULL;
1121 char *last_component_oldname = NULL;
1122 char *last_component_newname = NULL;
1123 files_struct *fsp1,*fsp2;
1124 uint32 fattr;
1125 int info;
1126 SMB_OFF_T ret=-1;
1127 NTSTATUS status = NT_STATUS_OK;
1129 ZERO_STRUCT(sbuf1);
1130 ZERO_STRUCT(sbuf2);
1132 if (!CAN_WRITE(conn)) {
1133 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1136 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1137 &last_component_oldname, &sbuf1);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 return status;
1142 status = check_name(conn, oldname);
1143 if (!NT_STATUS_IS_OK(status)) {
1144 return status;
1147 /* Source must already exist. */
1148 if (!VALID_STAT(sbuf1)) {
1149 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1151 /* Ensure attributes match. */
1152 fattr = dos_mode(conn,oldname,&sbuf1);
1153 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1154 return NT_STATUS_NO_SUCH_FILE;
1157 status = unix_convert(ctx, conn, newname_in, False, &newname,
1158 &last_component_newname, &sbuf2);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 return status;
1163 status = check_name(conn, newname);
1164 if (!NT_STATUS_IS_OK(status)) {
1165 return status;
1168 /* Disallow if newname already exists. */
1169 if (VALID_STAT(sbuf2)) {
1170 return NT_STATUS_OBJECT_NAME_COLLISION;
1173 /* No links from a directory. */
1174 if (S_ISDIR(sbuf1.st_mode)) {
1175 return NT_STATUS_FILE_IS_A_DIRECTORY;
1178 /* Ensure this is within the share. */
1179 status = check_reduced_name(conn, oldname);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 return status;
1184 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1185 oldname, newname));
1187 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1188 FILE_READ_DATA, /* Read-only. */
1189 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1190 FILE_OPEN,
1191 0, /* No create options. */
1192 FILE_ATTRIBUTE_NORMAL,
1193 NO_OPLOCK,
1194 &info, &fsp1);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 return status;
1200 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1201 FILE_WRITE_DATA, /* Read-only. */
1202 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1203 FILE_CREATE,
1204 0, /* No create options. */
1205 fattr,
1206 NO_OPLOCK,
1207 &info, &fsp2);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 close_file(fsp1,ERROR_CLOSE);
1211 return status;
1214 if (sbuf1.st_size) {
1215 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1219 * As we are opening fsp1 read-only we only expect
1220 * an error on close on fsp2 if we are out of space.
1221 * Thus we don't look at the error return from the
1222 * close of fsp1.
1224 close_file(fsp1,NORMAL_CLOSE);
1226 /* Ensure the modtime is set correctly on the destination file. */
1227 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1229 status = close_file(fsp2,NORMAL_CLOSE);
1231 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1232 creates the file. This isn't the correct thing to do in the copy
1233 case. JRA */
1234 file_set_dosmode(conn, newname, fattr, &sbuf2,
1235 parent_dirname(newname),false);
1237 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1238 return NT_STATUS_DISK_FULL;
1241 if (!NT_STATUS_IS_OK(status)) {
1242 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1243 nt_errstr(status), oldname, newname));
1245 return status;
1248 /****************************************************************************
1249 Reply to a NT rename request.
1250 ****************************************************************************/
1252 void reply_ntrename(struct smb_request *req)
1254 connection_struct *conn = req->conn;
1255 char *oldname = NULL;
1256 char *newname = NULL;
1257 char *p;
1258 NTSTATUS status;
1259 bool src_has_wcard = False;
1260 bool dest_has_wcard = False;
1261 uint32 attrs;
1262 uint16 rename_type;
1263 TALLOC_CTX *ctx = talloc_tos();
1265 START_PROFILE(SMBntrename);
1267 if (req->wct < 4) {
1268 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1269 END_PROFILE(SMBntrename);
1270 return;
1273 attrs = SVAL(req->inbuf,smb_vwv0);
1274 rename_type = SVAL(req->inbuf,smb_vwv1);
1276 p = smb_buf(req->inbuf) + 1;
1277 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1278 0, STR_TERMINATE, &status,
1279 &src_has_wcard);
1280 if (!NT_STATUS_IS_OK(status)) {
1281 reply_nterror(req, status);
1282 END_PROFILE(SMBntrename);
1283 return;
1286 if( is_ntfs_stream_name(oldname)) {
1287 /* Can't rename a stream. */
1288 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1289 END_PROFILE(SMBntrename);
1290 return;
1293 if (ms_has_wild(oldname)) {
1294 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1295 END_PROFILE(SMBntrename);
1296 return;
1299 p++;
1300 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1301 0, STR_TERMINATE, &status,
1302 &dest_has_wcard);
1303 if (!NT_STATUS_IS_OK(status)) {
1304 reply_nterror(req, status);
1305 END_PROFILE(SMBntrename);
1306 return;
1309 status = resolve_dfspath(ctx, conn,
1310 req->flags2 & FLAGS2_DFS_PATHNAMES,
1311 oldname,
1312 &oldname);
1313 if (!NT_STATUS_IS_OK(status)) {
1314 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1315 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1316 ERRSRV, ERRbadpath);
1317 END_PROFILE(SMBntrename);
1318 return;
1320 reply_nterror(req, status);
1321 END_PROFILE(SMBntrename);
1322 return;
1325 status = resolve_dfspath(ctx, conn,
1326 req->flags2 & FLAGS2_DFS_PATHNAMES,
1327 newname,
1328 &newname);
1329 if (!NT_STATUS_IS_OK(status)) {
1330 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1331 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1332 ERRSRV, ERRbadpath);
1333 END_PROFILE(SMBntrename);
1334 return;
1336 reply_nterror(req, status);
1337 END_PROFILE(SMBntrename);
1338 return;
1341 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1343 switch(rename_type) {
1344 case RENAME_FLAG_RENAME:
1345 status = rename_internals(ctx, conn, req, oldname,
1346 newname, attrs, False, src_has_wcard,
1347 dest_has_wcard, DELETE_ACCESS);
1348 break;
1349 case RENAME_FLAG_HARD_LINK:
1350 if (src_has_wcard || dest_has_wcard) {
1351 /* No wildcards. */
1352 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1353 } else {
1354 status = hardlink_internals(ctx,
1355 conn,
1356 oldname,
1357 newname);
1359 break;
1360 case RENAME_FLAG_COPY:
1361 if (src_has_wcard || dest_has_wcard) {
1362 /* No wildcards. */
1363 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1364 } else {
1365 status = copy_internals(ctx, conn, req, oldname,
1366 newname, attrs);
1368 break;
1369 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1370 status = NT_STATUS_INVALID_PARAMETER;
1371 break;
1372 default:
1373 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1374 break;
1377 if (!NT_STATUS_IS_OK(status)) {
1378 if (open_was_deferred(req->mid)) {
1379 /* We have re-scheduled this call. */
1380 END_PROFILE(SMBntrename);
1381 return;
1384 reply_nterror(req, status);
1385 END_PROFILE(SMBntrename);
1386 return;
1389 reply_outbuf(req, 0, 0);
1391 END_PROFILE(SMBntrename);
1392 return;
1395 /****************************************************************************
1396 Reply to a notify change - queue the request and
1397 don't allow a directory to be opened.
1398 ****************************************************************************/
1400 static void call_nt_transact_notify_change(connection_struct *conn,
1401 struct smb_request *req,
1402 uint16 **ppsetup,
1403 uint32 setup_count,
1404 char **ppparams,
1405 uint32 parameter_count,
1406 char **ppdata, uint32 data_count,
1407 uint32 max_data_count,
1408 uint32 max_param_count)
1410 uint16 *setup = *ppsetup;
1411 files_struct *fsp;
1412 uint32 filter;
1413 NTSTATUS status;
1414 bool recursive;
1416 if(setup_count < 6) {
1417 reply_doserror(req, ERRDOS, ERRbadfunc);
1418 return;
1421 fsp = file_fsp(SVAL(setup,4));
1422 filter = IVAL(setup, 0);
1423 recursive = (SVAL(setup, 6) != 0) ? True : False;
1425 DEBUG(3,("call_nt_transact_notify_change\n"));
1427 if(!fsp) {
1428 reply_doserror(req, ERRDOS, ERRbadfid);
1429 return;
1433 char *filter_string;
1435 if (!(filter_string = notify_filter_string(NULL, filter))) {
1436 reply_nterror(req,NT_STATUS_NO_MEMORY);
1437 return;
1440 DEBUG(3,("call_nt_transact_notify_change: notify change "
1441 "called on %s, filter = %s, recursive = %d\n",
1442 fsp->fsp_name, filter_string, recursive));
1444 TALLOC_FREE(filter_string);
1447 if((!fsp->is_directory) || (conn != fsp->conn)) {
1448 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1449 return;
1452 if (fsp->notify == NULL) {
1454 status = change_notify_create(fsp, filter, recursive);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 DEBUG(10, ("change_notify_create returned %s\n",
1458 nt_errstr(status)));
1459 reply_nterror(req, status);
1460 return;
1464 if (fsp->notify->num_changes != 0) {
1467 * We've got changes pending, respond immediately
1471 * TODO: write a torture test to check the filtering behaviour
1472 * here.
1475 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1478 * change_notify_reply() above has independently sent its
1479 * results
1481 return;
1485 * No changes pending, queue the request
1488 status = change_notify_add_request(req,
1489 max_param_count,
1490 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 dest_has_wcard = False;
1513 NTSTATUS status;
1514 TALLOC_CTX *ctx = talloc_tos();
1516 if(parameter_count < 5) {
1517 reply_doserror(req, ERRDOS, ERRbadfunc);
1518 return;
1521 fsp = file_fsp(SVAL(params, 0));
1522 if (!check_fsp(conn, req, fsp)) {
1523 return;
1525 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1526 parameter_count - 4,
1527 STR_TERMINATE, &status, &dest_has_wcard);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 reply_nterror(req, status);
1530 return;
1534 * W2K3 ignores this request as the RAW-RENAME test
1535 * demonstrates, so we do.
1537 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1539 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1540 fsp->fsp_name, new_name));
1542 return;
1545 /******************************************************************************
1546 Fake up a completely empty SD.
1547 *******************************************************************************/
1549 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1551 size_t sd_size;
1553 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1554 if(!*ppsd) {
1555 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1556 return NT_STATUS_NO_MEMORY;
1559 return NT_STATUS_OK;
1562 /****************************************************************************
1563 Reply to query a security descriptor.
1564 ****************************************************************************/
1566 static void call_nt_transact_query_security_desc(connection_struct *conn,
1567 struct smb_request *req,
1568 uint16 **ppsetup,
1569 uint32 setup_count,
1570 char **ppparams,
1571 uint32 parameter_count,
1572 char **ppdata,
1573 uint32 data_count,
1574 uint32 max_data_count)
1576 char *params = *ppparams;
1577 char *data = *ppdata;
1578 SEC_DESC *psd = NULL;
1579 size_t sd_size;
1580 uint32 security_info_wanted;
1581 files_struct *fsp = NULL;
1582 NTSTATUS status;
1583 DATA_BLOB blob;
1585 if(parameter_count < 8) {
1586 reply_doserror(req, ERRDOS, ERRbadfunc);
1587 return;
1590 fsp = file_fsp(SVAL(params,0));
1591 if(!fsp) {
1592 reply_doserror(req, ERRDOS, ERRbadfid);
1593 return;
1596 security_info_wanted = IVAL(params,4);
1598 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1599 (unsigned int)security_info_wanted ));
1601 params = nttrans_realloc(ppparams, 4);
1602 if(params == NULL) {
1603 reply_doserror(req, ERRDOS, ERRnomem);
1604 return;
1608 * Get the permissions to return.
1611 if (!lp_nt_acl_support(SNUM(conn))) {
1612 status = get_null_nt_acl(talloc_tos(), &psd);
1613 } else {
1614 status = SMB_VFS_FGET_NT_ACL(
1615 fsp, security_info_wanted, &psd);
1618 if (!NT_STATUS_IS_OK(status)) {
1619 reply_nterror(req, status);
1620 return;
1623 sd_size = ndr_size_security_descriptor(psd, 0);
1625 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1627 SIVAL(params,0,(uint32)sd_size);
1629 if (max_data_count < sd_size) {
1630 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1631 params, 4, *ppdata, 0);
1632 return;
1636 * Allocate the data we will point this at.
1639 data = nttrans_realloc(ppdata, sd_size);
1640 if(data == NULL) {
1641 reply_doserror(req, ERRDOS, ERRnomem);
1642 return;
1645 status = marshall_sec_desc(talloc_tos(), psd,
1646 &blob.data, &blob.length);
1648 if (!NT_STATUS_IS_OK(status)) {
1649 reply_nterror(req, status);
1650 return;
1653 SMB_ASSERT(sd_size == blob.length);
1654 memcpy(data, blob.data, sd_size);
1656 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1658 return;
1661 /****************************************************************************
1662 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1663 ****************************************************************************/
1665 static void call_nt_transact_set_security_desc(connection_struct *conn,
1666 struct smb_request *req,
1667 uint16 **ppsetup,
1668 uint32 setup_count,
1669 char **ppparams,
1670 uint32 parameter_count,
1671 char **ppdata,
1672 uint32 data_count,
1673 uint32 max_data_count)
1675 char *params= *ppparams;
1676 char *data = *ppdata;
1677 files_struct *fsp = NULL;
1678 uint32 security_info_sent = 0;
1679 NTSTATUS status;
1681 if(parameter_count < 8) {
1682 reply_doserror(req, ERRDOS, ERRbadfunc);
1683 return;
1686 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
1687 reply_doserror(req, ERRDOS, ERRbadfid);
1688 return;
1691 if(!lp_nt_acl_support(SNUM(conn))) {
1692 goto done;
1695 security_info_sent = IVAL(params,4);
1697 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1698 (unsigned int)security_info_sent ));
1700 if (data_count == 0) {
1701 reply_doserror(req, ERRDOS, ERRnoaccess);
1702 return;
1705 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1707 if (!NT_STATUS_IS_OK(status)) {
1708 reply_nterror(req, status);
1709 return;
1712 done:
1713 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1714 return;
1717 /****************************************************************************
1718 Reply to NT IOCTL
1719 ****************************************************************************/
1721 static void call_nt_transact_ioctl(connection_struct *conn,
1722 struct smb_request *req,
1723 uint16 **ppsetup, uint32 setup_count,
1724 char **ppparams, uint32 parameter_count,
1725 char **ppdata, uint32 data_count,
1726 uint32 max_data_count)
1728 uint32 function;
1729 uint16 fidnum;
1730 files_struct *fsp;
1731 uint8 isFSctl;
1732 uint8 compfilter;
1733 static bool logged_message;
1734 char *pdata = *ppdata;
1736 if (setup_count != 8) {
1737 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1738 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1739 return;
1742 function = IVAL(*ppsetup, 0);
1743 fidnum = SVAL(*ppsetup, 4);
1744 isFSctl = CVAL(*ppsetup, 6);
1745 compfilter = CVAL(*ppsetup, 7);
1747 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1748 function, fidnum, isFSctl, compfilter));
1750 fsp=file_fsp(fidnum);
1751 /* this check is done in each implemented function case for now
1752 because I don't want to break anything... --metze
1753 FSP_BELONGS_CONN(fsp,conn);*/
1755 switch (function) {
1756 case FSCTL_SET_SPARSE:
1757 /* pretend this succeeded - tho strictly we should
1758 mark the file sparse (if the local fs supports it)
1759 so we can know if we need to pre-allocate or not */
1761 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1762 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1763 return;
1765 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1767 unsigned char objid[16];
1769 /* This should return the object-id on this file.
1770 * I think I'll make this be the inode+dev. JRA.
1773 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1775 if (!fsp_belongs_conn(conn, req, fsp)) {
1776 return;
1779 data_count = 64;
1780 pdata = nttrans_realloc(ppdata, data_count);
1781 if (pdata == NULL) {
1782 reply_nterror(req, NT_STATUS_NO_MEMORY);
1783 return;
1785 push_file_id_16(pdata, &fsp->file_id);
1786 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1787 push_file_id_16(pdata+32, &fsp->file_id);
1788 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1789 pdata, data_count);
1790 return;
1793 case FSCTL_GET_REPARSE_POINT:
1794 /* pretend this fail - my winXP does it like this
1795 * --metze
1798 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1799 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1800 return;
1802 case FSCTL_SET_REPARSE_POINT:
1803 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1804 * --metze
1807 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1808 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1809 return;
1811 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1814 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1815 * and return their volume names. If max_data_count is 16, then it is just
1816 * asking for the number of volumes and length of the combined names.
1818 * pdata is the data allocated by our caller, but that uses
1819 * total_data_count (which is 0 in our case) rather than max_data_count.
1820 * Allocate the correct amount and return the pointer to let
1821 * it be deallocated when we return.
1823 SHADOW_COPY_DATA *shadow_data = NULL;
1824 TALLOC_CTX *shadow_mem_ctx = NULL;
1825 bool labels = False;
1826 uint32 labels_data_count = 0;
1827 uint32 i;
1828 char *cur_pdata;
1830 if (!fsp_belongs_conn(conn, req, fsp)) {
1831 return;
1834 if (max_data_count < 16) {
1835 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1836 max_data_count));
1837 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1838 return;
1841 if (max_data_count > 16) {
1842 labels = True;
1845 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1846 if (shadow_mem_ctx == NULL) {
1847 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1848 reply_nterror(req, NT_STATUS_NO_MEMORY);
1849 return;
1852 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1853 if (shadow_data == NULL) {
1854 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1855 talloc_destroy(shadow_mem_ctx);
1856 reply_nterror(req, NT_STATUS_NO_MEMORY);
1857 return;
1860 shadow_data->mem_ctx = shadow_mem_ctx;
1863 * Call the VFS routine to actually do the work.
1865 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1866 talloc_destroy(shadow_data->mem_ctx);
1867 if (errno == ENOSYS) {
1868 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1869 conn->connectpath));
1870 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1871 return;
1872 } else {
1873 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1874 conn->connectpath));
1875 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1876 return;
1880 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1882 if (!labels) {
1883 data_count = 16;
1884 } else {
1885 data_count = 12+labels_data_count+4;
1888 if (max_data_count<data_count) {
1889 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1890 max_data_count,data_count));
1891 talloc_destroy(shadow_data->mem_ctx);
1892 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1893 return;
1896 pdata = nttrans_realloc(ppdata, data_count);
1897 if (pdata == NULL) {
1898 talloc_destroy(shadow_data->mem_ctx);
1899 reply_nterror(req, NT_STATUS_NO_MEMORY);
1900 return;
1903 cur_pdata = pdata;
1905 /* num_volumes 4 bytes */
1906 SIVAL(pdata,0,shadow_data->num_volumes);
1908 if (labels) {
1909 /* num_labels 4 bytes */
1910 SIVAL(pdata,4,shadow_data->num_volumes);
1913 /* needed_data_count 4 bytes */
1914 SIVAL(pdata,8,labels_data_count);
1916 cur_pdata+=12;
1918 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1919 shadow_data->num_volumes,fsp->fsp_name));
1920 if (labels && shadow_data->labels) {
1921 for (i=0;i<shadow_data->num_volumes;i++) {
1922 srvstr_push(pdata, req->flags2,
1923 cur_pdata, shadow_data->labels[i],
1924 2*sizeof(SHADOW_COPY_LABEL),
1925 STR_UNICODE|STR_TERMINATE);
1926 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1927 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1931 talloc_destroy(shadow_data->mem_ctx);
1933 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1934 pdata, data_count);
1936 return;
1939 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1941 /* pretend this succeeded -
1943 * we have to send back a list with all files owned by this SID
1945 * but I have to check that --metze
1947 DOM_SID sid;
1948 uid_t uid;
1949 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1951 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1953 if (!fsp_belongs_conn(conn, req, fsp)) {
1954 return;
1957 /* unknown 4 bytes: this is not the length of the sid :-( */
1958 /*unknown = IVAL(pdata,0);*/
1960 sid_parse(pdata+4,sid_len,&sid);
1961 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1963 if (!sid_to_uid(&sid, &uid)) {
1964 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1965 sid_string_dbg(&sid),
1966 (unsigned long)sid_len));
1967 uid = (-1);
1970 /* we can take a look at the find source :-)
1972 * find ./ -uid $uid -name '*' is what we need here
1975 * and send 4bytes len and then NULL terminated unicode strings
1976 * for each file
1978 * but I don't know how to deal with the paged results
1979 * (maybe we can hang the result anywhere in the fsp struct)
1981 * we don't send all files at once
1982 * and at the next we should *not* start from the beginning,
1983 * so we have to cache the result
1985 * --metze
1988 /* this works for now... */
1989 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1990 return;
1992 default:
1993 if (!logged_message) {
1994 logged_message = True; /* Only print this once... */
1995 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1996 function));
2000 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2004 #ifdef HAVE_SYS_QUOTAS
2005 /****************************************************************************
2006 Reply to get user quota
2007 ****************************************************************************/
2009 static void call_nt_transact_get_user_quota(connection_struct *conn,
2010 struct smb_request *req,
2011 uint16 **ppsetup,
2012 uint32 setup_count,
2013 char **ppparams,
2014 uint32 parameter_count,
2015 char **ppdata,
2016 uint32 data_count,
2017 uint32 max_data_count)
2019 NTSTATUS nt_status = NT_STATUS_OK;
2020 char *params = *ppparams;
2021 char *pdata = *ppdata;
2022 char *entry;
2023 int data_len=0,param_len=0;
2024 int qt_len=0;
2025 int entry_len = 0;
2026 files_struct *fsp = NULL;
2027 uint16 level = 0;
2028 size_t sid_len;
2029 DOM_SID sid;
2030 bool start_enum = True;
2031 SMB_NTQUOTA_STRUCT qt;
2032 SMB_NTQUOTA_LIST *tmp_list;
2033 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2035 ZERO_STRUCT(qt);
2037 /* access check */
2038 if (conn->server_info->utok.uid != 0) {
2039 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2040 "[%s]\n", lp_servicename(SNUM(conn)),
2041 conn->server_info->unix_name));
2042 reply_doserror(req, ERRDOS, ERRnoaccess);
2043 return;
2047 * Ensure minimum number of parameters sent.
2050 if (parameter_count < 4) {
2051 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2052 reply_doserror(req, ERRDOS, ERRinvalidparam);
2053 return;
2056 /* maybe we can check the quota_fnum */
2057 fsp = file_fsp(SVAL(params,0));
2058 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2059 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2060 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2061 return;
2064 /* the NULL pointer checking for fsp->fake_file_handle->pd
2065 * is done by CHECK_NTQUOTA_HANDLE_OK()
2067 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2069 level = SVAL(params,2);
2071 /* unknown 12 bytes leading in params */
2073 switch (level) {
2074 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2075 /* seems that we should continue with the enum here --metze */
2077 if (qt_handle->quota_list!=NULL &&
2078 qt_handle->tmp_list==NULL) {
2080 /* free the list */
2081 free_ntquota_list(&(qt_handle->quota_list));
2083 /* Realloc the size of parameters and data we will return */
2084 param_len = 4;
2085 params = nttrans_realloc(ppparams, param_len);
2086 if(params == NULL) {
2087 reply_doserror(req, ERRDOS, ERRnomem);
2088 return;
2091 data_len = 0;
2092 SIVAL(params,0,data_len);
2094 break;
2097 start_enum = False;
2099 case TRANSACT_GET_USER_QUOTA_LIST_START:
2101 if (qt_handle->quota_list==NULL &&
2102 qt_handle->tmp_list==NULL) {
2103 start_enum = True;
2106 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2107 reply_doserror(req, ERRSRV, ERRerror);
2108 return;
2111 /* Realloc the size of parameters and data we will return */
2112 param_len = 4;
2113 params = nttrans_realloc(ppparams, param_len);
2114 if(params == NULL) {
2115 reply_doserror(req, ERRDOS, ERRnomem);
2116 return;
2119 /* we should not trust the value in max_data_count*/
2120 max_data_count = MIN(max_data_count,2048);
2122 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2123 if(pdata == NULL) {
2124 reply_doserror(req, ERRDOS, ERRnomem);
2125 return;
2128 entry = pdata;
2130 /* set params Size of returned Quota Data 4 bytes*/
2131 /* but set it later when we know it */
2133 /* for each entry push the data */
2135 if (start_enum) {
2136 qt_handle->tmp_list = qt_handle->quota_list;
2139 tmp_list = qt_handle->tmp_list;
2141 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2142 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2144 sid_len = ndr_size_dom_sid(
2145 &tmp_list->quotas->sid, 0);
2146 entry_len = 40 + sid_len;
2148 /* nextoffset entry 4 bytes */
2149 SIVAL(entry,0,entry_len);
2151 /* then the len of the SID 4 bytes */
2152 SIVAL(entry,4,sid_len);
2154 /* unknown data 8 bytes SMB_BIG_UINT */
2155 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2157 /* the used disk space 8 bytes SMB_BIG_UINT */
2158 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2160 /* the soft quotas 8 bytes SMB_BIG_UINT */
2161 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2163 /* the hard quotas 8 bytes SMB_BIG_UINT */
2164 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2166 /* and now the SID */
2167 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2170 qt_handle->tmp_list = tmp_list;
2172 /* overwrite the offset of the last entry */
2173 SIVAL(entry-entry_len,0,0);
2175 data_len = 4+qt_len;
2176 /* overwrite the params quota_data_len */
2177 SIVAL(params,0,data_len);
2179 break;
2181 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2183 /* unknown 4 bytes IVAL(pdata,0) */
2185 if (data_count < 8) {
2186 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2187 reply_doserror(req, ERRDOS, ERRunknownlevel);
2188 return;
2191 sid_len = IVAL(pdata,4);
2192 /* Ensure this is less than 1mb. */
2193 if (sid_len > (1024*1024)) {
2194 reply_doserror(req, ERRDOS, ERRnomem);
2195 return;
2198 if (data_count < 8+sid_len) {
2199 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2200 reply_doserror(req, ERRDOS, ERRunknownlevel);
2201 return;
2204 data_len = 4+40+sid_len;
2206 if (max_data_count < data_len) {
2207 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2208 max_data_count, data_len));
2209 param_len = 4;
2210 SIVAL(params,0,data_len);
2211 data_len = 0;
2212 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2213 break;
2216 sid_parse(pdata+8,sid_len,&sid);
2218 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2219 ZERO_STRUCT(qt);
2221 * we have to return zero's in all fields
2222 * instead of returning an error here
2223 * --metze
2227 /* Realloc the size of parameters and data we will return */
2228 param_len = 4;
2229 params = nttrans_realloc(ppparams, param_len);
2230 if(params == NULL) {
2231 reply_doserror(req, ERRDOS, ERRnomem);
2232 return;
2235 pdata = nttrans_realloc(ppdata, data_len);
2236 if(pdata == NULL) {
2237 reply_doserror(req, ERRDOS, ERRnomem);
2238 return;
2241 entry = pdata;
2243 /* set params Size of returned Quota Data 4 bytes*/
2244 SIVAL(params,0,data_len);
2246 /* nextoffset entry 4 bytes */
2247 SIVAL(entry,0,0);
2249 /* then the len of the SID 4 bytes */
2250 SIVAL(entry,4,sid_len);
2252 /* unknown data 8 bytes SMB_BIG_UINT */
2253 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2255 /* the used disk space 8 bytes SMB_BIG_UINT */
2256 SBIG_UINT(entry,16,qt.usedspace);
2258 /* the soft quotas 8 bytes SMB_BIG_UINT */
2259 SBIG_UINT(entry,24,qt.softlim);
2261 /* the hard quotas 8 bytes SMB_BIG_UINT */
2262 SBIG_UINT(entry,32,qt.hardlim);
2264 /* and now the SID */
2265 sid_linearize(entry+40, sid_len, &sid);
2267 break;
2269 default:
2270 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2271 reply_doserror(req, ERRSRV, ERRerror);
2272 return;
2273 break;
2276 send_nt_replies(conn, req, nt_status, params, param_len,
2277 pdata, data_len);
2280 /****************************************************************************
2281 Reply to set user quota
2282 ****************************************************************************/
2284 static void call_nt_transact_set_user_quota(connection_struct *conn,
2285 struct smb_request *req,
2286 uint16 **ppsetup,
2287 uint32 setup_count,
2288 char **ppparams,
2289 uint32 parameter_count,
2290 char **ppdata,
2291 uint32 data_count,
2292 uint32 max_data_count)
2294 char *params = *ppparams;
2295 char *pdata = *ppdata;
2296 int data_len=0,param_len=0;
2297 SMB_NTQUOTA_STRUCT qt;
2298 size_t sid_len;
2299 DOM_SID sid;
2300 files_struct *fsp = NULL;
2302 ZERO_STRUCT(qt);
2304 /* access check */
2305 if (conn->server_info->utok.uid != 0) {
2306 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2307 "[%s]\n", lp_servicename(SNUM(conn)),
2308 conn->server_info->unix_name));
2309 reply_doserror(req, ERRDOS, ERRnoaccess);
2310 return;
2314 * Ensure minimum number of parameters sent.
2317 if (parameter_count < 2) {
2318 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2319 reply_doserror(req, ERRDOS, ERRinvalidparam);
2320 return;
2323 /* maybe we can check the quota_fnum */
2324 fsp = file_fsp(SVAL(params,0));
2325 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2326 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2327 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2328 return;
2331 if (data_count < 40) {
2332 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2333 reply_doserror(req, ERRDOS, ERRunknownlevel);
2334 return;
2337 /* offset to next quota record.
2338 * 4 bytes IVAL(pdata,0)
2339 * unused here...
2342 /* sid len */
2343 sid_len = IVAL(pdata,4);
2345 if (data_count < 40+sid_len) {
2346 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2347 reply_doserror(req, ERRDOS, ERRunknownlevel);
2348 return;
2351 /* unknown 8 bytes in pdata
2352 * maybe its the change time in NTTIME
2355 /* the used space 8 bytes (SMB_BIG_UINT)*/
2356 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2357 #ifdef LARGE_SMB_OFF_T
2358 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2359 #else /* LARGE_SMB_OFF_T */
2360 if ((IVAL(pdata,20) != 0)&&
2361 ((qt.usedspace != 0xFFFFFFFF)||
2362 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2363 /* more than 32 bits? */
2364 reply_doserror(req, ERRDOS, ERRunknownlevel);
2365 return;
2367 #endif /* LARGE_SMB_OFF_T */
2369 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2370 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2371 #ifdef LARGE_SMB_OFF_T
2372 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2373 #else /* LARGE_SMB_OFF_T */
2374 if ((IVAL(pdata,28) != 0)&&
2375 ((qt.softlim != 0xFFFFFFFF)||
2376 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2377 /* more than 32 bits? */
2378 reply_doserror(req, ERRDOS, ERRunknownlevel);
2379 return;
2381 #endif /* LARGE_SMB_OFF_T */
2383 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2384 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2385 #ifdef LARGE_SMB_OFF_T
2386 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2387 #else /* LARGE_SMB_OFF_T */
2388 if ((IVAL(pdata,36) != 0)&&
2389 ((qt.hardlim != 0xFFFFFFFF)||
2390 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2391 /* more than 32 bits? */
2392 reply_doserror(req, ERRDOS, ERRunknownlevel);
2393 return;
2395 #endif /* LARGE_SMB_OFF_T */
2397 sid_parse(pdata+40,sid_len,&sid);
2398 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2400 /* 44 unknown bytes left... */
2402 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2403 reply_doserror(req, ERRSRV, ERRerror);
2404 return;
2407 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2408 pdata, data_len);
2410 #endif /* HAVE_SYS_QUOTAS */
2412 static void handle_nttrans(connection_struct *conn,
2413 struct trans_state *state,
2414 struct smb_request *req)
2416 if (Protocol >= PROTOCOL_NT1) {
2417 req->flags2 |= 0x40; /* IS_LONG_NAME */
2418 SSVAL(req->inbuf,smb_flg2,req->flags2);
2421 /* Now we must call the relevant NT_TRANS function */
2422 switch(state->call) {
2423 case NT_TRANSACT_CREATE:
2425 START_PROFILE(NT_transact_create);
2426 call_nt_transact_create(
2427 conn, req,
2428 &state->setup, state->setup_count,
2429 &state->param, state->total_param,
2430 &state->data, state->total_data,
2431 state->max_data_return);
2432 END_PROFILE(NT_transact_create);
2433 break;
2436 case NT_TRANSACT_IOCTL:
2438 START_PROFILE(NT_transact_ioctl);
2439 call_nt_transact_ioctl(
2440 conn, req,
2441 &state->setup, state->setup_count,
2442 &state->param, state->total_param,
2443 &state->data, state->total_data,
2444 state->max_data_return);
2445 END_PROFILE(NT_transact_ioctl);
2446 break;
2449 case NT_TRANSACT_SET_SECURITY_DESC:
2451 START_PROFILE(NT_transact_set_security_desc);
2452 call_nt_transact_set_security_desc(
2453 conn, req,
2454 &state->setup, state->setup_count,
2455 &state->param, state->total_param,
2456 &state->data, state->total_data,
2457 state->max_data_return);
2458 END_PROFILE(NT_transact_set_security_desc);
2459 break;
2462 case NT_TRANSACT_NOTIFY_CHANGE:
2464 START_PROFILE(NT_transact_notify_change);
2465 call_nt_transact_notify_change(
2466 conn, req,
2467 &state->setup, state->setup_count,
2468 &state->param, state->total_param,
2469 &state->data, state->total_data,
2470 state->max_data_return,
2471 state->max_param_return);
2472 END_PROFILE(NT_transact_notify_change);
2473 break;
2476 case NT_TRANSACT_RENAME:
2478 START_PROFILE(NT_transact_rename);
2479 call_nt_transact_rename(
2480 conn, req,
2481 &state->setup, state->setup_count,
2482 &state->param, state->total_param,
2483 &state->data, state->total_data,
2484 state->max_data_return);
2485 END_PROFILE(NT_transact_rename);
2486 break;
2489 case NT_TRANSACT_QUERY_SECURITY_DESC:
2491 START_PROFILE(NT_transact_query_security_desc);
2492 call_nt_transact_query_security_desc(
2493 conn, req,
2494 &state->setup, state->setup_count,
2495 &state->param, state->total_param,
2496 &state->data, state->total_data,
2497 state->max_data_return);
2498 END_PROFILE(NT_transact_query_security_desc);
2499 break;
2502 #ifdef HAVE_SYS_QUOTAS
2503 case NT_TRANSACT_GET_USER_QUOTA:
2505 START_PROFILE(NT_transact_get_user_quota);
2506 call_nt_transact_get_user_quota(
2507 conn, req,
2508 &state->setup, state->setup_count,
2509 &state->param, state->total_param,
2510 &state->data, state->total_data,
2511 state->max_data_return);
2512 END_PROFILE(NT_transact_get_user_quota);
2513 break;
2516 case NT_TRANSACT_SET_USER_QUOTA:
2518 START_PROFILE(NT_transact_set_user_quota);
2519 call_nt_transact_set_user_quota(
2520 conn, req,
2521 &state->setup, state->setup_count,
2522 &state->param, state->total_param,
2523 &state->data, state->total_data,
2524 state->max_data_return);
2525 END_PROFILE(NT_transact_set_user_quota);
2526 break;
2528 #endif /* HAVE_SYS_QUOTAS */
2530 default:
2531 /* Error in request */
2532 DEBUG(0,("handle_nttrans: Unknown request %d in "
2533 "nttrans call\n", state->call));
2534 reply_doserror(req, ERRSRV, ERRerror);
2535 return;
2537 return;
2540 /****************************************************************************
2541 Reply to a SMBNTtrans.
2542 ****************************************************************************/
2544 void reply_nttrans(struct smb_request *req)
2546 connection_struct *conn = req->conn;
2547 uint32_t pscnt;
2548 uint32_t psoff;
2549 uint32_t dscnt;
2550 uint32_t dsoff;
2551 uint16 function_code;
2552 NTSTATUS result;
2553 struct trans_state *state;
2554 uint32_t size;
2555 uint32_t av_size;
2557 START_PROFILE(SMBnttrans);
2559 if (req->wct < 19) {
2560 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2561 END_PROFILE(SMBnttrans);
2562 return;
2565 size = smb_len(req->inbuf) + 4;
2566 av_size = smb_len(req->inbuf);
2567 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
2568 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
2569 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
2570 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
2571 function_code = SVAL(req->inbuf, smb_nt_Function);
2573 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2574 reply_doserror(req, ERRSRV, ERRaccess);
2575 END_PROFILE(SMBnttrans);
2576 return;
2579 result = allow_new_trans(conn->pending_trans, req->mid);
2580 if (!NT_STATUS_IS_OK(result)) {
2581 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2582 reply_nterror(req, result);
2583 END_PROFILE(SMBnttrans);
2584 return;
2587 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2588 reply_doserror(req, ERRSRV, ERRaccess);
2589 END_PROFILE(SMBnttrans);
2590 return;
2593 state->cmd = SMBnttrans;
2595 state->mid = req->mid;
2596 state->vuid = req->vuid;
2597 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
2598 state->data = NULL;
2599 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
2600 state->param = NULL;
2601 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
2602 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
2604 /* setup count is in *words* */
2605 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
2606 state->setup = NULL;
2607 state->call = function_code;
2610 * All nttrans messages we handle have smb_wct == 19 +
2611 * state->setup_count. Ensure this is so as a sanity check.
2614 if(req->wct != 19 + (state->setup_count/2)) {
2615 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2616 req->wct, 19 + (state->setup_count/2)));
2617 goto bad_param;
2620 /* Don't allow more than 128mb for each value. */
2621 if ((state->total_data > (1024*1024*128)) ||
2622 (state->total_param > (1024*1024*128))) {
2623 reply_doserror(req, ERRDOS, ERRnomem);
2624 END_PROFILE(SMBnttrans);
2625 return;
2628 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2629 goto bad_param;
2631 if (state->total_data) {
2632 /* Can't use talloc here, the core routines do realloc on the
2633 * params and data. */
2634 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2635 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2636 "bytes !\n", (unsigned int)state->total_data));
2637 TALLOC_FREE(state);
2638 reply_doserror(req, ERRDOS, ERRnomem);
2639 END_PROFILE(SMBnttrans);
2640 return;
2643 if (dscnt > state->total_data ||
2644 dsoff+dscnt < dsoff) {
2645 goto bad_param;
2648 if (dsoff > av_size ||
2649 dscnt > av_size ||
2650 dsoff+dscnt > av_size) {
2651 goto bad_param;
2654 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2657 if (state->total_param) {
2658 /* Can't use talloc here, the core routines do realloc on the
2659 * params and data. */
2660 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2661 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2662 "bytes !\n", (unsigned int)state->total_param));
2663 SAFE_FREE(state->data);
2664 TALLOC_FREE(state);
2665 reply_doserror(req, ERRDOS, ERRnomem);
2666 END_PROFILE(SMBnttrans);
2667 return;
2670 if (pscnt > state->total_param ||
2671 psoff+pscnt < psoff) {
2672 goto bad_param;
2675 if (psoff > av_size ||
2676 pscnt > av_size ||
2677 psoff+pscnt > av_size) {
2678 goto bad_param;
2681 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2684 state->received_data = dscnt;
2685 state->received_param = pscnt;
2687 if(state->setup_count > 0) {
2688 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2689 state->setup_count));
2690 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2691 if (state->setup == NULL) {
2692 DEBUG(0,("reply_nttrans : Out of memory\n"));
2693 SAFE_FREE(state->data);
2694 SAFE_FREE(state->param);
2695 TALLOC_FREE(state);
2696 reply_doserror(req, ERRDOS, ERRnomem);
2697 END_PROFILE(SMBnttrans);
2698 return;
2701 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2702 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2703 goto bad_param;
2705 if (smb_nt_SetupStart + state->setup_count > size) {
2706 goto bad_param;
2709 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2710 state->setup_count);
2711 dump_data(10, (uint8 *)state->setup, state->setup_count);
2714 if ((state->received_data == state->total_data) &&
2715 (state->received_param == state->total_param)) {
2716 handle_nttrans(conn, state, req);
2717 SAFE_FREE(state->param);
2718 SAFE_FREE(state->data);
2719 TALLOC_FREE(state);
2720 END_PROFILE(SMBnttrans);
2721 return;
2724 DLIST_ADD(conn->pending_trans, state);
2726 /* We need to send an interim response then receive the rest
2727 of the parameter/data bytes */
2728 reply_outbuf(req, 0, 0);
2729 show_msg((char *)req->outbuf);
2730 END_PROFILE(SMBnttrans);
2731 return;
2733 bad_param:
2735 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2736 SAFE_FREE(state->data);
2737 SAFE_FREE(state->param);
2738 TALLOC_FREE(state);
2739 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2740 END_PROFILE(SMBnttrans);
2741 return;
2744 /****************************************************************************
2745 Reply to a SMBnttranss
2746 ****************************************************************************/
2748 void reply_nttranss(struct smb_request *req)
2750 connection_struct *conn = req->conn;
2751 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2752 struct trans_state *state;
2753 uint32_t av_size;
2754 uint32_t size;
2756 START_PROFILE(SMBnttranss);
2758 show_msg((char *)req->inbuf);
2760 if (req->wct < 18) {
2761 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2762 END_PROFILE(SMBnttranss);
2763 return;
2766 for (state = conn->pending_trans; state != NULL;
2767 state = state->next) {
2768 if (state->mid == req->mid) {
2769 break;
2773 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2774 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2775 END_PROFILE(SMBnttranss);
2776 return;
2779 /* Revise state->total_param and state->total_data in case they have
2780 changed downwards */
2781 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
2782 < state->total_param) {
2783 state->total_param = IVAL(req->inbuf,
2784 smb_nts_TotalParameterCount);
2786 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
2787 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
2790 size = smb_len(req->inbuf) + 4;
2791 av_size = smb_len(req->inbuf);
2793 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
2794 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
2795 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
2797 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
2798 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
2799 doff = IVAL(req->inbuf, smb_nts_DataOffset);
2801 state->received_param += pcnt;
2802 state->received_data += dcnt;
2804 if ((state->received_data > state->total_data) ||
2805 (state->received_param > state->total_param))
2806 goto bad_param;
2808 if (pcnt) {
2809 if (pdisp > state->total_param ||
2810 pcnt > state->total_param ||
2811 pdisp+pcnt > state->total_param ||
2812 pdisp+pcnt < pdisp) {
2813 goto bad_param;
2816 if (poff > av_size ||
2817 pcnt > av_size ||
2818 poff+pcnt > av_size ||
2819 poff+pcnt < poff) {
2820 goto bad_param;
2823 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2824 pcnt);
2827 if (dcnt) {
2828 if (ddisp > state->total_data ||
2829 dcnt > state->total_data ||
2830 ddisp+dcnt > state->total_data ||
2831 ddisp+dcnt < ddisp) {
2832 goto bad_param;
2835 if (ddisp > av_size ||
2836 dcnt > av_size ||
2837 ddisp+dcnt > av_size ||
2838 ddisp+dcnt < ddisp) {
2839 goto bad_param;
2842 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2843 dcnt);
2846 if ((state->received_param < state->total_param) ||
2847 (state->received_data < state->total_data)) {
2848 END_PROFILE(SMBnttranss);
2849 return;
2853 * construct_reply_common will copy smb_com from inbuf to
2854 * outbuf. SMBnttranss is wrong here.
2856 SCVAL(req->inbuf,smb_com,SMBnttrans);
2858 handle_nttrans(conn, state, req);
2860 DLIST_REMOVE(conn->pending_trans, state);
2861 SAFE_FREE(state->data);
2862 SAFE_FREE(state->param);
2863 TALLOC_FREE(state);
2864 END_PROFILE(SMBnttranss);
2865 return;
2867 bad_param:
2869 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2870 DLIST_REMOVE(conn->pending_trans, state);
2871 SAFE_FREE(state->data);
2872 SAFE_FREE(state->param);
2873 TALLOC_FREE(state);
2874 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2875 END_PROFILE(SMBnttranss);
2876 return;