r4230: now that we set the FLAGS2_EXTENDED_ATTRIBUTES flag, we should mark
[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         uint32_t list_size;
516         
517         switch (st->generic.level) {
518         case RAW_FILEINFO_GENERIC:
519         case RAW_FILEINFO_GETATTR:
520         case RAW_FILEINFO_GETATTRE:
521         case RAW_FILEINFO_SEC_DESC:
522                 /* handled elsewhere */
523                 return NT_STATUS_INVALID_LEVEL;
524
525         case RAW_FILEINFO_BASIC_INFO:
526         case RAW_FILEINFO_BASIC_INFORMATION:
527                 trans2_setup_reply(req, trans, 2, 40, 0);
528
529                 SSVAL(trans->out.params.data, 0, 0);
530                 push_nttime(trans->out.data.data,  0, st->basic_info.out.create_time);
531                 push_nttime(trans->out.data.data,  8, st->basic_info.out.access_time);
532                 push_nttime(trans->out.data.data, 16, st->basic_info.out.write_time);
533                 push_nttime(trans->out.data.data, 24, st->basic_info.out.change_time);
534                 SIVAL(trans->out.data.data,       32, st->basic_info.out.attrib);
535                 SIVAL(trans->out.data.data,       36, 0); /* padding */
536                 return NT_STATUS_OK;
537
538         case RAW_FILEINFO_STANDARD:
539                 trans2_setup_reply(req, trans, 2, 22, 0);
540
541                 SSVAL(trans->out.params.data, 0, 0);
542                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 0, st->standard.out.create_time);
543                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 4, st->standard.out.access_time);
544                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 8, st->standard.out.write_time);
545                 SIVAL(trans->out.data.data,        12, st->standard.out.size);
546                 SIVAL(trans->out.data.data,        16, st->standard.out.alloc_size);
547                 SSVAL(trans->out.data.data,        20, st->standard.out.attrib);
548                 return NT_STATUS_OK;
549
550         case RAW_FILEINFO_EA_SIZE:
551                 trans2_setup_reply(req, trans, 2, 26, 0);
552
553                 SSVAL(trans->out.params.data, 0, 0);
554                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 0, st->ea_size.out.create_time);
555                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 4, st->ea_size.out.access_time);
556                 srv_push_dos_date2(req->smb_conn, trans->out.data.data, 8, st->ea_size.out.write_time);
557                 SIVAL(trans->out.data.data,        12, st->ea_size.out.size);
558                 SIVAL(trans->out.data.data,        16, st->ea_size.out.alloc_size);
559                 SSVAL(trans->out.data.data,        20, st->ea_size.out.attrib);
560                 SIVAL(trans->out.data.data,        22, st->ea_size.out.ea_size);
561                 return NT_STATUS_OK;
562
563         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
564                 trans2_setup_reply(req, trans, 2, 56, 0);
565
566                 SSVAL(trans->out.params.data, 0, 0);
567                 push_nttime(trans->out.data.data,  0, st->network_open_information.out.create_time);
568                 push_nttime(trans->out.data.data,  8, st->network_open_information.out.access_time);
569                 push_nttime(trans->out.data.data, 16, st->network_open_information.out.write_time);
570                 push_nttime(trans->out.data.data, 24, st->network_open_information.out.change_time);
571                 SBVAL(trans->out.data.data,       32, st->network_open_information.out.alloc_size);
572                 SBVAL(trans->out.data.data,       40, st->network_open_information.out.size);
573                 SIVAL(trans->out.data.data,       48, st->network_open_information.out.attrib);
574                 SIVAL(trans->out.data.data,       52, 0); /* padding */
575                 return NT_STATUS_OK;
576
577         case RAW_FILEINFO_STANDARD_INFO:
578         case RAW_FILEINFO_STANDARD_INFORMATION:
579                 trans2_setup_reply(req, trans, 2, 24, 0);
580                 SSVAL(trans->out.params.data, 0, 0);
581                 SBVAL(trans->out.data.data,  0, st->standard_info.out.alloc_size);
582                 SBVAL(trans->out.data.data,  8, st->standard_info.out.size);
583                 SIVAL(trans->out.data.data, 16, st->standard_info.out.nlink);
584                 SCVAL(trans->out.data.data, 20, st->standard_info.out.delete_pending);
585                 SCVAL(trans->out.data.data, 21, st->standard_info.out.directory);
586                 SSVAL(trans->out.data.data, 22, 0); /* padding */
587                 return NT_STATUS_OK;
588
589         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
590                 trans2_setup_reply(req, trans, 2, 8, 0);
591                 SSVAL(trans->out.params.data, 0, 0);
592                 SIVAL(trans->out.data.data,  0, st->attribute_tag_information.out.attrib);
593                 SIVAL(trans->out.data.data,  4, st->attribute_tag_information.out.reparse_tag);
594                 return NT_STATUS_OK;
595
596         case RAW_FILEINFO_EA_INFO:
597         case RAW_FILEINFO_EA_INFORMATION:
598                 trans2_setup_reply(req, trans, 2, 4, 0);
599                 SSVAL(trans->out.params.data, 0, 0);
600                 SIVAL(trans->out.data.data,  0, st->ea_info.out.ea_size);
601                 return NT_STATUS_OK;
602
603         case RAW_FILEINFO_MODE_INFORMATION:
604                 trans2_setup_reply(req, trans, 2, 4, 0);
605                 SSVAL(trans->out.params.data, 0, 0);
606                 SIVAL(trans->out.data.data,  0, st->mode_information.out.mode);
607                 return NT_STATUS_OK;
608
609         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
610                 trans2_setup_reply(req, trans, 2, 4, 0);
611                 SSVAL(trans->out.params.data, 0, 0);
612                 SIVAL(trans->out.data.data,  0, 
613                       st->alignment_information.out.alignment_requirement);
614                 return NT_STATUS_OK;
615
616         case RAW_FILEINFO_ALL_EAS:
617                 list_size = ea_list_size(st->all_eas.out.num_eas,
618                                                         st->all_eas.out.eas);
619                         trans2_setup_reply(req, trans, 2, list_size, 0);
620                         SSVAL(trans->out.params.data, 0, 0);
621                         ea_put_list(trans->out.data.data, 
622                                     st->all_eas.out.num_eas, st->all_eas.out.eas);
623                 return NT_STATUS_OK;
624
625         case RAW_FILEINFO_ACCESS_INFORMATION:
626                 trans2_setup_reply(req, trans, 2, 4, 0);
627                 SSVAL(trans->out.params.data, 0, 0);
628                 SIVAL(trans->out.data.data,  0, st->access_information.out.access_flags);
629                 return NT_STATUS_OK;
630
631         case RAW_FILEINFO_POSITION_INFORMATION:
632                 trans2_setup_reply(req, trans, 2, 8, 0);
633                 SSVAL(trans->out.params.data, 0, 0);
634                 SBVAL(trans->out.data.data,  0, st->position_information.out.position);
635                 return NT_STATUS_OK;
636
637         case RAW_FILEINFO_COMPRESSION_INFO:
638         case RAW_FILEINFO_COMPRESSION_INFORMATION:
639                 trans2_setup_reply(req, trans, 2, 16, 0);
640                 SSVAL(trans->out.params.data, 0, 0);
641                 SBVAL(trans->out.data.data,  0, st->compression_info.out.compressed_size);
642                 SSVAL(trans->out.data.data,  8, st->compression_info.out.format);
643                 SCVAL(trans->out.data.data, 10, st->compression_info.out.unit_shift);
644                 SCVAL(trans->out.data.data, 11, st->compression_info.out.chunk_shift);
645                 SCVAL(trans->out.data.data, 12, st->compression_info.out.cluster_shift);
646                 SSVAL(trans->out.data.data, 13, 0); /* 3 bytes padding */
647                 SCVAL(trans->out.data.data, 15, 0);
648                 return NT_STATUS_OK;
649
650         case RAW_FILEINFO_IS_NAME_VALID:
651                 trans2_setup_reply(req, trans, 2, 0, 0);
652                 SSVAL(trans->out.params.data, 0, 0);
653                 return NT_STATUS_OK;
654
655         case RAW_FILEINFO_INTERNAL_INFORMATION:
656                 trans2_setup_reply(req, trans, 2, 8, 0);
657                 SSVAL(trans->out.params.data, 0, 0);
658                 SBVAL(trans->out.data.data,  0, st->internal_information.out.file_id);
659                 return NT_STATUS_OK;
660
661         case RAW_FILEINFO_ALL_INFO:
662         case RAW_FILEINFO_ALL_INFORMATION:
663                 trans2_setup_reply(req, trans, 2, 72, 0);
664
665                 SSVAL(trans->out.params.data, 0, 0);
666                 push_nttime(trans->out.data.data,  0, st->all_info.out.create_time);
667                 push_nttime(trans->out.data.data,  8, st->all_info.out.access_time);
668                 push_nttime(trans->out.data.data, 16, st->all_info.out.write_time);
669                 push_nttime(trans->out.data.data, 24, st->all_info.out.change_time);
670                 SIVAL(trans->out.data.data,       32, st->all_info.out.attrib);
671                 SIVAL(trans->out.data.data,       36, 0);
672                 SBVAL(trans->out.data.data,       40, st->all_info.out.alloc_size);
673                 SBVAL(trans->out.data.data,       48, st->all_info.out.size);
674                 SIVAL(trans->out.data.data,       56, st->all_info.out.nlink);
675                 SCVAL(trans->out.data.data,       60, st->all_info.out.delete_pending);
676                 SCVAL(trans->out.data.data,       61, st->all_info.out.directory);
677                 SSVAL(trans->out.data.data,       62, 0); /* padding */
678                 SIVAL(trans->out.data.data,       64, st->all_info.out.ea_size);
679                 trans2_append_data_string(req, trans, &st->all_info.out.fname, 
680                                           68, STR_UNICODE);
681                 return NT_STATUS_OK;
682
683         case RAW_FILEINFO_NAME_INFO:
684         case RAW_FILEINFO_NAME_INFORMATION:
685                 trans2_setup_reply(req, trans, 2, 4, 0);
686                 SSVAL(trans->out.params.data, 0, 0);
687                 trans2_append_data_string(req, trans, &st->name_info.out.fname, 0, STR_UNICODE);
688                 return NT_STATUS_OK;
689
690         case RAW_FILEINFO_ALT_NAME_INFO:
691         case RAW_FILEINFO_ALT_NAME_INFORMATION:
692                 trans2_setup_reply(req, trans, 2, 4, 0);
693                 SSVAL(trans->out.params.data, 0, 0);
694                 trans2_append_data_string(req, trans, &st->alt_name_info.out.fname, 0, STR_UNICODE);
695                 return NT_STATUS_OK;
696
697         case RAW_FILEINFO_STREAM_INFO:
698         case RAW_FILEINFO_STREAM_INFORMATION:
699                 trans2_setup_reply(req, trans, 2, 0, 0);
700
701                 SSVAL(trans->out.params.data, 0, 0);
702
703                 for (i=0;i<st->stream_info.out.num_streams;i++) {
704                         uint16_t data_size = trans->out.data.length;
705                         uint8_t *data;
706
707                         trans2_grow_data(req, trans, data_size + 24);
708                         data = trans->out.data.data + data_size;
709                         SBVAL(data,  8, st->stream_info.out.streams[i].size);
710                         SBVAL(data, 16, st->stream_info.out.streams[i].alloc_size);
711                         trans2_append_data_string(req, trans, 
712                                                   &st->stream_info.out.streams[i].stream_name, 
713                                                   data_size + 4, STR_UNICODE);
714                         if (i == st->stream_info.out.num_streams - 1) {
715                                 SIVAL(trans->out.data.data, data_size, 0);
716                         } else {
717                                 trans2_grow_data_fill(req, trans, (trans->out.data.length+7)&~7);
718                                 SIVAL(trans->out.data.data, data_size, 
719                                       trans->out.data.length - data_size);
720                         }
721                 }
722                 return NT_STATUS_OK;
723         }
724
725         return NT_STATUS_INVALID_LEVEL;
726 }
727
728 /*
729   trans2 qpathinfo implementation
730 */
731 static NTSTATUS trans2_qpathinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
732 {
733         union smb_fileinfo st;
734         NTSTATUS status;
735         uint16_t level;
736
737         /* make sure we got enough parameters */
738         if (trans->in.params.length < 2) {
739                 return NT_STATUS_FOOBAR;
740         }
741
742         level = SVAL(trans->in.params.data, 0);
743
744         trans2_pull_blob_string(req, &trans->in.params, 6, &st.generic.in.fname, 0);
745         if (st.generic.in.fname == NULL) {
746                 return NT_STATUS_FOOBAR;
747         }
748
749         /* work out the backend level - we make it 1-1 in the header */
750         st.generic.level = (enum smb_fileinfo_level)level;
751         if (st.generic.level >= RAW_FILEINFO_GENERIC) {
752                 return NT_STATUS_INVALID_LEVEL;
753         }
754
755         /* call the backend */
756         status = ntvfs_qpathinfo(req, &st);
757         if (!NT_STATUS_IS_OK(status)) {
758                 return status;
759         }
760
761         /* fill in the reply parameters */
762         status = trans2_fileinfo_fill(req, trans, &st);
763
764         return status;
765 }
766
767
768 /*
769   trans2 qpathinfo implementation
770 */
771 static NTSTATUS trans2_qfileinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
772 {
773         union smb_fileinfo st;
774         NTSTATUS status;
775         uint16_t level;
776
777         /* make sure we got enough parameters */
778         if (trans->in.params.length < 4) {
779                 return NT_STATUS_FOOBAR;
780         }
781
782         st.generic.in.fnum  = SVAL(trans->in.params.data, 0);
783         level = SVAL(trans->in.params.data, 2);
784
785         /* work out the backend level - we make it 1-1 in the header */
786         st.generic.level = (enum smb_fileinfo_level)level;
787         if (st.generic.level >= RAW_FILEINFO_GENERIC) {
788                 return NT_STATUS_INVALID_LEVEL;
789         }
790
791         /* call the backend */
792         status = ntvfs_qfileinfo(req, &st);
793         if (!NT_STATUS_IS_OK(status)) {
794                 return status;
795         }
796
797         /* fill in the reply parameters */
798         status = trans2_fileinfo_fill(req, trans, &st);
799
800         return status;
801 }
802
803
804 /*
805   parse a trans2 setfileinfo/setpathinfo data blob
806 */
807 static NTSTATUS trans2_parse_sfileinfo(struct smbsrv_request *req,
808                                        union smb_setfileinfo *st,
809                                        const DATA_BLOB *blob)
810 {
811         uint32_t len;
812
813         switch (st->generic.level) {
814         case RAW_SFILEINFO_GENERIC:
815         case RAW_SFILEINFO_SETATTR:
816         case RAW_SFILEINFO_SETATTRE:
817         case RAW_SFILEINFO_SEC_DESC:
818                 /* handled elsewhere */
819                 return NT_STATUS_INVALID_LEVEL;
820
821         case RAW_SFILEINFO_STANDARD:
822                 CHECK_MIN_BLOB_SIZE(blob, 12);
823                 st->standard.in.create_time = srv_pull_dos_date2(req->smb_conn, blob->data + 0);
824                 st->standard.in.access_time = srv_pull_dos_date2(req->smb_conn, blob->data + 4);
825                 st->standard.in.write_time  = srv_pull_dos_date2(req->smb_conn, blob->data + 8);
826                 return NT_STATUS_OK;
827
828         case RAW_SFILEINFO_EA_SET:
829                 CHECK_MIN_BLOB_SIZE(blob, 4);
830                 len = IVAL(blob->data, 0);
831                 if (len > blob->length || len < 4) {
832                         return NT_STATUS_INFO_LENGTH_MISMATCH;
833                 }
834                 {
835                         DATA_BLOB blob2;
836                         blob2.data = blob->data+4;
837                         blob2.length = len-4;
838                         len = ea_pull_struct(&blob2, req, &st->ea_set.in.ea);
839                 }
840                 if (len == 0) {
841                         return NT_STATUS_INVALID_PARAMETER;
842                 }
843                 return NT_STATUS_OK;
844
845         case SMB_SFILEINFO_BASIC_INFO:
846         case SMB_SFILEINFO_BASIC_INFORMATION:
847                 CHECK_MIN_BLOB_SIZE(blob, 36);
848                 st->basic_info.in.create_time = pull_nttime(blob->data,  0);
849                 st->basic_info.in.access_time = pull_nttime(blob->data,  8);
850                 st->basic_info.in.write_time =  pull_nttime(blob->data, 16);
851                 st->basic_info.in.change_time = pull_nttime(blob->data, 24);
852                 st->basic_info.in.attrib =      IVAL(blob->data,        32);
853                 return NT_STATUS_OK;
854
855         case SMB_SFILEINFO_DISPOSITION_INFO:
856         case SMB_SFILEINFO_DISPOSITION_INFORMATION:
857                 CHECK_MIN_BLOB_SIZE(blob, 1);
858                 st->disposition_info.in.delete_on_close = CVAL(blob->data, 0);
859                 return NT_STATUS_OK;
860
861         case SMB_SFILEINFO_ALLOCATION_INFO:
862         case SMB_SFILEINFO_ALLOCATION_INFORMATION:
863                 CHECK_MIN_BLOB_SIZE(blob, 8);
864                 st->allocation_info.in.alloc_size = BVAL(blob->data, 0);
865                 return NT_STATUS_OK;                            
866
867         case RAW_SFILEINFO_END_OF_FILE_INFO:
868         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
869                 CHECK_MIN_BLOB_SIZE(blob, 8);
870                 st->end_of_file_info.in.size = BVAL(blob->data, 0);
871                 return NT_STATUS_OK;
872
873         case RAW_SFILEINFO_RENAME_INFORMATION: {
874                 DATA_BLOB blob2;
875
876                 CHECK_MIN_BLOB_SIZE(blob, 12);
877                 st->rename_information.in.overwrite = CVAL(blob->data, 0);
878                 st->rename_information.in.root_fid  = IVAL(blob->data, 4);
879                 len                                 = IVAL(blob->data, 8);
880                 blob2.data = blob->data+12;
881                 blob2.length = MIN(blob->length, len);
882                 trans2_pull_blob_string(req, &blob2, 0, 
883                                         &st->rename_information.in.new_name, STR_UNICODE);
884                 return NT_STATUS_OK;
885         }
886
887         case RAW_SFILEINFO_POSITION_INFORMATION:
888                 CHECK_MIN_BLOB_SIZE(blob, 8);
889                 st->position_information.in.position = BVAL(blob->data, 0);
890                 return NT_STATUS_OK;
891
892         case RAW_SFILEINFO_MODE_INFORMATION:
893                 CHECK_MIN_BLOB_SIZE(blob, 4);
894                 st->mode_information.in.mode = IVAL(blob->data, 0);
895                 return NT_STATUS_OK;
896         }
897
898         return NT_STATUS_INVALID_LEVEL;
899 }
900
901 /*
902   trans2 setfileinfo implementation
903 */
904 static NTSTATUS trans2_setfileinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
905 {
906         union smb_setfileinfo st;
907         NTSTATUS status;
908         uint16_t level, fnum;
909         DATA_BLOB *blob;
910
911         /* make sure we got enough parameters */
912         if (trans->in.params.length < 4) {
913                 return NT_STATUS_FOOBAR;
914         }
915
916         fnum  = SVAL(trans->in.params.data, 0);
917         level = SVAL(trans->in.params.data, 2);
918
919         blob = &trans->in.data;
920
921         st.generic.file.fnum = fnum;
922         st.generic.level = (enum smb_setfileinfo_level)level;
923
924         status = trans2_parse_sfileinfo(req, &st, blob);
925         if (!NT_STATUS_IS_OK(status)) {
926                 return status;
927         }
928
929         status = ntvfs_setfileinfo(req, &st);
930         if (!NT_STATUS_IS_OK(status)) {
931                 return status;
932         }
933
934         trans2_setup_reply(req, trans, 2, 0, 0);
935         SSVAL(trans->out.params.data, 0, 0);
936         return NT_STATUS_OK;
937 }
938
939 /*
940   trans2 setpathinfo implementation
941 */
942 static NTSTATUS trans2_setpathinfo(struct smbsrv_request *req, struct smb_trans2 *trans)
943 {
944         union smb_setfileinfo st;
945         NTSTATUS status;
946         uint16_t level;
947         DATA_BLOB *blob;
948
949         /* make sure we got enough parameters */
950         if (trans->in.params.length < 4) {
951                 return NT_STATUS_FOOBAR;
952         }
953
954         level = SVAL(trans->in.params.data, 0);
955         blob = &trans->in.data;
956         st.generic.level = (enum smb_setfileinfo_level)level;
957
958         trans2_pull_blob_string(req, &trans->in.params, 6, &st.generic.file.fname, 0);
959         if (st.generic.file.fname == NULL) {
960                 return NT_STATUS_FOOBAR;
961         }
962
963         status = trans2_parse_sfileinfo(req, &st, blob);
964         if (!NT_STATUS_IS_OK(status)) {
965                 return status;
966         }
967
968         status = ntvfs_setpathinfo(req, &st);
969         if (!NT_STATUS_IS_OK(status)) {
970                 return status;
971         }
972
973         trans2_setup_reply(req, trans, 2, 0, 0);
974         SSVAL(trans->out.params.data, 0, 0);
975         return NT_STATUS_OK;
976 }
977
978
979 /* a structure to encapsulate the state information about an in-progress ffirst/fnext operation */
980 struct find_state {
981         struct smbsrv_request *req;
982         struct smb_trans2 *trans;
983         enum smb_search_level level;
984         uint16_t last_entry_offset;
985         uint16_t flags;
986 };
987
988 /*
989   fill a single entry in a trans2 find reply 
990 */
991 static void find_fill_info(struct smbsrv_request *req,
992                            struct smb_trans2 *trans, 
993                            struct find_state *state,
994                            union smb_search_data *file)
995 {
996         uint8_t *data;
997         uint_t ofs = trans->out.data.length;
998
999         switch (state->level) {
1000         case RAW_SEARCH_SEARCH:
1001         case RAW_SEARCH_FFIRST:
1002         case RAW_SEARCH_FUNIQUE:
1003         case RAW_SEARCH_GENERIC:
1004                 /* handled elsewhere */
1005                 break;
1006
1007         case RAW_SEARCH_STANDARD:
1008                 if (state->flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) {
1009                         trans2_grow_data(req, trans, ofs + 27);
1010                         SIVAL(trans->out.data.data, ofs, file->standard.resume_key);
1011                         ofs += 4;
1012                 } else {
1013                         trans2_grow_data(req, trans, ofs + 23);
1014                 }
1015                 data = trans->out.data.data + ofs;
1016                 srv_push_dos_date2(req->smb_conn, data, 0, file->standard.create_time);
1017                 srv_push_dos_date2(req->smb_conn, data, 4, file->standard.access_time);
1018                 srv_push_dos_date2(req->smb_conn, data, 8, file->standard.write_time);
1019                 SIVAL(data, 12, file->standard.size);
1020                 SIVAL(data, 16, file->standard.alloc_size);
1021                 SSVAL(data, 20, file->standard.attrib);
1022                 trans2_append_data_string(req, trans, &file->standard.name, 
1023                                           ofs + 22, STR_LEN8BIT | STR_TERMINATE | STR_LEN_NOTERM);
1024                 break;
1025
1026         case RAW_SEARCH_EA_SIZE:
1027                 if (state->flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) {
1028                         trans2_grow_data(req, trans, ofs + 31);
1029                         SIVAL(trans->out.data.data, ofs, file->ea_size.resume_key);
1030                         ofs += 4;
1031                 } else {
1032                         trans2_grow_data(req, trans, ofs + 27);
1033                 }
1034                 data = trans->out.data.data + ofs;
1035                 srv_push_dos_date2(req->smb_conn, data, 0, file->ea_size.create_time);
1036                 srv_push_dos_date2(req->smb_conn, data, 4, file->ea_size.access_time);
1037                 srv_push_dos_date2(req->smb_conn, data, 8, file->ea_size.write_time);
1038                 SIVAL(data, 12, file->ea_size.size);
1039                 SIVAL(data, 16, file->ea_size.alloc_size);
1040                 SSVAL(data, 20, file->ea_size.attrib);
1041                 SIVAL(data, 22, file->ea_size.ea_size);
1042                 trans2_append_data_string(req, trans, &file->ea_size.name, 
1043                                           ofs + 26, STR_LEN8BIT | STR_NOALIGN);
1044                 trans2_grow_data(req, trans, trans->out.data.length + 1);
1045                 trans->out.data.data[trans->out.data.length-1] = 0;
1046                 break;
1047
1048         case RAW_SEARCH_DIRECTORY_INFO:
1049                 trans2_grow_data(req, trans, ofs + 64);
1050                 data = trans->out.data.data + ofs;
1051                 SIVAL(data,          4, file->directory_info.file_index);
1052                 push_nttime(data,    8, file->directory_info.create_time);
1053                 push_nttime(data,   16, file->directory_info.access_time);
1054                 push_nttime(data,   24, file->directory_info.write_time);
1055                 push_nttime(data,   32, file->directory_info.change_time);
1056                 SBVAL(data,         40, file->directory_info.size);
1057                 SBVAL(data,         48, file->directory_info.alloc_size);
1058                 SIVAL(data,         56, file->directory_info.attrib);
1059                 trans2_append_data_string(req, trans, &file->directory_info.name, 
1060                                           ofs + 60, STR_TERMINATE_ASCII);
1061                 data = trans->out.data.data + ofs;
1062                 SIVAL(data,          0, trans->out.data.length - ofs);
1063                 break;
1064
1065         case RAW_SEARCH_FULL_DIRECTORY_INFO:
1066                 trans2_grow_data(req, trans, ofs + 68);
1067                 data = trans->out.data.data + ofs;
1068                 SIVAL(data,          4, file->full_directory_info.file_index);
1069                 push_nttime(data,    8, file->full_directory_info.create_time);
1070                 push_nttime(data,   16, file->full_directory_info.access_time);
1071                 push_nttime(data,   24, file->full_directory_info.write_time);
1072                 push_nttime(data,   32, file->full_directory_info.change_time);
1073                 SBVAL(data,         40, file->full_directory_info.size);
1074                 SBVAL(data,         48, file->full_directory_info.alloc_size);
1075                 SIVAL(data,         56, file->full_directory_info.attrib);
1076                 SIVAL(data,         64, file->full_directory_info.ea_size);
1077                 trans2_append_data_string(req, trans, &file->full_directory_info.name, 
1078                                           ofs + 60, STR_TERMINATE_ASCII);
1079                 data = trans->out.data.data + ofs;
1080                 SIVAL(data,          0, trans->out.data.length - ofs);
1081                 break;
1082
1083         case RAW_SEARCH_NAME_INFO:
1084                 trans2_grow_data(req, trans, ofs + 12);
1085                 data = trans->out.data.data + ofs;
1086                 SIVAL(data,          4, file->name_info.file_index);
1087                 trans2_append_data_string(req, trans, &file->name_info.name, 
1088                                           ofs + 8, STR_TERMINATE_ASCII);
1089                 data = trans->out.data.data + ofs;
1090                 SIVAL(data,          0, trans->out.data.length - ofs);
1091                 break;
1092
1093         case RAW_SEARCH_BOTH_DIRECTORY_INFO:
1094                 trans2_grow_data(req, trans, ofs + 94);
1095                 data = trans->out.data.data + ofs;
1096                 SIVAL(data,          4, file->both_directory_info.file_index);
1097                 push_nttime(data,    8, file->both_directory_info.create_time);
1098                 push_nttime(data,   16, file->both_directory_info.access_time);
1099                 push_nttime(data,   24, file->both_directory_info.write_time);
1100                 push_nttime(data,   32, file->both_directory_info.change_time);
1101                 SBVAL(data,         40, file->both_directory_info.size);
1102                 SBVAL(data,         48, file->both_directory_info.alloc_size);
1103                 SIVAL(data,         56, file->both_directory_info.attrib);
1104                 SIVAL(data,         64, file->both_directory_info.ea_size);
1105                 SCVAL(data,         69, 0); /* reserved */
1106                 memset(data+70,0,24);
1107                 trans2_push_data_string(req, trans, 
1108                                         68 + ofs, 70 + ofs, 
1109                                         &file->both_directory_info.short_name, 
1110                                         24, STR_UNICODE | STR_LEN8BIT);
1111                 trans2_append_data_string(req, trans, &file->both_directory_info.name, 
1112                                           ofs + 60, STR_TERMINATE_ASCII);
1113                 trans2_align_data(req, trans);
1114                 data = trans->out.data.data + ofs;
1115                 SIVAL(data,          0, trans->out.data.length - ofs);
1116                 break;
1117
1118         case RAW_SEARCH_ID_FULL_DIRECTORY_INFO:
1119                 trans2_grow_data(req, trans, ofs + 80);
1120                 data = trans->out.data.data + ofs;
1121                 SIVAL(data,          4, file->id_full_directory_info.file_index);
1122                 push_nttime(data,    8, file->id_full_directory_info.create_time);
1123                 push_nttime(data,   16, file->id_full_directory_info.access_time);
1124                 push_nttime(data,   24, file->id_full_directory_info.write_time);
1125                 push_nttime(data,   32, file->id_full_directory_info.change_time);
1126                 SBVAL(data,         40, file->id_full_directory_info.size);
1127                 SBVAL(data,         48, file->id_full_directory_info.alloc_size);
1128                 SIVAL(data,         56, file->id_full_directory_info.attrib);
1129                 SIVAL(data,         64, file->id_full_directory_info.ea_size);
1130                 SIVAL(data,         68, 0); /* padding */
1131                 SBVAL(data,         72, file->id_full_directory_info.file_id);
1132                 trans2_append_data_string(req, trans, &file->id_full_directory_info.name, 
1133                                           ofs + 60, STR_TERMINATE_ASCII);
1134                 data = trans->out.data.data + ofs;
1135                 SIVAL(data,          0, trans->out.data.length - ofs);
1136                 break;
1137
1138         case RAW_SEARCH_ID_BOTH_DIRECTORY_INFO:
1139                 trans2_grow_data(req, trans, ofs + 104);
1140                 data = trans->out.data.data + ofs;
1141                 SIVAL(data,          4, file->id_both_directory_info.file_index);
1142                 push_nttime(data,    8, file->id_both_directory_info.create_time);
1143                 push_nttime(data,   16, file->id_both_directory_info.access_time);
1144                 push_nttime(data,   24, file->id_both_directory_info.write_time);
1145                 push_nttime(data,   32, file->id_both_directory_info.change_time);
1146                 SBVAL(data,         40, file->id_both_directory_info.size);
1147                 SBVAL(data,         48, file->id_both_directory_info.alloc_size);
1148                 SIVAL(data,         56, file->id_both_directory_info.attrib);
1149                 SIVAL(data,         64, file->id_both_directory_info.ea_size);
1150                 SCVAL(data,         69, 0); /* reserved */
1151                 memset(data+70,0,26);
1152                 trans2_push_data_string(req, trans, 
1153                                         68 + ofs, 70 + ofs, 
1154                                         &file->id_both_directory_info.short_name, 
1155                                         24, STR_UNICODE | STR_LEN8BIT);
1156                 SBVAL(data,         96, file->id_both_directory_info.file_id);
1157                 trans2_append_data_string(req, trans, &file->id_both_directory_info.name, 
1158                                           ofs + 60, STR_TERMINATE_ASCII);
1159                 data = trans->out.data.data + ofs;
1160                 SIVAL(data,          0, trans->out.data.length - ofs);
1161                 break;
1162         }
1163 }
1164
1165 /* callback function for trans2 findfirst/findnext */
1166 static BOOL find_callback(void *private, union smb_search_data *file)
1167 {
1168         struct find_state *state = (struct find_state *)private;
1169         struct smb_trans2 *trans = state->trans;
1170         uint_t old_length;
1171
1172         old_length = trans->out.data.length;
1173
1174         find_fill_info(state->req, trans, state, file);
1175
1176         /* see if we have gone beyond the user specified maximum */
1177         if (trans->out.data.length > trans->in.max_data) {
1178                 /* restore the old length and tell the backend to stop */
1179                 trans2_grow_data(state->req, trans, old_length);
1180                 return False;
1181         }
1182
1183         state->last_entry_offset = old_length;  
1184         return True;
1185 }
1186
1187
1188 /*
1189   trans2 findfirst implementation
1190 */
1191 static NTSTATUS trans2_findfirst(struct smbsrv_request *req, struct smb_trans2 *trans)
1192 {
1193         union smb_search_first search;
1194         NTSTATUS status;
1195         uint16_t level;
1196         uint8_t *param;
1197         struct find_state state;
1198
1199         /* make sure we got all the parameters */
1200         if (trans->in.params.length < 14) {
1201                 return NT_STATUS_FOOBAR;
1202         }
1203
1204         search.t2ffirst.in.search_attrib = SVAL(trans->in.params.data, 0);
1205         search.t2ffirst.in.max_count     = SVAL(trans->in.params.data, 2);
1206         search.t2ffirst.in.flags         = SVAL(trans->in.params.data, 4);
1207         level                            = SVAL(trans->in.params.data, 6);
1208         search.t2ffirst.in.storage_type  = IVAL(trans->in.params.data, 8);
1209
1210         trans2_pull_blob_string(req, &trans->in.params, 12, &search.t2ffirst.in.pattern, 0);
1211         if (search.t2ffirst.in.pattern == NULL) {
1212                 return NT_STATUS_FOOBAR;
1213         }
1214
1215         search.t2ffirst.level = (enum smb_search_level)level;
1216         if (search.t2ffirst.level >= RAW_SEARCH_GENERIC) {
1217                 return NT_STATUS_INVALID_LEVEL;
1218         }
1219
1220         /* setup the private state structure that the backend will give us in the callback */
1221         state.req = req;
1222         state.trans = trans;
1223         state.level = search.t2ffirst.level;
1224         state.last_entry_offset = 0;
1225         state.flags = search.t2ffirst.in.flags;
1226
1227         /* setup for just a header in the reply */
1228         trans2_setup_reply(req, trans, 10, 0, 0);
1229
1230         /* call the backend */
1231         status = ntvfs_search_first(req, &search, &state, find_callback);
1232         if (!NT_STATUS_IS_OK(status)) {
1233                 trans2_setup_reply(req, trans, 0, 0, 0);
1234                 return status;
1235         }
1236
1237         /* fill in the findfirst reply header */
1238         param = trans->out.params.data;
1239         SSVAL(param, VWV(0), search.t2ffirst.out.handle);
1240         SSVAL(param, VWV(1), search.t2ffirst.out.count);
1241         SSVAL(param, VWV(2), search.t2ffirst.out.end_of_search);
1242         SSVAL(param, VWV(3), 0);
1243         SSVAL(param, VWV(4), state.last_entry_offset);
1244
1245         return NT_STATUS_OK;
1246 }
1247
1248
1249 /*
1250   trans2 findnext implementation
1251 */
1252 static NTSTATUS trans2_findnext(struct smbsrv_request *req, struct smb_trans2 *trans)
1253 {
1254         union smb_search_next search;
1255         NTSTATUS status;
1256         uint16_t level;
1257         uint8_t *param;
1258         struct find_state state;
1259
1260         /* make sure we got all the parameters */
1261         if (trans->in.params.length < 12) {
1262                 return NT_STATUS_FOOBAR;
1263         }
1264
1265         search.t2fnext.in.handle        = SVAL(trans->in.params.data, 0);
1266         search.t2fnext.in.max_count     = SVAL(trans->in.params.data, 2);
1267         level                           = SVAL(trans->in.params.data, 4);
1268         search.t2fnext.in.resume_key    = IVAL(trans->in.params.data, 6);
1269         search.t2fnext.in.flags         = SVAL(trans->in.params.data, 10);
1270
1271         trans2_pull_blob_string(req, &trans->in.params, 12, &search.t2fnext.in.last_name, 0);
1272         if (search.t2fnext.in.last_name == NULL) {
1273                 return NT_STATUS_FOOBAR;
1274         }
1275
1276         search.t2fnext.level = (enum smb_search_level)level;
1277         if (search.t2fnext.level >= RAW_SEARCH_GENERIC) {
1278                 return NT_STATUS_INVALID_LEVEL;
1279         }
1280
1281         /* setup the private state structure that the backend will give us in the callback */
1282         state.req = req;
1283         state.trans = trans;
1284         state.level = search.t2fnext.level;
1285         state.last_entry_offset = 0;
1286         state.flags = search.t2fnext.in.flags;
1287
1288         /* setup for just a header in the reply */
1289         trans2_setup_reply(req, trans, 8, 0, 0);
1290
1291         /* call the backend */
1292         status = ntvfs_search_next(req, &search, &state, find_callback);
1293         if (!NT_STATUS_IS_OK(status)) {
1294                 return status;
1295         }
1296
1297         /* fill in the findfirst reply header */
1298         param = trans->out.params.data;
1299         SSVAL(param, VWV(0), search.t2fnext.out.count);
1300         SSVAL(param, VWV(1), search.t2fnext.out.end_of_search);
1301         SSVAL(param, VWV(2), 0);
1302         SSVAL(param, VWV(3), state.last_entry_offset);
1303         
1304         return NT_STATUS_OK;
1305 }
1306
1307
1308 /*
1309   backend for trans2 requests
1310 */
1311 static NTSTATUS trans2_backend(struct smbsrv_request *req, struct smb_trans2 *trans)
1312 {
1313         NTSTATUS status;
1314
1315         /* direct trans2 pass thru */
1316         status = ntvfs_trans2(req, trans);
1317         if (!NT_STATUS_EQUAL(NT_STATUS_NOT_IMPLEMENTED, status)) {
1318                 return status;
1319         }
1320
1321         /* must have at least one setup word */
1322         if (trans->in.setup_count < 1) {
1323                 return NT_STATUS_FOOBAR;
1324         }
1325
1326         /* the trans2 command is in setup[0] */
1327         switch (trans->in.setup[0]) {
1328         case TRANSACT2_FINDFIRST:
1329                 return trans2_findfirst(req, trans);
1330         case TRANSACT2_FINDNEXT:
1331                 return trans2_findnext(req, trans);
1332         case TRANSACT2_QPATHINFO:
1333                 return trans2_qpathinfo(req, trans);
1334         case TRANSACT2_QFILEINFO:
1335                 return trans2_qfileinfo(req, trans);
1336         case TRANSACT2_SETFILEINFO:
1337                 return trans2_setfileinfo(req, trans);
1338         case TRANSACT2_SETPATHINFO:
1339                 return trans2_setpathinfo(req, trans);
1340         case TRANSACT2_QFSINFO:
1341                 return trans2_qfsinfo(req, trans);
1342         case TRANSACT2_OPEN:
1343                 return trans2_open(req, trans);
1344         case TRANSACT2_MKDIR:
1345                 return trans2_mkdir(req, trans);
1346         }
1347
1348         /* an unknown trans2 command */
1349         return NT_STATUS_FOOBAR;
1350 }
1351
1352 /****************************************************************************
1353  Reply to an SMBtrans or SMBtrans2 request
1354 ****************************************************************************/
1355 void reply_trans_generic(struct smbsrv_request *req, uint8_t command)
1356 {
1357         struct smb_trans2 trans;
1358         int i;
1359         uint16_t param_ofs, data_ofs;
1360         uint16_t param_count, data_count;
1361         uint16_t params_left, data_left;
1362         uint16_t param_total, data_total;
1363         uint8_t *params, *data;
1364         NTSTATUS status;
1365
1366         /* parse request */
1367         if (req->in.wct < 14) {
1368                 req_reply_error(req, NT_STATUS_FOOBAR);
1369                 return;
1370         }
1371
1372         param_total          = SVAL(req->in.vwv, VWV(0));
1373         data_total           = SVAL(req->in.vwv, VWV(1));
1374         trans.in.max_param   = SVAL(req->in.vwv, VWV(2));
1375         trans.in.max_data    = SVAL(req->in.vwv, VWV(3));
1376         trans.in.max_setup   = CVAL(req->in.vwv, VWV(4));
1377         trans.in.flags       = SVAL(req->in.vwv, VWV(5));
1378         trans.in.timeout     = IVAL(req->in.vwv, VWV(6));
1379         param_count          = SVAL(req->in.vwv, VWV(9));
1380         param_ofs            = SVAL(req->in.vwv, VWV(10));
1381         data_count           = SVAL(req->in.vwv, VWV(11));
1382         data_ofs             = SVAL(req->in.vwv, VWV(12));
1383         trans.in.setup_count = CVAL(req->in.vwv, VWV(13));
1384
1385         if (req->in.wct != 14 + trans.in.setup_count) {
1386                 req_reply_dos_error(req, ERRSRV, ERRerror);
1387                 return;
1388         }
1389
1390         /* parse out the setup words */
1391         trans.in.setup = talloc_array_p(req, uint16_t, trans.in.setup_count);
1392         if (trans.in.setup_count && !trans.in.setup) {
1393                 req_reply_error(req, NT_STATUS_NO_MEMORY);
1394                 return;
1395         }
1396         for (i=0;i<trans.in.setup_count;i++) {
1397                 trans.in.setup[i] = SVAL(req->in.vwv, VWV(14+i));
1398         }
1399
1400         if (command == SMBtrans) {
1401                 req_pull_string(req, &trans.in.trans_name, req->in.data, -1, STR_TERMINATE);
1402         }
1403
1404         if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans.in.params) ||
1405             !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans.in.data)) {
1406                 req_reply_error(req, NT_STATUS_FOOBAR);
1407                 return;
1408         }
1409
1410         /* is it a partial request? if so, then send a 'send more' message */
1411         if (param_total > param_count ||
1412             data_total > data_count) {
1413                 DEBUG(0,("REWRITE: not handling partial trans requests!\n"));
1414                 return;
1415         }
1416
1417         /* its a full request, give it to the backend */
1418         if (command == SMBtrans) {
1419                 status = ntvfs_trans(req, &trans);
1420         } else {
1421                 status = trans2_backend(req, &trans);
1422         }
1423
1424         if (NT_STATUS_IS_ERR(status)) {
1425                 req_reply_error(req, status);
1426                 return;
1427         }
1428
1429         params_left = trans.out.params.length;
1430         data_left   = trans.out.data.length;
1431         params      = trans.out.params.data;
1432         data        = trans.out.data.data;
1433
1434
1435         req_setup_reply(req, 10 + trans.out.setup_count, 0);
1436
1437         if (!NT_STATUS_IS_OK(status)) {
1438                 req_setup_error(req, status);
1439         }
1440
1441         /* we need to divide up the reply into chunks that fit into
1442            the negotiated buffer size */
1443         do {
1444                 uint16_t this_data, this_param, max_bytes;
1445                 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
1446                 struct smbsrv_request *this_req;
1447
1448                 max_bytes = req_max_data(req) - (align1 + align2);
1449
1450                 this_param = params_left;
1451                 if (this_param > max_bytes) {
1452                         this_param = max_bytes;
1453                 }
1454                 max_bytes -= this_param;
1455
1456                 this_data = data_left;
1457                 if (this_data > max_bytes) {
1458                         this_data = max_bytes;
1459                 }
1460
1461                 /* don't destroy unless this is the last chunk */
1462                 if (params_left - this_param != 0 || 
1463                     data_left - this_data != 0) {
1464                         this_req = req_setup_secondary(req);
1465                 } else {
1466                         this_req = req;
1467                 }
1468
1469                 req_grow_data(this_req, this_param + this_data + (align1 + align2));
1470
1471                 SSVAL(this_req->out.vwv, VWV(0), trans.out.params.length);
1472                 SSVAL(this_req->out.vwv, VWV(1), trans.out.data.length);
1473                 SSVAL(this_req->out.vwv, VWV(2), 0);
1474
1475                 SSVAL(this_req->out.vwv, VWV(3), this_param);
1476                 SSVAL(this_req->out.vwv, VWV(4), align1 + PTR_DIFF(this_req->out.data, this_req->out.hdr));
1477                 SSVAL(this_req->out.vwv, VWV(5), PTR_DIFF(params, trans.out.params.data));
1478
1479                 SSVAL(this_req->out.vwv, VWV(6), this_data);
1480                 SSVAL(this_req->out.vwv, VWV(7), align1 + align2 + 
1481                       PTR_DIFF(this_req->out.data + this_param, this_req->out.hdr));
1482                 SSVAL(this_req->out.vwv, VWV(8), PTR_DIFF(data, trans.out.data.data));
1483
1484                 SSVAL(this_req->out.vwv, VWV(9), trans.out.setup_count);
1485                 for (i=0;i<trans.out.setup_count;i++) {
1486                         SSVAL(this_req->out.vwv, VWV(10+i), trans.out.setup[i]);
1487                 }
1488
1489                 memset(this_req->out.data, 0, align1);
1490                 if (this_param != 0) {
1491                         memcpy(this_req->out.data + align1, params, this_param);
1492                 }
1493                 memset(this_req->out.data+this_param+align1, 0, align2);
1494                 if (this_data != 0) {
1495                         memcpy(this_req->out.data+this_param+align1+align2, data, this_data);
1496                 }
1497
1498                 params_left -= this_param;
1499                 data_left -= this_data;
1500                 params += this_param;
1501                 data += this_data;
1502
1503                 req_send_reply(this_req);
1504         } while (params_left != 0 || data_left != 0);
1505 }
1506
1507
1508 /****************************************************************************
1509  Reply to an SMBtrans2
1510 ****************************************************************************/
1511 void reply_trans2(struct smbsrv_request *req)
1512 {
1513         reply_trans_generic(req, SMBtrans2);
1514 }
1515
1516 /****************************************************************************
1517  Reply to an SMBtrans
1518 ****************************************************************************/
1519 void reply_trans(struct smbsrv_request *req)
1520 {
1521         reply_trans_generic(req, SMBtrans);
1522 }
1523
1524 /****************************************************************************
1525  Reply to an SMBtranss2 request
1526 ****************************************************************************/
1527 void reply_transs2(struct smbsrv_request *req)
1528 {
1529         req_reply_error(req, NT_STATUS_FOOBAR);
1530 }
1531
1532