1*2264Sjacobs /*
2*2264Sjacobs  * CDDL HEADER START
3*2264Sjacobs  *
4*2264Sjacobs  * The contents of this file are subject to the terms of the
5*2264Sjacobs  * Common Development and Distribution License (the "License").
6*2264Sjacobs  * You may not use this file except in compliance with the License.
7*2264Sjacobs  *
8*2264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs  * See the License for the specific language governing permissions
11*2264Sjacobs  * and limitations under the License.
12*2264Sjacobs  *
13*2264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs  *
19*2264Sjacobs  * CDDL HEADER END
20*2264Sjacobs  */
21*2264Sjacobs /*
22*2264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*2264Sjacobs  * Use is subject to license terms.
24*2264Sjacobs  */
25*2264Sjacobs 
26*2264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*2264Sjacobs 
28*2264Sjacobs #include <stdio.h>
29*2264Sjacobs #include <string.h>
30*2264Sjacobs #include <ctype.h>
31*2264Sjacobs #include <sys/types.h>
32*2264Sjacobs #include <nss_dbdefs.h>
33*2264Sjacobs #include <syslog.h>
34*2264Sjacobs #include <ns.h>
35*2264Sjacobs 
36*2264Sjacobs #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
37*2264Sjacobs #define	NSS_DBNAM__PRINTERS	"_printers"
38*2264Sjacobs #endif
39*2264Sjacobs 
40*2264Sjacobs static DEFINE_NSS_DB_ROOT(db_root);
41*2264Sjacobs static DEFINE_NSS_GETENT(context);
42*2264Sjacobs 
43*2264Sjacobs static int printers_stayopen;
44*2264Sjacobs static char *private_ns = NULL;
45*2264Sjacobs static char initialized = 0;
46*2264Sjacobs 
47*2264Sjacobs 
48*2264Sjacobs static void
49*2264Sjacobs _nss_initf_printers(p)
50*2264Sjacobs     nss_db_params_t *p;
51*2264Sjacobs {
52*2264Sjacobs 	if (private_ns != NULL) {
53*2264Sjacobs 		/*
54*2264Sjacobs 		 * because we need to support a legacy interface that allows
55*2264Sjacobs 		 * us to select a specific name service, we need to dummy up
56*2264Sjacobs 		 * the parameters to use a private nsswitch database and set
57*2264Sjacobs 		 * the * default_config entry to the name service we are
58*2264Sjacobs 		 * looking into.
59*2264Sjacobs 		 */
60*2264Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
61*2264Sjacobs 		p->default_config = normalize_ns_name(private_ns);
62*2264Sjacobs 		private_ns = NULL;
63*2264Sjacobs 	} else if (initialized == 0) {
64*2264Sjacobs 		/* regular behaviour */
65*2264Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
66*2264Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
67*2264Sjacobs 		initialized = 1;
68*2264Sjacobs 	}
69*2264Sjacobs 	syslog(LOG_DEBUG, "database: %s, services: %s",
70*2264Sjacobs 		(p->name ? p->name : "NULL"),
71*2264Sjacobs 		(p->default_config ? p->default_config : "NULL"));
72*2264Sjacobs }
73*2264Sjacobs 
74*2264Sjacobs /*
75*2264Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
76*2264Sjacobs  * The structure pointer passed in is a structure in the caller's space
77*2264Sjacobs  * wherein the field pointers would be set to areas in the buffer if
78*2264Sjacobs  * need be. instring and buffer should be separate areas.
79*2264Sjacobs  */
80*2264Sjacobs /* ARGSUSED */
81*2264Sjacobs static int
82*2264Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
83*2264Sjacobs {
84*2264Sjacobs 	if (lenstr + 1 > buflen)
85*2264Sjacobs 		return (NSS_STR_PARSE_ERANGE);
86*2264Sjacobs 	/*
87*2264Sjacobs 	 * We copy the input string into the output buffer
88*2264Sjacobs 	 */
89*2264Sjacobs 	(void) memcpy(buffer, instr, lenstr);
90*2264Sjacobs 	buffer[lenstr] = '\0';
91*2264Sjacobs 
92*2264Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
93*2264Sjacobs }
94*2264Sjacobs 
95*2264Sjacobs 
96*2264Sjacobs int
97*2264Sjacobs setprinterentry(int stayopen, char *ns)
98*2264Sjacobs {
99*2264Sjacobs 	printers_stayopen |= stayopen;
100*2264Sjacobs 	initialized = 0;
101*2264Sjacobs 	private_ns = ns;
102*2264Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
103*2264Sjacobs 	return (0);
104*2264Sjacobs }
105*2264Sjacobs 
106*2264Sjacobs 
107*2264Sjacobs int
108*2264Sjacobs endprinterentry()
109*2264Sjacobs {
110*2264Sjacobs 	printers_stayopen = 0;
111*2264Sjacobs 	initialized = 0;
112*2264Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
113*2264Sjacobs 	nss_delete(&db_root);
114*2264Sjacobs 	return (0);
115*2264Sjacobs }
116*2264Sjacobs 
117*2264Sjacobs 
118*2264Sjacobs /* ARGSUSED2 */
119*2264Sjacobs int
120*2264Sjacobs getprinterentry(char *linebuf, int linelen, char *ns)
121*2264Sjacobs {
122*2264Sjacobs 	nss_XbyY_args_t arg;
123*2264Sjacobs 	nss_status_t	res;
124*2264Sjacobs 
125*2264Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
126*2264Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
127*2264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
128*2264Sjacobs 	return (arg.status = res);
129*2264Sjacobs }
130*2264Sjacobs 
131*2264Sjacobs 
132*2264Sjacobs int
133*2264Sjacobs getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
134*2264Sjacobs {
135*2264Sjacobs 	nss_XbyY_args_t arg;
136*2264Sjacobs 	nss_status_t	res;
137*2264Sjacobs 
138*2264Sjacobs 	private_ns = ns;
139*2264Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
140*2264Sjacobs 	arg.key.name = name;
141*2264Sjacobs 	res = nss_search(&db_root, _nss_initf_printers,
142*2264Sjacobs 			NSS_DBOP_PRINTERS_BYNAME, &arg);
143*2264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
144*2264Sjacobs 	return (arg.status = res);
145*2264Sjacobs }
146