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 
22355b4669Sjacobs /*
2346b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24355b4669Sjacobs  *
25355b4669Sjacobs  */
26355b4669Sjacobs 
279c378f4bSjacobs /* Id: nss.c 180 2006-07-20 17:33:02Z njacobs $ */
28355b4669Sjacobs 
29355b4669Sjacobs #include <stdio.h>
30355b4669Sjacobs #include <stdlib.h>
31355b4669Sjacobs #include <unistd.h>
32355b4669Sjacobs #include <string.h>
33355b4669Sjacobs #include <ctype.h>
34355b4669Sjacobs #include <sys/types.h>
35355b4669Sjacobs #include <syslog.h>
36355b4669Sjacobs #include <papi.h>
37355b4669Sjacobs #include <uri.h>
38355b4669Sjacobs #include <papi_impl.h>
39355b4669Sjacobs #ifdef NSS_EMULATION
40355b4669Sjacobs #include <nss-emulation.h>
41355b4669Sjacobs #elif NSS_SOLARIS
42355b4669Sjacobs #include <nss_dbdefs.h>
43355b4669Sjacobs #endif
44355b4669Sjacobs #include <config-site.h>
45355b4669Sjacobs #if defined(__sun) && defined(__SVR4)
46355b4669Sjacobs #include <sys/systeminfo.h>
47355b4669Sjacobs #endif
48355b4669Sjacobs 
49355b4669Sjacobs 
50355b4669Sjacobs static char *
bsdaddr_to_uri(papi_attribute_t ** list,char * bsdaddr)519c378f4bSjacobs bsdaddr_to_uri(papi_attribute_t **list, char *bsdaddr)
52355b4669Sjacobs {
53355b4669Sjacobs 	char *result = NULL;
54355b4669Sjacobs 
55355b4669Sjacobs 	if (bsdaddr != NULL) {
56355b4669Sjacobs 		char *bsd[3], *tmp, *iter = NULL;
57355b4669Sjacobs 		char buf[512];
58355b4669Sjacobs 
59355b4669Sjacobs 		tmp = strdup(bsdaddr);
60355b4669Sjacobs 
61355b4669Sjacobs 		bsd[0] = strtok_r(tmp, ":,", &iter);
629c378f4bSjacobs 		if ((bsd[1] = strtok_r(NULL, ":,", &iter)) == NULL)
639c378f4bSjacobs 			papiAttributeListGetString(list, NULL,
6446b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 			    "printer-name", &bsd[1]);
65355b4669Sjacobs 		bsd[2] = strtok_r(NULL, ":,", &iter);
66355b4669Sjacobs 
679c378f4bSjacobs 		snprintf(buf, sizeof (buf), "lpd://%s/printers/%s%s%s", bsd[0],
6846b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    (bsd[1] != NULL) ? bsd[1] : "",
6946b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    (bsd[2] != NULL) ? "#" : "",
7046b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    (bsd[2] != NULL) ? bsd[2] : "");
71355b4669Sjacobs 
72355b4669Sjacobs 		free(tmp);
73355b4669Sjacobs 
74355b4669Sjacobs 		result = strdup(buf);
75355b4669Sjacobs 	}
76355b4669Sjacobs 
77355b4669Sjacobs 	return (result);
78355b4669Sjacobs }
79355b4669Sjacobs 
80355b4669Sjacobs #if defined(__sun) && defined(__SVR4)
81355b4669Sjacobs /*
82355b4669Sjacobs  * This is an awful HACK to force the dynamic PAPI library to use the
83355b4669Sjacobs  * lpsched support when the destination apears to be a local lpsched
84355b4669Sjacobs  * queue on Solaris.
85355b4669Sjacobs  */
86355b4669Sjacobs static void
solaris_lpsched_shortcircuit_hack(papi_attribute_t *** list)87355b4669Sjacobs solaris_lpsched_shortcircuit_hack(papi_attribute_t ***list)
88355b4669Sjacobs {
89355b4669Sjacobs 	papi_attribute_t *attribute;
90355b4669Sjacobs 	uri_t *uri = NULL;
91355b4669Sjacobs 	char *printer = NULL;
92355b4669Sjacobs 	char buf[128], buf2[128];
93355b4669Sjacobs 
94355b4669Sjacobs 	/* setting this in the calling env can be useful for debugging */
95355b4669Sjacobs 	if (getenv("DISABLE_LPSCHED_SHORTCIRCUIT") != NULL)
96355b4669Sjacobs 		return;
97355b4669Sjacobs 
98355b4669Sjacobs 	papiAttributeListGetString(*list, NULL,
9946b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	    "printer-uri-supported", &printer);
100c1ecd8b9Sjacobs 	/* if there is no printer-uri-supported, there is nothing to do */
1018c8a8d17SWendy Phillips 	if (printer == NULL) {
102355b4669Sjacobs 		return;
1038c8a8d17SWendy Phillips 	}
104355b4669Sjacobs 
105c1ecd8b9Sjacobs 	if (uri_from_string(printer, &uri) < 0) {
106c1ecd8b9Sjacobs 		papiAttributeListFree(*list);
107c1ecd8b9Sjacobs 		*list = NULL;
1088c8a8d17SWendy Phillips 		uri_free(uri);
109c1ecd8b9Sjacobs 		return;
110c1ecd8b9Sjacobs 	}
111c1ecd8b9Sjacobs 
112355b4669Sjacobs 	/* already an lpsched URI ? */
1138c8a8d17SWendy Phillips 	if (strcasecmp(uri->scheme, "lpsched") == 0) {
1148c8a8d17SWendy Phillips 		uri_free(uri);
115355b4669Sjacobs 		return;
1168c8a8d17SWendy Phillips 	}
117355b4669Sjacobs 
118e059026eSKeerthi Kondaka 	if (uri->path == NULL) {
119e059026eSKeerthi Kondaka 		printer = "";
120e059026eSKeerthi Kondaka 	} else {
121e059026eSKeerthi Kondaka 		if ((printer = strrchr(uri->path, '/')) == NULL)
122e059026eSKeerthi Kondaka 			printer = uri->path;
123e059026eSKeerthi Kondaka 		else
124e059026eSKeerthi Kondaka 			printer++;
125e059026eSKeerthi Kondaka 	}
126355b4669Sjacobs 
127355b4669Sjacobs 	/* is there an lpsched queue (printer/class) */
128355b4669Sjacobs 	snprintf(buf, sizeof (buf), "/etc/lp/interfaces/%s", printer);
129355b4669Sjacobs 	snprintf(buf2, sizeof (buf2), "/etc/lp/classes/%s", printer);
1308c8a8d17SWendy Phillips 	if ((access(buf, F_OK) < 0) && (access(buf2, F_OK) < 0)) {
1318c8a8d17SWendy Phillips 		uri_free(uri);
132355b4669Sjacobs 		return;
1338c8a8d17SWendy Phillips 	}
134355b4669Sjacobs 
1350ce6acb8Sjacobs 	/* is this the "local" host */
1368c8a8d17SWendy Phillips 	if ((uri->host != NULL) && (is_localhost(uri->host) == 0)) {
1378c8a8d17SWendy Phillips 		uri_free(uri);
1380ce6acb8Sjacobs 		return;
1398c8a8d17SWendy Phillips 	}
1400ce6acb8Sjacobs 
1410ce6acb8Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://%s/printers/%s",
14246b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	    (uri->host ? uri->host : "localhost"), printer);
143355b4669Sjacobs 	papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
14446b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	    "printer-uri-supported", buf);
1458c8a8d17SWendy Phillips 	uri_free(uri);
146355b4669Sjacobs }
147355b4669Sjacobs #endif
148355b4669Sjacobs 
149355b4669Sjacobs static void
fill_printer_uri_supported(papi_attribute_t *** list)150355b4669Sjacobs fill_printer_uri_supported(papi_attribute_t ***list)
151355b4669Sjacobs {
152355b4669Sjacobs 	papi_attribute_t *attribute;
153355b4669Sjacobs 	char *string = NULL;
154355b4669Sjacobs 
155355b4669Sjacobs 	/* do we have a printer-uri-supported */
156355b4669Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri-supported");
157355b4669Sjacobs 	if (attribute != NULL) /* we have what we need, return */
158355b4669Sjacobs 		return;
159355b4669Sjacobs 
16036615d24Sjacobs 	/* do we have a printer-uri (in URI form) to rename */
161355b4669Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri");
16236615d24Sjacobs 	if ((attribute != NULL) &&
16336615d24Sjacobs 	    (attribute->type == PAPI_STRING) &&
16436615d24Sjacobs 	    (attribute->values != NULL) &&
16536615d24Sjacobs 	    (attribute->values[0]->string != NULL) &&
16636615d24Sjacobs 	    (strstr(attribute->values[0]->string, "://") != NULL)) {
16736615d24Sjacobs 			/* rename it in place and return */
168355b4669Sjacobs 		free(attribute->name);
169355b4669Sjacobs 		attribute->name = strdup("printer-uri-supported");
170355b4669Sjacobs 		return;
171355b4669Sjacobs 	}
172355b4669Sjacobs 
173*bbf21555SRichard Lowe 	/* do we have a printers.conf(5) "bsdaddr" to convert */
174355b4669Sjacobs 	papiAttributeListGetString(*list, NULL, "bsdaddr", &string);
175355b4669Sjacobs 	if (string != NULL) { /* parse it, convert it, add it */
1769c378f4bSjacobs 		char *uri = bsdaddr_to_uri(*list, string);
177355b4669Sjacobs 
178355b4669Sjacobs 		if (uri != NULL) {
179355b4669Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
18046b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 			    "printer-uri-supported", uri);
181355b4669Sjacobs 			papiAttributeListDelete(list, "bsdaddr");
182355b4669Sjacobs 			free(uri);
183355b4669Sjacobs 			return;
184355b4669Sjacobs 		}
185355b4669Sjacobs 	}
186355b4669Sjacobs 
187*bbf21555SRichard Lowe 	/* do we have a printers.conf(5) "rm" (and "rp") to convert */
188355b4669Sjacobs 	papiAttributeListGetString(*list, NULL, "rm", &string);
189355b4669Sjacobs 	if (string != NULL) {
190355b4669Sjacobs 		char *rp = NULL;
191355b4669Sjacobs 
192355b4669Sjacobs 		/* default to "printer-name", but use "rp" if we have it */
193355b4669Sjacobs 		papiAttributeListGetString(*list, NULL, "printer-name", &rp);
194355b4669Sjacobs 		papiAttributeListGetString(*list, NULL, "rp", &rp);
195355b4669Sjacobs 
196355b4669Sjacobs 		if (rp != NULL) { /* fill in the uri if we have the data */
197355b4669Sjacobs 			char buf[BUFSIZ];
198355b4669Sjacobs 
199355b4669Sjacobs 			snprintf(buf, sizeof (buf), "lpd://%s/printers/%s",
20046b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 			    string, rp);
201355b4669Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
20246b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 			    "printer-uri-supported", strdup(buf));
203355b4669Sjacobs 			return;
204355b4669Sjacobs 		}
205355b4669Sjacobs 	}
206355b4669Sjacobs 
207355b4669Sjacobs 	/* if were are here, we don't have a printer-uri-supported */
208355b4669Sjacobs }
209355b4669Sjacobs 
210355b4669Sjacobs #ifdef NEED_BROKEN_PRINTER_URI_SEMANTIC
211355b4669Sjacobs static void
fill_printer_uri(papi_attribute_t *** list)212355b4669Sjacobs fill_printer_uri(papi_attribute_t ***list)
213355b4669Sjacobs {
214355b4669Sjacobs 	papi_attribute_t *attribute;
215355b4669Sjacobs 	char *uri = NULL;
216355b4669Sjacobs 
217355b4669Sjacobs 	if ((list == NULL) || (*list == NULL))
218355b4669Sjacobs 		return;
219355b4669Sjacobs 
2200ce6acb8Sjacobs 	/* do we have a printer-uri */
221355b4669Sjacobs 	attribute = papiAttributeListFind(*list, "printer-uri");
222355b4669Sjacobs 	if (attribute != NULL) /* we have what we need, return */
223355b4669Sjacobs 		return;
224355b4669Sjacobs 
225355b4669Sjacobs 	/*
226355b4669Sjacobs 	 * this is sufficient to fool libgnomeprintpapi, but not promote it's
227355b4669Sjacobs 	 * use in the future.
228355b4669Sjacobs 	 */
229355b4669Sjacobs 	papiAttributeListAddString(list, PAPI_ATTR_EXCL, "printer-uri",
23046b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	    "broken printer-uri semantic");
231355b4669Sjacobs }
232355b4669Sjacobs #endif /* NEED_BROKEN_PRINTER_URI_SEMANTIC */
233355b4669Sjacobs 
234355b4669Sjacobs static void
cvt_all_to_member_names(papi_attribute_t *** list)235355b4669Sjacobs cvt_all_to_member_names(papi_attribute_t ***list)
236355b4669Sjacobs {
237355b4669Sjacobs 	papi_status_t status;
238355b4669Sjacobs 	void *iter = NULL;
239355b4669Sjacobs 	char *string = NULL;
240355b4669Sjacobs 
241355b4669Sjacobs 	papiAttributeListGetString(*list, NULL, "member-names", &string);
242355b4669Sjacobs 	if (string != NULL) /* already have a member-names */
243355b4669Sjacobs 		return;
244355b4669Sjacobs 
245355b4669Sjacobs 	for (status = papiAttributeListGetString(*list, &iter, "all", &string);
2468c8a8d17SWendy Phillips 	    status == PAPI_OK;
2478c8a8d17SWendy Phillips 	    status = papiAttributeListGetString(*list, &iter, NULL, &string)) {
248355b4669Sjacobs 		char *s_iter = NULL, *value, *tmp = strdup(string);
249355b4669Sjacobs 
250355b4669Sjacobs 		for (value = strtok_r(tmp, ", \t", &s_iter);
2518c8a8d17SWendy Phillips 		    value != NULL;
2528c8a8d17SWendy Phillips 		    value = strtok_r(NULL, ", \t", &s_iter))
253355b4669Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_APPEND,
25446b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 			    "member-names", value);
255355b4669Sjacobs 		free(tmp);
256355b4669Sjacobs 	}
257355b4669Sjacobs }
258355b4669Sjacobs 
259355b4669Sjacobs static papi_attribute_t **
_cvt_nss_entry_to_printer(char * entry)260355b4669Sjacobs _cvt_nss_entry_to_printer(char *entry)
261355b4669Sjacobs {
26246b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	char    *key = NULL;
26346b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	char    *cp;
26446b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	char    buf[BUFSIZ];
265355b4669Sjacobs 	int in_namelist = 1, buf_pos = 0;
266355b4669Sjacobs 	papi_attribute_t **list = NULL;
267355b4669Sjacobs 
268355b4669Sjacobs 	if (entry == NULL)
269355b4669Sjacobs 		return (NULL);
270355b4669Sjacobs 
271355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
272355b4669Sjacobs 	for (cp = entry; *cp != '\0'; cp++) {
273355b4669Sjacobs 		switch (*cp) {
274355b4669Sjacobs 		case ':':	/* end of kvp */
275355b4669Sjacobs 			if (in_namelist != 0) {
276355b4669Sjacobs 				papiAttributeListAddString(&list,
27746b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 				    PAPI_ATTR_APPEND, "printer-name", buf);
278355b4669Sjacobs 				in_namelist = 0;
2798c8a8d17SWendy Phillips 			} else if (key != NULL) {
280355b4669Sjacobs 				papiAttributeListAddString(&list,
28146b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 				    PAPI_ATTR_APPEND, key, buf);
2828c8a8d17SWendy Phillips 				free(key);
2838c8a8d17SWendy Phillips 			}
284355b4669Sjacobs 			memset(buf, 0, sizeof (buf));
285355b4669Sjacobs 			buf_pos = 0;
286355b4669Sjacobs 			key = NULL;
287355b4669Sjacobs 			break;
288355b4669Sjacobs 		case '=':	/* kvp seperator */
289355b4669Sjacobs 			if (key == NULL) {
290355b4669Sjacobs 				key = strdup(buf);
291355b4669Sjacobs 				memset(buf, 0, sizeof (buf));
292355b4669Sjacobs 				buf_pos = 0;
293355b4669Sjacobs 			} else
294355b4669Sjacobs 				buf[buf_pos++] = *cp;
295355b4669Sjacobs 			break;
296355b4669Sjacobs 		case '|':	/* namelist seperator */
297355b4669Sjacobs 			if (in_namelist != 0) {
298355b4669Sjacobs 				papiAttributeListAddString(&list,
29946b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 				    PAPI_ATTR_APPEND, "printer-name", buf);
300355b4669Sjacobs 				memset(buf, 0, sizeof (buf));
301355b4669Sjacobs 				buf_pos = 0;
302355b4669Sjacobs 			} else	/* add it to the buffer */
303355b4669Sjacobs 				buf[buf_pos++] = *cp;
304355b4669Sjacobs 			break;
305355b4669Sjacobs 		case '\\':	/* escape char */
306355b4669Sjacobs 			buf[buf_pos++] = *(++cp);
307355b4669Sjacobs 			break;
308355b4669Sjacobs 		default:
309355b4669Sjacobs 			buf[buf_pos++] = *cp;
310355b4669Sjacobs 		}
311355b4669Sjacobs 
312355b4669Sjacobs 	}
313355b4669Sjacobs 
3148c8a8d17SWendy Phillips 	if (key != NULL) {
315355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_APPEND, key, buf);
3168c8a8d17SWendy Phillips 		free(key);
3178c8a8d17SWendy Phillips 	}
318355b4669Sjacobs 
319355b4669Sjacobs 	/* resolve any "use" references in the configuration DB */
320355b4669Sjacobs 	key = NULL;
321355b4669Sjacobs 	papiAttributeListGetString(list, NULL, "use", &key);
322355b4669Sjacobs 	if (key != NULL) {
323355b4669Sjacobs 		papi_attribute_t **use_attrs = getprinterbyname(key, NULL);
324355b4669Sjacobs 
325355b4669Sjacobs 		list_concatenate(&list, use_attrs);
326355b4669Sjacobs 	}
327355b4669Sjacobs 
328355b4669Sjacobs 	fill_printer_uri_supported(&list);
329355b4669Sjacobs 	cvt_all_to_member_names(&list); /* convert "all" to "member-names" */
330355b4669Sjacobs 
331355b4669Sjacobs 	return (list);
332355b4669Sjacobs }
333355b4669Sjacobs 
334355b4669Sjacobs #if defined(NSS_SOLARIS) && !defined(NSS_EMULATION)
335355b4669Sjacobs 
336355b4669Sjacobs #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
337355b4669Sjacobs #define	NSS_DBNAM__PRINTERS	"_printers"
338355b4669Sjacobs #endif
339355b4669Sjacobs 
340355b4669Sjacobs static DEFINE_NSS_DB_ROOT(db_root);
341355b4669Sjacobs static DEFINE_NSS_GETENT(context);
342355b4669Sjacobs 
343355b4669Sjacobs static char *private_ns = NULL;
344355b4669Sjacobs 
345355b4669Sjacobs static void
_nss_initf_printers(p)346355b4669Sjacobs _nss_initf_printers(p)
347355b4669Sjacobs     nss_db_params_t *p;
348355b4669Sjacobs {
349355b4669Sjacobs 	if (private_ns != NULL) {
350355b4669Sjacobs 		/*
351355b4669Sjacobs 		 * because we need to support a legacy interface that allows
352355b4669Sjacobs 		 * us to select a specific name service, we need to dummy up
353355b4669Sjacobs 		 * the parameters to use a private nsswitch database and set
354355b4669Sjacobs 		 * the * default_config entry to the name service we are
355355b4669Sjacobs 		 * looking into.
356355b4669Sjacobs 		 */
357355b4669Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
358355b4669Sjacobs 		p->default_config = private_ns;
359fe1de1a1Sjacobs 	} else {
360355b4669Sjacobs 		/* regular behaviour */
361355b4669Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
362355b4669Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
363355b4669Sjacobs 	}
364fe1de1a1Sjacobs 	syslog(LOG_DEBUG, "database: %s, default: %s",
365355b4669Sjacobs 		(p->name ? p->name : "NULL"),
366355b4669Sjacobs 		(p->default_config ? p->default_config : "NULL"));
367355b4669Sjacobs }
368355b4669Sjacobs 
369355b4669Sjacobs /*
370355b4669Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
371355b4669Sjacobs  * The structure pointer passed in is a structure in the caller's space
372355b4669Sjacobs  * wherein the field pointers would be set to areas in the buffer if
373355b4669Sjacobs  * need be. instring and buffer should be separate areas.
374355b4669Sjacobs  */
375355b4669Sjacobs /* ARGSUSED */
376355b4669Sjacobs static int
str2printer(const char * instr,int lenstr,void * ent,char * buffer,int buflen)377355b4669Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
378355b4669Sjacobs {
379355b4669Sjacobs 	if (lenstr + 1 > buflen)
380355b4669Sjacobs 		return (NSS_STR_PARSE_ERANGE);
38132232bf4Sjacobs 
38232232bf4Sjacobs 	/* skip entries that begin with '#' */
38332232bf4Sjacobs 	if (instr[0] == '#')
38432232bf4Sjacobs 		return (NSS_STR_PARSE_PARSE);
38532232bf4Sjacobs 
386355b4669Sjacobs 	/*
387355b4669Sjacobs 	 * We copy the input string into the output buffer
388355b4669Sjacobs 	 */
389355b4669Sjacobs 	(void) memcpy(buffer, instr, lenstr);
390355b4669Sjacobs 	buffer[lenstr] = '\0';
391355b4669Sjacobs 
392355b4669Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
393355b4669Sjacobs }
394355b4669Sjacobs #endif /* NSS_SOLARIS */
395355b4669Sjacobs 
396355b4669Sjacobs int
setprinterentry(int stayopen,char * ns)397355b4669Sjacobs setprinterentry(int stayopen, char *ns)
398355b4669Sjacobs {
399355b4669Sjacobs #ifdef NSS_EMULATION
400355b4669Sjacobs 	emul_setprinterentry(stayopen);
401355b4669Sjacobs #elif NSS_SOLARIS
402355b4669Sjacobs 	private_ns = ns;
403355b4669Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
404fe1de1a1Sjacobs 	private_ns = NULL;
405355b4669Sjacobs #endif
406355b4669Sjacobs 	return (0);
407355b4669Sjacobs }
408355b4669Sjacobs 
409355b4669Sjacobs 
410355b4669Sjacobs int
endprinterentry(int i)411355b4669Sjacobs endprinterentry(int i)
412355b4669Sjacobs {
413355b4669Sjacobs #ifdef NSS_EMULATION
414355b4669Sjacobs 	emul_endprinterentry();
415355b4669Sjacobs #elif NSS_SOLARIS
416355b4669Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
417355b4669Sjacobs 	nss_delete(&db_root);
418fe1de1a1Sjacobs 	private_ns = NULL;
419355b4669Sjacobs #endif
420355b4669Sjacobs 	return (0);
421355b4669Sjacobs }
422355b4669Sjacobs 
423355b4669Sjacobs /* ARGSUSED2 */
424355b4669Sjacobs papi_attribute_t **
getprinterentry(char * ns)425355b4669Sjacobs getprinterentry(char *ns)
426355b4669Sjacobs {
427355b4669Sjacobs 	papi_attribute_t **result = NULL;
428355b4669Sjacobs 
429355b4669Sjacobs #if defined(NSS_EMULATION) || defined(NSS_SOLARIS)
430355b4669Sjacobs 	char buf[10240];
431355b4669Sjacobs 	nss_status_t	res = NSS_NOTFOUND;
432355b4669Sjacobs 
433355b4669Sjacobs #ifdef NSS_EMULATION
434355b4669Sjacobs 	res = emul_getprinterentry_r(buf, sizeof (buf));
435355b4669Sjacobs #elif NSS_SOLARIS
436355b4669Sjacobs 	nss_XbyY_args_t arg;
437355b4669Sjacobs 
438fe1de1a1Sjacobs 	private_ns = ns;
43946b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	buf[0] = '\0';
440355b4669Sjacobs 	NSS_XbyY_INIT(&arg, buf, buf, sizeof (buf), str2printer);
441355b4669Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
442355b4669Sjacobs 	(void) NSS_XbyY_FINI(&arg);
443fe1de1a1Sjacobs 	private_ns = NULL;
444355b4669Sjacobs #endif
445355b4669Sjacobs 
446355b4669Sjacobs 	if (res != NSS_SUCCESS)
447355b4669Sjacobs 		buf[0] = '\0';
448355b4669Sjacobs 
449355b4669Sjacobs 	result = _cvt_nss_entry_to_printer(buf);
4500ce6acb8Sjacobs #if defined(__sun) && defined(__SVR4)
4510ce6acb8Sjacobs 	solaris_lpsched_shortcircuit_hack(&result);
4520ce6acb8Sjacobs #endif
453355b4669Sjacobs #ifdef NEED_BROKEN_PRINTER_URI_SEMANTIC
454355b4669Sjacobs 	fill_printer_uri(&result);
455355b4669Sjacobs #endif /* NEED_BROKEN_PRINTER_URI_SEMANTIC */
456355b4669Sjacobs #endif
457355b4669Sjacobs 
458355b4669Sjacobs #ifdef DEBUG
459355b4669Sjacobs 	printf("getprinterentry(%s): 0x%8.8x\n", (ns ? ns : "NULL"), result);
460355b4669Sjacobs 	if (result != NULL) {
461355b4669Sjacobs 		char buf[4096];
462355b4669Sjacobs 
463355b4669Sjacobs 		papiAttributeListToString(result, "\n\t", buf, sizeof (buf));
464355b4669Sjacobs 		printf("\t%s\n", buf);
465355b4669Sjacobs 	}
466355b4669Sjacobs #endif /* DEBUG */
467355b4669Sjacobs 
468355b4669Sjacobs 	return (result);
469355b4669Sjacobs }
470355b4669Sjacobs 
471355b4669Sjacobs 
472355b4669Sjacobs papi_attribute_t **
getprinterbyname(char * name,char * ns)473355b4669Sjacobs getprinterbyname(char *name, char *ns)
474355b4669Sjacobs {
475355b4669Sjacobs 	papi_attribute_t **result = NULL;
476355b4669Sjacobs 
477355b4669Sjacobs 	if (strstr(name, "://") != NULL) {	/* shortcut for URI form */
478355b4669Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
47946b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    "printer-name", name);
480355b4669Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
48146b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    "printer-uri-supported", name);
482355b4669Sjacobs 	} else if (strchr(name, ':') != NULL) {	/* shortcut for POSIX form */
4839c378f4bSjacobs 		char *uri = bsdaddr_to_uri(result, name);
484355b4669Sjacobs 
485355b4669Sjacobs 		papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
48646b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    "printer-name", name);
487355b4669Sjacobs 		if (uri != NULL) {
488355b4669Sjacobs 			papiAttributeListAddString(&result, PAPI_ATTR_APPEND,
48946b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 			    "printer-uri-supported", uri);
490355b4669Sjacobs 			free(uri);
491355b4669Sjacobs 		}
492355b4669Sjacobs 	} else {				/* anything else */
493355b4669Sjacobs #if defined(NSS_EMULATION) || defined(NSS_SOLARIS)
494355b4669Sjacobs 		char buf[10240];
495355b4669Sjacobs 		nss_status_t	res = NSS_NOTFOUND;
496355b4669Sjacobs 
497355b4669Sjacobs #ifdef NSS_EMULATION
498355b4669Sjacobs 		res = emul_getprinterbyname_r(name, buf, sizeof (buf));
499355b4669Sjacobs #elif NSS_SOLARIS
500355b4669Sjacobs 		nss_XbyY_args_t arg;
501355b4669Sjacobs 
502355b4669Sjacobs 		private_ns = ns;
503355b4669Sjacobs 		NSS_XbyY_INIT(&arg, buf, buf, sizeof (buf), str2printer);
504355b4669Sjacobs 		arg.key.name = name;
505355b4669Sjacobs 		res = nss_search(&db_root, _nss_initf_printers,
50646b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 		    NSS_DBOP_PRINTERS_BYNAME, &arg);
507355b4669Sjacobs 		(void) NSS_XbyY_FINI(&arg);
508fe1de1a1Sjacobs 		private_ns = NULL;
509355b4669Sjacobs 
510355b4669Sjacobs 		if (res != NSS_SUCCESS)
511355b4669Sjacobs 			buf[0] = '\0';
512355b4669Sjacobs #endif
513355b4669Sjacobs 
514355b4669Sjacobs 		result = _cvt_nss_entry_to_printer(buf);
515355b4669Sjacobs #endif
516355b4669Sjacobs 	}
5170ce6acb8Sjacobs #if defined(__sun) && defined(__SVR4)
5180ce6acb8Sjacobs 	solaris_lpsched_shortcircuit_hack(&result);
5190ce6acb8Sjacobs #endif
520355b4669Sjacobs #ifdef DEBUG
521355b4669Sjacobs 	printf("getprinterbyname(%s): %s = 0x%8.8x\n", (ns ? ns : "NULL"),
52246b2e54fSgowtham thommandra - Sun Microsystems - Bangalore India 	    name, result);
523355b4669Sjacobs 	if (result != NULL) {
524355b4669Sjacobs 		char buf[4096];
525355b4669Sjacobs 
526355b4669Sjacobs 		papiAttributeListToString(result, "\n\t", buf, sizeof (buf));
527355b4669Sjacobs 		printf("\t%s\n", buf);
528355b4669Sjacobs 	}
529355b4669Sjacobs #endif /* DEBUG */
530355b4669Sjacobs 
531355b4669Sjacobs 	return (result);
532355b4669Sjacobs }
533