Package: autopostgresqlbackup
Version: 1.0-1
Tags: patch

Hi Emmanuel,

autopostgresqlbackup uses login shells ("su -l") for calling multiple
commands as a non-root user. Login shells differ from normal shells
(beyond other things) by the fact that they also source /etc/profile
and .profile on startup, while non-login shells don't. See [1] for
reference.

  [1] http://tanguy.ortolo.eu/blog/article25/shrc

Bigger sites usually use that feature to call some programs upon user
login while those programs are not called from within cron jobs, etc.

In our case, login shells check which printer is either relevant for
the local machine or, in case of SSH connections, for the the machine
from the which the user has logged it. If it can't find any of both it
sends print jobs by mail and warns about that fact upon login:

# su -l postgres -c "LANG=C psql -U postgres  -l -A -F: | sed -ne '/:/ { 
/Name:Owner/d; /template0/d; s/:.*$//; p }'"
Your workstation has no default printer.
                           Printjobs are mailed to [email protected]
************************************************************************
postgres
template1
#

The unexpected output above comes from the script at
/etc/profile.d/bash_profile_dphys.sh, so you should be able to
reproduce this with the following command:

# printf '#!/bin/sh\necho foo\n' > /etc/profile.d/foo.sh

and then run the su-command as shown above.

In our case, this causes following unusual error mails:

pg_dump: [archiver (db)] connection to database "Your" failed: FATAL:  database 
"Your" does not exist
pg_dump: [archiver (db)] connection to database "workstation" failed: FATAL:  
database "workstation" does not exist
pg_dump: [archiver (db)] connection to database "has" failed: FATAL:  database 
"has" does not exist
pg_dump: [archiver (db)] connection to database "no" failed: FATAL:  database 
"no" does not exist
pg_dump: [archiver (db)] connection to database "default" failed: FATAL:  
database "default" does not exist
pg_dump: [archiver (db)] connection to database "printer." failed: FATAL:  
database "printer." does not exist
pg_dump: [archiver (db)] connection to database "Printjobs" failed: FATAL:  
database "Printjobs" does not exist
pg_dump: [archiver (db)] connection to database "are" failed: FATAL:  database 
"are" does not exist
pg_dump: [archiver (db)] connection to database "mailed" failed: FATAL:  
database "mailed" does not exist
pg_dump: [archiver (db)] connection to database "to" failed: FATAL:  database 
"to" does not exist
pg_dump: [archiver (db)] connection to database "[email protected]" failed: 
FATAL:  database "[email protected]" does not exist
pg_dump: [archiver (db)] connection to database "acpid" failed: FATAL:  
database "acpid" does not exist
pg_dump: [archiver (db)] connection to database "apache2" failed: FATAL:  
database "apache2" does not exist
pg_dump: [archiver (db)] connection to database "aptitude-robot" failed: FATAL: 
 database "aptitude-robot" does not exist
pg_dump: [archiver (db)] connection to database "automysqlbackup" failed: 
FATAL:  database "automysqlbackup" does not exist
pg_dump: [archiver (db)] connection to database "autopostgresqlbackup" failed: 
FATAL:  database "autopostgresqlbackup" does not exist
pg_dump: [archiver (db)] connection to database "bsdmainutils" failed: FATAL:  
database "bsdmainutils" does not exist
pg_dump: [archiver (db)] connection to database "console-setup" failed: FATAL:  
database "console-setup" does not exist
pg_dump: [archiver (db)] connection to database "cron" failed: FATAL:  database 
"cron" does not exist
pg_dump: [archiver (db)] connection to database "debsums" failed: FATAL:  
database "debsums" does not exist
pg_dump: [archiver (db)] connection to database "devpts" failed: FATAL:  
database "devpts" does not exist
pg_dump: [archiver (db)] connection to database "dphys-config" failed: FATAL:  
database "dphys-config" does not exist
pg_dump: [archiver (db)] connection to database "fail2ban" failed: FATAL:  
database "fail2ban" does not exist
pg_dump: [archiver (db)] connection to database "grub" failed: FATAL:  database 
"grub" does not exist
pg_dump: [archiver (db)] connection to database "halt" failed: FATAL:  database 
"halt" does not exist
pg_dump: [archiver (db)] connection to database "hddtemp" failed: FATAL:  
database "hddtemp" does not exist
pg_dump: [archiver (db)] connection to database "hobbit-client" failed: FATAL:  
database "hobbit-client" does not exist
pg_dump: [archiver (db)] connection to database "hwclock" failed: FATAL:  
database "hwclock" does not exist
pg_dump: [archiver (db)] connection to database "keyboard" failed: FATAL:  
database "keyboard" does not exist
pg_dump: [archiver (db)] connection to database "locale" failed: FATAL:  
database "locale" does not exist
pg_dump: [archiver (db)] connection to database "mbmon" failed: FATAL:  
database "mbmon" does not exist
pg_dump: [archiver (db)] connection to database "mdadm" failed: FATAL:  
database "mdadm" does not exist
pg_dump: [archiver (db)] connection to database "netdiag" failed: FATAL:  
database "netdiag" does not exist
pg_dump: [archiver (db)] connection to database "networking" failed: FATAL:  
database "networking" does not exist
pg_dump: [archiver (db)] connection to database "nfs-common" failed: FATAL:  
database "nfs-common" does not exist
pg_dump: [archiver (db)] connection to database "nss" failed: FATAL:  database 
"nss" does not exist
pg_dump: [archiver (db)] connection to database "ntp" failed: FATAL:  database 
"ntp" does not exist
pg_dump: [archiver (db)] connection to database "ntpdate" failed: FATAL:  
database "ntpdate" does not exist
pg_dump: [archiver (db)] connection to database "ntp-servers" failed: FATAL:  
database "ntp-servers" does not exist
pg_dump: [archiver (db)] connection to database "rcS" failed: FATAL:  database 
"rcS" does not exist
pg_dump: [archiver (db)] connection to database "rsync" failed: FATAL:  
database "rsync" does not exist
pg_dump: [archiver (db)] connection to database "rsyslog" failed: FATAL:  
database "rsyslog" does not exist
pg_dump: [archiver (db)] connection to database "smartmontools" failed: FATAL:  
database "smartmontools" does not exist
pg_dump: [archiver (db)] connection to database "ssh" failed: FATAL:  database 
"ssh" does not exist
pg_dump: [archiver (db)] connection to database "tmpfs" failed: FATAL:  
database "tmpfs" does not exist
pg_dump: [archiver (db)] connection to database "useradd" failed: FATAL:  
database "useradd" does not exist

