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/hash.h>
18#include <linux/stringhash.h>
19#include <linux/cred.h>
20
21struct svc_cred {
22	kuid_t			cr_uid;
23	kgid_t			cr_gid;
24	struct group_info	*cr_group_info;
25	u32			cr_flavor; /* pseudoflavor */
26	/* name of form servicetype/hostname@REALM, passed down by
27	 * gss-proxy: */
28	char			*cr_raw_principal;
29	/* name of form servicetype@hostname, passed down by
30	 * rpc.svcgssd, or computed from the above: */
31	char			*cr_principal;
32	char			*cr_targ_princ;
33	struct gss_api_mech	*cr_gss_mech;
34};
35
36static inline void init_svc_cred(struct svc_cred *cred)
37{
38	cred->cr_group_info = NULL;
39	cred->cr_raw_principal = NULL;
40	cred->cr_principal = NULL;
41	cred->cr_targ_princ = NULL;
42	cred->cr_gss_mech = NULL;
43}
44
45static inline void free_svc_cred(struct svc_cred *cred)
46{
47	if (cred->cr_group_info)
48		put_group_info(cred->cr_group_info);
49	kfree(cred->cr_raw_principal);
50	kfree(cred->cr_principal);
51	kfree(cred->cr_targ_princ);
52	gss_mech_put(cred->cr_gss_mech);
53	init_svc_cred(cred);
54}
55
56struct svc_rqst;		/* forward decl */
57struct in6_addr;
58
59/* Authentication is done in the context of a domain.
60 *
61 * Currently, the nfs server uses the auth_domain to stand
62 * for the "client" listed in /etc/exports.
63 *
64 * More generally, a domain might represent a group of clients using
65 * a common mechanism for authentication and having a common mapping
66 * between local identity (uid) and network identity.  All clients
67 * in a domain have similar general access rights.  Each domain can
68 * contain multiple principals which will have different specific right
69 * based on normal Discretionary Access Control.
70 *
71 * A domain is created by an authentication flavour module based on name
72 * only.  Userspace then fills in detail on demand.
73 *
74 * In the case of auth_unix and auth_null, the auth_domain is also
75 * associated with entries in another cache representing the mapping
76 * of ip addresses to the given client.
77 */
78struct auth_domain {
79	struct kref		ref;
80	struct hlist_node	hash;
81	char			*name;
82	struct auth_ops		*flavour;
83	struct rcu_head		rcu_head;
84};
85
86enum svc_auth_status {
87	SVC_GARBAGE = 1,
88	SVC_SYSERR,
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 */
140struct 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
152struct svc_xprt;
153
154extern enum svc_auth_status svc_authenticate(struct svc_rqst *rqstp);
155extern rpc_authflavor_t svc_auth_flavor(struct svc_rqst *rqstp);
156extern int	svc_authorise(struct svc_rqst *rqstp);
157extern enum svc_auth_status svc_set_client(struct svc_rqst *rqstp);
158extern int	svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops);
159extern void	svc_auth_unregister(rpc_authflavor_t flavor);
160
161extern struct auth_domain *unix_domain_find(char *name);
162extern void auth_domain_put(struct auth_domain *item);
163extern struct auth_domain *auth_domain_lookup(char *name, struct auth_domain *new);
164extern struct auth_domain *auth_domain_find(char *name);
165extern void svcauth_unix_purge(struct net *net);
166extern void svcauth_unix_info_release(struct svc_xprt *xpt);
167extern enum svc_auth_status svcauth_unix_set_client(struct svc_rqst *rqstp);
168
169extern int unix_gid_cache_create(struct net *net);
170extern void unix_gid_cache_destroy(struct net *net);
171
172/*
173 * The <stringhash.h> functions are good enough that we don't need to
174 * use hash_32() on them; just extracting the high bits is enough.
175 */
176static inline unsigned long hash_str(char const *name, int bits)
177{
178	return hashlen_hash(hashlen_string(NULL, name)) >> (32 - bits);
179}
180
181static inline unsigned long hash_mem(char const *buf, int length, int bits)
182{
183	return full_name_hash(NULL, buf, length) >> (32 - bits);
184}
185
186#endif /* _LINUX_SUNRPC_SVCAUTH_H_ */
187