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 52b52f2afSjk217608 * Common Development and Distribution License (the "License"). 62b52f2afSjk217608 * 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 /* 232b52f2afSjk217608 * 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 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/stat.h> 327c478bd9Sstevel@tonic-gate #include <sys/param.h> 337c478bd9Sstevel@tonic-gate #include <fcntl.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <ctype.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <dirent.h> 387c478bd9Sstevel@tonic-gate #include <libintl.h> 397c478bd9Sstevel@tonic-gate #include <errno.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <tzfile.h> 437c478bd9Sstevel@tonic-gate #include "cron.h" 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #define CANTCD "can't change directory to the at directory" 467c478bd9Sstevel@tonic-gate #define NOREADDIR "can't read the at directory" 477c478bd9Sstevel@tonic-gate #define YEAR 1900 487c478bd9Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate time_t 517c478bd9Sstevel@tonic-gate num(char **ptr) 527c478bd9Sstevel@tonic-gate { 537c478bd9Sstevel@tonic-gate time_t n = 0; 547c478bd9Sstevel@tonic-gate while (isdigit(**ptr)) { 557c478bd9Sstevel@tonic-gate n = n*10 + (**ptr - '0'); 567c478bd9Sstevel@tonic-gate *ptr += 1; } 577c478bd9Sstevel@tonic-gate return (n); 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 627c478bd9Sstevel@tonic-gate 63032624d5Sbasabi int 647c478bd9Sstevel@tonic-gate days_btwn(int m1, int d1, int y1, int m2, int d2, int y2) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * calculate the number of "full" days in between 687c478bd9Sstevel@tonic-gate * m1/d1/y1 and m2/d2/y2. 697c478bd9Sstevel@tonic-gate * NOTE: there should not be more than a year separation in the 707c478bd9Sstevel@tonic-gate * dates. also, m should be in 0 to 11, and d should be in 1 to 31. 717c478bd9Sstevel@tonic-gate */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate int days; 747c478bd9Sstevel@tonic-gate int m; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate if ((m1 == m2) && (d1 == d2) && (y1 == y2)) 777c478bd9Sstevel@tonic-gate return (0); 787c478bd9Sstevel@tonic-gate if ((m1 == m2) && (d1 < d2)) { 797c478bd9Sstevel@tonic-gate /* 807c478bd9Sstevel@tonic-gate * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not 817c478bd9Sstevel@tonic-gate * a leap year, this function should return the days till the 827c478bd9Sstevel@tonic-gate * the next Feb 29.See Bug 4257355. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate if (d2 > days_in_mon(m2, y2)) { 857c478bd9Sstevel@tonic-gate int p; 862b52f2afSjk217608 for (p = 1; ! isleap(y2+YEAR+p); p++) 872b52f2afSjk217608 ; 887c478bd9Sstevel@tonic-gate return (p*365 + d2-d1-1); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate return (d2-d1-1); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate /* the remaining dates are on different months */ 937c478bd9Sstevel@tonic-gate days = (days_in_mon(m1, y1)-d1) + (d2-1); 947c478bd9Sstevel@tonic-gate m = (m1 + 1) % 12; 957c478bd9Sstevel@tonic-gate while (m != m2) { 967c478bd9Sstevel@tonic-gate if (m == 0) 977c478bd9Sstevel@tonic-gate y1++; 987c478bd9Sstevel@tonic-gate days += days_in_mon(m, y1); 997c478bd9Sstevel@tonic-gate m = (m + 1) % 12; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate return (days); 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate int 1057c478bd9Sstevel@tonic-gate days_in_mon(int m, int y) 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * returns the number of days in month m of year y 1097c478bd9Sstevel@tonic-gate * NOTE: m should be in the range 0 to 11 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0)); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate void * 1157c478bd9Sstevel@tonic-gate xmalloc(size_t size) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate char *p; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate if ((p = malloc(size)) == NULL) { 1207c478bd9Sstevel@tonic-gate perror("malloc"); 1217c478bd9Sstevel@tonic-gate exit(55); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate return (p); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate void 1277c478bd9Sstevel@tonic-gate cron_sendmsg(char action, char *login, char *fname, char etype) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate static int msgfd = -2; 1307c478bd9Sstevel@tonic-gate struct message *pmsg; 1317c478bd9Sstevel@tonic-gate int i; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate pmsg = &msgbuf; 1347c478bd9Sstevel@tonic-gate if (msgfd == -2) { 1357c478bd9Sstevel@tonic-gate if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) { 1367c478bd9Sstevel@tonic-gate if (errno == ENXIO || errno == ENOENT) 1377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cron may not" 1387c478bd9Sstevel@tonic-gate " be running - call your system" 1397c478bd9Sstevel@tonic-gate " administrator\n")); 1407c478bd9Sstevel@tonic-gate else 1417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1427c478bd9Sstevel@tonic-gate "error in message queue open\n")); 1437c478bd9Sstevel@tonic-gate return; 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate pmsg->etype = etype; 1477c478bd9Sstevel@tonic-gate pmsg->action = action; 1487c478bd9Sstevel@tonic-gate (void) strncpy(pmsg->fname, fname, FLEN); 1497c478bd9Sstevel@tonic-gate (void) strncpy(pmsg->logname, login, LLEN); 1507c478bd9Sstevel@tonic-gate if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0) 1517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("error in message send\n")); 1527c478bd9Sstevel@tonic-gate else if (i != sizeof (struct message)) 1537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1547c478bd9Sstevel@tonic-gate "error in message send: Premature EOF\n")); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate char 1587c478bd9Sstevel@tonic-gate *errmsg(int errnum) 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate char *msg; 1617c478bd9Sstevel@tonic-gate static char msgbuf[32]; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate msg = strerror(errnum); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if (msg == NULL) { 1667c478bd9Sstevel@tonic-gate (void) snprintf(msgbuf, sizeof (msgbuf), 1677c478bd9Sstevel@tonic-gate gettext("Error %d"), errnum); 1687c478bd9Sstevel@tonic-gate return (msgbuf); 1697c478bd9Sstevel@tonic-gate } else 1707c478bd9Sstevel@tonic-gate return (msg); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate int 1747c478bd9Sstevel@tonic-gate filewanted(struct dirent *direntry) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate char *p; 177032624d5Sbasabi char c; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate p = direntry->d_name; 1807c478bd9Sstevel@tonic-gate (void) num(&p); 1817c478bd9Sstevel@tonic-gate if (p == direntry->d_name) 1827c478bd9Sstevel@tonic-gate return (0); /* didn't start with a number */ 1837c478bd9Sstevel@tonic-gate if (*p++ != '.') 1847c478bd9Sstevel@tonic-gate return (0); /* followed by a period */ 1857c478bd9Sstevel@tonic-gate c = *p++; 1867c478bd9Sstevel@tonic-gate if (c < 'a' || c > 'z') 1877c478bd9Sstevel@tonic-gate return (0); /* followed by a queue name */ 1887c478bd9Sstevel@tonic-gate if (audit_cron_is_anc_name(direntry->d_name)) 1897c478bd9Sstevel@tonic-gate return (0); 1907c478bd9Sstevel@tonic-gate return (1); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate void * 1947c478bd9Sstevel@tonic-gate xcalloc(size_t nElements, size_t size) 1957c478bd9Sstevel@tonic-gate { 1967c478bd9Sstevel@tonic-gate void *p; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if ((p = calloc(nElements, size)) == NULL) { 1997c478bd9Sstevel@tonic-gate perror("calloc"); 2007c478bd9Sstevel@tonic-gate exit(55); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate return (p); 2037c478bd9Sstevel@tonic-gate } 204*5b08e637SChris Gerhard 205*5b08e637SChris Gerhard int 206*5b08e637SChris Gerhard isvalid_shell(const char *shell) 207*5b08e637SChris Gerhard { 208*5b08e637SChris Gerhard char *t; 209*5b08e637SChris Gerhard int ret = 0; 210*5b08e637SChris Gerhard 211*5b08e637SChris Gerhard while ((t = getusershell()) != NULL) { 212*5b08e637SChris Gerhard if (strcmp(t, shell) == 0) { 213*5b08e637SChris Gerhard ret = 1; 214*5b08e637SChris Gerhard break; 215*5b08e637SChris Gerhard } 216*5b08e637SChris Gerhard } 217*5b08e637SChris Gerhard endusershell(); 218*5b08e637SChris Gerhard return (ret); 219*5b08e637SChris Gerhard } 220*5b08e637SChris Gerhard 221*5b08e637SChris Gerhard int 222*5b08e637SChris Gerhard isvalid_dir(const char *dir) 223*5b08e637SChris Gerhard { 224*5b08e637SChris Gerhard char *cwd = getcwd(NULL, 0); 225*5b08e637SChris Gerhard 226*5b08e637SChris Gerhard if (dir[0] != '/' || chdir(dir) == -1) { 227*5b08e637SChris Gerhard return (0); 228*5b08e637SChris Gerhard } 229*5b08e637SChris Gerhard if (cwd != NULL) { 230*5b08e637SChris Gerhard (void) chdir(cwd); 231*5b08e637SChris Gerhard } 232*5b08e637SChris Gerhard return (1); 233*5b08e637SChris Gerhard } 234