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->fatalerr) 117 return (skres->fatalerr); 118 119 /* 120 * Now use the standard capability-db routines to check the values 121 * in each of the queues defined in the printcap file. 122 */ 123 more = firstprinter(pp, &error); 124 if (interpret_error(pp, error) && more) 125 goto next; 126 127 while (more) { 128 struct stat stab; 129 130 queuecnt++; 131 errno = 0; 132 if (stat(pp->spool_dir, &stab) < 0) { 133 if (errno == ENOENT && makedirs) { 134 make_spool_dir(pp); 135 } else { 136 problems++; 137 warn("%s: %s", pp->printer, pp->spool_dir); 138 } 139 } else { 140 note_spool_dir(pp, &stab); 141 } 142 143 /* Make other queue-specific validity checks here... */ 144 145 next: 146 more = nextprinter(pp, &error); 147 if (interpret_error(pp, error) && more) 148 goto next; 149 } 150 151 check_spool_dirs(); 152 153 if (queuecnt != skres->entries) { 154 warnx("WARNING: found %d entries when skimming %s,", 155 skres->entries, pcap_fname); 156 warnx("WARNING: but only found %d queues to process!", 157 queuecnt); 158 } 159 return (problems); 160 } 161 162 /* 163 * Interpret the error code. Returns 1 if we should skip to the next 164 * record (as this record is unlikely to make sense). If the problem 165 * is very severe, exit. Otherwise, return zero. 166 */ 167 static int 168 interpret_error(const struct printer *pp, int error) 169 { 170 switch(error) { 171 case PCAPERR_OSERR: 172 err(++problems, "reading printer database"); 173 case PCAPERR_TCLOOP: 174 ++problems; 175 warnx("%s: loop detected in tc= expansion", pp->printer); 176 return 1; 177 case PCAPERR_TCOPEN: 178 warnx("%s: unresolved tc= expansion", pp->printer); 179 return 1; 180 case PCAPERR_SUCCESS: 181 break; 182 default: 183 errx(++problems, "unknown printcap library error %d", error); 184 } 185 return 0; 186 } 187 188 /* 189 * Keep the list of spool directories. Note that we don't whine 190 * until all spool directories are noted, so that all of the more serious 191 * problems are noted first. We keep the list sorted by st_dev and 192 * st_ino, so that the problem spool directories can be noted in 193 * a single loop. 194 */ 195 struct dirlist { 196 LIST_ENTRY(dirlist) link; 197 struct stat stab; 198 char *path; 199 char *printer; 200 }; 201 202 static LIST_HEAD(, dirlist) dirlist; 203 204 static int 205 lessp(const struct dirlist *a, const struct dirlist *b) 206 { 207 if (a->stab.st_dev == b->stab.st_dev) 208 return a->stab.st_ino < b->stab.st_ino; 209 return a->stab.st_dev < b->stab.st_dev; 210 } 211 212 static int 213 equal(const struct dirlist *a, const struct dirlist *b) 214 { 215 return ((a->stab.st_dev == b->stab.st_dev) 216 && (a->stab.st_ino == b->stab.st_ino)); 217 } 218 219 static void 220 note_spool_dir(const struct printer *pp, const struct stat *st) 221 { 222 struct dirlist *dp, *dp2, *last; 223 224 dp = malloc(sizeof *dp); 225 if (dp == NULL) 226 err(++problems, "malloc(%lu)", (u_long)sizeof *dp); 227 228 dp->stab = *st; 229 dp->printer = strdup(pp->printer); 230 if (dp->printer == 0) 231 err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL); 232 dp->path = strdup(pp->spool_dir); 233 if (dp->path == 0) 234 err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL); 235 236 last = NULL; 237 LIST_FOREACH(dp2, &dirlist, link) { 238 if(!lessp(dp, dp2)) 239 break; 240 last = dp2; 241 } 242 243 if (last) { 244 LIST_INSERT_AFTER(last, dp, link); 245 } else { 246 LIST_INSERT_HEAD(&dirlist, dp, link); 247 } 248 } 249 250 static void 251 check_spool_dirs(void) 252 { 253 struct dirlist *dp, *dp2; 254 255 for (dp = LIST_FIRST(&dirlist); dp; dp = dp2) { 256 dp2 = LIST_NEXT(dp, link); 257 258 if (dp2 != NULL && equal(dp, dp2)) { 259 ++problems; 260 if (strcmp(dp->path, dp2->path) == 0) { 261 warnx("%s and %s share the same spool, %s", 262 dp->printer, dp2->printer, dp->path); 263 } else { 264 warnx("%s (%s) and %s (%s) are the same " 265 "directory", dp->path, dp->printer, 266 dp2->path, dp2->printer); 267 } 268 continue; 269 } 270 /* Should probably check owners and modes here. */ 271 } 272 } 273 274 #ifndef SPOOL_DIR_MODE 275 #define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \ 276 | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 277 #endif 278 279 static void 280 make_spool_dir(const struct printer *pp) 281 { 282 char *sd = pp->spool_dir; 283 struct group *gr; 284 struct stat stab; 285 286 if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) { 287 problems++; 288 warn("%s: mkdir %s", pp->printer, sd); 289 return; 290 } 291 gr = getgrnam("daemon"); 292 if (gr == NULL) 293 errx(++problems, "cannot locate daemon group"); 294 295 if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) { 296 ++problems; 297 warn("%s: cannot change ownership to %ld:%ld", sd, 298 (long)pp->daemon_user, (long)gr->gr_gid); 299 return; 300 } 301 302 if (chmod(sd, SPOOL_DIR_MODE) < 0) { 303 ++problems; 304 warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE); 305 return; 306 } 307 if (stat(sd, &stab) < 0) 308 err(++problems, "stat: %s", sd); 309 310 note_spool_dir(pp, &stab); 311 } 312 313 static void 314 usage(void) 315 { 316 fprintf(stderr, "usage:\n\tchkprintcap [-dv] [-f printcapfile]\n"); 317 exit(1); 318 } 319