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