core icon indicating copy to clipboard operation
core copied to clipboard

Cron still attempts mailing job output despite `MAILTO=""` for jobs invoked via `configctl -d --`

Open jusicgn opened this issue 2 months ago • 4 comments

Title: cron still attempts mailing job output for any GUI cron job invoked via configctl -d, despite MAILTO="" — breaks remote syslog for all such jobs

Important notices

Before you add a new report, we ask you kindly to acknowledge the following:

  • [x] I have read the contributing guidelines at https://github.com/opnsense/core/blob/master/CONTRIBUTING.md
  • [x] I am convinced that my issue is new after having checked both open and closed issues at https://github.com/opnsense/core/issues?q=is%3Aissue
  • [x] AI tools were used to create at least part of the text submitted herewith.

If AI was used, please disclose:

  • Model used: Claude (Anthropic)
  • Extent of AI involvement: Investigated the log correlation, identified the UUID/byte-count match and cross-job pattern, and drafted this report text based on findings verified live on the reporter's own system (shell + remote syslog queries). Reviewed and submitted by a human.

Describe the bug

This significantly limits the usefulness of remote syslog for anyone running GUI-managed cron jobs: for every affected job run, the broken mail-attempt log line and the legitimate job-execution log line for that same job arrive as separate syslog entries, and the broken one is malformed badly enough that it loses its structured header entirely on the remote side (see below) — polluting the log stream and making it harder to build reliable alerting or dashboards on cron activity.

