]> granicus.if.org Git - cronie/commitdiff
Now is possible to use different character encodings for mailed cron job output by
authormmaslano <mmaslano@redhat.com>
Fri, 17 Aug 2007 13:15:49 +0000 (15:15 +0200)
committermmaslano <mmaslano@redhat.com>
Fri, 24 Aug 2007 13:06:00 +0000 (15:06 +0200)
setting the CONTENT_TYPE and CONTENT_TRANSFER_ENCODING variables in crontabs,
to the correct values of the mail headers of those names.

cron.c
crontab.5
do_command.c
externs.h
globals.h

diff --git a/cron.c b/cron.c
index 44912974418517d385c849e3f01da225bbce0972..70c0656be62f2680236ee42f4baa5f856474d6d1 100644 (file)
--- a/cron.c
+++ b/cron.c
@@ -60,6 +60,7 @@ main(int argc, char *argv[]) {
        struct sigaction sact;
        cron_db database;
        int fd;
+       char *cs;
 
        ProgramName = argv[0];
 
@@ -99,6 +100,16 @@ main(int argc, char *argv[]) {
        if ( getenv("CRON_VALIDATE_MAILRCPTS") != 0L )
            ValidateMailRcpts=1;
 
+       /* Get the default locale character set for the mail 
+        * "Content-Type: ...; charset=" header
+        */
+       setlocale(LC_ALL,""); /* set locale to system defaults or to
+                                that specified by any  LC_* env vars */
+       if ( ( cs = nl_langinfo( CODESET ) ) != 0L )
+           strncpy( cron_default_mail_charset, cs, MAX_ENVSTR );
+       else
+           strcpy( cron_default_mail_charset, "US-ASCII" );
+       
        /* if there are no debug flags turned on, fork as a daemon should.
         */
        if (DebugFlags) {
index f103debe5a22e7ee03ebe5ff6d4eb1bcd51f85af..31547cabdaf5476bf0a9bcaecbfd1299b628802d 100644 (file)
--- a/crontab.5
+++ b/crontab.5
@@ -61,7 +61,7 @@ automatically by the
 .IR cron (8)
 daemon.
 SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd 
-line of the crontab's owner.
+line of the crontab\'s owner.
 HOME and SHELL may be overridden by settings in the crontab; LOGNAME may not.
 .PP
 (Another note: the LOGNAME variable is sometimes called USER on BSD systems...
@@ -74,8 +74,18 @@ commands in ``this'' crontab.  If MAILTO is defined (and non-empty), mail is
 sent to the user so named.  If MAILTO is defined but empty (MAILTO=""), no
 mail will be sent.  Otherwise mail is sent to the owner of the crontab.  This
 option is useful if you decide on /bin/mail instead of /usr/lib/sendmail as
-your mailer when you install cron -- /bin/mail doesn't do aliasing, and UUCP
-usually doesn't read its mail.
+your mailer when you install cron -- /bin/mail doesn\'t do aliasing, and UUCP
+usually doesn\'t read its mail.
+.PP
+By default, cron will send mail using the mail 'Content-Type:' header of 'text/plain' with the 'charset=' parameter set to the charmap / codeset of the locale in which 
+.IR crond(8)
+is started up - ie. either the default system locale, if no LC_* environment
+variables are set, or the locale specified by the LC_* environment variables
+( see 
+.IR locale(7) ). 
+You can use different character encodings for mailed cron job output by 
+setting the CONTENT_TYPE and CONTENT_TRANSFER_ENCODING variables in crontabs,
+to the correct values of the mail headers of those names.  
 .PP
 The format of a cron command is very much the V7 standard, with a number of
 upward-compatible extensions.  Each line has five time and date fields,
index 9cc4c9cb57f1ed2c25b846637e36d5894686f2e6..bf07bd15db3d4125dbbb05a3e7f717f691fe7a96 100644 (file)
@@ -375,6 +375,8 @@ child_process(entry *e, user *u) {
                                char    **env;
                                char    mailcmd[MAX_COMMAND];
                                char    hostname[MAXHOSTNAMELEN];
+                               char    *content_type = env_get("CONTENT_TYPE",jobenv),
+                                       *content_transfer_encoding = env_get("CONTENT_TRANSFER_ENCODING",jobenv);
 
                                gethostname(hostname, MAXHOSTNAMELEN);
                                
@@ -400,10 +402,40 @@ child_process(entry *e, user *u) {
                                fprintf(mail, "Subject: Cron <%s@%s> %s\n",
                                        usernm, first_word(hostname, "."),
                                        e->cmd);
+
 #ifdef MAIL_DATE
                                fprintf(mail, "Date: %s\n",
                                        arpadate(&StartTime));
 #endif /*MAIL_DATE*/
+                               if ( content_type == 0L )
+                               {   
+                                       fprintf(mail, "Content-Type: text/plain; charset=%s\n",
+                                               cron_default_mail_charset
+                                              );
+                               }else
+                               {       /* user specified Content-Type header. 
+                                        * disallow new-lines for security reasons 
+                                        * (else users could specify arbitrary mail headers!)
+                                        */
+                                       char *nl=content_type;
+                                       size_t ctlen = strlen(content_type);                                
+                                       while(  (*nl != '\0') 
+                                            && ((nl=strchr(nl,'\n')) != 0L)
+                                            && (nl < (content_type+ctlen)) 
+                                            ) *nl = ' ';
+                                       fprintf(mail,"Content-Type: %s\n", content_type);
+                               }
+                               if ( content_transfer_encoding != 0L )
+                               {
+                                       char *nl=content_transfer_encoding;
+                                       size_t ctlen = strlen(content_transfer_encoding);
+                                       while(  (*nl != '\0') 
+                                            && ((nl=strchr(nl,'\n')) != 0L)
+                                            && (nl < (content_transfer_encoding+ctlen))
+                                            ) *nl = ' ';                               
+                                       fprintf(mail,"Content-Transfer-Encoding: %s\n", content_transfer_encoding);
+                               }
+
                                for (env = jobenv;  *env;  env++)
                                        fprintf(mail, "X-Cron-Env: <%s>\n",
                                                *env);
index 470f9365c9b19557935b19f2f327794721ca8b6c..8d8fd94f740702da7b88d14c378c3ff25ec7e822 100644 (file)
--- a/externs.h
+++ b/externs.h
 # include <bsd_auth.h>
 #endif /*BSD_AUTH*/
 
+/* include locale stuff for mailer "Content-Type": 
+ */
+#include <locale.h>
+#include <nl_types.h>
+#include <langinfo.h>
+
 #define DIR_T  struct dirent
 #define WAIT_T int
 #define SIG_T  sig_t
index a31bec54d04ac4d16ae42c556041d49ba3b5c222..2a8e14befe7bbdc2e0888a40983d8d1e44708ccc 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -66,7 +66,8 @@ XTRN int      NoFork INIT(0);
 XTRN int        PermitAnyCrontab INIT(0);
 XTRN int        ValidateMailRcpts INIT(0);
 XTRN char       MailCmd[MAX_COMMAND] INIT("");
-
+XTRN char       cron_default_mail_charset[MAX_ENVSTR] INIT("");
 #if DEBUGGING
 XTRN int       DebugFlags INIT(0);
 XTRN const char *DebugFlagNames[]