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