From b13782fdd8647813c981a21ff9acde3b80a75827 Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Thu, 1 Sep 2011 22:25:24 -0400 Subject: [PATCH] Create the bucardo user before we call bucardo.schema Store the password in the .pgpass file if we can. Remove some password chars that cause problems in the shell. --- bucardo | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/bucardo b/bucardo index 6a67e83c9..678d30118 100755 --- a/bucardo +++ b/bucardo @@ -1589,7 +1589,7 @@ sub add_database { if ('postgres' eq $vals->{dbtype}) { ## For Postgres, we get a little fancy and try to account for instances - ## where the bucardo user may not exist yet, by reconnecting and + ## where the bucardo user may not exist yet, by reconnecting and ## creating said user if needed. my ($type,$dsn,$user,$pass,$ssp) = split /\n/ => $dbconn; @@ -8042,8 +8042,38 @@ sub install { die "Sorry, Bucardo requires Postgres version 8.1 or higher. This is only $maj.$min\n"; } + ## Create the bucardo user if needed + $COM = qq{$PSQL -c "SELECT 1 FROM pg_user WHERE usename = 'bucardo'"}; + $res = qx{$COM 2>&1}; + + if ($res !~ /1/) { + print "Creating user 'bucardo'\n"; + my $pass = generate_password(); + $SQL = qq{CREATE USER bucardo SUPERUSER PASSWORD '$pass'}, + $COM = qq{$PSQL -c "$SQL"}; + $res = qx{$COM 2>&1}; + + ## Put the password into the .pgpass file + my $file = "$ENV{HOME}/.pgpass"; + my $fh; + if (open my $fh, '>>', $file) { + printf {$fh} "%s:%s:%s:%s:%s\n", + $host =~ /^\w/ ? $host : '*', + $port =~ /^\d/ ? $port : '*', + '*', + 'bucardo', + $pass; + close $fh or warn qq{Could not close file "$file": $!\n}; + chmod 0600, $file; + } + else { + print qq{Could not append password information to file "$file"\n}; + print qq{Password for user bucardo is: $pass\n}; + print qq{You probably want to change it or put it in a .pgpass file\n}; + } + } + $COM = "$PSQL -AX -qt -f $schema_file 2>&1"; - #print "COM=$COM\n"; print "Attempting to create and populate the bucardo database and schema\n"; @@ -8166,7 +8196,7 @@ sub generate_password { ## Arguments: none ## Returns: new password - my @chars = split // => q!ABCDEFGHJKMNPQRSTWXYZabcdefghjkmnpqrstwxyz23456789@#$%^&*(){}[];<>./?!; + my @chars = split // => q!ABCDEFGHJKMNPQRSTWXYZabcdefghjkmnpqrstwxyz23456789@#%^&(){}[];./!; my $pass = join '' => @chars[map{ rand @chars }(1..42)]; return $pass; -- 2.39.5