xref: /openbsd/usr.sbin/lpr/lprm/lprm.c (revision 2761ea90)
1*2761ea90Sjmc /*	$OpenBSD: lprm.c,v 1.17 2007/02/16 13:34:58 jmc Exp $	*/
2a7643117Smillert /*	$$NetBSD: lprm.c,v 1.9 1999/08/16 03:12:32 simonb Exp $	*/
3ca5d3c4eSmillert 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1983, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  *
9df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
10df930be7Sderaadt  * modification, are permitted provided that the following conditions
11df930be7Sderaadt  * are met:
12df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
14df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
16df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
1729295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
18df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
19df930be7Sderaadt  *    without specific prior written permission.
20df930be7Sderaadt  *
21df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df930be7Sderaadt  * SUCH DAMAGE.
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt #ifndef lint
35c1624b2fSmillert static const char copyright[] =
36df930be7Sderaadt "@(#) Copyright (c) 1983, 1993\n\
37df930be7Sderaadt 	The Regents of the University of California.  All rights reserved.\n";
38df930be7Sderaadt #endif /* not lint */
39df930be7Sderaadt 
40df930be7Sderaadt #ifndef lint
41ca5d3c4eSmillert #if 0
42c1624b2fSmillert static const char sccsid[] = "@(#)lprm.c	8.1 (Berkeley) 6/6/93";
43ca5d3c4eSmillert #else
44*2761ea90Sjmc static const char rcsid[] = "$OpenBSD: lprm.c,v 1.17 2007/02/16 13:34:58 jmc Exp $";
45ca5d3c4eSmillert #endif
46df930be7Sderaadt #endif /* not lint */
47df930be7Sderaadt 
48df930be7Sderaadt /*
49df930be7Sderaadt  * lprm - remove the current user's spool entry
50df930be7Sderaadt  *
51df930be7Sderaadt  * lprm [-] [[job #] [user] ...]
52df930be7Sderaadt  *
53df930be7Sderaadt  * Using information in the lock file, lprm will kill the
54df930be7Sderaadt  * currently active daemon (if necessary), remove the associated files,
55a7643117Smillert  * and startup a new daemon.  Privileged users may remove anyone's spool
56df930be7Sderaadt  * entries, otherwise one can only remove their own.
57df930be7Sderaadt  */
58df930be7Sderaadt 
59df930be7Sderaadt #include <sys/param.h>
60df930be7Sderaadt 
61a7643117Smillert #include <ctype.h>
62df930be7Sderaadt #include <dirent.h>
63a7643117Smillert #include <err.h>
646468ba68Smillert #include <errno.h>
65df930be7Sderaadt #include <pwd.h>
66df930be7Sderaadt #include <stdlib.h>
67df930be7Sderaadt #include <stdio.h>
68df930be7Sderaadt #include <string.h>
69a7643117Smillert #include <syslog.h>
70a7643117Smillert #include <unistd.h>
71a7643117Smillert 
72df930be7Sderaadt #include "lp.h"
73df930be7Sderaadt #include "lp.local.h"
74df930be7Sderaadt 
75df930be7Sderaadt /*
76df930be7Sderaadt  * Stuff for handling job specifications
77df930be7Sderaadt  */
78df930be7Sderaadt char	*person;		/* name of person doing lprm */
79df930be7Sderaadt int	 requ[MAXREQUESTS];	/* job number of spool entries */
80df930be7Sderaadt int	 requests;		/* # of spool requests */
81df930be7Sderaadt char	*user[MAXUSERS];	/* users to process */
82df930be7Sderaadt int	 users;			/* # of users in user array */
83a7643117Smillert volatile sig_atomic_t gotintr;	/* set when we receive SIGINT */
8449848d36Sderaadt static char luser[MAXLOGNAME];	/* buffer for person */
85df930be7Sderaadt 
86a7643117Smillert static __dead void usage(void);
87df930be7Sderaadt 
88df930be7Sderaadt int
89a7643117Smillert main(int argc, char **argv)
90df930be7Sderaadt {
919f6ae840Smillert 	struct passwd *pw;
92a7643117Smillert 	char *cp;
93a7643117Smillert 	long l;
94a7643117Smillert 	int ch;
95df930be7Sderaadt 
969f6ae840Smillert 	/*
972d40ed46Smillert 	 * Simulate setuid daemon w/ PRIV_END called.
989f6ae840Smillert 	 * We don't want lpr to actually be setuid daemon since that
999f6ae840Smillert 	 * requires that the lpr binary be owned by user daemon, which
1009f6ae840Smillert 	 * is potentially unsafe.
1019f6ae840Smillert 	 */
1029f6ae840Smillert 	if ((pw = getpwuid(DEFUID)) == NULL)
1039f6ae840Smillert 		errx(1, "daemon uid (%u) not in password file", DEFUID);
1049f6ae840Smillert 	effective_uid = pw->pw_uid;
1056468ba68Smillert 	real_uid = getuid();
1069f6ae840Smillert 	effective_gid = pw->pw_gid;
1076468ba68Smillert 	real_gid = getgid();
1082d40ed46Smillert 	setresgid(real_gid, real_gid, effective_gid);
1092d40ed46Smillert 	setresuid(real_uid, real_uid, effective_uid);
110222c0752Sericj 
111df930be7Sderaadt 	gethostname(host, sizeof(host));
112a7643117Smillert 	openlog("lprm", 0, LOG_LPR);
1139f6ae840Smillert 	if ((pw = getpwuid(real_uid)) == NULL)
114df930be7Sderaadt 		fatal("Who are you?");
1159f6ae840Smillert 	if (strlen(pw->pw_name) >= sizeof(luser))
116df930be7Sderaadt 		fatal("Your name is too long");
1179f6ae840Smillert 	strlcpy(luser, pw->pw_name, sizeof(luser));
118df930be7Sderaadt 	person = luser;
11902e888b3Smillert 	while ((ch = getopt(argc, argv, "P:w:-")) != -1) {
120a7643117Smillert 		switch (ch) {
121a7643117Smillert 		case '-':
122df930be7Sderaadt 			users = -1;
123df930be7Sderaadt 			break;
124a7643117Smillert 		case 'P':
125a7643117Smillert 			printer = optarg;
126a7643117Smillert 			break;
127a7643117Smillert 		case 'w':
128a7643117Smillert 			l = strtol(optarg, &cp, 10);
129a7643117Smillert 			if (*cp != '\0' || l < 0 || l >= INT_MAX)
130a7643117Smillert 				errx(1, "wait time must be postive integer: %s",
131a7643117Smillert 				    optarg);
132a7643117Smillert 			wait_time = (u_int)l;
133a7643117Smillert 			if (wait_time < 30)
134a7643117Smillert 				warnx("warning: wait time less than 30 seconds");
135a7643117Smillert 			break;
136df930be7Sderaadt 		default:
137df930be7Sderaadt 			usage();
138df930be7Sderaadt 		}
139a7643117Smillert 	}
140a7643117Smillert 	argc -= optind;
141a7643117Smillert 	argv += optind;
142a7643117Smillert 
143a7643117Smillert 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
144a7643117Smillert 		printer = DEFLP;
145a7643117Smillert 	if (users < 0 && argc != 0)
146df930be7Sderaadt 		usage();
147a7643117Smillert 	while (argc > 0) {
148a7643117Smillert 		if (isdigit(*argv[0])) {
149df930be7Sderaadt 			if (requests >= MAXREQUESTS)
150df930be7Sderaadt 				fatal("Too many requests");
151a7643117Smillert 			requ[requests++] = atoi(argv[0]);
152df930be7Sderaadt 		} else {
153df930be7Sderaadt 			if (users >= MAXUSERS)
154df930be7Sderaadt 				fatal("Too many users");
155a7643117Smillert 			user[users++] = argv[0];
156df930be7Sderaadt 		}
157a7643117Smillert 		argc--;
158a7643117Smillert 		argv++;
15962831258Sderaadt 	}
160df930be7Sderaadt 
161df930be7Sderaadt 	rmjob();
162df930be7Sderaadt 	exit(0);
163df930be7Sderaadt }
164df930be7Sderaadt 
165a7643117Smillert static __dead void
166a7643117Smillert usage(void)
167df930be7Sderaadt {
168a7643117Smillert 	extern char *__progname;
169a7643117Smillert 
170*2761ea90Sjmc 	fprintf(stderr, "usage: %s [-] [-Pprinter] [[job# ...] [user ...]]\n",
171a7643117Smillert 	    __progname);
172df930be7Sderaadt 	exit(2);
173df930be7Sderaadt }
174