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 void change_user(const struct passwd *pas)
30 setenv("USER", pas->pw_name, 1);
31 setenv("HOME", pas->pw_dir, 1);
32 setenv("SHELL", DEFAULT_SHELL, 1);
34 /* initgroups, setgid, setuid */
37 if (chdir(pas->pw_dir) < 0) {
38 bb_perror_msg("chdir(%s) by %s failed",
39 pas->pw_dir, pas->pw_name);
44 static void edit_file(const struct passwd *pas, const char *file)
49 if (pid < 0) /* failure */
50 bb_perror_msg_and_die("vfork");
51 if (pid) { /* parent */
56 /* CHILD - change user and run editor */
58 ptr = getenv("VISUAL");
60 ptr = getenv("EDITOR");
65 /* TODO: clean up the environment!!! */
66 /* not execlp - we won't use PATH */
67 execl(ptr, ptr, file, NULL);
68 bb_perror_msg_and_die("exec %s", ptr);
71 static int open_as_user(const struct passwd *pas, const char *file)
73 struct fd_pair filedes;
79 if (pid < 0) /* ERROR */
80 bb_perror_msg_and_die("vfork");
81 if (pid) { /* PARENT */
82 int n = safe_read(filedes.rd, &c, 1);
85 if (n > 0) /* child says it can read */
86 return open(file, O_RDONLY);
92 /* initgroups, setgid, setuid */
95 /* We just try to read one byte. If that works, file is readable
96 * under this user. We signal that by sending one byte to parent. */
97 if (safe_read(xopen(file, O_RDONLY), &c, 1) == 1)
98 safe_write(filedes.wr, &c, 1); /* "papa, I can read!" */
102 int crontab_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
103 int crontab_main(int argc, char **argv)
105 const struct passwd *pas;
106 const char *crontab_dir = CRONTABS;
109 char *user_name; /* -u USER */
114 /* file [opts] Replace crontab from file
115 * - [opts] Replace crontab from stdin
117 * -c dir Crontab directory
118 * -l List crontab for user
119 * -e Edit crontab for user
120 * -r Delete crontab for user
121 * bbox also supports -d == -r, but most other crontab
122 * implementations do not. Deprecated.
130 OPT_ler = OPT_l + OPT_e + OPT_r,
135 opt_complementary = "?1:dr"; /* max one argument; -d implies -r */
136 opt_ler = getopt32(argv, "u:c:lerd", &user_name, &crontab_dir);
139 if (opt_ler & (OPT_u|OPT_c))
140 if (my_uid != geteuid())
141 bb_error_msg_and_die("only root can use -c or -u");
143 if (opt_ler & OPT_u) {
144 pas = getpwnam(user_name);
146 bb_error_msg_and_die("user %s is not known", user_name);
147 my_uid = pas->pw_uid;
149 pas = getpwuid(my_uid);
151 bb_perror_msg_and_die("no user record for UID %u",
155 #define user_name DONT_USE_ME_BEYOND_THIS_POINT
156 #define my_uid DONT_USE_ME_BEYOND_THIS_POINT
158 /* From now on, keep only -l, -e, -r bits */
160 if ((opt_ler - 1) & opt_ler) /* more than one bit set? */
163 /* Read replacement file under user's UID/GID/group vector */
164 if (!opt_ler) { /* Replace? */
167 if (NOT_LONE_DASH(argv[0])) {
168 fd = open_as_user(pas, argv[0]);
170 bb_error_msg_and_die("user %s cannot read %s",
171 pas->pw_name, argv[0]);
172 xmove_fd(fd, STDIN_FILENO);
176 /* cd to our crontab directory */
181 /* Handle requested operation */
184 default: /* case OPT_r: Delete */
185 remove(pas->pw_name);
188 case OPT_l: /* List */
190 char *args[2] = { pas->pw_name, NULL };
193 * the rest go play with cron update file */
196 case OPT_e: /* Edit */
197 tmp_fname = xasprintf(TMPDIR "/crontab.%u", (unsigned)getpid());
198 fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
199 xmove_fd(fd, STDIN_FILENO);
200 fd = open(pas->pw_name, O_RDONLY);
202 bb_copyfd_eof(fd, STDIN_FILENO);
205 fchown(STDIN_FILENO, pas->pw_uid, pas->pw_gid);
206 edit_file(pas, tmp_fname);
207 xlseek(STDIN_FILENO, 0, SEEK_SET);
210 case 0: /* Replace (no -l, -e, or -r were given) */
211 new_fname = xasprintf("%s.new", pas->pw_name);
212 fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600);
214 bb_copyfd_eof(STDIN_FILENO, fd);
216 rename(new_fname, pas->pw_name);
218 bb_error_msg("cannot create %s/%s",
219 crontab_dir, new_fname);
228 /* Bump notification file. Handle window where crond picks file up
229 * before we can write our entry out.
231 while ((fd = open(CRONUPDATE, O_WRONLY|O_CREAT|O_APPEND)) >= 0) {
234 fdprintf(fd, "%s\n", pas->pw_name);
235 if (fstat(fd, &st) != 0 || st.st_nlink != 0) {
240 * file was deleted, maybe crond missed our notification */
245 bb_error_msg("cannot append to %s/%s",
246 crontab_dir, CRONUPDATE);