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_SYSERR,
90 	SVC_VALID,
91 	SVC_NEGATIVE,
92 	SVC_OK,
93 	SVC_DROP,
94 	SVC_CLOSE,
95 	SVC_DENIED,
96 	SVC_PENDING,
97 	SVC_COMPLETE,
98 };
99 
100 /*
101  * Each authentication flavour registers an auth_ops
102  * structure.
103  * name is simply the name.
104  * flavour gives the auth flavour. It determines where the flavour is registered
105  * accept() is given a request and should verify it.
106  *   It should inspect the authenticator and verifier, and possibly the data.
107  *    If there is a problem with the authentication *authp should be set.
108  *    The return value of accept() can indicate:
109  *      OK - authorised. client and credential are set in rqstp.
110  *           reqbuf points to arguments
111  *           resbuf points to good place for results.  verfier
112  *             is (probably) already in place.  Certainly space is
113  *	       reserved for it.
114  *      DROP - simply drop the request. It may have been deferred
115  *      CLOSE - like SVC_DROP, but request is definitely lost.
116  *		If there is a tcp connection, it should be closed.
117  *      GARBAGE - rpc garbage_args error
118  *      SYSERR - rpc system_err error
119  *      DENIED - authp holds reason for denial.
120  *      COMPLETE - the reply is encoded already and ready to be sent; no
121  *		further processing is necessary.  (This is used for processing
122  *		null procedure calls which are used to set up encryption
123  *		contexts.)
124  *
125  *   accept is passed the proc number so that it can accept NULL rpc requests
126  *   even if it cannot authenticate the client (as is sometimes appropriate).
127  *
128  * release() is given a request after the procedure has been run.
129  *  It should sign/encrypt the results if needed
130  *
131  * domain_release()
132  *   This call releases a domain.
133  *
134  * set_client()
135  *   Given a pending request (struct svc_rqst), finds and assigns
136  *   an appropriate 'auth_domain' as the client.
137  *
138  * pseudoflavor()
139  *   Returns RPC_AUTH pseudoflavor in use by @rqstp.
140  */
141 struct auth_ops {
142 	char *	name;
143 	struct module *owner;
144 	int	flavour;
145 
146 	enum svc_auth_status	(*accept)(struct svc_rqst *rqstp);
147 	int			(*release)(struct svc_rqst *rqstp);
148 	void			(*domain_release)(struct auth_domain *dom);
149 	enum svc_auth_status	(*set_client)(struct svc_rqst *rqstp);
150 	rpc_authflavor_t	(*pseudoflavor)(struct svc_rqst *rqstp);
151 };
152 
153 struct svc_xprt;
154 
155 extern rpc_authflavor_t svc_auth_flavor(struct svc_rqst *rqstp);
156 extern int	svc_authorise(struct svc_rqst *rqstp);
157 extern enum svc_auth_status svc_set_client(struct svc_rqst *rqstp);
158 extern int	svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops);
159 extern void	svc_auth_unregister(rpc_authflavor_t flavor);
160 
161 extern void	svcauth_map_clnt_to_svc_cred_local(struct rpc_clnt *clnt,
162 						   const struct cred *,
163 						   struct svc_cred *);
164 
165 extern struct auth_domain *unix_domain_find(char *name);
166 extern void auth_domain_put(struct auth_domain *item);
167 extern struct auth_domain *auth_domain_lookup(char *name, struct auth_domain *new);
168 extern struct auth_domain *auth_domain_find(char *name);
169 extern void svcauth_unix_purge(struct net *net);
170 extern void svcauth_unix_info_release(struct svc_xprt *xpt);
171 extern enum svc_auth_status svcauth_unix_set_client(struct svc_rqst *rqstp);
172 
173 extern int unix_gid_cache_create(struct net *net);
174 extern void unix_gid_cache_destroy(struct net *net);
175 
176 /*
177  * The <stringhash.h> functions are good enough that we don't need to
178  * use hash_32() on them; just extracting the high bits is enough.
179  */
hash_str(char const * name,int bits)180 static inline unsigned long hash_str(char const *name, int bits)
181 {
182 	return hashlen_hash(hashlen_string(NULL, name)) >> (32 - bits);
183 }
184 
hash_mem(char const * buf,int length,int bits)185 static inline unsigned long hash_mem(char const *buf, int length, int bits)
186 {
187 	return full_name_hash(NULL, buf, length) >> (32 - bits);
188 }
189 
190 #endif /* _LINUX_SUNRPC_SVCAUTH_H_ */
191