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