xref: /freebsd/usr.sbin/lpr/lprm/lprm.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
34 /*
35  * lprm - remove the current user's spool entry
36  *
37  * lprm [-] [[job #] [user] ...]
38  *
39  * Using information in the lock file, lprm will kill the
40  * currently active daemon (if necessary), remove the associated files,
41  * and startup a new daemon.  Priviledged users may remove anyone's spool
42  * entries, otherwise one can only remove their own.
43  */
44 
45 #include <sys/param.h>
46 
47 #include <syslog.h>
48 #include <dirent.h>
49 #include <err.h>
50 #include <pwd.h>
51 #include <unistd.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include "lp.h"
57 #include "lp.local.h"
58 
59 /*
60  * Stuff for handling job specifications
61  */
62 char	*person;		/* name of person doing lprm */
63 int	 requ[MAXREQUESTS];	/* job number of spool entries */
64 int	 requests;		/* # of spool requests */
65 char	*user[MAXUSERS];	/* users to process */
66 int	 users;			/* # of users in user array */
67 uid_t	 uid, euid;		/* real and effective user id's */
68 
69 static char	luser[16];	/* buffer for person */
70 
71 int		 main(int argc, char *_argv[]);
72 static void	 usage(void);
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	char *arg;
78 	const char *printer;
79 	struct passwd *p;
80 	static char root[] = "root";
81 
82 	printer = NULL;
83 	uid = getuid();
84 	euid = geteuid();
85 	PRIV_END	/* be safe */
86 	progname = argv[0];
87 	gethostname(local_host, sizeof(local_host));
88 	openlog("lpd", 0, LOG_LPR);
89 
90 	/*
91 	 * Bogus code later checks for string equality between
92 	 * `person' and "root", so if we are root, better make sure
93 	 * that code will succeed.
94 	 */
95 	if (getuid() == 0) {
96 		person = root;
97 	} else if ((person = getlogin()) == NULL) {
98 		if ((p = getpwuid(getuid())) == NULL)
99 			fatal(0, "Who are you?");
100 		if (strlen(p->pw_name) >= sizeof(luser))
101 			fatal(0, "Your name is too long");
102 		strcpy(luser, p->pw_name);
103 		person = luser;
104 	}
105 	while (--argc) {
106 		if ((arg = *++argv)[0] == '-')
107 			switch (arg[1]) {
108 			case 'P':
109 				if (arg[2])
110 					printer = &arg[2];
111 				else if (argc > 1) {
112 					argc--;
113 					printer = *++argv;
114 				}
115 				break;
116 			case '\0':
117 				if (!users) {
118 					users = -1;
119 					break;
120 				}
121 			default:
122 				usage();
123 			}
124 		else {
125 			if (users < 0)
126 				usage();
127 			if (isdigit(arg[0])) {
128 				if (requests >= MAXREQUESTS)
129 					fatal(0, "Too many requests");
130 				requ[requests++] = atoi(arg);
131 			} else {
132 				if (users >= MAXUSERS)
133 					fatal(0, "Too many users");
134 				user[users++] = arg;
135 			}
136 		}
137 	}
138 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
139 		printer = DEFLP;
140 
141 	rmjob(printer);
142 	exit(0);
143 }
144 
145 static void
146 usage(void)
147 {
148 	fprintf(stderr, "usage: lprm [-] [-Pprinter] [[job #] [user] ...]\n");
149 	exit(2);
150 }
151