BSD 4_3_Reno development
[unix-history] / .ref-fdb8e4aac282b72d4670aa3a26d9bba07afc7e6f / usr / src / usr.sbin / lpr / common_source / rmjob.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
9d85c861
KB
3 * All rights reserved.
4 *
417f7a11 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
5f84f8f0 8#ifndef lint
417f7a11 9static char sccsid[] = "@(#)rmjob.c 5.7 (Berkeley) %G%";
9d85c861 10#endif /* not lint */
5f84f8f0 11
ccbfa562
RC
12/*
13 * rmjob - remove the specified jobs from the queue.
14 */
15
16#include "lp.h"
7abf8d65 17#include "pathnames.h"
ccbfa562
RC
18
19/*
20 * Stuff for handling lprm specifications
21 */
22extern char *user[]; /* users to process */
23extern int users; /* # of users in user array */
24extern int requ[]; /* job number of spool entries */
25extern int requests; /* # of spool requests */
54266d1f 26extern char *person; /* name of person doing lprm */
ccbfa562 27
3d3c9f9f
RC
28char root[] = "root";
29int all = 0; /* eliminate all files (root only) */
30int cur_daemon; /* daemon's pid */
31char current[40]; /* active control file name */
ccbfa562
RC
32
33int iscf();
34
35rmjob()
36{
37 register int i, nitems;
38 int assasinated = 0;
39 struct direct **files;
b5eb1530 40 char *cp;
ccbfa562 41
ccbfa562
RC
42 if ((i = pgetent(line, printer)) < 0)
43 fatal("cannot open printer description file");
44 else if (i == 0)
45 fatal("unknown printer");
46 if ((SD = pgetstr("sd", &bp)) == NULL)
7abf8d65 47 SD = _PATH_DEFSPOOL;
ccbfa562
RC
48 if ((LO = pgetstr("lo", &bp)) == NULL)
49 LO = DEFLOCK;
50 if ((LP = pgetstr("lp", &bp)) == NULL)
7abf8d65 51 LP = _PATH_DEFDEVLP;
ccbfa562 52 if ((RP = pgetstr("rp", &bp)) == NULL)
4d4caa50 53 RP = DEFLP;
a87bde97 54 RM = pgetstr("rm", &bp);
b5eb1530
TF
55 if (cp = checkremote())
56 printf("Warning: %s\n", cp);
ccbfa562
RC
57
58 /*
59 * If the format was `lprm -' and the user isn't the super-user,
60 * then fake things to look like he said `lprm user'.
61 */
62 if (users < 0) {
63 if (getuid() == 0)
64 all = 1; /* all files in local queue */
65 else {
66 user[0] = person;
67 users = 1;
68 }
69 }
70 if (!strcmp(person, "-all")) {
71 if (from == host)
72 fatal("The login name \"-all\" is reserved");
73 all = 1; /* all those from 'from' */
74 person = root;
75 }
76
77 if (chdir(SD) < 0)
78 fatal("cannot chdir to spool directory");
79 if ((nitems = scandir(".", &files, iscf, NULL)) < 0)
80 fatal("cannot access spool directory");
81
82 if (nitems) {
83 /*
84 * Check for an active printer daemon (in which case we
85 * kill it if it is reading our file) then remove stuff
86 * (after which we have to restart the daemon).
87 */
88 if (lockchk(LO) && chk(current)) {
89 assasinated = kill(cur_daemon, SIGINT) == 0;
90 if (!assasinated)
91 fatal("cannot kill printer daemon");
92 }
93 /*
94 * process the files
95 */
96 for (i = 0; i < nitems; i++)
97 process(files[i]->d_name);
98 }
b5eb1530 99 rmremote();
ccbfa562
RC
100 /*
101 * Restart the printer daemon if it was killed
102 */
775e8534 103 if (assasinated && !startdaemon(printer))
ccbfa562
RC
104 fatal("cannot restart printer daemon\n");
105 exit(0);
106}
107
108/*
109 * Process a lock file: collect the pid of the active
110 * daemon and the file name of the active spool entry.
111 * Return boolean indicating existence of a lock file.
112 */
113lockchk(s)
114 char *s;
115{
116 register FILE *fp;
117 register int i, n;
118
119 if ((fp = fopen(s, "r")) == NULL)
120 if (errno == EACCES)
121 fatal("can't access lock file");
122 else
123 return(0);
8fed920b 124 if (!getline(fp)) {
ccbfa562
RC
125 (void) fclose(fp);
126 return(0); /* no daemon present */
127 }
128 cur_daemon = atoi(line);
8fed920b
RC
129 if (kill(cur_daemon, 0) < 0) {
130 (void) fclose(fp);
131 return(0); /* no daemon present */
132 }
ccbfa562 133 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) {
a87bde97 134 if (i > 5) {
ccbfa562
RC
135 n = 1;
136 break;
137 }
138 sleep(i);
139 }
140 current[n-1] = '\0';
141 (void) fclose(fp);
142 return(1);
143}
144
145/*
146 * Process a control file.
147 */
148process(file)
149 char *file;
150{
151 FILE *cfp;
152
153 if (!chk(file))
154 return;
155 if ((cfp = fopen(file, "r")) == NULL)
156 fatal("cannot open %s", file);
3d3c9f9f 157 while (getline(cfp)) {
ccbfa562
RC
158 switch (line[0]) {
159 case 'U': /* unlink associated files */
160 if (from != host)
161 printf("%s: ", host);
162 printf(unlink(line+1) ? "cannot dequeue %s\n" :
163 "%s dequeued\n", line+1);
164 }
165 }
166 (void) fclose(cfp);
167 if (from != host)
168 printf("%s: ", host);
169 printf(unlink(file) ? "cannot dequeue %s\n" : "%s dequeued\n", file);
170}
171
172/*
173 * Do the dirty work in checking
174 */
175chk(file)
176 char *file;
177{
178 register int *r, n;
179 register char **u, *cp;
180 FILE *cfp;
181
139505f2
RC
182 /*
183 * Check for valid cf file name (mostly checking current).
184 */
185 if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f')
186 return(0);
187
ccbfa562
RC
188 if (all && (from == host || !strcmp(from, file+6)))
189 return(1);
190
191 /*
192 * get the owner's name from the control file.
193 */
194 if ((cfp = fopen(file, "r")) == NULL)
195 return(0);
196 while (getline(cfp)) {
197 if (line[0] == 'P')
198 break;
199 }
200 (void) fclose(cfp);
201 if (line[0] != 'P')
202 return(0);
203
204 if (users == 0 && requests == 0)
205 return(!strcmp(file, current) && isowner(line+1, file));
206 /*
207 * Check the request list
208 */
209 for (n = 0, cp = file+3; isdigit(*cp); )
210 n = n * 10 + (*cp++ - '0');
211 for (r = requ; r < &requ[requests]; r++)
212 if (*r == n && isowner(line+1, file))
213 return(1);
214 /*
215 * Check to see if it's in the user list
216 */
217 for (u = user; u < &user[users]; u++)
218 if (!strcmp(*u, line+1) && isowner(line+1, file))
219 return(1);
220 return(0);
221}
222
223/*
224 * If root is removing a file on the local machine, allow it.
4d4caa50
RC
225 * If root is removing a file from a remote machine, only allow
226 * files sent from the remote machine to be removed.
ccbfa562
RC
227 * Normal users can only remove the file from where it was sent.
228 */
229isowner(owner, file)
230 char *owner, *file;
231{
4d4caa50
RC
232 if (!strcmp(person, root) && (from == host || !strcmp(from, file+6)))
233 return(1);
234 if (!strcmp(person, owner) && !strcmp(from, file+6))
235 return(1);
236 if (from != host)
237 printf("%s: ", host);
238 printf("%s: Permission denied\n", file);
239 return(0);
ccbfa562
RC
240}
241
242/*
243 * Check to see if we are sending files to a remote machine. If we are,
244 * then try removing files on the remote machine.
245 */
b5eb1530 246rmremote()
ccbfa562
RC
247{
248 register char *cp;
249 register int i, rem;
250 char buf[BUFSIZ];
251
b5eb1530 252 if (!sendtorem)
ccbfa562
RC
253 return; /* not sending to a remote machine */
254
4d4caa50
RC
255 /*
256 * Flush stdout so the user can see what has been deleted
257 * while we wait (possibly) for the connection.
258 */
259 fflush(stdout);
260
ccbfa562
RC
261 sprintf(buf, "\5%s %s", RP, all ? "-all" : person);
262 cp = buf;
263 for (i = 0; i < users; i++) {
264 cp += strlen(cp);
265 *cp++ = ' ';
266 strcpy(cp, user[i]);
267 }
268 for (i = 0; i < requests; i++) {
269 cp += strlen(cp);
270 (void) sprintf(cp, " %d", requ[i]);
271 }
272 strcat(cp, "\n");
a87bde97 273 rem = getport(RM);
ccbfa562
RC
274 if (rem < 0) {
275 if (from != host)
276 printf("%s: ", host);
277 printf("connection to %s is down\n", RM);
278 } else {
279 i = strlen(buf);
280 if (write(rem, buf, i) != i)
281 fatal("Lost connection");
282 while ((i = read(rem, buf, sizeof(buf))) > 0)
283 (void) fwrite(buf, 1, i, stdout);
284 (void) close(rem);
285 }
286}
287
288/*
289 * Return 1 if the filename begins with 'cf'
290 */
291iscf(d)
292 struct direct *d;
293{
294 return(d->d_name[0] == 'c' && d->d_name[1] == 'f');
295}