108.8. maddy-0.9.5
A mail server in one program — SMTP, submission and IMAP for a LAN
| Version | 0.9.5, release 2 |
| Shelf | servers — Daemons that serve other machines: remote shell, web, mail, XMPP, IRC, CalDAV/CardDAV, SMB and DLNA |
| Phase | 41_system (System), 946 of 1047 in build order |
| Licence | GPL-3.0-or-later |
| Homepage | https://maddy.email/ |
| Build time | 0.6 SBU |
| Installed size | 27.5 MiB in 5 files |
| Recipe | ports/core/servers/maddy/kpkgbuild |
About
Maddy is a mail server in one Go program: SMTP for receiving and sending, message submission, and IMAP for reading, with its own account database and mail storage. For a small installation, such as a LAN with no mail provider, it replaces the usual set of a mail transfer agent (the SMTP server), a mail delivery agent (which files each message into a mailbox) and an IMAP server. The package adds a service script, 78_maddy, which runs it under the supervisor as the maddy account; other machines reach it once the firewall’s mail name is on (see Opening a service).
Sources
maddy-0.9.5.tar.gz(424.6 KiB)
sha256c5670ff64930a4d69053557cdf252527b54c32bff882c33d120bd208f34a6e19
upstream: https://github.com/foxcpp/maddy/archive/refs/tags/v0.9.5.tar.gz
archive copy: sources-4/servers--maddy-0.9.5.tar.gzmaddy-vendor-0.9.5.tar.xz(10.3 MiB)
sha256a65f56720cb97808b3fac7453249aa591fc41599543bb71c691d3d4708e2895a
a vendor bundle: the dependencies upstream’s own tool would download, packed once
archive copy: sources-4/servers--maddy-vendor-0.9.5.tar.xz
Dependencies
Depends on: go, sqlite, scdoc, util-linux.
Required by: no port names it.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install maddy'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/servers/maddy.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/servers/maddy
echo 'c5670ff64930a4d69053557cdf252527b54c32bff882c33d120bd208f34a6e19 maddy-0.9.5.tar.gz' | sha256sum -c -
echo 'a65f56720cb97808b3fac7453249aa591fc41599543bb71c691d3d4708e2895a maddy-vendor-0.9.5.tar.xz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/maddy
SRC=$SRC_ROOT/maddy-0.9.5
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf maddy-0.9.5.tar.gz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
tar xf $PORT_SRC/${name}-vendor-${version}.tar.xz
export CGO_ENABLED=1
go build -mod=vendor -trimpath -tags libsqlite3 \
-ldflags "-s -w -X github.com/foxcpp/maddy.Version=$version" \
-o build/maddy ./cmd/maddy
install -Dm755 build/maddy "$PKG/usr/bin/maddy"
scdoc < docs/man/maddy.1.scd > build/maddy.1
install -Dm644 build/maddy.1 "$PKG/usr/share/man/man1/maddy.1"
install -Dm644 maddy.conf "$PKG/etc/maddy/maddy.conf"
install -dm750 "$PKG/var/lib/maddy"
install -d "$PKG/etc/init.d"
cat > "$PKG/etc/init.d/78_maddy.sh" <<'KDOS_SH'
#!/bin/bash
. /etc/init.d/service_helper
NAME="maddy"
DAEMON="/usr/bin/maddy"
case "$1" in
start)
[ ! -x "$DAEMON" ] && { echo "[SKIP] $NAME: $DAEMON not found"; exit 0; }
if ! getent passwd maddy >/dev/null; then
echo "[SKIP] $NAME: no maddy account (the port's postinstall makes it)"
exit 0
fi
if [ ! -s /var/lib/maddy/credentials.db ]; then
echo "[SKIP] $NAME: no account yet (run maddy creds create and maddy imap-acct create as the maddy account)"
exit 0
fi
install -d -o maddy -g maddy -m 750 /run/maddy
echo "[KDOS] Starting $NAME..."
supervise "$NAME" setpriv --reuid=maddy --regid=maddy --init-groups \
--inh-caps=+net_bind_service --ambient-caps=+net_bind_service \
"$DAEMON" --config /etc/maddy/maddy.conf run
;;
stop) stop_service "$NAME" ;;
status) check_status "$NAME" ;;
*) echo "Usage: $0 {start|stop|status}"; exit 1 ;;
esac
KDOS_SH
chmod 755 "$PKG/etc/init.d/78_maddy.sh"
The switches, and why each is set:
export CGO_ENABLED=1,-tags libsqlite3— Build with cgo for one library: thelibsqlite3tag makes the go-sqlite3 driver link the sqlite port instead of compiling the copy of SQLite’s C source it vendors, so the credentials and IMAP databases use the library every other program here does. Thelibpamtag, which would build the PAM authentication module, is not given; accounts live in Maddy’s own credentials table and are managed withmaddy creds.-X github.com/foxcpp/maddy.Version=$version— Stamp the release into the binary’s version string, as upstream’s ownbuild.shdoes.
The common switches of each build system are explained in the Flag glossary.
postinstall.sh runs on the installed system after the package is adopted:
# Everything is written under PKG_ROOT, the root kpkgadd is installing into:
# --root and an A/B update install into a tree that is not the running one, and
# an account made in / instead would be missing from the system that needs it.
#
# The guard reads PKG_ROOT's files: getent answers for the running root, not
# the tree being installed, and would let useradd run against an account the
# target already has.
root="${PKG_ROOT:-/}"
grep -q '^maddy:' "$root/etc/group" 2>/dev/null || groupadd -R "$root" -r maddy
grep -q '^maddy:' "$root/etc/passwd" 2>/dev/null || \
useradd -R "$root" -r -g maddy -d /var/lib/maddy -s /sbin/nologin maddy
# chown resolves a name against the running root's passwd, so the ids are read
# out of PKG_ROOT's.
# The mode is set as well: kpkgadd creates every directory 0755, whatever mode
# it was packaged with, because the package carries no group to give it.
while IFS=: read -r n _ uid gid _; do
[ "$n" = maddy ] || continue
chown "$uid:$gid" "$root/var/lib/maddy"
chmod 750 "$root/var/lib/maddy"
done < "$root/etc/passwd"
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/maddy, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf maddy-0.9.5-2.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/maddy, and runs the triggers its paths call for.
Contents
Installed programs: maddy.
Installed directories: /etc/maddy, /usr/share/licenses/maddy, /var/lib/maddy.
Short descriptions
maddy |
Composable all-in-one mail server. |
Notes
The Go modules come from the vendor bundle, maddy-vendor-<version>.tar.xz, which ports/fetch makes with go mod vendor because the recipe says vendoring = go; build.sh unpacks it and builds with -mod=vendor, offline. See Vendoring. The manual page is rendered from docs/man/maddy.1.scd with scdoc.
/etc/maddy/maddy.conf is upstream’s configuration as it ships. The host name, the domain and the certificate paths at its top are the part every installation edits.
Maddy never drops privileges itself. The service script makes /run/maddy, its runtime directory, which the account cannot create under /run, and starts it through setpriv as the maddy account with one capability, net_bind_service, so that it can bind ports 25, 143, 465, 587 and 993.
The service skips itself until the maddy account exists and /var/lib/maddy/credentials.db is non-empty. maddy creds create <address> writes the first account into credentials.db and maddy imap-acct create <address> its mailbox into imapsql.db; before that there is no mailbox to deliver to and nobody to let in. Both commands must run as the maddy account: a database created as root belongs to root, and the daemon can then read it but never write to it.
postinstall.sh creates the account and group and gives it /var/lib/maddy with mode 750, set there because kpkgadd creates every directory 0755, whatever mode it was packaged with. It writes under PKG_ROOT and reads the ids from that root’s own passwd.