1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
/* $OpenBSD: misc.c,v 1.56 2015/10/06 14:58:37 tedu Exp $ */
/* Copyright 1988,1990,1993,1994 by Paul Vixie
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "cron.h"
#include <limits.h>
#if defined(LOG_DAEMON) && !defined(LOG_CRON)
# define LOG_CRON LOG_DAEMON
#endif
#ifndef FACILITY
#define FACILITY LOG_CRON
#endif
static int LogFD = -1;
static int syslog_open = FALSE;
int
strcmp_until(const char *left, const char *right, char until)
{
while (*left && *left != until && *left == *right) {
left++;
right++;
}
if ((*left=='\0' || *left == until) &&
(*right=='\0' || *right == until)) {
return (0);
}
return (*left - *right);
}
void
set_cron_uid(void)
{
if (seteuid(ROOT_UID) < 0) {
perror("seteuid");
exit(EXIT_FAILURE);
}
}
void
set_cron_cwd(void)
{
struct stat sb;
struct group *grp = NULL;
#ifdef CRON_GROUP
grp = getgrnam(CRON_GROUP);
#endif
/* first check for CRONDIR ("/var/cron" or some such)
*/
if (stat(CRONDIR, &sb) < 0 && errno == ENOENT) {
perror(CRONDIR);
if (0 == mkdir(CRONDIR, 0710)) {
fprintf(stderr, "%s: created\n", CRONDIR);
stat(CRONDIR, &sb);
} else {
fprintf(stderr, "%s: ", CRONDIR);
perror("mkdir");
exit(EXIT_FAILURE);
}
}
if (!S_ISDIR(sb.st_mode)) {
fprintf(stderr, "'%s' is not a directory, bailing out.\n",
CRONDIR);
exit(EXIT_FAILURE);
}
if (chdir(CRONDIR) < 0) {
fprintf(stderr, "cannot chdir(%s), bailing out.\n", CRONDIR);
perror(CRONDIR);
exit(EXIT_FAILURE);
}
/* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such)
*/
if (stat(SPOOL_DIR, &sb) < 0 && errno == ENOENT) {
perror(SPOOL_DIR);
if (0 == mkdir(SPOOL_DIR, 0700)) {
fprintf(stderr, "%s: created\n", SPOOL_DIR);
stat(SPOOL_DIR, &sb);
} else {
fprintf(stderr, "%s: ", SPOOL_DIR);
perror("mkdir");
exit(EXIT_FAILURE);
}
}
if (!S_ISDIR(sb.st_mode)) {
fprintf(stderr, "'%s' is not a directory, bailing out.\n",
SPOOL_DIR);
exit(EXIT_FAILURE);
}
if (grp != NULL) {
if (sb.st_gid != grp->gr_gid)
chown(SPOOL_DIR, -1, grp->gr_gid);
if (sb.st_mode != 01730)
chmod(SPOOL_DIR, 01730);
}
/* finally, look at AT_DIR ("atjobs" or some such)
*/
if (stat(AT_DIR, &sb) < 0 && errno == ENOENT) {
perror(AT_DIR);
if (0 == mkdir(AT_DIR, 0700)) {
fprintf(stderr, "%s: created\n", AT_DIR);
stat(AT_DIR, &sb);
} else {
fprintf(stderr, "%s: ", AT_DIR);
perror("mkdir");
exit(EXIT_FAILURE);
}
}
if (!S_ISDIR(sb.st_mode)) {
fprintf(stderr, "'%s' is not a directory, bailing out.\n",
AT_DIR);
exit(EXIT_FAILURE);
}
if (grp != NULL) {
if (sb.st_gid != grp->gr_gid)
chown(AT_DIR, -1, grp->gr_gid);
if (sb.st_mode != 01770)
chmod(AT_DIR, 01770);
}
}
/* acquire_daemonlock() - write our PID into /var/run/cron.pid, unless
* another daemon is already running, which we detect here.
*
* note: main() calls us twice; once before forking, once after.
* we maintain static storage of the file pointer so that we
* can rewrite our PID into _PATH_CRON_PID after the fork.
*/
void
acquire_daemonlock(int closeflag)
{
static int fd = -1;
char buf[3*MAX_FNAME];
const char *pidfile;
char *ep;
long otherpid;
ssize_t num;
if (closeflag) {
/* close stashed fd for child so we don't leak it. */
if (fd != -1) {
close(fd);
fd = -1;
}
return;
}
if (fd == -1) {
pidfile = _PATH_CRON_PID;
fd = open(pidfile,
O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK|O_CLOEXEC, 0644);
if (fd == -1) {
int save_errno = errno;
if (errno != EWOULDBLOCK) {
snprintf(buf, sizeof buf,
"can't open or create %s: %s", pidfile,
strerror(save_errno));
fprintf(stderr, "%s: %s\n", ProgramName, buf);
log_it("CRON", getpid(), "DEATH", buf);
exit(EXIT_FAILURE);
}
/* couldn't lock the pid file, try to read existing. */
bzero(buf, sizeof(buf));
if ((fd = open(pidfile, O_RDONLY, 0)) >= 0 &&
(num = read(fd, buf, sizeof(buf) - 1)) > 0 &&
(otherpid = strtol(buf, &ep, 10)) > 0 &&
ep != buf && *ep == '\n' && otherpid != LONG_MAX) {
snprintf(buf, sizeof buf,
"can't lock %s, otherpid may be %ld: %s",
pidfile, otherpid, strerror(save_errno));
} else {
snprintf(buf, sizeof buf,
"can't lock %s, otherpid unknown: %s",
pidfile, strerror(save_errno));
}
fprintf(stderr, "%s: %s\n", ProgramName, buf);
log_it("CRON", getpid(), "DEATH", buf);
exit(EXIT_FAILURE);
}
/* fd must be > STDERR_FILENO since we dup fd 0-2 to /dev/null */
if (fd <= STDERR_FILENO) {
int newfd;
newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
if (newfd < 0) {
snprintf(buf, sizeof buf,
"can't dup pid fd: %s", strerror(errno));
fprintf(stderr, "%s: %s\n", ProgramName, buf);
log_it("CRON", getpid(), "DEATH", buf);
exit(EXIT_FAILURE);
}
close(fd);
fd = newfd;
}
}
snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
(void) lseek(fd, 0, SEEK_SET);
num = write(fd, buf, strlen(buf));
(void) ftruncate(fd, num);
/* abandon fd even though the file is open. we need to keep
* it open and locked, but we don't need the handles elsewhere.
*/
}
/* get_char(file) : like getc() but increment LineNumber on newlines
*/
int
get_char(FILE *file)
{
int ch;
ch = getc(file);
if (ch == '\n')
Set_LineNum(LineNumber + 1)
return (ch);
}
/* unget_char(ch, file) : like ungetc but do LineNumber processing
*/
void
unget_char(int ch, FILE *file)
{
ungetc(ch, file);
if (ch == '\n')
Set_LineNum(LineNumber - 1)
}
/* get_string(str, max, file, termstr) : like fgets() but
* (1) has terminator string which should include \n
* (2) will always leave room for the null
* (3) uses get_char() so LineNumber will be accurate
* (4) returns EOF or terminating character, whichever
*/
int
get_string(char *string, int size, FILE *file, char *terms)
{
int ch;
while (EOF != (ch = get_char(file)) && !strchr(terms, ch)) {
if (size > 1) {
*string++ = ch;
size--;
}
}
if (size > 0)
*string = '\0';
return (ch);
}
/* skip_comments(file) : read past comment (if any)
*/
void
skip_comments(FILE *file)
{
int ch;
while (EOF != (ch = get_char(file))) {
/* ch is now the first character of a line.
*/
while (ch == ' ' || ch == '\t')
ch = get_char(file);
if (ch == EOF)
break;
/* ch is now the first non-blank character of a line.
*/
if (ch != '\n' && ch != '#')
break;
/* ch must be a newline or comment as first non-blank
* character on a line.
*/
while (ch != '\n' && ch != EOF)
ch = get_char(file);
/* ch is now the newline of a line which we're going to
* ignore.
*/
}
if (ch != EOF)
unget_char(ch, file);
}
/* int in_file(const char *string, FILE *file, int error)
* return TRUE if one of the lines in file matches string exactly,
* FALSE if no lines match, and error on error.
*/
static int
in_file(const char *string, FILE *file, int error)
{
char line[MAX_TEMPSTR];
char *endp;
if (fseek(file, 0L, SEEK_SET))
return (error);
while (fgets(line, MAX_TEMPSTR, file)) {
if (line[0] != '\0') {
endp = &line[strlen(line) - 1];
if (*endp != '\n')
return (error);
*endp = '\0';
if (0 == strcmp(line, string))
return (TRUE);
}
}
if (ferror(file))
return (error);
return (FALSE);
}
/* int allowed(const char *username, const char *allow_file, const char *deny_file)
* returns TRUE if (allow_file exists and user is listed)
* or (deny_file exists and user is NOT listed).
* root is always allowed.
*/
int
allowed(const char *username, const char *allow_file, const char *deny_file)
{
FILE *fp;
int isallowed;
if (strcmp(username, ROOT_USER) == 0)
return (TRUE);
isallowed = FALSE;
if ((fp = fopen(allow_file, "r")) != NULL) {
isallowed = in_file(username, fp, FALSE);
fclose(fp);
} else if ((fp = fopen(deny_file, "r")) != NULL) {
isallowed = !in_file(username, fp, FALSE);
fclose(fp);
}
return (isallowed);
}
void
log_it(const char *username, pid_t xpid, const char *event, const char *detail)
{
char **info, *info_events[] = { "CMD", "ATJOB", "BEGIN EDIT", "DELETE",
"END EDIT", "LIST", "MAIL", "RELOAD", "REPLACE", "STARTUP", NULL };
if (!syslog_open) {
openlog(ProgramName, LOG_PID, FACILITY);
syslog_open = TRUE; /* assume openlog success */
}
for (info = info_events; *info; info++)
if (!strcmp(event, *info))
break;
syslog(*info ? LOG_INFO : LOG_WARNING, "(%s) %s (%s)", username, event,
detail);
}
void
log_close(void)
{
if (LogFD != -1) {
close(LogFD);
LogFD = -1;
}
closelog();
syslog_open = FALSE;
}
/* char *first_word(char *s, char *t)
* return pointer to first word
* parameters:
* s - string we want the first word of
* t - terminators, implicitly including \0
* warnings:
* (1) this routine is fairly slow
* (2) it returns a pointer to static storage
*/
char *
first_word(char *s, char *t)
{
static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */
static int retsel = 0;
char *rb, *rp;
/* select a return buffer */
retsel = 1-retsel;
rb = &retbuf[retsel][0];
rp = rb;
/* skip any leading terminators */
while (*s && (NULL != strchr(t, *s))) {
s++;
}
/* copy until next terminator or full buffer */
while (*s && (NULL == strchr(t, *s)) && (rp < &rb[MAX_TEMPSTR])) {
*rp++ = *s++;
}
/* finish the return-string and return it */
*rp = '\0';
return (rb);
}
/* warning:
* heavily ascii-dependent.
*/
void
mkprint(dst, src, len)
char *dst;
unsigned char *src;
int len;
{
/*
* XXX
* We know this routine can't overflow the dst buffer because mkprints()
* allocated enough space for the worst case.
*/
while (len-- > 0)
{
unsigned char ch = *src++;
if (ch < ' ') { /* control character */
*dst++ = '^';
*dst++ = ch + '@';
} else if (ch < 0177) { /* printable */
*dst++ = ch;
} else if (ch == 0177) { /* delete/rubout */
*dst++ = '^';
*dst++ = '?';
} else { /* parity character */
snprintf(dst, 5, "\\%03o", ch);
dst += strlen(dst);
}
}
*dst = '\0';
}
/* warning:
* returns a pointer to malloc'd storage, you must call free yourself.
*/
char *
mkprints(src, len)
unsigned char *src;
unsigned int len;
{
char *dst = malloc(len*4 + 1);
if (dst)
mkprint(dst, src, len);
return (dst);
}
#ifdef MAIL_DATE
/* Sat, 27 Feb 1993 11:44:51 -0800 (CST)
* 1234567890123456789012345678901234567
*/
char *
arpadate(clock)
time_t *clock;
{
time_t t = clock ? *clock : time(NULL);
struct tm *tm = localtime(&t);
static char ret[64]; /* zone name might be >3 chars */
char *qmark;
size_t len;
long gmtoff = get_gmtoff(&t, tm);
int hours = gmtoff / 3600;
int minutes = (gmtoff - (hours * 3600)) / 60;
if (minutes < 0)
minutes = -minutes;
/* Defensive coding (almost) never hurts... */
len = strftime(ret, sizeof(ret), "%a, %e %b %Y %T ????? (%Z)", tm);
if (len == 0) {
ret[0] = '?';
ret[1] = '\0';
return (ret);
}
qmark = strchr(ret, '?');
if (qmark && len - (qmark - ret) >= 6) {
snprintf(qmark, 6, "% .2d%.2d", hours, minutes);
qmark[5] = ' ';
}
return (ret);
}
#endif /*MAIL_DATE*/
static gid_t save_egid;
int swap_gids() { save_egid = getegid(); return (setegid(getgid())); }
int swap_gids_back() { return (setegid(save_egid)); }
/* Return the offset from GMT in seconds (algorithm taken from sendmail).
*
* warning:
* clobbers the static storage space used by localtime() and gmtime().
* If the local pointer is non-NULL it *must* point to a local copy.
*/
/* void open_socket(void)
* opens a UNIX domain socket that crontab uses to poke cron.
*/
int
open_socket(void)
{
int sock;
mode_t omask;
struct sockaddr_un s_un;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
fprintf(stderr, "%s: can't create socket: %s\n",
ProgramName, strerror(errno));
log_it("CRON", getpid(), "DEATH", "can't create socket");
exit(EXIT_FAILURE);
}
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
fprintf(stderr, "%s: can't make socket close on exec: %s\n",
ProgramName, strerror(errno));
log_it("CRON", getpid(), "DEATH",
"can't make socket close on exec");
exit(EXIT_FAILURE);
}
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
fprintf(stderr, "%s: can't make socket non-blocking: %s\n",
ProgramName, strerror(errno));
log_it("CRON", getpid(), "DEATH",
"can't make socket non-blocking");
exit(EXIT_FAILURE);
}
bzero(&s_un, sizeof(s_un));
if (snprintf(s_un.sun_path, sizeof s_un.sun_path, "%s/%s",
SPOOL_DIR, CRONSOCK) >= sizeof(s_un.sun_path)) {
fprintf(stderr, "%s/%s: path too long\n", SPOOL_DIR, CRONSOCK);
log_it("CRON", getpid(), "DEATH", "path too long");
exit(EXIT_FAILURE);
}
unlink(s_un.sun_path);
s_un.sun_family = AF_UNIX;
#ifdef SUN_LEN
s_un.sun_len = SUN_LEN(&s_un);
#endif
omask = umask(007);
if (bind(sock, (struct sockaddr *)&s_un, sizeof(s_un))) {
fprintf(stderr, "%s: can't bind socket: %s\n",
ProgramName, strerror(errno));
log_it("CRON", getpid(), "DEATH", "can't bind socket");
umask(omask);
exit(EXIT_FAILURE);
}
umask(omask);
if (listen(sock, SOMAXCONN)) {
fprintf(stderr, "%s: can't listen on socket: %s\n",
ProgramName, strerror(errno));
log_it("CRON", getpid(), "DEATH", "can't listen on socket");
exit(EXIT_FAILURE);
}
chmod(s_un.sun_path, 0660);
return(sock);
}
void
poke_daemon(const char *spool_dir, unsigned char cookie)
{
int sock = -1;
struct sockaddr_un s_un;
(void) utime(spool_dir, NULL); /* old poke method */
bzero(&s_un, sizeof(s_un));
if (snprintf(s_un.sun_path, sizeof s_un.sun_path, "%s/%s",
SPOOL_DIR, CRONSOCK) >= sizeof(s_un.sun_path)) {
fprintf(stderr, "%s: %s/%s: path too long\n",
ProgramName, SPOOL_DIR, CRONSOCK);
return;
}
s_un.sun_family = AF_UNIX;
#ifdef SUN_LEN
s_un.sun_len = SUN_LEN(&s_un);
#endif
(void) signal(SIGPIPE, SIG_IGN);
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
connect(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == 0)
write(sock, &cookie, 1);
else
fprintf(stderr, "%s: warning, cron does not appear to be "
"running.\n", ProgramName);
if (sock >= 0)
close(sock);
(void) signal(SIGPIPE, SIG_DFL);
}
int
strtot(const char *nptr, char **endptr, time_t *tp)
{
long long ll;
ll = strtoll(nptr, endptr, 10);
if (ll < 0 || (time_t)ll != ll)
return (-1);
*tp = (time_t)ll;
return (0);
}
|