1 /* crontab.c - files used to schedule the execution of programs.
2 *
3 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
4 *
5 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html
6
7 USE_CRONTAB(NEWTOY(crontab, "c:u:elr[!elr]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
8
9 config CRONTAB
10 bool "crontab"
11 default n
12 depends on TOYBOX_FORK
13 help
14 usage: crontab [-u user] FILE
15 [-u user] [-e | -l | -r]
16 [-c dir]
17
18 Files used to schedule the execution of programs.
19
20 -c crontab dir
21 -e edit user's crontab
22 -l list user's crontab
23 -r delete user's crontab
24 -u user
25 FILE Replace crontab by FILE ('-': stdin)
26 */
27 #define FOR_crontab
28 #include "toys.h"
29
GLOBALS(char * user;char * cdir;)30 GLOBALS(
31 char *user;
32 char *cdir;
33 )
34
35 static char *omitspace(char *line)
36 {
37 while (*line == ' ' || *line == '\t') line++;
38 return line;
39 }
40
41 /*
42 * Names can also be used for the 'month' and 'day of week' fields
43 * (First three letters of the particular day or month).
44 */
getindex(char * src,int size)45 static int getindex(char *src, int size)
46 {
47 int i;
48 char days[]={"sun""mon""tue""wed""thu""fri""sat"};
49 char months[]={"jan""feb""mar""apr""may""jun""jul"
50 "aug""sep""oct""nov""dec"};
51 char *field = (size == 12) ? months : days;
52
53 // strings are not allowed for min, hour and dom fields.
54 if (!(size == 7 || size == 12)) return -1;
55
56 for (i = 0; field[i]; i += 3) {
57 if (!strncasecmp(src, &field[i], 3))
58 return (i/3);
59 }
60 return -1;
61 }
62
getval(char * num,long low,long high)63 static long getval(char *num, long low, long high)
64 {
65 long val = strtol(num, &num, 10);
66
67 if (*num || (val < low) || (val > high)) return -1;
68 return val;
69 }
70
71 // Validate minute, hour, day of month, month and day of week fields.
validate_component(int min,int max,char * src)72 static int validate_component(int min, int max, char *src)
73 {
74 int skip = 0;
75 char *ptr;
76
77 if (!src) return 1;
78 if ((ptr = strchr(src, '/'))) {
79 *ptr++ = 0;
80 if ((skip = getval(ptr, min, (min ? max: max-1))) < 0) return 1;
81 }
82
83 if (*src == '-' || *src == ',') return 1;
84 if (*src == '*') {
85 if (*(src+1)) return 1;
86 }
87 else {
88 for (;;) {
89 char *ctoken = strsep(&src, ","), *dtoken;
90
91 if (!ctoken) break;
92 if (!*ctoken) return 1;
93
94 // validate start position.
95 dtoken = strsep(&ctoken, "-");
96 if (isdigit(*dtoken)) {
97 if (getval(dtoken, min, (min ? max : max-1)) < 0) return 1;
98 } else if (getindex(dtoken, max) < 0) return 1;
99
100 // validate end position.
101 if (!ctoken) {
102 if (skip) return 1; // case 10/20 or 1,2,4/3
103 }
104 else if (*ctoken) {// e.g. N-M
105 if (isdigit(*ctoken)) {
106 if (getval(ctoken, min, (min ? max : max-1)) < 0) return 1;
107 } else if (getindex(ctoken, max) < 0) return 1;
108 } else return 1; // error condition 'N-'
109 }
110 }
111 return 0;
112 }
113
parse_crontab(char * fname)114 static int parse_crontab(char *fname)
115 {
116 FILE *fp = xfopen(fname, "r");
117 long len = 0;
118 char *line = NULL;
119 size_t allocated_length;
120 int lno;
121
122 for (lno = 1; (len = getline(&line, &allocated_length, fp)) > 0; lno++) {
123 char *name, *val, *tokens[5] = {0,}, *ptr = line;
124 int count = 0;
125
126 if (line[len - 1] == '\n') line[--len] = '\0';
127 else {
128 snprintf(toybuf, sizeof(toybuf), "'%d': premature EOF\n", lno);
129 goto OUT;
130 }
131
132 ptr = omitspace(ptr);
133 if (!*ptr || *ptr == '#' || *ptr == '@') continue;
134 while (count<5) {
135 int len = strcspn(ptr, " \t");
136
137 if (ptr[len]) ptr[len++] = '\0';
138 tokens[count++] = ptr;
139 ptr += len;
140 ptr = omitspace(ptr);
141 if (!*ptr) break;
142 }
143 switch (count) {
144 case 1: // form SHELL=/bin/sh
145 name = tokens[0];
146 if ((val = strchr(name, '='))) *val++ = 0;
147 if (!val || !*val) {
148 snprintf(toybuf, sizeof(toybuf), "'%d': %s\n", lno, line);
149 goto OUT;
150 }
151 break;
152 case 2: // form SHELL =/bin/sh or SHELL= /bin/sh
153 name = tokens[0];
154 if ((val = strchr(name, '='))) {
155 *val = 0;
156 val = tokens[1];
157 } else {
158 if (*(tokens[1]) != '=') {
159 snprintf(toybuf, sizeof(toybuf), "'%d': %s\n", lno, line);
160 goto OUT;
161 }
162 val = tokens[1] + 1;
163 }
164 if (!*val) {
165 snprintf(toybuf, sizeof(toybuf), "'%d': %s\n", lno, line);
166 goto OUT;
167 }
168 break;
169 case 3: // NAME = VAL
170 name = tokens[0];
171 val = tokens[2];
172 if (*(tokens[1]) != '=') {
173 snprintf(toybuf, sizeof(toybuf), "'%d': %s\n", lno, line);
174 goto OUT;
175 }
176 break;
177 default:
178 if (validate_component(0, 60, tokens[0])) {
179 snprintf(toybuf, sizeof(toybuf), "'%d': bad minute\n", lno);
180 goto OUT;
181 }
182 if (validate_component(0, 24, tokens[1])) {
183 snprintf(toybuf, sizeof(toybuf), "'%d': bad hour\n", lno);
184 goto OUT;
185 }
186 if (validate_component(1, 31, tokens[2])) {
187 snprintf(toybuf, sizeof(toybuf), "'%d': bad day-of-month\n", lno);
188 goto OUT;
189 }
190 if (validate_component(1, 12, tokens[3])) {
191 snprintf(toybuf, sizeof(toybuf), "'%d': bad month\n", lno);
192 goto OUT;
193 }
194 if (validate_component(0, 7, tokens[4])) {
195 snprintf(toybuf, sizeof(toybuf), "'%d': bad day-of-week\n", lno);
196 goto OUT;
197 }
198 if (!*ptr) { // don't have any cmd to execute.
199 snprintf(toybuf, sizeof(toybuf), "'%d': bad command\n", lno);
200 goto OUT;
201 }
202 break;
203 }
204 }
205 free(line);
206 fclose(fp);
207 return 0;
208 OUT:
209 free(line);
210 printf("Error at line no %s", toybuf);
211 fclose(fp);
212 return 1;
213 }
214
do_list(char * name)215 static void do_list(char *name)
216 {
217 int fdin;
218
219 snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, name);
220 fdin = xopenro(toybuf);
221 xsendfile(fdin, 1);
222 xclose(fdin);
223 }
224
do_remove(char * name)225 static void do_remove(char *name)
226 {
227 snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, name);
228 if (unlink(toybuf))
229 error_exit("No crontab for '%s'", name);
230 }
231
update_crontab(char * src,char * dest)232 static void update_crontab(char *src, char *dest)
233 {
234 int fdin, fdout;
235
236 snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, dest);
237 unlink(toybuf);
238 fdout = xcreate(toybuf, O_WRONLY|O_CREAT|O_TRUNC, 0600);
239 fdin = xopenro(src);
240 xsendfile(fdin, fdout);
241 xclose(fdin);
242
243 fchown(fdout, getuid(), geteuid());
244 xclose(fdout);
245 }
246
do_replace(char * name)247 static void do_replace(char *name)
248 {
249 char *fname = *toys.optargs ? *toys.optargs : "-";
250 char tname[] = "/tmp/crontab.XXXXXX";
251
252 if ((*fname == '-') && !*(fname+1)) {
253 int tfd = mkstemp(tname);
254
255 if (tfd < 0) perror_exit("mkstemp");
256 xsendfile(0, tfd);
257 xclose(tfd);
258 fname = tname;
259 }
260
261 if (parse_crontab(fname))
262 error_exit("errors in crontab file '%s', can't install.", fname);
263 update_crontab(fname, name);
264 unlink(tname);
265 }
266
do_edit(struct passwd * pwd)267 static void do_edit(struct passwd *pwd)
268 {
269 struct stat sb;
270 time_t mtime = 0;
271 int srcfd, destfd, status;
272 pid_t pid, cpid;
273 char tname[] = "/tmp/crontab.XXXXXX";
274
275 if ((destfd = mkstemp(tname)) < 0)
276 perror_exit("Can't open tmp file");
277
278 fchmod(destfd, 0666);
279 snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, pwd->pw_name);
280
281 if (!stat(toybuf, &sb)) { // file exists and have some content.
282 if (sb.st_size) {
283 srcfd = xopenro(toybuf);
284 xsendfile(srcfd, destfd);
285 xclose(srcfd);
286 }
287 } else printf("No crontab for '%s'- using an empty one\n", pwd->pw_name);
288 xclose(destfd);
289
290 if (!stat(tname, &sb)) mtime = sb.st_mtime;
291
292 RETRY:
293 if (!(pid = xfork())) {
294 char *prog = pwd->pw_shell;
295
296 xsetuser(pwd);
297 if (pwd->pw_uid) {
298 if (setenv("USER", pwd->pw_name, 1)) _exit(1);
299 if (setenv("LOGNAME", pwd->pw_name, 1)) _exit(1);
300 }
301 if (setenv("HOME", pwd->pw_dir, 1)) _exit(1);
302 if (setenv("SHELL",((!prog || !*prog) ? "/bin/sh" : prog), 1)) _exit(1);
303
304 if (!(prog = getenv("VISUAL"))) {
305 if (!(prog = getenv("EDITOR")))
306 prog = "vi";
307 }
308 execlp(prog, prog, tname, (char *) NULL);
309 perror_exit("can't execute '%s'", prog);
310 }
311
312 // Parent Process.
313 do {
314 cpid = waitpid(pid, &status, 0);
315 } while ((cpid == -1) && (errno == EINTR));
316
317 if (!stat(tname, &sb) && (mtime == sb.st_mtime)) {
318 printf("%s: no changes made to crontab\n", toys.which->name);
319 unlink(tname);
320 return;
321 }
322 printf("%s: installing new crontab\n", toys.which->name);
323 if (parse_crontab(tname)) {
324 fprintf(stderr, "errors in crontab file, can't install.\n"
325 "Do you want to retry the same edit? ");
326 if (!yesno(0)) {
327 error_msg("edits left in '%s'", tname);
328 return;
329 }
330 goto RETRY;
331 }
332 // parsing of crontab success; update the crontab.
333 update_crontab(tname, pwd->pw_name);
334 unlink(tname);
335 }
336
crontab_main(void)337 void crontab_main(void)
338 {
339 struct passwd *pwd = NULL;
340 long FLAG_elr = toys.optflags & (FLAG_e|FLAG_l|FLAG_r);
341
342 if (TT.cdir && (TT.cdir[strlen(TT.cdir)-1] != '/'))
343 TT.cdir = xmprintf("%s/", TT.cdir);
344 if (!TT.cdir) TT.cdir = xstrdup("/var/spool/cron/crontabs/");
345
346 if (toys.optflags & FLAG_u) {
347 if (getuid()) error_exit("must be privileged to use -u");
348 pwd = xgetpwnam(TT.user);
349 } else pwd = xgetpwuid(getuid());
350
351 if (!toys.optc) {
352 if (!FLAG_elr) {
353 if (toys.optflags & FLAG_u)
354 help_exit("file name must be specified for replace");
355 do_replace(pwd->pw_name);
356 }
357 else if (toys.optflags & FLAG_e) do_edit(pwd);
358 else if (toys.optflags & FLAG_l) do_list(pwd->pw_name);
359 else if (toys.optflags & FLAG_r) do_remove(pwd->pw_name);
360 } else {
361 if (FLAG_elr) help_exit("no arguments permitted after this option");
362 do_replace(pwd->pw_name);
363 }
364 if (!(toys.optflags & FLAG_c)) free(TT.cdir);
365 }
366