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
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * 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  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24cb5caa98Sdjl  * Use is subject to license terms.
25*7257d1b4Sraf  */
26*7257d1b4Sraf 
27*7257d1b4Sraf /*
287c478bd9Sstevel@tonic-gate  *	nis/getrpcent.c -- "nis" backend for nsswitch "rpc" database
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "nis_common.h"
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <synch.h>
367c478bd9Sstevel@tonic-gate #include <rpc/rpcent.h>
377c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
387c478bd9Sstevel@tonic-gate #include <thread.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static int
check_name(args)417c478bd9Sstevel@tonic-gate check_name(args)
427c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
437c478bd9Sstevel@tonic-gate {
44cb5caa98Sdjl 	struct rpcent		*rpc	= (struct rpcent *)args->returnval;
457c478bd9Sstevel@tonic-gate 	const char		*name	= args->key.name;
467c478bd9Sstevel@tonic-gate 	char			**aliasp;
477c478bd9Sstevel@tonic-gate 
48cb5caa98Sdjl 	if (rpc) {
49cb5caa98Sdjl 		if (strcmp(rpc->r_name, name) == 0) {
507c478bd9Sstevel@tonic-gate 			return (1);
517c478bd9Sstevel@tonic-gate 		}
52cb5caa98Sdjl 		for (aliasp = rpc->r_aliases;  *aliasp != 0;  aliasp++) {
53cb5caa98Sdjl 			if (strcmp(*aliasp, name) == 0) {
54cb5caa98Sdjl 				return (1);
55cb5caa98Sdjl 			}
56cb5caa98Sdjl 		}
57cb5caa98Sdjl 		return (0);
58cb5caa98Sdjl 	} else {
59cb5caa98Sdjl 		/*
60cb5caa98Sdjl 		 *  NSS2: nscd is running.
61cb5caa98Sdjl 		 */
62cb5caa98Sdjl 		return (_nss_nis_check_name_aliases(args,
63cb5caa98Sdjl 					(const char *)args->buf.buffer,
64cb5caa98Sdjl 					strlen(args->buf.buffer)));
65cb5caa98Sdjl 
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static mutex_t	no_byname_lock	= DEFAULTMUTEX;
707c478bd9Sstevel@tonic-gate static int	no_byname_map	= 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(be,a)737c478bd9Sstevel@tonic-gate getbyname(be, a)
747c478bd9Sstevel@tonic-gate 	nis_backend_ptr_t	be;
757c478bd9Sstevel@tonic-gate 	void			*a;
767c478bd9Sstevel@tonic-gate {
77cb5caa98Sdjl 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
787c478bd9Sstevel@tonic-gate 	int			no_map;
797c478bd9Sstevel@tonic-gate 	sigset_t		oldmask, newmask;
807c478bd9Sstevel@tonic-gate 
81cb5caa98Sdjl 	(void) sigfillset(&newmask);
82*7257d1b4Sraf 	(void) thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
83*7257d1b4Sraf 	(void) mutex_lock(&no_byname_lock);
847c478bd9Sstevel@tonic-gate 	no_map = no_byname_map;
85*7257d1b4Sraf 	(void) mutex_unlock(&no_byname_lock);
86*7257d1b4Sraf 	(void) thr_sigsetmask(SIG_SETMASK, &oldmask, (sigset_t *)NULL);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (no_map == 0) {
897c478bd9Sstevel@tonic-gate 		int		yp_status;
907c478bd9Sstevel@tonic-gate 		nss_status_t	res;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		res = _nss_nis_lookup(be, argp, 1, "rpc.byname",
93cb5caa98Sdjl 					argp->key.name, &yp_status);
947c478bd9Sstevel@tonic-gate 		if (yp_status == YPERR_MAP) {
95cb5caa98Sdjl 			(void) sigfillset(&newmask);
96*7257d1b4Sraf 			(void) thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
97*7257d1b4Sraf 			(void) mutex_lock(&no_byname_lock);
987c478bd9Sstevel@tonic-gate 			no_byname_map = 1;
99*7257d1b4Sraf 			(void) mutex_unlock(&no_byname_lock);
100*7257d1b4Sraf 			(void) thr_sigsetmask(SIG_SETMASK, &oldmask,
101cb5caa98Sdjl 					(sigset_t *)NULL);
1027c478bd9Sstevel@tonic-gate 		} else /* if (res == NSS_SUCCESS) <==== */ {
1037c478bd9Sstevel@tonic-gate 			return (res);
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	return (_nss_nis_XY_all(be, argp, 1, argp->key.name, check_name));
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static nss_status_t
getbynumber(be,a)1117c478bd9Sstevel@tonic-gate getbynumber(be, a)
1127c478bd9Sstevel@tonic-gate 	nis_backend_ptr_t	be;
1137c478bd9Sstevel@tonic-gate 	void			*a;
1147c478bd9Sstevel@tonic-gate {
115cb5caa98Sdjl 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
1167c478bd9Sstevel@tonic-gate 	char			numstr[12];
1177c478bd9Sstevel@tonic-gate 
118cb5caa98Sdjl 	(void) sprintf(numstr, "%d", argp->key.number);
1197c478bd9Sstevel@tonic-gate 	return (_nss_nis_lookup(be, argp, 1, "rpc.bynumber", numstr, 0));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static nis_backend_op_t rpc_ops[] = {
1237c478bd9Sstevel@tonic-gate 	_nss_nis_destr,
1247c478bd9Sstevel@tonic-gate 	_nss_nis_endent,
1257c478bd9Sstevel@tonic-gate 	_nss_nis_setent,
1267c478bd9Sstevel@tonic-gate 	_nss_nis_getent_netdb,
1277c478bd9Sstevel@tonic-gate 	getbyname,
1287c478bd9Sstevel@tonic-gate 	getbynumber
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1327c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_nis_rpc_constr(dummy1,dummy2,dummy3)1337c478bd9Sstevel@tonic-gate _nss_nis_rpc_constr(dummy1, dummy2, dummy3)
1347c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	return (_nss_nis_constr(rpc_ops,
1377c478bd9Sstevel@tonic-gate 				sizeof (rpc_ops) / sizeof (rpc_ops[0]),
1387c478bd9Sstevel@tonic-gate 				"rpc.bynumber"));
1397c478bd9Sstevel@tonic-gate }
140