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