4 * usually setuid root, -c option only works if getuid() == geteuid()
6 * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
7 * May be distributed under the GNU General Public License
9 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 to be used in busybox
25 #include <sys/ioctl.h>
28 #include <sys/resource.h>
31 #define CRONTABS "/var/spool/cron/crontabs"
34 #define TMPDIR "/var/spool/cron"
37 #define CRONUPDATE "cron.update"
40 #define PATH_VI "/usr/bin/vi" /* location of vi */
45 static const char *CDir = CRONTABS;
47 static void EditFile(const char *user, const char *file);
48 static int GetReplaceStream(const char *user, const char *file);
49 static int ChangeUser(const char *user, short dochdir);
52 crontab_main(int ac, char **av)
54 enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
55 const struct passwd *pas;
56 const char *repFile = NULL;
59 char caller[256]; /* user that ran program */
63 if ((pas = getpwuid(UserId)) == NULL)
64 perror_msg_and_die("getpwuid");
66 strncpy(caller, pas->pw_name, sizeof(caller));
70 if (av[1][0] == '-' && av[1][1] == 0) {
73 } else if (av[1][0] != '-') {
101 if (i + 1 < ac && av[i+1][0] != '-') {
103 if (getuid() == geteuid()) {
104 pas = getpwnam(av[i]);
106 UserId = pas->pw_uid;
108 error_msg_and_die("user %s unknown", av[i]);
111 error_msg_and_die("only the superuser may specify a user");
116 if (getuid() == geteuid()) {
117 CDir = (*ptr) ? ptr : av[++i];
119 error_msg_and_die("-c option: superuser only");
127 if (i != ac || option == NONE)
134 if ((pas = getpwuid(UserId)) == NULL)
135 perror_msg_and_die("getpwuid");
138 * If there is a replacement file, obtain a secure descriptor to it.
142 repFd = GetReplaceStream(caller, repFile);
144 error_msg_and_die("unable to read replacement file");
148 * Change directory to our crontab directory
152 perror_msg_and_die("cannot change dir to %s", CDir);
155 * Handle options as appropriate
164 if ((fi = fopen(pas->pw_name, "r"))) {
165 while (fgets(buf, sizeof(buf), fi) != NULL)
169 error_msg("no crontab for %s", pas->pw_name);
181 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
182 if ((fd = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) >= 0) {
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);
193 error_msg_and_die("unable to create %s", tmp);
206 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
207 if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) {
208 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
212 rename(path, pas->pw_name);
214 error_msg("unable to create %s/%s", CDir, path);
220 remove(pas->pw_name);
228 * Bump notification file. Handle window where crond picks file up
229 * before we can write our entry out.
232 if (option == REPLACE || option == DELETE) {
236 while ((fo = fopen(CRONUPDATE, "a"))) {
237 fprintf(fo, "%s\n", pas->pw_name);
239 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
247 error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
254 GetReplaceStream(const char *user, const char *file)
262 if (pipe(filedes) < 0) {
266 if ((pid = fork()) < 0) {
276 if (read(filedes[0], buf, 1) != 1) {
289 if (ChangeUser(user, 0) < 0)
292 fd = open(file, O_RDONLY);
294 error_msg("unable to open %s", file);
298 write(filedes[1], buf, 1);
299 while ((n = read(fd, buf, sizeof(buf))) > 0) {
300 write(filedes[1], buf, n);
306 EditFile(const char *user, const char *file)
310 if ((pid = fork()) == 0) {
312 * CHILD - change user and run editor
317 if (ChangeUser(user, 1) < 0)
319 if ((ptr = getenv("VISUAL")) == NULL || strlen(ptr) > 256)
322 snprintf(visual, sizeof(visual), "%s %s", ptr, file);
323 execl("/bin/sh", "/bin/sh", "-c", visual, NULL);
331 perror_msg_and_die("fork");
333 wait4(pid, NULL, 0, NULL);
337 log(const char *ctl, ...)
343 vsnprintf(buf, sizeof(buf), ctl, va);
344 syslog(LOG_NOTICE, "%s",buf );
349 ChangeUser(const char *user, short dochdir)
354 * Obtain password entry and change privilages
357 if ((pas = getpwnam(user)) == 0) {
358 log("failed to get uid for %s", user);
361 setenv("USER", pas->pw_name, 1);
362 setenv("HOME", pas->pw_dir, 1);
363 setenv("SHELL", "/bin/sh", 1);
366 * Change running state to the user in question
369 if (initgroups(user, pas->pw_gid) < 0) {
370 log("initgroups failed: %s %m", user);
373 if (setregid(pas->pw_gid, pas->pw_gid) < 0) {
374 log("setregid failed: %s %d", user, pas->pw_gid);
377 if (setreuid(pas->pw_uid, pas->pw_uid) < 0) {
378 log("setreuid failed: %s %d", user, pas->pw_uid);
382 if (chdir(pas->pw_dir) < 0) {
383 if (chdir(TMPDIR) < 0) {
384 log("chdir failed: %s %s", user, pas->pw_dir);
385 log("chdir failed: %s " TMPDIR, user);