17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2b52f2afSjk217608 * Common Development and Distribution License (the "License"). 6*2b52f2afSjk217608 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 23*2b52f2afSjk217608 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27032624d5Sbasabi /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28032624d5Sbasabi /* All Rights Reserved */ 29032624d5Sbasabi 30032624d5Sbasabi #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/stat.h> 347c478bd9Sstevel@tonic-gate #include <sys/param.h> 357c478bd9Sstevel@tonic-gate #include <fcntl.h> 367c478bd9Sstevel@tonic-gate #include <stdlib.h> 377c478bd9Sstevel@tonic-gate #include <ctype.h> 387c478bd9Sstevel@tonic-gate #include <stdio.h> 397c478bd9Sstevel@tonic-gate #include <dirent.h> 407c478bd9Sstevel@tonic-gate #include <libintl.h> 417c478bd9Sstevel@tonic-gate #include <errno.h> 427c478bd9Sstevel@tonic-gate #include <string.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <tzfile.h> 457c478bd9Sstevel@tonic-gate #include "cron.h" 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #define CANTCD "can't change directory to the at directory" 487c478bd9Sstevel@tonic-gate #define NOREADDIR "can't read the at directory" 497c478bd9Sstevel@tonic-gate #define YEAR 1900 507c478bd9Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate time_t 537c478bd9Sstevel@tonic-gate num(char **ptr) 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate time_t n = 0; 567c478bd9Sstevel@tonic-gate while (isdigit(**ptr)) { 577c478bd9Sstevel@tonic-gate n = n*10 + (**ptr - '0'); 587c478bd9Sstevel@tonic-gate *ptr += 1; } 597c478bd9Sstevel@tonic-gate return (n); 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 647c478bd9Sstevel@tonic-gate 65032624d5Sbasabi int 667c478bd9Sstevel@tonic-gate days_btwn(int m1, int d1, int y1, int m2, int d2, int y2) 677c478bd9Sstevel@tonic-gate { 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * calculate the number of "full" days in between 707c478bd9Sstevel@tonic-gate * m1/d1/y1 and m2/d2/y2. 717c478bd9Sstevel@tonic-gate * NOTE: there should not be more than a year separation in the 727c478bd9Sstevel@tonic-gate * dates. also, m should be in 0 to 11, and d should be in 1 to 31. 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate int days; 767c478bd9Sstevel@tonic-gate int m; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate if ((m1 == m2) && (d1 == d2) && (y1 == y2)) 797c478bd9Sstevel@tonic-gate return (0); 807c478bd9Sstevel@tonic-gate if ((m1 == m2) && (d1 < d2)) { 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not 837c478bd9Sstevel@tonic-gate * a leap year, this function should return the days till the 847c478bd9Sstevel@tonic-gate * the next Feb 29.See Bug 4257355. 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate if (d2 > days_in_mon(m2, y2)) { 877c478bd9Sstevel@tonic-gate int p; 88*2b52f2afSjk217608 for (p = 1; ! isleap(y2+YEAR+p); p++) 89*2b52f2afSjk217608 ; 907c478bd9Sstevel@tonic-gate return (p*365 + d2-d1-1); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate return (d2-d1-1); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate /* the remaining dates are on different months */ 957c478bd9Sstevel@tonic-gate days = (days_in_mon(m1, y1)-d1) + (d2-1); 967c478bd9Sstevel@tonic-gate m = (m1 + 1) % 12; 977c478bd9Sstevel@tonic-gate while (m != m2) { 987c478bd9Sstevel@tonic-gate if (m == 0) 997c478bd9Sstevel@tonic-gate y1++; 1007c478bd9Sstevel@tonic-gate days += days_in_mon(m, y1); 1017c478bd9Sstevel@tonic-gate m = (m + 1) % 12; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate return (days); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate int 1077c478bd9Sstevel@tonic-gate days_in_mon(int m, int y) 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * returns the number of days in month m of year y 1117c478bd9Sstevel@tonic-gate * NOTE: m should be in the range 0 to 11 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0)); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate void * 1177c478bd9Sstevel@tonic-gate xmalloc(size_t size) 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate char *p; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate if ((p = malloc(size)) == NULL) { 1227c478bd9Sstevel@tonic-gate perror("malloc"); 1237c478bd9Sstevel@tonic-gate exit(55); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate return (p); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate void 1297c478bd9Sstevel@tonic-gate cron_sendmsg(char action, char *login, char *fname, char etype) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate static int msgfd = -2; 1327c478bd9Sstevel@tonic-gate struct message *pmsg; 1337c478bd9Sstevel@tonic-gate int i; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate pmsg = &msgbuf; 1367c478bd9Sstevel@tonic-gate if (msgfd == -2) { 1377c478bd9Sstevel@tonic-gate if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) { 1387c478bd9Sstevel@tonic-gate if (errno == ENXIO || errno == ENOENT) 1397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cron may not" 1407c478bd9Sstevel@tonic-gate " be running - call your system" 1417c478bd9Sstevel@tonic-gate " administrator\n")); 1427c478bd9Sstevel@tonic-gate else 1437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1447c478bd9Sstevel@tonic-gate "error in message queue open\n")); 1457c478bd9Sstevel@tonic-gate return; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate pmsg->etype = etype; 1497c478bd9Sstevel@tonic-gate pmsg->action = action; 1507c478bd9Sstevel@tonic-gate (void) strncpy(pmsg->fname, fname, FLEN); 1517c478bd9Sstevel@tonic-gate (void) strncpy(pmsg->logname, login, LLEN); 1527c478bd9Sstevel@tonic-gate if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0) 1537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("error in message send\n")); 1547c478bd9Sstevel@tonic-gate else if (i != sizeof (struct message)) 1557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1567c478bd9Sstevel@tonic-gate "error in message send: Premature EOF\n")); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate char 1607c478bd9Sstevel@tonic-gate *errmsg(int errnum) 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate char *msg; 1637c478bd9Sstevel@tonic-gate static char msgbuf[32]; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate msg = strerror(errnum); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate if (msg == NULL) { 1687c478bd9Sstevel@tonic-gate (void) snprintf(msgbuf, sizeof (msgbuf), 1697c478bd9Sstevel@tonic-gate gettext("Error %d"), errnum); 1707c478bd9Sstevel@tonic-gate return (msgbuf); 1717c478bd9Sstevel@tonic-gate } else 1727c478bd9Sstevel@tonic-gate return (msg); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate int 1767c478bd9Sstevel@tonic-gate filewanted(struct dirent *direntry) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate char *p; 179032624d5Sbasabi char c; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate p = direntry->d_name; 1827c478bd9Sstevel@tonic-gate (void) num(&p); 1837c478bd9Sstevel@tonic-gate if (p == direntry->d_name) 1847c478bd9Sstevel@tonic-gate return (0); /* didn't start with a number */ 1857c478bd9Sstevel@tonic-gate if (*p++ != '.') 1867c478bd9Sstevel@tonic-gate return (0); /* followed by a period */ 1877c478bd9Sstevel@tonic-gate c = *p++; 1887c478bd9Sstevel@tonic-gate if (c < 'a' || c > 'z') 1897c478bd9Sstevel@tonic-gate return (0); /* followed by a queue name */ 1907c478bd9Sstevel@tonic-gate if (audit_cron_is_anc_name(direntry->d_name)) 1917c478bd9Sstevel@tonic-gate return (0); 1927c478bd9Sstevel@tonic-gate return (1); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate void * 1967c478bd9Sstevel@tonic-gate xcalloc(size_t nElements, size_t size) 1977c478bd9Sstevel@tonic-gate { 1987c478bd9Sstevel@tonic-gate void *p; 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate if ((p = calloc(nElements, size)) == NULL) { 2017c478bd9Sstevel@tonic-gate perror("calloc"); 2027c478bd9Sstevel@tonic-gate exit(55); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate return (p); 2057c478bd9Sstevel@tonic-gate } 206