Handle hostnames with upper-case letters
[webmin.git] / exports / save_export.cgi
1 #!/usr/local/bin/perl
2 # save_export.cgi
3 # Save, create or delete an export
4
5 require './exports-lib.pl';
6 &ReadParse();
7 &lock_file($config{'exports_file'});
8 @exps = &list_exports();
9
10 if ($in{'delete'}) {
11         # Deleting some export
12         $exp = $exps[$in{'idx'}];
13         &delete_export($exp);
14         }
15 else {
16         if (!$in{'new'}) {
17                 # Get old export
18                 $oldexp = $exps[$in{'idx'}];
19                 %opts = %{$oldexp->{'options'}};
20                 }
21
22         # Validate and parse inputs
23         &error_setup($text{'save_err'});
24         $exp{'via_pfs'} = ($exp{'pfs'} ne "") ? $in{'via_pfs'} : 0;
25         -d $in{'dir'} || &error(&text('save_edir', $in{'dir'}));
26         $exp{'dir'} = $in{'dir'};
27         $exp{'pfs'} = $in{'pfs'};
28         $exp{'active'} = $in{'active'};
29         
30         if ($in{'mode'} == 0) { $exp{'host'} = "=public"; }
31         elsif ($in{'mode'} == 1) {
32                 $in{'netgroup'} =~ /^\S+$/ ||
33                         &error($text{'save_enetgroup'});
34                 $exp{'host'} = '@'.$in{'netgroup'};
35                 }
36         elsif ($in{'mode'} == 2) {
37                 &check_ipaddress($in{'network'}) ||
38                         &error(&text('save_enetwork', $in{'network'}));
39                 &check_ipaddress($in{'netmask'}) ||
40                         &error(&text('save_enetmask', $in{'netmask'}));
41                 $exp{'host'} = $in{'network'}."/".$in{'netmask'};
42                 }
43         elsif ($in{'mode'} == 3) { $exp{'host'} = ""; }
44         else {
45                 $in{'host'} =~ /\*/ || &to_ipaddress($in{'host'}) ||
46                         &error(&text('save_ehost', $in{'host'}));
47                 $exp{'host'} = $in{'host'};
48                 }
49
50         # Authentication is in the host name
51         # Only sys and krb5 for the moment
52         local $auth = "";
53         if ($in{'auth'}) {
54             if ($in{'sec'} == 0) { $auth = "krb5"; }
55             if ($in{'sec'} == 1) { $auth = "krb5i"; }
56             if ($in{'sec'} == 2) { $auth = "krb5p"; }
57         }
58         if ($auth ne "") { $exp{'host'} = "gss/$auth"; }
59
60         # validate and parse options
61         delete($opts{'rw'}); delete($opts{'ro'});
62         if ($in{'ro'}) {
63             $opts{'ro'} = "";
64         } else {
65             $opts{'rw'} = "";
66         }
67         
68         delete($opts{'secure'}); delete($opts{'insecure'});
69         $opts{'insecure'} = "" if ($in{'insecure'});
70
71         delete($opts{'no_subtree_check'}); delete($opts{'subtree_check'});
72         $opts{'no_subtree_check'} = "" if ($in{'no_subtree_check'});
73
74         delete($opts{'nohide'}); delete($opts{'hide'});
75         $opts{'nohide'} = "" if ($in{'nohide'});
76         
77         delete($opts{'sync'}); delete($opts{'async'});
78         if ($in{'sync'} == 1) {
79             $opts{'sync'} = "";
80         } elsif ($in{'sync'} == 2) {
81             $opts{'async'} = "";
82         }
83
84         delete($opts{'root_squash'}); delete($opts{'no_root_squash'});
85         delete($opts{'all_squash'}); delete($opts{'no_all_squash'});
86         $opts{'no_root_squash'} = "" if ($in{'squash'} == 0);
87         $opts{'all_squash'} = "" if ($in{'squash'} == 2);
88
89         if ($in{'anonuid_def'}) { delete($opts{'anonuid'}); }
90         elsif ($in{'anonuid'} =~ /^-?[0-9]+$/) {
91             $opts{'anonuid'} = $in{'anonuid'}; }
92         else { $opts{'anonuid'} = getpwnam($in{'anonuid'}); }
93
94         if ($in{'anongid_def'}) { delete($opts{'anongid'}); }
95         elsif ($in{'anongid'} =~ /^-?[0-9]+$/) {
96             $opts{'anongid'} = $in{'anongid'}; }
97         else { $opts{'anongid'} = getgrnam($in{'anongid'}); }
98
99         # NFSv2 specific options
100         delete($opts{'link_relative'}); delete($opts{'link_absolute'});
101         delete($opts{'noaccess'});
102         delete($opts{'squash_uids'});
103         delete($opts{'squash_gids'});
104         delete($opts{'map_daemon'});
105
106         if (nfs_max_version("localhost") == 2) {
107             $opts{'link_relative'} = "" if ($in{'link_relative'});
108             $opts{'noaccess'} = "" if ($in{'noaccess'});
109
110             if (!$in{'squash_uids_def'}) {
111                 if ($in{'squash_uids'} !~ /^[\d+\-\,]+$/) {
112                     &error($text{'save_euids'});
113                 } else {
114                     $opts{'squash_uids'} = $in{'squash_uids'};
115                     $opts{'map_daemon'} = "";
116                 }
117             }
118             
119             if (!$in{'squash_gids_def'}) {
120                 if ($in{'squash_gids'} !~ /^[\d+\-\,]+$/) {
121                     &error($text{'save_egids'});
122                 } else {
123                     $opts{'squash_gids'} = $in{'squash_gids'};
124                     $opts{'map_daemon'} = "";
125                 }
126             }
127         }
128
129         $exp{'options'} = \%opts;
130         if ($in{'new'}) {
131             if ($in{'via_pfs'} == 1) {
132                 &create_export_via_pfs(\%exp);
133             } else {
134                 &create_export(\%exp);
135             }
136         } else {
137             &modify_export(\%exp, $oldexp);
138         }
139     }
140 &unlock_file($config{'exports_file'});
141 if ($in{'delete'}) {
142         &webmin_log("delete", "export", $exp->{'dir'}, $exp);
143         }
144 elsif ($in{'new'}) {
145         &webmin_log("create", "export", $exp{'dir'}, \%exp);
146         }
147 else {
148         &webmin_log("modify", "export", $exp{'dir'}, \%exp);
149         }
150 &redirect("");
151