-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfilemin-lib.pl
More file actions
317 lines (287 loc) · 9.06 KB
/
filemin-lib.pl
File metadata and controls
317 lines (287 loc) · 9.06 KB
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
# filemin-lib.pl
BEGIN {
push(@INC, "..");
my $sp = __FILE__;
$sp =~ s!^(.*/)[^/]*$!\1!;
push(@INC, "$sp/lib");
};
use WebminCore;
&init_config();
use Encode qw(decode encode);
use POSIX;
use lib './lib';
use File::Basename;
use File::MimeInfo;
use Mojo::JSON;
$templates_path = "unauthenticated/templates";
sub get_paths {
my @errors;
%access = &get_module_acl();
# Switch to the correct user
if (&get_product_name() eq 'usermin') {
# In Usermin, the module only ever runs as the connected user
&switch_to_remote_user();
&create_user_config_dirs();
}
elsif ($access{'work_as_root'}) {
# Root user, so no switching
@remote_user_info = getpwnam('root');
}
elsif ($access{'work_as_user'}) {
# A specific user
@remote_user_info = getpwnam($access{'work_as_user'});
@remote_user_info ||
push @errors, "Unix user $access{'work_as_user'} does not exist!";
&switch_to_unix_user(\@remote_user_info);
}
else {
# The Webmin user we are connected as
&switch_to_remote_user();
}
# Get and check allowed paths
@allowed_paths = split(/\s+/, $access{'allowed_paths'});
if (&get_product_name() eq 'usermin') {
# Add paths from Usermin config
push(@allowed_paths, split(/\t+/, $config{'allowed_paths'}));
}
if($remote_user_info[0] eq 'root' || $allowed_paths[0] eq '$ROOT') {
# Assume any directory can be accessed
$base = "/";
@allowed_paths = ( $base );
} else {
@allowed_paths = map { $_ eq '$HOME' ? @remote_user_info[7] : $_ }
@allowed_paths;
@allowed_paths = map { s/\$USER/$remote_user/g; $_ } @allowed_paths;
if (scalar(@allowed_paths) == 1) {
$base = $allowed_paths[0];
} else {
$base = '/';
}
}
$path = $in{'path'} ? $in{'path'} : '';
$path =~ s/\.\.//g;
$path = &simplify_path($path);
$cwd = &simplify_path(&resolve_links($base.$path));
# Work out max upload size
if (&get_product_name() eq 'usermin') {
$upload_max = $config{'max'};
} else {
$upload_max = $access{'max'};
}
# Check that current directory is one of those that is allowed
my $error = 1;
for $allowed_path (@allowed_paths) {
if (&is_under_directory($allowed_path, $cwd) ||
$allowed_path =~ /^$cwd/) {
$error = 0;
}
}
if ($error) {
push @errors, &text('notallowed', &html_escape($cwd),
&html_escape(join(" , ", @allowed_paths)));
}
if (index($cwd, $base) == -1)
{
$cwd = $base;
}
# Not really elegant, but working :D
if (scalar(@errors) > 0) {
$result = '';
foreach $error(@errors) {
$result.= "$error\\n";
}
print_ajax_header();
print '{"error": "'.$result.'"}';
exit;
}
# Initiate per user config
$confdir = "$remote_user_info[7]/.filemin";
if(!-e "$confdir/.config") {
# &read_file_cached("$module_root_directory/defaultuconf", \%userconfig);
} else {
# &read_file_cached("$confdir/.config", \%userconfig);
}
}
sub print_template {
$template_name = @_[0];
if (open(my $fh, '<:encoding(UTF-8)', "$templates_path/$template_name")) {
while (my $row = <$fh>) {
print (eval "qq($row)");
}
close($fh);
} else {
print "$text{'error_load_template'} '$template_name' $!";
}
}
sub get_template {
$template_name = @_[0];
my $result = "";
if (open(my $fh, '<:encoding(UTF-8)', "$templates_path/$template_name")) {
while (my $row = <$fh>) {
$result .= (eval "qq($row)");
}
close($fh);
} else {
$result = "$text{'error_load_template'} '$template_name' $!";
}
return $result;
}
# get_bookmarks()
# Return list of bookmarks made by user as set of HTML <li>
sub get_bookmarks {
$confdir = get_config_dir();
if(!-e "$confdir/.bookmarks") {
return "<li><a>$text{'no_bookmarks'}</a></li>";
}
my $bookmarks = &read_file_lines($confdir.'/.bookmarks', 1);
if(scalar(@{$bookmarks}) == 0) {
return "<li><a>$text{'no_bookmarks'}</a></li>";
}
$result = '';
foreach $bookmark(@$bookmarks) {
$result.= "<li><a data-item='goto' data-original-title='$bookmark'>$bookmark</a><li>";
}
return $result;
}
# get_config_dir()
# Returns the directory for user config/bookmarks/copy & paste storage
sub get_config_dir
{
if (&get_product_name() eq 'usermin') {
return $user_module_config_directory;
}
else {
my $tmpdir = "$remote_user_info[7]/.filemin";
&make_dir($tmpdir, 0700) if (!-d $tmpdir);
return $tmpdir;
}
}
# get_paste_buffer_file()
# Returns the location of the file for temporary copy/paste state
sub get_paste_buffer_file
{
if (&get_product_name() eq 'usermin') {
return $user_module_config_directory."/.buffer";
}
else {
my $tmpdir = "$remote_user_info[7]/.filemin";
&make_dir($tmpdir, 0700) if (!-d $tmpdir);
return $tmpdir."/.buffer";
}
}
sub print_ajax_header {
print "Content-Security-Policy: script-src 'self' 'unsafe-inline'; frame-src 'self'\n";
print "Content-type: application/json; Charset=utf-8\n\n";
}
sub filemin_progress_callback {
if ($_[0] == 2) {
# Got size
print $progress_callback_prefix;
if ($_[1]) {
$progress_size = $_[1];
$progress_step = int($_[1] / 10);
print &text('progress_size2', $progress_callback_url,
&nice_size($progress_size)),"\n";
}
else {
$progress_size = undef;
print &text('progress_nosize', $progress_callback_url),"\n";
}
$last_progress_time = $last_progress_size = undef;
}
elsif ($_[0] == 3) {
# Got data update
if ($progress_size) {
# And we have a size to compare against
my $st = int(($_[1] * 10) / $progress_size);
my $time_now = time();
if ($st != $progress_step ||
$time_now - $last_progress_time > 60) {
# Show progress every 10% or 60 seconds
print &text('progress_datan', &nice_size($_[1]),
int($_[1]*100/$progress_size)),"\n";
$last_progress_time = $time_now;
}
$progress_step = $st;
}
else {
# No total size .. so only show in 1M jumps
if ($_[1] > $last_progress_size+1024*1024) {
print &text('progress_data2n',
&nice_size($_[1])),"\n";
$last_progress_size = $_[1];
}
}
}
elsif ($_[0] == 4) {
# All done downloading
print $progress_callback_prefix,&text('progress_done'),"\n";
}
elsif ($_[0] == 5) {
# Got new location after redirect
$progress_callback_url = $_[1];
}
elsif ($_[0] == 6) {
# URL is in cache
$progress_callback_url = $_[1];
print &text('progress_incache', $progress_callback_url),"\n";
}
}
# Simple hash to JSON conversion
# sub to_json {
# my %hash = @_;
# return "{".join(q{,}, map{qq{"$_":"$hash{$_}"}} keys %hash)."}";
# }
sub oct_to_symbolic {
my $permissions = $_[0];
my $sup = substr $permissions, 0, 1;
my $res = "";
if(($sup & 4) >> 2) { $res .= 'u+s,' } else { $res .= 'u-s,' }
if(($sup & 2) >> 1) {$res .= 'g+s,'} else { $res .= 'g-s,' }
if($sup & 1) { $res .= '+t,' } else { $res .= '-t,' }
my $usr = substr $permissions, 1, 1;
$res .= "u";
if(($usr & 4) >> 2) { $res .= '+r' } else { $res .= '-r' }
if(($usr & 2) >> 1) { $res .= '+w' } else { $res .= '-w' }
if($usr & 1) { $res .= '+x,' } else { $res .= '-x,' }
my $grp = substr $permissions, 2, 1;
$res .= "g";
if(($grp & 4) >> 2) { $res .= '+r' } else { $res .= '-r' }
if(($grp & 2) >> 1) { $res .= '+w' } else { $res .= '-w' }
if($grp & 1) { $res .= '+x,' } else { $res .= '-x,' }
my $oth = substr $permissions, 3, 1;
$res .= "o";
if(($oth & 4) >> 2) { $res .= '+r' } else { $res .= '-r' }
if(($oth & 2) >> 1) { $res .= '+w' } else { $res .= '-w' }
if($oth & 1) { $res .= '+x' } else { $res .= '-x' }
return $res;
}
sub suggest_filename {
my ($cwd, $name) = @_;
if (-e "$cwd/$name") {
if(-d "$cwd/$name") {
return suggest_filename($cwd, $name."_copy");
} else {
my $mime = mimetype("$cwd/$name");
my $ext = File::MimeInfo::extensions($mime);
$name =~ s/\.$ext//;
$ext = $ext ? ".$ext" : "";
return suggest_filename($cwd, $name."_copy".$ext);
}
}
return $name;
}
sub status {
my($status, $message) = @_;
if(ref($message) eq "ARRAY") {
$message = join(', ', @{$message});
}
return Mojo::JSON::to_json({$status => $message});
}
sub sanitize {
my $param = @_[0];
$param =~ s/\.\.//g;
$param = &simplify_path($param);
return $param;
}
1;