2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A#ifdef PORTMAP
2N/A
2N/A/*
2N/A * interface to pmap rpc service.
2N/A */
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <rpc/rpc.h>
2N/A#include <rpc/nettype.h>
2N/A#include <netdir.h>
2N/A#include <rpc/pmap_prot.h>
2N/A#include <rpc/pmap_clnt.h>
2N/A#include <rpc/pmap_rmt.h>
2N/A#include <string.h>
2N/A#include <syslog.h>
2N/A#include <netinet/in.h>
2N/A#include <sys/socket.h>
2N/A#include <unistd.h>
2N/A
2N/Aint use_portmapper = 0;
2N/Astatic const struct timeval timeout = { 5, 0 };
2N/Astatic const struct timeval tottimeout = { 60, 0 };
2N/Astatic const struct timeval rmttimeout = { 3, 0 };
2N/A
2N/A/*
2N/A * Set a mapping between program, version and port.
2N/A * Calls the pmap service remotely to do the mapping.
2N/A */
2N/Abool_t
2N/Apmap_set(rpcprog_t program, rpcvers_t version, rpcprot_t protocol,
2N/A ushort_t port)
2N/A{
2N/A bool_t rslt;
2N/A struct netbuf *na;
2N/A struct netconfig *nconf;
2N/A char buf[32];
2N/A
2N/A if ((protocol != IPPROTO_UDP) && (protocol != IPPROTO_TCP))
2N/A return (FALSE);
2N/A nconf = __rpc_getconfip(protocol == IPPROTO_UDP ? "udp" : "tcp");
2N/A if (!nconf)
2N/A return (FALSE);
2N/A (void) sprintf(buf, "0.0.0.0.%d.%d", port >> 8 & 0xff, port & 0xff);
2N/A na = uaddr2taddr(nconf, buf);
2N/A if (!na) {
2N/A freenetconfigent(nconf);
2N/A return (FALSE);
2N/A }
2N/A rslt = rpcb_set(program, version, nconf, na);
2N/A
2N/A netdir_free((char *)na, ND_ADDR);
2N/A freenetconfigent(nconf);
2N/A return (rslt);
2N/A}
2N/A
2N/A/*
2N/A * Remove the mapping between program, version and port.
2N/A * Calls the pmap service remotely to do the un-mapping.
2N/A */
2N/Abool_t
2N/Apmap_unset(rpcprog_t program, rpcvers_t version)
2N/A{
2N/A struct netconfig *nconf;
2N/A bool_t udp_rslt = FALSE;
2N/A bool_t tcp_rslt = FALSE;
2N/A
2N/A nconf = __rpc_getconfip("udp");
2N/A if (nconf) {
2N/A udp_rslt = rpcb_unset(program, version, nconf);
2N/A freenetconfigent(nconf);
2N/A }
2N/A nconf = __rpc_getconfip("tcp");
2N/A if (nconf) {
2N/A tcp_rslt = rpcb_unset(program, version, nconf);
2N/A freenetconfigent(nconf);
2N/A }
2N/A /*
2N/A * XXX: The call may still succeed even if only one of the
2N/A * calls succeeded. This was the best that could be
2N/A * done for backward compatibility.
2N/A */
2N/A return (tcp_rslt || udp_rslt);
2N/A}
2N/A
2N/A/*
2N/A * Find the mapped port for program, version.
2N/A * Calls the pmap service remotely to do the lookup.
2N/A * Returns 0 if no map exists.
2N/A *
2N/A * XXX: It talks only to the portmapper and not to the rpcbind
2N/A * service. There may be implementations out there which do not
2N/A * run portmapper as a part of rpcbind.
2N/A */
2N/Aushort_t
2N/Apmap_getport(struct sockaddr_in *address, rpcprog_t program,
2N/A rpcvers_t version, rpcprot_t protocol)
2N/A{
2N/A ushort_t port = 0;
2N/A int fd = RPC_ANYFD;
2N/A CLIENT *client;
2N/A struct pmap parms;
2N/A
2N/A address->sin_port = htons(PMAPPORT);
2N/A client = clntudp_bufcreate(address, PMAPPROG, PMAPVERS, timeout,
2N/A &fd, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
2N/A if (client != NULL) {
2N/A parms.pm_prog = program;
2N/A parms.pm_vers = version;
2N/A parms.pm_prot = protocol;
2N/A parms.pm_port = 0; /* not needed or used */
2N/A if (CLNT_CALL(client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
2N/A (caddr_t)&parms, (xdrproc_t)xdr_u_short,
2N/A (caddr_t)&port, tottimeout) != RPC_SUCCESS) {
2N/A rpc_createerr.cf_stat = RPC_PMAPFAILURE;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A } else if (port == 0) {
2N/A rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
2N/A }
2N/A CLNT_DESTROY(client);
2N/A }
2N/A address->sin_port = 0;
2N/A return (port);
2N/A}
2N/A
2N/A/*
2N/A * Get a copy of the current port maps.
2N/A * Calls the pmap service remotely to do get the maps.
2N/A */
2N/Astruct pmaplist *
2N/Apmap_getmaps(struct sockaddr_in *address)
2N/A{
2N/A pmaplist_ptr head = NULL;
2N/A int fd = RPC_ANYFD;
2N/A struct timeval minutetimeout;
2N/A CLIENT *client;
2N/A
2N/A minutetimeout.tv_sec = 60;
2N/A minutetimeout.tv_usec = 0;
2N/A address->sin_port = htons(PMAPPORT);
2N/A client = clnttcp_create(address, PMAPPROG, PMAPVERS, &fd, 50, 500);
2N/A if (client != NULL) {
2N/A if (CLNT_CALL(client, PMAPPROC_DUMP, (xdrproc_t)xdr_void,
2N/A NULL, (xdrproc_t)xdr_pmaplist_ptr,
2N/A (caddr_t)&head, minutetimeout) != RPC_SUCCESS) {
2N/A (void) syslog(LOG_ERR, "%s",
2N/A clnt_sperror(client, "pmap_getmaps rpc problem"));
2N/A }
2N/A CLNT_DESTROY(client);
2N/A }
2N/A address->sin_port = 0;
2N/A return ((struct pmaplist *)head);
2N/A}
2N/A
2N/A/*
2N/A * pmapper remote-call-service interface.
2N/A * This routine is used to call the pmapper remote call service
2N/A * which will look up a service program in the port maps, and then
2N/A * remotely call that routine with the given parameters. This allows
2N/A * programs to do a lookup and call in one step.
2N/A */
2N/Aenum clnt_stat
2N/Apmap_rmtcall(struct sockaddr_in *addr, rpcprog_t prog, rpcvers_t vers,
2N/A rpcproc_t proc, xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres,
2N/A caddr_t resp, struct timeval tout, rpcport_t *port_ptr)
2N/A{
2N/A int fd = RPC_ANYFD;
2N/A CLIENT *client;
2N/A struct p_rmtcallargs a;
2N/A struct p_rmtcallres r;
2N/A enum clnt_stat stat;
2N/A short tmp = addr->sin_port;
2N/A
2N/A addr->sin_port = htons(PMAPPORT);
2N/A client = clntudp_create(addr, PMAPPROG, PMAPVERS, rmttimeout, &fd);
2N/A if (client != NULL) {
2N/A a.prog = prog;
2N/A a.vers = vers;
2N/A a.proc = proc;
2N/A a.args.args_val = argsp;
2N/A a.xdr_args = xdrargs;
2N/A r.res.res_val = resp;
2N/A r.xdr_res = xdrres;
2N/A stat = CLNT_CALL(client, PMAPPROC_CALLIT,
2N/A (xdrproc_t)xdr_rmtcallargs,
2N/A (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
2N/A (caddr_t)&r, tout);
2N/A CLNT_DESTROY(client);
2N/A } else {
2N/A stat = RPC_FAILED;
2N/A }
2N/A addr->sin_port = tmp;
2N/A *port_ptr = r.port;
2N/A return (stat);
2N/A}
2N/A
2N/A#endif /* PORTMAP */