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>
9 #include <linux/seq_file.h>
10 #include <linux/hash.h>
11 #include <linux/string.h>
14 #define RPCDBG_FACILITY RPCDBG_AUTH
18 * AUTHUNIX and AUTHNULL credentials are both handled here.
19 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
20 * are always nobody (-2). i.e. we do the same IP address checks for
21 * AUTHNULL as for AUTHUNIX, and that is done here.
28 /* other stuff later */
31 extern struct auth_ops svcauth_unix
;
33 struct auth_domain
*unix_domain_find(char *name
)
35 struct auth_domain
*rv
;
36 struct unix_domain
*new = NULL
;
38 rv
= auth_domain_lookup(name
, NULL
);
41 if (new && rv
!= &new->h
)
42 auth_domain_put(&new->h
);
44 if (rv
->flavour
!= &svcauth_unix
) {
51 new = kmalloc(sizeof(*new), GFP_KERNEL
);
54 kref_init(&new->h
.ref
);
55 new->h
.name
= kstrdup(name
, GFP_KERNEL
);
56 if (new->h
.name
== NULL
) {
60 new->h
.flavour
= &svcauth_unix
;
61 new->addr_changes
= 0;
62 rv
= auth_domain_lookup(name
, &new->h
);
66 static void svcauth_unix_domain_release(struct auth_domain
*dom
)
68 struct unix_domain
*ud
= container_of(dom
, struct unix_domain
, h
);
75 /**************************************************
76 * cache for IP address to unix_domain
77 * as needed by AUTH_UNIX
80 #define IP_HASHMAX (1<<IP_HASHBITS)
81 #define IP_HASHMASK (IP_HASHMAX-1)
85 char m_class
[8]; /* e.g. "nfsd" */
86 struct in_addr m_addr
;
87 struct unix_domain
*m_client
;
90 static struct cache_head
*ip_table
[IP_HASHMAX
];
92 static void ip_map_put(struct kref
*kref
)
94 struct cache_head
*item
= container_of(kref
, struct cache_head
, ref
);
95 struct ip_map
*im
= container_of(item
, struct ip_map
,h
);
97 if (test_bit(CACHE_VALID
, &item
->flags
) &&
98 !test_bit(CACHE_NEGATIVE
, &item
->flags
))
99 auth_domain_put(&im
->m_client
->h
);
104 /* hash_long on a 64 bit machine is currently REALLY BAD for
105 * IP addresses in reverse-endian (i.e. on a little-endian machine).
106 * So use a trivial but reliable hash instead
108 static inline int hash_ip(__be32 ip
)
110 int hash
= (__force u32
)ip
^ ((__force u32
)ip
>>16);
111 return (hash
^ (hash
>>8)) & 0xff;
114 static int ip_map_match(struct cache_head
*corig
, struct cache_head
*cnew
)
116 struct ip_map
*orig
= container_of(corig
, struct ip_map
, h
);
117 struct ip_map
*new = container_of(cnew
, struct ip_map
, h
);
118 return strcmp(orig
->m_class
, new->m_class
) == 0
119 && orig
->m_addr
.s_addr
== new->m_addr
.s_addr
;
121 static void ip_map_init(struct cache_head
*cnew
, struct cache_head
*citem
)
123 struct ip_map
*new = container_of(cnew
, struct ip_map
, h
);
124 struct ip_map
*item
= container_of(citem
, struct ip_map
, h
);
126 strcpy(new->m_class
, item
->m_class
);
127 new->m_addr
.s_addr
= item
->m_addr
.s_addr
;
129 static void update(struct cache_head
*cnew
, struct cache_head
*citem
)
131 struct ip_map
*new = container_of(cnew
, struct ip_map
, h
);
132 struct ip_map
*item
= container_of(citem
, struct ip_map
, h
);
134 kref_get(&item
->m_client
->h
.ref
);
135 new->m_client
= item
->m_client
;
136 new->m_add_change
= item
->m_add_change
;
138 static struct cache_head
*ip_map_alloc(void)
140 struct ip_map
*i
= kmalloc(sizeof(*i
), GFP_KERNEL
);
147 static void ip_map_request(struct cache_detail
*cd
,
148 struct cache_head
*h
,
149 char **bpp
, int *blen
)
152 struct ip_map
*im
= container_of(h
, struct ip_map
, h
);
153 __be32 addr
= im
->m_addr
.s_addr
;
155 snprintf(text_addr
, 20, "%u.%u.%u.%u",
156 ntohl(addr
) >> 24 & 0xff,
157 ntohl(addr
) >> 16 & 0xff,
158 ntohl(addr
) >> 8 & 0xff,
159 ntohl(addr
) >> 0 & 0xff);
161 qword_add(bpp
, blen
, im
->m_class
);
162 qword_add(bpp
, blen
, text_addr
);
166 static struct ip_map
*ip_map_lookup(char *class, struct in_addr addr
);
167 static int ip_map_update(struct ip_map
*ipm
, struct unix_domain
*udom
, time_t expiry
);
169 static int ip_map_parse(struct cache_detail
*cd
,
170 char *mesg
, int mlen
)
172 /* class ipaddress [domainname] */
173 /* should be safe just to use the start of the input buffer
184 struct auth_domain
*dom
;
187 if (mesg
[mlen
-1] != '\n')
192 len
= qword_get(&mesg
, class, sizeof(class));
193 if (len
<= 0) return -EINVAL
;
196 len
= qword_get(&mesg
, buf
, mlen
);
197 if (len
<= 0) return -EINVAL
;
199 if (sscanf(buf
, "%u.%u.%u.%u%c", &b1
, &b2
, &b3
, &b4
, &c
) != 4)
202 expiry
= get_expiry(&mesg
);
206 /* domainname, or empty for NEGATIVE */
207 len
= qword_get(&mesg
, buf
, mlen
);
208 if (len
< 0) return -EINVAL
;
211 dom
= unix_domain_find(buf
);
218 htonl((((((b1
<<8)|b2
)<<8)|b3
)<<8)|b4
);
220 ipmp
= ip_map_lookup(class,addr
);
222 err
= ip_map_update(ipmp
,
223 container_of(dom
, struct unix_domain
, h
),
229 auth_domain_put(dom
);
235 static int ip_map_show(struct seq_file
*m
,
236 struct cache_detail
*cd
,
237 struct cache_head
*h
)
241 char *dom
= "-no-domain-";
244 seq_puts(m
, "#class IP domain\n");
247 im
= container_of(h
, struct ip_map
, h
);
248 /* class addr domain */
251 if (test_bit(CACHE_VALID
, &h
->flags
) &&
252 !test_bit(CACHE_NEGATIVE
, &h
->flags
))
253 dom
= im
->m_client
->h
.name
;
255 seq_printf(m
, "%s %d.%d.%d.%d %s\n",
257 ntohl(addr
.s_addr
) >> 24 & 0xff,
258 ntohl(addr
.s_addr
) >> 16 & 0xff,
259 ntohl(addr
.s_addr
) >> 8 & 0xff,
260 ntohl(addr
.s_addr
) >> 0 & 0xff,
267 struct cache_detail ip_map_cache
= {
268 .owner
= THIS_MODULE
,
269 .hash_size
= IP_HASHMAX
,
270 .hash_table
= ip_table
,
271 .name
= "auth.unix.ip",
272 .cache_put
= ip_map_put
,
273 .cache_request
= ip_map_request
,
274 .cache_parse
= ip_map_parse
,
275 .cache_show
= ip_map_show
,
276 .match
= ip_map_match
,
279 .alloc
= ip_map_alloc
,
282 static struct ip_map
*ip_map_lookup(char *class, struct in_addr addr
)
285 struct cache_head
*ch
;
287 strcpy(ip
.m_class
, class);
289 ch
= sunrpc_cache_lookup(&ip_map_cache
, &ip
.h
,
290 hash_str(class, IP_HASHBITS
) ^
291 hash_ip(addr
.s_addr
));
294 return container_of(ch
, struct ip_map
, h
);
299 static int ip_map_update(struct ip_map
*ipm
, struct unix_domain
*udom
, time_t expiry
)
302 struct cache_head
*ch
;
307 set_bit(CACHE_NEGATIVE
, &ip
.h
.flags
);
309 ip
.m_add_change
= udom
->addr_changes
;
310 /* if this is from the legacy set_client system call,
311 * we need m_add_change to be one higher
316 ip
.h
.expiry_time
= expiry
;
317 ch
= sunrpc_cache_update(&ip_map_cache
,
319 hash_str(ipm
->m_class
, IP_HASHBITS
) ^
320 hash_ip(ipm
->m_addr
.s_addr
));
323 cache_put(ch
, &ip_map_cache
);
327 int auth_unix_add_addr(struct in_addr addr
, struct auth_domain
*dom
)
329 struct unix_domain
*udom
;
332 if (dom
->flavour
!= &svcauth_unix
)
334 udom
= container_of(dom
, struct unix_domain
, h
);
335 ipmp
= ip_map_lookup("nfsd", addr
);
338 return ip_map_update(ipmp
, udom
, NEVER
);
343 int auth_unix_forget_old(struct auth_domain
*dom
)
345 struct unix_domain
*udom
;
347 if (dom
->flavour
!= &svcauth_unix
)
349 udom
= container_of(dom
, struct unix_domain
, h
);
350 udom
->addr_changes
++;
354 struct auth_domain
*auth_unix_lookup(struct in_addr addr
)
357 struct auth_domain
*rv
;
359 ipm
= ip_map_lookup("nfsd", addr
);
363 if (cache_check(&ip_map_cache
, &ipm
->h
, NULL
))
366 if ((ipm
->m_client
->addr_changes
- ipm
->m_add_change
) >0) {
367 if (test_and_set_bit(CACHE_NEGATIVE
, &ipm
->h
.flags
) == 0)
368 auth_domain_put(&ipm
->m_client
->h
);
371 rv
= &ipm
->m_client
->h
;
374 cache_put(&ipm
->h
, &ip_map_cache
);
378 void svcauth_unix_purge(void)
380 cache_purge(&ip_map_cache
);
383 static inline struct ip_map
*
384 ip_map_cached_get(struct svc_rqst
*rqstp
)
387 struct svc_sock
*svsk
= rqstp
->rq_sock
;
388 spin_lock_bh(&svsk
->sk_defer_lock
);
389 ipm
= svsk
->sk_info_authunix
;
391 if (!cache_valid(&ipm
->h
)) {
393 * The entry has been invalidated since it was
394 * remembered, e.g. by a second mount from the
397 svsk
->sk_info_authunix
= NULL
;
398 spin_unlock_bh(&svsk
->sk_defer_lock
);
399 cache_put(&ipm
->h
, &ip_map_cache
);
404 spin_unlock_bh(&svsk
->sk_defer_lock
);
409 ip_map_cached_put(struct svc_rqst
*rqstp
, struct ip_map
*ipm
)
411 struct svc_sock
*svsk
= rqstp
->rq_sock
;
413 spin_lock_bh(&svsk
->sk_defer_lock
);
414 if (svsk
->sk_sock
->type
== SOCK_STREAM
&&
415 svsk
->sk_info_authunix
== NULL
) {
416 /* newly cached, keep the reference */
417 svsk
->sk_info_authunix
= ipm
;
420 spin_unlock_bh(&svsk
->sk_defer_lock
);
422 cache_put(&ipm
->h
, &ip_map_cache
);
426 svcauth_unix_info_release(void *info
)
428 struct ip_map
*ipm
= info
;
429 cache_put(&ipm
->h
, &ip_map_cache
);
432 /****************************************************************************
433 * auth.unix.gid cache
434 * simple cache to map a UID to a list of GIDs
435 * because AUTH_UNIX aka AUTH_SYS has a max of 16
437 #define GID_HASHBITS 8
438 #define GID_HASHMAX (1<<GID_HASHBITS)
439 #define GID_HASHMASK (GID_HASHMAX - 1)
444 struct group_info
*gi
;
446 static struct cache_head
*gid_table
[GID_HASHMAX
];
448 static void unix_gid_put(struct kref
*kref
)
450 struct cache_head
*item
= container_of(kref
, struct cache_head
, ref
);
451 struct unix_gid
*ug
= container_of(item
, struct unix_gid
, h
);
452 if (test_bit(CACHE_VALID
, &item
->flags
) &&
453 !test_bit(CACHE_NEGATIVE
, &item
->flags
))
454 put_group_info(ug
->gi
);
458 static int unix_gid_match(struct cache_head
*corig
, struct cache_head
*cnew
)
460 struct unix_gid
*orig
= container_of(corig
, struct unix_gid
, h
);
461 struct unix_gid
*new = container_of(cnew
, struct unix_gid
, h
);
462 return orig
->uid
== new->uid
;
464 static void unix_gid_init(struct cache_head
*cnew
, struct cache_head
*citem
)
466 struct unix_gid
*new = container_of(cnew
, struct unix_gid
, h
);
467 struct unix_gid
*item
= container_of(citem
, struct unix_gid
, h
);
468 new->uid
= item
->uid
;
470 static void unix_gid_update(struct cache_head
*cnew
, struct cache_head
*citem
)
472 struct unix_gid
*new = container_of(cnew
, struct unix_gid
, h
);
473 struct unix_gid
*item
= container_of(citem
, struct unix_gid
, h
);
475 get_group_info(item
->gi
);
478 static struct cache_head
*unix_gid_alloc(void)
480 struct unix_gid
*g
= kmalloc(sizeof(*g
), GFP_KERNEL
);
487 static void unix_gid_request(struct cache_detail
*cd
,
488 struct cache_head
*h
,
489 char **bpp
, int *blen
)
492 struct unix_gid
*ug
= container_of(h
, struct unix_gid
, h
);
494 snprintf(tuid
, 20, "%u", ug
->uid
);
495 qword_add(bpp
, blen
, tuid
);
499 static struct unix_gid
*unix_gid_lookup(uid_t uid
);
500 extern struct cache_detail unix_gid_cache
;
502 static int unix_gid_parse(struct cache_detail
*cd
,
503 char *mesg
, int mlen
)
505 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
512 struct unix_gid ug
, *ugp
;
514 if (mlen
<= 0 || mesg
[mlen
-1] != '\n')
518 rv
= get_int(&mesg
, &uid
);
523 expiry
= get_expiry(&mesg
);
527 rv
= get_int(&mesg
, &gids
);
528 if (rv
|| gids
< 0 || gids
> 8192)
531 ug
.gi
= groups_alloc(gids
);
535 for (i
= 0 ; i
< gids
; i
++) {
537 rv
= get_int(&mesg
, &gid
);
541 GROUP_AT(ug
.gi
, i
) = gid
;
544 ugp
= unix_gid_lookup(uid
);
546 struct cache_head
*ch
;
548 ug
.h
.expiry_time
= expiry
;
549 ch
= sunrpc_cache_update(&unix_gid_cache
,
551 hash_long(uid
, GID_HASHBITS
));
556 cache_put(ch
, &unix_gid_cache
);
562 put_group_info(ug
.gi
);
566 static int unix_gid_show(struct seq_file
*m
,
567 struct cache_detail
*cd
,
568 struct cache_head
*h
)
575 seq_puts(m
, "#uid cnt: gids...\n");
578 ug
= container_of(h
, struct unix_gid
, h
);
579 if (test_bit(CACHE_VALID
, &h
->flags
) &&
580 !test_bit(CACHE_NEGATIVE
, &h
->flags
))
581 glen
= ug
->gi
->ngroups
;
585 seq_printf(m
, "%d %d:", ug
->uid
, glen
);
586 for (i
= 0; i
< glen
; i
++)
587 seq_printf(m
, " %d", GROUP_AT(ug
->gi
, i
));
592 struct cache_detail unix_gid_cache
= {
593 .owner
= THIS_MODULE
,
594 .hash_size
= GID_HASHMAX
,
595 .hash_table
= gid_table
,
596 .name
= "auth.unix.gid",
597 .cache_put
= unix_gid_put
,
598 .cache_request
= unix_gid_request
,
599 .cache_parse
= unix_gid_parse
,
600 .cache_show
= unix_gid_show
,
601 .match
= unix_gid_match
,
602 .init
= unix_gid_init
,
603 .update
= unix_gid_update
,
604 .alloc
= unix_gid_alloc
,
607 static struct unix_gid
*unix_gid_lookup(uid_t uid
)
610 struct cache_head
*ch
;
613 ch
= sunrpc_cache_lookup(&unix_gid_cache
, &ug
.h
,
614 hash_long(uid
, GID_HASHBITS
));
616 return container_of(ch
, struct unix_gid
, h
);
621 static int unix_gid_find(uid_t uid
, struct group_info
**gip
,
622 struct svc_rqst
*rqstp
)
624 struct unix_gid
*ug
= unix_gid_lookup(uid
);
627 switch (cache_check(&unix_gid_cache
, &ug
->h
, &rqstp
->rq_chandle
)) {
633 get_group_info(*gip
);
641 svcauth_unix_set_client(struct svc_rqst
*rqstp
)
643 struct sockaddr_in
*sin
= svc_addr_in(rqstp
);
646 rqstp
->rq_client
= NULL
;
647 if (rqstp
->rq_proc
== 0)
650 ipm
= ip_map_cached_get(rqstp
);
652 ipm
= ip_map_lookup(rqstp
->rq_server
->sv_program
->pg_class
,
658 switch (cache_check(&ip_map_cache
, &ipm
->h
, &rqstp
->rq_chandle
)) {
667 rqstp
->rq_client
= &ipm
->m_client
->h
;
668 kref_get(&rqstp
->rq_client
->ref
);
669 ip_map_cached_put(rqstp
, ipm
);
676 svcauth_null_accept(struct svc_rqst
*rqstp
, __be32
*authp
)
678 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
679 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
680 struct svc_cred
*cred
= &rqstp
->rq_cred
;
682 cred
->cr_group_info
= NULL
;
683 rqstp
->rq_client
= NULL
;
685 if (argv
->iov_len
< 3*4)
688 if (svc_getu32(argv
) != 0) {
689 dprintk("svc: bad null cred\n");
690 *authp
= rpc_autherr_badcred
;
693 if (svc_getu32(argv
) != htonl(RPC_AUTH_NULL
) || svc_getu32(argv
) != 0) {
694 dprintk("svc: bad null verf\n");
695 *authp
= rpc_autherr_badverf
;
699 /* Signal that mapping to nobody uid/gid is required */
700 cred
->cr_uid
= (uid_t
) -1;
701 cred
->cr_gid
= (gid_t
) -1;
702 cred
->cr_group_info
= groups_alloc(0);
703 if (cred
->cr_group_info
== NULL
)
704 return SVC_DROP
; /* kmalloc failure - client must retry */
706 /* Put NULL verifier */
707 svc_putnl(resv
, RPC_AUTH_NULL
);
714 svcauth_null_release(struct svc_rqst
*rqstp
)
716 if (rqstp
->rq_client
)
717 auth_domain_put(rqstp
->rq_client
);
718 rqstp
->rq_client
= NULL
;
719 if (rqstp
->rq_cred
.cr_group_info
)
720 put_group_info(rqstp
->rq_cred
.cr_group_info
);
721 rqstp
->rq_cred
.cr_group_info
= NULL
;
723 return 0; /* don't drop */
727 struct auth_ops svcauth_null
= {
729 .owner
= THIS_MODULE
,
730 .flavour
= RPC_AUTH_NULL
,
731 .accept
= svcauth_null_accept
,
732 .release
= svcauth_null_release
,
733 .set_client
= svcauth_unix_set_client
,
738 svcauth_unix_accept(struct svc_rqst
*rqstp
, __be32
*authp
)
740 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
741 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
742 struct svc_cred
*cred
= &rqstp
->rq_cred
;
744 int len
= argv
->iov_len
;
746 cred
->cr_group_info
= NULL
;
747 rqstp
->rq_client
= NULL
;
749 if ((len
-= 3*4) < 0)
752 svc_getu32(argv
); /* length */
753 svc_getu32(argv
); /* time stamp */
754 slen
= XDR_QUADLEN(svc_getnl(argv
)); /* machname length */
755 if (slen
> 64 || (len
-= (slen
+ 3)*4) < 0)
757 argv
->iov_base
= (void*)((__be32
*)argv
->iov_base
+ slen
); /* skip machname */
758 argv
->iov_len
-= slen
*4;
760 cred
->cr_uid
= svc_getnl(argv
); /* uid */
761 cred
->cr_gid
= svc_getnl(argv
); /* gid */
762 slen
= svc_getnl(argv
); /* gids length */
763 if (slen
> 16 || (len
-= (slen
+ 2)*4) < 0)
765 if (unix_gid_find(cred
->cr_uid
, &cred
->cr_group_info
, rqstp
)
768 if (cred
->cr_group_info
== NULL
) {
769 cred
->cr_group_info
= groups_alloc(slen
);
770 if (cred
->cr_group_info
== NULL
)
772 for (i
= 0; i
< slen
; i
++)
773 GROUP_AT(cred
->cr_group_info
, i
) = svc_getnl(argv
);
775 for (i
= 0; i
< slen
; i
++)
778 if (svc_getu32(argv
) != htonl(RPC_AUTH_NULL
) || svc_getu32(argv
) != 0) {
779 *authp
= rpc_autherr_badverf
;
783 /* Put NULL verifier */
784 svc_putnl(resv
, RPC_AUTH_NULL
);
790 *authp
= rpc_autherr_badcred
;
795 svcauth_unix_release(struct svc_rqst
*rqstp
)
797 /* Verifier (such as it is) is already in place.
799 if (rqstp
->rq_client
)
800 auth_domain_put(rqstp
->rq_client
);
801 rqstp
->rq_client
= NULL
;
802 if (rqstp
->rq_cred
.cr_group_info
)
803 put_group_info(rqstp
->rq_cred
.cr_group_info
);
804 rqstp
->rq_cred
.cr_group_info
= NULL
;
810 struct auth_ops svcauth_unix
= {
812 .owner
= THIS_MODULE
,
813 .flavour
= RPC_AUTH_UNIX
,
814 .accept
= svcauth_unix_accept
,
815 .release
= svcauth_unix_release
,
816 .domain_release
= svcauth_unix_domain_release
,
817 .set_client
= svcauth_unix_set_client
,