#include "namespace.h"
#include "reentrant.h"
#include <stdio.h>
#include <errno.h>
#include <netconfig.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <rpc/rpc.h>
#include <unistd.h>
#include "un-namespace.h"
#include "rpc_com.h"
#define NC_NONETCONFIG ENOENT
#define NC_NOMEM ENOMEM
#define NC_NOTINIT EINVAL
#define NC_BADFILE EBADF
#define NC_NOTFOUND ENOPROTOOPT
#define NC_TPI_CLTS_S "tpi_clts"
#define NC_TPI_COTS_S "tpi_cots"
#define NC_TPI_COTS_ORD_S "tpi_cots_ord"
#define NC_TPI_RAW_S "tpi_raw"
#define NC_NOFLAG_C '-'
#define NC_VISIBLE_C 'v'
#define NC_BROADCAST_C 'b'
#define NC_NOLOOKUP "-"
static const char * const _nc_errors[] = {
"Netconfig database not found",
"Not enough memory",
"Not initialized",
"Netconfig database has invalid format",
"Netid not found in netconfig database"
};
struct netconfig_info {
int eof;
int ref;
struct netconfig_list *head;
struct netconfig_list *tail;
};
struct netconfig_list {
char *linep;
struct netconfig *ncp;
struct netconfig_list *next;
};
struct netconfig_vars {
int valid;
int flag;
struct netconfig_list *nc_configs;
};
#define NC_VALID 0xfeed
#define NC_STORAGE 0xf00d
#define NC_INVALID 0
static int parse_ncp(char *, struct netconfig *);
static struct netconfig *dup_ncp(struct netconfig *);
static FILE *nc_file;
static mutex_t nc_file_lock = MUTEX_INITIALIZER;
static struct netconfig_info ni = { 0, 0, NULL, NULL};
static mutex_t ni_lock = MUTEX_INITIALIZER;
static _Thread_local int nc_error = 0;
#define MAXNETCONFIGLINE 1000
void *
setnetconfig(void)
{
struct netconfig_vars *nc_vars;
if ((nc_vars = (struct netconfig_vars *)malloc(sizeof
(struct netconfig_vars))) == NULL) {
return(NULL);
}
mutex_lock(&ni_lock);
ni.ref++;
mutex_unlock(&ni_lock);
mutex_lock(&nc_file_lock);
if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) {
nc_vars->valid = NC_VALID;
nc_vars->flag = 0;
nc_vars->nc_configs = ni.head;
mutex_unlock(&nc_file_lock);
return ((void *)nc_vars);
}
mutex_unlock(&nc_file_lock);
mutex_lock(&ni_lock);
ni.ref--;
mutex_unlock(&ni_lock);
nc_error = NC_NONETCONFIG;
free(nc_vars);
return (NULL);
}
struct netconfig *
getnetconfig(void *handlep)
{
struct netconfig_vars *ncp = (struct netconfig_vars *)handlep;
char *stringp;
struct netconfig_list *list;
struct netconfig *np;
struct netconfig *result;
mutex_lock(&nc_file_lock);
if (ncp == NULL || nc_file == NULL) {
nc_error = NC_NOTINIT;
mutex_unlock(&nc_file_lock);
return (NULL);
}
mutex_unlock(&nc_file_lock);
switch (ncp->valid) {
case NC_VALID:
if (ncp->flag == 0) {
ncp->flag = 1;
mutex_lock(&ni_lock);
ncp->nc_configs = ni.head;
mutex_unlock(&ni_lock);
if (ncp->nc_configs != NULL)
return(ncp->nc_configs->ncp);
}
else if (ncp->nc_configs != NULL && ncp->nc_configs->next != NULL) {
ncp->nc_configs = ncp->nc_configs->next;
return(ncp->nc_configs->ncp);
}
mutex_lock(&ni_lock);
if (ni.eof == 1) {
mutex_unlock(&ni_lock);
return(NULL);
}
mutex_unlock(&ni_lock);
break;
default:
nc_error = NC_NOTINIT;
return (NULL);
}
stringp = (char *) malloc(MAXNETCONFIGLINE);
if (stringp == NULL)
return (NULL);
#ifdef MEM_CHK
if (malloc_verify() == 0) {
fprintf(stderr, "memory heap corrupted in getnetconfig\n");
exit(1);
}
#endif
mutex_lock(&nc_file_lock);
do {
if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) {
free(stringp);
mutex_lock(&ni_lock);
ni.eof = 1;
mutex_unlock(&ni_lock);
mutex_unlock(&nc_file_lock);
return (NULL);
}
} while (*stringp == '#');
mutex_unlock(&nc_file_lock);
list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list));
if (list == NULL) {
free(stringp);
return(NULL);
}
np = (struct netconfig *) malloc(sizeof (struct netconfig));
if (np == NULL) {
free(stringp);
free(list);
return(NULL);
}
list->ncp = np;
list->next = NULL;
list->ncp->nc_lookups = NULL;
list->linep = stringp;
if (parse_ncp(stringp, list->ncp) == -1) {
free(stringp);
free(np);
free(list);
return (NULL);
}
else {
mutex_lock(&ni_lock);
if (ni.head == NULL) {
ni.head = ni.tail = list;
}
else {
ni.tail->next = list;
ni.tail = ni.tail->next;
}
ncp->nc_configs = ni.tail;
result = ni.tail->ncp;
mutex_unlock(&ni_lock);
return(result);
}
}
int
endnetconfig(void *handlep)
{
struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep;
struct netconfig_list *q, *p;
if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID &&
nc_handlep->valid != NC_STORAGE)) {
nc_error = NC_NOTINIT;
return (-1);
}
nc_handlep->valid = NC_INVALID;
nc_handlep->flag = 0;
nc_handlep->nc_configs = NULL;
mutex_lock(&ni_lock);
if (--ni.ref > 0) {
mutex_unlock(&ni_lock);
free(nc_handlep);
return(0);
}
q = ni.head;
ni.eof = ni.ref = 0;
ni.head = NULL;
ni.tail = NULL;
mutex_unlock(&ni_lock);
while (q != NULL) {
p = q->next;
free(q->ncp->nc_lookups);
free(q->ncp);
free(q->linep);
free(q);
q = p;
}
free(nc_handlep);
mutex_lock(&nc_file_lock);
fclose(nc_file);
nc_file = NULL;
mutex_unlock(&nc_file_lock);
return (0);
}
struct netconfig *
getnetconfigent(const char *netid)
{
FILE *file;
char *linep;
char *stringp;
struct netconfig *ncp = NULL;
struct netconfig_list *list;
nc_error = NC_NOTFOUND;
if (netid == NULL || strlen(netid) == 0) {
return (NULL);
}
mutex_lock(&ni_lock);
if (ni.head != NULL) {
for (list = ni.head; list; list = list->next) {
if (strcmp(list->ncp->nc_netid, netid) == 0) {
mutex_unlock(&ni_lock);
return(dup_ncp(list->ncp));
}
}
if (ni.eof == 1) {
mutex_unlock(&ni_lock);
return(NULL);
}
}
mutex_unlock(&ni_lock);
if ((file = fopen(NETCONFIG, "r")) == NULL) {
nc_error = NC_NONETCONFIG;
return (NULL);
}
if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) {
fclose(file);
nc_error = NC_NOMEM;
return (NULL);
}
do {
ptrdiff_t len;
char *tmpp;
do {
if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) == NULL) {
break;
}
} while (*stringp == '#');
if (stringp == NULL) {
break;
}
if ((tmpp = strpbrk(stringp, "\t ")) == NULL) {
nc_error = NC_BADFILE;
break;
}
if (strlen(netid) == (size_t) (len = tmpp - stringp) &&
strncmp(stringp, netid, (size_t)len) == 0) {
if ((ncp = (struct netconfig *)
malloc(sizeof (struct netconfig))) == NULL) {
break;
}
ncp->nc_lookups = NULL;
if (parse_ncp(linep, ncp) == -1) {
free(ncp);
ncp = NULL;
}
break;
}
} while (stringp != NULL);
if (ncp == NULL) {
free(linep);
}
fclose(file);
return(ncp);
}
void
freenetconfigent(struct netconfig *netconfigp)
{
if (netconfigp != NULL) {
free(netconfigp->nc_netid);
free(netconfigp->nc_lookups);
free(netconfigp);
}
return;
}
static int
parse_ncp(char *stringp, struct netconfig *ncp)
{
char *tokenp;
char *lasts;
char **nc_lookups;
nc_error = NC_BADFILE;
stringp[strlen(stringp)-1] = '\0';
if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) {
return (-1);
}
if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
return (-1);
}
if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0)
ncp->nc_semantics = NC_TPI_COTS_ORD;
else if (strcmp(tokenp, NC_TPI_COTS_S) == 0)
ncp->nc_semantics = NC_TPI_COTS;
else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0)
ncp->nc_semantics = NC_TPI_CLTS;
else if (strcmp(tokenp, NC_TPI_RAW_S) == 0)
ncp->nc_semantics = NC_TPI_RAW;
else
return (-1);
if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
return (-1);
}
for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0';
tokenp++) {
switch (*tokenp) {
case NC_NOFLAG_C:
break;
case NC_VISIBLE_C:
ncp->nc_flag |= NC_VISIBLE;
break;
case NC_BROADCAST_C:
ncp->nc_flag |= NC_BROADCAST;
break;
default:
return (-1);
}
}
if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) {
return (-1);
}
if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) {
return (-1);
}
if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) {
return (-1);
}
if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) {
return (-1);
}
if (strcmp(tokenp, NC_NOLOOKUP) == 0) {
ncp->nc_nlookups = 0;
ncp->nc_lookups = NULL;
} else {
char *cp;
free(ncp->nc_lookups);
ncp->nc_lookups = NULL;
ncp->nc_nlookups = 0;
while ((cp = tokenp) != NULL) {
if ((nc_lookups = reallocarray(ncp->nc_lookups,
ncp->nc_nlookups + 1, sizeof(*ncp->nc_lookups))) == NULL) {
free(ncp->nc_lookups);
ncp->nc_lookups = NULL;
return (-1);
}
tokenp = _get_next_token(cp, ',');
ncp->nc_lookups = nc_lookups;
ncp->nc_lookups[ncp->nc_nlookups++] = cp;
}
}
return (0);
}
char *
nc_sperror(void)
{
const char *message;
switch(nc_error) {
case NC_NONETCONFIG:
message = _nc_errors[0];
break;
case NC_NOMEM:
message = _nc_errors[1];
break;
case NC_NOTINIT:
message = _nc_errors[2];
break;
case NC_BADFILE:
message = _nc_errors[3];
break;
case NC_NOTFOUND:
message = _nc_errors[4];
break;
default:
message = "Unknown network selection error";
}
return ((char *)message);
}
void
nc_perror(const char *s)
{
fprintf(stderr, "%s: %s\n", s, nc_sperror());
}
static struct netconfig *
dup_ncp(struct netconfig *ncp)
{
struct netconfig *p;
char *tmp;
u_int i;
if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL)
return(NULL);
if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) {
free(tmp);
return(NULL);
}
*p = *ncp;
p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid);
tmp = strchr(tmp, '\0') + 1;
p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly);
tmp = strchr(tmp, '\0') + 1;
p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto);
tmp = strchr(tmp, '\0') + 1;
p->nc_device = (char *)strcpy(tmp,ncp->nc_device);
p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
if (p->nc_lookups == NULL) {
free(p->nc_netid);
free(p);
return(NULL);
}
for (i=0; i < p->nc_nlookups; i++) {
tmp = strchr(tmp, '\0') + 1;
p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]);
}
return(p);
}