|
Mike Gahagan |
ce1afc |
#!/bin/bash
|
|
Mike Gahagan |
ce1afc |
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
|
|
Mike Gahagan |
ce1afc |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# runtest.sh of /CoreOS/openssh/Sanity/port-forwarding
|
|
Mike Gahagan |
ce1afc |
# Description: Testing port forwarding (ideally all possibilities: -L, -R, -D)
|
|
Mike Gahagan |
ce1afc |
# Author: Stanislav Zidek <szidek@redhat.com>
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# Copyright (c) 2015 Red Hat, Inc.
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# This program is free software: you can redistribute it and/or
|
|
Mike Gahagan |
ce1afc |
# modify it under the terms of the GNU General Public License as
|
|
Mike Gahagan |
ce1afc |
# published by the Free Software Foundation, either version 2 of
|
|
Mike Gahagan |
ce1afc |
# the License, or (at your option) any later version.
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# This program is distributed in the hope that it will be
|
|
Mike Gahagan |
ce1afc |
# useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
Mike Gahagan |
ce1afc |
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
Mike Gahagan |
ce1afc |
# PURPOSE. See the GNU General Public License for more details.
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# You should have received a copy of the GNU General Public License
|
|
Mike Gahagan |
ce1afc |
# along with this program. If not, see http://www.gnu.org/licenses/.
|
|
Mike Gahagan |
ce1afc |
#
|
|
Mike Gahagan |
ce1afc |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
# Include Beaker environment
|
|
Mike Gahagan |
ce1afc |
. /usr/share/beakerlib/beakerlib.sh || exit 1
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
PACKAGE="openssh"
|
|
Mike Gahagan |
ce1afc |
USER="user$RANDOM"
|
|
Mike Gahagan |
ce1afc |
FORWARDED=$((RANDOM % 100 + 6800))
|
|
Mike Gahagan |
ce1afc |
LISTEN=$((RANDOM % 100 + 6900))
|
|
Mike Gahagan |
ce1afc |
TIMEOUT=5
|
|
Mike Gahagan |
ce1afc |
MESSAGE="HUGE_SUCCESS"
|
|
Mike Gahagan |
ce1afc |
SSH_OPTIONS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
rlJournalStart
|
|
Mike Gahagan |
ce1afc |
rlPhaseStartSetup
|
|
Mike Gahagan |
ce1afc |
rlAssertRpm $PACKAGE
|
|
Jakub Jelen |
ba99e0 |
rlFileBackup /etc/ssh/sshd_config
|
|
Mike Gahagan |
ce1afc |
rlRun "useradd -m $USER"
|
|
Mike Gahagan |
ce1afc |
rlRun "su - $USER -c \"mkdir .ssh; chmod 700 .ssh; cd .ssh; ssh-keygen -N '' -f id_rsa; cat id_rsa.pub >authorized_keys; chmod 600 authorized_keys\""
|
|
Mike Gahagan |
ce1afc |
rlRun "echo 'LogLevel DEBUG' >>/etc/ssh/sshd_config"
|
|
Mike Gahagan |
ce1afc |
rlServiceStart sshd
|
|
Mike Gahagan |
ce1afc |
rlRun "IP=\$( ip a |grep 'scope global' |grep -w inet |cut -d'/' -f1 |awk '{ print \$2 }' |tail -1 )"
|
|
Mike Gahagan |
ce1afc |
rlRun "echo 'IP=$IP'"
|
|
Mike Gahagan |
ce1afc |
rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory"
|
|
Mike Gahagan |
ce1afc |
rlRun "pushd $TmpDir"
|
|
Mike Gahagan |
ce1afc |
rlPhaseEnd
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
forwarding_test() {
|
|
Mike Gahagan |
ce1afc |
EXP_RESULT=$1
|
|
Mike Gahagan |
ce1afc |
FORWARDED=$2
|
|
Mike Gahagan |
ce1afc |
HOST=$3
|
|
Mike Gahagan |
ce1afc |
LISTEN=$4
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
rlRun "nc -l $LISTEN &>listen.log &"
|
|
Mike Gahagan |
ce1afc |
LISTEN_PID=$!
|
|
Mike Gahagan |
ce1afc |
rlWaitForSocket $LISTEN -t $TIMEOUT
|
|
Mike Gahagan |
ce1afc |
rlRun "ps -fp $LISTEN_PID"
|
|
Mike Gahagan |
ce1afc |
rlRun "su - $USER -c \"ssh $SSH_OPTIONS -N -L $FORWARDED:$HOST:$LISTEN $USER@localhost &\" &>tunnel.log"
|
|
Mike Gahagan |
ce1afc |
rlRun "ps -fC ssh"
|
|
Mike Gahagan |
ce1afc |
rlRun "SSH_PID=\$( pgrep -n -u $USER ssh )"
|
|
Mike Gahagan |
ce1afc |
rlRun "echo SSH_PID is '$SSH_PID'"
|
|
Mike Gahagan |
ce1afc |
rlWaitForSocket $FORWARDED -t $TIMEOUT
|
|
Mike Gahagan |
ce1afc |
rlRun "[[ -n '$SSH_PID' ]] && ps -fp $SSH_PID"
|
|
Jakub Jelen |
6b2140 |
rlRun "echo '$MESSAGE'|nc localhost $FORWARDED" 0,1
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
if [[ $EXP_RESULT == "success" ]]; then
|
|
Mike Gahagan |
ce1afc |
rlAssertGrep "$MESSAGE" listen.log
|
|
Mike Gahagan |
ce1afc |
else # failure expected
|
|
Mike Gahagan |
ce1afc |
rlAssertGrep "open failed" tunnel.log -i
|
|
Mike Gahagan |
ce1afc |
rlAssertGrep "administratively prohibited" tunnel.log -i
|
|
Mike Gahagan |
ce1afc |
rlAssertNotGrep "$MESSAGE" listen.log
|
|
Mike Gahagan |
ce1afc |
fi
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
rlRun "kill -9 $LISTEN_PID $SSH_PID" 0,1 "Killing cleanup"
|
|
Mike Gahagan |
ce1afc |
rlWaitForSocket $LISTEN -t $TIMEOUT --close
|
|
Mike Gahagan |
ce1afc |
rlWaitForSocket $FORWARDED -t $TIMEOUT --close
|
|
Mike Gahagan |
ce1afc |
if ! rlGetPhaseState; then
|
|
Mike Gahagan |
ce1afc |
rlRun "cat listen.log"
|
|
Mike Gahagan |
ce1afc |
rlRun "cat tunnel.log"
|
|
Mike Gahagan |
ce1afc |
fi
|
|
Jakub Jelen |
ba99e0 |
rlFileSubmit listen.log tunnel.log
|
|
Jakub Jelen |
ba99e0 |
rlRun "rm -f *.log;"
|
|
Mike Gahagan |
ce1afc |
}
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
rlPhaseStartTest "Local forwarding"
|
|
Mike Gahagan |
ce1afc |
forwarding_test "success" $FORWARDED localhost $LISTEN
|
|
Mike Gahagan |
ce1afc |
((FORWARDED+=1))
|
|
Mike Gahagan |
ce1afc |
((LISTEN+=1))
|
|
Mike Gahagan |
ce1afc |
rlPhaseEnd
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
rlPhaseStartTest "PermitOpen with 'any'"
|
|
Mike Gahagan |
ce1afc |
rlFileBackup --namespace permitopen_any /etc/ssh/sshd_config /etc/hosts
|
|
Mike Gahagan |
ce1afc |
rlRun "echo 'PermitOpen any' >>/etc/ssh/sshd_config"
|
|
Mike Gahagan |
ce1afc |
rlRun "echo '$IP anyhost1 anyhost2' >>/etc/hosts"
|
|
Mike Gahagan |
ce1afc |
rlRun "service sshd restart"
|
|
Mike Gahagan |
ce1afc |
for i in `seq 3`; do
|
|
Mike Gahagan |
ce1afc |
forwarding_test "success" $FORWARDED anyhost1 $LISTEN
|
|
Mike Gahagan |
ce1afc |
forwarding_test "success" $FORWARDED anyhost2 $LISTEN
|
|
Mike Gahagan |
ce1afc |
((FORWARDED+=1))
|
|
Mike Gahagan |
ce1afc |
((LISTEN+=1))
|
|
Mike Gahagan |
ce1afc |
done
|
|
Mike Gahagan |
ce1afc |
rlFileRestore --namespace permitopen_any
|
|
Mike Gahagan |
ce1afc |
rlPhaseEnd
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
if ! rlIsRHEL '<6.7'; then
|
|
Mike Gahagan |
ce1afc |
# PermitOpen with wildcards is new feature in RHEL-6.7
|
|
Mike Gahagan |
ce1afc |
rlPhaseStartTest "PermitOpen with port wildcard"
|
|
Mike Gahagan |
ce1afc |
rlFileBackup --namespace port_wildcard /etc/ssh/sshd_config /etc/hosts
|
|
Mike Gahagan |
ce1afc |
rlRun "echo 'PermitOpen wildportallow:*' >>/etc/ssh/sshd_config"
|
|
Mike Gahagan |
ce1afc |
rlRun "echo '$IP wildportallow wildportdeny' >>/etc/hosts"
|
|
Mike Gahagan |
ce1afc |
rlRun "service sshd restart"
|
|
Mike Gahagan |
ce1afc |
forwarding_test "success" $FORWARDED wildportallow $LISTEN
|
|
Mike Gahagan |
ce1afc |
((FORWARDED+=1))
|
|
Mike Gahagan |
ce1afc |
((LISTEN+=1))
|
|
Mike Gahagan |
ce1afc |
forwarding_test "failure" $FORWARDED wildportdeny $LISTEN
|
|
Mike Gahagan |
ce1afc |
((FORWARDED+=1))
|
|
Mike Gahagan |
ce1afc |
((LISTEN+=1))
|
|
Mike Gahagan |
ce1afc |
rlFileRestore --namespace port_wildcard
|
|
Mike Gahagan |
ce1afc |
rlRun "service sshd restart"
|
|
Mike Gahagan |
ce1afc |
rlPhaseEnd
|
|
Mike Gahagan |
ce1afc |
fi
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
if ! rlIsRHEL '<7.3'; then
|
|
Mike Gahagan |
ce1afc |
rlPhaseStartTest "PermitOpen with host wildcard and specific port"
|
|
Mike Gahagan |
ce1afc |
rlFileBackup --namespace host_wildcard /etc/ssh/sshd_config /etc/hosts
|
|
Mike Gahagan |
ce1afc |
rlRun "echo 'PermitOpen *:$LISTEN' >>/etc/ssh/sshd_config"
|
|
Mike Gahagan |
ce1afc |
rlRun "echo '$IP wildhost1 wildhost2' >>/etc/hosts"
|
|
Mike Gahagan |
ce1afc |
rlRun "service sshd restart"
|
|
Mike Gahagan |
ce1afc |
forwarding_test "success" $FORWARDED wildhost1 $LISTEN
|
|
Mike Gahagan |
ce1afc |
((FORWARDED+=1))
|
|
Mike Gahagan |
ce1afc |
forwarding_test "success" $FORWARDED wildhost2 $LISTEN
|
|
Mike Gahagan |
ce1afc |
((FORWARDED+=1))
|
|
Mike Gahagan |
ce1afc |
((LISTEN+=1)) # different listen port, should fail
|
|
Mike Gahagan |
ce1afc |
forwarding_test "failure" $FORWARDED wildhost2 $LISTEN
|
|
Mike Gahagan |
ce1afc |
rlFileRestore --namespace host_wildcard
|
|
Mike Gahagan |
ce1afc |
rlPhaseEnd
|
|
Mike Gahagan |
ce1afc |
fi
|
|
Mike Gahagan |
ce1afc |
|
|
Mike Gahagan |
ce1afc |
rlPhaseStartCleanup
|
|
Jakub Jelen |
ec02bb |
rlRun "userdel -rf $USER"
|
|
Mike Gahagan |
ce1afc |
rlRun "popd"
|
|
Mike Gahagan |
ce1afc |
rlFileRestore
|
|
Mike Gahagan |
ce1afc |
rlServiceRestore sshd
|
|
Mike Gahagan |
ce1afc |
rlRun "rm -r $TmpDir" 0 "Removing tmp directory"
|
|
Mike Gahagan |
ce1afc |
rlPhaseEnd
|
|
Mike Gahagan |
ce1afc |
rlJournalPrintText
|
|
Mike Gahagan |
ce1afc |
rlJournalEnd
|