xref: /freebsd/usr.sbin/lpr/common_source/rmjob.c (revision f3c5273d315a64826d2149ac453ff8c4583ddbe8)
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 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)rmjob.c	8.2 (Berkeley) 4/28/95";
35 #endif /* not lint */
36 #endif
37 
38 #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/uio.h>
43 
44 #include <ctype.h>
45 #include <dirent.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #define psignal foil_gcc_psignal
53 #define	sys_siglist foil_gcc_siglist
54 #include <unistd.h>
55 #undef psignal
56 #undef sys_siglist
57 
58 #include "lp.h"
59 #include "lp.local.h"
60 #include "pathnames.h"
61 
62 /*
63  * rmjob - remove the specified jobs from the queue.
64  */
65 
66 /*
67  * Stuff for handling lprm specifications
68  */
69 static char	root[] = "root";
70 static int	all = 0;		/* eliminate all files (root only) */
71 static int	cur_daemon;		/* daemon's pid */
72 static char	current[7+MAXHOSTNAMELEN];  /* active control file name */
73 
74 static	void	alarmhandler(int _signo);
75 static	void	do_unlink(char *_file);
76 static int	 isowner(char *_owner, char *_file, const char *_cfhost);
77 
78 void
79 rmjob(const char *printer)
80 {
81 	register int i, nitems;
82 	int assassinated = 0;
83 	struct dirent **files;
84 	char *cp;
85 	struct printer myprinter, *pp = &myprinter;
86 
87 	init_printer(pp);
88 	if ((i = getprintcap(printer, pp)) < 0)
89 		fatal(pp, "getprintcap: %s", pcaperr(i));
90 	if ((cp = checkremote(pp))) {
91 		printf("Warning: %s\n", cp);
92 		free(cp);
93 	}
94 
95 	/*
96 	 * If the format was `lprm -' and the user isn't the super-user,
97 	 *  then fake things to look like he said `lprm user'.
98 	 */
99 	if (users < 0) {
100 		if (getuid() == 0)
101 			all = 1;	/* all files in local queue */
102 		else {
103 			user[0] = person;
104 			users = 1;
105 		}
106 	}
107 	if (!strcmp(person, "-all")) {
108 		if (from_host == local_host)
109 			fatal(pp, "The login name \"-all\" is reserved");
110 		all = 1;	/* all those from 'from_host' */
111 		person = root;
112 	}
113 
114 	PRIV_START
115 	if (chdir(pp->spool_dir) < 0)
116 		fatal(pp, "cannot chdir to spool directory");
117 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
118 		fatal(pp, "cannot access spool directory");
119 	PRIV_END
120 
121 	if (nitems) {
122 		/*
123 		 * Check for an active printer daemon (in which case we
124 		 *  kill it if it is reading our file) then remove stuff
125 		 *  (after which we have to restart the daemon).
126 		 */
127 		if (lockchk(pp, pp->lock_file) && chk(current)) {
128 			PRIV_START
129 			assassinated = kill(cur_daemon, SIGINT) == 0;
130 			PRIV_END
131 			if (!assassinated)
132 				fatal(pp, "cannot kill printer daemon");
133 		}
134 		/*
135 		 * process the files
136 		 */
137 		for (i = 0; i < nitems; i++)
138 			process(pp, files[i]->d_name);
139 	}
140 	rmremote(pp);
141 	/*
142 	 * Restart the printer daemon if it was killed
143 	 */
144 	if (assassinated && !startdaemon(pp))
145 		fatal(pp, "cannot restart printer daemon\n");
146 	exit(0);
147 }
148 
149 /*
150  * Process a lock file: collect the pid of the active
151  *  daemon and the file name of the active spool entry.
152  * Return boolean indicating existence of a lock file.
153  */
154 int
155 lockchk(struct printer *pp, char *slockf)
156 {
157 	register FILE *fp;
158 	register int i, n;
159 
160 	PRIV_START
161 	if ((fp = fopen(slockf, "r")) == NULL) {
162 		if (errno == EACCES)
163 			fatal(pp, "%s: %s", slockf, strerror(errno));
164 		else
165 			return(0);
166 	}
167 	PRIV_END
168 	if (!get_line(fp)) {
169 		(void) fclose(fp);
170 		return(0);		/* no daemon present */
171 	}
172 	cur_daemon = atoi(line);
173 	if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
174 		(void) fclose(fp);
175 		return(0);		/* no daemon present */
176 	}
177 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
178 		if (i > 5) {
179 			n = 1;
180 			break;
181 		}
182 		sleep(i);
183 	}
184 	current[n-1] = '\0';
185 	(void) fclose(fp);
186 	return(1);
187 }
188 
189 /*
190  * Process a control file.
191  */
192 void
193 process(const struct printer *pp, char *file)
194 {
195 	FILE *cfp;
196 
197 	if (!chk(file))
198 		return;
199 	PRIV_START
200 	if ((cfp = fopen(file, "r")) == NULL)
201 		fatal(pp, "cannot open %s", file);
202 	PRIV_END
203 	while (get_line(cfp)) {
204 		switch (line[0]) {
205 		case 'U':  /* unlink associated files */
206 			if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
207 				break;
208 			do_unlink(line+1);
209 		}
210 	}
211 	(void) fclose(cfp);
212 	do_unlink(file);
213 }
214 
215 static void
216 do_unlink(char *file)
217 {
218 	int	ret;
219 
220 	if (from_host != local_host)
221 		printf("%s: ", local_host);
222 	PRIV_START
223 	ret = unlink(file);
224 	PRIV_END
225 	printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
226 }
227 
228 /*
229  * Do the dirty work in checking
230  */
231 int
232 chk(char *file)
233 {
234 	int *r, jnum;
235 	char **u;
236 	const char *cfhost;
237 	FILE *cfp;
238 
239 	/*
240 	 * Check for valid cf file name (mostly checking current).
241 	 */
242 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
243 		return(0);
244 
245 	jnum = calc_jobnum(file, &cfhost);
246 	if (all && (from_host == local_host || !strcmp(from_host, cfhost)))
247 		return(1);
248 
249 	/*
250 	 * get the owner's name from the control file.
251 	 */
252 	PRIV_START
253 	if ((cfp = fopen(file, "r")) == NULL)
254 		return(0);
255 	PRIV_END
256 	while (get_line(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, cfhost));
266 	/*
267 	 * Check the request list
268 	 */
269 	for (r = requ; r < &requ[requests]; r++)
270 		if (*r == jnum && isowner(line+1, file, cfhost))
271 			return(1);
272 	/*
273 	 * Check to see if it's in the user list
274 	 */
275 	for (u = user; u < &user[users]; u++)
276 		if (!strcmp(*u, line+1) && isowner(line+1, file, cfhost))
277 			return(1);
278 	return(0);
279 }
280 
281 /*
282  * If root is removing a file on the local machine, allow it.
283  * If root is removing a file from a remote machine, only allow
284  * files sent from the remote machine to be removed.
285  * Normal users can only remove the file from where it was sent.
286  */
287 static int
288 isowner(char *owner, char *file, const char *cfhost)
289 {
290 	if (!strcmp(person, root) && (from_host == local_host ||
291 	    !strcmp(from_host, cfhost)))
292 		return (1);
293 	if (!strcmp(person, owner) && !strcmp(from_host, cfhost))
294 		return (1);
295 	if (from_host != local_host)
296 		printf("%s: ", local_host);
297 	printf("%s: Permission denied\n", file);
298 	return(0);
299 }
300 
301 /*
302  * Check to see if we are sending files to a remote machine. If we are,
303  * then try removing files on the remote machine.
304  */
305 void
306 rmremote(const struct printer *pp)
307 {
308 	int i, elem, firstreq, niov, rem, totlen;
309 	char buf[BUFSIZ];
310 	void (*savealrm)(int);
311 	struct iovec *iov;
312 
313 	if (!pp->remote)
314 		return;	/* not sending to a remote machine */
315 
316 	/*
317 	 * Flush stdout so the user can see what has been deleted
318 	 * while we wait (possibly) for the connection.
319 	 */
320 	fflush(stdout);
321 
322 	/*
323 	 * Counting:
324 	 *	4 == "\5" + remote_queue + " " + person
325 	 *	2 * users == " " + user[i] for each user
326 	 *	requests == asprintf results for each request
327 	 *	1 == "\n"
328 	 * Although laborious, doing it this way makes it possible for
329 	 * us to process requests of indeterminate length without
330 	 * applying an arbitrary limit.  Arbitrary Limits Are Bad (tm).
331 	 */
332 	if (users > 0)
333 		niov = 4 + 2 * users + requests + 1;
334 	else
335 		niov = 4 + requests + 1;
336 	iov = malloc(niov * sizeof *iov);
337 	if (iov == NULL)
338 		fatal(pp, "out of memory in rmremote()");
339 	iov[0].iov_base = "\5";
340 	iov[1].iov_base = pp->remote_queue;
341 	iov[2].iov_base = " ";
342 	iov[3].iov_base = all ? "-all" : person;
343 	elem = 4;
344 	for (i = 0; i < users; i++) {
345 		iov[elem].iov_base = " ";
346 		iov[elem + 1].iov_base = user[i];
347 		elem += 2;
348 	}
349 	firstreq = elem;
350 	for (i = 0; i < requests; i++) {
351 		asprintf((char **)&iov[elem].iov_base, " %d", requ[i]);
352 		if (iov[elem].iov_base == 0)
353 			fatal(pp, "out of memory in rmremote()");
354 		elem++;
355 	}
356 	iov[elem++].iov_base = "\n";
357 	for (totlen = i = 0; i < niov; i++)
358 		totlen += (iov[i].iov_len = strlen(iov[i].iov_base));
359 
360 	savealrm = signal(SIGALRM, alarmhandler);
361 	alarm(pp->conn_timeout);
362 	rem = getport(pp, pp->remote_host, 0);
363 	(void)signal(SIGALRM, savealrm);
364 	if (rem < 0) {
365 		if (from_host != local_host)
366 			printf("%s: ", local_host);
367 		printf("connection to %s is down\n", pp->remote_host);
368 	} else {
369 		if (writev(rem, iov, niov) != totlen)
370 			fatal(pp, "Lost connection");
371 		while ((i = read(rem, buf, sizeof(buf))) > 0)
372 			(void) fwrite(buf, 1, i, stdout);
373 		(void) close(rem);
374 	}
375 	for (i = 0; i < requests; i++)
376 		free(iov[firstreq + i].iov_base);
377 	free(iov);
378 }
379 
380 /*
381  * Return 1 if the filename begins with 'cf'
382  */
383 int
384 iscf(const struct dirent *d)
385 {
386 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
387 }
388 
389 void
390 alarmhandler(int signo __unused)
391 {
392 	/* the signal is ignored */
393 	/* (the '__unused' is just to avoid a compile-time warning) */
394 }
395