#include <sys/param.h>
#include <sys/errno.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/cred.h>
#include <sys/cmn_err.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/pathname.h>
#include <sys/utsname.h>
#include <sys/debug.h>
#include <sys/door.h>
#include <sys/sdt.h>
#include <sys/thread.h>
#include <sys/avl.h>
#include <rpc/types.h>
#include <rpc/auth.h>
#include <rpc/clnt.h>
#include <nfs/nfs.h>
#include <nfs/export.h>
#include <nfs/nfs_clnt.h>
#include <nfs/auth.h>
static struct kmem_cache *exi_cache_handle;
static void exi_cache_reclaim(void *);
static void exi_cache_reclaim_zone(nfs_globals_t *);
static void exi_cache_trim(struct exportinfo *exi);
extern pri_t minclsyspri;
volatile uint_t nfsauth_cache_hit;
volatile uint_t nfsauth_cache_miss;
volatile uint_t nfsauth_cache_refresh;
volatile uint_t nfsauth_cache_reclaim;
volatile uint_t exi_cache_auth_reclaim_failed;
volatile uint_t exi_cache_clnt_reclaim_failed;
#define NFSAUTH_CACHE_REFRESH 600
#define NFSAUTH_CACHE_TRIM 3600
typedef struct refreshq_exi_node {
struct exportinfo *ren_exi;
list_t ren_authlist;
list_node_t ren_node;
} refreshq_exi_node_t;
typedef struct refreshq_auth_node {
struct auth_cache *ran_auth;
char *ran_netid;
list_node_t ran_node;
} refreshq_auth_node_t;
typedef enum nfsauth_refreshq_thread_state {
REFRESHQ_THREAD_RUNNING,
REFRESHQ_THREAD_FINI_REQ,
REFRESHQ_THREAD_HALTED,
REFRESHQ_THREAD_NEED_CREATE
} nfsauth_refreshq_thread_state_t;
typedef struct nfsauth_globals {
kmutex_t mountd_lock;
door_handle_t mountd_dh;
kmutex_t refreshq_lock;
list_t refreshq_queue;
kcondvar_t refreshq_cv;
struct auth_cache *refreshq_dead_entries;
nfsauth_refreshq_thread_state_t refreshq_thread_state;
} nfsauth_globals_t;
static void nfsauth_free_node(struct auth_cache *);
static void nfsauth_refresh_thread(nfsauth_globals_t *);
static int nfsauth_cache_compar(const void *, const void *);
static nfsauth_globals_t *
nfsauth_get_zg(void)
{
nfs_globals_t *ng = nfs_srv_getzg();
nfsauth_globals_t *nag = ng->nfs_auth;
ASSERT(nag != NULL);
return (nag);
}
void
mountd_args(uint_t did)
{
nfsauth_globals_t *nag;
nag = nfsauth_get_zg();
mutex_enter(&nag->mountd_lock);
if (nag->mountd_dh != NULL)
door_ki_rele(nag->mountd_dh);
nag->mountd_dh = door_ki_lookup(did);
mutex_exit(&nag->mountd_lock);
}
void
nfsauth_init(void)
{
exi_cache_handle = kmem_cache_create("exi_cache_handle",
sizeof (struct auth_cache), 0, NULL, NULL,
exi_cache_reclaim, NULL, NULL, 0);
}
void
nfsauth_fini(void)
{
kmem_cache_destroy(exi_cache_handle);
}
void
nfsauth_zone_init(nfs_globals_t *ng)
{
nfsauth_globals_t *nag;
nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
offsetof(refreshq_exi_node_t, ren_node));
cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
ng->nfs_auth = nag;
}
void
nfsauth_zone_shutdown(nfs_globals_t *ng)
{
refreshq_exi_node_t *ren;
nfsauth_globals_t *nag = ng->nfs_auth;
mutex_enter(&nag->refreshq_lock);
if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
cv_broadcast(&nag->refreshq_cv);
while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
}
mutex_exit(&nag->refreshq_lock);
while ((ren = list_remove_head(&nag->refreshq_queue))) {
refreshq_auth_node_t *ran;
while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
struct auth_cache *p = ran->ran_auth;
if (p->auth_state == NFS_AUTH_INVALID)
nfsauth_free_node(p);
strfree(ran->ran_netid);
kmem_free(ran, sizeof (*ran));
}
list_destroy(&ren->ren_authlist);
exi_rele(ren->ren_exi);
kmem_free(ren, sizeof (*ren));
}
}
void
nfsauth_zone_fini(nfs_globals_t *ng)
{
nfsauth_globals_t *nag = ng->nfs_auth;
ng->nfs_auth = NULL;
list_destroy(&nag->refreshq_queue);
cv_destroy(&nag->refreshq_cv);
mutex_destroy(&nag->refreshq_lock);
mutex_destroy(&nag->mountd_lock);
if (nag->mountd_dh != NULL)
door_ki_rele(nag->mountd_dh);
kmem_free(nag, sizeof (*nag));
}
static int
hash(struct netbuf *a)
{
int i, h = 0;
for (i = 0; i < a->len; i++)
h ^= a->buf[i];
return (h & (AUTH_TABLESIZE - 1));
}
static void
addrmask(struct netbuf *addr, struct netbuf *mask)
{
int i;
for (i = 0; i < addr->len; i++)
addr->buf[i] &= mask->buf[i];
}
int
nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req,
cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
{
int access;
access = nfsauth_access(exi, req, cr, uid, gid, ngids, gids);
if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
if (has_visible(exi, vp))
return (NFSAUTH_LIMITED);
}
return (access);
}
static void
sys_log(const char *msg)
{
static time_t tstamp = 0;
time_t now;
now = gethrestime_sec();
if ((tstamp + 60) < now) {
tstamp = now;
cmn_err(CE_WARN, msg);
}
}
static bool_t
nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
char *req_netid, int flavor, struct netbuf *addr, int *access,
cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
gid_t **srv_gids)
{
varg_t varg = {0};
nfsauth_res_t res = {0};
XDR xdrs;
size_t absz;
caddr_t abuf;
int last = 0;
door_arg_t da;
door_info_t di;
door_handle_t dh;
uint_t ntries = 0;
varg.vers = V_PROTO;
varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
varg.arg_u.arg.areq.req_client.n_len = addr->len;
varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
varg.arg_u.arg.areq.req_netid = req_netid;
varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
varg.arg_u.arg.areq.req_flavor = flavor;
varg.arg_u.arg.areq.req_clnt_uid = crgetuid(clnt_cred);
varg.arg_u.arg.areq.req_clnt_gid = crgetgid(clnt_cred);
varg.arg_u.arg.areq.req_clnt_gids.len = crgetngroups(clnt_cred);
varg.arg_u.arg.areq.req_clnt_gids.val = (gid_t *)crgetgroups(clnt_cred);
DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
if ((absz = xdr_sizeof(xdr_varg, &varg)) == 0) {
*access = NFSAUTH_DENIED;
return (FALSE);
}
abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
xdrmem_create(&xdrs, abuf, absz, XDR_ENCODE);
if (!xdr_varg(&xdrs, &varg)) {
XDR_DESTROY(&xdrs);
goto fail;
}
XDR_DESTROY(&xdrs);
da.data_ptr = (char *)abuf;
da.data_size = absz;
da.desc_ptr = NULL;
da.desc_num = 0;
da.rbuf = NULL;
da.rsize = 1;
retry:
mutex_enter(&nag->mountd_lock);
dh = nag->mountd_dh;
if (dh != NULL)
door_ki_hold(dh);
mutex_exit(&nag->mountd_lock);
if (dh == NULL) {
if (++ntries % NFSAUTH_DR_TRYCNT) {
delay(hz);
goto retry;
}
kmem_free(abuf, absz);
sys_log("nfsauth: mountd has not established door");
*access = NFSAUTH_DROP;
return (FALSE);
}
ntries = 0;
switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
case 0:
door_ki_rele(dh);
if (da.data_ptr == NULL && da.data_size == 0) {
DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
door_arg_t *, &da);
goto fail;
}
break;
case EAGAIN:
door_ki_rele(dh);
delay(hz);
goto retry;
case EINTR:
if (!door_ki_info(dh, &di)) {
door_ki_rele(dh);
if (di.di_attributes & DOOR_REVOKED) {
mutex_enter(&nag->mountd_lock);
if (dh == nag->mountd_dh) {
door_ki_rele(nag->mountd_dh);
nag->mountd_dh = NULL;
}
mutex_exit(&nag->mountd_lock);
delay(hz);
goto retry;
}
goto fail;
}
case EBADF:
case EINVAL:
default:
door_ki_rele(dh);
if (!last) {
delay(hz);
last++;
goto retry;
}
sys_log("nfsauth: stale mountd door handle");
goto fail;
}
ASSERT(da.rbuf != NULL);
xdrmem_create(&xdrs, da.rbuf, da.rsize, XDR_DECODE);
if (!xdr_nfsauth_res(&xdrs, &res)) {
xdr_free(xdr_nfsauth_res, (char *)&res);
XDR_DESTROY(&xdrs);
kmem_free(da.rbuf, da.rsize);
goto fail;
}
XDR_DESTROY(&xdrs);
kmem_free(da.rbuf, da.rsize);
DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
switch (res.stat) {
case NFSAUTH_DR_OKAY:
*access = res.ares.auth_perm;
*srv_uid = res.ares.auth_srv_uid;
*srv_gid = res.ares.auth_srv_gid;
if ((*srv_gids_cnt = res.ares.auth_srv_gids.len) != 0) {
*srv_gids = kmem_alloc(*srv_gids_cnt *
sizeof (gid_t), KM_SLEEP);
bcopy(res.ares.auth_srv_gids.val, *srv_gids,
*srv_gids_cnt * sizeof (gid_t));
} else {
*srv_gids = NULL;
}
break;
case NFSAUTH_DR_EFAIL:
case NFSAUTH_DR_DECERR:
case NFSAUTH_DR_BADCMD:
default:
xdr_free(xdr_nfsauth_res, (char *)&res);
fail:
*access = NFSAUTH_DENIED;
kmem_free(abuf, absz);
return (FALSE);
}
xdr_free(xdr_nfsauth_res, (char *)&res);
kmem_free(abuf, absz);
return (TRUE);
}
static void
nfsauth_refresh_thread(nfsauth_globals_t *nag)
{
refreshq_exi_node_t *ren;
refreshq_auth_node_t *ran;
struct exportinfo *exi;
int access;
bool_t retrieval;
callb_cpr_t cprinfo;
CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
"nfsauth_refresh");
for (;;) {
mutex_enter(&nag->refreshq_lock);
if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
break;
}
ren = list_remove_head(&nag->refreshq_queue);
if (ren == NULL) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
mutex_exit(&nag->refreshq_lock);
continue;
}
mutex_exit(&nag->refreshq_lock);
exi = ren->ren_exi;
ASSERT(exi != NULL);
while ((ran = list_remove_head(&ren->ren_authlist))) {
uid_t uid;
gid_t gid;
uint_t ngids;
gid_t *gids;
struct auth_cache *p = ran->ran_auth;
char *netid = ran->ran_netid;
ASSERT(p != NULL);
ASSERT(netid != NULL);
kmem_free(ran, sizeof (refreshq_auth_node_t));
mutex_enter(&p->auth_lock);
if (p->auth_state == NFS_AUTH_INVALID ||
nag->refreshq_thread_state !=
REFRESHQ_THREAD_RUNNING) {
mutex_exit(&p->auth_lock);
if (p->auth_state == NFS_AUTH_INVALID)
nfsauth_free_node(p);
strfree(netid);
continue;
}
ASSERT(p->auth_state == NFS_AUTH_STALE);
p->auth_state = NFS_AUTH_REFRESHING;
mutex_exit(&p->auth_lock);
DTRACE_PROBE2(nfsauth__debug__cache__refresh,
struct exportinfo *, exi,
struct auth_cache *, p);
retrieval = nfsauth_retrieve(nag, exi, netid,
p->auth_flavor, &p->auth_clnt->authc_addr, &access,
p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
strfree(netid);
mutex_enter(&p->auth_lock);
if (p->auth_state == NFS_AUTH_INVALID) {
mutex_exit(&p->auth_lock);
nfsauth_free_node(p);
if (retrieval == TRUE)
kmem_free(gids, ngids * sizeof (gid_t));
} else {
if (retrieval == TRUE) {
p->auth_access = access;
p->auth_srv_uid = uid;
p->auth_srv_gid = gid;
kmem_free(p->auth_srv_gids,
p->auth_srv_ngids * sizeof (gid_t));
p->auth_srv_ngids = ngids;
p->auth_srv_gids = gids;
p->auth_freshness = gethrestime_sec();
}
p->auth_state = NFS_AUTH_FRESH;
cv_broadcast(&p->auth_cv);
mutex_exit(&p->auth_lock);
}
}
list_destroy(&ren->ren_authlist);
exi_rele(ren->ren_exi);
kmem_free(ren, sizeof (refreshq_exi_node_t));
}
nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
cv_broadcast(&nag->refreshq_cv);
CALLB_CPR_EXIT(&cprinfo);
DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
zthread_exit();
}
int
nfsauth_cache_clnt_compar(const void *v1, const void *v2)
{
int c;
const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
if (a1->authc_addr.len < a2->authc_addr.len)
return (-1);
if (a1->authc_addr.len > a2->authc_addr.len)
return (1);
c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
if (c < 0)
return (-1);
if (c > 0)
return (1);
return (0);
}
static int
nfsauth_cache_compar(const void *v1, const void *v2)
{
int c;
const struct auth_cache *a1 = (const struct auth_cache *)v1;
const struct auth_cache *a2 = (const struct auth_cache *)v2;
if (a1->auth_flavor < a2->auth_flavor)
return (-1);
if (a1->auth_flavor > a2->auth_flavor)
return (1);
if (crgetuid(a1->auth_clnt_cred) < crgetuid(a2->auth_clnt_cred))
return (-1);
if (crgetuid(a1->auth_clnt_cred) > crgetuid(a2->auth_clnt_cred))
return (1);
if (crgetgid(a1->auth_clnt_cred) < crgetgid(a2->auth_clnt_cred))
return (-1);
if (crgetgid(a1->auth_clnt_cred) > crgetgid(a2->auth_clnt_cred))
return (1);
if (crgetngroups(a1->auth_clnt_cred) < crgetngroups(a2->auth_clnt_cred))
return (-1);
if (crgetngroups(a1->auth_clnt_cred) > crgetngroups(a2->auth_clnt_cred))
return (1);
c = memcmp(crgetgroups(a1->auth_clnt_cred),
crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
if (c < 0)
return (-1);
if (c > 0)
return (1);
return (0);
}
static int
nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
{
nfsauth_globals_t *nag;
struct netbuf *taddrmask;
struct netbuf addr;
const struct netbuf *claddr;
avl_tree_t *tree;
struct auth_cache ac;
struct auth_cache_clnt *c;
struct auth_cache_clnt acc;
struct auth_cache *p = NULL;
int access;
uid_t tmpuid;
gid_t tmpgid;
uint_t tmpngids;
gid_t *tmpgids;
avl_index_t where;
ASSERT(cr != NULL);
ASSERT3P(curzone->zone_id, ==, exi->exi_zoneid);
nag = nfsauth_get_zg();
claddr = svc_getrpccaller(req->rq_xprt);
addr = *claddr;
if (claddr->len != 0) {
addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
bcopy(claddr->buf, addr.buf, claddr->len);
} else {
addr.buf = NULL;
}
SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
ASSERT(taddrmask != NULL);
addrmask(&addr, taddrmask);
acc.authc_addr = addr;
tree = exi->exi_cache[hash(&addr)];
rw_enter(&exi->exi_cache_lock, RW_READER);
c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
if (c == NULL) {
struct auth_cache_clnt *nc;
rw_exit(&exi->exi_cache_lock);
nc = kmem_alloc(sizeof (*nc), KM_NOSLEEP_LAZY);
if (nc == NULL)
goto retrieve;
nc->authc_addr = addr;
nc->authc_addr.buf = kmem_alloc(addr.maxlen, KM_NOSLEEP_LAZY);
if (addr.maxlen != 0 && nc->authc_addr.buf == NULL) {
kmem_free(nc, sizeof (*nc));
goto retrieve;
}
bcopy(addr.buf, nc->authc_addr.buf, addr.len);
rw_init(&nc->authc_lock, NULL, RW_DEFAULT, NULL);
avl_create(&nc->authc_tree, nfsauth_cache_compar,
sizeof (struct auth_cache),
offsetof(struct auth_cache, auth_link));
rw_enter(&exi->exi_cache_lock, RW_WRITER);
c = (struct auth_cache_clnt *)avl_find(tree, &acc, &where);
if (c == NULL) {
avl_insert(tree, nc, where);
rw_downgrade(&exi->exi_cache_lock);
c = nc;
} else {
rw_downgrade(&exi->exi_cache_lock);
avl_destroy(&nc->authc_tree);
rw_destroy(&nc->authc_lock);
kmem_free(nc->authc_addr.buf, nc->authc_addr.maxlen);
kmem_free(nc, sizeof (*nc));
}
}
ASSERT(c != NULL);
rw_enter(&c->authc_lock, RW_READER);
ac.auth_flavor = flavor;
ac.auth_clnt_cred = cr;
p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, NULL);
if (p == NULL) {
struct auth_cache *np;
rw_exit(&c->authc_lock);
np = kmem_cache_alloc(exi_cache_handle, KM_NOSLEEP_LAZY);
if (np == NULL) {
rw_exit(&exi->exi_cache_lock);
goto retrieve;
}
np->auth_clnt = c;
np->auth_flavor = flavor;
np->auth_clnt_cred = crdup(cr);
np->auth_srv_ngids = 0;
np->auth_srv_gids = NULL;
np->auth_time = np->auth_freshness = gethrestime_sec();
np->auth_state = NFS_AUTH_NEW;
mutex_init(&np->auth_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&np->auth_cv, NULL, CV_DEFAULT, NULL);
rw_enter(&c->authc_lock, RW_WRITER);
rw_exit(&exi->exi_cache_lock);
p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, &where);
if (p == NULL) {
avl_insert(&c->authc_tree, np, where);
rw_downgrade(&c->authc_lock);
p = np;
} else {
rw_downgrade(&c->authc_lock);
cv_destroy(&np->auth_cv);
mutex_destroy(&np->auth_lock);
crfree(np->auth_clnt_cred);
kmem_cache_free(exi_cache_handle, np);
}
} else {
rw_exit(&exi->exi_cache_lock);
}
mutex_enter(&p->auth_lock);
rw_exit(&c->authc_lock);
while (p->auth_state == NFS_AUTH_WAITING)
cv_wait(&p->auth_cv, &p->auth_lock);
ASSERT(p->auth_state != NFS_AUTH_WAITING);
ASSERT(p->auth_state != NFS_AUTH_INVALID);
if (p->auth_state == NFS_AUTH_NEW) {
bool_t res;
auth_state_t state = NFS_AUTH_NEW;
p->auth_state = NFS_AUTH_WAITING;
mutex_exit(&p->auth_lock);
kmem_free(addr.buf, addr.maxlen);
addr = p->auth_clnt->authc_addr;
nfsauth_cache_miss++;
res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
&tmpgids);
p->auth_access = access;
p->auth_time = p->auth_freshness = gethrestime_sec();
if (res == TRUE) {
if (uid != NULL)
*uid = tmpuid;
if (gid != NULL)
*gid = tmpgid;
if (ngids != NULL && gids != NULL) {
*ngids = tmpngids;
*gids = tmpgids;
tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
KM_NOSLEEP_LAZY);
if (tmpgids != NULL)
bcopy(*gids, tmpgids,
tmpngids * sizeof (gid_t));
}
if (tmpgids != NULL || tmpngids == 0) {
p->auth_srv_uid = tmpuid;
p->auth_srv_gid = tmpgid;
p->auth_srv_ngids = tmpngids;
p->auth_srv_gids = tmpgids;
state = NFS_AUTH_FRESH;
}
}
mutex_enter(&p->auth_lock);
p->auth_state = state;
cv_broadcast(&p->auth_cv);
mutex_exit(&p->auth_lock);
} else {
uint_t nach;
time_t refresh;
refresh = gethrestime_sec() - p->auth_freshness;
p->auth_time = gethrestime_sec();
if (uid != NULL)
*uid = p->auth_srv_uid;
if (gid != NULL)
*gid = p->auth_srv_gid;
if (ngids != NULL && gids != NULL) {
if ((*ngids = p->auth_srv_ngids) != 0) {
size_t sz = *ngids * sizeof (gid_t);
*gids = kmem_alloc(sz, KM_SLEEP);
bcopy(p->auth_srv_gids, *gids, sz);
} else {
*gids = NULL;
}
}
access = p->auth_access;
if ((refresh > NFSAUTH_CACHE_REFRESH) &&
p->auth_state == NFS_AUTH_FRESH) {
refreshq_auth_node_t *ran;
uint_t nacr;
p->auth_state = NFS_AUTH_STALE;
mutex_exit(&p->auth_lock);
nacr = ++nfsauth_cache_refresh;
DTRACE_PROBE3(nfsauth__debug__cache__stale,
struct exportinfo *, exi,
struct auth_cache *, p,
uint_t, nacr);
ran = kmem_alloc(sizeof (refreshq_auth_node_t),
KM_SLEEP);
ran->ran_auth = p;
ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
mutex_enter(&nag->refreshq_lock);
if (nag->refreshq_thread_state ==
REFRESHQ_THREAD_NEED_CREATE) {
nag->refreshq_thread_state =
REFRESHQ_THREAD_RUNNING;
(void) zthread_create(NULL, 0,
nfsauth_refresh_thread, nag, 0,
minclsyspri);
}
if (nag->refreshq_thread_state ==
REFRESHQ_THREAD_RUNNING) {
refreshq_exi_node_t *ren;
for (ren = list_head(&nag->refreshq_queue);
ren != NULL;
ren = list_next(&nag->refreshq_queue,
ren)) {
if (ren->ren_exi == exi) {
list_insert_tail(
&ren->ren_authlist, ran);
break;
}
}
if (ren == NULL) {
ren = kmem_alloc(
sizeof (refreshq_exi_node_t),
KM_SLEEP);
exi_hold(exi);
ren->ren_exi = exi;
list_create(&ren->ren_authlist,
sizeof (refreshq_auth_node_t),
offsetof(refreshq_auth_node_t,
ran_node));
list_insert_tail(&ren->ren_authlist,
ran);
list_insert_tail(&nag->refreshq_queue,
ren);
}
cv_broadcast(&nag->refreshq_cv);
} else {
strfree(ran->ran_netid);
kmem_free(ran, sizeof (refreshq_auth_node_t));
}
mutex_exit(&nag->refreshq_lock);
} else {
mutex_exit(&p->auth_lock);
}
nach = ++nfsauth_cache_hit;
DTRACE_PROBE2(nfsauth__debug__cache__hit,
uint_t, nach,
time_t, refresh);
kmem_free(addr.buf, addr.maxlen);
}
return (access);
retrieve:
ASSERT(p == NULL);
nfsauth_cache_miss++;
if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
&addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
if (uid != NULL)
*uid = tmpuid;
if (gid != NULL)
*gid = tmpgid;
if (ngids != NULL && gids != NULL) {
*ngids = tmpngids;
*gids = tmpgids;
} else {
kmem_free(tmpgids, tmpngids * sizeof (gid_t));
}
}
kmem_free(addr.buf, addr.maxlen);
return (access);
}
int
nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
int flavor, int perm, cred_t *cr)
{
int access;
if (! (perm & M_4SEC_EXPORTED)) {
return (NFSAUTH_DENIED);
}
if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0) {
perm &= ~M_4SEC_EXPORTED;
if (perm == M_RO)
return (NFSAUTH_RO);
if (perm == M_RW)
return (NFSAUTH_RW);
}
access = nfsauth_cache_get(exi, req, flavor, cr, NULL, NULL, NULL,
NULL);
return (access);
}
int
nfsauth_access(struct exportinfo *exi, struct svc_req *req, cred_t *cr,
uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
{
int access, mapaccess;
struct secinfo *sp;
int i, flavor, perm;
int authnone_entry = -1;
if (crgetuid(cr) == 0) {
if (uid != NULL)
*uid = exi->exi_export.ex_anon;
if (gid != NULL)
*gid = exi->exi_export.ex_anon;
} else {
if (uid != NULL)
*uid = crgetuid(cr);
if (gid != NULL)
*gid = crgetgid(cr);
}
if (ngids != NULL)
*ngids = 0;
if (gids != NULL)
*gids = NULL;
flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
sp = exi->exi_export.ex_secinfo;
for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
if (flavor != sp[i].s_secinfo.sc_nfsnum) {
if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
authnone_entry = i;
continue;
}
break;
}
mapaccess = 0;
if (i >= exi->exi_export.ex_seccnt) {
if (authnone_entry == -1)
return (NFSAUTH_DENIED);
flavor = AUTH_NONE;
mapaccess = NFSAUTH_MAPNONE;
i = authnone_entry;
}
if (!(sp[i].s_flags & M_4SEC_EXPORTED))
return (mapaccess | NFSAUTH_RO);
perm = sp[i].s_flags;
if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0 && (ngroups_max <= NGRPS ||
flavor != AUTH_SYS || crgetngroups(cr) < NGRPS)) {
perm &= ~M_4SEC_EXPORTED;
if (perm == M_RO)
return (mapaccess | NFSAUTH_RO);
if (perm == M_RW)
return (mapaccess | NFSAUTH_RW);
}
access = nfsauth_cache_get(exi, req, flavor, cr, uid, gid, ngids, gids);
if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
if (ngids != NULL && gids != NULL) {
kmem_free(*gids, *ngids * sizeof (gid_t));
*ngids = 0;
*gids = NULL;
}
}
if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
if (authnone_entry != -1) {
mapaccess = NFSAUTH_MAPNONE;
access = nfsauth_cache_get(exi, req, AUTH_NONE, cr,
NULL, NULL, NULL, NULL);
} else {
for (; i < exi->exi_export.ex_seccnt; i++) {
if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
mapaccess = NFSAUTH_MAPNONE;
access = nfsauth_cache_get(exi, req,
AUTH_NONE, cr, NULL, NULL, NULL,
NULL);
break;
}
}
}
}
if (access & NFSAUTH_DENIED)
access = NFSAUTH_DENIED;
return (access | mapaccess);
}
static void
nfsauth_free_clnt_node(struct auth_cache_clnt *p)
{
void *cookie = NULL;
struct auth_cache *node;
while ((node = avl_destroy_nodes(&p->authc_tree, &cookie)) != NULL)
nfsauth_free_node(node);
avl_destroy(&p->authc_tree);
kmem_free(p->authc_addr.buf, p->authc_addr.maxlen);
rw_destroy(&p->authc_lock);
kmem_free(p, sizeof (*p));
}
static void
nfsauth_free_node(struct auth_cache *p)
{
crfree(p->auth_clnt_cred);
kmem_free(p->auth_srv_gids, p->auth_srv_ngids * sizeof (gid_t));
mutex_destroy(&p->auth_lock);
cv_destroy(&p->auth_cv);
kmem_cache_free(exi_cache_handle, p);
}
void
nfsauth_cache_free(struct exportinfo *exi)
{
int i;
for (i = 0; i < AUTH_TABLESIZE; i++) {
avl_tree_t *tree = exi->exi_cache[i];
void *cookie = NULL;
struct auth_cache_clnt *node;
while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
nfsauth_free_clnt_node(node);
}
}
void
exi_cache_reclaim(void *cdrarg)
{
nfs_globals_t *ng;
rw_enter(&nfssrv_globals_rwl, RW_READER);
ng = list_head(&nfssrv_globals_list);
while (ng != NULL) {
exi_cache_reclaim_zone(ng);
ng = list_next(&nfssrv_globals_list, ng);
}
rw_exit(&nfssrv_globals_rwl);
}
static void
exi_cache_reclaim_zone(nfs_globals_t *ng)
{
int i;
struct exportinfo *exi;
nfs_export_t *ne = ng->nfs_export;
rw_enter(&ne->exported_lock, RW_READER);
for (i = 0; i < EXPTABLESIZE; i++) {
for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
exi_cache_trim(exi);
}
rw_exit(&ne->exported_lock);
nfsauth_cache_reclaim++;
}
static void
exi_cache_trim(struct exportinfo *exi)
{
struct auth_cache_clnt *c;
struct auth_cache_clnt *nextc;
struct auth_cache *p;
struct auth_cache *next;
int i;
time_t stale_time;
avl_tree_t *tree;
for (i = 0; i < AUTH_TABLESIZE; i++) {
tree = exi->exi_cache[i];
stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
rw_enter(&exi->exi_cache_lock, RW_READER);
for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {
if (rw_tryenter(&c->authc_lock, RW_WRITER) == 0) {
exi_cache_auth_reclaim_failed++;
rw_exit(&exi->exi_cache_lock);
return;
}
for (p = avl_first(&c->authc_tree); p != NULL;
p = next) {
next = AVL_NEXT(&c->authc_tree, p);
ASSERT(p->auth_state != NFS_AUTH_INVALID);
mutex_enter(&p->auth_lock);
if (p->auth_time > stale_time ||
p->auth_state == NFS_AUTH_WAITING) {
mutex_exit(&p->auth_lock);
continue;
}
DTRACE_PROBE1(nfsauth__debug__trim__state,
auth_state_t, p->auth_state);
if (p->auth_state == NFS_AUTH_STALE ||
p->auth_state == NFS_AUTH_REFRESHING) {
p->auth_state = NFS_AUTH_INVALID;
mutex_exit(&p->auth_lock);
avl_remove(&c->authc_tree, p);
} else {
mutex_exit(&p->auth_lock);
avl_remove(&c->authc_tree, p);
nfsauth_free_node(p);
}
}
rw_exit(&c->authc_lock);
}
if (rw_tryupgrade(&exi->exi_cache_lock) == 0) {
rw_exit(&exi->exi_cache_lock);
exi_cache_clnt_reclaim_failed++;
continue;
}
for (c = avl_first(tree); c != NULL; c = nextc) {
nextc = AVL_NEXT(tree, c);
if (avl_is_empty(&c->authc_tree) == B_FALSE)
continue;
avl_remove(tree, c);
nfsauth_free_clnt_node(c);
}
rw_exit(&exi->exi_cache_lock);
}
}