Handle hostnames with upper-case letters
[webmin.git] / file / save_export.cgi
1 #!/usr/local/bin/perl
2 # save_export.cgi
3 # Update, create or delete an NFS export
4
5 require './file-lib.pl';
6 $disallowed_buttons{'sharing'} && &error($text{'ebutton'});
7 &ReadParse();
8 print "Content-type: text/plain\n\n";
9 if ($access{'ro'} || $access{'uid'}) {
10         # User has no access to NFS
11         print "0\n";
12         exit;
13         }
14
15 &read_acl(\%acl, undef);
16 %einfo = &get_module_info("exports");
17 %dinfo = &get_module_info("dfsadmin");
18 %binfo = &get_module_info("bsdexports");
19
20 if (%einfo && &check_os_support(\%einfo)) {
21         # Linux NFS exports
22         &module_check("exports");
23         &foreign_require("exports", "exports-lib.pl");
24         %econfig = &foreign_config("exports");
25         &lock_file($econfig{'exports_file'});
26         foreach $e (&foreign_call("exports", "list_exports")) {
27                 push(@{$exp{$e->{'dir'}}}, $e);
28                 }
29         if ($in{'delete'}) {
30                 # Delete all exports for some dir
31                 foreach $e (reverse(@{$exp{$in{'path'}}})) {
32                         &foreign_call("exports", "delete_export", $e);
33                         }
34                 }
35         else {
36                 # Adding or updating an export
37                 if (!$in{'new'}) {
38                         # Updating, so delete old exports first
39                         foreach $e (reverse(@{$exp{$in{'path'}}})) {
40                                 $host{$e->{'host'}} = $e;
41                                 &foreign_call("exports", "delete_export", $e);
42                                 }
43                         }
44                 for($i=0; $in{"host$i"}; $i++) {
45                         $h = $in{"host$i"} eq '*' ? '' : $in{"host$i"};
46                         $e = $host{$h};
47                         $e = { 'active' => 1,
48                                'host' => $h,
49                                'dir' => $in{'path'} } if (!$e);
50                         delete($e->{'options'}->{'ro'});
51                         if ($in{"ro$i"}) {
52                                 $e->{'options'}->{'ro'} = '';
53                                 }
54                         delete($e->{'options'}->{'all_squash'});
55                         delete($e->{'options'}->{'no_root_squash'});
56                         if ($in{"squash$i"} == 0) {
57                                 $e->{'options'}->{'all_squash'} = '';
58                                 }
59                         elsif ($in{"squash$i"} == 2) {
60                                 $e->{'options'}->{'no_root_squash'} = '';
61                                 }
62                         &foreign_call("exports", "create_export", $e);
63                         }
64                 }
65         &unlock_file($econfig{'exports_file'});
66
67         # Apply configuration
68         &exports::restart_mountd();
69
70         &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
71                     'export', $in{'path'});
72         print "1\n";
73         }
74 elsif (%dinfo && &check_os_support(\%dinfo)) {
75         # Solaris NFS shares
76         &module_check("dfsadmin");
77         &foreign_require("dfsadmin", "dfs-lib.pl");
78         %iconfig = &foreign_config("dfsadmin");
79         &lock_file($iconfig{'dfstab_file'});
80         @shlist = &foreign_call("dfsadmin", "list_shares");
81         foreach $s (@shlist) {
82                 $share = $s if ($s->{'dir'} eq $in{'path'});
83                 }
84         if ($in{'delete'}) {
85                 # Delete existing share
86                 &foreign_call("dfsadmin", "delete_share", $share);
87                 }
88         elsif ($in{'new'}) {
89                 # Create new share
90                 foreach $r ('ro', 'rw', 'root') {
91                         if ($in{$r} ne '-') {
92                                 $in{$r} =~ s/\s+/:/g;
93                                 $opts->{$r} = $in{$r};
94                                 }
95                         }
96                 $share->{'dir'} = $in{'path'};
97                 $share->{'desc'} = $in{'desc'};
98                 $share->{'opts'} =
99                         &foreign_call("dfsadmin", "join_options", $opts);
100                 &foreign_call("dfsadmin", "create_share", $share);
101                 }
102         else {
103                 # Update existing share
104                 $opts = &foreign_call("dfsadmin", "parse_options",
105                                       $share->{'opts'});
106                 foreach $r ('ro', 'rw', 'root') {
107                         if ($in{$r} eq '-') { delete($opts->{$r}); }
108                         else {
109                                 $in{$r} =~ s/\s+/:/g;
110                                 $opts->{$r} = $in{$r};
111                                 }
112                         }
113                 $share->{'dir'} = $in{'path'};
114                 $share->{'desc'} = $in{'desc'};
115                 $share->{'opts'} =
116                         &foreign_call("dfsadmin", "join_options", $opts);
117                 &foreign_call("dfsadmin", "modify_share", $share);
118                 }
119         &unlock_file($iconfig{'dfstab_file'});
120
121         # Apply changes to NFS daemon
122         &dfsadmin::apply_configuration();
123
124         &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
125                     'export', $in{'path'});
126         print "1\n";
127         }
128 elsif (%binfo && &check_os_support(\%binfo)) {
129         # BSD NFS exports
130         &module_check("bsdexports");
131         }
132 else {
133         # No NFS modules installed or supported
134         print "0\n";
135         }
136
137 sub module_check
138 {
139 if (!$acl{$base_remote_user,$_[0]}) {
140         print "0\n";
141         exit;
142         }
143 }
144