-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirlist.c
321 lines (292 loc) · 8.58 KB
/
dirlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <config.h>
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#include <unistd.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <mystring.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include <pwd.h>
#include <grp.h>
#include <errno.h>
/* #include <glob.h> */
#include <fnmatch.h>
#include "cwd.h"
#include "options.h"
#include "main.h"
#include "login.h"
#include "dirlist.h"
struct hidegroup *hidegroups = NULL;
void add_to_hidegroups(int gid)
{
static struct hidegroup *tmp = NULL;
if (tmp)
tmp = tmp->next = malloc(sizeof(struct hidegroup));
else
hidegroups = tmp = malloc(sizeof(struct hidegroup));
if (tmp)
{
tmp->next = NULL;
tmp->data = gid;
}
}
void hidegroups_init()
{
char *foo = strdup(config_getoption("HIDE_GROUP"));
char *foo_save = foo;
char *bar;
struct group *tmpgrp;
while ((bar = strtok(foo, ","))) {
foo = NULL; /* strtok requirement */
if ((strcmp(bar, "0")) && (!strtoul(bar, NULL, 10))) {
/* bar is not numeric */
if ((tmpgrp = getgrnam(bar)))
add_to_hidegroups(tmpgrp->gr_gid);
} else
if (strtoul(bar, NULL, 10))
add_to_hidegroups(strtoul(bar, NULL, 10));
}
free(foo_save);
}
void hidegroups_end()
{
struct hidegroup *tmp = hidegroups;
if (hidegroups)
while (hidegroups->next) {
tmp = hidegroups->next;
free(hidegroups);
hidegroups = tmp;
}
}
void bftpd_stat(char *name, FILE * client)
{
struct stat statbuf;
char temp[MAXCMD + 6], linktarget[MAXCMD + 5], perm[12], timestr[17],
uid[USERLEN + 1], gid[USERLEN + 1];
struct tm filetime;
struct tm *tea_time, *local_time;
time_t t;
if (lstat(name, (struct stat *) &statbuf) == -1) { // used for command_stat
fprintf(client, "213-Error: %s.\n", strerror(errno));
return;
}
#ifdef S_ISLNK
if (S_ISLNK(statbuf.st_mode)) {
strcpy(perm, "lrwxrwxrwx");
memset(temp, '\0', sizeof(temp));
readlink(name, temp, sizeof(temp) - 2);
temp[MAXCMD] = '\0';
// temp[readlink(name, temp, sizeof(temp) - 1)] = '\0';
sprintf(linktarget, " -> %s", temp);
} else {
#endif
strcpy(perm, "----------");
if (S_ISDIR(statbuf.st_mode))
perm[0] = 'd';
if (statbuf.st_mode & S_IRUSR)
perm[1] = 'r';
if (statbuf.st_mode & S_IWUSR)
perm[2] = 'w';
if (statbuf.st_mode & S_IXUSR)
perm[3] = 'x';
if (statbuf.st_mode & S_IRGRP)
perm[4] = 'r';
if (statbuf.st_mode & S_IWGRP)
perm[5] = 'w';
if (statbuf.st_mode & S_IXGRP)
perm[6] = 'x';
if (statbuf.st_mode & S_IROTH)
perm[7] = 'r';
if (statbuf.st_mode & S_IWOTH)
perm[8] = 'w';
if (statbuf.st_mode & S_IXOTH)
perm[9] = 'x';
linktarget[0] = '\0';
#ifdef S_ISLNK
}
#endif
/* memcpy(&filetime, localtime(&(statbuf.st_mtime)), sizeof(struct tm)); */
local_time = localtime(&(statbuf.st_mtime));
if (! local_time) return;
memcpy(&filetime, local_time, sizeof(struct tm));
time(&t);
tea_time = localtime(&t);
if (! tea_time) return;
/* if (filetime.tm_year == localtime(&t)->tm_year) */
if (filetime.tm_year == tea_time->tm_year)
mystrncpy(timestr, ctime(&(statbuf.st_mtime)) + 4, 12);
else
strftime(timestr, sizeof(timestr), "%b %d %G", &filetime);
mygetpwuid(statbuf.st_uid, passwdfile, uid)[8] = 0;
mygetpwuid(statbuf.st_gid, groupfile, gid)[8] = 0;
fprintf(client, "%s %3i %-8s %-8s %llu %s %s%s\r\n", perm,
(int) statbuf.st_nlink, uid, gid,
(unsigned long long) statbuf.st_size,
timestr, name, linktarget);
}
void dirlist_one_file(char *name, FILE *client, char verbose)
{
struct stat statbuf;
struct hidegroup *tmp = hidegroups;
char *filename_index; /* place where filename starts in path */
if (!stat(name, (struct stat *) &statbuf)) {
if (tmp)
do {
if (statbuf.st_gid == tmp->data)
return;
} while ((tmp = tmp->next));
}
/* find start of filename after path */
filename_index = strrchr(name, '/');
if (filename_index)
filename_index++; /* goto first character after '/' */
else
filename_index = name;
if (verbose)
bftpd_stat(name, client);
else
fprintf(client, "%s\r\n", filename_index);
}
void dirlist(char *name, FILE * client, char verbose, int show_hidden)
{
DIR *directory = NULL;
FILE *can_see_file;
int show_nonreadable_files = FALSE;
int show_hidden_files = FALSE;
int skip_file, file_is_hidden;
char *local_cwd = NULL;
char *pattern = NULL, *short_pattern;
/* int i; */
struct dirent *dir_entry;
/* glob_t globbuf; */
if ( (show_hidden) &&
(! strcasecmp( config_getoption("SHOW_HIDDEN_FILES"), "yes") ) )
show_hidden_files = TRUE;
/* check for always show hidden */
if (! strcasecmp( config_getoption("SHOW_HIDDEN_FILES"), "always") )
show_hidden_files = TRUE;
if (! strcasecmp( config_getoption("SHOW_NONREADABLE_FILES"), "yes") )
show_nonreadable_files = TRUE;
if ((strstr(name, "/.")) && strchr(name, '*'))
return; /* DoS protection */
if ((directory = opendir(name)))
{
closedir(directory);
local_cwd = bftpd_cwd_getcwd();
if ( chdir(name) == -1)
fprintf(client, "Chdir failed: %s\n", strerror(errno));
}
else
pattern = name;
/*
glob("*", 0, NULL, &globbuf);
if (show_hidden_files)
glob(".*", GLOB_APPEND, NULL, &globbuf);
}
else
{
if ( (name[0] == '*') && (show_hidden_files) )
{
glob(name, 0, NULL, &globbuf);
glob(".*", GLOB_APPEND, NULL, &globbuf);
}
else
glob(name, 0, NULL, &globbuf);
}
*/
/*
for (i = 0; i < globbuf.gl_pathc; i++)
{
if (! show_nonreadable_files)
{
if ( (can_see_file = fopen(globbuf.gl_pathv[i], "r") ) == NULL)
continue;
else
fclose(can_see_file);
}
dirlist_one_file(globbuf.gl_pathv[i], client, verbose);
}
globfree(&globbuf);
*/
directory = opendir(".");
if (directory)
{
dir_entry = readdir(directory);
while (dir_entry)
{
/* This check makes sure we skipped named pipes,
which cannot be opened like normal files
without hanging the server. -- Jesse
*/
#ifndef __minix
if (dir_entry->d_type != DT_FIFO)
{
#endif
can_see_file = fopen(dir_entry->d_name, "r");
if (can_see_file)
fclose(can_see_file);
file_is_hidden = (dir_entry->d_name[0] == '.') ? TRUE : FALSE;
skip_file = TRUE;
if ( (! file_is_hidden) || (show_hidden_files) )
{
if ( (can_see_file) || (show_nonreadable_files) )
{
if (pattern)
{
/* strip leading path */
short_pattern = strrchr(pattern, '/');
if (short_pattern)
short_pattern++;
else
short_pattern = pattern;
skip_file = fnmatch(short_pattern, dir_entry->d_name,
FNM_NOESCAPE);
}
else
skip_file = FALSE;
}
}
if (! skip_file)
dirlist_one_file(dir_entry->d_name, client, verbose);
#ifndef __minix
}
#endif
dir_entry = readdir(directory);
}
closedir(directory);
} /* unable to open directory */
if (local_cwd) {
if (chdir(local_cwd) == -1)
fprintf(client, "Chdir failed: %s\n", strerror(errno));
free(local_cwd);
}
}