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(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, NIPQUAD_FMT,
166 ntohl(im->m_addr.s6_addr32[3]) >> 24 & 0xff,
167 ntohl(im->m_addr.s6_addr32[3]) >> 16 & 0xff,
168 ntohl(im->m_addr.s6_addr32[3]) >> 8 & 0xff,
169 ntohl(im->m_addr.s6_addr32[3]) >> 0 & 0xff);
171 snprintf(text_addr, 40, "%p6", &im->m_addr);
173 qword_add(bpp, blen, im->m_class);
174 qword_add(bpp, blen, text_addr);
178 static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr);
179 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
181 static int ip_map_parse(struct cache_detail *cd,
182 char *mesg, int mlen)
184 /* class ipaddress [domainname] */
185 /* should be safe just to use the start of the input buffer
189 int b1, b2, b3, b4, b5, b6, b7, b8;
192 struct in6_addr addr;
196 struct auth_domain *dom;
199 if (mesg[mlen-1] != '\n')
204 len = qword_get(&mesg, class, sizeof(class));
205 if (len <= 0) return -EINVAL;
208 len = qword_get(&mesg, buf, mlen);
209 if (len <= 0) return -EINVAL;
211 if (sscanf(buf, NIPQUAD_FMT "%c", &b1, &b2, &b3, &b4, &c) == 4) {
212 addr.s6_addr32[0] = 0;
213 addr.s6_addr32[1] = 0;
214 addr.s6_addr32[2] = htonl(0xffff);
216 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
217 } else if (sscanf(buf, NIP6_FMT "%c",
218 &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8, &c) == 8) {
219 addr.s6_addr16[0] = htons(b1);
220 addr.s6_addr16[1] = htons(b2);
221 addr.s6_addr16[2] = htons(b3);
222 addr.s6_addr16[3] = htons(b4);
223 addr.s6_addr16[4] = htons(b5);
224 addr.s6_addr16[5] = htons(b6);
225 addr.s6_addr16[6] = htons(b7);
226 addr.s6_addr16[7] = htons(b8);
230 expiry = get_expiry(&mesg);
234 /* domainname, or empty for NEGATIVE */
235 len = qword_get(&mesg, buf, mlen);
236 if (len < 0) return -EINVAL;
239 dom = unix_domain_find(buf);
245 ipmp = ip_map_lookup(class, &addr);
247 err = ip_map_update(ipmp,
248 container_of(dom, struct unix_domain, h),
254 auth_domain_put(dom);
260 static int ip_map_show(struct seq_file *m,
261 struct cache_detail *cd,
262 struct cache_head *h)
265 struct in6_addr addr;
266 char *dom = "-no-domain-";
269 seq_puts(m, "#class IP domain\n");
272 im = container_of(h, struct ip_map, h);
273 /* class addr domain */
274 ipv6_addr_copy(&addr, &im->m_addr);
276 if (test_bit(CACHE_VALID, &h->flags) &&
277 !test_bit(CACHE_NEGATIVE, &h->flags))
278 dom = im->m_client->h.name;
280 if (ipv6_addr_v4mapped(&addr)) {
281 seq_printf(m, "%s " NIPQUAD_FMT " %s\n",
283 ntohl(addr.s6_addr32[3]) >> 24 & 0xff,
284 ntohl(addr.s6_addr32[3]) >> 16 & 0xff,
285 ntohl(addr.s6_addr32[3]) >> 8 & 0xff,
286 ntohl(addr.s6_addr32[3]) >> 0 & 0xff,
289 seq_printf(m, "%s %p6 %s\n", im->m_class, &addr, dom);
295 struct cache_detail ip_map_cache = {
296 .owner = THIS_MODULE,
297 .hash_size = IP_HASHMAX,
298 .hash_table = ip_table,
299 .name = "auth.unix.ip",
300 .cache_put = ip_map_put,
301 .cache_request = ip_map_request,
302 .cache_parse = ip_map_parse,
303 .cache_show = ip_map_show,
304 .match = ip_map_match,
307 .alloc = ip_map_alloc,
310 static struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
313 struct cache_head *ch;
315 strcpy(ip.m_class, class);
316 ipv6_addr_copy(&ip.m_addr, addr);
317 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
318 hash_str(class, IP_HASHBITS) ^
322 return container_of(ch, struct ip_map, h);
327 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
330 struct cache_head *ch;
335 set_bit(CACHE_NEGATIVE, &ip.h.flags);
337 ip.m_add_change = udom->addr_changes;
338 /* if this is from the legacy set_client system call,
339 * we need m_add_change to be one higher
344 ip.h.expiry_time = expiry;
345 ch = sunrpc_cache_update(&ip_map_cache,
347 hash_str(ipm->m_class, IP_HASHBITS) ^
348 hash_ip6(ipm->m_addr));
351 cache_put(ch, &ip_map_cache);
355 int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
357 struct unix_domain *udom;
360 if (dom->flavour != &svcauth_unix)
362 udom = container_of(dom, struct unix_domain, h);
363 ipmp = ip_map_lookup("nfsd", addr);
366 return ip_map_update(ipmp, udom, NEVER);
370 EXPORT_SYMBOL(auth_unix_add_addr);
372 int auth_unix_forget_old(struct auth_domain *dom)
374 struct unix_domain *udom;
376 if (dom->flavour != &svcauth_unix)
378 udom = container_of(dom, struct unix_domain, h);
379 udom->addr_changes++;
382 EXPORT_SYMBOL(auth_unix_forget_old);
384 struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
387 struct auth_domain *rv;
389 ipm = ip_map_lookup("nfsd", addr);
393 if (cache_check(&ip_map_cache, &ipm->h, NULL))
396 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
397 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
398 auth_domain_put(&ipm->m_client->h);
401 rv = &ipm->m_client->h;
404 cache_put(&ipm->h, &ip_map_cache);
407 EXPORT_SYMBOL(auth_unix_lookup);
409 void svcauth_unix_purge(void)
411 cache_purge(&ip_map_cache);
413 EXPORT_SYMBOL(svcauth_unix_purge);
415 static inline struct ip_map *
416 ip_map_cached_get(struct svc_rqst *rqstp)
418 struct ip_map *ipm = NULL;
419 struct svc_xprt *xprt = rqstp->rq_xprt;
421 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
422 spin_lock(&xprt->xpt_lock);
423 ipm = xprt->xpt_auth_cache;
425 if (!cache_valid(&ipm->h)) {
427 * The entry has been invalidated since it was
428 * remembered, e.g. by a second mount from the
431 xprt->xpt_auth_cache = NULL;
432 spin_unlock(&xprt->xpt_lock);
433 cache_put(&ipm->h, &ip_map_cache);
438 spin_unlock(&xprt->xpt_lock);
444 ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
446 struct svc_xprt *xprt = rqstp->rq_xprt;
448 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
449 spin_lock(&xprt->xpt_lock);
450 if (xprt->xpt_auth_cache == NULL) {
451 /* newly cached, keep the reference */
452 xprt->xpt_auth_cache = ipm;
455 spin_unlock(&xprt->xpt_lock);
458 cache_put(&ipm->h, &ip_map_cache);
462 svcauth_unix_info_release(void *info)
464 struct ip_map *ipm = info;
465 cache_put(&ipm->h, &ip_map_cache);
468 /****************************************************************************
469 * auth.unix.gid cache
470 * simple cache to map a UID to a list of GIDs
471 * because AUTH_UNIX aka AUTH_SYS has a max of 16
473 #define GID_HASHBITS 8
474 #define GID_HASHMAX (1<<GID_HASHBITS)
475 #define GID_HASHMASK (GID_HASHMAX - 1)
480 struct group_info *gi;
482 static struct cache_head *gid_table[GID_HASHMAX];
484 static void unix_gid_put(struct kref *kref)
486 struct cache_head *item = container_of(kref, struct cache_head, ref);
487 struct unix_gid *ug = container_of(item, struct unix_gid, h);
488 if (test_bit(CACHE_VALID, &item->flags) &&
489 !test_bit(CACHE_NEGATIVE, &item->flags))
490 put_group_info(ug->gi);
494 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
496 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
497 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
498 return orig->uid == new->uid;
500 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
502 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
503 struct unix_gid *item = container_of(citem, struct unix_gid, h);
504 new->uid = item->uid;
506 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
508 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
509 struct unix_gid *item = container_of(citem, struct unix_gid, h);
511 get_group_info(item->gi);
514 static struct cache_head *unix_gid_alloc(void)
516 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
523 static void unix_gid_request(struct cache_detail *cd,
524 struct cache_head *h,
525 char **bpp, int *blen)
528 struct unix_gid *ug = container_of(h, struct unix_gid, h);
530 snprintf(tuid, 20, "%u", ug->uid);
531 qword_add(bpp, blen, tuid);
535 static struct unix_gid *unix_gid_lookup(uid_t uid);
536 extern struct cache_detail unix_gid_cache;
538 static int unix_gid_parse(struct cache_detail *cd,
539 char *mesg, int mlen)
541 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
548 struct unix_gid ug, *ugp;
550 if (mlen <= 0 || mesg[mlen-1] != '\n')
554 rv = get_int(&mesg, &uid);
559 expiry = get_expiry(&mesg);
563 rv = get_int(&mesg, &gids);
564 if (rv || gids < 0 || gids > 8192)
567 ug.gi = groups_alloc(gids);
571 for (i = 0 ; i < gids ; i++) {
573 rv = get_int(&mesg, &gid);
577 GROUP_AT(ug.gi, i) = gid;
580 ugp = unix_gid_lookup(uid);
582 struct cache_head *ch;
584 ug.h.expiry_time = expiry;
585 ch = sunrpc_cache_update(&unix_gid_cache,
587 hash_long(uid, GID_HASHBITS));
592 cache_put(ch, &unix_gid_cache);
598 put_group_info(ug.gi);
602 static int unix_gid_show(struct seq_file *m,
603 struct cache_detail *cd,
604 struct cache_head *h)
611 seq_puts(m, "#uid cnt: gids...\n");
614 ug = container_of(h, struct unix_gid, h);
615 if (test_bit(CACHE_VALID, &h->flags) &&
616 !test_bit(CACHE_NEGATIVE, &h->flags))
617 glen = ug->gi->ngroups;
621 seq_printf(m, "%d %d:", ug->uid, glen);
622 for (i = 0; i < glen; i++)
623 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
628 struct cache_detail unix_gid_cache = {
629 .owner = THIS_MODULE,
630 .hash_size = GID_HASHMAX,
631 .hash_table = gid_table,
632 .name = "auth.unix.gid",
633 .cache_put = unix_gid_put,
634 .cache_request = unix_gid_request,
635 .cache_parse = unix_gid_parse,
636 .cache_show = unix_gid_show,
637 .match = unix_gid_match,
638 .init = unix_gid_init,
639 .update = unix_gid_update,
640 .alloc = unix_gid_alloc,
643 static struct unix_gid *unix_gid_lookup(uid_t uid)
646 struct cache_head *ch;
649 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
650 hash_long(uid, GID_HASHBITS));
652 return container_of(ch, struct unix_gid, h);
657 static int unix_gid_find(uid_t uid, struct group_info **gip,
658 struct svc_rqst *rqstp)
660 struct unix_gid *ug = unix_gid_lookup(uid);
663 switch (cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle)) {
669 get_group_info(*gip);
677 svcauth_unix_set_client(struct svc_rqst *rqstp)
679 struct sockaddr_in *sin;
680 struct sockaddr_in6 *sin6, sin6_storage;
683 switch (rqstp->rq_addr.ss_family) {
685 sin = svc_addr_in(rqstp);
686 sin6 = &sin6_storage;
687 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
688 htonl(0x0000FFFF), sin->sin_addr.s_addr);
691 sin6 = svc_addr_in6(rqstp);
697 rqstp->rq_client = NULL;
698 if (rqstp->rq_proc == 0)
701 ipm = ip_map_cached_get(rqstp);
703 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
709 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
718 rqstp->rq_client = &ipm->m_client->h;
719 kref_get(&rqstp->rq_client->ref);
720 ip_map_cached_put(rqstp, ipm);
726 EXPORT_SYMBOL(svcauth_unix_set_client);
729 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
731 struct kvec *argv = &rqstp->rq_arg.head[0];
732 struct kvec *resv = &rqstp->rq_res.head[0];
733 struct svc_cred *cred = &rqstp->rq_cred;
735 cred->cr_group_info = NULL;
736 rqstp->rq_client = NULL;
738 if (argv->iov_len < 3*4)
741 if (svc_getu32(argv) != 0) {
742 dprintk("svc: bad null cred\n");
743 *authp = rpc_autherr_badcred;
746 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
747 dprintk("svc: bad null verf\n");
748 *authp = rpc_autherr_badverf;
752 /* Signal that mapping to nobody uid/gid is required */
753 cred->cr_uid = (uid_t) -1;
754 cred->cr_gid = (gid_t) -1;
755 cred->cr_group_info = groups_alloc(0);
756 if (cred->cr_group_info == NULL)
757 return SVC_DROP; /* kmalloc failure - client must retry */
759 /* Put NULL verifier */
760 svc_putnl(resv, RPC_AUTH_NULL);
763 rqstp->rq_flavor = RPC_AUTH_NULL;
768 svcauth_null_release(struct svc_rqst *rqstp)
770 if (rqstp->rq_client)
771 auth_domain_put(rqstp->rq_client);
772 rqstp->rq_client = NULL;
773 if (rqstp->rq_cred.cr_group_info)
774 put_group_info(rqstp->rq_cred.cr_group_info);
775 rqstp->rq_cred.cr_group_info = NULL;
777 return 0; /* don't drop */
781 struct auth_ops svcauth_null = {
783 .owner = THIS_MODULE,
784 .flavour = RPC_AUTH_NULL,
785 .accept = svcauth_null_accept,
786 .release = svcauth_null_release,
787 .set_client = svcauth_unix_set_client,
792 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
794 struct kvec *argv = &rqstp->rq_arg.head[0];
795 struct kvec *resv = &rqstp->rq_res.head[0];
796 struct svc_cred *cred = &rqstp->rq_cred;
798 int len = argv->iov_len;
800 cred->cr_group_info = NULL;
801 rqstp->rq_client = NULL;
803 if ((len -= 3*4) < 0)
806 svc_getu32(argv); /* length */
807 svc_getu32(argv); /* time stamp */
808 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
809 if (slen > 64 || (len -= (slen + 3)*4) < 0)
811 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
812 argv->iov_len -= slen*4;
814 cred->cr_uid = svc_getnl(argv); /* uid */
815 cred->cr_gid = svc_getnl(argv); /* gid */
816 slen = svc_getnl(argv); /* gids length */
817 if (slen > 16 || (len -= (slen + 2)*4) < 0)
819 if (unix_gid_find(cred->cr_uid, &cred->cr_group_info, rqstp)
822 if (cred->cr_group_info == NULL) {
823 cred->cr_group_info = groups_alloc(slen);
824 if (cred->cr_group_info == NULL)
826 for (i = 0; i < slen; i++)
827 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
829 for (i = 0; i < slen ; i++)
832 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
833 *authp = rpc_autherr_badverf;
837 /* Put NULL verifier */
838 svc_putnl(resv, RPC_AUTH_NULL);
841 rqstp->rq_flavor = RPC_AUTH_UNIX;
845 *authp = rpc_autherr_badcred;
850 svcauth_unix_release(struct svc_rqst *rqstp)
852 /* Verifier (such as it is) is already in place.
854 if (rqstp->rq_client)
855 auth_domain_put(rqstp->rq_client);
856 rqstp->rq_client = NULL;
857 if (rqstp->rq_cred.cr_group_info)
858 put_group_info(rqstp->rq_cred.cr_group_info);
859 rqstp->rq_cred.cr_group_info = NULL;
865 struct auth_ops svcauth_unix = {
867 .owner = THIS_MODULE,
868 .flavour = RPC_AUTH_UNIX,
869 .accept = svcauth_unix_accept,
870 .release = svcauth_unix_release,
871 .domain_release = svcauth_unix_domain_release,
872 .set_client = svcauth_unix_set_client,