11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/auth.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Generic RPC client authentication API. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/types.h> 101da177e4SLinus Torvalds #include <linux/sched.h> 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds #include <linux/slab.h> 131da177e4SLinus Torvalds #include <linux/errno.h> 1425337fdcSTrond Myklebust #include <linux/hash.h> 151da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 161da177e4SLinus Torvalds #include <linux/spinlock.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #ifdef RPC_DEBUG 191da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_AUTH 201da177e4SLinus Torvalds #endif 211da177e4SLinus Torvalds 22fc1b356fSTrond Myklebust static DEFINE_SPINLOCK(rpc_authflavor_lock); 23f1c0a861STrond Myklebust static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { 241da177e4SLinus Torvalds &authnull_ops, /* AUTH_NULL */ 251da177e4SLinus Torvalds &authunix_ops, /* AUTH_UNIX */ 261da177e4SLinus Torvalds NULL, /* others can be loadable modules */ 271da177e4SLinus Torvalds }; 281da177e4SLinus Torvalds 29e092bdcdSTrond Myklebust static LIST_HEAD(cred_unused); 30f5c2187cSTrond Myklebust static unsigned long number_cred_unused; 31e092bdcdSTrond Myklebust 321da177e4SLinus Torvalds static u32 331da177e4SLinus Torvalds pseudoflavor_to_flavor(u32 flavor) { 341da177e4SLinus Torvalds if (flavor >= RPC_AUTH_MAXFLAVOR) 351da177e4SLinus Torvalds return RPC_AUTH_GSS; 361da177e4SLinus Torvalds return flavor; 371da177e4SLinus Torvalds } 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds int 40f1c0a861STrond Myklebust rpcauth_register(const struct rpc_authops *ops) 411da177e4SLinus Torvalds { 421da177e4SLinus Torvalds rpc_authflavor_t flavor; 43fc1b356fSTrond Myklebust int ret = -EPERM; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) 461da177e4SLinus Torvalds return -EINVAL; 47fc1b356fSTrond Myklebust spin_lock(&rpc_authflavor_lock); 48fc1b356fSTrond Myklebust if (auth_flavors[flavor] == NULL) { 491da177e4SLinus Torvalds auth_flavors[flavor] = ops; 50fc1b356fSTrond Myklebust ret = 0; 51fc1b356fSTrond Myklebust } 52fc1b356fSTrond Myklebust spin_unlock(&rpc_authflavor_lock); 53fc1b356fSTrond Myklebust return ret; 541da177e4SLinus Torvalds } 55e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_register); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds int 58f1c0a861STrond Myklebust rpcauth_unregister(const struct rpc_authops *ops) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds rpc_authflavor_t flavor; 61fc1b356fSTrond Myklebust int ret = -EPERM; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) 641da177e4SLinus Torvalds return -EINVAL; 65fc1b356fSTrond Myklebust spin_lock(&rpc_authflavor_lock); 66fc1b356fSTrond Myklebust if (auth_flavors[flavor] == ops) { 671da177e4SLinus Torvalds auth_flavors[flavor] = NULL; 68fc1b356fSTrond Myklebust ret = 0; 69fc1b356fSTrond Myklebust } 70fc1b356fSTrond Myklebust spin_unlock(&rpc_authflavor_lock); 71fc1b356fSTrond Myklebust return ret; 721da177e4SLinus Torvalds } 73e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_unregister); 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds struct rpc_auth * 761da177e4SLinus Torvalds rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds struct rpc_auth *auth; 79f1c0a861STrond Myklebust const struct rpc_authops *ops; 801da177e4SLinus Torvalds u32 flavor = pseudoflavor_to_flavor(pseudoflavor); 811da177e4SLinus Torvalds 82f344f6dfSOlaf Kirch auth = ERR_PTR(-EINVAL); 83f344f6dfSOlaf Kirch if (flavor >= RPC_AUTH_MAXFLAVOR) 84f344f6dfSOlaf Kirch goto out; 85f344f6dfSOlaf Kirch 86f344f6dfSOlaf Kirch if ((ops = auth_flavors[flavor]) == NULL) 87f344f6dfSOlaf Kirch request_module("rpc-auth-%u", flavor); 88fc1b356fSTrond Myklebust spin_lock(&rpc_authflavor_lock); 89fc1b356fSTrond Myklebust ops = auth_flavors[flavor]; 90fc1b356fSTrond Myklebust if (ops == NULL || !try_module_get(ops->owner)) { 91fc1b356fSTrond Myklebust spin_unlock(&rpc_authflavor_lock); 92f344f6dfSOlaf Kirch goto out; 93fc1b356fSTrond Myklebust } 94fc1b356fSTrond Myklebust spin_unlock(&rpc_authflavor_lock); 951da177e4SLinus Torvalds auth = ops->create(clnt, pseudoflavor); 96fc1b356fSTrond Myklebust module_put(ops->owner); 976a19275aSJ. Bruce Fields if (IS_ERR(auth)) 986a19275aSJ. Bruce Fields return auth; 991da177e4SLinus Torvalds if (clnt->cl_auth) 100de7a8ce3STrond Myklebust rpcauth_release(clnt->cl_auth); 1011da177e4SLinus Torvalds clnt->cl_auth = auth; 102f344f6dfSOlaf Kirch 103f344f6dfSOlaf Kirch out: 1041da177e4SLinus Torvalds return auth; 1051da177e4SLinus Torvalds } 106e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_create); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds void 109de7a8ce3STrond Myklebust rpcauth_release(struct rpc_auth *auth) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds if (!atomic_dec_and_test(&auth->au_count)) 1121da177e4SLinus Torvalds return; 1131da177e4SLinus Torvalds auth->au_ops->destroy(auth); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_credcache_lock); 1171da177e4SLinus Torvalds 11831be5bf1STrond Myklebust static void 11931be5bf1STrond Myklebust rpcauth_unhash_cred_locked(struct rpc_cred *cred) 12031be5bf1STrond Myklebust { 12131be5bf1STrond Myklebust hlist_del_rcu(&cred->cr_hash); 12231be5bf1STrond Myklebust smp_mb__before_clear_bit(); 12331be5bf1STrond Myklebust clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); 12431be5bf1STrond Myklebust } 12531be5bf1STrond Myklebust 1269499b434STrond Myklebust static void 1279499b434STrond Myklebust rpcauth_unhash_cred(struct rpc_cred *cred) 1289499b434STrond Myklebust { 1299499b434STrond Myklebust spinlock_t *cache_lock; 1309499b434STrond Myklebust 1319499b434STrond Myklebust cache_lock = &cred->cr_auth->au_credcache->lock; 1329499b434STrond Myklebust spin_lock(cache_lock); 1339499b434STrond Myklebust if (atomic_read(&cred->cr_count) == 0) 1349499b434STrond Myklebust rpcauth_unhash_cred_locked(cred); 1359499b434STrond Myklebust spin_unlock(cache_lock); 1369499b434STrond Myklebust } 1379499b434STrond Myklebust 1381da177e4SLinus Torvalds /* 1391da177e4SLinus Torvalds * Initialize RPC credential cache 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds int 142f5c2187cSTrond Myklebust rpcauth_init_credcache(struct rpc_auth *auth) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds struct rpc_cred_cache *new; 1451da177e4SLinus Torvalds int i; 1461da177e4SLinus Torvalds 1478b3a7005SKris Katterjohn new = kmalloc(sizeof(*new), GFP_KERNEL); 1481da177e4SLinus Torvalds if (!new) 1491da177e4SLinus Torvalds return -ENOMEM; 1501da177e4SLinus Torvalds for (i = 0; i < RPC_CREDCACHE_NR; i++) 1511da177e4SLinus Torvalds INIT_HLIST_HEAD(&new->hashtable[i]); 1529499b434STrond Myklebust spin_lock_init(&new->lock); 1531da177e4SLinus Torvalds auth->au_credcache = new; 1541da177e4SLinus Torvalds return 0; 1551da177e4SLinus Torvalds } 156e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_credcache); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * Destroy a list of credentials 1601da177e4SLinus Torvalds */ 1611da177e4SLinus Torvalds static inline 162e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds struct rpc_cred *cred; 1651da177e4SLinus Torvalds 166e092bdcdSTrond Myklebust while (!list_empty(head)) { 167e092bdcdSTrond Myklebust cred = list_entry(head->next, struct rpc_cred, cr_lru); 168e092bdcdSTrond Myklebust list_del_init(&cred->cr_lru); 1691da177e4SLinus Torvalds put_rpccred(cred); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* 1741da177e4SLinus Torvalds * Clear the RPC credential cache, and delete those credentials 1751da177e4SLinus Torvalds * that are not referenced. 1761da177e4SLinus Torvalds */ 1771da177e4SLinus Torvalds void 1783ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache) 1791da177e4SLinus Torvalds { 180e092bdcdSTrond Myklebust LIST_HEAD(free); 181e092bdcdSTrond Myklebust struct hlist_head *head; 1821da177e4SLinus Torvalds struct rpc_cred *cred; 1831da177e4SLinus Torvalds int i; 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds spin_lock(&rpc_credcache_lock); 1869499b434STrond Myklebust spin_lock(&cache->lock); 1871da177e4SLinus Torvalds for (i = 0; i < RPC_CREDCACHE_NR; i++) { 188e092bdcdSTrond Myklebust head = &cache->hashtable[i]; 189e092bdcdSTrond Myklebust while (!hlist_empty(head)) { 190e092bdcdSTrond Myklebust cred = hlist_entry(head->first, struct rpc_cred, cr_hash); 191e092bdcdSTrond Myklebust get_rpccred(cred); 192f5c2187cSTrond Myklebust if (!list_empty(&cred->cr_lru)) { 193f5c2187cSTrond Myklebust list_del(&cred->cr_lru); 194f5c2187cSTrond Myklebust number_cred_unused--; 195f5c2187cSTrond Myklebust } 196f5c2187cSTrond Myklebust list_add_tail(&cred->cr_lru, &free); 19731be5bf1STrond Myklebust rpcauth_unhash_cred_locked(cred); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds } 2009499b434STrond Myklebust spin_unlock(&cache->lock); 2011da177e4SLinus Torvalds spin_unlock(&rpc_credcache_lock); 2021da177e4SLinus Torvalds rpcauth_destroy_credlist(&free); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2053ab9bb72STrond Myklebust /* 2063ab9bb72STrond Myklebust * Destroy the RPC credential cache 2073ab9bb72STrond Myklebust */ 2083ab9bb72STrond Myklebust void 2093ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth) 2103ab9bb72STrond Myklebust { 2113ab9bb72STrond Myklebust struct rpc_cred_cache *cache = auth->au_credcache; 2123ab9bb72STrond Myklebust 2133ab9bb72STrond Myklebust if (cache) { 2143ab9bb72STrond Myklebust auth->au_credcache = NULL; 2153ab9bb72STrond Myklebust rpcauth_clear_credcache(cache); 2163ab9bb72STrond Myklebust kfree(cache); 2173ab9bb72STrond Myklebust } 2183ab9bb72STrond Myklebust } 219e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache); 2203ab9bb72STrond Myklebust 221d2b83141STrond Myklebust 222d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ) 223d2b83141STrond Myklebust 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds * Remove stale credentials. Avoid sleeping inside the loop. 2261da177e4SLinus Torvalds */ 227f5c2187cSTrond Myklebust static int 228f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan) 2291da177e4SLinus Torvalds { 2309499b434STrond Myklebust spinlock_t *cache_lock; 231*eac0d18dSTrond Myklebust struct rpc_cred *cred, *next; 232d2b83141STrond Myklebust unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM; 2331da177e4SLinus Torvalds 234*eac0d18dSTrond Myklebust list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) { 235*eac0d18dSTrond Myklebust 236*eac0d18dSTrond Myklebust /* Enforce a 60 second garbage collection moratorium */ 237*eac0d18dSTrond Myklebust if (time_in_range(cred->cr_expire, expired, jiffies) && 238*eac0d18dSTrond Myklebust test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) 239*eac0d18dSTrond Myklebust continue; 240*eac0d18dSTrond Myklebust 241e092bdcdSTrond Myklebust list_del_init(&cred->cr_lru); 242f5c2187cSTrond Myklebust number_cred_unused--; 243e092bdcdSTrond Myklebust if (atomic_read(&cred->cr_count) != 0) 244e092bdcdSTrond Myklebust continue; 245*eac0d18dSTrond Myklebust 2469499b434STrond Myklebust cache_lock = &cred->cr_auth->au_credcache->lock; 2479499b434STrond Myklebust spin_lock(cache_lock); 2489499b434STrond Myklebust if (atomic_read(&cred->cr_count) == 0) { 249e092bdcdSTrond Myklebust get_rpccred(cred); 250e092bdcdSTrond Myklebust list_add_tail(&cred->cr_lru, free); 25131be5bf1STrond Myklebust rpcauth_unhash_cred_locked(cred); 252f5c2187cSTrond Myklebust nr_to_scan--; 2531da177e4SLinus Torvalds } 2549499b434STrond Myklebust spin_unlock(cache_lock); 255f5c2187cSTrond Myklebust if (nr_to_scan == 0) 256f5c2187cSTrond Myklebust break; 2579499b434STrond Myklebust } 258f5c2187cSTrond Myklebust return nr_to_scan; 2591da177e4SLinus Torvalds } 260e092bdcdSTrond Myklebust 261e092bdcdSTrond Myklebust /* 262f5c2187cSTrond Myklebust * Run memory cache shrinker. 263e092bdcdSTrond Myklebust */ 264f5c2187cSTrond Myklebust static int 265f5c2187cSTrond Myklebust rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask) 266e092bdcdSTrond Myklebust { 267f5c2187cSTrond Myklebust LIST_HEAD(free); 268f5c2187cSTrond Myklebust int res; 269f5c2187cSTrond Myklebust 270f5c2187cSTrond Myklebust if (list_empty(&cred_unused)) 271f5c2187cSTrond Myklebust return 0; 27231be5bf1STrond Myklebust spin_lock(&rpc_credcache_lock); 273f5c2187cSTrond Myklebust nr_to_scan = rpcauth_prune_expired(&free, nr_to_scan); 274f5c2187cSTrond Myklebust res = (number_cred_unused / 100) * sysctl_vfs_cache_pressure; 27531be5bf1STrond Myklebust spin_unlock(&rpc_credcache_lock); 276f5c2187cSTrond Myklebust rpcauth_destroy_credlist(&free); 277f5c2187cSTrond Myklebust return res; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* 2811da177e4SLinus Torvalds * Look up a process' credentials in the authentication cache 2821da177e4SLinus Torvalds */ 2831da177e4SLinus Torvalds struct rpc_cred * 2841da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, 2858a317760STrond Myklebust int flags) 2861da177e4SLinus Torvalds { 287e092bdcdSTrond Myklebust LIST_HEAD(free); 2881da177e4SLinus Torvalds struct rpc_cred_cache *cache = auth->au_credcache; 289e092bdcdSTrond Myklebust struct hlist_node *pos; 29031be5bf1STrond Myklebust struct rpc_cred *cred = NULL, 29131be5bf1STrond Myklebust *entry, *new; 29225337fdcSTrond Myklebust unsigned int nr; 29325337fdcSTrond Myklebust 29425337fdcSTrond Myklebust nr = hash_long(acred->uid, RPC_CREDCACHE_HASHBITS); 2951da177e4SLinus Torvalds 29631be5bf1STrond Myklebust rcu_read_lock(); 29731be5bf1STrond Myklebust hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { 29831be5bf1STrond Myklebust if (!entry->cr_ops->crmatch(acred, entry, flags)) 29931be5bf1STrond Myklebust continue; 3009499b434STrond Myklebust spin_lock(&cache->lock); 30131be5bf1STrond Myklebust if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) { 3029499b434STrond Myklebust spin_unlock(&cache->lock); 30331be5bf1STrond Myklebust continue; 30431be5bf1STrond Myklebust } 30531be5bf1STrond Myklebust cred = get_rpccred(entry); 3069499b434STrond Myklebust spin_unlock(&cache->lock); 30731be5bf1STrond Myklebust break; 30831be5bf1STrond Myklebust } 30931be5bf1STrond Myklebust rcu_read_unlock(); 31031be5bf1STrond Myklebust 3119499b434STrond Myklebust if (cred != NULL) 31231be5bf1STrond Myklebust goto found; 31331be5bf1STrond Myklebust 31431be5bf1STrond Myklebust new = auth->au_ops->crcreate(auth, acred, flags); 31531be5bf1STrond Myklebust if (IS_ERR(new)) { 31631be5bf1STrond Myklebust cred = new; 31731be5bf1STrond Myklebust goto out; 31831be5bf1STrond Myklebust } 31931be5bf1STrond Myklebust 3209499b434STrond Myklebust spin_lock(&cache->lock); 321e092bdcdSTrond Myklebust hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) { 322e092bdcdSTrond Myklebust if (!entry->cr_ops->crmatch(acred, entry, flags)) 323e092bdcdSTrond Myklebust continue; 324e092bdcdSTrond Myklebust cred = get_rpccred(entry); 3251da177e4SLinus Torvalds break; 3261da177e4SLinus Torvalds } 32731be5bf1STrond Myklebust if (cred == NULL) { 32831be5bf1STrond Myklebust cred = new; 32931be5bf1STrond Myklebust set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); 33031be5bf1STrond Myklebust hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); 33131be5bf1STrond Myklebust } else 332e092bdcdSTrond Myklebust list_add_tail(&new->cr_lru, &free); 3339499b434STrond Myklebust spin_unlock(&cache->lock); 33431be5bf1STrond Myklebust found: 33531be5bf1STrond Myklebust if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) 336fba3bad4STrond Myklebust && cred->cr_ops->cr_init != NULL 337fba3bad4STrond Myklebust && !(flags & RPCAUTH_LOOKUP_NEW)) { 338fba3bad4STrond Myklebust int res = cred->cr_ops->cr_init(auth, cred); 339fba3bad4STrond Myklebust if (res < 0) { 340fba3bad4STrond Myklebust put_rpccred(cred); 341fba3bad4STrond Myklebust cred = ERR_PTR(res); 342fba3bad4STrond Myklebust } 3431da177e4SLinus Torvalds } 34431be5bf1STrond Myklebust rpcauth_destroy_credlist(&free); 34531be5bf1STrond Myklebust out: 34631be5bf1STrond Myklebust return cred; 3471da177e4SLinus Torvalds } 348e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds struct rpc_cred * 3518a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags) 3521da177e4SLinus Torvalds { 3531da177e4SLinus Torvalds struct auth_cred acred = { 3541da177e4SLinus Torvalds .uid = current->fsuid, 3551da177e4SLinus Torvalds .gid = current->fsgid, 3561da177e4SLinus Torvalds .group_info = current->group_info, 3571da177e4SLinus Torvalds }; 3581da177e4SLinus Torvalds struct rpc_cred *ret; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds dprintk("RPC: looking up %s cred\n", 3611da177e4SLinus Torvalds auth->au_ops->au_name); 3621da177e4SLinus Torvalds get_group_info(acred.group_info); 3638a317760STrond Myklebust ret = auth->au_ops->lookup_cred(auth, &acred, flags); 3641da177e4SLinus Torvalds put_group_info(acred.group_info); 3651da177e4SLinus Torvalds return ret; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3685fe4755eSTrond Myklebust void 3695fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, 3705fe4755eSTrond Myklebust struct rpc_auth *auth, const struct rpc_credops *ops) 3715fe4755eSTrond Myklebust { 3725fe4755eSTrond Myklebust INIT_HLIST_NODE(&cred->cr_hash); 373e092bdcdSTrond Myklebust INIT_LIST_HEAD(&cred->cr_lru); 3745fe4755eSTrond Myklebust atomic_set(&cred->cr_count, 1); 3755fe4755eSTrond Myklebust cred->cr_auth = auth; 3765fe4755eSTrond Myklebust cred->cr_ops = ops; 3775fe4755eSTrond Myklebust cred->cr_expire = jiffies; 3785fe4755eSTrond Myklebust #ifdef RPC_DEBUG 3795fe4755eSTrond Myklebust cred->cr_magic = RPCAUTH_CRED_MAGIC; 3805fe4755eSTrond Myklebust #endif 3815fe4755eSTrond Myklebust cred->cr_uid = acred->uid; 3825fe4755eSTrond Myklebust } 383e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred); 3845fe4755eSTrond Myklebust 3855c691044STrond Myklebust void 3864ccda2cdSTrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred) 3874ccda2cdSTrond Myklebust { 3884ccda2cdSTrond Myklebust task->tk_msg.rpc_cred = get_rpccred(cred); 3894ccda2cdSTrond Myklebust dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, 3904ccda2cdSTrond Myklebust cred->cr_auth->au_ops->au_name, cred); 3914ccda2cdSTrond Myklebust } 3925c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); 3934ccda2cdSTrond Myklebust 3944ccda2cdSTrond Myklebust static void 395af093835STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task) 3961da177e4SLinus Torvalds { 3971be27f36STrond Myklebust struct rpc_auth *auth = task->tk_client->cl_auth; 3981da177e4SLinus Torvalds struct auth_cred acred = { 399af093835STrond Myklebust .uid = 0, 400af093835STrond Myklebust .gid = 0, 4011da177e4SLinus Torvalds }; 4021da177e4SLinus Torvalds struct rpc_cred *ret; 4031da177e4SLinus Torvalds 40446121cf7SChuck Lever dprintk("RPC: %5u looking up %s cred\n", 4051be27f36STrond Myklebust task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); 406af093835STrond Myklebust ret = auth->au_ops->lookup_cred(auth, &acred, 0); 4071da177e4SLinus Torvalds if (!IS_ERR(ret)) 4081da177e4SLinus Torvalds task->tk_msg.rpc_cred = ret; 4091da177e4SLinus Torvalds else 4101da177e4SLinus Torvalds task->tk_status = PTR_ERR(ret); 411af093835STrond Myklebust } 412af093835STrond Myklebust 4134ccda2cdSTrond Myklebust static void 4144ccda2cdSTrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task) 415af093835STrond Myklebust { 416af093835STrond Myklebust struct rpc_auth *auth = task->tk_client->cl_auth; 417af093835STrond Myklebust struct rpc_cred *ret; 418af093835STrond Myklebust 419af093835STrond Myklebust dprintk("RPC: %5u looking up %s cred\n", 420af093835STrond Myklebust task->tk_pid, auth->au_ops->au_name); 421af093835STrond Myklebust ret = rpcauth_lookupcred(auth, 0); 422af093835STrond Myklebust if (!IS_ERR(ret)) 423af093835STrond Myklebust task->tk_msg.rpc_cred = ret; 424af093835STrond Myklebust else 425af093835STrond Myklebust task->tk_status = PTR_ERR(ret); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds void 4294ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) 4301da177e4SLinus Torvalds { 4314ccda2cdSTrond Myklebust if (cred != NULL) 4325c691044STrond Myklebust cred->cr_ops->crbind(task, cred); 4334ccda2cdSTrond Myklebust else if (flags & RPC_TASK_ROOTCREDS) 4344ccda2cdSTrond Myklebust rpcauth_bind_root_cred(task); 4354ccda2cdSTrond Myklebust else 4364ccda2cdSTrond Myklebust rpcauth_bind_new_cred(task); 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds void 4401da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred) 4411da177e4SLinus Torvalds { 442e092bdcdSTrond Myklebust /* Fast path for unhashed credentials */ 44331be5bf1STrond Myklebust if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) 444e092bdcdSTrond Myklebust goto need_lock; 445e092bdcdSTrond Myklebust 4461da177e4SLinus Torvalds if (!atomic_dec_and_test(&cred->cr_count)) 4471da177e4SLinus Torvalds return; 448e092bdcdSTrond Myklebust goto out_destroy; 449e092bdcdSTrond Myklebust need_lock: 450e092bdcdSTrond Myklebust if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock)) 451e092bdcdSTrond Myklebust return; 452f5c2187cSTrond Myklebust if (!list_empty(&cred->cr_lru)) { 453f5c2187cSTrond Myklebust number_cred_unused--; 454e092bdcdSTrond Myklebust list_del_init(&cred->cr_lru); 455f5c2187cSTrond Myklebust } 456e092bdcdSTrond Myklebust if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0) 4579499b434STrond Myklebust rpcauth_unhash_cred(cred); 45831be5bf1STrond Myklebust else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) { 459e092bdcdSTrond Myklebust cred->cr_expire = jiffies; 460e092bdcdSTrond Myklebust list_add_tail(&cred->cr_lru, &cred_unused); 461f5c2187cSTrond Myklebust number_cred_unused++; 462e092bdcdSTrond Myklebust spin_unlock(&rpc_credcache_lock); 463e092bdcdSTrond Myklebust return; 464e092bdcdSTrond Myklebust } 465e092bdcdSTrond Myklebust spin_unlock(&rpc_credcache_lock); 466e092bdcdSTrond Myklebust out_destroy: 4671da177e4SLinus Torvalds cred->cr_ops->crdestroy(cred); 4681da177e4SLinus Torvalds } 469e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds void 4721da177e4SLinus Torvalds rpcauth_unbindcred(struct rpc_task *task) 4731da177e4SLinus Torvalds { 4741da177e4SLinus Torvalds struct rpc_cred *cred = task->tk_msg.rpc_cred; 4751da177e4SLinus Torvalds 47646121cf7SChuck Lever dprintk("RPC: %5u releasing %s cred %p\n", 4771be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds put_rpccred(cred); 4801da177e4SLinus Torvalds task->tk_msg.rpc_cred = NULL; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 483d8ed029dSAlexey Dobriyan __be32 * 484d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p) 4851da177e4SLinus Torvalds { 4861da177e4SLinus Torvalds struct rpc_cred *cred = task->tk_msg.rpc_cred; 4871da177e4SLinus Torvalds 48846121cf7SChuck Lever dprintk("RPC: %5u marshaling %s cred %p\n", 4891be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 4900bbacc40SChuck Lever 4911da177e4SLinus Torvalds return cred->cr_ops->crmarshal(task, p); 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494d8ed029dSAlexey Dobriyan __be32 * 495d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p) 4961da177e4SLinus Torvalds { 4971da177e4SLinus Torvalds struct rpc_cred *cred = task->tk_msg.rpc_cred; 4981da177e4SLinus Torvalds 49946121cf7SChuck Lever dprintk("RPC: %5u validating %s cred %p\n", 5001be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 5010bbacc40SChuck Lever 5021da177e4SLinus Torvalds return cred->cr_ops->crvalidate(task, p); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds int 5061da177e4SLinus Torvalds rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, 507d8ed029dSAlexey Dobriyan __be32 *data, void *obj) 5081da177e4SLinus Torvalds { 5091da177e4SLinus Torvalds struct rpc_cred *cred = task->tk_msg.rpc_cred; 5101da177e4SLinus Torvalds 51146121cf7SChuck Lever dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", 5121da177e4SLinus Torvalds task->tk_pid, cred->cr_ops->cr_name, cred); 5131da177e4SLinus Torvalds if (cred->cr_ops->crwrap_req) 5141da177e4SLinus Torvalds return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); 5151da177e4SLinus Torvalds /* By default, we encode the arguments normally. */ 516be879c4eSJ. Bruce Fields return rpc_call_xdrproc(encode, rqstp, data, obj); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds int 5201da177e4SLinus Torvalds rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, 521d8ed029dSAlexey Dobriyan __be32 *data, void *obj) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds struct rpc_cred *cred = task->tk_msg.rpc_cred; 5241da177e4SLinus Torvalds 52546121cf7SChuck Lever dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", 5261da177e4SLinus Torvalds task->tk_pid, cred->cr_ops->cr_name, cred); 5271da177e4SLinus Torvalds if (cred->cr_ops->crunwrap_resp) 5281da177e4SLinus Torvalds return cred->cr_ops->crunwrap_resp(task, decode, rqstp, 5291da177e4SLinus Torvalds data, obj); 5301da177e4SLinus Torvalds /* By default, we decode the arguments normally. */ 531be879c4eSJ. Bruce Fields return rpc_call_xdrproc(decode, rqstp, data, obj); 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds int 5351da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task) 5361da177e4SLinus Torvalds { 5371da177e4SLinus Torvalds struct rpc_cred *cred = task->tk_msg.rpc_cred; 5381da177e4SLinus Torvalds int err; 5391da177e4SLinus Torvalds 54046121cf7SChuck Lever dprintk("RPC: %5u refreshing %s cred %p\n", 5411be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 5420bbacc40SChuck Lever 5431da177e4SLinus Torvalds err = cred->cr_ops->crrefresh(task); 5441da177e4SLinus Torvalds if (err < 0) 5451da177e4SLinus Torvalds task->tk_status = err; 5461da177e4SLinus Torvalds return err; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds void 5501da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task) 5511da177e4SLinus Torvalds { 552fc432dd9STrond Myklebust struct rpc_cred *cred = task->tk_msg.rpc_cred; 553fc432dd9STrond Myklebust 55446121cf7SChuck Lever dprintk("RPC: %5u invalidating %s cred %p\n", 5551be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 556fc432dd9STrond Myklebust if (cred) 557fc432dd9STrond Myklebust clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds int 5611da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task) 5621da177e4SLinus Torvalds { 563fc432dd9STrond Myklebust struct rpc_cred *cred = task->tk_msg.rpc_cred; 564fc432dd9STrond Myklebust 565fc432dd9STrond Myklebust return cred == NULL || 566fc432dd9STrond Myklebust test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; 5671da177e4SLinus Torvalds } 568f5c2187cSTrond Myklebust 5698e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = { 5708e1f936bSRusty Russell .shrink = rpcauth_cache_shrinker, 5718e1f936bSRusty Russell .seeks = DEFAULT_SEEKS, 5728e1f936bSRusty Russell }; 573f5c2187cSTrond Myklebust 574f5c2187cSTrond Myklebust void __init rpcauth_init_module(void) 575f5c2187cSTrond Myklebust { 576f5c2187cSTrond Myklebust rpc_init_authunix(); 5779a559efdSTrond Myklebust rpc_init_generic_auth(); 5788e1f936bSRusty Russell register_shrinker(&rpc_cred_shrinker); 579f5c2187cSTrond Myklebust } 580f5c2187cSTrond Myklebust 581f5c2187cSTrond Myklebust void __exit rpcauth_remove_module(void) 582f5c2187cSTrond Myklebust { 5838e1f936bSRusty Russell unregister_shrinker(&rpc_cred_shrinker); 584f5c2187cSTrond Myklebust } 585