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