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