xref: /titanic_50/usr/src/cmd/cron/funcs.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
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 2009 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 xcalloc(size_t nElements, size_t size)
128 {
129 	void *p;
130 
131 	if ((p = calloc(nElements, size)) == NULL) {
132 		perror("calloc");
133 		exit(55);
134 	}
135 	return (p);
136 }
137 
138 char *
139 xstrdup(const char *str)
140 {
141 	int	len;
142 	char	*p;
143 
144 	len = strlen(str);
145 	p = xmalloc(len + 1);
146 	(void) memcpy(p, str, len);
147 	p[len] = '\0';
148 
149 	return (p);
150 }
151 
152 void
153 cron_sendmsg(char action, char *login, char *fname, char etype)
154 {
155 	static int	msgfd = -2;
156 	struct message	*pmsg, msgbuf;
157 	int	i;
158 
159 	(void) memset(&msgbuf, 0, sizeof (msgbuf));
160 	pmsg = &msgbuf;
161 	if (msgfd == -2) {
162 		if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
163 			if (errno == ENXIO || errno == ENOENT)
164 				(void) fprintf(stderr, gettext("cron may not"
165 				    " be running - call your system"
166 				    " administrator\n"));
167 			else
168 				(void) fprintf(stderr, gettext(
169 				    "error in message queue open\n"));
170 			return;
171 		}
172 	}
173 	pmsg->etype = etype;
174 	pmsg->action = action;
175 	(void) strlcpy(pmsg->fname, fname, FLEN);
176 	(void) strlcpy(pmsg->logname, login, LLEN);
177 	if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
178 		(void) fprintf(stderr, gettext("error in message send\n"));
179 	else if (i != sizeof (struct message))
180 		(void) fprintf(stderr, gettext(
181 		    "error in message send: Premature EOF\n"));
182 }
183 
184 char
185 *errmsg(int errnum)
186 {
187 	char *msg;
188 	static char msg_buf[32];
189 
190 	msg = strerror(errnum);
191 
192 	if (msg == NULL) {
193 		(void) snprintf(msg_buf, sizeof (msg_buf),
194 		    gettext("Error %d"), errnum);
195 		return (msg_buf);
196 	} else
197 		return (msg);
198 }
199 
200 int
201 filewanted(struct dirent *direntry)
202 {
203 	char		*p;
204 	char		c;
205 
206 	p = direntry->d_name;
207 	(void) num(&p);
208 	if (p == direntry->d_name)
209 		return (0);	/* didn't start with a number */
210 	if (*p++ != '.')
211 		return (0);	/* followed by a period */
212 	c = *p++;
213 	if (c < 'a' || c > 'z')
214 		return (0);	/* followed by a queue name */
215 	if (audit_cron_is_anc_name(direntry->d_name))
216 		return (0);
217 	return (1);
218 }
219 
220 int
221 isvalid_shell(const char *shell)
222 {
223 	char *t;
224 	int ret = 0;
225 
226 	while ((t =  getusershell()) != NULL) {
227 		if (strcmp(t, shell) == 0) {
228 			ret = 1;
229 			break;
230 		}
231 	}
232 	endusershell();
233 	return (ret);
234 }
235 
236 int
237 isvalid_dir(const char *dir)
238 {
239 	char *cwd = getcwd(NULL, 0);
240 
241 	if (dir[0] != '/' || chdir(dir) == -1) {
242 		return (0);
243 	}
244 	if (cwd != NULL) {
245 		(void) chdir(cwd);
246 	}
247 	return (1);
248 }
249