xref: /linux/include/linux/sunrpc/svcauth.h (revision ce3f5bb7504ca802efa710280a4601a06545bd2e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * linux/include/linux/sunrpc/svcauth.h
4  *
5  * RPC server-side authentication stuff.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 #ifndef _LINUX_SUNRPC_SVCAUTH_H_
11 #define _LINUX_SUNRPC_SVCAUTH_H_
12 
13 #include <linux/string.h>
14 #include <linux/sunrpc/msg_prot.h>
15 #include <linux/sunrpc/cache.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/hash.h>
19 #include <linux/stringhash.h>
20 #include <linux/cred.h>
21 
22 struct svc_cred {
23 	kuid_t			cr_uid;
24 	kgid_t			cr_gid;
25 	struct group_info	*cr_group_info;
26 	u32			cr_flavor; /* pseudoflavor */
27 	/* name of form servicetype/hostname@REALM, passed down by
28 	 * gss-proxy: */
29 	char			*cr_raw_principal;
30 	/* name of form servicetype@hostname, passed down by
31 	 * rpc.svcgssd, or computed from the above: */
32 	char			*cr_principal;
33 	char			*cr_targ_princ;
34 	struct gss_api_mech	*cr_gss_mech;
35 };
36 
init_svc_cred(struct svc_cred * cred)37 static inline void init_svc_cred(struct svc_cred *cred)
38 {
39 	cred->cr_group_info = NULL;
40 	cred->cr_raw_principal = NULL;
41 	cred->cr_principal = NULL;
42 	cred->cr_targ_princ = NULL;
43 	cred->cr_gss_mech = NULL;
44 }
45 
free_svc_cred(struct svc_cred * cred)46 static inline void free_svc_cred(struct svc_cred *cred)
47 {
48 	if (cred->cr_group_info)
49 		put_group_info(cred->cr_group_info);
50 	kfree(cred->cr_raw_principal);
51 	kfree(cred->cr_principal);
52 	kfree(cred->cr_targ_princ);
53 	gss_mech_put(cred->cr_gss_mech);
54 	init_svc_cred(cred);
55 }
56 
57 struct svc_rqst;		/* forward decl */
58 struct in6_addr;
59 
60 /* Authentication is done in the context of a domain.
61  *
62  * Currently, the nfs server uses the auth_domain to stand
63  * for the "client" listed in /etc/exports.
64  *
65  * More generally, a domain might represent a group of clients using
66  * a common mechanism for authentication and having a common mapping
67  * between local identity (uid) and network identity.  All clients
68  * in a domain have similar general access rights.  Each domain can
69  * contain multiple principals which will have different specific right
70  * based on normal Discretionary Access Control.
71  *
72  * A domain is created by an authentication flavour module based on name
73  * only.  Userspace then fills in detail on demand.
74  *
75  * In the case of auth_unix and auth_null, the auth_domain is also
76  * associated with entries in another cache representing the mapping
77  * of ip addresses to the given client.
78  */
79 struct auth_domain {
80 	struct kref		ref;
81 	struct hlist_node	hash;
82 	char			*name;
83 	struct auth_ops		*flavour;
84 	struct rcu_head		rcu_head;
85 };
86 
87 enum svc_auth_status {
88 	SVC_GARBAGE = 1,
89 	SVC_VALID,
90 	SVC_NEGATIVE,
91 	SVC_OK,
92 	SVC_DROP,
93 	SVC_CLOSE,
94 	SVC_DENIED,
95 	SVC_PENDING,
96 	SVC_COMPLETE,
97 };
98 
99 /*
100  * Each authentication flavour registers an auth_ops
101  * structure.
102  * name is simply the name.
103  * flavour gives the auth flavour. It determines where the flavour is registered
104  * accept() is given a request and should verify it.
105  *   It should inspect the authenticator and verifier, and possibly the data.
106  *    If there is a problem with the authentication *authp should be set.
107  *    The return value of accept() can indicate:
108  *      OK - authorised. client and credential are set in rqstp.
109  *           reqbuf points to arguments
110  *           resbuf points to good place for results.  verfier
111  *             is (probably) already in place.  Certainly space is
112  *	       reserved for it.
113  *      DROP - simply drop the request. It may have been deferred
114  *      CLOSE - like SVC_DROP, but request is definitely lost.
115  *		If there is a tcp connection, it should be closed.
116  *      GARBAGE - rpc garbage_args error
117  *      SYSERR - rpc system_err error
118  *      DENIED - authp holds reason for denial.
119  *      COMPLETE - the reply is encoded already and ready to be sent; no
120  *		further processing is necessary.  (This is used for processing
121  *		null procedure calls which are used to set up encryption
122  *		contexts.)
123  *
124  *   accept is passed the proc number so that it can accept NULL rpc requests
125  *   even if it cannot authenticate the client (as is sometimes appropriate).
126  *
127  * release() is given a request after the procedure has been run.
128  *  It should sign/encrypt the results if needed
129  *
130  * domain_release()
131  *   This call releases a domain.
132  *
133  * set_client()
134  *   Given a pending request (struct svc_rqst), finds and assigns
135  *   an appropriate 'auth_domain' as the client.
136  *
137  * pseudoflavor()
138  *   Returns RPC_AUTH pseudoflavor in use by @rqstp.
139  */
140 struct auth_ops {
141 	char *	name;
142 	struct module *owner;
143 	int	flavour;
144 
145 	enum svc_auth_status	(*accept)(struct svc_rqst *rqstp);
146 	int			(*release)(struct svc_rqst *rqstp);
147 	void			(*domain_release)(struct auth_domain *dom);
148 	enum svc_auth_status	(*set_client)(struct svc_rqst *rqstp);
149 	rpc_authflavor_t	(*pseudoflavor)(struct svc_rqst *rqstp);
150 };
151 
152 struct svc_xprt;
153 
154 extern rpc_authflavor_t svc_auth_flavor(struct svc_rqst *rqstp);
155 extern int	svc_authorise(struct svc_rqst *rqstp);
156 extern enum svc_auth_status svc_set_client(struct svc_rqst *rqstp);
157 extern int	svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops);
158 extern void	svc_auth_unregister(rpc_authflavor_t flavor);
159 
160 extern void	svcauth_map_clnt_to_svc_cred_local(struct rpc_clnt *clnt,
161 						   const struct cred *,
162 						   struct svc_cred *);
163 
164 extern struct auth_domain *unix_domain_find(char *name);
165 extern void auth_domain_put(struct auth_domain *item);
166 extern struct auth_domain *auth_domain_lookup(char *name, struct auth_domain *new);
167 extern struct auth_domain *auth_domain_find(char *name);
168 extern void svcauth_unix_purge(struct net *net);
169 extern void svcauth_unix_info_release(struct svc_xprt *xpt);
170 extern enum svc_auth_status svcauth_unix_set_client(struct svc_rqst *rqstp);
171 
172 extern int unix_gid_cache_create(struct net *net);
173 extern void unix_gid_cache_destroy(struct net *net);
174 
175 /*
176  * The <stringhash.h> functions are good enough that we don't need to
177  * use hash_32() on them; just extracting the high bits is enough.
178  */
hash_str(char const * name,int bits)179 static inline unsigned long hash_str(char const *name, int bits)
180 {
181 	return hashlen_hash(hashlen_string(NULL, name)) >> (32 - bits);
182 }
183 
hash_mem(char const * buf,int length,int bits)184 static inline unsigned long hash_mem(char const *buf, int length, int bits)
185 {
186 	return full_name_hash(NULL, buf, length) >> (32 - bits);
187 }
188 
189 #endif /* _LINUX_SUNRPC_SVCAUTH_H_ */
190