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

@@ -24,11 +24,11 @@
  */
 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
 /*        All Rights Reserved   */
 
 /*
- * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
+ * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
  */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/types.h>

@@ -70,14 +70,13 @@
 #define BADSHELL        \
         "because your login shell isn't /usr/bin/sh, you can't use cron."
 #define WARNSHELL       "warning: commands will be executed using /usr/bin/sh\n"
 #define BADUSAGE        \
         "usage:\n"                      \
-        "\tcrontab [file]\n"            \
-        "\tcrontab -e [username]\n"     \
-        "\tcrontab -l [username]\n"     \
-        "\tcrontab -r [username]"
+        "\tcrontab [-u username] [file]\n"              \
+        "\tcrontab [-u username] { -e | -l | -r }\n"    \
+        "\tcrontab { -e | -l | -r } [username]"
 #define INVALIDUSER     "you are not a valid user (no entry in /etc/passwd)."
 #define NOTALLOWED      "you are not authorized to use cron.  Sorry."
 #define NOTROOT         \
         "you must be super-user to access another user's crontab file"
 #define AUDITREJECT     "The audit context for your shell has not been set."

@@ -134,29 +133,30 @@
         uid_t ruid;
         pid_t pid;
         int stat_loc;
         int ret;
         char real_login[UNAMESIZE];
+        char *user = NULL;
         int tmpfd = -1;
         pam_handle_t *pamh;
         int pam_error;
         char *buf;
         size_t buflen;
 
         (void) setlocale(LC_ALL, "");
 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
-#define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
+#define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it wasn't */
 #endif
         (void) textdomain(TEXT_DOMAIN);
 
         if (init_yes() < 0) {
                 (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
                     strerror(errno));
                 exit(1);
         }
 
-        while ((c = getopt(argc, argv, "elr")) != EOF)
+        while ((c = getopt(argc, argv, "elru:")) != EOF) {
                 switch (c) {
                         case 'e':
                                 eflag++;
                                 break;
                         case 'l':

@@ -163,43 +163,57 @@
                                 lflag++;
                                 break;
                         case 'r':
                                 rflag++;
                                 break;
+                        case 'u':
+                                user = optarg;
+                                break;
                         case '?':
                                 errflg++;
                                 break;
                 }
+        }
 
+        argc -= optind;
+        argv += optind;
+
         if (eflag + lflag + rflag > 1)
                 errflg++;
 
-        argc -= optind;
-        argv += optind;
+        if ((eflag || lflag || rflag) && argc > 0) {
+                if (user != NULL)
+                        errflg++;
+                else
+                        user = *argv;
+        }
+
         if (errflg || argc > 1)
                 crabort(BADUSAGE);
 
         ruid = getuid();
         if ((pwp = getpwuid(ruid)) == NULL)
                 crabort(INVALIDUSER);
 
         if (strlcpy(real_login, pwp->pw_name, sizeof (real_login))
-            >= sizeof (real_login))
+            >= sizeof (real_login)) {
                 crabort(NAMETOOLONG);
+        }
 
-        if ((eflag || lflag || rflag) && argc == 1) {
-                if ((pwp = getpwnam(*argv)) == NULL)
+        if (user != NULL) {
+                if ((pwp = getpwnam(user)) == NULL)
                         crabort(INVALIDUSER);
 
                 if (!cron_admin(real_login)) {
                         if (pwp->pw_uid != ruid)
                                 crabort(NOTROOT);
                         else
                                 pp = getuser(ruid);
-                } else
-                        pp = *argv++;
         } else {
+                        pp = user;
+                }
+        } else {
                 pp = getuser(ruid);
         }
 
         if (pp == NULL) {
                 if (per_errno == 2)