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/bootparams_getbyname.c -- "files" backend for
24*0Sstevel@tonic-gate  *	nsswitch "bootparams" database.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  *	Copyright (c) 1988-1995 Sun Microsystems Inc
27*0Sstevel@tonic-gate  *	All Rights Reserved.
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 *bootparams = "/etc/bootparams";
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "files_common.h"
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <ctype.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate static nss_status_t _nss_files_XY_bootparams(files_backend_ptr_t,
40*0Sstevel@tonic-gate 	nss_XbyY_args_t *, const char *);
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static nss_status_t
43*0Sstevel@tonic-gate getbyname(be, a)
44*0Sstevel@tonic-gate 	files_backend_ptr_t	be;
45*0Sstevel@tonic-gate 	void			*a;
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
48*0Sstevel@tonic-gate 	nss_status_t		res;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	/* bootparams_getbyname() has not set/endent; rewind on each call */
51*0Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
52*0Sstevel@tonic-gate 		return (res);
53*0Sstevel@tonic-gate 	}
54*0Sstevel@tonic-gate 	return (_nss_files_XY_bootparams(be, argp, argp->key.name));
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static files_backend_op_t bootparams_ops[] = {
58*0Sstevel@tonic-gate 	_nss_files_destr,
59*0Sstevel@tonic-gate 	getbyname
60*0Sstevel@tonic-gate };
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*ARGSUSED*/
63*0Sstevel@tonic-gate nss_backend_t *
64*0Sstevel@tonic-gate _nss_files_bootparams_constr(dummy1, dummy2, dummy3)
65*0Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	return (_nss_files_constr(bootparams_ops,
68*0Sstevel@tonic-gate 		sizeof (bootparams_ops) / sizeof (bootparams_ops[0]),
69*0Sstevel@tonic-gate 		bootparams,
70*0Sstevel@tonic-gate 		NSS_LINELEN_BOOTPARAMS,
71*0Sstevel@tonic-gate 		NULL));
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * bootparams has the hostname as part of the data in the file, but the other
76*0Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
77*0Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate /*ARGSUSED*/
80*0Sstevel@tonic-gate static nss_status_t
81*0Sstevel@tonic-gate _nss_files_XY_bootparams(be, args, filter)
82*0Sstevel@tonic-gate 	files_backend_ptr_t	be;
83*0Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
84*0Sstevel@tonic-gate 	const char		*filter;
85*0Sstevel@tonic-gate 			/*
86*0Sstevel@tonic-gate 			 * filter not useful here since the key
87*0Sstevel@tonic-gate 			 * we are looking for is the first "word"
88*0Sstevel@tonic-gate 			 * on the line and we can be fast enough.
89*0Sstevel@tonic-gate 			 */
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	nss_status_t		res;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if (be->buf == 0 &&
94*0Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
95*0Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
96*0Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	res = NSS_NOTFOUND;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/*CONSTCOND*/
102*0Sstevel@tonic-gate 	while (1) {
103*0Sstevel@tonic-gate 		char		*instr	= be->buf;
104*0Sstevel@tonic-gate 		char		*p, *host, *limit;
105*0Sstevel@tonic-gate 		int		linelen;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 		/*
108*0Sstevel@tonic-gate 		 * _nss_files_read_line does process the '\' that are used
109*0Sstevel@tonic-gate 		 * in /etc/bootparams for continuation and gives one long
110*0Sstevel@tonic-gate 		 * buffer.
111*0Sstevel@tonic-gate 		 *
112*0Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
113*0Sstevel@tonic-gate 		 */
114*0Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
115*0Sstevel@tonic-gate 		    be->minbuf)) < 0) {
116*0Sstevel@tonic-gate 			/* End of file */
117*0Sstevel@tonic-gate 			args->returnval = 0;
118*0Sstevel@tonic-gate 			args->erange    = 0;
119*0Sstevel@tonic-gate 			break;
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		/*
123*0Sstevel@tonic-gate 		 * we need to strip off the host name before returning it.
124*0Sstevel@tonic-gate 		 */
125*0Sstevel@tonic-gate 		p = instr;
126*0Sstevel@tonic-gate 		limit = p + linelen;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		/* Skip over leading whitespace */
129*0Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
130*0Sstevel@tonic-gate 			p++;
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 		host = p;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		/* Skip over the hostname */
135*0Sstevel@tonic-gate 		while (p < limit && !isspace(*p)) {
136*0Sstevel@tonic-gate 			p++;
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 		*p++ = '\0';
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		if (strcasecmp(args->key.name, host) != 0) {
141*0Sstevel@tonic-gate 			continue;
142*0Sstevel@tonic-gate 		}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		/* Skip over whitespace between name and first datum */
145*0Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
146*0Sstevel@tonic-gate 			p++;
147*0Sstevel@tonic-gate 		}
148*0Sstevel@tonic-gate 		if (p >= limit) {
149*0Sstevel@tonic-gate 			/* Syntax error -- no data! Just skip it. */
150*0Sstevel@tonic-gate 			continue;
151*0Sstevel@tonic-gate 		}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		linelen -= (p - instr);
154*0Sstevel@tonic-gate 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
155*0Sstevel@tonic-gate 			args->erange = 1;
156*0Sstevel@tonic-gate 			break;
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 		(void) memcpy(args->buf.buffer, p, linelen);
159*0Sstevel@tonic-gate 		args->buf.buffer[linelen] = '\0';
160*0Sstevel@tonic-gate 		args->returnval = args->buf.result;
161*0Sstevel@tonic-gate 		res = NSS_SUCCESS;
162*0Sstevel@tonic-gate 		break;
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 	(void) _nss_files_endent(be, 0);
165*0Sstevel@tonic-gate 	return (res);
166*0Sstevel@tonic-gate }
167