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 * May be distributed under the GNU General Public License
10 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 to be used in busybox
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 */
46 static const char *CDir = CRONTABS;
48 static void EditFile(const char *user, const char *file);
49 static int GetReplaceStream(const char *user, const char *file);
50 static int ChangeUser(const char *user, short dochdir);
53 crontab_main(int ac, char **av)
55 enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
56 const struct passwd *pas;
57 const char *repFile = NULL;
60 char caller[256]; /* user that ran program */
64 if ((pas = getpwuid(UserId)) == NULL)
65 bb_perror_msg_and_die("getpwuid");
67 strncpy(caller, pas->pw_name, sizeof(caller));
71 if (av[1][0] == '-' && av[1][1] == 0) {
74 } else if (av[1][0] != '-') {
102 if (i + 1 < ac && av[i+1][0] != '-') {
104 if (getuid() == geteuid()) {
105 pas = getpwnam(av[i]);
107 UserId = pas->pw_uid;
109 bb_error_msg_and_die("user %s unknown", av[i]);
112 bb_error_msg_and_die("only the superuser may specify a user");
117 if (getuid() == geteuid()) {
118 CDir = (*ptr) ? ptr : av[++i];
120 bb_error_msg_and_die("-c option: superuser only");
128 if (i != ac || option == NONE)
135 if ((pas = getpwuid(UserId)) == NULL)
136 bb_perror_msg_and_die("getpwuid");
139 * If there is a replacement file, obtain a secure descriptor to it.
143 repFd = GetReplaceStream(caller, repFile);
145 bb_error_msg_and_die("unable to read replacement file");
149 * Change directory to our crontab directory
155 * Handle options as appropriate
164 if ((fi = fopen(pas->pw_name, "r"))) {
165 while (fgets(buf, sizeof(buf), fi) != NULL)
169 bb_error_msg("no crontab for %s", pas->pw_name);
181 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
182 fd = bb_xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
183 chown(tmp, getuid(), getgid());
184 if ((fi = fopen(pas->pw_name, "r"))) {
185 while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
188 EditFile(caller, tmp);
202 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
203 if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) {
204 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
208 rename(path, pas->pw_name);
210 bb_error_msg("unable to create %s/%s", CDir, path);
216 remove(pas->pw_name);
224 * Bump notification file. Handle window where crond picks file up
225 * before we can write our entry out.
228 if (option == REPLACE || option == DELETE) {
232 while ((fo = fopen(CRONUPDATE, "a"))) {
233 fprintf(fo, "%s\n", pas->pw_name);
235 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
243 bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
250 GetReplaceStream(const char *user, const char *file)
258 if (pipe(filedes) < 0) {
262 if ((pid = fork()) < 0) {
272 if (read(filedes[0], buf, 1) != 1) {
285 if (ChangeUser(user, 0) < 0)
288 bb_default_error_retval = 0;
289 fd = bb_xopen3(file, O_RDONLY, 0);
291 write(filedes[1], buf, 1);
292 while ((n = read(fd, buf, sizeof(buf))) > 0) {
293 write(filedes[1], buf, n);
299 EditFile(const char *user, const char *file)
303 if ((pid = fork()) == 0) {
305 * CHILD - change user and run editor
310 if (ChangeUser(user, 1) < 0)
312 if ((ptr = getenv("VISUAL")) == NULL || strlen(ptr) > 256)
315 snprintf(visual, sizeof(visual), "%s %s", ptr, file);
316 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL);
324 bb_perror_msg_and_die("fork");
326 wait4(pid, NULL, 0, NULL);
330 ChangeUser(const char *user, short dochdir)
335 * Obtain password entry and change privileges
338 if ((pas = getpwnam(user)) == NULL) {
339 bb_perror_msg_and_die("failed to get uid for %s", user);
342 setenv("USER", pas->pw_name, 1);
343 setenv("HOME", pas->pw_dir, 1);
344 setenv("SHELL", DEFAULT_SHELL, 1);
347 * Change running state to the user in question
349 change_identity(pas);
352 if (chdir(pas->pw_dir) < 0) {
353 bb_perror_msg("chdir failed: %s %s", user, pas->pw_dir);