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
29 * @(#) Copyright (C) 1997, Massachusetts Institute of Technology
30 * $FreeBSD: src/usr.sbin/lpr/chkprintcap/chkprintcap.c,v 1.3.2.2 2002/04/18 20:45:23 gad Exp $
31 * $DragonFly: src/usr.sbin/lpr/chkprintcap/chkprintcap.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
34 #include <sys/types.h>
35 #include <sys/queue.h>
46 #include <sys/param.h> /* needed for lp.h but not used here */
47 #include <dirent.h> /* ditto */
50 #include "pathnames.h"
51 #include "skimprintcap.h"
53 static void check_spool_dirs(void);
54 static int interpret_error(const struct printer
*pp
, int error
);
55 static void make_spool_dir(const struct printer
*pp
);
56 static void note_spool_dir(const struct printer
*pp
, const struct stat
*st
);
57 static void usage(void) __dead2
;
59 static int problems
; /* number of problems encountered */
62 * chkprintcap - check the printcap file for syntactic and semantic errors
63 * Returns the number of problems found.
66 main(int argc
, char **argv
)
68 struct skiminfo
*skres
;
70 int c
, error
, makedirs
, more
, queuecnt
, verbosity
;
71 struct printer myprinter
, *pp
;
79 while ((c
= getopt(argc
, argv
, "df:v")) != -1) {
86 pcap_fname
= strdup(optarg
);
87 setprintcap(pcap_fname
);
102 if (pcap_fname
== NULL
)
103 pcap_fname
= strdup(_PATH_PRINTCAP
);
106 * Skim through the printcap file looking for simple user-mistakes
107 * which will produce the wrong result for the user, but which may
108 * be pretty hard for the user to notice. Such user-mistakes will
109 * only generate warning messages. The (fatal-) problem count will
110 * only be incremented if there is a system problem trying to read
113 skres
= skim_printcap(pcap_fname
, verbosity
);
115 return (skres
->fatalerr
);
118 * Now use the standard capability-db routines to check the values
119 * in each of the queues defined in the printcap file.
121 more
= firstprinter(pp
, &error
);
122 if (interpret_error(pp
, error
) && more
)
130 if (stat(pp
->spool_dir
, &stab
) < 0) {
131 if (errno
== ENOENT
&& makedirs
) {
135 warn("%s: %s", pp
->printer
, pp
->spool_dir
);
138 note_spool_dir(pp
, &stab
);
141 /* Make other queue-specific validity checks here... */
144 more
= nextprinter(pp
, &error
);
145 if (interpret_error(pp
, error
) && more
)
151 if (queuecnt
!= skres
->entries
) {
152 warnx("WARNING: found %d entries when skimming %s,",
153 skres
->entries
, pcap_fname
);
154 warnx("WARNING: but only found %d queues to process!",
161 * Interpret the error code. Returns 1 if we should skip to the next
162 * record (as this record is unlikely to make sense). If the problem
163 * is very severe, exit. Otherwise, return zero.
166 interpret_error(const struct printer
*pp
, int error
)
170 err(++problems
, "reading printer database");
173 warnx("%s: loop detected in tc= expansion", pp
->printer
);
176 warnx("%s: unresolved tc= expansion", pp
->printer
);
178 case PCAPERR_SUCCESS
:
181 errx(++problems
, "unknown printcap library error %d", error
);
187 * Keep the list of spool directories. Note that we don't whine
188 * until all spool directories are noted, so that all of the more serious
189 * problems are noted first. We keep the list sorted by st_dev and
190 * st_ino, so that the problem spool directories can be noted in
194 LIST_ENTRY(dirlist
) link
;
200 static LIST_HEAD(, dirlist
) dirlist
;
203 lessp(const struct dirlist
*a
, const struct dirlist
*b
)
205 if (a
->stab
.st_dev
== b
->stab
.st_dev
)
206 return a
->stab
.st_ino
< b
->stab
.st_ino
;
207 return a
->stab
.st_dev
< b
->stab
.st_dev
;
211 equal(const struct dirlist
*a
, const struct dirlist
*b
)
213 return ((a
->stab
.st_dev
== b
->stab
.st_dev
)
214 && (a
->stab
.st_ino
== b
->stab
.st_ino
));
218 note_spool_dir(const struct printer
*pp
, const struct stat
*st
)
220 struct dirlist
*dp
, *dp2
, *last
;
222 dp
= malloc(sizeof *dp
);
224 err(++problems
, "malloc(%lu)", (u_long
)sizeof *dp
);
227 dp
->printer
= strdup(pp
->printer
);
228 if (dp
->printer
== 0)
229 err(++problems
, "malloc(%lu)", strlen(pp
->printer
) + 1UL);
230 dp
->path
= strdup(pp
->spool_dir
);
232 err(++problems
, "malloc(%lu)", strlen(pp
->spool_dir
) + 1UL);
235 LIST_FOREACH(dp2
, &dirlist
, link
) {
242 LIST_INSERT_AFTER(last
, dp
, link
);
244 LIST_INSERT_HEAD(&dirlist
, dp
, link
);
249 check_spool_dirs(void)
251 struct dirlist
*dp
, *dp2
;
253 for (dp
= LIST_FIRST(&dirlist
); dp
; dp
= dp2
) {
254 dp2
= LIST_NEXT(dp
, link
);
256 if (dp2
!= 0 && equal(dp
, dp2
)) {
258 if (strcmp(dp
->path
, dp2
->path
) == 0) {
259 warnx("%s and %s share the same spool, %s",
260 dp
->printer
, dp2
->printer
, dp
->path
);
262 warnx("%s (%s) and %s (%s) are the same "
263 "directory", dp
->path
, dp
->printer
,
264 dp2
->path
, dp2
->printer
);
268 /* Should probably check owners and modes here. */
272 #ifndef SPOOL_DIR_MODE
273 #define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \
274 | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
278 make_spool_dir(const struct printer
*pp
)
280 char *sd
= pp
->spool_dir
;
284 if (mkdir(sd
, S_IRUSR
| S_IXUSR
) < 0) {
286 warn("%s: mkdir %s", pp
->printer
, sd
);
289 gr
= getgrnam("daemon");
291 errx(++problems
, "cannot locate daemon group");
293 if (chown(sd
, pp
->daemon_user
, gr
->gr_gid
) < 0) {
295 warn("%s: cannot change ownership to %ld:%ld", sd
,
296 (long)pp
->daemon_user
, (long)gr
->gr_gid
);
300 if (chmod(sd
, SPOOL_DIR_MODE
) < 0) {
302 warn("%s: cannot change mode to %lo", sd
, (long)SPOOL_DIR_MODE
);
305 if (stat(sd
, &stab
) < 0)
306 err(++problems
, "stat: %s", sd
);
308 note_spool_dir(pp
, &stab
);
314 fprintf(stderr
, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n");