Makefile: No need for c++ patch, also use fhs
[Samba/vfs_proxy.git] / source3 / smbd / nttrans.c
blob24a14a8c1b8f1f363fcc3804fdbba90915e05fe6
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 extern int max_send;
24 extern enum protocol_types Protocol;
25 extern const struct generic_mapping file_generic_mapping;
27 static char *nttrans_realloc(char **ptr, size_t size)
29 if (ptr==NULL) {
30 smb_panic("nttrans_realloc() called with NULL ptr");
33 *ptr = (char *)SMB_REALLOC(*ptr, size);
34 if(*ptr == NULL) {
35 return NULL;
37 memset(*ptr,'\0',size);
38 return *ptr;
41 /****************************************************************************
42 Send the required number of replies back.
43 We assume all fields other than the data fields are
44 set correctly for the type of call.
45 HACK ! Always assumes smb_setup field is zero.
46 ****************************************************************************/
48 void send_nt_replies(connection_struct *conn,
49 struct smb_request *req, NTSTATUS nt_error,
50 char *params, int paramsize,
51 char *pdata, int datasize)
53 int data_to_send = datasize;
54 int params_to_send = paramsize;
55 int useable_space;
56 char *pp = params;
57 char *pd = pdata;
58 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
59 int alignment_offset = 3;
60 int data_alignment_offset = 0;
63 * If there genuinely are no parameters or data to send just send
64 * the empty packet.
67 if(params_to_send == 0 && data_to_send == 0) {
68 reply_outbuf(req, 18, 0);
69 show_msg((char *)req->outbuf);
70 return;
74 * When sending params and data ensure that both are nicely aligned.
75 * Only do this alignment when there is also data to send - else
76 * can cause NT redirector problems.
79 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
80 data_alignment_offset = 4 - (params_to_send % 4);
84 * Space is bufsize minus Netbios over TCP header minus SMB header.
85 * The alignment_offset is to align the param bytes on a four byte
86 * boundary (2 bytes for data len, one byte pad).
87 * NT needs this to work correctly.
90 useable_space = max_send - (smb_size
91 + 2 * 18 /* wct */
92 + alignment_offset
93 + data_alignment_offset);
95 if (useable_space < 0) {
96 char *msg = talloc_asprintf(
97 talloc_tos(),
98 "send_nt_replies failed sanity useable_space = %d!!!",
99 useable_space);
100 DEBUG(0, ("%s\n", msg));
101 exit_server_cleanly(msg);
104 while (params_to_send || data_to_send) {
107 * Calculate whether we will totally or partially fill this packet.
110 total_sent_thistime = params_to_send + data_to_send;
113 * We can never send more than useable_space.
116 total_sent_thistime = MIN(total_sent_thistime, useable_space);
118 reply_outbuf(req, 18,
119 total_sent_thistime + alignment_offset
120 + data_alignment_offset);
123 * We might have had SMBnttranss in req->inbuf, fix that.
125 SCVAL(req->outbuf, smb_com, SMBnttrans);
128 * Set total params and data to be sent.
131 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
132 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
135 * Calculate how many parameters and data we can fit into
136 * this packet. Parameters get precedence.
139 params_sent_thistime = MIN(params_to_send,useable_space);
140 data_sent_thistime = useable_space - params_sent_thistime;
141 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
143 SIVAL(req->outbuf, smb_ntr_ParameterCount,
144 params_sent_thistime);
146 if(params_sent_thistime == 0) {
147 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
148 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
149 } else {
151 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
152 * parameter bytes, however the first 4 bytes of outbuf are
153 * the Netbios over TCP header. Thus use smb_base() to subtract
154 * them from the calculation.
157 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
158 ((smb_buf(req->outbuf)+alignment_offset)
159 - smb_base(req->outbuf)));
161 * Absolute displacement of param bytes sent in this packet.
164 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
165 pp - params);
169 * Deal with the data portion.
172 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
174 if(data_sent_thistime == 0) {
175 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
176 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
177 } else {
179 * The offset of the data bytes is the offset of the
180 * parameter bytes plus the number of parameters being sent this time.
183 SIVAL(req->outbuf, smb_ntr_DataOffset,
184 ((smb_buf(req->outbuf)+alignment_offset) -
185 smb_base(req->outbuf))
186 + params_sent_thistime + data_alignment_offset);
187 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
191 * Copy the param bytes into the packet.
194 if(params_sent_thistime) {
195 if (alignment_offset != 0) {
196 memset(smb_buf(req->outbuf), 0,
197 alignment_offset);
199 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
200 params_sent_thistime);
204 * Copy in the data bytes
207 if(data_sent_thistime) {
208 if (data_alignment_offset != 0) {
209 memset((smb_buf(req->outbuf)+alignment_offset+
210 params_sent_thistime), 0,
211 data_alignment_offset);
213 memcpy(smb_buf(req->outbuf)+alignment_offset
214 +params_sent_thistime+data_alignment_offset,
215 pd,data_sent_thistime);
218 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
219 params_sent_thistime, data_sent_thistime, useable_space));
220 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
221 params_to_send, data_to_send, paramsize, datasize));
223 if (NT_STATUS_V(nt_error)) {
224 error_packet_set((char *)req->outbuf,
225 0, 0, nt_error,
226 __LINE__,__FILE__);
229 /* Send the packet */
230 show_msg((char *)req->outbuf);
231 if (!srv_send_smb(smbd_server_fd(),
232 (char *)req->outbuf,
233 IS_CONN_ENCRYPTED(conn))) {
234 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
237 TALLOC_FREE(req->outbuf);
239 pp += params_sent_thistime;
240 pd += data_sent_thistime;
242 params_to_send -= params_sent_thistime;
243 data_to_send -= data_sent_thistime;
246 * Sanity check
249 if(params_to_send < 0 || data_to_send < 0) {
250 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
251 params_to_send, data_to_send));
252 exit_server_cleanly("send_nt_replies: internal error");
257 /****************************************************************************
258 Is it an NTFS stream name ?
259 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
260 $DATA can be used as both a stream name and a stream type. A missing stream
261 name or type implies $DATA.
262 ****************************************************************************/
264 bool is_ntfs_stream_name(const char *fname)
266 if (lp_posix_pathnames()) {
267 return False;
269 return (strchr_m(fname, ':') != NULL) ? True : False;
272 /****************************************************************************
273 Reply to an NT create and X call on a pipe
274 ****************************************************************************/
276 static void nt_open_pipe(char *fname, connection_struct *conn,
277 struct smb_request *req, int *ppnum)
279 files_struct *fsp;
280 NTSTATUS status;
282 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
284 /* Strip \\ off the name. */
285 fname++;
287 status = np_open(req, fname, &fsp);
288 if (!NT_STATUS_IS_OK(status)) {
289 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
290 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
291 ERRDOS, ERRbadpipe);
292 return;
294 reply_nterror(req, status);
295 return;
298 *ppnum = fsp->fnum;
299 return;
302 /****************************************************************************
303 Reply to an NT create and X call for pipes.
304 ****************************************************************************/
306 static void do_ntcreate_pipe_open(connection_struct *conn,
307 struct smb_request *req)
309 char *fname = NULL;
310 int pnum = -1;
311 char *p = NULL;
312 uint32 flags = IVAL(req->vwv+3, 1);
313 TALLOC_CTX *ctx = talloc_tos();
315 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
317 if (!fname) {
318 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
319 ERRDOS, ERRbadpipe);
320 return;
322 nt_open_pipe(fname, conn, req, &pnum);
324 if (req->outbuf) {
325 /* error reply */
326 return;
330 * Deal with pipe return.
333 if (flags & EXTENDED_RESPONSE_REQUIRED) {
334 /* This is very strange. We
335 * return 50 words, but only set
336 * the wcnt to 42 ? It's definately
337 * what happens on the wire....
339 reply_outbuf(req, 50, 0);
340 SCVAL(req->outbuf,smb_wct,42);
341 } else {
342 reply_outbuf(req, 34, 0);
345 p = (char *)req->outbuf + smb_vwv2;
346 p++;
347 SSVAL(p,0,pnum);
348 p += 2;
349 SIVAL(p,0,FILE_WAS_OPENED);
350 p += 4;
351 p += 32;
352 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
353 p += 20;
354 /* File type. */
355 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
356 /* Device state. */
357 SSVAL(p,2, 0x5FF); /* ? */
358 p += 4;
360 if (flags & EXTENDED_RESPONSE_REQUIRED) {
361 p += 25;
362 SIVAL(p,0,FILE_GENERIC_ALL);
364 * For pipes W2K3 seems to return
365 * 0x12019B next.
366 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
368 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
371 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
373 chain_reply(req);
376 /****************************************************************************
377 Reply to an NT create and X call.
378 ****************************************************************************/
380 void reply_ntcreate_and_X(struct smb_request *req)
382 connection_struct *conn = req->conn;
383 char *fname = NULL;
384 uint32 flags;
385 uint32 access_mask;
386 uint32 file_attributes;
387 uint32 share_access;
388 uint32 create_disposition;
389 uint32 create_options;
390 uint16 root_dir_fid;
391 uint64_t allocation_size;
392 /* Breakout the oplock request bits so we can set the
393 reply bits separately. */
394 uint32 fattr=0;
395 SMB_OFF_T file_len = 0;
396 SMB_STRUCT_STAT sbuf;
397 int info = 0;
398 files_struct *fsp = NULL;
399 char *p = NULL;
400 struct timespec c_timespec;
401 struct timespec a_timespec;
402 struct timespec m_timespec;
403 NTSTATUS status;
404 int oplock_request;
405 uint8_t oplock_granted = NO_OPLOCK_RETURN;
406 TALLOC_CTX *ctx = talloc_tos();
408 START_PROFILE(SMBntcreateX);
410 if (req->wct < 24) {
411 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
412 return;
415 flags = IVAL(req->vwv+3, 1);
416 access_mask = IVAL(req->vwv+7, 1);
417 file_attributes = IVAL(req->vwv+13, 1);
418 share_access = IVAL(req->vwv+15, 1);
419 create_disposition = IVAL(req->vwv+17, 1);
420 create_options = IVAL(req->vwv+19, 1);
421 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
423 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
424 #ifdef LARGE_SMB_OFF_T
425 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
426 #endif
428 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
429 STR_TERMINATE, &status);
431 if (!NT_STATUS_IS_OK(status)) {
432 reply_nterror(req, status);
433 END_PROFILE(SMBntcreateX);
434 return;
437 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
438 "file_attributes = 0x%x, share_access = 0x%x, "
439 "create_disposition = 0x%x create_options = 0x%x "
440 "root_dir_fid = 0x%x, fname = %s\n",
441 (unsigned int)flags,
442 (unsigned int)access_mask,
443 (unsigned int)file_attributes,
444 (unsigned int)share_access,
445 (unsigned int)create_disposition,
446 (unsigned int)create_options,
447 (unsigned int)root_dir_fid,
448 fname));
451 * we need to remove ignored bits when they come directly from the client
452 * because we reuse some of them for internal stuff
454 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
457 * If it's an IPC, use the pipe handler.
460 if (IS_IPC(conn)) {
461 if (lp_nt_pipe_support()) {
462 do_ntcreate_pipe_open(conn, req);
463 END_PROFILE(SMBntcreateX);
464 return;
466 reply_doserror(req, ERRDOS, ERRnoaccess);
467 END_PROFILE(SMBntcreateX);
468 return;
471 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
472 if (oplock_request) {
473 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
474 ? BATCH_OPLOCK : 0;
477 status = SMB_VFS_CREATE_FILE(
478 conn, /* conn */
479 req, /* req */
480 root_dir_fid, /* root_dir_fid */
481 fname, /* fname */
482 CFF_DOS_PATH, /* create_file_flags */
483 access_mask, /* access_mask */
484 share_access, /* share_access */
485 create_disposition, /* create_disposition*/
486 create_options, /* create_options */
487 file_attributes, /* file_attributes */
488 oplock_request, /* oplock_request */
489 allocation_size, /* allocation_size */
490 NULL, /* sd */
491 NULL, /* ea_list */
492 &fsp, /* result */
493 &info, /* pinfo */
494 &sbuf); /* psbuf */
496 if (!NT_STATUS_IS_OK(status)) {
497 if (open_was_deferred(req->mid)) {
498 /* We have re-scheduled this call, no error. */
499 END_PROFILE(SMBntcreateX);
500 return;
502 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
503 reply_botherror(req, status, ERRDOS, ERRfilexists);
505 else {
506 reply_nterror(req, status);
508 END_PROFILE(SMBntcreateX);
509 return;
513 * If the caller set the extended oplock request bit
514 * and we granted one (by whatever means) - set the
515 * correct bit for extended oplock reply.
518 if (oplock_request &&
519 (lp_fake_oplocks(SNUM(conn))
520 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
523 * Exclusive oplock granted
526 if (flags & REQUEST_BATCH_OPLOCK) {
527 oplock_granted = BATCH_OPLOCK_RETURN;
528 } else {
529 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
531 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
532 oplock_granted = LEVEL_II_OPLOCK_RETURN;
533 } else {
534 oplock_granted = NO_OPLOCK_RETURN;
537 file_len = sbuf.st_size;
538 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
539 if (fattr == 0) {
540 fattr = FILE_ATTRIBUTE_NORMAL;
543 if (flags & EXTENDED_RESPONSE_REQUIRED) {
544 /* This is very strange. We
545 * return 50 words, but only set
546 * the wcnt to 42 ? It's definately
547 * what happens on the wire....
549 reply_outbuf(req, 50, 0);
550 SCVAL(req->outbuf,smb_wct,42);
551 } else {
552 reply_outbuf(req, 34, 0);
555 p = (char *)req->outbuf + smb_vwv2;
557 SCVAL(p, 0, oplock_granted);
559 p++;
560 SSVAL(p,0,fsp->fnum);
561 p += 2;
562 if ((create_disposition == FILE_SUPERSEDE)
563 && (info == FILE_WAS_OVERWRITTEN)) {
564 SIVAL(p,0,FILE_WAS_SUPERSEDED);
565 } else {
566 SIVAL(p,0,info);
568 p += 4;
570 /* Create time. */
571 c_timespec = get_create_timespec(
572 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
573 a_timespec = get_atimespec(&sbuf);
574 m_timespec = get_mtimespec(&sbuf);
576 if (lp_dos_filetime_resolution(SNUM(conn))) {
577 dos_filetime_timespec(&c_timespec);
578 dos_filetime_timespec(&a_timespec);
579 dos_filetime_timespec(&m_timespec);
582 put_long_date_timespec(p, c_timespec); /* create time. */
583 p += 8;
584 put_long_date_timespec(p, a_timespec); /* access time */
585 p += 8;
586 put_long_date_timespec(p, m_timespec); /* write time */
587 p += 8;
588 put_long_date_timespec(p, m_timespec); /* change time */
589 p += 8;
590 SIVAL(p,0,fattr); /* File Attributes. */
591 p += 4;
592 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
593 p += 8;
594 SOFF_T(p,0,file_len);
595 p += 8;
596 if (flags & EXTENDED_RESPONSE_REQUIRED) {
597 SSVAL(p,2,0x7);
599 p += 4;
600 SCVAL(p,0,fsp->is_directory ? 1 : 0);
602 if (flags & EXTENDED_RESPONSE_REQUIRED) {
603 uint32 perms = 0;
604 p += 25;
605 if (fsp->is_directory
606 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
607 perms = FILE_GENERIC_ALL;
608 } else {
609 perms = FILE_GENERIC_READ|FILE_EXECUTE;
611 SIVAL(p,0,perms);
614 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
615 fsp->fnum, fsp->fsp_name));
617 chain_reply(req);
618 END_PROFILE(SMBntcreateX);
619 return;
622 /****************************************************************************
623 Reply to a NT_TRANSACT_CREATE call to open a pipe.
624 ****************************************************************************/
626 static void do_nt_transact_create_pipe(connection_struct *conn,
627 struct smb_request *req,
628 uint16 **ppsetup, uint32 setup_count,
629 char **ppparams, uint32 parameter_count,
630 char **ppdata, uint32 data_count)
632 char *fname = NULL;
633 char *params = *ppparams;
634 int pnum = -1;
635 char *p = NULL;
636 NTSTATUS status;
637 size_t param_len;
638 uint32 flags;
639 TALLOC_CTX *ctx = talloc_tos();
642 * Ensure minimum number of parameters sent.
645 if(parameter_count < 54) {
646 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
647 reply_doserror(req, ERRDOS, ERRnoaccess);
648 return;
651 flags = IVAL(params,0);
653 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
654 parameter_count-53, STR_TERMINATE,
655 &status);
656 if (!NT_STATUS_IS_OK(status)) {
657 reply_nterror(req, status);
658 return;
661 nt_open_pipe(fname, conn, req, &pnum);
663 if (req->outbuf) {
664 /* Error return */
665 return;
668 /* Realloc the size of parameters and data we will return */
669 if (flags & EXTENDED_RESPONSE_REQUIRED) {
670 /* Extended response is 32 more byyes. */
671 param_len = 101;
672 } else {
673 param_len = 69;
675 params = nttrans_realloc(ppparams, param_len);
676 if(params == NULL) {
677 reply_doserror(req, ERRDOS, ERRnomem);
678 return;
681 p = params;
682 SCVAL(p,0,NO_OPLOCK_RETURN);
684 p += 2;
685 SSVAL(p,0,pnum);
686 p += 2;
687 SIVAL(p,0,FILE_WAS_OPENED);
688 p += 8;
690 p += 32;
691 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
692 p += 20;
693 /* File type. */
694 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
695 /* Device state. */
696 SSVAL(p,2, 0x5FF); /* ? */
697 p += 4;
699 if (flags & EXTENDED_RESPONSE_REQUIRED) {
700 p += 25;
701 SIVAL(p,0,FILE_GENERIC_ALL);
703 * For pipes W2K3 seems to return
704 * 0x12019B next.
705 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
707 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
710 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
712 /* Send the required number of replies */
713 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
715 return;
718 /****************************************************************************
719 Internal fn to set security descriptors.
720 ****************************************************************************/
722 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
723 uint32 security_info_sent)
725 SEC_DESC *psd = NULL;
726 NTSTATUS status;
728 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
729 return NT_STATUS_OK;
732 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
734 if (!NT_STATUS_IS_OK(status)) {
735 return status;
738 if (psd->owner_sid == NULL) {
739 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
741 if (psd->group_sid == NULL) {
742 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
745 /* Convert all the generic bits. */
746 security_acl_map_generic(psd->dacl, &file_generic_mapping);
747 security_acl_map_generic(psd->sacl, &file_generic_mapping);
749 if (DEBUGLEVEL >= 10) {
750 DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
751 NDR_PRINT_DEBUG(security_descriptor, psd);
754 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
756 TALLOC_FREE(psd);
758 return status;
761 /****************************************************************************
762 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
763 ****************************************************************************/
765 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
767 struct ea_list *ea_list_head = NULL;
768 size_t offset = 0;
770 if (data_size < 4) {
771 return NULL;
774 while (offset + 4 <= data_size) {
775 size_t next_offset = IVAL(pdata,offset);
776 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
778 if (!eal) {
779 return NULL;
782 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
783 if (next_offset == 0) {
784 break;
786 offset += next_offset;
789 return ea_list_head;
792 /****************************************************************************
793 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
794 ****************************************************************************/
796 static void call_nt_transact_create(connection_struct *conn,
797 struct smb_request *req,
798 uint16 **ppsetup, uint32 setup_count,
799 char **ppparams, uint32 parameter_count,
800 char **ppdata, uint32 data_count,
801 uint32 max_data_count)
803 char *fname = NULL;
804 char *params = *ppparams;
805 char *data = *ppdata;
806 /* Breakout the oplock request bits so we can set the reply bits separately. */
807 uint32 fattr=0;
808 SMB_OFF_T file_len = 0;
809 SMB_STRUCT_STAT sbuf;
810 int info = 0;
811 files_struct *fsp = NULL;
812 char *p = NULL;
813 uint32 flags;
814 uint32 access_mask;
815 uint32 file_attributes;
816 uint32 share_access;
817 uint32 create_disposition;
818 uint32 create_options;
819 uint32 sd_len;
820 struct security_descriptor *sd = NULL;
821 uint32 ea_len;
822 uint16 root_dir_fid;
823 struct timespec c_timespec;
824 struct timespec a_timespec;
825 struct timespec m_timespec;
826 struct ea_list *ea_list = NULL;
827 NTSTATUS status;
828 size_t param_len;
829 uint64_t allocation_size;
830 int oplock_request;
831 uint8_t oplock_granted;
832 TALLOC_CTX *ctx = talloc_tos();
834 DEBUG(5,("call_nt_transact_create\n"));
837 * If it's an IPC, use the pipe handler.
840 if (IS_IPC(conn)) {
841 if (lp_nt_pipe_support()) {
842 do_nt_transact_create_pipe(
843 conn, req,
844 ppsetup, setup_count,
845 ppparams, parameter_count,
846 ppdata, data_count);
847 return;
849 reply_doserror(req, ERRDOS, ERRnoaccess);
850 return;
854 * Ensure minimum number of parameters sent.
857 if(parameter_count < 54) {
858 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
859 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
860 return;
863 flags = IVAL(params,0);
864 access_mask = IVAL(params,8);
865 file_attributes = IVAL(params,20);
866 share_access = IVAL(params,24);
867 create_disposition = IVAL(params,28);
868 create_options = IVAL(params,32);
869 sd_len = IVAL(params,36);
870 ea_len = IVAL(params,40);
871 root_dir_fid = (uint16)IVAL(params,4);
872 allocation_size = (uint64_t)IVAL(params,12);
873 #ifdef LARGE_SMB_OFF_T
874 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
875 #endif
878 * we need to remove ignored bits when they come directly from the client
879 * because we reuse some of them for internal stuff
881 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
883 /* Ensure the data_len is correct for the sd and ea values given. */
884 if ((ea_len + sd_len > data_count)
885 || (ea_len > data_count) || (sd_len > data_count)
886 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
887 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
888 "%u, data_count = %u\n", (unsigned int)ea_len,
889 (unsigned int)sd_len, (unsigned int)data_count));
890 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
891 return;
894 if (sd_len) {
895 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
896 sd_len));
898 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
899 &sd);
900 if (!NT_STATUS_IS_OK(status)) {
901 DEBUG(10, ("call_nt_transact_create: "
902 "unmarshall_sec_desc failed: %s\n",
903 nt_errstr(status)));
904 reply_nterror(req, status);
905 return;
909 if (ea_len) {
910 if (!lp_ea_support(SNUM(conn))) {
911 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
912 "EA's not supported.\n",
913 (unsigned int)ea_len));
914 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
915 return;
918 if (ea_len < 10) {
919 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
920 "too small (should be more than 10)\n",
921 (unsigned int)ea_len ));
922 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
923 return;
926 /* We have already checked that ea_len <= data_count here. */
927 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
928 ea_len);
929 if (ea_list == NULL) {
930 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
931 return;
935 srvstr_get_path(ctx, params, req->flags2, &fname,
936 params+53, parameter_count-53,
937 STR_TERMINATE, &status);
938 if (!NT_STATUS_IS_OK(status)) {
939 reply_nterror(req, status);
940 return;
943 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
944 if (oplock_request) {
945 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
946 ? BATCH_OPLOCK : 0;
949 status = SMB_VFS_CREATE_FILE(
950 conn, /* conn */
951 req, /* req */
952 root_dir_fid, /* root_dir_fid */
953 fname, /* fname */
954 CFF_DOS_PATH, /* create_file_flags */
955 access_mask, /* access_mask */
956 share_access, /* share_access */
957 create_disposition, /* create_disposition*/
958 create_options, /* create_options */
959 file_attributes, /* file_attributes */
960 oplock_request, /* oplock_request */
961 allocation_size, /* allocation_size */
962 sd, /* sd */
963 ea_list, /* ea_list */
964 &fsp, /* result */
965 &info, /* pinfo */
966 &sbuf); /* psbuf */
968 if(!NT_STATUS_IS_OK(status)) {
969 if (open_was_deferred(req->mid)) {
970 /* We have re-scheduled this call, no error. */
971 return;
973 reply_openerror(req, status);
974 return;
978 * If the caller set the extended oplock request bit
979 * and we granted one (by whatever means) - set the
980 * correct bit for extended oplock reply.
983 if (oplock_request &&
984 (lp_fake_oplocks(SNUM(conn))
985 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
988 * Exclusive oplock granted
991 if (flags & REQUEST_BATCH_OPLOCK) {
992 oplock_granted = BATCH_OPLOCK_RETURN;
993 } else {
994 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
996 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
997 oplock_granted = LEVEL_II_OPLOCK_RETURN;
998 } else {
999 oplock_granted = NO_OPLOCK_RETURN;
1002 file_len = sbuf.st_size;
1003 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1004 if (fattr == 0) {
1005 fattr = FILE_ATTRIBUTE_NORMAL;
1008 /* Realloc the size of parameters and data we will return */
1009 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1010 /* Extended response is 32 more byyes. */
1011 param_len = 101;
1012 } else {
1013 param_len = 69;
1015 params = nttrans_realloc(ppparams, param_len);
1016 if(params == NULL) {
1017 reply_doserror(req, ERRDOS, ERRnomem);
1018 return;
1021 p = params;
1022 SCVAL(p, 0, oplock_granted);
1024 p += 2;
1025 SSVAL(p,0,fsp->fnum);
1026 p += 2;
1027 if ((create_disposition == FILE_SUPERSEDE)
1028 && (info == FILE_WAS_OVERWRITTEN)) {
1029 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1030 } else {
1031 SIVAL(p,0,info);
1033 p += 8;
1035 /* Create time. */
1036 c_timespec = get_create_timespec(
1037 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1038 a_timespec = get_atimespec(&sbuf);
1039 m_timespec = get_mtimespec(&sbuf);
1041 if (lp_dos_filetime_resolution(SNUM(conn))) {
1042 dos_filetime_timespec(&c_timespec);
1043 dos_filetime_timespec(&a_timespec);
1044 dos_filetime_timespec(&m_timespec);
1047 put_long_date_timespec(p, c_timespec); /* create time. */
1048 p += 8;
1049 put_long_date_timespec(p, a_timespec); /* access time */
1050 p += 8;
1051 put_long_date_timespec(p, m_timespec); /* write time */
1052 p += 8;
1053 put_long_date_timespec(p, m_timespec); /* change time */
1054 p += 8;
1055 SIVAL(p,0,fattr); /* File Attributes. */
1056 p += 4;
1057 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1058 p += 8;
1059 SOFF_T(p,0,file_len);
1060 p += 8;
1061 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1062 SSVAL(p,2,0x7);
1064 p += 4;
1065 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1067 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1068 uint32 perms = 0;
1069 p += 25;
1070 if (fsp->is_directory
1071 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1072 perms = FILE_GENERIC_ALL;
1073 } else {
1074 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1076 SIVAL(p,0,perms);
1079 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1081 /* Send the required number of replies */
1082 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1084 return;
1087 /****************************************************************************
1088 Reply to a NT CANCEL request.
1089 conn POINTER CAN BE NULL HERE !
1090 ****************************************************************************/
1092 void reply_ntcancel(struct smb_request *req)
1095 * Go through and cancel any pending change notifies.
1098 START_PROFILE(SMBntcancel);
1099 remove_pending_change_notify_requests_by_mid(req->mid);
1100 remove_pending_lock_requests_by_mid(req->mid);
1101 srv_cancel_sign_response(req->mid);
1103 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1105 END_PROFILE(SMBntcancel);
1106 return;
1109 /****************************************************************************
1110 Copy a file.
1111 ****************************************************************************/
1113 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1114 connection_struct *conn,
1115 struct smb_request *req,
1116 const char *oldname_in,
1117 const char *newname_in,
1118 uint32 attrs)
1120 SMB_STRUCT_STAT sbuf1, sbuf2;
1121 char *oldname = NULL;
1122 char *newname = NULL;
1123 char *last_component_oldname = NULL;
1124 char *last_component_newname = NULL;
1125 files_struct *fsp1,*fsp2;
1126 uint32 fattr;
1127 int info;
1128 SMB_OFF_T ret=-1;
1129 NTSTATUS status = NT_STATUS_OK;
1131 ZERO_STRUCT(sbuf1);
1132 ZERO_STRUCT(sbuf2);
1134 if (!CAN_WRITE(conn)) {
1135 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1138 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1139 &last_component_oldname, &sbuf1);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 return status;
1144 status = check_name(conn, oldname);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 return status;
1149 /* Source must already exist. */
1150 if (!VALID_STAT(sbuf1)) {
1151 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1153 /* Ensure attributes match. */
1154 fattr = dos_mode(conn,oldname,&sbuf1);
1155 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1156 return NT_STATUS_NO_SUCH_FILE;
1159 status = unix_convert(ctx, conn, newname_in, False, &newname,
1160 &last_component_newname, &sbuf2);
1161 if (!NT_STATUS_IS_OK(status)) {
1162 return status;
1165 status = check_name(conn, newname);
1166 if (!NT_STATUS_IS_OK(status)) {
1167 return status;
1170 /* Disallow if newname already exists. */
1171 if (VALID_STAT(sbuf2)) {
1172 return NT_STATUS_OBJECT_NAME_COLLISION;
1175 /* No links from a directory. */
1176 if (S_ISDIR(sbuf1.st_mode)) {
1177 return NT_STATUS_FILE_IS_A_DIRECTORY;
1180 /* Ensure this is within the share. */
1181 status = check_reduced_name(conn, oldname);
1182 if (!NT_STATUS_IS_OK(status)) {
1183 return status;
1186 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1187 oldname, newname));
1189 status = SMB_VFS_CREATE_FILE(
1190 conn, /* conn */
1191 req, /* req */
1192 0, /* root_dir_fid */
1193 oldname, /* fname */
1194 0, /* create_file_flags */
1195 FILE_READ_DATA, /* access_mask */
1196 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1197 FILE_SHARE_DELETE),
1198 FILE_OPEN, /* create_disposition*/
1199 0, /* create_options */
1200 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1201 NO_OPLOCK, /* oplock_request */
1202 0, /* allocation_size */
1203 NULL, /* sd */
1204 NULL, /* ea_list */
1205 &fsp1, /* result */
1206 &info, /* pinfo */
1207 &sbuf1); /* psbuf */
1209 if (!NT_STATUS_IS_OK(status)) {
1210 return status;
1213 status = SMB_VFS_CREATE_FILE(
1214 conn, /* conn */
1215 req, /* req */
1216 0, /* root_dir_fid */
1217 newname, /* fname */
1218 0, /* create_file_flags */
1219 FILE_WRITE_DATA, /* access_mask */
1220 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1221 FILE_SHARE_DELETE),
1222 FILE_CREATE, /* create_disposition*/
1223 0, /* create_options */
1224 fattr, /* file_attributes */
1225 NO_OPLOCK, /* oplock_request */
1226 0, /* allocation_size */
1227 NULL, /* sd */
1228 NULL, /* ea_list */
1229 &fsp2, /* result */
1230 &info, /* pinfo */
1231 &sbuf2); /* psbuf */
1233 if (!NT_STATUS_IS_OK(status)) {
1234 close_file(NULL, fsp1, ERROR_CLOSE);
1235 return status;
1238 if (sbuf1.st_size) {
1239 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1243 * As we are opening fsp1 read-only we only expect
1244 * an error on close on fsp2 if we are out of space.
1245 * Thus we don't look at the error return from the
1246 * close of fsp1.
1248 close_file(NULL, fsp1, NORMAL_CLOSE);
1250 /* Ensure the modtime is set correctly on the destination file. */
1251 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1253 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1255 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1256 creates the file. This isn't the correct thing to do in the copy
1257 case. JRA */
1258 file_set_dosmode(conn, newname, fattr, &sbuf2,
1259 parent_dirname(newname),false);
1261 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1262 return NT_STATUS_DISK_FULL;
1265 if (!NT_STATUS_IS_OK(status)) {
1266 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1267 nt_errstr(status), oldname, newname));
1269 return status;
1272 /****************************************************************************
1273 Reply to a NT rename request.
1274 ****************************************************************************/
1276 void reply_ntrename(struct smb_request *req)
1278 connection_struct *conn = req->conn;
1279 char *oldname = NULL;
1280 char *newname = NULL;
1281 const char *p;
1282 NTSTATUS status;
1283 bool src_has_wcard = False;
1284 bool dest_has_wcard = False;
1285 uint32 attrs;
1286 uint16 rename_type;
1287 TALLOC_CTX *ctx = talloc_tos();
1289 START_PROFILE(SMBntrename);
1291 if (req->wct < 4) {
1292 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1293 END_PROFILE(SMBntrename);
1294 return;
1297 attrs = SVAL(req->vwv+0, 0);
1298 rename_type = SVAL(req->vwv+1, 0);
1300 p = (const char *)req->buf + 1;
1301 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1302 &status, &src_has_wcard);
1303 if (!NT_STATUS_IS_OK(status)) {
1304 reply_nterror(req, status);
1305 END_PROFILE(SMBntrename);
1306 return;
1309 if( is_ntfs_stream_name(oldname)) {
1310 /* Can't rename a stream. */
1311 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1312 END_PROFILE(SMBntrename);
1313 return;
1316 if (ms_has_wild(oldname)) {
1317 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1318 END_PROFILE(SMBntrename);
1319 return;
1322 p++;
1323 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1324 &status, &dest_has_wcard);
1325 if (!NT_STATUS_IS_OK(status)) {
1326 reply_nterror(req, status);
1327 END_PROFILE(SMBntrename);
1328 return;
1331 status = resolve_dfspath(ctx, conn,
1332 req->flags2 & FLAGS2_DFS_PATHNAMES,
1333 oldname,
1334 &oldname);
1335 if (!NT_STATUS_IS_OK(status)) {
1336 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1337 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1338 ERRSRV, ERRbadpath);
1339 END_PROFILE(SMBntrename);
1340 return;
1342 reply_nterror(req, status);
1343 END_PROFILE(SMBntrename);
1344 return;
1347 status = resolve_dfspath(ctx, conn,
1348 req->flags2 & FLAGS2_DFS_PATHNAMES,
1349 newname,
1350 &newname);
1351 if (!NT_STATUS_IS_OK(status)) {
1352 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1353 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1354 ERRSRV, ERRbadpath);
1355 END_PROFILE(SMBntrename);
1356 return;
1358 reply_nterror(req, status);
1359 END_PROFILE(SMBntrename);
1360 return;
1363 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1365 switch(rename_type) {
1366 case RENAME_FLAG_RENAME:
1367 status = rename_internals(ctx, conn, req, oldname,
1368 newname, attrs, False, src_has_wcard,
1369 dest_has_wcard, DELETE_ACCESS);
1370 break;
1371 case RENAME_FLAG_HARD_LINK:
1372 if (src_has_wcard || dest_has_wcard) {
1373 /* No wildcards. */
1374 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1375 } else {
1376 status = hardlink_internals(ctx,
1377 conn,
1378 oldname,
1379 newname);
1381 break;
1382 case RENAME_FLAG_COPY:
1383 if (src_has_wcard || dest_has_wcard) {
1384 /* No wildcards. */
1385 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1386 } else {
1387 status = copy_internals(ctx, conn, req, oldname,
1388 newname, attrs);
1390 break;
1391 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1392 status = NT_STATUS_INVALID_PARAMETER;
1393 break;
1394 default:
1395 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1396 break;
1399 if (!NT_STATUS_IS_OK(status)) {
1400 if (open_was_deferred(req->mid)) {
1401 /* We have re-scheduled this call. */
1402 END_PROFILE(SMBntrename);
1403 return;
1406 reply_nterror(req, status);
1407 END_PROFILE(SMBntrename);
1408 return;
1411 reply_outbuf(req, 0, 0);
1413 END_PROFILE(SMBntrename);
1414 return;
1417 /****************************************************************************
1418 Reply to a notify change - queue the request and
1419 don't allow a directory to be opened.
1420 ****************************************************************************/
1422 static void call_nt_transact_notify_change(connection_struct *conn,
1423 struct smb_request *req,
1424 uint16 **ppsetup,
1425 uint32 setup_count,
1426 char **ppparams,
1427 uint32 parameter_count,
1428 char **ppdata, uint32 data_count,
1429 uint32 max_data_count,
1430 uint32 max_param_count)
1432 uint16 *setup = *ppsetup;
1433 files_struct *fsp;
1434 uint32 filter;
1435 NTSTATUS status;
1436 bool recursive;
1438 if(setup_count < 6) {
1439 reply_doserror(req, ERRDOS, ERRbadfunc);
1440 return;
1443 fsp = file_fsp(req, SVAL(setup,4));
1444 filter = IVAL(setup, 0);
1445 recursive = (SVAL(setup, 6) != 0) ? True : False;
1447 DEBUG(3,("call_nt_transact_notify_change\n"));
1449 if(!fsp) {
1450 reply_doserror(req, ERRDOS, ERRbadfid);
1451 return;
1455 char *filter_string;
1457 if (!(filter_string = notify_filter_string(NULL, filter))) {
1458 reply_nterror(req,NT_STATUS_NO_MEMORY);
1459 return;
1462 DEBUG(3,("call_nt_transact_notify_change: notify change "
1463 "called on %s, filter = %s, recursive = %d\n",
1464 fsp->fsp_name, filter_string, recursive));
1466 TALLOC_FREE(filter_string);
1469 if((!fsp->is_directory) || (conn != fsp->conn)) {
1470 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1471 return;
1474 if (fsp->notify == NULL) {
1476 status = change_notify_create(fsp, filter, recursive);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 DEBUG(10, ("change_notify_create returned %s\n",
1480 nt_errstr(status)));
1481 reply_nterror(req, status);
1482 return;
1486 if (fsp->notify->num_changes != 0) {
1489 * We've got changes pending, respond immediately
1493 * TODO: write a torture test to check the filtering behaviour
1494 * here.
1497 change_notify_reply(fsp->conn, req, max_param_count,
1498 fsp->notify);
1501 * change_notify_reply() above has independently sent its
1502 * results
1504 return;
1508 * No changes pending, queue the request
1511 status = change_notify_add_request(req,
1512 max_param_count,
1513 filter,
1514 recursive, fsp);
1515 if (!NT_STATUS_IS_OK(status)) {
1516 reply_nterror(req, status);
1518 return;
1521 /****************************************************************************
1522 Reply to an NT transact rename command.
1523 ****************************************************************************/
1525 static void call_nt_transact_rename(connection_struct *conn,
1526 struct smb_request *req,
1527 uint16 **ppsetup, uint32 setup_count,
1528 char **ppparams, uint32 parameter_count,
1529 char **ppdata, uint32 data_count,
1530 uint32 max_data_count)
1532 char *params = *ppparams;
1533 char *new_name = NULL;
1534 files_struct *fsp = NULL;
1535 bool dest_has_wcard = False;
1536 NTSTATUS status;
1537 TALLOC_CTX *ctx = talloc_tos();
1539 if(parameter_count < 5) {
1540 reply_doserror(req, ERRDOS, ERRbadfunc);
1541 return;
1544 fsp = file_fsp(req, SVAL(params, 0));
1545 if (!check_fsp(conn, req, fsp)) {
1546 return;
1548 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1549 parameter_count - 4,
1550 STR_TERMINATE, &status, &dest_has_wcard);
1551 if (!NT_STATUS_IS_OK(status)) {
1552 reply_nterror(req, status);
1553 return;
1557 * W2K3 ignores this request as the RAW-RENAME test
1558 * demonstrates, so we do.
1560 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1562 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1563 fsp->fsp_name, new_name));
1565 return;
1568 /******************************************************************************
1569 Fake up a completely empty SD.
1570 *******************************************************************************/
1572 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1574 size_t sd_size;
1576 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1577 if(!*ppsd) {
1578 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1579 return NT_STATUS_NO_MEMORY;
1582 return NT_STATUS_OK;
1585 /****************************************************************************
1586 Reply to query a security descriptor.
1587 ****************************************************************************/
1589 static void call_nt_transact_query_security_desc(connection_struct *conn,
1590 struct smb_request *req,
1591 uint16 **ppsetup,
1592 uint32 setup_count,
1593 char **ppparams,
1594 uint32 parameter_count,
1595 char **ppdata,
1596 uint32 data_count,
1597 uint32 max_data_count)
1599 char *params = *ppparams;
1600 char *data = *ppdata;
1601 SEC_DESC *psd = NULL;
1602 size_t sd_size;
1603 uint32 security_info_wanted;
1604 files_struct *fsp = NULL;
1605 NTSTATUS status;
1606 DATA_BLOB blob;
1608 if(parameter_count < 8) {
1609 reply_doserror(req, ERRDOS, ERRbadfunc);
1610 return;
1613 fsp = file_fsp(req, SVAL(params,0));
1614 if(!fsp) {
1615 reply_doserror(req, ERRDOS, ERRbadfid);
1616 return;
1619 security_info_wanted = IVAL(params,4);
1621 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1622 (unsigned int)security_info_wanted ));
1624 params = nttrans_realloc(ppparams, 4);
1625 if(params == NULL) {
1626 reply_doserror(req, ERRDOS, ERRnomem);
1627 return;
1631 * Get the permissions to return.
1634 if (!lp_nt_acl_support(SNUM(conn))) {
1635 status = get_null_nt_acl(talloc_tos(), &psd);
1636 } else {
1637 status = SMB_VFS_FGET_NT_ACL(
1638 fsp, security_info_wanted, &psd);
1640 if (!NT_STATUS_IS_OK(status)) {
1641 reply_nterror(req, status);
1642 return;
1645 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1646 * present in the reply to match Windows behavior */
1647 if (psd->sacl == NULL &&
1648 security_info_wanted & SACL_SECURITY_INFORMATION)
1649 psd->type |= SEC_DESC_SACL_PRESENT;
1650 if (psd->dacl == NULL &&
1651 security_info_wanted & DACL_SECURITY_INFORMATION)
1652 psd->type |= SEC_DESC_DACL_PRESENT;
1654 sd_size = ndr_size_security_descriptor(psd, 0);
1656 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1658 if (DEBUGLEVEL >= 10) {
1659 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1660 NDR_PRINT_DEBUG(security_descriptor, psd);
1663 SIVAL(params,0,(uint32)sd_size);
1665 if (max_data_count < sd_size) {
1666 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1667 params, 4, *ppdata, 0);
1668 return;
1672 * Allocate the data we will point this at.
1675 data = nttrans_realloc(ppdata, sd_size);
1676 if(data == NULL) {
1677 reply_doserror(req, ERRDOS, ERRnomem);
1678 return;
1681 status = marshall_sec_desc(talloc_tos(), psd,
1682 &blob.data, &blob.length);
1684 if (!NT_STATUS_IS_OK(status)) {
1685 reply_nterror(req, status);
1686 return;
1689 SMB_ASSERT(sd_size == blob.length);
1690 memcpy(data, blob.data, sd_size);
1692 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1694 return;
1697 /****************************************************************************
1698 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1699 ****************************************************************************/
1701 static void call_nt_transact_set_security_desc(connection_struct *conn,
1702 struct smb_request *req,
1703 uint16 **ppsetup,
1704 uint32 setup_count,
1705 char **ppparams,
1706 uint32 parameter_count,
1707 char **ppdata,
1708 uint32 data_count,
1709 uint32 max_data_count)
1711 char *params= *ppparams;
1712 char *data = *ppdata;
1713 files_struct *fsp = NULL;
1714 uint32 security_info_sent = 0;
1715 NTSTATUS status;
1717 if(parameter_count < 8) {
1718 reply_doserror(req, ERRDOS, ERRbadfunc);
1719 return;
1722 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1723 reply_doserror(req, ERRDOS, ERRbadfid);
1724 return;
1727 if(!lp_nt_acl_support(SNUM(conn))) {
1728 goto done;
1731 security_info_sent = IVAL(params,4);
1733 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1734 (unsigned int)security_info_sent ));
1736 if (data_count == 0) {
1737 reply_doserror(req, ERRDOS, ERRnoaccess);
1738 return;
1741 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1743 if (!NT_STATUS_IS_OK(status)) {
1744 reply_nterror(req, status);
1745 return;
1748 done:
1749 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1750 return;
1753 /****************************************************************************
1754 Reply to NT IOCTL
1755 ****************************************************************************/
1757 static void call_nt_transact_ioctl(connection_struct *conn,
1758 struct smb_request *req,
1759 uint16 **ppsetup, uint32 setup_count,
1760 char **ppparams, uint32 parameter_count,
1761 char **ppdata, uint32 data_count,
1762 uint32 max_data_count)
1764 uint32 function;
1765 uint16 fidnum;
1766 files_struct *fsp;
1767 uint8 isFSctl;
1768 uint8 compfilter;
1769 static bool logged_message;
1770 char *pdata = *ppdata;
1772 if (setup_count != 8) {
1773 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1774 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1775 return;
1778 function = IVAL(*ppsetup, 0);
1779 fidnum = SVAL(*ppsetup, 4);
1780 isFSctl = CVAL(*ppsetup, 6);
1781 compfilter = CVAL(*ppsetup, 7);
1783 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1784 function, fidnum, isFSctl, compfilter));
1786 fsp=file_fsp(req, fidnum);
1787 /* this check is done in each implemented function case for now
1788 because I don't want to break anything... --metze
1789 FSP_BELONGS_CONN(fsp,conn);*/
1791 switch (function) {
1792 case FSCTL_SET_SPARSE:
1793 /* pretend this succeeded - tho strictly we should
1794 mark the file sparse (if the local fs supports it)
1795 so we can know if we need to pre-allocate or not */
1797 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1798 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1799 return;
1801 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1803 unsigned char objid[16];
1805 /* This should return the object-id on this file.
1806 * I think I'll make this be the inode+dev. JRA.
1809 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1811 if (!fsp_belongs_conn(conn, req, fsp)) {
1812 return;
1815 data_count = 64;
1816 pdata = nttrans_realloc(ppdata, data_count);
1817 if (pdata == NULL) {
1818 reply_nterror(req, NT_STATUS_NO_MEMORY);
1819 return;
1821 push_file_id_16(pdata, &fsp->file_id);
1822 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1823 push_file_id_16(pdata+32, &fsp->file_id);
1824 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1825 pdata, data_count);
1826 return;
1829 case FSCTL_GET_REPARSE_POINT:
1830 /* pretend this fail - my winXP does it like this
1831 * --metze
1834 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1835 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1836 return;
1838 case FSCTL_SET_REPARSE_POINT:
1839 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1840 * --metze
1843 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1844 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1845 return;
1847 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1850 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1851 * and return their volume names. If max_data_count is 16, then it is just
1852 * asking for the number of volumes and length of the combined names.
1854 * pdata is the data allocated by our caller, but that uses
1855 * total_data_count (which is 0 in our case) rather than max_data_count.
1856 * Allocate the correct amount and return the pointer to let
1857 * it be deallocated when we return.
1859 SHADOW_COPY_DATA *shadow_data = NULL;
1860 TALLOC_CTX *shadow_mem_ctx = NULL;
1861 bool labels = False;
1862 uint32 labels_data_count = 0;
1863 uint32 i;
1864 char *cur_pdata;
1866 if (!fsp_belongs_conn(conn, req, fsp)) {
1867 return;
1870 if (max_data_count < 16) {
1871 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1872 max_data_count));
1873 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1874 return;
1877 if (max_data_count > 16) {
1878 labels = True;
1881 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1882 if (shadow_mem_ctx == NULL) {
1883 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1884 reply_nterror(req, NT_STATUS_NO_MEMORY);
1885 return;
1888 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1889 if (shadow_data == NULL) {
1890 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1891 talloc_destroy(shadow_mem_ctx);
1892 reply_nterror(req, NT_STATUS_NO_MEMORY);
1893 return;
1896 shadow_data->mem_ctx = shadow_mem_ctx;
1899 * Call the VFS routine to actually do the work.
1901 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1902 talloc_destroy(shadow_data->mem_ctx);
1903 if (errno == ENOSYS) {
1904 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1905 conn->connectpath));
1906 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1907 return;
1908 } else {
1909 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1910 conn->connectpath));
1911 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1912 return;
1916 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1918 if (!labels) {
1919 data_count = 16;
1920 } else {
1921 data_count = 12+labels_data_count+4;
1924 if (max_data_count<data_count) {
1925 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1926 max_data_count,data_count));
1927 talloc_destroy(shadow_data->mem_ctx);
1928 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1929 return;
1932 pdata = nttrans_realloc(ppdata, data_count);
1933 if (pdata == NULL) {
1934 talloc_destroy(shadow_data->mem_ctx);
1935 reply_nterror(req, NT_STATUS_NO_MEMORY);
1936 return;
1939 cur_pdata = pdata;
1941 /* num_volumes 4 bytes */
1942 SIVAL(pdata,0,shadow_data->num_volumes);
1944 if (labels) {
1945 /* num_labels 4 bytes */
1946 SIVAL(pdata,4,shadow_data->num_volumes);
1949 /* needed_data_count 4 bytes */
1950 SIVAL(pdata,8,labels_data_count);
1952 cur_pdata+=12;
1954 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1955 shadow_data->num_volumes,fsp->fsp_name));
1956 if (labels && shadow_data->labels) {
1957 for (i=0;i<shadow_data->num_volumes;i++) {
1958 srvstr_push(pdata, req->flags2,
1959 cur_pdata, shadow_data->labels[i],
1960 2*sizeof(SHADOW_COPY_LABEL),
1961 STR_UNICODE|STR_TERMINATE);
1962 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1963 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1967 talloc_destroy(shadow_data->mem_ctx);
1969 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1970 pdata, data_count);
1972 return;
1975 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1977 /* pretend this succeeded -
1979 * we have to send back a list with all files owned by this SID
1981 * but I have to check that --metze
1983 DOM_SID sid;
1984 uid_t uid;
1985 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1987 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1989 if (!fsp_belongs_conn(conn, req, fsp)) {
1990 return;
1993 /* unknown 4 bytes: this is not the length of the sid :-( */
1994 /*unknown = IVAL(pdata,0);*/
1996 sid_parse(pdata+4,sid_len,&sid);
1997 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1999 if (!sid_to_uid(&sid, &uid)) {
2000 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2001 sid_string_dbg(&sid),
2002 (unsigned long)sid_len));
2003 uid = (-1);
2006 /* we can take a look at the find source :-)
2008 * find ./ -uid $uid -name '*' is what we need here
2011 * and send 4bytes len and then NULL terminated unicode strings
2012 * for each file
2014 * but I don't know how to deal with the paged results
2015 * (maybe we can hang the result anywhere in the fsp struct)
2017 * we don't send all files at once
2018 * and at the next we should *not* start from the beginning,
2019 * so we have to cache the result
2021 * --metze
2024 /* this works for now... */
2025 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2026 return;
2028 default:
2029 if (!logged_message) {
2030 logged_message = True; /* Only print this once... */
2031 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2032 function));
2036 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2040 #ifdef HAVE_SYS_QUOTAS
2041 /****************************************************************************
2042 Reply to get user quota
2043 ****************************************************************************/
2045 static void call_nt_transact_get_user_quota(connection_struct *conn,
2046 struct smb_request *req,
2047 uint16 **ppsetup,
2048 uint32 setup_count,
2049 char **ppparams,
2050 uint32 parameter_count,
2051 char **ppdata,
2052 uint32 data_count,
2053 uint32 max_data_count)
2055 NTSTATUS nt_status = NT_STATUS_OK;
2056 char *params = *ppparams;
2057 char *pdata = *ppdata;
2058 char *entry;
2059 int data_len=0,param_len=0;
2060 int qt_len=0;
2061 int entry_len = 0;
2062 files_struct *fsp = NULL;
2063 uint16 level = 0;
2064 size_t sid_len;
2065 DOM_SID sid;
2066 bool start_enum = True;
2067 SMB_NTQUOTA_STRUCT qt;
2068 SMB_NTQUOTA_LIST *tmp_list;
2069 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2071 ZERO_STRUCT(qt);
2073 /* access check */
2074 if (conn->server_info->utok.uid != 0) {
2075 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2076 "[%s]\n", lp_servicename(SNUM(conn)),
2077 conn->server_info->unix_name));
2078 reply_doserror(req, ERRDOS, ERRnoaccess);
2079 return;
2083 * Ensure minimum number of parameters sent.
2086 if (parameter_count < 4) {
2087 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2088 reply_doserror(req, ERRDOS, ERRinvalidparam);
2089 return;
2092 /* maybe we can check the quota_fnum */
2093 fsp = file_fsp(req, SVAL(params,0));
2094 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2095 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2096 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2097 return;
2100 /* the NULL pointer checking for fsp->fake_file_handle->pd
2101 * is done by CHECK_NTQUOTA_HANDLE_OK()
2103 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2105 level = SVAL(params,2);
2107 /* unknown 12 bytes leading in params */
2109 switch (level) {
2110 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2111 /* seems that we should continue with the enum here --metze */
2113 if (qt_handle->quota_list!=NULL &&
2114 qt_handle->tmp_list==NULL) {
2116 /* free the list */
2117 free_ntquota_list(&(qt_handle->quota_list));
2119 /* Realloc the size of parameters and data we will return */
2120 param_len = 4;
2121 params = nttrans_realloc(ppparams, param_len);
2122 if(params == NULL) {
2123 reply_doserror(req, ERRDOS, ERRnomem);
2124 return;
2127 data_len = 0;
2128 SIVAL(params,0,data_len);
2130 break;
2133 start_enum = False;
2135 case TRANSACT_GET_USER_QUOTA_LIST_START:
2137 if (qt_handle->quota_list==NULL &&
2138 qt_handle->tmp_list==NULL) {
2139 start_enum = True;
2142 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2143 reply_doserror(req, ERRSRV, ERRerror);
2144 return;
2147 /* Realloc the size of parameters and data we will return */
2148 param_len = 4;
2149 params = nttrans_realloc(ppparams, param_len);
2150 if(params == NULL) {
2151 reply_doserror(req, ERRDOS, ERRnomem);
2152 return;
2155 /* we should not trust the value in max_data_count*/
2156 max_data_count = MIN(max_data_count,2048);
2158 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2159 if(pdata == NULL) {
2160 reply_doserror(req, ERRDOS, ERRnomem);
2161 return;
2164 entry = pdata;
2166 /* set params Size of returned Quota Data 4 bytes*/
2167 /* but set it later when we know it */
2169 /* for each entry push the data */
2171 if (start_enum) {
2172 qt_handle->tmp_list = qt_handle->quota_list;
2175 tmp_list = qt_handle->tmp_list;
2177 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2178 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2180 sid_len = ndr_size_dom_sid(
2181 &tmp_list->quotas->sid, 0);
2182 entry_len = 40 + sid_len;
2184 /* nextoffset entry 4 bytes */
2185 SIVAL(entry,0,entry_len);
2187 /* then the len of the SID 4 bytes */
2188 SIVAL(entry,4,sid_len);
2190 /* unknown data 8 bytes uint64_t */
2191 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2193 /* the used disk space 8 bytes uint64_t */
2194 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2196 /* the soft quotas 8 bytes uint64_t */
2197 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2199 /* the hard quotas 8 bytes uint64_t */
2200 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2202 /* and now the SID */
2203 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2206 qt_handle->tmp_list = tmp_list;
2208 /* overwrite the offset of the last entry */
2209 SIVAL(entry-entry_len,0,0);
2211 data_len = 4+qt_len;
2212 /* overwrite the params quota_data_len */
2213 SIVAL(params,0,data_len);
2215 break;
2217 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2219 /* unknown 4 bytes IVAL(pdata,0) */
2221 if (data_count < 8) {
2222 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2223 reply_doserror(req, ERRDOS, ERRunknownlevel);
2224 return;
2227 sid_len = IVAL(pdata,4);
2228 /* Ensure this is less than 1mb. */
2229 if (sid_len > (1024*1024)) {
2230 reply_doserror(req, ERRDOS, ERRnomem);
2231 return;
2234 if (data_count < 8+sid_len) {
2235 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2236 reply_doserror(req, ERRDOS, ERRunknownlevel);
2237 return;
2240 data_len = 4+40+sid_len;
2242 if (max_data_count < data_len) {
2243 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2244 max_data_count, data_len));
2245 param_len = 4;
2246 SIVAL(params,0,data_len);
2247 data_len = 0;
2248 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2249 break;
2252 sid_parse(pdata+8,sid_len,&sid);
2254 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2255 ZERO_STRUCT(qt);
2257 * we have to return zero's in all fields
2258 * instead of returning an error here
2259 * --metze
2263 /* Realloc the size of parameters and data we will return */
2264 param_len = 4;
2265 params = nttrans_realloc(ppparams, param_len);
2266 if(params == NULL) {
2267 reply_doserror(req, ERRDOS, ERRnomem);
2268 return;
2271 pdata = nttrans_realloc(ppdata, data_len);
2272 if(pdata == NULL) {
2273 reply_doserror(req, ERRDOS, ERRnomem);
2274 return;
2277 entry = pdata;
2279 /* set params Size of returned Quota Data 4 bytes*/
2280 SIVAL(params,0,data_len);
2282 /* nextoffset entry 4 bytes */
2283 SIVAL(entry,0,0);
2285 /* then the len of the SID 4 bytes */
2286 SIVAL(entry,4,sid_len);
2288 /* unknown data 8 bytes uint64_t */
2289 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2291 /* the used disk space 8 bytes uint64_t */
2292 SBIG_UINT(entry,16,qt.usedspace);
2294 /* the soft quotas 8 bytes uint64_t */
2295 SBIG_UINT(entry,24,qt.softlim);
2297 /* the hard quotas 8 bytes uint64_t */
2298 SBIG_UINT(entry,32,qt.hardlim);
2300 /* and now the SID */
2301 sid_linearize(entry+40, sid_len, &sid);
2303 break;
2305 default:
2306 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2307 reply_doserror(req, ERRSRV, ERRerror);
2308 return;
2309 break;
2312 send_nt_replies(conn, req, nt_status, params, param_len,
2313 pdata, data_len);
2316 /****************************************************************************
2317 Reply to set user quota
2318 ****************************************************************************/
2320 static void call_nt_transact_set_user_quota(connection_struct *conn,
2321 struct smb_request *req,
2322 uint16 **ppsetup,
2323 uint32 setup_count,
2324 char **ppparams,
2325 uint32 parameter_count,
2326 char **ppdata,
2327 uint32 data_count,
2328 uint32 max_data_count)
2330 char *params = *ppparams;
2331 char *pdata = *ppdata;
2332 int data_len=0,param_len=0;
2333 SMB_NTQUOTA_STRUCT qt;
2334 size_t sid_len;
2335 DOM_SID sid;
2336 files_struct *fsp = NULL;
2338 ZERO_STRUCT(qt);
2340 /* access check */
2341 if (conn->server_info->utok.uid != 0) {
2342 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2343 "[%s]\n", lp_servicename(SNUM(conn)),
2344 conn->server_info->unix_name));
2345 reply_doserror(req, ERRDOS, ERRnoaccess);
2346 return;
2350 * Ensure minimum number of parameters sent.
2353 if (parameter_count < 2) {
2354 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2355 reply_doserror(req, ERRDOS, ERRinvalidparam);
2356 return;
2359 /* maybe we can check the quota_fnum */
2360 fsp = file_fsp(req, SVAL(params,0));
2361 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2362 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2363 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2364 return;
2367 if (data_count < 40) {
2368 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2369 reply_doserror(req, ERRDOS, ERRunknownlevel);
2370 return;
2373 /* offset to next quota record.
2374 * 4 bytes IVAL(pdata,0)
2375 * unused here...
2378 /* sid len */
2379 sid_len = IVAL(pdata,4);
2381 if (data_count < 40+sid_len) {
2382 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2383 reply_doserror(req, ERRDOS, ERRunknownlevel);
2384 return;
2387 /* unknown 8 bytes in pdata
2388 * maybe its the change time in NTTIME
2391 /* the used space 8 bytes (uint64_t)*/
2392 qt.usedspace = (uint64_t)IVAL(pdata,16);
2393 #ifdef LARGE_SMB_OFF_T
2394 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2395 #else /* LARGE_SMB_OFF_T */
2396 if ((IVAL(pdata,20) != 0)&&
2397 ((qt.usedspace != 0xFFFFFFFF)||
2398 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2399 /* more than 32 bits? */
2400 reply_doserror(req, ERRDOS, ERRunknownlevel);
2401 return;
2403 #endif /* LARGE_SMB_OFF_T */
2405 /* the soft quotas 8 bytes (uint64_t)*/
2406 qt.softlim = (uint64_t)IVAL(pdata,24);
2407 #ifdef LARGE_SMB_OFF_T
2408 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2409 #else /* LARGE_SMB_OFF_T */
2410 if ((IVAL(pdata,28) != 0)&&
2411 ((qt.softlim != 0xFFFFFFFF)||
2412 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2413 /* more than 32 bits? */
2414 reply_doserror(req, ERRDOS, ERRunknownlevel);
2415 return;
2417 #endif /* LARGE_SMB_OFF_T */
2419 /* the hard quotas 8 bytes (uint64_t)*/
2420 qt.hardlim = (uint64_t)IVAL(pdata,32);
2421 #ifdef LARGE_SMB_OFF_T
2422 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2423 #else /* LARGE_SMB_OFF_T */
2424 if ((IVAL(pdata,36) != 0)&&
2425 ((qt.hardlim != 0xFFFFFFFF)||
2426 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2427 /* more than 32 bits? */
2428 reply_doserror(req, ERRDOS, ERRunknownlevel);
2429 return;
2431 #endif /* LARGE_SMB_OFF_T */
2433 sid_parse(pdata+40,sid_len,&sid);
2434 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2436 /* 44 unknown bytes left... */
2438 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2439 reply_doserror(req, ERRSRV, ERRerror);
2440 return;
2443 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2444 pdata, data_len);
2446 #endif /* HAVE_SYS_QUOTAS */
2448 static void handle_nttrans(connection_struct *conn,
2449 struct trans_state *state,
2450 struct smb_request *req)
2452 if (Protocol >= PROTOCOL_NT1) {
2453 req->flags2 |= 0x40; /* IS_LONG_NAME */
2454 SSVAL(req->inbuf,smb_flg2,req->flags2);
2457 /* Now we must call the relevant NT_TRANS function */
2458 switch(state->call) {
2459 case NT_TRANSACT_CREATE:
2461 START_PROFILE(NT_transact_create);
2462 call_nt_transact_create(
2463 conn, req,
2464 &state->setup, state->setup_count,
2465 &state->param, state->total_param,
2466 &state->data, state->total_data,
2467 state->max_data_return);
2468 END_PROFILE(NT_transact_create);
2469 break;
2472 case NT_TRANSACT_IOCTL:
2474 START_PROFILE(NT_transact_ioctl);
2475 call_nt_transact_ioctl(
2476 conn, req,
2477 &state->setup, state->setup_count,
2478 &state->param, state->total_param,
2479 &state->data, state->total_data,
2480 state->max_data_return);
2481 END_PROFILE(NT_transact_ioctl);
2482 break;
2485 case NT_TRANSACT_SET_SECURITY_DESC:
2487 START_PROFILE(NT_transact_set_security_desc);
2488 call_nt_transact_set_security_desc(
2489 conn, req,
2490 &state->setup, state->setup_count,
2491 &state->param, state->total_param,
2492 &state->data, state->total_data,
2493 state->max_data_return);
2494 END_PROFILE(NT_transact_set_security_desc);
2495 break;
2498 case NT_TRANSACT_NOTIFY_CHANGE:
2500 START_PROFILE(NT_transact_notify_change);
2501 call_nt_transact_notify_change(
2502 conn, req,
2503 &state->setup, state->setup_count,
2504 &state->param, state->total_param,
2505 &state->data, state->total_data,
2506 state->max_data_return,
2507 state->max_param_return);
2508 END_PROFILE(NT_transact_notify_change);
2509 break;
2512 case NT_TRANSACT_RENAME:
2514 START_PROFILE(NT_transact_rename);
2515 call_nt_transact_rename(
2516 conn, req,
2517 &state->setup, state->setup_count,
2518 &state->param, state->total_param,
2519 &state->data, state->total_data,
2520 state->max_data_return);
2521 END_PROFILE(NT_transact_rename);
2522 break;
2525 case NT_TRANSACT_QUERY_SECURITY_DESC:
2527 START_PROFILE(NT_transact_query_security_desc);
2528 call_nt_transact_query_security_desc(
2529 conn, req,
2530 &state->setup, state->setup_count,
2531 &state->param, state->total_param,
2532 &state->data, state->total_data,
2533 state->max_data_return);
2534 END_PROFILE(NT_transact_query_security_desc);
2535 break;
2538 #ifdef HAVE_SYS_QUOTAS
2539 case NT_TRANSACT_GET_USER_QUOTA:
2541 START_PROFILE(NT_transact_get_user_quota);
2542 call_nt_transact_get_user_quota(
2543 conn, req,
2544 &state->setup, state->setup_count,
2545 &state->param, state->total_param,
2546 &state->data, state->total_data,
2547 state->max_data_return);
2548 END_PROFILE(NT_transact_get_user_quota);
2549 break;
2552 case NT_TRANSACT_SET_USER_QUOTA:
2554 START_PROFILE(NT_transact_set_user_quota);
2555 call_nt_transact_set_user_quota(
2556 conn, req,
2557 &state->setup, state->setup_count,
2558 &state->param, state->total_param,
2559 &state->data, state->total_data,
2560 state->max_data_return);
2561 END_PROFILE(NT_transact_set_user_quota);
2562 break;
2564 #endif /* HAVE_SYS_QUOTAS */
2566 default:
2567 /* Error in request */
2568 DEBUG(0,("handle_nttrans: Unknown request %d in "
2569 "nttrans call\n", state->call));
2570 reply_doserror(req, ERRSRV, ERRerror);
2571 return;
2573 return;
2576 /****************************************************************************
2577 Reply to a SMBNTtrans.
2578 ****************************************************************************/
2580 void reply_nttrans(struct smb_request *req)
2582 connection_struct *conn = req->conn;
2583 uint32_t pscnt;
2584 uint32_t psoff;
2585 uint32_t dscnt;
2586 uint32_t dsoff;
2587 uint16 function_code;
2588 NTSTATUS result;
2589 struct trans_state *state;
2591 START_PROFILE(SMBnttrans);
2593 if (req->wct < 19) {
2594 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2595 END_PROFILE(SMBnttrans);
2596 return;
2599 pscnt = IVAL(req->vwv+9, 1);
2600 psoff = IVAL(req->vwv+11, 1);
2601 dscnt = IVAL(req->vwv+13, 1);
2602 dsoff = IVAL(req->vwv+15, 1);
2603 function_code = SVAL(req->vwv+18, 0);
2605 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2606 reply_doserror(req, ERRSRV, ERRaccess);
2607 END_PROFILE(SMBnttrans);
2608 return;
2611 result = allow_new_trans(conn->pending_trans, req->mid);
2612 if (!NT_STATUS_IS_OK(result)) {
2613 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2614 reply_nterror(req, result);
2615 END_PROFILE(SMBnttrans);
2616 return;
2619 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2620 reply_doserror(req, ERRSRV, ERRaccess);
2621 END_PROFILE(SMBnttrans);
2622 return;
2625 state->cmd = SMBnttrans;
2627 state->mid = req->mid;
2628 state->vuid = req->vuid;
2629 state->total_data = IVAL(req->vwv+3, 1);
2630 state->data = NULL;
2631 state->total_param = IVAL(req->vwv+1, 1);
2632 state->param = NULL;
2633 state->max_data_return = IVAL(req->vwv+7, 1);
2634 state->max_param_return = IVAL(req->vwv+5, 1);
2636 /* setup count is in *words* */
2637 state->setup_count = 2*CVAL(req->vwv+17, 1);
2638 state->setup = NULL;
2639 state->call = function_code;
2641 DEBUG(10, ("num_setup=%u, "
2642 "param_total=%u, this_param=%u, max_param=%u, "
2643 "data_total=%u, this_data=%u, max_data=%u, "
2644 "param_offset=%u, data_offset=%u\n",
2645 (unsigned)state->setup_count,
2646 (unsigned)state->total_param, (unsigned)pscnt,
2647 (unsigned)state->max_param_return,
2648 (unsigned)state->total_data, (unsigned)dscnt,
2649 (unsigned)state->max_data_return,
2650 (unsigned)psoff, (unsigned)dsoff));
2653 * All nttrans messages we handle have smb_wct == 19 +
2654 * state->setup_count. Ensure this is so as a sanity check.
2657 if(req->wct != 19 + (state->setup_count/2)) {
2658 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2659 req->wct, 19 + (state->setup_count/2)));
2660 goto bad_param;
2663 /* Don't allow more than 128mb for each value. */
2664 if ((state->total_data > (1024*1024*128)) ||
2665 (state->total_param > (1024*1024*128))) {
2666 reply_doserror(req, ERRDOS, ERRnomem);
2667 END_PROFILE(SMBnttrans);
2668 return;
2671 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2672 goto bad_param;
2674 if (state->total_data) {
2676 if (trans_oob(state->total_data, 0, dscnt)
2677 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2678 goto bad_param;
2681 /* Can't use talloc here, the core routines do realloc on the
2682 * params and data. */
2683 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2684 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2685 "bytes !\n", (unsigned int)state->total_data));
2686 TALLOC_FREE(state);
2687 reply_doserror(req, ERRDOS, ERRnomem);
2688 END_PROFILE(SMBnttrans);
2689 return;
2692 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2695 if (state->total_param) {
2697 if (trans_oob(state->total_param, 0, pscnt)
2698 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2699 goto bad_param;
2702 /* Can't use talloc here, the core routines do realloc on the
2703 * params and data. */
2704 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2705 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2706 "bytes !\n", (unsigned int)state->total_param));
2707 SAFE_FREE(state->data);
2708 TALLOC_FREE(state);
2709 reply_doserror(req, ERRDOS, ERRnomem);
2710 END_PROFILE(SMBnttrans);
2711 return;
2714 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2717 state->received_data = dscnt;
2718 state->received_param = pscnt;
2720 if(state->setup_count > 0) {
2721 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2722 state->setup_count));
2725 * No overflow possible here, state->setup_count is an
2726 * unsigned int, being filled by a single byte from
2727 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2728 * below is not necessary, it's here to clarify things. The
2729 * validity of req->vwv and req->wct has been checked in
2730 * init_smb_request already.
2732 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2733 goto bad_param;
2736 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2737 if (state->setup == NULL) {
2738 DEBUG(0,("reply_nttrans : Out of memory\n"));
2739 SAFE_FREE(state->data);
2740 SAFE_FREE(state->param);
2741 TALLOC_FREE(state);
2742 reply_doserror(req, ERRDOS, ERRnomem);
2743 END_PROFILE(SMBnttrans);
2744 return;
2747 memcpy(state->setup, req->vwv+19, state->setup_count);
2748 dump_data(10, (uint8 *)state->setup, state->setup_count);
2751 if ((state->received_data == state->total_data) &&
2752 (state->received_param == state->total_param)) {
2753 handle_nttrans(conn, state, req);
2754 SAFE_FREE(state->param);
2755 SAFE_FREE(state->data);
2756 TALLOC_FREE(state);
2757 END_PROFILE(SMBnttrans);
2758 return;
2761 DLIST_ADD(conn->pending_trans, state);
2763 /* We need to send an interim response then receive the rest
2764 of the parameter/data bytes */
2765 reply_outbuf(req, 0, 0);
2766 show_msg((char *)req->outbuf);
2767 END_PROFILE(SMBnttrans);
2768 return;
2770 bad_param:
2772 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2773 SAFE_FREE(state->data);
2774 SAFE_FREE(state->param);
2775 TALLOC_FREE(state);
2776 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2777 END_PROFILE(SMBnttrans);
2778 return;
2781 /****************************************************************************
2782 Reply to a SMBnttranss
2783 ****************************************************************************/
2785 void reply_nttranss(struct smb_request *req)
2787 connection_struct *conn = req->conn;
2788 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2789 struct trans_state *state;
2791 START_PROFILE(SMBnttranss);
2793 show_msg((char *)req->inbuf);
2795 if (req->wct < 18) {
2796 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2797 END_PROFILE(SMBnttranss);
2798 return;
2801 for (state = conn->pending_trans; state != NULL;
2802 state = state->next) {
2803 if (state->mid == req->mid) {
2804 break;
2808 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2809 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2810 END_PROFILE(SMBnttranss);
2811 return;
2814 /* Revise state->total_param and state->total_data in case they have
2815 changed downwards */
2816 if (IVAL(req->vwv+1, 1) < state->total_param) {
2817 state->total_param = IVAL(req->vwv+1, 1);
2819 if (IVAL(req->vwv+3, 1) < state->total_data) {
2820 state->total_data = IVAL(req->vwv+3, 1);
2823 pcnt = IVAL(req->vwv+5, 1);
2824 poff = IVAL(req->vwv+7, 1);
2825 pdisp = IVAL(req->vwv+9, 1);
2827 dcnt = IVAL(req->vwv+11, 1);
2828 doff = IVAL(req->vwv+13, 1);
2829 ddisp = IVAL(req->vwv+15, 1);
2831 state->received_param += pcnt;
2832 state->received_data += dcnt;
2834 if ((state->received_data > state->total_data) ||
2835 (state->received_param > state->total_param))
2836 goto bad_param;
2838 if (pcnt) {
2839 if (trans_oob(state->total_param, pdisp, pcnt)
2840 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2841 goto bad_param;
2843 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2846 if (dcnt) {
2847 if (trans_oob(state->total_data, ddisp, dcnt)
2848 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2849 goto bad_param;
2851 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2854 if ((state->received_param < state->total_param) ||
2855 (state->received_data < state->total_data)) {
2856 END_PROFILE(SMBnttranss);
2857 return;
2860 handle_nttrans(conn, state, req);
2862 DLIST_REMOVE(conn->pending_trans, state);
2863 SAFE_FREE(state->data);
2864 SAFE_FREE(state->param);
2865 TALLOC_FREE(state);
2866 END_PROFILE(SMBnttranss);
2867 return;
2869 bad_param:
2871 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2872 DLIST_REMOVE(conn->pending_trans, state);
2873 SAFE_FREE(state->data);
2874 SAFE_FREE(state->param);
2875 TALLOC_FREE(state);
2876 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2877 END_PROFILE(SMBnttranss);
2878 return;