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