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