OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [sunrpc/] [auth.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*
/*
 * linux/fs/nfs/rpcauth.c
 * linux/fs/nfs/rpcauth.c
 *
 *
 * Generic RPC authentication API.
 * Generic RPC authentication API.
 *
 *
 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
 */
 */
 
 
#include <linux/types.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/socket.h>
#include <linux/socket.h>
#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/clnt.h>
#include <linux/spinlock.h>
#include <linux/spinlock.h>
 
 
#ifdef RPC_DEBUG
#ifdef RPC_DEBUG
# define RPCDBG_FACILITY        RPCDBG_AUTH
# define RPCDBG_FACILITY        RPCDBG_AUTH
#endif
#endif
 
 
#define RPC_MAXFLAVOR   8
#define RPC_MAXFLAVOR   8
 
 
static struct rpc_authops *     auth_flavors[RPC_MAXFLAVOR] = {
static struct rpc_authops *     auth_flavors[RPC_MAXFLAVOR] = {
        &authnull_ops,          /* AUTH_NULL */
        &authnull_ops,          /* AUTH_NULL */
        &authunix_ops,          /* AUTH_UNIX */
        &authunix_ops,          /* AUTH_UNIX */
        NULL,                   /* others can be loadable modules */
        NULL,                   /* others can be loadable modules */
};
};
 
 
int
int
rpcauth_register(struct rpc_authops *ops)
rpcauth_register(struct rpc_authops *ops)
{
{
        unsigned int    flavor;
        unsigned int    flavor;
 
 
        if ((flavor = ops->au_flavor) >= RPC_MAXFLAVOR)
        if ((flavor = ops->au_flavor) >= RPC_MAXFLAVOR)
                return -EINVAL;
                return -EINVAL;
        if (auth_flavors[flavor] != NULL)
        if (auth_flavors[flavor] != NULL)
                return -EPERM;          /* what else? */
                return -EPERM;          /* what else? */
        auth_flavors[flavor] = ops;
        auth_flavors[flavor] = ops;
        return 0;
        return 0;
}
}
 
 
int
int
rpcauth_unregister(struct rpc_authops *ops)
rpcauth_unregister(struct rpc_authops *ops)
{
{
        unsigned int    flavor;
        unsigned int    flavor;
 
 
        if ((flavor = ops->au_flavor) >= RPC_MAXFLAVOR)
        if ((flavor = ops->au_flavor) >= RPC_MAXFLAVOR)
                return -EINVAL;
                return -EINVAL;
        if (auth_flavors[flavor] != ops)
        if (auth_flavors[flavor] != ops)
                return -EPERM;          /* what else? */
                return -EPERM;          /* what else? */
        auth_flavors[flavor] = NULL;
        auth_flavors[flavor] = NULL;
        return 0;
        return 0;
}
}
 
 
struct rpc_auth *
struct rpc_auth *
rpcauth_create(unsigned int flavor, struct rpc_clnt *clnt)
rpcauth_create(unsigned int flavor, struct rpc_clnt *clnt)
{
{
        struct rpc_authops      *ops;
        struct rpc_authops      *ops;
 
 
        if (flavor >= RPC_MAXFLAVOR || !(ops = auth_flavors[flavor]))
        if (flavor >= RPC_MAXFLAVOR || !(ops = auth_flavors[flavor]))
                return NULL;
                return NULL;
        clnt->cl_auth = ops->create(clnt);
        clnt->cl_auth = ops->create(clnt);
        return clnt->cl_auth;
        return clnt->cl_auth;
}
}
 
 
void
void
rpcauth_destroy(struct rpc_auth *auth)
rpcauth_destroy(struct rpc_auth *auth)
{
{
        auth->au_ops->destroy(auth);
        auth->au_ops->destroy(auth);
}
}
 
 
static spinlock_t rpc_credcache_lock = SPIN_LOCK_UNLOCKED;
static spinlock_t rpc_credcache_lock = SPIN_LOCK_UNLOCKED;
 
 
/*
/*
 * Initialize RPC credential cache
 * Initialize RPC credential cache
 */
 */
void
void
rpcauth_init_credcache(struct rpc_auth *auth)
rpcauth_init_credcache(struct rpc_auth *auth)
{
{
        memset(auth->au_credcache, 0, sizeof(auth->au_credcache));
        memset(auth->au_credcache, 0, sizeof(auth->au_credcache));
        auth->au_nextgc = jiffies + (auth->au_expire >> 1);
        auth->au_nextgc = jiffies + (auth->au_expire >> 1);
}
}
 
 
/*
/*
 * Destroy an unreferenced credential
 * Destroy an unreferenced credential
 */
 */
static inline void
static inline void
rpcauth_crdestroy(struct rpc_cred *cred)
rpcauth_crdestroy(struct rpc_cred *cred)
{
{
#ifdef RPC_DEBUG
#ifdef RPC_DEBUG
        if (cred->cr_magic != RPCAUTH_CRED_MAGIC)
        if (cred->cr_magic != RPCAUTH_CRED_MAGIC)
                BUG();
                BUG();
        cred->cr_magic = 0;
        cred->cr_magic = 0;
        if (atomic_read(&cred->cr_count) || cred->cr_auth)
        if (atomic_read(&cred->cr_count) || cred->cr_auth)
                BUG();
                BUG();
#endif
#endif
        cred->cr_ops->crdestroy(cred);
        cred->cr_ops->crdestroy(cred);
}
}
 
 
/*
/*
 * Destroy a list of credentials
 * Destroy a list of credentials
 */
 */
static inline
static inline
void rpcauth_destroy_credlist(struct rpc_cred *head)
void rpcauth_destroy_credlist(struct rpc_cred *head)
{
{
        struct rpc_cred *cred;
        struct rpc_cred *cred;
 
 
        while ((cred = head) != NULL) {
        while ((cred = head) != NULL) {
                head = cred->cr_next;
                head = cred->cr_next;
                rpcauth_crdestroy(cred);
                rpcauth_crdestroy(cred);
        }
        }
}
}
 
 
/*
/*
 * Clear the RPC credential cache, and delete those credentials
 * Clear the RPC credential cache, and delete those credentials
 * that are not referenced.
 * that are not referenced.
 */
 */
void
void
rpcauth_free_credcache(struct rpc_auth *auth)
rpcauth_free_credcache(struct rpc_auth *auth)
{
{
        struct rpc_cred **q, *cred, *free = NULL;
        struct rpc_cred **q, *cred, *free = NULL;
        int             i;
        int             i;
 
 
        spin_lock(&rpc_credcache_lock);
        spin_lock(&rpc_credcache_lock);
        for (i = 0; i < RPC_CREDCACHE_NR; i++) {
        for (i = 0; i < RPC_CREDCACHE_NR; i++) {
                q = &auth->au_credcache[i];
                q = &auth->au_credcache[i];
                while ((cred = *q) != NULL) {
                while ((cred = *q) != NULL) {
                        *q = cred->cr_next;
                        *q = cred->cr_next;
                        cred->cr_auth = NULL;
                        cred->cr_auth = NULL;
                        if (atomic_read(&cred->cr_count) == 0) {
                        if (atomic_read(&cred->cr_count) == 0) {
                                cred->cr_next = free;
                                cred->cr_next = free;
                                free = cred;
                                free = cred;
                        } else
                        } else
                                cred->cr_next = NULL;
                                cred->cr_next = NULL;
                }
                }
        }
        }
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
        rpcauth_destroy_credlist(free);
        rpcauth_destroy_credlist(free);
}
}
 
 
/*
/*
 * Remove stale credentials. Avoid sleeping inside the loop.
 * Remove stale credentials. Avoid sleeping inside the loop.
 */
 */
static void
static void
rpcauth_gc_credcache(struct rpc_auth *auth)
rpcauth_gc_credcache(struct rpc_auth *auth)
{
{
        struct rpc_cred **q, *cred, *free = NULL;
        struct rpc_cred **q, *cred, *free = NULL;
        int             i;
        int             i;
 
 
        dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
        dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
        spin_lock(&rpc_credcache_lock);
        spin_lock(&rpc_credcache_lock);
        for (i = 0; i < RPC_CREDCACHE_NR; i++) {
        for (i = 0; i < RPC_CREDCACHE_NR; i++) {
                q = &auth->au_credcache[i];
                q = &auth->au_credcache[i];
                while ((cred = *q) != NULL) {
                while ((cred = *q) != NULL) {
                        if (!atomic_read(&cred->cr_count) &&
                        if (!atomic_read(&cred->cr_count) &&
                            time_before(cred->cr_expire, jiffies)) {
                            time_before(cred->cr_expire, jiffies)) {
                                *q = cred->cr_next;
                                *q = cred->cr_next;
                                cred->cr_auth = NULL;
                                cred->cr_auth = NULL;
                                cred->cr_next = free;
                                cred->cr_next = free;
                                free = cred;
                                free = cred;
                                continue;
                                continue;
                        }
                        }
                        q = &cred->cr_next;
                        q = &cred->cr_next;
                }
                }
        }
        }
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
        rpcauth_destroy_credlist(free);
        rpcauth_destroy_credlist(free);
        auth->au_nextgc = jiffies + auth->au_expire;
        auth->au_nextgc = jiffies + auth->au_expire;
}
}
 
 
/*
/*
 * Insert credential into cache
 * Insert credential into cache
 */
 */
void
void
rpcauth_insert_credcache(struct rpc_auth *auth, struct rpc_cred *cred)
rpcauth_insert_credcache(struct rpc_auth *auth, struct rpc_cred *cred)
{
{
        int             nr;
        int             nr;
 
 
        nr = (cred->cr_uid & RPC_CREDCACHE_MASK);
        nr = (cred->cr_uid & RPC_CREDCACHE_MASK);
        spin_lock(&rpc_credcache_lock);
        spin_lock(&rpc_credcache_lock);
        cred->cr_next = auth->au_credcache[nr];
        cred->cr_next = auth->au_credcache[nr];
        auth->au_credcache[nr] = cred;
        auth->au_credcache[nr] = cred;
        cred->cr_auth = auth;
        cred->cr_auth = auth;
        get_rpccred(cred);
        get_rpccred(cred);
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
}
}
 
 
/*
/*
 * Look up a process' credentials in the authentication cache
 * Look up a process' credentials in the authentication cache
 */
 */
static struct rpc_cred *
static struct rpc_cred *
rpcauth_lookup_credcache(struct rpc_auth *auth, int taskflags)
rpcauth_lookup_credcache(struct rpc_auth *auth, int taskflags)
{
{
        struct rpc_cred **q, *cred = NULL;
        struct rpc_cred **q, *cred = NULL;
        int             nr = 0;
        int             nr = 0;
 
 
        if (!(taskflags & RPC_TASK_ROOTCREDS))
        if (!(taskflags & RPC_TASK_ROOTCREDS))
                nr = current->uid & RPC_CREDCACHE_MASK;
                nr = current->uid & RPC_CREDCACHE_MASK;
 
 
        if (time_before(auth->au_nextgc, jiffies))
        if (time_before(auth->au_nextgc, jiffies))
                rpcauth_gc_credcache(auth);
                rpcauth_gc_credcache(auth);
 
 
        spin_lock(&rpc_credcache_lock);
        spin_lock(&rpc_credcache_lock);
        q = &auth->au_credcache[nr];
        q = &auth->au_credcache[nr];
        while ((cred = *q) != NULL) {
        while ((cred = *q) != NULL) {
                if (!(cred->cr_flags & RPCAUTH_CRED_DEAD) &&
                if (!(cred->cr_flags & RPCAUTH_CRED_DEAD) &&
                    cred->cr_ops->crmatch(cred, taskflags)) {
                    cred->cr_ops->crmatch(cred, taskflags)) {
                        *q = cred->cr_next;
                        *q = cred->cr_next;
                        break;
                        break;
                }
                }
                q = &cred->cr_next;
                q = &cred->cr_next;
        }
        }
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
 
 
        if (!cred) {
        if (!cred) {
                cred = auth->au_ops->crcreate(taskflags);
                cred = auth->au_ops->crcreate(taskflags);
#ifdef RPC_DEBUG
#ifdef RPC_DEBUG
                if (cred)
                if (cred)
                        cred->cr_magic = RPCAUTH_CRED_MAGIC;
                        cred->cr_magic = RPCAUTH_CRED_MAGIC;
#endif
#endif
        }
        }
 
 
        if (cred)
        if (cred)
                rpcauth_insert_credcache(auth, cred);
                rpcauth_insert_credcache(auth, cred);
 
 
        return (struct rpc_cred *) cred;
        return (struct rpc_cred *) cred;
}
}
 
 
/*
/*
 * Remove cred handle from cache
 * Remove cred handle from cache
 */
 */
static void
static void
rpcauth_remove_credcache(struct rpc_cred *cred)
rpcauth_remove_credcache(struct rpc_cred *cred)
{
{
        struct rpc_auth *auth = cred->cr_auth;
        struct rpc_auth *auth = cred->cr_auth;
        struct rpc_cred **q, *cr;
        struct rpc_cred **q, *cr;
        int             nr;
        int             nr;
 
 
        nr = (cred->cr_uid & RPC_CREDCACHE_MASK);
        nr = (cred->cr_uid & RPC_CREDCACHE_MASK);
        q = &auth->au_credcache[nr];
        q = &auth->au_credcache[nr];
        while ((cr = *q) != NULL) {
        while ((cr = *q) != NULL) {
                if (cred == cr) {
                if (cred == cr) {
                        *q = cred->cr_next;
                        *q = cred->cr_next;
                        cred->cr_next = NULL;
                        cred->cr_next = NULL;
                        cred->cr_auth = NULL;
                        cred->cr_auth = NULL;
                        break;
                        break;
                }
                }
                q = &cred->cr_next;
                q = &cred->cr_next;
        }
        }
}
}
 
 
struct rpc_cred *
struct rpc_cred *
rpcauth_lookupcred(struct rpc_auth *auth, int taskflags)
rpcauth_lookupcred(struct rpc_auth *auth, int taskflags)
{
{
        dprintk("RPC:     looking up %s cred\n",
        dprintk("RPC:     looking up %s cred\n",
                auth->au_ops->au_name);
                auth->au_ops->au_name);
        return rpcauth_lookup_credcache(auth, taskflags);
        return rpcauth_lookup_credcache(auth, taskflags);
}
}
 
 
struct rpc_cred *
struct rpc_cred *
rpcauth_bindcred(struct rpc_task *task)
rpcauth_bindcred(struct rpc_task *task)
{
{
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_auth *auth = task->tk_auth;
 
 
        dprintk("RPC: %4d looking up %s cred\n",
        dprintk("RPC: %4d looking up %s cred\n",
                task->tk_pid, task->tk_auth->au_ops->au_name);
                task->tk_pid, task->tk_auth->au_ops->au_name);
        task->tk_msg.rpc_cred = rpcauth_lookup_credcache(auth, task->tk_flags);
        task->tk_msg.rpc_cred = rpcauth_lookup_credcache(auth, task->tk_flags);
        if (task->tk_msg.rpc_cred == 0)
        if (task->tk_msg.rpc_cred == 0)
                task->tk_status = -ENOMEM;
                task->tk_status = -ENOMEM;
        return task->tk_msg.rpc_cred;
        return task->tk_msg.rpc_cred;
}
}
 
 
int
int
rpcauth_matchcred(struct rpc_auth *auth, struct rpc_cred *cred, int taskflags)
rpcauth_matchcred(struct rpc_auth *auth, struct rpc_cred *cred, int taskflags)
{
{
        dprintk("RPC:     matching %s cred %d\n",
        dprintk("RPC:     matching %s cred %d\n",
                auth->au_ops->au_name, taskflags);
                auth->au_ops->au_name, taskflags);
        return cred->cr_ops->crmatch(cred, taskflags);
        return cred->cr_ops->crmatch(cred, taskflags);
}
}
 
 
void
void
rpcauth_holdcred(struct rpc_task *task)
rpcauth_holdcred(struct rpc_task *task)
{
{
        dprintk("RPC: %4d holding %s cred %p\n",
        dprintk("RPC: %4d holding %s cred %p\n",
                task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
                task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
        if (task->tk_msg.rpc_cred)
        if (task->tk_msg.rpc_cred)
                get_rpccred(task->tk_msg.rpc_cred);
                get_rpccred(task->tk_msg.rpc_cred);
}
}
 
 
void
void
put_rpccred(struct rpc_cred *cred)
put_rpccred(struct rpc_cred *cred)
{
{
        if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
        if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
                return;
                return;
 
 
        if (cred->cr_auth && cred->cr_flags & RPCAUTH_CRED_DEAD)
        if (cred->cr_auth && cred->cr_flags & RPCAUTH_CRED_DEAD)
                rpcauth_remove_credcache(cred);
                rpcauth_remove_credcache(cred);
 
 
        if (!cred->cr_auth) {
        if (!cred->cr_auth) {
                spin_unlock(&rpc_credcache_lock);
                spin_unlock(&rpc_credcache_lock);
                rpcauth_crdestroy(cred);
                rpcauth_crdestroy(cred);
                return;
                return;
        }
        }
        cred->cr_expire = jiffies + cred->cr_auth->au_expire;
        cred->cr_expire = jiffies + cred->cr_auth->au_expire;
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
}
}
 
 
void
void
rpcauth_unbindcred(struct rpc_task *task)
rpcauth_unbindcred(struct rpc_task *task)
{
{
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
 
 
        dprintk("RPC: %4d releasing %s cred %p\n",
        dprintk("RPC: %4d releasing %s cred %p\n",
                task->tk_pid, auth->au_ops->au_name, cred);
                task->tk_pid, auth->au_ops->au_name, cred);
 
 
        put_rpccred(cred);
        put_rpccred(cred);
        task->tk_msg.rpc_cred = NULL;
        task->tk_msg.rpc_cred = NULL;
}
}
 
 
u32 *
u32 *
rpcauth_marshcred(struct rpc_task *task, u32 *p)
rpcauth_marshcred(struct rpc_task *task, u32 *p)
{
{
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
 
 
        dprintk("RPC: %4d marshaling %s cred %p\n",
        dprintk("RPC: %4d marshaling %s cred %p\n",
                task->tk_pid, auth->au_ops->au_name, cred);
                task->tk_pid, auth->au_ops->au_name, cred);
        return cred->cr_ops->crmarshal(task, p,
        return cred->cr_ops->crmarshal(task, p,
                                task->tk_flags & RPC_CALL_REALUID);
                                task->tk_flags & RPC_CALL_REALUID);
}
}
 
 
u32 *
u32 *
rpcauth_checkverf(struct rpc_task *task, u32 *p)
rpcauth_checkverf(struct rpc_task *task, u32 *p)
{
{
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
 
 
        dprintk("RPC: %4d validating %s cred %p\n",
        dprintk("RPC: %4d validating %s cred %p\n",
                task->tk_pid, auth->au_ops->au_name, cred);
                task->tk_pid, auth->au_ops->au_name, cred);
        return cred->cr_ops->crvalidate(task, p);
        return cred->cr_ops->crvalidate(task, p);
}
}
 
 
int
int
rpcauth_refreshcred(struct rpc_task *task)
rpcauth_refreshcred(struct rpc_task *task)
{
{
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_auth *auth = task->tk_auth;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
        struct rpc_cred *cred = task->tk_msg.rpc_cred;
 
 
        dprintk("RPC: %4d refreshing %s cred %p\n",
        dprintk("RPC: %4d refreshing %s cred %p\n",
                task->tk_pid, auth->au_ops->au_name, cred);
                task->tk_pid, auth->au_ops->au_name, cred);
        task->tk_status = cred->cr_ops->crrefresh(task);
        task->tk_status = cred->cr_ops->crrefresh(task);
        return task->tk_status;
        return task->tk_status;
}
}
 
 
void
void
rpcauth_invalcred(struct rpc_task *task)
rpcauth_invalcred(struct rpc_task *task)
{
{
        dprintk("RPC: %4d invalidating %s cred %p\n",
        dprintk("RPC: %4d invalidating %s cred %p\n",
                task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
                task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
        spin_lock(&rpc_credcache_lock);
        spin_lock(&rpc_credcache_lock);
        if (task->tk_msg.rpc_cred)
        if (task->tk_msg.rpc_cred)
                task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
                task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
}
}
 
 
int
int
rpcauth_uptodatecred(struct rpc_task *task)
rpcauth_uptodatecred(struct rpc_task *task)
{
{
        int retval;
        int retval;
        spin_lock(&rpc_credcache_lock);
        spin_lock(&rpc_credcache_lock);
        retval = !(task->tk_msg.rpc_cred) ||
        retval = !(task->tk_msg.rpc_cred) ||
                (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
                (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
        spin_unlock(&rpc_credcache_lock);
        spin_unlock(&rpc_credcache_lock);
        return retval;
        return retval;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.