113955Ssam #ifndef lint
2*15543Sralph static char sccsid[] = "@(#)rmjob.c	4.8 (Berkeley) 11/15/83";
313955Ssam #endif
413955Ssam 
512112Sralph /*
612112Sralph  * rmjob - remove the specified jobs from the queue.
712112Sralph  */
812112Sralph 
912112Sralph #include "lp.h"
1012112Sralph 
1112112Sralph /*
1212112Sralph  * Stuff for handling lprm specifications
1312112Sralph  */
1412112Sralph extern char	*user[];		/* users to process */
1512112Sralph extern int	users;			/* # of users in user array */
1612112Sralph extern int	requ[];			/* job number of spool entries */
1712112Sralph extern int	requests;		/* # of spool requests */
1812877Sralph extern char	*person;		/* name of person doing lprm */
1912112Sralph 
2012877Sralph static char	root[] = "root";
2112877Sralph static int	all = 0;		/* eliminate all files (root only) */
2212877Sralph static int	cur_daemon;		/* daemon's pid */
2312877Sralph static char	current[40];		/* active control file name */
2412112Sralph 
2512112Sralph int	iscf();
2612112Sralph 
2712112Sralph rmjob()
2812112Sralph {
2912112Sralph 	register int i, nitems;
3012112Sralph 	int assasinated = 0;
3112112Sralph 	struct direct **files;
3212112Sralph 
3312112Sralph 	if ((i = pgetent(line, printer)) < 0)
3412112Sralph 		fatal("cannot open printer description file");
3512112Sralph 	else if (i == 0)
3612112Sralph 		fatal("unknown printer");
3712112Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
3812112Sralph 		SD = DEFSPOOL;
3912112Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
4012112Sralph 		LO = DEFLOCK;
4112112Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
4212112Sralph 		LP = DEFDEVLP;
4312112Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
4412433Sralph 		RP = DEFLP;
4512527Sralph 	RM = pgetstr("rm", &bp);
4612112Sralph 
4712112Sralph 	/*
4812112Sralph 	 * If the format was `lprm -' and the user isn't the super-user,
4912112Sralph 	 *  then fake things to look like he said `lprm user'.
5012112Sralph 	 */
5112112Sralph 	if (users < 0) {
5212112Sralph 		if (getuid() == 0)
5312112Sralph 			all = 1;	/* all files in local queue */
5412112Sralph 		else {
5512112Sralph 			user[0] = person;
5612112Sralph 			users = 1;
5712112Sralph 		}
5812112Sralph 	}
5912112Sralph 	if (!strcmp(person, "-all")) {
6012112Sralph 		if (from == host)
6112112Sralph 			fatal("The login name \"-all\" is reserved");
6212112Sralph 		all = 1;	/* all those from 'from' */
6312112Sralph 		person = root;
6412112Sralph 	}
6512112Sralph 
6612112Sralph 	if (chdir(SD) < 0)
6712112Sralph 		fatal("cannot chdir to spool directory");
6812112Sralph 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
6912112Sralph 		fatal("cannot access spool directory");
7012112Sralph 
7112112Sralph 	if (nitems) {
7212112Sralph 		/*
7312112Sralph 		 * Check for an active printer daemon (in which case we
7412112Sralph 		 *  kill it if it is reading our file) then remove stuff
7512112Sralph 		 *  (after which we have to restart the daemon).
7612112Sralph 		 */
7712112Sralph 		if (lockchk(LO) && chk(current)) {
7812112Sralph 			assasinated = kill(cur_daemon, SIGINT) == 0;
7912112Sralph 			if (!assasinated)
8012112Sralph 				fatal("cannot kill printer daemon");
8112112Sralph 		}
8212112Sralph 		/*
8312112Sralph 		 * process the files
8412112Sralph 		 */
8512112Sralph 		for (i = 0; i < nitems; i++)
8612112Sralph 			process(files[i]->d_name);
8712112Sralph 	}
8812112Sralph 	chkremote();
8912112Sralph 	/*
9012112Sralph 	 * Restart the printer daemon if it was killed
9112112Sralph 	 */
9212877Sralph 	if (assasinated && !startdaemon(host))
9312112Sralph 		fatal("cannot restart printer daemon\n");
9412112Sralph 	exit(0);
9512112Sralph }
9612112Sralph 
9712112Sralph /*
9812112Sralph  * Process a lock file: collect the pid of the active
9912112Sralph  *  daemon and the file name of the active spool entry.
10012112Sralph  * Return boolean indicating existence of a lock file.
10112112Sralph  */
10212877Sralph static
10312112Sralph lockchk(s)
10412112Sralph 	char *s;
10512112Sralph {
10612112Sralph 	register FILE *fp;
10712112Sralph 	register int i, n;
10812112Sralph 
10912112Sralph 	if ((fp = fopen(s, "r")) == NULL)
11012112Sralph 		if (errno == EACCES)
11112112Sralph 			fatal("can't access lock file");
11212112Sralph 		else
11312112Sralph 			return(0);
11413443Sralph 	if (!getline(fp)) {
11512112Sralph 		(void) fclose(fp);
11612112Sralph 		return(0);		/* no daemon present */
11712112Sralph 	}
11812112Sralph 	cur_daemon = atoi(line);
11913443Sralph 	if (kill(cur_daemon, 0) < 0) {
12013443Sralph 		(void) fclose(fp);
12113443Sralph 		return(0);		/* no daemon present */
12213443Sralph 	}
12312112Sralph 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
12412527Sralph 		if (i > 5) {
12512112Sralph 			n = 1;
12612112Sralph 			break;
12712112Sralph 		}
12812112Sralph 		sleep(i);
12912112Sralph 	}
13012112Sralph 	current[n-1] = '\0';
13112112Sralph 	(void) fclose(fp);
13212112Sralph 	return(1);
13312112Sralph }
13412112Sralph 
13512112Sralph /*
13612112Sralph  * Process a control file.
13712112Sralph  */
13812877Sralph static
13912112Sralph process(file)
14012112Sralph 	char *file;
14112112Sralph {
14212112Sralph 	FILE *cfp;
14312112Sralph 
14412112Sralph 	if (!chk(file))
14512112Sralph 		return;
14612112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
14712112Sralph 		fatal("cannot open %s", file);
14812112Sralph 	while (getline()) {
14912112Sralph 		switch (line[0]) {
15012112Sralph 		case 'U':  /* unlink associated files */
15112112Sralph 			if (from != host)
15212112Sralph 				printf("%s: ", host);
15312112Sralph 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
15412112Sralph 				"%s dequeued\n", line+1);
15512112Sralph 		}
15612112Sralph 	}
15712112Sralph 	(void) fclose(cfp);
15812112Sralph 	if (from != host)
15912112Sralph 		printf("%s: ", host);
16012112Sralph 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
16112112Sralph }
16212112Sralph 
16312112Sralph /*
16412112Sralph  * Do the dirty work in checking
16512112Sralph  */
16612877Sralph static
16712112Sralph chk(file)
16812112Sralph 	char *file;
16912112Sralph {
17012112Sralph 	register int *r, n;
17112112Sralph 	register char **u, *cp;
17212112Sralph 	FILE *cfp;
17312112Sralph 
174*15543Sralph 	/*
175*15543Sralph 	 * Check for valid cf file name (mostly checking current).
176*15543Sralph 	 */
177*15543Sralph 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
178*15543Sralph 		return(0);
179*15543Sralph 
18012112Sralph 	if (all && (from == host || !strcmp(from, file+6)))
18112112Sralph 		return(1);
18212112Sralph 
18312112Sralph 	/*
18412112Sralph 	 * get the owner's name from the control file.
18512112Sralph 	 */
18612112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
18712112Sralph 		return(0);
18812112Sralph 	while (getline(cfp)) {
18912112Sralph 		if (line[0] == 'P')
19012112Sralph 			break;
19112112Sralph 	}
19212112Sralph 	(void) fclose(cfp);
19312112Sralph 	if (line[0] != 'P')
19412112Sralph 		return(0);
19512112Sralph 
19612112Sralph 	if (users == 0 && requests == 0)
19712112Sralph 		return(!strcmp(file, current) && isowner(line+1, file));
19812112Sralph 	/*
19912112Sralph 	 * Check the request list
20012112Sralph 	 */
20112112Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
20212112Sralph 		n = n * 10 + (*cp++ - '0');
20312112Sralph 	for (r = requ; r < &requ[requests]; r++)
20412112Sralph 		if (*r == n && isowner(line+1, file))
20512112Sralph 			return(1);
20612112Sralph 	/*
20712112Sralph 	 * Check to see if it's in the user list
20812112Sralph 	 */
20912112Sralph 	for (u = user; u < &user[users]; u++)
21012112Sralph 		if (!strcmp(*u, line+1) && isowner(line+1, file))
21112112Sralph 			return(1);
21212112Sralph 	return(0);
21312112Sralph }
21412112Sralph 
21512112Sralph /*
21612112Sralph  * If root is removing a file on the local machine, allow it.
21712433Sralph  * If root is removing a file from a remote machine, only allow
21812433Sralph  * files sent from the remote machine to be removed.
21912112Sralph  * Normal users can only remove the file from where it was sent.
22012112Sralph  */
22112877Sralph static
22212112Sralph isowner(owner, file)
22312112Sralph 	char *owner, *file;
22412112Sralph {
22512433Sralph 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
22612433Sralph 		return(1);
22712433Sralph 	if (!strcmp(person, owner) && !strcmp(from, file+6))
22812433Sralph 		return(1);
22912433Sralph 	if (from != host)
23012433Sralph 		printf("%s: ", host);
23112433Sralph 	printf("%s: Permission denied\n", file);
23212433Sralph 	return(0);
23312112Sralph }
23412112Sralph 
23512112Sralph /*
23612112Sralph  * Check to see if we are sending files to a remote machine. If we are,
23712112Sralph  * then try removing files on the remote machine.
23812112Sralph  */
23912877Sralph static
24012112Sralph chkremote()
24112112Sralph {
24212112Sralph 	register char *cp;
24312112Sralph 	register int i, rem;
24412112Sralph 	char buf[BUFSIZ];
24512112Sralph 
24612112Sralph 	if (*LP || RM == NULL)
24712112Sralph 		return;	/* not sending to a remote machine */
24812112Sralph 
24912433Sralph 	/*
25012433Sralph 	 * Flush stdout so the user can see what has been deleted
25112433Sralph 	 * while we wait (possibly) for the connection.
25212433Sralph 	 */
25312433Sralph 	fflush(stdout);
25412433Sralph 
25512112Sralph 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
25612112Sralph 	cp = buf;
25712112Sralph 	for (i = 0; i < users; i++) {
25812112Sralph 		cp += strlen(cp);
25912112Sralph 		*cp++ = ' ';
26012112Sralph 		strcpy(cp, user[i]);
26112112Sralph 	}
26212112Sralph 	for (i = 0; i < requests; i++) {
26312112Sralph 		cp += strlen(cp);
26412112Sralph 		(void) sprintf(cp, " %d", requ[i]);
26512112Sralph 	}
26612112Sralph 	strcat(cp, "\n");
26712527Sralph 	rem = getport(RM);
26812112Sralph 	if (rem < 0) {
26912112Sralph 		if (from != host)
27012112Sralph 			printf("%s: ", host);
27112112Sralph 		printf("connection to %s is down\n", RM);
27212112Sralph 	} else {
27312112Sralph 		i = strlen(buf);
27412112Sralph 		if (write(rem, buf, i) != i)
27512112Sralph 			fatal("Lost connection");
27612112Sralph 		while ((i = read(rem, buf, sizeof(buf))) > 0)
27712112Sralph 			(void) fwrite(buf, 1, i, stdout);
27812112Sralph 		(void) close(rem);
27912112Sralph 	}
28012112Sralph }
28112112Sralph 
28212112Sralph /*
28312112Sralph  * Return 1 if the filename begins with 'cf'
28412112Sralph  */
28512877Sralph static
28612112Sralph iscf(d)
28712112Sralph 	struct direct *d;
28812112Sralph {
28912112Sralph 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
29012112Sralph }
291