xref: /illumos-gate/usr/src/cmd/cron/funcs.c (revision 951bafea)
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
52b52f2afSjk  * Common Development and Distribution License (the "License").
62b52f2afSjk  * 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 /*
23d1419d5aSNobutomo Nakano  * Copyright 2009 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
num(char ** ptr)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
days_btwn(int m1,int d1,int y1,int m2,int d2,int y2)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;
862b52f2afSjk 			for (p = 1; ! isleap(y2+YEAR+p); p++)
872b52f2afSjk 				;
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
days_in_mon(int m,int y)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 *
xmalloc(size_t size)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 
126d1419d5aSNobutomo Nakano void *
xcalloc(size_t nElements,size_t size)127d1419d5aSNobutomo Nakano xcalloc(size_t nElements, size_t size)
128d1419d5aSNobutomo Nakano {
129d1419d5aSNobutomo Nakano 	void *p;
130d1419d5aSNobutomo Nakano 
131d1419d5aSNobutomo Nakano 	if ((p = calloc(nElements, size)) == NULL) {
132d1419d5aSNobutomo Nakano 		perror("calloc");
133d1419d5aSNobutomo Nakano 		exit(55);
134d1419d5aSNobutomo Nakano 	}
135d1419d5aSNobutomo Nakano 	return (p);
136d1419d5aSNobutomo Nakano }
137d1419d5aSNobutomo Nakano 
138d1419d5aSNobutomo Nakano char *
xstrdup(const char * str)139d1419d5aSNobutomo Nakano xstrdup(const char *str)
140d1419d5aSNobutomo Nakano {
141d1419d5aSNobutomo Nakano 	int	len;
142d1419d5aSNobutomo Nakano 	char	*p;
143d1419d5aSNobutomo Nakano 
144d1419d5aSNobutomo Nakano 	len = strlen(str);
145d1419d5aSNobutomo Nakano 	p = xmalloc(len + 1);
146d1419d5aSNobutomo Nakano 	(void) memcpy(p, str, len);
147d1419d5aSNobutomo Nakano 	p[len] = '\0';
148d1419d5aSNobutomo Nakano 
149d1419d5aSNobutomo Nakano 	return (p);
150d1419d5aSNobutomo Nakano }
151d1419d5aSNobutomo Nakano 
1527c478bd9Sstevel@tonic-gate void
cron_sendmsg(char action,char * login,char * fname,char etype)1537c478bd9Sstevel@tonic-gate cron_sendmsg(char action, char *login, char *fname, char etype)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	static int	msgfd = -2;
156d1419d5aSNobutomo Nakano 	struct message	*pmsg, msgbuf;
1577c478bd9Sstevel@tonic-gate 	int	i;
1587c478bd9Sstevel@tonic-gate 
159d1419d5aSNobutomo Nakano 	(void) memset(&msgbuf, 0, sizeof (msgbuf));
1607c478bd9Sstevel@tonic-gate 	pmsg = &msgbuf;
1617c478bd9Sstevel@tonic-gate 	if (msgfd == -2) {
1627c478bd9Sstevel@tonic-gate 		if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
1637c478bd9Sstevel@tonic-gate 			if (errno == ENXIO || errno == ENOENT)
1647c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("cron may not"
1657c478bd9Sstevel@tonic-gate 				    " be running - call your system"
1667c478bd9Sstevel@tonic-gate 				    " administrator\n"));
1677c478bd9Sstevel@tonic-gate 			else
1687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1697c478bd9Sstevel@tonic-gate 				    "error in message queue open\n"));
1707c478bd9Sstevel@tonic-gate 			return;
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	pmsg->etype = etype;
1747c478bd9Sstevel@tonic-gate 	pmsg->action = action;
175*951bafeaSRichard Lowe 	(void) strlcpy(pmsg->fname, fname, FLEN);
176*951bafeaSRichard Lowe 	(void) strlcpy(pmsg->logname, login, LLEN);
1777c478bd9Sstevel@tonic-gate 	if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
1787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("error in message send\n"));
1797c478bd9Sstevel@tonic-gate 	else if (i != sizeof (struct message))
1807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1817c478bd9Sstevel@tonic-gate 		    "error in message send: Premature EOF\n"));
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate char
errmsg(int errnum)1857c478bd9Sstevel@tonic-gate *errmsg(int errnum)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	char *msg;
188d1419d5aSNobutomo Nakano 	static char msg_buf[32];
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	msg = strerror(errnum);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (msg == NULL) {
193d1419d5aSNobutomo Nakano 		(void) snprintf(msg_buf, sizeof (msg_buf),
1947c478bd9Sstevel@tonic-gate 		    gettext("Error %d"), errnum);
195d1419d5aSNobutomo Nakano 		return (msg_buf);
1967c478bd9Sstevel@tonic-gate 	} else
1977c478bd9Sstevel@tonic-gate 		return (msg);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate int
filewanted(struct dirent * direntry)2017c478bd9Sstevel@tonic-gate filewanted(struct dirent *direntry)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	char		*p;
204032624d5Sbasabi 	char		c;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	p = direntry->d_name;
2077c478bd9Sstevel@tonic-gate 	(void) num(&p);
2087c478bd9Sstevel@tonic-gate 	if (p == direntry->d_name)
2097c478bd9Sstevel@tonic-gate 		return (0);	/* didn't start with a number */
2107c478bd9Sstevel@tonic-gate 	if (*p++ != '.')
2117c478bd9Sstevel@tonic-gate 		return (0);	/* followed by a period */
2127c478bd9Sstevel@tonic-gate 	c = *p++;
2137c478bd9Sstevel@tonic-gate 	if (c < 'a' || c > 'z')
2147c478bd9Sstevel@tonic-gate 		return (0);	/* followed by a queue name */
2157c478bd9Sstevel@tonic-gate 	if (audit_cron_is_anc_name(direntry->d_name))
2167c478bd9Sstevel@tonic-gate 		return (0);
2177c478bd9Sstevel@tonic-gate 	return (1);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2205b08e637SChris Gerhard int
isvalid_shell(const char * shell)2215b08e637SChris Gerhard isvalid_shell(const char *shell)
2225b08e637SChris Gerhard {
2235b08e637SChris Gerhard 	char *t;
2245b08e637SChris Gerhard 	int ret = 0;
2255b08e637SChris Gerhard 
2265b08e637SChris Gerhard 	while ((t =  getusershell()) != NULL) {
2275b08e637SChris Gerhard 		if (strcmp(t, shell) == 0) {
2285b08e637SChris Gerhard 			ret = 1;
2295b08e637SChris Gerhard 			break;
2305b08e637SChris Gerhard 		}
2315b08e637SChris Gerhard 	}
2325b08e637SChris Gerhard 	endusershell();
2335b08e637SChris Gerhard 	return (ret);
2345b08e637SChris Gerhard }
2355b08e637SChris Gerhard 
2365b08e637SChris Gerhard int
isvalid_dir(const char * dir)2375b08e637SChris Gerhard isvalid_dir(const char *dir)
2385b08e637SChris Gerhard {
2395b08e637SChris Gerhard 	char *cwd = getcwd(NULL, 0);
2405b08e637SChris Gerhard 
2415b08e637SChris Gerhard 	if (dir[0] != '/' || chdir(dir) == -1) {
2425b08e637SChris Gerhard 		return (0);
2435b08e637SChris Gerhard 	}
2445b08e637SChris Gerhard 	if (cwd != NULL) {
2455b08e637SChris Gerhard 		(void) chdir(cwd);
2465b08e637SChris Gerhard 	}
2475b08e637SChris Gerhard 	return (1);
2485b08e637SChris Gerhard }
249