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