1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	files/printers_getbyname.c -- "files" backend for
24*0Sstevel@tonic-gate  *	nsswitch "printers" database.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
27*0Sstevel@tonic-gate  * Use is subject to license terms.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate static const char *printers = "/etc/printers.conf";
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma weak _nss_files__printers_constr = _nss_files_printers_constr
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include "files_common.h"
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <strings.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static nss_status_t _nss_files_XY_printers(files_backend_ptr_t,
41*0Sstevel@tonic-gate 	nss_XbyY_args_t *, const char *);
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static nss_status_t
45*0Sstevel@tonic-gate getent(be, a)
46*0Sstevel@tonic-gate 	files_backend_ptr_t	be;
47*0Sstevel@tonic-gate 	void			*a;
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	nss_XbyY_args_t	 *args = (nss_XbyY_args_t *)a;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	return (_nss_files_XY_all(be, args, 1, 0, 0));
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static nss_status_t
56*0Sstevel@tonic-gate getbyname(be, a)
57*0Sstevel@tonic-gate 	files_backend_ptr_t	be;
58*0Sstevel@tonic-gate 	void			*a;
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
61*0Sstevel@tonic-gate 	nss_status_t		res;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	/* printers_getbyname() has not set/endent; rewind on each call */
64*0Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
65*0Sstevel@tonic-gate 		return (res);
66*0Sstevel@tonic-gate 	}
67*0Sstevel@tonic-gate 	return (_nss_files_XY_printers(be, argp, argp->key.name));
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static files_backend_op_t printers_ops[] = {
71*0Sstevel@tonic-gate 	_nss_files_destr,
72*0Sstevel@tonic-gate 	_nss_files_endent,
73*0Sstevel@tonic-gate 	_nss_files_setent,
74*0Sstevel@tonic-gate 	getent,
75*0Sstevel@tonic-gate 	getbyname
76*0Sstevel@tonic-gate };
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate nss_backend_t *
79*0Sstevel@tonic-gate _nss_files_printers_constr(dummy1, dummy2, dummy3)
80*0Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	return (_nss_files_constr(printers_ops,
83*0Sstevel@tonic-gate 		sizeof (printers_ops) / sizeof (printers_ops[0]),
84*0Sstevel@tonic-gate 		printers,
85*0Sstevel@tonic-gate 		NSS_LINELEN_PRINTERS,
86*0Sstevel@tonic-gate 		NULL));
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate /*
90*0Sstevel@tonic-gate  * printers has the hostname as part of the data in the file, but the other
91*0Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
92*0Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate static nss_status_t
95*0Sstevel@tonic-gate _nss_files_XY_printers(be, args, filter)
96*0Sstevel@tonic-gate 	files_backend_ptr_t	be;
97*0Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
98*0Sstevel@tonic-gate 	const char		*filter;
99*0Sstevel@tonic-gate 			/*
100*0Sstevel@tonic-gate 			 * filter not useful here since the key
101*0Sstevel@tonic-gate 			 * we are looking for is the first "word"
102*0Sstevel@tonic-gate 			 * on the line and we can be fast enough.
103*0Sstevel@tonic-gate 			 */
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	nss_status_t		res;
106*0Sstevel@tonic-gate 	int	parsestat;
107*0Sstevel@tonic-gate 	int namelen;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (be->buf == 0 &&
110*0Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
111*0Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
112*0Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	res = NSS_NOTFOUND;
116*0Sstevel@tonic-gate 	namelen = strlen(args->key.name);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	while (1) {
119*0Sstevel@tonic-gate 		char		*instr	= be->buf;
120*0Sstevel@tonic-gate 		char		*p, *limit;
121*0Sstevel@tonic-gate 		int		linelen;
122*0Sstevel@tonic-gate 		int		found = 0;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		/*
125*0Sstevel@tonic-gate 		 * _nss_files_read_line does process the '\' that are used
126*0Sstevel@tonic-gate 		 * in /etc/printers.conf for continuation and gives one long
127*0Sstevel@tonic-gate 		 * buffer.
128*0Sstevel@tonic-gate 		 *
129*0Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
130*0Sstevel@tonic-gate 		 */
131*0Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
132*0Sstevel@tonic-gate 		    be->minbuf)) < 0) {
133*0Sstevel@tonic-gate 			/* End of file */
134*0Sstevel@tonic-gate 			args->returnval = 0;
135*0Sstevel@tonic-gate 			args->erange    = 0;
136*0Sstevel@tonic-gate 			break;
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 		p = instr;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		if (*p == '#')					/* comment */
141*0Sstevel@tonic-gate 			continue;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		/*
144*0Sstevel@tonic-gate 		 * find the name in the namelist a|b|c...:
145*0Sstevel@tonic-gate 		 */
146*0Sstevel@tonic-gate 		if ((limit = strchr(instr, ':')) == NULL)	/* bad line */
147*0Sstevel@tonic-gate 			continue;
148*0Sstevel@tonic-gate 		while ((p < limit) && (found == 0)) {
149*0Sstevel@tonic-gate 			if ((strncmp(p, args->key.name, namelen) == 0) &&
150*0Sstevel@tonic-gate 			    ((*(p+namelen) == '|') || (*(p+namelen) == ':')))
151*0Sstevel@tonic-gate 				found++;
152*0Sstevel@tonic-gate 			else {
153*0Sstevel@tonic-gate 				if ((p = strchr(p, '|')) == NULL)
154*0Sstevel@tonic-gate 					p = limit;
155*0Sstevel@tonic-gate 				else	/* skip the '|' */
156*0Sstevel@tonic-gate 					p++;
157*0Sstevel@tonic-gate 			}
158*0Sstevel@tonic-gate 		}
159*0Sstevel@tonic-gate 		if (found == 0)
160*0Sstevel@tonic-gate 			continue;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		p = instr;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
165*0Sstevel@tonic-gate 			args->erange = 1;
166*0Sstevel@tonic-gate 			break;
167*0Sstevel@tonic-gate 		}
168*0Sstevel@tonic-gate 		(void) memcpy(args->buf.buffer, p, linelen);
169*0Sstevel@tonic-gate 		args->buf.buffer[linelen] = '\0';
170*0Sstevel@tonic-gate 		args->returnval = args->buf.result;
171*0Sstevel@tonic-gate 		res = NSS_SUCCESS;
172*0Sstevel@tonic-gate 		break;
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 	(void) _nss_files_endent(be, 0);
175*0Sstevel@tonic-gate 	return (res);
176*0Sstevel@tonic-gate }
177