1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright 1988-2003 Sun Microsystems, Inc. All rights reserved. 28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.4 */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 36*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 37*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 38*7c478bd9Sstevel@tonic-gate #include <ctype.h> 39*7c478bd9Sstevel@tonic-gate #include <stdio.h> 40*7c478bd9Sstevel@tonic-gate #include <dirent.h> 41*7c478bd9Sstevel@tonic-gate #include <libintl.h> 42*7c478bd9Sstevel@tonic-gate #include <errno.h> 43*7c478bd9Sstevel@tonic-gate #include <string.h> 44*7c478bd9Sstevel@tonic-gate #include <unistd.h> 45*7c478bd9Sstevel@tonic-gate #include <tzfile.h> 46*7c478bd9Sstevel@tonic-gate #include "cron.h" 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #define CANTCD "can't change directory to the at directory" 49*7c478bd9Sstevel@tonic-gate #define NOREADDIR "can't read the at directory" 50*7c478bd9Sstevel@tonic-gate #define YEAR 1900 51*7c478bd9Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *); 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate time_t 54*7c478bd9Sstevel@tonic-gate num(char **ptr) 55*7c478bd9Sstevel@tonic-gate { 56*7c478bd9Sstevel@tonic-gate time_t n = 0; 57*7c478bd9Sstevel@tonic-gate while (isdigit(**ptr)) { 58*7c478bd9Sstevel@tonic-gate n = n*10 + (**ptr - '0'); 59*7c478bd9Sstevel@tonic-gate *ptr += 1; } 60*7c478bd9Sstevel@tonic-gate return (n); 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate days_btwn(int m1, int d1, int y1, int m2, int d2, int y2) 67*7c478bd9Sstevel@tonic-gate { 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * calculate the number of "full" days in between 70*7c478bd9Sstevel@tonic-gate * m1/d1/y1 and m2/d2/y2. 71*7c478bd9Sstevel@tonic-gate * NOTE: there should not be more than a year separation in the 72*7c478bd9Sstevel@tonic-gate * dates. also, m should be in 0 to 11, and d should be in 1 to 31. 73*7c478bd9Sstevel@tonic-gate */ 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate int days; 76*7c478bd9Sstevel@tonic-gate int m; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate if ((m1 == m2) && (d1 == d2) && (y1 == y2)) 79*7c478bd9Sstevel@tonic-gate return (0); 80*7c478bd9Sstevel@tonic-gate if ((m1 == m2) && (d1 < d2)) { 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not 83*7c478bd9Sstevel@tonic-gate * a leap year, this function should return the days till the 84*7c478bd9Sstevel@tonic-gate * the next Feb 29.See Bug 4257355. 85*7c478bd9Sstevel@tonic-gate */ 86*7c478bd9Sstevel@tonic-gate if (d2 > days_in_mon(m2, y2)) { 87*7c478bd9Sstevel@tonic-gate int p; 88*7c478bd9Sstevel@tonic-gate for (p = 1; ! isleap(y2+YEAR+p); p++); 89*7c478bd9Sstevel@tonic-gate return (p*365 + d2-d1-1); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate return (d2-d1-1); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate /* the remaining dates are on different months */ 94*7c478bd9Sstevel@tonic-gate days = (days_in_mon(m1, y1)-d1) + (d2-1); 95*7c478bd9Sstevel@tonic-gate m = (m1 + 1) % 12; 96*7c478bd9Sstevel@tonic-gate while (m != m2) { 97*7c478bd9Sstevel@tonic-gate if (m == 0) 98*7c478bd9Sstevel@tonic-gate y1++; 99*7c478bd9Sstevel@tonic-gate days += days_in_mon(m, y1); 100*7c478bd9Sstevel@tonic-gate m = (m + 1) % 12; 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate return (days); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate int 106*7c478bd9Sstevel@tonic-gate days_in_mon(int m, int y) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate /* 109*7c478bd9Sstevel@tonic-gate * returns the number of days in month m of year y 110*7c478bd9Sstevel@tonic-gate * NOTE: m should be in the range 0 to 11 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0)); 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate void * 116*7c478bd9Sstevel@tonic-gate xmalloc(size_t size) 117*7c478bd9Sstevel@tonic-gate { 118*7c478bd9Sstevel@tonic-gate char *p; 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate if ((p = malloc(size)) == NULL) { 121*7c478bd9Sstevel@tonic-gate perror("malloc"); 122*7c478bd9Sstevel@tonic-gate exit(55); 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate return (p); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate void 128*7c478bd9Sstevel@tonic-gate cron_sendmsg(char action, char *login, char *fname, char etype) 129*7c478bd9Sstevel@tonic-gate { 130*7c478bd9Sstevel@tonic-gate static int msgfd = -2; 131*7c478bd9Sstevel@tonic-gate struct message *pmsg; 132*7c478bd9Sstevel@tonic-gate int i; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate pmsg = &msgbuf; 135*7c478bd9Sstevel@tonic-gate if (msgfd == -2) { 136*7c478bd9Sstevel@tonic-gate if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) { 137*7c478bd9Sstevel@tonic-gate if (errno == ENXIO || errno == ENOENT) 138*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cron may not" 139*7c478bd9Sstevel@tonic-gate " be running - call your system" 140*7c478bd9Sstevel@tonic-gate " administrator\n")); 141*7c478bd9Sstevel@tonic-gate else 142*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 143*7c478bd9Sstevel@tonic-gate "error in message queue open\n")); 144*7c478bd9Sstevel@tonic-gate return; 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate pmsg->etype = etype; 148*7c478bd9Sstevel@tonic-gate pmsg->action = action; 149*7c478bd9Sstevel@tonic-gate (void) strncpy(pmsg->fname, fname, FLEN); 150*7c478bd9Sstevel@tonic-gate (void) strncpy(pmsg->logname, login, LLEN); 151*7c478bd9Sstevel@tonic-gate if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0) 152*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("error in message send\n")); 153*7c478bd9Sstevel@tonic-gate else if (i != sizeof (struct message)) 154*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 155*7c478bd9Sstevel@tonic-gate "error in message send: Premature EOF\n")); 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate char 159*7c478bd9Sstevel@tonic-gate *errmsg(int errnum) 160*7c478bd9Sstevel@tonic-gate { 161*7c478bd9Sstevel@tonic-gate char *msg; 162*7c478bd9Sstevel@tonic-gate static char msgbuf[32]; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate msg = strerror(errnum); 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate if (msg == NULL) { 167*7c478bd9Sstevel@tonic-gate (void) snprintf(msgbuf, sizeof (msgbuf), 168*7c478bd9Sstevel@tonic-gate gettext("Error %d"), errnum); 169*7c478bd9Sstevel@tonic-gate return (msgbuf); 170*7c478bd9Sstevel@tonic-gate } else 171*7c478bd9Sstevel@tonic-gate return (msg); 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate int 177*7c478bd9Sstevel@tonic-gate filewanted(struct dirent *direntry) 178*7c478bd9Sstevel@tonic-gate { 179*7c478bd9Sstevel@tonic-gate char *p; 180*7c478bd9Sstevel@tonic-gate register char c; 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate p = direntry->d_name; 183*7c478bd9Sstevel@tonic-gate (void) num(&p); 184*7c478bd9Sstevel@tonic-gate if (p == direntry->d_name) 185*7c478bd9Sstevel@tonic-gate return (0); /* didn't start with a number */ 186*7c478bd9Sstevel@tonic-gate if (*p++ != '.') 187*7c478bd9Sstevel@tonic-gate return (0); /* followed by a period */ 188*7c478bd9Sstevel@tonic-gate c = *p++; 189*7c478bd9Sstevel@tonic-gate if (c < 'a' || c > 'z') 190*7c478bd9Sstevel@tonic-gate return (0); /* followed by a queue name */ 191*7c478bd9Sstevel@tonic-gate if (audit_cron_is_anc_name(direntry->d_name)) 192*7c478bd9Sstevel@tonic-gate return (0); 193*7c478bd9Sstevel@tonic-gate return (1); 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate /* 197*7c478bd9Sstevel@tonic-gate * Scan the directory dirname calling select to make a list of selected 198*7c478bd9Sstevel@tonic-gate * directory entries then sort using qsort and compare routine dcomp. 199*7c478bd9Sstevel@tonic-gate * Returns the number of entries and a pointer to a list of pointers to 200*7c478bd9Sstevel@tonic-gate * struct direct (through namelist). Returns -1 if there were any errors. 201*7c478bd9Sstevel@tonic-gate */ 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate #ifdef DIRSIZ 205*7c478bd9Sstevel@tonic-gate #undef DIRSIZ 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate #endif 208*7c478bd9Sstevel@tonic-gate #define DIRSIZ(dp) \ 209*7c478bd9Sstevel@tonic-gate (dp)->d_reclen 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate int 212*7c478bd9Sstevel@tonic-gate ascandir(dirname, namelist, select, dcomp) 213*7c478bd9Sstevel@tonic-gate char *dirname; 214*7c478bd9Sstevel@tonic-gate struct dirent *(*namelist[]); 215*7c478bd9Sstevel@tonic-gate int (*select)(); 216*7c478bd9Sstevel@tonic-gate int (*dcomp)(); 217*7c478bd9Sstevel@tonic-gate { 218*7c478bd9Sstevel@tonic-gate register struct dirent *d, *p, **names; 219*7c478bd9Sstevel@tonic-gate register int nitems; 220*7c478bd9Sstevel@tonic-gate register char *cp1, *cp2; 221*7c478bd9Sstevel@tonic-gate struct stat stb; 222*7c478bd9Sstevel@tonic-gate long arraysz; 223*7c478bd9Sstevel@tonic-gate DIR *dirp; 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(dirname)) == NULL) 226*7c478bd9Sstevel@tonic-gate return (-1); 227*7c478bd9Sstevel@tonic-gate if (fstat(dirp->dd_fd, &stb) < 0) 228*7c478bd9Sstevel@tonic-gate return (-1); 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate /* 231*7c478bd9Sstevel@tonic-gate * estimate the array size by taking the size of the directory file 232*7c478bd9Sstevel@tonic-gate * and dividing it by a multiple of the minimum size entry. 233*7c478bd9Sstevel@tonic-gate */ 234*7c478bd9Sstevel@tonic-gate arraysz = (stb.st_size / 24); 235*7c478bd9Sstevel@tonic-gate names = (struct dirent **)malloc(arraysz * sizeof (struct dirent *)); 236*7c478bd9Sstevel@tonic-gate if (names == NULL) 237*7c478bd9Sstevel@tonic-gate return (-1); 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate nitems = 0; 240*7c478bd9Sstevel@tonic-gate while ((d = readdir(dirp)) != NULL) { 241*7c478bd9Sstevel@tonic-gate if (select != NULL && !(*select)(d)) 242*7c478bd9Sstevel@tonic-gate continue; /* just selected names */ 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate /* 245*7c478bd9Sstevel@tonic-gate * Make a minimum size copy of the data 246*7c478bd9Sstevel@tonic-gate */ 247*7c478bd9Sstevel@tonic-gate p = (struct dirent *)malloc(DIRSIZ(d)); 248*7c478bd9Sstevel@tonic-gate if (p == NULL) 249*7c478bd9Sstevel@tonic-gate return (-1); 250*7c478bd9Sstevel@tonic-gate p->d_ino = d->d_ino; 251*7c478bd9Sstevel@tonic-gate p->d_reclen = d->d_reclen; 252*7c478bd9Sstevel@tonic-gate /* p->d_namlen = d->d_namlen; */ 253*7c478bd9Sstevel@tonic-gate for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; ); 254*7c478bd9Sstevel@tonic-gate /* 255*7c478bd9Sstevel@tonic-gate * Check to make sure the array has space left and 256*7c478bd9Sstevel@tonic-gate * realloc the maximum size. 257*7c478bd9Sstevel@tonic-gate */ 258*7c478bd9Sstevel@tonic-gate if (++nitems >= arraysz) { 259*7c478bd9Sstevel@tonic-gate if (fstat(dirp->dd_fd, &stb) < 0) 260*7c478bd9Sstevel@tonic-gate return (-1); /* just might have grown */ 261*7c478bd9Sstevel@tonic-gate arraysz = stb.st_size / 12; 262*7c478bd9Sstevel@tonic-gate names = (struct dirent **)realloc((char *)names, 263*7c478bd9Sstevel@tonic-gate arraysz * sizeof (struct dirent *)); 264*7c478bd9Sstevel@tonic-gate if (names == NULL) 265*7c478bd9Sstevel@tonic-gate return (-1); 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate names[nitems-1] = p; 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 270*7c478bd9Sstevel@tonic-gate if (nitems && dcomp != NULL) 271*7c478bd9Sstevel@tonic-gate qsort(names, nitems, sizeof (struct dirent *), dcomp); 272*7c478bd9Sstevel@tonic-gate *namelist = names; 273*7c478bd9Sstevel@tonic-gate return (nitems); 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate void * 277*7c478bd9Sstevel@tonic-gate xcalloc(size_t nElements, size_t size) 278*7c478bd9Sstevel@tonic-gate { 279*7c478bd9Sstevel@tonic-gate void *p; 280*7c478bd9Sstevel@tonic-gate 281*7c478bd9Sstevel@tonic-gate if ((p = calloc(nElements, size)) == NULL) { 282*7c478bd9Sstevel@tonic-gate perror("calloc"); 283*7c478bd9Sstevel@tonic-gate exit(55); 284*7c478bd9Sstevel@tonic-gate } 285*7c478bd9Sstevel@tonic-gate return (p); 286*7c478bd9Sstevel@tonic-gate } 287