aboutsummaryrefslogtreecommitdiff
path: root/net/radicale2/files/radicale2.init
blob: ac7a81a757b2128a5d91a923e6a59f52bea13de6 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/sh /etc/rc.common

START=80
STOP=10

CFGDIR=/var/etc/radicale2
SYSCFG=$CFGDIR/config
USRCFG=$CFGDIR/users

DATADIR="/srv/radicale2/data"
LOGDIR=""
USE_PROCD=1

# we could start with empty configuration file using defaults
[ -f ${IPKG_INSTROOT}/etc/config/radicale2 ] || touch ${IPKG_INSTROOT}/etc/config/radicale2

conf_line() {
	local cfgfile="$1"
	local option="$2"
	local value="$3"

	if [ -n "$value" ]; then
		eval "echo '$2' = '$value' >>'$cfgfile'"
	fi
}

conf_getline() {
	local cfg="$1"
	local cfgfile="$2"
	local option="$3"
	local defval="$4"
	local flag="$5"
	unset value

	if [ "$flag" != "1" ]; then
		config_get value "$cfg" "$option" "$defval"
		conf_line "$cfgfile" "$option" "$value"
	else
		config_get_bool value "$cfg" "$option" "$defval"
		[ -z "$defval" ] && defval=1
		if [ "$value" -ne "$defval" ]; then
			if [ "$value" -ne 0 ]; then
				conf_line "$cfgfile" "$option" "True"
			else
				conf_line "$cfgfile" "$option" "False"
			fi
		fi
	fi
}

build_hosts_line() {
	local val="$1"

	append hostlist "$val" ", "
}

conf_section() {
	local cfg="$1"
	local cfgfile="$2"
	local hostlist=""
	local value

	echo "[$cfg]
" >>$cfgfile

	case $cfg in
	server)
		config_list_foreach "$cfg" host build_hosts_line
		conf_line "$tmpfile" hosts "$hostlist"
		conf_getline "$cfg" $tmpfile max_connections
		conf_getline "$cfg" $tmpfile max_conntent_length
		conf_getline "$cfg" $tmpfile timeout

		conf_getline "$cfg" $tmpfile ssl 0 1
		if [ "$value" -eq 1 ]; then
			conf_getline "$cfg" $tmpfile certificate
			conf_getline "$cfg" $tmpfile key
			conf_getline "$cfg" $tmpfile certificate_authority
			conf_getline "$cfg" $tmpfile protocol
			conf_getline "$cfg" $tmpfile ciphers
		fi

		conf_getline "$cfg" $tmpfile dns_lookup 1 1
		conf_getline "$cfg" $tmpfile realm
		;;
	encoding)
		conf_getline "$cfg" $tmpfile request
		conf_getline "$cfg" $tmpfile stock
		;;
	auth)
		conf_getline "$cfg" $tmpfile "type" htpasswd
		if [ "$value" = "htpasswd" ]; then
			conf_getline "$cfg" $tmpfile htpasswd_filename $CFGDIR/users
			conf_getline "$cfg" "$tmpfile" htpasswd_encryption plain
		fi

		conf_getline "$cfg" "$tmpfile" delay
		;;
	rights)
		conf_getline "$cfg" "$tmpfile" "type"
		if [ "$value" = "from_file" ]; then
			conf_getline "$cfg" "$tmpfile" "file"
		fi
		;;
	storage)
		conf_getline "$cfg" $tmpfile filesystem_folder "$DATADIR"
		DATADIR="$value"
		conf_getline "$cfg" $tmpfile filesystem_locking 1 1
		conf_getline "$cfg" $tmpfile max_sync_token_age
		conf_getline "$cfg" $tmpfile filesystem_close_lock_file 0 1
		conf_getline "$cfg" $tmpfile hook
		;;
	web)
		conf_getline "$cfg" $tmpfile "type"
		;;
	logging)
		conf_getline "$cfg" "$tmpfile" config
		conf_getline "$cfg" "$tmpfile" debug 0 1
		conf_getline "$cfg" "$tmpfile" full_environment 0 1
		conf_getline "$cfg" "$tmpfile" mask_passwords 1 1
		;;
	headers)
		config_get "$cfg" "$tmpfile" cors
		if [ -n "$cors" ]; then
			echo "Access-Control-Allow-Origin = $cors" >>$tmpfile
		fi
		;;
	esac

	echo "
