Package: cron Version: 3.0pl1-133 Severity: normal Tags: patch Dear Maintainer,
Outgoing mail has the following From: header: From: root (Cron Daemon) This gets rejected by smarthosts as suspicious spam. Rather than manually rewriting the From: header with the MTA, it would be nice if cron would use /etc/mailname. Here is a patch that has been working great for me. Thanks, Alex -- System Information: Debian Release: 10.0 APT prefers unstable-debug APT policy: (500, 'unstable-debug'), (500, 'unstable') Architecture: amd64 (x86_64) Kernel: Linux 4.19.0-5-amd64 (SMP w/4 CPU cores) Locale: LANG=fr_FR.utf8, LC_CTYPE=fr_FR.utf8 (charmap=UTF-8), LANGUAGE=fr_FR.utf8 (charmap=UTF-8) Shell: /bin/sh linked to /bin/dash Init: systemd (via /run/systemd/system) Versions of packages cron depends on: ii adduser 3.118 ii debianutils 4.8.6.1 ii init-system-helpers 1.56+nmu1 ii libc6 2.28-10 ii libpam-runtime 1.3.1-5 ii libpam0g 1.3.1-5 ii libselinux1 2.8-1+b1 ii lsb-base 10.2019051400 ii sensible-utils 0.0.12 Versions of packages cron recommends: ii ssmtp [mail-transport-agent] 2.64-8+b2 Versions of packages cron suggests: pn anacron <none> pn checksecurity <none> ii logrotate 3.14.0-4 Versions of packages cron is related to: pn libnss-ldap <none> pn libnss-ldapd <none> pn libpam-ldap <none> pn libpam-mount <none> pn nis <none> pn nscd <none> -- no debconf information
diff --git a/debian/changelog b/debian/changelog index a32710e..c506471 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +cron (3.0pl1-133+mailfrom+2) unstable; urgency=medium + + * Add domain name to mail From: header. + + -- Alexandre Rossi <alexandre.ro...@gmail.com> Sun, 12 May 2019 12:48:18 +0200 + cron (3.0pl1-133) unstable; urgency=medium * SECURITY: Fix bypass of /etc/cron.{allow,deny} on failure to open diff --git a/do_command.c b/do_command.c index 930e910..c958636 100644 --- a/do_command.c +++ b/do_command.c @@ -533,17 +533,20 @@ child_process(e, u) char **jobenv = build_env(e->envp); auto char mailcmd[MAX_COMMAND]; auto char hostname[MAXHOSTNAMELEN]; - char *content_type = env_get("CONTENT_TYPE",jobenv), + auto char domainname[MAXHOSTNAMELEN]; *content_transfer_encoding = env_get("CONTENT_TRANSFER_ENCODING",jobenv); (void) gethostname(hostname, MAXHOSTNAMELEN); + if (0 != first_line(ETCMAILNAME, domainname, MAXHOSTNAMELEN)) { + strncpy(domainname, hostname, MAXHOSTNAMELEN); + } (void) snprintf(mailcmd, sizeof(mailcmd), MAILARGS, MAILCMD, mailto); if (!(mail = cron_popen(mailcmd, "w", e))) { perror(MAILCMD); (void) _exit(ERROR_EXIT); } - fprintf(mail, "From: root (Cron Daemon)\n"); + fprintf(mail, "From: root (Cron Daemon) <root@%s>\n", domainname); fprintf(mail, "To: %s\n", mailto); fprintf(mail, "Subject: Cron <%s@%s> %s%s\n", usernm, diff --git a/misc.c b/misc.c index 77e7091..e98fef5 100644 --- a/misc.c +++ b/misc.c @@ -647,6 +647,58 @@ first_word(s, t) } +char* +strstrip(char *str) +{ + size_t size = strlen(str); + char *start = NULL; + char *end = NULL; + + if (size) { + start = str; + end = start + size - 1; + + while (end > start && isspace(*end)) + end--; + *(end + 1) = '\0'; + + while (*start && isspace(*start)) + start++; + } + + return start; +} + + +int +first_line(char *fpath, char *s, int maxlength) +{ + FILE *fp = NULL; + char *line = NULL; + char *stripped_line = NULL; + int ret = 1; + + fp = fopen(fpath, "r"); + if (!fp) { + return 1; + } + + line = malloc(sizeof(char) * maxlength); + *line = '\0'; + if (fgets(line, maxlength, fp)) { + stripped_line = strstrip(line); + if (stripped_line) { + strncpy(s, stripped_line, strlen(stripped_line) + 1); + ret = 0; + } + } + + free(line); + fclose(fp); + return ret; +} + + /* warning: * heavily ascii-dependent. */ diff --git a/pathnames.h b/pathnames.h index 669d33f..a9c7880 100644 --- a/pathnames.h +++ b/pathnames.h @@ -64,7 +64,7 @@ #define SYSCRONTAB "/etc/crontab" /* where package specific crontabs live */ #define SYSCRONDIR "/etc/cron.d" - +#define ETCMAILNAME "/etc/mailname" /* what editor to use if no EDITOR or VISUAL * environment variable specified. */