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