From e26f238f2601c91178d9edfad1d9d625cd627d4a Mon Sep 17 00:00:00 2001 From: XargonWan Date: Thu, 9 Nov 2023 10:28:48 +0100 Subject: [PATCH] SFTP: adding SFTP Server --- functions/ftp_server.sh | 34 -------------------------- functions/prepare_emulator.sh | 15 ------------ functions/sftp_server.sh | 45 +++++++++++++++++++++++++++++++++++ net.retrodeck.retrodeck.yml | 26 +++++++------------- tools/configurator.sh | 12 +++++----- tools/ftp/virtual_users.txt | 1 - tools/ftp/vsftpd.conf | 17 ------------- 7 files changed, 60 insertions(+), 90 deletions(-) delete mode 100644 functions/ftp_server.sh create mode 100644 functions/sftp_server.sh delete mode 100644 tools/ftp/virtual_users.txt delete mode 100644 tools/ftp/vsftpd.conf diff --git a/functions/ftp_server.sh b/functions/ftp_server.sh deleted file mode 100644 index 0b672cbe..00000000 --- a/functions/ftp_server.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -port=21 -ip=$(hostname -I | awk '{print $1}') - -# Check if the port is in use -if nc -z localhost $port; then - zenity --error --no-wrap \ - --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \ - --title "RetroDECK - FTP Server" \ - --text="Port $port is already in use. Please stop any services on that port and try again." - exit 1 -fi - -# Start FTP server on port $port -nohup vsftpd /var/config/retrodeck/ftp/vsftpd.conf & - -# Get the PID of the FTP server process -ftp_pid=$! - -# Function to stop the FTP server -stop_ftp_server() { - kill -9 $ftp_pid - exit 0 -} - -# Create a Zenity window with only the "Stop" button -zenity --icon-name=net.retrodeck.retrodeck --info --no-wrap \ - --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \ - --title "RetroDECK - FTP Server" \ - --text="FTP server started.\n\nAddress: $ip\nport: $port\nID:\tretrodeck\nPassword:\tretrodeck\npointing to:\n$rdhome\n\nPress Stop to terminate the server." --ok-label="Stop" || stop_ftp_server - -# If the user clicks "Stop", call the function to stop the FTP server -stop_ftp_server diff --git a/functions/prepare_emulator.sh b/functions/prepare_emulator.sh index b51ca8f8..2eb1afa2 100644 --- a/functions/prepare_emulator.sh +++ b/functions/prepare_emulator.sh @@ -714,21 +714,6 @@ prepare_emulator() { fi - # TODO: this is not properly an emulator but let's treat like this for the moment - if [[ "$emulator" =~ ^(ftp|FTP|all)$ ]]; then - # TODO: do a proper script - # This is just a placeholder script to test the emulator's flow - echo "------------------------" - echo "Initializing FTP Server" - echo "------------------------" - - mkdir -p "/var/config/ftp" - cp "/app/retrodeck/ftp/*" "/var/config/retrodeck/ftp" - # TODO: this step is to be done properly: Replacing RETRODECKHOMEDIR placeholder - sed -i 's#RETRODECKHOMEDIR#'$rdhome'#g' "/var/config/retrodeck/ftp/vsftpd.conf" - - fi - # Update presets for all emulators after any reset or move if [[ ! "$emulator" == "retrodeck" ]]; then build_retrodeck_current_presets diff --git a/functions/sftp_server.sh b/functions/sftp_server.sh new file mode 100644 index 00000000..9abbe1eb --- /dev/null +++ b/functions/sftp_server.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +port=22 +ip=$(hostname -I | awk '{print $1}') + +# Check if the port is in use +if nc -z localhost $port; then + zenity --error --no-wrap \ + --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \ + --title "RetroDECK - SFTP Server" \ + --text="Port $port is already in use. Please stop any services on that port and try again." + exit 1 +fi + +# Create a temporary directory for SFTP chroot +mkdir -p /tmp/sftp_home/retrodeck +echo "retrodeck:$(openssl passwd -1 retrodeck)" >> /tmp/sftp_home/retrodeck/etc/passwd + +# Set rdhome as the home directory for retrodeck user +echo "Match User retrodeck\n ChrootDirectory $rdhome" >> /etc/ssh/sshd_config + +# Restart SSHD to apply the new configuration +service ssh restart + +# Start SSHD with SFTP support and specific user and password +nohup /usr/sbin/sshd -p $port -o PasswordAuthentication=yes -o PubkeyAuthentication=no -o AuthorizedKeysFile=/dev/null -o UsePAM=no -o AllowTcpForwarding=no -o PermitRootLogin=no -o ChrootDirectory=/tmp/sftp_home/retrodeck & + +# Get the PID of the SSH/SFTP server process +ssh_pid=$! + +# Function to stop the SSH/SFTP server +stop_ssh_server() { + kill -9 $ssh_pid + rm -rf /tmp/sftp_home + exit 0 +} + +# Create a Zenity window with only the "Stop" button +zenity --icon-name=net.retrodeck.retrodeck --info --no-wrap \ + --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \ + --title "RetroDECK - SFTP Server" \ + --text="SFTP server started.\n\nAddress: $ip\nport: $port\nID:\tretrodeck\nPassword:\tretrodeck\npointing to:\n$rdhome\n\nPress Stop to terminate the server." --ok-label="Stop" || stop_ssh_server + +# If the user clicks "Stop", call the function to stop the SSH/SFTP server +stop_ssh_server diff --git a/net.retrodeck.retrodeck.yml b/net.retrodeck.retrodeck.yml index 613e24fd..030e043c 100644 --- a/net.retrodeck.retrodeck.yml +++ b/net.retrodeck.retrodeck.yml @@ -284,33 +284,25 @@ modules: # dependency of: CEMU, RPCS3 - rd-submodules/shared-modules/glew/glew.json - # FTP Server - # TODO: address + # SFTP Server - - name: vsftpd + - name: sftp-hostname buildsystem: simple build-commands: - - ar -x vsftpd*.deb - - rm -f vsftpd*.deb + - ar -x hostname*.deb - tar -xf data.tar.zst - - rm -f control.tar.gz data.tar.zst debian-binary - - cp -r usr/* ${FLATPAK_DEST} - - rm -rf usr lib etc - - mkdir -p ${FLATPAK_DEST}/retrodeck/ftp + - cp -r bin/* ${FLATPAK_DEST}/bin - mkdir -p ${FLATPAK_DEST}/libexec - - cp -vf virtual_users.txt vsftpd.conf ${FLATPAK_DEST}/retrodeck/ftp - cp -vf ftp_server.sh ${FLATPAK_DEST}/libexec - chmod +x ${FLATPAK_DEST}/libexec/ftp_server.sh sources: - type: file - url: http://de.archive.ubuntu.com/ubuntu/pool/main/v/vsftpd/vsftpd_3.0.5-0ubuntu1_amd64.deb - sha256: 248582c4511ceaab5fd0d7f28d3a95af1a946a83bffe1a36d1accb72d99c2cf8 + url: http://de.archive.ubuntu.com/ubuntu/pool/main/h/hostname/hostname_3.23ubuntu2_amd64.deb + sha256: cec0448fef88a43a3c232fd9df555ca4c468cf5e16ddf3a1b0a5f7b7f076e413 - type: file - path: tools/ftp/virtual_users.txt - - type: file - path: tools/ftp/vsftpd.conf - - type: file - path: functions/ftp_server.sh + path: functions/sftp_server.sh + + # SFTP Server - END # ES-DE - START # https://gitlab.com/es-de/emulationstation-de diff --git a/tools/configurator.sh b/tools/configurator.sh index 0656a12e..9e82dded 100644 --- a/tools/configurator.sh +++ b/tools/configurator.sh @@ -61,7 +61,7 @@ source /app/libexec/global.sh # - Install: RetroDECK SD Controller Profile # - Install: PS3 firmware # - RetroDECK: Change Update Setting -# - Start FTP Server +# - Start an SFTP Server # - Troubleshooting # - Backup: RetroDECK Userdata # - Check & Verify: BIOS @@ -529,7 +529,7 @@ configurator_retrodeck_tools_dialog() { "Install: RetroDECK SD Controller Profile" "Install the custom RetroDECK controller layout for the Steam Deck" \ "Install: PS3 Firmware" "Download and install PS3 firmware for use with the RPCS3 emulator" \ "RetroDECK: Change Update Setting" "Enable or disable online checks for new versions of RetroDECK" \ - "Start FTP Server" "Start an FTP server to manage your retrodeck folder from another device" + "Start an SFTP Server" "Start an SFTP server to manage your retrodeck folder from another device" ) case $choice in @@ -574,8 +574,8 @@ configurator_retrodeck_tools_dialog() { configurator_online_update_setting_dialog ;; - "Start FTP Server" ) - source /app/libexec/ftp_server.sh + "Start an SFTP Server" ) + source /app/libexec/sftp_server.sh ;; "" ) # No selection made or Back button clicked @@ -989,7 +989,7 @@ configurator_reset_dialog() { --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" --width=1200 --height=720 \ --column="Choice" --column="Action" \ "Reset Specific Emulator" "Reset only one specific emulator to default settings" \ - "Reset All Emulators" "Reset all emulators and FTP server to their default settings" \ + "Reset All Emulators" "Reset all emulators to their default settings" \ "Reset RetroDECK" "Reset RetroDECK to default settings" ) case $choice in @@ -1044,7 +1044,7 @@ configurator_reset_dialog() { ;; "Reset All Emulators" ) - if [[ $(configurator_reset_confirmation_dialog "all emulators" "Are you sure you want to reset all emulators and FTP server to their default settings?\n\nThis process cannot be undone.") == "true" ]]; then + if [[ $(configurator_reset_confirmation_dialog "all emulators" "Are you sure you want to reset all emulators to their default settings?\n\nThis process cannot be undone.") == "true" ]]; then ( prepare_emulator "reset" "all" ) | diff --git a/tools/ftp/virtual_users.txt b/tools/ftp/virtual_users.txt deleted file mode 100644 index 15f36fd0..00000000 --- a/tools/ftp/virtual_users.txt +++ /dev/null @@ -1 +0,0 @@ -retrodeck:retrodeck \ No newline at end of file diff --git a/tools/ftp/vsftpd.conf b/tools/ftp/vsftpd.conf deleted file mode 100644 index 7c1c48a3..00000000 --- a/tools/ftp/vsftpd.conf +++ /dev/null @@ -1,17 +0,0 @@ -# vsftpd config file - -anonymous_enable=NO -virtual_users=YES -userlist_file=/var/config/ftp/virtual_users.txt -userlist_enable=YES -userlist_deny=NO -write_enable=YES -local_root=RETRODECKHOMEDIR -chroot_local_user=YES -local_umask=000 -listen_port=777 -xferlog_enable=YES -xferlog_file=RETRODECKHOMEDIR/.logs/ftp.log -log_ftp_protocol=YES -ftpd_banner=Welcome to RetroDECK -force_dot_files=YES \ No newline at end of file