Handle hostnames with upper-case letters
[webmin.git] / exports-nfs4 / save_export.cgi
1 #!/usr/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 local $nfsv = nfs_max_version("localhost");
10
11 if ($in{'delete'}) {
12         # Deleting some export
13         $exp = $exps[$in{'idx'}];
14         &delete_export($exp);
15         }
16 else {
17         if (!$in{'new'}) {
18                 # Get old export
19                 $oldexp = $exps[$in{'idx'}];
20                 %opts = %{$oldexp->{'options'}};
21         }
22
23         # Validate and parse inputs
24         &error_setup($text{'save_err'});
25         $exp{'via_pfs'} = ($exp{'pfs'} ne "") ? $in{'via_pfs'} : 0;
26         -d $in{'dir'} || &error(&text('save_edir', $in{'dir'}));
27         $exp{'dir'} = $in{'dir'};
28         $exp{'pfs'} = $in{'pfs'};
29         $exp{'active'} = $in{'active'};
30         
31         if ($in{'mode'} == 0) { $exp{'host'} = "=public"; }
32         elsif ($in{'mode'} == 1) {
33                 $in{'netgroup'} =~ /^\S+$/ ||
34                         &error($text{'save_enetgroup'});
35                 $exp{'host'} = '@'.$in{'netgroup'};
36                 }
37         elsif ($in{'mode'} == 2) {
38                 &check_ipaddress($in{'network'}) ||
39                         &error(&text('save_enetwork', $in{'network'}));
40                 &check_ipaddress($in{'netmask'}) ||
41                         &error(&text('save_enetmask', $in{'netmask'}));
42                 $exp{'host'} = $in{'network'}."/".$in{'netmask'};
43                 }
44         elsif ($in{'mode'} == 3) { $exp{'host'} = ""; }
45         #Support for IPv6 address and prefix
46         elsif ($in{'mode'} == 6){
47                 &check_ip6address($in{'address'}) ||
48                         &error(&text('save_eaddress', $in{'address'}));
49                 if(!($in{'prefix'}>0 && $in{'prefix'}<129)){
50                         &error(&text('save_eprefix', $in{'prefix'}));
51                 }
52                 $exp{'host'} = $in{'address'}."/".$in{'prefix'};
53         }
54         else{
55                 $in{'host'} =~ /\*/ || &to_ipaddress($in{'host'}) ||
56                         &error(&text('save_ehost', $in{'host'}));
57                 $exp{'host'} = $in{'host'};
58         }
59
60         # Authentication is in the host name
61         # with support for spkm-3 and lipkey (experimental in NFSv4)
62         
63          @sec_to_name=('sys','krb5','krb5i','krb5p','spkm-3','spkm-3i','spkm-3p','lipkey','lipkeyi','lipkeyp');
64          
65          $string=$in{'the_flav'};        
66          @sec_flav= split(/,/, $in{'the_flav'});
67          $str_flav="";
68          foreach $nb (@sec_flav){
69                if($nb==0){ $str_flav .= "*,";}
70                else{ $str_flav .= "gss/$sec_to_name[$nb],";} 
71          }
72          chop($str_flav);
73          $str_flav="" if($in{'nfsv'}<4);
74          
75          if ($exp{'host'} ne "" && $str_flav ne ""){
76             $exp{'host'}=$str_flav.":".$exp{'host'};
77          }
78          elsif ($exp{'host'} eq "" && $str_flav eq ""){
79             $exp{'host'}="*";
80          }
81          elsif ($exp{'host'} eq ""){
82             $exp{'host'}=$str_flav;
83          }
84
85         # validate and parse options
86         delete ($opts{'fsid'});
87         $opts{'fsid'} = "0" if($in{'is_pfs'});
88         
89         delete($opts{'rw'}); delete($opts{'ro'});
90         if ($in{'ro'}) {
91             $opts{'ro'} = "";
92         } else {
93             $opts{'rw'} = "";
94         }       
95         delete($opts{'secure'}); delete($opts{'insecure'});
96         $opts{'insecure'} = "" if ($in{'insecure'});
97
98         delete($opts{'no_subtree_check'}); delete($opts{'subtree_check'});
99         $opts{'no_subtree_check'} = "" if ($in{'no_subtree_check'});
100
101         delete($opts{'nohide'}); delete($opts{'hide'});
102         $opts{'nohide'} = "" if ($in{'nohide'});
103         
104         delete($opts{'sync'}); delete($opts{'async'});
105         if ($in{'sync'}) {
106             $opts{'sync'} = "";
107         } else {
108             $opts{'async'} = ""; 
109         }
110         
111         delete($opts{'root_squash'}); delete($opts{'no_root_squash'});
112         delete($opts{'all_squash'}); delete($opts{'no_all_squash'});
113         $opts{'no_root_squash'} = "" if ($in{'squash'} == 0);
114         $opts{'all_squash'} = "" if ($in{'squash'} == 2);
115
116         if ($in{'anonuid_def'}) { delete($opts{'anonuid'}); }
117         elsif ($in{'anonuid'} =~ /^-?[0-9]+$/) {
118             $opts{'anonuid'} = $in{'anonuid'}; }
119         else { $opts{'anonuid'} = getpwnam($in{'anonuid'}); }
120
121         if ($in{'anongid_def'}) { delete($opts{'anongid'}); }
122         elsif ($in{'anongid'} =~ /^-?[0-9]+$/) {
123             $opts{'anongid'} = $in{'anongid'}; }
124         else { $opts{'anongid'} = getgrnam($in{'anongid'}); }
125
126         # NFSv2 specific options ---
127         delete($opts{'link_relative'}); delete($opts{'link_absolute'});
128         delete($opts{'noaccess'});
129         delete($opts{'squash_uids'});
130         delete($opts{'squash_gids'});
131         delete($opts{'map_daemon'});
132
133         if (nfs_max_version("localhost") == 2) {
134             $opts{'link_relative'} = "" if ($in{'link_relative'});
135             $opts{'noaccess'} = "" if ($in{'noaccess'});
136
137             if (!$in{'squash_uids_def'}) {
138                 if ($in{'squash_uids'} !~ /^[\d+\-\,]+$/) {
139                     &error($text{'save_euids'});
140                 } else {
141                     $opts{'squash_uids'} = $in{'squash_uids'};
142                     $opts{'map_daemon'} = "";
143                 }
144             }
145             
146             if (!$in{'squash_gids_def'}) {
147                 if ($in{'squash_gids'} !~ /^[\d+\-\,]+$/) {
148                     &error($text{'save_egids'});
149                 } else {
150                     $opts{'squash_gids'} = $in{'squash_gids'};
151                     $opts{'map_daemon'} = "";
152                 }
153             }
154         }
155         # ---
156
157         $exp{'options'} = \%opts;
158         if ($in{'new'}) {
159             if ($in{'is_pfs'} || $nfsv<4) {
160                 &create_export(\%exp);
161             } else {
162                 &create_export_in_root(\%exp);
163             }
164         } else {
165             &modify_export(\%exp, $oldexp);
166         }
167     }
168 &unlock_file($config{'exports_file'});
169 if ($in{'delete'}) {
170         &webmin_log("delete", "export", $exp->{'dir'}, $exp);
171         }
172 elsif ($in{'new'}) {
173         &webmin_log("create", "export", $exp{'dir'}, \%exp);
174         }
175 else {
176         &webmin_log("modify", "export", $exp{'dir'}, \%exp);
177         }
178 &redirect("");
179