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