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