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