Print this page
374 cron should send more useful mail

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/cron/cron.c
          +++ new/usr/src/cmd/cron/cron.c
↓ open down ↓ 64 lines elided ↑ open up ↑
  65   65  #include <stdarg.h>
  66   66  #include <stdio.h>
  67   67  #include <stdlib.h>
  68   68  #include <string.h>
  69   69  #include <stropts.h>
  70   70  #include <time.h>
  71   71  #include <unistd.h>
  72   72  #include <libzoneinfo.h>
  73   73  
  74   74  #include "cron.h"
       75 +#include "cron_scf.h"
  75   76  
  76   77  /*
  77   78   * #define      DEBUG
  78   79   */
  79   80  
  80   81  #define MAIL            "/usr/bin/mail" /* mail program to use */
  81   82  #define CONSOLE         "/dev/console"  /* where messages go when cron dies */
  82   83  
  83   84  #define TMPINFILE       "/tmp/crinXXXXXX"  /* file to put stdin in for cmd  */
  84   85  #define TMPDIR          "/tmp"
↓ open down ↓ 54 lines elided ↑ open up ↑
 139  140  
 140  141  #define STDOUTERR       "one of your commands generated output or errors, \
 141  142  but cron was unable to mail you this output.\
 142  143  \nRemember to redirect standard output and standard \
 143  144  error for each of your commands."
 144  145  
 145  146  #define CLOCK_DRIFT     "clock time drifted backwards after event!\n"
 146  147  #define PIDERR          "unexpected pid returned %d (ignored)"
 147  148  #define CRONTABERR      "Subject: Your crontab file has an error in it\n\n"
 148  149  #define CRONOUT         "Subject: Output from \"cron\" command\n\n"
      150 +#define CRONOUTNEW      "Subject: Cron <%s@%s>: %s\n\n"
 149  151  #define MALLOCERR       "out of space, cannot create new string\n"
 150  152  
 151  153  #define DIDFORK didfork
 152  154  #define NOFORK !didfork
 153  155  
 154  156  #define MAILBUFLEN      (8*1024)
 155  157  #define LINELIMIT       80
 156  158  #define MAILBINITFREE   (MAILBUFLEN - (sizeof (cte_intro) - 1) \
 157  159              - (sizeof (cte_trail1) - 1) - (sizeof (cte_trail2) - 1) - 1)
 158  160  
↓ open down ↓ 191 lines elided ↑ open up ↑
 350  352   * to indicate the last time an event was executed.  This was (surely)
 351  353   * it's original intended use.
 352  354   */
 353  355  static time_t last_time, init_time, t_old;
 354  356  static int reset_needed; /* set to 1 when cron(1M) needs to re-initialize */
 355  357  
 356  358  static int              refresh;
 357  359  static sigset_t         defmask, sigmask;
 358  360  
 359  361  /*
      362 + * Configuration from smf(5)
      363 + */
      364 +static int legacy_subject;
      365 +static int extra_headers;
      366 +
      367 +/*
 360  368   * BSM hooks
 361  369   */
 362  370  extern int      audit_cron_session(char *, char *, uid_t, gid_t, char *);
 363  371  extern void     audit_cron_new_job(char *, int, void *);
 364  372  extern void     audit_cron_bad_user(char *);
 365  373  extern void     audit_cron_user_acct_expired(char *);
 366  374  extern int      audit_cron_create_anc_file(char *, char *, char *, uid_t);
 367  375  extern int      audit_cron_delete_anc_file(char *, char *);
 368  376  extern int      audit_cron_is_anc_name(char *);
 369  377  extern int      audit_cron_mode();
