xref: /titanic_41/usr/src/lib/print/libprint/common/nss_printer.c (revision 40db2e2b777b79f3dd0d6d9629593a07f86b9c0a)
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 
46 static void
47 _nss_initf_printers(p)
48     nss_db_params_t *p;
49 {
50 	if (private_ns != NULL) {
51 		/*
52 		 * because we need to support a legacy interface that allows
53 		 * us to select a specific name service, we need to dummy up
54 		 * the parameters to use a private nsswitch database and set
55 		 * the * default_config entry to the name service we are
56 		 * looking into.
57 		 */
58 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
59 		p->default_config = normalize_ns_name(private_ns);
60 	} else {
61 		/* regular behaviour */
62 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
63 		p->default_config = NSS_DEFCONF_PRINTERS;
64 	}
65 	syslog(LOG_DEBUG, "database: %s, default: %s",
66 		(p->name ? p->name : "NULL"),
67 		(p->default_config ? p->default_config : "NULL"));
68 }
69 
70 /*
71  * Return values: 0 = success, 1 = parse error, 2 = erange ...
72  * The structure pointer passed in is a structure in the caller's space
73  * wherein the field pointers would be set to areas in the buffer if
74  * need be. instring and buffer should be separate areas.
75  */
76 /* ARGSUSED */
77 static int
78 str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
79 {
80 	if (lenstr + 1 > buflen)
81 		return (NSS_STR_PARSE_ERANGE);
82 
83 	/* skip entries that begin with '#' */
84 	if (instr[0] == '#')
85 		return (NSS_STR_PARSE_PARSE);
86 
87 	/*
88 	 * We copy the input string into the output buffer
89 	 */
90 	(void) memcpy(buffer, instr, lenstr);
91 	buffer[lenstr] = '\0';
92 
93 	return (NSS_STR_PARSE_SUCCESS);
94 }
95 
96 
97 int
98 setprinterentry(int stayopen, char *ns)
99 {
100 	printers_stayopen |= stayopen;
101 	private_ns = ns;
102 	nss_setent(&db_root, _nss_initf_printers, &context);
103 	private_ns = NULL;
104 	return (0);
105 }
106 
107 
108 int
109 endprinterentry()
110 {
111 	printers_stayopen = 0;
112 	nss_endent(&db_root, _nss_initf_printers, &context);
113 	nss_delete(&db_root);
114 	private_ns = NULL;
115 	return (0);
116 }
117 
118 
119 /* ARGSUSED2 */
120 int
121 getprinterentry(char *linebuf, int linelen, char *ns)
122 {
123 	nss_XbyY_args_t arg;
124 	nss_status_t	res;
125 
126 	private_ns = ns;
127 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
128 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
129 	(void) NSS_XbyY_FINI(&arg);
130 	private_ns = NULL;
131 
132 	return (arg.status = res);
133 }
134 
135 
136 int
137 getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
138 {
139 	nss_XbyY_args_t arg;
140 	nss_status_t	res;
141 
142 	private_ns = ns;
143 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
144 	arg.key.name = name;
145 	res = nss_search(&db_root, _nss_initf_printers,
146 			NSS_DBOP_PRINTERS_BYNAME, &arg);
147 	(void) NSS_XbyY_FINI(&arg);
148 	private_ns = NULL;
149 
150 	return (arg.status = res);
151 }
152