vsftpd ver. 2.2.1 (10/20/2009)
[tomato.git] / release / src / router / vsftpd / ls.c
blobbdf5233ec5b63cfe8cbcd885c6cf62376212338f
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 "str.h"
13 #include "strlist.h"
14 #include "sysstr.h"
15 #include "sysutil.h"
16 #include "tunables.h"
18 static void build_dir_line(struct mystr* p_str,
19 const struct mystr* p_filename_str,
20 const struct vsf_sysutil_statbuf* p_stat,
21 long curr_time);
23 void
24 vsf_ls_populate_dir_list(struct mystr_list* p_list,
25 struct mystr_list* p_subdir_list,
26 struct vsf_sysutil_dir* p_dir,
27 const struct mystr* p_base_dir_str,
28 const struct mystr* p_option_str,
29 const struct mystr* p_filter_str,
30 int is_verbose)
32 struct mystr dirline_str = INIT_MYSTR;
33 struct mystr normalised_base_dir_str = INIT_MYSTR;
34 struct str_locate_result loc_result;
35 int a_option;
36 int r_option;
37 int t_option;
38 int F_option;
39 int do_stat = 0;
40 long curr_time = 0;
41 loc_result = str_locate_char(p_option_str, 'a');
42 a_option = loc_result.found;
43 loc_result = str_locate_char(p_option_str, 'r');
44 r_option = loc_result.found;
45 loc_result = str_locate_char(p_option_str, 't');
46 t_option = loc_result.found;
47 loc_result = str_locate_char(p_option_str, 'F');
48 F_option = loc_result.found;
49 loc_result = str_locate_char(p_option_str, 'l');
50 if (loc_result.found)
52 is_verbose = 1;
54 /* Invert "reverse" arg for "-t", the time sorting */
55 if (t_option)
57 r_option = !r_option;
59 if (is_verbose || t_option || F_option || p_subdir_list != 0)
61 do_stat = 1;
63 /* If the filter starts with a . then implicitly enable -a */
64 if (!str_isempty(p_filter_str) && str_get_char_at(p_filter_str, 0) == '.')
66 a_option = 1;
68 /* "Normalise" the incoming base directory string by making sure it
69 * ends in a '/' if it is nonempty
71 if (!str_equal_text(p_base_dir_str, "."))
73 str_copy(&normalised_base_dir_str, p_base_dir_str);
75 if (!str_isempty(&normalised_base_dir_str))
77 unsigned int len = str_getlen(&normalised_base_dir_str);
78 if (str_get_char_at(&normalised_base_dir_str, len - 1) != '/')
80 str_append_char(&normalised_base_dir_str, '/');
83 /* If we're going to need to do time comparisions, cache the local time */
84 if (is_verbose)
86 curr_time = vsf_sysutil_get_time_sec();
88 while (1)
90 int len;
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;
99 len = str_getlen(&s_next_filename_str);
100 if (len > 0 && str_get_char_at(&s_next_filename_str, 0) == '.')
102 if (!a_option && !tunable_force_dot_files)
104 continue;
106 if (!a_option &&
107 ((len == 2 && str_get_char_at(&s_next_filename_str, 1) == '.') ||
108 len == 1))
110 continue;
113 /* Don't show hidden directory entries */
114 if (!vsf_access_check_file_visible(&s_next_filename_str))
116 continue;
118 /* If we have an ls option which is a filter, apply it */
119 if (!str_isempty(p_filter_str))
121 if (!vsf_filename_passes_filter(&s_next_filename_str, p_filter_str))
123 continue;
126 /* Calculate the full path (relative to CWD) for lstat() and
127 * output purposes
129 str_copy(&s_next_path_and_filename_str, &normalised_base_dir_str);
130 str_append_str(&s_next_path_and_filename_str, &s_next_filename_str);
131 if (do_stat)
133 /* lstat() the file. Of course there's a race condition - the
134 * directory entry may have gone away whilst we read it, so
135 * ignore failure to stat
137 int retval = str_lstat(&s_next_path_and_filename_str, &s_p_statbuf);
138 if (vsf_sysutil_retval_is_error(retval))
140 continue;
143 if (is_verbose)
145 static struct mystr s_final_file_str;
146 /* If it's a damn symlink, we need to append the target */
147 str_copy(&s_final_file_str, &s_next_filename_str);
148 if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf))
150 static struct mystr s_temp_str;
151 int retval = str_readlink(&s_temp_str, &s_next_path_and_filename_str);
152 if (retval == 0 && !str_isempty(&s_temp_str))
154 str_append_text(&s_final_file_str, " -> ");
155 str_append_str(&s_final_file_str, &s_temp_str);
158 if (F_option && vsf_sysutil_statbuf_is_dir(s_p_statbuf))
160 str_append_char(&s_final_file_str, '/');
162 build_dir_line(&dirline_str, &s_final_file_str, s_p_statbuf, curr_time);
164 else
166 /* Just emit the filenames - note, we prepend the directory for NLST
167 * but not for LIST
169 str_copy(&dirline_str, &s_next_path_and_filename_str);
170 if (F_option)
172 if (vsf_sysutil_statbuf_is_dir(s_p_statbuf))
174 str_append_char(&dirline_str, '/');
176 else if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf))
178 str_append_char(&dirline_str, '@');
181 str_append_text(&dirline_str, "\r\n");
183 /* Add filename into our sorted list - sorting by filename or time. Also,
184 * if we are required to, maintain a distinct list of direct
185 * subdirectories.
188 static struct mystr s_temp_str;
189 const struct mystr* p_sort_str = 0;
190 const struct mystr* p_sort_subdir_str = 0;
191 if (!t_option)
193 p_sort_str = &s_next_filename_str;
195 else
197 str_alloc_text(&s_temp_str,
198 vsf_sysutil_statbuf_get_sortkey_mtime(s_p_statbuf));
199 p_sort_str = &s_temp_str;
200 p_sort_subdir_str = &s_temp_str;
202 str_list_add(p_list, &dirline_str, p_sort_str);
203 if (p_subdir_list != 0 && vsf_sysutil_statbuf_is_dir(s_p_statbuf))
205 str_list_add(p_subdir_list, &s_next_filename_str, p_sort_subdir_str);
208 } /* END: while(1) */
209 str_list_sort(p_list, r_option);
210 if (p_subdir_list != 0)
212 str_list_sort(p_subdir_list, r_option);
214 str_free(&dirline_str);
215 str_free(&normalised_base_dir_str);
219 vsf_filename_passes_filter(const struct mystr* p_filename_str,
220 const struct mystr* p_filter_str)
222 /* A simple routine to match a filename against a pattern.
223 * This routine is used instead of e.g. fnmatch(3), because we should be
224 * reluctant to trust the latter. fnmatch(3) involves _lots_ of string
225 * parsing and handling. There is broad potential for any given fnmatch(3)
226 * implementation to be buggy.
228 * Currently supported pattern(s):
229 * - any number of wildcards, "*" or "?"
230 * - {,} syntax (not nested)
232 * Note that pattern matching is only supported within the last path
233 * component. For example, searching for /a/b/? will work, but searching
234 * for /a/?/c will not.
236 struct mystr filter_remain_str = INIT_MYSTR;
237 struct mystr name_remain_str = INIT_MYSTR;
238 struct mystr temp_str = INIT_MYSTR;
239 struct mystr brace_list_str = INIT_MYSTR;
240 struct mystr new_filter_str = INIT_MYSTR;
241 int ret = 0;
242 char last_token = 0;
243 int must_match_at_current_pos = 1;
244 str_copy(&filter_remain_str, p_filter_str);
245 str_copy(&name_remain_str, p_filename_str);
247 while (!str_isempty(&filter_remain_str))
249 static struct mystr s_match_needed_str;
250 /* Locate next special token */
251 struct str_locate_result locate_result =
252 str_locate_chars(&filter_remain_str, "*?{");
253 /* Isolate text leading up to token (if any) - needs to be matched */
254 if (locate_result.found)
256 unsigned int indexx = locate_result.index;
257 str_left(&filter_remain_str, &s_match_needed_str, indexx);
258 str_mid_to_end(&filter_remain_str, &temp_str, indexx + 1);
259 str_copy(&filter_remain_str, &temp_str);
260 last_token = locate_result.char_found;
262 else
264 /* No more tokens. Must match remaining filter string exactly. */
265 str_copy(&s_match_needed_str, &filter_remain_str);
266 str_empty(&filter_remain_str);
267 last_token = 0;
269 if (!str_isempty(&s_match_needed_str))
271 /* Need to match something.. could be a match which has to start at
272 * current position, or we could allow it to start anywhere
274 unsigned int indexx;
275 locate_result = str_locate_str(&name_remain_str, &s_match_needed_str);
276 if (!locate_result.found)
278 /* Fail */
279 goto out;
281 indexx = locate_result.index;
282 if (must_match_at_current_pos && indexx > 0)
284 goto out;
286 /* Chop matched string out of remainder */
287 str_mid_to_end(&name_remain_str, &temp_str,
288 indexx + str_getlen(&s_match_needed_str));
289 str_copy(&name_remain_str, &temp_str);
291 if (last_token == '?')
293 if (str_isempty(&name_remain_str))
295 goto out;
297 str_right(&name_remain_str, &temp_str, str_getlen(&name_remain_str) - 1);
298 str_copy(&name_remain_str, &temp_str);
299 must_match_at_current_pos = 1;
301 else if (last_token == '{')
303 struct str_locate_result end_brace =
304 str_locate_char(&filter_remain_str, '}');
305 must_match_at_current_pos = 1;
306 if (end_brace.found)
308 str_split_char(&filter_remain_str, &temp_str, '}');
309 str_copy(&brace_list_str, &filter_remain_str);
310 str_copy(&filter_remain_str, &temp_str);
311 str_split_char(&brace_list_str, &temp_str, ',');
312 while (!str_isempty(&brace_list_str))
314 str_copy(&new_filter_str, &brace_list_str);
315 str_append_str(&new_filter_str, &filter_remain_str);
316 if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str))
318 ret = 1;
319 goto out;
321 str_copy(&brace_list_str, &temp_str);
322 str_split_char(&brace_list_str, &temp_str, ',');
324 goto out;
326 else if (str_isempty(&name_remain_str) ||
327 str_get_char_at(&name_remain_str, 0) != '{')
329 goto out;
331 else
333 str_right(&name_remain_str, &temp_str,
334 str_getlen(&name_remain_str) - 1);
335 str_copy(&name_remain_str, &temp_str);
338 else
340 must_match_at_current_pos = 0;
343 /* Any incoming string left means no match unless we ended on the correct
344 * type of wildcard.
346 if (str_getlen(&name_remain_str) > 0 && last_token != '*')
348 goto out;
350 /* OK, a match */
351 ret = 1;
352 out:
353 str_free(&filter_remain_str);
354 str_free(&name_remain_str);
355 str_free(&temp_str);
356 str_free(&brace_list_str);
357 str_free(&new_filter_str);
358 return ret;
361 static void
362 build_dir_line(struct mystr* p_str, const struct mystr* p_filename_str,
363 const struct vsf_sysutil_statbuf* p_stat, long curr_time)
365 static struct mystr s_tmp_str;
366 filesize_t size = vsf_sysutil_statbuf_get_size(p_stat);
367 /* Permissions */
368 str_alloc_text(p_str, vsf_sysutil_statbuf_get_perms(p_stat));
369 str_append_char(p_str, ' ');
370 /* Hard link count */
371 str_alloc_ulong(&s_tmp_str, vsf_sysutil_statbuf_get_links(p_stat));
372 str_lpad(&s_tmp_str, 4);
373 str_append_str(p_str, &s_tmp_str);
374 str_append_char(p_str, ' ');
375 /* User */
376 if (tunable_hide_ids)
378 str_alloc_text(&s_tmp_str, "ftp");
380 else
382 int uid = vsf_sysutil_statbuf_get_uid(p_stat);
383 struct vsf_sysutil_user* p_user = 0;
384 if (tunable_text_userdb_names)
386 p_user = vsf_sysutil_getpwuid(uid);
388 if (p_user == 0)
390 str_alloc_ulong(&s_tmp_str, (unsigned long) uid);
392 else
394 str_alloc_text(&s_tmp_str, vsf_sysutil_user_getname(p_user));
397 str_rpad(&s_tmp_str, 8);
398 str_append_str(p_str, &s_tmp_str);
399 str_append_char(p_str, ' ');
400 /* Group */
401 if (tunable_hide_ids)
403 str_alloc_text(&s_tmp_str, "ftp");
405 else
407 int gid = vsf_sysutil_statbuf_get_gid(p_stat);
408 struct vsf_sysutil_group* p_group = 0;
409 if (tunable_text_userdb_names)
411 p_group = vsf_sysutil_getgrgid(gid);
413 if (p_group == 0)
415 str_alloc_ulong(&s_tmp_str, (unsigned long) gid);
417 else
419 str_alloc_text(&s_tmp_str, vsf_sysutil_group_getname(p_group));
422 str_rpad(&s_tmp_str, 8);
423 str_append_str(p_str, &s_tmp_str);
424 str_append_char(p_str, ' ');
425 /* Size in bytes */
426 str_alloc_filesize_t(&s_tmp_str, size);
427 str_lpad(&s_tmp_str, 8);
428 str_append_str(p_str, &s_tmp_str);
429 str_append_char(p_str, ' ');
430 /* Date stamp */
431 str_append_text(p_str, vsf_sysutil_statbuf_get_date(p_stat,
432 tunable_use_localtime,
433 curr_time));
434 str_append_char(p_str, ' ');
435 /* Filename */
436 str_append_str(p_str, p_filename_str);
437 str_append_text(p_str, "\r\n");