r12608: Remove some unused #include lines.
[kamenim/samba.git] / source4 / smb_server / smb / nttrans.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NT transaction handling
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) James J Myers 2003 <myersjj@samba.org>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 /*
22    This file handles the parsing of transact2 requests
23 */
24
25 #include "includes.h"
26 #include "smb_server/smb_server.h"
27 #include "ntvfs/ntvfs.h"
28 #include "libcli/raw/libcliraw.h"
29
30
31
32 #define CHECK_MIN_BLOB_SIZE(blob, size) do { \
33         if ((blob)->length < (size)) { \
34                 return NT_STATUS_INFO_LENGTH_MISMATCH; \
35         }} while (0)
36
37
38 /* setup a nttrans reply, given the data and params sizes */
39 static void nttrans_setup_reply(struct smbsrv_request *req, 
40                                struct smb_nttrans *trans,
41                                uint16_t param_size, uint16_t data_size,
42                                uint16_t setup_count)
43 {
44         trans->out.setup_count = setup_count;
45         if (setup_count != 0) {
46                 trans->out.setup = talloc_zero_array(req, uint16_t, setup_count);
47         }
48         trans->out.params = data_blob_talloc(req, NULL, param_size);
49         trans->out.data = data_blob_talloc(req, NULL, data_size);
50 }
51
52
53 /* 
54    parse NTTRANS_CREATE request
55  */
56 static NTSTATUS nttrans_create(struct smbsrv_request *req, 
57                                struct smb_nttrans *trans)
58 {
59         union smb_open *io;
60         uint16_t fname_len;
61         uint32_t sd_length, ea_length;
62         NTSTATUS status;
63         uint8_t *params;
64
65         if (trans->in.params.length < 54) {
66                 return NT_STATUS_INVALID_PARAMETER;
67         }
68
69         /* parse the request */
70         io = talloc(req, union smb_open);
71         if (io == NULL) {
72                 return NT_STATUS_NO_MEMORY;
73         }
74
75         io->ntcreatex.level = RAW_OPEN_NTTRANS_CREATE;
76
77         params = trans->in.params.data;
78
79         io->ntcreatex.in.flags            = IVAL(params,  0);
80         io->ntcreatex.in.root_fid         = IVAL(params,  4);
81         io->ntcreatex.in.access_mask      = IVAL(params,  8);
82         io->ntcreatex.in.alloc_size       = BVAL(params, 12);
83         io->ntcreatex.in.file_attr        = IVAL(params, 20);
84         io->ntcreatex.in.share_access     = IVAL(params, 24);
85         io->ntcreatex.in.open_disposition = IVAL(params, 28);
86         io->ntcreatex.in.create_options   = IVAL(params, 32);
87         sd_length                         = IVAL(params, 36);
88         ea_length                         = IVAL(params, 40);
89         fname_len                         = IVAL(params, 44);
90         io->ntcreatex.in.impersonation    = IVAL(params, 48);
91         io->ntcreatex.in.security_flags   = CVAL(params, 52);
92         io->ntcreatex.in.sec_desc         = NULL;
93         io->ntcreatex.in.ea_list          = NULL;
94
95         req_pull_string(req, &io->ntcreatex.in.fname, 
96                         params + 54, 
97                         trans->in.params.length - 54,
98                         STR_NO_RANGE_CHECK | STR_TERMINATE);
99         if (!io->ntcreatex.in.fname) {
100                 return NT_STATUS_INVALID_PARAMETER;
101         }
102
103         if (sd_length > trans->in.data.length ||
104             ea_length > trans->in.data.length ||
105             (sd_length+ea_length) > trans->in.data.length) {
106                 return NT_STATUS_INVALID_PARAMETER;
107         }
108
109         /* this call has an optional security descriptor */
110         if (sd_length != 0) {
111                 DATA_BLOB blob;
112                 blob.data = trans->in.data.data;
113                 blob.length = sd_length;
114                 io->ntcreatex.in.sec_desc = talloc(io, struct security_descriptor);
115                 if (io->ntcreatex.in.sec_desc == NULL) {
116                         return NT_STATUS_NO_MEMORY;
117                 }
118                 status = ndr_pull_struct_blob(&blob, io, 
119                                               io->ntcreatex.in.sec_desc, 
120                                               (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
121                 if (!NT_STATUS_IS_OK(status)) {
122                         return status;
123                 }
124         }
125
126         /* and an optional ea_list */
127         if (ea_length > 4) {
128                 DATA_BLOB blob;
129                 blob.data = trans->in.data.data + sd_length;
130                 blob.length = ea_length;
131                 io->ntcreatex.in.ea_list = talloc(io, struct smb_ea_list);
132                 if (io->ntcreatex.in.ea_list == NULL) {
133                         return NT_STATUS_NO_MEMORY;
134                 }
135
136                 status = ea_pull_list_chained(&blob, io, 
137                                               &io->ntcreatex.in.ea_list->num_eas,
138                                               &io->ntcreatex.in.ea_list->eas);
139                 if (!NT_STATUS_IS_OK(status)) {
140                         return status;
141                 }
142         }
143
144         /* call the backend - notice that we do it sync for now, until we support
145            async nttrans requests */    
146         status = ntvfs_openfile(req, io);
147         if (!NT_STATUS_IS_OK(status)) {
148                 return status;
149         }
150
151         trans->out.setup_count = 0;
152         trans->out.setup       = NULL;
153         trans->out.params      = data_blob_talloc(req, NULL, 69);
154         trans->out.data        = data_blob(NULL, 0);
155
156         params = trans->out.params.data;
157         if (params == NULL) {
158                 return NT_STATUS_NO_MEMORY;
159         }
160
161         SSVAL(params,        0, io->ntcreatex.out.oplock_level);
162         SSVAL(params,        2, io->ntcreatex.out.fnum);
163         SIVAL(params,        4, io->ntcreatex.out.create_action);
164         SIVAL(params,        8, 0); /* ea error offset */
165         push_nttime(params, 12, io->ntcreatex.out.create_time);
166         push_nttime(params, 20, io->ntcreatex.out.access_time);
167         push_nttime(params, 28, io->ntcreatex.out.write_time);
168         push_nttime(params, 36, io->ntcreatex.out.change_time);
169         SIVAL(params,       44, io->ntcreatex.out.attrib);
170         SBVAL(params,       48, io->ntcreatex.out.alloc_size);
171         SBVAL(params,       56, io->ntcreatex.out.size);
172         SSVAL(params,       64, io->ntcreatex.out.file_type);
173         SSVAL(params,       66, io->ntcreatex.out.ipc_state);
174         SCVAL(params,       68, io->ntcreatex.out.is_directory);
175
176         return NT_STATUS_OK;
177 }
178
179
180 /* 
181    parse NTTRANS_QUERY_SEC_DESC request
182  */
183 static NTSTATUS nttrans_query_sec_desc(struct smbsrv_request *req, 
184                                        struct smb_nttrans *trans)
185 {
186         union smb_fileinfo *io;
187         NTSTATUS status;
188
189         if (trans->in.params.length < 8) {
190                 return NT_STATUS_INVALID_PARAMETER;
191         }
192
193         /* parse the request */
194         io = talloc(req, union smb_fileinfo);
195         if (io == NULL) {
196                 return NT_STATUS_NO_MEMORY;
197         }
198
199         io->query_secdesc.level            = RAW_FILEINFO_SEC_DESC;
200         io->query_secdesc.in.fnum          = SVAL(trans->in.params.data, 0);
201         io->query_secdesc.secinfo_flags    = IVAL(trans->in.params.data, 4);
202
203         /* call the backend - notice that we do it sync for now, until we support
204            async nttrans requests */    
205         status = ntvfs_qfileinfo(req, io);
206         if (!NT_STATUS_IS_OK(status)) {
207                 return status;
208         }
209
210         trans->out.setup_count = 0;
211         trans->out.setup       = NULL;
212         trans->out.params      = data_blob_talloc(req, NULL, 4);
213         trans->out.data        = data_blob(NULL, 0);
214
215         status = ndr_push_struct_blob(&trans->out.data, req, 
216                                       io->query_secdesc.out.sd, 
217                                       (ndr_push_flags_fn_t)ndr_push_security_descriptor);
218         if (!NT_STATUS_IS_OK(status)) {
219                 return status;
220         }
221
222         SIVAL(trans->out.params.data, 0, trans->out.data.length);
223
224         return NT_STATUS_OK;
225 }
226
227
228 /* 
229    parse NTTRANS_SET_SEC_DESC request
230  */
231 static NTSTATUS nttrans_set_sec_desc(struct smbsrv_request *req, 
232                                        struct smb_nttrans *trans)
233 {
234         union smb_setfileinfo *io;
235         NTSTATUS status;
236
237         if (trans->in.params.length < 8) {
238                 return NT_STATUS_INVALID_PARAMETER;
239         }
240
241         /* parse the request */
242         io = talloc(req, union smb_setfileinfo);
243         if (io == NULL) {
244                 return NT_STATUS_NO_MEMORY;
245         }
246
247         io->set_secdesc.level            = RAW_SFILEINFO_SEC_DESC;
248         io->set_secdesc.file.fnum        = SVAL(trans->in.params.data, 0);
249         io->set_secdesc.in.secinfo_flags = IVAL(trans->in.params.data, 4);
250
251         io->set_secdesc.in.sd = talloc(io, struct security_descriptor);
252         if (io->set_secdesc.in.sd == NULL) {
253                 return NT_STATUS_NO_MEMORY;
254         }
255
256         status = ndr_pull_struct_blob(&trans->in.data, req, 
257                                       io->set_secdesc.in.sd, 
258                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
259         if (!NT_STATUS_IS_OK(status)) {
260                 return status;
261         }
262
263         /* call the backend - notice that we do it sync for now, until we support
264            async nttrans requests */    
265         status = ntvfs_setfileinfo(req, io);
266         if (!NT_STATUS_IS_OK(status)) {
267                 return status;
268         }
269
270         trans->out.setup_count = 0;
271         trans->out.setup       = NULL;
272         trans->out.params      = data_blob(NULL, 0);
273         trans->out.data        = data_blob(NULL, 0);
274
275         return NT_STATUS_OK;
276 }
277
278
279 /* parse NTTRANS_RENAME request
280  */
281 static NTSTATUS nttrans_rename(struct smbsrv_request *req, 
282                                struct smb_nttrans *trans)
283 {
284         return NT_STATUS_FOOBAR;
285 }
286
287 /* 
288    parse NTTRANS_IOCTL request
289  */
290 static NTSTATUS nttrans_ioctl(struct smbsrv_request *req, 
291                 struct smb_nttrans *trans)
292 {
293         union smb_ioctl nt;
294         uint32_t function;
295         uint16_t fnum;
296         uint8_t filter;
297         BOOL fsctl;
298         DATA_BLOB *blob;
299
300         /* should have at least 4 setup words */
301         if (trans->in.setup_count != 4) {
302                 return NT_STATUS_INVALID_PARAMETER;
303         }
304         
305         function  = IVAL(trans->in.setup, 0);
306         fnum  = SVAL(trans->in.setup, 4);
307         fsctl = CVAL(trans->in.setup, 6);
308         filter = CVAL(trans->in.setup, 7);
309
310         blob = &trans->in.data;
311
312         nt.ntioctl.level = RAW_IOCTL_NTIOCTL;
313         nt.ntioctl.in.fnum = fnum;
314         nt.ntioctl.in.function = function;
315         nt.ntioctl.in.fsctl = fsctl;
316         nt.ntioctl.in.filter = filter;
317
318         nttrans_setup_reply(req, trans, 0, 0, 1);
319         trans->out.setup[0] = 0;
320         
321         return ntvfs_ioctl(req, &nt);
322 }
323
324 /*
325   backend for nttrans requests
326 */
327 static NTSTATUS nttrans_backend(struct smbsrv_request *req, 
328                 struct smb_nttrans *trans)
329 {
330         /* the nttrans command is in function */
331         switch (trans->in.function) {
332         case NT_TRANSACT_CREATE:
333                 return nttrans_create(req, trans);
334         case NT_TRANSACT_IOCTL:
335                 return nttrans_ioctl(req, trans);
336         case NT_TRANSACT_RENAME:
337                 return nttrans_rename(req, trans);
338         case NT_TRANSACT_QUERY_SECURITY_DESC:
339                 return nttrans_query_sec_desc(req, trans);
340         case NT_TRANSACT_SET_SECURITY_DESC:
341                 return nttrans_set_sec_desc(req, trans);
342         }
343
344         /* an unknown nttrans command */
345         return NT_STATUS_FOOBAR;
346 }
347
348
349 /****************************************************************************
350  Reply to an SMBnttrans request
351 ****************************************************************************/
352 void reply_nttrans(struct smbsrv_request *req)
353 {
354         struct smb_nttrans trans;
355         int i;
356         uint16_t param_ofs, data_ofs;
357         uint16_t param_count, data_count;
358         uint16_t params_left, data_left;
359         uint16_t param_total, data_total;
360         uint8_t *params, *data;
361         NTSTATUS status;
362
363         /* parse request */
364         if (req->in.wct < 19) {
365                 req_reply_error(req, NT_STATUS_FOOBAR);
366                 return;
367         }
368
369         trans.in.max_setup   = CVAL(req->in.vwv, 0);
370         param_total          = IVAL(req->in.vwv, 3);
371         data_total           = IVAL(req->in.vwv, 7);
372         trans.in.max_param   = IVAL(req->in.vwv, 11);
373         trans.in.max_data    = IVAL(req->in.vwv, 15);
374         param_count          = IVAL(req->in.vwv, 19);
375         param_ofs            = IVAL(req->in.vwv, 23);
376         data_count           = IVAL(req->in.vwv, 27);
377         data_ofs             = IVAL(req->in.vwv, 31);
378         trans.in.setup_count = CVAL(req->in.vwv, 35);
379         trans.in.function        = SVAL(req->in.vwv, 36);
380
381         if (req->in.wct != 19 + trans.in.setup_count) {
382                 req_reply_dos_error(req, ERRSRV, ERRerror);
383                 return;
384         }
385
386         /* parse out the setup words */
387         trans.in.setup = talloc_array(req, uint16_t, trans.in.setup_count);
388         if (!trans.in.setup) {
389                 req_reply_error(req, NT_STATUS_NO_MEMORY);
390                 return;
391         }
392         for (i=0;i<trans.in.setup_count;i++) {
393                 trans.in.setup[i] = SVAL(req->in.vwv, VWV(19+i));
394         }
395
396         if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans.in.params) ||
397             !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans.in.data)) {
398                 req_reply_error(req, NT_STATUS_FOOBAR);
399                 return;
400         }
401
402         /* is it a partial request? if so, then send a 'send more' message */
403         if (param_total > param_count ||
404             data_total > data_count) {
405                 DEBUG(0,("REWRITE: not handling partial nttrans requests!\n"));
406                 return;
407         }
408
409         /* its a full request, give it to the backend */
410         status = nttrans_backend(req, &trans);
411
412         if (NT_STATUS_IS_ERR(status)) {
413                 req_reply_error(req, status);
414                 return;
415         }
416
417 #if 0
418         /* w2k3 does not check the max_setup count */
419         if (trans.out.setup_count > trans.in.max_setup) {
420                 req_reply_error(req, NT_STATUS_BUFFER_TOO_SMALL);
421                 return;
422         }
423 #endif
424         if (trans.out.params.length > trans.in.max_param) {
425                 status = NT_STATUS_BUFFER_TOO_SMALL;
426                 trans.out.params.length = trans.in.max_param;
427         }
428         if (trans.out.data.length > trans.in.max_data) {
429                 status = NT_STATUS_BUFFER_TOO_SMALL;
430                 trans.out.data.length = trans.in.max_data;
431         }
432
433         params_left = trans.out.params.length;
434         data_left   = trans.out.data.length;
435         params      = trans.out.params.data;
436         data        = trans.out.data.data;
437
438         req_setup_reply(req, 18 + trans.out.setup_count, 0);
439
440         if (!NT_STATUS_IS_OK(status)) {
441                 req_setup_error(req, status);
442         }
443
444         /* we need to divide up the reply into chunks that fit into
445            the negotiated buffer size */
446         do {
447                 uint16_t this_data, this_param, max_bytes;
448                 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
449                 struct smbsrv_request *this_req;
450
451                 max_bytes = req_max_data(req) - (align1 + align2);
452
453                 this_param = params_left;
454                 if (this_param > max_bytes) {
455                         this_param = max_bytes;
456                 }
457                 max_bytes -= this_param;
458
459                 this_data = data_left;
460                 if (this_data > max_bytes) {
461                         this_data = max_bytes;
462                 }
463
464                 /* don't destroy unless this is the last chunk */
465                 if (params_left - this_param != 0 || 
466                     data_left - this_data != 0) {
467                         this_req = req_setup_secondary(req);
468                 } else {
469                         this_req = req;
470                 }
471
472                 req_grow_data(req, this_param + this_data + (align1 + align2));
473
474                 SSVAL(this_req->out.vwv, 0, 0); /* reserved */
475                 SCVAL(this_req->out.vwv, 2, 0); /* reserved */
476                 SIVAL(this_req->out.vwv, 3, trans.out.params.length);
477                 SIVAL(this_req->out.vwv, 7, trans.out.data.length);
478
479                 SIVAL(this_req->out.vwv, 11, this_param);
480                 SIVAL(this_req->out.vwv, 15, align1 + PTR_DIFF(this_req->out.data, this_req->out.hdr));
481                 SIVAL(this_req->out.vwv, 19, PTR_DIFF(params, trans.out.params.data));
482
483                 SIVAL(this_req->out.vwv, 23, this_data);
484                 SIVAL(this_req->out.vwv, 27, align1 + align2 + 
485                       PTR_DIFF(this_req->out.data + this_param, this_req->out.hdr));
486                 SIVAL(this_req->out.vwv, 31, PTR_DIFF(data, trans.out.data.data));
487
488                 SCVAL(this_req->out.vwv, 35, trans.out.setup_count);
489                 for (i=0;i<trans.out.setup_count;i++) {
490                         SSVAL(this_req->out.vwv, VWV(18+i), trans.out.setup[i]);
491                 }
492
493                 memset(this_req->out.data, 0, align1);
494                 if (this_param != 0) {
495                         memcpy(this_req->out.data + align1, params, this_param);
496                 }
497                 memset(this_req->out.data+this_param+align1, 0, align2);
498                 if (this_data != 0) {
499                         memcpy(this_req->out.data+this_param+align1+align2, 
500                                data, this_data);
501                 }
502
503                 params_left -= this_param;
504                 data_left -= this_data;
505                 params += this_param;
506                 data += this_data;
507
508                 req_send_reply(this_req);
509         } while (params_left != 0 || data_left != 0);
510 }
511
512
513 /****************************************************************************
514  Reply to an SMBnttranss request
515 ****************************************************************************/
516 void reply_nttranss(struct smbsrv_request *req)
517 {
518         req_reply_error(req, NT_STATUS_FOOBAR);
519 }