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