vsftpd ver. 2.3.4 (2011-02-15)
[tomato.git] / release / src / router / vsftpd / ls.c
blobb2bc929d92a398571000f770da0f8adda2cc334a
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 int len;
92 static struct mystr s_next_filename_str;
93 static struct mystr s_next_path_and_filename_str;
94 static struct vsf_sysutil_statbuf* s_p_statbuf;
95 str_next_dirent(&s_next_filename_str, p_dir);
96 if (str_isempty(&s_next_filename_str))
98 break;
100 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;
114 /* Don't show hidden directory entries */
115 if (!vsf_access_check_file_visible(&s_next_filename_str))
117 continue;
119 /* If we have an ls option which is a filter, apply it */
120 if (!str_isempty(p_filter_str))
122 unsigned int iters = 0;
123 if (!vsf_filename_passes_filter(&s_next_filename_str, p_filter_str,
124 &iters))
126 continue;
129 /* Calculate the full path (relative to CWD) for lstat() and
130 * output purposes
132 str_copy(&s_next_path_and_filename_str, &normalised_base_dir_str);
133 str_append_str(&s_next_path_and_filename_str, &s_next_filename_str);
134 if (do_stat)
136 /* lstat() the file. Of course there's a race condition - the
137 * directory entry may have gone away whilst we read it, so
138 * ignore failure to stat
140 int retval = str_lstat(&s_next_path_and_filename_str, &s_p_statbuf);
141 if (vsf_sysutil_retval_is_error(retval))
143 continue;
146 if (is_verbose)
148 static struct mystr s_final_file_str;
149 /* If it's a damn symlink, we need to append the target */
150 str_copy(&s_final_file_str, &s_next_filename_str);
151 if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf))
153 static struct mystr s_temp_str;
154 int retval = str_readlink(&s_temp_str, &s_next_path_and_filename_str);
155 if (retval == 0 && !str_isempty(&s_temp_str))
157 str_append_text(&s_final_file_str, " -> ");
158 str_append_str(&s_final_file_str, &s_temp_str);
161 if (F_option && vsf_sysutil_statbuf_is_dir(s_p_statbuf))
163 str_append_char(&s_final_file_str, '/');
165 build_dir_line(&dirline_str, &s_final_file_str, s_p_statbuf, curr_time);
167 else
169 /* Just emit the filenames - note, we prepend the directory for NLST
170 * but not for LIST
172 str_copy(&dirline_str, &s_next_path_and_filename_str);
173 if (F_option)
175 if (vsf_sysutil_statbuf_is_dir(s_p_statbuf))
177 str_append_char(&dirline_str, '/');
179 else if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf))
181 str_append_char(&dirline_str, '@');
184 str_append_text(&dirline_str, "\r\n");
186 /* Add filename into our sorted list - sorting by filename or time. Also,
187 * if we are required to, maintain a distinct list of direct
188 * subdirectories.
191 static struct mystr s_temp_str;
192 const struct mystr* p_sort_str = 0;
193 const struct mystr* p_sort_subdir_str = 0;
194 if (!t_option)
196 p_sort_str = &s_next_filename_str;
198 else
200 str_alloc_text(&s_temp_str,
201 vsf_sysutil_statbuf_get_sortkey_mtime(s_p_statbuf));
202 p_sort_str = &s_temp_str;
203 p_sort_subdir_str = &s_temp_str;
205 str_list_add(p_list, &dirline_str, p_sort_str);
206 if (p_subdir_list != 0 && vsf_sysutil_statbuf_is_dir(s_p_statbuf))
208 str_list_add(p_subdir_list, &s_next_filename_str, p_sort_subdir_str);
211 } /* END: while(1) */
212 str_list_sort(p_list, r_option);
213 if (p_subdir_list != 0)
215 str_list_sort(p_subdir_list, r_option);
217 str_free(&dirline_str);
218 str_free(&normalised_base_dir_str);
222 vsf_filename_passes_filter(const struct mystr* p_filename_str,
223 const struct mystr* p_filter_str,
224 unsigned int* iters)
226 /* A simple routine to match a filename against a pattern.
227 * This routine is used instead of e.g. fnmatch(3), because we should be
228 * reluctant to trust the latter. fnmatch(3) involves _lots_ of string
229 * parsing and handling. There is broad potential for any given fnmatch(3)
230 * implementation to be buggy.
232 * Currently supported pattern(s):
233 * - any number of wildcards, "*" or "?"
234 * - {,} syntax (not nested)
236 * Note that pattern matching is only supported within the last path
237 * component. For example, searching for /a/b/? will work, but searching
238 * for /a/?/c will not.
240 struct mystr filter_remain_str = INIT_MYSTR;
241 struct mystr name_remain_str = INIT_MYSTR;
242 struct mystr temp_str = INIT_MYSTR;
243 struct mystr brace_list_str = INIT_MYSTR;
244 struct mystr new_filter_str = INIT_MYSTR;
245 int ret = 0;
246 char last_token = 0;
247 int must_match_at_current_pos = 1;
248 str_copy(&filter_remain_str, p_filter_str);
249 str_copy(&name_remain_str, p_filename_str);
251 while (!str_isempty(&filter_remain_str) && *iters < VSFTP_MATCHITERS_MAX)
253 static struct mystr s_match_needed_str;
254 /* Locate next special token */
255 struct str_locate_result locate_result =
256 str_locate_chars(&filter_remain_str, "*?{");
257 (*iters)++;
258 /* Isolate text leading up to token (if any) - needs to be matched */
259 if (locate_result.found)
261 unsigned int indexx = locate_result.index;
262 str_left(&filter_remain_str, &s_match_needed_str, indexx);
263 str_mid_to_end(&filter_remain_str, &temp_str, indexx + 1);
264 str_copy(&filter_remain_str, &temp_str);
265 last_token = locate_result.char_found;
267 else
269 /* No more tokens. Must match remaining filter string exactly. */
270 str_copy(&s_match_needed_str, &filter_remain_str);
271 str_empty(&filter_remain_str);
272 last_token = 0;
274 if (!str_isempty(&s_match_needed_str))
276 /* Need to match something.. could be a match which has to start at
277 * current position, or we could allow it to start anywhere
279 unsigned int indexx;
280 locate_result = str_locate_str(&name_remain_str, &s_match_needed_str);
281 if (!locate_result.found)
283 /* Fail */
284 goto out;
286 indexx = locate_result.index;
287 if (must_match_at_current_pos && indexx > 0)
289 goto out;
291 /* Chop matched string out of remainder */
292 str_mid_to_end(&name_remain_str, &temp_str,
293 indexx + str_getlen(&s_match_needed_str));
294 str_copy(&name_remain_str, &temp_str);
296 if (last_token == '?')
298 if (str_isempty(&name_remain_str))
300 goto out;
302 str_right(&name_remain_str, &temp_str, str_getlen(&name_remain_str) - 1);
303 str_copy(&name_remain_str, &temp_str);
304 must_match_at_current_pos = 1;
306 else if (last_token == '{')
308 struct str_locate_result end_brace =
309 str_locate_char(&filter_remain_str, '}');
310 must_match_at_current_pos = 1;
311 if (end_brace.found)
313 str_split_char(&filter_remain_str, &temp_str, '}');
314 str_copy(&brace_list_str, &filter_remain_str);
315 str_copy(&filter_remain_str, &temp_str);
316 str_split_char(&brace_list_str, &temp_str, ',');
317 while (!str_isempty(&brace_list_str))
319 str_copy(&new_filter_str, &brace_list_str);
320 str_append_str(&new_filter_str, &filter_remain_str);
321 if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str,
322 iters))
324 ret = 1;
325 goto out;
327 str_copy(&brace_list_str, &temp_str);
328 str_split_char(&brace_list_str, &temp_str, ',');
330 goto out;
332 else if (str_isempty(&name_remain_str) ||
333 str_get_char_at(&name_remain_str, 0) != '{')
335 goto out;
337 else
339 str_right(&name_remain_str, &temp_str,
340 str_getlen(&name_remain_str) - 1);
341 str_copy(&name_remain_str, &temp_str);
344 else
346 must_match_at_current_pos = 0;
349 /* Any incoming string left means no match unless we ended on the correct
350 * type of wildcard.
352 if (str_getlen(&name_remain_str) > 0 && last_token != '*')
354 goto out;
356 /* OK, a match */
357 ret = 1;
358 if (*iters == VSFTP_MATCHITERS_MAX) {
359 ret = 0;
361 out:
362 str_free(&filter_remain_str);
363 str_free(&name_remain_str);
364 str_free(&temp_str);
365 str_free(&brace_list_str);
366 str_free(&new_filter_str);
367 return ret;
370 static void
371 build_dir_line(struct mystr* p_str, const struct mystr* p_filename_str,
372 const struct vsf_sysutil_statbuf* p_stat, long curr_time)
374 static struct mystr s_tmp_str;
375 filesize_t size = vsf_sysutil_statbuf_get_size(p_stat);
376 /* Permissions */
377 str_alloc_text(p_str, vsf_sysutil_statbuf_get_perms(p_stat));
378 str_append_char(p_str, ' ');
379 /* Hard link count */
380 str_alloc_ulong(&s_tmp_str, vsf_sysutil_statbuf_get_links(p_stat));
381 str_lpad(&s_tmp_str, 4);
382 str_append_str(p_str, &s_tmp_str);
383 str_append_char(p_str, ' ');
384 /* User */
385 if (tunable_hide_ids)
387 str_alloc_text(&s_tmp_str, "ftp");
389 else
391 int uid = vsf_sysutil_statbuf_get_uid(p_stat);
392 struct vsf_sysutil_user* p_user = 0;
393 if (tunable_text_userdb_names)
395 p_user = vsf_sysutil_getpwuid(uid);
397 if (p_user == 0)
399 str_alloc_ulong(&s_tmp_str, (unsigned long) uid);
401 else
403 str_alloc_text(&s_tmp_str, vsf_sysutil_user_getname(p_user));
406 str_rpad(&s_tmp_str, 8);
407 str_append_str(p_str, &s_tmp_str);
408 str_append_char(p_str, ' ');
409 /* Group */
410 if (tunable_hide_ids)
412 str_alloc_text(&s_tmp_str, "ftp");
414 else
416 int gid = vsf_sysutil_statbuf_get_gid(p_stat);
417 struct vsf_sysutil_group* p_group = 0;
418 if (tunable_text_userdb_names)
420 p_group = vsf_sysutil_getgrgid(gid);
422 if (p_group == 0)
424 str_alloc_ulong(&s_tmp_str, (unsigned long) gid);
426 else
428 str_alloc_text(&s_tmp_str, vsf_sysutil_group_getname(p_group));
431 str_rpad(&s_tmp_str, 8);
432 str_append_str(p_str, &s_tmp_str);
433 str_append_char(p_str, ' ');
434 /* Size in bytes */
435 str_alloc_filesize_t(&s_tmp_str, size);
436 str_lpad(&s_tmp_str, 8);
437 str_append_str(p_str, &s_tmp_str);
438 str_append_char(p_str, ' ');
439 /* Date stamp */
440 str_append_text(p_str, vsf_sysutil_statbuf_get_date(p_stat,
441 tunable_use_localtime,
442 curr_time));
443 str_append_char(p_str, ' ');
444 /* Filename */
445 str_append_str(p_str, p_filename_str);
446 str_append_text(p_str, "\r\n");