xref: /titanic_53/usr/src/cmd/cron/funcs.c (revision 032624d56c174c5c55126582b32e314a6af15522)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*032624d5Sbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28*032624d5Sbasabi /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*032624d5Sbasabi /*	  All Rights Reserved  	*/
30*032624d5Sbasabi 
31*032624d5Sbasabi #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <dirent.h>
417c478bd9Sstevel@tonic-gate #include <libintl.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <tzfile.h>
467c478bd9Sstevel@tonic-gate #include "cron.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	CANTCD		"can't change directory to the at directory"
497c478bd9Sstevel@tonic-gate #define	NOREADDIR	"can't read the at directory"
507c478bd9Sstevel@tonic-gate #define	YEAR		1900
517c478bd9Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate time_t
547c478bd9Sstevel@tonic-gate num(char **ptr)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	time_t n = 0;
577c478bd9Sstevel@tonic-gate 	while (isdigit(**ptr)) {
587c478bd9Sstevel@tonic-gate 		n = n*10 + (**ptr - '0');
597c478bd9Sstevel@tonic-gate 		*ptr += 1; }
607c478bd9Sstevel@tonic-gate 	return (n);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
657c478bd9Sstevel@tonic-gate 
66*032624d5Sbasabi int
677c478bd9Sstevel@tonic-gate days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	/*
707c478bd9Sstevel@tonic-gate 	 * calculate the number of "full" days in between
717c478bd9Sstevel@tonic-gate 	 * m1/d1/y1 and m2/d2/y2.
727c478bd9Sstevel@tonic-gate 	 * NOTE: there should not be more than a year separation in the
737c478bd9Sstevel@tonic-gate 	 * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	int	days;
777c478bd9Sstevel@tonic-gate 	int	m;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if ((m1 == m2) && (d1 == d2) && (y1 == y2))
807c478bd9Sstevel@tonic-gate 		return (0);
817c478bd9Sstevel@tonic-gate 	if ((m1 == m2) && (d1 < d2)) {
827c478bd9Sstevel@tonic-gate 		/*
837c478bd9Sstevel@tonic-gate 		 * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
847c478bd9Sstevel@tonic-gate 		 * a leap year, this function should return the days till the
857c478bd9Sstevel@tonic-gate 		 * the next Feb 29.See Bug 4257355.
867c478bd9Sstevel@tonic-gate 		 */
877c478bd9Sstevel@tonic-gate 		if (d2 > days_in_mon(m2, y2)) {
887c478bd9Sstevel@tonic-gate 			int p;
897c478bd9Sstevel@tonic-gate 			for (p = 1; ! isleap(y2+YEAR+p); p++);
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 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate int
1787c478bd9Sstevel@tonic-gate filewanted(struct dirent *direntry)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	char		*p;
181*032624d5Sbasabi 	char		c;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	p = direntry->d_name;
1847c478bd9Sstevel@tonic-gate 	(void) num(&p);
1857c478bd9Sstevel@tonic-gate 	if (p == direntry->d_name)
1867c478bd9Sstevel@tonic-gate 		return (0);	/* didn't start with a number */
1877c478bd9Sstevel@tonic-gate 	if (*p++ != '.')
1887c478bd9Sstevel@tonic-gate 		return (0);	/* followed by a period */
1897c478bd9Sstevel@tonic-gate 	c = *p++;
1907c478bd9Sstevel@tonic-gate 	if (c < 'a' || c > 'z')
1917c478bd9Sstevel@tonic-gate 		return (0);	/* followed by a queue name */
1927c478bd9Sstevel@tonic-gate 	if (audit_cron_is_anc_name(direntry->d_name))
1937c478bd9Sstevel@tonic-gate 		return (0);
1947c478bd9Sstevel@tonic-gate 	return (1);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Scan the directory dirname calling select to make a list of selected
1997c478bd9Sstevel@tonic-gate  * directory entries then sort using qsort and compare routine dcomp.
2007c478bd9Sstevel@tonic-gate  * Returns the number of entries and a pointer to a list of pointers to
2017c478bd9Sstevel@tonic-gate  * struct direct (through namelist). Returns -1 if there were any errors.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #ifdef DIRSIZ
2067c478bd9Sstevel@tonic-gate #undef DIRSIZ
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate #endif
2097c478bd9Sstevel@tonic-gate #define	DIRSIZ(dp) \
2107c478bd9Sstevel@tonic-gate 	(dp)->d_reclen
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate int
2137c478bd9Sstevel@tonic-gate ascandir(dirname, namelist, select, dcomp)
2147c478bd9Sstevel@tonic-gate 	char		*dirname;
2157c478bd9Sstevel@tonic-gate 	struct dirent	*(*namelist[]);
2167c478bd9Sstevel@tonic-gate 	int		(*select)();
2177c478bd9Sstevel@tonic-gate 	int		(*dcomp)();
2187c478bd9Sstevel@tonic-gate {
219*032624d5Sbasabi 	struct dirent *d, *p, **names;
220*032624d5Sbasabi 	int nitems;
221*032624d5Sbasabi 	char *cp1, *cp2;
2227c478bd9Sstevel@tonic-gate 	struct stat stb;
2237c478bd9Sstevel@tonic-gate 	long arraysz;
2247c478bd9Sstevel@tonic-gate 	DIR *dirp;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
2277c478bd9Sstevel@tonic-gate 		return (-1);
2287c478bd9Sstevel@tonic-gate 	if (fstat(dirp->dd_fd, &stb) < 0)
2297c478bd9Sstevel@tonic-gate 		return (-1);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
2337c478bd9Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	arraysz = (stb.st_size / 24);
2367c478bd9Sstevel@tonic-gate 	names = (struct dirent **)malloc(arraysz * sizeof (struct dirent *));
2377c478bd9Sstevel@tonic-gate 	if (names == NULL)
2387c478bd9Sstevel@tonic-gate 		return (-1);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	nitems = 0;
2417c478bd9Sstevel@tonic-gate 	while ((d = readdir(dirp)) != NULL) {
2427c478bd9Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
2437c478bd9Sstevel@tonic-gate 			continue;	/* just selected names */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * Make a minimum size copy of the data
2477c478bd9Sstevel@tonic-gate 		 */
2487c478bd9Sstevel@tonic-gate 		p = (struct dirent *)malloc(DIRSIZ(d));
2497c478bd9Sstevel@tonic-gate 		if (p == NULL)
2507c478bd9Sstevel@tonic-gate 			return (-1);
2517c478bd9Sstevel@tonic-gate 		p->d_ino = d->d_ino;
2527c478bd9Sstevel@tonic-gate 		p->d_reclen = d->d_reclen;
2537c478bd9Sstevel@tonic-gate 		/* p->d_namlen = d->d_namlen; */
2547c478bd9Sstevel@tonic-gate 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * Check to make sure the array has space left and
2577c478bd9Sstevel@tonic-gate 		 * realloc the maximum size.
2587c478bd9Sstevel@tonic-gate 		 */
2597c478bd9Sstevel@tonic-gate 		if (++nitems >= arraysz) {
2607c478bd9Sstevel@tonic-gate 			if (fstat(dirp->dd_fd, &stb) < 0)
2617c478bd9Sstevel@tonic-gate 				return (-1);	/* just might have grown */
2627c478bd9Sstevel@tonic-gate 			arraysz = stb.st_size / 12;
2637c478bd9Sstevel@tonic-gate 			names = (struct dirent **)realloc((char *)names,
2647c478bd9Sstevel@tonic-gate 				arraysz * sizeof (struct dirent *));
2657c478bd9Sstevel@tonic-gate 			if (names == NULL)
2667c478bd9Sstevel@tonic-gate 				return (-1);
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		names[nitems-1] = p;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2717c478bd9Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
2727c478bd9Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct dirent *), dcomp);
2737c478bd9Sstevel@tonic-gate 	*namelist = names;
2747c478bd9Sstevel@tonic-gate 	return (nitems);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate void *
2787c478bd9Sstevel@tonic-gate xcalloc(size_t nElements, size_t size)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	void *p;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if ((p = calloc(nElements, size)) == NULL) {
2837c478bd9Sstevel@tonic-gate 		perror("calloc");
2847c478bd9Sstevel@tonic-gate 		exit(55);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	return (p);
2877c478bd9Sstevel@tonic-gate }
288