↓ open down ↓ 298 lines elided ↑ open up ↑
 668  676          /*NOTREACHED*/
 669  677  }
 670  678  
 671  679  static void
 672  680  initialize(int firstpass)
 673  681  {
 674  682  #ifdef DEBUG
 675  683          (void) fprintf(stderr, "in initialize\n");
 676  684  #endif
 677  685          if (firstpass) {
      686 +                int val;
 678  687                  /* for mail(1), make sure messages come from root */
 679  688                  if (putenv("LOGNAME=root") != 0) {
 680  689                          crabort("cannot expand env variable",
 681  690                              REMOVE_FIFO|CONSOLE_MSG);
 682  691                  }
 683  692                  if (access(FIFO, R_OK) == -1) {
 684  693                          if (errno == ENOENT) {
 685  694                                  if (mknod(FIFO, S_IFIFO|0600, 0) != 0)
 686  695                                          crabort("cannot create fifo queue",
 687  696                                              REMOVE_FIFO|CONSOLE_MSG);
↓ open down ↓ 10 lines elided ↑ open up ↑
 698  707                          if (NOFORK) {
 699  708                                  /* didn't fork... init(1M) is waiting */
 700  709                                  (void) sleep(60);
 701  710                                  /*
 702  711                                   * the wait is painful, but we don't want
 703  712                                   * init respawning this quickly
 704  713                                   */
 705  714                          }
 706  715                          crabort("cannot start cron; FIFO exists", CONSOLE_MSG);
 707  716                  }
      717 +                switch (init_scf()) {
      718 +                case 0:
      719 +                        val = get_config_boolean("legacy_subject");
      720 +                        legacy_subject = (val < 0 ? 0 : val);
      721 +                        val = get_config_boolean("extra_headers");
      722 +                        extra_headers = (val < 0 ? 1 : val);
      723 +                        break;
      724 +                case -2:
      725 +                        crabort("cron not running under smf(5)",
      726 +                            REMOVE_FIFO|CONSOLE_MSG);
      727 +                        break;
      728 +                default:
      729 +                        crabort("could not initialise libscf",
      730 +                            REMOVE_FIFO|CONSOLE_MSG);
      731 +                }
 708  732          }
 709  733  
 710  734          if ((msgfd = open(FIFO, O_RDWR)) < 0) {
 711  735                  perror("! open");
 712  736                  crabort("cannot open fifo queue", REMOVE_FIFO|CONSOLE_MSG);
 713  737          }
 714  738  
 715  739          init_time = time(NULL);
 716  740          el_init(8, init_time, (time_t)(60*60*24), 10);
 717  741  
↓ open down ↓ 1999 lines elided ↑ open up ↑
2717 2741                  exit(0);
2718 2742          (void) setuid(ruser_ids->pw_uid);
2719 2743  
2720 2744          cmd = xmalloc(strlen(MAIL) + strlen(p->name)+2);
2721 2745          (void) sprintf(cmd, "%s %s", MAIL, p->name);
2722 2746          mailpipe = popen(cmd, "w");
2723 2747          free(cmd);
2724 2748          if (mailpipe == NULL)
2725 2749                  exit(127);
2726 2750          (void) fprintf(mailpipe, "To: %s\n", p->name);
     2751 +        if (extra_headers) {
     2752 +                (void) fprintf(mailpipe, "X-Mailer: cron (%s %s)\n",
     2753 +                    name.sysname, name.release);
     2754 +                (void) fprintf(mailpipe, "X-Cron-User: %s\n", p->name);
     2755 +                (void) fprintf(mailpipe, "X-Cron-Host: %s\n", name.nodename);
     2756 +                (void) fprintf(mailpipe, "X-Cron-Job-Name: %s\n", pr->jobname);
     2757 +                (void) fprintf(mailpipe, "X-Cron-Job-Type: %s\n",
     2758 +                    (pr->jobtype == CRONEVENT ? "cron" : "at"));
     2759 +        }
2727 2760          if (pr->jobtype == CRONEVENT) {
2728      -                (void) fprintf(mailpipe, CRONOUT);
     2761 +                if (legacy_subject)
     2762 +                        (void) fprintf(mailpipe, CRONOUT);
     2763 +                else
     2764 +                        (void) fprintf(mailpipe, CRONOUTNEW,
     2765 +                            p->name, name.nodename, pr->jobname);
2729 2766                  (void) fprintf(mailpipe, "Your \"cron\" job on %s\n",
2730 2767                      name.nodename);
2731 2768                  if (pr->jobname != NULL) {
2732 2769                          (void) fprintf(mailpipe, "%s\n\n", pr->jobname);
2733 2770                  }
2734 2771          } else {
2735 2772                  (void) fprintf(mailpipe, "Subject: Output from \"at\" job\n\n");
2736 2773                  (void) fprintf(mailpipe, "Your \"at\" job on %s\n",
2737 2774                      name.nodename);
2738 2775                  if (pr->jobname != NULL) {
↓ open down ↓ 912 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX