2 Unix SMB/CIFS implementation.
3 SMB transaction2 handling
4 Copyright (C) Jeremy Allison 1994-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
6 Copyright (C) Volker Lendecke 2005-2007
7 Copyright (C) Steve French 2005
8 Copyright (C) James Peach 2006-2007
10 Extensively modified by Andrew Tridgell, 1995
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "smbd/globals.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../librpc/gen_ndr/xattr.h"
32 #define DIR_ENTRY_SAFETY_MARGIN 4096
34 static char *store_file_unix_basic(connection_struct *conn,
37 const SMB_STRUCT_STAT *psbuf);
39 static char *store_file_unix_basic_info2(connection_struct *conn,
42 const SMB_STRUCT_STAT *psbuf);
44 /********************************************************************
45 Roundup a value to the nearest allocation roundup size boundary.
46 Only do this for Windows clients.
47 ********************************************************************/
49 uint64_t smb_roundup(connection_struct *conn, uint64_t val)
51 uint64_t rval = lp_allocation_roundup_size(SNUM(conn));
53 /* Only roundup for Windows clients. */
54 enum remote_arch_types ra_type = get_remote_arch();
55 if (rval && (ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
56 val = SMB_ROUNDUP(val,rval);
61 /****************************************************************************
62 Utility functions for dealing with extended attributes.
63 ****************************************************************************/
65 /****************************************************************************
66 Refuse to allow clients to overwrite our private xattrs.
67 ****************************************************************************/
69 static bool samba_private_attr_name(const char *unix_ea_name)
71 static const char * const prohibited_ea_names[] = {
72 SAMBA_POSIX_INHERITANCE_EA_NAME,
73 SAMBA_XATTR_DOS_ATTRIB,
81 for (i = 0; prohibited_ea_names[i]; i++) {
82 if (strequal( prohibited_ea_names[i], unix_ea_name))
85 if (StrnCaseCmp(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
86 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) == 0) {
92 /****************************************************************************
93 Get one EA value. Fill in a struct ea_struct.
94 ****************************************************************************/
96 NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
97 files_struct *fsp, const char *fname,
98 const char *ea_name, struct ea_struct *pea)
100 /* Get the value of this xattr. Max size is 64k. */
101 size_t attr_size = 256;
107 val = TALLOC_REALLOC_ARRAY(mem_ctx, val, char, attr_size);
109 return NT_STATUS_NO_MEMORY;
112 if (fsp && fsp->fh->fd != -1) {
113 sizeret = SMB_VFS_FGETXATTR(fsp, ea_name, val, attr_size);
115 sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
118 if (sizeret == -1 && errno == ERANGE && attr_size != 65536) {
124 return map_nt_error_from_unix(errno);
127 DEBUG(10,("get_ea_value: EA %s is of length %u\n", ea_name, (unsigned int)sizeret));
128 dump_data(10, (uint8 *)val, sizeret);
131 if (strnequal(ea_name, "user.", 5)) {
132 pea->name = talloc_strdup(mem_ctx, &ea_name[5]);
134 pea->name = talloc_strdup(mem_ctx, ea_name);
136 if (pea->name == NULL) {
138 return NT_STATUS_NO_MEMORY;
140 pea->value.data = (unsigned char *)val;
141 pea->value.length = (size_t)sizeret;
145 NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
146 files_struct *fsp, const char *fname,
147 char ***pnames, size_t *pnum_names)
149 /* Get a list of all xattrs. Max namesize is 64k. */
150 size_t ea_namelist_size = 1024;
151 char *ea_namelist = NULL;
156 ssize_t sizeret = -1;
158 if (!lp_ea_support(SNUM(conn))) {
167 * TALLOC the result early to get the talloc hierarchy right.
170 names = TALLOC_ARRAY(mem_ctx, char *, 1);
172 DEBUG(0, ("talloc failed\n"));
173 return NT_STATUS_NO_MEMORY;
176 while (ea_namelist_size <= 65536) {
178 ea_namelist = TALLOC_REALLOC_ARRAY(
179 names, ea_namelist, char, ea_namelist_size);
180 if (ea_namelist == NULL) {
181 DEBUG(0, ("talloc failed\n"));
183 return NT_STATUS_NO_MEMORY;
186 if (fsp && fsp->fh->fd != -1) {
187 sizeret = SMB_VFS_FLISTXATTR(fsp, ea_namelist,
190 sizeret = SMB_VFS_LISTXATTR(conn, fname, ea_namelist,
194 if ((sizeret == -1) && (errno == ERANGE)) {
195 ea_namelist_size *= 2;
204 return map_nt_error_from_unix(errno);
207 DEBUG(10, ("get_ea_list_from_file: ea_namelist size = %u\n",
208 (unsigned int)sizeret));
220 * Ensure the result is 0-terminated
223 if (ea_namelist[sizeret-1] != '\0') {
225 return NT_STATUS_INTERNAL_ERROR;
233 for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
237 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, names, char *, num_names);
239 DEBUG(0, ("talloc failed\n"));
241 return NT_STATUS_NO_MEMORY;
247 for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
248 names[num_names++] = p;
256 *pnum_names = num_names;
260 /****************************************************************************
261 Return a linked list of the total EA's. Plus the total size
262 ****************************************************************************/
264 static struct ea_list *get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
265 const char *fname, size_t *pea_total_len)
267 /* Get a list of all xattrs. Max namesize is 64k. */
270 struct ea_list *ea_list_head = NULL;
275 if (!lp_ea_support(SNUM(conn))) {
279 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
282 if (!NT_STATUS_IS_OK(status) || (num_names == 0)) {
286 for (i=0; i<num_names; i++) {
287 struct ea_list *listp;
290 if (strnequal(names[i], "system.", 7)
291 || samba_private_attr_name(names[i]))
294 listp = TALLOC_P(mem_ctx, struct ea_list);
299 if (!NT_STATUS_IS_OK(get_ea_value(mem_ctx, conn, fsp,
305 push_ascii_fstring(dos_ea_name, listp->ea.name);
308 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
310 DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len "
311 "= %u\n", (unsigned int)*pea_total_len, dos_ea_name,
312 (unsigned int)listp->ea.value.length));
314 DLIST_ADD_END(ea_list_head, listp, struct ea_list *);
318 /* Add on 4 for total length. */
319 if (*pea_total_len) {
323 DEBUG(10, ("get_ea_list_from_file: total_len = %u\n",
324 (unsigned int)*pea_total_len));
329 /****************************************************************************
330 Fill a qfilepathinfo buffer with EA's. Returns the length of the buffer
332 ****************************************************************************/
334 static unsigned int fill_ea_buffer(TALLOC_CTX *mem_ctx, char *pdata, unsigned int total_data_size,
335 connection_struct *conn, struct ea_list *ea_list)
337 unsigned int ret_data_size = 4;
340 SMB_ASSERT(total_data_size >= 4);
342 if (!lp_ea_support(SNUM(conn))) {
347 for (p = pdata + 4; ea_list; ea_list = ea_list->next) {
350 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
351 dos_namelen = strlen(dos_ea_name);
352 if (dos_namelen > 255 || dos_namelen == 0) {
355 if (ea_list->ea.value.length > 65535) {
358 if (4 + dos_namelen + 1 + ea_list->ea.value.length > total_data_size) {
362 /* We know we have room. */
363 SCVAL(p,0,ea_list->ea.flags);
364 SCVAL(p,1,dos_namelen);
365 SSVAL(p,2,ea_list->ea.value.length);
366 fstrcpy(p+4, dos_ea_name);
367 memcpy( p + 4 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
369 total_data_size -= 4 + dos_namelen + 1 + ea_list->ea.value.length;
370 p += 4 + dos_namelen + 1 + ea_list->ea.value.length;
373 ret_data_size = PTR_DIFF(p, pdata);
374 DEBUG(10,("fill_ea_buffer: data_size = %u\n", ret_data_size ));
375 SIVAL(pdata,0,ret_data_size);
376 return ret_data_size;
379 static NTSTATUS fill_ea_chained_buffer(TALLOC_CTX *mem_ctx,
381 unsigned int total_data_size,
382 unsigned int *ret_data_size,
383 connection_struct *conn,
384 struct ea_list *ea_list)
386 uint8_t *p = (uint8_t *)pdata;
387 uint8_t *last_start = NULL;
391 if (!lp_ea_support(SNUM(conn))) {
392 return NT_STATUS_NO_EAS_ON_FILE;
395 for (; ea_list; ea_list = ea_list->next) {
401 SIVAL(last_start, 0, PTR_DIFF(p, last_start));
405 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
406 dos_namelen = strlen(dos_ea_name);
407 if (dos_namelen > 255 || dos_namelen == 0) {
408 return NT_STATUS_INTERNAL_ERROR;
410 if (ea_list->ea.value.length > 65535) {
411 return NT_STATUS_INTERNAL_ERROR;
414 this_size = 0x08 + dos_namelen + 1 + ea_list->ea.value.length;
417 size_t pad = 4 - (this_size % 4);
421 if (this_size > total_data_size) {
422 return NT_STATUS_INFO_LENGTH_MISMATCH;
425 /* We know we have room. */
426 SIVAL(p, 0x00, 0); /* next offset */
427 SCVAL(p, 0x04, ea_list->ea.flags);
428 SCVAL(p, 0x05, dos_namelen);
429 SSVAL(p, 0x06, ea_list->ea.value.length);
430 fstrcpy((char *)(p+0x08), dos_ea_name);
431 memcpy(p + 0x08 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
433 total_data_size -= this_size;
437 *ret_data_size = PTR_DIFF(p, pdata);
438 DEBUG(10,("fill_ea_chained_buffer: data_size = %u\n", *ret_data_size));
442 static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp, const char *fname)
444 size_t total_ea_len = 0;
445 TALLOC_CTX *mem_ctx = NULL;
447 if (!lp_ea_support(SNUM(conn))) {
450 mem_ctx = talloc_tos();
451 (void)get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
455 /****************************************************************************
456 Ensure the EA name is case insensitive by matching any existing EA name.
457 ****************************************************************************/
459 static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, const char *fname, fstring unix_ea_name)
462 TALLOC_CTX *mem_ctx = talloc_tos();
463 struct ea_list *ea_list = get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
465 for (; ea_list; ea_list = ea_list->next) {
466 if (strequal(&unix_ea_name[5], ea_list->ea.name)) {
467 DEBUG(10,("canonicalize_ea_name: %s -> %s\n",
468 &unix_ea_name[5], ea_list->ea.name));
469 safe_strcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-6);
475 /****************************************************************************
476 Set or delete an extended attribute.
477 ****************************************************************************/
479 NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
480 const struct smb_filename *smb_fname, struct ea_list *ea_list)
484 if (!lp_ea_support(SNUM(conn))) {
485 return NT_STATUS_EAS_NOT_SUPPORTED;
488 /* For now setting EAs on streams isn't supported. */
489 fname = smb_fname->base_name;
491 for (;ea_list; ea_list = ea_list->next) {
493 fstring unix_ea_name;
495 fstrcpy(unix_ea_name, "user."); /* All EA's must start with user. */
496 fstrcat(unix_ea_name, ea_list->ea.name);
498 canonicalize_ea_name(conn, fsp, fname, unix_ea_name);
500 DEBUG(10,("set_ea: ea_name %s ealen = %u\n", unix_ea_name, (unsigned int)ea_list->ea.value.length));
502 if (samba_private_attr_name(unix_ea_name)) {
503 DEBUG(10,("set_ea: ea name %s is a private Samba name.\n", unix_ea_name));
504 return NT_STATUS_ACCESS_DENIED;
507 if (ea_list->ea.value.length == 0) {
508 /* Remove the attribute. */
509 if (fsp && (fsp->fh->fd != -1)) {
510 DEBUG(10,("set_ea: deleting ea name %s on "
511 "file %s by file descriptor.\n",
512 unix_ea_name, fsp_str_dbg(fsp)));
513 ret = SMB_VFS_FREMOVEXATTR(fsp, unix_ea_name);
515 DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
516 unix_ea_name, fname));
517 ret = SMB_VFS_REMOVEXATTR(conn, fname, unix_ea_name);
520 /* Removing a non existent attribute always succeeds. */
521 if (ret == -1 && errno == ENOATTR) {
522 DEBUG(10,("set_ea: deleting ea name %s didn't exist - succeeding by default.\n",
528 if (fsp && (fsp->fh->fd != -1)) {
529 DEBUG(10,("set_ea: setting ea name %s on file "
530 "%s by file descriptor.\n",
531 unix_ea_name, fsp_str_dbg(fsp)));
532 ret = SMB_VFS_FSETXATTR(fsp, unix_ea_name,
533 ea_list->ea.value.data, ea_list->ea.value.length, 0);
535 DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
536 unix_ea_name, fname));
537 ret = SMB_VFS_SETXATTR(conn, fname, unix_ea_name,
538 ea_list->ea.value.data, ea_list->ea.value.length, 0);
544 if (errno == ENOTSUP) {
545 return NT_STATUS_EAS_NOT_SUPPORTED;
548 return map_nt_error_from_unix(errno);
554 /****************************************************************************
555 Read a list of EA names from an incoming data buffer. Create an ea_list with them.
556 ****************************************************************************/
558 static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
560 struct ea_list *ea_list_head = NULL;
561 size_t converted_size, offset = 0;
563 while (offset + 2 < data_size) {
564 struct ea_list *eal = TALLOC_ZERO_P(ctx, struct ea_list);
565 unsigned int namelen = CVAL(pdata,offset);
567 offset++; /* Go past the namelen byte. */
569 /* integer wrap paranioa. */
570 if ((offset + namelen < offset) || (offset + namelen < namelen) ||
571 (offset > data_size) || (namelen > data_size) ||
572 (offset + namelen >= data_size)) {
575 /* Ensure the name is null terminated. */
576 if (pdata[offset + namelen] != '\0') {
579 if (!pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset],
581 DEBUG(0,("read_ea_name_list: pull_ascii_talloc "
582 "failed: %s", strerror(errno)));
588 offset += (namelen + 1); /* Go past the name + terminating zero. */
589 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
590 DEBUG(10,("read_ea_name_list: read ea name %s\n", eal->ea.name));
596 /****************************************************************************
597 Read one EA list entry from the buffer.
598 ****************************************************************************/
600 struct ea_list *read_ea_list_entry(TALLOC_CTX *ctx, const char *pdata, size_t data_size, size_t *pbytes_used)
602 struct ea_list *eal = TALLOC_ZERO_P(ctx, struct ea_list);
604 unsigned int namelen;
605 size_t converted_size;
615 eal->ea.flags = CVAL(pdata,0);
616 namelen = CVAL(pdata,1);
617 val_len = SVAL(pdata,2);
619 if (4 + namelen + 1 + val_len > data_size) {
623 /* Ensure the name is null terminated. */
624 if (pdata[namelen + 4] != '\0') {
627 if (!pull_ascii_talloc(ctx, &eal->ea.name, pdata + 4, &converted_size)) {
628 DEBUG(0,("read_ea_list_entry: pull_ascii_talloc failed: %s",
635 eal->ea.value = data_blob_talloc(eal, NULL, (size_t)val_len + 1);
636 if (!eal->ea.value.data) {
640 memcpy(eal->ea.value.data, pdata + 4 + namelen + 1, val_len);
642 /* Ensure we're null terminated just in case we print the value. */
643 eal->ea.value.data[val_len] = '\0';
644 /* But don't count the null. */
645 eal->ea.value.length--;
648 *pbytes_used = 4 + namelen + 1 + val_len;
651 DEBUG(10,("read_ea_list_entry: read ea name %s\n", eal->ea.name));
652 dump_data(10, eal->ea.value.data, eal->ea.value.length);
657 /****************************************************************************
658 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
659 ****************************************************************************/
661 static struct ea_list *read_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
663 struct ea_list *ea_list_head = NULL;
665 size_t bytes_used = 0;
667 while (offset < data_size) {
668 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset, data_size - offset, &bytes_used);
674 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
675 offset += bytes_used;
681 /****************************************************************************
682 Count the total EA size needed.
683 ****************************************************************************/
685 static size_t ea_list_size(struct ea_list *ealist)
688 struct ea_list *listp;
691 for (listp = ealist; listp; listp = listp->next) {
692 push_ascii_fstring(dos_ea_name, listp->ea.name);
693 ret += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
695 /* Add on 4 for total length. */
703 /****************************************************************************
704 Return a union of EA's from a file list and a list of names.
705 The TALLOC context for the two lists *MUST* be identical as we steal
706 memory from one list to add to another. JRA.
707 ****************************************************************************/
709 static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *file_list, size_t *total_ea_len)
711 struct ea_list *nlistp, *flistp;
713 for (nlistp = name_list; nlistp; nlistp = nlistp->next) {
714 for (flistp = file_list; flistp; flistp = flistp->next) {
715 if (strequal(nlistp->ea.name, flistp->ea.name)) {
721 /* Copy the data from this entry. */
722 nlistp->ea.flags = flistp->ea.flags;
723 nlistp->ea.value = flistp->ea.value;
726 nlistp->ea.flags = 0;
727 ZERO_STRUCT(nlistp->ea.value);
731 *total_ea_len = ea_list_size(name_list);
735 /****************************************************************************
736 Send the required number of replies back.
737 We assume all fields other than the data fields are
738 set correctly for the type of call.
739 HACK ! Always assumes smb_setup field is zero.
740 ****************************************************************************/
742 void send_trans2_replies(connection_struct *conn,
743 struct smb_request *req,
750 /* As we are using a protocol > LANMAN1 then the max_send
751 variable must have been set in the sessetupX call.
752 This takes precedence over the max_xmit field in the
753 global struct. These different max_xmit variables should
754 be merged as this is now too confusing */
756 int data_to_send = datasize;
757 int params_to_send = paramsize;
759 const char *pp = params;
760 const char *pd = pdata;
761 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
762 int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
763 int data_alignment_offset = 0;
764 bool overflow = False;
765 struct smbd_server_connection *sconn = smbd_server_conn;
766 int max_send = sconn->smb1.sessions.max_send;
768 /* Modify the data_to_send and datasize and set the error if
769 we're trying to send more than max_data_bytes. We still send
770 the part of the packet(s) that fit. Strange, but needed
773 if (max_data_bytes > 0 && datasize > max_data_bytes) {
774 DEBUG(5,("send_trans2_replies: max_data_bytes %d exceeded by data %d\n",
775 max_data_bytes, datasize ));
776 datasize = data_to_send = max_data_bytes;
780 /* If there genuinely are no parameters or data to send just send the empty packet */
782 if(params_to_send == 0 && data_to_send == 0) {
783 reply_outbuf(req, 10, 0);
784 show_msg((char *)req->outbuf);
785 if (!srv_send_smb(smbd_server_fd(),
788 IS_CONN_ENCRYPTED(conn),
790 exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
792 TALLOC_FREE(req->outbuf);
796 /* When sending params and data ensure that both are nicely aligned */
797 /* Only do this alignment when there is also data to send - else
798 can cause NT redirector problems. */
800 if (((params_to_send % 4) != 0) && (data_to_send != 0))
801 data_alignment_offset = 4 - (params_to_send % 4);
803 /* Space is bufsize minus Netbios over TCP header minus SMB header */
804 /* The alignment_offset is to align the param bytes on an even byte
805 boundary. NT 4.0 Beta needs this to work correctly. */
807 useable_space = max_send - (smb_size
810 + data_alignment_offset);
812 if (useable_space < 0) {
813 DEBUG(0, ("send_trans2_replies failed sanity useable_space "
814 "= %d!!!", useable_space));
815 exit_server_cleanly("send_trans2_replies: Not enough space");
818 while (params_to_send || data_to_send) {
819 /* Calculate whether we will totally or partially fill this packet */
821 total_sent_thistime = params_to_send + data_to_send;
823 /* We can never send more than useable_space */
825 * Note that 'useable_space' does not include the alignment offsets,
826 * but we must include the alignment offsets in the calculation of
827 * the length of the data we send over the wire, as the alignment offsets
828 * are sent here. Fix from Marc_Jacobsen@hp.com.
831 total_sent_thistime = MIN(total_sent_thistime, useable_space);
833 reply_outbuf(req, 10, total_sent_thistime + alignment_offset
834 + data_alignment_offset);
837 * We might have SMBtrans2s in req which was transferred to
838 * the outbuf, fix that.
840 SCVAL(req->outbuf, smb_com, SMBtrans2);
842 /* Set total params and data to be sent */
843 SSVAL(req->outbuf,smb_tprcnt,paramsize);
844 SSVAL(req->outbuf,smb_tdrcnt,datasize);
846 /* Calculate how many parameters and data we can fit into
847 * this packet. Parameters get precedence
850 params_sent_thistime = MIN(params_to_send,useable_space);
851 data_sent_thistime = useable_space - params_sent_thistime;
852 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
854 SSVAL(req->outbuf,smb_prcnt, params_sent_thistime);
856 /* smb_proff is the offset from the start of the SMB header to the
857 parameter bytes, however the first 4 bytes of outbuf are
858 the Netbios over TCP header. Thus use smb_base() to subtract
859 them from the calculation */
861 SSVAL(req->outbuf,smb_proff,
862 ((smb_buf(req->outbuf)+alignment_offset)
863 - smb_base(req->outbuf)));
865 if(params_sent_thistime == 0)
866 SSVAL(req->outbuf,smb_prdisp,0);
868 /* Absolute displacement of param bytes sent in this packet */
869 SSVAL(req->outbuf,smb_prdisp,pp - params);
871 SSVAL(req->outbuf,smb_drcnt, data_sent_thistime);
872 if(data_sent_thistime == 0) {
873 SSVAL(req->outbuf,smb_droff,0);
874 SSVAL(req->outbuf,smb_drdisp, 0);
876 /* The offset of the data bytes is the offset of the
877 parameter bytes plus the number of parameters being sent this time */
878 SSVAL(req->outbuf, smb_droff,
879 ((smb_buf(req->outbuf)+alignment_offset)
880 - smb_base(req->outbuf))
881 + params_sent_thistime + data_alignment_offset);
882 SSVAL(req->outbuf,smb_drdisp, pd - pdata);
885 /* Initialize the padding for alignment */
887 if (alignment_offset != 0) {
888 memset(smb_buf(req->outbuf), 0, alignment_offset);
891 /* Copy the param bytes into the packet */
893 if(params_sent_thistime) {
894 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
895 params_sent_thistime);
898 /* Copy in the data bytes */
899 if(data_sent_thistime) {
900 if (data_alignment_offset != 0) {
901 memset((smb_buf(req->outbuf)+alignment_offset+
902 params_sent_thistime), 0,
903 data_alignment_offset);
905 memcpy(smb_buf(req->outbuf)+alignment_offset
906 +params_sent_thistime+data_alignment_offset,
907 pd,data_sent_thistime);
910 DEBUG(9,("t2_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
911 params_sent_thistime, data_sent_thistime, useable_space));
912 DEBUG(9,("t2_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
913 params_to_send, data_to_send, paramsize, datasize));
916 error_packet_set((char *)req->outbuf,
917 ERRDOS,ERRbufferoverflow,
918 STATUS_BUFFER_OVERFLOW,
922 /* Send the packet */
923 show_msg((char *)req->outbuf);
924 if (!srv_send_smb(smbd_server_fd(),
927 IS_CONN_ENCRYPTED(conn),
929 exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
931 TALLOC_FREE(req->outbuf);
933 pp += params_sent_thistime;
934 pd += data_sent_thistime;
936 params_to_send -= params_sent_thistime;
937 data_to_send -= data_sent_thistime;
940 if(params_to_send < 0 || data_to_send < 0) {
941 DEBUG(0,("send_trans2_replies failed sanity check pts = %d, dts = %d\n!!!",
942 params_to_send, data_to_send));
950 /****************************************************************************
951 Reply to a TRANSACT2_OPEN.
952 ****************************************************************************/
954 static void call_trans2open(connection_struct *conn,
955 struct smb_request *req,
956 char **pparams, int total_params,
957 char **ppdata, int total_data,
958 unsigned int max_data_bytes)
960 struct smb_filename *smb_fname = NULL;
961 char *params = *pparams;
962 char *pdata = *ppdata;
967 bool return_additional_info;
980 struct ea_list *ea_list = NULL;
985 uint32 create_disposition;
986 uint32 create_options = 0;
987 uint32_t private_flags = 0;
988 TALLOC_CTX *ctx = talloc_tos();
991 * Ensure we have enough parameters to perform the operation.
994 if (total_params < 29) {
995 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
999 flags = SVAL(params, 0);
1000 deny_mode = SVAL(params, 2);
1001 open_attr = SVAL(params,6);
1002 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1003 if (oplock_request) {
1004 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1008 return_additional_info = BITSETW(params,0);
1009 open_sattr = SVAL(params, 4);
1010 open_time = make_unix_date3(params+8);
1012 open_ofun = SVAL(params,12);
1013 open_size = IVAL(params,14);
1014 pname = ¶ms[28];
1017 reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
1021 srvstr_get_path(ctx, params, req->flags2, &fname, pname,
1022 total_params - 28, STR_TERMINATE,
1024 if (!NT_STATUS_IS_OK(status)) {
1025 reply_nterror(req, status);
1029 DEBUG(3,("call_trans2open %s deny_mode=0x%x attr=%d ofun=0x%x size=%d\n",
1030 fname, (unsigned int)deny_mode, (unsigned int)open_attr,
1031 (unsigned int)open_ofun, open_size));
1033 status = filename_convert(ctx,
1035 req->flags2 & FLAGS2_DFS_PATHNAMES,
1040 if (!NT_STATUS_IS_OK(status)) {
1041 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1042 reply_botherror(req,
1043 NT_STATUS_PATH_NOT_COVERED,
1044 ERRSRV, ERRbadpath);
1047 reply_nterror(req, status);
1051 if (open_ofun == 0) {
1052 reply_nterror(req, NT_STATUS_OBJECT_NAME_COLLISION);
1056 if (!map_open_params_to_ntcreate(smb_fname, deny_mode, open_ofun,
1057 &access_mask, &share_mode,
1058 &create_disposition,
1061 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1065 /* Any data in this call is an EA list. */
1066 if (total_data && (total_data != 4)) {
1067 if (total_data < 10) {
1068 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1072 if (IVAL(pdata,0) > total_data) {
1073 DEBUG(10,("call_trans2open: bad total data size (%u) > %u\n",
1074 IVAL(pdata,0), (unsigned int)total_data));
1075 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1079 ea_list = read_ea_list(talloc_tos(), pdata + 4,
1082 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1086 if (!lp_ea_support(SNUM(conn))) {
1087 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1092 status = SMB_VFS_CREATE_FILE(
1095 0, /* root_dir_fid */
1096 smb_fname, /* fname */
1097 access_mask, /* access_mask */
1098 share_mode, /* share_access */
1099 create_disposition, /* create_disposition*/
1100 create_options, /* create_options */
1101 open_attr, /* file_attributes */
1102 oplock_request, /* oplock_request */
1103 open_size, /* allocation_size */
1106 ea_list, /* ea_list */
1108 &smb_action); /* psbuf */
1110 if (!NT_STATUS_IS_OK(status)) {
1111 if (open_was_deferred(req->mid)) {
1112 /* We have re-scheduled this call. */
1115 reply_openerror(req, status);
1119 size = get_file_size_stat(&smb_fname->st);
1120 fattr = dos_mode(conn, smb_fname);
1121 mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
1122 inode = smb_fname->st.st_ex_ino;
1124 close_file(req, fsp, ERROR_CLOSE);
1125 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1129 /* Realloc the size of parameters and data we will return */
1130 *pparams = (char *)SMB_REALLOC(*pparams, 30);
1131 if(*pparams == NULL ) {
1132 reply_nterror(req, NT_STATUS_NO_MEMORY);
1137 SSVAL(params,0,fsp->fnum);
1138 SSVAL(params,2,fattr);
1139 srv_put_dos_date2(params,4, mtime);
1140 SIVAL(params,8, (uint32)size);
1141 SSVAL(params,12,deny_mode);
1142 SSVAL(params,14,0); /* open_type - file or directory. */
1143 SSVAL(params,16,0); /* open_state - only valid for IPC device. */
1145 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1146 smb_action |= EXTENDED_OPLOCK_GRANTED;
1149 SSVAL(params,18,smb_action);
1152 * WARNING - this may need to be changed if SMB_INO_T <> 4 bytes.
1154 SIVAL(params,20,inode);
1155 SSVAL(params,24,0); /* Padding. */
1157 uint32 ea_size = estimate_ea_size(conn, fsp,
1158 fsp->fsp_name->base_name);
1159 SIVAL(params, 26, ea_size);
1161 SIVAL(params, 26, 0);
1164 /* Send the required number of replies */
1165 send_trans2_replies(conn, req, params, 30, *ppdata, 0, max_data_bytes);
1167 TALLOC_FREE(smb_fname);
1170 /*********************************************************
1171 Routine to check if a given string matches exactly.
1172 as a special case a mask of "." does NOT match. That
1173 is required for correct wildcard semantics
1174 Case can be significant or not.
1175 **********************************************************/
1177 static bool exact_match(bool has_wild,
1178 bool case_sensitive,
1182 if (mask[0] == '.' && mask[1] == 0) {
1190 if (case_sensitive) {
1191 return strcmp(str,mask)==0;
1193 return StrCaseCmp(str,mask) == 0;
1197 /****************************************************************************
1198 Return the filetype for UNIX extensions.
1199 ****************************************************************************/
1201 static uint32 unix_filetype(mode_t mode)
1204 return UNIX_TYPE_FILE;
1205 else if(S_ISDIR(mode))
1206 return UNIX_TYPE_DIR;
1208 else if(S_ISLNK(mode))
1209 return UNIX_TYPE_SYMLINK;
1212 else if(S_ISCHR(mode))
1213 return UNIX_TYPE_CHARDEV;
1216 else if(S_ISBLK(mode))
1217 return UNIX_TYPE_BLKDEV;
1220 else if(S_ISFIFO(mode))
1221 return UNIX_TYPE_FIFO;
1224 else if(S_ISSOCK(mode))
1225 return UNIX_TYPE_SOCKET;
1228 DEBUG(0,("unix_filetype: unknown filetype %u\n", (unsigned)mode));
1229 return UNIX_TYPE_UNKNOWN;
1232 /****************************************************************************
1233 Map wire perms onto standard UNIX permissions. Obey share restrictions.
1234 ****************************************************************************/
1236 enum perm_type { PERM_NEW_FILE, PERM_NEW_DIR, PERM_EXISTING_FILE, PERM_EXISTING_DIR};
1238 static NTSTATUS unix_perms_from_wire( connection_struct *conn,
1239 const SMB_STRUCT_STAT *psbuf,
1241 enum perm_type ptype,
1246 if (perms == SMB_MODE_NO_CHANGE) {
1247 if (!VALID_STAT(*psbuf)) {
1248 return NT_STATUS_INVALID_PARAMETER;
1250 *ret_perms = psbuf->st_ex_mode;
1251 return NT_STATUS_OK;
1255 ret |= ((perms & UNIX_X_OTH ) ? S_IXOTH : 0);
1256 ret |= ((perms & UNIX_W_OTH ) ? S_IWOTH : 0);
1257 ret |= ((perms & UNIX_R_OTH ) ? S_IROTH : 0);
1258 ret |= ((perms & UNIX_X_GRP ) ? S_IXGRP : 0);
1259 ret |= ((perms & UNIX_W_GRP ) ? S_IWGRP : 0);
1260 ret |= ((perms & UNIX_R_GRP ) ? S_IRGRP : 0);
1261 ret |= ((perms & UNIX_X_USR ) ? S_IXUSR : 0);
1262 ret |= ((perms & UNIX_W_USR ) ? S_IWUSR : 0);
1263 ret |= ((perms & UNIX_R_USR ) ? S_IRUSR : 0);
1265 ret |= ((perms & UNIX_STICKY ) ? S_ISVTX : 0);
1268 ret |= ((perms & UNIX_SET_GID ) ? S_ISGID : 0);
1271 ret |= ((perms & UNIX_SET_UID ) ? S_ISUID : 0);
1276 /* Apply mode mask */
1277 ret &= lp_create_mask(SNUM(conn));
1278 /* Add in force bits */
1279 ret |= lp_force_create_mode(SNUM(conn));
1282 ret &= lp_dir_mask(SNUM(conn));
1283 /* Add in force bits */
1284 ret |= lp_force_dir_mode(SNUM(conn));
1286 case PERM_EXISTING_FILE:
1287 /* Apply mode mask */
1288 ret &= lp_security_mask(SNUM(conn));
1289 /* Add in force bits */
1290 ret |= lp_force_security_mode(SNUM(conn));
1292 case PERM_EXISTING_DIR:
1293 /* Apply mode mask */
1294 ret &= lp_dir_security_mask(SNUM(conn));
1295 /* Add in force bits */
1296 ret |= lp_force_dir_security_mode(SNUM(conn));
1301 return NT_STATUS_OK;
1304 /****************************************************************************
1305 Needed to show the msdfs symlinks as directories. Modifies psbuf
1306 to be a directory if it's a msdfs link.
1307 ****************************************************************************/
1309 static bool check_msdfs_link(connection_struct *conn,
1310 const char *pathname,
1311 SMB_STRUCT_STAT *psbuf)
1313 int saved_errno = errno;
1314 if(lp_host_msdfs() &&
1315 lp_msdfs_root(SNUM(conn)) &&
1316 is_msdfs_link(conn, pathname, psbuf)) {
1318 DEBUG(5,("check_msdfs_link: Masquerading msdfs link %s "
1321 psbuf->st_ex_mode = (psbuf->st_ex_mode & 0xFFF) | S_IFDIR;
1322 errno = saved_errno;
1325 errno = saved_errno;
1330 /****************************************************************************
1331 Get a level dependent lanman2 dir entry.
1332 ****************************************************************************/
1334 struct smbd_dirptr_lanman2_state {
1335 connection_struct *conn;
1336 uint32_t info_level;
1337 bool check_mangled_names;
1339 bool got_exact_match;
1342 static bool smbd_dirptr_lanman2_match_fn(TALLOC_CTX *ctx,
1348 struct smbd_dirptr_lanman2_state *state =
1349 (struct smbd_dirptr_lanman2_state *)private_data;
1351 char mangled_name[13]; /* mangled 8.3 name. */
1355 /* Mangle fname if it's an illegal name. */
1356 if (mangle_must_mangle(dname, state->conn->params)) {
1357 ok = name_to_8_3(dname, mangled_name,
1358 true, state->conn->params);
1362 fname = mangled_name;
1367 got_match = exact_match(state->has_wild,
1368 state->conn->case_sensitive,
1370 state->got_exact_match = got_match;
1372 got_match = mask_match(fname, mask,
1373 state->conn->case_sensitive);
1376 if(!got_match && state->check_mangled_names &&
1377 !mangle_is_8_3(fname, false, state->conn->params)) {
1379 * It turns out that NT matches wildcards against
1380 * both long *and* short names. This may explain some
1381 * of the wildcard wierdness from old DOS clients
1382 * that some people have been seeing.... JRA.
1384 /* Force the mangling into 8.3. */
1385 ok = name_to_8_3(fname, mangled_name,
1386 false, state->conn->params);
1391 got_match = exact_match(state->has_wild,
1392 state->conn->case_sensitive,
1393 mangled_name, mask);
1394 state->got_exact_match = got_match;
1396 got_match = mask_match(mangled_name, mask,
1397 state->conn->case_sensitive);
1405 *_fname = talloc_strdup(ctx, fname);
1406 if (*_fname == NULL) {
1413 static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
1415 struct smb_filename *smb_fname,
1418 struct smbd_dirptr_lanman2_state *state =
1419 (struct smbd_dirptr_lanman2_state *)private_data;
1420 bool ms_dfs_link = false;
1423 if (INFO_LEVEL_IS_UNIX(state->info_level)) {
1424 if (SMB_VFS_LSTAT(state->conn, smb_fname) != 0) {
1425 DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
1426 "Couldn't lstat [%s] (%s)\n",
1427 smb_fname_str_dbg(smb_fname),
1431 } else if (!VALID_STAT(smb_fname->st) &&
1432 SMB_VFS_STAT(state->conn, smb_fname) != 0) {
1433 /* Needed to show the msdfs symlinks as
1436 ms_dfs_link = check_msdfs_link(state->conn,
1437 smb_fname->base_name,
1440 DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
1441 "Couldn't stat [%s] (%s)\n",
1442 smb_fname_str_dbg(smb_fname),
1449 mode = dos_mode_msdfs(state->conn, smb_fname);
1451 mode = dos_mode(state->conn, smb_fname);
1458 static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
1459 connection_struct *conn,
1461 uint32_t info_level,
1462 struct ea_list *name_list,
1463 bool check_mangled_names,
1464 bool requires_resume_key,
1467 const struct smb_filename *smb_fname,
1468 int space_remaining,
1475 uint64_t *last_entry_off)
1477 char *p, *q, *pdata = *ppdata;
1479 uint64_t file_size = 0;
1480 uint64_t allocation_size = 0;
1482 struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
1483 time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
1484 time_t c_date = (time_t)0;
1486 char *last_entry_ptr;
1491 *out_of_space = false;
1493 ZERO_STRUCT(mdate_ts);
1494 ZERO_STRUCT(adate_ts);
1495 ZERO_STRUCT(create_date_ts);
1496 ZERO_STRUCT(cdate_ts);
1498 if (!(mode & aDIR)) {
1499 file_size = get_file_size_stat(&smb_fname->st);
1501 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
1503 mdate_ts = smb_fname->st.st_ex_mtime;
1504 adate_ts = smb_fname->st.st_ex_atime;
1505 create_date_ts = get_create_timespec(conn, NULL, smb_fname);
1506 cdate_ts = get_change_timespec(conn, NULL, smb_fname);
1508 if (lp_dos_filetime_resolution(SNUM(conn))) {
1509 dos_filetime_timespec(&create_date_ts);
1510 dos_filetime_timespec(&mdate_ts);
1511 dos_filetime_timespec(&adate_ts);
1512 dos_filetime_timespec(&cdate_ts);
1515 create_date = convert_timespec_to_time_t(create_date_ts);
1516 mdate = convert_timespec_to_time_t(mdate_ts);
1517 adate = convert_timespec_to_time_t(adate_ts);
1518 c_date = convert_timespec_to_time_t(cdate_ts);
1520 /* align the record */
1521 SMB_ASSERT(align >= 1);
1523 off = (int)PTR_DIFF(pdata, base_data);
1524 pad = (off + (align-1)) & ~(align-1);
1527 if (pad && pad > space_remaining) {
1528 *out_of_space = true;
1529 DEBUG(9,("smbd_marshall_dir_entry: out of space "
1530 "for padding (wanted %u, had %d)\n",
1533 return false; /* Not finished - just out of space */
1537 /* initialize padding to 0 */
1539 memset(pdata, 0, pad);
1541 space_remaining -= pad;
1543 DEBUG(10,("smbd_marshall_dir_entry: space_remaining = %d\n",
1553 switch (info_level) {
1554 case SMB_FIND_INFO_STANDARD:
1555 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_INFO_STANDARD\n"));
1556 if(requires_resume_key) {
1560 srv_put_dos_date2(p,0,create_date);
1561 srv_put_dos_date2(p,4,adate);
1562 srv_put_dos_date2(p,8,mdate);
1563 SIVAL(p,12,(uint32)file_size);
1564 SIVAL(p,16,(uint32)allocation_size);
1568 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1569 p += ucs2_align(base_data, p, 0);
1571 len = srvstr_push(base_data, flags2, p,
1572 fname, PTR_DIFF(end_data, p),
1574 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1576 SCVAL(nameptr, -1, len - 2);
1578 SCVAL(nameptr, -1, 0);
1582 SCVAL(nameptr, -1, len - 1);
1584 SCVAL(nameptr, -1, 0);
1590 case SMB_FIND_EA_SIZE:
1591 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_SIZE\n"));
1592 if (requires_resume_key) {
1596 srv_put_dos_date2(p,0,create_date);
1597 srv_put_dos_date2(p,4,adate);
1598 srv_put_dos_date2(p,8,mdate);
1599 SIVAL(p,12,(uint32)file_size);
1600 SIVAL(p,16,(uint32)allocation_size);
1603 unsigned int ea_size = estimate_ea_size(conn, NULL,
1604 smb_fname->base_name);
1605 SIVAL(p,22,ea_size); /* Extended attributes */
1609 len = srvstr_push(base_data, flags2,
1610 p, fname, PTR_DIFF(end_data, p),
1611 STR_TERMINATE | STR_NOALIGN);
1612 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1625 SCVAL(nameptr,0,len);
1627 SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1630 case SMB_FIND_EA_LIST:
1632 struct ea_list *file_list = NULL;
1635 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_LIST\n"));
1639 if (requires_resume_key) {
1643 srv_put_dos_date2(p,0,create_date);
1644 srv_put_dos_date2(p,4,adate);
1645 srv_put_dos_date2(p,8,mdate);
1646 SIVAL(p,12,(uint32)file_size);
1647 SIVAL(p,16,(uint32)allocation_size);
1649 p += 22; /* p now points to the EA area. */
1651 file_list = get_ea_list_from_file(ctx, conn, NULL,
1652 smb_fname->base_name,
1654 name_list = ea_list_union(name_list, file_list, &ea_len);
1656 /* We need to determine if this entry will fit in the space available. */
1657 /* Max string size is 255 bytes. */
1658 if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
1659 *out_of_space = true;
1660 DEBUG(9,("smbd_marshall_dir_entry: out of space "
1661 "(wanted %u, had %d)\n",
1662 (unsigned int)PTR_DIFF(p + 255 + ea_len,pdata),
1664 return False; /* Not finished - just out of space */
1667 /* Push the ea_data followed by the name. */
1668 p += fill_ea_buffer(ctx, p, space_remaining, conn, name_list);
1670 len = srvstr_push(base_data, flags2,
1671 p + 1, fname, PTR_DIFF(end_data, p+1),
1672 STR_TERMINATE | STR_NOALIGN);
1673 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1686 SCVAL(nameptr,0,len);
1688 SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1692 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1693 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
1694 was_8_3 = mangle_is_8_3(fname, True, conn->params);
1696 SIVAL(p,0,reskey); p += 4;
1697 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1698 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1699 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1700 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1701 SOFF_T(p,0,file_size); p += 8;
1702 SOFF_T(p,0,allocation_size); p += 8;
1703 SIVAL(p,0,mode); p += 4;
1704 q = p; p += 4; /* q is placeholder for name length. */
1706 unsigned int ea_size = estimate_ea_size(conn, NULL,
1707 smb_fname->base_name);
1708 SIVAL(p,0,ea_size); /* Extended attributes */
1711 /* Clear the short name buffer. This is
1712 * IMPORTANT as not doing so will trigger
1713 * a Win2k client bug. JRA.
1715 if (!was_8_3 && check_mangled_names) {
1716 char mangled_name[13]; /* mangled 8.3 name. */
1717 if (!name_to_8_3(fname,mangled_name,True,
1719 /* Error - mangle failed ! */
1720 memset(mangled_name,'\0',12);
1722 mangled_name[12] = 0;
1723 len = srvstr_push(base_data, flags2,
1724 p+2, mangled_name, 24,
1725 STR_UPPER|STR_UNICODE);
1727 memset(p + 2 + len,'\0',24 - len);
1734 len = srvstr_push(base_data, flags2, p,
1735 fname, PTR_DIFF(end_data, p),
1736 STR_TERMINATE_ASCII);
1740 len = PTR_DIFF(p, pdata);
1741 pad = (len + (align-1)) & ~(align-1);
1743 * offset to the next entry, the caller
1744 * will overwrite it for the last entry
1745 * that's why we always include the padding
1749 * set padding to zero
1752 memset(p, 0, pad - len);
1759 case SMB_FIND_FILE_DIRECTORY_INFO:
1760 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
1762 SIVAL(p,0,reskey); p += 4;
1763 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1764 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1765 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1766 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1767 SOFF_T(p,0,file_size); p += 8;
1768 SOFF_T(p,0,allocation_size); p += 8;
1769 SIVAL(p,0,mode); p += 4;
1770 len = srvstr_push(base_data, flags2,
1771 p + 4, fname, PTR_DIFF(end_data, p+4),
1772 STR_TERMINATE_ASCII);
1776 len = PTR_DIFF(p, pdata);
1777 pad = (len + (align-1)) & ~(align-1);
1779 * offset to the next entry, the caller
1780 * will overwrite it for the last entry
1781 * that's why we always include the padding
1785 * set padding to zero
1788 memset(p, 0, pad - len);
1795 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1796 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
1798 SIVAL(p,0,reskey); p += 4;
1799 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1800 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1801 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1802 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1803 SOFF_T(p,0,file_size); p += 8;
1804 SOFF_T(p,0,allocation_size); p += 8;
1805 SIVAL(p,0,mode); p += 4;
1806 q = p; p += 4; /* q is placeholder for name length. */
1808 unsigned int ea_size = estimate_ea_size(conn, NULL,
1809 smb_fname->base_name);
1810 SIVAL(p,0,ea_size); /* Extended attributes */
1813 len = srvstr_push(base_data, flags2, p,
1814 fname, PTR_DIFF(end_data, p),
1815 STR_TERMINATE_ASCII);
1819 len = PTR_DIFF(p, pdata);
1820 pad = (len + (align-1)) & ~(align-1);
1822 * offset to the next entry, the caller
1823 * will overwrite it for the last entry
1824 * that's why we always include the padding
1828 * set padding to zero
1831 memset(p, 0, pad - len);
1838 case SMB_FIND_FILE_NAMES_INFO:
1839 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
1841 SIVAL(p,0,reskey); p += 4;
1843 /* this must *not* be null terminated or w2k gets in a loop trying to set an
1844 acl on a dir (tridge) */
1845 len = srvstr_push(base_data, flags2, p,
1846 fname, PTR_DIFF(end_data, p),
1847 STR_TERMINATE_ASCII);
1851 len = PTR_DIFF(p, pdata);
1852 pad = (len + (align-1)) & ~(align-1);
1854 * offset to the next entry, the caller
1855 * will overwrite it for the last entry
1856 * that's why we always include the padding
1860 * set padding to zero
1863 memset(p, 0, pad - len);
1870 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1871 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
1873 SIVAL(p,0,reskey); p += 4;
1874 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1875 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1876 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1877 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1878 SOFF_T(p,0,file_size); p += 8;
1879 SOFF_T(p,0,allocation_size); p += 8;
1880 SIVAL(p,0,mode); p += 4;
1881 q = p; p += 4; /* q is placeholder for name length. */
1883 unsigned int ea_size = estimate_ea_size(conn, NULL,
1884 smb_fname->base_name);
1885 SIVAL(p,0,ea_size); /* Extended attributes */
1888 SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
1889 SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
1890 SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
1891 len = srvstr_push(base_data, flags2, p,
1892 fname, PTR_DIFF(end_data, p),
1893 STR_TERMINATE_ASCII);
1897 len = PTR_DIFF(p, pdata);
1898 pad = (len + (align-1)) & ~(align-1);
1900 * offset to the next entry, the caller
1901 * will overwrite it for the last entry
1902 * that's why we always include the padding
1906 * set padding to zero
1909 memset(p, 0, pad - len);
1916 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1917 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
1918 was_8_3 = mangle_is_8_3(fname, True, conn->params);
1920 SIVAL(p,0,reskey); p += 4;
1921 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1922 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1923 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1924 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1925 SOFF_T(p,0,file_size); p += 8;
1926 SOFF_T(p,0,allocation_size); p += 8;
1927 SIVAL(p,0,mode); p += 4;
1928 q = p; p += 4; /* q is placeholder for name length */
1930 unsigned int ea_size = estimate_ea_size(conn, NULL,
1931 smb_fname->base_name);
1932 SIVAL(p,0,ea_size); /* Extended attributes */
1935 /* Clear the short name buffer. This is
1936 * IMPORTANT as not doing so will trigger
1937 * a Win2k client bug. JRA.
1939 if (!was_8_3 && check_mangled_names) {
1940 char mangled_name[13]; /* mangled 8.3 name. */
1941 if (!name_to_8_3(fname,mangled_name,True,
1943 /* Error - mangle failed ! */
1944 memset(mangled_name,'\0',12);
1946 mangled_name[12] = 0;
1947 len = srvstr_push(base_data, flags2,
1948 p+2, mangled_name, 24,
1949 STR_UPPER|STR_UNICODE);
1952 memset(p + 2 + len,'\0',24 - len);
1959 SSVAL(p,0,0); p += 2; /* Reserved ? */
1960 SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
1961 SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
1962 len = srvstr_push(base_data, flags2, p,
1963 fname, PTR_DIFF(end_data, p),
1964 STR_TERMINATE_ASCII);
1968 len = PTR_DIFF(p, pdata);
1969 pad = (len + (align-1)) & ~(align-1);
1971 * offset to the next entry, the caller
1972 * will overwrite it for the last entry
1973 * that's why we always include the padding
1977 * set padding to zero
1980 memset(p, 0, pad - len);
1987 /* CIFS UNIX Extension. */
1989 case SMB_FIND_FILE_UNIX:
1990 case SMB_FIND_FILE_UNIX_INFO2:
1992 SIVAL(p,0,reskey); p+= 4; /* Used for continuing search. */
1994 /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
1996 if (info_level == SMB_FIND_FILE_UNIX) {
1997 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX\n"));
1998 p = store_file_unix_basic(conn, p,
1999 NULL, &smb_fname->st);
2000 len = srvstr_push(base_data, flags2, p,
2001 fname, PTR_DIFF(end_data, p),
2004 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
2005 p = store_file_unix_basic_info2(conn, p,
2006 NULL, &smb_fname->st);
2009 len = srvstr_push(base_data, flags2, p, fname,
2010 PTR_DIFF(end_data, p), 0);
2011 SIVAL(nameptr, 0, len);
2016 len = PTR_DIFF(p, pdata);
2017 pad = (len + (align-1)) & ~(align-1);
2019 * offset to the next entry, the caller
2020 * will overwrite it for the last entry
2021 * that's why we always include the padding
2025 * set padding to zero
2028 memset(p, 0, pad - len);
2033 /* End of SMB_QUERY_FILE_UNIX_BASIC */
2041 if (PTR_DIFF(p,pdata) > space_remaining) {
2042 *out_of_space = true;
2043 DEBUG(9,("smbd_marshall_dir_entry: out of space "
2044 "(wanted %u, had %d)\n",
2045 (unsigned int)PTR_DIFF(p,pdata),
2047 return false; /* Not finished - just out of space */
2050 /* Setup the last entry pointer, as an offset from base_data */
2051 *last_entry_off = PTR_DIFF(last_entry_ptr,base_data);
2052 /* Advance the data pointer to the next slot */
2058 bool smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
2059 connection_struct *conn,
2060 struct dptr_struct *dirptr,
2062 const char *path_mask,
2065 int requires_resume_key,
2073 int space_remaining,
2075 bool *got_exact_match,
2076 int *_last_entry_off,
2077 struct ea_list *name_list)
2080 const char *mask = NULL;
2081 long prev_dirpos = 0;
2084 struct smb_filename *smb_fname = NULL;
2085 struct smbd_dirptr_lanman2_state state;
2087 uint64_t last_entry_off = 0;
2091 state.info_level = info_level;
2092 state.check_mangled_names = lp_manglednames(conn->params);
2093 state.has_wild = dptr_has_wild(dirptr);
2094 state.got_exact_match = false;
2096 *out_of_space = false;
2097 *got_exact_match = false;
2099 p = strrchr_m(path_mask,'/');
2110 ok = smbd_dirptr_get_entry(ctx,
2116 smbd_dirptr_lanman2_match_fn,
2117 smbd_dirptr_lanman2_mode_fn,
2127 *got_exact_match = state.got_exact_match;
2129 ok = smbd_marshall_dir_entry(ctx,
2134 state.check_mangled_names,
2135 requires_resume_key,
2148 TALLOC_FREE(smb_fname);
2149 if (*out_of_space) {
2150 dptr_SeekDir(dirptr, prev_dirpos);
2157 *_last_entry_off = last_entry_off;
2161 static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
2162 connection_struct *conn,
2163 struct dptr_struct *dirptr,
2165 const char *path_mask,
2168 bool requires_resume_key,
2174 int space_remaining,
2176 bool *got_exact_match,
2177 int *last_entry_off,
2178 struct ea_list *name_list)
2181 const bool do_pad = true;
2183 if (info_level >= 1 && info_level <= 3) {
2184 /* No alignment on earlier info levels. */
2188 return smbd_dirptr_lanman2_entry(ctx, conn, dirptr, flags2,
2189 path_mask, dirtype, info_level,
2190 requires_resume_key, dont_descend, ask_sharemode,
2192 ppdata, base_data, end_data,
2194 out_of_space, got_exact_match,
2195 last_entry_off, name_list);
2198 /****************************************************************************
2199 Reply to a TRANS2_FINDFIRST.
2200 ****************************************************************************/
2202 static void call_trans2findfirst(connection_struct *conn,
2203 struct smb_request *req,
2204 char **pparams, int total_params,
2205 char **ppdata, int total_data,
2206 unsigned int max_data_bytes)
2208 /* We must be careful here that we don't return more than the
2209 allowed number of data bytes. If this means returning fewer than
2210 maxentries then so be it. We assume that the redirector has
2211 enough room for the fixed number of parameter bytes it has
2213 struct smb_filename *smb_dname = NULL;
2214 char *params = *pparams;
2215 char *pdata = *ppdata;
2219 uint16 findfirst_flags;
2220 bool close_after_first;
2222 bool requires_resume_key;
2224 char *directory = NULL;
2227 int last_entry_off=0;
2231 bool finished = False;
2232 bool dont_descend = False;
2233 bool out_of_space = False;
2234 int space_remaining;
2235 bool mask_contains_wcard = False;
2236 struct ea_list *ea_list = NULL;
2237 NTSTATUS ntstatus = NT_STATUS_OK;
2238 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
2239 TALLOC_CTX *ctx = talloc_tos();
2240 struct dptr_struct *dirptr = NULL;
2241 struct smbd_server_connection *sconn = smbd_server_conn;
2243 if (total_params < 13) {
2244 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2248 dirtype = SVAL(params,0);
2249 maxentries = SVAL(params,2);
2250 findfirst_flags = SVAL(params,4);
2251 close_after_first = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE);
2252 close_if_end = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2253 requires_resume_key = (findfirst_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2254 info_level = SVAL(params,6);
2256 DEBUG(3,("call_trans2findfirst: dirtype = %x, maxentries = %d, close_after_first=%d, \
2257 close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
2258 (unsigned int)dirtype, maxentries, close_after_first, close_if_end, requires_resume_key,
2259 info_level, max_data_bytes));
2262 /* W2K3 seems to treat zero as 1. */
2266 switch (info_level) {
2267 case SMB_FIND_INFO_STANDARD:
2268 case SMB_FIND_EA_SIZE:
2269 case SMB_FIND_EA_LIST:
2270 case SMB_FIND_FILE_DIRECTORY_INFO:
2271 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2272 case SMB_FIND_FILE_NAMES_INFO:
2273 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2274 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2275 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2277 case SMB_FIND_FILE_UNIX:
2278 case SMB_FIND_FILE_UNIX_INFO2:
2279 /* Always use filesystem for UNIX mtime query. */
2280 ask_sharemode = false;
2281 if (!lp_unix_extensions()) {
2282 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2287 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2291 srvstr_get_path_wcard(ctx, params, req->flags2, &directory,
2292 params+12, total_params - 12,
2293 STR_TERMINATE, &ntstatus, &mask_contains_wcard);
2294 if (!NT_STATUS_IS_OK(ntstatus)) {
2295 reply_nterror(req, ntstatus);
2299 ntstatus = filename_convert(ctx, conn,
2300 req->flags2 & FLAGS2_DFS_PATHNAMES,
2303 UCF_ALWAYS_ALLOW_WCARD_LCOMP),
2304 &mask_contains_wcard,
2306 if (!NT_STATUS_IS_OK(ntstatus)) {
2307 if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
2308 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2309 ERRSRV, ERRbadpath);
2312 reply_nterror(req, ntstatus);
2316 mask = smb_dname->original_lcomp;
2318 directory = smb_dname->base_name;
2320 p = strrchr_m(directory,'/');
2322 /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
2323 if((directory[0] == '.') && (directory[1] == '\0')) {
2324 mask = talloc_strdup(ctx,"*");
2326 reply_nterror(req, NT_STATUS_NO_MEMORY);
2329 mask_contains_wcard = True;
2331 directory = talloc_strdup(talloc_tos(), "./");
2333 reply_nterror(req, NT_STATUS_NO_MEMORY);
2340 DEBUG(5,("dir=%s, mask = %s\n",directory, mask));
2342 if (info_level == SMB_FIND_EA_LIST) {
2345 if (total_data < 4) {
2346 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2350 ea_size = IVAL(pdata,0);
2351 if (ea_size != total_data) {
2352 DEBUG(4,("call_trans2findfirst: Rejecting EA request with incorrect \
2353 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2354 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2358 if (!lp_ea_support(SNUM(conn))) {
2359 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
2363 /* Pull out the list of names. */
2364 ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
2366 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2371 *ppdata = (char *)SMB_REALLOC(
2372 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2373 if(*ppdata == NULL ) {
2374 reply_nterror(req, NT_STATUS_NO_MEMORY);
2378 data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2380 /* Realloc the params space */
2381 *pparams = (char *)SMB_REALLOC(*pparams, 10);
2382 if (*pparams == NULL) {
2383 reply_nterror(req, NT_STATUS_NO_MEMORY);
2388 /* Save the wildcard match and attribs we are using on this directory -
2389 needed as lanman2 assumes these are being saved between calls */
2391 ntstatus = dptr_create(conn,
2397 mask_contains_wcard,
2401 if (!NT_STATUS_IS_OK(ntstatus)) {
2402 reply_nterror(req, ntstatus);
2406 dptr_num = dptr_dnum(dirptr);
2407 DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
2409 /* Initialize per TRANS2_FIND_FIRST operation data */
2410 dptr_init_search_op(dirptr);
2412 /* We don't need to check for VOL here as this is returned by
2413 a different TRANS2 call. */
2415 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
2416 directory,lp_dontdescend(SNUM(conn))));
2417 if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
2418 dont_descend = True;
2421 space_remaining = max_data_bytes;
2422 out_of_space = False;
2424 for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
2425 bool got_exact_match = False;
2427 /* this is a heuristic to avoid seeking the dirptr except when
2428 absolutely necessary. It allows for a filename of about 40 chars */
2429 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2430 out_of_space = True;
2433 finished = !get_lanman2_dir_entry(ctx,
2437 mask,dirtype,info_level,
2438 requires_resume_key,dont_descend,
2441 space_remaining, &out_of_space,
2443 &last_entry_off, ea_list);
2446 if (finished && out_of_space)
2449 if (!finished && !out_of_space)
2453 * As an optimisation if we know we aren't looking
2454 * for a wildcard name (ie. the name matches the wildcard exactly)
2455 * then we can finish on any (first) match.
2456 * This speeds up large directory searches. JRA.
2462 /* Ensure space_remaining never goes -ve. */
2463 if (PTR_DIFF(p,pdata) > max_data_bytes) {
2464 space_remaining = 0;
2465 out_of_space = true;
2467 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2471 /* Check if we can close the dirptr */
2472 if(close_after_first || (finished && close_if_end)) {
2473 DEBUG(5,("call_trans2findfirst - (2) closing dptr_num %d\n", dptr_num));
2474 dptr_close(sconn, &dptr_num);
2478 * If there are no matching entries we must return ERRDOS/ERRbadfile -
2479 * from observation of NT. NB. This changes to ERRDOS,ERRnofiles if
2480 * the protocol level is less than NT1. Tested with smbclient. JRA.
2481 * This should fix the OS/2 client bug #2335.
2484 if(numentries == 0) {
2485 dptr_close(sconn, &dptr_num);
2486 if (get_Protocol() < PROTOCOL_NT1) {
2487 reply_force_doserror(req, ERRDOS, ERRnofiles);
2490 reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
2491 ERRDOS, ERRbadfile);
2496 /* At this point pdata points to numentries directory entries. */
2498 /* Set up the return parameter block */
2499 SSVAL(params,0,dptr_num);
2500 SSVAL(params,2,numentries);
2501 SSVAL(params,4,finished);
2502 SSVAL(params,6,0); /* Never an EA error */
2503 SSVAL(params,8,last_entry_off);
2505 send_trans2_replies(conn, req, params, 10, pdata, PTR_DIFF(p,pdata),
2508 if ((! *directory) && dptr_path(sconn, dptr_num)) {
2509 directory = talloc_strdup(talloc_tos(),dptr_path(sconn, dptr_num));
2511 reply_nterror(req, NT_STATUS_NO_MEMORY);
2515 DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2516 smb_fn_name(req->cmd),
2517 mask, directory, dirtype, numentries ) );
2520 * Force a name mangle here to ensure that the
2521 * mask as an 8.3 name is top of the mangled cache.
2522 * The reasons for this are subtle. Don't remove
2523 * this code unless you know what you are doing
2524 * (see PR#13758). JRA.
2527 if(!mangle_is_8_3_wildcards( mask, False, conn->params)) {
2528 char mangled_name[13];
2529 name_to_8_3(mask, mangled_name, True, conn->params);
2532 TALLOC_FREE(smb_dname);
2536 /****************************************************************************
2537 Reply to a TRANS2_FINDNEXT.
2538 ****************************************************************************/
2540 static void call_trans2findnext(connection_struct *conn,
2541 struct smb_request *req,
2542 char **pparams, int total_params,
2543 char **ppdata, int total_data,
2544 unsigned int max_data_bytes)
2546 /* We must be careful here that we don't return more than the
2547 allowed number of data bytes. If this means returning fewer than
2548 maxentries then so be it. We assume that the redirector has
2549 enough room for the fixed number of parameter bytes it has
2551 char *params = *pparams;
2552 char *pdata = *ppdata;
2558 uint16 findnext_flags;
2559 bool close_after_request;
2561 bool requires_resume_key;
2563 bool mask_contains_wcard = False;
2564 char *resume_name = NULL;
2565 const char *mask = NULL;
2566 const char *directory = NULL;
2570 int i, last_entry_off=0;
2571 bool finished = False;
2572 bool dont_descend = False;
2573 bool out_of_space = False;
2574 int space_remaining;
2575 struct ea_list *ea_list = NULL;
2576 NTSTATUS ntstatus = NT_STATUS_OK;
2577 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
2578 TALLOC_CTX *ctx = talloc_tos();
2579 struct dptr_struct *dirptr;
2580 struct smbd_server_connection *sconn = smbd_server_conn;
2582 if (total_params < 13) {
2583 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2587 dptr_num = SVAL(params,0);
2588 maxentries = SVAL(params,2);
2589 info_level = SVAL(params,4);
2590 resume_key = IVAL(params,6);
2591 findnext_flags = SVAL(params,10);
2592 close_after_request = (findnext_flags & FLAG_TRANS2_FIND_CLOSE);
2593 close_if_end = (findnext_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2594 requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2595 continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
2597 if (!continue_bit) {
2598 /* We only need resume_name if continue_bit is zero. */
2599 srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
2601 total_params - 12, STR_TERMINATE, &ntstatus,
2602 &mask_contains_wcard);
2603 if (!NT_STATUS_IS_OK(ntstatus)) {
2604 /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
2605 complain (it thinks we're asking for the directory above the shared
2606 path or an invalid name). Catch this as the resume name is only compared, never used in
2607 a file access. JRA. */
2608 srvstr_pull_talloc(ctx, params, req->flags2,
2609 &resume_name, params+12,
2613 if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
2614 reply_nterror(req, ntstatus);
2620 DEBUG(3,("call_trans2findnext: dirhandle = %d, max_data_bytes = %d, maxentries = %d, \
2621 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
2622 resume_key = %d resume name = %s continue=%d level = %d\n",
2623 dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end,
2624 requires_resume_key, resume_key,
2625 resume_name ? resume_name : "(NULL)", continue_bit, info_level));
2628 /* W2K3 seems to treat zero as 1. */
2632 switch (info_level) {
2633 case SMB_FIND_INFO_STANDARD:
2634 case SMB_FIND_EA_SIZE:
2635 case SMB_FIND_EA_LIST:
2636 case SMB_FIND_FILE_DIRECTORY_INFO:
2637 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2638 case SMB_FIND_FILE_NAMES_INFO:
2639 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2640 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2641 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2643 case SMB_FIND_FILE_UNIX:
2644 case SMB_FIND_FILE_UNIX_INFO2:
2645 /* Always use filesystem for UNIX mtime query. */
2646 ask_sharemode = false;
2647 if (!lp_unix_extensions()) {
2648 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2653 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2657 if (info_level == SMB_FIND_EA_LIST) {
2660 if (total_data < 4) {
2661 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2665 ea_size = IVAL(pdata,0);
2666 if (ea_size != total_data) {
2667 DEBUG(4,("call_trans2findnext: Rejecting EA request with incorrect \
2668 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2669 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2673 if (!lp_ea_support(SNUM(conn))) {
2674 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
2678 /* Pull out the list of names. */
2679 ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
2681 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2686 *ppdata = (char *)SMB_REALLOC(
2687 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2688 if(*ppdata == NULL) {
2689 reply_nterror(req, NT_STATUS_NO_MEMORY);
2694 data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2696 /* Realloc the params space */
2697 *pparams = (char *)SMB_REALLOC(*pparams, 6*SIZEOFWORD);
2698 if(*pparams == NULL ) {
2699 reply_nterror(req, NT_STATUS_NO_MEMORY);
2705 /* Check that the dptr is valid */
2706 if(!(dirptr = dptr_fetch_lanman2(sconn, dptr_num))) {
2707 reply_nterror(req, STATUS_NO_MORE_FILES);
2711 directory = dptr_path(sconn, dptr_num);
2713 /* Get the wildcard mask from the dptr */
2714 if((p = dptr_wcard(sconn, dptr_num))== NULL) {
2715 DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
2716 reply_nterror(req, STATUS_NO_MORE_FILES);
2722 /* Get the attr mask from the dptr */
2723 dirtype = dptr_attr(sconn, dptr_num);
2725 DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld)\n",
2726 dptr_num, mask, dirtype,
2728 dptr_TellDir(dirptr)));
2730 /* Initialize per TRANS2_FIND_NEXT operation data */
2731 dptr_init_search_op(dirptr);
2733 /* We don't need to check for VOL here as this is returned by
2734 a different TRANS2 call. */
2736 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
2737 directory,lp_dontdescend(SNUM(conn))));
2738 if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
2739 dont_descend = True;
2742 space_remaining = max_data_bytes;
2743 out_of_space = False;
2746 * Seek to the correct position. We no longer use the resume key but
2747 * depend on the last file name instead.
2750 if(!continue_bit && resume_name && *resume_name) {
2753 long current_pos = 0;
2755 * Remember, name_to_8_3 is called by
2756 * get_lanman2_dir_entry(), so the resume name
2757 * could be mangled. Ensure we check the unmangled name.
2760 if (mangle_is_mangled(resume_name, conn->params)) {
2761 char *new_resume_name = NULL;
2762 mangle_lookup_name_from_8_3(ctx,
2766 if (new_resume_name) {
2767 resume_name = new_resume_name;
2772 * Fix for NT redirector problem triggered by resume key indexes
2773 * changing between directory scans. We now return a resume key of 0
2774 * and instead look for the filename to continue from (also given
2775 * to us by NT/95/smbfs/smbclient). If no other scans have been done between the
2776 * findfirst/findnext (as is usual) then the directory pointer
2777 * should already be at the correct place.
2780 finished = !dptr_SearchDir(dirptr, resume_name, ¤t_pos, &st);
2781 } /* end if resume_name && !continue_bit */
2783 for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
2784 bool got_exact_match = False;
2786 /* this is a heuristic to avoid seeking the dirptr except when
2787 absolutely necessary. It allows for a filename of about 40 chars */
2788 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2789 out_of_space = True;
2792 finished = !get_lanman2_dir_entry(ctx,
2796 mask,dirtype,info_level,
2797 requires_resume_key,dont_descend,
2800 space_remaining, &out_of_space,
2802 &last_entry_off, ea_list);
2805 if (finished && out_of_space)
2808 if (!finished && !out_of_space)
2812 * As an optimisation if we know we aren't looking
2813 * for a wildcard name (ie. the name matches the wildcard exactly)
2814 * then we can finish on any (first) match.
2815 * This speeds up large directory searches. JRA.
2821 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2824 DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2825 smb_fn_name(req->cmd),
2826 mask, directory, dirtype, numentries ) );
2828 /* Check if we can close the dirptr */
2829 if(close_after_request || (finished && close_if_end)) {
2830 DEBUG(5,("call_trans2findnext: closing dptr_num = %d\n", dptr_num));
2831 dptr_close(sconn, &dptr_num); /* This frees up the saved mask */
2834 /* Set up the return parameter block */
2835 SSVAL(params,0,numentries);
2836 SSVAL(params,2,finished);
2837 SSVAL(params,4,0); /* Never an EA error */
2838 SSVAL(params,6,last_entry_off);
2840 send_trans2_replies(conn, req, params, 8, pdata, PTR_DIFF(p,pdata),
2846 unsigned char *create_volume_objectid(connection_struct *conn, unsigned char objid[16])
2848 E_md4hash(lp_servicename(SNUM(conn)),objid);
2852 static void samba_extended_info_version(struct smb_extended_info *extended_info)
2854 SMB_ASSERT(extended_info != NULL);
2856 extended_info->samba_magic = SAMBA_EXTENDED_INFO_MAGIC;
2857 extended_info->samba_version = ((SAMBA_VERSION_MAJOR & 0xff) << 24)
2858 | ((SAMBA_VERSION_MINOR & 0xff) << 16)
2859 | ((SAMBA_VERSION_RELEASE & 0xff) << 8);
2860 #ifdef SAMBA_VERSION_REVISION
2861 extended_info->samba_version |= (tolower(*SAMBA_VERSION_REVISION) - 'a' + 1) & 0xff;
2863 extended_info->samba_subversion = 0;
2864 #ifdef SAMBA_VERSION_RC_RELEASE
2865 extended_info->samba_subversion |= (SAMBA_VERSION_RC_RELEASE & 0xff) << 24;
2867 #ifdef SAMBA_VERSION_PRE_RELEASE
2868 extended_info->samba_subversion |= (SAMBA_VERSION_PRE_RELEASE & 0xff) << 16;
2871 #ifdef SAMBA_VERSION_VENDOR_PATCH
2872 extended_info->samba_subversion |= (SAMBA_VERSION_VENDOR_PATCH & 0xffff);
2874 extended_info->samba_gitcommitdate = 0;
2875 #ifdef SAMBA_VERSION_GIT_COMMIT_TIME
2876 unix_to_nt_time(&extended_info->samba_gitcommitdate, SAMBA_VERSION_GIT_COMMIT_TIME);
2879 memset(extended_info->samba_version_string, 0,
2880 sizeof(extended_info->samba_version_string));
2882 snprintf (extended_info->samba_version_string,
2883 sizeof(extended_info->samba_version_string),
2884 "%s", samba_version_string());
2887 NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
2888 TALLOC_CTX *mem_ctx,
2889 uint16_t info_level,
2891 unsigned int max_data_bytes,
2895 char *pdata, *end_data;
2896 int data_len = 0, len;
2897 const char *vname = volume_label(SNUM(conn));
2898 int snum = SNUM(conn);
2899 char *fstype = lp_fstype(SNUM(conn));
2900 uint32 additional_flags = 0;
2901 struct smb_filename smb_fname_dot;
2905 if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
2906 DEBUG(0,("smbd_do_qfsinfo: not an allowed "
2907 "info level (0x%x) on IPC$.\n",
2908 (unsigned int)info_level));
2909 return NT_STATUS_ACCESS_DENIED;
2913 DEBUG(3,("smbd_do_qfsinfo: level = %d\n", info_level));
2915 ZERO_STRUCT(smb_fname_dot);
2916 smb_fname_dot.base_name = discard_const_p(char, ".");
2918 if(SMB_VFS_STAT(conn, &smb_fname_dot) != 0) {
2919 DEBUG(2,("stat of . failed (%s)\n", strerror(errno)));
2920 return map_nt_error_from_unix(errno);
2923 st = smb_fname_dot.st;
2925 *ppdata = (char *)SMB_REALLOC(
2926 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2927 if (*ppdata == NULL) {
2928 return NT_STATUS_NO_MEMORY;
2932 memset((char *)pdata,'\0',max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2933 end_data = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2935 switch (info_level) {
2936 case SMB_INFO_ALLOCATION:
2938 uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2940 if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
2941 return map_nt_error_from_unix(errno);
2944 block_size = lp_block_size(snum);
2945 if (bsize < block_size) {
2946 uint64_t factor = block_size/bsize;
2951 if (bsize > block_size) {
2952 uint64_t factor = bsize/block_size;
2957 bytes_per_sector = 512;
2958 sectors_per_unit = bsize/bytes_per_sector;
2960 DEBUG(5,("smbd_do_qfsinfo : SMB_INFO_ALLOCATION id=%x, bsize=%u, cSectorUnit=%u, \
2961 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_ex_dev, (unsigned int)bsize, (unsigned int)sectors_per_unit,
2962 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2964 SIVAL(pdata,l1_idFileSystem,st.st_ex_dev);
2965 SIVAL(pdata,l1_cSectorUnit,sectors_per_unit);
2966 SIVAL(pdata,l1_cUnit,dsize);
2967 SIVAL(pdata,l1_cUnitAvail,dfree);
2968 SSVAL(pdata,l1_cbSector,bytes_per_sector);
2972 case SMB_INFO_VOLUME:
2973 /* Return volume name */
2975 * Add volume serial number - hash of a combination of
2976 * the called hostname and the service name.
2978 SIVAL(pdata,0,str_checksum(lp_servicename(snum)) ^ (str_checksum(get_local_machine_name())<<16) );
2980 * Win2k3 and previous mess this up by sending a name length
2981 * one byte short. I believe only older clients (OS/2 Win9x) use
2982 * this call so try fixing this by adding a terminating null to
2983 * the pushed string. The change here was adding the STR_TERMINATE. JRA.
2987 pdata+l2_vol_szVolLabel, vname,
2988 PTR_DIFF(end_data, pdata+l2_vol_szVolLabel),
2989 STR_NOALIGN|STR_TERMINATE);
2990 SCVAL(pdata,l2_vol_cch,len);
2991 data_len = l2_vol_szVolLabel + len;
2992 DEBUG(5,("smbd_do_qfsinfo : time = %x, namelen = %d, name = %s\n",
2993 (unsigned)convert_timespec_to_time_t(st.st_ex_ctime),
2997 case SMB_QUERY_FS_ATTRIBUTE_INFO:
2998 case SMB_FS_ATTRIBUTE_INFORMATION:
3000 additional_flags = 0;
3001 #if defined(HAVE_SYS_QUOTAS)
3002 additional_flags |= FILE_VOLUME_QUOTAS;
3005 if(lp_nt_acl_support(SNUM(conn))) {
3006 additional_flags |= FILE_PERSISTENT_ACLS;
3009 /* Capabilities are filled in at connection time through STATVFS call */
3010 additional_flags |= conn->fs_capabilities;
3011 additional_flags |= lp_parm_int(conn->params->service,
3012 "share", "fake_fscaps",
3015 SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
3016 FILE_SUPPORTS_OBJECT_IDS|FILE_UNICODE_ON_DISK|
3017 additional_flags); /* FS ATTRIBUTES */
3019 SIVAL(pdata,4,255); /* Max filename component length */
3020 /* NOTE! the fstype must *not* be null terminated or win98 won't recognise it
3021 and will think we can't do long filenames */
3022 len = srvstr_push(pdata, flags2, pdata+12, fstype,
3023 PTR_DIFF(end_data, pdata+12),
3026 data_len = 12 + len;
3029 case SMB_QUERY_FS_LABEL_INFO:
3030 case SMB_FS_LABEL_INFORMATION:
3031 len = srvstr_push(pdata, flags2, pdata+4, vname,
3032 PTR_DIFF(end_data, pdata+4), 0);
3037 case SMB_QUERY_FS_VOLUME_INFO:
3038 case SMB_FS_VOLUME_INFORMATION:
3041 * Add volume serial number - hash of a combination of
3042 * the called hostname and the service name.
3044 SIVAL(pdata,8,str_checksum(lp_servicename(snum)) ^
3045 (str_checksum(get_local_machine_name())<<16));
3047 /* Max label len is 32 characters. */
3048 len = srvstr_push(pdata, flags2, pdata+18, vname,
3049 PTR_DIFF(end_data, pdata+18),
3051 SIVAL(pdata,12,len);
3054 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_VOLUME_INFO namelen = %d, vol=%s serv=%s\n",
3055 (int)strlen(vname),vname, lp_servicename(snum)));
3058 case SMB_QUERY_FS_SIZE_INFO:
3059 case SMB_FS_SIZE_INFORMATION:
3061 uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3063 if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3064 return map_nt_error_from_unix(errno);
3066 block_size = lp_block_size(snum);
3067 if (bsize < block_size) {
3068 uint64_t factor = block_size/bsize;
3073 if (bsize > block_size) {
3074 uint64_t factor = bsize/block_size;
3079 bytes_per_sector = 512;
3080 sectors_per_unit = bsize/bytes_per_sector;
3081 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_SIZE_INFO bsize=%u, cSectorUnit=%u, \
3082 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
3083 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3084 SBIG_UINT(pdata,0,dsize);
3085 SBIG_UINT(pdata,8,dfree);
3086 SIVAL(pdata,16,sectors_per_unit);
3087 SIVAL(pdata,20,bytes_per_sector);
3091 case SMB_FS_FULL_SIZE_INFORMATION:
3093 uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3095 if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3096 return map_nt_error_from_unix(errno);
3098 block_size = lp_block_size(snum);
3099 if (bsize < block_size) {
3100 uint64_t factor = block_size/bsize;
3105 if (bsize > block_size) {
3106 uint64_t factor = bsize/block_size;
3111 bytes_per_sector = 512;
3112 sectors_per_unit = bsize/bytes_per_sector;
3113 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_FULL_SIZE_INFO bsize=%u, cSectorUnit=%u, \
3114 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
3115 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3116 SBIG_UINT(pdata,0,dsize); /* Total Allocation units. */
3117 SBIG_UINT(pdata,8,dfree); /* Caller available allocation units. */
3118 SBIG_UINT(pdata,16,dfree); /* Actual available allocation units. */
3119 SIVAL(pdata,24,sectors_per_unit); /* Sectors per allocation unit. */
3120 SIVAL(pdata,28,bytes_per_sector); /* Bytes per sector. */
3124 case SMB_QUERY_FS_DEVICE_INFO:
3125 case SMB_FS_DEVICE_INFORMATION:
3127 SIVAL(pdata,0,0); /* dev type */
3128 SIVAL(pdata,4,0); /* characteristics */
3131 #ifdef HAVE_SYS_QUOTAS
3132 case SMB_FS_QUOTA_INFORMATION:
3134 * what we have to send --metze:
3136 * Unknown1: 24 NULL bytes
3137 * Soft Quota Treshold: 8 bytes seems like uint64_t or so
3138 * Hard Quota Limit: 8 bytes seems like uint64_t or so
3139 * Quota Flags: 2 byte :
3140 * Unknown3: 6 NULL bytes
3144 * details for Quota Flags:
3146 * 0x0020 Log Limit: log if the user exceeds his Hard Quota
3147 * 0x0010 Log Warn: log if the user exceeds his Soft Quota
3148 * 0x0002 Deny Disk: deny disk access when the user exceeds his Hard Quota
3149 * 0x0001 Enable Quotas: enable quota for this fs
3153 /* we need to fake up a fsp here,
3154 * because its not send in this call
3157 SMB_NTQUOTA_STRUCT quotas;
3160 ZERO_STRUCT(quotas);
3166 if (conn->server_info->utok.uid != sec_initial_uid()) {
3167 DEBUG(0,("set_user_quota: access_denied "
3168 "service [%s] user [%s]\n",
3169 lp_servicename(SNUM(conn)),
3170 conn->server_info->unix_name));
3171 return NT_STATUS_ACCESS_DENIED;
3174 if (vfs_get_ntquota(&fsp, SMB_USER_FS_QUOTA_TYPE, NULL, "as)!=0) {
3175 DEBUG(0,("vfs_get_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
3176 return map_nt_error_from_unix(errno);
3181 DEBUG(10,("SMB_FS_QUOTA_INFORMATION: for service [%s]\n",
3182 lp_servicename(SNUM(conn))));
3184 /* Unknown1 24 NULL bytes*/
3185 SBIG_UINT(pdata,0,(uint64_t)0);
3186 SBIG_UINT(pdata,8,(uint64_t)0);
3187 SBIG_UINT(pdata,16,(uint64_t)0);
3189 /* Default Soft Quota 8 bytes */
3190 SBIG_UINT(pdata,24,quotas.softlim);
3192 /* Default Hard Quota 8 bytes */
3193 SBIG_UINT(pdata,32,quotas.hardlim);
3195 /* Quota flag 2 bytes */
3196 SSVAL(pdata,40,quotas.qflags);
3198 /* Unknown3 6 NULL bytes */
3204 #endif /* HAVE_SYS_QUOTAS */
3205 case SMB_FS_OBJECTID_INFORMATION:
3207 unsigned char objid[16];
3208 struct smb_extended_info extended_info;
3209 memcpy(pdata,create_volume_objectid(conn, objid),16);
3210 samba_extended_info_version (&extended_info);
3211 SIVAL(pdata,16,extended_info.samba_magic);
3212 SIVAL(pdata,20,extended_info.samba_version);
3213 SIVAL(pdata,24,extended_info.samba_subversion);
3214 SBIG_UINT(pdata,28,extended_info.samba_gitcommitdate);
3215 memcpy(pdata+36,extended_info.samba_version_string,28);
3221 * Query the version and capabilities of the CIFS UNIX extensions
3225 case SMB_QUERY_CIFS_UNIX_INFO:
3227 bool large_write = lp_min_receive_file_size() &&
3228 !srv_is_signing_active(smbd_server_conn);
3229 bool large_read = !srv_is_signing_active(smbd_server_conn);
3230 int encrypt_caps = 0;
3232 if (!lp_unix_extensions()) {
3233 return NT_STATUS_INVALID_LEVEL;
3236 switch (conn->encrypt_level) {
3242 encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP;
3245 encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP|
3246 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP;
3247 large_write = false;
3253 SSVAL(pdata,0,CIFS_UNIX_MAJOR_VERSION);
3254 SSVAL(pdata,2,CIFS_UNIX_MINOR_VERSION);
3256 /* We have POSIX ACLs, pathname, encryption,
3257 * large read/write, and locking capability. */
3259 SBIG_UINT(pdata,4,((uint64_t)(
3260 CIFS_UNIX_POSIX_ACLS_CAP|
3261 CIFS_UNIX_POSIX_PATHNAMES_CAP|
3262 CIFS_UNIX_FCNTL_LOCKS_CAP|
3263 CIFS_UNIX_EXTATTR_CAP|
3264 CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP|
3266 (large_read ? CIFS_UNIX_LARGE_READ_CAP : 0) |
3268 CIFS_UNIX_LARGE_WRITE_CAP : 0))));
3272 case SMB_QUERY_POSIX_FS_INFO:
3275 vfs_statvfs_struct svfs;
3277 if (!lp_unix_extensions()) {
3278 return NT_STATUS_INVALID_LEVEL;
3281 rc = SMB_VFS_STATVFS(conn, ".", &svfs);
3285 SIVAL(pdata,0,svfs.OptimalTransferSize);
3286 SIVAL(pdata,4,svfs.BlockSize);
3287 SBIG_UINT(pdata,8,svfs.TotalBlocks);
3288 SBIG_UINT(pdata,16,svfs.BlocksAvail);
3289 SBIG_UINT(pdata,24,svfs.UserBlocksAvail);
3290 SBIG_UINT(pdata,32,svfs.TotalFileNodes);
3291 SBIG_UINT(pdata,40,svfs.FreeFileNodes);
3292 SBIG_UINT(pdata,48,svfs.FsIdentifier);
3293 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_POSIX_FS_INFO succsessful\n"));
3295 } else if (rc == EOPNOTSUPP) {
3296 return NT_STATUS_INVALID_LEVEL;
3297 #endif /* EOPNOTSUPP */
3299 DEBUG(0,("vfs_statvfs() failed for service [%s]\n",lp_servicename(SNUM(conn))));
3300 return NT_STATUS_DOS(ERRSRV, ERRerror);
3305 case SMB_QUERY_POSIX_WHOAMI:
3311 if (!lp_unix_extensions()) {
3312 return NT_STATUS_INVALID_LEVEL;
3315 if (max_data_bytes < 40) {
3316 return NT_STATUS_BUFFER_TOO_SMALL;
3319 /* We ARE guest if global_sid_Builtin_Guests is
3320 * in our list of SIDs.
3322 if (nt_token_check_sid(&global_sid_Builtin_Guests,
3323 conn->server_info->ptok)) {
3324 flags |= SMB_WHOAMI_GUEST;
3327 /* We are NOT guest if global_sid_Authenticated_Users
3328 * is in our list of SIDs.
3330 if (nt_token_check_sid(&global_sid_Authenticated_Users,
3331 conn->server_info->ptok)) {
3332 flags &= ~SMB_WHOAMI_GUEST;
3335 /* NOTE: 8 bytes for UID/GID, irrespective of native
3336 * platform size. This matches
3337 * SMB_QUERY_FILE_UNIX_BASIC and friends.
3339 data_len = 4 /* flags */
3346 + 4 /* pad/reserved */
3347 + (conn->server_info->utok.ngroups * 8)
3349 + (conn->server_info->ptok->num_sids *
3353 SIVAL(pdata, 0, flags);
3354 SIVAL(pdata, 4, SMB_WHOAMI_MASK);
3356 (uint64_t)conn->server_info->utok.uid);
3357 SBIG_UINT(pdata, 16,
3358 (uint64_t)conn->server_info->utok.gid);
3361 if (data_len >= max_data_bytes) {
3362 /* Potential overflow, skip the GIDs and SIDs. */
3364 SIVAL(pdata, 24, 0); /* num_groups */
3365 SIVAL(pdata, 28, 0); /* num_sids */
3366 SIVAL(pdata, 32, 0); /* num_sid_bytes */
3367 SIVAL(pdata, 36, 0); /* reserved */
3373 SIVAL(pdata, 24, conn->server_info->utok.ngroups);
3374 SIVAL(pdata, 28, conn->server_info->num_sids);
3376 /* We walk the SID list twice, but this call is fairly
3377 * infrequent, and I don't expect that it's performance
3378 * sensitive -- jpeach
3380 for (i = 0, sid_bytes = 0;
3381 i < conn->server_info->ptok->num_sids; ++i) {
3382 sid_bytes += ndr_size_dom_sid(
3383 &conn->server_info->ptok->user_sids[i],
3387 /* SID list byte count */
3388 SIVAL(pdata, 32, sid_bytes);
3390 /* 4 bytes pad/reserved - must be zero */
3391 SIVAL(pdata, 36, 0);
3395 for (i = 0; i < conn->server_info->utok.ngroups; ++i) {
3396 SBIG_UINT(pdata, data_len,
3397 (uint64_t)conn->server_info->utok.groups[i]);
3403 i < conn->server_info->ptok->num_sids; ++i) {
3404 int sid_len = ndr_size_dom_sid(
3405 &conn->server_info->ptok->user_sids[i],
3408 sid_linearize(pdata + data_len, sid_len,
3409 &conn->server_info->ptok->user_sids[i]);
3410 data_len += sid_len;
3416 case SMB_MAC_QUERY_FS_INFO:
3418 * Thursby MAC extension... ONLY on NTFS filesystems
3419 * once we do streams then we don't need this
3421 if (strequal(lp_fstype(SNUM(conn)),"NTFS")) {
3423 SIVAL(pdata,84,0x100); /* Don't support mac... */
3428 return NT_STATUS_INVALID_LEVEL;
3431 *ret_data_len = data_len;
3432 return NT_STATUS_OK;
3435 /****************************************************************************
3436 Reply to a TRANS2_QFSINFO (query filesystem info).
3437 ****************************************************************************/
3439 static void call_trans2qfsinfo(connection_struct *conn,
3440 struct smb_request *req,
3441 char **pparams, int total_params,
3442 char **ppdata, int total_data,
3443 unsigned int max_data_bytes)
3445 char *params = *pparams;
3446 uint16_t info_level;
3450 if (total_params < 2) {
3451 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3455 info_level = SVAL(params,0);
3457 if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
3458 if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
3459 DEBUG(0,("call_trans2qfsinfo: encryption required "
3460 "and info level 0x%x sent.\n",
3461 (unsigned int)info_level));
3462 exit_server_cleanly("encryption required "
3468 DEBUG(3,("call_trans2qfsinfo: level = %d\n", info_level));
3470 status = smbd_do_qfsinfo(conn, req,
3475 if (!NT_STATUS_IS_OK(status)) {
3476 reply_nterror(req, status);
3480 send_trans2_replies(conn, req, params, 0, *ppdata, data_len,
3483 DEBUG( 4, ( "%s info_level = %d\n",
3484 smb_fn_name(req->cmd), info_level) );
3489 /****************************************************************************
3490 Reply to a TRANS2_SETFSINFO (set filesystem info).
3491 ****************************************************************************/
3493 static void call_trans2setfsinfo(connection_struct *conn,
3494 struct smb_request *req,
3495 char **pparams, int total_params,
3496 char **ppdata, int total_data,
3497 unsigned int max_data_bytes)
3499 char *pdata = *ppdata;
3500 char *params = *pparams;
3503 DEBUG(10,("call_trans2setfsinfo: for service [%s]\n",lp_servicename(SNUM(conn))));
3506 if (total_params < 4) {
3507 DEBUG(0,("call_trans2setfsinfo: requires total_params(%d) >= 4 bytes!\n",
3509 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3513 info_level = SVAL(params,2);
3516 if (info_level != SMB_REQUEST_TRANSPORT_ENCRYPTION &&
3517 info_level != SMB_SET_CIFS_UNIX_INFO) {
3518 DEBUG(0,("call_trans2setfsinfo: not an allowed "
3519 "info level (0x%x) on IPC$.\n",
3520 (unsigned int)info_level));
3521 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3526 if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
3527 if (info_level != SMB_REQUEST_TRANSPORT_ENCRYPTION) {
3528 DEBUG(0,("call_trans2setfsinfo: encryption required "
3529 "and info level 0x%x sent.\n",
3530 (unsigned int)info_level));
3531 exit_server_cleanly("encryption required "
3537 switch(info_level) {
3538 case SMB_SET_CIFS_UNIX_INFO:
3540 uint16 client_unix_major;
3541 uint16 client_unix_minor;
3542 uint32 client_unix_cap_low;
3543 uint32 client_unix_cap_high;
3545 if (!lp_unix_extensions()) {
3547 NT_STATUS_INVALID_LEVEL);
3551 /* There should be 12 bytes of capabilities set. */
3552 if (total_data < 8) {
3555 NT_STATUS_INVALID_PARAMETER);
3558 client_unix_major = SVAL(pdata,0);
3559 client_unix_minor = SVAL(pdata,2);
3560 client_unix_cap_low = IVAL(pdata,4);
3561 client_unix_cap_high = IVAL(pdata,8);
3562 /* Just print these values for now. */
3563 DEBUG(10,("call_trans2setfsinfo: set unix info. major = %u, minor = %u \
3564 cap_low = 0x%x, cap_high = 0x%x\n",
3565 (unsigned int)client_unix_major,
3566 (unsigned int)client_unix_minor,
3567 (unsigned int)client_unix_cap_low,
3568 (unsigned int)client_unix_cap_high ));
3570 /* Here is where we must switch to posix pathname processing... */
3571 if (client_unix_cap_low & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3572 lp_set_posix_pathnames();
3573 mangle_change_to_posix();
3576 if ((client_unix_cap_low & CIFS_UNIX_FCNTL_LOCKS_CAP) &&
3577 !(client_unix_cap_low & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP)) {
3578 /* Client that knows how to do posix locks,
3579 * but not posix open/mkdir operations. Set a
3580 * default type for read/write checks. */
3582 lp_set_posix_default_cifsx_readwrite_locktype(POSIX_LOCK);
3588 case SMB_REQUEST_TRANSPORT_ENCRYPTION:
3591 size_t param_len = 0;
3592 size_t data_len = total_data;
3594 if (!lp_unix_extensions()) {
3597 NT_STATUS_INVALID_LEVEL);
3601 if (lp_smb_encrypt(SNUM(conn)) == false) {
3604 NT_STATUS_NOT_SUPPORTED);
3608 if (smbd_server_conn->smb1.echo_handler.trusted_fde) {
3609 DEBUG( 2,("call_trans2setfsinfo: "
3610 "request transport encryption disabled"
3611 "with 'fork echo handler = yes'\n"));
3614 NT_STATUS_NOT_SUPPORTED);
3618 DEBUG( 4,("call_trans2setfsinfo: "
3619 "request transport encryption.\n"));
3621 status = srv_request_encryption_setup(conn,
3622 (unsigned char **)ppdata,
3624 (unsigned char **)pparams,
3627 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
3628 !NT_STATUS_IS_OK(status)) {
3629 reply_nterror(req, status);
3633 send_trans2_replies(conn, req,
3640 if (NT_STATUS_IS_OK(status)) {
3641 /* Server-side transport
3642 * encryption is now *on*. */
3643 status = srv_encryption_start(conn);
3644 if (!NT_STATUS_IS_OK(status)) {
3645 exit_server_cleanly(
3646 "Failure in setting "
3647 "up encrypted transport");
3653 case SMB_FS_QUOTA_INFORMATION:
3655 files_struct *fsp = NULL;
3656 SMB_NTQUOTA_STRUCT quotas;
3658 ZERO_STRUCT(quotas);
3661 if ((conn->server_info->utok.uid != sec_initial_uid())
3662 ||!CAN_WRITE(conn)) {
3663 DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
3664 lp_servicename(SNUM(conn)),
3665 conn->server_info->unix_name));
3666 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3670 /* note: normaly there're 48 bytes,
3671 * but we didn't use the last 6 bytes for now
3674 fsp = file_fsp(req, SVAL(params,0));
3676 if (!check_fsp_ntquota_handle(conn, req,
3678 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3680 req, NT_STATUS_INVALID_HANDLE);
3684 if (total_data < 42) {
3685 DEBUG(0,("call_trans2setfsinfo: SET_FS_QUOTA: requires total_data(%d) >= 42 bytes!\n",
3689 NT_STATUS_INVALID_PARAMETER);
3693 /* unknown_1 24 NULL bytes in pdata*/
3695 /* the soft quotas 8 bytes (uint64_t)*/
3696 quotas.softlim = (uint64_t)IVAL(pdata,24);
3697 #ifdef LARGE_SMB_OFF_T
3698 quotas.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
3699 #else /* LARGE_SMB_OFF_T */
3700 if ((IVAL(pdata,28) != 0)&&
3701 ((quotas.softlim != 0xFFFFFFFF)||
3702 (IVAL(pdata,28)!=0xFFFFFFFF))) {
3703 /* more than 32 bits? */
3706 NT_STATUS_INVALID_PARAMETER);
3709 #endif /* LARGE_SMB_OFF_T */
3711 /* the hard quotas 8 bytes (uint64_t)*/
3712 quotas.hardlim = (uint64_t)IVAL(pdata,32);
3713 #ifdef LARGE_SMB_OFF_T
3714 quotas.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
3715 #else /* LARGE_SMB_OFF_T */
3716 if ((IVAL(pdata,36) != 0)&&
3717 ((quotas.hardlim != 0xFFFFFFFF)||
3718 (IVAL(pdata,36)!=0xFFFFFFFF))) {
3719 /* more than 32 bits? */
3722 NT_STATUS_INVALID_PARAMETER);
3725 #endif /* LARGE_SMB_OFF_T */
3727 /* quota_flags 2 bytes **/
3728 quotas.qflags = SVAL(pdata,40);
3730 /* unknown_2 6 NULL bytes follow*/
3732 /* now set the quotas */
3733 if (vfs_set_ntquota(fsp, SMB_USER_FS_QUOTA_TYPE, NULL, "as)!=0) {
3734 DEBUG(0,("vfs_set_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
3735 reply_nterror(req, map_nt_error_from_unix(errno));
3742 DEBUG(3,("call_trans2setfsinfo: unknown level (0x%X) not implemented yet.\n",
3744 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3750 * sending this reply works fine,
3751 * but I'm not sure it's the same
3752 * like windows do...
3755 reply_outbuf(req, 10, 0);
3758 #if defined(HAVE_POSIX_ACLS)
3759 /****************************************************************************
3760 Utility function to count the number of entries in a POSIX acl.
3761 ****************************************************************************/
3763 static unsigned int count_acl_entries(connection_struct *conn, SMB_ACL_T posix_acl)
3765 unsigned int ace_count = 0;
3766 int entry_id = SMB_ACL_FIRST_ENTRY;
3767 SMB_ACL_ENTRY_T entry;
3769 while ( posix_acl && (SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1)) {
3771 if (entry_id == SMB_ACL_FIRST_ENTRY) {
3772 entry_id = SMB_ACL_NEXT_ENTRY;
3779 /****************************************************************************
3780 Utility function to marshall a POSIX acl into wire format.
3781 ****************************************************************************/
3783 static bool marshall_posix_acl(connection_struct *conn, char *pdata, SMB_STRUCT_STAT *pst, SMB_ACL_T posix_acl)
3785 int entry_id = SMB_ACL_FIRST_ENTRY;
3786 SMB_ACL_ENTRY_T entry;
3788 while ( posix_acl && (SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1)) {
3789 SMB_ACL_TAG_T tagtype;
3790 SMB_ACL_PERMSET_T permset;
3791 unsigned char perms = 0;
3792 unsigned int own_grp;
3795 if (entry_id == SMB_ACL_FIRST_ENTRY) {
3796 entry_id = SMB_ACL_NEXT_ENTRY;
3799 if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1) {
3800 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_TAG_TYPE failed.\n"));
3804 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, entry, &permset) == -1) {
3805 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_PERMSET failed.\n"));
3809 perms |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_READ) ? SMB_POSIX_ACL_READ : 0);
3810 perms |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_WRITE) ? SMB_POSIX_ACL_WRITE : 0);
3811 perms |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_EXECUTE) ? SMB_POSIX_ACL_EXECUTE : 0);
3813 SCVAL(pdata,1,perms);
3816 case SMB_ACL_USER_OBJ:
3817 SCVAL(pdata,0,SMB_POSIX_ACL_USER_OBJ);
3818 own_grp = (unsigned int)pst->st_ex_uid;
3819 SIVAL(pdata,2,own_grp);
3824 uid_t *puid = (uid_t *)SMB_VFS_SYS_ACL_GET_QUALIFIER(conn, entry);
3826 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_QUALIFIER failed.\n"));
3829 own_grp = (unsigned int)*puid;
3830 SMB_VFS_SYS_ACL_FREE_QUALIFIER(conn, (void *)puid,tagtype);
3831 SCVAL(pdata,0,SMB_POSIX_ACL_USER);
3832 SIVAL(pdata,2,own_grp);
3836 case SMB_ACL_GROUP_OBJ:
3837 SCVAL(pdata,0,SMB_POSIX_ACL_GROUP_OBJ);
3838 own_grp = (unsigned int)pst->st_ex_gid;
3839 SIVAL(pdata,2,own_grp);
3844 gid_t *pgid= (gid_t *)SMB_VFS_SYS_ACL_GET_QUALIFIER(conn, entry);
3846 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_QUALIFIER failed.\n"));
3849 own_grp = (unsigned int)*pgid;
3850 SMB_VFS_SYS_ACL_FREE_QUALIFIER(conn, (void *)pgid,tagtype);
3851 SCVAL(pdata,0,SMB_POSIX_ACL_GROUP);
3852 SIVAL(pdata,2,own_grp);
3857 SCVAL(pdata,0,SMB_POSIX_ACL_MASK);
3858 SIVAL(pdata,2,0xFFFFFFFF);
3859 SIVAL(pdata,6,0xFFFFFFFF);
3862 SCVAL(pdata,0,SMB_POSIX_ACL_OTHER);
3863 SIVAL(pdata,2,0xFFFFFFFF);
3864 SIVAL(pdata,6,0xFFFFFFFF);
3867 DEBUG(0,("marshall_posix_acl: unknown tagtype.\n"));
3870 pdata += SMB_POSIX_ACL_ENTRY_SIZE;
3877 /****************************************************************************
3878 Store the FILE_UNIX_BASIC info.
3879 ****************************************************************************/
3881 static char *store_file_unix_basic(connection_struct *conn,
3884 const SMB_STRUCT_STAT *psbuf)
3886 DEBUG(10,("store_file_unix_basic: SMB_QUERY_FILE_UNIX_BASIC\n"));
3887 DEBUG(4,("store_file_unix_basic: st_mode=%o\n",(int)psbuf->st_ex_mode));
3889 SOFF_T(pdata,0,get_file_size_stat(psbuf)); /* File size 64 Bit */
3892 SOFF_T(pdata,0,SMB_VFS_GET_ALLOC_SIZE(conn,fsp,psbuf)); /* Number of bytes used on disk - 64 Bit */
3895 put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER, pdata, psbuf->st_ex_ctime); /* Change Time 64 Bit */
3896 put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER ,pdata+8, psbuf->st_ex_atime); /* Last access time 64 Bit */
3897 put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER, pdata+16, psbuf->st_ex_mtime); /* Last modification time 64 Bit */
3900 SIVAL(pdata,0,psbuf->st_ex_uid); /* user id for the owner */
3904 SIVAL(pdata,0,psbuf->st_ex_gid); /* group id of owner */
3908 SIVAL(pdata,0,unix_filetype(psbuf->st_ex_mode));
3911 SIVAL(pdata,0,unix_dev_major(psbuf->st_ex_rdev)); /* Major device number if type is device */
3915 SIVAL(pdata,0,unix_dev_minor(psbuf->st_ex_rdev)); /* Minor device number if type is device */
3919 SINO_T_VAL(pdata,0,(SMB_INO_T)psbuf->st_ex_ino); /* inode number */
3922 SIVAL(pdata,0, unix_perms_to_wire(psbuf->st_ex_mode)); /* Standard UNIX file permissions */
3926 SIVAL(pdata,0,psbuf->st_ex_nlink); /* number of hard links */
3933 /* Forward and reverse mappings from the UNIX_INFO2 file flags field and
3934 * the chflags(2) (or equivalent) flags.
3936 * XXX: this really should be behind the VFS interface. To do this, we would
3937 * need to alter SMB_STRUCT_STAT so that it included a flags and a mask field.
3938 * Each VFS module could then implement its own mapping as appropriate for the
3939 * platform. We would then pass the SMB flags into SMB_VFS_CHFLAGS.
3941 static const struct {unsigned stat_fflag; unsigned smb_fflag;}
3945 { UF_NODUMP, EXT_DO_NOT_BACKUP },
3949 { UF_IMMUTABLE, EXT_IMMUTABLE },
3953 { UF_APPEND, EXT_OPEN_APPEND_ONLY },
3957 { UF_HIDDEN, EXT_HIDDEN },
3960 /* Do not remove. We need to guarantee that this array has at least one
3961 * entry to build on HP-UX.
3967 static void map_info2_flags_from_sbuf(const SMB_STRUCT_STAT *psbuf,
3968 uint32 *smb_fflags, uint32 *smb_fmask)
3972 for (i = 0; i < ARRAY_SIZE(info2_flags_map); ++i) {
3973 *smb_fmask |= info2_flags_map[i].smb_fflag;
3974 if (psbuf->st_ex_flags & info2_flags_map[i].stat_fflag) {
3975 *smb_fflags |= info2_flags_map[i].smb_fflag;
3980 static bool map_info2_flags_to_sbuf(const SMB_STRUCT_STAT *psbuf,
3981 const uint32 smb_fflags,
3982 const uint32 smb_fmask,
3985 uint32 max_fmask = 0;
3988 *stat_fflags = psbuf->st_ex_flags;
3990 /* For each flags requested in smb_fmask, check the state of the
3991 * corresponding flag in smb_fflags and set or clear the matching
3995 for (i = 0; i < ARRAY_SIZE(info2_flags_map); ++i) {
3996 max_fmask |= info2_flags_map[i].smb_fflag;
3997 if (smb_fmask & info2_flags_map[i].smb_fflag) {
3998 if (smb_fflags & info2_flags_map[i].smb_fflag) {
3999 *stat_fflags |= info2_flags_map[i].stat_fflag;
4001 *stat_fflags &= ~info2_flags_map[i].stat_fflag;
4006 /* If smb_fmask is asking to set any bits that are not supported by
4007 * our flag mappings, we should fail.
4009 if ((smb_fmask & max_fmask) != smb_fmask) {
4017 /* Just like SMB_QUERY_FILE_UNIX_BASIC, but with the addition
4018 * of file flags and birth (create) time.
4020 static char *store_file_unix_basic_info2(connection_struct *conn,
4023 const SMB_STRUCT_STAT *psbuf)
4025 uint32 file_flags = 0;
4026 uint32 flags_mask = 0;
4028 pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
4030 /* Create (birth) time 64 bit */
4031 put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER,pdata, psbuf->st_ex_btime);
4034 map_info2_flags_from_sbuf(psbuf, &file_flags, &flags_mask);
4035 SIVAL(pdata, 0, file_flags); /* flags */
4036 SIVAL(pdata, 4, flags_mask); /* mask */
4042 static NTSTATUS marshall_stream_info(unsigned int num_streams,
4043 const struct stream_struct *streams,
4045 unsigned int max_data_bytes,
4046 unsigned int *data_size)
4049 unsigned int ofs = 0;
4051 for (i = 0; i < num_streams && ofs <= max_data_bytes; i++) {
4052 unsigned int next_offset;
4054 smb_ucs2_t *namebuf;
4056 if (!push_ucs2_talloc(talloc_tos(), &namebuf,
4057 streams[i].name, &namelen) ||
4060 return NT_STATUS_INVALID_PARAMETER;
4064 * name_buf is now null-terminated, we need to marshall as not
4070 SIVAL(data, ofs+4, namelen);
4071 SOFF_T(data, ofs+8, streams[i].size);
4072 SOFF_T(data, ofs+16, streams[i].alloc_size);
4073 memcpy(data+ofs+24, namebuf, namelen);
4074 TALLOC_FREE(namebuf);
4076 next_offset = ofs + 24 + namelen;
4078 if (i == num_streams-1) {
4079 SIVAL(data, ofs, 0);
4082 unsigned int align = ndr_align_size(next_offset, 8);
4084 memset(data+next_offset, 0, align);
4085 next_offset += align;
4087 SIVAL(data, ofs, next_offset - ofs);
4096 return NT_STATUS_OK;
4099 /****************************************************************************
4100 Reply to a TRANSACT2_QFILEINFO on a PIPE !
4101 ****************************************************************************/
4103 static void call_trans2qpipeinfo(connection_struct *conn,
4104 struct smb_request *req,
4105 unsigned int tran_call,
4106 char **pparams, int total_params,
4107 char **ppdata, int total_data,
4108 unsigned int max_data_bytes)
4110 char *params = *pparams;
4111 char *pdata = *ppdata;
4112 unsigned int data_size = 0;
4113 unsigned int param_size = 2;
4118 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4122 if (total_params < 4) {
4123 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4127 fsp = file_fsp(req, SVAL(params,0));
4128 if (!fsp_is_np(fsp)) {
4129 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
4133 info_level = SVAL(params,2);
4135 *pparams = (char *)SMB_REALLOC(*pparams,2);
4136 if (*pparams == NULL) {
4137 reply_nterror(req, NT_STATUS_NO_MEMORY);
4142 data_size = max_data_bytes + DIR_ENTRY_SAFETY_MARGIN;
4143 *ppdata = (char *)SMB_REALLOC(*ppdata, data_size);
4144 if (*ppdata == NULL ) {
4145 reply_nterror(req, NT_STATUS_NO_MEMORY);
4150 switch (info_level) {
4151 case SMB_FILE_STANDARD_INFORMATION:
4153 SOFF_T(pdata,0,4096LL);
4160 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
4164 send_trans2_replies(conn, req, params, param_size, *ppdata, data_size,
4170 NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
4171 TALLOC_CTX *mem_ctx,
4172 uint16_t info_level,
4174 struct smb_filename *smb_fname,
4175 bool delete_pending,
4176 struct timespec write_time_ts,
4177 struct ea_list *ea_list,
4178 int lock_data_count,
4181 unsigned int max_data_bytes,
4183 unsigned int *pdata_size)
4185 char *pdata = *ppdata;
4186 char *dstart, *dend;
4187 unsigned int data_size;
4188 struct timespec create_time_ts, mtime_ts, atime_ts, ctime_ts;
4189 time_t create_time, mtime, atime, c_time;
4190 SMB_STRUCT_STAT *psbuf = &smb_fname->st;
4197 uint64_t file_size = 0;
4199 uint64_t allocation_size = 0;
4200 uint64_t file_index = 0;
4201 uint32_t access_mask = 0;
4203 if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
4204 return NT_STATUS_INVALID_LEVEL;
4207 DEBUG(5,("smbd_do_qfilepathinfo: %s (fnum = %d) level=%d max_data=%u\n",
4208 smb_fname_str_dbg(smb_fname), fsp ? fsp->fnum : -1,
4209 info_level, max_data_bytes));
4211 mode = dos_mode(conn, smb_fname);
4212 nlink = psbuf->st_ex_nlink;
4214 if (nlink && (mode&aDIR)) {
4218 if ((nlink > 0) && delete_pending) {
4222 data_size = max_data_bytes + DIR_ENTRY_SAFETY_MARGIN;
4223 *ppdata = (char *)SMB_REALLOC(*ppdata, data_size);
4224 if (*ppdata == NULL) {
4225 return NT_STATUS_NO_MEMORY;
4229 dend = dstart + data_size - 1;
4231 if (!null_timespec(write_time_ts) && !INFO_LEVEL_IS_UNIX(info_level)) {
4232 update_stat_ex_mtime(psbuf, write_time_ts);
4235 create_time_ts = get_create_timespec(conn, fsp, smb_fname);
4236 mtime_ts = psbuf->st_ex_mtime;
4237 atime_ts = psbuf->st_ex_atime;
4238 ctime_ts = get_change_timespec(conn, fsp, smb_fname);
4240 if (lp_dos_filetime_resolution(SNUM(conn))) {
4241 dos_filetime_timespec(&create_time_ts);
4242 dos_filetime_timespec(&mtime_ts);
4243 dos_filetime_timespec(&atime_ts);
4244 dos_filetime_timespec(&ctime_ts);
4247 create_time = convert_timespec_to_time_t(create_time_ts);
4248 mtime = convert_timespec_to_time_t(mtime_ts);
4249 atime = convert_timespec_to_time_t(atime_ts);
4250 c_time = convert_timespec_to_time_t(ctime_ts);
4252 p = strrchr_m(smb_fname->base_name,'/');
4254 base_name = smb_fname->base_name;
4258 /* NT expects the name to be in an exact form of the *full*
4259 filename. See the trans2 torture test */
4260 if (ISDOT(base_name)) {
4261 dos_fname = talloc_strdup(mem_ctx, "\\");
4263 return NT_STATUS_NO_MEMORY;
4266 dos_fname = talloc_asprintf(mem_ctx,
4268 smb_fname->base_name);
4270 return NT_STATUS_NO_MEMORY;
4272 if (is_ntfs_stream_smb_fname(smb_fname)) {
4273 dos_fname = talloc_asprintf(dos_fname, "%s",
4274 smb_fname->stream_name);
4276 return NT_STATUS_NO_MEMORY;
4280 string_replace(dos_fname, '/', '\\');
4283 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp, psbuf);
4286 /* Do we have this path open ? */
4288 struct file_id fileid = vfs_file_id_from_sbuf(conn, psbuf);
4289 fsp1 = file_find_di_first(fileid);
4290 if (fsp1 && fsp1->initial_allocation_size) {
4291 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp1, psbuf);
4295 if (!(mode & aDIR)) {
4296 file_size = get_file_size_stat(psbuf);
4300 pos = fsp->fh->position_information;
4304 access_mask = fsp->access_mask;
4306 /* GENERIC_EXECUTE mapping from Windows */
4307 access_mask = 0x12019F;
4310 /* This should be an index number - looks like
4313 I think this causes us to fail the IFSKIT
4314 BasicFileInformationTest. -tpot */
4315 file_index = ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
4316 file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
4318 switch (info_level) {
4319 case SMB_INFO_STANDARD:
4320 DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_STANDARD\n"));
4322 srv_put_dos_date2(pdata,l1_fdateCreation,create_time);
4323 srv_put_dos_date2(pdata,l1_fdateLastAccess,atime);
4324 srv_put_dos_date2(pdata,l1_fdateLastWrite,mtime); /* write time */
4325 SIVAL(pdata,l1_cbFile,(uint32)file_size);
4326 SIVAL(pdata,l1_cbFileAlloc,(uint32)allocation_size);
4327 SSVAL(pdata,l1_attrFile,mode);
4330 case SMB_INFO_QUERY_EA_SIZE:
4332 unsigned int ea_size =
4333 estimate_ea_size(conn, fsp,
4334 smb_fname->base_name);
4335 DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_EA_SIZE\n"));
4337 srv_put_dos_date2(pdata,0,create_time);
4338 srv_put_dos_date2(pdata,4,atime);
4339 srv_put_dos_date2(pdata,8,mtime); /* write time */
4340 SIVAL(pdata,12,(uint32)file_size);
4341 SIVAL(pdata,16,(uint32)allocation_size);
4342 SSVAL(pdata,20,mode);
4343 SIVAL(pdata,22,ea_size);
4347 case SMB_INFO_IS_NAME_VALID:
4348 DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_IS_NAME_VALID\n"));
4350 /* os/2 needs this ? really ?*/
4351 return NT_STATUS_DOS(ERRDOS, ERRbadfunc);
4353 /* This is only reached for qpathinfo */
4357 case SMB_INFO_QUERY_EAS_FROM_LIST:
4359 size_t total_ea_len = 0;
4360 struct ea_list *ea_file_list = NULL;
4362 DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_EAS_FROM_LIST\n"));
4365 get_ea_list_from_file(mem_ctx, conn, fsp,
4366 smb_fname->base_name,
4368 ea_list = ea_list_union(ea_list, ea_file_list, &total_ea_len);
4370 if (!ea_list || (total_ea_len > data_size)) {
4372 SIVAL(pdata,0,4); /* EA List Length must be set to 4 if no EA's. */
4376 data_size = fill_ea_buffer(mem_ctx, pdata, data_size, conn, ea_list);
4380 case SMB_INFO_QUERY_ALL_EAS:
4382 /* We have data_size bytes to put EA's into. */
4383 size_t total_ea_len = 0;
4385 DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_ALL_EAS\n"));
4387 ea_list = get_ea_list_from_file(mem_ctx, conn, fsp,
4388 smb_fname->base_name,
4390 if (!ea_list || (total_ea_len > data_size)) {
4392 SIVAL(pdata,0,4); /* EA List Length must be set to 4 if no EA's. */
4396 data_size = fill_ea_buffer(mem_ctx, pdata, data_size, conn, ea_list);
4400 case 0xFF0F:/*SMB2_INFO_QUERY_ALL_EAS*/
4402 /* This is FileFullEaInformation - 0xF which maps to
4403 * 1015 (decimal) in smbd_do_setfilepathinfo. */
4405 /* We have data_size bytes to put EA's into. */
4406 size_t total_ea_len = 0;
4407 struct ea_list *ea_file_list = NULL;
4409 DEBUG(10,("smbd_do_qfilepathinfo: SMB2_INFO_QUERY_ALL_EAS\n"));
4411 /*TODO: add filtering and index handling */
4414 get_ea_list_from_file(mem_ctx, conn, fsp,
4415 smb_fname->base_name,
4417 if (!ea_file_list) {
4418 return NT_STATUS_NO_EAS_ON_FILE;
4421 status = fill_ea_chained_buffer(mem_ctx,
4425 conn, ea_file_list);
4426 if (!NT_STATUS_IS_OK(status)) {
4432 case SMB_FILE_BASIC_INFORMATION:
4433 case SMB_QUERY_FILE_BASIC_INFO:
4435 if (info_level == SMB_QUERY_FILE_BASIC_INFO) {
4436 DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_BASIC_INFO\n"));
4437 data_size = 36; /* w95 returns 40 bytes not 36 - why ?. */
4439 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_BASIC_INFORMATION\n"));
4443 put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
4444 put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
4445 put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
4446 put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
4447 SIVAL(pdata,32,mode);
4449 DEBUG(5,("SMB_QFBI - "));
4450 DEBUG(5,("create: %s ", ctime(&create_time)));
4451 DEBUG(5,("access: %s ", ctime(&atime)));
4452 DEBUG(5,("write: %s ", ctime(&mtime)));
4453 DEBUG(5,("change: %s ", ctime(&c_time)));
4454 DEBUG(5,("mode: %x\n", mode));
4457 case SMB_FILE_STANDARD_INFORMATION:
4458 case SMB_QUERY_FILE_STANDARD_INFO:
4460 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_STANDARD_INFORMATION\n"));
4462 SOFF_T(pdata,0,allocation_size);
4463 SOFF_T(pdata,8,file_size);
4464 SIVAL(pdata,16,nlink);
4465 SCVAL(pdata,20,delete_pending?1:0);
4466 SCVAL(pdata,21,(mode&aDIR)?1:0);
4467 SSVAL(pdata,22,0); /* Padding. */
4470 case SMB_FILE_EA_INFORMATION:
4471 case SMB_QUERY_FILE_EA_INFO:
4473 unsigned int ea_size =
4474 estimate_ea_size(conn, fsp, smb_fname->base_name);
4475 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_EA_INFORMATION\n"));
4477 SIVAL(pdata,0,ea_size);
4481 /* Get the 8.3 name - used if NT SMB was negotiated. */
4482 case SMB_QUERY_FILE_ALT_NAME_INFO:
4483 case SMB_FILE_ALTERNATE_NAME_INFORMATION:
4486 char mangled_name[13];
4487 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALTERNATE_NAME_INFORMATION\n"));
4488 if (!name_to_8_3(base_name,mangled_name,
4489 True,conn->params)) {
4490 return NT_STATUS_NO_MEMORY;
4492 len = srvstr_push(dstart, flags2,
4493 pdata+4, mangled_name,
4494 PTR_DIFF(dend, pdata+4),
4496 data_size = 4 + len;
4501 case SMB_QUERY_FILE_NAME_INFO:
4505 this must be *exactly* right for ACLs on mapped drives to work
4507 len = srvstr_push(dstart, flags2,
4509 PTR_DIFF(dend, pdata+4),
4511 DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_NAME_INFO\n"));
4512 data_size = 4 + len;
4517 case SMB_FILE_ALLOCATION_INFORMATION:
4518 case SMB_QUERY_FILE_ALLOCATION_INFO:
4519 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALLOCATION_INFORMATION\n"));
4521 SOFF_T(pdata,0,allocation_size);
4524 case SMB_FILE_END_OF_FILE_INFORMATION:
4525 case SMB_QUERY_FILE_END_OF_FILEINFO:
4526 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_END_OF_FILE_INFORMATION\n"));
4528 SOFF_T(pdata,0,file_size);
4531 case SMB_QUERY_FILE_ALL_INFO:
4532 case SMB_FILE_ALL_INFORMATION:
4535 unsigned int ea_size =
4536 estimate_ea_size(conn, fsp, smb_fname->base_name);
4537 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALL_INFORMATION\n"));
4538 put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
4539 put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
4540 put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
4541 put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
4542 SIVAL(pdata,32,mode);
4543 SIVAL(pdata,36,0); /* padding. */
4545 SOFF_T(pdata,0,allocation_size);
4546 SOFF_T(pdata,8,file_size);
4547 SIVAL(pdata,16,nlink);
4548 SCVAL(pdata,20,delete_pending);
4549 SCVAL(pdata,21,(mode&aDIR)?1:0);
4552 SIVAL(pdata,0,ea_size);
4553 pdata += 4; /* EA info */
4554 len = srvstr_push(dstart, flags2,
4556 PTR_DIFF(dend, pdata+4),
4560 data_size = PTR_DIFF(pdata,(*ppdata));
4564 case 0xFF12:/*SMB2_FILE_ALL_INFORMATION*/
4567 unsigned int ea_size =
4568 estimate_ea_size(conn, fsp, smb_fname->base_name);
4569 DEBUG(10,("smbd_do_qfilepathinfo: SMB2_FILE_ALL_INFORMATION\n"));
4570 put_long_date_timespec(conn->ts_res,pdata+0x00,create_time_ts);
4571 put_long_date_timespec(conn->ts_res,pdata+0x08,atime_ts);
4572 put_long_date_timespec(conn->ts_res,pdata+0x10,mtime_ts); /* write time */
4573 put_long_date_timespec(conn->ts_res,pdata+0x18,ctime_ts); /* change time */
4574 SIVAL(pdata, 0x20, mode);
4575 SIVAL(pdata, 0x24, 0); /* padding. */
4576 SBVAL(pdata, 0x28, allocation_size);
4577 SBVAL(pdata, 0x30, file_size);
4578 SIVAL(pdata, 0x38, nlink);
4579 SCVAL(pdata, 0x3C, delete_pending);
4580 SCVAL(pdata, 0x3D, (mode&aDIR)?1:0);
4581 SSVAL(pdata, 0x3E, 0); /* padding */
4582 SBVAL(pdata, 0x40, file_index);
4583 SIVAL(pdata, 0x48, ea_size);
4584 SIVAL(pdata, 0x4C, access_mask);
4585 SBVAL(pdata, 0x50, pos);
4586 SIVAL(pdata, 0x58, mode); /*TODO: mode != mode fix this!!! */
4587 SIVAL(pdata, 0x5C, 0); /* No alignment needed. */
4591 len = srvstr_push(dstart, flags2,
4593 PTR_DIFF(dend, pdata+4),
4597 data_size = PTR_DIFF(pdata,(*ppdata));
4600 case SMB_FILE_INTERNAL_INFORMATION:
4602 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_INTERNAL_INFORMATION\n"));
4603 SBVAL(pdata, 0, file_index);
4607 case SMB_FILE_ACCESS_INFORMATION:
4608 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ACCESS_INFORMATION\n"));
4609 SIVAL(pdata, 0, access_mask);
4613 case SMB_FILE_NAME_INFORMATION:
4614 /* Pathname with leading '\'. */
4617 byte_len = dos_PutUniCode(pdata+4,dos_fname,(size_t)max_data_bytes,False);
4618 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_NAME_INFORMATION\n"));
4619 SIVAL(pdata,0,byte_len);
4620 data_size = 4 + byte_len;
4624 case SMB_FILE_DISPOSITION_INFORMATION:
4625 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_DISPOSITION_INFORMATION\n"));
4627 SCVAL(pdata,0,delete_pending);
4630 case SMB_FILE_POSITION_INFORMATION:
4631 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_POSITION_INFORMATION\n"));
4633 SOFF_T(pdata,0,pos);
4636 case SMB_FILE_MODE_INFORMATION:
4637 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_MODE_INFORMATION\n"));
4638 SIVAL(pdata,0,mode);
4642 case SMB_FILE_ALIGNMENT_INFORMATION:
4643 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALIGNMENT_INFORMATION\n"));
4644 SIVAL(pdata,0,0); /* No alignment needed. */
4649 * NT4 server just returns "invalid query" to this - if we try
4650 * to answer it then NTws gets a BSOD! (tridge). W2K seems to
4653 /* The first statement above is false - verified using Thursby
4654 * client against NT4 -- gcolley.
4656 case SMB_QUERY_FILE_STREAM_INFO:
4657 case SMB_FILE_STREAM_INFORMATION: {
4658 unsigned int num_streams;
4659 struct stream_struct *streams;
4661 DEBUG(10,("smbd_do_qfilepathinfo: "
4662 "SMB_FILE_STREAM_INFORMATION\n"));
4664 if (is_ntfs_stream_smb_fname(smb_fname)) {
4665 return NT_STATUS_INVALID_PARAMETER;
4668 status = SMB_VFS_STREAMINFO(
4669 conn, fsp, smb_fname->base_name, talloc_tos(),
4670 &num_streams, &streams);
4672 if (!NT_STATUS_IS_OK(status)) {
4673 DEBUG(10, ("could not get stream info: %s\n",
4674 nt_errstr(status)));
4678 status = marshall_stream_info(num_streams, streams,
4679 pdata, max_data_bytes,
4682 if (!NT_STATUS_IS_OK(status)) {
4683 DEBUG(10, ("marshall_stream_info failed: %s\n",
4684 nt_errstr(status)));
4688 TALLOC_FREE(streams);
4692 case SMB_QUERY_COMPRESSION_INFO:
4693 case SMB_FILE_COMPRESSION_INFORMATION:
4694 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_COMPRESSION_INFORMATION\n"));
4695 SOFF_T(pdata,0,file_size);
4696 SIVAL(pdata,8,0); /* ??? */
4697 SIVAL(pdata,12,0); /* ??? */
4701 case SMB_FILE_NETWORK_OPEN_INFORMATION:
4702 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_NETWORK_OPEN_INFORMATION\n"));
4703 put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
4704 put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
4705 put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
4706 put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
4707 SOFF_T(pdata,32,allocation_size);
4708 SOFF_T(pdata,40,file_size);
4709 SIVAL(pdata,48,mode);
4710 SIVAL(pdata,52,0); /* ??? */
4714 case SMB_FILE_ATTRIBUTE_TAG_INFORMATION:
4715 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ATTRIBUTE_TAG_INFORMATION\n"));
4716 SIVAL(pdata,0,mode);
4722 * CIFS UNIX Extensions.
4725 case SMB_QUERY_FILE_UNIX_BASIC:
4727 pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
4728 data_size = PTR_DIFF(pdata,(*ppdata));
4732 DEBUG(4,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_BASIC "));
4734 for (i=0; i<100; i++)
4735 DEBUG(4,("%d=%x, ",i, (*ppdata)[i]));
4741 case SMB_QUERY_FILE_UNIX_INFO2:
4743 pdata = store_file_unix_basic_info2(conn, pdata, fsp, psbuf);
4744 data_size = PTR_DIFF(pdata,(*ppdata));
4748 DEBUG(4,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_INFO2 "));
4750 for (i=0; i<100; i++)
4751 DEBUG(4,("%d=%x, ",i, (*ppdata)[i]));
4757 case SMB_QUERY_FILE_UNIX_LINK:
4760 char *buffer = TALLOC_ARRAY(mem_ctx, char, PATH_MAX+1);
4763 return NT_STATUS_NO_MEMORY;
4766 DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_LINK\n"));
4768 if(!S_ISLNK(psbuf->st_ex_mode)) {
4769 return NT_STATUS_DOS(ERRSRV, ERRbadlink);
4772 return NT_STATUS_DOS(ERRDOS, ERRbadlink);
4774 len = SMB_VFS_READLINK(conn,
4775 smb_fname->base_name,
4778 return map_nt_error_from_unix(errno);
4781 len = srvstr_push(dstart, flags2,
4783 PTR_DIFF(dend, pdata),
4786 data_size = PTR_DIFF(pdata,(*ppdata));
4791 #if defined(HAVE_POSIX_ACLS)
4792 case SMB_QUERY_POSIX_ACL:
4794 SMB_ACL_T file_acl = NULL;
4795 SMB_ACL_T def_acl = NULL;
4796 uint16 num_file_acls = 0;
4797 uint16 num_def_acls = 0;
4799 if (fsp && !fsp->is_directory && (fsp->fh->fd != -1)) {
4800 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
4803 SMB_VFS_SYS_ACL_GET_FILE(conn,
4804 smb_fname->base_name,
4805 SMB_ACL_TYPE_ACCESS);
4808 if (file_acl == NULL && no_acl_syscall_error(errno)) {
4809 DEBUG(5,("smbd_do_qfilepathinfo: ACLs "
4810 "not implemented on "
4811 "filesystem containing %s\n",
4812 smb_fname->base_name));
4813 return NT_STATUS_NOT_IMPLEMENTED;
4816 if (S_ISDIR(psbuf->st_ex_mode)) {
4817 if (fsp && fsp->is_directory) {
4819 SMB_VFS_SYS_ACL_GET_FILE(
4821 fsp->fsp_name->base_name,
4822 SMB_ACL_TYPE_DEFAULT);
4825 SMB_VFS_SYS_ACL_GET_FILE(
4827 smb_fname->base_name,
4828 SMB_ACL_TYPE_DEFAULT);
4830 def_acl = free_empty_sys_acl(conn, def_acl);
4833 num_file_acls = count_acl_entries(conn, file_acl);
4834 num_def_acls = count_acl_entries(conn, def_acl);
4836 if ( data_size < (num_file_acls + num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE + SMB_POSIX_ACL_HEADER_SIZE) {
4837 DEBUG(5,("smbd_do_qfilepathinfo: data_size too small (%u) need %u\n",
4839 (unsigned int)((num_file_acls + num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE +
4840 SMB_POSIX_ACL_HEADER_SIZE) ));
4842 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4845 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4847 return NT_STATUS_BUFFER_TOO_SMALL;
4850 SSVAL(pdata,0,SMB_POSIX_ACL_VERSION);
4851 SSVAL(pdata,2,num_file_acls);
4852 SSVAL(pdata,4,num_def_acls);
4853 if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE, psbuf, file_acl)) {
4855 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4858 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4860 return NT_STATUS_INTERNAL_ERROR;
4862 if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE + (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE), psbuf, def_acl)) {
4864 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4867 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4869 return NT_STATUS_INTERNAL_ERROR;
4873 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4876 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4878 data_size = (num_file_acls + num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE + SMB_POSIX_ACL_HEADER_SIZE;
4884 case SMB_QUERY_POSIX_LOCK:
4889 enum brl_type lock_type;
4891 /* We need an open file with a real fd for this. */
4892 if (!fsp || fsp->is_directory || fsp->fh->fd == -1) {
4893 return NT_STATUS_INVALID_LEVEL;
4896 if (lock_data_count != POSIX_LOCK_DATA_SIZE) {
4897 return NT_STATUS_INVALID_PARAMETER;
4900 switch (SVAL(pdata, POSIX_LOCK_TYPE_OFFSET)) {
4901 case POSIX_LOCK_TYPE_READ:
4902 lock_type = READ_LOCK;
4904 case POSIX_LOCK_TYPE_WRITE:
4905 lock_type = WRITE_LOCK;
4907 case POSIX_LOCK_TYPE_UNLOCK:
4909 /* There's no point in asking for an unlock... */
4910 return NT_STATUS_INVALID_PARAMETER;
4913 smblctx = (uint64_t)IVAL(pdata, POSIX_LOCK_PID_OFFSET);
4914 #if defined(HAVE_LONGLONG)
4915 offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
4916 ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
4917 count = (((uint64_t) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
4918 ((uint64_t) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
4919 #else /* HAVE_LONGLONG */
4920 offset = (uint64_t)IVAL(pdata,POSIX_LOCK_START_OFFSET);
4921 count = (uint64_t)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
4922 #endif /* HAVE_LONGLONG */
4924 status = query_lock(fsp,
4931 if (ERROR_WAS_LOCK_DENIED(status)) {
4932 /* Here we need to report who has it locked... */
4933 data_size = POSIX_LOCK_DATA_SIZE;
4935 SSVAL(pdata, POSIX_LOCK_TYPE_OFFSET, lock_type);
4936 SSVAL(pdata, POSIX_LOCK_FLAGS_OFFSET, 0);
4937 SIVAL(pdata, POSIX_LOCK_PID_OFFSET, (uint32_t)smblctx);
4938 #if defined(HAVE_LONGLONG)
4939 SIVAL(pdata, POSIX_LOCK_START_OFFSET, (uint32)(offset & 0xFFFFFFFF));
4940 SIVAL(pdata, POSIX_LOCK_START_OFFSET + 4, (uint32)((offset >> 32) & 0xFFFFFFFF));
4941 SIVAL(pdata, POSIX_LOCK_LEN_OFFSET, (uint32)(count & 0xFFFFFFFF));
4942 SIVAL(pdata, POSIX_LOCK_LEN_OFFSET + 4, (uint32)((count >> 32) & 0xFFFFFFFF));
4943 #else /* HAVE_LONGLONG */
4944 SIVAL(pdata, POSIX_LOCK_START_OFFSET, offset);
4945 SIVAL(pdata, POSIX_LOCK_LEN_OFFSET, count);
4946 #endif /* HAVE_LONGLONG */
4948 } else if (NT_STATUS_IS_OK(status)) {
4949 /* For success we just return a copy of what we sent
4950 with the lock type set to POSIX_LOCK_TYPE_UNLOCK. */
4951 data_size = POSIX_LOCK_DATA_SIZE;
4952 memcpy(pdata, lock_data, POSIX_LOCK_DATA_SIZE);
4953 SSVAL(pdata, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
4961 return NT_STATUS_INVALID_LEVEL;
4964 *pdata_size = data_size;
4965 return NT_STATUS_OK;
4968 /****************************************************************************
4969 Reply to a TRANS2_QFILEPATHINFO or TRANSACT2_QFILEINFO (query file info by
4970 file name or file id).
4971 ****************************************************************************/
4973 static void call_trans2qfilepathinfo(connection_struct *conn,
4974 struct smb_request *req,
4975 unsigned int tran_call,
4976 char **pparams, int total_params,
4977 char **ppdata, int total_data,
4978 unsigned int max_data_bytes)
4980 char *params = *pparams;
4981 char *pdata = *ppdata;
4983 unsigned int data_size = 0;
4984 unsigned int param_size = 2;
4985 struct smb_filename *smb_fname = NULL;
4986 bool delete_pending = False;
4987 struct timespec write_time_ts;
4988 files_struct *fsp = NULL;
4989 struct file_id fileid;
4990 struct ea_list *ea_list = NULL;
4991 int lock_data_count = 0;
4992 char *lock_data = NULL;
4993 NTSTATUS status = NT_STATUS_OK;
4996 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5000 ZERO_STRUCT(write_time_ts);
5002 if (tran_call == TRANSACT2_QFILEINFO) {
5003 if (total_params < 4) {
5004 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5009 call_trans2qpipeinfo(conn, req, tran_call,
5010 pparams, total_params,
5016 fsp = file_fsp(req, SVAL(params,0));
5017 info_level = SVAL(params,2);
5019 DEBUG(3,("call_trans2qfilepathinfo: TRANSACT2_QFILEINFO: level = %d\n", info_level));
5021 if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
5022 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
5026 /* Initial check for valid fsp ptr. */
5027 if (!check_fsp_open(conn, req, fsp)) {
5031 status = copy_smb_filename(talloc_tos(), fsp->fsp_name,
5033 if (!NT_STATUS_IS_OK(status)) {
5034 reply_nterror(req, status);
5038 if(fsp->fake_file_handle) {
5040 * This is actually for the QUOTA_FAKE_FILE --metze
5043 /* We know this name is ok, it's already passed the checks. */
5045 } else if(fsp->is_directory || fsp->fh->fd == -1) {
5047 * This is actually a QFILEINFO on a directory
5048 * handle (returned from an NT SMB). NT5.0 seems
5049 * to do this call. JRA.
5052 if (INFO_LEVEL_IS_UNIX(info_level)) {
5053 /* Always do lstat for UNIX calls. */
5054 if (SMB_VFS_LSTAT(conn, smb_fname)) {
5055 DEBUG(3,("call_trans2qfilepathinfo: "
5056 "SMB_VFS_LSTAT of %s failed "
5058 smb_fname_str_dbg(smb_fname),
5061 map_nt_error_from_unix(errno));
5064 } else if (SMB_VFS_STAT(conn, smb_fname)) {
5065 DEBUG(3,("call_trans2qfilepathinfo: "
5066 "SMB_VFS_STAT of %s failed (%s)\n",
5067 smb_fname_str_dbg(smb_fname),
5070 map_nt_error_from_unix(errno));
5074 fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
5075 get_file_infos(fileid, &delete_pending, &write_time_ts);
5078 * Original code - this is an open file.
5080 if (!check_fsp(conn, req, fsp)) {
5084 if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
5085 DEBUG(3, ("fstat of fnum %d failed (%s)\n",
5086 fsp->fnum, strerror(errno)));
5088 map_nt_error_from_unix(errno));
5091 fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
5092 get_file_infos(fileid, &delete_pending, &write_time_ts);
5099 if (total_params < 7) {
5100 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5104 info_level = SVAL(params,0);
5106 DEBUG(3,("call_trans2qfilepathinfo: TRANSACT2_QPATHINFO: level = %d\n", info_level));
5108 if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
5109 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
5113 srvstr_get_path(req, params, req->flags2, &fname, ¶ms[6],
5115 STR_TERMINATE, &status);
5116 if (!NT_STATUS_IS_OK(status)) {
5117 reply_nterror(req, status);
5121 status = filename_convert(req,
5123 req->flags2 & FLAGS2_DFS_PATHNAMES,
5128 if (!NT_STATUS_IS_OK(status)) {
5129 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5130 reply_botherror(req,
5131 NT_STATUS_PATH_NOT_COVERED,
5132 ERRSRV, ERRbadpath);
5135 reply_nterror(req, status);
5139 /* If this is a stream, check if there is a delete_pending. */
5140 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
5141 && is_ntfs_stream_smb_fname(smb_fname)) {
5142 struct smb_filename *smb_fname_base = NULL;
5144 /* Create an smb_filename with stream_name == NULL. */
5146 create_synthetic_smb_fname(talloc_tos(),
5147 smb_fname->base_name,
5150 if (!NT_STATUS_IS_OK(status)) {
5151 reply_nterror(req, status);
5155 if (INFO_LEVEL_IS_UNIX(info_level)) {
5156 /* Always do lstat for UNIX calls. */
5157 if (SMB_VFS_LSTAT(conn, smb_fname_base) != 0) {
5158 DEBUG(3,("call_trans2qfilepathinfo: "
5159 "SMB_VFS_LSTAT of %s failed "
5161 smb_fname_str_dbg(smb_fname_base),
5163 TALLOC_FREE(smb_fname_base);
5165 map_nt_error_from_unix(errno));
5169 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
5170 DEBUG(3,("call_trans2qfilepathinfo: "
5171 "fileinfo of %s failed "
5173 smb_fname_str_dbg(smb_fname_base),
5175 TALLOC_FREE(smb_fname_base);
5177 map_nt_error_from_unix(errno));
5182 fileid = vfs_file_id_from_sbuf(conn,
5183 &smb_fname_base->st);
5184 TALLOC_FREE(smb_fname_base);
5185 get_file_infos(fileid, &delete_pending, NULL);
5186 if (delete_pending) {
5187 reply_nterror(req, NT_STATUS_DELETE_PENDING);
5192 if (INFO_LEVEL_IS_UNIX(info_level)) {
5193 /* Always do lstat for UNIX calls. */
5194 if (SMB_VFS_LSTAT(conn, smb_fname)) {
5195 DEBUG(3,("call_trans2qfilepathinfo: "
5196 "SMB_VFS_LSTAT of %s failed (%s)\n",
5197 smb_fname_str_dbg(smb_fname),
5200 map_nt_error_from_unix(errno));
5205 if (SMB_VFS_STAT(conn, smb_fname) != 0) {
5206 DEBUG(3,("call_trans2qfilepathinfo: "
5207 "SMB_VFS_STAT of %s failed (%s)\n",
5208 smb_fname_str_dbg(smb_fname),
5211 map_nt_error_from_unix(errno));
5216 fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
5217 get_file_infos(fileid, &delete_pending, &write_time_ts);
5218 if (delete_pending) {
5219 reply_nterror(req, NT_STATUS_DELETE_PENDING);
5224 DEBUG(3,("call_trans2qfilepathinfo %s (fnum = %d) level=%d call=%d "
5225 "total_data=%d\n", smb_fname_str_dbg(smb_fname),
5226 fsp ? fsp->fnum : -1, info_level,tran_call,total_data));
5228 /* Pull out any data sent here before we realloc. */
5229 switch (info_level) {
5230 case SMB_INFO_QUERY_EAS_FROM_LIST:
5232 /* Pull any EA list from the data portion. */
5235 if (total_data < 4) {
5237 req, NT_STATUS_INVALID_PARAMETER);
5240 ea_size = IVAL(pdata,0);
5242 if (total_data > 0 && ea_size != total_data) {
5243 DEBUG(4,("call_trans2qfilepathinfo: Rejecting EA request with incorrect \
5244 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
5246 req, NT_STATUS_INVALID_PARAMETER);
5250 if (!lp_ea_support(SNUM(conn))) {
5251 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
5255 /* Pull out the list of names. */
5256 ea_list = read_ea_name_list(req, pdata + 4, ea_size - 4);
5259 req, NT_STATUS_INVALID_PARAMETER);
5265 case SMB_QUERY_POSIX_LOCK:
5267 if (fsp == NULL || fsp->fh->fd == -1) {
5268 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
5272 if (total_data != POSIX_LOCK_DATA_SIZE) {
5274 req, NT_STATUS_INVALID_PARAMETER);
5278 /* Copy the lock range data. */
5279 lock_data = (char *)TALLOC_MEMDUP(
5280 req, pdata, total_data);
5282 reply_nterror(req, NT_STATUS_NO_MEMORY);
5285 lock_data_count = total_data;
5291 *pparams = (char *)SMB_REALLOC(*pparams,2);
5292 if (*pparams == NULL) {
5293 reply_nterror(req, NT_STATUS_NO_MEMORY);
5300 * draft-leach-cifs-v1-spec-02.txt
5301 * 4.2.14 TRANS2_QUERY_PATH_INFORMATION: Get File Attributes given Path
5304 * The requested information is placed in the Data portion of the
5305 * transaction response. For the information levels greater than 0x100,
5306 * the transaction response has 1 parameter word which should be
5307 * ignored by the client.
5309 * However Windows only follows this rule for the IS_NAME_VALID call.
5311 switch (info_level) {
5312 case SMB_INFO_IS_NAME_VALID:
5317 if ((info_level & 0xFF00) == 0xFF00) {
5319 * We use levels that start with 0xFF00
5320 * internally to represent SMB2 specific levels
5322 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
5326 status = smbd_do_qfilepathinfo(conn, req, info_level,
5328 delete_pending, write_time_ts,
5330 lock_data_count, lock_data,
5331 req->flags2, max_data_bytes,
5332 ppdata, &data_size);
5333 if (!NT_STATUS_IS_OK(status)) {
5334 reply_nterror(req, status);
5338 send_trans2_replies(conn, req, params, param_size, *ppdata, data_size,
5344 /****************************************************************************
5345 Set a hard link (called by UNIX extensions and by NT rename with HARD link
5347 ****************************************************************************/
5349 NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
5350 connection_struct *conn,
5351 const struct smb_filename *smb_fname_old,
5352 const struct smb_filename *smb_fname_new)
5354 NTSTATUS status = NT_STATUS_OK;
5356 /* source must already exist. */
5357 if (!VALID_STAT(smb_fname_old->st)) {
5358 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5361 /* Disallow if newname already exists. */
5362 if (VALID_STAT(smb_fname_new->st)) {
5363 return NT_STATUS_OBJECT_NAME_COLLISION;
5366 /* No links from a directory. */
5367 if (S_ISDIR(smb_fname_old->st.st_ex_mode)) {
5368 return NT_STATUS_FILE_IS_A_DIRECTORY;
5371 /* Setting a hardlink to/from a stream isn't currently supported. */
5372 if (is_ntfs_stream_smb_fname(smb_fname_old) ||
5373 is_ntfs_stream_smb_fname(smb_fname_new)) {
5374 return NT_STATUS_INVALID_PARAMETER;
5377 DEBUG(10,("hardlink_internals: doing hard link %s -> %s\n",
5378 smb_fname_old->base_name, smb_fname_new->base_name));
5380 if (SMB_VFS_LINK(conn, smb_fname_old->base_name,
5381 smb_fname_new->base_name) != 0) {
5382 status = map_nt_error_from_unix(errno);
5383 DEBUG(3,("hardlink_internals: Error %s hard link %s -> %s\n",
5384 nt_errstr(status), smb_fname_old->base_name,
5385 smb_fname_new->base_name));
5390 /****************************************************************************
5391 Deal with setting the time from any of the setfilepathinfo functions.
5392 ****************************************************************************/
5394 NTSTATUS smb_set_file_time(connection_struct *conn,
5396 const struct smb_filename *smb_fname,
5397 struct smb_file_time *ft,
5398 bool setting_write_time)
5400 struct smb_filename smb_fname_base;
5402 FILE_NOTIFY_CHANGE_LAST_ACCESS
5403 |FILE_NOTIFY_CHANGE_LAST_WRITE
5404 |FILE_NOTIFY_CHANGE_CREATION;
5406 if (!VALID_STAT(smb_fname->st)) {
5407 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5410 /* get some defaults (no modifications) if any info is zero or -1. */
5411 if (null_timespec(ft->create_time)) {
5412 action &= ~FILE_NOTIFY_CHANGE_CREATION;
5415 if (null_timespec(ft->atime)) {
5416 action &= ~FILE_NOTIFY_CHANGE_LAST_ACCESS;
5419 if (null_timespec(ft->mtime)) {
5420 action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
5423 if (!setting_write_time) {
5424 /* ft->mtime comes from change time, not write time. */
5425 action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
5428 /* Ensure the resolution is the correct for
5429 * what we can store on this filesystem. */
5431 round_timespec(conn->ts_res, &ft->create_time);
5432 round_timespec(conn->ts_res, &ft->ctime);
5433 round_timespec(conn->ts_res, &ft->atime);
5434 round_timespec(conn->ts_res, &ft->mtime);
5436 DEBUG(5,("smb_set_filetime: actime: %s\n ",
5437 time_to_asc(convert_timespec_to_time_t(ft->atime))));
5438 DEBUG(5,("smb_set_filetime: modtime: %s\n ",
5439 time_to_asc(convert_timespec_to_time_t(ft->mtime))));
5440 DEBUG(5,("smb_set_filetime: ctime: %s\n ",
5441 time_to_asc(convert_timespec_to_time_t(ft->ctime))));
5442 DEBUG(5,("smb_set_file_time: createtime: %s\n ",
5443 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5445 if (setting_write_time) {
5447 * This was a Windows setfileinfo on an open file.
5448 * NT does this a lot. We also need to
5449 * set the time here, as it can be read by
5450 * FindFirst/FindNext and with the patch for bug #2045
5451 * in smbd/fileio.c it ensures that this timestamp is
5452 * kept sticky even after a write. We save the request
5453 * away and will set it on file close and after a write. JRA.
5456 DEBUG(10,("smb_set_file_time: setting pending modtime to %s\n",
5457 time_to_asc(convert_timespec_to_time_t(ft->mtime))));
5460 if (fsp->base_fsp) {
5461 set_sticky_write_time_fsp(fsp->base_fsp,
5464 set_sticky_write_time_fsp(fsp, ft->mtime);
5467 set_sticky_write_time_path(
5468 vfs_file_id_from_sbuf(conn, &smb_fname->st),
5473 DEBUG(10,("smb_set_file_time: setting utimes to modified values.\n"));
5475 /* Always call ntimes on the base, even if a stream was passed in. */
5476 smb_fname_base = *smb_fname;
5477 smb_fname_base.stream_name = NULL;
5479 if(file_ntimes(conn, &smb_fname_base, ft)!=0) {
5480 return map_nt_error_from_unix(errno);
5483 notify_fname(conn, NOTIFY_ACTION_MODIFIED, action,
5484 smb_fname->base_name);
5485 return NT_STATUS_OK;
5488 /****************************************************************************
5489 Deal with setting the dosmode from any of the setfilepathinfo functions.
5490 ****************************************************************************/
5492 static NTSTATUS smb_set_file_dosmode(connection_struct *conn,
5493 const struct smb_filename *smb_fname,
5496 struct smb_filename *smb_fname_base = NULL;
5499 if (!VALID_STAT(smb_fname->st)) {
5500 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5503 /* Always operate on the base_name, even if a stream was passed in. */
5504 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
5505 NULL, &smb_fname->st,
5507 if (!NT_STATUS_IS_OK(status)) {
5512 if (S_ISDIR(smb_fname_base->st.st_ex_mode)) {
5519 DEBUG(6,("smb_set_file_dosmode: dosmode: 0x%x\n", (unsigned int)dosmode));
5521 /* check the mode isn't different, before changing it */
5522 if ((dosmode != 0) && (dosmode != dos_mode(conn, smb_fname_base))) {
5523 DEBUG(10,("smb_set_file_dosmode: file %s : setting dos mode "
5524 "0x%x\n", smb_fname_str_dbg(smb_fname_base),
5525 (unsigned int)dosmode));
5527 if(file_set_dosmode(conn, smb_fname_base, dosmode, NULL,
5529 DEBUG(2,("smb_set_file_dosmode: file_set_dosmode of "
5531 smb_fname_str_dbg(smb_fname_base),
5533 status = map_nt_error_from_unix(errno);
5537 status = NT_STATUS_OK;
5539 TALLOC_FREE(smb_fname_base);
5543 /****************************************************************************
5544 Deal with setting the size from any of the setfilepathinfo functions.
5545 ****************************************************************************/
5547 static NTSTATUS smb_set_file_size(connection_struct *conn,
5548 struct smb_request *req,
5550 const struct smb_filename *smb_fname,
5551 const SMB_STRUCT_STAT *psbuf,
5553 bool fail_after_createfile)
5555 NTSTATUS status = NT_STATUS_OK;
5556 struct smb_filename *smb_fname_tmp = NULL;
5557 files_struct *new_fsp = NULL;
5559 if (!VALID_STAT(*psbuf)) {
5560 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5563 DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));
5565 if (size == get_file_size_stat(psbuf)) {
5566 return NT_STATUS_OK;
5569 DEBUG(10,("smb_set_file_size: file %s : setting new size to %.0f\n",
5570 smb_fname_str_dbg(smb_fname), (double)size));
5572 if (fsp && fsp->fh->fd != -1) {
5573 /* Handle based call. */
5574 if (vfs_set_filelen(fsp, size) == -1) {
5575 return map_nt_error_from_unix(errno);
5577 trigger_write_time_update_immediate(fsp);
5578 return NT_STATUS_OK;
5581 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
5582 if (!NT_STATUS_IS_OK(status)) {
5586 smb_fname_tmp->st = *psbuf;
5588 status = SMB_VFS_CREATE_FILE(
5591 0, /* root_dir_fid */
5592 smb_fname_tmp, /* fname */
5593 FILE_WRITE_DATA, /* access_mask */
5594 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
5596 FILE_OPEN, /* create_disposition*/
5597 0, /* create_options */
5598 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
5599 FORCE_OPLOCK_BREAK_TO_NONE, /* oplock_request */
5600 0, /* allocation_size */
5601 0, /* private_flags */
5604 &new_fsp, /* result */
5607 TALLOC_FREE(smb_fname_tmp);
5609 if (!NT_STATUS_IS_OK(status)) {
5610 /* NB. We check for open_was_deferred in the caller. */
5614 /* See RAW-SFILEINFO-END-OF-FILE */
5615 if (fail_after_createfile) {
5616 close_file(req, new_fsp,NORMAL_CLOSE);
5617 return NT_STATUS_INVALID_LEVEL;
5620 if (vfs_set_filelen(new_fsp, size) == -1) {
5621 status = map_nt_error_from_unix(errno);
5622 close_file(req, new_fsp,NORMAL_CLOSE);
5626 trigger_write_time_update_immediate(new_fsp);
5627 close_file(req, new_fsp,NORMAL_CLOSE);
5628 return NT_STATUS_OK;
5631 /****************************************************************************
5632 Deal with SMB_INFO_SET_EA.
5633 ****************************************************************************/
5635 static NTSTATUS smb_info_set_ea(connection_struct *conn,
5639 const struct smb_filename *smb_fname)
5641 struct ea_list *ea_list = NULL;
5642 TALLOC_CTX *ctx = NULL;
5643 NTSTATUS status = NT_STATUS_OK;
5645 if (total_data < 10) {
5647 /* OS/2 workplace shell seems to send SET_EA requests of "null"
5648 length. They seem to have no effect. Bug #3212. JRA */
5650 if ((total_data == 4) && (IVAL(pdata,0) == 4)) {
5651 /* We're done. We only get EA info in this call. */
5652 return NT_STATUS_OK;
5655 return NT_STATUS_INVALID_PARAMETER;
5658 if (IVAL(pdata,0) > total_data) {
5659 DEBUG(10,("smb_info_set_ea: bad total data size (%u) > %u\n",
5660 IVAL(pdata,0), (unsigned int)total_data));
5661 return NT_STATUS_INVALID_PARAMETER;
5665 ea_list = read_ea_list(ctx, pdata + 4, total_data - 4);
5667 return NT_STATUS_INVALID_PARAMETER;
5669 status = set_ea(conn, fsp, smb_fname, ea_list);
5674 /****************************************************************************
5675 Deal with SMB_FILE_FULL_EA_INFORMATION set.
5676 ****************************************************************************/
5678 static NTSTATUS smb_set_file_full_ea_info(connection_struct *conn,
5683 struct ea_list *ea_list = NULL;
5687 return NT_STATUS_INVALID_HANDLE;
5690 if (!lp_ea_support(SNUM(conn))) {
5691 DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u but "
5692 "EA's not supported.\n",
5693 (unsigned int)total_data));
5694 return NT_STATUS_EAS_NOT_SUPPORTED;
5697 if (total_data < 10) {
5698 DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u "
5700 (unsigned int)total_data));
5701 return NT_STATUS_INVALID_PARAMETER;
5704 ea_list = read_nttrans_ea_list(talloc_tos(),
5709 return NT_STATUS_INVALID_PARAMETER;
5711 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
5713 DEBUG(10, ("smb_set_file_full_ea_info on file %s returned %s\n",
5714 smb_fname_str_dbg(fsp->fsp_name),
5715 nt_errstr(status) ));
5721 /****************************************************************************
5722 Deal with SMB_SET_FILE_DISPOSITION_INFO.
5723 ****************************************************************************/
5725 static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
5729 struct smb_filename *smb_fname)
5731 NTSTATUS status = NT_STATUS_OK;
5732 bool delete_on_close;
5735 if (total_data < 1) {
5736 return NT_STATUS_INVALID_PARAMETER;
5740 return NT_STATUS_INVALID_HANDLE;
5743 delete_on_close = (CVAL(pdata,0) ? True : False);
5744 dosmode = dos_mode(conn, smb_fname);
5746 DEBUG(10,("smb_set_file_disposition_info: file %s, dosmode = %u, "
5747 "delete_on_close = %u\n",
5748 smb_fname_str_dbg(smb_fname),
5749 (unsigned int)dosmode,
5750 (unsigned int)delete_on_close ));
5752 if (delete_on_close) {
5753 status = can_set_delete_on_close(fsp, dosmode);
5754 if (!NT_STATUS_IS_OK(status)) {
5759 /* The set is across all open files on this dev/inode pair. */
5760 if (!set_delete_on_close(fsp, delete_on_close,
5761 &conn->server_info->utok)) {
5762 return NT_STATUS_ACCESS_DENIED;
5764 return NT_STATUS_OK;
5767 /****************************************************************************
5768 Deal with SMB_FILE_POSITION_INFORMATION.
5769 ****************************************************************************/
5771 static NTSTATUS smb_file_position_information(connection_struct *conn,
5776 uint64_t position_information;
5778 if (total_data < 8) {
5779 return NT_STATUS_INVALID_PARAMETER;
5783 /* Ignore on pathname based set. */
5784 return NT_STATUS_OK;
5787 position_information = (uint64_t)IVAL(pdata,0);
5788 #ifdef LARGE_SMB_OFF_T
5789 position_information |= (((uint64_t)IVAL(pdata,4)) << 32);
5790 #else /* LARGE_SMB_OFF_T */
5791 if (IVAL(pdata,4) != 0) {
5792 /* more than 32 bits? */
5793 return NT_STATUS_INVALID_PARAMETER;
5795 #endif /* LARGE_SMB_OFF_T */
5797 DEBUG(10,("smb_file_position_information: Set file position "
5798 "information for file %s to %.0f\n", fsp_str_dbg(fsp),
5799 (double)position_information));
5800 fsp->fh->position_information = position_information;
5801 return NT_STATUS_OK;
5804 /****************************************************************************
5805 Deal with SMB_FILE_MODE_INFORMATION.
5806 ****************************************************************************/
5808 static NTSTATUS smb_file_mode_information(connection_struct *conn,
5814 if (total_data < 4) {
5815 return NT_STATUS_INVALID_PARAMETER;
5817 mode = IVAL(pdata,0);
5818 if (mode != 0 && mode != 2 && mode != 4 && mode != 6) {
5819 return NT_STATUS_INVALID_PARAMETER;
5821 return NT_STATUS_OK;
5824 /****************************************************************************
5825 Deal with SMB_SET_FILE_UNIX_LINK (create a UNIX symlink).
5826 ****************************************************************************/
5828 static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
5829 struct smb_request *req,
5832 const struct smb_filename *smb_fname)
5834 char *link_target = NULL;
5835 const char *newname = smb_fname->base_name;
5836 TALLOC_CTX *ctx = talloc_tos();
5838 /* Set a symbolic link. */
5839 /* Don't allow this if follow links is false. */
5841 if (total_data == 0) {
5842 return NT_STATUS_INVALID_PARAMETER;
5845 if (!lp_symlinks(SNUM(conn))) {
5846 return NT_STATUS_ACCESS_DENIED;
5849 srvstr_pull_talloc(ctx, pdata, req->flags2, &link_target, pdata,
5850 total_data, STR_TERMINATE);
5853 return NT_STATUS_INVALID_PARAMETER;
5856 DEBUG(10,("smb_set_file_unix_link: SMB_SET_FILE_UNIX_LINK doing symlink %s -> %s\n",
5857 newname, link_target ));
5859 if (SMB_VFS_SYMLINK(conn,link_target,newname) != 0) {
5860 return map_nt_error_from_unix(errno);
5863 return NT_STATUS_OK;
5866 /****************************************************************************
5867 Deal with SMB_SET_FILE_UNIX_HLINK (create a UNIX hard link).
5868 ****************************************************************************/
5870 static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
5871 struct smb_request *req,
5872 const char *pdata, int total_data,
5873 const struct smb_filename *smb_fname_new)
5875 char *oldname = NULL;
5876 struct smb_filename *smb_fname_old = NULL;
5877 TALLOC_CTX *ctx = talloc_tos();
5878 NTSTATUS status = NT_STATUS_OK;
5880 /* Set a hard link. */
5881 if (total_data == 0) {
5882 return NT_STATUS_INVALID_PARAMETER;
5885 srvstr_get_path(ctx, pdata, req->flags2, &oldname, pdata,
5886 total_data, STR_TERMINATE, &status);
5887 if (!NT_STATUS_IS_OK(status)) {
5891 DEBUG(10,("smb_set_file_unix_hlink: SMB_SET_FILE_UNIX_LINK doing hard link %s -> %s\n",
5892 smb_fname_str_dbg(smb_fname_new), oldname));
5894 status = filename_convert(ctx,
5896 req->flags2 & FLAGS2_DFS_PATHNAMES,
5901 if (!NT_STATUS_IS_OK(status)) {
5905 return hardlink_internals(ctx, conn, smb_fname_old, smb_fname_new);
5908 /****************************************************************************
5909 Deal with SMB2_FILE_RENAME_INFORMATION_INTERNAL
5910 ****************************************************************************/
5912 static NTSTATUS smb2_file_rename_information(connection_struct *conn,
5913 struct smb_request *req,
5917 struct smb_filename *smb_fname_src)
5921 char *newname = NULL;
5922 struct smb_filename *smb_fname_dst = NULL;
5923 NTSTATUS status = NT_STATUS_OK;
5924 TALLOC_CTX *ctx = talloc_tos();
5927 return NT_STATUS_INVALID_HANDLE;
5930 if (total_data < 20) {
5931 return NT_STATUS_INVALID_PARAMETER;
5934 overwrite = (CVAL(pdata,0) ? True : False);
5935 len = IVAL(pdata,16);
5937 if (len > (total_data - 20) || (len == 0)) {
5938 return NT_STATUS_INVALID_PARAMETER;
5941 srvstr_get_path(ctx, pdata, req->flags2, &newname,
5942 &pdata[20], len, STR_TERMINATE,
5944 if (!NT_STATUS_IS_OK(status)) {
5948 DEBUG(10,("smb2_file_rename_information: got name |%s|\n",
5951 status = filename_convert(ctx,
5953 req->flags2 & FLAGS2_DFS_PATHNAMES,
5958 if (!NT_STATUS_IS_OK(status)) {
5962 /* Ok, this looks wrong to me, but appears to
5963 * be how SMB2 renames work. CHECK WITH Microsoft !
5966 if (fsp->oplock_type != NO_OPLOCK) {
5967 return NT_STATUS_SHARING_VIOLATION;
5970 if (fsp->base_fsp) {
5971 /* newname must be a stream name. */
5972 if (newname[0] != ':') {
5973 return NT_STATUS_NOT_SUPPORTED;
5976 /* Create an smb_fname to call rename_internals_fsp() with. */
5977 status = create_synthetic_smb_fname(talloc_tos(),
5978 fsp->base_fsp->fsp_name->base_name, newname, NULL,
5980 if (!NT_STATUS_IS_OK(status)) {
5985 * Set the original last component, since
5986 * rename_internals_fsp() requires it.
5988 smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
5990 if (smb_fname_dst->original_lcomp == NULL) {
5991 status = NT_STATUS_NO_MEMORY;
5997 DEBUG(10,("smb2_file_rename_information: "
5998 "SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
5999 fsp->fnum, fsp_str_dbg(fsp),
6000 smb_fname_str_dbg(smb_fname_dst)));
6001 status = rename_internals_fsp(conn, fsp, smb_fname_dst, 0,
6005 TALLOC_FREE(smb_fname_dst);
6010 /****************************************************************************
6011 Deal with SMB_FILE_RENAME_INFORMATION.
6012 ****************************************************************************/
6014 static NTSTATUS smb_file_rename_information(connection_struct *conn,
6015 struct smb_request *req,
6019 struct smb_filename *smb_fname_src)
6024 char *newname = NULL;
6025 struct smb_filename *smb_fname_dst = NULL;
6026 bool dest_has_wcard = False;
6027 NTSTATUS status = NT_STATUS_OK;
6029 TALLOC_CTX *ctx = talloc_tos();
6031 if (total_data < 13) {
6032 return NT_STATUS_INVALID_PARAMETER;
6035 overwrite = (CVAL(pdata,0) ? True : False);
6036 root_fid = IVAL(pdata,4);
6037 len = IVAL(pdata,8);
6039 if (len > (total_data - 12) || (len == 0) || (root_fid != 0)) {
6040 return NT_STATUS_INVALID_PARAMETER;
6043 srvstr_get_path_wcard(ctx, pdata, req->flags2, &newname, &pdata[12],
6046 if (!NT_STATUS_IS_OK(status)) {
6050 DEBUG(10,("smb_file_rename_information: got name |%s|\n",
6053 status = resolve_dfspath_wcard(ctx, conn,
6054 req->flags2 & FLAGS2_DFS_PATHNAMES,
6059 if (!NT_STATUS_IS_OK(status)) {
6063 /* Check the new name has no '/' characters. */
6064 if (strchr_m(newname, '/')) {
6065 return NT_STATUS_NOT_SUPPORTED;
6068 if (fsp && fsp->base_fsp) {
6069 /* newname must be a stream name. */
6070 if (newname[0] != ':') {
6071 return NT_STATUS_NOT_SUPPORTED;
6074 /* Create an smb_fname to call rename_internals_fsp() with. */
6075 status = create_synthetic_smb_fname(talloc_tos(),
6076 fsp->base_fsp->fsp_name->base_name, newname, NULL,
6078 if (!NT_STATUS_IS_OK(status)) {
6083 * Set the original last component, since
6084 * rename_internals_fsp() requires it.
6086 smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
6088 if (smb_fname_dst->original_lcomp == NULL) {
6089 status = NT_STATUS_NO_MEMORY;
6095 * Build up an smb_fname_dst based on the filename passed in.
6096 * We basically just strip off the last component, and put on
6097 * the newname instead.
6099 char *base_name = NULL;
6101 /* newname must *not* be a stream name. */
6102 if (newname[0] == ':') {
6103 return NT_STATUS_NOT_SUPPORTED;
6107 * Strip off the last component (filename) of the path passed
6110 base_name = talloc_strdup(ctx, smb_fname_src->base_name);
6112 return NT_STATUS_NO_MEMORY;
6114 p = strrchr_m(base_name, '/');
6118 base_name = talloc_strdup(ctx, "./");
6120 return NT_STATUS_NO_MEMORY;
6123 /* Append the new name. */
6124 base_name = talloc_asprintf_append(base_name,
6128 return NT_STATUS_NO_MEMORY;
6131 status = unix_convert(ctx, conn, base_name, &smb_fname_dst,
6134 UCF_ALWAYS_ALLOW_WCARD_LCOMP :
6137 /* If an error we expect this to be
6138 * NT_STATUS_OBJECT_PATH_NOT_FOUND */
6140 if (!NT_STATUS_IS_OK(status)) {
6141 if(!NT_STATUS_EQUAL(NT_STATUS_OBJECT_PATH_NOT_FOUND,
6145 /* Create an smb_fname to call rename_internals_fsp() */
6146 status = create_synthetic_smb_fname(ctx,
6150 if (!NT_STATUS_IS_OK(status)) {
6157 DEBUG(10,("smb_file_rename_information: "
6158 "SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
6159 fsp->fnum, fsp_str_dbg(fsp),
6160 smb_fname_str_dbg(smb_fname_dst)));
6161 status = rename_internals_fsp(conn, fsp, smb_fname_dst, 0,
6164 DEBUG(10,("smb_file_rename_information: "
6165 "SMB_FILE_RENAME_INFORMATION %s -> %s\n",
6166 smb_fname_str_dbg(smb_fname_src),
6167 smb_fname_str_dbg(smb_fname_dst)));
6168 status = rename_internals(ctx, conn, req, smb_fname_src,
6169 smb_fname_dst, 0, overwrite, false,
6171 FILE_WRITE_ATTRIBUTES);
6174 TALLOC_FREE(smb_fname_dst);
6178 /****************************************************************************
6179 Deal with SMB_SET_POSIX_ACL.
6180 ****************************************************************************/
6182 #if defined(HAVE_POSIX_ACLS)
6183 static NTSTATUS smb_set_posix_acl(connection_struct *conn,
6187 const struct smb_filename *smb_fname)
6189 uint16 posix_acl_version;
6190 uint16 num_file_acls;
6191 uint16 num_def_acls;
6192 bool valid_file_acls = True;
6193 bool valid_def_acls = True;
6195 if (total_data < SMB_POSIX_ACL_HEADER_SIZE) {
6196 return NT_STATUS_INVALID_PARAMETER;
6198 posix_acl_version = SVAL(pdata,0);
6199 num_file_acls = SVAL(pdata,2);
6200 num_def_acls = SVAL(pdata,4);
6202 if (num_file_acls == SMB_POSIX_IGNORE_ACE_ENTRIES) {
6203 valid_file_acls = False;
6207 if (num_def_acls == SMB_POSIX_IGNORE_ACE_ENTRIES) {
6208 valid_def_acls = False;
6212 if (posix_acl_version != SMB_POSIX_ACL_VERSION) {
6213 return NT_STATUS_INVALID_PARAMETER;
6216 if (total_data < SMB_POSIX_ACL_HEADER_SIZE +
6217 (num_file_acls+num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE) {
6218 return NT_STATUS_INVALID_PARAMETER;
6221 DEBUG(10,("smb_set_posix_acl: file %s num_file_acls = %u, num_def_acls = %u\n",
6222 smb_fname ? smb_fname_str_dbg(smb_fname) : fsp_str_dbg(fsp),
6223 (unsigned int)num_file_acls,
6224 (unsigned int)num_def_acls));
6226 if (valid_file_acls && !set_unix_posix_acl(conn, fsp,
6227 smb_fname->base_name, num_file_acls,
6228 pdata + SMB_POSIX_ACL_HEADER_SIZE)) {
6229 return map_nt_error_from_unix(errno);
6232 if (valid_def_acls && !set_unix_posix_default_acl(conn,
6233 smb_fname->base_name, &smb_fname->st, num_def_acls,
6234 pdata + SMB_POSIX_ACL_HEADER_SIZE +
6235 (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE))) {
6236 return map_nt_error_from_unix(errno);
6238 return NT_STATUS_OK;
6242 /****************************************************************************
6243 Deal with SMB_SET_POSIX_LOCK.
6244 ****************************************************************************/
6246 static NTSTATUS smb_set_posix_lock(connection_struct *conn,
6247 struct smb_request *req,
6255 bool blocking_lock = False;
6256 enum brl_type lock_type;
6258 NTSTATUS status = NT_STATUS_OK;
6260 if (fsp == NULL || fsp->fh->fd == -1) {
6261 return NT_STATUS_INVALID_HANDLE;
6264 if (total_data != POSIX_LOCK_DATA_SIZE) {
6265 return NT_STATUS_INVALID_PARAMETER;
6268 switch (SVAL(pdata, POSIX_LOCK_TYPE_OFFSET)) {
6269 case POSIX_LOCK_TYPE_READ:
6270 lock_type = READ_LOCK;
6272 case POSIX_LOCK_TYPE_WRITE:
6273 /* Return the right POSIX-mappable error code for files opened read-only. */
6274 if (!fsp->can_write) {
6275 return NT_STATUS_INVALID_HANDLE;
6277 lock_type = WRITE_LOCK;
6279 case POSIX_LOCK_TYPE_UNLOCK:
6280 lock_type = UNLOCK_LOCK;
6283 return NT_STATUS_INVALID_PARAMETER;
6286 if (SVAL(pdata,POSIX_LOCK_FLAGS_OFFSET) == POSIX_LOCK_FLAG_NOWAIT) {
6287 blocking_lock = False;
6288 } else if (SVAL(pdata,POSIX_LOCK_FLAGS_OFFSET) == POSIX_LOCK_FLAG_WAIT) {
6289 blocking_lock = True;
6291 return NT_STATUS_INVALID_PARAMETER;
6294 if (!lp_blocking_locks(SNUM(conn))) {
6295 blocking_lock = False;
6298 smblctx = (uint64_t)IVAL(pdata, POSIX_LOCK_PID_OFFSET);
6299 #if defined(HAVE_LONGLONG)
6300 offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
6301 ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
6302 count = (((uint64_t) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
6303 ((uint64_t) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
6304 #else /* HAVE_LONGLONG */
6305 offset = (uint64_t)IVAL(pdata,POSIX_LOCK_START_OFFSET);
6306 count = (uint64_t)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
6307 #endif /* HAVE_LONGLONG */
6309 DEBUG(10,("smb_set_posix_lock: file %s, lock_type = %u,"
6310 "smblctx = %llu, count = %.0f, offset = %.0f\n",
6312 (unsigned int)lock_type,
6313 (unsigned long long)smblctx,
6317 if (lock_type == UNLOCK_LOCK) {
6318 status = do_unlock(smbd_messaging_context(),
6325 uint64_t block_smblctx;
6327 struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
6339 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
6341 * A blocking lock was requested. Package up
6342 * this smb into a queued request and push it
6343 * onto the blocking lock queue.
6345 if(push_blocking_lock_request(br_lck,
6348 -1, /* infinite timeout. */
6356 TALLOC_FREE(br_lck);
6360 TALLOC_FREE(br_lck);
6366 /****************************************************************************
6367 Deal with SMB_SET_FILE_BASIC_INFO.
6368 ****************************************************************************/
6370 static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
6374 const struct smb_filename *smb_fname)
6376 /* Patch to do this correctly from Paul Eggert <eggert@twinsun.com>. */
6377 struct smb_file_time ft;
6379 NTSTATUS status = NT_STATUS_OK;
6383 if (total_data < 36) {
6384 return NT_STATUS_INVALID_PARAMETER;
6387 /* Set the attributes */
6388 dosmode = IVAL(pdata,32);
6389 status = smb_set_file_dosmode(conn, smb_fname, dosmode);
6390 if (!NT_STATUS_IS_OK(status)) {
6395 ft.create_time = interpret_long_date(pdata);
6398 ft.atime = interpret_long_date(pdata+8);
6401 ft.mtime = interpret_long_date(pdata+16);
6404 ft.ctime = interpret_long_date(pdata+24);
6406 DEBUG(10, ("smb_set_file_basic_info: file %s\n",
6407 smb_fname_str_dbg(smb_fname)));
6409 return smb_set_file_time(conn, fsp, smb_fname, &ft,
6413 /****************************************************************************
6414 Deal with SMB_INFO_STANDARD.
6415 ****************************************************************************/
6417 static NTSTATUS smb_set_info_standard(connection_struct *conn,
6421 const struct smb_filename *smb_fname)
6423 struct smb_file_time ft;
6427 if (total_data < 12) {
6428 return NT_STATUS_INVALID_PARAMETER;
6432 ft.create_time = convert_time_t_to_timespec(srv_make_unix_date2(pdata));
6434 ft.atime = convert_time_t_to_timespec(srv_make_unix_date2(pdata+4));
6436 ft.mtime = convert_time_t_to_timespec(srv_make_unix_date2(pdata+8));
6438 DEBUG(10,("smb_set_info_standard: file %s\n",
6439 smb_fname_str_dbg(smb_fname)));
6441 return smb_set_file_time(conn,
6448 /****************************************************************************
6449 Deal with SMB_SET_FILE_ALLOCATION_INFO.
6450 ****************************************************************************/
6452 static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
6453 struct smb_request *req,
6457 struct smb_filename *smb_fname)
6459 uint64_t allocation_size = 0;
6460 NTSTATUS status = NT_STATUS_OK;
6461 files_struct *new_fsp = NULL;
6463 if (!VALID_STAT(smb_fname->st)) {
6464 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
6467 if (total_data < 8) {
6468 return NT_STATUS_INVALID_PARAMETER;
6471 allocation_size = (uint64_t)IVAL(pdata,0);
6472 #ifdef LARGE_SMB_OFF_T
6473 allocation_size |= (((uint64_t)IVAL(pdata,4)) << 32);
6474 #else /* LARGE_SMB_OFF_T */
6475 if (IVAL(pdata,4) != 0) {
6476 /* more than 32 bits? */
6477 return NT_STATUS_INVALID_PARAMETER;
6479 #endif /* LARGE_SMB_OFF_T */
6481 DEBUG(10,("smb_set_file_allocation_info: Set file allocation info for "
6482 "file %s to %.0f\n", smb_fname_str_dbg(smb_fname),
6483 (double)allocation_size));
6485 if (allocation_size) {
6486 allocation_size = smb_roundup(conn, allocation_size);
6489 DEBUG(10,("smb_set_file_allocation_info: file %s : setting new "
6490 "allocation size to %.0f\n", smb_fname_str_dbg(smb_fname),
6491 (double)allocation_size));
6493 if (fsp && fsp->fh->fd != -1) {
6494 /* Open file handle. */
6495 /* Only change if needed. */
6496 if (allocation_size != get_file_size_stat(&smb_fname->st)) {
6497 if (vfs_allocate_file_space(fsp, allocation_size) == -1) {
6498 return map_nt_error_from_unix(errno);
6501 /* But always update the time. */
6503 * This is equivalent to a write. Ensure it's seen immediately
6504 * if there are no pending writes.
6506 trigger_write_time_update_immediate(fsp);
6507 return NT_STATUS_OK;
6510 /* Pathname or stat or directory file. */
6511 status = SMB_VFS_CREATE_FILE(
6514 0, /* root_dir_fid */
6515 smb_fname, /* fname */
6516 FILE_WRITE_DATA, /* access_mask */
6517 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
6519 FILE_OPEN, /* create_disposition*/
6520 0, /* create_options */
6521 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
6522 FORCE_OPLOCK_BREAK_TO_NONE, /* oplock_request */
6523 0, /* allocation_size */
6524 0, /* private_flags */
6527 &new_fsp, /* result */
6530 if (!NT_STATUS_IS_OK(status)) {
6531 /* NB. We check for open_was_deferred in the caller. */
6535 /* Only change if needed. */
6536 if (allocation_size != get_file_size_stat(&smb_fname->st)) {
6537 if (vfs_allocate_file_space(new_fsp, allocation_size) == -1) {
6538 status = map_nt_error_from_unix(errno);
6539 close_file(req, new_fsp, NORMAL_CLOSE);
6544 /* Changing the allocation size should set the last mod time. */
6546 * This is equivalent to a write. Ensure it's seen immediately
6547 * if there are no pending writes.
6549 trigger_write_time_update_immediate(new_fsp);
6551 close_file(req, new_fsp, NORMAL_CLOSE);
6552 return NT_STATUS_OK;
6555 /****************************************************************************
6556 Deal with SMB_SET_FILE_END_OF_FILE_INFO.
6557 ****************************************************************************/
6559 static NTSTATUS smb_set_file_end_of_file_info(connection_struct *conn,
6560 struct smb_request *req,
6564 const struct smb_filename *smb_fname,
6565 bool fail_after_createfile)
6569 if (total_data < 8) {
6570 return NT_STATUS_INVALID_PARAMETER;
6573 size = IVAL(pdata,0);
6574 #ifdef LARGE_SMB_OFF_T
6575 size |= (((SMB_OFF_T)IVAL(pdata,4)) << 32);
6576 #else /* LARGE_SMB_OFF_T */
6577 if (IVAL(pdata,4) != 0) {
6578 /* more than 32 bits? */
6579 return NT_STATUS_INVALID_PARAMETER;
6581 #endif /* LARGE_SMB_OFF_T */
6582 DEBUG(10,("smb_set_file_end_of_file_info: Set end of file info for "
6583 "file %s to %.0f\n", smb_fname_str_dbg(smb_fname),
6586 return smb_set_file_size(conn, req,
6591 fail_after_createfile);
6594 /****************************************************************************
6595 Allow a UNIX info mknod.
6596 ****************************************************************************/
6598 static NTSTATUS smb_unix_mknod(connection_struct *conn,
6601 const struct smb_filename *smb_fname)
6603 uint32 file_type = IVAL(pdata,56);
6604 #if defined(HAVE_MAKEDEV)
6605 uint32 dev_major = IVAL(pdata,60);
6606 uint32 dev_minor = IVAL(pdata,68);
6608 SMB_DEV_T dev = (SMB_DEV_T)0;
6609 uint32 raw_unixmode = IVAL(pdata,84);
6613 if (total_data < 100) {
6614 return NT_STATUS_INVALID_PARAMETER;
6617 status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
6618 PERM_NEW_FILE, &unixmode);
6619 if (!NT_STATUS_IS_OK(status)) {
6623 #if defined(HAVE_MAKEDEV)
6624 dev = makedev(dev_major, dev_minor);
6627 switch (file_type) {
6628 #if defined(S_IFIFO)
6629 case UNIX_TYPE_FIFO:
6630 unixmode |= S_IFIFO;
6633 #if defined(S_IFSOCK)
6634 case UNIX_TYPE_SOCKET:
6635 unixmode |= S_IFSOCK;
6638 #if defined(S_IFCHR)
6639 case UNIX_TYPE_CHARDEV:
6640 unixmode |= S_IFCHR;
6643 #if defined(S_IFBLK)
6644 case UNIX_TYPE_BLKDEV:
6645 unixmode |= S_IFBLK;
6649 return NT_STATUS_INVALID_PARAMETER;
6652 DEBUG(10,("smb_unix_mknod: SMB_SET_FILE_UNIX_BASIC doing mknod dev "
6653 "%.0f mode 0%o for file %s\n", (double)dev,
6654 (unsigned int)unixmode, smb_fname_str_dbg(smb_fname)));
6656 /* Ok - do the mknod. */
6657 if (SMB_VFS_MKNOD(conn, smb_fname->base_name, unixmode, dev) != 0) {
6658 return map_nt_error_from_unix(errno);
6661 /* If any of the other "set" calls fail we
6662 * don't want to end up with a half-constructed mknod.
6665 if (lp_inherit_perms(SNUM(conn))) {
6667 if (!parent_dirname(talloc_tos(), smb_fname->base_name,
6669 return NT_STATUS_NO_MEMORY;
6671 inherit_access_posix_acl(conn, parent, smb_fname->base_name,
6673 TALLOC_FREE(parent);
6676 return NT_STATUS_OK;
6679 /****************************************************************************
6680 Deal with SMB_SET_FILE_UNIX_BASIC.
6681 ****************************************************************************/
6683 static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
6684 struct smb_request *req,
6688 const struct smb_filename *smb_fname)
6690 struct smb_file_time ft;
6691 uint32 raw_unixmode;
6694 uid_t set_owner = (uid_t)SMB_UID_NO_CHANGE;
6695 gid_t set_grp = (uid_t)SMB_GID_NO_CHANGE;
6696 NTSTATUS status = NT_STATUS_OK;
6697 bool delete_on_fail = False;
6698 enum perm_type ptype;
6699 files_struct *all_fsps = NULL;
6700 bool modify_mtime = true;
6702 SMB_STRUCT_STAT sbuf;
6706 if (total_data < 100) {
6707 return NT_STATUS_INVALID_PARAMETER;
6710 if(IVAL(pdata, 0) != SMB_SIZE_NO_CHANGE_LO &&
6711 IVAL(pdata, 4) != SMB_SIZE_NO_CHANGE_HI) {
6712 size=IVAL(pdata,0); /* first 8 Bytes are size */
6713 #ifdef LARGE_SMB_OFF_T
6714 size |= (((SMB_OFF_T)IVAL(pdata,4)) << 32);
6715 #else /* LARGE_SMB_OFF_T */
6716 if (IVAL(pdata,4) != 0) {
6717 /* more than 32 bits? */
6718 return NT_STATUS_INVALID_PARAMETER;
6720 #endif /* LARGE_SMB_OFF_T */
6723 ft.atime = interpret_long_date(pdata+24); /* access_time */
6724 ft.mtime = interpret_long_date(pdata+32); /* modification_time */
6725 set_owner = (uid_t)IVAL(pdata,40);
6726 set_grp = (gid_t)IVAL(pdata,48);
6727 raw_unixmode = IVAL(pdata,84);
6729 if (VALID_STAT(smb_fname->st)) {
6730 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
6731 ptype = PERM_EXISTING_DIR;
6733 ptype = PERM_EXISTING_FILE;
6736 ptype = PERM_NEW_FILE;
6739 status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
6741 if (!NT_STATUS_IS_OK(status)) {
6745 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC: name = "
6746 "%s size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
6747 smb_fname_str_dbg(smb_fname), (double)size,
6748 (unsigned int)set_owner, (unsigned int)set_grp,
6749 (int)raw_unixmode));
6751 sbuf = smb_fname->st;
6753 if (!VALID_STAT(sbuf)) {
6754 struct smb_filename *smb_fname_tmp = NULL;
6756 * The only valid use of this is to create character and block
6757 * devices, and named pipes. This is deprecated (IMHO) and
6758 * a new info level should be used for mknod. JRA.
6761 status = smb_unix_mknod(conn,
6765 if (!NT_STATUS_IS_OK(status)) {
6769 status = copy_smb_filename(talloc_tos(), smb_fname,
6771 if (!NT_STATUS_IS_OK(status)) {
6775 if (SMB_VFS_STAT(conn, smb_fname_tmp) != 0) {
6776 status = map_nt_error_from_unix(errno);
6777 TALLOC_FREE(smb_fname_tmp);
6778 SMB_VFS_UNLINK(conn, smb_fname);
6782 sbuf = smb_fname_tmp->st;
6783 TALLOC_FREE(smb_fname_tmp);
6785 /* Ensure we don't try and change anything else. */
6786 raw_unixmode = SMB_MODE_NO_CHANGE;
6787 size = get_file_size_stat(&sbuf);
6788 ft.atime = sbuf.st_ex_atime;
6789 ft.mtime = sbuf.st_ex_mtime;
6791 * We continue here as we might want to change the
6794 delete_on_fail = True;
6798 /* Horrible backwards compatibility hack as an old server bug
6799 * allowed a CIFS client bug to remain unnoticed :-(. JRA.
6803 size = get_file_size_stat(&sbuf);
6808 * Deal with the UNIX specific mode set.
6811 if (raw_unixmode != SMB_MODE_NO_CHANGE) {
6812 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
6813 "setting mode 0%o for file %s\n",
6814 (unsigned int)unixmode,
6815 smb_fname_str_dbg(smb_fname)));
6816 if (SMB_VFS_CHMOD(conn, smb_fname->base_name, unixmode) != 0) {
6817 return map_nt_error_from_unix(errno);
6822 * Deal with the UNIX specific uid set.
6825 if ((set_owner != (uid_t)SMB_UID_NO_CHANGE) &&
6826 (sbuf.st_ex_uid != set_owner)) {
6829 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
6830 "changing owner %u for path %s\n",
6831 (unsigned int)set_owner,
6832 smb_fname_str_dbg(smb_fname)));
6834 if (S_ISLNK(sbuf.st_ex_mode)) {
6835 ret = SMB_VFS_LCHOWN(conn, smb_fname->base_name,
6836 set_owner, (gid_t)-1);
6838 ret = SMB_VFS_CHOWN(conn, smb_fname->base_name,
6839 set_owner, (gid_t)-1);
6843 status = map_nt_error_from_unix(errno);
6844 if (delete_on_fail) {
6845 SMB_VFS_UNLINK(conn, smb_fname);
6852 * Deal with the UNIX specific gid set.
6855 if ((set_grp != (uid_t)SMB_GID_NO_CHANGE) &&
6856 (sbuf.st_ex_gid != set_grp)) {
6857 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
6858 "changing group %u for file %s\n",
6859 (unsigned int)set_owner,
6860 smb_fname_str_dbg(smb_fname)));
6861 if (SMB_VFS_CHOWN(conn, smb_fname->base_name, (uid_t)-1,
6863 status = map_nt_error_from_unix(errno);
6864 if (delete_on_fail) {
6865 SMB_VFS_UNLINK(conn, smb_fname);
6871 /* Deal with any size changes. */
6873 status = smb_set_file_size(conn, req,
6879 if (!NT_STATUS_IS_OK(status)) {
6883 /* Deal with any time changes. */
6884 if (null_timespec(ft.mtime) && null_timespec(ft.atime)) {
6885 /* No change, don't cancel anything. */
6889 id = vfs_file_id_from_sbuf(conn, &sbuf);
6890 for(all_fsps = file_find_di_first(id); all_fsps;
6891 all_fsps = file_find_di_next(all_fsps)) {
6893 * We're setting the time explicitly for UNIX.
6894 * Cancel any pending changes over all handles.
6896 all_fsps->update_write_time_on_close = false;
6897 TALLOC_FREE(all_fsps->update_write_time_event);
6901 * Override the "setting_write_time"
6902 * parameter here as it almost does what
6903 * we need. Just remember if we modified
6904 * mtime and send the notify ourselves.
6906 if (null_timespec(ft.mtime)) {
6907 modify_mtime = false;
6910 status = smb_set_file_time(conn,
6916 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
6917 FILE_NOTIFY_CHANGE_LAST_WRITE, smb_fname->base_name);
6922 /****************************************************************************
6923 Deal with SMB_SET_FILE_UNIX_INFO2.
6924 ****************************************************************************/
6926 static NTSTATUS smb_set_file_unix_info2(connection_struct *conn,
6927 struct smb_request *req,
6931 const struct smb_filename *smb_fname)
6937 if (total_data < 116) {
6938 return NT_STATUS_INVALID_PARAMETER;
6941 /* Start by setting all the fields that are common between UNIX_BASIC
6944 status = smb_set_file_unix_basic(conn, req, pdata, total_data,
6946 if (!NT_STATUS_IS_OK(status)) {
6950 smb_fflags = IVAL(pdata, 108);
6951 smb_fmask = IVAL(pdata, 112);
6953 /* NB: We should only attempt to alter the file flags if the client
6954 * sends a non-zero mask.
6956 if (smb_fmask != 0) {
6957 int stat_fflags = 0;
6959 if (!map_info2_flags_to_sbuf(&smb_fname->st, smb_fflags,
6960 smb_fmask, &stat_fflags)) {
6961 /* Client asked to alter a flag we don't understand. */
6962 return NT_STATUS_INVALID_PARAMETER;
6965 if (fsp && fsp->fh->fd != -1) {
6966 /* XXX: we should be using SMB_VFS_FCHFLAGS here. */
6967 return NT_STATUS_NOT_SUPPORTED;
6969 if (SMB_VFS_CHFLAGS(conn, smb_fname->base_name,
6970 stat_fflags) != 0) {
6971 return map_nt_error_from_unix(errno);
6976 /* XXX: need to add support for changing the create_time here. You
6977 * can do this for paths on Darwin with setattrlist(2). The right way
6978 * to hook this up is probably by extending the VFS utimes interface.
6981 return NT_STATUS_OK;
6984 /****************************************************************************
6985 Create a directory with POSIX semantics.
6986 ****************************************************************************/
6988 static NTSTATUS smb_posix_mkdir(connection_struct *conn,
6989 struct smb_request *req,
6992 struct smb_filename *smb_fname,
6993 int *pdata_return_size)
6995 NTSTATUS status = NT_STATUS_OK;
6996 uint32 raw_unixmode = 0;
6997 uint32 mod_unixmode = 0;
6998 mode_t unixmode = (mode_t)0;
6999 files_struct *fsp = NULL;
7000 uint16 info_level_return = 0;
7002 char *pdata = *ppdata;
7004 if (total_data < 18) {
7005 return NT_STATUS_INVALID_PARAMETER;
7008 raw_unixmode = IVAL(pdata,8);
7009 /* Next 4 bytes are not yet defined. */
7011 status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
7012 PERM_NEW_DIR, &unixmode);
7013 if (!NT_STATUS_IS_OK(status)) {
7017 mod_unixmode = (uint32)unixmode | FILE_FLAG_POSIX_SEMANTICS;
7019 DEBUG(10,("smb_posix_mkdir: file %s, mode 0%o\n",
7020 smb_fname_str_dbg(smb_fname), (unsigned int)unixmode));
7022 status = SMB_VFS_CREATE_FILE(
7025 0, /* root_dir_fid */
7026 smb_fname, /* fname */
7027 FILE_READ_ATTRIBUTES, /* access_mask */
7028 FILE_SHARE_NONE, /* share_access */
7029 FILE_CREATE, /* create_disposition*/
7030 FILE_DIRECTORY_FILE, /* create_options */
7031 mod_unixmode, /* file_attributes */
7032 0, /* oplock_request */
7033 0, /* allocation_size */
7034 0, /* private_flags */
7040 if (NT_STATUS_IS_OK(status)) {
7041 close_file(req, fsp, NORMAL_CLOSE);
7044 info_level_return = SVAL(pdata,16);
7046 if (info_level_return == SMB_QUERY_FILE_UNIX_BASIC) {
7047 *pdata_return_size = 12 + SMB_FILE_UNIX_BASIC_SIZE;
7048 } else if (info_level_return == SMB_QUERY_FILE_UNIX_INFO2) {
7049 *pdata_return_size = 12 + SMB_FILE_UNIX_INFO2_SIZE;
7051 *pdata_return_size = 12;
7054 /* Realloc the data size */
7055 *ppdata = (char *)SMB_REALLOC(*ppdata,*pdata_return_size);
7056 if (*ppdata == NULL) {
7057 *pdata_return_size = 0;
7058 return NT_STATUS_NO_MEMORY;
7062 SSVAL(pdata,0,NO_OPLOCK_RETURN);
7063 SSVAL(pdata,2,0); /* No fnum. */
7064 SIVAL(pdata,4,info); /* Was directory created. */
7066 switch (info_level_return) {
7067 case SMB_QUERY_FILE_UNIX_BASIC:
7068 SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_BASIC);
7069 SSVAL(pdata,10,0); /* Padding. */
7070 store_file_unix_basic(conn, pdata + 12, fsp,
7073 case SMB_QUERY_FILE_UNIX_INFO2:
7074 SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_INFO2);
7075 SSVAL(pdata,10,0); /* Padding. */
7076 store_file_unix_basic_info2(conn, pdata + 12, fsp,
7080 SSVAL(pdata,8,SMB_NO_INFO_LEVEL_RETURNED);
7081 SSVAL(pdata,10,0); /* Padding. */
7088 /****************************************************************************
7089 Open/Create a file with POSIX semantics.
7090 ****************************************************************************/
7092 static NTSTATUS smb_posix_open(connection_struct *conn,
7093 struct smb_request *req,
7096 struct smb_filename *smb_fname,
7097 int *pdata_return_size)
7099 bool extended_oplock_granted = False;
7100 char *pdata = *ppdata;
7102 uint32 wire_open_mode = 0;
7103 uint32 raw_unixmode = 0;
7104 uint32 mod_unixmode = 0;
7105 uint32 create_disp = 0;
7106 uint32 access_mask = 0;
7107 uint32 create_options = 0;
7108 NTSTATUS status = NT_STATUS_OK;
7109 mode_t unixmode = (mode_t)0;
7110 files_struct *fsp = NULL;
7111 int oplock_request = 0;
7113 uint16 info_level_return = 0;
7115 if (total_data < 18) {
7116 return NT_STATUS_INVALID_PARAMETER;
7119 flags = IVAL(pdata,0);
7120 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
7121 if (oplock_request) {
7122 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
7125 wire_open_mode = IVAL(pdata,4);
7127 if (wire_open_mode == (SMB_O_CREAT|SMB_O_DIRECTORY)) {
7128 return smb_posix_mkdir(conn, req,
7135 switch (wire_open_mode & SMB_ACCMODE) {
7137 access_mask = FILE_READ_DATA;
7140 access_mask = FILE_WRITE_DATA;
7143 access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
7146 DEBUG(5,("smb_posix_open: invalid open mode 0x%x\n",
7147 (unsigned int)wire_open_mode ));
7148 return NT_STATUS_INVALID_PARAMETER;
7151 wire_open_mode &= ~SMB_ACCMODE;
7153 if((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL)) == (SMB_O_CREAT | SMB_O_EXCL)) {
7154 create_disp = FILE_CREATE;
7155 } else if((wire_open_mode & (SMB_O_CREAT | SMB_O_TRUNC)) == (SMB_O_CREAT | SMB_O_TRUNC)) {
7156 create_disp = FILE_OVERWRITE_IF;
7157 } else if((wire_open_mode & SMB_O_CREAT) == SMB_O_CREAT) {
7158 create_disp = FILE_OPEN_IF;
7159 } else if ((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL | SMB_O_TRUNC)) == 0) {
7160 create_disp = FILE_OPEN;
7162 DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
7163 (unsigned int)wire_open_mode ));
7164 return NT_STATUS_INVALID_PARAMETER;
7167 raw_unixmode = IVAL(pdata,8);
7168 /* Next 4 bytes are not yet defined. */
7170 status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
7171 (VALID_STAT(smb_fname->st) ?
7172 PERM_EXISTING_FILE : PERM_NEW_FILE),
7175 if (!NT_STATUS_IS_OK(status)) {
7179 mod_unixmode = (uint32)unixmode | FILE_FLAG_POSIX_SEMANTICS;
7181 if (wire_open_mode & SMB_O_SYNC) {
7182 create_options |= FILE_WRITE_THROUGH;
7184 if (wire_open_mode & SMB_O_APPEND) {
7185 access_mask |= FILE_APPEND_DATA;
7187 if (wire_open_mode & SMB_O_DIRECT) {
7188 mod_unixmode |= FILE_FLAG_NO_BUFFERING;
7191 DEBUG(10,("smb_posix_open: file %s, smb_posix_flags = %u, mode 0%o\n",
7192 smb_fname_str_dbg(smb_fname),
7193 (unsigned int)wire_open_mode,
7194 (unsigned int)unixmode ));
7196 status = SMB_VFS_CREATE_FILE(
7199 0, /* root_dir_fid */
7200 smb_fname, /* fname */
7201 access_mask, /* access_mask */
7202 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
7204 create_disp, /* create_disposition*/
7205 FILE_NON_DIRECTORY_FILE, /* create_options */
7206 mod_unixmode, /* file_attributes */
7207 oplock_request, /* oplock_request */
7208 0, /* allocation_size */
7209 0, /* private_flags */
7215 if (!NT_STATUS_IS_OK(status)) {
7219 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
7220 extended_oplock_granted = True;
7223 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
7224 extended_oplock_granted = True;
7227 info_level_return = SVAL(pdata,16);
7229 /* Allocate the correct return size. */
7231 if (info_level_return == SMB_QUERY_FILE_UNIX_BASIC) {
7232 *pdata_return_size = 12 + SMB_FILE_UNIX_BASIC_SIZE;
7233 } else if (info_level_return == SMB_QUERY_FILE_UNIX_INFO2) {
7234 *pdata_return_size = 12 + SMB_FILE_UNIX_INFO2_SIZE;
7236 *pdata_return_size = 12;
7239 /* Realloc the data size */
7240 *ppdata = (char *)SMB_REALLOC(*ppdata,*pdata_return_size);
7241 if (*ppdata == NULL) {
7242 close_file(req, fsp, ERROR_CLOSE);
7243 *pdata_return_size = 0;
7244 return NT_STATUS_NO_MEMORY;
7248 if (extended_oplock_granted) {
7249 if (flags & REQUEST_BATCH_OPLOCK) {
7250 SSVAL(pdata,0, BATCH_OPLOCK_RETURN);
7252 SSVAL(pdata,0, EXCLUSIVE_OPLOCK_RETURN);
7254 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
7255 SSVAL(pdata,0, LEVEL_II_OPLOCK_RETURN);
7257 SSVAL(pdata,0,NO_OPLOCK_RETURN);
7260 SSVAL(pdata,2,fsp->fnum);
7261 SIVAL(pdata,4,info); /* Was file created etc. */
7263 switch (info_level_return) {
7264 case SMB_QUERY_FILE_UNIX_BASIC:
7265 SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_BASIC);
7266 SSVAL(pdata,10,0); /* padding. */
7267 store_file_unix_basic(conn, pdata + 12, fsp,
7270 case SMB_QUERY_FILE_UNIX_INFO2:
7271 SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_INFO2);
7272 SSVAL(pdata,10,0); /* padding. */
7273 store_file_unix_basic_info2(conn, pdata + 12, fsp,
7277 SSVAL(pdata,8,SMB_NO_INFO_LEVEL_RETURNED);
7278 SSVAL(pdata,10,0); /* padding. */
7281 return NT_STATUS_OK;
7284 /****************************************************************************
7285 Delete a file with POSIX semantics.
7286 ****************************************************************************/
7288 static NTSTATUS smb_posix_unlink(connection_struct *conn,
7289 struct smb_request *req,
7292 struct smb_filename *smb_fname)
7294 NTSTATUS status = NT_STATUS_OK;
7295 files_struct *fsp = NULL;
7299 int create_options = 0;
7301 struct share_mode_lock *lck = NULL;
7303 if (total_data < 2) {
7304 return NT_STATUS_INVALID_PARAMETER;
7307 flags = SVAL(pdata,0);
7309 if (!VALID_STAT(smb_fname->st)) {
7310 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
7313 if ((flags == SMB_POSIX_UNLINK_DIRECTORY_TARGET) &&
7314 !VALID_STAT_OF_DIR(smb_fname->st)) {
7315 return NT_STATUS_NOT_A_DIRECTORY;
7318 DEBUG(10,("smb_posix_unlink: %s %s\n",
7319 (flags == SMB_POSIX_UNLINK_DIRECTORY_TARGET) ? "directory" : "file",
7320 smb_fname_str_dbg(smb_fname)));
7322 if (VALID_STAT_OF_DIR(smb_fname->st)) {
7323 create_options |= FILE_DIRECTORY_FILE;
7326 status = SMB_VFS_CREATE_FILE(
7329 0, /* root_dir_fid */
7330 smb_fname, /* fname */
7331 DELETE_ACCESS, /* access_mask */
7332 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
7334 FILE_OPEN, /* create_disposition*/
7335 create_options, /* create_options */
7336 FILE_FLAG_POSIX_SEMANTICS|0777, /* file_attributes */
7337 0, /* oplock_request */
7338 0, /* allocation_size */
7339 0, /* private_flags */
7345 if (!NT_STATUS_IS_OK(status)) {
7350 * Don't lie to client. If we can't really delete due to
7351 * non-POSIX opens return SHARING_VIOLATION.
7354 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
7357 DEBUG(0, ("smb_posix_unlink: Could not get share mode "
7358 "lock for file %s\n", fsp_str_dbg(fsp)));
7359 close_file(req, fsp, NORMAL_CLOSE);
7360 return NT_STATUS_INVALID_PARAMETER;
7364 * See if others still have the file open. If this is the case, then
7365 * don't delete. If all opens are POSIX delete we can set the delete
7366 * on close disposition.
7368 for (i=0; i<lck->num_share_modes; i++) {
7369 struct share_mode_entry *e = &lck->share_modes[i];
7370 if (is_valid_share_mode_entry(e)) {
7371 if (e->flags & SHARE_MODE_FLAG_POSIX_OPEN) {
7374 /* Fail with sharing violation. */
7375 close_file(req, fsp, NORMAL_CLOSE);
7377 return NT_STATUS_SHARING_VIOLATION;
7382 * Set the delete on close.
7384 status = smb_set_file_disposition_info(conn,
7390 if (!NT_STATUS_IS_OK(status)) {
7391 close_file(req, fsp, NORMAL_CLOSE);
7396 return close_file(req, fsp, NORMAL_CLOSE);
7399 NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
7400 struct smb_request *req,
7401 TALLOC_CTX *mem_ctx,
7402 uint16_t info_level,
7404 struct smb_filename *smb_fname,
7405 char **ppdata, int total_data,
7408 char *pdata = *ppdata;
7409 NTSTATUS status = NT_STATUS_OK;
7410 int data_return_size = 0;
7414 if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
7415 return NT_STATUS_INVALID_LEVEL;
7418 if (!CAN_WRITE(conn)) {
7419 /* Allow POSIX opens. The open path will deny
7420 * any non-readonly opens. */
7421 if (info_level != SMB_POSIX_PATH_OPEN) {
7422 return NT_STATUS_DOS(ERRSRV, ERRaccess);
7426 DEBUG(3,("smbd_do_setfilepathinfo: %s (fnum %d) info_level=%d "
7427 "totdata=%d\n", smb_fname_str_dbg(smb_fname),
7428 fsp ? fsp->fnum : -1, info_level, total_data));
7430 switch (info_level) {
7432 case SMB_INFO_STANDARD:
7434 status = smb_set_info_standard(conn,
7442 case SMB_INFO_SET_EA:
7444 status = smb_info_set_ea(conn,
7452 case SMB_SET_FILE_BASIC_INFO:
7453 case SMB_FILE_BASIC_INFORMATION:
7455 status = smb_set_file_basic_info(conn,
7463 case SMB_FILE_ALLOCATION_INFORMATION:
7464 case SMB_SET_FILE_ALLOCATION_INFO:
7466 status = smb_set_file_allocation_info(conn, req,
7474 case SMB_FILE_END_OF_FILE_INFORMATION:
7475 case SMB_SET_FILE_END_OF_FILE_INFO:
7478 * XP/Win7 both fail after the createfile with
7479 * SMB_SET_FILE_END_OF_FILE_INFO but not
7480 * SMB_FILE_END_OF_FILE_INFORMATION (pass-through).
7481 * The level is known here, so pass it down
7485 (info_level == SMB_SET_FILE_END_OF_FILE_INFO);
7487 status = smb_set_file_end_of_file_info(conn, req,
7496 case SMB_FILE_DISPOSITION_INFORMATION:
7497 case SMB_SET_FILE_DISPOSITION_INFO: /* Set delete on close for open file. */
7500 /* JRA - We used to just ignore this on a path ?
7501 * Shouldn't this be invalid level on a pathname
7504 if (tran_call != TRANSACT2_SETFILEINFO) {
7505 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
7508 status = smb_set_file_disposition_info(conn,
7516 case SMB_FILE_POSITION_INFORMATION:
7518 status = smb_file_position_information(conn,
7525 case SMB_FILE_FULL_EA_INFORMATION:
7527 status = smb_set_file_full_ea_info(conn,
7534 /* From tridge Samba4 :
7535 * MODE_INFORMATION in setfileinfo (I have no
7536 * idea what "mode information" on a file is - it takes a value of 0,
7537 * 2, 4 or 6. What could it be?).
7540 case SMB_FILE_MODE_INFORMATION:
7542 status = smb_file_mode_information(conn,
7549 * CIFS UNIX extensions.
7552 case SMB_SET_FILE_UNIX_BASIC:
7554 status = smb_set_file_unix_basic(conn, req,
7562 case SMB_SET_FILE_UNIX_INFO2:
7564 status = smb_set_file_unix_info2(conn, req,
7572 case SMB_SET_FILE_UNIX_LINK:
7575 /* We must have a pathname for this. */
7576 return NT_STATUS_INVALID_LEVEL;
7578 status = smb_set_file_unix_link(conn, req, pdata,
7579 total_data, smb_fname);
7583 case SMB_SET_FILE_UNIX_HLINK:
7586 /* We must have a pathname for this. */
7587 return NT_STATUS_INVALID_LEVEL;
7589 status = smb_set_file_unix_hlink(conn, req,
7595 case SMB_FILE_RENAME_INFORMATION:
7597 status = smb_file_rename_information(conn, req,
7603 case SMB2_FILE_RENAME_INFORMATION_INTERNAL:
7605 /* SMB2 rename information. */
7606 status = smb2_file_rename_information(conn, req,
7612 #if defined(HAVE_POSIX_ACLS)
7613 case SMB_SET_POSIX_ACL:
7615 status = smb_set_posix_acl(conn,
7624 case SMB_SET_POSIX_LOCK:
7627 return NT_STATUS_INVALID_LEVEL;
7629 status = smb_set_posix_lock(conn, req,
7630 pdata, total_data, fsp);
7634 case SMB_POSIX_PATH_OPEN:
7637 /* We must have a pathname for this. */
7638 return NT_STATUS_INVALID_LEVEL;
7641 status = smb_posix_open(conn, req,
7649 case SMB_POSIX_PATH_UNLINK:
7652 /* We must have a pathname for this. */
7653 return NT_STATUS_INVALID_LEVEL;
7656 status = smb_posix_unlink(conn, req,
7664 return NT_STATUS_INVALID_LEVEL;
7667 if (!NT_STATUS_IS_OK(status)) {
7671 *ret_data_size = data_return_size;
7672 return NT_STATUS_OK;
7675 /****************************************************************************
7676 Reply to a TRANS2_SETFILEINFO (set file info by fileid or pathname).
7677 ****************************************************************************/
7679 static void call_trans2setfilepathinfo(connection_struct *conn,
7680 struct smb_request *req,
7681 unsigned int tran_call,
7682 char **pparams, int total_params,
7683 char **ppdata, int total_data,
7684 unsigned int max_data_bytes)
7686 char *params = *pparams;
7687 char *pdata = *ppdata;
7689 struct smb_filename *smb_fname = NULL;
7690 files_struct *fsp = NULL;
7691 NTSTATUS status = NT_STATUS_OK;
7692 int data_return_size = 0;
7695 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7699 if (tran_call == TRANSACT2_SETFILEINFO) {
7700 if (total_params < 4) {
7701 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7705 fsp = file_fsp(req, SVAL(params,0));
7706 /* Basic check for non-null fsp. */
7707 if (!check_fsp_open(conn, req, fsp)) {
7710 info_level = SVAL(params,2);
7712 status = copy_smb_filename(talloc_tos(), fsp->fsp_name,
7714 if (!NT_STATUS_IS_OK(status)) {
7715 reply_nterror(req, status);
7719 if(fsp->is_directory || fsp->fh->fd == -1) {
7721 * This is actually a SETFILEINFO on a directory
7722 * handle (returned from an NT SMB). NT5.0 seems
7723 * to do this call. JRA.
7725 if (INFO_LEVEL_IS_UNIX(info_level)) {
7726 /* Always do lstat for UNIX calls. */
7727 if (SMB_VFS_LSTAT(conn, smb_fname)) {
7728 DEBUG(3,("call_trans2setfilepathinfo: "
7729 "SMB_VFS_LSTAT of %s failed "
7731 smb_fname_str_dbg(smb_fname),
7733 reply_nterror(req, map_nt_error_from_unix(errno));
7737 if (SMB_VFS_STAT(conn, smb_fname) != 0) {
7738 DEBUG(3,("call_trans2setfilepathinfo: "
7739 "fileinfo of %s failed (%s)\n",
7740 smb_fname_str_dbg(smb_fname),
7742 reply_nterror(req, map_nt_error_from_unix(errno));
7746 } else if (fsp->print_file) {
7748 * Doing a DELETE_ON_CLOSE should cancel a print job.
7750 if ((info_level == SMB_SET_FILE_DISPOSITION_INFO) && CVAL(pdata,0)) {
7751 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
7753 DEBUG(3,("call_trans2setfilepathinfo: "
7754 "Cancelling print job (%s)\n",
7758 send_trans2_replies(conn, req, params, 2,
7764 NT_STATUS_OBJECT_PATH_NOT_FOUND);
7769 * Original code - this is an open file.
7771 if (!check_fsp(conn, req, fsp)) {
7775 if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
7776 DEBUG(3,("call_trans2setfilepathinfo: fstat "
7777 "of fnum %d failed (%s)\n", fsp->fnum,
7779 reply_nterror(req, map_nt_error_from_unix(errno));
7787 if (total_params < 7) {
7788 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7792 info_level = SVAL(params,0);
7793 srvstr_get_path(req, params, req->flags2, &fname, ¶ms[6],
7794 total_params - 6, STR_TERMINATE,
7796 if (!NT_STATUS_IS_OK(status)) {
7797 reply_nterror(req, status);
7801 status = filename_convert(req, conn,
7802 req->flags2 & FLAGS2_DFS_PATHNAMES,
7807 if (!NT_STATUS_IS_OK(status)) {
7808 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
7809 reply_botherror(req,
7810 NT_STATUS_PATH_NOT_COVERED,
7811 ERRSRV, ERRbadpath);
7814 reply_nterror(req, status);
7818 if (INFO_LEVEL_IS_UNIX(info_level)) {
7820 * For CIFS UNIX extensions the target name may not exist.
7823 /* Always do lstat for UNIX calls. */
7824 SMB_VFS_LSTAT(conn, smb_fname);
7826 } else if (!VALID_STAT(smb_fname->st) &&
7827 SMB_VFS_STAT(conn, smb_fname)) {
7828 DEBUG(3,("call_trans2setfilepathinfo: SMB_VFS_STAT of "
7830 smb_fname_str_dbg(smb_fname),
7832 reply_nterror(req, map_nt_error_from_unix(errno));
7837 DEBUG(3,("call_trans2setfilepathinfo(%d) %s (fnum %d) info_level=%d "
7838 "totdata=%d\n", tran_call, smb_fname_str_dbg(smb_fname),
7839 fsp ? fsp->fnum : -1, info_level,total_data));
7841 /* Realloc the parameter size */
7842 *pparams = (char *)SMB_REALLOC(*pparams,2);
7843 if (*pparams == NULL) {
7844 reply_nterror(req, NT_STATUS_NO_MEMORY);
7851 status = smbd_do_setfilepathinfo(conn, req, req,
7857 if (!NT_STATUS_IS_OK(status)) {
7858 if (open_was_deferred(req->mid)) {
7859 /* We have re-scheduled this call. */
7862 if (blocking_lock_was_deferred_smb1(req->mid)) {
7863 /* We have re-scheduled this call. */
7866 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
7867 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
7868 ERRSRV, ERRbadpath);
7871 if (info_level == SMB_POSIX_PATH_OPEN) {
7872 reply_openerror(req, status);
7876 reply_nterror(req, status);
7880 send_trans2_replies(conn, req, params, 2, *ppdata, data_return_size,
7886 /****************************************************************************
7887 Reply to a TRANS2_MKDIR (make directory with extended attributes).
7888 ****************************************************************************/
7890 static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
7891 char **pparams, int total_params,
7892 char **ppdata, int total_data,
7893 unsigned int max_data_bytes)
7895 struct smb_filename *smb_dname = NULL;
7896 char *params = *pparams;
7897 char *pdata = *ppdata;
7898 char *directory = NULL;
7899 NTSTATUS status = NT_STATUS_OK;
7900 struct ea_list *ea_list = NULL;
7901 TALLOC_CTX *ctx = talloc_tos();
7903 if (!CAN_WRITE(conn)) {
7904 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
7908 if (total_params < 5) {
7909 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7913 srvstr_get_path(ctx, params, req->flags2, &directory, ¶ms[4],
7914 total_params - 4, STR_TERMINATE,
7916 if (!NT_STATUS_IS_OK(status)) {
7917 reply_nterror(req, status);
7921 DEBUG(3,("call_trans2mkdir : name = %s\n", directory));
7923 status = filename_convert(ctx,
7925 req->flags2 & FLAGS2_DFS_PATHNAMES,
7931 if (!NT_STATUS_IS_OK(status)) {
7932 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
7933 reply_botherror(req,
7934 NT_STATUS_PATH_NOT_COVERED,
7935 ERRSRV, ERRbadpath);
7938 reply_nterror(req, status);
7943 * OS/2 workplace shell seems to send SET_EA requests of "null"
7944 * length (4 bytes containing IVAL 4).
7945 * They seem to have no effect. Bug #3212. JRA.
7948 if (total_data && (total_data != 4)) {
7949 /* Any data in this call is an EA list. */
7950 if (total_data < 10) {
7951 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7955 if (IVAL(pdata,0) > total_data) {
7956 DEBUG(10,("call_trans2mkdir: bad total data size (%u) > %u\n",
7957 IVAL(pdata,0), (unsigned int)total_data));
7958 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7962 ea_list = read_ea_list(talloc_tos(), pdata + 4,
7965 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7969 if (!lp_ea_support(SNUM(conn))) {
7970 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
7974 /* If total_data == 4 Windows doesn't care what values
7975 * are placed in that field, it just ignores them.
7976 * The System i QNTC IBM SMB client puts bad values here,
7977 * so ignore them. */
7979 status = create_directory(conn, req, smb_dname);
7981 if (!NT_STATUS_IS_OK(status)) {
7982 reply_nterror(req, status);
7986 /* Try and set any given EA. */
7988 status = set_ea(conn, NULL, smb_dname, ea_list);
7989 if (!NT_STATUS_IS_OK(status)) {
7990 reply_nterror(req, status);
7995 /* Realloc the parameter and data sizes */
7996 *pparams = (char *)SMB_REALLOC(*pparams,2);
7997 if(*pparams == NULL) {
7998 reply_nterror(req, NT_STATUS_NO_MEMORY);
8005 send_trans2_replies(conn, req, params, 2, *ppdata, 0, max_data_bytes);
8008 TALLOC_FREE(smb_dname);
8012 /****************************************************************************
8013 Reply to a TRANS2_FINDNOTIFYFIRST (start monitoring a directory for changes).
8014 We don't actually do this - we just send a null response.
8015 ****************************************************************************/
8017 static void call_trans2findnotifyfirst(connection_struct *conn,
8018 struct smb_request *req,
8019 char **pparams, int total_params,
8020 char **ppdata, int total_data,
8021 unsigned int max_data_bytes)
8023 char *params = *pparams;
8026 if (total_params < 6) {
8027 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8031 info_level = SVAL(params,4);
8032 DEBUG(3,("call_trans2findnotifyfirst - info_level %d\n", info_level));
8034 switch (info_level) {
8039 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
8043 /* Realloc the parameter and data sizes */
8044 *pparams = (char *)SMB_REALLOC(*pparams,6);
8045 if (*pparams == NULL) {
8046 reply_nterror(req, NT_STATUS_NO_MEMORY);
8051 SSVAL(params,0,fnf_handle);
8052 SSVAL(params,2,0); /* No changes */
8053 SSVAL(params,4,0); /* No EA errors */
8060 send_trans2_replies(conn, req, params, 6, *ppdata, 0, max_data_bytes);
8065 /****************************************************************************
8066 Reply to a TRANS2_FINDNOTIFYNEXT (continue monitoring a directory for
8067 changes). Currently this does nothing.
8068 ****************************************************************************/
8070 static void call_trans2findnotifynext(connection_struct *conn,
8071 struct smb_request *req,
8072 char **pparams, int total_params,
8073 char **ppdata, int total_data,
8074 unsigned int max_data_bytes)
8076 char *params = *pparams;
8078 DEBUG(3,("call_trans2findnotifynext\n"));
8080 /* Realloc the parameter and data sizes */
8081 *pparams = (char *)SMB_REALLOC(*pparams,4);
8082 if (*pparams == NULL) {
8083 reply_nterror(req, NT_STATUS_NO_MEMORY);
8088 SSVAL(params,0,0); /* No changes */
8089 SSVAL(params,2,0); /* No EA errors */
8091 send_trans2_replies(conn, req, params, 4, *ppdata, 0, max_data_bytes);
8096 /****************************************************************************
8097 Reply to a TRANS2_GET_DFS_REFERRAL - Shirish Kalele <kalele@veritas.com>.
8098 ****************************************************************************/
8100 static void call_trans2getdfsreferral(connection_struct *conn,
8101 struct smb_request *req,
8102 char **pparams, int total_params,
8103 char **ppdata, int total_data,
8104 unsigned int max_data_bytes)
8106 char *params = *pparams;
8107 char *pathname = NULL;
8109 int max_referral_level;
8110 NTSTATUS status = NT_STATUS_OK;
8111 TALLOC_CTX *ctx = talloc_tos();
8113 DEBUG(10,("call_trans2getdfsreferral\n"));
8115 if (total_params < 3) {
8116 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8120 max_referral_level = SVAL(params,0);
8122 if(!lp_host_msdfs()) {
8123 reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
8127 srvstr_pull_talloc(ctx, params, req->flags2, &pathname, ¶ms[2],
8128 total_params - 2, STR_TERMINATE);
8130 reply_nterror(req, NT_STATUS_NOT_FOUND);
8133 if((reply_size = setup_dfs_referral(conn, pathname, max_referral_level,
8134 ppdata,&status)) < 0) {
8135 reply_nterror(req, status);
8139 SSVAL(req->inbuf, smb_flg2,
8140 SVAL(req->inbuf,smb_flg2) | FLAGS2_DFS_PATHNAMES);
8141 send_trans2_replies(conn, req,0,0,*ppdata,reply_size, max_data_bytes);
8146 #define LMCAT_SPL 0x53
8147 #define LMFUNC_GETJOBID 0x60
8149 /****************************************************************************
8150 Reply to a TRANS2_IOCTL - used for OS/2 printing.
8151 ****************************************************************************/
8153 static void call_trans2ioctl(connection_struct *conn,
8154 struct smb_request *req,
8155 char **pparams, int total_params,
8156 char **ppdata, int total_data,
8157 unsigned int max_data_bytes)
8159 char *pdata = *ppdata;
8160 files_struct *fsp = file_fsp(req, SVAL(req->vwv+15, 0));
8162 /* check for an invalid fid before proceeding */
8165 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
8169 if ((SVAL(req->vwv+16, 0) == LMCAT_SPL)
8170 && (SVAL(req->vwv+17, 0) == LMFUNC_GETJOBID)) {
8171 *ppdata = (char *)SMB_REALLOC(*ppdata, 32);
8172 if (*ppdata == NULL) {
8173 reply_nterror(req, NT_STATUS_NO_MEMORY);
8178 /* NOTE - THIS IS ASCII ONLY AT THE MOMENT - NOT SURE IF OS/2
8179 CAN ACCEPT THIS IN UNICODE. JRA. */
8182 if (fsp->print_file) {
8183 SSVAL(pdata, 0, fsp->print_file->rap_jobid);
8187 srvstr_push(pdata, req->flags2, pdata + 2,
8188 global_myname(), 15,
8189 STR_ASCII|STR_TERMINATE); /* Our NetBIOS name */
8190 srvstr_push(pdata, req->flags2, pdata+18,
8191 lp_servicename(SNUM(conn)), 13,
8192 STR_ASCII|STR_TERMINATE); /* Service name */
8193 send_trans2_replies(conn, req, *pparams, 0, *ppdata, 32,
8198 DEBUG(2,("Unknown TRANS2_IOCTL\n"));
8199 reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
8202 /****************************************************************************
8203 Reply to a SMBfindclose (stop trans2 directory search).
8204 ****************************************************************************/
8206 void reply_findclose(struct smb_request *req)
8209 struct smbd_server_connection *sconn = smbd_server_conn;
8211 START_PROFILE(SMBfindclose);
8214 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8215 END_PROFILE(SMBfindclose);
8219 dptr_num = SVALS(req->vwv+0, 0);
8221 DEBUG(3,("reply_findclose, dptr_num = %d\n", dptr_num));
8223 dptr_close(sconn, &dptr_num);
8225 reply_outbuf(req, 0, 0);
8227 DEBUG(3,("SMBfindclose dptr_num = %d\n", dptr_num));
8229 END_PROFILE(SMBfindclose);
8233 /****************************************************************************
8234 Reply to a SMBfindnclose (stop FINDNOTIFYFIRST directory search).
8235 ****************************************************************************/
8237 void reply_findnclose(struct smb_request *req)
8241 START_PROFILE(SMBfindnclose);
8244 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8245 END_PROFILE(SMBfindnclose);
8249 dptr_num = SVAL(req->vwv+0, 0);
8251 DEBUG(3,("reply_findnclose, dptr_num = %d\n", dptr_num));
8253 /* We never give out valid handles for a
8254 findnotifyfirst - so any dptr_num is ok here.
8257 reply_outbuf(req, 0, 0);
8259 DEBUG(3,("SMB_findnclose dptr_num = %d\n", dptr_num));
8261 END_PROFILE(SMBfindnclose);
8265 static void handle_trans2(connection_struct *conn, struct smb_request *req,
8266 struct trans_state *state)
8268 if (get_Protocol() >= PROTOCOL_NT1) {
8269 req->flags2 |= 0x40; /* IS_LONG_NAME */
8270 SSVAL(req->inbuf,smb_flg2,req->flags2);
8273 if (conn->encrypt_level == Required && !req->encrypted) {
8274 if (state->call != TRANSACT2_QFSINFO &&
8275 state->call != TRANSACT2_SETFSINFO) {
8276 DEBUG(0,("handle_trans2: encryption required "
8278 (unsigned int)state->call));
8279 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
8284 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
8286 /* Now we must call the relevant TRANS2 function */
8287 switch(state->call) {
8288 case TRANSACT2_OPEN:
8290 START_PROFILE(Trans2_open);
8291 call_trans2open(conn, req,
8292 &state->param, state->total_param,
8293 &state->data, state->total_data,
8294 state->max_data_return);
8295 END_PROFILE(Trans2_open);
8299 case TRANSACT2_FINDFIRST:
8301 START_PROFILE(Trans2_findfirst);
8302 call_trans2findfirst(conn, req,
8303 &state->param, state->total_param,
8304 &state->data, state->total_data,
8305 state->max_data_return);
8306 END_PROFILE(Trans2_findfirst);
8310 case TRANSACT2_FINDNEXT:
8312 START_PROFILE(Trans2_findnext);
8313 call_trans2findnext(conn, req,
8314 &state->param, state->total_param,
8315 &state->data, state->total_data,
8316 state->max_data_return);
8317 END_PROFILE(Trans2_findnext);
8321 case TRANSACT2_QFSINFO:
8323 START_PROFILE(Trans2_qfsinfo);
8324 call_trans2qfsinfo(conn, req,
8325 &state->param, state->total_param,
8326 &state->data, state->total_data,
8327 state->max_data_return);
8328 END_PROFILE(Trans2_qfsinfo);
8332 case TRANSACT2_SETFSINFO:
8334 START_PROFILE(Trans2_setfsinfo);
8335 call_trans2setfsinfo(conn, req,
8336 &state->param, state->total_param,
8337 &state->data, state->total_data,
8338 state->max_data_return);
8339 END_PROFILE(Trans2_setfsinfo);
8343 case TRANSACT2_QPATHINFO:
8344 case TRANSACT2_QFILEINFO:
8346 START_PROFILE(Trans2_qpathinfo);
8347 call_trans2qfilepathinfo(conn, req, state->call,
8348 &state->param, state->total_param,
8349 &state->data, state->total_data,
8350 state->max_data_return);
8351 END_PROFILE(Trans2_qpathinfo);
8355 case TRANSACT2_SETPATHINFO:
8356 case TRANSACT2_SETFILEINFO:
8358 START_PROFILE(Trans2_setpathinfo);
8359 call_trans2setfilepathinfo(conn, req, state->call,
8360 &state->param, state->total_param,
8361 &state->data, state->total_data,
8362 state->max_data_return);
8363 END_PROFILE(Trans2_setpathinfo);
8367 case TRANSACT2_FINDNOTIFYFIRST:
8369 START_PROFILE(Trans2_findnotifyfirst);
8370 call_trans2findnotifyfirst(conn, req,
8371 &state->param, state->total_param,
8372 &state->data, state->total_data,
8373 state->max_data_return);
8374 END_PROFILE(Trans2_findnotifyfirst);
8378 case TRANSACT2_FINDNOTIFYNEXT:
8380 START_PROFILE(Trans2_findnotifynext);
8381 call_trans2findnotifynext(conn, req,
8382 &state->param, state->total_param,
8383 &state->data, state->total_data,
8384 state->max_data_return);
8385 END_PROFILE(Trans2_findnotifynext);
8389 case TRANSACT2_MKDIR:
8391 START_PROFILE(Trans2_mkdir);
8392 call_trans2mkdir(conn, req,
8393 &state->param, state->total_param,
8394 &state->data, state->total_data,
8395 state->max_data_return);
8396 END_PROFILE(Trans2_mkdir);
8400 case TRANSACT2_GET_DFS_REFERRAL:
8402 START_PROFILE(Trans2_get_dfs_referral);
8403 call_trans2getdfsreferral(conn, req,
8404 &state->param, state->total_param,
8405 &state->data, state->total_data,
8406 state->max_data_return);
8407 END_PROFILE(Trans2_get_dfs_referral);
8411 case TRANSACT2_IOCTL:
8413 START_PROFILE(Trans2_ioctl);
8414 call_trans2ioctl(conn, req,
8415 &state->param, state->total_param,
8416 &state->data, state->total_data,
8417 state->max_data_return);
8418 END_PROFILE(Trans2_ioctl);
8423 /* Error in request */
8424 DEBUG(2,("Unknown request %d in trans2 call\n", state->call));
8425 reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
8429 /****************************************************************************
8430 Reply to a SMBtrans2.
8431 ****************************************************************************/
8433 void reply_trans2(struct smb_request *req)
8435 connection_struct *conn = req->conn;
8440 unsigned int tran_call;
8441 struct trans_state *state;
8444 START_PROFILE(SMBtrans2);
8446 if (req->wct < 14) {
8447 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8448 END_PROFILE(SMBtrans2);
8452 dsoff = SVAL(req->vwv+12, 0);
8453 dscnt = SVAL(req->vwv+11, 0);
8454 psoff = SVAL(req->vwv+10, 0);
8455 pscnt = SVAL(req->vwv+9, 0);
8456 tran_call = SVAL(req->vwv+14, 0);
8458 result = allow_new_trans(conn->pending_trans, req->mid);
8459 if (!NT_STATUS_IS_OK(result)) {
8460 DEBUG(2, ("Got invalid trans2 request: %s\n",
8461 nt_errstr(result)));
8462 reply_nterror(req, result);
8463 END_PROFILE(SMBtrans2);
8468 switch (tran_call) {
8469 /* List the allowed trans2 calls on IPC$ */
8470 case TRANSACT2_OPEN:
8471 case TRANSACT2_GET_DFS_REFERRAL:
8472 case TRANSACT2_QFILEINFO:
8473 case TRANSACT2_QFSINFO:
8474 case TRANSACT2_SETFSINFO:
8477 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
8478 END_PROFILE(SMBtrans2);
8483 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
8484 DEBUG(0, ("talloc failed\n"));
8485 reply_nterror(req, NT_STATUS_NO_MEMORY);
8486 END_PROFILE(SMBtrans2);
8490 state->cmd = SMBtrans2;
8492 state->mid = req->mid;
8493 state->vuid = req->vuid;
8494 state->setup_count = SVAL(req->vwv+13, 0);
8495 state->setup = NULL;
8496 state->total_param = SVAL(req->vwv+0, 0);
8497 state->param = NULL;
8498 state->total_data = SVAL(req->vwv+1, 0);
8500 state->max_param_return = SVAL(req->vwv+2, 0);
8501 state->max_data_return = SVAL(req->vwv+3, 0);
8502 state->max_setup_return = SVAL(req->vwv+4, 0);
8503 state->close_on_completion = BITSETW(req->vwv+5, 0);
8504 state->one_way = BITSETW(req->vwv+5, 1);
8506 state->call = tran_call;
8508 /* All trans2 messages we handle have smb_sucnt == 1 - ensure this
8509 is so as a sanity check */
8510 if (state->setup_count != 1) {
8512 * Need to have rc=0 for ioctl to get job id for OS/2.
8513 * Network printing will fail if function is not successful.
8514 * Similar function in reply.c will be used if protocol
8515 * is LANMAN1.0 instead of LM1.2X002.
8516 * Until DosPrintSetJobInfo with PRJINFO3 is supported,
8517 * outbuf doesn't have to be set(only job id is used).
8519 if ( (state->setup_count == 4)
8520 && (tran_call == TRANSACT2_IOCTL)
8521 && (SVAL(req->vwv+16, 0) == LMCAT_SPL)
8522 && (SVAL(req->vwv+17, 0) == LMFUNC_GETJOBID)) {
8523 DEBUG(2,("Got Trans2 DevIOctl jobid\n"));
8525 DEBUG(2,("Invalid smb_sucnt in trans2 call(%u)\n",state->setup_count));
8526 DEBUG(2,("Transaction is %d\n",tran_call));
8528 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8529 END_PROFILE(SMBtrans2);
8534 if ((dscnt > state->total_data) || (pscnt > state->total_param))
8537 if (state->total_data) {
8539 if (trans_oob(state->total_data, 0, dscnt)
8540 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
8544 /* Can't use talloc here, the core routines do realloc on the
8545 * params and data. */
8546 state->data = (char *)SMB_MALLOC(state->total_data);
8547 if (state->data == NULL) {
8548 DEBUG(0,("reply_trans2: data malloc fail for %u "
8549 "bytes !\n", (unsigned int)state->total_data));
8551 reply_nterror(req, NT_STATUS_NO_MEMORY);
8552 END_PROFILE(SMBtrans2);
8556 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
8559 if (state->total_param) {
8561 if (trans_oob(state->total_param, 0, pscnt)
8562 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
8566 /* Can't use talloc here, the core routines do realloc on the
8567 * params and data. */
8568 state->param = (char *)SMB_MALLOC(state->total_param);
8569 if (state->param == NULL) {
8570 DEBUG(0,("reply_trans: param malloc fail for %u "
8571 "bytes !\n", (unsigned int)state->total_param));
8572 SAFE_FREE(state->data);
8574 reply_nterror(req, NT_STATUS_NO_MEMORY);
8575 END_PROFILE(SMBtrans2);
8579 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
8582 state->received_data = dscnt;
8583 state->received_param = pscnt;
8585 if ((state->received_param == state->total_param) &&
8586 (state->received_data == state->total_data)) {
8588 handle_trans2(conn, req, state);
8590 SAFE_FREE(state->data);
8591 SAFE_FREE(state->param);
8593 END_PROFILE(SMBtrans2);
8597 DLIST_ADD(conn->pending_trans, state);
8599 /* We need to send an interim response then receive the rest
8600 of the parameter/data bytes */
8601 reply_outbuf(req, 0, 0);
8602 show_msg((char *)req->outbuf);
8603 END_PROFILE(SMBtrans2);
8608 DEBUG(0,("reply_trans2: invalid trans parameters\n"));
8609 SAFE_FREE(state->data);
8610 SAFE_FREE(state->param);
8612 END_PROFILE(SMBtrans2);
8613 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8617 /****************************************************************************
8618 Reply to a SMBtranss2
8619 ****************************************************************************/
8621 void reply_transs2(struct smb_request *req)
8623 connection_struct *conn = req->conn;
8624 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
8625 struct trans_state *state;
8627 START_PROFILE(SMBtranss2);
8629 show_msg((char *)req->inbuf);
8632 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8633 END_PROFILE(SMBtranss2);
8637 for (state = conn->pending_trans; state != NULL;
8638 state = state->next) {
8639 if (state->mid == req->mid) {
8644 if ((state == NULL) || (state->cmd != SMBtrans2)) {
8645 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8646 END_PROFILE(SMBtranss2);
8650 /* Revise state->total_param and state->total_data in case they have
8651 changed downwards */
8653 if (SVAL(req->vwv+0, 0) < state->total_param)
8654 state->total_param = SVAL(req->vwv+0, 0);
8655 if (SVAL(req->vwv+1, 0) < state->total_data)
8656 state->total_data = SVAL(req->vwv+1, 0);
8658 pcnt = SVAL(req->vwv+2, 0);
8659 poff = SVAL(req->vwv+3, 0);
8660 pdisp = SVAL(req->vwv+4, 0);
8662 dcnt = SVAL(req->vwv+5, 0);
8663 doff = SVAL(req->vwv+6, 0);
8664 ddisp = SVAL(req->vwv+7, 0);
8666 state->received_param += pcnt;
8667 state->received_data += dcnt;
8669 if ((state->received_data > state->total_data) ||
8670 (state->received_param > state->total_param))
8674 if (trans_oob(state->total_param, pdisp, pcnt)
8675 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
8678 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
8682 if (trans_oob(state->total_data, ddisp, dcnt)
8683 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
8686 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
8689 if ((state->received_param < state->total_param) ||
8690 (state->received_data < state->total_data)) {
8691 END_PROFILE(SMBtranss2);
8695 handle_trans2(conn, req, state);
8697 DLIST_REMOVE(conn->pending_trans, state);
8698 SAFE_FREE(state->data);
8699 SAFE_FREE(state->param);
8702 END_PROFILE(SMBtranss2);
8707 DEBUG(0,("reply_transs2: invalid trans parameters\n"));
8708 DLIST_REMOVE(conn->pending_trans, state);
8709 SAFE_FREE(state->data);
8710 SAFE_FREE(state->param);
8712 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8713 END_PROFILE(SMBtranss2);