1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1983, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 41 #ifndef lint 42 /* 43 static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95"; 44 */ 45 static const char rcsid[] = 46 "$FreeBSD$"; 47 #endif /* not lint */ 48 49 /* 50 * lpc -- line printer control program -- commands: 51 */ 52 53 #include <sys/param.h> 54 #include <sys/time.h> 55 #include <sys/stat.h> 56 #include <sys/file.h> 57 58 #include <signal.h> 59 #include <fcntl.h> 60 #include <errno.h> 61 #include <dirent.h> 62 #include <unistd.h> 63 #include <stdlib.h> 64 #include <stdio.h> 65 #include <ctype.h> 66 #include <string.h> 67 #include "lp.h" 68 #include "lp.local.h" 69 #include "lpc.h" 70 #include "extern.h" 71 #include "pathnames.h" 72 73 static void abortpr(struct printer *_pp, int _dis); 74 static int doarg(char *_job); 75 static int doselect(struct dirent *_d); 76 static void putmsg(struct printer *_pp, int _argc, char **_argv); 77 static int sortq(const void *_a, const void *_b); 78 static void startpr(struct printer *_pp, int _chgenable); 79 static int touch(struct jobqueue *_jq); 80 static void unlinkf(char *_name); 81 static void upstat(struct printer *_pp, const char *_msg); 82 static void wrapup_clean(int _laststatus); 83 84 /* 85 * generic framework for commands which operate on all or a specified 86 * set of printers 87 */ 88 enum qsel_val { /* how a given ptr was selected */ 89 QSEL_UNKNOWN = -1, /* ... not selected yet */ 90 QSEL_BYNAME = 0, /* ... user specifed it by name */ 91 QSEL_ALL = 1 /* ... user wants "all" printers */ 92 /* (with more to come) */ 93 }; 94 95 static enum qsel_val generic_qselect; /* indicates how ptr was selected */ 96 static int generic_initerr; /* result of initrtn processing */ 97 static char *generic_nullarg; 98 static void (*generic_wrapup)(int _last_status); /* perform rtn wrap-up */ 99 100 void 101 generic(void (*specificrtn)(struct printer *_pp), 102 void (*initrtn)(int _argc, char *_argv[]), int argc, char *argv[]) 103 { 104 int cmdstatus, more, targc; 105 struct printer myprinter, *pp; 106 char **targv; 107 108 if (argc == 1) { 109 printf("usage: %s {all | printer ...}\n", argv[0]); 110 return; 111 } 112 113 /* 114 * The initialization routine for a command might set a generic 115 * "wrapup" routine, which should be called after processing all 116 * the printers in the command. This might print summary info. 117 * 118 * Note that the initialization routine may also parse (and 119 * nullify) some of the parameters given on the command, leaving 120 * only the parameters which have to do with printer names. 121 */ 122 pp = &myprinter; 123 generic_wrapup = NULL; 124 generic_qselect = QSEL_UNKNOWN; 125 cmdstatus = 0; 126 /* this just needs to be a distinct value of type 'char *' */ 127 if (generic_nullarg == NULL) 128 generic_nullarg = strdup(""); 129 130 /* call initialization routine, if there is one for this cmd */ 131 if (initrtn != NULL) { 132 generic_initerr = 0; 133 (*initrtn)(argc, argv); 134 if (generic_initerr) 135 return; 136 /* skip any initial arguments null-ified by initrtn */ 137 targc = argc; 138 targv = argv; 139 while (--targc) { 140 if (targv[1] != generic_nullarg) 141 break; 142 ++targv; 143 } 144 if (targv != argv) { 145 targv[0] = argv[0]; /* copy the command-name */ 146 argv = targv; 147 argc = targc + 1; 148 } 149 } 150 151 if (argc == 2 && strcmp(argv[1], "all") == 0) { 152 generic_qselect = QSEL_ALL; 153 more = firstprinter(pp, &cmdstatus); 154 if (cmdstatus) 155 goto looperr; 156 while (more) { 157 (*specificrtn)(pp); 158 do { 159 more = nextprinter(pp, &cmdstatus); 160 looperr: 161 switch (cmdstatus) { 162 case PCAPERR_TCOPEN: 163 printf("warning: %s: unresolved " 164 "tc= reference(s) ", 165 pp->printer); 166 case PCAPERR_SUCCESS: 167 break; 168 default: 169 fatal(pp, "%s", pcaperr(cmdstatus)); 170 } 171 } while (more && cmdstatus); 172 } 173 goto wrapup; 174 } 175 176 generic_qselect = QSEL_BYNAME; /* specifically-named ptrs */ 177 while (--argc) { 178 ++argv; 179 if (*argv == generic_nullarg) 180 continue; 181 init_printer(pp); 182 cmdstatus = getprintcap(*argv, pp); 183 switch (cmdstatus) { 184 default: 185 fatal(pp, "%s", pcaperr(cmdstatus)); 186 case PCAPERR_NOTFOUND: 187 printf("unknown printer %s\n", *argv); 188 continue; 189 case PCAPERR_TCOPEN: 190 printf("warning: %s: unresolved tc= reference(s)\n", 191 *argv); 192 break; 193 case PCAPERR_SUCCESS: 194 break; 195 } 196 (*specificrtn)(pp); 197 } 198 199 wrapup: 200 if (generic_wrapup) { 201 (*generic_wrapup)(cmdstatus); 202 } 203 204 } 205 206 /* 207 * kill an existing daemon and disable printing. 208 */ 209 void 210 doabort(struct printer *pp) 211 { 212 abortpr(pp, 1); 213 } 214 215 static void 216 abortpr(struct printer *pp, int dis) 217 { 218 register FILE *fp; 219 struct stat stbuf; 220 int pid, fd; 221 char lf[MAXPATHLEN]; 222 223 lock_file_name(pp, lf, sizeof lf); 224 printf("%s:\n", pp->printer); 225 226 /* 227 * Turn on the owner execute bit of the lock file to disable printing. 228 */ 229 if (dis) { 230 seteuid(euid); 231 if (stat(lf, &stbuf) >= 0) { 232 if (chmod(lf, stbuf.st_mode | LFM_PRINT_DIS) < 0) 233 printf("\tcannot disable printing: %s\n", 234 strerror(errno)); 235 else { 236 upstat(pp, "printing disabled\n"); 237 printf("\tprinting disabled\n"); 238 } 239 } else if (errno == ENOENT) { 240 if ((fd = open(lf, O_WRONLY|O_CREAT, 241 LOCK_FILE_MODE | LFM_PRINT_DIS)) < 0) 242 printf("\tcannot create lock file: %s\n", 243 strerror(errno)); 244 else { 245 (void) close(fd); 246 upstat(pp, "printing disabled\n"); 247 printf("\tprinting disabled\n"); 248 printf("\tno daemon to abort\n"); 249 } 250 goto out; 251 } else { 252 printf("\tcannot stat lock file\n"); 253 goto out; 254 } 255 } 256 /* 257 * Kill the current daemon to stop printing now. 258 */ 259 if ((fp = fopen(lf, "r")) == NULL) { 260 printf("\tcannot open lock file\n"); 261 goto out; 262 } 263 if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) { 264 (void) fclose(fp); /* unlocks as well */ 265 printf("\tno daemon to abort\n"); 266 goto out; 267 } 268 (void) fclose(fp); 269 if (kill(pid = atoi(line), SIGTERM) < 0) { 270 if (errno == ESRCH) 271 printf("\tno daemon to abort\n"); 272 else 273 printf("\tWarning: daemon (pid %d) not killed\n", pid); 274 } else 275 printf("\tdaemon (pid %d) killed\n", pid); 276 out: 277 seteuid(uid); 278 } 279 280 /* 281 * Write a message into the status file. 282 */ 283 static void 284 upstat(struct printer *pp, const char *msg) 285 { 286 register int fd; 287 char statfile[MAXPATHLEN]; 288 289 status_file_name(pp, statfile, sizeof statfile); 290 umask(0); 291 fd = open(statfile, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 292 if (fd < 0) { 293 printf("\tcannot create status file: %s\n", strerror(errno)); 294 return; 295 } 296 (void) ftruncate(fd, 0); 297 if (msg == (char *)NULL) 298 (void) write(fd, "\n", 1); 299 else 300 (void) write(fd, msg, strlen(msg)); 301 (void) close(fd); 302 } 303 304 /* 305 * "global" variables for all the routines related to 'clean' and 'tclean' 306 */ 307 static time_t cln_now; /* current time */ 308 static double cln_minage; /* minimum age before file is removed */ 309 static long cln_sizecnt; /* amount of space freed up */ 310 static int cln_debug; /* print extra debugging msgs */ 311 static int cln_filecnt; /* number of files destroyed */ 312 static int cln_foundcore; /* found a core file! */ 313 static int cln_queuecnt; /* number of queues checked */ 314 static int cln_testonly; /* remove-files vs just-print-info */ 315 316 static int 317 doselect(struct dirent *d) 318 { 319 int c = d->d_name[0]; 320 321 if ((c == 'c' || c == 'd' || c == 'r' || c == 't') && 322 d->d_name[1] == 'f') 323 return 1; 324 if (c == 'c') { 325 if (!strcmp(d->d_name, "core")) 326 cln_foundcore = 1; 327 } 328 if (c == 'e') { 329 if (!strncmp(d->d_name, "errs.", 5)) 330 return 1; 331 } 332 return 0; 333 } 334 335 /* 336 * Comparison routine that clean_q() uses for scandir. 337 * 338 * The purpose of this sort is to have all `df' files end up immediately 339 * after the matching `cf' file. For files matching `cf', `df', `rf', or 340 * `tf', it sorts by job number and machine, then by `cf', `df', `rf', or 341 * `tf', and then by the sequence letter (which is A-Z, or a-z). This 342 * routine may also see filenames which do not start with `cf', `df', `rf', 343 * or `tf' (such as `errs.*'), and those are simply sorted by the full 344 * filename. 345 * 346 * XXX 347 * This assumes that all control files start with `cfA*', and it turns 348 * out there are a few implementations of lpr which will create `cfB*' 349 * filenames (they will have datafile names which start with `dfB*'). 350 */ 351 static int 352 sortq(const void *a, const void *b) 353 { 354 const int a_lt_b = -1, a_gt_b = 1, cat_other = 10; 355 const char *fname_a, *fname_b, *jnum_a, *jnum_b; 356 int cat_a, cat_b, ch, res, seq_a, seq_b; 357 358 fname_a = (*(const struct dirent * const *)a)->d_name; 359 fname_b = (*(const struct dirent * const *)b)->d_name; 360 361 /* 362 * First separate filenames into cagatories. Catagories are 363 * legitimate `cf', `df', `rf' & `tf' filenames, and "other" - in 364 * that order. It is critical that the mapping be exactly the 365 * same for 'a' vs 'b', so define a macro for the job. 366 * 367 * [aside: the standard `cf' file has the jobnumber start in 368 * position 4, but some implementations have that as an extra 369 * file-sequence letter, and start the job number in position 5.] 370 */ 371 #define MAP_TO_CAT(fname_X,cat_X,jnum_X,seq_X) do { \ 372 cat_X = cat_other; \ 373 ch = *(fname_X + 2); \ 374 jnum_X = fname_X + 3; \ 375 seq_X = 0; \ 376 if ((*(fname_X + 1) == 'f') && (isalpha(ch))) { \ 377 seq_X = ch; \ 378 if (*fname_X == 'c') \ 379 cat_X = 1; \ 380 else if (*fname_X == 'd') \ 381 cat_X = 2; \ 382 else if (*fname_X == 'r') \ 383 cat_X = 3; \ 384 else if (*fname_X == 't') \ 385 cat_X = 4; \ 386 if (cat_X != cat_other) { \ 387 ch = *jnum_X; \ 388 if (!isdigit(ch)) { \ 389 if (isalpha(ch)) { \ 390 jnum_X++; \ 391 ch = *jnum_X; \ 392 seq_X = (seq_X << 8) + ch; \ 393 } \ 394 if (!isdigit(ch)) \ 395 cat_X = cat_other; \ 396 } \ 397 } \ 398 } \ 399 } while (0) 400 401 MAP_TO_CAT(fname_a, cat_a, jnum_a, seq_a); 402 MAP_TO_CAT(fname_b, cat_b, jnum_b, seq_b); 403 404 #undef MAP_TO_CAT 405 406 /* First handle all cases which have "other" files */ 407 if ((cat_a >= cat_other) || (cat_b >= cat_other)) { 408 /* for two "other" files, just compare the full name */ 409 if (cat_a == cat_b) 410 res = strcmp(fname_a, fname_b); 411 else if (cat_a < cat_b) 412 res = a_lt_b; 413 else 414 res = a_gt_b; 415 goto have_res; 416 } 417 418 /* 419 * At this point, we know both files are legitimate `cf', `df', `rf', 420 * or `tf' files. Compare them by job-number and machine name. 421 */ 422 res = strcmp(jnum_a, jnum_b); 423 if (res != 0) 424 goto have_res; 425 426 /* 427 * We have two files which belong to the same job. Sort based 428 * on the catagory of file (`c' before `d', etc). 429 */ 430 if (cat_a < cat_b) { 431 res = a_lt_b; 432 goto have_res; 433 } else if (cat_a > cat_b) { 434 res = a_gt_b; 435 goto have_res; 436 } 437 438 /* 439 * Two files in the same catagory for a single job. Sort based 440 * on the sequence letter(s). (usually `A' thru `Z', etc). 441 */ 442 if (seq_a < seq_b) { 443 res = a_lt_b; 444 goto have_res; 445 } else if (seq_a > seq_b) { 446 res = a_gt_b; 447 goto have_res; 448 } 449 450 /* 451 * Given that the filenames in a directory are unique, this SHOULD 452 * never happen (unless there are logic errors in this routine). 453 * But if it does happen, we must return "is equal" or the caller 454 * might see inconsistent results in the sorting order, and that 455 * can trigger other problems. 456 */ 457 printf("\t*** Error in sortq: %s == %s !\n", fname_a, fname_b); 458 printf("\t*** cat %d == %d ; seq = %d %d\n", cat_a, cat_b, 459 seq_a, seq_b); 460 res = 0; 461 462 have_res: 463 return res; 464 } 465 466 /* 467 * Remove all spool files and temporaries from the spooling area. 468 * Or, perhaps: 469 * Remove incomplete jobs from spooling area. 470 */ 471 472 void 473 init_clean(int argc, char *argv[]) 474 { 475 476 /* init some fields before 'clean' is called for each queue */ 477 cln_queuecnt = 0; 478 cln_now = time(NULL); 479 cln_minage = 3600.0; /* only delete files >1h old */ 480 cln_filecnt = 0; 481 cln_sizecnt = 0; 482 cln_debug = 0; 483 cln_testonly = 0; 484 generic_wrapup = &wrapup_clean; 485 486 /* see if there are any options specified before the ptr list */ 487 while (--argc) { 488 ++argv; 489 if (**argv != '-') 490 break; 491 if (strcmp(*argv, "-d") == 0) { 492 /* just an example of an option... */ 493 cln_debug++; 494 *argv = generic_nullarg; /* "erase" it */ 495 } else { 496 printf("Invalid option '%s'\n", *argv); 497 generic_initerr = 1; 498 } 499 } 500 501 return; 502 } 503 504 void 505 init_tclean(int argc, char *argv[]) 506 { 507 508 /* only difference between 'clean' and 'tclean' is one value */ 509 /* (...and the fact that 'clean' is priv and 'tclean' is not) */ 510 init_clean(argc, argv); 511 cln_testonly = 1; 512 513 return; 514 } 515 516 void 517 clean_q(struct printer *pp) 518 { 519 char *cp, *cp1, *lp; 520 struct dirent **queue; 521 size_t linerem; 522 int didhead, i, n, nitems, rmcp; 523 524 cln_queuecnt++; 525 526 didhead = 0; 527 if (generic_qselect == QSEL_BYNAME) { 528 printf("%s:\n", pp->printer); 529 didhead = 1; 530 } 531 532 lp = line; 533 cp = pp->spool_dir; 534 while (lp < &line[sizeof(line) - 1]) { 535 if ((*lp++ = *cp++) == 0) 536 break; 537 } 538 lp[-1] = '/'; 539 linerem = sizeof(line) - (lp - line); 540 541 cln_foundcore = 0; 542 seteuid(euid); 543 nitems = scandir(pp->spool_dir, &queue, doselect, sortq); 544 seteuid(uid); 545 if (nitems < 0) { 546 if (!didhead) { 547 printf("%s:\n", pp->printer); 548 didhead = 1; 549 } 550 printf("\tcannot examine spool directory\n"); 551 return; 552 } 553 if (cln_foundcore) { 554 if (!didhead) { 555 printf("%s:\n", pp->printer); 556 didhead = 1; 557 } 558 printf("\t** found a core file in %s !\n", pp->spool_dir); 559 } 560 if (nitems == 0) 561 return; 562 if (!didhead) 563 printf("%s:\n", pp->printer); 564 if (cln_debug) { 565 printf("\t** ----- Sorted list of files being checked:\n"); 566 i = 0; 567 do { 568 cp = queue[i]->d_name; 569 printf("\t** [%3d] = %s\n", i, cp); 570 } while (++i < nitems); 571 printf("\t** ----- end of sorted list\n"); 572 } 573 i = 0; 574 do { 575 cp = queue[i]->d_name; 576 rmcp = 0; 577 if (*cp == 'c') { 578 /* 579 * A control file. Look for matching data-files. 580 */ 581 /* XXX 582 * Note the logic here assumes that the hostname 583 * part of cf-filenames match the hostname part 584 * in df-filenames, and that is not necessarily 585 * true (eg: for multi-homed hosts). This needs 586 * some further thought... 587 */ 588 n = 0; 589 while (i + 1 < nitems) { 590 cp1 = queue[i + 1]->d_name; 591 if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3)) 592 break; 593 i++; 594 n++; 595 } 596 if (n == 0) { 597 rmcp = 1; 598 } 599 } else if (*cp == 'e') { 600 /* 601 * Must be an errrs or email temp file. 602 */ 603 rmcp = 1; 604 } else { 605 /* 606 * Must be a df with no cf (otherwise, it would have 607 * been skipped above) or an rf or tf file (which can 608 * always be removed if it is old enough). 609 */ 610 rmcp = 1; 611 } 612 if (rmcp) { 613 if (strlen(cp) >= linerem) { 614 printf("\t** internal error: 'line' overflow!\n"); 615 printf("\t** spooldir = %s\n", pp->spool_dir); 616 printf("\t** cp = %s\n", cp); 617 return; 618 } 619 strlcpy(lp, cp, linerem); 620 unlinkf(line); 621 } 622 } while (++i < nitems); 623 } 624 625 static void 626 wrapup_clean(int laststatus __unused) 627 { 628 629 printf("Checked %d queues, and ", cln_queuecnt); 630 if (cln_filecnt < 1) { 631 printf("no cruft was found\n"); 632 return; 633 } 634 if (cln_testonly) { 635 printf("would have "); 636 } 637 printf("removed %d files (%ld bytes).\n", cln_filecnt, cln_sizecnt); 638 } 639 640 static void 641 unlinkf(char *name) 642 { 643 struct stat stbuf; 644 double agemod, agestat; 645 int res; 646 char linkbuf[BUFSIZ]; 647 648 /* 649 * We have to use lstat() instead of stat(), in case this is a df* 650 * "file" which is really a symlink due to 'lpr -s' processing. In 651 * that case, we need to check the last-mod time of the symlink, and 652 * not the file that the symlink is pointed at. 653 */ 654 seteuid(euid); 655 res = lstat(name, &stbuf); 656 seteuid(uid); 657 if (res < 0) { 658 printf("\terror return from stat(%s):\n", name); 659 printf("\t %s\n", strerror(errno)); 660 return; 661 } 662 663 agemod = difftime(cln_now, stbuf.st_mtime); 664 agestat = difftime(cln_now, stbuf.st_ctime); 665 if (cln_debug > 1) { 666 /* this debugging-aid probably is not needed any more... */ 667 printf("\t\t modify age=%g secs, stat age=%g secs\n", 668 agemod, agestat); 669 } 670 if ((agemod <= cln_minage) && (agestat <= cln_minage)) 671 return; 672 673 /* 674 * if this file is a symlink, then find out the target of the 675 * symlink before unlink-ing the file itself 676 */ 677 if (S_ISLNK(stbuf.st_mode)) { 678 seteuid(euid); 679 res = readlink(name, linkbuf, sizeof(linkbuf)); 680 seteuid(uid); 681 if (res < 0) { 682 printf("\terror return from readlink(%s):\n", name); 683 printf("\t %s\n", strerror(errno)); 684 return; 685 } 686 if (res == sizeof(linkbuf)) 687 res--; 688 linkbuf[res] = '\0'; 689 } 690 691 cln_filecnt++; 692 cln_sizecnt += stbuf.st_size; 693 694 if (cln_testonly) { 695 printf("\twould remove %s\n", name); 696 if (S_ISLNK(stbuf.st_mode)) { 697 printf("\t (which is a symlink to %s)\n", linkbuf); 698 } 699 } else { 700 seteuid(euid); 701 res = unlink(name); 702 seteuid(uid); 703 if (res < 0) 704 printf("\tcannot remove %s (!)\n", name); 705 else 706 printf("\tremoved %s\n", name); 707 /* XXX 708 * Note that for a df* file, this code should also check to see 709 * if it is a symlink to some other file, and if the original 710 * lpr command included '-r' ("remove file"). Of course, this 711 * code would not be removing the df* file unless there was no 712 * matching cf* file, and without the cf* file it is currently 713 * impossible to determine if '-r' had been specified... 714 * 715 * As a result of this quandry, we may be leaving behind a 716 * user's file that was supposed to have been removed after 717 * being printed. This may effect services such as CAP or 718 * samba, if they were configured to use 'lpr -r', and if 719 * datafiles are not being properly removed. 720 */ 721 if (S_ISLNK(stbuf.st_mode)) { 722 printf("\t (which was a symlink to %s)\n", linkbuf); 723 } 724 } 725 } 726 727 /* 728 * Enable queuing to the printer (allow lpr's). 729 */ 730 void 731 enable(struct printer *pp) 732 { 733 struct stat stbuf; 734 char lf[MAXPATHLEN]; 735 736 lock_file_name(pp, lf, sizeof lf); 737 printf("%s:\n", pp->printer); 738 739 /* 740 * Turn off the group execute bit of the lock file to enable queuing. 741 */ 742 seteuid(euid); 743 if (stat(lf, &stbuf) >= 0) { 744 if (chmod(lf, stbuf.st_mode & ~LFM_QUEUE_DIS) < 0) 745 printf("\tcannot enable queuing\n"); 746 else 747 printf("\tqueuing enabled\n"); 748 } 749 seteuid(uid); 750 } 751 752 /* 753 * Disable queuing. 754 */ 755 void 756 disable(struct printer *pp) 757 { 758 register int fd; 759 struct stat stbuf; 760 char lf[MAXPATHLEN]; 761 762 lock_file_name(pp, lf, sizeof lf); 763 printf("%s:\n", pp->printer); 764 /* 765 * Turn on the group execute bit of the lock file to disable queuing. 766 */ 767 seteuid(euid); 768 if (stat(lf, &stbuf) >= 0) { 769 if (chmod(lf, stbuf.st_mode | LFM_QUEUE_DIS) < 0) 770 printf("\tcannot disable queuing: %s\n", 771 strerror(errno)); 772 else 773 printf("\tqueuing disabled\n"); 774 } else if (errno == ENOENT) { 775 if ((fd = open(lf, O_WRONLY|O_CREAT, 776 LOCK_FILE_MODE | LFM_QUEUE_DIS)) < 0) 777 printf("\tcannot create lock file: %s\n", 778 strerror(errno)); 779 else { 780 (void) close(fd); 781 printf("\tqueuing disabled\n"); 782 } 783 } else 784 printf("\tcannot stat lock file\n"); 785 seteuid(uid); 786 } 787 788 /* 789 * Disable queuing and printing and put a message into the status file 790 * (reason for being down). 791 */ 792 void 793 down(int argc, char *argv[]) 794 { 795 int cmdstatus, more; 796 struct printer myprinter, *pp = &myprinter; 797 798 if (argc == 1) { 799 printf("usage: down {all | printer} [message ...]\n"); 800 return; 801 } 802 if (!strcmp(argv[1], "all")) { 803 more = firstprinter(pp, &cmdstatus); 804 if (cmdstatus) 805 goto looperr; 806 while (more) { 807 putmsg(pp, argc - 2, argv + 2); 808 do { 809 more = nextprinter(pp, &cmdstatus); 810 looperr: 811 switch (cmdstatus) { 812 case PCAPERR_TCOPEN: 813 printf("warning: %s: unresolved " 814 "tc= reference(s) ", 815 pp->printer); 816 case PCAPERR_SUCCESS: 817 break; 818 default: 819 fatal(pp, "%s", pcaperr(cmdstatus)); 820 } 821 } while (more && cmdstatus); 822 } 823 return; 824 } 825 init_printer(pp); 826 cmdstatus = getprintcap(argv[1], pp); 827 switch (cmdstatus) { 828 default: 829 fatal(pp, "%s", pcaperr(cmdstatus)); 830 case PCAPERR_NOTFOUND: 831 printf("unknown printer %s\n", argv[1]); 832 return; 833 case PCAPERR_TCOPEN: 834 printf("warning: %s: unresolved tc= reference(s)", argv[1]); 835 break; 836 case PCAPERR_SUCCESS: 837 break; 838 } 839 putmsg(pp, argc - 2, argv + 2); 840 } 841 842 static void 843 putmsg(struct printer *pp, int argc, char **argv) 844 { 845 register int fd; 846 register char *cp1, *cp2; 847 char buf[1024]; 848 char file[MAXPATHLEN]; 849 struct stat stbuf; 850 851 printf("%s:\n", pp->printer); 852 /* 853 * Turn on the group execute bit of the lock file to disable queuing; 854 * turn on the owner execute bit of the lock file to disable printing. 855 */ 856 lock_file_name(pp, file, sizeof file); 857 seteuid(euid); 858 if (stat(file, &stbuf) >= 0) { 859 if (chmod(file, stbuf.st_mode|LFM_PRINT_DIS|LFM_QUEUE_DIS) < 0) 860 printf("\tcannot disable queuing: %s\n", 861 strerror(errno)); 862 else 863 printf("\tprinter and queuing disabled\n"); 864 } else if (errno == ENOENT) { 865 if ((fd = open(file, O_WRONLY|O_CREAT, 866 LOCK_FILE_MODE|LFM_PRINT_DIS|LFM_QUEUE_DIS)) < 0) 867 printf("\tcannot create lock file: %s\n", 868 strerror(errno)); 869 else { 870 (void) close(fd); 871 printf("\tprinter and queuing disabled\n"); 872 } 873 seteuid(uid); 874 return; 875 } else 876 printf("\tcannot stat lock file\n"); 877 /* 878 * Write the message into the status file. 879 */ 880 status_file_name(pp, file, sizeof file); 881 fd = open(file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 882 if (fd < 0) { 883 printf("\tcannot create status file: %s\n", strerror(errno)); 884 seteuid(uid); 885 return; 886 } 887 seteuid(uid); 888 (void) ftruncate(fd, 0); 889 if (argc <= 0) { 890 (void) write(fd, "\n", 1); 891 (void) close(fd); 892 return; 893 } 894 cp1 = buf; 895 while (--argc >= 0) { 896 cp2 = *argv++; 897 while ((size_t)(cp1 - buf) < sizeof(buf) && (*cp1++ = *cp2++)) 898 ; 899 cp1[-1] = ' '; 900 } 901 cp1[-1] = '\n'; 902 *cp1 = '\0'; 903 (void) write(fd, buf, strlen(buf)); 904 (void) close(fd); 905 } 906 907 /* 908 * Exit lpc 909 */ 910 void 911 quit(int argc __unused, char *argv[] __unused) 912 { 913 exit(0); 914 } 915 916 /* 917 * Kill and restart the daemon. 918 */ 919 void 920 restart(struct printer *pp) 921 { 922 abortpr(pp, 0); 923 startpr(pp, 0); 924 } 925 926 /* 927 * Enable printing on the specified printer and startup the daemon. 928 */ 929 void 930 startcmd(struct printer *pp) 931 { 932 startpr(pp, 1); 933 } 934 935 static void 936 startpr(struct printer *pp, int chgenable) 937 { 938 struct stat stbuf; 939 char lf[MAXPATHLEN]; 940 941 lock_file_name(pp, lf, sizeof lf); 942 printf("%s:\n", pp->printer); 943 944 /* 945 * For chgenable==1 ('start'), turn off the LFM_PRINT_DIS bit of the 946 * lock file to re-enable printing. For chgenable==2 ('up'), also 947 * turn off the LFM_QUEUE_DIS bit to re-enable queueing. 948 */ 949 seteuid(euid); 950 if (chgenable && stat(lf, &stbuf) >= 0) { 951 mode_t bits = (chgenable == 2 ? 0 : LFM_QUEUE_DIS); 952 if (chmod(lf, stbuf.st_mode & (LOCK_FILE_MODE | bits)) < 0) 953 printf("\tcannot enable printing\n"); 954 else 955 printf("\tprinting enabled\n"); 956 } 957 if (!startdaemon(pp)) 958 printf("\tcouldn't start daemon\n"); 959 else 960 printf("\tdaemon started\n"); 961 seteuid(uid); 962 } 963 964 /* 965 * Print the status of the printer queue. 966 */ 967 void 968 status(struct printer *pp) 969 { 970 struct stat stbuf; 971 register int fd, i; 972 register struct dirent *dp; 973 DIR *dirp; 974 char file[MAXPATHLEN]; 975 976 printf("%s:\n", pp->printer); 977 lock_file_name(pp, file, sizeof file); 978 if (stat(file, &stbuf) >= 0) { 979 printf("\tqueuing is %s\n", 980 ((stbuf.st_mode & LFM_QUEUE_DIS) ? "disabled" 981 : "enabled")); 982 printf("\tprinting is %s\n", 983 ((stbuf.st_mode & LFM_PRINT_DIS) ? "disabled" 984 : "enabled")); 985 } else { 986 printf("\tqueuing is enabled\n"); 987 printf("\tprinting is enabled\n"); 988 } 989 if ((dirp = opendir(pp->spool_dir)) == NULL) { 990 printf("\tcannot examine spool directory\n"); 991 return; 992 } 993 i = 0; 994 while ((dp = readdir(dirp)) != NULL) { 995 if (*dp->d_name == 'c' && dp->d_name[1] == 'f') 996 i++; 997 } 998 closedir(dirp); 999 if (i == 0) 1000 printf("\tno entries in spool area\n"); 1001 else if (i == 1) 1002 printf("\t1 entry in spool area\n"); 1003 else 1004 printf("\t%d entries in spool area\n", i); 1005 fd = open(file, O_RDONLY); 1006 if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) { 1007 (void) close(fd); /* unlocks as well */ 1008 printf("\tprinter idle\n"); 1009 return; 1010 } 1011 (void) close(fd); 1012 /* print out the contents of the status file, if it exists */ 1013 status_file_name(pp, file, sizeof file); 1014 fd = open(file, O_RDONLY|O_SHLOCK); 1015 if (fd >= 0) { 1016 (void) fstat(fd, &stbuf); 1017 if (stbuf.st_size > 0) { 1018 putchar('\t'); 1019 while ((i = read(fd, line, sizeof(line))) > 0) 1020 (void) fwrite(line, 1, i, stdout); 1021 } 1022 (void) close(fd); /* unlocks as well */ 1023 } 1024 } 1025 1026 /* 1027 * Stop the specified daemon after completing the current job and disable 1028 * printing. 1029 */ 1030 void 1031 stop(struct printer *pp) 1032 { 1033 register int fd; 1034 struct stat stbuf; 1035 char lf[MAXPATHLEN]; 1036 1037 lock_file_name(pp, lf, sizeof lf); 1038 printf("%s:\n", pp->printer); 1039 1040 /* 1041 * Turn on the owner execute bit of the lock file to disable printing. 1042 */ 1043 seteuid(euid); 1044 if (stat(lf, &stbuf) >= 0) { 1045 if (chmod(lf, stbuf.st_mode | LFM_PRINT_DIS) < 0) 1046 printf("\tcannot disable printing: %s\n", 1047 strerror(errno)); 1048 else { 1049 upstat(pp, "printing disabled\n"); 1050 printf("\tprinting disabled\n"); 1051 } 1052 } else if (errno == ENOENT) { 1053 if ((fd = open(lf, O_WRONLY|O_CREAT, 1054 LOCK_FILE_MODE | LFM_PRINT_DIS)) < 0) 1055 printf("\tcannot create lock file: %s\n", 1056 strerror(errno)); 1057 else { 1058 (void) close(fd); 1059 upstat(pp, "printing disabled\n"); 1060 printf("\tprinting disabled\n"); 1061 } 1062 } else 1063 printf("\tcannot stat lock file\n"); 1064 seteuid(uid); 1065 } 1066 1067 struct jobqueue **queue; 1068 int nitems; 1069 time_t mtime; 1070 1071 /* 1072 * Put the specified jobs at the top of printer queue. 1073 */ 1074 void 1075 topq(int argc, char *argv[]) 1076 { 1077 register int i; 1078 struct stat stbuf; 1079 int cmdstatus, changed; 1080 struct printer myprinter, *pp = &myprinter; 1081 1082 if (argc < 3) { 1083 printf("usage: topq printer [jobnum ...] [user ...]\n"); 1084 return; 1085 } 1086 1087 --argc; 1088 ++argv; 1089 init_printer(pp); 1090 cmdstatus = getprintcap(*argv, pp); 1091 switch(cmdstatus) { 1092 default: 1093 fatal(pp, "%s", pcaperr(cmdstatus)); 1094 case PCAPERR_NOTFOUND: 1095 printf("unknown printer %s\n", *argv); 1096 return; 1097 case PCAPERR_TCOPEN: 1098 printf("warning: %s: unresolved tc= reference(s)", *argv); 1099 break; 1100 case PCAPERR_SUCCESS: 1101 break; 1102 } 1103 printf("%s:\n", pp->printer); 1104 1105 seteuid(euid); 1106 if (chdir(pp->spool_dir) < 0) { 1107 printf("\tcannot chdir to %s\n", pp->spool_dir); 1108 goto out; 1109 } 1110 seteuid(uid); 1111 nitems = getq(pp, &queue); 1112 if (nitems == 0) 1113 return; 1114 changed = 0; 1115 mtime = queue[0]->job_time; 1116 for (i = argc; --i; ) { 1117 if (doarg(argv[i]) == 0) { 1118 printf("\tjob %s is not in the queue\n", argv[i]); 1119 continue; 1120 } else 1121 changed++; 1122 } 1123 for (i = 0; i < nitems; i++) 1124 free(queue[i]); 1125 free(queue); 1126 if (!changed) { 1127 printf("\tqueue order unchanged\n"); 1128 return; 1129 } 1130 /* 1131 * Turn on the public execute bit of the lock file to 1132 * get lpd to rebuild the queue after the current job. 1133 */ 1134 seteuid(euid); 1135 if (changed && stat(pp->lock_file, &stbuf) >= 0) 1136 (void) chmod(pp->lock_file, stbuf.st_mode | LFM_RESET_QUE); 1137 1138 out: 1139 seteuid(uid); 1140 } 1141 1142 /* 1143 * Reposition the job by changing the modification time of 1144 * the control file. 1145 */ 1146 static int 1147 touch(struct jobqueue *jq) 1148 { 1149 struct timeval tvp[2]; 1150 int ret; 1151 1152 tvp[0].tv_sec = tvp[1].tv_sec = --mtime; 1153 tvp[0].tv_usec = tvp[1].tv_usec = 0; 1154 seteuid(euid); 1155 ret = utimes(jq->job_cfname, tvp); 1156 seteuid(uid); 1157 return (ret); 1158 } 1159 1160 /* 1161 * Checks if specified job name is in the printer's queue. 1162 * Returns: negative (-1) if argument name is not in the queue. 1163 */ 1164 static int 1165 doarg(char *job) 1166 { 1167 register struct jobqueue **qq; 1168 register int jobnum, n; 1169 register char *cp, *machine; 1170 int cnt = 0; 1171 FILE *fp; 1172 1173 /* 1174 * Look for a job item consisting of system name, colon, number 1175 * (example: ucbarpa:114) 1176 */ 1177 if ((cp = strchr(job, ':')) != NULL) { 1178 machine = job; 1179 *cp++ = '\0'; 1180 job = cp; 1181 } else 1182 machine = NULL; 1183 1184 /* 1185 * Check for job specified by number (example: 112 or 235ucbarpa). 1186 */ 1187 if (isdigit(*job)) { 1188 jobnum = 0; 1189 do 1190 jobnum = jobnum * 10 + (*job++ - '0'); 1191 while (isdigit(*job)); 1192 for (qq = queue + nitems; --qq >= queue; ) { 1193 n = 0; 1194 for (cp = (*qq)->job_cfname+3; isdigit(*cp); ) 1195 n = n * 10 + (*cp++ - '0'); 1196 if (jobnum != n) 1197 continue; 1198 if (*job && strcmp(job, cp) != 0) 1199 continue; 1200 if (machine != NULL && strcmp(machine, cp) != 0) 1201 continue; 1202 if (touch(*qq) == 0) { 1203 printf("\tmoved %s\n", (*qq)->job_cfname); 1204 cnt++; 1205 } 1206 } 1207 return(cnt); 1208 } 1209 /* 1210 * Process item consisting of owner's name (example: henry). 1211 */ 1212 for (qq = queue + nitems; --qq >= queue; ) { 1213 seteuid(euid); 1214 fp = fopen((*qq)->job_cfname, "r"); 1215 seteuid(uid); 1216 if (fp == NULL) 1217 continue; 1218 while (getline(fp) > 0) 1219 if (line[0] == 'P') 1220 break; 1221 (void) fclose(fp); 1222 if (line[0] != 'P' || strcmp(job, line+1) != 0) 1223 continue; 1224 if (touch(*qq) == 0) { 1225 printf("\tmoved %s\n", (*qq)->job_cfname); 1226 cnt++; 1227 } 1228 } 1229 return(cnt); 1230 } 1231 1232 /* 1233 * Enable everything and start printer (undo `down'). 1234 */ 1235 void 1236 up(struct printer *pp) 1237 { 1238 startpr(pp, 2); 1239 } 1240