xref: /titanic_52/usr/src/lib/libsocket/inet/getservent_r.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.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * lib/libsocket/inet/getservent_r.c
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * This file defines and implements the re-entrant enumeration routines for
287c478bd9Sstevel@tonic-gate  *   services: setservent(), getservent_r(), and endservent(). They consult
297c478bd9Sstevel@tonic-gate  *   the switch policy directly and do not "share" their enumeration state
307c478bd9Sstevel@tonic-gate  *   nor the stayopen flag with the implentation of the more common
317c478bd9Sstevel@tonic-gate  *   getservbyname_r()/getservbyport_r(). The latter follows a tortuous
327c478bd9Sstevel@tonic-gate  *   route in order to be consistent with netdir_getbyYY() (see
337c478bd9Sstevel@tonic-gate  *   getservbyname_r.c and lib/libnsl/nss/netdir_inet.c).
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * str2servent is implemented in libnsl, libnsl/nss/netdir_inet.c, since
437c478bd9Sstevel@tonic-gate  * the "engine" of the new gethost/getserv/netdir lives in libnsl.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate int str2servent(const char *, int, void *, char *, int);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Unsynchronized, but it affects only
497c478bd9Sstevel@tonic-gate  * efficiency, not correctness.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static int services_stayopen;
527c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
537c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
547c478bd9Sstevel@tonic-gate 
55*cb5caa98Sdjl void
567c478bd9Sstevel@tonic-gate _nss_initf_services(nss_db_params_t *p)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	p->name	= NSS_DBNAM_SERVICES;
597c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_SERVICES;
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate int
637c478bd9Sstevel@tonic-gate setservent(int stay)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	services_stayopen |= stay;
667c478bd9Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_services, &context);
677c478bd9Sstevel@tonic-gate 	return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
717c478bd9Sstevel@tonic-gate endservent()
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	services_stayopen = 0;
747c478bd9Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_services, &context);
757c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
767c478bd9Sstevel@tonic-gate 	return (0);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct servent *
807c478bd9Sstevel@tonic-gate getservent_r(struct servent *result, char *buffer, int buflen)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
837c478bd9Sstevel@tonic-gate 	nss_status_t	res;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2servent);
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Setting proto to NULL here is a bit of a hack since we share
887c478bd9Sstevel@tonic-gate 	 * the parsing code in the NIS+ backend with our getservbyYY()
897c478bd9Sstevel@tonic-gate 	 * brethren who can search on 1-1/2 key. If they pass a NULL
907c478bd9Sstevel@tonic-gate 	 * proto, the parsing code deals with it by picking the protocol
917c478bd9Sstevel@tonic-gate 	 * from the first NIS+ matching object and combining all entries
927c478bd9Sstevel@tonic-gate 	 * with "that" proto field. NIS+ is the only name service, so far,
937c478bd9Sstevel@tonic-gate 	 * that can return multiple entries on a lookup.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	arg.key.serv.proto	= NULL;
967c478bd9Sstevel@tonic-gate 	/* === No stayopen flag;  of course you stay open for iteration */
977c478bd9Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_services, &context, &arg);
987c478bd9Sstevel@tonic-gate 	arg.status = res;
997c478bd9Sstevel@tonic-gate 	return (struct servent *)NSS_XbyY_FINI(&arg);
1007c478bd9Sstevel@tonic-gate }
101