1*12527Sralph /*	rmjob.c	4.3	83/05/18	*/
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)
4112433Sralph 		RP = DEFLP;
42*12527Sralph 	RM = pgetstr("rm", &bp);
4312112Sralph 
4412112Sralph 	/*
4512112Sralph 	 * If the format was `lprm -' and the user isn't the super-user,
4612112Sralph 	 *  then fake things to look like he said `lprm user'.
4712112Sralph 	 */
4812112Sralph 	if (users < 0) {
4912112Sralph 		if (getuid() == 0)
5012112Sralph 			all = 1;	/* all files in local queue */
5112112Sralph 		else {
5212112Sralph 			user[0] = person;
5312112Sralph 			users = 1;
5412112Sralph 		}
5512112Sralph 	}
5612112Sralph 	if (!strcmp(person, "-all")) {
5712112Sralph 		if (from == host)
5812112Sralph 			fatal("The login name \"-all\" is reserved");
5912112Sralph 		all = 1;	/* all those from 'from' */
6012112Sralph 		person = root;
6112112Sralph 	}
6212112Sralph 
6312112Sralph 	if (chdir(SD) < 0)
6412112Sralph 		fatal("cannot chdir to spool directory");
6512112Sralph 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
6612112Sralph 		fatal("cannot access spool directory");
6712112Sralph 
6812112Sralph 	if (nitems) {
6912112Sralph 		/*
7012112Sralph 		 * Check for an active printer daemon (in which case we
7112112Sralph 		 *  kill it if it is reading our file) then remove stuff
7212112Sralph 		 *  (after which we have to restart the daemon).
7312112Sralph 		 */
7412112Sralph 		if (lockchk(LO) && chk(current)) {
7512112Sralph 			assasinated = kill(cur_daemon, SIGINT) == 0;
7612112Sralph 			if (!assasinated)
7712112Sralph 				fatal("cannot kill printer daemon");
7812112Sralph 		}
7912112Sralph 		/*
8012112Sralph 		 * process the files
8112112Sralph 		 */
8212112Sralph 		for (i = 0; i < nitems; i++)
8312112Sralph 			process(files[i]->d_name);
8412112Sralph 	}
8512112Sralph 	chkremote();
8612112Sralph 	/*
8712112Sralph 	 * Restart the printer daemon if it was killed
8812112Sralph 	 */
8912112Sralph 	if (assasinated && !startdaemon())
9012112Sralph 		fatal("cannot restart printer daemon\n");
9112112Sralph 	exit(0);
9212112Sralph }
9312112Sralph 
9412112Sralph /*
9512112Sralph  * Process a lock file: collect the pid of the active
9612112Sralph  *  daemon and the file name of the active spool entry.
9712112Sralph  * Return boolean indicating existence of a lock file.
9812112Sralph  */
9912112Sralph lockchk(s)
10012112Sralph 	char *s;
10112112Sralph {
10212112Sralph 	register FILE *fp;
10312112Sralph 	register int i, n;
10412112Sralph 
10512112Sralph 	if ((fp = fopen(s, "r")) == NULL)
10612112Sralph 		if (errno == EACCES)
10712112Sralph 			fatal("can't access lock file");
10812112Sralph 		else
10912112Sralph 			return(0);
11012112Sralph 	if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
11112112Sralph 		(void) fclose(fp);
11212112Sralph 		return(0);		/* no daemon present */
11312112Sralph 	}
11412112Sralph 	cur_daemon = atoi(line);
11512112Sralph 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
116*12527Sralph 		if (i > 5) {
11712112Sralph 			n = 1;
11812112Sralph 			break;
11912112Sralph 		}
12012112Sralph 		sleep(i);
12112112Sralph 	}
12212112Sralph 	current[n-1] = '\0';
12312112Sralph 	(void) fclose(fp);
12412112Sralph 	return(1);
12512112Sralph }
12612112Sralph 
12712112Sralph /*
12812112Sralph  * Process a control file.
12912112Sralph  */
13012112Sralph process(file)
13112112Sralph 	char *file;
13212112Sralph {
13312112Sralph 	FILE *cfp;
13412112Sralph 
13512112Sralph 	if (!chk(file))
13612112Sralph 		return;
13712112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
13812112Sralph 		fatal("cannot open %s", file);
13912112Sralph 	while (getline()) {
14012112Sralph 		switch (line[0]) {
14112112Sralph 		case 'U':  /* unlink associated files */
14212112Sralph 			if (from != host)
14312112Sralph 				printf("%s: ", host);
14412112Sralph 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
14512112Sralph 				"%s dequeued\n", line+1);
14612112Sralph 		}
14712112Sralph 	}
14812112Sralph 	(void) fclose(cfp);
14912112Sralph 	if (from != host)
15012112Sralph 		printf("%s: ", host);
15112112Sralph 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
15212112Sralph }
15312112Sralph 
15412112Sralph /*
15512112Sralph  * Do the dirty work in checking
15612112Sralph  */
15712112Sralph chk(file)
15812112Sralph 	char *file;
15912112Sralph {
16012112Sralph 	register int *r, n;
16112112Sralph 	register char **u, *cp;
16212112Sralph 	FILE *cfp;
16312112Sralph 
16412112Sralph 	if (all && (from == host || !strcmp(from, file+6)))
16512112Sralph 		return(1);
16612112Sralph 
16712112Sralph 	/*
16812112Sralph 	 * get the owner's name from the control file.
16912112Sralph 	 */
17012112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
17112112Sralph 		return(0);
17212112Sralph 	while (getline(cfp)) {
17312112Sralph 		if (line[0] == 'P')
17412112Sralph 			break;
17512112Sralph 	}
17612112Sralph 	(void) fclose(cfp);
17712112Sralph 	if (line[0] != 'P')
17812112Sralph 		return(0);
17912112Sralph 
18012112Sralph 	if (users == 0 && requests == 0)
18112112Sralph 		return(!strcmp(file, current) && isowner(line+1, file));
18212112Sralph 	/*
18312112Sralph 	 * Check the request list
18412112Sralph 	 */
18512112Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
18612112Sralph 		n = n * 10 + (*cp++ - '0');
18712112Sralph 	for (r = requ; r < &requ[requests]; r++)
18812112Sralph 		if (*r == n && isowner(line+1, file))
18912112Sralph 			return(1);
19012112Sralph 	/*
19112112Sralph 	 * Check to see if it's in the user list
19212112Sralph 	 */
19312112Sralph 	for (u = user; u < &user[users]; u++)
19412112Sralph 		if (!strcmp(*u, line+1) && isowner(line+1, file))
19512112Sralph 			return(1);
19612112Sralph 	return(0);
19712112Sralph }
19812112Sralph 
19912112Sralph /*
20012112Sralph  * If root is removing a file on the local machine, allow it.
20112433Sralph  * If root is removing a file from a remote machine, only allow
20212433Sralph  * files sent from the remote machine to be removed.
20312112Sralph  * Normal users can only remove the file from where it was sent.
20412112Sralph  */
20512112Sralph isowner(owner, file)
20612112Sralph 	char *owner, *file;
20712112Sralph {
20812433Sralph 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
20912433Sralph 		return(1);
21012433Sralph 	if (!strcmp(person, owner) && !strcmp(from, file+6))
21112433Sralph 		return(1);
21212433Sralph 	if (from != host)
21312433Sralph 		printf("%s: ", host);
21412433Sralph 	printf("%s: Permission denied\n", file);
21512433Sralph 	return(0);
21612112Sralph }
21712112Sralph 
21812112Sralph /*
21912112Sralph  * Check to see if we are sending files to a remote machine. If we are,
22012112Sralph  * then try removing files on the remote machine.
22112112Sralph  */
22212112Sralph chkremote()
22312112Sralph {
22412112Sralph 	register char *cp;
22512112Sralph 	register int i, rem;
22612112Sralph 	char buf[BUFSIZ];
22712112Sralph 
22812112Sralph 	if (*LP || RM == NULL)
22912112Sralph 		return;	/* not sending to a remote machine */
23012112Sralph 
23112433Sralph 	/*
23212433Sralph 	 * Flush stdout so the user can see what has been deleted
23312433Sralph 	 * while we wait (possibly) for the connection.
23412433Sralph 	 */
23512433Sralph 	fflush(stdout);
23612433Sralph 
23712112Sralph 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
23812112Sralph 	cp = buf;
23912112Sralph 	for (i = 0; i < users; i++) {
24012112Sralph 		cp += strlen(cp);
24112112Sralph 		*cp++ = ' ';
24212112Sralph 		strcpy(cp, user[i]);
24312112Sralph 	}
24412112Sralph 	for (i = 0; i < requests; i++) {
24512112Sralph 		cp += strlen(cp);
24612112Sralph 		(void) sprintf(cp, " %d", requ[i]);
24712112Sralph 	}
24812112Sralph 	strcat(cp, "\n");
249*12527Sralph 	rem = getport(RM);
25012112Sralph 	if (rem < 0) {
25112112Sralph 		if (from != host)
25212112Sralph 			printf("%s: ", host);
25312112Sralph 		printf("connection to %s is down\n", RM);
25412112Sralph 	} else {
25512112Sralph 		i = strlen(buf);
25612112Sralph 		if (write(rem, buf, i) != i)
25712112Sralph 			fatal("Lost connection");
25812112Sralph 		while ((i = read(rem, buf, sizeof(buf))) > 0)
25912112Sralph 			(void) fwrite(buf, 1, i, stdout);
26012112Sralph 		(void) close(rem);
26112112Sralph 	}
26212112Sralph }
26312112Sralph 
26412112Sralph /*
26512112Sralph  * Return 1 if the filename begins with 'cf'
26612112Sralph  */
26712112Sralph iscf(d)
26812112Sralph 	struct direct *d;
26912112Sralph {
27012112Sralph 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
27112112Sralph }
272