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