1 /*
2  * linux/include/linux/sunrpc/auth.h
3  *
4  * Declarations for the RPC client authentication machinery.
5  *
6  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7  */
8 
9 #ifndef _LINUX_SUNRPC_AUTH_H
10 #define _LINUX_SUNRPC_AUTH_H
11 
12 #ifdef __KERNEL__
13 
14 #include <linux/sunrpc/sched.h>
15 #include <linux/sunrpc/msg_prot.h>
16 #include <linux/sunrpc/xdr.h>
17 
18 #include <linux/atomic.h>
19 #include <linux/rcupdate.h>
20 #include <linux/uidgid.h>
21 #include <linux/utsname.h>
22 
23 /*
24  * Size of the nodename buffer. RFC1831 specifies a hard limit of 255 bytes,
25  * but Linux hostnames are actually limited to __NEW_UTS_LEN bytes.
26  */
27 #define UNX_MAXNODENAME	__NEW_UTS_LEN
28 
29 struct rpcsec_gss_info;
30 
31 /* auth_cred ac_flags bits */
32 enum {
33 	RPC_CRED_NO_CRKEY_TIMEOUT = 0, /* underlying cred has no key timeout */
34 	RPC_CRED_KEY_EXPIRE_SOON = 1, /* underlying cred key will expire soon */
35 	RPC_CRED_NOTIFY_TIMEOUT = 2,   /* nofity generic cred when underlying
36 					key will expire soon */
37 };
38 
39 /* Work around the lack of a VFS credential */
40 struct auth_cred {
41 	kuid_t	uid;
42 	kgid_t	gid;
43 	struct group_info *group_info;
44 	const char *principal;
45 	unsigned long ac_flags;
46 	unsigned char machine_cred : 1;
47 };
48 
49 /*
50  * Client user credentials
51  */
52 struct rpc_auth;
53 struct rpc_credops;
54 struct rpc_cred {
55 	struct hlist_node	cr_hash;	/* hash chain */
56 	struct list_head	cr_lru;		/* lru garbage collection */
57 	struct rcu_head		cr_rcu;
58 	struct rpc_auth *	cr_auth;
59 	const struct rpc_credops *cr_ops;
60 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
61 	unsigned long		cr_magic;	/* 0x0f4aa4f0 */
62 #endif
63 	unsigned long		cr_expire;	/* when to gc */
64 	unsigned long		cr_flags;	/* various flags */
65 	atomic_t		cr_count;	/* ref count */
66 
67 	kuid_t			cr_uid;
68 
69 	/* per-flavor data */
70 };
71 #define RPCAUTH_CRED_NEW	0
72 #define RPCAUTH_CRED_UPTODATE	1
73 #define RPCAUTH_CRED_HASHED	2
74 #define RPCAUTH_CRED_NEGATIVE	3
75 
76 #define RPCAUTH_CRED_MAGIC	0x0f4aa4f0
77 
78 /*
79  * Client authentication handle
80  */
81 struct rpc_cred_cache;
82 struct rpc_authops;
83 struct rpc_auth {
84 	unsigned int		au_cslack;	/* call cred size estimate */
85 				/* guess at number of u32's auth adds before
86 				 * reply data; normally the verifier size: */
87 	unsigned int		au_rslack;
88 				/* for gss, used to calculate au_rslack: */
89 	unsigned int		au_verfsize;
90 
91 	unsigned int		au_flags;	/* various flags */
92 	const struct rpc_authops *au_ops;		/* operations */
93 	rpc_authflavor_t	au_flavor;	/* pseudoflavor (note may
94 						 * differ from the flavor in
95 						 * au_ops->au_flavor in gss
96 						 * case) */
97 	atomic_t		au_count;	/* Reference counter */
98 
99 	struct rpc_cred_cache *	au_credcache;
100 	/* per-flavor data */
101 };
102 
103 struct rpc_auth_create_args {
104 	rpc_authflavor_t pseudoflavor;
105 	const char *target_name;
106 };
107 
108 /* Flags for rpcauth_lookupcred() */
109 #define RPCAUTH_LOOKUP_NEW		0x01	/* Accept an uninitialised cred */
110 #define RPCAUTH_LOOKUP_RCU		0x02	/* lock-less lookup */
111 
112 /*
113  * Client authentication ops
114  */
115 struct rpc_authops {
116 	struct module		*owner;
117 	rpc_authflavor_t	au_flavor;	/* flavor (RPC_AUTH_*) */
118 	char *			au_name;
119 	struct rpc_auth *	(*create)(struct rpc_auth_create_args *, struct rpc_clnt *);
120 	void			(*destroy)(struct rpc_auth *);
121 
122 	struct rpc_cred *	(*lookup_cred)(struct rpc_auth *, struct auth_cred *, int);
123 	struct rpc_cred *	(*crcreate)(struct rpc_auth*, struct auth_cred *, int);
124 	int			(*list_pseudoflavors)(rpc_authflavor_t *, int);
125 	rpc_authflavor_t	(*info2flavor)(struct rpcsec_gss_info *);
126 	int			(*flavor2info)(rpc_authflavor_t,
127 						struct rpcsec_gss_info *);
128 	int			(*key_timeout)(struct rpc_auth *,
129 						struct rpc_cred *);
130 };
131 
132 struct rpc_credops {
133 	const char *		cr_name;	/* Name of the auth flavour */
134 	int			(*cr_init)(struct rpc_auth *, struct rpc_cred *);
135 	void			(*crdestroy)(struct rpc_cred *);
136 
137 	int			(*crmatch)(struct auth_cred *, struct rpc_cred *, int);
138 	struct rpc_cred *	(*crbind)(struct rpc_task *, struct rpc_cred *, int);
139 	__be32 *		(*crmarshal)(struct rpc_task *, __be32 *);
140 	int			(*crrefresh)(struct rpc_task *);
141 	__be32 *		(*crvalidate)(struct rpc_task *, __be32 *);
142 	int			(*crwrap_req)(struct rpc_task *, kxdreproc_t,
143 						void *, __be32 *, void *);
144 	int			(*crunwrap_resp)(struct rpc_task *, kxdrdproc_t,
145 						void *, __be32 *, void *);
146 	int			(*crkey_timeout)(struct rpc_cred *);
147 	bool			(*crkey_to_expire)(struct rpc_cred *);
148 	char *			(*crstringify_acceptor)(struct rpc_cred *);
149 };
150 
151 extern const struct rpc_authops	authunix_ops;
152 extern const struct rpc_authops	authnull_ops;
153 
154 int __init		rpc_init_authunix(void);
155 int __init		rpc_init_generic_auth(void);
156 int __init		rpcauth_init_module(void);
157 void			rpcauth_remove_module(void);
158 void			rpc_destroy_generic_auth(void);
159 void 			rpc_destroy_authunix(void);
160 
161 struct rpc_cred *	rpc_lookup_cred(void);
162 struct rpc_cred *	rpc_lookup_cred_nonblock(void);
163 struct rpc_cred *	rpc_lookup_machine_cred(const char *service_name);
164 int			rpcauth_register(const struct rpc_authops *);
165 int			rpcauth_unregister(const struct rpc_authops *);
166 struct rpc_auth *	rpcauth_create(struct rpc_auth_create_args *,
167 				struct rpc_clnt *);
168 void			rpcauth_release(struct rpc_auth *);
169 rpc_authflavor_t	rpcauth_get_pseudoflavor(rpc_authflavor_t,
170 				struct rpcsec_gss_info *);
171 int			rpcauth_get_gssinfo(rpc_authflavor_t,
172 				struct rpcsec_gss_info *);
173 int			rpcauth_list_flavors(rpc_authflavor_t *, int);
174 struct rpc_cred *	rpcauth_lookup_credcache(struct rpc_auth *, struct auth_cred *, int);
175 void			rpcauth_init_cred(struct rpc_cred *, const struct auth_cred *, struct rpc_auth *, const struct rpc_credops *);
176 struct rpc_cred *	rpcauth_lookupcred(struct rpc_auth *, int);
177 struct rpc_cred *	rpcauth_generic_bind_cred(struct rpc_task *, struct rpc_cred *, int);
178 void			put_rpccred(struct rpc_cred *);
179 __be32 *		rpcauth_marshcred(struct rpc_task *, __be32 *);
180 __be32 *		rpcauth_checkverf(struct rpc_task *, __be32 *);
181 int			rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp, __be32 *data, void *obj);
182 int			rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp, __be32 *data, void *obj);
183 int			rpcauth_refreshcred(struct rpc_task *);
184 void			rpcauth_invalcred(struct rpc_task *);
185 int			rpcauth_uptodatecred(struct rpc_task *);
186 int			rpcauth_init_credcache(struct rpc_auth *);
187 void			rpcauth_destroy_credcache(struct rpc_auth *);
188 void			rpcauth_clear_credcache(struct rpc_cred_cache *);
189 int			rpcauth_key_timeout_notify(struct rpc_auth *,
190 						struct rpc_cred *);
191 bool			rpcauth_cred_key_to_expire(struct rpc_cred *);
192 char *			rpcauth_stringify_acceptor(struct rpc_cred *);
193 
194 static inline
get_rpccred(struct rpc_cred * cred)195 struct rpc_cred *	get_rpccred(struct rpc_cred *cred)
196 {
197 	atomic_inc(&cred->cr_count);
198 	return cred;
199 }
200 
201 #endif /* __KERNEL__ */
202 #endif /* _LINUX_SUNRPC_AUTH_H */
203