summaryrefslogblamecommitdiff
path: root/user/netqmail/netqmail.initd
blob: 36e43e74ebcc806be88631aa817efc1ed99890d7 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                     

                                                                         

                          
                                                                           





                          
                     
                               
                             





                               
                                                                
                             

                                         













                                                         
                                  

         
                      




                                           






















                                                                    




                                                     






















                                                           

                        
                                                                   
                         

           

         

                                  


                                                          



                               
#!/sbin/openrc-run

description="Start/stop the s6 services for netqmail"

depend()
{
  after net
  after localmount
}

makesmtpd()
{
  set -e
  ip="$1"
  ext="$2"
  if s6-tcpserver6-socketbinder -dBb0 -- "$ip" 25 true 2>/dev/null ; then
    ipv=6
    relayfor="$smtprelay6"
  elif s6-tcpserver4-socketbinder -dBb0 -- "$ip" 25 true 2>/dev/null ; then
    ipv=4
    relayfor="$smtprelay4"
  else
    return 1
  fi

  service="smtpd-$ip"
  sdir=/run/services/"$service"
  logdir=/var/log/"smtpd-$ip"

  mkdir -p -m 0755 "$logdir"
  chown qmaill:qmaill "$logdir"
  chmod 2700 "$logdir"

  rm -rf "$sdir"
  cp -a /etc/qmail/services/smtpd"${ipv}"-skeleton"$ext" "$sdir"
  echo "$ip" > "$sdir"/env/IP
  echo "$KEYFILE" > "$sdir"/env/KEYFILE
  echo "$CERTFILE" > "$sdir"/env/CERTFILE

  for j in $relayfor ; do
    mkdir -p -m 0755 "$sdir/data/rules/ip${ipv}/$j/env"
    touch "$sdir/data/rules/ip${ipv}/$j/allow"
    echo > "$sdir/data/rules/ip${ipv}/$j/env/RELAYCLIENT"
  done

  return 0
}

stop()
{
  set -e
  . /etc/conf.d/netqmail
  ebegin "Stopping SMTP listeners"
  dirs=""
  logs=""
  for i in ${IPS} ; do
    rm -f "/run/service/smtpd-$i"
    s6-svc -d "/run/services/smtpd-$i"
    dirs="$dirs /run/services/smtpd-$i"
    logs="$logs /run/services/smtpd-$i/log"
  done
  s6-svwait -D $dirs
  eend $?

  ebegin "Stopping the qmail-send service (this may take some time)"
  rm -f /run/service/qmail
  logs="$logs /var/qmail/services/qmail/log"
  s6-svc -dwD /var/qmail/services/qmail
  eend $?

  ebegin "Stopping loggers and cleaning up qmail services"
  for i in $logs ; do
    s6-svc -d "$i"
  done
  s6-svwait -D $logs
  s6-svscanctl -aN /run/service
  rm -rf $dirs
  eend $?
}

start()
{
  set -e
  . /etc/conf.d/netqmail
  if test -r "$KEYFILE" && test -r "$CERTFILE" ; then
    ext=""
  else
    ext="-notls"
  fi

  ebegin "Parsing the relay subnets"
  smtprelay4=
  smtprelay6=
  for i in ${RELAY_SUBNETS} ; do
    j=`echo "$i" | tr / _`
    if echo "$i" | grep -qF : ; then
      smtprelay6="$smtprelay6 $j"
    elif echo "$i" | grep -qF . ; then
      smtprelay4="$smtprelay4 $j"
    else
      false
      break
    fi
  done
  eend $?

  ebegin "Starting the qmail-send service"
  ln -nsf ../../var/qmail/services/qmail /run/service/qmail
  s6-svscanctl -a /run/service
  s6-svwait -U /var/qmail/services/qmail
  eend $?

  mkdir -p /run/services
  for i in ${IPS} ; do
    ebegin "Creating a service directory for a SMTP listener on $i"
    makesmtpd "$i" "$ext"
    eend $?
  done

  dirs=""
  ebegin "Starting SMTP listeners"
  for i in ${IPS} ; do
    ln -nsf "../services/smtpd-$i" "/run/service/smtpd-$i"
    dirs="$dirs /run/service/smtpd-$i"
  done
  s6-svscanctl -aN /run/service
  s6-svwait -U $dirs
  eend $?
}