| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# Add, remove, or test a pg_hba.conf entry. |
| 4 |
# |
| 5 |
# (C) 2005-2009 Martin Pitt <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
|
| 17 |
use strict; |
| 18 |
use PgCommon; |
| 19 |
use Getopt::Long; |
| 20 |
use Net::CIDR; |
| 21 |
|
| 22 |
# global variables |
| 23 |
|
| 24 |
my $ip = ''; # default to local unix socket |
| 25 |
my $force_ssl = 0; |
| 26 |
my ($method, $ver_cluster, $db, $user); |
| 27 |
my $mode; |
| 28 |
my @hba; |
| 29 |
|
| 30 |
# Print an error message to stderr and exit with status 2 |
| 31 |
sub error2 { |
| 32 |
print STDERR 'Error: ', $_[0], "\n"; |
| 33 |
exit 2; |
| 34 |
} |
| 35 |
|
| 36 |
# Check if s1 is equal to s2 or s2 is 'all'. |
| 37 |
# Arguments: <s1> <s2> |
| 38 |
sub match_all { |
| 39 |
return ($_[1] eq 'all' || $_[0] eq $_[1]); |
| 40 |
} |
| 41 |
|
| 42 |
# Check if given IP matches the specification in the HBA record. |
| 43 |
# Arguments: <ip> <ref to hba hash> |
| 44 |
sub match_ip { |
| 45 |
my ($ip, $hba) = @_; |
| 46 |
|
| 47 |
# Don't try to mix IPv4 and IPv6 addresses since that will make cidrlookup |
| 48 |
# croak |
| 49 |
return 0 if ((index $ip, ':') < 0) ^ ((index $$hba{'ip'}, ':') < 0); |
| 50 |
|
| 51 |
return Net::CIDR::cidrlookup ($ip, $$hba{'ip'}); |
| 52 |
} |
| 53 |
|
| 54 |
# Check if arguments match any line |
| 55 |
# Return: 1 if match was found, 0 otherwise. |
| 56 |
sub mode_test { |
| 57 |
foreach my $hbarec (@hba) { |
| 58 |
if (!defined($$hbarec{'type'})) { |
| 59 |
next; |
| 60 |
} |
| 61 |
next if $$hbarec{'type'} eq 'comment'; |
| 62 |
next unless match_all ($user, $$hbarec{'user'}) && |
| 63 |
match_all ($db, $$hbarec{'db'}) && |
| 64 |
$$hbarec{'method'} eq $method; |
| 65 |
|
| 66 |
if ($ip) { |
| 67 |
return 1 if |
| 68 |
(($force_ssl && $$hbarec{'type'} eq 'hostssl') || |
| 69 |
$$hbarec{'type'} =~ /^host/) && |
| 70 |
match_ip ($ip, $hbarec); |
| 71 |
} else { |
| 72 |
return 1 if $$hbarec{'type'} eq 'local'; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
# Print hba conf. |
| 80 |
sub mode_print { |
| 81 |
foreach my $hbarec (@hba) { |
| 82 |
print "$$hbarec{'line'}\n"; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
# Generate a pg_hba.conf line that matches the command line args. |
| 87 |
sub create_hba_line { |
| 88 |
if ($ip) { |
| 89 |
return sprintf "%-7s %-11s %-11s %-35s %s\n", |
| 90 |
$force_ssl ? 'hostssl' : 'host', $db, $user, $ip, $method; |
| 91 |
} else { |
| 92 |
return sprintf "%-7s %-11s %-47s %s\n", 'local', $db, $user, $method; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
# parse arguments |
| 97 |
|
| 98 |
my $ip_arg; |
| 99 |
exit 3 unless GetOptions ( |
| 100 |
'cluster=s' => \$ver_cluster, |
| 101 |
'ip=s' => \$ip_arg, |
| 102 |
'method=s' => \$method, |
| 103 |
'force-ssl' => \$force_ssl |
| 104 |
); |
| 105 |
|
| 106 |
if ($#ARGV != 2) { |
| 107 |
print STDERR "Usage: $0 mode [options] <database> <user>\n"; |
| 108 |
exit 2; |
| 109 |
} |
| 110 |
($mode, $db, $user) = @ARGV; |
| 111 |
|
| 112 |
error2 '--cluster must be specified' unless $ver_cluster; |
| 113 |
my ($version, $cluster) = split ('/', $ver_cluster); |
| 114 |
error2 'No version specified with --cluster' unless $version; |
| 115 |
error2 'No cluster specified with --cluster' unless $cluster; |
| 116 |
error2 'Cluster does not exist' unless cluster_exists $version, $cluster; |
| 117 |
if (defined $ip_arg) { |
| 118 |
$ip = Net::CIDR::cidrvalidate $ip_arg; |
| 119 |
error2 'Invalid --ip argument' unless defined $ip; |
| 120 |
} |
| 121 |
|
| 122 |
unless (defined $method) { |
| 123 |
$method = ($ip ? 'md5' : 'ident sameuser'); |
| 124 |
} |
| 125 |
error2 'Invalid --method argument' unless PgCommon::valid_hba_method($method); |
| 126 |
|
| 127 |
# parse file |
| 128 |
|
| 129 |
my $hbafile = "/etc/postgresql/$version/$cluster/pg_hba.conf"; |
| 130 |
@hba = read_pg_hba $hbafile; |
| 131 |
error2 "Could not read $hbafile" unless $#hba; |
| 132 |
|
| 133 |
if ($mode eq 'pg_test_hba') { |
| 134 |
if (mode_test) { |
| 135 |
exit 0; |
| 136 |
} else { |
| 137 |
print create_hba_line(); |
| 138 |
exit 1; |
| 139 |
} |
| 140 |
} elsif ($mode eq 'pg_print_hba') { |
| 141 |
mode_print(); |
| 142 |
} else { |
| 143 |
error2 "Unknown mode: $mode"; |
| 144 |
} |