xref: /netbsd-src/usr.sbin/lpr/lprm/lprm.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: lprm.c,v 1.6 1996/12/09 09:57:49 mrg Exp $	*/
2 /*
3  * Copyright (c) 1983, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1983, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 static char sccsid[] = "@(#)lprm.c	8.1 (Berkeley) 6/6/93";
44 #endif /* not lint */
45 
46 /*
47  * lprm - remove the current user's spool entry
48  *
49  * lprm [-] [[job #] [user] ...]
50  *
51  * Using information in the lock file, lprm will kill the
52  * currently active daemon (if necessary), remove the associated files,
53  * and startup a new daemon.  Priviledged users may remove anyone's spool
54  * entries, otherwise one can only remove their own.
55  */
56 
57 #include <sys/param.h>
58 
59 #include <syslog.h>
60 #include <dirent.h>
61 #include <pwd.h>
62 #include <unistd.h>
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <ctype.h>
67 #include "lp.h"
68 #include "lp.local.h"
69 
70 /*
71  * Stuff for handling job specifications
72  */
73 char	*person;		/* name of person doing lprm */
74 int	 requ[MAXREQUESTS];	/* job number of spool entries */
75 int	 requests;		/* # of spool requests */
76 char	*user[MAXUSERS];	/* users to process */
77 int	 users;			/* # of users in user array */
78 uid_t	 uid, euid;		/* real and effective user id's */
79 
80 static char	luser[16];	/* buffer for person */
81 
82 void usage __P((void));
83 
84 int
85 main(argc, argv)
86 	int argc;
87 	char *argv[];
88 {
89 	register char *arg;
90 	struct passwd *p;
91 
92 	uid = getuid();
93 	euid = geteuid();
94 	seteuid(uid);	/* be safe */
95 	name = argv[0];
96 	gethostname(host, sizeof(host));
97 	openlog("lpd", 0, LOG_LPR);
98 	if ((p = getpwuid(getuid())) == NULL)
99 		fatal("Who are you?");
100 	if (strlen(p->pw_name) >= sizeof(luser))
101 		fatal("Your name is too long");
102 	strncpy(luser, p->pw_name, sizeof(luser) - 1);
103 	person = luser;
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("Too many requests");
129 				requ[requests++] = atoi(arg);
130 			} else {
131 				if (users >= MAXUSERS)
132 					fatal("Too many users");
133 				user[users++] = arg;
134 			}
135 		}
136 	}
137 	if (printer == NULL && (printer = getenv("PRINTER")) == NULL)
138 		printer = DEFLP;
139 
140 	rmjob();
141 	exit(0);
142 }
143 
144 void
145 usage()
146 {
147 	fprintf(stderr, "usage: lprm [-] [-Pprinter] [[job #] [user] ...]\n");
148 	exit(2);
149 }
150