vsftpd 3.0.2
[tomato/tomato-dir865l.git] / release / src / router / vsftpd / ls.c
blob7e1376d14a2863fec6adceb6694e5d3c4f69a7e1
1 /*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * ls.c
7 * Would you believe, code to handle directory listing.
8 */
10 #include "ls.h"
11 #include "access.h"
12 #include "defs.h"
13 #include "str.h"
14 #include "strlist.h"
15 #include "sysstr.h"
16 #include "sysutil.h"
17 #include "tunables.h"
19 static void build_dir_line(struct mystr* p_str,
20 const struct mystr* p_filename_str,
21 const struct vsf_sysutil_statbuf* p_stat,
22 long curr_time);
24 void
25 vsf_ls_populate_dir_list(struct mystr_list* p_list,
26 struct mystr_list* p_subdir_list,
27 struct vsf_sysutil_dir* p_dir,
28 const struct mystr* p_base_dir_str,
29 const struct mystr* p_option_str,
30 const struct mystr* p_filter_str,
31 int is_verbose)
33 struct mystr dirline_str = INIT_MYSTR;
34 struct mystr normalised_base_dir_str = INIT_MYSTR;
35 struct str_locate_result loc_result;
36 int a_option;
37 int r_option;
38 int t_option;
39 int F_option;
40 int do_stat = 0;
41 long curr_time = 0;
42 loc_result = str_locate_char(p_option_str, 'a');
43 a_option = loc_result.found;
44 loc_result = str_locate_char(p_option_str, 'r');
45 r_option = loc_result.found;
46 loc_result = str_locate_char(p_option_str, 't');
47 t_option = loc_result.found;
48 loc_result = str_locate_char(p_option_str, 'F');
49 F_option = loc_result.found;
50 loc_result = str_locate_char(p_option_str, 'l');
51 if (loc_result.found)
53 is_verbose = 1;
55 /* Invert "reverse" arg for "-t", the time sorting */
56 if (t_option)
58 r_option = !r_option;
60 if (is_verbose || t_option || F_option || p_subdir_list != 0)
62 do_stat = 1;
64 /* If the filter starts with a . then implicitly enable -a */
65 if (!str_isempty(p_filter_str) && str_get_char_at(p_filter_str, 0) == '.')
67 a_option = 1;
69 /* "Normalise" the incoming base directory string by making sure it
70 * ends in a '/' if it is nonempty
72 if (!str_equal_text(p_base_dir_str, "."))
74 str_copy(&normalised_base_dir_str, p_base_dir_str);
76 if (!str_isempty(&normalised_base_dir_str))
78 unsigned int len = str_getlen(&normalised_base_dir_str);
79 if (str_get_char_at(&normalised_base_dir_str, len - 1) != '/')
81 str_append_char(&normalised_base_dir_str, '/');
84 /* If we're going to need to do time comparisions, cache the local time */
85 if (is_verbose)
87 curr_time = vsf_sysutil_get_time_sec();
89 while (1)
91 static struct mystr s_next_filename_str;
92 static struct mystr s_next_path_and_filename_str;
93 static struct vsf_sysutil_statbuf* s_p_statbuf;
94 str_next_dirent(&s_next_filename_str, p_dir);
95 if (str_isempty(&s_next_filename_str))
97 break;
100 unsigned int len = str_getlen(&s_next_filename_str);
101 if (len > 0 && str_get_char_at(&s_next_filename_str, 0) == '.')
103 if (!a_option && !tunable_force_dot_files)
105 continue;
107 if (!a_option &&
108 ((len == 2 && str_get_char_at(&s_next_filename_str, 1) == '.') ||
109 len == 1))
111 continue;
115 /* Don't show hidden directory entries */
116 if (!vsf_access_check_file_visible(&s_next_filename_str))
118 continue;
120 /* If we have an ls option which is a filter, apply it */
121 if (!str_isempty(p_filter_str))
123 unsigned int iters = 0;
124 if (!vsf_filename_passes_filter(&s_next_filename_str, p_filter_str,
125 &iters))
127 continue;
130 /* Calculate the full path (relative to CWD) for lstat() and
131 * output purposes
133 str_copy(&s_next_path_and_filename_str, &normalised_base_dir_str);
134 str_append_str(&s_next_path_and_filename_str, &s_next_filename_str);
135 if (do_stat)
137 /* lstat() the file. Of course there's a race condition - the
138 * directory entry may have gone away whilst we read it, so
139 * ignore failure to stat
141 int retval = str_lstat(&s_next_path_and_filename_str, &s_p_statbuf);
142 if (vsf_sysutil_retval_is_error(retval))
144 continue;
147 if (is_verbose)
149 static struct mystr s_final_file_str;
150 /* If it's a damn symlink, we need to append the target */
151 str_copy(&s_final_file_str, &s_next_filename_str);
152 if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf))
154 static struct mystr s_temp_str;
155 int retval = str_readlink(&s_temp_str, &s_next_path_and_filename_str);
156 if (retval == 0 && !str_isempty(&s_temp_str))
158 str_append_text(&s_final_file_str, " -> ");
159 str_append_str(&s_final_file_str, &s_temp_str);
162 if (F_option && vsf_sysutil_statbuf_is_dir(s_p_statbuf))
164 str_append_char(&s_final_file_str, '/');
166 build_dir_line(&dirline_str, &s_final_file_str, s_p_statbuf, curr_time);
168 else
170 /* Just emit the filenames - note, we prepend the directory for NLST
171 * but not for LIST
173 str_copy(&dirline_str, &s_next_path_and_filename_str);
174 if (F_option)
176 if (vsf_sysutil_statbuf_is_dir(s_p_statbuf))
178 str_append_char(&dirline_str, '/');
180 else if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf))
182 str_append_char(&dirline_str, '@');
185 str_append_text(&dirline_str, "\r\n");
187 /* Add filename into our sorted list - sorting by filename or time. Also,
188 * if we are required to, maintain a distinct list of direct
189 * subdirectories.
192 static struct mystr s_temp_str;
193 const struct mystr* p_sort_str = 0;
194 const struct mystr* p_sort_subdir_str = 0;
195 if (!t_option)
197 p_sort_str = &s_next_filename_str;
199 else
201 str_alloc_text(&s_temp_str,
202 vsf_sysutil_statbuf_get_sortkey_mtime(s_p_statbuf));
203 p_sort_str = &s_temp_str;
204 p_sort_subdir_str = &s_temp_str;
206 str_list_add(p_list, &dirline_str, p_sort_str);
207 if (p_subdir_list != 0 && vsf_sysutil_statbuf_is_dir(s_p_statbuf))
209 str_list_add(p_subdir_list, &s_next_filename_str, p_sort_subdir_str);
212 } /* END: while(1) */
213 str_list_sort(p_list, r_option);
214 if (p_subdir_list != 0)
216 str_list_sort(p_subdir_list, r_option);
218 str_free(&dirline_str);
219 str_free(&normalised_base_dir_str);
223 vsf_filename_passes_filter(const struct mystr* p_filename_str,
224 const struct mystr* p_filter_str,
225 unsigned int* iters)
227 /* A simple routine to match a filename against a pattern.
228 * This routine is used instead of e.g. fnmatch(3), because we should be
229 * reluctant to trust the latter. fnmatch(3) involves _lots_ of string
230 * parsing and handling. There is broad potential for any given fnmatch(3)
231 * implementation to be buggy.
233 * Currently supported pattern(s):
234 * - any number of wildcards, "*" or "?"
235 * - {,} syntax (not nested)
237 * Note that pattern matching is only supported within the last path
238 * component. For example, searching for /a/b/? will work, but searching
239 * for /a/?/c will not.
241 struct mystr filter_remain_str = INIT_MYSTR;
242 struct mystr name_remain_str = INIT_MYSTR;
243 struct mystr temp_str = INIT_MYSTR;
244 struct mystr brace_list_str = INIT_MYSTR;
245 struct mystr new_filter_str = INIT_MYSTR;
246 int ret = 0;
247 char last_token = 0;
248 int must_match_at_current_pos = 1;
249 str_copy(&filter_remain_str, p_filter_str);
250 str_copy(&name_remain_str, p_filename_str);
252 while (!str_isempty(&filter_remain_str) && *iters < VSFTP_MATCHITERS_MAX)
254 static struct mystr s_match_needed_str;
255 /* Locate next special token */
256 struct str_locate_result locate_result =
257 str_locate_chars(&filter_remain_str, "*?{");
258 (*iters)++;
259 /* Isolate text leading up to token (if any) - needs to be matched */
260 if (locate_result.found)
262 unsigned int indexx = locate_result.index;
263 str_left(&filter_remain_str, &s_match_needed_str, indexx);
264 str_mid_to_end(&filter_remain_str, &temp_str, indexx + 1);
265 str_copy(&filter_remain_str, &temp_str);
266 last_token = locate_result.char_found;
268 else
270 /* No more tokens. Must match remaining filter string exactly. */
271 str_copy(&s_match_needed_str, &filter_remain_str);
272 str_empty(&filter_remain_str);
273 last_token = 0;
275 if (!str_isempty(&s_match_needed_str))
277 /* Need to match something.. could be a match which has to start at
278 * current position, or we could allow it to start anywhere
280 unsigned int indexx;
281 locate_result = str_locate_str(&name_remain_str, &s_match_needed_str);
282 if (!locate_result.found)
284 /* Fail */
285 goto out;
287 indexx = locate_result.index;
288 if (must_match_at_current_pos && indexx > 0)
290 goto out;
292 /* Chop matched string out of remainder */
293 str_mid_to_end(&name_remain_str, &temp_str,
294 indexx + str_getlen(&s_match_needed_str));
295 str_copy(&name_remain_str, &temp_str);
297 if (last_token == '?')
299 if (str_isempty(&name_remain_str))
301 goto out;
303 str_right(&name_remain_str, &temp_str, str_getlen(&name_remain_str) - 1);
304 str_copy(&name_remain_str, &temp_str);
305 must_match_at_current_pos = 1;
307 else if (last_token == '{')
309 struct str_locate_result end_brace =
310 str_locate_char(&filter_remain_str, '}');
311 must_match_at_current_pos = 1;
312 if (end_brace.found)
314 str_split_char(&filter_remain_str, &temp_str, '}');
315 str_copy(&brace_list_str, &filter_remain_str);
316 str_copy(&filter_remain_str, &temp_str);
317 str_split_char(&brace_list_str, &temp_str, ',');
318 while (!str_isempty(&brace_list_str))
320 str_copy(&new_filter_str, &brace_list_str);
321 str_append_str(&new_filter_str, &filter_remain_str);
322 if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str,
323 iters))
325 ret = 1;
326 goto out;
328 str_copy(&brace_list_str, &temp_str);
329 str_split_char(&brace_list_str, &temp_str, ',');
331 goto out;
333 else if (str_isempty(&name_remain_str) ||
334 str_get_char_at(&name_remain_str, 0) != '{')
336 goto out;
338 else
340 str_right(&name_remain_str, &temp_str,
341 str_getlen(&name_remain_str) - 1);
342 str_copy(&name_remain_str, &temp_str);
345 else
347 must_match_at_current_pos = 0;
350 /* Any incoming string left means no match unless we ended on the correct
351 * type of wildcard.
353 if (str_getlen(&name_remain_str) > 0 && last_token != '*')
355 goto out;
357 /* OK, a match */
358 ret = 1;
359 if (*iters == VSFTP_MATCHITERS_MAX) {
360 ret = 0;
362 out:
363 str_free(&filter_remain_str);
364 str_free(&name_remain_str);
365 str_free(&temp_str);
366 str_free(&brace_list_str);
367 str_free(&new_filter_str);
368 return ret;
371 static void
372 build_dir_line(struct mystr* p_str, const struct mystr* p_filename_str,
373 const struct vsf_sysutil_statbuf* p_stat, long curr_time)
375 static struct mystr s_tmp_str;
376 filesize_t size = vsf_sysutil_statbuf_get_size(p_stat);
377 /* Permissions */
378 str_alloc_text(p_str, vsf_sysutil_statbuf_get_perms(p_stat));
379 str_append_char(p_str, ' ');
380 /* Hard link count */
381 str_alloc_ulong(&s_tmp_str, vsf_sysutil_statbuf_get_links(p_stat));
382 str_lpad(&s_tmp_str, 4);
383 str_append_str(p_str, &s_tmp_str);
384 str_append_char(p_str, ' ');
385 /* User */
386 if (tunable_hide_ids)
388 str_alloc_text(&s_tmp_str, "ftp");
390 else
392 int uid = vsf_sysutil_statbuf_get_uid(p_stat);
393 struct vsf_sysutil_user* p_user = 0;
394 if (tunable_text_userdb_names)
396 p_user = vsf_sysutil_getpwuid(uid);
398 if (p_user == 0)
400 str_alloc_ulong(&s_tmp_str, (unsigned long) uid);
402 else
404 str_alloc_text(&s_tmp_str, vsf_sysutil_user_getname(p_user));
407 str_rpad(&s_tmp_str, 8);
408 str_append_str(p_str, &s_tmp_str);
409 str_append_char(p_str, ' ');
410 /* Group */
411 if (tunable_hide_ids)
413 str_alloc_text(&s_tmp_str, "ftp");
415 else
417 int gid = vsf_sysutil_statbuf_get_gid(p_stat);
418 struct vsf_sysutil_group* p_group = 0;
419 if (tunable_text_userdb_names)
421 p_group = vsf_sysutil_getgrgid(gid);
423 if (p_group == 0)
425 str_alloc_ulong(&s_tmp_str, (unsigned long) gid);
427 else
429 str_alloc_text(&s_tmp_str, vsf_sysutil_group_getname(p_group));
432 str_rpad(&s_tmp_str, 8);
433 str_append_str(p_str, &s_tmp_str);
434 str_append_char(p_str, ' ');
435 /* Size in bytes */
436 str_alloc_filesize_t(&s_tmp_str, size);
437 str_lpad(&s_tmp_str, 8);
438 str_append_str(p_str, &s_tmp_str);
439 str_append_char(p_str, ' ');
440 /* Date stamp */
441 str_append_text(p_str, vsf_sysutil_statbuf_get_date(p_stat,
442 tunable_use_localtime,
443 curr_time));
444 str_append_char(p_str, ' ');
445 /* Filename */
446 str_append_str(p_str, p_filename_str);
447 str_append_text(p_str, "\r\n");