Handle hostnames with upper-case letters
[webmin.git] / dfsadmin / dfs-lib.pl
1 # dfs-lib.pl
2 # Common functions for managing dfstab files
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 %access = &get_module_acl();
8
9 $default_type = 'nfs';
10 if ($config{'fstypes_file'} && open(TYPES, $config{'fstypes_file'})) {
11         if (<TYPES> =~ /^(\S+)/) {
12                 $default_type = $1;
13                 }
14         close(TYPES);
15         }
16 %access = &get_module_acl();
17
18 # list_shares()
19 # Returns a list of structures containing share details
20 sub list_shares
21 {
22 local $lnum = 0;
23 local @rv;
24 open(DFS, $config{'dfstab_file'});
25 while(<DFS>) {
26         s/\r|\n//g; s/#.*$//;
27         if (/^\s*\S*share\s+(.*)/) {
28                 # Found a share line
29                 local $share = { 'line' => $lnum,
30                                  'index' => scalar(@rv) };
31                 local $line = $1;
32                 while($line =~ /\\$/) {
33                         $_ = <DFS>;
34                         s/\r|\n//g; s/#.*$//;
35                         $line =~ s/\\$//;
36                         $line .= $_;
37                         $lnum++;
38                         }
39                 $share->{'eline'} = $lnum;
40                 if ($line =~ /\s(\/\S+)/) {
41                         $share->{'dir'} = $1;
42                         }
43                 if ($line =~ /-d\s+"([^"]+)"/) { $share->{'desc'} = $1; }
44                 elsif ($line =~ /-d\s+(\S+)/) { $share->{'desc'} = $1; }
45                 if ($line =~ /-o\s+"([^"]+)"/) { $share->{'opts'} = $1; }
46                 elsif ($line =~ /-o\s+(\S+)/) { $share->{'opts'} = $1; }
47                 if ($line =~ /\s-F\s+(\S+)/) { $share->{'type'} = $1; }
48                 else { $share->{'type'} = $default_type; }
49                 push(@rv, $share);
50                 }
51         $lnum++;
52         }
53 close(DFS);
54 return @rv;
55 }
56
57 # create_share(&share)
58 # Add a new share to the dfstab file
59 sub create_share
60 {
61 &open_tempfile(DFS, ">> $config{dfstab_file}");
62 &print_tempfile(DFS, &share_line($_[0]),"\n");
63 &close_tempfile(DFS);
64 }
65
66 # modify_share(&share)
67 # Modify an existing share
68 sub modify_share
69 {
70 local $lref = &read_file_lines($config{'dfstab_file'});
71 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1,
72        &share_line($_[0]));
73 &flush_file_lines();
74 }
75
76 # share_line(&share)
77 sub share_line
78 {
79 local $s = "share";
80 $s .= " -d \"$_[0]->{'desc'}\"" if ($_[0]->{'desc'});
81 $s .= " -o $_[0]->{'opts'}" if ($_[0]->{'opts'});
82 $s .= " -F $_[0]->{'type'}" if ($_[0]->{'type'} &&
83                                 $_[0]->{'type'} ne $default_type);
84 $s .= " $_[0]->{'dir'}";
85 return $s;
86 }
87
88 # delete_share(&share)
89 # Delete the share for a particular directory
90 sub delete_share
91 {
92 local $lref = &read_file_lines($config{'dfstab_file'});
93 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1);
94 &flush_file_lines();
95 }
96
97 # parse_options(string)
98 # Parse a mount options string like rw=foo,nosuid,... into the associative
99 # array %options. Parts with no value are given an empty string as the value
100 sub parse_options
101 {
102 local($opt);
103 undef(%options);
104 foreach $opt (split(/,/, $_[0])) {
105         if ($opt =~ /^([^=]+)=(.*)$/) {
106                 $options{$1} = $2;
107                 }
108         else {
109                 $options{$opt} = "";
110                 }
111         }
112 return \%options;
113 }
114
115 # join_options([&options])
116 # Returns a list of options from the %options array, in the form used in
117 # the dfstab file
118 sub join_options
119 {
120 local $o = $_[0] ? $_[0] : \%options;
121 local(@list, $k);
122 foreach $k (keys %$o) {
123         if ($o->{$k} eq "") {
124                 push(@list, $k);
125                 }
126         else {
127                 push(@list, "$k=$o->{$k}");
128                 }
129         }
130 return join(',', @list);
131 }
132
133 # apply_configuration()
134 # Apply the NFS configuration, returning undef on success or an error message
135 # on failure
136 sub apply_configuration
137 {
138 local $temp = &transname();
139 &system_logged("$config{unshare_all_command} >/dev/null 2>&1");
140 &system_logged("$config{share_all_command} >/dev/null 2>$temp");
141 local $why = `/bin/cat $temp`;
142 unlink($temp);
143 if ($why =~ /\S+/) {
144         return $why;
145         }
146 return undef;
147 }
148
149 1;
150