xref: /freebsd/usr.sbin/lpr/common_source/rmjob.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)rmjob.c	8.2 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/uio.h>
44 
45 #include <ctype.h>
46 #include <dirent.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 extern uid_t	uid, euid;		/* real and effective user id's */
75 
76 static	void	alarmhandler __P((int));
77 static	void	do_unlink __P((char *));
78 
79 void
80 rmjob(printer)
81 	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)
111 			fatal(pp, "The login name \"-all\" is reserved");
112 		all = 1;	/* all those from 'from' */
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  */
156 int
157 lockchk(pp, s)
158 	struct printer *pp;
159 	char *s;
160 {
161 	register FILE *fp;
162 	register int i, n;
163 
164 	seteuid(euid);
165 	if ((fp = fopen(s, "r")) == NULL) {
166 		if (errno == EACCES)
167 			fatal(pp, "%s: %s", s, strerror(errno));
168 		else
169 			return(0);
170 	}
171 	seteuid(uid);
172 	if (!getline(fp)) {
173 		(void) fclose(fp);
174 		return(0);		/* no daemon present */
175 	}
176 	cur_daemon = atoi(line);
177 	if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
178 		(void) fclose(fp);
179 		return(0);		/* no daemon present */
180 	}
181 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
182 		if (i > 5) {
183 			n = 1;
184 			break;
185 		}
186 		sleep(i);
187 	}
188 	current[n-1] = '\0';
189 	(void) fclose(fp);
190 	return(1);
191 }
192 
193 /*
194  * Process a control file.
195  */
196 void
197 process(pp, file)
198 	const struct printer *pp;
199 	char *file;
200 {
201 	FILE *cfp;
202 
203 	if (!chk(file))
204 		return;
205 	seteuid(euid);
206 	if ((cfp = fopen(file, "r")) == NULL)
207 		fatal(pp, "cannot open %s", file);
208 	seteuid(uid);
209 	while (getline(cfp)) {
210 		switch (line[0]) {
211 		case 'U':  /* unlink associated files */
212 			if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
213 				break;
214 			do_unlink(line+1);
215 		}
216 	}
217 	(void) fclose(cfp);
218 	do_unlink(file);
219 }
220 
221 static void
222 do_unlink(file)
223 	char *file;
224 {
225 	int	ret;
226 
227 	if (from != host)
228 		printf("%s: ", host);
229 	seteuid(euid);
230 	ret = unlink(file);
231 	seteuid(uid);
232 	printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
233 }
234 
235 /*
236  * Do the dirty work in checking
237  */
238 int
239 chk(file)
240 	char *file;
241 {
242 	register int *r, n;
243 	register char **u, *cp;
244 	FILE *cfp;
245 
246 	/*
247 	 * Check for valid cf file name (mostly checking current).
248 	 */
249 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
250 		return(0);
251 
252 	if (all && (from == host || !strcmp(from, file+6)))
253 		return(1);
254 
255 	/*
256 	 * get the owner's name from the control file.
257 	 */
258 	seteuid(euid);
259 	if ((cfp = fopen(file, "r")) == NULL)
260 		return(0);
261 	seteuid(uid);
262 	while (getline(cfp)) {
263 		if (line[0] == 'P')
264 			break;
265 	}
266 	(void) fclose(cfp);
267 	if (line[0] != 'P')
268 		return(0);
269 
270 	if (users == 0 && requests == 0)
271 		return(!strcmp(file, current) && isowner(line+1, file));
272 	/*
273 	 * Check the request list
274 	 */
275 	for (n = 0, cp = file+3; isdigit(*cp); )
276 		n = n * 10 + (*cp++ - '0');
277 	for (r = requ; r < &requ[requests]; r++)
278 		if (*r == n && isowner(line+1, file))
279 			return(1);
280 	/*
281 	 * Check to see if it's in the user list
282 	 */
283 	for (u = user; u < &user[users]; u++)
284 		if (!strcmp(*u, line+1) && isowner(line+1, file))
285 			return(1);
286 	return(0);
287 }
288 
289 /*
290  * If root is removing a file on the local machine, allow it.
291  * If root is removing a file from a remote machine, only allow
292  * files sent from the remote machine to be removed.
293  * Normal users can only remove the file from where it was sent.
294  */
295 int
296 isowner(owner, file)
297 	char *owner, *file;
298 {
299 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
300 		return(1);
301 	if (!strcmp(person, owner) && !strcmp(from, file+6))
302 		return(1);
303 	if (from != host)
304 		printf("%s: ", host);
305 	printf("%s: Permission denied\n", file);
306 	return(0);
307 }
308 
309 /*
310  * Check to see if we are sending files to a remote machine. If we are,
311  * then try removing files on the remote machine.
312  */
313 void
314 rmremote(pp)
315 	const struct printer *pp;
316 {
317 	int i, rem, niov, totlen;
318 	char buf[BUFSIZ];
319 	void (*savealrm)(int);
320 	struct iovec *iov;
321 
322 	if (!pp->remote)
323 		return;	/* not sending to a remote machine */
324 
325 	/*
326 	 * Flush stdout so the user can see what has been deleted
327 	 * while we wait (possibly) for the connection.
328 	 */
329 	fflush(stdout);
330 
331 	/*
332 	 * Counting:
333 	 *	4 == "\5" + remote_queue + " " + person
334 	 *	2 * users == " " + user[i] for each user
335 	 *	requests == asprintf results for each request
336 	 *	1 == "\n"
337 	 * Although laborious, doing it this way makes it possible for
338 	 * us to process requests of indeterminate length without
339 	 * applying an arbitrary limit.  Arbitrary Limits Are Bad (tm).
340 	 */
341 	niov = 4 + 2 * users + requests + 1;
342 	iov = malloc(niov * sizeof *iov);
343 	if (iov == 0)
344 		fatal(pp, "out of memory");
345 	iov[0].iov_base = "\5";
346 	iov[1].iov_base = pp->remote_queue;
347 	iov[2].iov_base = " ";
348 	iov[3].iov_base = all ? "-all" : person;
349 	for (i = 0; i < users; i++) {
350 		iov[4 + 2 * i].iov_base = " ";
351 		iov[4 + 2 * i + 1].iov_base = user[i];
352 	}
353 	for (i = 0; i < requests; i++) {
354 		asprintf(&iov[4 + 2 * users + i].iov_base, " %d", requ[i]);
355 		if (iov[4 + 2 * users + i].iov_base == 0)
356 			fatal(pp, "out of memory");
357 	}
358 	iov[4 + 2 * users + requests].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)
368 			printf("%s: ", 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[4 + 2 * users + i].iov_base);
379 	free(iov);
380 }
381 
382 /*
383  * Return 1 if the filename begins with 'cf'
384  */
385 int
386 iscf(d)
387 	struct dirent *d;
388 {
389 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
390 }
391 
392 void
393 alarmhandler(signo)
394 	int signo;
395 {
396 	/* ignored */
397 }
398