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 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/queue.h>
38 #include <sys/stat.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <grp.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 #include <sys/param.h>		/* needed for lp.h but not used here */
49 #include <dirent.h>		/* ditto */
50 #include "lp.h"
51 #include "lp.local.h"
52 #include "pathnames.h"
53 #include "skimprintcap.h"
54 
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;
60 
61 static	int problems;		/* number of problems encountered */
62 
63 /*
64  * chkprintcap - check the printcap file for syntactic and semantic errors
65  * Returns the number of problems found.
66  */
67 int
68 main(int argc, char **argv)
69 {
70 	struct skiminfo *skres;
71 	char *pcap_fname;
72 	int c, error, makedirs, more, queuecnt, verbosity;
73 	struct printer myprinter, *pp;
74 
75 	makedirs = 0;
76 	queuecnt = 0;
77 	verbosity = 0;
78 	pcap_fname = NULL;
79 	pp = &myprinter;
80 
81 	while ((c = getopt(argc, argv, "df:v")) != -1) {
82 		switch (c) {
83 		case 'd':
84 			makedirs = 1;
85 			break;
86 
87 		case 'f':
88 			pcap_fname = strdup(optarg);
89 			setprintcap(pcap_fname);
90 			break;
91 
92 		case 'v':
93 			verbosity++;
94 			break;
95 
96 		default:
97 			usage();
98 		}
99 	}
100 
101 	if (optind != argc)
102 		usage();
103 
104 	if (pcap_fname == NULL)
105 		pcap_fname = strdup(_PATH_PRINTCAP);
106 
107 	/*
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
113 	 * the printcap file.
114 	*/
115 	skres = skim_printcap(pcap_fname, verbosity);
116 	if (skres == NULL) {
117 		problems = 1;
118 		goto main_ret;
119 	} else if (skres->fatalerr) {
120 		problems = skres->fatalerr;
121 		goto main_ret;
122 	}
123 
124 	/*
125 	 * Now use the standard capability-db routines to check the values
126 	 * in each of the queues defined in the printcap file.
127 	*/
128 	more = firstprinter(pp, &error);
129 	if (interpret_error(pp, error) && more)
130 		goto next;
131 
132 	while (more) {
133 		struct stat stab;
134 
135 		queuecnt++;
136 		errno = 0;
137 		if (stat(pp->spool_dir, &stab) < 0) {
138 			if (errno == ENOENT && makedirs) {
139 				make_spool_dir(pp);
140 			} else {
141 				problems++;
142 				warn("%s: %s", pp->printer, pp->spool_dir);
143 			}
144 		} else {
145 			note_spool_dir(pp, &stab);
146 		}
147 
148 		/* Make other queue-specific validity checks here... */
149 
150 next:
151 		more = nextprinter(pp, &error);
152 		if (interpret_error(pp, error) && more)
153 			goto next;
154 	}
155 
156 	check_spool_dirs();
157 
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!",
162 		    queuecnt);
163 	}
164 
165 main_ret:
166 	free(pcap_fname);
167 	return (problems);
168 }
169 
170 /*
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.
174  */
175 static int
176 interpret_error(const struct printer *pp, int error)
177 {
178 	switch(error) {
179 	case PCAPERR_OSERR:
180 		err(++problems, "reading printer database");
181 	case PCAPERR_TCLOOP:
182 		++problems;
183 		warnx("%s: loop detected in tc= expansion", pp->printer);
184 		return 1;
185 	case PCAPERR_TCOPEN:
186 		warnx("%s: unresolved tc= expansion", pp->printer);
187 		return 1;
188 	case PCAPERR_SUCCESS:
189 		break;
190 	default:
191 		errx(++problems, "unknown printcap library error %d", error);
192 	}
193 	return 0;
194 }
195 
196 /*
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
201  * a single loop.
202  */
203 struct	dirlist {
204 	LIST_ENTRY(dirlist) link;
205 	struct stat stab;
206 	char *path;
207 	char *printer;
208 };
209 
210 static	LIST_HEAD(, dirlist) dirlist;
211 
212 static int
213 lessp(const struct dirlist *a, const struct dirlist *b)
214 {
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;
218 }
219 
220 static int
221 equal(const struct dirlist *a, const struct dirlist *b)
222 {
223 	return ((a->stab.st_dev == b->stab.st_dev)
224 		&& (a->stab.st_ino == b->stab.st_ino));
225 }
226 
227 static void
228 note_spool_dir(const struct printer *pp, const struct stat *st)
229 {
230 	struct dirlist *dp, *dp2, *last;
231 
232 	dp = malloc(sizeof *dp);
233 	if (dp == NULL)
234 		err(++problems, "malloc(%lu)", (u_long)sizeof *dp);
235 
236 	dp->stab = *st;
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);
241 	if (dp->path == 0)
242 		err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL);
243 
244 	last = NULL;
245 	LIST_FOREACH(dp2, &dirlist, link) {
246 		if(!lessp(dp, dp2))
247 			break;
248 		last = dp2;
249 	}
250 
251 	if (last) {
252 		LIST_INSERT_AFTER(last, dp, link);
253 	} else {
254 		LIST_INSERT_HEAD(&dirlist, dp, link);
255 	}
256 }
257 
258 static void
259 check_spool_dirs(void)
260 {
261 	struct dirlist *dp, *dp2;
262 
263 	for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) {
264 		dp2 = LIST_NEXT(dp, link);
265 
266 		if (dp2 != NULL && equal(dp, dp2)) {
267 			++problems;
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);
271 			} else {
272 				warnx("%s (%s) and %s (%s) are the same "
273 				      "directory", dp->path, dp->printer,
274 				      dp2->path, dp2->printer);
275 			}
276 			continue;
277 		}
278 		/* Should probably check owners and modes here. */
279 	}
280 }
281 
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)
285 #endif
286 
287 static void
288 make_spool_dir(const struct printer *pp)
289 {
290 	char *sd = pp->spool_dir;
291 	struct group *gr;
292 	struct stat stab;
293 
294 	if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) {
295 		problems++;
296 		warn("%s: mkdir %s", pp->printer, sd);
297 		return;
298 	}
299 	gr = getgrnam("daemon");
300 	if (gr == NULL)
301 		errx(++problems, "cannot locate daemon group");
302 
303 	if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) {
304 		++problems;
305 		warn("%s: cannot change ownership to %ld:%ld", sd,
306 		     (long)pp->daemon_user, (long)gr->gr_gid);
307 		return;
308 	}
309 
310 	if (chmod(sd, SPOOL_DIR_MODE) < 0) {
311 		++problems;
312 		warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE);
313 		return;
314 	}
315 	if (stat(sd, &stab) < 0)
316 		err(++problems, "stat: %s", sd);
317 
318 	note_spool_dir(pp, &stab);
319 }
320 
321 static void
322 usage(void)
323 {
324 	fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n");
325 	exit(1);
326 }
327