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