8e03094aef5c95669e69515f052c895771106bfe
[tprouty/samba.git] / source / 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                    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
28 extern int max_send;
29 extern enum protocol_types Protocol;
30 extern int smb_read_error;
31 extern uint32 global_client_caps;
32 extern struct current_user current_user;
33
34 #define get_file_size(sbuf) ((sbuf).st_size)
35 #define DIR_ENTRY_SAFETY_MARGIN 4096
36
37 static char *store_file_unix_basic(connection_struct *conn,
38                                 char *pdata,
39                                 files_struct *fsp,
40                                 const SMB_STRUCT_STAT *psbuf);
41
42 static char *store_file_unix_basic_info2(connection_struct *conn,
43                                 char *pdata,
44                                 files_struct *fsp,
45                                 const SMB_STRUCT_STAT *psbuf);
46
47 /********************************************************************
48  Roundup a value to the nearest allocation roundup size boundary.
49  Only do this for Windows clients.
50 ********************************************************************/
51
52 SMB_BIG_UINT smb_roundup(connection_struct *conn, SMB_BIG_UINT val)
53 {
54         SMB_BIG_UINT rval = lp_allocation_roundup_size(SNUM(conn));
55
56         /* Only roundup for Windows clients. */
57         enum remote_arch_types ra_type = get_remote_arch();
58         if (rval && (ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
59                 val = SMB_ROUNDUP(val,rval);
60         }
61         return val;
62 }
63
64 /********************************************************************
65  Given a stat buffer return the allocated size on disk, taking into
66  account sparse files.
67 ********************************************************************/
68
69 SMB_BIG_UINT get_allocation_size(connection_struct *conn, files_struct *fsp, const SMB_STRUCT_STAT *sbuf)
70 {
71         SMB_BIG_UINT ret;
72
73         if(S_ISDIR(sbuf->st_mode)) {
74                 return 0;
75         }
76
77 #if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
78         ret = (SMB_BIG_UINT)STAT_ST_BLOCKSIZE * (SMB_BIG_UINT)sbuf->st_blocks;
79 #else
80         ret = (SMB_BIG_UINT)get_file_size(*sbuf);
81 #endif
82
83         if (fsp && fsp->initial_allocation_size)
84                 ret = MAX(ret,fsp->initial_allocation_size);
85
86         return smb_roundup(conn, ret);
87 }
88
89 /****************************************************************************
90  Utility functions for dealing with extended attributes.
91 ****************************************************************************/
92
93 /****************************************************************************
94  Refuse to allow clients to overwrite our private xattrs.
95 ****************************************************************************/
96
97 static BOOL samba_private_attr_name(const char *unix_ea_name)
98 {
99         static const char *prohibited_ea_names[] = {
100                 SAMBA_POSIX_INHERITANCE_EA_NAME,
101                 SAMBA_XATTR_DOS_ATTRIB,
102                 NULL
103         };
104
105         int i;
106
107         for (i = 0; prohibited_ea_names[i]; i++) {
108                 if (strequal( prohibited_ea_names[i], unix_ea_name))
109                         return True;
110         }
111         return False;
112 }
113
114 /****************************************************************************
115  Get one EA value. Fill in a struct ea_struct.
116 ****************************************************************************/
117
118 static BOOL get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
119                                 const char *fname, char *ea_name, struct ea_struct *pea)
120 {
121         /* Get the value of this xattr. Max size is 64k. */
122         size_t attr_size = 256;
123         char *val = NULL;
124         ssize_t sizeret;
125
126  again:
127
128         val = TALLOC_REALLOC_ARRAY(mem_ctx, val, char, attr_size);
129         if (!val) {
130                 return False;
131         }
132
133         if (fsp && fsp->fh->fd != -1) {
134                 sizeret = SMB_VFS_FGETXATTR(fsp, fsp->fh->fd, ea_name, val, attr_size);
135         } else {
136                 sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
137         }
138
139         if (sizeret == -1 && errno == ERANGE && attr_size != 65536) {
140                 attr_size = 65536;
141                 goto again;
142         }
143
144         if (sizeret == -1) {
145                 return False;
146         }
147
148         DEBUG(10,("get_ea_value: EA %s is of length %u: ", ea_name, (unsigned int)sizeret));
149         dump_data(10, (uint8 *)val, sizeret);
150
151         pea->flags = 0;
152         if (strnequal(ea_name, "user.", 5)) {
153                 pea->name = &ea_name[5];
154         } else {
155                 pea->name = ea_name;
156         }
157         pea->value.data = (unsigned char *)val;
158         pea->value.length = (size_t)sizeret;
159         return True;
160 }
161
162 /****************************************************************************
163  Return a linked list of the total EA's. Plus the total size
164 ****************************************************************************/
165
166 static struct ea_list *get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
167                                         const char *fname, size_t *pea_total_len)
168 {
169         /* Get a list of all xattrs. Max namesize is 64k. */
170         size_t ea_namelist_size = 1024;
171         char *ea_namelist;
172         char *p;
173         ssize_t sizeret;
174         int i;
175         struct ea_list *ea_list_head = NULL;
176
177         *pea_total_len = 0;
178
179         if (!lp_ea_support(SNUM(conn))) {
180                 return NULL;
181         }
182
183         for (i = 0, ea_namelist = TALLOC_ARRAY(mem_ctx, char, ea_namelist_size); i < 6;
184              ea_namelist = TALLOC_REALLOC_ARRAY(mem_ctx, ea_namelist, char, ea_namelist_size), i++) {
185
186                 if (!ea_namelist) {
187                         return NULL;
188                 }
189
190                 if (fsp && fsp->fh->fd != -1) {
191                         sizeret = SMB_VFS_FLISTXATTR(fsp, fsp->fh->fd, ea_namelist, ea_namelist_size);
192                 } else {
193                         sizeret = SMB_VFS_LISTXATTR(conn, fname, ea_namelist, ea_namelist_size);
194                 }
195
196                 if (sizeret == -1 && errno == ERANGE) {
197                         ea_namelist_size *= 2;
198                 } else {
199                         break;
200                 }
201         }
202
203         if (sizeret == -1)
204                 return NULL;
205
206         DEBUG(10,("get_ea_list_from_file: ea_namelist size = %u\n", (unsigned int)sizeret ));
207
208         if (sizeret) {
209                 for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p) + 1) {
210                         struct ea_list *listp;
211
212                         if (strnequal(p, "system.", 7) || samba_private_attr_name(p))
213                                 continue;
214                 
215                         listp = TALLOC_P(mem_ctx, struct ea_list);
216                         if (!listp)
217                                 return NULL;
218
219                         if (!get_ea_value(mem_ctx, conn, fsp, fname, p, &listp->ea)) {
220                                 return NULL;
221                         }
222
223                         {
224                                 fstring dos_ea_name;
225                                 push_ascii_fstring(dos_ea_name, listp->ea.name);
226                                 *pea_total_len += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
227                                 DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len = %u\n",
228                                         (unsigned int)*pea_total_len, dos_ea_name,
229                                         (unsigned int)listp->ea.value.length ));
230                         }
231                         DLIST_ADD_END(ea_list_head, listp, struct ea_list *);
232                 }
233                 /* Add on 4 for total length. */
234                 if (*pea_total_len) {
235                         *pea_total_len += 4;
236                 }
237         }
238
239         DEBUG(10,("get_ea_list_from_file: total_len = %u\n", (unsigned int)*pea_total_len));
240         return ea_list_head;
241 }
242
243 /****************************************************************************
244  Fill a qfilepathinfo buffer with EA's. Returns the length of the buffer
245  that was filled.
246 ****************************************************************************/
247
248 static unsigned int fill_ea_buffer(TALLOC_CTX *mem_ctx, char *pdata, unsigned int total_data_size,
249         connection_struct *conn, struct ea_list *ea_list)
250 {
251         unsigned int ret_data_size = 4;
252         char *p = pdata;
253
254         SMB_ASSERT(total_data_size >= 4);
255
256         if (!lp_ea_support(SNUM(conn))) {
257                 SIVAL(pdata,4,0);
258                 return 4;
259         }
260
261         for (p = pdata + 4; ea_list; ea_list = ea_list->next) {
262                 size_t dos_namelen;
263                 fstring dos_ea_name;
264                 push_ascii_fstring(dos_ea_name, ea_list->ea.name);
265                 dos_namelen = strlen(dos_ea_name);
266                 if (dos_namelen > 255 || dos_namelen == 0) {
267                         break;
268                 }
269                 if (ea_list->ea.value.length > 65535) {
270                         break;
271                 }
272                 if (4 + dos_namelen + 1 + ea_list->ea.value.length > total_data_size) {
273                         break;
274                 }
275
276                 /* We know we have room. */
277                 SCVAL(p,0,ea_list->ea.flags);
278                 SCVAL(p,1,dos_namelen);
279                 SSVAL(p,2,ea_list->ea.value.length);
280                 fstrcpy(p+4, dos_ea_name);
281                 memcpy( p + 4 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
282
283                 total_data_size -= 4 + dos_namelen + 1 + ea_list->ea.value.length;
284                 p += 4 + dos_namelen + 1 + ea_list->ea.value.length;
285         }
286
287         ret_data_size = PTR_DIFF(p, pdata);
288         DEBUG(10,("fill_ea_buffer: data_size = %u\n", ret_data_size ));
289         SIVAL(pdata,0,ret_data_size);
290         return ret_data_size;
291 }
292
293 static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp, const char *fname)
294 {
295         size_t total_ea_len = 0;
296         TALLOC_CTX *mem_ctx = NULL;
297
298         if (!lp_ea_support(SNUM(conn))) {
299                 return 0;
300         }
301         mem_ctx = talloc_init("estimate_ea_size");
302         (void)get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
303         talloc_destroy(mem_ctx);
304         return total_ea_len;
305 }
306
307 /****************************************************************************
308  Ensure the EA name is case insensitive by matching any existing EA name.
309 ****************************************************************************/
310
311 static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, const char *fname, fstring unix_ea_name)
312 {
313         size_t total_ea_len;
314         TALLOC_CTX *mem_ctx = talloc_init("canonicalize_ea_name");
315         struct ea_list *ea_list = get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
316
317         for (; ea_list; ea_list = ea_list->next) {
318                 if (strequal(&unix_ea_name[5], ea_list->ea.name)) {
319                         DEBUG(10,("canonicalize_ea_name: %s -> %s\n",
320                                 &unix_ea_name[5], ea_list->ea.name));
321                         safe_strcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-6);
322                         break;
323                 }
324         }
325         talloc_destroy(mem_ctx);
326 }
327
328 /****************************************************************************
329  Set or delete an extended attribute.
330 ****************************************************************************/
331
332 NTSTATUS set_ea(connection_struct *conn, files_struct *fsp, const char *fname, struct ea_list *ea_list)
333 {
334         if (!lp_ea_support(SNUM(conn))) {
335                 return NT_STATUS_EAS_NOT_SUPPORTED;
336         }
337
338         for (;ea_list; ea_list = ea_list->next) {
339                 int ret;
340                 fstring unix_ea_name;
341
342                 fstrcpy(unix_ea_name, "user."); /* All EA's must start with user. */
343                 fstrcat(unix_ea_name, ea_list->ea.name);
344
345                 canonicalize_ea_name(conn, fsp, fname, unix_ea_name);
346
347                 DEBUG(10,("set_ea: ea_name %s ealen = %u\n", unix_ea_name, (unsigned int)ea_list->ea.value.length));
348
349                 if (samba_private_attr_name(unix_ea_name)) {
350                         DEBUG(10,("set_ea: ea name %s is a private Samba name.\n", unix_ea_name));
351                         return NT_STATUS_ACCESS_DENIED;
352                 }
353
354                 if (ea_list->ea.value.length == 0) {
355                         /* Remove the attribute. */
356                         if (fsp && (fsp->fh->fd != -1)) {
357                                 DEBUG(10,("set_ea: deleting ea name %s on file %s by file descriptor.\n",
358                                         unix_ea_name, fsp->fsp_name));
359                                 ret = SMB_VFS_FREMOVEXATTR(fsp, fsp->fh->fd, unix_ea_name);
360                         } else {
361                                 DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
362                                         unix_ea_name, fname));
363                                 ret = SMB_VFS_REMOVEXATTR(conn, fname, unix_ea_name);
364                         }
365 #ifdef ENOATTR
366                         /* Removing a non existent attribute always succeeds. */
367                         if (ret == -1 && errno == ENOATTR) {
368                                 DEBUG(10,("set_ea: deleting ea name %s didn't exist - succeeding by default.\n",
369                                                 unix_ea_name));
370                                 ret = 0;
371                         }
372 #endif
373                 } else {
374                         if (fsp && (fsp->fh->fd != -1)) {
375                                 DEBUG(10,("set_ea: setting ea name %s on file %s by file descriptor.\n",
376                                         unix_ea_name, fsp->fsp_name));
377                                 ret = SMB_VFS_FSETXATTR(fsp, fsp->fh->fd, unix_ea_name,
378                                                         ea_list->ea.value.data, ea_list->ea.value.length, 0);
379                         } else {
380                                 DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
381                                         unix_ea_name, fname));
382                                 ret = SMB_VFS_SETXATTR(conn, fname, unix_ea_name,
383                                                         ea_list->ea.value.data, ea_list->ea.value.length, 0);
384                         }
385                 }
386
387                 if (ret == -1) {
388 #ifdef ENOTSUP
389                         if (errno == ENOTSUP) {
390                                 return NT_STATUS_EAS_NOT_SUPPORTED;
391                         }
392 #endif
393                         return map_nt_error_from_unix(errno);
394                 }
395
396         }
397         return NT_STATUS_OK;
398 }
399 /****************************************************************************
400  Read a list of EA names from an incoming data buffer. Create an ea_list with them.
401 ****************************************************************************/
402
403 static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
404 {
405         struct ea_list *ea_list_head = NULL;
406         size_t offset = 0;
407
408         while (offset + 2 < data_size) {
409                 struct ea_list *eal = TALLOC_ZERO_P(ctx, struct ea_list);
410                 unsigned int namelen = CVAL(pdata,offset);
411
412                 offset++; /* Go past the namelen byte. */
413
414                 /* integer wrap paranioa. */
415                 if ((offset + namelen < offset) || (offset + namelen < namelen) ||
416                                 (offset > data_size) || (namelen > data_size) ||
417                                 (offset + namelen >= data_size)) {
418                         break;
419                 }
420                 /* Ensure the name is null terminated. */
421                 if (pdata[offset + namelen] != '\0') {
422                         return NULL;
423                 }
424                 pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset]);
425                 if (!eal->ea.name) {
426                         return NULL;
427                 }
428
429                 offset += (namelen + 1); /* Go past the name + terminating zero. */
430                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
431                 DEBUG(10,("read_ea_name_list: read ea name %s\n", eal->ea.name));
432         }
433
434         return ea_list_head;
435 }
436
437 /****************************************************************************
438  Read one EA list entry from the buffer.
439 ****************************************************************************/
440
441 struct ea_list *read_ea_list_entry(TALLOC_CTX *ctx, const char *pdata, size_t data_size, size_t *pbytes_used)
442 {
443         struct ea_list *eal = TALLOC_ZERO_P(ctx, struct ea_list);
444         uint16 val_len;
445         unsigned int namelen;
446
447         if (!eal) {
448                 return NULL;
449         }
450
451         if (data_size < 6) {
452                 return NULL;
453         }
454
455         eal->ea.flags = CVAL(pdata,0);
456         namelen = CVAL(pdata,1);
457         val_len = SVAL(pdata,2);
458
459         if (4 + namelen + 1 + val_len > data_size) {
460                 return NULL;
461         }
462
463         /* Ensure the name is null terminated. */
464         if (pdata[namelen + 4] != '\0') {
465                 return NULL;
466         }
467         pull_ascii_talloc(ctx, &eal->ea.name, pdata + 4);
468         if (!eal->ea.name) {
469                 return NULL;
470         }
471
472         eal->ea.value = data_blob_talloc(eal, NULL, (size_t)val_len + 1);
473         if (!eal->ea.value.data) {
474                 return NULL;
475         }
476
477         memcpy(eal->ea.value.data, pdata + 4 + namelen + 1, val_len);
478
479         /* Ensure we're null terminated just in case we print the value. */
480         eal->ea.value.data[val_len] = '\0';
481         /* But don't count the null. */
482         eal->ea.value.length--;
483
484         if (pbytes_used) {
485                 *pbytes_used = 4 + namelen + 1 + val_len;
486         }
487
488         DEBUG(10,("read_ea_list_entry: read ea name %s\n", eal->ea.name));
489         dump_data(10, eal->ea.value.data, eal->ea.value.length);
490
491         return eal;
492 }
493
494 /****************************************************************************
495  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
496 ****************************************************************************/
497
498 static struct ea_list *read_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
499 {
500         struct ea_list *ea_list_head = NULL;
501         size_t offset = 0;
502         size_t bytes_used = 0;
503
504         while (offset < data_size) {
505                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset, data_size - offset, &bytes_used);
506
507                 if (!eal) {
508                         return NULL;
509                 }
510
511                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
512                 offset += bytes_used;
513         }
514
515         return ea_list_head;
516 }
517
518 /****************************************************************************
519  Count the total EA size needed.
520 ****************************************************************************/
521
522 static size_t ea_list_size(struct ea_list *ealist)
523 {
524         fstring dos_ea_name;
525         struct ea_list *listp;
526         size_t ret = 0;
527
528         for (listp = ealist; listp; listp = listp->next) {
529                 push_ascii_fstring(dos_ea_name, listp->ea.name);
530                 ret += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
531         }
532         /* Add on 4 for total length. */
533         if (ret) {
534                 ret += 4;
535         }
536
537         return ret;
538 }
539
540 /****************************************************************************
541  Return a union of EA's from a file list and a list of names.
542  The TALLOC context for the two lists *MUST* be identical as we steal
543  memory from one list to add to another. JRA.
544 ****************************************************************************/
545
546 static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *file_list, size_t *total_ea_len)
547 {
548         struct ea_list *nlistp, *flistp;
549
550         for (nlistp = name_list; nlistp; nlistp = nlistp->next) {
551                 for (flistp = file_list; flistp; flistp = flistp->next) {
552                         if (strequal(nlistp->ea.name, flistp->ea.name)) {
553                                 break;
554                         }
555                 }
556
557                 if (flistp) {
558                         /* Copy the data from this entry. */
559                         nlistp->ea.flags = flistp->ea.flags;
560                         nlistp->ea.value = flistp->ea.value;
561                 } else {
562                         /* Null entry. */
563                         nlistp->ea.flags = 0;
564                         ZERO_STRUCT(nlistp->ea.value);
565                 }
566         }
567
568         *total_ea_len = ea_list_size(name_list);
569         return name_list;
570 }
571
572 /****************************************************************************
573   Send the required number of replies back.
574   We assume all fields other than the data fields are
575   set correctly for the type of call.
576   HACK ! Always assumes smb_setup field is zero.
577 ****************************************************************************/
578
579 void send_trans2_replies(struct smb_request *req,
580                          const char *params,
581                          int paramsize,
582                          const char *pdata,
583                          int datasize,
584                          int max_data_bytes)
585 {
586         /* As we are using a protocol > LANMAN1 then the max_send
587          variable must have been set in the sessetupX call.
588          This takes precedence over the max_xmit field in the
589          global struct. These different max_xmit variables should
590          be merged as this is now too confusing */
591
592         int data_to_send = datasize;
593         int params_to_send = paramsize;
594         int useable_space;
595         const char *pp = params;
596         const char *pd = pdata;
597         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
598         int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
599         int data_alignment_offset = 0;
600         BOOL overflow = False;
601
602         /* Modify the data_to_send and datasize and set the error if
603            we're trying to send more than max_data_bytes. We still send
604            the part of the packet(s) that fit. Strange, but needed
605            for OS/2. */
606
607         if (max_data_bytes > 0 && datasize > max_data_bytes) {
608                 DEBUG(5,("send_trans2_replies: max_data_bytes %d exceeded by data %d\n",
609                         max_data_bytes, datasize ));
610                 datasize = data_to_send = max_data_bytes;
611                 overflow = True;
612         }
613
614         /* If there genuinely are no parameters or data to send just send the empty packet */
615
616         if(params_to_send == 0 && data_to_send == 0) {
617                 reply_outbuf(req, 10, 0);
618                 show_msg((char *)req->outbuf);
619                 return;
620         }
621
622         /* When sending params and data ensure that both are nicely aligned */
623         /* Only do this alignment when there is also data to send - else
624                 can cause NT redirector problems. */
625
626         if (((params_to_send % 4) != 0) && (data_to_send != 0))
627                 data_alignment_offset = 4 - (params_to_send % 4);
628
629         /* Space is bufsize minus Netbios over TCP header minus SMB header */
630         /* The alignment_offset is to align the param bytes on an even byte
631                 boundary. NT 4.0 Beta needs this to work correctly. */
632
633         useable_space = max_send - (smb_size
634                                     + 2 * 10 /* wct */
635                                     + alignment_offset
636                                     + data_alignment_offset);
637
638         /* useable_space can never be more than max_send minus the alignment offset. */
639
640         useable_space = MIN(useable_space, max_send - (alignment_offset+data_alignment_offset));
641
642         while (params_to_send || data_to_send) {
643                 /* Calculate whether we will totally or partially fill this packet */
644
645                 total_sent_thistime = params_to_send + data_to_send + alignment_offset + data_alignment_offset;
646
647                 /* We can never send more than useable_space */
648                 /*
649                  * Note that 'useable_space' does not include the alignment offsets,
650                  * but we must include the alignment offsets in the calculation of
651                  * the length of the data we send over the wire, as the alignment offsets
652                  * are sent here. Fix from Marc_Jacobsen@hp.com.
653                  */
654
655                 total_sent_thistime = MIN(total_sent_thistime, useable_space+ alignment_offset + data_alignment_offset);
656
657                 reply_outbuf(req, 10, total_sent_thistime);
658
659                 /* Set total params and data to be sent */
660                 SSVAL(req->outbuf,smb_tprcnt,paramsize);
661                 SSVAL(req->outbuf,smb_tdrcnt,datasize);
662
663                 /* Calculate how many parameters and data we can fit into
664                  * this packet. Parameters get precedence
665                  */
666
667                 params_sent_thistime = MIN(params_to_send,useable_space);
668                 data_sent_thistime = useable_space - params_sent_thistime;
669                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
670
671                 SSVAL(req->outbuf,smb_prcnt, params_sent_thistime);
672
673                 /* smb_proff is the offset from the start of the SMB header to the
674                         parameter bytes, however the first 4 bytes of outbuf are
675                         the Netbios over TCP header. Thus use smb_base() to subtract
676                         them from the calculation */
677
678                 SSVAL(req->outbuf,smb_proff,
679                       ((smb_buf(req->outbuf)+alignment_offset)
680                        - smb_base(req->outbuf)));
681
682                 if(params_sent_thistime == 0)
683                         SSVAL(req->outbuf,smb_prdisp,0);
684                 else
685                         /* Absolute displacement of param bytes sent in this packet */
686                         SSVAL(req->outbuf,smb_prdisp,pp - params);
687
688                 SSVAL(req->outbuf,smb_drcnt, data_sent_thistime);
689                 if(data_sent_thistime == 0) {
690                         SSVAL(req->outbuf,smb_droff,0);
691                         SSVAL(req->outbuf,smb_drdisp, 0);
692                 } else {
693                         /* The offset of the data bytes is the offset of the
694                                 parameter bytes plus the number of parameters being sent this time */
695                         SSVAL(req->outbuf, smb_droff,
696                               ((smb_buf(req->outbuf)+alignment_offset)
697                                - smb_base(req->outbuf))
698                               + params_sent_thistime + data_alignment_offset);
699                         SSVAL(req->outbuf,smb_drdisp, pd - pdata);
700                 }
701
702                 /* Initialize the padding for alignment */
703
704                 if (alignment_offset != 0) {
705                         memset(smb_buf(req->outbuf), 0, alignment_offset);
706                 }
707
708                 /* Copy the param bytes into the packet */
709
710                 if(params_sent_thistime) {
711                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
712                                params_sent_thistime);
713                 }
714
715                 /* Copy in the data bytes */
716                 if(data_sent_thistime) {
717                         if (data_alignment_offset != 0) {
718                                 memset((smb_buf(req->outbuf)+alignment_offset+
719                                         params_sent_thistime), 0,
720                                        data_alignment_offset);
721                         }
722                         memcpy(smb_buf(req->outbuf)+alignment_offset
723                                +params_sent_thistime+data_alignment_offset,
724                                pd,data_sent_thistime);
725                 }
726
727                 DEBUG(9,("t2_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
728                         params_sent_thistime, data_sent_thistime, useable_space));
729                 DEBUG(9,("t2_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
730                         params_to_send, data_to_send, paramsize, datasize));
731
732                 if (overflow) {
733                         error_packet_set((char *)req->outbuf,
734                                          ERRDOS,ERRbufferoverflow,
735                                          STATUS_BUFFER_OVERFLOW,
736                                          __LINE__,__FILE__);
737                 }
738
739                 /* Send the packet */
740                 show_msg((char *)req->outbuf);
741                 if (!send_smb(smbd_server_fd(),(char *)req->outbuf))
742                         exit_server_cleanly("send_trans2_replies: send_smb failed.");
743
744                 TALLOC_FREE(req->outbuf);
745
746                 pp += params_sent_thistime;
747                 pd += data_sent_thistime;
748
749                 params_to_send -= params_sent_thistime;
750                 data_to_send -= data_sent_thistime;
751
752                 /* Sanity check */
753                 if(params_to_send < 0 || data_to_send < 0) {
754                         DEBUG(0,("send_trans2_replies failed sanity check pts = %d, dts = %d\n!!!",
755                                 params_to_send, data_to_send));
756                         return;
757                 }
758         }
759
760         return;
761 }
762
763 /****************************************************************************
764  Reply to a TRANSACT2_OPEN.
765 ****************************************************************************/
766
767 static void call_trans2open(connection_struct *conn,
768                             struct smb_request *req,
769                             char **pparams, int total_params,
770                             char **ppdata, int total_data,
771                             unsigned int max_data_bytes)
772 {
773         char *params = *pparams;
774         char *pdata = *ppdata;
775         int deny_mode;
776         int32 open_attr;
777         BOOL oplock_request;
778 #if 0
779         BOOL return_additional_info;
780         int16 open_sattr;
781         time_t open_time;
782 #endif
783         int open_ofun;
784         uint32 open_size;
785         char *pname;
786         char *fname = NULL;
787         SMB_OFF_T size=0;
788         int fattr=0,mtime=0;
789         SMB_INO_T inode = 0;
790         SMB_STRUCT_STAT sbuf;
791         int smb_action = 0;
792         files_struct *fsp;
793         struct ea_list *ea_list = NULL;
794         uint16 flags = 0;
795         NTSTATUS status;
796         uint32 access_mask;
797         uint32 share_mode;
798         uint32 create_disposition;
799         uint32 create_options = 0;
800         TALLOC_CTX *ctx = talloc_tos();
801
802         /*
803          * Ensure we have enough parameters to perform the operation.
804          */
805
806         if (total_params < 29) {
807                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
808                 return;
809         }
810
811         flags = SVAL(params, 0);
812         deny_mode = SVAL(params, 2);
813         open_attr = SVAL(params,6);
814         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
815         if (oplock_request) {
816                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
817         }
818
819 #if 0
820         return_additional_info = BITSETW(params,0);
821         open_sattr = SVAL(params, 4);
822         open_time = make_unix_date3(params+8);
823 #endif
824         open_ofun = SVAL(params,12);
825         open_size = IVAL(params,14);
826         pname = &params[28];
827
828         if (IS_IPC(conn)) {
829                 reply_doserror(req, ERRSRV, ERRaccess);
830                 return;
831         }
832
833         srvstr_get_path(ctx, params, req->flags2, &fname, pname,
834                         total_params - 28, STR_TERMINATE,
835                         &status);
836         if (!NT_STATUS_IS_OK(status)) {
837                 reply_nterror(req, status);
838                 return;
839         }
840
841         DEBUG(3,("call_trans2open %s deny_mode=0x%x attr=%d ofun=0x%x size=%d\n",
842                 fname, (unsigned int)deny_mode, (unsigned int)open_attr,
843                 (unsigned int)open_ofun, open_size));
844
845         /* XXXX we need to handle passed times, sattr and flags */
846
847         status = unix_convert(conn, fname, False, &fname, NULL, &sbuf);
848         if (!NT_STATUS_IS_OK(status)) {
849                 reply_nterror(req, status);
850                 return;
851         }
852
853         status = check_name(conn, fname);
854         if (!NT_STATUS_IS_OK(status)) {
855                 reply_nterror(req, status);
856                 return;
857         }
858
859         if (open_ofun == 0) {
860                 reply_nterror(req, NT_STATUS_OBJECT_NAME_COLLISION);
861                 return;
862         }
863
864         if (!map_open_params_to_ntcreate(fname, deny_mode, open_ofun,
865                                 &access_mask,
866                                 &share_mode,
867                                 &create_disposition,
868                                 &create_options)) {
869                 reply_doserror(req, ERRDOS, ERRbadaccess);
870                 return;
871         }
872
873         /* Any data in this call is an EA list. */
874         if (total_data && (total_data != 4) && !lp_ea_support(SNUM(conn))) {
875                 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
876                 return;
877         }
878
879         if (total_data != 4) {
880                 if (total_data < 10) {
881                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
882                         return;
883                 }
884
885                 if (IVAL(pdata,0) > total_data) {
886                         DEBUG(10,("call_trans2open: bad total data size (%u) > %u\n",
887                                 IVAL(pdata,0), (unsigned int)total_data));
888                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
889                         return;
890                 }
891
892                 ea_list = read_ea_list(talloc_tos(), pdata + 4,
893                                        total_data - 4);
894                 if (!ea_list) {
895                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
896                         return;
897                 }
898         } else if (IVAL(pdata,0) != 4) {
899                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
900                 return;
901         }
902
903         status = open_file_ntcreate(conn, req, fname, &sbuf,
904                 access_mask,
905                 share_mode,
906                 create_disposition,
907                 create_options,
908                 open_attr,
909                 oplock_request,
910                 &smb_action, &fsp);
911
912         if (!NT_STATUS_IS_OK(status)) {
913                 if (open_was_deferred(req->mid)) {
914                         /* We have re-scheduled this call. */
915                         return;
916                 }
917                 reply_openerror(req, status);
918                 return;
919         }
920
921         size = get_file_size(sbuf);
922         fattr = dos_mode(conn,fname,&sbuf);
923         mtime = sbuf.st_mtime;
924         inode = sbuf.st_ino;
925         if (fattr & aDIR) {
926                 close_file(fsp,ERROR_CLOSE);
927                 reply_doserror(req, ERRDOS,ERRnoaccess);
928                 return;
929         }
930
931         /* Save the requested allocation size. */
932         /* Allocate space for the file if a size hint is supplied */
933         if ((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) {
934                 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)open_size;
935                 if (allocation_size && (allocation_size > (SMB_BIG_UINT)size)) {
936                         fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
937                         if (fsp->is_directory) {
938                                 close_file(fsp,ERROR_CLOSE);
939                                 /* Can't set allocation size on a directory. */
940                                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
941                                 return;
942                         }
943                         if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
944                                 close_file(fsp,ERROR_CLOSE);
945                                 reply_nterror(req, NT_STATUS_DISK_FULL);
946                                 return;
947                         }
948
949                         /* Adjust size here to return the right size in the reply.
950                            Windows does it this way. */
951                         size = fsp->initial_allocation_size;
952                 } else {
953                         fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)size);
954                 }
955         }
956
957         if (ea_list && smb_action == FILE_WAS_CREATED) {
958                 status = set_ea(conn, fsp, fname, ea_list);
959                 if (!NT_STATUS_IS_OK(status)) {
960                         close_file(fsp,ERROR_CLOSE);
961                         reply_nterror(req, status);
962                         return;
963                 }
964         }
965
966         /* Realloc the size of parameters and data we will return */
967         *pparams = (char *)SMB_REALLOC(*pparams, 30);
968         if(*pparams == NULL ) {
969                 reply_nterror(req, NT_STATUS_NO_MEMORY);
970                 return;
971         }
972         params = *pparams;
973
974         SSVAL(params,0,fsp->fnum);
975         SSVAL(params,2,fattr);
976         srv_put_dos_date2(params,4, mtime);
977         SIVAL(params,8, (uint32)size);
978         SSVAL(params,12,deny_mode);
979         SSVAL(params,14,0); /* open_type - file or directory. */
980         SSVAL(params,16,0); /* open_state - only valid for IPC device. */
981
982         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
983                 smb_action |= EXTENDED_OPLOCK_GRANTED;
984         }
985
986         SSVAL(params,18,smb_action);
987
988         /*
989          * WARNING - this may need to be changed if SMB_INO_T <> 4 bytes.
990          */
991         SIVAL(params,20,inode);
992         SSVAL(params,24,0); /* Padding. */
993         if (flags & 8) {
994                 uint32 ea_size = estimate_ea_size(conn, fsp, fname);
995                 SIVAL(params, 26, ea_size);
996         } else {
997                 SIVAL(params, 26, 0);
998         }
999
1000         /* Send the required number of replies */
1001         send_trans2_replies(req, params, 30, *ppdata, 0, max_data_bytes);
1002 }
1003
1004 /*********************************************************
1005  Routine to check if a given string matches exactly.
1006  as a special case a mask of "." does NOT match. That
1007  is required for correct wildcard semantics
1008  Case can be significant or not.
1009 **********************************************************/
1010
1011 static BOOL exact_match(connection_struct *conn, char *str, char *mask)
1012 {
1013         if (mask[0] == '.' && mask[1] == 0)
1014                 return False;
1015         if (conn->case_sensitive)
1016                 return strcmp(str,mask)==0;
1017         if (StrCaseCmp(str,mask) != 0) {
1018                 return False;
1019         }
1020         if (dptr_has_wild(conn->dirptr)) {
1021                 return False;
1022         }
1023         return True;
1024 }
1025
1026 /****************************************************************************
1027  Return the filetype for UNIX extensions.
1028 ****************************************************************************/
1029
1030 static uint32 unix_filetype(mode_t mode)
1031 {
1032         if(S_ISREG(mode))
1033                 return UNIX_TYPE_FILE;
1034         else if(S_ISDIR(mode))
1035                 return UNIX_TYPE_DIR;
1036 #ifdef S_ISLNK
1037         else if(S_ISLNK(mode))
1038                 return UNIX_TYPE_SYMLINK;
1039 #endif
1040 #ifdef S_ISCHR
1041         else if(S_ISCHR(mode))
1042                 return UNIX_TYPE_CHARDEV;
1043 #endif
1044 #ifdef S_ISBLK
1045         else if(S_ISBLK(mode))
1046                 return UNIX_TYPE_BLKDEV;
1047 #endif
1048 #ifdef S_ISFIFO
1049         else if(S_ISFIFO(mode))
1050                 return UNIX_TYPE_FIFO;
1051 #endif
1052 #ifdef S_ISSOCK
1053         else if(S_ISSOCK(mode))
1054                 return UNIX_TYPE_SOCKET;
1055 #endif
1056
1057         DEBUG(0,("unix_filetype: unknown filetype %u", (unsigned)mode));
1058         return UNIX_TYPE_UNKNOWN;
1059 }
1060
1061 /****************************************************************************
1062  Map wire perms onto standard UNIX permissions. Obey share restrictions.
1063 ****************************************************************************/
1064
1065 enum perm_type { PERM_NEW_FILE, PERM_NEW_DIR, PERM_EXISTING_FILE, PERM_EXISTING_DIR};
1066
1067 static NTSTATUS unix_perms_from_wire( connection_struct *conn,
1068                                 SMB_STRUCT_STAT *psbuf,
1069                                 uint32 perms,
1070                                 enum perm_type ptype,
1071                                 mode_t *ret_perms)
1072 {
1073         mode_t ret = 0;
1074
1075         if (perms == SMB_MODE_NO_CHANGE) {
1076                 if (!VALID_STAT(*psbuf)) {
1077                         return NT_STATUS_INVALID_PARAMETER;
1078                 } else {
1079                         *ret_perms = psbuf->st_mode;
1080                         return NT_STATUS_OK;
1081                 }
1082         }
1083
1084         ret |= ((perms & UNIX_X_OTH ) ? S_IXOTH : 0);
1085         ret |= ((perms & UNIX_W_OTH ) ? S_IWOTH : 0);
1086         ret |= ((perms & UNIX_R_OTH ) ? S_IROTH : 0);
1087         ret |= ((perms & UNIX_X_GRP ) ? S_IXGRP : 0);
1088         ret |= ((perms & UNIX_W_GRP ) ? S_IWGRP : 0);
1089         ret |= ((perms & UNIX_R_GRP ) ? S_IRGRP : 0);
1090         ret |= ((perms & UNIX_X_USR ) ? S_IXUSR : 0);
1091         ret |= ((perms & UNIX_W_USR ) ? S_IWUSR : 0);
1092         ret |= ((perms & UNIX_R_USR ) ? S_IRUSR : 0);
1093 #ifdef S_ISVTX
1094         ret |= ((perms & UNIX_STICKY ) ? S_ISVTX : 0);
1095 #endif
1096 #ifdef S_ISGID
1097         ret |= ((perms & UNIX_SET_GID ) ? S_ISGID : 0);
1098 #endif
1099 #ifdef S_ISUID
1100         ret |= ((perms & UNIX_SET_UID ) ? S_ISUID : 0);
1101 #endif
1102
1103         switch (ptype) {
1104         case PERM_NEW_FILE:
1105                 /* Apply mode mask */
1106                 ret &= lp_create_mask(SNUM(conn));
1107                 /* Add in force bits */
1108                 ret |= lp_force_create_mode(SNUM(conn));
1109                 break;
1110         case PERM_NEW_DIR:
1111                 ret &= lp_dir_mask(SNUM(conn));
1112                 /* Add in force bits */
1113                 ret |= lp_force_dir_mode(SNUM(conn));
1114                 break;
1115         case PERM_EXISTING_FILE:
1116                 /* Apply mode mask */
1117                 ret &= lp_security_mask(SNUM(conn));
1118                 /* Add in force bits */
1119                 ret |= lp_force_security_mode(SNUM(conn));
1120                 break;
1121         case PERM_EXISTING_DIR:
1122                 /* Apply mode mask */
1123                 ret &= lp_dir_security_mask(SNUM(conn));
1124                 /* Add in force bits */
1125                 ret |= lp_force_dir_security_mode(SNUM(conn));
1126                 break;
1127         }
1128
1129         *ret_perms = ret;
1130         return NT_STATUS_OK;
1131 }
1132
1133 /****************************************************************************
1134  Get a level dependent lanman2 dir entry.
1135 ****************************************************************************/
1136
1137 static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
1138                                  char *path_mask,uint32 dirtype,int info_level,
1139                                  int requires_resume_key,
1140                                  BOOL dont_descend,char **ppdata, 
1141                                  char *base_data, char *end_data,
1142                                  int space_remaining,
1143                                  BOOL *out_of_space, BOOL *got_exact_match,
1144                                  int *last_entry_off, struct ea_list *name_list, TALLOC_CTX *ea_ctx)
1145 {
1146         const char *dname;
1147         BOOL found = False;
1148         SMB_STRUCT_STAT sbuf;
1149         pstring mask;
1150         pstring pathreal;
1151         pstring fname;
1152         char *p, *q, *pdata = *ppdata;
1153         uint32 reskey=0;
1154         long prev_dirpos=0;
1155         uint32 mode=0;
1156         SMB_OFF_T file_size = 0;
1157         SMB_BIG_UINT allocation_size = 0;
1158         uint32 len;
1159         struct timespec mdate_ts, adate_ts, create_date_ts;
1160         time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
1161         char *nameptr;
1162         char *last_entry_ptr;
1163         BOOL was_8_3;
1164         uint32 nt_extmode; /* Used for NT connections instead of mode */
1165         BOOL needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
1166         BOOL check_mangled_names = lp_manglednames(conn->params);
1167         char mangled_name[13]; /* mangled 8.3 name. */
1168
1169         *fname = 0;
1170         *out_of_space = False;
1171         *got_exact_match = False;
1172
1173         ZERO_STRUCT(mdate_ts);
1174         ZERO_STRUCT(adate_ts);
1175         ZERO_STRUCT(create_date_ts);
1176
1177         if (!conn->dirptr)
1178                 return(False);
1179
1180         p = strrchr_m(path_mask,'/');
1181         if(p != NULL) {
1182                 if(p[1] == '\0')
1183                         pstrcpy(mask,"*.*");
1184                 else
1185                         pstrcpy(mask, p+1);
1186         } else
1187                 pstrcpy(mask, path_mask);
1188
1189
1190         while (!found) {
1191                 BOOL got_match;
1192                 BOOL ms_dfs_link = False;
1193
1194                 /* Needed if we run out of space */
1195                 long curr_dirpos = prev_dirpos = dptr_TellDir(conn->dirptr);
1196                 dname = dptr_ReadDirName(conn->dirptr,&curr_dirpos,&sbuf);
1197
1198                 /*
1199                  * Due to bugs in NT client redirectors we are not using
1200                  * resume keys any more - set them to zero.
1201                  * Check out the related comments in findfirst/findnext.
1202                  * JRA.
1203                  */
1204
1205                 reskey = 0;
1206
1207                 DEBUG(8,("get_lanman2_dir_entry:readdir on dirptr 0x%lx now at offset %ld\n",
1208                         (long)conn->dirptr,curr_dirpos));
1209       
1210                 if (!dname) {
1211                         return(False);
1212                 }
1213
1214                 /*
1215                  * fname may get mangled, dname is never mangled.
1216                  * Whenever we're accessing the filesystem we use
1217                  * pathreal which is composed from dname.
1218                  */
1219
1220                 pstrcpy(fname,dname);
1221
1222                 /* Mangle fname if it's an illegal name. */
1223                 if (mangle_must_mangle(fname,conn->params)) {
1224                         if (!name_to_8_3(fname,mangled_name,True,conn->params)) {
1225                                 continue; /* Error - couldn't mangle. */
1226                         }
1227                         pstrcpy(fname,mangled_name);
1228                 }
1229
1230                 if(!(got_match = *got_exact_match = exact_match(conn, fname, mask))) {
1231                         got_match = mask_match(fname, mask, conn->case_sensitive);
1232                 }
1233
1234                 if(!got_match && check_mangled_names &&
1235                    !mangle_is_8_3(fname, False, conn->params)) {
1236                         /*
1237                          * It turns out that NT matches wildcards against
1238                          * both long *and* short names. This may explain some
1239                          * of the wildcard wierdness from old DOS clients
1240                          * that some people have been seeing.... JRA.
1241                          */
1242                         /* Force the mangling into 8.3. */
1243                         if (!name_to_8_3( fname, mangled_name, False, conn->params)) {
1244                                 continue; /* Error - couldn't mangle. */
1245                         }
1246
1247                         if(!(got_match = *got_exact_match = exact_match(conn, mangled_name, mask))) {
1248                                 got_match = mask_match(mangled_name, mask, conn->case_sensitive);
1249                         }
1250                 }
1251
1252                 if (got_match) {
1253                         BOOL isdots = (strequal(dname,"..") || strequal(dname,"."));
1254                         if (dont_descend && !isdots) {
1255                                 continue;
1256                         }
1257           
1258                         pstrcpy(pathreal,conn->dirpath);
1259                         if(needslash) {
1260                                 pstrcat(pathreal,"/");
1261                         }
1262                         pstrcat(pathreal,dname);
1263
1264                         if (INFO_LEVEL_IS_UNIX(info_level)) {
1265                                 if (SMB_VFS_LSTAT(conn,pathreal,&sbuf) != 0) {
1266                                         DEBUG(5,("get_lanman2_dir_entry:Couldn't lstat [%s] (%s)\n",
1267                                                 pathreal,strerror(errno)));
1268                                         continue;
1269                                 }
1270                         } else if (!VALID_STAT(sbuf) && SMB_VFS_STAT(conn,pathreal,&sbuf) != 0) {
1271                                 /* Needed to show the msdfs symlinks as
1272                                  * directories */
1273
1274                                 if(lp_host_msdfs() &&
1275                                    lp_msdfs_root(SNUM(conn)) &&
1276                                    ((ms_dfs_link = is_msdfs_link(conn, pathreal, &sbuf)) == True)) {
1277                                         DEBUG(5,("get_lanman2_dir_entry: Masquerading msdfs link %s "
1278                                                 "as a directory\n",
1279                                                 pathreal));
1280                                         sbuf.st_mode = (sbuf.st_mode & 0xFFF) | S_IFDIR;
1281
1282                                 } else {
1283
1284                                         DEBUG(5,("get_lanman2_dir_entry:Couldn't stat [%s] (%s)\n",
1285                                                 pathreal,strerror(errno)));
1286                                         continue;
1287                                 }
1288                         }
1289
1290                         if (ms_dfs_link) {
1291                                 mode = dos_mode_msdfs(conn,pathreal,&sbuf);
1292                         } else {
1293                                 mode = dos_mode(conn,pathreal,&sbuf);
1294                         }
1295
1296                         if (!dir_check_ftype(conn,mode,dirtype)) {
1297                                 DEBUG(5,("get_lanman2_dir_entry: [%s] attribs didn't match %x\n",fname,dirtype));
1298                                 continue;
1299                         }
1300
1301                         if (!(mode & aDIR)) {
1302                                 file_size = get_file_size(sbuf);
1303                         }
1304                         allocation_size = get_allocation_size(conn,NULL,&sbuf);
1305
1306                         mdate_ts = get_mtimespec(&sbuf);
1307                         adate_ts = get_atimespec(&sbuf);
1308                         create_date_ts = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1309
1310                         if (lp_dos_filetime_resolution(SNUM(conn))) {
1311                                 dos_filetime_timespec(&create_date_ts);
1312                                 dos_filetime_timespec(&mdate_ts);
1313                                 dos_filetime_timespec(&adate_ts);
1314                         }
1315
1316                         create_date = convert_timespec_to_time_t(create_date_ts);
1317                         mdate = convert_timespec_to_time_t(mdate_ts);
1318                         adate = convert_timespec_to_time_t(adate_ts);
1319                         
1320                         DEBUG(5,("get_lanman2_dir_entry: found %s fname=%s\n",pathreal,fname));
1321           
1322                         found = True;
1323
1324                         dptr_DirCacheAdd(conn->dirptr, dname, curr_dirpos);
1325                 }
1326         }
1327
1328         p = pdata;
1329         last_entry_ptr = p;
1330
1331         nt_extmode = mode ? mode : FILE_ATTRIBUTE_NORMAL;
1332
1333         switch (info_level) {
1334                 case SMB_FIND_INFO_STANDARD:
1335                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_INFO_STANDARD\n"));
1336                         if(requires_resume_key) {
1337                                 SIVAL(p,0,reskey);
1338                                 p += 4;
1339                         }
1340                         srv_put_dos_date2(p,0,create_date);
1341                         srv_put_dos_date2(p,4,adate);
1342                         srv_put_dos_date2(p,8,mdate);
1343                         SIVAL(p,12,(uint32)file_size);
1344                         SIVAL(p,16,(uint32)allocation_size);
1345                         SSVAL(p,20,mode);
1346                         p += 23;
1347                         nameptr = p;
1348                         p += align_string(pdata, p, 0);
1349                         len = srvstr_push(base_data, flags2, p,
1350                                           fname, PTR_DIFF(end_data, p),
1351                                           STR_TERMINATE);
1352                         if (flags2 & FLAGS2_UNICODE_STRINGS) {
1353                                 if (len > 2) {
1354                                         SCVAL(nameptr, -1, len - 2);
1355                                 } else {
1356                                         SCVAL(nameptr, -1, 0);
1357                                 }
1358                         } else {
1359                                 if (len > 1) {
1360                                         SCVAL(nameptr, -1, len - 1);
1361                                 } else {
1362                                         SCVAL(nameptr, -1, 0);
1363                                 }
1364                         }
1365                         p += len;
1366                         break;
1367
1368                 case SMB_FIND_EA_SIZE:
1369                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_SIZE\n"));
1370                         if(requires_resume_key) {
1371                                 SIVAL(p,0,reskey);
1372                                 p += 4;
1373                         }
1374                         srv_put_dos_date2(p,0,create_date);
1375                         srv_put_dos_date2(p,4,adate);
1376                         srv_put_dos_date2(p,8,mdate);
1377                         SIVAL(p,12,(uint32)file_size);
1378                         SIVAL(p,16,(uint32)allocation_size);
1379                         SSVAL(p,20,mode);
1380                         {
1381                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1382                                 SIVAL(p,22,ea_size); /* Extended attributes */
1383                         }
1384                         p += 27;
1385                         nameptr = p - 1;
1386                         len = srvstr_push(base_data, flags2,
1387                                           p, fname, PTR_DIFF(end_data, p),
1388                                           STR_TERMINATE | STR_NOALIGN);
1389                         if (flags2 & FLAGS2_UNICODE_STRINGS) {
1390                                 if (len > 2) {
1391                                         len -= 2;
1392                                 } else {
1393                                         len = 0;
1394                                 }
1395                         } else {
1396                                 if (len > 1) {
1397                                         len -= 1;
1398                                 } else {
1399                                         len = 0;
1400                                 }
1401                         }
1402                         SCVAL(nameptr,0,len);
1403                         p += len;
1404                         SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1405                         break;
1406
1407                 case SMB_FIND_EA_LIST:
1408                 {
1409                         struct ea_list *file_list = NULL;
1410                         size_t ea_len = 0;
1411
1412                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_LIST\n"));
1413                         if (!name_list) {
1414                                 return False;
1415                         }
1416                         if(requires_resume_key) {
1417                                 SIVAL(p,0,reskey);
1418                                 p += 4;
1419                         }
1420                         srv_put_dos_date2(p,0,create_date);
1421                         srv_put_dos_date2(p,4,adate);
1422                         srv_put_dos_date2(p,8,mdate);
1423                         SIVAL(p,12,(uint32)file_size);
1424                         SIVAL(p,16,(uint32)allocation_size);
1425                         SSVAL(p,20,mode);
1426                         p += 22; /* p now points to the EA area. */
1427
1428                         file_list = get_ea_list_from_file(ea_ctx, conn, NULL, pathreal, &ea_len);
1429                         name_list = ea_list_union(name_list, file_list, &ea_len);
1430
1431                         /* We need to determine if this entry will fit in the space available. */
1432                         /* Max string size is 255 bytes. */
1433                         if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
1434                                 /* Move the dirptr back to prev_dirpos */
1435                                 dptr_SeekDir(conn->dirptr, prev_dirpos);
1436                                 *out_of_space = True;
1437                                 DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
1438                                 return False; /* Not finished - just out of space */
1439                         }
1440
1441                         /* Push the ea_data followed by the name. */
1442                         p += fill_ea_buffer(ea_ctx, p, space_remaining, conn, name_list);
1443                         nameptr = p;
1444                         len = srvstr_push(base_data, flags2,
1445                                           p + 1, fname, PTR_DIFF(end_data, p),
1446                                           STR_TERMINATE | STR_NOALIGN);
1447                         if (flags2 & FLAGS2_UNICODE_STRINGS) {
1448                                 if (len > 2) {
1449                                         len -= 2;
1450                                 } else {
1451                                         len = 0;
1452                                 }
1453                         } else {
1454                                 if (len > 1) {
1455                                         len -= 1;
1456                                 } else {
1457                                         len = 0;
1458                                 }
1459                         }
1460                         SCVAL(nameptr,0,len);
1461                         p += len + 1;
1462                         SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1463                         break;
1464                 }
1465
1466                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1467                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
1468                         was_8_3 = mangle_is_8_3(fname, True, conn->params);
1469                         p += 4;
1470                         SIVAL(p,0,reskey); p += 4;
1471                         put_long_date_timespec(p,create_date_ts); p += 8;
1472                         put_long_date_timespec(p,adate_ts); p += 8;
1473                         put_long_date_timespec(p,mdate_ts); p += 8;
1474                         put_long_date_timespec(p,mdate_ts); p += 8;
1475                         SOFF_T(p,0,file_size); p += 8;
1476                         SOFF_T(p,0,allocation_size); p += 8;
1477                         SIVAL(p,0,nt_extmode); p += 4;
1478                         q = p; p += 4; /* q is placeholder for name length. */
1479                         {
1480                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1481                                 SIVAL(p,0,ea_size); /* Extended attributes */
1482                                 p += 4;
1483                         }
1484                         /* Clear the short name buffer. This is
1485                          * IMPORTANT as not doing so will trigger
1486                          * a Win2k client bug. JRA.
1487                          */
1488                         if (!was_8_3 && check_mangled_names) {
1489                                 if (!name_to_8_3(fname,mangled_name,True,
1490                                                    conn->params)) {
1491                                         /* Error - mangle failed ! */
1492                                         memset(mangled_name,'\0',12);
1493                                 }
1494                                 mangled_name[12] = 0;
1495                                 len = srvstr_push(base_data, flags2,
1496                                                   p+2, mangled_name, 24,
1497                                                   STR_UPPER|STR_UNICODE);
1498                                 if (len < 24) {
1499                                         memset(p + 2 + len,'\0',24 - len);
1500                                 }
1501                                 SSVAL(p, 0, len);
1502                         } else {
1503                                 memset(p,'\0',26);
1504                         }
1505                         p += 2 + 24;
1506                         len = srvstr_push(base_data, flags2, p,
1507                                           fname, PTR_DIFF(end_data, p),
1508                                           STR_TERMINATE_ASCII);
1509                         SIVAL(q,0,len);
1510                         p += len;
1511                         SIVAL(p,0,0); /* Ensure any padding is null. */
1512                         len = PTR_DIFF(p, pdata);
1513                         len = (len + 3) & ~3;
1514                         SIVAL(pdata,0,len);
1515                         p = pdata + len;
1516                         break;
1517
1518                 case SMB_FIND_FILE_DIRECTORY_INFO:
1519                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
1520                         p += 4;
1521                         SIVAL(p,0,reskey); p += 4;
1522                         put_long_date_timespec(p,create_date_ts); p += 8;
1523                         put_long_date_timespec(p,adate_ts); p += 8;
1524                         put_long_date_timespec(p,mdate_ts); p += 8;
1525                         put_long_date_timespec(p,mdate_ts); p += 8;
1526                         SOFF_T(p,0,file_size); p += 8;
1527                         SOFF_T(p,0,allocation_size); p += 8;
1528                         SIVAL(p,0,nt_extmode); p += 4;
1529                         len = srvstr_push(base_data, flags2,
1530                                           p + 4, fname, PTR_DIFF(end_data, p),
1531                                           STR_TERMINATE_ASCII);
1532                         SIVAL(p,0,len);
1533                         p += 4 + len;
1534                         SIVAL(p,0,0); /* Ensure any padding is null. */
1535                         len = PTR_DIFF(p, pdata);
1536                         len = (len + 3) & ~3;
1537                         SIVAL(pdata,0,len);
1538                         p = pdata + len;
1539                         break;
1540       
1541                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1542                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
1543                         p += 4;
1544                         SIVAL(p,0,reskey); p += 4;
1545                         put_long_date_timespec(p,create_date_ts); p += 8;
1546                         put_long_date_timespec(p,adate_ts); p += 8;
1547                         put_long_date_timespec(p,mdate_ts); p += 8;
1548                         put_long_date_timespec(p,mdate_ts); p += 8;
1549                         SOFF_T(p,0,file_size); p += 8;
1550                         SOFF_T(p,0,allocation_size); p += 8;
1551                         SIVAL(p,0,nt_extmode); p += 4;
1552                         q = p; p += 4; /* q is placeholder for name length. */
1553                         {
1554                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1555                                 SIVAL(p,0,ea_size); /* Extended attributes */
1556                                 p +=4;
1557                         }
1558                         len = srvstr_push(base_data, flags2, p,
1559                                           fname, PTR_DIFF(end_data, p),
1560                                           STR_TERMINATE_ASCII);
1561                         SIVAL(q, 0, len);
1562                         p += len;
1563
1564                         SIVAL(p,0,0); /* Ensure any padding is null. */
1565                         len = PTR_DIFF(p, pdata);
1566                         len = (len + 3) & ~3;
1567                         SIVAL(pdata,0,len);
1568                         p = pdata + len;
1569                         break;
1570
1571                 case SMB_FIND_FILE_NAMES_INFO:
1572                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
1573                         p += 4;
1574                         SIVAL(p,0,reskey); p += 4;
1575                         p += 4;
1576                         /* this must *not* be null terminated or w2k gets in a loop trying to set an
1577                            acl on a dir (tridge) */
1578                         len = srvstr_push(base_data, flags2, p,
1579                                           fname, PTR_DIFF(end_data, p),
1580                                           STR_TERMINATE_ASCII);
1581                         SIVAL(p, -4, len);
1582                         p += len;
1583                         SIVAL(p,0,0); /* Ensure any padding is null. */
1584                         len = PTR_DIFF(p, pdata);
1585                         len = (len + 3) & ~3;
1586                         SIVAL(pdata,0,len);
1587                         p = pdata + len;
1588                         break;
1589
1590                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1591                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
1592                         p += 4;
1593                         SIVAL(p,0,reskey); p += 4;
1594                         put_long_date_timespec(p,create_date_ts); p += 8;
1595                         put_long_date_timespec(p,adate_ts); p += 8;
1596                         put_long_date_timespec(p,mdate_ts); p += 8;
1597                         put_long_date_timespec(p,mdate_ts); p += 8;
1598                         SOFF_T(p,0,file_size); p += 8;
1599                         SOFF_T(p,0,allocation_size); p += 8;
1600                         SIVAL(p,0,nt_extmode); p += 4;
1601                         q = p; p += 4; /* q is placeholder for name length. */
1602                         {
1603                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1604                                 SIVAL(p,0,ea_size); /* Extended attributes */
1605                                 p +=4;
1606                         }
1607                         SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
1608                         SIVAL(p,0,sbuf.st_ino); p += 4; /* FileIndexLow */
1609                         SIVAL(p,0,sbuf.st_dev); p += 4; /* FileIndexHigh */
1610                         len = srvstr_push(base_data, flags2, p,
1611                                           fname, PTR_DIFF(end_data, p),
1612                                           STR_TERMINATE_ASCII);
1613                         SIVAL(q, 0, len);
1614                         p += len; 
1615                         SIVAL(p,0,0); /* Ensure any padding is null. */
1616                         len = PTR_DIFF(p, pdata);
1617                         len = (len + 3) & ~3;
1618                         SIVAL(pdata,0,len);
1619                         p = pdata + len;
1620                         break;
1621
1622                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1623                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
1624                         was_8_3 = mangle_is_8_3(fname, True, conn->params);
1625                         p += 4;
1626                         SIVAL(p,0,reskey); p += 4;
1627                         put_long_date_timespec(p,create_date_ts); p += 8;
1628                         put_long_date_timespec(p,adate_ts); p += 8;
1629                         put_long_date_timespec(p,mdate_ts); p += 8;
1630                         put_long_date_timespec(p,mdate_ts); p += 8;
1631                         SOFF_T(p,0,file_size); p += 8;
1632                         SOFF_T(p,0,allocation_size); p += 8;
1633                         SIVAL(p,0,nt_extmode); p += 4;
1634                         q = p; p += 4; /* q is placeholder for name length */
1635                         {
1636                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1637                                 SIVAL(p,0,ea_size); /* Extended attributes */
1638                                 p +=4;
1639                         }
1640                         /* Clear the short name buffer. This is
1641                          * IMPORTANT as not doing so will trigger
1642                          * a Win2k client bug. JRA.
1643                          */
1644                         if (!was_8_3 && check_mangled_names) {
1645                                 if (!name_to_8_3(fname,mangled_name,True,
1646                                                 conn->params)) {
1647                                         /* Error - mangle failed ! */
1648                                         memset(mangled_name,'\0',12);
1649                                 }
1650                                 mangled_name[12] = 0;
1651                                 len = srvstr_push(base_data, flags2,
1652                                                   p+2, mangled_name, 24,
1653                                                   STR_UPPER|STR_UNICODE);
1654                                 SSVAL(p, 0, len);
1655                                 if (len < 24) {
1656                                         memset(p + 2 + len,'\0',24 - len);
1657                                 }
1658                                 SSVAL(p, 0, len);
1659                         } else {
1660                                 memset(p,'\0',26);
1661                         }
1662                         p += 26;
1663                         SSVAL(p,0,0); p += 2; /* Reserved ? */
1664                         SIVAL(p,0,sbuf.st_ino); p += 4; /* FileIndexLow */
1665                         SIVAL(p,0,sbuf.st_dev); p += 4; /* FileIndexHigh */
1666                         len = srvstr_push(base_data, flags2, p,
1667                                           fname, PTR_DIFF(end_data, p),
1668                                           STR_TERMINATE_ASCII);
1669                         SIVAL(q,0,len);
1670                         p += len;
1671                         SIVAL(p,0,0); /* Ensure any padding is null. */
1672                         len = PTR_DIFF(p, pdata);
1673                         len = (len + 3) & ~3;
1674                         SIVAL(pdata,0,len);
1675                         p = pdata + len;
1676                         break;
1677
1678                 /* CIFS UNIX Extension. */
1679
1680                 case SMB_FIND_FILE_UNIX:
1681                 case SMB_FIND_FILE_UNIX_INFO2:
1682                         p+= 4;
1683                         SIVAL(p,0,reskey); p+= 4;    /* Used for continuing search. */
1684
1685                         /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
1686
1687                         if (info_level == SMB_FIND_FILE_UNIX) {
1688                                 DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX\n"));
1689                                 p = store_file_unix_basic(conn, p,
1690                                                         NULL, &sbuf);
1691                                 len = srvstr_push(base_data, flags2, p,
1692                                                   fname, PTR_DIFF(end_data, p),
1693                                                   STR_TERMINATE);
1694                         } else {
1695                                 DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
1696                                 p = store_file_unix_basic_info2(conn, p,
1697                                                         NULL, &sbuf);
1698                                 nameptr = p;
1699                                 p += 4;
1700                                 len = srvstr_push(base_data, flags2, p, fname,
1701                                                   PTR_DIFF(end_data, p), 0);
1702                                 SIVAL(nameptr, 0, len);
1703                         }
1704
1705                         p += len;
1706                         SIVAL(p,0,0); /* Ensure any padding is null. */
1707
1708                         len = PTR_DIFF(p, pdata);
1709                         len = (len + 3) & ~3;
1710                         SIVAL(pdata,0,len);     /* Offset from this structure to the beginning of the next one */
1711                         p = pdata + len;
1712                         /* End of SMB_QUERY_FILE_UNIX_BASIC */
1713
1714                         break;
1715
1716                 default:      
1717                         return(False);
1718         }
1719
1720
1721         if (PTR_DIFF(p,pdata) > space_remaining) {
1722                 /* Move the dirptr back to prev_dirpos */
1723                 dptr_SeekDir(conn->dirptr, prev_dirpos);
1724                 *out_of_space = True;
1725                 DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
1726                 return False; /* Not finished - just out of space */
1727         }
1728
1729         /* Setup the last entry pointer, as an offset from base_data */
1730         *last_entry_off = PTR_DIFF(last_entry_ptr,base_data);
1731         /* Advance the data pointer to the next slot */
1732         *ppdata = p;
1733
1734         return(found);
1735 }
1736
1737 /****************************************************************************
1738  Reply to a TRANS2_FINDFIRST.
1739 ****************************************************************************/
1740
1741 static void call_trans2findfirst(connection_struct *conn,
1742                                  struct smb_request *req,
1743                                  char **pparams, int total_params,
1744                                  char **ppdata, int total_data,
1745                                  unsigned int max_data_bytes)
1746 {
1747         /* We must be careful here that we don't return more than the
1748                 allowed number of data bytes. If this means returning fewer than
1749                 maxentries then so be it. We assume that the redirector has
1750                 enough room for the fixed number of parameter bytes it has
1751                 requested. */
1752         char *params = *pparams;
1753         char *pdata = *ppdata;
1754         char *data_end;
1755         uint32 dirtype;
1756         int maxentries;
1757         uint16 findfirst_flags;
1758         BOOL close_after_first;
1759         BOOL close_if_end;
1760         BOOL requires_resume_key;
1761         int info_level;
1762         char *directory = NULL;
1763         pstring mask;
1764         char *p;
1765         int last_entry_off=0;
1766         int dptr_num = -1;
1767         int numentries = 0;
1768         int i;
1769         BOOL finished = False;
1770         BOOL dont_descend = False;
1771         BOOL out_of_space = False;
1772         int space_remaining;
1773         BOOL mask_contains_wcard = False;
1774         SMB_STRUCT_STAT sbuf;
1775         TALLOC_CTX *ea_ctx = NULL;
1776         struct ea_list *ea_list = NULL;
1777         NTSTATUS ntstatus = NT_STATUS_OK;
1778         TALLOC_CTX *ctx = talloc_tos();
1779
1780         if (total_params < 13) {
1781                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1782                 return;
1783         }
1784
1785         dirtype = SVAL(params,0);
1786         maxentries = SVAL(params,2);
1787         findfirst_flags = SVAL(params,4);
1788         close_after_first = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE);
1789         close_if_end = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
1790         requires_resume_key = (findfirst_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
1791         info_level = SVAL(params,6);
1792
1793         *mask = 0;
1794
1795         DEBUG(3,("call_trans2findfirst: dirtype = %x, maxentries = %d, close_after_first=%d, \
1796 close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
1797                 (unsigned int)dirtype, maxentries, close_after_first, close_if_end, requires_resume_key,
1798                 info_level, max_data_bytes));
1799
1800         if (!maxentries) {
1801                 /* W2K3 seems to treat zero as 1. */
1802                 maxentries = 1;
1803         }
1804  
1805         switch (info_level) {
1806                 case SMB_FIND_INFO_STANDARD:
1807                 case SMB_FIND_EA_SIZE:
1808                 case SMB_FIND_EA_LIST:
1809                 case SMB_FIND_FILE_DIRECTORY_INFO:
1810                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1811                 case SMB_FIND_FILE_NAMES_INFO:
1812                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1813                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1814                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1815                         break;
1816                 case SMB_FIND_FILE_UNIX:
1817                 case SMB_FIND_FILE_UNIX_INFO2:
1818                         if (!lp_unix_extensions()) {
1819                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
1820                                 return;
1821                         }
1822                         break;
1823                 default:
1824                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
1825                         return;
1826         }
1827
1828         srvstr_get_path_wcard(ctx, params, req->flags2, &directory,
1829                               params+12, total_params - 12,
1830                               STR_TERMINATE, &ntstatus, &mask_contains_wcard);
1831         if (!NT_STATUS_IS_OK(ntstatus)) {
1832                 reply_nterror(req, ntstatus);
1833                 return;
1834         }
1835
1836         ntstatus = resolve_dfspath_wcard(ctx, conn,
1837                         req->flags2 & FLAGS2_DFS_PATHNAMES,
1838                         directory,
1839                         &directory,
1840                         &mask_contains_wcard);
1841         if (!NT_STATUS_IS_OK(ntstatus)) {
1842                 if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
1843                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1844                                         ERRSRV, ERRbadpath);
1845                         return;
1846                 }
1847                 reply_nterror(req, ntstatus);
1848                 return;
1849         }
1850
1851         ntstatus = unix_convert(conn, directory, True, &directory, NULL, &sbuf);
1852         if (!NT_STATUS_IS_OK(ntstatus)) {
1853                 reply_nterror(req, ntstatus);
1854                 return;
1855         }
1856
1857         ntstatus = check_name(conn, directory);
1858         if (!NT_STATUS_IS_OK(ntstatus)) {
1859                 reply_nterror(req, ntstatus);
1860                 return;
1861         }
1862
1863         p = strrchr_m(directory,'/');
1864         if(p == NULL) {
1865                 /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
1866                 if((directory[0] == '.') && (directory[1] == '\0')) {
1867                         pstrcpy(mask,"*");
1868                         mask_contains_wcard = True;
1869                 } else {
1870                         pstrcpy(mask,directory);
1871                 }
1872                 directory = talloc_strdup(talloc_tos(), "./");
1873                 if (!directory) {
1874                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1875                         return;
1876                 }
1877         } else {
1878                 pstrcpy(mask,p+1);
1879                 *p = 0;
1880         }
1881
1882         DEBUG(5,("dir=%s, mask = %s\n",directory, mask));
1883
1884         if (info_level == SMB_FIND_EA_LIST) {
1885                 uint32 ea_size;
1886
1887                 if (total_data < 4) {
1888                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1889                         return;
1890                 }
1891
1892                 ea_size = IVAL(pdata,0);
1893                 if (ea_size != total_data) {
1894                         DEBUG(4,("call_trans2findfirst: Rejecting EA request with incorrect \
1895 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
1896                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1897                         return;
1898                 }
1899
1900                 if (!lp_ea_support(SNUM(conn))) {
1901                         reply_doserror(req, ERRDOS, ERReasnotsupported);
1902                         return;
1903                 }
1904                                                                                                                                                         
1905                 if ((ea_ctx = talloc_init("findnext_ea_list")) == NULL) {
1906                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1907                         return;
1908                 }
1909
1910                 /* Pull out the list of names. */
1911                 ea_list = read_ea_name_list(ea_ctx, pdata + 4, ea_size - 4);
1912                 if (!ea_list) {
1913                         talloc_destroy(ea_ctx);
1914                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1915                         return;
1916                 }
1917         }
1918
1919         *ppdata = (char *)SMB_REALLOC(
1920                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
1921         if(*ppdata == NULL ) {
1922                 talloc_destroy(ea_ctx);
1923                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1924                 return;
1925         }
1926         pdata = *ppdata;
1927         data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
1928
1929         /* Realloc the params space */
1930         *pparams = (char *)SMB_REALLOC(*pparams, 10);
1931         if (*pparams == NULL) {
1932                 talloc_destroy(ea_ctx);
1933                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1934                 return;
1935         }
1936         params = *pparams;
1937
1938         /* Save the wildcard match and attribs we are using on this directory - 
1939                 needed as lanman2 assumes these are being saved between calls */
1940
1941         ntstatus = dptr_create(conn,
1942                                 directory,
1943                                 False,
1944                                 True,
1945                                 req->smbpid,
1946                                 mask,
1947                                 mask_contains_wcard,
1948                                 dirtype,
1949                                 &conn->dirptr);
1950
1951         if (!NT_STATUS_IS_OK(ntstatus)) {
1952                 talloc_destroy(ea_ctx);
1953                 reply_nterror(req, ntstatus);
1954                 return;
1955         }
1956
1957         dptr_num = dptr_dnum(conn->dirptr);
1958         DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
1959
1960         /* We don't need to check for VOL here as this is returned by 
1961                 a different TRANS2 call. */
1962   
1963         DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n", conn->dirpath,lp_dontdescend(SNUM(conn))));
1964         if (in_list(conn->dirpath,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
1965                 dont_descend = True;
1966     
1967         p = pdata;
1968         space_remaining = max_data_bytes;
1969         out_of_space = False;
1970
1971         for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
1972                 BOOL got_exact_match = False;
1973
1974                 /* this is a heuristic to avoid seeking the dirptr except when 
1975                         absolutely necessary. It allows for a filename of about 40 chars */
1976                 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
1977                         out_of_space = True;
1978                         finished = False;
1979                 } else {
1980                         finished = !get_lanman2_dir_entry(conn,
1981                                         req->flags2,
1982                                         mask,dirtype,info_level,
1983                                         requires_resume_key,dont_descend,
1984                                         &p,pdata,data_end,
1985                                         space_remaining, &out_of_space,
1986                                         &got_exact_match,
1987                                         &last_entry_off, ea_list, ea_ctx);
1988                 }
1989
1990                 if (finished && out_of_space)
1991                         finished = False;
1992
1993                 if (!finished && !out_of_space)
1994                         numentries++;
1995
1996                 /*
1997                  * As an optimisation if we know we aren't looking
1998                  * for a wildcard name (ie. the name matches the wildcard exactly)
1999                  * then we can finish on any (first) match.
2000                  * This speeds up large directory searches. JRA.
2001                  */
2002
2003                 if(got_exact_match)
2004                         finished = True;
2005
2006                 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2007         }
2008   
2009         talloc_destroy(ea_ctx);
2010
2011         /* Check if we can close the dirptr */
2012         if(close_after_first || (finished && close_if_end)) {
2013                 DEBUG(5,("call_trans2findfirst - (2) closing dptr_num %d\n", dptr_num));
2014                 dptr_close(&dptr_num);
2015         }
2016
2017         /* 
2018          * If there are no matching entries we must return ERRDOS/ERRbadfile - 
2019          * from observation of NT. NB. This changes to ERRDOS,ERRnofiles if
2020          * the protocol level is less than NT1. Tested with smbclient. JRA.
2021          * This should fix the OS/2 client bug #2335.
2022          */
2023
2024         if(numentries == 0) {
2025                 dptr_close(&dptr_num);
2026                 if (Protocol < PROTOCOL_NT1) {
2027                         reply_doserror(req, ERRDOS, ERRnofiles);
2028                         return;
2029                 } else {
2030                         reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
2031                                         ERRDOS, ERRbadfile);
2032                         return;
2033                 }
2034         }
2035
2036         /* At this point pdata points to numentries directory entries. */
2037
2038         /* Set up the return parameter block */
2039         SSVAL(params,0,dptr_num);
2040         SSVAL(params,2,numentries);
2041         SSVAL(params,4,finished);
2042         SSVAL(params,6,0); /* Never an EA error */
2043         SSVAL(params,8,last_entry_off);
2044
2045         send_trans2_replies(req, params, 10, pdata, PTR_DIFF(p,pdata),
2046                             max_data_bytes);
2047
2048         if ((! *directory) && dptr_path(dptr_num)) {
2049                 directory = talloc_strdup(talloc_tos(),dptr_path(dptr_num));
2050                 if (!directory) {
2051                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2052                 }
2053         }
2054
2055         DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2056                 smb_fn_name(CVAL(req->inbuf,smb_com)),
2057                 mask, directory, dirtype, numentries ) );
2058
2059         /*
2060          * Force a name mangle here to ensure that the
2061          * mask as an 8.3 name is top of the mangled cache.
2062          * The reasons for this are subtle. Don't remove
2063          * this code unless you know what you are doing
2064          * (see PR#13758). JRA.
2065          */
2066
2067         if(!mangle_is_8_3_wildcards( mask, False, conn->params)) {
2068                 char mangled_name[13];
2069                 name_to_8_3(mask, mangled_name, True, conn->params);
2070         }
2071
2072         return;
2073 }
2074
2075 /****************************************************************************
2076  Reply to a TRANS2_FINDNEXT.
2077 ****************************************************************************/
2078
2079 static void call_trans2findnext(connection_struct *conn,
2080                                 struct smb_request *req,
2081                                 char **pparams, int total_params,
2082                                 char **ppdata, int total_data,
2083                                 unsigned int max_data_bytes)
2084 {
2085         /* We must be careful here that we don't return more than the
2086                 allowed number of data bytes. If this means returning fewer than
2087                 maxentries then so be it. We assume that the redirector has
2088                 enough room for the fixed number of parameter bytes it has
2089                 requested. */
2090         char *params = *pparams;
2091         char *pdata = *ppdata;
2092         char *data_end;
2093         int dptr_num;
2094         int maxentries;
2095         uint16 info_level;
2096         uint32 resume_key;
2097         uint16 findnext_flags;
2098         BOOL close_after_request;
2099         BOOL close_if_end;
2100         BOOL requires_resume_key;
2101         BOOL continue_bit;
2102         BOOL mask_contains_wcard = False;
2103         char *resume_name = NULL;
2104         pstring mask;
2105         pstring directory;
2106         char *p;
2107         uint16 dirtype;
2108         int numentries = 0;
2109         int i, last_entry_off=0;
2110         BOOL finished = False;
2111         BOOL dont_descend = False;
2112         BOOL out_of_space = False;
2113         int space_remaining;
2114         TALLOC_CTX *ea_ctx = NULL;
2115         struct ea_list *ea_list = NULL;
2116         NTSTATUS ntstatus = NT_STATUS_OK;
2117         TALLOC_CTX *ctx = talloc_tos();
2118
2119         if (total_params < 13) {
2120                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2121                 return;
2122         }
2123
2124         dptr_num = SVAL(params,0);
2125         maxentries = SVAL(params,2);
2126         info_level = SVAL(params,4);
2127         resume_key = IVAL(params,6);
2128         findnext_flags = SVAL(params,10);
2129         close_after_request = (findnext_flags & FLAG_TRANS2_FIND_CLOSE);
2130         close_if_end = (findnext_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2131         requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2132         continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
2133
2134         *mask = *directory = 0;
2135
2136         srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
2137                               params+12,
2138                               total_params - 12, STR_TERMINATE, &ntstatus,
2139                               &mask_contains_wcard);
2140         if (!NT_STATUS_IS_OK(ntstatus)) {
2141                 /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
2142                    complain (it thinks we're asking for the directory above the shared
2143                    path or an invalid name). Catch this as the resume name is only compared, never used in
2144                    a file access. JRA. */
2145                 srvstr_pull_talloc(ctx, params, req->flags2,
2146                                 &resume_name, params+12,
2147                                 total_params - 12,
2148                                 STR_TERMINATE);
2149
2150                 if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
2151                         reply_nterror(req, ntstatus);
2152                         return;
2153                 }
2154         }
2155
2156         DEBUG(3,("call_trans2findnext: dirhandle = %d, max_data_bytes = %d, maxentries = %d, \
2157 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
2158 resume_key = %d resume name = %s continue=%d level = %d\n",
2159                 dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end, 
2160                 requires_resume_key, resume_key, resume_name, continue_bit, info_level));
2161
2162         if (!maxentries) {
2163                 /* W2K3 seems to treat zero as 1. */
2164                 maxentries = 1;
2165         }
2166
2167         switch (info_level) {
2168                 case SMB_FIND_INFO_STANDARD:
2169                 case SMB_FIND_EA_SIZE:
2170                 case SMB_FIND_EA_LIST:
2171                 case SMB_FIND_FILE_DIRECTORY_INFO:
2172                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2173                 case SMB_FIND_FILE_NAMES_INFO:
2174                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2175                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2176                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2177                         break;
2178                 case SMB_FIND_FILE_UNIX:
2179                 case SMB_FIND_FILE_UNIX_INFO2:
2180                         if (!lp_unix_extensions()) {
2181                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2182                                 return;
2183                         }
2184                         break;
2185                 default:
2186                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2187                         return;
2188         }
2189
2190         if (info_level == SMB_FIND_EA_LIST) {
2191                 uint32 ea_size;
2192
2193                 if (total_data < 4) {
2194                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2195                         return;
2196                 }
2197
2198                 ea_size = IVAL(pdata,0);
2199                 if (ea_size != total_data) {
2200                         DEBUG(4,("call_trans2findnext: Rejecting EA request with incorrect \
2201 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2202                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2203                         return;
2204                 }
2205                                                                                                                                                      
2206                 if (!lp_ea_support(SNUM(conn))) {
2207                         reply_doserror(req, ERRDOS, ERReasnotsupported);
2208                         return;
2209                 }
2210                                                                                                                                                      
2211                 if ((ea_ctx = talloc_init("findnext_ea_list")) == NULL) {
2212                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2213                         return;
2214                 }
2215
2216                 /* Pull out the list of names. */
2217                 ea_list = read_ea_name_list(ea_ctx, pdata + 4, ea_size - 4);
2218                 if (!ea_list) {
2219                         talloc_destroy(ea_ctx);
2220                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2221                         return;
2222                 }
2223         }
2224
2225         *ppdata = (char *)SMB_REALLOC(
2226                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2227         if(*ppdata == NULL) {
2228                 talloc_destroy(ea_ctx);
2229                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2230                 return;
2231         }
2232
2233         pdata = *ppdata;
2234         data_end = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2235
2236         /* Realloc the params space */
2237         *pparams = (char *)SMB_REALLOC(*pparams, 6*SIZEOFWORD);
2238         if(*pparams == NULL ) {
2239                 talloc_destroy(ea_ctx);
2240                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2241                 return;
2242         }
2243
2244         params = *pparams;
2245
2246         /* Check that the dptr is valid */
2247         if(!(conn->dirptr = dptr_fetch_lanman2(dptr_num))) {
2248                 talloc_destroy(ea_ctx);
2249                 reply_doserror(req, ERRDOS, ERRnofiles);
2250                 return;
2251         }
2252
2253         string_set(&conn->dirpath,dptr_path(dptr_num));
2254
2255         /* Get the wildcard mask from the dptr */
2256         if((p = dptr_wcard(dptr_num))== NULL) {
2257                 DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
2258                 talloc_destroy(ea_ctx);
2259                 reply_doserror(req, ERRDOS, ERRnofiles);
2260                 return;
2261         }
2262
2263         pstrcpy(mask, p);
2264         pstrcpy(directory,conn->dirpath);
2265
2266         /* Get the attr mask from the dptr */
2267         dirtype = dptr_attr(dptr_num);
2268
2269         DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld)\n",
2270                 dptr_num, mask, dirtype, 
2271                 (long)conn->dirptr,
2272                 dptr_TellDir(conn->dirptr)));
2273
2274         /* We don't need to check for VOL here as this is returned by 
2275                 a different TRANS2 call. */
2276
2277         DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",conn->dirpath,lp_dontdescend(SNUM(conn))));
2278         if (in_list(conn->dirpath,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
2279                 dont_descend = True;
2280     
2281         p = pdata;
2282         space_remaining = max_data_bytes;
2283         out_of_space = False;
2284
2285         /* 
2286          * Seek to the correct position. We no longer use the resume key but
2287          * depend on the last file name instead.
2288          */
2289
2290         if(*resume_name && !continue_bit) {
2291                 SMB_STRUCT_STAT st;
2292
2293                 long current_pos = 0;
2294                 /*
2295                  * Remember, name_to_8_3 is called by
2296                  * get_lanman2_dir_entry(), so the resume name
2297                  * could be mangled. Ensure we check the unmangled name.
2298                  */
2299
2300                 if (mangle_is_mangled(resume_name, conn->params)) {
2301                         char *new_resume_name = NULL;
2302                         mangle_lookup_name_from_8_3(talloc_tos(),
2303                                                 resume_name,
2304                                                 &new_resume_name,
2305                                                 conn->params);
2306                         if (new_resume_name) {
2307                                 pstrcpy(resume_name, new_resume_name);
2308                         }
2309                 }
2310
2311                 /*
2312                  * Fix for NT redirector problem triggered by resume key indexes
2313                  * changing between directory scans. We now return a resume key of 0
2314                  * and instead look for the filename to continue from (also given
2315                  * to us by NT/95/smbfs/smbclient). If no other scans have been done between the
2316                  * findfirst/findnext (as is usual) then the directory pointer
2317                  * should already be at the correct place.
2318                  */
2319
2320                 finished = !dptr_SearchDir(conn->dirptr, resume_name, &current_pos, &st);
2321         } /* end if resume_name && !continue_bit */
2322
2323         for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
2324                 BOOL got_exact_match = False;
2325
2326                 /* this is a heuristic to avoid seeking the dirptr except when 
2327                         absolutely necessary. It allows for a filename of about 40 chars */
2328                 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2329                         out_of_space = True;
2330                         finished = False;
2331                 } else {
2332                         finished = !get_lanman2_dir_entry(conn,
2333                                                 req->flags2,
2334                                                 mask,dirtype,info_level,
2335                                                 requires_resume_key,dont_descend,
2336                                                 &p,pdata,data_end,
2337                                                 space_remaining, &out_of_space,
2338                                                 &got_exact_match,
2339                                                 &last_entry_off, ea_list, ea_ctx);
2340                 }
2341
2342                 if (finished && out_of_space)
2343                         finished = False;
2344
2345                 if (!finished && !out_of_space)
2346                         numentries++;
2347
2348                 /*
2349                  * As an optimisation if we know we aren't looking
2350                  * for a wildcard name (ie. the name matches the wildcard exactly)
2351                  * then we can finish on any (first) match.
2352                  * This speeds up large directory searches. JRA.
2353                  */
2354
2355                 if(got_exact_match)
2356                         finished = True;
2357
2358                 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2359         }
2360   
2361         talloc_destroy(ea_ctx);
2362
2363         /* Check if we can close the dirptr */
2364         if(close_after_request || (finished && close_if_end)) {
2365                 DEBUG(5,("call_trans2findnext: closing dptr_num = %d\n", dptr_num));
2366                 dptr_close(&dptr_num); /* This frees up the saved mask */
2367         }
2368
2369         /* Set up the return parameter block */
2370         SSVAL(params,0,numentries);
2371         SSVAL(params,2,finished);
2372         SSVAL(params,4,0); /* Never an EA error */
2373         SSVAL(params,6,last_entry_off);
2374
2375         send_trans2_replies(req, params, 8, pdata, PTR_DIFF(p,pdata),
2376                             max_data_bytes);
2377
2378         if ((! *directory) && dptr_path(dptr_num))
2379                 slprintf(directory,sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
2380
2381         DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2382                 smb_fn_name(CVAL(req->inbuf,smb_com)),
2383                 mask, directory, dirtype, numentries ) );
2384
2385         return;
2386 }
2387
2388 unsigned char *create_volume_objectid(connection_struct *conn, unsigned char objid[16])
2389 {
2390         E_md4hash(lp_servicename(SNUM(conn)),objid);
2391         return objid;
2392 }
2393
2394 /****************************************************************************
2395  Reply to a TRANS2_QFSINFO (query filesystem info).
2396 ****************************************************************************/
2397
2398 static void call_trans2qfsinfo(connection_struct *conn,
2399                                struct smb_request *req,
2400                                char **pparams, int total_params,
2401                                char **ppdata, int total_data,
2402                                unsigned int max_data_bytes)
2403 {
2404         char *pdata, *end_data;
2405         char *params = *pparams;
2406         uint16 info_level;
2407         int data_len, len;
2408         SMB_STRUCT_STAT st;
2409         const char *vname = volume_label(SNUM(conn));
2410         int snum = SNUM(conn);
2411         char *fstype = lp_fstype(SNUM(conn));
2412         int quota_flag = 0;
2413
2414         if (total_params < 2) {
2415                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2416                 return;
2417         }
2418
2419         info_level = SVAL(params,0);
2420
2421         DEBUG(3,("call_trans2qfsinfo: level = %d\n", info_level));
2422
2423         if(SMB_VFS_STAT(conn,".",&st)!=0) {
2424                 DEBUG(2,("call_trans2qfsinfo: stat of . failed (%s)\n", strerror(errno)));
2425                 reply_doserror(req, ERRSRV, ERRinvdevice);
2426                 return;
2427         }
2428
2429         *ppdata = (char *)SMB_REALLOC(
2430                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2431         if (*ppdata == NULL ) {
2432                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2433                 return;
2434         }
2435
2436         pdata = *ppdata;
2437         memset((char *)pdata,'\0',max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2438         end_data = pdata + max_data_bytes + DIR_ENTRY_SAFETY_MARGIN - 1;
2439
2440         switch (info_level) {
2441                 case SMB_INFO_ALLOCATION:
2442                 {
2443                         SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2444                         data_len = 18;
2445                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
2446                                 reply_unixerror(req, ERRHRD, ERRgeneral);
2447                                 return;
2448                         }
2449
2450                         block_size = lp_block_size(snum);
2451                         if (bsize < block_size) {
2452                                 SMB_BIG_UINT factor = block_size/bsize;
2453                                 bsize = block_size;
2454                                 dsize /= factor;
2455                                 dfree /= factor;
2456                         }
2457                         if (bsize > block_size) {
2458                                 SMB_BIG_UINT factor = bsize/block_size;
2459                                 bsize = block_size;
2460                                 dsize *= factor;
2461                                 dfree *= factor;
2462                         }
2463                         bytes_per_sector = 512;
2464                         sectors_per_unit = bsize/bytes_per_sector;
2465
2466                         DEBUG(5,("call_trans2qfsinfo : SMB_INFO_ALLOCATION id=%x, bsize=%u, cSectorUnit=%u, \
2467 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_dev, (unsigned int)bsize, (unsigned int)sectors_per_unit,
2468                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2469
2470                         SIVAL(pdata,l1_idFileSystem,st.st_dev);
2471                         SIVAL(pdata,l1_cSectorUnit,sectors_per_unit);
2472                         SIVAL(pdata,l1_cUnit,dsize);
2473                         SIVAL(pdata,l1_cUnitAvail,dfree);
2474                         SSVAL(pdata,l1_cbSector,bytes_per_sector);
2475                         break;
2476                 }
2477
2478                 case SMB_INFO_VOLUME:
2479                         /* Return volume name */
2480                         /* 
2481                          * Add volume serial number - hash of a combination of
2482                          * the called hostname and the service name.
2483                          */
2484                         SIVAL(pdata,0,str_checksum(lp_servicename(snum)) ^ (str_checksum(get_local_machine_name())<<16) );
2485                         /*
2486                          * Win2k3 and previous mess this up by sending a name length
2487                          * one byte short. I believe only older clients (OS/2 Win9x) use
2488                          * this call so try fixing this by adding a terminating null to
2489                          * the pushed string. The change here was adding the STR_TERMINATE. JRA.
2490                          */
2491                         len = srvstr_push(
2492                                 pdata, req->flags2,
2493                                 pdata+l2_vol_szVolLabel, vname,
2494                                 PTR_DIFF(end_data, pdata+l2_vol_szVolLabel),
2495                                 STR_NOALIGN|STR_TERMINATE);
2496                         SCVAL(pdata,l2_vol_cch,len);
2497                         data_len = l2_vol_szVolLabel + len;
2498                         DEBUG(5,("call_trans2qfsinfo : time = %x, namelen = %d, name = %s\n",
2499                                 (unsigned)st.st_ctime, len, vname));
2500                         break;
2501
2502                 case SMB_QUERY_FS_ATTRIBUTE_INFO:
2503                 case SMB_FS_ATTRIBUTE_INFORMATION:
2504
2505
2506 #if defined(HAVE_SYS_QUOTAS)
2507                         quota_flag = FILE_VOLUME_QUOTAS;
2508 #endif
2509
2510                         SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
2511                                 (lp_nt_acl_support(SNUM(conn)) ? FILE_PERSISTENT_ACLS : 0)|
2512                                 FILE_SUPPORTS_OBJECT_IDS|
2513                                 FILE_UNICODE_ON_DISK|
2514                                 quota_flag); /* FS ATTRIBUTES */
2515
2516                         SIVAL(pdata,4,255); /* Max filename component length */
2517                         /* NOTE! the fstype must *not* be null terminated or win98 won't recognise it
2518                                 and will think we can't do long filenames */
2519                         len = srvstr_push(pdata, req->flags2, pdata+12, fstype,
2520                                           PTR_DIFF(end_data, pdata+12),
2521                                           STR_UNICODE);
2522                         SIVAL(pdata,8,len);
2523                         data_len = 12 + len;
2524                         break;
2525
2526                 case SMB_QUERY_FS_LABEL_INFO:
2527                 case SMB_FS_LABEL_INFORMATION:
2528                         len = srvstr_push(pdata, req->flags2, pdata+4, vname,
2529                                           PTR_DIFF(end_data, pdata+4), 0);
2530                         data_len = 4 + len;
2531                         SIVAL(pdata,0,len);
2532                         break;
2533
2534                 case SMB_QUERY_FS_VOLUME_INFO:      
2535                 case SMB_FS_VOLUME_INFORMATION:
2536
2537                         /* 
2538                          * Add volume serial number - hash of a combination of
2539                          * the called hostname and the service name.
2540                          */
2541                         SIVAL(pdata,8,str_checksum(lp_servicename(snum)) ^ 
2542                                 (str_checksum(get_local_machine_name())<<16));
2543
2544                         /* Max label len is 32 characters. */
2545                         len = srvstr_push(pdata, req->flags2, pdata+18, vname,
2546                                           PTR_DIFF(end_data, pdata+18),
2547                                           STR_UNICODE);
2548                         SIVAL(pdata,12,len);
2549                         data_len = 18+len;
2550
2551                         DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_VOLUME_INFO namelen = %d, vol=%s serv=%s\n", 
2552                                 (int)strlen(vname),vname, lp_servicename(snum)));
2553                         break;
2554
2555                 case SMB_QUERY_FS_SIZE_INFO:
2556                 case SMB_FS_SIZE_INFORMATION:
2557                 {
2558                         SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2559                         data_len = 24;
2560                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
2561                                 reply_unixerror(req, ERRHRD, ERRgeneral);
2562                                 return;
2563                         }
2564                         block_size = lp_block_size(snum);
2565                         if (bsize < block_size) {
2566                                 SMB_BIG_UINT factor = block_size/bsize;
2567                                 bsize = block_size;
2568                                 dsize /= factor;
2569                                 dfree /= factor;
2570                         }
2571                         if (bsize > block_size) {
2572                                 SMB_BIG_UINT factor = bsize/block_size;
2573                                 bsize = block_size;
2574                                 dsize *= factor;
2575                                 dfree *= factor;
2576                         }
2577                         bytes_per_sector = 512;
2578                         sectors_per_unit = bsize/bytes_per_sector;
2579                         DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_SIZE_INFO bsize=%u, cSectorUnit=%u, \
2580 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
2581                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2582                         SBIG_UINT(pdata,0,dsize);
2583                         SBIG_UINT(pdata,8,dfree);
2584                         SIVAL(pdata,16,sectors_per_unit);
2585                         SIVAL(pdata,20,bytes_per_sector);
2586                         break;
2587                 }
2588
2589                 case SMB_FS_FULL_SIZE_INFORMATION:
2590                 {
2591                         SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2592                         data_len = 32;
2593                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
2594                                 reply_unixerror(req, ERRHRD, ERRgeneral);
2595                                 return;
2596                         }
2597                         block_size = lp_block_size(snum);
2598                         if (bsize < block_size) {
2599                                 SMB_BIG_UINT factor = block_size/bsize;
2600                                 bsize = block_size;
2601                                 dsize /= factor;
2602                                 dfree /= factor;
2603                         }
2604                         if (bsize > block_size) {
2605                                 SMB_BIG_UINT factor = bsize/block_size;
2606                                 bsize = block_size;
2607                                 dsize *= factor;
2608                                 dfree *= factor;
2609                         }
2610                         bytes_per_sector = 512;
2611                         sectors_per_unit = bsize/bytes_per_sector;
2612                         DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_FULL_SIZE_INFO bsize=%u, cSectorUnit=%u, \
2613 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
2614                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2615                         SBIG_UINT(pdata,0,dsize); /* Total Allocation units. */
2616                         SBIG_UINT(pdata,8,dfree); /* Caller available allocation units. */
2617                         SBIG_UINT(pdata,16,dfree); /* Actual available allocation units. */
2618                         SIVAL(pdata,24,sectors_per_unit); /* Sectors per allocation unit. */
2619                         SIVAL(pdata,28,bytes_per_sector); /* Bytes per sector. */
2620                         break;
2621                 }
2622
2623                 case SMB_QUERY_FS_DEVICE_INFO:
2624                 case SMB_FS_DEVICE_INFORMATION:
2625                         data_len = 8;
2626                         SIVAL(pdata,0,0); /* dev type */
2627                         SIVAL(pdata,4,0); /* characteristics */
2628                         break;
2629
2630 #ifdef HAVE_SYS_QUOTAS
2631                 case SMB_FS_QUOTA_INFORMATION:
2632                 /* 
2633                  * what we have to send --metze:
2634                  *
2635                  * Unknown1:            24 NULL bytes
2636                  * Soft Quota Treshold: 8 bytes seems like SMB_BIG_UINT or so
2637                  * Hard Quota Limit:    8 bytes seems like SMB_BIG_UINT or so
2638                  * Quota Flags:         2 byte :
2639                  * Unknown3:            6 NULL bytes
2640                  *
2641                  * 48 bytes total
2642                  * 
2643                  * details for Quota Flags:
2644                  * 
2645                  * 0x0020 Log Limit: log if the user exceeds his Hard Quota
2646                  * 0x0010 Log Warn:  log if the user exceeds his Soft Quota
2647                  * 0x0002 Deny Disk: deny disk access when the user exceeds his Hard Quota
2648                  * 0x0001 Enable Quotas: enable quota for this fs
2649                  *
2650                  */
2651                 {
2652                         /* we need to fake up a fsp here,
2653                          * because its not send in this call
2654                          */
2655                         files_struct fsp;
2656                         SMB_NTQUOTA_STRUCT quotas;
2657                         
2658                         ZERO_STRUCT(fsp);
2659                         ZERO_STRUCT(quotas);
2660                         
2661                         fsp.conn = conn;
2662                         fsp.fnum = -1;
2663                         
2664                         /* access check */
2665                         if (current_user.ut.uid != 0) {
2666                                 DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
2667                                         lp_servicename(SNUM(conn)),conn->user));
2668                                 reply_doserror(req, ERRDOS, ERRnoaccess);
2669                                 return;
2670                         }
2671                         
2672                         if (vfs_get_ntquota(&fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
2673                                 DEBUG(0,("vfs_get_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
2674                                 reply_doserror(req, ERRSRV, ERRerror);
2675                                 return;
2676                         }
2677
2678                         data_len = 48;
2679
2680                         DEBUG(10,("SMB_FS_QUOTA_INFORMATION: for service [%s]\n",lp_servicename(SNUM(conn))));          
2681                 
2682                         /* Unknown1 24 NULL bytes*/
2683                         SBIG_UINT(pdata,0,(SMB_BIG_UINT)0);
2684                         SBIG_UINT(pdata,8,(SMB_BIG_UINT)0);
2685                         SBIG_UINT(pdata,16,(SMB_BIG_UINT)0);
2686                 
2687                         /* Default Soft Quota 8 bytes */
2688                         SBIG_UINT(pdata,24,quotas.softlim);
2689
2690                         /* Default Hard Quota 8 bytes */
2691                         SBIG_UINT(pdata,32,quotas.hardlim);
2692         
2693                         /* Quota flag 2 bytes */
2694                         SSVAL(pdata,40,quotas.qflags);
2695                 
2696                         /* Unknown3 6 NULL bytes */
2697                         SSVAL(pdata,42,0);
2698                         SIVAL(pdata,44,0);
2699                         
2700                         break;
2701                 }
2702 #endif /* HAVE_SYS_QUOTAS */
2703                 case SMB_FS_OBJECTID_INFORMATION:
2704                 {
2705                         unsigned char objid[16];
2706                         memcpy(pdata,create_volume_objectid(conn, objid),16);
2707                         data_len = 64;
2708                         break;
2709                 }
2710
2711                 /*
2712                  * Query the version and capabilities of the CIFS UNIX extensions
2713                  * in use.
2714                  */
2715
2716                 case SMB_QUERY_CIFS_UNIX_INFO:
2717                         if (!lp_unix_extensions()) {
2718                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2719                                 return;
2720                         }
2721                         data_len = 12;
2722                         SSVAL(pdata,0,CIFS_UNIX_MAJOR_VERSION);
2723                         SSVAL(pdata,2,CIFS_UNIX_MINOR_VERSION);
2724                         /* We have POSIX ACLs, pathname and locking capability. */
2725                         SBIG_UINT(pdata,4,((SMB_BIG_UINT)(
2726                                         CIFS_UNIX_POSIX_ACLS_CAP|
2727                                         CIFS_UNIX_POSIX_PATHNAMES_CAP|
2728                                         CIFS_UNIX_FCNTL_LOCKS_CAP|
2729                                         CIFS_UNIX_EXTATTR_CAP|
2730                                         CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP|
2731                                         /* Ensure we don't do this on signed or sealed data. */
2732                                         (srv_is_signing_active() ? 0 : CIFS_UNIX_LARGE_READ_CAP)
2733                                         )));
2734                         break;
2735
2736                 case SMB_QUERY_POSIX_FS_INFO:
2737                 {
2738                         int rc;
2739                         vfs_statvfs_struct svfs;
2740
2741                         if (!lp_unix_extensions()) {
2742                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2743                                 return;
2744                         }
2745
2746                         rc = SMB_VFS_STATVFS(conn, ".", &svfs);
2747
2748                         if (!rc) {
2749                                 data_len = 56;
2750                                 SIVAL(pdata,0,svfs.OptimalTransferSize);
2751                                 SIVAL(pdata,4,svfs.BlockSize);
2752                                 SBIG_UINT(pdata,8,svfs.TotalBlocks);
2753                                 SBIG_UINT(pdata,16,svfs.BlocksAvail);
2754                                 SBIG_UINT(pdata,24,svfs.UserBlocksAvail);
2755                                 SBIG_UINT(pdata,32,svfs.TotalFileNodes);
2756                                 SBIG_UINT(pdata,40,svfs.FreeFileNodes);
2757                                 SBIG_UINT(pdata,48,svfs.FsIdentifier);
2758                                 DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_POSIX_FS_INFO succsessful\n"));
2759 #ifdef EOPNOTSUPP
2760                         } else if (rc == EOPNOTSUPP) {
2761                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2762                                 return;
2763 #endif /* EOPNOTSUPP */
2764                         } else {
2765                                 DEBUG(0,("vfs_statvfs() failed for service [%s]\n",lp_servicename(SNUM(conn))));
2766                                 reply_doserror(req, ERRSRV, ERRerror);
2767                                 return;
2768                         }
2769                         break;
2770                 }
2771
2772                 case SMB_QUERY_POSIX_WHOAMI:
2773                 {
2774                         uint32_t flags = 0;
2775                         uint32_t sid_bytes;
2776                         int i;
2777
2778                         if (!lp_unix_extensions()) {
2779                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2780                                 return;
2781                         }
2782
2783                         if (max_data_bytes < 40) {
2784                                 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2785                                 return;
2786                         }
2787
2788                         /* We ARE guest if global_sid_Builtin_Guests is
2789                          * in our list of SIDs.
2790                          */
2791                         if (nt_token_check_sid(&global_sid_Builtin_Guests,
2792                                     current_user.nt_user_token)) {
2793                                 flags |= SMB_WHOAMI_GUEST;
2794                         }
2795
2796                         /* We are NOT guest if global_sid_Authenticated_Users
2797                          * is in our list of SIDs.
2798                          */
2799                         if (nt_token_check_sid(&global_sid_Authenticated_Users,
2800                                     current_user.nt_user_token)) {
2801                                 flags &= ~SMB_WHOAMI_GUEST;
2802                         }
2803
2804                         /* NOTE: 8 bytes for UID/GID, irrespective of native
2805                          * platform size. This matches
2806                          * SMB_QUERY_FILE_UNIX_BASIC and friends.
2807                          */
2808                         data_len = 4 /* flags */
2809                             + 4 /* flag mask */
2810                             + 8 /* uid */
2811                             + 8 /* gid */
2812                             + 4 /* ngroups */
2813                             + 4 /* num_sids */
2814                             + 4 /* SID bytes */
2815                             + 4 /* pad/reserved */
2816                             + (current_user.ut.ngroups * 8)
2817                                 /* groups list */
2818                             + (current_user.nt_user_token->num_sids *
2819                                     SID_MAX_SIZE)
2820                                 /* SID list */;
2821
2822                         SIVAL(pdata, 0, flags);
2823                         SIVAL(pdata, 4, SMB_WHOAMI_MASK);
2824                         SBIG_UINT(pdata, 8, (SMB_BIG_UINT)current_user.ut.uid);
2825                         SBIG_UINT(pdata, 16, (SMB_BIG_UINT)current_user.ut.gid);
2826
2827
2828                         if (data_len >= max_data_bytes) {
2829                                 /* Potential overflow, skip the GIDs and SIDs. */
2830
2831                                 SIVAL(pdata, 24, 0); /* num_groups */
2832                                 SIVAL(pdata, 28, 0); /* num_sids */
2833                                 SIVAL(pdata, 32, 0); /* num_sid_bytes */
2834                                 SIVAL(pdata, 36, 0); /* reserved */
2835
2836                                 data_len = 40;
2837                                 break;
2838                         }
2839
2840                         SIVAL(pdata, 24, current_user.ut.ngroups);
2841                         SIVAL(pdata, 28,
2842                                 current_user.nt_user_token->num_sids);
2843
2844                         /* We walk the SID list twice, but this call is fairly
2845                          * infrequent, and I don't expect that it's performance
2846                          * sensitive -- jpeach
2847                          */
2848                         for (i = 0, sid_bytes = 0;
2849                             i < current_user.nt_user_token->num_sids; ++i) {
2850                                 sid_bytes +=
2851                                     sid_size(&current_user.nt_user_token->user_sids[i]);
2852                         }
2853
2854                         /* SID list byte count */
2855                         SIVAL(pdata, 32, sid_bytes);
2856
2857                         /* 4 bytes pad/reserved - must be zero */
2858                         SIVAL(pdata, 36, 0);
2859                         data_len = 40;
2860
2861                         /* GID list */
2862                         for (i = 0; i < current_user.ut.ngroups; ++i) {
2863                                 SBIG_UINT(pdata, data_len,
2864                                         (SMB_BIG_UINT)current_user.ut.groups[i]);
2865                                 data_len += 8;
2866                         }
2867
2868                         /* SID list */
2869                         for (i = 0;
2870                             i < current_user.nt_user_token->num_sids; ++i) {
2871                                 int sid_len =
2872                                     sid_size(&current_user.nt_user_token->user_sids[i]);
2873
2874                                 sid_linearize(pdata + data_len, sid_len,
2875                                     &current_user.nt_user_token->user_sids[i]);
2876                                 data_len += sid_len;
2877                         }
2878
2879                         break;
2880                 }
2881
2882                 case SMB_MAC_QUERY_FS_INFO:
2883                         /*
2884                          * Thursby MAC extension... ONLY on NTFS filesystems
2885                          * once we do streams then we don't need this
2886                          */
2887                         if (strequal(lp_fstype(SNUM(conn)),"NTFS")) {
2888                                 data_len = 88;
2889                                 SIVAL(pdata,84,0x100); /* Don't support mac... */
2890                                 break;
2891                         }
2892                         /* drop through */
2893                 default:
2894                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2895                         return;
2896         }
2897
2898
2899         send_trans2_replies(req, params, 0, pdata, data_len,
2900                             max_data_bytes);
2901
2902         DEBUG( 4, ( "%s info_level = %d\n",
2903                     smb_fn_name(CVAL(req->inbuf,smb_com)), info_level) );
2904
2905         return;
2906 }
2907
2908 /****************************************************************************
2909  Reply to a TRANS2_SETFSINFO (set filesystem info).
2910 ****************************************************************************/
2911
2912 static void call_trans2setfsinfo(connection_struct *conn,
2913                                  struct smb_request *req,
2914                                  char **pparams, int total_params,
2915                                  char **ppdata, int total_data,
2916                                  unsigned int max_data_bytes)
2917 {
2918         char *pdata = *ppdata;
2919         char *params = *pparams;
2920         uint16 info_level;
2921
2922         DEBUG(10,("call_trans2setfsinfo: for service [%s]\n",lp_servicename(SNUM(conn))));
2923
2924         /*  */
2925         if (total_params < 4) {
2926                 DEBUG(0,("call_trans2setfsinfo: requires total_params(%d) >= 4 bytes!\n",
2927                         total_params));
2928                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2929                 return;
2930         }
2931
2932         info_level = SVAL(params,2);
2933
2934         switch(info_level) {
2935                 case SMB_SET_CIFS_UNIX_INFO:
2936                         {
2937                                 uint16 client_unix_major;
2938                                 uint16 client_unix_minor;
2939                                 uint32 client_unix_cap_low;
2940                                 uint32 client_unix_cap_high;
2941
2942                                 if (!lp_unix_extensions()) {
2943                                         reply_nterror(req,
2944                                                       NT_STATUS_INVALID_LEVEL);
2945                                         return;
2946                                 }
2947
2948                                 /* There should be 12 bytes of capabilities set. */
2949                                 if (total_data < 8) {
2950                                         reply_nterror(
2951                                                 req,
2952                                                 NT_STATUS_INVALID_PARAMETER);
2953                                         return;
2954                                 }
2955                                 client_unix_major = SVAL(pdata,0);
2956                                 client_unix_minor = SVAL(pdata,2);
2957                                 client_unix_cap_low = IVAL(pdata,4);
2958                                 client_unix_cap_high = IVAL(pdata,8);
2959                                 /* Just print these values for now. */
2960                                 DEBUG(10,("call_trans2setfsinfo: set unix info. major = %u, minor = %u \
2961 cap_low = 0x%x, cap_high = 0x%x\n",
2962                                         (unsigned int)client_unix_major,
2963                                         (unsigned int)client_unix_minor,
2964                                         (unsigned int)client_unix_cap_low,
2965                                         (unsigned int)client_unix_cap_high ));
2966
2967                                 /* Here is where we must switch to posix pathname processing... */
2968                                 if (client_unix_cap_low & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2969                                         lp_set_posix_pathnames();
2970                                         mangle_change_to_posix();
2971                                 }
2972
2973                                 if ((client_unix_cap_low & CIFS_UNIX_FCNTL_LOCKS_CAP) &&
2974                                     !(client_unix_cap_low & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP)) {
2975                                         /* Client that knows how to do posix locks,
2976                                          * but not posix open/mkdir operations. Set a
2977                                          * default type for read/write checks. */
2978
2979                                         lp_set_posix_default_cifsx_readwrite_locktype(POSIX_LOCK);
2980
2981                                 }
2982                                 break;
2983                         }
2984                 case SMB_REQUEST_TRANSPORT_ENCRYPTION:
2985                         {
2986                                 NTSTATUS status;
2987                                 size_t param_len = 0;
2988                                 size_t data_len = total_data;
2989
2990                                 if (!lp_unix_extensions()) {
2991                                         reply_nterror(
2992                                                 req, NT_STATUS_INVALID_LEVEL);
2993                                         return;
2994                                 }
2995
2996                                 DEBUG( 4,("call_trans2setfsinfo: request transport encrption.\n"));
2997
2998                                 status = srv_request_encryption_setup(conn,
2999                                                                         (unsigned char **)ppdata,
3000                                                                         &data_len,
3001                                                                         (unsigned char **)pparams,
3002                                                                         &param_len
3003                                                                         );
3004
3005                                 if (!NT_STATUS_IS_OK(status)) {
3006                                         /*
3007                                          * TODO: Check
3008                                          * MORE_PROCESSING_REQUIRED, this used
3009                                          * to have special handling here.
3010                                          */
3011                                         reply_nterror(req, status);
3012                                         return;
3013                                 }
3014
3015                                 send_trans2_replies(req,
3016                                                     *pparams, param_len,
3017                                                     *ppdata, data_len,
3018                                                     max_data_bytes);
3019
3020                                 if (NT_STATUS_IS_OK(status)) {
3021                                         /* Server-side transport encryption is now *on*. */
3022                                         status = srv_encryption_start(conn);
3023                                         if (!NT_STATUS_IS_OK(status)) {
3024                                                 exit_server_cleanly("Failure in setting up encrypted transport");
3025                                         }
3026                                 }
3027                                 return;
3028                         }
3029                 case SMB_FS_QUOTA_INFORMATION:
3030                         {
3031                                 files_struct *fsp = NULL;
3032                                 SMB_NTQUOTA_STRUCT quotas;
3033         
3034                                 ZERO_STRUCT(quotas);
3035
3036                                 /* access check */
3037                                 if ((current_user.ut.uid != 0)||!CAN_WRITE(conn)) {
3038                                         DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
3039                                                 lp_servicename(SNUM(conn)),conn->user));
3040                                         reply_doserror(req, ERRSRV, ERRaccess);
3041                                         return;
3042                                 }
3043
3044                                 /* note: normaly there're 48 bytes,
3045                                  * but we didn't use the last 6 bytes for now 
3046                                  * --metze 
3047                                  */
3048                                 fsp = file_fsp(SVAL(params,0));
3049                                 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
3050                                         DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
3051                                         reply_nterror(
3052                                                 req, NT_STATUS_INVALID_HANDLE);
3053                                         return;
3054                                 }
3055
3056                                 if (total_data < 42) {
3057                                         DEBUG(0,("call_trans2setfsinfo: SET_FS_QUOTA: requires total_data(%d) >= 42 bytes!\n",
3058                                                 total_data));
3059                                         reply_nterror(
3060                                                 req,
3061                                                 NT_STATUS_INVALID_PARAMETER);
3062                                         return;
3063                                 }
3064                         
3065                                 /* unknown_1 24 NULL bytes in pdata*/
3066                 
3067                                 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
3068                                 quotas.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
3069 #ifdef LARGE_SMB_OFF_T
3070                                 quotas.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
3071 #else /* LARGE_SMB_OFF_T */
3072                                 if ((IVAL(pdata,28) != 0)&&
3073                                         ((quotas.softlim != 0xFFFFFFFF)||
3074                                         (IVAL(pdata,28)!=0xFFFFFFFF))) {
3075                                         /* more than 32 bits? */
3076                                         reply_nterror(
3077                                                 req,
3078                                                 NT_STATUS_INVALID_PARAMETER);
3079                                         return;
3080                                 }
3081 #endif /* LARGE_SMB_OFF_T */
3082                 
3083                                 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
3084                                 quotas.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
3085 #ifdef LARGE_SMB_OFF_T
3086                                 quotas.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
3087 #else /* LARGE_SMB_OFF_T */
3088                                 if ((IVAL(pdata,36) != 0)&&
3089                                         ((quotas.hardlim != 0xFFFFFFFF)||
3090                                         (IVAL(pdata,36)!=0xFFFFFFFF))) {
3091                                         /* more than 32 bits? */
3092                                         reply_nterror(
3093                                                 req,
3094                                                 NT_STATUS_INVALID_PARAMETER);
3095                                         return;
3096                                 }
3097 #endif /* LARGE_SMB_OFF_T */
3098                 
3099                                 /* quota_flags 2 bytes **/
3100                                 quotas.qflags = SVAL(pdata,40);
3101                 
3102                                 /* unknown_2 6 NULL bytes follow*/
3103                 
3104                                 /* now set the quotas */
3105                                 if (vfs_set_ntquota(fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
3106                                         DEBUG(0,("vfs_set_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
3107                                         reply_doserror(req, ERRSRV, ERRerror);
3108                                         return;
3109                                 }
3110                         
3111                                 break;
3112                         }
3113                 default:
3114                         DEBUG(3,("call_trans2setfsinfo: unknown level (0x%X) not implemented yet.\n",
3115                                 info_level));
3116                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3117                         return;
3118                         break;
3119         }
3120
3121         /* 
3122          * sending this reply works fine, 
3123          * but I'm not sure it's the same 
3124          * like windows do...
3125          * --metze
3126          */
3127         reply_outbuf(req, 10, 0);
3128 }
3129
3130 #if defined(HAVE_POSIX_ACLS)
3131 /****************************************************************************
3132  Utility function to count the number of entries in a POSIX acl.
3133 ****************************************************************************/
3134
3135 static unsigned int count_acl_entries(connection_struct *conn, SMB_ACL_T posix_acl)
3136 {
3137         unsigned int ace_count = 0;
3138         int entry_id = SMB_ACL_FIRST_ENTRY;
3139         SMB_ACL_ENTRY_T entry;
3140
3141         while ( posix_acl && (SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1)) {
3142                 /* get_next... */
3143                 if (entry_id == SMB_ACL_FIRST_ENTRY) {
3144                         entry_id = SMB_ACL_NEXT_ENTRY;
3145                 }
3146                 ace_count++;
3147         }
3148         return ace_count;
3149 }
3150
3151 /****************************************************************************
3152  Utility function to marshall a POSIX acl into wire format.
3153 ****************************************************************************/
3154
3155 static BOOL marshall_posix_acl(connection_struct *conn, char *pdata, SMB_STRUCT_STAT *pst, SMB_ACL_T posix_acl)
3156 {
3157         int entry_id = SMB_ACL_FIRST_ENTRY;
3158         SMB_ACL_ENTRY_T entry;
3159
3160         while ( posix_acl && (SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1)) {
3161                 SMB_ACL_TAG_T tagtype;
3162                 SMB_ACL_PERMSET_T permset;
3163                 unsigned char perms = 0;
3164                 unsigned int own_grp;
3165
3166                 /* get_next... */
3167                 if (entry_id == SMB_ACL_FIRST_ENTRY) {
3168                         entry_id = SMB_ACL_NEXT_ENTRY;
3169                 }
3170
3171                 if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1) {
3172                         DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_TAG_TYPE failed.\n"));
3173                         return False;
3174                 }
3175
3176                 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, entry, &permset) == -1) {
3177                         DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_PERMSET failed.\n"));
3178                         return False;
3179                 }
3180
3181                 perms |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_READ) ? SMB_POSIX_ACL_READ : 0);
3182                 perms |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_WRITE) ? SMB_POSIX_ACL_WRITE : 0);
3183                 perms |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_EXECUTE) ? SMB_POSIX_ACL_EXECUTE : 0);
3184
3185                 SCVAL(pdata,1,perms);
3186
3187                 switch (tagtype) {
3188                         case SMB_ACL_USER_OBJ:
3189                                 SCVAL(pdata,0,SMB_POSIX_ACL_USER_OBJ);
3190                                 own_grp = (unsigned int)pst->st_uid;
3191                                 SIVAL(pdata,2,own_grp);
3192                                 SIVAL(pdata,6,0);
3193                                 break;
3194                         case SMB_ACL_USER:
3195                                 {
3196                                         uid_t *puid = (uid_t *)SMB_VFS_SYS_ACL_GET_QUALIFIER(conn, entry);
3197                                         if (!puid) {
3198                                                 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_QUALIFIER failed.\n"));
3199                                                 return False;
3200                                         }
3201                                         own_grp = (unsigned int)*puid;
3202                                         SMB_VFS_SYS_ACL_FREE_QUALIFIER(conn, (void *)puid,tagtype);
3203                                         SCVAL(pdata,0,SMB_POSIX_ACL_USER);
3204                                         SIVAL(pdata,2,own_grp);
3205                                         SIVAL(pdata,6,0);
3206                                         break;
3207                                 }
3208                         case SMB_ACL_GROUP_OBJ:
3209                                 SCVAL(pdata,0,SMB_POSIX_ACL_GROUP_OBJ);
3210                                 own_grp = (unsigned int)pst->st_gid;
3211                                 SIVAL(pdata,2,own_grp);
3212                                 SIVAL(pdata,6,0);
3213                                 break;
3214                         case SMB_ACL_GROUP:
3215                                 {
3216                                         gid_t *pgid= (gid_t *)SMB_VFS_SYS_ACL_GET_QUALIFIER(conn, entry);
3217                                         if (!pgid) {
3218                                                 DEBUG(0,("marshall_posix_acl: SMB_VFS_SYS_ACL_GET_QUALIFIER failed.\n"));
3219                                                 return False;
3220                                         }
3221                                         own_grp = (unsigned int)*pgid;
3222                                         SMB_VFS_SYS_ACL_FREE_QUALIFIER(conn, (void *)pgid,tagtype);
3223                                         SCVAL(pdata,0,SMB_POSIX_ACL_GROUP);
3224                                         SIVAL(pdata,2,own_grp);
3225                                         SIVAL(pdata,6,0);
3226                                         break;
3227                                 }
3228                         case SMB_ACL_MASK:
3229                                 SCVAL(pdata,0,SMB_POSIX_ACL_MASK);
3230                                 SIVAL(pdata,2,0xFFFFFFFF);
3231                                 SIVAL(pdata,6,0xFFFFFFFF);
3232                                 break;
3233                         case SMB_ACL_OTHER:
3234                                 SCVAL(pdata,0,SMB_POSIX_ACL_OTHER);
3235                                 SIVAL(pdata,2,0xFFFFFFFF);
3236                                 SIVAL(pdata,6,0xFFFFFFFF);
3237                                 break;
3238                         default:
3239                                 DEBUG(0,("marshall_posix_acl: unknown tagtype.\n"));
3240                                 return False;
3241                 }
3242                 pdata += SMB_POSIX_ACL_ENTRY_SIZE;
3243         }
3244
3245         return True;
3246 }
3247 #endif
3248
3249 /****************************************************************************
3250  Store the FILE_UNIX_BASIC info.
3251 ****************************************************************************/
3252
3253 static char *store_file_unix_basic(connection_struct *conn,
3254                                 char *pdata,
3255                                 files_struct *fsp,
3256                                 const SMB_STRUCT_STAT *psbuf)
3257 {
3258         DEBUG(10,("store_file_unix_basic: SMB_QUERY_FILE_UNIX_BASIC\n"));
3259         DEBUG(4,("store_file_unix_basic: st_mode=%o\n",(int)psbuf->st_mode));
3260
3261         SOFF_T(pdata,0,get_file_size(*psbuf));             /* File size 64 Bit */
3262         pdata += 8;
3263
3264         SOFF_T(pdata,0,get_allocation_size(conn,fsp,psbuf)); /* Number of bytes used on disk - 64 Bit */
3265         pdata += 8;
3266
3267         put_long_date_timespec(pdata,get_ctimespec(psbuf));       /* Change Time 64 Bit */
3268         put_long_date_timespec(pdata+8,get_atimespec(psbuf));     /* Last access time 64 Bit */
3269         put_long_date_timespec(pdata+16,get_mtimespec(psbuf));    /* Last modification time 64 Bit */
3270         pdata += 24;
3271
3272         SIVAL(pdata,0,psbuf->st_uid);               /* user id for the owner */
3273         SIVAL(pdata,4,0);
3274         pdata += 8;
3275
3276         SIVAL(pdata,0,psbuf->st_gid);               /* group id of owner */
3277         SIVAL(pdata,4,0);
3278         pdata += 8;
3279
3280         SIVAL(pdata,0,unix_filetype(psbuf->st_mode));
3281         pdata += 4;
3282
3283         SIVAL(pdata,0,unix_dev_major(psbuf->st_rdev));   /* Major device number if type is device */
3284         SIVAL(pdata,4,0);
3285         pdata += 8;
3286
3287         SIVAL(pdata,0,unix_dev_minor(psbuf->st_rdev));   /* Minor device number if type is device */
3288         SIVAL(pdata,4,0);
3289         pdata += 8;
3290
3291         SINO_T_VAL(pdata,0,(SMB_INO_T)psbuf->st_ino);   /* inode number */
3292         pdata += 8;
3293                                 
3294         SIVAL(pdata,0, unix_perms_to_wire(psbuf->st_mode));     /* Standard UNIX file permissions */
3295         SIVAL(pdata,4,0);
3296         pdata += 8;
3297
3298         SIVAL(pdata,0,psbuf->st_nlink);             /* number of hard links */
3299         SIVAL(pdata,4,0);
3300         pdata += 8;
3301
3302         return pdata;
3303 }
3304
3305 /* Forward and reverse mappings from the UNIX_INFO2 file flags field and
3306  * the chflags(2) (or equivalent) flags.
3307  *
3308  * XXX:&n