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>
611553Srgrimes#include <pwd.h>
621553Srgrimes#include <unistd.h>
631553Srgrimes#include <stdlib.h>
641553Srgrimes#include <stdio.h>
651553Srgrimes#include <string.h>
661553Srgrimes#include <ctype.h>
671553Srgrimes#include "lp.h"
681553Srgrimes#include "lp.local.h"
691553Srgrimes
701553Srgrimes/*
711553Srgrimes * Stuff for handling job specifications
721553Srgrimes */
731553Srgrimeschar	*person;		/* name of person doing lprm */
741553Srgrimesint	 requ[MAXREQUESTS];	/* job number of spool entries */
751553Srgrimesint	 requests;		/* # of spool requests */
761553Srgrimeschar	*user[MAXUSERS];	/* users to process */
771553Srgrimesint	 users;			/* # of users in user array */
7827618Simpuid_t	 uid, euid;		/* real and effective user id's */
791553Srgrimes
801553Srgrimesstatic char	luser[16];	/* buffer for person */
811553Srgrimes
8278146Sgadint		 main(int argc, char *_argv[]);
8378146Sgadstatic void	 usage(void);
841553Srgrimes
851553Srgrimesint
8678146Sgadmain(int argc, char *argv[])
871553Srgrimes{
8878146Sgad	char *arg;
8978146Sgad	const char *printer;
901553Srgrimes	struct passwd *p;
9131492Swollman	static char root[] = "root";
921553Srgrimes
9331492Swollman	printer = NULL;
9427618Simp	uid = getuid();
9527618Simp	euid = geteuid();
9627618Simp	seteuid(uid);	/* be safe */
9778280Sgad	progname = argv[0];
9878300Sgad	gethostname(local_host, sizeof(local_host));
991553Srgrimes	openlog("lpd", 0, LOG_LPR);
10031492Swollman
10131492Swollman	/*
10231492Swollman	 * Bogus code later checks for string equality between
10331492Swollman	 * `person' and "root", so if we are root, better make sure
10431492Swollman	 * that code will succeed.
10531492Swollman	 */
10631492Swollman	if (getuid() == 0) {
10731492Swollman		person = root;
10831492Swollman	} else if ((person = getlogin()) == NULL) {
10931492Swollman		if ((p = getpwuid(getuid())) == NULL)
11031492Swollman			fatal(0, "Who are you?");
11131492Swollman		if (strlen(p->pw_name) >= sizeof(luser))
11231492Swollman			fatal(0, "Your name is too long");
11331492Swollman		strcpy(luser, p->pw_name);
11431492Swollman		person = luser;
11531492Swollman	}
1161553Srgrimes	while (--argc) {
1171553Srgrimes		if ((arg = *++argv)[0] == '-')
1181553Srgrimes			switch (arg[1]) {
1191553Srgrimes			case 'P':
1201553Srgrimes				if (arg[2])
1211553Srgrimes					printer = &arg[2];
1221553Srgrimes				else if (argc > 1) {
1231553Srgrimes					argc--;
1241553Srgrimes					printer = *++argv;
1251553Srgrimes				}
1261553Srgrimes				break;
1271553Srgrimes			case '\0':
1281553Srgrimes				if (!users) {
1291553Srgrimes					users = -1;
1301553Srgrimes					break;
1311553Srgrimes				}
1321553Srgrimes			default:
1331553Srgrimes				usage();
1341553Srgrimes			}
1351553Srgrimes		else {
1361553Srgrimes			if (users < 0)
1371553Srgrimes				usage();
1381553Srgrimes			if (isdigit(arg[0])) {
1391553Srgrimes				if (requests >= MAXREQUESTS)
14031492Swollman					fatal(0, "Too many requests");
1411553Srgrimes				requ[requests++] = atoi(arg);
1421553Srgrimes			} else {
1431553Srgrimes				if (users >= MAXUSERS)
14431492Swollman					fatal(0, "Too many users");
1451553Srgrimes				user[users++] = arg;
1461553Srgrimes			}
1471553Srgrimes		}
1481553Srgrimes	}
1491553Srgrimes	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
1501553Srgrimes		printer = DEFLP;
1511553Srgrimes
15231492Swollman	rmjob(printer);
1531553Srgrimes	exit(0);
1541553Srgrimes}
1551553Srgrimes
15629780Scharnierstatic void
15778146Sgadusage(void)
1581553Srgrimes{
1591553Srgrimes	fprintf(stderr, "usage: lprm [-] [-Pprinter] [[job #] [user] ...]\n");
1601553Srgrimes	exit(2);
1611553Srgrimes}
162