1
  5   ˆl–äY>”T†» åÀŠàžS�ÿ�þBã‚Y²ÖÝ¤`óÂƒmÖAÁÀ¿ b    #! /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 (TLS tunnel for network daemons)
# Description:       Starts or stops all configured TLS network tunnels. Each *.conf file in
#                    /etc/stunnel/ will spawn a separate stunnel process. The list of files
#                    can be overridden in /etc/default/stunnel, and that same file can be used
#                    to completely disable *all* tunnels.
### END INIT INFO

# Author / upstream maintainer note:
# With the planned introduction of a control interface (conceptually similar to
# apache2ctl), running separate processes for each *.conf will become obsolete.
# Please add "include = /etc/stunnel/conf.d" to stunnel.conf instead.

. /lib/lsb/init-functions

DEFAULTPIDFILE="/var/run/stunnel4.pid"
DAEMON=/usr/bin/stunnel4
NAME=stunnel
DESC="TLS tunnels"
OPTIONS=""

get_opt() {
  sed -e "s;^[[:space:]]*;;" -e "s;[[:space:]]*$;;" \
    -e "s;[[:space:]]*=[[:space:]]*;=;" "$1" |
    grep -i "^$2=" | sed -e "s;^[^=]*=;;"
}

get_pidfile() {
  local file=$1
  if [ -f $file ]; then
    CHROOT=`get_opt $file chroot`
    PIDFILE=`get_opt $file pid`
    if [ "$PIDFILE" = "" ]; then
      PIDFILE=$DEFAULTPIDFILE
    fi
    echo "$CHROOT/$PIDFILE"
  fi
}

startdaemons() {
  local res file args pidfile warn status

  if ! [ -d /var/run/stunnel4 ]; then
    rm -rf /var/run/stunnel4
    install -d -o stunnel4 -g stunnel4 /var/run/stunnel4
  fi
  if [ -n "$RLIMITS" ]; then
    ulimit $RLIMITS
  fi
  res=0
  for file in $FILES; do
    if [ -f $file ]; then
      echo -n " $file: "
      args="$file $OPTIONS"
      pidfile=`get_pidfile $file`
      if egrep -qe '^pid[[:space:]]*=' "$file"; then
        warn=''
      else
        warn=' (no pid=pidfile specified!)'
      fi
      status=0
      start_daemon -p "$pidfile" "$DAEMON" $args || status=$?
      if [ "$status" -eq 0 ]; then
        echo -n "started$warn"
      else
        echo "failed$warn"
        echo "You should check that you have specified the pid= in you configuration file"
        res=1
      fi
    fi
  done;
  echo ''
  return "$res"
}

killdaemons()
{
  local sig file pidfile status

  sig=$1
  res=0
  for file in $FILES; do
    echo -n " $file: "
    pidfile=`get_pidfile $file`
    if [ ! -e "$pidfile" ]; then
      echo -n "no pid file"
    else
      status=0
      killproc -p "$pidfile" "$DAEMON" ${sig:+"$sig"} || status=$?
      if [ "$status" -eq 0 ]; then
        echo -n 'stopped'
      else
        echo -n 'failed'
        res=1
      fi
    fi
  done
  echo ''
  return "$res"
}

querydaemons()
{
  local res file pidfile status

  res=0
  for file in $FILES; do
    echo -n " $file: "
    pidfile=`get_pidfile "$file"`
    if [ ! -e "$pidfile" ]; then
      echo -n 'no pid file'
      res=1
    else
      status=0
      pidofproc -p "$pidfile" "$DAEMON" >/dev/null || status="$?"
      if [ "$status" = 0 ]; then
        echo -n 'running'
      elif [ "$status" = 4 ]; then
        echo "cannot access the pid file $pidfile"
        res=1
      else
        echo -n 'stopped'
        res=1
      fi
    fi
  done
  echo ''
  exit "$res"
}

restartrunningdaemons()
{
  local res file pidfile status args

  res=0
  for file in $FILES; do
    echo -n " $file: "
    pidfile=`get_pidfile "$file"`
    if [ ! -e "$pidfile" ]; then
      echo -n 'no pid file'
    else
      status=0
      pidofproc -p "$pidfile" "$DAEMON" >/dev/null || status="$?"
      if [ "$status" = 0 ]; then
        echo -n 'stopping'
        killproc -p "$pidfile" "$DAEMON" "$sig" || status="$?"
        if [ "$status" -eq 0 ]; then
          echo -n ' starting'
          args="$file $OPTIONS"
          start_daemon -p "$pidfile" "$DAEMON" $args || status="$?"
          if [ "$status" -eq 0 ]; then
            echo