2 * Copyright 1997 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char copyright[] =
31 "Copyright (C) 1997, Massachusetts Institute of Technology\r\n";
33 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/queue.h>
48 #include <sys/param.h> /* needed for lp.h but not used here */
49 #include <dirent.h> /* ditto */
52 #include "pathnames.h"
53 #include "skimprintcap.h"
55 static void check_spool_dirs(void);
56 static int interpret_error(const struct printer *pp, int error);
57 static void make_spool_dir(const struct printer *pp);
58 static void note_spool_dir(const struct printer *pp, const struct stat *st);
59 static void usage(void) __dead2;
61 static int problems; /* number of problems encountered */
64 * chkprintcap - check the printcap file for syntactic and semantic errors
65 * Returns the number of problems found.
68 main(int argc, char **argv)
70 struct skiminfo *skres;
72 int c, error, makedirs, more, queuecnt, verbosity;
73 struct printer myprinter, *pp;
81 while ((c = getopt(argc, argv, "df:v")) != -1) {
88 pcap_fname = strdup(optarg);
89 setprintcap(pcap_fname);
104 if (pcap_fname == NULL)
105 pcap_fname = strdup(_PATH_PRINTCAP);
108 * Skim through the printcap file looking for simple user-mistakes
109 * which will produce the wrong result for the user, but which may
110 * be pretty hard for the user to notice. Such user-mistakes will
111 * only generate warning messages. The (fatal-) problem count will
112 * only be incremented if there is a system problem trying to read
115 skres = skim_printcap(pcap_fname, verbosity);
119 } else if (skres->fatalerr) {
120 problems = skres->fatalerr;
125 * Now use the standard capability-db routines to check the values
126 * in each of the queues defined in the printcap file.
128 more = firstprinter(pp, &error);
129 if (interpret_error(pp, error) && more)
137 if (stat(pp->spool_dir, &stab) < 0) {
138 if (errno == ENOENT && makedirs) {
142 warn("%s: %s", pp->printer, pp->spool_dir);
145 note_spool_dir(pp, &stab);
148 /* Make other queue-specific validity checks here... */
151 more = nextprinter(pp, &error);
152 if (interpret_error(pp, error) && more)
158 if (queuecnt != skres->entries) {
159 warnx("WARNING: found %d entries when skimming %s,",
160 skres->entries, pcap_fname);
161 warnx("WARNING: but only found %d queues to process!",
171 * Interpret the error code. Returns 1 if we should skip to the next
172 * record (as this record is unlikely to make sense). If the problem
173 * is very severe, exit. Otherwise, return zero.
176 interpret_error(const struct printer *pp, int error)
180 err(++problems, "reading printer database");
183 warnx("%s: loop detected in tc= expansion", pp->printer);
186 warnx("%s: unresolved tc= expansion", pp->printer);
188 case PCAPERR_SUCCESS:
191 errx(++problems, "unknown printcap library error %d", error);
197 * Keep the list of spool directories. Note that we don't whine
198 * until all spool directories are noted, so that all of the more serious
199 * problems are noted first. We keep the list sorted by st_dev and
200 * st_ino, so that the problem spool directories can be noted in
204 LIST_ENTRY(dirlist) link;
210 static LIST_HEAD(, dirlist) dirlist;
213 lessp(const struct dirlist *a, const struct dirlist *b)
215 if (a->stab.st_dev == b->stab.st_dev)
216 return a->stab.st_ino < b->stab.st_ino;
217 return a->stab.st_dev < b->stab.st_dev;
221 equal(const struct dirlist *a, const struct dirlist *b)
223 return ((a->stab.st_dev == b->stab.st_dev)
224 && (a->stab.st_ino == b->stab.st_ino));
228 note_spool_dir(const struct printer *pp, const struct stat *st)
230 struct dirlist *dp, *dp2, *last;
232 dp = malloc(sizeof *dp);
234 err(++problems, "malloc(%lu)", (u_long)sizeof *dp);
237 dp->printer = strdup(pp->printer);
238 if (dp->printer == 0)
239 err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL);
240 dp->path = strdup(pp->spool_dir);
242 err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL);
245 LIST_FOREACH(dp2, &dirlist, link) {
252 LIST_INSERT_AFTER(last, dp, link);
254 LIST_INSERT_HEAD(&dirlist, dp, link);
259 check_spool_dirs(void)
261 struct dirlist *dp, *dp2;
263 for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) {
264 dp2 = LIST_NEXT(dp, link);
266 if (dp2 != NULL && equal(dp, dp2)) {
268 if (strcmp(dp->path, dp2->path) == 0) {
269 warnx("%s and %s share the same spool, %s",
270 dp->printer, dp2->printer, dp->path);
272 warnx("%s (%s) and %s (%s) are the same "
273 "directory", dp->path, dp->printer,
274 dp2->path, dp2->printer);
278 /* Should probably check owners and modes here. */
282 #ifndef SPOOL_DIR_MODE
283 #define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \
284 | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
288 make_spool_dir(const struct printer *pp)
290 char *sd = pp->spool_dir;
294 if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) {
296 warn("%s: mkdir %s", pp->printer, sd);
299 gr = getgrnam("daemon");
301 errx(++problems, "cannot locate daemon group");
303 if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) {
305 warn("%s: cannot change ownership to %ld:%ld", sd,
306 (long)pp->daemon_user, (long)gr->gr_gid);
310 if (chmod(sd, SPOOL_DIR_MODE) < 0) {
312 warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE);
315 if (stat(sd, &stab) < 0)
316 err(++problems, "stat: %s", sd);
318 note_spool_dir(pp, &stab);
324 fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n");