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>
15 #include <linux/kernel.h>
16 #define RPCDBG_FACILITY RPCDBG_AUTH
18 #include <linux/sunrpc/clnt.h>
21 * AUTHUNIX and AUTHNULL credentials are both handled here.
22 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
23 * are always nobody (-2). i.e. we do the same IP address checks for
24 * AUTHNULL as for AUTHUNIX, and that is done here.
31 /* other stuff later */
34 extern struct auth_ops svcauth_unix;
36 struct auth_domain *unix_domain_find(char *name)
38 struct auth_domain *rv;
39 struct unix_domain *new = NULL;
41 rv = auth_domain_lookup(name, NULL);
44 if (new && rv != &new->h)
45 auth_domain_put(&new->h);
47 if (rv->flavour != &svcauth_unix) {
54 new = kmalloc(sizeof(*new), GFP_KERNEL);
57 kref_init(&new->h.ref);
58 new->h.name = kstrdup(name, GFP_KERNEL);
59 if (new->h.name == NULL) {
63 new->h.flavour = &svcauth_unix;
64 new->addr_changes = 0;
65 rv = auth_domain_lookup(name, &new->h);
68 EXPORT_SYMBOL_GPL(unix_domain_find);
70 static void svcauth_unix_domain_release(struct auth_domain *dom)
72 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
79 /**************************************************
80 * cache for IP address to unix_domain
81 * as needed by AUTH_UNIX
84 #define IP_HASHMAX (1<<IP_HASHBITS)
85 #define IP_HASHMASK (IP_HASHMAX-1)
89 char m_class[8]; /* e.g. "nfsd" */
90 struct in6_addr m_addr;
91 struct unix_domain *m_client;
94 static struct cache_head *ip_table[IP_HASHMAX];
96 static void ip_map_put(struct kref *kref)
98 struct cache_head *item = container_of(kref, struct cache_head, ref);
99 struct ip_map *im = container_of(item, struct ip_map,h);
101 if (test_bit(CACHE_VALID, &item->flags) &&
102 !test_bit(CACHE_NEGATIVE, &item->flags))
103 auth_domain_put(&im->m_client->h);
108 /* hash_long on a 64 bit machine is currently REALLY BAD for
109 * IP addresses in reverse-endian (i.e. on a little-endian machine).
110 * So use a trivial but reliable hash instead
112 static inline int hash_ip(__be32 ip)
114 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
115 return (hash ^ (hash>>8)) & 0xff;
118 static inline int hash_ip6(struct in6_addr ip)
120 return (hash_ip(ip.s6_addr32[0]) ^
121 hash_ip(ip.s6_addr32[1]) ^
122 hash_ip(ip.s6_addr32[2]) ^
123 hash_ip(ip.s6_addr32[3]));
125 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
127 struct ip_map *orig = container_of(corig, struct ip_map, h);
128 struct ip_map *new = container_of(cnew, struct ip_map, h);
129 return strcmp(orig->m_class, new->m_class) == 0 &&
130 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
132 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
134 struct ip_map *new = container_of(cnew, struct ip_map, h);
135 struct ip_map *item = container_of(citem, struct ip_map, h);
137 strcpy(new->m_class, item->m_class);
138 ipv6_addr_copy(&new->m_addr, &item->m_addr);
140 static void update(struct cache_head *cnew, struct cache_head *citem)
142 struct ip_map *new = container_of(cnew, struct ip_map, h);
143 struct ip_map *item = container_of(citem, struct ip_map, h);
145 kref_get(&item->m_client->h.ref);
146 new->m_client = item->m_client;
147 new->m_add_change = item->m_add_change;
149 static struct cache_head *ip_map_alloc(void)
151 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
158 static void ip_map_request(struct cache_detail *cd,
159 struct cache_head *h,
160 char **bpp, int *blen)
163 struct ip_map *im = container_of(h, struct ip_map, h);
165 if (ipv6_addr_v4mapped(&(im->m_addr))) {
166 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
168 snprintf(text_addr, 40, "%pI6", &im->m_addr);
170 qword_add(bpp, blen, im->m_class);
171 qword_add(bpp, blen, text_addr);
175 static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
177 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
180 static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr);
181 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
183 static int ip_map_parse(struct cache_detail *cd,
184 char *mesg, int mlen)
186 /* class ipaddress [domainname] */
187 /* should be safe just to use the start of the input buffer
194 struct sockaddr_in s4;
195 struct sockaddr_in6 s6;
197 struct sockaddr_in6 sin6;
201 struct auth_domain *dom;
204 if (mesg[mlen-1] != '\n')
209 len = qword_get(&mesg, class, sizeof(class));
210 if (len <= 0) return -EINVAL;
213 len = qword_get(&mesg, buf, mlen);
214 if (len <= 0) return -EINVAL;
216 if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0)
218 switch (address.sa.sa_family) {
220 /* Form a mapped IPv4 address in sin6 */
221 memset(&sin6, 0, sizeof(sin6));
222 sin6.sin6_family = AF_INET6;
223 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
224 sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr;
226 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
228 memcpy(&sin6, &address.s6, sizeof(sin6));
235 expiry = get_expiry(&mesg);
239 /* domainname, or empty for NEGATIVE */
240 len = qword_get(&mesg, buf, mlen);
241 if (len < 0) return -EINVAL;
244 dom = unix_domain_find(buf);
250 /* IPv6 scope IDs are ignored for now */
251 ipmp = ip_map_lookup(class, &sin6.sin6_addr);
253 err = ip_map_update(ipmp,
254 container_of(dom, struct unix_domain, h),
260 auth_domain_put(dom);
266 static int ip_map_show(struct seq_file *m,
267 struct cache_detail *cd,
268 struct cache_head *h)
271 struct in6_addr addr;
272 char *dom = "-no-domain-";
275 seq_puts(m, "#class IP domain\n");
278 im = container_of(h, struct ip_map, h);
279 /* class addr domain */
280 ipv6_addr_copy(&addr, &im->m_addr);
282 if (test_bit(CACHE_VALID, &h->flags) &&
283 !test_bit(CACHE_NEGATIVE, &h->flags))
284 dom = im->m_client->h.name;
286 if (ipv6_addr_v4mapped(&addr)) {
287 seq_printf(m, "%s %pI4 %s\n",
288 im->m_class, &addr.s6_addr32[3], dom);
290 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
296 struct cache_detail ip_map_cache = {
297 .owner = THIS_MODULE,
298 .hash_size = IP_HASHMAX,
299 .hash_table = ip_table,
300 .name = "auth.unix.ip",
301 .cache_put = ip_map_put,
302 .cache_upcall = ip_map_upcall,
303 .cache_parse = ip_map_parse,
304 .cache_show = ip_map_show,
305 .match = ip_map_match,
308 .alloc = ip_map_alloc,
311 static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
314 struct cache_head *ch;
316 strcpy(ip.m_class, class);
317 ipv6_addr_copy(&ip.m_addr, addr);
318 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
319 hash_str(class, IP_HASHBITS) ^
323 return container_of(ch, struct ip_map, h);
328 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
331 struct cache_head *ch;
336 set_bit(CACHE_NEGATIVE, &ip.h.flags);
338 ip.m_add_change = udom->addr_changes;
339 /* if this is from the legacy set_client system call,
340 * we need m_add_change to be one higher
345 ip.h.expiry_time = expiry;
346 ch = sunrpc_cache_update(&ip_map_cache,
348 hash_str(ipm->m_class, IP_HASHBITS) ^
349 hash_ip6(ipm->m_addr));
352 cache_put(ch, &ip_map_cache);
356 int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
358 struct unix_domain *udom;
361 if (dom->flavour != &svcauth_unix)
363 udom = container_of(dom, struct unix_domain, h);
364 ipmp = ip_map_lookup("nfsd", addr);
367 return ip_map_update(ipmp, udom, NEVER);
371 EXPORT_SYMBOL_GPL(auth_unix_add_addr);
373 int auth_unix_forget_old(struct auth_domain *dom)
375 struct unix_domain *udom;
377 if (dom->flavour != &svcauth_unix)
379 udom = container_of(dom, struct unix_domain, h);
380 udom->addr_changes++;
383 EXPORT_SYMBOL_GPL(auth_unix_forget_old);
385 struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
388 struct auth_domain *rv;
390 ipm = ip_map_lookup("nfsd", addr);
394 if (cache_check(&ip_map_cache, &ipm->h, NULL))
397 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
398 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
399 auth_domain_put(&ipm->m_client->h);
402 rv = &ipm->m_client->h;
405 cache_put(&ipm->h, &ip_map_cache);
408 EXPORT_SYMBOL_GPL(auth_unix_lookup);
410 void svcauth_unix_purge(void)
412 cache_purge(&ip_map_cache);
414 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
416 static inline struct ip_map *
417 ip_map_cached_get(struct svc_rqst *rqstp)
419 struct ip_map *ipm = NULL;
420 struct svc_xprt *xprt = rqstp->rq_xprt;
422 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
423 spin_lock(&xprt->xpt_lock);
424 ipm = xprt->xpt_auth_cache;
426 if (!cache_valid(&ipm->h)) {
428 * The entry has been invalidated since it was
429 * remembered, e.g. by a second mount from the
432 xprt->xpt_auth_cache = NULL;
433 spin_unlock(&xprt->xpt_lock);
434 cache_put(&ipm->h, &ip_map_cache);
439 spin_unlock(&xprt->xpt_lock);
445 ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
447 struct svc_xprt *xprt = rqstp->rq_xprt;
449 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
450 spin_lock(&xprt->xpt_lock);
451 if (xprt->xpt_auth_cache == NULL) {
452 /* newly cached, keep the reference */
453 xprt->xpt_auth_cache = ipm;
456 spin_unlock(&xprt->xpt_lock);
459 cache_put(&ipm->h, &ip_map_cache);
463 svcauth_unix_info_release(void *info)
465 struct ip_map *ipm = info;
466 cache_put(&ipm->h, &ip_map_cache);
469 /****************************************************************************
470 * auth.unix.gid cache
471 * simple cache to map a UID to a list of GIDs
472 * because AUTH_UNIX aka AUTH_SYS has a max of 16
474 #define GID_HASHBITS 8
475 #define GID_HASHMAX (1<<GID_HASHBITS)
476 #define GID_HASHMASK (GID_HASHMAX - 1)
481 struct group_info *gi;
483 static struct cache_head *gid_table[GID_HASHMAX];
485 static void unix_gid_put(struct kref *kref)
487 struct cache_head *item = container_of(kref, struct cache_head, ref);
488 struct unix_gid *ug = container_of(item, struct unix_gid, h);
489 if (test_bit(CACHE_VALID, &item->flags) &&
490 !test_bit(CACHE_NEGATIVE, &item->flags))
491 put_group_info(ug->gi);
495 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
497 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
498 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
499 return orig->uid == new->uid;
501 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
503 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
504 struct unix_gid *item = container_of(citem, struct unix_gid, h);
505 new->uid = item->uid;
507 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
509 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
510 struct unix_gid *item = container_of(citem, struct unix_gid, h);
512 get_group_info(item->gi);
515 static struct cache_head *unix_gid_alloc(void)
517 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
524 static void unix_gid_request(struct cache_detail *cd,
525 struct cache_head *h,
526 char **bpp, int *blen)
529 struct unix_gid *ug = container_of(h, struct unix_gid, h);
531 snprintf(tuid, 20, "%u", ug->uid);
532 qword_add(bpp, blen, tuid);
536 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
538 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
541 static struct unix_gid *unix_gid_lookup(uid_t uid);
542 extern struct cache_detail unix_gid_cache;
544 static int unix_gid_parse(struct cache_detail *cd,
545 char *mesg, int mlen)
547 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
554 struct unix_gid ug, *ugp;
556 if (mlen <= 0 || mesg[mlen-1] != '\n')
560 rv = get_int(&mesg, &uid);
565 expiry = get_expiry(&mesg);
569 rv = get_int(&mesg, &gids);
570 if (rv || gids < 0 || gids > 8192)
573 ug.gi = groups_alloc(gids);
577 for (i = 0 ; i < gids ; i++) {
579 rv = get_int(&mesg, &gid);
583 GROUP_AT(ug.gi, i) = gid;
586 ugp = unix_gid_lookup(uid);
588 struct cache_head *ch;
590 ug.h.expiry_time = expiry;
591 ch = sunrpc_cache_update(&unix_gid_cache,
593 hash_long(uid, GID_HASHBITS));
598 cache_put(ch, &unix_gid_cache);
604 put_group_info(ug.gi);
608 static int unix_gid_show(struct seq_file *m,
609 struct cache_detail *cd,
610 struct cache_head *h)
617 seq_puts(m, "#uid cnt: gids...\n");
620 ug = container_of(h, struct unix_gid, h);
621 if (test_bit(CACHE_VALID, &h->flags) &&
622 !test_bit(CACHE_NEGATIVE, &h->flags))
623 glen = ug->gi->ngroups;
627 seq_printf(m, "%u %d:", ug->uid, glen);
628 for (i = 0; i < glen; i++)
629 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
634 struct cache_detail unix_gid_cache = {
635 .owner = THIS_MODULE,
636 .hash_size = GID_HASHMAX,
637 .hash_table = gid_table,
638 .name = "auth.unix.gid",
639 .cache_put = unix_gid_put,
640 .cache_upcall = unix_gid_upcall,
641 .cache_parse = unix_gid_parse,
642 .cache_show = unix_gid_show,
643 .match = unix_gid_match,
644 .init = unix_gid_init,
645 .update = unix_gid_update,
646 .alloc = unix_gid_alloc,
649 static struct unix_gid *unix_gid_lookup(uid_t uid)
652 struct cache_head *ch;
655 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
656 hash_long(uid, GID_HASHBITS));
658 return container_of(ch, struct unix_gid, h);
663 static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
666 struct group_info *gi;
669 ug = unix_gid_lookup(uid);
671 return ERR_PTR(-EAGAIN);
672 ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
675 return ERR_PTR(-ENOENT);
677 gi = get_group_info(ug->gi);
678 cache_put(&ug->h, &unix_gid_cache);
681 return ERR_PTR(-EAGAIN);
686 svcauth_unix_set_client(struct svc_rqst *rqstp)
688 struct sockaddr_in *sin;
689 struct sockaddr_in6 *sin6, sin6_storage;
691 struct group_info *gi;
692 struct svc_cred *cred = &rqstp->rq_cred;
694 switch (rqstp->rq_addr.ss_family) {
696 sin = svc_addr_in(rqstp);
697 sin6 = &sin6_storage;
698 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
701 sin6 = svc_addr_in6(rqstp);
707 rqstp->rq_client = NULL;
708 if (rqstp->rq_proc == 0)
711 ipm = ip_map_cached_get(rqstp);
713 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
719 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
728 rqstp->rq_client = &ipm->m_client->h;
729 kref_get(&rqstp->rq_client->ref);
730 ip_map_cached_put(rqstp, ipm);
734 gi = unix_gid_find(cred->cr_uid, rqstp);
735 switch (PTR_ERR(gi)) {
741 put_group_info(cred->cr_group_info);
742 cred->cr_group_info = gi;
747 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
750 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
752 struct kvec *argv = &rqstp->rq_arg.head[0];
753 struct kvec *resv = &rqstp->rq_res.head[0];
754 struct svc_cred *cred = &rqstp->rq_cred;
756 cred->cr_group_info = NULL;
757 rqstp->rq_client = NULL;
759 if (argv->iov_len < 3*4)
762 if (svc_getu32(argv) != 0) {
763 dprintk("svc: bad null cred\n");
764 *authp = rpc_autherr_badcred;
767 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
768 dprintk("svc: bad null verf\n");
769 *authp = rpc_autherr_badverf;
773 /* Signal that mapping to nobody uid/gid is required */
774 cred->cr_uid = (uid_t) -1;
775 cred->cr_gid = (gid_t) -1;
776 cred->cr_group_info = groups_alloc(0);
777 if (cred->cr_group_info == NULL)
778 return SVC_DROP; /* kmalloc failure - client must retry */
780 /* Put NULL verifier */
781 svc_putnl(resv, RPC_AUTH_NULL);
784 rqstp->rq_flavor = RPC_AUTH_NULL;
789 svcauth_null_release(struct svc_rqst *rqstp)
791 if (rqstp->rq_client)
792 auth_domain_put(rqstp->rq_client);
793 rqstp->rq_client = NULL;
794 if (rqstp->rq_cred.cr_group_info)
795 put_group_info(rqstp->rq_cred.cr_group_info);
796 rqstp->rq_cred.cr_group_info = NULL;
798 return 0; /* don't drop */
802 struct auth_ops svcauth_null = {
804 .owner = THIS_MODULE,
805 .flavour = RPC_AUTH_NULL,
806 .accept = svcauth_null_accept,
807 .release = svcauth_null_release,
808 .set_client = svcauth_unix_set_client,
813 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
815 struct kvec *argv = &rqstp->rq_arg.head[0];
816 struct kvec *resv = &rqstp->rq_res.head[0];
817 struct svc_cred *cred = &rqstp->rq_cred;
819 int len = argv->iov_len;
821 cred->cr_group_info = NULL;
822 rqstp->rq_client = NULL;
824 if ((len -= 3*4) < 0)
827 svc_getu32(argv); /* length */
828 svc_getu32(argv); /* time stamp */
829 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
830 if (slen > 64 || (len -= (slen + 3)*4) < 0)
832 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
833 argv->iov_len -= slen*4;
835 cred->cr_uid = svc_getnl(argv); /* uid */
836 cred->cr_gid = svc_getnl(argv); /* gid */
837 slen = svc_getnl(argv); /* gids length */
838 if (slen > 16 || (len -= (slen + 2)*4) < 0)
840 cred->cr_group_info = groups_alloc(slen);
841 if (cred->cr_group_info == NULL)
843 for (i = 0; i < slen; i++)
844 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
845 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
846 *authp = rpc_autherr_badverf;
850 /* Put NULL verifier */
851 svc_putnl(resv, RPC_AUTH_NULL);
854 rqstp->rq_flavor = RPC_AUTH_UNIX;
858 *authp = rpc_autherr_badcred;
863 svcauth_unix_release(struct svc_rqst *rqstp)
865 /* Verifier (such as it is) is already in place.
867 if (rqstp->rq_client)
868 auth_domain_put(rqstp->rq_client);
869 rqstp->rq_client = NULL;
870 if (rqstp->rq_cred.cr_group_info)
871 put_group_info(rqstp->rq_cred.cr_group_info);
872 rqstp->rq_cred.cr_group_info = NULL;
878 struct auth_ops svcauth_unix = {
880 .owner = THIS_MODULE,
881 .flavour = RPC_AUTH_UNIX,
882 .accept = svcauth_unix_accept,
883 .release = svcauth_unix_release,
884 .domain_release = svcauth_unix_domain_release,
885 .set_client = svcauth_unix_set_client,