2 Unix SMB/CIFS implementation.
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
26 #include "smb_server/smb_server.h"
27 #include "ntvfs/ntvfs.h"
30 /* check req->async.status and if not OK then send an error reply */
31 #define CHECK_ASYNC_STATUS do { \
32 if (!NT_STATUS_IS_OK(req->async_states->status)) { \
33 req_reply_error(req, req->async_states->status); \
38 check if the backend wants to handle the request asynchronously.
39 if it wants it handled synchronously then call the send function
42 #define REQ_ASYNC_TAIL do { \
43 if (!(req->async_states->state & NTVFS_ASYNC_STATE_ASYNC)) { \
44 req->async_states->send_fn(req); \
47 /* useful wrapper for talloc with NO_MEMORY reply */
48 #define REQ_TALLOC(ptr) do { \
49 ptr = talloc_size(req, sizeof(*(ptr))); \
51 req_reply_error(req, NT_STATUS_NO_MEMORY); \
55 #define CHECK_MIN_BLOB_SIZE(blob, size) do { \
56 if ((blob)->length < (size)) { \
57 return NT_STATUS_INFO_LENGTH_MISMATCH; \
60 /* a structure to encapsulate the state information about
61 * an in-progress search first/next operation */
63 struct smbsrv_request *req;
64 union smb_search_data *file;
65 uint16_t last_entry_offset;
69 fill a single entry in a search find reply
71 static BOOL find_fill_info(struct smbsrv_request *req,
72 union smb_search_data *file)
76 if (req->out.data_size + 43 > req_max_data(req)) {
80 req_grow_data(req, req->out.data_size + 43);
81 p = req->out.data + req->out.data_size - 43;
83 SCVAL(p, 0, file->search.id.reserved);
84 memcpy(p+1, file->search.id.name, 11);
85 SCVAL(p, 12, file->search.id.handle);
86 SIVAL(p, 13, file->search.id.server_cookie);
87 SIVAL(p, 17, file->search.id.client_cookie);
88 SCVAL(p, 21, file->search.attrib);
89 srv_push_dos_date(req->smb_conn, p, 22, file->search.write_time);
90 SIVAL(p, 26, file->search.size);
91 memset(p+30, ' ', 12);
92 memcpy(p+30, file->search.name, MIN(strlen(file->search.name)+1, 12));
98 /* callback function for search first/next */
99 static BOOL find_callback(void *private, union smb_search_data *file)
101 struct search_state *state = (struct search_state *)private;
103 return find_fill_info(state->req, file);
106 /****************************************************************************
108 ****************************************************************************/
109 void reply_search(struct smbsrv_request *req)
111 union smb_search_first *sf;
112 union smb_search_next *sn;
113 uint16_t resume_key_length;
114 struct search_state state;
117 enum smb_search_level level = RAW_SEARCH_SEARCH;
118 uint8_t op = CVAL(req->in.hdr,HDR_COM);
120 if (op == SMBffirst) {
121 level = RAW_SEARCH_FFIRST;
122 } else if (op == SMBfunique) {
123 level = RAW_SEARCH_FUNIQUE;
129 if (req->in.wct != 2) {
130 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
135 p += req_pull_ascii4(req, &sf->search_first.in.pattern,
137 if (!sf->search_first.in.pattern) {
138 req_reply_error(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
142 if (req_data_oob(req, p, 3)) {
143 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
147 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
150 resume_key_length = SVAL(p, 1);
153 /* setup state for callback */
156 state.last_entry_offset = 0;
158 /* construct reply */
159 req_setup_reply(req, 1, 0);
160 req_append_var_block(req, NULL, 0);
162 if (resume_key_length != 0) {
163 if (resume_key_length != 21 ||
164 req_data_oob(req, p, 21) ||
165 level == RAW_SEARCH_FUNIQUE) {
166 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
170 /* do a search next operation */
173 sn->search_next.in.id.reserved = CVAL(p, 0);
174 memcpy(sn->search_next.in.id.name, p+1, 11);
175 sn->search_next.in.id.handle = CVAL(p, 12);
176 sn->search_next.in.id.server_cookie = IVAL(p, 13);
177 sn->search_next.in.id.client_cookie = IVAL(p, 17);
179 sn->search_next.level = level;
180 sn->search_next.in.max_count = SVAL(req->in.vwv, VWV(0));
181 sn->search_next.in.search_attrib = SVAL(req->in.vwv, VWV(1));
184 status = ntvfs_search_next(req, sn, &state, find_callback);
185 SSVAL(req->out.vwv, VWV(0), sn->search_next.out.count);
187 /* do a search first operation */
188 sf->search_first.level = level;
189 sf->search_first.in.search_attrib = SVAL(req->in.vwv, VWV(1));
190 sf->search_first.in.max_count = SVAL(req->in.vwv, VWV(0));
193 status = ntvfs_search_first(req, sf, &state, find_callback);
194 SSVAL(req->out.vwv, VWV(0), sf->search_first.out.count);
197 if (!NT_STATUS_IS_OK(status)) {
198 req_reply_error(req, status);
206 /****************************************************************************
207 Reply to a fclose (async reply)
208 ****************************************************************************/
209 static void reply_fclose_send(struct smbsrv_request *req)
213 /* construct reply */
214 req_setup_reply(req, 1, 0);
216 SSVAL(req->out.vwv, VWV(0), 0);
222 /****************************************************************************
223 Reply to fclose (stop directory search).
224 ****************************************************************************/
225 void reply_fclose(struct smbsrv_request *req)
227 union smb_search_close *sc;
228 uint16_t resume_key_length;
235 if (req->in.wct != 2) {
236 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
241 p += req_pull_ascii4(req, &pattern, p, STR_TERMINATE);
242 if (pattern && *pattern) {
243 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
247 if (req_data_oob(req, p, 3)) {
248 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
252 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
255 resume_key_length = SVAL(p, 1);
258 if (resume_key_length != 21) {
259 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
263 if (req_data_oob(req, p, 21)) {
264 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
268 sc->fclose.level = RAW_FINDCLOSE_FCLOSE;
269 sc->fclose.in.max_count = SVAL(req->in.vwv, VWV(0));
270 sc->fclose.in.search_attrib = SVAL(req->in.vwv, VWV(1));
271 sc->fclose.in.id.reserved = CVAL(p, 0);
272 memcpy(sc->fclose.in.id.name, p+1, 11);
273 sc->fclose.in.id.handle = CVAL(p, 12);
274 sc->fclose.in.id.server_cookie = IVAL(p, 13);
275 sc->fclose.in.id.client_cookie = IVAL(p, 17);
277 /* do a search close operation */
278 req->async_states->state |= NTVFS_ASYNC_STATE_MAY_ASYNC;
279 req->async_states->send_fn = reply_fclose_send;
280 req->async_states->private_data = sc;
283 req->async_states->status = ntvfs_search_close(req, sc);