xref: /illumos-gate/usr/src/lib/print/libprint/common/ns.c (revision ef2333d135d7eeabe888ac20be8a501a394db591)
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 *
normalize_ns_name(char * ns)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
ns_printer_destroy(ns_printer_t * printer)77 ns_printer_destroy(ns_printer_t *printer)
78 {
79 	if (printer != NULL) {
80 		if (printer->attributes != NULL) {	/* attributes */
81 			list_iterate((void **)printer->attributes,
82 			    ns_kvp_destroy);
83 			free(printer->attributes);
84 		}
85 		if (printer->aliases != NULL) {		/* aliases */
86 			free(printer->aliases);
87 		}
88 		if (printer->name != NULL)		/* primary name */
89 			free(printer->name);
90 		free(printer);
91 	}
92 }
93 
94 
95 /*
96  * FUNCTION:
97  *	ns_printer_t **ns_printer_get_list()
98  * OUTPUT:
99  *	ns_printer_t ** (return value) - an array of pointers to printer
100  *					 objects.
101  * DESCRIPTION:
102  *	This function will return a list of all printer objects found in every
103  *	configuration interface.
104  */
105 ns_printer_t **
ns_printer_get_list(const char * ns)106 ns_printer_get_list(const char *ns)
107 {
108 	char	    buf[NSS_LINELEN_PRINTERS];
109 	ns_printer_t    **printer_list = NULL;
110 
111 	(void) setprinterentry(0, (char *)ns);
112 
113 	ns = normalize_ns_name((char *)ns);
114 	while (getprinterentry(buf, sizeof (buf), (char *)ns) == 0) {
115 		ns_printer_t *printer =
116 		    (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
117 
118 		printer_list = (ns_printer_t **)list_append(
119 		    (void **)printer_list,
120 		    (void *)printer);
121 	}
122 
123 	(void) endprinterentry();
124 
125 	return (printer_list);
126 }
127 
128 
129 /*
130  * This function looks for the named printer in the supplied
131  * name service (ns), or the name services configured under
132  * the nsswitch.
133  */
134 ns_printer_t *
ns_printer_get_name(const char * name,const char * ns)135 ns_printer_get_name(const char *name, const char *ns)
136 {
137 	ns_printer_t *result = NULL;
138 	char buf[NSS_LINELEN_PRINTERS];
139 
140 	/*
141 	 * Reset printer entries to the start so we know we will always
142 	 * pick up the correct entry
143 	 */
144 	endprinterentry();
145 
146 	if ((result = (ns_printer_t *)posix_name(name)) != NULL)
147 		return (result);
148 
149 	ns = normalize_ns_name((char *)ns);
150 	if (getprinterbyname((char *)name, buf, sizeof (buf), (char *)ns) == 0)
151 		result = (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
152 
153 	return (result);
154 }
155 
156 
157 /*
158  * FUNCTION:
159  *	int ns_printer_put(const ns_printer_t *printer)
160  * INPUT:
161  *	const ns_printer_t *printer - a printer object
162  * DESCRIPTION:
163  *	This function attempts to put the data in the printer object back
164  *	to the "name service" specified in the source field of the object.
165  */
166 int
ns_printer_put(const ns_printer_t * printer)167 ns_printer_put(const ns_printer_t *printer)
168 {
169 	char func[32];
170 	int (*fpt)();
171 
172 	if ((printer == NULL) || (printer->source == NULL))
173 		return (-1);
174 
175 	if (snprintf(func, sizeof (func), "%s_put_printer",
176 	    normalize_ns_name(printer->source)) >= sizeof (func)) {
177 			syslog(LOG_ERR, "ns_printer_put: buffer overflow");
178 			return (-1);
179 	}
180 
181 	if ((fpt = (int (*)())dlsym(RTLD_DEFAULT, func)) != NULL)
182 		return ((*fpt)(printer));
183 
184 	return (-1);
185 }
186