xref: /freebsd/usr.sbin/lpr/common_source/printcap.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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  * 	From: @(#)common.c	8.5 (Berkeley) 4/28/95
39  */
40 
41 #ifndef lint
42 static const char rcsid[] =
43   "$FreeBSD$";
44 #endif /* not lint */
45 
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include <sys/param.h>		/* required for lp.h, but not used here */
53 #include <sys/dirent.h>		/* ditto */
54 #include "lp.h"
55 #include "lp.local.h"
56 #include "pathnames.h"
57 
58 /*
59  * Routines and data used in processing the printcap file.
60  */
61 static 	char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; /* list for cget* */
62 
63 static	char *capdb_canonical_name(const char *);
64 static	int capdb_getaltlog(char *, const char *, const char *);
65 static	int capdb_getaltnum(char *, const char *, const char *, long, long *);
66 static	int capdb_getaltstr(char *, const char *, const char *, const char *,
67 			    char **);
68 static	int getprintcap_int(char *bp, struct printer *pp);
69 
70 /*
71  * Change the name of the printcap file.  Used by chkprintcap(8),
72  * but could be used by other members of the suite with appropriate
73  * security measures.
74  */
75 void
76 setprintcap(char *newfile)
77 {
78 	printcapdb[0] = newfile;
79 }
80 
81 /*
82  * Read the printcap database for printer `printer' into the
83  * struct printer pointed by `pp'.  Return values are as for
84  * cgetent(3): -1 means we could not find what we wanted, -2
85  * means a system error occurred (and errno is set), -3 if a
86  * reference (`tc=') loop was detected, and 0 means success.
87  *
88  * Copied from lpr; should add additional capabilities as they
89  * are required by the other programs in the suite so that
90  * printcap-reading is consistent across the entire family.
91  */
92 int
93 getprintcap(const char *printer, struct printer *pp)
94 {
95 	int status;
96 	char *XXX;
97 	char *bp;
98 
99 	/*
100 	 * A bug in the declaration of cgetent(3) means that we have
101 	 * to hide the constness of its third argument.
102 	 */
103 	XXX = (char *)printer;
104 	if ((status = cgetent(&bp, printcapdb, XXX)) < 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(bp, pp)
214 	char *bp;
215 	struct printer *pp;
216 {
217 	enum lpd_filters filt;
218 	char *rp_name;
219 	int error;
220 
221 	if ((pp->printer = capdb_canonical_name(bp)) == 0)
222 		return PCAPERR_OSERR;
223 
224 #define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
225 	CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
226 	CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
227 	CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT,
228 			    &pp->conn_timeout));
229 	CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID,
230 			    &pp->daemon_user));
231 	CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
232 	CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE,
233 			    &pp->log_file));
234 	CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
235 	CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
236 	CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES,
237 			    &pp->max_copies));
238 	CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
239 	CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
240 	CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
241 	CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
242 			    &pp->page_length));
243 	CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH,
244 			    &pp->page_width));
245 	CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
246 	CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
247 	CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0,
248 			    &pp->restrict_grp));
249 	CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
250 	CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP,
251 			    &pp->remote_queue));
252 	CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
253 			    &pp->spool_dir));
254 	CHK(capdb_getaltstr(bp, "sr", "stat.recv", 0, &pp->stat_recv));
255 	CHK(capdb_getaltstr(bp, "ss", "stat.send", 0, &pp->stat_send));
256 	CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
257 			    &pp->status_file));
258 	CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));
259 
260 	pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
261 	pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
262 	pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
263 	pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
264 	pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
265 	pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
266 	pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
267 	pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
268 
269 	/*
270 	 * Decide if the remote printer name matches the local printer name.
271 	 * If no name is given then we assume they mean them to match.
272 	 * If a name is given see if the rp_name is one of the names for
273 	 * this printer.
274 	 */
275 	pp->rp_matches_local = 1;
276 	CHK((error = capdb_getaltstr(bp, "rp", "remote.queue", 0, &rp_name)));
277 	if (error != PCAPERR_NOTFOUND && rp_name != NULL) {
278 		if (cgetmatch(bp,rp_name) != 0)
279 			pp->rp_matches_local = 0;
280 		free(rp_name);
281 	}
282 
283 	/*
284 	 * Filters:
285 	 */
286 	for (filt = 0; filt < LPF_COUNT; filt++) {
287 		CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
288 				    &pp->filters[filt]));
289 	}
290 
291 	return 0;
292 }
293 
294 /*
295  * Decode the error codes returned by cgetent() using the names we
296  * made up for them from "lp.h".
297  * This would have been much better done with Common Error, >sigh<.
298  * Perhaps this can be fixed in the next incarnation of cget*.
299  */
300 const char *
301 pcaperr(int error)
302 {
303 	switch(error) {
304 	case PCAPERR_TCOPEN:
305 		return "unresolved tc= expansion";
306 	case PCAPERR_SUCCESS:
307 		return "no error";
308 	case PCAPERR_NOTFOUND:
309 		return "printer not found";
310 	case PCAPERR_OSERR:
311 		return strerror(errno);
312 	case PCAPERR_TCLOOP:
313 		return "loop detected in tc= expansion";
314 	default:
315 		return "unknown printcap error";
316 	}
317 }
318 
319 /*
320  * Initialize a `struct printer' to contain values harmless to
321  * the other routines in liblpr.
322  */
323 void
324 init_printer(struct printer *pp)
325 {
326 	static struct printer zero;
327 	*pp = zero;
328 }
329 
330 /*
331  * Free the dynamically-allocated strings in a `struct printer'.
332  * Idempotent.
333  */
334 void
335 free_printer(struct printer *pp)
336 {
337 	enum lpd_filters filt;
338 #define	cfree(x)	do { if (x) free(x); } while(0)
339 	cfree(pp->printer);
340 	cfree(pp->acct_file);
341 	for (filt = 0; filt < LPF_COUNT; filt++)
342 		cfree(pp->filters[filt]);
343 	cfree(pp->form_feed);
344 	cfree(pp->log_file);
345 	cfree(pp->lock_file);
346 	cfree(pp->lp);
347 	cfree(pp->restrict_grp);
348 	cfree(pp->remote_host);
349 	cfree(pp->remote_queue);
350 	cfree(pp->spool_dir);
351 	cfree(pp->stat_recv);
352 	cfree(pp->stat_send);
353 	cfree(pp->status_file);
354 	cfree(pp->trailer);
355 	cfree(pp->mode_set);
356 
357 	init_printer(pp);
358 }
359 
360 
361 /*
362  * The following routines are part of what would be a sensible library
363  * interface to capability databases.  Maybe someday this will become
364  * the default.
365  */
366 
367 /*
368  * It provides similar functionality to cgetstr(),
369  * except that it provides for both a long and a short
370  * capability name and allows for a default to be specified.
371  */
372 static int
373 capdb_getaltstr(char *bp, const char *shrt, const char *lng,
374 		const char *dflt, char **result)
375 {
376 	int status;
377 
378 	status = cgetstr(bp, (char *)/*XXX*/lng, result);
379 	if (status >= 0 || status == PCAPERR_OSERR)
380 		return status;
381 	status = cgetstr(bp, (char *)/*XXX*/shrt, result);
382 	if (status >= 0 || status == PCAPERR_OSERR)
383 		return status;
384 	if (dflt) {
385 		*result = strdup(dflt);
386 		if (*result == 0)
387 			return PCAPERR_OSERR;
388 		return strlen(*result);
389 	}
390 	return PCAPERR_NOTFOUND;
391 }
392 
393 /*
394  * The same, only for integers.
395  */
396 static int
397 capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt,
398 		long *result)
399 {
400 	int status;
401 
402 	status = cgetnum(bp, (char *)/*XXX*/lng, result);
403 	if (status >= 0)
404 		return status;
405 	status = cgetnum(bp, (char *)/*XXX*/shrt, result);
406 	if (status >= 0)
407 		return status;
408 	*result = dflt;
409 	return 0;
410 }
411 
412 /*
413  * Likewise for logical values.  There's no need for a default parameter
414  * because the default is always false.
415  */
416 static int
417 capdb_getaltlog(char *bp, const char *shrt, const char *lng)
418 {
419 	if (cgetcap(bp, (char *)/*XXX*/lng, ':'))
420 		return 1;
421 	if (cgetcap(bp, (char *)/*XXX*/shrt, ':'))
422 		return 1;
423 	return 0;
424 }
425 
426 /*
427  * Also should be a part of a better cget* library.
428  * Given a capdb entry, attempt to figure out what its canonical name
429  * is, and return a malloced copy of it.  The canonical name is
430  * considered to be the first one listed.
431  */
432 static char *
433 capdb_canonical_name(const char *bp)
434 {
435 	char *retval;
436 	const char *nameend;
437 
438 	nameend = strpbrk(bp, "|:");
439 	if (nameend == 0)
440 		nameend = bp + 1;
441 	if ((retval = malloc(nameend - bp + 1)) != 0) {
442 		retval[0] = '\0';
443 		strncat(retval, bp, nameend - bp);
444 	}
445 	return retval;
446 }
447 
448 
449