r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[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
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 static const char *prohibited_ea_names[] = {
94         SAMBA_POSIX_INHERITANCE_EA_NAME,
95         SAMBA_XATTR_DOS_ATTRIB,
96         NULL
97 };
98
99 /****************************************************************************
100  Refuse to allow clients to overwrite our private xattrs.
101 ****************************************************************************/
102
103 static BOOL samba_private_attr_name(const char *unix_ea_name)
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 int send_trans2_replies(const char *inbuf,
580                         char *outbuf,
581                         int bufsize,
582                         const char *params, 
583                         int paramsize,
584                         const char *pdata,
585                         int datasize,
586                         int max_data_bytes)
587 {
588         /* As we are using a protocol > LANMAN1 then the max_send
589          variable must have been set in the sessetupX call.
590          This takes precedence over the max_xmit field in the
591          global struct. These different max_xmit variables should
592          be merged as this is now too confusing */
593
594         int data_to_send = datasize;
595         int params_to_send = paramsize;
596         int useable_space;
597         const char *pp = params;
598         const char *pd = pdata;
599         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
600         int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
601         int data_alignment_offset = 0;
602
603         /* Initially set the wcnt area to be 10 - this is true for all trans2 replies */
604         
605         set_message(inbuf,outbuf,10,0,True);
606
607         /* Modify the data_to_send and datasize and set the error if
608            we're trying to send more than max_data_bytes. We still send
609            the part of the packet(s) that fit. Strange, but needed
610            for OS/2. */
611
612         if (max_data_bytes > 0 && datasize > max_data_bytes) {
613                 DEBUG(5,("send_trans2_replies: max_data_bytes %d exceeded by data %d\n",
614                         max_data_bytes, datasize ));
615                 datasize = data_to_send = max_data_bytes;
616                 error_packet_set(outbuf,ERRDOS,ERRbufferoverflow,STATUS_BUFFER_OVERFLOW,__LINE__,__FILE__);
617         }
618
619         /* If there genuinely are no parameters or data to send just send the empty packet */
620
621         if(params_to_send == 0 && data_to_send == 0) {
622                 show_msg(outbuf);
623                 if (!send_smb(smbd_server_fd(),outbuf))
624                         exit_server_cleanly("send_trans2_replies: send_smb failed.");
625                 return 0;
626         }
627
628         /* When sending params and data ensure that both are nicely aligned */
629         /* Only do this alignment when there is also data to send - else
630                 can cause NT redirector problems. */
631
632         if (((params_to_send % 4) != 0) && (data_to_send != 0))
633                 data_alignment_offset = 4 - (params_to_send % 4);
634
635         /* Space is bufsize minus Netbios over TCP header minus SMB header */
636         /* The alignment_offset is to align the param bytes on an even byte
637                 boundary. NT 4.0 Beta needs this to work correctly. */
638
639         useable_space = bufsize - ((smb_buf(outbuf)+ alignment_offset+data_alignment_offset) - outbuf);
640
641         /* useable_space can never be more than max_send minus the alignment offset. */
642
643         useable_space = MIN(useable_space, max_send - (alignment_offset+data_alignment_offset));
644
645         while (params_to_send || data_to_send) {
646                 /* Calculate whether we will totally or partially fill this packet */
647
648                 total_sent_thistime = params_to_send + data_to_send + alignment_offset + data_alignment_offset;
649
650                 /* We can never send more than useable_space */
651                 /*
652                  * Note that 'useable_space' does not include the alignment offsets,
653                  * but we must include the alignment offsets in the calculation of
654                  * the length of the data we send over the wire, as the alignment offsets
655                  * are sent here. Fix from Marc_Jacobsen@hp.com.
656                  */
657
658                 total_sent_thistime = MIN(total_sent_thistime, useable_space+ alignment_offset + data_alignment_offset);
659
660                 set_message(inbuf, outbuf, 10, total_sent_thistime, True);
661
662                 /* Set total params and data to be sent */
663                 SSVAL(outbuf,smb_tprcnt,paramsize);
664                 SSVAL(outbuf,smb_tdrcnt,datasize);
665
666                 /* Calculate how many parameters and data we can fit into
667                  * this packet. Parameters get precedence
668                  */
669
670                 params_sent_thistime = MIN(params_to_send,useable_space);
671                 data_sent_thistime = useable_space - params_sent_thistime;
672                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
673
674                 SSVAL(outbuf,smb_prcnt, params_sent_thistime);
675
676                 /* smb_proff is the offset from the start of the SMB header to the
677                         parameter bytes, however the first 4 bytes of outbuf are
678                         the Netbios over TCP header. Thus use smb_base() to subtract
679                         them from the calculation */
680
681                 SSVAL(outbuf,smb_proff,((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
682
683                 if(params_sent_thistime == 0)
684                         SSVAL(outbuf,smb_prdisp,0);
685                 else
686                         /* Absolute displacement of param bytes sent in this packet */
687                         SSVAL(outbuf,smb_prdisp,pp - params);
688
689                 SSVAL(outbuf,smb_drcnt, data_sent_thistime);
690                 if(data_sent_thistime == 0) {
691                         SSVAL(outbuf,smb_droff,0);
692                         SSVAL(outbuf,smb_drdisp, 0);
693                 } else {
694                         /* The offset of the data bytes is the offset of the
695                                 parameter bytes plus the number of parameters being sent this time */
696                         SSVAL(outbuf,smb_droff,((smb_buf(outbuf)+alignment_offset) - 
697                                 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
698                         SSVAL(outbuf,smb_drdisp, pd - pdata);
699                 }
700
701                 /* Copy the param bytes into the packet */
702
703                 if(params_sent_thistime)
704                         memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
705
706                 /* Copy in the data bytes */
707                 if(data_sent_thistime)
708                         memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
709                                 data_alignment_offset,pd,data_sent_thistime);
710
711                 DEBUG(9,("t2_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
712                         params_sent_thistime, data_sent_thistime, useable_space));
713                 DEBUG(9,("t2_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
714                         params_to_send, data_to_send, paramsize, datasize));
715
716                 /* Send the packet */
717                 show_msg(outbuf);
718                 if (!send_smb(smbd_server_fd(),outbuf))
719                         exit_server_cleanly("send_trans2_replies: send_smb failed.");
720
721                 pp += params_sent_thistime;
722                 pd += data_sent_thistime;
723
724                 params_to_send -= params_sent_thistime;
725                 data_to_send -= data_sent_thistime;
726
727                 /* Sanity check */
728                 if(params_to_send < 0 || data_to_send < 0) {
729                         DEBUG(0,("send_trans2_replies failed sanity check pts = %d, dts = %d\n!!!",
730                                 params_to_send, data_to_send));
731                         return -1;
732                 }
733         }
734
735         return 0;
736 }
737
738 /****************************************************************************
739  Reply to a TRANSACT2_OPEN.
740 ****************************************************************************/
741
742 static int call_trans2open(connection_struct *conn,
743                            struct smb_request *req,
744                            char *inbuf, char *outbuf, int bufsize,
745                            char **pparams, int total_params,
746                            char **ppdata, int total_data,
747                            unsigned int max_data_bytes)
748 {
749         char *params = *pparams;
750         char *pdata = *ppdata;
751         int deny_mode;
752         int32 open_attr;
753         BOOL oplock_request;
754 #if 0
755         BOOL return_additional_info;
756         int16 open_sattr;
757         time_t open_time;
758 #endif
759         int open_ofun;
760         uint32 open_size;
761         char *pname;
762         pstring fname;
763         SMB_OFF_T size=0;
764         int fattr=0,mtime=0;
765         SMB_INO_T inode = 0;
766         SMB_STRUCT_STAT sbuf;
767         int smb_action = 0;
768         files_struct *fsp;
769         struct ea_list *ea_list = NULL;
770         uint16 flags = 0;
771         NTSTATUS status;
772         uint32 access_mask;
773         uint32 share_mode;
774         uint32 create_disposition;
775         uint32 create_options = 0;
776
777         /*
778          * Ensure we have enough parameters to perform the operation.
779          */
780
781         if (total_params < 29) {
782                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
783         }
784
785         flags = SVAL(params, 0);
786         deny_mode = SVAL(params, 2);
787         open_attr = SVAL(params,6);
788         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
789         if (oplock_request) {
790                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
791         }
792
793 #if 0
794         return_additional_info = BITSETW(params,0);
795         open_sattr = SVAL(params, 4);
796         open_time = make_unix_date3(params+8);
797 #endif
798         open_ofun = SVAL(params,12);
799         open_size = IVAL(params,14);
800         pname = &params[28];
801
802         if (IS_IPC(conn)) {
803                 return(ERROR_DOS(ERRSRV,ERRaccess));
804         }
805
806         srvstr_get_path(inbuf, SVAL(inbuf,smb_flg2), fname, pname,
807                         sizeof(fname), total_params - 28, STR_TERMINATE,
808                         &status);
809         if (!NT_STATUS_IS_OK(status)) {
810                 return ERROR_NT(status);
811         }
812
813         DEBUG(3,("call_trans2open %s deny_mode=0x%x attr=%d ofun=0x%x size=%d\n",
814                 fname, (unsigned int)deny_mode, (unsigned int)open_attr,
815                 (unsigned int)open_ofun, open_size));
816
817         /* XXXX we need to handle passed times, sattr and flags */
818
819         status = unix_convert(conn, fname, False, NULL, &sbuf);
820         if (!NT_STATUS_IS_OK(status)) {
821                 return ERROR_NT(status);
822         }
823     
824         status = check_name(conn, fname);
825         if (!NT_STATUS_IS_OK(status)) {
826                 return ERROR_NT(status);
827         }
828
829         if (open_ofun == 0) {
830                 return ERROR_NT(NT_STATUS_OBJECT_NAME_COLLISION);
831         }
832
833         if (!map_open_params_to_ntcreate(fname, deny_mode, open_ofun,
834                                 &access_mask,
835                                 &share_mode,
836                                 &create_disposition,
837                                 &create_options)) {
838                 return ERROR_DOS(ERRDOS, ERRbadaccess);
839         }
840
841         /* Any data in this call is an EA list. */
842         if (total_data && (total_data != 4) && !lp_ea_support(SNUM(conn))) {
843                 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
844         }
845
846         if (total_data != 4) {
847                 if (total_data < 10) {
848                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
849                 }
850
851                 if (IVAL(pdata,0) > total_data) {
852                         DEBUG(10,("call_trans2open: bad total data size (%u) > %u\n",
853                                 IVAL(pdata,0), (unsigned int)total_data));
854                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
855                 }
856
857                 ea_list = read_ea_list(tmp_talloc_ctx(), pdata + 4,
858                                        total_data - 4);
859                 if (!ea_list) {
860                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
861                 }
862         } else if (IVAL(pdata,0) != 4) {
863                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
864         }
865
866         status = open_file_ntcreate(conn, req, fname, &sbuf,
867                 access_mask,
868                 share_mode,
869                 create_disposition,
870                 create_options,
871                 open_attr,
872                 oplock_request,
873                 &smb_action, &fsp);
874       
875         if (!NT_STATUS_IS_OK(status)) {
876                 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
877                         /* We have re-scheduled this call. */
878                         return -1;
879                 }
880                 return ERROR_NT(status);
881         }
882
883         size = get_file_size(sbuf);
884         fattr = dos_mode(conn,fname,&sbuf);
885         mtime = sbuf.st_mtime;
886         inode = sbuf.st_ino;
887         if (fattr & aDIR) {
888                 close_file(fsp,ERROR_CLOSE);
889                 return(ERROR_DOS(ERRDOS,ERRnoaccess));
890         }
891
892         /* Save the requested allocation size. */
893         /* Allocate space for the file if a size hint is supplied */
894         if ((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) {
895                 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)open_size;
896                 if (allocation_size && (allocation_size > (SMB_BIG_UINT)size)) {
897                         fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
898                         if (fsp->is_directory) {
899                                 close_file(fsp,ERROR_CLOSE);
900                                 /* Can't set allocation size on a directory. */
901                                 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
902                         }
903                         if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
904                                 close_file(fsp,ERROR_CLOSE);
905                                 return ERROR_NT(NT_STATUS_DISK_FULL);
906                         }
907
908                         /* Adjust size here to return the right size in the reply.
909                            Windows does it this way. */
910                         size = fsp->initial_allocation_size;
911                 } else {
912                         fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)size);
913                 }
914         }
915
916         if (ea_list && smb_action == FILE_WAS_CREATED) {
917                 status = set_ea(conn, fsp, fname, ea_list);
918                 if (!NT_STATUS_IS_OK(status)) {
919                         close_file(fsp,ERROR_CLOSE);
920                         return ERROR_NT(status);
921                 }
922         }
923
924         /* Realloc the size of parameters and data we will return */
925         *pparams = (char *)SMB_REALLOC(*pparams, 30);
926         if(*pparams == NULL ) {
927                 return ERROR_NT(NT_STATUS_NO_MEMORY);
928         }
929         params = *pparams;
930
931         SSVAL(params,0,fsp->fnum);
932         SSVAL(params,2,fattr);
933         srv_put_dos_date2(params,4, mtime);
934         SIVAL(params,8, (uint32)size);
935         SSVAL(params,12,deny_mode);
936         SSVAL(params,14,0); /* open_type - file or directory. */
937         SSVAL(params,16,0); /* open_state - only valid for IPC device. */
938
939         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
940                 smb_action |= EXTENDED_OPLOCK_GRANTED;
941         }
942
943         SSVAL(params,18,smb_action);
944
945         /*
946          * WARNING - this may need to be changed if SMB_INO_T <> 4 bytes.
947          */
948         SIVAL(params,20,inode);
949         SSVAL(params,24,0); /* Padding. */
950         if (flags & 8) {
951                 uint32 ea_size = estimate_ea_size(conn, fsp, fname);
952                 SIVAL(params, 26, ea_size);
953         } else {
954                 SIVAL(params, 26, 0);
955         }
956
957         /* Send the required number of replies */
958         send_trans2_replies(inbuf, outbuf, bufsize, params, 30, *ppdata, 0, max_data_bytes);
959
960         return -1;
961 }
962
963 /*********************************************************
964  Routine to check if a given string matches exactly.
965  as a special case a mask of "." does NOT match. That
966  is required for correct wildcard semantics
967  Case can be significant or not.
968 **********************************************************/
969
970 static BOOL exact_match(connection_struct *conn, char *str, char *mask)
971 {
972         if (mask[0] == '.' && mask[1] == 0)
973                 return False;
974         if (conn->case_sensitive)
975                 return strcmp(str,mask)==0;
976         if (StrCaseCmp(str,mask) != 0) {
977                 return False;
978         }
979         if (dptr_has_wild(conn->dirptr)) {
980                 return False;
981         }
982         return True;
983 }
984
985 /****************************************************************************
986  Return the filetype for UNIX extensions.
987 ****************************************************************************/
988
989 static uint32 unix_filetype(mode_t mode)
990 {
991         if(S_ISREG(mode))
992                 return UNIX_TYPE_FILE;
993         else if(S_ISDIR(mode))
994                 return UNIX_TYPE_DIR;
995 #ifdef S_ISLNK
996         else if(S_ISLNK(mode))
997                 return UNIX_TYPE_SYMLINK;
998 #endif
999 #ifdef S_ISCHR
1000         else if(S_ISCHR(mode))
1001                 return UNIX_TYPE_CHARDEV;
1002 #endif
1003 #ifdef S_ISBLK
1004         else if(S_ISBLK(mode))
1005                 return UNIX_TYPE_BLKDEV;
1006 #endif
1007 #ifdef S_ISFIFO
1008         else if(S_ISFIFO(mode))
1009                 return UNIX_TYPE_FIFO;
1010 #endif
1011 #ifdef S_ISSOCK
1012         else if(S_ISSOCK(mode))
1013                 return UNIX_TYPE_SOCKET;
1014 #endif
1015
1016         DEBUG(0,("unix_filetype: unknown filetype %u", (unsigned)mode));
1017         return UNIX_TYPE_UNKNOWN;
1018 }
1019
1020 /****************************************************************************
1021  Map wire perms onto standard UNIX permissions. Obey share restrictions.
1022 ****************************************************************************/
1023
1024 enum perm_type { PERM_NEW_FILE, PERM_NEW_DIR, PERM_EXISTING_FILE, PERM_EXISTING_DIR};
1025
1026 static NTSTATUS unix_perms_from_wire( connection_struct *conn,
1027                                 SMB_STRUCT_STAT *psbuf,
1028                                 uint32 perms,
1029                                 enum perm_type ptype,
1030                                 mode_t *ret_perms)
1031 {
1032         mode_t ret = 0;
1033
1034         if (perms == SMB_MODE_NO_CHANGE) {
1035                 if (!VALID_STAT(*psbuf)) {
1036                         return NT_STATUS_INVALID_PARAMETER;
1037                 } else {
1038                         *ret_perms = psbuf->st_mode;
1039                         return NT_STATUS_OK;
1040                 }
1041         }
1042
1043         ret |= ((perms & UNIX_X_OTH ) ? S_IXOTH : 0);
1044         ret |= ((perms & UNIX_W_OTH ) ? S_IWOTH : 0);
1045         ret |= ((perms & UNIX_R_OTH ) ? S_IROTH : 0);
1046         ret |= ((perms & UNIX_X_GRP ) ? S_IXGRP : 0);
1047         ret |= ((perms & UNIX_W_GRP ) ? S_IWGRP : 0);
1048         ret |= ((perms & UNIX_R_GRP ) ? S_IRGRP : 0);
1049         ret |= ((perms & UNIX_X_USR ) ? S_IXUSR : 0);
1050         ret |= ((perms & UNIX_W_USR ) ? S_IWUSR : 0);
1051         ret |= ((perms & UNIX_R_USR ) ? S_IRUSR : 0);
1052 #ifdef S_ISVTX
1053         ret |= ((perms & UNIX_STICKY ) ? S_ISVTX : 0);
1054 #endif
1055 #ifdef S_ISGID
1056         ret |= ((perms & UNIX_SET_GID ) ? S_ISGID : 0);
1057 #endif
1058 #ifdef S_ISUID
1059         ret |= ((perms & UNIX_SET_UID ) ? S_ISUID : 0);
1060 #endif
1061
1062         switch (ptype) {
1063         case PERM_NEW_FILE:
1064                 /* Apply mode mask */
1065                 ret &= lp_create_mask(SNUM(conn));
1066                 /* Add in force bits */
1067                 ret |= lp_force_create_mode(SNUM(conn));
1068                 break;
1069         case PERM_NEW_DIR:
1070                 ret &= lp_dir_mask(SNUM(conn));
1071                 /* Add in force bits */
1072                 ret |= lp_force_dir_mode(SNUM(conn));
1073                 break;
1074         case PERM_EXISTING_FILE:
1075                 /* Apply mode mask */
1076                 ret &= lp_security_mask(SNUM(conn));
1077                 /* Add in force bits */
1078                 ret |= lp_force_security_mode(SNUM(conn));
1079                 break;
1080         case PERM_EXISTING_DIR:
1081                 /* Apply mode mask */
1082                 ret &= lp_dir_security_mask(SNUM(conn));
1083                 /* Add in force bits */
1084                 ret |= lp_force_dir_security_mode(SNUM(conn));
1085                 break;
1086         }
1087
1088         *ret_perms = ret;
1089         return NT_STATUS_OK;
1090 }
1091
1092 /****************************************************************************
1093  Get a level dependent lanman2 dir entry.
1094 ****************************************************************************/
1095
1096 static BOOL get_lanman2_dir_entry(connection_struct *conn,
1097                                   void *inbuf, char *outbuf,
1098                                  char *path_mask,uint32 dirtype,int info_level,
1099                                  int requires_resume_key,
1100                                  BOOL dont_descend,char **ppdata, 
1101                                  char *base_data, int space_remaining, 
1102                                  BOOL *out_of_space, BOOL *got_exact_match,
1103                                  int *last_entry_off, struct ea_list *name_list, TALLOC_CTX *ea_ctx)
1104 {
1105         const char *dname;
1106         BOOL found = False;
1107         SMB_STRUCT_STAT sbuf;
1108         pstring mask;
1109         pstring pathreal;
1110         pstring fname;
1111         char *p, *q, *pdata = *ppdata;
1112         uint32 reskey=0;
1113         long prev_dirpos=0;
1114         uint32 mode=0;
1115         SMB_OFF_T file_size = 0;
1116         SMB_BIG_UINT allocation_size = 0;
1117         uint32 len;
1118         struct timespec mdate_ts, adate_ts, create_date_ts;
1119         time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
1120         char *nameptr;
1121         char *last_entry_ptr;
1122         BOOL was_8_3;
1123         uint32 nt_extmode; /* Used for NT connections instead of mode */
1124         BOOL needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
1125         BOOL check_mangled_names = lp_manglednames(conn->params);
1126
1127         *fname = 0;
1128         *out_of_space = False;
1129         *got_exact_match = False;
1130
1131         ZERO_STRUCT(mdate_ts);
1132         ZERO_STRUCT(adate_ts);
1133         ZERO_STRUCT(create_date_ts);
1134
1135         if (!conn->dirptr)
1136                 return(False);
1137
1138         p = strrchr_m(path_mask,'/');
1139         if(p != NULL) {
1140                 if(p[1] == '\0')
1141                         pstrcpy(mask,"*.*");
1142                 else
1143                         pstrcpy(mask, p+1);
1144         } else
1145                 pstrcpy(mask, path_mask);
1146
1147
1148         while (!found) {
1149                 BOOL got_match;
1150                 BOOL ms_dfs_link = False;
1151
1152                 /* Needed if we run out of space */
1153                 long curr_dirpos = prev_dirpos = dptr_TellDir(conn->dirptr);
1154                 dname = dptr_ReadDirName(conn->dirptr,&curr_dirpos,&sbuf);
1155
1156                 /*
1157                  * Due to bugs in NT client redirectors we are not using
1158                  * resume keys any more - set them to zero.
1159                  * Check out the related comments in findfirst/findnext.
1160                  * JRA.
1161                  */
1162
1163                 reskey = 0;
1164
1165                 DEBUG(8,("get_lanman2_dir_entry:readdir on dirptr 0x%lx now at offset %ld\n",
1166                         (long)conn->dirptr,curr_dirpos));
1167       
1168                 if (!dname) {
1169                         return(False);
1170                 }
1171
1172                 /*
1173                  * fname may get mangled, dname is never mangled.
1174                  * Whenever we're accessing the filesystem we use
1175                  * pathreal which is composed from dname.
1176                  */
1177
1178                 pstrcpy(fname,dname);      
1179
1180                 /* This will mangle fname if it's an illegal name. */
1181                 mangle_map(fname,False,True,conn->params);
1182
1183                 if(!(got_match = *got_exact_match = exact_match(conn, fname, mask))) {
1184                         got_match = mask_match(fname, mask, conn->case_sensitive);
1185                 }
1186
1187                 if(!got_match && check_mangled_names &&
1188                    !mangle_is_8_3(fname, False, conn->params)) {
1189                         pstring mangled_name;
1190
1191                         /*
1192                          * It turns out that NT matches wildcards against
1193                          * both long *and* short names. This may explain some
1194                          * of the wildcard wierdness from old DOS clients
1195                          * that some people have been seeing.... JRA.
1196                          */
1197
1198                         pstrcpy(mangled_name, fname);
1199
1200                         /* Force the mangling into 8.3. */
1201                         mangle_map( mangled_name, True, False, conn->params);
1202                         if(!(got_match = *got_exact_match = exact_match(conn, mangled_name, mask))) {
1203                                 got_match = mask_match(mangled_name, mask, conn->case_sensitive);
1204                         }
1205                 }
1206
1207                 if (got_match) {
1208                         BOOL isdots = (strequal(dname,"..") || strequal(dname,"."));
1209                         if (dont_descend && !isdots) {
1210                                 continue;
1211                         }
1212           
1213                         pstrcpy(pathreal,conn->dirpath);
1214                         if(needslash) {
1215                                 pstrcat(pathreal,"/");
1216                         }
1217                         pstrcat(pathreal,dname);
1218
1219                         if (INFO_LEVEL_IS_UNIX(info_level)) {
1220                                 if (SMB_VFS_LSTAT(conn,pathreal,&sbuf) != 0) {
1221                                         DEBUG(5,("get_lanman2_dir_entry:Couldn't lstat [%s] (%s)\n",
1222                                                 pathreal,strerror(errno)));
1223                                         continue;
1224                                 }
1225                         } else if (!VALID_STAT(sbuf) && SMB_VFS_STAT(conn,pathreal,&sbuf) != 0) {
1226                                 pstring link_target;
1227
1228                                 /* Needed to show the msdfs symlinks as 
1229                                  * directories */
1230
1231                                 if(lp_host_msdfs() && 
1232                                    lp_msdfs_root(SNUM(conn)) &&
1233                                    ((ms_dfs_link = is_msdfs_link(conn, pathreal, link_target, &sbuf)) == True)) {
1234                                         DEBUG(5,("get_lanman2_dir_entry: Masquerading msdfs link %s "
1235                                                 "as a directory\n",
1236                                                 pathreal));
1237                                         sbuf.st_mode = (sbuf.st_mode & 0xFFF) | S_IFDIR;
1238
1239                                 } else {
1240
1241                                         DEBUG(5,("get_lanman2_dir_entry:Couldn't stat [%s] (%s)\n",
1242                                                 pathreal,strerror(errno)));
1243                                         continue;
1244                                 }
1245                         }
1246
1247                         if (ms_dfs_link) {
1248                                 mode = dos_mode_msdfs(conn,pathreal,&sbuf);
1249                         } else {
1250                                 mode = dos_mode(conn,pathreal,&sbuf);
1251                         }
1252
1253                         if (!dir_check_ftype(conn,mode,dirtype)) {
1254                                 DEBUG(5,("get_lanman2_dir_entry: [%s] attribs didn't match %x\n",fname,dirtype));
1255                                 continue;
1256                         }
1257
1258                         if (!(mode & aDIR)) {
1259                                 file_size = get_file_size(sbuf);
1260                         }
1261                         allocation_size = get_allocation_size(conn,NULL,&sbuf);
1262
1263                         mdate_ts = get_mtimespec(&sbuf);
1264                         adate_ts = get_atimespec(&sbuf);
1265                         create_date_ts = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1266
1267                         if (lp_dos_filetime_resolution(SNUM(conn))) {
1268                                 dos_filetime_timespec(&create_date_ts);
1269                                 dos_filetime_timespec(&mdate_ts);
1270                                 dos_filetime_timespec(&adate_ts);
1271                         }
1272
1273                         create_date = convert_timespec_to_time_t(create_date_ts);
1274                         mdate = convert_timespec_to_time_t(mdate_ts);
1275                         adate = convert_timespec_to_time_t(adate_ts);
1276                         
1277                         DEBUG(5,("get_lanman2_dir_entry: found %s fname=%s\n",pathreal,fname));
1278           
1279                         found = True;
1280
1281                         dptr_DirCacheAdd(conn->dirptr, dname, curr_dirpos);
1282                 }
1283         }
1284
1285         p = pdata;
1286         last_entry_ptr = p;
1287
1288         nt_extmode = mode ? mode : FILE_ATTRIBUTE_NORMAL;
1289
1290         switch (info_level) {
1291                 case SMB_FIND_INFO_STANDARD:
1292                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_INFO_STANDARD\n"));
1293                         if(requires_resume_key) {
1294                                 SIVAL(p,0,reskey);
1295                                 p += 4;
1296                         }
1297                         srv_put_dos_date2(p,0,create_date);
1298                         srv_put_dos_date2(p,4,adate);
1299                         srv_put_dos_date2(p,8,mdate);
1300                         SIVAL(p,12,(uint32)file_size);
1301                         SIVAL(p,16,(uint32)allocation_size);
1302                         SSVAL(p,20,mode);
1303                         p += 23;
1304                         nameptr = p;
1305                         p += align_string(outbuf, p, 0);
1306                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE);
1307                         if (SVAL(outbuf, smb_flg2) & FLAGS2_UNICODE_STRINGS) {
1308                                 if (len > 2) {
1309                                         SCVAL(nameptr, -1, len - 2);
1310                                 } else {
1311                                         SCVAL(nameptr, -1, 0);
1312                                 }
1313                         } else {
1314                                 if (len > 1) {
1315                                         SCVAL(nameptr, -1, len - 1);
1316                                 } else {
1317                                         SCVAL(nameptr, -1, 0);
1318                                 }
1319                         }
1320                         p += len;
1321                         break;
1322
1323                 case SMB_FIND_EA_SIZE:
1324                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_SIZE\n"));
1325                         if(requires_resume_key) {
1326                                 SIVAL(p,0,reskey);
1327                                 p += 4;
1328                         }
1329                         srv_put_dos_date2(p,0,create_date);
1330                         srv_put_dos_date2(p,4,adate);
1331                         srv_put_dos_date2(p,8,mdate);
1332                         SIVAL(p,12,(uint32)file_size);
1333                         SIVAL(p,16,(uint32)allocation_size);
1334                         SSVAL(p,20,mode);
1335                         {
1336                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1337                                 SIVAL(p,22,ea_size); /* Extended attributes */
1338                         }
1339                         p += 27;
1340                         nameptr = p - 1;
1341                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE | STR_NOALIGN);
1342                         if (SVAL(outbuf, smb_flg2) & FLAGS2_UNICODE_STRINGS) {
1343                                 if (len > 2) {
1344                                         len -= 2;
1345                                 } else {
1346                                         len = 0;
1347                                 }
1348                         } else {
1349                                 if (len > 1) {
1350                                         len -= 1;
1351                                 } else {
1352                                         len = 0;
1353                                 }
1354                         }
1355                         SCVAL(nameptr,0,len);
1356                         p += len;
1357                         SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1358                         break;
1359
1360                 case SMB_FIND_EA_LIST:
1361                 {
1362                         struct ea_list *file_list = NULL;
1363                         size_t ea_len = 0;
1364
1365                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_LIST\n"));
1366                         if (!name_list) {
1367                                 return False;
1368                         }
1369                         if(requires_resume_key) {
1370                                 SIVAL(p,0,reskey);
1371                                 p += 4;
1372                         }
1373                         srv_put_dos_date2(p,0,create_date);
1374                         srv_put_dos_date2(p,4,adate);
1375                         srv_put_dos_date2(p,8,mdate);
1376                         SIVAL(p,12,(uint32)file_size);
1377                         SIVAL(p,16,(uint32)allocation_size);
1378                         SSVAL(p,20,mode);
1379                         p += 22; /* p now points to the EA area. */
1380
1381                         file_list = get_ea_list_from_file(ea_ctx, conn, NULL, pathreal, &ea_len);
1382                         name_list = ea_list_union(name_list, file_list, &ea_len);
1383
1384                         /* We need to determine if this entry will fit in the space available. */
1385                         /* Max string size is 255 bytes. */
1386                         if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
1387                                 /* Move the dirptr back to prev_dirpos */
1388                                 dptr_SeekDir(conn->dirptr, prev_dirpos);
1389                                 *out_of_space = True;
1390                                 DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
1391                                 return False; /* Not finished - just out of space */
1392                         }
1393
1394                         /* Push the ea_data followed by the name. */
1395                         p += fill_ea_buffer(ea_ctx, p, space_remaining, conn, name_list);
1396                         nameptr = p;
1397                         len = srvstr_push(outbuf, p + 1, fname, -1, STR_TERMINATE | STR_NOALIGN);
1398                         if (SVAL(outbuf, smb_flg2) & FLAGS2_UNICODE_STRINGS) {
1399                                 if (len > 2) {
1400                                         len -= 2;
1401                                 } else {
1402                                         len = 0;
1403                                 }
1404                         } else {
1405                                 if (len > 1) {
1406                                         len -= 1;
1407                                 } else {
1408                                         len = 0;
1409                                 }
1410                         }
1411                         SCVAL(nameptr,0,len);
1412                         p += len + 1;
1413                         SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
1414                         break;
1415                 }
1416
1417                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1418                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
1419                         was_8_3 = mangle_is_8_3(fname, True, conn->params);
1420                         p += 4;
1421                         SIVAL(p,0,reskey); p += 4;
1422                         put_long_date_timespec(p,create_date_ts); p += 8;
1423                         put_long_date_timespec(p,adate_ts); p += 8;
1424                         put_long_date_timespec(p,mdate_ts); p += 8;
1425                         put_long_date_timespec(p,mdate_ts); p += 8;
1426                         SOFF_T(p,0,file_size); p += 8;
1427                         SOFF_T(p,0,allocation_size); p += 8;
1428                         SIVAL(p,0,nt_extmode); p += 4;
1429                         q = p; p += 4; /* q is placeholder for name length. */
1430                         {
1431                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1432                                 SIVAL(p,0,ea_size); /* Extended attributes */
1433                                 p += 4;
1434                         }
1435                         /* Clear the short name buffer. This is
1436                          * IMPORTANT as not doing so will trigger
1437                          * a Win2k client bug. JRA.
1438                          */
1439                         if (!was_8_3 && check_mangled_names) {
1440                                 pstring mangled_name;
1441                                 pstrcpy(mangled_name, fname);
1442                                 mangle_map(mangled_name,True,True,
1443                                            conn->params);
1444                                 mangled_name[12] = 0;
1445                                 len = srvstr_push(outbuf, p+2, mangled_name, 24, STR_UPPER|STR_UNICODE);
1446                                 if (len < 24) {
1447                                         memset(p + 2 + len,'\0',24 - len);
1448                                 }
1449                                 SSVAL(p, 0, len);
1450                         } else {
1451                                 memset(p,'\0',26);
1452                         }
1453                         p += 2 + 24;
1454                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE_ASCII);
1455                         SIVAL(q,0,len);
1456                         p += len;
1457                         SIVAL(p,0,0); /* Ensure any padding is null. */
1458                         len = PTR_DIFF(p, pdata);
1459                         len = (len + 3) & ~3;
1460                         SIVAL(pdata,0,len);
1461                         p = pdata + len;
1462                         break;
1463
1464                 case SMB_FIND_FILE_DIRECTORY_INFO:
1465                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
1466                         p += 4;
1467                         SIVAL(p,0,reskey); p += 4;
1468                         put_long_date_timespec(p,create_date_ts); p += 8;
1469                         put_long_date_timespec(p,adate_ts); p += 8;
1470                         put_long_date_timespec(p,mdate_ts); p += 8;
1471                         put_long_date_timespec(p,mdate_ts); p += 8;
1472                         SOFF_T(p,0,file_size); p += 8;
1473                         SOFF_T(p,0,allocation_size); p += 8;
1474                         SIVAL(p,0,nt_extmode); p += 4;
1475                         len = srvstr_push(outbuf, p + 4, fname, -1, STR_TERMINATE_ASCII);
1476                         SIVAL(p,0,len);
1477                         p += 4 + len;
1478                         SIVAL(p,0,0); /* Ensure any padding is null. */
1479                         len = PTR_DIFF(p, pdata);
1480                         len = (len + 3) & ~3;
1481                         SIVAL(pdata,0,len);
1482                         p = pdata + len;
1483                         break;
1484       
1485                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1486                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
1487                         p += 4;
1488                         SIVAL(p,0,reskey); p += 4;
1489                         put_long_date_timespec(p,create_date_ts); p += 8;
1490                         put_long_date_timespec(p,adate_ts); p += 8;
1491                         put_long_date_timespec(p,mdate_ts); p += 8;
1492                         put_long_date_timespec(p,mdate_ts); p += 8;
1493                         SOFF_T(p,0,file_size); p += 8;
1494                         SOFF_T(p,0,allocation_size); p += 8;
1495                         SIVAL(p,0,nt_extmode); p += 4;
1496                         q = p; p += 4; /* q is placeholder for name length. */
1497                         {
1498                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1499                                 SIVAL(p,0,ea_size); /* Extended attributes */
1500                                 p +=4;
1501                         }
1502                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE_ASCII);
1503                         SIVAL(q, 0, len);
1504                         p += len;
1505
1506                         SIVAL(p,0,0); /* Ensure any padding is null. */
1507                         len = PTR_DIFF(p, pdata);
1508                         len = (len + 3) & ~3;
1509                         SIVAL(pdata,0,len);
1510                         p = pdata + len;
1511                         break;
1512
1513                 case SMB_FIND_FILE_NAMES_INFO:
1514                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
1515                         p += 4;
1516                         SIVAL(p,0,reskey); p += 4;
1517                         p += 4;
1518                         /* this must *not* be null terminated or w2k gets in a loop trying to set an
1519                            acl on a dir (tridge) */
1520                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE_ASCII);
1521                         SIVAL(p, -4, len);
1522                         p += len;
1523                         SIVAL(p,0,0); /* Ensure any padding is null. */
1524                         len = PTR_DIFF(p, pdata);
1525                         len = (len + 3) & ~3;
1526                         SIVAL(pdata,0,len);
1527                         p = pdata + len;
1528                         break;
1529
1530                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1531                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
1532                         p += 4;
1533                         SIVAL(p,0,reskey); p += 4;
1534                         put_long_date_timespec(p,create_date_ts); p += 8;
1535                         put_long_date_timespec(p,adate_ts); p += 8;
1536                         put_long_date_timespec(p,mdate_ts); p += 8;
1537                         put_long_date_timespec(p,mdate_ts); p += 8;
1538                         SOFF_T(p,0,file_size); p += 8;
1539                         SOFF_T(p,0,allocation_size); p += 8;
1540                         SIVAL(p,0,nt_extmode); p += 4;
1541                         q = p; p += 4; /* q is placeholder for name length. */
1542                         {
1543                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1544                                 SIVAL(p,0,ea_size); /* Extended attributes */
1545                                 p +=4;
1546                         }
1547                         SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
1548                         SIVAL(p,0,sbuf.st_ino); p += 4; /* FileIndexLow */
1549                         SIVAL(p,0,sbuf.st_dev); p += 4; /* FileIndexHigh */
1550                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE_ASCII);
1551                         SIVAL(q, 0, len);
1552                         p += len; 
1553                         SIVAL(p,0,0); /* Ensure any padding is null. */
1554                         len = PTR_DIFF(p, pdata);
1555                         len = (len + 3) & ~3;
1556                         SIVAL(pdata,0,len);
1557                         p = pdata + len;
1558                         break;
1559
1560                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1561                         DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
1562                         was_8_3 = mangle_is_8_3(fname, True, conn->params);
1563                         p += 4;
1564                         SIVAL(p,0,reskey); p += 4;
1565                         put_long_date_timespec(p,create_date_ts); p += 8;
1566                         put_long_date_timespec(p,adate_ts); p += 8;
1567                         put_long_date_timespec(p,mdate_ts); p += 8;
1568                         put_long_date_timespec(p,mdate_ts); p += 8;
1569                         SOFF_T(p,0,file_size); p += 8;
1570                         SOFF_T(p,0,allocation_size); p += 8;
1571                         SIVAL(p,0,nt_extmode); p += 4;
1572                         q = p; p += 4; /* q is placeholder for name length */
1573                         {
1574                                 unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
1575                                 SIVAL(p,0,ea_size); /* Extended attributes */
1576                                 p +=4;
1577                         }
1578                         /* Clear the short name buffer. This is
1579                          * IMPORTANT as not doing so will trigger
1580                          * a Win2k client bug. JRA.
1581                          */
1582                         if (!was_8_3 && check_mangled_names) {
1583                                 pstring mangled_name;
1584                                 pstrcpy(mangled_name, fname);
1585                                 mangle_map(mangled_name,True,True,
1586                                            conn->params);
1587                                 mangled_name[12] = 0;
1588                                 len = srvstr_push(outbuf, p+2, mangled_name, 24, STR_UPPER|STR_UNICODE);
1589                                 SSVAL(p, 0, len);
1590                                 if (len < 24) {
1591                                         memset(p + 2 + len,'\0',24 - len);
1592                                 }
1593                                 SSVAL(p, 0, len);
1594                         } else {
1595                                 memset(p,'\0',26);
1596                         }
1597                         p += 26;
1598                         SSVAL(p,0,0); p += 2; /* Reserved ? */
1599                         SIVAL(p,0,sbuf.st_ino); p += 4; /* FileIndexLow */
1600                         SIVAL(p,0,sbuf.st_dev); p += 4; /* FileIndexHigh */
1601                         len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE_ASCII);
1602                         SIVAL(q,0,len);
1603                         p += len;
1604                         SIVAL(p,0,0); /* Ensure any padding is null. */
1605                         len = PTR_DIFF(p, pdata);
1606                         len = (len + 3) & ~3;
1607                         SIVAL(pdata,0,len);
1608                         p = pdata + len;
1609                         break;
1610
1611                 /* CIFS UNIX Extension. */
1612
1613                 case SMB_FIND_FILE_UNIX:
1614                 case SMB_FIND_FILE_UNIX_INFO2:
1615                         p+= 4;
1616                         SIVAL(p,0,reskey); p+= 4;    /* Used for continuing search. */
1617
1618                         /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
1619
1620                         if (info_level == SMB_FIND_FILE_UNIX) {
1621                                 DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX\n"));
1622                                 p = store_file_unix_basic(conn, p,
1623                                                         NULL, &sbuf);
1624                                 len = srvstr_push(outbuf, p, fname, -1, STR_TERMINATE);
1625                         } else {
1626                                 DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
1627                                 p = store_file_unix_basic_info2(conn, p,
1628                                                         NULL, &sbuf);
1629                                 nameptr = p;
1630                                 p += 4;
1631                                 len = srvstr_push(outbuf, p, fname, -1, 0);
1632                                 SIVAL(nameptr, 0, len);
1633                         }
1634
1635                         p += len;
1636                         SIVAL(p,0,0); /* Ensure any padding is null. */
1637
1638                         len = PTR_DIFF(p, pdata);
1639                         len = (len + 3) & ~3;
1640                         SIVAL(pdata,0,len);     /* Offset from this structure to the beginning of the next one */
1641                         p = pdata + len;
1642                         /* End of SMB_QUERY_FILE_UNIX_BASIC */
1643
1644                         break;
1645
1646                 default:      
1647                         return(False);
1648         }
1649
1650
1651         if (PTR_DIFF(p,pdata) > space_remaining) {
1652                 /* Move the dirptr back to prev_dirpos */
1653                 dptr_SeekDir(conn->dirptr, prev_dirpos);
1654                 *out_of_space = True;
1655                 DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
1656                 return False; /* Not finished - just out of space */
1657         }
1658
1659         /* Setup the last entry pointer, as an offset from base_data */
1660         *last_entry_off = PTR_DIFF(last_entry_ptr,base_data);
1661         /* Advance the data pointer to the next slot */
1662         *ppdata = p;
1663
1664         return(found);
1665 }
1666
1667 /****************************************************************************
1668  Reply to a TRANS2_FINDFIRST.
1669 ****************************************************************************/
1670
1671 static int call_trans2findfirst(connection_struct *conn, char *inbuf, char *outbuf, int bufsize,  
1672                                 char **pparams, int total_params, char **ppdata, int total_data,
1673                                 unsigned int max_data_bytes)
1674 {
1675         /* We must be careful here that we don't return more than the
1676                 allowed number of data bytes. If this means returning fewer than
1677                 maxentries then so be it. We assume that the redirector has
1678                 enough room for the fixed number of parameter bytes it has
1679                 requested. */
1680         char *params = *pparams;
1681         char *pdata = *ppdata;
1682         uint32 dirtype;
1683         int maxentries;
1684         uint16 findfirst_flags;
1685         BOOL close_after_first;
1686         BOOL close_if_end;
1687         BOOL requires_resume_key;
1688         int info_level;
1689         pstring directory;
1690         pstring mask;
1691         char *p;
1692         int last_entry_off=0;
1693         int dptr_num = -1;
1694         int numentries = 0;
1695         int i;
1696         BOOL finished = False;
1697         BOOL dont_descend = False;
1698         BOOL out_of_space = False;
1699         int space_remaining;
1700         BOOL mask_contains_wcard = False;
1701         SMB_STRUCT_STAT sbuf;
1702         TALLOC_CTX *ea_ctx = NULL;
1703         struct ea_list *ea_list = NULL;
1704         NTSTATUS ntstatus = NT_STATUS_OK;
1705
1706         if (total_params < 13) {
1707                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1708         }
1709
1710         dirtype = SVAL(params,0);
1711         maxentries = SVAL(params,2);
1712         findfirst_flags = SVAL(params,4);
1713         close_after_first = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE);
1714         close_if_end = (findfirst_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
1715         requires_resume_key = (findfirst_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
1716         info_level = SVAL(params,6);
1717
1718         *directory = *mask = 0;
1719
1720         DEBUG(3,("call_trans2findfirst: dirtype = %x, maxentries = %d, close_after_first=%d, \
1721 close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
1722                 (unsigned int)dirtype, maxentries, close_after_first, close_if_end, requires_resume_key,
1723                 info_level, max_data_bytes));
1724
1725         if (!maxentries) {
1726                 /* W2K3 seems to treat zero as 1. */
1727                 maxentries = 1;
1728         }
1729  
1730         switch (info_level) {
1731                 case SMB_FIND_INFO_STANDARD:
1732                 case SMB_FIND_EA_SIZE:
1733                 case SMB_FIND_EA_LIST:
1734                 case SMB_FIND_FILE_DIRECTORY_INFO:
1735                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1736                 case SMB_FIND_FILE_NAMES_INFO:
1737                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
1738                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
1739                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
1740                         break;
1741                 case SMB_FIND_FILE_UNIX:
1742                 case SMB_FIND_FILE_UNIX_INFO2:
1743                         if (!lp_unix_extensions()) {
1744                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
1745                         }
1746                         break;
1747                 default:
1748                         return ERROR_NT(NT_STATUS_INVALID_LEVEL);
1749         }
1750
1751         srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), directory,
1752                               params+12, sizeof(directory), total_params - 12,
1753                               STR_TERMINATE, &ntstatus, &mask_contains_wcard);
1754         if (!NT_STATUS_IS_OK(ntstatus)) {
1755                 return ERROR_NT(ntstatus);
1756         }
1757
1758         ntstatus = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, directory, &mask_contains_wcard);
1759         if (!NT_STATUS_IS_OK(ntstatus)) {
1760                 if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
1761                         return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1762                 }
1763                 return ERROR_NT(ntstatus);
1764         }
1765
1766         ntstatus = unix_convert(conn, directory, True, NULL, &sbuf);
1767         if (!NT_STATUS_IS_OK(ntstatus)) {
1768                 return ERROR_NT(ntstatus);
1769         }
1770         ntstatus = check_name(conn, directory);
1771         if (!NT_STATUS_IS_OK(ntstatus)) {
1772                 return ERROR_NT(ntstatus);
1773         }
1774
1775         p = strrchr_m(directory,'/');
1776         if(p == NULL) {
1777                 /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
1778                 if((directory[0] == '.') && (directory[1] == '\0')) {
1779                         pstrcpy(mask,"*");
1780                         mask_contains_wcard = True;
1781                 } else {
1782                         pstrcpy(mask,directory);
1783                 }
1784                 pstrcpy(directory,"./");
1785         } else {
1786                 pstrcpy(mask,p+1);
1787                 *p = 0;
1788         }
1789
1790         DEBUG(5,("dir=%s, mask = %s\n",directory, mask));
1791
1792         if (info_level == SMB_FIND_EA_LIST) {
1793                 uint32 ea_size;
1794
1795                 if (total_data < 4) {
1796                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1797                 }
1798
1799                 ea_size = IVAL(pdata,0);
1800                 if (ea_size != total_data) {
1801                         DEBUG(4,("call_trans2findfirst: Rejecting EA request with incorrect \
1802 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
1803                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1804                 }
1805
1806                 if (!lp_ea_support(SNUM(conn))) {
1807                         return ERROR_DOS(ERRDOS,ERReasnotsupported);
1808                 }
1809                                                                                                                                                         
1810                 if ((ea_ctx = talloc_init("findnext_ea_list")) == NULL) {
1811                         return ERROR_NT(NT_STATUS_NO_MEMORY);
1812                 }
1813
1814                 /* Pull out the list of names. */
1815                 ea_list = read_ea_name_list(ea_ctx, pdata + 4, ea_size - 4);
1816                 if (!ea_list) {
1817                         talloc_destroy(ea_ctx);
1818                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1819                 }
1820         }
1821
1822         *ppdata = (char *)SMB_REALLOC(
1823                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
1824         if(*ppdata == NULL ) {
1825                 talloc_destroy(ea_ctx);
1826                 return ERROR_NT(NT_STATUS_NO_MEMORY);
1827         }
1828         pdata = *ppdata;
1829
1830         /* Realloc the params space */
1831         *pparams = (char *)SMB_REALLOC(*pparams, 10);
1832         if (*pparams == NULL) {
1833                 talloc_destroy(ea_ctx);
1834                 return ERROR_NT(NT_STATUS_NO_MEMORY);
1835         }
1836         params = *pparams;
1837
1838         /* Save the wildcard match and attribs we are using on this directory - 
1839                 needed as lanman2 assumes these are being saved between calls */
1840
1841         ntstatus = dptr_create(conn,
1842                                 directory,
1843                                 False,
1844                                 True,
1845                                 SVAL(inbuf,smb_pid),
1846                                 mask,
1847                                 mask_contains_wcard,
1848                                 dirtype,
1849                                 &conn->dirptr);
1850
1851         if (!NT_STATUS_IS_OK(ntstatus)) {
1852                 talloc_destroy(ea_ctx);
1853                 return ERROR_NT(ntstatus);
1854         }
1855
1856         dptr_num = dptr_dnum(conn->dirptr);
1857         DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
1858
1859         /* We don't need to check for VOL here as this is returned by 
1860                 a different TRANS2 call. */
1861   
1862         DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n", conn->dirpath,lp_dontdescend(SNUM(conn))));
1863         if (in_list(conn->dirpath,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
1864                 dont_descend = True;
1865     
1866         p = pdata;
1867         space_remaining = max_data_bytes;
1868         out_of_space = False;
1869
1870         for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
1871                 BOOL got_exact_match = False;
1872
1873                 /* this is a heuristic to avoid seeking the dirptr except when 
1874                         absolutely necessary. It allows for a filename of about 40 chars */
1875                 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
1876                         out_of_space = True;
1877                         finished = False;
1878                 } else {
1879                         finished = !get_lanman2_dir_entry(conn,
1880                                         inbuf, outbuf,
1881                                         mask,dirtype,info_level,
1882                                         requires_resume_key,dont_descend,
1883                                         &p,pdata,space_remaining, &out_of_space, &got_exact_match,
1884                                         &last_entry_off, ea_list, ea_ctx);
1885                 }
1886
1887                 if (finished && out_of_space)
1888                         finished = False;
1889
1890                 if (!finished && !out_of_space)
1891                         numentries++;
1892
1893                 /*
1894                  * As an optimisation if we know we aren't looking
1895                  * for a wildcard name (ie. the name matches the wildcard exactly)
1896                  * then we can finish on any (first) match.
1897                  * This speeds up large directory searches. JRA.
1898                  */
1899
1900                 if(got_exact_match)
1901                         finished = True;
1902
1903                 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
1904         }
1905   
1906         talloc_destroy(ea_ctx);
1907
1908         /* Check if we can close the dirptr */
1909         if(close_after_first || (finished && close_if_end)) {
1910                 DEBUG(5,("call_trans2findfirst - (2) closing dptr_num %d\n", dptr_num));
1911                 dptr_close(&dptr_num);
1912         }
1913
1914         /* 
1915          * If there are no matching entries we must return ERRDOS/ERRbadfile - 
1916          * from observation of NT. NB. This changes to ERRDOS,ERRnofiles if
1917          * the protocol level is less than NT1. Tested with smbclient. JRA.
1918          * This should fix the OS/2 client bug #2335.
1919          */
1920
1921         if(numentries == 0) {
1922                 dptr_close(&dptr_num);
1923                 if (Protocol < PROTOCOL_NT1) {
1924                         return ERROR_DOS(ERRDOS,ERRnofiles);
1925                 } else {
1926                         return ERROR_BOTH(NT_STATUS_NO_SUCH_FILE,ERRDOS,ERRbadfile);
1927                 }
1928         }
1929
1930         /* At this point pdata points to numentries directory entries. */
1931
1932         /* Set up the return parameter block */
1933         SSVAL(params,0,dptr_num);
1934         SSVAL(params,2,numentries);
1935         SSVAL(params,4,finished);
1936         SSVAL(params,6,0); /* Never an EA error */
1937         SSVAL(params,8,last_entry_off);
1938
1939         send_trans2_replies(inbuf, outbuf, bufsize, params, 10, pdata, PTR_DIFF(p,pdata), max_data_bytes);
1940
1941         if ((! *directory) && dptr_path(dptr_num))
1942                 slprintf(directory,sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
1943
1944         DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
1945                 smb_fn_name(CVAL(inbuf,smb_com)), 
1946                 mask, directory, dirtype, numentries ) );
1947
1948         /* 
1949          * Force a name mangle here to ensure that the
1950          * mask as an 8.3 name is top of the mangled cache.
1951          * The reasons for this are subtle. Don't remove
1952          * this code unless you know what you are doing
1953          * (see PR#13758). JRA.
1954          */
1955
1956         if(!mangle_is_8_3_wildcards( mask, False, conn->params))
1957                 mangle_map(mask, True, True, conn->params);
1958
1959         return(-1);
1960 }
1961
1962 /****************************************************************************
1963  Reply to a TRANS2_FINDNEXT.
1964 ****************************************************************************/
1965
1966 static int call_trans2findnext(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1967                                         char **pparams, int total_params, char **ppdata, int total_data,
1968                                         unsigned int max_data_bytes)
1969 {
1970         /* We must be careful here that we don't return more than the
1971                 allowed number of data bytes. If this means returning fewer than
1972                 maxentries then so be it. We assume that the redirector has
1973                 enough room for the fixed number of parameter bytes it has
1974                 requested. */
1975         char *params = *pparams;
1976         char *pdata = *ppdata;
1977         int dptr_num;
1978         int maxentries;
1979         uint16 info_level;
1980         uint32 resume_key;
1981         uint16 findnext_flags;
1982         BOOL close_after_request;
1983         BOOL close_if_end;
1984         BOOL requires_resume_key;
1985         BOOL continue_bit;
1986         BOOL mask_contains_wcard = False;
1987         pstring resume_name;
1988         pstring mask;
1989         pstring directory;
1990         char *p;
1991         uint16 dirtype;
1992         int numentries = 0;
1993         int i, last_entry_off=0;
1994         BOOL finished = False;
1995         BOOL dont_descend = False;
1996         BOOL out_of_space = False;
1997         int space_remaining;
1998         TALLOC_CTX *ea_ctx = NULL;
1999         struct ea_list *ea_list = NULL;
2000         NTSTATUS ntstatus = NT_STATUS_OK;
2001
2002         if (total_params < 13) {
2003                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2004         }
2005
2006         dptr_num = SVAL(params,0);
2007         maxentries = SVAL(params,2);
2008         info_level = SVAL(params,4);
2009         resume_key = IVAL(params,6);
2010         findnext_flags = SVAL(params,10);
2011         close_after_request = (findnext_flags & FLAG_TRANS2_FIND_CLOSE);
2012         close_if_end = (findnext_flags & FLAG_TRANS2_FIND_CLOSE_IF_END);
2013         requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
2014         continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
2015
2016         *mask = *directory = *resume_name = 0;
2017
2018         srvstr_get_path_wcard(inbuf, SVAL(inbuf,smb_flg2), resume_name,
2019                               params+12, sizeof(resume_name),
2020                               total_params - 12, STR_TERMINATE, &ntstatus,
2021                               &mask_contains_wcard);
2022         if (!NT_STATUS_IS_OK(ntstatus)) {
2023                 /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
2024                    complain (it thinks we're asking for the directory above the shared
2025                    path or an invalid name). Catch this as the resume name is only compared, never used in
2026                    a file access. JRA. */
2027                 if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_OBJECT_PATH_SYNTAX_BAD)) {
2028                         pstrcpy(resume_name, "..");
2029                 } else if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_OBJECT_NAME_INVALID)) {
2030                         pstrcpy(resume_name, ".");
2031                 } else {
2032                         return ERROR_NT(ntstatus);
2033                 }
2034         }
2035
2036         DEBUG(3,("call_trans2findnext: dirhandle = %d, max_data_bytes = %d, maxentries = %d, \
2037 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
2038 resume_key = %d resume name = %s continue=%d level = %d\n",
2039                 dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end, 
2040                 requires_resume_key, resume_key, resume_name, continue_bit, info_level));
2041
2042         if (!maxentries) {
2043                 /* W2K3 seems to treat zero as 1. */
2044                 maxentries = 1;
2045         }
2046
2047         switch (info_level) {
2048                 case SMB_FIND_INFO_STANDARD:
2049                 case SMB_FIND_EA_SIZE:
2050                 case SMB_FIND_EA_LIST:
2051                 case SMB_FIND_FILE_DIRECTORY_INFO:
2052                 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
2053                 case SMB_FIND_FILE_NAMES_INFO:
2054                 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
2055                 case SMB_FIND_ID_FULL_DIRECTORY_INFO:
2056                 case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
2057                         break;
2058                 case SMB_FIND_FILE_UNIX:
2059                 case SMB_FIND_FILE_UNIX_INFO2:
2060                         if (!lp_unix_extensions()) {
2061                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2062                         }
2063                         break;
2064                 default:
2065                         return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2066         }
2067
2068         if (info_level == SMB_FIND_EA_LIST) {
2069                 uint32 ea_size;
2070
2071                 if (total_data < 4) {
2072                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2073                 }
2074
2075                 ea_size = IVAL(pdata,0);
2076                 if (ea_size != total_data) {
2077                         DEBUG(4,("call_trans2findnext: Rejecting EA request with incorrect \
2078 total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pdata,0) ));
2079                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2080                 }
2081                                                                                                                                                      
2082                 if (!lp_ea_support(SNUM(conn))) {
2083                         return ERROR_DOS(ERRDOS,ERReasnotsupported);
2084                 }
2085                                                                                                                                                      
2086                 if ((ea_ctx = talloc_init("findnext_ea_list")) == NULL) {
2087                         return ERROR_NT(NT_STATUS_NO_MEMORY);
2088                 }
2089
2090                 /* Pull out the list of names. */
2091                 ea_list = read_ea_name_list(ea_ctx, pdata + 4, ea_size - 4);
2092                 if (!ea_list) {
2093                         talloc_destroy(ea_ctx);
2094                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2095                 }
2096         }
2097
2098         *ppdata = (char *)SMB_REALLOC(
2099                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2100         if(*ppdata == NULL) {
2101                 talloc_destroy(ea_ctx);
2102                 return ERROR_NT(NT_STATUS_NO_MEMORY);
2103         }
2104
2105         pdata = *ppdata;
2106
2107         /* Realloc the params space */
2108         *pparams = (char *)SMB_REALLOC(*pparams, 6*SIZEOFWORD);
2109         if(*pparams == NULL ) {
2110                 talloc_destroy(ea_ctx);
2111                 return ERROR_NT(NT_STATUS_NO_MEMORY);
2112         }
2113
2114         params = *pparams;
2115
2116         /* Check that the dptr is valid */
2117         if(!(conn->dirptr = dptr_fetch_lanman2(dptr_num))) {
2118                 talloc_destroy(ea_ctx);
2119                 return ERROR_DOS(ERRDOS,ERRnofiles);
2120         }
2121
2122         string_set(&conn->dirpath,dptr_path(dptr_num));
2123
2124         /* Get the wildcard mask from the dptr */
2125         if((p = dptr_wcard(dptr_num))== NULL) {
2126                 DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
2127                 talloc_destroy(ea_ctx);
2128                 return ERROR_DOS(ERRDOS,ERRnofiles);
2129         }
2130
2131         pstrcpy(mask, p);
2132         pstrcpy(directory,conn->dirpath);
2133
2134         /* Get the attr mask from the dptr */
2135         dirtype = dptr_attr(dptr_num);
2136
2137         DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld)\n",
2138                 dptr_num, mask, dirtype, 
2139                 (long)conn->dirptr,
2140                 dptr_TellDir(conn->dirptr)));
2141
2142         /* We don't need to check for VOL here as this is returned by 
2143                 a different TRANS2 call. */
2144
2145         DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",conn->dirpath,lp_dontdescend(SNUM(conn))));
2146         if (in_list(conn->dirpath,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
2147                 dont_descend = True;
2148     
2149         p = pdata;
2150         space_remaining = max_data_bytes;
2151         out_of_space = False;
2152
2153         /* 
2154          * Seek to the correct position. We no longer use the resume key but
2155          * depend on the last file name instead.
2156          */
2157
2158         if(*resume_name && !continue_bit) {
2159                 SMB_STRUCT_STAT st;
2160
2161                 long current_pos = 0;
2162                 /*
2163                  * Remember, mangle_map is called by
2164                  * get_lanman2_dir_entry(), so the resume name
2165                  * could be mangled. Ensure we check the unmangled name.
2166                  */
2167
2168                 if (mangle_is_mangled(resume_name, conn->params)) {
2169                         mangle_check_cache(resume_name, sizeof(resume_name)-1,
2170                                            conn->params);
2171                 }
2172
2173                 /*
2174                  * Fix for NT redirector problem triggered by resume key indexes
2175                  * changing between directory scans. We now return a resume key of 0
2176                  * and instead look for the filename to continue from (also given
2177                  * to us by NT/95/smbfs/smbclient). If no other scans have been done between the
2178                  * findfirst/findnext (as is usual) then the directory pointer
2179                  * should already be at the correct place.
2180                  */
2181
2182                 finished = !dptr_SearchDir(conn->dirptr, resume_name, &current_pos, &st);
2183         } /* end if resume_name && !continue_bit */
2184
2185         for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
2186                 BOOL got_exact_match = False;
2187
2188                 /* this is a heuristic to avoid seeking the dirptr except when 
2189                         absolutely necessary. It allows for a filename of about 40 chars */
2190                 if (space_remaining < DIRLEN_GUESS && numentries > 0) {
2191                         out_of_space = True;
2192                         finished = False;
2193                 } else {
2194                         finished = !get_lanman2_dir_entry(conn,
2195                                                 inbuf, outbuf,
2196                                                 mask,dirtype,info_level,
2197                                                 requires_resume_key,dont_descend,
2198                                                 &p,pdata,space_remaining, &out_of_space, &got_exact_match,
2199                                                 &last_entry_off, ea_list, ea_ctx);
2200                 }
2201
2202                 if (finished && out_of_space)
2203                         finished = False;
2204
2205                 if (!finished && !out_of_space)
2206                         numentries++;
2207
2208                 /*
2209                  * As an optimisation if we know we aren't looking
2210                  * for a wildcard name (ie. the name matches the wildcard exactly)
2211                  * then we can finish on any (first) match.
2212                  * This speeds up large directory searches. JRA.
2213                  */
2214
2215                 if(got_exact_match)
2216                         finished = True;
2217
2218                 space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
2219         }
2220   
2221         talloc_destroy(ea_ctx);
2222
2223         /* Check if we can close the dirptr */
2224         if(close_after_request || (finished && close_if_end)) {
2225                 DEBUG(5,("call_trans2findnext: closing dptr_num = %d\n", dptr_num));
2226                 dptr_close(&dptr_num); /* This frees up the saved mask */
2227         }
2228
2229         /* Set up the return parameter block */
2230         SSVAL(params,0,numentries);
2231         SSVAL(params,2,finished);
2232         SSVAL(params,4,0); /* Never an EA error */
2233         SSVAL(params,6,last_entry_off);
2234
2235         send_trans2_replies(inbuf, outbuf, bufsize, params, 8, pdata, PTR_DIFF(p,pdata), max_data_bytes);
2236
2237         if ((! *directory) && dptr_path(dptr_num))
2238                 slprintf(directory,sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
2239
2240         DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
2241                 smb_fn_name(CVAL(inbuf,smb_com)), 
2242                 mask, directory, dirtype, numentries ) );
2243
2244         return(-1);
2245 }
2246
2247 unsigned char *create_volume_objectid(connection_struct *conn, unsigned char objid[16])
2248 {
2249         E_md4hash(lp_servicename(SNUM(conn)),objid);
2250         return objid;
2251 }
2252
2253 /****************************************************************************
2254  Reply to a TRANS2_QFSINFO (query filesystem info).
2255 ****************************************************************************/
2256
2257 static int call_trans2qfsinfo(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2258                                         char **pparams, int total_params, char **ppdata, int total_data,
2259                                         unsigned int max_data_bytes)
2260 {
2261         char *pdata;
2262         char *params = *pparams;
2263         uint16 info_level;
2264         int data_len, len;
2265         SMB_STRUCT_STAT st;
2266         const char *vname = volume_label(SNUM(conn));
2267         int snum = SNUM(conn);
2268         char *fstype = lp_fstype(SNUM(conn));
2269         int quota_flag = 0;
2270
2271         if (total_params < 2) {
2272                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2273         }
2274
2275         info_level = SVAL(params,0);
2276
2277         DEBUG(3,("call_trans2qfsinfo: level = %d\n", info_level));
2278
2279         if(SMB_VFS_STAT(conn,".",&st)!=0) {
2280                 DEBUG(2,("call_trans2qfsinfo: stat of . failed (%s)\n", strerror(errno)));
2281                 return ERROR_DOS(ERRSRV,ERRinvdevice);
2282         }
2283
2284         *ppdata = (char *)SMB_REALLOC(
2285                 *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2286         if (*ppdata == NULL ) {
2287                 return ERROR_NT(NT_STATUS_NO_MEMORY);
2288         }
2289
2290         pdata = *ppdata;
2291         memset((char *)pdata,'\0',max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
2292
2293         switch (info_level) {
2294                 case SMB_INFO_ALLOCATION:
2295                 {
2296                         SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2297                         data_len = 18;
2298                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
2299                                 return(UNIXERROR(ERRHRD,ERRgeneral));
2300                         }
2301
2302                         block_size = lp_block_size(snum);
2303                         if (bsize < block_size) {
2304                                 SMB_BIG_UINT factor = block_size/bsize;
2305                                 bsize = block_size;
2306                                 dsize /= factor;
2307                                 dfree /= factor;
2308                         }
2309                         if (bsize > block_size) {
2310                                 SMB_BIG_UINT factor = bsize/block_size;
2311                                 bsize = block_size;
2312                                 dsize *= factor;
2313                                 dfree *= factor;
2314                         }
2315                         bytes_per_sector = 512;
2316                         sectors_per_unit = bsize/bytes_per_sector;
2317
2318                         DEBUG(5,("call_trans2qfsinfo : SMB_INFO_ALLOCATION id=%x, bsize=%u, cSectorUnit=%u, \
2319 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_dev, (unsigned int)bsize, (unsigned int)sectors_per_unit,
2320                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2321
2322                         SIVAL(pdata,l1_idFileSystem,st.st_dev);
2323                         SIVAL(pdata,l1_cSectorUnit,sectors_per_unit);
2324                         SIVAL(pdata,l1_cUnit,dsize);
2325                         SIVAL(pdata,l1_cUnitAvail,dfree);
2326                         SSVAL(pdata,l1_cbSector,bytes_per_sector);
2327                         break;
2328                 }
2329
2330                 case SMB_INFO_VOLUME:
2331                         /* Return volume name */
2332                         /* 
2333                          * Add volume serial number - hash of a combination of
2334                          * the called hostname and the service name.
2335                          */
2336                         SIVAL(pdata,0,str_checksum(lp_servicename(snum)) ^ (str_checksum(get_local_machine_name())<<16) );
2337                         /*
2338                          * Win2k3 and previous mess this up by sending a name length
2339                          * one byte short. I believe only older clients (OS/2 Win9x) use
2340                          * this call so try fixing this by adding a terminating null to
2341                          * the pushed string. The change here was adding the STR_TERMINATE. JRA.
2342                          */
2343                         len = srvstr_push(outbuf, pdata+l2_vol_szVolLabel, vname, -1, STR_NOALIGN|STR_TERMINATE);
2344                         SCVAL(pdata,l2_vol_cch,len);
2345                         data_len = l2_vol_szVolLabel + len;
2346                         DEBUG(5,("call_trans2qfsinfo : time = %x, namelen = %d, name = %s\n",
2347                                 (unsigned)st.st_ctime, len, vname));
2348                         break;
2349
2350                 case SMB_QUERY_FS_ATTRIBUTE_INFO:
2351                 case SMB_FS_ATTRIBUTE_INFORMATION:
2352
2353
2354 #if defined(HAVE_SYS_QUOTAS)
2355                         quota_flag = FILE_VOLUME_QUOTAS;
2356 #endif
2357
2358                         SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
2359                                 (lp_nt_acl_support(SNUM(conn)) ? FILE_PERSISTENT_ACLS : 0)|
2360                                 FILE_SUPPORTS_OBJECT_IDS|
2361                                 FILE_UNICODE_ON_DISK|
2362                                 quota_flag); /* FS ATTRIBUTES */
2363
2364                         SIVAL(pdata,4,255); /* Max filename component length */
2365                         /* NOTE! the fstype must *not* be null terminated or win98 won't recognise it
2366                                 and will think we can't do long filenames */
2367                         len = srvstr_push(outbuf, pdata+12, fstype, -1, STR_UNICODE);
2368                         SIVAL(pdata,8,len);
2369                         data_len = 12 + len;
2370                         break;
2371
2372                 case SMB_QUERY_FS_LABEL_INFO:
2373                 case SMB_FS_LABEL_INFORMATION:
2374                         len = srvstr_push(outbuf, pdata+4, vname, -1, 0);
2375                         data_len = 4 + len;
2376                         SIVAL(pdata,0,len);
2377                         break;
2378
2379                 case SMB_QUERY_FS_VOLUME_INFO:      
2380                 case SMB_FS_VOLUME_INFORMATION:
2381
2382                         /* 
2383                          * Add volume serial number - hash of a combination of
2384                          * the called hostname and the service name.
2385                          */
2386                         SIVAL(pdata,8,str_checksum(lp_servicename(snum)) ^ 
2387                                 (str_checksum(get_local_machine_name())<<16));
2388
2389                         /* Max label len is 32 characters. */
2390                         len = srvstr_push(outbuf, pdata+18, vname, -1, STR_UNICODE);
2391                         SIVAL(pdata,12,len);
2392                         data_len = 18+len;
2393
2394                         DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_VOLUME_INFO namelen = %d, vol=%s serv=%s\n", 
2395                                 (int)strlen(vname),vname, lp_servicename(snum)));
2396                         break;
2397
2398                 case SMB_QUERY_FS_SIZE_INFO:
2399                 case SMB_FS_SIZE_INFORMATION:
2400                 {
2401                         SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2402                         data_len = 24;
2403                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
2404                                 return(UNIXERROR(ERRHRD,ERRgeneral));
2405                         }
2406                         block_size = lp_block_size(snum);
2407                         if (bsize < block_size) {
2408                                 SMB_BIG_UINT factor = block_size/bsize;
2409                                 bsize = block_size;
2410                                 dsize /= factor;
2411                                 dfree /= factor;
2412                         }
2413                         if (bsize > block_size) {
2414                                 SMB_BIG_UINT factor = bsize/block_size;
2415                                 bsize = block_size;
2416                                 dsize *= factor;
2417                                 dfree *= factor;
2418                         }
2419                         bytes_per_sector = 512;
2420                         sectors_per_unit = bsize/bytes_per_sector;
2421                         DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_SIZE_INFO bsize=%u, cSectorUnit=%u, \
2422 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
2423                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2424                         SBIG_UINT(pdata,0,dsize);
2425                         SBIG_UINT(pdata,8,dfree);
2426                         SIVAL(pdata,16,sectors_per_unit);
2427                         SIVAL(pdata,20,bytes_per_sector);
2428                         break;
2429                 }
2430
2431                 case SMB_FS_FULL_SIZE_INFORMATION:
2432                 {
2433                         SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
2434                         data_len = 32;
2435                         if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
2436                                 return(UNIXERROR(ERRHRD,ERRgeneral));
2437                         }
2438                         block_size = lp_block_size(snum);
2439                         if (bsize < block_size) {
2440                                 SMB_BIG_UINT factor = block_size/bsize;
2441                                 bsize = block_size;
2442                                 dsize /= factor;
2443                                 dfree /= factor;
2444                         }
2445                         if (bsize > block_size) {
2446                                 SMB_BIG_UINT factor = bsize/block_size;
2447                                 bsize = block_size;
2448                                 dsize *= factor;
2449                                 dfree *= factor;
2450                         }
2451                         bytes_per_sector = 512;
2452                         sectors_per_unit = bsize/bytes_per_sector;
2453                         DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_FS_FULL_SIZE_INFO bsize=%u, cSectorUnit=%u, \
2454 cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned int)sectors_per_unit,
2455                                 (unsigned int)bytes_per_sector, (unsigned int)dsize, (unsigned int)dfree));
2456                         SBIG_UINT(pdata,0,dsize); /* Total Allocation units. */
2457                         SBIG_UINT(pdata,8,dfree); /* Caller available allocation units. */
2458                         SBIG_UINT(pdata,16,dfree); /* Actual available allocation units. */
2459                         SIVAL(pdata,24,sectors_per_unit); /* Sectors per allocation unit. */
2460                         SIVAL(pdata,28,bytes_per_sector); /* Bytes per sector. */
2461                         break;
2462                 }
2463
2464                 case SMB_QUERY_FS_DEVICE_INFO:
2465                 case SMB_FS_DEVICE_INFORMATION:
2466                         data_len = 8;
2467                         SIVAL(pdata,0,0); /* dev type */
2468                         SIVAL(pdata,4,0); /* characteristics */
2469                         break;
2470
2471 #ifdef HAVE_SYS_QUOTAS
2472                 case SMB_FS_QUOTA_INFORMATION:
2473                 /* 
2474                  * what we have to send --metze:
2475                  *
2476                  * Unknown1:            24 NULL bytes
2477                  * Soft Quota Treshold: 8 bytes seems like SMB_BIG_UINT or so
2478                  * Hard Quota Limit:    8 bytes seems like SMB_BIG_UINT or so
2479                  * Quota Flags:         2 byte :
2480                  * Unknown3:            6 NULL bytes
2481                  *
2482                  * 48 bytes total
2483                  * 
2484                  * details for Quota Flags:
2485                  * 
2486                  * 0x0020 Log Limit: log if the user exceeds his Hard Quota
2487                  * 0x0010 Log Warn:  log if the user exceeds his Soft Quota
2488                  * 0x0002 Deny Disk: deny disk access when the user exceeds his Hard Quota
2489                  * 0x0001 Enable Quotas: enable quota for this fs
2490                  *
2491                  */
2492                 {
2493                         /* we need to fake up a fsp here,
2494                          * because its not send in this call
2495                          */
2496                         files_struct fsp;
2497                         SMB_NTQUOTA_STRUCT quotas;
2498                         
2499                         ZERO_STRUCT(fsp);
2500                         ZERO_STRUCT(quotas);
2501                         
2502                         fsp.conn = conn;
2503                         fsp.fnum = -1;
2504                         
2505                         /* access check */
2506                         if (current_user.ut.uid != 0) {
2507                                 DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
2508                                         lp_servicename(SNUM(conn)),conn->user));
2509                                 return ERROR_DOS(ERRDOS,ERRnoaccess);
2510                         }
2511                         
2512                         if (vfs_get_ntquota(&fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
2513                                 DEBUG(0,("vfs_get_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
2514                                 return ERROR_DOS(ERRSRV,ERRerror);
2515                         }
2516
2517                         data_len = 48;
2518
2519                         DEBUG(10,("SMB_FS_QUOTA_INFORMATION: for service [%s]\n",lp_servicename(SNUM(conn))));          
2520                 
2521                         /* Unknown1 24 NULL bytes*/
2522                         SBIG_UINT(pdata,0,(SMB_BIG_UINT)0);
2523                         SBIG_UINT(pdata,8,(SMB_BIG_UINT)0);
2524                         SBIG_UINT(pdata,16,(SMB_BIG_UINT)0);
2525                 
2526                         /* Default Soft Quota 8 bytes */
2527                         SBIG_UINT(pdata,24,quotas.softlim);
2528
2529                         /* Default Hard Quota 8 bytes */
2530                         SBIG_UINT(pdata,32,quotas.hardlim);
2531         
2532                         /* Quota flag 2 bytes */
2533                         SSVAL(pdata,40,quotas.qflags);
2534                 
2535                         /* Unknown3 6 NULL bytes */
2536                         SSVAL(pdata,42,0);
2537                         SIVAL(pdata,44,0);
2538                         
2539                         break;
2540                 }
2541 #endif /* HAVE_SYS_QUOTAS */
2542                 case SMB_FS_OBJECTID_INFORMATION:
2543                 {
2544                         unsigned char objid[16];
2545                         memcpy(pdata,create_volume_objectid(conn, objid),16);
2546                         data_len = 64;
2547                         break;
2548                 }
2549
2550                 /*
2551                  * Query the version and capabilities of the CIFS UNIX extensions
2552                  * in use.
2553                  */
2554
2555                 case SMB_QUERY_CIFS_UNIX_INFO:
2556                         if (!lp_unix_extensions()) {
2557                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2558                         }
2559                         data_len = 12;
2560                         SSVAL(pdata,0,CIFS_UNIX_MAJOR_VERSION);
2561                         SSVAL(pdata,2,CIFS_UNIX_MINOR_VERSION);
2562                         /* We have POSIX ACLs, pathname and locking capability. */
2563                         SBIG_UINT(pdata,4,((SMB_BIG_UINT)(
2564                                         CIFS_UNIX_POSIX_ACLS_CAP|
2565                                         CIFS_UNIX_POSIX_PATHNAMES_CAP|
2566                                         CIFS_UNIX_FCNTL_LOCKS_CAP|
2567                                         CIFS_UNIX_EXTATTR_CAP|
2568                                         CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP|
2569                                         /* Ensure we don't do this on signed or sealed data. */
2570                                         (srv_is_signing_active() ? 0 : CIFS_UNIX_LARGE_READ_CAP)
2571                                         )));
2572                         break;
2573
2574                 case SMB_QUERY_POSIX_FS_INFO:
2575                 {
2576                         int rc;
2577                         vfs_statvfs_struct svfs;
2578
2579                         if (!lp_unix_extensions()) {
2580                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2581                         }
2582
2583                         rc = SMB_VFS_STATVFS(conn, ".", &svfs);
2584
2585                         if (!rc) {
2586                                 data_len = 56;
2587                                 SIVAL(pdata,0,svfs.OptimalTransferSize);
2588                                 SIVAL(pdata,4,svfs.BlockSize);
2589                                 SBIG_UINT(pdata,8,svfs.TotalBlocks);
2590                                 SBIG_UINT(pdata,16,svfs.BlocksAvail);
2591                                 SBIG_UINT(pdata,24,svfs.UserBlocksAvail);
2592                                 SBIG_UINT(pdata,32,svfs.TotalFileNodes);
2593                                 SBIG_UINT(pdata,40,svfs.FreeFileNodes);
2594                                 SBIG_UINT(pdata,48,svfs.FsIdentifier);
2595                                 DEBUG(5,("call_trans2qfsinfo : SMB_QUERY_POSIX_FS_INFO succsessful\n"));
2596 #ifdef EOPNOTSUPP
2597                         } else if (rc == EOPNOTSUPP) {
2598                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2599 #endif /* EOPNOTSUPP */
2600                         } else {
2601                                 DEBUG(0,("vfs_statvfs() failed for service [%s]\n",lp_servicename(SNUM(conn))));
2602                                 return ERROR_DOS(ERRSRV,ERRerror);
2603                         }
2604                         break;
2605                 }
2606
2607                 case SMB_QUERY_POSIX_WHOAMI:
2608                 {
2609                         uint32_t flags = 0;
2610                         uint32_t sid_bytes;
2611                         int i;
2612
2613                         if (!lp_unix_extensions()) {
2614                                 return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2615                         }
2616
2617                         if (max_data_bytes < 40) {
2618                                 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2619                         }
2620
2621                         /* We ARE guest if global_sid_Builtin_Guests is
2622                          * in our list of SIDs.
2623                          */
2624                         if (nt_token_check_sid(&global_sid_Builtin_Guests,
2625                                     current_user.nt_user_token)) {
2626                                 flags |= SMB_WHOAMI_GUEST;
2627                         }
2628
2629                         /* We are NOT guest if global_sid_Authenticated_Users
2630                          * is in our list of SIDs.
2631                          */
2632                         if (nt_token_check_sid(&global_sid_Authenticated_Users,
2633                                     current_user.nt_user_token)) {
2634                                 flags &= ~SMB_WHOAMI_GUEST;
2635                         }
2636
2637                         /* NOTE: 8 bytes for UID/GID, irrespective of native
2638                          * platform size. This matches
2639                          * SMB_QUERY_FILE_UNIX_BASIC and friends.
2640                          */
2641                         data_len = 4 /* flags */
2642                             + 4 /* flag mask */
2643                             + 8 /* uid */
2644                             + 8 /* gid */
2645                             + 4 /* ngroups */
2646                             + 4 /* num_sids */
2647                             + 4 /* SID bytes */
2648                             + 4 /* pad/reserved */
2649                             + (current_user.ut.ngroups * 8)
2650                                 /* groups list */
2651                             + (current_user.nt_user_token->num_sids *
2652                                     SID_MAX_SIZE)
2653                                 /* SID list */;
2654
2655                         SIVAL(pdata, 0, flags);
2656                         SIVAL(pdata, 4, SMB_WHOAMI_MASK);
2657                         SBIG_UINT(pdata, 8, (SMB_BIG_UINT)current_user.ut.uid);
2658                         SBIG_UINT(pdata, 16, (SMB_BIG_UINT)current_user.ut.gid);
2659
2660
2661                         if (data_len >= max_data_bytes) {
2662                                 /* Potential overflow, skip the GIDs and SIDs. */
2663
2664                                 SIVAL(pdata, 24, 0); /* num_groups */
2665                                 SIVAL(pdata, 28, 0); /* num_sids */
2666                                 SIVAL(pdata, 32, 0); /* num_sid_bytes */
2667                                 SIVAL(pdata, 36, 0); /* reserved */
2668
2669                                 data_len = 40;
2670                                 break;
2671                         }
2672
2673                         SIVAL(pdata, 24, current_user.ut.ngroups);
2674                         SIVAL(pdata, 28,
2675                                 current_user.nt_user_token->num_sids);
2676
2677                         /* We walk the SID list twice, but this call is fairly
2678                          * infrequent, and I don't expect that it's performance
2679                          * sensitive -- jpeach
2680                          */
2681                         for (i = 0, sid_bytes = 0;
2682                             i < current_user.nt_user_token->num_sids; ++i) {
2683                                 sid_bytes +=
2684                                     sid_size(&current_user.nt_user_token->user_sids[i]);
2685                         }
2686
2687                         /* SID list byte count */
2688                         SIVAL(pdata, 32, sid_bytes);
2689
2690                         /* 4 bytes pad/reserved - must be zero */
2691                         SIVAL(pdata, 36, 0);
2692                         data_len = 40;
2693
2694                         /* GID list */
2695                         for (i = 0; i < current_user.ut.ngroups; ++i) {
2696                                 SBIG_UINT(pdata, data_len,
2697                                         (SMB_BIG_UINT)current_user.ut.groups[i]);
2698                                 data_len += 8;
2699                         }
2700
2701                         /* SID list */
2702                         for (i = 0;
2703                             i < current_user.nt_user_token->num_sids; ++i) {
2704                                 int sid_len =
2705                                     sid_size(&current_user.nt_user_token->user_sids[i]);
2706
2707                                 sid_linearize(pdata + data_len, sid_len,
2708                                     &current_user.nt_user_token->user_sids[i]);
2709                                 data_len += sid_len;
2710                         }
2711
2712                         break;
2713                 }
2714
2715                 case SMB_MAC_QUERY_FS_INFO:
2716                         /*
2717                          * Thursby MAC extension... ONLY on NTFS filesystems
2718                          * once we do streams then we don't need this
2719                          */
2720                         if (strequal(lp_fstype(SNUM(conn)),"NTFS")) {
2721                                 data_len = 88;
2722                                 SIVAL(pdata,84,0x100); /* Don't support mac... */
2723                                 break;
2724                         }
2725                         /* drop through */
2726                 default:
2727                         return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2728         }
2729
2730
2731         send_trans2_replies(inbuf, outbuf, bufsize, params, 0, pdata, data_len, max_data_bytes);
2732
2733         DEBUG( 4, ( "%s info_level = %d\n", smb_fn_name(CVAL(inbuf,smb_com)), info_level) );
2734
2735         return -1;
2736 }
2737
2738 /****************************************************************************
2739  Reply to a TRANS2_SETFSINFO (set filesystem info).
2740 ****************************************************************************/
2741
2742 static int call_trans2setfsinfo(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2743                                         char **pparams, int total_params, char **ppdata, int total_data,
2744                                         unsigned int max_data_bytes)
2745 {
2746         char *pdata = *ppdata;
2747         char *params = *pparams;
2748         uint16 info_level;
2749         int outsize;
2750
2751         DEBUG(10,("call_trans2setfsinfo: for service [%s]\n",lp_servicename(SNUM(conn))));
2752
2753         /*  */
2754         if (total_params < 4) {
2755                 DEBUG(0,("call_trans2setfsinfo: requires total_params(%d) >= 4 bytes!\n",
2756                         total_params));
2757                 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2758         }
2759
2760         info_level = SVAL(params,2);
2761
2762         switch(info_level) {
2763                 case SMB_SET_CIFS_UNIX_INFO:
2764                         {
2765                                 uint16 client_unix_major;
2766                                 uint16 client_unix_minor;
2767                                 uint32 client_unix_cap_low;
2768                                 uint32 client_unix_cap_high;
2769
2770                                 if (!lp_unix_extensions()) {
2771                                         return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2772                                 }
2773
2774                                 /* There should be 12 bytes of capabilities set. */
2775                                 if (total_data < 8) {
2776                                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2777                                 }
2778                                 client_unix_major = SVAL(pdata,0);
2779                                 client_unix_minor = SVAL(pdata,2);
2780                                 client_unix_cap_low = IVAL(pdata,4);
2781                                 client_unix_cap_high = IVAL(pdata,8);
2782                                 /* Just print these values for now. */
2783                                 DEBUG(10,("call_trans2setfsinfo: set unix info. major = %u, minor = %u \
2784 cap_low = 0x%x, cap_high = 0x%x\n",
2785                                         (unsigned int)client_unix_major,
2786                                         (unsigned int)client_unix_minor,
2787                                         (unsigned int)client_unix_cap_low,
2788                                         (unsigned int)client_unix_cap_high ));
2789
2790                                 /* Here is where we must switch to posix pathname processing... */
2791                                 if (client_unix_cap_low & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2792                                         lp_set_posix_pathnames();
2793                                         mangle_change_to_posix();
2794                                 }
2795
2796                                 if ((client_unix_cap_low & CIFS_UNIX_FCNTL_LOCKS_CAP) &&
2797                                     !(client_unix_cap_low & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP)) {
2798                                         /* Client that knows how to do posix locks,
2799                                          * but not posix open/mkdir operations. Set a
2800                                          * default type for read/write checks. */
2801
2802                                         lp_set_posix_default_cifsx_readwrite_locktype(POSIX_LOCK);
2803
2804                                 }
2805                                 break;
2806                         }
2807                 case SMB_REQUEST_TRANSPORT_ENCRYPTION:
2808                         {
2809                                 NTSTATUS status;
2810                                 size_t param_len = 0;
2811                                 size_t data_len = total_data;
2812
2813                                 if (!lp_unix_extensions()) {
2814                                         return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2815                                 }
2816
2817                                 DEBUG( 4,("call_trans2setfsinfo: request transport encrption.\n"));
2818
2819                                 status = srv_request_encryption_setup(conn,
2820                                                                         (unsigned char **)ppdata,
2821                                                                         &data_len,
2822                                                                         (unsigned char **)pparams,
2823                                                                         &param_len
2824                                                                         );
2825
2826                                 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
2827                                         error_packet_set(outbuf, 0, 0, status, __LINE__,__FILE__);
2828                                 } else if (!NT_STATUS_IS_OK(status)) {
2829                                         return ERROR_NT(status);
2830                                 }
2831
2832                                 send_trans2_replies(inbuf, outbuf, bufsize, *pparams, param_len, *ppdata, data_len, max_data_bytes);
2833
2834                                 if (NT_STATUS_IS_OK(status)) {
2835                                         /* Server-side transport encryption is now *on*. */
2836                                         status = srv_encryption_start(conn);
2837                                         if (!NT_STATUS_IS_OK(status)) {
2838                                                 exit_server_cleanly("Failure in setting up encrypted transport");
2839                                         }
2840                                 }
2841                                 return -1;
2842                         }
2843                 case SMB_FS_QUOTA_INFORMATION:
2844                         {
2845                                 files_struct *fsp = NULL;
2846                                 SMB_NTQUOTA_STRUCT quotas;
2847         
2848                                 ZERO_STRUCT(quotas);
2849
2850                                 /* access check */
2851                                 if ((current_user.ut.uid != 0)||!CAN_WRITE(conn)) {
2852                                         DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
2853                                                 lp_servicename(SNUM(conn)),conn->user));
2854                                         return ERROR_DOS(ERRSRV,ERRaccess);
2855                                 }
2856
2857                                 /* note: normaly there're 48 bytes,
2858                                  * but we didn't use the last 6 bytes for now 
2859                                  * --metze 
2860                                  */
2861                                 fsp = file_fsp(params,0);
2862                                 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2863                                         DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2864                                         return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2865                                 }
2866
2867                                 if (total_data < 42) {
2868                                         DEBUG(0,("call_trans2setfsinfo: SET_FS_QUOTA: requires total_data(%d) >= 42 bytes!\n",
2869                                                 total_data));
2870                                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2871                                 }
2872                         
2873                                 /* unknown_1 24 NULL bytes in pdata*/
2874                 
2875                                 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2876                                 quotas.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2877 #ifdef LARGE_SMB_OFF_T
2878                                 quotas.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2879 #else /* LARGE_SMB_OFF_T */
2880                                 if ((IVAL(pdata,28) != 0)&&
2881                                         ((quotas.softlim != 0xFFFFFFFF)||
2882                                         (IVAL(pdata,28)!=0xFFFFFFFF))) {
2883                                         /* more than 32 bits? */
2884                                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2885                                 }
2886 #endif /* LARGE_SMB_OFF_T */
2887                 
2888                                 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2889                                 quotas.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2890 #ifdef LARGE_SMB_OFF_T
2891                                 quotas.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2892 #else /* LARGE_SMB_OFF_T */
2893                                 if ((IVAL(pdata,36) != 0)&&
2894                                         ((quotas.hardlim != 0xFFFFFFFF)||
2895                                         (IVAL(pdata,36)!=0xFFFFFFFF))) {
2896                                         /* more than 32 bits? */
2897                                         return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2898                                 }
2899 #endif /* LARGE_SMB_OFF_T */
2900                 
2901                                 /* quota_flags 2 bytes **/
2902                                 quotas.qflags = SVAL(pdata,40);
2903                 
2904                                 /* unknown_2 6 NULL bytes follow*/
2905                 
2906                                 /* now set the quotas */
2907                                 if (vfs_set_ntquota(fsp, SMB_USER_FS_QUOTA_TYPE, NULL, &quotas)!=0) {
2908                                         DEBUG(0,("vfs_set_ntquota() failed for service [%s]\n",lp_servicename(SNUM(conn))));
2909                                         return ERROR_DOS(ERRSRV,ERRerror);
2910                                 }
2911                         
2912                                 break;
2913                         }
2914                 default:
2915                         DEBUG(3,("call_trans2setfsinfo: unknown level (0x%X) not implemented yet.\n",
2916                                 info_level));
2917                         return ERROR_NT(NT_STATUS_INVALID_LEVEL);
2918                         break;
2919         }
2920
2921         /* 
2922          * sending this reply works fine, 
2923          * but I'm not sure it's the same 
2924          * like windows do...
2925          * --metze
2926          */ 
2927         outsize = set_message(inbuf, outbuf,10,0,True);
2928
2929         return outsize;
2930 }
2931
2932 #if defined(HAVE_POSIX_ACLS)
2933 /***********************************