lprm.c revision 27618
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1983, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes *
61553Srgrimes * Redistribution and use in source and binary forms, with or without
71553Srgrimes * modification, are permitted provided that the following conditions
81553Srgrimes * are met:
91553Srgrimes * 1. Redistributions of source code must retain the above copyright
101553Srgrimes *    notice, this list of conditions and the following disclaimer.
111553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer in the
131553Srgrimes *    documentation and/or other materials provided with the distribution.
141553Srgrimes * 3. All advertising materials mentioning features or use of this software
151553Srgrimes *    must display the following acknowledgement:
161553Srgrimes *	This product includes software developed by the University of
171553Srgrimes *	California, Berkeley and its contributors.
181553Srgrimes * 4. Neither the name of the University nor the names of its contributors
191553Srgrimes *    may be used to endorse or promote products derived from this software
201553Srgrimes *    without specific prior written permission.
211553Srgrimes *
221553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321553Srgrimes * SUCH DAMAGE.
331553Srgrimes */
341553Srgrimes
351553Srgrimes#ifndef lint
361553Srgrimesstatic char copyright[] =
371553Srgrimes"@(#) Copyright (c) 1983, 1993\n\
381553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
391553Srgrimes#endif /* not lint */
401553Srgrimes
411553Srgrimes#ifndef lint
421553Srgrimesstatic char sccsid[] = "@(#)lprm.c	8.1 (Berkeley) 6/6/93";
431553Srgrimes#endif /* not lint */
441553Srgrimes
451553Srgrimes/*
461553Srgrimes * lprm - remove the current user's spool entry
471553Srgrimes *
481553Srgrimes * lprm [-] [[job #] [user] ...]
491553Srgrimes *
501553Srgrimes * Using information in the lock file, lprm will kill the
511553Srgrimes * currently active daemon (if necessary), remove the associated files,
521553Srgrimes * and startup a new daemon.  Priviledged users may remove anyone's spool
531553Srgrimes * entries, otherwise one can only remove their own.
541553Srgrimes */
551553Srgrimes
561553Srgrimes#include <sys/param.h>
571553Srgrimes
581553Srgrimes#include <syslog.h>
591553Srgrimes#include <dirent.h>
601553Srgrimes#include <pwd.h>
611553Srgrimes#include <unistd.h>
621553Srgrimes#include <stdlib.h>
631553Srgrimes#include <stdio.h>
641553Srgrimes#include <string.h>
651553Srgrimes#include <ctype.h>
661553Srgrimes#include "lp.h"
671553Srgrimes#include "lp.local.h"
681553Srgrimes
691553Srgrimes/*
701553Srgrimes * Stuff for handling job specifications
711553Srgrimes */
721553Srgrimeschar	*person;		/* name of person doing lprm */
731553Srgrimesint	 requ[MAXREQUESTS];	/* job number of spool entries */
741553Srgrimesint	 requests;		/* # of spool requests */
751553Srgrimeschar	*user[MAXUSERS];	/* users to process */
761553Srgrimesint	 users;			/* # of users in user array */
7727618Simpuid_t	 uid, euid;		/* real and effective user id's */
781553Srgrimes
791553Srgrimesstatic char	luser[16];	/* buffer for person */
801553Srgrimes
811553Srgrimesvoid usage __P((void));
821553Srgrimes
831553Srgrimesint
841553Srgrimesmain(argc, argv)
851553Srgrimes	int argc;
861553Srgrimes	char *argv[];
871553Srgrimes{
881553Srgrimes	register char *arg;
891553Srgrimes	struct passwd *p;
901553Srgrimes
9127618Simp	uid = getuid();
9227618Simp	euid = geteuid();
9327618Simp	seteuid(uid);	/* be safe */
941553Srgrimes	name = argv[0];
951553Srgrimes	gethostname(host, sizeof(host));
961553Srgrimes	openlog("lpd", 0, LOG_LPR);
971553Srgrimes	if ((p = getpwuid(getuid())) == NULL)
981553Srgrimes		fatal("Who are you?");
991553Srgrimes	if (strlen(p->pw_name) >= sizeof(luser))
1001553Srgrimes		fatal("Your name is too long");
1011553Srgrimes	strcpy(luser, p->pw_name);
1021553Srgrimes	person = luser;
1031553Srgrimes	while (--argc) {
1041553Srgrimes		if ((arg = *++argv)[0] == '-')
1051553Srgrimes			switch (arg[1]) {
1061553Srgrimes			case 'P':
1071553Srgrimes				if (arg[2])
1081553Srgrimes					printer = &arg[2];
1091553Srgrimes				else if (argc > 1) {
1101553Srgrimes					argc--;
1111553Srgrimes					printer = *++argv;
1121553Srgrimes				}
1131553Srgrimes				break;
1141553Srgrimes			case '\0':
1151553Srgrimes				if (!users) {
1161553Srgrimes					users = -1;
1171553Srgrimes					break;
1181553Srgrimes				}
1191553Srgrimes			default:
1201553Srgrimes				usage();
1211553Srgrimes			}
1221553Srgrimes		else {
1231553Srgrimes			if (users < 0)
1241553Srgrimes				usage();
1251553Srgrimes			if (isdigit(arg[0])) {
1261553Srgrimes				if (requests >= MAXREQUESTS)
1271553Srgrimes					fatal("Too many requests");
1281553Srgrimes				requ[requests++] = atoi(arg);
1291553Srgrimes			} else {
1301553Srgrimes				if (users >= MAXUSERS)
1311553Srgrimes					fatal("Too many users");
1321553Srgrimes				user[users++] = arg;
1331553Srgrimes			}
1341553Srgrimes		}
1351553Srgrimes	}
1361553Srgrimes	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
1371553Srgrimes		printer = DEFLP;
1381553Srgrimes
1391553Srgrimes	rmjob();
1401553Srgrimes	exit(0);
1411553Srgrimes}
1421553Srgrimes
1431553Srgrimesvoid
1441553Srgrimesusage()
1451553Srgrimes{
1461553Srgrimes	fprintf(stderr, "usage: lprm [-] [-Pprinter] [[job #] [user] ...]\n");
1471553Srgrimes	exit(2);
1481553Srgrimes}
149