Hi everybody,
I would like to create an UCI config and an init.d script for the SSH/SSL multiplexer sslh.
The structure of the UCI config itself is easy:
/etc/config/sslh
config 'sslh' 'default'
# disable or enable start of sslh
option 'enable' '1'
# pid file is OBLIGATORY, defaults to /var/run/sslh.pid
# -P pidfile
option 'pidfile' '/var/run/sslh.pid'
# listen defaults to 0.0.0.0:443 (all interfaces)
# -p <listenaddr>:<listenport>
option 'listenaddr' ''
option 'listenport' ''
# ssh defaults to localhost:22
# -s <sshhost>:<sshport>
option 'sshhost' ''
option 'sshport' ''
# ssl defaults to localhost:442
# -l <sslhost>:<sslport>
option 'sslhost' ''
option 'sslport' ''
# timeout (for ssh, then ssl is assumed) defaults to 2
# -t
option 'timeout' ''
# verbose defaults to off
# -v
option 'verbose' '0'
But I don't know how to read the configuration, check for obligatory values, etc. in the init.d script.
/etc/init.d/sslh (this will be updated with the progress of this thread, please read the thread for answers to your questions)
#!/bin/sh /etc/rc.common
# Copyright (C) 2009 OpenWrt.org
START=95
start()
{
local RC=0
## load config into variables
uci_load 'sslh'
## check parameters
# A) pid file is mandatory
if [ -z ${CONFIG_default_pidfile} ]
then
echo 'sslh: pidfile not stated, but mandatory (default is /var/run/sslh.pid)'
RC=1
fi
# B) host and port are mandatory if one of them is stated
local failed
# B1) listen
failed=0
[ ! -z ${CONFIG_default_listenaddr} ] && [ -z ${CONFIG_default_listenport} ] && failed=1
[ -z ${CONFIG_default_listenaddr} ] && [ ! -z ${CONFIG_default_listenport} ] && failed=1
if [ ${failed} -eq 1 ]
then
echo 'sslh: listen address and port must be stated'
RC=1
fi
# B2) ssh
failed=0
[ ! -z ${CONFIG_default_sshhost} ] && [ -z ${CONFIG_default_sshport} ] && failed=1
[ -z ${CONFIG_default_sshhost} ] && [ ! -z ${CONFIG_default_sshport} ] && failed=1
if [ ${failed} -eq 1 ]
then
echo 'sslh: ssh host and port must be stated'
RC=1
fi
# B3) ssl
failed=0
[ ! -z ${CONFIG_default_sslhost} ] && [ -z ${CONFIG_default_sslport} ] && failed=1
[ -z ${CONFIG_default_sslhost} ] && [ ! -z ${CONFIG_default_sslport} ] && failed=1
if [ ${failed} -eq 1 ]
then
echo 'sslh: ssl host and port must be stated'
RC=1
fi
## check if sslh is already running with this pid file
if [ ! -z ${CONFIG_default_pidfile} ]
then
start-stop-daemon -K -t -q -p ${CONFIG_default_pidfile} -n sslh
if [ $? -eq 0 ]
then
echo "sslh: already running with pidfile ${CONFIG_default_pidfile}"
RC=1
fi
fi
## leave if any check failed
[ ${RC} -ne 0 ] && return ${RC}
## check if sslh is enabled
local enabled=0
config_get_bool enabled 'default' 'enable' 0
if [ ${enabled} -eq 0 ]
then
echo 'sslh is not enabled'
return 1
fi
## prepare parameters (initialise with pid file)
local SSLHARGS="-P ${CONFIG_default_pidfile}"
#
local option
local added
# A) listen parameter
option='-p'
added=0
if [ ! -z ${CONFIG_default_listenaddr} ]
then
SSLHARGS="${SSLHARGS} ${option} "
SSLHARGS="${SSLHARGS}${CONFIG_default_listenaddr}"
added=1
fi
if [ ! -z ${CONFIG_default_listenport} ]
then
[ ${added} -eq 0 ] && SSLHARGS="${SSLHARGS} ${option} "
SSLHARGS="${SSLHARGS}:${CONFIG_default_listenport}"
fi
# B) ssh parameter
option='-s'
added=0
if [ ! -z ${CONFIG_default_sshhost} ]
then
SSLHARGS="${SSLHARGS} ${option} "
SSLHARGS="${SSLHARGS}${CONFIG_default_sshhost}"
added=1
fi
if [ ! -z ${CONFIG_default_sshport} ]
then
[ ${added} -eq 0 ] && SSLHARGS="${SSLHARGS} ${option} "
SSLHARGS="${SSLHARGS}:${CONFIG_default_sshport}"
fi
# C) ssl parameter
option='-l'
added=0
if [ ! -z ${CONFIG_default_sslhost} ]
then
SSLHARGS="${SSLHARGS} ${option} "
SSLHARGS="${SSLHARGS}${CONFIG_default_sslhost}"
added=1
fi
if [ ! -z ${CONFIG_default_sslport} ]
then
[ ${added} -eq 0 ] && SSLHARGS="${SSLHARGS} ${option} "
SSLHARGS="${SSLHARGS}:${CONFIG_default_sslport}"
fi
# D) timeout (for ssh, then ssl is assumed)
if [ ! -z ${CONFIG_default_timeout} ]
then
SSLHARGS="${SSLHARGS} -t ${CONFIG_default_timeout}"
fi
# E) verbose parameter
local verbosed=0
config_get_bool verbosed 'default' 'verbose' 0
if [ ${verbosed} -eq 1 ]
then
SSLHARGS="${SSLHARGS} -v"
fi
#
if [ ${verbosed} -eq 1 ]
then
echo "Starting sslh ${SSLHARGS}"
fi
## execute command and return its exit code
sslh ${SSLHARGS}
RC=$?
return ${RC}
};
stop()
{
local RC=0
## load config into variables
uci_load 'sslh'
## check parameters
# pid file is mandatory
if [ -z ${CONFIG_default_pidfile} ]
then
echo 'sslh: pidfile not stated, but mandatory (default is /var/run/sslh.pid)'
RC=1
fi
## execute command and return its exit code
start-stop-daemon -K -q -p ${CONFIG_default_pidfile} -n sslh
RC=$?
if [ ${RC} -eq 0 ]
then
rm -f ${CONFIG_default_pidfile}
fi
return ${RC}
};
Can somebody help or link pages that explain how to use uci config inside init.d scripts?
All help is appreciated
Maddes
(Last edited by maddes.b on 11 Jul 2009, 16:54)