xref: /freebsd/usr.sbin/lpr/chkprintcap/chkprintcap.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*
2  * Copyright 1997 Massachusetts Institute of Technology
3  *
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
14  * warranty.
15  *
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
27  * SUCH DAMAGE.
28  */
29 
30 static const char copyright[] =
31 	"Copyright (C) 1997, Massachusetts Institute of Technology\r\n";
32 
33 #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
34 #include <sys/types.h>
35 #include <sys/queue.h>
36 #include <sys/stat.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <grp.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 
46 #include <sys/param.h>		/* needed for lp.h but not used here */
47 #include <dirent.h>		/* ditto */
48 #include "lp.h"
49 #include "lp.local.h"
50 #include "pathnames.h"
51 #include "skimprintcap.h"
52 
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;
58 
59 static	int problems;		/* number of problems encountered */
60 
61 /*
62  * chkprintcap - check the printcap file for syntactic and semantic errors
63  * Returns the number of problems found.
64  */
65 int
66 main(int argc, char **argv)
67 {
68 	struct skiminfo *skres;
69 	char *pcap_fname;
70 	int c, error, makedirs, more, queuecnt, verbosity;
71 	struct printer myprinter, *pp;
72 
73 	makedirs = 0;
74 	queuecnt = 0;
75 	verbosity = 0;
76 	pcap_fname = NULL;
77 	pp = &myprinter;
78 
79 	while ((c = getopt(argc, argv, "df:v")) != -1) {
80 		switch (c) {
81 		case 'd':
82 			makedirs = 1;
83 			break;
84 
85 		case 'f':
86 			pcap_fname = strdup(optarg);
87 			setprintcap(pcap_fname);
88 			break;
89 
90 		case 'v':
91 			verbosity++;
92 			break;
93 
94 		default:
95 			usage();
96 		}
97 	}
98 
99 	if (optind != argc)
100 		usage();
101 
102 	if (pcap_fname == NULL)
103 		pcap_fname = strdup(_PATH_PRINTCAP);
104 
105 	/*
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
111 	 * the printcap file.
112 	*/
113 	skres = skim_printcap(pcap_fname, verbosity);
114 	if (skres == NULL) {
115 		problems = 1;
116 		goto main_ret;
117 	} else if (skres->fatalerr) {
118 		problems = skres->fatalerr;
119 		goto main_ret;
120 	}
121 
122 	/*
123 	 * Now use the standard capability-db routines to check the values
124 	 * in each of the queues defined in the printcap file.
125 	*/
126 	more = firstprinter(pp, &error);
127 	if (interpret_error(pp, error) && more)
128 		goto next;
129 
130 	while (more) {
131 		struct stat stab;
132 
133 		queuecnt++;
134 		errno = 0;
135 		if (stat(pp->spool_dir, &stab) < 0) {
136 			if (errno == ENOENT && makedirs) {
137 				make_spool_dir(pp);
138 			} else {
139 				problems++;
140 				warn("%s: %s", pp->printer, pp->spool_dir);
141 			}
142 		} else {
143 			note_spool_dir(pp, &stab);
144 		}
145 
146 		/* Make other queue-specific validity checks here... */
147 
148 next:
149 		more = nextprinter(pp, &error);
150 		if (interpret_error(pp, error) && more)
151 			goto next;
152 	}
153 
154 	check_spool_dirs();
155 
156 	if (queuecnt != skres->entries) {
157 		warnx("WARNING: found %d entries when skimming %s,",
158 		    skres->entries, pcap_fname);
159 		warnx("WARNING:  but only found %d queues to process!",
160 		    queuecnt);
161 	}
162 
163 main_ret:
164 	free(pcap_fname);
165 	return (problems);
166 }
167 
168 /*
169  * Interpret the error code.  Returns 1 if we should skip to the next
170  * record (as this record is unlikely to make sense).  If the problem
171  * is very severe, exit.  Otherwise, return zero.
172  */
173 static int
174 interpret_error(const struct printer *pp, int error)
175 {
176 	switch(error) {
177 	case PCAPERR_OSERR:
178 		err(++problems, "reading printer database");
179 	case PCAPERR_TCLOOP:
180 		++problems;
181 		warnx("%s: loop detected in tc= expansion", pp->printer);
182 		return 1;
183 	case PCAPERR_TCOPEN:
184 		warnx("%s: unresolved tc= expansion", pp->printer);
185 		return 1;
186 	case PCAPERR_SUCCESS:
187 		break;
188 	default:
189 		errx(++problems, "unknown printcap library error %d", error);
190 	}
191 	return 0;
192 }
193 
194 /*
195  * Keep the list of spool directories.  Note that we don't whine
196  * until all spool directories are noted, so that all of the more serious
197  * problems are noted first.  We keep the list sorted by st_dev and
198  * st_ino, so that the problem spool directories can be noted in
199  * a single loop.
200  */
201 struct	dirlist {
202 	LIST_ENTRY(dirlist) link;
203 	struct stat stab;
204 	char *path;
205 	char *printer;
206 };
207 
208 static	LIST_HEAD(, dirlist) dirlist;
209 
210 static int
211 lessp(const struct dirlist *a, const struct dirlist *b)
212 {
213 	if (a->stab.st_dev == b->stab.st_dev)
214 		return a->stab.st_ino < b->stab.st_ino;
215 	return a->stab.st_dev < b->stab.st_dev;
216 }
217 
218 static int
219 equal(const struct dirlist *a, const struct dirlist *b)
220 {
221 	return ((a->stab.st_dev == b->stab.st_dev)
222 		&& (a->stab.st_ino == b->stab.st_ino));
223 }
224 
225 static void
226 note_spool_dir(const struct printer *pp, const struct stat *st)
227 {
228 	struct dirlist *dp, *dp2, *last;
229 
230 	dp = malloc(sizeof *dp);
231 	if (dp == NULL)
232 		err(++problems, "malloc(%lu)", (u_long)sizeof *dp);
233 
234 	dp->stab = *st;
235 	dp->printer = strdup(pp->printer);
236 	if (dp->printer == 0)
237 		err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL);
238 	dp->path = strdup(pp->spool_dir);
239 	if (dp->path == 0)
240 		err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL);
241 
242 	last = NULL;
243 	LIST_FOREACH(dp2, &dirlist, link) {
244 		if(!lessp(dp, dp2))
245 			break;
246 		last = dp2;
247 	}
248 
249 	if (last) {
250 		LIST_INSERT_AFTER(last, dp, link);
251 	} else {
252 		LIST_INSERT_HEAD(&dirlist, dp, link);
253 	}
254 }
255 
256 static void
257 check_spool_dirs(void)
258 {
259 	struct dirlist *dp, *dp2;
260 
261 	for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) {
262 		dp2 = LIST_NEXT(dp, link);
263 
264 		if (dp2 != NULL && equal(dp, dp2)) {
265 			++problems;
266 			if (strcmp(dp->path, dp2->path) == 0) {
267 				warnx("%s and %s share the same spool, %s",
268 				      dp->printer, dp2->printer, dp->path);
269 			} else {
270 				warnx("%s (%s) and %s (%s) are the same "
271 				      "directory", dp->path, dp->printer,
272 				      dp2->path, dp2->printer);
273 			}
274 			continue;
275 		}
276 		/* Should probably check owners and modes here. */
277 	}
278 }
279 
280 #ifndef SPOOL_DIR_MODE
281 #define	SPOOL_DIR_MODE	(S_IRUSR | S_IWUSR | S_IXUSR \
282 			 | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
283 #endif
284 
285 static void
286 make_spool_dir(const struct printer *pp)
287 {
288 	char *sd = pp->spool_dir;
289 	struct group *gr;
290 	struct stat stab;
291 
292 	if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) {
293 		problems++;
294 		warn("%s: mkdir %s", pp->printer, sd);
295 		return;
296 	}
297 	gr = getgrnam("daemon");
298 	if (gr == NULL)
299 		errx(++problems, "cannot locate daemon group");
300 
301 	if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) {
302 		++problems;
303 		warn("%s: cannot change ownership to %ld:%ld", sd,
304 		     (long)pp->daemon_user, (long)gr->gr_gid);
305 		return;
306 	}
307 
308 	if (chmod(sd, SPOOL_DIR_MODE) < 0) {
309 		++problems;
310 		warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE);
311 		return;
312 	}
313 	if (stat(sd, &stab) < 0)
314 		err(++problems, "stat: %s", sd);
315 
316 	note_spool_dir(pp, &stab);
317 }
318 
319 static void
320 usage(void)
321 {
322 	fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n");
323 	exit(1);
324 }
325