17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5d1419d5Nobutomo Nakano * Common Development and Distribution License (the "License").
6d1419d5Nobutomo Nakano * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate
227c478bdstevel@tonic-gate/*
23b0d023dGary Mills * Copyright (c) 2011 Gary Mills
24b0d023dGary Mills *
25d1419d5Nobutomo Nakano * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
267c478bdstevel@tonic-gate * Use is subject to license terms.
2748bbca8Daniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
30032624dbasabi/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
31032624dbasabi/*	  All Rights Reserved  	*/
32032624dbasabi
337c478bdstevel@tonic-gate#include <sys/resource.h>
347c478bdstevel@tonic-gate#include <sys/stat.h>
357c478bdstevel@tonic-gate#include <sys/types.h>
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate#include <dirent.h>
387c478bdstevel@tonic-gate#include <string.h>
397c478bdstevel@tonic-gate#include <stdlib.h>
407c478bdstevel@tonic-gate#include <fcntl.h>
417c478bdstevel@tonic-gate#include <pwd.h>
427c478bdstevel@tonic-gate#include <stdio.h>
437c478bdstevel@tonic-gate#include <ctype.h>
447c478bdstevel@tonic-gate#include <time.h>
457c478bdstevel@tonic-gate#include <signal.h>
467c478bdstevel@tonic-gate#include <errno.h>
477c478bdstevel@tonic-gate#include <limits.h>
487c478bdstevel@tonic-gate#include <ulimit.h>
497c478bdstevel@tonic-gate#include <unistd.h>
507c478bdstevel@tonic-gate#include <locale.h>
517c478bdstevel@tonic-gate#include <libintl.h>
527c478bdstevel@tonic-gate#include <tzfile.h>
537c478bdstevel@tonic-gate#include <project.h>
546a5408eRichard Lowe#include <paths.h>
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate#include "cron.h"
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate#define	TMPFILE		"_at" /* prefix for temporary files	*/
597c478bdstevel@tonic-gate/*
607c478bdstevel@tonic-gate * Mode for creating files in ATDIR.
617c478bdstevel@tonic-gate * Setuid bit on so that if an owner of a file gives that file
627c478bdstevel@tonic-gate * away to someone else, the setuid bit will no longer be set.
637c478bdstevel@tonic-gate * If this happens, atrun will not execute the file
647c478bdstevel@tonic-gate */
657c478bdstevel@tonic-gate#define	ATMODE		(S_ISUID | S_IRUSR | S_IRGRP | S_IROTH)
667c478bdstevel@tonic-gate#define	ROOT		0	/* user-id of super-user */
677c478bdstevel@tonic-gate#define	MAXTRYS		100	/* max trys to create at job file */
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate#define	BADTIME		"bad time specification"
707c478bdstevel@tonic-gate#define	BADQUEUE	"queue name must be a single character a-z"
717c478bdstevel@tonic-gate#define	NOTCQUEUE	"queue c is reserved for cron entries"
727c478bdstevel@tonic-gate#define	BADSHELL	"because your login shell isn't /usr/bin/sh,"\
737c478bdstevel@tonic-gate			"you can't use at"
747c478bdstevel@tonic-gate#define	WARNSHELL	"commands will be executed using %s\n"
757c478bdstevel@tonic-gate#define	CANTCD		"can't change directory to the at directory"
767c478bdstevel@tonic-gate#define	CANTCHOWN	"can't change the owner of your job to you"
77b0d023dGary Mills#define	CANTCHUID	"can't change user identifier"
787c478bdstevel@tonic-gate#define	CANTCREATE	"can't create a job for you"
797c478bdstevel@tonic-gate#define	INVALIDUSER	"you are not a valid user (no entry in /etc/passwd)"
807c478bdstevel@tonic-gate#define	NOOPENDIR	"can't open the at directory"
817c478bdstevel@tonic-gate#define	NOTALLOWED	"you are not authorized to use at.  Sorry."
827c478bdstevel@tonic-gate#define	USAGE\
837c478bdstevel@tonic-gate	"usage: at [-c|-k|-s] [-m] [-f file] [-p project] [-q queuename] "\
847c478bdstevel@tonic-gate	    "-t time\n"\
857c478bdstevel@tonic-gate	"       at [-c|-k|-s] [-m] [-f file] [-p project] [-q queuename] "\
867c478bdstevel@tonic-gate	    "timespec\n"\
877c478bdstevel@tonic-gate	"       at -l [-p project] [-q queuename] [at_job_id...]\n"\
887c478bdstevel@tonic-gate	"       at -r at_job_id ...\n"
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gate#define	FORMAT		"%a %b %e %H:%M:%S %Y"
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gatestatic int leap(int);
937c478bdstevel@tonic-gatestatic int atoi_for2(char *);
947c478bdstevel@tonic-gatestatic int check_queue(char *, int);
957c478bdstevel@tonic-gatestatic int list_jobs(int, char **, int, int);
967c478bdstevel@tonic-gatestatic int remove_jobs(int, char **, char *);
977c478bdstevel@tonic-gatestatic void usage(void);
987c478bdstevel@tonic-gatestatic void catch(int);
997c478bdstevel@tonic-gatestatic void copy(char *, FILE *, int);
1007c478bdstevel@tonic-gatestatic void atime(struct tm *, struct tm *);
1017c478bdstevel@tonic-gatestatic int not_this_project(char *);
1027c478bdstevel@tonic-gatestatic char *mkjobname(time_t);
1037c478bdstevel@tonic-gatestatic time_t parse_time(char *);
1047c478bdstevel@tonic-gatestatic time_t gtime(struct tm *);
105a725189Mohamed A. Khalfellastatic void escapestr(const char *);
106032624dbasabivoid atabort(char *)__NORETURN;
1077c478bdstevel@tonic-gatevoid yyerror(void);
1087c478bdstevel@tonic-gateextern int yyparse(void);
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gateextern void	audit_at_delete(char *, char *, int);
1117c478bdstevel@tonic-gateextern int	audit_at_create(char *, int);
1127c478bdstevel@tonic-gateextern int	audit_cron_is_anc_name(char *);
1137c478bdstevel@tonic-gateextern int	audit_cron_delete_anc_file(char *, char *);
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate/*
1167c478bdstevel@tonic-gate * Error in getdate(3G)
1177c478bdstevel@tonic-gate */
1187c478bdstevel@tonic-gatestatic char 	*errlist[] = {
1197c478bdstevel@tonic-gate/* 0 */ 	"",
1207c478bdstevel@tonic-gate/* 1 */	"getdate: The DATEMSK environment variable is not set",
1217c478bdstevel@tonic-gate/* 2 */	"getdate: Error on \"open\" of the template file",
1227c478bdstevel@tonic-gate/* 3 */	"getdate: Error on \"stat\" of the template file",
1237c478bdstevel@tonic-gate/* 4 */	"getdate: The template file is not a regular file",
1247c478bdstevel@tonic-gate/* 5 */	"getdate: An error is encountered while reading the template",
1257c478bdstevel@tonic-gate/* 6 */	"getdate: Malloc(3C) failed",
1267c478bdstevel@tonic-gate/* 7 */	"getdate: There is no line in the template that matches the input",
1277c478bdstevel@tonic-gate/* 8 */	"getdate: Invalid input specification"
1287c478bdstevel@tonic-gate};
1297c478bdstevel@tonic-gate
1307c478bdstevel@tonic-gateint		gmtflag = 0;
1317c478bdstevel@tonic-gateint		mday[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1327c478bdstevel@tonic-gateuid_t		user;
1337c478bdstevel@tonic-gatestruct	tm	*tp, at, rt;
1347c478bdstevel@tonic-gatestatic int	cshflag		= 0;
1357c478bdstevel@tonic-gatestatic int	kshflag		= 0;
1367c478bdstevel@tonic-gatestatic int	shflag		= 0;
1377c478bdstevel@tonic-gatestatic int	mflag		= 0;
1387c478bdstevel@tonic-gatestatic int	pflag		= 0;
1397c478bdstevel@tonic-gatestatic char	*Shell;
1407c478bdstevel@tonic-gatestatic char	*tfname;
1417c478bdstevel@tonic-gatestatic char	pname[80];
1427c478bdstevel@tonic-gatestatic char	pname1[80];
1437c478bdstevel@tonic-gatestatic short	jobtype = ATEVENT;	/* set to 1 if batch job */
1447c478bdstevel@tonic-gateextern char	*argp;
1457c478bdstevel@tonic-gateextern int	per_errno;
1467c478bdstevel@tonic-gatestatic projid_t	project;
1477c478bdstevel@tonic-gate
148032624dbasabiint
149032624dbasabimain(int argc, char **argv)
1507c478bdstevel@tonic-gate{
1517c478bdstevel@tonic-gate	FILE		*inputfile;
1527c478bdstevel@tonic-gate	int		i, fd;
1537c478bdstevel@tonic-gate	int		try = 0;
1547c478bdstevel@tonic-gate	int		fflag = 0;
1557c478bdstevel@tonic-gate	int		lflag = 0;
1567c478bdstevel@tonic-gate	int		qflag = 0;
1577c478bdstevel@tonic-gate	int		rflag = 0;
1587c478bdstevel@tonic-gate	int		tflag = 0;
1597c478bdstevel@tonic-gate	int		c;
1607c478bdstevel@tonic-gate	int		tflen;
1617c478bdstevel@tonic-gate	char		*file;
1627c478bdstevel@tonic-gate	char		*login;
1637c478bdstevel@tonic-gate	char		*job;
1647c478bdstevel@tonic-gate	char		*jobfile = NULL; /* file containing job to be run */
1657c478bdstevel@tonic-gate	char		argpbuf[LINE_MAX], timebuf[80];
1667c478bdstevel@tonic-gate	time_t		now;
1677c478bdstevel@tonic-gate	time_t		when = 0;
1687c478bdstevel@tonic-gate	struct	tm	*ct;
1697c478bdstevel@tonic-gate	char		*proj;
1707c478bdstevel@tonic-gate	struct project	prj, *pprj;
1717c478bdstevel@tonic-gate	char		mybuf[PROJECT_BUFSZ];
1727c478bdstevel@tonic-gate	char		ipbuf[PROJECT_BUFSZ];
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gate	(void) setlocale(LC_ALL, "");
1757c478bdstevel@tonic-gate#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1767c478bdstevel@tonic-gate#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1777c478bdstevel@tonic-gate#endif
1787c478bdstevel@tonic-gate	(void) textdomain(TEXT_DOMAIN);
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gate	user = getuid();
1817c478bdstevel@tonic-gate	login = getuser(user);
1827c478bdstevel@tonic-gate	if (login == NULL) {
1837c478bdstevel@tonic-gate		if (per_errno == 2)
1847c478bdstevel@tonic-gate			atabort(BADSHELL);
1857c478bdstevel@tonic-gate		else
1867c478bdstevel@tonic-gate			atabort(INVALIDUSER);
1877c478bdstevel@tonic-gate	}
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	if (!allowed(login, ATALLOW, ATDENY))
1907c478bdstevel@tonic-gate		atabort(NOTALLOWED);
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate	while ((c = getopt(argc, argv, "cklmsrf:p:q:t:")) != EOF)
1937c478bdstevel@tonic-gate		switch (c) {
1947c478bdstevel@tonic-gate			case 'c':
1957c478bdstevel@tonic-gate				cshflag++;
1967c478bdstevel@tonic-gate				break;
1977c478bdstevel@tonic-gate			case 'f':
1987c478bdstevel@tonic-gate				fflag++;
1997c478bdstevel@tonic-gate				jobfile = optarg;
2007c478bdstevel@tonic-gate				break;
2017c478bdstevel@tonic-gate			case 'k':
2027c478bdstevel@tonic-gate				kshflag++;
2037c478bdstevel@tonic-gate				break;
2047c478bdstevel@tonic-gate			case 'l':
2057c478bdstevel@tonic-gate				lflag++;
2067c478bdstevel@tonic-gate				break;
2077c478bdstevel@tonic-gate			case 'm':
2087c478bdstevel@tonic-gate				mflag++;
2097c478bdstevel@tonic-gate				break;
2107c478bdstevel@tonic-gate			case 'p':
2117c478bdstevel@tonic-gate				proj = optarg;
2127c478bdstevel@tonic-gate				pprj = &prj;
2137c478bdstevel@tonic-gate				if ((pprj = getprojbyname(proj, pprj,
2147c478bdstevel@tonic-gate				    (void *)&mybuf, sizeof (mybuf))) != NULL) {
2157c478bdstevel@tonic-gate					project = pprj->pj_projid;
2167c478bdstevel@tonic-gate					if (inproj(login, pprj->pj_name,
2177c478bdstevel@tonic-gate					    (void *)&ipbuf, sizeof (ipbuf)))
2187c478bdstevel@tonic-gate						pflag++;
2197c478bdstevel@tonic-gate					else {
2207c478bdstevel@tonic-gate						(void) fprintf(stderr,
2217c478bdstevel@tonic-gate						    gettext("at: user %s is "
222d1419d5Nobutomo Nakano						    "not a member of "
223d1419d5Nobutomo Nakano						    "project %s (%d)\n"),
2247c478bdstevel@tonic-gate						    login, pprj->pj_name,
2257c478bdstevel@tonic-gate						    project);
2267c478bdstevel@tonic-gate						exit(2);
2277c478bdstevel@tonic-gate					}
2287c478bdstevel@tonic-gate					break;
2297c478bdstevel@tonic-gate				}
2307c478bdstevel@tonic-gate				pprj = &prj;
2317c478bdstevel@tonic-gate				if (isdigit(proj[0]) &&
2327c478bdstevel@tonic-gate				    (pprj = getprojbyid(atoi(proj), pprj,
2337c478bdstevel@tonic-gate				    (void *)&mybuf, sizeof (mybuf))) != NULL) {
2347c478bdstevel@tonic-gate					project = pprj->pj_projid;
2357c478bdstevel@tonic-gate					if (inproj(login, pprj->pj_name,
2367c478bdstevel@tonic-gate					    (void *)&ipbuf, sizeof (ipbuf)))
2377c478bdstevel@tonic-gate						pflag++;
2387c478bdstevel@tonic-gate					else {
2397c478bdstevel@tonic-gate						(void) fprintf(stderr,
2407c478bdstevel@tonic-gate						    gettext("at: user %s is "
241d1419d5Nobutomo Nakano						    "not a member of "
242d1419d5Nobutomo Nakano						    "project %s (%d)\n"),
2437c478bdstevel@tonic-gate						    login, pprj->pj_name,
2447c478bdstevel@tonic-gate						    project);
2457c478bdstevel@tonic-gate						exit(2);
2467c478bdstevel@tonic-gate					}
2477c478bdstevel@tonic-gate					break;
2487c478bdstevel@tonic-gate				}
2497c478bdstevel@tonic-gate				(void) fprintf(stderr, gettext("at: project "
2507c478bdstevel@tonic-gate				    "%s not found.\n"), proj);
2517c478bdstevel@tonic-gate				exit(2);
2527c478bdstevel@tonic-gate				break;
2537c478bdstevel@tonic-gate			case 'q':
2547c478bdstevel@tonic-gate				qflag++;
2557c478bdstevel@tonic-gate				if (optarg[1] != '\0')
2567c478bdstevel@tonic-gate					atabort(BADQUEUE);
2577c478bdstevel@tonic-gate				jobtype = *optarg - 'a';
2587c478bdstevel@tonic-gate				if ((jobtype < 0) || (jobtype > 25))
2597c478bdstevel@tonic-gate					atabort(BADQUEUE);
2607c478bdstevel@tonic-gate				if (jobtype == 2)
2617c478bdstevel@tonic-gate					atabort(NOTCQUEUE);
2627c478bdstevel@tonic-gate				break;
2637c478bdstevel@tonic-gate			case 'r':
2647c478bdstevel@tonic-gate				rflag++;
2657c478bdstevel@tonic-gate				break;
2667c478bdstevel@tonic-gate			case 's':
2677c478bdstevel@tonic-gate				shflag++;
2687c478bdstevel@tonic-gate				break;
2697c478bdstevel@tonic-gate			case 't':
2707c478bdstevel@tonic-gate				tflag++;
2717c478bdstevel@tonic-gate				when = parse_time(optarg);
2727c478bdstevel@tonic-gate				break;
2737c478bdstevel@tonic-gate			default:
2747c478bdstevel@tonic-gate				usage();
2757c478bdstevel@tonic-gate		}
2767c478bdstevel@tonic-gate
2777c478bdstevel@tonic-gate	argc -= optind;
2787c478bdstevel@tonic-gate	argv += optind;
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate	if (lflag + rflag > 1)
2817c478bdstevel@tonic-gate		usage();
2827c478bdstevel@tonic-gate
2837c478bdstevel@tonic-gate	if (lflag) {
2847c478bdstevel@tonic-gate		if (cshflag || kshflag || shflag || mflag ||
285d1419d5Nobutomo Nakano		    fflag || tflag || rflag)
2867c478bdstevel@tonic-gate			usage();
2877c478bdstevel@tonic-gate		return (list_jobs(argc, argv, qflag, jobtype));
2887c478bdstevel@tonic-gate	}
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate	if (rflag) {
2917c478bdstevel@tonic-gate		if (cshflag || kshflag || shflag || mflag ||
292d1419d5Nobutomo Nakano		    fflag || tflag || qflag)
2937c478bdstevel@tonic-gate			usage();
2947c478bdstevel@tonic-gate		return (remove_jobs(argc, argv, login));
2957c478bdstevel@tonic-gate	}
2967c478bdstevel@tonic-gate
2977c478bdstevel@tonic-gate	if ((argc + tflag == 0) && (jobtype != BATCHEVENT))
2987c478bdstevel@tonic-gate		usage();
2997c478bdstevel@tonic-gate
3007c478bdstevel@tonic-gate	if (cshflag + kshflag + shflag > 1)
3017c478bdstevel@tonic-gate		atabort("ambiguous shell request");
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate	time(&now);
3047c478bdstevel@tonic-gate
3057c478bdstevel@tonic-gate	if (jobtype == BATCHEVENT)
3067c478bdstevel@tonic-gate		when = now;
3077c478bdstevel@tonic-gate
3087c478bdstevel@tonic-gate	if (when == 0) { /* figure out what time to run the job */
3097c478bdstevel@tonic-gate		int	argplen = sizeof (argpbuf) - 1;
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate		argpbuf[0] = '\0';
3127c478bdstevel@tonic-gate		argp = argpbuf;
3137c478bdstevel@tonic-gate		i = 0;
3147c478bdstevel@tonic-gate		while (i < argc) {
3157c478bdstevel@tonic-gate			/* guard against buffer overflow */
3167c478bdstevel@tonic-gate			argplen -= strlen(argv[i]) + 1;
3177c478bdstevel@tonic-gate			if (argplen < 0)
3187c478bdstevel@tonic-gate				atabort(BADTIME);
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate			strcat(argp, argv[i]);
3217c478bdstevel@tonic-gate			strcat(argp, " ");
3227c478bdstevel@tonic-gate			i++;
3237c478bdstevel@tonic-gate		}
3247c478bdstevel@tonic-gate		if ((file = getenv("DATEMSK")) == 0 || file[0] == '\0') {
3257c478bdstevel@tonic-gate			tp = localtime(&now);
3267c478bdstevel@tonic-gate			/*
3277c478bdstevel@tonic-gate			 * Fix for 1047182 - we have to let yyparse
3287c478bdstevel@tonic-gate			 * check bounds on mday[] first, then fixup
3297c478bdstevel@tonic-gate			 * the leap year case.
3307c478bdstevel@tonic-gate			 */
3317c478bdstevel@tonic-gate			yyparse();
3327c478bdstevel@tonic-gate
3337c478bdstevel@tonic-gate			mday[1] = 28 + leap(at.tm_year);
3347c478bdstevel@tonic-gate
3357c478bdstevel@tonic-gate			if (at.tm_mday > mday[at.tm_mon])
3367c478bdstevel@tonic-gate				atabort("bad date");
3377c478bdstevel@tonic-gate
3387c478bdstevel@tonic-gate			atime(&at, &rt);
3397c478bdstevel@tonic-gate			when = gtime(&at);
3407c478bdstevel@tonic-gate			if (!gmtflag) {
3417c478bdstevel@tonic-gate				when += timezone;
3427c478bdstevel@tonic-gate				if (localtime(&when)->tm_isdst)
3437c478bdstevel@tonic-gate					when -= (timezone-altzone);
3447c478bdstevel@tonic-gate			}
3457c478bdstevel@tonic-gate		} else {	/*   DATEMSK is set  */
3467c478bdstevel@tonic-gate			if ((ct = getdate(argpbuf)) == NULL)
3477c478bdstevel@tonic-gate				atabort(errlist[getdate_err]);
3487c478bdstevel@tonic-gate			else
3497c478bdstevel@tonic-gate				when = mktime(ct);
3507c478bdstevel@tonic-gate		}
3517c478bdstevel@tonic-gate	}
3527c478bdstevel@tonic-gate
3537c478bdstevel@tonic-gate	if (when < now)	/* time has already past */
3547c478bdstevel@tonic-gate		atabort("too late");
3557c478bdstevel@tonic-gate
3567c478bdstevel@tonic-gate	tflen = strlen(ATDIR) + 1 + strlen(TMPFILE) +
3577c478bdstevel@tonic-gate	    10 + 1; /* 10 for an INT_MAX pid */
3587c478bdstevel@tonic-gate	tfname = xmalloc(tflen);
3597c478bdstevel@tonic-gate	snprintf(tfname, tflen, "%s/%s%d", ATDIR, TMPFILE, getpid());
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate	/* catch INT, HUP, TERM and QUIT signals */
3627c478bdstevel@tonic-gate	if (signal(SIGINT, catch) == SIG_IGN)
3637c478bdstevel@tonic-gate		signal(SIGINT, SIG_IGN);
3647c478bdstevel@tonic-gate	if (signal(SIGHUP, catch) == SIG_IGN)
3657c478bdstevel@tonic-gate		signal(SIGHUP, SIG_IGN);
3667c478bdstevel@tonic-gate	if (signal(SIGQUIT, catch) == SIG_IGN)
3677c478bdstevel@tonic-gate		signal(SIGQUIT, SIG_IGN);
3687c478bdstevel@tonic-gate	if (signal(SIGTERM, catch) == SIG_IGN)
3697c478bdstevel@tonic-gate		signal(SIGTERM, SIG_IGN);
3707c478bdstevel@tonic-gate	if ((fd = open(tfname, O_CREAT|O_EXCL|O_WRONLY, ATMODE)) < 0)
3717c478bdstevel@tonic-gate		atabort(CANTCREATE);
3727c478bdstevel@tonic-gate	if (chown(tfname, user, getgid()) == -1) {
3737c478bdstevel@tonic-gate		unlink(tfname);
3747c478bdstevel@tonic-gate		atabort(CANTCHOWN);
3757c478bdstevel@tonic-gate	}
3767c478bdstevel@tonic-gate	close(1);
3777c478bdstevel@tonic-gate	dup(fd);
3787c478bdstevel@tonic-gate	close(fd);
3797c478bdstevel@tonic-gate	sprintf(pname, "%s", PROTO);
3807c478bdstevel@tonic-gate	sprintf(pname1, "%s.%c", PROTO, 'a'+jobtype);
3817c478bdstevel@tonic-gate
3827c478bdstevel@tonic-gate	/*
3837c478bdstevel@tonic-gate	 * Open the input file with the user's permissions.
3847c478bdstevel@tonic-gate	 */
3857c478bdstevel@tonic-gate	if (jobfile != NULL) {
3867c478bdstevel@tonic-gate		if ((seteuid(user) < 0) ||
3877c478bdstevel@tonic-gate		    (inputfile = fopen(jobfile, "r")) == NULL) {
3887c478bdstevel@tonic-gate			unlink(tfname);
3897c478bdstevel@tonic-gate			fprintf(stderr, "at: %s: %s\n", jobfile, errmsg(errno));
3907c478bdstevel@tonic-gate			exit(1);
3917c478bdstevel@tonic-gate		}
3927c478bdstevel@tonic-gate		else
3937c478bdstevel@tonic-gate			seteuid(0);
3947c478bdstevel@tonic-gate	} else
3957c478bdstevel@tonic-gate		inputfile = stdin;
3967c478bdstevel@tonic-gate
3977c478bdstevel@tonic-gate	copy(jobfile, inputfile, when);
3987c478bdstevel@tonic-gate	while (rename(tfname, job = mkjobname(when)) == -1) {
3997c478bdstevel@tonic-gate		sleep(1);
4007c478bdstevel@tonic-gate		if (++try > MAXTRYS / 10) {
4017c478bdstevel@tonic-gate			unlink(tfname);
4027c478bdstevel@tonic-gate			atabort(CANTCREATE);
4037c478bdstevel@tonic-gate		}
4047c478bdstevel@tonic-gate	}
4057c478bdstevel@tonic-gate	unlink(tfname);
4067c478bdstevel@tonic-gate	if (audit_at_create(job, 0))
4077c478bdstevel@tonic-gate		atabort(CANTCREATE);
4087c478bdstevel@tonic-gate
4097c478bdstevel@tonic-gate	cron_sendmsg(ADD, login, strrchr(job, '/')+1, AT);
4107c478bdstevel@tonic-gate	if (per_errno == 2)
4117c478bdstevel@tonic-gate		fprintf(stderr, gettext(WARNSHELL), Shell);
4127c478bdstevel@tonic-gate	cftime(timebuf, FORMAT, &when);
4137c478bdstevel@tonic-gate	fprintf(stderr, gettext("job %s at %s\n"),
4147c478bdstevel@tonic-gate	    strrchr(job, '/')+1, timebuf);
4157c478bdstevel@tonic-gate	if (when - MINUTE < HOUR)
4167c478bdstevel@tonic-gate		fprintf(stderr, gettext(
4177c478bdstevel@tonic-gate		    "at: this job may not be executed at the proper time.\n"));
4187c478bdstevel@tonic-gate	return (0);
4197c478bdstevel@tonic-gate}
4207c478bdstevel@tonic-gate
4217c478bdstevel@tonic-gate
4227c478bdstevel@tonic-gatestatic char *
4234159a98Toomas Soomemkjobname(time_t t)
4247c478bdstevel@tonic-gate{
4257c478bdstevel@tonic-gate	int i, fd;
4267c478bdstevel@tonic-gate	char *name;
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gate	name = xmalloc(200);
4297c478bdstevel@tonic-gate	for (i = 0; i < MAXTRYS; i++) {
4307c478bdstevel@tonic-gate		sprintf(name, "%s/%ld.%c", ATDIR, t, 'a'+jobtype);
4317c478bdstevel@tonic-gate		/* fix for 1099183, 1116833 - create file here, avoid race */
4327c478bdstevel@tonic-gate		if ((fd = open(name, O_CREAT | O_EXCL, ATMODE)) > 0) {
4337c478bdstevel@tonic-gate			close(fd);
4347c478bdstevel@tonic-gate			return (name);
4357c478bdstevel@tonic-gate		}
4367c478bdstevel@tonic-gate		t += 1;
4377c478bdstevel@tonic-gate	}
4387c478bdstevel@tonic-gate	atabort("queue full");
439032624dbasabi	/* NOTREACHED */
4407c478bdstevel@tonic-gate}
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gate
4437c478bdstevel@tonic-gatestatic void
4447c478bdstevel@tonic-gatecatch(int x)
4457c478bdstevel@tonic-gate{
4467c478bdstevel@tonic-gate	unlink(tfname);
4477c478bdstevel@tonic-gate	exit(1);
4487c478bdstevel@tonic-gate}
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate
4517c478bdstevel@tonic-gatevoid
4524159a98Toomas Soomeatabort(char *msg)
4537c478bdstevel@tonic-gate{
4547c478bdstevel@tonic-gate	fprintf(stderr, "at: %s\n", gettext(msg));
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate	exit(1);
4577c478bdstevel@tonic-gate}
4587c478bdstevel@tonic-gate
459032624dbasabiint
4607c478bdstevel@tonic-gateyywrap(void)
4617c478bdstevel@tonic-gate{
4627c478bdstevel@tonic-gate	return (1);
4637c478bdstevel@tonic-gate}
4647c478bdstevel@tonic-gate
4657c478bdstevel@tonic-gatevoid
4667c478bdstevel@tonic-gateyyerror(void)
4677c478bdstevel@tonic-gate{
4687c478bdstevel@tonic-gate	atabort(BADTIME);
4697c478bdstevel@tonic-gate}
4707c478bdstevel@tonic-gate
4717c478bdstevel@tonic-gate/*
4727c478bdstevel@tonic-gate * add time structures logically
4737c478bdstevel@tonic-gate */
4747c478bdstevel@tonic-gatestatic void
4757c478bdstevel@tonic-gateatime(struct tm *a, struct tm *b)
4767c478bdstevel@tonic-gate{
4777c478bdstevel@tonic-gate	if ((a->tm_sec += b->tm_sec) >= 60) {
4787c478bdstevel@tonic-gate		b->tm_min += a->tm_sec / 60;
4797c478bdstevel@tonic-gate		a->tm_sec %= 60;
4807c478bdstevel@tonic-gate	}
4817c478bdstevel@tonic-gate	if ((a->tm_min += b->tm_min) >= 60) {
4827c478bdstevel@tonic-gate		b->tm_hour += a->tm_min / 60;
4837c478bdstevel@tonic-gate		a->tm_min %= 60;
4847c478bdstevel@tonic-gate	}
4857c478bdstevel@tonic-gate	if ((a->tm_hour += b->tm_hour) >= 24) {
4867c478bdstevel@tonic-gate		b->tm_mday += a->tm_hour / 24;
4877c478bdstevel@tonic-gate		a->tm_hour %= 24;
4887c478bdstevel@tonic-gate	}
4897c478bdstevel@tonic-gate	a->tm_year += b->tm_year;
4907c478bdstevel@tonic-gate	if ((a->tm_mon += b->tm_mon) >= 12) {
4917c478bdstevel@tonic-gate		a->tm_year += a->tm_mon / 12;
4927c478bdstevel@tonic-gate		a->tm_mon %= 12;
4937c478bdstevel@tonic-gate	}
4947c478bdstevel@tonic-gate	a->tm_mday += b->tm_mday;
4957c478bdstevel@tonic-gate	mday[1] = 28 + leap(a->tm_year);
4967c478bdstevel@tonic-gate	while (a->tm_mday > mday[a->tm_mon]) {
4977c478bdstevel@tonic-gate		a->tm_mday -= mday[a->tm_mon++];
4987c478bdstevel@tonic-gate		if (a->tm_mon > 11) {
4997c478bdstevel@tonic-gate			a->tm_mon = 0;
5007c478bdstevel@tonic-gate			mday[1] = 28 + leap(++a->tm_year);
5017c478bdstevel@tonic-gate		}
5027c478bdstevel@tonic-gate	}
5037c478bdstevel@tonic-gate
5047c478bdstevel@tonic-gate}
5057c478bdstevel@tonic-gate
5067c478bdstevel@tonic-gatestatic int
5077c478bdstevel@tonic-gateleap(int year)
5087c478bdstevel@tonic-gate{
5097c478bdstevel@tonic-gate	return (isleap(year + TM_YEAR_BASE));
5107c478bdstevel@tonic-gate}
5117c478bdstevel@tonic-gate
5127c478bdstevel@tonic-gate/*
5137c478bdstevel@tonic-gate * return time from time structure
5147c478bdstevel@tonic-gate */
5157c478bdstevel@tonic-gatestatic time_t
5164159a98Toomas Soomegtime(struct tm *tptr)
5177c478bdstevel@tonic-gate{
518032624dbasabi	int i;
5197c478bdstevel@tonic-gate	long	tv;
5207c478bdstevel@tonic-gate	int	dmsize[12] =
5217c478bdstevel@tonic-gate	    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gate
5247c478bdstevel@tonic-gate	tv = 0;
5257c478bdstevel@tonic-gate	for (i = 1970; i != tptr->tm_year+TM_YEAR_BASE; i++)
5267c478bdstevel@tonic-gate		tv += (365 + isleap(i));
5277c478bdstevel@tonic-gate		/*
5287c478bdstevel@tonic-gate		 * We call isleap since leap() adds
5297c478bdstevel@tonic-gate		 * 1900 onto any value passed
5307c478bdstevel@tonic-gate		 */
5317c478bdstevel@tonic-gate
5327c478bdstevel@tonic-gate	if (!leap(tptr->tm_year) && at.tm_mday == 29 && at.tm_mon == 1)
5337c478bdstevel@tonic-gate		atabort("bad date - not a leap year");
5347c478bdstevel@tonic-gate
5357c478bdstevel@tonic-gate	if ((leap(tptr->tm_year)) && tptr->tm_mon >= 2)
5367c478bdstevel@tonic-gate		++tv;
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate	for (i = 0; i < tptr->tm_mon; ++i)
5397c478bdstevel@tonic-gate		tv += dmsize[i];
5407c478bdstevel@tonic-gate	tv += tptr->tm_mday - 1;
5417c478bdstevel@tonic-gate	tv = 24 * tv + tptr->tm_hour;
5427c478bdstevel@tonic-gate	tv = 60 * tv + tptr->tm_min;
5437c478bdstevel@tonic-gate	tv = 60 * tv + tptr->tm_sec;
5447c478bdstevel@tonic-gate	return (tv);
5457c478bdstevel@tonic-gate}
5467c478bdstevel@tonic-gate
5477c478bdstevel@tonic-gate/*
548a725189Mohamed A. Khalfella * Escape a string to be used inside the job shell script.
549a725189Mohamed A. Khalfella */
550a725189Mohamed A. Khalfellastatic void
551a725189Mohamed A. Khalfellaescapestr(const char *str)
552a725189Mohamed A. Khalfella{
553a725189Mohamed A. Khalfella	char c;
554a725189Mohamed A. Khalfella	(void) putchar('\'');
555a725189Mohamed A. Khalfella	while ((c = *str++) != '\0') {
556a725189Mohamed A. Khalfella		if (c != '\'')
557a725189Mohamed A. Khalfella			(void) putchar(c);
558a725189Mohamed A. Khalfella		else
559a725189Mohamed A. Khalfella			(void) fputs("'\\''", stdout); /* ' -> '\'' */
560a725189Mohamed A. Khalfella	}
561a725189Mohamed A. Khalfella	(void) putchar('\'');
562a725189Mohamed A. Khalfella}
563a725189Mohamed A. Khalfella
564a725189Mohamed A. Khalfella/*
5657c478bdstevel@tonic-gate * make job file from proto + stdin
5667c478bdstevel@tonic-gate */
5677c478bdstevel@tonic-gatestatic void
5687c478bdstevel@tonic-gatecopy(char *jobfile, FILE *inputfile, int when)
5697c478bdstevel@tonic-gate{
570032624dbasabi	int c;
571032624dbasabi	FILE *pfp;
5727c478bdstevel@tonic-gate	char *shell;
5737c478bdstevel@tonic-gate	char	dirbuf[PATH_MAX + 1];
5747c478bdstevel@tonic-gate	char	line[LINE_MAX];
575032624dbasabi	char **ep;
5767c478bdstevel@tonic-gate	mode_t um;
5777c478bdstevel@tonic-gate	char *val;
5787c478bdstevel@tonic-gate	extern char **environ;
579b0d023dGary Mills	uid_t realusr, effeusr;
5807c478bdstevel@tonic-gate	int ttyinput;
5817c478bdstevel@tonic-gate	int ulimit_flag = 0;
5827c478bdstevel@tonic-gate	struct rlimit rlp;
5837c478bdstevel@tonic-gate	struct project prj, *pprj;
5847c478bdstevel@tonic-gate	char pbuf[PROJECT_BUFSZ];
5857c478bdstevel@tonic-gate	char pbuf2[PROJECT_BUFSZ];
5867c478bdstevel@tonic-gate	char *user;
5877c478bdstevel@tonic-gate
5887c478bdstevel@tonic-gate	/*
5897c478bdstevel@tonic-gate	 * Fix for 1099381:
5907c478bdstevel@tonic-gate	 * If the inputfile is from a tty, then turn on prompting, and
5917c478bdstevel@tonic-gate	 * put out a prompt now, instead of waiting for a lot of file
5927c478bdstevel@tonic-gate	 * activity to complete.
5937c478bdstevel@tonic-gate	 */
5947c478bdstevel@tonic-gate	ttyinput = isatty(fileno(inputfile));
5957c478bdstevel@tonic-gate	if (ttyinput) {
5967c478bdstevel@tonic-gate		fputs("at> ", stderr);
5977c478bdstevel@tonic-gate		fflush(stderr);
5987c478bdstevel@tonic-gate	}
5997c478bdstevel@tonic-gate
6007c478bdstevel@tonic-gate	/*
6017c478bdstevel@tonic-gate	 * Fix for 1053807:
6027c478bdstevel@tonic-gate	 * Determine what shell we should use to run the job. If the user
60348bbca8Daniel Hoffman	 * didn't explicitly request that their current shell be over-
6047c478bdstevel@tonic-gate	 * ridden (shflag or cshflag), then we use the current shell.
6057c478bdstevel@tonic-gate	 */
6067c478bdstevel@tonic-gate	if (cshflag)
6077c478bdstevel@tonic-gate		Shell = shell = "/bin/csh";
6087c478bdstevel@tonic-gate	else if (kshflag) {
6097c478bdstevel@tonic-gate		Shell = shell = "/bin/ksh";
6107c478bdstevel@tonic-gate		ulimit_flag = 1;
6117c478bdstevel@tonic-gate	} else if (shflag) {
6127c478bdstevel@tonic-gate		Shell = shell = "/bin/sh";
6137c478bdstevel@tonic-gate		ulimit_flag = 1;
6147c478bdstevel@tonic-gate	} else if (((Shell = val = getenv("SHELL")) != NULL) &&
6157c478bdstevel@tonic-gate	    (*val != '\0')) {
6167c478bdstevel@tonic-gate		shell = "$SHELL";
6177c478bdstevel@tonic-gate		if ((strstr(val, "/sh") != NULL) ||
6187c478bdstevel@tonic-gate		    (strstr(val, "/ksh") != NULL))
6197c478bdstevel@tonic-gate			ulimit_flag = 1;
6207c478bdstevel@tonic-gate	} else {
6217c478bdstevel@tonic-gate		/* SHELL is NULL or unset, therefore use default */
6226a5408eRichard Lowe		Shell = shell = _PATH_BSHELL;
6237c478bdstevel@tonic-gate		ulimit_flag = 1;
6247c478bdstevel@tonic-gate	}
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate	printf(": %s job\n", jobtype ? "batch" : "at");
6277c478bdstevel@tonic-gate	printf(": jobname: %.127s\n", (jobfile == NULL) ? "stdin" : jobfile);
6287c478bdstevel@tonic-gate	printf(": notify by mail: %s\n", (mflag) ? "yes" : "no");
6297c478bdstevel@tonic-gate
6307c478bdstevel@tonic-gate	if (pflag) {
6317c478bdstevel@tonic-gate		(void) printf(": project: %d\n", project);
6327c478bdstevel@tonic-gate	} else {
6337c478bdstevel@tonic-gate		/*
6347c478bdstevel@tonic-gate		 * Check if current user is a member of current project.
6357c478bdstevel@tonic-gate		 * This check is done here to avoid setproject() failure
6367c478bdstevel@tonic-gate		 * later when the job gets executed.  If current user does
6377c478bdstevel@tonic-gate		 * not belong to current project, user's default project
6387c478bdstevel@tonic-gate		 * will be used instead.  This is achieved by not specifying
6397c478bdstevel@tonic-gate		 * the project (": project: <project>\n") in the job file.
6407c478bdstevel@tonic-gate		 */
6417c478bdstevel@tonic-gate		if ((user = getuser(getuid())) == NULL)
6427c478bdstevel@tonic-gate			atabort(INVALIDUSER);
6437c478bdstevel@tonic-gate		project = getprojid();
6447c478bdstevel@tonic-gate		pprj = getprojbyid(project, &prj, pbuf, sizeof (pbuf));
6457c478bdstevel@tonic-gate		if (pprj != NULL) {
6467c478bdstevel@tonic-gate			if (inproj(user, pprj->pj_name, pbuf2, sizeof (pbuf2)))
6477c478bdstevel@tonic-gate				(void) printf(": project: %d\n", project);
6487c478bdstevel@tonic-gate		}
6497c478bdstevel@tonic-gate	}
6507c478bdstevel@tonic-gate
6517c478bdstevel@tonic-gate	for (ep = environ; *ep; ep++) {
6527c478bdstevel@tonic-gate		if ((val = strchr(*ep, '=')) == NULL)
6537c478bdstevel@tonic-gate			continue;
6547c478bdstevel@tonic-gate		*val++ = '\0';
655a725189Mohamed A. Khalfella		(void) printf("export %s; %s=", *ep, *ep);
656a725189Mohamed A. Khalfella		escapestr(val);
657a725189Mohamed A. Khalfella		(void) putchar('\n');
6587c478bdstevel@tonic-gate		*--val = '=';
6597c478bdstevel@tonic-gate	}
6607c478bdstevel@tonic-gate	if ((pfp = fopen(pname1, "r")) == NULL &&
6617c478bdstevel@tonic-gate	    (pfp = fopen(pname, "r")) == NULL)
6627c478bdstevel@tonic-gate		atabort("no prototype");
6637c478bdstevel@tonic-gate	/*
6647c478bdstevel@tonic-gate	 * Put in a line to run the proper shell using the rest of
6657c478bdstevel@tonic-gate	 * the file as input.  Note that 'exec'ing the shell will
6667c478bdstevel@tonic-gate	 * cause sh() to leave a /tmp/sh### file around. (1053807)
6677c478bdstevel@tonic-gate	 */
6687c478bdstevel@tonic-gate	printf("%s << '...the rest of this file is shell input'\n", shell);
6697c478bdstevel@tonic-gate
6707c478bdstevel@tonic-gate	um = umask(0);
6717c478bdstevel@tonic-gate	while ((c = getc(pfp)) != EOF) {
6727c478bdstevel@tonic-gate		if (c != '$')
6737c478bdstevel@tonic-gate			putchar(c);
6747c478bdstevel@tonic-gate		else switch (c =  getc(pfp)) {
6757c478bdstevel@tonic-gate		case EOF:
6767c478bdstevel@tonic-gate			goto out;
6777c478bdstevel@tonic-gate		case 'd':
6787c478bdstevel@tonic-gate			/*
679b0d023dGary Mills			 * Must obtain current working directory as the user
6807c478bdstevel@tonic-gate			 */
6817c478bdstevel@tonic-gate
682b0d023dGary Mills			dirbuf[0] = '\0';
683b0d023dGary Mills			realusr = getuid();
684b0d023dGary Mills			effeusr = geteuid();
685b0d023dGary Mills			/* change euid for getcwd */
686b0d023dGary Mills			if (seteuid(realusr) < 0) {
687b0d023dGary Mills				atabort(CANTCHUID);
688b0d023dGary Mills			}
689b0d023dGary Mills			if (getcwd(dirbuf, sizeof (dirbuf)) == NULL) {
690b0d023dGary Mills				atabort(
691b0d023dGary Mills				    "can't obtain current working directory");
692b0d023dGary Mills			}
693b0d023dGary Mills			/* change back afterwards */
694b0d023dGary Mills			if (seteuid(effeusr) < 0) {
695b0d023dGary Mills				atabort(CANTCHUID);
6967c478bdstevel@tonic-gate			}
697a725189Mohamed A. Khalfella			escapestr(dirbuf);
6987c478bdstevel@tonic-gate			break;
6997c478bdstevel@tonic-gate		case 'm':
7007c478bdstevel@tonic-gate			printf("%o", um);
7017c478bdstevel@tonic-gate			break;
7027c478bdstevel@tonic-gate		case '<':
7037c478bdstevel@tonic-gate			if (ulimit_flag) {
7047c478bdstevel@tonic-gate				if (getrlimit(RLIMIT_FSIZE, &rlp) == 0) {
7057c478bdstevel@tonic-gate					if (rlp.rlim_cur == RLIM_INFINITY)
7067c478bdstevel@tonic-gate						printf("ulimit unlimited\n");
7077c478bdstevel@tonic-gate					else
7087c478bdstevel@tonic-gate						printf("ulimit %lld\n",
709d1419d5Nobutomo Nakano						    rlp.rlim_cur / 512);
7107c478bdstevel@tonic-gate				}
7117c478bdstevel@tonic-gate			}
7127c478bdstevel@tonic-gate			/*
7137c478bdstevel@tonic-gate			 * fix for 1113572 - use fputs() so that a
7147c478bdstevel@tonic-gate			 * newline isn't appended to the one returned
7157c478bdstevel@tonic-gate			 * with fgets(); 1099381 - prompt for input.
7167c478bdstevel@tonic-gate			 */
7177c478bdstevel@tonic-gate			while (fgets(line, LINE_MAX, inputfile) != NULL) {
7187c478bdstevel@tonic-gate				fputs(line, stdout);
7197c478bdstevel@tonic-gate				if (ttyinput)
7207c478bdstevel@tonic-gate					fputs("at> ", stderr);
7217c478bdstevel@tonic-gate			}
7227c478bdstevel@tonic-gate			if (ttyinput) /* clean up the final output */
7237c478bdstevel@tonic-gate				fputs("<EOT>\n", stderr);
7247c478bdstevel@tonic-gate			break;
7257c478bdstevel@tonic-gate		case 't':
7267c478bdstevel@tonic-gate			printf(":%lu", when);
7277c478bdstevel@tonic-gate			break;
7287c478bdstevel@tonic-gate		default:
7297c478bdstevel@tonic-gate			putchar(c);
7307c478bdstevel@tonic-gate		}
7317c478bdstevel@tonic-gate	}
7327c478bdstevel@tonic-gateout:
7337c478bdstevel@tonic-gate	fclose(pfp);
7347c478bdstevel@tonic-gate	fflush(NULL);
7357c478bdstevel@tonic-gate}
7367c478bdstevel@tonic-gate
7374159a98Toomas Soome/* remove jobs that are specified */
7387c478bdstevel@tonic-gatestatic int
7397c478bdstevel@tonic-gateremove_jobs(int argc, char **argv, char *login)
7407c478bdstevel@tonic-gate{
7417c478bdstevel@tonic-gate	int		i, r;
7427c478bdstevel@tonic-gate	int		error = 0;
7437c478bdstevel@tonic-gate	struct stat	buf;
7447c478bdstevel@tonic-gate	struct passwd *pw;
7457c478bdstevel@tonic-gate
7467c478bdstevel@tonic-gate	pw = getpwuid(user);
7477c478bdstevel@tonic-gate	if (pw == NULL) {
7487c478bdstevel@tonic-gate		atabort("Invalid user.\n");
7497c478bdstevel@tonic-gate	}
7507c478bdstevel@tonic-gate
7517c478bdstevel@tonic-gate	if (argc == 0)
7527c478bdstevel@tonic-gate		usage();
7537c478bdstevel@tonic-gate	if (chdir(ATDIR) == -1)
7547c478bdstevel@tonic-gate		atabort(CANTCD);
7557c478bdstevel@tonic-gate	for (i = 0; i < argc; i++)
7567c478bdstevel@tonic-gate		if (strchr(argv[i], '/') != NULL) {
7577c478bdstevel@tonic-gate			fprintf(stderr, "at: %s: not a valid job-id\n",
7584159a98Toomas Soome			    argv[i]);
7597c478bdstevel@tonic-gate		} else if (stat(argv[i], &buf)) {
7607c478bdstevel@tonic-gate			fprintf(stderr, "at: %s: ", argv[i]);
7617c478bdstevel@tonic-gate			perror("");
7627c478bdstevel@tonic-gate		} else if ((user != buf.st_uid) &&
763d1419d5Nobutomo Nakano		    (!cron_admin(pw->pw_name))) {
7647c478bdstevel@tonic-gate			fprintf(stderr, "at: you don't own %s\n",
7657c478bdstevel@tonic-gate			    argv[i]);
7667c478bdstevel@tonic-gate			error = 1;
7677c478bdstevel@tonic-gate		} else {
768d1419d5Nobutomo Nakano			if (cron_admin(pw->pw_name)) {
7697c478bdstevel@tonic-gate				login = getuser((uid_t)buf.st_uid);
7707c478bdstevel@tonic-gate				if (login == NULL) {
7717c478bdstevel@tonic-gate					if (per_errno == 2)
7727c478bdstevel@tonic-gate						atabort(BADSHELL);
7737c478bdstevel@tonic-gate					else
7747c478bdstevel@tonic-gate						atabort(INVALIDUSER);
7757c478bdstevel@tonic-gate					}
7767c478bdstevel@tonic-gate			}
7777c478bdstevel@tonic-gate			cron_sendmsg(DELETE, login, argv[i], AT);
7787c478bdstevel@tonic-gate			r = unlink(argv[i]);
7797c478bdstevel@tonic-gate			audit_at_delete(argv[i], ATDIR, r);
7807c478bdstevel@tonic-gate		}
7817c478bdstevel@tonic-gate	return (error);
7827c478bdstevel@tonic-gate}
7837c478bdstevel@tonic-gate
7847c478bdstevel@tonic-gate
7857c478bdstevel@tonic-gate
7867c478bdstevel@tonic-gatestatic int
7877c478bdstevel@tonic-gatelist_jobs(int argc, char **argv, int qflag, int queue)
7887c478bdstevel@tonic-gate{
7897c478bdstevel@tonic-gate	DIR		*dir;
7907c478bdstevel@tonic-gate	int		i;
7917c478bdstevel@tonic-gate	int		error = 0;
7927c478bdstevel@tonic-gate	char		*patdir, *atdir, *ptr;
7937c478bdstevel@tonic-gate	char		timebuf[80];
7947c478bdstevel@tonic-gate	time_t		t;
7957c478bdstevel@tonic-gate	struct stat	buf, st1, st2;
7967c478bdstevel@tonic-gate	struct dirent	*dentry;
7977c478bdstevel@tonic-gate	struct passwd	*pw;
7987c478bdstevel@tonic-gate	unsigned int	atdirlen;
7997c478bdstevel@tonic-gate	int r;
8007c478bdstevel@tonic-gate	struct passwd	*pwd, pwds;
8017c478bdstevel@tonic-gate	char buf_pwd[1024];
8027c478bdstevel@tonic-gate	char job_file[PATH_MAX];
8037c478bdstevel@tonic-gate
8047c478bdstevel@tonic-gate	pwd = getpwuid_r(user, &pwds, buf_pwd, sizeof (buf_pwd));
8057c478bdstevel@tonic-gate	if (pwd == NULL) {
8067c478bdstevel@tonic-gate		atabort("Invalid user.\n");
8077c478bdstevel@tonic-gate	}
8087c478bdstevel@tonic-gate
8097c478bdstevel@tonic-gate	/* list jobs for user */
8107c478bdstevel@tonic-gate	if (chdir(ATDIR) == -1)
8117c478bdstevel@tonic-gate		atabort(CANTCD);
8127c478bdstevel@tonic-gate
8137c478bdstevel@tonic-gate	atdirlen = strlen(ATDIR);
8147c478bdstevel@tonic-gate	atdir = xmalloc(atdirlen + 1);
8157c478bdstevel@tonic-gate	strcpy(atdir, ATDIR);
8167c478bdstevel@tonic-gate	patdir = strrchr(atdir, '/');
8177c478bdstevel@tonic-gate	*patdir = '\0';
8187c478bdstevel@tonic-gate	if (argc == 0) {
8197c478bdstevel@tonic-gate		/* list all jobs for a user */
8207c478bdstevel@tonic-gate		if (stat(ATDIR, &st1) != 0 || stat(atdir, &st2) != 0)
8217c478bdstevel@tonic-gate			atabort("Can not get status of spooling"
8227c478bdstevel@tonic-gate			    "directory for at");
8237c478bdstevel@tonic-gate		if ((dir = opendir(ATDIR)) == NULL)
8247c478bdstevel@tonic-gate			atabort(NOOPENDIR);
8257c478bdstevel@tonic-gate		while (1) {
8267c478bdstevel@tonic-gate			if ((dentry = readdir(dir)) == NULL)
8277c478bdstevel@tonic-gate				break;
8287c478bdstevel@tonic-gate			if ((dentry->d_ino == st1.st_ino) ||
8297c478bdstevel@tonic-gate			    (dentry->d_ino == st2.st_ino))
8307c478bdstevel@tonic-gate				continue;
8317c478bdstevel@tonic-gate			if ((r = audit_cron_is_anc_name(dentry->d_name)) == 1)
8327c478bdstevel@tonic-gate				continue;
8337c478bdstevel@tonic-gate			if (stat(dentry->d_name, &buf)) {
8347c478bdstevel@tonic-gate				unlink(dentry->d_name);
8357c478bdstevel@tonic-gate				audit_cron_delete_anc_file(dentry->d_name,
836d1419d5Nobutomo Nakano				    NULL);
8377c478bdstevel@tonic-gate				continue;
8387c478bdstevel@tonic-gate			}
839d1419d5Nobutomo Nakano			if ((!cron_admin(pwd->pw_name)) &&
8407c478bdstevel@tonic-gate			    (buf.st_uid != user))
8417c478bdstevel@tonic-gate				continue;
8427c478bdstevel@tonic-gate			ptr = dentry->d_name;
8437c478bdstevel@tonic-gate			if (((t = num(&ptr)) == 0) || (*ptr != '.'))
8447c478bdstevel@tonic-gate				continue;
8457c478bdstevel@tonic-gate			strcpy(job_file, patdir);
8467c478bdstevel@tonic-gate			strcat(job_file, dentry->d_name);
8477c478bdstevel@tonic-gate			if (pflag && not_this_project(job_file))
8487c478bdstevel@tonic-gate				continue;
8497c478bdstevel@tonic-gate			ascftime(timebuf, FORMAT, localtime(&t));
850d1419d5Nobutomo Nakano			if ((cron_admin(pwd->pw_name)) &&
8517c478bdstevel@tonic-gate			    ((pw = getpwuid(buf.st_uid)) != NULL)) {
8527c478bdstevel@tonic-gate				if (!qflag || (qflag &&
8537c478bdstevel@tonic-gate				    check_queue(ptr, queue)))
8547c478bdstevel@tonic-gate					printf("user = %s\t%s\t%s\n",
8557c478bdstevel@tonic-gate					    pw->pw_name, dentry->d_name,
8567c478bdstevel@tonic-gate					    timebuf);
8577c478bdstevel@tonic-gate			} else
8587c478bdstevel@tonic-gate				if (!qflag || (qflag &&
8597c478bdstevel@tonic-gate				    check_queue(ptr, queue)))
8607c478bdstevel@tonic-gate					printf("%s\t%s\n",
8617c478bdstevel@tonic-gate					    dentry->d_name, timebuf);
8627c478bdstevel@tonic-gate		}
8637c478bdstevel@tonic-gate		(void) closedir(dir);
8647c478bdstevel@tonic-gate	} else	/* list particular jobs for user */
8657c478bdstevel@tonic-gate		for (i = 0; i < argc; i++) {
8667c478bdstevel@tonic-gate			ptr = argv[i];
8677c478bdstevel@tonic-gate			strlcpy(job_file, patdir, PATH_MAX);
8687c478bdstevel@tonic-gate			strlcat(job_file, ptr, PATH_MAX);
8697c478bdstevel@tonic-gate			if (((t = num(&ptr)) == 0) || (*ptr != '.')) {
8707c478bdstevel@tonic-gate				fprintf(stderr, gettext(
8717c478bdstevel@tonic-gate				    "at: invalid job name %s\n"), argv[i]);
8727c478bdstevel@tonic-gate				error = 1;
8737c478bdstevel@tonic-gate			} else if (stat(argv[i], &buf)) {
8747c478bdstevel@tonic-gate				fprintf(stderr, "at: %s: ", argv[i]);
8757c478bdstevel@tonic-gate				perror("");
8767c478bdstevel@tonic-gate				error = 1;
8777c478bdstevel@tonic-gate			} else if ((user != buf.st_uid) &&
878d1419d5Nobutomo Nakano			    (!cron_admin(pwd->pw_name))) {
8797c478bdstevel@tonic-gate				fprintf(stderr, gettext(
8807c478bdstevel@tonic-gate				    "at: you don't own %s\n"), argv[i]);
8817c478bdstevel@tonic-gate				error = 1;
8827c478bdstevel@tonic-gate			} else if (pflag && not_this_project(job_file)) {
8837c478bdstevel@tonic-gate				continue;
8847c478bdstevel@tonic-gate			} else {
8857c478bdstevel@tonic-gate				if (!qflag || (qflag &&
8867c478bdstevel@tonic-gate				    check_queue(ptr, queue))) {
8877c478bdstevel@tonic-gate					ascftime(timebuf, FORMAT,
8887c478bdstevel@tonic-gate					    localtime(&t));
8897c478bdstevel@tonic-gate					printf("%s\t%s\n", argv[i], timebuf);
8907c478bdstevel@tonic-gate				}
8917c478bdstevel@tonic-gate			}
8927c478bdstevel@tonic-gate		}
8937c478bdstevel@tonic-gate	return (error);
8947c478bdstevel@tonic-gate}
8957c478bdstevel@tonic-gate
8967c478bdstevel@tonic-gate/*
8977c478bdstevel@tonic-gate * open the command file and read the project id line
8987c478bdstevel@tonic-gate * compare to the project number provided via -p on the command line
8997c478bdstevel@tonic-gate * return 0 if they match, 1 if they don't match or an error occurs.
9007c478bdstevel@tonic-gate */
9017c478bdstevel@tonic-gate#define	SKIPCOUNT 3	/* lines to skip to get to project line in file */
9027c478bdstevel@tonic-gate
9037c478bdstevel@tonic-gatestatic int
9047c478bdstevel@tonic-gatenot_this_project(char *filename)
9057c478bdstevel@tonic-gate{
9067c478bdstevel@tonic-gate	FILE *fp;
9077c478bdstevel@tonic-gate	projid_t sproj;
9087c478bdstevel@tonic-gate	int i;
9097c478bdstevel@tonic-gate
9107c478bdstevel@tonic-gate	if ((fp = fopen(filename, "r")) == NULL)
9117c478bdstevel@tonic-gate		return (1);
9127c478bdstevel@tonic-gate
9137c478bdstevel@tonic-gate	for (i = 0; i < SKIPCOUNT; i++)
9147c478bdstevel@tonic-gate		fscanf(fp, "%*[^\n]\n");
9157c478bdstevel@tonic-gate
9167c478bdstevel@tonic-gate	fscanf(fp, ": project: %d\n", &sproj);
9177c478bdstevel@tonic-gate	fclose(fp);
9187c478bdstevel@tonic-gate
9197c478bdstevel@tonic-gate	return (sproj == project ? 0 : 1);
9207c478bdstevel@tonic-gate}
9217c478bdstevel@tonic-gate
9227c478bdstevel@tonic-gatestatic int
9237c478bdstevel@tonic-gatecheck_queue(char *name, int queue)
9247c478bdstevel@tonic-gate{
9257c478bdstevel@tonic-gate	if ((name[strlen(name) - 1] - 'a') == queue)
9267c478bdstevel@tonic-gate		return (1);
9277c478bdstevel@tonic-gate	else
9287c478bdstevel@tonic-gate		return (0);
9297c478bdstevel@tonic-gate}
9307c478bdstevel@tonic-gate
9317c478bdstevel@tonic-gatestatic time_t
9327c478bdstevel@tonic-gateparse_time(char *t)
9337c478bdstevel@tonic-gate{
9347c478bdstevel@tonic-gate	int		century = 0;
9357c478bdstevel@tonic-gate	int		seconds = 0;
9367c478bdstevel@tonic-gate	char		*p;
9377c478bdstevel@tonic-gate	time_t		when	= 0;
9387c478bdstevel@tonic-gate	struct tm	tm;
9397c478bdstevel@tonic-gate
9407c478bdstevel@tonic-gate	/*
9417c478bdstevel@tonic-gate	 * time in the following format (defined by the touch(1) spec):
9427c478bdstevel@tonic-gate	 *	[[CC]YY]MMDDhhmm[.SS]
9437c478bdstevel@tonic-gate	 */
9447c478bdstevel@tonic-gate	if ((p = strchr(t, '.')) != NULL) {
9457c478bdstevel@tonic-gate		if (strchr(p+1, '.') != NULL)
9467c478bdstevel@tonic-gate			atabort(BADTIME);
9477c478bdstevel@tonic-gate		seconds = atoi_for2(p+1);
9487c478bdstevel@tonic-gate		*p = '\0';
9497c478bdstevel@tonic-gate	}
9507c478bdstevel@tonic-gate
9517c478bdstevel@tonic-gate	memset(&tm, 0, sizeof (struct tm));
9527c478bdstevel@tonic-gate	when = time(0);
9537c478bdstevel@tonic-gate	tm.tm_year = localtime(&when)->tm_year;
9547c478bdstevel@tonic-gate
9557c478bdstevel@tonic-gate	switch (strlen(t)) {
9567c478bdstevel@tonic-gate		case 12:	/* CCYYMMDDhhmm */
9577c478bdstevel@tonic-gate			century = atoi_for2(t);
9587c478bdstevel@tonic-gate			t += 2;
9594159a98Toomas Soome			/* FALLTHROUGH */
9607c478bdstevel@tonic-gate		case 10:	/* YYMMDDhhmm */
9617c478bdstevel@tonic-gate			tm.tm_year = atoi_for2(t);
9627c478bdstevel@tonic-gate			t += 2;
9637c478bdstevel@tonic-gate			if (century == 0) {
9647c478bdstevel@tonic-gate				if (tm.tm_year < 69)
9657c478bdstevel@tonic-gate					tm.tm_year += 100;
9667c478bdstevel@tonic-gate			} else
9677c478bdstevel@tonic-gate				tm.tm_year += (century - 19) * 100;
9684159a98Toomas Soome			/* FALLTHROUGH */
9697c478bdstevel@tonic-gate		case 8:		/* MMDDhhmm */
9707c478bdstevel@tonic-gate			tm.tm_mon = atoi_for2(t) - 1;
9717c478bdstevel@tonic-gate			t += 2;
9727c478bdstevel@tonic-gate			tm.tm_mday = atoi_for2(t);
9737c478bdstevel@tonic-gate			t += 2;
9747c478bdstevel@tonic-gate			tm.tm_hour = atoi_for2(t);
9757c478bdstevel@tonic-gate			t += 2;
9767c478bdstevel@tonic-gate			tm.tm_min = atoi_for2(t);
9777c478bdstevel@tonic-gate			t += 2;
9787c478bdstevel@tonic-gate			tm.tm_sec = seconds;
9797c478bdstevel@tonic-gate			break;
9807c478bdstevel@tonic-gate		default:
9817c478bdstevel@tonic-gate			atabort(BADTIME);
9827c478bdstevel@tonic-gate	}
9837c478bdstevel@tonic-gate
9847c478bdstevel@tonic-gate	if ((when = mktime(&tm)) == -1)
9857c478bdstevel@tonic-gate		atabort(BADTIME);
9867c478bdstevel@tonic-gate	if (tm.tm_isdst)
9877c478bdstevel@tonic-gate		when -= (timezone-altzone);
9887c478bdstevel@tonic-gate	return (when);
9897c478bdstevel@tonic-gate}
9907c478bdstevel@tonic-gate
9917c478bdstevel@tonic-gatestatic int
9924159a98Toomas Soomeatoi_for2(char *p)
9934159a98Toomas Soome{
9947c478bdstevel@tonic-gate	int value;
9957c478bdstevel@tonic-gate
9967c478bdstevel@tonic-gate	value = (*p - '0') * 10 + *(p+1) - '0';
9977c478bdstevel@tonic-gate	if ((value < 0) || (value > 99))
9987c478bdstevel@tonic-gate		atabort(BADTIME);
9997c478bdstevel@tonic-gate	return (value);
10007c478bdstevel@tonic-gate}
10017c478bdstevel@tonic-gate
10027c478bdstevel@tonic-gatestatic void
10037c478bdstevel@tonic-gateusage(void)
10047c478bdstevel@tonic-gate{
10057c478bdstevel@tonic-gate	fprintf(stderr, USAGE);
10067c478bdstevel@tonic-gate	exit(1);
10077c478bdstevel@tonic-gate}
1008