xref: /titanic_44/usr/src/lib/nsswitch/nis/common/getservent.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
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/getservent.c -- "nis" backend for nsswitch "services" 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 <malloc.h>
36 #include <netdb.h>
37 #include <synch.h>
38 #include <rpcsvc/ypclnt.h>
39 #include <thread.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 
43 static int
44 check_name(args)
45 	nss_XbyY_args_t		*args;
46 {
47 	struct servent		*serv	= (struct servent *) args->returnval;
48 	const char		*name	= args->key.serv.serv.name;
49 	const char		*proto	= args->key.serv.proto;
50 	char			**aliasp;
51 
52 	if (proto != 0 && strcmp(serv->s_proto, proto) != 0) {
53 		return (0);
54 	}
55 	if (strcmp(serv->s_name, name) == 0) {
56 		return (1);
57 	}
58 	for (aliasp = serv->s_aliases;  *aliasp != 0;  aliasp++) {
59 		if (strcmp(*aliasp, name) == 0) {
60 			return (1);
61 		}
62 	}
63 	return (0);
64 }
65 
66 static mutex_t	no_byname_lock	= DEFAULTMUTEX;
67 static int	no_byname_map	= 0;
68 
69 static nss_status_t
70 getbyname(be, a)
71 	nis_backend_ptr_t	be;
72 	void			*a;
73 {
74 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
75 	const char		*name	= argp->key.serv.serv.name;
76 	const char		*proto	= argp->key.serv.proto;
77 	int			no_map;
78 	sigset_t		oldmask, newmask;
79 
80 	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, NULL);
86 
87 	if (no_map == 0) {
88 		int		yp_status;
89 		nss_status_t	res;
90 
91 		if (proto == 0) {
92 			res = _nss_nis_lookup(be, argp, 1,
93 			    "services.byservicename", name, &yp_status);
94 		} else {
95 			char *key = malloc(strlen(name) + strlen(proto) + 3);
96 
97 			if (key == 0) {
98 				return (NSS_UNAVAIL);
99 			}
100 			sprintf(key, "%s/%s", name, proto);
101 			res = _nss_nis_lookup(be, argp, 1,
102 			    "services.byservicename", key, &yp_status);
103 			free(key);
104 		}
105 
106 		if (yp_status == YPERR_MAP) {
107 			sigfillset(&newmask);
108 			_thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
109 			_mutex_lock(&no_byname_lock);
110 			no_byname_map = 1;
111 			_mutex_unlock(&no_byname_lock);
112 			_thr_sigsetmask(SIG_SETMASK, &oldmask, (sigset_t*)NULL);
113 		} else /* if (res == NSS_SUCCESS) <==== */ {
114 			return (res);
115 		}
116 	}
117 
118 	return (_nss_nis_XY_all(be, argp, 1, name, check_name));
119 }
120 
121 static int
122 check_port(args)
123 	nss_XbyY_args_t		*args;
124 {
125 	struct servent		*serv	= (struct servent *) args->returnval;
126 
127 	/*
128 	 * We only resorted to _nss_nis_XY_all because proto == 0, so just...
129 	 */
130 	return (serv->s_port == args->key.serv.serv.port);
131 }
132 
133 static nss_status_t
134 getbyport(be, a)
135 	nis_backend_ptr_t	be;
136 	void			*a;
137 {
138 	nss_XbyY_args_t		*argp	= (nss_XbyY_args_t *) a;
139 	int			port	= ntohs(argp->key.serv.serv.port);
140 	const char		*proto	= argp->key.serv.proto;
141 	char			*key;
142 	nss_status_t		res;
143 
144 	if (proto == 0) {
145 		char		portstr[12];
146 
147 		sprintf(portstr, "%d", port);
148 		return (_nss_nis_XY_all(be, argp, 1, portstr, check_port));
149 	}
150 
151 	if ((key = malloc(strlen(proto) + 14)) == 0) {
152 		return (NSS_UNAVAIL);
153 	}
154 	sprintf(key, "%d/%s", port, proto);
155 
156 	res = _nss_nis_lookup(be, argp, 1, "services.byname", key, 0);
157 
158 	free(key);
159 	return (res);
160 }
161 
162 static nis_backend_op_t serv_ops[] = {
163 	_nss_nis_destr,
164 	_nss_nis_endent,
165 	_nss_nis_setent,
166 	_nss_nis_getent_netdb,
167 	getbyname,
168 	getbyport
169 };
170 
171 /*ARGSUSED*/
172 nss_backend_t *
173 _nss_nis_services_constr(dummy1, dummy2, dummy3)
174 	const char	*dummy1, *dummy2, *dummy3;
175 {
176 	return (_nss_nis_constr(serv_ops,
177 				sizeof (serv_ops) / sizeof (serv_ops[0]),
178 				"services.byname"));
179 }
180