r23029: fixed formatting
[Samba.git] / source / smb_server / smb2 / find.c
blob428d13366aa879d53db9ee63c636756518cfc71b
1 /*
2 Unix SMB/CIFS implementation.
3 SMB2 Find
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (c) Stefan Metzmacher 2006
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
25 #include "includes.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "smb_server/smb_server.h"
29 #include "smb_server/service_smb_proto.h"
30 #include "smb_server/smb2/smb2_server.h"
31 #include "ntvfs/ntvfs.h"
34 /* a structure to encapsulate the state information about an in-progress ffirst/fnext operation */
35 struct smb2srv_find_state {
36 struct smb2srv_request *req;
37 struct smb2_find *info;
38 union smb_search_first *ff;
39 union smb_search_next *fn;
40 uint32_t last_entry_offset;
43 /* callback function for SMB2 Find */
44 static BOOL smb2srv_find_callback(void *private, union smb_search_data *file)
46 struct smb2srv_find_state *state = talloc_get_type(private, struct smb2srv_find_state);
47 struct smb2_find *info = state->info;
48 uint32_t old_length;
49 NTSTATUS status;
51 old_length = info->out.blob.length;
53 status = smbsrv_push_passthru_search(state, &info->out.blob, info->data_level, file, STR_UNICODE);
54 if (!NT_STATUS_IS_OK(status) ||
55 info->out.blob.length > info->in.max_response_size) {
56 /* restore the old length and tell the backend to stop */
57 smbsrv_blob_grow_data(state, &info->out.blob, old_length);
58 return False;
61 state->last_entry_offset = old_length;
63 return True;
66 static void smb2srv_find_send(struct ntvfs_request *ntvfs)
68 struct smb2srv_request *req;
69 struct smb2srv_find_state *state;
71 SMB2SRV_CHECK_ASYNC_STATUS(state, struct smb2srv_find_state);
72 SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, True, state->info->out.blob.length));
74 if (state->info->out.blob.length > 0) {
75 SIVAL(state->info->out.blob.data + state->last_entry_offset, 0, 0);
78 SMB2SRV_CHECK(smb2_push_o16s32_blob(&req->out, 0x02, state->info->out.blob));
80 smb2srv_send_reply(req);
83 static NTSTATUS smb2srv_find_backend(struct smb2srv_find_state *state)
85 struct smb2_find *info = state->info;
87 switch (info->in.level) {
88 case SMB2_FIND_DIRECTORY_INFO:
89 info->data_level = RAW_SEARCH_DATA_DIRECTORY_INFO;
90 break;
92 case SMB2_FIND_FULL_DIRECTORY_INFO:
93 info->data_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO;
94 break;
96 case SMB2_FIND_BOTH_DIRECTORY_INFO:
97 info->data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
98 break;
100 case SMB2_FIND_NAME_INFO:
101 info->data_level = RAW_SEARCH_DATA_NAME_INFO;
102 break;
104 case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
105 info->data_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO;
106 break;
108 case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
109 info->data_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO;
110 break;
112 default:
113 return NT_STATUS_FOOBAR;
116 if (info->in.continue_flags & SMB2_CONTINUE_FLAG_NEW) {
117 state->ff = talloc(state, union smb_search_first);
118 NT_STATUS_HAVE_NO_MEMORY(state->ff);
120 state->ff->smb2 = *info;
121 state->info = &state->ff->smb2;
122 ZERO_STRUCT(state->ff->smb2.out);
124 return ntvfs_search_first(state->req->ntvfs, state->ff, state, smb2srv_find_callback);
125 } else {
126 state->fn = talloc(state, union smb_search_next);
127 NT_STATUS_HAVE_NO_MEMORY(state->fn);
129 state->fn->smb2 = *info;
130 state->info = &state->fn->smb2;
131 ZERO_STRUCT(state->fn->smb2.out);
133 return ntvfs_search_next(state->req->ntvfs, state->fn, state, smb2srv_find_callback);
136 /* should not be reached */
137 return NT_STATUS_INTERNAL_ERROR;
140 void smb2srv_find_recv(struct smb2srv_request *req)
142 struct smb2srv_find_state *state;
143 struct smb2_find *info;
145 SMB2SRV_CHECK_BODY_SIZE(req, 0x20, True);
146 SMB2SRV_TALLOC_IO_PTR(info, struct smb2_find);
147 /* this overwrites req->io_ptr !*/
148 SMB2SRV_TALLOC_IO_PTR(state, struct smb2srv_find_state);
149 state->req = req;
150 state->info = info;
151 state->ff = NULL;
152 state->fn = NULL;
153 state->last_entry_offset= 0;
154 SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_find_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
156 info->level = RAW_SEARCH_SMB2;
157 info->data_level = RAW_SEARCH_DATA_GENERIC;/* will be overwritten later */
158 info->in.level = CVAL(req->in.body, 0x02);
159 info->in.continue_flags = CVAL(req->in.body, 0x03);
160 info->in.unknown = IVAL(req->in.body, 0x04);
161 info->in.file.ntvfs = smb2srv_pull_handle(req, req->in.body, 0x08);
162 SMB2SRV_CHECK(smb2_pull_o16s16_string(&req->in, info, req->in.body+0x18, &info->in.pattern));
163 info->in.max_response_size = IVAL(req->in.body, 0x1C);
165 SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
166 SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_find_backend(state));