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