1/* 2 * Part of Very Secure FTPd 3 * Licence: GPL v2 4 * Author: Chris Evans 5 * ls.c 6 * 7 * Would you believe, code to handle directory listing. 8 */ 9 10#include "sysstr.h" 11#include "ls.h" 12#include "access.h" 13#include "str.h" 14#include "strlist.h" 15#include "sysutil.h" 16#include "tunables.h" 17#include "utility.h" // Jiahao 18#include <stdlib.h> 19#include <string.h> // James 20 21static void build_dir_line(struct mystr* p_str, 22 const struct mystr* p_filename_str, 23 const struct vsf_sysutil_statbuf* p_stat); 24 25void 26vsf_ls_populate_dir_list(const char* session_user, // James 27 struct mystr_list* p_list, 28 struct mystr_list* p_subdir_list, 29 struct vsf_sysutil_dir* p_dir, 30 const struct mystr* p_base_dir_str, 31 const struct mystr* p_option_str, 32 const struct mystr* p_filter_str, 33 int is_verbose) 34{ 35 struct mystr dirline_str = INIT_MYSTR; 36 struct mystr normalised_base_dir_str = INIT_MYSTR; 37 struct str_locate_result loc_result; 38 int a_option; 39 int r_option; 40 int t_option; 41 int F_option; 42 int do_stat = 0; 43 loc_result = str_locate_char(p_option_str, 'a'); 44 a_option = loc_result.found; 45 loc_result = str_locate_char(p_option_str, 'r'); 46 r_option = loc_result.found; 47 loc_result = str_locate_char(p_option_str, 't'); 48 t_option = loc_result.found; 49 loc_result = str_locate_char(p_option_str, 'F'); 50 F_option = loc_result.found; 51 loc_result = str_locate_char(p_option_str, 'l'); 52 if (loc_result.found) 53 { 54 is_verbose = 1; 55 } 56 /* Invert "reverse" arg for "-t", the time sorting */ 57 if (t_option) 58 { 59 r_option = !r_option; 60 } 61 if (is_verbose || t_option || F_option || p_subdir_list != 0) 62 { 63 do_stat = 1; 64 } 65 /* If the filter starts with a . then implicitly enable -a */ 66 if (!str_isempty(p_filter_str) && str_get_char_at(p_filter_str, 0) == '.') 67 { 68 a_option = 1; 69 } 70 /* "Normalise" the incoming base directory string by making sure it 71 * ends in a '/' if it is nonempty 72 */ 73 if (!str_equal_text(p_base_dir_str, ".")) 74 { 75 str_copy(&normalised_base_dir_str, p_base_dir_str); 76 } 77 if (!str_isempty(&normalised_base_dir_str)) 78 { 79 unsigned int len = str_getlen(&normalised_base_dir_str); 80 if (str_get_char_at(&normalised_base_dir_str, len - 1) != '/') 81 { 82 str_append_char(&normalised_base_dir_str, '/'); 83 } 84 } 85 /* If we're going to need to do time comparisions, cache the local time */ 86 if (is_verbose) 87 { 88 vsf_sysutil_update_cached_time(); 89 } 90 91 while (1){ 92 int len; 93 static struct mystr s_next_filename_str; 94 static struct mystr s_next_path_and_filename_str; 95 static struct vsf_sysutil_statbuf* s_p_statbuf; 96// 2007.05 James { 97 str_next_dirent(session_user, str_getbuf(p_base_dir_str), &s_next_filename_str, p_dir); 98 99 if(!strcmp(str_getbuf(&s_next_filename_str), DENIED_DIR)) 100 continue; 101// 2007.05 James } 102 103 if (str_isempty(&s_next_filename_str)){ 104 break; 105 } 106 len = str_getlen(&s_next_filename_str); 107 if (len > 0 && str_get_char_at(&s_next_filename_str, 0) == '.'){ 108 if (!a_option && !tunable_force_dot_files){ 109 continue; 110 } 111 if (!a_option && 112 ((len == 2 && str_get_char_at(&s_next_filename_str, 1) == '.') || 113 len == 1)){ 114 continue; 115 } 116 } 117 118 /* Don't show hidden directory entries */ 119 if (!vsf_access_check_file_visible(&s_next_filename_str)){ 120 continue; 121 } 122 /* If we have an ls option which is a filter, apply it */ 123 if (!str_isempty(p_filter_str)){ 124 if (!vsf_filename_passes_filter(&s_next_filename_str, p_filter_str)){ 125 continue; 126 } 127 } 128 /* Calculate the full path (relative to CWD) for lstat() and 129 * output purposes 130 */ 131 str_copy(&s_next_path_and_filename_str, &normalised_base_dir_str); 132 str_append_str(&s_next_path_and_filename_str, &s_next_filename_str); 133 if (do_stat){ 134 /* lstat() the file. Of course there's a race condition - the 135 * directory entry may have gone away whilst we read it, so 136 * ignore failure to stat 137 */ 138 int retval = str_lstat(&s_next_path_and_filename_str, &s_p_statbuf); 139 if (vsf_sysutil_retval_is_error(retval)){ 140 continue; 141 } 142 } 143 144 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)){ 149 static struct mystr s_temp_str; 150 int retval = str_readlink(&s_temp_str, &s_next_path_and_filename_str); 151 if (retval == 0 && !str_isempty(&s_temp_str)){ 152 str_append_text(&s_final_file_str, " -> "); 153 str_append_str(&s_final_file_str, &s_temp_str); 154 } 155 } 156 if (F_option && vsf_sysutil_statbuf_is_dir(s_p_statbuf)){ 157 str_append_char(&s_final_file_str, '/'); 158 } 159 160 build_dir_line(&dirline_str, &s_final_file_str, s_p_statbuf); 161 } 162 else{ 163 /* Just emit the filenames - note, we prepend the directory for NLST 164 * but not for LIST 165 */ 166 str_copy(&dirline_str, &s_next_path_and_filename_str); 167 if (F_option){ 168 if (vsf_sysutil_statbuf_is_dir(s_p_statbuf)){ 169 str_append_char(&dirline_str, '/'); 170 } 171 else if (vsf_sysutil_statbuf_is_symlink(s_p_statbuf)){ 172 str_append_char(&dirline_str, '@'); 173 } 174 } 175 str_append_text(&dirline_str, "\r\n"); 176 } 177 178 /* Add filename into our sorted list - sorting by filename or time. Also, 179 * if we are required to, maintain a distinct list of direct 180 * subdirectories. 181 */ 182 static struct mystr s_temp_str; 183 const struct mystr* p_sort_str = 0; 184 const struct mystr* p_sort_subdir_str = 0; 185 if (!t_option){ 186 p_sort_str = &s_next_filename_str; 187 } 188 else{ 189 str_alloc_text(&s_temp_str, vsf_sysutil_statbuf_get_sortkey_mtime(s_p_statbuf)); 190 p_sort_str = &s_temp_str; 191 p_sort_subdir_str = &s_temp_str; 192 } 193 194 str_list_add(p_list, &dirline_str, p_sort_str); 195 if (p_subdir_list != 0 && vsf_sysutil_statbuf_is_dir(s_p_statbuf)){ 196 str_list_add(p_subdir_list, &s_next_filename_str, p_sort_subdir_str); 197 } 198 } /* END: while(1) */ 199 200 str_list_sort(p_list, r_option); 201 if (p_subdir_list != 0){ 202 str_list_sort(p_subdir_list, r_option); 203 } 204 205 str_free(&dirline_str); 206 str_free(&normalised_base_dir_str); 207} 208 209int 210vsf_filename_passes_filter(const struct mystr* p_filename_str, 211 const struct mystr* p_filter_str) 212{ 213 /* A simple routine to match a filename against a pattern. 214 * This routine is used instead of e.g. fnmatch(3), because we should be 215 * reluctant to trust the latter. fnmatch(3) involves _lots_ of string 216 * parsing and handling. There is broad potential for any given fnmatch(3) 217 * implementation to be buggy. 218 * 219 * Currently supported pattern(s): 220 * - any number of wildcards, "*" or "?" 221 * - {,} syntax (not nested) 222 * 223 * Note that pattern matching is only supported within the last path 224 * component. For example, searching for /a/b/? will work, but searching 225 * for /a/?/c will not. 226 */ 227 struct mystr filter_remain_str = INIT_MYSTR; 228 struct mystr name_remain_str = INIT_MYSTR; 229 struct mystr temp_str = INIT_MYSTR; 230 struct mystr brace_list_str = INIT_MYSTR; 231 struct mystr new_filter_str = INIT_MYSTR; 232 int ret = 0; 233 char last_token = 0; 234 int must_match_at_current_pos = 1; 235 str_copy(&filter_remain_str, p_filter_str); 236 str_copy(&name_remain_str, p_filename_str); 237 238 while (!str_isempty(&filter_remain_str)) 239 { 240 static struct mystr s_match_needed_str; 241 /* Locate next special token */ 242 struct str_locate_result locate_result = 243 str_locate_chars(&filter_remain_str, "*?{"); 244 /* Isolate text leading up to token (if any) - needs to be matched */ 245 if (locate_result.found) 246 { 247 unsigned int indexx = locate_result.index; 248 str_left(&filter_remain_str, &s_match_needed_str, indexx); 249 str_mid_to_end(&filter_remain_str, &temp_str, indexx + 1); 250 str_copy(&filter_remain_str, &temp_str); 251 last_token = locate_result.char_found; 252 } 253 else 254 { 255 /* No more tokens. Must match remaining filter string exactly. */ 256 str_copy(&s_match_needed_str, &filter_remain_str); 257 str_empty(&filter_remain_str); 258 last_token = 0; 259 } 260 if (!str_isempty(&s_match_needed_str)) 261 { 262 /* Need to match something.. could be a match which has to start at 263 * current position, or we could allow it to start anywhere 264 */ 265 unsigned int indexx; 266 locate_result = str_locate_str(&name_remain_str, &s_match_needed_str); 267 if (!locate_result.found) 268 { 269 /* Fail */ 270 goto out; 271 } 272 indexx = locate_result.index; 273 if (must_match_at_current_pos && indexx > 0) 274 { 275 goto out; 276 } 277 /* Chop matched string out of remainder */ 278 str_mid_to_end(&name_remain_str, &temp_str, 279 indexx + str_getlen(&s_match_needed_str)); 280 str_copy(&name_remain_str, &temp_str); 281 } 282 if (last_token == '?') 283 { 284 if (str_isempty(&name_remain_str)) 285 { 286 goto out; 287 } 288 str_right(&name_remain_str, &temp_str, str_getlen(&name_remain_str) - 1); 289 str_copy(&name_remain_str, &temp_str); 290 must_match_at_current_pos = 1; 291 } 292 else if (last_token == '{') 293 { 294 struct str_locate_result end_brace = 295 str_locate_char(&filter_remain_str, '}'); 296 must_match_at_current_pos = 1; 297 if (end_brace.found) 298 { 299 str_split_char(&filter_remain_str, &temp_str, '}'); 300 str_copy(&brace_list_str, &filter_remain_str); 301 str_copy(&filter_remain_str, &temp_str); 302 str_split_char(&brace_list_str, &temp_str, ','); 303 while (!str_isempty(&brace_list_str)) 304 { 305 str_copy(&new_filter_str, &brace_list_str); 306 str_append_str(&new_filter_str, &filter_remain_str); 307 if (vsf_filename_passes_filter(&name_remain_str, &new_filter_str)) 308 { 309 ret = 1; 310 goto out; 311 } 312 str_copy(&brace_list_str, &temp_str); 313 str_split_char(&brace_list_str, &temp_str, ','); 314 } 315 goto out; 316 } 317 else if (str_isempty(&name_remain_str) || 318 str_get_char_at(&name_remain_str, 0) != '{') 319 { 320 goto out; 321 } 322 else 323 { 324 str_right(&name_remain_str, &temp_str, 325 str_getlen(&name_remain_str) - 1); 326 str_copy(&name_remain_str, &temp_str); 327 } 328 } 329 else 330 { 331 must_match_at_current_pos = 0; 332 } 333 } 334 /* Any incoming string left means no match unless we ended on the correct 335 * type of wildcard. 336 */ 337 if (str_getlen(&name_remain_str) > 0 && last_token != '*') 338 { 339 goto out; 340 } 341 /* OK, a match */ 342 ret = 1; 343out: 344 str_free(&filter_remain_str); 345 str_free(&name_remain_str); 346 str_free(&temp_str); 347 str_free(&brace_list_str); 348 str_free(&new_filter_str); 349 return ret; 350} 351 352static void 353build_dir_line(struct mystr* p_str, const struct mystr* p_filename_str, 354 const struct vsf_sysutil_statbuf* p_stat) 355{ 356 static struct mystr s_tmp_str; 357 char *tmp_filename=NULL; // Jiahao 358 filesize_t size = vsf_sysutil_statbuf_get_size(p_stat); 359 /* Permissions */ 360 str_alloc_text(p_str, vsf_sysutil_statbuf_get_perms(p_stat)); 361 str_append_char(p_str, ' '); 362 /* Hard link count */ 363 str_alloc_ulong(&s_tmp_str, vsf_sysutil_statbuf_get_links(p_stat)); 364 str_lpad(&s_tmp_str, 4); 365 str_append_str(p_str, &s_tmp_str); 366 str_append_char(p_str, ' '); 367 /* User */ 368 if (tunable_hide_ids) 369 { 370 str_alloc_text(&s_tmp_str, "ftp"); 371 } 372 else 373 { 374 int uid = vsf_sysutil_statbuf_get_uid(p_stat); 375 struct vsf_sysutil_user* p_user = 0; 376 if (tunable_text_userdb_names) 377 { 378 p_user = vsf_sysutil_getpwuid(uid); 379 } 380 if (p_user == 0) 381 { 382 str_alloc_ulong(&s_tmp_str, (unsigned long) uid); 383 } 384 else 385 { 386 str_alloc_text(&s_tmp_str, vsf_sysutil_user_getname(p_user)); 387 } 388 } 389 str_rpad(&s_tmp_str, 8); 390 str_append_str(p_str, &s_tmp_str); 391 str_append_char(p_str, ' '); 392 /* Group */ 393 if (tunable_hide_ids) 394 { 395 str_alloc_text(&s_tmp_str, "ftp"); 396 } 397 else 398 { 399 int gid = vsf_sysutil_statbuf_get_gid(p_stat); 400 struct vsf_sysutil_group* p_group = 0; 401 if (tunable_text_userdb_names) 402 { 403 p_group = vsf_sysutil_getgrgid(gid); 404 } 405 if (p_group == 0) 406 { 407 str_alloc_ulong(&s_tmp_str, (unsigned long) gid); 408 } 409 else 410 { 411 str_alloc_text(&s_tmp_str, vsf_sysutil_group_getname(p_group)); 412 } 413 } 414 str_rpad(&s_tmp_str, 8); 415 str_append_str(p_str, &s_tmp_str); 416 str_append_char(p_str, ' '); 417 /* Size in bytes */ 418 str_alloc_filesize_t(&s_tmp_str, size); 419 str_lpad(&s_tmp_str, 8); 420 str_append_str(p_str, &s_tmp_str); 421 str_append_char(p_str, ' '); 422 /* Date stamp */ 423 str_append_text(p_str, vsf_sysutil_statbuf_get_date(p_stat, 424 tunable_use_localtime)); 425 str_append_char(p_str, ' '); 426 /* Filename */ 427// str_append_str(p_str, p_filename_str); // Jiahao 428 tmp_filename = local2remote(str_getbuf(p_filename_str)); 429 if (tmp_filename == NULL) 430 str_append_str(p_str, p_filename_str); 431 else { 432 str_append_text(p_str, tmp_filename); 433 vsf_sysutil_free(tmp_filename); 434 } 435 str_append_text(p_str, "\r\n"); 436} 437 438