xref: /netbsd-src/usr.sbin/lpr/lprm/lprm.c (revision fbffadb9f864c0324fb295860ab0faeb187269cc)
1*fbffadb9Smrg /*	$NetBSD: lprm.c,v 1.20 2019/02/03 03:19:31 mrg Exp $	*/
2fe7ed7ceSmrg 
361f28255Scgd /*
43e603ff3Scgd  * Copyright (c) 1983, 1993
53e603ff3Scgd  *	The Regents of the University of California.  All rights reserved.
63e603ff3Scgd  *
761f28255Scgd  *
861f28255Scgd  * Redistribution and use in source and binary forms, with or without
961f28255Scgd  * modification, are permitted provided that the following conditions
1061f28255Scgd  * are met:
1161f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1261f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1361f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1561f28255Scgd  *    documentation and/or other materials provided with the distribution.
16326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
1761f28255Scgd  *    may be used to endorse or promote products derived from this software
1861f28255Scgd  *    without specific prior written permission.
1961f28255Scgd  *
2061f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2161f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2461f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2561f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2661f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2761f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2861f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2961f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3061f28255Scgd  * SUCH DAMAGE.
3161f28255Scgd  */
3261f28255Scgd 
33fe7ed7ceSmrg #include <sys/cdefs.h>
3461f28255Scgd #ifndef lint
359c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
369c194566Slukem  The Regents of the University of California.  All rights reserved.");
37fe7ed7ceSmrg #if 0
383e603ff3Scgd static char sccsid[] = "@(#)lprm.c	8.1 (Berkeley) 6/6/93";
39fe7ed7ceSmrg #else
40*fbffadb9Smrg __RCSID("$NetBSD: lprm.c,v 1.20 2019/02/03 03:19:31 mrg Exp $");
41fe7ed7ceSmrg #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd 
4461f28255Scgd /*
4561f28255Scgd  * lprm - remove the current user's spool entry
4661f28255Scgd  *
4761f28255Scgd  * lprm [-] [[job #] [user] ...]
4861f28255Scgd  *
4961f28255Scgd  * Using information in the lock file, lprm will kill the
5061f28255Scgd  * currently active daemon (if necessary), remove the associated files,
51299578ebSsimonb  * and startup a new daemon.  Privileged users may remove anyone's spool
5261f28255Scgd  * entries, otherwise one can only remove their own.
5361f28255Scgd  */
5461f28255Scgd 
553e603ff3Scgd #include <sys/param.h>
563e603ff3Scgd 
575b6d0e7eSmrg #include <err.h>
583e603ff3Scgd #include <syslog.h>
593e603ff3Scgd #include <dirent.h>
603e603ff3Scgd #include <pwd.h>
613e603ff3Scgd #include <unistd.h>
623e603ff3Scgd #include <stdlib.h>
633e603ff3Scgd #include <stdio.h>
643e603ff3Scgd #include <string.h>
653e603ff3Scgd #include <ctype.h>
66fe7ed7ceSmrg 
6761f28255Scgd #include "lp.h"
683e603ff3Scgd #include "lp.local.h"
6961f28255Scgd 
7061f28255Scgd /*
7161f28255Scgd  * Stuff for handling job specifications
7261f28255Scgd  */
733e603ff3Scgd char	*person;		/* name of person doing lprm */
7461f28255Scgd int	 requ[MAXREQUESTS];	/* job number of spool entries */
7561f28255Scgd int	 requests;		/* # of spool requests */
763e603ff3Scgd char	*user[MAXUSERS];	/* users to process */
773e603ff3Scgd int	 users;			/* # of users in user array */
788e41ca80Shpeyerl uid_t	 uid, euid;		/* real and effective user id's */
7961f28255Scgd 
8061f28255Scgd static char	luser[16];	/* buffer for person */
8161f28255Scgd 
828b0f9554Sperry static void usage(void) __dead;
833e603ff3Scgd 
843e603ff3Scgd int
main(int argc,char * argv[])85895dc72aSwiz main(int argc, char *argv[])
8661f28255Scgd {
87fe7ed7ceSmrg 	char *arg;
8861f28255Scgd 	struct passwd *p;
8961f28255Scgd 
908e41ca80Shpeyerl 	uid = getuid();
918e41ca80Shpeyerl 	euid = geteuid();
928e41ca80Shpeyerl 	seteuid(uid);	/* be safe */
9304723c3fSchristos 	setprogname(*argv);
9461f28255Scgd 	gethostname(host, sizeof(host));
9532f51971Smrg 	host[sizeof(host) - 1] = '\0';
9661f28255Scgd 	openlog("lpd", 0, LOG_LPR);
9761f28255Scgd 	if ((p = getpwuid(getuid())) == NULL)
9861f28255Scgd 		fatal("Who are you?");
9961f28255Scgd 	if (strlen(p->pw_name) >= sizeof(luser))
10061f28255Scgd 		fatal("Your name is too long");
10185da8822Sitojun 	strlcpy(luser, p->pw_name, sizeof(luser));
10261f28255Scgd 	person = luser;
10361f28255Scgd 	while (--argc) {
10461f28255Scgd 		if ((arg = *++argv)[0] == '-')
10561f28255Scgd 			switch (arg[1]) {
10661f28255Scgd 			case 'P':
10761f28255Scgd 				if (arg[2])
10861f28255Scgd 					printer = &arg[2];
10961f28255Scgd 				else if (argc > 1) {
11061f28255Scgd 					argc--;
11161f28255Scgd 					printer = *++argv;
11261f28255Scgd 				}
11361f28255Scgd 				break;
1145b6d0e7eSmrg 			case 'w':
1155b6d0e7eSmrg 				if (arg[2])
1165b6d0e7eSmrg 					wait_time = atoi(&arg[2]);
1175b6d0e7eSmrg 				else if (argc > 1) {
1185b6d0e7eSmrg 					argc--;
1195b6d0e7eSmrg 					wait_time = atoi(*++argv);
1205b6d0e7eSmrg 				}
1215b6d0e7eSmrg 				if (wait_time < 0)
12241b91279Skleink 					errx(1, "wait time must be positive: %d",
12353e326ddSwiz 					    wait_time);
1245b6d0e7eSmrg 				if (wait_time < 30)
1255b6d0e7eSmrg 				    warnx("warning: wait time less than 30 seconds");
1265b6d0e7eSmrg 				break;
12761f28255Scgd 			case '\0':
12861f28255Scgd 				if (!users) {
12961f28255Scgd 					users = -1;
13061f28255Scgd 					break;
13161f28255Scgd 				}
132*fbffadb9Smrg 				/* FALLTHROUGH */
13361f28255Scgd 			default:
13461f28255Scgd 				usage();
13561f28255Scgd 			}
13661f28255Scgd 		else {
13761f28255Scgd 			if (users < 0)
13861f28255Scgd 				usage();
13975ba9fc7Sdsl 			if (isdigit((unsigned char)arg[0])) {
14061f28255Scgd 				if (requests >= MAXREQUESTS)
14161f28255Scgd 					fatal("Too many requests");
14261f28255Scgd 				requ[requests++] = atoi(arg);
14361f28255Scgd 			} else {
14461f28255Scgd 				if (users >= MAXUSERS)
14561f28255Scgd 					fatal("Too many users");
14661f28255Scgd 				user[users++] = arg;
14761f28255Scgd 			}
14861f28255Scgd 		}
14961f28255Scgd 	}
15061f28255Scgd 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
15161f28255Scgd 		printer = DEFLP;
15261f28255Scgd 
15361f28255Scgd 	rmjob();
1543e603ff3Scgd 	exit(0);
15561f28255Scgd }
15661f28255Scgd 
157fe7ed7ceSmrg static void
usage(void)158895dc72aSwiz usage(void)
15961f28255Scgd {
16004723c3fSchristos 	(void)fprintf(stderr,
16104723c3fSchristos 	    "Usage: %s [-] [-Pprinter] [-w maxwait] [[job #] [user] ...]\n",
16204723c3fSchristos 	    getprogname());
16361f28255Scgd 	exit(2);
16461f28255Scgd }
165