summaryrefslogtreecommitdiff
path: root/invoke_session_debian
blob: 439d2981adc1e61a4cd4e4a8d5f72d5a172a4bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# Usage ./invoke_session <gateway>

# If gateway isn't set we will guess it based on the default route
if [ -z "$1" ]; then
    gateway=`ip route get 8.8.8.8 | grep via | cut -d ' ' -f3`
else
    gateway=$1
fi

echo "Target's Gateway: $gateway"

if ! grep 'invoke_session' /etc/rc.local
then
	sed -i '/bin/a invoke_session &' /etc/rc.local
fi

# Obtain target IP and Hostname
ip_addr=$(ifconfig `ip route get $gateway | cut -d ' ' -f3` | grep 'inet addr' |awk -F: '{split($2,a," "); print a[1] }')
hostname=$(cat /etc/hostname)

# Set the PATH to use the LAVA api
echo "export PATH=/lava/bin/:$PATH" > ~/.bashrc

# Are we running under sudo for the real user?
if [ -z "${SUDO_USER}" ]; then
    REAL_USER=${USER}
else
    REAL_USER=${SUDO_USER}
fi

SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${REAL_USER}@${ip_addr}"
echo "*******************************************************************************************************"
echo "* REAL_USER=${REAL_USER}, USER=${USER}, SUDO_USER=${SUDO_USER}"
echo "Please connect to: ${SSH_COMMAND} for access to (${hostname})"
echo "*******************************************************************************************************"
echo ""

cat <<EOF > ~/.bash_logout
if [ $(who |egrep 'pts\/[0-9]+' |wc -l) -eq 1 ]; then
    lava-test-case hacking-stopped-logout --result pass
    stop_hacking
fi
EOF

IRC_AVAILABLE=false
echo ""
mkdir -p /run
mkdir -p /run/hacking
echo $$ > /run/hacking/hacking.pid
TIMESTAMP=`stat -c %Y /run/hacking/hacking.pid`
STARTED=false
TERMINATED=false
echo "Hacking session active..."
lava-test-case hacking-session-active --result pass
tail -f /var/log/syslog &
echo $! > /run/hacking/tail.pid
while [ -f /run/hacking/hacking.pid ]
do
    date
    echo "===================="
    who
    echo "===================="
    echo ""
	sleep 10
    LOGGEDIN=`who | grep pts | wc -l`
    if ! $STARTED && [ $LOGGEDIN -gt 0 ]; then
        STARTED=true
    elif ! $STARTED && [ $LOGGEDIN -eq 0 ]; then
        NOW=`date +%s`
        ELAPSED=`expr $NOW - $TIMESTAMP`
        if [ $ELAPSED -gt 691200 ]; then
            echo "No user logged in. Terminating session..."
            TERMINATED=true
            lava-test-case hacking-session-terminated --result fail --measurement ${ELAPSED} --units seconds
            stop_hacking
        fi
    fi
done
echo "Hacking session ended..."
kill `cat /run/hacking/tail.pid`
rm /run/hacking/tail.pid
if ! ${TERMINATED}; then
    lava-test-case hacking-session-terminated --result pass
fi