xref: /netbsd-src/usr.sbin/lpr/common_source/rmjob.c (revision 7027866a09786f44d29080aa3a48badc640ddfe2)
1*7027866aSroy /*	$NetBSD: rmjob.c,v 1.25 2009/07/13 19:05:41 roy Exp $	*/
221908ddbSjtc 
361f28255Scgd /*
42847add2Scgd  * Copyright (c) 1983, 1993
52847add2Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32fe7ed7ceSmrg #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34ad1d6861Smikel #if 0
35e6a91a09Smrg static char sccsid[] = "@(#)rmjob.c	8.2 (Berkeley) 4/28/95";
36ad1d6861Smikel #else
37*7027866aSroy __RCSID("$NetBSD: rmjob.c,v 1.25 2009/07/13 19:05:41 roy Exp $");
38ad1d6861Smikel #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
412847add2Scgd #include <sys/param.h>
422847add2Scgd 
432847add2Scgd #include <signal.h>
442847add2Scgd #include <errno.h>
452847add2Scgd #include <dirent.h>
462847add2Scgd #include <unistd.h>
472847add2Scgd #include <stdlib.h>
482847add2Scgd #include <stdio.h>
492847add2Scgd #include <string.h>
502847add2Scgd #include <ctype.h>
512847add2Scgd #include "lp.h"
522847add2Scgd #include "lp.local.h"
532847add2Scgd #include "pathnames.h"
542847add2Scgd 
5561f28255Scgd /*
5661f28255Scgd  * rmjob - remove the specified jobs from the queue.
5761f28255Scgd  */
5861f28255Scgd 
5961f28255Scgd /*
6061f28255Scgd  * Stuff for handling lprm specifications
6161f28255Scgd  */
6261f28255Scgd extern char	*user[];		/* users to process */
6361f28255Scgd extern int	users;			/* # of users in user array */
6461f28255Scgd extern int	requ[];			/* job number of spool entries */
6561f28255Scgd extern int	requests;		/* # of spool requests */
6661f28255Scgd extern char	*person;		/* name of person doing lprm */
6761f28255Scgd 
682847add2Scgd static char	root[] = "root";
692847add2Scgd static int	all = 0;		/* eliminate all files (root only) */
702847add2Scgd static int	cur_daemon;		/* daemon's pid */
712847add2Scgd static char	current[40];		/* active control file name */
7261f28255Scgd 
738e41ca80Shpeyerl extern uid_t	uid, euid;		/* real and effective user id's */
748e41ca80Shpeyerl 
7504723c3fSchristos static	void	do_unlink(const char *);
76895dc72aSwiz static	void	alarmer(int);
778e41ca80Shpeyerl 
782847add2Scgd void
rmjob(void)79895dc72aSwiz rmjob(void)
8061f28255Scgd {
81fe7ed7ceSmrg 	int i, nitems;
8261f28255Scgd 	int assasinated = 0;
833ccb8240Sjtc 	struct dirent **files;
8461f28255Scgd 
8504723c3fSchristos 	getprintcap(printer);
8661f28255Scgd 
8761f28255Scgd 	/*
8861f28255Scgd 	 * If the format was `lprm -' and the user isn't the super-user,
8961f28255Scgd 	 *  then fake things to look like he said `lprm user'.
9061f28255Scgd 	 */
9161f28255Scgd 	if (users < 0) {
9261f28255Scgd 		if (getuid() == 0)
9361f28255Scgd 			all = 1;	/* all files in local queue */
9461f28255Scgd 		else {
9561f28255Scgd 			user[0] = person;
9661f28255Scgd 			users = 1;
9761f28255Scgd 		}
9861f28255Scgd 	}
9961f28255Scgd 	if (!strcmp(person, "-all")) {
10061f28255Scgd 		if (from == host)
10161f28255Scgd 			fatal("The login name \"-all\" is reserved");
10261f28255Scgd 		all = 1;	/* all those from 'from' */
10361f28255Scgd 		person = root;
10461f28255Scgd 	}
10561f28255Scgd 
1068e41ca80Shpeyerl 	seteuid(euid);
10761f28255Scgd 	if (chdir(SD) < 0)
10861f28255Scgd 		fatal("cannot chdir to spool directory");
10961f28255Scgd 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
11061f28255Scgd 		fatal("cannot access spool directory");
1118e41ca80Shpeyerl 	seteuid(uid);
11261f28255Scgd 
11361f28255Scgd 	if (nitems) {
11461f28255Scgd 		/*
11561f28255Scgd 		 * Check for an active printer daemon (in which case we
11661f28255Scgd 		 *  kill it if it is reading our file) then remove stuff
11761f28255Scgd 		 *  (after which we have to restart the daemon).
11861f28255Scgd 		 */
11961f28255Scgd 		if (lockchk(LO) && chk(current)) {
1208e41ca80Shpeyerl 			seteuid(euid);
12161f28255Scgd 			assasinated = kill(cur_daemon, SIGINT) == 0;
1228e41ca80Shpeyerl 			seteuid(uid);
12361f28255Scgd 			if (!assasinated)
12461f28255Scgd 				fatal("cannot kill printer daemon");
12561f28255Scgd 		}
12661f28255Scgd 		/*
12761f28255Scgd 		 * process the files
12861f28255Scgd 		 */
12961f28255Scgd 		for (i = 0; i < nitems; i++)
13061f28255Scgd 			process(files[i]->d_name);
13161f28255Scgd 	}
13261f28255Scgd 	rmremote();
13361f28255Scgd 	/*
13461f28255Scgd 	 * Restart the printer daemon if it was killed
13561f28255Scgd 	 */
13661f28255Scgd 	if (assasinated && !startdaemon(printer))
13761f28255Scgd 		fatal("cannot restart printer daemon\n");
13861f28255Scgd 	exit(0);
13961f28255Scgd }
14061f28255Scgd 
14161f28255Scgd /*
14261f28255Scgd  * Process a lock file: collect the pid of the active
14361f28255Scgd  *  daemon and the file name of the active spool entry.
14461f28255Scgd  * Return boolean indicating existence of a lock file.
14561f28255Scgd  */
1462847add2Scgd int
lockchk(const char * s)14704723c3fSchristos lockchk(const char *s)
14861f28255Scgd {
149fe7ed7ceSmrg 	FILE *fp;
150fe7ed7ceSmrg 	int i, n;
15161f28255Scgd 
1528e41ca80Shpeyerl 	seteuid(euid);
1538e41ca80Shpeyerl 	if ((fp = fopen(s, "r")) == NULL) {
15461f28255Scgd 		if (errno == EACCES)
15561f28255Scgd 			fatal("can't access lock file");
15661f28255Scgd 		else
15761f28255Scgd 			return(0);
1588e41ca80Shpeyerl 	}
1598e41ca80Shpeyerl 	seteuid(uid);
160*7027866aSroy 	if (!get_line(fp)) {
16161f28255Scgd 		(void)fclose(fp);
16261f28255Scgd 		return(0);		/* no daemon present */
16361f28255Scgd 	}
16461f28255Scgd 	cur_daemon = atoi(line);
1658e41ca80Shpeyerl 	if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
16661f28255Scgd 		(void)fclose(fp);
16761f28255Scgd 		return(0);		/* no daemon present */
16861f28255Scgd 	}
16961f28255Scgd 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
17061f28255Scgd 		if (i > 5) {
17161f28255Scgd 			n = 1;
17261f28255Scgd 			break;
17361f28255Scgd 		}
17423b9fac0Smrg 		sleep((size_t)i);
17561f28255Scgd 	}
17661f28255Scgd 	current[n-1] = '\0';
17761f28255Scgd 	(void)fclose(fp);
17861f28255Scgd 	return(1);
17961f28255Scgd }
18061f28255Scgd 
18161f28255Scgd /*
18261f28255Scgd  * Process a control file.
18361f28255Scgd  */
1842847add2Scgd void
process(const char * file)18504723c3fSchristos process(const char *file)
18661f28255Scgd {
18761f28255Scgd 	FILE *cfp;
18861f28255Scgd 
18961f28255Scgd 	if (!chk(file))
19061f28255Scgd 		return;
1918e41ca80Shpeyerl 	seteuid(euid);
19261f28255Scgd 	if ((cfp = fopen(file, "r")) == NULL)
19361f28255Scgd 		fatal("cannot open %s", file);
1948e41ca80Shpeyerl 	seteuid(uid);
195*7027866aSroy 	while (get_line(cfp)) {
19661f28255Scgd 		switch (line[0]) {
19761f28255Scgd 		case 'U':  /* unlink associated files */
198fe7ed7ceSmrg 			if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
199fe7ed7ceSmrg 				break;
2000e9c04faSpk 			do_unlink(line+1);
20161f28255Scgd 		}
20261f28255Scgd 	}
20361f28255Scgd 	(void)fclose(cfp);
2048e41ca80Shpeyerl 	do_unlink(file);
2058e41ca80Shpeyerl }
2068e41ca80Shpeyerl 
2078e41ca80Shpeyerl static void
do_unlink(const char * file)20804723c3fSchristos do_unlink(const char *file)
2098e41ca80Shpeyerl {
2108e41ca80Shpeyerl 	int	ret;
2118e41ca80Shpeyerl 
21261f28255Scgd 	if (from != host)
21361f28255Scgd 		printf("%s: ", host);
2148e41ca80Shpeyerl 	seteuid(euid);
2158e41ca80Shpeyerl 	ret = unlink(file);
2168e41ca80Shpeyerl 	seteuid(uid);
2178e41ca80Shpeyerl 	printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
21861f28255Scgd }
21961f28255Scgd 
22061f28255Scgd /*
22161f28255Scgd  * Do the dirty work in checking
22261f28255Scgd  */
2232847add2Scgd int
chk(const char * file)22404723c3fSchristos chk(const char *file)
22561f28255Scgd {
226fe7ed7ceSmrg 	int *r, n;
22704723c3fSchristos 	char **u;
22804723c3fSchristos 	const char *cp;
22961f28255Scgd 	FILE *cfp;
23061f28255Scgd 
23161f28255Scgd 	/*
23261f28255Scgd 	 * Check for valid cf file name (mostly checking current).
23361f28255Scgd 	 */
23461f28255Scgd 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
23561f28255Scgd 		return(0);
23661f28255Scgd 
23761f28255Scgd 	if (all && (from == host || !strcmp(from, file+6)))
23861f28255Scgd 		return(1);
23961f28255Scgd 
24061f28255Scgd 	/*
24161f28255Scgd 	 * get the owner's name from the control file.
24261f28255Scgd 	 */
2438e41ca80Shpeyerl 	seteuid(euid);
24461f28255Scgd 	if ((cfp = fopen(file, "r")) == NULL)
24561f28255Scgd 		return(0);
2468e41ca80Shpeyerl 	seteuid(uid);
247*7027866aSroy 	while (get_line(cfp)) {
24861f28255Scgd 		if (line[0] == 'P')
24961f28255Scgd 			break;
25061f28255Scgd 	}
25161f28255Scgd 	(void)fclose(cfp);
25261f28255Scgd 	if (line[0] != 'P')
25361f28255Scgd 		return(0);
25461f28255Scgd 
25561f28255Scgd 	if (users == 0 && requests == 0)
25661f28255Scgd 		return(!strcmp(file, current) && isowner(line+1, file));
25761f28255Scgd 	/*
25861f28255Scgd 	 * Check the request list
25961f28255Scgd 	 */
26075ba9fc7Sdsl 	for (n = 0, cp = file+3; isdigit((unsigned char)*cp); )
26161f28255Scgd 		n = n * 10 + (*cp++ - '0');
26261f28255Scgd 	for (r = requ; r < &requ[requests]; r++)
26361f28255Scgd 		if (*r == n && isowner(line+1, file))
26461f28255Scgd 			return(1);
26561f28255Scgd 	/*
26661f28255Scgd 	 * Check to see if it's in the user list
26761f28255Scgd 	 */
26861f28255Scgd 	for (u = user; u < &user[users]; u++)
26961f28255Scgd 		if (!strcmp(*u, line+1) && isowner(line+1, file))
27061f28255Scgd 			return(1);
27161f28255Scgd 	return(0);
27261f28255Scgd }
27361f28255Scgd 
27461f28255Scgd /*
27561f28255Scgd  * If root is removing a file on the local machine, allow it.
27661f28255Scgd  * If root is removing a file from a remote machine, only allow
27761f28255Scgd  * files sent from the remote machine to be removed.
27861f28255Scgd  * Normal users can only remove the file from where it was sent.
27961f28255Scgd  */
2802847add2Scgd int
isowner(const char * owner,const char * file)28104723c3fSchristos isowner(const char *owner, const char *file)
28261f28255Scgd {
28361f28255Scgd 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
28461f28255Scgd 		return(1);
28561f28255Scgd 	if (!strcmp(person, owner) && !strcmp(from, file+6))
28661f28255Scgd 		return(1);
28761f28255Scgd 	if (from != host)
28861f28255Scgd 		printf("%s: ", host);
28961f28255Scgd 	printf("%s: Permission denied\n", file);
29061f28255Scgd 	return(0);
29161f28255Scgd }
29261f28255Scgd 
29361f28255Scgd /*
29461f28255Scgd  * Check to see if we are sending files to a remote machine. If we are,
29561f28255Scgd  * then try removing files on the remote machine.
29661f28255Scgd  */
2972847add2Scgd void
rmremote(void)298895dc72aSwiz rmremote(void)
29961f28255Scgd {
300c6813048Schristos 	char *cp, *s;
30123b9fac0Smrg 	int i, rem;
30223b9fac0Smrg 	size_t len;
30361f28255Scgd 
304e6a91a09Smrg 	if (!remote)
30561f28255Scgd 		return;	/* not sending to a remote machine */
30661f28255Scgd 
30761f28255Scgd 	/*
30861f28255Scgd 	 * Flush stdout so the user can see what has been deleted
30961f28255Scgd 	 * while we wait (possibly) for the connection.
31061f28255Scgd 	 */
31161f28255Scgd 	fflush(stdout);
31261f28255Scgd 
313077acf50Smrg 	/* \5 RP space all */
314077acf50Smrg 	len = 1 + strlen(RP) + 1 + strlen(all ? "-all" : person);
31561f28255Scgd 	for (i = 0; i < users; i++) {
316077acf50Smrg 		len += strlen(user[i]) + 1;
31761f28255Scgd 	}
31861f28255Scgd 	for (i = 0; i < requests; i++) {
319077acf50Smrg 		len += snprintf(line, sizeof(line), " %d", requ[i]);
32061f28255Scgd 	}
3217262daa7Scgd 	/* newline nul */
3227262daa7Scgd 	len += 2;
323077acf50Smrg 	if (len > sizeof(line))
324077acf50Smrg 		s = malloc(len);
325077acf50Smrg 	else
326077acf50Smrg 		s = line;
327077acf50Smrg 	cp = s;
328077acf50Smrg 
329077acf50Smrg 	cp += snprintf(s, len, "\5%s %s", RP, all ? "-all" : person);
330077acf50Smrg 	for (i = 0; i < users; i++) {
331077acf50Smrg 		*cp++ = ' ';
332077acf50Smrg 		strncpy(cp, user[i], len - (cp - s) - 2);
333077acf50Smrg 		cp += strlen(cp);
334077acf50Smrg 	}
335077acf50Smrg 	for (i = 0; i < requests; i++) {
33672b0837aScgd 		(void)snprintf(cp, len - (cp - s) - 1, " %d", requ[i]);
337077acf50Smrg 		cp += strlen(cp);
338077acf50Smrg 	}
339077acf50Smrg 	cp[0] = '\n';
340077acf50Smrg 	cp[1] = '\0';
341077acf50Smrg 
342c6813048Schristos 	rem = getport(RM);
34361f28255Scgd 	if (rem < 0) {
34461f28255Scgd 		if (from != host)
34561f28255Scgd 			printf("%s: ", host);
34661f28255Scgd 		printf("connection to %s is down\n", RM);
34761f28255Scgd 	} else {
3485b6d0e7eSmrg 		struct sigaction osa, nsa;
3495b6d0e7eSmrg 
350f5a433a7Slukem 		if ((size_t)write(rem, s, len) != len)
35161f28255Scgd 			fatal("Lost connection");
352077acf50Smrg 		if (len > sizeof(line))
353077acf50Smrg 			(void)free(s);
3545b6d0e7eSmrg 		nsa.sa_handler = alarmer;
3555b6d0e7eSmrg 		sigemptyset(&nsa.sa_mask);
3565b6d0e7eSmrg 		sigaddset(&nsa.sa_mask, SIGALRM);
3575b6d0e7eSmrg 		nsa.sa_flags = 0;
3585b6d0e7eSmrg 		(void)sigaction(SIGALRM, &nsa, &osa);
3595b6d0e7eSmrg 		alarm(wait_time);
3605b6d0e7eSmrg 		while ((i = read(rem, line, sizeof(line))) > 0) {
36123b9fac0Smrg 			(void)fwrite(line, 1, (size_t)i, stdout);
3625b6d0e7eSmrg 			alarm(wait_time);
3635b6d0e7eSmrg 		}
3645b6d0e7eSmrg 		alarm(0);
3655b6d0e7eSmrg 		(void)sigaction(SIGALRM, &osa, NULL);
36661f28255Scgd 		(void)close(rem);
36761f28255Scgd 	}
36861f28255Scgd }
36961f28255Scgd 
3705b6d0e7eSmrg static void
alarmer(int s)371895dc72aSwiz alarmer(int s)
3725b6d0e7eSmrg {
3735b6d0e7eSmrg 	/* nothing */
3745b6d0e7eSmrg }
3755b6d0e7eSmrg 
37661f28255Scgd /*
37761f28255Scgd  * Return 1 if the filename begins with 'cf'
37861f28255Scgd  */
3792847add2Scgd int
iscf(const struct dirent * d)380895dc72aSwiz iscf(const struct dirent *d)
38161f28255Scgd {
38261f28255Scgd 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
38361f28255Scgd }
384