10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56628Sjk217608 * Common Development and Distribution License (the "License").
66628Sjk217608 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21523Sbasabi
22523Sbasabi /*
23*11115SNobutomo.Nakano@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24523Sbasabi * Use is subject to license terms.
25523Sbasabi */
26523Sbasabi
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stat.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <dirent.h>
380Sstevel@tonic-gate #include <libintl.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <tzfile.h>
430Sstevel@tonic-gate #include "cron.h"
440Sstevel@tonic-gate
450Sstevel@tonic-gate #define CANTCD "can't change directory to the at directory"
460Sstevel@tonic-gate #define NOREADDIR "can't read the at directory"
470Sstevel@tonic-gate #define YEAR 1900
480Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *);
490Sstevel@tonic-gate
500Sstevel@tonic-gate time_t
num(char ** ptr)510Sstevel@tonic-gate num(char **ptr)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate time_t n = 0;
540Sstevel@tonic-gate while (isdigit(**ptr)) {
550Sstevel@tonic-gate n = n*10 + (**ptr - '0');
560Sstevel@tonic-gate *ptr += 1; }
570Sstevel@tonic-gate return (n);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate
610Sstevel@tonic-gate static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
620Sstevel@tonic-gate
63523Sbasabi int
days_btwn(int m1,int d1,int y1,int m2,int d2,int y2)640Sstevel@tonic-gate days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate * calculate the number of "full" days in between
680Sstevel@tonic-gate * m1/d1/y1 and m2/d2/y2.
690Sstevel@tonic-gate * NOTE: there should not be more than a year separation in the
700Sstevel@tonic-gate * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate
730Sstevel@tonic-gate int days;
740Sstevel@tonic-gate int m;
750Sstevel@tonic-gate
760Sstevel@tonic-gate if ((m1 == m2) && (d1 == d2) && (y1 == y2))
770Sstevel@tonic-gate return (0);
780Sstevel@tonic-gate if ((m1 == m2) && (d1 < d2)) {
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
810Sstevel@tonic-gate * a leap year, this function should return the days till the
820Sstevel@tonic-gate * the next Feb 29.See Bug 4257355.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate if (d2 > days_in_mon(m2, y2)) {
850Sstevel@tonic-gate int p;
866628Sjk217608 for (p = 1; ! isleap(y2+YEAR+p); p++)
876628Sjk217608 ;
880Sstevel@tonic-gate return (p*365 + d2-d1-1);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate return (d2-d1-1);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate /* the remaining dates are on different months */
930Sstevel@tonic-gate days = (days_in_mon(m1, y1)-d1) + (d2-1);
940Sstevel@tonic-gate m = (m1 + 1) % 12;
950Sstevel@tonic-gate while (m != m2) {
960Sstevel@tonic-gate if (m == 0)
970Sstevel@tonic-gate y1++;
980Sstevel@tonic-gate days += days_in_mon(m, y1);
990Sstevel@tonic-gate m = (m + 1) % 12;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate return (days);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate int
days_in_mon(int m,int y)1050Sstevel@tonic-gate days_in_mon(int m, int y)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * returns the number of days in month m of year y
1090Sstevel@tonic-gate * NOTE: m should be in the range 0 to 11
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0));
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate void *
xmalloc(size_t size)1150Sstevel@tonic-gate xmalloc(size_t size)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate char *p;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if ((p = malloc(size)) == NULL) {
1200Sstevel@tonic-gate perror("malloc");
1210Sstevel@tonic-gate exit(55);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate return (p);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
126*11115SNobutomo.Nakano@Sun.COM void *
xcalloc(size_t nElements,size_t size)127*11115SNobutomo.Nakano@Sun.COM xcalloc(size_t nElements, size_t size)
128*11115SNobutomo.Nakano@Sun.COM {
129*11115SNobutomo.Nakano@Sun.COM void *p;
130*11115SNobutomo.Nakano@Sun.COM
131*11115SNobutomo.Nakano@Sun.COM if ((p = calloc(nElements, size)) == NULL) {
132*11115SNobutomo.Nakano@Sun.COM perror("calloc");
133*11115SNobutomo.Nakano@Sun.COM exit(55);
134*11115SNobutomo.Nakano@Sun.COM }
135*11115SNobutomo.Nakano@Sun.COM return (p);
136*11115SNobutomo.Nakano@Sun.COM }
137*11115SNobutomo.Nakano@Sun.COM
138*11115SNobutomo.Nakano@Sun.COM char *
xstrdup(const char * str)139*11115SNobutomo.Nakano@Sun.COM xstrdup(const char *str)
140*11115SNobutomo.Nakano@Sun.COM {
141*11115SNobutomo.Nakano@Sun.COM int len;
142*11115SNobutomo.Nakano@Sun.COM char *p;
143*11115SNobutomo.Nakano@Sun.COM
144*11115SNobutomo.Nakano@Sun.COM len = strlen(str);
145*11115SNobutomo.Nakano@Sun.COM p = xmalloc(len + 1);
146*11115SNobutomo.Nakano@Sun.COM (void) memcpy(p, str, len);
147*11115SNobutomo.Nakano@Sun.COM p[len] = '\0';
148*11115SNobutomo.Nakano@Sun.COM
149*11115SNobutomo.Nakano@Sun.COM return (p);
150*11115SNobutomo.Nakano@Sun.COM }
151*11115SNobutomo.Nakano@Sun.COM
1520Sstevel@tonic-gate void
cron_sendmsg(char action,char * login,char * fname,char etype)1530Sstevel@tonic-gate cron_sendmsg(char action, char *login, char *fname, char etype)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate static int msgfd = -2;
156*11115SNobutomo.Nakano@Sun.COM struct message *pmsg, msgbuf;
1570Sstevel@tonic-gate int i;
1580Sstevel@tonic-gate
159*11115SNobutomo.Nakano@Sun.COM (void) memset(&msgbuf, 0, sizeof (msgbuf));
1600Sstevel@tonic-gate pmsg = &msgbuf;
1610Sstevel@tonic-gate if (msgfd == -2) {
1620Sstevel@tonic-gate if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
1630Sstevel@tonic-gate if (errno == ENXIO || errno == ENOENT)
1640Sstevel@tonic-gate (void) fprintf(stderr, gettext("cron may not"
1650Sstevel@tonic-gate " be running - call your system"
1660Sstevel@tonic-gate " administrator\n"));
1670Sstevel@tonic-gate else
1680Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1690Sstevel@tonic-gate "error in message queue open\n"));
1700Sstevel@tonic-gate return;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate pmsg->etype = etype;
1740Sstevel@tonic-gate pmsg->action = action;
1750Sstevel@tonic-gate (void) strncpy(pmsg->fname, fname, FLEN);
1760Sstevel@tonic-gate (void) strncpy(pmsg->logname, login, LLEN);
1770Sstevel@tonic-gate if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
1780Sstevel@tonic-gate (void) fprintf(stderr, gettext("error in message send\n"));
1790Sstevel@tonic-gate else if (i != sizeof (struct message))
1800Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1810Sstevel@tonic-gate "error in message send: Premature EOF\n"));
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate char
errmsg(int errnum)1850Sstevel@tonic-gate *errmsg(int errnum)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate char *msg;
188*11115SNobutomo.Nakano@Sun.COM static char msg_buf[32];
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate msg = strerror(errnum);
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate if (msg == NULL) {
193*11115SNobutomo.Nakano@Sun.COM (void) snprintf(msg_buf, sizeof (msg_buf),
1940Sstevel@tonic-gate gettext("Error %d"), errnum);
195*11115SNobutomo.Nakano@Sun.COM return (msg_buf);
1960Sstevel@tonic-gate } else
1970Sstevel@tonic-gate return (msg);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate int
filewanted(struct dirent * direntry)2010Sstevel@tonic-gate filewanted(struct dirent *direntry)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate char *p;
204523Sbasabi char c;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate p = direntry->d_name;
2070Sstevel@tonic-gate (void) num(&p);
2080Sstevel@tonic-gate if (p == direntry->d_name)
2090Sstevel@tonic-gate return (0); /* didn't start with a number */
2100Sstevel@tonic-gate if (*p++ != '.')
2110Sstevel@tonic-gate return (0); /* followed by a period */
2120Sstevel@tonic-gate c = *p++;
2130Sstevel@tonic-gate if (c < 'a' || c > 'z')
2140Sstevel@tonic-gate return (0); /* followed by a queue name */
2150Sstevel@tonic-gate if (audit_cron_is_anc_name(direntry->d_name))
2160Sstevel@tonic-gate return (0);
2170Sstevel@tonic-gate return (1);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2208439SChris.Gerhard@sun.com int
isvalid_shell(const char * shell)2218439SChris.Gerhard@sun.com isvalid_shell(const char *shell)
2228439SChris.Gerhard@sun.com {
2238439SChris.Gerhard@sun.com char *t;
2248439SChris.Gerhard@sun.com int ret = 0;
2258439SChris.Gerhard@sun.com
2268439SChris.Gerhard@sun.com while ((t = getusershell()) != NULL) {
2278439SChris.Gerhard@sun.com if (strcmp(t, shell) == 0) {
2288439SChris.Gerhard@sun.com ret = 1;
2298439SChris.Gerhard@sun.com break;
2308439SChris.Gerhard@sun.com }
2318439SChris.Gerhard@sun.com }
2328439SChris.Gerhard@sun.com endusershell();
2338439SChris.Gerhard@sun.com return (ret);
2348439SChris.Gerhard@sun.com }
2358439SChris.Gerhard@sun.com
2368439SChris.Gerhard@sun.com int
isvalid_dir(const char * dir)2378439SChris.Gerhard@sun.com isvalid_dir(const char *dir)
2388439SChris.Gerhard@sun.com {
2398439SChris.Gerhard@sun.com char *cwd = getcwd(NULL, 0);
2408439SChris.Gerhard@sun.com
2418439SChris.Gerhard@sun.com if (dir[0] != '/' || chdir(dir) == -1) {
2428439SChris.Gerhard@sun.com return (0);
2438439SChris.Gerhard@sun.com }
2448439SChris.Gerhard@sun.com if (cwd != NULL) {
2458439SChris.Gerhard@sun.com (void) chdir(cwd);
2468439SChris.Gerhard@sun.com }
2478439SChris.Gerhard@sun.com return (1);
2488439SChris.Gerhard@sun.com }
249