Home | History | Annotate | Line # | Download | only in common_source
rmjob.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1983 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)rmjob.c	5.7 (Berkeley) 6/1/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd /*
     39  1.1  cgd  * rmjob - remove the specified jobs from the queue.
     40  1.1  cgd  */
     41  1.1  cgd 
     42  1.1  cgd #include "lp.h"
     43  1.1  cgd #include "pathnames.h"
     44  1.1  cgd 
     45  1.1  cgd /*
     46  1.1  cgd  * Stuff for handling lprm specifications
     47  1.1  cgd  */
     48  1.1  cgd extern char	*user[];		/* users to process */
     49  1.1  cgd extern int	users;			/* # of users in user array */
     50  1.1  cgd extern int	requ[];			/* job number of spool entries */
     51  1.1  cgd extern int	requests;		/* # of spool requests */
     52  1.1  cgd extern char	*person;		/* name of person doing lprm */
     53  1.1  cgd 
     54  1.1  cgd char	root[] = "root";
     55  1.1  cgd int	all = 0;		/* eliminate all files (root only) */
     56  1.1  cgd int	cur_daemon;		/* daemon's pid */
     57  1.1  cgd char	current[40];		/* active control file name */
     58  1.1  cgd 
     59  1.1  cgd int	iscf();
     60  1.1  cgd 
     61  1.1  cgd rmjob()
     62  1.1  cgd {
     63  1.1  cgd 	register int i, nitems;
     64  1.1  cgd 	int assasinated = 0;
     65  1.1  cgd 	struct direct **files;
     66  1.1  cgd 	char *cp;
     67  1.1  cgd 
     68  1.1  cgd 	if ((i = pgetent(line, printer)) < 0)
     69  1.1  cgd 		fatal("cannot open printer description file");
     70  1.1  cgd 	else if (i == 0)
     71  1.1  cgd 		fatal("unknown printer");
     72  1.1  cgd 	if ((SD = pgetstr("sd", &bp)) == NULL)
     73  1.1  cgd 		SD = _PATH_DEFSPOOL;
     74  1.1  cgd 	if ((LO = pgetstr("lo", &bp)) == NULL)
     75  1.1  cgd 		LO = DEFLOCK;
     76  1.1  cgd 	if ((LP = pgetstr("lp", &bp)) == NULL)
     77  1.1  cgd 		LP = _PATH_DEFDEVLP;
     78  1.1  cgd 	if ((RP = pgetstr("rp", &bp)) == NULL)
     79  1.1  cgd 		RP = DEFLP;
     80  1.1  cgd 	RM = pgetstr("rm", &bp);
     81  1.1  cgd 	if (cp = checkremote())
     82  1.1  cgd 		printf("Warning: %s\n", cp);
     83  1.1  cgd 
     84  1.1  cgd 	/*
     85  1.1  cgd 	 * If the format was `lprm -' and the user isn't the super-user,
     86  1.1  cgd 	 *  then fake things to look like he said `lprm user'.
     87  1.1  cgd 	 */
     88  1.1  cgd 	if (users < 0) {
     89  1.1  cgd 		if (getuid() == 0)
     90  1.1  cgd 			all = 1;	/* all files in local queue */
     91  1.1  cgd 		else {
     92  1.1  cgd 			user[0] = person;
     93  1.1  cgd 			users = 1;
     94  1.1  cgd 		}
     95  1.1  cgd 	}
     96  1.1  cgd 	if (!strcmp(person, "-all")) {
     97  1.1  cgd 		if (from == host)
     98  1.1  cgd 			fatal("The login name \"-all\" is reserved");
     99  1.1  cgd 		all = 1;	/* all those from 'from' */
    100  1.1  cgd 		person = root;
    101  1.1  cgd 	}
    102  1.1  cgd 
    103  1.1  cgd 	if (chdir(SD) < 0)
    104  1.1  cgd 		fatal("cannot chdir to spool directory");
    105  1.1  cgd 	if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
    106  1.1  cgd 		fatal("cannot access spool directory");
    107  1.1  cgd 
    108  1.1  cgd 	if (nitems) {
    109  1.1  cgd 		/*
    110  1.1  cgd 		 * Check for an active printer daemon (in which case we
    111  1.1  cgd 		 *  kill it if it is reading our file) then remove stuff
    112  1.1  cgd 		 *  (after which we have to restart the daemon).
    113  1.1  cgd 		 */
    114  1.1  cgd 		if (lockchk(LO) && chk(current)) {
    115  1.1  cgd 			assasinated = kill(cur_daemon, SIGINT) == 0;
    116  1.1  cgd 			if (!assasinated)
    117  1.1  cgd 				fatal("cannot kill printer daemon");
    118  1.1  cgd 		}
    119  1.1  cgd 		/*
    120  1.1  cgd 		 * process the files
    121  1.1  cgd 		 */
    122  1.1  cgd 		for (i = 0; i < nitems; i++)
    123  1.1  cgd 			process(files[i]->d_name);
    124  1.1  cgd 	}
    125  1.1  cgd 	rmremote();
    126  1.1  cgd 	/*
    127  1.1  cgd 	 * Restart the printer daemon if it was killed
    128  1.1  cgd 	 */
    129  1.1  cgd 	if (assasinated && !startdaemon(printer))
    130  1.1  cgd 		fatal("cannot restart printer daemon\n");
    131  1.1  cgd 	exit(0);
    132  1.1  cgd }
    133  1.1  cgd 
    134  1.1  cgd /*
    135  1.1  cgd  * Process a lock file: collect the pid of the active
    136  1.1  cgd  *  daemon and the file name of the active spool entry.
    137  1.1  cgd  * Return boolean indicating existence of a lock file.
    138  1.1  cgd  */
    139  1.1  cgd lockchk(s)
    140  1.1  cgd 	char *s;
    141  1.1  cgd {
    142  1.1  cgd 	register FILE *fp;
    143  1.1  cgd 	register int i, n;
    144  1.1  cgd 
    145  1.1  cgd 	if ((fp = fopen(s, "r")) == NULL)
    146  1.1  cgd 		if (errno == EACCES)
    147  1.1  cgd 			fatal("can't access lock file");
    148  1.1  cgd 		else
    149  1.1  cgd 			return(0);
    150  1.1  cgd 	if (!getline(fp)) {
    151  1.1  cgd 		(void) fclose(fp);
    152  1.1  cgd 		return(0);		/* no daemon present */
    153  1.1  cgd 	}
    154  1.1  cgd 	cur_daemon = atoi(line);
    155  1.1  cgd 	if (kill(cur_daemon, 0) < 0) {
    156  1.1  cgd 		(void) fclose(fp);
    157  1.1  cgd 		return(0);		/* no daemon present */
    158  1.1  cgd 	}
    159  1.1  cgd 	for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
    160  1.1  cgd 		if (i > 5) {
    161  1.1  cgd 			n = 1;
    162  1.1  cgd 			break;
    163  1.1  cgd 		}
    164  1.1  cgd 		sleep(i);
    165  1.1  cgd 	}
    166  1.1  cgd 	current[n-1] = '\0';
    167  1.1  cgd 	(void) fclose(fp);
    168  1.1  cgd 	return(1);
    169  1.1  cgd }
    170  1.1  cgd 
    171  1.1  cgd /*
    172  1.1  cgd  * Process a control file.
    173  1.1  cgd  */
    174  1.1  cgd process(file)
    175  1.1  cgd 	char *file;
    176  1.1  cgd {
    177  1.1  cgd 	FILE *cfp;
    178  1.1  cgd 
    179  1.1  cgd 	if (!chk(file))
    180  1.1  cgd 		return;
    181  1.1  cgd 	if ((cfp = fopen(file, "r")) == NULL)
    182  1.1  cgd 		fatal("cannot open %s", file);
    183  1.1  cgd 	while (getline(cfp)) {
    184  1.1  cgd 		switch (line[0]) {
    185  1.1  cgd 		case 'U':  /* unlink associated files */
    186  1.1  cgd 			if (from != host)
    187  1.1  cgd 				printf("%s: ", host);
    188  1.1  cgd 			printf(unlink(line+1) ? "cannot dequeue %s\n" :
    189  1.1  cgd 				"%s dequeued\n", line+1);
    190  1.1  cgd 		}
    191  1.1  cgd 	}
    192  1.1  cgd 	(void) fclose(cfp);
    193  1.1  cgd 	if (from != host)
    194  1.1  cgd 		printf("%s: ", host);
    195  1.1  cgd 	printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
    196  1.1  cgd }
    197  1.1  cgd 
    198  1.1  cgd /*
    199  1.1  cgd  * Do the dirty work in checking
    200  1.1  cgd  */
    201  1.1  cgd chk(file)
    202  1.1  cgd 	char *file;
    203  1.1  cgd {
    204  1.1  cgd 	register int *r, n;
    205  1.1  cgd 	register char **u, *cp;
    206  1.1  cgd 	FILE *cfp;
    207  1.1  cgd 
    208  1.1  cgd 	/*
    209  1.1  cgd 	 * Check for valid cf file name (mostly checking current).
    210  1.1  cgd 	 */
    211  1.1  cgd 	if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
    212  1.1  cgd 		return(0);
    213  1.1  cgd 
    214  1.1  cgd 	if (all && (from == host || !strcmp(from, file+6)))
    215  1.1  cgd 		return(1);
    216  1.1  cgd 
    217  1.1  cgd 	/*
    218  1.1  cgd 	 * get the owner's name from the control file.
    219  1.1  cgd 	 */
    220  1.1  cgd 	if ((cfp = fopen(file, "r")) == NULL)
    221  1.1  cgd 		return(0);
    222  1.1  cgd 	while (getline(cfp)) {
    223  1.1  cgd 		if (line[0] == 'P')
    224  1.1  cgd 			break;
    225  1.1  cgd 	}
    226  1.1  cgd 	(void) fclose(cfp);
    227  1.1  cgd 	if (line[0] != 'P')
    228  1.1  cgd 		return(0);
    229  1.1  cgd 
    230  1.1  cgd 	if (users == 0 && requests == 0)
    231  1.1  cgd 		return(!strcmp(file, current) && isowner(line+1, file));
    232  1.1  cgd 	/*
    233  1.1  cgd 	 * Check the request list
    234  1.1  cgd 	 */
    235  1.1  cgd 	for (n = 0, cp = file+3; isdigit(*cp); )
    236  1.1  cgd 		n = n * 10 + (*cp++ - '0');
    237  1.1  cgd 	for (r = requ; r < &requ[requests]; r++)
    238  1.1  cgd 		if (*r == n && isowner(line+1, file))
    239  1.1  cgd 			return(1);
    240  1.1  cgd 	/*
    241  1.1  cgd 	 * Check to see if it's in the user list
    242  1.1  cgd 	 */
    243  1.1  cgd 	for (u = user; u < &user[users]; u++)
    244  1.1  cgd 		if (!strcmp(*u, line+1) && isowner(line+1, file))
    245  1.1  cgd 			return(1);
    246  1.1  cgd 	return(0);
    247  1.1  cgd }
    248  1.1  cgd 
    249  1.1  cgd /*
    250  1.1  cgd  * If root is removing a file on the local machine, allow it.
    251  1.1  cgd  * If root is removing a file from a remote machine, only allow
    252  1.1  cgd  * files sent from the remote machine to be removed.
    253  1.1  cgd  * Normal users can only remove the file from where it was sent.
    254  1.1  cgd  */
    255  1.1  cgd isowner(owner, file)
    256  1.1  cgd 	char *owner, *file;
    257  1.1  cgd {
    258  1.1  cgd 	if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
    259  1.1  cgd 		return(1);
    260  1.1  cgd 	if (!strcmp(person, owner) && !strcmp(from, file+6))
    261  1.1  cgd 		return(1);
    262  1.1  cgd 	if (from != host)
    263  1.1  cgd 		printf("%s: ", host);
    264  1.1  cgd 	printf("%s: Permission denied\n", file);
    265  1.1  cgd 	return(0);
    266  1.1  cgd }
    267  1.1  cgd 
    268  1.1  cgd /*
    269  1.1  cgd  * Check to see if we are sending files to a remote machine. If we are,
    270  1.1  cgd  * then try removing files on the remote machine.
    271  1.1  cgd  */
    272  1.1  cgd rmremote()
    273  1.1  cgd {
    274  1.1  cgd 	register char *cp;
    275  1.1  cgd 	register int i, rem;
    276  1.1  cgd 	char buf[BUFSIZ];
    277  1.1  cgd 
    278  1.1  cgd 	if (!sendtorem)
    279  1.1  cgd 		return;	/* not sending to a remote machine */
    280  1.1  cgd 
    281  1.1  cgd 	/*
    282  1.1  cgd 	 * Flush stdout so the user can see what has been deleted
    283  1.1  cgd 	 * while we wait (possibly) for the connection.
    284  1.1  cgd 	 */
    285  1.1  cgd 	fflush(stdout);
    286  1.1  cgd 
    287  1.1  cgd 	sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
    288  1.1  cgd 	cp = buf;
    289  1.1  cgd 	for (i = 0; i < users; i++) {
    290  1.1  cgd 		cp += strlen(cp);
    291  1.1  cgd 		*cp++ = ' ';
    292  1.1  cgd 		strcpy(cp, user[i]);
    293  1.1  cgd 	}
    294  1.1  cgd 	for (i = 0; i < requests; i++) {
    295  1.1  cgd 		cp += strlen(cp);
    296  1.1  cgd 		(void) sprintf(cp, " %d", requ[i]);
    297  1.1  cgd 	}
    298  1.1  cgd 	strcat(cp, "\n");
    299  1.1  cgd 	rem = getport(RM);
    300  1.1  cgd 	if (rem < 0) {
    301  1.1  cgd 		if (from != host)
    302  1.1  cgd 			printf("%s: ", host);
    303  1.1  cgd 		printf("connection to %s is down\n", RM);
    304  1.1  cgd 	} else {
    305  1.1  cgd 		i = strlen(buf);
    306  1.1  cgd 		if (write(rem, buf, i) != i)
    307  1.1  cgd 			fatal("Lost connection");
    308  1.1  cgd 		while ((i = read(rem, buf, sizeof(buf))) > 0)
    309  1.1  cgd 			(void) fwrite(buf, 1, i, stdout);
    310  1.1  cgd 		(void) close(rem);
    311  1.1  cgd 	}
    312  1.1  cgd }
    313  1.1  cgd 
    314  1.1  cgd /*
    315  1.1  cgd  * Return 1 if the filename begins with 'cf'
    316  1.1  cgd  */
    317  1.1  cgd iscf(d)
    318  1.1  cgd 	struct direct *d;
    319  1.1  cgd {
    320  1.1  cgd 	return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
    321  1.1  cgd }
    322