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
20 * AUTHUNIX and AUTHNULL credentials are both handled here.
21 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
22 * are always nobody (-2). i.e. we do the same IP address checks for
23 * AUTHNULL as for AUTHUNIX, and that is done here.
30 /* other stuff later */
33 extern struct auth_ops svcauth_unix;
35 struct auth_domain *unix_domain_find(char *name)
37 struct auth_domain *rv;
38 struct unix_domain *new = NULL;
40 rv = auth_domain_lookup(name, NULL);
43 if (new && rv != &new->h)
44 auth_domain_put(&new->h);
46 if (rv->flavour != &svcauth_unix) {
53 new = kmalloc(sizeof(*new), GFP_KERNEL);
56 kref_init(&new->h.ref);
57 new->h.name = kstrdup(name, GFP_KERNEL);
58 if (new->h.name == NULL) {
62 new->h.flavour = &svcauth_unix;
63 new->addr_changes = 0;
64 rv = auth_domain_lookup(name, &new->h);
67 EXPORT_SYMBOL_GPL(unix_domain_find);
69 static void svcauth_unix_domain_release(struct auth_domain *dom)
71 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
78 /**************************************************
79 * cache for IP address to unix_domain
80 * as needed by AUTH_UNIX
83 #define IP_HASHMAX (1<<IP_HASHBITS)
84 #define IP_HASHMASK (IP_HASHMAX-1)
88 char m_class[8]; /* e.g. "nfsd" */
89 struct in6_addr m_addr;
90 struct unix_domain *m_client;
93 static struct cache_head *ip_table[IP_HASHMAX];
95 static void ip_map_put(struct kref *kref)
97 struct cache_head *item = container_of(kref, struct cache_head, ref);
98 struct ip_map *im = container_of(item, struct ip_map,h);
100 if (test_bit(CACHE_VALID, &item->flags) &&
101 !test_bit(CACHE_NEGATIVE, &item->flags))
102 auth_domain_put(&im->m_client->h);
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
111 static inline int hash_ip(__be32 ip)
113 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
114 return (hash ^ (hash>>8)) & 0xff;
117 static inline int hash_ip6(struct in6_addr ip)
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]));
124 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
126 struct ip_map *orig = container_of(corig, struct ip_map, h);
127 struct ip_map *new = container_of(cnew, struct ip_map, h);
128 return strcmp(orig->m_class, new->m_class) == 0 &&
129 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
131 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
133 struct ip_map *new = container_of(cnew, struct ip_map, h);
134 struct ip_map *item = container_of(citem, struct ip_map, h);
136 strcpy(new->m_class, item->m_class);
137 ipv6_addr_copy(&new->m_addr, &item->m_addr);
139 static void update(struct cache_head *cnew, struct cache_head *citem)
141 struct ip_map *new = container_of(cnew, struct ip_map, h);
142 struct ip_map *item = container_of(citem, struct ip_map, h);
144 kref_get(&item->m_client->h.ref);
145 new->m_client = item->m_client;
146 new->m_add_change = item->m_add_change;
148 static struct cache_head *ip_map_alloc(void)
150 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
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 int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
176 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
179 static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr);
180 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
182 static int ip_map_parse(struct cache_detail *cd,
183 char *mesg, int mlen)
185 /* class ipaddress [domainname] */
186 /* should be safe just to use the start of the input buffer
190 int b1, b2, b3, b4, b5, b6, b7, b8;
193 struct in6_addr addr;
197 struct auth_domain *dom;
200 if (mesg[mlen-1] != '\n')
205 len = qword_get(&mesg, class, sizeof(class));
206 if (len <= 0) return -EINVAL;
209 len = qword_get(&mesg, buf, mlen);
210 if (len <= 0) return -EINVAL;
212 if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) == 4) {
213 addr.s6_addr32[0] = 0;
214 addr.s6_addr32[1] = 0;
215 addr.s6_addr32[2] = htonl(0xffff);
217 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
218 } else if (sscanf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x%c",
219 &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8, &c) == 8) {
220 addr.s6_addr16[0] = htons(b1);
221 addr.s6_addr16[1] = htons(b2);
222 addr.s6_addr16[2] = htons(b3);
223 addr.s6_addr16[3] = htons(b4);
224 addr.s6_addr16[4] = htons(b5);
225 addr.s6_addr16[5] = htons(b6);
226 addr.s6_addr16[6] = htons(b7);
227 addr.s6_addr16[7] = htons(b8);
231 expiry = get_expiry(&mesg);
235 /* domainname, or empty for NEGATIVE */
236 len = qword_get(&mesg, buf, mlen);
237 if (len < 0) return -EINVAL;
240 dom = unix_domain_find(buf);
246 ipmp = ip_map_lookup(class, &addr);
248 err = ip_map_update(ipmp,
249 container_of(dom, struct unix_domain, h),
255 auth_domain_put(dom);
261 static int ip_map_show(struct seq_file *m,
262 struct cache_detail *cd,
263 struct cache_head *h)
266 struct in6_addr addr;
267 char *dom = "-no-domain-";
270 seq_puts(m, "#class IP domain\n");
273 im = container_of(h, struct ip_map, h);
274 /* class addr domain */
275 ipv6_addr_copy(&addr, &im->m_addr);
277 if (test_bit(CACHE_VALID, &h->flags) &&
278 !test_bit(CACHE_NEGATIVE, &h->flags))
279 dom = im->m_client->h.name;
281 if (ipv6_addr_v4mapped(&addr)) {
282 seq_printf(m, "%s %pI4 %s\n",
283 im->m_class, &addr.s6_addr32[3], dom);
285 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
291 struct cache_detail ip_map_cache = {
292 .owner = THIS_MODULE,
293 .hash_size = IP_HASHMAX,
294 .hash_table = ip_table,
295 .name = "auth.unix.ip",
296 .cache_put = ip_map_put,
297 .cache_upcall = ip_map_upcall,
298 .cache_parse = ip_map_parse,
299 .cache_show = ip_map_show,
300 .match = ip_map_match,
303 .alloc = ip_map_alloc,
306 static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
309 struct cache_head *ch;
311 strcpy(ip.m_class, class);
312 ipv6_addr_copy(&ip.m_addr, addr);
313 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
314 hash_str(class, IP_HASHBITS) ^
318 return container_of(ch, struct ip_map, h);
323 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
326 struct cache_head *ch;
331 set_bit(CACHE_NEGATIVE, &ip.h.flags);
333 ip.m_add_change = udom->addr_changes;
334 /* if this is from the legacy set_client system call,
335 * we need m_add_change to be one higher
340 ip.h.expiry_time = expiry;
341 ch = sunrpc_cache_update(&ip_map_cache,
343 hash_str(ipm->m_class, IP_HASHBITS) ^
344 hash_ip6(ipm->m_addr));
347 cache_put(ch, &ip_map_cache);
351 int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
353 struct unix_domain *udom;
356 if (dom->flavour != &svcauth_unix)
358 udom = container_of(dom, struct unix_domain, h);
359 ipmp = ip_map_lookup("nfsd", addr);
362 return ip_map_update(ipmp, udom, NEVER);
366 EXPORT_SYMBOL_GPL(auth_unix_add_addr);
368 int auth_unix_forget_old(struct auth_domain *dom)
370 struct unix_domain *udom;
372 if (dom->flavour != &svcauth_unix)
374 udom = container_of(dom, struct unix_domain, h);
375 udom->addr_changes++;
378 EXPORT_SYMBOL_GPL(auth_unix_forget_old);
380 struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
383 struct auth_domain *rv;
385 ipm = ip_map_lookup("nfsd", addr);
389 if (cache_check(&ip_map_cache, &ipm->h, NULL))
392 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
393 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
394 auth_domain_put(&ipm->m_client->h);
397 rv = &ipm->m_client->h;
400 cache_put(&ipm->h, &ip_map_cache);
403 EXPORT_SYMBOL_GPL(auth_unix_lookup);
405 void svcauth_unix_purge(void)
407 cache_purge(&ip_map_cache);
409 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
411 static inline struct ip_map *
412 ip_map_cached_get(struct svc_rqst *rqstp)
414 struct ip_map *ipm = NULL;
415 struct svc_xprt *xprt = rqstp->rq_xprt;
417 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
418 spin_lock(&xprt->xpt_lock);
419 ipm = xprt->xpt_auth_cache;
421 if (!cache_valid(&ipm->h)) {
423 * The entry has been invalidated since it was
424 * remembered, e.g. by a second mount from the
427 xprt->xpt_auth_cache = NULL;
428 spin_unlock(&xprt->xpt_lock);
429 cache_put(&ipm->h, &ip_map_cache);
434 spin_unlock(&xprt->xpt_lock);
440 ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
442 struct svc_xprt *xprt = rqstp->rq_xprt;
444 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
445 spin_lock(&xprt->xpt_lock);
446 if (xprt->xpt_auth_cache == NULL) {
447 /* newly cached, keep the reference */
448 xprt->xpt_auth_cache = ipm;
451 spin_unlock(&xprt->xpt_lock);
454 cache_put(&ipm->h, &ip_map_cache);
458 svcauth_unix_info_release(void *info)
460 struct ip_map *ipm = info;
461 cache_put(&ipm->h, &ip_map_cache);
464 /****************************************************************************
465 * auth.unix.gid cache
466 * simple cache to map a UID to a list of GIDs
467 * because AUTH_UNIX aka AUTH_SYS has a max of 16
469 #define GID_HASHBITS 8
470 #define GID_HASHMAX (1<<GID_HASHBITS)
471 #define GID_HASHMASK (GID_HASHMAX - 1)
476 struct group_info *gi;
478 static struct cache_head *gid_table[GID_HASHMAX];
480 static void unix_gid_put(struct kref *kref)
482 struct cache_head *item = container_of(kref, struct cache_head, ref);
483 struct unix_gid *ug = container_of(item, struct unix_gid, h);
484 if (test_bit(CACHE_VALID, &item->flags) &&
485 !test_bit(CACHE_NEGATIVE, &item->flags))
486 put_group_info(ug->gi);
490 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
492 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
493 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
494 return orig->uid == new->uid;
496 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
498 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
499 struct unix_gid *item = container_of(citem, struct unix_gid, h);
500 new->uid = item->uid;
502 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
504 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
505 struct unix_gid *item = container_of(citem, struct unix_gid, h);
507 get_group_info(item->gi);
510 static struct cache_head *unix_gid_alloc(void)
512 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
519 static void unix_gid_request(struct cache_detail *cd,
520 struct cache_head *h,
521 char **bpp, int *blen)
524 struct unix_gid *ug = container_of(h, struct unix_gid, h);
526 snprintf(tuid, 20, "%u", ug->uid);
527 qword_add(bpp, blen, tuid);
531 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
533 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
536 static struct unix_gid *unix_gid_lookup(uid_t uid);
537 extern struct cache_detail unix_gid_cache;
539 static int unix_gid_parse(struct cache_detail *cd,
540 char *mesg, int mlen)
542 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
549 struct unix_gid ug, *ugp;
551 if (mlen <= 0 || mesg[mlen-1] != '\n')
555 rv = get_int(&mesg, &uid);
560 expiry = get_expiry(&mesg);
564 rv = get_int(&mesg, &gids);
565 if (rv || gids < 0 || gids > 8192)
568 ug.gi = groups_alloc(gids);
572 for (i = 0 ; i < gids ; i++) {
574 rv = get_int(&mesg, &gid);
578 GROUP_AT(ug.gi, i) = gid;
581 ugp = unix_gid_lookup(uid);
583 struct cache_head *ch;
585 ug.h.expiry_time = expiry;
586 ch = sunrpc_cache_update(&unix_gid_cache,
588 hash_long(uid, GID_HASHBITS));
593 cache_put(ch, &unix_gid_cache);
599 put_group_info(ug.gi);
603 static int unix_gid_show(struct seq_file *m,
604 struct cache_detail *cd,
605 struct cache_head *h)
612 seq_puts(m, "#uid cnt: gids...\n");
615 ug = container_of(h, struct unix_gid, h);
616 if (test_bit(CACHE_VALID, &h->flags) &&
617 !test_bit(CACHE_NEGATIVE, &h->flags))
618 glen = ug->gi->ngroups;
622 seq_printf(m, "%d %d:", ug->uid, glen);
623 for (i = 0; i < glen; i++)
624 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
629 struct cache_detail unix_gid_cache = {
630 .owner = THIS_MODULE,
631 .hash_size = GID_HASHMAX,
632 .hash_table = gid_table,
633 .name = "auth.unix.gid",
634 .cache_put = unix_gid_put,
635 .cache_upcall = unix_gid_upcall,
636 .cache_parse = unix_gid_parse,
637 .cache_show = unix_gid_show,
638 .match = unix_gid_match,
639 .init = unix_gid_init,
640 .update = unix_gid_update,
641 .alloc = unix_gid_alloc,
644 static struct unix_gid *unix_gid_lookup(uid_t uid)
647 struct cache_head *ch;
650 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
651 hash_long(uid, GID_HASHBITS));
653 return container_of(ch, struct unix_gid, h);
658 static int unix_gid_find(uid_t uid, struct group_info **gip,
659 struct svc_rqst *rqstp)
661 struct unix_gid *ug = unix_gid_lookup(uid);
664 switch (cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle)) {
670 get_group_info(*gip);
671 cache_put(&ug->h, &unix_gid_cache);
679 svcauth_unix_set_client(struct svc_rqst *rqstp)
681 struct sockaddr_in *sin;
682 struct sockaddr_in6 *sin6, sin6_storage;
685 switch (rqstp->rq_addr.ss_family) {
687 sin = svc_addr_in(rqstp);
688 sin6 = &sin6_storage;
689 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
692 sin6 = svc_addr_in6(rqstp);
698 rqstp->rq_client = NULL;
699 if (rqstp->rq_proc == 0)
702 ipm = ip_map_cached_get(rqstp);
704 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
710 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
719 rqstp->rq_client = &ipm->m_client->h;
720 kref_get(&rqstp->rq_client->ref);
721 ip_map_cached_put(rqstp, ipm);
727 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
730 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
732 struct kvec *argv = &rqstp->rq_arg.head[0];
733 struct kvec *resv = &rqstp->rq_res.head[0];
734 struct svc_cred *cred = &rqstp->rq_cred;
736 cred->cr_group_info = NULL;
737 rqstp->rq_client = NULL;
739 if (argv->iov_len < 3*4)
742 if (svc_getu32(argv) != 0) {
743 dprintk("svc: bad null cred\n");
744 *authp = rpc_autherr_badcred;
747 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
748 dprintk("svc: bad null verf\n");
749 *authp = rpc_autherr_badverf;
753 /* Signal that mapping to nobody uid/gid is required */
754 cred->cr_uid = (uid_t) -1;
755 cred->cr_gid = (gid_t) -1;
756 cred->cr_group_info = groups_alloc(0);
757 if (cred->cr_group_info == NULL)
758 return SVC_DROP; /* kmalloc failure - client must retry */
760 /* Put NULL verifier */
761 svc_putnl(resv, RPC_AUTH_NULL);
764 rqstp->rq_flavor = RPC_AUTH_NULL;
769 svcauth_null_release(struct svc_rqst *rqstp)
771 if (rqstp->rq_client)
772 auth_domain_put(rqstp->rq_client);
773 rqstp->rq_client = NULL;
774 if (rqstp->rq_cred.cr_group_info)
775 put_group_info(rqstp->rq_cred.cr_group_info);
776 rqstp->rq_cred.cr_group_info = NULL;
778 return 0; /* don't drop */
782 struct auth_ops svcauth_null = {
784 .owner = THIS_MODULE,
785 .flavour = RPC_AUTH_NULL,
786 .accept = svcauth_null_accept,
787 .release = svcauth_null_release,
788 .set_client = svcauth_unix_set_client,
793 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
795 struct kvec *argv = &rqstp->rq_arg.head[0];
796 struct kvec *resv = &rqstp->rq_res.head[0];
797 struct svc_cred *cred = &rqstp->rq_cred;
799 int len = argv->iov_len;
801 cred->cr_group_info = NULL;
802 rqstp->rq_client = NULL;
804 if ((len -= 3*4) < 0)
807 svc_getu32(argv); /* length */
808 svc_getu32(argv); /* time stamp */
809 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
810 if (slen > 64 || (len -= (slen + 3)*4) < 0)
812 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
813 argv->iov_len -= slen*4;
815 cred->cr_uid = svc_getnl(argv); /* uid */
816 cred->cr_gid = svc_getnl(argv); /* gid */
817 slen = svc_getnl(argv); /* gids length */
818 if (slen > 16 || (len -= (slen + 2)*4) < 0)
820 if (unix_gid_find(cred->cr_uid, &cred->cr_group_info, rqstp)
823 if (cred->cr_group_info == NULL) {
824 cred->cr_group_info = groups_alloc(slen);
825 if (cred->cr_group_info == NULL)
827 for (i = 0; i < slen; i++)
828 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
830 for (i = 0; i < slen ; i++)
833 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
834 *authp = rpc_autherr_badverf;
838 /* Put NULL verifier */
839 svc_putnl(resv, RPC_AUTH_NULL);
842 rqstp->rq_flavor = RPC_AUTH_UNIX;
846 *authp = rpc_autherr_badcred;
851 svcauth_unix_release(struct svc_rqst *rqstp)
853 /* Verifier (such as it is) is already in place.
855 if (rqstp->rq_client)
856 auth_domain_put(rqstp->rq_client);
857 rqstp->rq_client = NULL;
858 if (rqstp->rq_cred.cr_group_info)
859 put_group_info(rqstp->rq_cred.cr_group_info);
860 rqstp->rq_cred.cr_group_info = NULL;
866 struct auth_ops svcauth_unix = {
868 .owner = THIS_MODULE,
869 .flavour = RPC_AUTH_UNIX,
870 .accept = svcauth_unix_accept,
871 .release = svcauth_unix_release,
872 .domain_release = svcauth_unix_domain_release,
873 .set_client = svcauth_unix_set_client,