vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jakub Jelen 1ce235
#!/bin/bash
Jakub Jelen 1ce235
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
Jakub Jelen 1ce235
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
#   runtest.sh of /CoreOS/openssh/Sanity/pam_ssh_agent_auth
Jakub Jelen 1ce235
#   Description: This is a basic sanity test for pam_ssh_agent_auth
Jakub Jelen 1ce235
#   Author: Jakub Jelen <jjelen@redhat.com>
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
#   Copyright (c) 2015 Red Hat, Inc.
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
#   This program is free software: you can redistribute it and/or
Jakub Jelen 1ce235
#   modify it under the terms of the GNU General Public License as
Jakub Jelen 1ce235
#   published by the Free Software Foundation, either version 2 of
Jakub Jelen 1ce235
#   the License, or (at your option) any later version.
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
#   This program is distributed in the hope that it will be
Jakub Jelen 1ce235
#   useful, but WITHOUT ANY WARRANTY; without even the implied
Jakub Jelen 1ce235
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Jakub Jelen 1ce235
#   PURPOSE.  See the GNU General Public License for more details.
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
#   You should have received a copy of the GNU General Public License
Jakub Jelen 1ce235
#   along with this program. If not, see http://www.gnu.org/licenses/.
Jakub Jelen 1ce235
#
Jakub Jelen 1ce235
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jakub Jelen 1ce235
Jakub Jelen 1ce235
# Include Beaker environment
Jakub Jelen 1ce235
. /usr/bin/rhts-environment.sh || exit 1
Jakub Jelen 1ce235
. /usr/share/beakerlib/beakerlib.sh || exit 1
Jakub Jelen 1ce235
Jakub Jelen 1ce235
PACKAGE="openssh"
Jakub Jelen 1ce235
PAM_SUDO="/etc/pam.d/sudo"
Jakub Jelen 1ce235
PAM_SSHD="/etc/pam.d/sshd"
Jakub Jelen 1ce235
PAM_MODULE="pam_save_ssh_var"
Jakub Jelen 1ce235
SUDOERS_CFG="/etc/sudoers.d/01_pam_ssh_auth"
Jakub Jelen 1ce235
SSHD_CFG="/etc/ssh/sshd_config"
Jakub Jelen 1ce235
USER="testuser$RANDOM"
Jakub Jelen 1ce235
PASS="testpassxy4re.3298fhdsaf"
Jakub Jelen 1ce235
AUTH_KEYS="/etc/security/authorized_keys"
Jakub Jelen 1ce235
AK_COMMAND_BIN="/root/ak.sh"
Jakub Jelen 1ce235
AK_COMMAND_KEYS="/root/akeys"
Jakub Jelen 1ce235
declare -a KEYS=("rsa" "ecdsa")
Jakub Jelen 1ce235
Jakub Jelen 1ce235
rlJournalStart
Jakub Jelen 1ce235
    rlPhaseStartSetup
Jakub Jelen 1ce235
        rlAssertRpm $PACKAGE
Jakub Jelen 1ce235
        rlAssertRpm pam_ssh_agent_auth
Jakub Jelen 1ce235
        rlImport distribution/fips
Jakub Jelen 1ce235
        rlServiceStart sshd
Jakub Jelen 1ce235
        rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory"
Jakub Jelen 1ce235
        rlRun "cp ${PAM_MODULE}.c $TmpDir/"
Jakub Jelen 1ce235
        rlRun "pushd $TmpDir"
Jakub Jelen 1ce235
        rlFileBackup --clean $PAM_SUDO /etc/sudoers /etc/sudoers.d/ /etc/security/ $AUTH_KEYS
Jakub Jelen 1ce235
        rlRun "sed -i '1 a\
Jakub Jelen 1ce235
auth       sufficient   pam_ssh_agent_auth.so file=$AUTH_KEYS' $PAM_SUDO"
Jakub Jelen 1ce235
        rlRun "echo 'Defaults    env_keep += \"SSH_AUTH_SOCK\"' > $SUDOERS_CFG"
