c3768d6ee02bb571320d2922687f2e9bc01c73a5
[samba.git] / source4 / smb_server / trans2.c
1 /* 
2    Unix SMB/CIFS implementation.
3    transaction2 handling
4    Copyright (C) Andrew Tridgell 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21    This file handles the parsing of transact2 requests
22 */
23
24 #include "includes.h"
25 #include "smb_server/smb_server.h"
26
27
28
29 #define CHECK_MIN_BLOB_SIZE(blob, size) do { \
30         if ((blob)->length < (size)) { \
31                 return NT_STATUS_INFO_LENGTH_MISMATCH; \
32         }} while (0)
33
34 /* grow the data allocation size of a trans2 reply - this guarantees
35    that requests to grow the data size later will not change the
36    pointer */
37 static void trans2_grow_data_allocation(struct smbsrv_request *req, 
38                                         struct smb_trans2 *trans,
39                                         uint16_t new_size)
40 {
41         if (new_size <= trans->out.data.length) {
42                 return;
43         }
44         trans->out.data.data = talloc_realloc(req, trans->out.data.data, new_size);
45 }
46
47
48 /* grow the data size of a trans2 reply */
49 static void trans2_grow_data(struct smbsrv_request *req, 
50                              struct smb_trans2 *trans,
51                              uint16_t new_size)
52 {
53         trans2_grow_data_allocation(req, trans, new_size);
54         trans->out.data.length = new_size;
55 }
56
57 /* grow the data, zero filling any new bytes */
58 static void trans2_grow_data_fill(struct smbsrv_request *req, 
59                                   struct smb_trans2 *trans,
60                                   uint16_t new_size)
61 {
62         uint16_t old_size = trans->out.data.length;
63         trans2_grow_data(req, trans, new_size);
64         if (new_size > old_size) {
65                 memset(trans->out.data.data + old_size, 0, new_size - old_size);
66         }
67 }
68
69
70 /* setup a trans2 reply, given the data and params sizes */
71 static void trans2_setup_reply(struct smbsrv_request *req, 
72                                struct smb_trans2 *trans,
73                                uint16_t param_size, uint16_t data_size,
74                                uint16_t setup_count)
75 {
76         trans->out.setup_count = setup_count;
77         if (setup_count != 0) {
78                 trans->out.setup = talloc_zero_array_p(req, uint16_t, setup_count);
79         }
80         trans->out.params = data_blob_talloc(req, NULL, param_size);
81         trans->out.data = data_blob_talloc(req, NULL, data_size);
82 }
83
84
85 /*
86   pull a string from a blob in a trans2 request
87 */
88 static size_t trans2_pull_blob_string(struct smbsrv_request *req, 
89                                       const DATA_BLOB *blob,
90                                       uint16_t offset,
91                                       const char **str,
92                                       int flags)
93 {
94         /* we use STR_NO_RANGE_CHECK because the params are allocated
95            separately in a DATA_BLOB, so we need to do our own range
96            checking */
97         if (offset >= blob->length) {
98                 *str = NULL;
99                 return 0;
100         }
101         
102         return req_pull_string(req, str, 
103                                blob->data + offset, 
104                                blob->length - offset,
105                                STR_NO_RANGE_CHECK | flags);
106 }
107
108 /*
109   push a string into the data section of a trans2 request
110   return the number of bytes consumed in the output
111 */
112 static size_t trans2_push_data_string(struct smbsrv_request *req, 
113                                       struct smb_trans2 *trans,
114                                       uint16_t len_offset,
115                                       uint16_t offset,
116                                       const WIRE_STRING *str,
117                                       int dest_len,
118                                       int flags)
119 {
120         int alignment = 0, ret = 0, pkt_len;
121
122         /* we use STR_NO_RANGE_CHECK because the params are allocated
123            separately in a DATA_BLOB, so we need to do our own range
124            checking */
125         if (!str->s || offset >= trans->out.data.length) {
126                 if (flags & STR_LEN8BIT) {
127                         SCVAL(trans->out.data.data, len_offset, 0);
128                 } else {
129                         SIVAL(trans->out.data.data, len_offset, 0);
130                 }
131                 return 0;
132         }
133
134         flags |= STR_NO_RANGE_CHECK;
135
136         if (dest_len == -1 || (dest_len > trans->out.data.length - offset)) {
137                 dest_len = trans->out.data.length - offset;
138         }
139
140         if (!(flags & (STR_ASCII|STR_UNICODE))) {
141                 flags |= (req->flags2 & FLAGS2_UNICODE_STRINGS) ? STR_UNICODE : STR_ASCII;
142         }
143
144         if ((offset&1) && (flags & STR_UNICODE) && !(flags & STR_NOALIGN)) {
145                 alignment = 1;
146                 if (dest_len > 0) {
147                         SCVAL(trans->out.data.data + offset, 0, 0);
148                         ret = push_string(trans->out.data.data + offset + 1, str->s, dest_len-1, flags);
149                 }
150         } else {
151                 ret = push_string(trans->out.data.data + offset, str->s, dest_len, flags);
152         }
153
154         /* sometimes the string needs to be terminated, but the length
155            on the wire must not include the termination! */
156         pkt_len = ret;
157
158         if ((flags & STR_LEN_NOTERM) && (flags & STR_TERMINATE)) {
159                 if ((flags & STR_UNICODE) && ret >= 2) {
160                         pkt_len = ret-2;
161                 }
162                 if ((flags & STR_ASCII) && ret >= 1) {
163                         pkt_len = ret-1;
164                 }
165         }       
166
167         if (flags & STR_LEN8BIT) {
168                 SCVAL(trans->out.data.data, len_offset, pkt_len);
169         } else {
170                 SIVAL(trans->out.data.data, len_offset, pkt_len);
171         }
172
173         return ret + alignment;
174 }
175
176 /*
177   append a string to the data section of a trans2 reply
178   len_offset points to the place in the packet where the length field
179   should go
180 */
181 static void trans2_append_data_string(struct smbsrv_request *req, 
182                                         struct smb_trans2 *trans,
183                                         const WIRE_STRING *str,
184                                         uint_t len_offset,
185                                         int flags)
186 {
187         size_t ret;
188         uint16_t offset;
189         const int max_bytes_per_char = 3;
190
191         offset = trans->out.data.length;
192         trans2_grow_data(req, trans, offset + (2+strlen_m(str->s))*max_bytes_per_char);
193         ret = trans2_push_data_string(req, trans, len_offset, offset, str, -1, flags);
194         trans2_grow_data(req, trans, offset + ret);
195 }
196
197 /*
198   align the end of the data section of a trans reply on an even boundary
199 */
200 static void trans2_align_data(struct smbsrv_request *req, struct smb_trans2 *trans)
201 {
202         if ((trans->out.data.length & 1) == 0) {
203                 return;
204         }
205         trans2_grow_data(req, trans, trans->out.data.length+1);
206         SCVAL(trans->out.data.data, trans->out.data.length-1, 0);
207 }
208
209
210 /*
211   trans2 qfsinfo implementation
212 */
213 static NTSTATUS trans2_qfsinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
214 {
215         union smb_fsinfo fsinfo;
216         NTSTATUS status;
217         uint16_t level;
218         uint_t i;
219         DATA_BLOB guid_blob;
220
221         /* make sure we got enough parameters */
222         if (trans->in.params.length != 2) {
223                 return NT_STATUS_FOOBAR;
224         }
225
226         level = SVAL(trans->in.params.data, 0);
227
228         switch (level) {
229         case SMB_QFS_ALLOCATION:
230                 fsinfo.allocation.level = RAW_QFS_ALLOCATION;
231
232                 status = ntvfs_fsinfo(req, &fsinfo);
233                 if (!NT_STATUS_IS_OK(status)) {
234                         return status;
235                 }
236
237                 trans2_setup_reply(req, trans, 0, 18, 0);
238
239                 SIVAL(trans->out.data.data,  0, fsinfo.allocation.out.fs_id);
240                 SIVAL(trans->out.data.data,  4, fsinfo.allocation.out.sectors_per_unit);
241                 SIVAL(trans->out.data.data,  8, fsinfo.allocation.out.total_alloc_units);
242                 SIVAL(trans->out.data.data, 12, fsinfo.allocation.out.avail_alloc_units);
243                 SSVAL(trans->out.data.data, 16, fsinfo.allocation.out.bytes_per_sector);
244
245                 return NT_STATUS_OK;
246
247         case SMB_QFS_VOLUME:
248                 fsinfo.volume.level = RAW_QFS_VOLUME;
249
250                 status = ntvfs_fsinfo(req, &fsinfo);
251                 if (!NT_STATUS_IS_OK(status)) {
252                         return status;
253                 }
254
255                 trans2_setup_reply(req, trans, 0, 5, 0);
256
257                 SIVAL(trans->out.data.data,       0, fsinfo.volume.out.serial_number);
258                 /* w2k3 implements this incorrectly for unicode - it
259                  * leaves the last byte off the string */
260                 trans2_append_data_string(req, trans, 
261                                           &fsinfo.volume.out.volume_name, 
262                                           4, STR_LEN8BIT|STR_NOALIGN);
263
264                 return NT_STATUS_OK;
265
266         case SMB_QFS_VOLUME_INFO:
267         case SMB_QFS_VOLUME_INFORMATION:
268                 fsinfo.volume_info.level = RAW_QFS_VOLUME_INFO;
269
270                 status = ntvfs_fsinfo(req, &fsinfo);
271                 if (!NT_STATUS_IS_OK(status)) {
272                         return status;
273                 }
274
275                 trans2_setup_reply(req, trans, 0, 18, 0);
276
277                 push_nttime(trans->out.data.data, 0, fsinfo.volume_info.out.create_time);
278                 SIVAL(trans->out.data.data,       8, fsinfo.volume_info.out.serial_number);
279                 SSVAL(trans->out.data.data,      16, 0); /* padding */
280                 trans2_append_data_string(req, trans, 
281                                           &fsinfo.volume_info.out.volume_name, 
282                                           12, STR_UNICODE);
283
284                 return NT_STATUS_OK;
285
286         case SMB_QFS_SIZE_INFO:
287         case SMB_QFS_SIZE_INFORMATION:
288                 fsinfo.size_info.level = RAW_QFS_SIZE_INFO;
289
290                 status = ntvfs_fsinfo(req, &fsinfo);
291                 if (!NT_STATUS_IS_OK(status)) {
292                         return status;
293                 }
294
295                 trans2_setup_reply(req, trans, 0, 24, 0);
296
297                 SBVAL(trans->out.data.data,  0, fsinfo.size_info.out.total_alloc_units);
298                 SBVAL(trans->out.data.data,  8, fsinfo.size_info.out.avail_alloc_units);
299                 SIVAL(trans->out.data.data, 16, fsinfo.size_info.out.sectors_per_unit);
300                 SIVAL(trans->out.data.data, 20, fsinfo.size_info.out.bytes_per_sector);
301
302                 return NT_STATUS_OK;
303
304         case SMB_QFS_DEVICE_INFO:
305         case SMB_QFS_DEVICE_INFORMATION:
306                 fsinfo.device_info.level = RAW_QFS_DEVICE_INFO;
307
308                 status = ntvfs_fsinfo(req, &fsinfo);
309                 if (!NT_STATUS_IS_OK(status)) {
310                         return status;
311                 }
312                 trans2_setup_reply(req, trans, 0, 8, 0);
313                 SIVAL(trans->out.data.data,      0, fsinfo.device_info.out.device_type);
314                 SIVAL(trans->out.data.data,      4, fsinfo.device_info.out.characteristics);
315                 return NT_STATUS_OK;
316
317
318         case SMB_QFS_ATTRIBUTE_INFO:
319         case SMB_QFS_ATTRIBUTE_INFORMATION:
320                 fsinfo.attribute_info.level = RAW_QFS_ATTRIBUTE_INFO;
321
322                 status = ntvfs_fsinfo(req, &fsinfo);
323                 if (!NT_STATUS_IS_OK(status)) {
324                         return status;
325                 }
326
327                 trans2_setup_reply(req, trans, 0, 12, 0);
328
329                 SIVAL(trans->out.data.data, 0, fsinfo.attribute_info.out.fs_attr);
330                 SIVAL(trans->out.data.data, 4, fsinfo.attribute_info.out.max_file_component_length);
331                 /* this must not be null terminated or win98 gets
332                    confused!  also note that w2k3 returns this as
333                    unicode even when ascii is negotiated */
334                 trans2_append_data_string(req, trans, 
335                                           &fsinfo.attribute_info.out.fs_type,
336                                           8, STR_UNICODE);
337                 return NT_STATUS_OK;
338
339
340         case SMB_QFS_QUOTA_INFORMATION:
341                 fsinfo.quota_information.level = RAW_QFS_QUOTA_INFORMATION;
342
343                 status = ntvfs_fsinfo(req, &fsinfo);
344                 if (!NT_STATUS_IS_OK(status)) {
345                         return status;
346                 }
347
348                 trans2_setup_reply(req, trans, 0, 48, 0);
349
350                 SBVAL(trans->out.data.data,   0, fsinfo.quota_information.out.unknown[0]);
351                 SBVAL(trans->out.data.data,   8, fsinfo.quota_information.out.unknown[1]);
352                 SBVAL(trans->out.data.data,  16, fsinfo.quota_information.out.unknown[2]);
353                 SBVAL(trans->out.data.data,  24, fsinfo.quota_information.out.quota_soft);
354                 SBVAL(trans->out.data.data,  32, fsinfo.quota_information.out.quota_hard);
355                 SBVAL(trans->out.data.data,  40, fsinfo.quota_information.out.quota_flags);
356
357                 return NT_STATUS_OK;
358
359
360         case SMB_QFS_FULL_SIZE_INFORMATION:
361                 fsinfo.full_size_information.level = RAW_QFS_FULL_SIZE_INFORMATION;
362
363                 status = ntvfs_fsinfo(req, &fsinfo);
364                 if (!NT_STATUS_IS_OK(status)) {
365                         return status;
366                 }
367
368                 trans2_setup_reply(req, trans, 0, 32, 0);
369
370                 SBVAL(trans->out.data.data,  0, fsinfo.full_size_information.out.total_alloc_units);
371                 SBVAL(trans->out.data.data,  8, fsinfo.full_size_information.out.call_avail_alloc_units);
372                 SBVAL(trans->out.data.data, 16, fsinfo.full_size_information.out.actual_avail_alloc_units);
373                 SIVAL(trans->out.data.data, 24, fsinfo.full_size_information.out.sectors_per_unit);
374                 SIVAL(trans->out.data.data, 28, fsinfo.full_size_information.out.bytes_per_sector);
375
376                 return NT_STATUS_OK;
377
378         case SMB_QFS_OBJECTID_INFORMATION:
379                 fsinfo.objectid_information.level = RAW_QFS_OBJECTID_INFORMATION;
380
381                 status = ntvfs_fsinfo(req, &fsinfo);
382                 if (!NT_STATUS_IS_OK(status)) {
383                         return status;
384                 }
385
386                 trans2_setup_reply(req, trans, 0, 64, 0);
387
388                 status = ndr_push_struct_blob(&guid_blob, req, 
389                                               &fsinfo.objectid_information.out.guid,
390                                               (ndr_push_flags_fn_t)ndr_push_GUID);
391                 if (!NT_STATUS_IS_OK(status)) {
392                         return status;
393                 }
394
395                 memcpy(trans->out.data.data, guid_blob.data, guid_blob.length);
396
397                 for (i=0;i<6;i++) {
398                         SBVAL(trans->out.data.data, 16 + 8*i, fsinfo.objectid_information.out.unknown[i]);
399                 }
400                 return NT_STATUS_OK;
401         }
402
403         return NT_STATUS_INVALID_LEVEL;
404 }
405
406
407 /*
408   trans2 open implementation
409 */
410 static NTSTATUS trans2_open(struct smbsrv_request *req, struct smb_trans2 *trans)
411 {
412         union smb_open *io;
413         NTSTATUS status;
414
415         /* make sure we got enough parameters */
416         if (trans->in.params.length < 29) {
417                 return NT_STATUS_FOOBAR;
418         }
419
420         io = talloc_p(req, union smb_open);
421         if (io == NULL) {
422                 return NT_STATUS_NO_MEMORY;
423         }
424
425         io->t2open.level         = RAW_OPEN_T2OPEN;
426         io->t2open.in.flags      = SVAL(trans->in.params.data, VWV(0));
427         io->t2open.in.open_mode  = SVAL(trans->in.params.data, VWV(1));
428         io->t2open.in.file_attrs = SVAL(trans->in.params.data, VWV(3));
429         io->t2open.in.write_time = srv_pull_dos_date(req->smb_conn, 
430                                                     trans->in.params.data + VWV(4));;
431         io->t2open.in.open_func  = SVAL(trans->in.params.data, VWV(6));
432         io->t2open.in.size       = IVAL(trans->in.params.data, VWV(7));
433         io->t2open.in.timeout    = IVAL(trans->in.params.data, VWV(9));
434         io->t2open.in.num_eas    = 0;
435         io->t2open.in.eas        = NULL;
436
437         trans2_pull_blob_string(req, &trans->in.params, 28, &io->t2open.in.fname, 0);
438
439         status = ea_pull_list(&trans->in.data, io, &io->t2open.in.num_eas, &io->t2open.in.eas);
440         if (!NT_STATUS_IS_OK(status)) {
441                 return status;
442         }
443
444         status = ntvfs_openfile(req, io);
445         if (!NT_STATUS_IS_OK(status)) {
446                 return status;
447         }
448
449         trans2_setup_reply(req, trans, 30, 0, 0);
450
451         SSVAL(trans->out.params.data, VWV(0), io->t2open.out.fnum);
452         SSVAL(trans->out.params.data, VWV(1), io->t2open.out.attrib);
453         srv_push_dos_date3(req->smb_conn, trans->out.params.data, 
454                            VWV(2), io->t2open.out.write_time);
455         SIVAL(trans->out.params.data, VWV(4), io->t2open.out.size);
456         SSVAL(trans->out.params.data, VWV(6), io->t2open.out.access);
457         SSVAL(trans->out.params.data, VWV(7), io->t2open.out.ftype);
458         SSVAL(trans->out.params.data, VWV(8), io->t2open.out.devstate);
459         SSVAL(trans->out.params.data, VWV(9), io->t2open.out.action);
460         SIVAL(trans->out.params.data, VWV(10), 0); /* reserved */
461         SSVAL(trans->out.params.data, VWV(12), 0); /* EaErrorOffset */
462         SIVAL(trans->out.params.data, VWV(13), 0); /* EaLength */
463
464         return status;
465 }
466
467
468 /*
469   trans2 mkdir implementation
470 */
471 static NTSTATUS trans2_mkdir(struct smbsrv_request *req, struct smb_trans2 *trans)
472 {
473         union smb_mkdir *io;
474         NTSTATUS status;
475
476         /* make sure we got enough parameters */
477         if (trans->in.params.length < 5) {
478                 return NT_STATUS_FOOBAR;
479         }
480
481         io = talloc_p(req, union smb_mkdir);
482         if (io == NULL) {
483                 return NT_STATUS_NO_MEMORY;
484         }
485
486         io->t2mkdir.level = RAW_MKDIR_T2MKDIR;
487         trans2_pull_blob_string(req, &trans->in.params, 4, &io->t2mkdir.in.path, 0);
488
489         status = ea_pull_list(&trans->in.data, io, 
490                               &io->t2mkdir.in.num_eas, 
491                               &io->t2mkdir.in.eas);
492         if (!NT_STATUS_IS_OK(status)) {
493                 return status;
494         }
495
496         status = ntvfs_mkdir(req, io);
497         if (!NT_STATUS_IS_OK(status)) {
498                 return status;
499         }
500
501         trans2_setup_reply(req, trans, 2, 0, 0);
502
503         SSVAL(trans->out.params.data, VWV(0), 0);
504
505         return status;
506 }
507
508 /*
509   fill in the reply from a qpathinfo or qfileinfo call
510 */
511 static NTSTATUS trans2_fileinfo_fill(struct smbsrv_request *req, struct smb_trans2 *trans,
512                                      union smb_fileinfo *st)
513 {
514         uint_t i;
515         
516         switch (st->generic.level) {
517         case RAW_FILEINFO_GENERIC:
518         case RAW_FILEINFO_GETATTR:
519         case RAW_FILEINFO_GETATTRE:
520         case RAW_FILEINFO_SEC_DESC:
521                 /* handled elsewhere */
522                 return NT_STATUS_INVALID_LEVEL;
523
524         case RAW_FILEINFO_BASIC_INFO:
525         case RAW_FILEINFO_BASIC_INFORMATION:
526                 trans2_setup_reply(req, trans, 2, 40, 0);
527
528                 SSVAL(trans->out.params.data, 0, 0);
529                 push_nttime(trans->out.data.data,  0, st->basic_info.out.create_time);
530                 push_nttime(trans->out.data.data,  8, st->basic_info.out.access_time);
531                 push_nttime(trans->out.data.data, 16, st->basic_info.out.write_time);
532                 push_nttime(trans->out.data.data, 24, st->basic_info.out.change_time);
533                 SIVAL(trans->out.data.data,       32, st->basic_info.out.attrib);
534                 SIVAL(trans->out.data.data,       36, 0); /* padding */
535                 return NT_STATUS_OK;
536
537         case RAW_FILEINFO_STANDARD:
538                 trans2_setup_reply(req, trans, 2, 22, 0);
539
540                 SSVAL(trans->out.params.data, 0, 0);
541                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 0, st->standard.out.create_time);
542                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 4, st->standard.out.access_time);
543                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 8, st->standard.out.write_time);
544                 SIVAL(trans->out.data.data,        12, st->standard.out.size);
545                 SIVAL(trans->out.data.data,        16, st->standard.out.alloc_size);
546                 SSVAL(trans->out.data.data,        20, st->standard.out.attrib);
547                 return NT_STATUS_OK;
548
549         case RAW_FILEINFO_EA_SIZE:
550                 trans2_setup_reply(req, trans, 2, 26, 0);
551
552                 SSVAL(trans->out.params.data, 0, 0);
553                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 0, st->ea_size.out.create_time);
554                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 4, st->ea_size.out.access_time);
555                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 8, st->ea_size.out.write_time);
556                 SIVAL(trans->out.data.data,        12, st->ea_size.out.size);
557                 SIVAL(trans->out.data.data,        16, st->ea_size.out.alloc_size);
558                 SSVAL(trans->out.data.data,        20, st->ea_size.out.attrib);
559                 SIVAL(trans->out.data.data,        22, st->ea_size.out.ea_size);
560                 return NT_STATUS_OK;
561
562         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
563                 trans2_setup_reply(req, trans, 2, 56, 0);
564
565                 SSVAL(trans->out.params.data, 0, 0);
566                 push_nttime(trans->out.data.data,  0, st->network_open_information.out.create_time);
567                 push_nttime(trans->out.data.data,  8, st->network_open_information.out.access_time);
568                 push_nttime(trans->out.data.data, 16, st->network_open_information.out.write_time);
569                 push_nttime(trans->out.data.data, 24, st->network_open_information.out.change_time);
570                 SBVAL(trans->out.data.data,       32, st->network_open_information.out.alloc_size);
571                 SBVAL(trans->out.data.data,       40, st->network_open_information.out.size);
572                 SIVAL(trans->out.data.data,       48, st->network_open_information.out.attrib);
573                 SIVAL(trans->out.data.data,       52, 0); /* padding */
574                 return NT_STATUS_OK;
575
576         case RAW_FILEINFO_STANDARD_INFO:
577         case RAW_FILEINFO_STANDARD_INFORMATION:
578                 trans2_setup_reply(req, trans, 2, 24, 0);
579                 SSVAL(trans->out.params.data, 0, 0);
580                 SBVAL(trans->out.data.data,  0, st->standard_info.out.alloc_size);
581                 SBVAL(trans->out.data.data,  8, st->standard_info.out.size);
582                 SIVAL(trans->out.data.data, 16, st->standard_info.out.nlink);
583                 SCVAL(trans->out.data.data, 20, st->standard_info.out.delete_pending);
584                 SCVAL(trans->out.data.data, 21, st->standard_info.out.directory);
585                 SSVAL(trans->out.data.data, 22, 0); /* padding */
586                 return NT_STATUS_OK;
587
588         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
589                 trans2_setup_reply(req, trans, 2, 8, 0);
590                 SSVAL(trans->out.params.data, 0, 0);
591                 SIVAL(trans->out.data.data,  0, st->attribute_tag_information.out.attrib);
592                 SIVAL(trans->out.data.data,  4, st->attribute_tag_information.out.reparse_tag);
593                 return NT_STATUS_OK;
594
595         case RAW_FILEINFO_EA_INFO:
596         case RAW_FILEINFO_EA_INFORMATION:
597                 trans2_setup_reply(req, trans, 2, 4, 0);
598                 SSVAL(trans->out.params.data, 0, 0);
599                 SIVAL(trans->out.data.data,  0, st->ea_info.out.ea_size);
600                 return NT_STATUS_OK;
601
602         case RAW_FILEINFO_MODE_INFORMATION:
603                 trans2_setup_reply(req, trans, 2, 4, 0);
604                 SSVAL(trans->out.params.data, 0, 0);
605                 SIVAL(trans->out.data.data,  0, st->mode_information.out.mode);
606                 return NT_STATUS_OK;
607
608         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
609                 trans2_setup_reply(req, trans, 2, 4, 0);
610                 SSVAL(trans->out.params.data, 0, 0);
611                 SIVAL(trans->out.data.data,  0, 
612                       st->alignment_information.out.alignment_requirement);
613                 return NT_STATUS_OK;
614
615         case RAW_FILEINFO_ALL_EAS:
616                 if (st->all_eas.out.num_eas == 0) {
617                         trans2_setup_reply(req, trans, 2, 4, 0);
618                         SSVAL(trans->out.params.data, 0, 0);
619                         SIVAL(trans->out.data.data,  0, 0);
620                 } else {
621                         uint32_t list_size = ea_list_size(st->all_eas.out.num_eas,
622                                                         st->all_eas.out.eas);
623                         trans2_setup_reply(req, trans, 2, list_size, 0);
624                         SSVAL(trans->out.params.data, 0, 0);
625                         ea_put_list(trans->out.data.data, 
626                                     st->all_eas.out.num_eas, st->all_eas.out.eas);
627                 }
628                 return NT_STATUS_OK;
629
630         case RAW_FILEINFO_ACCESS_INFORMATION:
631                 trans2_setup_reply(req, trans, 2, 4, 0);
632                 SSVAL(trans->out.params.data, 0, 0);
633                 SIVAL(trans->out.data.data,  0, st->access_information.out.access_flags);
634                 return NT_STATUS_OK;
635
636         case RAW_FILEINFO_POSITION_INFORMATION:
637                 trans2_setup_reply(req, trans, 2, 8, 0);
638                 SSVAL(trans->out.params.data, 0, 0);
639                 SBVAL(trans->out.data.data,  0, st->position_information.out.position);
640                 return NT_STATUS_OK;
641
642         case RAW_FILEINFO_COMPRESSION_INFO:
643         case RAW_FILEINFO_COMPRESSION_INFORMATION:
644                 trans2_setup_reply(req, trans, 2, 16, 0);
645                 SSVAL(trans->out.params.data, 0, 0);
646                 SBVAL(trans->out.data.data,  0, st->compression_info.out.compressed_size);
647                 SSVAL(trans->out.data.data,  8, st->compression_info.out.format);
648                 SCVAL(trans->out.data.data, 10, st->compression_info.out.unit_shift);
649                 SCVAL(trans->out.data.data, 11, st->compression_info.out.chunk_shift);
650                 SCVAL(trans->out.data.data, 12, st->compression_info.out.cluster_shift);
651                 SSVAL(trans->out.data.data, 13, 0); /* 3 bytes padding */
652                 SCVAL(trans->out.data.data, 15, 0);
653                 return NT_STATUS_OK;
654
655         case RAW_FILEINFO_IS_NAME_VALID:
656                 trans2_setup_reply(req, trans, 2, 0, 0);
657                 SSVAL(trans->out.params.data, 0, 0);
658                 return NT_STATUS_OK;
659
660         case RAW_FILEINFO_INTERNAL_INFORMATION:
661                 trans2_setup_reply(req, trans, 2, 8, 0);
662                 SSVAL(trans->out.params.data, 0, 0);
663                 SBVAL(trans->out.data.data,  0, st->internal_information.out.file_id);
664                 return NT_STATUS_OK;
665
666         case RAW_FILEINFO_ALL_INFO:
667         case RAW_FILEINFO_ALL_INFORMATION:
668                 trans2_setup_reply(req, trans, 2, 72, 0);
669
670                 SSVAL(trans->out.params.data, 0, 0);
671                 push_nttime(trans->out.data.data,  0, st->all_info.out.create_time);
672                 push_nttime(trans->out.data.data,  8, st->all_info.out.access_time);
673                 push_nttime(trans->out.data.data, 16, st->all_info.out.write_time);
674                 push_nttime(trans->out.data.data, 24, st->all_info.out.change_time);
675                 SIVAL(trans->out.data.data,       32, st->all_info.out.attrib);
676                 SIVAL(trans->out.data.data,       36, 0);
677                 SBVAL(trans->out.data.data,       40, st->all_info.out.alloc_size);
678                 SBVAL(trans->out.data.data,       48, st->all_info.out.size);
679                 SIVAL(trans->out.data.data,       56, st->all_info.out.nlink);
680                 SCVAL(trans->out.data.data,       60, st->all_info.out.delete_pending);
681                 SCVAL(trans->out.data.data,       61, st->all_info.out.directory);
682                 SSVAL(trans->out.data.data,       62, 0); /* padding */
683                 SIVAL(trans->out.data.data,       64, st->all_info.out.ea_size);
684                 trans2_append_data_string(req, trans, &st->all_info.out.fname, 
685                                           68, STR_UNICODE);
686                 return NT_STATUS_OK;
687
688         case RAW_FILEINFO_NAME_INFO:
689         case RAW_FILEINFO_NAME_INFORMATION:
690                 trans2_setup_reply(req, trans, 2, 4, 0);
691                 SSVAL(trans->out.params.data, 0, 0);
692                 trans2_append_data_string(req, trans, &st->name_info.out.fname, 0, STR_UNICODE);
693                 return NT_STATUS_OK;
694
695         case RAW_FILEINFO_ALT_NAME_INFO:
696         case RAW_FILEINFO_ALT_NAME_INFORMATION:
697                 trans2_setup_reply(req, trans, 2, 4, 0);
698                 SSVAL(trans->out.params.data, 0, 0);
699                 trans2_append_data_string(req, trans, &st->alt_name_info.out.fname, 0, STR_UNICODE);
700                 return NT_STATUS_OK;
701
702         case RAW_FILEINFO_STREAM_INFO:
703         case RAW_FILEINFO_STREAM_INFORMATION:
704                 trans2_setup_reply(req, trans, 2, 0, 0);
705
706                 SSVAL(trans->out.params.data, 0, 0);
707
708                 for (i=0;i<st->stream_info.out.num_streams;i++) {
709                         uint16_t data_size = trans->out.data.length;
710                         uint8_t *data;
711
712                         trans2_grow_data(req, trans, data_size + 24);
713                         data = trans->out.data.data + data_size;
714                         SBVAL(data,  8, st->stream_info.out.streams[i].size);
715                         SBVAL(data, 16, st->stream_info.out.streams[i].alloc_size);
716                         trans2_append_data_string(req, trans, 
717                                                   &st->stream_info.out.streams[i].stream_name, 
718                                                   data_size + 4, STR_UNICODE);
719                         if (i == st->stream_info.out.num_streams - 1) {
720                                 SIVAL(trans->out.data.data, data_size, 0);
721                         } else {
722                                 trans2_grow_data_fill(req, trans, (trans->out.data.length+7)&~7);
723                                 SIVAL(trans->out.data.data, data_size, 
724                                       trans->out.data.length - data_size);
725                         }
726                 }
727                 return NT_STATUS_OK;
728         }
729
730         return NT_STATUS_INVALID_LEVEL;
731 }
732
733 /*
734   trans2 qpathinfo implementation
735 */
736 static NTSTATUS trans2_qpathinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
737 {
738         union smb_fileinfo st;
739         NTSTATUS status;
740         uint16_t level;
741
742         /* make sure we got enough parameters */
743         if (trans->in.params.length < 2) {
744                 return NT_STATUS_FOOBAR;
745         }
746
747         level = SVAL(trans->in.params.data, 0);
748
749         trans2_pull_blob_string(req, &trans->in.params, 6, &st.generic.in.fname, 0);
750         if (st.generic.in.fname == NULL) {
751                 return NT_STATUS_FOOBAR;
752         }
753
754         /* work out the backend level - we make it 1-1 in the header */
755         st.generic.level = (enum smb_fileinfo_level)level;
756         if (st.generic.level >= RAW_FILEINFO_GENERIC) {
757                 return NT_STATUS_INVALID_LEVEL;
758         }
759
760         /* call the backend */
761         status = ntvfs_qpathinfo(req, &st);
762         if (!NT_STATUS_IS_OK(status)) {
763                 return status;
764         }
765
766         /* fill in the reply parameters */
767         status = trans2_fileinfo_fill(req, trans, &st);
768
769         return status;
770 }
771
772
773 /*
774   trans2 qpathinfo implementation
775 */
776 static NTSTATUS trans2_qfileinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
777 {
778         union smb_fileinfo st;
779         NTSTATUS status;
780         uint16_t level;
781
782         /* make sure we got enough parameters */
783         if (trans->in.params.length < 4) {
784                 return NT_STATUS_FOOBAR;
785         }
786
787         st.generic.in.fnum  = SVAL(trans->in.params.data, 0);
788         level = SVAL(trans->in.params.data, 2);
789
790         /* work out the backend level - we make it 1-1 in the header */
791         st.generic.level = (enum smb_fileinfo_level)level;
792         if (st.generic.level >= RAW_FILEINFO_GENERIC) {
793                 return NT_STATUS_INVALID_LEVEL;
794         }
795
796         /* call the backend */
797         status = ntvfs_qfileinfo(req, &st);
798         if (!NT_STATUS_IS_OK(status)) {
799                 return status;
800         }
801
802         /* fill in the reply parameters */
803         status = trans2_fileinfo_fill(req, trans, &st);
804
805         return status;
806 }
807
808
809 /*
810   parse a trans2 setfileinfo/setpathinfo data blob
811 */
812 static NTSTATUS trans2_parse_sfileinfo(struct smbsrv_request *req,
813                                        union smb_setfileinfo *st,
814                                        const DATA_BLOB *blob)
815 {
816         uint32_t len;
817
818         switch (st->generic.level) {
819         case RAW_SFILEINFO_GENERIC:
820         case RAW_SFILEINFO_SETATTR:
821         case RAW_SFILEINFO_SETATTRE:
822         case RAW_SFILEINFO_SEC_DESC:
823                 /* handled elsewhere */
824                 return NT_STATUS_INVALID_LEVEL;
825
826         case RAW_SFILEINFO_STANDARD:
827                 CHECK_MIN_BLOB_SIZE(blob, 12);
828                 st->standard.in.create_time = srv_pull_dos_date2(req->smb_conn, blob->data + 0);
829                 st->standard.in.access_time = srv_pull_dos_date2(req->smb_conn, blob->data + 4);
830                 st->standard.in.write_time  = srv_pull_dos_date2(req->smb_conn, blob->data + 8);
831                 return NT_STATUS_OK;
832
833         case RAW_SFILEINFO_EA_SET:
834                 CHECK_MIN_BLOB_SIZE(blob, 4);
835                 len = IVAL(blob->data, 0);
836                 if (len > blob->length || len < 4) {
837                         return NT_STATUS_INFO_LENGTH_MISMATCH;
838                 }
839                 {
840                         DATA_BLOB blob2;
841                         blob2.data = blob->data+4;
842                         blob2.length = len-4;
843                         len = ea_pull_struct(&blob2, req, &st->ea_set.in.ea);
844                 }
845                 if (len == 0) {
846                         return NT_STATUS_INVALID_PARAMETER;
847                 }
848                 return NT_STATUS_OK;
849
850         case SMB_SFILEINFO_BASIC_INFO:
851         case SMB_SFILEINFO_BASIC_INFORMATION:
852                 CHECK_MIN_BLOB_SIZE(blob, 36);
853                 st->basic_info.in.create_time = pull_nttime(blob->data,  0);
854                 st->basic_info.in.access_time = pull_nttime(blob->data,  8);
855                 st->basic_info.in.write_time =  pull_nttime(blob->data, 16);
856                 st->basic_info.in.change_time = pull_nttime(blob->data, 24);
857                 st->basic_info.in.attrib =      IVAL(blob->data,        32);
858                 return NT_STATUS_OK;
859
860         case SMB_SFILEINFO_DISPOSITION_INFO:
861         case SMB_SFILEINFO_DISPOSITION_INFORMATION:
862                 CHECK_MIN_BLOB_SIZE(blob, 1);
863                 st->disposition_info.in.delete_on_close = CVAL(blob->data, 0);
864                 return NT_STATUS_OK;
865
866         case SMB_SFILEINFO_ALLOCATION_INFO:
867         case SMB_SFILEINFO_ALLOCATION_INFORMATION:
868                 CHECK_MIN_BLOB_SIZE(blob, 8);
869                 st->allocation_info.in.alloc_size = BVAL(blob->data, 0);
870                 return NT_STATUS_OK;                            
871
872         case RAW_SFILEINFO_END_OF_FILE_INFO:
873         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
874                 CHECK_MIN_BLOB_SIZE(blob, 8);
875                 st->end_of_file_info.in.size = BVAL(blob->data, 0);
876                 return NT_STATUS_OK;
877
878         case RAW_SFILEINFO_RENAME_INFORMATION: {
879                 DATA_BLOB blob2;
880
881                 CHECK_MIN_BLOB_SIZE(blob, 12);
882                 st->rename_information.in.overwrite = CVAL(blob->data, 0);
883                 st->rename_information.in.root_fid  = IVAL(blob->data, 4);
884                 len                                 = IVAL(blob->data, 8);
885                 blob2.data = blob->data+12;
886                 blob2.length = MIN(blob->length, len);
887                 trans2_pull_blob_string(req, &blob2, 0, 
888                                         &st->rename_information.in.new_name, STR_UNICODE);
889                 return NT_STATUS_OK;
890         }
891
892         case RAW_SFILEINFO_POSITION_INFORMATION:
893                 CHECK_MIN_BLOB_SIZE(blob, 8);
894                 st->position_information.in.position = BVAL(blob->data, 0);
895                 return NT_STATUS_OK;
896
897         case RAW_SFILEINFO_MODE_INFORMATION:
898                 CHECK_MIN_BLOB_SIZE(blob, 4);
899                 st->mode_information.in.mode = IVAL(blob->data, 0);
900                 return NT_STATUS_OK;
901         }
902
903         return NT_STATUS_INVALID_LEVEL;
904 }
905
906 /*
907   trans2 setfileinfo implementation
908 */
909 static NTSTATUS trans2_setfileinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
910 {
911         union smb_setfileinfo st;
912         NTSTATUS status;
913         uint16_t level, fnum;
914         DATA_BLOB *blob;
915
916         /* make sure we got enough parameters */
917         if (trans->in.params.length < 4) {
918                 return NT_STATUS_FOOBAR;
919         }
920
921         fnum  = SVAL(trans->in.params.data, 0);
922         level = SVAL(trans->in.params.data, 2);
923
924         blob = &trans->in.data;
925
926         st.generic.file.fnum = fnum;
927         st.generic.level = (enum smb_setfileinfo_level)level;
928
929         status = trans2_parse_sfileinfo(req, &st, blob);
930         if (!NT_STATUS_IS_OK(status)) {
931                 return status;
932         }
933
934         status = ntvfs_setfileinfo(req, &st);
935         if (!NT_STATUS_IS_OK(status)) {
936                 return status;
937         }
938
939         trans2_setup_reply(req, trans, 2, 0, 0);
940         SSVAL(trans->out.params.data, 0, 0);
941         return NT_STATUS_OK;
942 }
943
944 /*
945   trans2 setpathinfo implementation
946 */
947 static NTSTATUS trans2_setpathinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
948 {
949         union smb_setfileinfo st;
950         NTSTATUS status;
951         uint16_t level;
952         DATA_BLOB *blob;
953
954         /* make sure we got enough parameters */
955         if (trans->in.params.length < 4) {
956                 return NT_STATUS_FOOBAR;
957         }
958
959         level = SVAL(trans->in.params.data, 0);
960         blob = &trans->in.data;
961         st.generic.level = (enum smb_setfileinfo_level)level;
962
963         trans2_pull_blob_string(req, &trans->in.params, 6, &st.generic.file.fname, 0);
964         if (st.generic.file.fname == NULL) {
965                 return NT_STATUS_FOOBAR;
966         }
967
968         status = trans2_parse_sfileinfo(req, &st, blob);
969         if (!NT_STATUS_IS_OK(status)) {
970                 return status;
971         }
972
973         status = ntvfs_setpathinfo(req, &st);
974         if (!NT_STATUS_IS_OK(status)) {
975                 return status;
976         }
977
978         trans2_setup_reply(req, trans, 2, 0, 0);
979         SSVAL(trans->out.params.data, 0, 0);
980         return NT_STATUS_OK;
981 }
982
983
984 /* a structure to encapsulate the state information about an in-progress ffirst/fnext operation */
985 struct find_state {
986         struct smbsrv_request *req;
987         struct smb_trans2 *trans;
988         enum smb_search_level level;
989         uint16_t last_entry_offset;
990         uint16_t flags;
991 };
992
993 /*
994   fill a single entry in a trans2 find reply 
995 */
996 static void find_fill_info(struct smbsrv_request *req,
997                            struct smb_trans2 *trans, 
998                            struct find_state *state,
999                            union smb_search_data *file)
1000 {
1001         uint8_t *data;
1002         uint_t ofs = trans->out.data.length;
1003
1004         switch (state->level) {
1005         case RAW_SEARCH_SEARCH:
1006         case RAW_SEARCH_FFIRST:
1007         case RAW_SEARCH_FUNIQUE:
1008         case RAW_SEARCH_GENERIC:
1009                 /* handled elsewhere */
1010                 break;
1011
1012         case RAW_SEARCH_STANDARD:
1013                 if (state->flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) {
1014                         trans2_grow_data(req, trans, ofs + 27);
1015                         SIVAL(trans->out.data.data, ofs, file->standard.resume_key);
1016                         ofs += 4;
1017                 } else {
1018                         trans2_grow_data(req, trans, ofs + 23);
1019                 }
1020                 data = trans->out.data.data + ofs;
1021                 srv_push_dos_date2(req->smb_conn, data, 0, file->standard.create_time);
1022                 srv_push_dos_date2(req->smb_conn, data, 4, file->standard.access_time);
1023                 srv_push_dos_date2(req->smb_conn, data, 8, file->standard.write_time);
1024                 SIVAL(data, 12, file->standard.size);
1025                 SIVAL(data, 16, file->standard.alloc_size);
1026                 SSVAL(data, 20, file->standard.attrib);
1027                 trans2_append_data_string(req, trans, &file->standard.name, 
1028                                           ofs + 22, STR_LEN8BIT | STR_TERMINATE | STR_LEN_NOTERM);
1029                 break;
1030
1031         case RAW_SEARCH_EA_SIZE:
1032                 if (state->flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) {
1033                         trans2_grow_data(req, trans, ofs + 31);
1034                         SIVAL(trans->out.data.data, ofs, file->ea_size.resume_key);
1035                         ofs += 4;
1036                 } else {
1037                         trans2_grow_data(req, trans, ofs + 27);
1038                 }
1039                 data = trans->out.data.data + ofs;
1040                 srv_push_dos_date2(req->smb_conn, data, 0, file->ea_size.create_time);
1041                 srv_push_dos_date2(req->smb_conn, data, 4, file->ea_size.access_time);
1042                 srv_push_dos_date2(req->smb_conn, data, 8, file->ea_size.write_time);
1043                 SIVAL(data, 12, file->ea_size.size);
1044                 SIVAL(data, 16, file->ea_size.alloc_size);
1045                 SSVAL(data, 20, file->ea_size.attrib);
1046                 SIVAL(data, 22, file->ea_size.ea_size);
1047                 trans2_append_data_string(req, trans, &file->ea_size.name, 
1048                                           ofs + 26, STR_LEN8BIT | STR_NOALIGN);
1049                 trans2_grow_data(req, trans, trans->out.data.length + 1);
1050                 trans->out.data.data[trans->out.data.length-1] = 0;
1051                 break;
1052
1053         case RAW_SEARCH_DIRECTORY_INFO:
1054                 trans2_grow_data(req, trans, ofs + 64);
1055                 data = trans->out.data.data + ofs;
1056                 SIVAL(data,          4, file->directory_info.file_index);
1057                 push_nttime(data,    8, file->directory_info.create_time);
1058                 push_nttime(data,   16, file->directory_info.access_time);
1059                 push_nttime(data,   24, file->directory_info.write_time);
1060                 push_nttime(data,   32, file->directory_info.change_time);
1061                 SBVAL(data,         40, file->directory_info.size);
1062                 SBVAL(data,         48, file->directory_info.alloc_size);
1063                 SIVAL(data,         56, file->directory_info.attrib);
1064                 trans2_append_data_string(req, trans, &file->directory_info.name, 
1065                                           ofs + 60, STR_TERMINATE_ASCII);
1066                 data = trans->out.data.data + ofs;
1067                 SIVAL(data,          0, trans->out.data.length - ofs);
1068                 break;
1069
1070         case RAW_SEARCH_FULL_DIRECTORY_INFO:
1071                 trans2_grow_data(req, trans, ofs + 68);
1072                 data = trans->out.data.data + ofs;
1073                 SIVAL(data,          4, file->full_directory_info.file_index);
1074                 push_nttime(data,    8, file->full_directory_info.create_time);
1075                 push_nttime(data,   16, file->full_directory_info.access_time);
1076                 push_nttime(data,   24, file->full_directory_info.write_time);
1077                 push_nttime(data,   32, file->full_directory_info.change_time);
1078                 SBVAL(data,         40, file->full_directory_info.size);
1079                 SBVAL(data,         48, file->full_directory_info.alloc_size);
1080                 SIVAL(data,         56, file->full_directory_info.attrib);
1081                 SIVAL(data,         64, file->full_directory_info.ea_size);
1082                 trans2_append_data_string(req, trans, &file->full_directory_info.name, 
1083                                           ofs + 60, STR_TERMINATE_ASCII);
1084                 data = trans->out.data.data + ofs;
1085                 SIVAL(data,          0, trans->out.data.length - ofs);
1086                 break;
1087
1088         case RAW_SEARCH_NAME_INFO:
1089                 trans2_grow_data(req, trans, ofs + 12);
1090                 data = trans->out.data.data + ofs;
1091                 SIVAL(data,          4, file->name_info.file_index);
1092                 trans2_append_data_string(req, trans, &file->name_info.name, 
1093                                           ofs + 8, STR_TERMINATE_ASCII);
1094                 data = trans->out.data.data + ofs;
1095                 SIVAL(data,          0, trans->out.data.length - ofs);
1096                 break;
1097
1098         case RAW_SEARCH_BOTH_DIRECTORY_INFO:
1099                 trans2_grow_data(req, trans, ofs + 94);
1100                 data = trans->out.data.data + ofs;
1101                 SIVAL(data,          4, file->both_directory_info.file_index);
1102                 push_nttime(data,    8, file->both_directory_info.create_time);
1103                 push_nttime(data,   16, file->both_directory_info.access_time);
1104                 push_nttime(data,   24, file->both_directory_info.write_time);
1105                 push_nttime(data,   32, file->both_directory_info.change_time);
1106                 SBVAL(data,         40, file->both_directory_info.size);
1107                 SBVAL(data,         48, file->both_directory_info.alloc_size);
1108                 SIVAL(data,         56, file->both_directory_info.attrib);
1109                 SIVAL(data,         64, file->both_directory_info.ea_size);
1110                 SCVAL(data,         69, 0); /* reserved */
1111                 memset(data+70,0,24);
1112                 trans2_push_data_string(req, trans, 
1113                                         68 + ofs, 70 + ofs, 
1114                                         &file->both_directory_info.short_name, 
1115                                         24, STR_UNICODE | STR_LEN8BIT);
1116                 trans2_append_data_string(req, trans, &file->both_directory_info.name, 
1117                                           ofs + 60, STR_TERMINATE_ASCII);
1118                 trans2_align_data(req, trans);
1119                 data = trans->out.data.data + ofs;
1120                 SIVAL(data,          0, trans->out.data.length - ofs);
1121                 break;
1122
1123         case RAW_SEARCH_ID_FULL_DIRECTORY_INFO:
1124                 trans2_grow_data(req, trans, ofs + 80);
1125                 data = trans->out.data.data + ofs;
1126                 SIVAL(data,          4, file->id_full_directory_info.file_index);
1127                 push_nttime(data,    8, file->id_full_directory_info.create_time);
1128                 push_nttime(data,   16, file->id_full_directory_info.access_time);
1129                 push_nttime(data,   24, file->id_full_directory_info.write_time);
1130                 push_nttime(data,   32, file->id_full_directory_info.change_time);
1131                 SBVAL(data,         40, file->id_full_directory_info.size);
1132                 SBVAL(data,         48, file->id_full_directory_info.alloc_size);
1133                 SIVAL(data,         56, file->id_full_directory_info.attrib);
1134                 SIVAL(data,         64, file->id_full_directory_info.ea_size);
1135                 SIVAL(data,         68, 0); /* padding */
1136                 SBVAL(data,         72, file->id_full_directory_info.file_id);
1137                 trans2_append_data_string(req, trans, &file->id_full_directory_info.name, 
1138                                           ofs + 60, STR_TERMINATE_ASCII);
1139                 data = trans->out.data.data + ofs;
1140                 SIVAL(data,          0, trans->out.data.length - ofs);
1141                 break;
1142
1143         case RAW_SEARCH_ID_BOTH_DIRECTORY_INFO:
1144                 trans2_grow_data(req, trans, ofs + 104);
1145                 data = trans->out.data.data + ofs;
1146                 SIVAL(data,          4, file->id_both_directory_info.file_index);
1147                 push_nttime(data,    8, file->id_both_directory_info.create_time);
1148                 push_nttime(data,   16, file->id_both_directory_info.access_time);
1149                 push_nttime(data,   24, file->id_both_directory_info.write_time);
1150                 push_nttime(data,   32, file->id_both_directory_info.change_time);
1151                 SBVAL(data,         40, file->id_both_directory_info.size);
1152                 SBVAL(data,         48, file->id_both_directory_info.alloc_size);
1153                 SIVAL(data,         56, file->id_both_directory_info.attrib);
1154                 SIVAL(data,         64, file->id_both_directory_info.ea_size);
1155                 SCVAL(data,         69, 0); /* reserved */
1156                 memset(data+70,0,26);
1157                 trans2_push_data_string(req, trans, 
1158                                         68 + ofs, 70 + ofs, 
1159                                         &file->id_both_directory_info.short_name, 
1160                                         24, STR_UNICODE | STR_LEN8BIT);
1161                 SBVAL(data,         96, file->id_both_directory_info.file_id);
1162                 trans2_append_data_string(req, trans, &file->id_both_directory_info.name, 
1163                                           ofs + 60, STR_TERMINATE_ASCII);
1164                 data = trans->out.data.data + ofs;
1165                 SIVAL(data,          0, trans->out.data.length - ofs);
1166                 break;
1167         }
1168 }
1169
1170 /* callback function for trans2 findfirst/findnext */
1171 static BOOL find_callback(void *private, union smb_search_data *file)
1172 {
1173         struct find_state *state = (struct find_state *)private;
1174         struct smb_trans2 *trans = state->trans;
1175         uint_t old_length;
1176
1177         old_length = trans->out.data.length;
1178
1179         find_fill_info(state->req, trans, state, file);
1180
1181         /* see if we have gone beyond the user specified maximum */
1182         if (trans->out.data.length > trans->in.max_data) {
1183                 /* restore the old length and tell the backend to stop */
1184                 trans2_grow_data(state->req, trans, old_length);
1185                 return False;
1186         }
1187
1188         state->last_entry_offset = old_length;  
1189         return True;
1190 }
1191
1192
1193 /*
1194   trans2 findfirst implementation
1195 */
1196 static NTSTATUS trans2_findfirst(struct smbsrv_request *req, struct smb_trans2 *trans)
1197 {
1198         union smb_search_first search;
1199         NTSTATUS status;
1200         uint16_t level;
1201         uint8_t *param;
1202         struct find_state state;
1203
1204         /* make sure we got all the parameters */
1205         if (trans->in.params.length < 14) {
1206                 return NT_STATUS_FOOBAR;
1207         }
1208
1209         search.t2ffirst.in.search_attrib = SVAL(trans->in.params.data, 0);
1210         search.t2ffirst.in.max_count     = SVAL(trans->in.params.data, 2);
1211         search.t2ffirst.in.flags         = SVAL(trans->in.params.data, 4);
1212         level                            = SVAL(trans->in.params.data, 6);
1213         search.t2ffirst.in.storage_type  = IVAL(trans->in.params.data, 8);
1214
1215         trans2_pull_blob_string(req, &trans->in.params, 12, &search.t2ffirst.in.pattern, 0);
1216         if (search.t2ffirst.in.pattern == NULL) {
1217                 return NT_STATUS_FOOBAR;
1218         }
1219
1220         search.t2ffirst.level = (enum smb_search_level)level;
1221         if (search.t2ffirst.level >= RAW_SEARCH_GENERIC) {
1222                 return NT_STATUS_INVALID_LEVEL;
1223         }
1224
1225         /* setup the private state structure that the backend will give us in the callback */
1226         state.req = req;
1227         state.trans = trans;
1228         state.level = search.t2ffirst.level;
1229         state.last_entry_offset = 0;
1230         state.flags = search.t2ffirst.in.flags;
1231
1232         /* setup for just a header in the reply */
1233         trans2_setup_reply(req, trans, 10, 0, 0);
1234
1235         /* call the backend */
1236         status = ntvfs_search_first(req, &search, &state, find_callback);
1237         if (!NT_STATUS_IS_OK(status)) {
1238                 trans2_setup_reply(req, trans, 0, 0, 0);
1239                 return status;
1240         }
1241
1242         /* fill in the findfirst reply header */
1243         param = trans->out.params.data;
1244         SSVAL(param, VWV(0), search.t2ffirst.out.handle);
1245         SSVAL(param, VWV(1), search.t2ffirst.out.count);
1246         SSVAL(param, VWV(2), search.t2ffirst.out.end_of_search);
1247         SSVAL(param, VWV(3), 0);
1248         SSVAL(param, VWV(4), state.last_entry_offset);
1249
1250         return NT_STATUS_OK;
1251 }
1252
1253
1254 /*
1255   trans2 findnext implementation
1256 */
1257 static NTSTATUS trans2_findnext(struct smbsrv_request *req, struct smb_trans2 *trans)
1258 {
1259         union smb_search_next search;
1260         NTSTATUS status;
1261         uint16_t level;
1262         uint8_t *param;
1263         struct find_state state;
1264
1265         /* make sure we got all the parameters */
1266         if (trans->in.params.length < 12) {
1267                 return NT_STATUS_FOOBAR;
1268         }
1269
1270         search.t2fnext.in.handle        = SVAL(trans->in.params.data, 0);
1271         search.t2fnext.in.max_count     = SVAL(trans->in.params.data, 2);
1272         level                           = SVAL(trans->in.params.data, 4);
1273         search.t2fnext.in.resume_key    = IVAL(trans->in.params.data, 6);
1274         search.t2fnext.in.flags         = SVAL(trans->in.params.data, 10);
1275
1276         trans2_pull_blob_string(req, &trans->in.params, 12, &search.t2fnext.in.last_name, 0);
1277         if (search.t2fnext.in.last_name == NULL) {
1278                 return NT_STATUS_FOOBAR;
1279         }
1280
1281         search.t2fnext.level = (enum smb_search_level)level;
1282         if (search.t2fnext.level >= RAW_SEARCH_GENERIC) {
1283                 return NT_STATUS_INVALID_LEVEL;
1284         }
1285
1286         /* setup the private state structure that the backend will give us in the callback */
1287         state.req = req;
1288         state.trans = trans;
1289         state.level = search.t2fnext.level;
1290         state.last_entry_offset = 0;
1291         state.flags = search.t2fnext.in.flags;
1292
1293         /* setup for just a header in the reply */
1294         trans2_setup_reply(req, trans, 8, 0, 0);
1295
1296         /* call the backend */
1297         status = ntvfs_search_next(req, &search, &state, find_callback);
1298         if (!NT_STATUS_IS_OK(status)) {
1299                 return status;
1300         }
1301
1302         /* fill in the findfirst reply header */
1303         param = trans->out.params.data;
1304         SSVAL(param, VWV(0), search.t2fnext.out.count);
1305         SSVAL(param, VWV(1), search.t2fnext.out.end_of_search);
1306         SSVAL(param, VWV(2), 0);
1307         SSVAL(param, VWV(3), state.last_entry_offset);
1308         
1309         return NT_STATUS_OK;
1310 }
1311
1312
1313 /*
1314   backend for trans2 requests
1315 */
1316 static NTSTATUS trans2_backend(struct smbsrv_request *req, struct smb_trans2 *trans)
1317 {
1318         NTSTATUS status;
1319
1320         /* direct trans2 pass thru */
1321         status = ntvfs_trans2(req, trans);
1322         if (!NT_STATUS_EQUAL(NT_STATUS_NOT_IMPLEMENTED, status)) {
1323                 return status;
1324         }
1325
1326         /* must have at least one setup word */
1327         if (trans->in.setup_count < 1) {
1328                 return NT_STATUS_FOOBAR;
1329         }
1330
1331         /* the trans2 command is in setup[0] */
1332         switch (trans->in.setup[0]) {
1333         case TRANSACT2_FINDFIRST:
1334                 return trans2_findfirst(req, trans);
1335         case TRANSACT2_FINDNEXT:
1336                 return trans2_findnext(req, trans);
1337         case TRANSACT2_QPATHINFO:
1338                 return trans2_qpathinfo(req, trans);
1339         case TRANSACT2_QFILEINFO:
1340                 return trans2_qfileinfo(req, trans);
1341         case TRANSACT2_SETFILEINFO:
1342                 return trans2_setfileinfo(req, trans);
1343         case TRANSACT2_SETPATHINFO:
1344                 return trans2_setpathinfo(req, trans);
1345         case TRANSACT2_QFSINFO:
1346                 return trans2_qfsinfo(req, trans);
1347         case TRANSACT2_OPEN:
1348                 return trans2_open(req, trans);
1349         case TRANSACT2_MKDIR:
1350                 return trans2_mkdir(req, trans);
1351         }
1352
1353         /* an unknown trans2 command */
1354         return NT_STATUS_FOOBAR;
1355 }
1356
1357 /****************************************************************************
1358  Reply to an SMBtrans or SMBtrans2 request
1359 ****************************************************************************/
1360 void reply_trans_generic(struct smbsrv_request *req, uint8_t command)
1361 {
1362         struct smb_trans2 trans;
1363         int i;
1364         uint16_t param_ofs, data_ofs;
1365         uint16_t param_count, data_count;
1366         uint16_t params_left, data_left;
1367         uint16_t param_total, data_total;
1368         uint8_t *params, *data;
1369         NTSTATUS status;
1370
1371         /* parse request */
1372         if (req->in.wct < 14) {
1373                 req_reply_error(req, NT_STATUS_FOOBAR);
1374                 return;
1375         }
1376
1377         param_total          = SVAL(req->in.vwv, VWV(0));
1378         data_total           = SVAL(req->in.vwv, VWV(1));
1379         trans.in.max_param   = SVAL(req->in.vwv, VWV(2));
1380         trans.in.max_data    = SVAL(req->in.vwv, VWV(3));
1381         trans.in.max_setup   = CVAL(req->in.vwv, VWV(4));
1382         trans.in.flags       = SVAL(req->in.vwv, VWV(5));
1383         trans.in.timeout     = IVAL(req->in.vwv, VWV(6));
1384         param_count          = SVAL(req->in.vwv, VWV(9));
1385         param_ofs            = SVAL(req->in.vwv, VWV(10));
1386         data_count           = SVAL(req->in.vwv, VWV(11));
1387         data_ofs             = SVAL(req->in.vwv, VWV(12));
1388         trans.in.setup_count = CVAL(req->in.vwv, VWV(13));
1389
1390         if (req->in.wct != 14 + trans.in.setup_count) {
1391                 req_reply_dos_error(req, ERRSRV, ERRerror);
1392                 return;
1393         }
1394
1395         /* parse out the setup words */
1396         trans.in.setup = talloc_array_p(req, uint16_t, trans.in.setup_count);
1397         if (trans.in.setup_count && !trans.in.setup) {
1398                 req_reply_error(req, NT_STATUS_NO_MEMORY);
1399                 return;
1400         }
1401         for (i=0;i<trans.in.setup_count;i++) {
1402                 trans.in.setup[i] = SVAL(req->in.vwv, VWV(14+i));
1403         }
1404
1405         if (command == SMBtrans) {
1406                 req_pull_string(req, &trans.in.trans_name, req->in.data, -1, STR_TERMINATE);
1407         }
1408
1409         if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans.in.params) ||
1410             !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans.in.data)) {
1411                 req_reply_error(req, NT_STATUS_FOOBAR);
1412                 return;
1413         }
1414
1415         /* is it a partial request? if so, then send a 'send more' message */
1416         if (param_total > param_count ||
1417             data_total > data_count) {
1418                 DEBUG(0,("REWRITE: not handling partial trans requests!\n"));
1419                 return;
1420         }
1421
1422         /* its a full request, give it to the backend */
1423         if (command == SMBtrans) {
1424                 status = ntvfs_trans(req, &trans);
1425         } else {
1426                 status = trans2_backend(req, &trans);
1427         }
1428
1429         if (NT_STATUS_IS_ERR(status)) {
1430                 req_reply_error(req, status);
1431                 return;
1432         }
1433
1434         params_left = trans.out.params.length;
1435         data_left   = trans.out.data.length;
1436         params      = trans.out.params.data;
1437         data        = trans.out.data.data;
1438
1439
1440         req_setup_reply(req, 10 + trans.out.setup_count, 0);
1441
1442         if (!NT_STATUS_IS_OK(status)) {
1443                 req_setup_error(req, status);
1444         }
1445
1446         /* we need to divide up the reply into chunks that fit into
1447            the negotiated buffer size */
1448         do {
1449                 uint16_t this_data, this_param, max_bytes;
1450                 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
1451                 struct smbsrv_request *this_req;
1452
1453                 max_bytes = req_max_data(req) - (align1 + align2);
1454
1455                 this_param = params_left;
1456                 if (this_param > max_bytes) {
1457                         this_param = max_bytes;
1458                 }
1459                 max_bytes -= this_param;
1460
1461                 this_data = data_left;
1462                 if (this_data > max_bytes) {
1463                         this_data = max_bytes;
1464                 }
1465
1466                 /* don't destroy unless this is the last chunk */
1467                 if (params_left - this_param != 0 || 
1468                     data_left - this_data != 0) {
1469                         this_req = req_setup_secondary(req);
1470                 } else {
1471                         this_req = req;
1472                 }
1473
1474                 req_grow_data(this_req, this_param + this_data + (align1 + align2));
1475
1476                 SSVAL(this_req->out.vwv, VWV(0), trans.out.params.length);
1477                 SSVAL(this_req->out.vwv, VWV(1), trans.out.data.length);
1478                 SSVAL(this_req->out.vwv, VWV(2), 0);
1479
1480                 SSVAL(this_req->out.vwv, VWV(3), this_param);
1481                 SSVAL(this_req->out.vwv, VWV(4), align1 + PTR_DIFF(this_req->out.data, this_req->out.hdr));
1482                 SSVAL(this_req->out.vwv, VWV(5), PTR_DIFF(params, trans.out.params.data));
1483
1484                 SSVAL(this_req->out.vwv, VWV(6), this_data);
1485                 SSVAL(this_req->out.vwv, VWV(7), align1 + align2 + 
1486                       PTR_DIFF(this_req->out.data + this_param, this_req->out.hdr));
1487                 SSVAL(this_req->out.vwv, VWV(8), PTR_DIFF(data, trans.out.data.data));
1488
1489                 SSVAL(this_req->out.vwv, VWV(9), trans.out.setup_count);
1490                 for (i=0;i<trans.out.setup_count;i++) {
1491                         SSVAL(this_req->out.vwv, VWV(10+i), trans.out.setup[i]);
1492                 }
1493
1494                 memset(this_req->out.data, 0, align1);
1495                 if (this_param != 0) {
1496                         memcpy(this_req->out.data + align1, params, this_param);
1497                 }
1498                 memset(this_req->out.data+this_param+align1, 0, align2);
1499                 if (this_data != 0) {
1500                         memcpy(this_req->out.data+this_param+align1+align2, data, this_data);
1501                 }
1502
1503                 params_left -= this_param;
1504                 data_left -= this_data;
1505                 params += this_param;
1506                 data += this_data;
1507
1508                 req_send_reply(this_req);
1509         } while (params_left != 0 || data_left != 0);
1510 }
1511
1512
1513 /****************************************************************************
1514  Reply to an SMBtrans2
1515 ****************************************************************************/
1516 void reply_trans2(struct smbsrv_request *req)
1517 {
1518         reply_trans_generic(req, SMBtrans2);
1519 }
1520
1521 /****************************************************************************
1522  Reply to an SMBtrans
1523 ****************************************************************************/
1524 void reply_trans(struct smbsrv_request *req)
1525 {
1526         reply_trans_generic(req, SMBtrans);
1527 }
1528
1529 /****************************************************************************
1530  Reply to an SMBtranss2 request
1531 ****************************************************************************/
1532 void reply_transs2(struct smbsrv_request *req)
1533 {
1534         req_reply_error(req, NT_STATUS_FOOBAR);
1535 }
1536
1537