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 */
45 if ((pas = getpwuid(UserId)) == NULL)
46 bb_perror_msg_and_die("getpwuid");
48 safe_strncpy(caller, pas->pw_name, sizeof(caller));
52 if (av[1][0] == '-' && av[1][1] == 0) {
55 } else if (av[1][0] != '-') {
83 if (i + 1 < ac && av[i+1][0] != '-') {
85 if (getuid() == geteuid()) {
86 pas = getpwnam(av[i]);
90 bb_error_msg_and_die("user %s unknown", av[i]);
93 bb_error_msg_and_die("only the superuser may specify a user");
98 if (getuid() == geteuid()) {
99 CDir = (*ptr) ? ptr : av[++i];
101 bb_error_msg_and_die("-c option: superuser only");
109 if (i != ac || option == NONE)
116 if ((pas = getpwuid(UserId)) == NULL)
117 bb_perror_msg_and_die("getpwuid");
120 * If there is a replacement file, obtain a secure descriptor to it.
124 repFd = GetReplaceStream(caller, repFile);
126 bb_error_msg_and_die("unable to read replacement file");
130 * Change directory to our crontab directory
136 * Handle options as appropriate
145 if ((fi = fopen(pas->pw_name, "r"))) {
146 while (fgets(buf, sizeof(buf), fi) != NULL)
150 bb_error_msg("no crontab for %s", pas->pw_name);
162 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
163 fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
164 chown(tmp, getuid(), getgid());
165 if ((fi = fopen(pas->pw_name, "r"))) {
166 while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
169 EditFile(caller, tmp);
183 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
184 if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) {
185 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
189 rename(path, pas->pw_name);
191 bb_error_msg("unable to create %s/%s", CDir, path);
197 remove(pas->pw_name);
205 * Bump notification file. Handle window where crond picks file up
206 * before we can write our entry out.
209 if (option == REPLACE || option == DELETE) {
213 while ((fo = fopen(CRONUPDATE, "a"))) {
214 fprintf(fo, "%s\n", pas->pw_name);
216 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
224 bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
230 static int GetReplaceStream(const char *user, const char *file)
238 if (pipe(filedes) < 0) {
242 if ((pid = fork()) < 0) {
252 if (read(filedes[0], buf, 1) != 1) {
265 if (ChangeUser(user, 0) < 0)
268 bb_default_error_retval = 0;
269 fd = xopen(file, O_RDONLY);
271 write(filedes[1], buf, 1);
272 while ((n = read(fd, buf, sizeof(buf))) > 0) {
273 write(filedes[1], buf, n);
278 static void EditFile(const char *user, const char *file)
282 if ((pid = fork()) == 0) {
284 * CHILD - change user and run editor
289 if (ChangeUser(user, 1) < 0)
291 if ((ptr = getenv("VISUAL")) == NULL || strlen(ptr) > 256)
294 snprintf(visual, sizeof(visual), "%s %s", ptr, file);
295 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL);
303 bb_perror_msg_and_die("fork");
305 wait4(pid, NULL, 0, NULL);
308 static int ChangeUser(const char *user, short dochdir)
313 * Obtain password entry and change privileges
316 if ((pas = getpwnam(user)) == NULL) {
317 bb_perror_msg_and_die("failed to get uid for %s", user);
320 setenv("USER", pas->pw_name, 1);
321 setenv("HOME", pas->pw_dir, 1);
322 setenv("SHELL", DEFAULT_SHELL, 1);
325 * Change running state to the user in question
327 change_identity(pas);
330 if (chdir(pas->pw_dir) < 0) {
331 bb_perror_msg("chdir failed: %s %s", user, pas->pw_dir);