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