summaryrefslogtreecommitdiff
path: root/user/netqmail/netqmail.initd
blob: 36e43e74ebcc806be88631aa817efc1ed99890d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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 $?
}