xref: /titanic_52/usr/src/lib/print/libprint/common/nss_printer.c (revision fe1de1a1b0d461787bdf93afeab869604e2e6205)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs /*
22355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23355b4669Sjacobs  * Use is subject to license terms.
24355b4669Sjacobs  */
25355b4669Sjacobs 
26355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
27355b4669Sjacobs 
28355b4669Sjacobs #include <stdio.h>
29355b4669Sjacobs #include <string.h>
30355b4669Sjacobs #include <ctype.h>
31355b4669Sjacobs #include <sys/types.h>
32355b4669Sjacobs #include <nss_dbdefs.h>
33355b4669Sjacobs #include <syslog.h>
34355b4669Sjacobs #include <ns.h>
35355b4669Sjacobs 
36355b4669Sjacobs #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
37355b4669Sjacobs #define	NSS_DBNAM__PRINTERS	"_printers"
38355b4669Sjacobs #endif
39355b4669Sjacobs 
40355b4669Sjacobs static DEFINE_NSS_DB_ROOT(db_root);
41355b4669Sjacobs static DEFINE_NSS_GETENT(context);
42355b4669Sjacobs 
43355b4669Sjacobs static int printers_stayopen;
44355b4669Sjacobs static char *private_ns = NULL;
45355b4669Sjacobs 
46355b4669Sjacobs static void
47355b4669Sjacobs _nss_initf_printers(p)
48355b4669Sjacobs     nss_db_params_t *p;
49355b4669Sjacobs {
50355b4669Sjacobs 	if (private_ns != NULL) {
51355b4669Sjacobs 		/*
52355b4669Sjacobs 		 * because we need to support a legacy interface that allows
53355b4669Sjacobs 		 * us to select a specific name service, we need to dummy up
54355b4669Sjacobs 		 * the parameters to use a private nsswitch database and set
55355b4669Sjacobs 		 * the * default_config entry to the name service we are
56355b4669Sjacobs 		 * looking into.
57355b4669Sjacobs 		 */
58355b4669Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
59355b4669Sjacobs 		p->default_config = normalize_ns_name(private_ns);
60*fe1de1a1Sjacobs 	} else {
61355b4669Sjacobs 		/* regular behaviour */
62355b4669Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
63355b4669Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
64355b4669Sjacobs 	}
65*fe1de1a1Sjacobs 	syslog(LOG_DEBUG, "database: %s, default: %s",
66355b4669Sjacobs 		(p->name ? p->name : "NULL"),
67355b4669Sjacobs 		(p->default_config ? p->default_config : "NULL"));
68355b4669Sjacobs }
69355b4669Sjacobs 
70355b4669Sjacobs /*
71355b4669Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
72355b4669Sjacobs  * The structure pointer passed in is a structure in the caller's space
73355b4669Sjacobs  * wherein the field pointers would be set to areas in the buffer if
74355b4669Sjacobs  * need be. instring and buffer should be separate areas.
75355b4669Sjacobs  */
76355b4669Sjacobs /* ARGSUSED */
77355b4669Sjacobs static int
78355b4669Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
79355b4669Sjacobs {
80355b4669Sjacobs 	if (lenstr + 1 > buflen)
81355b4669Sjacobs 		return (NSS_STR_PARSE_ERANGE);
8232232bf4Sjacobs 
8332232bf4Sjacobs 	/* skip entries that begin with '#' */
8432232bf4Sjacobs 	if (instr[0] == '#')
8532232bf4Sjacobs 		return (NSS_STR_PARSE_PARSE);
8632232bf4Sjacobs 
87355b4669Sjacobs 	/*
88355b4669Sjacobs 	 * We copy the input string into the output buffer
89355b4669Sjacobs 	 */
90355b4669Sjacobs 	(void) memcpy(buffer, instr, lenstr);
91355b4669Sjacobs 	buffer[lenstr] = '\0';
92355b4669Sjacobs 
93355b4669Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
94355b4669Sjacobs }
95355b4669Sjacobs 
96355b4669Sjacobs 
97355b4669Sjacobs int
98355b4669Sjacobs setprinterentry(int stayopen, char *ns)
99355b4669Sjacobs {
100355b4669Sjacobs 	printers_stayopen |= stayopen;
101355b4669Sjacobs 	private_ns = ns;
102355b4669Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
103*fe1de1a1Sjacobs 	private_ns = NULL;
104355b4669Sjacobs 	return (0);
105355b4669Sjacobs }
106355b4669Sjacobs 
107355b4669Sjacobs 
108355b4669Sjacobs int
109355b4669Sjacobs endprinterentry()
110355b4669Sjacobs {
111355b4669Sjacobs 	printers_stayopen = 0;
112355b4669Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
113355b4669Sjacobs 	nss_delete(&db_root);
114*fe1de1a1Sjacobs 	private_ns = NULL;
115355b4669Sjacobs 	return (0);
116355b4669Sjacobs }
117355b4669Sjacobs 
118355b4669Sjacobs 
119355b4669Sjacobs /* ARGSUSED2 */
120355b4669Sjacobs int
121355b4669Sjacobs getprinterentry(char *linebuf, int linelen, char *ns)
122355b4669Sjacobs {
123355b4669Sjacobs 	nss_XbyY_args_t arg;
124355b4669Sjacobs 	nss_status_t	res;
125355b4669Sjacobs 
126*fe1de1a1Sjacobs 	private_ns = ns;
127355b4669Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
128355b4669Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
129355b4669Sjacobs 	(void) NSS_XbyY_FINI(&arg);
130*fe1de1a1Sjacobs 	private_ns = NULL;
131*fe1de1a1Sjacobs 
132355b4669Sjacobs 	return (arg.status = res);
133355b4669Sjacobs }
134355b4669Sjacobs 
135355b4669Sjacobs 
136355b4669Sjacobs int
137355b4669Sjacobs getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
138355b4669Sjacobs {
139355b4669Sjacobs 	nss_XbyY_args_t arg;
140355b4669Sjacobs 	nss_status_t	res;
141355b4669Sjacobs 
142355b4669Sjacobs 	private_ns = ns;
143355b4669Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
144355b4669Sjacobs 	arg.key.name = name;
145355b4669Sjacobs 	res = nss_search(&db_root, _nss_initf_printers,
146355b4669Sjacobs 			NSS_DBOP_PRINTERS_BYNAME, &arg);
147355b4669Sjacobs 	(void) NSS_XbyY_FINI(&arg);
148*fe1de1a1Sjacobs 	private_ns = NULL;
149*fe1de1a1Sjacobs 
150355b4669Sjacobs 	return (arg.status = res);
151355b4669Sjacobs }
152