rmjob.c revision 1.5 1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)rmjob.c 8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39
40 #include <signal.h>
41 #include <errno.h>
42 #include <dirent.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include "lp.h"
49 #include "lp.local.h"
50 #include "pathnames.h"
51
52 /*
53 * rmjob - remove the specified jobs from the queue.
54 */
55
56 /*
57 * Stuff for handling lprm specifications
58 */
59 extern char *user[]; /* users to process */
60 extern int users; /* # of users in user array */
61 extern int requ[]; /* job number of spool entries */
62 extern int requests; /* # of spool requests */
63 extern char *person; /* name of person doing lprm */
64
65 static char root[] = "root";
66 static int all = 0; /* eliminate all files (root only) */
67 static int cur_daemon; /* daemon's pid */
68 static char current[40]; /* active control file name */
69
70 extern uid_t uid, euid; /* real and effective user id's */
71
72 static void do_unlink __P((char *));
73
74 void
75 rmjob()
76 {
77 register int i, nitems;
78 int assasinated = 0;
79 struct dirent **files;
80 char *cp;
81
82 if ((i = cgetent(&bp, printcapdb, printer)) == -2)
83 fatal("can't open printer description file");
84 else if (i == -1)
85 fatal("unknown printer");
86 else if (i == -3)
87 fatal("potential reference loop detected in printcap file");
88 if (cgetstr(bp, "lp", &LP) < 0)
89 LP = _PATH_DEFDEVLP;
90 if (cgetstr(bp, "rp", &RP) < 0)
91 RP = DEFLP;
92 if (cgetstr(bp, "sd", &SD) < 0)
93 SD = _PATH_DEFSPOOL;
94 if (cgetstr(bp,"lo", &LO) < 0)
95 LO = DEFLOCK;
96 cgetstr(bp, "rm", &RM);
97 if (cp = checkremote())
98 printf("Warning: %s\n", cp);
99
100 /*
101 * If the format was `lprm -' and the user isn't the super-user,
102 * then fake things to look like he said `lprm user'.
103 */
104 if (users < 0) {
105 if (getuid() == 0)
106 all = 1; /* all files in local queue */
107 else {
108 user[0] = person;
109 users = 1;
110 }
111 }
112 if (!strcmp(person, "-all")) {
113 if (from == host)
114 fatal("The login name \"-all\" is reserved");
115 all = 1; /* all those from 'from' */
116 person = root;
117 }
118
119 seteuid(euid);
120 if (chdir(SD) < 0)
121 fatal("cannot chdir to spool directory");
122 if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
123 fatal("cannot access spool directory");
124 seteuid(uid);
125
126 if (nitems) {
127 /*
128 * Check for an active printer daemon (in which case we
129 * kill it if it is reading our file) then remove stuff
130 * (after which we have to restart the daemon).
131 */
132 if (lockchk(LO) && chk(current)) {
133 seteuid(euid);
134 assasinated = kill(cur_daemon, SIGINT) == 0;
135 seteuid(uid);
136 if (!assasinated)
137 fatal("cannot kill printer daemon");
138 }
139 /*
140 * process the files
141 */
142 for (i = 0; i < nitems; i++)
143 process(files[i]->d_name);
144 }
145 rmremote();
146 /*
147 * Restart the printer daemon if it was killed
148 */
149 if (assasinated && !startdaemon(printer))
150 fatal("cannot restart printer daemon\n");
151 exit(0);
152 }
153
154 /*
155 * Process a lock file: collect the pid of the active
156 * daemon and the file name of the active spool entry.
157 * Return boolean indicating existence of a lock file.
158 */
159 int
160 lockchk(s)
161 char *s;
162 {
163 register FILE *fp;
164 register int i, n;
165
166 seteuid(euid);
167 if ((fp = fopen(s, "r")) == NULL) {
168 if (errno == EACCES)
169 fatal("can't access lock file");
170 else
171 return(0);
172 }
173 seteuid(uid);
174 if (!getline(fp)) {
175 (void) fclose(fp);
176 return(0); /* no daemon present */
177 }
178 cur_daemon = atoi(line);
179 if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
180 (void) fclose(fp);
181 return(0); /* no daemon present */
182 }
183 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
184 if (i > 5) {
185 n = 1;
186 break;
187 }
188 sleep(i);
189 }
190 current[n-1] = '\0';
191 (void) fclose(fp);
192 return(1);
193 }
194
195 /*
196 * Process a control file.
197 */
198 void
199 process(file)
200 char *file;
201 {
202 FILE *cfp;
203
204 if (!chk(file))
205 return;
206 seteuid(euid);
207 if ((cfp = fopen(file, "r")) == NULL)
208 fatal("cannot open %s", file);
209 seteuid(uid);
210 while (getline(cfp)) {
211 switch (line[0]) {
212 case 'U': /* unlink associated files */
213 do_unlink(file);
214 }
215 }
216 (void) fclose(cfp);
217 do_unlink(file);
218 }
219
220 static void
221 do_unlink(file)
222 char *file;
223 {
224 int ret;
225
226 if (from != host)
227 printf("%s: ", host);
228 seteuid(euid);
229 ret = unlink(file);
230 seteuid(uid);
231 printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
232 }
233
234 /*
235 * Do the dirty work in checking
236 */
237 int
238 chk(file)
239 char *file;
240 {
241 register int *r, n;
242 register char **u, *cp;
243 FILE *cfp;
244
245 /*
246 * Check for valid cf file name (mostly checking current).
247 */
248 if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
249 return(0);
250
251 if (all && (from == host || !strcmp(from, file+6)))
252 return(1);
253
254 /*
255 * get the owner's name from the control file.
256 */
257 seteuid(euid);
258 if ((cfp = fopen(file, "r")) == NULL)
259 return(0);
260 seteuid(uid);
261 while (getline(cfp)) {
262 if (line[0] == 'P')
263 break;
264 }
265 (void) fclose(cfp);
266 if (line[0] != 'P')
267 return(0);
268
269 if (users == 0 && requests == 0)
270 return(!strcmp(file, current) && isowner(line+1, file));
271 /*
272 * Check the request list
273 */
274 for (n = 0, cp = file+3; isdigit(*cp); )
275 n = n * 10 + (*cp++ - '0');
276 for (r = requ; r < &requ[requests]; r++)
277 if (*r == n && isowner(line+1, file))
278 return(1);
279 /*
280 * Check to see if it's in the user list
281 */
282 for (u = user; u < &user[users]; u++)
283 if (!strcmp(*u, line+1) && isowner(line+1, file))
284 return(1);
285 return(0);
286 }
287
288 /*
289 * If root is removing a file on the local machine, allow it.
290 * If root is removing a file from a remote machine, only allow
291 * files sent from the remote machine to be removed.
292 * Normal users can only remove the file from where it was sent.
293 */
294 int
295 isowner(owner, file)
296 char *owner, *file;
297 {
298 if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
299 return(1);
300 if (!strcmp(person, owner) && !strcmp(from, file+6))
301 return(1);
302 if (from != host)
303 printf("%s: ", host);
304 printf("%s: Permission denied\n", file);
305 return(0);
306 }
307
308 /*
309 * Check to see if we are sending files to a remote machine. If we are,
310 * then try removing files on the remote machine.
311 */
312 void
313 rmremote()
314 {
315 register char *cp;
316 register int i, rem;
317 char buf[BUFSIZ];
318
319 if (!sendtorem)
320 return; /* not sending to a remote machine */
321
322 /*
323 * Flush stdout so the user can see what has been deleted
324 * while we wait (possibly) for the connection.
325 */
326 fflush(stdout);
327
328 (void)snprintf(buf, sizeof(buf), "\5%s %s", RP, all ? "-all" : person);
329 cp = buf;
330 for (i = 0; i < users; i++) {
331 cp += strlen(cp);
332 *cp++ = ' ';
333 strcpy(cp, user[i]);
334 }
335 for (i = 0; i < requests; i++) {
336 cp += strlen(cp);
337 (void) sprintf(cp, " %d", requ[i]);
338 }
339 strcat(cp, "\n");
340 rem = getport(RM);
341 if (rem < 0) {
342 if (from != host)
343 printf("%s: ", host);
344 printf("connection to %s is down\n", RM);
345 } else {
346 i = strlen(buf);
347 if (write(rem, buf, i) != i)
348 fatal("Lost connection");
349 while ((i = read(rem, buf, sizeof(buf))) > 0)
350 (void) fwrite(buf, 1, i, stdout);
351 (void) close(rem);
352 }
353 }
354
355 /*
356 * Return 1 if the filename begins with 'cf'
357 */
358 int
359 iscf(d)
360 struct dirent *d;
361 {
362 return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
363 }
364