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.
16 #define CRONTABS "/var/spool/cron/crontabs"
19 #define TMPDIR "/var/spool/cron"
22 #define CRONUPDATE "cron.update"
25 #define PATH_VI "/bin/vi" /* location of vi */
28 static const char *CDir = CRONTABS;
30 static void EditFile(const char *user, const char *file);
31 static int GetReplaceStream(const char *user, const char *file);
32 static int ChangeUser(const char *user, short dochdir);
34 int crontab_main(int ac, char **av);
35 int crontab_main(int ac, char **av)
37 enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
38 const struct passwd *pas;
39 const char *repFile = NULL;
42 char caller[256]; /* user that ran program */
47 pas = getpwuid(UserId);
49 bb_perror_msg_and_die("getpwuid");
51 safe_strncpy(caller, pas->pw_name, sizeof(caller));
55 if (LONE_DASH(av[1])) {
58 } else if (av[1][0] != '-') {
86 if (i + 1 < ac && av[i+1][0] != '-') {
88 if (getuid() == geteuid()) {
89 pas = getpwnam(av[i]);
93 bb_error_msg_and_die("user %s unknown", av[i]);
96 bb_error_msg_and_die("only the superuser may specify a user");
101 if (getuid() == geteuid()) {
102 CDir = (*ptr) ? ptr : av[++i];
104 bb_error_msg_and_die("-c option: superuser only");
112 if (i != ac || option == NONE)
119 pas = getpwuid(UserId);
121 bb_perror_msg_and_die("getpwuid");
124 * If there is a replacement file, obtain a secure descriptor to it.
128 repFd = GetReplaceStream(caller, repFile);
130 bb_error_msg_and_die("cannot read replacement file");
134 * Change directory to our crontab directory
140 * Handle options as appropriate
148 fi = fopen(pas->pw_name, "r");
150 while (fgets(buf, sizeof(buf), fi) != NULL)
154 bb_error_msg("no crontab for %s", pas->pw_name);
160 /* FIXME: messy code here! we have file copying helpers for this! */
166 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
167 fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
168 /* race, use fchown */
169 chown(tmp, getuid(), getgid());
170 fi = fopen(pas->pw_name, "r");
172 while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
173 full_write(fd, buf, n);
175 EditFile(caller, tmp);
177 lseek(fd, 0L, SEEK_SET);
189 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
190 fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
192 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
193 full_write(fd, buf, n);
196 rename(path, pas->pw_name);
198 bb_error_msg("cannot create %s/%s", CDir, path);
204 remove(pas->pw_name);
212 * Bump notification file. Handle window where crond picks file up
213 * before we can write our entry out.
216 if (option == REPLACE || option == DELETE) {
220 while ((fo = fopen(CRONUPDATE, "a"))) {
221 fprintf(fo, "%s\n", pas->pw_name);
223 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
231 bb_error_msg("cannot append to %s/%s", CDir, CRONUPDATE);
237 static int GetReplaceStream(const char *user, const char *file)
245 if (pipe(filedes) < 0) {
260 if (read(filedes[0], buf, 1) != 1) {
273 if (ChangeUser(user, 0) < 0)
276 xfunc_error_retval = 0;
277 fd = xopen(file, O_RDONLY);
279 write(filedes[1], buf, 1);
280 while ((n = read(fd, buf, sizeof(buf))) > 0) {
281 write(filedes[1], buf, n);
286 static void EditFile(const char *user, const char *file)
292 * CHILD - change user and run editor
296 if (ChangeUser(user, 1) < 0)
298 ptr = getenv("VISUAL");
299 if (ptr == NULL || strlen(ptr) > 256)
302 ptr = xasprintf("%s %s", ptr, file);
303 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", ptr, NULL);
304 bb_perror_msg_and_die("exec");
310 bb_perror_msg_and_die("fork");
312 wait4(pid, NULL, 0, NULL);
315 static int ChangeUser(const char *user, short dochdir)
320 * Obtain password entry and change privileges
323 pas = getpwnam(user);
325 bb_perror_msg_and_die("failed to get uid for %s", user);
327 setenv("USER", pas->pw_name, 1);
328 setenv("HOME", pas->pw_dir, 1);
329 setenv("SHELL", DEFAULT_SHELL, 1);
332 * Change running state to the user in question
334 change_identity(pas);
337 if (chdir(pas->pw_dir) < 0) {
338 bb_perror_msg("chdir(%s) by %s failed", pas->pw_dir, user);