blob: 1f95bd61205340c3cb678557d7283d1f48f95696 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/include/linux/sunrpc/auth.h
4 *
5 * Declarations for the RPC client authentication machinery.
6 *
7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8 */
9
10#ifndef _LINUX_SUNRPC_AUTH_H
11#define _LINUX_SUNRPC_AUTH_H
12
13#ifdef __KERNEL__
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/sched.h>
16#include <linux/sunrpc/msg_prot.h>
17#include <linux/sunrpc/xdr.h>
18
Arun Sharma600634972011-07-26 16:09:06 -070019#include <linux/atomic.h>
Trond Myklebust31be5bf2007-06-24 15:55:26 -040020#include <linux/rcupdate.h>
Eric W. Biederman7eaf0402013-02-01 16:31:17 -080021#include <linux/uidgid.h>
Jeff Layton24a9a962015-08-03 07:44:53 -040022#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jeff Layton24a9a962015-08-03 07:44:53 -040024/*
Chuck Lever45006322016-03-01 13:06:02 -050025 * Maximum size of AUTH_NONE authentication information, in XDR words.
26 */
27#define NUL_CALLSLACK (4)
28#define NUL_REPLYSLACK (2)
29
30/*
Jeff Layton24a9a962015-08-03 07:44:53 -040031 * Size of the nodename buffer. RFC1831 specifies a hard limit of 255 bytes,
32 * but Linux hostnames are actually limited to __NEW_UTS_LEN bytes.
33 */
34#define UNX_MAXNODENAME __NEW_UTS_LEN
Chuck Lever45006322016-03-01 13:06:02 -050035#define UNX_CALLSLACK (21 + XDR_QUADLEN(UNX_MAXNODENAME))
Kinglong Mee57864612017-02-07 21:48:11 +080036#define UNX_NGROUPS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Chuck Lever9568c5e2013-03-16 15:54:43 -040038struct rpcsec_gss_info;
39
Andy Adamson4de6caa2013-08-14 11:59:15 -040040/* auth_cred ac_flags bits */
41enum {
Andy Adamson4de6caa2013-08-14 11:59:15 -040042 RPC_CRED_KEY_EXPIRE_SOON = 1, /* underlying cred key will expire soon */
43 RPC_CRED_NOTIFY_TIMEOUT = 2, /* nofity generic cred when underlying
44 key will expire soon */
45};
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* Work around the lack of a VFS credential */
48struct auth_cred {
NeilBrown97f68c6b2018-12-03 11:30:30 +110049 const struct cred *cred;
Eric W. Biederman7eaf0402013-02-01 16:31:17 -080050 kuid_t uid;
51 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct group_info *group_info;
Trond Myklebust68c97152012-01-03 13:22:46 -050053 const char *principal;
Andy Adamson4de6caa2013-08-14 11:59:15 -040054 unsigned long ac_flags;
Trond Myklebust7c67db32008-04-07 20:50:11 -040055 unsigned char machine_cred : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58/*
59 * Client user credentials
60 */
Trond Myklebust4a8c1342007-06-07 10:14:14 -040061struct rpc_auth;
62struct rpc_credops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063struct rpc_cred {
64 struct hlist_node cr_hash; /* hash chain */
Trond Myklebuste092bdc2007-06-23 19:45:36 -040065 struct list_head cr_lru; /* lru garbage collection */
Trond Myklebust31be5bf2007-06-24 15:55:26 -040066 struct rcu_head cr_rcu;
Trond Myklebust4a8c1342007-06-07 10:14:14 -040067 struct rpc_auth * cr_auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -040068 const struct rpc_credops *cr_ops;
Trond Myklebustfc432dd2007-06-25 10:15:15 -040069 unsigned long cr_expire; /* when to gc */
70 unsigned long cr_flags; /* various flags */
Trond Myklebust79b18182018-10-14 10:34:31 -040071 refcount_t cr_count; /* ref count */
NeilBrown97f68c6b2018-12-03 11:30:30 +110072 const struct cred *cr_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric W. Biederman7eaf0402013-02-01 16:31:17 -080074 kuid_t cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* per-flavor data */
77};
Trond Myklebustfc432dd2007-06-25 10:15:15 -040078#define RPCAUTH_CRED_NEW 0
79#define RPCAUTH_CRED_UPTODATE 1
Trond Myklebust31be5bf2007-06-24 15:55:26 -040080#define RPCAUTH_CRED_HASHED 2
Trond Myklebust126e2162010-05-13 12:55:38 -040081#define RPCAUTH_CRED_NEGATIVE 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Scott Mayhewce52914e2016-06-07 15:14:48 -040083/* rpc_auth au_flags */
84#define RPCAUTH_AUTH_NO_CRKEY_TIMEOUT 0x0001 /* underlying cred has no key timeout */
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * Client authentication handle
88 */
Trond Myklebust241269b2010-07-31 14:29:08 -040089struct rpc_cred_cache;
Trond Myklebust4a8c1342007-06-07 10:14:14 -040090struct rpc_authops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct rpc_auth {
92 unsigned int au_cslack; /* call cred size estimate */
J. Bruce Fields24b26052005-10-13 16:54:53 -040093 /* guess at number of u32's auth adds before
94 * reply data; normally the verifier size: */
95 unsigned int au_rslack;
96 /* for gss, used to calculate au_rslack: */
97 unsigned int au_verfsize;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned int au_flags; /* various flags */
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400100 const struct rpc_authops *au_ops; /* operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 rpc_authflavor_t au_flavor; /* pseudoflavor (note may
102 * differ from the flavor in
103 * au_ops->au_flavor in gss
104 * case) */
Trond Myklebust331bc712018-10-14 10:40:29 -0400105 refcount_t au_count; /* Reference counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 struct rpc_cred_cache * au_credcache;
108 /* per-flavor data */
109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Chuck Lever65b80172016-06-29 13:55:06 -0400111/* rpc_auth au_flags */
112#define RPCAUTH_AUTH_DATATOUCH 0x00000002
113
Trond Myklebustc2190662013-08-26 19:23:04 -0400114struct rpc_auth_create_args {
115 rpc_authflavor_t pseudoflavor;
116 const char *target_name;
117};
118
Trond Myklebust8a317762006-02-01 12:18:36 -0500119/* Flags for rpcauth_lookupcred() */
120#define RPCAUTH_LOOKUP_NEW 0x01 /* Accept an uninitialised cred */
NeilBrownbd956082014-07-14 11:28:20 +1000121#define RPCAUTH_LOOKUP_RCU 0x02 /* lock-less lookup */
Trond Myklebust8a317762006-02-01 12:18:36 -0500122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Client authentication ops
125 */
126struct rpc_authops {
127 struct module *owner;
128 rpc_authflavor_t au_flavor; /* flavor (RPC_AUTH_*) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char * au_name;
Sargun Dhillon82b98ca2018-07-05 16:48:50 +0000130 struct rpc_auth * (*create)(const struct rpc_auth_create_args *,
131 struct rpc_clnt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 void (*destroy)(struct rpc_auth *);
133
Frank Sorensone856a232016-09-29 10:44:37 -0500134 int (*hash_cred)(struct auth_cred *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct rpc_cred * (*lookup_cred)(struct rpc_auth *, struct auth_cred *, int);
Jeff Layton3c6e0bc2016-04-21 20:51:54 -0400136 struct rpc_cred * (*crcreate)(struct rpc_auth*, struct auth_cred *, int, gfp_t);
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400137 int (*list_pseudoflavors)(rpc_authflavor_t *, int);
Chuck Lever9568c5e2013-03-16 15:54:43 -0400138 rpc_authflavor_t (*info2flavor)(struct rpcsec_gss_info *);
Chuck Levera77c8062013-03-16 15:55:10 -0400139 int (*flavor2info)(rpc_authflavor_t,
140 struct rpcsec_gss_info *);
Andy Adamson4de6caa2013-08-14 11:59:15 -0400141 int (*key_timeout)(struct rpc_auth *,
142 struct rpc_cred *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct rpc_credops {
146 const char * cr_name; /* Name of the auth flavour */
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500147 int (*cr_init)(struct rpc_auth *, struct rpc_cred *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 void (*crdestroy)(struct rpc_cred *);
149
150 int (*crmatch)(struct auth_cred *, struct rpc_cred *, int);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400151 struct rpc_cred * (*crbind)(struct rpc_task *, struct rpc_cred *, int);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700152 __be32 * (*crmarshal)(struct rpc_task *, __be32 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int (*crrefresh)(struct rpc_task *);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700154 __be32 * (*crvalidate)(struct rpc_task *, __be32 *);
Chuck Lever9f06c712010-12-14 14:59:18 +0000155 int (*crwrap_req)(struct rpc_task *, kxdreproc_t,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700156 void *, __be32 *, void *);
Chuck Leverbf269552010-12-14 14:59:29 +0000157 int (*crunwrap_resp)(struct rpc_task *, kxdrdproc_t,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700158 void *, __be32 *, void *);
Andy Adamson4de6caa2013-08-14 11:59:15 -0400159 int (*crkey_timeout)(struct rpc_cred *);
160 bool (*crkey_to_expire)(struct rpc_cred *);
Jeff Laytona0337d12014-06-21 20:52:16 -0400161 char * (*crstringify_acceptor)(struct rpc_cred *);
Trond Myklebust3021a5bb2018-08-14 13:50:21 -0400162 bool (*crneed_reencode)(struct rpc_task *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400165extern const struct rpc_authops authunix_ops;
166extern const struct rpc_authops authnull_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400168int __init rpc_init_authunix(void);
169int __init rpc_init_generic_auth(void);
170int __init rpcauth_init_module(void);
Stephen Rothwellc135e842010-09-29 14:16:57 +1000171void rpcauth_remove_module(void);
172void rpc_destroy_generic_auth(void);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400173void rpc_destroy_authunix(void);
Trond Myklebust9499b432007-06-24 15:57:57 -0400174
Trond Myklebust9a559ef2008-03-12 12:24:49 -0400175struct rpc_cred * rpc_lookup_cred(void);
NeilBrownbd956082014-07-14 11:28:20 +1000176struct rpc_cred * rpc_lookup_cred_nonblock(void);
Weston Andros Adamsonc065d222016-04-21 20:51:55 -0400177struct rpc_cred * rpc_lookup_generic_cred(struct auth_cred *, int, gfp_t);
Trond Myklebust68c97152012-01-03 13:22:46 -0500178struct rpc_cred * rpc_lookup_machine_cred(const char *service_name);
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400179int rpcauth_register(const struct rpc_authops *);
180int rpcauth_unregister(const struct rpc_authops *);
Sargun Dhillon82b98ca2018-07-05 16:48:50 +0000181struct rpc_auth * rpcauth_create(const struct rpc_auth_create_args *,
Trond Myklebustc2190662013-08-26 19:23:04 -0400182 struct rpc_clnt *);
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400183void rpcauth_release(struct rpc_auth *);
Chuck Lever9568c5e2013-03-16 15:54:43 -0400184rpc_authflavor_t rpcauth_get_pseudoflavor(rpc_authflavor_t,
185 struct rpcsec_gss_info *);
Chuck Levera77c8062013-03-16 15:55:10 -0400186int rpcauth_get_gssinfo(rpc_authflavor_t,
187 struct rpcsec_gss_info *);
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400188int rpcauth_list_flavors(rpc_authflavor_t *, int);
Jeff Layton3c6e0bc2016-04-21 20:51:54 -0400189struct rpc_cred * rpcauth_lookup_credcache(struct rpc_auth *, struct auth_cred *, int, gfp_t);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400190void rpcauth_init_cred(struct rpc_cred *, const struct auth_cred *, struct rpc_auth *, const struct rpc_credops *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191struct rpc_cred * rpcauth_lookupcred(struct rpc_auth *, int);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400192struct rpc_cred * rpcauth_generic_bind_cred(struct rpc_task *, struct rpc_cred *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193void put_rpccred(struct rpc_cred *);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700194__be32 * rpcauth_marshcred(struct rpc_task *, __be32 *);
195__be32 * rpcauth_checkverf(struct rpc_task *, __be32 *);
Chuck Lever9f06c712010-12-14 14:59:18 +0000196int rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp, __be32 *data, void *obj);
Chuck Leverbf269552010-12-14 14:59:29 +0000197int rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp, __be32 *data, void *obj);
Trond Myklebust3021a5bb2018-08-14 13:50:21 -0400198bool rpcauth_xmit_need_reencode(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199int rpcauth_refreshcred(struct rpc_task *);
200void rpcauth_invalcred(struct rpc_task *);
201int rpcauth_uptodatecred(struct rpc_task *);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400202int rpcauth_init_credcache(struct rpc_auth *);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400203void rpcauth_destroy_credcache(struct rpc_auth *);
204void rpcauth_clear_credcache(struct rpc_cred_cache *);
Andy Adamson4de6caa2013-08-14 11:59:15 -0400205int rpcauth_key_timeout_notify(struct rpc_auth *,
206 struct rpc_cred *);
Scott Mayhewce52914e2016-06-07 15:14:48 -0400207bool rpcauth_cred_key_to_expire(struct rpc_auth *, struct rpc_cred *);
Jeff Laytona0337d12014-06-21 20:52:16 -0400208char * rpcauth_stringify_acceptor(struct rpc_cred *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210static inline
Trond Myklebust07d02a62018-10-12 13:28:26 -0400211struct rpc_cred *get_rpccred(struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Trond Myklebust79b18182018-10-14 10:34:31 -0400213 if (cred != NULL && refcount_inc_not_zero(&cred->cr_count))
Trond Myklebust07d02a62018-10-12 13:28:26 -0400214 return cred;
215 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Jeff Layton62dbef22016-04-21 20:51:56 -0400218/**
219 * get_rpccred_rcu - get a reference to a cred using rcu-protected pointer
220 * @cred: cred of which to take a reference
221 *
222 * In some cases, we may have a pointer to a credential to which we
223 * want to take a reference, but don't already have one. Because these
224 * objects are freed using RCU, we can access the cr_count while its
225 * on its way to destruction and only take a reference if it's not already
226 * zero.
227 */
228static inline struct rpc_cred *
229get_rpccred_rcu(struct rpc_cred *cred)
230{
Trond Myklebust07d02a62018-10-12 13:28:26 -0400231 return get_rpccred(cred);
Jeff Layton62dbef22016-04-21 20:51:56 -0400232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif /* __KERNEL__ */
235#endif /* _LINUX_SUNRPC_AUTH_H */