NetBSD/usr.sbin/lpr/common_source/rmjob.c

390 lines
9.2 KiB
C
Raw Normal View History

/* $NetBSD: rmjob.c,v 1.13 1997/10/05 15:12:04 mrg Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-05-18 05:25:21 +04:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 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
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
1997-10-05 15:52:17 +04:00
static char sccsid[] = "@(#)rmjob.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: rmjob.c,v 1.13 1997/10/05 15:12:04 mrg Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
1994-05-18 05:25:21 +04:00
#include <sys/param.h>
1993-03-21 12:45:37 +03:00
1994-05-18 05:25:21 +04:00
#include <signal.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
1993-03-21 12:45:37 +03:00
#include "lp.h"
1994-05-18 05:25:21 +04:00
#include "lp.local.h"
1993-03-21 12:45:37 +03:00
#include "pathnames.h"
1994-05-18 05:25:21 +04:00
/*
* rmjob - remove the specified jobs from the queue.
*/
1993-03-21 12:45:37 +03:00
/*
* 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 */
1994-05-18 05:25:21 +04:00
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 */
1993-03-21 12:45:37 +03:00
extern uid_t uid, euid; /* real and effective user id's */
static void do_unlink __P((char *));
1994-05-18 05:25:21 +04:00
void
1993-03-21 12:45:37 +03:00
rmjob()
{
int i, nitems;
1993-03-21 12:45:37 +03:00
int assasinated = 0;
1993-12-08 03:47:01 +03:00
struct dirent **files;
1993-03-21 12:45:37 +03:00
char *cp;
1994-05-18 05:25:21 +04:00
if ((i = cgetent(&bp, printcapdb, printer)) == -2)
fatal("can't open printer description file");
else if (i == -1)
1993-03-21 12:45:37 +03:00
fatal("unknown printer");
1994-05-18 05:25:21 +04:00
else if (i == -3)
fatal("potential reference loop detected in printcap file");
if (cgetstr(bp, "lp", &LP) < 0)
1993-03-21 12:45:37 +03:00
LP = _PATH_DEFDEVLP;
1994-05-18 05:25:21 +04:00
if (cgetstr(bp, "rp", &RP) < 0)
1993-03-21 12:45:37 +03:00
RP = DEFLP;
1994-05-18 05:25:21 +04:00
if (cgetstr(bp, "sd", &SD) < 0)
SD = _PATH_DEFSPOOL;
if (cgetstr(bp,"lo", &LO) < 0)
LO = DEFLOCK;
cgetstr(bp, "rm", &RM);
if ((cp = checkremote()) != NULL)
1993-03-21 12:45:37 +03:00
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'.
*/
if (users < 0) {
if (getuid() == 0)
all = 1; /* all files in local queue */
else {
user[0] = person;
users = 1;
}
}
if (!strcmp(person, "-all")) {
if (from == host)
fatal("The login name \"-all\" is reserved");
all = 1; /* all those from 'from' */
person = root;
}
seteuid(euid);
1993-03-21 12:45:37 +03:00
if (chdir(SD) < 0)
fatal("cannot chdir to spool directory");
if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
fatal("cannot access spool directory");
seteuid(uid);
1993-03-21 12:45:37 +03:00
if (nitems) {
/*
* 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)) {
seteuid(euid);
1993-03-21 12:45:37 +03:00
assasinated = kill(cur_daemon, SIGINT) == 0;
seteuid(uid);
1993-03-21 12:45:37 +03:00
if (!assasinated)
fatal("cannot kill printer daemon");
}
/*
* process the files
*/
for (i = 0; i < nitems; i++)
process(files[i]->d_name);
}
rmremote();
/*
* Restart the printer daemon if it was killed
*/
if (assasinated && !startdaemon(printer))
fatal("cannot restart printer daemon\n");
exit(0);
}
/*
* 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.
*/
1994-05-18 05:25:21 +04:00
int
1993-03-21 12:45:37 +03:00
lockchk(s)
char *s;
{
FILE *fp;
int i, n;
1993-03-21 12:45:37 +03:00
seteuid(euid);
if ((fp = fopen(s, "r")) == NULL) {
1993-03-21 12:45:37 +03:00
if (errno == EACCES)
fatal("can't access lock file");
else
return(0);
}
seteuid(uid);
1993-03-21 12:45:37 +03:00
if (!getline(fp)) {
(void)fclose(fp);
1993-03-21 12:45:37 +03:00
return(0); /* no daemon present */
}
cur_daemon = atoi(line);
if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
(void)fclose(fp);
1993-03-21 12:45:37 +03:00
return(0); /* no daemon present */
}
for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
if (i > 5) {
n = 1;
break;
}
sleep(i);
}
current[n-1] = '\0';
(void)fclose(fp);
1993-03-21 12:45:37 +03:00
return(1);
}
/*
* Process a control file.
*/
1994-05-18 05:25:21 +04:00
void
1993-03-21 12:45:37 +03:00
process(file)
char *file;
{
FILE *cfp;
if (!chk(file))
return;
seteuid(euid);
1993-03-21 12:45:37 +03:00
if ((cfp = fopen(file, "r")) == NULL)
fatal("cannot open %s", file);
seteuid(uid);
1993-03-21 12:45:37 +03:00
while (getline(cfp)) {
switch (line[0]) {
case 'U': /* unlink associated files */
if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
break;
do_unlink(line+1);
1993-03-21 12:45:37 +03:00
}
}
(void)fclose(cfp);
do_unlink(file);
}
static void
do_unlink(file)
char *file;
{
int ret;
1993-03-21 12:45:37 +03:00
if (from != host)
printf("%s: ", host);
seteuid(euid);
ret = unlink(file);
seteuid(uid);
printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
1993-03-21 12:45:37 +03:00
}
/*
* Do the dirty work in checking
*/
1994-05-18 05:25:21 +04:00
int
1993-03-21 12:45:37 +03:00
chk(file)
char *file;
{
int *r, n;
char **u, *cp;
1993-03-21 12:45:37 +03:00
FILE *cfp;
/*
* Check for valid cf file name (mostly checking current).
*/
if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
return(0);
if (all && (from == host || !strcmp(from, file+6)))
return(1);
/*
* get the owner's name from the control file.
*/
seteuid(euid);
1993-03-21 12:45:37 +03:00
if ((cfp = fopen(file, "r")) == NULL)
return(0);
seteuid(uid);
1993-03-21 12:45:37 +03:00
while (getline(cfp)) {
if (line[0] == 'P')
break;
}
(void)fclose(cfp);
1993-03-21 12:45:37 +03:00
if (line[0] != 'P')
return(0);
if (users == 0 && requests == 0)
return(!strcmp(file, current) && isowner(line+1, file));
/*
* Check the request list
*/
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))
return(1);
/*
* 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))
return(1);
return(0);
}
/*
* 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.
*/
1994-05-18 05:25:21 +04:00
int
1993-03-21 12:45:37 +03:00
isowner(owner, file)
char *owner, *file;
{
if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
return(1);
if (!strcmp(person, owner) && !strcmp(from, file+6))
return(1);
if (from != host)
printf("%s: ", host);
printf("%s: Permission denied\n", file);
return(0);
}
/*
* Check to see if we are sending files to a remote machine. If we are,
* then try removing files on the remote machine.
*/
1994-05-18 05:25:21 +04:00
void
1993-03-21 12:45:37 +03:00
rmremote()
{
char *cp, *s;
int i, rem, len;
1993-03-21 12:45:37 +03:00
1997-10-05 15:52:17 +04:00
if (!remote)
1993-03-21 12:45:37 +03:00
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.
*/
fflush(stdout);
/* \5 RP space all */
len = 1 + strlen(RP) + 1 + strlen(all ? "-all" : person);
for (i = 0; i < users; i++) {
len += strlen(user[i]) + 1;
}
for (i = 0; i < requests; i++) {
len += snprintf(line, sizeof(line), " %d", requ[i]);
}
/* newline nul */
len += 2;
if (len > sizeof(line))
s = malloc(len);
else
s = line;
cp = s;
cp += snprintf(s, len, "\5%s %s", RP, all ? "-all" : person);
1993-03-21 12:45:37 +03:00
for (i = 0; i < users; i++) {
*cp++ = ' ';
strncpy(cp, user[i], len - (cp - s) - 2);
cp += strlen(cp);
1993-03-21 12:45:37 +03:00
}
for (i = 0; i < requests; i++) {
(void)snprintf(cp, len - (cp - s) - 1, " %d", requ[i]);
1993-03-21 12:45:37 +03:00
cp += strlen(cp);
}
cp[0] = '\n';
cp[1] = '\0';
1997-10-05 15:52:17 +04:00
rem = getport(RM, 0);
1993-03-21 12:45:37 +03:00
if (rem < 0) {
if (from != host)
printf("%s: ", host);
printf("connection to %s is down\n", RM);
} else {
if (write(rem, s, len) != len)
1993-03-21 12:45:37 +03:00
fatal("Lost connection");
if (len > sizeof(line))
(void)free(s);
while ((i = read(rem, line, sizeof(line))) > 0)
(void)fwrite(line, 1, i, stdout);
(void)close(rem);
1993-03-21 12:45:37 +03:00
}
}
/*
* Return 1 if the filename begins with 'cf'
*/
1994-05-18 05:25:21 +04:00
int
1993-03-21 12:45:37 +03:00
iscf(d)
1993-12-08 03:47:01 +03:00
struct dirent *d;
1993-03-21 12:45:37 +03:00
{
return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
}