xref: /onnv-gate/usr/src/lib/print/libpapi-dynamic/common/nss.c (revision 12224:aa7c10baac4c)
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 /*
23*12224SGowtham.Thommandra@Sun.COM  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
242264Sjacobs  *
252264Sjacobs  */
262264Sjacobs 
272409Sjacobs /* Id: nss.c 180 2006-07-20 17:33:02Z njacobs $ */
282264Sjacobs 
292264Sjacobs #include <stdio.h>
302264Sjacobs #include <stdlib.h>
312264Sjacobs #include <unistd.h>
322264Sjacobs #include <string.h>
332264Sjacobs #include <ctype.h>
342264Sjacobs #include <sys/types.h>
352264Sjacobs #include <syslog.h>
362264Sjacobs #include <papi.h>
372264Sjacobs #include <uri.h>
382264Sjacobs #include <papi_impl.h>
392264Sjacobs #ifdef NSS_EMULATION
402264Sjacobs #include <nss-emulation.h>
412264Sjacobs #elif NSS_SOLARIS
422264Sjacobs #include <nss_dbdefs.h>
432264Sjacobs #endif
442264Sjacobs #include <config-site.h>
452264Sjacobs #if defined(__sun) && defined(__SVR4)
462264Sjacobs #include <sys/systeminfo.h>
472264Sjacobs #endif
482264Sjacobs 
492264Sjacobs 
502264Sjacobs static char *
bsdaddr_to_uri(papi_attribute_t ** list,char * bsdaddr)512409Sjacobs bsdaddr_to_uri(papi_attribute_t **list, char *bsdaddr)
522264Sjacobs {
532264Sjacobs 	char *result = NULL;
542264Sjacobs 
552264Sjacobs 	if (bsdaddr != NULL) {
562264Sjacobs 		char *bsd[3], *tmp, *iter = NULL;
572264Sjacobs 		char buf[512];
582264Sjacobs 
592264Sjacobs 		tmp = strdup(bsdaddr);
602264Sjacobs 
612264Sjacobs 		bsd[0] = strtok_r(tmp, ":,", &iter);
622409Sjacobs 		if ((bsd[1] = strtok_r(NULL, ":,", &iter)) == NULL)
632409Sjacobs 			papiAttributeListGetString(list, NULL,
64*12224SGowtham.Thommandra@Sun.COM 			    "printer-name", &bsd[1]);
652264Sjacobs 		bsd[2] = strtok_r(NULL, ":,", &iter);
662264Sjacobs 
672409Sjacobs 		snprintf(buf, sizeof (buf), "lpd://%s/printers/%s%s%s", bsd[0],
68*12224SGowtham.Thommandra@Sun.COM 		    (bsd[1] != NULL) ? bsd[1] : "",
69*12224SGowtham.Thommandra@Sun.COM 		    (bsd[2] != NULL) ? "#" : "",
70*12224SGowtham.Thommandra@Sun.COM 		    (bsd[2] != NULL) ? bsd[2] : "");
712264Sjacobs 
722264Sjacobs 		free(tmp);
732264Sjacobs 
742264Sjacobs 		result = strdup(buf);
752264Sjacobs 	}
762264Sjacobs 
772264Sjacobs 	return (result);
782264Sjacobs }
792264Sjacobs 
802264Sjacobs #if defined(__sun) && defined(__SVR4)
812264Sjacobs /*
822264Sjacobs  * This is an awful HACK to force the dynamic PAPI library to use the
832264Sjacobs  * lpsched support when the destination apears to be a local lpsched
842264Sjacobs  * queue on Solaris.
852264Sjacobs  */
862264Sjacobs static void
solaris_lpsched_shortcircuit_hack(papi_attribute_t *** list)872264Sjacobs solaris_lpsched_shortcircuit_hack(papi_attribute_t ***list)
882264Sjacobs {
892264Sjacobs 	papi_attribute_t *attribute;
902264Sjacobs 	uri_t *uri = NULL;
912264Sjacobs 	char *printer = NULL;
922264Sjacobs 	char buf[128], buf2[128];
932264Sjacobs 
942264Sjacobs 	/* setting this in the calling env can be useful for debugging */
952264Sjacobs 	if (getenv("DISABLE_LPSCHED_SHORTCIRCUIT") != NULL)
962264Sjacobs 		return;
972264Sjacobs 
982264Sjacobs 	papiAttributeListGetString(*list, NULL,
99*12224SGowtham.Thommandra@Sun.COM 	    "printer-uri-supported", &printer);
1007253Sjacobs 	/* if there is no printer-uri-supported, there is nothing to do */
1017322SWendy.Phillips@Sun.COM 	if (printer == NULL) {
1022264Sjacobs 		return;
1037322SWendy.Phillips@Sun.COM 	}
1042264Sjacobs 
1057253Sjacobs 	if (uri_from_string(printer, &uri) < 0) {
1067253Sjacobs 		papiAttributeListFree(*list);
1077253Sjacobs 		*list = NULL;
1087322SWendy.Phillips@Sun.COM 		uri_free(uri);
1097253Sjacobs 		return;
1107253Sjacobs 	}
1117253Sjacobs 
1122264Sjacobs 	/* already an lpsched URI ? */
1137322SWendy.Phillips@Sun.COM 	if (strcasecmp(uri->scheme, "lpsched") == 0) {
1147322SWendy.Phillips@Sun.COM 		uri_free(uri);
1152264Sjacobs 		return;
1167322SWendy.Phillips@Sun.COM 	}
1172264Sjacobs 
1189411SKeerthi.Kondaka@Sun.COM 	if (uri->path == NULL) {
1199411SKeerthi.Kondaka@Sun.COM 		printer = "";
1209411SKeerthi.Kondaka@Sun.COM 	} else {
1219411SKeerthi.Kondaka@Sun.COM 		if ((printer = strrchr(uri->path, '/')) == NULL)
1229411SKeerthi.Kondaka@Sun.COM 			printer = uri->path;
1239411SKeerthi.Kondaka@Sun.COM 		else
1249411SKeerthi.Kondaka@Sun.COM 			printer++;
1259411SKeerthi.Kondaka@Sun.COM 	}
1262264Sjacobs 
1272264Sjacobs 	/* is there an lpsched queue (printer/class) */
1282264Sjacobs 	snprintf(buf, sizeof (buf), "/etc/lp/interfaces/%s", printer);
1292264Sjacobs 	snprintf(buf2, sizeof (buf2), "/etc/lp/classes/%s", printer);
1307322SWendy.Phillips@Sun.COM 	if ((access(buf, F_OK) < 0) && (access(buf2, F_OK) < 0)) {
1317322SWendy.Phillips@Sun.COM 		uri_free(uri);
1322264Sjacobs 		return;
1337322SWendy.Phillips@Sun.COM 	}
1342264Sjacobs 
1352313Sjacobs 	/* is this the "local" host */
1367322SWendy.Phillips@Sun.COM 	if ((uri->host != NULL) && (is_localhost(uri->host) == 0)) {
1377322SWendy.Phillips@Sun.COM 		uri_free(uri);
1382313Sjacobs 		return;
1397322SWendy.Phillips@Sun.COM 	}
1402313Sjacobs 
1412313Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://%s/printers/%s",
142*12224SGowtham.Thommandra@Sun.COM 	    (uri->host ? uri->host : "localhost"), printer);
1432264Sjacobs 	papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
144*12224SGowtham.Thommandra@Sun.COM 	    "printer-uri-supported", buf);
1457322SWendy.Phillips@Sun.COM 	uri_free(uri);
1462264Sjacobs }
1472264Sjacobs #endif
1482264Sjacobs 
1492264Sjacobs static void
fill_printer_uri_supported(papi_attribute_t *** list)1502264Sjacobs fill_printer_uri_supported(papi_attribute_t ***list)
1512264Sjacobs {
1522264Sjacobs 	papi_attribute_t *attribute;
1532264Sjacobs 	char *string = NULL;
1542264Sjacobs 
1552264Sjacobs 	/* do we have a printer-uri-supported */
1562264Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri-supported");
1572264Sjacobs 	if (attribute != NULL) /* we have what we need, return */
1582264Sjacobs 		return;
1592264Sjacobs 
1606817Sjacobs 	/* do we have a printer-uri (in URI form) to rename */
1612264Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri");
1626817Sjacobs 	if ((attribute != NULL) &&
1636817Sjacobs 	    (attribute->type == PAPI_STRING) &&
1646817Sjacobs 	    (attribute->values != NULL) &&
1656817Sjacobs 	    (attribute->values[0]->string != NULL) &&
1666817Sjacobs 	    (strstr(attribute->values[0]->string, "://") != NULL)) {
1676817Sjacobs 			/* rename it in place and return */
1682264Sjacobs 		free(attribute->name);
1692264Sjacobs 		attribute->name = strdup("printer-uri-supported");
1702264Sjacobs 		return;
1712264Sjacobs 	}
1722264Sjacobs 
1732264Sjacobs 	/* do we have a printers.conf(4) "bsdaddr" to convert */
1742264Sjacobs 	papiAttributeListGetString(*list, NULL, "bsdaddr", &string);
1752264Sjacobs 	if (string != NULL) { /* parse it, convert it, add it */
1762409Sjacobs 		char *uri = bsdaddr_to_uri(*list, string);
1772264Sjacobs 
1782264Sjacobs 		if (uri != NULL) {
1792264Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
180*12224SGowtham.Thommandra@Sun.COM 			    "printer-uri-supported", uri);
1812264Sjacobs 			papiAttributeListDelete(list, "bsdaddr");
1822264Sjacobs 			free(uri);
1832264Sjacobs 			return;
1842264Sjacobs 		}
1852264Sjacobs 	}
1862264Sjacobs 
1872264Sjacobs 	/* do we have a printers.conf(4) "rm" (and "rp") to convert */
1882264Sjacobs 	papiAttributeListGetString(*list, NULL, "rm", &string);
1892264Sjacobs 	if (string != NULL) {
1902264Sjacobs 		char *rp = NULL;
1912264Sjacobs 
1922264Sjacobs 		/* default to "printer-name", but use "rp" if we have it */
1932264Sjacobs 		papiAttributeListGetString(*list, NULL, "printer-name", &rp);
1942264Sjacobs 		papiAttributeListGetString(*list, NULL, "rp", &rp);
1952264Sjacobs 
1962264Sjacobs 		if (rp != NULL) { /* fill in the uri if we have the data */
1972264Sjacobs 			char buf[BUFSIZ];
1982264Sjacobs 
1992264Sjacobs 			snprintf(buf, sizeof (buf), "lpd://%s/printers/%s",
200*12224SGowtham.Thommandra@Sun.COM 			    string, rp);
2012264Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
202*12224SGowtham.Thommandra@Sun.COM 			    "printer-uri-supported", strdup(buf));
2032264Sjacobs 			return;
2042264Sjacobs 		}
2052264Sjacobs 	}
2062264Sjacobs 
2072264Sjacobs 	/* if were are here, we don't have a printer-uri-supported */
2082264Sjacobs }
2092264Sjacobs 
2102264Sjacobs #ifdef NEED_BROKEN_PRINTER_URI_SEMANTIC
2112264Sjacobs static void
fill_printer_uri(papi_attribute_t *** list)2122264Sjacobs fill_printer_uri(papi_attribute_t ***list)
2132264Sjacobs {
2142264Sjacobs 	papi_attribute_t *attribute;
2152264Sjacobs 	char *uri = NULL;
2162264Sjacobs 
2172264Sjacobs 	if ((list == NULL) || (*list == NULL))
2182264Sjacobs 		return;
2192264Sjacobs 
2202313Sjacobs 	/* do we have a printer-uri */
2212264Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri");
2222264Sjacobs 	if (attribute != NULL) /* we have what we need, return */
2232264Sjacobs 		return;
2242264Sjacobs 
2252264Sjacobs 	/*
2262264Sjacobs 	 * this is sufficient to fool libgnomeprintpapi, but not promote it's
2272264Sjacobs 	 * use in the future.
2282264Sjacobs 	 */
2292264Sjacobs 	papiAttributeListAddString(list, PAPI_ATTR_EXCL, "printer-uri",
230*12224SGowtham.Thommandra@Sun.COM 	    "broken printer-uri semantic");
2312264Sjacobs }
2322264Sjacobs #endif /* NEED_BROKEN_PRINTER_URI_SEMANTIC */
2332264Sjacobs 
2342264Sjacobs static void
cvt_all_to_member_names(papi_attribute_t *** list)2352264Sjacobs cvt_all_to_member_names(papi_attribute_t ***list)
2362264Sjacobs {
2372264Sjacobs 	papi_status_t status;
2382264Sjacobs 	void *iter = NULL;
2392264Sjacobs 	char *string = NULL;
2402264Sjacobs 
2412264Sjacobs 	papiAttributeListGetString(*list, NULL, "member-names", &string);
2422264Sjacobs 	if (string != NULL) /* already have a member-names */
2432264Sjacobs 		return;
2442264Sjacobs 
2452264Sjacobs 	for (status = papiAttributeListGetString(*list, &iter, "all", &string);
2467322SWendy.Phillips@Sun.COM 	    status == PAPI_OK;
2477322SWendy.Phillips@Sun.COM 	    status = papiAttributeListGetString(*list, &iter, NULL, &string)) {
2482264Sjacobs 		char *s_iter = NULL, *value, *tmp = strdup(string);
2492264Sjacobs 
2502264Sjacobs 		for (value = strtok_r(tmp, ", \t", &s_iter);
2517322SWendy.Phillips@Sun.COM 		    value != NULL;
2527322SWendy.Phillips@Sun.COM 		    value = strtok_r(NULL, ", \t", &s_iter))
2532264Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
254*12224SGowtham.Thommandra@Sun.COM 			    "member-names", value);
2552264Sjacobs 		free(tmp);
2562264Sjacobs 	}
2572264Sjacobs }
2582264Sjacobs 
2592264Sjacobs static papi_attribute_t **
_cvt_nss_entry_to_printer(char * entry)2602264Sjacobs _cvt_nss_entry_to_printer(char *entry)
2612264Sjacobs {
262*12224SGowtham.Thommandra@Sun.COM 	char    *key = NULL;
263*12224SGowtham.Thommandra@Sun.COM 	char    *cp;
264*12224SGowtham.Thommandra@Sun.COM 	char    buf[BUFSIZ];
2652264Sjacobs 	int in_namelist = 1, buf_pos = 0;
2662264Sjacobs 	papi_attribute_t **list = NULL;
2672264Sjacobs 
2682264Sjacobs 	if (entry == NULL)
2692264Sjacobs 		return (NULL);
2702264Sjacobs 
2712264Sjacobs 	memset(buf, 0, sizeof (buf));
2722264Sjacobs 	for (cp = entry; *cp != '\0'; cp++) {
2732264Sjacobs 		switch (*cp) {
2742264Sjacobs 		case ':':	/* end of kvp */
2752264Sjacobs 			if (in_namelist != 0) {
2762264Sjacobs 				papiAttributeListAddString(&list,
277*12224SGowtham.Thommandra@Sun.COM 				    PAPI_ATTR_APPEND, "printer-name", buf);
2782264Sjacobs 				in_namelist = 0;
2797322SWendy.Phillips@Sun.COM 			} else if (key != NULL) {
2802264Sjacobs 				papiAttributeListAddString(&list,
281*12224SGowtham.Thommandra@Sun.COM 				    PAPI_ATTR_APPEND, key, buf);
2827322SWendy.Phillips@Sun.COM 				free(key);
2837322SWendy.Phillips@Sun.COM 			}
2842264Sjacobs 			memset(buf, 0, sizeof (buf));
2852264Sjacobs 			buf_pos = 0;
2862264Sjacobs 			key = NULL;
2872264Sjacobs 			break;
2882264Sjacobs 		case '=':	/* kvp seperator */
2892264Sjacobs 			if (key == NULL) {
2902264Sjacobs 				key = strdup(buf);
2912264Sjacobs 				memset(buf, 0, sizeof (buf));
2922264Sjacobs 				buf_pos = 0;
2932264Sjacobs 			} else
2942264Sjacobs 				buf[buf_pos++] = *cp;
2952264Sjacobs 			break;
2962264Sjacobs 		case '|':	/* namelist seperator */
2972264Sjacobs 			if (in_namelist != 0) {
2982264Sjacobs 				papiAttributeListAddString(&list,
299*12224SGowtham.Thommandra@Sun.COM 				    PAPI_ATTR_APPEND, "printer-name", buf);
3002264Sjacobs 				memset(buf, 0, sizeof (buf));
3012264Sjacobs 				buf_pos = 0;
3022264Sjacobs 			} else	/* add it to the buffer */
3032264Sjacobs 				buf[buf_pos++] = *cp;
3042264Sjacobs 			break;
3052264Sjacobs 		case '\\':	/* escape char */
3062264Sjacobs 			buf[buf_pos++] = *(++cp);
3072264Sjacobs 			break;
3082264Sjacobs 		default:
3092264Sjacobs 			buf[buf_pos++] = *cp;
3102264Sjacobs 		}
3112264Sjacobs 
3122264Sjacobs 	}
3132264Sjacobs 
3147322SWendy.Phillips@Sun.COM 	if (key != NULL) {
3152264Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_APPEND, key, buf);
3167322SWendy.Phillips@Sun.COM 		free(key);
3177322SWendy.Phillips@Sun.COM 	}
3182264Sjacobs 
3192264Sjacobs 	/* resolve any "use" references in the configuration DB */
3202264Sjacobs 	key = NULL;
3212264Sjacobs 	papiAttributeListGetString(list, NULL, "use", &key);
3222264Sjacobs 	if (key != NULL) {
3232264Sjacobs 		papi_attribute_t **use_attrs = getprinterbyname(key, NULL);
3242264Sjacobs 
3252264Sjacobs 		list_concatenate(&list, use_attrs);
3262264Sjacobs 	}
3272264Sjacobs 
3282264Sjacobs 	fill_printer_uri_supported(&list);
3292264Sjacobs 	cvt_all_to_member_names(&list); /* convert "all" to "member-names" */
3302264Sjacobs 
3312264Sjacobs 	return (list);
3322264Sjacobs }
3332264Sjacobs 
3342264Sjacobs #if defined(NSS_SOLARIS) && !defined(NSS_EMULATION)
3352264Sjacobs 
3362264Sjacobs #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
3372264Sjacobs #define	NSS_DBNAM__PRINTERS	"_printers"
3382264Sjacobs #endif
3392264Sjacobs 
3402264Sjacobs static DEFINE_NSS_DB_ROOT(db_root);
3412264Sjacobs static DEFINE_NSS_GETENT(context);
3422264Sjacobs 
3432264Sjacobs static char *private_ns = NULL;
3442264Sjacobs 
3452264Sjacobs static void
_nss_initf_printers(p)3462264Sjacobs _nss_initf_printers(p)
3472264Sjacobs     nss_db_params_t *p;
3482264Sjacobs {
3492264Sjacobs 	if (private_ns != NULL) {
3502264Sjacobs 		/*
3512264Sjacobs 		 * because we need to support a legacy interface that allows
3522264Sjacobs 		 * us to select a specific name service, we need to dummy up
3532264Sjacobs 		 * the parameters to use a private nsswitch database and set
3542264Sjacobs 		 * the * default_config entry to the name service we are
3552264Sjacobs 		 * looking into.
3562264Sjacobs 		 */
3572264Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
3582264Sjacobs 		p->default_config = private_ns;
3592847Sjacobs 	} else {
3602264Sjacobs 		/* regular behaviour */
3612264Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
3622264Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
3632264Sjacobs 	}
3642847Sjacobs 	syslog(LOG_DEBUG, "database: %s, default: %s",
3652264Sjacobs 		(p->name ? p->name : "NULL"),
3662264Sjacobs 		(p->default_config ? p->default_config : "NULL"));
3672264Sjacobs }
3682264Sjacobs 
3692264Sjacobs /*
3702264Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
3712264Sjacobs  * The structure pointer passed in is a structure in the caller's space
3722264Sjacobs  * wherein the field pointers would be set to areas in the buffer if
3732264Sjacobs  * need be. instring and buffer should be separate areas.
3742264Sjacobs  */
3752264Sjacobs /* ARGSUSED */
3762264Sjacobs static int
str2printer(const char * instr,int lenstr,void * ent,char * buffer,int buflen)3772264Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
3782264Sjacobs {
3792264Sjacobs 	if (lenstr + 1 > buflen)
3802264Sjacobs 		return (NSS_STR_PARSE_ERANGE);
3812571Sjacobs 
3822571Sjacobs 	/* skip entries that begin with '#' */
3832571Sjacobs 	if (instr[0] == '#')
3842571Sjacobs 		return (NSS_STR_PARSE_PARSE);
3852571Sjacobs 
3862264Sjacobs 	/*
3872264Sjacobs 	 * We copy the input string into the output buffer
3882264Sjacobs 	 */
3892264Sjacobs 	(void) memcpy(buffer, instr, lenstr);
3902264Sjacobs 	buffer[lenstr] = '\0';
3912264Sjacobs 
3922264Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
3932264Sjacobs }
3942264Sjacobs #endif /* NSS_SOLARIS */
3952264Sjacobs 
3962264Sjacobs int
setprinterentry(int stayopen,char * ns)3972264Sjacobs setprinterentry(int stayopen, char *ns)
3982264Sjacobs {
3992264Sjacobs #ifdef NSS_EMULATION
4002264Sjacobs 	emul_setprinterentry(stayopen);
4012264Sjacobs #elif NSS_SOLARIS
4022264Sjacobs 	private_ns = ns;
4032264Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
4042847Sjacobs 	private_ns = NULL;
4052264Sjacobs #endif
4062264Sjacobs 	return (0);
4072264Sjacobs }
4082264Sjacobs 
4092264Sjacobs 
4102264Sjacobs int
endprinterentry(int i)4112264Sjacobs endprinterentry(int i)
4122264Sjacobs {
4132264Sjacobs #ifdef NSS_EMULATION
4142264Sjacobs 	emul_endprinterentry();
4152264Sjacobs #elif NSS_SOLARIS
4162264Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
4172264Sjacobs 	nss_delete(&db_root);
4182847Sjacobs 	private_ns = NULL;
4192264Sjacobs #endif
4202264Sjacobs 	return (0);
4212264Sjacobs }
4222264Sjacobs 
4232264Sjacobs /* ARGSUSED2 */
4242264Sjacobs papi_attribute_t **
getprinterentry(char * ns)4252264Sjacobs getprinterentry(char *ns)
4262264Sjacobs {
4272264Sjacobs 	papi_attribute_t **result = NULL;
4282264Sjacobs 
4292264Sjacobs #if defined(NSS_EMULATION) || defined(NSS_SOLARIS)
4302264Sjacobs 	char buf[10240];
4312264Sjacobs 	nss_status_t	res = NSS_NOTFOUND;
4322264Sjacobs 
4332264Sjacobs #ifdef NSS_EMULATION
4342264Sjacobs 	res = emul_getprinterentry_r(buf, sizeof (buf));
4352264Sjacobs #elif NSS_SOLARIS
4362264Sjacobs 	nss_XbyY_args_t arg;
4372264Sjacobs 
4382847Sjacobs 	private_ns = ns;
439*12224SGowtham.Thommandra@Sun.COM 	buf[0] = '\0';
4402264Sjacobs 	NSS_XbyY_INIT(&arg, buf, buf, sizeof (buf), str2printer);
4412264Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
4422264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
4432847Sjacobs 	private_ns = NULL;
4442264Sjacobs #endif
4452264Sjacobs 
4462264Sjacobs 	if (res != NSS_SUCCESS)
4472264Sjacobs 		buf[0] = '\0';
4482264Sjacobs 
4492264Sjacobs 	result = _cvt_nss_entry_to_printer(buf);
4502313Sjacobs #if defined(__sun) && defined(__SVR4)
4512313Sjacobs 	solaris_lpsched_shortcircuit_hack(&result);
4522313Sjacobs #endif
4532264Sjacobs #ifdef NEED_BROKEN_PRINTER_URI_SEMANTIC
4542264Sjacobs 	fill_printer_uri(&result);
4552264Sjacobs #endif /* NEED_BROKEN_PRINTER_URI_SEMANTIC */
4562264Sjacobs #endif
4572264Sjacobs 
4582264Sjacobs #ifdef DEBUG
4592264Sjacobs 	printf("getprinterentry(%s): 0x%8.8x\n", (ns ? ns : "NULL"), result);
4602264Sjacobs 	if (result != NULL) {
4612264Sjacobs 		char buf[4096];
4622264Sjacobs 
4632264Sjacobs 		papiAttributeListToString(result, "\n\t", buf, sizeof (buf));
4642264Sjacobs 		printf("\t%s\n", buf);
4652264Sjacobs 	}
4662264Sjacobs #endif /* DEBUG */
4672264Sjacobs 
4682264Sjacobs 	return (result);
4692264Sjacobs }
4702264Sjacobs 
4712264Sjacobs 
4722264Sjacobs papi_attribute_t **
getprinterbyname(char * name,char * ns)4732264Sjacobs getprinterbyname(char *name, char *ns)
4742264Sjacobs {
4752264Sjacobs 	papi_attribute_t **result = NULL;
4762264Sjacobs 
4772264Sjacobs 	if (strstr(name, "://") != NULL) {	/* shortcut for URI form */
4782264Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
479*12224SGowtham.Thommandra@Sun.COM 		    "printer-name", name);
4802264Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
481*12224SGowtham.Thommandra@Sun.COM 		    "printer-uri-supported", name);
4822264Sjacobs 	} else if (strchr(name, ':') != NULL) {	/* shortcut for POSIX form */
4832409Sjacobs 		char *uri = bsdaddr_to_uri(result, name);
4842264Sjacobs 
4852264Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
486*12224SGowtham.Thommandra@Sun.COM 		    "printer-name", name);
4872264Sjacobs 		if (uri != NULL) {
4882264Sjacobs 			papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
489*12224SGowtham.Thommandra@Sun.COM 			    "printer-uri-supported", uri);
4902264Sjacobs 			free(uri);
4912264Sjacobs 		}
4922264Sjacobs 	} else {				/* anything else */
4932264Sjacobs #if defined(NSS_EMULATION) || defined(NSS_SOLARIS)
4942264Sjacobs 		char buf[10240];
4952264Sjacobs 		nss_status_t	res = NSS_NOTFOUND;
4962264Sjacobs 
4972264Sjacobs #ifdef NSS_EMULATION
4982264Sjacobs 		res = emul_getprinterbyname_r(name, buf, sizeof (buf));
4992264Sjacobs #elif NSS_SOLARIS
5002264Sjacobs 		nss_XbyY_args_t arg;
5012264Sjacobs 
5022264Sjacobs 		private_ns = ns;
5032264Sjacobs 		NSS_XbyY_INIT(&arg, buf, buf, sizeof (buf), str2printer);
5042264Sjacobs 		arg.key.name = name;
5052264Sjacobs 		res = nss_search(&db_root, _nss_initf_printers,
506*12224SGowtham.Thommandra@Sun.COM 		    NSS_DBOP_PRINTERS_BYNAME, &arg);
5072264Sjacobs 		(void) NSS_XbyY_FINI(&arg);
5082847Sjacobs 		private_ns = NULL;
5092264Sjacobs 
5102264Sjacobs 		if (res != NSS_SUCCESS)
5112264Sjacobs 			buf[0] = '\0';
5122264Sjacobs #endif
5132264Sjacobs 
5142264Sjacobs 		result = _cvt_nss_entry_to_printer(buf);
5152264Sjacobs #endif
5162264Sjacobs 	}
5172313Sjacobs #if defined(__sun) && defined(__SVR4)
5182313Sjacobs 	solaris_lpsched_shortcircuit_hack(&result);
5192313Sjacobs #endif
5202264Sjacobs #ifdef DEBUG
5212264Sjacobs 	printf("getprinterbyname(%s): %s = 0x%8.8x\n", (ns ? ns : "NULL"),
522*12224SGowtham.Thommandra@Sun.COM 	    name, result);
5232264Sjacobs 	if (result != NULL) {
5242264Sjacobs 		char buf[4096];
5252264Sjacobs 
5262264Sjacobs 		papiAttributeListToString(result, "\n\t", buf, sizeof (buf));
5272264Sjacobs 		printf("\t%s\n", buf);
5282264Sjacobs 	}
5292264Sjacobs #endif /* DEBUG */
5302264Sjacobs 
5312264Sjacobs 	return (result);
5322264Sjacobs }
533