chkprintcap.c revision 60938
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 30static const char copyright[] = 31 "Copyright (C) 1997, Massachusetts Institute of Technology\r\n"; 32static const char rcsid[] = 33 "$FreeBSD: head/usr.sbin/lpr/chkprintcap/chkprintcap.c 60938 2000-05-26 02:09:24Z jake $"; 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 52static void check_spool_dirs(void); 53static int interpret_error(const struct printer *pp, int error); 54static void make_spool_dir(const struct printer *pp); 55static void note_spool_dir(const struct printer *pp, const struct stat *st); 56static void usage(void) __dead2; 57 58static 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 */ 64int 65main(int argc, char **argv) 66{ 67 int c, error, makedirs, more; 68 struct printer myprinter, *pp; 69 70 makedirs = 0; 71 pp = &myprinter; 72 73 while ((c = getopt(argc, argv, "df:")) != -1) { 74 switch (c) { 75 case 'd': 76 makedirs = 1; 77 break; 78 79 case 'f': 80 setprintcap(optarg); 81 break; 82 83 default: 84 usage(); 85 } 86 } 87 88 if (optind != argc) 89 usage(); 90 91 more = firstprinter(pp, &error); 92 if (interpret_error(pp, error) && more) 93 goto next; 94 95 while (more) { 96 struct stat stab; 97 98 errno = 0; 99 if (stat(pp->spool_dir, &stab) < 0) { 100 if (errno == ENOENT && makedirs) { 101 make_spool_dir(pp); 102 } else { 103 problems++; 104 warn("%s: %s", pp->printer, pp->spool_dir); 105 } 106 } else { 107 note_spool_dir(pp, &stab); 108 } 109 110 /* Make other validity checks here... */ 111 112next: 113 more = nextprinter(pp, &error); 114 if (interpret_error(pp, error) && more) 115 goto next; 116 } 117 check_spool_dirs(); 118 return problems; 119} 120 121/* 122 * Interpret the error code. Returns 1 if we should skip to the next 123 * record (as this record is unlikely to make sense). If the problem 124 * is very severe, exit. Otherwise, return zero. 125 */ 126static int 127interpret_error(const struct printer *pp, int error) 128{ 129 switch(error) { 130 case PCAPERR_OSERR: 131 err(++problems, "reading printer database"); 132 case PCAPERR_TCLOOP: 133 ++problems; 134 warnx("%s: loop detected in tc= expansion", pp->printer); 135 return 1; 136 case PCAPERR_TCOPEN: 137 warnx("%s: unresolved tc= expansion", pp->printer); 138 return 1; 139 case PCAPERR_SUCCESS: 140 break; 141 default: 142 errx(++problems, "unknown printcap library error %d", error); 143 } 144 return 0; 145} 146 147/* 148 * Keep the list of spool directories. Note that we don't whine 149 * until all spool directories are noted, so that all of the more serious 150 * problems are noted first. We keep the list sorted by st_dev and 151 * st_ino, so that the problem spool directories can be noted in 152 * a single loop. 153 */ 154struct dirlist { 155 LIST_ENTRY(dirlist) link; 156 struct stat stab; 157 char *path; 158 char *printer; 159}; 160 161static LIST_HEAD(, dirlist) dirlist; 162 163static int 164lessp(const struct dirlist *a, const struct dirlist *b) 165{ 166 if (a->stab.st_dev == b->stab.st_dev) 167 return a->stab.st_ino < b->stab.st_ino; 168 return a->stab.st_dev < b->stab.st_dev; 169} 170 171static int 172equal(const struct dirlist *a, const struct dirlist *b) 173{ 174 return ((a->stab.st_dev == b->stab.st_dev) 175 && (a->stab.st_ino == b->stab.st_ino)); 176} 177 178static void 179note_spool_dir(const struct printer *pp, const struct stat *st) 180{ 181 struct dirlist *dp, *dp2, *last; 182 183 dp = malloc(sizeof *dp); 184 if (dp == 0) 185 err(++problems, "malloc(%lu)", (u_long)sizeof *dp); 186 187 dp->stab = *st; 188 dp->printer = strdup(pp->printer); 189 if (dp->printer == 0) 190 err(++problems, "malloc(%lu)", strlen(pp->printer) + 1UL); 191 dp->path = strdup(pp->spool_dir); 192 if (dp->path == 0) 193 err(++problems, "malloc(%lu)", strlen(pp->spool_dir) + 1UL); 194 195 last = 0; 196 dp2 = dirlist.lh_first; 197 while (dp2 && lessp(dp, dp2)) { 198 last = dp2; 199 dp2 = dp2->link.le_next; 200 } 201 202 if (last) { 203 LIST_INSERT_AFTER(last, dp, link); 204 } else { 205 LIST_INSERT_HEAD(&dirlist, dp, link); 206 } 207} 208 209static void 210check_spool_dirs(void) 211{ 212 struct dirlist *dp, *dp2; 213 214 for (dp = dirlist.lh_first; dp; dp = dp2) { 215 dp2 = dp->link.le_next; 216 217 if (dp2 != 0 && equal(dp, dp2)) { 218 ++problems; 219 if (strcmp(dp->path, dp2->path) == 0) { 220 warnx("%s and %s share the same spool, %s", 221 dp->printer, dp2->printer, dp->path); 222 } else { 223 warnx("%s (%s) and %s (%s) are the same " 224 "directory", dp->path, dp->printer, 225 dp2->path, dp2->printer); 226 } 227 continue; 228 } 229 /* Should probably check owners and modes here. */ 230 } 231} 232 233#ifndef SPOOL_DIR_MODE 234#define SPOOL_DIR_MODE (S_IRUSR | S_IWUSR | S_IXUSR \ 235 | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 236#endif 237 238static void 239make_spool_dir(const struct printer *pp) 240{ 241 char *sd = pp->spool_dir; 242 struct group *gr; 243 struct stat stab; 244 245 if (mkdir(sd, S_IRUSR | S_IXUSR) < 0) { 246 problems++; 247 warn("%s: mkdir %s", pp->printer, sd); 248 return; 249 } 250 gr = getgrnam("daemon"); 251 if (gr == 0) 252 errx(++problems, "cannot locate daemon group"); 253 254 if (chown(sd, pp->daemon_user, gr->gr_gid) < 0) { 255 ++problems; 256 warn("%s: cannot change ownership to %ld:%ld", sd, 257 (long)pp->daemon_user, (long)gr->gr_gid); 258 return; 259 } 260 261 if (chmod(sd, SPOOL_DIR_MODE) < 0) { 262 ++problems; 263 warn("%s: cannot change mode to %lo", sd, (long)SPOOL_DIR_MODE); 264 return; 265 } 266 if (stat(sd, &stab) < 0) 267 err(++problems, "stat: %s", sd); 268 269 note_spool_dir(pp, &stab); 270} 271 272static void 273usage(void) 274{ 275 fprintf(stderr, "usage:\n\tchkprintcap [-d] [-f printcapfile]\n"); 276 exit(1); 277} 278