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