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>
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.
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.
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.
22 This file handles the parsing of transact2 requests
28 #define CHECK_MIN_BLOB_SIZE(blob, size) do { \
29 if ((blob)->length < (size)) { \
30 return NT_STATUS_INFO_LENGTH_MISMATCH; \
34 /* setup a nttrans reply, given the data and params sizes */
35 static void nttrans_setup_reply(struct smbsrv_request *req,
36 struct smb_nttrans *trans,
37 uint16_t param_size, uint16_t data_size,
40 trans->out.setup_count = setup_count;
41 if (setup_count != 0) {
42 trans->out.setup = talloc_zero(req->mem_ctx, sizeof(uint16_t) * setup_count);
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);
49 /* parse NTTRANS_CREATE request
51 static NTSTATUS nttrans_create(struct smbsrv_request *req,
52 struct smb_nttrans *trans)
54 return NT_STATUS_FOOBAR;
57 /* parse NTTRANS_RENAME request
59 static NTSTATUS nttrans_rename(struct smbsrv_request *req,
60 struct smb_nttrans *trans)
62 return NT_STATUS_FOOBAR;
64 /* parse NTTRANS_IOCTL request
66 static NTSTATUS nttrans_ioctl(struct smbsrv_request *req,
67 struct smb_nttrans *trans)
76 /* should have at least 4 setup words */
77 if (trans->in.setup_count != 4) {
78 return NT_STATUS_INVALID_PARAMETER;
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);
86 blob = &trans->in.data;
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;
94 nttrans_setup_reply(req, trans, 0, 0, 1);
95 trans->out.setup[0] = 0;
97 return req->tcon->ntvfs_ops->ioctl(req, &nt);
101 backend for nttrans requests
103 static NTSTATUS nttrans_backend(struct smbsrv_request *req,
104 struct smb_nttrans *trans)
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;
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);
123 /* an unknown nttrans command */
124 return NT_STATUS_FOOBAR;
128 /****************************************************************************
129 Reply to an SMBnttrans request
130 ****************************************************************************/
131 void reply_nttrans(struct smbsrv_request *req)
133 struct smb_nttrans trans;
135 uint16_t param_ofs, data_ofs;
136 uint16_t param_count, data_count;
137 uint16_t params_left, data_left;
138 uint16_t param_total, data_total;
143 if (req->in.wct < 19) {
144 req_reply_error(req, NT_STATUS_FOOBAR);
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);
160 if (req->in.wct != 19 + trans.in.setup_count) {
161 req_reply_dos_error(req, ERRSRV, ERRerror);
165 /* parse out the setup words */
166 trans.in.setup = talloc(req->mem_ctx, trans.in.setup_count * sizeof(uint16_t));
167 if (!trans.in.setup) {
168 req_reply_error(req, NT_STATUS_NO_MEMORY);
171 for (i=0;i<trans.in.setup_count;i++) {
172 trans.in.setup[i] = SVAL(req->in.vwv, VWV(19+i));
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);
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"));
188 /* its a full request, give it to the backend */
189 status = nttrans_backend(req, &trans);
191 if (!NT_STATUS_IS_OK(status)) {
192 req_reply_error(req, status);
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;
201 req->control_flags |= REQ_CONTROL_PROTECTED;
203 /* we need to divide up the reply into chunks that fit into
204 the negotiated buffer size */
206 uint16_t this_data, this_param, max_bytes;
207 uint_t align1 = 1, align2 = (params_left ? 2 : 0);
209 req_setup_reply(req, 18 + trans.out.setup_count, 0);
211 max_bytes = req_max_data(req) - (align1 + align2);
213 this_param = params_left;
214 if (this_param > max_bytes) {
215 this_param = max_bytes;
217 max_bytes -= this_param;
219 this_data = data_left;
220 if (this_data > max_bytes) {
221 this_data = max_bytes;
224 req_grow_data(req, this_param + this_data + (align1 + align2));
226 SIVAL(req->out.vwv, 3, trans.out.params.length);
227 SIVAL(req->out.vwv, 7, trans.out.data.length);
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));
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));
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]);
243 memset(req->out.data, 0, align1);
244 if (this_param != 0) {
245 memcpy(req->out.data + align1, params, this_param);
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);
252 params_left -= this_param;
253 data_left -= this_data;
254 params += this_param;
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;
263 } while (params_left != 0 || data_left != 0);
267 /****************************************************************************
268 Reply to an SMBnttranss request
269 ****************************************************************************/
270 void reply_nttranss(struct smbsrv_request *req)
272 req_reply_error(req, NT_STATUS_FOOBAR);