This is the wild-expanded contents of /etc/default/ fed into another
command. At the point where the wildcards were expanded, there's as
far as I can see, no chance to inject backticks or a $() construct.

But since the database names are later used inside an eval and can
contain arbitrary user controlled characters, there's an security
issue in here, too, which I will file in a seperate bug report.

If just "su" instead of "su -l" is used, that line works just as
expected:

# su postgres -c "LANG=C psql -U postgres  -l -A -F: | sed -ne '/:/ { 
/Name:Owner/d; /template0/d; s/:.*$//; p }'"
postgres
template1
#

The seocnd "su -l" causes the messages from the profile scripts to end
of up the database dump, too, this time not wildcard-expanded:

.------
| Your workstation has no default printer.
|                            Printjobs are mailed to [email protected]
| ************************************************************************
| --
| -- PostgreSQL database dump
| --
| [...]
`-----

So please remove at least the "-l" in these lines of
/usr/sbin/autopostgresqlbackup:

# fgrep -n 'su -l' /usr/sbin/autopostgresqlbackup.orig 
323:            su -l $SU_USERNAME -c "pg_dump $PGHOST $OPT $1" > $2
388:            DBNAMES="$(su -l $SU_USERNAME -c "LANG=C psql -U $USERNAME 
$PGHOST -l -A -F: | sed -ne '/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p 
}'")"
#

Here's the according patch:

# diff -u /usr/sbin/autopostgresqlbackup.orig /usr/sbin/autopostgresqlbackup
--- /usr/sbin/autopostgresqlbackup.orig 2011-05-28 20:49:12.000000000 +0200
+++ /usr/sbin/autopostgresqlbackup      2013-04-24 16:19:57.051801705 +0200
@@ -320,7 +320,7 @@
        touch $2
        chmod 600 $2
        if [ -n "$SU_USERNAME" ]; then
-               su -l $SU_USERNAME -c "pg_dump $PGHOST $OPT $1" > $2
+               su $SU_USERNAME -c "pg_dump $PGHOST $OPT $1" > $2
        else
                pg_dump --username=$USERNAME $PGHOST $OPT $1 > $2
        fi
@@ -385,7 +385,7 @@
 # If backing up all DBs on the server
 if [ "$DBNAMES" = "all" ]; then
        if [ -n "$SU_USERNAME" ]; then
-               DBNAMES="$(su -l $SU_USERNAME -c "LANG=C psql -U $USERNAME 
$PGHOST -l -A -F: | sed -ne '/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p 
}'")"
+               DBNAMES="$(su $SU_USERNAME -c "LANG=C psql -U $USERNAME $PGHOST 
-l -A -F: | sed -ne '/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }'")"
        else
                DBNAMES="`LANG=C psql -U $USERNAME $PGHOST -l -A -F: | sed -ne 
"/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`"
        fi

TIA.

(Happened on a Wheezy amd64 box, but I doubt that this is relevant.
And since it was easier for me to write the bug report elsewhere, I
did so. :-)

                Regards, Axel
-- 
 ,''`.  |  Axel Beckert <[email protected]>, http://people.debian.org/~abe/
: :' :  |  Debian Developer, ftp.ch.debian.org Admin
`. `'   |  1024D: F067 EA27 26B9 C3FC 1486  202E C09E 1D89 9593 0EDE
  `-    |  4096R: 2517 B724 C5F6 CA99 5329  6E61 2FF9 CD59 6126 16B5


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to