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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Initialize RPC credential cache |
| 125 | */ |
| 126 | int |
| 127 | rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire) |
| 128 | { |
| 129 | struct rpc_cred_cache *new; |
| 130 | int i; |
| 131 | |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 132 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (!new) |
| 134 | return -ENOMEM; |
| 135 | for (i = 0; i < RPC_CREDCACHE_NR; i++) |
| 136 | INIT_HLIST_HEAD(&new->hashtable[i]); |
| 137 | new->expire = expire; |
| 138 | new->nextgc = jiffies + (expire >> 1); |
| 139 | auth->au_credcache = new; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Destroy a list of credentials |
| 145 | */ |
| 146 | static inline |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 147 | void rpcauth_destroy_credlist(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
| 149 | struct rpc_cred *cred; |
| 150 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 151 | while (!list_empty(head)) { |
| 152 | cred = list_entry(head->next, struct rpc_cred, cr_lru); |
| 153 | list_del_init(&cred->cr_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | put_rpccred(cred); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Clear the RPC credential cache, and delete those credentials |
| 160 | * that are not referenced. |
| 161 | */ |
| 162 | void |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 163 | rpcauth_clear_credcache(struct rpc_cred_cache *cache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 165 | LIST_HEAD(free); |
| 166 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | struct rpc_cred *cred; |
| 168 | int i; |
| 169 | |
| 170 | spin_lock(&rpc_credcache_lock); |
| 171 | for (i = 0; i < RPC_CREDCACHE_NR; i++) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 172 | head = &cache->hashtable[i]; |
| 173 | while (!hlist_empty(head)) { |
| 174 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); |
| 175 | get_rpccred(cred); |
| 176 | list_move_tail(&cred->cr_lru, &free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 177 | rpcauth_unhash_cred_locked(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | spin_unlock(&rpc_credcache_lock); |
| 181 | rpcauth_destroy_credlist(&free); |
| 182 | } |
| 183 | |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 184 | /* |
| 185 | * Destroy the RPC credential cache |
| 186 | */ |
| 187 | void |
| 188 | rpcauth_destroy_credcache(struct rpc_auth *auth) |
| 189 | { |
| 190 | struct rpc_cred_cache *cache = auth->au_credcache; |
| 191 | |
| 192 | if (cache) { |
| 193 | auth->au_credcache = NULL; |
| 194 | rpcauth_clear_credcache(cache); |
| 195 | kfree(cache); |
| 196 | } |
| 197 | } |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* |
| 200 | * Remove stale credentials. Avoid sleeping inside the loop. |
| 201 | */ |
| 202 | static void |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 203 | rpcauth_prune_expired(struct list_head *free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 205 | struct rpc_cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 207 | while (!list_empty(&cred_unused)) { |
| 208 | cred = list_entry(cred_unused.next, struct rpc_cred, cr_lru); |
| 209 | if (time_after(jiffies, cred->cr_expire + |
| 210 | cred->cr_auth->au_credcache->expire)) |
| 211 | break; |
| 212 | list_del_init(&cred->cr_lru); |
| 213 | if (atomic_read(&cred->cr_count) != 0) |
| 214 | continue; |
| 215 | get_rpccred(cred); |
| 216 | list_add_tail(&cred->cr_lru, free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 217 | rpcauth_unhash_cred_locked(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Run garbage collector. |
| 223 | */ |
| 224 | static void |
| 225 | rpcauth_gc_credcache(struct rpc_cred_cache *cache, struct list_head *free) |
| 226 | { |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 227 | if (list_empty(&cred_unused) || time_before(jiffies, cache->nextgc)) |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 228 | return; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 229 | spin_lock(&rpc_credcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | cache->nextgc = jiffies + cache->expire; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 231 | rpcauth_prune_expired(free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 232 | spin_unlock(&rpc_credcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Look up a process' credentials in the authentication cache |
| 237 | */ |
| 238 | struct rpc_cred * |
| 239 | rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 240 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 242 | LIST_HEAD(free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | struct rpc_cred_cache *cache = auth->au_credcache; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 244 | struct hlist_node *pos; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 245 | struct rpc_cred *cred = NULL, |
| 246 | *entry, *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | int nr = 0; |
| 248 | |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 249 | if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | nr = acred->uid & RPC_CREDCACHE_MASK; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 251 | |
| 252 | rcu_read_lock(); |
| 253 | hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { |
| 254 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 255 | continue; |
| 256 | spin_lock(&rpc_credcache_lock); |
| 257 | if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) { |
| 258 | spin_unlock(&rpc_credcache_lock); |
| 259 | continue; |
| 260 | } |
| 261 | cred = get_rpccred(entry); |
| 262 | spin_unlock(&rpc_credcache_lock); |
| 263 | break; |
| 264 | } |
| 265 | rcu_read_unlock(); |
| 266 | |
| 267 | if (cred != NULL) { |
| 268 | rpcauth_gc_credcache(cache, &free); |
| 269 | goto found; |
| 270 | } |
| 271 | |
| 272 | new = auth->au_ops->crcreate(auth, acred, flags); |
| 273 | if (IS_ERR(new)) { |
| 274 | cred = new; |
| 275 | goto out; |
| 276 | } |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | spin_lock(&rpc_credcache_lock); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 279 | hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) { |
| 280 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 281 | continue; |
| 282 | cred = get_rpccred(entry); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 283 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 285 | if (cred == NULL) { |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 286 | cred = new; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 287 | set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
| 288 | hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); |
| 289 | } else |
| 290 | list_add_tail(&new->cr_lru, &free); |
| 291 | rpcauth_prune_expired(&free); |
| 292 | cache->nextgc = jiffies + cache->expire; |
| 293 | spin_unlock(&rpc_credcache_lock); |
| 294 | found: |
| 295 | if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) |
Trond Myklebust | fba3bad | 2006-02-01 12:19:27 -0500 | [diff] [blame] | 296 | && cred->cr_ops->cr_init != NULL |
| 297 | && !(flags & RPCAUTH_LOOKUP_NEW)) { |
| 298 | int res = cred->cr_ops->cr_init(auth, cred); |
| 299 | if (res < 0) { |
| 300 | put_rpccred(cred); |
| 301 | cred = ERR_PTR(res); |
| 302 | } |
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 | rpcauth_destroy_credlist(&free); |
| 305 | out: |
| 306 | return cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | struct rpc_cred * |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 310 | rpcauth_lookupcred(struct rpc_auth *auth, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
| 312 | struct auth_cred acred = { |
| 313 | .uid = current->fsuid, |
| 314 | .gid = current->fsgid, |
| 315 | .group_info = current->group_info, |
| 316 | }; |
| 317 | struct rpc_cred *ret; |
| 318 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 319 | dprintk("RPC: looking up %s cred\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | auth->au_ops->au_name); |
| 321 | get_group_info(acred.group_info); |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 322 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | put_group_info(acred.group_info); |
| 324 | return ret; |
| 325 | } |
| 326 | |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 327 | void |
| 328 | rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, |
| 329 | struct rpc_auth *auth, const struct rpc_credops *ops) |
| 330 | { |
| 331 | INIT_HLIST_NODE(&cred->cr_hash); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 332 | INIT_LIST_HEAD(&cred->cr_lru); |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 333 | atomic_set(&cred->cr_count, 1); |
| 334 | cred->cr_auth = auth; |
| 335 | cred->cr_ops = ops; |
| 336 | cred->cr_expire = jiffies; |
| 337 | #ifdef RPC_DEBUG |
| 338 | cred->cr_magic = RPCAUTH_CRED_MAGIC; |
| 339 | #endif |
| 340 | cred->cr_uid = acred->uid; |
| 341 | } |
| 342 | EXPORT_SYMBOL(rpcauth_init_cred); |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | struct rpc_cred * |
| 345 | rpcauth_bindcred(struct rpc_task *task) |
| 346 | { |
| 347 | struct rpc_auth *auth = task->tk_auth; |
| 348 | struct auth_cred acred = { |
| 349 | .uid = current->fsuid, |
| 350 | .gid = current->fsgid, |
| 351 | .group_info = current->group_info, |
| 352 | }; |
| 353 | struct rpc_cred *ret; |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 354 | int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 356 | dprintk("RPC: %5u looking up %s cred\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | task->tk_pid, task->tk_auth->au_ops->au_name); |
| 358 | get_group_info(acred.group_info); |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 359 | if (task->tk_flags & RPC_TASK_ROOTCREDS) |
| 360 | flags |= RPCAUTH_LOOKUP_ROOTCREDS; |
| 361 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (!IS_ERR(ret)) |
| 363 | task->tk_msg.rpc_cred = ret; |
| 364 | else |
| 365 | task->tk_status = PTR_ERR(ret); |
| 366 | put_group_info(acred.group_info); |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | rpcauth_holdcred(struct rpc_task *task) |
| 372 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 373 | dprintk("RPC: %5u holding %s cred %p\n", |
| 374 | task->tk_pid, task->tk_auth->au_ops->au_name, |
| 375 | task->tk_msg.rpc_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (task->tk_msg.rpc_cred) |
| 377 | get_rpccred(task->tk_msg.rpc_cred); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | put_rpccred(struct rpc_cred *cred) |
| 382 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 383 | /* Fast path for unhashed credentials */ |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 384 | if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 385 | goto need_lock; |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (!atomic_dec_and_test(&cred->cr_count)) |
| 388 | return; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 389 | goto out_destroy; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 390 | need_lock: |
| 391 | if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock)) |
| 392 | return; |
| 393 | if (!list_empty(&cred->cr_lru)) |
| 394 | list_del_init(&cred->cr_lru); |
| 395 | if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0) |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame^] | 396 | rpcauth_unhash_cred_locked(cred); |
| 397 | else if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 398 | cred->cr_expire = jiffies; |
| 399 | list_add_tail(&cred->cr_lru, &cred_unused); |
| 400 | spin_unlock(&rpc_credcache_lock); |
| 401 | return; |
| 402 | } |
| 403 | spin_unlock(&rpc_credcache_lock); |
| 404 | out_destroy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | cred->cr_ops->crdestroy(cred); |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | rpcauth_unbindcred(struct rpc_task *task) |
| 410 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 412 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 413 | dprintk("RPC: %5u releasing %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 414 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | put_rpccred(cred); |
| 417 | task->tk_msg.rpc_cred = NULL; |
| 418 | } |
| 419 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 420 | __be32 * |
| 421 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 424 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 425 | dprintk("RPC: %5u marshaling %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 426 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return cred->cr_ops->crmarshal(task, p); |
| 429 | } |
| 430 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 431 | __be32 * |
| 432 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 435 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 436 | dprintk("RPC: %5u validating %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 437 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return cred->cr_ops->crvalidate(task, p); |
| 440 | } |
| 441 | |
| 442 | int |
| 443 | rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 444 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 447 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 448 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 450 | if (cred->cr_ops->crwrap_req) |
| 451 | return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); |
| 452 | /* By default, we encode the arguments normally. */ |
| 453 | return encode(rqstp, data, obj); |
| 454 | } |
| 455 | |
| 456 | int |
| 457 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 458 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 461 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 462 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 464 | if (cred->cr_ops->crunwrap_resp) |
| 465 | return cred->cr_ops->crunwrap_resp(task, decode, rqstp, |
| 466 | data, obj); |
| 467 | /* By default, we decode the arguments normally. */ |
| 468 | return decode(rqstp, data, obj); |
| 469 | } |
| 470 | |
| 471 | int |
| 472 | rpcauth_refreshcred(struct rpc_task *task) |
| 473 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 475 | int err; |
| 476 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 477 | dprintk("RPC: %5u refreshing %s cred %p\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 478 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | err = cred->cr_ops->crrefresh(task); |
| 481 | if (err < 0) |
| 482 | task->tk_status = err; |
| 483 | return err; |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | rpcauth_invalcred(struct rpc_task *task) |
| 488 | { |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 489 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 490 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 491 | dprintk("RPC: %5u invalidating %s cred %p\n", |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 492 | task->tk_pid, task->tk_auth->au_ops->au_name, cred); |
| 493 | if (cred) |
| 494 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int |
| 498 | rpcauth_uptodatecred(struct rpc_task *task) |
| 499 | { |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 500 | struct rpc_cred *cred = task->tk_msg.rpc_cred; |
| 501 | |
| 502 | return cred == NULL || |
| 503 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |