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. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#if 0
40#ifndef lint
41static char sccsid[] = "@(#)printcap.c	8.2 (Berkeley) 4/28/95";
42#endif /* not lint */
43#endif
44
45#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
46__FBSDID("$FreeBSD$");
47
48#include <errno.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#include <sys/param.h>		/* required for lp.h, but not used here */
55#include <sys/dirent.h>		/* ditto */
56#include "lp.h"
57#include "lp.local.h"
58#include "pathnames.h"
59
60/*
61 * Routines and data used in processing the printcap file.
62 */
63static char *printcapdb[2] = { _PATH_PRINTCAP, 0 };  /* list for cget* */
64
65static char 	*capdb_canonical_name(const char *_bp);
66static int	 capdb_getaltlog(char *_bp, const char *_shrt,
67		    const char *_lng);
68static int	 capdb_getaltnum(char *_bp, const char *_shrt,
69		    const char *_lng, long _dflt, long *_result);
70static int	 capdb_getaltstr(char *_bp, const char *_shrt,
71		    const char *lng, const char *_dflt, char **_result);
72static int	 getprintcap_int(char *_bp, struct printer *_pp);
73
74/*
75 * Change the name of the printcap file.  Used by chkprintcap(8),
76 * but could be used by other members of the suite with appropriate
77 * security measures.
78 */
79void
80setprintcap(char *newfile)
81{
82	printcapdb[0] = newfile;
83}
84
85/*
86 * Read the printcap database for printer `printer' into the
87 * struct printer pointed by `pp'.  Return values are as for
88 * cgetent(3): -1 means we could not find what we wanted, -2
89 * means a system error occurred (and errno is set), -3 if a
90 * reference (`tc=') loop was detected, and 0 means success.
91 *
92 * Copied from lpr; should add additional capabilities as they
93 * are required by the other programs in the suite so that
94 * printcap-reading is consistent across the entire family.
95 */
96int
97getprintcap(const char *printer, struct printer *pp)
98{
99	int status;
100	char *XXX;
101	char *bp;
102
103	/*
104	 * A bug in the declaration of cgetent(3) means that we have
105	 * to hide the constness of its third argument.
106	 */
107	XXX = (char *)printer;
108	if ((status = cgetent(&bp, printcapdb, XXX)) < 0)
109		return status;
110	status = getprintcap_int(bp, pp);
111	free(bp);
112	return status;
113}
114
115/*
116 * Map the status values returned by cgetfirst/cgetnext into those
117 * used by cgetent, returning truth if there are more records to
118 * examine.  This points out what is arguably a bug in the cget*
119 * interface (or at least a nasty wart).
120 */
121static int
122firstnextmap(int *status)
123{
124	switch (*status) {
125	case 0:
126		return 0;
127	case 1:
128		*status = 0;
129		return 1;
130	case 2:
131		*status = 1;
132		return 1;
133	case -1:
134		*status = -2;
135		return 0;
136	case -2:
137		*status = -3;
138		return 1;
139	default:
140		return 0;
141	}
142}
143
144/*
145 * Scan through the database of printers using cgetfirst/cgetnext.
146 * Return false of error or end-of-database; else true.
147 */
148int
149firstprinter(struct printer *pp, int *error)
150{
151	int status;
152	char *bp;
153
154	init_printer(pp);
155	status = cgetfirst(&bp, printcapdb);
156	if (firstnextmap(&status) == 0) {
157		if (error)
158			*error = status;
159		return 0;
160	}
161	if (error)
162		*error = status;
163	status = getprintcap_int(bp, pp);
164	free(bp);
165	if (error && status)
166		*error = status;
167	return 1;
168}
169
170int
171nextprinter(struct printer *pp, int *error)
172{
173	int status;
174	char *bp;
175
176	free_printer(pp);
177	status = cgetnext(&bp, printcapdb);
178	if (firstnextmap(&status) == 0) {
179		if (error)
180			*error = status;
181		return 0;
182	}
183	if (error)
184		*error = status;
185	status = getprintcap_int(bp, pp);
186	free(bp);
187	if (error && status)
188		*error = status;
189	return 1;
190}
191
192void
193lastprinter(void)
194{
195	cgetclose();
196}
197
198/*
199 * This must match the order of declaration of enum filter in lp.h.
200 */
201static const char *filters[] = {
202	"cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf"
203};
204
205static const char *longfilters[] = {
206	"filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff",
207	"filt.output", "filt.fortran", "filt.troff", "filt.raster"
208};
209
210/*
211 * Internal routine for both getprintcap() and nextprinter().
212 * Actually parse the printcap entry using cget* functions.
213 * Also attempt to figure out the canonical name of the printer
214 * and store a malloced copy of it in pp->printer.
215 */
216static int
217getprintcap_int(char *bp, struct printer *pp)
218{
219	enum lpd_filters filt;
220	char *rp_name;
221	int error;
222
223	if ((pp->printer = capdb_canonical_name(bp)) == 0)
224		return PCAPERR_OSERR;
225
226#define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
227	CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
228	CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
229	CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT,
230			    &pp->conn_timeout));
231	CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID,
232			    &pp->daemon_user));
233	CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
234	CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE,
235			    &pp->log_file));
236	CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
237	CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
238	CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES,
239			    &pp->max_copies));
240	CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
241	CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
242	CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
243	CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
244			    &pp->page_length));
245	CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH,
246			    &pp->page_width));
247	CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
248	CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
249	CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0,
250			    &pp->restrict_grp));
251	CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
252	CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP,
253			    &pp->remote_queue));
254	CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
255			    &pp->spool_dir));
256	CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv));
257	CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send));
258	CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
259			    &pp->status_file));
260	CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));
261
262	pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies");
263	pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
264	pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
265	pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
266	pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
267	pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
268	pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
269	pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
270	pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
271
272	/*
273	 * Decide if the remote printer name matches the local printer name.
274	 * If no name is given then we assume they mean them to match.
275	 * If a name is given see if the rp_name is one of the names for
276	 * this printer.
277	 */
278	pp->rp_matches_local = 1;
279	CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name)));
280	if (error != PCAPERR_NOTFOUND && rp_name != NULL) {
281		if (cgetmatch(bp,rp_name) != 0)
282			pp->rp_matches_local = 0;
283		free(rp_name);
284	}
285
286	/*
287	 * Filters:
288	 */
289	for (filt = 0; filt < LPF_COUNT; filt++) {
290		CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
291				    &pp->filters[filt]));
292	}
293
294	return 0;
295}
296
297/*
298 * Decode the error codes returned by cgetent() using the names we
299 * made up for them from "lp.h".
300 * This would have been much better done with Common Error, >sigh<.
301 * 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