1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * From: @(#)common.c 8.5 (Berkeley) 4/28/95 35 * 36 * $FreeBSD: src/usr.sbin/lpr/common_source/printcap.c,v 1.6.2.3 2002/04/28 23:29:10 gad Exp $ 37 */ 38 39 #include <errno.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include <sys/param.h> /* required for lp.h, but not used here */ 46 #include <sys/dirent.h> /* ditto */ 47 #include "lp.h" 48 #include "lp.local.h" 49 #include "pathnames.h" 50 51 /* 52 * Routines and data used in processing the printcap file. 53 */ 54 static char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; /* list for cget* */ 55 56 static char *capdb_canonical_name(const char *_bp); 57 static int capdb_getaltlog(char *_bp, const char *_shrt, 58 const char *_lng); 59 static int capdb_getaltnum(char *_bp, const char *_shrt, 60 const char *_lng, long _dflt, long *_result); 61 static int capdb_getaltstr(char *_bp, const char *_shrt, 62 const char *lng, const char *_dflt, char **_result); 63 static int getprintcap_int(char *_bp, struct printer *_pp); 64 65 /* 66 * Change the name of the printcap file. Used by chkprintcap(8), 67 * but could be used by other members of the suite with appropriate 68 * security measures. 69 */ 70 void 71 setprintcap(char *newfile) 72 { 73 printcapdb[0] = newfile; 74 } 75 76 /* 77 * Read the printcap database for printer `printer' into the 78 * struct printer pointed by `pp'. Return values are as for 79 * cgetent(3): -1 means we could not find what we wanted, -2 80 * means a system error occurred (and errno is set), -3 if a 81 * reference (`tc=') loop was detected, and 0 means success. 82 * 83 * Copied from lpr; should add additional capabilities as they 84 * are required by the other programs in the suite so that 85 * printcap-reading is consistent across the entire family. 86 */ 87 int 88 getprintcap(const char *printer, struct printer *pp) 89 { 90 int status; 91 char *XXX; 92 char *bp; 93 94 /* 95 * A bug in the declaration of cgetent(3) means that we have 96 * to hide the constness of its third argument. 97 */ 98 XXX = (char *)printer; 99 if ((status = cgetent(&bp, printcapdb, XXX)) < 0) 100 return status; 101 status = getprintcap_int(bp, pp); 102 free(bp); 103 return status; 104 } 105 106 /* 107 * Map the status values returned by cgetfirst/cgetnext into those 108 * used by cgetent, returning truth if there are more records to 109 * examine. This points out what is arguably a bug in the cget* 110 * interface (or at least a nasty wart). 111 */ 112 static int 113 firstnextmap(int *status) 114 { 115 switch (*status) { 116 case 0: 117 return 0; 118 case 1: 119 *status = 0; 120 return 1; 121 case 2: 122 *status = 1; 123 return 1; 124 case -1: 125 *status = -2; 126 return 0; 127 case -2: 128 *status = -3; 129 return 1; 130 default: 131 return 0; 132 } 133 } 134 135 /* 136 * Scan through the database of printers using cgetfirst/cgetnext. 137 * Return false of error or end-of-database; else true. 138 */ 139 int 140 firstprinter(struct printer *pp, int *error) 141 { 142 int status; 143 char *bp; 144 145 init_printer(pp); 146 status = cgetfirst(&bp, printcapdb); 147 if (firstnextmap(&status) == 0) { 148 if (error) 149 *error = status; 150 return 0; 151 } 152 if (error) 153 *error = status; 154 status = getprintcap_int(bp, pp); 155 free(bp); 156 if (error && status) 157 *error = status; 158 return 1; 159 } 160 161 int 162 nextprinter(struct printer *pp, int *error) 163 { 164 int status; 165 char *bp; 166 167 free_printer(pp); 168 status = cgetnext(&bp, printcapdb); 169 if (firstnextmap(&status) == 0) { 170 if (error) 171 *error = status; 172 return 0; 173 } 174 if (error) 175 *error = status; 176 status = getprintcap_int(bp, pp); 177 free(bp); 178 if (error && status) 179 *error = status; 180 return 1; 181 } 182 183 void 184 lastprinter(void) 185 { 186 cgetclose(); 187 } 188 189 /* 190 * This must match the order of declaration of enum filter in lp.h. 191 */ 192 static const char *filters[] = { 193 "cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf" 194 }; 195 196 static const char *longfilters[] = { 197 "filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff", 198 "filt.output", "filt.fortran", "filt.troff", "filt.raster" 199 }; 200 201 /* 202 * Internal routine for both getprintcap() and nextprinter(). 203 * Actually parse the printcap entry using cget* functions. 204 * Also attempt to figure out the canonical name of the printer 205 * and store a malloced copy of it in pp->printer. 206 */ 207 static int 208 getprintcap_int(char *bp, struct printer *pp) 209 { 210 enum lpd_filters filt; 211 char *rp_name; 212 int error; 213 214 if ((pp->printer = capdb_canonical_name(bp)) == NULL) 215 return PCAPERR_OSERR; 216 217 #define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0) 218 CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file)); 219 CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate)); 220 CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT, 221 &pp->conn_timeout)); 222 CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID, 223 &pp->daemon_user)); 224 CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed)); 225 CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE, 226 &pp->log_file)); 227 CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file)); 228 CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp)); 229 CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES, 230 &pp->max_copies)); 231 CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set)); 232 CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks)); 233 CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100)); 234 CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH, 235 &pp->page_length)); 236 CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH, 237 &pp->page_width)); 238 CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth)); 239 CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength)); 240 CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0, 241 &pp->restrict_grp)); 242 CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host)); 243 CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP, 244 &pp->remote_queue)); 245 CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL, 246 &pp->spool_dir)); 247 CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv)); 248 CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send)); 249 CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT, 250 &pp->status_file)); 251 CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer)); 252 253 pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies"); 254 pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted"); 255 pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short"); 256 pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies"); 257 pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed"); 258 pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable"); 259 pp->header_last = capdb_getaltlog(bp, "hl", "banner.last"); 260 pp->rw = capdb_getaltlog(bp, "rw", "tty.rw"); 261 pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform"); 262 263 /* 264 * Decide if the remote printer name matches the local printer name. 265 * If no name is given then we assume they mean them to match. 266 * If a name is given see if the rp_name is one of the names for 267 * this printer. 268 */ 269 pp->rp_matches_local = 1; 270 CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name))); 271 if (error != PCAPERR_NOTFOUND && rp_name != NULL) { 272 if (cgetmatch(bp,rp_name) != 0) 273 pp->rp_matches_local = 0; 274 free(rp_name); 275 } 276 277 /* 278 * Filters: 279 */ 280 for (filt = 0; filt < LPF_COUNT; filt++) { 281 CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0, 282 &pp->filters[filt])); 283 } 284 285 return 0; 286 } 287 288 /* 289 * Decode the error codes returned by cgetent() using the names we 290 * made up for them from "lp.h". 291 * This would have been much better done with Common Error, >sigh<. 292 * Perhaps this can be fixed in the next incarnation of cget*. 293 */ 294 const char * 295 pcaperr(int error) 296 { 297 switch(error) { 298 case PCAPERR_TCOPEN: 299 return "unresolved tc= expansion"; 300 case PCAPERR_SUCCESS: 301 return "no error"; 302 case PCAPERR_NOTFOUND: 303 return "printer not found"; 304 case PCAPERR_OSERR: 305 return strerror(errno); 306 case PCAPERR_TCLOOP: 307 return "loop detected in tc= expansion"; 308 default: 309 return "unknown printcap error"; 310 } 311 } 312 313 /* 314 * Initialize a `struct printer' to contain values harmless to 315 * the other routines in liblpr. 316 */ 317 void 318 init_printer(struct printer *pp) 319 { 320 static struct printer zero; 321 *pp = zero; 322 } 323 324 /* 325 * Free the dynamically-allocated strings in a `struct printer'. 326 * Idempotent. 327 */ 328 void 329 free_printer(struct printer *pp) 330 { 331 enum lpd_filters filt; 332 #define cfree(x) do { if (x) free(x); } while(0) 333 cfree(pp->printer); 334 cfree(pp->acct_file); 335 for (filt = 0; filt < LPF_COUNT; filt++) 336 cfree(pp->filters[filt]); 337 cfree(pp->form_feed); 338 cfree(pp->log_file); 339 cfree(pp->lock_file); 340 cfree(pp->lp); 341 cfree(pp->restrict_grp); 342 cfree(pp->remote_host); 343 cfree(pp->remote_queue); 344 cfree(pp->spool_dir); 345 cfree(pp->stat_recv); 346 cfree(pp->stat_send); 347 cfree(pp->status_file); 348 cfree(pp->trailer); 349 cfree(pp->mode_set); 350 351 init_printer(pp); 352 } 353 354 /* 355 * The following routines are part of what would be a sensible library 356 * interface to capability databases. Maybe someday this will become 357 * the default. 358 */ 359 360 /* 361 * It provides similar functionality to cgetstr(), 362 * except that it provides for both a long and a short 363 * capability name and allows for a default to be specified. 364 */ 365 static int 366 capdb_getaltstr(char *bp, const char *shrt, const char *lng, 367 const char *dflt, char **result) 368 { 369 int status; 370 371 status = cgetstr(bp, (char *)/*XXX*/lng, result); 372 if (status >= 0 || status == PCAPERR_OSERR) 373 return status; 374 status = cgetstr(bp, (char *)/*XXX*/shrt, result); 375 if (status >= 0 || status == PCAPERR_OSERR) 376 return status; 377 if (dflt) { 378 *result = strdup(dflt); 379 if (*result == NULL) 380 return PCAPERR_OSERR; 381 return strlen(*result); 382 } 383 return PCAPERR_NOTFOUND; 384 } 385 386 /* 387 * The same, only for integers. 388 */ 389 static int 390 capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt, 391 long *result) 392 { 393 int status; 394 395 status = cgetnum(bp, (char *)/*XXX*/lng, result); 396 if (status >= 0) 397 return status; 398 status = cgetnum(bp, (char *)/*XXX*/shrt, result); 399 if (status >= 0) 400 return status; 401 *result = dflt; 402 return 0; 403 } 404 405 /* 406 * Likewise for logical values. There's no need for a default parameter 407 * because the default is always false. 408 */ 409 static int 410 capdb_getaltlog(char *bp, const char *shrt, const char *lng) 411 { 412 if (cgetcap(bp, (char *)/*XXX*/lng, ':')) 413 return 1; 414 if (cgetcap(bp, (char *)/*XXX*/shrt, ':')) 415 return 1; 416 return 0; 417 } 418 419 /* 420 * Also should be a part of a better cget* library. 421 * Given a capdb entry, attempt to figure out what its canonical name 422 * is, and return a malloced copy of it. The canonical name is 423 * considered to be the first one listed. 424 */ 425 static char * 426 capdb_canonical_name(const char *bp) 427 { 428 char *retval; 429 const char *nameend; 430 431 nameend = strpbrk(bp, "|:"); 432 if (nameend == NULL) 433 nameend = bp + 1; 434 if ((retval = malloc(nameend - bp + 1)) != NULL) { 435 retval[0] = '\0'; 436 strncat(retval, bp, nameend - bp); 437 } 438 return retval; 439 } 440 441 442