You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
5.2 KiB
57 lines
5.2 KiB
#!/bin/bash
|
|
#
|
|
# Aplica los parches necesarios a /usr/share/vicibox/vicibox-install.pl
|
|
# (detección socket MySQL/MariaDB, random_pass sin pwgen, DBI con socket)
|
|
#
|
|
# Uso: sudo ./apply-vicibox-patches.sh
|
|
#
|
|
|
|
set -e
|
|
|
|
INSTALL_PL="/usr/share/vicibox/vicibox-install.pl"
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Ejecutar como root."
|
|
exit 1
|
|
fi
|
|
if [ ! -f "$INSTALL_PL" ]; then
|
|
echo "No se encuentra $INSTALL_PL. Ejecuta primero restore.sh."
|
|
exit 1
|
|
fi
|
|
|
|
# Si ya tiene los parches (socket y random_pass), no hacer nada
|
|
if grep -q 'mysql_socket' "$INSTALL_PL" && grep -q 'sub random_pass' "$INSTALL_PL"; then
|
|
echo "vicibox-install.pl ya tiene los parches aplicados."
|
|
exit 0
|
|
fi
|
|
|
|
BACKUP="${INSTALL_PL}.bak.$(date +%Y%m%d%H%M%S)"
|
|
cp -a "$INSTALL_PL" "$BACKUP"
|
|
echo "Copia de seguridad: $BACKUP"
|
|
|
|
# 1) Insertar detección de socket después de "$VICIport = 3306;"
|
|
perl -i -0pe 's/(\$VICIport = 3306;\n)(\$VICIuser = "cron";)/$1# Detect MySQL\/MariaDB socket (openSUSE uses \/var\/run\/mysql\/mysql.sock, others may use \/run\/mysql\/mysql.sock)\n\$mysql_socket = "";\nforeach my \$sock ("\/var\/run\/mysql\/mysql.sock", "\/run\/mysql\/mysql.sock", "\/var\/lib\/mysql\/mysql.sock", "\/tmp\/mysql.sock") {\n\tif (-S \$sock) { \$mysql_socket = \$sock; last; }\n}\n$2/s' "$INSTALL_PL"
|
|
|
|
# 2) Insertar sub random_pass después de "return \$string;\n}\n\n# Get the admin"
|
|
perl -i -0pe 's/(return \$string;\n\}\n\n)(# Get the admin\.php build)/$1# Generate random password string; use pwgen if available, else openssl, else Perl fallback\nsub random_pass {\n\tmy \$len = \$_\[0\] \|\| 15;\n\tif (-x \"\/usr\/bin\/pwgen\") {\n\t\tmy \$s = trim(\`\/usr\/bin\/pwgen -cns \$len 1\`);\n\t\treturn \$s if \$s;\n\t}\n\tif (-x \"\/usr\/bin\/openssl\") {\n\t\tmy \$s = trim(\`\/usr\/bin\/openssl rand -base64 \$len\`);\n\t\t\$s =~ s\/\[^a-zA-Z0-9\]\/\/g;\n\t\treturn substr(\$s, 0, \$len) if length(\$s) >= \$len;\n\t}\n\tmy \@c = (\x27a\x27..\x27z\x27, \x27A\x27..\x27Z\x27, 0..9);\n\treturn join(\x27\x27, map { \$c[rand \@c] } 1..\$len);\n}\n\n$2/s' "$INSTALL_PL"
|
|
|
|
# 3) Primera conexión DBI (root) -> usar dsn_root
|
|
perl -i -0pe 's/\t\t\$dbhVDnew = DBI->connect\("DBI:mysql::localhost:\$VICIport", "root", ""\) or die "Couldn\x27t connect to MySQL to create database: " \. DBI->errstr;\n\t\tif \(\$DBS==0\)/\t\tmy \$dsn_root = "DBI:mysql::localhost:\$VICIport" \. (\$mysql_socket ? ";mysql_socket=\$mysql_socket" : "");\n\t\t\$dbhVDnew = DBI->connect(\$dsn_root, "root", "") or die "Couldn\x27t connect to MySQL to create database: " \. DBI->errstr;\n\t\tif (\$DBS==0)/s' "$INSTALL_PL"
|
|
|
|
# 4) Reconnect to new database -> dsn_db
|
|
perl -i -0pe 's/\t\t# Reconnect to the new database\n\t\t\$dbhVDnew->disconnect;\n\t\t\$dbhVDnew = DBI->connect\("DBI:mysql:\$VICIdatabase:localhost:\$VICIport", "root", ""\) or die "Couldn\x27t reconnect to MySQL to create database tables: " \. DBI->errstr;/# Reconnect to the new database\n\t\t\$dbhVDnew->disconnect;\n\t\tmy \$dsn_db = "DBI:mysql:\$VICIdatabase:localhost:\$VICIport" \. (\$mysql_socket ? ";mysql_socket=\$mysql_socket" : "");\n\t\t\$dbhVDnew = DBI->connect(\$dsn_db, "root", "") or die "Couldn\x27t reconnect to MySQL to create database tables: " \. DBI->errstr;/s' "$INSTALL_PL"
|
|
|
|
# 5) Slave reconnect -> dsn_root2
|
|
perl -i -0pe 's/\t\t\t# release and reconnect since we restarted the server\n\t\t\t\$dbhVDnew->disconnect;\n\t\t\t\$dbhVDnew = DBI->connect\("DBI:mysql::localhost:\$VICIport", "root", ""\) or die "Couldn\x27t connect to MySQL to create database: " \. DBI->errstr;\n\t\t\t# Reset the slave/\t\t\t# release and reconnect since we restarted the server\n\t\t\t\$dbhVDnew->disconnect;\n\t\t\tmy \$dsn_root2 = "DBI:mysql::localhost:\$VICIport" \. (\$mysql_socket ? ";mysql_socket=\$mysql_socket" : "");\n\t\t\t\$dbhVDnew = DBI->connect(\$dsn_root2, "root", "") or die "Couldn\x27t connect to MySQL to create database: " \. DBI->errstr;\n\t\t\t# Reset the slave/s' "$INSTALL_PL"
|
|
|
|
# 6) pwgen -> random_pass (dos líneas de passwords)
|
|
perl -i -pe 's/my \$randomstring1=trim\(`\/usr\/bin\/pwgen -cns 15 1`\);/my \$randomstring1=random_pass(15);/; s/my \$randomstring2=trim\(`\/usr\/bin\/pwgen -cns 15 1`\);/my \$randomstring2=random_pass(15);/' "$INSTALL_PL"
|
|
|
|
# 7) Conexión cron (dbhVD) -> dsn_cron
|
|
perl -i -0pe 's/\t\t\t\t# At this point the primary should be up and good, so we set-up our normal DBI connection for later processes\n\t\t\t\t# Also a good double-check procedure that stuff works\n\t\t\t\t\$dbhVD = DBI->connect\("DBI:mysql:\$VICIdatabase:localhost:\$VICIport", "\$VICIuser", "\$VICIpass"\) or die "Couldn\x27t connect to ViciDial database: " \. DBI->errstr;/# At this point the primary should be up and good, so we set-up our normal DBI connection for later processes\n\t\t\t\t# Also a good double-check procedure that stuff works\n\t\t\t\tmy \$dsn_cron = "DBI:mysql:\$VICIdatabase:localhost:\$VICIport" \. (\$mysql_socket ? ";mysql_socket=\$mysql_socket" : "");\n\t\t\t\t\$dbhVD = DBI->connect(\$dsn_cron, "\$VICIuser", "\$VICIpass") or die "Couldn\x27t connect to ViciDial database: " \. DBI->errstr;/s' "$INSTALL_PL"
|
|
|
|
# 8) Más pwgen -> random_pass
|
|
perl -i -pe 's/\$randomstring=trim\(`pwgen -cns 32 1`\);/\$randomstring=random_pass(32);/; s/\$randomstring=trim\(`\/usr\/bin\/pwgen -cns 15 1`\);/\$randomstring=random_pass(15);/g' "$INSTALL_PL"
|
|
|
|
echo "Parches aplicados correctamente a $INSTALL_PL"
|