forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chooser.cgi
executable file
·240 lines (226 loc) · 5.93 KB
/
chooser.cgi
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
#!/usr/local/bin/perl
# chooser.cgi
# Outputs HTML for a frame-based file chooser
BEGIN { push(@INC, ".."); };
use WebminCore;
@icon_map = ( "c", "text.gif",
"txt", "text.gif",
"pl", "text.gif",
"cgi", "text.gif",
"html", "text.gif",
"htm", "text.gif",
"gif", "image.gif",
"jpg", "image.gif",
"tar", "binary.gif"
);
$trust_unknown_referers = 1;
&init_config();
if (&get_product_name() eq 'usermin') {
&switch_to_remote_user();
}
%access = &get_module_acl();
# Work out root directory
local @uinfo = getpwnam($remote_user);
if (!$access{'root'}) {
$rootdir = $uinfo[7] ? $uinfo[7] : "/";
}
else {
$rootdir = $access{'root'};
$rootdir =~ s/^\~/$uinfo[7]/;
}
# Switch to correct Unix user
if (&supports_users()) {
if (&get_product_name() eq 'usermin') {
# Always run as Usermin login
&switch_to_remote_user();
}
else {
# ACL determines
$fileunix = $access{'fileunix'} || $remote_user;
@uinfo = getpwnam($fileunix);
if (@uinfo) {
&switch_to_unix_user(\@uinfo);
}
}
}
&ReadParse(undef, undef, 1);
if ($gconfig{'os_type'} eq 'windows') {
# On Windows, chroot should be empty if not use, and default path
# should be c:/
if ($in{'chroot'} eq "/") {
$in{'chroot'} = "";
}
if ($rootdir eq "/") {
$rootdir = "c:";
}
}
if ($in{'add'}) {
# Only use last filename by default
$in{'file'} =~ s/\s+$//;
if ($in{'file'} =~ /\n(.*)$/) {
$in{'file'} = $1;
}
}
if ($in{'file'} =~ /^(([a-z]:)?.*\/)([^\/]*)$/i && $in{'file'} !~ /\.\./) {
# File entered is valid
$dir = $1;
$file = $3;
}
else {
# Fall back to default
$dir = $rootdir;
$dir .= '/' if ($dir !~ /\/$/);
$file = "";
}
$add = int($in{'add'});
if (!(-d $in{'chroot'}.$dir)) {
# Entered directory does not exist
$dir = $rootdir.'/';
$file = "";
}
if (!&allowed_dir($dir)) {
# Directory is outside allowed root
$dir = $rootdir.'/';
$file = "";
}
# Work out the top allowed dir
$topdir = $rootdir eq "/" || $rootdir eq "c:" ? $rootdir :
$access{'otherdirs'} ? "/" : $rootdir;
$uchroot = &urlize($in{'chroot'});
$utype = &urlize($in{'type'});
$ufile = &urlize($in{'file'});
if ($in{'frame'} == 0) {
# base frame
&PrintHeader();
if ($in{'type'} == 0) {
print "<title>$text{'chooser_title1'}</title>\n";
}
elsif ($in{'type'} == 1) {
print "<title>$text{'chooser_title2'}</title>\n";
}
print "<frameset rows='*,50'>\n";
print "<frame marginwidth=5 marginheight=5 name=topframe ",
"src=\"chooser.cgi?frame=1&file=".$ufile.
"&chroot=".$uchroot."&type=".$utype."&add=$add\">\n";
print "<frame marginwidth=0 marginheight=0 name=bottomframe ",
"src=\"chooser.cgi?frame=2&file=".$ufile.
"&chroot=".$uchroot."&type=".$utype."&add=$add\" scrolling=no>\n";
print "</frameset>\n";
}
elsif ($in{'frame'} == 1) {
# List of files in this directory
&popup_header();
print <<EOF;
<script>
function fileclick(f, d)
{
curr = top.frames[1].document.forms[0].elements[1].value;
if (curr == f) {
// Double-click! Enter directory or select file
if (d) {
// Enter this directory
location = "chooser.cgi?frame=1&add=$add&chroot=$uchroot&type=$utype&file="+f+"/";
}
else {
// Select this file and close the window
if ($add == 0) {
top.opener.ifield.value = f;
}
else {
if (top.opener.ifield.value != "") {
top.opener.ifield.value += "\\n";
}
top.opener.ifield.value += f;
}
top.close();
}
}
else {
top.frames[1].document.forms[0].elements[1].value = f;
}
}
function parentdir(p)
{
top.frames[1].document.forms[0].elements[1].value = p;
location = "chooser.cgi?frame=1&chroot=$uchroot&type=$utype&file="+p;
}
</script>
EOF
print "<b>",&text('chooser_dir', &html_escape($dir)),"</b>\n";
opendir(DIR, $in{'chroot'}.$dir) ||
&popup_error(&text('chooser_eopen', "$!"));
print &ui_columns_start(undef, 100);
foreach $f (sort { $a cmp $b } readdir(DIR)) {
$path = "$in{'chroot'}$dir$f";
if ($f eq ".") { next; }
if ($f eq ".." && ($dir eq "/" || $dir eq $topdir.'/')) { next; }
if ($f =~ /^\./ && $f ne ".." && $access{'nodot'}) { next; }
if (!(-d $path) && $in{'type'} == 1) { next; }
@st = stat($path);
$isdir = 0; undef($icon);
if (-d $path) { $icon = "dir.gif"; $isdir = 1; }
elsif ($path =~ /\.([^\.\/]+)$/) { $icon = $icon_map{$1}; }
if (!$icon) { $icon = "unknown.gif"; }
if ($f eq "..") {
$dir =~ /^(.*\/)[^\/]+\/$/;
$link = "<a href=\"\" onClick='parentdir(\""."e_escape($1)."\"); return false'>";
}
else {
$link = "<a href=\"\" onClick='fileclick(\""."e_escape("$dir$f")."\", $isdir); return false'>";
}
local @cols;
push(@cols, "$link<img border=0 src=$gconfig{'webprefix'}/images/$icon></a>");
push(@cols, "$link".&html_escape($f)."</a>");
push(@cols, &nice_size($st[7]));
@tm = localtime($st[9]);
push(@cols, sprintf "<tt>%.2d/%s/%.4d</tt>",
$tm[3], $text{'smonth_'.($tm[4]+1)}, $tm[5]+1900);
push(@cols, sprintf "<tt>%.2d:%.2d</tt>", $tm[2], $tm[1]);
print &ui_columns_row(\@cols);
}
closedir(DIR);
print &ui_columns_end();
&popup_footer();
}
elsif ($in{'frame'} == 2) {
# Current file and OK/cancel buttons
&popup_header();
print <<EOF;
<script>
function filechosen()
{
if ($add == 0) {
top.opener.ifield.value = document.forms[0].path.value;
}
else {
if (top.opener.ifield.value != "") {
top.opener.ifield.value += "\\n";
}
top.opener.ifield.value += document.forms[0].path.value;
}
top.close();
}
</script>
EOF
print &ui_form_start(undef, undef, undef,
"onSubmit='filechosen(); return false'");
print &ui_table_start(undef, "width=100%", 2);
print &ui_table_row(undef,
&ui_submit($text{'chooser_ok'})." ".
&ui_textbox("path", $dir.$file, 45, 0, undef,
"style='width:90%'"), 2);
print &ui_table_end();
print &ui_form_end();
&popup_footer();
}
# allowed_dir(dir)
# Returns 1 if some directory should be listable
sub allowed_dir
{
local ($dir) = @_;
return 1 if ($rootdir eq "" || $rootdir eq "/" || $rootdir eq "c:");
foreach my $allowed ($rootdir, split(/\t+/, $access{'otherdirs'})) {
return 1 if (&is_under_directory($allowed, $dir));
}
return 0;
}