rmjob.c revision 121525
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char sccsid[] = "@(#)rmjob.c	8.2 (Berkeley) 4/28/95";
37#endif /* not lint */
38#endif
39
40#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
41__FBSDID("$FreeBSD: head/usr.sbin/lpr/common_source/rmjob.c 121525 2003-10-26 03:01:24Z peter $");
42
43#include <sys/param.h>
44#include <sys/uio.h>
45
46#include <ctype.h>
47#include <dirent.h>
48#include <errno.h>
49#include <signal.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#define psignal foil_gcc_psignal
54#define	sys_siglist foil_gcc_siglist
55#include <unistd.h>
56#undef psignal
57#undef sys_siglist
58
59#include "lp.h"
60#include "lp.local.h"
61#include "pathnames.h"
62
63/*
64 * rmjob - remove the specified jobs from the queue.
65 */
66
67/*
68 * Stuff for handling lprm specifications
69 */
70static char	root[] = "root";
71static int	all = 0;		/* eliminate all files (root only) */
72static int	cur_daemon;		/* daemon's pid */
73static char	current[7+MAXHOSTNAMELEN];  /* active control file name */
74
75extern uid_t	uid, euid;		/* real and effective user id's */
76
77static	void	alarmhandler(int _signo);
78static	void	do_unlink(char *_file);
79
80void
81rmjob(const char *printer)
82{
83	register int i, nitems;
84	int assasinated = 0;
85	struct dirent **files;
86	char *cp;
87	struct printer myprinter, *pp = &myprinter;
88
89	init_printer(pp);
90	if ((i = getprintcap(printer, pp)) < 0)
91		fatal(pp, "getprintcap: %s", pcaperr(i));
92	if ((cp = checkremote(pp))) {
93		printf("Warning: %s\n", cp);
94		free(cp);
95	}
96
97	/*
98	 * If the format was `lprm -' and the user isn't the super-user,
99	 *  then fake things to look like he said `lprm user'.
100	 */
101	if (users < 0) {
102		if (getuid() == 0)
103			all = 1;	/* all files in local queue */
104		else {
105			user[0] = person;
106			users = 1;
107		}
108	}
109	if (!strcmp(person, "-all")) {
110		if (from_host == local_host)
111			fatal(pp, "The login name \"-all\" is reserved");
112		all = 1;	/* all those from 'from_host' */
113		person = root;
114	}
115
116	seteuid(euid);
117	if (chdir(pp->spool_dir) < 0)
118		fatal(pp, "cannot chdir to spool directory");
119	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
120		fatal(pp, "cannot access spool directory");
121	seteuid(uid);
122
123	if (nitems) {
124		/*
125		 * Check for an active printer daemon (in which case we
126		 *  kill it if it is reading our file) then remove stuff
127		 *  (after which we have to restart the daemon).
128		 */
129		if (lockchk(pp, pp->lock_file) && chk(current)) {
130			seteuid(euid);
131			assasinated = kill(cur_daemon, SIGINT) == 0;
132			seteuid(uid);
133			if (!assasinated)
134				fatal(pp, "cannot kill printer daemon");
135		}
136		/*
137		 * process the files
138		 */
139		for (i = 0; i < nitems; i++)
140			process(pp, files[i]->d_name);
141	}
142	rmremote(pp);
143	/*
144	 * Restart the printer daemon if it was killed
145	 */
146	if (assasinated && !startdaemon(pp))
147		fatal(pp, "cannot restart printer daemon\n");
148	exit(0);
149}
150
151/*
152 * Process a lock file: collect the pid of the active
153 *  daemon and the file name of the active spool entry.
154 * Return boolean indicating existence of a lock file.
155 */
156int
157lockchk(struct printer *pp, char *slockf)
158{
159	register FILE *fp;
160	register int i, n;
161
162	seteuid(euid);
163	if ((fp = fopen(slockf, "r")) == NULL) {
164		if (errno == EACCES)
165			fatal(pp, "%s: %s", slockf, strerror(errno));
166		else
167			return(0);
168	}
169	seteuid(uid);
170	if (!getline(fp)) {
171		(void) fclose(fp);
172		return(0);		/* no daemon present */
173	}
174	cur_daemon = atoi(line);
175	if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
176		(void) fclose(fp);
177		return(0);		/* no daemon present */
178	}
179	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
180		if (i > 5) {
181			n = 1;
182			break;
183		}
184		sleep(i);
185	}
186	current[n-1] = '\0';
187	(void) fclose(fp);
188	return(1);
189}
190
191/*
192 * Process a control file.
193 */
194void
195process(const struct printer *pp, char *file)
196{
197	FILE *cfp;
198
199	if (!chk(file))
200		return;
201	seteuid(euid);
202	if ((cfp = fopen(file, "r")) == NULL)
203		fatal(pp, "cannot open %s", file);
204	seteuid(uid);
205	while (getline(cfp)) {
206		switch (line[0]) {
207		case 'U':  /* unlink associated files */
208			if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
209				break;
210			do_unlink(line+1);
211		}
212	}
213	(void) fclose(cfp);
214	do_unlink(file);
215}
216
217static void
218do_unlink(char *file)
219{
220	int	ret;
221
222	if (from_host != local_host)
223		printf("%s: ", local_host);
224	seteuid(euid);
225	ret = unlink(file);
226	seteuid(uid);
227	printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
228}
229
230/*
231 * Do the dirty work in checking
232 */
233int
234chk(char *file)
235{
236	register int *r, n;
237	register char **u, *cp;
238	FILE *cfp;
239
240	/*
241	 * Check for valid cf file name (mostly checking current).
242	 */
243	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
244		return(0);
245
246	if (all && (from_host == local_host || !strcmp(from_host, file+6)))
247		return(1);
248
249	/*
250	 * get the owner's name from the control file.
251	 */
252	seteuid(euid);
253	if ((cfp = fopen(file, "r")) == NULL)
254		return(0);
255	seteuid(uid);
256	while (getline(cfp)) {
257		if (line[0] == 'P')
258			break;
259	}
260	(void) fclose(cfp);
261	if (line[0] != 'P')
262		return(0);
263
264	if (users == 0 && requests == 0)
265		return(!strcmp(file, current) && isowner(line+1, file));
266	/*
267	 * Check the request list
268	 */
269	for (n = 0, cp = file+3; isdigit(*cp); )
270		n = n * 10 + (*cp++ - '0');
271	for (r = requ; r < &requ[requests]; r++)
272		if (*r == n && isowner(line+1, file))
273			return(1);
274	/*
275	 * Check to see if it's in the user list
276	 */
277	for (u = user; u < &user[users]; u++)
278		if (!strcmp(*u, line+1) && isowner(line+1, file))
279			return(1);
280	return(0);
281}
282
283/*
284 * If root is removing a file on the local machine, allow it.
285 * If root is removing a file from a remote machine, only allow
286 * files sent from the remote machine to be removed.
287 * Normal users can only remove the file from where it was sent.
288 */
289int
290isowner(char *owner, char *file)
291{
292	if (!strcmp(person, root) && (from_host == local_host ||
293	    !strcmp(from_host, file+6)))
294		return (1);
295	if (!strcmp(person, owner) && !strcmp(from_host, file+6))
296		return (1);
297	if (from_host != local_host)
298		printf("%s: ", local_host);
299	printf("%s: Permission denied\n", file);
300	return(0);
301}
302
303/*
304 * Check to see if we are sending files to a remote machine. If we are,
305 * then try removing files on the remote machine.
306 */
307void
308rmremote(const struct printer *pp)
309{
310	int i, elem, firstreq, niov, rem, totlen;
311	char buf[BUFSIZ];
312	void (*savealrm)(int);
313	struct iovec *iov;
314
315	if (!pp->remote)
316		return;	/* not sending to a remote machine */
317
318	/*
319	 * Flush stdout so the user can see what has been deleted
320	 * while we wait (possibly) for the connection.
321	 */
322	fflush(stdout);
323
324	/*
325	 * Counting:
326	 *	4 == "\5" + remote_queue + " " + person
327	 *	2 * users == " " + user[i] for each user
328	 *	requests == asprintf results for each request
329	 *	1 == "\n"
330	 * Although laborious, doing it this way makes it possible for
331	 * us to process requests of indeterminate length without
332	 * applying an arbitrary limit.  Arbitrary Limits Are Bad (tm).
333	 */
334	if (users > 0)
335		niov = 4 + 2 * users + requests + 1;
336	else
337		niov = 4 + requests + 1;
338	iov = malloc(niov * sizeof *iov);
339	if (iov == 0)
340		fatal(pp, "out of memory in rmremote()");
341	iov[0].iov_base = "\5";
342	iov[1].iov_base = pp->remote_queue;
343	iov[2].iov_base = " ";
344	iov[3].iov_base = all ? "-all" : person;
345	elem = 4;
346	for (i = 0; i < users; i++) {
347		iov[elem].iov_base = " ";
348		iov[elem + 1].iov_base = user[i];
349		elem += 2;
350	}
351	firstreq = elem;
352	for (i = 0; i < requests; i++) {
353		asprintf((char **)&iov[elem].iov_base, " %d", requ[i]);
354		if (iov[elem].iov_base == 0)
355			fatal(pp, "out of memory in rmremote()");
356		elem++;
357	}
358	iov[elem++].iov_base = "\n";
359	for (totlen = i = 0; i < niov; i++)
360		totlen += (iov[i].iov_len = strlen(iov[i].iov_base));
361
362	savealrm = signal(SIGALRM, alarmhandler);
363	alarm(pp->conn_timeout);
364	rem = getport(pp, pp->remote_host, 0);
365	(void)signal(SIGALRM, savealrm);
366	if (rem < 0) {
367		if (from_host != local_host)
368			printf("%s: ", local_host);
369		printf("connection to %s is down\n", pp->remote_host);
370	} else {
371		if (writev(rem, iov, niov) != totlen)
372			fatal(pp, "Lost connection");
373		while ((i = read(rem, buf, sizeof(buf))) > 0)
374			(void) fwrite(buf, 1, i, stdout);
375		(void) close(rem);
376	}
377	for (i = 0; i < requests; i++)
378		free(iov[firstreq + i].iov_base);
379	free(iov);
380}
381
382/*
383 * Return 1 if the filename begins with 'cf'
384 */
385int
386iscf(struct dirent *d)
387{
388	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
389}
390
391void
392alarmhandler(int signo __unused)
393{
394	/* the signal is ignored */
395	/* (the '__unused' is just to avoid a compile-time warning) */
396}
397