#ifdef PORTMAP
#include "mt.h"
#include "rpc_mt.h"
#include <rpc/rpc.h>
#include <rpc/nettype.h>
#include <netdir.h>
#include <rpc/pmap_prot.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_rmt.h>
#include <string.h>
#include <syslog.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
int use_portmapper = 0;
static const struct timeval timeout = { 5, 0 };
static const struct timeval tottimeout = { 60, 0 };
static const struct timeval rmttimeout = { 3, 0 };
bool_t
pmap_set(rpcprog_t program, rpcvers_t version, rpcprot_t protocol,
ushort_t port)
{
bool_t rslt;
struct netbuf *na;
struct netconfig *nconf;
char buf[32];
if ((protocol != IPPROTO_UDP) && (protocol != IPPROTO_TCP))
return (FALSE);
nconf = __rpc_getconfip(protocol == IPPROTO_UDP ? "udp" : "tcp");
if (!nconf)
return (FALSE);
(void) sprintf(buf, "0.0.0.0.%d.%d", port >> 8 & 0xff, port & 0xff);
na = uaddr2taddr(nconf, buf);
if (!na) {
freenetconfigent(nconf);
return (FALSE);
}
rslt = rpcb_set(program, version, nconf, na);
netdir_free((char *)na, ND_ADDR);
freenetconfigent(nconf);
return (rslt);
}
bool_t
pmap_unset(rpcprog_t program, rpcvers_t version)
{
struct netconfig *nconf;
bool_t udp_rslt = FALSE;
bool_t tcp_rslt = FALSE;
nconf = __rpc_getconfip("udp");
if (nconf) {
udp_rslt = rpcb_unset(program, version, nconf);
freenetconfigent(nconf);
}
nconf = __rpc_getconfip("tcp");
if (nconf) {
tcp_rslt = rpcb_unset(program, version, nconf);
freenetconfigent(nconf);
}
return (tcp_rslt || udp_rslt);
}
ushort_t
pmap_getport(struct sockaddr_in *address, rpcprog_t program,
rpcvers_t version, rpcprot_t protocol)
{
ushort_t port = 0;
int fd = RPC_ANYFD;
CLIENT *client;
struct pmap parms;
address->sin_port = htons(PMAPPORT);
client = clntudp_bufcreate(address, PMAPPROG, PMAPVERS, timeout,
&fd, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
if (client != NULL) {
parms.pm_prog = program;
parms.pm_vers = version;
parms.pm_prot = protocol;
parms.pm_port = 0;
if (CLNT_CALL(client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
(caddr_t)&parms, (xdrproc_t)xdr_u_short,
(caddr_t)&port, tottimeout) != RPC_SUCCESS) {
rpc_createerr.cf_stat = RPC_PMAPFAILURE;
clnt_geterr(client, &rpc_createerr.cf_error);
} else if (port == 0) {
rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
}
CLNT_DESTROY(client);
}
address->sin_port = 0;
return (port);
}
struct pmaplist *
pmap_getmaps(struct sockaddr_in *address)
{
pmaplist_ptr head = NULL;
int fd = RPC_ANYFD;
struct timeval minutetimeout;
CLIENT *client;
minutetimeout.tv_sec = 60;
minutetimeout.tv_usec = 0;
address->sin_port = htons(PMAPPORT);
client = clnttcp_create(address, PMAPPROG, PMAPVERS, &fd, 50, 500);
if (client != NULL) {
if (CLNT_CALL(client, PMAPPROC_DUMP, (xdrproc_t)xdr_void,
NULL, (xdrproc_t)xdr_pmaplist_ptr,
(caddr_t)&head, minutetimeout) != RPC_SUCCESS) {
(void) syslog(LOG_ERR, "%s",
clnt_sperror(client, "pmap_getmaps rpc problem"));
}
CLNT_DESTROY(client);
}
address->sin_port = 0;
return ((struct pmaplist *)head);
}
enum clnt_stat
pmap_rmtcall(struct sockaddr_in *addr, rpcprog_t prog, rpcvers_t vers,
rpcproc_t proc, xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres,
caddr_t resp, struct timeval tout, rpcport_t *port_ptr)
{
int fd = RPC_ANYFD;
CLIENT *client;
struct p_rmtcallargs a;
struct p_rmtcallres r;
enum clnt_stat stat;
short tmp = addr->sin_port;
addr->sin_port = htons(PMAPPORT);
client = clntudp_create(addr, PMAPPROG, PMAPVERS, rmttimeout, &fd);
if (client != NULL) {
a.prog = prog;
a.vers = vers;
a.proc = proc;
a.args.args_val = argsp;
a.xdr_args = xdrargs;
r.res.res_val = resp;
r.xdr_res = xdrres;
stat = CLNT_CALL(client, PMAPPROC_CALLIT,
(xdrproc_t)xdr_rmtcallargs,
(caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
(caddr_t)&r, tout);
CLNT_DESTROY(client);
} else {
stat = RPC_FAILED;
}
addr->sin_port = tmp;
*port_ptr = r.port;
return (stat);
}
#endif