* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)rmjob.c 8.1 (Berkeley) 6/6/93";
* rmjob - remove the specified jobs from the queue.
* Stuff for handling lprm specifications
extern char *user
[]; /* users to process */
extern int users
; /* # of users in user array */
extern int requ
[]; /* job number of spool entries */
extern int requests
; /* # of spool requests */
extern char *person
; /* name of person doing lprm */
static char root
[] = "root";
static int all
= 0; /* eliminate all files (root only) */
static int cur_daemon
; /* daemon's pid */
static char current
[40]; /* active control file name */
if ((i
= cgetent(&bp
, printcapdb
, printer
)) == -2)
fatal("can't open printer description file");
fatal("unknown printer");
fatal("potential reference loop detected in printcap file");
if (cgetstr(bp
, "lp", &LP
) < 0)
if (cgetstr(bp
, "rp", &RP
) < 0)
if (cgetstr(bp
, "sd", &SD
) < 0)
if (cgetstr(bp
,"lo", &LO
) < 0)
printf("Warning: %s\n", cp
);
* If the format was `lprm -' and the user isn't the super-user,
* then fake things to look like he said `lprm user'.
all
= 1; /* all files in local queue */
if (!strcmp(person
, "-all")) {
fatal("The login name \"-all\" is reserved");
all
= 1; /* all those from 'from' */
fatal("cannot chdir to spool directory");
if ((nitems
= scandir(".", &files
, iscf
, NULL
)) < 0)
fatal("cannot access spool directory");
* Check for an active printer daemon (in which case we
* kill it if it is reading our file) then remove stuff
* (after which we have to restart the daemon).
if (lockchk(LO
) && chk(current
)) {
assasinated
= kill(cur_daemon
, SIGINT
) == 0;
fatal("cannot kill printer daemon");
for (i
= 0; i
< nitems
; i
++)
process(files
[i
]->d_name
);
* Restart the printer daemon if it was killed
if (assasinated
&& !startdaemon(printer
))
fatal("cannot restart printer daemon\n");
* Process a lock file: collect the pid of the active
* daemon and the file name of the active spool entry.
* Return boolean indicating existence of a lock file.
if ((fp
= fopen(s
, "r")) == NULL
)
fatal("can't access lock file");
return(0); /* no daemon present */
if (kill(cur_daemon
, 0) < 0) {
return(0); /* no daemon present */
for (i
= 1; (n
= fread(current
, sizeof(char), sizeof(current
), fp
)) <= 0; i
++) {
* Process a control file.
if ((cfp
= fopen(file
, "r")) == NULL
)
fatal("cannot open %s", file
);
case 'U': /* unlink associated files */
printf(unlink(line
+1) ? "cannot dequeue %s\n" :
"%s dequeued\n", line
+1);
printf(unlink(file
) ? "cannot dequeue %s\n" : "%s dequeued\n", file
);
* Do the dirty work in checking
* Check for valid cf file name (mostly checking current).
if (strlen(file
) < 7 || file
[0] != 'c' || file
[1] != 'f')
if (all
&& (from
== host
|| !strcmp(from
, file
+6)))
* get the owner's name from the control file.
if ((cfp
= fopen(file
, "r")) == NULL
)
if (users
== 0 && requests
== 0)
return(!strcmp(file
, current
) && isowner(line
+1, file
));
for (n
= 0, cp
= file
+3; isdigit(*cp
); )
n
= n
* 10 + (*cp
++ - '0');
for (r
= requ
; r
< &requ
[requests
]; r
++)
if (*r
== n
&& isowner(line
+1, file
))
* Check to see if it's in the user list
for (u
= user
; u
< &user
[users
]; u
++)
if (!strcmp(*u
, line
+1) && isowner(line
+1, file
))
* If root is removing a file on the local machine, allow it.
* If root is removing a file from a remote machine, only allow
* files sent from the remote machine to be removed.
* Normal users can only remove the file from where it was sent.
if (!strcmp(person
, root
) && (from
== host
|| !strcmp(from
, file
+6)))
if (!strcmp(person
, owner
) && !strcmp(from
, file
+6))
printf("%s: Permission denied\n", file
);
* Check to see if we are sending files to a remote machine. If we are,
* then try removing files on the remote machine.
return; /* not sending to a remote machine */
* Flush stdout so the user can see what has been deleted
* while we wait (possibly) for the connection.
(void)snprintf(buf
, sizeof(buf
), "\5%s %s", RP
, all
? "-all" : person
);
for (i
= 0; i
< users
; i
++) {
for (i
= 0; i
< requests
; i
++) {
(void) sprintf(cp
, " %d", requ
[i
]);
printf("connection to %s is down\n", RM
);
if (write(rem
, buf
, i
) != i
)
fatal("Lost connection");
while ((i
= read(rem
, buf
, sizeof(buf
))) > 0)
(void) fwrite(buf
, 1, i
, stdout
);
* Return 1 if the filename begins with 'cf'
return(d
->d_name
[0] == 'c' && d
->d_name
[1] == 'f');