Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] cron/anocron don't respect MAILTO
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Fri Mar 15, 2024 10:49 am    Post subject: [SOLVED] cron/anocron don't respect MAILTO Reply with quote

I have anacron installed in my desktop. sendmail works smoothly via nullmailer.
In case of failures, it sends an email to root@my_hostname instead of the MAILTO address.

/etc/anacrontab:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=my_working_email_address@domain
RANDOM_DELAY=45
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly


Does anybody understand why?

Regards,
HUjuice
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.


Last edited by hujuice on Tue Mar 19, 2024 10:19 pm; edited 1 time in total
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Sat Mar 16, 2024 1:21 am    Post subject: Reply with quote

Just to confirm, do you actually have some MTA (Mail/Message Transfer Agent), like msmtp / qmail / postfix / etc., running on that machine?
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sat Mar 16, 2024 11:40 pm    Post subject: Reply with quote

flexibeast wrote:
Just to confirm, do you actually have some MTA (Mail/Message Transfer Agent), like msmtp / qmail / postfix / etc., running on that machine?

No, just nullmailer using the gmail smtp.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sat Mar 16, 2024 11:47 pm    Post subject: Reply with quote

The sender of the emails sent my nullmailer is my gmail account. There I receive the traditional "Mail Delivery Subsystem" message reporting that root@my_hostname is not a valid address. My desktop is configured with a FQDN using a domain I manage, but it's just a home computer with a home ADSL.

Code:
DNS Error: DNS type 'mx' lookup of box.my_domain responded with code NXDOMAIN Domain name not found: box.my_domain For more information, go to https://support.google.com/mail/?p=BadRcptDomain


anacron shouldn't try to send to root@blabla.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Sat Mar 16, 2024 11:59 pm    Post subject: Reply with quote

Is the MX record for your domain configured ("host -t mx box.my_domain")? Because that error message suggests it's not.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sun Mar 17, 2024 7:32 am    Post subject: Reply with quote

flexibeast wrote:
Is the MX record for your domain configured ("host -t mx box.my_domain")? Because that error message suggests it's not.

No. My desktop has a fake belonging to this domain.

Still unclear why root@my_hostname is involved instead of the MAILTO value, with or without domain.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Sun Mar 17, 2024 7:37 am    Post subject: Reply with quote

i've also just taken a look at the anacrontab(5) man page. It not only mentions MAILTO, but also MAILFROM:
Quote:
If MAILTO is defined (and non-empty), mail is sent to the specified address, otherwise, system user is used.

If MAILFROM is defined (and non-empty), it is used as the envelope sender address, otherwise, system user is used.

From the file you've supplied, you're not setting MAILFROM, so the system user will be used. Which means the envelope-from (i.e. the value that will be put in the Return-Path email header) will contain the system user.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sun Mar 17, 2024 11:41 am    Post subject: Reply with quote

flexibeast wrote:
you're not setting MAILFROM, so the system user will be used


Tried. It didn't change.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Sun Mar 17, 2024 5:48 pm    Post subject: Reply with quote

(As some context, in my own case, i have an OpenBSD server whose cron job output is sent to a user on that host; i access that user's mail folder via the OpenSMTPD+Dovecot setup on that machine, using POP3 to retrieve those mails from my laptop. So i'm interested in other possible ways of doing this.)

Grepping the source of both anacron and nullmailer, neither directly reference the MAILTO and MAILFROM environment variables. A quick Web search then led me to this serverfault post, which seems to indicate there's a more general issue involved:

https://serverfault.com/questions/121121/how-to-change-from-field-for-emails-from-cron

i'd be interested to know if anything there turns out to be helpful for your issue.
Back to top
View user's profile Send private message
freke
l33t
l33t


Joined: 23 Jan 2003
Posts: 977
Location: Somewhere in Denmark

PostPosted: Sun Mar 17, 2024 7:26 pm    Post subject: Reply with quote

