#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <string.h>
#include "rpcv2.h"
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "rpc.h"
struct auth_info {
int32_t authtype;
uint32_t authlen;
};
struct auth_unix {
int32_t ua_time;
int32_t ua_hostname;
int32_t ua_uid;
int32_t ua_gid;
int32_t ua_gidlist;
};
struct rpc_call {
uint32_t rp_xid;
int32_t rp_direction;
uint32_t rp_rpcvers;
uint32_t rp_prog;
uint32_t rp_vers;
uint32_t rp_proc;
};
struct rpc_reply {
uint32_t rp_xid;
int32_t rp_direction;
int32_t rp_astatus;
union {
uint32_t rpu_errno;
struct {
struct auth_info rok_auth;
uint32_t rok_status;
} rpu_rok;
} rp_u;
};
static ssize_t recvrpc(struct iodesc *, void **, void **, time_t, void *);
static int rpc_getport(struct iodesc *, n_long, n_long);
int rpc_xid;
int rpc_port = 0x400;
ssize_t
rpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc,
void *sdata, size_t slen, void **rdata, void **pkt)
{
ssize_t cc, rsize;
struct auth_info *auth;
struct rpc_call *call;
struct rpc_reply *reply;
char *send_head, *send_tail;
void *ptr;
n_long x;
int port;
#ifdef RPC_DEBUG
if (debug)
printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
prog, vers, proc);
#endif
port = rpc_getport(d, prog, vers);
if (port == -1)
return (-1);
d->destport = htons(port);
send_head = sdata;
send_tail = (char *)sdata + slen;
send_head -= sizeof(*auth);
auth = (struct auth_info *)send_head;
auth->authtype = htonl(RPCAUTH_NULL);
auth->authlen = 0;
send_head -= sizeof(struct auth_unix);
bzero(send_head, sizeof(struct auth_unix));
send_head -= sizeof(*auth);
auth = (struct auth_info *)send_head;
auth->authtype = htonl(RPCAUTH_UNIX);
auth->authlen = htonl(sizeof(struct auth_unix));
send_head -= sizeof(*call);
call = (struct rpc_call *)send_head;
rpc_xid++;
call->rp_xid = htonl(rpc_xid);
call->rp_direction = htonl(RPC_CALL);
call->rp_rpcvers = htonl(RPC_VER2);
call->rp_prog = htonl(prog);
call->rp_vers = htonl(vers);
call->rp_proc = htonl(proc);
ptr = NULL;
cc = sendrecv(d,
sendudp, send_head, send_tail - send_head,
recvrpc, &ptr, (void **)&reply, NULL);
#ifdef RPC_DEBUG
if (debug)
printf("callrpc: cc=%zd\n", cc);
#endif
if (cc == -1)
return (-1);
if (cc <= sizeof(*reply)) {
errno = EBADRPC;
free(ptr);
return (-1);
}
auth = &reply->rp_u.rpu_rok.rok_auth;
x = ntohl(auth->authlen);
if (x != 0) {
#ifdef RPC_DEBUG
if (debug)
printf("callrpc: reply auth != NULL\n");
#endif
errno = EBADRPC;
free(ptr);
return (-1);
}
x = ntohl(reply->rp_u.rpu_rok.rok_status);
if (x != 0) {
printf("callrpc: error = %ld\n", (long)x);
errno = EBADRPC;
free(ptr);
return (-1);
}
rsize = cc - sizeof(*reply);
*rdata = (void *)((uintptr_t)reply + sizeof(*reply));
*pkt = ptr;
return (rsize);
}
static ssize_t
recvrpc(struct iodesc *d, void **pkt, void **payload, time_t tleft, void *extra)
{
void *ptr;
struct rpc_reply *reply;
ssize_t n;
int x;
errno = 0;
#ifdef RPC_DEBUG
if (debug)
printf("recvrpc: called\n");
#endif
ptr = NULL;
n = readudp(d, &ptr, (void **)&reply, tleft);
if (n <= (4 * 4)) {
free(ptr);
return (-1);
}
x = ntohl(reply->rp_xid);
if (x != rpc_xid) {
#ifdef RPC_DEBUG
if (debug)
printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
#endif
free(ptr);
return (-1);
}
x = ntohl(reply->rp_direction);
if (x != RPC_REPLY) {
#ifdef RPC_DEBUG
if (debug)
printf("recvrpc: rp_direction %d != REPLY\n", x);
#endif
free(ptr);
return (-1);
}
x = ntohl(reply->rp_astatus);
if (x != RPC_MSGACCEPTED) {
errno = ntohl(reply->rp_u.rpu_errno);
printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
free(ptr);
return (-1);
}
*pkt = ptr;
*payload = reply;
return (n);
}
void
rpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
{
struct hackhdr {
n_long ip_src;
n_long ip_dst;
uint16_t uh_sport;
uint16_t uh_dport;
int16_t uh_ulen;
uint16_t uh_sum;
struct rpc_reply rpc;
} *hhdr;
hhdr = ((struct hackhdr *)pkt) - 1;
addr->s_addr = hhdr->ip_src;
*port = hhdr->uh_sport;
}
#define PMAP_NUM 8
int rpc_pmap_num;
struct pmap_list {
struct in_addr addr;
u_int prog;
u_int vers;
int port;
} rpc_pmap_list[PMAP_NUM];
int
rpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
{
struct pmap_list *pl;
for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
if (pl->addr.s_addr == addr.s_addr &&
pl->prog == prog && pl->vers == vers )
{
return (pl->port);
}
}
return (-1);
}
void
rpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
{
struct pmap_list *pl;
if (rpc_pmap_num >= PMAP_NUM) {
rpc_pmap_num = PMAP_NUM - 1;
#ifdef RPC_DEBUG
printf("rpc_pmap_putcache: cache overflow\n");
#endif
}
pl = &rpc_pmap_list[rpc_pmap_num];
rpc_pmap_num++;
pl->addr = addr;
pl->prog = prog;
pl->vers = vers;
pl->port = port;
}
int
rpc_getport(struct iodesc *d, n_long prog, n_long vers)
{
struct args {
n_long prog;
n_long vers;
n_long proto;
n_long port;
} *args;
struct res {
n_long port;
} *res;
struct {
n_long h[RPC_HEADER_WORDS];
struct args d;
} sdata;
void *pkt;
ssize_t cc;
int port;
#ifdef RPC_DEBUG
if (debug)
printf("%s: prog=0x%x vers=%d\n", __func__, prog, vers);
#endif
if (prog == PMAPPROG) {
port = PMAPPORT;
goto out;
}
port = rpc_pmap_getcache(d->destip, prog, vers);
if (port != -1)
goto out;
args = &sdata.d;
args->prog = htonl(prog);
args->vers = htonl(vers);
args->proto = htonl(IPPROTO_UDP);
args->port = 0;
pkt = NULL;
cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
args, sizeof(*args), (void **)&res, &pkt);
if (cc < sizeof(*res)) {
printf("getport: %s", strerror(errno));
errno = EBADRPC;
free(pkt);
return (-1);
}
port = (int)ntohl(res->port);
free(pkt);
rpc_pmap_putcache(d->destip, prog, vers, port);
out:
#ifdef RPC_DEBUG
if (debug)
printf("%s: port=%u\n", __func__, port);
#endif
return (port);
}