#!/bin/bash do { #//////////////////////////////////// # DietPi Function: # - Enables control and applies settings for specific software and configs. # #//////////////////////////////////// # Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com # #//////////////////////////////////// # # Usage: # setvar FP_SCRIPT = ''/DietPi/dietpi/func/dietpi-set_software'' setvar AVAIABLE_COMMANDS = "" Available commands $FP_SCRIPT locale en_GB.UTF-8 $FP_SCRIPT allo eth_dhcp / eth_static $FP_SCRIPT useradd X=create user with name X and default permissions, using global DietPi password (dietpi) $FP_SCRIPT userdel X=delete user with name X $FP_SCRIPT apt-mirror url/default $FP_SCRIPT ntpd-mode configures NTPD mode (eg: ntp/systemd) $FP_SCRIPT verify_dietpi.txt verifies dietpi.txt entries, adds missing entries if required "" #//////////////////////////////////// #Grab inputs setvar INPUT_MODE_NAME = "$1" setvar INPUT_MODE_VALUE = "$2" setvar INPUT_ADDITIONAL_1 = "$3" setvar INPUT_ADDITIONAL_2 = "$4" setvar INPUT_ADDITIONAL_3 = "$5" setvar INPUT_ADDITIONAL_4 = "$6" #Import DietPi-Globals --------------------------------------------------------------- source /DietPi/dietpi/func/dietpi-globals G_CHECK_ROOT_USER G_CHECK_ROOTFS_RW export G_PROGRAM_NAME='DietPi-Set_software' #Import DietPi-Globals --------------------------------------------------------------- setvar EXIT_CODE = '0' proc Unknown_Input_Name { setvar EXIT_CODE = '1' G_DIETPI-NOTIFY 2 "Unknown input name ($INPUT_MODE_NAME). Nothing has been applied." echo -e $AVAIABLE_COMMANDS } proc Unknown_Input_Mode { setvar EXIT_CODE = '1' G_DIETPI-NOTIFY 2 "Unknown input value ($INPUT_MODE_VALUE). Nothing has been applied." } #///////////////////////////////////////////////////////////////////////////////////// # locale # NB: auto installs en_GB.UTF-8 alongside any input option #///////////////////////////////////////////////////////////////////////////////////// proc Locale_Main { if test -n $INPUT_MODE_VALUE { cat <<< """ > /etc/locale.gen $INPUT_MODE_VALUE UTF-8 en_GB.UTF-8 UTF-8 """ > /etc/locale.gen $INPUT_MODE_VALUE UTF-8 en_GB.UTF-8 UTF-8 _EOF_ G_RUN_CMD dpkg-reconfigure -f noninteractive locales # - and force locale for remote access, especially dropbear, where receiving locale from client can't be suppressed: cat <<< """ > /etc/profile.d/99-dietpi-force-locale.sh # To force server locales on SSH access, as dropbear does automatically overwrite them by client values: export LANG=$INPUT_MODE_VALUE export LC_ALL=$INPUT_MODE_VALUE export LANGUAGE=$( echo $INPUT_MODE_VALUE | sed 's/\..*//' ):$( echo $INPUT_MODE_VALUE | sed 's/_.*//' ) """ > /etc/profile.d/99-dietpi-force-locale.sh # To force server locales on SSH access, as dropbear does automatically overwrite them by client values: export LANG=$INPUT_MODE_VALUE export LC_ALL=$INPUT_MODE_VALUE export LANGUAGE=$( echo $INPUT_MODE_VALUE | sed 's/\..*//' ):$( echo $INPUT_MODE_VALUE | sed 's/_.*//' ) _EOF_ chmod +x /etc/profile.d/99-dietpi-force-locale.sh sed -i "/AUTO_SETUP_LOCALE=/c\AUTO_SETUP_LOCALE=$INPUT_MODE_VALUE" /DietPi/dietpi.txt } else { Unknown_Input_Mode } } #///////////////////////////////////////////////////////////////////////////////////// # Set Apt Mirror #///////////////////////////////////////////////////////////////////////////////////// proc AptMirror_Main { if test -n $INPUT_MODE_VALUE { # - Set defaults? if test $INPUT_MODE_VALUE = "default" { if (( $G_HW_MODEL < 10 )) { setvar INPUT_MODE_VALUE = ''https://www.mirrorservice.org/sites/archive.raspbian.org/raspbian'' } else { setvar INPUT_MODE_VALUE = ''https://deb.debian.org/debian/'' } } # - Set raspbian if (( $G_HW_MODEL < 10 )) { cat <<< """ > /etc/apt/sources.list deb $INPUT_MODE_VALUE $G_DISTRO_NAME main contrib non-free rpi """ > /etc/apt/sources.list deb $INPUT_MODE_VALUE $G_DISTRO_NAME main contrib non-free rpi _EOF_ cat <<< """ > /etc/apt/sources.list.d/raspi.list deb https://archive.raspberrypi.org/debian/ $G_DISTRO_NAME main ui """ > /etc/apt/sources.list.d/raspi.list deb https://archive.raspberrypi.org/debian/ $G_DISTRO_NAME main ui _EOF_ # Update dietpi.txt entry sed -i "/CONFIG_APT_RASPBIAN_MIRROR=/c\CONFIG_APT_RASPBIAN_MIRROR=$INPUT_MODE_VALUE" /DietPi/dietpi.txt # - Set debian } else { cat <<< """ > /etc/apt/sources.list deb $INPUT_MODE_VALUE $G_DISTRO_NAME main contrib non-free deb $INPUT_MODE_VALUE $G_DISTRO_NAME-updates main contrib non-free deb https://deb.debian.org/debian-security/ $G_DISTRO_NAME/updates main contrib non-free deb $INPUT_MODE_VALUE $G_DISTRO_NAME-backports main contrib non-free """ > /etc/apt/sources.list deb $INPUT_MODE_VALUE $G_DISTRO_NAME main contrib non-free deb $INPUT_MODE_VALUE $G_DISTRO_NAME-updates main contrib non-free deb https://deb.debian.org/debian-security/ $G_DISTRO_NAME/updates main contrib non-free deb $INPUT_MODE_VALUE $G_DISTRO_NAME-backports main contrib non-free _EOF_ # Jessie, switch deb.debian.org to http: https://github.com/Fourdee/DietPi/issues/1285#issuecomment-351830101 if (( $G_DISTRO < 4 )) { sed -i 's|https://deb.debian.org|http://deb.debian.org|g' /etc/apt/sources.list # Buster, remove backports: https://github.com/Fourdee/DietPi/issues/1285#issuecomment-351830101 } elif (( $G_DISTRO > 4 )) { sed -i '/backports/d' /etc/apt/sources.list } # Update dietpi.txt entry sed -i "/CONFIG_APT_DEBIAN_MIRROR=/c\CONFIG_APT_DEBIAN_MIRROR=$INPUT_MODE_VALUE" /DietPi/dietpi.txt } } else { Unknown_Input_Mode } } #///////////////////////////////////////////////////////////////////////////////////// # Set NTPD Mode # NB: also sets mirror as defined in dietpi.txt CONFIG_NTP_MIRROR # NB: Also restarts NTP updates and waits for them to complete #///////////////////////////////////////////////////////////////////////////////////// proc NtpdMode_Main { if test -n $INPUT_MODE_VALUE { # - Reset global to disabled, prevents run_ntpd in dietpi-software sed -i "/CONFIG_NTP_MODE=/c\CONFIG_NTP_MODE=0" /DietPi/dietpi.txt # Kill all existing ntp killall -w /DietPi/dietpi/func/run_ntpd &> /dev/null killall -w ntpd &> /dev/null rm /var/lib/dietpi/.ntpd_exit_status &> /dev/null local ntpd_mirror=$(grep -m1 '^CONFIG_NTP_MIRROR=' /DietPi/dietpi.txt | sed 's/.*=//') # - Set defaults? if test $ntpd_mirror = "default" { setvar ntpd_mirror = ''debian.pool.ntp.org'' } #Install ntp if required if (( $INPUT_MODE_VALUE >= 1 && $INPUT_MODE_VALUE <= 3 )) { # - avoid dpsoftware install if system first run is not completed if (( $G_DIETPI_INSTALL_STAGE >= 0 )) { /DietPi/dietpi/dietpi-software install 170 } # - Set NTPD mirror # - Remove obsolete 'server' entries. sed -i '/^[[:blank:]#;]*server [0-9]/d' /etc/ntp.conf for ((i=0; i<4; i++)) do if grep "^[[:blank:]]*pool $i" /etc/ntp.conf; then sed -i "s/^[[:blank:]]*pool $i.*/pool $i.$ntpd_mirror iburst/" /etc/ntp.conf elif grep "^[[:blank:]#;]*pool $i" /etc/ntp.conf; then sed -i "s/^[[:blank:]#;]*pool $i.*/pool $i.$ntpd_mirror iburst/" /etc/ntp.conf else echo -e "pool $i.$ntpd_mirror iburst" >> /etc/ntp.conf fi done #Uninstall ntp if not required } else { # - avoid dpsoftware uninstall if system first run is not completed if (( $G_DIETPI_INSTALL_STAGE >= 0 )) { /DietPi/dietpi/dietpi-software uninstall 170 } else { G_AGP ntp } } #Setup Drift mode, use SystemD if (( $INPUT_MODE_VALUE == 4 )) { # - Set NTPD mirror cat <<< """ > /etc/systemd/timesyncd.conf [Time] Servers=0.$ntpd_mirror 1.$ntpd_mirror 2.$ntpd_mirror 3.$ntpd_mirror """ > /etc/systemd/timesyncd.conf [Time] Servers=0.$ntpd_mirror 1.$ntpd_mirror 2.$ntpd_mirror 3.$ntpd_mirror _EOF_ # - enable systemctl daemon-reload timedatectl set-ntp true #Disable SystemD and allow "run_ntpd" script to take control } else { systemctl stop systemd-timesyncd systemctl disable systemd-timesyncd } # - Update DietPi.txt sed -i "/CONFIG_NTP_MODE=/c\CONFIG_NTP_MODE=$INPUT_MODE_VALUE" /DietPi/dietpi.txt # - Update now /DietPi/dietpi/func/run_ntpd status } else { Unknown_Input_Mode } } #///////////////////////////////////////////////////////////////////////////////////// # allo #///////////////////////////////////////////////////////////////////////////////////// proc Allo_Main { if test -n $INPUT_MODE_VALUE { if test $INPUT_MODE_VALUE = "eth_dhcp" { sed -i "/iface eth/c\iface eth0 inet dhcp" /etc/network/interfaces sed -i "$(cat -n /etc/network/interfaces | grep 'dns-nameservers ' | sed -n 1p | awk '{print $1}')s/.*/#dns-nameservers 8.8.8.8/" /etc/network/interfaces } elif test $INPUT_MODE_VALUE = "eth_static" { sed -i "/iface eth/c\iface eth0 inet static" /etc/network/interfaces sed -i "$(cat -n /etc/network/interfaces | grep 'address ' | sed -n 1p | awk '{print $1}')s/.*/address $INPUT_ADDITIONAL_1/" /etc/network/interfaces sed -i "$(cat -n /etc/network/interfaces | grep 'gateway ' | sed -n 1p | awk '{print $1}')s/.*/gateway $INPUT_ADDITIONAL_2/" /etc/network/interfaces sed -i "$(cat -n /etc/network/interfaces | grep 'netmask ' | sed -n 1p | awk '{print $1}')s/.*/netmask $INPUT_ADDITIONAL_3/" /etc/network/interfaces sed -i "$(cat -n /etc/network/interfaces | grep 'dns-nameservers ' | sed -n 1p | awk '{print $1}')s/.*/dns-nameservers $INPUT_ADDITIONAL_4/" /etc/network/interfaces } } else { Unknown_Input_Mode } } #///////////////////////////////////////////////////////////////////////////////////// # Users #///////////////////////////////////////////////////////////////////////////////////// proc Useradd_Main { if test -n $INPUT_MODE_VALUE { # - Create $INPUT_MODE_VALUE mkdir -p /home useradd -m -s /bin/bash $INPUT_MODE_VALUE chpasswd <<< "$INPUT_MODE_VALUE:$(cat /DietPi/dietpi.txt | grep -m1 '^AUTO_SETUP_GLOBAL_PASSWORD=' | sed 's/.*=//')" # Copy existing profile/bashrc cp /root/.profile /home/$INPUT_MODE_VALUE/ cp /root/.bashrc /home/$INPUT_MODE_VALUE/ # Sudo up dietpi login script sed -i '/^\/DietPi\/dietpi\/login/c\sudo \/DietPi\/dietpi\/login' /home/$INPUT_MODE_VALUE/.bashrc chown -R $INPUT_MODE_VALUE:$INPUT_MODE_VALUE /home/$INPUT_MODE_VALUE # Allow sudo without pw if (( ! $(cat /etc/sudoers | grep -ci -m1 "^$INPUT_MODE_VALUE[[:space:]]") )) { cat <<< """ >> /etc/sudoers $INPUT_MODE_VALUE ALL=NOPASSWD: ALL """ >> /etc/sudoers $INPUT_MODE_VALUE ALL=NOPASSWD: ALL _EOF_ } # Same groups as user pi local group_array=() setvar group_array = ''('input') setvar group_array = ''('netdev') setvar group_array = ''('spi') setvar group_array = ''('i2c') setvar group_array = ''('gpio') setvar group_array = ''('tty') setvar group_array = ''('users') setvar group_array = ''('games') setvar group_array = ''('plugdev') setvar group_array = ''('video') setvar group_array = ''('audio') setvar group_array = ''('sudo') setvar group_array = ''('cdrom') setvar group_array = ''('dialout') setvar group_array = ''('adm') # + allow access to www-data setvar group_array = ''('www-data') for ((i=0; i<${#group_array[@]}; i++)) do usermod -a -G ${group_array[$i]} $INPUT_MODE_VALUE done unset group_array } else { Unknown_Input_Mode } } proc Userdel_Main { if test -n $INPUT_MODE_VALUE { # - Delete $INPUT_MODE_VALUE userdel -f $INPUT_MODE_VALUE rm -R /home/$INPUT_MODE_VALUE # - Remove from sudoers sed -i "/^$INPUT_MODE_VALUE[[:space:]]/d" /etc/sudoers } else { Unknown_Input_Mode } } proc Verify_DietPi_Txt { #Verify entries with git dietpi.txt local gitbranch=$(grep -m1 '^DEV_GITBRANCH=' /DietPi/dietpi.txt | sed 's/.*=//') if test -z $gitbranch { setvar gitbranch = ''master'' } setvar INSTALL_URL = ""https://raw.githubusercontent.com/Fourdee/DietPi/$gitbranch/dietpi.txt"" G_CHECK_URL $INSTALL_URL if (( $? == 0 )) { G_DIETPI-NOTIFY 0 "Patching dietpi.txt" wget $INSTALL_URL -O /tmp/dietpi.txt_patch local entry=0 local value=0 while read line { setvar entry = $(echo -e "$line" | grep "^[^#;/]" ) if test -n $entry { setvar value = $(echo -e "${entry##*=}") #*=X setvar entry = $(echo -e "${entry%%=*}") #X* # - Patch new entry if required if (( ! $(grep -ci -m1 "^$entry=" /DietPi/dietpi.txt) )) { G_DIETPI-NOTIFY 2 "Updating dietpi.txt with new entry: $entry=$value" cat <<< """ >> /DietPi/dietpi.txt $entry=$value """ >> /DietPi/dietpi.txt $entry=$value _EOF_ } else { G_DIETPI-NOTIFY 0 "Verified: $entry" } } } < /tmp/dietpi.txt_patch rm /tmp/dietpi.txt_patch G_DIETPI-NOTIFY 0 "dietpi.txt verification completed" } else { G_DIETPI-NOTIFY 1 "Failed to verify dietpi.txt, $INSTALL_URL is offline and/or inaccessible" } } #///////////////////////////////////////////////////////////////////////////////////// # Main Loop #///////////////////////////////////////////////////////////////////////////////////// #----------------------------------------------------------------------------------- #info G_DIETPI-NOTIFY 3 DietPi-Set_Software "$INPUT_MODE_NAME ($INPUT_MODE_VALUE)" #----------------------------------------------------------------------------------- if test $INPUT_MODE_NAME = "apt-mirror" { AptMirror_Main } elif test $INPUT_MODE_NAME = "useradd" { Useradd_Main } elif test $INPUT_MODE_NAME = "userdel" { Userdel_Main } elif test $INPUT_MODE_NAME = "ntpd-mirror" { NtpdMirror_Main } elif test $INPUT_MODE_NAME = "ntpd-mode" { NtpdMode_Main } elif test $INPUT_MODE_NAME = "allo" { Allo_Main } elif test $INPUT_MODE_NAME = "verify_dietpi.txt" { Verify_DietPi_Txt } elif test $INPUT_MODE_NAME = 'locale' { Locale_Main } else { Unknown_Input_Name } #----------------------------------------------------------------------------------- G_DIETPI-NOTIFY -1 $EXIT_CODE "$INPUT_MODE_NAME $INPUT_MODE_VALUE |" #----------------------------------------------------------------------------------- exit $EXIT_CODE #----------------------------------------------------------------------------------- }