s3:smbd: change trans2.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / smbd / trans2.c
bloba093575c03db0e559e5eeaa159913ec636005a2e
1 /*
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/>.
26 #include "includes.h"
27 #include "system/filesys.h"
28 #include "version.h"
29 #include "smbd/smbd.h"
30 #include "smbd/globals.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../librpc/gen_ndr/xattr.h"
33 #include "../librpc/gen_ndr/ndr_security.h"
34 #include "../librpc/gen_ndr/open_files.h"
35 #include "libcli/security/security.h"
36 #include "trans2.h"
37 #include "auth.h"
38 #include "smbprofile.h"
39 #include "rpc_server/srv_pipe_hnd.h"
40 #include "printing.h"
42 #define DIR_ENTRY_SAFETY_MARGIN 4096
44 static char *store_file_unix_basic(connection_struct *conn,
45 char *pdata,
46 files_struct *fsp,
47 const SMB_STRUCT_STAT *psbuf);
49 static char *store_file_unix_basic_info2(connection_struct *conn,
50 char *pdata,
51 files_struct *fsp,
52 const SMB_STRUCT_STAT *psbuf);
54 /********************************************************************
55 The canonical "check access" based on object handle or path function.
56 ********************************************************************/
58 NTSTATUS check_access(connection_struct *conn,
59 files_struct *fsp,
60 const struct smb_filename *smb_fname,
61 uint32_t access_mask)
63 if (fsp) {
64 if (!(fsp->access_mask & access_mask)) {
65 return NT_STATUS_ACCESS_DENIED;
67 } else {
68 NTSTATUS status = smbd_check_access_rights(conn,
69 smb_fname,
70 access_mask);
71 if (!NT_STATUS_IS_OK(status)) {
72 return status;
75 return NT_STATUS_OK;
78 /********************************************************************
79 Roundup a value to the nearest allocation roundup size boundary.
80 Only do this for Windows clients.
81 ********************************************************************/
83 uint64_t smb_roundup(connection_struct *conn, uint64_t val)
85 uint64_t rval = lp_allocation_roundup_size(SNUM(conn));
87 /* Only roundup for Windows clients. */
88 enum remote_arch_types ra_type = get_remote_arch();
89 if (rval && (ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
90 val = SMB_ROUNDUP(val,rval);
92 return val;
95 /********************************************************************
96 Create a 64 bit FileIndex. If the file is on the same device as
97 the root of the share, just return the 64-bit inode. If it isn't,
98 mangle as we used to do.
99 ********************************************************************/
101 uint64_t get_FileIndex(connection_struct *conn, const SMB_STRUCT_STAT *psbuf)
103 uint64_t file_index;
104 if (conn->base_share_dev == psbuf->st_ex_dev) {
105 return (uint64_t)psbuf->st_ex_ino;
107 file_index = ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
108 file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
109 return file_index;
112 /****************************************************************************
113 Utility functions for dealing with extended attributes.
114 ****************************************************************************/
116 /****************************************************************************
117 Refuse to allow clients to overwrite our private xattrs.
118 ****************************************************************************/
120 static bool samba_private_attr_name(const char *unix_ea_name)
122 static const char * const prohibited_ea_names[] = {
123 SAMBA_POSIX_INHERITANCE_EA_NAME,
124 SAMBA_XATTR_DOS_ATTRIB,
125 SAMBA_XATTR_MARKER,
126 XATTR_NTACL_NAME,
127 NULL
130 int i;
132 for (i = 0; prohibited_ea_names[i]; i++) {
133 if (strequal( prohibited_ea_names[i], unix_ea_name))
134 return true;
136 if (strncasecmp_m(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
137 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) == 0) {
138 return true;
140 return false;
143 /****************************************************************************
144 Get one EA value. Fill in a struct ea_struct.
145 ****************************************************************************/
147 NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
148 files_struct *fsp, const char *fname,
149 const char *ea_name, struct ea_struct *pea)
151 /* Get the value of this xattr. Max size is 64k. */
152 size_t attr_size = 256;
153 char *val = NULL;
154 ssize_t sizeret;
156 again:
158 val = talloc_realloc(mem_ctx, val, char, attr_size);
159 if (!val) {
160 return NT_STATUS_NO_MEMORY;
163 if (fsp && fsp->fh->fd != -1) {
164 sizeret = SMB_VFS_FGETXATTR(fsp, ea_name, val, attr_size);
165 } else {
166 sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
169 if (sizeret == -1 && errno == ERANGE && attr_size != 65536) {
170 attr_size = 65536;
171 goto again;
174 if (sizeret == -1) {
175 return map_nt_error_from_unix(errno);
178 DEBUG(10,("get_ea_value: EA %s is of length %u\n", ea_name, (unsigned int)sizeret));
179 dump_data(10, (uint8 *)val, sizeret);
181 pea->flags = 0;
182 if (strnequal(ea_name, "user.", 5)) {
183 pea->name = talloc_strdup(mem_ctx, &ea_name[5]);
184 } else {
185 pea->name = talloc_strdup(mem_ctx, ea_name);
187 if (pea->name == NULL) {
188 TALLOC_FREE(val);
189 return NT_STATUS_NO_MEMORY;
191 pea->value.data = (unsigned char *)val;
192 pea->value.length = (size_t)sizeret;
193 return NT_STATUS_OK;
196 NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
197 files_struct *fsp, const char *fname,
198 char ***pnames, size_t *pnum_names)
200 /* Get a list of all xattrs. Max namesize is 64k. */
201 size_t ea_namelist_size = 1024;
202 char *ea_namelist = NULL;
204 char *p;
205 char **names, **tmp;
206 size_t num_names;
207 ssize_t sizeret = -1;
209 if (!lp_ea_support(SNUM(conn))) {
210 if (pnames) {
211 *pnames = NULL;
213 *pnum_names = 0;
214 return NT_STATUS_OK;
218 * TALLOC the result early to get the talloc hierarchy right.
221 names = talloc_array(mem_ctx, char *, 1);
222 if (names == NULL) {
223 DEBUG(0, ("talloc failed\n"));
224 return NT_STATUS_NO_MEMORY;
227 while (ea_namelist_size <= 65536) {
229 ea_namelist = talloc_realloc(
230 names, ea_namelist, char, ea_namelist_size);
231 if (ea_namelist == NULL) {
232 DEBUG(0, ("talloc failed\n"));
233 TALLOC_FREE(names);
234 return NT_STATUS_NO_MEMORY;
237 if (fsp && fsp->fh->fd != -1) {
238 sizeret = SMB_VFS_FLISTXATTR(fsp, ea_namelist,
239 ea_namelist_size);
240 } else {
241 sizeret = SMB_VFS_LISTXATTR(conn, fname, ea_namelist,
242 ea_namelist_size);
245 if ((sizeret == -1) && (errno == ERANGE)) {
246 ea_namelist_size *= 2;
248 else {
249 break;
253 if (sizeret == -1) {
254 TALLOC_FREE(names);
255 return map_nt_error_from_unix(errno);
258 DEBUG(10, ("get_ea_list_from_file: ea_namelist size = %u\n",
259 (unsigned int)sizeret));
261 if (sizeret == 0) {
262 TALLOC_FREE(names);
263 if (pnames) {
264 *pnames = NULL;
266 *pnum_names = 0;
267 return NT_STATUS_OK;
271 * Ensure the result is 0-terminated
274 if (ea_namelist[sizeret-1] != '\0') {
275 TALLOC_FREE(names);
276 return NT_STATUS_INTERNAL_ERROR;
280 * count the names
282 num_names = 0;
284 for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
285 num_names += 1;
288 tmp = talloc_realloc(mem_ctx, names, char *, num_names);
289 if (tmp == NULL) {
290 DEBUG(0, ("talloc failed\n"));
291 TALLOC_FREE(names);
292 return NT_STATUS_NO_MEMORY;
295 names = tmp;
296 num_names = 0;
298 for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
299 names[num_names++] = p;
302 if (pnames) {
303 *pnames = names;
304 } else {
305 TALLOC_FREE(names);
307 *pnum_names = num_names;
308 return NT_STATUS_OK;
311 /****************************************************************************
312 Return a linked list of the total EA's. Plus the total size
313 ****************************************************************************/
315 static struct ea_list *get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
316 const char *fname, size_t *pea_total_len)
318 /* Get a list of all xattrs. Max namesize is 64k. */
319 size_t i, num_names;
320 char **names;
321 struct ea_list *ea_list_head = NULL;
322 NTSTATUS status;
324 *pea_total_len = 0;
326 if (!lp_ea_support(SNUM(conn))) {
327 return NULL;
330 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
331 &names, &num_names);
333 if (!NT_STATUS_IS_OK(status) || (num_names == 0)) {
334 return NULL;
337 for (i=0; i<num_names; i++) {
338 struct ea_list *listp;
339 fstring dos_ea_name;
341 if (strnequal(names[i], "system.", 7)
342 || samba_private_attr_name(names[i]))
343 continue;
345 listp = talloc(mem_ctx, struct ea_list);
346 if (listp == NULL) {
347 return NULL;
350 if (!NT_STATUS_IS_OK(get_ea_value(mem_ctx, conn, fsp,
351 fname, names[i],
352 &listp->ea))) {
353 return NULL;
356 push_ascii_fstring(dos_ea_name, listp->ea.name);
358 *pea_total_len +=
359 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
361 DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len "
362 "= %u\n", (unsigned int)*pea_total_len, dos_ea_name,
363 (unsigned int)listp->ea.value.length));
365 DLIST_ADD_END(ea_list_head, listp, struct ea_list *);
369 /* Add on 4 for total length. */
370 if (*pea_total_len) {
371 *pea_total_len += 4;
374 DEBUG(10, ("get_ea_list_from_file: total_len = %u\n",
375 (unsigned int)*pea_total_len));
377 return ea_list_head;
380 /****************************************************************************
381 Fill a qfilepathinfo buffer with EA's. Returns the length of the buffer
382 that was filled.
383 ****************************************************************************/
385 static unsigned int fill_ea_buffer(TALLOC_CTX *mem_ctx, char *pdata, unsigned int total_data_size,
386 connection_struct *conn, struct ea_list *ea_list)
388 unsigned int ret_data_size = 4;
389 char *p = pdata;
391 SMB_ASSERT(total_data_size >= 4);
393 if (!lp_ea_support(SNUM(conn))) {
394 SIVAL(pdata,4,0);
395 return 4;
398 for (p = pdata + 4; ea_list; ea_list = ea_list->next) {
399 size_t dos_namelen;
400 fstring dos_ea_name;
401 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
402 dos_namelen = strlen(dos_ea_name);
403 if (dos_namelen > 255 || dos_namelen == 0) {
404 break;
406 if (ea_list->ea.value.length > 65535) {
407 break;
409 if (4 + dos_namelen + 1 + ea_list->ea.value.length > total_data_size) {
410 break;
413 /* We know we have room. */
414 SCVAL(p,0,ea_list->ea.flags);
415 SCVAL(p,1,dos_namelen);
416 SSVAL(p,2,ea_list->ea.value.length);
417 strlcpy(p+4, dos_ea_name, dos_namelen+1);
418 memcpy( p + 4 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
420 total_data_size -= 4 + dos_namelen + 1 + ea_list->ea.value.length;
421 p += 4 + dos_namelen + 1 + ea_list->ea.value.length;
424 ret_data_size = PTR_DIFF(p, pdata);
425 DEBUG(10,("fill_ea_buffer: data_size = %u\n", ret_data_size ));
426 SIVAL(pdata,0,ret_data_size);
427 return ret_data_size;
430 static NTSTATUS fill_ea_chained_buffer(TALLOC_CTX *mem_ctx,
431 char *pdata,
432 unsigned int total_data_size,
433 unsigned int *ret_data_size,
434 connection_struct *conn,
435 struct ea_list *ea_list)
437 uint8_t *p = (uint8_t *)pdata;
438 uint8_t *last_start = NULL;
440 *ret_data_size = 0;
442 if (!lp_ea_support(SNUM(conn))) {
443 return NT_STATUS_NO_EAS_ON_FILE;
446 for (; ea_list; ea_list = ea_list->next) {
447 size_t dos_namelen;
448 fstring dos_ea_name;
449 size_t this_size;
451 if (last_start) {
452 SIVAL(last_start, 0, PTR_DIFF(p, last_start));
454 last_start = p;
456 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
457 dos_namelen = strlen(dos_ea_name);
458 if (dos_namelen > 255 || dos_namelen == 0) {
459 return NT_STATUS_INTERNAL_ERROR;
461 if (ea_list->ea.value.length > 65535) {
462 return NT_STATUS_INTERNAL_ERROR;
465 this_size = 0x08 + dos_namelen + 1 + ea_list->ea.value.length;
467 if (ea_list->next) {
468 size_t pad = 4 - (this_size % 4);
469 this_size += pad;
472 if (this_size > total_data_size) {
473 return NT_STATUS_INFO_LENGTH_MISMATCH;
476 /* We know we have room. */
477 SIVAL(p, 0x00, 0); /* next offset */
478 SCVAL(p, 0x04, ea_list->ea.flags);
479 SCVAL(p, 0x05, dos_namelen);
480 SSVAL(p, 0x06, ea_list->ea.value.length);
481 strlcpy((char *)(p+0x08), dos_ea_name, dos_namelen+1);
482 memcpy(p + 0x08 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
484 total_data_size -= this_size;
485 p += this_size;
488 *ret_data_size = PTR_DIFF(p, pdata);
489 DEBUG(10,("fill_ea_chained_buffer: data_size = %u\n", *ret_data_size));
490 return NT_STATUS_OK;
493 static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp, const char *fname)
495 size_t total_ea_len = 0;
496 TALLOC_CTX *mem_ctx = NULL;
498 if (!lp_ea_support(SNUM(conn))) {
499 return 0;
501 mem_ctx = talloc_tos();
502 (void)get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
503 return total_ea_len;
506 /****************************************************************************
507 Ensure the EA name is case insensitive by matching any existing EA name.
508 ****************************************************************************/
510 static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, const char *fname, fstring unix_ea_name)
512 size_t total_ea_len;
513 TALLOC_CTX *mem_ctx = talloc_tos();
514 struct ea_list *ea_list = get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
516 for (; ea_list; ea_list = ea_list->next) {
517 if (strequal(&unix_ea_name[5], ea_list->ea.name)) {
518 DEBUG(10,("canonicalize_ea_name: %s -> %s\n",
519 &unix_ea_name[5], ea_list->ea.name));
520 strlcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-5);
521 break;
526 /****************************************************************************
527 Set or delete an extended attribute.
528 ****************************************************************************/
530 NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
531 const struct smb_filename *smb_fname, struct ea_list *ea_list)
533 NTSTATUS status;
534 char *fname = NULL;
536 if (!lp_ea_support(SNUM(conn))) {
537 return NT_STATUS_EAS_NOT_SUPPORTED;
540 status = check_access(conn, fsp, smb_fname, FILE_WRITE_EA);
541 if (!NT_STATUS_IS_OK(status)) {
542 return status;
545 /* For now setting EAs on streams isn't supported. */
546 fname = smb_fname->base_name;
548 for (;ea_list; ea_list = ea_list->next) {
549 int ret;
550 fstring unix_ea_name;
552 fstrcpy(unix_ea_name, "user."); /* All EA's must start with user. */
553 fstrcat(unix_ea_name, ea_list->ea.name);
555 canonicalize_ea_name(conn, fsp, fname, unix_ea_name);
557 DEBUG(10,("set_ea: ea_name %s ealen = %u\n", unix_ea_name, (unsigned int)ea_list->ea.value.length));
559 if (samba_private_attr_name(unix_ea_name)) {
560 DEBUG(10,("set_ea: ea name %s is a private Samba name.\n", unix_ea_name));
561 return NT_STATUS_ACCESS_DENIED;
564 if (ea_list->ea.value.length == 0) {
565 /* Remove the attribute. */
566 if (fsp && (fsp->fh->fd != -1)) {
567 DEBUG(10,("set_ea: deleting ea name %s on "
568 "file %s by file descriptor.\n",
569 unix_ea_name, fsp_str_dbg(fsp)));
570 ret = SMB_VFS_FREMOVEXATTR(fsp, unix_ea_name);
571 } else {
572 DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
573 unix_ea_name, fname));
574 ret = SMB_VFS_REMOVEXATTR(conn, fname, unix_ea_name);
576 #ifdef ENOATTR
577 /* Removing a non existent attribute always succeeds. */
578 if (ret == -1 && errno == ENOATTR) {
579 DEBUG(10,("set_ea: deleting ea name %s didn't exist - succeeding by default.\n",
580 unix_ea_name));
581 ret = 0;
583 #endif
584 } else {
585 if (fsp && (fsp->fh->fd != -1)) {
586 DEBUG(10,("set_ea: setting ea name %s on file "
587 "%s by file descriptor.\n",
588 unix_ea_name, fsp_str_dbg(fsp)));
589 ret = SMB_VFS_FSETXATTR(fsp, unix_ea_name,
590 ea_list->ea.value.data, ea_list->ea.value.length, 0);
591 } else {
592 DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
593 unix_ea_name, fname));
594 ret = SMB_VFS_SETXATTR(conn, fname, unix_ea_name,
595 ea_list->ea.value.data, ea_list->ea.value.length, 0);
599 if (ret == -1) {
600 #ifdef ENOTSUP
601 if (errno == ENOTSUP) {
602 return NT_STATUS_EAS_NOT_SUPPORTED;
604 #endif
605 return map_nt_error_from_unix(errno);
609 return NT_STATUS_OK;
611 /****************************************************************************
612 Read a list of EA names from an incoming data buffer. Create an ea_list with them.
613 ****************************************************************************/
615 static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
617 struct ea_list *ea_list_head = NULL;
618 size_t converted_size, offset = 0;
620 while (offset + 2 < data_size) {
621 struct ea_list *eal = talloc_zero(ctx, struct ea_list);
622 unsigned int namelen = CVAL(pdata,offset);
624 offset++; /* Go past the namelen byte. */
626 /* integer wrap paranioa. */
627 if ((offset + namelen < offset) || (offset + namelen < namelen) ||
628 (offset > data_size) || (namelen > data_size) ||
629 (offset + namelen >= data_size)) {
630 break;
632 /* Ensure the name is null terminated. */
633 if (pdata[offset + namelen] != '\0') {
634 return NULL;
636 if (!pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset],
637 &converted_size)) {
638 DEBUG(0,("read_ea_name_list: pull_ascii_talloc "
639 "failed: %s", strerror(errno)));
641 if (!eal->ea.name) {
642 return NULL;
645 offset += (namelen + 1); /* Go past the name + terminating zero. */
646 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
647 DEBUG(10,("read_ea_name_list: read ea name %s\n", eal->ea.name));
650 return ea_list_head;
653 /****************************************************************************
654 Read one EA list entry from the buffer.
655 ****************************************************************************/
657 struct ea_list *read_ea_list_entry(TALLOC_CTX *ctx, const char *pdata, size_t data_size, size_t *pbytes_used)
659 struct ea_list *eal = talloc_zero(ctx, struct ea_list);
660 uint16 val_len;
661 unsigned int namelen;
662 size_t converted_size;
664 if (!eal) {
665 return NULL;
668 if (data_size < 6) {
669 return NULL;
672 eal->ea.flags = CVAL(pdata,0);
673 namelen = CVAL(pdata,1);
674 val_len = SVAL(pdata,2);
676 if (4 + namelen + 1 + val_len > data_size) {
677 return NULL;
680 /* Ensure the name is null terminated. */
681 if (pdata[namelen + 4] != '\0') {
682 return NULL;
684 if (!pull_ascii_talloc(ctx, &eal->ea.name, pdata + 4, &converted_size)) {
685 DEBUG(0,("read_ea_list_entry: pull_ascii_talloc failed: %s",
686 strerror(errno)));
688 if (!eal->ea.name) {
689 return NULL;
692 eal->ea.value = data_blob_talloc(eal, NULL, (size_t)val_len + 1);
693 if (!eal->ea.value.data) {
694 return NULL;
697 memcpy(eal->ea.value.data, pdata + 4 + namelen + 1, val_len);
699 /* Ensure we're null terminated just in case we print the value. */
700 eal->ea.value.data[val_len] = '\0';
701 /* But don't count the null. */
702 eal->ea.value.length--;
704 if (pbytes_used) {
705 *pbytes_used = 4 + namelen + 1 + val_len;
708 DEBUG(10,("read_ea_list_entry: read ea name %s\n", eal->ea.name));
709 dump_data(10, eal->ea.value.data, eal->ea.value.length);
711 return eal;
714 /****************************************************************************
715 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
716 ****************************************************************************/
718 static struct ea_list *read_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
720 struct ea_list *ea_list_head = NULL;
721 size_t offset = 0;
722 size_t bytes_used = 0;
724 while (offset < data_size) {
725 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset, data_size - offset, &bytes_used);
727 if (!eal) {
728 return NULL;
731 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
732 offset += bytes_used;
735 return ea_list_head;
738 /****************************************************************************
739 Count the total EA size needed.
740 ****************************************************************************/
742 static size_t ea_list_size(struct ea_list *ealist)
744 fstring dos_ea_name;
745 struct ea_list *listp;
746 size_t ret = 0;
748 for (listp = ealist; listp; listp = listp->next) {
749 push_ascii_fstring(dos_ea_name, listp->ea.name);
750 ret += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
752 /* Add on 4 for total length. */
753 if (ret) {
754 ret += 4;
757 return ret;
760 /****************************************************************************
761 Return a union of EA's from a file list and a list of names.
762 The TALLOC context for the two lists *MUST* be identical as we steal
763 memory from one list to add to another. JRA.
764 ****************************************************************************/
766 static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *file_list, size_t *total_ea_len)
768 struct ea_list *nlistp, *flistp;
770 for (nlistp = name_list; nlistp; nlistp = nlistp->next) {
771 for (flistp = file_list; flistp; flistp = flistp->next) {
772 if (strequal(nlistp->ea.name, flistp->ea.name)) {
773 break;
777 if (flistp) {
778 /* Copy the data from this entry. */
779 nlistp->ea.flags = flistp->ea.flags;
780 nlistp->ea.value = flistp->ea.value;
781 } else {
782 /* Null entry. */
783 nlistp->ea.flags = 0;
784 ZERO_STRUCT(nlistp->ea.value);
788 *total_ea_len = ea_list_size(name_list);
789 return name_list;
792 /****************************************************************************
793 Send the required number of replies back.
794 We assume all fields other than the data fields are
795 set correctly for the type of call.
796 HACK ! Always assumes smb_setup field is zero.
797 ****************************************************************************/
799 void send_trans2_replies(connection_struct *conn,
800 struct smb_request *req,
801 const char *params,
802 int paramsize,
803 const char *pdata,
804 int datasize,
805 int max_data_bytes)
807 /* As we are using a protocol > LANMAN1 then the max_send
808 variable must have been set in the sessetupX call.
809 This takes precedence over the max_xmit field in the
810 global struct. These different max_xmit variables should
811 be merged as this is now too confusing */
813 int data_to_send = datasize;
814 int params_to_send = paramsize;
815 int useable_space;
816 const char *pp = params;
817 const char *pd = pdata;
818 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
819 int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
820 int data_alignment_offset = 0;
821 bool overflow = False;
822 struct smbd_server_connection *sconn = req->sconn;
823 int max_send = sconn->smb1.sessions.max_send;
825 /* Modify the data_to_send and datasize and set the error if
826 we're trying to send more than max_data_bytes. We still send
827 the part of the packet(s) that fit. Strange, but needed
828 for OS/2. */
830 if (max_data_bytes > 0 && datasize > max_data_bytes) {
831 DEBUG(5,("send_trans2_replies: max_data_bytes %d exceeded by data %d\n",
832 max_data_bytes, datasize ));
833 datasize = data_to_send = max_data_bytes;
834 overflow = True;
837 /* If there genuinely are no parameters or data to send just send the empty packet */
839 if(params_to_send == 0 && data_to_send == 0) {
840 reply_outbuf(req, 10, 0);
841 show_msg((char *)req->outbuf);
842 if (!srv_send_smb(sconn,
843 (char *)req->outbuf,
844 true, req->seqnum+1,
845 IS_CONN_ENCRYPTED(conn),
846 &req->pcd)) {
847 exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
849 TALLOC_FREE(req->outbuf);
850 return;
853 /* When sending params and data ensure that both are nicely aligned */
854 /* Only do this alignment when there is also data to send - else
855 can cause NT redirector problems. */
857 if (((params_to_send % 4) != 0) && (data_to_send != 0))
858 data_alignment_offset = 4 - (params_to_send % 4);
860 /* Space is bufsize minus Netbios over TCP header minus SMB header */
861 /* The alignment_offset is to align the param bytes on an even byte
862 boundary. NT 4.0 Beta needs this to work correctly. */
864 useable_space = max_send - (smb_size
865 + 2 * 10 /* wct */
866 + alignment_offset
867 + data_alignment_offset);
869 if (useable_space < 0) {
870 DEBUG(0, ("send_trans2_replies failed sanity useable_space "
871 "= %d!!!", useable_space));
872 exit_server_cleanly("send_trans2_replies: Not enough space");
875 while (params_to_send || data_to_send) {
876 /* Calculate whether we will totally or partially fill this packet */
878 total_sent_thistime = params_to_send + data_to_send;
880 /* We can never send more than useable_space */
882 * Note that 'useable_space' does not include the alignment offsets,
883 * but we must include the alignment offsets in the calculation of
884 * the length of the data we send over the wire, as the alignment offsets
885 * are sent here. Fix from Marc_Jacobsen@hp.com.
888 total_sent_thistime = MIN(total_sent_thistime, useable_space);
890 reply_outbuf(req, 10, total_sent_thistime + alignment_offset
891 + data_alignment_offset);
894 * We might have SMBtrans2s in req which was transferred to
895 * the outbuf, fix that.
897 SCVAL(req->outbuf, smb_com, SMBtrans2);
899 /* Set total params and data to be sent */
900 SSVAL(req->outbuf,smb_tprcnt,paramsize);
901 SSVAL(req->outbuf,smb_tdrcnt,datasize);
903 /* Calculate how many parameters and data we can fit into
904 * this packet. Parameters get precedence
907 params_sent_thistime = MIN(params_to_send,useable_space);
908 data_sent_thistime = useable_space - params_sent_thistime;
909 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
911 SSVAL(req->outbuf,smb_prcnt, params_sent_thistime);
913 /* smb_proff is the offset from the start of the SMB header to the
914 parameter bytes, however the first 4 bytes of outbuf are
915 the Netbios over TCP header. Thus use smb_base() to subtract
916 them from the calculation */
918 SSVAL(req->outbuf,smb_proff,
919 ((smb_buf(req->outbuf)+alignment_offset)
920 - smb_base(req->outbuf)));
922 if(params_sent_thistime == 0)
923 SSVAL(req->outbuf,smb_prdisp,0);
924 else
925 /* Absolute displacement of param bytes sent in this packet */
926 SSVAL(req->outbuf,smb_prdisp,pp - params);
928 SSVAL(req->outbuf,smb_drcnt, data_sent_thistime);
929 if(data_sent_thistime == 0) {
930 SSVAL(req->outbuf,smb_droff,0);
931 SSVAL(req->outbuf,smb_drdisp, 0);
932 } else {
933 /* The offset of the data bytes is the offset of the
934 parameter bytes plus the number of parameters being sent this time */
935 SSVAL(req->outbuf, smb_droff,
936 ((smb_buf(req->outbuf)+alignment_offset)
937 - smb_base(req->outbuf))
938 + params_sent_thistime + data_alignment_offset);
939 SSVAL(req->outbuf,smb_drdisp, pd - pdata);
942 /* Initialize the padding for alignment */
944 if (alignment_offset != 0) {
945 memset(smb_buf(req->outbuf), 0, alignment_offset);
948 /* Copy the param bytes into the packet */
950 if(params_sent_thistime) {
951 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
952 params_sent_thistime);
955 /* Copy in the data bytes */
956 if(data_sent_thistime) {
957 if (data_alignment_offset != 0) {
958 memset((smb_buf(req->outbuf)+alignment_offset+
959 params_sent_thistime), 0,
960 data_alignment_offset);
962 memcpy(smb_buf(req->outbuf)+alignment_offset
963 +params_sent_thistime+data_alignment_offset,
964 pd,data_sent_thistime);
967 DEBUG(9,("t2_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
968 params_sent_thistime, data_sent_thistime, useable_space));
969 DEBUG(9,("t2_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
970 params_to_send, data_to_send, paramsize, datasize));
972 if (overflow) {
973 error_packet_set((char *)req->outbuf,
974 ERRDOS,ERRbufferoverflow,
975 STATUS_BUFFER_OVERFLOW,
976 __LINE__,__FILE__);
979 /* Send the packet */
980 show_msg((char *)req->outbuf);
981 if (!srv_send_smb(sconn,
982 (char *)req->outbuf,
983 true, req->seqnum+1,
984 IS_CONN_ENCRYPTED(conn),
985 &req->pcd))
986 exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
988 TALLOC_FREE(req->outbuf);
990 pp += params_sent_thistime;
991 pd += data_sent_thistime;
993 params_to_send -= params_sent_thistime;
994 data_to_send -= data_sent_thistime;
996 /* Sanity check */
997 if(params_to_send < 0 || data_to_send < 0) {
998 DEBUG(0,("send_trans2_replies failed sanity check pts = %d, dts = %d\n!!!",
999 params_to_send, data_to_send));
1000 return;
1004 return;
1007 /****************************************************************************
1008 Reply to a TRANSACT2_OPEN.
1009 ****************************************************************************/
1011 static void call_trans2open(connection_struct *conn,
1012 struct smb_request *req,
1013 char **pparams, int total_params,
1014 char **ppdata, int total_data,
1015 unsigned int max_data_bytes)
1017 struct smb_filename *smb_fname = NULL;
1018 char *params = *pparams;
1019 char *pdata = *ppdata;
1020 int deny_mode;
1021 int32 open_attr;
1022 bool oplock_request;
1023 #if 0
1024 bool return_additional_info;
1025 int16 open_sattr;
1026 time_t open_time;
1027 #endif
1028 int open_ofun;
1029 uint32 open_size;
1030 char *pname;
1031 char *fname = NULL;
1032 off_t size=0;
1033 int fattr=0,mtime=0;
1034 SMB_INO_T inode = 0;
1035 int smb_action = 0;
1036 files_struct *fsp;
1037 struct ea_list *ea_list = NULL;
1038 uint16 flags = 0;
1039 NTSTATUS status;
1040 uint32 access_mask;
1041 uint32 share_mode;
1042 uint32 create_disposition;
1043 uint32 create_options = 0;
1044 uint32_t private_flags = 0;
1045 TALLOC_CTX *ctx = talloc_tos();
1048 * Ensure we have enough parameters to perform the operation.
1051 if (total_params < 29) {
1052 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1053 goto out;
1056 flags = SVAL(params, 0);
1057 deny_mode = SVAL(params, 2);
1058 open_attr = SVAL(params,6);
1059 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1060 if (oplock_request) {
1061 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1064 #if 0
1065 return_additional_info = BITSETW(params,0);
1066 open_sattr = SVAL(params, 4);
1067 open_time = make_unix_date3(params+8);
1068 #endif
1069 open_ofun = SVAL(params,12);
1070 open_size = IVAL(params,14);
1071 pname = &params[28];
1073 if (IS_IPC(conn)) {
1074 reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
1075 goto out;
1078 srvstr_get_path(ctx, params, req->flags2, &fname, pname,
1079 total_params - 28, STR_TERMINATE,
1080 &status);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 reply_nterror(req, status);
1083 goto out;
1086 DEBUG(3,("call_trans2open %s deny_mode=0x%x attr=%d ofun=0x%x size=%d\n",
1087 fname, (unsigned int)deny_mode, (unsigned int)open_attr,
1088 (unsigned int)open_ofun, open_size));
1090 status = filename_convert(ctx,
1091 conn,
1092 req->flags2 & FLAGS2_DFS_PATHNAMES,
1093 fname,
1095 NULL,
1096 &smb_fname);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1099 reply_botherror(req,
1100 NT_STATUS_PATH_NOT_COVERED,
1101 ERRSRV, ERRbadpath);
1102 goto out;
1104 reply_nterror(req, status);
1105 goto out;
1108 if (open_ofun == 0) {
1109 reply_nterror(req, NT_STATUS_OBJECT_NAME_COLLISION);
1110 goto out;
1113 if (!map_open_params_to_ntcreate(smb_fname->base_name, deny_mode,
1114 open_ofun,
1115 &access_mask, &share_mode,
1116 &create_disposition,
1117 &create_options,
1118 &private_flags)) {
1119 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1120 goto out;
1123 /* Any data in this call is an EA list. */
1124 if (total_data && (total_data != 4)) {
1125 if (total_data < 10) {
1126 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1127 goto out;
1130 if (IVAL(pdata,0) > total_data) {
1131 DEBUG(10,("call_trans2open: bad total data size (%u) > %u\n",
1132 IVAL(pdata,0), (unsigned int)total_data));
1133 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1134 goto out;
1137 ea_list = read_ea_list(talloc_tos(), pdata + 4,
1138 total_data - 4);
1139 if (!ea_list) {
1140 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1141 goto out;
1144 if (!lp_ea_support(SNUM(conn))) {
1145 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1146 goto out;
1150 status = SMB_VFS_CREATE_FILE(
1151 conn, /* conn */
1152 req, /* req */
1153 0, /* root_dir_fid */
1154 smb_fname, /* fname */
1155 access_mask, /* access_mask */
1156 share_mode, /* share_access */
1157 create_disposition, /* create_disposition*/
1158 create_options, /* create_options */
1159 open_attr, /* file_attributes */
1160 oplock_request, /* oplock_request */
1161 open_size, /* allocation_size */
1162 private_flags,
1163 NULL, /* sd */
1164 ea_list, /* ea_list */
1165 &fsp, /* result */
1166 &smb_action); /* psbuf */
1168 if (!NT_STATUS_IS_OK(status)) {
1169 if (open_was_deferred(req->sconn, req->mid)) {
1170 /* We have re-scheduled this call. */
1171 goto out;
1173 reply_openerror(req, status);
1174 goto out;
1177 size = get_file_size_stat(&smb_fname->st);
1178 fattr = dos_mode(conn, smb_fname);
1179 mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
1180 inode = smb_fname->st.st_ex_ino;
1181 if (fattr & FILE_ATTRIBUTE_DIRECTORY) {
1182 close_file(req, fsp, ERROR_CLOSE);
1183 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1184 goto out;
1187 /* Realloc the size of parameters and data we will return */
1188 *pparams = (char *)SMB_REALLOC(*pparams, 30);
1189 if(*pparams == NULL ) {
1190 reply_nterror(req, NT_STATUS_NO_MEMORY);
1191 goto out;
1193 params = *pparams;
1195 SSVAL(params,0,fsp->fnum);
1196 SSVAL(params,2,fattr);
1197 srv_put_dos_date2(params,4, mtime);
1198 SIVAL(params,8, (uint32)size);
1199 SSVAL(params,12,deny_mode);
1200 SSVAL(params,14,0); /* open_type - file or directory. */
1201 SSVAL(params,16,0); /* open_state - only valid for IPC device. */
1203 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1204 smb_action |= EXTENDED_OPLOCK_GRANTED;
1207 SSVAL(params,18,smb_action);
1210 * WARNING - this may need to be changed if SMB_INO_T <> 4 bytes.
1212 SIVAL(params,20,inode);
1213 SSVAL(params,24,0); /* Padding. */
1214 if (flags & 8) {
1215 uint32 ea_size = estimate_ea_size(conn, fsp,
1216 fsp->fsp_name->base_name);
1217 SIVAL(params, 26, ea_size);
1218 } else {
1219 SIVAL(params, 26, 0);
1222 /* Send the required number of replies */
1223 send_trans2_replies(conn, req, params, 30, *ppdata, 0, max_data_bytes);
1224 out:
1225 TALLOC_FREE(smb_fname);
1228 /*********************************************************
1229 Routine to check if a given string matches exactly.
1230 as a special case a mask of "." does NOT match. That
1231 is required for correct wildcard semantics
1232 Case can be significant or not.
1233 **********************************************************/
1235 static bool exact_match(bool has_wild,
1236 bool case_sensitive,
1237 const char *str,
1238 const char *mask)
1240 if (mask[0] == '.' && mask[1] == 0) {
1241 return false;
1244 if (has_wild) {
1245 return false;
1248 if (case_sensitive) {
1249 return strcmp(str,mask)==0;
1250 } else {
1251 return strcasecmp_m(str,mask) == 0;
1255 /****************************************************************************
1256 Return the filetype for UNIX extensions.
1257 ****************************************************************************/
1259 static uint32 unix_filetype(mode_t mode)
1261 if(S_ISREG(mode))
1262 return UNIX_TYPE_FILE;
1263 else if(S_ISDIR(mode))
1264 return UNIX_TYPE_DIR;
1265 #ifdef S_ISLNK
1266 else if(S_ISLNK(mode))
1267 return UNIX_TYPE_SYMLINK;
1268 #endif
1269 #ifdef S_ISCHR
1270 else if(S_ISCHR(mode))
1271 return UNIX_TYPE_CHARDEV;
1272 #endif
1273 #ifdef S_ISBLK
1274 else if(S_ISBLK(mode))
1275 return UNIX_TYPE_BLKDEV;
1276 #endif
1277 #ifdef S_ISFIFO
1278 else if(S_ISFIFO(mode))
1279 return UNIX_TYPE_FIFO;
1280 #endif
1281 #ifdef S_ISSOCK
1282 else if(S_ISSOCK(mode))
1283 return UNIX_TYPE_SOCKET;
1284 #endif
1286 DEBUG(0,("unix_filetype: unknown filetype %u\n", (unsigned)mode));
1287 return UNIX_TYPE_UNKNOWN;
1290 /****************************************************************************
1291 Map wire perms onto standard UNIX permissions. Obey share restrictions.
1292 ****************************************************************************/
1294 enum perm_type { PERM_NEW_FILE, PERM_NEW_DIR, PERM_EXISTING_FILE, PERM_EXISTING_DIR};
1296 static NTSTATUS unix_perms_from_wire( connection_struct *conn,
1297 const SMB_STRUCT_STAT *psbuf,
1298 uint32 perms,
1299 enum perm_type ptype,
1300 mode_t *ret_perms)
1302 mode_t ret = 0;
1304 if (perms == SMB_MODE_NO_CHANGE) {
1305 if (!VALID_STAT(*psbuf)) {
1306 return NT_STATUS_INVALID_PARAMETER;
1307 } else {
1308 *ret_perms = psbuf->st_ex_mode;
1309 return NT_STATUS_OK;
1313 ret |= ((perms & UNIX_X_OTH ) ? S_IXOTH : 0);
1314 ret |= ((perms & UNIX_W_OTH ) ? S_IWOTH : 0);
1315 ret |= ((perms & UNIX_R_OTH ) ? S_IROTH : 0);
1316 ret |= ((perms & UNIX_X_GRP ) ? S_IXGRP : 0);
1317 ret |= ((perms & UNIX_W_GRP ) ? S_IWGRP : 0);
1318 ret |= ((perms & UNIX_R_GRP ) ? S_IRGRP : 0);
1319 ret |= ((perms & UNIX_X_USR ) ? S_IXUSR : 0);
1320 ret |= ((perms & UNIX_W_USR ) ? S_IWUSR : 0);
1321 ret |= ((perms & UNIX_R_USR ) ? S_IRUSR : 0);
1322 #ifdef S_ISVTX
1323 ret |= ((perms & UNIX_STICKY ) ? S_ISVTX : 0);
1324 #endif
1325 #ifdef S_ISGID
1326 ret |= ((perms & UNIX_SET_GID ) ? S_ISGID : 0);
1327 #endif
1328 #ifdef S_ISUID
1329 ret |= ((perms & UNIX_SET_UID ) ? S_ISUID : 0);
1330 #endif
1332 switch (ptype) {
1333 case PERM_NEW_FILE:
1334 /* Apply mode mask */
1335 ret &= lp_create_mask(SNUM(conn));
1336 /* Add in force bits */
1337 ret |= lp_force_create_mode(SNUM(conn));
1338 break;
1339 case PERM_NEW_DIR:
1340 ret &= lp_dir_mask(SNUM(conn));
1341 /* Add in force bits */
1342 ret |= lp_force_dir_mode(SNUM(conn));
1343 break;
1344 case PERM_EXISTING_FILE:
1345 /* Apply mode mask */
1346 ret &= lp_security_mask(SNUM(conn));
1347 /* Add in force bits */
1348 ret |= lp_force_security_mode(SNUM(conn));
1349 break;
1350 case PERM_EXISTING_DIR:
1351 /* Apply mode mask */
1352 ret &= lp_dir_security_mask(SNUM(conn));
1353 /* Add in force bits */
1354 ret |= lp_force_dir_security_mode(SNUM(conn));
1355 break;
1358 *ret_perms = ret;
1359 return NT_STATUS_OK;
1362 /****************************************************************************
1363 Needed to show the msdfs symlinks as directories. Modifies psbuf
1364 to be a directory if it's a msdfs link.
1365 ****************************************************************************/
1367 static bool check_msdfs_link(connection_struct *conn,
1368 const char *pathname,
1369 SMB_STRUCT_STAT *psbuf)
1371 int saved_errno = errno;
1372 if(lp_host_msdfs() &&
1373 lp_msdfs_root(SNUM(conn)) &&
1374 is_msdfs_link(conn, pathname, psbuf)) {
1376 DEBUG(5,("check_msdfs_link: Masquerading msdfs link %s "
1377 "as a directory\n",
1378 pathname));
1379 psbuf->st_ex_mode = (psbuf->st_ex_mode & 0xFFF) | S_IFDIR;
1380 errno = saved_errno;
1381 return true;
1383 errno = saved_errno;
1384 return false;
1388 /****************************************************************************
1389 Get a level dependent lanman2 dir entry.
1390 ****************************************************************************/
1392 struct smbd_dirptr_lanman2_state {
1393 connection_struct *conn;
1394 uint32_t info_level;
1395 bool check_mangled_names;
1396 bool has_wild;
1397 bool got_exact_match;
1400 static bool smbd_dirptr_lanman2_match_fn(TALLOC_CTX *ctx,
1401 void *private_data,
1402 const char *dname,
1403 const char *mask,
1404 char **_fname)
1406 struct smbd_dirptr_lanman2_state *state =
1407 (struct smbd_dirptr_lanman2_state *)private_data;
1408 bool ok;
1409 char mangled_name[13]; /* mangled 8.3 name. */
1410 bool got_match;
1411 const char *fname;
1413 /* Mangle fname if it's an illegal name. */
1414 if (mangle_must_mangle(dname, state->conn->params)) {
1415 ok = name_to_8_3(dname, mangled_name,
1416 true, state->conn->params);
1417 if (!ok) {
1418 return false;
1420 fname = mangled_name;
1421 } else {
1422 fname = dname;
1425 got_match = exact_match(state->has_wild,
1426 state->conn->case_sensitive,
1427 fname, mask);
1428 state->got_exact_match = got_match;
1429 if (!got_match) {
1430 got_match = mask_match(fname, mask,
1431 state->conn->case_sensitive);
1434 if(!got_match && state->check_mangled_names &&
1435 !mangle_is_8_3(fname, false, state->conn->params)) {
1437 * It turns out that NT matches wildcards against
1438 * both long *and* short names. This may explain some
1439 * of the wildcard wierdness from old DOS clients
1440 * that some people have been seeing.... JRA.
1442 /* Force the mangling into 8.3. */
1443 ok = name_to_8_3(fname, mangled_name,
1444 false, state->conn->params);
1445 if (!ok) {
1446 return false;
1449 got_match = exact_match(state->has_wild,
1450 state->conn->case_sensitive,
1451 mangled_name, mask);
1452 state->got_exact_match = got_match;
1453 if (!got_match) {
1454 got_match = mask_match(mangled_name, mask,
1455 state->conn->case_sensitive);
1459 if (!got_match) {
1460 return false;
1463 *_fname = talloc_strdup(ctx, fname);
1464 if (*_fname == NULL) {
1465 return false;
1468 return true;
1471 static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
1472 void *private_data,
1473 struct smb_filename *smb_fname,
1474 uint32_t *_mode)
1476 struct smbd_dirptr_lanman2_state *state =
1477 (struct smbd_dirptr_lanman2_state *)private_data;
1478 bool ms_dfs_link = false;
1479 uint32_t mode = 0;
1481 if (INFO_LEVEL_IS_UNIX(state->info_level)) {
1482 if (SMB_VFS_LSTAT(state->conn, smb_fname) != 0) {
1483 DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
1484 "Couldn't lstat [%s] (%s)\n",
1485 smb_fname_str_dbg(smb_fname),
1486 strerror(errno)));
1487 return false;
1489 } else if (!VALID_STAT(smb_fname->st) &&
1490 SMB_VFS_STAT(state->conn, smb_fname) != 0) {
1491 /* Needed to show the msdfs symlinks as
1492 * directories */
1494 ms_dfs_link = check_msdfs_link(state->conn,
1495 smb_fname->base_name,
1496 &smb_fname->st);
1497 if (!ms_dfs_link) {
1498 DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
1499 "Couldn't stat [%s] (%s)\n",
1500 smb_fname_str_dbg(smb_fname),
1501 strerror(errno)));
1502 return false;
1506 if (ms_dfs_link) {
1507 mode = dos_mode_msdfs(state->conn, smb_fname);
1508 } else {
1509 mode = dos_mode(state->conn, smb_fname);
1512 *_mode = mode;
1513 return true;
1516 static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
1517 connection_struct *conn,
1518 uint16_t flags2,
1519 uint32_t info_level,
1520 struct ea_list *name_list,
1521 bool check_mangled_names,
1522 bool requires_resume_key,
1523 uint32_t mode,
1524 const char *fname,
1525 const struct smb_filename *smb_fname,
1526 int space_remaining,
1527 uint8_t align,
1528 bool do_pad,
1529 char *base_data,
1530 char **ppdata,
1531 char *end_data,
1532 bool *out_of_space,
1533 uint64_t *last_entry_off)
1535 char *p, *q, *pdata = *ppdata;
1536 uint32_t reskey=0;
1537 uint64_t file_size = 0;
1538 uint64_t allocation_size = 0;
1539 uint64_t file_index = 0;
1540 uint32_t len;
1541 struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
1542 time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
1543 char *nameptr;
1544 char *last_entry_ptr;
1545 bool was_8_3;
1546 int off;
1547 int pad = 0;
1549 *out_of_space = false;
1551 ZERO_STRUCT(mdate_ts);
1552 ZERO_STRUCT(adate_ts);
1553 ZERO_STRUCT(create_date_ts);
1554 ZERO_STRUCT(cdate_ts);
1556 if (!(mode & FILE_ATTRIBUTE_DIRECTORY)) {
1557 file_size = get_file_size_stat(&smb_fname->st);
1559 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
1561 file_index = get_FileIndex(conn, &smb_fname->st);
1563 mdate_ts = smb_fname->st.st_ex_mtime;
1564 adate_ts = smb_fname->st.st_ex_atime;
1565 create_date_ts = get_create_timespec(conn, NULL, smb_fname);
1566 cdate_ts = get_change_timespec(conn, NULL, smb_fname);
1568 if (lp_dos_filetime_resolution(SNUM(conn))) {
1569 dos_filetime_timespec(&create_date_ts);
1570 dos_filetime_timespec(&mdate_ts);
1571 dos_filetime_timespec(&adate_ts);
1572 dos_filetime_timespec(&cdate_ts);
1575 create_date = convert_timespec_to_time_t(create_date_ts);
1576 mdate = convert_timespec_to_time_t(mdate_ts);
1577 adate = convert_timespec_to_time_t(adate_ts);
1579 /* align the record */
1580 SMB_ASSERT(align >= 1);
1582 off = (int)PTR_DIFF(pdata, base_data);
1583 pad = (off + (align-1)) & ~(align-1);
1584 pad -= off;
1586 if (pad && pad > space_remaining) {
1587 *out_of_space = true;
1588 DEBUG(9,("smbd_marshall_dir_entry: out of space "
1589 "for padding (wanted %u, had %d)\n",
1590 (unsigned int)pad,
1591 space_remaining ));
1592 return false; /* Not finished - just out of space */
1595 off += pad;
1596 /* initialize padding to 0 */
1597 if (pad) {
1598 memset(pdata, 0, pad);
1600 space_remaining -= pad;
1602 DEBUG(10,("smbd_marshall_dir_entry: space_remaining = %d\n",
1603 space_remaining ));
1605 pdata += pad;
1606 p = pdata;
1607 last_entry_ptr = p;
1609 pad = 0;
1610 off = 0;
1612 switch (info_level) {
1613 case SMB_FIND_INFO_STANDARD:
1614 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_INFO_STANDARD\n"));
1615 if(requires_resume_key) {
1616 SIVAL(p,0,reskey);
1617 p += 4;
1619 srv_put_dos_date2(p,0,create_date);
1620 srv_put_dos_date2(p,4,adate);
1621 srv_put_dos_date2(p,8,mdate);
1622 SIVAL(p,12,(uint32)file_size);
1623 SIVAL(p,16,(uint32)allocation_size);
1624 SSVAL(p,20,mode);
1625 p += 23;
1626 nameptr = p;
1627 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1628 p += ucs2_align(base_data, p, 0);
1630 len = srvstr_push(base_data, flags2, p,
1631 fname, PTR_DIFF(end_data, p),
1632 STR_TERMINATE);
1633 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1634 if (len > 2) {
1635 SCVAL(nameptr, -1, len - 2);
1636 } else {
1637 SCVAL(nameptr, -1, 0);
1639 } else {
1640 if (len > 1) {
1641 SCVAL(nameptr, -1, len - 1);
1642 } else {
1643 SCVAL(nameptr, -1, 0);
1646 p += len;
1647 break;
1649 case SMB_FIND_EA_SIZE:
1650 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_SIZE\n"));
1651 if (requires_resume_key) {
1652 SIVAL(p,0,reskey);
1653 p += 4;
1655 srv_put_dos_date2(p,0,create_date);
1656 srv_put_dos_date2(p,4,adate);
1657 srv_put_dos_date2(p,8,mdate);
1658 SIVAL(p,12,(uint32)file_size);
1659 SIVAL(p,16,(uint32)allocation_size);
1660 SSVAL(p,20,mode);
1662 unsigned int ea_size = estimate_ea_size(conn, NULL,
1663 smb_fname->base_name);
1664 SIVAL(p,22,ea_size); /* Extended attributes */
1666 p += 27;
1667 nameptr = p - 1;
1668 len = srvstr_push(base_data, flags2,
1669 p, fname, PTR_DIFF(end_data, p),
1670 STR_TERMINATE | STR_NOALIGN);
1671 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1672 if (len > 2) {
1673 len -= 2;
1674 } else {
1675 len = 0;
1677 } else {
1678 if (len > 1) {
1679 len -= 1;
1680 } else {
1681 len = 0;
1684 SCVAL(nameptr,0,len);
1685 p += len;
1686 SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1687 break;
1689 case SMB_FIND_EA_LIST:
1691 struct ea_list *file_list = NULL;
1692 size_t ea_len = 0;
1694 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_LIST\n"));
1695 if (!name_list) {
1696 return false;
1698 if (requires_resume_key) {
1699 SIVAL(p,0,reskey);
1700 p += 4;
1702 srv_put_dos_date2(p,0,create_date);
1703 srv_put_dos_date2(p,4,adate);
1704 srv_put_dos_date2(p,8,mdate);
1705 SIVAL(p,12,(uint32)file_size);
1706 SIVAL(p,16,(uint32)allocation_size);
1707 SSVAL(p,20,mode);
1708 p += 22; /* p now points to the EA area. */
1710 file_list = get_ea_list_from_file(ctx, conn, NULL,
1711 smb_fname->base_name,
1712 &ea_len);
1713 name_list = ea_list_union(name_list, file_list, &ea_len);
1715 /* We need to determine if this entry will fit in the space available. */
1716 /* Max string size is 255 bytes. */
1717 if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
1718 *out_of_space = true;
1719 DEBUG(9,("smbd_marshall_dir_entry: out of space "
1720 "(wanted %u, had %d)\n",
1721 (unsigned int)PTR_DIFF(p + 255 + ea_len,pdata),
1722 space_remaining ));
1723 return False; /* Not finished - just out of space */
1726 /* Push the ea_data followed by the name. */
1727 p += fill_ea_buffer(ctx, p, space_remaining, conn, name_list);
1728 nameptr = p;
1729 len = srvstr_push(base_data, flags2,
1730 p + 1, fname, PTR_DIFF(end_data, p+1),
1731 STR_TERMINATE | STR_NOALIGN);
1732 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1733 if (len > 2) {
1734 len -= 2;
1735 } else {
1736 len = 0;
1738 } else {
1739 if (len > 1) {
1740 len -= 1;
1741 } else {
1742 len = 0;
1745 SCVAL(nameptr,0,len);
1746 p += len + 1;
1747 SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1748 break;
1751 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1752 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
1753 was_8_3 = mangle_is_8_3(fname, True, conn->params);
1754 p += 4;
1755 SIVAL(p,0,reskey); p += 4;
1756 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1757 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1758 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1759 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1760 SOFF_T(p,0,file_size); p += 8;
1761 SOFF_T(p,0,allocation_size); p += 8;
1762 SIVAL(p,0,mode); p += 4;
1763 q = p; p += 4; /* q is placeholder for name length. */
1765 unsigned int ea_size = estimate_ea_size(conn, NULL,
1766 smb_fname->base_name);
1767 SIVAL(p,0,ea_size); /* Extended attributes */
1768 p += 4;
1770 /* Clear the short name buffer. This is
1771 * IMPORTANT as not doing so will trigger
1772 * a Win2k client bug. JRA.
1774 if (!was_8_3 && check_mangled_names) {
1775 char mangled_name[13]; /* mangled 8.3 name. */
1776 if (!name_to_8_3(fname,mangled_name,True,
1777 conn->params)) {
1778 /* Error - mangle failed ! */
1779 memset(mangled_name,'\0',12);
1781 mangled_name[12] = 0;
1782 len = srvstr_push(base_data, flags2,
1783 p+2, mangled_name, 24,
1784 STR_UPPER|STR_UNICODE);
1785 if (len < 24) {
1786 memset(p + 2 + len,'\0',24 - len);
1788 SSVAL(p, 0, len);
1789 } else {
1790 memset(p,'\0',26);
1792 p += 2 + 24;
1793 len = srvstr_push(base_data, flags2, p,
1794 fname, PTR_DIFF(end_data, p),
1795 STR_TERMINATE_ASCII);
1796 SIVAL(q,0,len);
1797 p += len;
1799 len = PTR_DIFF(p, pdata);
1800 pad = (len + (align-1)) & ~(align-1);
1802 * offset to the next entry, the caller
1803 * will overwrite it for the last entry
1804 * that's why we always include the padding
1806 SIVAL(pdata,0,pad);
1808 * set padding to zero
1810 if (do_pad) {
1811 memset(p, 0, pad - len);
1812 p = pdata + pad;
1813 } else {
1814 p = pdata + len;
1816 break;
1818 case SMB_FIND_FILE_DIRECTORY_INFO:
1819 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
1820 p += 4;
1821 SIVAL(p,0,reskey); p += 4;
1822 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1823 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1824 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1825 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1826 SOFF_T(p,0,file_size); p += 8;
1827 SOFF_T(p,0,allocation_size); p += 8;
1828 SIVAL(p,0,mode); p += 4;
1829 len = srvstr_push(base_data, flags2,
1830 p + 4, fname, PTR_DIFF(end_data, p+4),
1831 STR_TERMINATE_ASCII);
1832 SIVAL(p,0,len);
1833 p += 4 + len;
1835 len = PTR_DIFF(p, pdata);
1836 pad = (len + (align-1)) & ~(align-1);
1838 * offset to the next entry, the caller
1839 * will overwrite it for the last entry
1840 * that's why we always include the padding
1842 SIVAL(pdata,0,pad);
1844 * set padding to zero
1846 if (do_pad) {
1847 memset(p, 0, pad - len);
1848 p = pdata + pad;
1849 } else {
1850 p = pdata + len;
1852 break;
1854 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1855 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
1856 p += 4;
1857 SIVAL(p,0,reskey); p += 4;
1858 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1859 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1860 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1861 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1862 SOFF_T(p,0,file_size); p += 8;
1863 SOFF_T(p,0,allocation_size); p += 8;
1864 SIVAL(p,0,mode); p += 4;
1865 q = p; p += 4; /* q is placeholder for name length. */
1867 unsigned int ea_size = estimate_ea_size(conn, NULL,
1868 smb_fname->base_name);
1869 SIVAL(p,0,ea_size); /* Extended attributes */
1870 p +=4;
1872 len = srvstr_push(base_data, flags2, p,
1873 fname, PTR_DIFF(end_data, p),
1874 STR_TERMINATE_ASCII);
1875 SIVAL(q, 0, len);
1876 p += len;
1878 len = PTR_DIFF(p, pdata);
1879 pad = (len + (align-1)) & ~(align-1);
1881 * offset to the next entry, the caller
1882 * will overwrite it for the last entry
1883 * that's why we always include the padding
1885 SIVAL(pdata,0,pad);
1887 * set padding to zero
1889 if (do_pad) {
1890 memset(p, 0, pad - len);
1891 p = pdata + pad;
1892 } else {
1893 p = pdata + len;
1895 break;
1897 case SMB_FIND_FILE_NAMES_INFO:
1898 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
1899 p += 4;
1900 SIVAL(p,0,reskey); p += 4;
1901 p += 4;
1902 /* this must *not* be null terminated or w2k gets in a loop trying to set an
1903 acl on a dir (tridge) */
1904 len = srvstr_push(base_data, flags2, p,
1905 fname, PTR_DIFF(end_data, p),
1906 STR_TERMINATE_ASCII);
1907 SIVAL(p, -4, len);
1908 p += len;
1910 len = PTR_DIFF(p, pdata);
1911 pad = (len + (align-1)) & ~(align-1);
1913 * offset to the next entry, the caller
1914 * will overwrite it for the last entry
1915 * that's why we always include the padding
1917 SIVAL(pdata,0,pad);
1919 * set padding to zero
1921 if (do_pad) {
1922 memset(p, 0, pad - len);
1923 p = pdata + pad;
1924 } else {
1925 p = pdata + len;
1927 break;
1929 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1930 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
1931 p += 4;
1932 SIVAL(p,0,reskey); p += 4;
1933 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1934 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1935 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1936 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1937 SOFF_T(p,0,file_size); p += 8;
1938 SOFF_T(p,0,allocation_size); p += 8;
1939 SIVAL(p,0,mode); p += 4;
1940 q = p; p += 4; /* q is placeholder for name length. */
1942 unsigned int ea_size = estimate_ea_size(conn, NULL,
1943 smb_fname->base_name);
1944 SIVAL(p,0,ea_size); /* Extended attributes */
1945 p +=4;
1947 SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
1948 SBVAL(p,0,file_index); p += 8;
1949 len = srvstr_push(base_data, flags2, p,
1950 fname, PTR_DIFF(end_data, p),
1951 STR_TERMINATE_ASCII);
1952 SIVAL(q, 0, len);
1953 p += len;
1955 len = PTR_DIFF(p, pdata);
1956 pad = (len + (align-1)) & ~(align-1);
1958 * offset to the next entry, the caller
1959 * will overwrite it for the last entry
1960 * that's why we always include the padding
1962 SIVAL(pdata,0,pad);
1964 * set padding to zero
1966 if (do_pad) {
1967 memset(p, 0, pad - len);
1968 p = pdata + pad;
1969 } else {
1970 p = pdata + len;
1972 break;
1974 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1975 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
1976 was_8_3 = mangle_is_8_3(fname, True, conn->params);
1977 p += 4;
1978 SIVAL(p,0,reskey); p += 4;
1979 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1980 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1981 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1982 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1983 SOFF_T(p,0,file_size); p += 8;
1984 SOFF_T(p,0,allocation_size); p += 8;
1985 SIVAL(p,0,mode); p += 4;
1986 q = p; p += 4; /* q is placeholder for name length */
1988 unsigned int ea_size = estimate_ea_size(conn, NULL,
1989 smb_fname->base_name);
1990 SIVAL(p,0,ea_size); /* Extended attributes */
1991 p +=4;
1993 /* Clear the short name buffer. This is
1994 * IMPORTANT as not doing so will trigger
1995 * a Win2k client bug. JRA.
1997 if (!was_8_3 && check_mangled_names) {
1998 char mangled_name[13]; /* mangled 8.3 name. */
1999 if (!name_to_8_3(fname,mangled_name,True,
2000 conn->params)) {
2001 /* Error - mangle failed ! */
2002 memset(mangled_name,'\0',12);
2004 mangled_name[12] = 0;
2005 len = srvstr_push(base_data, flags2,
2006 p+2, mangled_name, 24,
2007 STR_UPPER|STR_UNICODE);
2008 SSVAL(p, 0, len);
2009 if (len < 24) {
2010 memset(p + 2 + len,'\0',24 - len);
2012 SSVAL(p, 0, len);
2013 } else {
2014 memset(p,'\0',26);
2016 p += 26;
2017 SSVAL(p,0,0); p += 2; /* Reserved ? */
2018 SBVAL(p,0,file_index); p += 8;
2019 len = srvstr_push(base_data, flags2, p,
2020 fname, PTR_DIFF(end_data, p),
2021 STR_TERMINATE_ASCII);
2022 SIVAL(q,0,len);
2023 p += len;
2025 len = PTR_DIFF(p, pdata);
2026 pad = (len + (align-1)) & ~(align-1);
2028 * offset to the next entry, the caller
2029 * will overwrite it for the last entry
2030 * that's why we always include the padding
2032 SIVAL(pdata,0,pad);
2034 * set padding to zero
2036 if (do_pad) {
2037 memset(p, 0, pad - len);
2038 p = pdata + pad;
2039 } else {
2040 p = pdata + len;
2042 break;
2044 /* CIFS UNIX Extension. */
2046 case SMB_FIND_FILE_UNIX:
2047 case SMB_FIND_FILE_UNIX_INFO2:
2048 p+= 4;
2049 SIVAL(p,0,reskey); p+= 4; /* Used for continuing search. */
2051 /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
2053 if (info_level == SMB_FIND_FILE_UNIX) {
2054 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX\n"));
2055 p = store_file_unix_basic(conn, p,
2056 NULL, &smb_fname->st);
2057 len = srvstr_push(base_data, flags2, p,
2058 fname, PTR_DIFF(end_data, p),
2059 STR_TERMINATE);
2060 } else {
2061 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
2062 p = store_file_unix_basic_info2(conn, p,
2063 NULL, &smb_fname->st);
2064 nameptr = p;
2065 p += 4;
2066 len = srvstr_push(base_data, flags2, p, fname,
2067 PTR_DIFF(end_data, p), 0);
2068 SIVAL(nameptr, 0, len);
2071 p += len;
2073 len = PTR_DIFF(p, pdata);
2074 pad = (len + (align-1)) & ~(align-1);
2076 * offset to the next entry, the caller
2077 * will overwrite it for the last entry
2078 * that's why we always include the padding
2080 SIVAL(pdata,0,pad);
2082 * set padding to zero
2084 if (do_pad) {
2085 memset(p, 0, pad - len);
2086 p = pdata + pad;
2087 } else {
2088 p = pdata + len;
2090 /* End of SMB_QUERY_FILE_UNIX_BASIC */
2092 break;
2094 default:
2095 return false;
2098 if (PTR_DIFF(p,pdata) > space_remaining) {
2099 *out_of_space = true;
2100 DEBUG(9,("smbd_marshall_dir_entry: out of space "
2101 "(wanted %u, had %d)\n",
2102 (unsigned int)PTR_DIFF(p,pdata),
2103 space_remaining ));
2104 return false; /* Not finished - just out of space */
2107 /* Setup the last entry pointer, as an offset from base_data */
2108 *last_entry_off = PTR_DIFF(last_entry_ptr,base_data);
2109 /* Advance the data pointer to the next slot */
2110 *ppdata = p;
2112 return true;
2115 bool smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
2116 connection_struct *conn,
2117 struct dptr_struct *dirptr,
2118 uint16 flags2,
2119 const char *path_mask,
2120 uint32 dirtype,
2121 int info_level,
2122 int requires_resume_key,
2123 bool dont_descend,
2124 bool ask_sharemode,
2125 uint8_t align,
2126 bool do_pad,
2127 char **ppdata,
2128 char *base_data,
2129 char *end_data,
2130 int space_remaining,
2131 bool *out_of_space,
2132 bool *got_exact_match,
2133 int *_last_entry_off,
2134 struct ea_list *name_list)
2136 const char *p;
2137 const char *mask = NULL;
2138 long prev_dirpos = 0;
2139 uint32_t mode = 0;
2140 char *fname = NULL;
2141 struct smb_filename *smb_fname = NULL;
2142 struct smbd_dirptr_lanman2_state state;
2143 bool ok;
2144 uint64_t last_entry_off = 0;
2146 ZERO_STRUCT(state);
2147 state.conn = conn;
2148 state.info_level = info_level;
2149 state.check_mangled_names = lp_manglednames(conn->params);
2150 state.has_wild = dptr_has_wild(dirptr);
2151 state.got_exact_match = false;
2153 *out_of_space = false;
2154 *got_exact_match = false;
2156 p = strrchr_m(path_mask,'/');
2157 if(p != NULL) {
2158 if(p[1] == '\0') {
2159 mask = "*.*";
2160 } else {
2161 mask = p+1;
2163 } else {
2164 mask = path_mask;
2167 ok = smbd_dirptr_get_entry(ctx,
2168 dirptr,
2169 mask,
2170 dirtype,
2171 dont_descend,
2172 ask_sharemode,
2173 smbd_dirptr_lanman2_match_fn,
2174 smbd_dirptr_lanman2_mode_fn,
2175 &state,
2176 &fname,
2177 &smb_fname,
2178 &mode,
2179 &prev_dirpos);
2180 if (!ok) {
2181 return false;
2184 *got_exact_match = state.got_exact_match;
2186 ok = smbd_marshall_dir_entry(ctx,
2187 conn,
2188 flags2,
2189 info_level,
2190 name_list,
2191 state.check_mangled_names,
2192 requires_resume_key,
2193 mode,
2194 fname,
2195 smb_fname,
2196 space_remaining,
2197 align,
2198 do_pad,
2199 base_data,
2200 ppdata,
2201 end_data,
2202 out_of_space,
2203 &last_entry_off);
2204 TALLOC_FREE(fname);
2205 TALLOC_FREE(smb_fname);
2206 if (*out_of_space) {
2207 dptr_SeekDir(dirptr, prev_dirpos);
2208 return false;
2210 if (!ok) {
2211 return false;
2214 *_last_entry_off = last_entry_off;
2215 return true;
2218 static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
2219 connection_struct *conn,
2220 struct dptr_struct *dirptr,
2221 uint16 flags2,
2222 const char *path_mask,
2223 uint32 dirtype,
2224 int info_level,
2225 bool requires_resume_key,
2226 bool dont_descend,
2227 bool ask_sharemode,
2228 char **ppdata,
2229 char *base_data,
2230 char *end_data,
2231 int space_remaining,
2232 bool *out_of_space,
2233 bool *got_exact_match,
2234 int *last_entry_off,
2235 struct ea_list *name_list)
2237 uint8_t align = 4;
2238 const bool do_pad = true;
2240 if (info_level >= 1 && info_level <= 3) {
2241 /* No alignment on earlier info levels. */
2242 align = 1;
2245 return smbd_dirptr_lanman2_entry(ctx, conn, dirptr, flags2,
2246 path_mask, dirtype, info_level,
2247 requires_resume_key, dont_descend, ask_sharemode,
2248 align, do_pad,
2249 ppdata, base_data, end_data,
2250 space_remaining,
2251 out_of_space, got_exact_match,
2252 last_entry_off, name_list);
2255 /****************************************************************************
2256 Reply to a TRANS2_FINDFIRST.
2257 ****************************************************************************/
2259 static void call_trans2findfirst(connection_struct *conn,
2260 struct smb_request *req,
2261 char **pparams, int total_params,
2262 char **ppdata, int total_data,
2263 unsigned int max_data_bytes)
2265 /* We must be careful here that we don't return more than the
2266 allowed number of data bytes. If this means returning fewer than
2267 maxentries then so be it. We assume that the redirector has
2268 enough room for the fixed number of parameter bytes it has
2269 requested. */
2270 struct smb_filename *smb_dname = NULL;
2271 char *params = *pparams;
2272 char *pdata = *ppdata;
2273 char *data_end;
2274 uint32 dirtype;
2275 int maxentries;
2276 uint16 findfirst_flags;
2277 bool close_after_first;
2278 bool close_if_end;
2279 bool requires_resume_key;
2280 int info_level;
2281 char *directory = NULL;
2282 char *mask = NULL;
2283 char *p;
2284 int last_entry_off=0;
2285 int dptr_num = -1;
2286 int numentries = 0;
2287 int i;
2288 bool finished = False;
2289 bool dont_descend = False;
2290 bool out_of_space = False;
2291 int space_remaining;
2292 bool mask_contains_wcard = False;
2293 struct ea_list *ea_list = NULL;
2294 NTSTATUS ntstatus = NT_STATUS_OK;
2295 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
2296 TALLOC_CTX *ctx = talloc_tos();
2297 struct dptr_struct *dirptr = NULL;
2298 struct smbd_server_connection *sconn = req->sconn;
2299 uint32_t ucf_flags = (UCF_SAVE_LCOMP | UCF_ALWAYS_ALLOW_WCARD_LCOMP);
2300 bool backup_priv = false;
2302 if (total_params < 13) {
2303 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2304 goto out;
2307 dirtype = SVAL(params,0);
2308 maxentries = SVAL(params,2);
2309 findfirst_flags = SVAL(params,4);
2310 close_after_first = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE);
2311 close_if_end = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2312 requires_resume_key = (findfirst_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2313 backup_priv = ((findfirst_flags & FLAG_TRANS2_FIND_BACKUP_INTENT) &&
2314 security_token_has_privilege(get_current_nttok(conn),
2315 SEC_PRIV_BACKUP));
2317 info_level = SVAL(params,6);
2319 DEBUG(3,("call_trans2findfirst: dirtype = %x, maxentries = %d, close_after_first=%d, \
2320 close_if_end = %d requires_resume_key = %d backup_priv = %d level = 0x%x, max_data_bytes = %d\n",
2321 (unsigned int)dirtype, maxentries, close_after_first, close_if_end, requires_resume_key,
2322 (int)backup_priv,
2323 info_level, max_data_bytes));
2325 if (!maxentries) {
2326 /* W2K3 seems to treat zero as 1. */
2327 maxentries = 1;
2330 switch (info_level) {
2331 case SMB_FIND_INFO_STANDARD:
2332 case SMB_FIND_EA_SIZE:
2333 case SMB_FIND_EA_LIST:
2334 case SMB_FIND_FILE_DIRECTORY_INFO:
2335 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2336 case SMB_FIND_FILE_NAMES_INFO:
2337 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2338 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2339 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2340 break;
2341 case SMB_FIND_FILE_UNIX:
2342 case SMB_FIND_FILE_UNIX_INFO2:
2343 /* Always use filesystem for UNIX mtime query. */
2344 ask_sharemode = false;
2345 if (!lp_unix_extensions()) {
2346 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2347 goto out;
2349 ucf_flags |= UCF_UNIX_NAME_LOOKUP;
2350 break;
2351 default:
2352 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2353 goto out;
2356 srvstr_get_path_wcard(ctx, params, req->flags2, &directory,
2357 params+12, total_params - 12,
2358 STR_TERMINATE, &ntstatus, &mask_contains_wcard);
2359 if (!NT_STATUS_IS_OK(ntstatus)) {
2360 reply_nterror(req, ntstatus);
2361 goto out;
2364 if (backup_priv) {
2365 become_root();
2366 ntstatus = filename_convert_with_privilege(ctx,
2367 conn,
2368 req,
2369 directory,
2370 ucf_flags,
2371 &mask_contains_wcard,
2372 &smb_dname);
2373 } else {
2374 ntstatus = filename_convert(ctx, conn,
2375 req->flags2 & FLAGS2_DFS_PATHNAMES,
2376 directory,
2377 ucf_flags,
2378 &mask_contains_wcard,
2379 &smb_dname);
2382 if (!NT_STATUS_IS_OK(ntstatus)) {
2383 if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
2384 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2385 ERRSRV, ERRbadpath);
2386 goto out;
2388 reply_nterror(req, ntstatus);
2389 goto out;
2392 mask = smb_dname->original_lcomp;
2394 directory = smb_dname->base_name;
2396 p = strrchr_m(directory,'/');
2397 if(p == NULL) {
2398 /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
2399 if((directory[0] == '.') && (directory[1] == '\0')) {
2400 mask = talloc_strdup(ctx,"*");
2401 if (!mask) {
2402 reply_nterror(req, NT_STATUS_NO_MEMORY);
2403 goto out;
2405 mask_contains_wcard = True;
2407 } else {
2408 *p = 0;
2411 if (p == NULL || p == directory) {
2412 /* Ensure we don't have a directory name of "". */
2413 directory = talloc_strdup(talloc_tos(), ".");
2414 if (!directory) {
2415 reply_nterror(req, NT_STATUS_NO_MEMORY);
2416 goto out;
2420 DEBUG(5,("dir=%s, mask = %s\n",directory, mask));
2422 if (info_level == SMB_FIND_EA_LIST) {
2423 uint32 ea_size;
2425 if (total_data < 4) {
2426 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2427 goto out;
2430 ea_size = IVAL(pdata,0);
2431 if (ea_size != total_data) {
2432 DEBUG(4,("call_trans2findfirst: Rejecting EA request with incorrect \
2433 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2434 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2435 goto out;
2438 if (!lp_ea_support(SNUM(conn))) {
2439 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
2440 goto out;
2443 /* Pull out the list of names. */
2444 ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
2445 if (!ea_list) {
2446 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2447 goto out;
2451 *ppdata = (char *)SMB_REALLOC(
2452 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2453 if(*ppdata == NULL ) {
2454 reply_nterror(req, NT_STATUS_NO_MEMORY);
2455 goto out;
2457 pdata = *ppdata;
2458 data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2460 /* Realloc the params space */
2461 *pparams = (char *)SMB_REALLOC(*pparams, 10);
2462 if (*pparams == NULL) {
2463 reply_nterror(req, NT_STATUS_NO_MEMORY);
2464 goto out;
2466 params = *pparams;
2468 /* Save the wildcard match and attribs we are using on this directory -
2469 needed as lanman2 assumes these are being saved between calls */
2471 ntstatus = dptr_create(conn,
2472 req,
2473 NULL, /* fsp */
2474 directory,
2475 False,
2476 True,
2477 req->smbpid,
2478 mask,
2479 mask_contains_wcard,
2480 dirtype,
2481 &dirptr);
2483 if (!NT_STATUS_IS_OK(ntstatus)) {
2484 reply_nterror(req, ntstatus);
2485 goto out;
2488 if (backup_priv) {
2489 /* Remember this in case we have
2490 to do a findnext. */
2491 dptr_set_priv(dirptr);
2494 dptr_num = dptr_dnum(dirptr);
2495 DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
2497 /* Initialize per TRANS2_FIND_FIRST operation data */
2498 dptr_init_search_op(dirptr);
2500 /* We don't need to check for VOL here as this is returned by
2501 a different TRANS2 call. */
2503 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
2504 directory,lp_dontdescend(SNUM(conn))));
2505 if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
2506 dont_descend = True;
2508 p = pdata;
2509 space_remaining = max_data_bytes;
2510 out_of_space = False;
2512 for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
2513 bool got_exact_match = False;
2515 /* this is a heuristic to avoid seeking the dirptr except when
2516 absolutely necessary. It allows for a filename of about 40 chars */
2517 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2518 out_of_space = True;
2519 finished = False;
2520 } else {
2521 finished = !get_lanman2_dir_entry(ctx,
2522 conn,
2523 dirptr,
2524 req->flags2,
2525 mask,dirtype,info_level,
2526 requires_resume_key,dont_descend,
2527 ask_sharemode,
2528 &p,pdata,data_end,
2529 space_remaining, &out_of_space,
2530 &got_exact_match,
2531 &last_entry_off, ea_list);
2534 if (finished && out_of_space)
2535 finished = False;
2537 if (!finished && !out_of_space)
2538 numentries++;
2541 * As an optimisation if we know we aren't looking
2542 * for a wildcard name (ie. the name matches the wildcard exactly)
2543 * then we can finish on any (first) match.
2544 * This speeds up large directory searches. JRA.
2547 if(got_exact_match)
2548 finished = True;
2550 /* Ensure space_remaining never goes -ve. */
2551 if (PTR_DIFF(p,pdata) > max_data_bytes) {
2552 space_remaining = 0;
2553 out_of_space = true;
2554 } else {
2555 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2559 /* Check if we can close the dirptr */
2560 if(close_after_first || (finished && close_if_end)) {
2561 DEBUG(5,("call_trans2findfirst - (2) closing dptr_num %d\n", dptr_num));
2562 dptr_close(sconn, &dptr_num);
2566 * If there are no matching entries we must return ERRDOS/ERRbadfile -
2567 * from observation of NT. NB. This changes to ERRDOS,ERRnofiles if
2568 * the protocol level is less than NT1. Tested with smbclient. JRA.
2569 * This should fix the OS/2 client bug #2335.
2572 if(numentries == 0) {
2573 dptr_close(sconn, &dptr_num);
2574 if (get_Protocol() < PROTOCOL_NT1) {
2575 reply_force_doserror(req, ERRDOS, ERRnofiles);
2576 goto out;
2577 } else {
2578 reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
2579 ERRDOS, ERRbadfile);
2580 goto out;
2584 /* At this point pdata points to numentries directory entries. */
2586 /* Set up the return parameter block */
2587 SSVAL(params,0,dptr_num);
2588 SSVAL(params,2,numentries);
2589 SSVAL(params,4,finished);
2590 SSVAL(params,6,0); /* Never an EA error */
2591 SSVAL(params,8,last_entry_off);
2593 send_trans2_replies(conn, req, params, 10, pdata, PTR_DIFF(p,pdata),
2594 max_data_bytes);
2596 if ((! *directory) && dptr_path(sconn, dptr_num)) {
2597 directory = talloc_strdup(talloc_tos(),dptr_path(sconn, dptr_num));
2598 if (!directory) {
2599 reply_nterror(req, NT_STATUS_NO_MEMORY);
2603 DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2604 smb_fn_name(req->cmd),
2605 mask, directory, dirtype, numentries ) );
2608 * Force a name mangle here to ensure that the
2609 * mask as an 8.3 name is top of the mangled cache.
2610 * The reasons for this are subtle. Don't remove
2611 * this code unless you know what you are doing
2612 * (see PR#13758). JRA.
2615 if(!mangle_is_8_3_wildcards( mask, False, conn->params)) {
2616 char mangled_name[13];
2617 name_to_8_3(mask, mangled_name, True, conn->params);
2619 out:
2621 if (backup_priv) {
2622 unbecome_root();
2625 TALLOC_FREE(smb_dname);
2626 return;
2629 /****************************************************************************
2630 Reply to a TRANS2_FINDNEXT.
2631 ****************************************************************************/
2633 static void call_trans2findnext(connection_struct *conn,
2634 struct smb_request *req,
2635 char **pparams, int total_params,
2636 char **ppdata, int total_data,
2637 unsigned int max_data_bytes)
2639 /* We must be careful here that we don't return more than the
2640 allowed number of data bytes. If this means returning fewer than
2641 maxentries then so be it. We assume that the redirector has
2642 enough room for the fixed number of parameter bytes it has
2643 requested. */
2644 char *params = *pparams;
2645 char *pdata = *ppdata;
2646 char *data_end;
2647 int dptr_num;
2648 int maxentries;
2649 uint16 info_level;
2650 uint32 resume_key;
2651 uint16 findnext_flags;
2652 bool close_after_request;
2653 bool close_if_end;
2654 bool requires_resume_key;
2655 bool continue_bit;
2656 bool mask_contains_wcard = False;
2657 char *resume_name = NULL;
2658 const char *mask = NULL;
2659 const char *directory = NULL;
2660 char *p = NULL;
2661 uint16 dirtype;
2662 int numentries = 0;
2663 int i, last_entry_off=0;
2664 bool finished = False;
2665 bool dont_descend = False;
2666 bool out_of_space = False;
2667 int space_remaining;
2668 struct ea_list *ea_list = NULL;
2669 NTSTATUS ntstatus = NT_STATUS_OK;
2670 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
2671 TALLOC_CTX *ctx = talloc_tos();
2672 struct dptr_struct *dirptr;
2673 struct smbd_server_connection *sconn = req->sconn;
2674 bool backup_priv = false;
2676 if (total_params < 13) {
2677 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2678 return;
2681 dptr_num = SVAL(params,0);
2682 maxentries = SVAL(params,2);
2683 info_level = SVAL(params,4);
2684 resume_key = IVAL(params,6);
2685 findnext_flags = SVAL(params,10);
2686 close_after_request = (findnext_flags & FLAG_TRANS2_FIND_CLOSE);
2687 close_if_end = (findnext_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2688 requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2689 continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
2691 if (!continue_bit) {
2692 /* We only need resume_name if continue_bit is zero. */
2693 srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
2694 params+12,
2695 total_params - 12, STR_TERMINATE, &ntstatus,
2696 &mask_contains_wcard);
2697 if (!NT_STATUS_IS_OK(ntstatus)) {
2698 /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
2699 complain (it thinks we're asking for the directory above the shared
2700 path or an invalid name). Catch this as the resume name is only compared, never used in
2701 a file access. JRA. */
2702 srvstr_pull_talloc(ctx, params, req->flags2,
2703 &resume_name, params+12,
2704 total_params - 12,
2705 STR_TERMINATE);
2707 if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
2708 reply_nterror(req, ntstatus);
2709 return;
2714 DEBUG(3,("call_trans2findnext: dirhandle = %d, max_data_bytes = %d, maxentries = %d, \
2715 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
2716 resume_key = %d resume name = %s continue=%d level = %d\n",
2717 dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end,
2718 requires_resume_key, resume_key,
2719 resume_name ? resume_name : "(NULL)", continue_bit, info_level));
2721 if (!maxentries) {
2722 /* W2K3 seems to treat zero as 1. */
2723 maxentries = 1;
2726 switch (info_level) {
2727 case SMB_FIND_INFO_STANDARD:
2728 case SMB_FIND_EA_SIZE:
2729 case SMB_FIND_EA_LIST:
2730 case SMB_FIND_FILE_DIRECTORY_INFO:
2731 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2732 case SMB_FIND_FILE_NAMES_INFO:
2733 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2734 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2735 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2736 break;
2737 case SMB_FIND_FILE_UNIX:
2738 case SMB_FIND_FILE_UNIX_INFO2:
2739 /* Always use filesystem for UNIX mtime query. */
2740 ask_sharemode = false;
2741 if (!lp_unix_extensions()) {
2742 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2743 return;
2745 break;
2746 default:
2747 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2748 return;
2751 if (info_level == SMB_FIND_EA_LIST) {
2752 uint32 ea_size;
2754 if (total_data < 4) {
2755 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2756 return;
2759 ea_size = IVAL(pdata,0);
2760 if (ea_size != total_data) {
2761 DEBUG(4,("call_trans2findnext: Rejecting EA request with incorrect \
2762 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2764 return;
2767 if (!lp_ea_support(SNUM(conn))) {
2768 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
2769 return;
2772 /* Pull out the list of names. */
2773 ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
2774 if (!ea_list) {
2775 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2776 return;
2780 *ppdata = (char *)SMB_REALLOC(
2781 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2782 if(*ppdata == NULL) {
2783 reply_nterror(req, NT_STATUS_NO_MEMORY);
2784 return;
2787 pdata = *ppdata;
2788 data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2790 /* Realloc the params space */
2791 *pparams = (char *)SMB_REALLOC(*pparams, 6*SIZEOFWORD);
2792 if(*pparams == NULL ) {
2793 reply_nterror(req, NT_STATUS_NO_MEMORY);
2794 return;
2797 params = *pparams;
2799 /* Check that the dptr is valid */
2800 if(!(dirptr = dptr_fetch_lanman2(sconn, dptr_num))) {
2801 reply_nterror(req, STATUS_NO_MORE_FILES);
2802 return;
2805 directory = dptr_path(sconn, dptr_num);
2807 /* Get the wildcard mask from the dptr */
2808 if((mask = dptr_wcard(sconn, dptr_num))== NULL) {
2809 DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
2810 reply_nterror(req, STATUS_NO_MORE_FILES);
2811 return;
2814 /* Get the attr mask from the dptr */
2815 dirtype = dptr_attr(sconn, dptr_num);
2817 backup_priv = dptr_get_priv(dirptr);
2819 DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld) "
2820 "backup_priv = %d\n",
2821 dptr_num, mask, dirtype,
2822 (long)dirptr,
2823 dptr_TellDir(dirptr),
2824 (int)backup_priv));
2826 /* Initialize per TRANS2_FIND_NEXT operation data */
2827 dptr_init_search_op(dirptr);
2829 /* We don't need to check for VOL here as this is returned by
2830 a different TRANS2 call. */
2832 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
2833 directory,lp_dontdescend(SNUM(conn))));
2834 if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
2835 dont_descend = True;
2837 p = pdata;
2838 space_remaining = max_data_bytes;
2839 out_of_space = False;
2841 if (backup_priv) {
2842 become_root();
2846 * Seek to the correct position. We no longer use the resume key but
2847 * depend on the last file name instead.
2850 if(!continue_bit && resume_name && *resume_name) {
2851 SMB_STRUCT_STAT st;
2853 long current_pos = 0;
2855 * Remember, name_to_8_3 is called by
2856 * get_lanman2_dir_entry(), so the resume name
2857 * could be mangled. Ensure we check the unmangled name.
2860 if (mangle_is_mangled(resume_name, conn->params)) {
2861 char *new_resume_name = NULL;
2862 mangle_lookup_name_from_8_3(ctx,
2863 resume_name,
2864 &new_resume_name,
2865 conn->params);
2866 if (new_resume_name) {
2867 resume_name = new_resume_name;
2872 * Fix for NT redirector problem triggered by resume key indexes
2873 * changing between directory scans. We now return a resume key of 0
2874 * and instead look for the filename to continue from (also given
2875 * to us by NT/95/smbfs/smbclient). If no other scans have been done between the
2876 * findfirst/findnext (as is usual) then the directory pointer
2877 * should already be at the correct place.
2880 finished = !dptr_SearchDir(dirptr, resume_name, &current_pos, &st);
2881 } /* end if resume_name && !continue_bit */
2883 for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
2884 bool got_exact_match = False;
2886 /* this is a heuristic to avoid seeking the dirptr except when
2887 absolutely necessary. It allows for a filename of about 40 chars */
2888 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2889 out_of_space = True;
2890 finished = False;
2891 } else {
2892 finished = !get_lanman2_dir_entry(ctx,
2893 conn,
2894 dirptr,
2895 req->flags2,
2896 mask,dirtype,info_level,
2897 requires_resume_key,dont_descend,
2898 ask_sharemode,
2899 &p,pdata,data_end,
2900 space_remaining, &out_of_space,
2901 &got_exact_match,
2902 &last_entry_off, ea_list);
2905 if (finished && out_of_space)
2906 finished = False;
2908 if (!finished && !out_of_space)
2909 numentries++;
2912 * As an optimisation if we know we aren't looking
2913 * for a wildcard name (ie. the name matches the wildcard exactly)
2914 * then we can finish on any (first) match.
2915 * This speeds up large directory searches. JRA.
2918 if(got_exact_match)
2919 finished = True;
2921 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2924 DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2925 smb_fn_name(req->cmd),
2926 mask, directory, dirtype, numentries ) );
2928 /* Check if we can close the dirptr */
2929 if(close_after_request || (finished && close_if_end)) {
2930 DEBUG(5,("call_trans2findnext: closing dptr_num = %d\n", dptr_num));
2931 dptr_close(sconn, &dptr_num); /* This frees up the saved mask */
2934 if (backup_priv) {
2935 unbecome_root();
2938 /* Set up the return parameter block */
2939 SSVAL(params,0,numentries);
2940 SSVAL(params,2,finished);
2941 SSVAL(params,4,0); /* Never an EA error */
2942 SSVAL(params,6,last_entry_off);
2944 send_trans2_replies(conn, req, params, 8, pdata, PTR_DIFF(p,pdata),
2945 max_data_bytes);
2947 return;
2950 unsigned char *create_volume_objectid(connection_struct *conn, unsigned char objid[16])
2952 E_md4hash(lp_servicename(SNUM(conn)),objid);
2953 return objid;
2956 static void samba_extended_info_version(struct smb_extended_info *extended_info)
2958 SMB_ASSERT(extended_info != NULL);
2960 extended_info->samba_magic = SAMBA_EXTENDED_INFO_MAGIC;
2961 extended_info->samba_version = ((SAMBA_VERSION_MAJOR & 0xff) << 24)
2962 | ((SAMBA_VERSION_MINOR & 0xff) << 16)
2963 | ((SAMBA_VERSION_RELEASE & 0xff) << 8);
2964 #ifdef SAMBA_VERSION_REVISION
2965 extended_info->samba_version |= (tolower(*SAMBA_VERSION_REVISION) - 'a' + 1) & 0xff;
2966 #endif
2967 extended_info->samba_subversion = 0;
2968 #ifdef SAMBA_VERSION_RC_RELEASE
2969 extended_info->samba_subversion |= (SAMBA_VERSION_RC_RELEASE & 0xff) << 24;
2970 #else
2971 #ifdef SAMBA_VERSION_PRE_RELEASE
2972 extended_info->samba_subversion |= (SAMBA_VERSION_PRE_RELEASE & 0xff) << 16;
2973 #endif
2974 #endif
2975 #ifdef SAMBA_VERSION_VENDOR_PATCH
2976 extended_info->samba_subversion |= (SAMBA_VERSION_VENDOR_PATCH & 0xffff);
2977 #endif
2978 extended_info->samba_gitcommitdate = 0;
2979 #ifdef SAMBA_VERSION_COMMIT_TIME
2980 unix_to_nt_time(&extended_info->samba_gitcommitdate, SAMBA_VERSION_COMMIT_TIME);
2981 #endif
2983 memset(extended_info->samba_version_string, 0,
2984 sizeof(extended_info->samba_version_string));
2986 snprintf (extended_info->samba_version_string,
2987 sizeof(extended_info->samba_version_string),
2988 "%s", samba_version_string());
2991 NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
2992 TALLOC_CTX *mem_ctx,
2993 uint16_t info_level,
2994 uint16_t flags2,
2995 unsigned int max_data_bytes,
2996 char **ppdata,
2997 int *ret_data_len)
2999 char *pdata, *end_data;
3000 int data_len = 0, len;
3001 const char *vname = volume_label(SNUM(conn));
3002 int snum = SNUM(conn);
3003 char *fstype = lp_fstype(SNUM(conn));
3004 uint32 additional_flags = 0;
3005 struct smb_filename smb_fname_dot;
3006 SMB_STRUCT_STAT st;
3008 if (IS_IPC(conn)) {
3009 if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
3010 DEBUG(0,("smbd_do_qfsinfo: not an allowed "
3011 "info level (0x%x) on IPC$.\n",
3012 (unsigned int)info_level));
3013 return NT_STATUS_ACCESS_DENIED;
3017 DEBUG(3,("smbd_do_qfsinfo: level = %d\n", info_level));
3019 ZERO_STRUCT(smb_fname_dot);
3020 smb_fname_dot.base_name = discard_const_p(char, ".");
3022 if(SMB_VFS_STAT(conn, &smb_fname_dot) != 0) {
3023 DEBUG(2,("stat of . failed (%s)\n", strerror(errno)));
3024 return map_nt_error_from_unix(errno);
3027 st = smb_fname_dot.st;
3029 *ppdata = (char *)SMB_REALLOC(
3030 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
3031 if (*ppdata == NULL) {
3032 return NT_STATUS_NO_MEMORY;
3035 pdata = *ppdata;
3036 memset((char *)pdata,'\0',max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
3037 end_data = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
3039 switch (info_level) {
3040 case SMB_INFO_ALLOCATION:
3042 uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3043 data_len = 18;
3044 if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3045 return map_nt_error_from_unix(errno);
3048 block_size = lp_block_size(snum);
3049 if (bsize < block_size) {
3050 uint64_t factor = block_size/bsize;
3051 bsize = block_size;
3052 dsize /= factor;
3053 dfree /= factor;
3055 if (bsize > block_size) {
3056 uint64_t factor = bsize/block_size;
3057 bsize = block_size;
3058 dsize *= factor;
3059 dfree *= factor;
3061 bytes_per_sector = 512;
3062 sectors_per_unit = bsize/bytes_per_sector;
3064 DEBUG(5,("smbd_do_qfsinfo : SMB_INFO_ALLOCATION id=%x, bsize=%u, cSectorUnit=%u, \
3065 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_ex_dev, (unsigned int)bsize, (unsigned int)sectors_per_unit,
3066 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3068 SIVAL(pdata,l1_idFileSystem,st.st_ex_dev);
3069 SIVAL(pdata,l1_cSectorUnit,sectors_per_unit);
3070 SIVAL(pdata,l1_cUnit,dsize);
3071 SIVAL(pdata,l1_cUnitAvail,dfree);
3072 SSVAL(pdata,l1_cbSector,bytes_per_sector);
3073 break;
3076 case SMB_INFO_VOLUME:
3077 /* Return volume name */
3079 * Add volume serial number - hash of a combination of
3080 * the called hostname and the service name.
3082 SIVAL(pdata,0,str_checksum(lp_servicename(snum)) ^ (str_checksum(get_local_machine_name())<<16) );
3084 * Win2k3 and previous mess this up by sending a name length
3085 * one byte short. I believe only older clients (OS/2 Win9x) use
3086 * this call so try fixing this by adding a terminating null to
3087 * the pushed string. The change here was adding the STR_TERMINATE. JRA.
3089 len = srvstr_push(
3090 pdata, flags2,
3091 pdata+l2_vol_szVolLabel, vname,
3092 PTR_DIFF(end_data, pdata+l2_vol_szVolLabel),
3093 STR_NOALIGN|STR_TERMINATE);
3094 SCVAL(pdata,l2_vol_cch,len);
3095 data_len = l2_vol_szVolLabel + len;
3096 DEBUG(5,("smbd_do_qfsinfo : time = %x, namelen = %d, name = %s\n",
3097 (unsigned)convert_timespec_to_time_t(st.st_ex_ctime),
3098 len, vname));
3099 break;
3101 case SMB_QUERY_FS_ATTRIBUTE_INFO:
3102 case SMB_FS_ATTRIBUTE_INFORMATION:
3104 additional_flags = 0;
3105 #if defined(HAVE_SYS_QUOTAS)
3106 additional_flags |= FILE_VOLUME_QUOTAS;
3107 #endif
3109 if(lp_nt_acl_support(SNUM(conn))) {
3110 additional_flags |= FILE_PERSISTENT_ACLS;
3113 /* Capabilities are filled in at connection time through STATVFS call */
3114 additional_flags |= conn->fs_capabilities;
3115 additional_flags |= lp_parm_int(conn->params->service,
3116 "share", "fake_fscaps",
3119 SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
3120 FILE_SUPPORTS_OBJECT_IDS|FILE_UNICODE_ON_DISK|
3121 additional_flags); /* FS ATTRIBUTES */
3123 SIVAL(pdata,4,255); /* Max filename component length */
3124 /* NOTE! the fstype must *not* be null terminated or win98 won't recognise it
3125 and will think we can't do long filenames */
3126 len = srvstr_push(pdata, flags2, pdata+12, fstype,
3127 PTR_DIFF(end_data, pdata+12),
3128 STR_UNICODE);
3129 SIVAL(pdata,8,len);
3130 data_len = 12 + len;
3131 break;
3133 case SMB_QUERY_FS_LABEL_INFO:
3134 case SMB_FS_LABEL_INFORMATION:
3135 len = srvstr_push(pdata, flags2, pdata+4, vname,
3136 PTR_DIFF(end_data, pdata+4), 0);
3137 data_len = 4 + len;
3138 SIVAL(pdata,0,len);
3139 break;
3141 case SMB_QUERY_FS_VOLUME_INFO:
3142 case SMB_FS_VOLUME_INFORMATION:
3145 * Add volume serial number - hash of a combination of
3146 * the called hostname and the service name.
3148 SIVAL(pdata,8,str_checksum(lp_servicename(snum)) ^
3149 (str_checksum(get_local_machine_name())<<16));
3151 /* Max label len is 32 characters. */
3152 len = srvstr_push(pdata, flags2, pdata+18, vname,
3153 PTR_DIFF(end_data, pdata+18),
3154 STR_UNICODE);
3155 SIVAL(pdata,12,len);
3156 data_len = 18+len;
3158 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_VOLUME_INFO namelen = %d, vol=%s serv=%s\n",
3159 (int)strlen(vname),vname, lp_servicename(snum)));
3160 break;
3162 case SMB_QUERY_FS_SIZE_INFO:
3163 case SMB_FS_SIZE_INFORMATION:
3165 uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3166 data_len = 24;
3167 if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3168 return map_nt_error_from_unix(errno);
3170 block_size = lp_block_size(snum);
3171 if (bsize < block_size) {
3172 uint64_t factor = block_size/bsize;
3173 bsize = block_size;
3174 dsize /= factor;
3175 dfree /= factor;
3177 if (bsize > block_size) {
3178 uint64_t factor = bsize/block_size;
3179 bsize = block_size;
3180 dsize *= factor;
3181 dfree *= factor;
3183 bytes_per_sector = 512;
3184 sectors_per_unit = bsize/bytes_per_sector;
3185 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_SIZE_INFO bsize=%u, cSectorUnit=%u, \
3186 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
3187 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3188 SBIG_UINT(pdata,0,dsize);
3189 SBIG_UINT(pdata,8,dfree);
3190 SIVAL(pdata,16,sectors_per_unit);
3191 SIVAL(pdata,20,bytes_per_sector);
3192 break;
3195 case SMB_FS_FULL_SIZE_INFORMATION:
3197 uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3198 data_len = 32;
3199 if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3200 return map_nt_error_from_unix(errno);
3202 block_size = lp_block_size(snum);
3203 if (bsize < block_size) {
3204 uint64_t factor = block_size/bsize;
3205 bsize = block_size;
3206 dsize /= factor;
3207 dfree /= factor;
3209 if (bsize > block_size) {
3210 uint64_t factor = bsize/block_size;
3211 bsize = block_size;
3212 dsize *= factor;
3213 dfree *= factor;
3215 bytes_per_sector = 512;
3216 sectors_per_unit = bsize/bytes_per_sector;
3217 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_FULL_SIZE_INFO bsize=%u, cSectorUnit=%u, \
3218 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
3219 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3220 SBIG_UINT(pdata,0,dsize); /* Total Allocation units. */
3221 SBIG_UINT(pdata,8,dfree); /* Caller available allocation units. */
3222 SBIG_UINT(pdata,16,dfree); /* Actual available allocation units. */
3223 SIVAL(pdata,24,sectors_per_unit); /* Sectors per allocation unit. */
3224 SIVAL(pdata,28,bytes_per_sector); /* Bytes per sector. */
3225 break;
3228 case SMB_QUERY_FS_DEVICE_INFO:
3229 case SMB_FS_DEVICE_INFORMATION:
3231 uint32_t characteristics = FILE_DEVICE_IS_MOUNTED;
3233 if (!CAN_WRITE(conn)) {
3234 characteristics |= FILE_READ_ONLY_DEVICE;
3236 data_len = 8;
3237 SIVAL(pdata,0,FILE_DEVICE_DISK); /* dev type */
3238 SIVAL(pdata,4,characteristics);
3239 break;
3242 #ifdef HAVE_SYS_QUOTAS
3243 case SMB_FS_QUOTA_INFORMATION:
3245 * what we have to send --metze:
3247 * Unknown1: 24 NULL bytes
3248 * Soft Quota Treshold: 8 bytes seems like uint64_t or so
3249 * Hard Quota Limit: 8 bytes seems like uint64_t or so
3250 * Quota Flags: 2 byte :
3251 * Unknown3: 6 NULL bytes
3253 * 48 bytes total
3255 * details for Quota Flags:
3257 * 0x0020 Log Limit: log if the user exceeds his Hard Quota
3258 * 0x0010 Log Warn: log if the user exceeds his Soft Quota
3259 * 0x0002 Deny Disk: deny disk access when the user exceeds his Hard Quota
3260 * 0x0001 Enable Quotas: enable quota for this fs
3264 /* we need to fake up a fsp here,
3265 * because its not send in this call
3267 files_struct fsp;
3268 SMB_NTQUOTA_STRUCT quotas;
3270 ZERO_STRUCT(fsp);
3271 ZERO_STRUCT(quotas);
3273 fsp.conn = conn;
3274 fsp.fnum = FNUM_FIELD_INVALID;
3276 /* access check */
3277 if (get_current_uid(conn) != 0) {
3278 DEBUG(0,("set_user_quota: access_denied "
3279 "service [%s] user [%s]\n",
3280 lp_servicename(SNUM(conn)),
3281 conn->session_info->unix_info->unix_name));
3282 return NT_STATUS_ACCESS_DENIED;
3285 if (vfs_get_ntquota(&fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
3286 DEBUG(0,("vfs_get_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
3287 return map_nt_error_from_unix(errno);
3290 data_len = 48;
3292 DEBUG(10,("SMB_FS_QUOTA_INFORMATION: for service [%s]\n",
3293 lp_servicename(SNUM(conn))));
3295 /* Unknown1 24 NULL bytes*/
3296 SBIG_UINT(pdata,0,(uint64_t)0);
3297 SBIG_UINT(pdata,8,(uint64_t)0);
3298 SBIG_UINT(pdata,16,(uint64_t)0);
3300 /* Default Soft Quota 8 bytes */
3301 SBIG_UINT(pdata,24,quotas.softlim);
3303 /* Default Hard Quota 8 bytes */
3304 SBIG_UINT(pdata,32,quotas.hardlim);
3306 /* Quota flag 2 bytes */
3307 SSVAL(pdata,40,quotas.qflags);
3309 /* Unknown3 6 NULL bytes */
3310 SSVAL(pdata,42,0);
3311 SIVAL(pdata,44,0);
3313 break;
3315 #endif /* HAVE_SYS_QUOTAS */
3316 case SMB_FS_OBJECTID_INFORMATION:
3318 unsigned char objid[16];
3319 struct smb_extended_info extended_info;
3320 memcpy(pdata,create_volume_objectid(conn, objid),16);
3321 samba_extended_info_version (&extended_info);
3322 SIVAL(pdata,16,extended_info.samba_magic);
3323 SIVAL(pdata,20,extended_info.samba_version);
3324 SIVAL(pdata,24,extended_info.samba_subversion);
3325 SBIG_UINT(pdata,28,extended_info.samba_gitcommitdate);
3326 memcpy(pdata+36,extended_info.samba_version_string,28);
3327 data_len = 64;
3328 break;
3332 * Query the version and capabilities of the CIFS UNIX extensions
3333 * in use.
3336 case SMB_QUERY_CIFS_UNIX_INFO:
3338 bool large_write = lp_min_receive_file_size() &&
3339 !srv_is_signing_active(conn->sconn);
3340 bool large_read = !srv_is_signing_active(conn->sconn);
3341 int encrypt_caps = 0;
3343 if (!lp_unix_extensions()) {
3344 return NT_STATUS_INVALID_LEVEL;
3347 switch (conn->encrypt_level) {
3348 case 0:
3349 encrypt_caps = 0;
3350 break;
3351 case 1:
3352 case Auto:
3353 encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP;
3354 break;
3355 case Required:
3356 encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP|
3357 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP;
3358 large_write = false;
3359 large_read = false;
3360 break;
3363 data_len = 12;
3364 SSVAL(pdata,0,CIFS_UNIX_MAJOR_VERSION);
3365 SSVAL(pdata,2,CIFS_UNIX_MINOR_VERSION);
3367 /* We have POSIX ACLs, pathname, encryption,
3368 * large read/write, and locking capability. */
3370 SBIG_UINT(pdata,4,((uint64_t)(
3371 CIFS_UNIX_POSIX_ACLS_CAP|
3372 CIFS_UNIX_POSIX_PATHNAMES_CAP|
3373 CIFS_UNIX_FCNTL_LOCKS_CAP|
3374 CIFS_UNIX_EXTATTR_CAP|
3375 CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP|
3376 encrypt_caps|
3377 (large_read ? CIFS_UNIX_LARGE_READ_CAP : 0) |
3378 (large_write ?
3379 CIFS_UNIX_LARGE_WRITE_CAP : 0))));
3380 break;
3383 case SMB_QUERY_POSIX_FS_INFO:
3385 int rc;
3386 vfs_statvfs_struct svfs;
3388 if (!lp_unix_extensions()) {
3389 return NT_STATUS_INVALID_LEVEL;
3392 rc = SMB_VFS_STATVFS(conn, ".", &svfs);
3394 if (!rc) {
3395 data_len = 56;
3396 SIVAL(pdata,0,svfs.OptimalTransferSize);
3397 SIVAL(pdata,4,svfs.BlockSize);
3398 SBIG_UINT(pdata,8,svfs.TotalBlocks);
3399 SBIG_UINT(pdata,16,svfs.BlocksAvail);
3400 SBIG_UINT(pdata,24,svfs.UserBlocksAvail);
3401 SBIG_UINT(pdata,32,svfs.TotalFileNodes);
3402 SBIG_UINT(pdata,40,svfs.FreeFileNodes);
3403 SBIG_UINT(pdata,48,svfs.FsIdentifier);
3404 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_POSIX_FS_INFO succsessful\n"));
3405 #ifdef EOPNOTSUPP
3406 } else if (rc == EOPNOTSUPP) {
3407 return NT_STATUS_INVALID_LEVEL;
3408 #endif /* EOPNOTSUPP */
3409 } else {
3410 DEBUG(0,("vfs_statvfs() failed for service [%s]\n",lp_servicename(SNUM(conn))));
3411 return NT_STATUS_DOS(ERRSRV, ERRerror);
3413 break;
3416 case SMB_QUERY_POSIX_WHOAMI:
3418 uint32_t flags = 0;
3419 uint32_t sid_bytes;
3420 int i;
3422 if (!lp_unix_extensions()) {
3423 return NT_STATUS_INVALID_LEVEL;
3426 if (max_data_bytes < 40) {
3427 return NT_STATUS_BUFFER_TOO_SMALL;
3430 if (security_session_user_level(conn->session_info, NULL) < SECURITY_USER) {
3431 flags |= SMB_WHOAMI_GUEST;
3434 /* NOTE: 8 bytes for UID/GID, irrespective of native
3435 * platform size. This matches
3436 * SMB_QUERY_FILE_UNIX_BASIC and friends.
3438 data_len = 4 /* flags */
3439 + 4 /* flag mask */
3440 + 8 /* uid */
3441 + 8 /* gid */
3442 + 4 /* ngroups */
3443 + 4 /* num_sids */
3444 + 4 /* SID bytes */
3445 + 4 /* pad/reserved */
3446 + (conn->session_info->unix_token->ngroups * 8)
3447 /* groups list */
3448 + (conn->session_info->security_token->num_sids *
3449 SID_MAX_SIZE)
3450 /* SID list */;
3452 SIVAL(pdata, 0, flags);
3453 SIVAL(pdata, 4, SMB_WHOAMI_MASK);
3454 SBIG_UINT(pdata, 8,
3455 (uint64_t)conn->session_info->unix_token->uid);
3456 SBIG_UINT(pdata, 16,
3457 (uint64_t)conn->session_info->unix_token->gid);
3460 if (data_len >= max_data_bytes) {
3461 /* Potential overflow, skip the GIDs and SIDs. */
3463 SIVAL(pdata, 24, 0); /* num_groups */
3464 SIVAL(pdata, 28, 0); /* num_sids */
3465 SIVAL(pdata, 32, 0); /* num_sid_bytes */
3466 SIVAL(pdata, 36, 0); /* reserved */
3468 data_len = 40;
3469 break;
3472 SIVAL(pdata, 24, conn->session_info->unix_token->ngroups);
3473 SIVAL(pdata, 28, conn->session_info->security_token->num_sids);
3475 /* We walk the SID list twice, but this call is fairly
3476 * infrequent, and I don't expect that it's performance
3477 * sensitive -- jpeach
3479 for (i = 0, sid_bytes = 0;
3480 i < conn->session_info->security_token->num_sids; ++i) {
3481 sid_bytes += ndr_size_dom_sid(
3482 &conn->session_info->security_token->sids[i],
3486 /* SID list byte count */
3487 SIVAL(pdata, 32, sid_bytes);
3489 /* 4 bytes pad/reserved - must be zero */
3490 SIVAL(pdata, 36, 0);
3491 data_len = 40;
3493 /* GID list */
3494 for (i = 0; i < conn->session_info->unix_token->ngroups; ++i) {
3495 SBIG_UINT(pdata, data_len,
3496 (uint64_t)conn->session_info->unix_token->groups[i]);
3497 data_len += 8;
3500 /* SID list */
3501 for (i = 0;
3502 i < conn->session_info->security_token->num_sids; ++i) {
3503 int sid_len = ndr_size_dom_sid(
3504 &conn->session_info->security_token->sids[i],
3507 sid_linearize(pdata + data_len, sid_len,
3508 &conn->session_info->security_token->sids[i]);
3509 data_len += sid_len;
3512 break;
3515 case SMB_MAC_QUERY_FS_INFO:
3517 * Thursby MAC extension... ONLY on NTFS filesystems
3518 * once we do streams then we don't need this
3520 if (strequal(lp_fstype(SNUM(conn)),"NTFS")) {
3521 data_len = 88;
3522 SIVAL(pdata,84,0x100); /* Don't support mac... */
3523 break;
3525 /* drop through */
3526 default:
3527 return NT_STATUS_INVALID_LEVEL;
3530 *ret_data_len = data_len;
3531 return NT_STATUS_OK;
3534 /****************************************************************************
3535 Reply to a TRANS2_QFSINFO (query filesystem info).
3536 ****************************************************************************/
3538 static void call_trans2qfsinfo(connection_struct