1*12433Sralph /*	rmjob.c	4.2	83/05/13	*/
212112Sralph /*
312112Sralph  * rmjob - remove the specified jobs from the queue.
412112Sralph  */
512112Sralph 
612112Sralph #include "lp.h"
712112Sralph 
812112Sralph /*
912112Sralph  * Stuff for handling lprm specifications
1012112Sralph  */
1112112Sralph extern char	*user[];		/* users to process */
1212112Sralph extern int	users;			/* # of users in user array */
1312112Sralph extern int	requ[];			/* job number of spool entries */
1412112Sralph extern int	requests;		/* # of spool requests */
1512112Sralph 
1612112Sralph char	*person;			/* name of person doing lprm */
1712112Sralph char	root[] = "root";
1812112Sralph int	all = 0;			/* eliminate all files (root only) */
1912112Sralph int	cur_daemon;			/* daemon's pid */
2012112Sralph char	current[40];			/* active control file name */
2112112Sralph 
2212112Sralph int	iscf();
2312112Sralph 
2412112Sralph rmjob()
2512112Sralph {
2612112Sralph 	register int i, nitems;
2712112Sralph 	int assasinated = 0;
2812112Sralph 	struct direct **files;
2912112Sralph 
3012112Sralph 	if ((i = pgetent(line, printer)) < 0)
3112112Sralph 		fatal("cannot open printer description file");
3212112Sralph 	else if (i == 0)
3312112Sralph 		fatal("unknown printer");
3412112Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
3512112Sralph 		SD = DEFSPOOL;
3612112Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
3712112Sralph 		LO = DEFLOCK;
3812112Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
3912112Sralph 		LP = DEFDEVLP;
4012112Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
41*12433Sralph 		RP = DEFLP;
4212112Sralph 	if ((RM = pgetstr("rm", &bp)) == NULL)
4312112Sralph 		RM = host;
4412112Sralph 
4512112Sralph 	/*
4612112Sralph 	 * If the format was `lprm -' and the user isn't the super-user,
4712112Sralph 	 *  then fake things to look like he said `lprm user'.
4812112Sralph 	 */
4912112Sralph 	if (users < 0) {
5012112Sralph 		if (getuid() == 0)
5112112Sralph 			all = 1;	/* all files in local queue */
5212112Sralph 		else {
5312112Sralph 			user[0] = person;
5412112Sralph 			users = 1;
5512112Sralph 		}
5612112Sralph 	}
5712112Sralph 	if (!strcmp(person, "-all")) {
5812112Sralph 		if (from == host)
5912112Sralph 			fatal("The login name \"-all\" is reserved");
6012112Sralph 		all = 1;	/* all those from 'from' */
6112112Sralph 		person = root;
6212112Sralph 	}
6312112Sralph 
6412112Sralph 	if (chdir(SD) < 0)
6512112Sralph 		fatal("cannot chdir to spool directory");
6612112Sralph 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
6712112Sralph 		fatal("cannot access spool directory");
6812112Sralph 
6912112Sralph 	if (nitems) {
7012112Sralph 		/*
7112112Sralph 		 * Check for an active printer daemon (in which case we
7212112Sralph 		 *  kill it if it is reading our file) then remove stuff
7312112Sralph 		 *  (after which we have to restart the daemon).
7412112Sralph 		 */
7512112Sralph 		if (lockchk(LO) && chk(current)) {
7612112Sralph 			assasinated = kill(cur_daemon, SIGINT) == 0;
7712112Sralph 			if (!assasinated)
7812112Sralph 				fatal("cannot kill printer daemon");
7912112Sralph 		}
8012112Sralph 		/*
8112112Sralph 		 * process the files
8212112Sralph 		 */
8312112Sralph 		for (i = 0; i < nitems; i++)
8412112Sralph 			process(files[i]->d_name);
8512112Sralph 	}
8612112Sralph 	chkremote();
8712112Sralph 	/*
8812112Sralph 	 * Restart the printer daemon if it was killed
8912112Sralph 	 */
9012112Sralph 	if (assasinated && !startdaemon())
9112112Sralph 		fatal("cannot restart printer daemon\n");
9212112Sralph 	exit(0);
9312112Sralph }
9412112Sralph 
9512112Sralph /*
9612112Sralph  * Process a lock file: collect the pid of the active
9712112Sralph  *  daemon and the file name of the active spool entry.
9812112Sralph  * Return boolean indicating existence of a lock file.
9912112Sralph  */
10012112Sralph lockchk(s)
10112112Sralph 	char *s;
10212112Sralph {
10312112Sralph 	register FILE *fp;
10412112Sralph 	register int i, n;
10512112Sralph 
10612112Sralph 	if ((fp = fopen(s, "r")) == NULL)
10712112Sralph 		if (errno == EACCES)
10812112Sralph 			fatal("can't access lock file");
10912112Sralph 		else
11012112Sralph 			return(0);
11112112Sralph 	if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
11212112Sralph 		(void) fclose(fp);
11312112Sralph 		return(0);		/* no daemon present */
11412112Sralph 	}
11512112Sralph 	cur_daemon = atoi(line);
11612112Sralph 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
11712112Sralph 		if (i > 20) {
11812112Sralph 			n = 1;
11912112Sralph 			break;
12012112Sralph 		}
12112112Sralph 		sleep(i);
12212112Sralph 	}
12312112Sralph 	current[n-1] = '\0';
12412112Sralph 	(void) fclose(fp);
12512112Sralph 	return(1);
12612112Sralph }
12712112Sralph 
12812112Sralph /*
12912112Sralph  * Process a control file.
13012112Sralph  */
13112112Sralph process(file)
13212112Sralph 	char *file;
13312112Sralph {
13412112Sralph 	FILE *cfp;
13512112Sralph 
13612112Sralph 	if (!chk(file))
13712112Sralph 		return;
13812112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
13912112Sralph 		fatal("cannot open %s", file);
14012112Sralph 	while (getline()) {
14112112Sralph 		switch (line[0]) {
14212112Sralph 		case 'U':  /* unlink associated files */
14312112Sralph 			if (from != host)
14412112Sralph 				printf("%s: ", host);
14512112Sralph 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
14612112Sralph 				"%s dequeued\n", line+1);
14712112Sralph 		}
14812112Sralph 	}
14912112Sralph 	(void) fclose(cfp);
15012112Sralph 	if (from != host)
15112112Sralph 		printf("%s: ", host);
15212112Sralph 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
15312112Sralph }
15412112Sralph 
15512112Sralph /*
15612112Sralph  * Do the dirty work in checking
15712112Sralph  */
15812112Sralph chk(file)
15912112Sralph 	char *file;
16012112Sralph {
16112112Sralph 	register int *r, n;
16212112Sralph 	register char **u, *cp;
16312112Sralph 	FILE *cfp;
16412112Sralph 
16512112Sralph 	if (all && (from == host || !strcmp(from, file+6)))
16612112Sralph 		return(1);
16712112Sralph 
16812112Sralph 	/*
16912112Sralph 	 * get the owner's name from the control file.
17012112Sralph 	 */
17112112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
17212112Sralph 		return(0);
17312112Sralph 	while (getline(cfp)) {
17412112Sralph 		if (line[0] == 'P')
17512112Sralph 			break;
17612112Sralph 	}
17712112Sralph 	(void) fclose(cfp);
17812112Sralph 	if (line[0] != 'P')
17912112Sralph 		return(0);
18012112Sralph 
18112112Sralph 	if (users == 0 && requests == 0)
18212112Sralph 		return(!strcmp(file, current) && isowner(line+1, file));
18312112Sralph 	/*
18412112Sralph 	 * Check the request list
18512112Sralph 	 */
18612112Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
18712112Sralph 		n = n * 10 + (*cp++ - '0');
18812112Sralph 	for (r = requ; r < &requ[requests]; r++)
18912112Sralph 		if (*r == n && isowner(line+1, file))
19012112Sralph 			return(1);
19112112Sralph 	/*
19212112Sralph 	 * Check to see if it's in the user list
19312112Sralph 	 */
19412112Sralph 	for (u = user; u < &user[users]; u++)
19512112Sralph 		if (!strcmp(*u, line+1) && isowner(line+1, file))
19612112Sralph 			return(1);
19712112Sralph 	return(0);
19812112Sralph }
19912112Sralph 
20012112Sralph /*
20112112Sralph  * If root is removing a file on the local machine, allow it.
202*12433Sralph  * If root is removing a file from a remote machine, only allow
203*12433Sralph  * files sent from the remote machine to be removed.
20412112Sralph  * Normal users can only remove the file from where it was sent.
20512112Sralph  */
20612112Sralph isowner(owner, file)
20712112Sralph 	char *owner, *file;
20812112Sralph {
209*12433Sralph 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
210*12433Sralph 		return(1);
211*12433Sralph 	if (!strcmp(person, owner) && !strcmp(from, file+6))
212*12433Sralph 		return(1);
213*12433Sralph 	if (from != host)
214*12433Sralph 		printf("%s: ", host);
215*12433Sralph 	printf("%s: Permission denied\n", file);
216*12433Sralph 	return(0);
21712112Sralph }
21812112Sralph 
21912112Sralph /*
22012112Sralph  * Check to see if we are sending files to a remote machine. If we are,
22112112Sralph  * then try removing files on the remote machine.
22212112Sralph  */
22312112Sralph chkremote()
22412112Sralph {
22512112Sralph 	register char *cp;
22612112Sralph 	register int i, rem;
22712112Sralph 	char buf[BUFSIZ];
22812112Sralph 
22912112Sralph 	if (*LP || RM == NULL)
23012112Sralph 		return;	/* not sending to a remote machine */
23112112Sralph 
232*12433Sralph 	/*
233*12433Sralph 	 * Flush stdout so the user can see what has been deleted
234*12433Sralph 	 * while we wait (possibly) for the connection.
235*12433Sralph 	 */
236*12433Sralph 	fflush(stdout);
237*12433Sralph 
23812112Sralph 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
23912112Sralph 	cp = buf;
24012112Sralph 	for (i = 0; i < users; i++) {
24112112Sralph 		cp += strlen(cp);
24212112Sralph 		*cp++ = ' ';
24312112Sralph 		strcpy(cp, user[i]);
24412112Sralph 	}
24512112Sralph 	for (i = 0; i < requests; i++) {
24612112Sralph 		cp += strlen(cp);
24712112Sralph 		(void) sprintf(cp, " %d", requ[i]);
24812112Sralph 	}
24912112Sralph 	strcat(cp, "\n");
25012112Sralph 	rem = getport();
25112112Sralph 	if (rem < 0) {
25212112Sralph 		if (from != host)
25312112Sralph 			printf("%s: ", host);
25412112Sralph 		printf("connection to %s is down\n", RM);
25512112Sralph 	} else {
25612112Sralph 		i = strlen(buf);
25712112Sralph 		if (write(rem, buf, i) != i)
25812112Sralph 			fatal("Lost connection");
25912112Sralph 		while ((i = read(rem, buf, sizeof(buf))) > 0)
26012112Sralph 			(void) fwrite(buf, 1, i, stdout);
26112112Sralph 		(void) close(rem);
26212112Sralph 	}
26312112Sralph }
26412112Sralph 
26512112Sralph /*
26612112Sralph  * Return 1 if the filename begins with 'cf'
26712112Sralph  */
26812112Sralph iscf(d)
26912112Sralph 	struct direct *d;
27012112Sralph {
27112112Sralph 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
27212112Sralph }
273