xref: /titanic_51/usr/src/lib/libbc/libc/yp/yp_bind.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
32*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
33*7c478bd9Sstevel@tonic-gate #include <rpc/pmap_prot.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "yp_prot.h"
39*7c478bd9Sstevel@tonic-gate #include "ypv1_prot.h"
40*7c478bd9Sstevel@tonic-gate #include "ypclnt.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * This is the same as struct dom_binding used by the base __yp_dobind().
44*7c478bd9Sstevel@tonic-gate  * Named differently here to avoid name conflict with the compat
45*7c478bd9Sstevel@tonic-gate  * struct dom_binding.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* Copied from base <sys/netconfig.h> */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate struct  netconfig {
51*7c478bd9Sstevel@tonic-gate 	char		*nc_netid;	/* network identifier		*/
52*7c478bd9Sstevel@tonic-gate 	unsigned long	nc_semantics;	/* defined below		*/
53*7c478bd9Sstevel@tonic-gate 	unsigned long	nc_flag;	/* defined below		*/
54*7c478bd9Sstevel@tonic-gate 	char		*nc_protofmly;	/* protocol family name		*/
55*7c478bd9Sstevel@tonic-gate 	char		*nc_proto;	/* protocol name		*/
56*7c478bd9Sstevel@tonic-gate 	char		*nc_device;	/* device name for network id	*/
57*7c478bd9Sstevel@tonic-gate 	unsigned long	nc_nlookups;	/* # of entries in nc_lookups	*/
58*7c478bd9Sstevel@tonic-gate 	char		**nc_lookups;	/* list of lookup directories	*/
59*7c478bd9Sstevel@tonic-gate 	unsigned long	nc_unused[8];
60*7c478bd9Sstevel@tonic-gate };
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* Copied from base <sys/tiuser.h> */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate struct netbuf {
65*7c478bd9Sstevel@tonic-gate 	unsigned int maxlen;
66*7c478bd9Sstevel@tonic-gate 	unsigned int len;
67*7c478bd9Sstevel@tonic-gate 	char *buf;
68*7c478bd9Sstevel@tonic-gate };
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate struct s5_dom_binding {
71*7c478bd9Sstevel@tonic-gate 	struct s5_dom_binding *dom_next;
72*7c478bd9Sstevel@tonic-gate 	char *dom_domain;
73*7c478bd9Sstevel@tonic-gate 	struct s5_ypbind_binding *dom_binding;
74*7c478bd9Sstevel@tonic-gate 	CLIENT *dom_client;
75*7c478bd9Sstevel@tonic-gate };
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate struct s5_ypbind_binding {
78*7c478bd9Sstevel@tonic-gate 	struct netconfig *ypbind_conf;
79*7c478bd9Sstevel@tonic-gate 	struct netbuf *ypbind_svcaddr;
80*7c478bd9Sstevel@tonic-gate 	char *ypbind_servername;
81*7c478bd9Sstevel@tonic-gate 	long ypbind_hi_vers;
82*7c478bd9Sstevel@tonic-gate 	long ypbind_lo_vers;
83*7c478bd9Sstevel@tonic-gate };
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static void _yp_unbind();
86*7c478bd9Sstevel@tonic-gate static struct	dom_binding *load_dom_binding_cache();
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate static struct dom_binding *bound_domains; /* List of bound domains */
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate  * This is a "wrapper" function that is implemented by the yp_bind()
92*7c478bd9Sstevel@tonic-gate  * function in base libnsl/yp.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate #ifdef NOTDEFINED
95*7c478bd9Sstevel@tonic-gate int
96*7c478bd9Sstevel@tonic-gate yp_bind(domain)
97*7c478bd9Sstevel@tonic-gate 	char *domain;
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	/* XXX */
100*7c478bd9Sstevel@tonic-gate 	_yp_bind(domain);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate #endif
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * Attempts to find a dom_binding in the list at bound_domains having the
106*7c478bd9Sstevel@tonic-gate  * domain name field equal to the passed domain name, and removes it if found.
107*7c478bd9Sstevel@tonic-gate  * The domain-server binding will not exist after the call to this function.
108*7c478bd9Sstevel@tonic-gate  * All resources associated with the binding will be freed.
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate #ifdef NOTDEFINED
111*7c478bd9Sstevel@tonic-gate void
112*7c478bd9Sstevel@tonic-gate yp_unbind (domain)
113*7c478bd9Sstevel@tonic-gate 	char *domain;
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	_yp_unbind(domain); /* clean our local cache */
116*7c478bd9Sstevel@tonic-gate 	/* XXX */
117*7c478bd9Sstevel@tonic-gate 	_yp_unbind(domain);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate #endif
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * This is a wrapper around the yp_get_default_domain()
123*7c478bd9Sstevel@tonic-gate  * function in base libnsl/yp.
124*7c478bd9Sstevel@tonic-gate  */
125*7c478bd9Sstevel@tonic-gate #ifdef NOTDEFINED
126*7c478bd9Sstevel@tonic-gate int
127*7c478bd9Sstevel@tonic-gate yp_get_default_domain(domain)
128*7c478bd9Sstevel@tonic-gate 	char **domain;
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	/* XXX */
131*7c478bd9Sstevel@tonic-gate 	_yp_get_default_domain(domain);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate #endif
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * Attempts to locate a NIS server that serves a passed domain.
137*7c478bd9Sstevel@tonic-gate  * This is a wrapper around the __yp_dobind() function in base
138*7c478bd9Sstevel@tonic-gate  * libnsl/yp; it converts the libnsl [netbuf based] dom_binding structure into
139*7c478bd9Sstevel@tonic-gate  * the [sockaddr based] one that is expected by binary compat apps. Note that,
140*7c478bd9Sstevel@tonic-gate  * the wrapper must allocate memory resources in order to hold
141*7c478bd9Sstevel@tonic-gate  * the
142*7c478bd9Sstevel@tonic-gate  */
143*7c478bd9Sstevel@tonic-gate int
144*7c478bd9Sstevel@tonic-gate _yp_dobind(domain, binding)
145*7c478bd9Sstevel@tonic-gate 	char *domain;
146*7c478bd9Sstevel@tonic-gate 	struct dom_binding **binding;	/* if result == 0, ptr to dom_binding */
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	int retval;
149*7c478bd9Sstevel@tonic-gate 	struct s5_dom_binding *dom_binding; /* Ptr to dom_binding from libnsl __yp_dobind() */
150*7c478bd9Sstevel@tonic-gate 	int status;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/* XXX */
153*7c478bd9Sstevel@tonic-gate 	retval = __yp_dobind(domain, &dom_binding);
154*7c478bd9Sstevel@tonic-gate 	if (retval != 0)
155*7c478bd9Sstevel@tonic-gate 		return(retval);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	if ((*binding = load_dom_binding_cache(domain, dom_binding)) == NULL)
158*7c478bd9Sstevel@tonic-gate 		return (YPERR_RESRC);		/* make sure it is in our cache */
159*7c478bd9Sstevel@tonic-gate 	return (0);				/* This is the go path */
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate /*
164*7c478bd9Sstevel@tonic-gate  * This allocates some memory for a domain binding, initialize it, and
165*7c478bd9Sstevel@tonic-gate  * returns a pointer to it.  Based on the program version we ended up
166*7c478bd9Sstevel@tonic-gate  * talking to ypbind with, fill out an opvector of appropriate protocol
167*7c478bd9Sstevel@tonic-gate  * modules.
168*7c478bd9Sstevel@tonic-gate  */
169*7c478bd9Sstevel@tonic-gate static struct dom_binding *
170*7c478bd9Sstevel@tonic-gate load_dom_binding_cache(domain, dom_binding)
171*7c478bd9Sstevel@tonic-gate 	char *domain;
172*7c478bd9Sstevel@tonic-gate 	struct s5_dom_binding *dom_binding;
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb = NULL;
175*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sa;	/* To get a port bound to socket */
176*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in local_name;
177*7c478bd9Sstevel@tonic-gate 	int local_name_len = sizeof(struct sockaddr_in);
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	for (pdomb = bound_domains; pdomb != NULL; pdomb = pdomb->dom_pnext) {
181*7c478bd9Sstevel@tonic-gate 		if (strcmp(domain, pdomb->dom_domain) == 0)
182*7c478bd9Sstevel@tonic-gate 				return (pdomb);
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if ((pdomb = (struct dom_binding *) malloc(sizeof(struct dom_binding)))
186*7c478bd9Sstevel@tonic-gate 		== NULL) {
187*7c478bd9Sstevel@tonic-gate 		(void) syslog(LOG_ERR, "load_dom_binding_cache:  malloc failure.");
188*7c478bd9Sstevel@tonic-gate 		return (struct dom_binding *) (NULL);
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	sa = (struct sockaddr_in *)dom_binding->dom_binding->ypbind_svcaddr->buf;
192*7c478bd9Sstevel@tonic-gate 	pdomb->dom_server_addr.sin_family = sa->sin_family;
193*7c478bd9Sstevel@tonic-gate 	pdomb->dom_server_addr.sin_port = sa->sin_port;
194*7c478bd9Sstevel@tonic-gate 	pdomb->dom_server_addr.sin_addr.s_addr = sa->sin_addr.s_addr;
195*7c478bd9Sstevel@tonic-gate 	bzero(pdomb->dom_server_addr.sin_zero, 8);
196*7c478bd9Sstevel@tonic-gate 	pdomb->dom_server_port = sa->sin_port;
197*7c478bd9Sstevel@tonic-gate 	pdomb->dom_socket = RPC_ANYSOCK;
198*7c478bd9Sstevel@tonic-gate 	pdomb->dom_vers = dom_binding->dom_binding->ypbind_hi_vers;
199*7c478bd9Sstevel@tonic-gate 	/* the claim is 5.0 CLIENT * can be used by a 4.x RPC user */
200*7c478bd9Sstevel@tonic-gate 	pdomb->dom_client = dom_binding->dom_client;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	(void) strcpy(pdomb->dom_domain, domain);/* Remember the domain name */
203*7c478bd9Sstevel@tonic-gate 	pdomb->dom_pnext = bound_domains;	/* Link this to the list as */
204*7c478bd9Sstevel@tonic-gate 	bound_domains = pdomb;			/* ... the head entry */
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return (pdomb);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate static void
210*7c478bd9Sstevel@tonic-gate _yp_unbind (domain)
211*7c478bd9Sstevel@tonic-gate 	char *domain;
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
214*7c478bd9Sstevel@tonic-gate 	struct dom_binding *ptrail = 0;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	if ( (domain == NULL) ||(strlen(domain) == 0) ) {
218*7c478bd9Sstevel@tonic-gate 		return;
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	for (pdomb = bound_domains; pdomb != NULL;
222*7c478bd9Sstevel@tonic-gate 	    ptrail = pdomb, pdomb = pdomb->dom_pnext) {
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		if (strcmp(domain, pdomb->dom_domain) == 0) {
225*7c478bd9Sstevel@tonic-gate 			if (pdomb == bound_domains)
226*7c478bd9Sstevel@tonic-gate 				bound_domains = pdomb->dom_pnext;
227*7c478bd9Sstevel@tonic-gate 			else
228*7c478bd9Sstevel@tonic-gate 				ptrail->dom_pnext = pdomb->dom_pnext;
229*7c478bd9Sstevel@tonic-gate 			free((char *) pdomb);
230*7c478bd9Sstevel@tonic-gate 			break;
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate int
236*7c478bd9Sstevel@tonic-gate yp_ismapthere(stat)
237*7c478bd9Sstevel@tonic-gate 	int stat;
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	switch (stat) {
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	case 0:  /* it actually succeeded! */
243*7c478bd9Sstevel@tonic-gate 	case YPERR_KEY:  /* no such key in map */
244*7c478bd9Sstevel@tonic-gate 	case YPERR_NOMORE:
245*7c478bd9Sstevel@tonic-gate 	case YPERR_BUSY:
246*7c478bd9Sstevel@tonic-gate 		return (TRUE);
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 	return (FALSE);
249*7c478bd9Sstevel@tonic-gate }
250