#include <stdio.h>
#include <rpc/rpc.h>
#include <netconfig.h>
#include <netdir.h>
#include <sys/syslog.h>
#include <stdlib.h>
#include "rpcbind.h"
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <thread.h>
#include <synch.h>
#include <syslog.h>
struct fdlist {
int fd;
mutex_t fd_lock;
struct netconfig *nconf;
struct fdlist *next;
int check_binding;
};
static struct fdlist *fdhead;
static struct fdlist *fdtail;
static char *nullstring = "";
static bool_t
check_bound(struct fdlist *fdl, char *uaddr)
{
int fd;
struct netbuf *na;
struct t_bind taddr, *baddr;
int ans;
if (fdl->check_binding == FALSE)
return (TRUE);
na = uaddr2taddr(fdl->nconf, uaddr);
if (!na)
return (TRUE);
taddr.addr = *na;
taddr.qlen = 1;
(void) mutex_lock(&fdl->fd_lock);
fd = fdl->fd;
baddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
if (baddr == NULL) {
(void) mutex_unlock(&fdl->fd_lock);
netdir_free((char *)na, ND_ADDR);
return (TRUE);
}
if (t_bind(fd, &taddr, baddr) != 0) {
(void) mutex_unlock(&fdl->fd_lock);
netdir_free((char *)na, ND_ADDR);
(void) t_free((char *)baddr, T_BIND);
return (TRUE);
}
if (t_unbind(fd) != 0) {
(void) t_close(fd);
fdl->fd = t_open(fdl->nconf->nc_device, O_RDWR, NULL);
if (fdl->fd == -1)
fdl->check_binding = FALSE;
}
(void) mutex_unlock(&fdl->fd_lock);
ans = memcmp(taddr.addr.buf, baddr->addr.buf, baddr->addr.len);
netdir_free((char *)na, ND_ADDR);
(void) t_free((char *)baddr, T_BIND);
return (ans == 0 ? FALSE : TRUE);
}
int
add_bndlist(struct netconfig *nconf, struct t_bind *taddr, struct t_bind *baddr)
{
int fd;
struct fdlist *fdl;
struct netconfig *newnconf;
struct t_info tinfo;
struct t_bind tmpaddr;
newnconf = getnetconfigent(nconf->nc_netid);
if (newnconf == NULL)
return (-1);
fdl = (struct fdlist *)malloc((uint_t)sizeof (struct fdlist));
if (fdl == NULL) {
freenetconfigent(newnconf);
syslog(LOG_ERR, "no memory!");
return (-1);
}
(void) mutex_init(&fdl->fd_lock, USYNC_THREAD, NULL);
fdl->nconf = newnconf;
fdl->next = NULL;
if (fdhead == NULL) {
fdhead = fdl;
fdtail = fdl;
} else {
fdtail->next = fdl;
fdtail = fdl;
}
fdl->check_binding = FALSE;
if ((fdl->fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) < 0) {
if (debugging) {
fprintf(stderr,
"%s: add_bndlist cannot open connection: %s",
nconf->nc_netid, t_errlist[t_errno]);
}
return (-1);
}
switch (tinfo.servtype) {
case T_COTS:
case T_COTS_ORD:
taddr->qlen = 1;
break;
case T_CLTS:
taddr->qlen = 0;
break;
default:
goto error;
}
if (t_bind(fdl->fd, taddr, baddr) != 0) {
if (t_errno == TNOADDR) {
fdl->check_binding = TRUE;
return (0);
}
if (debugging) {
fprintf(stderr, "%s: add_bndlist cannot bind (1): %s",
nconf->nc_netid, t_errlist[t_errno]);
}
goto not_bound;
}
if (!memcmp(taddr->addr.buf, baddr->addr.buf,
(int)baddr->addr.len)) {
goto not_bound;
}
t_unbind(fdl->fd);
switch (tinfo.servtype) {
case T_COTS:
case T_COTS_ORD:
tmpaddr.qlen = 1;
break;
case T_CLTS:
tmpaddr.qlen = 0;
break;
default:
goto error;
}
tmpaddr.addr.len = tmpaddr.addr.maxlen = 0;
tmpaddr.addr.buf = NULL;
if (t_bind(fdl->fd, &tmpaddr, taddr) != 0) {
if (debugging) {
fprintf(stderr, "%s: add_bndlist cannot bind (2): %s",
nconf->nc_netid, t_errlist[t_errno]);
}
goto error;
}
if ((fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) < 0) {
if (debugging) {
fprintf(stderr,
"%s: add_bndlist cannot open connection: %s",
nconf->nc_netid, t_errlist[t_errno]);
}
goto error;
}
if (t_bind(fd, taddr, baddr) != 0) {
if (t_errno == TNOADDR) {
t_close(fd);
goto not_bound;
}
if (debugging) {
fprintf(stderr, "%s: add_bndlist cannot bind (3): %s",
nconf->nc_netid, t_errlist[t_errno]);
}
t_close(fd);
goto error;
}
t_close(fd);
if (!memcmp(taddr->addr.buf, baddr->addr.buf,
(int)baddr->addr.len)) {
switch (tinfo.servtype) {
case T_COTS:
case T_COTS_ORD:
if (baddr->qlen == 1) {
goto not_bound;
}
break;
case T_CLTS:
goto not_bound;
default:
goto error;
}
}
t_unbind(fdl->fd);
fdl->check_binding = TRUE;
return (0);
not_bound:
t_close(fdl->fd);
fdl->fd = -1;
return (1);
error:
t_close(fdl->fd);
fdl->fd = -1;
return (-1);
}
bool_t
is_bound(char *netid, char *uaddr)
{
struct fdlist *fdl;
for (fdl = fdhead; fdl; fdl = fdl->next)
if (strcmp(fdl->nconf->nc_netid, netid) == 0)
break;
if (fdl == NULL)
return (TRUE);
return (check_bound(fdl, uaddr));
}
#define UADDR_PRT_INDX(UADDR, PORT) { \
PORT = strrchr(UADDR, '.'); \
while (*--PORT != '.'); }
char *
mergeaddr(SVCXPRT *xprt, char *netid, char *uaddr, char *saddr)
{
struct fdlist *fdl;
struct nd_mergearg ma;
int stat;
for (fdl = fdhead; fdl; fdl = fdl->next)
if (strcmp(fdl->nconf->nc_netid, netid) == 0)
break;
if (fdl == NULL)
return (NULL);
if (check_bound(fdl, uaddr) == FALSE)
return (nullstring);
if (saddr != NULL) {
ma.c_uaddr = saddr;
} else {
ma.c_uaddr = taddr2uaddr(fdl->nconf, svc_getrpccaller(xprt));
if (ma.c_uaddr == NULL) {
syslog(LOG_ERR, "taddr2uaddr failed for %s: %s",
fdl->nconf->nc_netid, netdir_sperror());
return (NULL);
}
}
if ((strcmp(fdl->nconf->nc_protofmly, NC_INET) != 0) &&
(strcmp(fdl->nconf->nc_protofmly, NC_INET6) != 0)) {
ma.s_uaddr = uaddr;
stat = netdir_options(fdl->nconf, ND_MERGEADDR, 0, (char *)&ma);
}
else if ((ma.s_uaddr = taddr2uaddr(fdl->nconf,
&(xprt)->xp_ltaddr)) == NULL) {
ma.s_uaddr = uaddr;
stat = netdir_options(fdl->nconf, ND_MERGEADDR, 0, (char *)&ma);
} else {
char *s_uport, *uport;
UADDR_PRT_INDX(ma.s_uaddr, s_uport);
*s_uport = '\0';
UADDR_PRT_INDX(uaddr, uport);
ma.m_uaddr = malloc(strlen(ma.s_uaddr) + strlen(uport) + 1);
if (ma.m_uaddr == NULL) {
syslog(LOG_ERR, "mergeaddr: no memory!");
free(ma.s_uaddr);
if (saddr == NULL)
free(ma.c_uaddr);
return (NULL);
}
strcpy(ma.m_uaddr, ma.s_uaddr);
strcat(ma.m_uaddr, uport);
free(ma.s_uaddr);
stat = 0;
}
if (saddr == NULL) {
free(ma.c_uaddr);
}
if (stat) {
syslog(LOG_ERR, "netdir_merge failed for %s: %s",
fdl->nconf->nc_netid, netdir_sperror());
return (NULL);
}
return (ma.m_uaddr);
}
struct netconfig *
rpcbind_get_conf(char *netid)
{
struct fdlist *fdl;
for (fdl = fdhead; fdl; fdl = fdl->next)
if (strcmp(fdl->nconf->nc_netid, netid) == 0)
break;
if (fdl == NULL)
return (NULL);
return (fdl->nconf);
}