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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
232264Sjacobs  * Use is subject to license terms.
242264Sjacobs  */
252264Sjacobs 
262264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
272264Sjacobs 
282264Sjacobs #include <stdio.h>
292264Sjacobs #include <string.h>
302264Sjacobs #include <ctype.h>
312264Sjacobs #include <sys/types.h>
322264Sjacobs #include <nss_dbdefs.h>
332264Sjacobs #include <syslog.h>
342264Sjacobs #include <ns.h>
352264Sjacobs 
362264Sjacobs #ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
372264Sjacobs #define	NSS_DBNAM__PRINTERS	"_printers"
382264Sjacobs #endif
392264Sjacobs 
402264Sjacobs static DEFINE_NSS_DB_ROOT(db_root);
412264Sjacobs static DEFINE_NSS_GETENT(context);
422264Sjacobs 
432264Sjacobs static int printers_stayopen;
442264Sjacobs static char *private_ns = NULL;
452264Sjacobs static char initialized = 0;
462264Sjacobs 
472264Sjacobs 
482264Sjacobs static void
492264Sjacobs _nss_initf_printers(p)
502264Sjacobs     nss_db_params_t *p;
512264Sjacobs {
522264Sjacobs 	if (private_ns != NULL) {
532264Sjacobs 		/*
542264Sjacobs 		 * because we need to support a legacy interface that allows
552264Sjacobs 		 * us to select a specific name service, we need to dummy up
562264Sjacobs 		 * the parameters to use a private nsswitch database and set
572264Sjacobs 		 * the * default_config entry to the name service we are
582264Sjacobs 		 * looking into.
592264Sjacobs 		 */
602264Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
612264Sjacobs 		p->default_config = normalize_ns_name(private_ns);
622264Sjacobs 		private_ns = NULL;
632264Sjacobs 	} else if (initialized == 0) {
642264Sjacobs 		/* regular behaviour */
652264Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
662264Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
672264Sjacobs 		initialized = 1;
682264Sjacobs 	}
692264Sjacobs 	syslog(LOG_DEBUG, "database: %s, services: %s",
702264Sjacobs 		(p->name ? p->name : "NULL"),
712264Sjacobs 		(p->default_config ? p->default_config : "NULL"));
722264Sjacobs }
732264Sjacobs 
742264Sjacobs /*
752264Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
762264Sjacobs  * The structure pointer passed in is a structure in the caller's space
772264Sjacobs  * wherein the field pointers would be set to areas in the buffer if
782264Sjacobs  * need be. instring and buffer should be separate areas.
792264Sjacobs  */
802264Sjacobs /* ARGSUSED */
812264Sjacobs static int
822264Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
832264Sjacobs {
842264Sjacobs 	if (lenstr + 1 > buflen)
852264Sjacobs 		return (NSS_STR_PARSE_ERANGE);
86*2571Sjacobs 
87*2571Sjacobs 	/* skip entries that begin with '#' */
88*2571Sjacobs 	if (instr[0] == '#')
89*2571Sjacobs 		return (NSS_STR_PARSE_PARSE);
90*2571Sjacobs 
912264Sjacobs 	/*
922264Sjacobs 	 * We copy the input string into the output buffer
932264Sjacobs 	 */
942264Sjacobs 	(void) memcpy(buffer, instr, lenstr);
952264Sjacobs 	buffer[lenstr] = '\0';
962264Sjacobs 
972264Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
982264Sjacobs }
992264Sjacobs 
1002264Sjacobs 
1012264Sjacobs int
1022264Sjacobs setprinterentry(int stayopen, char *ns)
1032264Sjacobs {
1042264Sjacobs 	printers_stayopen |= stayopen;
1052264Sjacobs 	initialized = 0;
1062264Sjacobs 	private_ns = ns;
1072264Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
1082264Sjacobs 	return (0);
1092264Sjacobs }
1102264Sjacobs 
1112264Sjacobs 
1122264Sjacobs int
1132264Sjacobs endprinterentry()
1142264Sjacobs {
1152264Sjacobs 	printers_stayopen = 0;
1162264Sjacobs 	initialized = 0;
1172264Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
1182264Sjacobs 	nss_delete(&db_root);
1192264Sjacobs 	return (0);
1202264Sjacobs }
1212264Sjacobs 
1222264Sjacobs 
1232264Sjacobs /* ARGSUSED2 */
1242264Sjacobs int
1252264Sjacobs getprinterentry(char *linebuf, int linelen, char *ns)
1262264Sjacobs {
1272264Sjacobs 	nss_XbyY_args_t arg;
1282264Sjacobs 	nss_status_t	res;
1292264Sjacobs 
1302264Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
1312264Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
1322264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
1332264Sjacobs 	return (arg.status = res);
1342264Sjacobs }
1352264Sjacobs 
1362264Sjacobs 
1372264Sjacobs int
1382264Sjacobs getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
1392264Sjacobs {
1402264Sjacobs 	nss_XbyY_args_t arg;
1412264Sjacobs 	nss_status_t	res;
1422264Sjacobs 
1432264Sjacobs 	private_ns = ns;
1442264Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
1452264Sjacobs 	arg.key.name = name;
1462264Sjacobs 	res = nss_search(&db_root, _nss_initf_printers,
1472264Sjacobs 			NSS_DBOP_PRINTERS_BYNAME, &arg);
1482264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
1492264Sjacobs 	return (arg.status = res);
1502264Sjacobs }
151