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 /*
22c1ecd8b9Sjacobs  * Copyright 2008 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 <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate 
36355b4669Sjacobs #include <ns.h>
37355b4669Sjacobs #include <list.h>
38c1ecd8b9Sjacobs 
39c1ecd8b9Sjacobs static char **
strsplit(char * string,char * seperators)40c1ecd8b9Sjacobs strsplit(char *string, char *seperators)
41c1ecd8b9Sjacobs {
42c1ecd8b9Sjacobs 	char **list = NULL;
43c1ecd8b9Sjacobs 	char *where = NULL;
44c1ecd8b9Sjacobs 	char *element;
45c1ecd8b9Sjacobs 
46c1ecd8b9Sjacobs 	for (element = strtok_r(string, seperators, &where); element != NULL;
47c1ecd8b9Sjacobs 	    element = strtok_r(NULL, seperators, &where))
48c1ecd8b9Sjacobs 		list = (char **)list_append((void **)list, element);
49c1ecd8b9Sjacobs 
50c1ecd8b9Sjacobs 	return (list);
51c1ecd8b9Sjacobs }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  *	Manipulate bsd_addr structures
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate ns_bsd_addr_t *
bsd_addr_create(const char * server,const char * printer,const char * extension)577c478bd9Sstevel@tonic-gate bsd_addr_create(const char *server, const char *printer, const char *extension)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	ns_bsd_addr_t *addr = NULL;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if ((server != NULL) &&
627c478bd9Sstevel@tonic-gate 	    ((addr = calloc(1, sizeof (*addr))) != NULL)) {
637c478bd9Sstevel@tonic-gate 		addr->printer = (char *)printer;
647c478bd9Sstevel@tonic-gate 		addr->server = (char *)server;
657c478bd9Sstevel@tonic-gate 		addr->extension = (char *)extension;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	return (addr);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static char *
bsd_addr_to_string(const ns_bsd_addr_t * addr)727c478bd9Sstevel@tonic-gate bsd_addr_to_string(const ns_bsd_addr_t *addr)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if ((addr == NULL) || (addr->server == NULL))
777c478bd9Sstevel@tonic-gate 		return (NULL);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s", addr->server) >= sizeof (buf)) {
807c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "bsd_addr_to_string: buffer overflow");
817c478bd9Sstevel@tonic-gate 		return (NULL);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if ((addr->printer != NULL) || (addr->extension != NULL))
857c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, ",", sizeof (buf));
867c478bd9Sstevel@tonic-gate 	if (addr->printer != NULL)
877c478bd9Sstevel@tonic-gate 		if (strlcat(buf, addr->printer, sizeof (buf)) >= sizeof (buf)) {
887c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "bsd_addr_to_string: buffer overflow");
897c478bd9Sstevel@tonic-gate 			return (NULL);
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 	if (addr->extension != NULL) {
927c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, ",", sizeof (buf));
937c478bd9Sstevel@tonic-gate 		if (strlcat(buf, addr->extension, sizeof (buf))
94*f4593de7SToomas Soome 		    >= sizeof (buf)) {
957c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "bsd_addr_to_string: buffer overflow");
967c478bd9Sstevel@tonic-gate 			return (NULL);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	return (strdup(buf));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate ns_bsd_addr_t *
string_to_bsd_addr(const char * string)1047c478bd9Sstevel@tonic-gate string_to_bsd_addr(const char *string)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	char **list, *tmp, *printer = NULL, *extension = NULL;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (string == NULL)
1097c478bd9Sstevel@tonic-gate 		return (NULL);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	tmp = strdup(string);
1127c478bd9Sstevel@tonic-gate 	list = strsplit(tmp, ",");
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (list[1] != NULL) {
1157c478bd9Sstevel@tonic-gate 		printer = list[1];
1167c478bd9Sstevel@tonic-gate 		if (list[2] != NULL)
1177c478bd9Sstevel@tonic-gate 			extension = list[2];
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (bsd_addr_create(list[0], printer, extension));
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static char *
list_to_string(const char ** list)1247c478bd9Sstevel@tonic-gate list_to_string(const char **list)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if ((list == NULL) || (*list == NULL))
1297c478bd9Sstevel@tonic-gate 		return (NULL);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s", *list) >= sizeof (buf)) {
1327c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "list_to_string: buffer overflow");
1337c478bd9Sstevel@tonic-gate 		return (NULL);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	while (*++list != NULL) {
1377c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, ",", sizeof (buf));
1387c478bd9Sstevel@tonic-gate 		if (strlcat(buf, *list, sizeof (buf)) >= sizeof (buf)) {
1397c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "list_to_string: buffer overflow");
1407c478bd9Sstevel@tonic-gate 			return (NULL);
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return (strdup(buf));
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static char *
internal_list_to_string(const ns_printer_t ** list)1487c478bd9Sstevel@tonic-gate internal_list_to_string(const ns_printer_t **list)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if ((list == NULL) || (*list == NULL))
1537c478bd9Sstevel@tonic-gate 		return (NULL);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s", (*list)->name) >= sizeof (buf)) {
1567c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "internal_list_to_string:buffer overflow");
1577c478bd9Sstevel@tonic-gate 		return (NULL);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	while (*++list != NULL) {
1617c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, ",", sizeof (buf));
1627c478bd9Sstevel@tonic-gate 		if (strlcat(buf, (*list)->name, sizeof (buf)) >= sizeof (buf)) {
1637c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
164*f4593de7SToomas Soome 			    "internal_list_to_string:buffer overflow");
1657c478bd9Sstevel@tonic-gate 			return (NULL);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (strdup(buf));
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate char *
value_to_string(const char * key,void * value)1747c478bd9Sstevel@tonic-gate value_to_string(const char *key, void *value)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	char *string = NULL;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if ((key != NULL) && (value != NULL)) {
1797c478bd9Sstevel@tonic-gate 		if (strcmp(key, NS_KEY_BSDADDR) == 0) {
1807c478bd9Sstevel@tonic-gate 			string = bsd_addr_to_string(value);
1817c478bd9Sstevel@tonic-gate 		} else if ((strcmp(key, NS_KEY_ALL) == 0) ||
182*f4593de7SToomas Soome 		    (strcmp(key, NS_KEY_GROUP) == 0)) {
1837c478bd9Sstevel@tonic-gate 			string = list_to_string(value);
1847c478bd9Sstevel@tonic-gate 		} else if (strcmp(key, NS_KEY_LIST) == 0) {
1857c478bd9Sstevel@tonic-gate 			string = internal_list_to_string(value);
1867c478bd9Sstevel@tonic-gate 		} else {
1877c478bd9Sstevel@tonic-gate 			string = strdup((char *)value);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	return (string);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate void *
string_to_value(const char * key,char * string)1967c478bd9Sstevel@tonic-gate string_to_value(const char *key, char *string)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	void *value = NULL;
1997c478bd9Sstevel@tonic-gate 
200*f4593de7SToomas Soome 	if ((key != NULL) && (string != NULL) && (string[0] != '\0')) {
2017c478bd9Sstevel@tonic-gate 		if (strcmp(key, NS_KEY_BSDADDR) == 0) {
2027c478bd9Sstevel@tonic-gate 			value = (void *)string_to_bsd_addr(string);
2037c478bd9Sstevel@tonic-gate 		} else if ((strcmp(key, NS_KEY_ALL) == 0) ||
204*f4593de7SToomas Soome 		    (strcmp(key, NS_KEY_GROUP) == 0)) {
2057c478bd9Sstevel@tonic-gate 			value = (void *)strsplit(string, ",");
2067c478bd9Sstevel@tonic-gate 		} else {
2077c478bd9Sstevel@tonic-gate 			value = (void *)string;
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return (value);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static void
split_name(char * name,const char * delimiter,char ** p1,char ** p2,char ** p3)2157c478bd9Sstevel@tonic-gate split_name(char *name, const char *delimiter, char **p1, char **p2, char **p3)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	char *tmp, *junk = NULL;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (p1 != NULL)
2207c478bd9Sstevel@tonic-gate 		*p1 = NULL;
2217c478bd9Sstevel@tonic-gate 	if (p2 != NULL)
2227c478bd9Sstevel@tonic-gate 		*p2 = NULL;
2237c478bd9Sstevel@tonic-gate 	if (p3 != NULL)
2247c478bd9Sstevel@tonic-gate 		*p3 = NULL;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((name == NULL) || (delimiter == NULL)) {
2277c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "split_name(): name/delimter invalid\n");
2287c478bd9Sstevel@tonic-gate 		return;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	for (tmp = (char *)strtok_r(name, delimiter, &junk); tmp != NULL;
2327c478bd9Sstevel@tonic-gate 	    tmp = (char *)strtok_r(NULL, delimiter, &junk))
233*f4593de7SToomas Soome 		if ((p1 != NULL) && (*p1 == NULL)) {
2347c478bd9Sstevel@tonic-gate 			*p1 = tmp;
235*f4593de7SToomas Soome 		} else if ((p2 != NULL) && (*p2 == NULL)) {
2367c478bd9Sstevel@tonic-gate 			*p2 = tmp;
2377c478bd9Sstevel@tonic-gate 			if (p3 == NULL)
2387c478bd9Sstevel@tonic-gate 				break;
2397c478bd9Sstevel@tonic-gate 		} else if ((p3 != NULL) && (*p3 == NULL)) {
2407c478bd9Sstevel@tonic-gate 			*p3 = tmp;
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * This implements support for printer names that are fully resolvable
2477c478bd9Sstevel@tonic-gate  * on their own.  These "complete" names are converted into a ns_printer_t
2487c478bd9Sstevel@tonic-gate  * structure containing an appropriate "bsdaddr" attribute.  The supported
2497c478bd9Sstevel@tonic-gate  * formats are as follows:
2507c478bd9Sstevel@tonic-gate  *	POSIX style (server:printer[:conformance]).
2517c478bd9Sstevel@tonic-gate  *		This format is an adaptation of the format originally
2527c478bd9Sstevel@tonic-gate  *		described in POSIX 1387.4.  The POSIX draft has since been
2537c478bd9Sstevel@tonic-gate  *		squashed, but this particular component lives on.  The
2547c478bd9Sstevel@tonic-gate  *		conformace field has been added to allow further identification
2557c478bd9Sstevel@tonic-gate  *		of the the server.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate ns_printer_t *
posix_name(const char * name)2587c478bd9Sstevel@tonic-gate posix_name(const char *name)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	ns_printer_t *printer = NULL;
2617c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if ((name != NULL) && ((tmp = strpbrk(name, ":")) != NULL)) {
2647c478bd9Sstevel@tonic-gate 		char *server = NULL;
2657c478bd9Sstevel@tonic-gate 		char *queue = NULL;
2667c478bd9Sstevel@tonic-gate 		char *extension = NULL;
2677c478bd9Sstevel@tonic-gate 		char *addr = strdup(name);
2687c478bd9Sstevel@tonic-gate 		char buf[BUFSIZ];
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (*tmp == ':')
2717c478bd9Sstevel@tonic-gate 			split_name(addr, ": \t", &server, &queue, &extension);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		memset(buf, 0, sizeof (buf));
2747c478bd9Sstevel@tonic-gate 		if ((server != NULL) && (queue != NULL))
2757c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "%s,%s%s%s", server,
276*f4593de7SToomas Soome 			    queue, (extension != NULL ? "," : ""),
277*f4593de7SToomas Soome 			    (extension != NULL ? extension : ""));
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		/* build the structure here */
280*f4593de7SToomas Soome 		if (buf[0] != '\0') {
2817c478bd9Sstevel@tonic-gate 			ns_kvp_t **list, *kvp;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			kvp = ns_kvp_create(NS_KEY_BSDADDR, buf);
2847c478bd9Sstevel@tonic-gate 			list = (ns_kvp_t **)list_append(NULL, kvp);
2857c478bd9Sstevel@tonic-gate 			if (list != NULL)
2867c478bd9Sstevel@tonic-gate 				printer = ns_printer_create(strdup(name), NULL,
287*f4593de7SToomas Soome 				    "posix", list);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	return (printer);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * FUNCTION:
2967c478bd9Sstevel@tonic-gate  *	int ns_bsd_addr_cmp(ns_bsd_addr_t *at, ns_bsd_addr_t *a2)
2977c478bd9Sstevel@tonic-gate  * INPUTS:
2987c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t *a1 - a bsd addr
2997c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t *21 - another bsd addr
3007c478bd9Sstevel@tonic-gate  * DESCRIPTION:
3017c478bd9Sstevel@tonic-gate  *	This functions compare 2 bsd_addr structures to determine if the
3027c478bd9Sstevel@tonic-gate  *	information in them is the same.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static int
ns_bsd_addr_cmp(ns_bsd_addr_t * a1,ns_bsd_addr_t * a2)3057c478bd9Sstevel@tonic-gate ns_bsd_addr_cmp(ns_bsd_addr_t *a1, ns_bsd_addr_t *a2)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	int rc;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if ((a1 == NULL) || (a2 == NULL))
3107c478bd9Sstevel@tonic-gate 		return (1);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if ((rc = strcmp(a1->server, a2->server)) != 0)
3137c478bd9Sstevel@tonic-gate 		return (rc);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if ((a1->printer == NULL) || (a2->printer == NULL))
3167c478bd9Sstevel@tonic-gate 		return (a1->printer != a2->printer);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return (strcmp(a1->printer, a2->printer));
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * FUNCTION:    ns_bsd_addr_cmp_local()
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * DESCRIPTION: This function compares 2 bsd_addr structures to determine if
3287c478bd9Sstevel@tonic-gate  *              the information in them is the same. It destinquishes between
3297c478bd9Sstevel@tonic-gate  *              real printer names and alias names while doing the compare.
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  * INPUTS:      ns_bsd_addr_t *a1 - a bsd addr
332*f4593de7SToomas Soome  *              ns_bsd_addr_t *a2 - another bsd addr
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static int
ns_bsd_addr_cmp_local(ns_bsd_addr_t * a1,ns_bsd_addr_t * a2)3367c478bd9Sstevel@tonic-gate ns_bsd_addr_cmp_local(ns_bsd_addr_t *a1, ns_bsd_addr_t *a2)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	int rc;
3397c478bd9Sstevel@tonic-gate 
340*f4593de7SToomas Soome 	if ((a1 == NULL) || (a2 == NULL)) {
3417c478bd9Sstevel@tonic-gate 		return (1);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
344*f4593de7SToomas Soome 	if ((rc = strcmp(a1->server, a2->server)) != 0) {
3457c478bd9Sstevel@tonic-gate 		return (rc);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
348*f4593de7SToomas Soome 	if ((a1->printer == NULL) || (a2->printer == NULL)) {
3497c478bd9Sstevel@tonic-gate 		return (a1->printer != a2->printer);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	rc = strcmp(a1->printer, a2->printer);
353*f4593de7SToomas Soome 	if (rc == 0) {
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * The printer's real names are the same, but now check if
3567c478bd9Sstevel@tonic-gate 		 * their local names (alias) are the same.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate 		rc = strcmp(a1->pname, a2->pname);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (rc);
3627c478bd9Sstevel@tonic-gate } /* ns_bsd_addr_cmp_local */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  * FUNCTION:
3687c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t *ns_bsd_addr_get_name(char *name)
3697c478bd9Sstevel@tonic-gate  * INPUTS:
3707c478bd9Sstevel@tonic-gate  *	char *name - name of printer to get address for
3717c478bd9Sstevel@tonic-gate  * OUTPUTS:
3727c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t *(return) - the address of the printer
3737c478bd9Sstevel@tonic-gate  * DESCRIPTION:
3747c478bd9Sstevel@tonic-gate  *	This function will get the BSD address of the printer specified.
3757c478bd9Sstevel@tonic-gate  *	it fills in the printer name if none is specified in the "name service"
3767c478bd9Sstevel@tonic-gate  *	as a convenience to calling functions.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate ns_bsd_addr_t *
ns_bsd_addr_get_name(char * name)3797c478bd9Sstevel@tonic-gate ns_bsd_addr_get_name(char *name)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	ns_printer_t *printer;
3827c478bd9Sstevel@tonic-gate 	ns_bsd_addr_t *addr = NULL;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	endprinterentry();
3857c478bd9Sstevel@tonic-gate 	if ((printer = ns_printer_get_name(name, NULL)) != NULL) {
3867c478bd9Sstevel@tonic-gate 		addr = ns_get_value(NS_KEY_BSDADDR, printer);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		if (addr != NULL && addr->printer == NULL)
3897c478bd9Sstevel@tonic-gate 			addr->printer = strdup(printer->name);
3907c478bd9Sstevel@tonic-gate 		if (addr != NULL) {
3917c478bd9Sstevel@tonic-gate 			/*
3927c478bd9Sstevel@tonic-gate 			 * if the name given is not the same as that in the
3937c478bd9Sstevel@tonic-gate 			 * this is an alias/remote name so put that into the
3947c478bd9Sstevel@tonic-gate 			 * pname field otherwise duplicate the real printer
3957c478bd9Sstevel@tonic-gate 			 * name
3967c478bd9Sstevel@tonic-gate 			 */
3977c478bd9Sstevel@tonic-gate 			if (strcmp(name, printer->name) != 0) {
3987c478bd9Sstevel@tonic-gate 				addr->pname = strdup(name);
3997c478bd9Sstevel@tonic-gate 			} else {
4007c478bd9Sstevel@tonic-gate 				addr->pname = strdup(printer->name);
4017c478bd9Sstevel@tonic-gate 			}
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	return (addr);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * FUNCTION:
4117c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t **ns_bsd_addr_get_list()
4127c478bd9Sstevel@tonic-gate  * OUTPUT:
4137c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t **(return) - a list of bsd addresses for all printers
4147c478bd9Sstevel@tonic-gate  *				   in all "name services"
4157c478bd9Sstevel@tonic-gate  * DESCRIPTION:
4167c478bd9Sstevel@tonic-gate  *	This function will gather a list of all printer addresses in all
4177c478bd9Sstevel@tonic-gate  *	of the "name services".  All redundancy is removed.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate ns_bsd_addr_t **
ns_bsd_addr_get_list(int unique)4207c478bd9Sstevel@tonic-gate ns_bsd_addr_get_list(int unique)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	ns_printer_t **printers;
4237c478bd9Sstevel@tonic-gate 	ns_bsd_addr_t **list = NULL;
4247c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	for (printers = ns_printer_get_list(NULL);
427*f4593de7SToomas Soome 	    printers != NULL && *printers != NULL; printers++) {
4287c478bd9Sstevel@tonic-gate 		ns_bsd_addr_t *addr;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		if (strcmp(NS_NAME_ALL, (*printers)->name) == 0)
4317c478bd9Sstevel@tonic-gate 			continue;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		if ((addr = ns_get_value(NS_KEY_BSDADDR, *printers)) != NULL) {
4347c478bd9Sstevel@tonic-gate 			if (addr->printer == NULL)
4357c478bd9Sstevel@tonic-gate 				addr->printer = strdup((*printers)->name);
4367c478bd9Sstevel@tonic-gate 			addr->pname = strdup((*printers)->name);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if (unique == UNIQUE)
4407c478bd9Sstevel@tonic-gate 			list =
4417c478bd9Sstevel@tonic-gate 			    (ns_bsd_addr_t **)list_append_unique((void **)list,
442*f4593de7SToomas Soome 			    (void *)addr, (COMP_T)ns_bsd_addr_cmp);
443*f4593de7SToomas Soome 		else if (unique == LOCAL_UNIQUE)
4447c478bd9Sstevel@tonic-gate 			list =
4457c478bd9Sstevel@tonic-gate 			    (ns_bsd_addr_t **)list_append_unique((void **)list,
446*f4593de7SToomas Soome 			    (void *)addr, (COMP_T)ns_bsd_addr_cmp_local);
4477c478bd9Sstevel@tonic-gate 		else
4487c478bd9Sstevel@tonic-gate 			list = (ns_bsd_addr_t **)list_append((void **)list,
449*f4593de7SToomas Soome 			    (void *)addr);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		for (aliases = (*printers)->aliases;
452*f4593de7SToomas Soome 		    (aliases != NULL) && (*aliases != NULL); aliases++) {
4537c478bd9Sstevel@tonic-gate 			/*
4547c478bd9Sstevel@tonic-gate 			 * Include any alias names that belong to the printer
4557c478bd9Sstevel@tonic-gate 			 */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 			if ((addr =
458*f4593de7SToomas Soome 			    ns_get_value(NS_KEY_BSDADDR, *printers)) != NULL) {
459*f4593de7SToomas Soome 				if (addr->printer == NULL) {
4607c478bd9Sstevel@tonic-gate 					addr->printer = strdup(*aliases);
4617c478bd9Sstevel@tonic-gate 				}
4627c478bd9Sstevel@tonic-gate 				addr->pname = strdup(*aliases);
4637c478bd9Sstevel@tonic-gate 			}
4647c478bd9Sstevel@tonic-gate 
465*f4593de7SToomas Soome 			if (unique == UNIQUE) {
4667c478bd9Sstevel@tonic-gate 				list = (ns_bsd_addr_t **)
467*f4593de7SToomas Soome 				    list_append_unique((void **)list,
468*f4593de7SToomas Soome 				    (void *)addr, (COMP_T)ns_bsd_addr_cmp);
469*f4593de7SToomas Soome 			} else if (unique == LOCAL_UNIQUE) {
4707c478bd9Sstevel@tonic-gate 				list = (ns_bsd_addr_t **)
471*f4593de7SToomas Soome 				    list_append_unique((void **)list,
472*f4593de7SToomas Soome 				    (void *)addr,
473*f4593de7SToomas Soome 				    (COMP_T)ns_bsd_addr_cmp_local);
474*f4593de7SToomas Soome 			} else {
4757c478bd9Sstevel@tonic-gate 				list = (ns_bsd_addr_t **)
4767c478bd9Sstevel@tonic-gate 				    list_append((void **)list, (void *)addr);
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	return (list);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * FUNCTION:
4897c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t **ns_bsd_addr_get_list()
4907c478bd9Sstevel@tonic-gate  * OUTPUT:
4917c478bd9Sstevel@tonic-gate  *	ns_bsd_addr_t **(return) - a list of bsd addresses for "_all" printers
4927c478bd9Sstevel@tonic-gate  *				   in the "name service"
4937c478bd9Sstevel@tonic-gate  * DESCRIPTION:
4947c478bd9Sstevel@tonic-gate  *	This function will use the "_all" entry to find a list of printers and
4957c478bd9Sstevel@tonic-gate  *	addresses. The "default" printer is also added to the list.
4967c478bd9Sstevel@tonic-gate  *	All redundancy is removed.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate ns_bsd_addr_t **
ns_bsd_addr_get_all(int unique)4997c478bd9Sstevel@tonic-gate ns_bsd_addr_get_all(int unique)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	ns_printer_t *printer;
5027c478bd9Sstevel@tonic-gate 	ns_bsd_addr_t **list = NULL;
5037c478bd9Sstevel@tonic-gate 	char **printers;
5047c478bd9Sstevel@tonic-gate 	char *def = NULL;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (((def = (char *)getenv("PRINTER")) == NULL) &&
5077c478bd9Sstevel@tonic-gate 	    ((def = (char *)getenv("LPDEST")) == NULL))
5087c478bd9Sstevel@tonic-gate 		def = NS_NAME_DEFAULT;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	list = (ns_bsd_addr_t **)list_append((void **)list,
511*f4593de7SToomas Soome 	    (void *)ns_bsd_addr_get_name(def));
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	endprinterentry();
5147c478bd9Sstevel@tonic-gate 	if ((printer = ns_printer_get_name(NS_NAME_ALL, NULL)) == NULL)
5157c478bd9Sstevel@tonic-gate 		return (ns_bsd_addr_get_list(unique));
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	for (printers = (char **)ns_get_value(NS_KEY_ALL, printer);
518*f4593de7SToomas Soome 	    printers != NULL && *printers != NULL; printers++) {
5197c478bd9Sstevel@tonic-gate 		ns_bsd_addr_t *addr;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		addr = ns_bsd_addr_get_name(*printers);
5227c478bd9Sstevel@tonic-gate 		if (addr != NULL)
5237c478bd9Sstevel@tonic-gate 			addr->pname = *printers;
5247c478bd9Sstevel@tonic-gate 		if (unique == UNIQUE)
5257c478bd9Sstevel@tonic-gate 			list =
5267c478bd9Sstevel@tonic-gate 			    (ns_bsd_addr_t **)list_append_unique((void **)list,
527*f4593de7SToomas Soome 			    (void *)addr, (COMP_T)ns_bsd_addr_cmp);
5287c478bd9Sstevel@tonic-gate 		else
5297c478bd9Sstevel@tonic-gate 			list = (ns_bsd_addr_t **)list_append((void **)list,
530*f4593de7SToomas Soome 			    (void *)addr);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	return (list);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate ns_bsd_addr_t *
ns_bsd_addr_get_default()5377c478bd9Sstevel@tonic-gate ns_bsd_addr_get_default()
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	char *def = NULL;
5407c478bd9Sstevel@tonic-gate 	ns_bsd_addr_t *addr;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (((def = (char *)getenv("PRINTER")) == NULL) &&
5437c478bd9Sstevel@tonic-gate 	    ((def = (char *)getenv("LPDEST")) == NULL)) {
5447c478bd9Sstevel@tonic-gate 		def = NS_NAME_DEFAULT;
5457c478bd9Sstevel@tonic-gate 		addr = ns_bsd_addr_get_name(def);
5467c478bd9Sstevel@tonic-gate 		if (addr != NULL) {
5477c478bd9Sstevel@tonic-gate 			addr->pname = def;
5487c478bd9Sstevel@tonic-gate 			return (addr);
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	return (NULL);
5537c478bd9Sstevel@tonic-gate }
554