forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreebsd-lib.pl
executable file
·1107 lines (1002 loc) · 34.4 KB
/
freebsd-lib.pl
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# freebsd-lib.pl
# Mount table functions for freebsd
$uname_release = `uname -r`;
if (&has_command("mount_smbfs")) {
$smbfs_support = 1;
$nsmb_conf = "/etc/nsmb.conf";
}
# Return information about a filesystem, in the form:
# directory, device, type, options, fsck_order, mount_at_boot
# If a field is unused or ignored, a - appears instead of the value.
# Swap-filesystems (devices or files mounted for VM) have a type of 'swap',
# and 'swap' in the directory field
sub list_mounts
{
local(@rv, @p, @o, $_, $i, $j); $i = 0;
# Get /etc/fstab mounts
open(FSTAB, $config{'fstab_file'});
while(<FSTAB>) {
local(@o, $at_boot);
chop; s/#.*$//g;
if (!/\S/) { next; }
@p = split(/\s+/, $_);
if ($p[2] eq "proc" || $p[2] eq "procfs") { $p[2] = $p[0] = "proc"; }
elsif ($p[2] eq "swap") { $p[1] = "swap"; }
elsif ($p[2] eq "smbfs") {
# Need to get nsmb.conf options, covert share and extract user
$p[0] = lc($p[0]);
local $noptions = &read_nsmb($p[0]);
local %options;
&parse_options($p[3]);
if ($p[0] =~ /^\/\/(\S+)\@(\S+)\/(.*)$/) {
$p[0] = "\\\\$2\\$3";
$options{'user'} = $1;
}
elsif ($p[0] =~ /\/\/(\S+)\/(.*)$/) {
$p[0] = "\\\\$1\\$2";
}
%options = ( %options, %$noptions );
$p[3] = &join_options();
}
$rv[$i] = [ $p[1], $p[0], $p[2] ];
$rv[$i]->[5] = "yes";
@o = split(/,/ , $p[3] eq "defaults" ? "" : $p[3]);
if (($j = &indexof("noauto", @o)) >= 0) {
# filesytem is not mounted at boot
splice(@o, $j, 1);
$rv[$i]->[5] = "no";
}
$rv[$i]->[3] = (@o ? join(',' , @o) : "-");
$rv[$i]->[4] = (@p >= 5 ? $p[5] : 0);
$i++;
}
close(FSTAB);
return @rv;
}
# create_mount(directory, device, type, options, fsck_order, mount_at_boot)
# Add a new entry to the fstab file, old or new automounter file
sub create_mount
{
local(@mlist, @amd, $_, $opts);
&open_tempfile(FSTAB, ">> $config{'fstab_file'}");
&print_mount_line(@_);
&close_tempfile(FSTAB);
}
# print_mount_line(directory, device, type, options, fsck_order, mount_at_boot)
sub print_mount_line
{
if ($_[2] eq "smbfs") {
# Adding an SMB mount, which needs special handling
local %options;
&parse_options("smbfs", $_[3]);
local $share;
if ($options{'user'} && $_[1] =~ /^\\\\(.*)\\(.*)$/) {
$share = "//$options{'user'}\@$1/$2";
}
else {
($share = $_[1]) =~ s/\\/\//g;
}
&print_tempfile(FSTAB, "$share $_[0] $_[2]");
local $roptions = &update_nsmb($share, \%options);
delete($roptions->{'user'});
$opts = &join_options($_[2], $roptions);
}
else {
# Adding a normal mount to the fstab file
&print_tempfile(FSTAB, "$_[1] $_[0] $_[2]");
$opts = $_[3] eq "-" ? "" : $_[3];
}
if ($_[5] eq "no") {
$opts = join(',' , (split(/,/ , $opts) , "noauto"));
}
if ($opts eq "") {
&print_tempfile(FSTAB, " defaults");
}
else {
&print_tempfile(FSTAB, " $opts");
}
&print_tempfile(FSTAB, " 0 ");
&print_tempfile(FSTAB, $_[4] eq "-" ? "0\n" : "$_[4]\n");
}
# change_mount(num, directory, device, type, options, fsck_order, mount_at_boot)
# Change an existing permanent mount
sub change_mount
{
local($i, @fstab, $line, $opts, $j, @amd);
$i = 0;
# Update fstab file
open(FSTAB, $config{fstab_file});
@fstab = <FSTAB>;
close(FSTAB);
&open_tempfile(FSTAB, "> $config{fstab_file}");
foreach (@fstab) {
chop; ($line = $_) =~ s/#.*$//g;
if ($line =~ /\S/ && $i++ == $_[0]) {
# Found the line to replace
&print_mount_line(@_[1..$#_]);
}
else {
&print_tempfile(FSTAB, $_,"\n");
}
}
&close_tempfile(FSTAB);
}
# delete_mount(index)
# Delete an existing permanent mount
sub delete_mount
{
local($i, @fstab, $line, $opts, $j, @amd);
$i = 0;
# Update fstab file
open(FSTAB, $config{fstab_file});
@fstab = <FSTAB>;
close(FSTAB);
&open_tempfile(FSTAB, "> $config{fstab_file}");
foreach (@fstab) {
chop; ($line = $_) =~ s/#.*$//g;
if ($line !~ /\S/ || $i++ != $_[0]) {
# Don't delete this line
&print_tempfile(FSTAB, $_,"\n");
}
}
&close_tempfile(FSTAB);
}
# list_mounted()
# Return a list of all the currently mounted filesystems and swap files.
# The list is in the form:
# directory device type options
# Under FreeBSD, there seems to be no way to get additional mount options
# used by filesystems like NFS etc. Even getting the full details of mounted
# filesystems requires C code! So we have to call a specially-written external
# program to get the mount list
sub list_mounted
{
# get the list of mounted filesystems
local(@rv, $_);
local $cmd = $uname_release =~ /^[789]\.[0-9]/ ? "freebsd-mounts-7" :
$uname_release =~ /^[56]\.[0-9]/ ? "freebsd-mounts-5" :
$uname_release =~ /^4\.[0-9]/ ? "freebsd-mounts-4" :
$uname_release =~ /^3\.[1-9]/ ? "freebsd-mounts-3" :
"freebsd-mounts-2";
&compile_program($cmd, '.*86');
open(CMD, "$module_config_directory/$cmd |");
while(<CMD>) {
local @p = split(/\t/, $_);
if ($p[2] eq "procfs" || $p[1] eq "procfs") { $p[1] = $p[2] = "proc"; }
elsif ($p[2] eq "mfs") { $p[1] =~ s/:.*$//; }
elsif ($p[2] eq "smbfs") {
# Need to get nsmb.conf options, covert share and extract user
$p[1] = lc($p[1]);
local $noptions = &read_nsmb($p[1]);
local %options;
&parse_options($p[3]);
if ($p[1] =~ /^\/\/(\S+)\@(\S+)\/(.*)$/) {
$p[1] = "\\\\$2\\$3";
$options{'user'} = $1;
}
elsif ($p[1] =~ /\/\/(\S+)\/(.*)$/) {
$p[1] = "\\\\$1\\$2";
}
%options = ( %options, %$noptions );
$p[3] = &join_options();
}
push(@rv, \@p);
}
close(CMD);
# add output from swapinfo
local $out;
&execute_command("swapinfo", undef, \$out, undef, 0, 1);
foreach (split(/\n/, $out)) {
if (/^(\/\S+)\s+\d+\s+\d+/) {
push(@rv, [ "swap", $1, "swap", "-" ]);
}
}
return @rv;
}
# mount_dir(directory, device, type, options)
# Mount a new directory from some device, with some options. Returns 0 if ok,
# or an error string if failed
sub mount_dir
{
local($out, $opts, $shar, %options, %smbopts);
if ($_[2] eq "swap") {
# Use swapon to add the swap space..
$out = &backquote_logged("swapon $_[1] 2>&1");
if ($?) { return "<pre>$out</pre>"; }
}
elsif ($_[2] eq "smbfs") {
# Need special handling for SMB mounts
local %options;
&parse_options($_[2], $_[3]);
local $share;
if ($options{'user'} && $_[1] =~ /^\\\\(.*)\\(.*)$/) {
$share = "//$options{'user'}\@$1/$2";
}
else {
($share = $_[1]) =~ s/\\/\//g;
}
local $roptions = &update_nsmb($share, \%options);
delete($roptions->{'user'});
local $opts = &join_options($_[2], $roptions);
$opts = $opts ne "-" ? " -o $opts" : "";
&foreign_require("proc");
local ($fh, $fpid) = &proc::pty_process_exec_logged(
"mount -t $_[2] $opts $share $_[0]");
local $got;
local $rv = &wait_for($fh, "Password:");
$got .= $wait_for_input;
if ($rv == 0) {
print $fh $options{'nsmb_password'},"\n";
}
$rv = &wait_for($fh);
$got .= $wait_for_input;
close($fh);
if ($? || $got =~ /failed|error|syserr|usage:/i) { return "<pre>$out</pre>"; }
}
else {
# Can use mount command for this filesystem
$opts = $_[3] eq "-" ? "" :
"-o ".join(',', grep { !/quota/ } split(/,/, $_[3]));
$out = &backquote_logged("mount -t $_[2] $opts $_[1] $_[0] 2>&1");
if ($?) { return "<pre>$out</pre>"; }
}
return 0;
}
# unmount_dir(directory, device, type)
# Unmount a directory that is currently mounted. Returns 0 if ok,
# or an error string if failed
sub unmount_dir
{
local($out, %smbopts, $dir);
if ($_[2] eq "swap") {
# Not possible!
&error("Swap space cannot be removed");
}
else {
$out = &backquote_logged("umount $_[0] 2>&1");
if ($?) { return "<pre>$out</pre>"; }
}
return 0;
}
# mount_modes(type)
# Given a filesystem type, returns 4 numbers that determine how the file
# system can be mounted, and whether it can be fsck'd
# The first is:
# 0 - cannot be permanently recorded
# (smbfs under linux)
# 1 - can be permanently recorded, and is always mounted at boot
# (swap under linux)
# 2 - can be permanently recorded, and may or may not be mounted at boot
# (most normal filesystems)
# The second is:
# 0 - mount is always permanent => mounted when saved
# (swap under linux)
# 1 - doesn't have to be permanent
# (normal fs types)
# The third is:
# 0 - cannot be fsck'd at boot time
# 1 - can be be fsck'd at boot
# The fourth is:
# 0 - can be unmounted
# 1 - cannot be unmounted
sub mount_modes
{
if ($_[0] eq "swap")
{ return (2, 1, 0, 1); }
elsif ($_[0] eq "ufs")
{ return (2, 1, 1, 0); }
else
{ return (2, 1, 0, 0); }
}
# disk_space(type, directory)
# Returns the amount of total and free space for some filesystem, or an
# empty array if not appropriate.
sub disk_space
{
if (&get_mounted($_[1], "*") < 0) { return (); }
if ($_[0] eq "proc" || $_[0] eq "swap") { return (); }
my $out;
&execute_command("df -k ".quotemeta($_[1]), undef, \$out, undef, 0, 1);
if ($out =~ /Mounted on\n\S+\s+(\S+)\s+\S+\s+(\S+)/) {
return ($1, $2);
}
else {
return ( );
}
}
# list_fstypes()
# Returns an array of all the supported filesystem types. If a filesystem is
# found that is not one of the supported types, generate_location() and
# generate_options() will not be called for it.
sub list_fstypes
{
local @rv = ("ufs", "nfs", "cd9660", "msdos", "swap");
push(@rv, "ext2fs") if (&has_command("mount_ext2fs"));
push(@rv, "ntfs") if (&has_command("mount_ntfs"));
push(@rv, "smbfs") if ($smbfs_support);
return @rv;
}
# fstype_name(type)
# Given a short filesystem type, return a human-readable name for it
sub fstype_name
{
local(%fsmap);
%fsmap = ("ufs", "FreeBSD Unix Filesystem",
"nfs", "Network Filesystem",
"cd9660", "ISO9660 CD-ROM",
"msdos", "MS-DOS Filesystem",
"ext2fs", "Linux Filesystem",
"ntfs", "Windows NT Filesystem",
"swap", "Virtual Memory",
"proc", "Process Image Filesystem",
"smbfs", "Windows Networking Filesystem");
return $config{long_fstypes} && $fsmap{$_[0]} ? $fsmap{$_[0]} : uc($_[0]);
}
# multiple_mount(type)
# Returns 1 if filesystems of this type can be mounted multiple times, 0 if not
sub multiple_mount
{
return $_[0] eq "nfs" || $_[0] eq "smbfs";
}
# generate_location(type, location)
# Output HTML for editing the mount location of some filesystem.
sub generate_location
{
local ($type, $loc) = @_;
if ($type eq "nfs") {
# NFS mount from some host and directory
local ($host, $dir) = $loc =~ /^([^:]+):(.*)$/ ? ( $1, $2 ) : ( );
print &ui_table_row(&hlink($text{'linux_nfshost'}, "nfshost"),
&ui_textbox("nfs_host", $host, 30).
&nfs_server_chooser_button("nfs_host").
" ".
"<b>".&hlink($text{'linux_nfsdir'}, "nfsdir")."</b> ".
&ui_textbox("nfs_dir",
($type eq "nfs4") && ($dir eq "") ? "/" : $dir, 30).
&nfs_export_chooser_button("nfs_host", "nfs_dir"));
}
elsif ($type eq "smbfs") {
# SMB mount from some server and share
local ($server, $share) = $loc =~ /^\\\\([^\\]*)\\(.*)$/ ?
($1, $2) : ( );
print &ui_table_row($text{'linux_smbserver'},
&ui_textbox("smbfs_server", $server, 30)." ".
&smb_server_chooser_button("smbfs_server")." ".
" ".
"<b>$text{'linux_smbshare'}</b> ".
&ui_textbox("smbfs_share", $share, 30)." ".
&smb_share_chooser_button("smbfs_server", "smbfs_share"));
}
else {
local $msg;
if ($type eq "swap") {
# Swap file or device
$msg = $text{'linux_swapfile'};
}
else {
# Disk-based filesystem
$msg = &fstype_name($type);
}
local ($disk_dev, $ide_t, $ide_s, $ide_p, $scsi_t, $scsi_s, $scsi_p);
if ($loc =~ /^\/dev\/ad(\d)s(\d)([a-z]*)$/) {
$disk_dev = 0; $ide_t = $1; $ide_s = $2; $ide_p = $3;
}
elsif ($loc =~ /^\/dev\/da(\d)s(\d)([a-z]*)$/) {
$disk_dev = 1; $scsi_t = $1; $scsi_s = $2; $scsi_p = $3;
}
else { $disk_dev = 2; }
print &ui_table_row($msg,
&ui_radio_table("disk_dev", $disk_dev,
[ [ 0, $text{'freebsd_ide'},
$text{'freebsd_device'}." ".
&ui_textbox("ide_t", $ide_t, 4)." ".
$text{'freebsd_slice'}." ".
&ui_textbox("ide_s", $ide_s, 4)." ".
$text{'freebsd_part'}." ".
&ui_textbox("ide_p", $ide_p, 4) ],
[ 1, $text{'freebsd_scsi'},
$text{'freebsd_device'}." ".
&ui_textbox("scsi_t", $scsi_t, 4)." ".
$text{'freebsd_slice'}." ".
&ui_textbox("scsi_s", $scsi_s, 4)." ".
$text{'freebsd_part'}." ".
&ui_textbox("scsi_p", $scsi_p, 4) ],
[ 2, $text{'freebsd_other'},
&ui_textbox("dev_path", $disk_dev == 2 ? $loc : "", 40).
" ".&file_chooser_button("dev_path", 0) ] ]));
}
}
# generate_options(type, newmount)
# Output HTML for editing mount options for a particular filesystem
# under this OS
sub generate_options
{
if ($_[0] ne "swap") {
# These options are common to all filesystems
print "<tr> <td><b>Read-only?</b></td>\n";
printf "<td nowrap><input type=radio name=bsd_ro value=1 %s> Yes\n",
defined($options{"rdonly"}) || defined($options{"ro"})
? "checked" : "";
printf "<input type=radio name=bsd_ro value=0 %s> No</td>\n",
defined($options{"rdonly"}) || defined($options{"ro"})
? "" : "checked";
print "<td><b>Buffer writes to filesystem?</b></td>\n";
printf"<td nowrap><input type=radio name=bsd_sync value=0 %s> Yes\n",
defined($options{"sync"}) ? "" : "checked";
printf "<input type=radio name=bsd_sync value=1 %s> No</td> </tr>\n",
defined($options{"sync"}) ? "checked" : "";
print "<tr> <td><b>Allow device files?</b></td>\n";
printf "<td nowrap><input type=radio name=bsd_nodev value=0 %s> Yes\n",
defined($options{"nodev"}) ? "" : "checked";
printf "<input type=radio name=bsd_nodev value=1 %s> No</td>\n",
defined($options{"nodev"}) ? "checked" : "";
print "<td><b>Allow execution of binaries?</b></td>\n";
printf"<td nowrap><input type=radio name=bsd_noexec value=0 %s> Yes\n",
defined($options{"noexec"}) ? "" : "checked";
printf "<input type=radio name=bsd_noexec value=1 %s> No</td> </tr>\n",
defined($options{"noexec"}) ? "checked" : "";
print "<tr> <td><b>Disallow setuid programs?</b></td>\n";
printf "<td nowrap><input type=radio name=bsd_nosuid value=1 %s> Yes\n",
defined($options{"nosuid"}) ? "checked" : "";
printf "<input type=radio name=bsd_nosuid value=0 %s> No</td>\n",
defined($options{"nosuid"}) ? "" : "checked";
print "<td><b>Update access times?</b></td>\n";
printf"<td nowrap><input type=radio name=bsd_noatime value=0 %s> Yes\n",
defined($options{"noatime"}) ? "" : "checked";
printf "<input type=radio name=bsd_noatime value=1 %s> No</td> </tr>\n",
defined($options{"noatime"}) ? "checked" : "";
if ($uname_release =~ /^[34]\./) {
# FreeBSD 3.x has some more options
print "<tr> <td><b>Follow symbolic links?</b></td>\n";
printf "<td nowrap><input type=radio name=bsd_nosymfollow value=0 %s> Yes\n",
defined($options{"nosymfollow"}) ? "" : "checked";
printf "<input type=radio name=bsd_nosymfollow value=1 %s> No</td>\n",
defined($options{"nosymfollow"}) ? "checked" : "";
print "<td><b>Files inherit group from SUID directories?</b></td>\n";
printf"<td nowrap><input type=radio name=bsd_suiddir value=1 %s> Yes\n",
defined($options{"suiddir"}) ? "checked" : "";
printf "<input type=radio name=bsd_suiddir value=0 %s> No</td> </tr>\n",
defined($options{"suiddir"}) ? "" : "checked";
}
}
if ($_[0] eq "ufs") {
# UFS filesystems support quotas
print "<tr> <td><b>User quotas at boot</b></td> <td colspan=3>\n";
printf "<input type=radio name=ufs_userquota value=0 %s> Disabled\n",
defined($options{'userquota'}) ? "" : "checked";
printf "<input type=radio name=ufs_userquota value=1 %s> Enabled\n",
defined($options{'userquota'}) && $options{'userquota'} eq ""
? "checked" : "";
printf "<input type=radio name=ufs_userquota value=2 %s>\n",
$options{'userquota'} ? "checked" : "";
print "Enabled, use file\n";
printf "<input name=ufs_userquota_file size=30 value=\"%s\">\n",
$options{'userquota'};
print "</td> </tr>\n";
print "<tr> <td><b>Group quotas at boot</b></td> <td colspan=3>\n";
printf "<input type=radio name=ufs_groupquota value=0 %s> Disabled\n",
defined($options{'groupquota'}) ? "" : "checked";
printf "<input type=radio name=ufs_groupquota value=1 %s> Enabled\n",
defined($options{'groupquota'}) && $options{'groupquota'} eq ""
? "checked" : "";
printf "<input type=radio name=ufs_groupquota value=2 %s>\n",
$options{'groupquota'} ? "checked" : "";
print "Enabled, use file\n";
printf "<input name=ufs_groupquota_file size=30 value=\"%s\">\n",
$options{'groupquota'};
print "</td> </tr>\n";
}
elsif ($_[0] eq "nfs") {
# NFS filesystems have lots more options
print "<tr> <td><b>Retry mounts in background?</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_b value=1 %s> Yes\n",
defined($options{"-b"}) ? "checked" : "";
printf "<input type=radio name=nfs_b value=0 %s> No</td>\n",
defined($options{"-b"}) ? "" : "checked";
print "<td><b>Return error on timeouts?</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_s value=1 %s> Yes\n",
defined($options{"-s"}) ? "checked" : "";
printf "<input type=radio name=nfs_s value=0 %s> No</td> </tr>\n",
defined($options{"-s"}) ? "" : "checked";
print "<tr> <td><b>Timeout</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_t_def value=1 %s> Default\n",
defined($options{"-t"}) ? "" : "checked";
printf "<input type=radio name=nfs_t_def value=0 %s>\n",
defined($options{"-t"}) ? "checked" : "";
printf "<input size=5 name=nfs_t value=\"$options{'-t'}\"></td>\n";
print "<td><b>Number of Retransmissions</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_x_def value=1 %s> Default\n",
defined($options{"-x"}) ? "" : "checked";
printf "<input type=radio name=nfs_x_def value=0 %s>\n",
defined($options{"-x"}) ? "checked" : "";
print "<input size=5 name=nfs_x value=\"$options{'-x'}\"></td> </tr>\n";
print "<tr> <td><b>NFS version</b></td> <td nowrap>\n";
local $v = defined($options{"-2"}) ? 2 :
defined($options{"-3"}) ? 3 : 0;
printf "<input type=radio name=nfs_ver value=0 %s> Auto\n",
$v ? "" : "checked";
printf "<input type=radio name=nfs_ver value=2 %s> V2\n",
$v == 2 ? "checked" : "";
printf "<input type=radio name=nfs_ver value=3 %s> V3</td>\n",
$v == 3 ? "checked" : "";
print "<td><b>Mount retries</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_r_def value=1 %s> Default\n",
defined($options{"-R"}) ? "" : "checked";
printf "<input type=radio name=nfs_r_def value=0 %s>\n",
defined($options{"-R"}) ? "checked" : "";
print "<input size=5 name=nfs_r value=\"$options{'-R'}\"></td> </tr>\n";
print "<tr> <td><b>Read-ahead blocks</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_a_def value=1 %s> Default\n",
defined($options{"-a"}) ? "" : "checked";
printf "<input type=radio name=nfs_a_def value=0 %s>\n",
defined($options{"-a"}) ? "checked" : "";
print "<input size=5 name=nfs_a value=\"$options{'-a'}\"></td>\n";
print "<td><b>RPC Protocol</b></td>\n";
printf "<td nowrap><input type=radio name=nfs_t2 value=1 %s> TCP\n",
defined($options{"-T"}) ? "checked" : "";
printf "<input type=radio name=nfs_t2 value=0 %s> UDP</td> </tr>\n",
defined($options{"-T"}) ? "" : "checked";
}
elsif ($_[0] eq "msdos"){
# MS-DOS filesystems options deal with filling in
# missing unix functionality
print "<tr> <td><b>User files are owned by</b></td>\n";
printf "<td><input name=msdos_u size=8 value=\"%s\">\n",
defined($options{"-u"}) ? getpwuid($options{"-u"}) : "";
print &user_chooser_button("msdos_u", 0),"</td>\n";
print "<td><b>Group files are owned by</b></td>\n";
printf "<td><input name=msdos_g size=8 value=\"%s\">\n",
defined($options{"-g"}) ? getgrgid($options{"-g"}) : "";
print &group_chooser_button("msdos_g", 0),"</td>\n";
print "<tr> <td><b>File permissions mask</b></td>\n";
printf "<td><input type=radio name=msdos_m_def value=1 %s> Default\n",
defined($options{"-m"}) ? "" : "checked";
printf "<input type=radio name=msdos_m_def value=0 %s>\n",
defined($options{"-m"}) ? "checked" : "";
print "<input size=5 name=msdos_m value=\"$options{'-m'}\"></td>\n";
}
elsif ($_[0] eq "cd9660") {
# CDROM filesystem
print "<tr> <td><b>Ignore Unix Attributes?</b></td>\n";
printf "<td><input type=radio name=cd9660_r value=1 %s> Yes\n",
defined($options{"-r"}) ? "checked" : "";
printf "<input type=radio name=cd9660_r value=0 %s> No</td>\n",
defined($options{"-r"}) ? "" : "checked";
print "<td><b>Show version numbers?</b></td>\n";
printf "<td><input type=radio name=cd9660_g value=1 %s> Yes\n",
defined($options{"-g"}) ? "checked" : "";
printf "<input type=radio name=cd9660_g value=0 %s> No</td> </tr>\n",
defined($options{"-g"}) ? "" : "checked";
print "<tr> <td><b>Use extended attributes?</b></td>\n";
printf "<td><input type=radio name=cd9660_e value=1 %s> Yes\n",
defined($options{"-e"}) ? "checked" : "";
printf "<input type=radio name=cd9660_e value=0 %s> No</td> </tr>\n",
defined($options{"-e"}) ? "" : "checked";
}
elsif ($_[0] eq "ntfs") {
# Windows NT filesystem
print "<tr> <td><b>Display MSDOS 8.3 filenames?</b></td>\n";
printf "<td><input type=radio name=ntfs_a value=1 %s> Yes\n",
defined($options{"-a"}) ? "checked" : "";
printf "<input type=radio name=ntfs_a value=0 %s> No</td>\n",
defined($options{"-a"}) ? "" : "checked";
print "<td><b>Case sensitive filenames?</b></td>\n";
printf "<td><input type=radio name=ntfs_i value=0 %s> Yes\n",
defined($options{"-i"}) ? "" : "checked";
printf "<input type=radio name=ntfs_i value=1 %s> No</td> </tr>\n",
defined($options{"-i"}) ? "checked" : "";
print "<tr> <td><b>User files are owned by</b></td>\n";
printf "<td><input type=radio name=ntfs_u_def value=1 %s> Default\n",
defined($options{"-u"}) ? "" : "checked";
printf "<input type=radio name=ntfs_u_def value=0 %s>\n",
defined($options{"-u"}) ? "checked" : "";
printf "<input name=ntfs_u size=8 value='%s'> %s</td>\n",
defined($options{"-u"}) ? scalar(getpwuid($options{"-u"})) : "",
&user_chooser_button("ntfs_u");
print "<td><b>Group files are owned by</b></td>\n";
printf "<td><input type=radio name=ntfs_g_def value=1 %s> Default\n",
defined($options{"-u"}) ? "" : "checked";
printf "<input type=radio name=ntfs_g_def value=0 %s>\n",
defined($options{"-u"}) ? "checked" : "";
printf "<input name=ntfs_g size=8 value='%s'> %s</td> </tr>\n",
defined($options{"-g"}) ? scalar(getgrgid($options{"-g"})) : "",
&group_chooser_button("ntfs_g");
}
elsif ($_[0] eq "swap") {
# Swap has no options..
print "<tr> <td><i>No Options Available</i></td> </tr>\n";
}
elsif ($_[0] eq "smbfs") {
# SMBFS has some special options
print "<tr> <td><b>$text{'linux_username'}</b></td>\n";
printf "<td><input name=smbfs_user size=15 value=\"%s\"></td>\n",
$options{"user"};
print "<td><b>$text{'linux_password'}</b></td>\n";
printf "<td><input type=password name=smbfs_password size=15 value=\"%s\"></td> </tr>\n",
$options{"nsmb_password"};
print "<tr> <td><b>$text{'linux_wg'}</b></td>\n";
printf "<td colspan=3><input type=radio name=smbfs_workgroup_def value=1 %s> $text{'linux_auto'}\n",
defined($options{"nsmb_workgroup"}) ? "" : "checked";
printf "<input type=radio name=smbfs_workgroup_def value=0 %s>\n",
defined($options{"nsmb_workgroup"}) ? "checked" : "";
print "<input size=10 name=smbfs_workgroup value=\"$options{'nsmb_workgroup'}\"></td> </tr>\n";
print "<tr> <td><b>$text{'linux_mname'}</b></td>\n";
printf "<td colspan=3><input type=radio name=smbfs_addr_def value=1 %s> %s\n",
defined($options{"nsmb_addr"}) ? "" : "checked", $text{'linux_auto'};
printf "<input type=radio name=smbfs_addr_def value=0 %s>\n",
defined($options{"nsmb_addr"}) ? "checked" : "";
print "<input size=30 name=smbfs_addr value=\"$options{'nsmb_addr'}\"></td> </tr>\n";
}
}
# check_location(type)
# Parse and check inputs from %in, calling &error() if something is wrong.
# Returns the location string for storing in the fstab file
sub check_location
{
if ($_[0] eq "nfs") {
local($out, $temp, $mout, $dirlist);
# Use ping and showmount to see if the host exists and is up
if ($in{nfs_host} !~ /^\S+$/) {
&error("'$in{nfs_host}' is not a valid hostname");
}
&execute_command("ping -c 1 '$in{nfs_host}'", undef, \$out, \$out);
if ($out =~ /unknown host/i) {
&error("The host '$in{nfs_host}' does not exist");
}
elsif ($out =~ /100\% packet loss/) {
&error("The host '$in{nfs_host}' is down");
}
&execute_command("showmount -e '$in{nfs_host}'", undef, \$out, \$out);
if ($out =~ /Unable to receive/) {
&error("The host '$in{nfs_host}' does not support NFS");
}
elsif ($?) {
&error("Failed to get mount list : $out");
}
# Validate directory name
foreach (split(/\n/, $out)) {
if (/^(\/\S+)/) { $dirlist .= "$1\n"; }
}
if ($in{nfs_dir} !~ /^\/\S+$/) {
&error("'$in{nfs_dir}' is not a valid directory name. The ".
"available directories on $in{nfs_host} are:".
"<pre>$dirlist</pre>");
}
# Try a test mount to see if filesystem is available
$temp = &transname();
&make_dir($temp, 0755);
&execute_command("mount $in{nfs_host}:$in{nfs_dir} $temp",
undef, \$mout, \$mout);
if ($mout =~ /No such file or directory/) {
&error("The directory '$in{nfs_dir}' does not exist on the ".
"host $in{nfs_host}. The available directories are:".
"<pre>$dirlist</pre>");
}
elsif ($mout =~ /Permission denied/) {
&error("This host is not allowed to mount the directory ".
"$in{nfs_dir} from $in{nfs_host}");
}
elsif ($?) {
&error("NFS Error - $mout");
}
# It worked! unmount
&execute_command("umount $temp");
&unlink_file($temp);
return "$in{nfs_host}:$in{nfs_dir}";
}
elsif ($_[0] eq "smbfs") {
# A windows server filesystem .. check the server and share
$in{'smbfs_server'} =~ /\S/ || &error($text{'linux_eserver'});
$in{'smbfs_share'} =~ /\S/ || &error($text{'linux_eshare'});
return "\\\\".lc($in{'smbfs_server'})."\\".lc($in{'smbfs_share'});
}
else {
# This is some kind of disk-based filesystem.. get the device name
if ($in{'disk_dev'} == 0) {
$in{'ide_t'} =~ /^\d+$/ ||
&error("'$in{ide_t}' is not a valid device number");
$in{'ide_s'} =~ /^\d+$/ ||
&error("'$in{ide_s}' is not a valid slice number");
$in{'ide_p'} =~ /^[a-z]*$/ ||
&error("'$in{ide_p}' is not a valid partition letter");
$dv = "/dev/ad$in{ide_t}s$in{ide_s}$in{ide_p}";
}
elsif ($in{'disk_dev'} == 1) {
$in{'scsi_t'} =~ /^\d+$/ ||
&error("'$in{scsi_t}' is not a valid device number");
$in{'scsi_s'} =~ /^\d+$/ ||
&error("'$in{scsi_s}' is not a valid slice number");
$in{'scsi_p'} =~ /^[a-z]*$/ ||
&error("'$in{scsi_p}' is not a valid partition letter");
$dv = "/dev/da$in{scsi_t}s$in{scsi_s}$in{scsi_p}";
}
else {
$dv = $in{'dev_path'};
}
# If the device entered is a symlink, follow it
if ($dvlink = readlink($dv)) {
if ($dvlink =~ /^\//) { $dv = $dvlink; }
else { $dv =~ /^(.*\/)[^\/]+$/;
$dv = $1.$dvlink;
}
}
# Check if the device actually exists and uses the right filesystem
(-r $dv) || &error("The device file '$dv' does not exist");
return $dv;
}
}
# check_options(type, device, directory)
# Read options for some filesystem from %in, and use them to update the
# %options array. Options handled by the user interface will be set or
# removed, while unknown options will be left untouched.
sub check_options
{
local($k, @rv);
# Parse the common options first..
if ($_[0] ne "swap") {
delete($options{"ro"}); delete($options{"rw"});
delete($options{"rdonly"});
if ($in{'bsd_ro'}) { $options{'ro'} = ''; }
else { $options{'rw'} = ""; }
delete($options{"sync"}); delete($options{"async"});
if ($in{'bsd_sync'}) { $options{'sync'} = ''; }
delete($options{'nodev'});
if ($in{'bsd_nodev'}) { $options{'nodev'} = ''; }
delete($options{'noexec'});
if ($in{'bsd_noexec'}) { $options{'noexec'} = ''; }
delete($options{'nosuid'});
if ($in{'bsd_nosuid'}) { $options{'nosuid'} = ''; }
delete($options{'noatime'});
if ($in{'bsd_noatime'}) { $options{'noatime'} = ''; }
if ($uname_release =~ /^[34]\./) {
delete($options{'nosymfollow'});
$options{'nosymfollow'} = '' if ($in{'bsd_nosymfollow'});
delete($options{'suiddir'});
$options{'suiddir'} = '' if ($in{'bsd_suiddir'});
}
}
else {
# Swap always has the sw option
$options{'sw'} = "";
}
if ($_[0] eq "ufs") {
# Parse UFS quota options
delete($options{'userquota'}) if ($in{'ufs_userquota'} == 0);
$options{'userquota'} = "" if ($in{'ufs_userquota'} == 1);
$options{'userquota'} = $in{'ufs_groupquota_file'}
if ($in{'ufs_userquota'} == 2);
delete($options{'groupquota'}) if ($in{'ufs_groupquota'} == 0);
$options{'groupquota'} = "" if ($in{'ufs_groupquota'} == 1);
$options{'groupquota'} = $in{'ufs_groupquota_file'}
if ($in{'ufs_groupquota'} == 2);
}
elsif ($_[0] eq "nfs") {
# NFS has a few specific options..
delete($options{'-b'});
$options{'-b'} = "" if ($in{'nfs_b'});
delete($options{'-s'});
$options{'-s'} = "" if ($in{'nfs_s'});
delete($options{'-t'});
$options{'-t'} = $in{'nfs_t'} if (!$in{'nfs_t_def'});
delete($options{'-x'});
$options{'-x'} = $in{'nfs_x'} if (!$in{'nfs_x_def'});
delete($options{'-2'}); delete($options{'-3'});
$options{'-2'} = "" if ($in{'nfs_ver'} == 2);
$options{'-3'} = "" if ($in{'nfs_ver'} == 3);
delete($options{'-R'});
$options{'-R'} = $in{'nfs_r'} if (!$in{'nfs_r_def'});
delete($options{'-a'});
$options{'-a'} = $in{'nfs_a'} if (!$in{'nfs_a_def'});
delete($options{'-T'});
$options{'-T'} = "" if ($in{'nfs_t2'});
}
elsif ($_[0] eq "msdos") {
# MSDOS options for file ownership/perms
delete($options{"-u"}); delete($options{"-g"});
if ($in{'msdos_u'} ne "") { $options{'-u'} = getpwnam($in{'msdos_u'}); }
if ($in{'msdos_g'} ne "") { $options{'-g'} = getgrnam($in{'msdos_g'}); }
delete($options{"-m"});
if (!$in{'msdos_m_def'}) {
$in{'msdos_m'} =~ /^[0-7]{3}$/ ||
&error("'$in{'msdos_m'}' is not a valid octal mask");
$options{'-m'} = $in{'msdos_m'};
}
}
elsif ($_[0] eq "cd9660") {
# Options for iso9660 cd-roms
delete($options{'-r'});
$options{'-r'} = "" if ($in{'cd9660_r'});
delete($options{'-g'});
$options{'-g'} = "" if ($in{'cd9660_g'});
delete($options{'-e'});
$options{'-e'} = "" if ($in{'cd9660_e'});
}
elsif ($_[0] eq "ntfs") {
delete($options{"-u"}); delete($options{"-g"});
if ($in{'ntfs_u'} ne "") { $options{'-u'} = getpwnam($in{'ntfs_u'}); }
if ($in{'ntfs_g'} ne "") { $options{'-g'} = getgrnam($in{'ntfs_g'}); }
delete($options{"-a"});
$options{"-a"} = '' if ($in{'ntfs_a'});
delete($options{"-i"});
$options{"-i"} = '' if ($in{'ntfs_i'});
}
elsif ($_[0] eq "smbfs") {
# Parse SMBFS options
delete($options{'user'});
$options{'user'} = $in{'smbfs_user'} if ($in{'smbfs_user'});
delete($options{'nsmb_password'});
$options{'nsmb_password'} = $in{'smbfs_password'}
if ($in{'smbfs_password'});
delete($options{'nsmb_addr'});
if (!$in{"smbfs_addr_def"}) {
&check_ipaddress($in{"smbfs_addr"}) ||
&error($text{'freebsd_eaddr'});
$options{'nsmb_addr'} = $in{"smbfs_addr"};
}
delete($options{'nsmb_workgroup'});
if (!$in{"smbfs_workgroup_def"}) {
$in{"smbfs_workgroup"} =~ /^\S+$/ ||
&error($text{'freebsd_eworkgroup'});
$options{'nsmb_workgroup'} = $in{"smbfs_workgroup"};
}
}
# Return options string
return &join_options();
}
# create_swap(file, size, units)
# Calls dd and mkswap to setup a swap file
sub create_swap
{
local($out, $bl);
$bl = $_[1] * ($_[2] eq "t" ? 1024*1024*1024 :
$_[2] eq "g" ? 1024*1024 :
$_[2] eq "m" ? 1024 : 1);
$out = &backquote_logged("dd if=/dev/zero of=$_[0] bs=1024 count=$bl 2>&1");
if ($?) { return "dd failed : $out"; }
$out = &backquote_logged("mkswap $_[0] $bl 2>&1");
if ($?) { return "mkswap failed : $out"; }
&system_logged("sync >/dev/null 2>&1");
return 0;
}
# exports_list(host, dirarray, clientarray)
# Fills the directory and client array references with exports from some
# host. Returns an error string if something went wrong
sub exports_list
{
local($dref, $cref, $out, $_);
$dref = $_[1]; $cref = $_[2];
&execute_command("showmount -e ".quotemeta($_[0]), undef, \$out, \$out, 0, 1);
if ($?) { return $out; }
foreach (split(/\n/, $out)) {
if (/^(\/\S*)\s+(.*)$/) {
push(@$dref, $1); push(@$cref, $2);
}
}
return undef;
}
# broadcast_addr()
# Returns a useable broadcast address for finding NFS servers
sub broadcast_addr
{
local($out);
&execute_command("ifconfig -a", undef, \$out, \$out, 0, 1);
if ($out =~ /broadcast\s+(\S+)\s+/) { return $1; }
return "255.255.255.255";
}
sub device_name
{
return $_[0];
}
sub files_to_lock
{
return ( $config{'fstab_file'}, $nsmb_conf );
}
# get_nsmb_conf(server, share, user)
# Finds a single nsmb.conf section
sub get_nsmb_conf
{
local $conf;
local $insection = 0;
local $lnum = 0;