Print this page
13066 Want crontab -u
Change-ID: I3dc2251dbbcc721aeff25a9dde21a24271c927bc

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/cron/crontab.c
          +++ new/usr/src/cmd/cron/crontab.c
↓ open down ↓ 18 lines elided ↑ open up ↑
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  26   26  /*        All Rights Reserved   */
  27   27  
  28   28  /*
  29      - * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
       29 + * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
  30   30   */
  31   31  
  32   32  #include <sys/types.h>
  33   33  #include <sys/stat.h>
  34   34  #include <sys/types.h>
  35   35  #include <sys/wait.h>
  36   36  #include <errno.h>
  37   37  #include <signal.h>
  38   38  #include <stdio.h>
  39   39  #include <stdlib.h>
↓ open down ↓ 25 lines elided ↑ open up ↑
  65   65  #define CRMODE          0600    /* mode for creating crontabs */
  66   66  
  67   67  #define BADCREATE       \
  68   68          "can't create your crontab file in the crontab directory."
  69   69  #define BADOPEN         "can't open your crontab file."
  70   70  #define BADSHELL        \
  71   71          "because your login shell isn't /usr/bin/sh, you can't use cron."
  72   72  #define WARNSHELL       "warning: commands will be executed using /usr/bin/sh\n"
  73   73  #define BADUSAGE        \
  74   74          "usage:\n"                      \
  75      -        "\tcrontab [file]\n"            \
  76      -        "\tcrontab -e [username]\n"     \
  77      -        "\tcrontab -l [username]\n"     \
  78      -        "\tcrontab -r [username]"
       75 +        "\tcrontab [-u username] [file]\n"              \
       76 +        "\tcrontab [-u username] { -e | -l | -r }\n"    \
       77 +        "\tcrontab { -e | -l | -r } [username]"
  79   78  #define INVALIDUSER     "you are not a valid user (no entry in /etc/passwd)."
  80   79  #define NOTALLOWED      "you are not authorized to use cron.  Sorry."
  81   80  #define NOTROOT         \
  82   81          "you must be super-user to access another user's crontab file"
  83   82  #define AUDITREJECT     "The audit context for your shell has not been set."
  84   83  #define EOLN            "unexpected end of line."
  85   84  #define UNEXPECT        "unexpected character found in line."
  86   85  #define OUTOFBOUND      "number out of bounds."
  87   86  #define OVERFLOW        "too many elements."
  88   87  #define ERRSFND         "errors detected in input, no crontab file generated."
↓ open down ↓ 40 lines elided ↑ open up ↑
 129  128          FILE *fp, *tmpfp;
 130  129          struct stat stbuf;
 131  130          struct passwd *pwp;
 132  131          time_t omodtime;
 133  132          char *editor;
 134  133          uid_t ruid;
 135  134          pid_t pid;
 136  135          int stat_loc;
 137  136          int ret;
 138  137          char real_login[UNAMESIZE];
      138 +        char *user = NULL;
 139  139          int tmpfd = -1;
 140  140          pam_handle_t *pamh;
 141  141          int pam_error;
 142  142          char *buf;
 143  143          size_t buflen;
 144  144  
 145  145          (void) setlocale(LC_ALL, "");
 146  146  #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 147      -#define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
      147 +#define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it wasn't */
 148  148  #endif
 149  149          (void) textdomain(TEXT_DOMAIN);
 150  150  
 151  151          if (init_yes() < 0) {
 152  152                  (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
 153  153                      strerror(errno));
 154  154                  exit(1);
 155  155          }
 156  156  
 157      -        while ((c = getopt(argc, argv, "elr")) != EOF)
      157 +        while ((c = getopt(argc, argv, "elru:")) != EOF) {
 158  158                  switch (c) {
 159  159                          case 'e':
 160  160                                  eflag++;
 161  161                                  break;
 162  162                          case 'l':
 163  163                                  lflag++;
 164  164                                  break;
 165  165                          case 'r':
 166  166                                  rflag++;
 167  167                                  break;
      168 +                        case 'u':
      169 +                                user = optarg;
      170 +                                break;
 168  171                          case '?':
 169  172                                  errflg++;
 170  173                                  break;
 171  174                  }
      175 +        }
 172  176  
      177 +        argc -= optind;
      178 +        argv += optind;
      179 +
 173  180          if (eflag + lflag + rflag > 1)
 174  181                  errflg++;
 175  182  
 176      -        argc -= optind;
 177      -        argv += optind;
      183 +        if ((eflag || lflag || rflag) && argc > 0) {
      184 +                if (user != NULL)
      185 +                        errflg++;
      186 +                else
      187 +                        user = *argv;
      188 +        }
      189 +
 178  190          if (errflg || argc > 1)
 179  191                  crabort(BADUSAGE);
 180  192  
 181  193          ruid = getuid();
 182  194          if ((pwp = getpwuid(ruid)) == NULL)
 183  195                  crabort(INVALIDUSER);
 184  196  
 185  197          if (strlcpy(real_login, pwp->pw_name, sizeof (real_login))
 186      -            >= sizeof (real_login))
      198 +            >= sizeof (real_login)) {
 187  199                  crabort(NAMETOOLONG);
      200 +        }
 188  201  
 189      -        if ((eflag || lflag || rflag) && argc == 1) {
 190      -                if ((pwp = getpwnam(*argv)) == NULL)
      202 +        if (user != NULL) {
      203 +                if ((pwp = getpwnam(user)) == NULL)
 191  204                          crabort(INVALIDUSER);
 192  205  
 193  206                  if (!cron_admin(real_login)) {
 194  207                          if (pwp->pw_uid != ruid)
 195  208                                  crabort(NOTROOT);
 196  209                          else
 197  210                                  pp = getuser(ruid);
 198      -                } else
 199      -                        pp = *argv++;
      211 +                } else {
      212 +                        pp = user;
      213 +                }
 200  214          } else {
 201  215                  pp = getuser(ruid);
 202  216          }
 203  217  
 204  218          if (pp == NULL) {
 205  219                  if (per_errno == 2)
 206  220                          crabort(BADSHELL);
 207  221                  else
 208  222                          crabort(INVALIDUSER);
 209  223          }
↓ open down ↓ 376 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX