4

My ISP blocks port 25 and I want to send email after unattended-upgrades completes. I set things up so that I can successfully send mail with s-nail, but unattended-upgrades still isn't sending me email and I don't know why.

s-nail setup

First, the s-nail configuration.

Installing s-nail

I came across info for heirloom-mailx, but delving into it, the maintainers said, "ahh, you don't need that, just create a link from /usr/bin/mail to s-nail!" So that is what I did:

# first, uninstall any mailx programs you have
sudo apt remove bsd-mailx
sudo rm -f /usr/bin/mail  # just in case something is still installed

# install s-nail and link it
sudo apt install s-nail
sudo ln -s /usr/bin/s-nail /usr/bin/mail

$HOME/.mailrc

v15-compat

from="CronUpdates <[email protected]>"
sendwait
sendcharsets=utf-8,iso-8859-1

mta=smtp://cron%40mydomain.com:<redacted>@smtppro.zoho.com:587 \
smtp-auth=login \
smtp-use-starttls

which is of course chmod 600 $HOME/.mailrc'd as it is supposed to be.

$HOME/send_test_email.sh

#!/bin/bash

EMAIL_SUBJECT="Cron Email"
TO_ADDRESS="[email protected]"

echo 'Hello world!' | s-nail -s "$EMAIL_SUBJECT" "$TO_ADDRESS"

which is of course chmod u+x $HOME/send_test_email.sh'd, allowing us to:

./send_test_email.sh
# success!

Woo-hoo, I get an email!

unattended-upgrades setup

Next, the unattended-upgrades configuration in /etc/apt/apt.conf.d/50unattended-upgrades. This represents an ubuntu configuration, but I have a raspberry pi configuration that is a little different, but has the same issue.

Unattended-Upgrade::Allowed-Origins {
  "${distro_id}:${distro_codename}";
  "${distro_id}:${distro_codename}-security";
  "${distro_id}ESMApps:${distro_codename}-app-security";
  "${distro_id}ESM:${distro_codename}-infra-security";
  "${distro_id}:${distro_codename}-updates";
};

Unattended-Upgrade::Package-Blacklist {
};

Unattended-Upgrade::DevRelease "auto";

Unattended-Upgrade::Mail "[email protected]";
Unattended-Upgrade::MailReport "always";

I can --dry-run unattended upgrades, if there's anything to upgrade, but I don't think that sends an email. Still, that's useful:

# do a dry-run to iron out any issues that you can with unattended-upgrades
sudo unattended-upgrades -v -d --dry-run

# doesn't send an email, but that's operating as designed :(

Then, if there's anything to upgrade (which makes everything very difficult to debug, especially since apt-cache madison only returns one result), I can drop --dry-run and it will allegedly attempt to send the email:

machine$ sudo unattended-upgrades -v -d
Running on the development release
Starting unattended upgrades script
Allowed origins are: o=Ubuntu,a=impish, o=Ubuntu,a=impish-security, o=UbuntuESMApps,a=impish-apps-security, o=UbuntuESM,a=impish-infra-security, o=Ubuntu,a=impish-updates
Initial blacklist:
Initial whitelist (not strict):
Marking not allowed <apt_pkg.PackageFile object: filename:'/var/lib/apt/lists/download.docker.com_linux_ubuntu_dists_impish_stable_binary-amd64_Packages'  a=impish,c=stable,v=,o=Docker,l=Docker CE arch='amd64' site='download.docker.com' IndexType='Debian Package Index' Size=28335 ID:44> with -32768 pin

... many lines later ...

Package docker-ce-rootless-extras has a higher version available, checking if it is from an allowed origin and is not pinned down.
Extracting content from /var/log/unattended-upgrades/unattended-upgrades-dpkg.log since 2022-04-15 17:53:56
Sending mail to [email protected]
mail returned: 0

Oh no, I don't get any email!

The above configuration represents many hours of research, trial, and error attempting to get both email and unattended-upgrades to work; both of which work, but now must work together...

How can I make unattended-upgrades actually send its email given that I must use the .mailrc configuration defined above?

Erasmus
  • 163
  • 5

1 Answers1

1

I was stuck in the same problem and found a workaround.

We can look into the source code of the unattended-upgrades script.  As you can see, it supports only mail or sendmail commands.

MAIL_BINARY = "/usr/bin/mail"
SENDMAIL_BINARY = "/usr/sbin/sendmail"

The following lines send mail using the mail command, which has a similar interface as s-nail (-r for from address, -s for subject):

mail = subprocess.Popen(
    [MAIL_BINARY, "-r", from_address, "-s", subject, to_address],
    stdin=subprocess.PIPE, universal_newlines=False)

You can make a symbolic link from /usr/bin/mail to /usr/bin/s-nail.

  • make sure that /usr/sbin/sendmail is not installed as it first tries to use sendmail
  • s-nail is configured for the root user (config file: /etc/s-nail.rc) because unattended-upgrades runs as the root user
ywat
  • 111
  • 2