Jakub Jelen 1ce235
        rlRun "echo 'Defaults    !requiretty' >> $SUDOERS_CFG"
Jakub Jelen 1ce235
        grep '^%wheel' /etc/sudoers || \
Jakub Jelen 1ce235
           rlRun "echo '%wheel        ALL=(ALL)       ALL' >> $SUDOERS_CFG"
Jakub Jelen 1ce235
        rlRun "useradd $USER -G wheel"
Jakub Jelen 1ce235
        rlRun "echo $PASS |passwd --stdin $USER"
Jakub Jelen 1ce235
    rlPhaseEnd
Jakub Jelen 1ce235
Jakub Jelen 1ce235
    if ! fipsIsEnabled; then
Jakub Jelen 1ce235
        KEYS+=("dsa")
Jakub Jelen 1ce235
    fi
Jakub Jelen 1ce235
Jakub Jelen 1ce235
    for KEY in "${KEYS[@]}"; do
Jakub Jelen 1ce235
        rlPhaseStartTest "Test with key type $KEY"
Jakub Jelen 1ce235
            rlRun "su $USER -c 'ssh-keygen -t $KEY -f ~/.ssh/my_id_$KEY -N \"\"'" 0
Jakub Jelen 1ce235
Jakub Jelen 1ce235
            # Without authorized_keys, the authentication should fail
Jakub Jelen 1ce235
            rlRun -s "su $USER -c 'eval \`ssh-agent\`; sudo id; ssh-agent -k'" 0
Jakub Jelen 1ce235
            rlAssertNotGrep "uid=0(root) gid=0(root)" $rlRun_LOG
Jakub Jelen 1ce235
Jakub Jelen 1ce235
            # Append the keys only to make sure we can match also the non-first line
Jakub Jelen 1ce235
            rlRun "cat ~$USER/.ssh/my_id_${KEY}.pub >> $AUTH_KEYS"
Jakub Jelen 1ce235
            rlRun -s "su $USER -c 'eval \`ssh-agent\`; ssh-add ~/.ssh/my_id_$KEY; sudo id; ssh-agent -k'"
Jakub Jelen 1ce235
            rlAssertGrep "uid=0(root) gid=0(root)" $rlRun_LOG
Jakub Jelen 1ce235
        rlPhaseEnd
Jakub Jelen 1ce235
    done
Jakub Jelen 1ce235
Jakub Jelen 1ce235
    if rlIsRHEL '<6.8' || ( rlIsRHEL '<7.3' && rlIsRHEL 7 ) ; then
Jakub Jelen 1ce235
        : # not available
Jakub Jelen 1ce235
    else
Jakub Jelen 1ce235
        rlPhaseStartSetup "Setup for authorized_keys_command"
Jakub Jelen 1ce235
            rlFileBackup --namespace ak_command $PAM_SUDO
Jakub Jelen 1ce235
            rlRun "rm -f $AUTH_KEYS"
Jakub Jelen 1ce235
            cat >$AK_COMMAND_BIN <<_EOF
Jakub Jelen 1ce235
#!/bin/bash
Jakub Jelen 1ce235
cat $AK_COMMAND_KEYS
Jakub Jelen 1ce235
_EOF
Jakub Jelen 1ce235
            rlRun "chmod +x $AK_COMMAND_BIN"
Jakub Jelen 1ce235
            rlRun "sed -i 's|.*pam_ssh_agent_auth.*|auth sufficient pam_ssh_agent_auth.so authorized_keys_command=$AK_COMMAND_BIN authorized_keys_command_user=root|' $PAM_SUDO"
Jakub Jelen 1ce235
            rlRun "cat $PAM_SUDO"
Jakub Jelen 1ce235
        rlPhaseEnd
Jakub Jelen 1ce235
Jakub Jelen 1ce235
        for KEY in "${KEYS[@]}"; do
Jakub Jelen 1ce235
            rlPhaseStartTest "Test authorized_keys_command with key type $KEY (bz1299555, bz1317858)"
