Re: Problema en script de inicio de 'stunnel4'
2014-08-04 19:40 GMT+02:00 F. J. Blanco <security.debian@gmail.com>:
> Buenas tardes.
>
> Me he encontrado con un error en uno de mis servidores y por muchas
> vueltas que le dé a la cabeza no consigo encontrar el error.
>
> El problema es con el paquete 'stunnel4', concretamente con el
> script de inicio (start/stop/restart/reload):
>
> ----------------------------------------------------------------------------
> cat /etc/init.d/stunnel4
>
> #! /bin/sh -e
> ### BEGIN INIT INFO
> # Provides: stunnel4
> # Required-Start: $local_fs $remote_fs
> # Required-Stop: $local_fs $remote_fs
> # Should-Start: $syslog
> # Should-Stop: $syslog
> # Default-Start: 2 3 4 5
> # Default-Stop: 0 1 6
> # Short-Description: Start or stop stunnel 4.x (SSL tunnel for network daemons)
> # Description: Starts or stops all configured SSL network
> tunnels. Each *.conf file in
> # /etc/stunnel/ will spawn a separate stunnel
> process. The list of files
> # can be overriden in /etc/default/stunnel, and
> that same file can be used
> # to completely disable *all* tunnels.
> ### END INIT INFO
>
> DEFAULTPIDFILE="var/run/stunnel4.pid"
> DAEMON=/usr/bin/stunnel4
> NAME=stunnel
> DESC="SSL tunnels"
> OPTIONS=""
> ENABLED=0
>
> get_pids() {
> local file=$1
> if test -f $file; then
> CHROOT=`grep "^chroot" $file|sed "s;.*= *;;"`
> PIDFILE=`grep "^pid" $file|sed "s;.*= *;;"`
> if [ "$PIDFILE" = "" ]; then
> PIDFILE=$DEFAULTPIDFILE
> fi
> if test -f $CHROOT/$PIDFILE; then
> cat $CHROOT/$PIDFILE
> fi
> fi
> }
>
> startdaemons() {
> if ! [ -d /var/run/stunnel4 ]; then
> rm -rf /var/run/stunnel4
> install -d -o stunnel4 -g stunnel4 /var/run/stunnel4
> fi
> for file in $FILES; do
> if test -f $file; then
> ARGS="$file $OPTIONS"
> PROCLIST=`get_pids $file`
> if [ "$PROCLIST" ] && kill -s 0 $PROCLIST 2>/dev/null; then
> echo -n "[Already running: $file] "
>
> elif $DAEMON $ARGS; then
> echo -n "[Started: $file] "
> else
> echo "[Failed: $file]"
> echo "You should check that you have specified the pid= in you
> configuration file"
> exit 1
> fi
> fi
> done;
> }
>
> killdaemons()
> {
> SIGNAL=${1:-TERM}
> for file in $FILES; do
> PROCLIST=`get_pids $file`
> if [ "$PROCLIST" ] && kill -s 0 $PROCLIST 2>/dev/null; then
> kill -s $SIGNAL $PROCLIST
> echo -n "[stopped: $file] "
> fi
> done
> }
>
> if [ "x$OPTIONS" != "x" ]; then
> OPTIONS="-- $OPTIONS"
> fi
>
> test -f /etc/default/stunnel4 && . /etc/default/stunnel4
> if [ "$ENABLED" = "0" ] ; then
> echo "$DESC disabled, see /etc/default/stunnel4"
> exit 0
> fi
>
> # If the user want to manage a single tunnel, the conf file's name
> # is in $2. Otherwise, respect /etc/default/stunnel4 setting. If no
> # setting there, use /etc/stunnel/*.conf
> if [ -n "${2:-}" ]; then
> if [ -e "/etc/stunnel/$2.conf" ]; then
> FILES="/etc/stunnel/$2.conf"
> else
> echo >&2 "/etc/stunnel/$2.conf does not exist."
> exit 1
> fi
> else
> if [ -z "$FILES" ]; then
> FILES="/etc/stunnel/*.conf"
> fi
> fi
>
> test -x $DAEMON || exit 0
>
>
> set -e
>
> case "$1" in
> start)
> echo -n "Starting $DESC: "
> startdaemons
> echo "$NAME."
> ;;
> stop)
> echo -n "Stopping $DESC: "
> killdaemons
> echo "$NAME."
> ;;
> reopen-logs)
> echo -n "Reopening log files $DESC: "
> killdaemons USR1
> echo "$NAME."
> ;;
> force-reload|reload)
> echo -n "Reloading configuration $DESC: "
> killdaemons HUP
> echo "$NAME."
> ;;
> restart)
> echo -n "Restarting $DESC: "
> killdaemons
> sleep 5
> startdaemons
> echo "$NAME."
> ;;
> *)
> N=/etc/init.d/$NAME
> echo "Usage: $N {start|stop|reload|reopen-logs|restart}
> [<stunnel instance>]" >&2
> exit 1
> ;;
> esac
>
> exit 0
> ----------------------------------------------------------------------------
>
> En resumidas cuentas, por lo que alcanzo a entender, el script:
>
> - Busca todos los archivos de configuración (.conf) ubicados en
> /etc/stunnel4/, y por cada uno de ellos, extrae unos parámetros
> (CHROOT y PIDFILE). Si éstos existen, usa la función get_pids() para
> obtener el pid del proceso stunnel. En caso contrario, devuelve un
> valor vacío.
>
> - Si se pide un 'START', comprueba si existe un pid activo y no es
> así, lo levanta.
>
> - Si se pide un 'STOP', lo contrario, etc.
>
> El asunto es que, cada vez que lanzo un /etc/init.d/stunnel4
> (start/stop/restart), me aparece el error:
>
> Starting SSL tunnels : [started: test: 32: /var/lib/stunnel4]
> unexpected operator
>
> Tras comprobar los logs me consta que el script lee correctamente
> mi fichero de configuración '/etc/stunnel4/stunnel.conf'.
>
> He 'destripado' el script y he podido ratificar que, extrayendo
> línea a línea y probándolo en la shell, todo funciona como debería
> (toma correctamente los valores 'chroot = /var/lib/stunnel4/' y 'pid =
> /stunnel.pid' del fichero de configuración). En cambio, si utilizo el
> script tal cual, no funciona.
>
> Como ejemplo, he probado un pequeño 'hack' en el que he modificado
> la función get_pids(), evitando las comprobaciones y forzando los
> valores de este modo:
>
> get_pids() {
> # local file=$1
> # if test -f $file; then
> # CHROOT=`grep "^chroot" $file|sed "s;.*= *;;"`
> # PIDFILE=`grep "^pid" $file|sed "s;.*= *;;"`
> # if [ "$PIDFILE" = "" ]; then
> # PIDFILE=$DEFAULTPIDFILE
> # fi
> # if test -f $CHROOT/$PIDFILE; then
> # cat $CHROOT/$PIDFILE
> # fi
> # fi
> if test -f /var/lib/stunnel4/stunnel.pid; then
> cat /var/lib/stunnel4/stunnel.pid
> fi
> }
>
> Y el caso es que así, funciona.
>
> ¿Dónde puedo estar metiendo la pata?
>
> Perdón por el ladrillo, toda sugerencia será bienvenida.
>
> Saludos.
>
>
> P.D: Sé que no se trata de un bug del paquete, pues en otra máquina
> el script funciona perfectamente, con idéntica configuración.
>
>
ejecuta:
# bash -x /etc/init.d/stunnel4
Esto te permite ver la ejecución linea a linea y ver donde falla.
S2.
> --
> To UNSUBSCRIBE, email to debian-user-spanish-REQUEST@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> Archive: [🔎] CADgSyeP474DECya_-2s4tn8q68gL6ebNknSeoRHnGnNAAXNahg@mail.gmail.com">https://lists.debian.org/[🔎] CADgSyeP474DECya_-2s4tn8q68gL6ebNknSeoRHnGnNAAXNahg@mail.gmail.com
>
Reply to: