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