hust testsuite: fix a false positive
[oweals/busybox.git] / miscutils / crontab.c
index 163e15dce1413123be56d2cb458e8c8385baa247..85c6623b26698effdd204cb4c9176ea8bac081f4 100644 (file)
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
+//usage:#define crontab_trivial_usage
+//usage:       "[-c DIR] [-u USER] [-ler]|[FILE]"
+//usage:#define crontab_full_usage "\n\n"
+//usage:       "       -c      Crontab directory"
+//usage:     "\n       -u      User"
+//usage:     "\n       -l      List crontab"
+//usage:     "\n       -e      Edit crontab"
+//usage:     "\n       -r      Delete crontab"
+//usage:     "\n       FILE    Replace crontab by FILE ('-': stdin)"
+
 #include "libbb.h"
 
 #define CRONTABS        CONFIG_FEATURE_CROND_DIR "/crontabs"
@@ -20,8 +30,9 @@
 static void edit_file(const struct passwd *pas, const char *file)
 {
        const char *ptr;
-       int pid = xvfork();
+       pid_t pid;
 
+       pid = xvfork();
        if (pid) { /* parent */
                wait4pid(pid);
                return;
@@ -30,7 +41,7 @@ static void edit_file(const struct passwd *pas, const char *file)
        /* CHILD - change user and run editor */
        /* initgroups, setgid, setuid */
        change_identity(pas);
-       setup_environment(DEFAULT_SHELL,
+       setup_environment(pas->pw_shell,
                        SETUP_ENV_CHANGEENV | SETUP_ENV_TO_TMP,
                        pas);
        ptr = getenv("VISUAL");
@@ -41,29 +52,7 @@ static void edit_file(const struct passwd *pas, const char *file)
        }
 
        BB_EXECLP(ptr, ptr, file, NULL);
-       bb_perror_msg_and_die("exec %s", ptr);
-}
-
-static int open_as_user(const struct passwd *pas, const char *file)
-{
-       pid_t pid;
-       char c;
-
-       pid = xvfork();
-       if (pid) { /* PARENT */
-               if (wait4pid(pid) == 0) {
-                       /* exitcode 0: child says it can read */
-                       return open(file, O_RDONLY);
-               }
-               return -1;
-       }
-
-       /* CHILD */
-       /* initgroups, setgid, setuid */
-       change_identity(pas);
-       /* We just try to read one byte. If it works, file is readable
-        * under this user. We signal that by exiting with 0. */
-       _exit(safe_read(xopen(file, O_RDONLY), &c, 1) < 0);
+       bb_perror_msg_and_die("can't execute '%s'", ptr);
 }
 
 int crontab_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -126,10 +115,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
                if (!argv[0])
                        bb_show_usage();
                if (NOT_LONE_DASH(argv[0])) {
-                       src_fd = open_as_user(pas, argv[0]);
-                       if (src_fd < 0)
-                               bb_error_msg_and_die("user %s cannot read %s",
-                                               pas->pw_name, argv[0]);
+                       src_fd = xopen_as_uid_gid(argv[0], O_RDONLY, pas->pw_uid, pas->pw_gid);
                }
        }
 
@@ -184,7 +170,6 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
                        unlink(tmp_fname);
                /*free(tmp_fname);*/
                /*free(new_fname);*/
-
        } /* switch */
 
        /* Bump notification file.  Handle window where crond picks file up