Jakub Jelen 1ce235
                rlRun "cat ~$USER/.ssh/my_id_${KEY}.pub >$AK_COMMAND_KEYS"
Jakub Jelen 1ce235
                rlRun -s "su $USER -c 'eval \`ssh-agent\`; ssh-add ~/.ssh/my_id_$KEY; sudo id; ssh-agent -k'"
Jakub Jelen 1ce235
                rlAssertGrep "uid=0(root) gid=0(root)" $rlRun_LOG
Jakub Jelen 1ce235
            rlPhaseEnd
Jakub Jelen 1ce235
        done
Jakub Jelen 1ce235
Jakub Jelen 1ce235
        rlPhaseStartCleanup "Cleanup for authorized_keys_command"
Jakub Jelen 1ce235
            rlFileRestore --namespace ak_command
Jakub Jelen 1ce235
            rlRun "rm -f $AK_COMMAND_BIN $AK_COMMAND_KEYS"
Jakub Jelen 1ce235
        rlPhaseEnd
Jakub Jelen 1ce235
    fi
Jakub Jelen 1ce235
Jakub Jelen 1ce235
    if rlIsRHEL '>=7.3'; then # not in Fedora anymore
Jakub Jelen 1ce235
        rlPhaseStartTest "bz1312304 - Exposing information about succesful auth"
Jakub Jelen 1ce235
            rlRun "rlFileBackup --namespace exposing $PAM_SSHD"
Jakub Jelen 1ce235
            rlRun "rlFileBackup --namespace exposing $SSHD_CFG"
Jakub Jelen 1ce235
            rlRun "rlFileBackup --namespace exposing /root/.ssh/"
Jakub Jelen 1ce235
            rlRun "rm -f ~/.ssh/id_rsa*"
Jakub Jelen 1ce235
            rlRun "ssh-keygen -f ~/.ssh/id_rsa -N \"\"" 0
Jakub Jelen 1ce235
            rlRun "ssh-keyscan localhost >~/.ssh/known_hosts" 0
Jakub Jelen 1ce235
            USER_AK_FILE=~$USER/.ssh/authorized_keys
Jakub Jelen 1ce235
            rlRun "cat ~/.ssh/id_rsa.pub >$USER_AK_FILE"
Jakub Jelen 1ce235
            rlRun "chown $USER:$USER $USER_AK_FILE"
Jakub Jelen 1ce235
            rlRun "chmod 0600 $USER_AK_FILE"
Jakub Jelen 1ce235
            rlRun "gcc -fPIC -DPIC -shared -rdynamic -o $PAM_MODULE.o $PAM_MODULE.c"
Jakub Jelen 1ce235
            rlRun "test -d /lib64/security && cp $PAM_MODULE.o /lib64/security/" 0,1
Jakub Jelen 1ce235
            rlRun "test -d /lib/security && cp $PAM_MODULE.o /lib/security/" 0,1
Jakub Jelen 1ce235
            rlRun "sed -i '1 i auth       optional         $PAM_MODULE.o' $PAM_SSHD"
Jakub Jelen 1ce235
Jakub Jelen 1ce235
            # pam-and-env should expose information to both PAM and environmental variable;
Jakub Jelen 1ce235
            # we will be testing only env variable here for the time being,
Jakub Jelen 1ce235
            rlRun "echo 'ExposeAuthenticationMethods pam-and-env' >>$SSHD_CFG"
Jakub Jelen 1ce235
            rlRun "sed -i '/^ChallengeResponseAuthentication/ d' $SSHD_CFG"
Jakub Jelen 1ce235
            rlRun "service sshd restart"
Jakub Jelen 1ce235
            rlWaitForSocket 22 -t 5
Jakub Jelen 1ce235
            rlRun -s "ssh -i ~/.ssh/id_rsa $USER@localhost \"env|grep SSH_USER_AUTH\"" 0 \
Jakub Jelen 1ce235
                "Environment variable SSH_USER_AUTH is set"
