122439Sdist /*
222439Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
534203Sbostic  * Redistribution and use in source and binary forms are permitted
634936Sbostic  * provided that the above copyright notice and this paragraph are
734936Sbostic  * duplicated in all such forms and that any documentation,
834936Sbostic  * advertising materials, and other materials related to such
934936Sbostic  * distribution and use acknowledge that the software was developed
1034936Sbostic  * by the University of California, Berkeley.  The name of the
1134936Sbostic  * University may not be used to endorse or promote products derived
1234936Sbostic  * from this software without specific prior written permission.
1334936Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434936Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534936Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622439Sdist  */
1722439Sdist 
1813955Ssam #ifndef lint
19*36844Stef static char sccsid[] = "@(#)rmjob.c	5.4 (Berkeley) 02/20/89";
2034203Sbostic #endif /* not lint */
2113955Ssam 
2212112Sralph /*
2312112Sralph  * rmjob - remove the specified jobs from the queue.
2412112Sralph  */
2512112Sralph 
2612112Sralph #include "lp.h"
2712112Sralph 
2812112Sralph /*
2912112Sralph  * Stuff for handling lprm specifications
3012112Sralph  */
3112112Sralph extern char	*user[];		/* users to process */
3212112Sralph extern int	users;			/* # of users in user array */
3312112Sralph extern int	requ[];			/* job number of spool entries */
3412112Sralph extern int	requests;		/* # of spool requests */
3512877Sralph extern char	*person;		/* name of person doing lprm */
3612112Sralph 
3716759Sralph char	root[] = "root";
3816759Sralph int	all = 0;		/* eliminate all files (root only) */
3916759Sralph int	cur_daemon;		/* daemon's pid */
4016759Sralph char	current[40];		/* active control file name */
4112112Sralph 
4212112Sralph int	iscf();
4312112Sralph 
4412112Sralph rmjob()
4512112Sralph {
4612112Sralph 	register int i, nitems;
4712112Sralph 	int assasinated = 0;
4812112Sralph 	struct direct **files;
4912112Sralph 
5012112Sralph 	if ((i = pgetent(line, printer)) < 0)
5112112Sralph 		fatal("cannot open printer description file");
5212112Sralph 	else if (i == 0)
5312112Sralph 		fatal("unknown printer");
5412112Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
5512112Sralph 		SD = DEFSPOOL;
5612112Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
5712112Sralph 		LO = DEFLOCK;
5812112Sralph 	if ((LP = pgetstr("lp", &bp)) == NULL)
5912112Sralph 		LP = DEFDEVLP;
6012112Sralph 	if ((RP = pgetstr("rp", &bp)) == NULL)
6112433Sralph 		RP = DEFLP;
6212527Sralph 	RM = pgetstr("rm", &bp);
63*36844Stef 	/*
64*36844Stef 	 * Figure out whether the local machine is the same as the remote
65*36844Stef 	 * machine entry (if it exists).  If not, then ignore the local
66*36844Stef 	 * queue information.
67*36844Stef 	 */
68*36844Stef 	 if (RM != (char *) NULL) {
69*36844Stef 		char name[256];
70*36844Stef 		struct hostent *hp;
7112112Sralph 
72*36844Stef 		/* get the standard network name of the local host */
73*36844Stef 		gethostname(name, sizeof(name));
74*36844Stef 		name[sizeof(name)-1] = '\0';
75*36844Stef 		hp = gethostbyname(name);
76*36844Stef 		if (hp == (struct hostent *) NULL) {
77*36844Stef 		    printf("unable to get network name for local machine %s",
78*36844Stef 			name);
79*36844Stef 		    goto localcheck_done;
80*36844Stef 		} else strcpy(name, hp->h_name);
81*36844Stef 
82*36844Stef 		/* get the standard network name of RM */
83*36844Stef 		hp = gethostbyname(RM);
84*36844Stef 		if (hp == (struct hostent *) NULL) {
85*36844Stef 		    printf("unable to get hostname for remote machine %s", RM);
86*36844Stef 		    goto localcheck_done;
87*36844Stef 		}
88*36844Stef 
89*36844Stef 		/* if printer is not on local machine, ignore LP */
90*36844Stef 		if (strcmp(name, hp->h_name) != 0) *LP = '\0';
91*36844Stef 	}
92*36844Stef localcheck_done:
93*36844Stef 
9412112Sralph 	/*
9512112Sralph 	 * If the format was `lprm -' and the user isn't the super-user,
9612112Sralph 	 *  then fake things to look like he said `lprm user'.
9712112Sralph 	 */
9812112Sralph 	if (users < 0) {
9912112Sralph 		if (getuid() == 0)
10012112Sralph 			all = 1;	/* all files in local queue */
10112112Sralph 		else {
10212112Sralph 			user[0] = person;
10312112Sralph 			users = 1;
10412112Sralph 		}
10512112Sralph 	}
10612112Sralph 	if (!strcmp(person, "-all")) {
10712112Sralph 		if (from == host)
10812112Sralph 			fatal("The login name \"-all\" is reserved");
10912112Sralph 		all = 1;	/* all those from 'from' */
11012112Sralph 		person = root;
11112112Sralph 	}
11212112Sralph 
11312112Sralph 	if (chdir(SD) < 0)
11412112Sralph 		fatal("cannot chdir to spool directory");
11512112Sralph 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
11612112Sralph 		fatal("cannot access spool directory");
11712112Sralph 
11812112Sralph 	if (nitems) {
11912112Sralph 		/*
12012112Sralph 		 * Check for an active printer daemon (in which case we
12112112Sralph 		 *  kill it if it is reading our file) then remove stuff
12212112Sralph 		 *  (after which we have to restart the daemon).
12312112Sralph 		 */
12412112Sralph 		if (lockchk(LO) && chk(current)) {
12512112Sralph 			assasinated = kill(cur_daemon, SIGINT) == 0;
12612112Sralph 			if (!assasinated)
12712112Sralph 				fatal("cannot kill printer daemon");
12812112Sralph 		}
12912112Sralph 		/*
13012112Sralph 		 * process the files
13112112Sralph 		 */
13212112Sralph 		for (i = 0; i < nitems; i++)
13312112Sralph 			process(files[i]->d_name);
13412112Sralph 	}
13512112Sralph 	chkremote();
13612112Sralph 	/*
13712112Sralph 	 * Restart the printer daemon if it was killed
13812112Sralph 	 */
13915828Sralph 	if (assasinated && !startdaemon(printer))
14012112Sralph 		fatal("cannot restart printer daemon\n");
14112112Sralph 	exit(0);
14212112Sralph }
14312112Sralph 
14412112Sralph /*
14512112Sralph  * Process a lock file: collect the pid of the active
14612112Sralph  *  daemon and the file name of the active spool entry.
14712112Sralph  * Return boolean indicating existence of a lock file.
14812112Sralph  */
14912112Sralph lockchk(s)
15012112Sralph 	char *s;
15112112Sralph {
15212112Sralph 	register FILE *fp;
15312112Sralph 	register int i, n;
15412112Sralph 
15512112Sralph 	if ((fp = fopen(s, "r")) == NULL)
15612112Sralph 		if (errno == EACCES)
15712112Sralph 			fatal("can't access lock file");
15812112Sralph 		else
15912112Sralph 			return(0);
16013443Sralph 	if (!getline(fp)) {
16112112Sralph 		(void) fclose(fp);
16212112Sralph 		return(0);		/* no daemon present */
16312112Sralph 	}
16412112Sralph 	cur_daemon = atoi(line);
16513443Sralph 	if (kill(cur_daemon, 0) < 0) {
16613443Sralph 		(void) fclose(fp);
16713443Sralph 		return(0);		/* no daemon present */
16813443Sralph 	}
16912112Sralph 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
17012527Sralph 		if (i > 5) {
17112112Sralph 			n = 1;
17212112Sralph 			break;
17312112Sralph 		}
17412112Sralph 		sleep(i);
17512112Sralph 	}
17612112Sralph 	current[n-1] = '\0';
17712112Sralph 	(void) fclose(fp);
17812112Sralph 	return(1);
17912112Sralph }
18012112Sralph 
18112112Sralph /*
18212112Sralph  * Process a control file.
18312112Sralph  */
18412112Sralph process(file)
18512112Sralph 	char *file;
18612112Sralph {
18712112Sralph 	FILE *cfp;
18812112Sralph 
18912112Sralph 	if (!chk(file))
19012112Sralph 		return;
19112112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
19212112Sralph 		fatal("cannot open %s", file);
19316759Sralph 	while (getline(cfp)) {
19412112Sralph 		switch (line[0]) {
19512112Sralph 		case 'U':  /* unlink associated files */
19612112Sralph 			if (from != host)
19712112Sralph 				printf("%s: ", host);
19812112Sralph 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
19912112Sralph 				"%s dequeued\n", line+1);
20012112Sralph 		}
20112112Sralph 	}
20212112Sralph 	(void) fclose(cfp);
20312112Sralph 	if (from != host)
20412112Sralph 		printf("%s: ", host);
20512112Sralph 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
20612112Sralph }
20712112Sralph 
20812112Sralph /*
20912112Sralph  * Do the dirty work in checking
21012112Sralph  */
21112112Sralph chk(file)
21212112Sralph 	char *file;
21312112Sralph {
21412112Sralph 	register int *r, n;
21512112Sralph 	register char **u, *cp;
21612112Sralph 	FILE *cfp;
21712112Sralph 
21815543Sralph 	/*
21915543Sralph 	 * Check for valid cf file name (mostly checking current).
22015543Sralph 	 */
22115543Sralph 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
22215543Sralph 		return(0);
22315543Sralph 
22412112Sralph 	if (all && (from == host || !strcmp(from, file+6)))
22512112Sralph 		return(1);
22612112Sralph 
22712112Sralph 	/*
22812112Sralph 	 * get the owner's name from the control file.
22912112Sralph 	 */
23012112Sralph 	if ((cfp = fopen(file, "r")) == NULL)
23112112Sralph 		return(0);
23212112Sralph 	while (getline(cfp)) {
23312112Sralph 		if (line[0] == 'P')
23412112Sralph 			break;
23512112Sralph 	}
23612112Sralph 	(void) fclose(cfp);
23712112Sralph 	if (line[0] != 'P')
23812112Sralph 		return(0);
23912112Sralph 
24012112Sralph 	if (users == 0 && requests == 0)
24112112Sralph 		return(!strcmp(file, current) && isowner(line+1, file));
24212112Sralph 	/*
24312112Sralph 	 * Check the request list
24412112Sralph 	 */
24512112Sralph 	for (n = 0, cp = file+3; isdigit(*cp); )
24612112Sralph 		n = n * 10 + (*cp++ - '0');
24712112Sralph 	for (r = requ; r < &requ[requests]; r++)
24812112Sralph 		if (*r == n && isowner(line+1, file))
24912112Sralph 			return(1);
25012112Sralph 	/*
25112112Sralph 	 * Check to see if it's in the user list
25212112Sralph 	 */
25312112Sralph 	for (u = user; u < &user[users]; u++)
25412112Sralph 		if (!strcmp(*u, line+1) && isowner(line+1, file))
25512112Sralph 			return(1);
25612112Sralph 	return(0);
25712112Sralph }
25812112Sralph 
25912112Sralph /*
26012112Sralph  * If root is removing a file on the local machine, allow it.
26112433Sralph  * If root is removing a file from a remote machine, only allow
26212433Sralph  * files sent from the remote machine to be removed.
26312112Sralph  * Normal users can only remove the file from where it was sent.
26412112Sralph  */
26512112Sralph isowner(owner, file)
26612112Sralph 	char *owner, *file;
26712112Sralph {
26812433Sralph 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
26912433Sralph 		return(1);
27012433Sralph 	if (!strcmp(person, owner) && !strcmp(from, file+6))
27112433Sralph 		return(1);
27212433Sralph 	if (from != host)
27312433Sralph 		printf("%s: ", host);
27412433Sralph 	printf("%s: Permission denied\n", file);
27512433Sralph 	return(0);
27612112Sralph }
27712112Sralph 
27812112Sralph /*
27912112Sralph  * Check to see if we are sending files to a remote machine. If we are,
28012112Sralph  * then try removing files on the remote machine.
28112112Sralph  */
28212112Sralph chkremote()
28312112Sralph {
28412112Sralph 	register char *cp;
28512112Sralph 	register int i, rem;
28612112Sralph 	char buf[BUFSIZ];
28712112Sralph 
28812112Sralph 	if (*LP || RM == NULL)
28912112Sralph 		return;	/* not sending to a remote machine */
29012112Sralph 
29112433Sralph 	/*
29212433Sralph 	 * Flush stdout so the user can see what has been deleted
29312433Sralph 	 * while we wait (possibly) for the connection.
29412433Sralph 	 */
29512433Sralph 	fflush(stdout);
29612433Sralph 
29712112Sralph 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
29812112Sralph 	cp = buf;
29912112Sralph 	for (i = 0; i < users; i++) {
30012112Sralph 		cp += strlen(cp);
30112112Sralph 		*cp++ = ' ';
30212112Sralph 		strcpy(cp, user[i]);
30312112Sralph 	}
30412112Sralph 	for (i = 0; i < requests; i++) {
30512112Sralph 		cp += strlen(cp);
30612112Sralph 		(void) sprintf(cp, " %d", requ[i]);
30712112Sralph 	}
30812112Sralph 	strcat(cp, "\n");
30912527Sralph 	rem = getport(RM);
31012112Sralph 	if (rem < 0) {
31112112Sralph 		if (from != host)
31212112Sralph 			printf("%s: ", host);
31312112Sralph 		printf("connection to %s is down\n", RM);
31412112Sralph 	} else {
31512112Sralph 		i = strlen(buf);
31612112Sralph 		if (write(rem, buf, i) != i)
31712112Sralph 			fatal("Lost connection");
31812112Sralph 		while ((i = read(rem, buf, sizeof(buf))) > 0)
31912112Sralph 			(void) fwrite(buf, 1, i, stdout);
32012112Sralph 		(void) close(rem);
32112112Sralph 	}
32212112Sralph }
32312112Sralph 
32412112Sralph /*
32512112Sralph  * Return 1 if the filename begins with 'cf'
32612112Sralph  */
32712112Sralph iscf(d)
32812112Sralph 	struct direct *d;
32912112Sralph {
33012112Sralph 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
33112112Sralph }
332