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)
36 enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
37 const struct passwd *pas;
38 const char *repFile = NULL;
41 char caller[256]; /* user that ran program */
46 pas = getpwuid(UserId);
48 bb_perror_msg_and_die("getpwuid");
50 safe_strncpy(caller, pas->pw_name, sizeof(caller));
54 if (LONE_DASH(av[1])) {
57 } else if (av[1][0] != '-') {
85 if (i + 1 < ac && av[i+1][0] != '-') {
87 if (getuid() == geteuid()) {
88 pas = getpwnam(av[i]);
92 bb_error_msg_and_die("user %s unknown", av[i]);
95 bb_error_msg_and_die("only the superuser may specify a user");
100 if (getuid() == geteuid()) {
101 CDir = (*ptr) ? ptr : av[++i];
103 bb_error_msg_and_die("-c option: superuser only");
111 if (i != ac || option == NONE)
118 pas = getpwuid(UserId);
120 bb_perror_msg_and_die("getpwuid");
123 * If there is a replacement file, obtain a secure descriptor to it.
127 repFd = GetReplaceStream(caller, repFile);
129 bb_error_msg_and_die("cannot read replacement file");
133 * Change directory to our crontab directory
139 * Handle options as appropriate
147 fi = fopen(pas->pw_name, "r");
149 while (fgets(buf, sizeof(buf), fi) != NULL)
153 bb_error_msg("no crontab for %s", pas->pw_name);
159 /* FIXME: messy code here! we have file copying helpers for this! */
165 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
166 fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
167 /* race, use fchown */
168 chown(tmp, getuid(), getgid());
169 fi = fopen(pas->pw_name, "r");
171 while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
172 full_write(fd, buf, n);
174 EditFile(caller, tmp);
176 lseek(fd, 0L, SEEK_SET);
188 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
189 fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
191 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
192 full_write(fd, buf, n);
195 rename(path, pas->pw_name);
197 bb_error_msg("cannot create %s/%s", CDir, path);
203 remove(pas->pw_name);
211 * Bump notification file. Handle window where crond picks file up
212 * before we can write our entry out.
215 if (option == REPLACE || option == DELETE) {
219 while ((fo = fopen(CRONUPDATE, "a"))) {
220 fprintf(fo, "%s\n", pas->pw_name);
222 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
230 bb_error_msg("cannot append to %s/%s", CDir, CRONUPDATE);
236 static int GetReplaceStream(const char *user, const char *file)
244 if (pipe(filedes) < 0) {
259 if (read(filedes[0], buf, 1) != 1) {
272 if (ChangeUser(user, 0) < 0)
275 xfunc_error_retval = 0;
276 fd = xopen(file, O_RDONLY);
278 write(filedes[1], buf, 1);
279 while ((n = read(fd, buf, sizeof(buf))) > 0) {
280 write(filedes[1], buf, n);
285 static void EditFile(const char *user, const char *file)
291 * CHILD - change user and run editor
295 if (ChangeUser(user, 1) < 0)
297 ptr = getenv("VISUAL");
298 if (ptr == NULL || strlen(ptr) > 256)
301 ptr = xasprintf("%s %s", ptr, file);
302 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", ptr, NULL);
303 bb_perror_msg_and_die("exec");
309 bb_perror_msg_and_die("fork");
311 wait4(pid, NULL, 0, NULL);
314 static int ChangeUser(const char *user, short dochdir)
319 * Obtain password entry and change privileges
322 pas = getpwnam(user);
324 bb_perror_msg_and_die("failed to get uid for %s", user);
326 setenv("USER", pas->pw_name, 1);
327 setenv("HOME", pas->pw_dir, 1);
328 setenv("SHELL", DEFAULT_SHELL, 1);
331 * Change running state to the user in question
333 change_identity(pas);
336 if (chdir(pas->pw_dir) < 0) {
337 bb_perror_msg("chdir(%s) by %s failed", pas->pw_dir, user);