Fix infinite read when generating passwords; fix UTF-8 enabled systems (#1128)

Fix infinite read when generating passwords; fix UTF-8 enabled systems

When locale is set to UTF-8, tr complains about invalid multibyte characters.
Fix this by setting locale for 'tr' command to LC_ALL=C

Fix infinite read from /dev/urandom when generating passwords.
Password is now generated by reading from /dev/urandom in 100-byte chunks.

Signed-off-by: SirFerdek <17548441+SirFerdek@users.noreply.github.com>

Co-authored-by: Daniel Hansson <github@hanssonit.se>
This commit is contained in:
SirFerdek 2020-03-01 10:23:28 +01:00 committed by GitHub
parent 09658185b0
commit c8cfc600c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

28
lib.sh
View File

@ -48,14 +48,14 @@ UNIXUSER_PROFILE="/home/$UNIXUSER/.bash_profile"
ROOT_PROFILE="/root/.bash_profile"
# Database
SHUF=$(shuf -i 25-29 -n 1)
MARIADB_PASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
NEWMARIADBPASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
MARIADB_PASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=")
NEWMARIADBPASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=")
[ -n "$NCDB" ] && NCCONFIGDB=$(grep "dbname" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")
ETCMYCNF=/etc/mysql/my.cnf
MYCNF=/root/.my.cnf
[ -n "$MYCNFPW" ] && MARIADBMYCNFPASS=$(grep "password" $MYCNF | sed -n "/password/s/^password='\(.*\)'$/\1/p")
PGDB_PASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
NEWPGPASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
PGDB_PASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=")
NEWPGPASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=")
[ -n "$NCDB" ] && NCCONFIGDB=$(grep "dbname" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")
[ -n "$NCDBPASS" ] && NCCONFIGDBPASS=$(grep "dbpassword" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")
# Path to specific files
@ -101,21 +101,21 @@ ADMINER_CONF=/etc/apache2/conf-available/adminer.conf
REDIS_CONF=/etc/redis/redis.conf
REDIS_SOCK=/var/run/redis/redis-server.sock
RSHUF=$(shuf -i 30-35 -n 1)
REDIS_PASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$RSHUF" | head -n 1)
REDIS_PASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=")
# Extra security
SPAMHAUS=/etc/spamhaus.wl
ENVASIVE=/etc/apache2/mods-available/mod-evasive.load
APACHE2=/etc/apache2/apache2.conf
# Full text Search
[ -n "$ES_INSTALL" ] && INDEX_USER=$(tr -dc '[:lower:]' < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ -n "$ES_INSTALL" ] && ROREST=$(tr -dc "A-Za-z0-9" < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ -n "$ES_INSTALL" ] && INDEX_USER=$(gen_passwd "$SHUF" '[:lower:]')
[ -n "$ES_INSTALL" ] && ROREST=$(gen_passwd "$SHUF" "A-Za-z0-9")
[ -n "$ES_INSTALL" ] && nc_fts="ark74/nc_fts"
[ -n "$ES_INSTALL" ] && fts_es_name="fts_esror"
# Talk
[ -n "$TURN_INSTALL" ] && TURN_CONF="/etc/turnserver.conf"
[ -n "$TURN_INSTALL" ] && TURN_PORT=5349
[ -n "$TURN_INSTALL" ] && SHUF=$(shuf -i 25-29 -n 1)
[ -n "$TURN_INSTALL" ] && TURN_SECRET=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ -n "$TURN_INSTALL" ] && TURN_SECRET=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=")
[ -n "$TURN_INSTALL" ] && TURN_DOMAIN=$(sudo -u www-data /var/www/nextcloud/occ config:system:get overwrite.cli.url | sed 's#https://##;s#/##')
## FUNCTIONS
@ -1164,6 +1164,18 @@ do
done
}
# Helper function for generating random passwords
gen_passwd() {
local length=$1
local charset="$2"
local password=""
while [ ${#password} -lt "$length" ]
do
password=$(echo "$password""$(head -c 100 /dev/urandom | LC_ALL=C tr -dc "$charset")" | fold -w "$length" | head -n 1)
done
print_text_in_color "$Cyan" "$password"
}
## bash colors
# Reset
Color_Off='\e[0m' # Text Reset