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