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
);
105 /* hash_long on a 64 bit machine is currently REALLY BAD for
106 * IP addresses in reverse-endian (i.e. on a little-endian machine).
107 * So use a trivial but reliable hash instead
109 static inline int hash_ip(unsigned long ip
)
111 int hash
= ip
^ (ip
>>16);
112 return (hash
^ (hash
>>8)) & 0xff;
116 static inline int ip_map_hash(struct ip_map
*item
)
118 return hash_str(item
->m_class
, IP_HASHBITS
) ^
119 hash_ip((unsigned long)item
->m_addr
.s_addr
);
121 static inline int ip_map_match(struct ip_map
*item
, struct ip_map
*tmp
)
123 return strcmp(tmp
->m_class
, item
->m_class
) == 0
124 && tmp
->m_addr
.s_addr
== item
->m_addr
.s_addr
;
126 static inline void ip_map_init(struct ip_map
*new, struct ip_map
*item
)
128 strcpy(new->m_class
, item
->m_class
);
129 new->m_addr
.s_addr
= item
->m_addr
.s_addr
;
131 static inline void ip_map_update(struct ip_map
*new, struct ip_map
*item
)
133 cache_get(&item
->m_client
->h
.h
);
134 new->m_client
= item
->m_client
;
135 new->m_add_change
= item
->m_add_change
;
138 static void ip_map_request(struct cache_detail
*cd
,
139 struct cache_head
*h
,
140 char **bpp
, int *blen
)
143 struct ip_map
*im
= container_of(h
, struct ip_map
, h
);
144 __u32 addr
= im
->m_addr
.s_addr
;
146 snprintf(text_addr
, 20, "%u.%u.%u.%u",
147 ntohl(addr
) >> 24 & 0xff,
148 ntohl(addr
) >> 16 & 0xff,
149 ntohl(addr
) >> 8 & 0xff,
150 ntohl(addr
) >> 0 & 0xff);
152 qword_add(bpp
, blen
, im
->m_class
);
153 qword_add(bpp
, blen
, text_addr
);
157 static struct ip_map
*ip_map_lookup(struct ip_map
*, int);
159 static int ip_map_parse(struct cache_detail
*cd
,
160 char *mesg
, int mlen
)
162 /* class ipaddress [domainname] */
163 /* should be safe just to use the start of the input buffer
169 struct ip_map ipm
, *ipmp
;
170 struct auth_domain
*dom
;
173 if (mesg
[mlen
-1] != '\n')
178 len
= qword_get(&mesg
, ipm
.m_class
, sizeof(ipm
.m_class
));
179 if (len
<= 0) return -EINVAL
;
182 len
= qword_get(&mesg
, buf
, mlen
);
183 if (len
<= 0) return -EINVAL
;
185 if (sscanf(buf
, "%u.%u.%u.%u%c", &b1
, &b2
, &b3
, &b4
, &c
) != 4)
188 expiry
= get_expiry(&mesg
);
192 /* domainname, or empty for NEGATIVE */
193 len
= qword_get(&mesg
, buf
, mlen
);
194 if (len
< 0) return -EINVAL
;
197 dom
= unix_domain_find(buf
);
204 htonl((((((b1
<<8)|b2
)<<8)|b3
)<<8)|b4
);
207 ipm
.m_client
= container_of(dom
, struct unix_domain
, h
);
208 ipm
.m_add_change
= ipm
.m_client
->addr_changes
;
210 set_bit(CACHE_NEGATIVE
, &ipm
.h
.flags
);
211 ipm
.h
.expiry_time
= expiry
;
213 ipmp
= ip_map_lookup(&ipm
, 1);
215 ip_map_put(&ipmp
->h
, &ip_map_cache
);
217 auth_domain_put(dom
);
224 static int ip_map_show(struct seq_file
*m
,
225 struct cache_detail
*cd
,
226 struct cache_head
*h
)
230 char *dom
= "-no-domain-";
233 seq_puts(m
, "#class IP domain\n");
236 im
= container_of(h
, struct ip_map
, h
);
237 /* class addr domain */
240 if (test_bit(CACHE_VALID
, &h
->flags
) &&
241 !test_bit(CACHE_NEGATIVE
, &h
->flags
))
242 dom
= im
->m_client
->h
.name
;
244 seq_printf(m
, "%s %d.%d.%d.%d %s\n",
246 htonl(addr
.s_addr
) >> 24 & 0xff,
247 htonl(addr
.s_addr
) >> 16 & 0xff,
248 htonl(addr
.s_addr
) >> 8 & 0xff,
249 htonl(addr
.s_addr
) >> 0 & 0xff,
256 struct cache_detail ip_map_cache
= {
257 .owner
= THIS_MODULE
,
258 .hash_size
= IP_HASHMAX
,
259 .hash_table
= ip_table
,
260 .name
= "auth.unix.ip",
261 .cache_put
= ip_map_put
,
262 .cache_request
= ip_map_request
,
263 .cache_parse
= ip_map_parse
,
264 .cache_show
= ip_map_show
,
267 static DefineSimpleCacheLookup(ip_map
, 0)
270 int auth_unix_add_addr(struct in_addr addr
, struct auth_domain
*dom
)
272 struct unix_domain
*udom
;
273 struct ip_map ip
, *ipmp
;
275 if (dom
->flavour
!= RPC_AUTH_UNIX
)
277 udom
= container_of(dom
, struct unix_domain
, h
);
278 strcpy(ip
.m_class
, "nfsd");
281 ip
.m_add_change
= udom
->addr_changes
+1;
283 ip
.h
.expiry_time
= NEVER
;
285 ipmp
= ip_map_lookup(&ip
, 1);
288 ip_map_put(&ipmp
->h
, &ip_map_cache
);
294 int auth_unix_forget_old(struct auth_domain
*dom
)
296 struct unix_domain
*udom
;
298 if (dom
->flavour
!= RPC_AUTH_UNIX
)
300 udom
= container_of(dom
, struct unix_domain
, h
);
301 udom
->addr_changes
++;
305 struct auth_domain
*auth_unix_lookup(struct in_addr addr
)
307 struct ip_map key
, *ipm
;
308 struct auth_domain
*rv
;
310 strcpy(key
.m_class
, "nfsd");
313 ipm
= ip_map_lookup(&key
, 0);
317 if (cache_check(&ip_map_cache
, &ipm
->h
, NULL
))
320 if ((ipm
->m_client
->addr_changes
- ipm
->m_add_change
) >0) {
321 if (test_and_set_bit(CACHE_NEGATIVE
, &ipm
->h
.flags
) == 0)
322 auth_domain_put(&ipm
->m_client
->h
);
325 rv
= &ipm
->m_client
->h
;
328 ip_map_put(&ipm
->h
, &ip_map_cache
);
332 void svcauth_unix_purge(void)
334 cache_purge(&ip_map_cache
);
335 cache_purge(&auth_domain_cache
);
339 svcauth_unix_set_client(struct svc_rqst
*rqstp
)
341 struct ip_map key
, *ipm
;
343 rqstp
->rq_client
= NULL
;
344 if (rqstp
->rq_proc
== 0)
347 strcpy(key
.m_class
, rqstp
->rq_server
->sv_program
->pg_class
);
348 key
.m_addr
= rqstp
->rq_addr
.sin_addr
;
350 ipm
= ip_map_lookup(&key
, 0);
355 switch (cache_check(&ip_map_cache
, &ipm
->h
, &rqstp
->rq_chandle
)) {
363 rqstp
->rq_client
= &ipm
->m_client
->h
;
364 cache_get(&rqstp
->rq_client
->h
);
365 ip_map_put(&ipm
->h
, &ip_map_cache
);
372 svcauth_null_accept(struct svc_rqst
*rqstp
, u32
*authp
)
374 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
375 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
376 struct svc_cred
*cred
= &rqstp
->rq_cred
;
378 cred
->cr_group_info
= NULL
;
379 rqstp
->rq_client
= NULL
;
381 if (argv
->iov_len
< 3*4)
384 if (svc_getu32(argv
) != 0) {
385 dprintk("svc: bad null cred\n");
386 *authp
= rpc_autherr_badcred
;
389 if (svc_getu32(argv
) != RPC_AUTH_NULL
|| svc_getu32(argv
) != 0) {
390 dprintk("svc: bad null verf\n");
391 *authp
= rpc_autherr_badverf
;
395 /* Signal that mapping to nobody uid/gid is required */
396 cred
->cr_uid
= (uid_t
) -1;
397 cred
->cr_gid
= (gid_t
) -1;
398 cred
->cr_group_info
= groups_alloc(0);
399 if (cred
->cr_group_info
== NULL
)
400 return SVC_DROP
; /* kmalloc failure - client must retry */
402 /* Put NULL verifier */
403 svc_putu32(resv
, RPC_AUTH_NULL
);
410 svcauth_null_release(struct svc_rqst
*rqstp
)
412 if (rqstp
->rq_client
)
413 auth_domain_put(rqstp
->rq_client
);
414 rqstp
->rq_client
= NULL
;
415 if (rqstp
->rq_cred
.cr_group_info
)
416 put_group_info(rqstp
->rq_cred
.cr_group_info
);
417 rqstp
->rq_cred
.cr_group_info
= NULL
;
419 return 0; /* don't drop */
423 struct auth_ops svcauth_null
= {
425 .owner
= THIS_MODULE
,
426 .flavour
= RPC_AUTH_NULL
,
427 .accept
= svcauth_null_accept
,
428 .release
= svcauth_null_release
,
429 .set_client
= svcauth_unix_set_client
,
434 svcauth_unix_accept(struct svc_rqst
*rqstp
, u32
*authp
)
436 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
437 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
438 struct svc_cred
*cred
= &rqstp
->rq_cred
;
440 int len
= argv
->iov_len
;
442 cred
->cr_group_info
= NULL
;
443 rqstp
->rq_client
= NULL
;
445 if ((len
-= 3*4) < 0)
448 svc_getu32(argv
); /* length */
449 svc_getu32(argv
); /* time stamp */
450 slen
= XDR_QUADLEN(ntohl(svc_getu32(argv
))); /* machname length */
451 if (slen
> 64 || (len
-= (slen
+ 3)*4) < 0)
453 argv
->iov_base
= (void*)((u32
*)argv
->iov_base
+ slen
); /* skip machname */
454 argv
->iov_len
-= slen
*4;
456 cred
->cr_uid
= ntohl(svc_getu32(argv
)); /* uid */
457 cred
->cr_gid
= ntohl(svc_getu32(argv
)); /* gid */
458 slen
= ntohl(svc_getu32(argv
)); /* gids length */
459 if (slen
> 16 || (len
-= (slen
+ 2)*4) < 0)
461 cred
->cr_group_info
= groups_alloc(slen
);
462 if (cred
->cr_group_info
== NULL
)
464 for (i
= 0; i
< slen
; i
++)
465 GROUP_AT(cred
->cr_group_info
, i
) = ntohl(svc_getu32(argv
));
467 if (svc_getu32(argv
) != RPC_AUTH_NULL
|| svc_getu32(argv
) != 0) {
468 *authp
= rpc_autherr_badverf
;
472 /* Put NULL verifier */
473 svc_putu32(resv
, RPC_AUTH_NULL
);
479 *authp
= rpc_autherr_badcred
;
484 svcauth_unix_release(struct svc_rqst
*rqstp
)
486 /* Verifier (such as it is) is already in place.
488 if (rqstp
->rq_client
)
489 auth_domain_put(rqstp
->rq_client
);
490 rqstp
->rq_client
= NULL
;
491 if (rqstp
->rq_cred
.cr_group_info
)
492 put_group_info(rqstp
->rq_cred
.cr_group_info
);
493 rqstp
->rq_cred
.cr_group_info
= NULL
;
499 struct auth_ops svcauth_unix
= {
501 .owner
= THIS_MODULE
,
502 .flavour
= RPC_AUTH_UNIX
,
503 .accept
= svcauth_unix_accept
,
504 .release
= svcauth_unix_release
,
505 .domain_release
= svcauth_unix_domain_release
,
506 .set_client
= svcauth_unix_set_client
,