1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#!/usr/bin/perl
open INFILE, "/etc/postgresql/pg_hba.conf" or die "Could not read /etc/postgresql/pg_hba.conf";
open TEMPLATE, "/usr/share/postgresql/pg_hba.conf.sample" or die "Could not read /usr/lib/postgresql/share/pg_hba.conf.sample";
print "
#!# Lines beginning #!# are comments from the old version of this file #!#
#!# and should be deleted if they are no longer correct #!#
";
my $first = 1;
my ($type, $database, $ip, $mask, $cidr, $method, $param);
while (<TEMPLATE>) {
chomp;
if (/^# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD$/) {
last;
} else {
print $_, "\n";
}
}
while (<INFILE>) {
chomp;
if (/^$/) {
print "\n";
} elsif (/^#/) {
print "#!", $_, "\n";
} else {
if ($first) {
print "
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD\n\n";
$first = ! $first;
}
@fields = split /\s+/, $_;
my $badfield;
if ($fields[0] eq "local") {
$type = $fields[0];
$database = $fields[1];
$ip = "";
$mask = "";
$cidr = "";
$method = $fields[2];
$param = $fields[3];
if ($method eq "ident" or $method = "pam") {
$badfield = 4;
} else {
$badfield = 3;
}
} else {
$type = $fields[0];
$database = $fields[1];
$ip = $fields[2];
if ($ip =~ m|/|) {
$cidr = $ip;
$ip = "";
$mask = "";
$method = $fields[3];
$param = $fields[4];
$badfield = 5;
} else {
$cidr = "";
$mask = $fields[3];
$method = $fields[4];
$param = $fields[5];
$badfield = 6;
}
}
die "pg_hba.conf contains a field after the authentication specification;\nthe file is corrupt or has already been converted.\n"
unless (! defined $fields[$badfield]);
if ($cidr eq "") {
printf("%-7s %-11s all %-15s %-15s %s %s\n", $type, $database, $ip, $mask, $method, $param);
} else {
printf("%-7s %-11s all %-15s %s %s\n", $type, $database, $cidr, $method, $param);
}
}
}
exit 0;
|