10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*2830Sdjl  * Use is subject to license terms.
24*2830Sdjl  *
250Sstevel@tonic-gate  *	files/bootparams_getbyname.c -- "files" backend for
260Sstevel@tonic-gate  *	nsswitch "bootparams" database.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate static const char *bootparams = "/etc/bootparams";
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "files_common.h"
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate static nss_status_t _nss_files_XY_bootparams(files_backend_ptr_t,
390Sstevel@tonic-gate 	nss_XbyY_args_t *, const char *);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static nss_status_t
getbyname(be,a)420Sstevel@tonic-gate getbyname(be, a)
430Sstevel@tonic-gate 	files_backend_ptr_t	be;
440Sstevel@tonic-gate 	void			*a;
450Sstevel@tonic-gate {
46*2830Sdjl 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
470Sstevel@tonic-gate 	nss_status_t		res;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	/* bootparams_getbyname() has not set/endent; rewind on each call */
500Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
510Sstevel@tonic-gate 		return (res);
520Sstevel@tonic-gate 	}
530Sstevel@tonic-gate 	return (_nss_files_XY_bootparams(be, argp, argp->key.name));
540Sstevel@tonic-gate }
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static files_backend_op_t bootparams_ops[] = {
570Sstevel@tonic-gate 	_nss_files_destr,
580Sstevel@tonic-gate 	getbyname
590Sstevel@tonic-gate };
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*ARGSUSED*/
620Sstevel@tonic-gate nss_backend_t *
_nss_files_bootparams_constr(dummy1,dummy2,dummy3)630Sstevel@tonic-gate _nss_files_bootparams_constr(dummy1, dummy2, dummy3)
640Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	return (_nss_files_constr(bootparams_ops,
670Sstevel@tonic-gate 		sizeof (bootparams_ops) / sizeof (bootparams_ops[0]),
680Sstevel@tonic-gate 		bootparams,
690Sstevel@tonic-gate 		NSS_LINELEN_BOOTPARAMS,
700Sstevel@tonic-gate 		NULL));
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * bootparams has the hostname as part of the data in the file, but the other
750Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
760Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate /*ARGSUSED*/
790Sstevel@tonic-gate static nss_status_t
_nss_files_XY_bootparams(be,args,filter)800Sstevel@tonic-gate _nss_files_XY_bootparams(be, args, filter)
810Sstevel@tonic-gate 	files_backend_ptr_t	be;
820Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
830Sstevel@tonic-gate 	const char		*filter;
840Sstevel@tonic-gate 			/*
850Sstevel@tonic-gate 			 * filter not useful here since the key
860Sstevel@tonic-gate 			 * we are looking for is the first "word"
870Sstevel@tonic-gate 			 * on the line and we can be fast enough.
880Sstevel@tonic-gate 			 */
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	nss_status_t		res;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if (be->buf == 0 &&
930Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
940Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
950Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	res = NSS_NOTFOUND;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/*CONSTCOND*/
1010Sstevel@tonic-gate 	while (1) {
1020Sstevel@tonic-gate 		char		*instr	= be->buf;
1030Sstevel@tonic-gate 		char		*p, *host, *limit;
1040Sstevel@tonic-gate 		int		linelen;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		/*
1070Sstevel@tonic-gate 		 * _nss_files_read_line does process the '\' that are used
1080Sstevel@tonic-gate 		 * in /etc/bootparams for continuation and gives one long
1090Sstevel@tonic-gate 		 * buffer.
110*2830Sdjl 		 *
1110Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
1120Sstevel@tonic-gate 		 */
1130Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
1140Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1150Sstevel@tonic-gate 			/* End of file */
1160Sstevel@tonic-gate 			args->returnval = 0;
117*2830Sdjl 			args->returnlen = 0;
1180Sstevel@tonic-gate 			break;
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		/*
1220Sstevel@tonic-gate 		 * we need to strip off the host name before returning it.
1230Sstevel@tonic-gate 		 */
1240Sstevel@tonic-gate 		p = instr;
1250Sstevel@tonic-gate 		limit = p + linelen;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 		/* Skip over leading whitespace */
1280Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
1290Sstevel@tonic-gate 			p++;
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		host = p;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		/* Skip over the hostname */
1340Sstevel@tonic-gate 		while (p < limit && !isspace(*p)) {
1350Sstevel@tonic-gate 			p++;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 		*p++ = '\0';
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		if (strcasecmp(args->key.name, host) != 0) {
1400Sstevel@tonic-gate 			continue;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		/* Skip over whitespace between name and first datum */
1440Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
1450Sstevel@tonic-gate 			p++;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 		if (p >= limit) {
1480Sstevel@tonic-gate 			/* Syntax error -- no data! Just skip it. */
1490Sstevel@tonic-gate 			continue;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		linelen -= (p - instr);
1530Sstevel@tonic-gate 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
1540Sstevel@tonic-gate 			args->erange = 1;
1550Sstevel@tonic-gate 			break;
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 		(void) memcpy(args->buf.buffer, p, linelen);
1580Sstevel@tonic-gate 		args->buf.buffer[linelen] = '\0';
1590Sstevel@tonic-gate 		args->returnval = args->buf.result;
160*2830Sdjl 		args->returnlen = linelen;
1610Sstevel@tonic-gate 		res = NSS_SUCCESS;
1620Sstevel@tonic-gate 		break;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	(void) _nss_files_endent(be, 0);
1650Sstevel@tonic-gate 	return (res);
1660Sstevel@tonic-gate }
167