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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)rmjob.c 8.2 (Berkeley) 4/28/95";
33 #endif /* not lint */
34 #endif
35
36 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
37 __FBSDID("$FreeBSD: stable/9/usr.sbin/lpr/common_source/rmjob.c 216372 2010-12-11 09:38:12Z joel $");
38
39 #include <sys/param.h>
40 #include <sys/uio.h>
41
42 #include <ctype.h>
43 #include <dirent.h>
44 #include <errno.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #define psignal foil_gcc_psignal
50 #define sys_siglist foil_gcc_siglist
51 #include <unistd.h>
52 #undef psignal
53 #undef sys_siglist
54
55 #include "lp.h"
56 #include "lp.local.h"
57 #include "pathnames.h"
58
59 /*
60 * rmjob - remove the specified jobs from the queue.
61 */
62
63 /*
64 * Stuff for handling lprm specifications
65 */
66 static char root[] = "root";
67 static int all = 0; /* eliminate all files (root only) */
68 static int cur_daemon; /* daemon's pid */
69 static char current[7+MAXHOSTNAMELEN]; /* active control file name */
70
71 extern uid_t uid, euid; /* real and effective user id's */
72
73 static void alarmhandler(int _signo);
74 static void do_unlink(char *_file);
75 static int isowner(char *_owner, char *_file, const char *_cfhost);
76
77 void
rmjob(const char * printer)78 rmjob(const char *printer)
79 {
80 register int i, nitems;
81 int assasinated = 0;
82 struct dirent **files;
83 char *cp;
84 struct printer myprinter, *pp = &myprinter;
85
86 init_printer(pp);
87 if ((i = getprintcap(printer, pp)) < 0)
88 fatal(pp, "getprintcap: %s", pcaperr(i));
89 if ((cp = checkremote(pp))) {
90 printf("Warning: %s\n", cp);
91 free(cp);
92 }
93
94 /*
95 * If the format was `lprm -' and the user isn't the super-user,
96 * then fake things to look like he said `lprm user'.
97 */
98 if (users < 0) {
99 if (getuid() == 0)
100 all = 1; /* all files in local queue */
101 else {
102 user[0] = person;
103 users = 1;
104 }
105 }
106 if (!strcmp(person, "-all")) {
107 if (from_host == local_host)
108 fatal(pp, "The login name \"-all\" is reserved");
109 all = 1; /* all those from 'from_host' */
110 person = root;
111 }
112
113 seteuid(euid);
114 if (chdir(pp->spool_dir) < 0)
115 fatal(pp, "cannot chdir to spool directory");
116 if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
117 fatal(pp, "cannot access spool directory");
118 seteuid(uid);
119
120 if (nitems) {
121 /*
122 * Check for an active printer daemon (in which case we
123 * kill it if it is reading our file) then remove stuff
124 * (after which we have to restart the daemon).
125 */
126 if (lockchk(pp, pp->lock_file) && chk(current)) {
127 seteuid(euid);
128 assasinated = kill(cur_daemon, SIGINT) == 0;
129 seteuid(uid);
130 if (!assasinated)
131 fatal(pp, "cannot kill printer daemon");
132 }
133 /*
134 * process the files
135 */
136 for (i = 0; i < nitems; i++)
137 process(pp, files[i]->d_name);
138 }
139 rmremote(pp);
140 /*
141 * Restart the printer daemon if it was killed
142 */
143 if (assasinated && !startdaemon(pp))
144 fatal(pp, "cannot restart printer daemon\n");
145 exit(0);
146 }
147
148 /*
149 * Process a lock file: collect the pid of the active
150 * daemon and the file name of the active spool entry.
151 * Return boolean indicating existence of a lock file.
152 */
153 int
lockchk(struct printer * pp,char * slockf)154 lockchk(struct printer *pp, char *slockf)
155 {
156 register FILE *fp;
157 register int i, n;
158
159 seteuid(euid);
160 if ((fp = fopen(slockf, "r")) == NULL) {
161 if (errno == EACCES)
162 fatal(pp, "%s: %s", slockf, strerror(errno));
163 else
164 return(0);
165 }
166 seteuid(uid);
167 if (!getline(fp)) {
168 (void) fclose(fp);
169 return(0); /* no daemon present */
170 }
171 cur_daemon = atoi(line);
172 if (kill(cur_daemon, 0) < 0 && errno != EPERM) {
173 (void) fclose(fp);
174 return(0); /* no daemon present */
175 }
176 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
177 if (i > 5) {
178 n = 1;
179 break;
180 }
181 sleep(i);
182 }
183 current[n-1] = '\0';
184 (void) fclose(fp);
185 return(1);
186 }
187
188 /*
189 * Process a control file.
190 */
191 void
process(const struct printer * pp,char * file)192 process(const struct printer *pp, char *file)
193 {
194 FILE *cfp;
195
196 if (!chk(file))
197 return;
198 seteuid(euid);
199 if ((cfp = fopen(file, "r")) == NULL)
200 fatal(pp, "cannot open %s", file);
201 seteuid(uid);
202 while (getline(cfp)) {
203 switch (line[0]) {
204 case 'U': /* unlink associated files */
205 if (strchr(line+1, '/') || strncmp(line+1, "df", 2))
206 break;
207 do_unlink(line+1);
208 }
209 }
210 (void) fclose(cfp);
211 do_unlink(file);
212 }
213
214 static void
do_unlink(char * file)215 do_unlink(char *file)
216 {
217 int ret;
218
219 if (from_host != local_host)
220 printf("%s: ", local_host);
221 seteuid(euid);
222 ret = unlink(file);
223 seteuid(uid);
224 printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file);
225 }
226
227 /*
228 * Do the dirty work in checking
229 */
230 int
chk(char * file)231 chk(char *file)
232 {
233 int *r, jnum;
234 char **u;
235 const char *cfhost;
236 FILE *cfp;
237
238 /*
239 * Check for valid cf file name (mostly checking current).
240 */
241 if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
242 return(0);
243
244 jnum = calc_jobnum(file, &cfhost);
245 if (all && (from_host == local_host || !strcmp(from_host, cfhost)))
246 return(1);
247
248 /*
249 * get the owner's name from the control file.
250 */
251 seteuid(euid);
252 if ((cfp = fopen(file, "r")) == NULL)
253 return(0);
254 seteuid(uid);
255 while (getline(cfp)) {
256 if (line[0] == 'P')
257 break;
258 }
259 (void) fclose(cfp);
260 if (line[0] != 'P')
261 return(0);
262
263 if (users == 0 && requests == 0)
264 return(!strcmp(file, current) && isowner(line+1, file, cfhost));
265 /*
266 * Check the request list
267 */
268 for (r = requ; r < &requ[requests]; r++)
269 if (*r == jnum && isowner(line+1, file, cfhost))
270 return(1);
271 /*
272 * Check to see if it's in the user list
273 */
274 for (u = user; u < &user[users]; u++)
275 if (!strcmp(*u, line+1) && isowner(line+1, file, cfhost))
276 return(1);
277 return(0);
278 }
279
280 /*
281 * If root is removing a file on the local machine, allow it.
282 * If root is removing a file from a remote machine, only allow
283 * files sent from the remote machine to be removed.
284 * Normal users can only remove the file from where it was sent.
285 */
286 static int
isowner(char * owner,char * file,const char * cfhost)287 isowner(char *owner, char *file, const char *cfhost)
288 {
289 if (!strcmp(person, root) && (from_host == local_host ||
290 !strcmp(from_host, cfhost)))
291 return (1);
292 if (!strcmp(person, owner) && !strcmp(from_host, cfhost))
293 return (1);
294 if (from_host != local_host)
295 printf("%s: ", local_host);
296 printf("%s: Permission denied\n", file);
297 return(0);
298 }
299
300 /*
301 * Check to see if we are sending files to a remote machine. If we are,
302 * then try removing files on the remote machine.
303 */
304 void
rmremote(const struct printer * pp)305 rmremote(const struct printer *pp)
306 {
307 int i, elem, firstreq, niov, rem, totlen;
308 char buf[BUFSIZ];
309 void (*savealrm)(int);
310 struct iovec *iov;
311
312 if (!pp->remote)
313 return; /* not sending to a remote machine */
314
315 /*
316 * Flush stdout so the user can see what has been deleted
317 * while we wait (possibly) for the connection.
318 */
319 fflush(stdout);
320
321 /*
322 * Counting:
323 * 4 == "\5" + remote_queue + " " + person
324 * 2 * users == " " + user[i] for each user
325 * requests == asprintf results for each request
326 * 1 == "\n"
327 * Although laborious, doing it this way makes it possible for
328 * us to process requests of indeterminate length without
329 * applying an arbitrary limit. Arbitrary Limits Are Bad (tm).
330 */
331 if (users > 0)
332 niov = 4 + 2 * users + requests + 1;
333 else
334 niov = 4 + requests + 1;
335 iov = malloc(niov * sizeof *iov);
336 if (iov == 0)
337 fatal(pp, "out of memory in rmremote()");
338 iov[0].iov_base = "\5";
339 iov[1].iov_base = pp->remote_queue;
340 iov[2].iov_base = " ";
341 iov[3].iov_base = all ? "-all" : person;
342 elem = 4;
343 for (i = 0; i < users; i++) {
344 iov[elem].iov_base = " ";
345 iov[elem + 1].iov_base = user[i];
346 elem += 2;
347 }
348 firstreq = elem;
349 for (i = 0; i < requests; i++) {
350 asprintf((char **)&iov[elem].iov_base, " %d", requ[i]);
351 if (iov[elem].iov_base == 0)
352 fatal(pp, "out of memory in rmremote()");
353 elem++;
354 }
355 iov[elem++].iov_base = "\n";
356 for (totlen = i = 0; i < niov; i++)
357 totlen += (iov[i].iov_len = strlen(iov[i].iov_base));
358
359 savealrm = signal(SIGALRM, alarmhandler);
360 alarm(pp->conn_timeout);
361 rem = getport(pp, pp->remote_host, 0);
362 (void)signal(SIGALRM, savealrm);
363 if (rem < 0) {
364 if (from_host != local_host)
365 printf("%s: ", local_host);
366 printf("connection to %s is down\n", pp->remote_host);
367 } else {
368 if (writev(rem, iov, niov) != totlen)
369 fatal(pp, "Lost connection");
370 while ((i = read(rem, buf, sizeof(buf))) > 0)
371 (void) fwrite(buf, 1, i, stdout);
372 (void) close(rem);
373 }
374 for (i = 0; i < requests; i++)
375 free(iov[firstreq + i].iov_base);
376 free(iov);
377 }
378
379 /*
380 * Return 1 if the filename begins with 'cf'
381 */
382 int
iscf(const struct dirent * d)383 iscf(const struct dirent *d)
384 {
385 return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
386 }
387
388 void
alarmhandler(int signo __unused)389 alarmhandler(int signo __unused)
390 {
391 /* the signal is ignored */
392 /* (the '__unused' is just to avoid a compile-time warning) */
393 }
394