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