xref: /freebsd/lib/libc/rpc/pmap_clnt.c (revision e0c4386e)
1 /*	$NetBSD: pmap_clnt.c,v 1.16 2000/07/06 03:10:34 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * pmap_clnt.c
35  * Client interface to pmap rpc service.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  */
39 
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 
45 #include <rpc/rpc.h>
46 #include <rpc/pmap_prot.h>
47 #include <rpc/pmap_clnt.h>
48 #include <rpc/nettype.h>
49 #include <netinet/in.h>
50 #include "un-namespace.h"
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #include "rpc_com.h"
56 
57 bool_t
58 pmap_set(u_long program, u_long version, int protocol, int port)
59 {
60 	bool_t rslt;
61 	struct netbuf *na;
62 	struct netconfig *nconf;
63 	char buf[32];
64 
65 	if ((protocol != IPPROTO_UDP) && (protocol != IPPROTO_TCP)) {
66 		return (FALSE);
67 	}
68 	nconf = __rpc_getconfip(protocol == IPPROTO_UDP ? "udp" : "tcp");
69 	if (nconf == NULL) {
70 		return (FALSE);
71 	}
72 	snprintf(buf, sizeof buf, "0.0.0.0.%d.%d",
73 	    (((u_int32_t)port) >> 8) & 0xff, port & 0xff);
74 	na = uaddr2taddr(nconf, buf);
75 	if (na == NULL) {
76 		freenetconfigent(nconf);
77 		return (FALSE);
78 	}
79 	rslt = rpcb_set((rpcprog_t)program, (rpcvers_t)version, nconf, na);
80 	free(na);
81 	freenetconfigent(nconf);
82 	return (rslt);
83 }
84 
85 /*
86  * Remove the mapping between program, version and port.
87  * Calls the pmap service remotely to do the un-mapping.
88  */
89 bool_t
90 pmap_unset(u_long program, u_long version)
91 {
92 	struct netconfig *nconf;
93 	bool_t udp_rslt = FALSE;
94 	bool_t tcp_rslt = FALSE;
95 
96 	nconf = __rpc_getconfip("udp");
97 	if (nconf != NULL) {
98 		udp_rslt = rpcb_unset((rpcprog_t)program, (rpcvers_t)version,
99 		    nconf);
100 		freenetconfigent(nconf);
101 	}
102 	nconf = __rpc_getconfip("tcp");
103 	if (nconf != NULL) {
104 		tcp_rslt = rpcb_unset((rpcprog_t)program, (rpcvers_t)version,
105 		    nconf);
106 		freenetconfigent(nconf);
107 	}
108 	/*
109 	 * XXX: The call may still succeed even if only one of the
110 	 * calls succeeded.  This was the best that could be
111 	 * done for backward compatibility.
112 	 */
113 	return (tcp_rslt || udp_rslt);
114 }
115