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 #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
check_name(args)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 (rpc) {
49 		if (strcmp(rpc->r_name, name) == 0) {
50 			return (1);
51 		}
52 		for (aliasp = rpc->r_aliases;  *aliasp != 0;  aliasp++) {
53 			if (strcmp(*aliasp, name) == 0) {
54 				return (1);
55 			}
56 		}
57 		return (0);
58 	} else {
59 		/*
60 		 *  NSS2: nscd is running.
61 		 */
62 		return (_nss_nis_check_name_aliases(args,
63 					(const char *)args->buf.buffer,
64 					strlen(args->buf.buffer)));
65 
66 	}
67 }
68 
69 static mutex_t	no_byname_lock	= DEFAULTMUTEX;
70 static int	no_byname_map	= 0;
71 
72 static nss_status_t
getbyname(be,a)73 getbyname(be, a)
74 	nis_backend_ptr_t	be;
75 	void			*a;
76 {
77 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
78 	int			no_map;
79 	sigset_t		oldmask, newmask;
80 
81 	(void) sigfillset(&newmask);
82 	(void) thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
83 	(void) mutex_lock(&no_byname_lock);
84 	no_map = no_byname_map;
85 	(void) mutex_unlock(&no_byname_lock);
86 	(void) thr_sigsetmask(SIG_SETMASK, &oldmask, (sigset_t *)NULL);
87 
88 	if (no_map == 0) {
89 		int		yp_status;
90 		nss_status_t	res;
91 
92 		res = _nss_nis_lookup(be, argp, 1, "rpc.byname",
93 					argp->key.name, &yp_status);
94 		if (yp_status == YPERR_MAP) {
95 			(void) sigfillset(&newmask);
96 			(void) thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
97 			(void) mutex_lock(&no_byname_lock);
98 			no_byname_map = 1;
99 			(void) mutex_unlock(&no_byname_lock);
100 			(void) thr_sigsetmask(SIG_SETMASK, &oldmask,
101 					(sigset_t *)NULL);
102 		} else /* if (res == NSS_SUCCESS) <==== */ {
103 			return (res);
104 		}
105 	}
106 
107 	return (_nss_nis_XY_all(be, argp, 1, argp->key.name, check_name));
108 }
109 
110 static nss_status_t
getbynumber(be,a)111 getbynumber(be, a)
112 	nis_backend_ptr_t	be;
113 	void			*a;
114 {
115 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
116 	char			numstr[12];
117 
118 	(void) sprintf(numstr, "%d", argp->key.number);
119 	return (_nss_nis_lookup(be, argp, 1, "rpc.bynumber", numstr, 0));
120 }
121 
122 static nis_backend_op_t rpc_ops[] = {
123 	_nss_nis_destr,
124 	_nss_nis_endent,
125 	_nss_nis_setent,
126 	_nss_nis_getent_netdb,
127 	getbyname,
128 	getbynumber
129 };
130 
131 /*ARGSUSED*/
132 nss_backend_t *
_nss_nis_rpc_constr(dummy1,dummy2,dummy3)133 _nss_nis_rpc_constr(dummy1, dummy2, dummy3)
134 	const char	*dummy1, *dummy2, *dummy3;
135 {
136 	return (_nss_nis_constr(rpc_ops,
137 				sizeof (rpc_ops) / sizeof (rpc_ops[0]),
138 				"rpc.bynumber"));
139 }
140