r884: convert samba4 to use [u]int32_t instead of [u]int32
[bbaumbach/samba-autobuild/.git] / source4 / smb_server / 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
27
28 #define CHECK_MIN_BLOB_SIZE(blob, size) do { \
29         if ((blob)->length < (size)) { \
30                 return NT_STATUS_INFO_LENGTH_MISMATCH; \
31         }} while (0)
32
33
34 /* setup a nttrans reply, given the data and params sizes */
35 static void nttrans_setup_reply(struct request_context *req, 
36                                struct smb_nttrans *trans,
37                                uint16 param_size, uint16 data_size,
38                                uint16 setup_count)
39 {
40         trans->out.setup_count = setup_count;
41         if (setup_count != 0) {
42                 trans->out.setup = talloc_zero(req->mem_ctx, sizeof(uint16) * setup_count);
43         }
44         trans->out.params = data_blob_talloc(req->mem_ctx, NULL, param_size);
45         trans->out.data = data_blob_talloc(req->mem_ctx, NULL, data_size);
46 }
47
48
49 /* parse NTTRANS_CREATE request
50  */
51 static NTSTATUS nttrans_create(struct request_context *req, 
52                 struct smb_nttrans *trans)
53 {
54         return NT_STATUS_FOOBAR;
55 }
56
57 /* parse NTTRANS_RENAME request
58  */
59 static NTSTATUS nttrans_rename(struct request_context *req, 
60                 struct smb_nttrans *trans)
61 {
62         return NT_STATUS_FOOBAR;
63 }
64 /* parse NTTRANS_IOCTL request
65  */
66 static NTSTATUS nttrans_ioctl(struct request_context *req, 
67                 struct smb_nttrans *trans)
68 {
69         union smb_ioctl nt;
70         uint32_t function;
71         uint16 fnum;
72         uint8 filter;
73         BOOL fsctl;
74         DATA_BLOB *blob;
75
76         /* should have at least 4 setup words */
77         if (trans->in.setup_count != 4) {
78                 return NT_STATUS_INVALID_PARAMETER;
79         }
80         
81         function  = IVAL(trans->in.setup, 0);
82         fnum  = SVAL(trans->in.setup, 4);
83         fsctl = CVAL(trans->in.setup, 6);
84         filter = CVAL(trans->in.setup, 7);
85
86         blob = &trans->in.data;
87
88         nt.ntioctl.level = RAW_IOCTL_NTIOCTL;
89         nt.ntioctl.in.fnum = fnum;
90         nt.ntioctl.in.function = function;
91         nt.ntioctl.in.fsctl = fsctl;
92         nt.ntioctl.in.filter = filter;
93
94         nttrans_setup_reply(req, trans, 0, 0, 1);
95         trans->out.setup[0] = 0;
96         
97         return req->conn->ntvfs_ops->ioctl(req, &nt);
98 }
99
100 /*
101   backend for nttrans requests
102 */
103 static NTSTATUS nttrans_backend(struct request_context *req, 
104                 struct smb_nttrans *trans)
105 {
106         DEBUG(9,("nttrans_backend: setup_count=%d function=%d\n",
107                 trans->in.setup_count, trans->in.function));
108         /* must have at least one setup word */
109         if (trans->in.setup_count < 1) {
110                 return NT_STATUS_FOOBAR;
111         }
112         
113         /* the nttrans command is in function */
114         switch (trans->in.function) {
115         case NT_TRANSACT_CREATE:
116                 return nttrans_create(req, trans);
117         case NT_TRANSACT_IOCTL:
118                 return nttrans_ioctl(req, trans);
119         case NT_TRANSACT_RENAME:
120                 return nttrans_rename(req, trans);
121         }
122
123         /* an unknown nttrans command */
124         return NT_STATUS_FOOBAR;
125 }
126
127
128 /****************************************************************************
129  Reply to an SMBnttrans request
130 ****************************************************************************/
131 void reply_nttrans(struct request_context *req)
132 {
133         struct smb_nttrans trans;
134         int i;
135         uint16 param_ofs, data_ofs;
136         uint16 param_count, data_count;
137         uint16 params_left, data_left;
138         uint16 param_total, data_total;
139         char *params, *data;
140         NTSTATUS status;
141
142         /* parse request */
143         if (req->in.wct < 19) {
144                 req_reply_error(req, NT_STATUS_FOOBAR);
145                 return;
146         }
147
148         trans.in.max_setup   = CVAL(req->in.vwv, 0);
149         param_total          = IVAL(req->in.vwv, 3);
150         data_total           = IVAL(req->in.vwv, 7);
151         trans.in.max_param   = IVAL(req->in.vwv, 11);
152         trans.in.max_data    = IVAL(req->in.vwv, 15);
153         param_count          = IVAL(req->in.vwv, 19);
154         param_ofs            = IVAL(req->in.vwv, 23);
155         data_count           = IVAL(req->in.vwv, 27);
156         data_ofs             = IVAL(req->in.vwv, 31);
157         trans.in.setup_count = CVAL(req->in.vwv, 35);
158         trans.in.function        = SVAL(req->in.vwv, 36);
159
160         if (req->in.wct != 19 + trans.in.setup_count) {
161                 req_reply_dos_error(req, ERRSRV, ERRerror);
162                 return;
163         }
164
165         /* parse out the setup words */
166         trans.in.setup = talloc(req->mem_ctx, trans.in.setup_count * sizeof(uint16));
167         if (!trans.in.setup) {
168                 req_reply_error(req, NT_STATUS_NO_MEMORY);
169                 return;
170         }
171         for (i=0;i<trans.in.setup_count;i++) {
172                 trans.in.setup[i] = SVAL(req->in.vwv, VWV(19+i));
173         }
174
175         if (!req_pull_blob(req, req->in.hdr + param_ofs, param_count, &trans.in.params) ||
176             !req_pull_blob(req, req->in.hdr + data_ofs, data_count, &trans.in.data)) {
177                 req_reply_error(req, NT_STATUS_FOOBAR);
178                 return;
179         }
180
181         /* is it a partial request? if so, then send a 'send more' message */
182         if (param_total > param_count ||
183             data_total > data_count) {
184                 DEBUG(0,("REWRITE: not handling partial nttrans requests!\n"));
185                 return;
186         }
187
188         /* its a full request, give it to the backend */
189         status = nttrans_backend(req, &trans);
190
191         if (!NT_STATUS_IS_OK(status)) {
192                 req_reply_error(req, status);
193                 return;
194         }
195
196         params_left = trans.out.params.length;
197         data_left   = trans.out.data.length;
198         params      = trans.out.params.data;
199         data        = trans.out.data.data;
200
201         req->control_flags |= REQ_CONTROL_PROTECTED;
202
203         /* we need to divide up the reply into chunks that fit into
204            the negotiated buffer size */
205         do {
206                 uint16 this_data, this_param, max_bytes;
207                 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
208
209                 req_setup_reply(req, 18 + trans.out.setup_count, 0);
210         
211                 max_bytes = req_max_data(req) - (align1 + align2);
212
213                 this_param = params_left;
214                 if (this_param > max_bytes) {
215                         this_param = max_bytes;
216                 }
217                 max_bytes -= this_param;
218
219                 this_data = data_left;
220                 if (this_data > max_bytes) {
221                         this_data = max_bytes;
222                 }
223
224                 req_grow_data(req, this_param + this_data + (align1 + align2));
225
226                 SIVAL(req->out.vwv, 3, trans.out.params.length);
227                 SIVAL(req->out.vwv, 7, trans.out.data.length);
228
229                 SIVAL(req->out.vwv, 11, this_param);
230                 SIVAL(req->out.vwv, 15, align1 + PTR_DIFF(req->out.data, req->out.hdr));
231                 SIVAL(req->out.vwv, 19, PTR_DIFF(params, trans.out.params.data));
232
233                 SIVAL(req->out.vwv, 23, this_data);
234                 SIVAL(req->out.vwv, 27, align1 + align2 + 
235                       PTR_DIFF(req->out.data + this_param, req->out.hdr));
236                 SIVAL(req->out.vwv, 31, PTR_DIFF(data, trans.out.data.data));
237
238                 SCVAL(req->out.vwv, 35, trans.out.setup_count);
239                 for (i=0;i<trans.out.setup_count;i++) {
240                         SSVAL(req->out.vwv, VWV(18+i)+1, trans.out.setup[i]);
241                 }
242
243                 memset(req->out.data, 0, align1);
244                 if (this_param != 0) {
245                         memcpy(req->out.data + align1, params, this_param);
246                 }
247                 memset(req->out.data+this_param+align1, 0, align2);
248                 if (this_data != 0) {
249                         memcpy(req->out.data+this_param+align1+align2, data, this_data);
250                 }
251
252                 params_left -= this_param;
253                 data_left -= this_data;
254                 params += this_param;
255                 data += this_data;
256
257                 /* if this is the last chunk then the request can be destroyed */
258                 if (params_left == 0 && data_left == 0) {
259                         req->control_flags &= ~REQ_CONTROL_PROTECTED;
260                 }
261
262                 req_send_reply(req);
263         } while (params_left != 0 || data_left != 0);
264 }
265
266
267 /****************************************************************************
268  Reply to an SMBnttranss request
269 ****************************************************************************/
270 void reply_nttranss(struct request_context *req)
271 {
272         req_reply_error(req, NT_STATUS_FOOBAR);
273 }