12264Sjacobs /*
22264Sjacobs * CDDL HEADER START
32264Sjacobs *
42264Sjacobs * The contents of this file are subject to the terms of the
52264Sjacobs * Common Development and Distribution License (the "License").
62264Sjacobs * You may not use this file except in compliance with the License.
72264Sjacobs *
82264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs * or http://www.opensolaris.org/os/licensing.
102264Sjacobs * See the License for the specific language governing permissions
112264Sjacobs * and limitations under the License.
122264Sjacobs *
132264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs *
192264Sjacobs * CDDL HEADER END
202264Sjacobs */
212264Sjacobs /*
22*11262SRajagopal.Andra@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
232264Sjacobs * Use is subject to license terms.
242264Sjacobs */
252264Sjacobs
262264Sjacobs /*LINTLIBRARY*/
272264Sjacobs
282264Sjacobs #include <stdio.h>
292264Sjacobs #include <stdlib.h>
302264Sjacobs #include <unistd.h>
312264Sjacobs #include <sys/types.h>
322264Sjacobs #include <string.h>
332264Sjacobs #include <stdarg.h>
342264Sjacobs #include <dlfcn.h>
352264Sjacobs #include <nss_dbdefs.h>
362264Sjacobs #include <syslog.h>
372264Sjacobs
382264Sjacobs #include <ns.h>
392264Sjacobs #include <list.h>
402264Sjacobs
412264Sjacobs
422264Sjacobs /*
432264Sjacobs * because legacy code can use a variety of values for various name
442264Sjacobs * services, this routine is needed to "normalize" them.
452264Sjacobs */
462264Sjacobs char *
normalize_ns_name(char * ns)472264Sjacobs normalize_ns_name(char *ns)
482264Sjacobs {
492264Sjacobs if (ns == NULL)
502264Sjacobs return (NULL);
512264Sjacobs else if ((strcasecmp(ns, "files") == 0) ||
52*11262SRajagopal.Andra@Sun.COM (strcasecmp(ns, "system") == 0) ||
53*11262SRajagopal.Andra@Sun.COM (strcasecmp(ns, "etc") == 0))
542264Sjacobs return ("files");
552264Sjacobs else if (strcasecmp(ns, "nis") == 0)
562264Sjacobs return ("nis");
572264Sjacobs else if (strcasecmp(ns, "ldap") == 0)
582264Sjacobs return ("ldap");
592264Sjacobs else
602264Sjacobs return (ns);
612264Sjacobs }
622264Sjacobs
632264Sjacobs
642264Sjacobs /*
652264Sjacobs * FUNCTION:
662264Sjacobs * void ns_printer_destroy(ns_printer_t *printer)
672264Sjacobs * INPUT:
682264Sjacobs * ns_printer_t *printer - a pointer to the printer "object" to destroy
692264Sjacobs * DESCRIPTION:
702264Sjacobs * This function will free all of the memory associated with a printer
712264Sjacobs * object. It does this by walking the structure ad freeing everything
722264Sjacobs * underneath it, with the exception of the object source field. This
732264Sjacobs * field is not filled in with newly allocated space when it is
742264Sjacobs * generated
752264Sjacobs */
762264Sjacobs void
ns_printer_destroy(ns_printer_t * printer)772264Sjacobs ns_printer_destroy(ns_printer_t *printer)
782264Sjacobs {
792264Sjacobs if (printer != NULL) {
802264Sjacobs if (printer->attributes != NULL) { /* attributes */
812264Sjacobs extern void ns_kvp_destroy(ns_kvp_t *);
822264Sjacobs
832264Sjacobs list_iterate((void **)printer->attributes,
84*11262SRajagopal.Andra@Sun.COM (VFUNC_T)ns_kvp_destroy);
852264Sjacobs free(printer->attributes);
862264Sjacobs }
872264Sjacobs if (printer->aliases != NULL) { /* aliases */
882264Sjacobs free(printer->aliases);
892264Sjacobs }
902264Sjacobs if (printer->name != NULL) /* primary name */
912264Sjacobs free(printer->name);
922264Sjacobs free(printer);
932264Sjacobs }
942264Sjacobs }
952264Sjacobs
962264Sjacobs
972264Sjacobs /*
982264Sjacobs * FUNCTION:
992264Sjacobs * ns_printer_t **ns_printer_get_list()
1002264Sjacobs * OUTPUT:
1012264Sjacobs * ns_printer_t ** (return value) - an array of pointers to printer
1022264Sjacobs * objects.
1032264Sjacobs * DESCRIPTION:
1042264Sjacobs * This function will return a list of all printer objects found in every
1052264Sjacobs * configuration interface.
1062264Sjacobs */
1072264Sjacobs ns_printer_t **
ns_printer_get_list(const char * ns)1082264Sjacobs ns_printer_get_list(const char *ns)
1092264Sjacobs {
1102264Sjacobs char buf[NSS_LINELEN_PRINTERS];
1112264Sjacobs ns_printer_t **printer_list = NULL;
1122264Sjacobs
1132264Sjacobs (void) setprinterentry(0, (char *)ns);
1142264Sjacobs
1152264Sjacobs ns = normalize_ns_name((char *)ns);
1162264Sjacobs while (getprinterentry(buf, sizeof (buf), (char *)ns) == 0) {
1172264Sjacobs ns_printer_t *printer =
118*11262SRajagopal.Andra@Sun.COM (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
1192264Sjacobs
1202264Sjacobs printer_list = (ns_printer_t **)list_append(
121*11262SRajagopal.Andra@Sun.COM (void **)printer_list,
122*11262SRajagopal.Andra@Sun.COM (void *)printer);
1232264Sjacobs }
1242264Sjacobs
1252264Sjacobs (void) endprinterentry();
1262264Sjacobs
1272264Sjacobs return (printer_list);
1282264Sjacobs }
1292264Sjacobs
1302264Sjacobs
1312264Sjacobs /*
1322264Sjacobs * This function looks for the named printer in the supplied
1332264Sjacobs * name service (ns), or the name services configured under
1342264Sjacobs * the nsswitch.
1352264Sjacobs */
1362264Sjacobs ns_printer_t *
ns_printer_get_name(const char * name,const char * ns)1372264Sjacobs ns_printer_get_name(const char *name, const char *ns)
1382264Sjacobs {
1392264Sjacobs ns_printer_t *result = NULL;
1402264Sjacobs char buf[NSS_LINELEN_PRINTERS];
1412264Sjacobs
1422264Sjacobs /*
1432264Sjacobs * Reset printer entries to the start so we know we will always
1442264Sjacobs * pick up the correct entry
1452264Sjacobs */
1462264Sjacobs endprinterentry();
1472264Sjacobs
1482264Sjacobs if ((result = (ns_printer_t *)posix_name(name)) != NULL)
1492264Sjacobs return (result);
1502264Sjacobs
1512264Sjacobs ns = normalize_ns_name((char *)ns);
1522264Sjacobs if (getprinterbyname((char *)name, buf, sizeof (buf), (char *)ns) == 0)
1532264Sjacobs result = (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL);
1542264Sjacobs
1552264Sjacobs return (result);
1562264Sjacobs }
1572264Sjacobs
1582264Sjacobs
1592264Sjacobs /*
1602264Sjacobs * FUNCTION:
1612264Sjacobs * int ns_printer_put(const ns_printer_t *printer)
1622264Sjacobs * INPUT:
1632264Sjacobs * const ns_printer_t *printer - a printer object
1642264Sjacobs * DESCRIPTION:
1652264Sjacobs * This function attempts to put the data in the printer object back
1662264Sjacobs * to the "name service" specified in the source field of the object.
1672264Sjacobs */
1682264Sjacobs int
ns_printer_put(const ns_printer_t * printer)1692264Sjacobs ns_printer_put(const ns_printer_t *printer)
1702264Sjacobs {
1712264Sjacobs char func[32];
1722264Sjacobs int (*fpt)();
1732264Sjacobs
1742264Sjacobs if ((printer == NULL) || (printer->source == NULL))
1752264Sjacobs return (-1);
1762264Sjacobs
1772264Sjacobs if (snprintf(func, sizeof (func), "%s_put_printer",
178*11262SRajagopal.Andra@Sun.COM normalize_ns_name(printer->source)) >= sizeof (func)) {
1792264Sjacobs syslog(LOG_ERR, "ns_printer_put: buffer overflow");
1802264Sjacobs return (-1);
1812264Sjacobs }
1822264Sjacobs
1832264Sjacobs if ((fpt = (int (*)())dlsym(RTLD_DEFAULT, func)) != NULL)
1842264Sjacobs return ((*fpt)(printer));
1852264Sjacobs
1862264Sjacobs return (-1);
1872264Sjacobs }
188