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>
8 #include <linux/sunrpc/gss_api.h>
10 #include <linux/seq_file.h>
11 #include <linux/hash.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #define RPCDBG_FACILITY RPCDBG_AUTH
19 #include <linux/sunrpc/clnt.h>
22 * AUTHUNIX and AUTHNULL credentials are both handled here.
23 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
24 * are always nobody (-2). i.e. we do the same IP address checks for
25 * AUTHNULL as for AUTHUNIX, and that is done here.
32 /* other stuff later */
35 extern struct auth_ops svcauth_unix;
37 struct auth_domain *unix_domain_find(char *name)
39 struct auth_domain *rv;
40 struct unix_domain *new = NULL;
42 rv = auth_domain_lookup(name, NULL);
45 if (new && rv != &new->h)
46 auth_domain_put(&new->h);
48 if (rv->flavour != &svcauth_unix) {
55 new = kmalloc(sizeof(*new), GFP_KERNEL);
58 kref_init(&new->h.ref);
59 new->h.name = kstrdup(name, GFP_KERNEL);
60 if (new->h.name == NULL) {
64 new->h.flavour = &svcauth_unix;
65 new->addr_changes = 0;
66 rv = auth_domain_lookup(name, &new->h);
69 EXPORT_SYMBOL_GPL(unix_domain_find);
71 static void svcauth_unix_domain_release(struct auth_domain *dom)
73 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
80 /**************************************************
81 * cache for IP address to unix_domain
82 * as needed by AUTH_UNIX
85 #define IP_HASHMAX (1<<IP_HASHBITS)
86 #define IP_HASHMASK (IP_HASHMAX-1)
90 char m_class[8]; /* e.g. "nfsd" */
91 struct in6_addr m_addr;
92 struct unix_domain *m_client;
95 static struct cache_head *ip_table[IP_HASHMAX];
97 static void ip_map_put(struct kref *kref)
99 struct cache_head *item = container_of(kref, struct cache_head, ref);
100 struct ip_map *im = container_of(item, struct ip_map,h);
102 if (test_bit(CACHE_VALID, &item->flags) &&
103 !test_bit(CACHE_NEGATIVE, &item->flags))
104 auth_domain_put(&im->m_client->h);
109 /* hash_long on a 64 bit machine is currently REALLY BAD for
110 * IP addresses in reverse-endian (i.e. on a little-endian machine).
111 * So use a trivial but reliable hash instead
113 static inline int hash_ip(__be32 ip)
115 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
116 return (hash ^ (hash>>8)) & 0xff;
119 static inline int hash_ip6(struct in6_addr ip)
121 return (hash_ip(ip.s6_addr32[0]) ^
122 hash_ip(ip.s6_addr32[1]) ^
123 hash_ip(ip.s6_addr32[2]) ^
124 hash_ip(ip.s6_addr32[3]));
126 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
128 struct ip_map *orig = container_of(corig, struct ip_map, h);
129 struct ip_map *new = container_of(cnew, struct ip_map, h);
130 return strcmp(orig->m_class, new->m_class) == 0 &&
131 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
133 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
135 struct ip_map *new = container_of(cnew, struct ip_map, h);
136 struct ip_map *item = container_of(citem, struct ip_map, h);
138 strcpy(new->m_class, item->m_class);
139 ipv6_addr_copy(&new->m_addr, &item->m_addr);
141 static void update(struct cache_head *cnew, struct cache_head *citem)
143 struct ip_map *new = container_of(cnew, struct ip_map, h);
144 struct ip_map *item = container_of(citem, struct ip_map, h);
146 kref_get(&item->m_client->h.ref);
147 new->m_client = item->m_client;
148 new->m_add_change = item->m_add_change;
150 static struct cache_head *ip_map_alloc(void)
152 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
159 static void ip_map_request(struct cache_detail *cd,
160 struct cache_head *h,
161 char **bpp, int *blen)
164 struct ip_map *im = container_of(h, struct ip_map, h);
166 if (ipv6_addr_v4mapped(&(im->m_addr))) {
167 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
169 snprintf(text_addr, 40, "%pI6", &im->m_addr);
171 qword_add(bpp, blen, im->m_class);
172 qword_add(bpp, blen, text_addr);
176 static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
178 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
181 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
182 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
184 static int ip_map_parse(struct cache_detail *cd,
185 char *mesg, int mlen)
187 /* class ipaddress [domainname] */
188 /* should be safe just to use the start of the input buffer
195 struct sockaddr_in s4;
196 struct sockaddr_in6 s6;
198 struct sockaddr_in6 sin6;
202 struct auth_domain *dom;
205 if (mesg[mlen-1] != '\n')
210 len = qword_get(&mesg, class, sizeof(class));
211 if (len <= 0) return -EINVAL;
214 len = qword_get(&mesg, buf, mlen);
215 if (len <= 0) return -EINVAL;
217 if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0)
219 switch (address.sa.sa_family) {
221 /* Form a mapped IPv4 address in sin6 */
222 memset(&sin6, 0, sizeof(sin6));
223 sin6.sin6_family = AF_INET6;
224 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
225 sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr;
227 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
229 memcpy(&sin6, &address.s6, sizeof(sin6));
236 expiry = get_expiry(&mesg);
240 /* domainname, or empty for NEGATIVE */
241 len = qword_get(&mesg, buf, mlen);
242 if (len < 0) return -EINVAL;
245 dom = unix_domain_find(buf);
251 /* IPv6 scope IDs are ignored for now */
252 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
254 err = __ip_map_update(cd, ipmp,
255 container_of(dom, struct unix_domain, h),
261 auth_domain_put(dom);
267 static int ip_map_show(struct seq_file *m,
268 struct cache_detail *cd,
269 struct cache_head *h)
272 struct in6_addr addr;
273 char *dom = "-no-domain-";
276 seq_puts(m, "#class IP domain\n");
279 im = container_of(h, struct ip_map, h);
280 /* class addr domain */
281 ipv6_addr_copy(&addr, &im->m_addr);
283 if (test_bit(CACHE_VALID, &h->flags) &&
284 !test_bit(CACHE_NEGATIVE, &h->flags))
285 dom = im->m_client->h.name;
287 if (ipv6_addr_v4mapped(&addr)) {
288 seq_printf(m, "%s %pI4 %s\n",
289 im->m_class, &addr.s6_addr32[3], dom);
291 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
297 struct cache_detail ip_map_cache = {
298 .owner = THIS_MODULE,
299 .hash_size = IP_HASHMAX,
300 .hash_table = ip_table,
301 .name = "auth.unix.ip",
302 .cache_put = ip_map_put,
303 .cache_upcall = ip_map_upcall,
304 .cache_parse = ip_map_parse,
305 .cache_show = ip_map_show,
306 .match = ip_map_match,
309 .alloc = ip_map_alloc,
312 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
313 struct in6_addr *addr)
316 struct cache_head *ch;
318 strcpy(ip.m_class, class);
319 ipv6_addr_copy(&ip.m_addr, addr);
320 ch = sunrpc_cache_lookup(cd, &ip.h,
321 hash_str(class, IP_HASHBITS) ^
325 return container_of(ch, struct ip_map, h);
330 static inline struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
332 return __ip_map_lookup(&ip_map_cache, class, addr);
335 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
336 struct unix_domain *udom, time_t expiry)
339 struct cache_head *ch;
344 set_bit(CACHE_NEGATIVE, &ip.h.flags);
346 ip.m_add_change = udom->addr_changes;
347 /* if this is from the legacy set_client system call,
348 * we need m_add_change to be one higher
353 ip.h.expiry_time = expiry;
354 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
355 hash_str(ipm->m_class, IP_HASHBITS) ^
356 hash_ip6(ipm->m_addr));
363 static inline int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
365 return __ip_map_update(&ip_map_cache, ipm, udom, expiry);
368 int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
370 struct unix_domain *udom;
373 if (dom->flavour != &svcauth_unix)
375 udom = container_of(dom, struct unix_domain, h);
376 ipmp = ip_map_lookup("nfsd", addr);
379 return ip_map_update(ipmp, udom, NEVER);
383 EXPORT_SYMBOL_GPL(auth_unix_add_addr);
385 int auth_unix_forget_old(struct auth_domain *dom)
387 struct unix_domain *udom;
389 if (dom->flavour != &svcauth_unix)
391 udom = container_of(dom, struct unix_domain, h);
392 udom->addr_changes++;
395 EXPORT_SYMBOL_GPL(auth_unix_forget_old);
397 struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
400 struct auth_domain *rv;
402 ipm = ip_map_lookup("nfsd", addr);
406 if (cache_check(&ip_map_cache, &ipm->h, NULL))
409 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
410 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
411 auth_domain_put(&ipm->m_client->h);
414 rv = &ipm->m_client->h;
417 cache_put(&ipm->h, &ip_map_cache);
420 EXPORT_SYMBOL_GPL(auth_unix_lookup);
422 void svcauth_unix_purge(void)
424 cache_purge(&ip_map_cache);
426 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
428 static inline struct ip_map *
429 ip_map_cached_get(struct svc_rqst *rqstp)
431 struct ip_map *ipm = NULL;
432 struct svc_xprt *xprt = rqstp->rq_xprt;
434 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
435 spin_lock(&xprt->xpt_lock);
436 ipm = xprt->xpt_auth_cache;
438 if (!cache_valid(&ipm->h)) {
440 * The entry has been invalidated since it was
441 * remembered, e.g. by a second mount from the
444 xprt->xpt_auth_cache = NULL;
445 spin_unlock(&xprt->xpt_lock);
446 cache_put(&ipm->h, &ip_map_cache);
451 spin_unlock(&xprt->xpt_lock);
457 ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
459 struct svc_xprt *xprt = rqstp->rq_xprt;
461 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
462 spin_lock(&xprt->xpt_lock);
463 if (xprt->xpt_auth_cache == NULL) {
464 /* newly cached, keep the reference */
465 xprt->xpt_auth_cache = ipm;
468 spin_unlock(&xprt->xpt_lock);
471 cache_put(&ipm->h, &ip_map_cache);
475 svcauth_unix_info_release(void *info)
477 struct ip_map *ipm = info;
478 cache_put(&ipm->h, &ip_map_cache);
481 /****************************************************************************
482 * auth.unix.gid cache
483 * simple cache to map a UID to a list of GIDs
484 * because AUTH_UNIX aka AUTH_SYS has a max of 16
486 #define GID_HASHBITS 8
487 #define GID_HASHMAX (1<<GID_HASHBITS)
488 #define GID_HASHMASK (GID_HASHMAX - 1)
493 struct group_info *gi;
495 static struct cache_head *gid_table[GID_HASHMAX];
497 static void unix_gid_put(struct kref *kref)
499 struct cache_head *item = container_of(kref, struct cache_head, ref);
500 struct unix_gid *ug = container_of(item, struct unix_gid, h);
501 if (test_bit(CACHE_VALID, &item->flags) &&
502 !test_bit(CACHE_NEGATIVE, &item->flags))
503 put_group_info(ug->gi);
507 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
509 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
510 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
511 return orig->uid == new->uid;
513 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
515 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
516 struct unix_gid *item = container_of(citem, struct unix_gid, h);
517 new->uid = item->uid;
519 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
521 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
522 struct unix_gid *item = container_of(citem, struct unix_gid, h);
524 get_group_info(item->gi);
527 static struct cache_head *unix_gid_alloc(void)
529 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
536 static void unix_gid_request(struct cache_detail *cd,
537 struct cache_head *h,
538 char **bpp, int *blen)
541 struct unix_gid *ug = container_of(h, struct unix_gid, h);
543 snprintf(tuid, 20, "%u", ug->uid);
544 qword_add(bpp, blen, tuid);
548 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
550 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
553 static struct unix_gid *unix_gid_lookup(uid_t uid);
554 extern struct cache_detail unix_gid_cache;
556 static int unix_gid_parse(struct cache_detail *cd,
557 char *mesg, int mlen)
559 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
566 struct unix_gid ug, *ugp;
568 if (mlen <= 0 || mesg[mlen-1] != '\n')
572 rv = get_int(&mesg, &uid);
577 expiry = get_expiry(&mesg);
581 rv = get_int(&mesg, &gids);
582 if (rv || gids < 0 || gids > 8192)
585 ug.gi = groups_alloc(gids);
589 for (i = 0 ; i < gids ; i++) {
591 rv = get_int(&mesg, &gid);
595 GROUP_AT(ug.gi, i) = gid;
598 ugp = unix_gid_lookup(uid);
600 struct cache_head *ch;
602 ug.h.expiry_time = expiry;
603 ch = sunrpc_cache_update(&unix_gid_cache,
605 hash_long(uid, GID_HASHBITS));
610 cache_put(ch, &unix_gid_cache);
616 put_group_info(ug.gi);
620 static int unix_gid_show(struct seq_file *m,
621 struct cache_detail *cd,
622 struct cache_head *h)
629 seq_puts(m, "#uid cnt: gids...\n");
632 ug = container_of(h, struct unix_gid, h);
633 if (test_bit(CACHE_VALID, &h->flags) &&
634 !test_bit(CACHE_NEGATIVE, &h->flags))
635 glen = ug->gi->ngroups;
639 seq_printf(m, "%u %d:", ug->uid, glen);
640 for (i = 0; i < glen; i++)
641 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
646 struct cache_detail unix_gid_cache = {
647 .owner = THIS_MODULE,
648 .hash_size = GID_HASHMAX,
649 .hash_table = gid_table,
650 .name = "auth.unix.gid",
651 .cache_put = unix_gid_put,
652 .cache_upcall = unix_gid_upcall,
653 .cache_parse = unix_gid_parse,
654 .cache_show = unix_gid_show,
655 .match = unix_gid_match,
656 .init = unix_gid_init,
657 .update = unix_gid_update,
658 .alloc = unix_gid_alloc,
661 static struct unix_gid *unix_gid_lookup(uid_t uid)
664 struct cache_head *ch;
667 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
668 hash_long(uid, GID_HASHBITS));
670 return container_of(ch, struct unix_gid, h);
675 static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
678 struct group_info *gi;
681 ug = unix_gid_lookup(uid);
683 return ERR_PTR(-EAGAIN);
684 ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
687 return ERR_PTR(-ENOENT);
689 return ERR_PTR(-ESHUTDOWN);
691 gi = get_group_info(ug->gi);
692 cache_put(&ug->h, &unix_gid_cache);
695 return ERR_PTR(-EAGAIN);
700 svcauth_unix_set_client(struct svc_rqst *rqstp)
702 struct sockaddr_in *sin;
703 struct sockaddr_in6 *sin6, sin6_storage;
705 struct group_info *gi;
706 struct svc_cred *cred = &rqstp->rq_cred;
708 switch (rqstp->rq_addr.ss_family) {
710 sin = svc_addr_in(rqstp);
711 sin6 = &sin6_storage;
712 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
715 sin6 = svc_addr_in6(rqstp);
721 rqstp->rq_client = NULL;
722 if (rqstp->rq_proc == 0)
725 ipm = ip_map_cached_get(rqstp);
727 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
733 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
743 rqstp->rq_client = &ipm->m_client->h;
744 kref_get(&rqstp->rq_client->ref);
745 ip_map_cached_put(rqstp, ipm);
749 gi = unix_gid_find(cred->cr_uid, rqstp);
750 switch (PTR_ERR(gi)) {
758 put_group_info(cred->cr_group_info);
759 cred->cr_group_info = gi;
764 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
767 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
769 struct kvec *argv = &rqstp->rq_arg.head[0];
770 struct kvec *resv = &rqstp->rq_res.head[0];
771 struct svc_cred *cred = &rqstp->rq_cred;
773 cred->cr_group_info = NULL;
774 rqstp->rq_client = NULL;
776 if (argv->iov_len < 3*4)
779 if (svc_getu32(argv) != 0) {
780 dprintk("svc: bad null cred\n");
781 *authp = rpc_autherr_badcred;
784 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
785 dprintk("svc: bad null verf\n");
786 *authp = rpc_autherr_badverf;
790 /* Signal that mapping to nobody uid/gid is required */
791 cred->cr_uid = (uid_t) -1;
792 cred->cr_gid = (gid_t) -1;
793 cred->cr_group_info = groups_alloc(0);
794 if (cred->cr_group_info == NULL)
795 return SVC_CLOSE; /* kmalloc failure - client must retry */
797 /* Put NULL verifier */
798 svc_putnl(resv, RPC_AUTH_NULL);
801 rqstp->rq_flavor = RPC_AUTH_NULL;
806 svcauth_null_release(struct svc_rqst *rqstp)
808 if (rqstp->rq_client)
809 auth_domain_put(rqstp->rq_client);
810 rqstp->rq_client = NULL;
811 if (rqstp->rq_cred.cr_group_info)
812 put_group_info(rqstp->rq_cred.cr_group_info);
813 rqstp->rq_cred.cr_group_info = NULL;
815 return 0; /* don't drop */
819 struct auth_ops svcauth_null = {
821 .owner = THIS_MODULE,
822 .flavour = RPC_AUTH_NULL,
823 .accept = svcauth_null_accept,
824 .release = svcauth_null_release,
825 .set_client = svcauth_unix_set_client,
830 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
832 struct kvec *argv = &rqstp->rq_arg.head[0];
833 struct kvec *resv = &rqstp->rq_res.head[0];
834 struct svc_cred *cred = &rqstp->rq_cred;
836 int len = argv->iov_len;
838 cred->cr_group_info = NULL;
839 rqstp->rq_client = NULL;
841 if ((len -= 3*4) < 0)
844 svc_getu32(argv); /* length */
845 svc_getu32(argv); /* time stamp */
846 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
847 if (slen > 64 || (len -= (slen + 3)*4) < 0)
849 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
850 argv->iov_len -= slen*4;
852 cred->cr_uid = svc_getnl(argv); /* uid */
853 cred->cr_gid = svc_getnl(argv); /* gid */
854 slen = svc_getnl(argv); /* gids length */
855 if (slen > 16 || (len -= (slen + 2)*4) < 0)
857 cred->cr_group_info = groups_alloc(slen);
858 if (cred->cr_group_info == NULL)
860 for (i = 0; i < slen; i++)
861 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
862 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
863 *authp = rpc_autherr_badverf;
867 /* Put NULL verifier */
868 svc_putnl(resv, RPC_AUTH_NULL);
871 rqstp->rq_flavor = RPC_AUTH_UNIX;
875 *authp = rpc_autherr_badcred;
880 svcauth_unix_release(struct svc_rqst *rqstp)
882 /* Verifier (such as it is) is already in place.
884 if (rqstp->rq_client)
885 auth_domain_put(rqstp->rq_client);
886 rqstp->rq_client = NULL;
887 if (rqstp->rq_cred.cr_group_info)
888 put_group_info(rqstp->rq_cred.cr_group_info);
889 rqstp->rq_cred.cr_group_info = NULL;
895 struct auth_ops svcauth_unix = {
897 .owner = THIS_MODULE,
898 .flavour = RPC_AUTH_UNIX,
899 .accept = svcauth_unix_accept,
900 .release = svcauth_unix_release,
901 .domain_release = svcauth_unix_domain_release,
902 .set_client = svcauth_unix_set_client,