14a1a0dbeSGarrett Wollman /* 24a1a0dbeSGarrett Wollman * Copyright 1997 Massachusetts Institute of Technology 34a1a0dbeSGarrett Wollman * 44a1a0dbeSGarrett Wollman * Permission to use, copy, modify, and distribute this software and 54a1a0dbeSGarrett Wollman * its documentation for any purpose and without fee is hereby 64a1a0dbeSGarrett Wollman * granted, provided that both the above copyright notice and this 74a1a0dbeSGarrett Wollman * permission notice appear in all copies, that both the above 84a1a0dbeSGarrett Wollman * copyright notice and this permission notice appear in all 94a1a0dbeSGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 104a1a0dbeSGarrett Wollman * in advertising or publicity pertaining to distribution of the 114a1a0dbeSGarrett Wollman * software without specific, written prior permission. M.I.T. makes 124a1a0dbeSGarrett Wollman * no representations about the suitability of this software for any 134a1a0dbeSGarrett Wollman * purpose. It is provided "as is" without express or implied 144a1a0dbeSGarrett Wollman * warranty. 154a1a0dbeSGarrett Wollman * 164a1a0dbeSGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 174a1a0dbeSGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 184a1a0dbeSGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 194a1a0dbeSGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 204a1a0dbeSGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 214a1a0dbeSGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224a1a0dbeSGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 234a1a0dbeSGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 244a1a0dbeSGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 254a1a0dbeSGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 264a1a0dbeSGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274a1a0dbeSGarrett Wollman * SUCH DAMAGE. 284a1a0dbeSGarrett Wollman */ 294a1a0dbeSGarrett Wollman 304a1a0dbeSGarrett Wollman static const char copyright[] = 314a1a0dbeSGarrett Wollman "Copyright (C) 1997, Massachusetts Institute of Technology\r\n"; 324a1a0dbeSGarrett Wollman static const char rcsid[] = 3397d92980SPeter Wemm "$FreeBSD$"; 344a1a0dbeSGarrett Wollman 354a1a0dbeSGarrett Wollman #include <sys/types.h> 364a1a0dbeSGarrett Wollman #include <sys/queue.h> 374a1a0dbeSGarrett Wollman #include <sys/stat.h> 384a1a0dbeSGarrett Wollman 394a1a0dbeSGarrett Wollman #include <err.h> 404a1a0dbeSGarrett Wollman #include <errno.h> 414a1a0dbeSGarrett Wollman #include <grp.h> 424a1a0dbeSGarrett Wollman #include <stdio.h> 434a1a0dbeSGarrett Wollman #include <string.h> 444a1a0dbeSGarrett Wollman #include <stdlib.h> 454a1a0dbeSGarrett Wollman #include <unistd.h> 464a1a0dbeSGarrett Wollman 474a1a0dbeSGarrett Wollman #include <sys/param.h> /* needed for lp.h but not used here */ 484a1a0dbeSGarrett Wollman #include <dirent.h> /* ditto */ 494a1a0dbeSGarrett Wollman #include "lp.h" 504a1a0dbeSGarrett Wollman #include "lp.local.h" 5143323a1dSGarance A Drosehn #include "pathnames.h" 5243323a1dSGarance A Drosehn #include "skimprintcap.h" 534a1a0dbeSGarrett Wollman 544a1a0dbeSGarrett Wollman static void check_spool_dirs(void); 554a1a0dbeSGarrett Wollman static int interpret_error(const struct printer *pp, int error); 564a1a0dbeSGarrett Wollman static void make_spool_dir(const struct printer *pp); 574a1a0dbeSGarrett Wollman static void note_spool_dir(const struct printer *pp, const struct stat *st); 584a1a0dbeSGarrett Wollman static void usage(void) __dead2; 594a1a0dbeSGarrett Wollman 604a1a0dbeSGarrett Wollman static int problems; /* number of problems encountered */ 614a1a0dbeSGarrett Wollman 624a1a0dbeSGarrett Wollman /* 634a1a0dbeSGarrett Wollman * chkprintcap - check the printcap file for syntactic and semantic errors 644a1a0dbeSGarrett Wollman * Returns the number of problems found. 654a1a0dbeSGarrett Wollman */ 664a1a0dbeSGarrett Wollman int 674a1a0dbeSGarrett Wollman main(int argc, char **argv) 684a1a0dbeSGarrett Wollman { 6943323a1dSGarance A Drosehn struct skiminfo *skres; 7043323a1dSGarance A Drosehn char *pcap_fname; 7143323a1dSGarance A Drosehn int c, error, makedirs, more, queuecnt, verbosity; 724a1a0dbeSGarrett Wollman struct printer myprinter, *pp; 734a1a0dbeSGarrett Wollman 744a1a0dbeSGarrett Wollman makedirs = 0; 7543323a1dSGarance A Drosehn queuecnt = 0; 7643323a1dSGarance A Drosehn verbosity = 0; 7743323a1dSGarance A Drosehn pcap_fname = NULL; 784a1a0dbeSGarrett Wollman pp = &myprinter; 794a1a0dbeSGarrett Wollman 8043323a1dSGarance A Drosehn while ((c = getopt(argc, argv, "df:v")) != -1) { 814a1a0dbeSGarrett Wollman switch (c) { 824a1a0dbeSGarrett Wollman case 'd': 834a1a0dbeSGarrett Wollman makedirs = 1; 844a1a0dbeSGarrett Wollman break; 854a1a0dbeSGarrett Wollman 864a1a0dbeSGarrett Wollman case 'f': 8743323a1dSGarance A Drosehn pcap_fname = strdup(optarg); 8843323a1dSGarance A Drosehn setprintcap(pcap_fname); 8943323a1dSGarance A Drosehn break; 9043323a1dSGarance A Drosehn 9143323a1dSGarance A Drosehn case 'v': 9243323a1dSGarance A Drosehn verbosity++; 934a1a0dbeSGarrett Wollman break; 944a1a0dbeSGarrett Wollman 954a1a0dbeSGarrett Wollman default: 964a1a0dbeSGarrett Wollman usage(); 974a1a0dbeSGarrett Wollman } 984a1a0dbeSGarrett Wollman } 994a1a0dbeSGarrett Wollman 1004a1a0dbeSGarrett Wollman if (optind != argc) 1014a1a0dbeSGarrett Wollman usage(); 1024a1a0dbeSGarrett Wollman 10343323a1dSGarance A Drosehn if (pcap_fname == NULL) 10443323a1dSGarance A Drosehn pcap_fname = strdup(_PATH_PRINTCAP); 10543323a1dSGarance A Drosehn 10643323a1dSGarance A Drosehn /* 10743323a1dSGarance A Drosehn * Skim through the printcap file looking for simple user-mistakes 10843323a1dSGarance A Drosehn * which will produce the wrong result for the user, but which may 10943323a1dSGarance A Drosehn * be pretty hard for the user to notice. Such user-mistakes will 11043323a1dSGarance A Drosehn * only generate warning messages. The (fatal-) problem count will 11143323a1dSGarance A Drosehn * only be incremented if there is a system problem trying to read 11243323a1dSGarance A Drosehn * the printcap file. 11343323a1dSGarance A Drosehn */ 11443323a1dSGarance A Drosehn skres = skim_printcap(pcap_fname, verbosity); 11543323a1dSGarance A Drosehn if (skres->fatalerr) 11643323a1dSGarance A Drosehn return (skres->fatalerr); 11743323a1dSGarance A Drosehn 11843323a1dSGarance A Drosehn /* 11943323a1dSGarance A Drosehn * Now use the standard capability-db routines to check the values 12043323a1dSGarance A Drosehn * in each of the queues defined in the printcap file. 12143323a1dSGarance A Drosehn */ 1224a1a0dbeSGarrett Wollman more = firstprinter(pp, &error); 1234a1a0dbeSGarrett Wollman if (interpret_error(pp, error) && more) 1244a1a0dbeSGarrett Wollman goto next; 1254a1a0dbeSGarrett Wollman 1264a1a0dbeSGarrett Wollman while (more) { 1274a1a0dbeSGarrett Wollman struct stat stab; 1284a1a0dbeSGarrett Wollman 12943323a1dSGarance A Drosehn queuecnt++; 1304a1a0dbeSGarrett Wollman errno = 0; 1314a1a0dbeSGarrett Wollman if (stat(pp->spool_dir, &stab) < 0) { 1324a1a0dbeSGarrett Wollman if (errno == ENOENT && makedirs) { 1334a1a0dbeSGarrett Wollman make_spool_dir(pp); 1344a1a0dbeSGarrett Wollman } else { 1354a1a0dbeSGarrett Wollman problems++; 1364a1a0dbeSGarrett Wollman warn("%s: %s", pp->printer, pp->spool_dir); 1374a1a0dbeSGarrett Wollman } 1384a1a0dbeSGarrett Wollman } else { 1394a1a0dbeSGarrett Wollman note_spool_dir(pp, &stab); 1404a1a0dbeSGarrett Wollman } 1414a1a0dbeSGarrett Wollman 14243323a1dSGarance A Drosehn /* Make other queue-specific validity checks here... */ 1434a1a0dbeSGarrett Wollman 1444a1a0dbeSGarrett Wollman next: 1454a1a0dbeSGarrett Wollman more = nextprinter(pp, &error); 1464a1a0dbeSGarrett Wollman if (interpret_error(pp, error) && more) 1474a1a0dbeSGarrett Wollman goto next; 1484a1a0dbeSGarrett Wollman } 14943323a1dSGarance A Drosehn 1504a1a0dbeSGarrett Wollman check_spool_dirs(); 15143323a1dSGarance A Drosehn 15243323a1dSGarance A Drosehn if (queuecnt != skres->entries) { 15343323a1dSGarance A Drosehn warnx("WARNING: found %d entries when skimming %s,", 15443323a1dSGarance A Drosehn skres->entries, pcap_fname); 15543323a1dSGarance A Drosehn warnx("WARNING: but only found %d queues to process!", 15643323a1dSGarance A Drosehn queuecnt); 15743323a1dSGarance A Drosehn } 15843323a1dSGarance A Drosehn return (problems); 1594a1a0dbeSGarrett Wollman } 1604a1a0dbeSGarrett Wollman 1614a1a0dbeSGarrett Wollman /* 1624a1a0dbeSGarrett Wollman * Interpret the error code. Returns 1 if we should skip to the next 1634a1a0dbeSGarrett Wollman * record (as this record is unlikely to make sense). If the problem 1644a1a0dbeSGarrett Wollman * is very severe, exit. Otherwise, return zero. 1654a1a0dbeSGarrett Wollman */ 1664a1a0dbeSGarrett Wollman static int 1674a1a0dbeSGarrett Wollman interpret_error(const struct printer *pp, int error) 1684a1a0dbeSGarrett Wollman { 1694a1a0dbeSGarrett Wollman switch(error) { 1704a1a0dbeSGarrett Wollman case PCAPERR_OSERR: 1714a1a0dbeSGarrett Wollman err(++problems, "reading printer database"); 1724a1a0dbeSGarrett Wollman case PCAPERR_TCLOOP: 1734a1a0dbeSGarrett Wollman ++problems; 1744a1a0dbeSGarrett Wollman warnx("%s: loop detected in tc= expansion", pp->printer); 1754a1a0dbeSGarrett Wollman return 1; 1764a1a0dbeSGarrett Wollman case PCAPERR_TCOPEN: 1774a1a0dbeSGarrett Wollman warnx("%s: unresolved tc= expansion", pp->printer); 1784a1a0dbeSGarrett Wollman return 1; 1794a1a0dbeSGarrett Wollman case PCAPERR_SUCCESS: 1804a1a0dbeSGarrett Wollman break; 1814a1a0dbeSGarrett Wollman default: 1824a1a0dbeSGarrett Wollman errx(++problems, "unknown printcap library error %d", error); 1834a1a0dbeSGarrett Wollman } 1844a1a0dbeSGarrett Wollman return 0; 1854a1a0dbeSGarrett Wollman } 1864a1a0dbeSGarrett Wollman 1874a1a0dbeSGarrett Wollman /* 1884a1a0dbeSGarrett Wollman * Keep the list of spool directories. Note that we don't whine 1894a1a0dbeSGarrett Wollman * until all spool directories are noted, so that all of the more serious 1904a1a0dbeSGarrett Wollman * problems are noted first. We keep the list sorted by st_dev and 1914a1a0dbeSGarrett Wollman * st_ino, so that the problem spool directories can be noted in 1924a1a0dbeSGarrett Wollman * a single loop. 1934a1a0dbeSGarrett Wollman */ 1944a1a0dbeSGarrett Wollman struct dirlist { 195e3975643SJake Burkholder LIST_ENTRY(dirlist) link; 1964a1a0dbeSGarrett Wollman struct stat stab; 1974a1a0dbeSGarrett Wollman char *path; 1984a1a0dbeSGarrett Wollman char *printer; 1994a1a0dbeSGarrett Wollman }; 2004a1a0dbeSGarrett Wollman 201e3975643SJake Burkholder static LIST_HEAD(, dirlist) dirlist; 2024a1a0dbeSGarrett Wollman 2034a1a0dbeSGarrett Wollman static int 2044a1a0dbeSGarrett Wollman lessp(const struct dirlist *a, const struct dirlist *b) 2054a1a0dbeSGarrett Wollman { 2064a1a0dbeSGarrett Wollman if (a->stab.st_dev == b->stab.st_dev) 2074a1a0dbeSGarrett Wollman return a->stab.st_ino < b->stab.st_ino; 2084a1a0dbeSGarrett Wollman return a->stab.st_dev < b->stab.st_dev; 2094a1a0dbeSGarrett Wollman } 2104a1a0dbeSGarrett Wollman 2114a1a0dbeSGarrett Wollman static int 2124a1a0dbeSGarrett Wollman equal(const struct dirlist *a, const struct dirlist *b) 2134a1a0dbeSGarrett Wollman { 2144a1a0dbeSGarrett Wollman return ((a->stab.st_dev == b->stab.st_dev) 2154a1a0dbeSGarrett Wollman && (a->stab.st_ino == b->stab.st_ino)); 2164a1a0dbeSGarrett Wollman } 2174a1a0dbeSGarrett Wollman 2184a1a0dbeSGarrett Wollman static void 2194a1a0dbeSGarrett Wollman note_spool_dir(const struct printer *pp, const struct stat *st) 2204a1a0dbeSGarrett Wollman { 2214a1a0dbeSGarrett Wollman struct dirlist *dp, *dp2, *last; 2224a1a0dbeSGarrett Wollman 2234a1a0dbeSGarrett Wollman dp = malloc(sizeof *dp); 2244a1a0dbeSGarrett Wollman if (dp == 0) 2254a1a0dbeSGarrett Wollman err(++problems, "malloc(%lu)", (u_long)sizeof *dp); 2264a1a0dbeSGarrett Wollman 2274a1a0dbeSGarrett Wollman dp->stab = *st; 2284a1a0dbeSGarrett Wollman dp->printer = strdup(pp->printer); 2294a1a0dbeSGarrett Wollman if (dp->printer == 0) 2304a1a0dbeSGarrett Wollman err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL); 2314a1a0dbeSGarrett Wollman dp->path = strdup(pp->spool_dir); 2324a1a0dbeSGarrett Wollman if (dp->path == 0) 2334a1a0dbeSGarrett Wollman err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL); 2344a1a0dbeSGarrett Wollman 2354a1a0dbeSGarrett Wollman last = 0; 2362673ed47SPoul-Henning Kamp LIST_FOREACH(dp2, &dirlist, link) { 2372673ed47SPoul-Henning Kamp if(!lessp(dp, dp2)) 2382673ed47SPoul-Henning Kamp break; 2394a1a0dbeSGarrett Wollman last = dp2; 2404a1a0dbeSGarrett Wollman } 2414a1a0dbeSGarrett Wollman 2424a1a0dbeSGarrett Wollman if (last) { 2434a1a0dbeSGarrett Wollman LIST_INSERT_AFTER(last, dp, link); 2444a1a0dbeSGarrett Wollman } else { 2454a1a0dbeSGarrett Wollman LIST_INSERT_HEAD(&dirlist, dp, link); 2464a1a0dbeSGarrett Wollman } 2474a1a0dbeSGarrett Wollman } 2484a1a0dbeSGarrett Wollman 2494a1a0dbeSGarrett Wollman static void 2504a1a0dbeSGarrett Wollman check_spool_dirs(void) 2514a1a0dbeSGarrett Wollman { 2524a1a0dbeSGarrett Wollman struct dirlist *dp, *dp2; 2534a1a0dbeSGarrett Wollman 2542673ed47SPoul-Henning Kamp for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) { 2552673ed47SPoul-Henning Kamp dp2 = LIST_NEXT(dp, link); 2564a1a0dbeSGarrett Wollman 2574a1a0dbeSGarrett Wollman if (dp2 != 0 && equal(dp, dp2)) { 2584a1a0dbeSGarrett Wollman ++problems; 2594a1a0dbeSGarrett Wollman if (strcmp(dp->path, dp2->path) == 0) { 2604a1a0dbeSGarrett Wollman warnx("%s and %s share the same spool, %s", 2614a1a0dbeSGarrett Wollman dp->printer, dp2->printer, dp->path); 2624a1a0dbeSGarrett Wollman } else { 2634a1a0dbeSGarrett Wollman warnx("%s (%s) and %s (%s) are the same " 2644a1a0dbeSGarrett Wollman "directory", dp->path, dp->printer, 2654a1a0dbeSGarrett Wollman dp2->path, dp2->printer); 2664a1a0dbeSGarrett Wollman } 2674a1a0dbeSGarrett Wollman continue; 2684a1a0dbeSGarrett Wollman } 2694a1a0dbeSGarrett Wollman /* Should probably check owners and modes here. */ 2704a1a0dbeSGarrett Wollman } 2714a1a0dbeSGarrett Wollman } 2724a1a0dbeSGarrett Wollman 2734a1a0dbeSGarrett Wollman #ifndef SPOOL_DIR_MODE 2744a1a0dbeSGarrett Wollman #define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \ 2754a1a0dbeSGarrett Wollman | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 2764a1a0dbeSGarrett Wollman #endif 2774a1a0dbeSGarrett Wollman 2784a1a0dbeSGarrett Wollman static void 2794a1a0dbeSGarrett Wollman make_spool_dir(const struct printer *pp) 2804a1a0dbeSGarrett Wollman { 2814a1a0dbeSGarrett Wollman char *sd = pp->spool_dir; 2824a1a0dbeSGarrett Wollman struct group *gr; 2834a1a0dbeSGarrett Wollman struct stat stab; 2844a1a0dbeSGarrett Wollman 2854a1a0dbeSGarrett Wollman if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) { 2864a1a0dbeSGarrett Wollman problems++; 2874a1a0dbeSGarrett Wollman warn("%s: mkdir %s", pp->printer, sd); 2884a1a0dbeSGarrett Wollman return; 2894a1a0dbeSGarrett Wollman } 2904a1a0dbeSGarrett Wollman gr = getgrnam("daemon"); 2914a1a0dbeSGarrett Wollman if (gr == 0) 2924a1a0dbeSGarrett Wollman errx(++problems, "cannot locate daemon group"); 2934a1a0dbeSGarrett Wollman 2944a1a0dbeSGarrett Wollman if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) { 2954a1a0dbeSGarrett Wollman ++problems; 2964a1a0dbeSGarrett Wollman warn("%s: cannot change ownership to %ld:%ld", sd, 2974a1a0dbeSGarrett Wollman (long)pp->daemon_user, (long)gr->gr_gid); 2984a1a0dbeSGarrett Wollman return; 2994a1a0dbeSGarrett Wollman } 3004a1a0dbeSGarrett Wollman 3014a1a0dbeSGarrett Wollman if (chmod(sd, SPOOL_DIR_MODE) < 0) { 3024a1a0dbeSGarrett Wollman ++problems; 3039aa446f5SJohn Polstra warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE); 3044a1a0dbeSGarrett Wollman return; 3054a1a0dbeSGarrett Wollman } 3064a1a0dbeSGarrett Wollman if (stat(sd, &stab) < 0) 3074a1a0dbeSGarrett Wollman err(++problems, "stat: %s", sd); 3084a1a0dbeSGarrett Wollman 3094a1a0dbeSGarrett Wollman note_spool_dir(pp, &stab); 3104a1a0dbeSGarrett Wollman } 3114a1a0dbeSGarrett Wollman 3124a1a0dbeSGarrett Wollman static void 3134a1a0dbeSGarrett Wollman usage(void) 3144a1a0dbeSGarrett Wollman { 31543323a1dSGarance A Drosehn fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n"); 3164a1a0dbeSGarrett Wollman exit(1); 3174a1a0dbeSGarrett Wollman } 318