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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /*
24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
26*1219Sraf */
27*1219Sraf
28*1219Sraf /*
290Sstevel@tonic-gate * This file defines and implements the re-entrant enumeration routines for
300Sstevel@tonic-gate * hosts: sethostent(), gethostent_r(), and endhostent(). They consult
310Sstevel@tonic-gate * the switch policy directly and do not "share" their enumeration state
320Sstevel@tonic-gate * nor the stayopen flag with the implentation of the more commonly used
330Sstevel@tonic-gate * gethostbyname_r()/gethostbyaddr_r(). The latter follows a tortuous
340Sstevel@tonic-gate * route in order to be consistent with netdir_getbyYY() (see
350Sstevel@tonic-gate * gethostbyname_r.c and netdir_inet.c).
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
390Sstevel@tonic-gate
40*1219Sraf #include "mt.h"
410Sstevel@tonic-gate #include <sys/socket.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <nss_dbdefs.h>
440Sstevel@tonic-gate #include "nss.h"
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * str2hostent() is now a wrapper to __str2hostent().
480Sstevel@tonic-gate * __str2hostent() now takes an extra argument to specify the
490Sstevel@tonic-gate * AF_INET type for address parsing.
500Sstevel@tonic-gate */
510Sstevel@tonic-gate int
str2hostent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)520Sstevel@tonic-gate str2hostent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate return (__str2hostent(AF_INET, instr, lenstr, ent, buffer, buflen));
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate static int hosts_stayopen;
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate * Unsynchronized, but it affects only
600Sstevel@tonic-gate * efficiency, not correctness
610Sstevel@tonic-gate */
620Sstevel@tonic-gate
630Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
640Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
650Sstevel@tonic-gate
660Sstevel@tonic-gate void
_nss_initf_hosts(nss_db_params_t * p)670Sstevel@tonic-gate _nss_initf_hosts(nss_db_params_t *p)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate p->name = NSS_DBNAM_HOSTS;
700Sstevel@tonic-gate p->default_config = NSS_DEFCONF_HOSTS;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate int
sethostent(int stay)740Sstevel@tonic-gate sethostent(int stay)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate hosts_stayopen |= stay;
770Sstevel@tonic-gate nss_setent(&db_root, _nss_initf_hosts, &context);
780Sstevel@tonic-gate return (0);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate int
endhostent(void)820Sstevel@tonic-gate endhostent(void)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate hosts_stayopen = 0;
850Sstevel@tonic-gate nss_endent(&db_root, _nss_initf_hosts, &context);
860Sstevel@tonic-gate nss_delete(&db_root);
870Sstevel@tonic-gate return (0);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate struct hostent *
gethostent_r(struct hostent * result,char * buffer,int buflen,int * h_errnop)910Sstevel@tonic-gate gethostent_r(struct hostent *result, char *buffer, int buflen, int *h_errnop)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate nss_XbyY_args_t arg;
940Sstevel@tonic-gate nss_status_t res;
950Sstevel@tonic-gate
960Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
970Sstevel@tonic-gate res = nss_getent(&db_root, _nss_initf_hosts,
980Sstevel@tonic-gate &context, &arg);
990Sstevel@tonic-gate arg.status = res;
1000Sstevel@tonic-gate *h_errnop = arg.h_errno;
1010Sstevel@tonic-gate return ((struct hostent *)NSS_XbyY_FINI(&arg));
1020Sstevel@tonic-gate }
103