xref: /onnv-gate/usr/src/cmd/fs.d/nfs/lib/nfs_resolve.c (revision 11291:80bdcd03e626)
1*11291SRobert.Thurlow@Sun.COM /*
2*11291SRobert.Thurlow@Sun.COM  * CDDL HEADER START
3*11291SRobert.Thurlow@Sun.COM  *
4*11291SRobert.Thurlow@Sun.COM  * The contents of this file are subject to the terms of the
5*11291SRobert.Thurlow@Sun.COM  * Common Development and Distribution License (the "License").
6*11291SRobert.Thurlow@Sun.COM  * You may not use this file except in compliance with the License.
7*11291SRobert.Thurlow@Sun.COM  *
8*11291SRobert.Thurlow@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*11291SRobert.Thurlow@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*11291SRobert.Thurlow@Sun.COM  * See the License for the specific language governing permissions
11*11291SRobert.Thurlow@Sun.COM  * and limitations under the License.
12*11291SRobert.Thurlow@Sun.COM  *
13*11291SRobert.Thurlow@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*11291SRobert.Thurlow@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*11291SRobert.Thurlow@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*11291SRobert.Thurlow@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*11291SRobert.Thurlow@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*11291SRobert.Thurlow@Sun.COM  *
19*11291SRobert.Thurlow@Sun.COM  * CDDL HEADER END
20*11291SRobert.Thurlow@Sun.COM  */
21*11291SRobert.Thurlow@Sun.COM /*
22*11291SRobert.Thurlow@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*11291SRobert.Thurlow@Sun.COM  * Use is subject to license terms.
24*11291SRobert.Thurlow@Sun.COM  */
25*11291SRobert.Thurlow@Sun.COM 
26*11291SRobert.Thurlow@Sun.COM 
27*11291SRobert.Thurlow@Sun.COM /*
28*11291SRobert.Thurlow@Sun.COM  * Helper routines for  nfsmapid and autod daemon
29*11291SRobert.Thurlow@Sun.COM  * to translate hostname to IP address and Netinfo.
30*11291SRobert.Thurlow@Sun.COM  */
31*11291SRobert.Thurlow@Sun.COM #include <stdio.h>
32*11291SRobert.Thurlow@Sun.COM #include <stdlib.h>
33*11291SRobert.Thurlow@Sun.COM #include <alloca.h>
34*11291SRobert.Thurlow@Sun.COM #include <signal.h>
35*11291SRobert.Thurlow@Sun.COM #include <libintl.h>
36*11291SRobert.Thurlow@Sun.COM #include <limits.h>
37*11291SRobert.Thurlow@Sun.COM #include <errno.h>
38*11291SRobert.Thurlow@Sun.COM #include <sys/types.h>
39*11291SRobert.Thurlow@Sun.COM #include <string.h>
40*11291SRobert.Thurlow@Sun.COM #include <memory.h>
41*11291SRobert.Thurlow@Sun.COM #include <pwd.h>
42*11291SRobert.Thurlow@Sun.COM #include <grp.h>
43*11291SRobert.Thurlow@Sun.COM #include <door.h>
44*11291SRobert.Thurlow@Sun.COM #include <syslog.h>
45*11291SRobert.Thurlow@Sun.COM #include <fcntl.h>
46*11291SRobert.Thurlow@Sun.COM #include <unistd.h>
47*11291SRobert.Thurlow@Sun.COM #include <assert.h>
48*11291SRobert.Thurlow@Sun.COM #include <deflt.h>
49*11291SRobert.Thurlow@Sun.COM #include <netdir.h>
50*11291SRobert.Thurlow@Sun.COM #include <nfs/nfs4.h>
51*11291SRobert.Thurlow@Sun.COM #include <nfs/nfssys.h>
52*11291SRobert.Thurlow@Sun.COM #include <nfs/nfsid_map.h>
53*11291SRobert.Thurlow@Sun.COM #include <nfs/mapid.h>
54*11291SRobert.Thurlow@Sun.COM #include <nfs/nfs_sec.h>
55*11291SRobert.Thurlow@Sun.COM #include <sys/sdt.h>
56*11291SRobert.Thurlow@Sun.COM #include <sys/idmap.h>
57*11291SRobert.Thurlow@Sun.COM #include <idmap.h>
58*11291SRobert.Thurlow@Sun.COM #include <sys/fs/autofs.h>
59*11291SRobert.Thurlow@Sun.COM #include "nfs_resolve.h"
60*11291SRobert.Thurlow@Sun.COM 
61*11291SRobert.Thurlow@Sun.COM void
free_knconf(struct knetconfig * k)62*11291SRobert.Thurlow@Sun.COM free_knconf(struct knetconfig *k)
63*11291SRobert.Thurlow@Sun.COM {
64*11291SRobert.Thurlow@Sun.COM 	if (k == NULL)
65*11291SRobert.Thurlow@Sun.COM 		return;
66*11291SRobert.Thurlow@Sun.COM 	if (k->knc_protofmly)
67*11291SRobert.Thurlow@Sun.COM 		free(k->knc_protofmly);
68*11291SRobert.Thurlow@Sun.COM 	if (k->knc_proto)
69*11291SRobert.Thurlow@Sun.COM 		free(k->knc_proto);
70*11291SRobert.Thurlow@Sun.COM 	free(k);
71*11291SRobert.Thurlow@Sun.COM }
72*11291SRobert.Thurlow@Sun.COM 
73*11291SRobert.Thurlow@Sun.COM struct knetconfig *
get_knconf(struct netconfig * nconf)74*11291SRobert.Thurlow@Sun.COM get_knconf(struct netconfig *nconf)
75*11291SRobert.Thurlow@Sun.COM {
76*11291SRobert.Thurlow@Sun.COM 	struct stat stbuf;
77*11291SRobert.Thurlow@Sun.COM 	struct knetconfig *k = NULL;
78*11291SRobert.Thurlow@Sun.COM 	int len;
79*11291SRobert.Thurlow@Sun.COM 
80*11291SRobert.Thurlow@Sun.COM 	if (stat(nconf->nc_device, &stbuf) < 0) {
81*11291SRobert.Thurlow@Sun.COM 		syslog(LOG_ERR, "get_knconf: stat %s: %m", nconf->nc_device);
82*11291SRobert.Thurlow@Sun.COM 		return (NULL);
83*11291SRobert.Thurlow@Sun.COM 	}
84*11291SRobert.Thurlow@Sun.COM 	k = (struct knetconfig *)malloc(sizeof (*k));
85*11291SRobert.Thurlow@Sun.COM 	if (k == NULL)
86*11291SRobert.Thurlow@Sun.COM 		goto nomem;
87*11291SRobert.Thurlow@Sun.COM 	k->knc_semantics = nconf->nc_semantics;
88*11291SRobert.Thurlow@Sun.COM 
89*11291SRobert.Thurlow@Sun.COM 	len = strlen(nconf->nc_protofmly);
90*11291SRobert.Thurlow@Sun.COM 	if (len <= 0)
91*11291SRobert.Thurlow@Sun.COM 		goto err_out;
92*11291SRobert.Thurlow@Sun.COM 	k->knc_protofmly = malloc(KNC_STRSIZE);
93*11291SRobert.Thurlow@Sun.COM 	if (k->knc_protofmly == NULL)
94*11291SRobert.Thurlow@Sun.COM 		goto nomem;
95*11291SRobert.Thurlow@Sun.COM 	bzero(k->knc_protofmly, KNC_STRSIZE);
96*11291SRobert.Thurlow@Sun.COM 	bcopy(nconf->nc_protofmly, k->knc_protofmly, len);
97*11291SRobert.Thurlow@Sun.COM 
98*11291SRobert.Thurlow@Sun.COM 	len = strlen(nconf->nc_proto);
99*11291SRobert.Thurlow@Sun.COM 	if (len <= 0)
100*11291SRobert.Thurlow@Sun.COM 		goto err_out;
101*11291SRobert.Thurlow@Sun.COM 	k->knc_proto = malloc(KNC_STRSIZE);
102*11291SRobert.Thurlow@Sun.COM 	if (k->knc_proto == NULL)
103*11291SRobert.Thurlow@Sun.COM 		goto nomem;
104*11291SRobert.Thurlow@Sun.COM 	bzero(k->knc_proto, KNC_STRSIZE);
105*11291SRobert.Thurlow@Sun.COM 	bcopy(nconf->nc_proto, k->knc_proto, len);
106*11291SRobert.Thurlow@Sun.COM 
107*11291SRobert.Thurlow@Sun.COM 	k->knc_rdev = stbuf.st_rdev;
108*11291SRobert.Thurlow@Sun.COM 
109*11291SRobert.Thurlow@Sun.COM 	return (k);
110*11291SRobert.Thurlow@Sun.COM 
111*11291SRobert.Thurlow@Sun.COM nomem:
112*11291SRobert.Thurlow@Sun.COM 	syslog(LOG_ERR, "get_knconf: no memory");
113*11291SRobert.Thurlow@Sun.COM err_out:
114*11291SRobert.Thurlow@Sun.COM 	if (k != NULL)
115*11291SRobert.Thurlow@Sun.COM 		(void) free_knconf(k);
116*11291SRobert.Thurlow@Sun.COM 	return (NULL);
117*11291SRobert.Thurlow@Sun.COM }
118*11291SRobert.Thurlow@Sun.COM 
119*11291SRobert.Thurlow@Sun.COM /*
120*11291SRobert.Thurlow@Sun.COM  * Get the information needed for an NFSv4.x referral. This
121*11291SRobert.Thurlow@Sun.COM  * information includes the netbuf, netname and knconfig.
122*11291SRobert.Thurlow@Sun.COM  */
123*11291SRobert.Thurlow@Sun.COM struct nfs_fsl_info *
get_nfs4ref_info(char * host,int port,int nfsver)124*11291SRobert.Thurlow@Sun.COM get_nfs4ref_info(char *host, int port, int nfsver)
125*11291SRobert.Thurlow@Sun.COM {
126*11291SRobert.Thurlow@Sun.COM 	char netname[MAXNETNAMELEN + 1];
127*11291SRobert.Thurlow@Sun.COM 	enum clnt_stat cstat;
128*11291SRobert.Thurlow@Sun.COM 	struct nfs_fsl_info *fsl_retp = NULL;
129*11291SRobert.Thurlow@Sun.COM 	struct netconfig *netconf = NULL;
130*11291SRobert.Thurlow@Sun.COM 	char *nametemp, *namex = NULL;
131*11291SRobert.Thurlow@Sun.COM 	struct netbuf *nb = NULL;
132*11291SRobert.Thurlow@Sun.COM 	NCONF_HANDLE *nc = NULL;
133*11291SRobert.Thurlow@Sun.COM 
134*11291SRobert.Thurlow@Sun.COM 	fsl_retp = calloc(1, sizeof (struct nfs_fsl_info));
135*11291SRobert.Thurlow@Sun.COM 	if (fsl_retp == NULL) {
136*11291SRobert.Thurlow@Sun.COM 		syslog(LOG_ERR, "get_nfs4ref_info: no memory\n");
137*11291SRobert.Thurlow@Sun.COM 		return (NULL);
138*11291SRobert.Thurlow@Sun.COM 	}
139*11291SRobert.Thurlow@Sun.COM 
140*11291SRobert.Thurlow@Sun.COM 	nametemp = malloc(MAXNETNAMELEN + 1);
141*11291SRobert.Thurlow@Sun.COM 	if (nametemp == NULL) {
142*11291SRobert.Thurlow@Sun.COM 		free(fsl_retp);
143*11291SRobert.Thurlow@Sun.COM 		return (NULL);
144*11291SRobert.Thurlow@Sun.COM 	}
145*11291SRobert.Thurlow@Sun.COM 	host2netname(nametemp, host, NULL);
146*11291SRobert.Thurlow@Sun.COM 	namex = calloc(1, strlen(nametemp) + 1);
147*11291SRobert.Thurlow@Sun.COM 	if (namex == NULL) {
148*11291SRobert.Thurlow@Sun.COM 		free(nametemp);
149*11291SRobert.Thurlow@Sun.COM 		free(fsl_retp);
150*11291SRobert.Thurlow@Sun.COM 		return (NULL);
151*11291SRobert.Thurlow@Sun.COM 	}
152*11291SRobert.Thurlow@Sun.COM 	strncpy(namex, nametemp, strlen(nametemp));
153*11291SRobert.Thurlow@Sun.COM 	free(nametemp);
154*11291SRobert.Thurlow@Sun.COM 	fsl_retp->netname = namex;
155*11291SRobert.Thurlow@Sun.COM 	fsl_retp->netnm_len = strlen(namex) + 1;
156*11291SRobert.Thurlow@Sun.COM 
157*11291SRobert.Thurlow@Sun.COM 	fsl_retp->addr = resolve_netconf(host, NFS_PROGRAM, nfsver,
158*11291SRobert.Thurlow@Sun.COM 	    &netconf, port, NULL, NULL, TRUE, NULL, &cstat);
159*11291SRobert.Thurlow@Sun.COM 
160*11291SRobert.Thurlow@Sun.COM 	if (netconf == NULL || fsl_retp->addr == NULL)
161*11291SRobert.Thurlow@Sun.COM 		goto done;
162*11291SRobert.Thurlow@Sun.COM 
163*11291SRobert.Thurlow@Sun.COM 	fsl_retp->knconf = get_knconf(netconf);
164*11291SRobert.Thurlow@Sun.COM 	if (fsl_retp->knconf == NULL)
165*11291SRobert.Thurlow@Sun.COM 		goto done;
166*11291SRobert.Thurlow@Sun.COM 	fsl_retp->knconf_len = (sizeof (struct knetconfig) +
167*11291SRobert.Thurlow@Sun.COM 	    (KNC_STRSIZE * 2));
168*11291SRobert.Thurlow@Sun.COM 	fsl_retp->netbuf_len = (sizeof (struct netbuf) +
169*11291SRobert.Thurlow@Sun.COM 	    fsl_retp->addr->maxlen);
170*11291SRobert.Thurlow@Sun.COM 	return (fsl_retp);
171*11291SRobert.Thurlow@Sun.COM done:
172*11291SRobert.Thurlow@Sun.COM 	free_nfs4ref_info(fsl_retp);
173*11291SRobert.Thurlow@Sun.COM 	return (NULL);
174*11291SRobert.Thurlow@Sun.COM }
175*11291SRobert.Thurlow@Sun.COM 
176*11291SRobert.Thurlow@Sun.COM void
free_nfs4ref_info(struct nfs_fsl_info * fsl_retp)177*11291SRobert.Thurlow@Sun.COM free_nfs4ref_info(struct nfs_fsl_info *fsl_retp)
178*11291SRobert.Thurlow@Sun.COM {
179*11291SRobert.Thurlow@Sun.COM 	if (fsl_retp == NULL)
180*11291SRobert.Thurlow@Sun.COM 		return;
181*11291SRobert.Thurlow@Sun.COM 	free_knconf(fsl_retp->knconf);
182*11291SRobert.Thurlow@Sun.COM 	free(fsl_retp->netname);
183*11291SRobert.Thurlow@Sun.COM 	if (fsl_retp->addr != NULL) {
184*11291SRobert.Thurlow@Sun.COM 		free(fsl_retp->addr->buf);
185*11291SRobert.Thurlow@Sun.COM 		free(fsl_retp->addr);
186*11291SRobert.Thurlow@Sun.COM 	}
187*11291SRobert.Thurlow@Sun.COM 	free(fsl_retp);
188*11291SRobert.Thurlow@Sun.COM }
189*11291SRobert.Thurlow@Sun.COM 
190*11291SRobert.Thurlow@Sun.COM void
cleanup_tli_parms(struct t_bind * tbind,int fd)191*11291SRobert.Thurlow@Sun.COM cleanup_tli_parms(struct t_bind *tbind, int fd)
192*11291SRobert.Thurlow@Sun.COM {
193*11291SRobert.Thurlow@Sun.COM 	if (tbind != NULL) {
194*11291SRobert.Thurlow@Sun.COM 		t_free((char *)tbind, T_BIND);
195*11291SRobert.Thurlow@Sun.COM 		tbind = NULL;
196*11291SRobert.Thurlow@Sun.COM 	}
197*11291SRobert.Thurlow@Sun.COM 	if (fd >= 0)
198*11291SRobert.Thurlow@Sun.COM 		(void) t_close(fd);
199*11291SRobert.Thurlow@Sun.COM 	fd = -1;
200*11291SRobert.Thurlow@Sun.COM }
201*11291SRobert.Thurlow@Sun.COM 
202*11291SRobert.Thurlow@Sun.COM struct netbuf *
resolve_netconf(char * host,rpcprog_t prog,rpcvers_t nfsver,struct netconfig ** netconf,ushort_t port,struct t_info * tinfo,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstatp)203*11291SRobert.Thurlow@Sun.COM resolve_netconf(char *host, rpcprog_t prog, rpcvers_t nfsver,
204*11291SRobert.Thurlow@Sun.COM     struct netconfig **netconf, ushort_t port,
205*11291SRobert.Thurlow@Sun.COM     struct t_info *tinfo, caddr_t *fhp, bool_t direct_to_server,
206*11291SRobert.Thurlow@Sun.COM     char *fspath, enum clnt_stat *cstatp)
207*11291SRobert.Thurlow@Sun.COM {
208*11291SRobert.Thurlow@Sun.COM 	NCONF_HANDLE *nc;
209*11291SRobert.Thurlow@Sun.COM 	struct netconfig *nconf = NULL;
210*11291SRobert.Thurlow@Sun.COM 	int nthtry = FIRST_TRY;
211*11291SRobert.Thurlow@Sun.COM 	struct netbuf *nb;
212*11291SRobert.Thurlow@Sun.COM 	enum clnt_stat cstat;
213*11291SRobert.Thurlow@Sun.COM 
214*11291SRobert.Thurlow@Sun.COM 	nc = setnetpath();
215*11291SRobert.Thurlow@Sun.COM 	if (nc == NULL)
216*11291SRobert.Thurlow@Sun.COM 		goto done;
217*11291SRobert.Thurlow@Sun.COM retry:
218*11291SRobert.Thurlow@Sun.COM 	while (nconf = getnetpath(nc)) {
219*11291SRobert.Thurlow@Sun.COM 		if (nconf->nc_flag & NC_VISIBLE) {
220*11291SRobert.Thurlow@Sun.COM 			if (nthtry == FIRST_TRY) {
221*11291SRobert.Thurlow@Sun.COM 				if ((nconf->nc_semantics ==
222*11291SRobert.Thurlow@Sun.COM 				    NC_TPI_COTS_ORD) ||
223*11291SRobert.Thurlow@Sun.COM 				    (nconf->nc_semantics ==
224*11291SRobert.Thurlow@Sun.COM 				    NC_TPI_COTS)) {
225*11291SRobert.Thurlow@Sun.COM 					if (port == 0)
226*11291SRobert.Thurlow@Sun.COM 						break;
227*11291SRobert.Thurlow@Sun.COM 					if ((strcmp(nconf->nc_protofmly,
228*11291SRobert.Thurlow@Sun.COM 					    NC_INET) == 0 ||
229*11291SRobert.Thurlow@Sun.COM 					    strcmp(nconf->nc_protofmly,
230*11291SRobert.Thurlow@Sun.COM 					    NC_INET6) == 0) &&
231*11291SRobert.Thurlow@Sun.COM 					    (strcmp(nconf->nc_proto,
232*11291SRobert.Thurlow@Sun.COM 					    NC_TCP) == 0))
233*11291SRobert.Thurlow@Sun.COM 						break;
234*11291SRobert.Thurlow@Sun.COM 				}
235*11291SRobert.Thurlow@Sun.COM 			}
236*11291SRobert.Thurlow@Sun.COM 			if (nthtry == SECOND_TRY) {
237*11291SRobert.Thurlow@Sun.COM 				if (nconf->nc_semantics ==
238*11291SRobert.Thurlow@Sun.COM 				    NC_TPI_CLTS) {
239*11291SRobert.Thurlow@Sun.COM 					if (port == 0)
240*11291SRobert.Thurlow@Sun.COM 						break;
241*11291SRobert.Thurlow@Sun.COM 					if ((strcmp(nconf->nc_protofmly,
242*11291SRobert.Thurlow@Sun.COM 					    NC_INET) == 0 ||
243*11291SRobert.Thurlow@Sun.COM 					    strcmp(nconf->nc_protofmly,
244*11291SRobert.Thurlow@Sun.COM 					    NC_INET6) == 0) &&
245*11291SRobert.Thurlow@Sun.COM 					    (strcmp(nconf->nc_proto,
246*11291SRobert.Thurlow@Sun.COM 					    NC_UDP) == 0))
247*11291SRobert.Thurlow@Sun.COM 						break;
248*11291SRobert.Thurlow@Sun.COM 					}
249*11291SRobert.Thurlow@Sun.COM 			}
250*11291SRobert.Thurlow@Sun.COM 		}
251*11291SRobert.Thurlow@Sun.COM 	} /* while */
252*11291SRobert.Thurlow@Sun.COM 	if (nconf == NULL) {
253*11291SRobert.Thurlow@Sun.COM 		if (++nthtry <= MNT_PREF_LISTLEN) {
254*11291SRobert.Thurlow@Sun.COM 			endnetpath(nc);
255*11291SRobert.Thurlow@Sun.COM 			if ((nc = setnetpath()) == NULL)
256*11291SRobert.Thurlow@Sun.COM 				goto done;
257*11291SRobert.Thurlow@Sun.COM 			goto retry;
258*11291SRobert.Thurlow@Sun.COM 		} else
259*11291SRobert.Thurlow@Sun.COM 			return (NULL);
260*11291SRobert.Thurlow@Sun.COM 	} else {
261*11291SRobert.Thurlow@Sun.COM 		nb = get_server_addr(host, NFS_PROGRAM, nfsver,
262*11291SRobert.Thurlow@Sun.COM 		    nconf, port, NULL, NULL, TRUE, NULL, &cstat);
263*11291SRobert.Thurlow@Sun.COM 		if (cstat != RPC_SUCCESS)
264*11291SRobert.Thurlow@Sun.COM 			goto retry;
265*11291SRobert.Thurlow@Sun.COM 	}
266*11291SRobert.Thurlow@Sun.COM done:
267*11291SRobert.Thurlow@Sun.COM 	*netconf = nconf;
268*11291SRobert.Thurlow@Sun.COM 	*cstatp = cstat;
269*11291SRobert.Thurlow@Sun.COM 	if (nc)
270*11291SRobert.Thurlow@Sun.COM 		endnetpath(nc);
271*11291SRobert.Thurlow@Sun.COM 	return (nb);
272*11291SRobert.Thurlow@Sun.COM }
273*11291SRobert.Thurlow@Sun.COM 
274*11291SRobert.Thurlow@Sun.COM int
setup_nb_parms(struct netconfig * nconf,struct t_bind * tbind,struct t_info * tinfo,char * hostname,int fd,bool_t direct_to_server,ushort_t port,rpcprog_t prog,rpcvers_t vers,bool_t file_handle)275*11291SRobert.Thurlow@Sun.COM setup_nb_parms(struct netconfig *nconf, struct t_bind *tbind,
276*11291SRobert.Thurlow@Sun.COM     struct t_info *tinfo, char *hostname, int fd, bool_t direct_to_server,
277*11291SRobert.Thurlow@Sun.COM     ushort_t port, rpcprog_t prog, rpcvers_t vers, bool_t file_handle)
278*11291SRobert.Thurlow@Sun.COM {
279*11291SRobert.Thurlow@Sun.COM 	if (nconf == NULL) {
280*11291SRobert.Thurlow@Sun.COM 		return (-1);
281*11291SRobert.Thurlow@Sun.COM 	}
282*11291SRobert.Thurlow@Sun.COM 	if (direct_to_server == TRUE) {
283*11291SRobert.Thurlow@Sun.COM 		struct nd_hostserv hs;
284*11291SRobert.Thurlow@Sun.COM 		struct nd_addrlist *retaddrs;
285*11291SRobert.Thurlow@Sun.COM 		hs.h_host = hostname;
286*11291SRobert.Thurlow@Sun.COM 
287*11291SRobert.Thurlow@Sun.COM 		if (port == 0)
288*11291SRobert.Thurlow@Sun.COM 			hs.h_serv = "nfs";
289*11291SRobert.Thurlow@Sun.COM 		else
290*11291SRobert.Thurlow@Sun.COM 			hs.h_serv = NULL;
291*11291SRobert.Thurlow@Sun.COM 
292*11291SRobert.Thurlow@Sun.COM 		if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK) {
293*11291SRobert.Thurlow@Sun.COM 			return (-1);
294*11291SRobert.Thurlow@Sun.COM 		}
295*11291SRobert.Thurlow@Sun.COM 		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
296*11291SRobert.Thurlow@Sun.COM 		    retaddrs->n_addrs->len);
297*11291SRobert.Thurlow@Sun.COM 		tbind->addr.len = retaddrs->n_addrs->len;
298*11291SRobert.Thurlow@Sun.COM 		tbind->addr.maxlen = retaddrs->n_addrs->maxlen;
299*11291SRobert.Thurlow@Sun.COM 		netdir_free((void *)retaddrs, ND_ADDRLIST);
300*11291SRobert.Thurlow@Sun.COM 		if (port) {
301*11291SRobert.Thurlow@Sun.COM 			/* LINTED pointer alignment */
302*11291SRobert.Thurlow@Sun.COM 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
303*11291SRobert.Thurlow@Sun.COM 				((struct sockaddr_in *)
304*11291SRobert.Thurlow@Sun.COM 				    tbind->addr.buf)->sin_port =
305*11291SRobert.Thurlow@Sun.COM 				    htons((ushort_t)port);
306*11291SRobert.Thurlow@Sun.COM 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
307*11291SRobert.Thurlow@Sun.COM 				((struct sockaddr_in6 *)
308*11291SRobert.Thurlow@Sun.COM 				    tbind->addr.buf)->sin6_port =
309*11291SRobert.Thurlow@Sun.COM 				    htons((ushort_t)port);
310*11291SRobert.Thurlow@Sun.COM 		}
311*11291SRobert.Thurlow@Sun.COM 
312*11291SRobert.Thurlow@Sun.COM 		if (file_handle) {
313*11291SRobert.Thurlow@Sun.COM 			if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd,
314*11291SRobert.Thurlow@Sun.COM 			    NULL) == -1)
315*11291SRobert.Thurlow@Sun.COM 				return (-1);
316*11291SRobert.Thurlow@Sun.COM 		}
317*11291SRobert.Thurlow@Sun.COM 	} else if (!file_handle) {
318*11291SRobert.Thurlow@Sun.COM 		if (port) {
319*11291SRobert.Thurlow@Sun.COM 			/* LINTED pointer alignment */
320*11291SRobert.Thurlow@Sun.COM 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
321*11291SRobert.Thurlow@Sun.COM 				((struct sockaddr_in *)
322*11291SRobert.Thurlow@Sun.COM 				    tbind->addr.buf)->sin_port =
323*11291SRobert.Thurlow@Sun.COM 				    htons((ushort_t)port);
324*11291SRobert.Thurlow@Sun.COM 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
325*11291SRobert.Thurlow@Sun.COM 				((struct sockaddr_in6 *)
326*11291SRobert.Thurlow@Sun.COM 				    tbind->addr.buf)->sin6_port =
327*11291SRobert.Thurlow@Sun.COM 				    htons((ushort_t)port);
328*11291SRobert.Thurlow@Sun.COM 		}
329*11291SRobert.Thurlow@Sun.COM 	} else {
330*11291SRobert.Thurlow@Sun.COM 		return (-1);
331*11291SRobert.Thurlow@Sun.COM 	}
332*11291SRobert.Thurlow@Sun.COM 	return (1);
333*11291SRobert.Thurlow@Sun.COM }
334*11291SRobert.Thurlow@Sun.COM 
335*11291SRobert.Thurlow@Sun.COM /*
336*11291SRobert.Thurlow@Sun.COM  * Sets up TLI interface and finds the address withe netdir_getbyname().
337*11291SRobert.Thurlow@Sun.COM  * returns the address returned from the call.
338*11291SRobert.Thurlow@Sun.COM  * Caller frees up the memory allocated here.
339*11291SRobert.Thurlow@Sun.COM  */
340*11291SRobert.Thurlow@Sun.COM struct netbuf *
get_server_addr(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat)341*11291SRobert.Thurlow@Sun.COM get_server_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
342*11291SRobert.Thurlow@Sun.COM     struct netconfig *nconf, ushort_t port,
343*11291SRobert.Thurlow@Sun.COM     struct t_info *tinfo, caddr_t *fhp, bool_t direct_to_server,
344*11291SRobert.Thurlow@Sun.COM     char *fspath, enum clnt_stat *cstat)
345*11291SRobert.Thurlow@Sun.COM {
346*11291SRobert.Thurlow@Sun.COM 	int fd = -1;
347*11291SRobert.Thurlow@Sun.COM 	struct t_bind *tbind = NULL;
348*11291SRobert.Thurlow@Sun.COM 	enum clnt_stat cs = RPC_SYSTEMERROR;
349*11291SRobert.Thurlow@Sun.COM 	struct netbuf *nb = NULL;
350*11291SRobert.Thurlow@Sun.COM 	int ret = -1;
351*11291SRobert.Thurlow@Sun.COM 
352*11291SRobert.Thurlow@Sun.COM 	if (prog == NFS_PROGRAM && vers == NFS_V4)
353*11291SRobert.Thurlow@Sun.COM 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
354*11291SRobert.Thurlow@Sun.COM 			goto done;
355*11291SRobert.Thurlow@Sun.COM 
356*11291SRobert.Thurlow@Sun.COM 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0)
357*11291SRobert.Thurlow@Sun.COM 		goto done;
358*11291SRobert.Thurlow@Sun.COM 
359*11291SRobert.Thurlow@Sun.COM 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == NULL)
360*11291SRobert.Thurlow@Sun.COM 		goto done;
361*11291SRobert.Thurlow@Sun.COM 
362*11291SRobert.Thurlow@Sun.COM 	if (setup_nb_parms(nconf, tbind, tinfo, hostname, fd, direct_to_server,
363*11291SRobert.Thurlow@Sun.COM 	    port, prog, vers, 0) < 0)
364*11291SRobert.Thurlow@Sun.COM 		goto done;
365*11291SRobert.Thurlow@Sun.COM 
366*11291SRobert.Thurlow@Sun.COM 	nb = (struct netbuf *)malloc(sizeof (struct netbuf));
367*11291SRobert.Thurlow@Sun.COM 	if (nb == NULL) {
368*11291SRobert.Thurlow@Sun.COM 		syslog(LOG_ERR, "no memory\n");
369*11291SRobert.Thurlow@Sun.COM 		goto done;
370*11291SRobert.Thurlow@Sun.COM 	}
371*11291SRobert.Thurlow@Sun.COM 	nb->buf = (char *)malloc(tbind->addr.maxlen);
372*11291SRobert.Thurlow@Sun.COM 	if (nb->buf == NULL) {
373*11291SRobert.Thurlow@Sun.COM 		syslog(LOG_ERR, "no memory\n");
374*11291SRobert.Thurlow@Sun.COM 		free(nb);
375*11291SRobert.Thurlow@Sun.COM 		nb = NULL;
376*11291SRobert.Thurlow@Sun.COM 		goto done;
377*11291SRobert.Thurlow@Sun.COM 	}
378*11291SRobert.Thurlow@Sun.COM 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
379*11291SRobert.Thurlow@Sun.COM 	nb->len = tbind->addr.len;
380*11291SRobert.Thurlow@Sun.COM 	nb->maxlen = tbind->addr.maxlen;
381*11291SRobert.Thurlow@Sun.COM 	cs = RPC_SUCCESS;
382*11291SRobert.Thurlow@Sun.COM done:
383*11291SRobert.Thurlow@Sun.COM 	*cstat = cs;
384*11291SRobert.Thurlow@Sun.COM 	cleanup_tli_parms(tbind, fd);
385*11291SRobert.Thurlow@Sun.COM 	return (nb);
386*11291SRobert.Thurlow@Sun.COM }
387