X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=miscutils%2Fcrontab.c;h=bad7f0018b22254343bc7450a8bb7f5a4d32cc6c;hb=4ae2f511cf6a02fed6752877c62aaf8fab6f26da;hp=5bd7db6e5ddc9c740d91d1ac0b1591c0b0e104e2;hpb=5a7ec22c493a02106d892effeef78e65a4e6b4a0;p=oweals%2Fbusybox.git diff --git a/miscutils/crontab.c b/miscutils/crontab.c index 5bd7db6e5..bad7f0018 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c @@ -1,13 +1,13 @@ +/* vi: set sw=4 ts=4: */ /* * CRONTAB * * usually setuid root, -c option only works if getuid() == geteuid() * * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) - * May be distributed under the GNU General Public License - * - * Vladimir Oleynik (C) 2002 to be used in busybox + * Vladimir Oleynik (C) 2002 * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ #include @@ -37,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" @@ -61,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)); @@ -105,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; @@ -116,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: @@ -125,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. @@ -141,15 +141,14 @@ 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"); } /* * Change directory to our crontab directory */ - if (chdir(CDir) < 0) - perror_msg_and_die("cannot change dir to %s", CDir); + bb_xchdir(CDir); /* * Handle options as appropriate @@ -166,7 +165,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; @@ -179,20 +178,16 @@ crontab_main(int ac, char **av) char buf[1024]; snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); - if ((fd = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) >= 0) { - chown(tmp, getuid(), getgid()); - if ((fi = fopen(pas->pw_name, "r"))) { - while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) - write(fd, buf, n); - } - EditFile(caller, tmp); - remove(tmp); - lseek(fd, 0L, 0); - repFd = fd; - } else { - error_msg_and_die("unable to create %s", tmp); + fd = bb_xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600); + chown(tmp, getuid(), getgid()); + if ((fi = fopen(pas->pw_name, "r"))) { + while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) + write(fd, buf, n); } - + EditFile(caller, tmp); + remove(tmp); + lseek(fd, 0L, 0); + repFd = fd; } option = REPLACE; /* fall through */ @@ -211,7 +206,7 @@ crontab_main(int ac, char **av) close(fd); rename(path, pas->pw_name); } else { - error_msg("unable to create %s/%s", CDir, path); + bb_error_msg("unable to create %s/%s", CDir, path); } close(repFd); } @@ -244,7 +239,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; @@ -289,11 +284,8 @@ GetReplaceStream(const char *user, const char *file) if (ChangeUser(user, 0) < 0) exit(0); - fd = open(file, O_RDONLY); - if (fd < 0) { - error_msg("unable to open %s", file); - exit(0); - } + bb_default_error_retval = 0; + fd = bb_xopen3(file, O_RDONLY, 0); buf[0] = 0; write(filedes[1], buf, 1); while ((n = read(fd, buf, sizeof(buf))) > 0) { @@ -320,7 +312,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); } @@ -328,63 +320,37 @@ 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) { struct passwd *pas; /* - * Obtain password entry and change privilages + * Obtain password entry and change privileges */ - if ((pas = getpwnam(user)) == 0) { - log("failed to get uid for %s", user); + if ((pas = getpwnam(user)) == NULL) { + 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) { - if (chdir(TMPDIR) < 0) { - log("chdir failed: %s %s", user, pas->pw_dir); - log("chdir failed: %s " TMPDIR, user); - return(-1); - } + bb_perror_msg("chdir failed: %s %s", user, pas->pw_dir); + bb_xchdir(TMPDIR); } } return(pas->pw_uid);