1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include <stdio.h>
27*7c478bd9Sstevel@tonic-gate #include <string.h>
28*7c478bd9Sstevel@tonic-gate #include <ctype.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate static int str2bootent(const char *, int, void *, char *, int);
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate static void
_nss_initf_bootparams(nss_db_params_t * p)37*7c478bd9Sstevel@tonic-gate _nss_initf_bootparams(nss_db_params_t *p)
38*7c478bd9Sstevel@tonic-gate {
39*7c478bd9Sstevel@tonic-gate 	p->name = NSS_DBNAM_BOOTPARAMS;
40*7c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_BOOTPARAMS;
41*7c478bd9Sstevel@tonic-gate }
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate int
bootparams_getbyname(char * name,char * linebuf,int linelen)44*7c478bd9Sstevel@tonic-gate bootparams_getbyname(
45*7c478bd9Sstevel@tonic-gate     char *name,	/* lookup key */
46*7c478bd9Sstevel@tonic-gate     char *linebuf,	/* buffer to put the answer in */
47*7c478bd9Sstevel@tonic-gate     int linelen	/* max # of bytes to put into linebuf */
48*7c478bd9Sstevel@tonic-gate )
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
51*7c478bd9Sstevel@tonic-gate 	nss_status_t	res;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2bootent);
54*7c478bd9Sstevel@tonic-gate 	arg.key.name = name;
55*7c478bd9Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_bootparams,
56*7c478bd9Sstevel@tonic-gate 			NSS_DBOP_BOOTPARAMS_BYNAME, &arg);
57*7c478bd9Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
58*7c478bd9Sstevel@tonic-gate 	return (arg.status = res);
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
63*7c478bd9Sstevel@tonic-gate  * The structure pointer passed in is a buffer in the caller's space.
64*7c478bd9Sstevel@tonic-gate  * instring and buffer should be separate areas.
65*7c478bd9Sstevel@tonic-gate  * The calling routine does all the real parsing; we just check limits and
66*7c478bd9Sstevel@tonic-gate  * store the entry in the buffer we were passed by the caller.
67*7c478bd9Sstevel@tonic-gate  * NOTE: we expect the data we're passed (in instr) has had the host's name
68*7c478bd9Sstevel@tonic-gate  * stripped off the begining.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
71*7c478bd9Sstevel@tonic-gate static int
str2bootent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)72*7c478bd9Sstevel@tonic-gate str2bootent(
73*7c478bd9Sstevel@tonic-gate     const char *instr,
74*7c478bd9Sstevel@tonic-gate     int lenstr,
75*7c478bd9Sstevel@tonic-gate     void *ent,		/* really (char *) */
76*7c478bd9Sstevel@tonic-gate     char *buffer,
77*7c478bd9Sstevel@tonic-gate     int buflen
78*7c478bd9Sstevel@tonic-gate )
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	const char	*p, *limit;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr) ||
83*7c478bd9Sstevel@tonic-gate 	    (buffer >= instr && (instr + lenstr) > buffer)) {
84*7c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 	p = instr;
87*7c478bd9Sstevel@tonic-gate 	limit = p + lenstr;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	/* Skip over leading whitespace */
90*7c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
91*7c478bd9Sstevel@tonic-gate 		p++;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	if (p >= limit) {
94*7c478bd9Sstevel@tonic-gate 		/* Syntax error -- no data! */
95*7c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 	lenstr -= (p - instr);
98*7c478bd9Sstevel@tonic-gate 	if (buflen <= lenstr) {		/* not enough buffer */
99*7c478bd9Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, p, lenstr);
102*7c478bd9Sstevel@tonic-gate 	buffer[lenstr] = '\0';
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
105*7c478bd9Sstevel@tonic-gate }
106