summaryrefslogtreecommitdiffstats
path: root/net/adguardhome/files/adguardhome.defaults
blob: d19d531eee04828e70ce600b65e348359855a0dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
#
# SPDX-License-Identifier: GPL-2.0-only

# Migrate old config format only
OLD_CONFIG_FILE=$(uci -q get adguardhome.config.config)
OLD_CONFIG_FILE=${OLD_CONFIG_FILE:-/etc/adguardhome.yaml}
OLD_CONFIG_NEW_OPT_FILE=$(uci -q get adguardhome.config.config_file)
NEW_CONFIG_DIR=/etc/adguardhome
NEW_CONFIG_FILE="$NEW_CONFIG_DIR/adguardhome.yaml"

start_service() {
	if ! /etc/init.d/adguardhome running; then
		/etc/init.d/adguardhome start
	fi
}

stop_service() {
	if /etc/init.d/adguardhome running; then
		/etc/init.d/adguardhome stop
	fi
}

if [ -f "$OLD_CONFIG_NEW_OPT_FILE" ] && [ "$OLD_CONFIG_NEW_OPT_FILE" != "$NEW_CONFIG_FILE" ]; then
	echo "Old AdGuard Home config found in '$OLD_CONFIG_NEW_OPT_FILE'"

	USER=$(uci -q get adguardhome.config.user)
	USER=${USER:-adguardhome}

	echo "AdGuard Home config is stored in a non-default path."
	echo "Ensure configured service user '$USER' can access it."

elif [ -f "$OLD_CONFIG_FILE" ] && [ "$OLD_CONFIG_FILE" != "$NEW_CONFIG_FILE" ]; then
	echo "Old AdGuard Home config found in '$OLD_CONFIG_FILE'"
	OLD_CONFIG_DIR=$(dirname "$OLD_CONFIG_FILE")

	USER=$(uci -q get adguardhome.config.user)
	USER=${USER:-adguardhome}
	GROUP=$(uci -q get adguardhome.config.group)
	GROUP=${GROUP:-adguardhome}

	CUR_CONFIG_FILE="$OLD_CONFIG_FILE"
	if [ "$OLD_CONFIG_DIR" = "/etc" ]; then
		echo "AdGuard Home config must be stored in its own directory. Migrating..."
		stop_service

		echo "Using $USER:$GROUP for file ownership."

		[ -d "$NEW_CONFIG_DIR" ] || mkdir -m 0700 -p "$NEW_CONFIG_DIR"
		mv "$OLD_CONFIG_FILE" "$NEW_CONFIG_FILE"
		chown -R "$USER":"$GROUP" "$NEW_CONFIG_DIR"
		CUR_CONFIG_FILE="$NEW_CONFIG_FILE"

		echo "Config migrated to '$NEW_CONFIG_FILE'"

	else
		echo "AdGuard Home config is stored in a non-default path."
		echo "Ensure configured service user '$USER' can access it."
	fi

	uci set adguardhome.config.config_file="$CUR_CONFIG_FILE"
	uci -q delete adguardhome.config.config

	# Use awk to split match on :, remove double quotes and trim leading and
	# trailing spaces
	cert_path=$(grep certificate_path: "$CUR_CONFIG_FILE" \
		| awk -F':' '{gsub(/"/, "", $2); gsub(/^ +| +$/, "", $2); print $2}')
	if [ -n "$cert_path" ]; then
		echo "Found custom 'certificate_path' pointing to '$cert_path'." \
			+ "Ensure configured service user '$USER' can access it."

		stop_service

		if ! uci -q show adguardhome.config.jail_mount | grep -q "$cert_path"; then
			uci add_list adguardhome.config.jail_mount="$cert_path"
		fi
	fi

	private_key_path=$(grep private_key_path: "$CUR_CONFIG_FILE" \
		| awk -F':' '{gsub(/"/, "", $2); gsub(/^ +| +$/, "", $2); print $2}')
	if [ -n "$private_key_path" ]; then
		echo "Found custom 'private_key_path' pointing to '$private_key_path'." \
			+ "Ensure configured service user '$USER' can access it."

		stop_service

		if ! uci -q show adguardhome.config.jail_mount | grep -q "$private_key_path"; then
			uci add_list adguardhome.config.jail_mount="$private_key_path"
		fi
	fi

	uci commit adguardhome
	start_service

elif [ -z "$OLD_CONFIG_NEW_OPT_FILE" ] && [ "$OLD_CONFIG_FILE" != "$NEW_CONFIG_FILE" ]; then
	echo "Old AdGuard Home config not found in '$OLD_CONFIG_FILE'"
	stop_service

	# Service script will create the new config directory
	uci set adguardhome.config.config_file="$NEW_CONFIG_FILE"
	uci -q delete adguardhome.config.config
	echo "Config path changed to '$NEW_CONFIG_FILE'"

	uci commit adguardhome
	start_service

else
	echo "AdGuard Home config is in its default path '$NEW_CONFIG_FILE'"
fi