xref: /titanic_50/usr/src/lib/nsswitch/nis/common/getrpcent.c (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1988-1992 Sun Microsystems Inc
24  *	All Rights Reserved.
25  *
26  *	nis/getrpcent.c -- "nis" backend for nsswitch "rpc" database
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include "nis_common.h"
32 #include <stdio.h>
33 #include <string.h>
34 #include <signal.h>
35 #include <synch.h>
36 #include <rpc/rpcent.h>
37 #include <rpcsvc/ypclnt.h>
38 #include <thread.h>
39 
40 static int
41 check_name(args)
42 	nss_XbyY_args_t		*args;
43 {
44 	struct rpcent		*rpc	= (struct rpcent *) args->returnval;
45 	const char		*name	= args->key.name;
46 	char			**aliasp;
47 
48 	if (strcmp(rpc->r_name, name) == 0) {
49 		return (1);
50 	}
51 	for (aliasp = rpc->r_aliases;  *aliasp != 0;  aliasp++) {
52 		if (strcmp(*aliasp, name) == 0) {
53 			return (1);
54 		}
55 	}
56 	return (0);
57 }
58 
59 static mutex_t	no_byname_lock	= DEFAULTMUTEX;
60 static int	no_byname_map	= 0;
61 
62 static nss_status_t
63 getbyname(be, a)
64 	nis_backend_ptr_t	be;
65 	void			*a;
66 {
67 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
68 	int			no_map;
69 	sigset_t		oldmask, newmask;
70 
71 	sigfillset(&newmask);
72 	(void) _thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
73 	(void) _mutex_lock(&no_byname_lock);
74 	no_map = no_byname_map;
75 	(void) _mutex_unlock(&no_byname_lock);
76 	(void) _thr_sigsetmask(SIG_SETMASK, &oldmask, (sigset_t*)NULL);
77 
78 	if (no_map == 0) {
79 		int		yp_status;
80 		nss_status_t	res;
81 
82 		res = _nss_nis_lookup(be, argp, 1, "rpc.byname",
83 				      argp->key.name, &yp_status);
84 		if (yp_status == YPERR_MAP) {
85 			sigfillset(&newmask);
86 			_thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
87 			_mutex_lock(&no_byname_lock);
88 			no_byname_map = 1;
89 			_mutex_unlock(&no_byname_lock);
90 			_thr_sigsetmask(SIG_SETMASK, &oldmask, (sigset_t*)NULL);
91 		} else /* if (res == NSS_SUCCESS) <==== */ {
92 			return (res);
93 		}
94 	}
95 
96 	return (_nss_nis_XY_all(be, argp, 1, argp->key.name, check_name));
97 }
98 
99 static nss_status_t
100 getbynumber(be, a)
101 	nis_backend_ptr_t	be;
102 	void			*a;
103 {
104 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
105 	char			numstr[12];
106 
107 	sprintf(numstr, "%d", argp->key.number);
108 	return (_nss_nis_lookup(be, argp, 1, "rpc.bynumber", numstr, 0));
109 }
110 
111 static nis_backend_op_t rpc_ops[] = {
112 	_nss_nis_destr,
113 	_nss_nis_endent,
114 	_nss_nis_setent,
115 	_nss_nis_getent_netdb,
116 	getbyname,
117 	getbynumber
118 };
119 
120 /*ARGSUSED*/
121 nss_backend_t *
122 _nss_nis_rpc_constr(dummy1, dummy2, dummy3)
123 	const char	*dummy1, *dummy2, *dummy3;
124 {
125 	return (_nss_nis_constr(rpc_ops,
126 				sizeof (rpc_ops) / sizeof (rpc_ops[0]),
127 				"rpc.bynumber"));
128 }
129