xref: /titanic_50/usr/src/lib/print/libprint/common/ns.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
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 /*LINTLIBRARY*/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <dlfcn.h>
37 #include <nss_dbdefs.h>
38 #include <syslog.h>
39 
40 #include <ns.h>
41 #include <list.h>
42 #include <misc.h>
43 
44 
45 /*
46  * because legacy code can use a variety of values for various name
47  * services, this routine is needed to "normalize" them.
48  */
49 char *
50 normalize_ns_name(char *ns)
51 {
52 	if (ns == NULL)
53 		return (NULL);
54 	else if ((strcasecmp(ns, "files") == 0) ||
55 			(strcasecmp(ns, "system") == 0) ||
56 			(strcasecmp(ns, "etc") == 0))
57 		return ("files");
58 	else if (strcasecmp(ns, "nis") == 0)
59 		return ("nis");
60 	else if (strcasecmp(ns, "ldap") == 0)
61 		return ("ldap");
62 	else if ((strcasecmp(ns, "nisplus") == 0) ||
63 			(strcasecmp(ns, "nis+") == 0))
64 		return ("nisplus");
65 	else
66 		return (ns);
67 }
68 
69 
70 /*
71  * FUNCTION:
72  *	void ns_printer_destroy(ns_printer_t *printer)
73  * INPUT:
74  *	ns_printer_t *printer - a pointer to the printer "object" to destroy
75  * DESCRIPTION:
76  *	This function will free all of the memory associated with a printer
77  *	object.  It does this by walking the structure ad freeing everything
78  *	underneath it, with the exception of the object source field.  This
79  *	field is not filled in with newly allocated space when it is
80  *	generated
81  */
82 void
83 ns_printer_destroy(ns_printer_t *printer)
84 {
85 	if (printer != NULL) {
86 		if (printer->attributes != NULL) {	/* attributes */
87 			extern void ns_kvp_destroy(ns_kvp_t *);
88 
89 			list_iterate((void **)printer->attributes,
90 				(VFUNC_T)ns_kvp_destroy);
91 			free(printer->attributes);
92 		}
93 		if (printer->aliases != NULL) {		/* aliases */
94 			free(printer->aliases);
95 		}
96 		if (printer->name != NULL)		/* primary name */
97 			free(printer->name);
98 		free(printer);
99 	}
100 }
101 
102 
103 /*
104  * FUNCTION:
105  *	ns_printer_t **ns_printer_get_list()
106  * OUTPUT:
107  *	ns_printer_t ** (return value) - an array of pointers to printer
108  *					 objects.
109  * DESCRIPTION:
110  *	This function will return a list of all printer objects found in every
111  *	configuration interface.
112  */
113 ns_printer_t **
114 ns_printer_get_list(const char *ns)
115 {
116 	char	    buf[NSS_LINELEN_PRINTERS];
117 	ns_printer_t    **printer_list = NULL;
118 
119 	(void) setprinterentry(0, (char *)ns);
120 
121 	ns = normalize_ns_name((char *)ns);
122 	while (getprinterentry(buf, sizeof (buf), (char *)ns) == 0) {
123 		ns_printer_t *printer =
124 			(ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
125 
126 		printer_list = (ns_printer_t **)list_append(
127 					(void **)printer_list,
128 					(void *)printer);
129 	}
130 
131 	(void) endprinterentry();
132 
133 	return (printer_list);
134 }
135 
136 
137 /*
138  * This function looks for the named printer in the supplied
139  * name service (ns), or the name services configured under
140  * the nsswitch.
141  */
142 ns_printer_t *
143 ns_printer_get_name(const char *name, const char *ns)
144 {
145 	ns_printer_t *result = NULL;
146 	char buf[NSS_LINELEN_PRINTERS];
147 
148 	/*
149 	 * Reset printer entries to the start so we know we will always
150 	 * pick up the correct entry
151 	 */
152 	endprinterentry();
153 
154 	if ((result = (ns_printer_t *)posix_name(name)) != NULL)
155 		return (result);
156 
157 	ns = normalize_ns_name((char *)ns);
158 	if (getprinterbyname((char *)name, buf, sizeof (buf), (char *)ns) == 0)
159 		result = (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
160 
161 	return (result);
162 }
163 
164 
165 /*
166  * FUNCTION:
167  *	int ns_printer_put(const ns_printer_t *printer)
168  * INPUT:
169  *	const ns_printer_t *printer - a printer object
170  * DESCRIPTION:
171  *	This function attempts to put the data in the printer object back
172  *	to the "name service" specified in the source field of the object.
173  */
174 int
175 ns_printer_put(const ns_printer_t *printer)
176 {
177 	char func[32];
178 	int (*fpt)();
179 
180 	if ((printer == NULL) || (printer->source == NULL))
181 		return (-1);
182 
183 	if (snprintf(func, sizeof (func), "%s_put_printer",
184 		normalize_ns_name(printer->source)) >= sizeof (func)) {
185 			syslog(LOG_ERR, "ns_printer_put: buffer overflow");
186 			return (-1);
187 	}
188 
189 	if ((fpt = (int (*)())dlsym(RTLD_DEFAULT, func)) != NULL)
190 		return ((*fpt)(printer));
191 
192 	return (-1);
193 }
194