1 /* $NetBSD: rmjob.c,v 1.20 2004/10/30 08:44:26 dsl 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)rmjob.c 8.2 (Berkeley) 4/28/95"; 36 #else 37 __RCSID("$NetBSD: rmjob.c,v 1.20 2004/10/30 08:44:26 dsl Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 43 #include <signal.h> 44 #include <errno.h> 45 #include <dirent.h> 46 #include <unistd.h> 47 #include <stdlib.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include <ctype.h> 51 #include "lp.h" 52 #include "lp.local.h" 53 #include "pathnames.h" 54 55 /* 56 * rmjob - remove the specified jobs from the queue. 57 */ 58 59 /* 60 * Stuff for handling lprm specifications 61 */ 62 extern char *user[]; /* users to process */ 63 extern int users; /* # of users in user array */ 64 extern int requ[]; /* job number of spool entries */ 65 extern int requests; /* # of spool requests */ 66 extern char *person; /* name of person doing lprm */ 67 68 static char root[] = "root"; 69 static int all = 0; /* eliminate all files (root only) */ 70 static int cur_daemon; /* daemon's pid */ 71 static char current[40]; /* active control file name */ 72 73 extern uid_t uid, euid; /* real and effective user id's */ 74 75 static void do_unlink(char *); 76 static void alarmer(int); 77 78 void 79 rmjob(void) 80 { 81 int i, nitems; 82 int assasinated = 0; 83 struct dirent **files; 84 char *cp; 85 86 if ((i = cgetent(&bp, printcapdb, printer)) == -2) 87 fatal("can't open printer description file"); 88 else if (i == -1) 89 fatal("unknown printer"); 90 else if (i == -3) 91 fatal("potential reference loop detected in printcap file"); 92 if (cgetstr(bp, DEFLP, &LP) < 0) 93 LP = _PATH_DEFDEVLP; 94 if (cgetstr(bp, "rp", &RP) < 0) 95 RP = DEFLP; 96 if (cgetstr(bp, "sd", &SD) < 0) 97 SD = _PATH_DEFSPOOL; 98 if (cgetstr(bp,"lo", &LO) < 0) 99 LO = DEFLOCK; 100 cgetstr(bp, "rm", &RM); 101 if ((cp = checkremote()) != NULL) 102 printf("Warning: %s\n", cp); 103 104 /* 105 * If the format was `lprm -' and the user isn't the super-user, 106 * then fake things to look like he said `lprm user'. 107 */ 108 if (users < 0) { 109 if (getuid() == 0) 110 all = 1; /* all files in local queue */ 111 else { 112 user[0] = person; 113 users = 1; 114 } 115 } 116 if (!strcmp(person, "-all")) { 117 if (from == host) 118 fatal("The login name \"-all\" is reserved"); 119 all = 1; /* all those from 'from' */ 120 person = root; 121 } 122 123 seteuid(euid); 124 if (chdir(SD) < 0) 125 fatal("cannot chdir to spool directory"); 126 if ((nitems = scandir(".", &files, iscf, NULL)) < 0) 127 fatal("cannot access spool directory"); 128 seteuid(uid); 129 130 if (nitems) { 131 /* 132 * Check for an active printer daemon (in which case we 133 * kill it if it is reading our file) then remove stuff 134 * (after which we have to restart the daemon). 135 */ 136 if (lockchk(LO) && chk(current)) { 137 seteuid(euid); 138 assasinated = kill(cur_daemon, SIGINT) == 0; 139 seteuid(uid); 140 if (!assasinated) 141 fatal("cannot kill printer daemon"); 142 } 143 /* 144 * process the files 145 */ 146 for (i = 0; i < nitems; i++) 147 process(files[i]->d_name); 148 } 149 rmremote(); 150 /* 151 * Restart the printer daemon if it was killed 152 */ 153 if (assasinated && !startdaemon(printer)) 154 fatal("cannot restart printer daemon\n"); 155 exit(0); 156 } 157 158 /* 159 * Process a lock file: collect the pid of the active 160 * daemon and the file name of the active spool entry. 161 * Return boolean indicating existence of a lock file. 162 */ 163 int 164 lockchk(char *s) 165 { 166 FILE *fp; 167 int i, n; 168 169 seteuid(euid); 170 if ((fp = fopen(s, "r")) == NULL) { 171 if (errno == EACCES) 172 fatal("can't access lock file"); 173 else 174 return(0); 175 } 176 seteuid(uid); 177 if (!getline(fp)) { 178 (void)fclose(fp); 179 return(0); /* no daemon present */ 180 } 181 cur_daemon = atoi(line); 182 if (kill(cur_daemon, 0) < 0 && errno != EPERM) { 183 (void)fclose(fp); 184 return(0); /* no daemon present */ 185 } 186 for (i = 1; (n = fread(current, sizeof(char), sizeof(current), fp)) <= 0; i++) { 187 if (i > 5) { 188 n = 1; 189 break; 190 } 191 sleep((size_t)i); 192 } 193 current[n-1] = '\0'; 194 (void)fclose(fp); 195 return(1); 196 } 197 198 /* 199 * Process a control file. 200 */ 201 void 202 process(char *file) 203 { 204 FILE *cfp; 205 206 if (!chk(file)) 207 return; 208 seteuid(euid); 209 if ((cfp = fopen(file, "r")) == NULL) 210 fatal("cannot open %s", file); 211 seteuid(uid); 212 while (getline(cfp)) { 213 switch (line[0]) { 214 case 'U': /* unlink associated files */ 215 if (strchr(line+1, '/') || strncmp(line+1, "df", 2)) 216 break; 217 do_unlink(line+1); 218 } 219 } 220 (void)fclose(cfp); 221 do_unlink(file); 222 } 223 224 static void 225 do_unlink(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(char *file) 242 { 243 int *r, n; 244 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((unsigned char)*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(char *owner, char *file) 298 { 299 if (!strcmp(person, root) && (from == host || !strcmp(from, file+6))) 300 return(1); 301 if (!strcmp(person, owner) && !strcmp(from, file+6)) 302 return(1); 303 if (from != host) 304 printf("%s: ", host); 305 printf("%s: Permission denied\n", file); 306 return(0); 307 } 308 309 /* 310 * Check to see if we are sending files to a remote machine. If we are, 311 * then try removing files on the remote machine. 312 */ 313 void 314 rmremote(void) 315 { 316 char *cp, *s; 317 int i, rem; 318 size_t len; 319 320 if (!remote) 321 return; /* not sending to a remote machine */ 322 323 /* 324 * Flush stdout so the user can see what has been deleted 325 * while we wait (possibly) for the connection. 326 */ 327 fflush(stdout); 328 329 /* \5 RP space all */ 330 len = 1 + strlen(RP) + 1 + strlen(all ? "-all" : person); 331 for (i = 0; i < users; i++) { 332 len += strlen(user[i]) + 1; 333 } 334 for (i = 0; i < requests; i++) { 335 len += snprintf(line, sizeof(line), " %d", requ[i]); 336 } 337 /* newline nul */ 338 len += 2; 339 if (len > sizeof(line)) 340 s = malloc(len); 341 else 342 s = line; 343 cp = s; 344 345 cp += snprintf(s, len, "\5%s %s", RP, all ? "-all" : person); 346 for (i = 0; i < users; i++) { 347 *cp++ = ' '; 348 strncpy(cp, user[i], len - (cp - s) - 2); 349 cp += strlen(cp); 350 } 351 for (i = 0; i < requests; i++) { 352 (void)snprintf(cp, len - (cp - s) - 1, " %d", requ[i]); 353 cp += strlen(cp); 354 } 355 cp[0] = '\n'; 356 cp[1] = '\0'; 357 358 rem = getport(RM, 0); 359 if (rem < 0) { 360 if (from != host) 361 printf("%s: ", host); 362 printf("connection to %s is down\n", RM); 363 } else { 364 struct sigaction osa, nsa; 365 366 if (write(rem, s, len) != len) 367 fatal("Lost connection"); 368 if (len > sizeof(line)) 369 (void)free(s); 370 nsa.sa_handler = alarmer; 371 sigemptyset(&nsa.sa_mask); 372 sigaddset(&nsa.sa_mask, SIGALRM); 373 nsa.sa_flags = 0; 374 (void)sigaction(SIGALRM, &nsa, &osa); 375 alarm(wait_time); 376 while ((i = read(rem, line, sizeof(line))) > 0) { 377 (void)fwrite(line, 1, (size_t)i, stdout); 378 alarm(wait_time); 379 } 380 alarm(0); 381 (void)sigaction(SIGALRM, &osa, NULL); 382 (void)close(rem); 383 } 384 } 385 386 static void 387 alarmer(int s) 388 { 389 /* nothing */ 390 } 391 392 /* 393 * Return 1 if the filename begins with 'cf' 394 */ 395 int 396 iscf(const struct dirent *d) 397 { 398 return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); 399 } 400