2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
33 #include <sys/param.h>
43 #define psignal foil_gcc_psignal
44 #define sys_siglist foil_gcc_siglist
51 #include "pathnames.h"
54 * rmjob - remove the specified jobs from the queue.
58 * Stuff for handling lprm specifications
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 */
65 extern uid_t uid, euid; /* real and effective user id's */
67 static void alarmhandler(int _signo);
68 static void do_unlink(char *_file);
71 rmjob(const char *printer)
75 struct dirent **files;
77 struct printer myprinter, *pp = &myprinter;
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);
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'.
93 all = 1; /* all files in local queue */
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' */
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");
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).
119 if (lockchk(pp, pp->lock_file) && chk(current)) {
121 assassinated = kill(cur_daemon, SIGINT) == 0;
124 fatal(pp, "cannot kill printer daemon");
129 for (i = 0; i < nitems; i++)
130 process(pp, files[i]->d_name);
134 * Restart the printer daemon if it was killed
136 if (assassinated && !startdaemon(pp))
137 fatal(pp, "cannot restart printer daemon\n");
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.
147 lockchk(struct printer *pp, char *slockf)
153 if ((fp = fopen(slockf, "r")) == NULL) {
155 fatal(pp, "%s: %s", slockf, strerror(errno));
162 return(0); /* no daemon present */
164 cur_daemon = atoi(line);
165 if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
167 return(0); /* no daemon present */
169 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
182 * Process a control file.
185 process(const struct printer *pp, char *file)
192 if ((cfp = fopen(file, "r")) == NULL)
193 fatal(pp, "cannot open %s", file);
195 while (getline(cfp)) {
197 case 'U': /* unlink associated files */
198 if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
208 do_unlink(char *file)
212 if (from_host != local_host)
213 printf("%s: ", local_host);
217 printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
221 * Do the dirty work in checking
231 * Check for valid cf file name (mostly checking current).
233 if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
236 if (all && (from_host == local_host || !strcmp(from_host, file+6)))
240 * get the owner's name from the control file.
243 if ((cfp = fopen(file, "r")) == NULL)
246 while (getline(cfp)) {
254 if (users == 0 && requests == 0)
255 return(!strcmp(file, current) && isowner(line+1, file));
257 * Check the request list
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))
265 * Check to see if it's in the user list
267 for (u = user; u < &user[users]; u++)
268 if (!strcmp(*u, line+1) && isowner(line+1, file))
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.
280 isowner(char *owner, char *file)
282 if (!strcmp(person, root) && (from_host == local_host ||
283 !strcmp(from_host, file+6)))
285 if (!strcmp(person, owner) && !strcmp(from_host, file+6))
287 if (from_host != local_host)
288 printf("%s: ", local_host);
289 printf("%s: Permission denied\n", file);
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.
298 rmremote(const struct printer *pp)
300 int i, elem, firstreq, niov, rem, totlen;
302 void (*savealrm)(int);
306 return; /* not sending to a remote machine */
309 * Flush stdout so the user can see what has been deleted
310 * while we wait (possibly) for the connection.
316 * 4 == "\5" + remote_queue + " " + person
317 * 2 * users == " " + user[i] for each user
318 * requests == asprintf results for each request
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).
325 niov = 4 + 2 * users + requests + 1;
327 niov = 4 + requests + 1;
328 iov = malloc(niov * sizeof *iov);
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;
336 for (i = 0; i < users; i++) {
337 iov[elem].iov_base = " ";
338 iov[elem + 1].iov_base = user[i];
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()");
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));
352 savealrm = signal(SIGALRM, alarmhandler);
353 alarm(pp->conn_timeout);
354 rem = getport(pp, pp->remote_host, 0);
355 signal(SIGALRM, savealrm);
357 if (from_host != local_host)
358 printf("%s: ", local_host);
359 printf("connection to %s is down\n", pp->remote_host);
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);
367 for (i = 0; i < requests; i++)
368 free(iov[firstreq + i].iov_base);
373 * Return 1 if the filename begins with 'cf'
376 iscf(const struct dirent *d)
378 return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
382 alarmhandler(int signo __unused)
384 /* the signal is ignored */
385 /* (the '__unused' is just to avoid a compile-time warning) */