forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_mount.cgi
executable file
·474 lines (443 loc) · 13.2 KB
/
save_mount.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/local/bin/perl
# save_mount.cgi
# Save or create a mount. When saving an existing mount, at lot of different
# things can happen.
require './mount-lib.pl';
&error_setup($text{'save_err'});
&ReadParse();
$| = 1;
# Check for redirect to proc module to list processes on the FS
if ($in{'lsof'}) {
&redirect("../proc/index_search.cgi?mode=3&fs=".&urlize($in{'lsoffs'}));
return;
}
# check inputs
if ($in{type} ne "swap") {
if ($in{directory} !~ /^\//) {
if (@access_fs && $in{directory}) {
# Assume relative to allowed dir
$in{directory} = $access_fs[0]."/".$in{directory};
}
else {
&error(&text('save_edirname', $in{'directory'}));
}
}
if (-r $in{'directory'} && !(-d $in{'directory'})) {
&error(&text('save_edir', $in{'directory'}));
}
# non-existant directories get created later
}
else {
# for swap files, set the directory to 'swap'
$in{directory} = "swap";
}
&can_edit_fs($in{'directory'}, undef, $in{'type'}, undef, 1) &&
!$access{'only'} || &error($text{'edit_ecannot'});
$access{'create'} || defined($in{'old'}) || &error($text{'edit_ecannot2'});
# Get user choices
@mmodes = &mount_modes($in{type});
$msave = ($mmodes[0]==0 ? 0 : $in{msave});
$mnow = ($mmodes[1]==0 ? $msave : $in{mmount});
foreach $f (&files_to_lock()) {
&lock_file($f);
}
if (defined($in{old})) {
# Saving an existing mount
if ($in{temp}) { @mlist = &list_mounted(); }
else { @mlist = &list_mounts(); }
@mold = @{$mlist[$in{old}]};
if (!$mnow && !$in{oldmnow} && !$msave) {
# Not mounted, so remove from fstab without checking
$dev = $mold[1];
}
else {
# Changing an existing mount
$dev = &check_location($in{'type'});
&parse_options($mold[2], $mold[3]);
$opts = &check_options($in{'type'}, $dev, $in{'directory'});
@minfo = ($in{'directory'}, $dev, $in{'type'}, $opts,
$mmodes[2] ? $in{'order'} : "-",
$in{'msave'}==2||$mmodes[0]==1 ? "yes" : "no");
}
# Check for change in device
if ($mold[1] ne $dev) {
# Device has changed..check it
if (!&multiple_mount($minfo[2]) && &get_mounted("*", $dev)>=0) {
&error(&text('save_ealready', $dev));
}
if (!&multiple_mount($minfo[2]) && &get_mount("*", $dev) != -1){
&error(&text('save_ealready2', $dev));
}
$changed = 1;
}
# Check for change in directory
if ($in{type} ne "swap" && $mold[0] ne $in{directory}) {
# Directory has changed.. check it too
if (&get_mounted($in{directory}, "*")>=0) {
&error(&text('save_ealready3', $in{'directory'}));
}
if (&get_mount($in{directory}, "*") != -1) {
&error(&text('save_ealready4', $in{'directory'}));
}
$changed = 1;
if (!(-d $in{directory})) {
# Create the new directory
&lock_file($in{directory});
&make_dir($in{directory}, 0755) ||
&error(&text('save_emkdir',
$in{'directory'}, $!));
&unlock_file($in{directory});
$made_dir = 1;
}
}
# Check for change in current mount status
if ($in{'oldmnow'} && $mmodes[3] == 1) {
# Mounted, and cannot be unmounted
}
elsif ($in{'oldmnow'} && !$mnow) {
# Just been unmounted..
if ($error = &unmount_dir($mold[0], $mold[1], $in{'type'},
$mold[3], $in{'force'})) {
if (!$in{'force'} &&
$error =~ /busy|Invalid argument/ &&
defined(&can_force_unmount_dir) &&
&can_force_unmount_dir(@mold)) {
# Mount is busy.. most likely because it is
# currently in use. Offer the user a choice to
# forcibly un-mount
&ui_print_header(undef, $text{'edit_title'}, "");
print &text('save_force', "<tt>$mold[0]</tt>"),
"<p>\n";
print "<form action=save_mount.cgi>\n";
print "<input type=hidden name=force ",
"value=1>\n";
foreach $k (keys %in) {
print "<input type=hidden name=$k ",
"value=\"$in{$k}\">\n";
}
print "<center>\n";
print &ui_submit($text{'save_fapply'}),"\n";
print "</center>\n";
print "</form>\n";
&ui_print_footer("", $text{'index_return'});
exit;
}
else {
&error(&text('save_eumount', $error));
}
}
@tlog = ( "umount", "dir", $mold[0],
{ 'dir' => $mold[0], 'dev' => $mold[1],
'type' => $mold[2], 'opts' => $mold[3] } );
}
elsif ($mnow && !$in{oldmnow}) {
# Just been mounted..
if ($error = &mount_dir(@minfo)) {
&error(&text('save_emount', $error));
}
@tlog = ( "mount", "dir", $minfo[0],
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
elsif (!$mnow && !$in{oldmnow}) {
# Not mounted, and doesn't need to be
}
elsif ($mold[0] eq $minfo[0] && $mold[1] eq $minfo[1] &&
&diff_opts($mold[3], $minfo[3]) && !$in{'perm_only'} &&
defined(&os_remount_dir)) {
# Only options have changed .. just call remount
if ($error = &remount_dir(@minfo)) {
&error(&text('save_eremount', $error));
}
@tlog = ( "remount", "dir", $minfo[0],
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
elsif (($mold[0] ne $minfo[0] || $mold[1] ne $minfo[1] ||
&diff_opts($mold[3], $minfo[3])) && !$in{'perm_only'}) {
# Need to unmount/mount to apply new options
if ($error = &unmount_dir($mold[0], $mold[1], $in{type})) {
if ($error =~ /busy|Invalid argument/ && $msave) {
# Mount is busy.. most likely because it is
# currently in use. Offer the user a choice
# to update only the fstab file, rather than
# the real mount
&ui_print_header(undef, $text{'edit_title'}, "");
print &text('save_perm', "<tt>$mold[0]</tt>"),
"<p>\n";
print "<form action=save_mount.cgi>\n";
print "<input type=hidden name=perm_only ",
"value=1>\n";
foreach $k (keys %in) {
print "<input type=hidden name=$k ",
"value=\"$in{$k}\">\n";
}
print "<center>\n";
print &ui_submit($text{'save_apply'}),"\n";
print "</center>\n";
print "</form>\n";
&ui_print_footer("", $text{'index_return'});
exit;
}
else { &error(&text('save_eremount', $error)); }
}
if ($error = &mount_dir(@minfo)) {
&error(&text('save_eremount', $error));
}
@tlog = ( "remount", "dir", $minfo[0],
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
# Check for change in permanence
if ($in{oldmsave} && !$msave) {
# Delete from mount table
&delete_mount($in{old});
@plog = ( "delete", "dir", $in{'directory'},
{ 'dir' => $mold[0], 'dev' => $mold[1],
'type' => $mold[2], 'opts' => $mold[3] } );
}
elsif ($msave && !$in{oldmsave}) {
# Add to mount table
&create_mount(@minfo);
@plog = ( "create", "dir", $in{'directory'},
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
elsif (!$msave && !$in{oldmsave}) {
# Not in mount table
}
elsif ($mold[0] ne $minfo[0] || $mold[1] ne $minfo[1] ||
$mold[4] != $minfo[4] || $mold[5] ne $minfo[5] ||
&diff_opts($mold[3], $minfo[3])) {
# Apply any changes in mount options
&change_mount($in{old}, @minfo);
@plog = ( "modify", "dir", $in{'directory'},
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
# If no longer mounted, remove the dir
if (&get_mounted(@mold) < 0) {
&delete_unmounted(@mold);
}
}
elsif (defined($in{'old'})) {
# Doing a simple modification to a mount
if ($in{temp}) {
@mlist = &list_mounted();
$mnow = 1;
}
else {
@mlist = &list_mounts();
$msave = 1;
$now = 1 if ($in{'oldmnow'});
}
@mold = @{$mlist[$in{old}]};
if ($in{'umount'}) {
# Just unmount the filesystem
if ($error = &unmount_dir($mold[0], $mold[1], $in{type})) {
&error(&text('save_eumount', $error));
}
&delete_unmounted(@mold);
$mnow = 0;
}
elsif ($in{'mount'}) {
# Just mount the filesystem
if ($error = &mount_dir(@mold)) {
&error(&text('save_emount', $error));
}
$mnow = 1;
}
elsif ($in{'perm'}) {
# Add to permanent mount list
&create_mount($mold[0], $mold[1], $mold[2], $mold[3],
2, "yes");
$msave = 1;
}
elsif ($in{'delete'}) {
if ($in{'oldmnow'}) {
# Unmount first
if ($error = &unmount_dir($mold[0], $mold[1],
$in{type})) {
&error(&text('save_eumount', $error));
}
$mnow = 0;
}
# Remove from permanent list
&delete_mount($in{'old'});
&delete_unmounted(@mold);
$msave = 0;
}
else {
# Updating the mount in some way ..
# Check the mount source
$dev = &check_location($in{'type'});
&parse_options($mold[2], $mold[3]);
if (defined($access{'opts'}) &&
$access{'opts'} !~ /$in{'type'}/) {
# Just use existing options
local @opts;
foreach $k (keys %options) {
if ($options{$k} eq '') { push(@opts, $k); }
else { push(@opts, "$k=$options{$k}"); }
}
$opts = @opts ? join(",", @opts) : "-";
}
else {
# Get options from the user
$opts = &check_options($in{'type'}, $dev,
$in{'directory'});
}
@minfo = ($in{'directory'}, $dev, $in{'type'}, $opts, 2, 'yes');
# Check for change in device
if ($mold[1] ne $dev) {
# Device has changed..check it
if (!&multiple_mount($minfo[2]) &&
&get_mounted("*", $dev)>=0) {
&error(&text('save_ealready', $dev));
}
if (!&multiple_mount($minfo[2]) &&
&get_mount("*", $dev) != -1){
&error(&text('save_ealready2', $dev));
}
$changed = 1;
}
# Check for change in directory
if ($in{type} ne "swap" && $mold[0] ne $in{directory}) {
# Directory has changed.. check it too
if (&get_mounted($in{directory}, "*")>=0) {
&error(&text('save_ealready3',
$in{'directory'}));
}
if (&get_mount($in{directory}, "*") != -1) {
&error(&text('save_ealready4',
$in{'directory'}));
}
$changed = 1;
if (!(-d $in{directory})) {
# Create the new directory
&lock_file($in{directory});
&make_dir($in{directory}, 0755) ||
&error(&text('save_emkdir',
$in{'directory'}, $!));
&unlock_file($in{directory});
$made_dir = 1;
}
}
if ($in{'oldmnow'} && ($mold[0] ne $minfo[0] ||
$mold[1] ne $minfo[1] || &diff_opts($mold[3], $minfo[3]))) {
# Need to unmount/mount to apply new options
if ($error=&unmount_dir($mold[0], $mold[1], $in{type})){
&error(&text('save_eremount', $error));
}
if ($error = &mount_dir(@minfo)) {
&error(&text('save_eremount', $error));
}
}
if ($in{'oldmsave'}) {
# Change entry in fstab
&change_mount($in{'old'}, @minfo);
}
}
}
else {
# Creating a new mount, complex interface
$dev = &check_location($in{type});
&parse_options($minfo[3]);
$opts = &check_options($in{type}, $dev, $in{'directory'});
@minfo = ($in{directory}, $dev, $in{type}, $opts,
$mmodes[2] ? $in{order} : "-",
$in{msave}==2||$mmodes[0]==1 ? "yes" : "no");
# Check if anything is being done
if (!$msave && !$mnow) {
&error($text{'save_enone'});
}
# Check if the device is in use
if (!&multiple_mount($minfo[2]) && &get_mounted("*", $dev)>=0) {
&error(&text('save_ealready', $dev));
}
if (!&multiple_mount($minfo[2]) && &get_mount("*", $dev) != -1) {
&error(&text('save_ealready2', $dev));
}
# Check if the directory is in use
if ($in{type} ne "swap") {
if (&get_mounted($in{directory}, "*")>=0) {
&error(&text('save_ealready2', $in{'directory'}));
}
if (&get_mount($in{directory}, "*") != -1) {
&error(&text('save_ealready3', $in{'directory'}));
}
}
# Create the directory
if ($in{type} ne "swap" && !(-d $in{directory})) {
&lock_file($in{directory});
&make_dir($in{directory}, 0755) ||
&error(&text('save_emkdir', $in{'directory'}, $!));
&unlock_file($in{directory});
$made_dir = 1;
}
# If mounting now, attempt to do it
if ($mnow) {
# If the mount fails, give up totally
if ($error = &mount_dir($minfo[0], $minfo[1],
$minfo[2], $minfo[3])) {
if ($made_dir) { rmdir($in{directory}); }
&error(&text('save_emount', $error));
}
@tlog = ( "mount", "dir", $in{'directory'},
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
# If saving, save now
if ($msave) {
&create_mount(@minfo);
@plog = ( "create", "dir", $in{'directory'},
{ 'dir' => $minfo[0], 'dev' => $minfo[1],
'type' => $minfo[2], 'opts' => $minfo[3] } );
}
}
foreach $f (&files_to_lock()) {
&unlock_file($f);
}
&webmin_log(@plog) if (@plog);
%tpmap = ( 'create', 'mount', 'delete', 'umount', 'modify', 'remount' );
if (@tlog && $tpmap{$plog[0]} ne $tlog[0]) {
&webmin_log(@tlog);
}
# Mark this mount and owned by this current user
$users = &get_filesystem_users();
if ($msave || $mnow) {
$users->{$in{'directory'}} ||= $remote_user;
}
else {
delete($users->{$in{'directory'}});
}
&save_filesystem_users($users);
&redirect($in{'return'});
# undo_changes
# Put back any changes to the fstab file
sub undo_changes
{
if ($in{temp} && $in{mboot}) {
# a mount was made permanent.. undo by deleting it
&delete_mount($idx);
}
elsif (!$in{temp} && !$in{mboot}) {
# a permanent mount was made temporary.. undo by making it permanent
&create_mount(@mold);
}
elsif ($in{mboot}) {
# some mount options were changed.. undo by changing back
&change_mount($in{old}, @mold);
}
if ($made_dir) {
# A directory for mounting was created.. delete it
rmdir($in{directory});
}
}
# diff_opts(string1, string2)
sub diff_opts
{
local $o1 = join(",", sort { $a cmp $b } split(/,/, $_[0]));
local $o2 = join(",", sort { $a cmp $b } split(/,/, $_[1]));
return $o1 ne $o2;
}