44c457cae5d14395a16acc4bf12a053bdd57879b
[sfrench/samba-autobuild/.git] / source3 / smbd / trans2.c
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
9
10    Extensively modified by Andrew Tridgell, 1995
11
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.
16
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.
21
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/>.
24 */
25
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"
41
42 #define DIR_ENTRY_SAFETY_MARGIN 4096
43
44 static char *store_file_unix_basic(connection_struct *conn,
45                                 char *pdata,
46                                 files_struct *fsp,
47                                 const SMB_STRUCT_STAT *psbuf);
48
49 static char *store_file_unix_basic_info2(connection_struct *conn,
50                                 char *pdata,
51                                 files_struct *fsp,
52                                 const SMB_STRUCT_STAT *psbuf);
53
54 /********************************************************************
55  The canonical "check access" based on object handle or path function.
56 ********************************************************************/
57
58 NTSTATUS check_access(connection_struct *conn,
59                                 files_struct *fsp,
60                                 const struct smb_filename *smb_fname,
61                                 uint32_t access_mask)
62 {
63         if (fsp) {
64                 if (!(fsp->access_mask & access_mask)) {
65                         return NT_STATUS_ACCESS_DENIED;
66                 }
67         } else {
68                 NTSTATUS status = smbd_check_access_rights(conn,
69                                         smb_fname,
70                                         false,
71                                         access_mask);
72                 if (!NT_STATUS_IS_OK(status)) {
73                         return status;
74                 }
75         }
76         return NT_STATUS_OK;
77 }
78
79 /********************************************************************
80  Roundup a value to the nearest allocation roundup size boundary.
81  Only do this for Windows clients.
82 ********************************************************************/
83
84 uint64_t smb_roundup(connection_struct *conn, uint64_t val)
85 {
86         uint64_t rval = lp_allocation_roundup_size(SNUM(conn));
87
88         /* Only roundup for Windows clients. */
89         enum remote_arch_types ra_type = get_remote_arch();
90         if (rval && (ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
91                 val = SMB_ROUNDUP(val,rval);
92         }
93         return val;
94 }
95
96 /********************************************************************
97  Create a 64 bit FileIndex. If the file is on the same device as
98  the root of the share, just return the 64-bit inode. If it isn't,
99  mangle as we used to do.
100 ********************************************************************/
101
102 uint64_t get_FileIndex(connection_struct *conn, const SMB_STRUCT_STAT *psbuf)
103 {
104         uint64_t file_index;
105         if (conn->base_share_dev == psbuf->st_ex_dev) {
106                 return (uint64_t)psbuf->st_ex_ino;
107         }
108         file_index = ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
109         file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
110         return file_index;
111 }
112
113 /****************************************************************************
114  Utility functions for dealing with extended attributes.
115 ****************************************************************************/
116
117 /****************************************************************************
118  Refuse to allow clients to overwrite our private xattrs.
119 ****************************************************************************/
120
121 static bool samba_private_attr_name(const char *unix_ea_name)
122 {
123         static const char * const prohibited_ea_names[] = {
124                 SAMBA_POSIX_INHERITANCE_EA_NAME,
125                 SAMBA_XATTR_DOS_ATTRIB,
126                 SAMBA_XATTR_MARKER,
127                 XATTR_NTACL_NAME,
128                 NULL
129         };
130
131         int i;
132
133         for (i = 0; prohibited_ea_names[i]; i++) {
134                 if (strequal( prohibited_ea_names[i], unix_ea_name))
135                         return true;
136         }
137         if (strncasecmp_m(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
138                         strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) == 0) {
139                 return true;
140         }
141         return false;
142 }
143
144 /****************************************************************************
145  Get one EA value. Fill in a struct ea_struct.
146 ****************************************************************************/
147
148 NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
149                       files_struct *fsp, const char *fname,
150                       const char *ea_name, struct ea_struct *pea)
151 {
152         /* Get the value of this xattr. Max size is 64k. */
153         size_t attr_size = 256;
154         char *val = NULL;
155         ssize_t sizeret;
156
157  again:
158
159         val = talloc_realloc(mem_ctx, val, char, attr_size);
160         if (!val) {
161                 return NT_STATUS_NO_MEMORY;
162         }
163
164         if (fsp && fsp->fh->fd != -1) {
165                 sizeret = SMB_VFS_FGETXATTR(fsp, ea_name, val, attr_size);
166         } else {
167                 sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
168         }
169
170         if (sizeret == -1 && errno == ERANGE && attr_size != 65536) {
171                 attr_size = 65536;
172                 goto again;
173         }
174
175         if (sizeret == -1) {
176                 return map_nt_error_from_unix(errno);
177         }
178
179         DEBUG(10,("get_ea_value: EA %s is of length %u\n", ea_name, (unsigned int)sizeret));
180         dump_data(10, (uint8 *)val, sizeret);
181
182         pea->flags = 0;
183         if (strnequal(ea_name, "user.", 5)) {
184                 pea->name = talloc_strdup(mem_ctx, &ea_name[5]);
185         } else {
186                 pea->name = talloc_strdup(mem_ctx, ea_name);
187         }
188         if (pea->name == NULL) {
189                 TALLOC_FREE(val);
190                 return NT_STATUS_NO_MEMORY;
191         }
192         pea->value.data = (unsigned char *)val;
193         pea->value.length = (size_t)sizeret;
194         return NT_STATUS_OK;
195 }
196
197 NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
198                                 files_struct *fsp, const char *fname,
199                                 char ***pnames, size_t *pnum_names)
200 {
201         /* Get a list of all xattrs. Max namesize is 64k. */
202         size_t ea_namelist_size = 1024;
203         char *ea_namelist = NULL;
204
205         char *p;
206         char **names, **tmp;
207         size_t num_names;
208         ssize_t sizeret = -1;
209
210         if (!lp_ea_support(SNUM(conn))) {
211                 if (pnames) {
212                         *pnames = NULL;
213                 }
214                 *pnum_names = 0;
215                 return NT_STATUS_OK;
216         }
217
218         /*
219          * TALLOC the result early to get the talloc hierarchy right.
220          */
221
222         names = talloc_array(mem_ctx, char *, 1);
223         if (names == NULL) {
224                 DEBUG(0, ("talloc failed\n"));
225                 return NT_STATUS_NO_MEMORY;
226         }
227
228         while (ea_namelist_size <= 65536) {
229
230                 ea_namelist = talloc_realloc(
231                         names, ea_namelist, char, ea_namelist_size);
232                 if (ea_namelist == NULL) {
233                         DEBUG(0, ("talloc failed\n"));
234                         TALLOC_FREE(names);
235                         return NT_STATUS_NO_MEMORY;
236                 }
237
238                 if (fsp && fsp->fh->fd != -1) {
239                         sizeret = SMB_VFS_FLISTXATTR(fsp, ea_namelist,
240                                                      ea_namelist_size);
241                 } else {
242                         sizeret = SMB_VFS_LISTXATTR(conn, fname, ea_namelist,
243                                                     ea_namelist_size);
244                 }
245
246                 if ((sizeret == -1) && (errno == ERANGE)) {
247                         ea_namelist_size *= 2;
248                 }
249                 else {
250                         break;
251                 }
252         }
253
254         if (sizeret == -1) {
255                 TALLOC_FREE(names);
256                 return map_nt_error_from_unix(errno);
257         }
258
259         DEBUG(10, ("get_ea_list_from_file: ea_namelist size = %u\n",
260                    (unsigned int)sizeret));
261
262         if (sizeret == 0) {
263                 TALLOC_FREE(names);
264                 if (pnames) {
265                         *pnames = NULL;
266                 }
267                 *pnum_names = 0;
268                 return NT_STATUS_OK;
269         }
270
271         /*
272          * Ensure the result is 0-terminated
273          */
274
275         if (ea_namelist[sizeret-1] != '\0') {
276                 TALLOC_FREE(names);
277                 return NT_STATUS_INTERNAL_ERROR;
278         }
279
280         /*
281          * count the names
282          */
283         num_names = 0;
284
285         for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
286                 num_names += 1;
287         }
288
289         tmp = talloc_realloc(mem_ctx, names, char *, num_names);
290         if (tmp == NULL) {
291                 DEBUG(0, ("talloc failed\n"));
292                 TALLOC_FREE(names);
293                 return NT_STATUS_NO_MEMORY;
294         }
295
296         names = tmp;
297         num_names = 0;
298
299         for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
300                 names[num_names++] = p;
301         }
302
303         if (pnames) {
304                 *pnames = names;
305         } else {
306                 TALLOC_FREE(names);
307         }
308         *pnum_names = num_names;
309         return NT_STATUS_OK;
310 }
311
312 /****************************************************************************
313  Return a linked list of the total EA's. Plus the total size
314 ****************************************************************************/
315
316 static NTSTATUS get_ea_list_from_file_path(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
317                                       const char *fname, size_t *pea_total_len, struct ea_list **ea_list)
318 {
319         /* Get a list of all xattrs. Max namesize is 64k. */
320         size_t i, num_names;
321         char **names;
322         struct ea_list *ea_list_head = NULL;
323         NTSTATUS status;
324
325         *pea_total_len = 0;
326
327         status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
328                                         &names, &num_names);
329
330         if (!NT_STATUS_IS_OK(status)) {
331                 return status;
332         }
333
334         if (num_names == 0) {
335                 *ea_list = NULL;
336                 return NT_STATUS_OK;
337         }
338
339         for (i=0; i<num_names; i++) {
340                 struct ea_list *listp;
341                 fstring dos_ea_name;
342
343                 if (strnequal(names[i], "system.", 7)
344                     || samba_private_attr_name(names[i]))
345                         continue;
346
347                 listp = talloc(mem_ctx, struct ea_list);
348                 if (listp == NULL) {
349                         return NT_STATUS_NO_MEMORY;
350                 }
351
352                 status = get_ea_value(mem_ctx, conn, fsp,
353                                       fname, names[i],
354                                       &listp->ea);
355
356                 if (!NT_STATUS_IS_OK(status)) {
357                         return status;
358                 }
359
360                 push_ascii_fstring(dos_ea_name, listp->ea.name);
361
362                 *pea_total_len +=
363                         4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
364
365                 DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len "
366                           "= %u\n", (unsigned int)*pea_total_len, dos_ea_name,
367                           (unsigned int)listp->ea.value.length));
368
369                 DLIST_ADD_END(ea_list_head, listp, struct ea_list *);
370
371         }
372
373         /* Add on 4 for total length. */
374         if (*pea_total_len) {
375                 *pea_total_len += 4;
376         }
377
378         DEBUG(10, ("get_ea_list_from_file: total_len = %u\n",
379                    (unsigned int)*pea_total_len));
380
381         *ea_list = ea_list_head;
382         return NT_STATUS_OK;
383 }
384
385 static NTSTATUS get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
386                                       const struct smb_filename *smb_fname, size_t *pea_total_len, struct ea_list **ea_list)
387 {
388         *pea_total_len = 0;
389         *ea_list = NULL;
390
391         if (!lp_ea_support(SNUM(conn))) {
392                 return NT_STATUS_OK;
393         }
394
395         if (is_ntfs_stream_smb_fname(smb_fname)) {
396                 return NT_STATUS_INVALID_PARAMETER;
397         }
398
399         return get_ea_list_from_file_path(mem_ctx, conn, fsp, smb_fname->base_name, pea_total_len, ea_list);
400 }
401
402 /****************************************************************************
403  Fill a qfilepathinfo buffer with EA's. Returns the length of the buffer
404  that was filled.
405 ****************************************************************************/
406
407 static unsigned int fill_ea_buffer(TALLOC_CTX *mem_ctx, char *pdata, unsigned int total_data_size,
408         connection_struct *conn, struct ea_list *ea_list)
409 {
410         unsigned int ret_data_size = 4;
411         char *p = pdata;
412
413         SMB_ASSERT(total_data_size >= 4);
414
415         if (!lp_ea_support(SNUM(conn))) {
416                 SIVAL(pdata,4,0);
417                 return 4;
418         }
419
420         for (p = pdata + 4; ea_list; ea_list = ea_list->next) {
421                 size_t dos_namelen;
422                 fstring dos_ea_name;
423                 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
424                 dos_namelen = strlen(dos_ea_name);
425                 if (dos_namelen > 255 || dos_namelen == 0) {
426                         break;
427                 }
428                 if (ea_list->ea.value.length > 65535) {
429                         break;
430                 }
431                 if (4 + dos_namelen + 1 + ea_list->ea.value.length > total_data_size) {
432                         break;
433                 }
434
435                 /* We know we have room. */
436                 SCVAL(p,0,ea_list->ea.flags);
437                 SCVAL(p,1,dos_namelen);
438                 SSVAL(p,2,ea_list->ea.value.length);
439                 strlcpy(p+4, dos_ea_name, dos_namelen+1);
440                 memcpy( p + 4 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
441
442                 total_data_size -= 4 + dos_namelen + 1 + ea_list->ea.value.length;
443                 p += 4 + dos_namelen + 1 + ea_list->ea.value.length;
444         }
445
446         ret_data_size = PTR_DIFF(p, pdata);
447         DEBUG(10,("fill_ea_buffer: data_size = %u\n", ret_data_size ));
448         SIVAL(pdata,0,ret_data_size);
449         return ret_data_size;
450 }
451
452 static NTSTATUS fill_ea_chained_buffer(TALLOC_CTX *mem_ctx,
453                                        char *pdata,
454                                        unsigned int total_data_size,
455                                        unsigned int *ret_data_size,
456                                        connection_struct *conn,
457                                        struct ea_list *ea_list)
458 {
459         uint8_t *p = (uint8_t *)pdata;
460         uint8_t *last_start = NULL;
461
462         *ret_data_size = 0;
463
464         if (!lp_ea_support(SNUM(conn))) {
465                 return NT_STATUS_NO_EAS_ON_FILE;
466         }
467
468         for (; ea_list; ea_list = ea_list->next) {
469                 size_t dos_namelen;
470                 fstring dos_ea_name;
471                 size_t this_size;
472
473                 if (last_start) {
474                         SIVAL(last_start, 0, PTR_DIFF(p, last_start));
475                 }
476                 last_start = p;
477
478                 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
479                 dos_namelen = strlen(dos_ea_name);
480                 if (dos_namelen > 255 || dos_namelen == 0) {
481                         return NT_STATUS_INTERNAL_ERROR;
482                 }
483                 if (ea_list->ea.value.length > 65535) {
484                         return NT_STATUS_INTERNAL_ERROR;
485                 }
486
487                 this_size = 0x08 + dos_namelen + 1 + ea_list->ea.value.length;
488
489                 if (ea_list->next) {
490                         size_t pad = 4 - (this_size % 4);
491                         this_size += pad;
492                 }
493
494                 if (this_size > total_data_size) {
495                         return NT_STATUS_INFO_LENGTH_MISMATCH;
496                 }
497
498                 /* We know we have room. */
499                 SIVAL(p, 0x00, 0); /* next offset */
500                 SCVAL(p, 0x04, ea_list->ea.flags);
501                 SCVAL(p, 0x05, dos_namelen);
502                 SSVAL(p, 0x06, ea_list->ea.value.length);
503                 strlcpy((char *)(p+0x08), dos_ea_name, dos_namelen+1);
504                 memcpy(p + 0x08 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
505
506                 total_data_size -= this_size;
507                 p += this_size;
508         }
509
510         *ret_data_size = PTR_DIFF(p, pdata);
511         DEBUG(10,("fill_ea_chained_buffer: data_size = %u\n", *ret_data_size));
512         return NT_STATUS_OK;
513 }
514
515 static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp, const struct smb_filename *smb_fname)
516 {
517         size_t total_ea_len = 0;
518         TALLOC_CTX *mem_ctx;
519         struct ea_list *ea_list;
520
521         if (!lp_ea_support(SNUM(conn))) {
522                 return 0;
523         }
524         mem_ctx = talloc_stackframe();
525
526         /* If this is a stream fsp, then we need to instead find the
527          * estimated ea len from the main file, not the stream
528          * (streams cannot have EAs), but the estimate isn't just 0 in
529          * this case! */
530         if (is_ntfs_stream_smb_fname(smb_fname)) {
531                 fsp = NULL;
532         }
533         (void)get_ea_list_from_file_path(mem_ctx, conn, fsp, smb_fname->base_name, &total_ea_len, &ea_list);
534         TALLOC_FREE(mem_ctx);
535         return total_ea_len;
536 }
537
538 /****************************************************************************
539  Ensure the EA name is case insensitive by matching any existing EA name.
540 ****************************************************************************/
541
542 static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, const char *fname, fstring unix_ea_name)
543 {
544         size_t total_ea_len;
545         TALLOC_CTX *mem_ctx = talloc_tos();
546         struct ea_list *ea_list;
547         NTSTATUS status = get_ea_list_from_file_path(mem_ctx, conn, fsp, fname, &total_ea_len, &ea_list);
548         if (!NT_STATUS_IS_OK(status)) {
549                 return;
550         }
551
552         for (; ea_list; ea_list = ea_list->next) {
553                 if (strequal(&unix_ea_name[5], ea_list->ea.name)) {
554                         DEBUG(10,("canonicalize_ea_name: %s -> %s\n",
555                                 &unix_ea_name[5], ea_list->ea.name));
556                         strlcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-5);
557                         break;
558                 }
559         }
560 }
561
562 /****************************************************************************
563  Set or delete an extended attribute.
564 ****************************************************************************/
565
566 NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
567                 const struct smb_filename *smb_fname, struct ea_list *ea_list)
568 {
569         NTSTATUS status;
570         char *fname = NULL;
571
572         if (!lp_ea_support(SNUM(conn))) {
573                 return NT_STATUS_EAS_NOT_SUPPORTED;
574         }
575
576         status = check_access(conn, fsp, smb_fname, FILE_WRITE_EA);
577         if (!NT_STATUS_IS_OK(status)) {
578                 return status;
579         }
580
581         /* Setting EAs on streams isn't supported. */
582         if (is_ntfs_stream_smb_fname(smb_fname)) {
583                 return NT_STATUS_INVALID_PARAMETER;
584         }
585
586         fname = smb_fname->base_name;
587
588         for (;ea_list; ea_list = ea_list->next) {
589                 int ret;
590                 fstring unix_ea_name;
591
592                 fstrcpy(unix_ea_name, "user."); /* All EA's must start with user. */
593                 fstrcat(unix_ea_name, ea_list->ea.name);
594
595                 canonicalize_ea_name(conn, fsp, fname, unix_ea_name);
596
597                 DEBUG(10,("set_ea: ea_name %s ealen = %u\n", unix_ea_name, (unsigned int)ea_list->ea.value.length));
598
599                 if (samba_private_attr_name(unix_ea_name)) {
600                         DEBUG(10,("set_ea: ea name %s is a private Samba name.\n", unix_ea_name));
601                         return NT_STATUS_ACCESS_DENIED;
602                 }
603
604                 if (ea_list->ea.value.length == 0) {
605                         /* Remove the attribute. */
606                         if (fsp && (fsp->fh->fd != -1)) {
607                                 DEBUG(10,("set_ea: deleting ea name %s on "
608                                           "file %s by file descriptor.\n",
609                                           unix_ea_name, fsp_str_dbg(fsp)));
610                                 ret = SMB_VFS_FREMOVEXATTR(fsp, unix_ea_name);
611                         } else {
612                                 DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
613                                         unix_ea_name, fname));
614                                 ret = SMB_VFS_REMOVEXATTR(conn, fname, unix_ea_name);
615                         }
616 #ifdef ENOATTR
617                         /* Removing a non existent attribute always succeeds. */
618                         if (ret == -1 && errno == ENOATTR) {
619                                 DEBUG(10,("set_ea: deleting ea name %s didn't exist - succeeding by default.\n",
620                                                 unix_ea_name));
621                                 ret = 0;
622                         }
623 #endif
624                 } else {
625                         if (fsp && (fsp->fh->fd != -1)) {
626                                 DEBUG(10,("set_ea: setting ea name %s on file "
627                                           "%s by file descriptor.\n",
628                                           unix_ea_name, fsp_str_dbg(fsp)));
629                                 ret = SMB_VFS_FSETXATTR(fsp, unix_ea_name,
630                                                         ea_list->ea.value.data, ea_list->ea.value.length, 0);
631                         } else {
632                                 DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
633                                         unix_ea_name, fname));
634                                 ret = SMB_VFS_SETXATTR(conn, fname, unix_ea_name,
635                                                         ea_list->ea.value.data, ea_list->ea.value.length, 0);
636                         }
637                 }
638
639                 if (ret == -1) {
640 #ifdef ENOTSUP
641                         if (errno == ENOTSUP) {
642                                 return NT_STATUS_EAS_NOT_SUPPORTED;
643                         }
644 #endif
645                         return map_nt_error_from_unix(errno);
646                 }
647
648         }
649         return NT_STATUS_OK;
650 }
651 /****************************************************************************
652  Read a list of EA names from an incoming data buffer. Create an ea_list with them.
653 ****************************************************************************/
654
655 static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
656 {
657         struct ea_list *ea_list_head = NULL;
658         size_t converted_size, offset = 0;
659
660         while (offset + 2 < data_size) {
661                 struct ea_list *eal = talloc_zero(ctx, struct ea_list);
662                 unsigned int namelen = CVAL(pdata,offset);
663
664                 offset++; /* Go past the namelen byte. */
665
666                 /* integer wrap paranioa. */
667                 if ((offset + namelen < offset) || (offset + namelen < namelen) ||
668                                 (offset > data_size) || (namelen > data_size) ||
669                                 (offset + namelen >= data_size)) {
670                         break;
671                 }
672                 /* Ensure the name is null terminated. */
673                 if (pdata[offset + namelen] != '\0') {
674                         return NULL;
675                 }
676                 if (!pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset],
677                                        &converted_size)) {
678                         DEBUG(0,("read_ea_name_list: pull_ascii_talloc "
679                                  "failed: %s", strerror(errno)));
680                 }
681                 if (!eal->ea.name) {
682                         return NULL;
683                 }
684
685                 offset += (namelen + 1); /* Go past the name + terminating zero. */
686                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
687                 DEBUG(10,("read_ea_name_list: read ea name %s\n", eal->ea.name));
688         }
689
690         return ea_list_head;
691 }
692
693 /****************************************************************************
694  Read one EA list entry from the buffer.
695 ****************************************************************************/
696
697 struct ea_list *read_ea_list_entry(TALLOC_CTX *ctx, const char *pdata, size_t data_size, size_t *pbytes_used)
698 {
699         struct ea_list *eal = talloc_zero(ctx, struct ea_list);
700         uint16 val_len;
701         unsigned int namelen;
702         size_t converted_size;
703
704         if (!eal) {
705                 return NULL;
706         }
707
708         if (data_size < 6) {
709                 return NULL;
710         }
711
712         eal->ea.flags = CVAL(pdata,0);
713         namelen = CVAL(pdata,1);
714         val_len = SVAL(pdata,2);
715
716         if (4 + namelen + 1 + val_len > data_size) {
717                 return NULL;
718         }
719
720         /* Ensure the name is null terminated. */
721         if (pdata[namelen + 4] != '\0') {
722                 return NULL;
723         }
724         if (!pull_ascii_talloc(ctx, &eal->ea.name, pdata + 4, &converted_size)) {
725                 DEBUG(0,("read_ea_list_entry: pull_ascii_talloc failed: %s",
726                          strerror(errno)));
727         }
728         if (!eal->ea.name) {
729                 return NULL;
730         }
731
732         eal->ea.value = data_blob_talloc(eal, NULL, (size_t)val_len + 1);
733         if (!eal->ea.value.data) {
734                 return NULL;
735         }
736
737         memcpy(eal->ea.value.data, pdata + 4 + namelen + 1, val_len);
738
739         /* Ensure we're null terminated just in case we print the value. */
740         eal->ea.value.data[val_len] = '\0';
741         /* But don't count the null. */
742         eal->ea.value.length--;
743
744         if (pbytes_used) {
745                 *pbytes_used = 4 + namelen + 1 + val_len;
746         }
747
748         DEBUG(10,("read_ea_list_entry: read ea name %s\n", eal->ea.name));
749         dump_data(10, eal->ea.value.data, eal->ea.value.length);
750
751         return eal;
752 }
753
754 /****************************************************************************
755  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
756 ****************************************************************************/
757
758 static struct ea_list *read_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
759 {
760         struct ea_list *ea_list_head = NULL;
761         size_t offset = 0;
762         size_t bytes_used = 0;
763
764         while (offset < data_size) {
765                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset, data_size - offset, &bytes_used);
766
767                 if (!eal) {
768                         return NULL;
769                 }
770
771                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
772                 offset += bytes_used;
773         }
774
775         return ea_list_head;
776 }
777
778 /****************************************************************************
779  Count the total EA size needed.
780 ****************************************************************************/
781
782 static size_t ea_list_size(struct ea_list *ealist)
783 {
784         fstring dos_ea_name;
785         struct ea_list *listp;
786         size_t ret = 0;
787
788         for (listp = ealist; listp; listp = listp->next) {
789                 push_ascii_fstring(dos_ea_name, listp->ea.name);
790                 ret += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
791         }
792         /* Add on 4 for total length. */
793         if (ret) {
794                 ret += 4;
795         }
796
797         return ret;
798 }
799
800 /****************************************************************************
801  Return a union of EA's from a file list and a list of names.
802  The TALLOC context for the two lists *MUST* be identical as we steal
803  memory from one list to add to another. JRA.
804 ****************************************************************************/
805
806 static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *file_list, size_t *total_ea_len)
807 {
808         struct ea_list *nlistp, *flistp;
809
810         for (nlistp = name_list; nlistp; nlistp = nlistp->next) {
811                 for (flistp = file_list; flistp; flistp = flistp->next) {
812                         if (strequal(nlistp->ea.name, flistp->ea.name)) {
813                                 break;
814                         }
815                 }
816
817                 if (flistp) {
818                         /* Copy the data from this entry. */
819                         nlistp->ea.flags = flistp->ea.flags;
820                         nlistp->ea.value = flistp->ea.value;
821                 } else {
822                         /* Null entry. */
823                         nlistp->ea.flags = 0;
824                         ZERO_STRUCT(nlistp->ea.value);
825                 }
826         }
827
828         *total_ea_len = ea_list_size(name_list);
829         return name_list;
830 }
831
832 /****************************************************************************
833   Send the required number of replies back.
834   We assume all fields other than the data fields are
835   set correctly for the type of call.
836   HACK ! Always assumes smb_setup field is zero.
837 ****************************************************************************/
838
839 void send_trans2_replies(connection_struct *conn,
840                         struct smb_request *req,
841                          const char *params,
842                          int paramsize,
843                          const char *pdata,
844                          int datasize,
845                          int max_data_bytes)
846 {
847         /* As we are using a protocol > LANMAN1 then the max_send
848          variable must have been set in the sessetupX call.
849          This takes precedence over the max_xmit field in the
850          global struct. These different max_xmit variables should
851          be merged as this is now too confusing */
852
853         int data_to_send = datasize;
854         int params_to_send = paramsize;
855         int useable_space;
856         const char *pp = params;
857         const char *pd = pdata;
858         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
859         int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
860         int data_alignment_offset = 0;
861         bool overflow = False;
862         struct smbd_server_connection *sconn = req->sconn;
863         int max_send = sconn->smb1.sessions.max_send;
864
865         /* Modify the data_to_send and datasize and set the error if
866            we're trying to send more than max_data_bytes. We still send
867            the part of the packet(s) that fit. Strange, but needed
868            for OS/2. */
869
870         if (max_data_bytes > 0 && datasize > max_data_bytes) {
871                 DEBUG(5,("send_trans2_replies: max_data_bytes %d exceeded by data %d\n",
872                         max_data_bytes, datasize ));
873                 datasize = data_to_send = max_data_bytes;
874                 overflow = True;
875         }
876
877         /* If there genuinely are no parameters or data to send just send the empty packet */
878
879         if(params_to_send == 0 && data_to_send == 0) {
880                 reply_outbuf(req, 10, 0);
881                 show_msg((char *)req->outbuf);
882                 if (!srv_send_smb(sconn,
883                                 (char *)req->outbuf,
884                                 true, req->seqnum+1,
885                                 IS_CONN_ENCRYPTED(conn),
886                                 &req->pcd)) {
887                         exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
888                 }
889                 TALLOC_FREE(req->outbuf);
890                 return;
891         }
892
893         /* When sending params and data ensure that both are nicely aligned */
894         /* Only do this alignment when there is also data to send - else
895                 can cause NT redirector problems. */
896
897         if (((params_to_send % 4) != 0) && (data_to_send != 0))
898                 data_alignment_offset = 4 - (params_to_send % 4);
899
900         /* Space is bufsize minus Netbios over TCP header minus SMB header */
901         /* The alignment_offset is to align the param bytes on an even byte
902                 boundary. NT 4.0 Beta needs this to work correctly. */
903
904         useable_space = max_send - (smb_size
905                                     + 2 * 10 /* wct */
906                                     + alignment_offset
907                                     + data_alignment_offset);
908
909         if (useable_space < 0) {
910                 DEBUG(0, ("send_trans2_replies failed sanity useable_space "
911                           "= %d!!!", useable_space));
912                 exit_server_cleanly("send_trans2_replies: Not enough space");
913         }
914
915         while (params_to_send || data_to_send) {
916                 /* Calculate whether we will totally or partially fill this packet */
917
918                 total_sent_thistime = params_to_send + data_to_send;
919
920                 /* We can never send more than useable_space */
921                 /*
922                  * Note that 'useable_space' does not include the alignment offsets,
923                  * but we must include the alignment offsets in the calculation of
924                  * the length of the data we send over the wire, as the alignment offsets
925                  * are sent here. Fix from Marc_Jacobsen@hp.com.
926                  */
927
928                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
929
930                 reply_outbuf(req, 10, total_sent_thistime + alignment_offset
931                              + data_alignment_offset);
932
933                 /* Set total params and data to be sent */
934                 SSVAL(req->outbuf,smb_tprcnt,paramsize);
935                 SSVAL(req->outbuf,smb_tdrcnt,datasize);
936
937                 /* Calculate how many parameters and data we can fit into
938                  * this packet. Parameters get precedence
939                  */
940
941                 params_sent_thistime = MIN(params_to_send,useable_space);
942                 data_sent_thistime = useable_space - params_sent_thistime;
943                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
944
945                 SSVAL(req->outbuf,smb_prcnt, params_sent_thistime);
946
947                 /* smb_proff is the offset from the start of the SMB header to the
948                         parameter bytes, however the first 4 bytes of outbuf are
949                         the Netbios over TCP header. Thus use smb_base() to subtract
950                         them from the calculation */
951
952                 SSVAL(req->outbuf,smb_proff,
953                       ((smb_buf(req->outbuf)+alignment_offset)
954                        - smb_base(req->outbuf)));
955
956                 if(params_sent_thistime == 0)
957                         SSVAL(req->outbuf,smb_prdisp,0);
958                 else
959                         /* Absolute displacement of param bytes sent in this packet */
960                         SSVAL(req->outbuf,smb_prdisp,pp - params);
961
962                 SSVAL(req->outbuf,smb_drcnt, data_sent_thistime);
963                 if(data_sent_thistime == 0) {
964                         SSVAL(req->outbuf,smb_droff,0);
965                         SSVAL(req->outbuf,smb_drdisp, 0);
966                 } else {
967                         /* The offset of the data bytes is the offset of the
968                                 parameter bytes plus the number of parameters being sent this time */
969                         SSVAL(req->outbuf, smb_droff,
970                               ((smb_buf(req->outbuf)+alignment_offset)
971                                - smb_base(req->outbuf))
972                               + params_sent_thistime + data_alignment_offset);
973                         SSVAL(req->outbuf,smb_drdisp, pd - pdata);
974                 }
975
976                 /* Initialize the padding for alignment */
977
978                 if (alignment_offset != 0) {
979                         memset(smb_buf(req->outbuf), 0, alignment_offset);
980                 }
981
982                 /* Copy the param bytes into the packet */
983
984                 if(params_sent_thistime) {
985                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
986                                params_sent_thistime);
987                 }
988
989                 /* Copy in the data bytes */
990                 if(data_sent_thistime) {
991                         if (data_alignment_offset != 0) {
992                                 memset((smb_buf(req->outbuf)+alignment_offset+
993                                         params_sent_thistime), 0,
994                                        data_alignment_offset);
995                         }
996                         memcpy(smb_buf(req->outbuf)+alignment_offset
997                                +params_sent_thistime+data_alignment_offset,
998                                pd,data_sent_thistime);
999                 }
1000
1001                 DEBUG(9,("t2_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
1002                         params_sent_thistime, data_sent_thistime, useable_space));
1003                 DEBUG(9,("t2_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
1004                         params_to_send, data_to_send, paramsize, datasize));
1005
1006                 if (overflow) {
1007                         error_packet_set((char *)req->outbuf,
1008                                          ERRDOS,ERRbufferoverflow,
1009                                          STATUS_BUFFER_OVERFLOW,
1010                                          __LINE__,__FILE__);
1011                 }
1012
1013                 /* Send the packet */
1014                 show_msg((char *)req->outbuf);
1015                 if (!srv_send_smb(sconn,
1016                                 (char *)req->outbuf,
1017                                 true, req->seqnum+1,
1018                                 IS_CONN_ENCRYPTED(conn),
1019                                 &req->pcd))
1020                         exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
1021
1022                 TALLOC_FREE(req->outbuf);
1023
1024                 pp += params_sent_thistime;
1025                 pd += data_sent_thistime;
1026
1027                 params_to_send -= params_sent_thistime;
1028                 data_to_send -= data_sent_thistime;
1029
1030                 /* Sanity check */
1031                 if(params_to_send < 0 || data_to_send < 0) {
1032                         DEBUG(0,("send_trans2_replies failed sanity check pts = %d, dts = %d\n!!!",
1033                                 params_to_send, data_to_send));
1034                         return;
1035                 }
1036         }
1037
1038         return;
1039 }
1040
1041 /****************************************************************************
1042  Reply to a TRANSACT2_OPEN.
1043 ****************************************************************************/
1044
1045 static void call_trans2open(connection_struct *conn,
1046                             struct smb_request *req,
1047                             char **pparams, int total_params,
1048                             char **ppdata, int total_data,
1049                             unsigned int max_data_bytes)
1050 {
1051         struct smb_filename *smb_fname = NULL;
1052         char *params = *pparams;
1053         char *pdata = *ppdata;
1054         int deny_mode;
1055         int32 open_attr;
1056         bool oplock_request;
1057 #if 0
1058         bool return_additional_info;
1059         int16 open_sattr;
1060         time_t open_time;
1061 #endif
1062         int open_ofun;
1063         uint32 open_size;
1064         char *pname;
1065         char *fname = NULL;
1066         off_t size=0;
1067         int fattr=0,mtime=0;
1068         SMB_INO_T inode = 0;
1069         int smb_action = 0;
1070         files_struct *fsp;
1071         struct ea_list *ea_list = NULL;
1072         uint16 flags = 0;
1073         NTSTATUS status;
1074         uint32 access_mask;
1075         uint32 share_mode;
1076         uint32 create_disposition;
1077         uint32 create_options = 0;
1078         uint32_t private_flags = 0;
1079         TALLOC_CTX *ctx = talloc_tos();
1080
1081         /*
1082          * Ensure we have enough parameters to perform the operation.
1083          */
1084
1085         if (total_params < 29) {
1086                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1087                 goto out;
1088         }
1089
1090         flags = SVAL(params, 0);
1091         deny_mode = SVAL(params, 2);
1092         open_attr = SVAL(params,6);
1093         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1094         if (oplock_request) {
1095                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1096         }
1097
1098 #if 0
1099         return_additional_info = BITSETW(params,0);
1100         open_sattr = SVAL(params, 4);
1101         open_time = make_unix_date3(params+8);
1102 #endif
1103         open_ofun = SVAL(params,12);
1104         open_size = IVAL(params,14);
1105         pname = &params[28];
1106
1107         if (IS_IPC(conn)) {
1108                 reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
1109                 goto out;
1110         }
1111
1112         srvstr_get_path(ctx, params, req->flags2, &fname, pname,
1113                         total_params - 28, STR_TERMINATE,
1114                         &status);
1115         if (!NT_STATUS_IS_OK(status)) {
1116                 reply_nterror(req, status);
1117                 goto out;
1118         }
1119
1120         DEBUG(3,("call_trans2open %s deny_mode=0x%x attr=%d ofun=0x%x size=%d\n",
1121                 fname, (unsigned int)deny_mode, (unsigned int)open_attr,
1122                 (unsigned int)open_ofun, open_size));
1123
1124         status = filename_convert(ctx,
1125                                 conn,
1126                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1127                                 fname,
1128                                 0,
1129                                 NULL,
1130                                 &smb_fname);
1131         if (!NT_STATUS_IS_OK(status)) {
1132                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1133                         reply_botherror(req,
1134                                 NT_STATUS_PATH_NOT_COVERED,
1135                                 ERRSRV, ERRbadpath);
1136                         goto out;
1137                 }
1138                 reply_nterror(req, status);
1139                 goto out;
1140         }
1141
1142         if (open_ofun == 0) {
1143                 reply_nterror(req, NT_STATUS_OBJECT_NAME_COLLISION);
1144                 goto out;
1145         }
1146
1147         if (!map_open_params_to_ntcreate(smb_fname->base_name, deny_mode,
1148                                          open_ofun,
1149                                          &access_mask, &share_mode,
1150                                          &create_disposition,
1151                                          &create_options,
1152                                          &private_flags)) {
1153                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1154                 goto out;
1155         }
1156
1157         /* Any data in this call is an EA list. */
1158         if (total_data && (total_data != 4)) {
1159                 if (total_data < 10) {
1160                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1161                         goto out;
1162                 }
1163
1164                 if (IVAL(pdata,0) > total_data) {
1165                         DEBUG(10,("call_trans2open: bad total data size (%u) > %u\n",
1166                                 IVAL(pdata,0), (unsigned int)total_data));
1167                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1168                         goto out;
1169                 }
1170
1171                 ea_list = read_ea_list(talloc_tos(), pdata + 4,
1172                                        total_data - 4);
1173                 if (!ea_list) {
1174                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1175                         goto out;
1176                 }
1177
1178                 if (!lp_ea_support(SNUM(conn))) {
1179                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1180                         goto out;
1181                 }
1182         }
1183
1184         status = SMB_VFS_CREATE_FILE(
1185                 conn,                                   /* conn */
1186                 req,                                    /* req */
1187                 0,                                      /* root_dir_fid */
1188                 smb_fname,                              /* fname */
1189                 access_mask,                            /* access_mask */
1190                 share_mode,                             /* share_access */
1191                 create_disposition,                     /* create_disposition*/
1192                 create_options,                         /* create_options */
1193                 open_attr,                              /* file_attributes */
1194                 oplock_request,                         /* oplock_request */
1195                 open_size,                              /* allocation_size */
1196                 private_flags,
1197                 NULL,                                   /* sd */
1198                 ea_list,                                /* ea_list */
1199                 &fsp,                                   /* result */
1200                 &smb_action);                           /* psbuf */
1201
1202         if (!NT_STATUS_IS_OK(status)) {
1203                 if (open_was_deferred(req->sconn, req->mid)) {
1204                         /* We have re-scheduled this call. */
1205                         goto out;
1206                 }
1207                 reply_openerror(req, status);
1208                 goto out;
1209         }
1210
1211         size = get_file_size_stat(&smb_fname->st);
1212         fattr = dos_mode(conn, smb_fname);
1213         mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
1214         inode = smb_fname->st.st_ex_ino;
1215         if (fattr & FILE_ATTRIBUTE_DIRECTORY) {
1216                 close_file(req, fsp, ERROR_CLOSE);
1217                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1218                 goto out;
1219         }
1220
1221         /* Realloc the size of parameters and data we will return */
1222         *pparams = (char *)SMB_REALLOC(*pparams, 30);
1223         if(*pparams == NULL ) {
1224                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1225                 goto out;
1226         }
1227         params = *pparams;
1228
1229         SSVAL(params,0,fsp->fnum);
1230         SSVAL(params,2,fattr);
1231         srv_put_dos_date2(params,4, mtime);
1232         SIVAL(params,8, (uint32)size);
1233         SSVAL(params,12,deny_mode);
1234         SSVAL(params,14,0); /* open_type - file or directory. */
1235         SSVAL(params,16,0); /* open_state - only valid for IPC device. */
1236
1237         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1238                 smb_action |= EXTENDED_OPLOCK_GRANTED;
1239         }
1240
1241         SSVAL(params,18,smb_action);
1242
1243         /*
1244          * WARNING - this may need to be changed if SMB_INO_T <> 4 bytes.
1245          */
1246         SIVAL(params,20,inode);
1247         SSVAL(params,24,0); /* Padding. */
1248         if (flags & 8) {
1249                 uint32 ea_size = estimate_ea_size(conn, fsp,
1250                                                   smb_fname);
1251                 SIVAL(params, 26, ea_size);
1252         } else {
1253                 SIVAL(params, 26, 0);
1254         }
1255
1256         /* Send the required number of replies */
1257         send_trans2_replies(conn, req, params, 30, *ppdata, 0, max_data_bytes);
1258  out:
1259         TALLOC_FREE(smb_fname);
1260 }
1261
1262 /*********************************************************
1263  Routine to check if a given string matches exactly.
1264  as a special case a mask of "." does NOT match. That
1265  is required for correct wildcard semantics
1266  Case can be significant or not.
1267 **********************************************************/
1268
1269 static bool exact_match(bool has_wild,
1270                         bool case_sensitive,
1271                         const char *str,
1272                         const char *mask)
1273 {
1274         if (mask[0] == '.' && mask[1] == 0) {
1275                 return false;
1276         }
1277
1278         if (has_wild) {
1279                 return false;
1280         }
1281
1282         if (case_sensitive) {
1283                 return strcmp(str,mask)==0;
1284         } else {
1285                 return strcasecmp_m(str,mask) == 0;
1286         }
1287 }
1288
1289 /****************************************************************************
1290  Return the filetype for UNIX extensions.
1291 ****************************************************************************/
1292
1293 static uint32 unix_filetype(mode_t mode)
1294 {
1295         if(S_ISREG(mode))
1296                 return UNIX_TYPE_FILE;
1297         else if(S_ISDIR(mode))
1298                 return UNIX_TYPE_DIR;
1299 #ifdef S_ISLNK
1300         else if(S_ISLNK(mode))
1301                 return UNIX_TYPE_SYMLINK;
1302 #endif
1303 #ifdef S_ISCHR
1304         else if(S_ISCHR(mode))
1305                 return UNIX_TYPE_CHARDEV;
1306 #endif
1307 #ifdef S_ISBLK
1308         else if(S_ISBLK(mode))
1309                 return UNIX_TYPE_BLKDEV;
1310 #endif
1311 #ifdef S_ISFIFO
1312         else if(S_ISFIFO(mode))
1313                 return UNIX_TYPE_FIFO;
1314 #endif
1315 #ifdef S_ISSOCK
1316         else if(S_ISSOCK(mode))
1317                 return UNIX_TYPE_SOCKET;
1318 #endif
1319
1320         DEBUG(0,("unix_filetype: unknown filetype %u\n", (unsigned)mode));
1321         return UNIX_TYPE_UNKNOWN;
1322 }
1323
1324 /****************************************************************************
1325  Map wire perms onto standard UNIX permissions. Obey share restrictions.
1326 ****************************************************************************/
1327
1328 enum perm_type { PERM_NEW_FILE, PERM_NEW_DIR, PERM_EXISTING_FILE, PERM_EXISTING_DIR};
1329
1330 static NTSTATUS unix_perms_from_wire( connection_struct *conn,
1331                                 const SMB_STRUCT_STAT *psbuf,
1332                                 uint32 perms,
1333                                 enum perm_type ptype,
1334                                 mode_t *ret_perms)
1335 {
1336         mode_t ret = 0;
1337
1338         if (perms == SMB_MODE_NO_CHANGE) {
1339                 if (!VALID_STAT(*psbuf)) {
1340                         return NT_STATUS_INVALID_PARAMETER;
1341                 } else {
1342                         *ret_perms = psbuf->st_ex_mode;
1343                         return NT_STATUS_OK;
1344                 }
1345         }
1346
1347         ret |= ((perms & UNIX_X_OTH ) ? S_IXOTH : 0);
1348         ret |= ((perms & UNIX_W_OTH ) ? S_IWOTH : 0);
1349         ret |= ((perms & UNIX_R_OTH ) ? S_IROTH : 0);
1350         ret |= ((perms & UNIX_X_GRP ) ? S_IXGRP : 0);
1351         ret |= ((perms & UNIX_W_GRP ) ? S_IWGRP : 0);
1352         ret |= ((perms & UNIX_R_GRP ) ? S_IRGRP : 0);
1353         ret |= ((perms & UNIX_X_USR ) ? S_IXUSR : 0);
1354         ret |= ((perms & UNIX_W_USR ) ? S_IWUSR : 0);
1355         ret |= ((perms & UNIX_R_USR ) ? S_IRUSR : 0);
1356 #ifdef S_ISVTX
1357         ret |= ((perms & UNIX_STICKY ) ? S_ISVTX : 0);
1358 #endif
1359 #ifdef S_ISGID
1360         ret |= ((perms & UNIX_SET_GID ) ? S_ISGID : 0);
1361 #endif
1362 #ifdef S_ISUID
1363         ret |= ((perms & UNIX_SET_UID ) ? S_ISUID : 0);
1364 #endif
1365
1366         switch (ptype) {
1367         case PERM_NEW_FILE:
1368         case PERM_EXISTING_FILE:
1369                 /* Apply mode mask */
1370                 ret &= lp_create_mask(SNUM(conn));
1371                 /* Add in force bits */
1372                 ret |= lp_force_create_mode(SNUM(conn));
1373                 break;
1374         case PERM_NEW_DIR:
1375         case PERM_EXISTING_DIR:
1376                 ret &= lp_dir_mask(SNUM(conn));
1377                 /* Add in force bits */
1378                 ret |= lp_force_dir_mode(SNUM(conn));
1379                 break;
1380         }
1381
1382         *ret_perms = ret;
1383         return NT_STATUS_OK;
1384 }
1385
1386 /****************************************************************************
1387  Needed to show the msdfs symlinks as directories. Modifies psbuf
1388  to be a directory if it's a msdfs link.
1389 ****************************************************************************/
1390
1391 static bool check_msdfs_link(connection_struct *conn,
1392                                 const char *pathname,
1393                                 SMB_STRUCT_STAT *psbuf)
1394 {
1395         int saved_errno = errno;
1396         if(lp_host_msdfs() &&
1397                 lp_msdfs_root(SNUM(conn)) &&
1398                 is_msdfs_link(conn, pathname, psbuf)) {
1399
1400                 DEBUG(5,("check_msdfs_link: Masquerading msdfs link %s "
1401                         "as a directory\n",
1402                         pathname));
1403                 psbuf->st_ex_mode = (psbuf->st_ex_mode & 0xFFF) | S_IFDIR;
1404                 errno = saved_errno;
1405                 return true;
1406         }
1407         errno = saved_errno;
1408         return false;
1409 }
1410
1411
1412 /****************************************************************************
1413  Get a level dependent lanman2 dir entry.
1414 ****************************************************************************/
1415
1416 struct smbd_dirptr_lanman2_state {
1417         connection_struct *conn;
1418         uint32_t info_level;
1419         bool check_mangled_names;
1420         bool has_wild;
1421         bool got_exact_match;
1422 };
1423
1424 static bool smbd_dirptr_lanman2_match_fn(TALLOC_CTX *ctx,
1425                                          void *private_data,
1426                                          const char *dname,
1427                                          const char *mask,
1428                                          char **_fname)
1429 {
1430         struct smbd_dirptr_lanman2_state *state =
1431                 (struct smbd_dirptr_lanman2_state *)private_data;
1432         bool ok;
1433         char mangled_name[13]; /* mangled 8.3 name. */
1434         bool got_match;
1435         const char *fname;
1436
1437         /* Mangle fname if it's an illegal name. */
1438         if (mangle_must_mangle(dname, state->conn->params)) {
1439                 ok = name_to_8_3(dname, mangled_name,
1440                                  true, state->conn->params);
1441                 if (!ok) {
1442                         return false;
1443                 }
1444                 fname = mangled_name;
1445         } else {
1446                 fname = dname;
1447         }
1448
1449         got_match = exact_match(state->has_wild,
1450                                 state->conn->case_sensitive,
1451                                 fname, mask);
1452         state->got_exact_match = got_match;
1453         if (!got_match) {
1454                 got_match = mask_match(fname, mask,
1455                                        state->conn->case_sensitive);
1456         }
1457
1458         if(!got_match && state->check_mangled_names &&
1459            !mangle_is_8_3(fname, false, state->conn->params)) {
1460                 /*
1461                  * It turns out that NT matches wildcards against
1462                  * both long *and* short names. This may explain some
1463                  * of the wildcard wierdness from old DOS clients
1464                  * that some people have been seeing.... JRA.
1465                  */
1466                 /* Force the mangling into 8.3. */
1467                 ok = name_to_8_3(fname, mangled_name,
1468                                  false, state->conn->params);
1469                 if (!ok) {
1470                         return false;
1471                 }
1472
1473                 got_match = exact_match(state->has_wild,
1474                                         state->conn->case_sensitive,
1475                                         mangled_name, mask);
1476                 state->got_exact_match = got_match;
1477                 if (!got_match) {
1478                         got_match = mask_match(mangled_name, mask,
1479                                                state->conn->case_sensitive);
1480                 }
1481         }
1482
1483         if (!got_match) {
1484                 return false;
1485         }
1486
1487         *_fname = talloc_strdup(ctx, fname);
1488         if (*_fname == NULL) {
1489                 return false;
1490         }
1491
1492         return true;
1493 }
1494
1495 static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
1496                                         void *private_data,
1497                                         struct smb_filename *smb_fname,
1498                                         uint32_t *_mode)
1499 {
1500         struct smbd_dirptr_lanman2_state *state =
1501                 (struct smbd_dirptr_lanman2_state *)private_data;
1502         bool ms_dfs_link = false;
1503         uint32_t mode = 0;
1504
1505         if (INFO_LEVEL_IS_UNIX(state->info_level)) {
1506                 if (SMB_VFS_LSTAT(state->conn, smb_fname) != 0) {
1507                         DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
1508                                  "Couldn't lstat [%s] (%s)\n",
1509                                  smb_fname_str_dbg(smb_fname),
1510                                  strerror(errno)));
1511                         return false;
1512                 }
1513         } else if (!VALID_STAT(smb_fname->st) &&
1514                    SMB_VFS_STAT(state->conn, smb_fname) != 0) {
1515                 /* Needed to show the msdfs symlinks as
1516                  * directories */
1517
1518                 ms_dfs_link = check_msdfs_link(state->conn,
1519                                                smb_fname->base_name,
1520                                                &smb_fname->st);
1521                 if (!ms_dfs_link) {
1522                         DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
1523                                  "Couldn't stat [%s] (%s)\n",
1524                                  smb_fname_str_dbg(smb_fname),
1525                                  strerror(errno)));
1526                         return false;
1527                 }
1528         }
1529
1530         if (ms_dfs_link) {
1531                 mode = dos_mode_msdfs(state->conn, smb_fname);
1532         } else {
1533                 mode = dos_mode(state->conn, smb_fname);
1534         }
1535
1536         *_mode = mode;
1537         return true;
1538 }
1539
1540 static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
1541                                     connection_struct *conn,
1542                                     uint16_t flags2,
1543                                     uint32_t info_level,
1544                                     struct ea_list *name_list,
1545                                     bool check_mangled_names,
1546                                     bool requires_resume_key,
1547                                     uint32_t mode,
1548                                     const char *fname,
1549                                     const struct smb_filename *smb_fname,
1550                                     int space_remaining,
1551                                     uint8_t align,
1552                                     bool do_pad,
1553                                     char *base_data,
1554                                     char **ppdata,
1555                                     char *end_data,
1556                                     bool *out_of_space,
1557                                     uint64_t *last_entry_off)
1558 {
1559         char *p, *q, *pdata = *ppdata;
1560         uint32_t reskey=0;
1561         uint64_t file_size = 0;
1562         uint64_t allocation_size = 0;
1563         uint64_t file_index = 0;
1564         uint32_t len;
1565         struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
1566         time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
1567         char *nameptr;
1568         char *last_entry_ptr;
1569         bool was_8_3;
1570         int off;
1571         int pad = 0;
1572
1573         *out_of_space = false;
1574
1575         ZERO_STRUCT(mdate_ts);
1576         ZERO_STRUCT(adate_ts);
1577         ZERO_STRUCT(create_date_ts);
1578         ZERO_STRUCT(cdate_ts);
1579
1580         if (!(mode & FILE_ATTRIBUTE_DIRECTORY)) {
1581                 file_size = get_file_size_stat(&smb_fname->st);
1582         }
1583         allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
1584
1585         file_index = get_FileIndex(conn, &smb_fname->st);
1586
1587         mdate_ts = smb_fname->st.st_ex_mtime;
1588         adate_ts = smb_fname->st.st_ex_atime;
1589         create_date_ts = get_create_timespec(conn, NULL, smb_fname);
1590         cdate_ts = get_change_timespec(conn, NULL, smb_fname);
1591
1592         if (lp_dos_filetime_resolution(SNUM(conn))) {
1593                 dos_filetime_timespec(&create_date_ts);
1594                 dos_filetime_timespec(&mdate_ts);
1595                 dos_filetime_timespec(&adate_ts);
1596                 dos_filetime_timespec(&cdate_ts);
1597         }
1598
1599         create_date = convert_timespec_to_time_t(create_date_ts);
1600         mdate = convert_timespec_to_time_t(mdate_ts);
1601         adate = convert_timespec_to_time_t(adate_ts);
1602
1603         /* align the record */
1604         SMB_ASSERT(align >= 1);
1605
1606         off = (int)PTR_DIFF(pdata, base_data);
1607         pad = (off + (align-1)) & ~(align-1);
1608         pad -= off;
1609
1610         if (pad && pad > space_remaining) {
1611                 *out_of_space = true;
1612                 DEBUG(9,("smbd_marshall_dir_entry: out of space "
1613                         "for padding (wanted %u, had %d)\n",
1614                         (unsigned int)pad,
1615                         space_remaining ));
1616                 return false; /* Not finished - just out of space */
1617         }
1618
1619         off += pad;
1620         /* initialize padding to 0 */
1621         if (pad) {
1622                 memset(pdata, 0, pad);
1623         }
1624         space_remaining -= pad;
1625
1626         DEBUG(10,("smbd_marshall_dir_entry: space_remaining = %d\n",
1627                 space_remaining ));
1628
1629         pdata += pad;
1630         p = pdata;
1631         last_entry_ptr = p;
1632
1633         pad = 0;
1634         off = 0;
1635
1636         switch (info_level) {
1637         case SMB_FIND_INFO_STANDARD:
1638                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_INFO_STANDARD\n"));
1639                 if(requires_resume_key) {
1640                         SIVAL(p,0,reskey);
1641                         p += 4;
1642                 }
1643                 srv_put_dos_date2(p,0,create_date);
1644                 srv_put_dos_date2(p,4,adate);
1645                 srv_put_dos_date2(p,8,mdate);
1646                 SIVAL(p,12,(uint32)file_size);
1647                 SIVAL(p,16,(uint32)allocation_size);
1648                 SSVAL(p,20,mode);
1649                 p += 23;
1650                 nameptr = p;
1651                 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1652                         p += ucs2_align(base_data, p, 0);
1653                 }
1654                 len = srvstr_push(base_data, flags2, p,
1655                                   fname, PTR_DIFF(end_data, p),
1656                                   STR_TERMINATE);
1657                 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1658                         if (len > 2) {
1659                                 SCVAL(nameptr, -1, len - 2);
1660                         } else {
1661                                 SCVAL(nameptr, -1, 0);
1662                         }
1663                 } else {
1664                         if (len > 1) {
1665                                 SCVAL(nameptr, -1, len - 1);
1666                         } else {
1667                                 SCVAL(nameptr, -1, 0);
1668                         }
1669                 }
1670                 p += len;
1671                 break;
1672
1673         case SMB_FIND_EA_SIZE:
1674                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_SIZE\n"));
1675                 if (requires_resume_key) {
1676                         SIVAL(p,0,reskey);
1677                         p += 4;
1678                 }
1679                 srv_put_dos_date2(p,0,create_date);
1680                 srv_put_dos_date2(p,4,adate);
1681                 srv_put_dos_date2(p,8,mdate);
1682                 SIVAL(p,12,(uint32)file_size);
1683                 SIVAL(p,16,(uint32)allocation_size);
1684                 SSVAL(p,20,mode);
1685                 {
1686                         unsigned int ea_size = estimate_ea_size(conn, NULL,
1687                                                                 smb_fname);
1688                         SIVAL(p,22,ea_size); /* Extended attributes */
1689                 }
1690                 p += 27;
1691                 nameptr = p - 1;
1692                 len = srvstr_push(base_data, flags2,
1693                                   p, fname, PTR_DIFF(end_data, p),
1694                                   STR_TERMINATE | STR_NOALIGN);
1695                 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1696                         if (len > 2) {
1697                                 len -= 2;
1698                         } else {
1699                                 len = 0;
1700                         }
1701                 } else {
1702                         if (len > 1) {
1703                                 len -= 1;
1704                         } else {
1705                                 len = 0;
1706                         }
1707                 }
1708                 SCVAL(nameptr,0,len);
1709                 p += len;
1710                 SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1711                 break;
1712
1713         case SMB_FIND_EA_LIST:
1714         {
1715                 struct ea_list *file_list = NULL;
1716                 size_t ea_len = 0;
1717                 NTSTATUS status;
1718
1719                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_LIST\n"));
1720                 if (!name_list) {
1721                         return false;
1722                 }
1723                 if (requires_resume_key) {
1724                         SIVAL(p,0,reskey);
1725                         p += 4;
1726                 }
1727                 srv_put_dos_date2(p,0,create_date);
1728                 srv_put_dos_date2(p,4,adate);
1729                 srv_put_dos_date2(p,8,mdate);
1730                 SIVAL(p,12,(uint32)file_size);
1731                 SIVAL(p,16,(uint32)allocation_size);
1732                 SSVAL(p,20,mode);
1733                 p += 22; /* p now points to the EA area. */
1734
1735                 status = get_ea_list_from_file(ctx, conn, NULL,
1736                                                smb_fname,
1737                                                &ea_len, &file_list);
1738                 if (!NT_STATUS_IS_OK(status)) {
1739                         file_list = NULL;
1740                 }
1741                 name_list = ea_list_union(name_list, file_list, &ea_len);
1742
1743                 /* We need to determine if this entry will fit in the space available. */
1744                 /* Max string size is 255 bytes. */
1745                 if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
1746                         *out_of_space = true;
1747                         DEBUG(9,("smbd_marshall_dir_entry: out of space "
1748                                 "(wanted %u, had %d)\n",
1749                                 (unsigned int)PTR_DIFF(p + 255 + ea_len,pdata),
1750                                 space_remaining ));
1751                         return False; /* Not finished - just out of space */
1752                 }
1753
1754                 /* Push the ea_data followed by the name. */
1755                 p += fill_ea_buffer(ctx, p, space_remaining, conn, name_list);
1756                 nameptr = p;
1757                 len = srvstr_push(base_data, flags2,
1758                                   p + 1, fname, PTR_DIFF(end_data, p+1),
1759                                   STR_TERMINATE | STR_NOALIGN);
1760                 if (flags2 & FLAGS2_UNICODE_STRINGS) {
1761                         if (len > 2) {
1762                                 len -= 2;
1763                         } else {
1764                                 len = 0;
1765                         }
1766                 } else {
1767                         if (len > 1) {
1768                                 len -= 1;
1769                         } else {
1770                                 len = 0;
1771                         }
1772                 }
1773                 SCVAL(nameptr,0,len);
1774                 p += len + 1;
1775                 SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1776                 break;
1777         }
1778
1779         case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1780                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
1781                 was_8_3 = mangle_is_8_3(fname, True, conn->params);
1782                 p += 4;
1783                 SIVAL(p,0,reskey); p += 4;
1784                 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1785                 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1786                 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1787                 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1788                 SOFF_T(p,0,file_size); p += 8;
1789                 SOFF_T(p,0,allocation_size); p += 8;
1790                 SIVAL(p,0,mode); p += 4;
1791                 q = p; p += 4; /* q is placeholder for name length. */
1792                 {
1793                         unsigned int ea_size = estimate_ea_size(conn, NULL,
1794                                                                 smb_fname);
1795                         SIVAL(p,0,ea_size); /* Extended attributes */
1796                         p += 4;
1797                 }
1798                 /* Clear the short name buffer. This is
1799                  * IMPORTANT as not doing so will trigger
1800                  * a Win2k client bug. JRA.
1801                  */
1802                 if (!was_8_3 && check_mangled_names) {
1803                         char mangled_name[13]; /* mangled 8.3 name. */
1804                         if (!name_to_8_3(fname,mangled_name,True,
1805                                            conn->params)) {
1806                                 /* Error - mangle failed ! */
1807                                 memset(mangled_name,'\0',12);
1808                         }
1809                         mangled_name[12] = 0;
1810                         len = srvstr_push(base_data, flags2,
1811                                           p+2, mangled_name, 24,
1812                                           STR_UPPER|STR_UNICODE);
1813                         if (len < 24) {
1814                                 memset(p + 2 + len,'\0',24 - len);
1815                         }
1816                         SSVAL(p, 0, len);
1817                 } else {
1818                         memset(p,'\0',26);
1819                 }
1820                 p += 2 + 24;
1821                 len = srvstr_push(base_data, flags2, p,
1822                                   fname, PTR_DIFF(end_data, p),
1823                                   STR_TERMINATE_ASCII);
1824                 SIVAL(q,0,len);
1825                 p += len;
1826
1827                 len = PTR_DIFF(p, pdata);
1828                 pad = (len + (align-1)) & ~(align-1);
1829                 /*
1830                  * offset to the next entry, the caller
1831                  * will overwrite it for the last entry
1832                  * that's why we always include the padding
1833                  */
1834                 SIVAL(pdata,0,pad);
1835                 /*
1836                  * set padding to zero
1837                  */
1838                 if (do_pad) {
1839                         memset(p, 0, pad - len);
1840                         p = pdata + pad;
1841                 } else {
1842                         p = pdata + len;
1843                 }
1844                 break;
1845
1846         case SMB_FIND_FILE_DIRECTORY_INFO:
1847                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
1848                 p += 4;
1849                 SIVAL(p,0,reskey); p += 4;
1850                 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1851                 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1852                 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1853                 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1854                 SOFF_T(p,0,file_size); p += 8;
1855                 SOFF_T(p,0,allocation_size); p += 8;
1856                 SIVAL(p,0,mode); p += 4;
1857                 len = srvstr_push(base_data, flags2,
1858                                   p + 4, fname, PTR_DIFF(end_data, p+4),
1859                                   STR_TERMINATE_ASCII);
1860                 SIVAL(p,0,len);
1861                 p += 4 + len;
1862
1863                 len = PTR_DIFF(p, pdata);
1864                 pad = (len + (align-1)) & ~(align-1);
1865                 /*
1866                  * offset to the next entry, the caller
1867                  * will overwrite it for the last entry
1868                  * that's why we always include the padding
1869                  */
1870                 SIVAL(pdata,0,pad);
1871                 /*
1872                  * set padding to zero
1873                  */
1874                 if (do_pad) {
1875                         memset(p, 0, pad - len);
1876                         p = pdata + pad;
1877                 } else {
1878                         p = pdata + len;
1879                 }
1880                 break;
1881
1882         case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1883                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
1884                 p += 4;
1885                 SIVAL(p,0,reskey); p += 4;
1886                 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1887                 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1888                 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1889                 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1890                 SOFF_T(p,0,file_size); p += 8;
1891                 SOFF_T(p,0,allocation_size); p += 8;
1892                 SIVAL(p,0,mode); p += 4;
1893                 q = p; p += 4; /* q is placeholder for name length. */
1894                 {
1895                         unsigned int ea_size = estimate_ea_size(conn, NULL,
1896                                                                 smb_fname);
1897                         SIVAL(p,0,ea_size); /* Extended attributes */
1898                         p +=4;
1899                 }
1900                 len = srvstr_push(base_data, flags2, p,
1901                                   fname, PTR_DIFF(end_data, p),
1902                                   STR_TERMINATE_ASCII);
1903                 SIVAL(q, 0, len);
1904                 p += len;
1905
1906                 len = PTR_DIFF(p, pdata);
1907                 pad = (len + (align-1)) & ~(align-1);
1908                 /*
1909                  * offset to the next entry, the caller
1910                  * will overwrite it for the last entry
1911                  * that's why we always include the padding
1912                  */
1913                 SIVAL(pdata,0,pad);
1914                 /*
1915                  * set padding to zero
1916                  */
1917                 if (do_pad) {
1918                         memset(p, 0, pad - len);
1919                         p = pdata + pad;
1920                 } else {
1921                         p = pdata + len;
1922                 }
1923                 break;
1924
1925         case SMB_FIND_FILE_NAMES_INFO:
1926                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
1927                 p += 4;
1928                 SIVAL(p,0,reskey); p += 4;
1929                 p += 4;
1930                 /* this must *not* be null terminated or w2k gets in a loop trying to set an
1931                    acl on a dir (tridge) */
1932                 len = srvstr_push(base_data, flags2, p,
1933                                   fname, PTR_DIFF(end_data, p),
1934                                   STR_TERMINATE_ASCII);
1935                 SIVAL(p, -4, len);
1936                 p += len;
1937
1938                 len = PTR_DIFF(p, pdata);
1939                 pad = (len + (align-1)) & ~(align-1);
1940                 /*
1941                  * offset to the next entry, the caller
1942                  * will overwrite it for the last entry
1943                  * that's why we always include the padding
1944                  */
1945                 SIVAL(pdata,0,pad);
1946                 /*
1947                  * set padding to zero
1948                  */
1949                 if (do_pad) {
1950                         memset(p, 0, pad - len);
1951                         p = pdata + pad;
1952                 } else {
1953                         p = pdata + len;
1954                 }
1955                 break;
1956
1957         case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1958                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
1959                 p += 4;
1960                 SIVAL(p,0,reskey); p += 4;
1961                 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
1962                 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
1963                 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
1964                 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
1965                 SOFF_T(p,0,file_size); p += 8;
1966                 SOFF_T(p,0,allocation_size); p += 8;
1967                 SIVAL(p,0,mode); p += 4;
1968                 q = p; p += 4; /* q is placeholder for name length. */
1969                 {
1970                         unsigned int ea_size = estimate_ea_size(conn, NULL,
1971                                                                 smb_fname);
1972                         SIVAL(p,0,ea_size); /* Extended attributes */
1973                         p +=4;
1974                 }
1975                 SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
1976                 SBVAL(p,0,file_index); p += 8;
1977                 len = srvstr_push(base_data, flags2, p,
1978                                   fname, PTR_DIFF(end_data, p),
1979                                   STR_TERMINATE_ASCII);
1980                 SIVAL(q, 0, len);
1981                 p += len;
1982
1983                 len = PTR_DIFF(p, pdata);
1984                 pad = (len + (align-1)) & ~(align-1);
1985                 /*
1986                  * offset to the next entry, the caller
1987                  * will overwrite it for the last entry
1988                  * that's why we always include the padding
1989                  */
1990                 SIVAL(pdata,0,pad);
1991                 /*
1992                  * set padding to zero
1993                  */
1994                 if (do_pad) {
1995                         memset(p, 0, pad - len);
1996                         p = pdata + pad;
1997                 } else {
1998                         p = pdata + len;
1999                 }
2000                 break;
2001
2002         case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2003                 DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
2004                 was_8_3 = mangle_is_8_3(fname, True, conn->params);
2005                 p += 4;
2006                 SIVAL(p,0,reskey); p += 4;
2007                 put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
2008                 put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
2009                 put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
2010                 put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
2011                 SOFF_T(p,0,file_size); p += 8;
2012                 SOFF_T(p,0,allocation_size); p += 8;
2013                 SIVAL(p,0,mode); p += 4;
2014                 q = p; p += 4; /* q is placeholder for name length */
2015                 {
2016                         unsigned int ea_size = estimate_ea_size(conn, NULL,
2017                                                                 smb_fname);
2018                         SIVAL(p,0,ea_size); /* Extended attributes */
2019                         p +=4;
2020                 }
2021                 /* Clear the short name buffer. This is
2022                  * IMPORTANT as not doing so will trigger
2023                  * a Win2k client bug. JRA.
2024                  */
2025                 if (!was_8_3 && check_mangled_names) {
2026                         char mangled_name[13]; /* mangled 8.3 name. */
2027                         if (!name_to_8_3(fname,mangled_name,True,
2028                                         conn->params)) {
2029                                 /* Error - mangle failed ! */
2030                                 memset(mangled_name,'\0',12);
2031                         }
2032                         mangled_name[12] = 0;
2033                         len = srvstr_push(base_data, flags2,
2034                                           p+2, mangled_name, 24,
2035                                           STR_UPPER|STR_UNICODE);
2036                         SSVAL(p, 0, len);
2037                         if (len < 24) {
2038                                 memset(p + 2 + len,'\0',24 - len);
2039                         }
2040                         SSVAL(p, 0, len);
2041                 } else {
2042                         memset(p,'\0',26);
2043                 }
2044                 p += 26;
2045                 SSVAL(p,0,0); p += 2; /* Reserved ? */
2046                 SBVAL(p,0,file_index); p += 8;
2047                 len = srvstr_push(base_data, flags2, p,
2048                                   fname, PTR_DIFF(end_data, p),
2049                                   STR_TERMINATE_ASCII);
2050                 SIVAL(q,0,len);
2051                 p += len;
2052
2053                 len = PTR_DIFF(p, pdata);
2054                 pad = (len + (align-1)) & ~(align-1);
2055                 /*
2056                  * offset to the next entry, the caller
2057                  * will overwrite it for the last entry
2058                  * that's why we always include the padding
2059                  */
2060                 SIVAL(pdata,0,pad);
2061                 /*
2062                  * set padding to zero
2063                  */
2064                 if (do_pad) {
2065                         memset(p, 0, pad - len);
2066                         p = pdata + pad;
2067                 } else {
2068                         p = pdata + len;
2069                 }
2070                 break;
2071
2072         /* CIFS UNIX Extension. */
2073
2074         case SMB_FIND_FILE_UNIX:
2075         case SMB_FIND_FILE_UNIX_INFO2:
2076                 p+= 4;
2077                 SIVAL(p,0,reskey); p+= 4;    /* Used for continuing search. */
2078
2079                 /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
2080
2081                 if (info_level == SMB_FIND_FILE_UNIX) {
2082                         DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX\n"));
2083                         p = store_file_unix_basic(conn, p,
2084                                                 NULL, &smb_fname->st);
2085                         len = srvstr_push(base_data, flags2, p,
2086                                           fname, PTR_DIFF(end_data, p),
2087                                           STR_TERMINATE);
2088                 } else {
2089                         DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
2090                         p = store_file_unix_basic_info2(conn, p,
2091                                                 NULL, &smb_fname->st);
2092                         nameptr = p;
2093                         p += 4;
2094                         len = srvstr_push(base_data, flags2, p, fname,
2095                                           PTR_DIFF(end_data, p), 0);
2096                         SIVAL(nameptr, 0, len);
2097                 }
2098
2099                 p += len;
2100
2101                 len = PTR_DIFF(p, pdata);
2102                 pad = (len + (align-1)) & ~(align-1);
2103                 /*
2104                  * offset to the next entry, the caller
2105                  * will overwrite it for the last entry
2106                  * that's why we always include the padding
2107                  */
2108                 SIVAL(pdata,0,pad);
2109                 /*
2110                  * set padding to zero
2111                  */
2112                 if (do_pad) {
2113                         memset(p, 0, pad - len);
2114                         p = pdata + pad;
2115                 } else {
2116                         p = pdata + len;
2117                 }
2118                 /* End of SMB_QUERY_FILE_UNIX_BASIC */
2119
2120                 break;
2121
2122         default:
2123                 return false;
2124         }
2125
2126         if (PTR_DIFF(p,pdata) > space_remaining) {
2127                 *out_of_space = true;
2128                 DEBUG(9,("smbd_marshall_dir_entry: out of space "
2129                         "(wanted %u, had %d)\n",
2130                         (unsigned int)PTR_DIFF(p,pdata),
2131                         space_remaining ));
2132                 return false; /* Not finished - just out of space */
2133         }
2134
2135         /* Setup the last entry pointer, as an offset from base_data */
2136         *last_entry_off = PTR_DIFF(last_entry_ptr,base_data);
2137         /* Advance the data pointer to the next slot */
2138         *ppdata = p;
2139
2140         return true;
2141 }
2142
2143 bool smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
2144                                connection_struct *conn,
2145                                struct dptr_struct *dirptr,
2146                                uint16 flags2,
2147                                const char *path_mask,
2148                                uint32 dirtype,
2149                                int info_level,
2150                                int requires_resume_key,
2151                                bool dont_descend,
2152                                bool ask_sharemode,
2153                                uint8_t align,
2154                                bool do_pad,
2155                                char **ppdata,
2156                                char *base_data,
2157                                char *end_data,
2158                                int space_remaining,
2159                                bool *out_of_space,
2160                                bool *got_exact_match,
2161                                int *_last_entry_off,
2162                                struct ea_list *name_list)
2163 {
2164         const char *p;
2165         const char *mask = NULL;
2166         long prev_dirpos = 0;
2167         uint32_t mode = 0;
2168         char *fname = NULL;
2169         struct smb_filename *smb_fname = NULL;
2170         struct smbd_dirptr_lanman2_state state;
2171         bool ok;
2172         uint64_t last_entry_off = 0;
2173
2174         ZERO_STRUCT(state);
2175         state.conn = conn;
2176         state.info_level = info_level;
2177         state.check_mangled_names = lp_manglednames(conn->params);
2178         state.has_wild = dptr_has_wild(dirptr);
2179         state.got_exact_match = false;
2180
2181         *out_of_space = false;
2182         *got_exact_match = false;
2183
2184         p = strrchr_m(path_mask,'/');
2185         if(p != NULL) {
2186                 if(p[1] == '\0') {
2187                         mask = "*.*";
2188                 } else {
2189                         mask = p+1;
2190                 }
2191         } else {
2192                 mask = path_mask;
2193         }
2194
2195         ok = smbd_dirptr_get_entry(ctx,
2196                                    dirptr,
2197                                    mask,
2198                                    dirtype,
2199                                    dont_descend,
2200                                    ask_sharemode,
2201                                    smbd_dirptr_lanman2_match_fn,
2202                                    smbd_dirptr_lanman2_mode_fn,
2203                                    &state,
2204                                    &fname,
2205                                    &smb_fname,
2206                                    &mode,
2207                                    &prev_dirpos);
2208         if (!ok) {
2209                 return false;
2210         }
2211
2212         *got_exact_match = state.got_exact_match;
2213
2214         ok = smbd_marshall_dir_entry(ctx,
2215                                      conn,
2216                                      flags2,
2217                                      info_level,
2218                                      name_list,
2219                                      state.check_mangled_names,
2220                                      requires_resume_key,
2221                                      mode,
2222                                      fname,
2223                                      smb_fname,
2224                                      space_remaining,
2225                                      align,
2226                                      do_pad,
2227                                      base_data,
2228                                      ppdata,
2229                                      end_data,
2230                                      out_of_space,
2231                                      &last_entry_off);
2232         TALLOC_FREE(fname);
2233         TALLOC_FREE(smb_fname);
2234         if (*out_of_space) {
2235                 dptr_SeekDir(dirptr, prev_dirpos);
2236                 return false;
2237         }
2238         if (!ok) {
2239                 return false;
2240         }
2241
2242         *_last_entry_off = last_entry_off;
2243         return true;
2244 }
2245
2246 static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
2247                                 connection_struct *conn,
2248                                 struct dptr_struct *dirptr,
2249                                 uint16 flags2,
2250                                 const char *path_mask,
2251                                 uint32 dirtype,
2252                                 int info_level,
2253                                 bool requires_resume_key,
2254                                 bool dont_descend,
2255                                 bool ask_sharemode,
2256                                 char **ppdata,
2257                                 char *base_data,
2258                                 char *end_data,
2259                                 int space_remaining,
2260                                 bool *out_of_space,
2261                                 bool *got_exact_match,
2262                                 int *last_entry_off,
2263                                 struct ea_list *name_list)
2264 {
2265         uint8_t align = 4;
2266         const bool do_pad = true;
2267
2268         if (info_level >= 1 && info_level <= 3) {
2269                 /* No alignment on earlier info levels. */
2270                 align = 1;
2271         }
2272
2273         return smbd_dirptr_lanman2_entry(ctx, conn, dirptr, flags2,
2274                                          path_mask, dirtype, info_level,
2275                                          requires_resume_key, dont_descend, ask_sharemode,
2276                                          align, do_pad,
2277                                          ppdata, base_data, end_data,
2278                                          space_remaining,
2279                                          out_of_space, got_exact_match,
2280                                          last_entry_off, name_list);
2281 }
2282
2283 /****************************************************************************
2284  Reply to a TRANS2_FINDFIRST.
2285 ****************************************************************************/
2286
2287 static void call_trans2findfirst(connection_struct *conn,
2288                                  struct smb_request *req,
2289                                  char **pparams, int total_params,
2290                                  char **ppdata, int total_data,
2291                                  unsigned int max_data_bytes)
2292 {
2293         /* We must be careful here that we don't return more than the
2294                 allowed number of data bytes. If this means returning fewer than
2295                 maxentries then so be it. We assume that the redirector has
2296                 enough room for the fixed number of parameter bytes it has
2297                 requested. */
2298         struct smb_filename *smb_dname = NULL;
2299         char *params = *pparams;
2300         char *pdata = *ppdata;
2301         char *data_end;
2302         uint32 dirtype;
2303         int maxentries;
2304         uint16 findfirst_flags;
2305         bool close_after_first;
2306         bool close_if_end;
2307         bool requires_resume_key;
2308         int info_level;
2309         char *directory = NULL;
2310         char *mask = NULL;
2311         char *p;
2312         int last_entry_off=0;
2313         int dptr_num = -1;
2314         int numentries = 0;
2315         int i;
2316         bool finished = False;
2317         bool dont_descend = False;
2318         bool out_of_space = False;
2319         int space_remaining;
2320         bool mask_contains_wcard = False;
2321         struct ea_list *ea_list = NULL;
2322         NTSTATUS ntstatus = NT_STATUS_OK;
2323         bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
2324         TALLOC_CTX *ctx = talloc_tos();
2325         struct dptr_struct *dirptr = NULL;
2326         struct smbd_server_connection *sconn = req->sconn;
2327         uint32_t ucf_flags = (UCF_SAVE_LCOMP | UCF_ALWAYS_ALLOW_WCARD_LCOMP);
2328         bool backup_priv = false;
2329
2330         if (total_params < 13) {
2331                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2332                 goto out;
2333         }
2334
2335         dirtype = SVAL(params,0);
2336         maxentries = SVAL(params,2);
2337         findfirst_flags = SVAL(params,4);
2338         close_after_first = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE);
2339         close_if_end = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2340         requires_resume_key = (findfirst_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2341         backup_priv = ((findfirst_flags & FLAG_TRANS2_FIND_BACKUP_INTENT) &&
2342                                 security_token_has_privilege(get_current_nttok(conn),
2343                                                 SEC_PRIV_BACKUP));
2344
2345         info_level = SVAL(params,6);
2346
2347         DEBUG(3,("call_trans2findfirst: dirtype = %x, maxentries = %d, close_after_first=%d, \
2348 close_if_end = %d requires_resume_key = %d backup_priv = %d level = 0x%x, max_data_bytes = %d\n",
2349                 (unsigned int)dirtype, maxentries, close_after_first, close_if_end, requires_resume_key,
2350                 (int)backup_priv,
2351                 info_level, max_data_bytes));
2352
2353         if (!maxentries) {
2354                 /* W2K3 seems to treat zero as 1. */
2355                 maxentries = 1;
2356         }
2357
2358         switch (info_level) {
2359                 case SMB_FIND_INFO_STANDARD:
2360                 case SMB_FIND_EA_SIZE:
2361                 case SMB_FIND_EA_LIST:
2362                 case SMB_FIND_FILE_DIRECTORY_INFO:
2363                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2364                 case SMB_FIND_FILE_NAMES_INFO:
2365                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2366                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2367                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2368                         break;
2369                 case SMB_FIND_FILE_UNIX:
2370                 case SMB_FIND_FILE_UNIX_INFO2:
2371                         /* Always use filesystem for UNIX mtime query. */
2372                         ask_sharemode = false;
2373                         if (!lp_unix_extensions()) {
2374                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2375                                 goto out;
2376                         }
2377                         ucf_flags |= UCF_UNIX_NAME_LOOKUP;
2378                         break;
2379                 default:
2380                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2381                         goto out;
2382         }
2383
2384         srvstr_get_path_wcard(ctx, params, req->flags2, &directory,
2385                               params+12, total_params - 12,
2386                               STR_TERMINATE, &ntstatus, &mask_contains_wcard);
2387         if (!NT_STATUS_IS_OK(ntstatus)) {
2388                 reply_nterror(req, ntstatus);
2389                 goto out;
2390         }
2391
2392         if (backup_priv) {
2393                 become_root();
2394                 ntstatus = filename_convert_with_privilege(ctx,
2395                                 conn,
2396                                 req,
2397                                 directory,
2398                                 ucf_flags,
2399                                 &mask_contains_wcard,
2400                                 &smb_dname);
2401         } else {
2402                 ntstatus = filename_convert(ctx, conn,
2403                                     req->flags2 & FLAGS2_DFS_PATHNAMES,
2404                                     directory,
2405                                     ucf_flags,
2406                                     &mask_contains_wcard,
2407                                     &smb_dname);
2408         }
2409
2410         if (!NT_STATUS_IS_OK(ntstatus)) {
2411                 if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
2412                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2413                                         ERRSRV, ERRbadpath);
2414                         goto out;
2415                 }
2416                 reply_nterror(req, ntstatus);
2417                 goto out;
2418         }
2419
2420         mask = smb_dname->original_lcomp;
2421
2422         directory = smb_dname->base_name;
2423
2424         p = strrchr_m(directory,'/');
2425         if(p == NULL) {
2426                 /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
2427                 if((directory[0] == '.') && (directory[1] == '\0')) {
2428                         mask = talloc_strdup(ctx,"*");
2429                         if (!mask) {
2430                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2431                                 goto out;
2432                         }
2433                         mask_contains_wcard = True;
2434                 }
2435         } else {
2436                 *p = 0;
2437         }
2438
2439         if (p == NULL || p == directory) {
2440                 /* Ensure we don't have a directory name of "". */
2441                 directory = talloc_strdup(talloc_tos(), ".");
2442                 if (!directory) {
2443                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2444                         goto out;
2445                 }
2446         }
2447
2448         DEBUG(5,("dir=%s, mask = %s\n",directory, mask));
2449
2450         if (info_level == SMB_FIND_EA_LIST) {
2451                 uint32 ea_size;
2452
2453                 if (total_data < 4) {
2454                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2455                         goto out;
2456                 }
2457
2458                 ea_size = IVAL(pdata,0);
2459                 if (ea_size != total_data) {
2460                         DEBUG(4,("call_trans2findfirst: Rejecting EA request with incorrect \
2461 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2462                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2463                         goto out;
2464                 }
2465
2466                 if (!lp_ea_support(SNUM(conn))) {
2467                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
2468                         goto out;
2469                 }
2470
2471                 /* Pull out the list of names. */
2472                 ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
2473                 if (!ea_list) {
2474                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2475                         goto out;
2476                 }
2477         }
2478
2479         *ppdata = (char *)SMB_REALLOC(
2480                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2481         if(*ppdata == NULL ) {
2482                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2483                 goto out;
2484         }
2485         pdata = *ppdata;
2486         data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2487
2488         /* Realloc the params space */
2489         *pparams = (char *)SMB_REALLOC(*pparams, 10);
2490         if (*pparams == NULL) {
2491                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2492                 goto out;
2493         }
2494         params = *pparams;
2495
2496         /* Save the wildcard match and attribs we are using on this directory -
2497                 needed as lanman2 assumes these are being saved between calls */
2498
2499         ntstatus = dptr_create(conn,
2500                                 req,
2501                                 NULL, /* fsp */
2502                                 directory,
2503                                 False,
2504                                 True,
2505                                 req->smbpid,
2506                                 mask,
2507                                 mask_contains_wcard,
2508                                 dirtype,
2509                                 &dirptr);
2510
2511         if (!NT_STATUS_IS_OK(ntstatus)) {
2512                 reply_nterror(req, ntstatus);
2513                 goto out;
2514         }
2515
2516         if (backup_priv) {
2517                 /* Remember this in case we have
2518                    to do a findnext. */
2519                 dptr_set_priv(dirptr);
2520         }
2521
2522         dptr_num = dptr_dnum(dirptr);
2523         DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
2524
2525         /* Initialize per TRANS2_FIND_FIRST operation data */
2526         dptr_init_search_op(dirptr);
2527
2528         /* We don't need to check for VOL here as this is returned by
2529                 a different TRANS2 call. */
2530
2531         DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
2532                  directory,lp_dontdescend(ctx, SNUM(conn))));
2533         if (in_list(directory,lp_dontdescend(ctx, SNUM(conn)),conn->case_sensitive))
2534                 dont_descend = True;
2535
2536         p = pdata;
2537         space_remaining = max_data_bytes;
2538         out_of_space = False;
2539
2540         for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
2541                 bool got_exact_match = False;
2542
2543                 /* this is a heuristic to avoid seeking the dirptr except when
2544                         absolutely necessary. It allows for a filename of about 40 chars */
2545                 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2546                         out_of_space = True;
2547                         finished = False;
2548                 } else {
2549                         finished = !get_lanman2_dir_entry(ctx,
2550                                         conn,
2551                                         dirptr,
2552                                         req->flags2,
2553                                         mask,dirtype,info_level,
2554                                         requires_resume_key,dont_descend,
2555                                         ask_sharemode,
2556                                         &p,pdata,data_end,
2557                                         space_remaining, &out_of_space,
2558                                         &got_exact_match,
2559                                         &last_entry_off, ea_list);
2560                 }
2561
2562                 if (finished && out_of_space)
2563                         finished = False;
2564
2565                 if (!finished && !out_of_space)
2566                         numentries++;
2567
2568                 /*
2569                  * As an optimisation if we know we aren't looking
2570                  * for a wildcard name (ie. the name matches the wildcard exactly)
2571                  * then we can finish on any (first) match.
2572                  * This speeds up large directory searches. JRA.
2573                  */
2574
2575                 if(got_exact_match)
2576                         finished = True;
2577
2578                 /* Ensure space_remaining never goes -ve. */
2579                 if (PTR_DIFF(p,pdata) > max_data_bytes) {
2580                         space_remaining = 0;
2581                         out_of_space = true;
2582                 } else {
2583                         space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2584                 }
2585         }
2586
2587         /* Check if we can close the dirptr */
2588         if(close_after_first || (finished && close_if_end)) {
2589                 DEBUG(5,("call_trans2findfirst - (2) closing dptr_num %d\n", dptr_num));
2590                 dptr_close(sconn, &dptr_num);
2591         }
2592
2593         /*
2594          * If there are no matching entries we must return ERRDOS/ERRbadfile -
2595          * from observation of NT. NB. This changes to ERRDOS,ERRnofiles if
2596          * the protocol level is less than NT1. Tested with smbclient. JRA.
2597          * This should fix the OS/2 client bug #2335.
2598          */
2599
2600         if(numentries == 0) {
2601                 dptr_close(sconn, &dptr_num);
2602                 if (get_Protocol() < PROTOCOL_NT1) {
2603                         reply_force_doserror(req, ERRDOS, ERRnofiles);
2604                         goto out;
2605                 } else {
2606                         reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
2607                                         ERRDOS, ERRbadfile);
2608                         goto out;
2609                 }
2610         }
2611
2612         /* At this point pdata points to numentries directory entries. */
2613
2614         /* Set up the return parameter block */
2615         SSVAL(params,0,dptr_num);
2616         SSVAL(params,2,numentries);
2617         SSVAL(params,4,finished);
2618         SSVAL(params,6,0); /* Never an EA error */
2619         SSVAL(params,8,last_entry_off);
2620
2621         send_trans2_replies(conn, req, params, 10, pdata, PTR_DIFF(p,pdata),
2622                             max_data_bytes);
2623
2624         if ((! *directory) && dptr_path(sconn, dptr_num)) {
2625                 directory = talloc_strdup(talloc_tos(),dptr_path(sconn, dptr_num));
2626                 if (!directory) {
2627                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2628                 }
2629         }
2630
2631         DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2632                 smb_fn_name(req->cmd),
2633                 mask, directory, dirtype, numentries ) );
2634
2635         /*
2636          * Force a name mangle here to ensure that the
2637          * mask as an 8.3 name is top of the mangled cache.
2638          * The reasons for this are subtle. Don't remove
2639          * this code unless you know what you are doing
2640          * (see PR#13758). JRA.
2641          */
2642
2643         if(!mangle_is_8_3_wildcards( mask, False, conn->params)) {
2644                 char mangled_name[13];
2645                 name_to_8_3(mask, mangled_name, True, conn->params);
2646         }
2647  out:
2648
2649         if (backup_priv) {
2650                 unbecome_root();
2651         }
2652
2653         TALLOC_FREE(smb_dname);
2654         return;
2655 }
2656
2657 /****************************************************************************
2658  Reply to a TRANS2_FINDNEXT.
2659 ****************************************************************************/
2660
2661 static void call_trans2findnext(connection_struct *conn,
2662                                 struct smb_request *req,
2663                                 char **pparams, int total_params,
2664                                 char **ppdata, int total_data,
2665                                 unsigned int max_data_bytes)
2666 {
2667         /* We must be careful here that we don't return more than the
2668                 allowed number of data bytes. If this means returning fewer than
2669                 maxentries then so be it. We assume that the redirector has
2670                 enough room for the fixed number of parameter bytes it has
2671                 requested. */
2672         char *params = *pparams;
2673         char *pdata = *ppdata;
2674         char *data_end;
2675         int dptr_num;
2676         int maxentries;
2677         uint16 info_level;
2678         uint32 resume_key;
2679         uint16 findnext_flags;
2680         bool close_after_request;
2681         bool close_if_end;
2682         bool requires_resume_key;
2683         bool continue_bit;
2684         bool mask_contains_wcard = False;
2685         char *resume_name = NULL;
2686         const char *mask = NULL;
2687         const char *directory = NULL;
2688         char *p = NULL;
2689         uint16 dirtype;
2690         int numentries = 0;
2691         int i, last_entry_off=0;
2692         bool finished = False;
2693         bool dont_descend = False;
2694         bool out_of_space = False;
2695         int space_remaining;
2696         struct ea_list *ea_list = NULL;
2697         NTSTATUS ntstatus = NT_STATUS_OK;
2698         bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
2699         TALLOC_CTX *ctx = talloc_tos();
2700         struct dptr_struct *dirptr;
2701         struct smbd_server_connection *sconn = req->sconn;
2702         bool backup_priv = false; 
2703
2704         if (total_params < 13) {
2705                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2706                 return;
2707         }
2708
2709         dptr_num = SVAL(params,0);
2710         maxentries = SVAL(params,2);
2711         info_level = SVAL(params,4);
2712         resume_key = IVAL(params,6);
2713         findnext_flags = SVAL(params,10);
2714         close_after_request = (findnext_flags & FLAG_TRANS2_FIND_CLOSE);
2715         close_if_end = (findnext_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2716         requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2717         continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
2718
2719         if (!continue_bit) {
2720                 /* We only need resume_name if continue_bit is zero. */
2721                 srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
2722                               params+12,
2723                               total_params - 12, STR_TERMINATE, &ntstatus,
2724                               &mask_contains_wcard);
2725                 if (!NT_STATUS_IS_OK(ntstatus)) {
2726                         /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
2727                            complain (it thinks we're asking for the directory above the shared
2728                            path or an invalid name). Catch this as the resume name is only compared, never used in
2729                            a file access. JRA. */
2730                         srvstr_pull_talloc(ctx, params, req->flags2,
2731                                 &resume_name, params+12,
2732                                 total_params - 12,
2733                                 STR_TERMINATE);
2734
2735                         if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
2736                                 reply_nterror(req, ntstatus);
2737                                 return;
2738                         }
2739                 }
2740         }
2741
2742         DEBUG(3,("call_trans2findnext: dirhandle = %d, max_data_bytes = %d, maxentries = %d, \
2743 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
2744 resume_key = %d resume name = %s continue=%d level = %d\n",
2745                 dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end, 
2746                 requires_resume_key, resume_key,
2747                 resume_name ? resume_name : "(NULL)", continue_bit, info_level));
2748
2749         if (!maxentries) {
2750                 /* W2K3 seems to treat zero as 1. */
2751                 maxentries = 1;
2752         }
2753
2754         switch (info_level) {
2755                 case SMB_FIND_INFO_STANDARD:
2756                 case SMB_FIND_EA_SIZE:
2757                 case SMB_FIND_EA_LIST:
2758                 case SMB_FIND_FILE_DIRECTORY_INFO:
2759                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2760                 case SMB_FIND_FILE_NAMES_INFO:
2761                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2762                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2763                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2764                         break;
2765                 case SMB_FIND_FILE_UNIX:
2766                 case SMB_FIND_FILE_UNIX_INFO2:
2767                         /* Always use filesystem for UNIX mtime query. */
2768                         ask_sharemode = false;
2769                         if (!lp_unix_extensions()) {
2770                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2771                                 return;
2772                         }
2773                         break;
2774                 default:
2775                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2776                         return;
2777         }
2778
2779         if (info_level == SMB_FIND_EA_LIST) {
2780                 uint32 ea_size;
2781
2782                 if (total_data < 4) {
2783                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2784                         return;
2785                 }
2786
2787                 ea_size = IVAL(pdata,0);
2788                 if (ea_size != total_data) {
2789                         DEBUG(4,("call_trans2findnext: Rejecting EA request with incorrect \
2790 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2791                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2792                         return;
2793                 }
2794
2795                 if (!lp_ea_support(SNUM(conn))) {
2796                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
2797                         return;
2798                 }
2799
2800                 /* Pull out the list of names. */
2801                 ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
2802                 if (!ea_list) {
2803                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2804                         return;
2805                 }
2806         }
2807
2808         *ppdata = (char *)SMB_REALLOC(
2809                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2810         if(*ppdata == NULL) {
2811                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2812                 return;
2813         }
2814
2815         pdata = *ppdata;
2816         data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2817
2818         /* Realloc the params space */
2819         *pparams = (char *)SMB_REALLOC(*pparams, 6*SIZEOFWORD);
2820         if(*pparams == NULL ) {
2821                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2822                 return;
2823         }
2824
2825         params = *pparams;
2826
2827         /* Check that the dptr is valid */
2828         if(!(dirptr = dptr_fetch_lanman2(sconn, dptr_num))) {
2829                 reply_nterror(req, STATUS_NO_MORE_FILES);
2830                 return;
2831         }
2832
2833         directory = dptr_path(sconn, dptr_num);
2834
2835         /* Get the wildcard mask from the dptr */
2836         if((mask = dptr_wcard(sconn, dptr_num))== NULL) {
2837                 DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
2838                 reply_nterror(req, STATUS_NO_MORE_FILES);
2839                 return;
2840         }
2841
2842         /* Get the attr mask from the dptr */
2843         dirtype = dptr_attr(sconn, dptr_num);
2844
2845         backup_priv = dptr_get_priv(dirptr);
2846
2847         DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld) "
2848                 "backup_priv = %d\n",
2849                 dptr_num, mask, dirtype,
2850                 (long)dirptr,
2851                 dptr_TellDir(dirptr),
2852                 (int)backup_priv));
2853
2854         /* Initialize per TRANS2_FIND_NEXT operation data */
2855         dptr_init_search_op(dirptr);
2856
2857         /* We don't need to check for VOL here as this is returned by
2858                 a different TRANS2 call. */
2859
2860         DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
2861                  directory,lp_dontdescend(ctx, SNUM(conn))));
2862         if (in_list(directory,lp_dontdescend(ctx, SNUM(conn)),conn->case_sensitive))
2863                 dont_descend = True;
2864
2865         p = pdata;
2866         space_remaining = max_data_bytes;
2867         out_of_space = False;
2868
2869         if (backup_priv) {
2870                 become_root();
2871         }
2872
2873         /*
2874          * Seek to the correct position. We no longer use the resume key but
2875          * depend on the last file name instead.
2876          */
2877
2878         if(!continue_bit && resume_name && *resume_name) {
2879                 SMB_STRUCT_STAT st;
2880
2881                 long current_pos = 0;
2882                 /*
2883                  * Remember, name_to_8_3 is called by
2884                  * get_lanman2_dir_entry(), so the resume name
2885                  * could be mangled. Ensure we check the unmangled name.
2886                  */
2887
2888                 if (mangle_is_mangled(resume_name, conn->params)) {
2889                         char *new_resume_name = NULL;
2890                         mangle_lookup_name_from_8_3(ctx,
2891                                                 resume_name,
2892                                                 &new_resume_name,
2893                                                 conn->params);
2894                         if (new_resume_name) {
2895                                 resume_name = new_resume_name;
2896                         }
2897                 }
2898
2899                 /*
2900                  * Fix for NT redirector problem triggered by resume key indexes
2901                  * changing between directory scans. We now return a resume key of 0
2902                  * and instead look for the filename to continue from (also given
2903                  * to us by NT/95/smbfs/smbclient). If no other scans have been done between the
2904                  * findfirst/findnext (as is usual) then the directory pointer
2905                  * should already be at the correct place.
2906                  */
2907
2908                 finished = !dptr_SearchDir(dirptr, resume_name, &current_pos, &st);
2909         } /* end if resume_name && !continue_bit */
2910
2911         for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
2912                 bool got_exact_match = False;
2913
2914                 /* this is a heuristic to avoid seeking the dirptr except when 
2915                         absolutely necessary. It allows for a filename of about 40 chars */
2916                 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2917                         out_of_space = True;
2918                         finished = False;
2919                 } else {
2920                         finished = !get_lanman2_dir_entry(ctx,
2921                                                 conn,
2922                                                 dirptr,
2923                                                 req->flags2,
2924                                                 mask,dirtype,info_level,
2925                                                 requires_resume_key,dont_descend,
2926                                                 ask_sharemode,
2927                                                 &p,pdata,data_end,
2928                                                 space_remaining, &out_of_space,
2929                                                 &got_exact_match,
2930                                                 &last_entry_off, ea_list);
2931                 }
2932
2933                 if (finished && out_of_space)
2934                         finished = False;
2935
2936                 if (!finished && !out_of_space)
2937                         numentries++;
2938
2939                 /*
2940                  * As an optimisation if we know we aren't looking
2941                  * for a wildcard name (ie. the name matches the wildcard exactly)
2942                  * then we can finish on any (first) match.
2943                  * This speeds up large directory searches. JRA.
2944                  */
2945
2946                 if(got_exact_match)
2947                         finished = True;
2948
2949                 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2950         }
2951
2952         DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2953                 smb_fn_name(req->cmd),
2954                 mask, directory, dirtype, numentries ) );
2955
2956         /* Check if we can close the dirptr */
2957         if(close_after_request || (finished && close_if_end)) {
2958                 DEBUG(5,("call_trans2findnext: closing dptr_num = %d\n", dptr_num));
2959                 dptr_close(sconn, &dptr_num); /* This frees up the saved mask */
2960         }
2961
2962         if (backup_priv) {
2963                 unbecome_root();
2964         }
2965
2966         /* Set up the return parameter block */
2967         SSVAL(params,0,numentries);
2968         SSVAL(params,2,finished);
2969         SSVAL(params,4,0); /* Never an EA error */
2970         SSVAL(params,6,last_entry_off);
2971
2972         send_trans2_replies(conn, req, params, 8, pdata, PTR_DIFF(p,pdata),
2973                             max_data_bytes);
2974
2975         return;
2976 }
2977
2978 unsigned char *create_volume_objectid(connection_struct *conn, unsigned char objid[16])
2979 {
2980         E_md4hash(lp_servicename(talloc_tos(), SNUM(conn)),objid);
2981         return objid;
2982 }
2983
2984 static void samba_extended_info_version(struct smb_extended_info *extended_info)
2985 {
2986         SMB_ASSERT(extended_info != NULL);
2987
2988         extended_info->samba_magic = SAMBA_EXTENDED_INFO_MAGIC;
2989         extended_info->samba_version = ((SAMBA_VERSION_MAJOR & 0xff) << 24)
2990                                        | ((SAMBA_VERSION_MINOR & 0xff) << 16)
2991                                        | ((SAMBA_VERSION_RELEASE & 0xff) << 8);
2992 #ifdef SAMBA_VERSION_REVISION
2993         extended_info->samba_version |= (tolower(*SAMBA_VERSION_REVISION) - 'a' + 1) & 0xff;
2994 #endif
2995         extended_info->samba_subversion = 0;
2996 #ifdef SAMBA_VERSION_RC_RELEASE
2997         extended_info->samba_subversion |= (SAMBA_VERSION_RC_RELEASE & 0xff) << 24;
2998 #else
2999 #ifdef SAMBA_VERSION_PRE_RELEASE
3000         extended_info->samba_subversion |= (SAMBA_VERSION_PRE_RELEASE & 0xff) << 16;
3001 #endif
3002 #endif
3003 #ifdef SAMBA_VERSION_VENDOR_PATCH
3004         extended_info->samba_subversion |= (SAMBA_VERSION_VENDOR_PATCH & 0xffff);
3005 #endif
3006         extended_info->samba_gitcommitdate = 0;
3007 #ifdef SAMBA_VERSION_COMMIT_TIME
3008         unix_to_nt_time(&extended_info->samba_gitcommitdate, SAMBA_VERSION_COMMIT_TIME);
3009 #endif
3010
3011         memset(extended_info->samba_version_string, 0,
3012                sizeof(extended_info->samba_version_string));
3013
3014         snprintf (extended_info->samba_version_string,
3015                   sizeof(extended_info->samba_version_string),
3016                   "%s", samba_version_string());
3017 }
3018
3019 NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
3020                          TALLOC_CTX *mem_ctx,
3021                          uint16_t info_level,
3022                          uint16_t flags2,
3023                          unsigned int max_data_bytes,
3024                          char **ppdata,
3025                          int *ret_data_len)
3026 {
3027         char *pdata, *end_data;
3028         int data_len = 0, len;
3029         const char *vname = volume_label(talloc_tos(), SNUM(conn));
3030         int snum = SNUM(conn);
3031         char *fstype = lp_fstype(talloc_tos(), SNUM(conn));
3032         uint32 additional_flags = 0;
3033         struct smb_filename smb_fname_dot;
3034         SMB_STRUCT_STAT st;
3035
3036         if (IS_IPC(conn)) {
3037                 if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
3038                         DEBUG(0,("smbd_do_qfsinfo: not an allowed "
3039                                 "info level (0x%x) on IPC$.\n",
3040                                 (unsigned int)info_level));
3041                         return NT_STATUS_ACCESS_DENIED;
3042                 }
3043         }
3044
3045         DEBUG(3,("smbd_do_qfsinfo: level = %d\n", info_level));
3046
3047         ZERO_STRUCT(smb_fname_dot);
3048         smb_fname_dot.base_name = discard_const_p(char, ".");
3049
3050         if(SMB_VFS_STAT(conn, &smb_fname_dot) != 0) {
3051                 DEBUG(2,("stat of . failed (%s)\n", strerror(errno)));
3052                 return map_nt_error_from_unix(errno);
3053         }
3054
3055         st = smb_fname_dot.st;
3056
3057         *ppdata = (char *)SMB_REALLOC(
3058                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
3059         if (*ppdata == NULL) {
3060                 return NT_STATUS_NO_MEMORY;
3061         }
3062
3063         pdata = *ppdata;
3064         memset((char *)pdata,'\0',max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
3065         end_data = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
3066
3067         switch (info_level) {
3068                 case SMB_INFO_ALLOCATION:
3069                 {
3070                         uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3071                         data_len = 18;
3072                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3073                                 return map_nt_error_from_unix(errno);
3074                         }
3075
3076                         block_size = lp_block_size(snum);
3077                         if (bsize < block_size) {
3078                                 uint64_t factor = block_size/bsize;
3079                                 bsize = block_size;
3080                                 dsize /= factor;
3081                                 dfree /= factor;
3082                         }
3083                         if (bsize > block_size) {
3084                                 uint64_t factor = bsize/block_size;
3085                                 bsize = block_size;
3086                                 dsize *= factor;
3087                                 dfree *= factor;
3088                         }
3089                         bytes_per_sector = 512;
3090                         sectors_per_unit = bsize/bytes_per_sector;
3091
3092                         DEBUG(5,("smbd_do_qfsinfo : SMB_INFO_ALLOCATION id=%x, bsize=%u, cSectorUnit=%u, \
3093 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_ex_dev, (unsigned int)bsize, (unsigned int)sectors_per_unit,
3094                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3095
3096                         SIVAL(pdata,l1_idFileSystem,st.st_ex_dev);
3097                         SIVAL(pdata,l1_cSectorUnit,sectors_per_unit);
3098                         SIVAL(pdata,l1_cUnit,dsize);
3099                         SIVAL(pdata,l1_cUnitAvail,dfree);
3100                         SSVAL(pdata,l1_cbSector,bytes_per_sector);
3101                         break;
3102                 }
3103
3104                 case SMB_INFO_VOLUME:
3105                         /* Return volume name */
3106                         /* 
3107                          * Add volume serial number - hash of a combination of
3108                          * the called hostname and the service name.
3109                          */
3110                         SIVAL(pdata,0,str_checksum(lp_servicename(talloc_tos(), snum)) ^ (str_checksum(get_local_machine_name())<<16) );
3111                         /*
3112                          * Win2k3 and previous mess this up by sending a name length
3113                          * one byte short. I believe only older clients (OS/2 Win9x) use
3114                          * this call so try fixing this by adding a terminating null to
3115                          * the pushed string. The change here was adding the STR_TERMINATE. JRA.
3116                          */
3117                         len = srvstr_push(
3118                                 pdata, flags2,
3119                                 pdata+l2_vol_szVolLabel, vname,
3120                                 PTR_DIFF(end_data, pdata+l2_vol_szVolLabel),
3121                                 STR_NOALIGN|STR_TERMINATE);
3122                         SCVAL(pdata,l2_vol_cch,len);
3123                         data_len = l2_vol_szVolLabel + len;
3124                         DEBUG(5,("smbd_do_qfsinfo : time = %x, namelen = %d, name = %s\n",
3125                                  (unsigned)convert_timespec_to_time_t(st.st_ex_ctime),
3126                                  len, vname));
3127                         break;
3128
3129                 case SMB_QUERY_FS_ATTRIBUTE_INFO:
3130                 case SMB_FS_ATTRIBUTE_INFORMATION:
3131
3132                         additional_flags = 0;
3133 #if defined(HAVE_SYS_QUOTAS)
3134                         additional_flags |= FILE_VOLUME_QUOTAS;
3135 #endif
3136
3137                         if(lp_nt_acl_support(SNUM(conn))) {
3138                                 additional_flags |= FILE_PERSISTENT_ACLS;
3139                         }
3140
3141                         /* Capabilities are filled in at connection time through STATVFS call */
3142                         additional_flags |= conn->fs_capabilities;
3143                         additional_flags |= lp_parm_int(conn->params->service,
3144                                                         "share", "fake_fscaps",
3145                                                         0);
3146
3147                         SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
3148                                 FILE_SUPPORTS_OBJECT_IDS|FILE_UNICODE_ON_DISK|
3149                                 additional_flags); /* FS ATTRIBUTES */
3150
3151                         SIVAL(pdata,4,255); /* Max filename component length */
3152                         /* NOTE! the fstype must *not* be null terminated or win98 won't recognise it
3153                                 and will think we can't do long filenames */
3154                         len = srvstr_push(pdata, flags2, pdata+12, fstype,
3155                                           PTR_DIFF(end_data, pdata+12),
3156                                           STR_UNICODE);
3157                         SIVAL(pdata,8,len);
3158                         data_len = 12 + len;
3159                         break;
3160
3161                 case SMB_QUERY_FS_LABEL_INFO:
3162                 case SMB_FS_LABEL_INFORMATION:
3163                         len = srvstr_push(pdata, flags2, pdata+4, vname,
3164                                           PTR_DIFF(end_data, pdata+4), 0);
3165                         data_len = 4 + len;
3166                         SIVAL(pdata,0,len);
3167                         break;
3168
3169                 case SMB_QUERY_FS_VOLUME_INFO:      
3170                 case SMB_FS_VOLUME_INFORMATION:
3171
3172                         /* 
3173                          * Add volume serial number - hash of a combination of
3174                          * the called hostname and the service name.
3175                          */
3176                         SIVAL(pdata,8,str_checksum(lp_servicename(talloc_tos(), snum)) ^
3177                                 (str_checksum(get_local_machine_name())<<16));
3178
3179                         /* Max label len is 32 characters. */
3180                         len = srvstr_push(pdata, flags2, pdata+18, vname,
3181                                           PTR_DIFF(end_data, pdata+18),
3182                                           STR_UNICODE);
3183                         SIVAL(pdata,12,len);
3184                         data_len = 18+len;
3185
3186                         DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_VOLUME_INFO namelen = %d, vol=%s serv=%s\n",
3187                                 (int)strlen(vname),vname,
3188                                 lp_servicename(talloc_tos(), snum)));
3189                         break;
3190
3191                 case SMB_QUERY_FS_SIZE_INFO:
3192                 case SMB_FS_SIZE_INFORMATION:
3193                 {
3194                         uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3195                         data_len = 24;
3196                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3197                                 return map_nt_error_from_unix(errno);
3198                         }
3199                         block_size = lp_block_size(snum);
3200                         if (bsize < block_size) {
3201                                 uint64_t factor = block_size/bsize;
3202                                 bsize = block_size;
3203                                 dsize /= factor;
3204                                 dfree /= factor;
3205                         }
3206                         if (bsize > block_size) {
3207                                 uint64_t factor = bsize/block_size;
3208                                 bsize = block_size;
3209                                 dsize *= factor;
3210                                 dfree *= factor;
3211                         }
3212                         bytes_per_sector = 512;
3213                         sectors_per_unit = bsize/bytes_per_sector;
3214                         DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_SIZE_INFO bsize=%u, cSectorUnit=%u, \
3215 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
3216                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3217                         SBIG_UINT(pdata,0,dsize);
3218                         SBIG_UINT(pdata,8,dfree);
3219                         SIVAL(pdata,16,sectors_per_unit);
3220                         SIVAL(pdata,20,bytes_per_sector);
3221                         break;
3222                 }
3223
3224                 case SMB_FS_FULL_SIZE_INFORMATION:
3225                 {
3226                         uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
3227                         data_len = 32;
3228                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
3229                                 return map_nt_error_from_unix(errno);
3230                         }
3231                         block_size = lp_block_size(snum);
3232                         if (bsize < block_size) {
3233                                 uint64_t factor = block_size/bsize;
3234                                 bsize = block_size;
3235                                 dsize /= factor;
3236                                 dfree /= factor;
3237                         }
3238                         if (bsize > block_size) {
3239                                 uint64_t factor = bsize/block_size;
3240                                 bsize = block_size;
3241                                 dsize *= factor;
3242                                 dfree *= factor;
3243                         }
3244                         bytes_per_sector = 512;
3245                         sectors_per_unit = bsize/bytes_per_sector;
3246                         DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_FS_FULL_SIZE_INFO bsize=%u, cSectorUnit=%u, \
3247 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
3248                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
3249                         SBIG_UINT(pdata,0,dsize); /* Total Allocation units. */
3250                         SBIG_UINT(pdata,8,dfree); /* Caller available allocation units. */
3251                         SBIG_UINT(pdata,16,dfree); /* Actual available allocation units. */
3252                         SIVAL(pdata,24,sectors_per_unit); /* Sectors per allocation unit. */
3253                         SIVAL(pdata,28,bytes_per_sector); /* Bytes per sector. */
3254                         break;
3255                 }
3256
3257                 case SMB_QUERY_FS_DEVICE_INFO:
3258                 case SMB_FS_DEVICE_INFORMATION:
3259                 {
3260                         uint32_t characteristics = FILE_DEVICE_IS_MOUNTED;
3261
3262                         if (!CAN_WRITE(conn)) {
3263                                 characteristics |= FILE_READ_ONLY_DEVICE;
3264                         }
3265                         data_len = 8;
3266                         SIVAL(pdata,0,FILE_DEVICE_DISK); /* dev type */
3267                         SIVAL(pdata,4,characteristics);
3268                         break;
3269                 }
3270
3271 #ifdef HAVE_SYS_QUOTAS
3272                 case SMB_FS_QUOTA_INFORMATION:
3273                 /* 
3274                  * what we have to send --metze:
3275                  *
3276                  * Unknown1:            24 NULL bytes
3277                  * Soft Quota Treshold: 8 bytes seems like uint64_t or so
3278                  * Hard Quota Limit:    8 bytes seems like uint64_t or so
3279                  * Quota Flags:         2 byte :
3280                  * Unknown3:            6 NULL bytes
3281                  *
3282                  * 48 bytes total
3283                  * 
3284                  * details for Quota Flags:
3285                  * 
3286                  * 0x0020 Log Limit: log if the user exceeds his Hard Quota
3287                  * 0x0010 Log Warn:  log if the user exceeds his Soft Quota
3288                  * 0x0002 Deny Disk: deny disk access when the user exceeds his Hard Quota
3289                  * 0x0001 Enable Quotas: enable quota for this fs
3290                  *
3291                  */
3292                 {
3293                         /* we need to fake up a fsp here,
3294                          * because its not send in this call
3295                          */
3296                         files_struct fsp;
3297                         SMB_NTQUOTA_STRUCT quotas;
3298
3299                         ZERO_STRUCT(fsp);
3300                         ZERO_STRUCT(quotas);
3301
3302                         fsp.conn = conn;
3303                         fsp.fnum = FNUM_FIELD_INVALID;
3304
3305                         /* access check */
3306                         if (get_current_uid(conn) != 0) {
3307                                 DEBUG(0,("set_user_quota: access_denied "
3308                                          "service [%s] user [%s]\n",
3309                                          lp_servicename(talloc_tos(), SNUM(conn)),
3310                                          conn->session_info->unix_info->unix_name));
3311                                 return NT_STATUS_ACCESS_DENIED;
3312                         }
3313
3314                         if (vfs_get_ntquota(&fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
3315                                 DEBUG(0,("vfs_get_ntquota() failed for service [%s]\n",lp_servicename(talloc_tos(), SNUM(conn))));
3316                                 return map_nt_error_from_unix(errno);
3317                         }
3318
3319                         data_len = 48;
3320
3321                         DEBUG(10,("SMB_FS_QUOTA_INFORMATION: for service [%s]\n",
3322                                   lp_servicename(talloc_tos(), SNUM(conn))));
3323
3324                         /* Unknown1 24 NULL bytes*/
3325                         SBIG_UINT(pdata,0,(uint64_t)0);
3326                         SBIG_UINT(pdata,8,(uint64_t)0);
3327                         SBIG_UINT(pdata,16,(uint64_t)0);
3328
3329                         /* Default Soft Quota 8 bytes */
3330                         SBIG_UINT(pdata,24,quotas.softlim);
3331
3332                         /* Default Hard Quota 8 bytes */
3333                         SBIG_UINT(pdata,32,quotas.hardlim);
3334
3335                         /* Quota flag 2 bytes */
3336                         SSVAL(pdata,40,quotas.qflags);
3337
3338                         /* Unknown3 6 NULL bytes */
3339                         SSVAL(pdata,42,0);
3340                         SIVAL(pdata,44,0);
3341
3342                         break;
3343                 }
3344 #endif /* HAVE_SYS_QUOTAS */
3345                 case SMB_FS_OBJECTID_INFORMATION:
3346                 {
3347                         unsigned char objid[16];
3348                         struct smb_extended_info extended_info;
3349                         memcpy(pdata,create_volume_objectid(conn, objid),16);
3350                         samba_extended_info_version (&extended_info);
3351                         SIVAL(pdata,16,extended_info.samba_magic);
3352                         SIVAL(pdata,20,extended_info.samba_version);
3353                         SIVAL(pdata,24,extended_info.samba_subversion);
3354                         SBIG_UINT(pdata,28,extended_info.samba_gitcommitdate);
3355                         memcpy(pdata+36,extended_info.samba_version_string,28);
3356                         data_len = 64;
3357                         break;
3358                 }
3359
3360                 /*
3361                  * Query the version and capabilities of the CIFS UNIX extensions
3362                  * in use.
3363                  */
3364
3365                 case SMB_QUERY_CIFS_UNIX_INFO:
3366                 {
3367                         bool large_write = lp_min_receive_file_size() &&
3368                                         !srv_is_signing_active(conn->sconn);
3369                         bool large_read = !srv_is_signing_active(conn->sconn);
3370                         int encrypt_caps = 0;
3371
3372                         if (!lp_unix_extensions()) {
3373                                 return NT_STATUS_INVALID_LEVEL;
3374                         }
3375
3376                         switch (conn->encrypt_level) {
3377                         case SMB_SIGNING_OFF:
3378                                 encrypt_caps = 0;
3379                                 break;
3380                         case SMB_SIGNING_IF_REQUIRED:
3381                         case SMB_SIGNING_DEFAULT:
3382                                 encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP;
3383                                 break;
3384                         case SMB_SIGNING_REQUIRED:
3385                                 encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP|
3386                                                 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP;
3387                                 large_write = false;
3388                                 large_read = false;
3389                                 break;
3390                         }
3391
3392                         data_len = 12;
3393                         SSVAL(pdata,0,CIFS_UNIX_MAJOR_VERSION);
3394                         SSVAL(pdata,2,CIFS_UNIX_MINOR_VERSION);
3395
3396                         /* We have POSIX ACLs, pathname, encryption, 
3397                          * large read/write, and locking capability. */
3398
3399                         SBIG_UINT(pdata,4,((uint64_t)(
3400                                         CIFS_UNIX_POSIX_ACLS_CAP|
3401                                         CIFS_UNIX_POSIX_PATHNAMES_CAP|
3402                                         CIFS_UNIX_FCNTL_LOCKS_CAP|
3403                                         CIFS_UNIX_EXTATTR_CAP|
3404                                         CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP|
3405                                         encrypt_caps|
3406                                         (large_read ? CIFS_UNIX_LARGE_READ_CAP : 0) |
3407                                         (large_write ?
3408                                         CIFS_UNIX_LARGE_WRITE_CAP : 0))));
3409                         break;
3410                 }
3411
3412                 case SMB_QUERY_POSIX_FS_INFO:
3413                 {
3414                         int rc;
3415                         vfs_statvfs_struct svfs;
3416
3417                         if (!lp_unix_extensions()) {
3418                                 return NT_STATUS_INVALID_LEVEL;
3419                         }
3420
3421                         rc = SMB_VFS_STATVFS(conn, ".", &svfs);
3422
3423                         if (!rc) {
3424                                 data_len = 56;
3425                                 SIVAL(pdata,0,svfs.OptimalTransferSize);
3426                                 SIVAL(pdata,4,svfs.BlockSize);
3427                                 SBIG_UINT(pdata,8,svfs.TotalBlocks);
3428                                 SBIG_UINT(pdata,16,svfs.BlocksAvail);
3429                                 SBIG_UINT(pdata,24,svfs.UserBlocksAvail);
3430                                 SBIG_UINT(pdata,32,svfs.TotalFileNodes);
3431                                 SBIG_UINT(pdata,40,svfs.FreeFileNodes);
3432                                 SBIG_UINT(pdata,48,svfs.FsIdentifier);
3433                                 DEBUG(5,("smbd_do_qfsinfo : SMB_QUERY_POSIX_FS_INFO succsessful\n"));
3434 #ifdef EOPNOTSUPP
3435                         } else if (rc == EOPNOTSUPP) {
3436                                 return NT_STATUS_INVALID_LEVEL;
3437 #endif /* EOPNOTSUPP */
3438                         } else {
3439                                 DEBUG(0,("vfs_statvfs() failed for service [%s]\n",lp_servicename(talloc_tos(), SNUM(conn))));
3440                                 return NT_STATUS_DOS(ERRSRV, ERRerror);
3441                         }
3442                         break;
3443                 }
3444
3445                 case SMB_QUERY_POSIX_WHOAMI:
3446                 {
3447                         uint32_t flags = 0;
3448                         uint32_t sid_bytes;
3449                         int i;
3450
3451                         if (!lp_unix_extensions()) {
3452                                 return NT_STATUS_INVALID_LEVEL;
3453                         }
3454
3455                         if (max_data_bytes < 40) {
3456                                 return NT_STATUS_BUFFER_TOO_SMALL;
3457                         }
3458
3459                         if (security_session_user_level(conn->session_info, NULL) < SECURITY_USER) {
3460                                 flags |= SMB_WHOAMI_GUEST;
3461                         }
3462
3463                         /* NOTE: 8 bytes for UID/GID, irrespective of native
3464                          * platform size. This matches
3465                          * SMB_QUERY_FILE_UNIX_BASIC and friends.
3466                          */
3467                         data_len = 4 /* flags */
3468                             + 4 /* flag mask */
3469                             + 8 /* uid */
3470                             + 8 /* gid */
3471                             + 4 /* ngroups */
3472                             + 4 /* num_sids */
3473                             + 4 /* SID bytes */
3474                             + 4 /* pad/reserved */
3475                             + (conn->session_info->unix_token->ngroups * 8)
3476                                 /* groups list */
3477                             + (conn->session_info->security_token->num_sids *
3478                                     SID_MAX_SIZE)
3479                                 /* SID list */;
3480
3481                         SIVAL(pdata, 0, flags);
3482                         SIVAL(pdata, 4, SMB_WHOAMI_MASK);
3483                         SBIG_UINT(pdata, 8,
3484                                   (uint64_t)conn->session_info->unix_token->uid);
3485                         SBIG_UINT(pdata, 16,
3486                                   (uint64_t)conn->session_info->unix_token->gid);
3487
3488
3489                         if (data_len >= max_data_bytes) {
3490                                 /* Potential overflow, skip the GIDs and SIDs. */
3491
3492                                 SIVAL(pdata, 24, 0); /* num_groups */
3493                                 SIVAL(pdata, 28, 0); /* num_sids */
3494                                 SIVAL(pdata, 32, 0); /* num_sid_bytes */
3495                                 SIVAL(pdata, 36, 0); /* reserved */
3496
3497                                 data_len = 40;
3498                                 break;
3499                         }
3500
3501                         SIVAL(pdata, 24, conn->session_info->unix_token->ngroups);
3502                         SIVAL(pdata, 28, conn->session_info->security_token->num_sids);
3503
3504                         /* We walk the SID list twice, but this call is fairly
3505                          * infrequent, and I don't expect that it's performance
3506                          * sensitive -- jpeach
3507                          */
3508                         for (i = 0, sid_bytes = 0;
3509                              i < conn->session_info->security_token->num_sids; ++i) {
3510                                 sid_bytes += ndr_size_dom_sid(
3511                                         &conn->session_info->security_token->sids[i],
3512                                         0);
3513                         }
3514
3515                         /* SID list byte count */
3516                         SIVAL(pdata, 32, sid_bytes);
3517
3518                         /* 4 bytes pad/reserved - must be zero */
3519                         SIVAL(pdata, 36, 0);
3520                         data_len = 40;
3521
3522                         /* GID list */
3523                         for (i = 0; i < conn->session_info->unix_token->ngroups; ++i) {
3524                                 SBIG_UINT(pdata, data_len,
3525                                           (uint64_t)conn->session_info->unix_token->groups[i]);
3526                                 data_len += 8;
3527                         }
3528
3529                         /* SID list */
3530                         for (i = 0;
3531                             i < conn->session_info->security_token->num_sids; ++i) {
3532                                 int sid_len = ndr_size_dom_sid(
3533                                         &conn->session_info->security_token->sids[i],
3534                                         0);
3535
3536                                 sid_linearize(pdata + data_len, sid_len,
3537                                     &conn->session_info->security_token->sids[i]);
3538                                 data_len += sid_len;
3539                         }
3540
3541                         break;
3542                 }
3543
3544                 case SMB_MAC_QUERY_FS_INFO:
3545                         /*
3546                          * Thursby MAC extension... ONLY on NTFS filesystems
3547                          * once we do streams then we don't need this
3548                          */
3549                         if (strequal(lp_fstype(talloc_tos(), SNUM(conn)),"NTFS")) {
3550                                 data_len = 88;
3551                                 SIVAL(pdata,84,0x100); /* Don't support mac... */
3552                                 break;
3553                         }
3554                         /* drop through */
3555                 default:
3556                         return NT_STATUS_INVALID_LEVEL;
3557         }
3558
3559         *ret_data_len = data_len;
3560         return NT_STATUS_OK;
3561 }
3562
3563 /****************************************************************************
3564  Reply to a TRANS2_QFSINFO (query filesystem info).
3565 ****************************************************************************/
3566
3567 static void call_trans2qfsinfo(connection_struct *conn,
3568                                struct smb_request *req,
3569                                char **pparams, int total_params,
3570                                char **ppdata, int total_data,
3571                                unsigned int max_data_bytes)
3572 {
3573         char *params = *pparams;
3574         uint16_t info_level;
3575         int data_len = 0;
3576         NTSTATUS status;
3577
3578         if (total_params < 2) {
3579                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3580                 return;
3581         }
3582
3583         info_level = SVAL(params,0);
3584
3585         if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
3586                 if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
3587                         DEBUG(0,("call_trans2qfsinfo: encryption required "
3588                                 "and info level 0x%x sent.\n",
3589                                 (unsigned int)info_level));
3590                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3591                         return;
3592                 }
3593         }
3594
3595         DEBUG(3,("call_trans2qfsinfo: level = %d\n", info_level));
3596
3597         status = smbd_do_qfsinfo(conn, req,
3598                                  info_level,
3599                                  req->flags2,
3600                                  max_data_bytes,
3601                                  ppdata, &data_len);
3602         if (!NT_STATUS_IS_OK(status)) {
3603                 reply_nterror(req, status);
3604                 return;
3605         }
3606
3607         send_trans2_replies(conn, req, params, 0, *ppdata, data_len,
3608                             max_data_bytes);
3609
3610         DEBUG( 4, ( "%s info_level = %d\n",
3611                     smb_fn_name(req->cmd), info_level) );
3612
3613         return;
3614 }
3615
3616 /****************************************************************************
3617  Reply to a TRANS2_SETFSINFO (set filesystem info).
3618 ****************************************************************************/
3619
3620 static void call_trans2setfsinfo(connection_struct *conn,
3621                                  struct smb_request *req,
3622                                  char **pparams, int total_params,
3623                                  char **ppdata, int total_data,
3624                                  unsigned int max_data_bytes)
3625 {
3626         struct smbd_server_connection *sconn = req->sconn;
3627         char *pdata = *ppdata;
3628         char *params = *pparams;
3629         uint16 info_level;
3630
3631         DEBUG(10,("call_trans2setfsinfo: for service [%s]\n",
3632                   lp_servicename(talloc_tos(), SNUM(conn))));
3633
3634         /*  */
3635         if (total_params < 4) {
3636                 DEBUG(0,("call_trans2setfsinfo: requires total_params(%d) >= 4 bytes!\n",
3637                         total_params));
3638                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3639                 return;
3640         }
3641
3642         info_level = SVAL(params,2);
3643
3644         if (IS_IPC(conn)) {
3645                 if (info_level != SMB_REQUEST_TRANSPORT_ENCRYPTION &&
3646                                 info_level != SMB_SET_CIFS_UNIX_INFO) {
3647                         DEBUG(0,("call_trans2setfsinfo: not an allowed "
3648                                 "info level (0x%x) on IPC$.\n",
3649                                 (unsigned int)info_level));
3650                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3651                         return;
3652                 }
3653         }
3654
3655         if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
3656                 if (info_level != SMB_REQUEST_TRANSPORT_ENCRYPTION) {
3657                         DEBUG(0,("call_trans2setfsinfo: encryption required "
3658                                 "and info level 0x%x sent.\n",
3659                                 (unsigned int)info_level));
3660                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3661                         return;
3662                 }
3663         }
3664
3665         switch(info_level) {
3666                 case SMB_SET_CIFS_UNIX_INFO:
3667                         if (!lp_unix_extensions()) {
3668                                 DEBUG(2,("call_trans2setfsinfo: "
3669                                         "SMB_SET_CIFS_UNIX_INFO is invalid with "
3670                                         "unix extensions off\n"));
3671                                 reply_nterror(req,
3672                                               NT_STATUS_INVALID_LEVEL);
3673                                 return;
3674                         }
3675
3676                         /* There should be 12 bytes of capabilities set. */
3677                         if (total_data < 12) {
3678                                 reply_nterror(
3679                                         req,
3680                                         NT_STATUS_INVALID_PARAMETER);
3681                                 return;
3682                         }
3683                         sconn->smb1.unix_info.client_major = SVAL(pdata,0);
3684                         sconn->smb1.unix_info.client_minor = SVAL(pdata,2);
3685                         sconn->smb1.unix_info.client_cap_low = IVAL(pdata,4);
3686                         sconn->smb1.unix_info.client_cap_high = IVAL(pdata,8);
3687                         /* Just print these values for now. */
3688                         DEBUG(10, ("call_trans2setfsinfo: set unix_info info. "
3689                                    "major = %u, minor = %u cap_low = 0x%x, "
3690                                    "cap_high = 0x%xn",
3691                                    (unsigned int)sconn->
3692                                    smb1.unix_info.client_major,
3693                                    (unsigned int)sconn->
3694                                    smb1.unix_info.client_minor,
3695                                    (unsigned int)sconn->
3696                                    smb1.unix_info.client_cap_low,
3697                                    (unsigned int)sconn->
3698                                    smb1.unix_info.client_cap_high));
3699
3700                         /* Here is where we must switch to posix pathname processing... */
3701                         if (sconn->smb1.unix_info.client_cap_low & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3702                                 lp_set_posix_pathnames();
3703                                 mangle_change_to_posix();
3704                         }
3705
3706                         if ((sconn->smb1.unix_info.client_cap_low & CIFS_UNIX_FCNTL_LOCKS_CAP) &&
3707                             !(sconn->smb1.unix_info.client_cap_low & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP)) {
3708                                 /* Client that knows how to do posix locks,
3709                                  * but not posix open/mkdir operations. Set a
3710                                  * default type for read/write checks. */
3711
3712                                 lp_set_posix_default_cifsx_readwrite_locktype(POSIX_LOCK);
3713
3714                         }
3715                         break;
3716
3717                 case SMB_REQUEST_TRANSPORT_ENCRYPTION:
3718                         {
3719                                 NTSTATUS status;
3720                                 size_t param_len = 0;
3721                                 size_t data_len = total_data;
3722
3723                                 if (!lp_unix_extensions()) {
3724                                         reply_nterror(
3725                                                 req,
3726                                                 NT_STATUS_INVALID_LEVEL);
3727                                         return;
3728                                 }
3729
3730                                 if (lp_smb_encrypt(SNUM(conn)) == SMB_SIGNING_OFF) {
3731                                         reply_nterror(
3732                                                 req,
3733                                                 NT_STATUS_NOT_SUPPORTED);
3734                                         return;
3735                                 }
3736
3737                                 if (req->sconn->smb1.echo_handler.trusted_fde) {
3738                                         DEBUG( 2,("call_trans2setfsinfo: "
3739                                                 "request transport encryption disabled"
3740                                                 "with 'fork echo handler = yes'\n"));
3741                                         reply_nterror(
3742                                                 req,
3743                                                 NT_STATUS_NOT_SUPPORTED);
3744                                         return;
3745                                 }
3746
3747                                 DEBUG( 4,("call_trans2setfsinfo: "
3748                                         "request transport encryption.\n"));
3749
3750                                 status = srv_request_encryption_setup(conn,
3751                                                                 (unsigned char **)ppdata,
3752                                                                 &data_len,
3753                                                                 (unsigned char **)pparams,
3754                                                                 &param_len);
3755
3756                                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
3757                                                 !NT_STATUS_IS_OK(status)) {
3758                                         reply_nterror(req, status);
3759                                         return;
3760                                 }
3761
3762                                 send_trans2_replies(conn, req,
3763                                                 *pparams,
3764                                                 param_len,
3765                                                 *ppdata,
3766                                                 data_len,
3767                                                 max_data_bytes);
3768
3769                                 if (NT_STATUS_IS_OK(status)) {
3770                                         /* Server-side transport
3771                                          * encryption is now *on*. */
3772                                         status = srv_encryption_start(conn);
3773                                         if (!NT_STATUS_IS_OK(status)) {
3774                                                 char *reason = talloc_asprintf(talloc_tos(),
3775                                                                                "Failure in setting "
3776                                                                                "up encrypted transport: %s",
3777                                                                                nt_errstr(status));
3778                                                 exit_server_cleanly(reason);
3779                                         }
3780                                 }
3781                                 return;
3782                         }
3783
3784                 case SMB_FS_QUOTA_INFORMATION:
3785                         {
3786                                 files_struct *fsp = NULL;
3787                                 SMB_NTQUOTA_STRUCT quotas;
3788
3789                                 ZERO_STRUCT(quotas);
3790
3791                                 /* access check */
3792                                 if ((get_current_uid(conn) != 0) || !CAN_WRITE(conn)) {
3793                                         DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
3794                                                  lp_servicename(talloc_tos(), SNUM(conn)),
3795                                                  conn->session_info->unix_info->unix_name));
3796                                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3797                                         return;
3798                                 }
3799
3800                                 /* note: normaly there're 48 bytes,
3801                                  * but we didn't use the last 6 bytes for now 
3802                                  * --metze 
3803                                  */
3804                                 fsp = file_fsp(req, SVAL(params,0));
3805
3806                                 if (!check_fsp_ntquota_handle(conn, req,
3807                                                               fsp)) {
3808                                         DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3809                                         reply_nterror(
3810                                                 req, NT_STATUS_INVALID_HANDLE);
3811                                         return;
3812                                 }
3813
3814                                 if (total_data < 42) {
3815                                         DEBUG(0,("call_trans2setfsinfo: SET_FS_QUOTA: requires total_data(%d) >= 42 bytes!\n",
3816                                                 total_data));
3817                                         reply_nterror(
3818                                                 req,
3819                                                 NT_STATUS_INVALID_PARAMETER);
3820                                         return;
3821                                 }
3822
3823                                 /* unknown_1 24 NULL bytes in pdata*/
3824
3825                                 /* the soft quotas 8 bytes (uint64_t)*/
3826                                 quotas.softlim = BVAL(pdata,24);
3827
3828                                 /* the hard quotas 8 bytes (uint64_t)*/
3829                                 quotas.hardlim = BVAL(pdata,32);
3830
3831                                 /* quota_flags 2 bytes **/
3832                                 quotas.qflags = SVAL(pdata,40);
3833
3834                                 /* unknown_2 6 NULL bytes follow*/
3835
3836                                 /* now set the quotas */
3837                                 if (vfs_set_ntquota(fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
3838                                         DEBUG(0,("vfs_set_ntquota() failed for service [%s]\n",lp_servicename(talloc_tos(), SNUM(conn))));
3839                                         reply_nterror(req, map_nt_error_from_unix(errno));
3840                                         return;
3841                                 }
3842
3843                                 break;
3844                         }
3845                 default:
3846                         DEBUG(3,("call_trans2setfsinfo: unknown level (0x%X) not implemented yet.\n",
3847                                 info_level));
3848                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3849                         return;
3850                         break;
3851         }
3852
3853         /* 
3854          * sending this reply works fine, 
3855          * but I'm not sure it's the same 
3856          * like windows do...
3857          * --metze
3858          */
3859         reply_outbuf(req, 10, 0);
3860 }
3861
3862 #if defined(HAVE_POSIX_ACLS)
3863 /****************************************************************************
3864  Utility function to count the number of entries in a POSIX acl.
3865 ****************************************************************************/
3866
3867 static unsigned int count_acl_entries(connection_struct *conn, SMB_ACL_T posix_acl)
3868 {
3869         unsigned int ace_count = 0;
3870         int entry_id = SMB_ACL_FIRST_ENTRY;
3871         SMB_ACL_ENTRY_T entry;
3872
3873         while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
3874                 /* get_next... */
3875                 if (entry_id == SMB_ACL_FIRST_ENTRY) {
3876                         entry_id = SMB_ACL_NEXT_ENTRY;
3877                 }
3878                 ace_count++;
3879         }
3880         return ace_count;
3881 }
3882
3883 /****************************************************************************
3884  Utility function to marshall a POSIX acl into wire format.
3885 ****************************************************************************/
3886
3887 static bool marshall_posix_acl(connection_struct *conn, char *pdata, SMB_STRUCT_STAT *pst, SMB_ACL_T posix_acl)
3888 {
3889         int entry_id = SMB_ACL_FIRST_ENTRY;
3890         SMB_ACL_ENTRY_T entry;
3891
3892         while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
3893                 SMB_ACL_TAG_T tagtype;
3894                 SMB_ACL_PERMSET_T permset;
3895                 unsigned char perms = 0;
3896                 unsigned int own_grp;
3897
3898                 /* get_next... */
3899                 if (entry_id == SMB_ACL_FIRST_ENTRY) {
3900                         entry_id = SMB_ACL_NEXT_ENTRY;
3901                 }
3902
3903                 if (sys_acl_get_tag_type(entry, &tagtype) == -1) {
3904                         DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_TAG_TYPE failed.\n"));
3905                         return False;
3906                 }
3907
3908                 if (sys_acl_get_permset(entry, &permset) == -1) {
3909                         DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_PERMSET failed.\n"));
3910                         return False;
3911                 }
3912
3913                 perms |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? SMB_POSIX_ACL_READ : 0);
3914                 perms |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? SMB_POSIX_ACL_WRITE : 0);
3915                 perms |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? SMB_POSIX_ACL_EXECUTE : 0);
3916
3917                 SCVAL(pdata,1,perms);
3918
3919                 switch (tagtype) {
3920                         case SMB_ACL_USER_OBJ:
3921                                 SCVAL(pdata,0,SMB_POSIX_ACL_USER_OBJ);
3922                                 own_grp = (unsigned int)pst->st_ex_uid;
3923                                 SIVAL(pdata,2,own_grp);
3924                                 SIVAL(pdata,6,0);
3925                                 break;
3926                         case SMB_ACL_USER:
3927                                 {
3928                                         uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
3929                                         if (!puid) {
3930                                                 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_QUALIFIER failed.\n"));
3931                                                 return False;
3932                                         }
3933                                         own_grp = (unsigned int)*puid;
3934                                         SCVAL(pdata,0,SMB_POSIX_ACL_USER);
3935                                         SIVAL(pdata,2,own_grp);
3936                                         SIVAL(pdata,6,0);
3937                                         break;
3938                                 }
3939                         case SMB_ACL_GROUP_OBJ:
3940                                 SCVAL(pdata,0,SMB_POSIX_ACL_GROUP_OBJ);
3941                                 own_grp = (unsigned int)pst->st_ex_gid;
3942                                 SIVAL(pdata,2,own_grp);
3943                                 SIVAL(pdata,6,0);
3944                                 break;
3945                         case SMB_ACL_GROUP:
3946                                 {
3947                                         gid_t *pgid= (gid_t *)sys_acl_get_qualifier(entry);
3948                                         if (!pgid) {
3949                                                 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_QUALIFIER failed.\n"));
3950                                                 return False;
3951                                         }
3952                                         own_grp = (unsigned int)*pgid;
3953                                         SCVAL(pdata,0,SMB_POSIX_ACL_GROUP);
3954                                         SIVAL(pdata,2,own_grp);
3955                                         SIVAL(pdata,6,0);
3956                                         break;
3957                                 }
3958                         case SMB_ACL_MASK:
3959                                 SCVAL(pdata,0,SMB_POSIX_ACL_MASK);
3960                                 SIVAL(pdata,2,0xFFFFFFFF);
3961                                 SIVAL(pdata,6,0xFFFFFFFF);
3962                                 break;
3963                         case SMB_ACL_OTHER:
3964                                 SCVAL(pdata,0,SMB_POSIX_ACL_OTHER);
3965                                 SIVAL(pdata,2,0xFFFFFFFF);
3966                                 SIVAL(pdata,6,0xFFFFFFFF);
3967                                 break;
3968                         default:
3969                                 DEBUG(0,("marshall_posix_acl: unknown tagtype.\n"));
3970                                 return False;
3971                 }
3972                 pdata += SMB_POSIX_ACL_ENTRY_SIZE;
3973         }
3974
3975         return True;
3976 }
3977 #endif
3978
3979 /****************************************************************************
3980  Store the FILE_UNIX_BASIC info.
3981 ****************************************************************************/
3982
3983 static char *store_file_unix_basic(connection_struct *conn,
3984                                 char *pdata,
3985                                 files_struct *fsp,
3986                                 const SMB_STRUCT_STAT *psbuf)
3987 {
3988         uint64_t file_index = get_FileIndex(conn, psbuf);
3989         dev_t devno;
3990
3991         DEBUG(10,("store_file_unix_basic: SMB_QUERY_FILE_UNIX_BASIC\n"));
3992         DEBUG(4,("store_file_unix_basic: st_mode=%o\n",(int)psbuf->st_ex_mode));
3993
3994         SOFF_T(pdata,0,get_file_size_stat(psbuf));             /* File size 64 Bit */
3995         pdata += 8;
3996
3997         SOFF_T(pdata,0,SMB_VFS_GET_ALLOC_SIZE(conn,fsp,psbuf)); /* Number of bytes used on disk - 64 Bit */
3998         pdata += 8;
3999
4000         put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER, pdata, psbuf->st_ex_ctime);       /* Change Time 64 Bit */
4001         put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER ,pdata+8, psbuf->st_ex_atime);     /* Last access time 64 Bit */
4002         put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER, pdata+16, psbuf->st_ex_mtime);    /* Last modification time 64 Bit */
4003         pdata += 24;
4004
4005         SIVAL(pdata,0,psbuf->st_ex_uid);               /* user id for the owner */
4006         SIVAL(pdata,4,0);
4007         pdata += 8;
4008
4009         SIVAL(pdata,0,psbuf->st_ex_gid);               /* group id of owner */
4010         SIVAL(pdata,4,0);
4011         pdata += 8;
4012
4013         SIVAL(pdata,0,unix_filetype(psbuf->st_ex_mode));
4014         pdata += 4;
4015
4016         if (S_ISBLK(psbuf->st_ex_mode) || S_ISCHR(psbuf->st_ex_mode)) {
4017                 devno = psbuf->st_ex_rdev;
4018         } else {
4019                 devno = psbuf->st_ex_dev;
4020         }
4021
4022         SIVAL(pdata,0,unix_dev_major(devno));   /* Major device number if type is device */
4023         SIVAL(pdata,4,0);
4024         pdata += 8;
4025
4026         SIVAL(pdata,0,unix_dev_minor(devno));   /* Minor device number if type is device */
4027         SIVAL(pdata,4,0);
4028         pdata += 8;
4029
4030         SINO_T_VAL(pdata,0,(SMB_INO_T)file_index);   /* inode number */
4031         pdata += 8;
4032
4033         SIVAL(pdata,0, unix_perms_to_wire(psbuf->st_ex_mode));     /* Standard UNIX file permissions */
4034         SIVAL(pdata,4,0);
4035         pdata += 8;
4036
4037         SIVAL(pdata,0,psbuf->st_ex_nlink);             /* number of hard links */
4038         SIVAL(pdata,4,0);
4039         pdata += 8;
4040
4041         return pdata;
4042 }
4043
4044 /* Forward and reverse mappings from the UNIX_INFO2 file flags field and
4045  * the chflags(2) (or equivalent) flags.
4046  *
4047  * XXX: this really should be behind the VFS interface. To do this, we would
4048  * need to alter SMB_STRUCT_STAT so that it included a flags and a mask field.
4049  * Each VFS module could then implement its own mapping as appropriate for the
4050  * platform. We would then pass the SMB flags into SMB_VFS_CHFLAGS.
4051  */
4052 static const struct {unsigned stat_fflag; unsigned smb_fflag;}
4053         info2_flags_map[] =
4054 {
4055 #ifdef UF_NODUMP
4056     { UF_NODUMP, EXT_DO_NOT_BACKUP },
4057 #endif
4058
4059 #ifdef UF_IMMUTABLE
4060     { UF_IMMUTABLE, EXT_IMMUTABLE },
4061 #endif
4062
4063 #ifdef UF_APPEND
4064     { UF_APPEND, EXT_OPEN_APPEND_ONLY },
4065 #endif
4066
4067 #ifdef UF_HIDDEN
4068     { UF_HIDDEN, EXT_HIDDEN },
4069 #endif
4070
4071     /* Do not remove. We need to guarantee that this array has at least one
4072      * entry to build on HP-UX.
4073      */
4074     { 0, 0 }
4075
4076 };
4077
4078 static void map_info2_flags_from_sbuf(const SMB_STRUCT_STAT *psbuf,
4079                                 uint32 *smb_fflags, uint32 *smb_fmask)
4080 {
4081         int i;
4082
4083         for (i = 0; i < ARRAY_SIZE(info2_flags_map); ++i) {
4084             *smb_fmask |= info2_flags_map[i].smb_fflag;
4085             if (psbuf->st_ex_flags & info2_flags_map[i].stat_fflag) {
4086                     *smb_fflags |= info2_flags_map[i].smb_fflag;
4087             }
4088         }
4089 }
4090
4091 static bool map_info2_flags_to_sbuf(const SMB_STRUCT_STAT *psbuf,
4092                                 const uint32 smb_fflags,
4093                                 const uint32 smb_fmask,
4094                                 int *stat_fflags)
4095 {
4096         uint32 max_fmask = 0;
4097         int i;
4098
4099         *stat_fflags = psbuf->st_ex_flags;
4100
4101         /* For each flags requested in smb_fmask, check the state of the
4102          * corresponding flag in smb_fflags and set or clear the matching
4103          * stat flag.
4104          */
4105
4106         for (i = 0; i < ARRAY_SIZE(info2_flags_map); ++i) {
4107             max_fmask |= info2_flags_map[i].smb_fflag;
4108             if (smb_fmask & info2_flags_map[i].smb_fflag) {
4109                     if (smb_fflags & info2_flags_map[i].smb_fflag) {
4110                             *stat_fflags |= info2_flags_map[i].stat_fflag;
4111                     } else {
4112                             *stat_fflags &= ~info2_flags_map[i].stat_fflag;
4113                     }
4114             }
4115         }
4116
4117         /* If smb_fmask is asking to set any bits that are not supported by
4118          * our flag mappings, we should fail.
4119          */
4120         if ((smb_fmask & max_fmask) != smb_fmask) {
4121                 return False;
4122         }
4123
4124         return True;
4125 }
4126
4127
4128 /* Just like SMB_QUERY_FILE_UNIX_BASIC, but with the addition
4129  * of file flags and birth (create) time.
4130  */
4131 static char *store_file_unix_basic_info2(connection_struct *conn,
4132                                 char *pdata,
4133                                 files_struct *fsp,
4134                                 const SMB_STRUCT_STAT *psbuf)
4135 {
4136         uint32 file_flags = 0;
4137         uint32 flags_mask = 0;
4138
4139         pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
4140
4141         /* Create (birth) time 64 bit */
4142         put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER,pdata, psbuf->st_ex_btime);
4143         pdata += 8;
4144
4145         map_info2_flags_from_sbuf(psbuf, &file_flags, &flags_mask);
4146         SIVAL(pdata, 0, file_flags); /* flags */
4147         SIVAL(pdata, 4, flags_mask); /* mask */
4148         pdata += 8;
4149
4150         return pdata;
4151 }
4152
4153 static NTSTATUS marshall_stream_info(unsigned int num_streams,
4154                                      const struct stream_struct *streams,
4155                                      char *data,
4156                                      unsigned int max_data_bytes,
4157                                      unsigned int *data_size)
4158 {
4159         unsigned int i;
4160         unsigned int ofs = 0;
4161
4162         for (i = 0; i < num_streams && ofs <= max_data_bytes; i++) {
4163                 unsigned int next_offset;
4164                 size_t namelen;
4165                 smb_ucs2_t *namebuf;
4166
4167                 if (!push_ucs2_talloc(talloc_tos(), &namebuf,
4168                                       streams[i].name, &namelen) ||
4169                     namelen <= 2)
4170                 {
4171                         return NT_STATUS_INVALID_PARAMETER;
4172                 }
4173
4174                 /*
4175                  * name_buf is now null-terminated, we need to marshall as not
4176                  * terminated
4177                  */
4178
4179                 namelen -= 2;
4180
4181                 SIVAL(data, ofs+4, namelen);
4182                 SOFF_T(data, ofs+8, streams[i].size);
4183                 SOFF_T(data, ofs+16, streams[i].alloc_size);
4184                 memcpy(data+ofs+24, namebuf, namelen);
4185                 TALLOC_FREE(namebuf);
4186
4187                 next_offset = ofs + 24 + namelen;
4188
4189                 if (i == num_streams-1) {
4190                         SIVAL(data, ofs, 0);
4191                 }
4192                 else {
4193                         unsigned int align = ndr_align_size(next_offset, 8);
4194
4195                         memset(data+next_offset, 0, align);
4196                         next_offset += align;
4197
4198                         SIVAL(data, ofs, next_offset - ofs);
4199                         ofs = next_offset;
4200                 }
4201
4202                 ofs = next_offset;
4203         }
4204
4205         *data_size = ofs;
4206
4207         return NT_STATUS_OK;
4208 }
4209
4210 /****************************************************************************
4211  Reply to a TRANSACT2_QFILEINFO on a PIPE !
4212 ****************************************************************************/
4213
4214 static void call_trans2qpipeinfo(connection_struct *conn,
4215                                  struct smb_request *req,
4216                                  unsigned int tran_call,
4217                                  char **pparams, int total_params,
4218                                  char **ppdata, int total_data,
4219                                  unsigned int max_data_bytes)
4220 {
4221         char *params = *pparams;
4222         char *pdata = *ppdata;
4223         unsigned int data_size = 0;
4224         unsigned int param_size = 2;
4225         uint16 info_level;
4226         files_struct *fsp;
4227
4228         if (!params) {
4229                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4230                 return;
4231         }
4232
4233         if (total_params < 4) {
4234                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4235                 return;
4236         }
4237
4238         fsp = file_fsp(req, SVAL(params,0));
4239         if (!fsp_is_np(fsp)) {
4240                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
4241                 return;
4242         }
4243
4244         info_level = SVAL(params,2);
4245
4246         *pparams = (char *)SMB_REALLOC(*pparams,2);
4247         if (*pparams == NULL) {
4248                 reply_nterror(req, NT_STATUS_NO_MEMORY);
4249                 return;
4250         }
4251         params = *pparams;
4252         SSVAL(params,0,0);
4253         data_size = max_data_bytes + DIR_ENTRY_SAFETY_MARGIN;
4254         *ppdata = (char *)SMB_REALLOC(*ppdata, data_size); 
4255         if (*ppdata == NULL ) {
4256                 reply_nterror(req, NT_STATUS_NO_MEMORY);
4257                 return;
4258         }
4259         pdata = *ppdata;
4260
4261         switch (info_level) {
4262                 case SMB_FILE_STANDARD_INFORMATION:
4263                         memset(pdata,0,24);
4264                         SOFF_T(pdata,0,4096LL);
4265                         SIVAL(pdata,16,1);
4266                         SIVAL(pdata,20,1);
4267                         data_size = 24;
4268                         break;
4269
4270                 default:
4271                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
4272                         return;
4273         }
4274
4275         send_trans2_replies(conn, req, params, param_size, *ppdata, data_size,
4276                             max_data_bytes);
4277
4278         return;
4279 }
4280
4281 NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
4282                                TALLOC_CTX *mem_ctx,
4283                                uint16_t info_level,
4284                                files_struct *fsp,
4285                                struct smb_filename *smb_fname,
4286                                bool delete_pending,
4287                                struct timespec write_time_ts,
4288                                struct ea_list *ea_list,
4289                                int lock_data_count,
4290                                char *lock_data,
4291                                uint16_t flags2,
4292                                unsigned int max_data_bytes,
4293                                char **ppdata,
4294                                unsigned int *pdata_size)
4295 {
4296         char *pdata = *ppdata;
4297         char *dstart, *dend;
4298         unsigned int data_size;
4299         struct timespec create_time_ts, mtime_ts, atime_ts, ctime_ts;
4300         time_t create_time, mtime, atime, c_time;
4301         SMB_STRUCT_STAT *psbuf = &smb_fname->st;
4302         char *p;
4303         char *base_name;
4304         char *dos_fname;
4305         int mode;
4306         int nlink;
4307         NTSTATUS status;
4308         uint64_t file_size = 0;
4309         uint64_t pos = 0;
4310         uint64_t allocation_size = 0;
4311         uint64_t file_index = 0;
4312         uint32_t access_mask = 0;
4313
4314         if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
4315                 return NT_STATUS_INVALID_LEVEL;
4316         }
4317
4318         DEBUG(5,("smbd_do_qfilepathinfo: %s (%s) level=%d max_data=%u\n",
4319                  smb_fname_str_dbg(smb_fname),
4320                  fsp_fnum_dbg(fsp),
4321                  info_level, max_data_bytes));
4322
4323         mode = dos_mode(conn, smb_fname);
4324         nlink = psbuf->st_ex_nlink;
4325
4326         if (nlink && (mode&FILE_ATTRIBUTE_DIRECTORY)) {
4327                 nlink = 1;
4328         }
4329
4330         if ((nlink > 0) && delete_pending) {
4331                 nlink -= 1;
4332         }
4333
4334         data_size = max_data_bytes + DIR_ENTRY_SAFETY_MARGIN;
4335         *ppdata = (char *)SMB_REALLOC(*ppdata, data_size); 
4336         if (*ppdata == NULL) {
4337                 return NT_STATUS_NO_MEMORY;
4338         }
4339         pdata = *ppdata;
4340         dstart = pdata;
4341         dend = dstart + data_size - 1;
4342
4343         if (!null_timespec(write_time_ts) && !INFO_LEVEL_IS_UNIX(info_level)) {
4344                 update_stat_ex_mtime(psbuf, write_time_ts);
4345         }
4346
4347         create_time_ts = get_create_timespec(conn, fsp, smb_fname);
4348         mtime_ts = psbuf->st_ex_mtime;
4349         atime_ts = psbuf->st_ex_atime;
4350         ctime_ts = get_change_timespec(conn, fsp, smb_fname);
4351
4352         if (lp_dos_filetime_resolution(SNUM(conn))) {
4353                 dos_filetime_timespec(&create_time_ts);
4354                 dos_filetime_timespec(&mtime_ts);
4355                 dos_filetime_timespec(&atime_ts);
4356                 dos_filetime_timespec(&ctime_ts);
4357         }
4358
4359         create_time = convert_timespec_to_time_t(create_time_ts);
4360         mtime = convert_timespec_to_time_t(mtime_ts);
4361         atime = convert_timespec_to_time_t(atime_ts);
4362         c_time = convert_timespec_to_time_t(ctime_ts);
4363
4364         p = strrchr_m(smb_fname->base_name,'/');
4365         if (!p)
4366                 base_name = smb_fname->base_name;
4367         else
4368                 base_name = p+1;
4369
4370         /* NT expects the name to be in an exact form of the *full*
4371            filename. See the trans2 torture test */
4372         if (ISDOT(base_name)) {
4373                 dos_fname = talloc_strdup(mem_ctx, "\\");
4374                 if (!dos_fname) {
4375                         return NT_STATUS_NO_MEMORY;
4376                 }
4377         } else {
4378                 dos_fname = talloc_asprintf(mem_ctx,
4379                                 "\\%s",
4380                                 smb_fname->base_name);
4381                 if (!dos_fname) {
4382                         return NT_STATUS_NO_MEMORY;
4383                 }
4384                 if (is_ntfs_stream_smb_fname(smb_fname)) {
4385                         dos_fname = talloc_asprintf(dos_fname, "%s",
4386                                                     smb_fname->stream_name);
4387                         if (!dos_fname) {
4388                                 return NT_STATUS_NO_MEMORY;
4389                         }
4390                 }
4391
4392                 string_replace(dos_fname, '/', '\\');
4393         }
4394
4395         allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp, psbuf);
4396
4397         if (!fsp) {
4398                 /* Do we have this path open ? */
4399                 files_struct *fsp1;
4400                 struct file_id fileid = vfs_file_id_from_sbuf(conn, psbuf);
4401                 fsp1 = file_find_di_first(conn->sconn, fileid);
4402                 if (fsp1 && fsp1->initial_allocation_size) {
4403                         allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp1, psbuf);
4404                 }
4405         }
4406
4407         if (!(mode & FILE_ATTRIBUTE_DIRECTORY)) {
4408                 file_size = get_file_size_stat(psbuf);
4409         }
4410
4411         if (fsp) {
4412                 pos = fsp->fh->position_information;
4413         }
4414
4415         if (fsp) {
4416                 access_mask = fsp->access_mask;
4417         } else {
4418                 /* GENERIC_EXECUTE mapping from Windows */
4419                 access_mask = 0x12019F;
4420         }
4421
4422         /* This should be an index number - looks like
4423            dev/ino to me :-)
4424
4425            I think this causes us to fail the IFSKIT
4426            BasicFileInformationTest. -tpot */
4427         file_index = get_FileIndex(conn, psbuf);
4428
4429         switch (info_level) {
4430                 case SMB_INFO_STANDARD:
4431                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_STANDARD\n"));
4432                         data_size = 22;
4433                         srv_put_dos_date2(pdata,l1_fdateCreation,create_time);
4434                         srv_put_dos_date2(pdata,l1_fdateLastAccess,atime);
4435                         srv_put_dos_date2(pdata,l1_fdateLastWrite,mtime); /* write time */
4436                         SIVAL(pdata,l1_cbFile,(uint32)file_size);
4437                         SIVAL(pdata,l1_cbFileAlloc,(uint32)allocation_size);
4438                         SSVAL(pdata,l1_attrFile,mode);
4439                         break;
4440
4441                 case SMB_INFO_QUERY_EA_SIZE:
4442                 {
4443                         unsigned int ea_size =
4444                             estimate_ea_size(conn, fsp,
4445                                              smb_fname);
4446                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_EA_SIZE\n"));
4447                         data_size = 26;
4448                         srv_put_dos_date2(pdata,0,create_time);
4449                         srv_put_dos_date2(pdata,4,atime);
4450                         srv_put_dos_date2(pdata,8,mtime); /* write time */
4451                         SIVAL(pdata,12,(uint32)file_size);
4452                         SIVAL(pdata,16,(uint32)allocation_size);
4453                         SSVAL(pdata,20,mode);
4454                         SIVAL(pdata,22,ea_size);
4455                         break;
4456                 }
4457
4458                 case SMB_INFO_IS_NAME_VALID:
4459                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_IS_NAME_VALID\n"));
4460                         if (fsp) {
4461                                 /* os/2 needs this ? really ?*/
4462                                 return NT_STATUS_DOS(ERRDOS, ERRbadfunc);
4463                         }
4464                         /* This is only reached for qpathinfo */
4465                         data_size = 0;
4466                         break;
4467
4468                 case SMB_INFO_QUERY_EAS_FROM_LIST:
4469                 {
4470                         size_t total_ea_len = 0;
4471                         struct ea_list *ea_file_list = NULL;
4472                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_EAS_FROM_LIST\n"));
4473
4474                         status =
4475                             get_ea_list_from_file(mem_ctx, conn, fsp,
4476                                                   smb_fname,
4477                                                   &total_ea_len, &ea_file_list);
4478                         if (!NT_STATUS_IS_OK(status)) {
4479                                 return status;
4480                         }
4481
4482                         ea_list = ea_list_union(ea_list, ea_file_list, &total_ea_len);
4483
4484                         if (!ea_list || (total_ea_len > data_size)) {
4485                                 data_size = 4;
4486                                 SIVAL(pdata,0,4);   /* EA List Length must be set to 4 if no EA's. */
4487                                 break;
4488                         }
4489
4490                         data_size = fill_ea_buffer(mem_ctx, pdata, data_size, conn, ea_list);
4491                         break;
4492                 }
4493
4494                 case SMB_INFO_QUERY_ALL_EAS:
4495                 {
4496                         /* We have data_size bytes to put EA's into. */
4497                         size_t total_ea_len = 0;
4498                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_ALL_EAS\n"));
4499
4500                         status = get_ea_list_from_file(mem_ctx, conn, fsp,
4501                                                         smb_fname,
4502                                                         &total_ea_len, &ea_list);
4503                         if (!NT_STATUS_IS_OK(status)) {
4504                                 return status;
4505                         }
4506
4507                         if (!ea_list || (total_ea_len > data_size)) {
4508                                 data_size = 4;
4509                                 SIVAL(pdata,0,4);   /* EA List Length must be set to 4 if no EA's. */
4510                                 break;
4511                         }
4512
4513                         data_size = fill_ea_buffer(mem_ctx, pdata, data_size, conn, ea_list);
4514                         break;
4515                 }
4516
4517                 case 0xFF0F:/*SMB2_INFO_QUERY_ALL_EAS*/
4518                 {
4519                         /* This is FileFullEaInformation - 0xF which maps to
4520                          * 1015 (decimal) in smbd_do_setfilepathinfo. */
4521
4522                         /* We have data_size bytes to put EA's into. */
4523                         size_t total_ea_len = 0;
4524                         struct ea_list *ea_file_list = NULL;
4525
4526                         DEBUG(10,("smbd_do_qfilepathinfo: SMB2_INFO_QUERY_ALL_EAS\n"));
4527
4528                         /*TODO: add filtering and index handling */
4529
4530                         status  =
4531                                 get_ea_list_from_file(mem_ctx, conn, fsp,
4532                                                   smb_fname,
4533                                                   &total_ea_len, &ea_file_list);
4534                         if (!NT_STATUS_IS_OK(status)) {
4535                                 return status;
4536                         }
4537                         if (!ea_file_list) {
4538                                 return NT_STATUS_NO_EAS_ON_FILE;
4539                         }
4540
4541                         status = fill_ea_chained_buffer(mem_ctx,
4542                                                         pdata,
4543                                                         data_size,
4544                                                         &data_size,
4545                                                         conn, ea_file_list);
4546                         if (!NT_STATUS_IS_OK(status)) {
4547                                 return status;
4548                         }
4549                         break;
4550                 }
4551
4552                 case SMB_FILE_BASIC_INFORMATION:
4553                 case SMB_QUERY_FILE_BASIC_INFO:
4554
4555                         if (info_level == SMB_QUERY_FILE_BASIC_INFO) {
4556                                 DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_BASIC_INFO\n"));
4557                                 data_size = 36; /* w95 returns 40 bytes not 36 - why ?. */
4558                         } else {
4559                                 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_BASIC_INFORMATION\n"));
4560                                 data_size = 40;
4561                                 SIVAL(pdata,36,0);
4562                         }
4563                         put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
4564                         put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
4565                         put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
4566                         put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
4567                         SIVAL(pdata,32,mode);
4568
4569                         DEBUG(5,("SMB_QFBI - "));
4570                         DEBUG(5,("create: %s ", ctime(&create_time)));
4571                         DEBUG(5,("access: %s ", ctime(&atime)));
4572                         DEBUG(5,("write: %s ", ctime(&mtime)));
4573                         DEBUG(5,("change: %s ", ctime(&c_time)));
4574                         DEBUG(5,("mode: %x\n", mode));
4575                         break;
4576
4577                 case SMB_FILE_STANDARD_INFORMATION:
4578                 case SMB_QUERY_FILE_STANDARD_INFO:
4579
4580                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_STANDARD_INFORMATION\n"));
4581                         data_size = 24;
4582                         SOFF_T(pdata,0,allocation_size);
4583                         SOFF_T(pdata,8,file_size);
4584                         SIVAL(pdata,16,nlink);
4585                         SCVAL(pdata,20,delete_pending?1:0);
4586                         SCVAL(pdata,21,(mode&FILE_ATTRIBUTE_DIRECTORY)?1:0);
4587                         SSVAL(pdata,22,0); /* Padding. */
4588                         break;
4589
4590                 case SMB_FILE_EA_INFORMATION:
4591                 case SMB_QUERY_FILE_EA_INFO:
4592                 {
4593                         unsigned int ea_size =
4594                             estimate_ea_size(conn, fsp, smb_fname);
4595                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_EA_INFORMATION\n"));
4596                         data_size = 4;
4597                         SIVAL(pdata,0,ea_size);
4598                         break;
4599                 }
4600
4601                 /* Get the 8.3 name - used if NT SMB was negotiated. */
4602                 case SMB_QUERY_FILE_ALT_NAME_INFO:
4603                 case SMB_FILE_ALTERNATE_NAME_INFORMATION:
4604                 {
4605                         int len;
4606                         char mangled_name[13];
4607                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALTERNATE_NAME_INFORMATION\n"));
4608                         if (!name_to_8_3(base_name,mangled_name,
4609                                                 True,conn->params)) {
4610                                 return NT_STATUS_NO_MEMORY;
4611                         }
4612                         len = srvstr_push(dstart, flags2,
4613                                           pdata+4, mangled_name,
4614                                           PTR_DIFF(dend, pdata+4),
4615                                           STR_UNICODE);
4616                         data_size = 4 + len;
4617                         SIVAL(pdata,0,len);
4618                         break;
4619                 }
4620
4621                 case SMB_QUERY_FILE_NAME_INFO:
4622                 {
4623                         int len;
4624                         /*
4625                           this must be *exactly* right for ACLs on mapped drives to work
4626                          */
4627                         len = srvstr_push(dstart, flags2,
4628                                           pdata+4, dos_fname,
4629                                           PTR_DIFF(dend, pdata+4),
4630                                           STR_UNICODE);
4631                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_NAME_INFO\n"));
4632                         data_size = 4 + len;
4633                         SIVAL(pdata,0,len);
4634                         break;
4635                 }
4636
4637                 case SMB_FILE_ALLOCATION_INFORMATION:
4638                 case SMB_QUERY_FILE_ALLOCATION_INFO:
4639                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALLOCATION_INFORMATION\n"));
4640                         data_size = 8;
4641                         SOFF_T(pdata,0,allocation_size);
4642                         break;
4643
4644                 case SMB_FILE_END_OF_FILE_INFORMATION:
4645                 case SMB_QUERY_FILE_END_OF_FILEINFO:
4646                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_END_OF_FILE_INFORMATION\n"));
4647                         data_size = 8;
4648                         SOFF_T(pdata,0,file_size);
4649                         break;
4650
4651                 case SMB_QUERY_FILE_ALL_INFO:
4652                 case SMB_FILE_ALL_INFORMATION:
4653                 {
4654                         int len;
4655                         unsigned int ea_size =
4656                             estimate_ea_size(conn, fsp, smb_fname);
4657                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALL_INFORMATION\n"));
4658                         put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
4659                         put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
4660                         put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
4661                         put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
4662                         SIVAL(pdata,32,mode);
4663                         SIVAL(pdata,36,0); /* padding. */
4664                         pdata += 40;
4665                         SOFF_T(pdata,0,allocation_size);
4666                         SOFF_T(pdata,8,file_size);
4667                         SIVAL(pdata,16,nlink);
4668                         SCVAL(pdata,20,delete_pending);
4669                         SCVAL(pdata,21,(mode&FILE_ATTRIBUTE_DIRECTORY)?1:0);
4670                         SSVAL(pdata,22,0);
4671                         pdata += 24;
4672                         SIVAL(pdata,0,ea_size);
4673                         pdata += 4; /* EA info */
4674                         len = srvstr_push(dstart, flags2,
4675                                           pdata+4, dos_fname,
4676                                           PTR_DIFF(dend, pdata+4),
4677                                           STR_UNICODE);
4678                         SIVAL(pdata,0,len);
4679                         pdata += 4 + len;
4680                         data_size = PTR_DIFF(pdata,(*ppdata));
4681                         break;
4682                 }
4683
4684                 case 0xFF12:/*SMB2_FILE_ALL_INFORMATION*/
4685                 {
4686                         int len;
4687                         unsigned int ea_size =
4688                             estimate_ea_size(conn, fsp, smb_fname);
4689                         DEBUG(10,("smbd_do_qfilepathinfo: SMB2_FILE_ALL_INFORMATION\n"));
4690                         put_long_date_timespec(conn->ts_res,pdata+0x00,create_time_ts);
4691                         put_long_date_timespec(conn->ts_res,pdata+0x08,atime_ts);
4692                         put_long_date_timespec(conn->ts_res,pdata+0x10,mtime_ts); /* write time */
4693                         put_long_date_timespec(conn->ts_res,pdata+0x18,ctime_ts); /* change time */
4694                         SIVAL(pdata,    0x20, mode);
4695                         SIVAL(pdata,    0x24, 0); /* padding. */
4696                         SBVAL(pdata,    0x28, allocation_size);
4697                         SBVAL(pdata,    0x30, file_size);
4698                         SIVAL(pdata,    0x38, nlink);
4699                         SCVAL(pdata,    0x3C, delete_pending);
4700                         SCVAL(pdata,    0x3D, (mode&FILE_ATTRIBUTE_DIRECTORY)?1:0);
4701                         SSVAL(pdata,    0x3E, 0); /* padding */
4702                         SBVAL(pdata,    0x40, file_index);
4703                         SIVAL(pdata,    0x48, ea_size);
4704                         SIVAL(pdata,    0x4C, access_mask);
4705                         SBVAL(pdata,    0x50, pos);
4706                         SIVAL(pdata,    0x58, mode); /*TODO: mode != mode fix this!!! */
4707                         SIVAL(pdata,    0x5C, 0); /* No alignment needed. */
4708
4709                         pdata += 0x60;
4710
4711                         len = srvstr_push(dstart, flags2,
4712                                           pdata+4, dos_fname,
4713                                           PTR_DIFF(dend, pdata+4),
4714                                           STR_UNICODE);
4715                         SIVAL(pdata,0,len);
4716                         pdata += 4 + len;
4717                         data_size = PTR_DIFF(pdata,(*ppdata));
4718                         break;
4719                 }
4720                 case SMB_FILE_INTERNAL_INFORMATION:
4721
4722                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_INTERNAL_INFORMATION\n"));
4723                         SBVAL(pdata, 0, file_index);
4724                         data_size = 8;
4725                         break;
4726
4727                 case SMB_FILE_ACCESS_INFORMATION:
4728                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ACCESS_INFORMATION\n"));
4729                         SIVAL(pdata, 0, access_mask);
4730                         data_size = 4;
4731                         break;
4732
4733                 case SMB_FILE_NAME_INFORMATION:
4734                         /* Pathname with leading '\'. */
4735                         {
4736                                 size_t byte_len;
4737                                 byte_len = dos_PutUniCode(pdata+4,dos_fname,(size_t)max_data_bytes,False);
4738                                 DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_NAME_INFORMATION\n"));
4739                                 SIVAL(pdata,0,byte_len);
4740                                 data_size = 4 + byte_len;
4741                                 break;
4742                         }
4743
4744                 case SMB_FILE_DISPOSITION_INFORMATION:
4745                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_DISPOSITION_INFORMATION\n"));
4746                         data_size = 1;
4747                         SCVAL(pdata,0,delete_pending);
4748                         break;
4749
4750                 case SMB_FILE_POSITION_INFORMATION:
4751                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_POSITION_INFORMATION\n"));
4752                         data_size = 8;
4753                         SOFF_T(pdata,0,pos);
4754                         break;
4755
4756                 case SMB_FILE_MODE_INFORMATION:
4757                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_MODE_INFORMATION\n"));
4758                         SIVAL(pdata,0,mode);
4759                         data_size = 4;
4760                         break;
4761
4762                 case SMB_FILE_ALIGNMENT_INFORMATION:
4763                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALIGNMENT_INFORMATION\n"));
4764                         SIVAL(pdata,0,0); /* No alignment needed. */
4765                         data_size = 4;
4766                         break;
4767
4768                 /*
4769                  * NT4 server just returns "invalid query" to this - if we try
4770                  * to answer it then NTws gets a BSOD! (tridge).  W2K seems to
4771                  * want this. JRA.
4772                  */
4773                 /* The first statement above is false - verified using Thursby
4774                  * client against NT4 -- gcolley.
4775                  */
4776                 case SMB_QUERY_FILE_STREAM_INFO:
4777                 case SMB_FILE_STREAM_INFORMATION: {
4778                         unsigned int num_streams = 0;
4779                         struct stream_struct *streams = NULL;
4780
4781                         DEBUG(10,("smbd_do_qfilepathinfo: "
4782                                   "SMB_FILE_STREAM_INFORMATION\n"));
4783
4784                         if (is_ntfs_stream_smb_fname(smb_fname)) {
4785                                 return NT_STATUS_INVALID_PARAMETER;
4786                         }
4787
4788                         status = vfs_streaminfo(conn, fsp, smb_fname->base_name,
4789                                                 talloc_tos(), &num_streams, &streams);
4790
4791                         if (!NT_STATUS_IS_OK(status)) {
4792                                 DEBUG(10, ("could not get stream info: %s\n",
4793                                            nt_errstr(status)));
4794                                 return status;
4795                         }
4796
4797                         status = marshall_stream_info(num_streams, streams,
4798                                                       pdata, max_data_bytes,
4799                                                       &data_size);
4800
4801                         if (!NT_STATUS_IS_OK(status)) {
4802                                 DEBUG(10, ("marshall_stream_info failed: %s\n",
4803                                            nt_errstr(status)));
4804                                 return status;
4805                         }
4806
4807                         TALLOC_FREE(streams);
4808
4809                         break;
4810                 }
4811                 case SMB_QUERY_COMPRESSION_INFO:
4812                 case SMB_FILE_COMPRESSION_INFORMATION:
4813                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_COMPRESSION_INFORMATION\n"));
4814                         SOFF_T(pdata,0,file_size);
4815                         SIVAL(pdata,8,0); /* ??? */
4816                         SIVAL(pdata,12,0); /* ??? */
4817                         data_size = 16;
4818                         break;
4819
4820                 case SMB_FILE_NETWORK_OPEN_INFORMATION:
4821                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_NETWORK_OPEN_INFORMATION\n"));
4822                         put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
4823                         put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
4824                         put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
4825                         put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
4826                         SOFF_T(pdata,32,allocation_size);
4827                         SOFF_T(pdata,40,file_size);
4828                         SIVAL(pdata,48,mode);
4829                         SIVAL(pdata,52,0); /* ??? */
4830                         data_size = 56;
4831                         break;
4832
4833                 case SMB_FILE_ATTRIBUTE_TAG_INFORMATION:
4834                         DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ATTRIBUTE_TAG_INFORMATION\n"));
4835                         SIVAL(pdata,0,mode);
4836                         SIVAL(pdata,4,0);
4837                         data_size = 8;
4838                         break;
4839
4840                 /*
4841                  * CIFS UNIX Extensions.
4842                  */
4843
4844                 case SMB_QUERY_FILE_UNIX_BASIC:
4845
4846                         pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
4847                         data_size = PTR_DIFF(pdata,(*ppdata));
4848
4849                         DEBUG(4,("smbd_do_qfilepathinfo: "
4850                                  "SMB_QUERY_FILE_UNIX_BASIC\n"));
4851                         dump_data(4, (uint8_t *)(*ppdata), data_size);
4852
4853                         break;
4854
4855                 case SMB_QUERY_FILE_UNIX_INFO2:
4856
4857                         pdata = store_file_unix_basic_info2(conn, pdata, fsp, psbuf);
4858                         data_size = PTR_DIFF(pdata,(*ppdata));
4859
4860                         {
4861                                 int i;
4862                                 DEBUG(4,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_INFO2 "));
4863
4864                                 for (i=0; i<100; i++)
4865                                         DEBUG(4,("%d=%x, ",i, (*ppdata)[i]));
4866                                 DEBUG(4,("\n"));
4867                         }
4868
4869                         break;
4870
4871                 case SMB_QUERY_FILE_UNIX_LINK:
4872                         {
4873                                 int len;
4874                                 char *buffer = talloc_array(mem_ctx, char, PATH_MAX+1);
4875
4876                                 if (!buffer) {
4877                                         return NT_STATUS_NO_MEMORY;
4878                                 }
4879
4880                                 DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_LINK\n"));
4881 #ifdef S_ISLNK
4882                                 if(!S_ISLNK(psbuf->st_ex_mode)) {
4883                                         return NT_STATUS_DOS(ERRSRV, ERRbadlink);
4884                                 }
4885 #else
4886                                 return NT_STATUS_DOS(ERRDOS, ERRbadlink);
4887 #endif
4888                                 len = SMB_VFS_READLINK(conn,
4889                                                        smb_fname->base_name,
4890                                                        buffer, PATH_MAX);
4891                                 if (len == -1) {
4892                                         return map_nt_error_from_unix(errno);
4893                                 }
4894                                 buffer[len] = 0;
4895                                 len = srvstr_push(dstart, flags2,
4896                                                   pdata, buffer,
4897                                                   PTR_DIFF(dend, pdata),
4898                                                   STR_TERMINATE);
4899                                 pdata += len;
4900                                 data_size = PTR_DIFF(pdata,(*ppdata));
4901
4902                                 break;
4903                         }
4904
4905 #if defined(HAVE_POSIX_ACLS)
4906                 case SMB_QUERY_POSIX_ACL:
4907                         {
4908                                 SMB_ACL_T file_acl = NULL;
4909                                 SMB_ACL_T def_acl = NULL;
4910                                 uint16 num_file_acls = 0;
4911                                 uint16 num_def_acls = 0;
4912
4913                                 if (fsp && fsp->fh->fd != -1) {
4914                                         file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
4915                                 } else {
4916                                         file_acl =
4917                                             SMB_VFS_SYS_ACL_GET_FILE(conn,
4918                                                 smb_fname->base_name,
4919                                                 SMB_ACL_TYPE_ACCESS);
4920                                 }
4921
4922                                 if (file_acl == NULL && no_acl_syscall_error(errno)) {
4923                                         DEBUG(5,("smbd_do_qfilepathinfo: ACLs "
4924                                                  "not implemented on "
4925                                                  "filesystem containing %s\n",
4926                                                  smb_fname->base_name));
4927                                         return NT_STATUS_NOT_IMPLEMENTED;
4928                                 }
4929
4930                                 if (S_ISDIR(psbuf->st_ex_mode)) {
4931                                         if (fsp && fsp->is_directory) {
4932                                                 def_acl =
4933                                                     SMB_VFS_SYS_ACL_GET_FILE(
4934                                                             conn,
4935                                                             fsp->fsp_name->base_name,
4936                                                             SMB_ACL_TYPE_DEFAULT);
4937                                         } else {
4938                                                 def_acl =
4939                                                     SMB_VFS_SYS_ACL_GET_FILE(
4940                                                             conn,
4941                                                             smb_fname->base_name,
4942                                                             SMB_ACL_TYPE_DEFAULT);
4943                                         }
4944                                         def_acl = free_empty_sys_acl(conn, def_acl);
4945                                 }
4946
4947                                 num_file_acls = count_acl_entries(conn, file_acl);
4948                                 num_def_acls = count_acl_entries(conn, def_acl);
4949
4950                                 if ( data_size < (num_file_acls + num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE + SMB_POSIX_ACL_HEADER_SIZE) {
4951                                         DEBUG(5,("smbd_do_qfilepathinfo: data_size too small (%u) need %u\n",
4952                                                 data_size,
4953                                                 (unsigned int)((num_file_acls + num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE +
4954                                                         SMB_POSIX_ACL_HEADER_SIZE) ));
4955                                         if (file_acl) {
4956                                                 TALLOC_FREE(file_acl);
4957                                         }
4958                                         if (def_acl) {
4959                                                 TALLOC_FREE(def_acl);
4960                                         }
4961                                         return NT_STATUS_BUFFER_TOO_SMALL;
4962                                 }
4963
4964                                 SSVAL(pdata,0,SMB_POSIX_ACL_VERSION);
4965                                 SSVAL(pdata,2,num_file_acls);
4966                                 SSVAL(pdata,4,num_def_acls);
4967                                 if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE, psbuf, file_acl)) {
4968                                         if (file_acl) {
4969                                                 TALLOC_FREE(file_acl);
4970                                         }
4971                                         if (def_acl) {
4972                                                 TALLOC_FREE(def_acl);
4973                                         }
4974                                         return NT_STATUS_INTERNAL_ERROR;
4975                                 }
4976                                 if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE + (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE), psbuf, def_acl)) {
4977                                         if (file_acl) {
4978                                                 TALLOC_FREE(file_acl);
4979                                         }
4980                                         if (def_acl) {
4981                                                 TALLOC_FREE(def_acl);
4982                                         }
4983                                         return NT_STATUS_INTERNAL_ERROR;
4984                                 }
4985
4986                                 if (file_acl) {
4987                                         TALLOC_FREE(file_acl);
4988                                 }
4989                                 if (def_acl) {
4990                                         TALLOC_FREE(def_acl);
4991                                 }
4992                                 data_size = (num_file_acls + num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE + SMB_POSIX_ACL_HEADER_SIZE;
4993                                 break;
4994                         }
4995 #endif
4996
4997
4998                 case SMB_QUERY_POSIX_LOCK:
4999                 {
5000                         uint64_t count;
5001                         uint64_t offset;
5002                         uint64_t smblctx;
5003                         enum brl_type lock_type;
5004
5005                         /* We need an open file with a real fd for this. */
5006                         if (!fsp || fsp->fh->fd == -1) {
5007                                 return NT_STATUS_INVALID_LEVEL;
5008                         }
5009
5010                         if (lock_data_count != POSIX_LOCK_DATA_SIZE) {
5011                                 return NT_STATUS_INVALID_PARAMETER;
5012                         }
5013
5014                         switch (SVAL(pdata, POSIX_LOCK_TYPE_OFFSET)) {
5015                                 case POSIX_LOCK_TYPE_READ:
5016                                         lock_type = READ_LOCK;
5017                                         break;
5018                                 case POSIX_LOCK_TYPE_WRITE:
5019                                         lock_type = WRITE_LOCK;
5020                                         break;
5021                                 case POSIX_LOCK_TYPE_UNLOCK:
5022                                 default:
5023                                         /* There's no point in asking for an unlock... */
5024                                         return NT_STATUS_INVALID_PARAMETER;
5025                         }
5026
5027                         smblctx = (uint64_t)IVAL(pdata, POSIX_LOCK_PID_OFFSET);
5028 #if defined(HAVE_LONGLONG)
5029                         offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
5030                                         ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
5031                         count = (((uint64_t) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
5032                                         ((uint64_t) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
5033 #else /* HAVE_LONGLONG */
5034                         offset = (uint64_t)IVAL(pdata,POSIX_LOCK_START_OFFSET);
5035                         count = (uint64_t)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
5036 #endif /* HAVE_LONGLONG */
5037
5038                         status = query_lock(fsp,
5039                                         &smblctx,
5040                                         &count,
5041                                         &offset,
5042                                         &lock_type,
5043                                         POSIX_LOCK);
5044
5045                         if (ERROR_WAS_LOCK_DENIED(status)) {
5046                                 /* Here we need to report who has it locked... */
5047                                 data_size = POSIX_LOCK_DATA_SIZE;
5048
5049                                 SSVAL(pdata, POSIX_LOCK_TYPE_OFFSET, lock_type);
5050                                 SSVAL(pdata, POSIX_LOCK_FLAGS_OFFSET, 0);
5051                                 SIVAL(pdata, POSIX_LOCK_PID_OFFSET, (uint32_t)smblctx);
5052 #if defined(HAVE_LONGLONG)
5053                                 SIVAL(pdata, POSIX_LOCK_START_OFFSET, (uint32)(offset & 0xFFFFFFFF));
5054                                 SIVAL(pdata, POSIX_LOCK_START_OFFSET + 4, (uint32)((offset >> 32) & 0xFFFFFFFF));
5055                                 SIVAL(pdata, POSIX_LOCK_LEN_OFFSET, (uint32)(count & 0xFFFFFFFF));
5056                                 SIVAL(pdata, POSIX_LOCK_LEN_OFFSET + 4, (uint32)((count >> 32) & 0xFFFFFFFF));
5057 #else /* HAVE_LONGLONG */
5058                                 SIVAL(pdata, POSIX_LOCK_START_OFFSET, offset);
5059                                 SIVAL(pdata, POSIX_LOCK_LEN_OFFSET, count);
5060 #endif /* HAVE_LONGLONG */
5061
5062                         } else if (NT_STATUS_IS_OK(status)) {
5063                                 /* For success we just return a copy of what we sent
5064                                    with the lock type set to POSIX_LOCK_TYPE_UNLOCK. */
5065                                 data_size = POSIX_LOCK_DATA_SIZE;
5066                                 memcpy(pdata, lock_data, POSIX_LOCK_DATA_SIZE);
5067                                 SSVAL(pdata, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
5068                         } else {
5069                                 return status;
5070                         }
5071                         break;
5072                 }
5073
5074                 default:
5075                         return NT_STATUS_INVALID_LEVEL;
5076         }
5077
5078         *pdata_size = data_size;
5079         return NT_STATUS_OK;
5080 }
5081
5082 /****************************************************************************
5083  Reply to a TRANS2_QFILEPATHINFO or TRANSACT2_QFILEINFO (query file info by
5084  file name or file id).
5085 ****************************************************************************/
5086
5087 static void call_trans2qfilepathinfo(connection_struct *conn,
5088                                      struct smb_request *req,
5089                                      unsigned int tran_call,
5090                                      char **pparams, int total_params,
5091                                      char **ppdata, int total_data,
5092                                      unsigned int max_data_bytes)
5093 {
5094         char *params = *pparams;
5095         char *pdata = *ppdata;
5096         uint16 info_level;
5097         unsigned int data_size = 0;
5098         unsigned int param_size = 2;
5099         struct smb_filename *smb_fname = NULL;
5100         bool delete_pending = False;
5101         struct timespec write_time_ts;
5102         files_struct *fsp = NULL;
5103         struct file_id fileid;
5104         struct ea_list *ea_list = NULL;
5105         int lock_data_count = 0;
5106         char *lock_data = NULL;
5107         NTSTATUS status = NT_STATUS_OK;
5108
5109         if (!params) {
5110                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5111                 return;
5112         }
5113
5114         ZERO_STRUCT(write_time_ts);
5115
5116         if (tran_call == TRANSACT2_QFILEINFO) {
5117                 if (total_params < 4) {
5118                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5119                         return;
5120                 }
5121
5122                 if (IS_IPC(conn)) {
5123                         call_trans2qpipeinfo(conn, req, tran_call,
5124                                              pparams, total_params,
5125                                              ppdata, total_data,
5126                                              max_data_bytes);
5127                         return;
5128                 }
5129
5130                 fsp = file_fsp(req, SVAL(params,0));
5131                 info_level = SVAL(params,2);
5132
5133                 DEBUG(3,("call_trans2qfilepathinfo: TRANSACT2_QFILEINFO: level = %d\n", info_level));
5134
5135                 if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
5136                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
5137                         return;
5138                 }
5139
5140                 /* Initial check for valid fsp ptr. */
5141                 if (!check_fsp_open(conn, req, fsp)) {
5142                         return;
5143                 }
5144
5145                 status = copy_smb_filename(talloc_tos(), fsp->fsp_name,
5146                                            &smb_fname);
5147                 if (!NT_STATUS_IS_OK(status)) {
5148                         reply_nterror(req, status);
5149                         return;
5150                 }
5151
5152                 if(fsp->fake_file_handle) {
5153                         /*
5154                          * This is actually for the QUOTA_FAKE_FILE --metze
5155                          */
5156
5157                         /* We know this name is ok, it's already passed the checks. */
5158
5159                 } else if(fsp->fh->fd == -1) {
5160                         /*
5161                          * This is actually a QFILEINFO on a directory
5162                          * handle (returned from an NT SMB). NT5.0 seems
5163                          * to do this call. JRA.
5164                          */
5165
5166                         if (INFO_LEVEL_IS_UNIX(info_level)) {
5167                                 /* Always do lstat for UNIX calls. */
5168                                 if (SMB_VFS_LSTAT(conn, smb_fname)) {
5169                                         DEBUG(3,("call_trans2qfilepathinfo: "
5170                                                  "SMB_VFS_LSTAT of %s failed "
5171                                                  "(%s)\n",
5172                                                  smb_fname_str_dbg(smb_fname),
5173                                                  strerror(errno)));
5174                                         reply_nterror(req,
5175                                                 map_nt_error_from_unix(errno));
5176                                         return;
5177                                 }
5178                         } else if (SMB_VFS_STAT(conn, smb_fname)) {
5179                                 DEBUG(3,("call_trans2qfilepathinfo: "
5180                                          "SMB_VFS_STAT of %s failed (%s)\n",
5181                                          smb_fname_str_dbg(smb_fname),
5182                                          strerror(errno)));
5183                                 reply_nterror(req,
5184                                         map_nt_error_from_unix(errno));
5185                                 return;
5186                         }
5187
5188                         fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
5189                         get_file_infos(fileid, fsp->name_hash, &delete_pending, &write_time_ts);
5190                 } else {
5191                         /*
5192                          * Original code - this is an open file.
5193                          */
5194                         if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
5195                                 DEBUG(3, ("fstat of %s failed (%s)\n",
5196                                           fsp_fnum_dbg(fsp), strerror(errno)));
5197                                 reply_nterror(req,
5198                                         map_nt_error_from_unix(errno));
5199                                 return;
5200                         }
5201                         fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
5202                         get_file_infos(fileid, fsp->name_hash, &delete_pending, &write_time_ts);
5203                 }
5204
5205         } else {
5206                 uint32_t name_hash;
5207                 char *fname = NULL;
5208                 uint32_t ucf_flags = 0;
5209
5210                 /* qpathinfo */
5211                 if (total_params < 7) {
5212                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5213                         return;
5214                 }
5215
5216                 info_level = SVAL(params,0);
5217
5218                 DEBUG(3,("call_trans2qfilepathinfo: TRANSACT2_QPATHINFO: level = %d\n", info_level));
5219
5220                 if (INFO_LEVEL_IS_UNIX(info_level)) {
5221                         if (!lp_unix_extensions()) {
5222                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
5223                                 return;
5224                         }
5225                         if (info_level == SMB_QUERY_FILE_UNIX_BASIC ||
5226                                         info_level == SMB_QUERY_FILE_UNIX_INFO2 ||
5227                                         info_level == SMB_QUERY_FILE_UNIX_LINK) {
5228                                 ucf_flags |= UCF_UNIX_NAME_LOOKUP;
5229                         }
5230                 }
5231
5232                 srvstr_get_path(req, params, req->flags2, &fname, &params[6],
5233                                 total_params - 6,
5234                                 STR_TERMINATE, &status);
5235                 if (!NT_STATUS_IS_OK(status)) {
5236                         reply_nterror(req, status);
5237                         return;
5238                 }
5239
5240                 status = filename_convert(req,
5241                                         conn,
5242                                         req->flags2 & FLAGS2_DFS_PATHNAMES,
5243                                         fname,
5244                                         ucf_flags,
5245                                         NULL,
5246                                         &smb_fname);
5247                 if (!NT_STATUS_IS_OK(status)) {
5248                         if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5249                                 reply_botherror(req,
5250                                                 NT_STATUS_PATH_NOT_COVERED,
5251                                                 ERRSRV, ERRbadpath);
5252                                 return;
5253                         }
5254                         reply_nterror(req, status);
5255                         return;
5256                 }
5257
5258                 /* If this is a stream, check if there is a delete_pending. */
5259                 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
5260                     && is_ntfs_stream_smb_fname(smb_fname)) {
5261                         struct smb_filename *smb_fname_base = NULL;
5262
5263                         /* Create an smb_filename with stream_name == NULL. */
5264                         status =
5265                             create_synthetic_smb_fname(talloc_tos(),
5266                                                        smb_fname->base_name,
5267                                                        NULL, NULL,
5268                                                        &smb_fname_base);
5269                         if (!NT_STATUS_IS_OK(status)) {
5270                                 reply_nterror(req, status);
5271                                 return;
5272                         }
5273
5274                         if (INFO_LEVEL_IS_UNIX(info_level)) {
5275                                 /* Always do lstat for UNIX calls. */
5276                                 if (SMB_VFS_LSTAT(conn, smb_fname_base) != 0) {
5277                                         DEBUG(3,("call_trans2qfilepathinfo: "
5278                                                  "SMB_VFS_LSTAT of %s failed "
5279                                                  "(%s)\n",
5280                                                  smb_fname_str_dbg(smb_fname_base),
5281                                                  strerror(errno)));
5282                                         TALLOC_FREE(smb_fname_base);
5283                                         reply_nterror(req,
5284                                                 map_nt_error_from_unix(errno));
5285                                         return;
5286                                 }
5287                         } else {
5288                                 if (SMB_VFS_STAT(conn, smb_fname_base) != 0) {
5289                                         DEBUG(3,("call_trans2qfilepathinfo: "
5290                                                  "fileinfo of %s failed "
5291                                                  "(%s)\n",
5292                                                  smb_fname_str_dbg(smb_fname_base),
5293                                                  strerror(errno)));
5294                                         TALLOC_FREE(smb_fname_base);
5295                                         reply_nterror(req,
5296                                                 map_nt_error_from_unix(errno));
5297                                         return;
5298                                 }
5299                         }
5300
5301                         status = file_name_hash(conn,
5302                                         smb_fname_str_dbg(smb_fname_base),
5303                                         &name_hash);
5304                         if (!NT_STATUS_IS_OK(status)) {
5305                                 TALLOC_FREE(smb_fname_base);
5306                                 reply_nterror(req, status);
5307                                 return;
5308                         }
5309
5310                         fileid = vfs_file_id_from_sbuf(conn,
5311                                                        &smb_fname_base->st);
5312                         TALLOC_FREE(smb_fname_base);
5313                         get_file_infos(fileid, name_hash, &delete_pending, NULL);
5314                         if (delete_pending) {
5315                                 reply_nterror(req, NT_STATUS_DELETE_PENDING);
5316                                 return;
5317                         }
5318                 }
5319
5320                 if (INFO_LEVEL_IS_UNIX(info_level)) {
5321                         /* Always do lstat for UNIX calls. */
5322                         if (SMB_VFS_LSTAT(conn, smb_fname)) {
5323                                 DEBUG(3,("call_trans2qfilepathinfo: "
5324                                          "SMB_VFS_LSTAT of %s failed (%s)\n",
5325                                          smb_fname_str_dbg(smb_fname),
5326                                          strerror(errno)));
5327                                 reply_nterror(req,
5328                                         map_nt_error_from_unix(errno));
5329                                 return;
5330                         }
5331
5332                 } else {
5333                         if (SMB_VFS_STAT(conn, smb_fname) != 0) {
5334                                 DEBUG(3,("call_trans2qfilepathinfo: "
5335                                          "SMB_VFS_STAT of %s failed (%s)\n",
5336                                          smb_fname_str_dbg(smb_fname),
5337                                          strerror(errno)));
5338                                 reply_nterror(req,
5339                                         map_nt_error_from_unix(errno));
5340                                 return;
5341                         }
5342                 }
5343
5344                 status = file_name_hash(conn,
5345                                 smb_fname_str_dbg(smb_fname),
5346                                 &name_hash);
5347                 if (!NT_STATUS_IS_OK(status)) {
5348                         reply_nterror(req, status);
5349                         return;
5350                 }
5351
5352                 fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
5353                 get_file_infos(fileid, name_hash, &delete_pending, &write_time_ts);
5354                 if (delete_pending) {
5355                         reply_nterror(req, NT_STATUS_DELETE_PENDING);
5356                         return;
5357                 }
5358         }
5359
5360         DEBUG(3,("call_trans2qfilepathinfo %s (%s) level=%d call=%d "
5361                  "total_data=%d\n", smb_fname_str_dbg(smb_fname),
5362                  fsp_fnum_dbg(fsp),
5363                  info_level,tran_call,total_data));
5364
5365         /* Pull out any data sent here before we realloc. */
5366         switch (info_level) {
5367                 case SMB_INFO_QUERY_EAS_FROM_LIST:
5368                 {
5369                         /* Pull any EA list from the data portion. */
5370                         uint32 ea_size;
5371
5372                         if (total_data < 4) {
5373                                 reply_nterror(
5374                                         req, NT_STATUS_INVALID_PARAMETER);
5375                                 return;
5376                         }
5377                         ea_size = IVAL(pdata,0);
5378
5379                         if (total_data > 0 && ea_size != total_data) {
5380                                 DEBUG(4,("call_trans2qfilepathinfo: Rejecting EA request with incorrect \
5381 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
5382                                 reply_nterror(
5383                                         req, NT_STATUS_INVALID_PARAMETER);
5384                                 return;
5385                         }
5386
5387                         if (!lp_ea_support(SNUM(conn))) {
5388                                 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
5389                                 return;
5390                         }
5391
5392                         /* Pull out the list of names. */
5393                         ea_list = read_ea_name_list(req, pdata + 4, ea_size - 4);
5394                         if (!ea_list) {
5395                                 reply_nterror(
5396                                         req, NT_STATUS_INVALID_PARAMETER);
5397                                 return;
5398                         }
5399                         break;
5400                 }
5401
5402                 case SMB_QUERY_POSIX_LOCK:
5403                 {
5404                         if (fsp == NULL || fsp->fh->fd == -1) {
5405                                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
5406                                 return;
5407                         }
5408
5409                         if (total_data != POSIX_LOCK_DATA_SIZE) {
5410                                 reply_nterror(
5411                                         req, NT_STATUS_INVALID_PARAMETER);
5412                                 return;
5413                         }
5414
5415                         /* Copy the lock range data. */
5416                         lock_data = (char *)talloc_memdup(
5417                                 req, pdata, total_data);
5418                         if (!lock_data) {
5419                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
5420                                 return;
5421                         }
5422                         lock_data_count = total_data;
5423                 }
5424                 default:
5425                         break;
5426         }
5427
5428         *pparams = (char *)SMB_REALLOC(*pparams,2);
5429         if (*pparams == NULL) {
5430                 reply_nterror(req, NT_STATUS_NO_MEMORY);
5431                 return;
5432         }
5433         params = *pparams;
5434         SSVAL(params,0,0);
5435
5436         /*
5437          * draft-leach-cifs-v1-spec-02.txt
5438          * 4.2.14 TRANS2_QUERY_PATH_INFORMATION: Get File Attributes given Path
5439          * says:
5440          *
5441          *  The requested information is placed in the Data portion of the
5442          *  transaction response. For the information levels greater than 0x100,
5443          *  the transaction response has 1 parameter word which should be
5444          *  ignored by the client.
5445          *
5446          * However Windows only follows this rule for the IS_NAME_VALID call.
5447          */
5448         switch (info_level) {
5449         case SMB_INFO_IS_NAME_VALID:
5450                 param_size = 0;
5451                 break;
5452         }
5453
5454         if ((info_level & 0xFF00) == 0xFF00) {
5455                 /*
5456                  * We use levels that start with 0xFF00
5457                  * internally to represent SMB2 specific levels
5458                  */
5459                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
5460                 return;
5461         }
5462
5463         status = smbd_do_qfilepathinfo(conn, req, info_level,
5464                                        fsp, smb_fname,
5465                                        delete_pending, write_time_ts,
5466                                        ea_list,
5467                                        lock_data_count, lock_data,
5468                                        req->flags2, max_data_bytes,
5469                                        ppdata, &data_size);
5470         if (!NT_STATUS_IS_OK(status)) {
5471                 reply_nterror(req, status);
5472                 return;
5473         }
5474
5475         send_trans2_replies(conn, req, params, param_size, *ppdata, data_size,
5476                             max_data_bytes);
5477
5478         return;
5479 }
5480
5481 /****************************************************************************
5482  Set a hard link (called by UNIX extensions and by NT rename with HARD link
5483  code.
5484 ****************************************************************************/
5485
5486 NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
5487                 connection_struct *conn,
5488                 struct smb_request *req,
5489                 bool overwrite_if_exists,
5490                 const struct smb_filename *smb_fname_old,
5491                 struct smb_filename *smb_fname_new)
5492 {
5493         NTSTATUS status = NT_STATUS_OK;
5494
5495         /* source must already exist. */
5496         if (!VALID_STAT(smb_fname_old->st)) {
5497                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5498         }
5499
5500         if (VALID_STAT(smb_fname_new->st)) {
5501                 if (overwrite_if_exists) {
5502                         if (S_ISDIR(smb_fname_new->st.st_ex_mode)) {
5503                                 return NT_STATUS_FILE_IS_A_DIRECTORY;
5504                         }
5505                         status = unlink_internals(conn,
5506                                                 req,
5507                                                 FILE_ATTRIBUTE_NORMAL,
5508                                                 smb_fname_new,
5509                                                 false);
5510                         if (!NT_STATUS_IS_OK(status)) {
5511                                 return status;
5512                         }
5513                 } else {
5514                         /* Disallow if newname already exists. */
5515                         return NT_STATUS_OBJECT_NAME_COLLISION;
5516                 }
5517         }
5518
5519         /* No links from a directory. */
5520         if (S_ISDIR(smb_fname_old->st.st_ex_mode)) {
5521                 return NT_STATUS_FILE_IS_A_DIRECTORY;
5522         }
5523
5524         /* Setting a hardlink to/from a stream isn't currently supported. */
5525         if (is_ntfs_stream_smb_fname(smb_fname_old) ||
5526             is_ntfs_stream_smb_fname(smb_fname_new)) {
5527                 return NT_STATUS_INVALID_PARAMETER;
5528         }
5529
5530         DEBUG(10,("hardlink_internals: doing hard link %s -> %s\n",
5531                   smb_fname_old->base_name, smb_fname_new->base_name));
5532
5533         if (SMB_VFS_LINK(conn, smb_fname_old->base_name,
5534                          smb_fname_new->base_name) != 0) {
5535                 status = map_nt_error_from_unix(errno);
5536                 DEBUG(3,("hardlink_internals: Error %s hard link %s -> %s\n",
5537                          nt_errstr(status), smb_fname_old->base_name,
5538                          smb_fname_new->base_name));
5539         }
5540         return status;
5541 }
5542
5543 /****************************************************************************
5544  Deal with setting the time from any of the setfilepathinfo functions.
5545  NOTE !!!! The check for FILE_WRITE_ATTRIBUTES access must be done *before*
5546  calling this function.
5547 ****************************************************************************/
5548
5549 NTSTATUS smb_set_file_time(connection_struct *conn,
5550                            files_struct *fsp,
5551                            const struct smb_filename *smb_fname,
5552                            struct smb_file_time *ft,
5553                            bool setting_write_time)
5554 {
5555         struct smb_filename smb_fname_base;
5556         uint32 action =
5557                 FILE_NOTIFY_CHANGE_LAST_ACCESS
5558                 |FILE_NOTIFY_CHANGE_LAST_WRITE
5559                 |FILE_NOTIFY_CHANGE_CREATION;
5560
5561         if (!VALID_STAT(smb_fname->st)) {
5562                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5563         }
5564
5565         /* get some defaults (no modifications) if any info is zero or -1. */
5566         if (null_timespec(ft->create_time)) {
5567                 action &= ~FILE_NOTIFY_CHANGE_CREATION;
5568         }
5569
5570         if (null_timespec(ft->atime)) {
5571                 action &= ~FILE_NOTIFY_CHANGE_LAST_ACCESS;
5572         }
5573
5574         if (null_timespec(ft->mtime)) {
5575                 action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
5576         }
5577
5578         if (!setting_write_time) {
5579                 /* ft->mtime comes from change time, not write time. */
5580                 action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
5581         }
5582
5583         /* Ensure the resolution is the correct for
5584          * what we can store on this filesystem. */
5585
5586         round_timespec(conn->ts_res, &ft->create_time);
5587         round_timespec(conn->ts_res, &ft->ctime);
5588         round_timespec(conn->ts_res, &ft->atime);
5589         round_timespec(conn->ts_res, &ft->mtime);
5590
5591         DEBUG(5,("smb_set_filetime: actime: %s\n ",
5592                 time_to_asc(convert_timespec_to_time_t(ft->atime))));
5593         DEBUG(5,("smb_set_filetime: modtime: %s\n ",
5594                 time_to_asc(convert_timespec_to_time_t(ft->mtime))));
5595         DEBUG(5,("smb_set_filetime: ctime: %s\n ",
5596                 time_to_asc(convert_timespec_to_time_t(ft->ctime))));
5597         DEBUG(5,("smb_set_file_time: createtime: %s\n ",
5598                 time_to_asc(convert_timespec_to_time_t(ft->create_time))));
5599
5600         if (setting_write_time) {
5601                 /*
5602                  * This was a Windows setfileinfo on an open file.
5603                  * NT does this a lot. We also need to 
5604                  * set the time here, as it can be read by 
5605                  * FindFirst/FindNext and with the patch for bug #2045
5606                  * in smbd/fileio.c it ensures that this timestamp is
5607                  * kept sticky even after a write. We save the request
5608                  * away and will set it on file close and after a write. JRA.
5609                  */
5610
5611                 DEBUG(10,("smb_set_file_time: setting pending modtime to %s\n",
5612                           time_to_asc(convert_timespec_to_time_t(ft->mtime))));
5613
5614                 if (fsp != NULL) {
5615                         if (fsp->base_fsp) {
5616                                 set_sticky_write_time_fsp(fsp->base_fsp,
5617                                                           ft->mtime);
5618                         } else {
5619                                 set_sticky_write_time_fsp(fsp, ft->mtime);
5620                         }
5621                 } else {
5622                         set_sticky_write_time_path(
5623                                 vfs_file_id_from_sbuf(conn, &smb_fname->st),
5624                                 ft->mtime);
5625                 }
5626         }
5627
5628         DEBUG(10,("smb_set_file_time: setting utimes to modified values.\n"));
5629
5630         /* Always call ntimes on the base, even if a stream was passed in. */
5631         smb_fname_base = *smb_fname;
5632         smb_fname_base.stream_name = NULL;
5633
5634         if(file_ntimes(conn, &smb_fname_base, ft)!=0) {
5635                 return map_nt_error_from_unix(errno);
5636         }
5637
5638         notify_fname(conn, NOTIFY_ACTION_MODIFIED, action,
5639                      smb_fname->base_name);
5640         return NT_STATUS_OK;
5641 }
5642
5643 /****************************************************************************
5644  Deal with setting the dosmode from any of the setfilepathinfo functions.
5645  NB. The check for FILE_WRITE_ATTRIBUTES access on this path must have been
5646  done before calling this function.
5647 ****************************************************************************/
5648
5649 static NTSTATUS smb_set_file_dosmode(connection_struct *conn,
5650                                      const struct smb_filename *smb_fname,
5651                                      uint32 dosmode)
5652 {
5653         struct smb_filename *smb_fname_base = NULL;
5654         NTSTATUS status;
5655
5656         if (!VALID_STAT(smb_fname->st)) {
5657                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5658         }
5659
5660         /* Always operate on the base_name, even if a stream was passed in. */
5661         status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
5662                                             NULL, &smb_fname->st,
5663                                             &smb_fname_base);
5664         if (!NT_STATUS_IS_OK(status)) {
5665                 return status;
5666         }
5667
5668         if (dosmode) {
5669                 if (S_ISDIR(smb_fname_base->st.st_ex_mode)) {
5670                         dosmode |= FILE_ATTRIBUTE_DIRECTORY;
5671                 } else {
5672                         dosmode &= ~FILE_ATTRIBUTE_DIRECTORY;
5673                 }
5674         }
5675
5676         DEBUG(6,("smb_set_file_dosmode: dosmode: 0x%x\n", (unsigned int)dosmode));
5677
5678         /* check the mode isn't different, before changing it */
5679         if ((dosmode != 0) && (dosmode != dos_mode(conn, smb_fname_base))) {
5680                 DEBUG(10,("smb_set_file_dosmode: file %s : setting dos mode "
5681                           "0x%x\n", smb_fname_str_dbg(smb_fname_base),
5682                           (unsigned int)dosmode));
5683
5684                 if(file_set_dosmode(conn, smb_fname_base, dosmode, NULL,
5685                                     false)) {
5686                         DEBUG(2,("smb_set_file_dosmode: file_set_dosmode of "
5687                                  "%s failed (%s)\n",
5688                                  smb_fname_str_dbg(smb_fname_base),
5689                                  strerror(errno)));
5690                         status = map_nt_error_from_unix(errno);
5691                         goto out;
5692                 }
5693         }
5694         status = NT_STATUS_OK;
5695  out:
5696         TALLOC_FREE(smb_fname_base);
5697         return status;
5698 }
5699
5700 /****************************************************************************
5701  Deal with setting the size from any of the setfilepathinfo functions.
5702 ****************************************************************************/
5703
5704 static NTSTATUS smb_set_file_size(connection_struct *conn,
5705                                   struct smb_request *req,
5706                                   files_struct *fsp,
5707                                   const struct smb_filename *smb_fname,
5708                                   const SMB_STRUCT_STAT *psbuf,
5709                                   off_t size,
5710                                   bool fail_after_createfile)
5711 {
5712         NTSTATUS status = NT_STATUS_OK;
5713         struct smb_filename *smb_fname_tmp = NULL;
5714         files_struct *new_fsp = NULL;
5715
5716         if (!VALID_STAT(*psbuf)) {
5717                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5718         }
5719
5720         DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));
5721
5722         if (size == get_file_size_stat(psbuf)) {
5723                 return NT_STATUS_OK;
5724         }
5725
5726         DEBUG(10,("smb_set_file_size: file %s : setting new size to %.0f\n",
5727                   smb_fname_str_dbg(smb_fname), (double)size));
5728
5729         if (fsp && fsp->fh->fd != -1) {
5730                 /* Handle based call. */
5731                 if (!(fsp->access_mask & FILE_WRITE_DATA)) {
5732                         return NT_STATUS_ACCESS_DENIED;
5733                 }
5734
5735                 if (vfs_set_filelen(fsp, size) == -1) {
5736                         return map_nt_error_from_unix(errno);
5737                 }
5738                 trigger_write_time_update_immediate(fsp);
5739                 return NT_STATUS_OK;
5740         }
5741
5742         status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
5743         if (!NT_STATUS_IS_OK(status)) {
5744                 return status;
5745         }
5746
5747         smb_fname_tmp->st = *psbuf;
5748
5749         status = SMB_VFS_CREATE_FILE(
5750                 conn,                                   /* conn */
5751                 req,                                    /* req */
5752                 0,                                      /* root_dir_fid */
5753                 smb_fname_tmp,                          /* fname */
5754                 FILE_WRITE_DATA,                        /* access_mask */
5755                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
5756                     FILE_SHARE_DELETE),
5757                 FILE_OPEN,                              /* create_disposition*/
5758                 0,                                      /* create_options */
5759                 FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
5760                 FORCE_OPLOCK_BREAK_TO_NONE,             /* oplock_request */
5761                 0,                                      /* allocation_size */
5762                 0,                                      /* private_flags */
5763                 NULL,                                   /* sd */
5764                 NULL,                                   /* ea_list */
5765                 &new_fsp,                               /* result */
5766                 NULL);                                  /* pinfo */
5767
5768         TALLOC_FREE(smb_fname_tmp);
5769
5770         if (!NT_STATUS_IS_OK(status)) {
5771                 /* NB. We check for open_was_deferred in the caller. */
5772                 return status;
5773         }
5774
5775         /* See RAW-SFILEINFO-END-OF-FILE */
5776         if (fail_after_createfile) {
5777                 close_file(req, new_fsp,NORMAL_CLOSE);
5778                 return NT_STATUS_INVALID_LEVEL;
5779         }
5780
5781         if (vfs_set_filelen(new_fsp, size) == -1) {
5782                 status = map_nt_error_from_unix(errno);
5783                 close_file(req, new_fsp,NORMAL_CLOSE);
5784                 return status;
5785         }
5786
5787         trigger_write_time_update_immediate(new_fsp);
5788         close_file(req, new_fsp,NORMAL_CLOSE);
5789         return NT_STATUS_OK;
5790 }
5791
5792 /****************************************************************************
5793  Deal with SMB_INFO_SET_EA.
5794 ****************************************************************************/
5795
5796 static NTSTATUS smb_info_set_ea(connection_struct *conn,
5797                                 const char *pdata,
5798                                 int total_data,
5799                                 files_struct *fsp,
5800                                 const struct smb_filename *smb_fname)
5801 {
5802         struct ea_list *ea_list = NULL;
5803         TALLOC_CTX *ctx = NULL;
5804         NTSTATUS status = NT_STATUS_OK;
5805
5806         if (total_data < 10) {
5807
5808                 /* OS/2 workplace shell seems to send SET_EA requests of "null"
5809                    length. They seem to have no effect. Bug #3212. JRA */
5810
5811                 if ((total_data == 4) && (IVAL(pdata,0) == 4)) {
5812                         /* We're done. We only get EA info in this call. */
5813                         return NT_STATUS_OK;
5814                 }
5815
5816                 return NT_STATUS_INVALID_PARAMETER;
5817         }
5818
5819         if (IVAL(pdata,0) > total_data) {
5820                 DEBUG(10,("smb_info_set_ea: bad total data size (%u) > %u\n",
5821                         IVAL(pdata,0), (unsigned int)total_data));
5822                 return NT_STATUS_INVALID_PARAMETER;
5823         }
5824
5825         ctx = talloc_tos();
5826         ea_list = read_ea_list(ctx, pdata + 4, total_data - 4);
5827         if (!ea_list) {
5828                 return NT_STATUS_INVALID_PARAMETER;
5829         }
5830
5831         status = set_ea(conn, fsp, smb_fname, ea_list);
5832
5833         return status;
5834 }
5835
5836 /****************************************************************************
5837  Deal with SMB_FILE_FULL_EA_INFORMATION set.
5838 ****************************************************************************/
5839
5840 static NTSTATUS smb_set_file_full_ea_info(connection_struct *conn,
5841                                 const char *pdata,
5842                                 int total_data,
5843                                 files_struct *fsp)
5844 {
5845         struct ea_list *ea_list = NULL;
5846         NTSTATUS status;
5847
5848         if (!fsp) {
5849                 return NT_STATUS_INVALID_HANDLE;
5850         }
5851
5852         if (!lp_ea_support(SNUM(conn))) {
5853                 DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u but "
5854                         "EA's not supported.\n",
5855                         (unsigned int)total_data));
5856                 return NT_STATUS_EAS_NOT_SUPPORTED;
5857         }
5858
5859         if (total_data < 10) {
5860                 DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u "
5861                         "too small.\n",
5862                         (unsigned int)total_data));
5863                 return NT_STATUS_INVALID_PARAMETER;
5864         }
5865
5866         ea_list = read_nttrans_ea_list(talloc_tos(),
5867                                 pdata,
5868                                 total_data);
5869
5870         if (!ea_list) {
5871                 return NT_STATUS_INVALID_PARAMETER;
5872         }
5873
5874         status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
5875
5876         DEBUG(10, ("smb_set_file_full_ea_info on file %s returned %s\n",
5877                 smb_fname_str_dbg(fsp->fsp_name),
5878                 nt_errstr(status) ));
5879
5880         return status;
5881 }
5882
5883
5884 /****************************************************************************
5885  Deal with SMB_SET_FILE_DISPOSITION_INFO.
5886 ****************************************************************************/
5887
5888 static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
5889                                 const char *pdata,
5890                                 int total_data,
5891                                 files_struct *fsp,
5892                                 struct smb_filename *smb_fname)
5893 {
5894         NTSTATUS status = NT_STATUS_OK;
5895         bool delete_on_close;
5896         uint32 dosmode = 0;
5897
5898         if (total_data < 1) {
5899                 return NT_STATUS_INVALID_PARAMETER;
5900         }
5901
5902         if (fsp == NULL) {
5903                 return NT_STATUS_INVALID_HANDLE;
5904         }
5905
5906         delete_on_close = (CVAL(pdata,0) ? True : False);
5907         dosmode = dos_mode(conn, smb_fname);
5908
5909         DEBUG(10,("smb_set_file_disposition_info: file %s, dosmode = %u, "
5910                 "delete_on_close = %u\n",
5911                 smb_fname_str_dbg(smb_fname),
5912                 (unsigned int)dosmode,
5913                 (unsigned int)delete_on_close ));
5914
5915         if (delete_on_close) {
5916                 status = can_set_delete_on_close(fsp, dosmode);
5917                 if (!NT_STATUS_IS_OK(status)) {
5918                         return status;
5919                 }
5920         }
5921
5922         /* The set is across all open files on this dev/inode pair. */
5923         if (!set_delete_on_close(fsp, delete_on_close,
5924                                  conn->session_info->security_token,
5925                                  conn->session_info->unix_token)) {
5926                 return NT_STATUS_ACCESS_DENIED;
5927         }
5928         return NT_STATUS_OK;
5929 }
5930
5931 /****************************************************************************
5932  Deal with SMB_FILE_POSITION_INFORMATION.
5933 ****************************************************************************/
5934
5935 static NTSTATUS smb_file_position_information(connection_struct *conn,
5936                                 const char *pdata,
5937                                 int total_data,
5938                                 files_struct *fsp)
5939 {
5940         uint64_t position_information;
5941
5942         if (total_data < 8) {
5943                 return NT_STATUS_INVALID_PARAMETER;
5944         }
5945
5946         if (fsp == NULL) {
5947                 /* Ignore on pathname based set. */
5948                 return NT_STATUS_OK;
5949         }
5950
5951         position_information = (uint64_t)IVAL(pdata,0);
5952         position_information |= (((uint64_t)IVAL(pdata,4)) << 32);
5953
5954         DEBUG(10,("smb_file_position_information: Set file position "
5955                   "information for file %s to %.0f\n", fsp_str_dbg(fsp),
5956                   (double)position_information));
5957         fsp->fh->position_information = position_information;
5958         return NT_STATUS_OK;
5959 }
5960
5961 /****************************************************************************
5962  Deal with SMB_FILE_MODE_INFORMATION.
5963 ****************************************************************************/
5964
5965 static NTSTATUS smb_file_mode_information(connection_struct *conn,
5966                                 const char *pdata,
5967                                 int total_data)
5968 {
5969         uint32 mode;
5970
5971         if (total_data < 4) {
5972                 return NT_STATUS_INVALID_PARAMETER;
5973         }
5974         mode = IVAL(pdata,0);
5975         if (mode != 0 && mode != 2 && mode != 4 && mode != 6) {
5976                 return NT_STATUS_INVALID_PARAMETER;
5977         }
5978         return NT_STATUS_OK;
5979 }
5980
5981 /****************************************************************************
5982  Deal with SMB_SET_FILE_UNIX_LINK (create a UNIX symlink).
5983 ****************************************************************************/
5984
5985 static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
5986                                        struct smb_request *req,
5987                                        const char *pdata,
5988                                        int total_data,
5989                                        const struct smb_filename *smb_fname)
5990 {
5991         char *link_target = NULL;
5992         const char *newname = smb_fname->base_name;
5993         TALLOC_CTX *ctx = talloc_tos();
5994
5995         /* Set a symbolic link. */
5996         /* Don't allow this if follow links is false. */
5997
5998         if (total_data == 0) {
5999                 return NT_STATUS_INVALID_PARAMETER;
6000         }
6001
6002         if (!lp_symlinks(SNUM(conn))) {
6003                 return NT_STATUS_ACCESS_DENIED;
6004         }
6005
6006         srvstr_pull_talloc(ctx, pdata, req->flags2, &link_target, pdata,
6007                     total_data, STR_TERMINATE);
6008
6009         if (!link_target) {
6010                 return NT_STATUS_INVALID_PARAMETER;
6011         }
6012
6013         DEBUG(10,("smb_set_file_unix_link: SMB_SET_FILE_UNIX_LINK doing symlink %s -> %s\n",
6014                         newname, link_target ));
6015
6016         if (SMB_VFS_SYMLINK(conn,link_target,newname) != 0) {
6017                 return map_nt_error_from_unix(errno);
6018         }
6019
6020         return NT_STATUS_OK;
6021 }
6022
6023 /****************************************************************************
6024  Deal with SMB_SET_FILE_UNIX_HLINK (create a UNIX hard link).
6025 ****************************************************************************/
6026
6027 static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
6028                                         struct smb_request *req,
6029                                         const char *pdata, int total_data,
6030                                         struct smb_filename *smb_fname_new)
6031 {
6032         char *oldname = NULL;
6033         struct smb_filename *smb_fname_old = NULL;
6034         TALLOC_CTX *ctx = talloc_tos();
6035         NTSTATUS status = NT_STATUS_OK;
6036
6037         /* Set a hard link. */
6038         if (total_data == 0) {
6039                 return NT_STATUS_INVALID_PARAMETER;
6040         }
6041
6042         srvstr_get_path(ctx, pdata, req->flags2, &oldname, pdata,
6043                         total_data, STR_TERMINATE, &status);
6044         if (!NT_STATUS_IS_OK(status)) {
6045                 return status;
6046         }
6047
6048         DEBUG(10,("smb_set_file_unix_hlink: SMB_SET_FILE_UNIX_LINK doing hard link %s -> %s\n",
6049                 smb_fname_str_dbg(smb_fname_new), oldname));
6050
6051         status = filename_convert(ctx,
6052                                 conn,
6053                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
6054                                 oldname,
6055                                 0,
6056                                 NULL,
6057                                 &smb_fname_old);
6058         if (!NT_STATUS_IS_OK(status)) {
6059                 return status;
6060         }
6061
6062         return hardlink_internals(ctx, conn, req, false,
6063                         smb_fname_old, smb_fname_new);
6064 }
6065
6066 /****************************************************************************
6067  Deal with SMB2_FILE_RENAME_INFORMATION_INTERNAL
6068 ****************************************************************************/
6069
6070 static NTSTATUS smb2_file_rename_information(connection_struct *conn,
6071                                             struct smb_request *req,
6072                                             const char *pdata,
6073                                             int total_data,
6074                                             files_struct *fsp,
6075                                             struct smb_filename *smb_fname_src)
6076 {
6077         bool overwrite;
6078         uint32_t len;
6079         char *newname = NULL;
6080         struct smb_filename *smb_fname_dst = NULL;
6081         NTSTATUS status = NT_STATUS_OK;
6082         TALLOC_CTX *ctx = talloc_tos();
6083
6084         if (!fsp) {
6085                 return NT_STATUS_INVALID_HANDLE;
6086         }
6087
6088         if (total_data < 20) {
6089                 return NT_STATUS_INVALID_PARAMETER;
6090         }
6091
6092         overwrite = (CVAL(pdata,0) ? True : False);
6093         len = IVAL(pdata,16);
6094
6095         if (len > (total_data - 20) || (len == 0)) {
6096                 return NT_STATUS_INVALID_PARAMETER;
6097         }
6098
6099         srvstr_get_path(ctx, pdata, req->flags2, &newname,
6100                                 &pdata[20], len, STR_TERMINATE,
6101                                 &status);
6102         if (!NT_STATUS_IS_OK(status)) {
6103                 return status;
6104         }
6105
6106         DEBUG(10,("smb2_file_rename_information: got name |%s|\n",
6107                                 newname));
6108
6109         status = filename_convert(ctx,
6110                                 conn,
6111                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
6112                                 newname,
6113                                 UCF_SAVE_LCOMP,
6114                                 NULL,
6115                                 &smb_fname_dst);
6116         if (!NT_STATUS_IS_OK(status)) {
6117                 return status;
6118         }
6119
6120         if (fsp->base_fsp) {
6121                 /* newname must be a stream name. */
6122                 if (newname[0] != ':') {
6123                         return NT_STATUS_NOT_SUPPORTED;
6124                 }
6125
6126                 /* Create an smb_fname to call rename_internals_fsp() with. */
6127                 status = create_synthetic_smb_fname(talloc_tos(),
6128                     fsp->base_fsp->fsp_name->base_name, newname, NULL,
6129                     &smb_fname_dst);
6130                 if (!NT_STATUS_IS_OK(status)) {
6131                         goto out;
6132                 }
6133
6134                 /*
6135                  * Set the original last component, since
6136                  * rename_internals_fsp() requires it.
6137                  */
6138                 smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
6139                                                               newname);
6140                 if (smb_fname_dst->original_lcomp == NULL) {
6141                         status = NT_STATUS_NO_MEMORY;
6142                         goto out;
6143                 }
6144
6145         }
6146
6147         DEBUG(10,("smb2_file_rename_information: "
6148                   "SMB_FILE_RENAME_INFORMATION (%s) %s -> %s\n",
6149                   fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
6150                   smb_fname_str_dbg(smb_fname_dst)));
6151         status = rename_internals_fsp(conn, fsp, smb_fname_dst,
6152                                 (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM),
6153                                 overwrite);
6154
6155  out:
6156         TALLOC_FREE(smb_fname_dst);
6157         return status;
6158 }
6159
6160 static NTSTATUS smb_file_link_information(connection_struct *conn,
6161                                             struct smb_request *req,
6162                                             const char *pdata,
6163                                             int total_data,
6164                                             files_struct *fsp,
6165                                             struct smb_filename *smb_fname_src)
6166 {
6167         bool overwrite;
6168         uint32_t len;
6169         char *newname = NULL;
6170         struct smb_filename *smb_fname_dst = NULL;
6171         NTSTATUS status = NT_STATUS_OK;
6172         TALLOC_CTX *ctx = talloc_tos();
6173
6174         if (!fsp) {
6175                 return NT_STATUS_INVALID_HANDLE;
6176         }
6177
6178         if (total_data < 20) {
6179                 return NT_STATUS_INVALID_PARAMETER;
6180         }
6181
6182         overwrite = (CVAL(pdata,0) ? true : false);
6183         len = IVAL(pdata,16);
6184
6185         if (len > (total_data - 20) || (len == 0)) {
6186                 return NT_STATUS_INVALID_PARAMETER;
6187         }
6188
6189         srvstr_get_path(ctx, pdata, req->flags2, &newname,
6190                                 &pdata[20], len, STR_TERMINATE,
6191                                 &status);
6192         if (!NT_STATUS_IS_OK(status)) {
6193                 return status;
6194         }
6195
6196         DEBUG(10,("smb_file_link_information: got name |%s|\n",
6197                                 newname));
6198
6199         status = filename_convert(ctx,
6200                                 conn,
6201                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
6202                                 newname,
6203                                 UCF_SAVE_LCOMP,
6204                                 NULL,
6205                                 &smb_fname_dst);
6206         if (!NT_STATUS_IS_OK(status)) {
6207                 return status;
6208         }
6209
6210         if (fsp->base_fsp) {
6211                 /* No stream names. */
6212                 return NT_STATUS_NOT_SUPPORTED;
6213         }
6214
6215         DEBUG(10,("smb_file_link_information: "
6216                   "SMB_FILE_LINK_INFORMATION (%s) %s -> %s\n",
6217                   fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
6218                   smb_fname_str_dbg(smb_fname_dst)));
6219         status = hardlink_internals(ctx,
6220                                 conn,
6221                                 req,
6222                                 overwrite,
6223                                 fsp->fsp_name,
6224                                 smb_fname_dst);
6225
6226         TALLOC_FREE(smb_fname_dst);
6227         return status;
6228 }
6229
6230 /****************************************************************************
6231  Deal with SMB_FILE_RENAME_INFORMATION.
6232 ****************************************************************************/
6233
6234 static NTSTATUS smb_file_rename_information(connection_struct *conn,
6235                                             struct smb_request *req,
6236                                             const char *pdata,
6237                                             int total_data,
6238                                             files_struct *fsp,
6239                                             struct smb_filename *smb_fname_src)
6240 {
6241         bool overwrite;
6242         uint32 root_fid;
6243         uint32 len;
6244         char *newname = NULL;
6245         struct smb_filename *smb_fname_dst = NULL;
6246         bool dest_has_wcard = False;
6247         NTSTATUS status = NT_STATUS_OK;
6248         char *p;
6249         TALLOC_CTX *ctx = talloc_tos();
6250
6251         if (total_data < 13) {
6252                 return NT_STATUS_INVALID_PARAMETER;
6253         }
6254
6255         overwrite = (CVAL(pdata,0) ? True : False);
6256         root_fid = IVAL(pdata,4);
6257         len = IVAL(pdata,8);
6258
6259         if (len > (total_data - 12) || (len == 0) || (root_fid != 0)) {
6260                 return NT_STATUS_INVALID_PARAMETER;
6261         }
6262
6263         srvstr_get_path_wcard(ctx, pdata, req->flags2, &newname, &pdata[12],
6264                               len, 0, &status,
6265                               &dest_has_wcard);
6266         if (!NT_STATUS_IS_OK(status)) {
6267                 return status;
6268         }
6269
6270         DEBUG(10,("smb_file_rename_information: got name |%s|\n",
6271                                 newname));
6272
6273         status = resolve_dfspath_wcard(ctx, conn,
6274                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
6275                                        newname,
6276                                        true,
6277                                        !conn->sconn->using_smb2,
6278                                        &newname,
6279                                        &dest_has_wcard);
6280         if (!NT_STATUS_IS_OK(status)) {
6281                 return status;
6282         }
6283
6284         /* Check the new name has no '/' characters. */
6285         if (strchr_m(newname, '/')) {
6286                 return NT_STATUS_NOT_SUPPORTED;
6287         }
6288
6289         if (fsp && fsp->base_fsp) {
6290                 /* newname must be a stream name. */
6291                 if (newname[0] != ':') {
6292                         return NT_STATUS_NOT_SUPPORTED;
6293                 }
6294
6295                 /* Create an smb_fname to call rename_internals_fsp() with. */
6296                 status = create_synthetic_smb_fname(talloc_tos(),
6297                     fsp->base_fsp->fsp_name->base_name, newname, NULL,
6298                     &smb_fname_dst);
6299                 if (!NT_STATUS_IS_OK(status)) {
6300                         goto out;
6301                 }
6302
6303                 /*
6304                  * Set the original last component, since
6305                  * rename_internals_fsp() requires it.
6306                  */
6307                 smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
6308                                                               newname);
6309                 if (smb_fname_dst->original_lcomp == NULL) {
6310                         status = NT_STATUS_NO_MEMORY;
6311                         goto out;
6312                 }
6313
6314         } else {
6315                 /*
6316                  * Build up an smb_fname_dst based on the filename passed in.
6317                  * We basically just strip off the last component, and put on
6318                  * the newname instead.
6319                  */
6320                 char *base_name = NULL;
6321
6322                 /* newname must *not* be a stream name. */
6323                 if (newname[0] == ':') {
6324                         return NT_STATUS_NOT_SUPPORTED;
6325                 }
6326
6327                 /*
6328                  * Strip off the last component (filename) of the path passed
6329                  * in.
6330                  */
6331                 base_name = talloc_strdup(ctx, smb_fname_src->base_name);
6332                 if (!base_name) {
6333                         return NT_STATUS_NO_MEMORY;
6334                 }
6335                 p = strrchr_m(base_name, '/');
6336                 if (p) {
6337                         p[1] = '\0';
6338                 } else {
6339                         base_name = talloc_strdup(ctx, "");
6340                         if (!base_name) {
6341                                 return NT_STATUS_NO_MEMORY;
6342                         }
6343                 }
6344                 /* Append the new name. */
6345                 base_name = talloc_asprintf_append(base_name,
6346                                 "%s",
6347                                 newname);
6348                 if (!base_name) {
6349                         return NT_STATUS_NO_MEMORY;
6350                 }
6351
6352                 status = unix_convert(ctx, conn, base_name, &smb_fname_dst,
6353                                       (UCF_SAVE_LCOMP |
6354                                           (dest_has_wcard ?
6355                                               UCF_ALWAYS_ALLOW_WCARD_LCOMP :
6356                                               0)));
6357
6358                 /* If an error we expect this to be
6359                  * NT_STATUS_OBJECT_PATH_NOT_FOUND */
6360
6361                 if (!NT_STATUS_IS_OK(status)) {
6362                         if(!NT_STATUS_EQUAL(NT_STATUS_OBJECT_PATH_NOT_FOUND,
6363                                             status)) {
6364                                 goto out;
6365                         }
6366                         /* Create an smb_fname to call rename_internals_fsp() */
6367                         status = create_synthetic_smb_fname(ctx,
6368                                                             base_name, NULL,
6369                                                             NULL,
6370                                                             &smb_fname_dst);
6371                         if (!NT_STATUS_IS_OK(status)) {
6372                                 goto out;
6373                         }
6374                 }
6375         }
6376
6377         if (fsp) {
6378                 DEBUG(10,("smb_file_rename_information: "
6379                           "SMB_FILE_RENAME_INFORMATION (%s) %s -> %s\n",
6380                           fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
6381                           smb_fname_str_dbg(smb_fname_dst)));
6382                 status = rename_internals_fsp(conn, fsp, smb_fname_dst, 0,
6383                                               overwrite);
6384         } else {
6385                 DEBUG(10,("smb_file_rename_information: "
6386                           "SMB_FILE_RENAME_INFORMATION %s -> %s\n",
6387                           smb_fname_str_dbg(smb_fname_src),
6388                           smb_fname_str_dbg(smb_fname_dst)));
6389                 status = rename_internals(ctx, conn, req, smb_fname_src,
6390                                           smb_fname_dst, 0, overwrite, false,
6391                                           dest_has_wcard,
6392                                           FILE_WRITE_ATTRIBUTES);
6393         }
6394  out:
6395         TALLOC_FREE(smb_fname_dst);
6396         return status;
6397 }
6398
6399 /****************************************************************************
6400  Deal with SMB_SET_POSIX_ACL.
6401 ****************************************************************************/
6402
6403 #if defined(HAVE_POSIX_ACLS)
6404 static NTSTATUS smb_set_posix_acl(connection_struct *conn,
6405                                 const char *pdata,
6406                                 int total_data,
6407                                 files_struct *fsp,
6408                                 const struct smb_filename *smb_fname)
6409 {
6410         uint16 posix_acl_version;
6411         uint16 num_file_acls;
6412         uint16 num_def_acls;
6413         bool valid_file_acls = True;
6414         bool valid_def_acls = True;
6415
6416         if (total_data < SMB_POSIX_ACL_HEADER_SIZE) {
6417                 return NT_STATUS_INVALID_PARAMETER;
6418         }
6419         posix_acl_version = SVAL(pdata,0);
6420         num_file_acls = SVAL(pdata,2);
6421         num_def_acls = SVAL(pdata,4);
6422
6423         if (num_file_acls == SMB_POSIX_IGNORE_ACE_ENTRIES) {
6424                 valid_file_acls = False;
6425                 num_file_acls = 0;
6426         }
6427
6428         if (num_def_acls == SMB_POSIX_IGNORE_ACE_ENTRIES) {
6429                 valid_def_acls = False;
6430                 num_def_acls = 0;
6431         }
6432
6433         if (posix_acl_version != SMB_POSIX_ACL_VERSION) {
6434                 return NT_STATUS_INVALID_PARAMETER;
6435         }
6436
6437         if (total_data < SMB_POSIX_ACL_HEADER_SIZE +
6438                         (num_file_acls+num_def_acls)*SMB_POSIX_ACL_ENTRY_SIZE) {
6439                 return NT_STATUS_INVALID_PARAMETER;
6440         }
6441
6442         DEBUG(10,("smb_set_posix_acl: file %s num_file_acls = %u, num_def_acls = %u\n",
6443                 smb_fname ? smb_fname_str_dbg(smb_fname) : fsp_str_dbg(fsp),
6444                 (unsigned int)num_file_acls,
6445                 (unsigned int)num_def_acls));
6446
6447         if (valid_file_acls && !set_unix_posix_acl(conn, fsp,
6448                 smb_fname->base_name, num_file_acls,
6449                 pdata + SMB_POSIX_ACL_HEADER_SIZE)) {
6450                 return map_nt_error_from_unix(errno);
6451         }
6452
6453         if (valid_def_acls && !set_unix_posix_default_acl(conn,
6454                 smb_fname->base_name, &smb_fname->st, num_def_acls,
6455                 pdata + SMB_POSIX_ACL_HEADER_SIZE +
6456                 (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE))) {
6457                 return map_nt_error_from_unix(errno);
6458         }
6459         return NT_STATUS_OK;
6460 }
6461 #endif
6462
6463 /****************************************************************************
6464  Deal with SMB_SET_POSIX_LOCK.
6465 ****************************************************************************/
6466
6467 static NTSTATUS smb_set_posix_lock(connection_struct *conn,
6468                                 struct smb_request *req,
6469                                 const char *pdata,
6470                                 int total_data,
6471                                 files_struct *fsp)
6472 {
6473         uint64_t count;
6474         uint64_t offset;
6475         uint64_t smblctx;
6476         bool blocking_lock = False;
6477         enum brl_type lock_type;
6478
6479         NTSTATUS status = NT_STATUS_OK;
6480
6481         if (fsp == NULL || fsp->fh->fd == -1) {
6482                 return NT_STATUS_INVALID_HANDLE;
6483         }
6484
6485         if (total_data != POSIX_LOCK_DATA_SIZE) {
6486                 return NT_STATUS_INVALID_PARAMETER;
6487         }
6488
6489         switch (SVAL(pdata, POSIX_LOCK_TYPE_OFFSET)) {
6490                 case POSIX_LOCK_TYPE_READ:
6491                         lock_type = READ_LOCK;
6492                         break;
6493                 case POSIX_LOCK_TYPE_WRITE:
6494                         /* Return the right POSIX-mappable error code for files opened read-only. */
6495                         if (!fsp->can_write) {
6496                                 return NT_STATUS_INVALID_HANDLE;
6497                         }
6498                         lock_type = WRITE_LOCK;
6499                         break;
6500                 case POSIX_LOCK_TYPE_UNLOCK:
6501                         lock_type = UNLOCK_LOCK;
6502                         break;
6503                 default:
6504                         return NT_STATUS_INVALID_PARAMETER;
6505         }
6506
6507         if (SVAL(pdata,POSIX_LOCK_FLAGS_OFFSET) == POSIX_LOCK_FLAG_NOWAIT) {
6508                 blocking_lock = False;
6509         } else if (SVAL(pdata,POSIX_LOCK_FLAGS_OFFSET) == POSIX_LOCK_FLAG_WAIT) {
6510                 blocking_lock = True;
6511         } else {
6512                 return NT_STATUS_INVALID_PARAMETER;
6513         }
6514
6515         if (!lp_blocking_locks(SNUM(conn))) { 
6516                 blocking_lock = False;
6517         }
6518
6519         smblctx = (uint64_t)IVAL(pdata, POSIX_LOCK_PID_OFFSET);
6520 #if defined(HAVE_LONGLONG)
6521         offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
6522                         ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
6523         count = (((uint64_t) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
6524                         ((uint64_t) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
6525 #else /* HAVE_LONGLONG */
6526         offset = (uint64_t)IVAL(pdata,POSIX_LOCK_START_OFFSET);
6527         count = (uint64_t)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
6528 #endif /* HAVE_LONGLONG */
6529
6530         DEBUG(10,("smb_set_posix_lock: file %s, lock_type = %u,"
6531                         "smblctx = %llu, count = %.0f, offset = %.0f\n",
6532                 fsp_str_dbg(fsp),
6533                 (unsigned int)lock_type,
6534                 (unsigned long long)smblctx,
6535                 (double)count,
6536                 (double)offset ));
6537
6538         if (lock_type == UNLOCK_LOCK) {
6539                 status = do_unlock(req->sconn->msg_ctx,
6540                                 fsp,
6541                                 smblctx,
6542                                 count,
6543                                 offset,
6544                                 POSIX_LOCK);
6545         } else {
6546                 uint64_t block_smblctx;
6547
6548                 struct byte_range_lock *br_lck = do_lock(req->sconn->msg_ctx,
6549                                                         fsp,
6550                                                         smblctx,
6551                                                         count,
6552                                                         offset,
6553                                                         lock_type,
6554                                                         POSIX_LOCK,
6555                                                         blocking_lock,
6556                                                         &status,
6557                                                         &block_smblctx,
6558                                                         NULL);
6559
6560                 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
6561                         /*
6562                          * A blocking lock was requested. Package up
6563                          * this smb into a queued request and push it
6564                          * onto the blocking lock queue.
6565                          */
6566                         if(push_blocking_lock_request(br_lck,
6567                                                 req,
6568                                                 fsp,
6569                                                 -1, /* infinite timeout. */
6570                                                 0,
6571                                                 smblctx,
6572                                                 lock_type,
6573                                                 POSIX_LOCK,
6574                                                 offset,
6575                                                 count,
6576                                                 block_smblctx)) {
6577                                 TALLOC_FREE(br_lck);
6578                                 return status;
6579                         }
6580                 }
6581                 TALLOC_FREE(br_lck);
6582         }
6583
6584         return status;
6585 }
6586
6587 /****************************************************************************
6588  Deal with SMB_SET_FILE_BASIC_INFO.
6589 ****************************************************************************/
6590
6591 static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
6592                                         const char *pdata,
6593                                         int total_data,
6594                                         files_struct *fsp,
6595                                         const struct smb_filename *smb_fname)
6596 {
6597         /* Patch to do this correctly from Paul Eggert <eggert@twinsun.com>. */
6598         struct smb_file_time ft;
6599         uint32 dosmode = 0;
6600         NTSTATUS status = NT_STATUS_OK;
6601
6602         ZERO_STRUCT(ft);
6603
6604         if (total_data < 36) {
6605                 return NT_STATUS_INVALID_PARAMETER;
6606         }
6607
6608         status = check_access(conn, fsp, smb_fname, FILE_WRITE_ATTRIBUTES);
6609         if (!NT_STATUS_IS_OK(status)) {
6610                 return status;
6611         }
6612
6613         /* Set the attributes */
6614         dosmode = IVAL(pdata,32);
6615         status = smb_set_file_dosmode(conn, smb_fname, dosmode);
6616         if (!NT_STATUS_IS_OK(status)) {
6617                 return status;
6618         }
6619
6620         /* create time */
6621         ft.create_time = interpret_long_date(pdata);
6622
6623         /* access time */
6624         ft.atime = interpret_long_date(pdata+8);
6625
6626         /* write time. */
6627         ft.mtime = interpret_long_date(pdata+16);
6628
6629         /* change time. */
6630         ft.ctime = interpret_long_date(pdata+24);
6631
6632         DEBUG(10, ("smb_set_file_basic_info: file %s\n",
6633                    smb_fname_str_dbg(smb_fname)));
6634
6635         return smb_set_file_time(conn, fsp, smb_fname, &ft,
6636                                  true);
6637 }
6638
6639 /****************************************************************************
6640  Deal with SMB_INFO_STANDARD.
6641 ****************************************************************************/
6642
6643 static NTSTATUS smb_set_info_standard(connection_struct *conn,
6644                                         const char *pdata,
6645                                         int total_data,
6646                                         files_struct *fsp,
6647                                         const struct smb_filename *smb_fname)
6648 {
6649         NTSTATUS status;
6650         struct smb_file_time ft;
6651
6652         ZERO_STRUCT(ft);
6653
6654         if (total_data < 12) {
6655                 return NT_STATUS_INVALID_PARAMETER;
6656         }
6657
6658         /* create time */
6659         ft.create_time = convert_time_t_to_timespec(srv_make_unix_date2(pdata));
6660         /* access time */
6661         ft.atime = convert_time_t_to_timespec(srv_make_unix_date2(pdata+4));
6662         /* write time */
6663         ft.mtime = convert_time_t_to_timespec(srv_make_unix_date2(pdata+8));
6664
6665         DEBUG(10,("smb_set_info_standard: file %s\n",
6666                 smb_fname_str_dbg(smb_fname)));
6667
6668         status = check_access(conn, fsp, smb_fname, FILE_WRITE_ATTRIBUTES);
6669         if (!NT_STATUS_IS_OK(status)) {
6670                 return status;
6671         }
6672
6673         return smb_set_file_time(conn,
6674                                 fsp,
6675                                 smb_fname,
6676                                 &ft,
6677                                 true);
6678 }
6679
6680 /****************************************************************************
6681  Deal with SMB_SET_FILE_ALLOCATION_INFO.
6682 ****************************************************************************/
6683
6684 static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
6685                                              struct smb_request *req,
6686                                         const char *pdata,
6687                                         int total_data,
6688                                         files_struct *fsp,
6689                                         struct smb_filename *smb_fname)
6690 {
6691         uint64_t allocation_size = 0;
6692         NTSTATUS status = NT_STATUS_OK;
6693         files_struct *new_fsp = NULL;
6694
6695         if (!VALID_STAT(smb_fname->st)) {
6696                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
6697         }
6698
6699         if (total_data < 8) {
6700                 return NT_STATUS_INVALID_PARAMETER;
6701         }
6702
6703         allocation_size = (uint64_t)IVAL(pdata,0);
6704         allocation_size |= (((uint64_t)IVAL(pdata,4)) << 32);
6705         DEBUG(10,("smb_set_file_allocation_info: Set file allocation info for "
6706                   "file %s to %.0f\n", smb_fname_str_dbg(smb_fname),
6707                   (double)allocation_size));
6708
6709         if (allocation_size) {
6710                 allocation_size = smb_roundup(conn, allocation_size);
6711         }
6712
6713         DEBUG(10,("smb_set_file_allocation_info: file %s : setting new "
6714                   "allocation size to %.0f\n", smb_fname_str_dbg(smb_fname),
6715                   (double)allocation_size));
6716
6717         if (fsp && fsp->fh->fd != -1) {
6718                 /* Open file handle. */
6719                 if (!(fsp->access_mask & FILE_WRITE_DATA)) {
6720                         return NT_STATUS_ACCESS_DENIED;
6721                 }
6722
6723                 /* Only change if needed. */
6724                 if (allocation_size != get_file_size_stat(&smb_fname->st)) {
6725                         if (vfs_allocate_file_space(fsp, allocation_size) == -1) {
6726                                 return map_nt_error_from_unix(errno);
6727                         }
6728                 }
6729                 /* But always update the time. */
6730                 /*
6731                  * This is equivalent to a write. Ensure it's seen immediately
6732                  * if there are no pending writes.
6733                  */
6734                 trigger_write_time_update_immediate(fsp);
6735                 return NT_STATUS_OK;
6736         }
6737
6738         /* Pathname or stat or directory file. */
6739         status = SMB_VFS_CREATE_FILE(
6740                 conn,                                   /* conn */
6741                 req,                                    /* req */
6742                 0,                                      /* root_dir_fid */
6743                 smb_fname,                              /* fname */
6744                 FILE_WRITE_DATA,                        /* access_mask */
6745                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
6746                     FILE_SHARE_DELETE),
6747                 FILE_OPEN,                              /* create_disposition*/
6748                 0,                                      /* create_options */
6749                 FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
6750                 FORCE_OPLOCK_BREAK_TO_NONE,             /* oplock_request */
6751                 0,                                      /* allocation_size */
6752                 0,                                      /* private_flags */
6753                 NULL,                                   /* sd */
6754                 NULL,                                   /* ea_list */
6755                 &new_fsp,                               /* result */
6756                 NULL);                                  /* pinfo */
6757
6758         if (!NT_STATUS_IS_OK(status)) {
6759                 /* NB. We check for open_was_deferred in the caller. */
6760                 return status;
6761         }
6762
6763         /* Only change if needed. */
6764         if (allocation_size != get_file_size_stat(&smb_fname->st)) {
6765                 if (vfs_allocate_file_space(new_fsp, allocation_size) == -1) {
6766                         status = map_nt_error_from_unix(errno);
6767                         close_file(req, new_fsp, NORMAL_CLOSE);
6768                         return status;
6769                 }
6770         }
6771
6772         /* Changing the allocation size should set the last mod time. */
6773         /*
6774          * This is equivalent to a write. Ensure it's seen immediately
6775          * if there are no pending writes.
6776          */
6777         trigger_write_time_update_immediate(new_fsp);
6778
6779         close_file(req, new_fsp, NORMAL_CLOSE);
6780         return NT_STATUS_OK;
6781 }
6782
6783 /****************************************************************************
6784  Deal with SMB_SET_FILE_END_OF_FILE_INFO.
6785 ****************************************************************************/
6786
6787 static NTSTATUS smb_set_file_end_of_file_info(connection_struct *conn,
6788                                               struct smb_request *req,
6789                                         const char *pdata,
6790                                         int total_data,
6791                                         files_struct *fsp,
6792                                         const struct smb_filename *smb_fname,
6793                                         bool fail_after_createfile)
6794 {
6795         off_t size;
6796
6797         if (total_data < 8) {
6798                 return NT_STATUS_INVALID_PARAMETER;
6799         }
6800
6801         size = IVAL(pdata,0);
6802         size |= (((off_t)IVAL(pdata,4)) << 32);
6803         DEBUG(10,("smb_set_file_end_of_file_info: Set end of file info for "
6804                   "file %s to %.0f\n", smb_fname_str_dbg(smb_fname),
6805                   (double)size));
6806
6807         return smb_set_file_size(conn, req,
6808                                 fsp,
6809                                 smb_fname,
6810                                 &smb_fname->st,
6811                                 size,
6812                                 fail_after_createfile);
6813 }
6814
6815 /****************************************************************************
6816  Allow a UNIX info mknod.
6817 ****************************************************************************/
6818
6819 static NTSTATUS smb_unix_mknod(connection_struct *conn,
6820                                         const char *pdata,
6821                                         int total_data,
6822                                         const struct smb_filename *smb_fname)
6823 {
6824         uint32 file_type = IVAL(pdata,56);
6825 #if defined(HAVE_MAKEDEV)
6826         uint32 dev_major = IVAL(pdata,60);
6827         uint32 dev_minor = IVAL(pdata,68);
6828 #endif
6829         SMB_DEV_T dev = (SMB_DEV_T)0;
6830         uint32 raw_unixmode = IVAL(pdata,84);
6831         NTSTATUS status;
6832         mode_t unixmode;
6833
6834         if (total_data < 100) {
6835                 return NT_STATUS_INVALID_PARAMETER;
6836         }
6837
6838         status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
6839                                       PERM_NEW_FILE, &unixmode);
6840         if (!NT_STATUS_IS_OK(status)) {
6841                 return status;
6842         }
6843
6844 #if defined(HAVE_MAKEDEV)
6845         dev = makedev(dev_major, dev_minor);
6846 #endif
6847
6848         switch (file_type) {
6849 #if defined(S_IFIFO)
6850                 case UNIX_TYPE_FIFO:
6851                         unixmode |= S_IFIFO;
6852                         break;
6853 #endif
6854 #if defined(S_IFSOCK)
6855                 case UNIX_TYPE_SOCKET:
6856                         unixmode |= S_IFSOCK;
6857                         break;
6858 #endif
6859 #if defined(S_IFCHR)
6860                 case UNIX_TYPE_CHARDEV:
6861                         unixmode |= S_IFCHR;
6862                         break;
6863 #endif
6864 #if defined(S_IFBLK)
6865                 case UNIX_TYPE_BLKDEV:
6866                         unixmode |= S_IFBLK;
6867                         break;
6868 #endif
6869                 default:
6870                         return NT_STATUS_INVALID_PARAMETER;
6871         }
6872
6873         DEBUG(10,("smb_unix_mknod: SMB_SET_FILE_UNIX_BASIC doing mknod dev "
6874                   "%.0f mode 0%o for file %s\n", (double)dev,
6875                   (unsigned int)unixmode, smb_fname_str_dbg(smb_fname)));
6876
6877         /* Ok - do the mknod. */
6878         if (SMB_VFS_MKNOD(conn, smb_fname->base_name, unixmode, dev) != 0) {
6879                 return map_nt_error_from_unix(errno);
6880         }
6881
6882         /* If any of the other "set" calls fail we
6883          * don't want to end up with a half-constructed mknod.
6884          */
6885
6886         if (lp_inherit_perms(SNUM(conn))) {
6887                 char *parent;
6888                 if (!parent_dirname(talloc_tos(), smb_fname->base_name,
6889                                     &parent, NULL)) {
6890                         return NT_STATUS_NO_MEMORY;
6891                 }
6892                 inherit_access_posix_acl(conn, parent, smb_fname->base_name,
6893                                          unixmode);
6894                 TALLOC_FREE(parent);
6895         }
6896
6897         return NT_STATUS_OK;
6898 }
6899
6900 /****************************************************************************
6901  Deal with SMB_SET_FILE_UNIX_BASIC.
6902 ****************************************************************************/
6903
6904 static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
6905                                         struct smb_request *req,
6906                                         const char *pdata,
6907                                         int total_data,
6908                                         files_struct *fsp,
6909                                         const struct smb_filename *smb_fname)
6910 {
6911         struct smb_file_time ft;
6912         uint32 raw_unixmode;
6913         mode_t unixmode;
6914         off_t size = 0;
6915         uid_t set_owner = (uid_t)SMB_UID_NO_CHANGE;
6916         gid_t set_grp = (uid_t)SMB_GID_NO_CHANGE;
6917         NTSTATUS status = NT_STATUS_OK;
6918         bool delete_on_fail = False;
6919         enum perm_type ptype;
6920         files_struct *all_fsps = NULL;
6921         bool modify_mtime = true;
6922         struct file_id id;
6923         struct smb_filename *smb_fname_tmp = NULL;
6924         SMB_STRUCT_STAT sbuf;
6925
6926         ZERO_STRUCT(ft);
6927
6928         if (total_data < 100) {
6929                 return NT_STATUS_INVALID_PARAMETER;
6930         }
6931
6932         if(IVAL(pdata, 0) != SMB_SIZE_NO_CHANGE_LO &&
6933            IVAL(pdata, 4) != SMB_SIZE_NO_CHANGE_HI) {
6934                 size=IVAL(pdata,0); /* first 8 Bytes are size */
6935                 size |= (((off_t)IVAL(pdata,4)) << 32);
6936         }
6937
6938         ft.atime = interpret_long_date(pdata+24); /* access_time */
6939         ft.mtime = interpret_long_date(pdata+32); /* modification_time */
6940         set_owner = (uid_t)IVAL(pdata,40);
6941         set_grp = (gid_t)IVAL(pdata,48);
6942         raw_unixmode = IVAL(pdata,84);
6943
6944         if (VALID_STAT(smb_fname->st)) {
6945                 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
6946                         ptype = PERM_EXISTING_DIR;
6947                 } else {
6948                         ptype = PERM_EXISTING_FILE;
6949                 }
6950         } else {
6951                 ptype = PERM_NEW_FILE;
6952         }
6953
6954         status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
6955                                       ptype, &unixmode);
6956         if (!NT_STATUS_IS_OK(status)) {
6957                 return status;
6958         }
6959
6960         DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC: name = "
6961                   "%s size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
6962                   smb_fname_str_dbg(smb_fname), (double)size,
6963                   (unsigned int)set_owner, (unsigned int)set_grp,
6964                   (int)raw_unixmode));
6965
6966         sbuf = smb_fname->st;
6967
6968         if (!VALID_STAT(sbuf)) {
6969                 /*
6970                  * The only valid use of this is to create character and block
6971                  * devices, and named pipes. This is deprecated (IMHO) and 
6972                  * a new info level should be used for mknod. JRA.
6973                  */
6974
6975                 status = smb_unix_mknod(conn,
6976                                         pdata,
6977                                         total_data,
6978                                         smb_fname);
6979                 if (!NT_STATUS_IS_OK(status)) {
6980                         return status;
6981                 }
6982
6983                 status = copy_smb_filename(talloc_tos(), smb_fname,
6984                                            &smb_fname_tmp);
6985                 if (!NT_STATUS_IS_OK(status)) {
6986                         return status;
6987                 }
6988
6989                 if (SMB_VFS_STAT(conn, smb_fname_tmp) != 0) {
6990                         status = map_nt_error_from_unix(errno);
6991                         TALLOC_FREE(smb_fname_tmp);
6992                         SMB_VFS_UNLINK(conn, smb_fname);
6993                         return status;
6994                 }
6995
6996                 sbuf = smb_fname_tmp->st;
6997                 smb_fname = smb_fname_tmp;
6998
6999                 /* Ensure we don't try and change anything else. */
7000                 raw_unixmode = SMB_MODE_NO_CHANGE;
7001                 size = get_file_size_stat(&sbuf);
7002                 ft.atime = sbuf.st_ex_atime;
7003                 ft.mtime = sbuf.st_ex_mtime;
7004                 /* 
7005                  * We continue here as we might want to change the 
7006                  * owner uid/gid.
7007                  */
7008                 delete_on_fail = True;
7009         }
7010
7011 #if 1
7012         /* Horrible backwards compatibility hack as an old server bug
7013          * allowed a CIFS client bug to remain unnoticed :-(. JRA.
7014          * */
7015
7016         if (!size) {
7017                 size = get_file_size_stat(&sbuf);
7018         }
7019 #endif
7020
7021         /*
7022          * Deal with the UNIX specific mode set.
7023          */
7024
7025         if (raw_unixmode != SMB_MODE_NO_CHANGE) {
7026                 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
7027                           "setting mode 0%o for file %s\n",
7028                           (unsigned int)unixmode,
7029                           smb_fname_str_dbg(smb_fname)));
7030                 if (SMB_VFS_CHMOD(conn, smb_fname->base_name, unixmode) != 0) {
7031                         return map_nt_error_from_unix(errno);
7032                 }
7033         }
7034
7035         /*
7036          * Deal with the UNIX specific uid set.
7037          */
7038
7039         if ((set_owner != (uid_t)SMB_UID_NO_CHANGE) &&
7040             (sbuf.st_ex_uid != set_owner)) {
7041                 int ret;
7042
7043                 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
7044                           "changing owner %u for path %s\n",
7045                           (unsigned int)set_owner,
7046                           smb_fname_str_dbg(smb_fname)));
7047
7048                 if (S_ISLNK(sbuf.st_ex_mode)) {
7049                         ret = SMB_VFS_LCHOWN(conn, smb_fname->base_name,
7050                                              set_owner, (gid_t)-1);
7051                 } else {
7052                         ret = SMB_VFS_CHOWN(conn, smb_fname->base_name,
7053                                             set_owner, (gid_t)-1);
7054                 }
7055
7056                 if (ret != 0) {
7057                         status = map_nt_error_from_unix(errno);
7058                         if (delete_on_fail) {
7059                                 SMB_VFS_UNLINK(conn, smb_fname);
7060                         }
7061                         return status;
7062                 }
7063         }
7064
7065         /*
7066          * Deal with the UNIX specific gid set.
7067          */
7068
7069         if ((set_grp != (uid_t)SMB_GID_NO_CHANGE) &&
7070             (sbuf.st_ex_gid != set_grp)) {
7071                 DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
7072                           "changing group %u for file %s\n",
7073                           (unsigned int)set_owner,
7074                           smb_fname_str_dbg(smb_fname)));
7075                 if (SMB_VFS_CHOWN(conn, smb_fname->base_name, (uid_t)-1,
7076                                   set_grp) != 0) {
7077                         status = map_nt_error_from_unix(errno);
7078                         if (delete_on_fail) {
7079                                 SMB_VFS_UNLINK(conn, smb_fname);
7080                         }
7081                         return status;
7082                 }
7083         }
7084
7085         /* Deal with any size changes. */
7086
7087         status = smb_set_file_size(conn, req,
7088                                    fsp,
7089                                    smb_fname,
7090                                    &sbuf,
7091                                    size,
7092                                    false);
7093         if (!NT_STATUS_IS_OK(status)) {
7094                 return status;
7095         }
7096
7097         /* Deal with any time changes. */
7098         if (null_timespec(ft.mtime) && null_timespec(ft.atime)) {
7099                 /* No change, don't cancel anything. */
7100                 return status;
7101         }
7102
7103         id = vfs_file_id_from_sbuf(conn, &sbuf);
7104         for(all_fsps = file_find_di_first(conn->sconn, id); all_fsps;
7105                         all_fsps = file_find_di_next(all_fsps)) {
7106                 /*
7107                  * We're setting the time explicitly for UNIX.
7108                  * Cancel any pending changes over all handles.
7109                  */
7110                 all_fsps->update_write_time_on_close = false;
7111                 TALLOC_FREE(all_fsps->update_write_time_event);
7112         }
7113
7114         /*
7115          * Override the "setting_write_time"
7116          * parameter here as it almost does what
7117          * we need. Just remember if we modified
7118          * mtime and send the notify ourselves.
7119          */
7120         if (null_timespec(ft.mtime)) {
7121                 modify_mtime = false;
7122         }
7123
7124         status = smb_set_file_time(conn,
7125                                 fsp,
7126                                 smb_fname,
7127                                 &ft,
7128                                 false);
7129         if (modify_mtime) {
7130                 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
7131                         FILE_NOTIFY_CHANGE_LAST_WRITE, smb_fname->base_name);
7132         }
7133         return status;
7134 }
7135
7136 /****************************************************************************
7137  Deal with SMB_SET_FILE_UNIX_INFO2.
7138 ****************************************************************************/
7139
7140 static NTSTATUS smb_set_file_unix_info2(connection_struct *conn,
7141                                         struct smb_request *req,
7142                                         const char *pdata,
7143                                         int total_data,
7144                                         files_struct *fsp,
7145                                         const struct smb_filename *smb_fname)
7146 {
7147         NTSTATUS status;
7148         uint32 smb_fflags;
7149         uint32 smb_fmask;
7150
7151         if (total_data < 116) {
7152                 return NT_STATUS_INVALID_PARAMETER;
7153         }
7154
7155         /* Start by setting all the fields that are common between UNIX_BASIC
7156          * and UNIX_INFO2.
7157          */
7158         status = smb_set_file_unix_basic(conn, req, pdata, total_data,
7159                                          fsp, smb_fname);
7160         if (!NT_STATUS_IS_OK(status)) {
7161                 return status;
7162         }
7163
7164         smb_fflags = IVAL(pdata, 108);
7165         smb_fmask = IVAL(pdata, 112);
7166
7167         /* NB: We should only attempt to alter the file flags if the client
7168          * sends a non-zero mask.
7169          */
7170         if (smb_fmask != 0) {
7171                 int stat_fflags = 0;
7172
7173                 if (!map_info2_flags_to_sbuf(&smb_fname->st, smb_fflags,
7174                                              smb_fmask, &stat_fflags)) {
7175                         /* Client asked to alter a flag we don't understand. */
7176                         return NT_STATUS_INVALID_PARAMETER;
7177                 }
7178
7179                 if (fsp && fsp->fh->fd != -1) {
7180                         /* XXX: we should be  using SMB_VFS_FCHFLAGS here. */
7181                         return NT_STATUS_NOT_SUPPORTED;
7182                 } else {
7183                         if (SMB_VFS_CHFLAGS(conn, smb_fname->base_name,
7184                                             stat_fflags) != 0) {
7185                                 return map_nt_error_from_unix(errno);
7186                         }
7187                 }
7188         }
7189
7190         /* XXX: need to add support for changing the create_time here. You
7191          * can do this for paths on Darwin with setattrlist(2). The right way
7192          * to hook this up is probably by extending the VFS utimes interface.
7193          */
7194
7195         return NT_STATUS_OK;
7196 }
7197
7198 /****************************************************************************
7199  Create a directory with POSIX semantics.
7200 ****************************************************************************/
7201
7202 static NTSTATUS smb_posix_mkdir(connection_struct *conn,
7203                                 struct smb_request *req,
7204                                 char **ppdata,
7205                                 int total_data,
7206                                 struct smb_filename *smb_fname,
7207                                 int *pdata_return_size)
7208 {
7209         NTSTATUS status = NT_STATUS_OK;
7210         uint32 raw_unixmode = 0;
7211         uint32 mod_unixmode = 0;
7212         mode_t unixmode = (mode_t)0;
7213         files_struct *fsp = NULL;
7214         uint16 info_level_return = 0;
7215         int info;
7216         char *pdata = *ppdata;
7217
7218         if (total_data < 18) {
7219                 return NT_STATUS_INVALID_PARAMETER;
7220         }
7221
7222         raw_unixmode = IVAL(pdata,8);
7223         /* Next 4 bytes are not yet defined. */
7224
7225         status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
7226                                       PERM_NEW_DIR, &unixmode);
7227         if (!NT_STATUS_IS_OK(status)) {
7228                 return status;
7229         }
7230
7231         mod_unixmode = (uint32)unixmode | FILE_FLAG_POSIX_SEMANTICS;
7232
7233         DEBUG(10,("smb_posix_mkdir: file %s, mode 0%o\n",
7234                   smb_fname_str_dbg(smb_fname), (unsigned int)unixmode));
7235
7236         status = SMB_VFS_CREATE_FILE(
7237                 conn,                                   /* conn */
7238                 req,                                    /* req */
7239                 0,                                      /* root_dir_fid */
7240                 smb_fname,                              /* fname */
7241                 FILE_READ_ATTRIBUTES,                   /* access_mask */
7242                 FILE_SHARE_NONE,                        /* share_access */
7243                 FILE_CREATE,                            /* create_disposition*/
7244                 FILE_DIRECTORY_FILE,                    /* create_options */
7245                 mod_unixmode,                           /* file_attributes */
7246                 0,                                      /* oplock_request */
7247                 0,                                      /* allocation_size */
7248                 0,                                      /* private_flags */
7249                 NULL,                                   /* sd */
7250                 NULL,                                   /* ea_list */
7251                 &fsp,                                   /* result */
7252                 &info);                                 /* pinfo */
7253
7254         if (NT_STATUS_IS_OK(status)) {
7255                 close_file(req, fsp, NORMAL_CLOSE);
7256         }
7257
7258         info_level_return = SVAL(pdata,16);
7259  
7260         if (info_level_return == SMB_QUERY_FILE_UNIX_BASIC) {
7261                 *pdata_return_size = 12 + SMB_FILE_UNIX_BASIC_SIZE;
7262         } else if (info_level_return ==  SMB_QUERY_FILE_UNIX_INFO2) {
7263                 *pdata_return_size = 12 + SMB_FILE_UNIX_INFO2_SIZE;
7264         } else {
7265                 *pdata_return_size = 12;
7266         }
7267
7268         /* Realloc the data size */
7269         *ppdata = (char *)SMB_REALLOC(*ppdata,*pdata_return_size);
7270         if (*ppdata == NULL) {
7271                 *pdata_return_size = 0;
7272                 return NT_STATUS_NO_MEMORY;
7273         }
7274         pdata = *ppdata;
7275
7276         SSVAL(pdata,0,NO_OPLOCK_RETURN);
7277         SSVAL(pdata,2,0); /* No fnum. */
7278         SIVAL(pdata,4,info); /* Was directory created. */
7279
7280         switch (info_level_return) {
7281                 case SMB_QUERY_FILE_UNIX_BASIC:
7282                         SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_BASIC);
7283                         SSVAL(pdata,10,0); /* Padding. */
7284                         store_file_unix_basic(conn, pdata + 12, fsp,
7285                                               &smb_fname->st);
7286                         break;
7287                 case SMB_QUERY_FILE_UNIX_INFO2:
7288                         SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_INFO2);
7289                         SSVAL(pdata,10,0); /* Padding. */
7290                         store_file_unix_basic_info2(conn, pdata + 12, fsp,
7291                                                     &smb_fname->st);
7292                         break;
7293                 default:
7294                         SSVAL(pdata,8,SMB_NO_INFO_LEVEL_RETURNED);
7295                         SSVAL(pdata,10,0); /* Padding. */
7296                         break;
7297         }
7298
7299         return status;
7300 }
7301
7302 /****************************************************************************
7303  Open/Create a file with POSIX semantics.
7304 ****************************************************************************/
7305
7306 #define SMB_O_RDONLY_MAPPING (FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA)
7307 #define SMB_O_WRONLY_MAPPING (FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA)
7308
7309 static NTSTATUS smb_posix_open(connection_struct *conn,
7310                                struct smb_request *req,
7311                                 char **ppdata,
7312                                 int total_data,
7313                                 struct smb_filename *smb_fname,
7314                                 int *pdata_return_size)
7315 {
7316         bool extended_oplock_granted = False;
7317         char *pdata = *ppdata;
7318         uint32 flags = 0;
7319         uint32 wire_open_mode = 0;
7320         uint32 raw_unixmode = 0;
7321         uint32 mod_unixmode = 0;
7322         uint32 create_disp = 0;
7323         uint32 access_mask = 0;
7324         uint32 create_options = FILE_NON_DIRECTORY_FILE;
7325         NTSTATUS status = NT_STATUS_OK;
7326         mode_t unixmode = (mode_t)0;
7327         files_struct *fsp = NULL;
7328         int oplock_request = 0;
7329         int info = 0;
7330         uint16 info_level_return = 0;
7331
7332         if (total_data < 18) {
7333                 return NT_STATUS_INVALID_PARAMETER;
7334         }
7335
7336         flags = IVAL(pdata,0);
7337         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
7338         if (oplock_request) {
7339                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
7340         }
7341
7342         wire_open_mode = IVAL(pdata,4);
7343
7344         if (wire_open_mode == (SMB_O_CREAT|SMB_O_DIRECTORY)) {
7345                 return smb_posix_mkdir(conn, req,
7346                                         ppdata,
7347                                         total_data,
7348                                         smb_fname,
7349                                         pdata_return_size);
7350         }
7351
7352         switch (wire_open_mode & SMB_ACCMODE) {
7353                 case SMB_O_RDONLY:
7354                         access_mask = SMB_O_RDONLY_MAPPING;
7355                         break;
7356                 case SMB_O_WRONLY:
7357                         access_mask = SMB_O_WRONLY_MAPPING;
7358                         break;
7359                 case SMB_O_RDWR:
7360                         access_mask = (SMB_O_RDONLY_MAPPING|
7361                                         SMB_O_WRONLY_MAPPING);
7362                         break;
7363                 default:
7364                         DEBUG(5,("smb_posix_open: invalid open mode 0x%x\n",
7365                                 (unsigned int)wire_open_mode ));
7366                         return NT_STATUS_INVALID_PARAMETER;
7367         }
7368
7369         wire_open_mode &= ~SMB_ACCMODE;
7370
7371         /* First take care of O_CREAT|O_EXCL interactions. */
7372         switch (wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL)) {
7373                 case (SMB_O_CREAT | SMB_O_EXCL):
7374                         /* File exists fail. File not exist create. */
7375                         create_disp = FILE_CREATE;
7376                         break;
7377                 case SMB_O_CREAT:
7378                         /* File exists open. File not exist create. */
7379                         create_disp = FILE_OPEN_IF;
7380                         break;
7381                 case 0:
7382                         /* File exists open. File not exist fail. */
7383                         create_disp = FILE_OPEN;
7384                         break;
7385                 case SMB_O_EXCL:
7386                         /* O_EXCL on its own without O_CREAT is undefined. */
7387                 default:
7388                         DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
7389                                 (unsigned int)wire_open_mode ));
7390                         return NT_STATUS_INVALID_PARAMETER;
7391         }
7392
7393         /* Next factor in the effects of O_TRUNC. */
7394         wire_open_mode &= ~(SMB_O_CREAT | SMB_O_EXCL);
7395
7396         if (wire_open_mode & SMB_O_TRUNC) {
7397                 switch (create_disp) {
7398                         case FILE_CREATE:
7399                                 /* (SMB_O_CREAT | SMB_O_EXCL | O_TRUNC) */
7400                                 /* Leave create_disp alone as
7401                                    (O_CREAT|O_EXCL|O_TRUNC) == (O_CREAT|O_EXCL)
7402                                 */
7403                                 /* File exists fail. File not exist create. */
7404                                 break;
7405                         case FILE_OPEN_IF:
7406                                 /* SMB_O_CREAT | SMB_O_TRUNC */
7407                                 /* File exists overwrite. File not exist create. */
7408                                 create_disp = FILE_OVERWRITE_IF;
7409                                 break;
7410                         case FILE_OPEN:
7411                                 /* SMB_O_TRUNC */
7412                                 /* File exists overwrite. File not exist fail. */
7413                                 create_disp = FILE_OVERWRITE;
7414                                 break;
7415                         default:
7416                                 /* Cannot get here. */
7417                                 smb_panic("smb_posix_open: logic error");
7418                                 return NT_STATUS_INVALID_PARAMETER;
7419                 }
7420         }
7421
7422         raw_unixmode = IVAL(pdata,8);
7423         /* Next 4 bytes are not yet defined. */
7424
7425         status = unix_perms_from_wire(conn, &smb_fname->st, raw_unixmode,
7426                                       (VALID_STAT(smb_fname->st) ?
7427                                           PERM_EXISTING_FILE : PERM_NEW_FILE),
7428                                       &unixmode);
7429
7430         if (!NT_STATUS_IS_OK(status)) {
7431                 return status;
7432         }
7433
7434         mod_unixmode = (uint32)unixmode | FILE_FLAG_POSIX_SEMANTICS;
7435
7436         if (wire_open_mode & SMB_O_SYNC) {
7437                 create_options |= FILE_WRITE_THROUGH;
7438         }
7439         if (wire_open_mode & SMB_O_APPEND) {
7440                 access_mask |= FILE_APPEND_DATA;
7441         }
7442         if (wire_open_mode & SMB_O_DIRECT) {
7443                 mod_unixmode |= FILE_FLAG_NO_BUFFERING;
7444         }
7445
7446         if ((wire_open_mode & SMB_O_DIRECTORY) ||
7447                         VALID_STAT_OF_DIR(smb_fname->st)) {
7448                 if (access_mask != SMB_O_RDONLY_MAPPING) {
7449                         return NT_STATUS_FILE_IS_A_DIRECTORY;
7450                 }
7451                 create_options &= ~FILE_NON_DIRECTORY_FILE;
7452                 create_options |= FILE_DIRECTORY_FILE;
7453         }
7454
7455         DEBUG(10,("smb_posix_open: file %s, smb_posix_flags = %u, mode 0%o\n",
7456                 smb_fname_str_dbg(smb_fname),
7457                 (unsigned int)wire_open_mode,
7458                 (unsigned int)unixmode ));
7459
7460         status = SMB_VFS_CREATE_FILE(
7461                 conn,                                   /* conn */
7462                 req,                                    /* req */
7463                 0,                                      /* root_dir_fid */
7464                 smb_fname,                              /* fname */
7465                 access_mask,                            /* access_mask */
7466                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
7467                     FILE_SHARE_DELETE),
7468                 create_disp,                            /* create_disposition*/
7469                 create_options,                         /* create_options */
7470                 mod_unixmode,                           /* file_attributes */
7471                 oplock_request,                         /* oplock_request */
7472                 0,                                      /* allocation_size */
7473                 0,                                      /* private_flags */
7474                 NULL,                                   /* sd */
7475                 NULL,                                   /* ea_list */
7476                 &fsp,                                   /* result */
7477                 &info);                                 /* pinfo */
7478
7479         if (!NT_STATUS_IS_OK(status)) {
7480                 return status;
7481         }
7482
7483         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
7484                 extended_oplock_granted = True;
7485         }
7486
7487         if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
7488                 extended_oplock_granted = True;
7489         }
7490
7491         info_level_return = SVAL(pdata,16);
7492  
7493         /* Allocate the correct return size. */
7494
7495         if (info_level_return == SMB_QUERY_FILE_UNIX_BASIC) {
7496                 *pdata_return_size = 12 + SMB_FILE_UNIX_BASIC_SIZE;
7497         } else if (info_level_return ==  SMB_QUERY_FILE_UNIX_INFO2) {
7498                 *pdata_return_size = 12 + SMB_FILE_UNIX_INFO2_SIZE;
7499         } else {
7500                 *pdata_return_size = 12;
7501         }
7502
7503         /* Realloc the data size */
7504         *ppdata = (char *)SMB_REALLOC(*ppdata,*pdata_return_size);
7505         if (*ppdata == NULL) {
7506                 close_file(req, fsp, ERROR_CLOSE);
7507                 *pdata_return_size = 0;
7508                 return NT_STATUS_NO_MEMORY;
7509         }
7510         pdata = *ppdata;
7511
7512         if (extended_oplock_granted) {
7513                 if (flags & REQUEST_BATCH_OPLOCK) {
7514                         SSVAL(pdata,0, BATCH_OPLOCK_RETURN);
7515                 } else {
7516                         SSVAL(pdata,0, EXCLUSIVE_OPLOCK_RETURN);
7517                 }
7518         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
7519                 SSVAL(pdata,0, LEVEL_II_OPLOCK_RETURN);
7520         } else {
7521                 SSVAL(pdata,0,NO_OPLOCK_RETURN);
7522         }
7523
7524         SSVAL(pdata,2,fsp->fnum);
7525         SIVAL(pdata,4,info); /* Was file created etc. */
7526
7527         switch (info_level_return) {
7528                 case SMB_QUERY_FILE_UNIX_BASIC:
7529                         SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_BASIC);
7530                         SSVAL(pdata,10,0); /* padding. */
7531                         store_file_unix_basic(conn, pdata + 12, fsp,
7532                                               &smb_fname->st);
7533                         break;
7534                 case SMB_QUERY_FILE_UNIX_INFO2:
7535                         SSVAL(pdata,8,SMB_QUERY_FILE_UNIX_INFO2);
7536                         SSVAL(pdata,10,0); /* padding. */
7537                         store_file_unix_basic_info2(conn, pdata + 12, fsp,
7538                                                     &smb_fname->st);
7539                         break;
7540                 default:
7541                         SSVAL(pdata,8,SMB_NO_INFO_LEVEL_RETURNED);
7542                         SSVAL(pdata,10,0); /* padding. */
7543                         break;
7544         }
7545         return NT_STATUS_OK;
7546 }
7547
7548 /****************************************************************************
7549  Delete a file with POSIX semantics.
7550 ****************************************************************************/
7551
7552 static NTSTATUS smb_posix_unlink(connection_struct *conn,
7553                                  struct smb_request *req,
7554                                 const char *pdata,
7555                                 int total_data,
7556                                 struct smb_filename *smb_fname)
7557 {
7558         NTSTATUS status = NT_STATUS_OK;
7559         files_struct *fsp = NULL;
7560         uint16 flags = 0;
7561         char del = 1;
7562         int info = 0;
7563         int create_options = 0;
7564         int i;
7565         struct share_mode_lock *lck = NULL;
7566
7567         if (total_data < 2) {
7568                 return NT_STATUS_INVALID_PARAMETER;
7569         }
7570
7571         flags = SVAL(pdata,0);
7572
7573         if (!VALID_STAT(smb_fname->st)) {
7574                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
7575         }
7576
7577         if ((flags == SMB_POSIX_UNLINK_DIRECTORY_TARGET) &&
7578                         !VALID_STAT_OF_DIR(smb_fname->st)) {
7579                 return NT_STATUS_NOT_A_DIRECTORY;
7580         }
7581
7582         DEBUG(10,("smb_posix_unlink: %s %s\n",
7583                 (flags == SMB_POSIX_UNLINK_DIRECTORY_TARGET) ? "directory" : "file",
7584                 smb_fname_str_dbg(smb_fname)));
7585
7586         if (VALID_STAT_OF_DIR(smb_fname->st)) {
7587                 create_options |= FILE_DIRECTORY_FILE;
7588         }
7589
7590         status = SMB_VFS_CREATE_FILE(
7591                 conn,                                   /* conn */
7592                 req,                                    /* req */
7593                 0,                                      /* root_dir_fid */
7594                 smb_fname,                              /* fname */
7595                 DELETE_ACCESS,                          /* access_mask */
7596                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
7597                     FILE_SHARE_DELETE),
7598                 FILE_OPEN,                              /* create_disposition*/
7599                 create_options,                         /* create_options */
7600                 FILE_FLAG_POSIX_SEMANTICS|0777,         /* file_attributes */
7601                 0,                                      /* oplock_request */
7602                 0,                                      /* allocation_size */
7603                 0,                                      /* private_flags */
7604                 NULL,                                   /* sd */
7605                 NULL,                                   /* ea_list */
7606                 &fsp,                                   /* result */
7607                 &info);                                 /* pinfo */
7608
7609         if (!NT_STATUS_IS_OK(status)) {
7610                 return status;
7611         }
7612
7613         /*
7614          * Don't lie to client. If we can't really delete due to
7615          * non-POSIX opens return SHARING_VIOLATION.
7616          */
7617
7618         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
7619         if (lck == NULL) {
7620                 DEBUG(0, ("smb_posix_unlink: Could not get share mode "
7621                           "lock for file %s\n", fsp_str_dbg(fsp)));
7622                 close_file(req, fsp, NORMAL_CLOSE);
7623                 return NT_STATUS_INVALID_PARAMETER;
7624         }
7625
7626         /*
7627          * See if others still have the file open. If this is the case, then
7628          * don't delete. If all opens are POSIX delete we can set the delete
7629          * on close disposition.
7630          */
7631         for (i=0; i<lck->data->num_share_modes; i++) {
7632                 struct share_mode_entry *e = &lck->data->share_modes[i];
7633                 if (is_valid_share_mode_entry(e)) {
7634                         if (e->flags & SHARE_MODE_FLAG_POSIX_OPEN) {
7635                                 continue;
7636                         }
7637                         if (share_mode_stale_pid(lck->data, i)) {
7638                                 continue;
7639                         }
7640                         /* Fail with sharing violation. */
7641                         close_file(req, fsp, NORMAL_CLOSE);
7642                         TALLOC_FREE(lck);
7643                         return NT_STATUS_SHARING_VIOLATION;
7644                 }
7645         }
7646
7647         /*
7648          * Set the delete on close.
7649          */
7650         status = smb_set_file_disposition_info(conn,
7651                                                 &del,
7652                                                 1,
7653                                                 fsp,
7654                                                 smb_fname);
7655
7656         if (!NT_STATUS_IS_OK(status)) {
7657                 close_file(req, fsp, NORMAL_CLOSE);
7658                 TALLOC_FREE(lck);
7659                 return status;
7660         }
7661         TALLOC_FREE(lck);
7662         return close_file(req, fsp, NORMAL_CLOSE);
7663 }
7664
7665 NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
7666                                 struct smb_request *req,
7667                                 TALLOC_CTX *mem_ctx,
7668                                 uint16_t info_level,
7669                                 files_struct *fsp,
7670                                 struct smb_filename *smb_fname,
7671                                 char **ppdata, int total_data,
7672                                 int *ret_data_size)
7673 {
7674         char *pdata = *ppdata;
7675         NTSTATUS status = NT_STATUS_OK;
7676         int data_return_size = 0;
7677
7678         *ret_data_size = 0;
7679
7680         if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
7681                 return NT_STATUS_INVALID_LEVEL;
7682         }
7683
7684         if (!CAN_WRITE(conn)) {
7685                 /* Allow POSIX opens. The open path will deny
7686                  * any non-readonly opens. */
7687                 if (info_level != SMB_POSIX_PATH_OPEN) {
7688                         return NT_STATUS_DOS(ERRSRV, ERRaccess);
7689                 }
7690         }
7691
7692         DEBUG(3,("smbd_do_setfilepathinfo: %s (%s) info_level=%d "
7693                  "totdata=%d\n", smb_fname_str_dbg(smb_fname),
7694                  fsp_fnum_dbg(fsp),
7695                  info_level, total_data));
7696
7697         switch (info_level) {
7698
7699                 case SMB_INFO_STANDARD:
7700                 {
7701                         status = smb_set_info_standard(conn,
7702                                         pdata,
7703                                         total_data,
7704                                         fsp,
7705                                         smb_fname);
7706                         break;
7707                 }
7708
7709                 case SMB_INFO_SET_EA:
7710                 {
7711                         status = smb_info_set_ea(conn,
7712                                                 pdata,
7713                                                 total_data,
7714                                                 fsp,
7715                                                 smb_fname);
7716                         break;
7717                 }
7718
7719                 case SMB_SET_FILE_BASIC_INFO:
7720                 case SMB_FILE_BASIC_INFORMATION:
7721                 {
7722                         status = smb_set_file_basic_info(conn,
7723                                                         pdata,
7724                                                         total_data,
7725                                                         fsp,
7726                                                         smb_fname);
7727                         break;
7728                 }
7729
7730                 case SMB_FILE_ALLOCATION_INFORMATION:
7731                 case SMB_SET_FILE_ALLOCATION_INFO:
7732                 {
7733                         status = smb_set_file_allocation_info(conn, req,
7734                                                                 pdata,
7735                                                                 total_data,
7736                                                                 fsp,
7737                                                                 smb_fname);
7738                         break;
7739                 }
7740
7741                 case SMB_FILE_END_OF_FILE_INFORMATION:
7742                 case SMB_SET_FILE_END_OF_FILE_INFO:
7743                 {
7744                         /*
7745                          * XP/Win7 both fail after the createfile with
7746                          * SMB_SET_FILE_END_OF_FILE_INFO but not
7747                          * SMB_FILE_END_OF_FILE_INFORMATION (pass-through).
7748                          * The level is known here, so pass it down
7749                          * appropriately.
7750                          */
7751                         bool should_fail =
7752                             (info_level == SMB_SET_FILE_END_OF_FILE_INFO);
7753
7754                         status = smb_set_file_end_of_file_info(conn, req,
7755                                                                 pdata,
7756                                                                 total_data,
7757                                                                 fsp,
7758                                                                 smb_fname,
7759                                                                 should_fail);
7760                         break;
7761                 }
7762
7763                 case SMB_FILE_DISPOSITION_INFORMATION:
7764                 case SMB_SET_FILE_DISPOSITION_INFO: /* Set delete on close for open file. */
7765                 {
7766 #if 0
7767                         /* JRA - We used to just ignore this on a path ? 
7768                          * Shouldn't this be invalid level on a pathname
7769                          * based call ?
7770                          */
7771                         if (tran_call != TRANSACT2_SETFILEINFO) {
7772                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
7773                         }
7774 #endif
7775                         status = smb_set_file_disposition_info(conn,
7776                                                 pdata,
7777                                                 total_data,
7778                                                 fsp,
7779                                                 smb_fname);
7780                         break;
7781                 }
7782
7783                 case SMB_FILE_POSITION_INFORMATION:
7784                 {
7785                         status = smb_file_position_information(conn,
7786                                                 pdata,
7787                                                 total_data,
7788                                                 fsp);
7789                         break;
7790                 }
7791
7792                 case SMB_FILE_FULL_EA_INFORMATION:
7793                 {
7794                         status = smb_set_file_full_ea_info(conn,
7795                                                 pdata,
7796                                                 total_data,
7797                                                 fsp);
7798                         break;
7799                 }
7800
7801                 /* From tridge Samba4 : 
7802                  * MODE_INFORMATION in setfileinfo (I have no
7803                  * idea what "mode information" on a file is - it takes a value of 0,
7804                  * 2, 4 or 6. What could it be?).
7805                  */
7806
7807                 case SMB_FILE_MODE_INFORMATION:
7808                 {
7809                         status = smb_file_mode_information(conn,
7810                                                 pdata,
7811                                                 total_data);
7812                         break;
7813                 }
7814
7815                 /*
7816                  * CIFS UNIX extensions.
7817                  */
7818
7819                 case SMB_SET_FILE_UNIX_BASIC:
7820                 {
7821                         status = smb_set_file_unix_basic(conn, req,
7822                                                         pdata,
7823                                                         total_data,
7824                                                         fsp,
7825                                                         smb_fname);
7826                         break;
7827                 }
7828
7829                 case SMB_SET_FILE_UNIX_INFO2:
7830                 {
7831                         status = smb_set_file_unix_info2(conn, req,
7832                                                         pdata,
7833                                                         total_data,
7834                                                         fsp,
7835                                                         smb_fname);
7836                         break;
7837                 }
7838
7839                 case SMB_SET_FILE_UNIX_LINK:
7840                 {
7841                         if (fsp) {
7842                                 /* We must have a pathname for this. */
7843                                 return NT_STATUS_INVALID_LEVEL;
7844                         }
7845                         status = smb_set_file_unix_link(conn, req, pdata,
7846                                                         total_data, smb_fname);
7847                         break;
7848                 }
7849
7850                 case SMB_SET_FILE_UNIX_HLINK:
7851                 {
7852                         if (fsp) {
7853                                 /* We must have a pathname for this. */
7854                                 return NT_STATUS_INVALID_LEVEL;
7855                         }
7856                         status = smb_set_file_unix_hlink(conn, req,
7857                                                          pdata, total_data,
7858                                                          smb_fname);
7859                         break;
7860                 }
7861
7862                 case SMB_FILE_RENAME_INFORMATION:
7863                 {
7864                         status = smb_file_rename_information(conn, req,
7865                                                              pdata, total_data,
7866                                                              fsp, smb_fname);
7867                         break;
7868                 }
7869
7870                 case SMB2_FILE_RENAME_INFORMATION_INTERNAL:
7871                 {
7872                         /* SMB2 rename information. */
7873                         status = smb2_file_rename_information(conn, req,
7874                                                              pdata, total_data,
7875                                                              fsp, smb_fname);
7876                         break;
7877                 }
7878
7879                 case SMB_FILE_LINK_INFORMATION:
7880                 {
7881                         status = smb_file_link_information(conn, req,
7882                                                         pdata, total_data,
7883                                                         fsp, smb_fname);
7884                         break;
7885                 }
7886
7887 #if defined(HAVE_POSIX_ACLS)
7888                 case SMB_SET_POSIX_ACL:
7889                 {
7890                         status = smb_set_posix_acl(conn,
7891                                                 pdata,
7892                                                 total_data,
7893                                                 fsp,
7894                                                 smb_fname);
7895                         break;
7896                 }
7897 #endif
7898
7899                 case SMB_SET_POSIX_LOCK:
7900                 {
7901                         if (!fsp) {
7902                                 return NT_STATUS_INVALID_LEVEL;
7903                         }
7904                         status = smb_set_posix_lock(conn, req,
7905                                                     pdata, total_data, fsp);
7906                         break;
7907                 }
7908
7909                 case SMB_POSIX_PATH_OPEN:
7910                 {
7911                         if (fsp) {
7912                                 /* We must have a pathname for this. */
7913                                 return NT_STATUS_INVALID_LEVEL;
7914                         }
7915
7916                         status = smb_posix_open(conn, req,
7917                                                 ppdata,
7918                                                 total_data,
7919                                                 smb_fname,
7920                                                 &data_return_size);
7921                         break;
7922                 }
7923
7924                 case SMB_POSIX_PATH_UNLINK:
7925                 {
7926                         if (fsp) {
7927                                 /* We must have a pathname for this. */
7928                                 return NT_STATUS_INVALID_LEVEL;
7929                         }
7930
7931                         status = smb_posix_unlink(conn, req,
7932                                                 pdata,
7933                                                 total_data,
7934                                                 smb_fname);
7935                         break;
7936                 }
7937
7938                 default:
7939                         return NT_STATUS_INVALID_LEVEL;
7940         }
7941
7942         if (!NT_STATUS_IS_OK(status)) {
7943                 return status;
7944         }
7945
7946         *ret_data_size = data_return_size;
7947         return NT_STATUS_OK;
7948 }
7949
7950 /****************************************************************************
7951  Reply to a TRANS2_SETFILEINFO (set file info by fileid or pathname).
7952 ****************************************************************************/
7953
7954 static void call_trans2setfilepathinfo(connection_struct *conn,
7955                                        struct smb_request *req,
7956                                        unsigned int tran_call,
7957                                        char **pparams, int total_params,
7958                                        char **ppdata, int total_data,
7959                                        unsigned int max_data_bytes)
7960 {
7961         char *params = *pparams;
7962         char *pdata = *ppdata;
7963         uint16 info_level;
7964         struct smb_filename *smb_fname = NULL;
7965         files_struct *fsp = NULL;
7966         NTSTATUS status = NT_STATUS_OK;
7967         int data_return_size = 0;
7968
7969         if (!params) {
7970                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7971                 return;
7972         }
7973
7974         if (tran_call == TRANSACT2_SETFILEINFO) {
7975                 if (total_params < 4) {
7976                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7977                         return;
7978                 }
7979
7980                 fsp = file_fsp(req, SVAL(params,0));
7981                 /* Basic check for non-null fsp. */
7982                 if (!check_fsp_open(conn, req, fsp)) {
7983                         return;
7984                 }
7985                 info_level = SVAL(params,2);
7986
7987                 status = copy_smb_filename(talloc_tos(), fsp->fsp_name,
7988                                            &smb_fname);
7989                 if (!NT_STATUS_IS_OK(status)) {
7990                         reply_nterror(req, status);
7991                         return;
7992                 }
7993
7994                 if(fsp->fh->fd == -1) {
7995                         /*
7996                          * This is actually a SETFILEINFO on a directory
7997                          * handle (returned from an NT SMB). NT5.0 seems
7998                          * to do this call. JRA.
7999                          */
8000                         if (INFO_LEVEL_IS_UNIX(info_level)) {
8001                                 /* Always do lstat for UNIX calls. */
8002                                 if (SMB_VFS_LSTAT(conn, smb_fname)) {
8003                                         DEBUG(3,("call_trans2setfilepathinfo: "
8004                                                  "SMB_VFS_LSTAT of %s failed "
8005                                                  "(%s)\n",
8006                                                  smb_fname_str_dbg(smb_fname),
8007                                                  strerror(errno)));
8008                                         reply_nterror(req, map_nt_error_from_unix(errno));
8009                                         return;
8010                                 }
8011                         } else {
8012                                 if (SMB_VFS_STAT(conn, smb_fname) != 0) {
8013                                         DEBUG(3,("call_trans2setfilepathinfo: "
8014                                                  "fileinfo of %s failed (%s)\n",
8015                                                  smb_fname_str_dbg(smb_fname),
8016                                                  strerror(errno)));
8017                                         reply_nterror(req, map_nt_error_from_unix(errno));
8018                                         return;
8019                                 }
8020                         }
8021                 } else if (fsp->print_file) {
8022                         /*
8023                          * Doing a DELETE_ON_CLOSE should cancel a print job.
8024                          */
8025                         if ((info_level == SMB_SET_FILE_DISPOSITION_INFO) && CVAL(pdata,0)) {
8026                                 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
8027
8028                                 DEBUG(3,("call_trans2setfilepathinfo: "
8029                                          "Cancelling print job (%s)\n",
8030                                          fsp_str_dbg(fsp)));
8031
8032                                 SSVAL(params,0,0);
8033                                 send_trans2_replies(conn, req, params, 2,
8034                                                     *ppdata, 0,
8035                                                     max_data_bytes);
8036                                 return;
8037                         } else {
8038                                 reply_nterror(req,
8039                                         NT_STATUS_OBJECT_PATH_NOT_FOUND);
8040                                 return;
8041                         }
8042                 } else {
8043                         /*
8044                          * Original code - this is an open file.
8045                          */
8046                         if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
8047                                 DEBUG(3,("call_trans2setfilepathinfo: fstat "
8048                                          "of %s failed (%s)\n", fsp_fnum_dbg(fsp),
8049                                          strerror(errno)));
8050                                 reply_nterror(req, map_nt_error_from_unix(errno));
8051                                 return;
8052                         }
8053                 }
8054         } else {
8055                 char *fname = NULL;
8056                 uint32_t ucf_flags = 0;
8057
8058                 /* set path info */
8059                 if (total_params < 7) {
8060                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8061                         return;
8062                 }
8063
8064                 info_level = SVAL(params,0);
8065                 srvstr_get_path(req, params, req->flags2, &fname, &params[6],
8066                                 total_params - 6, STR_TERMINATE,
8067                                 &status);
8068                 if (!NT_STATUS_IS_OK(status)) {
8069                         reply_nterror(req, status);
8070                         return;
8071                 }
8072
8073                 if (info_level == SMB_SET_FILE_UNIX_BASIC ||
8074                                 info_level == SMB_SET_FILE_UNIX_INFO2 ||
8075                                 info_level == SMB_FILE_RENAME_INFORMATION ||
8076                                 info_level == SMB_POSIX_PATH_UNLINK) {
8077                         ucf_flags |= UCF_UNIX_NAME_LOOKUP;
8078                 }
8079
8080                 status = filename_convert(req, conn,
8081                                          req->flags2 & FLAGS2_DFS_PATHNAMES,
8082                                          fname,
8083                                          ucf_flags,
8084                                          NULL,
8085                                          &smb_fname);
8086                 if (!NT_STATUS_IS_OK(status)) {
8087                         if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
8088                                 reply_botherror(req,
8089                                                 NT_STATUS_PATH_NOT_COVERED,
8090                                                 ERRSRV, ERRbadpath);
8091                                 return;
8092                         }
8093                         reply_nterror(req, status);
8094                         return;
8095                 }
8096
8097                 if (INFO_LEVEL_IS_UNIX(info_level)) {
8098                         /*
8099                          * For CIFS UNIX extensions the target name may not exist.
8100                          */
8101
8102                         /* Always do lstat for UNIX calls. */
8103                         SMB_VFS_LSTAT(conn, smb_fname);
8104
8105                 } else if (!VALID_STAT(smb_fname->st) &&
8106                            SMB_VFS_STAT(conn, smb_fname)) {
8107                         DEBUG(3,("call_trans2setfilepathinfo: SMB_VFS_STAT of "
8108                                  "%s failed (%s)\n",
8109                                  smb_fname_str_dbg(smb_fname),
8110                                  strerror(errno)));
8111                         reply_nterror(req, map_nt_error_from_unix(errno));
8112                         return;
8113                 }
8114         }
8115
8116         DEBUG(3,("call_trans2setfilepathinfo(%d) %s (%s) info_level=%d "
8117                  "totdata=%d\n", tran_call, smb_fname_str_dbg(smb_fname),
8118                  fsp_fnum_dbg(fsp),
8119                  info_level,total_data));
8120
8121         /* Realloc the parameter size */
8122         *pparams = (char *)SMB_REALLOC(*pparams,2);
8123         if (*pparams == NULL) {
8124                 reply_nterror(req, NT_STATUS_NO_MEMORY);
8125                 return;
8126         }
8127         params = *pparams;
8128
8129         SSVAL(params,0,0);
8130
8131         status = smbd_do_setfilepathinfo(conn, req, req,
8132                                          info_level,
8133                                          fsp,
8134                                          smb_fname,
8135                                          ppdata, total_data,
8136                                          &data_return_size);
8137         if (!NT_STATUS_IS_OK(status)) {
8138                 if (open_was_deferred(req->sconn, req->mid)) {
8139                         /* We have re-scheduled this call. */
8140                         return;
8141                 }
8142                 if (blocking_lock_was_deferred_smb1(req->sconn, req->mid)) {
8143                         /* We have re-scheduled this call. */
8144                         return;
8145                 }
8146                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
8147                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
8148                                         ERRSRV, ERRbadpath);
8149                         return;
8150                 }
8151                 if (info_level == SMB_POSIX_PATH_OPEN) {
8152                         reply_openerror(req, status);
8153                         return;
8154                 }
8155
8156                 reply_nterror(req, status);
8157                 return;
8158         }
8159
8160         send_trans2_replies(conn, req, params, 2, *ppdata, data_return_size,
8161                             max_data_bytes);
8162
8163         return;
8164 }
8165
8166 /****************************************************************************
8167  Reply to a TRANS2_MKDIR (make directory with extended attributes).
8168 ****************************************************************************/
8169
8170 static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
8171                              char **pparams, int total_params,
8172                              char **ppdata, int total_data,
8173                              unsigned int max_data_bytes)
8174 {
8175         struct smb_filename *smb_dname = NULL;
8176         char *params = *pparams;
8177         char *pdata = *ppdata;
8178         char *directory = NULL;
8179         NTSTATUS status = NT_STATUS_OK;
8180         struct ea_list *ea_list = NULL;
8181         TALLOC_CTX *ctx = talloc_tos();
8182
8183         if (!CAN_WRITE(conn)) {
8184                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
8185                 return;
8186         }
8187
8188         if (total_params < 5) {
8189                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8190                 return;
8191         }
8192
8193         srvstr_get_path(ctx, params, req->flags2, &directory, &params[4],
8194                         total_params - 4, STR_TERMINATE,
8195                         &status);
8196         if (!NT_STATUS_IS_OK(status)) {
8197                 reply_nterror(req, status);
8198                 return;
8199         }
8200
8201         DEBUG(3,("call_trans2mkdir : name = %s\n", directory));
8202
8203         status = filename_convert(ctx,
8204                                 conn,
8205                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
8206                                 directory,
8207                                 0,
8208                                 NULL,
8209                                 &smb_dname);
8210
8211         if (!NT_STATUS_IS_OK(status)) {
8212                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
8213                         reply_botherror(req,
8214                                 NT_STATUS_PATH_NOT_COVERED,
8215                                 ERRSRV, ERRbadpath);
8216                         return;
8217                 }
8218                 reply_nterror(req, status);
8219                 return;
8220         }
8221
8222         /*
8223          * OS/2 workplace shell seems to send SET_EA requests of "null"
8224          * length (4 bytes containing IVAL 4).
8225          * They seem to have no effect. Bug #3212. JRA.
8226          */
8227
8228         if (total_data && (total_data != 4)) {
8229                 /* Any data in this call is an EA list. */
8230                 if (total_data < 10) {
8231                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8232                         goto out;
8233                 }
8234
8235                 if (IVAL(pdata,0) > total_data) {
8236                         DEBUG(10,("call_trans2mkdir: bad total data size (%u) > %u\n",
8237                                 IVAL(pdata,0), (unsigned int)total_data));
8238                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8239                         goto out;
8240                 }
8241
8242                 ea_list = read_ea_list(talloc_tos(), pdata + 4,
8243                                        total_data - 4);
8244                 if (!ea_list) {
8245                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8246                         goto out;
8247                 }
8248
8249                 if (!lp_ea_support(SNUM(conn))) {
8250                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
8251                         goto out;
8252                 }
8253         }
8254         /* If total_data == 4 Windows doesn't care what values
8255          * are placed in that field, it just ignores them.
8256          * The System i QNTC IBM SMB client puts bad values here,
8257          * so ignore them. */
8258
8259         status = create_directory(conn, req, smb_dname);
8260
8261         if (!NT_STATUS_IS_OK(status)) {
8262                 reply_nterror(req, status);
8263                 goto out;
8264         }
8265
8266         /* Try and set any given EA. */
8267         if (ea_list) {
8268                 status = set_ea(conn, NULL, smb_dname, ea_list);
8269                 if (!NT_STATUS_IS_OK(status)) {
8270                         reply_nterror(req, status);
8271                         goto out;
8272                 }
8273         }
8274
8275         /* Realloc the parameter and data sizes */
8276         *pparams = (char *)SMB_REALLOC(*pparams,2);
8277         if(*pparams == NULL) {
8278                 reply_nterror(req, NT_STATUS_NO_MEMORY);
8279                 goto out;
8280         }
8281         params = *pparams;
8282
8283         SSVAL(params,0,0);
8284
8285         send_trans2_replies(conn, req, params, 2, *ppdata, 0, max_data_bytes);
8286
8287  out:
8288         TALLOC_FREE(smb_dname);
8289         return;
8290 }
8291
8292 /****************************************************************************
8293  Reply to a TRANS2_FINDNOTIFYFIRST (start monitoring a directory for changes).
8294  We don't actually do this - we just send a null response.
8295 ****************************************************************************/
8296
8297 static void call_trans2findnotifyfirst(connection_struct *conn,
8298                                        struct smb_request *req,
8299                                        char **pparams, int total_params,
8300                                        char **ppdata, int total_data,
8301                                        unsigned int max_data_bytes)
8302 {
8303         char *params = *pparams;
8304         uint16 info_level;
8305
8306         if (total_params < 6) {
8307                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8308                 return;
8309         }
8310
8311         info_level = SVAL(params,4);
8312         DEBUG(3,("call_trans2findnotifyfirst - info_level %d\n", info_level));
8313
8314         switch (info_level) {
8315                 case 1:
8316                 case 2:
8317                         break;
8318                 default:
8319                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
8320                         return;
8321         }
8322
8323         /* Realloc the parameter and data sizes */
8324         *pparams = (char *)SMB_REALLOC(*pparams,6);
8325         if (*pparams == NULL) {
8326                 reply_nterror(req, NT_STATUS_NO_MEMORY);
8327                 return;
8328         }
8329         params = *pparams;
8330
8331         SSVAL(params,0,fnf_handle);
8332         SSVAL(params,2,0); /* No changes */
8333         SSVAL(params,4,0); /* No EA errors */
8334
8335         fnf_handle++;
8336
8337         if(fnf_handle == 0)
8338                 fnf_handle = 257;
8339
8340         send_trans2_replies(conn, req, params, 6, *ppdata, 0, max_data_bytes);
8341
8342         return;
8343 }
8344
8345 /****************************************************************************
8346  Reply to a TRANS2_FINDNOTIFYNEXT (continue monitoring a directory for 
8347  changes). Currently this does nothing.
8348 ****************************************************************************/
8349
8350 static void call_trans2findnotifynext(connection_struct *conn,
8351                                       struct smb_request *req,
8352                                       char **pparams, int total_params,
8353                                       char **ppdata, int total_data,
8354                                       unsigned int max_data_bytes)
8355 {
8356         char *params = *pparams;
8357
8358         DEBUG(3,("call_trans2findnotifynext\n"));
8359
8360         /* Realloc the parameter and data sizes */
8361         *pparams = (char *)SMB_REALLOC(*pparams,4);
8362         if (*pparams == NULL) {
8363                 reply_nterror(req, NT_STATUS_NO_MEMORY);
8364                 return;
8365         }
8366         params = *pparams;
8367
8368         SSVAL(params,0,0); /* No changes */
8369         SSVAL(params,2,0); /* No EA errors */
8370
8371         send_trans2_replies(conn, req, params, 4, *ppdata, 0, max_data_bytes);
8372
8373         return;
8374 }
8375
8376 /****************************************************************************
8377  Reply to a TRANS2_GET_DFS_REFERRAL - Shirish Kalele <kalele@veritas.com>.
8378 ****************************************************************************/
8379
8380 static void call_trans2getdfsreferral(connection_struct *conn,
8381                                       struct smb_request *req,
8382                                       char **pparams, int total_params,
8383                                       char **ppdata, int total_data,
8384                                       unsigned int max_data_bytes)
8385 {
8386         char *params = *pparams;
8387         char *pathname = NULL;
8388         int reply_size = 0;
8389         int max_referral_level;
8390         NTSTATUS status = NT_STATUS_OK;
8391         TALLOC_CTX *ctx = talloc_tos();
8392
8393         DEBUG(10,("call_trans2getdfsreferral\n"));
8394
8395         if (total_params < 3) {
8396                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8397                 return;
8398         }
8399
8400         max_referral_level = SVAL(params,0);
8401
8402         if(!lp_host_msdfs()) {
8403                 reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
8404                 return;
8405         }
8406
8407         srvstr_pull_talloc(ctx, params, req->flags2, &pathname, &params[2],
8408                     total_params - 2, STR_TERMINATE);
8409         if (!pathname) {
8410                 reply_nterror(req, NT_STATUS_NOT_FOUND);
8411                 return;
8412         }
8413         if((reply_size = setup_dfs_referral(conn, pathname, max_referral_level,
8414                                             ppdata,&status)) < 0) {
8415                 reply_nterror(req, status);
8416                 return;
8417         }
8418
8419         SSVAL((discard_const_p(uint8_t, req->inbuf)), smb_flg2,
8420               SVAL(req->inbuf,smb_flg2) | FLAGS2_DFS_PATHNAMES);
8421         send_trans2_replies(conn, req,0,0,*ppdata,reply_size, max_data_bytes);
8422
8423         return;
8424 }
8425
8426 #define LMCAT_SPL       0x53
8427 #define LMFUNC_GETJOBID 0x60
8428
8429 /****************************************************************************
8430  Reply to a TRANS2_IOCTL - used for OS/2 printing.
8431 ****************************************************************************/
8432
8433 static void call_trans2ioctl(connection_struct *conn,
8434                              struct smb_request *req,
8435                              char **pparams, int total_params,
8436                              char **ppdata, int total_data,
8437                              unsigned int max_data_bytes)
8438 {
8439         char *pdata = *ppdata;
8440         files_struct *fsp = file_fsp(req, SVAL(req->vwv+15, 0));
8441
8442         /* check for an invalid fid before proceeding */
8443
8444         if (!fsp) {
8445                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
8446                 return;
8447         }
8448
8449         if ((SVAL(req->vwv+16, 0) == LMCAT_SPL)
8450             && (SVAL(req->vwv+17, 0) == LMFUNC_GETJOBID)) {
8451                 *ppdata = (char *)SMB_REALLOC(*ppdata, 32);
8452                 if (*ppdata == NULL) {
8453                         reply_nterror(req, NT_STATUS_NO_MEMORY);
8454                         return;
8455                 }
8456                 pdata = *ppdata;
8457
8458                 /* NOTE - THIS IS ASCII ONLY AT THE MOMENT - NOT SURE IF OS/2
8459                         CAN ACCEPT THIS IN UNICODE. JRA. */
8460
8461                 /* Job number */
8462                 SSVAL(pdata, 0, print_spool_rap_jobid(fsp->print_file));
8463
8464                 srvstr_push(pdata, req->flags2, pdata + 2,
8465                             lp_netbios_name(), 15,
8466                             STR_ASCII|STR_TERMINATE); /* Our NetBIOS name */
8467                 srvstr_push(pdata, req->flags2, pdata+18,
8468                             lp_servicename(talloc_tos(), SNUM(conn)), 13,
8469                             STR_ASCII|STR_TERMINATE); /* Service name */
8470                 send_trans2_replies(conn, req, *pparams, 0, *ppdata, 32,
8471                                     max_data_bytes);
8472                 return;
8473         }
8474
8475         DEBUG(2,("Unknown TRANS2_IOCTL\n"));
8476         reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
8477 }
8478
8479 /****************************************************************************
8480  Reply to a SMBfindclose (stop trans2 directory search).
8481 ****************************************************************************/
8482
8483 void reply_findclose(struct smb_request *req)
8484 {
8485         int dptr_num;
8486         struct smbd_server_connection *sconn = req->sconn;
8487
8488         START_PROFILE(SMBfindclose);
8489
8490         if (req->wct < 1) {
8491                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8492                 END_PROFILE(SMBfindclose);
8493                 return;
8494         }
8495
8496         dptr_num = SVALS(req->vwv+0, 0);
8497
8498         DEBUG(3,("reply_findclose, dptr_num = %d\n", dptr_num));
8499
8500         dptr_close(sconn, &dptr_num);
8501
8502         reply_outbuf(req, 0, 0);
8503
8504         DEBUG(3,("SMBfindclose dptr_num = %d\n", dptr_num));
8505
8506         END_PROFILE(SMBfindclose);
8507         return;
8508 }
8509
8510 /****************************************************************************
8511  Reply to a SMBfindnclose (stop FINDNOTIFYFIRST directory search).
8512 ****************************************************************************/
8513
8514 void reply_findnclose(struct smb_request *req)
8515 {
8516         int dptr_num;
8517
8518         START_PROFILE(SMBfindnclose);
8519
8520         if (req->wct < 1) {
8521                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8522                 END_PROFILE(SMBfindnclose);
8523                 return;
8524         }
8525
8526         dptr_num = SVAL(req->vwv+0, 0);
8527
8528         DEBUG(3,("reply_findnclose, dptr_num = %d\n", dptr_num));
8529
8530         /* We never give out valid handles for a 
8531            findnotifyfirst - so any dptr_num is ok here. 
8532            Just ignore it. */
8533
8534         reply_outbuf(req, 0, 0);
8535
8536         DEBUG(3,("SMB_findnclose dptr_num = %d\n", dptr_num));
8537
8538         END_PROFILE(SMBfindnclose);
8539         return;
8540 }
8541
8542 static void handle_trans2(connection_struct *conn, struct smb_request *req,
8543                           struct trans_state *state)
8544 {
8545         if (get_Protocol() >= PROTOCOL_NT1) {
8546                 req->flags2 |= 0x40; /* IS_LONG_NAME */
8547                 SSVAL((discard_const_p(uint8_t, req->inbuf)),smb_flg2,req->flags2);
8548         }
8549
8550         if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
8551                 if (state->call != TRANSACT2_QFSINFO &&
8552                     state->call != TRANSACT2_SETFSINFO) {
8553                         DEBUG(0,("handle_trans2: encryption required "
8554                                 "with call 0x%x\n",
8555                                 (unsigned int)state->call));
8556                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
8557                         return;
8558                 }
8559         }
8560
8561         SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
8562
8563         /* Now we must call the relevant TRANS2 function */
8564         switch(state->call)  {
8565         case TRANSACT2_OPEN:
8566         {
8567                 START_PROFILE(Trans2_open);
8568                 call_trans2open(conn, req,
8569                                 &state->param, state->total_param,
8570                                 &state->data, state->total_data,
8571                                 state->max_data_return);
8572                 END_PROFILE(Trans2_open);
8573                 break;
8574         }
8575
8576         case TRANSACT2_FINDFIRST:
8577         {
8578                 START_PROFILE(Trans2_findfirst);
8579                 call_trans2findfirst(conn, req,
8580                                      &state->param, state->total_param,
8581                                      &state->data, state->total_data,
8582                                      state->max_data_return);
8583                 END_PROFILE(Trans2_findfirst);
8584                 break;
8585         }
8586
8587         case TRANSACT2_FINDNEXT:
8588         {
8589                 START_PROFILE(Trans2_findnext);
8590                 call_trans2findnext(conn, req,
8591                                     &state->param, state->total_param,
8592                                     &state->data, state->total_data,
8593                                     state->max_data_return);
8594                 END_PROFILE(Trans2_findnext);
8595                 break;
8596         }
8597
8598         case TRANSACT2_QFSINFO:
8599         {
8600                 START_PROFILE(Trans2_qfsinfo);
8601                 call_trans2qfsinfo(conn, req,
8602                                    &state->param, state->total_param,
8603                                    &state->data, state->total_data,
8604                                    state->max_data_return);
8605                 END_PROFILE(Trans2_qfsinfo);
8606             break;
8607         }
8608
8609         case TRANSACT2_SETFSINFO:
8610         {
8611                 START_PROFILE(Trans2_setfsinfo);
8612                 call_trans2setfsinfo(conn, req,
8613                                      &state->param, state->total_param,
8614                                      &state->data, state->total_data,
8615                                      state->max_data_return);
8616                 END_PROFILE(Trans2_setfsinfo);
8617                 break;
8618         }
8619
8620         case TRANSACT2_QPATHINFO:
8621         case TRANSACT2_QFILEINFO:
8622         {
8623                 START_PROFILE(Trans2_qpathinfo);
8624                 call_trans2qfilepathinfo(conn, req, state->call,
8625                                          &state->param, state->total_param,
8626                                          &state->data, state->total_data,
8627                                          state->max_data_return);
8628                 END_PROFILE(Trans2_qpathinfo);
8629                 break;
8630         }
8631
8632         case TRANSACT2_SETPATHINFO:
8633         case TRANSACT2_SETFILEINFO:
8634         {
8635                 START_PROFILE(Trans2_setpathinfo);
8636                 call_trans2setfilepathinfo(conn, req, state->call,
8637                                            &state->param, state->total_param,
8638                                            &state->data, state->total_data,
8639                                            state->max_data_return);
8640                 END_PROFILE(Trans2_setpathinfo);
8641                 break;
8642         }
8643
8644         case TRANSACT2_FINDNOTIFYFIRST:
8645         {
8646                 START_PROFILE(Trans2_findnotifyfirst);
8647                 call_trans2findnotifyfirst(conn, req,
8648                                            &state->param, state->total_param,
8649                                            &state->data, state->total_data,
8650                                            state->max_data_return);
8651                 END_PROFILE(Trans2_findnotifyfirst);
8652                 break;
8653         }
8654
8655         case TRANSACT2_FINDNOTIFYNEXT:
8656         {
8657                 START_PROFILE(Trans2_findnotifynext);
8658                 call_trans2findnotifynext(conn, req,
8659                                           &state->param, state->total_param,
8660                                           &state->data, state->total_data,
8661                                           state->max_data_return);
8662                 END_PROFILE(Trans2_findnotifynext);
8663                 break;
8664         }
8665
8666         case TRANSACT2_MKDIR:
8667         {
8668                 START_PROFILE(Trans2_mkdir);
8669                 call_trans2mkdir(conn, req,
8670                                  &state->param, state->total_param,
8671                                  &state->data, state->total_data,
8672                                  state->max_data_return);
8673                 END_PROFILE(Trans2_mkdir);
8674                 break;
8675         }
8676
8677         case TRANSACT2_GET_DFS_REFERRAL:
8678         {
8679                 START_PROFILE(Trans2_get_dfs_referral);
8680                 call_trans2getdfsreferral(conn, req,
8681                                           &state->param, state->total_param,
8682                                           &state->data, state->total_data,
8683                                           state->max_data_return);
8684                 END_PROFILE(Trans2_get_dfs_referral);
8685                 break;
8686         }
8687
8688         case TRANSACT2_IOCTL:
8689         {
8690                 START_PROFILE(Trans2_ioctl);
8691                 call_trans2ioctl(conn, req,
8692                                  &state->param, state->total_param,
8693                                  &state->data, state->total_data,
8694                                  state->max_data_return);
8695                 END_PROFILE(Trans2_ioctl);
8696                 break;
8697         }
8698
8699         default:
8700                 /* Error in request */
8701                 DEBUG(2,("Unknown request %d in trans2 call\n", state->call));
8702                 reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
8703         }
8704 }
8705
8706 /****************************************************************************
8707  Reply to a SMBtrans2.
8708  ****************************************************************************/
8709
8710 void reply_trans2(struct smb_request *req)
8711 {
8712         connection_struct *conn = req->conn;
8713         unsigned int dsoff;
8714         unsigned int dscnt;
8715         unsigned int psoff;
8716         unsigned int pscnt;
8717         unsigned int tran_call;
8718         struct trans_state *state;
8719         NTSTATUS result;
8720
8721         START_PROFILE(SMBtrans2);
8722
8723         if (req->wct < 14) {
8724                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8725                 END_PROFILE(SMBtrans2);
8726                 return;
8727         }
8728
8729         dsoff = SVAL(req->vwv+12, 0);
8730         dscnt = SVAL(req->vwv+11, 0);
8731         psoff = SVAL(req->vwv+10, 0);
8732         pscnt = SVAL(req->vwv+9, 0);
8733         tran_call = SVAL(req->vwv+14, 0);
8734
8735         result = allow_new_trans(conn->pending_trans, req->mid);
8736         if (!NT_STATUS_IS_OK(result)) {
8737                 DEBUG(2, ("Got invalid trans2 request: %s\n",
8738                           nt_errstr(result)));
8739                 reply_nterror(req, result);
8740                 END_PROFILE(SMBtrans2);
8741                 return;
8742         }
8743
8744         if (IS_IPC(conn)) {
8745                 switch (tran_call) {
8746                 /* List the allowed trans2 calls on IPC$ */
8747                 case TRANSACT2_OPEN:
8748                 case TRANSACT2_GET_DFS_REFERRAL:
8749                 case TRANSACT2_QFILEINFO:
8750                 case TRANSACT2_QFSINFO:
8751                 case TRANSACT2_SETFSINFO:
8752                         break;
8753                 default:
8754                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
8755                         END_PROFILE(SMBtrans2);
8756                         return;
8757                 }
8758         }
8759
8760         if ((state = talloc(conn, struct trans_state)) == NULL) {
8761                 DEBUG(0, ("talloc failed\n"));
8762                 reply_nterror(req, NT_STATUS_NO_MEMORY);
8763                 END_PROFILE(SMBtrans2);
8764                 return;
8765         }
8766
8767         state->cmd = SMBtrans2;
8768
8769         state->mid = req->mid;
8770         state->vuid = req->vuid;
8771         state->setup_count = SVAL(req->vwv+13, 0);
8772         state->setup = NULL;
8773         state->total_param = SVAL(req->vwv+0, 0);
8774         state->param = NULL;
8775         state->total_data =  SVAL(req->vwv+1, 0);
8776         state->data = NULL;
8777         state->max_param_return = SVAL(req->vwv+2, 0);
8778         state->max_data_return  = SVAL(req->vwv+3, 0);
8779         state->max_setup_return = SVAL(req->vwv+4, 0);
8780         state->close_on_completion = BITSETW(req->vwv+5, 0);
8781         state->one_way = BITSETW(req->vwv+5, 1);
8782
8783         state->call = tran_call;
8784
8785         /* All trans2 messages we handle have smb_sucnt == 1 - ensure this
8786            is so as a sanity check */
8787         if (state->setup_count != 1) {
8788                 /*
8789                  * Need to have rc=0 for ioctl to get job id for OS/2.
8790                  *  Network printing will fail if function is not successful.
8791                  *  Similar function in reply.c will be used if protocol
8792                  *  is LANMAN1.0 instead of LM1.2X002.
8793                  *  Until DosPrintSetJobInfo with PRJINFO3 is supported,
8794                  *  outbuf doesn't have to be set(only job id is used).
8795                  */
8796                 if ( (state->setup_count == 4)
8797                      && (tran_call == TRANSACT2_IOCTL)
8798                      && (SVAL(req->vwv+16, 0) == LMCAT_SPL)
8799                      && (SVAL(req->vwv+17, 0) == LMFUNC_GETJOBID)) {
8800                         DEBUG(2,("Got Trans2 DevIOctl jobid\n"));
8801                 } else {
8802                         DEBUG(2,("Invalid smb_sucnt in trans2 call(%u)\n",state->setup_count));
8803                         DEBUG(2,("Transaction is %d\n",tran_call));
8804                         TALLOC_FREE(state);
8805                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8806                         END_PROFILE(SMBtrans2);
8807                         return;
8808                 }
8809         }
8810
8811         if ((dscnt > state->total_data) || (pscnt > state->total_param))
8812                 goto bad_param;
8813
8814         if (state->total_data) {
8815
8816                 if (trans_oob(state->total_data, 0, dscnt)
8817                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
8818                         goto bad_param;
8819                 }
8820
8821                 /* Can't use talloc here, the core routines do realloc on the
8822                  * params and data. */
8823                 state->data = (char *)SMB_MALLOC(state->total_data);
8824                 if (state->data == NULL) {
8825                         DEBUG(0,("reply_trans2: data malloc fail for %u "
8826                                  "bytes !\n", (unsigned int)state->total_data));
8827                         TALLOC_FREE(state);
8828                         reply_nterror(req, NT_STATUS_NO_MEMORY);
8829                         END_PROFILE(SMBtrans2);
8830                         return;
8831                 }
8832
8833                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
8834         }
8835
8836         if (state->total_param) {
8837
8838                 if (trans_oob(state->total_param, 0, pscnt)
8839                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
8840                         goto bad_param;
8841                 }
8842
8843                 /* Can't use talloc here, the core routines do realloc on the
8844                  * params and data. */
8845                 state->param = (char *)SMB_MALLOC(state->total_param);
8846                 if (state->param == NULL) {
8847                         DEBUG(0,("reply_trans: param malloc fail for %u "
8848                                  "bytes !\n", (unsigned int)state->total_param));
8849                         SAFE_FREE(state->data);
8850                         TALLOC_FREE(state);
8851                         reply_nterror(req, NT_STATUS_NO_MEMORY);
8852                         END_PROFILE(SMBtrans2);
8853                         return;
8854                 } 
8855
8856                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
8857         }
8858
8859         state->received_data  = dscnt;
8860         state->received_param = pscnt;
8861
8862         if ((state->received_param == state->total_param) &&
8863             (state->received_data == state->total_data)) {
8864
8865                 handle_trans2(conn, req, state);
8866
8867                 SAFE_FREE(state->data);
8868                 SAFE_FREE(state->param);
8869                 TALLOC_FREE(state);
8870                 END_PROFILE(SMBtrans2);
8871                 return;
8872         }
8873
8874         DLIST_ADD(conn->pending_trans, state);
8875
8876         /* We need to send an interim response then receive the rest
8877            of the parameter/data bytes */
8878         reply_outbuf(req, 0, 0);
8879         show_msg((char *)req->outbuf);
8880         END_PROFILE(SMBtrans2);
8881         return;
8882
8883   bad_param:
8884
8885         DEBUG(0,("reply_trans2: invalid trans parameters\n"));
8886         SAFE_FREE(state->data);
8887         SAFE_FREE(state->param);
8888         TALLOC_FREE(state);
8889         END_PROFILE(SMBtrans2);
8890         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8891 }
8892
8893
8894 /****************************************************************************
8895  Reply to a SMBtranss2
8896  ****************************************************************************/
8897
8898 void reply_transs2(struct smb_request *req)
8899 {
8900         connection_struct *conn = req->conn;
8901         unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
8902         struct trans_state *state;
8903
8904         START_PROFILE(SMBtranss2);
8905
8906         show_msg((const char *)req->inbuf);
8907
8908         /* Windows clients expect all replies to
8909            a transact secondary (SMBtranss2 0x33)
8910            to have a command code of transact
8911            (SMBtrans2 0x32). See bug #8989
8912            and also [MS-CIFS] section 2.2.4.47.2
8913            for details.
8914         */
8915         req->cmd = SMBtrans2;
8916
8917         if (req->wct < 8) {
8918                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8919                 END_PROFILE(SMBtranss2);
8920                 return;
8921         }
8922
8923         for (state = conn->pending_trans; state != NULL;
8924              state = state->next) {
8925                 if (state->mid == req->mid) {
8926                         break;
8927                 }
8928         }
8929
8930         if ((state == NULL) || (state->cmd != SMBtrans2)) {
8931                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8932                 END_PROFILE(SMBtranss2);
8933                 return;
8934         }
8935
8936         /* Revise state->total_param and state->total_data in case they have
8937            changed downwards */
8938
8939         if (SVAL(req->vwv+0, 0) < state->total_param)
8940                 state->total_param = SVAL(req->vwv+0, 0);
8941         if (SVAL(req->vwv+1, 0) < state->total_data)
8942                 state->total_data = SVAL(req->vwv+1, 0);
8943
8944         pcnt = SVAL(req->vwv+2, 0);
8945         poff = SVAL(req->vwv+3, 0);
8946         pdisp = SVAL(req->vwv+4, 0);
8947
8948         dcnt = SVAL(req->vwv+5, 0);
8949         doff = SVAL(req->vwv+6, 0);
8950         ddisp = SVAL(req->vwv+7, 0);
8951
8952         state->received_param += pcnt;
8953         state->received_data += dcnt;
8954
8955         if ((state->received_data > state->total_data) ||
8956             (state->received_param > state->total_param))
8957                 goto bad_param;
8958
8959         if (pcnt) {
8960                 if (trans_oob(state->total_param, pdisp, pcnt)
8961                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
8962                         goto bad_param;
8963                 }
8964                 memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
8965         }
8966
8967         if (dcnt) {
8968                 if (trans_oob(state->total_data, ddisp, dcnt)
8969                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
8970                         goto bad_param;
8971                 }
8972                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
8973         }
8974
8975         if ((state->received_param < state->total_param) ||
8976             (state->received_data < state->total_data)) {
8977                 END_PROFILE(SMBtranss2);
8978                 return;
8979         }
8980
8981         handle_trans2(conn, req, state);
8982
8983         DLIST_REMOVE(conn->pending_trans, state);
8984         SAFE_FREE(state->data);
8985         SAFE_FREE(state->param);
8986         TALLOC_FREE(state);
8987
8988         END_PROFILE(SMBtranss2);
8989         return;
8990
8991   bad_param:
8992
8993         DEBUG(0,("reply_transs2: invalid trans parameters\n"));
8994         DLIST_REMOVE(conn->pending_trans, state);
8995         SAFE_FREE(state->data);
8996         SAFE_FREE(state->param);
8997         TALLOC_FREE(state);
8998         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
8999         END_PROFILE(SMBtranss2);
9000         return;
9001 }