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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  *	nis/getrpcent.c -- "nis" backend for nsswitch "rpc" database
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include "nis_common.h"
31 #include <stdio.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <synch.h>
35 #include <rpc/rpcent.h>
36 #include <rpcsvc/ypclnt.h>
37 #include <thread.h>
38 
39 static int
40 check_name(args)
41 	nss_XbyY_args_t		*args;
42 {
43 	struct rpcent		*rpc	= (struct rpcent *)args->returnval;
44 	const char		*name	= args->key.name;
45 	char			**aliasp;
46 
47 	if (rpc) {
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 	} else {
58 		/*
59 		 *  NSS2: nscd is running.
60 		 */
61 		return (_nss_nis_check_name_aliases(args,
62 					(const char *)args->buf.buffer,
63 					strlen(args->buf.buffer)));
64 
65 	}
66 }
67 
68 static mutex_t	no_byname_lock	= DEFAULTMUTEX;
69 static int	no_byname_map	= 0;
70 
71 static nss_status_t
72 getbyname(be, a)
73 	nis_backend_ptr_t	be;
74 	void			*a;
75 {
76 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
77 	int			no_map;
78 	sigset_t		oldmask, newmask;
79 
80 	(void) sigfillset(&newmask);
81 	(void) _thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
82 	(void) _mutex_lock(&no_byname_lock);
83 	no_map = no_byname_map;
84 	(void) _mutex_unlock(&no_byname_lock);
85 	(void) _thr_sigsetmask(SIG_SETMASK, &oldmask, (sigset_t *)NULL);
86 
87 	if (no_map == 0) {
88 		int		yp_status;
89 		nss_status_t	res;
90 
91 		res = _nss_nis_lookup(be, argp, 1, "rpc.byname",
92 					argp->key.name, &yp_status);
93 		if (yp_status == YPERR_MAP) {
94 			(void) sigfillset(&newmask);
95 			_thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
96 			_mutex_lock(&no_byname_lock);
97 			no_byname_map = 1;
98 			_mutex_unlock(&no_byname_lock);
99 			_thr_sigsetmask(SIG_SETMASK, &oldmask,
100 					(sigset_t *)NULL);
101 		} else /* if (res == NSS_SUCCESS) <==== */ {
102 			return (res);
103 		}
104 	}
105 
106 	return (_nss_nis_XY_all(be, argp, 1, argp->key.name, check_name));
107 }
108 
109 static nss_status_t
110 getbynumber(be, a)
111 	nis_backend_ptr_t	be;
112 	void			*a;
113 {
114 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
115 	char			numstr[12];
116 
117 	(void) sprintf(numstr, "%d", argp->key.number);
118 	return (_nss_nis_lookup(be, argp, 1, "rpc.bynumber", numstr, 0));
119 }
120 
121 static nis_backend_op_t rpc_ops[] = {
122 	_nss_nis_destr,
123 	_nss_nis_endent,
124 	_nss_nis_setent,
125 	_nss_nis_getent_netdb,
126 	getbyname,
127 	getbynumber
128 };
129 
130 /*ARGSUSED*/
131 nss_backend_t *
132 _nss_nis_rpc_constr(dummy1, dummy2, dummy3)
133 	const char	*dummy1, *dummy2, *dummy3;
134 {
135 	return (_nss_nis_constr(rpc_ops,
136 				sizeof (rpc_ops) / sizeof (rpc_ops[0]),
137 				"rpc.bynumber"));
138 }
139