| 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 |  | 
| Eric W. Biederman | bf37f79 | 2013-02-01 15:55:38 -0800 | [diff] [blame] | 21 | #define RPC_MACHINE_CRED_USERID		GLOBAL_ROOT_UID | 
 | 22 | #define RPC_MACHINE_CRED_GROUPID	GLOBAL_ROOT_GID | 
| 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 |  */ | 
| Trond Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 44 | struct rpc_cred *rpc_lookup_machine_cred(const char *service_name) | 
| Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 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 | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 49 | 		.principal = service_name, | 
| Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 50 | 		.machine_cred = 1, | 
 | 51 | 	}; | 
 | 52 |  | 
| Trond Myklebust | 68c9715 | 2012-01-03 13:22:46 -0500 | [diff] [blame] | 53 | 	dprintk("RPC:       looking up machine cred for service %s\n", | 
 | 54 | 			service_name); | 
| Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 55 | 	return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0); | 
 | 56 | } | 
 | 57 | EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred); | 
 | 58 |  | 
| Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 59 | static struct rpc_cred *generic_bind_cred(struct rpc_task *task, | 
 | 60 | 		struct rpc_cred *cred, int lookupflags) | 
| Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 61 | { | 
 | 62 | 	struct rpc_auth *auth = task->tk_client->cl_auth; | 
 | 63 | 	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] | 64 |  | 
| Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 65 | 	return auth->au_ops->lookup_cred(auth, acred, lookupflags); | 
| Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 66 | } | 
 | 67 |  | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 68 | /* | 
 | 69 |  * Lookup generic creds for current process | 
 | 70 |  */ | 
 | 71 | static struct rpc_cred * | 
 | 72 | generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) | 
 | 73 | { | 
 | 74 | 	return rpcauth_lookup_credcache(&generic_auth, acred, flags); | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static struct rpc_cred * | 
 | 78 | generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags) | 
 | 79 | { | 
 | 80 | 	struct generic_cred *gcred; | 
 | 81 |  | 
 | 82 | 	gcred = kmalloc(sizeof(*gcred), GFP_KERNEL); | 
 | 83 | 	if (gcred == NULL) | 
 | 84 | 		return ERR_PTR(-ENOMEM); | 
 | 85 |  | 
 | 86 | 	rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops); | 
 | 87 | 	gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE; | 
 | 88 |  | 
 | 89 | 	gcred->acred.uid = acred->uid; | 
 | 90 | 	gcred->acred.gid = acred->gid; | 
 | 91 | 	gcred->acred.group_info = acred->group_info; | 
 | 92 | 	if (gcred->acred.group_info != NULL) | 
 | 93 | 		get_group_info(gcred->acred.group_info); | 
| Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 94 | 	gcred->acred.machine_cred = acred->machine_cred; | 
| Trond Myklebust | 875ad3f | 2012-01-23 12:49:36 -0500 | [diff] [blame] | 95 | 	gcred->acred.principal = acred->principal; | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 96 |  | 
| Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 97 | 	dprintk("RPC:       allocated %s cred %p for uid %d gid %d\n", | 
 | 98 | 			gcred->acred.machine_cred ? "machine" : "generic", | 
| Eric W. Biederman | cdba321 | 2013-02-01 17:10:52 -0800 | [diff] [blame] | 99 | 			gcred, | 
 | 100 | 			from_kuid(&init_user_ns, acred->uid), | 
 | 101 | 			from_kgid(&init_user_ns, acred->gid)); | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 102 | 	return &gcred->gc_base; | 
 | 103 | } | 
 | 104 |  | 
 | 105 | static void | 
 | 106 | generic_free_cred(struct rpc_cred *cred) | 
 | 107 | { | 
 | 108 | 	struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base); | 
 | 109 |  | 
 | 110 | 	dprintk("RPC:       generic_free_cred %p\n", gcred); | 
 | 111 | 	if (gcred->acred.group_info != NULL) | 
 | 112 | 		put_group_info(gcred->acred.group_info); | 
 | 113 | 	kfree(gcred); | 
 | 114 | } | 
 | 115 |  | 
 | 116 | static void | 
 | 117 | generic_free_cred_callback(struct rcu_head *head) | 
 | 118 | { | 
 | 119 | 	struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu); | 
 | 120 | 	generic_free_cred(cred); | 
 | 121 | } | 
 | 122 |  | 
 | 123 | static void | 
 | 124 | generic_destroy_cred(struct rpc_cred *cred) | 
 | 125 | { | 
 | 126 | 	call_rcu(&cred->cr_rcu, generic_free_cred_callback); | 
 | 127 | } | 
 | 128 |  | 
