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