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 new->h.flavour = &svcauth_unix;
57 new->addr_changes = 0;
58 rv = auth_domain_lookup(name, &new->h);
62 static void svcauth_unix_domain_release(struct auth_domain *dom)
64 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
71 /**************************************************
72 * cache for IP address to unix_domain
73 * as needed by AUTH_UNIX
76 #define IP_HASHMAX (1<<IP_HASHBITS)
77 #define IP_HASHMASK (IP_HASHMAX-1)
81 char m_class[8]; /* e.g. "nfsd" */
82 struct in_addr m_addr;
83 struct unix_domain *m_client;
86 static struct cache_head *ip_table[IP_HASHMAX];
88 static void ip_map_put(struct kref *kref)
90 struct cache_head *item = container_of(kref, struct cache_head, ref);
91 struct ip_map *im = container_of(item, struct ip_map,h);
93 if (test_bit(CACHE_VALID, &item->flags) &&
94 !test_bit(CACHE_NEGATIVE, &item->flags))
95 auth_domain_put(&im->m_client->h);
100 /* hash_long on a 64 bit machine is currently REALLY BAD for
101 * IP addresses in reverse-endian (i.e. on a little-endian machine).
102 * So use a trivial but reliable hash instead
104 static inline int hash_ip(__be32 ip)
106 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
107 return (hash ^ (hash>>8)) & 0xff;
110 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
112 struct ip_map *orig = container_of(corig, struct ip_map, h);
113 struct ip_map *new = container_of(cnew, struct ip_map, h);
114 return strcmp(orig->m_class, new->m_class) == 0
115 && orig->m_addr.s_addr == new->m_addr.s_addr;
117 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
119 struct ip_map *new = container_of(cnew, struct ip_map, h);
120 struct ip_map *item = container_of(citem, struct ip_map, h);
122 strcpy(new->m_class, item->m_class);
123 new->m_addr.s_addr = item->m_addr.s_addr;
125 static void update(struct cache_head *cnew, struct cache_head *citem)
127 struct ip_map *new = container_of(cnew, struct ip_map, h);
128 struct ip_map *item = container_of(citem, struct ip_map, h);
130 kref_get(&item->m_client->h.ref);
131 new->m_client = item->m_client;
132 new->m_add_change = item->m_add_change;
134 static struct cache_head *ip_map_alloc(void)
136 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
143 static void ip_map_request(struct cache_detail *cd,
144 struct cache_head *h,
145 char **bpp, int *blen)
148 struct ip_map *im = container_of(h, struct ip_map, h);
149 __be32 addr = im->m_addr.s_addr;
151 snprintf(text_addr, 20, "%u.%u.%u.%u",
152 ntohl(addr) >> 24 & 0xff,
153 ntohl(addr) >> 16 & 0xff,
154 ntohl(addr) >> 8 & 0xff,
155 ntohl(addr) >> 0 & 0xff);
157 qword_add(bpp, blen, im->m_class);
158 qword_add(bpp, blen, text_addr);
162 static struct ip_map *ip_map_lookup(char *class, struct in_addr addr);
163 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
165 static int ip_map_parse(struct cache_detail *cd,
166 char *mesg, int mlen)
168 /* class ipaddress [domainname] */
169 /* should be safe just to use the start of the input buffer
180 struct auth_domain *dom;
183 if (mesg[mlen-1] != '\n')
188 len = qword_get(&mesg, class, sizeof(class));
189 if (len <= 0) return -EINVAL;
192 len = qword_get(&mesg, buf, mlen);
193 if (len <= 0) return -EINVAL;
195 if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
198 expiry = get_expiry(&mesg);
202 /* domainname, or empty for NEGATIVE */
203 len = qword_get(&mesg, buf, mlen);
204 if (len < 0) return -EINVAL;
207 dom = unix_domain_find(buf);
214 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
216 ipmp = ip_map_lookup(class,addr);
218 err = ip_map_update(ipmp,
219 container_of(dom, struct unix_domain, h),
225 auth_domain_put(dom);
231 static int ip_map_show(struct seq_file *m,
232 struct cache_detail *cd,
233 struct cache_head *h)
237 char *dom = "-no-domain-";
240 seq_puts(m, "#class IP domain\n");
243 im = container_of(h, struct ip_map, h);
244 /* class addr domain */
247 if (test_bit(CACHE_VALID, &h->flags) &&
248 !test_bit(CACHE_NEGATIVE, &h->flags))
249 dom = im->m_client->h.name;
251 seq_printf(m, "%s %d.%d.%d.%d %s\n",
253 ntohl(addr.s_addr) >> 24 & 0xff,
254 ntohl(addr.s_addr) >> 16 & 0xff,
255 ntohl(addr.s_addr) >> 8 & 0xff,
256 ntohl(addr.s_addr) >> 0 & 0xff,
263 struct cache_detail ip_map_cache = {
264 .owner = THIS_MODULE,
265 .hash_size = IP_HASHMAX,
266 .hash_table = ip_table,
267 .name = "auth.unix.ip",
268 .cache_put = ip_map_put,
269 .cache_request = ip_map_request,
270 .cache_parse = ip_map_parse,
271 .cache_show = ip_map_show,
272 .match = ip_map_match,
275 .alloc = ip_map_alloc,
278 static struct ip_map *ip_map_lookup(char *class, struct in_addr addr)
281 struct cache_head *ch;
283 strcpy(ip.m_class, class);
285 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
286 hash_str(class, IP_HASHBITS) ^
287 hash_ip(addr.s_addr));
290 return container_of(ch, struct ip_map, h);
295 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
298 struct cache_head *ch;
303 set_bit(CACHE_NEGATIVE, &ip.h.flags);
305 ip.m_add_change = udom->addr_changes;
306 /* if this is from the legacy set_client system call,
307 * we need m_add_change to be one higher
312 ip.h.expiry_time = expiry;
313 ch = sunrpc_cache_update(&ip_map_cache,
315 hash_str(ipm->m_class, IP_HASHBITS) ^
316 hash_ip(ipm->m_addr.s_addr));
319 cache_put(ch, &ip_map_cache);
323 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
325 struct unix_domain *udom;
328 if (dom->flavour != &svcauth_unix)
330 udom = container_of(dom, struct unix_domain, h);
331 ipmp = ip_map_lookup("nfsd", addr);
334 return ip_map_update(ipmp, udom, NEVER);
339 int auth_unix_forget_old(struct auth_domain *dom)
341 struct unix_domain *udom;
343 if (dom->flavour != &svcauth_unix)
345 udom = container_of(dom, struct unix_domain, h);
346 udom->addr_changes++;
350 struct auth_domain *auth_unix_lookup(struct in_addr addr)
353 struct auth_domain *rv;
355 ipm = ip_map_lookup("nfsd", addr);
359 if (cache_check(&ip_map_cache, &ipm->h, NULL))
362 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
363 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
364 auth_domain_put(&ipm->m_client->h);
367 rv = &ipm->m_client->h;
370 cache_put(&ipm->h, &ip_map_cache);
374 void svcauth_unix_purge(void)
376 cache_purge(&ip_map_cache);
379 static inline struct ip_map *
380 ip_map_cached_get(struct svc_rqst *rqstp)
382 struct ip_map *ipm = rqstp->rq_sock->sk_info_authunix;
384 if (!cache_valid(&ipm->h)) {
386 * The entry has been invalidated since it was
387 * remembered, e.g. by a second mount from the
390 rqstp->rq_sock->sk_info_authunix = NULL;
391 cache_put(&ipm->h, &ip_map_cache);
400 ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
402 struct svc_sock *svsk = rqstp->rq_sock;
404 if (svsk->sk_sock->type == SOCK_STREAM && svsk->sk_info_authunix == NULL)
405 svsk->sk_info_authunix = ipm; /* newly cached, keep the reference */
407 cache_put(&ipm->h, &ip_map_cache);
411 svcauth_unix_info_release(void *info)
413 struct ip_map *ipm = info;
414 cache_put(&ipm->h, &ip_map_cache);
418 svcauth_unix_set_client(struct svc_rqst *rqstp)
422 rqstp->rq_client = NULL;
423 if (rqstp->rq_proc == 0)
426 ipm = ip_map_cached_get(rqstp);
428 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
429 rqstp->rq_addr.sin_addr);
434 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
442 rqstp->rq_client = &ipm->m_client->h;
443 kref_get(&rqstp->rq_client->ref);
444 ip_map_cached_put(rqstp, ipm);
451 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
453 struct kvec *argv = &rqstp->rq_arg.head[0];
454 struct kvec *resv = &rqstp->rq_res.head[0];
455 struct svc_cred *cred = &rqstp->rq_cred;
457 cred->cr_group_info = NULL;
458 rqstp->rq_client = NULL;
460 if (argv->iov_len < 3*4)
463 if (svc_getu32(argv) != 0) {
464 dprintk("svc: bad null cred\n");
465 *authp = rpc_autherr_badcred;
468 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
469 dprintk("svc: bad null verf\n");
470 *authp = rpc_autherr_badverf;
474 /* Signal that mapping to nobody uid/gid is required */
475 cred->cr_uid = (uid_t) -1;
476 cred->cr_gid = (gid_t) -1;
477 cred->cr_group_info = groups_alloc(0);
478 if (cred->cr_group_info == NULL)
479 return SVC_DROP; /* kmalloc failure - client must retry */
481 /* Put NULL verifier */
482 svc_putnl(resv, RPC_AUTH_NULL);
489 svcauth_null_release(struct svc_rqst *rqstp)
491 if (rqstp->rq_client)
492 auth_domain_put(rqstp->rq_client);
493 rqstp->rq_client = NULL;
494 if (rqstp->rq_cred.cr_group_info)
495 put_group_info(rqstp->rq_cred.cr_group_info);
496 rqstp->rq_cred.cr_group_info = NULL;
498 return 0; /* don't drop */
502 struct auth_ops svcauth_null = {
504 .owner = THIS_MODULE,
505 .flavour = RPC_AUTH_NULL,
506 .accept = svcauth_null_accept,
507 .release = svcauth_null_release,
508 .set_client = svcauth_unix_set_client,
513 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
515 struct kvec *argv = &rqstp->rq_arg.head[0];
516 struct kvec *resv = &rqstp->rq_res.head[0];
517 struct svc_cred *cred = &rqstp->rq_cred;
519 int len = argv->iov_len;
521 cred->cr_group_info = NULL;
522 rqstp->rq_client = NULL;
524 if ((len -= 3*4) < 0)
527 svc_getu32(argv); /* length */
528 svc_getu32(argv); /* time stamp */
529 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
530 if (slen > 64 || (len -= (slen + 3)*4) < 0)
532 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
533 argv->iov_len -= slen*4;
535 cred->cr_uid = svc_getnl(argv); /* uid */
536 cred->cr_gid = svc_getnl(argv); /* gid */
537 slen = svc_getnl(argv); /* gids length */
538 if (slen > 16 || (len -= (slen + 2)*4) < 0)
540 cred->cr_group_info = groups_alloc(slen);
541 if (cred->cr_group_info == NULL)
543 for (i = 0; i < slen; i++)
544 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
546 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
547 *authp = rpc_autherr_badverf;
551 /* Put NULL verifier */
552 svc_putnl(resv, RPC_AUTH_NULL);
558 *authp = rpc_autherr_badcred;
563 svcauth_unix_release(struct svc_rqst *rqstp)
565 /* Verifier (such as it is) is already in place.
567 if (rqstp->rq_client)
568 auth_domain_put(rqstp->rq_client);
569 rqstp->rq_client = NULL;
570 if (rqstp->rq_cred.cr_group_info)
571 put_group_info(rqstp->rq_cred.cr_group_info);
572 rqstp->rq_cred.cr_group_info = NULL;
578 struct auth_ops svcauth_unix = {
580 .owner = THIS_MODULE,
581 .flavour = RPC_AUTH_UNIX,
582 .accept = svcauth_unix_accept,
583 .release = svcauth_unix_release,
584 .domain_release = svcauth_unix_domain_release,
585 .set_client = svcauth_unix_set_client,