| Trond Myklebust | 875ad3f | 2012-01-23 12:49:36 -0500 | [diff] [blame] | 129 | static int | 
 | 130 | machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags) | 
 | 131 | { | 
 | 132 | 	if (!gcred->acred.machine_cred || | 
 | 133 | 	    gcred->acred.principal != acred->principal || | 
| Eric W. Biederman | 0b4d51b | 2013-02-01 16:39:32 -0800 | [diff] [blame] | 134 | 	    !uid_eq(gcred->acred.uid, acred->uid) || | 
 | 135 | 	    !gid_eq(gcred->acred.gid, acred->gid)) | 
| Trond Myklebust | 875ad3f | 2012-01-23 12:49:36 -0500 | [diff] [blame] | 136 | 		return 0; | 
 | 137 | 	return 1; | 
 | 138 | } | 
 | 139 |  | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 140 | /* | 
 | 141 |  * Match credentials against current process creds. | 
 | 142 |  */ | 
 | 143 | static int | 
 | 144 | generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags) | 
 | 145 | { | 
 | 146 | 	struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base); | 
| Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 147 | 	int i; | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 148 |  | 
| Trond Myklebust | 875ad3f | 2012-01-23 12:49:36 -0500 | [diff] [blame] | 149 | 	if (acred->machine_cred) | 
 | 150 | 		return machine_cred_match(acred, gcred, flags); | 
 | 151 |  | 
| Eric W. Biederman | 0b4d51b | 2013-02-01 16:39:32 -0800 | [diff] [blame] | 152 | 	if (!uid_eq(gcred->acred.uid, acred->uid) || | 
 | 153 | 	    !gid_eq(gcred->acred.gid, acred->gid) || | 
| Trond Myklebust | 875ad3f | 2012-01-23 12:49:36 -0500 | [diff] [blame] | 154 | 	    gcred->acred.machine_cred != 0) | 
| Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 155 | 		goto out_nomatch; | 
 | 156 |  | 
 | 157 | 	/* Optimisation in the case where pointers are identical... */ | 
 | 158 | 	if (gcred->acred.group_info == acred->group_info) | 
 | 159 | 		goto out_match; | 
 | 160 |  | 
 | 161 | 	/* Slow path... */ | 
 | 162 | 	if (gcred->acred.group_info->ngroups != acred->group_info->ngroups) | 
 | 163 | 		goto out_nomatch; | 
 | 164 | 	for (i = 0; i < gcred->acred.group_info->ngroups; i++) { | 
| Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 165 | 		if (!gid_eq(GROUP_AT(gcred->acred.group_info, i), | 
 | 166 | 				GROUP_AT(acred->group_info, i))) | 
| Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 167 | 			goto out_nomatch; | 
 | 168 | 	} | 
 | 169 | out_match: | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 170 | 	return 1; | 
| Trond Myklebust | 23918b0 | 2008-11-20 16:06:21 -0500 | [diff] [blame] | 171 | out_nomatch: | 
 | 172 | 	return 0; | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 173 | } | 
 | 174 |  | 
| Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 175 | int __init rpc_init_generic_auth(void) | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 176 | { | 
| Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 177 | 	return rpcauth_init_credcache(&generic_auth); | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 178 | } | 
 | 179 |  | 
| Stephen Rothwell | c135e84 | 2010-09-29 14:16:57 +1000 | [diff] [blame] | 180 | void rpc_destroy_generic_auth(void) | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 181 | { | 
| Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 182 | 	rpcauth_destroy_credcache(&generic_auth); | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 183 | } | 
 | 184 |  | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 185 | static const struct rpc_authops generic_auth_ops = { | 
 | 186 | 	.owner = THIS_MODULE, | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 187 | 	.au_name = "Generic", | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 188 | 	.lookup_cred = generic_lookup_cred, | 
 | 189 | 	.crcreate = generic_create_cred, | 
 | 190 | }; | 
 | 191 |  | 
 | 192 | static struct rpc_auth generic_auth = { | 
 | 193 | 	.au_ops = &generic_auth_ops, | 
 | 194 | 	.au_count = ATOMIC_INIT(0), | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 195 | }; | 
 | 196 |  | 
 | 197 | static const struct rpc_credops generic_credops = { | 
 | 198 | 	.cr_name = "Generic cred", | 
 | 199 | 	.crdestroy = generic_destroy_cred, | 
| Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 200 | 	.crbind = generic_bind_cred, | 
| Trond Myklebust | 9a559ef | 2008-03-12 12:24:49 -0400 | [diff] [blame] | 201 | 	.crmatch = generic_match, | 
 | 202 | }; |