File size: 3,023 Bytes
19a162c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
#!/bin/bash

# Print the current hostname and user details
echo "* The hostname of this container is: $(cat /etc/hostname)"
echo "* The Current User of this container is: $(whoami)"
#echo "* The host of this container is: $(cat /etc/hosts)"
echo "* ID of the user running the script: $(id -u) * Group: $(id -g) * Status of Admin: $(id admin 2>/dev/null || echo 'Admin user not found')"

# Switch to admin user and run a command
echo "password" | su - admin -c "bash -i"
# Commands to run as admin user
echo "* Now running as: $(whoami)"

# Ensure SSH host keys are present, generate if missing
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
    echo "* Generating SSH host keys *"
    ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ""
    ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N ""
    ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
fi

# Start SSH service if not running
if ! pgrep -x "sshd" >/dev/null; then
    echo "* Starting SSH server on port 2222 *"
    /usr/sbin/sshd -p 2222
else
    echo "* SSH server is already running *"
fi

# Check if 'admin' user exists, create if missing
if ! id -u admin >/dev/null 2>&1; then
    echo "* Creating 'ubuntu' user *"
    useradd -m -s /bin/bash -G sudo admin
    echo "admin:password" | chpasswd
    #echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
fi

# Ensure 'admin' home directory permissions
echo "* Fixing permissions for 'ubuntu' user *"
chown -R admin:admin /home/admin
chmod -R 777 /home/admin

# Generate SSH keys for the 'admin' user if missing
if [ ! -f /home/admin/.ssh/id_rsa ]; then
    echo "* Generating SSH keys for 'admin' user *"
    mkdir -p /home/admin/.ssh
    ssh-keygen -t rsa -b 2048 -f /app/users/ssh/id_rsa_admin -q -N ""
    ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N ""
    chown -R admin:admin /home/admin/.ssh
    chmod 700 /home/admin/.ssh
    chmod 600 /home/admin/.ssh/id_rsa
    chmod 644 /home/admin/.ssh/id_rsa.pub
fi

# Add public key to authorized_keys for 'admin' user
if [ ! -f /home/admin/.ssh/authorized_keys ]; then
    echo "* Adding public key to authorized_keys for 'admin' user *"
    cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys
    chmod 777 /home/admin/.ssh/authorized_keys
    chown admin:admin /home/admin/.ssh/authorized_keys
fi


echo "* Contents of id_rsa:"
cat /app/users/ssh/id_rsa_admin

echo "* Contents of id_rsa of Admin:"
cat /home/admin/.ssh/id_rsa

# Print SSH server status
echo "* Status of SSH service:"
netstat -tuln 

# Activate virtual environment
if [ -d "/app/WebSSHEnv" ]; then
    echo "* Activating virtual environment *"
    source /app/WebSSHEnv/bin/activate
else
    echo "* Virtual environment not found, please check setup *"
fi

# Set working directory
cd /app || exit

# Print the contents of the working directory
echo "* Contents of /app directory: *"
ls -la /app

# Run the WebSSH application
echo "* Starting WebSSH application *"
python3 -u -m WebSSH

# Keep the container running
tail -f /dev/null