1 /* vi: set sw=4 ts=4: */
5 * usually setuid root, -c option only works if getuid() == geteuid()
7 * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
8 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
10 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
26 #include <sys/ioctl.h>
29 #include <sys/resource.h>
32 #define CRONTABS "/var/spool/cron/crontabs"
35 #define TMPDIR "/var/spool/cron"
38 #define CRONUPDATE "cron.update"
41 #define PATH_VI "/bin/vi" /* location of vi */
44 static const char *CDir = CRONTABS;
46 static void EditFile(const char *user, const char *file);
47 static int GetReplaceStream(const char *user, const char *file);
48 static int ChangeUser(const char *user, short dochdir);
51 crontab_main(int ac, char **av)
53 enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
54 const struct passwd *pas;
55 const char *repFile = NULL;
58 char caller[256]; /* user that ran program */
62 if ((pas = getpwuid(UserId)) == NULL)
63 bb_perror_msg_and_die("getpwuid");
65 strncpy(caller, pas->pw_name, sizeof(caller));
69 if (av[1][0] == '-' && av[1][1] == 0) {
72 } else if (av[1][0] != '-') {
100 if (i + 1 < ac && av[i+1][0] != '-') {
102 if (getuid() == geteuid()) {
103 pas = getpwnam(av[i]);
105 UserId = pas->pw_uid;
107 bb_error_msg_and_die("user %s unknown", av[i]);
110 bb_error_msg_and_die("only the superuser may specify a user");
115 if (getuid() == geteuid()) {
116 CDir = (*ptr) ? ptr : av[++i];
118 bb_error_msg_and_die("-c option: superuser only");
126 if (i != ac || option == NONE)
133 if ((pas = getpwuid(UserId)) == NULL)
134 bb_perror_msg_and_die("getpwuid");
137 * If there is a replacement file, obtain a secure descriptor to it.
141 repFd = GetReplaceStream(caller, repFile);
143 bb_error_msg_and_die("unable to read replacement file");
147 * Change directory to our crontab directory
153 * Handle options as appropriate
162 if ((fi = fopen(pas->pw_name, "r"))) {
163 while (fgets(buf, sizeof(buf), fi) != NULL)
167 bb_error_msg("no crontab for %s", pas->pw_name);
179 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
180 fd = bb_xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
181 chown(tmp, getuid(), getgid());
182 if ((fi = fopen(pas->pw_name, "r"))) {
183 while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
186 EditFile(caller, tmp);
200 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
201 if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) {
202 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
206 rename(path, pas->pw_name);
208 bb_error_msg("unable to create %s/%s", CDir, path);
214 remove(pas->pw_name);
222 * Bump notification file. Handle window where crond picks file up
223 * before we can write our entry out.
226 if (option == REPLACE || option == DELETE) {
230 while ((fo = fopen(CRONUPDATE, "a"))) {
231 fprintf(fo, "%s\n", pas->pw_name);
233 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
241 bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
248 GetReplaceStream(const char *user, const char *file)
256 if (pipe(filedes) < 0) {
260 if ((pid = fork()) < 0) {
270 if (read(filedes[0], buf, 1) != 1) {
283 if (ChangeUser(user, 0) < 0)
286 bb_default_error_retval = 0;
287 fd = bb_xopen3(file, O_RDONLY, 0);
289 write(filedes[1], buf, 1);
290 while ((n = read(fd, buf, sizeof(buf))) > 0) {
291 write(filedes[1], buf, n);
297 EditFile(const char *user, const char *file)
301 if ((pid = fork()) == 0) {
303 * CHILD - change user and run editor
308 if (ChangeUser(user, 1) < 0)
310 if ((ptr = getenv("VISUAL")) == NULL || strlen(ptr) > 256)
313 snprintf(visual, sizeof(visual), "%s %s", ptr, file);
314 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL);
322 bb_perror_msg_and_die("fork");
324 wait4(pid, NULL, 0, NULL);
328 ChangeUser(const char *user, short dochdir)
333 * Obtain password entry and change privileges
336 if ((pas = getpwnam(user)) == NULL) {
337 bb_perror_msg_and_die("failed to get uid for %s", user);
340 setenv("USER", pas->pw_name, 1);
341 setenv("HOME", pas->pw_dir, 1);
342 setenv("SHELL", DEFAULT_SHELL, 1);
345 * Change running state to the user in question
347 change_identity(pas);
350 if (chdir(pas->pw_dir) < 0) {
351 bb_perror_msg("chdir failed: %s %s", user, pas->pw_dir);