blob: a3bfd495dd1e1566c82caf6db43b63330b610be5 [file] [log] [blame]
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +00002/*
3 * CRONTAB
4 *
5 * usually setuid root, -c option only works if getuid() == geteuid()
6 *
7 * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
Mike Frysingerf284c762006-04-16 20:38:26 +00008 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
Eric Andersenf6f7bfb2002-10-22 12:24:59 +00009 *
Mike Frysingerf284c762006-04-16 20:38:26 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000011 */
12
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000013#include "busybox.h"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000014
15#ifndef CRONTABS
16#define CRONTABS "/var/spool/cron/crontabs"
17#endif
18#ifndef TMPDIR
19#define TMPDIR "/var/spool/cron"
20#endif
21#ifndef CRONUPDATE
22#define CRONUPDATE "cron.update"
23#endif
24#ifndef PATH_VI
Denis Vlasenko94d5d822006-09-27 19:48:56 +000025#define PATH_VI "/bin/vi" /* location of vi */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000026#endif
27
Denis Vlasenko94d5d822006-09-27 19:48:56 +000028static const char *CDir = CRONTABS;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000029
30static void EditFile(const char *user, const char *file);
31static int GetReplaceStream(const char *user, const char *file);
Denis Vlasenko94d5d822006-09-27 19:48:56 +000032static int ChangeUser(const char *user, short dochdir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000033
Rob Landleyd921b2e2006-08-03 15:41:12 +000034int crontab_main(int ac, char **av)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000035{
Denis Vlasenko94d5d822006-09-27 19:48:56 +000036 enum { NONE, EDIT, LIST, REPLACE, DELETE } option = NONE;
37 const struct passwd *pas;
38 const char *repFile = NULL;
39 int repFd = 0;
40 int i;
41 char caller[256]; /* user that ran program */
42 char buf[1024];
43 int UserId;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000044
Denis Vlasenko94d5d822006-09-27 19:48:56 +000045 UserId = getuid();
46 pas = getpwuid(UserId);
47 if (pas == NULL)
48 bb_perror_msg_and_die("getpwuid");
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000049
Denis Vlasenko94d5d822006-09-27 19:48:56 +000050 safe_strncpy(caller, pas->pw_name, sizeof(caller));
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000051
Denis Vlasenko94d5d822006-09-27 19:48:56 +000052 i = 1;
53 if (ac > 1) {
54 if (av[1][0] == '-' && av[1][1] == 0) {
55 option = REPLACE;
56 ++i;
57 } else if (av[1][0] != '-') {
58 option = REPLACE;
59 ++i;
60 repFile = av[1];
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000061 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000062 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000063
Denis Vlasenko94d5d822006-09-27 19:48:56 +000064 for (; i < ac; ++i) {
65 char *ptr = av[i];
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000066
Denis Vlasenko94d5d822006-09-27 19:48:56 +000067 if (*ptr != '-')
68 break;
69 ptr += 2;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000070
Denis Vlasenko94d5d822006-09-27 19:48:56 +000071 switch (ptr[-1]) {
72 case 'l':
73 if (ptr[-1] == 'l')
74 option = LIST;
75 /* fall through */
76 case 'e':
77 if (ptr[-1] == 'e')
78 option = EDIT;
79 /* fall through */
80 case 'd':
81 if (ptr[-1] == 'd')
82 option = DELETE;
83 /* fall through */
84 case 'u':
85 if (i + 1 < ac && av[i+1][0] != '-') {
86 ++i;
87 if (getuid() == geteuid()) {
88 pas = getpwnam(av[i]);
89 if (pas) {
90 UserId = pas->pw_uid;
91 } else {
92 bb_error_msg_and_die("user %s unknown", av[i]);
93 }
94 } else {
95 bb_error_msg_and_die("only the superuser may specify a user");
96 }
97 }
98 break;
99 case 'c':
100 if (getuid() == geteuid()) {
101 CDir = (*ptr) ? ptr : av[++i];
102 } else {
103 bb_error_msg_and_die("-c option: superuser only");
104 }
105 break;
106 default:
107 i = ac;
108 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000109 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000110 }
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000111 if (i != ac || option == NONE)
112 bb_show_usage();
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000113
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000114 /*
115 * Get password entry
116 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000117
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000118 pas = getpwuid(UserId);
119 if (pas == NULL)
120 bb_perror_msg_and_die("getpwuid");
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000121
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000122 /*
123 * If there is a replacement file, obtain a secure descriptor to it.
124 */
125
126 if (repFile) {
127 repFd = GetReplaceStream(caller, repFile);
128 if (repFd < 0)
129 bb_error_msg_and_die("unable to read replacement file");
130 }
131
132 /*
133 * Change directory to our crontab directory
134 */
135
136 xchdir(CDir);
137
138 /*
139 * Handle options as appropriate
140 */
141
142 switch (option) {
143 case LIST:
144 {
145 FILE *fi;
146
147 fi = fopen(pas->pw_name, "r");
148 if (fi) {
149 while (fgets(buf, sizeof(buf), fi) != NULL)
150 fputs(buf, stdout);
151 fclose(fi);
152 } else {
153 bb_error_msg("no crontab for %s", pas->pw_name);
154 }
155 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000156 break;
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000157 case EDIT:
158 {
159 FILE *fi;
160 int fd;
161 int n;
162 char tmp[128];
163
164 snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
165 fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
166 chown(tmp, getuid(), getgid());
167 fi = fopen(pas->pw_name, "r");
168 if (fi) {
169 while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
170 write(fd, buf, n);
171 }
172 EditFile(caller, tmp);
173 remove(tmp);
174 lseek(fd, 0L, 0);
175 repFd = fd;
176 }
177 option = REPLACE;
178 /* fall through */
179 case REPLACE:
180 {
181 char path[1024];
182 int fd;
183 int n;
184
185 snprintf(path, sizeof(path), "%s.new", pas->pw_name);
186 fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
187 if (fd >= 0) {
188 while ((n = read(repFd, buf, sizeof(buf))) > 0) {
189 write(fd, buf, n);
190 }
191 close(fd);
192 rename(path, pas->pw_name);
193 } else {
194 bb_error_msg("unable to create %s/%s", CDir, path);
195 }
196 close(repFd);
197 }
198 break;
199 case DELETE:
200 remove(pas->pw_name);
201 break;
202 case NONE:
203 default:
204 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000205 }
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000206
207 /*
208 * Bump notification file. Handle window where crond picks file up
209 * before we can write our entry out.
210 */
211
212 if (option == REPLACE || option == DELETE) {
213 FILE *fo;
214 struct stat st;
215
216 while ((fo = fopen(CRONUPDATE, "a"))) {
217 fprintf(fo, "%s\n", pas->pw_name);
218 fflush(fo);
219 if (fstat(fileno(fo), &st) != 0 || st.st_nlink != 0) {
220 fclose(fo);
221 break;
222 }
223 fclose(fo);
224 /* loop */
225 }
226 if (fo == NULL) {
227 bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
228 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000229 }
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000230 return 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000231}
232
Rob Landleyd921b2e2006-08-03 15:41:12 +0000233static int GetReplaceStream(const char *user, const char *file)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000234{
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000235 int filedes[2];
236 int pid;
237 int fd;
238 int n;
239 char buf[1024];
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000240
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000241 if (pipe(filedes) < 0) {
242 perror("pipe");
243 return -1;
244 }
245 pid = fork();
246 if (pid < 0) {
247 perror("fork");
248 return -1;
249 }
250 if (pid > 0) {
251 /*
252 * PARENT
253 */
254
255 close(filedes[1]);
256 if (read(filedes[0], buf, 1) != 1) {
257 close(filedes[0]);
258 filedes[0] = -1;
259 }
260 return filedes[0];
261 }
262
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000263 /*
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000264 * CHILD
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000265 */
266
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000267 close(filedes[0]);
268
269 if (ChangeUser(user, 0) < 0)
270 exit(0);
271
Denis Vlasenko40920822006-10-03 20:28:06 +0000272 xfunc_error_retval = 0;
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000273 fd = xopen(file, O_RDONLY);
274 buf[0] = 0;
275 write(filedes[1], buf, 1);
276 while ((n = read(fd, buf, sizeof(buf))) > 0) {
277 write(filedes[1], buf, n);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000278 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000279 exit(0);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000280}
281
Rob Landleyd921b2e2006-08-03 15:41:12 +0000282static void EditFile(const char *user, const char *file)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000283{
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000284 int pid = fork();
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000285
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000286 if (pid == 0) {
287 /*
288 * CHILD - change user and run editor
289 */
290 char *ptr;
291 char visual[1024];
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000292
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000293 if (ChangeUser(user, 1) < 0)
294 exit(0);
295 ptr = getenv("VISUAL");
296 if (ptr == NULL || strlen(ptr) > 256)
297 ptr = PATH_VI;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000298
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000299 snprintf(visual, sizeof(visual), "%s %s", ptr, file);
300 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", visual, NULL);
301 perror("exec");
302 exit(0);
303 }
304 if (pid < 0) {
305 /*
306 * PARENT - failure
307 */
308 bb_perror_msg_and_die("fork");
309 }
310 wait4(pid, NULL, 0, NULL);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000311}
312
Rob Landleyd921b2e2006-08-03 15:41:12 +0000313static int ChangeUser(const char *user, short dochdir)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000314{
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000315 struct passwd *pas;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000316
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000317 /*
318 * Obtain password entry and change privileges
319 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000320
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000321 pas = getpwnam(user);
322 if (pas == NULL) {
323 bb_perror_msg_and_die("failed to get uid for %s", user);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000324 }
Denis Vlasenko94d5d822006-09-27 19:48:56 +0000325 setenv("USER", pas->pw_name, 1);
326 setenv("HOME", pas->pw_dir, 1);
327 setenv("SHELL", DEFAULT_SHELL, 1);
328
329 /*
330 * Change running state to the user in question
331 */
332 change_identity(pas);
333
334 if (dochdir) {
335 if (chdir(pas->pw_dir) < 0) {
336 bb_perror_msg("chdir(%s) by %s failed", pas->pw_dir, user);
337 xchdir(TMPDIR);
338 }
339 }
340 return pas->pw_uid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000341}