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);
117 return (skres->fatalerr);
120 * Now use the standard capability-db routines to check the values
121 * in each of the queues defined in the printcap file.
123 more = firstprinter(pp, &error);
124 if (interpret_error(pp, error) && more)
132 if (stat(pp->spool_dir, &stab) < 0) {
133 if (errno == ENOENT && makedirs) {
137 warn("%s: %s", pp->printer, pp->spool_dir);
140 note_spool_dir(pp, &stab);
143 /* Make other queue-specific validity checks here... */
146 more = nextprinter(pp, &error);
147 if (interpret_error(pp, error) && more)
153 if (queuecnt != skres->entries) {
154 warnx("WARNING: found %d entries when skimming %s,",
155 skres->entries, pcap_fname);
156 warnx("WARNING: but only found %d queues to process!",
163 * Interpret the error code. Returns 1 if we should skip to the next
164 * record (as this record is unlikely to make sense). If the problem
165 * is very severe, exit. Otherwise, return zero.
168 interpret_error(const struct printer *pp, int error)
172 err(++problems, "reading printer database");
175 warnx("%s: loop detected in tc= expansion", pp->printer);
178 warnx("%s: unresolved tc= expansion", pp->printer);
180 case PCAPERR_SUCCESS:
183 errx(++problems, "unknown printcap library error %d", error);
189 * Keep the list of spool directories. Note that we don't whine
190 * until all spool directories are noted, so that all of the more serious
191 * problems are noted first. We keep the list sorted by st_dev and
192 * st_ino, so that the problem spool directories can be noted in
196 LIST_ENTRY(dirlist) link;
202 static LIST_HEAD(, dirlist) dirlist;
205 lessp(const struct dirlist *a, const struct dirlist *b)
207 if (a->stab.st_dev == b->stab.st_dev)
208 return a->stab.st_ino < b->stab.st_ino;
209 return a->stab.st_dev < b->stab.st_dev;
213 equal(const struct dirlist *a, const struct dirlist *b)
215 return ((a->stab.st_dev == b->stab.st_dev)
216 && (a->stab.st_ino == b->stab.st_ino));
220 note_spool_dir(const struct printer *pp, const struct stat *st)
222 struct dirlist *dp, *dp2, *last;
224 dp = malloc(sizeof *dp);
226 err(++problems, "malloc(%lu)", (u_long)sizeof *dp);
229 dp->printer = strdup(pp->printer);
230 if (dp->printer == 0)
231 err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL);
232 dp->path = strdup(pp->spool_dir);
234 err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL);
237 LIST_FOREACH(dp2, &dirlist, link) {
244 LIST_INSERT_AFTER(last, dp, link);
246 LIST_INSERT_HEAD(&dirlist, dp, link);
251 check_spool_dirs(void)
253 struct dirlist *dp, *dp2;
255 for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) {
256 dp2 = LIST_NEXT(dp, link);
258 if (dp2 != 0 && equal(dp, dp2)) {
260 if (strcmp(dp->path, dp2->path) == 0) {
261 warnx("%s and %s share the same spool, %s",
262 dp->printer, dp2->printer, dp->path);
264 warnx("%s (%s) and %s (%s) are the same "
265 "directory", dp->path, dp->printer,
266 dp2->path, dp2->printer);
270 /* Should probably check owners and modes here. */
274 #ifndef SPOOL_DIR_MODE
275 #define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \
276 | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
280 make_spool_dir(const struct printer *pp)
282 char *sd = pp->spool_dir;
286 if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) {
288 warn("%s: mkdir %s", pp->printer, sd);
291 gr = getgrnam("daemon");
293 errx(++problems, "cannot locate daemon group");
295 if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) {
297 warn("%s: cannot change ownership to %ld:%ld", sd,
298 (long)pp->daemon_user, (long)gr->gr_gid);
302 if (chmod(sd, SPOOL_DIR_MODE) < 0) {
304 warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE);
307 if (stat(sd, &stab) < 0)
308 err(++problems, "stat: %s", sd);
310 note_spool_dir(pp, &stab);
316 fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n");