Handle hostnames with upper-case letters
[webmin.git] / dfsadmin / save_share.cgi
1 #!/usr/local/bin/perl
2 # save_share.cgi
3 # Save changes to a shared directory
4
5 require './dfs-lib.pl';
6 &error_setup($text{'save_err'});
7 use Socket;
8 &ReadParse();
9 @shlist = &list_shares();
10
11 if ($in{'delete'}) {
12         # Redirect to deletion CGI
13         &redirect("delete_share.cgi?idx=$in{'idx'}");
14         exit;
15         }
16
17 # check inputs
18 if ($in{directory} !~ /^\/.*/) {
19         &error(&text('save_edirectory', $in{'directory'}));
20         }
21 if (!(-d $in{directory})) {
22         &error(&text('save_edirectory2', $in{'directory'}));
23         }
24 @rolist = split(/\s+/, $in{rolist}); &check_hosts(@rolist);
25 @rwlist = split(/\s+/, $in{rwlist}); &check_hosts(@rwlist);
26 @rtlist = split(/\s+/, $in{rtlist}); &check_hosts(@rtlist);
27 if ($in{readwrite} == 2 && !@rwlist) {
28         &error($text{'save_erw'});
29         }
30 if ($in{readonly} == 2 && !@rolist) {
31         &error($text{'save_ero'});
32         }
33 if ($in{root} == 2 && !@rtlist) {
34         &error($text{'save_eroot'});
35         }
36
37 # Remove from the read-only list any hosts shared read-write as well
38 if ($in{readwrite} == 1) {
39         $in{readonly} = 0;
40         }
41 elsif ($in{readwrite} == 2) {
42         foreach $rwh (@rwlist) {
43                 if (($idx = &indexof($rwh, @rolist)) != -1) {
44                         splice(@rolist, $idx, 1);
45                         }
46                 }
47         if (@rolist == 0 && $in{readonly} == 2) {
48                 $in{readonly} = 0;
49                 }
50         }
51
52 &lock_file($config{dfstab_file});
53 foreach $s (@shlist) {
54         $taken = $s if ($s->{'dir'} eq $in{directory});
55         }
56
57 if (defined($in{'idx'})) {
58         $share = $shlist[$in{'idx'}];
59         $olddir = $share->{'dir'};
60         }
61 $share->{'dir'} = $in{'directory'};
62 $share->{'desc'} = $in{'desc'};
63 $share->{'type'} = 'nfs';
64
65 if (defined($in{'idx'})) {
66         # Changing an existing share
67         if ($taken && $taken->{'index'} != $in{'idx'}) {
68                 &error(&text('save_ealready', $in{'directory'}));
69                 }
70         &parse_options($share->{'opts'});
71         &set_options();
72         $share->{'opts'} = &join_options();
73         &modify_share($share);
74         }
75 else {
76         # Creating a new share
77         if ($taken) {
78                 &error(&text('save_ealready', $in{'directory'}));
79                 }
80         &set_options();
81         $share->{'opts'} = &join_options();
82         &create_share($share);
83         }
84 &unlock_file($config{dfstab_file});
85 if (defined($in{'idx'})) {
86         &webmin_log('modify', 'share', $olddir, \%in);
87         }
88 else {
89         &webmin_log('create', 'share', $share->{'dir'}, \%in);
90         }
91 &redirect("");
92
93 # set_options()
94 # Fill in the options associative array
95 sub set_options
96 {
97 if ($in{readonly} == 0) { delete($options{"ro"}); }
98 elsif ($in{readonly} == 1) { $options{"ro"} = ""; }
99 elsif ($in{readonly} == 2) { $options{"ro"} = join(':', @rolist); }
100
101 if ($in{readwrite} == 0) { delete($options{"rw"}); }
102 elsif ($in{readwrite} == 1) { $options{"rw"} = ""; }
103 elsif ($in{readwrite} == 2) { $options{"rw"} = join(':', @rwlist); }
104
105 if ($in{root} == 0) { delete($options{"root"}); }
106 elsif ($in{root} == 2) { $options{"root"} = join(':', @rtlist); }
107
108 if (!$access{'simple'}) {
109         if ($in{nosub}) { $options{"nosub"} = ""; }
110         else { delete($options{"nosub"}); }
111
112         if ($in{nosuid}) { $options{"nosuid"} = ""; }
113         else { delete($options{"nosuid"}); }
114
115         if ($in{secure}) { $options{"secure"} = ""; }
116         else { delete($options{"secure"}); }
117
118         if ($in{kerberos}) { $options{"kerberos"} = ""; }
119         else { delete($options{"kerberos"}); }
120
121         if ($in{'anon_m'} == 0) { delete($options{"anon"}); }
122         elsif ($in{'anon_m'} == 1) { $options{"anon"} = -1; }
123         else { $options{"anon"} = getpwnam($in{"anon"}); }
124
125         if ($in{aclok}) { $options{"aclok"} = ""; }
126         else { delete($options{"aclok"}); }
127
128         if ($gconfig{'os_version'} >= 7) {
129                 if ($in{'public'}) { $options{'public'} = ""; }
130                 else { delete($options{'public'}); }
131                 if (!$in{'index_def'}) { $options{'index'} = $in{'index'}; }
132                 else { delete($options{'index'}); }
133                 }
134         }
135 }
136
137 # check_hosts(host, host, ...)
138 # Die if any of the listed hosts does not exist
139 sub check_hosts
140 {
141 local $h;
142 if ($gconfig{'os_version'} < 7) {
143         foreach $h (@_) {
144                 &to_ipaddress($h) || &to_ip6address($h) ||
145                         &error(&text('save_ehost', $h));
146                 }
147         }
148 }
149
150