x11-misc/alock: Take ownership of package
[gentoo.git] / eclass / qmail.eclass
1 # Copyright 1999-2012 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: qmail.eclass
6 # @MAINTAINER:
7 # qmail-bugs@gentoo.org
8 # @BLURB: common qmail functions
9
10 inherit flag-o-matic toolchain-funcs fixheadtails user
11
12 # hardcoded paths
13 QMAIL_HOME="/var/qmail"
14 TCPRULES_DIR="/etc/tcprules.d"
15 SUPERVISE_DIR="/var/qmail/supervise"
16
17 # source files and directories
18 GENQMAIL_F=genqmail-${GENQMAIL_PV}.tar.bz2
19 GENQMAIL_S="${WORKDIR}"/genqmail-${GENQMAIL_PV}
20
21 QMAIL_SPP_F=qmail-spp-${QMAIL_SPP_PV}.tar.gz
22 QMAIL_SPP_S="${WORKDIR}"/qmail-spp-${QMAIL_SPP_PV}
23
24 # @FUNCTION: primes
25 # @USAGE: <min> <max>
26 # @DESCRIPTION:
27 # Prints a list of primes between min and max inclusive
28 # Note: this functions gets very slow when used with large numbers.
29 primes() {
30         local min=${1} max=${2}
31         local result= primelist=2 i p
32
33         [[ ${min} -le 2 ]] && result="${result} 2"
34
35         for ((i = 3; i <= max; i += 2))
36         do
37                 for p in ${primelist}
38                 do
39                         [[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \
40                                 break
41                 done
42                 if [[ $[i % p] != 0 ]]
43                 then
44                         primelist="${primelist} ${i}"
45                         [[ ${i} -ge ${min} ]] && \
46                                 result="${result} ${i}"
47                 fi
48         done
49
50         echo ${result}
51 }
52
53 # @FUNCTION: is_prima
54 # @USAGE: <number>
55 # @DESCRIPTION:
56 # Checks wether a number is a prime number
57 is_prime() {
58         local number=${1} i
59         for i in $(primes ${number} ${number})
60         do
61                 [[ ${i} == ${number} ]] && return 0
62         done
63         return 1
64 }
65
66 dospp() {
67         insinto "${QMAIL_HOME}"/plugins/
68         insopts -o root -g "$GROUP_ROOT" -m 0755
69         newins $1 ${2:-$(basename $1)}
70 }
71
72 # @FUNCTION: dosupervise
73 # @USAGE: dosupervise <service> [<runfile> <logfile>]
74 # @DESCRIPTION:
75 # Install runfiles for services and logging to supervise directory
76 dosupervise() {
77         local service=$1
78         local runfile=${2:-${service}} logfile=${3:-${service}-log}
79         [[ -z "${service}" ]] && die "no service given"
80
81         insopts -o root -g "$GROUP_ROOT" -m 0755
82         diropts -o root -g "$GROUP_ROOT" -m 0755
83
84         dodir ${SUPERVISE_DIR}/${service}{,/log}
85         fperms +t ${SUPERVISE_DIR}/${service}{,/log}
86
87         insinto ${SUPERVISE_DIR}/${service}
88         newins ${runfile} run
89
90         insinto ${SUPERVISE_DIR}/${service}/log
91         newins ${logfile} run
92 }
93
94 # @FUNCTION: qmail_set_cc
95 # @DESCRIPTION:
96 # The following commands patch the conf-{cc,ld} files to use the user's
97 # specified CFLAGS and LDFLAGS. These rather complex commands are needed
98 # because a user supplied patch might apply changes to these files, too.
99 # See bug #165981.
100 qmail_set_cc() {
101         local cc=$(head -n 1 ./conf-cc | sed -e "s#^g\?cc\s\+\(-O2\)\?#$(tc-getCC) #")
102         local ld=$(head -n 1 ./conf-ld | sed -e "s#^g\?cc\s\+\(-s\)\?#$(tc-getCC) #")
103
104         echo "${cc} ${CFLAGS} ${CPPFLAGS}"  > ./conf-cc || die 'Patching conf-cc failed.'
105         echo "${ld} ${LDFLAGS}" > ./conf-ld || die 'Patching conf-ld failed.'
106 }
107
108 # @FUNCTION: qmail_create_groups
109 # @DESCRIPTION:
110 # Keep qmail groups in sync across ebuilds
111 qmail_create_groups() {
112         einfo "Creating qmail groups"
113         enewgroup nofiles 200
114         enewgroup qmail 201
115 }
116
117 # @FUNCTION: qmail_create_users
118 # @DESCRIPTION:
119 # Keep qmail users in sync across ebuilds
120 qmail_create_users() {
121         qmail_create_groups
122
123         einfo "Creating qmail users"
124         enewuser alias 200 -1  "${QMAIL_HOME}"/alias 200
125         enewuser qmaild 201 -1 "${QMAIL_HOME}" 200
126         enewuser qmaill 202 -1 "${QMAIL_HOME}" 200
127         enewuser qmailp 203 -1 "${QMAIL_HOME}" 200
128         enewuser qmailq 204 -1 "${QMAIL_HOME}" 201
129         enewuser qmailr 205 -1 "${QMAIL_HOME}" 201
130         enewuser qmails 206 -1 "${QMAIL_HOME}" 201
131 }
132
133 genqmail_src_unpack() {
134         cd "${WORKDIR}"
135         [[ -n ${GENQMAIL_PV} ]] && unpack "${GENQMAIL_F}"
136 }
137
138 qmail_spp_src_unpack() {
139         cd "${WORKDIR}"
140         [[ -n ${QMAIL_SPP_PV} ]] && unpack "${QMAIL_SPP_F}"
141 }
142
143 # @FUNCTION: qmail_src_postunpack
144 # @DESCRIPTION:
145 # Unpack common config files, apply custom patches if supplied and
146 # set built configuration (CFLAGS, LDFLAGS, etc)
147 qmail_src_postunpack() {
148         cd "${S}"
149
150         qmail_set_cc
151
152         mysplit=${QMAIL_CONF_SPLIT:-23}
153         is_prime ${mysplit} || die "QMAIL_CONF_SPLIT is not a prime number."
154         einfo "Using conf-split value of ${mysplit}."
155         echo -n ${mysplit} > "${S}"/conf-split
156 }
157
158 qmail_src_compile() {
159         cd "${S}"
160         emake it man "$@" || die "make failed"
161 }
162
163 qmail_spp_src_compile() {
164         cd "${GENQMAIL_S}"/spp/
165         emake || die "make spp failed"
166 }
167
168 qmail_base_install() {
169         einfo "Setting up basic directory hierarchy"
170         diropts -o root -g qmail -m 755
171         keepdir "${QMAIL_HOME}"/{,bin,control}
172
173         einfo "Installing basic qmail software"
174         insinto "${QMAIL_HOME}"/bin
175
176         insopts -o root -g qmail -m 755
177         doins datemail elq forward maildir2mbox maildirmake \
178                 maildirwatch mailsubj pinq predate qail \
179                 qmail-{inject,qmqpc,showctl} sendmail
180
181         einfo "Adding env.d entry for qmail"
182         doenvd "${GENQMAIL_S}"/conf/99qmail
183
184         declare -F qmail_base_install_hook >/dev/null && \
185                 qmail_base_install_hook
186 }
187
188 qmail_full_install() {
189         einfo "Setting up full directory hierarchy"
190         keepdir "${QMAIL_HOME}"/users
191         diropts -o alias -g qmail -m 755
192         keepdir "${QMAIL_HOME}"/alias
193
194         einfo "Installing all qmail software"
195         insopts -o root -g qmail -m 755
196         doins bouncesaying condredirect config-fast except preline qbiff \
197                 qmail-{pop3d,qmqpd,qmtpd,qread,qstat,smtpd,tcpok,tcpto} \
198                 qreceipt qsmhook tcp-env
199
200         insopts -o root -g qmail -m 711
201         doins qmail-{clean,getpw,local,popup,pw2u,remote,rspawn,send} splogger
202
203         insopts -o root -g qmail -m 700
204         doins qmail-{lspawn,newmrh,newu,start}
205
206         insopts -o qmailq -g qmail -m 4711
207         doins qmail-queue
208
209         declare -F qmail_full_install_hook >/dev/null && \
210                 qmail_full_install_hook
211 }
212
213 qmail_config_install() {
214         einfo "Installing stock configuration files"
215         insinto "${QMAIL_HOME}"/control
216         insopts -o root -g "$GROUP_ROOT" -m 644
217         doins "${GENQMAIL_S}"/control/{conf-*,defaultdelivery}
218
219         einfo "Installing configuration sanity checker and launcher"
220         insinto "${QMAIL_HOME}"/bin
221         insopts -o root -g "$GROUP_ROOT" -m 644
222         doins "${GENQMAIL_S}"/control/qmail-config-system
223
224         declare -F qmail_config_install_hook >/dev/null && \
225                 qmail_config_install_hook
226 }
227
228 qmail_man_install() {
229         einfo "Installing manpages and documentation"
230
231         # those are tagged for section 8 but named for
232         # section 9 (which does not exist anyway)
233         for i in *.9; do
234                 mv ${i} ${i/.9/.8}
235         done
236
237         into /usr
238         doman *.[1578]
239         dodoc BLURB* CHANGES FAQ INSTALL* PIC* README* REMOVE* SECURITY \
240                 SENDMAIL SYSDEPS TEST* THANKS* THOUGHTS TODO* \
241                 UPGRADE VERSION*
242
243         declare -F qmail_man_install_hook >/dev/null && \
244                 qmail_man_install_hook
245 }
246
247 qmail_sendmail_install() {
248         einfo "Installing sendmail replacement"
249         diropts -m 755
250         dodir /usr/sbin /usr/lib
251
252         dosym "${QMAIL_HOME}"/bin/sendmail /usr/sbin/sendmail
253         dosym "${QMAIL_HOME}"/bin/sendmail /usr/lib/sendmail
254
255         declare -F qmail_sendmail_install_hook >/dev/null && \
256                 qmail_sendmail_install_hook
257 }
258
259 qmail_maildir_install() {
260         # use the correct maildirmake
261         # the courier-imap one has some extensions that are nicer
262         MAILDIRMAKE="${D}${QMAIL_HOME}/bin/maildirmake"
263         [[ -e /usr/bin/maildirmake ]] && \
264                 MAILDIRMAKE="/usr/bin/maildirmake"
265
266         einfo "Setting up the default aliases"
267         diropts -o alias -g qmail -m 700
268         "${MAILDIRMAKE}" "${D}${QMAIL_HOME}"/alias/.maildir
269         keepdir "${QMAIL_HOME}"/alias/.maildir/{cur,new,tmp}
270
271         for i in "${QMAIL_HOME}"/alias/.qmail-{mailer-daemon,postmaster,root}; do
272                 if [[ ! -f "${ROOT}${i}" ]]; then
273                         touch "${D}${i}"
274                         fowners alias:qmail "${i}"
275                 fi
276         done
277
278         einfo "Setting up default maildirs in the account skeleton"
279         diropts -o root -g "$GROUP_ROOT" -m 755
280         insinto /etc/skel
281         insopts -o root -g "$GROUP_ROOT" -m 644
282         newins "${GENQMAIL_S}"/control/defaultdelivery .qmail.sample
283         "${MAILDIRMAKE}" "${D}"/etc/skel/.maildir
284         keepdir /etc/skel/.maildir/{cur,new,tmp}
285
286         declare -F qmail_maildir_install_hook >/dev/null && \
287                 qmail_maildir_install_hook
288 }
289
290 qmail_tcprules_install() {
291         dodir "${TCPRULES_DIR}"
292         insinto "${TCPRULES_DIR}"
293         insopts -o root -g "$GROUP_ROOT" -m 0644
294         doins "${GENQMAIL_S}"/tcprules/Makefile.qmail
295         doins "${GENQMAIL_S}"/tcprules/tcp.qmail-*
296         use ssl || rm -f "${D}${TCPRULES_DIR}"/tcp.qmail-pop3sd
297 }
298
299 qmail_supervise_install() {
300         einfo "Installing supervise scripts"
301
302         cd "${GENQMAIL_S}"/supervise
303
304         for i in qmail-{send,smtpd,qmtpd,qmqpd,pop3d}; do
305                 dosupervise ${i}
306                 diropts -o qmaill -g "$GROUP_ROOT" -m 755
307                 keepdir /var/log/qmail/${i}
308         done
309
310         if use ssl; then
311                 dosupervise qmail-pop3sd
312                 diropts -o qmaill -g "$GROUP_ROOT" -m 755
313                 keepdir /var/log/qmail/qmail-pop3sd
314         fi
315
316         declare -F qmail_supervise_install_hook >/dev/null && \
317                 qmail_supervise_install_hook
318 }
319
320 qmail_spp_install() {
321         einfo "Installing qmail-spp configuration files"
322         insinto "${QMAIL_HOME}"/control/
323         insopts -o root -g "$GROUP_ROOT" -m 0644
324         doins "${GENQMAIL_S}"/spp/smtpplugins
325
326         einfo "Installing qmail-spp plugins"
327         keepdir "${QMAIL_HOME}"/plugins/
328         for i in authlog mfdnscheck ifauthnext tarpit; do
329                 dospp "${GENQMAIL_S}"/spp/${i}
330         done
331
332         declare -F qmail_spp_install_hook >/dev/null && \
333                 qmail_spp_install_hook
334 }
335
336 qmail_ssl_install() {
337         use gencertdaily && \
338                 CRON_FOLDER=cron.daily || \
339                 CRON_FOLDER=cron.hourly
340
341         einfo "Installing SSL Certificate creation script"
342         insinto "${QMAIL_HOME}"/control
343         insopts -o root -g "$GROUP_ROOT" -m 0644
344         doins "${GENQMAIL_S}"/ssl/servercert.cnf
345
346         insinto "${QMAIL_HOME}"/bin
347         insopts -o root -g "$GROUP_ROOT" -m 0755
348         doins "${GENQMAIL_S}"/ssl/mkservercert
349
350         einfo "Installing RSA key generation cronjob"
351         insinto /etc/${CRON_FOLDER}
352         insopts -o root -g "$GROUP_ROOT" -m 0755
353         doins "${GENQMAIL_S}"/ssl/qmail-genrsacert.sh
354
355         keepdir "${QMAIL_HOME}"/control/tlshosts
356
357         declare -F qmail_ssl_install_hook >/dev/null && \
358                 qmail_ssl_install_hook
359 }
360
361 qmail_src_install() {
362         export GROUP_ROOT="$(id -gn root)"
363         qmail_base_install
364         qmail_full_install
365         qmail_config_install
366         qmail_man_install
367         qmail_sendmail_install
368         qmail_maildir_install
369         qmail_tcprules_install
370         qmail_supervise_install
371
372         use qmail-spp && qmail_spp_install
373         use ssl && qmail_ssl_install
374 }
375
376 qmail_queue_setup() {
377         if use highvolume; then
378                 myconf="--bigtodo"
379         else
380                 myconf="--no-bigtodo"
381         fi
382
383         mysplit=${QMAIL_CONF_SPLIT:-23}
384         is_prime ${mysplit} || die "QMAIL_CONF_SPLIT is not a prime number."
385
386         einfo "Setting up the message queue hierarchy"
387         /usr/bin/queue-repair.py --create ${myconf} \
388                 --split ${mysplit} \
389                 "${ROOT}${QMAIL_HOME}" >/dev/null || \
390                 die 'queue-repair failed'
391 }
392
393 qmail_rootmail_fixup() {
394         local TMPCMD="ln -sf ${QMAIL_HOME}/alias/.maildir/ ${ROOT}/root/.maildir"
395
396         if [[ -d "${ROOT}"/root/.maildir && ! -L "${ROOT}"/root/.maildir ]] ; then
397                 elog "Previously the qmail ebuilds created /root/.maildir/ but not"
398                 elog "every mail was delivered there. If the directory does not"
399                 elog "contain any mail, please delete it and run:"
400                 elog "${TMPCMD}"
401         else
402                 ${TMPCMD}
403         fi
404
405         chown -R alias:qmail "${ROOT}${QMAIL_HOME}"/alias/.maildir 2>/dev/null
406 }
407
408 qmail_tcprules_fixup() {
409         mkdir -p "${TCPRULES_DIR}"
410         for f in {smtp,qmtp,qmqp,pop3}{,.cdb}; do
411                 old="/etc/tcp.${f}"
412                 new="${TCPRULES_DIR}/tcp.qmail-${f}"
413                 fail=0
414                 if [[ -f "${old}" && ! -f "${new}" ]]; then
415                         einfo "Moving ${old} to ${new}"
416                         cp "${old}" "${new}" || fail=1
417                 else
418                         fail=1
419                 fi
420                 if [[ "${fail}" = 1 && -f "${old}" ]]; then
421                         eerror "Error moving ${old} to ${new}, be sure to check the"
422                         eerror "configuration! You may have already moved the files,"
423                         eerror "in which case you can delete ${old}"
424                 fi
425         done
426 }
427
428 qmail_tcprules_build() {
429         for f in tcp.qmail-{smtp,qmtp,qmqp,pop3,pop3s}; do
430                 # please note that we don't check if it exists
431                 # as we want it to make the cdb files anyway!
432                 src="${ROOT}${TCPRULES_DIR}/${f}"
433                 cdb="${ROOT}${TCPRULES_DIR}/${f}.cdb"
434                 tmp="${ROOT}${TCPRULES_DIR}/.${f}.tmp"
435                 [[ -e "${src}" ]] && tcprules "${cdb}" "${tmp}" < "${src}"
436         done
437 }
438
439 qmail_config_notice() {
440         elog
441         elog "To setup ${PN} to run out-of-the-box on your system, run:"
442         elog "emerge --config =${CATEGORY}/${PF}"
443 }
444
445 qmail_supervise_config_notice() {
446         elog
447         elog "To start qmail at boot you have to add svscan to your startup"
448         elog "and create the following links:"
449         elog "ln -s ${SUPERVISE_DIR}/qmail-send /service/qmail-send"
450         elog "ln -s ${SUPERVISE_DIR}/qmail-smtpd /service/qmail-smtpd"
451         elog
452         elog "To start the pop3 server as well, create the following link:"
453         elog "ln -s ${SUPERVISE_DIR}/qmail-pop3d /service/qmail-pop3d"
454         elog
455         if use ssl; then
456                 elog "To start the pop3s server as well, create the following link:"
457                 elog "ln -s ${SUPERVISE_DIR}/qmail-pop3sd /service/qmail-pop3sd"
458                 elog
459         fi
460         elog "Additionally, the QMTP and QMQP protocols are supported, "
461         elog "and can be started as:"
462         elog "ln -s ${SUPERVISE_DIR}/qmail-qmtpd /service/qmail-qmtpd"
463         elog "ln -s ${SUPERVISE_DIR}/qmail-qmqpd /service/qmail-qmqpd"
464         elog
465         elog "Additionally, if you wish to run qmail right now, you should "
466         elog "run this before anything else:"
467         elog "source /etc/profile"
468 }
469
470 qmail_config_fast() {
471         if [[ ${ROOT} = / ]]; then
472                 local host=$(hostname --fqdn)
473
474                 if [[ -z "${host}" ]]; then
475                         eerror
476                         eerror "Cannot determine your fully-qualified hostname"
477                         eerror "Please setup your /etc/hosts as described in"
478                         eerror "https://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=8#doc_chap2_sect4"
479                         eerror
480                         die "cannot determine FQDN"
481                 fi
482
483                 if [[ ! -f "${ROOT}${QMAIL_HOME}"/control/me ]]; then
484                         "${ROOT}${QMAIL_HOME}"/bin/config-fast ${host}
485                 fi
486         else
487                 ewarn "Skipping some configuration as it MUST be run on the final host"
488         fi
489 }
490
491 qmail_tcprules_config() {
492         local localips ip tcpstring line proto f
493
494         einfo "Accepting relaying by default from all ips configured on this machine."
495
496         # Start with iproute2 as ifconfig is deprecated, and ifconfig does not handle
497         # additional addresses added via iproute2.
498         # Note: We have to strip off the packed netmask w/e.g. 192.168.0.2/24
499         localips=$(ip address show 2>/dev/null | awk '$1 == "inet" {print $2}' | sed 's:/.*::')
500         if [[ -z ${localips} ]] ; then
501                 # Hello old friend.  Maybe you can tell us at least something.
502                 localips=$(ifconfig | awk '$1 == "inet" {print $2}')
503         fi
504
505         tcpstring=':allow,RELAYCLIENT="",RBLSMTPD=""'
506
507         for ip in ${localips}; do
508                 line="${ip}${tcpstring}"
509                 for proto in smtp qmtp qmqp; do
510                         f="${EROOT}${TCPRULES_DIR}/tcp.qmail-${proto}"
511                         egrep -qs "${line}" "${f}" || echo "${line}" >> "${f}"
512                 done
513         done
514 }
515
516 qmail_ssl_generate() {
517         CRON_FOLDER=cron.hourly
518         use gencertdaily && CRON_FOLDER=cron.daily
519
520         ebegin "Generating RSA keys for SSL/TLS, this can take some time"
521         "${ROOT}"/etc/${CRON_FOLDER}/qmail-genrsacert.sh
522         eend $?
523
524         einfo "Creating a self-signed ssl-certificate:"
525         "${ROOT}${QMAIL_HOME}"/bin/mkservercert
526
527         einfo "If you want to have a properly signed certificate "
528         einfo "instead, do the following:"
529         # space at the end of the string because of the current implementation
530         # of einfo
531         einfo "openssl req -new -nodes -out req.pem \\ "
532         einfo "  -config ${QMAIL_HOME}/control/servercert.cnf \\ "
533         einfo "  -keyout ${QMAIL_HOME}/control/servercert.pem"
534         einfo "Send req.pem to your CA to obtain signed_req.pem, and do:"
535         einfo "cat signed_req.pem >> ${QMAIL_HOME}/control/servercert.pem"
536 }