1151497Sru/* 2104862Sru * Copyright (c) 1983, 1993 3151497Sru * The Regents of the University of California. All rights reserved. 4104862Sru * (c) UNIX System Laboratories, Inc. 5151497Sru * All or some portions of this file are derived from material licensed 6151497Sru * to the University of California by American Telephone and Telegraph 7104862Sru * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8104862Sru * the permission of UNIX System Laboratories, Inc. 9104862Sru * 10104862Sru * Redistribution and use in source and binary forms, with or without 11104862Sru * modification, are permitted provided that the following conditions 12104862Sru * are met: 13104862Sru * 1. Redistributions of source code must retain the above copyright 14104862Sru * notice, this list of conditions and the following disclaimer. 15104862Sru * 2. Redistributions in binary form must reproduce the above copyright 16104862Sru * notice, this list of conditions and the following disclaimer in the 17104862Sru * documentation and/or other materials provided with the distribution. 18104862Sru * 3. All advertising materials mentioning features or use of this software 19151497Sru * must display the following acknowledgement: 20114402Sru * This product includes software developed by the University of 21104862Sru * California, Berkeley and its contributors. 22104862Sru * 4. Neither the name of the University nor the names of its contributors 23104862Sru * may be used to endorse or promote products derived from this software 24104862Sru * without specific prior written permission. 25104862Sru * 26104862Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27151497Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28151497Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29151497Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30104862Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31104862Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32104862Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33151497Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34151497Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35151497Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36151497Sru * SUCH DAMAGE. 37151497Sru */ 38151497Sru 39151497Sru#if 0 40151497Sru#ifndef lint 41151497Srustatic char sccsid[] = "@(#)printcap.c 8.2 (Berkeley) 4/28/95"; 42151497Sru#endif /* not lint */ 43151497Sru#endif 44151497Sru 45151497Sru#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 46151497Sru__FBSDID("$FreeBSD$"); 47151497Sru 48151497Sru#include <errno.h> 49151497Sru#include <stdio.h> 50151497Sru#include <stdlib.h> 51151497Sru#include <string.h> 52151497Sru#include <unistd.h> 53151497Sru 54151497Sru#include <sys/param.h> /* required for lp.h, but not used here */ 55151497Sru#include <sys/dirent.h> /* ditto */ 56151497Sru#include "lp.h" 57151497Sru#include "lp.local.h" 58151497Sru#include "pathnames.h" 59151497Sru 60151497Sru/* 61151497Sru * Routines and data used in processing the printcap file. 62151497Sru */ 63151497Srustatic char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; /* list for cget* */ 64151497Sru 65151497Srustatic char *capdb_canonical_name(const char *_bp); 66151497Srustatic int capdb_getaltlog(char *_bp, const char *_shrt, 67151497Sru const char *_lng); 68151497Srustatic int capdb_getaltnum(char *_bp, const char *_shrt, 69151497Sru const char *_lng, long _dflt, long *_result); 70151497Srustatic int capdb_getaltstr(char *_bp, const char *_shrt, 71151497Sru const char *lng, const char *_dflt, char **_result); 72151497Srustatic int getprintcap_int(char *_bp, struct printer *_pp); 73151497Sru 74151497Sru/* 75151497Sru * Change the name of the printcap file. Used by chkprintcap(8), 76151497Sru * but could be used by other members of the suite with appropriate 77151497Sru * security measures. 78151497Sru */ 79151497Sruvoid 80151497Srusetprintcap(char *newfile) 81151497Sru{ 82151497Sru printcapdb[0] = newfile; 83151497Sru} 84151497Sru 85151497Sru/* 86151497Sru * Read the printcap database for printer `printer' into the 87151497Sru * struct printer pointed by `pp'. Return values are as for 88151497Sru * cgetent(3): -1 means we could not find what we wanted, -2 89151497Sru * means a system error occurred (and errno is set), -3 if a 90151497Sru * reference (`tc=') loop was detected, and 0 means success. 91151497Sru * 92151497Sru * Copied from lpr; should add additional capabilities as they 93151497Sru * are required by the other programs in the suite so that 94151497Sru * printcap-reading is consistent across the entire family. 95151497Sru */ 96151497Sruint 97151497Srugetprintcap(const char *printer, struct printer *pp) 98151497Sru{ 99151497Sru int status; 100151497Sru char *XXX; 101151497Sru char *bp; 102151497Sru 103151497Sru /* 104151497Sru * A bug in the declaration of cgetent(3) means that we have 105151497Sru * to hide the constness of its third argument. 106151497Sru */ 107151497Sru XXX = (char *)printer; 108151497Sru if ((status = cgetent(&bp, printcapdb, XXX)) < 0) 109151497Sru return status; 110151497Sru status = getprintcap_int(bp, pp); 111151497Sru free(bp); 112151497Sru return status; 113151497Sru} 114151497Sru 115151497Sru/* 116151497Sru * Map the status values returned by cgetfirst/cgetnext into those 117151497Sru * used by cgetent, returning truth if there are more records to 118151497Sru * examine. This points out what is arguably a bug in the cget* 119151497Sru * interface (or at least a nasty wart). 120151497Sru */ 121151497Srustatic int 122151497Srufirstnextmap(int *status) 123151497Sru{ 124151497Sru switch (*status) { 125151497Sru case 0: 126151497Sru return 0; 127151497Sru case 1: 128151497Sru *status = 0; 129151497Sru return 1; 130151497Sru case 2: 131151497Sru *status = 1; 132151497Sru return 1; 133151497Sru case -1: 134151497Sru *status = -2; 135151497Sru return 0; 136151497Sru case -2: 137151497Sru *status = -3; 138151497Sru return 1; 139151497Sru default: 140151497Sru return 0; 141151497Sru } 142151497Sru} 143151497Sru 144151497Sru/* 145151497Sru * Scan through the database of printers using cgetfirst/cgetnext. 146151497Sru * Return false of error or end-of-database; else true. 147151497Sru */ 148151497Sruint 149151497Srufirstprinter(struct printer *pp, int *error) 150151497Sru{ 151151497Sru int status; 152151497Sru char *bp; 153151497Sru 154151497Sru init_printer(pp); 155151497Sru status = cgetfirst(&bp, printcapdb); 156151497Sru if (firstnextmap(&status) == 0) { 157151497Sru if (error) 158151497Sru *error = status; 159151497Sru return 0; 160151497Sru } 161151497Sru if (error) 162151497Sru *error = status; 163151497Sru status = getprintcap_int(bp, pp); 164151497Sru free(bp); 165151497Sru if (error && status) 166151497Sru *error = status; 167151497Sru return 1; 168151497Sru} 169151497Sru 170151497Sruint 171151497Srunextprinter(struct printer *pp, int *error) 172151497Sru{ 173151497Sru int status; 174151497Sru char *bp; 175151497Sru 176151497Sru free_printer(pp); 177151497Sru status = cgetnext(&bp, printcapdb); 178151497Sru if (firstnextmap(&status) == 0) { 179151497Sru if (error) 180151497Sru *error = status; 181151497Sru return 0; 182151497Sru } 183151497Sru if (error) 184151497Sru *error = status; 185151497Sru status = getprintcap_int(bp, pp); 186151497Sru free(bp); 187151497Sru if (error && status) 188151497Sru *error = status; 189151497Sru return 1; 190151497Sru} 191151497Sru 192151497Sruvoid 193151497Srulastprinter(void) 194151497Sru{ 195151497Sru cgetclose(); 196151497Sru} 197151497Sru 198151497Sru/* 199151497Sru * This must match the order of declaration of enum filter in lp.h. 200151497Sru */ 201151497Srustatic const char *filters[] = { 202151497Sru "cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf" 203151497Sru}; 204151497Sru 205151497Srustatic const char *longfilters[] = { 206151497Sru "filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff", 207151497Sru "filt.output", "filt.fortran", "filt.troff", "filt.raster" 208151497Sru}; 209151497Sru 210151497Sru/* 211151497Sru * Internal routine for both getprintcap() and nextprinter(). 212151497Sru * Actually parse the printcap entry using cget* functions. 213151497Sru * Also attempt to figure out the canonical name of the printer 214151497Sru * and store a malloced copy of it in pp->printer. 215151497Sru */ 216151497Srustatic int 217151497Srugetprintcap_int(char *bp, struct printer *pp) 218151497Sru{ 219151497Sru enum lpd_filters filt; 220151497Sru char *rp_name; 221151497Sru int error; 222151497Sru 223151497Sru if ((pp->printer = capdb_canonical_name(bp)) == 0) 224151497Sru return PCAPERR_OSERR; 225151497Sru 226151497Sru#define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0) 227151497Sru CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file)); 228151497Sru CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate)); 229151497Sru CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT, 230151497Sru &pp->conn_timeout)); 231151497Sru CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID, 232151497Sru &pp->daemon_user)); 233151497Sru CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed)); 234151497Sru CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE, 235151497Sru &pp->log_file)); 236151497Sru CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file)); 237151497Sru CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp)); 238151497Sru CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES, 239151497Sru &pp->max_copies)); 240151497Sru CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set)); 241151497Sru CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks)); 242151497Sru CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100)); 243151497Sru CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH, 244151497Sru &pp->page_length)); 245151497Sru CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH, 246151497Sru &pp->page_width)); 247151497Sru CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth)); 248151497Sru CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength)); 249151497Sru CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0, 250151497Sru &pp->restrict_grp)); 251151497Sru CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host)); 252151497Sru CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP, 253151497Sru &pp->remote_queue)); 254151497Sru CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL, 255151497Sru &pp->spool_dir)); 256151497Sru CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv)); 257151497Sru CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send)); 258151497Sru CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT, 259151497Sru &pp->status_file)); 260151497Sru CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer)); 261151497Sru 262151497Sru pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies"); 263151497Sru pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted"); 264151497Sru pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short"); 265151497Sru pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies"); 266151497Sru pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed"); 267151497Sru pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable"); 268151497Sru pp->header_last = capdb_getaltlog(bp, "hl", "banner.last"); 269151497Sru pp->rw = capdb_getaltlog(bp, "rw", "tty.rw"); 270151497Sru pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform"); 271151497Sru 272151497Sru /* 273151497Sru * Decide if the remote printer name matches the local printer name. 274151497Sru * If no name is given then we assume they mean them to match. 275151497Sru * If a name is given see if the rp_name is one of the names for 276151497Sru * this printer. 277151497Sru */ 278151497Sru pp->rp_matches_local = 1; 279151497Sru CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name))); 280151497Sru if (error != PCAPERR_NOTFOUND && rp_name != NULL) { 281151497Sru if (cgetmatch(bp,rp_name) != 0) 282151497Sru pp->rp_matches_local = 0; 283151497Sru free(rp_name); 284151497Sru } 285151497Sru 286151497Sru /* 287151497Sru * Filters: 288151497Sru */ 289151497Sru for (filt = 0; filt < LPF_COUNT; filt++) { 290151497Sru CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0, 291151497Sru &pp->filters[filt])); 292151497Sru } 293151497Sru 294151497Sru return 0; 295104862Sru} 296104862Sru 297151497Sru/* 298151497Sru * Decode the error codes returned by cgetent() using the names we 299151497Sru * made up for them from "lp.h". 300151497Sru * This would have been much better done with Common Error, >sigh<. 301151497Sru * Perhaps this can be fixed in the next incarnation of cget*. 302 */ 303const char * 304pcaperr(int error) 305{ 306 switch(error) { 307 case PCAPERR_TCOPEN: 308 return "unresolved tc= expansion"; 309 case PCAPERR_SUCCESS: 310 return "no error"; 311 case PCAPERR_NOTFOUND: 312 return "printer not found"; 313 case PCAPERR_OSERR: 314 return strerror(errno); 315 case PCAPERR_TCLOOP: 316 return "loop detected in tc= expansion"; 317 default: 318 return "unknown printcap error"; 319 } 320} 321 322/* 323 * Initialize a `struct printer' to contain values harmless to 324 * the other routines in liblpr. 325 */ 326void 327init_printer(struct printer *pp) 328{ 329 static struct printer zero; 330 *pp = zero; 331} 332 333/* 334 * Free the dynamically-allocated strings in a `struct printer'. 335 * Idempotent. 336 */ 337void 338free_printer(struct printer *pp) 339{ 340 enum lpd_filters filt; 341#define cfree(x) do { if (x) free(x); } while(0) 342 cfree(pp->printer); 343 cfree(pp->acct_file); 344 for (filt = 0; filt < LPF_COUNT; filt++) 345 cfree(pp->filters[filt]); 346 cfree(pp->form_feed); 347 cfree(pp->log_file); 348 cfree(pp->lock_file); 349 cfree(pp->lp); 350 cfree(pp->restrict_grp); 351 cfree(pp->remote_host); 352 cfree(pp->remote_queue); 353 cfree(pp->spool_dir); 354 cfree(pp->stat_recv); 355 cfree(pp->stat_send); 356 cfree(pp->status_file); 357 cfree(pp->trailer); 358 cfree(pp->mode_set); 359 360 init_printer(pp); 361} 362 363 364/* 365 * The following routines are part of what would be a sensible library 366 * interface to capability databases. Maybe someday this will become 367 * the default. 368 */ 369 370/* 371 * It provides similar functionality to cgetstr(), 372 * except that it provides for both a long and a short 373 * capability name and allows for a default to be specified. 374 */ 375static int 376capdb_getaltstr(char *bp, const char *shrt, const char *lng, 377 const char *dflt, char **result) 378{ 379 int status; 380 381 status = cgetstr(bp, (char *)/*XXX*/lng, result); 382 if (status >= 0 || status == PCAPERR_OSERR) 383 return status; 384 status = cgetstr(bp, (char *)/*XXX*/shrt, result); 385 if (status >= 0 || status == PCAPERR_OSERR) 386 return status; 387 if (dflt) { 388 *result = strdup(dflt); 389 if (*result == 0) 390 return PCAPERR_OSERR; 391 return strlen(*result); 392 } 393 return PCAPERR_NOTFOUND; 394} 395 396/* 397 * The same, only for integers. 398 */ 399static int 400capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt, 401 long *result) 402{ 403 int status; 404 405 status = cgetnum(bp, (char *)/*XXX*/lng, result); 406 if (status >= 0) 407 return status; 408 status = cgetnum(bp, (char *)/*XXX*/shrt, result); 409 if (status >= 0) 410 return status; 411 *result = dflt; 412 return 0; 413} 414 415/* 416 * Likewise for logical values. There's no need for a default parameter 417 * because the default is always false. 418 */ 419static int 420capdb_getaltlog(char *bp, const char *shrt, const char *lng) 421{ 422 if (cgetcap(bp, (char *)/*XXX*/lng, ':')) 423 return 1; 424 if (cgetcap(bp, (char *)/*XXX*/shrt, ':')) 425 return 1; 426 return 0; 427} 428 429/* 430 * Also should be a part of a better cget* library. 431 * Given a capdb entry, attempt to figure out what its canonical name 432 * is, and return a malloced copy of it. The canonical name is 433 * considered to be the first one listed. 434 */ 435static char * 436capdb_canonical_name(const char *bp) 437{ 438 char *retval; 439 const char *nameend; 440 441 nameend = strpbrk(bp, "|:"); 442 if (nameend == 0) 443 nameend = bp + 1; 444 if ((retval = malloc(nameend - bp + 1)) != 0) { 445 retval[0] = '\0'; 446 strncat(retval, bp, nameend - bp); 447 } 448 return retval; 449} 450 451 452