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 /*
22355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23355b4669Sjacobs  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <ns.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
357c478bd9Sstevel@tonic-gate #define	NSS_DBNAM__PRINTERS	"_printers"
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
397c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int printers_stayopen;
427c478bd9Sstevel@tonic-gate static char *private_ns = NULL;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static void
_nss_initf_printers(p)457c478bd9Sstevel@tonic-gate _nss_initf_printers(p)
467c478bd9Sstevel@tonic-gate     nss_db_params_t *p;
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	if (private_ns != NULL) {
497c478bd9Sstevel@tonic-gate 		/*
507c478bd9Sstevel@tonic-gate 		 * because we need to support a legacy interface that allows
517c478bd9Sstevel@tonic-gate 		 * us to select a specific name service, we need to dummy up
527c478bd9Sstevel@tonic-gate 		 * the parameters to use a private nsswitch database and set
537c478bd9Sstevel@tonic-gate 		 * the * default_config entry to the name service we are
547c478bd9Sstevel@tonic-gate 		 * looking into.
557c478bd9Sstevel@tonic-gate 		 */
567c478bd9Sstevel@tonic-gate 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
577c478bd9Sstevel@tonic-gate 		p->default_config = normalize_ns_name(private_ns);
58*fe1de1a1Sjacobs 	} else {
597c478bd9Sstevel@tonic-gate 		/* regular behaviour */
607c478bd9Sstevel@tonic-gate 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
617c478bd9Sstevel@tonic-gate 		p->default_config = NSS_DEFCONF_PRINTERS;
627c478bd9Sstevel@tonic-gate 	}
63*fe1de1a1Sjacobs 	syslog(LOG_DEBUG, "database: %s, default: %s",
647c478bd9Sstevel@tonic-gate 		(p->name ? p->name : "NULL"),
657c478bd9Sstevel@tonic-gate 		(p->default_config ? p->default_config : "NULL"));
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
707c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
717c478bd9Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
727c478bd9Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate /* ARGSUSED */
757c478bd9Sstevel@tonic-gate static int
str2printer(const char * instr,int lenstr,void * ent,char * buffer,int buflen)767c478bd9Sstevel@tonic-gate str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	if (lenstr + 1 > buflen)
797c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
8032232bf4Sjacobs 
8132232bf4Sjacobs 	/* skip entries that begin with '#' */
8232232bf4Sjacobs 	if (instr[0] == '#')
8332232bf4Sjacobs 		return (NSS_STR_PARSE_PARSE);
8432232bf4Sjacobs 
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * We copy the input string into the output buffer
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, instr, lenstr);
897c478bd9Sstevel@tonic-gate 	buffer[lenstr] = '\0';
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int
setprinterentry(int stayopen,char * ns)967c478bd9Sstevel@tonic-gate setprinterentry(int stayopen, char *ns)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	printers_stayopen |= stayopen;
997c478bd9Sstevel@tonic-gate 	private_ns = ns;
1007c478bd9Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_printers, &context);
101*fe1de1a1Sjacobs 	private_ns = NULL;
1027c478bd9Sstevel@tonic-gate 	return (0);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int
endprinterentry()1077c478bd9Sstevel@tonic-gate endprinterentry()
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	printers_stayopen = 0;
1107c478bd9Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_printers, &context);
1117c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
112*fe1de1a1Sjacobs 	private_ns = NULL;
1137c478bd9Sstevel@tonic-gate 	return (0);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
1187c478bd9Sstevel@tonic-gate int
getprinterentry(char * linebuf,int linelen,char * ns)1197c478bd9Sstevel@tonic-gate getprinterentry(char *linebuf, int linelen, char *ns)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1227c478bd9Sstevel@tonic-gate 	nss_status_t	res;
1237c478bd9Sstevel@tonic-gate 
124*fe1de1a1Sjacobs 	private_ns = ns;
1257c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
1267c478bd9Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
1277c478bd9Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
128*fe1de1a1Sjacobs 	private_ns = NULL;
129*fe1de1a1Sjacobs 
1307c478bd9Sstevel@tonic-gate 	return (arg.status = res);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate int
getprinterbyname(char * name,char * linebuf,int linelen,char * ns)1357c478bd9Sstevel@tonic-gate getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1387c478bd9Sstevel@tonic-gate 	nss_status_t	res;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	private_ns = ns;
1417c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
1427c478bd9Sstevel@tonic-gate 	arg.key.name = name;
1437c478bd9Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_printers,
1447c478bd9Sstevel@tonic-gate 			NSS_DBOP_PRINTERS_BYNAME, &arg);
1457c478bd9Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
146*fe1de1a1Sjacobs 	private_ns = NULL;
147*fe1de1a1Sjacobs 
1487c478bd9Sstevel@tonic-gate 	return (arg.status = res);
1497c478bd9Sstevel@tonic-gate }
150