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