bootparams_getbyname.c revision 7c478bd95313f5f23a4c958a745db2134aa0324
17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate *
227c478bdstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include <stdio.h>
297c478bdstevel@tonic-gate#include <string.h>
307c478bdstevel@tonic-gate#include <ctype.h>
317c478bdstevel@tonic-gate#include <sys/types.h>
327c478bdstevel@tonic-gate#include <nss_dbdefs.h>
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gatestatic int str2bootent(const char *, int, void *, char *, int);
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gatestatic DEFINE_NSS_DB_ROOT(db_root);
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gatestatic void
397c478bdstevel@tonic-gate_nss_initf_bootparams(nss_db_params_t *p)
407c478bdstevel@tonic-gate{
417c478bdstevel@tonic-gate	p->name = NSS_DBNAM_BOOTPARAMS;
427c478bdstevel@tonic-gate	p->default_config = NSS_DEFCONF_BOOTPARAMS;
437c478bdstevel@tonic-gate}
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gateint
467c478bdstevel@tonic-gatebootparams_getbyname(
477c478bdstevel@tonic-gate    char *name,	/* lookup key */
487c478bdstevel@tonic-gate    char *linebuf,	/* buffer to put the answer in */
497c478bdstevel@tonic-gate    int linelen	/* max # of bytes to put into linebuf */
507c478bdstevel@tonic-gate)
517c478bdstevel@tonic-gate{
527c478bdstevel@tonic-gate	nss_XbyY_args_t arg;
537c478bdstevel@tonic-gate	nss_status_t	res;
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate	NSS_XbyY_INIT(&arg, linebuf, linebuf, linelen, str2bootent);
567c478bdstevel@tonic-gate	arg.key.name = name;
577c478bdstevel@tonic-gate	res = nss_search(&db_root, _nss_initf_bootparams,
587c478bdstevel@tonic-gate			NSS_DBOP_BOOTPARAMS_BYNAME, &arg);
597c478bdstevel@tonic-gate	(void) NSS_XbyY_FINI(&arg);
607c478bdstevel@tonic-gate	return (arg.status = res);
617c478bdstevel@tonic-gate}
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate/*
647c478bdstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ...
657c478bdstevel@tonic-gate * The structure pointer passed in is a buffer in the caller's space.
667c478bdstevel@tonic-gate * instring and buffer should be separate areas.
677c478bdstevel@tonic-gate * The calling routine does all the real parsing; we just check limits and
687c478bdstevel@tonic-gate * store the entry in the buffer we were passed by the caller.
697c478bdstevel@tonic-gate * NOTE: we expect the data we're passed (in instr) has had the host's name
707c478bdstevel@tonic-gate * stripped off the begining.
717c478bdstevel@tonic-gate */
727c478bdstevel@tonic-gate/* ARGSUSED */
737c478bdstevel@tonic-gatestatic int
747c478bdstevel@tonic-gatestr2bootent(
757c478bdstevel@tonic-gate    const char *instr,
767c478bdstevel@tonic-gate    int lenstr,
777c478bdstevel@tonic-gate    void *ent,		/* really (char *) */
787c478bdstevel@tonic-gate    char *buffer,
797c478bdstevel@tonic-gate    int buflen
807c478bdstevel@tonic-gate)
817c478bdstevel@tonic-gate{
827c478bdstevel@tonic-gate	const char	*p, *limit;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate	if ((instr >= buffer && (buffer + buflen) > instr) ||
857c478bdstevel@tonic-gate	    (buffer >= instr && (instr + lenstr) > buffer)) {
867c478bdstevel@tonic-gate		return (NSS_STR_PARSE_PARSE);
877c478bdstevel@tonic-gate	}
887c478bdstevel@tonic-gate	p = instr;
897c478bdstevel@tonic-gate	limit = p + lenstr;
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate	/* Skip over leading whitespace */
927c478bdstevel@tonic-gate	while (p < limit && isspace(*p)) {
937c478bdstevel@tonic-gate		p++;
947c478bdstevel@tonic-gate	}
957c478bdstevel@tonic-gate	if (p >= limit) {
967c478bdstevel@tonic-gate		/* Syntax error -- no data! */
977c478bdstevel@tonic-gate		return (NSS_STR_PARSE_PARSE);
987c478bdstevel@tonic-gate	}
997c478bdstevel@tonic-gate	lenstr -= (p - instr);
1007c478bdstevel@tonic-gate	if (buflen <= lenstr) {		/* not enough buffer */
1017c478bdstevel@tonic-gate		return (NSS_STR_PARSE_ERANGE);
1027c478bdstevel@tonic-gate	}
1037c478bdstevel@tonic-gate	(void) memcpy(buffer, p, lenstr);
1047c478bdstevel@tonic-gate	buffer[lenstr] = '\0';
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate	return (NSS_STR_PARSE_SUCCESS);
1077c478bdstevel@tonic-gate}
108