This source file includes following definitions.
- trylock_file
- glue_strings
- strcmp_until
- strdtb
- set_debug_flags
- set_cron_uid
- check_spool_dir
- acquire_daemonlock
- get_char
- unget_char
- get_string
- skip_comments
- in_file
- allowed
- log_it
- log_close
- first_word
- mkprint
- mkprints
- arpadate
- swap_uids
- swap_uids_back
- swap_uids
- swap_uids_back
- strlens
- get_gmtoff
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 #include "config.h"
27
28 #include "globals.h"
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38
39 #if defined(SYSLOG)
40 # include <syslog.h>
41 #endif
42
43 #ifdef WITH_AUDIT
44 # include <libaudit.h>
45 #endif
46
47 #ifdef HAVE_FCNTL_H
48 # include <fcntl.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
53 #ifdef HAVE_FLOCK
54 # include <sys/file.h>
55 #endif
56
57 #include "funcs.h"
58 #include "macros.h"
59 #include "pathnames.h"
60
61 #if defined(SYSLOG) && defined(LOG_FILE)
62 # undef LOG_FILE
63 #endif
64
65 #if defined(LOG_DAEMON) && !defined(LOG_CRON)
66 # define LOG_CRON LOG_DAEMON
67 #endif
68
69 #ifndef FACILITY
70 # define FACILITY LOG_CRON
71 #endif
72
73 static int LogFD = ERR;
74
75 #if defined(SYSLOG)
76 static int syslog_open = FALSE;
77 #endif
78
79 #if defined(HAVE_FLOCK)
80 # define trylock_file(fd) flock((fd), LOCK_EX|LOCK_NB)
81 #elif defined(HAVE_FCNTL) && defined(F_SETLK)
82 static int trylock_file(int fd) {
83 struct flock fl;
84
85 memset(&fl, '\0', sizeof (fl));
86 fl.l_type = F_WRLCK;
87 fl.l_whence = SEEK_SET;
88 fl.l_start = 0;
89 fl.l_len = 0;
90
91 return fcntl(fd, F_SETLK, &fl);
92 }
93 #elif defined(HAVE_LOCKF)
94 # define trylock_file(fd) lockf((fd), F_TLOCK, 0)
95 #endif
96
97
98
99
100
101
102
103
104 int
105 glue_strings(char *buffer, size_t buffer_size, const char *a, const char *b,
106 char separator) {
107 char *buf;
108 char *buf_end;
109
110 if (buffer_size <= 0)
111 return (0);
112 buf_end = buffer + buffer_size;
113 buf = buffer;
114
115 for ( ; buf < buf_end && *a != '\0'; buf++, a++)
116 *buf = *a;
117 if (buf == buf_end)
118 return (0);
119 if (separator != '/' || buf == buffer || buf[-1] != '/')
120 *buf++ = separator;
121 if (buf == buf_end)
122 return (0);
123 for ( ; buf < buf_end && *b != '\0'; buf++, b++)
124 *buf = *b;
125 if (buf == buf_end)
126 return (0);
127 *buf = '\0';
128 return (1);
129 }
130
131 int strcmp_until(const char *left, const char *right, char until) {
132 while (*left && *left != until && *left == *right) {
133 left++;
134 right++;
135 }
136
137 if ((*left == '\0' || *left == until) && (*right == '\0' ||
138 *right == until)) {
139 return (0);
140 }
141 return (*left - *right);
142 }
143
144
145
146 size_t strdtb(char *s) {
147 char *x = s;
148
149
150
151 while (*x)
152 x++;
153
154
155
156
157 do {
158 x--;
159 } while (x >= s && isspace((unsigned char) *x));
160
161
162
163
164 *++x = '\0';
165
166
167
168
169 return ((size_t)(x - s));
170 }
171
172 int set_debug_flags(const char *flags) {
173
174
175
176
177
178
179 #if !DEBUGGING
180
181 printf("this program was compiled without debugging enabled\n");
182 return (FALSE);
183
184 #else
185
186 const char *pc = flags;
187
188 DebugFlags = 0;
189
190 while (*pc) {
191 const char **test;
192 int mask;
193
194
195
196 for (test = DebugFlagNames, mask = 1;
197 *test != NULL && strcmp_until(*test, pc, ','); test++, mask <<= 1) ;
198
199 if (!*test) {
200 fprintf(stderr, "unrecognized debug flag <%s> <%s>\n", flags, pc);
201 return (FALSE);
202 }
203
204 DebugFlags |= mask;
205
206
207
208 while (*pc && *pc != ',')
209 pc++;
210 if (*pc == ',')
211 pc++;
212 }
213
214 if (DebugFlags) {
215 int flag;
216
217 fprintf(stderr, "debug flags enabled:");
218
219 for (flag = 0; DebugFlagNames[flag]; flag++)
220 if (DebugFlags & (1 << flag))
221 fprintf(stderr, " %s", DebugFlagNames[flag]);
222 fprintf(stderr, "\n");
223 }
224
225 return (TRUE);
226
227 #endif
228 }
229
230 void set_cron_uid(void) {
231 #if defined(BSD) || defined(POSIX)
232 if (seteuid(ROOT_UID) < OK) {
233 perror("seteuid");
234 exit(ERROR_EXIT);
235 }
236 #else
237 if (setuid(ROOT_UID) < OK) {
238 perror("setuid");
239 exit(ERROR_EXIT);
240 }
241 #endif
242 }
243
244 void check_spool_dir(void) {
245 struct stat sb;
246 #ifdef CRON_GROUP
247 struct group *grp = NULL;
248
249 grp = getgrnam(CRON_GROUP);
250 #endif
251
252
253 if (stat(SPOOL_DIR, &sb) < OK && errno == ENOENT) {
254 perror(SPOOL_DIR);
255 if (OK == mkdir(SPOOL_DIR, 0700)) {
256 fprintf(stderr, "%s: created\n", SPOOL_DIR);
257 if (stat(SPOOL_DIR, &sb) < OK) {
258 perror("stat retry");
259 exit(ERROR_EXIT);
260 }
261 }
262 else {
263 fprintf(stderr, "%s: ", SPOOL_DIR);
264 perror("mkdir");
265 exit(ERROR_EXIT);
266 }
267 }
268 if (!S_ISDIR(sb.st_mode)) {
269 fprintf(stderr, "'%s' is not a directory, bailing out.\n", SPOOL_DIR);
270 exit(ERROR_EXIT);
271 }
272 #ifdef CRON_GROUP
273 if (grp != NULL) {
274 if (sb.st_gid != grp->gr_gid)
275 if (chown(SPOOL_DIR, -1, grp->gr_gid) == -1) {
276 fprintf(stderr, "chown %s failed: %s\n", SPOOL_DIR,
277 strerror(errno));
278 exit(ERROR_EXIT);
279 }
280 if (sb.st_mode != 01730)
281 if (chmod(SPOOL_DIR, 01730) == -1) {
282 fprintf(stderr, "chmod 01730 %s failed: %s\n", SPOOL_DIR,
283 strerror(errno));
284 exit(ERROR_EXIT);
285 }
286 }
287 #endif
288 }
289
290
291
292
293
294
295
296
297 void acquire_daemonlock(int closeflag) {
298 static int fd = -1;
299 char buf[3 * MAX_FNAME];
300 const char *pidfile;
301 char *ep;
302 long otherpid = -1;
303 ssize_t num, len;
304 pid_t pid = getpid();
305
306 if (closeflag) {
307
308 if (fd != -1) {
309 close(fd);
310 fd = -1;
311 }
312
313 signal(SIGINT,SIG_DFL);
314 signal(SIGTERM,SIG_DFL);
315 return;
316 }
317
318 if (NoFork == 1)
319 return;
320
321 if (fd == -1) {
322 pidfile = _PATH_CRON_PID;
323
324 if ((fd = open(pidfile, O_RDWR | O_CREAT, 0600)) == -1) {
325 int save_errno = errno;
326 sprintf(buf, "can't open or create %s", pidfile);
327 fprintf(stderr, "%s: %s: %s\n", ProgramName, buf,
328 strerror(save_errno));
329 log_it("CRON", pid, "DEATH", buf, save_errno);
330 exit(ERROR_EXIT);
331 }
332
333 if (trylock_file(fd) < OK) {
334 int save_errno = errno;
335
336 memset(buf, 0, sizeof (buf));
337 if ((num = read(fd, buf, sizeof (buf) - 1)) > 0 &&
338 (otherpid = strtol(buf, &ep, 10)) > 0 &&
339 ep != buf && *ep == '\n' && otherpid != LONG_MAX) {
340 snprintf(buf, sizeof (buf),
341 "can't lock %s, otherpid may be %ld", pidfile, otherpid);
342 }
343 else {
344 snprintf(buf, sizeof (buf),
345 "can't lock %s, otherpid unknown", pidfile);
346 }
347 fprintf(stderr, "%s: %s: %s\n", ProgramName, buf,
348 strerror(save_errno));
349 log_it("CRON", pid, "DEATH", buf, save_errno);
350 exit(ERROR_EXIT);
351 }
352 (void) fchmod(fd, 0644);
353 (void) fcntl(fd, F_SETFD, 1);
354 }
355 #if !defined(HAVE_FLOCK)
356 else {
357
358 sleep(0);
359 trylock_file(fd);
360 }
361 #endif
362
363 sprintf(buf, "%ld\n", (long) pid);
364 (void) lseek(fd, (off_t) 0, SEEK_SET);
365 len = (ssize_t)strlen(buf);
366 if ((num = write(fd, buf, (size_t)len)) != len)
367 log_it("CRON", pid, "ERROR", "write() failed", errno);
368 else {
369 if (ftruncate(fd, num) == -1)
370 log_it("CRON", pid, "ERROR", "ftruncate() failed", errno);
371 }
372
373
374
375
376 }
377
378
379
380 int get_char(FILE * file) {
381 int ch;
382
383 ch = getc(file);
384 if (ch == '\n')
385 Set_LineNum(LineNumber + 1)
386 return (ch);
387 }
388
389
390
391 void unget_char(int ch, FILE * file) {
392 ungetc(ch, file);
393 if (ch == '\n')
394 Set_LineNum(LineNumber - 1)
395 }
396
397
398
399
400
401
402
403 int get_string(char *string, int size, FILE * file, const char *terms) {
404 int ch;
405
406 while (EOF != (ch = get_char(file)) && !strchr(terms, ch)) {
407 if (size > 1) {
408 *string++ = (char) ch;
409 size--;
410 }
411 }
412
413 if (size > 0)
414 *string = '\0';
415
416 return (ch);
417 }
418
419
420
421 void skip_comments(FILE * file) {
422 int ch;
423
424 while (EOF != (ch = get_char(file))) {
425
426
427 while (ch == ' ' || ch == '\t')
428 ch = get_char(file);
429
430 if (ch == EOF)
431 break;
432
433
434
435
436 if (ch != '\n' && ch != '#')
437 break;
438
439
440
441
442
443 while (ch != '\n' && ch != EOF)
444 ch = get_char(file);
445
446
447
448
449 }
450 if (ch != EOF)
451 unget_char(ch, file);
452 }
453
454
455
456
457
458 static int in_file(const char *string, FILE * file, int error) {
459 char line[MAX_TEMPSTR];
460 char *endp;
461
462 if (fseek(file, 0L, SEEK_SET))
463 return (error);
464 while (fgets(line, MAX_TEMPSTR, file)) {
465 if (line[0] != '\0') {
466 endp = &line[strlen(line) - 1];
467 if (*endp != '\n')
468 return (error);
469 *endp = '\0';
470 if (0 == strcmp(line, string))
471 return (TRUE);
472 }
473 }
474 if (ferror(file))
475 return (error);
476 return (FALSE);
477 }
478
479
480
481
482
483
484 int allowed(const char *username, const char *allow_file,
485 const char *deny_file) {
486 FILE *fp;
487 int isallowed;
488 char buf[128];
489
490 if (getuid() == 0)
491 return TRUE;
492 isallowed = FALSE;
493 if ((fp = fopen(allow_file, "r")) != NULL) {
494 isallowed = in_file(username, fp, FALSE);
495 fclose(fp);
496 if ((getuid() == 0) && (!isallowed)) {
497 snprintf(buf, sizeof (buf),
498 "root used -u for user %s not in cron.allow", username);
499 log_it("crontab", getpid(), "warning", buf, 0);
500 isallowed = TRUE;
501 }
502 }
503 else if ((fp = fopen(deny_file, "r")) != NULL) {
504 isallowed = !in_file(username, fp, FALSE);
505 fclose(fp);
506 if ((getuid() == 0) && (!isallowed)) {
507 snprintf(buf, sizeof (buf),
508 "root used -u for user %s in cron.deny", username);
509 log_it("crontab", getpid(), "warning", buf, 0);
510 isallowed = TRUE;
511 }
512 }
513 #ifdef WITH_AUDIT
514 if (isallowed == FALSE) {
515 int audit_fd = audit_open();
516 audit_log_user_message(audit_fd, AUDIT_USER_START, "cron deny",
517 NULL, NULL, NULL, 0);
518 close(audit_fd);
519 }
520 #endif
521 return (isallowed);
522 }
523
524 void log_it(const char *username, PID_T xpid, const char *event,
525 const char *detail, int err) {
526 #if defined(LOG_FILE) || DEBUGGING
527 PID_T pid = xpid;
528 #endif
529 #if defined(LOG_FILE)
530 char *msg;
531 TIME_T now = time((TIME_T) 0);
532 struct tm *t = localtime(&now);
533 int msg_size;
534 #endif
535
536 #if defined(LOG_FILE)
537
538
539 msg = malloc(msg_size = (strlen(username)
540 + strlen(event)
541 + strlen(detail)
542 + MAX_TEMPSTR)
543 );
544 if (msg == NULL) {
545 fprintf(stderr, "%s: Run OUT OF MEMORY while %s\n",
546 ProgramName, __FUNCTION__);
547 return;
548 }
549 if (LogFD < OK) {
550 LogFD = open(LOG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0600);
551 if (LogFD < OK) {
552 fprintf(stderr, "%s: can't open log file\n", ProgramName);
553 perror(LOG_FILE);
554 }
555 else {
556 (void) fcntl(LogFD, F_SETFD, 1);
557 }
558 }
559
560
561
562
563
564 snprintf(msg, msg_size,
565 "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)%s%s\n", username,
566 t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, pid,
567 event, detail, err != 0 ? ": " : "", err != 0 ? strerror(err) : "");
568
569
570
571 if (LogFD < OK || write(LogFD, msg, strlen(msg)) < OK) {
572 if (LogFD >= OK)
573 perror(LOG_FILE);
574 fprintf(stderr, "%s: can't write to log file\n", ProgramName);
575 write(STDERR, msg, strlen(msg));
576 }
577
578 free(msg);
579 #endif
580
581 #if defined(SYSLOG)
582 if (!syslog_open) {
583 # ifdef LOG_DAEMON
584 openlog(ProgramName, LOG_PID, FACILITY);
585 # else
586 openlog(ProgramName, LOG_PID);
587 # endif
588 syslog_open = TRUE;
589 }
590
591 syslog(err != 0 ? LOG_ERR : LOG_INFO,
592 "(%s) %s (%s)%s%s", username, event, detail,
593 err != 0 ? ": " : "", err != 0 ? strerror(err) : "");
594
595
596 #endif
597 #if DEBUGGING
598 if (DebugFlags) {
599 fprintf(stderr, "log_it: (%s %ld) %s (%s)%s%s\n",
600 username, (long) pid, event, detail,
601 err != 0 ? ": " : "", err != 0 ? strerror(err) : "");
602 }
603 #endif
604 }
605
606 void log_close(void) {
607 if (LogFD != ERR) {
608 close(LogFD);
609 LogFD = ERR;
610 }
611 #if defined(SYSLOG)
612 closelog();
613 syslog_open = FALSE;
614 #endif
615 }
616
617
618
619
620
621
622
623
624
625
626 char *first_word(const char *s, const char *t) {
627 static char retbuf[2][MAX_TEMPSTR + 1];
628 static int retsel = 0;
629 char *rb, *rp;
630
631
632 retsel = 1 - retsel;
633 rb = &retbuf[retsel][0];
634 rp = rb;
635
636
637 while (*s && (NULL != strchr(t, *s))) {
638 s++;
639 }
640
641
642 while (*s && (NULL == strchr(t, *s)) && (rp < &rb[MAX_TEMPSTR])) {
643 *rp++ = *s++;
644 }
645
646
647 *rp = '\0';
648 return (rb);
649 }
650
651
652
653
654 static void mkprint(char *dst, unsigned char *src, size_t len) {
655
656
657
658
659
660 while (len-- > 0) {
661 unsigned char ch = *src++;
662
663 if (ch < ' ') {
664 *dst++ = '^';
665 *dst++ = (char)(ch + '@');
666 }
667 else if (ch < 0177) {
668 *dst++ = (char)ch;
669 }
670 else if (ch == 0177) {
671 *dst++ = '^';
672 *dst++ = '?';
673 }
674 else {
675 sprintf(dst, "\\%03o", ch);
676 dst += 4;
677 }
678 }
679 *dst = '\0';
680 }
681
682
683
684
685 char *mkprints(unsigned char *src, size_t len) {
686 char *dst = malloc(len * 4 + 1);
687
688 if (dst)
689 mkprint(dst, src, len);
690
691 return (dst);
692 }
693
694 #ifdef MAIL_DATE
695
696
697
698 char *arpadate(time_t *clock) {
699 time_t t = clock ? *clock : time((TIME_T) 0);
700 struct tm tm = *localtime(&t);
701 long gmtoff = get_gmtoff(&t, &tm);
702 int hours = gmtoff / SECONDS_PER_HOUR;
703 int minutes =
704 (gmtoff - (hours * SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE;
705 static char ret[64];
706
707 (void) sprintf(ret, "%s, %2d %s %2d %02d:%02d:%02d %.2d%.2d (%s)",
708 DowNames[tm.tm_wday],
709 tm.tm_mday,
710 MonthNames[tm.tm_mon],
711 tm.tm_year + 1900,
712 tm.tm_hour, tm.tm_min, tm.tm_sec, hours, minutes, TZONE(tm));
713 return (ret);
714 }
715 #endif
716
717 #ifdef HAVE_SAVED_UIDS
718 static uid_t save_euid;
719 static gid_t save_egid;
720
721 int swap_uids(void) {
722 save_egid = getegid();
723 save_euid = geteuid();
724 return ((setegid(getgid()) || seteuid(getuid()))? -1 : 0);
725 }
726
727 int swap_uids_back(void) {
728 return ((setegid(save_egid) || seteuid(save_euid)) ? -1 : 0);
729 }
730
731 #else
732
733 int swap_uids(void) {
734 return ((setregid(getegid(), getgid())
735 || setreuid(geteuid(), getuid())) ? -1 : 0);
736 }
737
738 int swap_uids_back(void) {
739 return (swap_uids());
740 }
741 #endif
742
743 size_t strlens(const char *last, ...) {
744 va_list ap;
745 size_t ret = 0;
746 const char *str;
747
748 va_start(ap, last);
749 for (str = last; str != NULL; str = va_arg(ap, const char *))
750 ret += strlen(str);
751 va_end(ap);
752 return (ret);
753 }
754
755
756
757
758
759
760
761 #ifndef HAVE_STRUCT_TM_TM_GMTOFF
762 long get_gmtoff(time_t * clock, struct tm *local) {
763 struct tm gmt;
764 long offset;
765
766 gmt = *gmtime(clock);
767 if (local == NULL)
768 local = localtime(clock);
769
770 offset = (local->tm_sec - gmt.tm_sec) +
771 ((local->tm_min - gmt.tm_min) * 60) +
772 ((local->tm_hour - gmt.tm_hour) * 3600);
773
774
775 if (local->tm_year < gmt.tm_year)
776 offset -= 24 * 3600;
777 else if (local->tm_year > gmt.tm_year)
778 offset += 24 * 3600;
779 else if (local->tm_yday < gmt.tm_yday)
780 offset -= 24 * 3600;
781 else if (local->tm_yday > gmt.tm_yday)
782 offset += 24 * 3600;
783
784 return (offset);
785 }
786 #endif