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"
29 /* check req->async.status and if not OK then send an error reply */
30 #define CHECK_ASYNC_STATUS do { \
31 if (!NT_STATUS_IS_OK(req->async_states->status)) { \
32 req_reply_error(req, req->async_states->status); \
37 check if the backend wants to handle the request asynchronously.
38 if it wants it handled synchronously then call the send function
41 #define REQ_ASYNC_TAIL do { \
42 if (!(req->async_states->state & NTVFS_ASYNC_STATE_ASYNC)) { \
43 req->async_states->send_fn(req); \
46 /* useful wrapper for talloc with NO_MEMORY reply */
47 #define REQ_TALLOC(ptr) do { \
48 ptr = talloc_size(req, sizeof(*(ptr))); \
50 req_reply_error(req, NT_STATUS_NO_MEMORY); \
54 #define CHECK_MIN_BLOB_SIZE(blob, size) do { \
55 if ((blob)->length < (size)) { \
56 return NT_STATUS_INFO_LENGTH_MISMATCH; \
59 /* a structure to encapsulate the state information about
60 * an in-progress search first/next operation */
62 struct smbsrv_request *req;
63 union smb_search_data *file;
64 uint16_t last_entry_offset;
68 fill a single entry in a search find reply
70 static BOOL find_fill_info(struct smbsrv_request *req,
71 union smb_search_data *file)
75 if (req->out.data_size + 43 > req_max_data(req)) {
79 req_grow_data(req, req->out.data_size + 43);
80 p = req->out.data + req->out.data_size - 43;
82 SCVAL(p, 0, file->search.id.reserved);
83 memcpy(p+1, file->search.id.name, 11);
84 SCVAL(p, 12, file->search.id.handle);
85 SIVAL(p, 13, file->search.id.server_cookie);
86 SIVAL(p, 17, file->search.id.client_cookie);
87 SCVAL(p, 21, file->search.attrib);
88 srv_push_dos_date(req->smb_conn, p, 22, file->search.write_time);
89 SIVAL(p, 26, file->search.size);
90 memset(p+30, ' ', 12);
91 memcpy(p+30, file->search.name, MIN(strlen(file->search.name)+1, 12));
97 /* callback function for search first/next */
98 static BOOL find_callback(void *private, union smb_search_data *file)
100 struct search_state *state = (struct search_state *)private;
102 return find_fill_info(state->req, file);
105 /****************************************************************************
107 ****************************************************************************/
108 void reply_search(struct smbsrv_request *req)
110 union smb_search_first *sf;
111 union smb_search_next *sn;
112 uint16_t resume_key_length;
113 struct search_state state;
116 enum smb_search_level level = RAW_SEARCH_SEARCH;
117 uint8_t op = CVAL(req->in.hdr,HDR_COM);
119 if (op == SMBffirst) {
120 level = RAW_SEARCH_FFIRST;
121 } else if (op == SMBfunique) {
122 level = RAW_SEARCH_FUNIQUE;
128 if (req->in.wct != 2) {
129 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
134 p += req_pull_ascii4(req, &sf->search_first.in.pattern,
136 if (!sf->search_first.in.pattern) {
137 req_reply_error(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
141 if (req_data_oob(req, p, 3)) {
142 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
146 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
149 resume_key_length = SVAL(p, 1);
152 /* setup state for callback */
155 state.last_entry_offset = 0;
157 /* construct reply */
158 req_setup_reply(req, 1, 0);
159 req_append_var_block(req, NULL, 0);
161 if (resume_key_length != 0) {
162 if (resume_key_length != 21 ||
163 req_data_oob(req, p, 21) ||
164 level == RAW_SEARCH_FUNIQUE) {
165 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
169 /* do a search next operation */
172 sn->search_next.in.id.reserved = CVAL(p, 0);
173 memcpy(sn->search_next.in.id.name, p+1, 11);
174 sn->search_next.in.id.handle = CVAL(p, 12);
175 sn->search_next.in.id.server_cookie = IVAL(p, 13);
176 sn->search_next.in.id.client_cookie = IVAL(p, 17);
178 sn->search_next.level = level;
179 sn->search_next.in.max_count = SVAL(req->in.vwv, VWV(0));
180 sn->search_next.in.search_attrib = SVAL(req->in.vwv, VWV(1));
183 status = ntvfs_search_next(req, sn, &state, find_callback);
184 SSVAL(req->out.vwv, VWV(0), sn->search_next.out.count);
186 /* do a search first operation */
187 sf->search_first.level = level;
188 sf->search_first.in.search_attrib = SVAL(req->in.vwv, VWV(1));
189 sf->search_first.in.max_count = SVAL(req->in.vwv, VWV(0));
192 status = ntvfs_search_first(req, sf, &state, find_callback);
193 SSVAL(req->out.vwv, VWV(0), sf->search_first.out.count);
196 if (!NT_STATUS_IS_OK(status)) {
197 req_reply_error(req, status);
205 /****************************************************************************
206 Reply to a fclose (async reply)
207 ****************************************************************************/
208 static void reply_fclose_send(struct smbsrv_request *req)
212 /* construct reply */
213 req_setup_reply(req, 1, 0);
215 SSVAL(req->out.vwv, VWV(0), 0);
221 /****************************************************************************
222 Reply to fclose (stop directory search).
223 ****************************************************************************/
224 void reply_fclose(struct smbsrv_request *req)
226 union smb_search_close *sc;
227 uint16_t resume_key_length;
234 if (req->in.wct != 2) {
235 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
240 p += req_pull_ascii4(req, &pattern, p, STR_TERMINATE);
241 if (pattern && *pattern) {
242 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
246 if (req_data_oob(req, p, 3)) {
247 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
251 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
254 resume_key_length = SVAL(p, 1);
257 if (resume_key_length != 21) {
258 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
262 if (req_data_oob(req, p, 21)) {
263 req_reply_error(req, NT_STATUS_INVALID_PARAMETER);
267 sc->fclose.level = RAW_FINDCLOSE_FCLOSE;
268 sc->fclose.in.max_count = SVAL(req->in.vwv, VWV(0));
269 sc->fclose.in.search_attrib = SVAL(req->in.vwv, VWV(1));
270 sc->fclose.in.id.reserved = CVAL(p, 0);
271 memcpy(sc->fclose.in.id.name, p+1, 11);
272 sc->fclose.in.id.handle = CVAL(p, 12);
273 sc->fclose.in.id.server_cookie = IVAL(p, 13);
274 sc->fclose.in.id.client_cookie = IVAL(p, 17);
276 /* do a search close operation */
277 req->async_states->state |= NTVFS_ASYNC_STATE_MAY_ASYNC;
278 req->async_states->send_fn = reply_fclose_send;
279 req->async_states->private_data = sc;
282 req->async_states->status = ntvfs_search_close(req, sc);