Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Generic RPC credential |
| 3 | * |
| 4 | * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/sunrpc/auth.h> |
| 13 | #include <linux/sunrpc/clnt.h> |
| 14 | #include <linux/sunrpc/debug.h> |
| 15 | #include <linux/sunrpc/sched.h> |
| 16 | |
| 17 | #ifdef RPC_DEBUG |
| 18 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 19 | #endif |
| 20 | |
Trond Myklebust | b452876 | 2008-05-11 12:18:51 -0700 | [diff] [blame] | 21 | #define RPC_MACHINE_CRED_USERID ((uid_t)0) |
| 22 | #define RPC_MACHINE_CRED_GROUPID ((gid_t)0) |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 23 | |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 24 | struct generic_cred { |
| 25 | struct rpc_cred gc_base; |
| 26 | struct auth_cred acred; |
| 27 | }; |
| 28 | |
| 29 | static struct rpc_auth generic_auth; |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 30 | static const struct rpc_credops generic_credops; |
| 31 | |
| 32 | /* |
| 33 | * Public call interface |
| 34 | */ |
| 35 | struct rpc_cred *rpc_lookup_cred(void) |
| 36 | { |
| 37 | return rpcauth_lookupcred(&generic_auth, 0); |
| 38 | } |
| 39 | EXPORT_SYMBOL_GPL(rpc_lookup_cred); |
| 40 | |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 41 | /* |
| 42 | * Public call interface for looking up machine creds. |
| 43 | */ |
| 44 | struct rpc_cred *rpc_lookup_machine_cred(void) |
| 45 | { |
| 46 | struct auth_cred acred = { |
Trond Myklebust | b452876 | 2008-05-11 12:18:51 -0700 | [diff] [blame] | 47 | .uid = RPC_MACHINE_CRED_USERID, |
| 48 | .gid = RPC_MACHINE_CRED_GROUPID, |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 49 | .machine_cred = 1, |
| 50 | }; |
| 51 | |
| 52 | dprintk("RPC: looking up machine cred\n"); |
| 53 | return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0); |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred); |
| 56 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 57 | static struct rpc_cred *generic_bind_cred(struct rpc_task *task, |
| 58 | struct rpc_cred *cred, int lookupflags) |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 59 | { |
| 60 | struct rpc_auth *auth = task->tk_client->cl_auth; |
| 61 | struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred; |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 62 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 63 | return auth->au_ops->lookup_cred(auth, acred, lookupflags); |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 66 | /* |
| 67 | * Lookup generic creds for current process |
| 68 | */ |
| 69 | static struct rpc_cred * |
| 70 | generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) |
| 71 | { |
| 72 | return rpcauth_lookup_credcache(&generic_auth, acred, flags); |
| 73 | } |
| 74 | |
| 75 | static struct rpc_cred * |
| 76 | generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) |
| 77 | { |
| 78 | struct generic_cred *gcred; |
| 79 | |
| 80 | gcred = kmalloc(sizeof(*gcred), GFP_KERNEL); |
| 81 | if (gcred == NULL) |
| 82 | return ERR_PTR(-ENOMEM); |
| 83 | |
| 84 | rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops); |
| 85 | gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE; |
| 86 | |
| 87 | gcred->acred.uid = acred->uid; |
| 88 | gcred->acred.gid = acred->gid; |
| 89 | gcred->acred.group_info = acred->group_info; |
| 90 | if (gcred->acred.group_info != NULL) |
| 91 | get_group_info(gcred->acred.group_info); |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 92 | gcred->acred.machine_cred = acred->machine_cred; |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 93 | |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 94 | dprintk("RPC: allocated %s cred %p for uid %d gid %d\n", |
| 95 | gcred->acred.machine_cred ? "machine" : "generic", |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 96 | gcred, acred->uid, acred->gid); |
| 97 | return &gcred->gc_base; |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | generic_free_cred(struct rpc_cred *cred) |
| 102 | { |
| 103 | struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base); |
| 104 | |
| 105 | dprintk("RPC: generic_free_cred %p\n", gcred); |
| 106 | if (gcred->acred.group_info != NULL) |
| 107 | put_group_info(gcred->acred.group_info); |
| 108 | kfree(gcred); |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | generic_free_cred_callback(struct rcu_head *head) |
| 113 | { |
| 114 | struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu); |
| 115 | generic_free_cred(cred); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | generic_destroy_cred(struct rpc_cred *cred) |
| 120 | { |
| 121 | call_rcu(&cred->cr_rcu, generic_free_cred_callback); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Match credentials against current process creds. |
| 126 | */ |
| 127 | static int |
| 128 | generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags) |
| 129 | { |
| 130 | struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base); |
Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 131 | int i; |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 132 | |
| 133 | if (gcred->acred.uid != acred->uid || |
| 134 | gcred->acred.gid != acred->gid || |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 135 | gcred->acred.machine_cred != acred->machine_cred) |
Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 136 | goto out_nomatch; |
| 137 | |
| 138 | /* Optimisation in the case where pointers are identical... */ |
| 139 | if (gcred->acred.group_info == acred->group_info) |
| 140 | goto out_match; |
| 141 | |
| 142 | /* Slow path... */ |
| 143 | if (gcred->acred.group_info->ngroups != acred->group_info->ngroups) |
| 144 | goto out_nomatch; |
| 145 | for (i = 0; i < gcred->acred.group_info->ngroups; i++) { |
| 146 | if (GROUP_AT(gcred->acred.group_info, i) != |
| 147 | GROUP_AT(acred->group_info, i)) |
| 148 | goto out_nomatch; |
| 149 | } |
| 150 | out_match: |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 151 | return 1; |
Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 152 | out_nomatch: |
| 153 | return 0; |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Trond Myklebust | 5d8d9a4d | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 156 | int __init rpc_init_generic_auth(void) |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 157 | { |
Trond Myklebust | 5d8d9a4d | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 158 | return rpcauth_init_credcache(&generic_auth); |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Stephen Rothwell | c135e84 | 2010-09-29 14:16:57 +1000 | [diff] [blame] | 161 | void rpc_destroy_generic_auth(void) |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 162 | { |
Trond Myklebust | 5d8d9a4d | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 163 | rpcauth_destroy_credcache(&generic_auth); |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 166 | static const struct rpc_authops generic_auth_ops = { |
| 167 | .owner = THIS_MODULE, |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 168 | .au_name = "Generic", |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 169 | .lookup_cred = generic_lookup_cred, |
| 170 | .crcreate = generic_create_cred, |
| 171 | }; |
| 172 | |
| 173 | static struct rpc_auth generic_auth = { |
| 174 | .au_ops = &generic_auth_ops, |
| 175 | .au_count = ATOMIC_INIT(0), |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static const struct rpc_credops generic_credops = { |
| 179 | .cr_name = "Generic cred", |
| 180 | .crdestroy = generic_destroy_cred, |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 181 | .crbind = generic_bind_cred, |
Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 182 | .crmatch = generic_match, |
| 183 | }; |