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 
222264Sjacobs /*
236817Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282409Sjacobs /* Id: nss.c 180 2006-07-20 17:33:02Z njacobs $ */
292264Sjacobs 
302264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
352264Sjacobs #include <string.h>
362264Sjacobs #include <ctype.h>
372264Sjacobs #include <sys/types.h>
382264Sjacobs #include <syslog.h>
392264Sjacobs #include <papi.h>
402264Sjacobs #include <uri.h>
412264Sjacobs #include <papi_impl.h>
422264Sjacobs #ifdef NSS_EMULATION
432264Sjacobs #include <nss-emulation.h>
442264Sjacobs #elif NSS_SOLARIS
452264Sjacobs #include <nss_dbdefs.h>
462264Sjacobs #endif
472264Sjacobs #include <config-site.h>
482264Sjacobs #if defined(__sun) && defined(__SVR4)
492264Sjacobs #include <sys/systeminfo.h>
502264Sjacobs #endif
512264Sjacobs 
522264Sjacobs 
532264Sjacobs static char *
542409Sjacobs bsdaddr_to_uri(papi_attribute_t **list, char *bsdaddr)
552264Sjacobs {
562264Sjacobs 	char *result = NULL;
572264Sjacobs 
582264Sjacobs 	if (bsdaddr != NULL) {
592264Sjacobs 		char *bsd[3], *tmp, *iter = NULL;
602264Sjacobs 		char buf[512];
612264Sjacobs 
622264Sjacobs 		tmp = strdup(bsdaddr);
632264Sjacobs 
642264Sjacobs 		bsd[0] = strtok_r(tmp, ":,", &iter);
652409Sjacobs 		if ((bsd[1] = strtok_r(NULL, ":,", &iter)) == NULL)
662409Sjacobs 			papiAttributeListGetString(list, NULL,
672409Sjacobs 					"printer-name", &bsd[1]);
682264Sjacobs 		bsd[2] = strtok_r(NULL, ":,", &iter);
692264Sjacobs 
702409Sjacobs 		snprintf(buf, sizeof (buf), "lpd://%s/printers/%s%s%s", bsd[0],
712409Sjacobs 			(bsd[1] != NULL) ? bsd[1] : "",
722264Sjacobs 			(bsd[2] != NULL) ? "#" : "",
732264Sjacobs 			(bsd[2] != NULL) ? bsd[2] : "");
742264Sjacobs 
752264Sjacobs 		free(tmp);
762264Sjacobs 
772264Sjacobs 		result = strdup(buf);
782264Sjacobs 	}
792264Sjacobs 
802264Sjacobs 	return (result);
812264Sjacobs }
822264Sjacobs 
832264Sjacobs #if defined(__sun) && defined(__SVR4)
842264Sjacobs /*
852264Sjacobs  * This is an awful HACK to force the dynamic PAPI library to use the
862264Sjacobs  * lpsched support when the destination apears to be a local lpsched
872264Sjacobs  * queue on Solaris.
882264Sjacobs  */
892264Sjacobs static void
902264Sjacobs solaris_lpsched_shortcircuit_hack(papi_attribute_t ***list)
912264Sjacobs {
922264Sjacobs 	papi_attribute_t *attribute;
932264Sjacobs 	uri_t *uri = NULL;
942264Sjacobs 	char *printer = NULL;
952264Sjacobs 	char buf[128], buf2[128];
962264Sjacobs 
972264Sjacobs 	/* setting this in the calling env can be useful for debugging */
982264Sjacobs 	if (getenv("DISABLE_LPSCHED_SHORTCIRCUIT") != NULL)
992264Sjacobs 		return;
1002264Sjacobs 
1012264Sjacobs 	papiAttributeListGetString(*list, NULL,
1022264Sjacobs 				"printer-uri-supported", &printer);
103*7253Sjacobs 	/* if there is no printer-uri-supported, there is nothing to do */
104*7253Sjacobs 	if (printer == NULL)
1052264Sjacobs 		return;
1062264Sjacobs 
107*7253Sjacobs 	if (uri_from_string(printer, &uri) < 0) {
108*7253Sjacobs 		papiAttributeListFree(*list);
109*7253Sjacobs 		*list = NULL;
110*7253Sjacobs 		return;
111*7253Sjacobs 	}
112*7253Sjacobs 
1132264Sjacobs 	/* already an lpsched URI ? */
1142264Sjacobs 	if (strcasecmp(uri->scheme, "lpsched") == 0)
1152264Sjacobs 		return;
1162264Sjacobs 
1172264Sjacobs 	if ((printer = strrchr(uri->path, '/')) == NULL)
1182264Sjacobs 		printer = uri->path;
1192264Sjacobs 	else
1202264Sjacobs 		printer++;
1212264Sjacobs 
1222264Sjacobs 	/* is there an lpsched queue (printer/class) */
1232264Sjacobs 	snprintf(buf, sizeof (buf), "/etc/lp/interfaces/%s", printer);
1242264Sjacobs 	snprintf(buf2, sizeof (buf2), "/etc/lp/classes/%s", printer);
1252264Sjacobs 	if ((access(buf, F_OK) < 0) && (access(buf2, F_OK) < 0))
1262264Sjacobs 		return;
1272264Sjacobs 
1282313Sjacobs 	/* is this the "local" host */
1292313Sjacobs 	if ((uri->host != NULL) && (is_localhost(uri->host) == 0))
1302313Sjacobs 		return;
1312313Sjacobs 
1322313Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://%s/printers/%s",
1332313Sjacobs 			(uri->host ? uri->host : "localhost"), printer);
1342264Sjacobs 	papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
1352264Sjacobs 			"printer-uri-supported", buf);
1362264Sjacobs }
1372264Sjacobs #endif
1382264Sjacobs 
1392264Sjacobs static void
1402264Sjacobs fill_printer_uri_supported(papi_attribute_t ***list)
1412264Sjacobs {
1422264Sjacobs 	papi_attribute_t *attribute;
1432264Sjacobs 	char *string = NULL;
1442264Sjacobs 
1452264Sjacobs 	/* do we have a printer-uri-supported */
1462264Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri-supported");
1472264Sjacobs 	if (attribute != NULL) /* we have what we need, return */
1482264Sjacobs 		return;
1492264Sjacobs 
1506817Sjacobs 	/* do we have a printer-uri (in URI form) to rename */
1512264Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri");
1526817Sjacobs 	if ((attribute != NULL) &&
1536817Sjacobs 	    (attribute->type == PAPI_STRING) &&
1546817Sjacobs 	    (attribute->values != NULL) &&
1556817Sjacobs 	    (attribute->values[0]->string != NULL) &&
1566817Sjacobs 	    (strstr(attribute->values[0]->string, "://") != NULL)) {
1576817Sjacobs 			/* rename it in place and return */
1582264Sjacobs 		free(attribute->name);
1592264Sjacobs 		attribute->name = strdup("printer-uri-supported");
1602264Sjacobs 		return;
1612264Sjacobs 	}
1622264Sjacobs 
1632264Sjacobs 	/* do we have a printers.conf(4) "bsdaddr" to convert */
1642264Sjacobs 	papiAttributeListGetString(*list, NULL, "bsdaddr", &string);
1652264Sjacobs 	if (string != NULL) { /* parse it, convert it, add it */
1662409Sjacobs 		char *uri = bsdaddr_to_uri(*list, string);
1672264Sjacobs 
1682264Sjacobs 		if (uri != NULL) {
1692264Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
1702264Sjacobs 					"printer-uri-supported", uri);
1712264Sjacobs 			papiAttributeListDelete(list, "bsdaddr");
1722264Sjacobs 			free(uri);
1732264Sjacobs 			return;
1742264Sjacobs 		}
1752264Sjacobs 	}
1762264Sjacobs 
1772264Sjacobs 	/* do we have a printers.conf(4) "rm" (and "rp") to convert */
1782264Sjacobs 	papiAttributeListGetString(*list, NULL, "rm", &string);
1792264Sjacobs 	if (string != NULL) {
1802264Sjacobs 		char *rp = NULL;
1812264Sjacobs 
1822264Sjacobs 		/* default to "printer-name", but use "rp" if we have it */
1832264Sjacobs 		papiAttributeListGetString(*list, NULL, "printer-name", &rp);
1842264Sjacobs 		papiAttributeListGetString(*list, NULL, "rp", &rp);
1852264Sjacobs 
1862264Sjacobs 		if (rp != NULL) { /* fill in the uri if we have the data */
1872264Sjacobs 			char buf[BUFSIZ];
1882264Sjacobs 
1892264Sjacobs 			snprintf(buf, sizeof (buf), "lpd://%s/printers/%s",
1902264Sjacobs 				string, rp);
1912264Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
1922264Sjacobs 					"printer-uri-supported", strdup(buf));
1932264Sjacobs 			return;
1942264Sjacobs 		}
1952264Sjacobs 	}
1962264Sjacobs 
1972264Sjacobs 	/* if were are here, we don't have a printer-uri-supported */
1982264Sjacobs }
1992264Sjacobs 
2002264Sjacobs #ifdef NEED_BROKEN_PRINTER_URI_SEMANTIC
2012264Sjacobs static void
2022264Sjacobs fill_printer_uri(papi_attribute_t ***list)
2032264Sjacobs {
2042264Sjacobs 	papi_attribute_t *attribute;
2052264Sjacobs 	char *uri = NULL;
2062264Sjacobs 
2072264Sjacobs 	if ((list == NULL) || (*list == NULL))
2082264Sjacobs 		return;
2092264Sjacobs 
2102313Sjacobs 	/* do we have a printer-uri */
2112264Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri");
2122264Sjacobs 	if (attribute != NULL) /* we have what we need, return */
2132264Sjacobs 		return;
2142264Sjacobs 
2152264Sjacobs 	/*
2162264Sjacobs 	 * this is sufficient to fool libgnomeprintpapi, but not promote it's
2172264Sjacobs 	 * use in the future.
2182264Sjacobs 	 */
2192264Sjacobs 	papiAttributeListAddString(list, PAPI_ATTR_EXCL, "printer-uri",
2202264Sjacobs 			"broken printer-uri semantic");
2212264Sjacobs }
2222264Sjacobs #endif /* NEED_BROKEN_PRINTER_URI_SEMANTIC */
2232264Sjacobs 
2242264Sjacobs static void
2252264Sjacobs cvt_all_to_member_names(papi_attribute_t ***list)
2262264Sjacobs {
2272264Sjacobs 	papi_status_t status;
2282264Sjacobs 	void *iter = NULL;
2292264Sjacobs 	char *string = NULL;
2302264Sjacobs 
2312264Sjacobs 	papiAttributeListGetString(*list, NULL, "member-names", &string);
2322264Sjacobs 	if (string != NULL) /* already have a member-names */
2332264Sjacobs 		return;
2342264Sjacobs 
2352264Sjacobs 	for (status = papiAttributeListGetString(*list, &iter, "all", &string);
2362264Sjacobs 	     status == PAPI_OK;
2372264Sjacobs 	     status = papiAttributeListGetString(*list, &iter, NULL, &string)) {
2382264Sjacobs 		char *s_iter = NULL, *value, *tmp = strdup(string);
2392264Sjacobs 
2402264Sjacobs 		for (value = strtok_r(tmp, ", \t", &s_iter);
2412264Sjacobs 		     value != NULL;
2422264Sjacobs 		     value = strtok_r(NULL, ", \t", &s_iter))
2432264Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
2442264Sjacobs 					"member-names", value);
2452264Sjacobs 		free(tmp);
2462264Sjacobs 	}
2472264Sjacobs }
2482264Sjacobs 
2492264Sjacobs static papi_attribute_t **
2502264Sjacobs _cvt_nss_entry_to_printer(char *entry)
2512264Sjacobs {
2522264Sjacobs 	char    *key = NULL,
2532264Sjacobs 		*cp,
2542264Sjacobs 		buf[BUFSIZ];
2552264Sjacobs 	int in_namelist = 1, buf_pos = 0;
2562264Sjacobs 	papi_attribute_t **list = NULL;
2572264Sjacobs 
2582264Sjacobs 	if (entry == NULL)
2592264Sjacobs 		return (NULL);
2602264Sjacobs 
2612264Sjacobs 	memset(buf, 0, sizeof (buf));
2622264Sjacobs 	for (cp = entry; *cp != '\0'; cp++) {
2632264Sjacobs 		switch (*cp) {
2642264Sjacobs 		case ':':	/* end of kvp */
2652264Sjacobs 			if (in_namelist != 0) {
2662264Sjacobs 				papiAttributeListAddString(&list,
2672264Sjacobs 					PAPI_ATTR_APPEND, "printer-name", buf);
2682264Sjacobs 				in_namelist = 0;
2692264Sjacobs 			} else if (key != NULL)
2702264Sjacobs 				papiAttributeListAddString(&list,
2712264Sjacobs 					PAPI_ATTR_APPEND, key, buf);
2722264Sjacobs 			memset(buf, 0, sizeof (buf));
2732264Sjacobs 			buf_pos = 0;
2742264Sjacobs 			key = NULL;
2752264Sjacobs 			break;
2762264Sjacobs 		case '=':	/* kvp seperator */
2772264Sjacobs 			if (key == NULL) {
2782264Sjacobs 				key = strdup(buf);
2792264Sjacobs 				memset(buf, 0, sizeof (buf));
2802264Sjacobs 				buf_pos = 0;
2812264Sjacobs 			} else
2822264Sjacobs 				buf[buf_pos++] = *cp;
2832264Sjacobs 			break;
2842264Sjacobs 		case '|':	/* namelist seperator */
2852264Sjacobs 			if (in_namelist != 0) {
2862264Sjacobs 				papiAttributeListAddString(&list,
2872264Sjacobs 					PAPI_ATTR_APPEND, "printer-name", buf);
2882264Sjacobs 				memset(buf, 0, sizeof (buf));
2892264Sjacobs 				buf_pos = 0;
2902264Sjacobs 			} else	/* add it to the buffer */
2912264Sjacobs 				buf[buf_pos++] = *cp;
2922264Sjacobs 			break;
2932264Sjacobs 		case '\\':	/* escape char */
2942264Sjacobs 			buf[buf_pos++] = *(++cp);
2952264Sjacobs 			break;
2962264Sjacobs 		default:
2972264Sjacobs 			buf[buf_pos++] = *cp;
2982264Sjacobs 		}
2992264Sjacobs 
3002264Sjacobs 	}
3012264Sjacobs 
3022264Sjacobs 	if (key != NULL)
3032264Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_APPEND, key, buf);
3042264Sjacobs 
3052264Sjacobs 	/* resolve any "use" references in the configuration DB */
3062264Sjacobs 	key = NULL;
3072264Sjacobs 	papiAttributeListGetString(list, NULL, "use", &key);
3082264Sjacobs 	if (key != NULL) {
3092264Sjacobs 		papi_attribute_t **use_attrs = getprinterbyname(key, NULL);
3102264Sjacobs 
3112264Sjacobs 		list_concatenate(&list, use_attrs);
3122264Sjacobs 	}
3132264Sjacobs 
3142264Sjacobs 	fill_printer_uri_supported(&list);
3152264Sjacobs 	cvt_all_to_member_names(&list); /* convert "all" to "member-names" */
3162264Sjacobs 
3172264Sjacobs 	return (list);
3182264Sjacobs }
3192264Sjacobs 
3202264Sjacobs #if defined(NSS_SOLARIS) && !defined(NSS_EMULATION)
3212264Sjacobs 
3222264Sjacobs #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
3232264Sjacobs #define	NSS_DBNAM__PRINTERS	"_printers"
3242264Sjacobs #endif
3252264Sjacobs 
3262264Sjacobs static DEFINE_NSS_DB_ROOT(db_root);
3272264Sjacobs static DEFINE_NSS_GETENT(context);
3282264Sjacobs 
3292264Sjacobs static char *private_ns = NULL;
3302264Sjacobs 
3312264Sjacobs static void
3322264Sjacobs _nss_initf_printers(p)
3332264Sjacobs     nss_db_params_t *p;
3342264Sjacobs {
3352264Sjacobs 	if (private_ns != NULL) {
3362264Sjacobs 		/*
3372264Sjacobs 		 * because we need to support a legacy interface that allows
3382264Sjacobs 		 * us to select a specific name service, we need to dummy up
3392264Sjacobs 		 * the parameters to use a private nsswitch database and set
3402264Sjacobs 		 * the * default_config entry to the name service we are
3412264Sjacobs 		 * looking into.
3422264Sjacobs 		 */
3432264Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
3442264Sjacobs 		p->default_config = private_ns;
3452847Sjacobs 	} else {
3462264Sjacobs 		/* regular behaviour */
3472264Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
3482264Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
3492264Sjacobs 	}
3502847Sjacobs 	syslog(LOG_DEBUG, "database: %s, default: %s",
3512264Sjacobs 		(p->name ? p->name : "NULL"),
3522264Sjacobs 		(p->default_config ? p->default_config : "NULL"));
3532264Sjacobs }
3542264Sjacobs 
3552264Sjacobs /*
3562264Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
3572264Sjacobs  * The structure pointer passed in is a structure in the caller's space
3582264Sjacobs  * wherein the field pointers would be set to areas in the buffer if
3592264Sjacobs  * need be. instring and buffer should be separate areas.
3602264Sjacobs  */
3612264Sjacobs /* ARGSUSED */
3622264Sjacobs static int
3632264Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
3642264Sjacobs {
3652264Sjacobs 	if (lenstr + 1 > buflen)
3662264Sjacobs 		return (NSS_STR_PARSE_ERANGE);
3672571Sjacobs 
3682571Sjacobs 	/* skip entries that begin with '#' */
3692571Sjacobs 	if (instr[0] == '#')
3702571Sjacobs 		return (NSS_STR_PARSE_PARSE);
3712571Sjacobs 
3722264Sjacobs 	/*
3732264Sjacobs 	 * We copy the input string into the output buffer
3742264Sjacobs 	 */
3752264Sjacobs 	(void) memcpy(buffer, instr, lenstr);
3762264Sjacobs 	buffer[lenstr] = '\0';
3772264Sjacobs 
3782264Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
3792264Sjacobs }
3802264Sjacobs #endif /* NSS_SOLARIS */
3812264Sjacobs 
3822264Sjacobs int
3832264Sjacobs setprinterentry(int stayopen, char *ns)
3842264Sjacobs {
3852264Sjacobs #ifdef NSS_EMULATION
3862264Sjacobs 	emul_setprinterentry(stayopen);
3872264Sjacobs #elif NSS_SOLARIS
3882264Sjacobs 	private_ns = ns;
3892264Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
3902847Sjacobs 	private_ns = NULL;
3912264Sjacobs #endif
3922264Sjacobs 	return (0);
3932264Sjacobs }
3942264Sjacobs 
3952264Sjacobs 
3962264Sjacobs int
3972264Sjacobs endprinterentry(int i)
3982264Sjacobs {
3992264Sjacobs #ifdef NSS_EMULATION
4002264Sjacobs 	emul_endprinterentry();
4012264Sjacobs #elif NSS_SOLARIS
4022264Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
4032264Sjacobs 	nss_delete(&db_root);
4042847Sjacobs 	private_ns = NULL;
4052264Sjacobs #endif
4062264Sjacobs 	return (0);
4072264Sjacobs }
4082264Sjacobs 
4092264Sjacobs /* ARGSUSED2 */
4102264Sjacobs papi_attribute_t **
4112264Sjacobs getprinterentry(char *ns)
4122264Sjacobs {
4132264Sjacobs 	papi_attribute_t **result = NULL;
4142264Sjacobs 
4152264Sjacobs #if defined(NSS_EMULATION) || defined(NSS_SOLARIS)
4162264Sjacobs 	char buf[10240];
4172264Sjacobs 	nss_status_t	res = NSS_NOTFOUND;
4182264Sjacobs 
4192264Sjacobs #ifdef NSS_EMULATION
4202264Sjacobs 	res = emul_getprinterentry_r(buf, sizeof (buf));
4212264Sjacobs #elif NSS_SOLARIS
4222264Sjacobs 	nss_XbyY_args_t arg;
4232264Sjacobs 
4242847Sjacobs 	private_ns = ns;
4252264Sjacobs 	NSS_XbyY_INIT(&arg, buf, buf, sizeof (buf), str2printer);
4262264Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
4272264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
4282847Sjacobs 	private_ns = NULL;
4292264Sjacobs #endif
4302264Sjacobs 
4312264Sjacobs 	if (res != NSS_SUCCESS)
4322264Sjacobs 		buf[0] = '\0';
4332264Sjacobs 
4342264Sjacobs 	result = _cvt_nss_entry_to_printer(buf);
4352313Sjacobs #if defined(__sun) && defined(__SVR4)
4362313Sjacobs 	solaris_lpsched_shortcircuit_hack(&result);
4372313Sjacobs #endif
4382264Sjacobs #ifdef NEED_BROKEN_PRINTER_URI_SEMANTIC
4392264Sjacobs 	fill_printer_uri(&result);
4402264Sjacobs #endif /* NEED_BROKEN_PRINTER_URI_SEMANTIC */
4412264Sjacobs #endif
4422264Sjacobs 
4432264Sjacobs #ifdef DEBUG
4442264Sjacobs 	printf("getprinterentry(%s): 0x%8.8x\n", (ns ? ns : "NULL"), result);
4452264Sjacobs 	if (result != NULL) {
4462264Sjacobs 		char buf[4096];
4472264Sjacobs 
4482264Sjacobs 		papiAttributeListToString(result, "\n\t", buf, sizeof (buf));
4492264Sjacobs 		printf("\t%s\n", buf);
4502264Sjacobs 	}
4512264Sjacobs #endif /* DEBUG */
4522264Sjacobs 
4532264Sjacobs 	return (result);
4542264Sjacobs }
4552264Sjacobs 
4562264Sjacobs 
4572264Sjacobs papi_attribute_t **
4582264Sjacobs getprinterbyname(char *name, char *ns)
4592264Sjacobs {
4602264Sjacobs 	papi_attribute_t **result = NULL;
4612264Sjacobs 
4622264Sjacobs 	if (strstr(name, "://") != NULL) {	/* shortcut for URI form */
4632264Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
4642264Sjacobs 				"printer-name", name);
4652264Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
4662264Sjacobs 				"printer-uri-supported", name);
4672264Sjacobs 	} else if (strchr(name, ':') != NULL) {	/* shortcut for POSIX form */
4682409Sjacobs 		char *uri = bsdaddr_to_uri(result, name);
4692264Sjacobs 
4702264Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
4712264Sjacobs 				"printer-name", name);
4722264Sjacobs 		if (uri != NULL) {
4732264Sjacobs 			papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
4742264Sjacobs 					"printer-uri-supported", uri);
4752264Sjacobs 			free(uri);
4762264Sjacobs 		}
4772264Sjacobs 	} else {				/* anything else */
4782264Sjacobs #if defined(NSS_EMULATION) || defined(NSS_SOLARIS)
4792264Sjacobs 		char buf[10240];
4802264Sjacobs 		nss_status_t	res = NSS_NOTFOUND;
4812264Sjacobs 
4822264Sjacobs #ifdef NSS_EMULATION
4832264Sjacobs 		res = emul_getprinterbyname_r(name, buf, sizeof (buf));
4842264Sjacobs #elif NSS_SOLARIS
4852264Sjacobs 		nss_XbyY_args_t arg;
4862264Sjacobs 
4872264Sjacobs 		private_ns = ns;
4882264Sjacobs 		NSS_XbyY_INIT(&arg, buf, buf, sizeof (buf), str2printer);
4892264Sjacobs 		arg.key.name = name;
4902264Sjacobs 		res = nss_search(&db_root, _nss_initf_printers,
4912264Sjacobs 				NSS_DBOP_PRINTERS_BYNAME, &arg);
4922264Sjacobs 		(void) NSS_XbyY_FINI(&arg);
4932847Sjacobs 		private_ns = NULL;
4942264Sjacobs 
4952264Sjacobs 		if (res != NSS_SUCCESS)
4962264Sjacobs 			buf[0] = '\0';
4972264Sjacobs #endif
4982264Sjacobs 
4992264Sjacobs 		result = _cvt_nss_entry_to_printer(buf);
5002264Sjacobs #endif
5012264Sjacobs 	}
5022313Sjacobs #if defined(__sun) && defined(__SVR4)
5032313Sjacobs 	solaris_lpsched_shortcircuit_hack(&result);
5042313Sjacobs #endif
5052264Sjacobs #ifdef DEBUG
5062264Sjacobs 	printf("getprinterbyname(%s): %s = 0x%8.8x\n", (ns ? ns : "NULL"),
5072264Sjacobs 		name, result);
5082264Sjacobs 	if (result != NULL) {
5092264Sjacobs 		char buf[4096];
5102264Sjacobs 
5112264Sjacobs 		papiAttributeListToString(result, "\n\t", buf, sizeof (buf));
5122264Sjacobs 		printf("\t%s\n", buf);
5132264Sjacobs 	}
5142264Sjacobs #endif /* DEBUG */
5152264Sjacobs 
5162264Sjacobs 	return (result);
5172264Sjacobs }
518