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