xref: /illumos-gate/usr/src/lib/libsocket/inet/bootparams_getbyname.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
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 #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 
34 static int str2bootent(const char *, int, void *, char *, int);
35 
36 static DEFINE_NSS_DB_ROOT(db_root);
37 
38 static void
39 _nss_initf_bootparams(nss_db_params_t *p)
40 {
41 	p->name = NSS_DBNAM_BOOTPARAMS;
42 	p->default_config = NSS_DEFCONF_BOOTPARAMS;
43 }
44 
45 int
46 bootparams_getbyname(
47     char *name,	/* lookup key */
48     char *linebuf,	/* buffer to put the answer in */
49     int linelen	/* max # of bytes to put into linebuf */
50 )
51 {
52 	nss_XbyY_args_t arg;
53 	nss_status_t	res;
54 
55 	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2bootent);
56 	arg.key.name = name;
57 	res = nss_search(&db_root, _nss_initf_bootparams,
58 			NSS_DBOP_BOOTPARAMS_BYNAME, &arg);
59 	(void) NSS_XbyY_FINI(&arg);
60 	return (arg.status = res);
61 }
62 
63 /*
64  * Return values: 0 = success, 1 = parse error, 2 = erange ...
65  * The structure pointer passed in is a buffer in the caller's space.
66  * instring and buffer should be separate areas.
67  * The calling routine does all the real parsing; we just check limits and
68  * store the entry in the buffer we were passed by the caller.
69  * NOTE: we expect the data we're passed (in instr) has had the host's name
70  * stripped off the begining.
71  */
72 /* ARGSUSED */
73 static int
74 str2bootent(
75     const char *instr,
76     int lenstr,
77     void *ent,		/* really (char *) */
78     char *buffer,
79     int buflen
80 )
81 {
82 	const char	*p, *limit;
83 
84 	if ((instr >= buffer && (buffer + buflen) > instr) ||
85 	    (buffer >= instr && (instr + lenstr) > buffer)) {
86 		return (NSS_STR_PARSE_PARSE);
87 	}
88 	p = instr;
89 	limit = p + lenstr;
90 
91 	/* Skip over leading whitespace */
92 	while (p < limit && isspace(*p)) {
93 		p++;
94 	}
95 	if (p >= limit) {
96 		/* Syntax error -- no data! */
97 		return (NSS_STR_PARSE_PARSE);
98 	}
99 	lenstr -= (p - instr);
100 	if (buflen <= lenstr) {		/* not enough buffer */
101 		return (NSS_STR_PARSE_ERANGE);
102 	}
103 	(void) memcpy(buffer, p, lenstr);
104 	buffer[lenstr] = '\0';
105 
106 	return (NSS_STR_PARSE_SUCCESS);
107 }
108