Skip to content

Commit 5536a5f

Browse files
committed
feat: agregar soporte para configuración dinámica de SSH_PORT y PasswordAuthentication
1 parent e4b6cac commit 5536a5f

File tree

8 files changed

+138
-132
lines changed

8 files changed

+138
-132
lines changed

docker-compose.dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
environment:
1111
- LGSM_PASSWORD=${LGSM_PASSWORD}
1212
- SSH_KEY=${SSH_KEY}
13+
- SSH_PORT=${SSH_PORT}
1314
- KF2_GAME_PORT=${KF2_GAME_PORT:-7777}
1415
- KF2_QUERY_PORT=${KF2_QUERY_PORT:-27015}
1516
- KF2_WEBADMIN=${KF2_WEBADMIN:-false}

docker-scripts/post-install-config.sh

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ create_deferred_config_script() {
1717
echo "🔍 Buscando scripts de configuración en ${server_scripts_dir}"
1818

1919
# Crear script que se ejecutará como usuario linuxgsm después del inicio
20-
cat > "/data/run-server-config.sh" << 'EOF'
20+
cat > "$HOME/run-server-config.sh" << 'EOF'
2121
#!/bin/bash
2222
# Script de configuración diferida - Se ejecuta como usuario linuxgsm
2323
@@ -28,6 +28,7 @@ echo "Fecha: $(date)"
2828
# Esperar a que el sistema esté completamente iniciado
2929
sleep 10
3030
31+
3132
# Ejecutar scripts de configuración del servidor
3233
SERVER_SCRIPTS_DIR="/app/server-scripts"
3334
if [ -d "${SERVER_SCRIPTS_DIR}" ] && ls "${SERVER_SCRIPTS_DIR}"/*.sh 1> /dev/null 2>&1; then
@@ -36,14 +37,12 @@ if [ -d "${SERVER_SCRIPTS_DIR}" ] && ls "${SERVER_SCRIPTS_DIR}"/*.sh 1> /dev/nul
3637
echo ""
3738
echo "📝 Ejecutando: ${script_name}"
3839
echo "----------------------------------------"
39-
4040
# Pasar variables de entorno
4141
export KF2_GAME_PORT="${KF2_GAME_PORT}"
4242
export KF2_QUERY_PORT="${KF2_QUERY_PORT}"
4343
export KF2_WEBADMIN_PORT="${KF2_WEBADMIN_PORT}"
4444
export KF2_STEAM_PORT="${KF2_STEAM_PORT}"
4545
export KF2_NTP_PORT="${KF2_NTP_PORT}"
46-
4746
# Ejecutar el script
4847
bash "${script}"
4948
echo "✅ ${script_name} completado"
@@ -55,26 +54,26 @@ fi
5554
echo "🎯 Configuración post-instalación completada"
5655
5756
# Auto-eliminar este script después de la ejecución
58-
rm -f "/data/run-server-config.sh"
57+
rm -f "$HOME/run-server-config.sh"
5958
EOF
6059

6160
# Hacer el script ejecutable
62-
chmod +x "/data/run-server-config.sh"
63-
chown linuxgsm:linuxgsm "/data/run-server-config.sh"
61+
chmod +x "$HOME/run-server-config.sh"
62+
chown linuxgsm:linuxgsm "$HOME/run-server-config.sh"
6463

65-
echo "✅ Script de configuración diferida creado: /data/run-server-config.sh"
64+
echo "✅ Script de configuración diferida creado: $HOME/run-server-config.sh"
6665

6766
# Programar la ejecución del script en segundo plano
68-
cat > "/data/start-config.sh" << 'EOF'
67+
cat > "$HOME/start-config.sh" << 'EOF'
6968
#!/bin/bash
7069
# Ejecutar configuración en segundo plano después del inicio del usuario
71-
sleep 5 && /data/run-server-config.sh > /data/config-log.txt 2>&1 &
70+
sleep 5 && $HOME/run-server-config.sh > $HOME/config-log.txt 2>&1 &
7271
EOF
7372

74-
chmod +x "/data/start-config.sh"
75-
chown linuxgsm:linuxgsm "/data/start-config.sh"
73+
chmod +x "$HOME/start-config.sh"
74+
chown linuxgsm:linuxgsm "$HOME/start-config.sh"
7675

77-
echo "✅ Script de inicio diferido creado: /data/start-config.sh"
76+
echo "✅ Script de inicio diferido creado: $HOME/start-config.sh"
7877
}
7978

8079
# Función principal

docker-scripts/setup-config-trigger.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

docker-scripts/ssh.sh

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,78 @@
11
#!/bin/bash
22
# Script: ssh.sh
33
# Description: Configures the SSH service in the container, generates host keys (RSA, ECDSA, and ED25519),
4-
# and updates the SSH configuration to use a persistent directory (/data/ssh). It also enables or disables
4+
# and updates the SSH configuration to use a persistent directory ($HOME/ssh). It also enables or disables
55
# password authentication based on the LGSM_PASSWORD variable.
66

7-
if [ ! -d /data/ssh ]; then
8-
mkdir -p /data/ssh
7+
SSH_PERSIST_DIR="$HOME/ssh"
8+
if [ ! -d "$SSH_PERSIST_DIR" ]; then
9+
mkdir -p "$SSH_PERSIST_DIR"
910
fi
1011

11-
if [ ! -f /data/ssh/ssh_host_rsa_key ]; then
12-
ssh-keygen -t rsa -b 4096 -f /data/ssh/ssh_host_rsa_key -N ''
12+
if [ ! -f "$SSH_PERSIST_DIR/ssh_host_rsa_key" ]; then
13+
ssh-keygen -t rsa -b 4096 -f "$SSH_PERSIST_DIR/ssh_host_rsa_key" -N ''
1314
fi
1415

15-
if [ ! -f /data/ssh/ssh_host_ecdsa_key ]; then
16-
ssh-keygen -t ecdsa -f /data/ssh/ssh_host_ecdsa_key -N ''
16+
if [ ! -f "$SSH_PERSIST_DIR/ssh_host_ecdsa_key" ]; then
17+
ssh-keygen -t ecdsa -f "$SSH_PERSIST_DIR/ssh_host_ecdsa_key" -N ''
1718
fi
1819

19-
if [ ! -f /data/ssh/ssh_host_ed25519_key ]; then
20-
ssh-keygen -t ed25519 -f /data/ssh/ssh_host_ed25519_key -N ''
20+
if [ ! -f "$SSH_PERSIST_DIR/ssh_host_ed25519_key" ]; then
21+
ssh-keygen -t ed25519 -f "$SSH_PERSIST_DIR/ssh_host_ed25519_key" -N ''
2122
fi
2223

23-
sed -i 's|#HostKey /etc/ssh/ssh_host_rsa_key|HostKey /data/ssh/ssh_host_rsa_key|' /etc/ssh/sshd_config
24-
sed -i 's|#HostKey /etc/ssh/ssh_host_ecdsa_key|HostKey /data/ssh/ssh_host_ecdsa_key|' /etc/ssh/sshd_config
25-
sed -i 's|#HostKey /etc/ssh/ssh_host_ed25519_key|HostKey /data/ssh/ssh_host_ed25519_key|' /etc/ssh/sshd_config
24+
sed -i "s|#HostKey /etc/ssh/ssh_host_rsa_key|HostKey $clear/ssh_host_rsa_key|" /etc/ssh/sshd_config
25+
sed -i "s|#HostKey /etc/ssh/ssh_host_ecdsa_key|HostKey $SSH_PERSIST_DIR/ssh_host_ecdsa_key|" /etc/ssh/sshd_config
26+
sed -i "s|#HostKey /etc/ssh/ssh_host_ed25519_key|HostKey $SSH_PERSIST_DIR/ssh_host_ed25519_key|" /etc/ssh/sshd_config
2627

27-
# Configure password authentication based on the LGSM_PASSWORD variable:
28-
# If LGSM_PASSWORD has a value, enable PasswordAuthentication.
29-
# If it is empty, disable it.
30-
if [ -n "${LGSM_PASSWORD}" ]; then
31-
sed -i 's|#PasswordAuthentication yes|PasswordAuthentication yes|' /etc/ssh/sshd_config
28+
29+
# --- SOPORTE CAMBIO DINÁMICO DE PasswordAuthentication Y PUERTO SSH ---
30+
ssh_config_changed=0
31+
32+
# PasswordAuthentication
33+
desired_auth="no"
34+
[ -n "${LGSM_PASSWORD}" ] && desired_auth="yes"
35+
current_auth=$(grep -E '^[# ]*PasswordAuthentication[ ]+(yes|no)' /etc/ssh/sshd_config | tail -1 | awk '{print $2}')
36+
if [ -n "$current_auth" ]; then
37+
if [ "$current_auth" != "$desired_auth" ]; then
38+
sed -i "/^[# ]*PasswordAuthentication[ ]\+/c\PasswordAuthentication ${desired_auth}" /etc/ssh/sshd_config
39+
echo "[ssh.sh] PasswordAuthentication cambiado: $current_auth$desired_auth"
40+
ssh_config_changed=1
41+
else
42+
echo "[ssh.sh] PasswordAuthentication ya configurado en $desired_auth, sin cambios."
43+
fi
3244
else
33-
sed -i 's|#PasswordAuthentication yes|PasswordAuthentication no|' /etc/ssh/sshd_config
45+
echo "PasswordAuthentication ${desired_auth}" >> /etc/ssh/sshd_config
46+
echo "[ssh.sh] PasswordAuthentication agregado: $desired_auth"
47+
ssh_config_changed=1
3448
fi
3549

50+
# Port
3651
if [ -n "${SSH_PORT}" ]; then
37-
sed -i "s|#Port 22|Port ${SSH_PORT}|" /etc/ssh/sshd_config
52+
current_port=$(grep -E '^[# ]*Port[ ]+[0-9]+' /etc/ssh/sshd_config | tail -1 | awk '{print $2}')
53+
if [ -n "$current_port" ]; then
54+
if [ "$current_port" != "$SSH_PORT" ]; then
55+
sed -i "/^[# ]*Port[ ]\+/c\Port ${SSH_PORT}" /etc/ssh/sshd_config
56+
echo "[ssh.sh] Puerto SSH cambiado: $current_port$SSH_PORT"
57+
ssh_config_changed=1
58+
else
59+
echo "[ssh.sh] Puerto SSH ya configurado en $SSH_PORT, sin cambios."
60+
fi
61+
else
62+
echo "Port ${SSH_PORT}" >> /etc/ssh/sshd_config
63+
echo "[ssh.sh] Puerto SSH agregado: $SSH_PORT"
64+
ssh_config_changed=1
65+
fi
66+
else
67+
echo "[ssh.sh] SSH_PORT no definido, usando configuración por defecto."
3868
fi
3969

40-
# Start the SSH service
41-
service ssh start
70+
# Recargar/reiniciar solo si hubo cambios
71+
if [ "$ssh_config_changed" = "1" ]; then
72+
service ssh reload || service ssh restart
73+
fi
74+
75+
# Start the SSH service si no está corriendo
76+
if ! pgrep -x "sshd" > /dev/null; then
77+
service ssh start
78+
fi

entrypoint.sh

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export LGSM_SERVERFILES=${LGSM_SERVERFILES}
5151
export LGSM_DATADIR=${LGSM_DATADIR}
5252
export LGSM_CONFIG=${LGSM_CONFIG}
5353

54-
# Export KF2 configuration variables
54+
export SSH_PORT=${SSH_PORT}
55+
5556
export KF2_GAME_PORT=${KF2_GAME_PORT}
5657
export KF2_QUERY_PORT=${KF2_QUERY_PORT}
5758
export KF2_WEBADMIN_PORT=${KF2_WEBADMIN_PORT}
@@ -90,48 +91,6 @@ else
9091
echo -e "No .sh files found in /app/docker-scripts"
9192
fi
9293

93-
echo -e ""
94-
echo -e "Config Profile"
95-
echo -e "================================="
96-
if [ ! -f $HOME/.bashrc ]; then
97-
echo -e "Creating $HOME/.bashrc"
98-
cp /etc/skel/.bashrc $HOME/.bashrc
99-
echo -e "Setting ownership for $HOME/.bashrc"
100-
chown "${USER}":"${USER}" $HOME/.bashrc
101-
else
102-
echo -e "$HOME/.bashrc already exists"
103-
fi
104-
105-
if [ ! -d $HOME/.ssh ]; then
106-
echo -e "Creating $HOME/.ssh"
107-
mkdir -p $HOME/.ssh
108-
echo -e "Setting ownership and permissions for $HOME/.ssh"
109-
chown "${USER}":"${USER}" $HOME/.ssh
110-
chmod 700 $HOME/.ssh
111-
else
112-
echo -e "$HOME/.ssh already exists"
113-
fi
114-
115-
if [ ! -f $HOME/.ssh/authorized_keys ]; then
116-
echo -e "Creating authorized_keys"
117-
touch $HOME/.ssh/authorized_keys
118-
119-
if [ -n "${SSH_KEY}" ]; then
120-
IFS=',' read -ra KEYS <<< "${SSH_KEY}"
121-
for key in "${KEYS[@]}"; do
122-
echo -e "${key}" >> $HOME/.ssh/authorized_keys
123-
done
124-
else
125-
echo -e "SSH_KEY is empty, skipping..."
126-
fi
127-
128-
echo -e "Setting ownership and permissions for $HOME/.ssh/authorized_keys"
129-
chown "${USER}":"${USER}" $HOME/.ssh/authorized_keys
130-
chmod 600 $HOME/.ssh/authorized_keys
131-
else
132-
echo -e "authorized_keys already exists"
133-
fi
134-
13594
echo -e ""
13695
echo -e "Switch to user ${USER}"
13796
echo -e "================================="

server-scripts/configure-kf2-ports.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
echo "=== Configurando puertos personalizados KF2 ==="
77

88
# Rutas de archivos de configuración
9-
KF2_CONFIG_FILE="/data/serverfiles/KFGame/Config/kf2server/LinuxServer-KFEngine.ini"
10-
LGSM_CONFIG_FILE="/data/config-lgsm/kf2server/kf2server.cfg"
11-
WEBADMIN_CONFIG_FILE="/data/serverfiles/KFGame/Config/kf2server/KFWeb.ini"
9+
KF2_CONFIG_FILE="$HOME/serverfiles/KFGame/Config/kf2server/LinuxServer-KFEngine.ini"
10+
LGSM_CONFIG_FILE="$HOME/config-lgsm/kf2server/kf2server.cfg"
11+
WEBADMIN_CONFIG_FILE="$HOME/serverfiles/KFGame/Config/kf2server/KFWeb.ini"
1212

1313
# Función para verificar archivos de configuración
1414
check_config_files() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
# Script: manage-authorized-keys.sh
3+
# Descripción: Gestiona el archivo authorized_keys del usuario linuxgsm según la variable SSH_KEY
4+
5+
set -e
6+
7+
8+
SSH_DIR="$HOME/.ssh"
9+
AUTH_KEYS="$SSH_DIR/authorized_keys"
10+
11+
# Crear el directorio si no existe (migrado desde entrypoint.sh)
12+
if [ ! -d "$SSH_DIR" ]; then
13+
echo "[manage-authorized-keys.sh] Creando $SSH_DIR"
14+
mkdir -p "$SSH_DIR"
15+
echo "[manage-authorized-keys.sh] Asignando permisos y propietario a $SSH_DIR"
16+
chown linuxgsm:linuxgsm "$SSH_DIR"
17+
chmod 700 "$SSH_DIR"
18+
else
19+
echo "[manage-authorized-keys.sh] $SSH_DIR ya existe"
20+
fi
21+
22+
# Si SSH_KEY está vacío, eliminar authorized_keys
23+
if [ -z "$SSH_KEY" ]; then
24+
echo "[manage-authorized-keys.sh] SSH_KEY vacío. Eliminando $AUTH_KEYS si existe."
25+
rm -f "$AUTH_KEYS"
26+
exit 0
27+
fi
28+
29+
# Procesar claves separadas por coma
30+
IFS=',' read -ra KEYS <<< "$SSH_KEY"
31+
unset SSH_KEY # Elimina la variable tan pronto como se procesa
32+
echo "[manage-authorized-keys.sh] Escribiendo claves en $AUTH_KEYS:"
33+
> "$AUTH_KEYS"
34+
for key in "${KEYS[@]}"; do
35+
key_trimmed="$(echo -e "$key" | xargs)"
36+
if [ -n "$key_trimmed" ]; then
37+
echo "$key_trimmed" >> "$AUTH_KEYS"
38+
# Mostrar solo tipo y fingerprint parcial, no la clave completa
39+
tipo=$(echo "$key_trimmed" | awk '{print $1}')
40+
fingerprint=$(echo "$key_trimmed" | awk '{print $2}' | cut -c1-8)
41+
echo " - $tipo $fingerprint..."
42+
fi
43+
done
44+
unset KEYS # Elimina el array de claves
45+
46+
chown linuxgsm:linuxgsm "$AUTH_KEYS"
47+
chmod 600 "$AUTH_KEYS"
48+
echo "[manage-authorized-keys.sh] Operación completada."

server-scripts/manage-bashrc.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# Script: manage-bashrc.sh
3+
# Descripción: Gestiona la existencia y permisos de .bashrc para el usuario linuxgsm
4+
5+
6+
BASHRC="$HOME/.bashrc"
7+
8+
if [ ! -f "$BASHRC" ]; then
9+
echo "[manage-bashrc.sh] Creando $BASHRC"
10+
cp /etc/skel/.bashrc "$BASHRC"
11+
echo "[manage-bashrc.sh] Asignando propietario a $BASHRC"
12+
chown linuxgsm:linuxgsm "$BASHRC"
13+
else
14+
echo "[manage-bashrc.sh] $BASHRC ya existe"
15+
fi

0 commit comments

Comments
 (0)