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