xref: /onnv-gate/usr/src/lib/print/libprint/common/nss_printer.c (revision 2847:05bae185f48c)
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 
462264Sjacobs static void
_nss_initf_printers(p)472264Sjacobs _nss_initf_printers(p)
482264Sjacobs     nss_db_params_t *p;
492264Sjacobs {
502264Sjacobs 	if (private_ns != NULL) {
512264Sjacobs 		/*
522264Sjacobs 		 * because we need to support a legacy interface that allows
532264Sjacobs 		 * us to select a specific name service, we need to dummy up
542264Sjacobs 		 * the parameters to use a private nsswitch database and set
552264Sjacobs 		 * the * default_config entry to the name service we are
562264Sjacobs 		 * looking into.
572264Sjacobs 		 */
582264Sjacobs 		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
592264Sjacobs 		p->default_config = normalize_ns_name(private_ns);
60*2847Sjacobs 	} else {
612264Sjacobs 		/* regular behaviour */
622264Sjacobs 		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
632264Sjacobs 		p->default_config = NSS_DEFCONF_PRINTERS;
642264Sjacobs 	}
65*2847Sjacobs 	syslog(LOG_DEBUG, "database: %s, default: %s",
662264Sjacobs 		(p->name ? p->name : "NULL"),
672264Sjacobs 		(p->default_config ? p->default_config : "NULL"));
682264Sjacobs }
692264Sjacobs 
702264Sjacobs /*
712264Sjacobs  * Return values: 0 = success, 1 = parse error, 2 = erange ...
722264Sjacobs  * The structure pointer passed in is a structure in the caller's space
732264Sjacobs  * wherein the field pointers would be set to areas in the buffer if
742264Sjacobs  * need be. instring and buffer should be separate areas.
752264Sjacobs  */
762264Sjacobs /* ARGSUSED */
772264Sjacobs static int
str2printer(const char * instr,int lenstr,void * ent,char * buffer,int buflen)782264Sjacobs str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
792264Sjacobs {
802264Sjacobs 	if (lenstr + 1 > buflen)
812264Sjacobs 		return (NSS_STR_PARSE_ERANGE);
822571Sjacobs 
832571Sjacobs 	/* skip entries that begin with '#' */
842571Sjacobs 	if (instr[0] == '#')
852571Sjacobs 		return (NSS_STR_PARSE_PARSE);
862571Sjacobs 
872264Sjacobs 	/*
882264Sjacobs 	 * We copy the input string into the output buffer
892264Sjacobs 	 */
902264Sjacobs 	(void) memcpy(buffer, instr, lenstr);
912264Sjacobs 	buffer[lenstr] = '\0';
922264Sjacobs 
932264Sjacobs 	return (NSS_STR_PARSE_SUCCESS);
942264Sjacobs }
952264Sjacobs 
962264Sjacobs 
972264Sjacobs int
setprinterentry(int stayopen,char * ns)982264Sjacobs setprinterentry(int stayopen, char *ns)
992264Sjacobs {
1002264Sjacobs 	printers_stayopen |= stayopen;
1012264Sjacobs 	private_ns = ns;
1022264Sjacobs 	nss_setent(&db_root, _nss_initf_printers, &context);
103*2847Sjacobs 	private_ns = NULL;
1042264Sjacobs 	return (0);
1052264Sjacobs }
1062264Sjacobs 
1072264Sjacobs 
1082264Sjacobs int
endprinterentry()1092264Sjacobs endprinterentry()
1102264Sjacobs {
1112264Sjacobs 	printers_stayopen = 0;
1122264Sjacobs 	nss_endent(&db_root, _nss_initf_printers, &context);
1132264Sjacobs 	nss_delete(&db_root);
114*2847Sjacobs 	private_ns = NULL;
1152264Sjacobs 	return (0);
1162264Sjacobs }
1172264Sjacobs 
1182264Sjacobs 
1192264Sjacobs /* ARGSUSED2 */
1202264Sjacobs int
getprinterentry(char * linebuf,int linelen,char * ns)1212264Sjacobs getprinterentry(char *linebuf, int linelen, char *ns)
1222264Sjacobs {
1232264Sjacobs 	nss_XbyY_args_t arg;
1242264Sjacobs 	nss_status_t	res;
1252264Sjacobs 
126*2847Sjacobs 	private_ns = ns;
1272264Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
1282264Sjacobs 	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
1292264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
130*2847Sjacobs 	private_ns = NULL;
131*2847Sjacobs 
1322264Sjacobs 	return (arg.status = res);
1332264Sjacobs }
1342264Sjacobs 
1352264Sjacobs 
1362264Sjacobs int
getprinterbyname(char * name,char * linebuf,int linelen,char * ns)1372264Sjacobs getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
1382264Sjacobs {
1392264Sjacobs 	nss_XbyY_args_t arg;
1402264Sjacobs 	nss_status_t	res;
1412264Sjacobs 
1422264Sjacobs 	private_ns = ns;
1432264Sjacobs 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
1442264Sjacobs 	arg.key.name = name;
1452264Sjacobs 	res = nss_search(&db_root, _nss_initf_printers,
1462264Sjacobs 			NSS_DBOP_PRINTERS_BYNAME, &arg);
1472264Sjacobs 	(void) NSS_XbyY_FINI(&arg);
148*2847Sjacobs 	private_ns = NULL;
149*2847Sjacobs 
1502264Sjacobs 	return (arg.status = res);
1512264Sjacobs }
152