52 changes: 52 additions & 0 deletions tests/test-put-symlink-augnew.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /bin/bash

# Test that we don't follow symlinks when writing to .augnew

ROOT=$abs_top_builddir/build/test-put-symlink-augnew
LENSES=$abs_top_srcdir/lenses

HOSTS=$ROOT/etc/hosts
HOSTS_AUGNEW=${HOSTS}.augnew

ATTACK_FILE=$ROOT/other/attack

rm -rf $ROOT
mkdir -p $(dirname $HOSTS)
mkdir -p $(dirname $ATTACK_FILE)

cat <<EOF > $HOSTS
127.0.0.1 localhost
EOF
touch $ATTACK_FILE

(cd $(dirname $HOSTS) && ln -s ../other/attack $(basename $HOSTS).augnew)

HOSTS_SUM=$(sum $HOSTS)

augtool --nostdinc -I $LENSES -r $ROOT --new > /dev/null <<EOF
set /files/etc/hosts/1/alias myhost
save
EOF

if [ ! -f $HOSTS ] ; then
echo "/etc/hosts is no longer a regular file"
exit 1
fi
if [ ! "x${HOSTS_SUM}" = "x$(sum $HOSTS)" ]; then
echo "/etc/hosts has changed"
exit 1
fi

if [ ! -f $HOSTS_AUGNEW ] ; then
echo "/etc/hosts.augnew is still a symlink, should be unlinked"
exit 1
fi
if ! grep myhost $HOSTS_AUGNEW >/dev/null; then
echo "/etc/hosts does not contain the modification"
exit 1
fi

if [ -s $ATTACK_FILE ]; then
echo "/other/attack now contains data, should be blank"
exit 1
fi
52 changes: 52 additions & 0 deletions tests/test-put-symlink-augsave.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /bin/bash

# Test that we don't follow .augsave symlinks

ROOT=$abs_top_builddir/build/test-put-symlink-augsave
LENSES=$abs_top_srcdir/lenses

HOSTS=$ROOT/etc/hosts
HOSTS_AUGSAVE=${HOSTS}.augsave

ATTACK_FILE=$ROOT/other/attack

rm -rf $ROOT
mkdir -p $(dirname $HOSTS)
mkdir -p $(dirname $ATTACK_FILE)

cat <<EOF > $HOSTS
127.0.0.1 localhost
EOF
HOSTS_SUM=$(sum $HOSTS)

touch $ATTACK_FILE
(cd $(dirname $HOSTS) && ln -s ../other/attack $(basename $HOSTS).augsave)

# Now ask for the original to be saved in .augsave
augtool --nostdinc -I $LENSES -r $ROOT --backup > /dev/null <<EOF
set /files/etc/hosts/1/alias myhost
save
EOF

if [ ! -f $HOSTS ] ; then
echo "/etc/hosts is no longer a regular file"
exit 1
fi
if [ ! -f $HOSTS_AUGNEW ] ; then
echo "/etc/hosts.augsave is still a symlink, should be unlinked"
exit 1
fi

if [ ! "x${HOSTS_SUM}" = "x$(sum $HOSTS_AUGSAVE)" ]; then
echo "/etc/hosts.augsave has changed from the original /etc/hosts"
exit 1
fi
if ! grep myhost $HOSTS >/dev/null; then
echo "/etc/hosts does not contain the modification"
exit 1
fi

if [ -s $ATTACK_FILE ]; then
echo "/other/attack now contains data, should be blank"
exit 1
fi
52 changes: 52 additions & 0 deletions tests/test-put-symlink-augtemp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /bin/bash

# Test that we don't follow .augnew symlinks (regression test)

ROOT=$abs_top_builddir/build/test-put-symlink-augtemp
LENSES=$abs_top_srcdir/lenses

HOSTS=$ROOT/etc/hosts
HOSTS_AUGNEW=${HOSTS}.augnew

ATTACK_FILE=$ROOT/other/attack

rm -rf $ROOT
mkdir -p $(dirname $HOSTS)
mkdir -p $(dirname $ATTACK_FILE)

cat <<EOF > $HOSTS
127.0.0.1 localhost
EOF
touch $ATTACK_FILE

(cd $(dirname $HOSTS) && ln -s ../other/attack $(basename $HOSTS).augnew)

# Test the normal save code path which would use a temp augnew file
augtool --nostdinc -I $LENSES -r $ROOT > /dev/null <<EOF
set /files/etc/hosts/1/alias myhost1
save
EOF

if [ -h $HOSTS ] ; then
echo "/etc/hosts is now a symlink, pointing to" $(readlink $HOSTS)
exit 1
fi
if ! grep myhost1 $HOSTS >/dev/null; then
echo "/etc/hosts does not contain the modification"
exit 1
fi

if [ ! -h $HOSTS_AUGNEW ] ; then
echo "/etc/hosts.augnew is not a symbolic link"
exit 1
fi
LINK=$(readlink $HOSTS_AUGNEW)
if [ "x$LINK" != "x../other/attack" ] ; then
echo "/etc/hosts.augnew no longer links to ../other/attack"
exit 1
fi

if [ -s $ATTACK_FILE ]; then
echo "/other/attack now contains data, should be blank"
exit 1
fi
5 changes: 5 additions & 0 deletions tests/test-put-symlink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ if [ "x$LINK" != "x../other/hosts" ] ; then
echo "/etc/hosts does not link to ../other/hosts"
exit 1
fi

if ! grep myhost $REAL_HOSTS >/dev/null; then
echo "/other/hosts does not contain the modification"
exit 1
fi
5 changes: 3 additions & 2 deletions tests/test-save-empty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ EOF

expected_errors() {
cat <<EOF
/augeas/files/etc/hosts/error = "open_augnew"
/augeas/files/etc/hosts/error = "mk_augtemp"
/augeas/files/etc/hosts/error/message = "No such file or directory"
EOF
}
Expand All @@ -30,7 +30,8 @@ EXPECTED=$(expected_errors)

if [ "$ACTUAL" != "$EXPECTED" ]
then
echo "No error on missing /etc directory"
echo "No error on missing /etc directory:"
echo "$ACTUAL"
exit 1
fi

Expand Down