Any cron job created via System: Settings: Cron whose action is backed by configctl -d -- <action> can cause cron to attempt mailing job output, even though the generated /var/cron/tabs/<user> crontab already contains MAILTO="" (added upstream to close #10246). This is not limited to one specific action or plugin — we confirmed it for a third-party plugin action (a curl wrapper, run every minute) as well as for core actions (firmware poll, ids update), so it appears to be a general property of configctl -d-invoked cron jobs.

The 37 bytes cron reports mailing corresponds exactly to the UUID that configctl -d echoes to stdout on every invocation (36 hex/dash characters + newline = 37 bytes) before the actual action runs in the background — not any output from the action itself. Since no local MTA is configured, the mail attempt fails and cron logs:

(nobody) MAIL (mailed 37 bytes of output but got status 0x0001

This also reproduces the malformed multi-line syslog message described in #10246 (the log line contains an embedded LF before the closing )), which breaks framing on remote syslog receivers and causes these lines to arrive without a proper hostname/structured header — i.e. they are essentially unusable for filtering or correlation on the receiving end.

Per #10246 the accepted fix was setting MAILTO="" in the cron template, on the assumption this fully suppresses the mail attempt. On our system (see Environment) this is demonstrably not the case.

To Reproduce

  1. Go to System ‣ Settings ‣ Cron, add a new job with a schedule of */1 * * * * and any Command backed by configctl (e.g. "Custom CURL call" from a plugin, or a core action).
  2. Confirm the generated crontab already contains MAILTO="":
    crontab -l -u nobody | head -5
    
  3. After a few minutes, check for mail attempts:
    grep -i "mailed" /var/log/system/system_*.log
    
    (Note: locally, the cron facility does not appear to be written to /var/log/cron at all — point 2 raised in #10246's discussion does not appear to have been addressed by the merged fix. We had to inspect this via a remote syslog target instead.)
  4. Observe recurring (nobody) MAIL (mailed 37 bytes of output but got status 0x0001 entries, intermixed with the normal (nobody) CMD (/usr/local/sbin/configctl -d -- ...) line for the same job.
  5. Jobs that run far less frequently than every minute (e.g. the built-in "Check for new firmware every 4 hours" / firmware poll, or "ids update") also show this MAIL line on some of their runs, confirming this is not specific to one action.

Expected behavior

With MAILTO="" set, cron should not attempt to mail job output at all (this is the standard/documented cron behavior, and is what #10246 assumed would happen).

Describe alternatives you considered

  • Excluding the cron facility (9, "clock daemon") from remote syslog: rejected, since that facility also carries the legitimate (user) CMD (...) start-of-job records for every cron job (backups, firmware polls, ACME renewals, etc.) — filtering it out removes far more useful signal than it removes noise.
  • Adding output-suppressing parameters (e.g. -s -o /dev/null for a curl-based action): does not help, since configctl -d returns immediately with only its own UUID on stdout — the wrapped action's own output is captured by configd separately and never reaches cron's stdout in the first place.
  • Suppressing configctl's UUID output specifically (raised as suggestion 1 in #10246: invoke with -q) was not implemented as part of the merged fix; this may be the more targeted remaining fix, since the UUID is the actual mailed content in every case we observed.

Relevant log files

Sample from remote syslog target (values anonymized) showing a minute with four configctl -d jobs (two custom, two core) and three MAIL attempts — demonstrating this is not limited to one action:

<14>1 ... configd.py ... message ... [<plugin-action> ...] returned OK
<78>1 ... /usr/sbin/cron ... (nobody) CMD (/usr/local/sbin/configctl -d -- <plugin-action> ...)
<78>1 ... /usr/sbin/cron ... (nobody) CMD (/usr/local/sbin/configctl -d -- ids update)
<78>1 ... /usr/sbin/cron ... (nobody) CMD (/usr/local/sbin/configctl -d -- firmware poll)
<78>1 ... /usr/sbin/cron ... (nobody) CMD (/usr/local/sbin/configctl -d -- <plugin-action> ...)
<78>1 ... /usr/sbin/cron ... (nobody) MAIL (mailed 37 bytes of output but got status 0x0001
<78>1 ... /usr/sbin/cron ... (nobody) MAIL (mailed 37 bytes of output but got status 0x0001
<78>1 ... /usr/sbin/cron ... (nobody) MAIL (mailed 37 bytes of output but got status 0x0001

Current crontab excerpt (crontab -l -u nobody):

SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
MAILTO=

#minute hour mday month wday command
*/1 * * * * /usr/local/sbin/configctl -d -- <plugin-action> ...
18 1,5,9,13,17,21 * * * /usr/local/sbin/configctl -d -- firmware poll

Additional context

Related: #10246 (closed). That report and its fix (1c5f6b9) addressed the missing MAILTO="" in the generated template, but this report shows the mail attempt still occurs for configctl -d-invoked jobs regardless of which action is wrapped, and the two secondary issues raised in that thread — cron's facility not being logged locally, and the malformed multi-line remote syslog message — appear unresolved.

Environment

OPNsense 26.7.4_1 (amd64).

jusicgn avatar Sep 22 '26 13:09 jusicgn

We're not hearing much from users about this and it would also indicate cron(8) is broken as MAILTO is defined in the manual to be set to null to disable. As far as triage goes this still needs more data to proceed.

Cheers, Franco

fichtner avatar Sep 22 '26 13:09 fichtner

Thanks for the quick reply, Franco — fair skepticism. I ran the minimal test you'd want to see: a plain cron job with no configctl involved at all.

Added directly to /var/cron/tabs/nobody (which already has MAILTO= from the existing GUI-generated crontab):

* * * * * echo hello

Result, every single minute since installing it, in the remote syslog stream:

(nobody) CMD (echo hello)
(nobody) MAIL (mailed 6 bytes of output but got status 0x0001

6 bytes = hello\n. So this isn't specific to configctl -d after all — cron(8) on this system is not honoring MAILTO="" for job output at all, for a job as simple as echo hello. Happy to provide more detail (crontab -l -u nobody, OPNsense version, anything else) if that helps narrow it down further.

jusicgn avatar Sep 22 '26 18:09 jusicgn

I think this is the cause, and cron(8) is fine: the MAILTO line in the template lost its quotes.

MAILTO="" was added in 1c5f6b9 for #10246, and 8978c5f ("archaic style on cron file") changed it to MAILTO=. crontab(5) man page documents the empty value as MAILTO="". FreeBSD's cron rejects the unquoted line as a bad entry, so MAILTO is never set and every job with output is mailed.

Reproduced on a stock 26.7.7 VM (15.1-RELEASE-p4, no sendmail):

  • crontab -u operator <file> refuses the file with MAILTO=: bad minute and errors in crontab file, can't install. With MAILTO="" it installs.
  • The same file written straight to /var/cron/tabs/operator, as the template does, with * * * * * echo hello:
    • MAILTO=: one PARSE (bad minute), then CMD (echo hello) and MAIL (mailed 6 bytes of output but got status 0x0001 ) every minute
    • MAILTO="": CMD only
    • no MAILTO line: CMD and MAIL, the same as MAILTO=
  • configctl template reload OPNsense/Cron with the stock template logs (nobody) RELOAD (tabs/nobody) and then (nobody) PARSE (bad minute). With the quotes restored in the template, it logs the RELOAD only.

cron logs these lines at info level, and OPNsense keeps no local cron log, so they only show up in remote syslog. That may be why this went unnoticed.

Fix, in src/opnsense/service/templates/OPNsense/Cron/user.cron:

-MAILTO=
+MAILTO=""

AI disclosure: investigated and drafted with Claude Opus 5.5 (Anthropic). I reviewed it, and the reproduction ran on my own lab VM.

IvanTheGeek avatar Nov 02 '26 09:11 IvanTheGeek

Apparently cron is handrolling environment reading... brrr...

https://github.com/opnsense/src/blob/97a14fd3878f5db14e0a95ef902340eea8f3afec/usr.sbin/cron/lib/env.c#L224

fichtner avatar Nov 02 '26 10:11 fichtner