1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: qmail.eclass
6 # qmail-bugs@gentoo.org
7 # @BLURB: common qmail functions
9 inherit flag-o-matic toolchain-funcs fixheadtails
12 QMAIL_HOME="/var/qmail"
13 TCPRULES_DIR="/etc/tcprules.d"
14 SUPERVISE_DIR="/var/qmail/supervise"
16 # source files and directories
17 GENQMAIL_F=genqmail-${GENQMAIL_PV}.tar.bz2
18 GENQMAIL_S="${WORKDIR}"/genqmail-${GENQMAIL_PV}
20 QMAIL_SPP_F=qmail-spp-${QMAIL_SPP_PV}.tar.gz
21 QMAIL_SPP_S="${WORKDIR}"/qmail-spp-${QMAIL_SPP_PV}
26 # Prints a list of primes between min and max inclusive
27 # Note: this functions gets very slow when used with large numbers.
29 local min=${1} max=${2}
30 local result= primelist=2 i p
32 [[ ${min} -le 2 ]] && result="${result} 2"
34 for ((i = 3; i <= max; i += 2))
38 [[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \
41 if [[ $[i % p] != 0 ]]
43 primelist="${primelist} ${i}"
44 [[ ${i} -ge ${min} ]] && \
45 result="${result} ${i}"
55 # Checks wether a number is a prime number
58 for i in $(primes ${number} ${number})
60 [[ ${i} == ${number} ]] && return 0
66 insinto "${QMAIL_HOME}"/plugins/
67 insopts -o root -g "${GROUP_ROOT}" -m 0755
68 newins $1 ${2:-$(basename $1)}
71 # @FUNCTION: dosupervise
72 # @USAGE: <service> [<runfile> <logfile>]
74 # Install runfiles for services and logging to supervise directory
77 local runfile=${2:-${service}} logfile=${3:-${service}-log}
78 [[ -z "${service}" ]] && die "no service given"
80 insopts -o root -g "${GROUP_ROOT}" -m 0755
81 diropts -o root -g "${GROUP_ROOT}" -m 0755
83 dodir ${SUPERVISE_DIR}/${service}{,/log}
84 fperms +t ${SUPERVISE_DIR}/${service}{,/log}
86 insinto ${SUPERVISE_DIR}/${service}
89 insinto ${SUPERVISE_DIR}/${service}/log
93 # @FUNCTION: qmail_set_cc
95 # The following commands patch the conf-{cc,ld} files to use the user's
96 # specified CFLAGS and LDFLAGS. These rather complex commands are needed
97 # because a user supplied patch might apply changes to these files, too.
100 local cc=$(head -n 1 ./conf-cc | sed -e "s#^g\?cc\s\+\(-O2\)\?#$(tc-getCC) #")
101 local ld=$(head -n 1 ./conf-ld | sed -e "s#^g\?cc\s\+\(-s\)\?#$(tc-getCC) #")
103 echo "${cc} ${CFLAGS} ${CPPFLAGS}" > ./conf-cc || die 'Patching conf-cc failed.'
104 echo "${ld} ${LDFLAGS}" > ./conf-ld || die 'Patching conf-ld failed.'
105 sed -e "s#'ar #'$(tc-getAR) #" -e "s#'ranlib #'$(tc-getRANLIB) #" -i make-makelib.sh
108 genqmail_src_unpack() {
110 [[ -n ${GENQMAIL_PV} ]] && unpack "${GENQMAIL_F}"
113 qmail_spp_src_unpack() {
115 [[ -n ${QMAIL_SPP_PV} ]] && unpack "${QMAIL_SPP_F}"
118 # @FUNCTION: qmail_src_postunpack
120 # Unpack common config files, apply custom patches if supplied and
121 # set built configuration (CFLAGS, LDFLAGS, etc)
122 qmail_src_postunpack() {
127 mysplit=${QMAIL_CONF_SPLIT:-23}
128 is_prime ${mysplit} || die "QMAIL_CONF_SPLIT is not a prime number."
129 einfo "Using conf-split value of ${mysplit}."
130 echo -n ${mysplit} > "${S}"/conf-split
133 qmail_src_compile() {
135 emake it man "$@" || die "make failed"
138 qmail_spp_src_compile() {
139 cd "${GENQMAIL_S}"/spp/
140 emake || die "make spp failed"
143 qmail_base_install() {
144 einfo "Setting up basic directory hierarchy"
145 diropts -o root -g qmail -m 755
146 keepdir "${QMAIL_HOME}"/{,bin,control}
148 einfo "Installing basic qmail software"
149 insinto "${QMAIL_HOME}"/bin
151 insopts -o root -g qmail -m 755
152 doins datemail elq forward maildir2mbox maildirmake \
153 maildirwatch mailsubj pinq predate qail \
154 qmail-{inject,qmqpc,showctl} sendmail
156 einfo "Adding env.d entry for qmail"
157 doenvd "${GENQMAIL_S}"/conf/99qmail
159 declare -F qmail_base_install_hook >/dev/null && \
160 qmail_base_install_hook
163 qmail_full_install() {
164 einfo "Setting up full directory hierarchy"
165 keepdir "${QMAIL_HOME}"/users
166 diropts -o alias -g qmail -m 755
167 keepdir "${QMAIL_HOME}"/alias
169 einfo "Installing all qmail software"
170 insopts -o root -g qmail -m 755
171 doins bouncesaying condredirect config-fast except preline qbiff \
172 qmail-{qmqpd,qmtpd,qread,qstat,smtpd,tcpok,tcpto} \
173 qreceipt qsmhook tcp-env
174 use pop3 && doins qmail-pop3d
176 insopts -o root -g qmail -m 711
177 doins qmail-{clean,getpw,local,pw2u,remote,rspawn,send} splogger
178 use pop3 && doins qmail-popup
180 insopts -o root -g qmail -m 700
181 doins qmail-{lspawn,newmrh,newu,start}
183 insopts -o qmailq -g qmail -m 4711
186 declare -F qmail_full_install_hook >/dev/null && \
187 qmail_full_install_hook
190 qmail_config_install() {
191 einfo "Installing stock configuration files"
192 insinto "${QMAIL_HOME}"/control
193 insopts -o root -g "${GROUP_ROOT}" -m 644
194 doins "${GENQMAIL_S}"/control/{conf-*,defaultdelivery}
196 einfo "Installing configuration sanity checker and launcher"
197 insinto "${QMAIL_HOME}"/bin
198 insopts -o root -g "${GROUP_ROOT}" -m 644
199 doins "${GENQMAIL_S}"/control/qmail-config-system
201 declare -F qmail_config_install_hook >/dev/null && \
202 qmail_config_install_hook
205 qmail_man_install() {
206 einfo "Installing manpages and documentation"
210 dodoc BLURB* CHANGES FAQ INSTALL* PIC* README* REMOVE* SECURITY \
211 SENDMAIL* TEST* THANKS* THOUGHTS UPGRADE VERSION*
213 declare -F qmail_man_install_hook >/dev/null && \
214 qmail_man_install_hook
217 qmail_sendmail_install() {
218 einfo "Installing sendmail replacement"
220 dodir /usr/sbin /usr/lib
222 dosym "${QMAIL_HOME}"/bin/sendmail /usr/sbin/sendmail
223 dosym "${QMAIL_HOME}"/bin/sendmail /usr/lib/sendmail
225 declare -F qmail_sendmail_install_hook >/dev/null && \
226 qmail_sendmail_install_hook
229 qmail_maildir_install() {
230 # use the correct maildirmake
231 # the courier-imap one has some extensions that are nicer
232 MAILDIRMAKE="${D}${QMAIL_HOME}/bin/maildirmake"
233 [[ -e /usr/bin/maildirmake ]] && \
234 MAILDIRMAKE="/usr/bin/maildirmake"
236 einfo "Setting up the default aliases"
237 diropts -o alias -g qmail -m 700
238 "${MAILDIRMAKE}" "${D}${QMAIL_HOME}"/alias/.maildir
239 keepdir "${QMAIL_HOME}"/alias/.maildir/{cur,new,tmp}
241 for i in "${QMAIL_HOME}"/alias/.qmail-{mailer-daemon,postmaster,root}; do
242 if [[ ! -f "${ROOT}${i}" ]]; then
244 fowners alias:qmail "${i}"
248 einfo "Setting up default maildirs in the account skeleton"
249 diropts -o root -g "${GROUP_ROOT}" -m 755
251 insopts -o root -g "${GROUP_ROOT}" -m 644
252 newins "${GENQMAIL_S}"/control/defaultdelivery .qmail.sample
253 "${MAILDIRMAKE}" "${D}"/etc/skel/.maildir
254 keepdir /etc/skel/.maildir/{cur,new,tmp}
256 declare -F qmail_maildir_install_hook >/dev/null && \
257 qmail_maildir_install_hook
260 qmail_tcprules_install() {
261 dodir "${TCPRULES_DIR}"
262 insinto "${TCPRULES_DIR}"
263 insopts -o root -g "${GROUP_ROOT}" -m 0644
264 doins "${GENQMAIL_S}"/tcprules/Makefile.qmail
265 doins "${GENQMAIL_S}"/tcprules/tcp.qmail-*
266 use ssl && use pop3 || rm -f "${D}${TCPRULES_DIR}"/tcp.qmail-pop3sd
269 qmail_supervise_install_one() {
271 diropts -o qmaill -g "${GROUP_ROOT}" -m 755
272 keepdir /var/log/qmail/${1}
275 qmail_supervise_install() {
276 einfo "Installing supervise scripts"
278 cd "${GENQMAIL_S}"/supervise
280 for i in qmail-{send,smtpd,qmtpd,qmqpd}; do
281 qmail_supervise_install_one ${i}
285 qmail_supervise_install_one qmail-pop3d
286 use ssl && qmail_supervise_install_one qmail-pop3sd
289 declare -F qmail_supervise_install_hook >/dev/null && \
290 qmail_supervise_install_hook
293 qmail_spp_install() {
294 einfo "Installing qmail-spp configuration files"
295 insinto "${QMAIL_HOME}"/control/
296 insopts -o root -g "${GROUP_ROOT}" -m 0644
297 doins "${GENQMAIL_S}"/spp/smtpplugins
299 einfo "Installing qmail-spp plugins"
300 keepdir "${QMAIL_HOME}"/plugins/
301 for i in authlog mfdnscheck ifauthnext tarpit; do
302 dospp "${GENQMAIL_S}"/spp/${i}
305 declare -F qmail_spp_install_hook >/dev/null && \
306 qmail_spp_install_hook
309 qmail_ssl_install() {
310 use gencertdaily && \
311 CRON_FOLDER=cron.daily || \
312 CRON_FOLDER=cron.hourly
314 einfo "Installing SSL Certificate creation script"
315 insinto "${QMAIL_HOME}"/control
316 insopts -o root -g "${GROUP_ROOT}" -m 0644
317 doins "${GENQMAIL_S}"/ssl/servercert.cnf
319 insinto "${QMAIL_HOME}"/bin
320 insopts -o root -g "${GROUP_ROOT}" -m 0755
321 doins "${GENQMAIL_S}"/ssl/mkservercert
323 einfo "Installing RSA key generation cronjob"
324 insinto /etc/${CRON_FOLDER}
325 insopts -o root -g "${GROUP_ROOT}" -m 0755
326 doins "${GENQMAIL_S}"/ssl/qmail-genrsacert.sh
328 keepdir "${QMAIL_HOME}"/control/tlshosts
330 declare -F qmail_ssl_install_hook >/dev/null && \
331 qmail_ssl_install_hook
334 qmail_src_install() {
335 export GROUP_ROOT="$(id -gn root)"
340 qmail_sendmail_install
341 qmail_maildir_install
342 qmail_tcprules_install
343 qmail_supervise_install
345 use qmail-spp && qmail_spp_install
346 use ssl && qmail_ssl_install
349 qmail_queue_setup() {
350 if use highvolume; then
353 myconf="--no-bigtodo"
356 mysplit=${QMAIL_CONF_SPLIT:-23}
357 is_prime ${mysplit} || die "QMAIL_CONF_SPLIT is not a prime number."
359 einfo "Setting up the message queue hierarchy"
360 /usr/bin/queue-repair.py --create ${myconf} \
362 "${ROOT}${QMAIL_HOME}" >/dev/null || \
363 die 'queue-repair failed'
366 qmail_rootmail_fixup() {
367 local TMPCMD="ln -sf ${QMAIL_HOME}/alias/.maildir/ ${ROOT}/root/.maildir"
369 if [[ -d "${ROOT}"/root/.maildir && ! -L "${ROOT}"/root/.maildir ]] ; then
370 elog "Previously the qmail ebuilds created /root/.maildir/ but not"
371 elog "every mail was delivered there. If the directory does not"
372 elog "contain any mail, please delete it and run:"
378 chown -R alias:qmail "${ROOT}${QMAIL_HOME}"/alias/.maildir 2>/dev/null
381 qmail_tcprules_fixup() {
382 mkdir -p "${TCPRULES_DIR}"
384 use pop3 && POP_FILES="pop3 pop3.cdb"
385 for f in {smtp,qmtp,qmqp}{,.cdb} ${POP_FILES}; do
387 new="${TCPRULES_DIR}/tcp.qmail-${f}"
389 if [[ -f "${old}" && ! -f "${new}" ]]; then
390 einfo "Moving ${old} to ${new}"
391 cp "${old}" "${new}" || fail=1
395 if [[ "${fail}" = 1 && -f "${old}" ]]; then
396 eerror "Error moving ${old} to ${new}, be sure to check the"
397 eerror "configuration! You may have already moved the files,"
398 eerror "in which case you can delete ${old}"
403 qmail_tcprules_build() {
404 for f in tcp.qmail-{smtp,qmtp,qmqp,pop3,pop3s}; do
405 # please note that we don't check if it exists
406 # as we want it to make the cdb files anyway!
407 src="${ROOT}${TCPRULES_DIR}/${f}"
408 cdb="${ROOT}${TCPRULES_DIR}/${f}.cdb"
409 tmp="${ROOT}${TCPRULES_DIR}/.${f}.tmp"
410 [[ -e "${src}" ]] && tcprules "${cdb}" "${tmp}" < "${src}"
414 qmail_config_notice() {
416 elog "To setup ${PN} to run out-of-the-box on your system, run:"
417 elog "emerge --config =${CATEGORY}/${PF}"
420 qmail_supervise_config_notice() {
422 elog "To start qmail at boot you have to add svscan to your startup"
423 elog "and create the following links:"
424 elog "ln -s ${SUPERVISE_DIR}/qmail-send /service/qmail-send"
425 elog "ln -s ${SUPERVISE_DIR}/qmail-smtpd /service/qmail-smtpd"
428 elog "To start the pop3 server as well, create the following link:"
429 elog "ln -s ${SUPERVISE_DIR}/qmail-pop3d /service/qmail-pop3d"
432 elog "To start the pop3s server as well, create the following link:"
433 elog "ln -s ${SUPERVISE_DIR}/qmail-pop3sd /service/qmail-pop3sd"
437 elog "Additionally, the QMTP and QMQP protocols are supported, "
438 elog "and can be started as:"
439 elog "ln -s ${SUPERVISE_DIR}/qmail-qmtpd /service/qmail-qmtpd"
440 elog "ln -s ${SUPERVISE_DIR}/qmail-qmqpd /service/qmail-qmqpd"
442 elog "Additionally, if you wish to run qmail right now, you should "
443 elog "run this before anything else:"
444 elog "source /etc/profile"
447 qmail_config_fast() {
448 if [[ ${ROOT} = / ]]; then
449 local host=$(hostname --fqdn)
451 if [[ -z "${host}" ]]; then
453 eerror "Cannot determine your fully-qualified hostname"
454 eerror "Please setup your /etc/hosts as described in"
455 eerror "https://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=8#doc_chap2_sect4"
457 die "cannot determine FQDN"
460 if [[ ! -f "${ROOT}${QMAIL_HOME}"/control/me ]]; then
461 "${ROOT}${QMAIL_HOME}"/bin/config-fast ${host}
464 ewarn "Skipping some configuration as it MUST be run on the final host"
468 qmail_tcprules_config() {
469 local localips ip tcpstring line proto f
471 einfo "Accepting relaying by default from all ips configured on this machine."
473 # Start with iproute2 as ifconfig is deprecated, and ifconfig does not handle
474 # additional addresses added via iproute2.
475 # Note: We have to strip off the packed netmask w/e.g. 192.168.0.2/24
476 localips=$(ip address show 2>/dev/null | awk '$1 == "inet" {print $2}' | sed 's:/.*::')
477 if [[ -z ${localips} ]] ; then
478 # Hello old friend. Maybe you can tell us at least something.
479 localips=$(ifconfig | awk '$1 == "inet" {print $2}')
482 tcpstring=':allow,RELAYCLIENT="",RBLSMTPD=""'
484 for ip in ${localips}; do
485 line="${ip}${tcpstring}"
486 for proto in smtp qmtp qmqp; do
487 f="${EROOT}${TCPRULES_DIR}/tcp.qmail-${proto}"
488 egrep -qs "${line}" "${f}" || echo "${line}" >> "${f}"
493 qmail_ssl_generate() {
494 CRON_FOLDER=cron.hourly
495 use gencertdaily && CRON_FOLDER=cron.daily
497 ebegin "Generating RSA keys for SSL/TLS, this can take some time"
498 "${ROOT}"/etc/${CRON_FOLDER}/qmail-genrsacert.sh
501 einfo "Creating a self-signed ssl-certificate:"
502 "${ROOT}${QMAIL_HOME}"/bin/mkservercert
504 einfo "If you want to have a properly signed certificate "
505 einfo "instead, do the following:"
506 # space at the end of the string because of the current implementation
508 einfo "openssl req -new -nodes -out req.pem \\ "
509 einfo " -config ${QMAIL_HOME}/control/servercert.cnf \\ "
510 einfo " -keyout ${QMAIL_HOME}/control/servercert.pem"
511 einfo "Send req.pem to your CA to obtain signed_req.pem, and do:"
512 einfo "cat signed_req.pem >> ${QMAIL_HOME}/control/servercert.pem"