xref: /freebsd/usr.sbin/lpr/common_source/printcap.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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[] = { __DECONST(char *, _PATH_PRINTCAP), NULL };
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 *bp;
103 
104 	if ((status = cgetent(&bp, printcapdb, printer)) < 0)
105 		return status;
106 	status = getprintcap_int(bp, pp);
107 	free(bp);
108 	return status;
109 }
110 
111 /*
112  * Map the status values returned by cgetfirst/cgetnext into those
113  * used by cgetent, returning truth if there are more records to
114  * examine.  This points out what is arguably a bug in the cget*
115  * interface (or at least a nasty wart).
116  */
117 static int
118 firstnextmap(int *status)
119 {
120 	switch (*status) {
121 	case 0:
122 		return 0;
123 	case 1:
124 		*status = 0;
125 		return 1;
126 	case 2:
127 		*status = 1;
128 		return 1;
129 	case -1:
130 		*status = -2;
131 		return 0;
132 	case -2:
133 		*status = -3;
134 		return 1;
135 	default:
136 		return 0;
137 	}
138 }
139 
140 /*
141  * Scan through the database of printers using cgetfirst/cgetnext.
142  * Return false of error or end-of-database; else true.
143  */
144 int
145 firstprinter(struct printer *pp, int *error)
146 {
147 	int status;
148 	char *bp;
149 
150 	init_printer(pp);
151 	status = cgetfirst(&bp, printcapdb);
152 	if (firstnextmap(&status) == 0) {
153 		if (error)
154 			*error = status;
155 		return 0;
156 	}
157 	if (error)
158 		*error = status;
159 	status = getprintcap_int(bp, pp);
160 	free(bp);
161 	if (error && status)
162 		*error = status;
163 	return 1;
164 }
165 
166 int
167 nextprinter(struct printer *pp, int *error)
168 {
169 	int status;
170 	char *bp;
171 
172 	free_printer(pp);
173 	status = cgetnext(&bp, printcapdb);
174 	if (firstnextmap(&status) == 0) {
175 		if (error)
176 			*error = status;
177 		return 0;
178 	}
179 	if (error)
180 		*error = status;
181 	status = getprintcap_int(bp, pp);
182 	free(bp);
183 	if (error && status)
184 		*error = status;
185 	return 1;
186 }
187 
188 void
189 lastprinter(void)
190 {
191 	cgetclose();
192 }
193 
194 /*
195  * This must match the order of declaration of enum filter in lp.h.
196  */
197 static const char *filters[] = {
198 	"cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf"
199 };
200 
201 static const char *longfilters[] = {
202 	"filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff",
203 	"filt.output", "filt.fortran", "filt.troff", "filt.raster"
204 };
205 
206 /*
207  * Internal routine for both getprintcap() and nextprinter().
208  * Actually parse the printcap entry using cget* functions.
209  * Also attempt to figure out the canonical name of the printer
210  * and store a malloced copy of it in pp->printer.
211  */
212 static int
213 getprintcap_int(char *bp, struct printer *pp)
214 {
215 	enum lpd_filters filt;
216 	char *rp_name;
217 	int error;
218 
219 	if ((pp->printer = capdb_canonical_name(bp)) == NULL)
220 		return PCAPERR_OSERR;
221 
222 #define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
223 	CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
224 	CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
225 	CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT,
226 			    &pp->conn_timeout));
227 	CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID,
228 			    &pp->daemon_user));
229 	CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
230 	CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE,
231 			    &pp->log_file));
232 	CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
233 	CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
234 	CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES,
235 			    &pp->max_copies));
236 	CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
237 	CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
238 	CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
239 	CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
240 			    &pp->page_length));
241 	CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH,
242 			    &pp->page_width));
243 	CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
244 	CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
245 	CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0,
246 			    &pp->restrict_grp));
247 	CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
248 	CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP,
249 			    &pp->remote_queue));
250 	CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
251 			    &pp->spool_dir));
252 	CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv));
253 	CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send));
254 	CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
255 			    &pp->status_file));
256 	CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));
257 
258 	pp->resend_copies = capdb_getaltlog(bp, "rc", "remote.resend_copies");
259 	pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
260 	pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
261 	pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
262 	pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
263 	pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
264 	pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
265 	pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
266 	pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
267 
268 	/*
269 	 * Decide if the remote printer name matches the local printer name.
270 	 * If no name is given then we assume they mean them to match.
271 	 * If a name is given see if the rp_name is one of the names for
272 	 * this printer.
273 	 */
274 	pp->rp_matches_local = 1;
275 	CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name)));
276 	if (error != PCAPERR_NOTFOUND && rp_name != NULL) {
277 		if (cgetmatch(bp,rp_name) != 0)
278 			pp->rp_matches_local = 0;
279 		free(rp_name);
280 	}
281 
282 	/*
283 	 * Filters:
284 	 */
285 	for (filt = 0; filt < LPF_COUNT; filt++) {
286 		CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
287 				    &pp->filters[filt]));
288 	}
289 
290 	return 0;
291 }
292 
293 /*
294  * Decode the error codes returned by cgetent() using the names we
295  * made up for them from "lp.h".
296  * This would have been much better done with Common Error, >sigh<.
297  * Perhaps this can be fixed in the next incarnation of cget*.
298  */
299 const char *
300 pcaperr(int error)
301 {
302 	switch(error) {
303 	case PCAPERR_TCOPEN:
304 		return "unresolved tc= expansion";
305 	case PCAPERR_SUCCESS:
306 		return "no error";
307 	case PCAPERR_NOTFOUND:
308 		return "printer not found";
309 	case PCAPERR_OSERR:
310 		return strerror(errno);
311 	case PCAPERR_TCLOOP:
312 		return "loop detected in tc= expansion";
313 	default:
314 		return "unknown printcap error";
315 	}
316 }
317 
318 /*
319  * Initialize a `struct printer' to contain values harmless to
320  * the other routines in liblpr.
321  */
322 void
323 init_printer(struct printer *pp)
324 {
325 	static struct printer zero;
326 	*pp = zero;
327 }
328 
329 /*
330  * Free the dynamically-allocated strings in a `struct printer'.
331  * Idempotent.
332  */
333 void
334 free_printer(struct printer *pp)
335 {
336 	enum lpd_filters filt;
337 #define	cfree(x)	do { if (x) free(x); } while(0)
338 	cfree(pp->printer);
339 	cfree(pp->acct_file);
340 	for (filt = 0; filt < LPF_COUNT; filt++)
341 		cfree(pp->filters[filt]);
342 	cfree(pp->form_feed);
343 	cfree(pp->log_file);
344 	cfree(pp->lock_file);
345 	cfree(pp->lp);
346 	cfree(pp->restrict_grp);
347 	cfree(pp->remote_host);
348 	cfree(pp->remote_queue);
349 	cfree(pp->spool_dir);
350 	cfree(pp->stat_recv);
351 	cfree(pp->stat_send);
352 	cfree(pp->status_file);
353 	cfree(pp->trailer);
354 	cfree(pp->mode_set);
355 
356 	init_printer(pp);
357 }
358 
359 
360 /*
361  * The following routines are part of what would be a sensible library
362  * interface to capability databases.  Maybe someday this will become
363  * the default.
364  */
365 
366 /*
367  * It provides similar functionality to cgetstr(),
368  * except that it provides for both a long and a short
369  * capability name and allows for a default to be specified.
370  */
371 static int
372 capdb_getaltstr(char *bp, const char *shrt, const char *lng,
373     const char *dflt, char **result)
374 {
375 	int status;
376 
377 	status = cgetstr(bp, lng, result);
378 	if (status >= 0 || status == PCAPERR_OSERR)
379 		return status;
380 	status = cgetstr(bp, shrt, result);
381 	if (status >= 0 || status == PCAPERR_OSERR)
382 		return status;
383 	if (dflt) {
384 		*result = strdup(dflt);
385 		if (*result == NULL)
386 			return PCAPERR_OSERR;
387 		return strlen(*result);
388 	}
389 	return PCAPERR_NOTFOUND;
390 }
391 
392 /*
393  * The same, only for integers.
394  */
395 static int
396 capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt,
397     long *result)
398 {
399 	int status;
400 
401 	status = cgetnum(bp, lng, result);
402 	if (status >= 0)
403 		return status;
404 	status = cgetnum(bp, shrt, result);
405 	if (status >= 0)
406 		return status;
407 	*result = dflt;
408 	return 0;
409 }
410 
411 /*
412  * Likewise for logical values.  There's no need for a default parameter
413  * because the default is always false.
414  */
415 static int
416 capdb_getaltlog(char *bp, const char *shrt, const char *lng)
417 {
418 	if (cgetcap(bp, lng, ':'))
419 		return 1;
420 	if (cgetcap(bp, shrt, ':'))
421 		return 1;
422 	return 0;
423 }
424 
425 /*
426  * Also should be a part of a better cget* library.
427  * Given a capdb entry, attempt to figure out what its canonical name
428  * is, and return a malloced copy of it.  The canonical name is
429  * considered to be the first one listed.
430  */
431 static char *
432 capdb_canonical_name(const char *bp)
433 {
434 	char *retval;
435 	const char *nameend;
436 
437 	nameend = strpbrk(bp, "|:");
438 	if (nameend == NULL)
439 		nameend = bp + 1;
440 	if ((retval = malloc(nameend - bp + 1)) != NULL) {
441 		retval[0] = '\0';
442 		strncat(retval, bp, nameend - bp);
443 	}
444 	return retval;
445 }
446 
447 
448