| 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 | |
| 29 | |
| 30 | /* |
| 31 | * pmap_clnt.c |
| 32 | * Client interface to pmap rpc service. |
| 33 | * |
| 34 | * Copyright (C) 1984, Sun Microsystems, Inc. |
| 35 | */ |
| 36 | |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include <rpc/rpc.h> |
| 42 | #include <rpc/pmap_prot.h> |
| 43 | #include <rpc/pmap_clnt.h> |
| 44 | #include <rpc/nettype.h> |
| 45 | #include <netinet/in.h> |
| 46 | |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | |
| 50 | #include "rpc_com.h" |
| 51 | |
| 52 | bool_t |
| 53 | pmap_set(u_long program, u_long version, int protocol, int port) |
| 54 | { |
| 55 | bool_t rslt; |
| 56 | struct netbuf *na; |
| 57 | struct netconfig *nconf; |
| 58 | char buf[32]; |
| 59 | |
| 60 | if ((protocol != IPPROTO_UDP) && (protocol != IPPROTO_TCP)) { |
| 61 | return (FALSE); |
| 62 | } |
| 63 | nconf = __rpc_getconfip(protocol == IPPROTO_UDP ? "udp" : "tcp"); |
| 64 | if (nconf == NULL) { |
| 65 | return (FALSE); |
| 66 | } |
| 67 | snprintf(buf, sizeof buf, "0.0.0.0.%d.%d", |
| 68 | (((u_int32_t)port) >> 8) & 0xff, port & 0xff); |
| 69 | na = uaddr2taddr(nconf, buf); |
| 70 | if (na == NULL) { |
| 71 | freenetconfigent(nconf); |
| 72 | return (FALSE); |
| 73 | } |
| 74 | rslt = rpcb_set((rpcprog_t)program, (rpcvers_t)version, nconf, na); |
| 75 | free(na); |
| 76 | freenetconfigent(nconf); |
| 77 | return (rslt); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Remove the mapping between program, version and port. |
| 82 | * Calls the pmap service remotely to do the un-mapping. |
| 83 | */ |
| 84 | bool_t |
| 85 | pmap_unset(u_long program, u_long version) |
| 86 | { |
| 87 | struct netconfig *nconf; |
| 88 | bool_t udp_rslt = FALSE; |
| 89 | bool_t tcp_rslt = FALSE; |
| 90 | |
| 91 | nconf = __rpc_getconfip("udp"); |
| 92 | if (nconf != NULL) { |
| 93 | udp_rslt = rpcb_unset((rpcprog_t)program, (rpcvers_t)version, |
| 94 | nconf); |
| 95 | freenetconfigent(nconf); |
| 96 | } |
| 97 | nconf = __rpc_getconfip("tcp"); |
| 98 | if (nconf != NULL) { |
| 99 | tcp_rslt = rpcb_unset((rpcprog_t)program, (rpcvers_t)version, |
| 100 | nconf); |
| 101 | freenetconfigent(nconf); |
| 102 | } |
| 103 | /* |
| 104 | * XXX: The call may still succeed even if only one of the |
| 105 | * calls succeeded. This was the best that could be |
| 106 | * done for backward compatibility. |
| 107 | */ |
| 108 | return (tcp_rslt || udp_rslt); |
| 109 | } |