1# dfs-lib.pl
2# Common functions for managing dfstab files
3
4BEGIN { push(@INC, ".."); };
5use WebminCore;
6&init_config();
7%access = &get_module_acl();
8
9$default_type = 'nfs';
10if ($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
20sub list_shares
21{
22local $lnum = 0;
23local @rv;
24open(DFS, "<".$config{'dfstab_file'});
25while(<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	}
53close(DFS);
54return @rv;
55}
56
57# create_share(&share)
58# Add a new share to the dfstab file
59sub 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
68sub modify_share
69{
70local $lref = &read_file_lines($config{'dfstab_file'});
71splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1,
72       &share_line($_[0]));
73&flush_file_lines();
74}
75
76# share_line(&share)
77sub share_line
78{
79local $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'}";
85return $s;
86}
87
88# delete_share(&share)
89# Delete the share for a particular directory
90sub delete_share
91{
92local $lref = &read_file_lines($config{'dfstab_file'});
93splice(@$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
100sub parse_options
101{
102local($opt);
103undef(%options);
104foreach $opt (split(/,/, $_[0])) {
105	if ($opt =~ /^([^=]+)=(.*)$/) {
106		$options{$1} = $2;
107		}
108	else {
109		$options{$opt} = "";
110		}
111	}
112return \%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
118sub join_options
119{
120local $o = $_[0] ? $_[0] : \%options;
121local(@list, $k);
122foreach $k (keys %$o) {
123	if ($o->{$k} eq "") {
124		push(@list, $k);
125		}
126	else {
127		push(@list, "$k=$o->{$k}");
128		}
129	}
130return join(',', @list);
131}
132
133# apply_configuration()
134# Apply the NFS configuration, returning undef on success or an error message
135# on failure
136sub apply_configuration
137{
138local $temp = &transname();
139&system_logged("$config{unshare_all_command} >/dev/null 2>&1");
140&system_logged("$config{share_all_command} >/dev/null 2>$temp");
141local $why = `/bin/cat $temp`;
142unlink($temp);
143if ($why =~ /\S+/) {
144	return $why;
145	}
146return undef;
147}
148
1491;
150
151