1
/* $OpenBSD: rmjob.c,v 1.9 1998/04/22 14:44:08 deraadt Exp $ */
4
* Copyright (c) 1983, 1993
5
* The Regents of the University of California. All rights reserved.
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
* 3. All advertising materials mentioning features or use of this software
16
* must display the following acknowledgement:
17
* This product includes software developed by the University of
18
* California, Berkeley and its contributors.
19
* 4. Neither the name of the University nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
static char sccsid[] = "@(#)rmjob.c 8.2 (Berkeley) 4/28/95";
40
static char rcsid[] = "$OpenBSD: rmjob.c,v 1.9 1998/04/22 14:44:08 deraadt Exp $";
44
#include <sys/param.h>
56
#include "pathnames.h"
59
* rmjob - remove the specified jobs from the queue.
63
* Stuff for handling lprm specifications
65
extern char *user[]; /* users to process */
66
extern int users; /* # of users in user array */
67
extern int requ[]; /* job number of spool entries */
68
extern int requests; /* # of spool requests */
69
extern char *person; /* name of person doing lprm */
71
static char root[] = "root";
72
static int all = 0; /* eliminate all files (root only) */
73
static int cur_daemon; /* daemon's pid */
74
static char current[40]; /* active control file name */
76
extern uid_t uid, euid; /* real and effective user id's */
78
static void do_unlink __P((char *));
83
register int i, nitems;
85
struct dirent **files;
88
if ((i = cgetent(&bp, printcapdb, printer)) == -2)
89
fatal("can't open printer description file");
91
fatal("unknown printer");
93
fatal("potential reference loop detected in printcap file");
94
if (cgetstr(bp, "lp", &LP) < 0)
96
if (cgetstr(bp, "rp", &RP) < 0)
98
if (cgetstr(bp, "sd", &SD) < 0)
100
if (cgetstr(bp,"lo", &LO) < 0)
102
cgetstr(bp, "rm", &RM);
103
if ((cp = checkremote()))
104
printf("Warning: %s\n", cp);
107
* If the format was `lprm -' and the user isn't the super-user,
108
* then fake things to look like he said `lprm user'.
112
all = 1; /* all files in local queue */
118
if (!strcmp(person, "-all")) {
120
fatal("The login name \"-all\" is reserved");
121
all = 1; /* all those from 'from' */
127
fatal("cannot chdir to spool directory");
128
if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
129
fatal("cannot access spool directory");
134
* Check for an active printer daemon (in which case we
135
* kill it if it is reading our file) then remove stuff
136
* (after which we have to restart the daemon).
138
if (lockchk(LO) && chk(current)) {
140
assasinated = kill(cur_daemon, SIGINT) == 0;
143
fatal("cannot kill printer daemon");
148
for (i = 0; i < nitems; i++)
149
process(files[i]->d_name);
153
* Restart the printer daemon if it was killed
155
if (assasinated && !startdaemon(printer))
156
fatal("cannot restart printer daemon\n");
161
* Process a lock file: collect the pid of the active
162
* daemon and the file name of the active spool entry.
163
* Return boolean indicating existence of a lock file.
173
if ((fp = fopen(s, "r")) == NULL) {
175
fatal("can't access lock file");
182
return(0); /* no daemon present */
184
cur_daemon = atoi(line);
185
if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
187
return(0); /* no daemon present */
189
for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
202
* Process a control file.
213
if ((cfp = fopen(file, "r")) == NULL)
214
fatal("cannot open %s", file);
216
while (getline(cfp)) {
218
case 'U': /* unlink associated files */
219
if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
235
printf("%s: ", host);
239
printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
243
* Do the dirty work in checking
250
register char **u, *cp;
254
* Check for valid cf file name (mostly checking current).
256
if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
259
if (all && (from == host || !strcmp(from, file+6)))
263
* get the owner's name from the control file.
266
if ((cfp = fopen(file, "r")) == NULL)
269
while (getline(cfp)) {
277
if (users == 0 && requests == 0)
278
return(!strcmp(file, current) && isowner(line+1, file));
280
* Check the request list
282
for (n = 0, cp = file+3; isdigit(*cp); )
283
n = n * 10 + (*cp++ - '0');
284
for (r = requ; r < &requ[requests]; r++)
285
if (*r == n && isowner(line+1, file))
288
* Check to see if it's in the user list
290
for (u = user; u < &user[users]; u++)
291
if (!strcmp(*u, line+1) && isowner(line+1, file))
297
* If root is removing a file on the local machine, allow it.
298
* If root is removing a file from a remote machine, only allow
299
* files sent from the remote machine to be removed.
300
* Normal users can only remove the file from where it was sent.
306
if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
308
if (!strcmp(person, owner) && !strcmp(from, file+6))
311
printf("%s: ", host);
312
printf("%s: Permission denied\n", file);
317
* Check to see if we are sending files to a remote machine. If we are,
318
* then try removing files on the remote machine.
328
return; /* not sending to a remote machine */
331
* Flush stdout so the user can see what has been deleted
332
* while we wait (possibly) for the connection.
336
(void)snprintf(buf, sizeof(buf)-2, "\5%s %s", RP, all ? "-all" : person);
337
cp = buf + strlen(buf);
338
for (i = 0; i < users && cp-buf+1+strlen(user[i]) < sizeof buf - 2; i++) {
343
for (i = 0; i < requests && cp-buf+10 < sizeof(buf) - 2; i++) {
345
(void) sprintf(cp, " %d", requ[i]);
348
rem = getport(RM, 0);
351
printf("%s: ", host);
352
printf("connection to %s is down\n", RM);
355
if (write(rem, buf, i) != i)
356
fatal("Lost connection");
357
while ((i = read(rem, buf, sizeof(buf))) > 0)
358
(void) fwrite(buf, 1, i, stdout);
364
* Return 1 if the filename begins with 'cf'
370
return(d->d_name[0] == 'c' && d->d_name[1] == 'f');