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 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/uio.h> 44 45 #include <ctype.h> 46 #include <dirent.h> 47 #include <errno.h> 48 #include <signal.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #define psignal foil_gcc_psignal 53 #define sys_siglist foil_gcc_siglist 54 #include <unistd.h> 55 #undef psignal 56 #undef sys_siglist 57 58 #include "lp.h" 59 #include "lp.local.h" 60 #include "pathnames.h" 61 62 /* 63 * rmjob - remove the specified jobs from the queue. 64 */ 65 66 /* 67 * Stuff for handling lprm specifications 68 */ 69 static char root[] = "root"; 70 static int all = 0; /* eliminate all files (root only) */ 71 static int cur_daemon; /* daemon's pid */ 72 static char current[40]; /* active control file name */ 73 74 extern uid_t uid, euid; /* real and effective user id's */ 75 76 static void alarmhandler __P((int)); 77 static void do_unlink __P((char *)); 78 79 void 80 rmjob(printer) 81 const char *printer; 82 { 83 register int i, nitems; 84 int assasinated = 0; 85 struct dirent **files; 86 char *cp; 87 struct printer myprinter, *pp = &myprinter; 88 89 init_printer(pp); 90 if ((i = getprintcap(printer, pp)) < 0) 91 fatal(pp, "getprintcap: %s", pcaperr(i)); 92 if ((cp = checkremote(pp))) { 93 printf("Warning: %s\n", cp); 94 free(cp); 95 } 96 97 /* 98 * If the format was `lprm -' and the user isn't the super-user, 99 * then fake things to look like he said `lprm user'. 100 */ 101 if (users < 0) { 102 if (getuid() == 0) 103 all = 1; /* all files in local queue */ 104 else { 105 user[0] = person; 106 users = 1; 107 } 108 } 109 if (!strcmp(person, "-all")) { 110 if (from == host) 111 fatal(pp, "The login name \"-all\" is reserved"); 112 all = 1; /* all those from 'from' */ 113 person = root; 114 } 115 116 seteuid(euid); 117 if (chdir(pp->spool_dir) < 0) 118 fatal(pp, "cannot chdir to spool directory"); 119 if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 120 fatal(pp, "cannot access spool directory"); 121 seteuid(uid); 122 123 if (nitems) { 124 /* 125 * Check for an active printer daemon (in which case we 126 * kill it if it is reading our file) then remove stuff 127 * (after which we have to restart the daemon). 128 */ 129 if (lockchk(pp, pp->lock_file) && chk(current)) { 130 seteuid(euid); 131 assasinated = kill(cur_daemon, SIGINT) == 0; 132 seteuid(uid); 133 if (!assasinated) 134 fatal(pp, "cannot kill printer daemon"); 135 } 136 /* 137 * process the files 138 */ 139 for (i = 0; i < nitems; i++) 140 process(pp, files[i]->d_name); 141 } 142 rmremote(pp); 143 /* 144 * Restart the printer daemon if it was killed 145 */ 146 if (assasinated && !startdaemon(pp)) 147 fatal(pp, "cannot restart printer daemon\n"); 148 exit(0); 149 } 150 151 /* 152 * Process a lock file: collect the pid of the active 153 * daemon and the file name of the active spool entry. 154 * Return boolean indicating existence of a lock file. 155 */ 156 int 157 lockchk(pp, s) 158 struct printer *pp; 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(pp, "%s: %s", s, strerror(errno)); 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(pp, file) 198 const struct printer *pp; 199 char *file; 200 { 201 FILE *cfp; 202 203 if (!chk(file)) 204 return; 205 seteuid(euid); 206 if ((cfp = fopen(file, "r")) == NULL) 207 fatal(pp, "cannot open %s", file); 208 seteuid(uid); 209 while (getline(cfp)) { 210 switch (line[0]) { 211 case 'U': /* unlink associated files */ 212 if (strchr(line+1, '/') || strncmp(line+1, "df", 2)) 213 break; 214 if (from != host) 215 printf("%s: ", host); 216 do_unlink(line+1); 217 } 218 } 219 (void) fclose(cfp); 220 do_unlink(file); 221 } 222 223 static void 224 do_unlink(file) 225 char *file; 226 { 227 int ret; 228 229 if (from != host) 230 printf("%s: ", host); 231 seteuid(euid); 232 ret = unlink(file); 233 seteuid(uid); 234 printf(ret ? "cannot dequeue %s\n" : "%s dequeued\n", file); 235 } 236 237 /* 238 * Do the dirty work in checking 239 */ 240 int 241 chk(file) 242 char *file; 243 { 244 register int *r, n; 245 register char **u, *cp; 246 FILE *cfp; 247 248 /* 249 * Check for valid cf file name (mostly checking current). 250 */ 251 if (strlen(file) < 7 || file[0] != 'c' || file[1] != 'f') 252 return(0); 253 254 if (all && (from == host || !strcmp(from, file+6))) 255 return(1); 256 257 /* 258 * get the owner's name from the control file. 259 */ 260 seteuid(euid); 261 if ((cfp = fopen(file, "r")) == NULL) 262 return(0); 263 seteuid(uid); 264 while (getline(cfp)) { 265 if (line[0] == 'P') 266 break; 267 } 268 (void) fclose(cfp); 269 if (line[0] != 'P') 270 return(0); 271 272 if (users == 0 && requests == 0) 273 return(!strcmp(file, current) && isowner(line+1, file)); 274 /* 275 * Check the request list 276 */ 277 for (n = 0, cp = file+3; isdigit(*cp); ) 278 n = n * 10 + (*cp++ - '0'); 279 for (r = requ; r < &requ[requests]; r++) 280 if (*r == n && isowner(line+1, file)) 281 return(1); 282 /* 283 * Check to see if it's in the user list 284 */ 285 for (u = user; u < &user[users]; u++) 286 if (!strcmp(*u, line+1) && isowner(line+1, file)) 287 return(1); 288 return(0); 289 } 290 291 /* 292 * If root is removing a file on the local machine, allow it. 293 * If root is removing a file from a remote machine, only allow 294 * files sent from the remote machine to be removed. 295 * Normal users can only remove the file from where it was sent. 296 */ 297 int 298 isowner(owner, file) 299 char *owner, *file; 300 { 301 if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 302 return(1); 303 if (!strcmp(person, owner) && !strcmp(from, file+6)) 304 return(1); 305 if (from != host) 306 printf("%s: ", host); 307 printf("%s: Permission denied\n", file); 308 return(0); 309 } 310 311 /* 312 * Check to see if we are sending files to a remote machine. If we are, 313 * then try removing files on the remote machine. 314 */ 315 void 316 rmremote(pp) 317 const struct printer *pp; 318 { 319 int i, rem, niov, totlen; 320 char buf[BUFSIZ]; 321 void (*savealrm)(int); 322 struct iovec *iov; 323 324 if (!pp->remote) 325 return; /* not sending to a remote machine */ 326 327 /* 328 * Flush stdout so the user can see what has been deleted 329 * while we wait (possibly) for the connection. 330 */ 331 fflush(stdout); 332 333 /* 334 * Counting: 335 * 4 == "\5" + remote_queue + " " + person 336 * 2 * users == " " + user[i] for each user 337 * requests == asprintf results for each request 338 * 1 == "\n" 339 * Although laborious, doing it this way makes it possible for 340 * us to process requests of indeterminate length without 341 * applying an arbitrary limit. Arbitrary Limits Are Bad (tm). 342 */ 343 niov = 4 + 2 * users + requests + 1; 344 iov = malloc(niov * sizeof *iov); 345 if (iov == 0) 346 fatal(pp, "out of memory"); 347 iov[0].iov_base = "\5"; 348 iov[1].iov_base = pp->remote_queue; 349 iov[2].iov_base = " "; 350 iov[3].iov_base = all ? "-all" : person; 351 for (i = 0; i < users; i++) { 352 iov[4 + 2 * i].iov_base = " "; 353 iov[4 + 2 * i + 1].iov_base = user[i]; 354 } 355 for (i = 0; i < requests; i++) { 356 asprintf(&iov[4 + 2 * users + i].iov_base, " %d", requ[i]); 357 if (iov[4 + 2 * users + i].iov_base == 0) 358 fatal(pp, "out of memory"); 359 } 360 iov[4 + 2 * users + requests].iov_base = "\n"; 361 for (totlen = i = 0; i < niov; i++) 362 totlen += (iov[i].iov_len = strlen(iov[i].iov_base)); 363 364 savealrm = signal(SIGALRM, alarmhandler); 365 alarm(pp->conn_timeout); 366 rem = getport(pp, pp->remote_host, 0); 367 (void)signal(SIGALRM, savealrm); 368 if (rem < 0) { 369 if (from != host) 370 printf("%s: ", host); 371 printf("connection to %s is down\n", pp->remote_host); 372 } else { 373 if (writev(rem, iov, niov) != totlen) 374 fatal(pp, "Lost connection"); 375 while ((i = read(rem, buf, sizeof(buf))) > 0) 376 (void) fwrite(buf, 1, i, stdout); 377 (void) close(rem); 378 } 379 for (i = 0; i < requests; i++) 380 free(iov[4 + 2 * users + i].iov_base); 381 free(iov); 382 } 383 384 /* 385 * Return 1 if the filename begins with 'cf' 386 */ 387 int 388 iscf(d) 389 struct dirent *d; 390 { 391 return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 392 } 393 394 void 395 alarmhandler(signo) 396 int signo; 397 { 398 /* ignored */ 399 } 400