Import alt-mysql plugin
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
ROUNDCUBE_PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
ROUNDCUBE_SETTINGS_FILE="${ROUNDCUBE_SETTINGS_FILE:-$ROUNDCUBE_PLUGIN_DIR/plugin-settings.conf}"
|
||||
ROUNDCUBE_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=./mysql_common.sh
|
||||
source "$ROUNDCUBE_SCRIPT_DIR/mysql_common.sh"
|
||||
mysql_load_plugin_settings_file "$ROUNDCUBE_SETTINGS_FILE"
|
||||
|
||||
roundcube_read_setting() {
|
||||
local key="$1"
|
||||
local default="$2"
|
||||
local line value
|
||||
[ -r "$ROUNDCUBE_SETTINGS_FILE" ] || {
|
||||
printf '%s\n' "$default"
|
||||
return 0
|
||||
}
|
||||
line="$(grep -E "^${key}=" "$ROUNDCUBE_SETTINGS_FILE" | tail -n 1 || true)"
|
||||
[ -n "$line" ] || {
|
||||
printf '%s\n' "$default"
|
||||
return 0
|
||||
}
|
||||
value="${line#*=}"
|
||||
value="${value%%#*}"
|
||||
value="${value%\"}"
|
||||
value="${value#\"}"
|
||||
value="${value%\'}"
|
||||
value="${value#\'}"
|
||||
value="$(printf '%s' "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
|
||||
printf '%s\n' "${value:-$default}"
|
||||
}
|
||||
|
||||
roundcube_enabled() {
|
||||
case "$(roundcube_read_setting ROUNDCUBE_ENABLED true)" in
|
||||
true|yes|on|1) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
roundcube_config_file() {
|
||||
roundcube_read_setting ROUNDCUBE_CONFIG_FILE /var/www/html/roundcube/config/config.inc.php
|
||||
}
|
||||
|
||||
roundcube_custom_config_file() {
|
||||
roundcube_read_setting ROUNDCUBE_CUSTOM_CONFIG_FILE /usr/local/directadmin/custombuild/custom/roundcube/config.inc.php
|
||||
}
|
||||
|
||||
roundcube_write_custombuild_config() {
|
||||
case "$(roundcube_read_setting ROUNDCUBE_WRITE_CUSTOMBUILD_CONFIG true)" in
|
||||
true|yes|on|1) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
roundcube_db_name() {
|
||||
roundcube_read_setting ROUNDCUBE_DB_NAME roundcube
|
||||
}
|
||||
|
||||
roundcube_db_user() {
|
||||
roundcube_read_setting ROUNDCUBE_DB_USER roundcube
|
||||
}
|
||||
|
||||
roundcube_password_file() {
|
||||
roundcube_read_setting ROUNDCUBE_DB_PASSWORD_FILE /usr/local/directadmin/conf/alt-roundcube.conf
|
||||
}
|
||||
|
||||
roundcube_native_my_cnf() {
|
||||
roundcube_read_setting ROUNDCUBE_NATIVE_MY_CNF /usr/local/directadmin/conf/my.cnf
|
||||
}
|
||||
|
||||
roundcube_endpoint_mode() {
|
||||
roundcube_read_setting ROUNDCUBE_ALT_DSN_MODE tcp
|
||||
}
|
||||
|
||||
roundcube_generate_secret() {
|
||||
if command -v openssl >/dev/null 2>&1; then
|
||||
openssl rand -base64 36 | tr -d '\n'
|
||||
return 0
|
||||
fi
|
||||
tr -dc 'A-Za-z0-9_@%+=' </dev/urandom | head -c 48
|
||||
}
|
||||
|
||||
roundcube_load_or_create_password() {
|
||||
local file user pass group_name tmp
|
||||
file="$(roundcube_password_file)"
|
||||
user="$(roundcube_db_user)"
|
||||
|
||||
if [ -r "$file" ]; then
|
||||
pass="$(awk -F= '$1 == "password" || $1 == "passwd" { sub(/^[^=]*=/, ""); print; exit }' "$file")"
|
||||
if [ -n "$pass" ]; then
|
||||
printf '%s\n' "$pass"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
pass="$(roundcube_generate_secret)"
|
||||
group_name=""
|
||||
if command -v getent >/dev/null 2>&1 && getent group diradmin >/dev/null 2>&1; then
|
||||
group_name="diradmin"
|
||||
elif command -v getent >/dev/null 2>&1 && getent group root >/dev/null 2>&1; then
|
||||
group_name="root"
|
||||
fi
|
||||
tmp="$(mktemp)"
|
||||
{
|
||||
printf 'user=%s\n' "$user"
|
||||
printf 'password=%s\n' "$pass"
|
||||
} > "$tmp"
|
||||
if [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "$group_name" ]; then
|
||||
install -m 0640 -o root -g "$group_name" "$tmp" "$file"
|
||||
else
|
||||
install -m 0600 "$tmp" "$file"
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
printf '%s\n' "$pass"
|
||||
}
|
||||
|
||||
roundcube_load_password() {
|
||||
local file pass
|
||||
file="$(roundcube_password_file)"
|
||||
[ -r "$file" ] || return 1
|
||||
pass="$(awk -F= '$1 == "password" || $1 == "passwd" { sub(/^[^=]*=/, ""); print; exit }' "$file")"
|
||||
[ -n "$pass" ] || return 1
|
||||
printf '%s\n' "$pass"
|
||||
}
|
||||
|
||||
roundcube_urlencode() {
|
||||
php -r 'echo rawurlencode($argv[1]);' "$1"
|
||||
}
|
||||
|
||||
roundcube_alt_dsn() {
|
||||
local db user pass host port mode encoded_user encoded_pass
|
||||
mysql_load_alt_runtime
|
||||
db="$(roundcube_db_name)"
|
||||
user="$(roundcube_db_user)"
|
||||
pass="$(roundcube_load_or_create_password)"
|
||||
mode="$(roundcube_endpoint_mode)"
|
||||
encoded_user="$(roundcube_urlencode "$user")"
|
||||
encoded_pass="$(roundcube_urlencode "$pass")"
|
||||
|
||||
case "$mode" in
|
||||
socket)
|
||||
printf 'mysql://%s:%s@unix(%s)/%s\n' "$encoded_user" "$encoded_pass" "$MYSQL_ALT_SOCKET" "$db"
|
||||
;;
|
||||
*)
|
||||
host="127.0.0.1"
|
||||
port="$MYSQL_ALT_PORT"
|
||||
printf 'mysql://%s:%s@%s:%s/%s\n' "$encoded_user" "$encoded_pass" "$host" "$port" "$db"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
roundcube_sql_quote() {
|
||||
local value="$1"
|
||||
value="${value//\\/\\\\}"
|
||||
value="${value//\'/\'\'}"
|
||||
printf "'%s'" "$value"
|
||||
}
|
||||
|
||||
roundcube_create_alt_database_and_user() {
|
||||
local db user pass q_user q_pass
|
||||
mysql_load_alt_credentials
|
||||
mysql_ensure_metadata_schema
|
||||
db="$(roundcube_db_name)"
|
||||
user="$(roundcube_db_user)"
|
||||
pass="$(roundcube_load_or_create_password)"
|
||||
q_user="$(roundcube_sql_quote "$user")"
|
||||
q_pass="$(roundcube_sql_quote "$pass")"
|
||||
|
||||
mysql_safe_identifier "$db" >/dev/null
|
||||
mysql_safe_identifier "$user" >/dev/null
|
||||
|
||||
mysql_exec mysql -e "CREATE DATABASE IF NOT EXISTS $(mysql_quote_identifier "$db") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
mysql_exec mysql -e "CREATE USER IF NOT EXISTS ${q_user}@'localhost' IDENTIFIED BY ${q_pass};"
|
||||
mysql_exec mysql -e "ALTER USER ${q_user}@'localhost' IDENTIFIED BY ${q_pass};"
|
||||
mysql_exec mysql -e "CREATE USER IF NOT EXISTS ${q_user}@'127.0.0.1' IDENTIFIED BY ${q_pass};"
|
||||
mysql_exec mysql -e "ALTER USER ${q_user}@'127.0.0.1' IDENTIFIED BY ${q_pass};"
|
||||
mysql_exec mysql -e "GRANT ALL PRIVILEGES ON $(mysql_quote_identifier "$db").* TO ${q_user}@'localhost';"
|
||||
mysql_exec mysql -e "GRANT ALL PRIVILEGES ON $(mysql_quote_identifier "$db").* TO ${q_user}@'127.0.0.1';"
|
||||
mysql_exec mysql -e "
|
||||
INSERT INTO da_plugin_system_databases (service_name, db_name, db_user, endpoint_mode, config_path, enabled)
|
||||
VALUES ('roundcube', '$(mysql_escape_literal "$db")', '$(mysql_escape_literal "$user")', '$(mysql_escape_literal "$(roundcube_endpoint_mode)")', '$(mysql_escape_literal "$(roundcube_config_file)")', 1)
|
||||
ON DUPLICATE KEY UPDATE db_name=VALUES(db_name), db_user=VALUES(db_user), endpoint_mode=VALUES(endpoint_mode), config_path=VALUES(config_path), enabled=VALUES(enabled), updated_at=CURRENT_TIMESTAMP;
|
||||
"
|
||||
}
|
||||
Reference in New Issue
Block a user