17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*cb5caa98Sdjl  * Use is subject to license terms.
24*cb5caa98Sdjl  *
257c478bd9Sstevel@tonic-gate  *	files/bootparams_getbyname.c -- "files" backend for
267c478bd9Sstevel@tonic-gate  *	nsswitch "bootparams" database.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate static const char *bootparams = "/etc/bootparams";
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "files_common.h"
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static nss_status_t _nss_files_XY_bootparams(files_backend_ptr_t,
377c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t *, const char *);
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(be,a)407c478bd9Sstevel@tonic-gate getbyname(be, a)
417c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
427c478bd9Sstevel@tonic-gate 	void			*a;
437c478bd9Sstevel@tonic-gate {
44*cb5caa98Sdjl 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
457c478bd9Sstevel@tonic-gate 	nss_status_t		res;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	/* bootparams_getbyname() has not set/endent; rewind on each call */
487c478bd9Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
497c478bd9Sstevel@tonic-gate 		return (res);
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 	return (_nss_files_XY_bootparams(be, argp, argp->key.name));
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static files_backend_op_t bootparams_ops[] = {
557c478bd9Sstevel@tonic-gate 	_nss_files_destr,
567c478bd9Sstevel@tonic-gate 	getbyname
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
607c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_files_bootparams_constr(dummy1,dummy2,dummy3)617c478bd9Sstevel@tonic-gate _nss_files_bootparams_constr(dummy1, dummy2, dummy3)
627c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	return (_nss_files_constr(bootparams_ops,
657c478bd9Sstevel@tonic-gate 		sizeof (bootparams_ops) / sizeof (bootparams_ops[0]),
667c478bd9Sstevel@tonic-gate 		bootparams,
677c478bd9Sstevel@tonic-gate 		NSS_LINELEN_BOOTPARAMS,
687c478bd9Sstevel@tonic-gate 		NULL));
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * bootparams has the hostname as part of the data in the file, but the other
737c478bd9Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
747c478bd9Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
777c478bd9Sstevel@tonic-gate static nss_status_t
_nss_files_XY_bootparams(be,args,filter)787c478bd9Sstevel@tonic-gate _nss_files_XY_bootparams(be, args, filter)
797c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
807c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
817c478bd9Sstevel@tonic-gate 	const char		*filter;
827c478bd9Sstevel@tonic-gate 			/*
837c478bd9Sstevel@tonic-gate 			 * filter not useful here since the key
847c478bd9Sstevel@tonic-gate 			 * we are looking for is the first "word"
857c478bd9Sstevel@tonic-gate 			 * on the line and we can be fast enough.
867c478bd9Sstevel@tonic-gate 			 */
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	nss_status_t		res;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (be->buf == 0 &&
917c478bd9Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
927c478bd9Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
937c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	res = NSS_NOTFOUND;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
997c478bd9Sstevel@tonic-gate 	while (1) {
1007c478bd9Sstevel@tonic-gate 		char		*instr	= be->buf;
1017c478bd9Sstevel@tonic-gate 		char		*p, *host, *limit;
1027c478bd9Sstevel@tonic-gate 		int		linelen;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		/*
1057c478bd9Sstevel@tonic-gate 		 * _nss_files_read_line does process the '\' that are used
1067c478bd9Sstevel@tonic-gate 		 * in /etc/bootparams for continuation and gives one long
1077c478bd9Sstevel@tonic-gate 		 * buffer.
108*cb5caa98Sdjl 		 *
1097c478bd9Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
1107c478bd9Sstevel@tonic-gate 		 */
1117c478bd9Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
1127c478bd9Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1137c478bd9Sstevel@tonic-gate 			/* End of file */
1147c478bd9Sstevel@tonic-gate 			args->returnval = 0;
115*cb5caa98Sdjl 			args->returnlen = 0;
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		/*
1207c478bd9Sstevel@tonic-gate 		 * we need to strip off the host name before returning it.
1217c478bd9Sstevel@tonic-gate 		 */
1227c478bd9Sstevel@tonic-gate 		p = instr;
1237c478bd9Sstevel@tonic-gate 		limit = p + linelen;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		/* Skip over leading whitespace */
1267c478bd9Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
1277c478bd9Sstevel@tonic-gate 			p++;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		host = p;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		/* Skip over the hostname */
1327c478bd9Sstevel@tonic-gate 		while (p < limit && !isspace(*p)) {
1337c478bd9Sstevel@tonic-gate 			p++;
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		*p++ = '\0';
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		if (strcasecmp(args->key.name, host) != 0) {
1387c478bd9Sstevel@tonic-gate 			continue;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		/* Skip over whitespace between name and first datum */
1427c478bd9Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
1437c478bd9Sstevel@tonic-gate 			p++;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		if (p >= limit) {
1467c478bd9Sstevel@tonic-gate 			/* Syntax error -- no data! Just skip it. */
1477c478bd9Sstevel@tonic-gate 			continue;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		linelen -= (p - instr);
1517c478bd9Sstevel@tonic-gate 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
1527c478bd9Sstevel@tonic-gate 			args->erange = 1;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		(void) memcpy(args->buf.buffer, p, linelen);
1567c478bd9Sstevel@tonic-gate 		args->buf.buffer[linelen] = '\0';
1577c478bd9Sstevel@tonic-gate 		args->returnval = args->buf.result;
158*cb5caa98Sdjl 		args->returnlen = linelen;
1597c478bd9Sstevel@tonic-gate 		res = NSS_SUCCESS;
1607c478bd9Sstevel@tonic-gate 		break;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	(void) _nss_files_endent(be, 0);
1637c478bd9Sstevel@tonic-gate 	return (res);
1647c478bd9Sstevel@tonic-gate }
165