rmjob.c revision 1.10 1 /* $NetBSD: rmjob.c,v 1.10 1997/03/10 06:13:16 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
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 sccsid[] = "@(#)rmjob.c 8.1 (Berkeley) 6/6/93";
38 #endif /* not lint */
39
40 #include <sys/param.h>
41
42 #include <signal.h>
43 #include <errno.h>
44 #include <dirent.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include "lp.h"
51 #include "lp.local.h"
52 #include "pathnames.h"
53
54 /*
55 * rmjob - remove the specified jobs from the queue.
56 */
57
58 /*
59 * Stuff for handling lprm specifications
60 */
61 extern char *user[]; /* users to process */
62 extern int users; /* # of users in user array */
63 extern int requ[]; /* job number of spool entries */
64 extern int requests; /* # of spool requests */
65 extern char *person; /* name of person doing lprm */
66
67 static char root[] = "root";
68 static int all = 0; /* eliminate all files (root only) */
69 static int cur_daemon; /* daemon's pid */
70 static char current[40]; /* active control file name */
71
72 extern uid_t uid, euid; /* real and effective user id's */
73
74 static void do_unlink __P((char *));
75
76 void
77 rmjob()
78 {
79 register int i, nitems;
80 int assasinated = 0;
81 struct dirent **files;
82 char *cp;
83
84 if ((i = cgetent(&bp, printcapdb, printer)) == -2)
85 fatal("can't open printer description file");
86 else if (i == -1)
87 fatal("unknown printer");
88 else if (i == -3)
89 fatal("potential reference loop detected in printcap file");
90 if (cgetstr(bp, "lp", &LP) < 0)
91 LP = _PATH_DEFDEVLP;
92 if (cgetstr(bp, "rp", &RP) < 0)
93 RP = DEFLP;
94 if (cgetstr(bp, "sd", &SD) < 0)
95 SD = _PATH_DEFSPOOL;
96 if (cgetstr(bp,"lo", &LO) < 0)
97 LO = DEFLOCK;
98 cgetstr(bp, "rm", &RM);
99 if (cp = checkremote())
100 printf("Warning: %s\n", cp);
101
102 /*
103 * If the format was `lprm -' and the user isn't the super-user,
104 * then fake things to look like he said `lprm user'.
105 */
106 if (users < 0) {
107 if (getuid() == 0)
108 all = 1; /* all files in local queue */
109 else {
110 user[0] = person;
111 users = 1;
112 }
113 }
114 if (!strcmp(person, "-all")) {
115 if (from == host)
116 fatal("The login name \"-all\" is reserved");
117 all = 1; /* all those from 'from' */
118 person = root;
119 }
120
121 seteuid(euid);
122 if (chdir(SD) < 0)
123 fatal("cannot chdir to spool directory");
124 if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
125 fatal("cannot access spool directory");
126 seteuid(uid);
127
128 if (nitems) {
129 /*
130 * Check for an active printer daemon (in which case we
131 * kill it if it is reading our file) then remove stuff
132 * (after which we have to restart the daemon).
133 */
134 if (lockchk(LO) && chk(current)) {
135 seteuid(euid);
136 assasinated = kill(cur_daemon, SIGINT) == 0;
137 seteuid(uid);
138 if (!assasinated)
139 fatal("cannot kill printer daemon");
140 }
141 /*
142 * process the files
143 */
144 for (i = 0; i < nitems; i++)
145 process(files[i]->d_name);
146 }
147 rmremote();
148 /*
149 * Restart the printer daemon if it was killed
150 */
151 if (assasinated && !startdaemon(printer))
152 fatal("cannot restart printer daemon\n");
153 exit(0);
154 }
155
156 /*
157 * Process a lock file: collect the pid of the active
158 * daemon and the file name of the active spool entry.
159 * Return boolean indicating existence of a lock file.
160 */
161 int
162 lockchk(s)
163 char *s;
164 {
165 register FILE *fp;
166 register int i, n;
167
168 seteuid(euid);
169 if ((fp = fopen(s, "r")) == NULL) {
170 if (errno == EACCES)
171 fatal("can't access lock file");
172 else
173 return(0);
174 }
175 seteuid(uid);
176 if (!getline(fp)) {
177 (void)fclose(fp);
178 return(0); /* no daemon present */
179 }
180 cur_daemon = atoi(line);
181 if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
182 (void)fclose(fp);
183 return(0); /* no daemon present */
184 }
185 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
186 if (i > 5) {
187 n = 1;
188 break;
189 }
190 sleep(i);
191 }
192 current[n-1] = '\0';
193 (void)fclose(fp);
194 return(1);
195 }
196
197 /*
198 * Process a control file.
199 */
200 void
201 process(file)
202 char *file;
203 {
204 FILE *cfp;
205
206 if (!chk(file))
207 return;
208 seteuid(euid);
209 if ((cfp = fopen(file, "r")) == NULL)
210 fatal("cannot open %s", file);
211 seteuid(uid);
212 while (getline(cfp)) {
213 switch (line[0]) {
214 case 'U': /* unlink associated files */
215 do_unlink(line+1);
216 }
217 }
218 (void)fclose(cfp);
219 do_unlink(file);
220 }
221
222 static void
223 do_unlink(file)
224 char *file;
225 {
226 int ret;
227
228 if (from != host)
229 printf("%s: ", host);
230 seteuid(euid);
231 ret = unlink(file);
232 seteuid(uid);
233 printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
234 }
235
236 /*
237 * Do the dirty work in checking
238 */
239 int
240 chk(file)
241 char *file;
242 {
243 register int *r, n;
244 register char **u, *cp;
245 FILE *cfp;
246
247 /*
248 * Check for valid cf file name (mostly checking current).
249 */
250 if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
251 return(0);
252
253 if (all && (from == host || !strcmp(from, file+6)))
254 return(1);
255
256 /*
257 * get the owner's name from the control file.
258 */
259 seteuid(euid);
260 if ((cfp = fopen(file, "r")) == NULL)
261 return(0);
262 seteuid(uid);
263 while (getline(cfp)) {
264 if (line[0] == 'P')
265 break;
266 }
267 (void)fclose(cfp);
268 if (line[0] != 'P')
269 return(0);
270
271 if (users == 0 && requests == 0)
272 return(!strcmp(file, current) && isowner(line+1, file));
273 /*
274 * Check the request list
275 */
276 for (n = 0, cp = file+3; isdigit(*cp); )
277 n = n * 10 + (*cp++ - '0');
278 for (r = requ; r < &requ[requests]; r++)
279 if (*r == n && isowner(line+1, file))
280 return(1);
281 /*
282 * Check to see if it's in the user list
283 */
284 for (u = user; u < &user[users]; u++)
285 if (!strcmp(*u, line+1) && isowner(line+1, file))
286 return(1);
287 return(0);
288 }
289
290 /*
291 * If root is removing a file on the local machine, allow it.
292 * If root is removing a file from a remote machine, only allow
293 * files sent from the remote machine to be removed.
294 * Normal users can only remove the file from where it was sent.
295 */
296 int
297 isowner(owner, file)
298 char *owner, *file;
299 {
300 if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
301 return(1);
302 if (!strcmp(person, owner) && !strcmp(from, file+6))
303 return(1);
304 if (from != host)
305 printf("%s: ", host);
306 printf("%s: Permission denied\n", file);
307 return(0);
308 }
309
310 /*
311 * Check to see if we are sending files to a remote machine. If we are,
312 * then try removing files on the remote machine.
313 */
314 void
315 rmremote()
316 {
317 register char *cp, *s;
318 register int i, rem, len;
319
320 if (!sendtorem)
321 return; /* not sending to a remote machine */
322
323 /*
324 * Flush stdout so the user can see what has been deleted
325 * while we wait (possibly) for the connection.
326 */
327 fflush(stdout);
328
329 /* \5 RP space all */
330 len = 1 + strlen(RP) + 1 + strlen(all ? "-all" : person);
331 for (i = 0; i < users; i++) {
332 len += strlen(user[i]) + 1;
333 }
334 for (i = 0; i < requests; i++) {
335 len += snprintf(line, sizeof(line), " %d", requ[i]);
336 }
337 /* newline nul */
338 len += 2;
339 if (len > sizeof(line))
340 s = malloc(len);
341 else
342 s = line;
343 cp = s;
344
345 cp += snprintf(s, len, "\5%s %s", RP, all ? "-all" : person);
346 for (i = 0; i < users; i++) {
347 *cp++ = ' ';
348 strncpy(cp, user[i], len - (cp - s) - 2);
349 cp += strlen(cp);
350 }
351 for (i = 0; i < requests; i++) {
352 (void)snprintf(cp, len - (cp - s) - 1, " %d", requ[i]);
353 cp += strlen(cp);
354 }
355 cp[0] = '\n';
356 cp[1] = '\0';
357
358 rem = getport(RM);
359 if (rem < 0) {
360 if (from != host)
361 printf("%s: ", host);
362 printf("connection to %s is down\n", RM);
363 } else {
364 if (write(rem, s, len) != len)
365 fatal("Lost connection");
366 if (len > sizeof(line))
367 (void)free(s);
368 while ((i = read(rem, line, sizeof(line))) > 0)
369 (void)fwrite(line, 1, i, stdout);
370 (void)close(rem);
371 }
372 }
373
374 /*
375 * Return 1 if the filename begins with 'cf'
376 */
377 int
378 iscf(d)
379 struct dirent *d;
380 {
381 return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
382 }
383