1 # Copyright 1999-2012 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: qmail.eclass
7 # qmail-bugs@gentoo.org
8 # @BLURB: common qmail functions
10 inherit flag-o-matic toolchain-funcs fixheadtails user
13 QMAIL_HOME="/var/qmail"
14 TCPRULES_DIR="/etc/tcprules.d"
15 SUPERVISE_DIR="/var/qmail/supervise"
17 # source files and directories
18 GENQMAIL_F=genqmail-${GENQMAIL_PV}.tar.bz2
19 GENQMAIL_S="${WORKDIR}"/genqmail-${GENQMAIL_PV}
21 QMAIL_SPP_F=qmail-spp-${QMAIL_SPP_PV}.tar.gz
22 QMAIL_SPP_S="${WORKDIR}"/qmail-spp-${QMAIL_SPP_PV}
27 # Prints a list of primes between min and max inclusive
28 # Note: this functions gets very slow when used with large numbers.
30 local min=${1} max=${2}
31 local result= primelist=2 i p
33 [[ ${min} -le 2 ]] && result="${result} 2"
35 for ((i = 3; i <= max; i += 2))
39 [[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \
42 if [[ $[i % p] != 0 ]]
44 primelist="${primelist} ${i}"
45 [[ ${i} -ge ${min} ]] && \
46 result="${result} ${i}"
56 # Checks wether a number is a prime number
59 for i in $(primes ${number} ${number})
61 [[ ${i} == ${number} ]] && return 0
67 insinto "${QMAIL_HOME}"/plugins/
68 insopts -o root -g "$GROUP_ROOT" -m 0755
69 newins $1 ${2:-$(basename $1)}
72 # @FUNCTION: dosupervise
73 # @USAGE: dosupervise <service> [<runfile> <logfile>]
75 # Install runfiles for services and logging to supervise directory
78 local runfile=${2:-${service}} logfile=${3:-${service}-log}
79 [[ -z "${service}" ]] && die "no service given"
81 insopts -o root -g "$GROUP_ROOT" -m 0755
82 diropts -o root -g "$GROUP_ROOT" -m 0755
84 dodir ${SUPERVISE_DIR}/${service}{,/log}
85 fperms +t ${SUPERVISE_DIR}/${service}{,/log}
87 insinto ${SUPERVISE_DIR}/${service}
90 insinto ${SUPERVISE_DIR}/${service}/log
94 # @FUNCTION: qmail_set_cc
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.
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) #")
104 echo "${cc} ${CFLAGS} ${CPPFLAGS}" > ./conf-cc || die 'Patching conf-cc failed.'
105 echo "${ld} ${LDFLAGS}" > ./conf-ld || die 'Patching conf-ld failed.'
108 # @FUNCTION: qmail_create_groups
110 # Keep qmail groups in sync across ebuilds
111 qmail_create_groups() {
112 einfo "Creating qmail groups"
113 enewgroup nofiles 200
117 # @FUNCTION: qmail_create_users
119 # Keep qmail users in sync across ebuilds
120 qmail_create_users() {
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
133 genqmail_src_unpack() {
135 [[ -n ${GENQMAIL_PV} ]] && unpack "${GENQMAIL_F}"
138 qmail_spp_src_unpack() {
140 [[ -n ${QMAIL_SPP_PV} ]] && unpack "${QMAIL_SPP_F}"
143 # @FUNCTION: qmail_src_postunpack
145 # Unpack common config files, apply custom patches if supplied and
146 # set built configuration (CFLAGS, LDFLAGS, etc)
147 qmail_src_postunpack() {
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
158 qmail_src_compile() {
160 emake it man "$@" || die "make failed"
163 qmail_spp_src_compile() {
164 cd "${GENQMAIL_S}"/spp/
165 emake || die "make spp failed"
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}
173 einfo "Installing basic qmail software"
174 insinto "${QMAIL_HOME}"/bin
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
181 einfo "Adding env.d entry for qmail"
182 doenvd "${GENQMAIL_S}"/conf/99qmail
184 declare -F qmail_base_install_hook >/dev/null && \
185 qmail_base_install_hook
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
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
200 insopts -o root -g qmail -m 711
201 doins qmail-{clean,getpw,local,popup,pw2u,remote,rspawn,send} splogger
203 insopts -o root -g qmail -m 700
204 doins qmail-{lspawn,newmrh,newu,start}
206 insopts -o qmailq -g qmail -m 4711
209 declare -F qmail_full_install_hook >/dev/null && \
210 qmail_full_install_hook
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}
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
224 declare -F qmail_config_install_hook >/dev/null && \
225 qmail_config_install_hook
228 qmail_man_install() {
229 einfo "Installing manpages and documentation"
231 # those are tagged for section 8 but named for
232 # section 9 (which does not exist anyway)
239 dodoc BLURB* CHANGES FAQ INSTALL* PIC* README* REMOVE* SECURITY \
240 SENDMAIL SYSDEPS TEST* THANKS* THOUGHTS TODO* \
243 declare -F qmail_man_install_hook >/dev/null && \
244 qmail_man_install_hook
247 qmail_sendmail_install() {
248 einfo "Installing sendmail replacement"
250 dodir /usr/sbin /usr/lib
252 dosym "${QMAIL_HOME}"/bin/sendmail /usr/sbin/sendmail
253 dosym "${QMAIL_HOME}"/bin/sendmail /usr/lib/sendmail
255 declare -F qmail_sendmail_install_hook >/dev/null && \
256 qmail_sendmail_install_hook
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"
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}
271 for i in "${QMAIL_HOME}"/alias/.qmail-{mailer-daemon,postmaster,root}; do
272 if [[ ! -f "${ROOT}${i}" ]]; then
274 fowners alias:qmail "${i}"
278 einfo "Setting up default maildirs in the account skeleton"
279 diropts -o root -g "$GROUP_ROOT" -m 755
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}
286 declare -F qmail_maildir_install_hook >/dev/null && \
287 qmail_maildir_install_hook
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
299 qmail_supervise_install() {
300 einfo "Installing supervise scripts"
302 cd "${GENQMAIL_S}"/supervise
304 for i in qmail-{send,smtpd,qmtpd,qmqpd,pop3d}; do
306 diropts -o qmaill -g "$GROUP_ROOT" -m 755
307 keepdir /var/log/qmail/${i}
311 dosupervise qmail-pop3sd
312 diropts -o qmaill -g "$GROUP_ROOT" -m 755
313 keepdir /var/log/qmail/qmail-pop3sd
316 declare -F qmail_supervise_install_hook >/dev/null && \
317 qmail_supervise_install_hook
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
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}
332 declare -F qmail_spp_install_hook >/dev/null && \
333 qmail_spp_install_hook
336 qmail_ssl_install() {
337 use gencertdaily && \
338 CRON_FOLDER=cron.daily || \
339 CRON_FOLDER=cron.hourly
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
346 insinto "${QMAIL_HOME}"/bin
347 insopts -o root -g "$GROUP_ROOT" -m 0755
348 doins "${GENQMAIL_S}"/ssl/mkservercert
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
355 keepdir "${QMAIL_HOME}"/control/tlshosts
357 declare -F qmail_ssl_install_hook >/dev/null && \
358 qmail_ssl_install_hook
361 qmail_src_install() {
362 export GROUP_ROOT="$(id -gn root)"
367 qmail_sendmail_install
368 qmail_maildir_install
369 qmail_tcprules_install
370 qmail_supervise_install
372 use qmail-spp && qmail_spp_install
373 use ssl && qmail_ssl_install
376 qmail_queue_setup() {
377 if use highvolume; then
380 myconf="--no-bigtodo"
383 mysplit=${QMAIL_CONF_SPLIT:-23}
384 is_prime ${mysplit} || die "QMAIL_CONF_SPLIT is not a prime number."
386 einfo "Setting up the message queue hierarchy"
387 /usr/bin/queue-repair.py --create ${myconf} \
389 "${ROOT}${QMAIL_HOME}" >/dev/null || \
390 die 'queue-repair failed'
393 qmail_rootmail_fixup() {
394 local TMPCMD="ln -sf ${QMAIL_HOME}/alias/.maildir/ ${ROOT}/root/.maildir"
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:"
405 chown -R alias:qmail "${ROOT}${QMAIL_HOME}"/alias/.maildir 2>/dev/null
408 qmail_tcprules_fixup() {
409 mkdir -p "${TCPRULES_DIR}"
410 for f in {smtp,qmtp,qmqp,pop3}{,.cdb}; do
412 new="${TCPRULES_DIR}/tcp.qmail-${f}"
414 if [[ -f "${old}" && ! -f "${new}" ]]; then
415 einfo "Moving ${old} to ${new}"
416 cp "${old}" "${new}" || fail=1
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}"
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}"
439 qmail_config_notice() {
441 elog "To setup ${PN} to run out-of-the-box on your system, run:"
442 elog "emerge --config =${CATEGORY}/${PF}"
445 qmail_supervise_config_notice() {
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"
452 elog "To start the pop3 server as well, create the following link:"
453 elog "ln -s ${SUPERVISE_DIR}/qmail-pop3d /service/qmail-pop3d"
456 elog "To start the pop3s server as well, create the following link:"
457 elog "ln -s ${SUPERVISE_DIR}/qmail-pop3sd /service/qmail-pop3sd"
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"
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"
470 qmail_config_fast() {
471 if [[ ${ROOT} = / ]]; then
472 local host=$(hostname --fqdn)
474 if [[ -z "${host}" ]]; then
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"
480 die "cannot determine FQDN"
483 if [[ ! -f "${ROOT}${QMAIL_HOME}"/control/me ]]; then
484 "${ROOT}${QMAIL_HOME}"/bin/config-fast ${host}
487 ewarn "Skipping some configuration as it MUST be run on the final host"
491 qmail_tcprules_config() {
492 local localips ip tcpstring line proto f
494 einfo "Accepting relaying by default from all ips configured on this machine."
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}')
505 tcpstring=':allow,RELAYCLIENT="",RBLSMTPD=""'
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}"
516 qmail_ssl_generate() {
517 CRON_FOLDER=cron.hourly
518 use gencertdaily && CRON_FOLDER=cron.daily
520 ebegin "Generating RSA keys for SSL/TLS, this can take some time"
521 "${ROOT}"/etc/${CRON_FOLDER}/qmail-genrsacert.sh
524 einfo "Creating a self-signed ssl-certificate:"
525 "${ROOT}${QMAIL_HOME}"/bin/mkservercert
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
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"