Use "vwv" in trans parsing
[Samba/gebeck_regimport.git] / source3 / smbd / nttrans.c
blobf711b588c5ec1c9b76aefb61168bb234097bdc33
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 * Set total params and data to be sent.
126 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
127 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
130 * Calculate how many parameters and data we can fit into
131 * this packet. Parameters get precedence.
134 params_sent_thistime = MIN(params_to_send,useable_space);
135 data_sent_thistime = useable_space - params_sent_thistime;
136 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
138 SIVAL(req->outbuf, smb_ntr_ParameterCount,
139 params_sent_thistime);
141 if(params_sent_thistime == 0) {
142 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
143 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
144 } else {
146 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
147 * parameter bytes, however the first 4 bytes of outbuf are
148 * the Netbios over TCP header. Thus use smb_base() to subtract
149 * them from the calculation.
152 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
153 ((smb_buf(req->outbuf)+alignment_offset)
154 - smb_base(req->outbuf)));
156 * Absolute displacement of param bytes sent in this packet.
159 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
160 pp - params);
164 * Deal with the data portion.
167 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
169 if(data_sent_thistime == 0) {
170 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
171 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
172 } else {
174 * The offset of the data bytes is the offset of the
175 * parameter bytes plus the number of parameters being sent this time.
178 SIVAL(req->outbuf, smb_ntr_DataOffset,
179 ((smb_buf(req->outbuf)+alignment_offset) -
180 smb_base(req->outbuf))
181 + params_sent_thistime + data_alignment_offset);
182 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
186 * Copy the param bytes into the packet.
189 if(params_sent_thistime) {
190 if (alignment_offset != 0) {
191 memset(smb_buf(req->outbuf), 0,
192 alignment_offset);
194 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
195 params_sent_thistime);
199 * Copy in the data bytes
202 if(data_sent_thistime) {
203 if (data_alignment_offset != 0) {
204 memset((smb_buf(req->outbuf)+alignment_offset+
205 params_sent_thistime), 0,
206 data_alignment_offset);
208 memcpy(smb_buf(req->outbuf)+alignment_offset
209 +params_sent_thistime+data_alignment_offset,
210 pd,data_sent_thistime);
213 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
214 params_sent_thistime, data_sent_thistime, useable_space));
215 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
216 params_to_send, data_to_send, paramsize, datasize));
218 if (NT_STATUS_V(nt_error)) {
219 error_packet_set((char *)req->outbuf,
220 0, 0, nt_error,
221 __LINE__,__FILE__);
224 /* Send the packet */
225 show_msg((char *)req->outbuf);
226 if (!srv_send_smb(smbd_server_fd(),
227 (char *)req->outbuf,
228 IS_CONN_ENCRYPTED(conn))) {
229 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
232 TALLOC_FREE(req->outbuf);
234 pp += params_sent_thistime;
235 pd += data_sent_thistime;
237 params_to_send -= params_sent_thistime;
238 data_to_send -= data_sent_thistime;
241 * Sanity check
244 if(params_to_send < 0 || data_to_send < 0) {
245 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
246 params_to_send, data_to_send));
247 exit_server_cleanly("send_nt_replies: internal error");
252 /****************************************************************************
253 Is it an NTFS stream name ?
254 An NTFS file name is <path>.<extention>:<stream name>:<stream type>
255 $DATA can be used as both a stream name and a stream type. A missing stream
256 name or type implies $DATA.
257 ****************************************************************************/
259 bool is_ntfs_stream_name(const char *fname)
261 if (lp_posix_pathnames()) {
262 return False;
264 return (strchr_m(fname, ':') != NULL) ? True : False;
267 /****************************************************************************
268 Reply to an NT create and X call on a pipe
269 ****************************************************************************/
271 static void nt_open_pipe(char *fname, connection_struct *conn,
272 struct smb_request *req, int *ppnum)
274 files_struct *fsp;
275 NTSTATUS status;
277 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
279 /* Strip \\ off the name. */
280 fname++;
282 status = np_open(req, conn, fname, &fsp);
283 if (!NT_STATUS_IS_OK(status)) {
284 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
285 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
286 ERRDOS, ERRbadpipe);
287 return;
289 reply_nterror(req, status);
290 return;
293 *ppnum = fsp->fnum;
294 return;
297 /****************************************************************************
298 Reply to an NT create and X call for pipes.
299 ****************************************************************************/
301 static void do_ntcreate_pipe_open(connection_struct *conn,
302 struct smb_request *req)
304 char *fname = NULL;
305 int pnum = -1;
306 char *p = NULL;
307 uint32 flags = IVAL(req->vwv+3, 1);
308 TALLOC_CTX *ctx = talloc_tos();
310 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
312 if (!fname) {
313 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
314 ERRDOS, ERRbadpipe);
315 return;
317 nt_open_pipe(fname, conn, req, &pnum);
319 if (req->outbuf) {
320 /* error reply */
321 return;
325 * Deal with pipe return.
328 if (flags & EXTENDED_RESPONSE_REQUIRED) {
329 /* This is very strange. We
330 * return 50 words, but only set
331 * the wcnt to 42 ? It's definately
332 * what happens on the wire....
334 reply_outbuf(req, 50, 0);
335 SCVAL(req->outbuf,smb_wct,42);
336 } else {
337 reply_outbuf(req, 34, 0);
340 p = (char *)req->outbuf + smb_vwv2;
341 p++;
342 SSVAL(p,0,pnum);
343 p += 2;
344 SIVAL(p,0,FILE_WAS_OPENED);
345 p += 4;
346 p += 32;
347 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
348 p += 20;
349 /* File type. */
350 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
351 /* Device state. */
352 SSVAL(p,2, 0x5FF); /* ? */
353 p += 4;
355 if (flags & EXTENDED_RESPONSE_REQUIRED) {
356 p += 25;
357 SIVAL(p,0,FILE_GENERIC_ALL);
359 * For pipes W2K3 seems to return
360 * 0x12019B next.
361 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
363 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
366 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
368 chain_reply(req);
371 /****************************************************************************
372 Reply to an NT create and X call.
373 ****************************************************************************/
375 void reply_ntcreate_and_X(struct smb_request *req)
377 connection_struct *conn = req->conn;
378 char *fname = NULL;
379 uint32 flags;
380 uint32 access_mask;
381 uint32 file_attributes;
382 uint32 share_access;
383 uint32 create_disposition;
384 uint32 create_options;
385 uint16 root_dir_fid;
386 uint64_t allocation_size;
387 /* Breakout the oplock request bits so we can set the
388 reply bits separately. */
389 uint32 fattr=0;
390 SMB_OFF_T file_len = 0;
391 SMB_STRUCT_STAT sbuf;
392 int info = 0;
393 files_struct *fsp = NULL;
394 char *p = NULL;
395 struct timespec c_timespec;
396 struct timespec a_timespec;
397 struct timespec m_timespec;
398 NTSTATUS status;
399 int oplock_request;
400 uint8_t oplock_granted = NO_OPLOCK_RETURN;
401 TALLOC_CTX *ctx = talloc_tos();
403 START_PROFILE(SMBntcreateX);
405 if (req->wct < 24) {
406 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
407 return;
410 flags = IVAL(req->vwv+3, 1);
411 access_mask = IVAL(req->vwv+7, 1);
412 file_attributes = IVAL(req->vwv+13, 1);
413 share_access = IVAL(req->vwv+15, 1);
414 create_disposition = IVAL(req->vwv+17, 1);
415 create_options = IVAL(req->vwv+19, 1);
416 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
418 allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
419 #ifdef LARGE_SMB_OFF_T
420 allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
421 #endif
423 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
424 STR_TERMINATE, &status);
426 if (!NT_STATUS_IS_OK(status)) {
427 reply_nterror(req, status);
428 END_PROFILE(SMBntcreateX);
429 return;
432 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
433 "file_attributes = 0x%x, share_access = 0x%x, "
434 "create_disposition = 0x%x create_options = 0x%x "
435 "root_dir_fid = 0x%x, fname = %s\n",
436 (unsigned int)flags,
437 (unsigned int)access_mask,
438 (unsigned int)file_attributes,
439 (unsigned int)share_access,
440 (unsigned int)create_disposition,
441 (unsigned int)create_options,
442 (unsigned int)root_dir_fid,
443 fname));
446 * we need to remove ignored bits when they come directly from the client
447 * because we reuse some of them for internal stuff
449 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
452 * If it's an IPC, use the pipe handler.
455 if (IS_IPC(conn)) {
456 if (lp_nt_pipe_support()) {
457 do_ntcreate_pipe_open(conn, req);
458 END_PROFILE(SMBntcreateX);
459 return;
461 reply_doserror(req, ERRDOS, ERRnoaccess);
462 END_PROFILE(SMBntcreateX);
463 return;
466 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
467 if (oplock_request) {
468 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
469 ? BATCH_OPLOCK : 0;
472 status = create_file(conn, req, root_dir_fid, fname,
473 access_mask, share_access, create_disposition,
474 create_options, file_attributes, oplock_request,
475 allocation_size, NULL, NULL, &fsp, &info, &sbuf);
477 if (!NT_STATUS_IS_OK(status)) {
478 if (open_was_deferred(req->mid)) {
479 /* We have re-scheduled this call, no error. */
480 END_PROFILE(SMBntcreateX);
481 return;
483 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
484 reply_botherror(req, status, ERRDOS, ERRfilexists);
486 else {
487 reply_nterror(req, status);
489 END_PROFILE(SMBntcreateX);
490 return;
494 * If the caller set the extended oplock request bit
495 * and we granted one (by whatever means) - set the
496 * correct bit for extended oplock reply.
499 if (oplock_request &&
500 (lp_fake_oplocks(SNUM(conn))
501 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
504 * Exclusive oplock granted
507 if (flags & REQUEST_BATCH_OPLOCK) {
508 oplock_granted = BATCH_OPLOCK_RETURN;
509 } else {
510 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
512 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
513 oplock_granted = LEVEL_II_OPLOCK_RETURN;
514 } else {
515 oplock_granted = NO_OPLOCK_RETURN;
518 file_len = sbuf.st_size;
519 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
520 if (fattr == 0) {
521 fattr = FILE_ATTRIBUTE_NORMAL;
524 if (flags & EXTENDED_RESPONSE_REQUIRED) {
525 /* This is very strange. We
526 * return 50 words, but only set
527 * the wcnt to 42 ? It's definately
528 * what happens on the wire....
530 reply_outbuf(req, 50, 0);
531 SCVAL(req->outbuf,smb_wct,42);
532 } else {
533 reply_outbuf(req, 34, 0);
536 p = (char *)req->outbuf + smb_vwv2;
538 SCVAL(p, 0, oplock_granted);
540 p++;
541 SSVAL(p,0,fsp->fnum);
542 p += 2;
543 if ((create_disposition == FILE_SUPERSEDE)
544 && (info == FILE_WAS_OVERWRITTEN)) {
545 SIVAL(p,0,FILE_WAS_SUPERSEDED);
546 } else {
547 SIVAL(p,0,info);
549 p += 4;
551 /* Create time. */
552 c_timespec = get_create_timespec(
553 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
554 a_timespec = get_atimespec(&sbuf);
555 m_timespec = get_mtimespec(&sbuf);
557 if (lp_dos_filetime_resolution(SNUM(conn))) {
558 dos_filetime_timespec(&c_timespec);
559 dos_filetime_timespec(&a_timespec);
560 dos_filetime_timespec(&m_timespec);
563 put_long_date_timespec(p, c_timespec); /* create time. */
564 p += 8;
565 put_long_date_timespec(p, a_timespec); /* access time */
566 p += 8;
567 put_long_date_timespec(p, m_timespec); /* write time */
568 p += 8;
569 put_long_date_timespec(p, m_timespec); /* change time */
570 p += 8;
571 SIVAL(p,0,fattr); /* File Attributes. */
572 p += 4;
573 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
574 p += 8;
575 SOFF_T(p,0,file_len);
576 p += 8;
577 if (flags & EXTENDED_RESPONSE_REQUIRED) {
578 SSVAL(p,2,0x7);
580 p += 4;
581 SCVAL(p,0,fsp->is_directory ? 1 : 0);
583 if (flags & EXTENDED_RESPONSE_REQUIRED) {
584 uint32 perms = 0;
585 p += 25;
586 if (fsp->is_directory
587 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
588 perms = FILE_GENERIC_ALL;
589 } else {
590 perms = FILE_GENERIC_READ|FILE_EXECUTE;
592 SIVAL(p,0,perms);
595 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
596 fsp->fnum, fsp->fsp_name));
598 chain_reply(req);
599 END_PROFILE(SMBntcreateX);
600 return;
603 /****************************************************************************
604 Reply to a NT_TRANSACT_CREATE call to open a pipe.
605 ****************************************************************************/
607 static void do_nt_transact_create_pipe(connection_struct *conn,
608 struct smb_request *req,
609 uint16 **ppsetup, uint32 setup_count,
610 char **ppparams, uint32 parameter_count,
611 char **ppdata, uint32 data_count)
613 char *fname = NULL;
614 char *params = *ppparams;
615 int pnum = -1;
616 char *p = NULL;
617 NTSTATUS status;
618 size_t param_len;
619 uint32 flags;
620 TALLOC_CTX *ctx = talloc_tos();
623 * Ensure minimum number of parameters sent.
626 if(parameter_count < 54) {
627 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
628 reply_doserror(req, ERRDOS, ERRnoaccess);
629 return;
632 flags = IVAL(params,0);
634 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
635 parameter_count-53, STR_TERMINATE,
636 &status);
637 if (!NT_STATUS_IS_OK(status)) {
638 reply_nterror(req, status);
639 return;
642 nt_open_pipe(fname, conn, req, &pnum);
644 if (req->outbuf) {
645 /* Error return */
646 return;
649 /* Realloc the size of parameters and data we will return */
650 if (flags & EXTENDED_RESPONSE_REQUIRED) {
651 /* Extended response is 32 more byyes. */
652 param_len = 101;
653 } else {
654 param_len = 69;
656 params = nttrans_realloc(ppparams, param_len);
657 if(params == NULL) {
658 reply_doserror(req, ERRDOS, ERRnomem);
659 return;
662 p = params;
663 SCVAL(p,0,NO_OPLOCK_RETURN);
665 p += 2;
666 SSVAL(p,0,pnum);
667 p += 2;
668 SIVAL(p,0,FILE_WAS_OPENED);
669 p += 8;
671 p += 32;
672 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
673 p += 20;
674 /* File type. */
675 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
676 /* Device state. */
677 SSVAL(p,2, 0x5FF); /* ? */
678 p += 4;
680 if (flags & EXTENDED_RESPONSE_REQUIRED) {
681 p += 25;
682 SIVAL(p,0,FILE_GENERIC_ALL);
684 * For pipes W2K3 seems to return
685 * 0x12019B next.
686 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
688 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
691 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
693 /* Send the required number of replies */
694 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
696 return;
699 /****************************************************************************
700 Internal fn to set security descriptors.
701 ****************************************************************************/
703 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
704 uint32 security_info_sent)
706 SEC_DESC *psd = NULL;
707 NTSTATUS status;
709 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
710 return NT_STATUS_OK;
713 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
715 if (!NT_STATUS_IS_OK(status)) {
716 return status;
719 if (psd->owner_sid==0) {
720 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
722 if (psd->group_sid==0) {
723 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
725 if (psd->sacl==0) {
726 security_info_sent &= ~SACL_SECURITY_INFORMATION;
728 if (psd->dacl==0) {
729 security_info_sent &= ~DACL_SECURITY_INFORMATION;
732 /* Convert all the generic bits. */
733 security_acl_map_generic(psd->dacl, &file_generic_mapping);
734 security_acl_map_generic(psd->sacl, &file_generic_mapping);
736 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
738 TALLOC_FREE(psd);
740 return status;
743 /****************************************************************************
744 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
745 ****************************************************************************/
747 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
749 struct ea_list *ea_list_head = NULL;
750 size_t offset = 0;
752 if (data_size < 4) {
753 return NULL;
756 while (offset + 4 <= data_size) {
757 size_t next_offset = IVAL(pdata,offset);
758 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
760 if (!eal) {
761 return NULL;
764 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
765 if (next_offset == 0) {
766 break;
768 offset += next_offset;
771 return ea_list_head;
774 /****************************************************************************
775 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
776 ****************************************************************************/
778 static void call_nt_transact_create(connection_struct *conn,
779 struct smb_request *req,
780 uint16 **ppsetup, uint32 setup_count,
781 char **ppparams, uint32 parameter_count,
782 char **ppdata, uint32 data_count,
783 uint32 max_data_count)
785 char *fname = NULL;
786 char *params = *ppparams;
787 char *data = *ppdata;
788 /* Breakout the oplock request bits so we can set the reply bits separately. */
789 uint32 fattr=0;
790 SMB_OFF_T file_len = 0;
791 SMB_STRUCT_STAT sbuf;
792 int info = 0;
793 files_struct *fsp = NULL;
794 char *p = NULL;
795 uint32 flags;
796 uint32 access_mask;
797 uint32 file_attributes;
798 uint32 share_access;
799 uint32 create_disposition;
800 uint32 create_options;
801 uint32 sd_len;
802 struct security_descriptor *sd = NULL;
803 uint32 ea_len;
804 uint16 root_dir_fid;
805 struct timespec c_timespec;
806 struct timespec a_timespec;
807 struct timespec m_timespec;
808 struct ea_list *ea_list = NULL;
809 NTSTATUS status;
810 size_t param_len;
811 uint64_t allocation_size;
812 int oplock_request;
813 uint8_t oplock_granted;
814 TALLOC_CTX *ctx = talloc_tos();
816 DEBUG(5,("call_nt_transact_create\n"));
819 * If it's an IPC, use the pipe handler.
822 if (IS_IPC(conn)) {
823 if (lp_nt_pipe_support()) {
824 do_nt_transact_create_pipe(
825 conn, req,
826 ppsetup, setup_count,
827 ppparams, parameter_count,
828 ppdata, data_count);
829 return;
831 reply_doserror(req, ERRDOS, ERRnoaccess);
832 return;
836 * Ensure minimum number of parameters sent.
839 if(parameter_count < 54) {
840 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
841 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
842 return;
845 flags = IVAL(params,0);
846 access_mask = IVAL(params,8);
847 file_attributes = IVAL(params,20);
848 share_access = IVAL(params,24);
849 create_disposition = IVAL(params,28);
850 create_options = IVAL(params,32);
851 sd_len = IVAL(params,36);
852 ea_len = IVAL(params,40);
853 root_dir_fid = (uint16)IVAL(params,4);
854 allocation_size = (uint64_t)IVAL(params,12);
855 #ifdef LARGE_SMB_OFF_T
856 allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
857 #endif
860 * we need to remove ignored bits when they come directly from the client
861 * because we reuse some of them for internal stuff
863 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
865 /* Ensure the data_len is correct for the sd and ea values given. */
866 if ((ea_len + sd_len > data_count)
867 || (ea_len > data_count) || (sd_len > data_count)
868 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
869 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
870 "%u, data_count = %u\n", (unsigned int)ea_len,
871 (unsigned int)sd_len, (unsigned int)data_count));
872 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
873 return;
876 if (sd_len) {
877 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
878 sd_len));
880 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
881 &sd);
882 if (!NT_STATUS_IS_OK(status)) {
883 DEBUG(10, ("call_nt_transact_create: "
884 "unmarshall_sec_desc failed: %s\n",
885 nt_errstr(status)));
886 reply_nterror(req, status);
887 return;
891 if (ea_len) {
892 if (!lp_ea_support(SNUM(conn))) {
893 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
894 "EA's not supported.\n",
895 (unsigned int)ea_len));
896 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
897 return;
900 if (ea_len < 10) {
901 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
902 "too small (should be more than 10)\n",
903 (unsigned int)ea_len ));
904 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
905 return;
908 /* We have already checked that ea_len <= data_count here. */
909 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
910 ea_len);
911 if (ea_list == NULL) {
912 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
913 return;
917 srvstr_get_path(ctx, params, req->flags2, &fname,
918 params+53, parameter_count-53,
919 STR_TERMINATE, &status);
920 if (!NT_STATUS_IS_OK(status)) {
921 reply_nterror(req, status);
922 return;
925 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
926 if (oplock_request) {
927 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
928 ? BATCH_OPLOCK : 0;
931 status = create_file(conn, req, root_dir_fid, fname,
932 access_mask, share_access, create_disposition,
933 create_options, file_attributes, oplock_request,
934 allocation_size, sd, ea_list, &fsp, &info, &sbuf);
936 if(!NT_STATUS_IS_OK(status)) {
937 if (open_was_deferred(req->mid)) {
938 /* We have re-scheduled this call, no error. */
939 return;
941 reply_openerror(req, status);
942 return;
946 * If the caller set the extended oplock request bit
947 * and we granted one (by whatever means) - set the
948 * correct bit for extended oplock reply.
951 if (oplock_request &&
952 (lp_fake_oplocks(SNUM(conn))
953 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
956 * Exclusive oplock granted
959 if (flags & REQUEST_BATCH_OPLOCK) {
960 oplock_granted = BATCH_OPLOCK_RETURN;
961 } else {
962 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
964 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
965 oplock_granted = LEVEL_II_OPLOCK_RETURN;
966 } else {
967 oplock_granted = NO_OPLOCK_RETURN;
970 file_len = sbuf.st_size;
971 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
972 if (fattr == 0) {
973 fattr = FILE_ATTRIBUTE_NORMAL;
976 /* Realloc the size of parameters and data we will return */
977 if (flags & EXTENDED_RESPONSE_REQUIRED) {
978 /* Extended response is 32 more byyes. */
979 param_len = 101;
980 } else {
981 param_len = 69;
983 params = nttrans_realloc(ppparams, param_len);
984 if(params == NULL) {
985 reply_doserror(req, ERRDOS, ERRnomem);
986 return;
989 p = params;
990 SCVAL(p, 0, oplock_granted);
992 p += 2;
993 SSVAL(p,0,fsp->fnum);
994 p += 2;
995 if ((create_disposition == FILE_SUPERSEDE)
996 && (info == FILE_WAS_OVERWRITTEN)) {
997 SIVAL(p,0,FILE_WAS_SUPERSEDED);
998 } else {
999 SIVAL(p,0,info);
1001 p += 8;
1003 /* Create time. */
1004 c_timespec = get_create_timespec(
1005 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1006 a_timespec = get_atimespec(&sbuf);
1007 m_timespec = get_mtimespec(&sbuf);
1009 if (lp_dos_filetime_resolution(SNUM(conn))) {
1010 dos_filetime_timespec(&c_timespec);
1011 dos_filetime_timespec(&a_timespec);
1012 dos_filetime_timespec(&m_timespec);
1015 put_long_date_timespec(p, c_timespec); /* create time. */
1016 p += 8;
1017 put_long_date_timespec(p, a_timespec); /* access time */
1018 p += 8;
1019 put_long_date_timespec(p, m_timespec); /* write time */
1020 p += 8;
1021 put_long_date_timespec(p, m_timespec); /* change time */
1022 p += 8;
1023 SIVAL(p,0,fattr); /* File Attributes. */
1024 p += 4;
1025 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1026 p += 8;
1027 SOFF_T(p,0,file_len);
1028 p += 8;
1029 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1030 SSVAL(p,2,0x7);
1032 p += 4;
1033 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1035 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1036 uint32 perms = 0;
1037 p += 25;
1038 if (fsp->is_directory
1039 || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
1040 perms = FILE_GENERIC_ALL;
1041 } else {
1042 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1044 SIVAL(p,0,perms);
1047 DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
1049 /* Send the required number of replies */
1050 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1052 return;
1055 /****************************************************************************
1056 Reply to a NT CANCEL request.
1057 conn POINTER CAN BE NULL HERE !
1058 ****************************************************************************/
1060 void reply_ntcancel(struct smb_request *req)
1063 * Go through and cancel any pending change notifies.
1066 START_PROFILE(SMBntcancel);
1067 remove_pending_change_notify_requests_by_mid(req->mid);
1068 remove_pending_lock_requests_by_mid(req->mid);
1069 srv_cancel_sign_response(req->mid);
1071 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1073 END_PROFILE(SMBntcancel);
1074 return;
1077 /****************************************************************************
1078 Copy a file.
1079 ****************************************************************************/
1081 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1082 connection_struct *conn,
1083 struct smb_request *req,
1084 const char *oldname_in,
1085 const char *newname_in,
1086 uint32 attrs)
1088 SMB_STRUCT_STAT sbuf1, sbuf2;
1089 char *oldname = NULL;
1090 char *newname = NULL;
1091 char *last_component_oldname = NULL;
1092 char *last_component_newname = NULL;
1093 files_struct *fsp1,*fsp2;
1094 uint32 fattr;
1095 int info;
1096 SMB_OFF_T ret=-1;
1097 NTSTATUS status = NT_STATUS_OK;
1099 ZERO_STRUCT(sbuf1);
1100 ZERO_STRUCT(sbuf2);
1102 if (!CAN_WRITE(conn)) {
1103 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1106 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1107 &last_component_oldname, &sbuf1);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 return status;
1112 status = check_name(conn, oldname);
1113 if (!NT_STATUS_IS_OK(status)) {
1114 return status;
1117 /* Source must already exist. */
1118 if (!VALID_STAT(sbuf1)) {
1119 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1121 /* Ensure attributes match. */
1122 fattr = dos_mode(conn,oldname,&sbuf1);
1123 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1124 return NT_STATUS_NO_SUCH_FILE;
1127 status = unix_convert(ctx, conn, newname_in, False, &newname,
1128 &last_component_newname, &sbuf2);
1129 if (!NT_STATUS_IS_OK(status)) {
1130 return status;
1133 status = check_name(conn, newname);
1134 if (!NT_STATUS_IS_OK(status)) {
1135 return status;
1138 /* Disallow if newname already exists. */
1139 if (VALID_STAT(sbuf2)) {
1140 return NT_STATUS_OBJECT_NAME_COLLISION;
1143 /* No links from a directory. */
1144 if (S_ISDIR(sbuf1.st_mode)) {
1145 return NT_STATUS_FILE_IS_A_DIRECTORY;
1148 /* Ensure this is within the share. */
1149 status = check_reduced_name(conn, oldname);
1150 if (!NT_STATUS_IS_OK(status)) {
1151 return status;
1154 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1155 oldname, newname));
1157 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1158 FILE_READ_DATA, /* Read-only. */
1159 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1160 FILE_OPEN,
1161 0, /* No create options. */
1162 FILE_ATTRIBUTE_NORMAL,
1163 NO_OPLOCK,
1164 &info, &fsp1);
1166 if (!NT_STATUS_IS_OK(status)) {
1167 return status;
1170 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1171 FILE_WRITE_DATA, /* Read-only. */
1172 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1173 FILE_CREATE,
1174 0, /* No create options. */
1175 fattr,
1176 NO_OPLOCK,
1177 &info, &fsp2);
1179 if (!NT_STATUS_IS_OK(status)) {
1180 close_file(NULL, fsp1, ERROR_CLOSE);
1181 return status;
1184 if (sbuf1.st_size) {
1185 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1189 * As we are opening fsp1 read-only we only expect
1190 * an error on close on fsp2 if we are out of space.
1191 * Thus we don't look at the error return from the
1192 * close of fsp1.
1194 close_file(NULL, fsp1, NORMAL_CLOSE);
1196 /* Ensure the modtime is set correctly on the destination file. */
1197 set_close_write_time(fsp2, get_mtimespec(&sbuf1));
1199 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1201 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1202 creates the file. This isn't the correct thing to do in the copy
1203 case. JRA */
1204 file_set_dosmode(conn, newname, fattr, &sbuf2,
1205 parent_dirname(newname),false);
1207 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1208 return NT_STATUS_DISK_FULL;
1211 if (!NT_STATUS_IS_OK(status)) {
1212 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1213 nt_errstr(status), oldname, newname));
1215 return status;
1218 /****************************************************************************
1219 Reply to a NT rename request.
1220 ****************************************************************************/
1222 void reply_ntrename(struct smb_request *req)
1224 connection_struct *conn = req->conn;
1225 char *oldname = NULL;
1226 char *newname = NULL;
1227 const char *p;
1228 NTSTATUS status;
1229 bool src_has_wcard = False;
1230 bool dest_has_wcard = False;
1231 uint32 attrs;
1232 uint16 rename_type;
1233 TALLOC_CTX *ctx = talloc_tos();
1235 START_PROFILE(SMBntrename);
1237 if (req->wct < 4) {
1238 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1239 END_PROFILE(SMBntrename);
1240 return;
1243 attrs = SVAL(req->vwv+0, 0);
1244 rename_type = SVAL(req->vwv+1, 0);
1246 p = (const char *)req->buf + 1;
1247 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1248 &status, &src_has_wcard);
1249 if (!NT_STATUS_IS_OK(status)) {
1250 reply_nterror(req, status);
1251 END_PROFILE(SMBntrename);
1252 return;
1255 if( is_ntfs_stream_name(oldname)) {
1256 /* Can't rename a stream. */
1257 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1258 END_PROFILE(SMBntrename);
1259 return;
1262 if (ms_has_wild(oldname)) {
1263 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1264 END_PROFILE(SMBntrename);
1265 return;
1268 p++;
1269 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1270 &status, &dest_has_wcard);
1271 if (!NT_STATUS_IS_OK(status)) {
1272 reply_nterror(req, status);
1273 END_PROFILE(SMBntrename);
1274 return;
1277 status = resolve_dfspath(ctx, conn,
1278 req->flags2 & FLAGS2_DFS_PATHNAMES,
1279 oldname,
1280 &oldname);
1281 if (!NT_STATUS_IS_OK(status)) {
1282 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1283 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1284 ERRSRV, ERRbadpath);
1285 END_PROFILE(SMBntrename);
1286 return;
1288 reply_nterror(req, status);
1289 END_PROFILE(SMBntrename);
1290 return;
1293 status = resolve_dfspath(ctx, conn,
1294 req->flags2 & FLAGS2_DFS_PATHNAMES,
1295 newname,
1296 &newname);
1297 if (!NT_STATUS_IS_OK(status)) {
1298 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1299 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1300 ERRSRV, ERRbadpath);
1301 END_PROFILE(SMBntrename);
1302 return;
1304 reply_nterror(req, status);
1305 END_PROFILE(SMBntrename);
1306 return;
1309 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1311 switch(rename_type) {
1312 case RENAME_FLAG_RENAME:
1313 status = rename_internals(ctx, conn, req, oldname,
1314 newname, attrs, False, src_has_wcard,
1315 dest_has_wcard, DELETE_ACCESS);
1316 break;
1317 case RENAME_FLAG_HARD_LINK:
1318 if (src_has_wcard || dest_has_wcard) {
1319 /* No wildcards. */
1320 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1321 } else {
1322 status = hardlink_internals(ctx,
1323 conn,
1324 oldname,
1325 newname);
1327 break;
1328 case RENAME_FLAG_COPY:
1329 if (src_has_wcard || dest_has_wcard) {
1330 /* No wildcards. */
1331 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1332 } else {
1333 status = copy_internals(ctx, conn, req, oldname,
1334 newname, attrs);
1336 break;
1337 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1338 status = NT_STATUS_INVALID_PARAMETER;
1339 break;
1340 default:
1341 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1342 break;
1345 if (!NT_STATUS_IS_OK(status)) {
1346 if (open_was_deferred(req->mid)) {
1347 /* We have re-scheduled this call. */
1348 END_PROFILE(SMBntrename);
1349 return;
1352 reply_nterror(req, status);
1353 END_PROFILE(SMBntrename);
1354 return;
1357 reply_outbuf(req, 0, 0);
1359 END_PROFILE(SMBntrename);
1360 return;
1363 /****************************************************************************
1364 Reply to a notify change - queue the request and
1365 don't allow a directory to be opened.
1366 ****************************************************************************/
1368 static void call_nt_transact_notify_change(connection_struct *conn,
1369 struct smb_request *req,
1370 uint16 **ppsetup,
1371 uint32 setup_count,
1372 char **ppparams,
1373 uint32 parameter_count,
1374 char **ppdata, uint32 data_count,
1375 uint32 max_data_count,
1376 uint32 max_param_count)
1378 uint16 *setup = *ppsetup;
1379 files_struct *fsp;
1380 uint32 filter;
1381 NTSTATUS status;
1382 bool recursive;
1384 if(setup_count < 6) {
1385 reply_doserror(req, ERRDOS, ERRbadfunc);
1386 return;
1389 fsp = file_fsp(req, SVAL(setup,4));
1390 filter = IVAL(setup, 0);
1391 recursive = (SVAL(setup, 6) != 0) ? True : False;
1393 DEBUG(3,("call_nt_transact_notify_change\n"));
1395 if(!fsp) {
1396 reply_doserror(req, ERRDOS, ERRbadfid);
1397 return;
1401 char *filter_string;
1403 if (!(filter_string = notify_filter_string(NULL, filter))) {
1404 reply_nterror(req,NT_STATUS_NO_MEMORY);
1405 return;
1408 DEBUG(3,("call_nt_transact_notify_change: notify change "
1409 "called on %s, filter = %s, recursive = %d\n",
1410 fsp->fsp_name, filter_string, recursive));
1412 TALLOC_FREE(filter_string);
1415 if((!fsp->is_directory) || (conn != fsp->conn)) {
1416 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1417 return;
1420 if (fsp->notify == NULL) {
1422 status = change_notify_create(fsp, filter, recursive);
1424 if (!NT_STATUS_IS_OK(status)) {
1425 DEBUG(10, ("change_notify_create returned %s\n",
1426 nt_errstr(status)));
1427 reply_nterror(req, status);
1428 return;
1432 if (fsp->notify->num_changes != 0) {
1435 * We've got changes pending, respond immediately
1439 * TODO: write a torture test to check the filtering behaviour
1440 * here.
1443 change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
1446 * change_notify_reply() above has independently sent its
1447 * results
1449 return;
1453 * No changes pending, queue the request
1456 status = change_notify_add_request(req,
1457 max_param_count,
1458 filter,
1459 recursive, fsp);
1460 if (!NT_STATUS_IS_OK(status)) {
1461 reply_nterror(req, status);
1463 return;
1466 /****************************************************************************
1467 Reply to an NT transact rename command.
1468 ****************************************************************************/
1470 static void call_nt_transact_rename(connection_struct *conn,
1471 struct smb_request *req,
1472 uint16 **ppsetup, uint32 setup_count,
1473 char **ppparams, uint32 parameter_count,
1474 char **ppdata, uint32 data_count,
1475 uint32 max_data_count)
1477 char *params = *ppparams;
1478 char *new_name = NULL;
1479 files_struct *fsp = NULL;
1480 bool dest_has_wcard = False;
1481 NTSTATUS status;
1482 TALLOC_CTX *ctx = talloc_tos();
1484 if(parameter_count < 5) {
1485 reply_doserror(req, ERRDOS, ERRbadfunc);
1486 return;
1489 fsp = file_fsp(req, SVAL(params, 0));
1490 if (!check_fsp(conn, req, fsp)) {
1491 return;
1493 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1494 parameter_count - 4,
1495 STR_TERMINATE, &status, &dest_has_wcard);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 reply_nterror(req, status);
1498 return;
1502 * W2K3 ignores this request as the RAW-RENAME test
1503 * demonstrates, so we do.
1505 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1507 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1508 fsp->fsp_name, new_name));
1510 return;
1513 /******************************************************************************
1514 Fake up a completely empty SD.
1515 *******************************************************************************/
1517 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1519 size_t sd_size;
1521 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1522 if(!*ppsd) {
1523 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1524 return NT_STATUS_NO_MEMORY;
1527 return NT_STATUS_OK;
1530 /****************************************************************************
1531 Reply to query a security descriptor.
1532 ****************************************************************************/
1534 static void call_nt_transact_query_security_desc(connection_struct *conn,
1535 struct smb_request *req,
1536 uint16 **ppsetup,
1537 uint32 setup_count,
1538 char **ppparams,
1539 uint32 parameter_count,
1540 char **ppdata,
1541 uint32 data_count,
1542 uint32 max_data_count)
1544 char *params = *ppparams;
1545 char *data = *ppdata;
1546 SEC_DESC *psd = NULL;
1547 size_t sd_size;
1548 uint32 security_info_wanted;
1549 files_struct *fsp = NULL;
1550 NTSTATUS status;
1551 DATA_BLOB blob;
1553 if(parameter_count < 8) {
1554 reply_doserror(req, ERRDOS, ERRbadfunc);
1555 return;
1558 fsp = file_fsp(req, SVAL(params,0));
1559 if(!fsp) {
1560 reply_doserror(req, ERRDOS, ERRbadfid);
1561 return;
1564 security_info_wanted = IVAL(params,4);
1566 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1567 (unsigned int)security_info_wanted ));
1569 params = nttrans_realloc(ppparams, 4);
1570 if(params == NULL) {
1571 reply_doserror(req, ERRDOS, ERRnomem);
1572 return;
1576 * Get the permissions to return.
1579 if (!lp_nt_acl_support(SNUM(conn))) {
1580 status = get_null_nt_acl(talloc_tos(), &psd);
1581 } else {
1582 status = SMB_VFS_FGET_NT_ACL(
1583 fsp, security_info_wanted, &psd);
1586 if (!NT_STATUS_IS_OK(status)) {
1587 reply_nterror(req, status);
1588 return;
1591 sd_size = ndr_size_security_descriptor(psd, 0);
1593 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1595 SIVAL(params,0,(uint32)sd_size);
1597 if (max_data_count < sd_size) {
1598 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1599 params, 4, *ppdata, 0);
1600 return;
1604 * Allocate the data we will point this at.
1607 data = nttrans_realloc(ppdata, sd_size);
1608 if(data == NULL) {
1609 reply_doserror(req, ERRDOS, ERRnomem);
1610 return;
1613 status = marshall_sec_desc(talloc_tos(), psd,
1614 &blob.data, &blob.length);
1616 if (!NT_STATUS_IS_OK(status)) {
1617 reply_nterror(req, status);
1618 return;
1621 SMB_ASSERT(sd_size == blob.length);
1622 memcpy(data, blob.data, sd_size);
1624 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1626 return;
1629 /****************************************************************************
1630 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1631 ****************************************************************************/
1633 static void call_nt_transact_set_security_desc(connection_struct *conn,
1634 struct smb_request *req,
1635 uint16 **ppsetup,
1636 uint32 setup_count,
1637 char **ppparams,
1638 uint32 parameter_count,
1639 char **ppdata,
1640 uint32 data_count,
1641 uint32 max_data_count)
1643 char *params= *ppparams;
1644 char *data = *ppdata;
1645 files_struct *fsp = NULL;
1646 uint32 security_info_sent = 0;
1647 NTSTATUS status;
1649 if(parameter_count < 8) {
1650 reply_doserror(req, ERRDOS, ERRbadfunc);
1651 return;
1654 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1655 reply_doserror(req, ERRDOS, ERRbadfid);
1656 return;
1659 if(!lp_nt_acl_support(SNUM(conn))) {
1660 goto done;
1663 security_info_sent = IVAL(params,4);
1665 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1666 (unsigned int)security_info_sent ));
1668 if (data_count == 0) {
1669 reply_doserror(req, ERRDOS, ERRnoaccess);
1670 return;
1673 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1675 if (!NT_STATUS_IS_OK(status)) {
1676 reply_nterror(req, status);
1677 return;
1680 done:
1681 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1682 return;
1685 /****************************************************************************
1686 Reply to NT IOCTL
1687 ****************************************************************************/
1689 static void call_nt_transact_ioctl(connection_struct *conn,
1690 struct smb_request *req,
1691 uint16 **ppsetup, uint32 setup_count,
1692 char **ppparams, uint32 parameter_count,
1693 char **ppdata, uint32 data_count,
1694 uint32 max_data_count)
1696 uint32 function;
1697 uint16 fidnum;
1698 files_struct *fsp;
1699 uint8 isFSctl;
1700 uint8 compfilter;
1701 static bool logged_message;
1702 char *pdata = *ppdata;
1704 if (setup_count != 8) {
1705 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1706 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1707 return;
1710 function = IVAL(*ppsetup, 0);
1711 fidnum = SVAL(*ppsetup, 4);
1712 isFSctl = CVAL(*ppsetup, 6);
1713 compfilter = CVAL(*ppsetup, 7);
1715 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1716 function, fidnum, isFSctl, compfilter));
1718 fsp=file_fsp(req, fidnum);
1719 /* this check is done in each implemented function case for now
1720 because I don't want to break anything... --metze
1721 FSP_BELONGS_CONN(fsp,conn);*/
1723 switch (function) {
1724 case FSCTL_SET_SPARSE:
1725 /* pretend this succeeded - tho strictly we should
1726 mark the file sparse (if the local fs supports it)
1727 so we can know if we need to pre-allocate or not */
1729 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1730 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1731 return;
1733 case FSCTL_CREATE_OR_GET_OBJECT_ID:
1735 unsigned char objid[16];
1737 /* This should return the object-id on this file.
1738 * I think I'll make this be the inode+dev. JRA.
1741 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1743 if (!fsp_belongs_conn(conn, req, fsp)) {
1744 return;
1747 data_count = 64;
1748 pdata = nttrans_realloc(ppdata, data_count);
1749 if (pdata == NULL) {
1750 reply_nterror(req, NT_STATUS_NO_MEMORY);
1751 return;
1753 push_file_id_16(pdata, &fsp->file_id);
1754 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1755 push_file_id_16(pdata+32, &fsp->file_id);
1756 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1757 pdata, data_count);
1758 return;
1761 case FSCTL_GET_REPARSE_POINT:
1762 /* pretend this fail - my winXP does it like this
1763 * --metze
1766 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1767 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1768 return;
1770 case FSCTL_SET_REPARSE_POINT:
1771 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1772 * --metze
1775 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1776 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1777 return;
1779 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1782 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1783 * and return their volume names. If max_data_count is 16, then it is just
1784 * asking for the number of volumes and length of the combined names.
1786 * pdata is the data allocated by our caller, but that uses
1787 * total_data_count (which is 0 in our case) rather than max_data_count.
1788 * Allocate the correct amount and return the pointer to let
1789 * it be deallocated when we return.
1791 SHADOW_COPY_DATA *shadow_data = NULL;
1792 TALLOC_CTX *shadow_mem_ctx = NULL;
1793 bool labels = False;
1794 uint32 labels_data_count = 0;
1795 uint32 i;
1796 char *cur_pdata;
1798 if (!fsp_belongs_conn(conn, req, fsp)) {
1799 return;
1802 if (max_data_count < 16) {
1803 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1804 max_data_count));
1805 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1806 return;
1809 if (max_data_count > 16) {
1810 labels = True;
1813 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1814 if (shadow_mem_ctx == NULL) {
1815 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1816 reply_nterror(req, NT_STATUS_NO_MEMORY);
1817 return;
1820 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1821 if (shadow_data == NULL) {
1822 DEBUG(0,("TALLOC_ZERO() failed!\n"));
1823 talloc_destroy(shadow_mem_ctx);
1824 reply_nterror(req, NT_STATUS_NO_MEMORY);
1825 return;
1828 shadow_data->mem_ctx = shadow_mem_ctx;
1831 * Call the VFS routine to actually do the work.
1833 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1834 talloc_destroy(shadow_data->mem_ctx);
1835 if (errno == ENOSYS) {
1836 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1837 conn->connectpath));
1838 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1839 return;
1840 } else {
1841 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1842 conn->connectpath));
1843 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
1844 return;
1848 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1850 if (!labels) {
1851 data_count = 16;
1852 } else {
1853 data_count = 12+labels_data_count+4;
1856 if (max_data_count<data_count) {
1857 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1858 max_data_count,data_count));
1859 talloc_destroy(shadow_data->mem_ctx);
1860 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1861 return;
1864 pdata = nttrans_realloc(ppdata, data_count);
1865 if (pdata == NULL) {
1866 talloc_destroy(shadow_data->mem_ctx);
1867 reply_nterror(req, NT_STATUS_NO_MEMORY);
1868 return;
1871 cur_pdata = pdata;
1873 /* num_volumes 4 bytes */
1874 SIVAL(pdata,0,shadow_data->num_volumes);
1876 if (labels) {
1877 /* num_labels 4 bytes */
1878 SIVAL(pdata,4,shadow_data->num_volumes);
1881 /* needed_data_count 4 bytes */
1882 SIVAL(pdata,8,labels_data_count);
1884 cur_pdata+=12;
1886 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1887 shadow_data->num_volumes,fsp->fsp_name));
1888 if (labels && shadow_data->labels) {
1889 for (i=0;i<shadow_data->num_volumes;i++) {
1890 srvstr_push(pdata, req->flags2,
1891 cur_pdata, shadow_data->labels[i],
1892 2*sizeof(SHADOW_COPY_LABEL),
1893 STR_UNICODE|STR_TERMINATE);
1894 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
1895 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1899 talloc_destroy(shadow_data->mem_ctx);
1901 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1902 pdata, data_count);
1904 return;
1907 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1909 /* pretend this succeeded -
1911 * we have to send back a list with all files owned by this SID
1913 * but I have to check that --metze
1915 DOM_SID sid;
1916 uid_t uid;
1917 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
1919 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
1921 if (!fsp_belongs_conn(conn, req, fsp)) {
1922 return;
1925 /* unknown 4 bytes: this is not the length of the sid :-( */
1926 /*unknown = IVAL(pdata,0);*/
1928 sid_parse(pdata+4,sid_len,&sid);
1929 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1931 if (!sid_to_uid(&sid, &uid)) {
1932 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1933 sid_string_dbg(&sid),
1934 (unsigned long)sid_len));
1935 uid = (-1);
1938 /* we can take a look at the find source :-)
1940 * find ./ -uid $uid -name '*' is what we need here
1943 * and send 4bytes len and then NULL terminated unicode strings
1944 * for each file
1946 * but I don't know how to deal with the paged results
1947 * (maybe we can hang the result anywhere in the fsp struct)
1949 * we don't send all files at once
1950 * and at the next we should *not* start from the beginning,
1951 * so we have to cache the result
1953 * --metze
1956 /* this works for now... */
1957 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1958 return;
1960 default:
1961 if (!logged_message) {
1962 logged_message = True; /* Only print this once... */
1963 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1964 function));
1968 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1972 #ifdef HAVE_SYS_QUOTAS
1973 /****************************************************************************
1974 Reply to get user quota
1975 ****************************************************************************/
1977 static void call_nt_transact_get_user_quota(connection_struct *conn,
1978 struct smb_request *req,
1979 uint16 **ppsetup,
1980 uint32 setup_count,
1981 char **ppparams,
1982 uint32 parameter_count,
1983 char **ppdata,
1984 uint32 data_count,
1985 uint32 max_data_count)
1987 NTSTATUS nt_status = NT_STATUS_OK;
1988 char *params = *ppparams;
1989 char *pdata = *ppdata;
1990 char *entry;
1991 int data_len=0,param_len=0;
1992 int qt_len=0;
1993 int entry_len = 0;
1994 files_struct *fsp = NULL;
1995 uint16 level = 0;
1996 size_t sid_len;
1997 DOM_SID sid;
1998 bool start_enum = True;
1999 SMB_NTQUOTA_STRUCT qt;
2000 SMB_NTQUOTA_LIST *tmp_list;
2001 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2003 ZERO_STRUCT(qt);
2005 /* access check */
2006 if (conn->server_info->utok.uid != 0) {
2007 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2008 "[%s]\n", lp_servicename(SNUM(conn)),
2009 conn->server_info->unix_name));
2010 reply_doserror(req, ERRDOS, ERRnoaccess);
2011 return;
2015 * Ensure minimum number of parameters sent.
2018 if (parameter_count < 4) {
2019 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2020 reply_doserror(req, ERRDOS, ERRinvalidparam);
2021 return;
2024 /* maybe we can check the quota_fnum */
2025 fsp = file_fsp(req, SVAL(params,0));
2026 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2027 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2028 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2029 return;
2032 /* the NULL pointer checking for fsp->fake_file_handle->pd
2033 * is done by CHECK_NTQUOTA_HANDLE_OK()
2035 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2037 level = SVAL(params,2);
2039 /* unknown 12 bytes leading in params */
2041 switch (level) {
2042 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2043 /* seems that we should continue with the enum here --metze */
2045 if (qt_handle->quota_list!=NULL &&
2046 qt_handle->tmp_list==NULL) {
2048 /* free the list */
2049 free_ntquota_list(&(qt_handle->quota_list));
2051 /* Realloc the size of parameters and data we will return */
2052 param_len = 4;
2053 params = nttrans_realloc(ppparams, param_len);
2054 if(params == NULL) {
2055 reply_doserror(req, ERRDOS, ERRnomem);
2056 return;
2059 data_len = 0;
2060 SIVAL(params,0,data_len);
2062 break;
2065 start_enum = False;
2067 case TRANSACT_GET_USER_QUOTA_LIST_START:
2069 if (qt_handle->quota_list==NULL &&
2070 qt_handle->tmp_list==NULL) {
2071 start_enum = True;
2074 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2075 reply_doserror(req, ERRSRV, ERRerror);
2076 return;
2079 /* Realloc the size of parameters and data we will return */
2080 param_len = 4;
2081 params = nttrans_realloc(ppparams, param_len);
2082 if(params == NULL) {
2083 reply_doserror(req, ERRDOS, ERRnomem);
2084 return;
2087 /* we should not trust the value in max_data_count*/
2088 max_data_count = MIN(max_data_count,2048);
2090 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2091 if(pdata == NULL) {
2092 reply_doserror(req, ERRDOS, ERRnomem);
2093 return;
2096 entry = pdata;
2098 /* set params Size of returned Quota Data 4 bytes*/
2099 /* but set it later when we know it */
2101 /* for each entry push the data */
2103 if (start_enum) {
2104 qt_handle->tmp_list = qt_handle->quota_list;
2107 tmp_list = qt_handle->tmp_list;
2109 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2110 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2112 sid_len = ndr_size_dom_sid(
2113 &tmp_list->quotas->sid, 0);
2114 entry_len = 40 + sid_len;
2116 /* nextoffset entry 4 bytes */
2117 SIVAL(entry,0,entry_len);
2119 /* then the len of the SID 4 bytes */
2120 SIVAL(entry,4,sid_len);
2122 /* unknown data 8 bytes uint64_t */
2123 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2125 /* the used disk space 8 bytes uint64_t */
2126 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2128 /* the soft quotas 8 bytes uint64_t */
2129 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2131 /* the hard quotas 8 bytes uint64_t */
2132 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2134 /* and now the SID */
2135 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2138 qt_handle->tmp_list = tmp_list;
2140 /* overwrite the offset of the last entry */
2141 SIVAL(entry-entry_len,0,0);
2143 data_len = 4+qt_len;
2144 /* overwrite the params quota_data_len */
2145 SIVAL(params,0,data_len);
2147 break;
2149 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2151 /* unknown 4 bytes IVAL(pdata,0) */
2153 if (data_count < 8) {
2154 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2155 reply_doserror(req, ERRDOS, ERRunknownlevel);
2156 return;
2159 sid_len = IVAL(pdata,4);
2160 /* Ensure this is less than 1mb. */
2161 if (sid_len > (1024*1024)) {
2162 reply_doserror(req, ERRDOS, ERRnomem);
2163 return;
2166 if (data_count < 8+sid_len) {
2167 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2168 reply_doserror(req, ERRDOS, ERRunknownlevel);
2169 return;
2172 data_len = 4+40+sid_len;
2174 if (max_data_count < data_len) {
2175 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2176 max_data_count, data_len));
2177 param_len = 4;
2178 SIVAL(params,0,data_len);
2179 data_len = 0;
2180 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2181 break;
2184 sid_parse(pdata+8,sid_len,&sid);
2186 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2187 ZERO_STRUCT(qt);
2189 * we have to return zero's in all fields
2190 * instead of returning an error here
2191 * --metze
2195 /* Realloc the size of parameters and data we will return */
2196 param_len = 4;
2197 params = nttrans_realloc(ppparams, param_len);
2198 if(params == NULL) {
2199 reply_doserror(req, ERRDOS, ERRnomem);
2200 return;
2203 pdata = nttrans_realloc(ppdata, data_len);
2204 if(pdata == NULL) {
2205 reply_doserror(req, ERRDOS, ERRnomem);
2206 return;
2209 entry = pdata;
2211 /* set params Size of returned Quota Data 4 bytes*/
2212 SIVAL(params,0,data_len);
2214 /* nextoffset entry 4 bytes */
2215 SIVAL(entry,0,0);
2217 /* then the len of the SID 4 bytes */
2218 SIVAL(entry,4,sid_len);
2220 /* unknown data 8 bytes uint64_t */
2221 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2223 /* the used disk space 8 bytes uint64_t */
2224 SBIG_UINT(entry,16,qt.usedspace);
2226 /* the soft quotas 8 bytes uint64_t */
2227 SBIG_UINT(entry,24,qt.softlim);
2229 /* the hard quotas 8 bytes uint64_t */
2230 SBIG_UINT(entry,32,qt.hardlim);
2232 /* and now the SID */
2233 sid_linearize(entry+40, sid_len, &sid);
2235 break;
2237 default:
2238 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2239 reply_doserror(req, ERRSRV, ERRerror);
2240 return;
2241 break;
2244 send_nt_replies(conn, req, nt_status, params, param_len,
2245 pdata, data_len);
2248 /****************************************************************************
2249 Reply to set user quota
2250 ****************************************************************************/
2252 static void call_nt_transact_set_user_quota(connection_struct *conn,
2253 struct smb_request *req,
2254 uint16 **ppsetup,
2255 uint32 setup_count,
2256 char **ppparams,
2257 uint32 parameter_count,
2258 char **ppdata,
2259 uint32 data_count,
2260 uint32 max_data_count)
2262 char *params = *ppparams;
2263 char *pdata = *ppdata;
2264 int data_len=0,param_len=0;
2265 SMB_NTQUOTA_STRUCT qt;
2266 size_t sid_len;
2267 DOM_SID sid;
2268 files_struct *fsp = NULL;
2270 ZERO_STRUCT(qt);
2272 /* access check */
2273 if (conn->server_info->utok.uid != 0) {
2274 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2275 "[%s]\n", lp_servicename(SNUM(conn)),
2276 conn->server_info->unix_name));
2277 reply_doserror(req, ERRDOS, ERRnoaccess);
2278 return;
2282 * Ensure minimum number of parameters sent.
2285 if (parameter_count < 2) {
2286 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2287 reply_doserror(req, ERRDOS, ERRinvalidparam);
2288 return;
2291 /* maybe we can check the quota_fnum */
2292 fsp = file_fsp(req, SVAL(params,0));
2293 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2294 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2295 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2296 return;
2299 if (data_count < 40) {
2300 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2301 reply_doserror(req, ERRDOS, ERRunknownlevel);
2302 return;
2305 /* offset to next quota record.
2306 * 4 bytes IVAL(pdata,0)
2307 * unused here...
2310 /* sid len */
2311 sid_len = IVAL(pdata,4);
2313 if (data_count < 40+sid_len) {
2314 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2315 reply_doserror(req, ERRDOS, ERRunknownlevel);
2316 return;
2319 /* unknown 8 bytes in pdata
2320 * maybe its the change time in NTTIME
2323 /* the used space 8 bytes (uint64_t)*/
2324 qt.usedspace = (uint64_t)IVAL(pdata,16);
2325 #ifdef LARGE_SMB_OFF_T
2326 qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2327 #else /* LARGE_SMB_OFF_T */
2328 if ((IVAL(pdata,20) != 0)&&
2329 ((qt.usedspace != 0xFFFFFFFF)||
2330 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2331 /* more than 32 bits? */
2332 reply_doserror(req, ERRDOS, ERRunknownlevel);
2333 return;
2335 #endif /* LARGE_SMB_OFF_T */
2337 /* the soft quotas 8 bytes (uint64_t)*/
2338 qt.softlim = (uint64_t)IVAL(pdata,24);
2339 #ifdef LARGE_SMB_OFF_T
2340 qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2341 #else /* LARGE_SMB_OFF_T */
2342 if ((IVAL(pdata,28) != 0)&&
2343 ((qt.softlim != 0xFFFFFFFF)||
2344 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2345 /* more than 32 bits? */
2346 reply_doserror(req, ERRDOS, ERRunknownlevel);
2347 return;
2349 #endif /* LARGE_SMB_OFF_T */
2351 /* the hard quotas 8 bytes (uint64_t)*/
2352 qt.hardlim = (uint64_t)IVAL(pdata,32);
2353 #ifdef LARGE_SMB_OFF_T
2354 qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2355 #else /* LARGE_SMB_OFF_T */
2356 if ((IVAL(pdata,36) != 0)&&
2357 ((qt.hardlim != 0xFFFFFFFF)||
2358 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2359 /* more than 32 bits? */
2360 reply_doserror(req, ERRDOS, ERRunknownlevel);
2361 return;
2363 #endif /* LARGE_SMB_OFF_T */
2365 sid_parse(pdata+40,sid_len,&sid);
2366 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2368 /* 44 unknown bytes left... */
2370 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2371 reply_doserror(req, ERRSRV, ERRerror);
2372 return;
2375 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2376 pdata, data_len);
2378 #endif /* HAVE_SYS_QUOTAS */
2380 static void handle_nttrans(connection_struct *conn,
2381 struct trans_state *state,
2382 struct smb_request *req)
2384 if (Protocol >= PROTOCOL_NT1) {
2385 req->flags2 |= 0x40; /* IS_LONG_NAME */
2386 SSVAL(req->inbuf,smb_flg2,req->flags2);
2389 /* Now we must call the relevant NT_TRANS function */
2390 switch(state->call) {
2391 case NT_TRANSACT_CREATE:
2393 START_PROFILE(NT_transact_create);
2394 call_nt_transact_create(
2395 conn, req,
2396 &state->setup, state->setup_count,
2397 &state->param, state->total_param,
2398 &state->data, state->total_data,
2399 state->max_data_return);
2400 END_PROFILE(NT_transact_create);
2401 break;
2404 case NT_TRANSACT_IOCTL:
2406 START_PROFILE(NT_transact_ioctl);
2407 call_nt_transact_ioctl(
2408 conn, req,
2409 &state->setup, state->setup_count,
2410 &state->param, state->total_param,
2411 &state->data, state->total_data,
2412 state->max_data_return);
2413 END_PROFILE(NT_transact_ioctl);
2414 break;
2417 case NT_TRANSACT_SET_SECURITY_DESC:
2419 START_PROFILE(NT_transact_set_security_desc);
2420 call_nt_transact_set_security_desc(
2421 conn, req,
2422 &state->setup, state->setup_count,
2423 &state->param, state->total_param,
2424 &state->data, state->total_data,
2425 state->max_data_return);
2426 END_PROFILE(NT_transact_set_security_desc);
2427 break;
2430 case NT_TRANSACT_NOTIFY_CHANGE:
2432 START_PROFILE(NT_transact_notify_change);
2433 call_nt_transact_notify_change(
2434 conn, req,
2435 &state->setup, state->setup_count,
2436 &state->param, state->total_param,
2437 &state->data, state->total_data,
2438 state->max_data_return,
2439 state->max_param_return);
2440 END_PROFILE(NT_transact_notify_change);
2441 break;
2444 case NT_TRANSACT_RENAME:
2446 START_PROFILE(NT_transact_rename);
2447 call_nt_transact_rename(
2448 conn, req,
2449 &state->setup, state->setup_count,
2450 &state->param, state->total_param,
2451 &state->data, state->total_data,
2452 state->max_data_return);
2453 END_PROFILE(NT_transact_rename);
2454 break;
2457 case NT_TRANSACT_QUERY_SECURITY_DESC:
2459 START_PROFILE(NT_transact_query_security_desc);
2460 call_nt_transact_query_security_desc(
2461 conn, req,
2462 &state->setup, state->setup_count,
2463 &state->param, state->total_param,
2464 &state->data, state->total_data,
2465 state->max_data_return);
2466 END_PROFILE(NT_transact_query_security_desc);
2467 break;
2470 #ifdef HAVE_SYS_QUOTAS
2471 case NT_TRANSACT_GET_USER_QUOTA:
2473 START_PROFILE(NT_transact_get_user_quota);
2474 call_nt_transact_get_user_quota(
2475 conn, req,
2476 &state->setup, state->setup_count,
2477 &state->param, state->total_param,
2478 &state->data, state->total_data,
2479 state->max_data_return);
2480 END_PROFILE(NT_transact_get_user_quota);
2481 break;
2484 case NT_TRANSACT_SET_USER_QUOTA:
2486 START_PROFILE(NT_transact_set_user_quota);
2487 call_nt_transact_set_user_quota(
2488 conn, req,
2489 &state->setup, state->setup_count,
2490 &state->param, state->total_param,
2491 &state->data, state->total_data,
2492 state->max_data_return);
2493 END_PROFILE(NT_transact_set_user_quota);
2494 break;
2496 #endif /* HAVE_SYS_QUOTAS */
2498 default:
2499 /* Error in request */
2500 DEBUG(0,("handle_nttrans: Unknown request %d in "
2501 "nttrans call\n", state->call));
2502 reply_doserror(req, ERRSRV, ERRerror);
2503 return;
2505 return;
2508 /****************************************************************************
2509 Reply to a SMBNTtrans.
2510 ****************************************************************************/
2512 void reply_nttrans(struct smb_request *req)
2514 connection_struct *conn = req->conn;
2515 uint32_t pscnt;
2516 uint32_t psoff;
2517 uint32_t dscnt;
2518 uint32_t dsoff;
2519 uint16 function_code;
2520 NTSTATUS result;
2521 struct trans_state *state;
2522 uint32_t size;
2523 uint32_t av_size;
2525 START_PROFILE(SMBnttrans);
2527 if (req->wct < 19) {
2528 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2529 END_PROFILE(SMBnttrans);
2530 return;
2533 size = smb_len(req->inbuf) + 4;
2534 av_size = smb_len(req->inbuf);
2535 pscnt = IVAL(req->vwv+9, 1);
2536 psoff = IVAL(req->vwv+11, 1);
2537 dscnt = IVAL(req->vwv+13, 1);
2538 dsoff = IVAL(req->vwv+15, 1);
2539 function_code = SVAL(req->vwv+18, 0);
2541 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2542 reply_doserror(req, ERRSRV, ERRaccess);
2543 END_PROFILE(SMBnttrans);
2544 return;
2547 result = allow_new_trans(conn->pending_trans, req->mid);
2548 if (!NT_STATUS_IS_OK(result)) {
2549 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2550 reply_nterror(req, result);
2551 END_PROFILE(SMBnttrans);
2552 return;
2555 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2556 reply_doserror(req, ERRSRV, ERRaccess);
2557 END_PROFILE(SMBnttrans);
2558 return;
2561 state->cmd = SMBnttrans;
2563 state->mid = req->mid;
2564 state->vuid = req->vuid;
2565 state->total_data = IVAL(req->vwv+3, 1);
2566 state->data = NULL;
2567 state->total_param = IVAL(req->vwv+1, 1);
2568 state->param = NULL;
2569 state->max_data_return = IVAL(req->vwv+7, 1);
2570 state->max_param_return = IVAL(req->vwv+5, 1);
2572 /* setup count is in *words* */
2573 state->setup_count = 2*CVAL(req->vwv+17, 1);
2574 state->setup = NULL;
2575 state->call = function_code;
2577 DEBUG(10, ("num_setup=%u, "
2578 "param_total=%u, this_param=%u, max_param=%u, "
2579 "data_total=%u, this_data=%u, max_data=%u, "
2580 "param_offset=%u, data_offset=%u\n",
2581 (unsigned)state->setup_count,
2582 (unsigned)state->total_param, (unsigned)pscnt,
2583 (unsigned)state->max_param_return,
2584 (unsigned)state->total_data, (unsigned)dscnt,
2585 (unsigned)state->max_data_return,
2586 (unsigned)psoff, (unsigned)dsoff));
2589 * All nttrans messages we handle have smb_wct == 19 +
2590 * state->setup_count. Ensure this is so as a sanity check.
2593 if(req->wct != 19 + (state->setup_count/2)) {
2594 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2595 req->wct, 19 + (state->setup_count/2)));
2596 goto bad_param;
2599 /* Don't allow more than 128mb for each value. */
2600 if ((state->total_data > (1024*1024*128)) ||
2601 (state->total_param > (1024*1024*128))) {
2602 reply_doserror(req, ERRDOS, ERRnomem);
2603 END_PROFILE(SMBnttrans);
2604 return;
2607 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2608 goto bad_param;
2610 if (state->total_data) {
2611 /* Can't use talloc here, the core routines do realloc on the
2612 * params and data. */
2613 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2614 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2615 "bytes !\n", (unsigned int)state->total_data));
2616 TALLOC_FREE(state);
2617 reply_doserror(req, ERRDOS, ERRnomem);
2618 END_PROFILE(SMBnttrans);
2619 return;
2622 if (dscnt > state->total_data ||
2623 dsoff+dscnt < dsoff) {
2624 goto bad_param;
2627 if (dsoff > av_size ||
2628 dscnt > av_size ||
2629 dsoff+dscnt > av_size) {
2630 goto bad_param;
2633 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2636 if (state->total_param) {
2637 /* Can't use talloc here, the core routines do realloc on the
2638 * params and data. */
2639 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2640 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2641 "bytes !\n", (unsigned int)state->total_param));
2642 SAFE_FREE(state->data);
2643 TALLOC_FREE(state);
2644 reply_doserror(req, ERRDOS, ERRnomem);
2645 END_PROFILE(SMBnttrans);
2646 return;
2649 if (pscnt > state->total_param ||
2650 psoff+pscnt < psoff) {
2651 goto bad_param;
2654 if (psoff > av_size ||
2655 pscnt > av_size ||
2656 psoff+pscnt > av_size) {
2657 goto bad_param;
2660 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2663 state->received_data = dscnt;
2664 state->received_param = pscnt;
2666 if(state->setup_count > 0) {
2667 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2668 state->setup_count));
2669 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2670 if (state->setup == NULL) {
2671 DEBUG(0,("reply_nttrans : Out of memory\n"));
2672 SAFE_FREE(state->data);
2673 SAFE_FREE(state->param);
2674 TALLOC_FREE(state);
2675 reply_doserror(req, ERRDOS, ERRnomem);
2676 END_PROFILE(SMBnttrans);
2677 return;
2680 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2681 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2682 goto bad_param;
2684 if (smb_nt_SetupStart + state->setup_count > size) {
2685 goto bad_param;
2688 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
2689 state->setup_count);
2690 dump_data(10, (uint8 *)state->setup, state->setup_count);
2693 if ((state->received_data == state->total_data) &&
2694 (state->received_param == state->total_param)) {
2695 handle_nttrans(conn, state, req);
2696 SAFE_FREE(state->param);
2697 SAFE_FREE(state->data);
2698 TALLOC_FREE(state);
2699 END_PROFILE(SMBnttrans);
2700 return;
2703 DLIST_ADD(conn->pending_trans, state);
2705 /* We need to send an interim response then receive the rest
2706 of the parameter/data bytes */
2707 reply_outbuf(req, 0, 0);
2708 show_msg((char *)req->outbuf);
2709 END_PROFILE(SMBnttrans);
2710 return;
2712 bad_param:
2714 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2715 SAFE_FREE(state->data);
2716 SAFE_FREE(state->param);
2717 TALLOC_FREE(state);
2718 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2719 END_PROFILE(SMBnttrans);
2720 return;
2723 /****************************************************************************
2724 Reply to a SMBnttranss
2725 ****************************************************************************/
2727 void reply_nttranss(struct smb_request *req)
2729 connection_struct *conn = req->conn;
2730 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2731 struct trans_state *state;
2732 uint32_t av_size;
2733 uint32_t size;
2735 START_PROFILE(SMBnttranss);
2737 show_msg((char *)req->inbuf);
2739 if (req->wct < 18) {
2740 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2741 END_PROFILE(SMBnttranss);
2742 return;
2745 for (state = conn->pending_trans; state != NULL;
2746 state = state->next) {
2747 if (state->mid == req->mid) {
2748 break;
2752 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2753 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2754 END_PROFILE(SMBnttranss);
2755 return;
2758 /* Revise state->total_param and state->total_data in case they have
2759 changed downwards */
2760 if (IVAL(req->vwv+1, 1) < state->total_param) {
2761 state->total_param = IVAL(req->vwv+1, 1);
2763 if (IVAL(req->vwv+3, 1) < state->total_data) {
2764 state->total_data = IVAL(req->vwv+3, 1);
2767 size = smb_len(req->inbuf) + 4;
2768 av_size = smb_len(req->inbuf);
2770 pcnt = IVAL(req->vwv+5, 1);
2771 poff = IVAL(req->vwv+7, 1);
2772 pdisp = IVAL(req->vwv+9, 1);
2774 dcnt = IVAL(req->vwv+11, 1);
2775 doff = IVAL(req->vwv+13, 1);
2776 ddisp = IVAL(req->vwv+15, 1);
2778 state->received_param += pcnt;
2779 state->received_data += dcnt;
2781 if ((state->received_data > state->total_data) ||
2782 (state->received_param > state->total_param))
2783 goto bad_param;
2785 if (pcnt) {
2786 if (pdisp > state->total_param ||
2787 pcnt > state->total_param ||
2788 pdisp+pcnt > state->total_param ||
2789 pdisp+pcnt < pdisp) {
2790 goto bad_param;
2793 if (poff > av_size ||
2794 pcnt > av_size ||
2795 poff+pcnt > av_size ||
2796 poff+pcnt < poff) {
2797 goto bad_param;
2800 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
2801 pcnt);
2804 if (dcnt) {
2805 if (ddisp > state->total_data ||
2806 dcnt > state->total_data ||
2807 ddisp+dcnt > state->total_data ||
2808 ddisp+dcnt < ddisp) {
2809 goto bad_param;
2812 if (ddisp > av_size ||
2813 dcnt > av_size ||
2814 ddisp+dcnt > av_size ||
2815 ddisp+dcnt < ddisp) {
2816 goto bad_param;
2819 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
2820 dcnt);
2823 if ((state->received_param < state->total_param) ||
2824 (state->received_data < state->total_data)) {
2825 END_PROFILE(SMBnttranss);
2826 return;
2830 * construct_reply_common will copy smb_com from inbuf to
2831 * outbuf. SMBnttranss is wrong here.
2833 SCVAL(req->inbuf,smb_com,SMBnttrans);
2835 handle_nttrans(conn, state, req);
2837 DLIST_REMOVE(conn->pending_trans, state);
2838 SAFE_FREE(state->data);
2839 SAFE_FREE(state->param);
2840 TALLOC_FREE(state);
2841 END_PROFILE(SMBnttranss);
2842 return;
2844 bad_param:
2846 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2847 DLIST_REMOVE(conn->pending_trans, state);
2848 SAFE_FREE(state->data);
2849 SAFE_FREE(state->param);
2850 TALLOC_FREE(state);
2851 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2852 END_PROFILE(SMBnttranss);
2853 return;