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 * 4. Neither the name of the University nor the names of its contributors
151553Srgrimes *    may be used to endorse or promote products derived from this software
161553Srgrimes *    without specific prior written permission.
171553Srgrimes *
181553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281553Srgrimes * SUCH DAMAGE.
291553Srgrimes */
301553Srgrimes
311553Srgrimes#ifndef lint
3229780Scharnierstatic const char copyright[] =
331553Srgrimes"@(#) Copyright (c) 1983, 1993\n\
341553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351553Srgrimes#endif /* not lint */
361553Srgrimes
37117623Sgad#if 0
381553Srgrimes#ifndef lint
391553Srgrimesstatic char sccsid[] = "@(#)lprm.c	8.1 (Berkeley) 6/6/93";
40117623Sgad#endif /* not lint */
4129780Scharnier#endif
421553Srgrimes
43117623Sgad#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
44117623Sgad__FBSDID("$FreeBSD$");
45117623Sgad
461553Srgrimes/*
471553Srgrimes * lprm - remove the current user's spool entry
481553Srgrimes *
491553Srgrimes * lprm [-] [[job #] [user] ...]
501553Srgrimes *
511553Srgrimes * Using information in the lock file, lprm will kill the
521553Srgrimes * currently active daemon (if necessary), remove the associated files,
531553Srgrimes * and startup a new daemon.  Priviledged users may remove anyone's spool
541553Srgrimes * entries, otherwise one can only remove their own.
551553Srgrimes */
561553Srgrimes
571553Srgrimes#include <sys/param.h>
581553Srgrimes
591553Srgrimes#include <syslog.h>
601553Srgrimes#include <dirent.h>
61241852Seadler#include <err.h>
621553Srgrimes#include <pwd.h>
631553Srgrimes#include <unistd.h>
641553Srgrimes#include <stdlib.h>
651553Srgrimes#include <stdio.h>
661553Srgrimes#include <string.h>
671553Srgrimes#include <ctype.h>
681553Srgrimes#include "lp.h"
691553Srgrimes#include "lp.local.h"
701553Srgrimes
711553Srgrimes/*
721553Srgrimes * Stuff for handling job specifications
731553Srgrimes */
741553Srgrimeschar	*person;		/* name of person doing lprm */
751553Srgrimesint	 requ[MAXREQUESTS];	/* job number of spool entries */
761553Srgrimesint	 requests;		/* # of spool requests */
771553Srgrimeschar	*user[MAXUSERS];	/* users to process */
781553Srgrimesint	 users;			/* # of users in user array */
7927618Simpuid_t	 uid, euid;		/* real and effective user id's */
801553Srgrimes
811553Srgrimesstatic char	luser[16];	/* buffer for person */
821553Srgrimes
8378146Sgadint		 main(int argc, char *_argv[]);
8478146Sgadstatic void	 usage(void);
851553Srgrimes
861553Srgrimesint
8778146Sgadmain(int argc, char *argv[])
881553Srgrimes{
8978146Sgad	char *arg;
9078146Sgad	const char *printer;
911553Srgrimes	struct passwd *p;
9231492Swollman	static char root[] = "root";
931553Srgrimes
9431492Swollman	printer = NULL;
9527618Simp	uid = getuid();
9627618Simp	euid = geteuid();
97241852Seadler	PRIV_END	/* be safe */
9878280Sgad	progname = argv[0];
9978300Sgad	gethostname(local_host, sizeof(local_host));
1001553Srgrimes	openlog("lpd", 0, LOG_LPR);
10131492Swollman
10231492Swollman	/*
10331492Swollman	 * Bogus code later checks for string equality between
10431492Swollman	 * `person' and "root", so if we are root, better make sure
10531492Swollman	 * that code will succeed.
10631492Swollman	 */
10731492Swollman	if (getuid() == 0) {
10831492Swollman		person = root;
10931492Swollman	} else if ((person = getlogin()) == NULL) {
11031492Swollman		if ((p = getpwuid(getuid())) == NULL)
11131492Swollman			fatal(0, "Who are you?");
11231492Swollman		if (strlen(p->pw_name) >= sizeof(luser))
11331492Swollman			fatal(0, "Your name is too long");
11431492Swollman		strcpy(luser, p->pw_name);
11531492Swollman		person = luser;
11631492Swollman	}
1171553Srgrimes	while (--argc) {
1181553Srgrimes		if ((arg = *++argv)[0] == '-')
1191553Srgrimes			switch (arg[1]) {
1201553Srgrimes			case 'P':
1211553Srgrimes				if (arg[2])
1221553Srgrimes					printer = &arg[2];
1231553Srgrimes				else if (argc > 1) {
1241553Srgrimes					argc--;
1251553Srgrimes					printer = *++argv;
1261553Srgrimes				}
1271553Srgrimes				break;
1281553Srgrimes			case '\0':
1291553Srgrimes				if (!users) {
1301553Srgrimes					users = -1;
1311553Srgrimes					break;
1321553Srgrimes				}
1331553Srgrimes			default:
1341553Srgrimes				usage();
1351553Srgrimes			}
1361553Srgrimes		else {
1371553Srgrimes			if (users < 0)
1381553Srgrimes				usage();
1391553Srgrimes			if (isdigit(arg[0])) {
1401553Srgrimes				if (requests >= MAXREQUESTS)
14131492Swollman					fatal(0, "Too many requests");
1421553Srgrimes				requ[requests++] = atoi(arg);
1431553Srgrimes			} else {
1441553Srgrimes				if (users >= MAXUSERS)
14531492Swollman					fatal(0, "Too many users");
1461553Srgrimes				user[users++] = arg;
1471553Srgrimes			}
1481553Srgrimes		}
1491553Srgrimes	}
1501553Srgrimes	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
1511553Srgrimes		printer = DEFLP;
1521553Srgrimes
15331492Swollman	rmjob(printer);
1541553Srgrimes	exit(0);
1551553Srgrimes}
1561553Srgrimes
15729780Scharnierstatic void
15878146Sgadusage(void)
1591553Srgrimes{
1601553Srgrimes	fprintf(stderr, "usage: lprm [-] [-Pprinter] [[job #] [user] ...]\n");
1611553Srgrimes	exit(2);
1621553Srgrimes}
163