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";
328e36ed92SGarance A Drosehn
338e36ed92SGarance A Drosehn #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
344a1a0dbeSGarrett Wollman #include <sys/types.h>
354a1a0dbeSGarrett Wollman #include <sys/queue.h>
364a1a0dbeSGarrett Wollman #include <sys/stat.h>
374a1a0dbeSGarrett Wollman
384a1a0dbeSGarrett Wollman #include <err.h>
394a1a0dbeSGarrett Wollman #include <errno.h>
404a1a0dbeSGarrett Wollman #include <grp.h>
414a1a0dbeSGarrett Wollman #include <stdio.h>
424a1a0dbeSGarrett Wollman #include <string.h>
434a1a0dbeSGarrett Wollman #include <stdlib.h>
444a1a0dbeSGarrett Wollman #include <unistd.h>
454a1a0dbeSGarrett Wollman
464a1a0dbeSGarrett Wollman #include <sys/param.h> /* needed for lp.h but not used here */
474a1a0dbeSGarrett Wollman #include <dirent.h> /* ditto */
484a1a0dbeSGarrett Wollman #include "lp.h"
494a1a0dbeSGarrett Wollman #include "lp.local.h"
5043323a1dSGarance A Drosehn #include "pathnames.h"
5143323a1dSGarance A Drosehn #include "skimprintcap.h"
524a1a0dbeSGarrett Wollman
534a1a0dbeSGarrett Wollman static void check_spool_dirs(void);
544a1a0dbeSGarrett Wollman static int interpret_error(const struct printer *pp, int error);
554a1a0dbeSGarrett Wollman static void make_spool_dir(const struct printer *pp);
564a1a0dbeSGarrett Wollman static void note_spool_dir(const struct printer *pp, const struct stat *st);
574a1a0dbeSGarrett Wollman static void usage(void) __dead2;
584a1a0dbeSGarrett Wollman
594a1a0dbeSGarrett Wollman static int problems; /* number of problems encountered */
604a1a0dbeSGarrett Wollman
614a1a0dbeSGarrett Wollman /*
624a1a0dbeSGarrett Wollman * chkprintcap - check the printcap file for syntactic and semantic errors
634a1a0dbeSGarrett Wollman * Returns the number of problems found.
644a1a0dbeSGarrett Wollman */
654a1a0dbeSGarrett Wollman int
main(int argc,char ** argv)664a1a0dbeSGarrett Wollman main(int argc, char **argv)
674a1a0dbeSGarrett Wollman {
6843323a1dSGarance A Drosehn struct skiminfo *skres;
6943323a1dSGarance A Drosehn char *pcap_fname;
7043323a1dSGarance A Drosehn int c, error, makedirs, more, queuecnt, verbosity;
714a1a0dbeSGarrett Wollman struct printer myprinter, *pp;
724a1a0dbeSGarrett Wollman
734a1a0dbeSGarrett Wollman makedirs = 0;
7443323a1dSGarance A Drosehn queuecnt = 0;
7543323a1dSGarance A Drosehn verbosity = 0;
7643323a1dSGarance A Drosehn pcap_fname = NULL;
774a1a0dbeSGarrett Wollman pp = &myprinter;
784a1a0dbeSGarrett Wollman
7943323a1dSGarance A Drosehn while ((c = getopt(argc, argv, "df:v")) != -1) {
804a1a0dbeSGarrett Wollman switch (c) {
814a1a0dbeSGarrett Wollman case 'd':
824a1a0dbeSGarrett Wollman makedirs = 1;
834a1a0dbeSGarrett Wollman break;
844a1a0dbeSGarrett Wollman
854a1a0dbeSGarrett Wollman case 'f':
8643323a1dSGarance A Drosehn pcap_fname = strdup(optarg);
8743323a1dSGarance A Drosehn setprintcap(pcap_fname);
8843323a1dSGarance A Drosehn break;
8943323a1dSGarance A Drosehn
9043323a1dSGarance A Drosehn case 'v':
9143323a1dSGarance A Drosehn verbosity++;
924a1a0dbeSGarrett Wollman break;
934a1a0dbeSGarrett Wollman
944a1a0dbeSGarrett Wollman default:
954a1a0dbeSGarrett Wollman usage();
964a1a0dbeSGarrett Wollman }
974a1a0dbeSGarrett Wollman }
984a1a0dbeSGarrett Wollman
994a1a0dbeSGarrett Wollman if (optind != argc)
1004a1a0dbeSGarrett Wollman usage();
1014a1a0dbeSGarrett Wollman
10243323a1dSGarance A Drosehn if (pcap_fname == NULL)
10343323a1dSGarance A Drosehn pcap_fname = strdup(_PATH_PRINTCAP);
10443323a1dSGarance A Drosehn
10543323a1dSGarance A Drosehn /*
10643323a1dSGarance A Drosehn * Skim through the printcap file looking for simple user-mistakes
10743323a1dSGarance A Drosehn * which will produce the wrong result for the user, but which may
10843323a1dSGarance A Drosehn * be pretty hard for the user to notice. Such user-mistakes will
10943323a1dSGarance A Drosehn * only generate warning messages. The (fatal-) problem count will
11043323a1dSGarance A Drosehn * only be incremented if there is a system problem trying to read
11143323a1dSGarance A Drosehn * the printcap file.
11243323a1dSGarance A Drosehn */
11343323a1dSGarance A Drosehn skres = skim_printcap(pcap_fname, verbosity);
114*e550d65cSGarance A Drosehn if (skres == NULL) {
115*e550d65cSGarance A Drosehn problems = 1;
116*e550d65cSGarance A Drosehn goto main_ret;
117*e550d65cSGarance A Drosehn } else if (skres->fatalerr) {
118*e550d65cSGarance A Drosehn problems = skres->fatalerr;
119*e550d65cSGarance A Drosehn goto main_ret;
120*e550d65cSGarance A Drosehn }
12143323a1dSGarance A Drosehn
12243323a1dSGarance A Drosehn /*
12343323a1dSGarance A Drosehn * Now use the standard capability-db routines to check the values
12443323a1dSGarance A Drosehn * in each of the queues defined in the printcap file.
12543323a1dSGarance A Drosehn */
1264a1a0dbeSGarrett Wollman more = firstprinter(pp, &error);
1274a1a0dbeSGarrett Wollman if (interpret_error(pp, error) && more)
1284a1a0dbeSGarrett Wollman goto next;
1294a1a0dbeSGarrett Wollman
1304a1a0dbeSGarrett Wollman while (more) {
1314a1a0dbeSGarrett Wollman struct stat stab;
1324a1a0dbeSGarrett Wollman
13343323a1dSGarance A Drosehn queuecnt++;
1344a1a0dbeSGarrett Wollman errno = 0;
1354a1a0dbeSGarrett Wollman if (stat(pp->spool_dir, &stab) < 0) {
1364a1a0dbeSGarrett Wollman if (errno == ENOENT && makedirs) {
1374a1a0dbeSGarrett Wollman make_spool_dir(pp);
1384a1a0dbeSGarrett Wollman } else {
1394a1a0dbeSGarrett Wollman problems++;
1404a1a0dbeSGarrett Wollman warn("%s: %s", pp->printer, pp->spool_dir);
1414a1a0dbeSGarrett Wollman }
1424a1a0dbeSGarrett Wollman } else {
1434a1a0dbeSGarrett Wollman note_spool_dir(pp, &stab);
1444a1a0dbeSGarrett Wollman }
1454a1a0dbeSGarrett Wollman
14643323a1dSGarance A Drosehn /* Make other queue-specific validity checks here... */
1474a1a0dbeSGarrett Wollman
1484a1a0dbeSGarrett Wollman next:
1494a1a0dbeSGarrett Wollman more = nextprinter(pp, &error);
1504a1a0dbeSGarrett Wollman if (interpret_error(pp, error) && more)
1514a1a0dbeSGarrett Wollman goto next;
1524a1a0dbeSGarrett Wollman }
15343323a1dSGarance A Drosehn
1544a1a0dbeSGarrett Wollman check_spool_dirs();
15543323a1dSGarance A Drosehn
15643323a1dSGarance A Drosehn if (queuecnt != skres->entries) {
15743323a1dSGarance A Drosehn warnx("WARNING: found %d entries when skimming %s,",
15843323a1dSGarance A Drosehn skres->entries, pcap_fname);
15943323a1dSGarance A Drosehn warnx("WARNING: but only found %d queues to process!",
16043323a1dSGarance A Drosehn queuecnt);
16143323a1dSGarance A Drosehn }
162*e550d65cSGarance A Drosehn
163*e550d65cSGarance A Drosehn main_ret:
164*e550d65cSGarance A Drosehn free(pcap_fname);
16543323a1dSGarance A Drosehn return (problems);
1664a1a0dbeSGarrett Wollman }
1674a1a0dbeSGarrett Wollman
1684a1a0dbeSGarrett Wollman /*
1694a1a0dbeSGarrett Wollman * Interpret the error code. Returns 1 if we should skip to the next
1704a1a0dbeSGarrett Wollman * record (as this record is unlikely to make sense). If the problem
1714a1a0dbeSGarrett Wollman * is very severe, exit. Otherwise, return zero.
1724a1a0dbeSGarrett Wollman */
1734a1a0dbeSGarrett Wollman static int
interpret_error(const struct printer * pp,int error)1744a1a0dbeSGarrett Wollman interpret_error(const struct printer *pp, int error)
1754a1a0dbeSGarrett Wollman {
1764a1a0dbeSGarrett Wollman switch(error) {
1774a1a0dbeSGarrett Wollman case PCAPERR_OSERR:
1784a1a0dbeSGarrett Wollman err(++problems, "reading printer database");
1794a1a0dbeSGarrett Wollman case PCAPERR_TCLOOP:
1804a1a0dbeSGarrett Wollman ++problems;
1814a1a0dbeSGarrett Wollman warnx("%s: loop detected in tc= expansion", pp->printer);
1824a1a0dbeSGarrett Wollman return 1;
1834a1a0dbeSGarrett Wollman case PCAPERR_TCOPEN:
1844a1a0dbeSGarrett Wollman warnx("%s: unresolved tc= expansion", pp->printer);
1854a1a0dbeSGarrett Wollman return 1;
1864a1a0dbeSGarrett Wollman case PCAPERR_SUCCESS:
1874a1a0dbeSGarrett Wollman break;
1884a1a0dbeSGarrett Wollman default:
1894a1a0dbeSGarrett Wollman errx(++problems, "unknown printcap library error %d", error);
1904a1a0dbeSGarrett Wollman }
1914a1a0dbeSGarrett Wollman return 0;
1924a1a0dbeSGarrett Wollman }
1934a1a0dbeSGarrett Wollman
1944a1a0dbeSGarrett Wollman /*
1954a1a0dbeSGarrett Wollman * Keep the list of spool directories. Note that we don't whine
1964a1a0dbeSGarrett Wollman * until all spool directories are noted, so that all of the more serious
1974a1a0dbeSGarrett Wollman * problems are noted first. We keep the list sorted by st_dev and
1984a1a0dbeSGarrett Wollman * st_ino, so that the problem spool directories can be noted in
1994a1a0dbeSGarrett Wollman * a single loop.
2004a1a0dbeSGarrett Wollman */
2014a1a0dbeSGarrett Wollman struct dirlist {
202e3975643SJake Burkholder LIST_ENTRY(dirlist) link;
2034a1a0dbeSGarrett Wollman struct stat stab;
2044a1a0dbeSGarrett Wollman char *path;
2054a1a0dbeSGarrett Wollman char *printer;
2064a1a0dbeSGarrett Wollman };
2074a1a0dbeSGarrett Wollman
208e3975643SJake Burkholder static LIST_HEAD(, dirlist) dirlist;
2094a1a0dbeSGarrett Wollman
2104a1a0dbeSGarrett Wollman static int
lessp(const struct dirlist * a,const struct dirlist * b)2114a1a0dbeSGarrett Wollman lessp(const struct dirlist *a, const struct dirlist *b)
2124a1a0dbeSGarrett Wollman {
2134a1a0dbeSGarrett Wollman if (a->stab.st_dev == b->stab.st_dev)
2144a1a0dbeSGarrett Wollman return a->stab.st_ino < b->stab.st_ino;
2154a1a0dbeSGarrett Wollman return a->stab.st_dev < b->stab.st_dev;
2164a1a0dbeSGarrett Wollman }
2174a1a0dbeSGarrett Wollman
2184a1a0dbeSGarrett Wollman static int
equal(const struct dirlist * a,const struct dirlist * b)2194a1a0dbeSGarrett Wollman equal(const struct dirlist *a, const struct dirlist *b)
2204a1a0dbeSGarrett Wollman {
2214a1a0dbeSGarrett Wollman return ((a->stab.st_dev == b->stab.st_dev)
2224a1a0dbeSGarrett Wollman && (a->stab.st_ino == b->stab.st_ino));
2234a1a0dbeSGarrett Wollman }
2244a1a0dbeSGarrett Wollman
2254a1a0dbeSGarrett Wollman static void
note_spool_dir(const struct printer * pp,const struct stat * st)2264a1a0dbeSGarrett Wollman note_spool_dir(const struct printer *pp, const struct stat *st)
2274a1a0dbeSGarrett Wollman {
2284a1a0dbeSGarrett Wollman struct dirlist *dp, *dp2, *last;
2294a1a0dbeSGarrett Wollman
2304a1a0dbeSGarrett Wollman dp = malloc(sizeof *dp);
231bc17d12dSPedro F. Giffuni if (dp == NULL)
2324a1a0dbeSGarrett Wollman err(++problems, "malloc(%lu)", (u_long)sizeof *dp);
2334a1a0dbeSGarrett Wollman
2344a1a0dbeSGarrett Wollman dp->stab = *st;
2354a1a0dbeSGarrett Wollman dp->printer = strdup(pp->printer);
2364a1a0dbeSGarrett Wollman if (dp->printer == 0)
2374a1a0dbeSGarrett Wollman err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL);
2384a1a0dbeSGarrett Wollman dp->path = strdup(pp->spool_dir);
2394a1a0dbeSGarrett Wollman if (dp->path == 0)
2404a1a0dbeSGarrett Wollman err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL);
2414a1a0dbeSGarrett Wollman
242bc17d12dSPedro F. Giffuni last = NULL;
2432673ed47SPoul-Henning Kamp LIST_FOREACH(dp2, &dirlist, link) {
2442673ed47SPoul-Henning Kamp if(!lessp(dp, dp2))
2452673ed47SPoul-Henning Kamp break;
2464a1a0dbeSGarrett Wollman last = dp2;
2474a1a0dbeSGarrett Wollman }
2484a1a0dbeSGarrett Wollman
2494a1a0dbeSGarrett Wollman if (last) {
2504a1a0dbeSGarrett Wollman LIST_INSERT_AFTER(last, dp, link);
2514a1a0dbeSGarrett Wollman } else {
2524a1a0dbeSGarrett Wollman LIST_INSERT_HEAD(&dirlist, dp, link);
2534a1a0dbeSGarrett Wollman }
2544a1a0dbeSGarrett Wollman }
2554a1a0dbeSGarrett Wollman
2564a1a0dbeSGarrett Wollman static void
check_spool_dirs(void)2574a1a0dbeSGarrett Wollman check_spool_dirs(void)
2584a1a0dbeSGarrett Wollman {
2594a1a0dbeSGarrett Wollman struct dirlist *dp, *dp2;
2604a1a0dbeSGarrett Wollman
2612673ed47SPoul-Henning Kamp for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) {
2622673ed47SPoul-Henning Kamp dp2 = LIST_NEXT(dp, link);
2634a1a0dbeSGarrett Wollman
264bc17d12dSPedro F. Giffuni if (dp2 != NULL && equal(dp, dp2)) {
2654a1a0dbeSGarrett Wollman ++problems;
2664a1a0dbeSGarrett Wollman if (strcmp(dp->path, dp2->path) == 0) {
2674a1a0dbeSGarrett Wollman warnx("%s and %s share the same spool, %s",
2684a1a0dbeSGarrett Wollman dp->printer, dp2->printer, dp->path);
2694a1a0dbeSGarrett Wollman } else {
2704a1a0dbeSGarrett Wollman warnx("%s (%s) and %s (%s) are the same "
2714a1a0dbeSGarrett Wollman "directory", dp->path, dp->printer,
2724a1a0dbeSGarrett Wollman dp2->path, dp2->printer);
2734a1a0dbeSGarrett Wollman }
2744a1a0dbeSGarrett Wollman continue;
2754a1a0dbeSGarrett Wollman }
2764a1a0dbeSGarrett Wollman /* Should probably check owners and modes here. */
2774a1a0dbeSGarrett Wollman }
2784a1a0dbeSGarrett Wollman }
2794a1a0dbeSGarrett Wollman
2804a1a0dbeSGarrett Wollman #ifndef SPOOL_DIR_MODE
2814a1a0dbeSGarrett Wollman #define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \
2824a1a0dbeSGarrett Wollman | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
2834a1a0dbeSGarrett Wollman #endif
2844a1a0dbeSGarrett Wollman
2854a1a0dbeSGarrett Wollman static void
make_spool_dir(const struct printer * pp)2864a1a0dbeSGarrett Wollman make_spool_dir(const struct printer *pp)
2874a1a0dbeSGarrett Wollman {
2884a1a0dbeSGarrett Wollman char *sd = pp->spool_dir;
2894a1a0dbeSGarrett Wollman struct group *gr;
2904a1a0dbeSGarrett Wollman struct stat stab;
2914a1a0dbeSGarrett Wollman
2924a1a0dbeSGarrett Wollman if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) {
2934a1a0dbeSGarrett Wollman problems++;
2944a1a0dbeSGarrett Wollman warn("%s: mkdir %s", pp->printer, sd);
2954a1a0dbeSGarrett Wollman return;
2964a1a0dbeSGarrett Wollman }
2974a1a0dbeSGarrett Wollman gr = getgrnam("daemon");
298bc17d12dSPedro F. Giffuni if (gr == NULL)
2994a1a0dbeSGarrett Wollman errx(++problems, "cannot locate daemon group");
3004a1a0dbeSGarrett Wollman
3014a1a0dbeSGarrett Wollman if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) {
3024a1a0dbeSGarrett Wollman ++problems;
3034a1a0dbeSGarrett Wollman warn("%s: cannot change ownership to %ld:%ld", sd,
3044a1a0dbeSGarrett Wollman (long)pp->daemon_user, (long)gr->gr_gid);
3054a1a0dbeSGarrett Wollman return;
3064a1a0dbeSGarrett Wollman }
3074a1a0dbeSGarrett Wollman
3084a1a0dbeSGarrett Wollman if (chmod(sd, SPOOL_DIR_MODE) < 0) {
3094a1a0dbeSGarrett Wollman ++problems;
3109aa446f5SJohn Polstra warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE);
3114a1a0dbeSGarrett Wollman return;
3124a1a0dbeSGarrett Wollman }
3134a1a0dbeSGarrett Wollman if (stat(sd, &stab) < 0)
3144a1a0dbeSGarrett Wollman err(++problems, "stat: %s", sd);
3154a1a0dbeSGarrett Wollman
3164a1a0dbeSGarrett Wollman note_spool_dir(pp, &stab);
3174a1a0dbeSGarrett Wollman }
3184a1a0dbeSGarrett Wollman
3194a1a0dbeSGarrett Wollman static void
usage(void)3204a1a0dbeSGarrett Wollman usage(void)
3214a1a0dbeSGarrett Wollman {
32243323a1dSGarance A Drosehn fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n");
3234a1a0dbeSGarrett Wollman exit(1);
3244a1a0dbeSGarrett Wollman }
325