10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11115SNobutomo.Nakano@Sun.COM * Common Development and Distribution License (the "License").
6*11115SNobutomo.Nakano@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate
220Sstevel@tonic-gate /*
23*11115SNobutomo.Nakano@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27523Sbasabi /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28523Sbasabi /* All Rights Reserved */
29523Sbasabi
300Sstevel@tonic-gate #include <sys/resource.h>
310Sstevel@tonic-gate #include <sys/stat.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <dirent.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <pwd.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <ctype.h>
410Sstevel@tonic-gate #include <time.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <limits.h>
450Sstevel@tonic-gate #include <ulimit.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <locale.h>
480Sstevel@tonic-gate #include <libintl.h>
490Sstevel@tonic-gate #include <tzfile.h>
500Sstevel@tonic-gate #include <project.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #include "cron.h"
530Sstevel@tonic-gate
540Sstevel@tonic-gate #define TMPFILE "_at" /* prefix for temporary files */
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * Mode for creating files in ATDIR.
570Sstevel@tonic-gate * Setuid bit on so that if an owner of a file gives that file
580Sstevel@tonic-gate * away to someone else, the setuid bit will no longer be set.
590Sstevel@tonic-gate * If this happens, atrun will not execute the file
600Sstevel@tonic-gate */
610Sstevel@tonic-gate #define ATMODE (S_ISUID | S_IRUSR | S_IRGRP | S_IROTH)
620Sstevel@tonic-gate #define ROOT 0 /* user-id of super-user */
630Sstevel@tonic-gate #define MAXTRYS 100 /* max trys to create at job file */
640Sstevel@tonic-gate
650Sstevel@tonic-gate #define BADTIME "bad time specification"
660Sstevel@tonic-gate #define BADQUEUE "queue name must be a single character a-z"
670Sstevel@tonic-gate #define NOTCQUEUE "queue c is reserved for cron entries"
680Sstevel@tonic-gate #define BADSHELL "because your login shell isn't /usr/bin/sh,"\
690Sstevel@tonic-gate "you can't use at"
700Sstevel@tonic-gate #define WARNSHELL "commands will be executed using %s\n"
710Sstevel@tonic-gate #define CANTCD "can't change directory to the at directory"
720Sstevel@tonic-gate #define CANTCHOWN "can't change the owner of your job to you"
730Sstevel@tonic-gate #define CANTCREATE "can't create a job for you"
740Sstevel@tonic-gate #define INVALIDUSER "you are not a valid user (no entry in /etc/passwd)"
750Sstevel@tonic-gate #define NOOPENDIR "can't open the at directory"
760Sstevel@tonic-gate #define NOTALLOWED "you are not authorized to use at. Sorry."
770Sstevel@tonic-gate #define USAGE\
780Sstevel@tonic-gate "usage: at [-c|-k|-s] [-m] [-f file] [-p project] [-q queuename] "\
790Sstevel@tonic-gate "-t time\n"\
800Sstevel@tonic-gate " at [-c|-k|-s] [-m] [-f file] [-p project] [-q queuename] "\
810Sstevel@tonic-gate "timespec\n"\
820Sstevel@tonic-gate " at -l [-p project] [-q queuename] [at_job_id...]\n"\
830Sstevel@tonic-gate " at -r at_job_id ...\n"
840Sstevel@tonic-gate
850Sstevel@tonic-gate #define FORMAT "%a %b %e %H:%M:%S %Y"
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* Macros used in format for fscanf */
880Sstevel@tonic-gate #define AQ(x) #x
890Sstevel@tonic-gate #define BUFFMT(p) AQ(p)
900Sstevel@tonic-gate
910Sstevel@tonic-gate static int leap(int);
920Sstevel@tonic-gate static int atoi_for2(char *);
930Sstevel@tonic-gate static int check_queue(char *, int);
940Sstevel@tonic-gate static int list_jobs(int, char **, int, int);
950Sstevel@tonic-gate static int remove_jobs(int, char **, char *);
960Sstevel@tonic-gate static void usage(void);
970Sstevel@tonic-gate static void catch(int);
980Sstevel@tonic-gate static void copy(char *, FILE *, int);
990Sstevel@tonic-gate static void atime(struct tm *, struct tm *);
1000Sstevel@tonic-gate static int not_this_project(char *);
1010Sstevel@tonic-gate static char *mkjobname(time_t);
1020Sstevel@tonic-gate static time_t parse_time(char *);
1030Sstevel@tonic-gate static time_t gtime(struct tm *);
104523Sbasabi void atabort(char *)__NORETURN;
1050Sstevel@tonic-gate void yyerror(void);
1060Sstevel@tonic-gate extern int yyparse(void);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate extern void audit_at_delete(char *, char *, int);
1090Sstevel@tonic-gate extern int audit_at_create(char *, int);
1100Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *);
1110Sstevel@tonic-gate extern int audit_cron_delete_anc_file(char *, char *);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * Error in getdate(3G)
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate static char *errlist[] = {
1170Sstevel@tonic-gate /* 0 */ "",
1180Sstevel@tonic-gate /* 1 */ "getdate: The DATEMSK environment variable is not set",
1190Sstevel@tonic-gate /* 2 */ "getdate: Error on \"open\" of the template file",
1200Sstevel@tonic-gate /* 3 */ "getdate: Error on \"stat\" of the template file",
1210Sstevel@tonic-gate /* 4 */ "getdate: The template file is not a regular file",
1220Sstevel@tonic-gate /* 5 */ "getdate: An error is encountered while reading the template",
1230Sstevel@tonic-gate /* 6 */ "getdate: Malloc(3C) failed",
1240Sstevel@tonic-gate /* 7 */ "getdate: There is no line in the template that matches the input",
1250Sstevel@tonic-gate /* 8 */ "getdate: Invalid input specification"
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate int gmtflag = 0;
1290Sstevel@tonic-gate int mday[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1300Sstevel@tonic-gate uid_t user;
1310Sstevel@tonic-gate struct tm *tp, at, rt;
1320Sstevel@tonic-gate static int cshflag = 0;
1330Sstevel@tonic-gate static int kshflag = 0;
1340Sstevel@tonic-gate static int shflag = 0;
1350Sstevel@tonic-gate static int mflag = 0;
1360Sstevel@tonic-gate static int pflag = 0;
1370Sstevel@tonic-gate static char *Shell;
1380Sstevel@tonic-gate static char *tfname;
1390Sstevel@tonic-gate static char pname[80];
1400Sstevel@tonic-gate static char pname1[80];
1410Sstevel@tonic-gate static short jobtype = ATEVENT; /* set to 1 if batch job */
1420Sstevel@tonic-gate extern char *argp;
1430Sstevel@tonic-gate extern int per_errno;
1440Sstevel@tonic-gate static projid_t project;
1450Sstevel@tonic-gate
146523Sbasabi int
main(int argc,char ** argv)147523Sbasabi main(int argc, char **argv)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate FILE *inputfile;
1500Sstevel@tonic-gate int i, fd;
1510Sstevel@tonic-gate int try = 0;
1520Sstevel@tonic-gate int fflag = 0;
1530Sstevel@tonic-gate int lflag = 0;
1540Sstevel@tonic-gate int qflag = 0;
1550Sstevel@tonic-gate int rflag = 0;
1560Sstevel@tonic-gate int tflag = 0;
1570Sstevel@tonic-gate int c;
1580Sstevel@tonic-gate int tflen;
1590Sstevel@tonic-gate char *file;
1600Sstevel@tonic-gate char *login;
1610Sstevel@tonic-gate char *job;
1620Sstevel@tonic-gate char *jobfile = NULL; /* file containing job to be run */
1630Sstevel@tonic-gate char argpbuf[LINE_MAX], timebuf[80];
1640Sstevel@tonic-gate time_t now;
1650Sstevel@tonic-gate time_t when = 0;
1660Sstevel@tonic-gate struct tm *ct;
1670Sstevel@tonic-gate char *proj;
1680Sstevel@tonic-gate struct project prj, *pprj;
1690Sstevel@tonic-gate char mybuf[PROJECT_BUFSZ];
1700Sstevel@tonic-gate char ipbuf[PROJECT_BUFSZ];
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1730Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1740Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate user = getuid();
1790Sstevel@tonic-gate login = getuser(user);
1800Sstevel@tonic-gate if (login == NULL) {
1810Sstevel@tonic-gate if (per_errno == 2)
1820Sstevel@tonic-gate atabort(BADSHELL);
1830Sstevel@tonic-gate else
1840Sstevel@tonic-gate atabort(INVALIDUSER);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if (!allowed(login, ATALLOW, ATDENY))
1880Sstevel@tonic-gate atabort(NOTALLOWED);
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate while ((c = getopt(argc, argv, "cklmsrf:p:q:t:")) != EOF)
1910Sstevel@tonic-gate switch (c) {
1920Sstevel@tonic-gate case 'c':
1930Sstevel@tonic-gate cshflag++;
1940Sstevel@tonic-gate break;
1950Sstevel@tonic-gate case 'f':
1960Sstevel@tonic-gate fflag++;
1970Sstevel@tonic-gate jobfile = optarg;
1980Sstevel@tonic-gate break;
1990Sstevel@tonic-gate case 'k':
2000Sstevel@tonic-gate kshflag++;
2010Sstevel@tonic-gate break;
2020Sstevel@tonic-gate case 'l':
2030Sstevel@tonic-gate lflag++;
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate case 'm':
2060Sstevel@tonic-gate mflag++;
2070Sstevel@tonic-gate break;
2080Sstevel@tonic-gate case 'p':
2090Sstevel@tonic-gate proj = optarg;
2100Sstevel@tonic-gate pprj = &prj;
2110Sstevel@tonic-gate if ((pprj = getprojbyname(proj, pprj,
2120Sstevel@tonic-gate (void *)&mybuf, sizeof (mybuf))) != NULL) {
2130Sstevel@tonic-gate project = pprj->pj_projid;
2140Sstevel@tonic-gate if (inproj(login, pprj->pj_name,
2150Sstevel@tonic-gate (void *)&ipbuf, sizeof (ipbuf)))
2160Sstevel@tonic-gate pflag++;
2170Sstevel@tonic-gate else {
2180Sstevel@tonic-gate (void) fprintf(stderr,
2190Sstevel@tonic-gate gettext("at: user %s is "
220*11115SNobutomo.Nakano@Sun.COM "not a member of "
221*11115SNobutomo.Nakano@Sun.COM "project %s (%d)\n"),
2220Sstevel@tonic-gate login, pprj->pj_name,
2230Sstevel@tonic-gate project);
2240Sstevel@tonic-gate exit(2);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate break;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate pprj = &prj;
2290Sstevel@tonic-gate if (isdigit(proj[0]) &&
2300Sstevel@tonic-gate (pprj = getprojbyid(atoi(proj), pprj,
2310Sstevel@tonic-gate (void *)&mybuf, sizeof (mybuf))) != NULL) {
2320Sstevel@tonic-gate project = pprj->pj_projid;
2330Sstevel@tonic-gate if (inproj(login, pprj->pj_name,
2340Sstevel@tonic-gate (void *)&ipbuf, sizeof (ipbuf)))
2350Sstevel@tonic-gate pflag++;
2360Sstevel@tonic-gate else {
2370Sstevel@tonic-gate (void) fprintf(stderr,
2380Sstevel@tonic-gate gettext("at: user %s is "
239*11115SNobutomo.Nakano@Sun.COM "not a member of "
240*11115SNobutomo.Nakano@Sun.COM "project %s (%d)\n"),
2410Sstevel@tonic-gate login, pprj->pj_name,
2420Sstevel@tonic-gate project);
2430Sstevel@tonic-gate exit(2);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate break;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate (void) fprintf(stderr, gettext("at: project "
2480Sstevel@tonic-gate "%s not found.\n"), proj);
2490Sstevel@tonic-gate exit(2);
2500Sstevel@tonic-gate break;
2510Sstevel@tonic-gate case 'q':
2520Sstevel@tonic-gate qflag++;
2530Sstevel@tonic-gate if (optarg[1] != '\0')
2540Sstevel@tonic-gate atabort(BADQUEUE);
2550Sstevel@tonic-gate jobtype = *optarg - 'a';
2560Sstevel@tonic-gate if ((jobtype < 0) || (jobtype > 25))
2570Sstevel@tonic-gate atabort(BADQUEUE);
2580Sstevel@tonic-gate if (jobtype == 2)
2590Sstevel@tonic-gate atabort(NOTCQUEUE);
2600Sstevel@tonic-gate break;
2610Sstevel@tonic-gate case 'r':
2620Sstevel@tonic-gate rflag++;
2630Sstevel@tonic-gate break;
2640Sstevel@tonic-gate case 's':
2650Sstevel@tonic-gate shflag++;
2660Sstevel@tonic-gate break;
2670Sstevel@tonic-gate case 't':
2680Sstevel@tonic-gate tflag++;
2690Sstevel@tonic-gate when = parse_time(optarg);
2700Sstevel@tonic-gate break;
2710Sstevel@tonic-gate default:
2720Sstevel@tonic-gate usage();
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate argc -= optind;
2760Sstevel@tonic-gate argv += optind;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate if (lflag + rflag > 1)
2790Sstevel@tonic-gate usage();
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate if (lflag) {
2820Sstevel@tonic-gate if (cshflag || kshflag || shflag || mflag ||
283*11115SNobutomo.Nakano@Sun.COM fflag || tflag || rflag)
2840Sstevel@tonic-gate usage();
2850Sstevel@tonic-gate return (list_jobs(argc, argv, qflag, jobtype));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if (rflag) {
2890Sstevel@tonic-gate if (cshflag || kshflag || shflag || mflag ||
290*11115SNobutomo.Nakano@Sun.COM fflag || tflag || qflag)
2910Sstevel@tonic-gate usage();
2920Sstevel@tonic-gate return (remove_jobs(argc, argv, login));
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate if ((argc + tflag == 0) && (jobtype != BATCHEVENT))
2960Sstevel@tonic-gate usage();
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate if (cshflag + kshflag + shflag > 1)
2990Sstevel@tonic-gate atabort("ambiguous shell request");
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate time(&now);
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (jobtype == BATCHEVENT)
3040Sstevel@tonic-gate when = now;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate if (when == 0) { /* figure out what time to run the job */
3070Sstevel@tonic-gate int argplen = sizeof (argpbuf) - 1;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate argpbuf[0] = '\0';
3100Sstevel@tonic-gate argp = argpbuf;
3110Sstevel@tonic-gate i = 0;
3120Sstevel@tonic-gate while (i < argc) {
3130Sstevel@tonic-gate /* guard against buffer overflow */
3140Sstevel@tonic-gate argplen -= strlen(argv[i]) + 1;
3150Sstevel@tonic-gate if (argplen < 0)
3160Sstevel@tonic-gate atabort(BADTIME);
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate strcat(argp, argv[i]);
3190Sstevel@tonic-gate strcat(argp, " ");
3200Sstevel@tonic-gate i++;
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate if ((file = getenv("DATEMSK")) == 0 || file[0] == '\0') {
3230Sstevel@tonic-gate tp = localtime(&now);
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate * Fix for 1047182 - we have to let yyparse
3260Sstevel@tonic-gate * check bounds on mday[] first, then fixup
3270Sstevel@tonic-gate * the leap year case.
3280Sstevel@tonic-gate */
3290Sstevel@tonic-gate yyparse();
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate mday[1] = 28 + leap(at.tm_year);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate if (at.tm_mday > mday[at.tm_mon])
3340Sstevel@tonic-gate atabort("bad date");
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate atime(&at, &rt);
3370Sstevel@tonic-gate when = gtime(&at);
3380Sstevel@tonic-gate if (!gmtflag) {
3390Sstevel@tonic-gate when += timezone;
3400Sstevel@tonic-gate if (localtime(&when)->tm_isdst)
3410Sstevel@tonic-gate when -= (timezone-altzone);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate } else { /* DATEMSK is set */
3440Sstevel@tonic-gate if ((ct = getdate(argpbuf)) == NULL)
3450Sstevel@tonic-gate atabort(errlist[getdate_err]);
3460Sstevel@tonic-gate else
3470Sstevel@tonic-gate when = mktime(ct);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate if (when < now) /* time has already past */
3520Sstevel@tonic-gate atabort("too late");
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate tflen = strlen(ATDIR) + 1 + strlen(TMPFILE) +
3550Sstevel@tonic-gate 10 + 1; /* 10 for an INT_MAX pid */
3560Sstevel@tonic-gate tfname = xmalloc(tflen);
3570Sstevel@tonic-gate snprintf(tfname, tflen, "%s/%s%d", ATDIR, TMPFILE, getpid());
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /* catch INT, HUP, TERM and QUIT signals */
3600Sstevel@tonic-gate if (signal(SIGINT, catch) == SIG_IGN)
3610Sstevel@tonic-gate signal(SIGINT, SIG_IGN);
3620Sstevel@tonic-gate if (signal(SIGHUP, catch) == SIG_IGN)
3630Sstevel@tonic-gate signal(SIGHUP, SIG_IGN);
3640Sstevel@tonic-gate if (signal(SIGQUIT, catch) == SIG_IGN)
3650Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN);
3660Sstevel@tonic-gate if (signal(SIGTERM, catch) == SIG_IGN)
3670Sstevel@tonic-gate signal(SIGTERM, SIG_IGN);
3680Sstevel@tonic-gate if ((fd = open(tfname, O_CREAT|O_EXCL|O_WRONLY, ATMODE)) < 0)
3690Sstevel@tonic-gate atabort(CANTCREATE);
3700Sstevel@tonic-gate if (chown(tfname, user, getgid()) == -1) {
3710Sstevel@tonic-gate unlink(tfname);
3720Sstevel@tonic-gate atabort(CANTCHOWN);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate close(1);
3750Sstevel@tonic-gate dup(fd);
3760Sstevel@tonic-gate close(fd);
3770Sstevel@tonic-gate sprintf(pname, "%s", PROTO);
3780Sstevel@tonic-gate sprintf(pname1, "%s.%c", PROTO, 'a'+jobtype);
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate * Open the input file with the user's permissions.
3820Sstevel@tonic-gate */
3830Sstevel@tonic-gate if (jobfile != NULL) {
3840Sstevel@tonic-gate if ((seteuid(user) < 0) ||
3850Sstevel@tonic-gate (inputfile = fopen(jobfile, "r")) == NULL) {
3860Sstevel@tonic-gate unlink(tfname);
3870Sstevel@tonic-gate fprintf(stderr, "at: %s: %s\n", jobfile, errmsg(errno));
3880Sstevel@tonic-gate exit(1);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate else
3910Sstevel@tonic-gate seteuid(0);
3920Sstevel@tonic-gate } else
3930Sstevel@tonic-gate inputfile = stdin;
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate copy(jobfile, inputfile, when);
3960Sstevel@tonic-gate while (rename(tfname, job = mkjobname(when)) == -1) {
3970Sstevel@tonic-gate sleep(1);
3980Sstevel@tonic-gate if (++try > MAXTRYS / 10) {
3990Sstevel@tonic-gate unlink(tfname);
4000Sstevel@tonic-gate atabort(CANTCREATE);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate unlink(tfname);
4040Sstevel@tonic-gate if (audit_at_create(job, 0))
4050Sstevel@tonic-gate atabort(CANTCREATE);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate cron_sendmsg(ADD, login, strrchr(job, '/')+1, AT);
4080Sstevel@tonic-gate if (per_errno == 2)
4090Sstevel@tonic-gate fprintf(stderr, gettext(WARNSHELL), Shell);
4100Sstevel@tonic-gate cftime(timebuf, FORMAT, &when);
4110Sstevel@tonic-gate fprintf(stderr, gettext("job %s at %s\n"),
4120Sstevel@tonic-gate strrchr(job, '/')+1, timebuf);
4130Sstevel@tonic-gate if (when - MINUTE < HOUR)
4140Sstevel@tonic-gate fprintf(stderr, gettext(
4150Sstevel@tonic-gate "at: this job may not be executed at the proper time.\n"));
4160Sstevel@tonic-gate return (0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate static char *
mkjobname(t)4210Sstevel@tonic-gate mkjobname(t)
4220Sstevel@tonic-gate time_t t;
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate int i, fd;
4250Sstevel@tonic-gate char *name;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate name = xmalloc(200);
4280Sstevel@tonic-gate for (i = 0; i < MAXTRYS; i++) {
4290Sstevel@tonic-gate sprintf(name, "%s/%ld.%c", ATDIR, t, 'a'+jobtype);
4300Sstevel@tonic-gate /* fix for 1099183, 1116833 - create file here, avoid race */
4310Sstevel@tonic-gate if ((fd = open(name, O_CREAT | O_EXCL, ATMODE)) > 0) {
4320Sstevel@tonic-gate close(fd);
4330Sstevel@tonic-gate return (name);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate t += 1;
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate atabort("queue full");
438523Sbasabi /* NOTREACHED */
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate static void
catch(int x)4430Sstevel@tonic-gate catch(int x)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate unlink(tfname);
4460Sstevel@tonic-gate exit(1);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate void
atabort(msg)4510Sstevel@tonic-gate atabort(msg)
4520Sstevel@tonic-gate char *msg;
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate fprintf(stderr, "at: %s\n", gettext(msg));
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate exit(1);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate
459523Sbasabi int
yywrap(void)4600Sstevel@tonic-gate yywrap(void)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate return (1);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate void
yyerror(void)4660Sstevel@tonic-gate yyerror(void)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate atabort(BADTIME);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate * add time structures logically
4730Sstevel@tonic-gate */
4740Sstevel@tonic-gate static void
atime(struct tm * a,struct tm * b)4750Sstevel@tonic-gate atime(struct tm *a, struct tm *b)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate if ((a->tm_sec += b->tm_sec) >= 60) {
4780Sstevel@tonic-gate b->tm_min += a->tm_sec / 60;
4790Sstevel@tonic-gate a->tm_sec %= 60;
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate if ((a->tm_min += b->tm_min) >= 60) {
4820Sstevel@tonic-gate b->tm_hour += a->tm_min / 60;
4830Sstevel@tonic-gate a->tm_min %= 60;
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate if ((a->tm_hour += b->tm_hour) >= 24) {
4860Sstevel@tonic-gate b->tm_mday += a->tm_hour / 24;
4870Sstevel@tonic-gate a->tm_hour %= 24;
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate a->tm_year += b->tm_year;
4900Sstevel@tonic-gate if ((a->tm_mon += b->tm_mon) >= 12) {
4910Sstevel@tonic-gate a->tm_year += a->tm_mon / 12;
4920Sstevel@tonic-gate a->tm_mon %= 12;
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate a->tm_mday += b->tm_mday;
4950Sstevel@tonic-gate mday[1] = 28 + leap(a->tm_year);
4960Sstevel@tonic-gate while (a->tm_mday > mday[a->tm_mon]) {
4970Sstevel@tonic-gate a->tm_mday -= mday[a->tm_mon++];
4980Sstevel@tonic-gate if (a->tm_mon > 11) {
4990Sstevel@tonic-gate a->tm_mon = 0;
5000Sstevel@tonic-gate mday[1] = 28 + leap(++a->tm_year);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate static int
leap(int year)5070Sstevel@tonic-gate leap(int year)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate return (isleap(year + TM_YEAR_BASE));
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate * return time from time structure
5140Sstevel@tonic-gate */
5150Sstevel@tonic-gate static time_t
gtime(tptr)5160Sstevel@tonic-gate gtime(tptr)
5170Sstevel@tonic-gate struct tm *tptr;
5180Sstevel@tonic-gate {
519523Sbasabi int i;
5200Sstevel@tonic-gate long tv;
5210Sstevel@tonic-gate int dmsize[12] =
5220Sstevel@tonic-gate {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate tv = 0;
5260Sstevel@tonic-gate for (i = 1970; i != tptr->tm_year+TM_YEAR_BASE; i++)
5270Sstevel@tonic-gate tv += (365 + isleap(i));
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate * We call isleap since leap() adds
5300Sstevel@tonic-gate * 1900 onto any value passed
5310Sstevel@tonic-gate */
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate if (!leap(tptr->tm_year) && at.tm_mday == 29 && at.tm_mon == 1)
5340Sstevel@tonic-gate atabort("bad date - not a leap year");
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate if ((leap(tptr->tm_year)) && tptr->tm_mon >= 2)
5370Sstevel@tonic-gate ++tv;
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate for (i = 0; i < tptr->tm_mon; ++i)
5400Sstevel@tonic-gate tv += dmsize[i];
5410Sstevel@tonic-gate tv += tptr->tm_mday - 1;
5420Sstevel@tonic-gate tv = 24 * tv + tptr->tm_hour;
5430Sstevel@tonic-gate tv = 60 * tv + tptr->tm_min;
5440Sstevel@tonic-gate tv = 60 * tv + tptr->tm_sec;
5450Sstevel@tonic-gate return (tv);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate * make job file from proto + stdin
5500Sstevel@tonic-gate */
5510Sstevel@tonic-gate static void
copy(char * jobfile,FILE * inputfile,int when)5520Sstevel@tonic-gate copy(char *jobfile, FILE *inputfile, int when)
5530Sstevel@tonic-gate {
554523Sbasabi int c;
555523Sbasabi FILE *pfp;
556523Sbasabi FILE *xfp;
5570Sstevel@tonic-gate char *shell;
5580Sstevel@tonic-gate char dirbuf[PATH_MAX + 1];
5590Sstevel@tonic-gate char line[LINE_MAX];
560523Sbasabi char **ep;
5610Sstevel@tonic-gate mode_t um;
5620Sstevel@tonic-gate char *val;
5630Sstevel@tonic-gate extern char **environ;
5640Sstevel@tonic-gate int pfd[2];
5650Sstevel@tonic-gate pid_t pid;
5660Sstevel@tonic-gate uid_t realusr;
5670Sstevel@tonic-gate int ttyinput;
5680Sstevel@tonic-gate int ulimit_flag = 0;
5690Sstevel@tonic-gate struct rlimit rlp;
5700Sstevel@tonic-gate struct project prj, *pprj;
5710Sstevel@tonic-gate char pbuf[PROJECT_BUFSZ];
5720Sstevel@tonic-gate char pbuf2[PROJECT_BUFSZ];
5730Sstevel@tonic-gate char *user;
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate * Fix for 1099381:
5770Sstevel@tonic-gate * If the inputfile is from a tty, then turn on prompting, and
5780Sstevel@tonic-gate * put out a prompt now, instead of waiting for a lot of file
5790Sstevel@tonic-gate * activity to complete.
5800Sstevel@tonic-gate */
5810Sstevel@tonic-gate ttyinput = isatty(fileno(inputfile));
5820Sstevel@tonic-gate if (ttyinput) {
5830Sstevel@tonic-gate fputs("at> ", stderr);
5840Sstevel@tonic-gate fflush(stderr);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate * Fix for 1053807:
5890Sstevel@tonic-gate * Determine what shell we should use to run the job. If the user
5900Sstevel@tonic-gate * didn't explicitly request that his/her current shell be over-
5910Sstevel@tonic-gate * ridden (shflag or cshflag), then we use the current shell.
5920Sstevel@tonic-gate */
5930Sstevel@tonic-gate if (cshflag)
5940Sstevel@tonic-gate Shell = shell = "/bin/csh";
5950Sstevel@tonic-gate else if (kshflag) {
5960Sstevel@tonic-gate Shell = shell = "/bin/ksh";
5970Sstevel@tonic-gate ulimit_flag = 1;
5980Sstevel@tonic-gate } else if (shflag) {
5990Sstevel@tonic-gate Shell = shell = "/bin/sh";
6000Sstevel@tonic-gate ulimit_flag = 1;
6010Sstevel@tonic-gate } else if (((Shell = val = getenv("SHELL")) != NULL) &&
6020Sstevel@tonic-gate (*val != '\0')) {
6030Sstevel@tonic-gate shell = "$SHELL";
6040Sstevel@tonic-gate if ((strstr(val, "/sh") != NULL) ||
6050Sstevel@tonic-gate (strstr(val, "/ksh") != NULL))
6060Sstevel@tonic-gate ulimit_flag = 1;
6070Sstevel@tonic-gate } else {
6080Sstevel@tonic-gate /* SHELL is NULL or unset, therefore use default */
6090Sstevel@tonic-gate #ifdef XPG4
6100Sstevel@tonic-gate Shell = shell = "/usr/xpg4/bin/sh";
6110Sstevel@tonic-gate #else
6120Sstevel@tonic-gate Shell = shell = "/bin/sh";
6130Sstevel@tonic-gate #endif /* XPG4 */
6140Sstevel@tonic-gate ulimit_flag = 1;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate printf(": %s job\n", jobtype ? "batch" : "at");
6180Sstevel@tonic-gate printf(": jobname: %.127s\n", (jobfile == NULL) ? "stdin" : jobfile);
6190Sstevel@tonic-gate printf(": notify by mail: %s\n", (mflag) ? "yes" : "no");
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate if (pflag) {
6220Sstevel@tonic-gate (void) printf(": project: %d\n", project);
6230Sstevel@tonic-gate } else {
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate * Check if current user is a member of current project.
6260Sstevel@tonic-gate * This check is done here to avoid setproject() failure
6270Sstevel@tonic-gate * later when the job gets executed. If current user does
6280Sstevel@tonic-gate * not belong to current project, user's default project
6290Sstevel@tonic-gate * will be used instead. This is achieved by not specifying
6300Sstevel@tonic-gate * the project (": project: <project>\n") in the job file.
6310Sstevel@tonic-gate */
6320Sstevel@tonic-gate if ((user = getuser(getuid())) == NULL)
6330Sstevel@tonic-gate atabort(INVALIDUSER);
6340Sstevel@tonic-gate project = getprojid();
6350Sstevel@tonic-gate pprj = getprojbyid(project, &prj, pbuf, sizeof (pbuf));
6360Sstevel@tonic-gate if (pprj != NULL) {
6370Sstevel@tonic-gate if (inproj(user, pprj->pj_name, pbuf2, sizeof (pbuf2)))
6380Sstevel@tonic-gate (void) printf(": project: %d\n", project);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate for (ep = environ; *ep; ep++) {
6430Sstevel@tonic-gate if (strchr(*ep, '\'') != NULL)
6440Sstevel@tonic-gate continue;
6450Sstevel@tonic-gate if ((val = strchr(*ep, '=')) == NULL)
6460Sstevel@tonic-gate continue;
6470Sstevel@tonic-gate *val++ = '\0';
6480Sstevel@tonic-gate printf("export %s; %s='%s'\n", *ep, *ep, val);
6490Sstevel@tonic-gate *--val = '=';
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate if ((pfp = fopen(pname1, "r")) == NULL &&
6520Sstevel@tonic-gate (pfp = fopen(pname, "r")) == NULL)
6530Sstevel@tonic-gate atabort("no prototype");
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate * Put in a line to run the proper shell using the rest of
6560Sstevel@tonic-gate * the file as input. Note that 'exec'ing the shell will
6570Sstevel@tonic-gate * cause sh() to leave a /tmp/sh### file around. (1053807)
6580Sstevel@tonic-gate */
6590Sstevel@tonic-gate printf("%s << '...the rest of this file is shell input'\n", shell);
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate um = umask(0);
6620Sstevel@tonic-gate while ((c = getc(pfp)) != EOF) {
6630Sstevel@tonic-gate if (c != '$')
6640Sstevel@tonic-gate putchar(c);
6650Sstevel@tonic-gate else switch (c = getc(pfp)) {
6660Sstevel@tonic-gate case EOF:
6670Sstevel@tonic-gate goto out;
6680Sstevel@tonic-gate case 'd':
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate * fork off a child with submitter's permissions,
6710Sstevel@tonic-gate * otherwise, when IFS=/, /usr/bin/pwd would be parsed
6720Sstevel@tonic-gate * by the shell as file "bin". The shell would
6730Sstevel@tonic-gate * then search according to the submitter's PATH
6740Sstevel@tonic-gate * and run the file bin with root permission
6750Sstevel@tonic-gate */
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate (void) fflush(stdout);
6780Sstevel@tonic-gate dirbuf[0] = NULL;
6790Sstevel@tonic-gate if (pipe(pfd) != 0)
6800Sstevel@tonic-gate atabort("pipe open failed");
6810Sstevel@tonic-gate realusr = getuid(); /* get realusr before the fork */
6820Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1)
6830Sstevel@tonic-gate atabort("fork failed");
6840Sstevel@tonic-gate if (pid == 0) { /* child process */
6850Sstevel@tonic-gate (void) close(pfd[0]);
6860Sstevel@tonic-gate /* remove setuid for pwd */
6870Sstevel@tonic-gate (void) setuid(realusr);
6880Sstevel@tonic-gate if ((xfp = popen("/usr/bin/pwd", "r"))
6890Sstevel@tonic-gate != NULL) {
6900Sstevel@tonic-gate fscanf(xfp, "%" BUFFMT(PATH_MAX) "s",
6910Sstevel@tonic-gate dirbuf);
6920Sstevel@tonic-gate (void) pclose(xfp);
6930Sstevel@tonic-gate xfp = fdopen(pfd[1], "w");
6940Sstevel@tonic-gate fprintf(xfp, "%s", dirbuf);
6950Sstevel@tonic-gate (void) fclose(xfp);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate _exit(0);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate (void) close(pfd[1]); /* parent process */
7000Sstevel@tonic-gate xfp = fdopen(pfd[0], "r");
7010Sstevel@tonic-gate fscanf(xfp, "%" BUFFMT(PATH_MAX) "s", dirbuf);
7020Sstevel@tonic-gate printf("%s", dirbuf);
7030Sstevel@tonic-gate (void) fclose(xfp);
7040Sstevel@tonic-gate break;
7050Sstevel@tonic-gate case 'm':
7060Sstevel@tonic-gate printf("%o", um);
7070Sstevel@tonic-gate break;
7080Sstevel@tonic-gate case '<':
7090Sstevel@tonic-gate if (ulimit_flag) {
7100Sstevel@tonic-gate if (getrlimit(RLIMIT_FSIZE, &rlp) == 0) {
7110Sstevel@tonic-gate if (rlp.rlim_cur == RLIM_INFINITY)
7120Sstevel@tonic-gate printf("ulimit unlimited\n");
7130Sstevel@tonic-gate else
7140Sstevel@tonic-gate printf("ulimit %lld\n",
715*11115SNobutomo.Nakano@Sun.COM rlp.rlim_cur / 512);
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate * fix for 1113572 - use fputs() so that a
7200Sstevel@tonic-gate * newline isn't appended to the one returned
7210Sstevel@tonic-gate * with fgets(); 1099381 - prompt for input.
7220Sstevel@tonic-gate */
7230Sstevel@tonic-gate while (fgets(line, LINE_MAX, inputfile) != NULL) {
7240Sstevel@tonic-gate fputs(line, stdout);
7250Sstevel@tonic-gate if (ttyinput)
7260Sstevel@tonic-gate fputs("at> ", stderr);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate if (ttyinput) /* clean up the final output */
7290Sstevel@tonic-gate fputs("<EOT>\n", stderr);
7300Sstevel@tonic-gate break;
7310Sstevel@tonic-gate case 't':
7320Sstevel@tonic-gate printf(":%lu", when);
7330Sstevel@tonic-gate break;
7340Sstevel@tonic-gate default:
7350Sstevel@tonic-gate putchar(c);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate out:
7390Sstevel@tonic-gate fclose(pfp);
7400Sstevel@tonic-gate fflush(NULL);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate static int
remove_jobs(int argc,char ** argv,char * login)7440Sstevel@tonic-gate remove_jobs(int argc, char **argv, char *login)
7450Sstevel@tonic-gate /* remove jobs that are specified */
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate int i, r;
7480Sstevel@tonic-gate int error = 0;
7490Sstevel@tonic-gate struct stat buf;
7500Sstevel@tonic-gate struct passwd *pw;
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate pw = getpwuid(user);
7530Sstevel@tonic-gate if (pw == NULL) {
7540Sstevel@tonic-gate atabort("Invalid user.\n");
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate if (argc == 0)
7580Sstevel@tonic-gate usage();
7590Sstevel@tonic-gate if (chdir(ATDIR) == -1)
7600Sstevel@tonic-gate atabort(CANTCD);
7610Sstevel@tonic-gate for (i = 0; i < argc; i++)
7620Sstevel@tonic-gate if (strchr(argv[i], '/') != NULL) {
7630Sstevel@tonic-gate fprintf(stderr, "at: %s: not a valid job-id\n",
7640Sstevel@tonic-gate argv[i]);
7650Sstevel@tonic-gate } else if (stat(argv[i], &buf)) {
7660Sstevel@tonic-gate fprintf(stderr, "at: %s: ", argv[i]);
7670Sstevel@tonic-gate perror("");
7680Sstevel@tonic-gate } else if ((user != buf.st_uid) &&
769*11115SNobutomo.Nakano@Sun.COM (!cron_admin(pw->pw_name))) {
7700Sstevel@tonic-gate fprintf(stderr, "at: you don't own %s\n",
7710Sstevel@tonic-gate argv[i]);
7720Sstevel@tonic-gate error = 1;
7730Sstevel@tonic-gate } else {
774*11115SNobutomo.Nakano@Sun.COM if (cron_admin(pw->pw_name)) {
7750Sstevel@tonic-gate login = getuser((uid_t)buf.st_uid);
7760Sstevel@tonic-gate if (login == NULL) {
7770Sstevel@tonic-gate if (per_errno == 2)
7780Sstevel@tonic-gate atabort(BADSHELL);
7790Sstevel@tonic-gate else
7800Sstevel@tonic-gate atabort(INVALIDUSER);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate cron_sendmsg(DELETE, login, argv[i], AT);
7840Sstevel@tonic-gate r = unlink(argv[i]);
7850Sstevel@tonic-gate audit_at_delete(argv[i], ATDIR, r);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate return (error);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate static int
list_jobs(int argc,char ** argv,int qflag,int queue)7930Sstevel@tonic-gate list_jobs(int argc, char **argv, int qflag, int queue)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate DIR *dir;
7960Sstevel@tonic-gate int i;
7970Sstevel@tonic-gate int error = 0;
7980Sstevel@tonic-gate char *patdir, *atdir, *ptr;
7990Sstevel@tonic-gate char timebuf[80];
8000Sstevel@tonic-gate time_t t;
8010Sstevel@tonic-gate struct stat buf, st1, st2;
8020Sstevel@tonic-gate struct dirent *dentry;
8030Sstevel@tonic-gate struct passwd *pw;
8040Sstevel@tonic-gate unsigned int atdirlen;
8050Sstevel@tonic-gate int r;
8060Sstevel@tonic-gate struct passwd *pwd, pwds;
8070Sstevel@tonic-gate char buf_pwd[1024];
8080Sstevel@tonic-gate char job_file[PATH_MAX];
8090Sstevel@tonic-gate
8100Sstevel@tonic-gate pwd = getpwuid_r(user, &pwds, buf_pwd, sizeof (buf_pwd));
8110Sstevel@tonic-gate if (pwd == NULL) {
8120Sstevel@tonic-gate atabort("Invalid user.\n");
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate /* list jobs for user */
8160Sstevel@tonic-gate if (chdir(ATDIR) == -1)
8170Sstevel@tonic-gate atabort(CANTCD);
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate atdirlen = strlen(ATDIR);
8200Sstevel@tonic-gate atdir = xmalloc(atdirlen + 1);
8210Sstevel@tonic-gate strcpy(atdir, ATDIR);
8220Sstevel@tonic-gate patdir = strrchr(atdir, '/');
8230Sstevel@tonic-gate *patdir = '\0';
8240Sstevel@tonic-gate if (argc == 0) {
8250Sstevel@tonic-gate /* list all jobs for a user */
8260Sstevel@tonic-gate if (stat(ATDIR, &st1) != 0 || stat(atdir, &st2) != 0)
8270Sstevel@tonic-gate atabort("Can not get status of spooling"
8280Sstevel@tonic-gate "directory for at");
8290Sstevel@tonic-gate if ((dir = opendir(ATDIR)) == NULL)
8300Sstevel@tonic-gate atabort(NOOPENDIR);
8310Sstevel@tonic-gate while (1) {
8320Sstevel@tonic-gate if ((dentry = readdir(dir)) == NULL)
8330Sstevel@tonic-gate break;
8340Sstevel@tonic-gate if ((dentry->d_ino == st1.st_ino) ||
8350Sstevel@tonic-gate (dentry->d_ino == st2.st_ino))
8360Sstevel@tonic-gate continue;
8370Sstevel@tonic-gate if ((r = audit_cron_is_anc_name(dentry->d_name)) == 1)
8380Sstevel@tonic-gate continue;
8390Sstevel@tonic-gate if (stat(dentry->d_name, &buf)) {
8400Sstevel@tonic-gate unlink(dentry->d_name);
8410Sstevel@tonic-gate audit_cron_delete_anc_file(dentry->d_name,
842*11115SNobutomo.Nakano@Sun.COM NULL);
8430Sstevel@tonic-gate continue;
8440Sstevel@tonic-gate }
845*11115SNobutomo.Nakano@Sun.COM if ((!cron_admin(pwd->pw_name)) &&
8460Sstevel@tonic-gate (buf.st_uid != user))
8470Sstevel@tonic-gate continue;
8480Sstevel@tonic-gate ptr = dentry->d_name;
8490Sstevel@tonic-gate if (((t = num(&ptr)) == 0) || (*ptr != '.'))
8500Sstevel@tonic-gate continue;
8510Sstevel@tonic-gate strcpy(job_file, patdir);
8520Sstevel@tonic-gate strcat(job_file, dentry->d_name);
8530Sstevel@tonic-gate if (pflag && not_this_project(job_file))
8540Sstevel@tonic-gate continue;
8550Sstevel@tonic-gate ascftime(timebuf, FORMAT, localtime(&t));
856*11115SNobutomo.Nakano@Sun.COM if ((cron_admin(pwd->pw_name)) &&
8570Sstevel@tonic-gate ((pw = getpwuid(buf.st_uid)) != NULL)) {
8580Sstevel@tonic-gate if (!qflag || (qflag &&
8590Sstevel@tonic-gate check_queue(ptr, queue)))
8600Sstevel@tonic-gate printf("user = %s\t%s\t%s\n",
8610Sstevel@tonic-gate pw->pw_name, dentry->d_name,
8620Sstevel@tonic-gate timebuf);
8630Sstevel@tonic-gate } else
8640Sstevel@tonic-gate if (!qflag || (qflag &&
8650Sstevel@tonic-gate check_queue(ptr, queue)))
8660Sstevel@tonic-gate printf("%s\t%s\n",
8670Sstevel@tonic-gate dentry->d_name, timebuf);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate (void) closedir(dir);
8700Sstevel@tonic-gate } else /* list particular jobs for user */
8710Sstevel@tonic-gate for (i = 0; i < argc; i++) {
8720Sstevel@tonic-gate ptr = argv[i];
8730Sstevel@tonic-gate strlcpy(job_file, patdir, PATH_MAX);
8740Sstevel@tonic-gate strlcat(job_file, ptr, PATH_MAX);
8750Sstevel@tonic-gate if (((t = num(&ptr)) == 0) || (*ptr != '.')) {
8760Sstevel@tonic-gate fprintf(stderr, gettext(
8770Sstevel@tonic-gate "at: invalid job name %s\n"), argv[i]);
8780Sstevel@tonic-gate error = 1;
8790Sstevel@tonic-gate } else if (stat(argv[i], &buf)) {
8800Sstevel@tonic-gate fprintf(stderr, "at: %s: ", argv[i]);
8810Sstevel@tonic-gate perror("");
8820Sstevel@tonic-gate error = 1;
8830Sstevel@tonic-gate } else if ((user != buf.st_uid) &&
884*11115SNobutomo.Nakano@Sun.COM (!cron_admin(pwd->pw_name))) {
8850Sstevel@tonic-gate fprintf(stderr, gettext(
8860Sstevel@tonic-gate "at: you don't own %s\n"), argv[i]);
8870Sstevel@tonic-gate error = 1;
8880Sstevel@tonic-gate } else if (pflag && not_this_project(job_file)) {
8890Sstevel@tonic-gate continue;
8900Sstevel@tonic-gate } else {
8910Sstevel@tonic-gate if (!qflag || (qflag &&
8920Sstevel@tonic-gate check_queue(ptr, queue))) {
8930Sstevel@tonic-gate ascftime(timebuf, FORMAT,
8940Sstevel@tonic-gate localtime(&t));
8950Sstevel@tonic-gate printf("%s\t%s\n", argv[i], timebuf);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate return (error);
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate /*
9030Sstevel@tonic-gate * open the command file and read the project id line
9040Sstevel@tonic-gate * compare to the project number provided via -p on the command line
9050Sstevel@tonic-gate * return 0 if they match, 1 if they don't match or an error occurs.
9060Sstevel@tonic-gate */
9070Sstevel@tonic-gate #define SKIPCOUNT 3 /* lines to skip to get to project line in file */
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate static int
not_this_project(char * filename)9100Sstevel@tonic-gate not_this_project(char *filename)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate FILE *fp;
9130Sstevel@tonic-gate projid_t sproj;
9140Sstevel@tonic-gate int i;
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate if ((fp = fopen(filename, "r")) == NULL)
9170Sstevel@tonic-gate return (1);
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate for (i = 0; i < SKIPCOUNT; i++)
9200Sstevel@tonic-gate fscanf(fp, "%*[^\n]\n");
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate fscanf(fp, ": project: %d\n", &sproj);
9230Sstevel@tonic-gate fclose(fp);
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate return (sproj == project ? 0 : 1);
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate static int
check_queue(char * name,int queue)9290Sstevel@tonic-gate check_queue(char *name, int queue)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate if ((name[strlen(name) - 1] - 'a') == queue)
9320Sstevel@tonic-gate return (1);
9330Sstevel@tonic-gate else
9340Sstevel@tonic-gate return (0);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate static time_t
parse_time(char * t)9380Sstevel@tonic-gate parse_time(char *t)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate int century = 0;
9410Sstevel@tonic-gate int seconds = 0;
9420Sstevel@tonic-gate char *p;
9430Sstevel@tonic-gate time_t when = 0;
9440Sstevel@tonic-gate struct tm tm;
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate /*
9470Sstevel@tonic-gate * time in the following format (defined by the touch(1) spec):
9480Sstevel@tonic-gate * [[CC]YY]MMDDhhmm[.SS]
9490Sstevel@tonic-gate */
9500Sstevel@tonic-gate if ((p = strchr(t, '.')) != NULL) {
9510Sstevel@tonic-gate if (strchr(p+1, '.') != NULL)
9520Sstevel@tonic-gate atabort(BADTIME);
9530Sstevel@tonic-gate seconds = atoi_for2(p+1);
9540Sstevel@tonic-gate *p = '\0';
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate memset(&tm, 0, sizeof (struct tm));
9580Sstevel@tonic-gate when = time(0);
9590Sstevel@tonic-gate tm.tm_year = localtime(&when)->tm_year;
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate switch (strlen(t)) {
9620Sstevel@tonic-gate case 12: /* CCYYMMDDhhmm */
9630Sstevel@tonic-gate century = atoi_for2(t);
9640Sstevel@tonic-gate t += 2;
9650Sstevel@tonic-gate case 10: /* YYMMDDhhmm */
9660Sstevel@tonic-gate tm.tm_year = atoi_for2(t);
9670Sstevel@tonic-gate t += 2;
9680Sstevel@tonic-gate if (century == 0) {
9690Sstevel@tonic-gate if (tm.tm_year < 69)
9700Sstevel@tonic-gate tm.tm_year += 100;
9710Sstevel@tonic-gate } else
9720Sstevel@tonic-gate tm.tm_year += (century - 19) * 100;
9730Sstevel@tonic-gate case 8: /* MMDDhhmm */
9740Sstevel@tonic-gate tm.tm_mon = atoi_for2(t) - 1;
9750Sstevel@tonic-gate t += 2;
9760Sstevel@tonic-gate tm.tm_mday = atoi_for2(t);
9770Sstevel@tonic-gate t += 2;
9780Sstevel@tonic-gate tm.tm_hour = atoi_for2(t);
9790Sstevel@tonic-gate t += 2;
9800Sstevel@tonic-gate tm.tm_min = atoi_for2(t);
9810Sstevel@tonic-gate t += 2;
9820Sstevel@tonic-gate tm.tm_sec = seconds;
9830Sstevel@tonic-gate break;
9840Sstevel@tonic-gate default:
9850Sstevel@tonic-gate atabort(BADTIME);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate if ((when = mktime(&tm)) == -1)
9890Sstevel@tonic-gate atabort(BADTIME);
9900Sstevel@tonic-gate if (tm.tm_isdst)
9910Sstevel@tonic-gate when -= (timezone-altzone);
9920Sstevel@tonic-gate return (when);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate static int
atoi_for2(char * p)9960Sstevel@tonic-gate atoi_for2(char *p) {
9970Sstevel@tonic-gate int value;
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate value = (*p - '0') * 10 + *(p+1) - '0';
10000Sstevel@tonic-gate if ((value < 0) || (value > 99))
10010Sstevel@tonic-gate atabort(BADTIME);
10020Sstevel@tonic-gate return (value);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate static void
usage(void)10060Sstevel@tonic-gate usage(void)
10070Sstevel@tonic-gate {
10080Sstevel@tonic-gate fprintf(stderr, USAGE);
10090Sstevel@tonic-gate exit(1);
10100Sstevel@tonic-gate }
1011