You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.2 KiB
88 lines
2.2 KiB
#!/usr/bin/env bash |
|
# Install fetchmail POP3 drain: cPanel mail.geonet.co.id -> Mailcow support@geonet.co.id |
|
# Run on Mailcow VM (10.100.1.28) as root. |
|
set -euo pipefail |
|
|
|
PASS_FILE="${PASS_FILE:-/opt/fetchmail/support.pass}" |
|
FETCHMAILRC="${FETCHMAILRC:-/opt/fetchmail/fetchmailrc}" |
|
POP3_USER="${POP3_USER:-support@geonet.co.id}" |
|
POP3_HOST="${POP3_HOST:-mail.geonet.co.id}" |
|
LOCAL_USER="${LOCAL_USER:-support@geonet.co.id}" |
|
|
|
usage() { |
|
echo "Usage: POP3_PASSWORD='...' $0" >&2 |
|
echo " or: PASS_FILE=/path/to/secret $0" >&2 |
|
exit 1 |
|
} |
|
|
|
if [[ -n "${POP3_PASSWORD:-}" ]]; then |
|
install -d -m 700 /opt/fetchmail |
|
printf '%s' "$POP3_PASSWORD" >"$PASS_FILE" |
|
chmod 600 "$PASS_FILE" |
|
elif [[ ! -f "$PASS_FILE" ]]; then |
|
usage |
|
fi |
|
|
|
apt-get install -y -qq fetchmail |
|
|
|
python3 - "$PASS_FILE" "$FETCHMAILRC" "$POP3_USER" "$POP3_HOST" "$LOCAL_USER" <<'PY' |
|
import pathlib, sys |
|
|
|
pass_file, rc_path, pop3_user, pop3_host, local_user = sys.argv[1:6] |
|
password = pathlib.Path(pass_file).read_text() |
|
|
|
cfg = f'''set postmaster "{local_user}" |
|
set syslog |
|
set logfile /var/log/fetchmail-support.log |
|
|
|
poll {pop3_host} with proto POP3 port 995 |
|
user "{pop3_user}" there with password {password!r} is "{local_user}" here |
|
ssl |
|
no sslcertck |
|
smtphost 127.0.0.1 |
|
smtpname sync.geonet.co.id |
|
fetchall |
|
nokeep |
|
''' |
|
path = pathlib.Path(rc_path) |
|
path.write_text(cfg) |
|
path.chmod(0o600) |
|
PY |
|
|
|
touch /var/log/fetchmail-support.log |
|
chmod 600 /var/log/fetchmail-support.log |
|
|
|
cat >/etc/systemd/system/fetchmail-support.service <<EOF |
|
[Unit] |
|
Description=fetchmail POP3 drain cPanel to Mailcow (${LOCAL_USER}) |
|
After=network-online.target docker.service |
|
Wants=network-online.target |
|
|
|
[Service] |
|
Type=oneshot |
|
ExecStart=/usr/bin/fetchmail -f ${FETCHMAILRC} --nodetach |
|
SuccessExitStatus=0 1 |
|
EOF |
|
|
|
cat >/etc/systemd/system/fetchmail-support.timer <<'EOF' |
|
[Unit] |
|
Description=Poll cPanel POP3 every 2 minutes |
|
|
|
[Timer] |
|
OnBootSec=90s |
|
OnUnitActiveSec=2min |
|
AccuracySec=30s |
|
Persistent=true |
|
|
|
[Install] |
|
WantedBy=timers.target |
|
EOF |
|
|
|
systemctl daemon-reload |
|
systemctl enable --now fetchmail-support.timer |
|
|
|
echo "fetchmail installed. Timer status:" |
|
systemctl status fetchmail-support.timer --no-pager || true |
|
echo "Test run:" |
|
fetchmail -f "$FETCHMAILRC" -c && fetchmail -f "$FETCHMAILRC" -v -N |
|
echo "Log: /var/log/fetchmail-support.log"
|
|
|