Jakub Jelen 1ce235
            rlAssertGrep "^SSH_USER_AUTH=publickey:" $rlRun_LOG
Jakub Jelen 1ce235
            rlRun "rm -f $rlRun_LOG"
Jakub Jelen 1ce235
Jakub Jelen 1ce235
            # pam-only should expose information only to PAM and not to environment variable
Jakub Jelen 1ce235
            rlRun "sed -i 's/pam-and-env/pam-only/' $SSHD_CFG"
Jakub Jelen 1ce235
            rlRun "echo 'AuthenticationMethods publickey,keyboard-interactive:pam' >>$SSHD_CFG"
Jakub Jelen 1ce235
            rlRun "service sshd restart"
Jakub Jelen 1ce235
            rlWaitForSocket 22 -t 5
Jakub Jelen 1ce235
ssh_with_pass() {
Jakub Jelen 1ce235
    ssh_args=("-i /root/.ssh/id_rsa")
Jakub Jelen 1ce235
    ssh_args+=("$USER@localhost")
Jakub Jelen 1ce235
    cat >ssh.exp <<_EOF
Jakub Jelen 1ce235
#!/usr/bin/expect -f
Jakub Jelen 1ce235
Jakub Jelen 1ce235
set timeout 5
Jakub Jelen 1ce235
spawn ssh ${ssh_args[*]} "echo CONNECTED; env|grep SSH_USER_AUTH"
Jakub Jelen 1ce235
expect {
Jakub Jelen 1ce235
    -re {.*[Pp]assword.*} { send -- "$PASS\r"; exp_continue }
Jakub Jelen 1ce235
    timeout { exit 1 }
Jakub Jelen 1ce235
    eof { exit 0 }
Jakub Jelen 1ce235
}
Jakub Jelen 1ce235
_EOF
Jakub Jelen 1ce235
    rlRun -s "expect -f ssh.exp"
Jakub Jelen 1ce235
}
Jakub Jelen 1ce235
            #rlRun -s "ssh ${ssh_args[*]} \"echo CONNECTED; env|grep SSH_USER_AUTH\"" 1 \
Jakub Jelen 1ce235
                #"Environment variable SSH_USER_AUTH is NOT set"
Jakub Jelen 1ce235
            rlRun "ssh_with_pass"
Jakub Jelen 1ce235
            rlRun "grep -q CONNECTED $rlRun_LOG" 0 "Connection was successful"
Jakub Jelen 1ce235
            rlAssertGrep "^SSH_USER_AUTH: 'publickey:" /tmp/SSH_USER_AUTH
Jakub Jelen 1ce235
            rlRun "cat /tmp/SSH_USER_AUTH"
Jakub Jelen 1ce235
            rlRun "rm -f $rlRun_LOG /tmp/SSH_USER_AUTH"
Jakub Jelen 1ce235
            for pm in /lib64/security/$PAM_MODULE.o /lib/security/$PAM_MODULE.o; do
Jakub Jelen 1ce235
                rlRun "test -e $pm && rm -f $pm" 0,1
Jakub Jelen 1ce235
            done
Jakub Jelen 1ce235
            rlRun "rlFileRestore --namespace exposing"
Jakub Jelen 1ce235
        rlPhaseEnd
Jakub Jelen 1ce235
    fi
Jakub Jelen 1ce235
Jakub Jelen 1ce235
    rlPhaseStartCleanup
Jakub Jelen 1ce235
        rlRun "popd"
Jakub Jelen 1ce235
        rlRun "rm -r $TmpDir" 0 "Removing tmp directory"
Jakub Jelen 315357
        rlRun "userdel -fr $USER"
Jakub Jelen 1ce235
        rlFileRestore
Jakub Jelen 1ce235
        rlServiceRestore sshd
Jakub Jelen 1ce235
    rlPhaseEnd
Jakub Jelen 1ce235
rlJournalPrintText
Jakub Jelen 1ce235
rlJournalEnd