#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <sys/param.h>
#include <rpc/rpc.h>
#include <sys/stat.h>
#include <netconfig.h>
#include <netdir.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/resource.h>
#include <rpcsvc/mount.h>
#include <sys/pathconf.h>
#include <sys/systeminfo.h>
#include <sys/utsname.h>
#include <signal.h>
#include <locale.h>
#include <unistd.h>
#include <thread.h>
#include <sharefs/share.h>
#include "../lib/sharetab.h"
#include "mountd.h"
struct cache_entry {
char *cache_host;
time_t cache_time;
int cache_belong;
char **cache_grl;
int cache_grc;
struct cache_entry *cache_next;
};
static struct cache_entry *cache_head;
#define VALID_TIME 60
static rwlock_t cache_lock;
static void cache_free(struct cache_entry *entry);
static int cache_check(char *host, char **grl, int grc, int *belong);
static void cache_enter(char *host, char **grl, int grc, int belong);
void
netgroup_init()
{
(void) rwlock_init(&cache_lock, USYNC_THREAD, NULL);
}
int
netgroup_check(struct nd_hostservlist *clnames, char *glist, int grc)
{
char **grl;
char *gr;
int nhosts = clnames->h_cnt;
char *host0, *host;
int i, j, n;
int response;
int belong = 0;
static char *domain;
if (domain == NULL) {
int ssize;
domain = exmalloc(SYS_NMLN);
ssize = sysinfo(SI_SRPC_DOMAIN, domain, SYS_NMLN);
if (ssize > SYS_NMLN) {
free(domain);
domain = exmalloc(ssize);
ssize = sysinfo(SI_SRPC_DOMAIN, domain, ssize);
}
if (ssize <= 1) {
syslog(LOG_ERR, "No default domain set");
return (0);
}
}
grl = calloc(grc, sizeof (char *));
if (grl == NULL)
return (0);
for (i = 0, gr = glist; i < grc && !belong; ) {
response = (*gr != '-') ? 1 : 0;
for (n = 0; i < grc; i++, n++, gr += strlen(gr) + 1) {
if ((response && *gr == '-') ||
(!response && *gr != '-'))
break;
grl[n] = response ? gr : gr + 1;
}
host0 = clnames->h_hostservs[0].h_host;
if (!cache_check(host0, grl, n, &belong)) {
for (j = 0; j < nhosts && !belong; j++) {
host = clnames->h_hostservs[j].h_host;
if (__multi_innetgr(n, grl,
1, &host,
0, NULL,
1, &domain))
belong = 1;
}
cache_enter(host0, grl, n, belong);
}
}
free(grl);
return (belong ? response : 0);
}
static void
cache_free(struct cache_entry *entry)
{
struct cache_entry *ce, *next;
int i;
for (ce = entry; ce; ce = next) {
if (ce->cache_host)
free(ce->cache_host);
for (i = 0; i < ce->cache_grc; i++)
if (ce->cache_grl[i])
free(ce->cache_grl[i]);
if (ce->cache_grl)
free(ce->cache_grl);
next = ce->cache_next;
free(ce);
}
}
static int
cache_check(char *host, char **grl, int grc, int *belong)
{
struct cache_entry *ce, *prev;
time_t timenow = time(NULL);
int i;
(void) rw_rdlock(&cache_lock);
for (ce = cache_head; ce; ce = ce->cache_next) {
if (timenow > ce->cache_time) {
(void) rw_unlock(&cache_lock);
(void) rw_wrlock(&cache_lock);
for (prev = NULL, ce = cache_head; ce;
prev = ce, ce = ce->cache_next)
if (timenow > ce->cache_time)
break;
if (ce != NULL) {
if (prev)
prev->cache_next = NULL;
else
cache_head = NULL;
cache_free(ce);
}
(void) rw_unlock(&cache_lock);
return (0);
}
if (ce->cache_grc != grc)
continue;
if (strcasecmp(host, ce->cache_host) != 0)
continue;
for (i = 0; i < grc; i++)
if (strcasecmp(ce->cache_grl[i], grl[i]) != 0)
break;
if (i < grc)
continue;
*belong = ce->cache_belong;
(void) rw_unlock(&cache_lock);
return (1);
}
(void) rw_unlock(&cache_lock);
return (0);
}
static void
cache_enter(char *host, char **grl, int grc, int belong)
{
struct cache_entry *entry;
int i;
entry = malloc(sizeof (*entry));
if (entry == NULL)
return;
(void) memset((caddr_t)entry, 0, sizeof (*entry));
entry->cache_host = strdup(host);
if (entry->cache_host == NULL) {
cache_free(entry);
return;
}
entry->cache_time = time(NULL) + VALID_TIME;
entry->cache_belong = belong;
entry->cache_grl = malloc(grc * sizeof (char *));
if (entry->cache_grl == NULL) {
cache_free(entry);
return;
}
for (i = 0; i < grc; i++) {
entry->cache_grl[i] = strdup(grl[i]);
if (entry->cache_grl[i] == NULL) {
entry->cache_grc = i;
cache_free(entry);
return;
}
}
entry->cache_grc = grc;
(void) rw_wrlock(&cache_lock);
entry->cache_next = cache_head;
cache_head = entry;
(void) rw_unlock(&cache_lock);
}
void
netgrp_cache_flush(void)
{
(void) rw_wrlock(&cache_lock);
cache_free(cache_head);
cache_head = NULL;
(void) rw_unlock(&cache_lock);
}