" >>$cfgfile
}

add_missing_sections() {
	local cfgfile="$1"

	for section in server encoding auth rights storage web logging headers; do
		if [ "$section" = "server" ]; then
			grep -q "\[$section\]" $cfgfile || echo "
[$section]
hosts = 0.0.0.0:5232, [::]:5232

" >>$cfgfile
		elif [ "$section" = "auth" ]; then
			grep -q "\[$section\]" $cfgfile || echo "
[$section]
type = htpasswd
htpasswd_filename = $CFGDIR/users
htpasswd_encryption = plain

" >>$cfgfile
		elif [ "$section" = "storage" ]; then
			grep -q "\[$section\]" $cfgfile || echo "
[$section]
filesystem_folder = $DATADIR

" >>$cfgfile
		else
			grep -q "\[$section\]" $cfgfile || echo "
[$section]

" >>$cfgfile
		fi
	done
}

add_user() {
	local cfg="$1"
	local tmpfile="$2"
	local name password

	config_get name "$cfg" name
	config_get password "$cfg" password

	[ -n "$name" ] && echo "$name:$password" >>$tmpfile
}

build_users() {
	local tmpfile="$1"

	# temporary config file
	# radicale2 needs read access
	chmod 0640 $tmpfile

	config_foreach add_user user "$tmpfile"
}

build_config() {
	local tmpfile=$(mktemp)
	local tmpfile2=$(mktemp)

	# temporary config file
	# radicale2 need read access
	chmod 0640 $tmpfile

	config_load radicale2
	config_foreach conf_section section $tmpfile
	add_missing_sections $tmpfile

	build_users $tmpfile2

	# move tmp to final
	mkdir -m0750 -p $CFGDIR
	cat $tmpfile >$SYSCFG
	rm -f $tmpfile
	cat $tmpfile2 >$USRCFG
	rm -f $tmpfile2
}

set_permission() {
	# config file permissions (read access for group)
	chmod 0750 $CFGDIR
	chmod 0640 $SYSCFG
	chmod 0640 $USRCFG
	chgrp -R radicale2 $CFGDIR
	# data directory does not exist
	[ -d $DATADIR ] || {
		logger -p user.error -t "radicale2[----]" "Data directory '$DATADIR' does not exist. Startup failed !!!"
	}
}


interface_triggers() {
	local action="$1"
	local triggerlist trigger

	config_load radicale2
	config_get triggerlist server triggerlist

	. /lib/functions/network.sh

	if [ -n "$triggerlist" ]; then
		for trigger in $triggerlist; do
			if [ "$action" = "add_trigger" ]; then
				procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/radicale2 reload
			else
				network_is_up "$trigger" && return 0
			fi
		done
	else
		if [ "$action" = "add_trigger" ]; then
			procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/radicale2 reload
		else
			ubus call network.device status | grep -q '"up": true' && return 0
		fi
	fi
	[ "$action" = "add_trigger" ] || return 1
}

start_service() {
	local haveinterface

	if [ ! -r /etc/radicale2/config ]; then
		build_config
		set_permission
	fi

	interface_triggers "check_interface_up" || return

	procd_open_instance "radicale2"
	procd_set_param respawn
	procd_set_param stderr 1
	procd_set_param stdout 1
	if [ ! -r /etc/radicale2/config ]; then
		procd_set_param command /usr/bin/radicale2 --config="$SYSCFG"
	else
		procd_set_param command /usr/bin/radicale2 --config="/etc/radicale2/config"
	fi
	procd_set_param user radicale2
	procd_close_instance

	return 0
}

service_triggers() {
	interface_triggers "add_trigger"
	procd_add_reload_trigger "radicale2"
}