xref: /titanic_51/usr/src/lib/libnsl/rpc/pmap_clnt.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
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 /*
23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 #ifdef PORTMAP
35 
36 /*
37  * interface to pmap rpc service.
38  */
39 #include "mt.h"
40 #include "rpc_mt.h"
41 #include <rpc/rpc.h>
42 #include <rpc/nettype.h>
43 #include <netdir.h>
44 #include <rpc/pmap_prot.h>
45 #include <rpc/pmap_clnt.h>
46 #include <rpc/pmap_rmt.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <netinet/in.h>
50 #include <sys/socket.h>
51 #include <unistd.h>
52 
53 int use_portmapper = 0;
54 static const struct timeval timeout = { 5, 0 };
55 static const struct timeval tottimeout = { 60, 0 };
56 static const struct timeval rmttimeout = { 3, 0 };
57 
58 /*
59  * Set a mapping between program, version and port.
60  * Calls the pmap service remotely to do the mapping.
61  */
62 bool_t
63 pmap_set(rpcprog_t program, rpcvers_t version, rpcprot_t protocol,
64 								ushort_t port)
65 {
66 	bool_t rslt;
67 	struct netbuf *na;
68 	struct netconfig *nconf;
69 	char buf[32];
70 
71 	if ((protocol != IPPROTO_UDP) && (protocol != IPPROTO_TCP))
72 		return (FALSE);
73 	nconf = __rpc_getconfip(protocol == IPPROTO_UDP ? "udp" : "tcp");
74 	if (!nconf)
75 		return (FALSE);
76 	(void) sprintf(buf, "0.0.0.0.%d.%d", port >> 8 & 0xff, port & 0xff);
77 	na = uaddr2taddr(nconf, buf);
78 	if (!na) {
79 		freenetconfigent(nconf);
80 		return (FALSE);
81 	}
82 	rslt = rpcb_set(program, version, nconf, na);
83 
84 	netdir_free((char *)na, ND_ADDR);
85 	freenetconfigent(nconf);
86 	return (rslt);
87 }
88 
89 /*
90  * Remove the mapping between program, version and port.
91  * Calls the pmap service remotely to do the un-mapping.
92  */
93 bool_t
94 pmap_unset(rpcprog_t program, rpcvers_t version)
95 {
96 	struct netconfig *nconf;
97 	bool_t udp_rslt = FALSE;
98 	bool_t tcp_rslt = FALSE;
99 
100 	nconf = __rpc_getconfip("udp");
101 	if (nconf) {
102 		udp_rslt = rpcb_unset(program, version, nconf);
103 		freenetconfigent(nconf);
104 	}
105 	nconf = __rpc_getconfip("tcp");
106 	if (nconf) {
107 		tcp_rslt = rpcb_unset(program, version, nconf);
108 		freenetconfigent(nconf);
109 	}
110 	/*
111 	 * XXX: The call may still succeed even if only one of the
112 	 * calls succeeded.  This was the best that could be
113 	 * done for backward compatibility.
114 	 */
115 	return (tcp_rslt || udp_rslt);
116 }
117 
118 /*
119  * Find the mapped port for program, version.
120  * Calls the pmap service remotely to do the lookup.
121  * Returns 0 if no map exists.
122  *
123  * XXX: It talks only to the portmapper and not to the rpcbind
124  * service.  There may be implementations out there which do not
125  * run portmapper as a part of rpcbind.
126  */
127 ushort_t
128 pmap_getport(struct sockaddr_in *address, rpcprog_t program,
129 					rpcvers_t version, rpcprot_t protocol)
130 {
131 	ushort_t port = 0;
132 	int fd = RPC_ANYFD;
133 	CLIENT *client;
134 	struct pmap parms;
135 
136 	address->sin_port = htons(PMAPPORT);
137 	client = clntudp_bufcreate(address, PMAPPROG, PMAPVERS, timeout,
138 				&fd, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
139 	if (client != NULL) {
140 		parms.pm_prog = program;
141 		parms.pm_vers = version;
142 		parms.pm_prot = protocol;
143 		parms.pm_port = 0;	/* not needed or used */
144 		if (CLNT_CALL(client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
145 			    (caddr_t)&parms, (xdrproc_t)xdr_u_short,
146 			    (caddr_t)&port, tottimeout) != RPC_SUCCESS) {
147 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
148 			clnt_geterr(client, &rpc_createerr.cf_error);
149 		} else if (port == 0) {
150 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
151 		}
152 		CLNT_DESTROY(client);
153 	}
154 	address->sin_port = 0;
155 	return (port);
156 }
157 
158 /*
159  * Get a copy of the current port maps.
160  * Calls the pmap service remotely to do get the maps.
161  */
162 struct pmaplist *
163 pmap_getmaps(struct sockaddr_in *address)
164 {
165 	pmaplist_ptr head = NULL;
166 	int fd = RPC_ANYFD;
167 	struct timeval minutetimeout;
168 	CLIENT *client;
169 
170 	minutetimeout.tv_sec = 60;
171 	minutetimeout.tv_usec = 0;
172 	address->sin_port = htons(PMAPPORT);
173 	client = clnttcp_create(address, PMAPPROG, PMAPVERS, &fd, 50, 500);
174 	if (client != NULL) {
175 		if (CLNT_CALL(client, PMAPPROC_DUMP, (xdrproc_t)xdr_void,
176 			    NULL, (xdrproc_t)xdr_pmaplist_ptr,
177 			    (caddr_t)&head, minutetimeout) != RPC_SUCCESS) {
178 			(void) syslog(LOG_ERR, "%s",
179 			clnt_sperror(client, "pmap_getmaps rpc problem"));
180 		}
181 		CLNT_DESTROY(client);
182 	}
183 	address->sin_port = 0;
184 	return ((struct pmaplist *)head);
185 }
186 
187 /*
188  * pmapper remote-call-service interface.
189  * This routine is used to call the pmapper remote call service
190  * which will look up a service program in the port maps, and then
191  * remotely call that routine with the given parameters. This allows
192  * programs to do a lookup and call in one step.
193  */
194 enum clnt_stat
195 pmap_rmtcall(struct sockaddr_in *addr, rpcprog_t prog, rpcvers_t vers,
196 	rpcproc_t proc, xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres,
197 	caddr_t resp, struct timeval tout, rpcport_t *port_ptr)
198 {
199 	int fd = RPC_ANYFD;
200 	CLIENT *client;
201 	struct p_rmtcallargs a;
202 	struct p_rmtcallres r;
203 	enum clnt_stat stat;
204 	short tmp = addr->sin_port;
205 
206 	addr->sin_port = htons(PMAPPORT);
207 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, rmttimeout, &fd);
208 	if (client != NULL) {
209 		a.prog = prog;
210 		a.vers = vers;
211 		a.proc = proc;
212 		a.args.args_val = argsp;
213 		a.xdr_args = xdrargs;
214 		r.res.res_val = resp;
215 		r.xdr_res = xdrres;
216 		stat = CLNT_CALL(client, PMAPPROC_CALLIT,
217 				(xdrproc_t)xdr_rmtcallargs,
218 				(caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
219 				(caddr_t)&r, tout);
220 		CLNT_DESTROY(client);
221 	} else {
222 		stat = RPC_FAILED;
223 	}
224 	addr->sin_port = tmp;
225 	*port_ptr = r.port;
226 	return (stat);
227 }
228 
229 #endif /* PORTMAP */
230