If sys-process/cronie is an option I know that, that supports/uses MAILTO/MAILFROM fine.
(I haven't tried it with anacron use-flag though)

I use it in conjunction with nullmailer on 3 of my VMs.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sun Mar 17, 2024 10:20 pm    Post subject: Reply with quote

freke wrote:
If sys-process/cronie is an option I know that, that supports/uses MAILTO/MAILFROM fine.
(I haven't tried it with anacron use-flag though)

I use it in conjunction with nullmailer on 3 of my VMs.


There's a chain of tasks: cronie invokes anacron, anacron invokes run-parts, run-parts invokes /etc/cron.hourly/10-logcheck and 10-logcheck invokes logcheck.
When there is a problem, logcheck sends an email via nullmailar, and it works.
Meanwhile (I guess) the exit status of logcheck is not 0, so anacron sends another email, but not to MAILTO.
nullmailer is configured to use my gmail account, so normally the sender is my gmail account. It doesn't accept fake sender.

What I find in the returned email is that the original was:
Code:
Return-Path: <my_acount@gmail.com>
Received: from my_desktop_hostname (87-246-142.netrun.cytanet.com.cy. [87.228.246.142])
        by smtp.gmail.com with ESMTPSA id z5-20020a5d44c5000000b0033b87c2725csm7262006wrr.104.2024.03.17.04.01.05
        for <root@my_desktop_hostname>
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sun, 17 Mar 2024 04:01:05 -0700 (PDT)
From: "(Cron Daemon)" <my_account@gmail.com>
X-Google-Original-From: "(Cron Daemon)" <root@my_desktop_hostname>
Received: (nullmailer pid 8232 invoked by uid 0); Sun, 17 Mar 2024 11:01:01 -0000
To: root@my_desktop_hostname
Subject: Cron <root@my_desktop_hostname> run-parts /etc/cron.hourly
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <LANG=en_US.utf8>
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Sun, 17 Mar 2024 13:01:01 +0200
Message-Id: <1710673261.438595.8226.nullmailer@my_desktop_hostname>

Error: E: File could not be read: /var/log/syslog/DRAMA_levels.log
run-parts: /etc/cron.hourly/10-logcheck exited with return code 1


In the headers of the returned email there are the /etc/crontab variables, except MAILTO.

That's confusing :?

PS: the logcheck error is under control
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sun Mar 17, 2024 10:36 pm    Post subject: Reply with quote

flexibeast wrote:
A quick Web search then led me to this serverfault post, which seems to indicate there's a more general issue involved:
https://serverfault.com/questions/121121/how-to-change-from-field-for-emails-from-cron

The post is quite old, but if you didn't find MAIL* grepping the sources it could be still valid.

I should test the case putting 'exit 1' in cron, with or without MAILFROM.
I'll do it tomorrow.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Sun Mar 17, 2024 10:45 pm    Post subject: Reply with quote

flexibeast wrote:
i'm interested in other possible ways of doing this.

Generally, nullmailer works, using a real SMTP server. It resolves the annoyance of the setup.
I don't think this issue depends on nullmailer, it's a cronie/anacron issue (or my bad understanding).
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Sun Mar 17, 2024 11:10 pm    Post subject: Reply with quote

Indeed, the original post in that serverfault thread is quite old, but it includes a comment posted in July 2020 (and other comments posted after that):

Quote:
This is an old question, but it remains valid. There is no easy solution for Debian (and possibly others) because MAILFROM remains unsupported. This has bugged me for some time, as I receive cron generated mail from a number of different servers.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Mon Mar 18, 2024 9:04 am    Post subject: Reply with quote

flexibeast wrote:
Indeed, the original post in that serverfault thread is quite old, but it includes a comment posted in July 2020 (and other comments posted after that):

Quote:
This is an old question, but it remains valid. There is no easy solution for Debian (and possibly others) because MAILFROM remains unsupported. This has bugged me for some time, as I receive cron generated mail from a number of different servers.


Anyway, everybody is focusing on MAILFROM, but my problem is MAILTO.
nullmailer can be configured or instructed (the post itself shows how), but gmail overwrites the sender to be my account name. And it works in all the other cases.

The problem is that anacron (or cron?) don't respect the MAILTO variable.
Check the source snippet above.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Mon Mar 18, 2024 9:37 am    Post subject: Reply with quote

It might be that MAILTO is ultimately indeed the issue, but there's a reason i've also been considering MAILFROM.

In setting up the mail server on my OpenBSD box, to handle mail sent to 'user@my-domain', i had to set up reverse DNS (rDNS), Forward-Confirmed rDNS (FCrDNS), SPF, and DKIM, per this article. All these things are necessary to minimise the chances that the big players like Google assessing you as being at best someone with a misconfigured system, and at worst an active spammer.

It's perfectly plausible to me that command-line use of nullmailer works fine, but that (ana)cron or whatever don't try to send mail in the same way that you do from the command line. If, for example, nullmailer (sensibly) doesn't bother trying to do an MX lookup for its sending process (because it doesn't need to), but (ana)cron does something that does try to do such a lookup, it would explain both why you get the error message you quoted, plus the overall failure.

i'm nowhere near familiar enough with the *cough* venerable sendmail interface to understand what anacron is attempting to do in the following lines in runjob.c:

Code:
/* Here, I basically mirrored the way /usr/sbin/sendmail is called
* by cron on a Debian system, except for the "-oem" and "-or0s"
* options, which don't seem to be appropriate here.
* Hopefully, this will keep all the MTAs happy. */
execl(SENDMAIL, SENDMAIL, "-FAnacron", "-odi",
        username(), (char *)NULL);
die_e("Can't exec " SENDMAIL);

Because if i run "apropos sendmail" on my system, the only thing returned is:

Quote:
sendmail(8bsd) - a mail enqueuer for smtpd 8

which is only there because i installed the app-doc/openbsd-manpages package, and if i view that man page, what i get is:

Quote:
The options are as follows:

-F name
Set the sender's full name.

-f from
Set the sender's address.

-t Read the message's To:, Cc:, and Bcc: fields for recipients. The
Bcc: field will be deleted before sending.

-v Enable verbose output.

To maintain compatibility with Sendmail, Inc.'s implementation of
sendmail, various other flags are accepted, but have no effect.


which doesn't explain all the options anacron is passing to whatever is being used as the SENDMAIL program.

So i'm interested in this issue, both to try to help you and in general, but if you feel like i'm barking up a tree in the wrong forest, i'm happy to step back and stop spending time digging around.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Mon Mar 18, 2024 10:36 am    Post subject: Reply with quote

nullmailer has logs.

In the nullmailer logs I find
/var/log/nullmailer/nullmailer.log:
From: <root@hostname> to: <root@hostname>

when an email is sent by cron/anacron, despite my MAILTO.

Do you think that cron/anacron silently manipulate the sendmail behaviour and then silently change the "To:" fileld?

I changed /etc/crontab and /etc/anacrontab adding /usr/local/bin to the PATH variable
The returned email has still these headers:
headers:
X-Cron-Env: <LANG=en_US.utf8>
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>


While my /etc/crontab is:
/etc/crontab:
# Global variables
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO=my@address
MAILFROM=my_account@gmail.com
HOME=/


It looks like my /etc/crontab is ignored.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Mon Mar 18, 2024 11:19 am    Post subject: Reply with quote

*sigh*

The header TO is not the same as the envelope TO. The header FROM is not the same as the envelope FROM. The envelope TO and FROM are what matter in terms of SMTP exchanges between systems. It's possible for a program (such as anacron) to do something causing the 'root@localhost' address to be 'chosen' over the MAILTO value for one of the two FROMs, and the situation to be reversed for the other FROM. No active manipulation of a particular 'field' is required. i downloaded the anacron source to see if reading it made it clear what was or wasn't happening, but ran into the issue i described in my previous comment.

Someone who was sufficiently knowledgable about how email works, and the complexities of SMTP, might have taken the time to look at the freely-available source of both anacron and nullmailer, and go "Oh I see what's happening; that's a definite bug in anacron and/or nullmailer" and/or "Oh I see what's happening; I need to change my setup to do X." But you've given no indication that you ever made such an attempt, nor that you're aware of the existence of two distinct TOs and two distinct FROMs, instead making snarky comments.

i'm done.
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Mon Mar 18, 2024 2:04 pm    Post subject: Reply with quote

flexibeast wrote:
you've given no indication that you ever made such an attempt, nor that you're aware of the existence of two distinct TOs and two distinct FROMs, instead making snarky comments.


It's a forum, not a bugtracker. Of course I don't know enough about the SMTP protocol.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 324
Location: Naarm/Melbourne, Australia

PostPosted: Mon Mar 18, 2024 10:56 pm    Post subject: Reply with quote

hujuice wrote:
It's a forum, not a bugtracker. Of course I don't know enough about the SMTP protocol.

Whether on a forum or a bugtracker - and i've spent a lot of time on both, for various projects, over the years - the same point applies. If someone is asking for help, due to not knowing enough to be able to solve a problem themselves, they need to keep in mind that the people trying to help might have knowledge that they don't, and that those people might be bringing up certain things, or asking certain questions, because of that knowledge.

It's fine to not know about a certain topic - nobody can be an expert in everything. But i can tell you, from long-term experience, that a haughty and dismissive attitude, particularly from someone not knowing enough to be able to fix an issue themselves, is very likely to result in people not wanting to spend their (usually volunteer) time trying to help.
Back to top
View user's profile Send private message
wjb
l33t
l33t


Joined: 10 Jul 2005
Posts: 609
Location: Fife, Scotland

PostPosted: Tue Mar 19, 2024 4:28 pm    Post subject: Reply with quote

Is it maybe something to do with the configuration of the cron which runs anacron. Failing anacron jobs 'might' be handled in context of the parent cron. So is there a different MAILTO definition in /etc/crontab?
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 336
Location: Rome, Italy

PostPosted: Tue Mar 19, 2024 10:18 pm    Post subject: Reply with quote

SOLVED: I missed '/etc/nullmailer/allmailfrom' and '/etc/nullmailer/adminaddr'.

Short
Setting the first to my gmail account (the account connected with nullmailer) and the second to my notification address resolved the problem.

Long
I setup nullmailer in a VPS (with FQDN) reading the Arch Wiki: https://wiki.archlinux.org/title/Nullmailer
It doesn't mention the two files. It was working, that's it.
Then the same configuration didn't work as expected in my desktop (without FQDN). The basic 'mail' command was working, but cron didn't.

I didn't read the man pages carefully.
man nullmailer-queue wrote:
allmailfrom
If this file is not empty, its contents will override the envelope sender on all messages.

This is quite relevant for a host without FQDN.

Test

1) Without '/etc/nullmailer/allmailfrom' and '/etc/nullmailer/adminaddr'.

mail -s 'subject' recipient@domain <<< "Text"
/var/log/nullmailer.log:
From: <user@my_desktop_hostname> to: <recipient@domain>

Working, despite the fake sender.

Test script:
/etc/cron.hourly/00test (MAILTO=recipient@domain):
#!/bin/sh
echo "Failure test"
exit 1

/var/log/nullmailer.log:
From: <root@my_desktop_hostname> to: <root@my_desktop_hostname>

Not working, bad recipient.

2) With '/etc/nullmailer/allmailfrom' and '/etc/nullmailer/adminaddr'.

mail -s 'subject' recipient@domain <<< "Text"
/var/log/nullmailer.log:
From: From: <my_account@gmail.com> to: <recipient@domain>

Working

Test script:
/etc/cron.hourly/00test (MAILTO=recipient@domain):
#!/bin/sh
echo "Failure test"
exit 1

/var/log/nullmailer.log:
From: From: <my_account@gmail.com> to: <recipient@domain>

Working

I don't know how cron (not anacron) handles the protocol through nullmailer (both provided by sys-process/cronie). The hypothesis of flexibeast is significant, I don't have knowledge enough.

I will mark as solved.
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum