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