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