Handle hostnames with upper-case letters
[webmin.git] / bsdexports / bsdexports-lib.pl
1 # bsdexports-lib.pl
2 # Functions for managing the FreeBSD exports file
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7
8 # list_exports()
9 # Returns the current exports list
10 sub list_exports
11 {
12 local(@rv, $lnum, $_);
13 open(EXP, $config{'exports_file'});
14 $lnum = -1; $index = 0;
15 while(<EXP>) {
16         $lnum++;
17         s/\r|\n//g;     # remove newlines
18         s/#.*$//g;      # remove comments
19         next if (!/\S/);
20         local @w = split(/[\s=]+/, $_);
21         local %exp;
22         for($i=0; $i<@w; $i++) {
23                 if ($w[$i] =~ /^\//) { push(@{$exp{'dirs'}}, $w[$i]); }
24                 elsif ($w[$i] eq "-maproot") { $exp{'maproot'} = $w[++$i]; }
25                 elsif ($w[$i] eq "-r") { $exp{'maproot'} = $w[++$i]; }
26                 elsif ($w[$i] eq "-mapall") { $exp{'mapall'} = $w[++$i]; }
27                 elsif ($w[$i] eq "-kerb") { $exp{'kerb'}++; }
28                 elsif ($w[$i] eq "-ro") { $exp{'ro'}++; }
29                 elsif ($w[$i] eq "-alldirs") { $exp{'alldirs'}++; }
30                 elsif ($w[$i] eq "-network") { $exp{'network'} = $w[++$i]; }
31                 elsif ($w[$i] eq "-mask") { $exp{'mask'} = $w[++$i]; }
32                 elsif ($w[$i] eq "-public") { $exp{'public'}++; }
33                 elsif ($w[$i] eq "-webnfs") { $exp{'webnfs'}++; }
34                 elsif ($w[$i] eq "-index") { $exp{'index'} = $w[++$i]; }
35                 else { push(@{$exp{'hosts'}}, $w[$i]); }
36                 }
37         $exp{'line'} = $lnum;
38         $exp{'index'} = $index++;
39         push(@rv, \%exp);
40         }
41 close(EXP);
42 return @rv;
43 }
44
45 # create_export(&export)
46 sub create_export
47 {
48 &open_tempfile(EXP, ">> $config{'exports_file'}");
49 &print_tempfile(EXP, &export_line($_[0]));
50 &close_tempfile(EXP);
51 }
52
53 # modify_export(&old, &new)
54 sub modify_export
55 {
56 &replace_file_line($config{'exports_file'}, $_[0]->{'line'},
57                    &export_line($_[1]));
58 }
59
60 # delete_export(&export)
61 sub delete_export
62 {
63 &replace_file_line($config{'exports_file'}, $_[0]->{'line'});
64 }
65
66 # export_line(&export)
67 sub export_line
68 {
69 local %exp = %{$_[0]};
70 local $rv = join(' ', @{$exp{'dirs'}});
71 if ($exp{'alldirs'}) { $rv .= " -alldirs"; }
72 if ($exp{'ro'}) { $rv .= " -ro"; }
73 if ($exp{'kerb'}) { $rv .= " -kerb"; }
74 if ($exp{'maproot'}) { $rv .= " -maproot $exp{'maproot'}"; }
75 if ($exp{'mapall'}) { $rv .= " -mapall $exp{'mapall'}"; }
76 if ($exp{'mask'}) { $rv .= " -network $exp{'network'} -mask $exp{'mask'}"; }
77 if ($exp{'public'}) { $rv .= " -public"; }
78 if ($exp{'webnfs'}) { $rv .= " -webnfs"; }
79 if ($exp{'index'}) { $rv .= " -index $exp{'index'}"; }
80 else { $rv .= " ".join(" ", @{$exp{'hosts'}}); }
81 $rv .= "\n";
82 return $rv;
83 }
84
85 # restart_mountd()
86 # Attempt to apply the NFS configuration, returning undef on success or an
87 # error message on failure
88 sub restart_mountd
89 {
90 local $out = &backquote_logged("($config{'restart_command'}) </dev/null 2>&1");
91 if ($?) {
92         return "<pre>$out</pre>";
93         }
94 return undef;
95 }
96