Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/auth.c |
| 3 | * |
| 4 | * Generic RPC client authentication API. |
| 5 | * |
| 6 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/sunrpc/clnt.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | |
| 17 | #ifdef RPC_DEBUG |
| 18 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 19 | #endif |
| 20 | |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 21 | static DEFINE_SPINLOCK(rpc_authflavor_lock); |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 22 | static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | &authnull_ops, /* AUTH_NULL */ |
| 24 | &authunix_ops, /* AUTH_UNIX */ |
| 25 | NULL, /* others can be loadable modules */ |
| 26 | }; |
| 27 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 28 | static LIST_HEAD(cred_unused); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static u32 |
| 31 | pseudoflavor_to_flavor(u32 flavor) { |
| 32 | if (flavor >= RPC_AUTH_MAXFLAVOR) |
| 33 | return RPC_AUTH_GSS; |
| 34 | return flavor; |
| 35 | } |
| 36 | |
| 37 | int |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 38 | rpcauth_register(const struct rpc_authops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | rpc_authflavor_t flavor; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 41 | int ret = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) |
| 44 | return -EINVAL; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 45 | spin_lock(&rpc_authflavor_lock); |
| 46 | if (auth_flavors[flavor] == NULL) { |
| 47 | auth_flavors[flavor] = ops; |
| 48 | ret = 0; |
| 49 | } |
| 50 | spin_unlock(&rpc_authflavor_lock); |
| 51 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 55 | rpcauth_unregister(const struct rpc_authops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | rpc_authflavor_t flavor; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 58 | int ret = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) |
| 61 | return -EINVAL; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 62 | spin_lock(&rpc_authflavor_lock); |
| 63 | if (auth_flavors[flavor] == ops) { |
| 64 | auth_flavors[flavor] = NULL; |
| 65 | ret = 0; |
| 66 | } |
| 67 | spin_unlock(&rpc_authflavor_lock); |
| 68 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | struct rpc_auth * |
| 72 | rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt) |
| 73 | { |
| 74 | struct rpc_auth *auth; |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 75 | const struct rpc_authops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | u32 flavor = pseudoflavor_to_flavor(pseudoflavor); |
| 77 | |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 78 | auth = ERR_PTR(-EINVAL); |
| 79 | if (flavor >= RPC_AUTH_MAXFLAVOR) |
| 80 | goto out; |
| 81 | |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 82 | #ifdef CONFIG_KMOD |
| 83 | if ((ops = auth_flavors[flavor]) == NULL) |
| 84 | request_module("rpc-auth-%u", flavor); |
| 85 | #endif |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 86 | spin_lock(&rpc_authflavor_lock); |
| 87 | ops = auth_flavors[flavor]; |
| 88 | if (ops == NULL || !try_module_get(ops->owner)) { |
| 89 | spin_unlock(&rpc_authflavor_lock); |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 90 | goto out; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 91 | } |
| 92 | spin_unlock(&rpc_authflavor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | auth = ops->create(clnt, pseudoflavor); |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 94 | module_put(ops->owner); |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 95 | if (IS_ERR(auth)) |
| 96 | return auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (clnt->cl_auth) |
Trond Myklebust | de7a8ce | 2007-06-23 10:46:47 -0400 | [diff] [blame] | 98 | rpcauth_release(clnt->cl_auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | clnt->cl_auth = auth; |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 100 | |
| 101 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return auth; |
| 103 | } |
| 104 | |
| 105 | void |
Trond Myklebust | de7a8ce | 2007-06-23 10:46:47 -0400 | [diff] [blame] | 106 | rpcauth_release(struct rpc_auth *auth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | if (!atomic_dec_and_test(&auth->au_count)) |
| 109 | return; |
| 110 | auth->au_ops->destroy(auth); |
| 111 | } |
| 112 | |
| 113 | static DEFINE_SPINLOCK(rpc_credcache_lock); |
| 114 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 115 | static void |
| 116 | rpcauth_unhash_cred_locked(struct rpc_cred *cred) |
| 117 | { |
| 118 | hlist_del_rcu(&cred->cr_hash); |
| 119 | smp_mb__before_clear_bit(); |
| 120 | clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
| 121 | } |
| 122 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 123 | static void |
| 124 | rpcauth_unhash_cred(struct rpc_cred *cred) |
| 125 | { |
| 126 | spinlock_t *cache_lock; |
| 127 | |
| 128 | cache_lock = &cred->cr_auth->au_credcache->lock; |
| 129 | spin_lock(cache_lock); |
| 130 | if (atomic_read(&cred->cr_count) == 0) |
| 131 | rpcauth_unhash_cred_locked(cred); |
| 132 | spin_unlock(cache_lock); |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* |
| 136 | * Initialize RPC credential cache |
| 137 | */ |
| 138 | int |
| 139 | rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire) |
| 140 | { |
| 141 | struct rpc_cred_cache *new; |
| 142 | int i; |
| 143 | |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 144 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (!new) |
| 146 | return -ENOMEM; |
| 147 | for (i = 0; i < RPC_CREDCACHE_NR; i++) |
| 148 | INIT_HLIST_HEAD(&new->hashtable[i]); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 149 | spin_lock_init(&new->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | new->expire = expire; |
| 151 | new->nextgc = jiffies + (expire >> 1); |
| 152 | auth->au_credcache = new; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Destroy a list of credentials |
| 158 | */ |
| 159 | static inline |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 160 | void rpcauth_destroy_credlist(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct rpc_cred *cred; |
| 163 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 164 | while (!list_empty(head)) { |
| 165 | cred = list_entry(head->next, struct rpc_cred, cr_lru); |
| 166 | list_del_init(&cred->cr_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | put_rpccred(cred); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Clear the RPC credential cache, and delete those credentials |
| 173 | * that are not referenced. |
| 174 | */ |
| 175 | void |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 176 | rpcauth_clear_credcache(struct rpc_cred_cache *cache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 178 | LIST_HEAD(free); |
| 179 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | struct rpc_cred *cred; |
| 181 | int i; |
| 182 | |
| 183 | spin_lock(&rpc_credcache_lock); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 184 | spin_lock(&cache->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | for (i = 0; i < RPC_CREDCACHE_NR; i++) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 186 | head = &cache->hashtable[i]; |
| 187 | while (!hlist_empty(head)) { |
| 188 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); |
| 189 | get_rpccred(cred); |
| 190 | list_move_tail(&cred->cr_lru, &free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 191 | rpcauth_unhash_cred_locked(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 194 | spin_unlock(&cache->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | spin_unlock(&rpc_credcache_lock); |
| 196 | rpcauth_destroy_credlist(&free); |
| 197 | } |
| 198 | |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 199 | /* |
| 200 | * Destroy the RPC credential cache |
| 201 | */ |
| 202 | void |
| 203 | rpcauth_destroy_credcache(struct rpc_auth *auth) |
| 204 | { |
| 205 | struct rpc_cred_cache *cache = auth->au_credcache; |
| 206 | |
| 207 | if (cache) { |
| 208 | auth->au_credcache = NULL; |
| 209 | rpcauth_clear_credcache(cache); |
| 210 | kfree(cache); |
| 211 | } |
| 212 | } |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* |
| 215 | * Remove stale credentials. Avoid sleeping inside the loop. |
| 216 | */ |
| 217 | static void |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 218 | rpcauth_prune_expired(struct list_head *free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 220 | spinlock_t *cache_lock; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 221 | struct rpc_cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 223 | while (!list_empty(&cred_unused)) { |
| 224 | cred = list_entry(cred_unused.next, struct rpc_cred, cr_lru); |
| 225 | if (time_after(jiffies, cred->cr_expire + |
| 226 | cred->cr_auth->au_credcache->expire)) |
| 227 | break; |
| 228 | list_del_init(&cred->cr_lru); |
| 229 | if (atomic_read(&cred->cr_count) != 0) |
| 230 | continue; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 231 | cache_lock = &cred->cr_auth->au_credcache->lock; |
| 232 | spin_lock(cache_lock); |
| 233 | if (atomic_read(&cred->cr_count) == 0) { |
| 234 | get_rpccred(cred); |
| 235 | list_add_tail(&cred->cr_lru, free); |
| 236 | rpcauth_unhash_cred_locked(cred); |
| 237 | } |
| 238 | spin_unlock(cache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Run garbage collector. |
| 244 | */ |
| 245 | static void |
| 246 | rpcauth_gc_credcache(struct rpc_cred_cache *cache, struct list_head *free) |
| 247 | { |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 248 | if (list_empty(&cred_unused) || time_before(jiffies, cache->nextgc)) |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 249 | return; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 250 | spin_lock(&rpc_credcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | cache->nextgc = jiffies + cache->expire; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 252 | rpcauth_prune_expired(free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 253 | spin_unlock(&rpc_credcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Look up a process' credentials in the authentication cache |
| 258 | */ |
| 259 | struct rpc_cred * |
| 260 | rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 261 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 263 | LIST_HEAD(free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | struct rpc_cred_cache *cache = auth->au_credcache; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 265 | struct hlist_node *pos; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 266 | struct rpc_cred *cred = NULL, |
| 267 | *entry, *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | int nr = 0; |
| 269 | |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 270 | if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | nr = acred->uid & RPC_CREDCACHE_MASK; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 272 | |
| 273 | rcu_read_lock(); |
| 274 | hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { |
| 275 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 276 | continue; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 277 | spin_lock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 278 | if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) { |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 279 | spin_unlock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 280 | continue; |
| 281 | } |
| 282 | cred = get_rpccred(entry); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 283 | spin_unlock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | rcu_read_unlock(); |
| 287 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 288 | if (cred != NULL) |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 289 | goto found; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 290 | |
| 291 | new = auth->au_ops->crcreate(auth, acred, flags); |
| 292 | if (IS_ERR(new)) { |
| 293 | cred = new; |
| 294 | goto out; |
| 295 | } |
| 296 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 297 | spin_lock(&cache->lock); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 298 | hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) { |
| 299 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 300 | continue; |
| 301 | cred = get_rpccred(entry); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 302 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 304 | if (cred == NULL) { |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 305 | cred = new; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 306 | set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
| 307 | hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); |
| 308 | } else |
| 309 | list_add_tail(&new->cr_lru, &free); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 310 | spin_unlock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 311 | found: |
| 312 | if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) |
Trond Myklebust | fba3bad | 2006-02-01 12:19:27 -0500 | [diff] [blame] | 313 | && cred->cr_ops->cr_init != NULL |
| 314 | && !(flags & RPCAUTH_LOOKUP_NEW)) { |
| 315 | int res = cred->cr_ops->cr_init(auth, cred); |
| 316 | if (res < 0) { |
| 317 | put_rpccred(cred); |
| 318 | cred = ERR_PTR(res); |
| 319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 321 | rpcauth_gc_credcache(cache, &free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 322 | rpcauth_destroy_credlist(&free); |
| 323 | out: |
| 324 | return cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | struct rpc_cred * |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 328 | rpcauth_lookupcred(struct rpc_auth *auth, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | struct auth_cred acred = { |
| 331 | .uid = current->fsuid, |
| 332 | .gid = current->fsgid, |
| 333 | .group_info = current->group_info, |
| 334 | }; |
| 335 | struct rpc_cred *ret; |
| 336 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 337 | dprintk("RPC: looking up %s cred\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | auth->au_ops->au_name); |
| 339 | get_group_info(acred.group_info); |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 340 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | put_group_info(acred.group_info); |
| 342 | return ret; |
| 343 | } |
| 344 | |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 345 | void |
| 346 | rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, |
| 347 | struct rpc_auth *auth, const struct rpc_credops *ops) |
| 348 | { |
| 349 | INIT_HLIST_NODE(&cred->cr_hash); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 350 | INIT_LIST_HEAD(&cred->cr_lru); |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 351 | atomic_set(&cred->cr_count, 1); |
| 352 | cred->cr_auth = auth; |
| 353 | cred->cr_ops = ops; |
| 354 | cred->cr_expire = jiffies; |
| 355 | #ifdef RPC_DEBUG |
| 356 | cred->cr_magic = RPCAUTH_CRED_MAGIC; |
| 357 | #endif |
| 358 | cred->cr_uid = acred->uid; |
| 359 | } |
| 360 | EXPORT_SYMBOL(rpcauth_init_cred); |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | struct rpc_cred * |
| 363 | rpcauth_bindcred(struct rpc_task *task) |
| 364 | { |
| 365 | struct rpc_auth *auth = task->tk_auth; |
| 366 | struct auth_cred acred = { |
| 367 | .uid = current->fsuid, |
| 368 | .gid = current->fsgid, |
| 369 | .group_info = current->group_info, |
| 370 | }; |
| 371 | struct rpc_cred *ret; |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 372 | int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 374 | dprintk("RPC: %5u looking up %s cred\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | task->tk_pid, task->tk_auth->au_ops->au_name); |
| 376 | get_group_info(acred.group_info); |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 377 | if (task->tk_flags & RPC_TASK_ROOTCREDS) |
| 378 | flags |= RPCAUTH_LOOKUP_ROOTCREDS; |
| 379 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (!IS_ERR(ret)) |
| 381 | task->tk_msg.rpc_cred = ret; |
| 382 | else |
| 383 | task->tk_status = PTR_ERR(ret); |
| 384 | put_group_info(acred.group_info); |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | void |
| 389 | rpcauth_holdcred(struct rpc_task *task) |
| 390 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 391 | dprintk("RPC: %5u holding %s cred %p\n", |
| 392 | task->tk_pid, task->tk_auth->au_ops->au_name, |
| 393 | task->tk_msg.rpc_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | if (task->tk_msg.rpc_cred) |
| 395 | get_rpccred(task->tk_msg.rpc_cred); |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | put_rpccred(struct rpc_cred *cred) |
| 400 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 401 | /* Fast path for unhashed credentials */ |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 402 | if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 403 | goto need_lock; |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (!atomic_dec_and_test(&cred->cr_count)) |
| 406 | return; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 407 | goto out_destroy; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 408 | need_lock: |
| 409 | if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock)) |
| 410 | return; |
| 411 | if (!list_empty(&cred->cr_lru)) |
| 412 | list_del_init(&cred->cr_lru); |
| 413 | if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0) |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame^] | 414 | rpcauth_unhash_cred(cred); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 415 | else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 416 | cred->cr_expire = jiffies; |
| 417 | list_add_tail(&cred->cr_lru, &cred_unused); |
| 418 | spin_unlock(&rpc_credcache_lock); |
| 419 | return; |
| 420 | } |
| 421 | spin_unlock(&rpc_credcache_lock); |
| 422 | out_destroy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | cred->cr_ops->crdestroy(cred); |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | rpcauth_unbindcred(struct rpc_task *task) |
| 428 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 430 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 431 | dprintk("RPC: %5u releasing %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 432 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | put_rpccred(cred); |
| 435 | task->tk_msg.rpc_cred = NULL; |
| 436 | } |
| 437 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 438 | __be32 * |
| 439 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 442 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 443 | dprintk("RPC: %5u marshaling %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 444 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | return cred->cr_ops->crmarshal(task, p); |
| 447 | } |
| 448 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 449 | __be32 * |
| 450 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 453 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 454 | dprintk("RPC: %5u validating %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 455 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return cred->cr_ops->crvalidate(task, p); |
| 458 | } |
| 459 | |
| 460 | int |
| 461 | rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 462 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
| 464 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 465 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 466 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 468 | if (cred->cr_ops->crwrap_req) |
| 469 | return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); |
| 470 | /* By default, we encode the arguments normally. */ |
| 471 | return encode(rqstp, data, obj); |
| 472 | } |
| 473 | |
| 474 | int |
| 475 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 476 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 479 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 480 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 482 | if (cred->cr_ops->crunwrap_resp) |
| 483 | return cred->cr_ops->crunwrap_resp(task, decode, rqstp, |
| 484 | data, obj); |
| 485 | /* By default, we decode the arguments normally. */ |
| 486 | return decode(rqstp, data, obj); |
| 487 | } |
| 488 | |
| 489 | int |
| 490 | rpcauth_refreshcred(struct rpc_task *task) |
| 491 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 493 | int err; |
| 494 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 495 | dprintk("RPC: %5u refreshing %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 496 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | err = cred->cr_ops->crrefresh(task); |
| 499 | if (err < 0) |
| 500 | task->tk_status = err; |
| 501 | return err; |
| 502 | } |
| 503 | |
| 504 | void |
| 505 | rpcauth_invalcred(struct rpc_task *task) |
| 506 | { |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 507 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 508 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 509 | dprintk("RPC: %5u invalidating %s cred %p\n", |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 510 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 511 | if (cred) |
| 512 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | int |
| 516 | rpcauth_uptodatecred(struct rpc_task *task) |
| 517 | { |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 518 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 519 | |
| 520 | return cred == NULL || |
| 521 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |