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