18360efbdSAlfred Perlstein /* $NetBSD: pmap_clnt.c,v 1.16 2000/07/06 03:10:34 christos Exp $ */
28360efbdSAlfred Perlstein
32e322d37SHiroki Sato /*-
4*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato * All rights reserved.
899064799SGarrett Wollman *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
1999064799SGarrett Wollman *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman */
3299064799SGarrett Wollman
3399064799SGarrett Wollman /*
3499064799SGarrett Wollman * pmap_clnt.c
3599064799SGarrett Wollman * Client interface to pmap rpc service.
3699064799SGarrett Wollman *
3799064799SGarrett Wollman * Copyright (C) 1984, Sun Microsystems, Inc.
3899064799SGarrett Wollman */
3999064799SGarrett Wollman
40d201fe46SDaniel Eischen #include "namespace.h"
41ad133ed6SBill Paul #include <sys/types.h>
42ad133ed6SBill Paul #include <sys/stat.h>
434c3af266SPoul-Henning Kamp #include <unistd.h>
448360efbdSAlfred Perlstein
4599064799SGarrett Wollman #include <rpc/rpc.h>
4699064799SGarrett Wollman #include <rpc/pmap_prot.h>
4799064799SGarrett Wollman #include <rpc/pmap_clnt.h>
488360efbdSAlfred Perlstein #include <rpc/nettype.h>
49ec53c6faSPeter Wemm #include <netinet/in.h>
50d201fe46SDaniel Eischen #include "un-namespace.h"
51c124f3bdSJames Raynard
528360efbdSAlfred Perlstein #include <stdio.h>
538360efbdSAlfred Perlstein #include <stdlib.h>
5499064799SGarrett Wollman
558360efbdSAlfred Perlstein #include "rpc_com.h"
5699064799SGarrett Wollman
5799064799SGarrett Wollman bool_t
pmap_set(u_long program,u_long version,int protocol,int port)588360efbdSAlfred Perlstein pmap_set(u_long program, u_long version, int protocol, int port)
5999064799SGarrett Wollman {
6099064799SGarrett Wollman bool_t rslt;
618360efbdSAlfred Perlstein struct netbuf *na;
628360efbdSAlfred Perlstein struct netconfig *nconf;
638360efbdSAlfred Perlstein char buf[32];
6499064799SGarrett Wollman
658360efbdSAlfred Perlstein if ((protocol != IPPROTO_UDP) && (protocol != IPPROTO_TCP)) {
6699064799SGarrett Wollman return (FALSE);
6799064799SGarrett Wollman }
688360efbdSAlfred Perlstein nconf = __rpc_getconfip(protocol == IPPROTO_UDP ? "udp" : "tcp");
698360efbdSAlfred Perlstein if (nconf == NULL) {
708360efbdSAlfred Perlstein return (FALSE);
718360efbdSAlfred Perlstein }
728360efbdSAlfred Perlstein snprintf(buf, sizeof buf, "0.0.0.0.%d.%d",
738360efbdSAlfred Perlstein (((u_int32_t)port) >> 8) & 0xff, port & 0xff);
748360efbdSAlfred Perlstein na = uaddr2taddr(nconf, buf);
758360efbdSAlfred Perlstein if (na == NULL) {
768360efbdSAlfred Perlstein freenetconfigent(nconf);
778360efbdSAlfred Perlstein return (FALSE);
788360efbdSAlfred Perlstein }
798360efbdSAlfred Perlstein rslt = rpcb_set((rpcprog_t)program, (rpcvers_t)version, nconf, na);
808360efbdSAlfred Perlstein free(na);
818360efbdSAlfred Perlstein freenetconfigent(nconf);
8299064799SGarrett Wollman return (rslt);
8399064799SGarrett Wollman }
8499064799SGarrett Wollman
8599064799SGarrett Wollman /*
8699064799SGarrett Wollman * Remove the mapping between program, version and port.
8799064799SGarrett Wollman * Calls the pmap service remotely to do the un-mapping.
8899064799SGarrett Wollman */
8999064799SGarrett Wollman bool_t
pmap_unset(u_long program,u_long version)908360efbdSAlfred Perlstein pmap_unset(u_long program, u_long version)
9199064799SGarrett Wollman {
928360efbdSAlfred Perlstein struct netconfig *nconf;
938360efbdSAlfred Perlstein bool_t udp_rslt = FALSE;
948360efbdSAlfred Perlstein bool_t tcp_rslt = FALSE;
9599064799SGarrett Wollman
968360efbdSAlfred Perlstein nconf = __rpc_getconfip("udp");
978360efbdSAlfred Perlstein if (nconf != NULL) {
988360efbdSAlfred Perlstein udp_rslt = rpcb_unset((rpcprog_t)program, (rpcvers_t)version,
998360efbdSAlfred Perlstein nconf);
1008360efbdSAlfred Perlstein freenetconfigent(nconf);
101ad133ed6SBill Paul }
1028360efbdSAlfred Perlstein nconf = __rpc_getconfip("tcp");
1038360efbdSAlfred Perlstein if (nconf != NULL) {
1048360efbdSAlfred Perlstein tcp_rslt = rpcb_unset((rpcprog_t)program, (rpcvers_t)version,
1058360efbdSAlfred Perlstein nconf);
1068360efbdSAlfred Perlstein freenetconfigent(nconf);
1078360efbdSAlfred Perlstein }
1088360efbdSAlfred Perlstein /*
1098360efbdSAlfred Perlstein * XXX: The call may still succeed even if only one of the
1108360efbdSAlfred Perlstein * calls succeeded. This was the best that could be
1118360efbdSAlfred Perlstein * done for backward compatibility.
1128360efbdSAlfred Perlstein */
1138360efbdSAlfred Perlstein return (tcp_rslt || udp_rslt);
11499064799SGarrett Wollman }
115