r23792: convert Samba4 to GPLv3
[samba.git] / source / libcli / clilist.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client directory list routines
4    Copyright (C) Andrew Tridgell 1994-2003
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
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 #include "includes.h"
22 #include "libcli/libcli.h"
23 #include "libcli/raw/libcliraw.h"
24
25 struct search_private {
26         struct clilist_file_info *dirlist;
27         TALLOC_CTX *mem_ctx;
28         int dirlist_len;
29         int ff_searchcount;  /* total received in 1 server trip */
30         int total_received;  /* total received all together */
31         enum smb_search_data_level data_level;
32         const char *last_name;     /* used to continue trans2 search */
33         struct smb_search_id id;   /* used for old-style search */
34 };
35
36
37 /****************************************************************************
38  Interpret a long filename structure.
39 ****************************************************************************/
40 static BOOL interpret_long_filename(enum smb_search_data_level level,
41                                     const union smb_search_data *info,
42                                     struct clilist_file_info *finfo)
43 {
44         struct clilist_file_info finfo2;
45
46         if (!finfo) finfo = &finfo2;
47         ZERO_STRUCTP(finfo);
48
49         switch (level) {
50         case RAW_SEARCH_DATA_STANDARD:
51                 finfo->size = info->standard.size;
52                 finfo->mtime = info->standard.write_time;
53                 finfo->attrib = info->standard.attrib;
54                 finfo->name = info->standard.name.s;
55                 finfo->short_name = info->standard.name.s;
56                 break;
57
58         case RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO:
59                 finfo->size = info->both_directory_info.size;
60                 finfo->mtime = nt_time_to_unix(info->both_directory_info.write_time);
61                 finfo->attrib = info->both_directory_info.attrib;
62                 finfo->short_name = info->both_directory_info.short_name.s;
63                 finfo->name = info->both_directory_info.name.s;
64                 break;
65
66         default:
67                 DEBUG(0,("Unhandled level %d in interpret_long_filename\n", (int)level));
68                 return False;
69         }
70
71         return True;
72 }
73
74 /* callback function used for trans2 search */
75 static BOOL smbcli_list_new_callback(void *private, const union smb_search_data *file)
76 {
77         struct search_private *state = (struct search_private*) private;
78         struct clilist_file_info *tdl;
79  
80         /* add file info to the dirlist pool */
81         tdl = talloc_realloc(state, 
82                              state->dirlist,
83                              struct clilist_file_info,
84                              state->dirlist_len + 1);
85         if (!tdl) {
86                 return False;
87         }
88         state->dirlist = tdl;
89         state->dirlist_len++;
90
91         interpret_long_filename(state->data_level, file, &state->dirlist[state->total_received]);
92
93         state->last_name = state->dirlist[state->total_received].name;
94         state->total_received++;
95         state->ff_searchcount++;
96         
97         return True;
98 }
99
100 int smbcli_list_new(struct smbcli_tree *tree, const char *Mask, uint16_t attribute, 
101                     enum smb_search_data_level level,
102                     void (*fn)(struct clilist_file_info *, const char *, void *), 
103                     void *caller_state)
104 {
105         union smb_search_first first_parms;
106         union smb_search_next next_parms;
107         struct search_private state;  /* for callbacks */
108         int received = 0;
109         BOOL first = True;
110         int num_received = 0;
111         int max_matches = 512;
112         char *mask;
113         int ff_eos = 0, i, ff_searchcount;
114         int ff_dir_handle=0;
115
116         /* initialize state for search */
117         state.mem_ctx = talloc_init("smbcli_list_new");
118         state.dirlist_len = 0;
119         state.total_received = 0;
120         
121         state.dirlist = talloc_new(state.mem_ctx);
122         mask = talloc_strdup(state.mem_ctx, Mask);
123
124         if (level == RAW_SEARCH_DATA_GENERIC) {
125                 if (tree->session->transport->negotiate.capabilities & CAP_NT_SMBS) {
126                         level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
127                 } else {
128                         level = RAW_SEARCH_DATA_STANDARD;
129                 }
130         }
131         state.data_level = level;
132
133         while (1) {
134                 state.ff_searchcount = 0;
135                 if (first) {
136                         NTSTATUS status;
137
138                         first_parms.t2ffirst.level = RAW_SEARCH_TRANS2;
139                         first_parms.t2ffirst.data_level = state.data_level;
140                         first_parms.t2ffirst.in.max_count = max_matches;
141                         first_parms.t2ffirst.in.search_attrib = attribute;
142                         first_parms.t2ffirst.in.pattern = mask;
143                         first_parms.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
144                         first_parms.t2ffirst.in.storage_type = 0;
145                         
146                         status = smb_raw_search_first(tree, 
147                                                       state.mem_ctx, &first_parms,
148                                                       (void*)&state, smbcli_list_new_callback);
149                         if (!NT_STATUS_IS_OK(status)) {
150                                 talloc_free(state.mem_ctx);
151                                 return -1;
152                         }
153                 
154                         ff_dir_handle = first_parms.t2ffirst.out.handle;
155                         ff_searchcount = first_parms.t2ffirst.out.count;
156                         ff_eos = first_parms.t2ffirst.out.end_of_search;
157                         
158                         received = first_parms.t2ffirst.out.count;
159                         if (received <= 0) break;
160                         if (ff_eos) break;
161                         first = False;
162                 } else {
163                         NTSTATUS status;
164
165                         next_parms.t2fnext.level = RAW_SEARCH_TRANS2;
166                         next_parms.t2fnext.data_level = state.data_level;
167                         next_parms.t2fnext.in.max_count = max_matches;
168                         next_parms.t2fnext.in.last_name = state.last_name;
169                         next_parms.t2fnext.in.handle = ff_dir_handle;
170                         next_parms.t2fnext.in.resume_key = 0;
171                         next_parms.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
172                         
173                         status = smb_raw_search_next(tree, 
174                                                      state.mem_ctx,
175                                                      &next_parms,
176                                                      (void*)&state, 
177                                                      smbcli_list_new_callback);
178                         
179                         if (!NT_STATUS_IS_OK(status)) {
180                                 return -1;
181                         }
182                         ff_searchcount = next_parms.t2fnext.out.count;
183                         ff_eos = next_parms.t2fnext.out.end_of_search;
184                         received = next_parms.t2fnext.out.count;
185                         if (received <= 0) break;
186                         if (ff_eos) break;
187                 }
188                 
189                 num_received += received;
190         }
191
192         for (i=0;i<state.total_received;i++) {
193                 fn(&state.dirlist[i], Mask, caller_state);
194         }
195
196         talloc_free(state.mem_ctx);
197
198         return state.total_received;
199 }
200
201 /****************************************************************************
202  Interpret a short filename structure.
203  The length of the structure is returned.
204 ****************************************************************************/
205 static BOOL interpret_short_filename(enum smb_search_data_level level,
206                                      const union smb_search_data *info,
207                                      struct clilist_file_info *finfo)
208 {
209         struct clilist_file_info finfo2;
210
211         if (!finfo) finfo = &finfo2;
212         ZERO_STRUCTP(finfo);
213
214         switch (level) {
215         case RAW_SEARCH_DATA_SEARCH:
216                 finfo->mtime = info->search.write_time;
217                 finfo->size = info->search.size;
218                 finfo->attrib = info->search.attrib;
219                 finfo->name = info->search.name;
220                 finfo->short_name = info->search.name;
221                 break;
222
223         default:
224                 DEBUG(0,("Unhandled level %d in interpret_short_filename\n", (int)level));
225                 return False;
226         }
227         
228         return True;
229 }
230
231 /* callback function used for smb_search */
232 static BOOL smbcli_list_old_callback(void *private, const union smb_search_data *file)
233 {
234         struct search_private *state = (struct search_private*) private;
235         struct clilist_file_info *tdl;
236         
237         /* add file info to the dirlist pool */
238         tdl = talloc_realloc(state,
239                              state->dirlist,
240                              struct clilist_file_info,
241                              state->dirlist_len + 1);
242
243         if (!tdl) {
244                 return False;
245         }
246         state->dirlist = tdl;
247         state->dirlist_len++;
248
249         interpret_short_filename(state->data_level, file, &state->dirlist[state->total_received]);
250
251         state->total_received++;
252         state->ff_searchcount++;
253         state->id = file->search.id; /* return resume info */
254         
255         return True;
256 }
257
258 int smbcli_list_old(struct smbcli_tree *tree, const char *Mask, uint16_t attribute, 
259                  void (*fn)(struct clilist_file_info *, const char *, void *), 
260                  void *caller_state)
261 {
262         union smb_search_first first_parms;
263         union smb_search_next next_parms;
264         struct search_private state;  /* for callbacks */
265         const int num_asked = 500;
266         int received = 0;
267         BOOL first = True;
268         int num_received = 0;
269         char *mask;
270         int i;
271
272         /* initialize state for search */
273         state.mem_ctx = talloc_init("smbcli_list_old");
274         state.dirlist_len = 0;
275         state.total_received = 0;
276         state.data_level = RAW_SEARCH_DATA_SEARCH;
277
278         state.dirlist = talloc_new(state.mem_ctx);
279         mask = talloc_strdup(state.mem_ctx, Mask);
280   
281         while (1) {
282                 state.ff_searchcount = 0;
283                 if (first) {
284                         NTSTATUS status;
285
286                         first_parms.search_first.level = RAW_SEARCH_SEARCH;
287                         first_parms.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
288                         first_parms.search_first.in.max_count = num_asked;
289                         first_parms.search_first.in.search_attrib = attribute;
290                         first_parms.search_first.in.pattern = mask;
291                         
292                         status = smb_raw_search_first(tree, state.mem_ctx, 
293                                                       &first_parms,
294                                                       (void*)&state, 
295                                                       smbcli_list_old_callback);
296
297                         if (!NT_STATUS_IS_OK(status)) {
298                                 talloc_free(state.mem_ctx);
299                                 return -1;
300                         }
301                 
302                         received = first_parms.search_first.out.count;
303                         if (received <= 0) break;
304                         first = False;
305                 } else {
306                         NTSTATUS status;
307
308                         next_parms.search_next.level = RAW_SEARCH_SEARCH;
309                         next_parms.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
310                         next_parms.search_next.in.max_count = num_asked;
311                         next_parms.search_next.in.search_attrib = attribute;
312                         next_parms.search_next.in.id = state.id;
313                         
314                         status = smb_raw_search_next(tree, state.mem_ctx,
315                                                      &next_parms,
316                                                      (void*)&state, 
317                                                      smbcli_list_old_callback);
318
319                         if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
320                                 break;
321                         }
322                         if (!NT_STATUS_IS_OK(status)) {
323                                 talloc_free(state.mem_ctx);
324                                 return -1;
325                         }
326                         received = next_parms.search_next.out.count;
327                         if (received <= 0) break;
328                 }
329                 
330                 num_received += received;
331         }
332
333         for (i=0;i<state.total_received;i++) {
334                 fn(&state.dirlist[i], Mask, caller_state);
335         }
336
337         talloc_free(state.mem_ctx);
338
339         return state.total_received;
340 }
341
342 /****************************************************************************
343  Do a directory listing, calling fn on each file found.
344  This auto-switches between old and new style.
345 ****************************************************************************/
346
347 int smbcli_list(struct smbcli_tree *tree, const char *Mask,uint16_t attribute, 
348                 void (*fn)(struct clilist_file_info *, const char *, void *), void *state)
349 {
350         if (tree->session->transport->negotiate.protocol <= PROTOCOL_LANMAN1)
351                 return smbcli_list_old(tree, Mask, attribute, fn, state);
352         return smbcli_list_new(tree, Mask, attribute, RAW_SEARCH_DATA_GENERIC, fn, state);
353 }