]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sunrpc/svcauth_unix.c
nfsd4: allow removing clients not holding state
[mirror_ubuntu-bionic-kernel.git] / net / sunrpc / svcauth_unix.c
CommitLineData
1da177e4
LT
1#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>
c4170583 8#include <linux/sunrpc/gss_api.h>
1da177e4
LT
9#include <linux/err.h>
10#include <linux/seq_file.h>
11#include <linux/hash.h>
543537bd 12#include <linux/string.h>
5a0e3ad6 13#include <linux/slab.h>
7b2b1fee 14#include <net/sock.h>
f15364bd
AC
15#include <net/ipv6.h>
16#include <linux/kernel.h>
1da177e4
LT
17#define RPCDBG_FACILITY RPCDBG_AUTH
18
07396051 19#include <linux/sunrpc/clnt.h>
1da177e4 20
90d51b02
PE
21#include "netns.h"
22
1da177e4
LT
23/*
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
1da177e4
LT
31struct unix_domain {
32 struct auth_domain h;
1da177e4
LT
33 /* other stuff later */
34};
35
13e0e958 36extern struct auth_ops svcauth_null;
efc36aa5
N
37extern struct auth_ops svcauth_unix;
38
8b3e07ac
BF
39static void svcauth_unix_domain_release(struct auth_domain *dom)
40{
41 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
42
43 kfree(dom->name);
44 kfree(ud);
45}
46
1da177e4
LT
47struct auth_domain *unix_domain_find(char *name)
48{
efc36aa5
N
49 struct auth_domain *rv;
50 struct unix_domain *new = NULL;
51
52 rv = auth_domain_lookup(name, NULL);
53 while(1) {
ad1b5229
N
54 if (rv) {
55 if (new && rv != &new->h)
352b5d13 56 svcauth_unix_domain_release(&new->h);
ad1b5229
N
57
58 if (rv->flavour != &svcauth_unix) {
59 auth_domain_put(rv);
60 return NULL;
61 }
efc36aa5
N
62 return rv;
63 }
efc36aa5
N
64
65 new = kmalloc(sizeof(*new), GFP_KERNEL);
66 if (new == NULL)
67 return NULL;
68 kref_init(&new->h.ref);
69 new->h.name = kstrdup(name, GFP_KERNEL);
dd08d6ea
N
70 if (new->h.name == NULL) {
71 kfree(new);
72 return NULL;
73 }
efc36aa5 74 new->h.flavour = &svcauth_unix;
efc36aa5 75 rv = auth_domain_lookup(name, &new->h);
1da177e4 76 }
1da177e4 77}
24c3767e 78EXPORT_SYMBOL_GPL(unix_domain_find);
1da177e4 79
1da177e4
LT
80
81/**************************************************
82 * cache for IP address to unix_domain
83 * as needed by AUTH_UNIX
84 */
85#define IP_HASHBITS 8
86#define IP_HASHMAX (1<<IP_HASHBITS)
1da177e4
LT
87
88struct ip_map {
89 struct cache_head h;
90 char m_class[8]; /* e.g. "nfsd" */
f15364bd 91 struct in6_addr m_addr;
1da177e4 92 struct unix_domain *m_client;
1da177e4 93};
1da177e4 94
baab935f 95static void ip_map_put(struct kref *kref)
1da177e4 96{
baab935f 97 struct cache_head *item = container_of(kref, struct cache_head, ref);
1da177e4 98 struct ip_map *im = container_of(item, struct ip_map,h);
baab935f
N
99
100 if (test_bit(CACHE_VALID, &item->flags) &&
101 !test_bit(CACHE_NEGATIVE, &item->flags))
102 auth_domain_put(&im->m_client->h);
103 kfree(im);
1da177e4
LT
104}
105
1f1e030b
N
106#if IP_HASHBITS == 8
107/* hash_long on a 64 bit machine is currently REALLY BAD for
108 * IP addresses in reverse-endian (i.e. on a little-endian machine).
109 * So use a trivial but reliable hash instead
110 */
4806126d 111static inline int hash_ip(__be32 ip)
1f1e030b 112{
4806126d 113 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
1f1e030b
N
114 return (hash ^ (hash>>8)) & 0xff;
115}
116#endif
f15364bd
AC
117static inline int hash_ip6(struct in6_addr ip)
118{
119 return (hash_ip(ip.s6_addr32[0]) ^
120 hash_ip(ip.s6_addr32[1]) ^
121 hash_ip(ip.s6_addr32[2]) ^
122 hash_ip(ip.s6_addr32[3]));
123}
1a9917c2 124static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
1da177e4 125{
1a9917c2
N
126 struct ip_map *orig = container_of(corig, struct ip_map, h);
127 struct ip_map *new = container_of(cnew, struct ip_map, h);
f64f9e71
JP
128 return strcmp(orig->m_class, new->m_class) == 0 &&
129 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
1da177e4 130}
1a9917c2 131static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
1da177e4 132{
1a9917c2
N
133 struct ip_map *new = container_of(cnew, struct ip_map, h);
134 struct ip_map *item = container_of(citem, struct ip_map, h);
135
1da177e4 136 strcpy(new->m_class, item->m_class);
4e3fd7a0 137 new->m_addr = item->m_addr;
1da177e4 138}
1a9917c2 139static void update(struct cache_head *cnew, struct cache_head *citem)
1da177e4 140{
1a9917c2
N
141 struct ip_map *new = container_of(cnew, struct ip_map, h);
142 struct ip_map *item = container_of(citem, struct ip_map, h);
143
efc36aa5 144 kref_get(&item->m_client->h.ref);
1da177e4 145 new->m_client = item->m_client;
1da177e4 146}
1a9917c2
N
147static struct cache_head *ip_map_alloc(void)
148{
149 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
150 if (i)
151 return &i->h;
152 else
153 return NULL;
154}
1da177e4
LT
155
156static void ip_map_request(struct cache_detail *cd,
157 struct cache_head *h,
158 char **bpp, int *blen)
159{
f15364bd 160 char text_addr[40];
1da177e4 161 struct ip_map *im = container_of(h, struct ip_map, h);
1da177e4 162
f15364bd 163 if (ipv6_addr_v4mapped(&(im->m_addr))) {
21454aaa 164 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
f15364bd 165 } else {
5b095d98 166 snprintf(text_addr, 40, "%pI6", &im->m_addr);
f15364bd 167 }
1da177e4
LT
168 qword_add(bpp, blen, im->m_class);
169 qword_add(bpp, blen, text_addr);
170 (*bpp)[-1] = '\n';
171}
172
bc74b4f5
TM
173static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
174{
175 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
176}
177
bf18ab32
PE
178static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
179static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
1da177e4
LT
180
181static int ip_map_parse(struct cache_detail *cd,
182 char *mesg, int mlen)
183{
184 /* class ipaddress [domainname] */
185 /* should be safe just to use the start of the input buffer
186 * for scratch: */
187 char *buf = mesg;
188 int len;
1a9917c2 189 char class[8];
07396051
CL
190 union {
191 struct sockaddr sa;
192 struct sockaddr_in s4;
193 struct sockaddr_in6 s6;
194 } address;
195 struct sockaddr_in6 sin6;
1a9917c2
N
196 int err;
197
198 struct ip_map *ipmp;
1da177e4
LT
199 struct auth_domain *dom;
200 time_t expiry;
201
202 if (mesg[mlen-1] != '\n')
203 return -EINVAL;
204 mesg[mlen-1] = 0;
205
206 /* class */
1a9917c2 207 len = qword_get(&mesg, class, sizeof(class));
1da177e4
LT
208 if (len <= 0) return -EINVAL;
209
210 /* ip address */
211 len = qword_get(&mesg, buf, mlen);
212 if (len <= 0) return -EINVAL;
213
d05cc104 214 if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
1da177e4 215 return -EINVAL;
07396051
CL
216 switch (address.sa.sa_family) {
217 case AF_INET:
218 /* Form a mapped IPv4 address in sin6 */
07396051 219 sin6.sin6_family = AF_INET6;
70dc78da
PE
220 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
221 &sin6.sin6_addr);
07396051 222 break;
dfd56b8b 223#if IS_ENABLED(CONFIG_IPV6)
07396051
CL
224 case AF_INET6:
225 memcpy(&sin6, &address.s6, sizeof(sin6));
226 break;
227#endif
228 default:
229 return -EINVAL;
230 }
cca5172a 231
1da177e4
LT
232 expiry = get_expiry(&mesg);
233 if (expiry ==0)
234 return -EINVAL;
235
236 /* domainname, or empty for NEGATIVE */
237 len = qword_get(&mesg, buf, mlen);
238 if (len < 0) return -EINVAL;
239
240 if (len) {
241 dom = unix_domain_find(buf);
242 if (dom == NULL)
243 return -ENOENT;
244 } else
245 dom = NULL;
246
07396051 247 /* IPv6 scope IDs are ignored for now */
bf18ab32 248 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
1a9917c2 249 if (ipmp) {
bf18ab32 250 err = __ip_map_update(cd, ipmp,
1a9917c2
N
251 container_of(dom, struct unix_domain, h),
252 expiry);
1da177e4 253 } else
1a9917c2 254 err = -ENOMEM;
1da177e4 255
1da177e4
LT
256 if (dom)
257 auth_domain_put(dom);
1a9917c2 258
1da177e4 259 cache_flush();
1a9917c2 260 return err;
1da177e4
LT
261}
262
263static int ip_map_show(struct seq_file *m,
264 struct cache_detail *cd,
265 struct cache_head *h)
266{
267 struct ip_map *im;
f15364bd 268 struct in6_addr addr;
1da177e4
LT
269 char *dom = "-no-domain-";
270
271 if (h == NULL) {
272 seq_puts(m, "#class IP domain\n");
273 return 0;
274 }
275 im = container_of(h, struct ip_map, h);
276 /* class addr domain */
4e3fd7a0 277 addr = im->m_addr;
1da177e4 278
cca5172a 279 if (test_bit(CACHE_VALID, &h->flags) &&
1da177e4
LT
280 !test_bit(CACHE_NEGATIVE, &h->flags))
281 dom = im->m_client->h.name;
282
f15364bd 283 if (ipv6_addr_v4mapped(&addr)) {
21454aaa
HH
284 seq_printf(m, "%s %pI4 %s\n",
285 im->m_class, &addr.s6_addr32[3], dom);
f15364bd 286 } else {
5b095d98 287 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
f15364bd 288 }
1da177e4
LT
289 return 0;
290}
cca5172a 291
1da177e4 292
bf18ab32
PE
293static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
294 struct in6_addr *addr)
1a9917c2
N
295{
296 struct ip_map ip;
297 struct cache_head *ch;
298
299 strcpy(ip.m_class, class);
4e3fd7a0 300 ip.m_addr = *addr;
bf18ab32 301 ch = sunrpc_cache_lookup(cd, &ip.h,
1a9917c2 302 hash_str(class, IP_HASHBITS) ^
f15364bd 303 hash_ip6(*addr));
1a9917c2
N
304
305 if (ch)
306 return container_of(ch, struct ip_map, h);
307 else
308 return NULL;
309}
1da177e4 310
352114f3
PE
311static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
312 struct in6_addr *addr)
bf18ab32 313{
90d51b02
PE
314 struct sunrpc_net *sn;
315
316 sn = net_generic(net, sunrpc_net_id);
317 return __ip_map_lookup(sn->ip_map_cache, class, addr);
bf18ab32
PE
318}
319
320static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
321 struct unix_domain *udom, time_t expiry)
1a9917c2
N
322{
323 struct ip_map ip;
324 struct cache_head *ch;
325
326 ip.m_client = udom;
327 ip.h.flags = 0;
328 if (!udom)
329 set_bit(CACHE_NEGATIVE, &ip.h.flags);
1a9917c2 330 ip.h.expiry_time = expiry;
bf18ab32 331 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
1a9917c2 332 hash_str(ipm->m_class, IP_HASHBITS) ^
f15364bd 333 hash_ip6(ipm->m_addr));
1a9917c2
N
334 if (!ch)
335 return -ENOMEM;
bf18ab32 336 cache_put(ch, cd);
1a9917c2
N
337 return 0;
338}
1da177e4 339
352114f3
PE
340static inline int ip_map_update(struct net *net, struct ip_map *ipm,
341 struct unix_domain *udom, time_t expiry)
bf18ab32 342{
90d51b02
PE
343 struct sunrpc_net *sn;
344
345 sn = net_generic(net, sunrpc_net_id);
346 return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry);
bf18ab32
PE
347}
348
e5f06f72 349void svcauth_unix_purge(struct net *net)
1da177e4 350{
e5f06f72 351 struct sunrpc_net *sn;
90d51b02 352
e5f06f72
SK
353 sn = net_generic(net, sunrpc_net_id);
354 cache_purge(sn->ip_map_cache);
1da177e4 355}
24c3767e 356EXPORT_SYMBOL_GPL(svcauth_unix_purge);
1da177e4 357
7b2b1fee 358static inline struct ip_map *
3be4479f 359ip_map_cached_get(struct svc_xprt *xprt)
7b2b1fee 360{
def13d74 361 struct ip_map *ipm = NULL;
90d51b02 362 struct sunrpc_net *sn;
def13d74
TT
363
364 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
365 spin_lock(&xprt->xpt_lock);
366 ipm = xprt->xpt_auth_cache;
367 if (ipm != NULL) {
368 if (!cache_valid(&ipm->h)) {
369 /*
370 * The entry has been invalidated since it was
371 * remembered, e.g. by a second mount from the
372 * same IP address.
373 */
90d51b02 374 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
def13d74
TT
375 xprt->xpt_auth_cache = NULL;
376 spin_unlock(&xprt->xpt_lock);
90d51b02 377 cache_put(&ipm->h, sn->ip_map_cache);
def13d74
TT
378 return NULL;
379 }
380 cache_get(&ipm->h);
7b2b1fee 381 }
def13d74 382 spin_unlock(&xprt->xpt_lock);
7b2b1fee
GB
383 }
384 return ipm;
385}
386
387static inline void
3be4479f 388ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
7b2b1fee 389{
def13d74
TT
390 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
391 spin_lock(&xprt->xpt_lock);
392 if (xprt->xpt_auth_cache == NULL) {
393 /* newly cached, keep the reference */
394 xprt->xpt_auth_cache = ipm;
395 ipm = NULL;
396 }
397 spin_unlock(&xprt->xpt_lock);
30f3deee 398 }
90d51b02
PE
399 if (ipm) {
400 struct sunrpc_net *sn;
401
402 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
403 cache_put(&ipm->h, sn->ip_map_cache);
404 }
7b2b1fee
GB
405}
406
407void
e3bfca01 408svcauth_unix_info_release(struct svc_xprt *xpt)
7b2b1fee 409{
e3bfca01
PE
410 struct ip_map *ipm;
411
412 ipm = xpt->xpt_auth_cache;
90d51b02
PE
413 if (ipm != NULL) {
414 struct sunrpc_net *sn;
415
416 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
417 cache_put(&ipm->h, sn->ip_map_cache);
418 }
7b2b1fee
GB
419}
420
3fc605a2
N
421/****************************************************************************
422 * auth.unix.gid cache
423 * simple cache to map a UID to a list of GIDs
424 * because AUTH_UNIX aka AUTH_SYS has a max of 16
425 */
426#define GID_HASHBITS 8
427#define GID_HASHMAX (1<<GID_HASHBITS)
3fc605a2
N
428
429struct unix_gid {
430 struct cache_head h;
431 uid_t uid;
432 struct group_info *gi;
433};
3fc605a2
N
434
435static void unix_gid_put(struct kref *kref)
436{
437 struct cache_head *item = container_of(kref, struct cache_head, ref);
438 struct unix_gid *ug = container_of(item, struct unix_gid, h);
439 if (test_bit(CACHE_VALID, &item->flags) &&
440 !test_bit(CACHE_NEGATIVE, &item->flags))
441 put_group_info(ug->gi);
442 kfree(ug);
443}
444
445static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
446{
447 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
448 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
449 return orig->uid == new->uid;
450}
451static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
452{
453 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
454 struct unix_gid *item = container_of(citem, struct unix_gid, h);
455 new->uid = item->uid;
456}
457static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
458{
459 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
460 struct unix_gid *item = container_of(citem, struct unix_gid, h);
461
462 get_group_info(item->gi);
463 new->gi = item->gi;
464}
465static struct cache_head *unix_gid_alloc(void)
466{
467 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
468 if (g)
469 return &g->h;
470 else
471 return NULL;
472}
473
474static void unix_gid_request(struct cache_detail *cd,
475 struct cache_head *h,
476 char **bpp, int *blen)
477{
478 char tuid[20];
479 struct unix_gid *ug = container_of(h, struct unix_gid, h);
480
481 snprintf(tuid, 20, "%u", ug->uid);
482 qword_add(bpp, blen, tuid);
483 (*bpp)[-1] = '\n';
484}
485
bc74b4f5
TM
486static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
487{
488 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
489}
490
73393232 491static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid);
3fc605a2
N
492
493static int unix_gid_parse(struct cache_detail *cd,
494 char *mesg, int mlen)
495{
496 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
497 int uid;
498 int gids;
499 int rv;
500 int i;
501 int err;
502 time_t expiry;
503 struct unix_gid ug, *ugp;
504
3476964d 505 if (mesg[mlen - 1] != '\n')
3fc605a2
N
506 return -EINVAL;
507 mesg[mlen-1] = 0;
508
509 rv = get_int(&mesg, &uid);
510 if (rv)
511 return -EINVAL;
512 ug.uid = uid;
513
514 expiry = get_expiry(&mesg);
515 if (expiry == 0)
516 return -EINVAL;
517
518 rv = get_int(&mesg, &gids);
519 if (rv || gids < 0 || gids > 8192)
520 return -EINVAL;
521
522 ug.gi = groups_alloc(gids);
523 if (!ug.gi)
524 return -ENOMEM;
525
526 for (i = 0 ; i < gids ; i++) {
527 int gid;
528 rv = get_int(&mesg, &gid);
529 err = -EINVAL;
530 if (rv)
531 goto out;
532 GROUP_AT(ug.gi, i) = gid;
533 }
534
73393232 535 ugp = unix_gid_lookup(cd, uid);
3fc605a2
N
536 if (ugp) {
537 struct cache_head *ch;
538 ug.h.flags = 0;
539 ug.h.expiry_time = expiry;
73393232 540 ch = sunrpc_cache_update(cd,
3fc605a2
N
541 &ug.h, &ugp->h,
542 hash_long(uid, GID_HASHBITS));
543 if (!ch)
544 err = -ENOMEM;
545 else {
546 err = 0;
73393232 547 cache_put(ch, cd);
3fc605a2
N
548 }
549 } else
550 err = -ENOMEM;
551 out:
552 if (ug.gi)
553 put_group_info(ug.gi);
554 return err;
555}
556
557static int unix_gid_show(struct seq_file *m,
558 struct cache_detail *cd,
559 struct cache_head *h)
560{
561 struct unix_gid *ug;
562 int i;
563 int glen;
564
565 if (h == NULL) {
566 seq_puts(m, "#uid cnt: gids...\n");
567 return 0;
568 }
569 ug = container_of(h, struct unix_gid, h);
570 if (test_bit(CACHE_VALID, &h->flags) &&
571 !test_bit(CACHE_NEGATIVE, &h->flags))
572 glen = ug->gi->ngroups;
573 else
574 glen = 0;
575
ccdb357c 576 seq_printf(m, "%u %d:", ug->uid, glen);
3fc605a2
N
577 for (i = 0; i < glen; i++)
578 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
579 seq_printf(m, "\n");
580 return 0;
581}
582
73393232 583static struct cache_detail unix_gid_cache_template = {
3fc605a2
N
584 .owner = THIS_MODULE,
585 .hash_size = GID_HASHMAX,
3fc605a2
N
586 .name = "auth.unix.gid",
587 .cache_put = unix_gid_put,
bc74b4f5 588 .cache_upcall = unix_gid_upcall,
3fc605a2
N
589 .cache_parse = unix_gid_parse,
590 .cache_show = unix_gid_show,
591 .match = unix_gid_match,
592 .init = unix_gid_init,
593 .update = unix_gid_update,
594 .alloc = unix_gid_alloc,
595};
596
73393232
SK
597int unix_gid_cache_create(struct net *net)
598{
599 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
600 struct cache_detail *cd;
601 int err;
602
603 cd = cache_create_net(&unix_gid_cache_template, net);
604 if (IS_ERR(cd))
605 return PTR_ERR(cd);
606 err = cache_register_net(cd, net);
607 if (err) {
608 cache_destroy_net(cd, net);
609 return err;
610 }
611 sn->unix_gid_cache = cd;
612 return 0;
613}
614
615void unix_gid_cache_destroy(struct net *net)
616{
617 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
618 struct cache_detail *cd = sn->unix_gid_cache;
619
620 sn->unix_gid_cache = NULL;
621 cache_purge(cd);
622 cache_unregister_net(cd, net);
623 cache_destroy_net(cd, net);
624}
625
626static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid)
3fc605a2
N
627{
628 struct unix_gid ug;
629 struct cache_head *ch;
630
631 ug.uid = uid;
73393232 632 ch = sunrpc_cache_lookup(cd, &ug.h, hash_long(uid, GID_HASHBITS));
3fc605a2
N
633 if (ch)
634 return container_of(ch, struct unix_gid, h);
635 else
636 return NULL;
637}
638
dc83d6e2 639static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
3fc605a2 640{
dc83d6e2
BF
641 struct unix_gid *ug;
642 struct group_info *gi;
643 int ret;
73393232
SK
644 struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
645 sunrpc_net_id);
dc83d6e2 646
73393232 647 ug = unix_gid_lookup(sn->unix_gid_cache, uid);
3fc605a2 648 if (!ug)
dc83d6e2 649 return ERR_PTR(-EAGAIN);
73393232 650 ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
dc83d6e2 651 switch (ret) {
3fc605a2 652 case -ENOENT:
dc83d6e2 653 return ERR_PTR(-ENOENT);
1ebede86
N
654 case -ETIMEDOUT:
655 return ERR_PTR(-ESHUTDOWN);
3fc605a2 656 case 0:
dc83d6e2 657 gi = get_group_info(ug->gi);
73393232 658 cache_put(&ug->h, sn->unix_gid_cache);
dc83d6e2 659 return gi;
3fc605a2 660 default:
dc83d6e2 661 return ERR_PTR(-EAGAIN);
3fc605a2
N
662 }
663}
664
3ab4d8b1 665int
1da177e4
LT
666svcauth_unix_set_client(struct svc_rqst *rqstp)
667{
f15364bd
AC
668 struct sockaddr_in *sin;
669 struct sockaddr_in6 *sin6, sin6_storage;
1a9917c2 670 struct ip_map *ipm;
dc83d6e2
BF
671 struct group_info *gi;
672 struct svc_cred *cred = &rqstp->rq_cred;
3be4479f 673 struct svc_xprt *xprt = rqstp->rq_xprt;
90d51b02
PE
674 struct net *net = xprt->xpt_net;
675 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1da177e4 676
f15364bd
AC
677 switch (rqstp->rq_addr.ss_family) {
678 case AF_INET:
679 sin = svc_addr_in(rqstp);
680 sin6 = &sin6_storage;
b301e82c 681 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
f15364bd
AC
682 break;
683 case AF_INET6:
684 sin6 = svc_addr_in6(rqstp);
685 break;
686 default:
687 BUG();
688 }
689
1da177e4
LT
690 rqstp->rq_client = NULL;
691 if (rqstp->rq_proc == 0)
692 return SVC_OK;
693
3be4479f 694 ipm = ip_map_cached_get(xprt);
7b2b1fee 695 if (ipm == NULL)
90d51b02 696 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
f15364bd 697 &sin6->sin6_addr);
1da177e4
LT
698
699 if (ipm == NULL)
700 return SVC_DENIED;
701
90d51b02 702 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
1da177e4
LT
703 default:
704 BUG();
e0bb89ef 705 case -ETIMEDOUT:
1ebede86
N
706 return SVC_CLOSE;
707 case -EAGAIN:
1da177e4
LT
708 return SVC_DROP;
709 case -ENOENT:
710 return SVC_DENIED;
711 case 0:
712 rqstp->rq_client = &ipm->m_client->h;
efc36aa5 713 kref_get(&rqstp->rq_client->ref);
3be4479f 714 ip_map_cached_put(xprt, ipm);
1da177e4
LT
715 break;
716 }
dc83d6e2
BF
717
718 gi = unix_gid_find(cred->cr_uid, rqstp);
719 switch (PTR_ERR(gi)) {
720 case -EAGAIN:
721 return SVC_DROP;
1ebede86
N
722 case -ESHUTDOWN:
723 return SVC_CLOSE;
dc83d6e2
BF
724 case -ENOENT:
725 break;
726 default:
727 put_group_info(cred->cr_group_info);
728 cred->cr_group_info = gi;
729 }
1da177e4
LT
730 return SVC_OK;
731}
732
24c3767e 733EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
3ab4d8b1 734
1da177e4 735static int
d8ed029d 736svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
1da177e4
LT
737{
738 struct kvec *argv = &rqstp->rq_arg.head[0];
739 struct kvec *resv = &rqstp->rq_res.head[0];
740 struct svc_cred *cred = &rqstp->rq_cred;
741
742 cred->cr_group_info = NULL;
743 rqstp->rq_client = NULL;
744
745 if (argv->iov_len < 3*4)
746 return SVC_GARBAGE;
747
cca5172a 748 if (svc_getu32(argv) != 0) {
1da177e4
LT
749 dprintk("svc: bad null cred\n");
750 *authp = rpc_autherr_badcred;
751 return SVC_DENIED;
752 }
76994313 753 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
1da177e4
LT
754 dprintk("svc: bad null verf\n");
755 *authp = rpc_autherr_badverf;
756 return SVC_DENIED;
757 }
758
759 /* Signal that mapping to nobody uid/gid is required */
760 cred->cr_uid = (uid_t) -1;
761 cred->cr_gid = (gid_t) -1;
762 cred->cr_group_info = groups_alloc(0);
763 if (cred->cr_group_info == NULL)
1ebede86 764 return SVC_CLOSE; /* kmalloc failure - client must retry */
1da177e4
LT
765
766 /* Put NULL verifier */
76994313
AD
767 svc_putnl(resv, RPC_AUTH_NULL);
768 svc_putnl(resv, 0);
1da177e4 769
c4170583 770 rqstp->rq_flavor = RPC_AUTH_NULL;
1da177e4
LT
771 return SVC_OK;
772}
773
774static int
775svcauth_null_release(struct svc_rqst *rqstp)
776{
777 if (rqstp->rq_client)
778 auth_domain_put(rqstp->rq_client);
779 rqstp->rq_client = NULL;
780 if (rqstp->rq_cred.cr_group_info)
781 put_group_info(rqstp->rq_cred.cr_group_info);
782 rqstp->rq_cred.cr_group_info = NULL;
783
784 return 0; /* don't drop */
785}
786
787
788struct auth_ops svcauth_null = {
789 .name = "null",
790 .owner = THIS_MODULE,
791 .flavour = RPC_AUTH_NULL,
792 .accept = svcauth_null_accept,
793 .release = svcauth_null_release,
794 .set_client = svcauth_unix_set_client,
795};
796
797
798static int
d8ed029d 799svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
1da177e4
LT
800{
801 struct kvec *argv = &rqstp->rq_arg.head[0];
802 struct kvec *resv = &rqstp->rq_res.head[0];
803 struct svc_cred *cred = &rqstp->rq_cred;
804 u32 slen, i;
805 int len = argv->iov_len;
806
807 cred->cr_group_info = NULL;
808 rqstp->rq_client = NULL;
809
810 if ((len -= 3*4) < 0)
811 return SVC_GARBAGE;
812
813 svc_getu32(argv); /* length */
814 svc_getu32(argv); /* time stamp */
76994313 815 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
1da177e4
LT
816 if (slen > 64 || (len -= (slen + 3)*4) < 0)
817 goto badcred;
d8ed029d 818 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
1da177e4
LT
819 argv->iov_len -= slen*4;
820
76994313
AD
821 cred->cr_uid = svc_getnl(argv); /* uid */
822 cred->cr_gid = svc_getnl(argv); /* gid */
823 slen = svc_getnl(argv); /* gids length */
1da177e4
LT
824 if (slen > 16 || (len -= (slen + 2)*4) < 0)
825 goto badcred;
dc83d6e2
BF
826 cred->cr_group_info = groups_alloc(slen);
827 if (cred->cr_group_info == NULL)
1ebede86 828 return SVC_CLOSE;
dc83d6e2
BF
829 for (i = 0; i < slen; i++)
830 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
76994313 831 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
1da177e4
LT
832 *authp = rpc_autherr_badverf;
833 return SVC_DENIED;
834 }
835
836 /* Put NULL verifier */
76994313
AD
837 svc_putnl(resv, RPC_AUTH_NULL);
838 svc_putnl(resv, 0);
1da177e4 839
c4170583 840 rqstp->rq_flavor = RPC_AUTH_UNIX;
1da177e4
LT
841 return SVC_OK;
842
843badcred:
844 *authp = rpc_autherr_badcred;
845 return SVC_DENIED;
846}
847
848static int
849svcauth_unix_release(struct svc_rqst *rqstp)
850{
851 /* Verifier (such as it is) is already in place.
852 */
853 if (rqstp->rq_client)
854 auth_domain_put(rqstp->rq_client);
855 rqstp->rq_client = NULL;
856 if (rqstp->rq_cred.cr_group_info)
857 put_group_info(rqstp->rq_cred.cr_group_info);
858 rqstp->rq_cred.cr_group_info = NULL;
859
860 return 0;
861}
862
863
864struct auth_ops svcauth_unix = {
865 .name = "unix",
866 .owner = THIS_MODULE,
867 .flavour = RPC_AUTH_UNIX,
868 .accept = svcauth_unix_accept,
869 .release = svcauth_unix_release,
870 .domain_release = svcauth_unix_domain_release,
871 .set_client = svcauth_unix_set_client,
872};
873
d05cc104
SK
874static struct cache_detail ip_map_cache_template = {
875 .owner = THIS_MODULE,
876 .hash_size = IP_HASHMAX,
877 .name = "auth.unix.ip",
878 .cache_put = ip_map_put,
879 .cache_upcall = ip_map_upcall,
880 .cache_parse = ip_map_parse,
881 .cache_show = ip_map_show,
882 .match = ip_map_match,
883 .init = ip_map_init,
884 .update = update,
885 .alloc = ip_map_alloc,
886};
887
90d51b02
PE
888int ip_map_cache_create(struct net *net)
889{
90d51b02 890 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
d05cc104
SK
891 struct cache_detail *cd;
892 int err;
90d51b02 893
d05cc104
SK
894 cd = cache_create_net(&ip_map_cache_template, net);
895 if (IS_ERR(cd))
896 return PTR_ERR(cd);
90d51b02 897 err = cache_register_net(cd, net);
d05cc104
SK
898 if (err) {
899 cache_destroy_net(cd, net);
900 return err;
901 }
90d51b02
PE
902 sn->ip_map_cache = cd;
903 return 0;
90d51b02
PE
904}
905
906void ip_map_cache_destroy(struct net *net)
907{
d05cc104
SK
908 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
909 struct cache_detail *cd = sn->ip_map_cache;
90d51b02 910
d05cc104
SK
911 sn->ip_map_cache = NULL;
912 cache_purge(cd);
913 cache_unregister_net(cd, net);
914 cache_destroy_net(cd, net);
90d51b02 915}