nss_printer.c revision 2571:2fe9e3540b04
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include <stdio.h>
29#include <string.h>
30#include <ctype.h>
31#include <sys/types.h>
32#include <nss_dbdefs.h>
33#include <syslog.h>
34#include <ns.h>
35
36#ifndef	NSS_DBNAM__PRINTERS	/* not in nss_dbdefs.h because it's private */
37#define	NSS_DBNAM__PRINTERS	"_printers"
38#endif
39
40static DEFINE_NSS_DB_ROOT(db_root);
41static DEFINE_NSS_GETENT(context);
42
43static int printers_stayopen;
44static char *private_ns = NULL;
45static char initialized = 0;
46
47
48static void
49_nss_initf_printers(p)
50    nss_db_params_t *p;
51{
52	if (private_ns != NULL) {
53		/*
54		 * because we need to support a legacy interface that allows
55		 * us to select a specific name service, we need to dummy up
56		 * the parameters to use a private nsswitch database and set
57		 * the * default_config entry to the name service we are
58		 * looking into.
59		 */
60		p->name = NSS_DBNAM__PRINTERS;		/* "_printers" */
61		p->default_config = normalize_ns_name(private_ns);
62		private_ns = NULL;
63	} else if (initialized == 0) {
64		/* regular behaviour */
65		p->name = NSS_DBNAM_PRINTERS;	 /* "printers" */
66		p->default_config = NSS_DEFCONF_PRINTERS;
67		initialized = 1;
68	}
69	syslog(LOG_DEBUG, "database: %s, services: %s",
70		(p->name ? p->name : "NULL"),
71		(p->default_config ? p->default_config : "NULL"));
72}
73
74/*
75 * Return values: 0 = success, 1 = parse error, 2 = erange ...
76 * The structure pointer passed in is a structure in the caller's space
77 * wherein the field pointers would be set to areas in the buffer if
78 * need be. instring and buffer should be separate areas.
79 */
80/* ARGSUSED */
81static int
82str2printer(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
83{
84	if (lenstr + 1 > buflen)
85		return (NSS_STR_PARSE_ERANGE);
86
87	/* skip entries that begin with '#' */
88	if (instr[0] == '#')
89		return (NSS_STR_PARSE_PARSE);
90
91	/*
92	 * We copy the input string into the output buffer
93	 */
94	(void) memcpy(buffer, instr, lenstr);
95	buffer[lenstr] = '\0';
96
97	return (NSS_STR_PARSE_SUCCESS);
98}
99
100
101int
102setprinterentry(int stayopen, char *ns)
103{
104	printers_stayopen |= stayopen;
105	initialized = 0;
106	private_ns = ns;
107	nss_setent(&db_root, _nss_initf_printers, &context);
108	return (0);
109}
110
111
112int
113endprinterentry()
114{
115	printers_stayopen = 0;
116	initialized = 0;
117	nss_endent(&db_root, _nss_initf_printers, &context);
118	nss_delete(&db_root);
119	return (0);
120}
121
122
123/* ARGSUSED2 */
124int
125getprinterentry(char *linebuf, int linelen, char *ns)
126{
127	nss_XbyY_args_t arg;
128	nss_status_t	res;
129
130	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
131	res = nss_getent(&db_root, _nss_initf_printers, &context, &arg);
132	(void) NSS_XbyY_FINI(&arg);
133	return (arg.status = res);
134}
135
136
137int
138getprinterbyname(char *name, char *linebuf, int linelen, char *ns)
139{
140	nss_XbyY_args_t arg;
141	nss_status_t	res;
142
143	private_ns = ns;
144	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2printer);
145	arg.key.name = name;
146	res = nss_search(&db_root, _nss_initf_printers,
147			NSS_DBOP_PRINTERS_BYNAME, &arg);
148	(void) NSS_XbyY_FINI(&arg);
149	return (arg.status = res);
150}
151