xref: /freebsd/crypto/krb5/src/lib/rpc/pmap_clnt.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* @(#)pmap_clnt.c	2.2 88/08/01 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  *     * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  *     * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert  *       the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert  *       distribution.
17*7f2fe78bSCy Schubert  *
18*7f2fe78bSCy Schubert  *     * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert  *       its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert  *       derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert  *
22*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert  */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)pmap_clnt.c 1.37 87/08/11 Copyr 1984 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert  * pmap_clnt.c
40*7f2fe78bSCy Schubert  * Client interface to pmap rpc service.
41*7f2fe78bSCy Schubert  */
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert #include <unistd.h>
44*7f2fe78bSCy Schubert #include <gssrpc/rpc.h>
45*7f2fe78bSCy Schubert #include <gssrpc/pmap_prot.h>
46*7f2fe78bSCy Schubert #include <gssrpc/pmap_clnt.h>
47*7f2fe78bSCy Schubert 
48*7f2fe78bSCy Schubert #if TARGET_OS_MAC
49*7f2fe78bSCy Schubert #include <sys/un.h>
50*7f2fe78bSCy Schubert #include <string.h>
51*7f2fe78bSCy Schubert #include <syslog.h>
52*7f2fe78bSCy Schubert #endif
53*7f2fe78bSCy Schubert 
54*7f2fe78bSCy Schubert static struct timeval timeout = { 5, 0 };
55*7f2fe78bSCy Schubert static struct timeval tottimeout = { 60, 0 };
56*7f2fe78bSCy Schubert 
57*7f2fe78bSCy Schubert void clnt_perror();
58*7f2fe78bSCy Schubert 
59*7f2fe78bSCy Schubert /*
60*7f2fe78bSCy Schubert  * Set a mapping between program,version and port.
61*7f2fe78bSCy Schubert  * Calls the pmap service remotely to do the mapping.
62*7f2fe78bSCy Schubert  */
63*7f2fe78bSCy Schubert bool_t
pmap_set(rpcprog_t program,rpcvers_t version,rpcprot_t protocol,u_int port)64*7f2fe78bSCy Schubert pmap_set(
65*7f2fe78bSCy Schubert 	rpcprog_t program,
66*7f2fe78bSCy Schubert 	rpcvers_t version,
67*7f2fe78bSCy Schubert 	rpcprot_t protocol,
68*7f2fe78bSCy Schubert 	u_int port)
69*7f2fe78bSCy Schubert {
70*7f2fe78bSCy Schubert 	struct sockaddr_in myaddress;
71*7f2fe78bSCy Schubert 	int sock = -1;
72*7f2fe78bSCy Schubert 	CLIENT *client;
73*7f2fe78bSCy Schubert 	struct pmap parms;
74*7f2fe78bSCy Schubert 	bool_t rslt;
75*7f2fe78bSCy Schubert 
76*7f2fe78bSCy Schubert 	get_myaddress(&myaddress);
77*7f2fe78bSCy Schubert 	client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
78*7f2fe78bSCy Schubert 	    timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
79*7f2fe78bSCy Schubert 	if (client == (CLIENT *)NULL)
80*7f2fe78bSCy Schubert 		return (FALSE);
81*7f2fe78bSCy Schubert 	parms.pm_prog = program;
82*7f2fe78bSCy Schubert 	parms.pm_vers = version;
83*7f2fe78bSCy Schubert 	parms.pm_prot = protocol;
84*7f2fe78bSCy Schubert 	parms.pm_port = port;
85*7f2fe78bSCy Schubert #if TARGET_OS_MAC
86*7f2fe78bSCy Schubert 	{
87*7f2fe78bSCy Schubert 	    /*
88*7f2fe78bSCy Schubert 	     * Poke launchd, then wait for portmap to start up.
89*7f2fe78bSCy Schubert 	     *
90*7f2fe78bSCy Schubert 	     * My impression is that the protocol involves getting
91*7f2fe78bSCy Schubert 	     * something back from the server.  So wait, briefly, to
92*7f2fe78bSCy Schubert 	     * see if it's going to send us something.  Then continue
93*7f2fe78bSCy Schubert 	     * on, regardless.  I don't actually check what the data
94*7f2fe78bSCy Schubert 	     * is, because I have no idea what sort of validation, if
95*7f2fe78bSCy Schubert 	     * any, is needed.
96*7f2fe78bSCy Schubert 	     *
97*7f2fe78bSCy Schubert 	     * However, for whatever reason, the socket seems to be
98*7f2fe78bSCy Schubert 	     * mode 700 owner root on my system, so if you don't
99*7f2fe78bSCy Schubert 	     * change its ownership or mode, and if the program isn't
100*7f2fe78bSCy Schubert 	     * running as root, you still lose.
101*7f2fe78bSCy Schubert 	     */
102*7f2fe78bSCy Schubert #define TICKLER_SOCKET "/var/run/portmap.socket"
103*7f2fe78bSCy Schubert 	    int tickle;
104*7f2fe78bSCy Schubert 	    struct sockaddr_un a = {
105*7f2fe78bSCy Schubert 		.sun_family = AF_UNIX,
106*7f2fe78bSCy Schubert 		.sun_path = TICKLER_SOCKET,
107*7f2fe78bSCy Schubert 		.sun_len = (sizeof(TICKLER_SOCKET)
108*7f2fe78bSCy Schubert 			    + offsetof(struct sockaddr_un, sun_path)),
109*7f2fe78bSCy Schubert 	    };
110*7f2fe78bSCy Schubert 
111*7f2fe78bSCy Schubert 	    if (sizeof(TICKLER_SOCKET) <= sizeof(a.sun_path)) {
112*7f2fe78bSCy Schubert 		tickle = socket(AF_UNIX, SOCK_STREAM, 0);
113*7f2fe78bSCy Schubert 		if (tickle >= 0) {
114*7f2fe78bSCy Schubert 		    if (connect(tickle, (struct sockaddr *)&a, a.sun_len) == 0
115*7f2fe78bSCy Schubert 			&& tickle < FD_SETSIZE) {
116*7f2fe78bSCy Schubert 			fd_set readfds;
117*7f2fe78bSCy Schubert 			struct timeval tv;
118*7f2fe78bSCy Schubert 
119*7f2fe78bSCy Schubert 			FD_ZERO(&readfds);
120*7f2fe78bSCy Schubert 			/* XXX Range check.  */
121*7f2fe78bSCy Schubert 			FD_SET(tickle, &readfds);
122*7f2fe78bSCy Schubert 			tv.tv_sec = 5;
123*7f2fe78bSCy Schubert 			tv.tv_usec = 0;
124*7f2fe78bSCy Schubert 			(void) select(tickle+1, &readfds, 0, 0, &tv);
125*7f2fe78bSCy Schubert 		    }
126*7f2fe78bSCy Schubert 		    close(tickle);
127*7f2fe78bSCy Schubert 		}
128*7f2fe78bSCy Schubert 	    }
129*7f2fe78bSCy Schubert 	}
130*7f2fe78bSCy Schubert #endif
131*7f2fe78bSCy Schubert 	if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt,
132*7f2fe78bSCy Schubert 	    tottimeout) != RPC_SUCCESS) {
133*7f2fe78bSCy Schubert 		clnt_perror(client, "Cannot register service");
134*7f2fe78bSCy Schubert 		return (FALSE);
135*7f2fe78bSCy Schubert 	}
136*7f2fe78bSCy Schubert 	CLNT_DESTROY(client);
137*7f2fe78bSCy Schubert 	(void)close(sock);
138*7f2fe78bSCy Schubert 	return (rslt);
139*7f2fe78bSCy Schubert }
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert /*
142*7f2fe78bSCy Schubert  * Remove the mapping between program,version and port.
143*7f2fe78bSCy Schubert  * Calls the pmap service remotely to do the un-mapping.
144*7f2fe78bSCy Schubert  */
145*7f2fe78bSCy Schubert bool_t
pmap_unset(rpcprog_t program,rpcvers_t version)146*7f2fe78bSCy Schubert pmap_unset(
147*7f2fe78bSCy Schubert 	rpcprog_t program,
148*7f2fe78bSCy Schubert 	rpcvers_t version)
149*7f2fe78bSCy Schubert {
150*7f2fe78bSCy Schubert 	struct sockaddr_in myaddress;
151*7f2fe78bSCy Schubert 	int sock = -1;
152*7f2fe78bSCy Schubert 	CLIENT *client;
153*7f2fe78bSCy Schubert 	struct pmap parms;
154*7f2fe78bSCy Schubert 	bool_t rslt;
155*7f2fe78bSCy Schubert 
156*7f2fe78bSCy Schubert 	get_myaddress(&myaddress);
157*7f2fe78bSCy Schubert 	client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
158*7f2fe78bSCy Schubert 	    timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
159*7f2fe78bSCy Schubert 	if (client == (CLIENT *)NULL)
160*7f2fe78bSCy Schubert 		return (FALSE);
161*7f2fe78bSCy Schubert 	parms.pm_prog = program;
162*7f2fe78bSCy Schubert 	parms.pm_vers = version;
163*7f2fe78bSCy Schubert 	parms.pm_port = parms.pm_prot = 0;
164*7f2fe78bSCy Schubert 	CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt,
165*7f2fe78bSCy Schubert 	    tottimeout);
166*7f2fe78bSCy Schubert 	CLNT_DESTROY(client);
167*7f2fe78bSCy Schubert 	(void)close(sock);
168*7f2fe78bSCy Schubert 	return (rslt);
169*7f2fe78bSCy Schubert }
170