xref: /illumos-gate/usr/src/lib/print/libprint/common/ns.c (revision ef2333d1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2236e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <dlfcn.h>
357c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
367c478bd9Sstevel@tonic-gate #include <syslog.h>
377c478bd9Sstevel@tonic-gate 
38355b4669Sjacobs #include <ns.h>
39355b4669Sjacobs #include <list.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * because legacy code can use a variety of values for various name
447c478bd9Sstevel@tonic-gate  * services, this routine is needed to "normalize" them.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate char *
normalize_ns_name(char * ns)477c478bd9Sstevel@tonic-gate normalize_ns_name(char *ns)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	if (ns == NULL)
507c478bd9Sstevel@tonic-gate 		return (NULL);
517c478bd9Sstevel@tonic-gate 	else if ((strcasecmp(ns, "files") == 0) ||
5236e852a1SRaja Andra 	    (strcasecmp(ns, "system") == 0) ||
5336e852a1SRaja Andra 	    (strcasecmp(ns, "etc") == 0))
547c478bd9Sstevel@tonic-gate 		return ("files");
557c478bd9Sstevel@tonic-gate 	else if (strcasecmp(ns, "nis") == 0)
567c478bd9Sstevel@tonic-gate 		return ("nis");
577c478bd9Sstevel@tonic-gate 	else if (strcasecmp(ns, "ldap") == 0)
587c478bd9Sstevel@tonic-gate 		return ("ldap");
597c478bd9Sstevel@tonic-gate 	else
607c478bd9Sstevel@tonic-gate 		return (ns);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * FUNCTION:
667c478bd9Sstevel@tonic-gate  *	void ns_printer_destroy(ns_printer_t *printer)
677c478bd9Sstevel@tonic-gate  * INPUT:
687c478bd9Sstevel@tonic-gate  *	ns_printer_t *printer - a pointer to the printer "object" to destroy
697c478bd9Sstevel@tonic-gate  * DESCRIPTION:
707c478bd9Sstevel@tonic-gate  *	This function will free all of the memory associated with a printer
717c478bd9Sstevel@tonic-gate  *	object.  It does this by walking the structure ad freeing everything
727c478bd9Sstevel@tonic-gate  *	underneath it, with the exception of the object source field.  This
737c478bd9Sstevel@tonic-gate  *	field is not filled in with newly allocated space when it is
747c478bd9Sstevel@tonic-gate  *	generated
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate void
ns_printer_destroy(ns_printer_t * printer)777c478bd9Sstevel@tonic-gate ns_printer_destroy(ns_printer_t *printer)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	if (printer != NULL) {
807c478bd9Sstevel@tonic-gate 		if (printer->attributes != NULL) {	/* attributes */
817c478bd9Sstevel@tonic-gate 			list_iterate((void **)printer->attributes,
82*ef2333d1SToomas Soome 			    ns_kvp_destroy);
837c478bd9Sstevel@tonic-gate 			free(printer->attributes);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		if (printer->aliases != NULL) {		/* aliases */
867c478bd9Sstevel@tonic-gate 			free(printer->aliases);
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		if (printer->name != NULL)		/* primary name */
897c478bd9Sstevel@tonic-gate 			free(printer->name);
907c478bd9Sstevel@tonic-gate 		free(printer);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * FUNCTION:
977c478bd9Sstevel@tonic-gate  *	ns_printer_t **ns_printer_get_list()
987c478bd9Sstevel@tonic-gate  * OUTPUT:
997c478bd9Sstevel@tonic-gate  *	ns_printer_t ** (return value) - an array of pointers to printer
1007c478bd9Sstevel@tonic-gate  *					 objects.
1017c478bd9Sstevel@tonic-gate  * DESCRIPTION:
1027c478bd9Sstevel@tonic-gate  *	This function will return a list of all printer objects found in every
1037c478bd9Sstevel@tonic-gate  *	configuration interface.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate ns_printer_t **
ns_printer_get_list(const char * ns)1067c478bd9Sstevel@tonic-gate ns_printer_get_list(const char *ns)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char	    buf[NSS_LINELEN_PRINTERS];
1097c478bd9Sstevel@tonic-gate 	ns_printer_t    **printer_list = NULL;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	(void) setprinterentry(0, (char *)ns);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	ns = normalize_ns_name((char *)ns);
1147c478bd9Sstevel@tonic-gate 	while (getprinterentry(buf, sizeof (buf), (char *)ns) == 0) {
1157c478bd9Sstevel@tonic-gate 		ns_printer_t *printer =
11636e852a1SRaja Andra 		    (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		printer_list = (ns_printer_t **)list_append(
11936e852a1SRaja Andra 		    (void **)printer_list,
12036e852a1SRaja Andra 		    (void *)printer);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	(void) endprinterentry();
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	return (printer_list);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * This function looks for the named printer in the supplied
1317c478bd9Sstevel@tonic-gate  * name service (ns), or the name services configured under
1327c478bd9Sstevel@tonic-gate  * the nsswitch.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate ns_printer_t *
ns_printer_get_name(const char * name,const char * ns)1357c478bd9Sstevel@tonic-gate ns_printer_get_name(const char *name, const char *ns)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	ns_printer_t *result = NULL;
1387c478bd9Sstevel@tonic-gate 	char buf[NSS_LINELEN_PRINTERS];
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * Reset printer entries to the start so we know we will always
1427c478bd9Sstevel@tonic-gate 	 * pick up the correct entry
1437c478bd9Sstevel@tonic-gate 	 */
1447c478bd9Sstevel@tonic-gate 	endprinterentry();
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if ((result = (ns_printer_t *)posix_name(name)) != NULL)
1477c478bd9Sstevel@tonic-gate 		return (result);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	ns = normalize_ns_name((char *)ns);
1507c478bd9Sstevel@tonic-gate 	if (getprinterbyname((char *)name, buf, sizeof (buf), (char *)ns) == 0)
1517c478bd9Sstevel@tonic-gate 		result = (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	return (result);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * FUNCTION:
1597c478bd9Sstevel@tonic-gate  *	int ns_printer_put(const ns_printer_t *printer)
1607c478bd9Sstevel@tonic-gate  * INPUT:
1617c478bd9Sstevel@tonic-gate  *	const ns_printer_t *printer - a printer object
1627c478bd9Sstevel@tonic-gate  * DESCRIPTION:
1637c478bd9Sstevel@tonic-gate  *	This function attempts to put the data in the printer object back
1647c478bd9Sstevel@tonic-gate  *	to the "name service" specified in the source field of the object.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate int
ns_printer_put(const ns_printer_t * printer)1677c478bd9Sstevel@tonic-gate ns_printer_put(const ns_printer_t *printer)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	char func[32];
1707c478bd9Sstevel@tonic-gate 	int (*fpt)();
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if ((printer == NULL) || (printer->source == NULL))
1737c478bd9Sstevel@tonic-gate 		return (-1);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (snprintf(func, sizeof (func), "%s_put_printer",
17636e852a1SRaja Andra 	    normalize_ns_name(printer->source)) >= sizeof (func)) {
1777c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "ns_printer_put: buffer overflow");
1787c478bd9Sstevel@tonic-gate 			return (-1);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if ((fpt = (int (*)())dlsym(RTLD_DEFAULT, func)) != NULL)
1827c478bd9Sstevel@tonic-gate 		return ((*fpt)(printer));
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (-1);
1857c478bd9Sstevel@tonic-gate }
186