mirror of
https://dev.iopsys.eu/feed/iopsys.git
synced 2025-12-10 07:44:50 +01:00
203 lines
4.5 KiB
Bash
203 lines
4.5 KiB
Bash
#!/bin/sh /etc/rc.common
|
|
|
|
. /lib/functions/network.sh
|
|
|
|
START=90
|
|
STOP=10
|
|
|
|
USE_PROCD=1
|
|
PROG=/usr/bin/owsd
|
|
CONFIGFILE="/etc/config/owsd"
|
|
|
|
DHCP_DOMAINS=""
|
|
|
|
load_dhcp_domains() {
|
|
|
|
append_domain() {
|
|
local domain=$1
|
|
DHCP_DOMAINS="$DHCP_DOMAINS $domain"
|
|
}
|
|
|
|
dhcp_domain_section() {
|
|
local section=$1
|
|
local ip
|
|
config_get ip "$section" ip
|
|
[ -z "$ip" ] && config_list_foreach "$section" "name" append_domain
|
|
}
|
|
|
|
config_load dhcp # note: do not overload a config while parsing it
|
|
config_foreach dhcp_domain_section "domain"
|
|
}
|
|
|
|
validate_owsd() {
|
|
uci_validate_section "owsd" "owsd" "global" \
|
|
'sock:string' \
|
|
'redirect:string' \
|
|
'www:string' \
|
|
'ubusproxy:list(string)' \
|
|
'client_key:file' \
|
|
'client_cert:file' \
|
|
'client_ca:file' \
|
|
|
|
}
|
|
|
|
append_origin() {
|
|
procd_append_param command -o"$1"
|
|
}
|
|
|
|
append_origin_parts() {
|
|
local proto host port
|
|
proto="$1"
|
|
host="$2"
|
|
port="$3"
|
|
|
|
if [ "${proto}" = "https" -a "${port}" -eq 443 -o "${proto}" = "http" -a "${port}" -eq 80 ]; then
|
|
append_origin "${proto}://${host}"
|
|
else
|
|
append_origin "${proto}://${host}:${port}"
|
|
fi
|
|
}
|
|
|
|
validate_owsd_iface() {
|
|
uci_validate_section "owsd" "owsd-listen" "$1" \
|
|
'port:port' \
|
|
'interface:network' \
|
|
'origin:list(string)' \
|
|
'whitelist_interface_as_origin:bool:0' \
|
|
'whitelist_dhcp_domains:bool:0' \
|
|
'ipv6:bool:1' \
|
|
'ipv6only:bool:0' \
|
|
'cert:file' \
|
|
'key:file' \
|
|
'ca:file' \
|
|
'restrict_to_user:list(string)' \
|
|
&&
|
|
[ -n "${port}" ]
|
|
}
|
|
|
|
parse_owsd_iface() {
|
|
local port interface whitelist_interface_as_origin whitelist_dhcp_domains ipv6 ipv6only
|
|
local cert key ca
|
|
local restrict_to_user
|
|
|
|
validate_owsd_iface "$1" || {
|
|
echo "Validation failed"
|
|
return 1
|
|
}
|
|
|
|
# utility function
|
|
new_listen_socket() {
|
|
procd_append_param command -p "${port}"
|
|
|
|
procd_append_param command -L"$1"
|
|
|
|
[ -n "${cert}" ] && procd_append_param command -c"${cert}"
|
|
[ -n "${key}" ] && procd_append_param command -k"${key}"
|
|
[ -n "${ca}" ] && procd_append_param command -a"${ca}"
|
|
|
|
[ -n "${restrict_to_user}" ] && procd_append_param command -u"${restrict_to_user}"
|
|
|
|
[ -n "$2" ] && procd_append_param command -i"$2"
|
|
}
|
|
|
|
append_whitelists () {
|
|
config_list_foreach "$1" "origin" append_origin
|
|
|
|
if [ "$whitelist_dhcp_domains" -eq 1 ]; then
|
|
for domain in $DHCP_DOMAINS; do
|
|
append_origin_parts "${http}" "${domain}" "${port}"
|
|
done
|
|
fi
|
|
|
|
if [ -n "${interface}" -a -n "${addr}" -a "${whitelist_interface_as_origin}" -eq 1 ]; then
|
|
append_origin_parts "${http}" "${addr}" "${port}"
|
|
fi
|
|
}
|
|
|
|
local http="http${cert:+s}"
|
|
local ip4addrs ip6addrs
|
|
|
|
# bind to some network
|
|
if [ -n "${interface}" ]; then
|
|
# 1 listen-socket (vhost) for each IP address on that network's iface
|
|
|
|
# ipv4 addresses
|
|
if [ "${ipv6only}" -eq 0 ]; then
|
|
network_get_ipaddrs ip4addrs "${interface}";
|
|
fi
|
|
for addr in ${ip4addrs}; do
|
|
new_listen_socket "$1" "${addr}"
|
|
append_whitelists "$1"
|
|
done
|
|
|
|
# ipv6 addresses
|
|
if [ "${ipv6}" -eq 1 ]; then
|
|
network_get_ipaddrs6 ip6addrs "${interface}"
|
|
fi
|
|
for addr in ${ip6addrs}; do
|
|
new_listen_socket "$1" "${addr}"
|
|
addr="\\[${addr}]"
|
|
append_whitelists "$1"
|
|
procd_append_param command -66
|
|
done
|
|
else
|
|
new_listen_socket "$1"
|
|
if [ "${ipv6}" -eq 1 ]; then procd_append_param command -6; fi
|
|
if [ "${ipv6}" -eq 1 -a "${ipv6only}" -eq 1 ]; then procd_append_param command -6; fi
|
|
|
|
append_whitelists "$1"
|
|
fi
|
|
}
|
|
|
|
start_service() {
|
|
|
|
# preload dhcp domains list, in case any interface config requires it
|
|
load_dhcp_domains
|
|
|
|
config_load owsd # note: do not overload a config while parsing it
|
|
procd_open_instance
|
|
procd_set_param command $PROG
|
|
|
|
local sock www redirect
|
|
local client_cert client_key client_ca
|
|
|
|
validate_owsd || {
|
|
echo "Global validation failed"
|
|
return 1
|
|
}
|
|
|
|
[ -n "${sock}" ] && procd_append_param command -s"${sock}"
|
|
[ -n "${www}" ] && procd_append_param command -w"${www}"
|
|
[ -n "${redirect}" ] && procd_append_param command -r"${redirect}"
|
|
|
|
[ -n "${client_cert}" ] && procd_append_param command -C"${client_cert}"
|
|
[ -n "${client_key}" ] && procd_append_param command -K"${client_key}"
|
|
[ -n "${client_ca}" ] && procd_append_param command -A"${client_ca}"
|
|
|
|
append_ubusproxy () {
|
|
[ -n "$1" ] && procd_append_param command -P"$1"
|
|
}
|
|
|
|
config_list_foreach "global" "ubusproxy" append_ubusproxy
|
|
|
|
config_foreach parse_owsd_iface "owsd-listen"
|
|
|
|
# procd_set_param stderr 1
|
|
procd_set_param respawn
|
|
|
|
procd_close_instance
|
|
}
|
|
|
|
stop_service()
|
|
{
|
|
service_stop ${PROG}
|
|
}
|
|
|
|
reload_service() {
|
|
stop
|
|
start
|
|
}
|
|
|
|
service_triggers() {
|
|
procd_add_reload_trigger owsd
|
|
}
|