#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;
u_int32_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 {
u_int32_t rp_xid;
int32_t rp_direction;
u_int32_t rp_rpcvers;
u_int32_t rp_prog;
u_int32_t rp_vers;
u_int32_t rp_proc;
};
struct rpc_reply {
u_int32_t rp_xid;
int32_t rp_direction;
int32_t rp_astatus;
union {
u_int32_t rpu_errno;
struct {
struct auth_info rok_auth;
u_int32_t rok_status;
} rpu_rok;
} rp_u;
};
static ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
static int rpc_getport(struct iodesc *, n_long, n_long);
int rpc_xid;
static int rpc_port = 0x400;
int
rpc_newport(void)
{
if (rpc_port <= 256)
rpc_port = 0x400;
return (--rpc_port);
}
ssize_t
rpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc, void *sdata,
size_t slen, void *rdata, size_t rlen)
{
ssize_t cc;
struct auth_info *auth;
struct rpc_call *call;
struct rpc_reply *reply;
char *send_head, *send_tail;
char *recv_head, *recv_tail;
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;
#if 1
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));
#else
send_head -= sizeof(*auth);
auth = send_head;
auth->authtype = htonl(RPCAUTH_NULL);
auth->authlen = 0;
#endif
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);
recv_head = rdata;
recv_tail = (char *)rdata + rlen;
recv_head -= sizeof(*reply);
cc = sendrecv(d,
sendudp, send_head, send_tail - send_head,
recvrpc, recv_head, recv_tail - recv_head);
#ifdef RPC_DEBUG
if (debug)
printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
#endif
if (cc == -1)
return (-1);
if (cc <= sizeof(*reply)) {
errno = EBADRPC;
return (-1);
}
recv_tail = recv_head + cc;
reply = (struct rpc_reply *)recv_head;
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;
return(-1);
}
x = ntohl(reply->rp_u.rpu_rok.rok_status);
if (x != 0) {
printf("callrpc: error = %ld\n", (long)x);
errno = EBADRPC;
return(-1);
}
recv_head += sizeof(*reply);
return (ssize_t)(recv_tail - recv_head);
}
static ssize_t
recvrpc(struct iodesc *d, void *pkt, size_t len, time_t tleft)
{
struct rpc_reply *reply;
ssize_t n;
int x;
errno = 0;
#ifdef RPC_DEBUG
if (debug)
printf("recvrpc: called len=%lu\n", (u_long)len);
#endif
n = readudp(d, pkt, len, tleft);
if (n <= (4 * 4))
return -1;
reply = (struct rpc_reply *)pkt;
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
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
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);
return -1;
}
return (n);
}
void
rpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
{
struct hackhdr {
n_long ip_src;
n_long ip_dst;
u_int16_t uh_sport;
u_int16_t uh_dport;
int16_t uh_ulen;
u_int16_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;
struct {
n_long h[RPC_HEADER_WORDS];
struct res d;
n_long pad;
} rdata;
ssize_t cc;
int port;
#ifdef RPC_DEBUG
if (debug)
printf("getport: prog=0x%x vers=%d\n", prog, vers);
#endif
if (prog == PMAPPROG)
return (PMAPPORT);
port = rpc_pmap_getcache(d->destip, prog, vers);
if (port != -1)
return (port);
args = &sdata.d;
args->prog = htonl(prog);
args->vers = htonl(vers);
args->proto = htonl(IPPROTO_UDP);
args->port = 0;
res = &rdata.d;
cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
args, sizeof(*args), res, sizeof(*res));
if (cc < sizeof(*res)) {
printf("getport: %s", strerror(errno));
errno = EBADRPC;
return (-1);
}
port = (int)ntohl(res->port);
rpc_pmap_putcache(d->destip, prog, vers, port);
return (port);
}