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