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>
13 #define RPCDBG_FACILITY RPCDBG_AUTH
17 * AUTHUNIX and AUTHNULL credentials are both handled here.
18 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
19 * are always nobody (-2). i.e. we do the same IP address checks for
20 * AUTHNULL as for AUTHUNIX, and that is done here.
27 /* other stuff later */
30 struct auth_domain
*unix_domain_find(char *name
)
32 struct auth_domain
*rv
, ud
;
33 struct unix_domain
*new;
37 rv
= auth_domain_lookup(&ud
, 0);
40 if (rv
&& rv
->flavour
!= RPC_AUTH_UNIX
) {
47 new = kmalloc(sizeof(*new), GFP_KERNEL
);
50 cache_init(&new->h
.h
);
51 new->h
.name
= kstrdup(name
, GFP_KERNEL
);
52 new->h
.flavour
= RPC_AUTH_UNIX
;
53 new->addr_changes
= 0;
54 new->h
.h
.expiry_time
= NEVER
;
56 rv
= auth_domain_lookup(&new->h
, 2);
58 if (atomic_dec_and_test(&new->h
.h
.refcnt
)) BUG();
60 auth_domain_put(&new->h
);
67 static void svcauth_unix_domain_release(struct auth_domain
*dom
)
69 struct unix_domain
*ud
= container_of(dom
, struct unix_domain
, h
);
76 /**************************************************
77 * cache for IP address to unix_domain
78 * as needed by AUTH_UNIX
81 #define IP_HASHMAX (1<<IP_HASHBITS)
82 #define IP_HASHMASK (IP_HASHMAX-1)
86 char m_class
[8]; /* e.g. "nfsd" */
87 struct in_addr m_addr
;
88 struct unix_domain
*m_client
;
91 static struct cache_head
*ip_table
[IP_HASHMAX
];
93 static void ip_map_put(struct cache_head
*item
, struct cache_detail
*cd
)
95 struct ip_map
*im
= container_of(item
, struct ip_map
,h
);
96 if (cache_put(item
, cd
)) {
97 if (test_bit(CACHE_VALID
, &item
->flags
) &&
98 !test_bit(CACHE_NEGATIVE
, &item
->flags
))
99 auth_domain_put(&im
->m_client
->h
);
104 static inline int ip_map_hash(struct ip_map
*item
)
106 return hash_str(item
->m_class
, IP_HASHBITS
) ^
107 hash_long((unsigned long)item
->m_addr
.s_addr
, IP_HASHBITS
);
109 static inline int ip_map_match(struct ip_map
*item
, struct ip_map
*tmp
)
111 return strcmp(tmp
->m_class
, item
->m_class
) == 0
112 && tmp
->m_addr
.s_addr
== item
->m_addr
.s_addr
;
114 static inline void ip_map_init(struct ip_map
*new, struct ip_map
*item
)
116 strcpy(new->m_class
, item
->m_class
);
117 new->m_addr
.s_addr
= item
->m_addr
.s_addr
;
119 static inline void ip_map_update(struct ip_map
*new, struct ip_map
*item
)
121 cache_get(&item
->m_client
->h
.h
);
122 new->m_client
= item
->m_client
;
123 new->m_add_change
= item
->m_add_change
;
126 static void ip_map_request(struct cache_detail
*cd
,
127 struct cache_head
*h
,
128 char **bpp
, int *blen
)
131 struct ip_map
*im
= container_of(h
, struct ip_map
, h
);
132 __u32 addr
= im
->m_addr
.s_addr
;
134 snprintf(text_addr
, 20, "%u.%u.%u.%u",
135 ntohl(addr
) >> 24 & 0xff,
136 ntohl(addr
) >> 16 & 0xff,
137 ntohl(addr
) >> 8 & 0xff,
138 ntohl(addr
) >> 0 & 0xff);
140 qword_add(bpp
, blen
, im
->m_class
);
141 qword_add(bpp
, blen
, text_addr
);
145 static struct ip_map
*ip_map_lookup(struct ip_map
*, int);
147 static int ip_map_parse(struct cache_detail
*cd
,
148 char *mesg
, int mlen
)
150 /* class ipaddress [domainname] */
151 /* should be safe just to use the start of the input buffer
157 struct ip_map ipm
, *ipmp
;
158 struct auth_domain
*dom
;
161 if (mesg
[mlen
-1] != '\n')
166 len
= qword_get(&mesg
, ipm
.m_class
, sizeof(ipm
.m_class
));
167 if (len
<= 0) return -EINVAL
;
170 len
= qword_get(&mesg
, buf
, mlen
);
171 if (len
<= 0) return -EINVAL
;
173 if (sscanf(buf
, "%u.%u.%u.%u%c", &b1
, &b2
, &b3
, &b4
, &c
) != 4)
176 expiry
= get_expiry(&mesg
);
180 /* domainname, or empty for NEGATIVE */
181 len
= qword_get(&mesg
, buf
, mlen
);
182 if (len
< 0) return -EINVAL
;
185 dom
= unix_domain_find(buf
);
192 htonl((((((b1
<<8)|b2
)<<8)|b3
)<<8)|b4
);
195 ipm
.m_client
= container_of(dom
, struct unix_domain
, h
);
196 ipm
.m_add_change
= ipm
.m_client
->addr_changes
;
198 set_bit(CACHE_NEGATIVE
, &ipm
.h
.flags
);
199 ipm
.h
.expiry_time
= expiry
;
201 ipmp
= ip_map_lookup(&ipm
, 1);
203 ip_map_put(&ipmp
->h
, &ip_map_cache
);
205 auth_domain_put(dom
);
212 static int ip_map_show(struct seq_file
*m
,
213 struct cache_detail
*cd
,
214 struct cache_head
*h
)
218 char *dom
= "-no-domain-";
221 seq_puts(m
, "#class IP domain\n");
224 im
= container_of(h
, struct ip_map
, h
);
225 /* class addr domain */
228 if (test_bit(CACHE_VALID
, &h
->flags
) &&
229 !test_bit(CACHE_NEGATIVE
, &h
->flags
))
230 dom
= im
->m_client
->h
.name
;
232 seq_printf(m
, "%s %d.%d.%d.%d %s\n",
234 htonl(addr
.s_addr
) >> 24 & 0xff,
235 htonl(addr
.s_addr
) >> 16 & 0xff,
236 htonl(addr
.s_addr
) >> 8 & 0xff,
237 htonl(addr
.s_addr
) >> 0 & 0xff,
244 struct cache_detail ip_map_cache
= {
245 .owner
= THIS_MODULE
,
246 .hash_size
= IP_HASHMAX
,
247 .hash_table
= ip_table
,
248 .name
= "auth.unix.ip",
249 .cache_put
= ip_map_put
,
250 .cache_request
= ip_map_request
,
251 .cache_parse
= ip_map_parse
,
252 .cache_show
= ip_map_show
,
255 static DefineSimpleCacheLookup(ip_map
, 0)
258 int auth_unix_add_addr(struct in_addr addr
, struct auth_domain
*dom
)
260 struct unix_domain
*udom
;
261 struct ip_map ip
, *ipmp
;
263 if (dom
->flavour
!= RPC_AUTH_UNIX
)
265 udom
= container_of(dom
, struct unix_domain
, h
);
266 strcpy(ip
.m_class
, "nfsd");
269 ip
.m_add_change
= udom
->addr_changes
+1;
271 ip
.h
.expiry_time
= NEVER
;
273 ipmp
= ip_map_lookup(&ip
, 1);
276 ip_map_put(&ipmp
->h
, &ip_map_cache
);
282 int auth_unix_forget_old(struct auth_domain
*dom
)
284 struct unix_domain
*udom
;
286 if (dom
->flavour
!= RPC_AUTH_UNIX
)
288 udom
= container_of(dom
, struct unix_domain
, h
);
289 udom
->addr_changes
++;
293 struct auth_domain
*auth_unix_lookup(struct in_addr addr
)
295 struct ip_map key
, *ipm
;
296 struct auth_domain
*rv
;
298 strcpy(key
.m_class
, "nfsd");
301 ipm
= ip_map_lookup(&key
, 0);
305 if (cache_check(&ip_map_cache
, &ipm
->h
, NULL
))
308 if ((ipm
->m_client
->addr_changes
- ipm
->m_add_change
) >0) {
309 if (test_and_set_bit(CACHE_NEGATIVE
, &ipm
->h
.flags
) == 0)
310 auth_domain_put(&ipm
->m_client
->h
);
313 rv
= &ipm
->m_client
->h
;
316 ip_map_put(&ipm
->h
, &ip_map_cache
);
320 void svcauth_unix_purge(void)
322 cache_purge(&ip_map_cache
);
323 cache_purge(&auth_domain_cache
);
327 svcauth_unix_set_client(struct svc_rqst
*rqstp
)
329 struct ip_map key
, *ipm
;
331 rqstp
->rq_client
= NULL
;
332 if (rqstp
->rq_proc
== 0)
335 strcpy(key
.m_class
, rqstp
->rq_server
->sv_program
->pg_class
);
336 key
.m_addr
= rqstp
->rq_addr
.sin_addr
;
338 ipm
= ip_map_lookup(&key
, 0);
343 switch (cache_check(&ip_map_cache
, &ipm
->h
, &rqstp
->rq_chandle
)) {
351 rqstp
->rq_client
= &ipm
->m_client
->h
;
352 cache_get(&rqstp
->rq_client
->h
);
353 ip_map_put(&ipm
->h
, &ip_map_cache
);
360 svcauth_null_accept(struct svc_rqst
*rqstp
, u32
*authp
)
362 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
363 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
364 struct svc_cred
*cred
= &rqstp
->rq_cred
;
366 cred
->cr_group_info
= NULL
;
367 rqstp
->rq_client
= NULL
;
369 if (argv
->iov_len
< 3*4)
372 if (svc_getu32(argv
) != 0) {
373 dprintk("svc: bad null cred\n");
374 *authp
= rpc_autherr_badcred
;
377 if (svc_getu32(argv
) != RPC_AUTH_NULL
|| svc_getu32(argv
) != 0) {
378 dprintk("svc: bad null verf\n");
379 *authp
= rpc_autherr_badverf
;
383 /* Signal that mapping to nobody uid/gid is required */
384 cred
->cr_uid
= (uid_t
) -1;
385 cred
->cr_gid
= (gid_t
) -1;
386 cred
->cr_group_info
= groups_alloc(0);
387 if (cred
->cr_group_info
== NULL
)
388 return SVC_DROP
; /* kmalloc failure - client must retry */
390 /* Put NULL verifier */
391 svc_putu32(resv
, RPC_AUTH_NULL
);
398 svcauth_null_release(struct svc_rqst
*rqstp
)
400 if (rqstp
->rq_client
)
401 auth_domain_put(rqstp
->rq_client
);
402 rqstp
->rq_client
= NULL
;
403 if (rqstp
->rq_cred
.cr_group_info
)
404 put_group_info(rqstp
->rq_cred
.cr_group_info
);
405 rqstp
->rq_cred
.cr_group_info
= NULL
;
407 return 0; /* don't drop */
411 struct auth_ops svcauth_null
= {
413 .owner
= THIS_MODULE
,
414 .flavour
= RPC_AUTH_NULL
,
415 .accept
= svcauth_null_accept
,
416 .release
= svcauth_null_release
,
417 .set_client
= svcauth_unix_set_client
,
422 svcauth_unix_accept(struct svc_rqst
*rqstp
, u32
*authp
)
424 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
425 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
426 struct svc_cred
*cred
= &rqstp
->rq_cred
;
428 int len
= argv
->iov_len
;
430 cred
->cr_group_info
= NULL
;
431 rqstp
->rq_client
= NULL
;
433 if ((len
-= 3*4) < 0)
436 svc_getu32(argv
); /* length */
437 svc_getu32(argv
); /* time stamp */
438 slen
= XDR_QUADLEN(ntohl(svc_getu32(argv
))); /* machname length */
439 if (slen
> 64 || (len
-= (slen
+ 3)*4) < 0)
441 argv
->iov_base
= (void*)((u32
*)argv
->iov_base
+ slen
); /* skip machname */
442 argv
->iov_len
-= slen
*4;
444 cred
->cr_uid
= ntohl(svc_getu32(argv
)); /* uid */
445 cred
->cr_gid
= ntohl(svc_getu32(argv
)); /* gid */
446 slen
= ntohl(svc_getu32(argv
)); /* gids length */
447 if (slen
> 16 || (len
-= (slen
+ 2)*4) < 0)
449 cred
->cr_group_info
= groups_alloc(slen
);
450 if (cred
->cr_group_info
== NULL
)
452 for (i
= 0; i
< slen
; i
++)
453 GROUP_AT(cred
->cr_group_info
, i
) = ntohl(svc_getu32(argv
));
455 if (svc_getu32(argv
) != RPC_AUTH_NULL
|| svc_getu32(argv
) != 0) {
456 *authp
= rpc_autherr_badverf
;
460 /* Put NULL verifier */
461 svc_putu32(resv
, RPC_AUTH_NULL
);
467 *authp
= rpc_autherr_badcred
;
472 svcauth_unix_release(struct svc_rqst
*rqstp
)
474 /* Verifier (such as it is) is already in place.
476 if (rqstp
->rq_client
)
477 auth_domain_put(rqstp
->rq_client
);
478 rqstp
->rq_client
= NULL
;
479 if (rqstp
->rq_cred
.cr_group_info
)
480 put_group_info(rqstp
->rq_cred
.cr_group_info
);
481 rqstp
->rq_cred
.cr_group_info
= NULL
;
487 struct auth_ops svcauth_unix
= {
489 .owner
= THIS_MODULE
,
490 .flavour
= RPC_AUTH_UNIX
,
491 .accept
= svcauth_unix_accept
,
492 .release
= svcauth_unix_release
,
493 .domain_release
= svcauth_unix_domain_release
,
494 .set_client
= svcauth_unix_set_client
,