r23792: convert Samba4 to GPLv3
[tprouty/samba.git] / source4 / smb_server / smb2 / find.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB2 Find
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (c) Stefan Metzmacher 2006
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20 /*
21    This file handles the parsing of transact2 requests
22 */
23
24 #include "includes.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "smb_server/smb_server.h"
28 #include "smb_server/service_smb_proto.h"
29 #include "smb_server/smb2/smb2_server.h"
30 #include "ntvfs/ntvfs.h"
31
32
33 /* a structure to encapsulate the state information about an in-progress ffirst/fnext operation */
34 struct smb2srv_find_state {
35         struct smb2srv_request *req;
36         struct smb2_find *info;
37         union smb_search_first *ff;
38         union smb_search_next *fn;
39         uint32_t last_entry_offset;
40 };
41
42 /* callback function for SMB2 Find */
43 static BOOL smb2srv_find_callback(void *private, const union smb_search_data *file)
44 {
45         struct smb2srv_find_state *state = talloc_get_type(private, struct smb2srv_find_state);
46         struct smb2_find *info = state->info;
47         uint32_t old_length;
48         NTSTATUS status;
49
50         old_length = info->out.blob.length;
51
52         status = smbsrv_push_passthru_search(state, &info->out.blob, info->data_level, file, STR_UNICODE);
53         if (!NT_STATUS_IS_OK(status) ||
54             info->out.blob.length > info->in.max_response_size) {
55                 /* restore the old length and tell the backend to stop */
56                 smbsrv_blob_grow_data(state, &info->out.blob, old_length);
57                 return False;
58         }
59
60         state->last_entry_offset = old_length;
61
62         return True;
63 }
64
65 static void smb2srv_find_send(struct ntvfs_request *ntvfs)
66 {
67         struct smb2srv_request *req;
68         struct smb2srv_find_state *state;
69
70         SMB2SRV_CHECK_ASYNC_STATUS(state, struct smb2srv_find_state);
71         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, True, state->info->out.blob.length));
72
73         if (state->info->out.blob.length > 0) {
74                 SIVAL(state->info->out.blob.data + state->last_entry_offset, 0, 0);
75         }
76
77         SMB2SRV_CHECK(smb2_push_o16s32_blob(&req->out, 0x02, state->info->out.blob));
78
79         smb2srv_send_reply(req);
80 }
81
82 static NTSTATUS smb2srv_find_backend(struct smb2srv_find_state *state)
83 {
84         struct smb2_find *info = state->info;
85
86         switch (info->in.level) {
87         case SMB2_FIND_DIRECTORY_INFO:
88                 info->data_level = RAW_SEARCH_DATA_DIRECTORY_INFO;
89                 break;
90
91         case SMB2_FIND_FULL_DIRECTORY_INFO:
92                 info->data_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO;
93                 break;
94
95         case SMB2_FIND_BOTH_DIRECTORY_INFO:
96                 info->data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
97                 break;
98
99         case SMB2_FIND_NAME_INFO:
100                 info->data_level = RAW_SEARCH_DATA_NAME_INFO;
101                 break;
102
103         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
104                 info->data_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO;
105                 break;
106
107         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
108                 info->data_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO;
109                 break;
110
111         default:
112                 return NT_STATUS_FOOBAR;
113         }
114
115         if (info->in.continue_flags & SMB2_CONTINUE_FLAG_NEW) {
116                 state->ff = talloc(state, union smb_search_first);
117                 NT_STATUS_HAVE_NO_MEMORY(state->ff);
118
119                 state->ff->smb2 = *info;
120                 state->info = &state->ff->smb2;
121                 ZERO_STRUCT(state->ff->smb2.out);
122
123                 return ntvfs_search_first(state->req->ntvfs, state->ff, state, smb2srv_find_callback);
124         } else {
125                 state->fn = talloc(state, union smb_search_next);
126                 NT_STATUS_HAVE_NO_MEMORY(state->fn);
127
128                 state->fn->smb2 = *info;
129                 state->info = &state->fn->smb2;
130                 ZERO_STRUCT(state->fn->smb2.out);
131
132                 return ntvfs_search_next(state->req->ntvfs, state->fn, state, smb2srv_find_callback);
133         }
134
135         /* should not be reached */
136         return NT_STATUS_INTERNAL_ERROR;
137 }
138
139 void smb2srv_find_recv(struct smb2srv_request *req)
140 {
141         struct smb2srv_find_state *state;
142         struct smb2_find *info;
143
144         SMB2SRV_CHECK_BODY_SIZE(req, 0x20, True);
145         SMB2SRV_TALLOC_IO_PTR(info, struct smb2_find);
146         /* this overwrites req->io_ptr !*/
147         SMB2SRV_TALLOC_IO_PTR(state, struct smb2srv_find_state);
148         state->req              = req;
149         state->info             = info;
150         state->ff               = NULL;
151         state->fn               = NULL;
152         state->last_entry_offset= 0;
153         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_find_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
154
155         info->level                     = RAW_SEARCH_SMB2;
156         info->data_level                = RAW_SEARCH_DATA_GENERIC;/* will be overwritten later */
157         info->in.level                  = CVAL(req->in.body, 0x02);
158         info->in.continue_flags         = CVAL(req->in.body, 0x03);
159         info->in.unknown                = IVAL(req->in.body, 0x04);
160         info->in.file.ntvfs             = smb2srv_pull_handle(req, req->in.body, 0x08);
161         SMB2SRV_CHECK(smb2_pull_o16s16_string(&req->in, info, req->in.body+0x18, &info->in.pattern));
162         info->in.max_response_size      = IVAL(req->in.body, 0x1C);
163
164         SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
165         SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_find_backend(state));
166 }