summaryrefslogtreecommitdiffstats
path: root/usr.sbin/lpr/common_source/printcap.c
blob: a061c0ef82d9ab9217a4657c6aa085e6130de359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright (c) 1983, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * 	From: @(#)common.c	8.5 (Berkeley) 4/28/95
 */

#ifndef lint
static const char rcsid[] =
	"$Id: printcap.c,v 1.3 1997/12/02 20:45:25 wollman Exp $";
#endif /* not lint */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/param.h>		/* required for lp.h, but not used here */
#include <sys/dirent.h>		/* ditto */
#include "lp.h"
#include "lp.local.h"
#include "pathnames.h"

/*
 * Routines and data used in processing the printcap file.
 */
static 	char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; /* list for cget* */

static	char *capdb_canonical_name(const char *);
static	int capdb_getaltlog(char *, const char *, const char *);
static	int capdb_getaltnum(char *, const char *, const char *, long, long *);
static	int capdb_getaltstr(char *, const char *, const char *, const char *,
			    char **);
static	int getprintcap_int(char *bp, struct printer *pp);

/*
 * Change the name of the printcap file.  Used by chkprintcap(8),
 * but could be used by other members of the suite with appropriate
 * security measures.
 */
void
setprintcap(char *newfile)
{
	printcapdb[0] = newfile;
}

/*
 * Read the printcap database for printer `printer' into the
 * struct printer pointed by `pp'.  Return values are as for
 * cgetent(3): -1 means we could not find what we wanted, -2
 * means a system error occurred (and errno is set), -3 if a
 * reference (`tc=') loop was detected, and 0 means success.
 *
 * Copied from lpr; should add additional capabilities as they
 * are required by the other programs in the suite so that
 * printcap-reading is consistent across the entire family.
 */
int
getprintcap(const char *printer, struct printer *pp)
{
	int status;
	char *XXX;
	char *bp;

	/*
	 * A bug in the declaration of cgetent(3) means that we have
	 * to hide the constness of its third argument.
	 */
	XXX = (char *)printer;
	if ((status = cgetent(&bp, printcapdb, XXX)) < 0)
		return status;
	status = getprintcap_int(bp, pp);
	free(bp);
	return status;
}

/*
 * Map the status values returned by cgetfirst/cgetnext into those
 * used by cgetent, returning truth if there are more records to
 * examine.  This points out what is arguably a bug in the cget*
 * interface (or at least a nasty wart).
 */
static int
firstnextmap(int *status)
{
	switch (*status) {
	case 0:
		return 0;
	case 1:
		*status = 0;
		return 1;
	case 2:
		*status = 1;
		return 1;
	case -1:
		*status = -2;
		return 0;
	case -2:
		*status = -3;
		return 1;
	default:
		return 0;
	}
}

/*
 * Scan through the database of printers using cgetfirst/cgetnext.
 * Return false of error or end-of-database; else true.
 */
int
firstprinter(struct printer *pp, int *error)
{
	int status;
	char *bp;

	init_printer(pp);
	status = cgetfirst(&bp, printcapdb);
	if (firstnextmap(&status) == 0) {
		if (error)
			*error = status;
		return 0;
	}
	if (error)
		*error = status;
	status = getprintcap_int(bp, pp);
	free(bp);
	if (error && status)
		*error = status;
	return 1;
}

int
nextprinter(struct printer *pp, int *error)
{
	int status;
	char *bp;

	free_printer(pp);
	status = cgetnext(&bp, printcapdb);
	if (firstnextmap(&status) == 0) {
		if (error)
			*error = status;
		return 0;
	}
	if (error)
		*error = status;
	status = getprintcap_int(bp, pp);
	free(bp);
	if (error && status)
		*error = status;
	return 1;
}

void
lastprinter(void)
{
	cgetclose();
}

/*
 * This must match the order of declaration of enum filter in lp.h.
 */
static const char *filters[] = {
	"cf", "df", "gf", "if", "nf", "of", "rf", "tf", "vf"
};

static const char *longfilters[] = {
	"filt.cifplot", "filt.dvi", "filt.plot", "filt.input", "filt.ditroff",
	"filt.output", "filt.fortran", "filt.troff", "filt.raster"
};

/*
 * Internal routine for both getprintcap() and nextprinter().
 * Actually parse the printcap entry using cget* functions.
 * Also attempt to figure out the canonical name of the printer
 * and store a malloced copy of it in pp->printer.
 */
static int
getprintcap_int(bp, pp)
	char *bp;
	struct printer *pp;
{
	enum lpd_filters filt;

	if ((pp->printer = capdb_canonical_name(bp)) == 0)
		return PCAPERR_OSERR;

#define CHK(x) do {if ((x) == PCAPERR_OSERR) return PCAPERR_OSERR;}while(0)
	CHK(capdb_getaltstr(bp, "af", "acct.file", 0, &pp->acct_file));
	CHK(capdb_getaltnum(bp, "br", "tty.rate", 0, &pp->baud_rate));
	CHK(capdb_getaltnum(bp, "ct", "remote.timeout", DEFTIMEOUT, 
			    &pp->conn_timeout));
	CHK(capdb_getaltnum(bp, "du", "daemon.user", DEFUID, 
			    &pp->daemon_user));
	CHK(capdb_getaltstr(bp, "ff", "job.formfeed", DEFFF, &pp->form_feed));
	CHK(capdb_getaltstr(bp, "lf", "spool.log", _PATH_CONSOLE, 
			    &pp->log_file));
	CHK(capdb_getaltstr(bp, "lo", "spool.lock", DEFLOCK, &pp->lock_file));
	CHK(capdb_getaltstr(bp, "lp", "tty.device", _PATH_DEFDEVLP, &pp->lp));
	CHK(capdb_getaltnum(bp, "mc", "max.copies", DEFMAXCOPIES, 
			    &pp->max_copies));
	CHK(capdb_getaltstr(bp, "ms", "tty.mode", 0, &pp->mode_set));
	CHK(capdb_getaltnum(bp, "mx", "max.blocks", DEFMX, &pp->max_blocks));
	CHK(capdb_getaltnum(bp, "pc", "acct.price", 0, &pp->price100));
	CHK(capdb_getaltnum(bp, "pl", "page.length", DEFLENGTH,
			    &pp->page_length));
	CHK(capdb_getaltnum(bp, "pw", "page.width", DEFWIDTH, 
			    &pp->page_width));
	CHK(capdb_getaltnum(bp, "px", "page.pwidth", 0, &pp->page_pwidth));
	CHK(capdb_getaltnum(bp, "py", "page.plength", 0, &pp->page_plength));
	CHK(capdb_getaltstr(bp, "rg", "daemon.restrictgrp", 0, 
			    &pp->restrict_grp));
	CHK(capdb_getaltstr(bp, "rm", "remote.host", 0, &pp->remote_host));
	CHK(capdb_getaltstr(bp, "rp", "remote.queue", DEFLP, 
			    &pp->remote_queue));
	CHK(capdb_getaltstr(bp, "sd", "spool.dir", _PATH_DEFSPOOL,
			    &pp->spool_dir));
	CHK(capdb_getaltstr(bp, "st", "spool.status", DEFSTAT,
			    &pp->status_file));
	CHK(capdb_getaltstr(bp, "tr", "job.trailer", 0, &pp->trailer));

	pp->restricted = capdb_getaltlog(bp, "rs", "daemon.restricted");
	pp->short_banner = capdb_getaltlog(bp, "sb", "banner.short");
	pp->no_copies = capdb_getaltlog(bp, "sc", "job.no_copies");
	pp->no_formfeed = capdb_getaltlog(bp, "sf", "job.no_formfeed");
	pp->no_header = capdb_getaltlog(bp, "sh", "banner.disable");
	pp->header_last = capdb_getaltlog(bp, "hl", "banner.last");
	pp->rw = capdb_getaltlog(bp, "rw", "tty.rw");
	pp->tof = !capdb_getaltlog(bp, "fo", "job.topofform");
	
	/*
	 * Filters:
	 */
	for (filt = 0; filt < LPF_COUNT; filt++) {
		CHK(capdb_getaltstr(bp, filters[filt], longfilters[filt], 0,
				    &pp->filters[filt]));
	}

	return 0;
}

/*
 * Decode the error codes returned by cgetent() using the names we
 * made up for them from "lp.h".
 * This would have been much better done with Common Error, >sigh<.
 * Perhaps this can be fixed in the next incarnation of cget*.
 */
const char *
pcaperr(int error)
{
	switch(error) {
	case PCAPERR_TCOPEN:
		return "unresolved tc= expansion";
	case PCAPERR_SUCCESS:
		return "no error";
	case PCAPERR_NOTFOUND:
		return "printer not found";
	case PCAPERR_OSERR:
		return strerror(errno);
	case PCAPERR_TCLOOP:
		return "loop detected in tc= expansion";
	default:
		return "unknown printcap error";
	}
}

/*
 * Initialize a `struct printer' to contain values harmless to
 * the other routines in liblpr.
 */
void
init_printer(struct printer *pp)
{
	static struct printer zero;
	*pp = zero;
}

/*
 * Free the dynamically-allocated strings in a `struct printer'.
 * Idempotent.
 */
void
free_printer(struct printer *pp)
{
	enum lpd_filters filt;
#define	cfree(x)	do { if (x) free(x); } while(0)
	cfree(pp->printer);
	cfree(pp->acct_file);
	for (filt = 0; filt < LPF_COUNT; filt++)
		cfree(pp->filters[filt]);
	cfree(pp->form_feed);
	cfree(pp->log_file);
	cfree(pp->lock_file);
	cfree(pp->lp);
	cfree(pp->restrict_grp);
	cfree(pp->remote_host);
	cfree(pp->remote_queue);
	cfree(pp->spool_dir);
	cfree(pp->status_file);
	cfree(pp->trailer);
	cfree(pp->mode_set);

	init_printer(pp);
}


/* 
 * The following routines are part of what would be a sensible library 
 * interface to capability databases.  Maybe someday this will become
 * the default.
 */

/*
 * It provides similar functionality to cgetstr(),
 * except that it provides for both a long and a short
 * capability name and allows for a default to be specified.
 */
static int
capdb_getaltstr(char *bp, const char *shrt, const char *lng, 
		const char *dflt, char **result)
{
	int status;

	status = cgetstr(bp, (char *)/*XXX*/lng, result);
	if (status >= 0 || status == PCAPERR_OSERR)
		return status;
	status = cgetstr(bp, (char *)/*XXX*/shrt, result);
	if (status >= 0 || status == PCAPERR_OSERR)
		return status;
	if (dflt) {
		*result = strdup(dflt);
		if (*result == 0)
			return PCAPERR_OSERR;
		return strlen(*result);
	}
	return PCAPERR_NOTFOUND;
}

/*
 * The same, only for integers.
 */
static int
capdb_getaltnum(char *bp, const char *shrt, const char *lng, long dflt,
		long *result)
{
	int status;

	status = cgetnum(bp, (char *)/*XXX*/lng, result);
	if (status >= 0)
		return status;
	status = cgetnum(bp, (char *)/*XXX*/shrt, result);
	if (status >= 0)
		return status;
	*result = dflt;
	return 0;
}	

/*
 * Likewise for logical values.  There's no need for a default parameter
 * because the default is always false.
 */
static int
capdb_getaltlog(char *bp, const char *shrt, const char *lng)
{
	if (cgetcap(bp, (char *)/*XXX*/lng, ':'))
		return 1;
	if (cgetcap(bp, (char *)/*XXX*/shrt, ':'))
		return 1;
	return 0;
}

/*
 * Also should be a part of a better cget* library.
 * Given a capdb entry, attempt to figure out what its canonical name
 * is, and return a malloced copy of it.  The canonical name is
 * considered to be the first one listed.
 */
static char *
capdb_canonical_name(const char *bp)
{
	char *retval;	
	const char *nameend;

	nameend = strpbrk(bp, "|:");
	if (nameend == 0)
		nameend = bp + 1;
	if ((retval = malloc(nameend - bp + 1)) != 0) {
		retval[0] = '\0';
		strncat(retval, bp, nameend - bp);
	}
	return retval;
}


OpenPOWER on IntegriCloud