xref: /titanic_52/usr/src/lib/nsswitch/files/common/bootparams_getbyname.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate static const char *bootparams = "/etc/bootparams";
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "files_common.h"
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static nss_status_t _nss_files_XY_bootparams(files_backend_ptr_t,
397c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t *, const char *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static nss_status_t
427c478bd9Sstevel@tonic-gate getbyname(be, a)
437c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
447c478bd9Sstevel@tonic-gate 	void			*a;
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
477c478bd9Sstevel@tonic-gate 	nss_status_t		res;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	/* bootparams_getbyname() has not set/endent; rewind on each call */
507c478bd9Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
517c478bd9Sstevel@tonic-gate 		return (res);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 	return (_nss_files_XY_bootparams(be, argp, argp->key.name));
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static files_backend_op_t bootparams_ops[] = {
577c478bd9Sstevel@tonic-gate 	_nss_files_destr,
587c478bd9Sstevel@tonic-gate 	getbyname
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
627c478bd9Sstevel@tonic-gate nss_backend_t *
637c478bd9Sstevel@tonic-gate _nss_files_bootparams_constr(dummy1, dummy2, dummy3)
647c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	return (_nss_files_constr(bootparams_ops,
677c478bd9Sstevel@tonic-gate 		sizeof (bootparams_ops) / sizeof (bootparams_ops[0]),
687c478bd9Sstevel@tonic-gate 		bootparams,
697c478bd9Sstevel@tonic-gate 		NSS_LINELEN_BOOTPARAMS,
707c478bd9Sstevel@tonic-gate 		NULL));
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * bootparams has the hostname as part of the data in the file, but the other
757c478bd9Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
767c478bd9Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
797c478bd9Sstevel@tonic-gate static nss_status_t
807c478bd9Sstevel@tonic-gate _nss_files_XY_bootparams(be, args, filter)
817c478bd9Sstevel@tonic-gate 	files_backend_ptr_t	be;
827c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
837c478bd9Sstevel@tonic-gate 	const char		*filter;
847c478bd9Sstevel@tonic-gate 			/*
857c478bd9Sstevel@tonic-gate 			 * filter not useful here since the key
867c478bd9Sstevel@tonic-gate 			 * we are looking for is the first "word"
877c478bd9Sstevel@tonic-gate 			 * on the line and we can be fast enough.
887c478bd9Sstevel@tonic-gate 			 */
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	nss_status_t		res;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (be->buf == 0 &&
937c478bd9Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
947c478bd9Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
957c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	res = NSS_NOTFOUND;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
1017c478bd9Sstevel@tonic-gate 	while (1) {
1027c478bd9Sstevel@tonic-gate 		char		*instr	= be->buf;
1037c478bd9Sstevel@tonic-gate 		char		*p, *host, *limit;
1047c478bd9Sstevel@tonic-gate 		int		linelen;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		/*
1077c478bd9Sstevel@tonic-gate 		 * _nss_files_read_line does process the '\' that are used
1087c478bd9Sstevel@tonic-gate 		 * in /etc/bootparams for continuation and gives one long
1097c478bd9Sstevel@tonic-gate 		 * buffer.
1107c478bd9Sstevel@tonic-gate 		 *
1117c478bd9Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
1127c478bd9Sstevel@tonic-gate 		 */
1137c478bd9Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
1147c478bd9Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1157c478bd9Sstevel@tonic-gate 			/* End of file */
1167c478bd9Sstevel@tonic-gate 			args->returnval = 0;
117*cb5caa98Sdjl 			args->returnlen = 0;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/*
1227c478bd9Sstevel@tonic-gate 		 * we need to strip off the host name before returning it.
1237c478bd9Sstevel@tonic-gate 		 */
1247c478bd9Sstevel@tonic-gate 		p = instr;
1257c478bd9Sstevel@tonic-gate 		limit = p + linelen;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		/* Skip over leading whitespace */
1287c478bd9Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
1297c478bd9Sstevel@tonic-gate 			p++;
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 		host = p;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		/* Skip over the hostname */
1347c478bd9Sstevel@tonic-gate 		while (p < limit && !isspace(*p)) {
1357c478bd9Sstevel@tonic-gate 			p++;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		*p++ = '\0';
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		if (strcasecmp(args->key.name, host) != 0) {
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		/* Skip over whitespace between name and first datum */
1447c478bd9Sstevel@tonic-gate 		while (p < limit && isspace(*p)) {
1457c478bd9Sstevel@tonic-gate 			p++;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		if (p >= limit) {
1487c478bd9Sstevel@tonic-gate 			/* Syntax error -- no data! Just skip it. */
1497c478bd9Sstevel@tonic-gate 			continue;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		linelen -= (p - instr);
1537c478bd9Sstevel@tonic-gate 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
1547c478bd9Sstevel@tonic-gate 			args->erange = 1;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		(void) memcpy(args->buf.buffer, p, linelen);
1587c478bd9Sstevel@tonic-gate 		args->buf.buffer[linelen] = '\0';
1597c478bd9Sstevel@tonic-gate 		args->returnval = args->buf.result;
160*cb5caa98Sdjl 		args->returnlen = linelen;
1617c478bd9Sstevel@tonic-gate 		res = NSS_SUCCESS;
1627c478bd9Sstevel@tonic-gate 		break;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	(void) _nss_files_endent(be, 0);
1657c478bd9Sstevel@tonic-gate 	return (res);
1667c478bd9Sstevel@tonic-gate }
167