nss_printer.c revision 2264:b2b9267d002d
118334Speter/*
218334Speter * CDDL HEADER START
3132718Skan *
4169689Skan * The contents of this file are subject to the terms of the
5132718Skan * Common Development and Distribution License (the "License").
618334Speter * You may not use this file except in compliance with the License.
7132718Skan *
818334Speter * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
918334Speter * or http://www.opensolaris.org/os/licensing.
1018334Speter * See the License for the specific language governing permissions
1118334Speter * and limitations under the License.
12132718Skan *
1318334Speter * When distributing Covered Code, include this CDDL HEADER in each
1418334Speter * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1518334Speter * If applicable, add the following below this CDDL HEADER, with the
1618334Speter * fields enclosed by brackets "[]" replaced with your own identifying
1718334Speter * information: Portions Copyright [yyyy] [name of copyright owner]
18132718Skan *
19169689Skan * CDDL HEADER END
20169689Skan */
2118334Speter/*
2218334Speter * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2350397Sobrien * Use is subject to license terms.
24132718Skan */
25132718Skan
2618334Speter#pragma ident	"%Z%%M%	%I%	%E% SMI"
2718334Speter
2890075Sobrien#include <stdio.h>
2950397Sobrien#include <string.h>
3090075Sobrien#include <ctype.h>
3190075Sobrien#include <sys/types.h>
32117395Skan#include <nss_dbdefs.h>
33132718Skan#include <syslog.h>
3418334Speter#include <ns.h>
35169689Skan
3618334Speter#ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
3790075Sobrien#define	NSS_DBNAM__PRINTERS	"_printers"
3890075Sobrien#endif
3990075Sobrien
40132718Skanstatic DEFINE_NSS_DB_ROOT(db_root);
41132718Skanstatic DEFINE_NSS_GETENT(context);
4218334Speter
4390075Sobrienstatic int printers_stayopen;
4418334Speterstatic char *private_ns = NULL;
45132718Skanstatic char initialized = 0;
46132718Skan
47132718Skan
48132718Skanstatic void
49132718Skan_nss_initf_printers(p)
50132718Skan    nss_db_params_t *p;
51132718Skan{
52132718Skan	if (private_ns != NULL) {
53132718Skan		/*
54132718Skan		 * because we need to support a legacy interface that allows
5552284Sobrien		 * us to select a specific name service, we need to dummy up
56132718Skan		 * the parameters to use a private nsswitch database and set
57132718Skan		 * the * default_config entry to the name service we are
58132718Skan		 * looking into.
59132718Skan		 */
60132718Skan		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
61132718Skan		p->default_config = normalize_ns_name(private_ns);
62132718Skan		private_ns = NULL;
63132718Skan	} else if (initialized == 0) {
64132718Skan		/* regular behaviour */
65132718Skan		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
66169689Skan		p->default_config = NSS_DEFCONF_PRINTERS;
67132718Skan		initialized = 1;
68132718Skan	}
69132718Skan	syslog(LOG_DEBUG, "database: %s, services: %s",
70132718Skan		(p->name ? p->name : "NULL"),
71132718Skan		(p->default_config ? p->default_config : "NULL"));
72132718Skan}
73132718Skan
74132718Skan/*
75132718Skan * Return values: 0 = success, 1 = parse error, 2 = erange ...
76132718Skan * The structure pointer passed in is a structure in the caller's space
77132718Skan * wherein the field pointers would be set to areas in the buffer if
78132718Skan * need be. instring and buffer should be separate areas.
7918334Speter */
80132718Skan/* ARGSUSED */
81132718Skanstatic int
82132718Skanstr2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
83132718Skan{
84169689Skan	if (lenstr + 1 > buflen)
85132718Skan		return (NSS_STR_PARSE_ERANGE);
86132718Skan	/*
87132718Skan	 * We copy the input string into the output buffer
8818334Speter	 */
89169689Skan	(void) memcpy(buffer, instr, lenstr);
90169689Skan	buffer[lenstr] = '\0';
91132718Skan
9250397Sobrien	return (NSS_STR_PARSE_SUCCESS);
9318334Speter}
94132718Skan
9518334Speter
9690075Sobrienint
9790075Sobriensetprinterentry(int stayopen, char *ns)
9890075Sobrien{
9990075Sobrien	printers_stayopen |= stayopen;
100132718Skan	initialized = 0;
101132718Skan	private_ns = ns;
10218334Speter	nss_setent(&db_root, _nss_initf_printers, &context);
10318334Speter	return (0);
10490075Sobrien}
10552284Sobrien
10690075Sobrien
107132718Skanint
10852284Sobrienendprinterentry()
10990075Sobrien{
11052284Sobrien	printers_stayopen = 0;
11190075Sobrien	initialized = 0;
11290075Sobrien	nss_endent(&db_root, _nss_initf_printers, &context);
11390075Sobrien	nss_delete(&db_root);
11490075Sobrien	return (0);
11590075Sobrien}
11690075Sobrien
117169689Skan
118169689Skan/* ARGSUSED2 */
119169689Skanint
120169689Skangetprinterentry(char *linebuf, int linelen, char *ns)
12190075Sobrien{
12290075Sobrien	nss_XbyY_args_t arg;
12390075Sobrien	nss_status_t	res;
12490075Sobrien
125169689Skan	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
12690075Sobrien	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
12790075Sobrien	(void) NSS_XbyY_FINI(&arg);
12890075Sobrien	return (arg.status = res);
12990075Sobrien}
130169689Skan
13190075Sobrien
13252284Sobrienint
13352284Sobriengetprinterbyname(char *name, char *linebuf, int linelen, char *ns)
13490075Sobrien{
13552284Sobrien	nss_XbyY_args_t arg;
13690075Sobrien	nss_status_t	res;
137132718Skan
13890075Sobrien	private_ns = ns;
13990075Sobrien	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
14090075Sobrien	arg.key.name = name;
14190075Sobrien	res = nss_search(&db_root, _nss_initf_printers,
14290075Sobrien			NSS_DBOP_PRINTERS_BYNAME, &arg);
14390075Sobrien	(void) NSS_XbyY_FINI(&arg);
14452284Sobrien	return (arg.status = res);
14590075Sobrien}
14690075Sobrien