• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/net/sunrpc/svcauth.c
3  *
4  * The generic interface for RPC authentication on the server side.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  *
8  * CHANGES
9  * 19-Apr-2000 Chris Evans      - Security fix
10  */
11 
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/sunrpc/types.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/svcsock.h>
17 #include <linux/sunrpc/svcauth.h>
18 #include <linux/err.h>
19 #include <linux/hash.h>
20 
21 #include <trace/events/sunrpc.h>
22 
23 #include "sunrpc.h"
24 
25 #define RPCDBG_FACILITY	RPCDBG_AUTH
26 
27 
28 /*
29  * Table of authenticators
30  */
31 extern struct auth_ops svcauth_null;
32 extern struct auth_ops svcauth_unix;
33 
34 static DEFINE_SPINLOCK(authtab_lock);
35 static struct auth_ops	*authtab[RPC_AUTH_MAXFLAVOR] = {
36 	[0] = &svcauth_null,
37 	[1] = &svcauth_unix,
38 };
39 
40 int
svc_authenticate(struct svc_rqst * rqstp,__be32 * authp)41 svc_authenticate(struct svc_rqst *rqstp, __be32 *authp)
42 {
43 	rpc_authflavor_t	flavor;
44 	struct auth_ops		*aops;
45 
46 	*authp = rpc_auth_ok;
47 
48 	flavor = svc_getnl(&rqstp->rq_arg.head[0]);
49 
50 	dprintk("svc: svc_authenticate (%d)\n", flavor);
51 
52 	spin_lock(&authtab_lock);
53 	if (flavor >= RPC_AUTH_MAXFLAVOR || !(aops = authtab[flavor]) ||
54 	    !try_module_get(aops->owner)) {
55 		spin_unlock(&authtab_lock);
56 		*authp = rpc_autherr_badcred;
57 		return SVC_DENIED;
58 	}
59 	spin_unlock(&authtab_lock);
60 
61 	rqstp->rq_auth_slack = 0;
62 	init_svc_cred(&rqstp->rq_cred);
63 
64 	rqstp->rq_authop = aops;
65 	return aops->accept(rqstp, authp);
66 }
67 EXPORT_SYMBOL_GPL(svc_authenticate);
68 
svc_set_client(struct svc_rqst * rqstp)69 int svc_set_client(struct svc_rqst *rqstp)
70 {
71 	rqstp->rq_client = NULL;
72 	return rqstp->rq_authop->set_client(rqstp);
73 }
74 EXPORT_SYMBOL_GPL(svc_set_client);
75 
76 /* A request, which was authenticated, has now executed.
77  * Time to finalise the credentials and verifier
78  * and release and resources
79  */
svc_authorise(struct svc_rqst * rqstp)80 int svc_authorise(struct svc_rqst *rqstp)
81 {
82 	struct auth_ops *aops = rqstp->rq_authop;
83 	int rv = 0;
84 
85 	rqstp->rq_authop = NULL;
86 
87 	if (aops) {
88 		rv = aops->release(rqstp);
89 		module_put(aops->owner);
90 	}
91 	return rv;
92 }
93 
94 int
svc_auth_register(rpc_authflavor_t flavor,struct auth_ops * aops)95 svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
96 {
97 	int rv = -EINVAL;
98 	spin_lock(&authtab_lock);
99 	if (flavor < RPC_AUTH_MAXFLAVOR && authtab[flavor] == NULL) {
100 		authtab[flavor] = aops;
101 		rv = 0;
102 	}
103 	spin_unlock(&authtab_lock);
104 	return rv;
105 }
106 EXPORT_SYMBOL_GPL(svc_auth_register);
107 
108 void
svc_auth_unregister(rpc_authflavor_t flavor)109 svc_auth_unregister(rpc_authflavor_t flavor)
110 {
111 	spin_lock(&authtab_lock);
112 	if (flavor < RPC_AUTH_MAXFLAVOR)
113 		authtab[flavor] = NULL;
114 	spin_unlock(&authtab_lock);
115 }
116 EXPORT_SYMBOL_GPL(svc_auth_unregister);
117 
118 /**************************************************
119  * 'auth_domains' are stored in a hash table indexed by name.
120  * When the last reference to an 'auth_domain' is dropped,
121  * the object is unhashed and freed.
122  * If auth_domain_lookup fails to find an entry, it will return
123  * it's second argument 'new'.  If this is non-null, it will
124  * have been atomically linked into the table.
125  */
126 
127 #define	DN_HASHBITS	6
128 #define	DN_HASHMAX	(1<<DN_HASHBITS)
129 
130 static struct hlist_head	auth_domain_table[DN_HASHMAX];
131 static DEFINE_SPINLOCK(auth_domain_lock);
132 
auth_domain_release(struct kref * kref)133 static void auth_domain_release(struct kref *kref)
134 {
135 	struct auth_domain *dom = container_of(kref, struct auth_domain, ref);
136 
137 	hlist_del(&dom->hash);
138 	dom->flavour->domain_release(dom);
139 	spin_unlock(&auth_domain_lock);
140 }
141 
auth_domain_put(struct auth_domain * dom)142 void auth_domain_put(struct auth_domain *dom)
143 {
144 	kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock);
145 }
146 EXPORT_SYMBOL_GPL(auth_domain_put);
147 
148 struct auth_domain *
auth_domain_lookup(char * name,struct auth_domain * new)149 auth_domain_lookup(char *name, struct auth_domain *new)
150 {
151 	struct auth_domain *hp;
152 	struct hlist_head *head;
153 
154 	head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
155 
156 	spin_lock(&auth_domain_lock);
157 
158 	hlist_for_each_entry(hp, head, hash) {
159 		if (strcmp(hp->name, name)==0) {
160 			kref_get(&hp->ref);
161 			spin_unlock(&auth_domain_lock);
162 			return hp;
163 		}
164 	}
165 	if (new)
166 		hlist_add_head(&new->hash, head);
167 	spin_unlock(&auth_domain_lock);
168 	return new;
169 }
170 EXPORT_SYMBOL_GPL(auth_domain_lookup);
171 
auth_domain_find(char * name)172 struct auth_domain *auth_domain_find(char *name)
173 {
174 	return auth_domain_lookup(name, NULL);
175 }
176 EXPORT_SYMBOL_GPL(auth_domain_find);
177 
178 /**
179  * auth_domain_cleanup - check that the auth_domain table is empty
180  *
181  * On module unload the auth_domain_table must be empty.  To make it
182  * easier to catch bugs which don't clean up domains properly, we
183  * warn if anything remains in the table at cleanup time.
184  *
185  * Note that we cannot proactively remove the domains at this stage.
186  * The ->release() function might be in a module that has already been
187  * unloaded.
188  */
189 
auth_domain_cleanup(void)190 void auth_domain_cleanup(void)
191 {
192 	int h;
193 	struct auth_domain *hp;
194 
195 	for (h = 0; h < DN_HASHMAX; h++)
196 		hlist_for_each_entry(hp, &auth_domain_table[h], hash)
197 			pr_warn("svc: domain %s still present at module unload.\n",
198 				hp->name);
199 }
200