blob: 30916b06c12bd7f5ade9f9cea0641e51ac52e528 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/sched.h>
3#include <linux/module.h>
4#include <linux/sunrpc/types.h>
5#include <linux/sunrpc/xdr.h>
6#include <linux/sunrpc/svcsock.h>
7#include <linux/sunrpc/svcauth.h>
Andy Adamsonc4170582007-07-17 04:04:42 -07008#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/err.h>
10#include <linux/seq_file.h>
11#include <linux/hash.h>
Paulo Marques543537b2005-06-23 00:09:02 -070012#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Greg Banks7b2b1fe2006-10-04 02:15:50 -070014#include <net/sock.h>
Aurélien Charbonf15364b2008-01-18 15:50:56 +010015#include <net/ipv6.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define RPCDBG_FACILITY RPCDBG_AUTH
18
Chuck Lever07396052010-01-26 14:03:47 -050019#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Pavel Emelyanov90d51b02010-09-27 14:02:29 +040021#include "netns.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24 * AUTHUNIX and AUTHNULL credentials are both handled here.
25 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
26 * are always nobody (-2). i.e. we do the same IP address checks for
27 * AUTHNULL as for AUTHUNIX, and that is done here.
28 */
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct unix_domain {
32 struct auth_domain h;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -050033#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int addr_changes;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -050035#endif /* CONFIG_NFSD_DEPRECATED */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 /* other stuff later */
37};
38
NeilBrownefc36aa2006-03-27 01:14:59 -080039extern struct auth_ops svcauth_unix;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041struct auth_domain *unix_domain_find(char *name)
42{
NeilBrownefc36aa2006-03-27 01:14:59 -080043 struct auth_domain *rv;
44 struct unix_domain *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownefc36aa2006-03-27 01:14:59 -080046 rv = auth_domain_lookup(name, NULL);
47 while(1) {
NeilBrownad1b5222006-03-27 01:15:11 -080048 if (rv) {
49 if (new && rv != &new->h)
50 auth_domain_put(&new->h);
51
52 if (rv->flavour != &svcauth_unix) {
53 auth_domain_put(rv);
54 return NULL;
55 }
NeilBrownefc36aa2006-03-27 01:14:59 -080056 return rv;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
NeilBrownefc36aa2006-03-27 01:14:59 -080059 new = kmalloc(sizeof(*new), GFP_KERNEL);
60 if (new == NULL)
61 return NULL;
62 kref_init(&new->h.ref);
63 new->h.name = kstrdup(name, GFP_KERNEL);
NeilBrowndd08d6e2006-12-13 00:35:44 -080064 if (new->h.name == NULL) {
65 kfree(new);
66 return NULL;
67 }
NeilBrownefc36aa2006-03-27 01:14:59 -080068 new->h.flavour = &svcauth_unix;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -050069#ifdef CONFIG_NFSD_DEPRECATED
NeilBrownefc36aa2006-03-27 01:14:59 -080070 new->addr_changes = 0;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -050071#endif /* CONFIG_NFSD_DEPRECATED */
NeilBrownefc36aa2006-03-27 01:14:59 -080072 rv = auth_domain_lookup(name, &new->h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Trond Myklebust24c37672008-12-23 16:30:12 -050075EXPORT_SYMBOL_GPL(unix_domain_find);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static void svcauth_unix_domain_release(struct auth_domain *dom)
78{
79 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
80
81 kfree(dom->name);
82 kfree(ud);
83}
84
85
86/**************************************************
87 * cache for IP address to unix_domain
88 * as needed by AUTH_UNIX
89 */
90#define IP_HASHBITS 8
91#define IP_HASHMAX (1<<IP_HASHBITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93struct ip_map {
94 struct cache_head h;
95 char m_class[8]; /* e.g. "nfsd" */
Aurélien Charbonf15364b2008-01-18 15:50:56 +010096 struct in6_addr m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct unix_domain *m_client;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -050098#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int m_add_change;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500100#endif /* CONFIG_NFSD_DEPRECATED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
NeilBrownbaab9352006-03-27 01:15:09 -0800103static void ip_map_put(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
NeilBrownbaab9352006-03-27 01:15:09 -0800105 struct cache_head *item = container_of(kref, struct cache_head, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct ip_map *im = container_of(item, struct ip_map,h);
NeilBrownbaab9352006-03-27 01:15:09 -0800107
108 if (test_bit(CACHE_VALID, &item->flags) &&
109 !test_bit(CACHE_NEGATIVE, &item->flags))
110 auth_domain_put(&im->m_client->h);
111 kfree(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
NeilBrown1f1e0302006-01-06 00:09:49 -0800114#if IP_HASHBITS == 8
115/* hash_long on a 64 bit machine is currently REALLY BAD for
116 * IP addresses in reverse-endian (i.e. on a little-endian machine).
117 * So use a trivial but reliable hash instead
118 */
Al Viro48061262006-11-08 00:22:34 -0800119static inline int hash_ip(__be32 ip)
NeilBrown1f1e0302006-01-06 00:09:49 -0800120{
Al Viro48061262006-11-08 00:22:34 -0800121 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
NeilBrown1f1e0302006-01-06 00:09:49 -0800122 return (hash ^ (hash>>8)) & 0xff;
123}
124#endif
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100125static inline int hash_ip6(struct in6_addr ip)
126{
127 return (hash_ip(ip.s6_addr32[0]) ^
128 hash_ip(ip.s6_addr32[1]) ^
129 hash_ip(ip.s6_addr32[2]) ^
130 hash_ip(ip.s6_addr32[3]));
131}
NeilBrown1a9917c2006-03-27 01:15:02 -0800132static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
133{
134 struct ip_map *orig = container_of(corig, struct ip_map, h);
135 struct ip_map *new = container_of(cnew, struct ip_map, h);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800136 return strcmp(orig->m_class, new->m_class) == 0 &&
137 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800138}
139static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
140{
141 struct ip_map *new = container_of(cnew, struct ip_map, h);
142 struct ip_map *item = container_of(citem, struct ip_map, h);
NeilBrown1f1e0302006-01-06 00:09:49 -0800143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 strcpy(new->m_class, item->m_class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100145 ipv6_addr_copy(&new->m_addr, &item->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
NeilBrown1a9917c2006-03-27 01:15:02 -0800147static void update(struct cache_head *cnew, struct cache_head *citem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
NeilBrown1a9917c2006-03-27 01:15:02 -0800149 struct ip_map *new = container_of(cnew, struct ip_map, h);
150 struct ip_map *item = container_of(citem, struct ip_map, h);
151
NeilBrownefc36aa2006-03-27 01:14:59 -0800152 kref_get(&item->m_client->h.ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 new->m_client = item->m_client;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500154#ifdef CONFIG_NFSD_DEPRECATED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 new->m_add_change = item->m_add_change;
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500156#endif /* CONFIG_NFSD_DEPRECATED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
NeilBrown1a9917c2006-03-27 01:15:02 -0800158static struct cache_head *ip_map_alloc(void)
159{
160 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
161 if (i)
162 return &i->h;
163 else
164 return NULL;
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static void ip_map_request(struct cache_detail *cd,
168 struct cache_head *h,
169 char **bpp, int *blen)
170{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100171 char text_addr[40];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct ip_map *im = container_of(h, struct ip_map, h);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800173
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100174 if (ipv6_addr_v4mapped(&(im->m_addr))) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700175 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100176 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700177 snprintf(text_addr, 40, "%pI6", &im->m_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 qword_add(bpp, blen, im->m_class);
180 qword_add(bpp, blen, text_addr);
181 (*bpp)[-1] = '\n';
182}
183
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400184static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
185{
186 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
187}
188
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400189static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
190static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192static int ip_map_parse(struct cache_detail *cd,
193 char *mesg, int mlen)
194{
195 /* class ipaddress [domainname] */
196 /* should be safe just to use the start of the input buffer
197 * for scratch: */
198 char *buf = mesg;
199 int len;
NeilBrown1a9917c2006-03-27 01:15:02 -0800200 char class[8];
Chuck Lever07396052010-01-26 14:03:47 -0500201 union {
202 struct sockaddr sa;
203 struct sockaddr_in s4;
204 struct sockaddr_in6 s6;
205 } address;
206 struct sockaddr_in6 sin6;
NeilBrown1a9917c2006-03-27 01:15:02 -0800207 int err;
208
209 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct auth_domain *dom;
211 time_t expiry;
212
213 if (mesg[mlen-1] != '\n')
214 return -EINVAL;
215 mesg[mlen-1] = 0;
216
217 /* class */
NeilBrown1a9917c2006-03-27 01:15:02 -0800218 len = qword_get(&mesg, class, sizeof(class));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (len <= 0) return -EINVAL;
220
221 /* ip address */
222 len = qword_get(&mesg, buf, mlen);
223 if (len <= 0) return -EINVAL;
224
Chuck Lever07396052010-01-26 14:03:47 -0500225 if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EINVAL;
Chuck Lever07396052010-01-26 14:03:47 -0500227 switch (address.sa.sa_family) {
228 case AF_INET:
229 /* Form a mapped IPv4 address in sin6 */
Chuck Lever07396052010-01-26 14:03:47 -0500230 sin6.sin6_family = AF_INET6;
Pavel Emelyanov70dc78d2010-10-05 20:48:02 +0400231 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
232 &sin6.sin6_addr);
Chuck Lever07396052010-01-26 14:03:47 -0500233 break;
234#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
235 case AF_INET6:
236 memcpy(&sin6, &address.s6, sizeof(sin6));
237 break;
238#endif
239 default:
240 return -EINVAL;
241 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 expiry = get_expiry(&mesg);
244 if (expiry ==0)
245 return -EINVAL;
246
247 /* domainname, or empty for NEGATIVE */
248 len = qword_get(&mesg, buf, mlen);
249 if (len < 0) return -EINVAL;
250
251 if (len) {
252 dom = unix_domain_find(buf);
253 if (dom == NULL)
254 return -ENOENT;
255 } else
256 dom = NULL;
257
Chuck Lever07396052010-01-26 14:03:47 -0500258 /* IPv6 scope IDs are ignored for now */
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400259 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800260 if (ipmp) {
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400261 err = __ip_map_update(cd, ipmp,
NeilBrown1a9917c2006-03-27 01:15:02 -0800262 container_of(dom, struct unix_domain, h),
263 expiry);
264 } else
265 err = -ENOMEM;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (dom)
268 auth_domain_put(dom);
NeilBrown1a9917c2006-03-27 01:15:02 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cache_flush();
NeilBrown1a9917c2006-03-27 01:15:02 -0800271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274static int ip_map_show(struct seq_file *m,
275 struct cache_detail *cd,
276 struct cache_head *h)
277{
278 struct ip_map *im;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100279 struct in6_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 char *dom = "-no-domain-";
281
282 if (h == NULL) {
283 seq_puts(m, "#class IP domain\n");
284 return 0;
285 }
286 im = container_of(h, struct ip_map, h);
287 /* class addr domain */
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100288 ipv6_addr_copy(&addr, &im->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800290 if (test_bit(CACHE_VALID, &h->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 !test_bit(CACHE_NEGATIVE, &h->flags))
292 dom = im->m_client->h.name;
293
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100294 if (ipv6_addr_v4mapped(&addr)) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700295 seq_printf(m, "%s %pI4 %s\n",
296 im->m_class, &addr.s6_addr32[3], dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100297 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700298 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400304static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
305 struct in6_addr *addr)
NeilBrown1a9917c2006-03-27 01:15:02 -0800306{
307 struct ip_map ip;
308 struct cache_head *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
NeilBrown1a9917c2006-03-27 01:15:02 -0800310 strcpy(ip.m_class, class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100311 ipv6_addr_copy(&ip.m_addr, addr);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400312 ch = sunrpc_cache_lookup(cd, &ip.h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800313 hash_str(class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100314 hash_ip6(*addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800315
316 if (ch)
317 return container_of(ch, struct ip_map, h);
318 else
319 return NULL;
320}
321
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400322static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
323 struct in6_addr *addr)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400324{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400325 struct sunrpc_net *sn;
326
327 sn = net_generic(net, sunrpc_net_id);
328 return __ip_map_lookup(sn->ip_map_cache, class, addr);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400329}
330
331static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
332 struct unix_domain *udom, time_t expiry)
NeilBrown1a9917c2006-03-27 01:15:02 -0800333{
334 struct ip_map ip;
335 struct cache_head *ch;
336
337 ip.m_client = udom;
338 ip.h.flags = 0;
339 if (!udom)
340 set_bit(CACHE_NEGATIVE, &ip.h.flags);
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500341#ifdef CONFIG_NFSD_DEPRECATED
NeilBrown1a9917c2006-03-27 01:15:02 -0800342 else {
343 ip.m_add_change = udom->addr_changes;
344 /* if this is from the legacy set_client system call,
345 * we need m_add_change to be one higher
346 */
347 if (expiry == NEVER)
348 ip.m_add_change++;
349 }
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500350#endif /* CONFIG_NFSD_DEPRECATED */
NeilBrown1a9917c2006-03-27 01:15:02 -0800351 ip.h.expiry_time = expiry;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400352 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800353 hash_str(ipm->m_class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100354 hash_ip6(ipm->m_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800355 if (!ch)
356 return -ENOMEM;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400357 cache_put(ch, cd);
NeilBrown1a9917c2006-03-27 01:15:02 -0800358 return 0;
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400361static inline int ip_map_update(struct net *net, struct ip_map *ipm,
362 struct unix_domain *udom, time_t expiry)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400363{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400364 struct sunrpc_net *sn;
365
366 sn = net_generic(net, sunrpc_net_id);
367 return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400368}
369
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500370#ifdef CONFIG_NFSD_DEPRECATED
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400371int auth_unix_add_addr(struct net *net, struct in6_addr *addr, struct auth_domain *dom)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct unix_domain *udom;
NeilBrown1a9917c2006-03-27 01:15:02 -0800374 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
NeilBrownefc36aa2006-03-27 01:14:59 -0800376 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EINVAL;
378 udom = container_of(dom, struct unix_domain, h);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400379 ipmp = ip_map_lookup(net, "nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
NeilBrown1a9917c2006-03-27 01:15:02 -0800381 if (ipmp)
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400382 return ip_map_update(net, ipmp, udom, NEVER);
NeilBrown1a9917c2006-03-27 01:15:02 -0800383 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -ENOMEM;
385}
Trond Myklebust24c37672008-12-23 16:30:12 -0500386EXPORT_SYMBOL_GPL(auth_unix_add_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388int auth_unix_forget_old(struct auth_domain *dom)
389{
390 struct unix_domain *udom;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800391
NeilBrownefc36aa2006-03-27 01:14:59 -0800392 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -EINVAL;
394 udom = container_of(dom, struct unix_domain, h);
395 udom->addr_changes++;
396 return 0;
397}
Trond Myklebust24c37672008-12-23 16:30:12 -0500398EXPORT_SYMBOL_GPL(auth_unix_forget_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400400struct auth_domain *auth_unix_lookup(struct net *net, struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Greg Banks40f10522006-10-02 02:17:43 -0700402 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct auth_domain *rv;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400404 struct sunrpc_net *sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400406 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400407 ipm = ip_map_lookup(net, "nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (!ipm)
410 return NULL;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400411 if (cache_check(sn->ip_map_cache, &ipm->h, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return NULL;
413
414 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
J. Bruce Fields31f7aa62010-12-24 14:03:38 -0500415 sunrpc_invalidate(&ipm->h, sn->ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 rv = NULL;
417 } else {
418 rv = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800419 kref_get(&rv->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400421 cache_put(&ipm->h, sn->ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return rv;
423}
Trond Myklebust24c37672008-12-23 16:30:12 -0500424EXPORT_SYMBOL_GPL(auth_unix_lookup);
J. Bruce Fieldsbdd5f052011-01-04 13:31:45 -0500425#endif /* CONFIG_NFSD_DEPRECATED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427void svcauth_unix_purge(void)
428{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400429 struct net *net;
430
431 for_each_net(net) {
432 struct sunrpc_net *sn;
433
434 sn = net_generic(net, sunrpc_net_id);
435 cache_purge(sn->ip_map_cache);
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
Trond Myklebust24c37672008-12-23 16:30:12 -0500438EXPORT_SYMBOL_GPL(svcauth_unix_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700440static inline struct ip_map *
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400441ip_map_cached_get(struct svc_xprt *xprt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700442{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600443 struct ip_map *ipm = NULL;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400444 struct sunrpc_net *sn;
Tom Tuckerdef13d72007-12-30 21:08:08 -0600445
446 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
447 spin_lock(&xprt->xpt_lock);
448 ipm = xprt->xpt_auth_cache;
449 if (ipm != NULL) {
450 if (!cache_valid(&ipm->h)) {
451 /*
452 * The entry has been invalidated since it was
453 * remembered, e.g. by a second mount from the
454 * same IP address.
455 */
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400456 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600457 xprt->xpt_auth_cache = NULL;
458 spin_unlock(&xprt->xpt_lock);
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400459 cache_put(&ipm->h, sn->ip_map_cache);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600460 return NULL;
461 }
462 cache_get(&ipm->h);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700463 }
Tom Tuckerdef13d72007-12-30 21:08:08 -0600464 spin_unlock(&xprt->xpt_lock);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700465 }
466 return ipm;
467}
468
469static inline void
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400470ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700471{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600472 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
473 spin_lock(&xprt->xpt_lock);
474 if (xprt->xpt_auth_cache == NULL) {
475 /* newly cached, keep the reference */
476 xprt->xpt_auth_cache = ipm;
477 ipm = NULL;
478 }
479 spin_unlock(&xprt->xpt_lock);
NeilBrown30f3dee2007-04-16 22:53:25 -0700480 }
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400481 if (ipm) {
482 struct sunrpc_net *sn;
483
484 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
485 cache_put(&ipm->h, sn->ip_map_cache);
486 }
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700487}
488
489void
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400490svcauth_unix_info_release(struct svc_xprt *xpt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700491{
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400492 struct ip_map *ipm;
493
494 ipm = xpt->xpt_auth_cache;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400495 if (ipm != NULL) {
496 struct sunrpc_net *sn;
497
498 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
499 cache_put(&ipm->h, sn->ip_map_cache);
500 }
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700501}
502
NeilBrown3fc605a2007-02-14 00:33:13 -0800503/****************************************************************************
504 * auth.unix.gid cache
505 * simple cache to map a UID to a list of GIDs
506 * because AUTH_UNIX aka AUTH_SYS has a max of 16
507 */
508#define GID_HASHBITS 8
509#define GID_HASHMAX (1<<GID_HASHBITS)
NeilBrown3fc605a2007-02-14 00:33:13 -0800510
511struct unix_gid {
512 struct cache_head h;
513 uid_t uid;
514 struct group_info *gi;
515};
516static struct cache_head *gid_table[GID_HASHMAX];
517
518static void unix_gid_put(struct kref *kref)
519{
520 struct cache_head *item = container_of(kref, struct cache_head, ref);
521 struct unix_gid *ug = container_of(item, struct unix_gid, h);
522 if (test_bit(CACHE_VALID, &item->flags) &&
523 !test_bit(CACHE_NEGATIVE, &item->flags))
524 put_group_info(ug->gi);
525 kfree(ug);
526}
527
528static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
529{
530 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
531 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
532 return orig->uid == new->uid;
533}
534static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
535{
536 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
537 struct unix_gid *item = container_of(citem, struct unix_gid, h);
538 new->uid = item->uid;
539}
540static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
541{
542 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
543 struct unix_gid *item = container_of(citem, struct unix_gid, h);
544
545 get_group_info(item->gi);
546 new->gi = item->gi;
547}
548static struct cache_head *unix_gid_alloc(void)
549{
550 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
551 if (g)
552 return &g->h;
553 else
554 return NULL;
555}
556
557static void unix_gid_request(struct cache_detail *cd,
558 struct cache_head *h,
559 char **bpp, int *blen)
560{
561 char tuid[20];
562 struct unix_gid *ug = container_of(h, struct unix_gid, h);
563
564 snprintf(tuid, 20, "%u", ug->uid);
565 qword_add(bpp, blen, tuid);
566 (*bpp)[-1] = '\n';
567}
568
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400569static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
570{
571 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
572}
573
NeilBrown3fc605a2007-02-14 00:33:13 -0800574static struct unix_gid *unix_gid_lookup(uid_t uid);
575extern struct cache_detail unix_gid_cache;
576
577static int unix_gid_parse(struct cache_detail *cd,
578 char *mesg, int mlen)
579{
580 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
581 int uid;
582 int gids;
583 int rv;
584 int i;
585 int err;
586 time_t expiry;
587 struct unix_gid ug, *ugp;
588
589 if (mlen <= 0 || mesg[mlen-1] != '\n')
590 return -EINVAL;
591 mesg[mlen-1] = 0;
592
593 rv = get_int(&mesg, &uid);
594 if (rv)
595 return -EINVAL;
596 ug.uid = uid;
597
598 expiry = get_expiry(&mesg);
599 if (expiry == 0)
600 return -EINVAL;
601
602 rv = get_int(&mesg, &gids);
603 if (rv || gids < 0 || gids > 8192)
604 return -EINVAL;
605
606 ug.gi = groups_alloc(gids);
607 if (!ug.gi)
608 return -ENOMEM;
609
610 for (i = 0 ; i < gids ; i++) {
611 int gid;
612 rv = get_int(&mesg, &gid);
613 err = -EINVAL;
614 if (rv)
615 goto out;
616 GROUP_AT(ug.gi, i) = gid;
617 }
618
619 ugp = unix_gid_lookup(uid);
620 if (ugp) {
621 struct cache_head *ch;
622 ug.h.flags = 0;
623 ug.h.expiry_time = expiry;
624 ch = sunrpc_cache_update(&unix_gid_cache,
625 &ug.h, &ugp->h,
626 hash_long(uid, GID_HASHBITS));
627 if (!ch)
628 err = -ENOMEM;
629 else {
630 err = 0;
631 cache_put(ch, &unix_gid_cache);
632 }
633 } else
634 err = -ENOMEM;
635 out:
636 if (ug.gi)
637 put_group_info(ug.gi);
638 return err;
639}
640
641static int unix_gid_show(struct seq_file *m,
642 struct cache_detail *cd,
643 struct cache_head *h)
644{
645 struct unix_gid *ug;
646 int i;
647 int glen;
648
649 if (h == NULL) {
650 seq_puts(m, "#uid cnt: gids...\n");
651 return 0;
652 }
653 ug = container_of(h, struct unix_gid, h);
654 if (test_bit(CACHE_VALID, &h->flags) &&
655 !test_bit(CACHE_NEGATIVE, &h->flags))
656 glen = ug->gi->ngroups;
657 else
658 glen = 0;
659
J. Bruce Fieldsccdb3572010-03-02 15:49:21 -0500660 seq_printf(m, "%u %d:", ug->uid, glen);
NeilBrown3fc605a2007-02-14 00:33:13 -0800661 for (i = 0; i < glen; i++)
662 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
663 seq_printf(m, "\n");
664 return 0;
665}
666
667struct cache_detail unix_gid_cache = {
668 .owner = THIS_MODULE,
669 .hash_size = GID_HASHMAX,
670 .hash_table = gid_table,
671 .name = "auth.unix.gid",
672 .cache_put = unix_gid_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400673 .cache_upcall = unix_gid_upcall,
NeilBrown3fc605a2007-02-14 00:33:13 -0800674 .cache_parse = unix_gid_parse,
675 .cache_show = unix_gid_show,
676 .match = unix_gid_match,
677 .init = unix_gid_init,
678 .update = unix_gid_update,
679 .alloc = unix_gid_alloc,
680};
681
682static struct unix_gid *unix_gid_lookup(uid_t uid)
683{
684 struct unix_gid ug;
685 struct cache_head *ch;
686
687 ug.uid = uid;
688 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
689 hash_long(uid, GID_HASHBITS));
690 if (ch)
691 return container_of(ch, struct unix_gid, h);
692 else
693 return NULL;
694}
695
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400696static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
NeilBrown3fc605a2007-02-14 00:33:13 -0800697{
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400698 struct unix_gid *ug;
699 struct group_info *gi;
700 int ret;
701
702 ug = unix_gid_lookup(uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800703 if (!ug)
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400704 return ERR_PTR(-EAGAIN);
705 ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
706 switch (ret) {
NeilBrown3fc605a2007-02-14 00:33:13 -0800707 case -ENOENT:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400708 return ERR_PTR(-ENOENT);
NeilBrown1ebede82010-08-12 17:04:07 +1000709 case -ETIMEDOUT:
710 return ERR_PTR(-ESHUTDOWN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800711 case 0:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400712 gi = get_group_info(ug->gi);
NeilBrown560ab422009-08-04 15:22:39 +1000713 cache_put(&ug->h, &unix_gid_cache);
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400714 return gi;
NeilBrown3fc605a2007-02-14 00:33:13 -0800715 default:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400716 return ERR_PTR(-EAGAIN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800717 }
718}
719
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700720int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721svcauth_unix_set_client(struct svc_rqst *rqstp)
722{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100723 struct sockaddr_in *sin;
724 struct sockaddr_in6 *sin6, sin6_storage;
NeilBrown1a9917c2006-03-27 01:15:02 -0800725 struct ip_map *ipm;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400726 struct group_info *gi;
727 struct svc_cred *cred = &rqstp->rq_cred;
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400728 struct svc_xprt *xprt = rqstp->rq_xprt;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400729 struct net *net = xprt->xpt_net;
730 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100732 switch (rqstp->rq_addr.ss_family) {
733 case AF_INET:
734 sin = svc_addr_in(rqstp);
735 sin6 = &sin6_storage;
Brian Haleyb301e822009-10-07 13:58:25 -0700736 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100737 break;
738 case AF_INET6:
739 sin6 = svc_addr_in6(rqstp);
740 break;
741 default:
742 BUG();
743 }
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rqstp->rq_client = NULL;
746 if (rqstp->rq_proc == 0)
747 return SVC_OK;
748
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400749 ipm = ip_map_cached_get(xprt);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700750 if (ipm == NULL)
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400751 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100752 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (ipm == NULL)
755 return SVC_DENIED;
756
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400757 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 default:
759 BUG();
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800760 case -ETIMEDOUT:
NeilBrown1ebede82010-08-12 17:04:07 +1000761 return SVC_CLOSE;
762 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return SVC_DROP;
764 case -ENOENT:
765 return SVC_DENIED;
766 case 0:
767 rqstp->rq_client = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800768 kref_get(&rqstp->rq_client->ref);
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400769 ip_map_cached_put(xprt, ipm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771 }
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400772
773 gi = unix_gid_find(cred->cr_uid, rqstp);
774 switch (PTR_ERR(gi)) {
775 case -EAGAIN:
776 return SVC_DROP;
NeilBrown1ebede82010-08-12 17:04:07 +1000777 case -ESHUTDOWN:
778 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400779 case -ENOENT:
780 break;
781 default:
782 put_group_info(cred->cr_group_info);
783 cred->cr_group_info = gi;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return SVC_OK;
786}
787
Trond Myklebust24c37672008-12-23 16:30:12 -0500788EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700791svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct kvec *argv = &rqstp->rq_arg.head[0];
794 struct kvec *resv = &rqstp->rq_res.head[0];
795 struct svc_cred *cred = &rqstp->rq_cred;
796
797 cred->cr_group_info = NULL;
798 rqstp->rq_client = NULL;
799
800 if (argv->iov_len < 3*4)
801 return SVC_GARBAGE;
802
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800803 if (svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dprintk("svc: bad null cred\n");
805 *authp = rpc_autherr_badcred;
806 return SVC_DENIED;
807 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700808 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dprintk("svc: bad null verf\n");
810 *authp = rpc_autherr_badverf;
811 return SVC_DENIED;
812 }
813
814 /* Signal that mapping to nobody uid/gid is required */
815 cred->cr_uid = (uid_t) -1;
816 cred->cr_gid = (gid_t) -1;
817 cred->cr_group_info = groups_alloc(0);
818 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000819 return SVC_CLOSE; /* kmalloc failure - client must retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700822 svc_putnl(resv, RPC_AUTH_NULL);
823 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Andy Adamsonc4170582007-07-17 04:04:42 -0700825 rqstp->rq_flavor = RPC_AUTH_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return SVC_OK;
827}
828
829static int
830svcauth_null_release(struct svc_rqst *rqstp)
831{
832 if (rqstp->rq_client)
833 auth_domain_put(rqstp->rq_client);
834 rqstp->rq_client = NULL;
835 if (rqstp->rq_cred.cr_group_info)
836 put_group_info(rqstp->rq_cred.cr_group_info);
837 rqstp->rq_cred.cr_group_info = NULL;
838
839 return 0; /* don't drop */
840}
841
842
843struct auth_ops svcauth_null = {
844 .name = "null",
845 .owner = THIS_MODULE,
846 .flavour = RPC_AUTH_NULL,
847 .accept = svcauth_null_accept,
848 .release = svcauth_null_release,
849 .set_client = svcauth_unix_set_client,
850};
851
852
853static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700854svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct kvec *argv = &rqstp->rq_arg.head[0];
857 struct kvec *resv = &rqstp->rq_res.head[0];
858 struct svc_cred *cred = &rqstp->rq_cred;
859 u32 slen, i;
860 int len = argv->iov_len;
861
862 cred->cr_group_info = NULL;
863 rqstp->rq_client = NULL;
864
865 if ((len -= 3*4) < 0)
866 return SVC_GARBAGE;
867
868 svc_getu32(argv); /* length */
869 svc_getu32(argv); /* time stamp */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700870 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (slen > 64 || (len -= (slen + 3)*4) < 0)
872 goto badcred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700873 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 argv->iov_len -= slen*4;
875
Alexey Dobriyan76994312006-09-26 22:28:46 -0700876 cred->cr_uid = svc_getnl(argv); /* uid */
877 cred->cr_gid = svc_getnl(argv); /* gid */
878 slen = svc_getnl(argv); /* gids length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (slen > 16 || (len -= (slen + 2)*4) < 0)
880 goto badcred;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400881 cred->cr_group_info = groups_alloc(slen);
882 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000883 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400884 for (i = 0; i < slen; i++)
885 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
Alexey Dobriyan76994312006-09-26 22:28:46 -0700886 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *authp = rpc_autherr_badverf;
888 return SVC_DENIED;
889 }
890
891 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700892 svc_putnl(resv, RPC_AUTH_NULL);
893 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Andy Adamsonc4170582007-07-17 04:04:42 -0700895 rqstp->rq_flavor = RPC_AUTH_UNIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return SVC_OK;
897
898badcred:
899 *authp = rpc_autherr_badcred;
900 return SVC_DENIED;
901}
902
903static int
904svcauth_unix_release(struct svc_rqst *rqstp)
905{
906 /* Verifier (such as it is) is already in place.
907 */
908 if (rqstp->rq_client)
909 auth_domain_put(rqstp->rq_client);
910 rqstp->rq_client = NULL;
911 if (rqstp->rq_cred.cr_group_info)
912 put_group_info(rqstp->rq_cred.cr_group_info);
913 rqstp->rq_cred.cr_group_info = NULL;
914
915 return 0;
916}
917
918
919struct auth_ops svcauth_unix = {
920 .name = "unix",
921 .owner = THIS_MODULE,
922 .flavour = RPC_AUTH_UNIX,
923 .accept = svcauth_unix_accept,
924 .release = svcauth_unix_release,
925 .domain_release = svcauth_unix_domain_release,
926 .set_client = svcauth_unix_set_client,
927};
928
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400929int ip_map_cache_create(struct net *net)
930{
931 int err = -ENOMEM;
932 struct cache_detail *cd;
933 struct cache_head **tbl;
934 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
935
936 cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
937 if (cd == NULL)
938 goto err_cd;
939
940 tbl = kzalloc(IP_HASHMAX * sizeof(struct cache_head *), GFP_KERNEL);
941 if (tbl == NULL)
942 goto err_tbl;
943
944 cd->owner = THIS_MODULE,
945 cd->hash_size = IP_HASHMAX,
946 cd->hash_table = tbl,
947 cd->name = "auth.unix.ip",
948 cd->cache_put = ip_map_put,
949 cd->cache_upcall = ip_map_upcall,
950 cd->cache_parse = ip_map_parse,
951 cd->cache_show = ip_map_show,
952 cd->match = ip_map_match,
953 cd->init = ip_map_init,
954 cd->update = update,
955 cd->alloc = ip_map_alloc,
956
957 err = cache_register_net(cd, net);
958 if (err)
959 goto err_reg;
960
961 sn->ip_map_cache = cd;
962 return 0;
963
964err_reg:
965 kfree(tbl);
966err_tbl:
967 kfree(cd);
968err_cd:
969 return err;
970}
971
972void ip_map_cache_destroy(struct net *net)
973{
974 struct sunrpc_net *sn;
975
976 sn = net_generic(net, sunrpc_net_id);
977 cache_purge(sn->ip_map_cache);
978 cache_unregister_net(sn->ip_map_cache, net);
979 kfree(sn->ip_map_cache->hash_table);
980 kfree(sn->ip_map_cache);
981}