xref: /titanic_50/usr/src/cmd/cron/funcs.c (revision 60471b7bbfab236de7d8776aed871d919c5f81c3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <dirent.h>
38 #include <libintl.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <tzfile.h>
43 #include "cron.h"
44 
45 #define	CANTCD		"can't change directory to the at directory"
46 #define	NOREADDIR	"can't read the at directory"
47 #define	YEAR		1900
48 extern int audit_cron_is_anc_name(char *);
49 
50 time_t
51 num(char **ptr)
52 {
53 	time_t n = 0;
54 	while (isdigit(**ptr)) {
55 		n = n*10 + (**ptr - '0');
56 		*ptr += 1; }
57 	return (n);
58 }
59 
60 
61 static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
62 
63 int
64 days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
65 {
66 	/*
67 	 * calculate the number of "full" days in between
68 	 * m1/d1/y1 and m2/d2/y2.
69 	 * NOTE: there should not be more than a year separation in the
70 	 * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
71 	 */
72 
73 	int	days;
74 	int	m;
75 
76 	if ((m1 == m2) && (d1 == d2) && (y1 == y2))
77 		return (0);
78 	if ((m1 == m2) && (d1 < d2)) {
79 		/*
80 		 * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
81 		 * a leap year, this function should return the days till the
82 		 * the next Feb 29.See Bug 4257355.
83 		 */
84 		if (d2 > days_in_mon(m2, y2)) {
85 			int p;
86 			for (p = 1; ! isleap(y2+YEAR+p); p++)
87 				;
88 			return (p*365 + d2-d1-1);
89 		}
90 		return (d2-d1-1);
91 	}
92 	/* the remaining dates are on different months */
93 	days = (days_in_mon(m1, y1)-d1) + (d2-1);
94 	m = (m1 + 1) % 12;
95 	while (m != m2) {
96 		if (m == 0)
97 			y1++;
98 		days += days_in_mon(m, y1);
99 		m = (m + 1) % 12;
100 	}
101 	return (days);
102 }
103 
104 int
105 days_in_mon(int m, int y)
106 {
107 	/*
108 	 * returns the number of days in month m of year y
109 	 * NOTE: m should be in the range 0 to 11
110 	 */
111 	return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0));
112 }
113 
114 void *
115 xmalloc(size_t size)
116 {
117 	char *p;
118 
119 	if ((p = malloc(size)) == NULL) {
120 		perror("malloc");
121 		exit(55);
122 	}
123 	return (p);
124 }
125 
126 void
127 cron_sendmsg(char action, char *login, char *fname, char etype)
128 {
129 	static int	msgfd = -2;
130 	struct message	*pmsg;
131 	int	i;
132 
133 	pmsg = &msgbuf;
134 	if (msgfd == -2) {
135 		if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
136 			if (errno == ENXIO || errno == ENOENT)
137 				(void) fprintf(stderr, gettext("cron may not"
138 				    " be running - call your system"
139 				    " administrator\n"));
140 			else
141 				(void) fprintf(stderr, gettext(
142 				    "error in message queue open\n"));
143 			return;
144 		}
145 	}
146 	pmsg->etype = etype;
147 	pmsg->action = action;
148 	(void) strncpy(pmsg->fname, fname, FLEN);
149 	(void) strncpy(pmsg->logname, login, LLEN);
150 	if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
151 		(void) fprintf(stderr, gettext("error in message send\n"));
152 	else if (i != sizeof (struct message))
153 		(void) fprintf(stderr, gettext(
154 		    "error in message send: Premature EOF\n"));
155 }
156 
157 char
158 *errmsg(int errnum)
159 {
160 	char *msg;
161 	static char msgbuf[32];
162 
163 	msg = strerror(errnum);
164 
165 	if (msg == NULL) {
166 		(void) snprintf(msgbuf, sizeof (msgbuf),
167 		    gettext("Error %d"), errnum);
168 		return (msgbuf);
169 	} else
170 		return (msg);
171 }
172 
173 int
174 filewanted(struct dirent *direntry)
175 {
176 	char		*p;
177 	char		c;
178 
179 	p = direntry->d_name;
180 	(void) num(&p);
181 	if (p == direntry->d_name)
182 		return (0);	/* didn't start with a number */
183 	if (*p++ != '.')
184 		return (0);	/* followed by a period */
185 	c = *p++;
186 	if (c < 'a' || c > 'z')
187 		return (0);	/* followed by a queue name */
188 	if (audit_cron_is_anc_name(direntry->d_name))
189 		return (0);
190 	return (1);
191 }
192 
193 void *
194 xcalloc(size_t nElements, size_t size)
195 {
196 	void *p;
197 
198 	if ((p = calloc(nElements, size)) == NULL) {
199 		perror("calloc");
200 		exit(55);
201 	}
202 	return (p);
203 }
204 
205 int
206 isvalid_shell(const char *shell)
207 {
208 	char *t;
209 	int ret = 0;
210 
211 	while ((t =  getusershell()) != NULL) {
212 		if (strcmp(t, shell) == 0) {
213 			ret = 1;
214 			break;
215 		}
216 	}
217 	endusershell();
218 	return (ret);
219 }
220 
221 int
222 isvalid_dir(const char *dir)
223 {
224 	char *cwd = getcwd(NULL, 0);
225 
226 	if (dir[0] != '/' || chdir(dir) == -1) {
227 		return (0);
228 	}
229 	if (cwd != NULL) {
230 		(void) chdir(cwd);
231 	}
232 	return (1);
233 }
234