1*22439Sdist /*
2*22439Sdist  * Copyright (c) 1983 Regents of the University of California.
3*22439Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22439Sdist  * specifies the terms and conditions for redistribution.
5*22439Sdist  */
6*22439Sdist 
713955Ssam #ifndef lint
8*22439Sdist static char sccsid[] = "@(#)rmjob.c	5.1 (Berkeley) 06/06/85";
9*22439Sdist #endif not lint
1013955Ssam 
1112112Sralph /*
1212112Sralph  * rmjob - remove the specified jobs from the queue.
1312112Sralph  */
1412112Sralph 
1512112Sralph #include "lp.h"
1612112Sralph 
1712112Sralph /*
1812112Sralph  * Stuff for handling lprm specifications
1912112Sralph  */
2012112Sralph extern char	*user[];		/* users to process */
2112112Sralph extern int	users;			/* # of users in user array */
2212112Sralph extern int	requ[];			/* job number of spool entries */
2312112Sralph extern int	requests;		/* # of spool requests */
2412877Sralph extern char	*person;		/* name of person doing lprm */
2512112Sralph 
2616759Sralph char	root[] = "root";
2716759Sralph int	all = 0;		/* eliminate all files (root only) */
2816759Sralph int	cur_daemon;		/* daemon's pid */
2916759Sralph char	current[40];		/* active control file name */
3012112Sralph 
3112112Sralph int	iscf();
3212112Sralph 
3312112Sralph rmjob()
3412112Sralph {
3512112Sralph 	register int i, nitems;
3612112Sralph 	int assasinated = 0;
3712112Sralph 	struct direct **files;
3812112Sralph 
3912112Sralph 	if ((i = pgetent(line, printer)) < 0)
4012112Sralph 		fatal("cannot open printer description file");
4112112Sralph 	else if (i == 0)
4212112Sralph 		fatal("unknown printer");
4312112Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
4412112Sralph 		SD = DEFSPOOL;
4512112Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
4612112Sralph 		LO = DEFLOCK;
4712112Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
4812112Sralph 		LP = DEFDEVLP;
4912112Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
5012433Sralph 		RP = DEFLP;
5112527Sralph 	RM = pgetstr("rm", &bp);
5212112Sralph 
5312112Sralph 	/*
5412112Sralph 	 * If the format was `lprm -' and the user isn't the super-user,
5512112Sralph 	 *  then fake things to look like he said `lprm user'.
5612112Sralph 	 */
5712112Sralph 	if (users < 0) {
5812112Sralph 		if (getuid() == 0)
5912112Sralph 			all = 1;	/* all files in local queue */
6012112Sralph 		else {
6112112Sralph 			user[0] = person;
6212112Sralph 			users = 1;
6312112Sralph 		}
6412112Sralph 	}
6512112Sralph 	if (!strcmp(person, "-all")) {
6612112Sralph 		if (from == host)
6712112Sralph 			fatal("The login name \"-all\" is reserved");
6812112Sralph 		all = 1;	/* all those from 'from' */
6912112Sralph 		person = root;
7012112Sralph 	}
7112112Sralph 
7212112Sralph 	if (chdir(SD) < 0)
7312112Sralph 		fatal("cannot chdir to spool directory");
7412112Sralph 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
7512112Sralph 		fatal("cannot access spool directory");
7612112Sralph 
7712112Sralph 	if (nitems) {
7812112Sralph 		/*
7912112Sralph 		 * Check for an active printer daemon (in which case we
8012112Sralph 		 *  kill it if it is reading our file) then remove stuff
8112112Sralph 		 *  (after which we have to restart the daemon).
8212112Sralph 		 */
8312112Sralph 		if (lockchk(LO) && chk(current)) {
8412112Sralph 			assasinated = kill(cur_daemon, SIGINT) == 0;
8512112Sralph 			if (!assasinated)
8612112Sralph 				fatal("cannot kill printer daemon");
8712112Sralph 		}
8812112Sralph 		/*
8912112Sralph 		 * process the files
9012112Sralph 		 */
9112112Sralph 		for (i = 0; i < nitems; i++)
9212112Sralph 			process(files[i]->d_name);
9312112Sralph 	}
9412112Sralph 	chkremote();
9512112Sralph 	/*
9612112Sralph 	 * Restart the printer daemon if it was killed
9712112Sralph 	 */
9815828Sralph 	if (assasinated && !startdaemon(printer))
9912112Sralph 		fatal("cannot restart printer daemon\n");
10012112Sralph 	exit(0);
10112112Sralph }
10212112Sralph 
10312112Sralph /*
10412112Sralph  * Process a lock file: collect the pid of the active
10512112Sralph  *  daemon and the file name of the active spool entry.
10612112Sralph  * Return boolean indicating existence of a lock file.
10712112Sralph  */
10812112Sralph lockchk(s)
10912112Sralph 	char *s;
11012112Sralph {
11112112Sralph 	register FILE *fp;
11212112Sralph 	register int i, n;
11312112Sralph 
11412112Sralph 	if ((fp = fopen(s, "r")) == NULL)
11512112Sralph 		if (errno == EACCES)
11612112Sralph 			fatal("can't access lock file");
11712112Sralph 		else
11812112Sralph 			return(0);
11913443Sralph 	if (!getline(fp)) {
12012112Sralph 		(void) fclose(fp);
12112112Sralph 		return(0);		/* no daemon present */
12212112Sralph 	}
12312112Sralph 	cur_daemon = atoi(line);
12413443Sralph 	if (kill(cur_daemon, 0) < 0) {
12513443Sralph 		(void) fclose(fp);
12613443Sralph 		return(0);		/* no daemon present */
12713443Sralph 	}
12812112Sralph 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
12912527Sralph 		if (i > 5) {
13012112Sralph 			n = 1;
13112112Sralph 			break;
13212112Sralph 		}
13312112Sralph 		sleep(i);
13412112Sralph 	}
13512112Sralph 	current[n-1] = '\0';
13612112Sralph 	(void) fclose(fp);
13712112Sralph 	return(1);
13812112Sralph }
13912112Sralph 
14012112Sralph /*
14112112Sralph  * Process a control file.
14212112Sralph  */
14312112Sralph process(file)
14412112Sralph 	char *file;
14512112Sralph {
14612112Sralph 	FILE *cfp;
14712112Sralph 
14812112Sralph 	if (!chk(file))
14912112Sralph 		return;
15012112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
15112112Sralph 		fatal("cannot open %s", file);
15216759Sralph 	while (getline(cfp)) {
15312112Sralph 		switch (line[0]) {
15412112Sralph 		case 'U':  /* unlink associated files */
15512112Sralph 			if (from != host)
15612112Sralph 				printf("%s: ", host);
15712112Sralph 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
15812112Sralph 				"%s dequeued\n", line+1);
15912112Sralph 		}
16012112Sralph 	}
16112112Sralph 	(void) fclose(cfp);
16212112Sralph 	if (from != host)
16312112Sralph 		printf("%s: ", host);
16412112Sralph 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
16512112Sralph }
16612112Sralph 
16712112Sralph /*
16812112Sralph  * Do the dirty work in checking
16912112Sralph  */
17012112Sralph chk(file)
17112112Sralph 	char *file;
17212112Sralph {
17312112Sralph 	register int *r, n;
17412112Sralph 	register char **u, *cp;
17512112Sralph 	FILE *cfp;
17612112Sralph 
17715543Sralph 	/*
17815543Sralph 	 * Check for valid cf file name (mostly checking current).
17915543Sralph 	 */
18015543Sralph 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
18115543Sralph 		return(0);
18215543Sralph 
18312112Sralph 	if (all && (from == host || !strcmp(from, file+6)))
18412112Sralph 		return(1);
18512112Sralph 
18612112Sralph 	/*
18712112Sralph 	 * get the owner's name from the control file.
18812112Sralph 	 */
18912112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
19012112Sralph 		return(0);
19112112Sralph 	while (getline(cfp)) {
19212112Sralph 		if (line[0] == 'P')
19312112Sralph 			break;
19412112Sralph 	}
19512112Sralph 	(void) fclose(cfp);
19612112Sralph 	if (line[0] != 'P')
19712112Sralph 		return(0);
19812112Sralph 
19912112Sralph 	if (users == 0 && requests == 0)
20012112Sralph 		return(!strcmp(file, current) && isowner(line+1, file));
20112112Sralph 	/*
20212112Sralph 	 * Check the request list
20312112Sralph 	 */
20412112Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
20512112Sralph 		n = n * 10 + (*cp++ - '0');
20612112Sralph 	for (r = requ; r < &requ[requests]; r++)
20712112Sralph 		if (*r == n && isowner(line+1, file))
20812112Sralph 			return(1);
20912112Sralph 	/*
21012112Sralph 	 * Check to see if it's in the user list
21112112Sralph 	 */
21212112Sralph 	for (u = user; u < &user[users]; u++)
21312112Sralph 		if (!strcmp(*u, line+1) && isowner(line+1, file))
21412112Sralph 			return(1);
21512112Sralph 	return(0);
21612112Sralph }
21712112Sralph 
21812112Sralph /*
21912112Sralph  * If root is removing a file on the local machine, allow it.
22012433Sralph  * If root is removing a file from a remote machine, only allow
22112433Sralph  * files sent from the remote machine to be removed.
22212112Sralph  * Normal users can only remove the file from where it was sent.
22312112Sralph  */
22412112Sralph isowner(owner, file)
22512112Sralph 	char *owner, *file;
22612112Sralph {
22712433Sralph 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
22812433Sralph 		return(1);
22912433Sralph 	if (!strcmp(person, owner) && !strcmp(from, file+6))
23012433Sralph 		return(1);
23112433Sralph 	if (from != host)
23212433Sralph 		printf("%s: ", host);
23312433Sralph 	printf("%s: Permission denied\n", file);
23412433Sralph 	return(0);
23512112Sralph }
23612112Sralph 
23712112Sralph /*
23812112Sralph  * Check to see if we are sending files to a remote machine. If we are,
23912112Sralph  * then try removing files on the remote machine.
24012112Sralph  */
24112112Sralph chkremote()
24212112Sralph {
24312112Sralph 	register char *cp;
24412112Sralph 	register int i, rem;
24512112Sralph 	char buf[BUFSIZ];
24612112Sralph 
24712112Sralph 	if (*LP || RM == NULL)
24812112Sralph 		return;	/* not sending to a remote machine */
24912112Sralph 
25012433Sralph 	/*
25112433Sralph 	 * Flush stdout so the user can see what has been deleted
25212433Sralph 	 * while we wait (possibly) for the connection.
25312433Sralph 	 */
25412433Sralph 	fflush(stdout);
25512433Sralph 
25612112Sralph 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
25712112Sralph 	cp = buf;
25812112Sralph 	for (i = 0; i < users; i++) {
25912112Sralph 		cp += strlen(cp);
26012112Sralph 		*cp++ = ' ';
26112112Sralph 		strcpy(cp, user[i]);
26212112Sralph 	}
26312112Sralph 	for (i = 0; i < requests; i++) {
26412112Sralph 		cp += strlen(cp);
26512112Sralph 		(void) sprintf(cp, " %d", requ[i]);
26612112Sralph 	}
26712112Sralph 	strcat(cp, "\n");
26812527Sralph 	rem = getport(RM);
26912112Sralph 	if (rem < 0) {
27012112Sralph 		if (from != host)
27112112Sralph 			printf("%s: ", host);
27212112Sralph 		printf("connection to %s is down\n", RM);
27312112Sralph 	} else {
27412112Sralph 		i = strlen(buf);
27512112Sralph 		if (write(rem, buf, i) != i)
27612112Sralph 			fatal("Lost connection");
27712112Sralph 		while ((i = read(rem, buf, sizeof(buf))) > 0)
27812112Sralph 			(void) fwrite(buf, 1, i, stdout);
27912112Sralph 		(void) close(rem);
28012112Sralph 	}
28112112Sralph }
28212112Sralph 
28312112Sralph /*
28412112Sralph  * Return 1 if the filename begins with 'cf'
28512112Sralph  */
28612112Sralph iscf(d)
28712112Sralph 	struct direct *d;
28812112Sralph {
28912112Sralph 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
29012112Sralph }
291