Patch from David Meggy to make the swap default to the new version if no
[oweals/busybox.git] / miscutils / crontab.c
index c869906532d580aa0e798534af81aa7fb8cfe94e..52d08dda3a5de5fca494f4be3003137d76a8b302 100644 (file)
@@ -18,9 +18,7 @@
 #include <time.h>
 #include <dirent.h>
 #include <fcntl.h>
-#include <pwd.h>
 #include <unistd.h>
-#include <grp.h>
 #include <syslog.h>
 #include <signal.h>
 #include <getopt.h>
@@ -39,7 +37,7 @@
 #define CRONUPDATE      "cron.update"
 #endif
 #ifndef PATH_VI
-#define PATH_VI         "/usr/bin/vi"   /* location of vi       */
+#define PATH_VI         "/bin/vi"   /* location of vi       */
 #endif
 
 #include "busybox.h"
@@ -63,7 +61,7 @@ crontab_main(int ac, char **av)
 
     UserId = getuid();
     if ((pas = getpwuid(UserId)) == NULL)
-       perror_msg_and_die("getpwuid");
+       bb_perror_msg_and_die("getpwuid");
 
     strncpy(caller, pas->pw_name, sizeof(caller));
 
@@ -107,10 +105,10 @@ crontab_main(int ac, char **av)
                    if (pas) {
                        UserId = pas->pw_uid;
                    } else {
-                       error_msg_and_die("user %s unknown", av[i]);
+                       bb_error_msg_and_die("user %s unknown", av[i]);
                    }
                } else {
-                   error_msg_and_die("only the superuser may specify a user");
+                   bb_error_msg_and_die("only the superuser may specify a user");
                }
            }
            break;
@@ -118,7 +116,7 @@ crontab_main(int ac, char **av)
            if (getuid() == geteuid()) {
                CDir = (*ptr) ? ptr : av[++i];
            } else {
-               error_msg_and_die("-c option: superuser only");
+               bb_error_msg_and_die("-c option: superuser only");
            }
            break;
        default:
@@ -127,14 +125,14 @@ crontab_main(int ac, char **av)
        }
     }
     if (i != ac || option == NONE)
-       show_usage();
+       bb_show_usage();
 
     /*
      * Get password entry
      */
 
     if ((pas = getpwuid(UserId)) == NULL)
-       perror_msg_and_die("getpwuid");
+       bb_perror_msg_and_die("getpwuid");
 
     /*
      * If there is a replacement file, obtain a secure descriptor to it.
@@ -143,7 +141,7 @@ crontab_main(int ac, char **av)
     if (repFile) {
        repFd = GetReplaceStream(caller, repFile);
        if (repFd < 0)
-           error_msg_and_die("unable to read replacement file");
+           bb_error_msg_and_die("unable to read replacement file");
     }
 
     /*
@@ -151,7 +149,7 @@ crontab_main(int ac, char **av)
      */
 
     if (chdir(CDir) < 0)
-       perror_msg_and_die("cannot change dir to %s", CDir);
+       bb_perror_msg_and_die("cannot change dir to %s", CDir);
 
     /*
      * Handle options as appropriate
@@ -168,7 +166,7 @@ crontab_main(int ac, char **av)
                    fputs(buf, stdout);
                fclose(fi);
            } else {
-               error_msg("no crontab for %s", pas->pw_name);
+               bb_error_msg("no crontab for %s", pas->pw_name);
            }
        }
        break;
@@ -192,7 +190,7 @@ crontab_main(int ac, char **av)
                lseek(fd, 0L, 0);
                repFd = fd;
            } else {
-               error_msg_and_die("unable to create %s", tmp);
+               bb_error_msg_and_die("unable to create %s", tmp);
            }
 
        }
@@ -206,14 +204,14 @@ crontab_main(int ac, char **av)
            int n;
 
            snprintf(path, sizeof(path), "%s.new", pas->pw_name);
-           if ((fd = open(path, O_CREAT|O_TRUNC|O_EXCL|O_APPEND|O_WRONLY, 0600)) >= 0) {
+           if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) {
                while ((n = read(repFd, buf, sizeof(buf))) > 0) {
                    write(fd, buf, n);
                }
                close(fd);
                rename(path, pas->pw_name);
            } else {
-               error_msg("unable to create %s/%s", CDir, buf);
+               bb_error_msg("unable to create %s/%s", CDir, path);
            }
            close(repFd);
        }
@@ -246,7 +244,7 @@ crontab_main(int ac, char **av)
            /* loop */
        }
        if (fo == NULL) {
-           error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
+           bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
        }
     }
     return 0;
@@ -293,7 +291,7 @@ GetReplaceStream(const char *user, const char *file)
 
     fd = open(file, O_RDONLY);
     if (fd < 0) {
-       error_msg("unable to open %s", file);
+       bb_error_msg("unable to open %s", file);
        exit(0);
     }
     buf[0] = 0;
@@ -322,7 +320,7 @@ EditFile(const char *user, const char *file)
            ptr = PATH_VI;
 
        snprintf(visual, sizeof(visual), "%s %s", ptr, file);
-       execl("/bin/sh", "/bin/sh", "-c", visual, NULL);
+       execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL);
        perror("exec");
        exit(0);
     }
@@ -330,23 +328,11 @@ EditFile(const char *user, const char *file)
        /*
         * PARENT - failure
         */
-       perror_msg_and_die("fork");
+       bb_perror_msg_and_die("fork");
     }
     wait4(pid, NULL, 0, NULL);
 }
 
-static void
-log(const char *ctl, ...)
-{
-    va_list va;
-    char buf[1024];
-
-    va_start(va, ctl);
-    vsnprintf(buf, sizeof(buf), ctl, va);
-    syslog(LOG_NOTICE, "%s",buf );
-    va_end(va);
-}
-
 static int
 ChangeUser(const char *user, short dochdir)
 {
@@ -357,34 +343,23 @@ ChangeUser(const char *user, short dochdir)
      */
 
     if ((pas = getpwnam(user)) == 0) {
-       log("failed to get uid for %s", user);
+       bb_perror_msg_and_die("failed to get uid for %s", user);
        return(-1);
     }
     setenv("USER", pas->pw_name, 1);
     setenv("HOME", pas->pw_dir, 1);
-    setenv("SHELL", "/bin/sh", 1);
+    setenv("SHELL", DEFAULT_SHELL, 1);
 
     /*
      * Change running state to the user in question
      */
+    change_identity(pas);
 
-    if (initgroups(user, pas->pw_gid) < 0) {
-       log("initgroups failed: %s %m", user);
-       return(-1);
-    }
-    if (setregid(pas->pw_gid, pas->pw_gid) < 0) {
-       log("setregid failed: %s %d", user, pas->pw_gid);
-       return(-1);
-    }
-    if (setreuid(pas->pw_uid, pas->pw_uid) < 0) {
-       log("setreuid failed: %s %d", user, pas->pw_uid);
-       return(-1);
-    }
     if (dochdir) {
        if (chdir(pas->pw_dir) < 0) {
+           bb_perror_msg_and_die("chdir failed: %s %s", user, pas->pw_dir);
            if (chdir(TMPDIR) < 0) {
-               log("chdir failed: %s %s", user, pas->pw_dir);
-               log("chdir failed: %s " TMPDIR, user);
+               bb_perror_msg_and_die("chdir failed: %s %s", user, TMPDIR);
                return(-1);
            }
        }