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