1355b4669Sjacobs /* 2355b4669Sjacobs * CDDL HEADER START 3355b4669Sjacobs * 4355b4669Sjacobs * 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. 7355b4669Sjacobs * 8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing. 10355b4669Sjacobs * See the License for the specific language governing permissions 11355b4669Sjacobs * and limitations under the License. 12355b4669Sjacobs * 13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the 16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 18355b4669Sjacobs * 19355b4669Sjacobs * CDDL HEADER END 20355b4669Sjacobs */ 21355b4669Sjacobs /* 22*36e852a1SRaja Andra * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23355b4669Sjacobs * Use is subject to license terms. 24355b4669Sjacobs */ 25355b4669Sjacobs 26355b4669Sjacobs /*LINTLIBRARY*/ 27355b4669Sjacobs 28355b4669Sjacobs #include <stdio.h> 29355b4669Sjacobs #include <stdlib.h> 30355b4669Sjacobs #include <unistd.h> 31355b4669Sjacobs #include <sys/types.h> 32355b4669Sjacobs #include <string.h> 33355b4669Sjacobs #include <stdarg.h> 34355b4669Sjacobs #include <dlfcn.h> 35355b4669Sjacobs #include <nss_dbdefs.h> 36355b4669Sjacobs #include <syslog.h> 37355b4669Sjacobs 38355b4669Sjacobs #include <ns.h> 39355b4669Sjacobs #include <list.h> 40355b4669Sjacobs 41355b4669Sjacobs 42355b4669Sjacobs /* 43355b4669Sjacobs * because legacy code can use a variety of values for various name 44355b4669Sjacobs * services, this routine is needed to "normalize" them. 45355b4669Sjacobs */ 46355b4669Sjacobs char * 47355b4669Sjacobs normalize_ns_name(char *ns) 48355b4669Sjacobs { 49355b4669Sjacobs if (ns == NULL) 50355b4669Sjacobs return (NULL); 51355b4669Sjacobs else if ((strcasecmp(ns, "files") == 0) || 52355b4669Sjacobs (strcasecmp(ns, "system") == 0) || 53355b4669Sjacobs (strcasecmp(ns, "etc") == 0)) 54355b4669Sjacobs return ("files"); 55355b4669Sjacobs else if (strcasecmp(ns, "nis") == 0) 56355b4669Sjacobs return ("nis"); 57355b4669Sjacobs else if (strcasecmp(ns, "ldap") == 0) 58355b4669Sjacobs return ("ldap"); 59355b4669Sjacobs else 60355b4669Sjacobs return (ns); 61355b4669Sjacobs } 62355b4669Sjacobs 63355b4669Sjacobs 64355b4669Sjacobs /* 65355b4669Sjacobs * FUNCTION: 66355b4669Sjacobs * void ns_printer_destroy(ns_printer_t *printer) 67355b4669Sjacobs * INPUT: 68355b4669Sjacobs * ns_printer_t *printer - a pointer to the printer "object" to destroy 69355b4669Sjacobs * DESCRIPTION: 70355b4669Sjacobs * This function will free all of the memory associated with a printer 71355b4669Sjacobs * object. It does this by walking the structure ad freeing everything 72355b4669Sjacobs * underneath it, with the exception of the object source field. This 73355b4669Sjacobs * field is not filled in with newly allocated space when it is 74355b4669Sjacobs * generated 75355b4669Sjacobs */ 76355b4669Sjacobs void 77355b4669Sjacobs ns_printer_destroy(ns_printer_t *printer) 78355b4669Sjacobs { 79355b4669Sjacobs if (printer != NULL) { 80355b4669Sjacobs if (printer->attributes != NULL) { /* attributes */ 81355b4669Sjacobs extern void ns_kvp_destroy(ns_kvp_t *); 82355b4669Sjacobs 83355b4669Sjacobs list_iterate((void **)printer->attributes, 84355b4669Sjacobs (VFUNC_T)ns_kvp_destroy); 85355b4669Sjacobs free(printer->attributes); 86355b4669Sjacobs } 87355b4669Sjacobs if (printer->aliases != NULL) { /* aliases */ 88355b4669Sjacobs free(printer->aliases); 89355b4669Sjacobs } 90355b4669Sjacobs if (printer->name != NULL) /* primary name */ 91355b4669Sjacobs free(printer->name); 92355b4669Sjacobs free(printer); 93355b4669Sjacobs } 94355b4669Sjacobs } 95355b4669Sjacobs 96355b4669Sjacobs 97355b4669Sjacobs /* 98355b4669Sjacobs * FUNCTION: 99355b4669Sjacobs * ns_printer_t **ns_printer_get_list() 100355b4669Sjacobs * OUTPUT: 101355b4669Sjacobs * ns_printer_t ** (return value) - an array of pointers to printer 102355b4669Sjacobs * objects. 103355b4669Sjacobs * DESCRIPTION: 104355b4669Sjacobs * This function will return a list of all printer objects found in every 105355b4669Sjacobs * configuration interface. 106355b4669Sjacobs */ 107355b4669Sjacobs ns_printer_t ** 108355b4669Sjacobs ns_printer_get_list(const char *ns) 109355b4669Sjacobs { 110355b4669Sjacobs char buf[NSS_LINELEN_PRINTERS]; 111355b4669Sjacobs ns_printer_t **printer_list = NULL; 112355b4669Sjacobs 113355b4669Sjacobs (void) setprinterentry(0, (char *)ns); 114355b4669Sjacobs 115355b4669Sjacobs ns = normalize_ns_name((char *)ns); 116355b4669Sjacobs while (getprinterentry(buf, sizeof (buf), (char *)ns) == 0) { 117355b4669Sjacobs ns_printer_t *printer = 118355b4669Sjacobs (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL); 119355b4669Sjacobs 120355b4669Sjacobs printer_list = (ns_printer_t **)list_append( 121355b4669Sjacobs (void **)printer_list, 122355b4669Sjacobs (void *)printer); 123355b4669Sjacobs } 124355b4669Sjacobs 125355b4669Sjacobs (void) endprinterentry(); 126355b4669Sjacobs 127355b4669Sjacobs return (printer_list); 128355b4669Sjacobs } 129355b4669Sjacobs 130355b4669Sjacobs 131355b4669Sjacobs /* 132355b4669Sjacobs * This function looks for the named printer in the supplied 133355b4669Sjacobs * name service (ns), or the name services configured under 134355b4669Sjacobs * the nsswitch. 135355b4669Sjacobs */ 136355b4669Sjacobs ns_printer_t * 137355b4669Sjacobs ns_printer_get_name(const char *name, const char *ns) 138355b4669Sjacobs { 139355b4669Sjacobs ns_printer_t *result = NULL; 140355b4669Sjacobs char buf[NSS_LINELEN_PRINTERS]; 141355b4669Sjacobs 142355b4669Sjacobs /* 143355b4669Sjacobs * Reset printer entries to the start so we know we will always 144355b4669Sjacobs * pick up the correct entry 145355b4669Sjacobs */ 146355b4669Sjacobs endprinterentry(); 147355b4669Sjacobs 148355b4669Sjacobs if ((result = (ns_printer_t *)posix_name(name)) != NULL) 149355b4669Sjacobs return (result); 150355b4669Sjacobs 151355b4669Sjacobs ns = normalize_ns_name((char *)ns); 152355b4669Sjacobs if (getprinterbyname((char *)name, buf, sizeof (buf), (char *)ns) == 0) 153355b4669Sjacobs result = (ns_printer_t *)_cvt_nss_entry_to_printer(buf, NULL); 154355b4669Sjacobs 155355b4669Sjacobs return (result); 156355b4669Sjacobs } 157355b4669Sjacobs 158355b4669Sjacobs 159355b4669Sjacobs /* 160355b4669Sjacobs * FUNCTION: 161355b4669Sjacobs * int ns_printer_put(const ns_printer_t *printer) 162355b4669Sjacobs * INPUT: 163355b4669Sjacobs * const ns_printer_t *printer - a printer object 164355b4669Sjacobs * DESCRIPTION: 165355b4669Sjacobs * This function attempts to put the data in the printer object back 166355b4669Sjacobs * to the "name service" specified in the source field of the object. 167355b4669Sjacobs */ 168355b4669Sjacobs int 169355b4669Sjacobs ns_printer_put(const ns_printer_t *printer) 170355b4669Sjacobs { 171355b4669Sjacobs char func[32]; 172355b4669Sjacobs int (*fpt)(); 173355b4669Sjacobs 174355b4669Sjacobs if ((printer == NULL) || (printer->source == NULL)) 175355b4669Sjacobs return (-1); 176355b4669Sjacobs 177355b4669Sjacobs if (snprintf(func, sizeof (func), "%s_put_printer", 178355b4669Sjacobs normalize_ns_name(printer->source)) >= sizeof (func)) { 179355b4669Sjacobs syslog(LOG_ERR, "ns_printer_put: buffer overflow"); 180355b4669Sjacobs return (-1); 181355b4669Sjacobs } 182355b4669Sjacobs 183355b4669Sjacobs if ((fpt = (int (*)())dlsym(RTLD_DEFAULT, func)) != NULL) 184355b4669Sjacobs return ((*fpt)(printer)); 185355b4669Sjacobs 186355b4669Sjacobs return (-1); 187355b4669Sjacobs } 188