1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/sched.h>
4 #include <linux/module.h>
5 #include <linux/sunrpc/types.h>
6 #include <linux/sunrpc/xdr.h>
7 #include <linux/sunrpc/svcsock.h>
8 #include <linux/sunrpc/svcauth.h>
9 #include <linux/sunrpc/gss_api.h>
10 #include <linux/sunrpc/addr.h>
11 #include <linux/err.h>
12 #include <linux/seq_file.h>
13 #include <linux/hash.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/user_namespace.h>
20 #define RPCDBG_FACILITY RPCDBG_AUTH
26 * AUTHUNIX and AUTHNULL credentials are both handled here.
27 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
28 * are always nobody (-2). i.e. we do the same IP address checks for
29 * AUTHNULL as for AUTHUNIX, and that is done here.
35 /* other stuff later */
38 extern struct auth_ops svcauth_null;
39 extern struct auth_ops svcauth_unix;
40 extern struct auth_ops svcauth_tls;
42 static void svcauth_unix_domain_release_rcu(struct rcu_head *head)
44 struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
45 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
51 static void svcauth_unix_domain_release(struct auth_domain *dom)
53 call_rcu(&dom->rcu_head, svcauth_unix_domain_release_rcu);
56 struct auth_domain *unix_domain_find(char *name)
58 struct auth_domain *rv;
59 struct unix_domain *new = NULL;
61 rv = auth_domain_find(name);
64 if (new && rv != &new->h)
65 svcauth_unix_domain_release(&new->h);
67 if (rv->flavour != &svcauth_unix) {
74 new = kmalloc(sizeof(*new), GFP_KERNEL);
77 kref_init(&new->h.ref);
78 new->h.name = kstrdup(name, GFP_KERNEL);
79 if (new->h.name == NULL) {
83 new->h.flavour = &svcauth_unix;
84 rv = auth_domain_lookup(name, &new->h);
87 EXPORT_SYMBOL_GPL(unix_domain_find);
90 /**************************************************
91 * cache for IP address to unix_domain
92 * as needed by AUTH_UNIX
95 #define IP_HASHMAX (1<<IP_HASHBITS)
99 char m_class[8]; /* e.g. "nfsd" */
100 struct in6_addr m_addr;
101 struct unix_domain *m_client;
102 struct rcu_head m_rcu;
105 static void ip_map_put(struct kref *kref)
107 struct cache_head *item = container_of(kref, struct cache_head, ref);
108 struct ip_map *im = container_of(item, struct ip_map,h);
110 if (test_bit(CACHE_VALID, &item->flags) &&
111 !test_bit(CACHE_NEGATIVE, &item->flags))
112 auth_domain_put(&im->m_client->h);
113 kfree_rcu(im, m_rcu);
116 static inline int hash_ip6(const struct in6_addr *ip)
118 return hash_32(ipv6_addr_hash(ip), IP_HASHBITS);
120 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
122 struct ip_map *orig = container_of(corig, struct ip_map, h);
123 struct ip_map *new = container_of(cnew, struct ip_map, h);
124 return strcmp(orig->m_class, new->m_class) == 0 &&
125 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
127 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
129 struct ip_map *new = container_of(cnew, struct ip_map, h);
130 struct ip_map *item = container_of(citem, struct ip_map, h);
132 strcpy(new->m_class, item->m_class);
133 new->m_addr = item->m_addr;
135 static void update(struct cache_head *cnew, struct cache_head *citem)
137 struct ip_map *new = container_of(cnew, struct ip_map, h);
138 struct ip_map *item = container_of(citem, struct ip_map, h);
140 kref_get(&item->m_client->h.ref);
141 new->m_client = item->m_client;
143 static struct cache_head *ip_map_alloc(void)
145 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
152 static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
154 return sunrpc_cache_pipe_upcall(cd, h);
157 static void ip_map_request(struct cache_detail *cd,
158 struct cache_head *h,
159 char **bpp, int *blen)
162 struct ip_map *im = container_of(h, struct ip_map, h);
164 if (ipv6_addr_v4mapped(&(im->m_addr))) {
165 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
167 snprintf(text_addr, 40, "%pI6", &im->m_addr);
169 qword_add(bpp, blen, im->m_class);
170 qword_add(bpp, blen, text_addr);
174 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
175 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time64_t expiry);
177 static int ip_map_parse(struct cache_detail *cd,
178 char *mesg, int mlen)
180 /* class ipaddress [domainname] */
181 /* should be safe just to use the start of the input buffer
188 struct sockaddr_in s4;
189 struct sockaddr_in6 s6;
191 struct sockaddr_in6 sin6;
195 struct auth_domain *dom;
198 if (mesg[mlen-1] != '\n')
203 len = qword_get(&mesg, class, sizeof(class));
204 if (len <= 0) return -EINVAL;
207 len = qword_get(&mesg, buf, mlen);
208 if (len <= 0) return -EINVAL;
210 if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
212 switch (address.sa.sa_family) {
214 /* Form a mapped IPv4 address in sin6 */
215 sin6.sin6_family = AF_INET6;
216 ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
219 #if IS_ENABLED(CONFIG_IPV6)
221 memcpy(&sin6, &address.s6, sizeof(sin6));
228 expiry = get_expiry(&mesg);
232 /* domainname, or empty for NEGATIVE */
233 len = qword_get(&mesg, buf, mlen);
234 if (len < 0) return -EINVAL;
237 dom = unix_domain_find(buf);
243 /* IPv6 scope IDs are ignored for now */
244 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
246 err = __ip_map_update(cd, ipmp,
247 container_of(dom, struct unix_domain, h),
253 auth_domain_put(dom);
259 static int ip_map_show(struct seq_file *m,
260 struct cache_detail *cd,
261 struct cache_head *h)
264 struct in6_addr addr;
265 char *dom = "-no-domain-";
268 seq_puts(m, "#class IP domain\n");
271 im = container_of(h, struct ip_map, h);
272 /* class addr domain */
275 if (test_bit(CACHE_VALID, &h->flags) &&
276 !test_bit(CACHE_NEGATIVE, &h->flags))
277 dom = im->m_client->h.name;
279 if (ipv6_addr_v4mapped(&addr)) {
280 seq_printf(m, "%s %pI4 %s\n",
281 im->m_class, &addr.s6_addr32[3], dom);
283 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
289 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
290 struct in6_addr *addr)
293 struct cache_head *ch;
295 strcpy(ip.m_class, class);
297 ch = sunrpc_cache_lookup_rcu(cd, &ip.h,
298 hash_str(class, IP_HASHBITS) ^
302 return container_of(ch, struct ip_map, h);
307 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
308 struct unix_domain *udom, time64_t expiry)
311 struct cache_head *ch;
316 set_bit(CACHE_NEGATIVE, &ip.h.flags);
317 ip.h.expiry_time = expiry;
318 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
319 hash_str(ipm->m_class, IP_HASHBITS) ^
320 hash_ip6(&ipm->m_addr));
327 void svcauth_unix_purge(struct net *net)
329 struct sunrpc_net *sn;
331 sn = net_generic(net, sunrpc_net_id);
332 cache_purge(sn->ip_map_cache);
334 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
336 static inline struct ip_map *
337 ip_map_cached_get(struct svc_xprt *xprt)
339 struct ip_map *ipm = NULL;
340 struct sunrpc_net *sn;
342 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
343 spin_lock(&xprt->xpt_lock);
344 ipm = xprt->xpt_auth_cache;
346 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
347 if (cache_is_expired(sn->ip_map_cache, &ipm->h)) {
349 * The entry has been invalidated since it was
350 * remembered, e.g. by a second mount from the
353 xprt->xpt_auth_cache = NULL;
354 spin_unlock(&xprt->xpt_lock);
355 cache_put(&ipm->h, sn->ip_map_cache);
360 spin_unlock(&xprt->xpt_lock);
366 ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
368 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
369 spin_lock(&xprt->xpt_lock);
370 if (xprt->xpt_auth_cache == NULL) {
371 /* newly cached, keep the reference */
372 xprt->xpt_auth_cache = ipm;
375 spin_unlock(&xprt->xpt_lock);
378 struct sunrpc_net *sn;
380 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
381 cache_put(&ipm->h, sn->ip_map_cache);
386 svcauth_unix_info_release(struct svc_xprt *xpt)
390 ipm = xpt->xpt_auth_cache;
392 struct sunrpc_net *sn;
394 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
395 cache_put(&ipm->h, sn->ip_map_cache);
399 /****************************************************************************
400 * auth.unix.gid cache
401 * simple cache to map a UID to a list of GIDs
402 * because AUTH_UNIX aka AUTH_SYS has a max of UNX_NGROUPS
404 #define GID_HASHBITS 8
405 #define GID_HASHMAX (1<<GID_HASHBITS)
410 struct group_info *gi;
414 static int unix_gid_hash(kuid_t uid)
416 return hash_long(from_kuid(&init_user_ns, uid), GID_HASHBITS);
419 static void unix_gid_free(struct rcu_head *rcu)
421 struct unix_gid *ug = container_of(rcu, struct unix_gid, rcu);
422 struct cache_head *item = &ug->h;
424 if (test_bit(CACHE_VALID, &item->flags) &&
425 !test_bit(CACHE_NEGATIVE, &item->flags))
426 put_group_info(ug->gi);
430 static void unix_gid_put(struct kref *kref)
432 struct cache_head *item = container_of(kref, struct cache_head, ref);
433 struct unix_gid *ug = container_of(item, struct unix_gid, h);
435 call_rcu(&ug->rcu, unix_gid_free);
438 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
440 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
441 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
442 return uid_eq(orig->uid, new->uid);
444 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
446 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
447 struct unix_gid *item = container_of(citem, struct unix_gid, h);
448 new->uid = item->uid;
450 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
452 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
453 struct unix_gid *item = container_of(citem, struct unix_gid, h);
455 get_group_info(item->gi);
458 static struct cache_head *unix_gid_alloc(void)
460 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
467 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
469 return sunrpc_cache_pipe_upcall_timeout(cd, h);
472 static void unix_gid_request(struct cache_detail *cd,
473 struct cache_head *h,
474 char **bpp, int *blen)
477 struct unix_gid *ug = container_of(h, struct unix_gid, h);
479 snprintf(tuid, 20, "%u", from_kuid(&init_user_ns, ug->uid));
480 qword_add(bpp, blen, tuid);
484 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid);
486 static int unix_gid_parse(struct cache_detail *cd,
487 char *mesg, int mlen)
489 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
497 struct unix_gid ug, *ugp;
499 if (mesg[mlen - 1] != '\n')
503 rv = get_int(&mesg, &id);
506 uid = make_kuid(current_user_ns(), id);
509 expiry = get_expiry(&mesg);
513 rv = get_int(&mesg, &gids);
514 if (rv || gids < 0 || gids > 8192)
517 ug.gi = groups_alloc(gids);
521 for (i = 0 ; i < gids ; i++) {
524 rv = get_int(&mesg, &gid);
528 kgid = make_kgid(current_user_ns(), gid);
529 if (!gid_valid(kgid))
531 ug.gi->gid[i] = kgid;
535 ugp = unix_gid_lookup(cd, uid);
537 struct cache_head *ch;
539 ug.h.expiry_time = expiry;
540 ch = sunrpc_cache_update(cd,
553 put_group_info(ug.gi);
557 static int unix_gid_show(struct seq_file *m,
558 struct cache_detail *cd,
559 struct cache_head *h)
561 struct user_namespace *user_ns = m->file->f_cred->user_ns;
567 seq_puts(m, "#uid cnt: gids...\n");
570 ug = container_of(h, struct unix_gid, h);
571 if (test_bit(CACHE_VALID, &h->flags) &&
572 !test_bit(CACHE_NEGATIVE, &h->flags))
573 glen = ug->gi->ngroups;
577 seq_printf(m, "%u %d:", from_kuid_munged(user_ns, ug->uid), glen);
578 for (i = 0; i < glen; i++)
579 seq_printf(m, " %d", from_kgid_munged(user_ns, ug->gi->gid[i]));
584 static const struct cache_detail unix_gid_cache_template = {
585 .owner = THIS_MODULE,
586 .hash_size = GID_HASHMAX,
587 .name = "auth.unix.gid",
588 .cache_put = unix_gid_put,
589 .cache_upcall = unix_gid_upcall,
590 .cache_request = unix_gid_request,
591 .cache_parse = unix_gid_parse,
592 .cache_show = unix_gid_show,
593 .match = unix_gid_match,
594 .init = unix_gid_init,
595 .update = unix_gid_update,
596 .alloc = unix_gid_alloc,
599 int unix_gid_cache_create(struct net *net)
601 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
602 struct cache_detail *cd;
605 cd = cache_create_net(&unix_gid_cache_template, net);
608 err = cache_register_net(cd, net);
610 cache_destroy_net(cd, net);
613 sn->unix_gid_cache = cd;
617 void unix_gid_cache_destroy(struct net *net)
619 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
620 struct cache_detail *cd = sn->unix_gid_cache;
622 sn->unix_gid_cache = NULL;
624 cache_unregister_net(cd, net);
625 cache_destroy_net(cd, net);
628 static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, kuid_t uid)
631 struct cache_head *ch;
634 ch = sunrpc_cache_lookup_rcu(cd, &ug.h, unix_gid_hash(uid));
636 return container_of(ch, struct unix_gid, h);
641 static struct group_info *unix_gid_find(kuid_t uid, struct svc_rqst *rqstp)
644 struct group_info *gi;
646 struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
649 ug = unix_gid_lookup(sn->unix_gid_cache, uid);
651 return ERR_PTR(-EAGAIN);
652 ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
655 return ERR_PTR(-ENOENT);
657 return ERR_PTR(-ESHUTDOWN);
659 gi = get_group_info(ug->gi);
660 cache_put(&ug->h, sn->unix_gid_cache);
663 return ERR_PTR(-EAGAIN);
668 svcauth_unix_set_client(struct svc_rqst *rqstp)
670 struct sockaddr_in *sin;
671 struct sockaddr_in6 *sin6, sin6_storage;
673 struct group_info *gi;
674 struct svc_cred *cred = &rqstp->rq_cred;
675 struct svc_xprt *xprt = rqstp->rq_xprt;
676 struct net *net = xprt->xpt_net;
677 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
679 switch (rqstp->rq_addr.ss_family) {
681 sin = svc_addr_in(rqstp);
682 sin6 = &sin6_storage;
683 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
686 sin6 = svc_addr_in6(rqstp);
692 rqstp->rq_client = NULL;
693 if (rqstp->rq_proc == 0)
696 rqstp->rq_auth_stat = rpc_autherr_badcred;
697 ipm = ip_map_cached_get(xprt);
699 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
705 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
715 rqstp->rq_client = &ipm->m_client->h;
716 kref_get(&rqstp->rq_client->ref);
717 ip_map_cached_put(xprt, ipm);
721 gi = unix_gid_find(cred->cr_uid, rqstp);
722 switch (PTR_ERR(gi)) {
730 put_group_info(cred->cr_group_info);
731 cred->cr_group_info = gi;
735 rqstp->rq_auth_stat = rpc_auth_ok;
739 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
742 * svcauth_null_accept - Decode and validate incoming RPC_AUTH_NULL credential
743 * @rqstp: RPC transaction
746 * %SVC_OK: Both credential and verifier are valid
747 * %SVC_DENIED: Credential or verifier is not valid
748 * %SVC_GARBAGE: Failed to decode credential or verifier
749 * %SVC_CLOSE: Temporary failure
751 * rqstp->rq_auth_stat is set as mandated by RFC 5531.
754 svcauth_null_accept(struct svc_rqst *rqstp)
756 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
757 struct svc_cred *cred = &rqstp->rq_cred;
761 /* Length of Call's credential body field: */
762 if (xdr_stream_decode_u32(xdr, &len) < 0)
765 rqstp->rq_auth_stat = rpc_autherr_badcred;
769 /* Call's verf field: */
770 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
772 if (flavor != RPC_AUTH_NULL || len != 0) {
773 rqstp->rq_auth_stat = rpc_autherr_badverf;
777 /* Signal that mapping to nobody uid/gid is required */
778 cred->cr_uid = INVALID_UID;
779 cred->cr_gid = INVALID_GID;
780 cred->cr_group_info = groups_alloc(0);
781 if (cred->cr_group_info == NULL)
782 return SVC_CLOSE; /* kmalloc failure - client must retry */
784 if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
785 RPC_AUTH_NULL, NULL, 0) < 0)
787 if (!svcxdr_set_accept_stat(rqstp))
790 rqstp->rq_cred.cr_flavor = RPC_AUTH_NULL;
795 svcauth_null_release(struct svc_rqst *rqstp)
797 if (rqstp->rq_client)
798 auth_domain_put(rqstp->rq_client);
799 rqstp->rq_client = NULL;
800 if (rqstp->rq_cred.cr_group_info)
801 put_group_info(rqstp->rq_cred.cr_group_info);
802 rqstp->rq_cred.cr_group_info = NULL;
804 return 0; /* don't drop */
808 struct auth_ops svcauth_null = {
810 .owner = THIS_MODULE,
811 .flavour = RPC_AUTH_NULL,
812 .accept = svcauth_null_accept,
813 .release = svcauth_null_release,
814 .set_client = svcauth_unix_set_client,
819 * svcauth_tls_accept - Decode and validate incoming RPC_AUTH_TLS credential
820 * @rqstp: RPC transaction
823 * %SVC_OK: Both credential and verifier are valid
824 * %SVC_DENIED: Credential or verifier is not valid
825 * %SVC_GARBAGE: Failed to decode credential or verifier
826 * %SVC_CLOSE: Temporary failure
828 * rqstp->rq_auth_stat is set as mandated by RFC 5531.
831 svcauth_tls_accept(struct svc_rqst *rqstp)
833 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
834 struct svc_cred *cred = &rqstp->rq_cred;
839 /* Length of Call's credential body field: */
840 if (xdr_stream_decode_u32(xdr, &len) < 0)
843 rqstp->rq_auth_stat = rpc_autherr_badcred;
847 /* Call's verf field: */
848 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
850 if (flavor != RPC_AUTH_NULL || len != 0) {
851 rqstp->rq_auth_stat = rpc_autherr_badverf;
855 /* AUTH_TLS is not valid on non-NULL procedures */
856 if (rqstp->rq_proc != 0) {
857 rqstp->rq_auth_stat = rpc_autherr_badcred;
861 /* Signal that mapping to nobody uid/gid is required */
862 cred->cr_uid = INVALID_UID;
863 cred->cr_gid = INVALID_GID;
864 cred->cr_group_info = groups_alloc(0);
865 if (cred->cr_group_info == NULL)
868 if (rqstp->rq_xprt->xpt_ops->xpo_start_tls) {
869 p = xdr_reserve_space(&rqstp->rq_res_stream, XDR_UNIT * 2 + 8);
872 *p++ = rpc_auth_null;
873 *p++ = cpu_to_be32(8);
874 memcpy(p, "STARTTLS", 8);
876 if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
877 RPC_AUTH_NULL, NULL, 0) < 0)
880 if (!svcxdr_set_accept_stat(rqstp))
883 rqstp->rq_cred.cr_flavor = RPC_AUTH_TLS;
887 struct auth_ops svcauth_tls = {
889 .owner = THIS_MODULE,
890 .flavour = RPC_AUTH_TLS,
891 .accept = svcauth_tls_accept,
892 .release = svcauth_null_release,
893 .set_client = svcauth_unix_set_client,
898 * svcauth_unix_accept - Decode and validate incoming RPC_AUTH_SYS credential
899 * @rqstp: RPC transaction
902 * %SVC_OK: Both credential and verifier are valid
903 * %SVC_DENIED: Credential or verifier is not valid
904 * %SVC_GARBAGE: Failed to decode credential or verifier
905 * %SVC_CLOSE: Temporary failure
907 * rqstp->rq_auth_stat is set as mandated by RFC 5531.
910 svcauth_unix_accept(struct svc_rqst *rqstp)
912 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
913 struct svc_cred *cred = &rqstp->rq_cred;
914 struct user_namespace *userns;
920 * This implementation ignores the length of the Call's
921 * credential body field and the timestamp and machinename
924 p = xdr_inline_decode(xdr, XDR_UNIT * 3);
927 len = be32_to_cpup(p + 2);
928 if (len > RPC_MAX_MACHINENAME)
930 if (!xdr_inline_decode(xdr, len))
934 * Note: we skip uid_valid()/gid_valid() checks here for
935 * backwards compatibility with clients that use -1 id's.
936 * Instead, -1 uid or gid is later mapped to the
937 * (export-specific) anonymous id by nfsd_setuser.
938 * Supplementary gid's will be left alone.
940 userns = (rqstp->rq_xprt && rqstp->rq_xprt->xpt_cred) ?
941 rqstp->rq_xprt->xpt_cred->user_ns : &init_user_ns;
942 if (xdr_stream_decode_u32(xdr, &i) < 0)
944 cred->cr_uid = make_kuid(userns, i);
945 if (xdr_stream_decode_u32(xdr, &i) < 0)
947 cred->cr_gid = make_kgid(userns, i);
949 if (xdr_stream_decode_u32(xdr, &len) < 0)
951 if (len > UNX_NGROUPS)
953 p = xdr_inline_decode(xdr, XDR_UNIT * len);
956 cred->cr_group_info = groups_alloc(len);
957 if (cred->cr_group_info == NULL)
959 for (i = 0; i < len; i++) {
960 kgid_t kgid = make_kgid(userns, be32_to_cpup(p++));
961 cred->cr_group_info->gid[i] = kgid;
963 groups_sort(cred->cr_group_info);
965 /* Call's verf field: */
966 if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
968 if (flavor != RPC_AUTH_NULL || len != 0) {
969 rqstp->rq_auth_stat = rpc_autherr_badverf;
973 if (xdr_stream_encode_opaque_auth(&rqstp->rq_res_stream,
974 RPC_AUTH_NULL, NULL, 0) < 0)
976 if (!svcxdr_set_accept_stat(rqstp))
979 rqstp->rq_cred.cr_flavor = RPC_AUTH_UNIX;
983 rqstp->rq_auth_stat = rpc_autherr_badcred;
988 svcauth_unix_release(struct svc_rqst *rqstp)
990 /* Verifier (such as it is) is already in place.
992 if (rqstp->rq_client)
993 auth_domain_put(rqstp->rq_client);
994 rqstp->rq_client = NULL;
995 if (rqstp->rq_cred.cr_group_info)
996 put_group_info(rqstp->rq_cred.cr_group_info);
997 rqstp->rq_cred.cr_group_info = NULL;
1003 struct auth_ops svcauth_unix = {
1005 .owner = THIS_MODULE,
1006 .flavour = RPC_AUTH_UNIX,
1007 .accept = svcauth_unix_accept,
1008 .release = svcauth_unix_release,
1009 .domain_release = svcauth_unix_domain_release,
1010 .set_client = svcauth_unix_set_client,
1013 static const struct cache_detail ip_map_cache_template = {
1014 .owner = THIS_MODULE,
1015 .hash_size = IP_HASHMAX,
1016 .name = "auth.unix.ip",
1017 .cache_put = ip_map_put,
1018 .cache_upcall = ip_map_upcall,
1019 .cache_request = ip_map_request,
1020 .cache_parse = ip_map_parse,
1021 .cache_show = ip_map_show,
1022 .match = ip_map_match,
1023 .init = ip_map_init,
1025 .alloc = ip_map_alloc,
1028 int ip_map_cache_create(struct net *net)
1030 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1031 struct cache_detail *cd;
1034 cd = cache_create_net(&ip_map_cache_template, net);
1037 err = cache_register_net(cd, net);
1039 cache_destroy_net(cd, net);
1042 sn->ip_map_cache = cd;
1046 void ip_map_cache_destroy(struct net *net)
1048 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1049 struct cache_detail *cd = sn->ip_map_cache;
1051 sn->ip_map_cache = NULL;
1053 cache_unregister_net(cd, net);
1054 cache_destroy_net(cd, net);