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