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