Handle hostnames with upper-case letters
[webmin.git] / postgresql / save_host.cgi
1 #!/usr/local/bin/perl
2 # save_host.cgi
3 # Create, modify or delete an allowed host record
4
5 require './postgresql-lib.pl';
6 &ReadParse();
7 &lock_file($hba_conf_file);
8 $v = &get_postgresql_version();
9 @all = &get_hba_config($v);
10 $host = $all[$in{'idx'}] if (!$in{'new'});
11 &error_setup($text{'host_err'});
12
13 if ($in{'delete'}) {
14         # delete one host
15         &delete_hba($host, $v);
16         }
17 else {
18         # validate and parse inputs
19         if ($in{'addr_mode'} == 0) {
20                 $host->{'address'} = '0.0.0.0' if (!$host->{'address'});
21                 $object = $host->{'netmask'} = '0.0.0.0';
22                 $host->{'type'} = $in{'ssl'} ? 'hostssl' : 'host';
23                 }
24         elsif ($in{'addr_mode'} == 1) {
25                 &check_ipaddress($in{'host'}) ||
26                   &check_ip6address($in{'host'}) ||
27                         &error($text{'host_ehost'});
28                 $object = $host->{'address'} = $in{'host'};
29                 $host->{'netmask'} = '255.255.255.255';
30                 $host->{'type'} = $in{'ssl'} ? 'hostssl' : 'host';
31                 }
32         elsif ($in{'addr_mode'} == 2) {
33                 # Parse address / netmask
34                 &check_ipaddress($in{'network'}) ||
35                    &check_ip6address($in{'network'}) ||
36                         &error($text{'host_enetwork'});
37                 &check_ipaddress($in{'netmask'}) ||
38                         &error($text{'host_enetmask'});
39                 $host->{'address'} = $in{'network'};
40                 $host->{'netmask'} = $in{'netmask'};
41                 delete($host->{'cidr'});
42                 $host->{'type'} = $in{'ssl'} ? 'hostssl' : 'host';
43                 $object = "$in{'network'}/$in{'netmask'}";
44                 }
45         elsif ($in{'addr_mode'} == 4) {
46                 # Parse address / CIDR
47                 &check_ipaddress($in{'network2'}) ||
48                     &check_ip6address($in{'network2'}) ||
49                         &error($text{'host_enetwork'});
50                 $in{'cidr'} =~ /^\d+$/ ||
51                         &error($text{'host_ecidr'});
52                 $host->{'address'} = $in{'network2'};
53                 $host->{'cidr'} = $in{'cidr'};
54                 delete($host->{'netmask'});
55                 $host->{'type'} = $in{'ssl'} ? 'hostssl' : 'host';
56                 $object = "$in{'network2'}/$in{'cidr'}";
57                 }
58         else {
59                 $object = $host->{'type'} = 'local';
60                 }
61         if ($in{'db'}) {
62                 $host->{'db'} = $in{'db'};
63                 }
64         else {
65                 $in{'dbother'} || &error($text{'host_edb'});
66                 $host->{'db'} = join(",", split(/\s+/, $in{'dbother'}));
67                 }
68         if ($v >= 7.3) {
69                 $in{'user_def'} || $in{'user'} || &error($text{'host_euser'});
70                 $host->{'user'} = $in{'user_def'} ? 'all' :
71                                         join(",", split(/\s+/, $in{'user'}));
72                 }
73         $host->{'auth'} = $in{'auth'};
74         if ($in{'auth'} eq 'password' && $in{'passwordarg'}) {
75                 $in{'password'} =~ /^\S+$/ || &error($text{'host_epassword'});
76                 $host->{'arg'} = $in{'password'};
77                 }
78         elsif ($in{'auth'} eq 'ident' && $in{'identarg'}) {
79                 $in{'ident'} =~ /^\S+$/ || &error($text{'host_eident'});
80                 $host->{'arg'} = $in{'ident'};
81                 }
82         elsif ($in{'auth'} eq 'pam' && $in{'pamarg'}) {
83                 $in{'pam'} =~ /^\S+$/ || &error($text{'host_epam'});
84                 $host->{'arg'} = $in{'pam'};
85                 }
86         else {
87                 $host->{'arg'} = undef;
88                 }
89
90         if ($in{'new'}) {
91                 &create_hba($host, $v);
92                 }
93         else {
94                 &modify_hba($host, $v);
95                 }
96         }
97 &unlock_file($hba_conf_file);
98 &restart_postgresql();
99 &webmin_log($in{'new'} ? 'create' : $in{'delete'} ? 'delete' : 'modify',
100             'hba', $host->{'type'} eq 'local' ? 'local' :
101                    $host->{'netmask'} eq '0.0.0.0' ? 'all' :
102                    $host->{'netmask'} eq '255.255.255.255' ? $host->{'address'}:
103                    "$host->{'address'}/$host->{'netmask'}", $host);
104 &redirect("list_hosts.cgi");
105