xref: /illumos-gate/usr/src/lib/nsswitch/files/common/bootparams_getbyname.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  *	files/bootparams_getbyname.c -- "files" backend for
26  *	nsswitch "bootparams" database.
27  */
28 
29 static const char *bootparams = "/etc/bootparams";
30 
31 #include "files_common.h"
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <strings.h>
35 
36 static nss_status_t _nss_files_XY_bootparams(files_backend_ptr_t,
37 	nss_XbyY_args_t *, const char *);
38 
39 static nss_status_t
40 getbyname(be, a)
41 	files_backend_ptr_t	be;
42 	void			*a;
43 {
44 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
45 	nss_status_t		res;
46 
47 	/* bootparams_getbyname() has not set/endent; rewind on each call */
48 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
49 		return (res);
50 	}
51 	return (_nss_files_XY_bootparams(be, argp, argp->key.name));
52 }
53 
54 static files_backend_op_t bootparams_ops[] = {
55 	_nss_files_destr,
56 	getbyname
57 };
58 
59 /*ARGSUSED*/
60 nss_backend_t *
61 _nss_files_bootparams_constr(dummy1, dummy2, dummy3)
62 	const char	*dummy1, *dummy2, *dummy3;
63 {
64 	return (_nss_files_constr(bootparams_ops,
65 		sizeof (bootparams_ops) / sizeof (bootparams_ops[0]),
66 		bootparams,
67 		NSS_LINELEN_BOOTPARAMS,
68 		NULL));
69 }
70 
71 /*
72  * bootparams has the hostname as part of the data in the file, but the other
73  * backends don't include it in the data passed to the backend.  For this
74  * reason, we process everything here and don't bother calling the backend.
75  */
76 /*ARGSUSED*/
77 static nss_status_t
78 _nss_files_XY_bootparams(be, args, filter)
79 	files_backend_ptr_t	be;
80 	nss_XbyY_args_t		*args;
81 	const char		*filter;
82 			/*
83 			 * filter not useful here since the key
84 			 * we are looking for is the first "word"
85 			 * on the line and we can be fast enough.
86 			 */
87 {
88 	nss_status_t		res;
89 
90 	if (be->buf == 0 &&
91 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
92 		(void) _nss_files_endent(be, 0);
93 		return (NSS_UNAVAIL); /* really panic, malloc failed */
94 	}
95 
96 	res = NSS_NOTFOUND;
97 
98 	/*CONSTCOND*/
99 	while (1) {
100 		char		*instr	= be->buf;
101 		char		*p, *host, *limit;
102 		int		linelen;
103 
104 		/*
105 		 * _nss_files_read_line does process the '\' that are used
106 		 * in /etc/bootparams for continuation and gives one long
107 		 * buffer.
108 		 *
109 		 * linelen counts the characters up to but excluding the '\n'
110 		 */
111 		if ((linelen = _nss_files_read_line(be->f, instr,
112 		    be->minbuf)) < 0) {
113 			/* End of file */
114 			args->returnval = 0;
115 			args->returnlen = 0;
116 			break;
117 		}
118 
119 		/*
120 		 * we need to strip off the host name before returning it.
121 		 */
122 		p = instr;
123 		limit = p + linelen;
124 
125 		/* Skip over leading whitespace */
126 		while (p < limit && isspace(*p)) {
127 			p++;
128 		}
129 		host = p;
130 
131 		/* Skip over the hostname */
132 		while (p < limit && !isspace(*p)) {
133 			p++;
134 		}
135 		*p++ = '\0';
136 
137 		if (strcasecmp(args->key.name, host) != 0) {
138 			continue;
139 		}
140 
141 		/* Skip over whitespace between name and first datum */
142 		while (p < limit && isspace(*p)) {
143 			p++;
144 		}
145 		if (p >= limit) {
146 			/* Syntax error -- no data! Just skip it. */
147 			continue;
148 		}
149 
150 		linelen -= (p - instr);
151 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
152 			args->erange = 1;
153 			break;
154 		}
155 		(void) memcpy(args->buf.buffer, p, linelen);
156 		args->buf.buffer[linelen] = '\0';
157 		args->returnval = args->buf.result;
158 		args->returnlen = linelen;
159 		res = NSS_SUCCESS;
160 		break;
161 	}
162 	(void) _nss_files_endent(be, 0);
163 	return (res);
164 }
165