122439Sdist /*
222439Sdist  * Copyright (c) 1983 Regents of the University of California.
3*34203Sbostic  * All rights reserved.
4*34203Sbostic  *
5*34203Sbostic  * Redistribution and use in source and binary forms are permitted
6*34203Sbostic  * provided that this notice is preserved and that due credit is given
7*34203Sbostic  * to the University of California at Berkeley. The name of the University
8*34203Sbostic  * may not be used to endorse or promote products derived from this
9*34203Sbostic  * software without specific prior written permission. This software
10*34203Sbostic  * is provided ``as is'' without express or implied warranty.
1122439Sdist  */
1222439Sdist 
1313955Ssam #ifndef lint
14*34203Sbostic static char sccsid[] = "@(#)rmjob.c	5.2 (Berkeley) 05/05/88";
15*34203Sbostic #endif /* not lint */
1613955Ssam 
1712112Sralph /*
1812112Sralph  * rmjob - remove the specified jobs from the queue.
1912112Sralph  */
2012112Sralph 
2112112Sralph #include "lp.h"
2212112Sralph 
2312112Sralph /*
2412112Sralph  * Stuff for handling lprm specifications
2512112Sralph  */
2612112Sralph extern char	*user[];		/* users to process */
2712112Sralph extern int	users;			/* # of users in user array */
2812112Sralph extern int	requ[];			/* job number of spool entries */
2912112Sralph extern int	requests;		/* # of spool requests */
3012877Sralph extern char	*person;		/* name of person doing lprm */
3112112Sralph 
3216759Sralph char	root[] = "root";
3316759Sralph int	all = 0;		/* eliminate all files (root only) */
3416759Sralph int	cur_daemon;		/* daemon's pid */
3516759Sralph char	current[40];		/* active control file name */
3612112Sralph 
3712112Sralph int	iscf();
3812112Sralph 
3912112Sralph rmjob()
4012112Sralph {
4112112Sralph 	register int i, nitems;
4212112Sralph 	int assasinated = 0;
4312112Sralph 	struct direct **files;
4412112Sralph 
4512112Sralph 	if ((i = pgetent(line, printer)) < 0)
4612112Sralph 		fatal("cannot open printer description file");
4712112Sralph 	else if (i == 0)
4812112Sralph 		fatal("unknown printer");
4912112Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
5012112Sralph 		SD = DEFSPOOL;
5112112Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
5212112Sralph 		LO = DEFLOCK;
5312112Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
5412112Sralph 		LP = DEFDEVLP;
5512112Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
5612433Sralph 		RP = DEFLP;
5712527Sralph 	RM = pgetstr("rm", &bp);
5812112Sralph 
5912112Sralph 	/*
6012112Sralph 	 * If the format was `lprm -' and the user isn't the super-user,
6112112Sralph 	 *  then fake things to look like he said `lprm user'.
6212112Sralph 	 */
6312112Sralph 	if (users < 0) {
6412112Sralph 		if (getuid() == 0)
6512112Sralph 			all = 1;	/* all files in local queue */
6612112Sralph 		else {
6712112Sralph 			user[0] = person;
6812112Sralph 			users = 1;
6912112Sralph 		}
7012112Sralph 	}
7112112Sralph 	if (!strcmp(person, "-all")) {
7212112Sralph 		if (from == host)
7312112Sralph 			fatal("The login name \"-all\" is reserved");
7412112Sralph 		all = 1;	/* all those from 'from' */
7512112Sralph 		person = root;
7612112Sralph 	}
7712112Sralph 
7812112Sralph 	if (chdir(SD) < 0)
7912112Sralph 		fatal("cannot chdir to spool directory");
8012112Sralph 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
8112112Sralph 		fatal("cannot access spool directory");
8212112Sralph 
8312112Sralph 	if (nitems) {
8412112Sralph 		/*
8512112Sralph 		 * Check for an active printer daemon (in which case we
8612112Sralph 		 *  kill it if it is reading our file) then remove stuff
8712112Sralph 		 *  (after which we have to restart the daemon).
8812112Sralph 		 */
8912112Sralph 		if (lockchk(LO) && chk(current)) {
9012112Sralph 			assasinated = kill(cur_daemon, SIGINT) == 0;
9112112Sralph 			if (!assasinated)
9212112Sralph 				fatal("cannot kill printer daemon");
9312112Sralph 		}
9412112Sralph 		/*
9512112Sralph 		 * process the files
9612112Sralph 		 */
9712112Sralph 		for (i = 0; i < nitems; i++)
9812112Sralph 			process(files[i]->d_name);
9912112Sralph 	}
10012112Sralph 	chkremote();
10112112Sralph 	/*
10212112Sralph 	 * Restart the printer daemon if it was killed
10312112Sralph 	 */
10415828Sralph 	if (assasinated && !startdaemon(printer))
10512112Sralph 		fatal("cannot restart printer daemon\n");
10612112Sralph 	exit(0);
10712112Sralph }
10812112Sralph 
10912112Sralph /*
11012112Sralph  * Process a lock file: collect the pid of the active
11112112Sralph  *  daemon and the file name of the active spool entry.
11212112Sralph  * Return boolean indicating existence of a lock file.
11312112Sralph  */
11412112Sralph lockchk(s)
11512112Sralph 	char *s;
11612112Sralph {
11712112Sralph 	register FILE *fp;
11812112Sralph 	register int i, n;
11912112Sralph 
12012112Sralph 	if ((fp = fopen(s, "r")) == NULL)
12112112Sralph 		if (errno == EACCES)
12212112Sralph 			fatal("can't access lock file");
12312112Sralph 		else
12412112Sralph 			return(0);
12513443Sralph 	if (!getline(fp)) {
12612112Sralph 		(void) fclose(fp);
12712112Sralph 		return(0);		/* no daemon present */
12812112Sralph 	}
12912112Sralph 	cur_daemon = atoi(line);
13013443Sralph 	if (kill(cur_daemon, 0) < 0) {
13113443Sralph 		(void) fclose(fp);
13213443Sralph 		return(0);		/* no daemon present */
13313443Sralph 	}
13412112Sralph 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
13512527Sralph 		if (i > 5) {
13612112Sralph 			n = 1;
13712112Sralph 			break;
13812112Sralph 		}
13912112Sralph 		sleep(i);
14012112Sralph 	}
14112112Sralph 	current[n-1] = '\0';
14212112Sralph 	(void) fclose(fp);
14312112Sralph 	return(1);
14412112Sralph }
14512112Sralph 
14612112Sralph /*
14712112Sralph  * Process a control file.
14812112Sralph  */
14912112Sralph process(file)
15012112Sralph 	char *file;
15112112Sralph {
15212112Sralph 	FILE *cfp;
15312112Sralph 
15412112Sralph 	if (!chk(file))
15512112Sralph 		return;
15612112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
15712112Sralph 		fatal("cannot open %s", file);
15816759Sralph 	while (getline(cfp)) {
15912112Sralph 		switch (line[0]) {
16012112Sralph 		case 'U':  /* unlink associated files */
16112112Sralph 			if (from != host)
16212112Sralph 				printf("%s: ", host);
16312112Sralph 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
16412112Sralph 				"%s dequeued\n", line+1);
16512112Sralph 		}
16612112Sralph 	}
16712112Sralph 	(void) fclose(cfp);
16812112Sralph 	if (from != host)
16912112Sralph 		printf("%s: ", host);
17012112Sralph 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
17112112Sralph }
17212112Sralph 
17312112Sralph /*
17412112Sralph  * Do the dirty work in checking
17512112Sralph  */
17612112Sralph chk(file)
17712112Sralph 	char *file;
17812112Sralph {
17912112Sralph 	register int *r, n;
18012112Sralph 	register char **u, *cp;
18112112Sralph 	FILE *cfp;
18212112Sralph 
18315543Sralph 	/*
18415543Sralph 	 * Check for valid cf file name (mostly checking current).
18515543Sralph 	 */
18615543Sralph 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
18715543Sralph 		return(0);
18815543Sralph 
18912112Sralph 	if (all && (from == host || !strcmp(from, file+6)))
19012112Sralph 		return(1);
19112112Sralph 
19212112Sralph 	/*
19312112Sralph 	 * get the owner's name from the control file.
19412112Sralph 	 */
19512112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
19612112Sralph 		return(0);
19712112Sralph 	while (getline(cfp)) {
19812112Sralph 		if (line[0] == 'P')
19912112Sralph 			break;
20012112Sralph 	}
20112112Sralph 	(void) fclose(cfp);
20212112Sralph 	if (line[0] != 'P')
20312112Sralph 		return(0);
20412112Sralph 
20512112Sralph 	if (users == 0 && requests == 0)
20612112Sralph 		return(!strcmp(file, current) && isowner(line+1, file));
20712112Sralph 	/*
20812112Sralph 	 * Check the request list
20912112Sralph 	 */
21012112Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
21112112Sralph 		n = n * 10 + (*cp++ - '0');
21212112Sralph 	for (r = requ; r < &requ[requests]; r++)
21312112Sralph 		if (*r == n && isowner(line+1, file))
21412112Sralph 			return(1);
21512112Sralph 	/*
21612112Sralph 	 * Check to see if it's in the user list
21712112Sralph 	 */
21812112Sralph 	for (u = user; u < &user[users]; u++)
21912112Sralph 		if (!strcmp(*u, line+1) && isowner(line+1, file))
22012112Sralph 			return(1);
22112112Sralph 	return(0);
22212112Sralph }
22312112Sralph 
22412112Sralph /*
22512112Sralph  * If root is removing a file on the local machine, allow it.
22612433Sralph  * If root is removing a file from a remote machine, only allow
22712433Sralph  * files sent from the remote machine to be removed.
22812112Sralph  * Normal users can only remove the file from where it was sent.
22912112Sralph  */
23012112Sralph isowner(owner, file)
23112112Sralph 	char *owner, *file;
23212112Sralph {
23312433Sralph 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
23412433Sralph 		return(1);
23512433Sralph 	if (!strcmp(person, owner) && !strcmp(from, file+6))
23612433Sralph 		return(1);
23712433Sralph 	if (from != host)
23812433Sralph 		printf("%s: ", host);
23912433Sralph 	printf("%s: Permission denied\n", file);
24012433Sralph 	return(0);
24112112Sralph }
24212112Sralph 
24312112Sralph /*
24412112Sralph  * Check to see if we are sending files to a remote machine. If we are,
24512112Sralph  * then try removing files on the remote machine.
24612112Sralph  */
24712112Sralph chkremote()
24812112Sralph {
24912112Sralph 	register char *cp;
25012112Sralph 	register int i, rem;
25112112Sralph 	char buf[BUFSIZ];
25212112Sralph 
25312112Sralph 	if (*LP || RM == NULL)
25412112Sralph 		return;	/* not sending to a remote machine */
25512112Sralph 
25612433Sralph 	/*
25712433Sralph 	 * Flush stdout so the user can see what has been deleted
25812433Sralph 	 * while we wait (possibly) for the connection.
25912433Sralph 	 */
26012433Sralph 	fflush(stdout);
26112433Sralph 
26212112Sralph 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
26312112Sralph 	cp = buf;
26412112Sralph 	for (i = 0; i < users; i++) {
26512112Sralph 		cp += strlen(cp);
26612112Sralph 		*cp++ = ' ';
26712112Sralph 		strcpy(cp, user[i]);
26812112Sralph 	}
26912112Sralph 	for (i = 0; i < requests; i++) {
27012112Sralph 		cp += strlen(cp);
27112112Sralph 		(void) sprintf(cp, " %d", requ[i]);
27212112Sralph 	}
27312112Sralph 	strcat(cp, "\n");
27412527Sralph 	rem = getport(RM);
27512112Sralph 	if (rem < 0) {
27612112Sralph 		if (from != host)
27712112Sralph 			printf("%s: ", host);
27812112Sralph 		printf("connection to %s is down\n", RM);
27912112Sralph 	} else {
28012112Sralph 		i = strlen(buf);
28112112Sralph 		if (write(rem, buf, i) != i)
28212112Sralph 			fatal("Lost connection");
28312112Sralph 		while ((i = read(rem, buf, sizeof(buf))) > 0)
28412112Sralph 			(void) fwrite(buf, 1, i, stdout);
28512112Sralph 		(void) close(rem);
28612112Sralph 	}
28712112Sralph }
28812112Sralph 
28912112Sralph /*
29012112Sralph  * Return 1 if the filename begins with 'cf'
29112112Sralph  */
29212112Sralph iscf(d)
29312112Sralph 	struct direct *d;
29412112Sralph {
29512112Sralph 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
29612112Sralph }
297