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. 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  * @(#)rmjob.c	8.2 (Berkeley) 4/28/95
30  * $FreeBSD: src/usr.sbin/lpr/common_source/rmjob.c,v 1.12.2.5 2001/06/25 01:00:56 gad Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/uio.h>
35 
36 #include <ctype.h>
37 #include <dirent.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #define psignal foil_gcc_psignal
44 #define	sys_siglist foil_gcc_siglist
45 #include <unistd.h>
46 #undef psignal
47 #undef sys_siglist
48 
49 #include "lp.h"
50 #include "lp.local.h"
51 #include "pathnames.h"
52 
53 /*
54  * rmjob - remove the specified jobs from the queue.
55  */
56 
57 /*
58  * Stuff for handling lprm specifications
59  */
60 static char	root[] = "root";
61 static int	all = 0;		/* eliminate all files (root only) */
62 static int	cur_daemon;		/* daemon's pid */
63 static char	current[7+MAXHOSTNAMELEN];  /* active control file name */
64 
65 extern uid_t	uid, euid;		/* real and effective user id's */
66 
67 static	void	alarmhandler(int _signo);
68 static	void	do_unlink(char *_file);
69 
70 void
71 rmjob(const char *printer)
72 {
73 	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 	seteuid(euid);
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 	seteuid(uid);
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 			seteuid(euid);
121 			assassinated = kill(cur_daemon, SIGINT) == 0;
122 			seteuid(uid);
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  */
146 int
147 lockchk(struct printer *pp, char *slockf)
148 {
149 	FILE *fp;
150 	int i, n;
151 
152 	seteuid(euid);
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 	seteuid(uid);
160 	if (!get_line(fp)) {
161 		fclose(fp);
162 		return(0);		/* no daemon present */
163 	}
164 	cur_daemon = atoi(line);
165 	if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
166 		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 	fclose(fp);
178 	return(1);
179 }
180 
181 /*
182  * Process a control file.
183  */
184 void
185 process(const struct printer *pp, char *file)
186 {
187 	FILE *cfp;
188 
189 	if (!chk(file))
190 		return;
191 	seteuid(euid);
192 	if ((cfp = fopen(file, "r")) == NULL)
193 		fatal(pp, "cannot open %s", file);
194 	seteuid(uid);
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 	fclose(cfp);
204 	do_unlink(file);
205 }
206 
207 static void
208 do_unlink(char *file)
209 {
210 	int	ret;
211 
212 	if (from_host != local_host)
213 		printf("%s: ", local_host);
214 	seteuid(euid);
215 	ret = unlink(file);
216 	seteuid(uid);
217 	printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
218 }
219 
220 /*
221  * Do the dirty work in checking
222  */
223 int
224 chk(char *file)
225 {
226 	int *r, n;
227 	char **u, *cp;
228 	FILE *cfp;
229 
230 	/*
231 	 * Check for valid cf file name (mostly checking current).
232 	 */
233 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
234 		return(0);
235 
236 	if (all && (from_host == local_host || !strcmp(from_host, file+6)))
237 		return(1);
238 
239 	/*
240 	 * get the owner's name from the control file.
241 	 */
242 	seteuid(euid);
243 	if ((cfp = fopen(file, "r")) == NULL)
244 		return(0);
245 	seteuid(uid);
246 	while (get_line(cfp)) {
247 		if (line[0] == 'P')
248 			break;
249 	}
250 	fclose(cfp);
251 	if (line[0] != 'P')
252 		return(0);
253 
254 	if (users == 0 && requests == 0)
255 		return(!strcmp(file, current) && isowner(line+1, file));
256 	/*
257 	 * Check the request list
258 	 */
259 	for (n = 0, cp = file+3; isdigit(*cp); )
260 		n = n * 10 + (*cp++ - '0');
261 	for (r = requ; r < &requ[requests]; r++)
262 		if (*r == n && isowner(line+1, file))
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))
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  */
279 int
280 isowner(char *owner, char *file)
281 {
282 	if (!strcmp(person, root) && (from_host == local_host ||
283 	    !strcmp(from_host, file+6)))
284 		return (1);
285 	if (!strcmp(person, owner) && !strcmp(from_host, file+6))
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  */
297 void
298 rmremote(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 	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 			fwrite(buf, 1, i, stdout);
365 		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  */
375 int
376 iscf(const struct dirent *d)
377 {
378 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
379 }
380 
381 void
382 alarmhandler(int signo __unused)
383 {
384 	/* the signal is ignored */
385 	/* (the '__unused' is just to avoid a compile-time warning) */
386 }
387