ACPI: thinkpad-acpi: add subdriver debug statements
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / svcauth_unix.c
bloba568f9f541b9ea6826d72e1355904bebac02761f
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/err.h>
9 #include <linux/seq_file.h>
10 #include <linux/hash.h>
11 #include <linux/string.h>
12 #include <net/sock.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.
25 struct unix_domain {
26 struct auth_domain h;
27 int addr_changes;
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);
39 while(1) {
40 if (rv) {
41 if (new && rv != &new->h)
42 auth_domain_put(&new->h);
44 if (rv->flavour != &svcauth_unix) {
45 auth_domain_put(rv);
46 return NULL;
48 return rv;
51 new = kmalloc(sizeof(*new), GFP_KERNEL);
52 if (new == NULL)
53 return NULL;
54 kref_init(&new->h.ref);
55 new->h.name = kstrdup(name, GFP_KERNEL);
56 if (new->h.name == NULL) {
57 kfree(new);
58 return 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);
70 kfree(dom->name);
71 kfree(ud);
75 /**************************************************
76 * cache for IP address to unix_domain
77 * as needed by AUTH_UNIX
79 #define IP_HASHBITS 8
80 #define IP_HASHMAX (1<<IP_HASHBITS)
81 #define IP_HASHMASK (IP_HASHMAX-1)
83 struct ip_map {
84 struct cache_head h;
85 char m_class[8]; /* e.g. "nfsd" */
86 struct in_addr m_addr;
87 struct unix_domain *m_client;
88 int m_add_change;
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);
100 kfree(im);
103 #if IP_HASHBITS == 8
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;
113 #endif
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);
141 if (i)
142 return &i->h;
143 else
144 return NULL;
147 static void ip_map_request(struct cache_detail *cd,
148 struct cache_head *h,
149 char **bpp, int *blen)
151 char text_addr[20];
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);
163 (*bpp)[-1] = '\n';
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
174 * for scratch: */
175 char *buf = mesg;
176 int len;
177 int b1,b2,b3,b4;
178 char c;
179 char class[8];
180 struct in_addr addr;
181 int err;
183 struct ip_map *ipmp;
184 struct auth_domain *dom;
185 time_t expiry;
187 if (mesg[mlen-1] != '\n')
188 return -EINVAL;
189 mesg[mlen-1] = 0;
191 /* class */
192 len = qword_get(&mesg, class, sizeof(class));
193 if (len <= 0) return -EINVAL;
195 /* ip address */
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)
200 return -EINVAL;
202 expiry = get_expiry(&mesg);
203 if (expiry ==0)
204 return -EINVAL;
206 /* domainname, or empty for NEGATIVE */
207 len = qword_get(&mesg, buf, mlen);
208 if (len < 0) return -EINVAL;
210 if (len) {
211 dom = unix_domain_find(buf);
212 if (dom == NULL)
213 return -ENOENT;
214 } else
215 dom = NULL;
217 addr.s_addr =
218 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
220 ipmp = ip_map_lookup(class,addr);
221 if (ipmp) {
222 err = ip_map_update(ipmp,
223 container_of(dom, struct unix_domain, h),
224 expiry);
225 } else
226 err = -ENOMEM;
228 if (dom)
229 auth_domain_put(dom);
231 cache_flush();
232 return err;
235 static int ip_map_show(struct seq_file *m,
236 struct cache_detail *cd,
237 struct cache_head *h)
239 struct ip_map *im;
240 struct in_addr addr;
241 char *dom = "-no-domain-";
243 if (h == NULL) {
244 seq_puts(m, "#class IP domain\n");
245 return 0;
247 im = container_of(h, struct ip_map, h);
248 /* class addr domain */
249 addr = im->m_addr;
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",
256 im->m_class,
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,
263 return 0;
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,
277 .init = ip_map_init,
278 .update = update,
279 .alloc = ip_map_alloc,
282 static struct ip_map *ip_map_lookup(char *class, struct in_addr addr)
284 struct ip_map ip;
285 struct cache_head *ch;
287 strcpy(ip.m_class, class);
288 ip.m_addr = addr;
289 ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
290 hash_str(class, IP_HASHBITS) ^
291 hash_ip(addr.s_addr));
293 if (ch)
294 return container_of(ch, struct ip_map, h);
295 else
296 return NULL;
299 static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
301 struct ip_map ip;
302 struct cache_head *ch;
304 ip.m_client = udom;
305 ip.h.flags = 0;
306 if (!udom)
307 set_bit(CACHE_NEGATIVE, &ip.h.flags);
308 else {
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
313 if (expiry == NEVER)
314 ip.m_add_change++;
316 ip.h.expiry_time = expiry;
317 ch = sunrpc_cache_update(&ip_map_cache,
318 &ip.h, &ipm->h,
319 hash_str(ipm->m_class, IP_HASHBITS) ^
320 hash_ip(ipm->m_addr.s_addr));
321 if (!ch)
322 return -ENOMEM;
323 cache_put(ch, &ip_map_cache);
324 return 0;
327 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
329 struct unix_domain *udom;
330 struct ip_map *ipmp;
332 if (dom->flavour != &svcauth_unix)
333 return -EINVAL;
334 udom = container_of(dom, struct unix_domain, h);
335 ipmp = ip_map_lookup("nfsd", addr);
337 if (ipmp)
338 return ip_map_update(ipmp, udom, NEVER);
339 else
340 return -ENOMEM;
343 int auth_unix_forget_old(struct auth_domain *dom)
345 struct unix_domain *udom;
347 if (dom->flavour != &svcauth_unix)
348 return -EINVAL;
349 udom = container_of(dom, struct unix_domain, h);
350 udom->addr_changes++;
351 return 0;
354 struct auth_domain *auth_unix_lookup(struct in_addr addr)
356 struct ip_map *ipm;
357 struct auth_domain *rv;
359 ipm = ip_map_lookup("nfsd", addr);
361 if (!ipm)
362 return NULL;
363 if (cache_check(&ip_map_cache, &ipm->h, NULL))
364 return 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);
369 rv = NULL;
370 } else {
371 rv = &ipm->m_client->h;
372 kref_get(&rv->ref);
374 cache_put(&ipm->h, &ip_map_cache);
375 return rv;
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)
386 struct ip_map *ipm;
387 struct svc_sock *svsk = rqstp->rq_sock;
388 spin_lock_bh(&svsk->sk_defer_lock);
389 ipm = svsk->sk_info_authunix;
390 if (ipm != NULL) {
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
395 * same IP address.
397 svsk->sk_info_authunix = NULL;
398 spin_unlock_bh(&svsk->sk_defer_lock);
399 cache_put(&ipm->h, &ip_map_cache);
400 return NULL;
402 cache_get(&ipm->h);
404 spin_unlock_bh(&svsk->sk_defer_lock);
405 return ipm;
408 static inline void
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;
418 ipm = NULL;
420 spin_unlock_bh(&svsk->sk_defer_lock);
421 if (ipm)
422 cache_put(&ipm->h, &ip_map_cache);
425 void
426 svcauth_unix_info_release(void *info)
428 struct ip_map *ipm = info;
429 cache_put(&ipm->h, &ip_map_cache);
432 static int
433 svcauth_unix_set_client(struct svc_rqst *rqstp)
435 struct ip_map *ipm;
437 rqstp->rq_client = NULL;
438 if (rqstp->rq_proc == 0)
439 return SVC_OK;
441 ipm = ip_map_cached_get(rqstp);
442 if (ipm == NULL)
443 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
444 rqstp->rq_addr.sin_addr);
446 if (ipm == NULL)
447 return SVC_DENIED;
449 switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
450 default:
451 BUG();
452 case -EAGAIN:
453 case -ETIMEDOUT:
454 return SVC_DROP;
455 case -ENOENT:
456 return SVC_DENIED;
457 case 0:
458 rqstp->rq_client = &ipm->m_client->h;
459 kref_get(&rqstp->rq_client->ref);
460 ip_map_cached_put(rqstp, ipm);
461 break;
463 return SVC_OK;
466 static int
467 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
469 struct kvec *argv = &rqstp->rq_arg.head[0];
470 struct kvec *resv = &rqstp->rq_res.head[0];
471 struct svc_cred *cred = &rqstp->rq_cred;
473 cred->cr_group_info = NULL;
474 rqstp->rq_client = NULL;
476 if (argv->iov_len < 3*4)
477 return SVC_GARBAGE;
479 if (svc_getu32(argv) != 0) {
480 dprintk("svc: bad null cred\n");
481 *authp = rpc_autherr_badcred;
482 return SVC_DENIED;
484 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
485 dprintk("svc: bad null verf\n");
486 *authp = rpc_autherr_badverf;
487 return SVC_DENIED;
490 /* Signal that mapping to nobody uid/gid is required */
491 cred->cr_uid = (uid_t) -1;
492 cred->cr_gid = (gid_t) -1;
493 cred->cr_group_info = groups_alloc(0);
494 if (cred->cr_group_info == NULL)
495 return SVC_DROP; /* kmalloc failure - client must retry */
497 /* Put NULL verifier */
498 svc_putnl(resv, RPC_AUTH_NULL);
499 svc_putnl(resv, 0);
501 return SVC_OK;
504 static int
505 svcauth_null_release(struct svc_rqst *rqstp)
507 if (rqstp->rq_client)
508 auth_domain_put(rqstp->rq_client);
509 rqstp->rq_client = NULL;
510 if (rqstp->rq_cred.cr_group_info)
511 put_group_info(rqstp->rq_cred.cr_group_info);
512 rqstp->rq_cred.cr_group_info = NULL;
514 return 0; /* don't drop */
518 struct auth_ops svcauth_null = {
519 .name = "null",
520 .owner = THIS_MODULE,
521 .flavour = RPC_AUTH_NULL,
522 .accept = svcauth_null_accept,
523 .release = svcauth_null_release,
524 .set_client = svcauth_unix_set_client,
528 static int
529 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
531 struct kvec *argv = &rqstp->rq_arg.head[0];
532 struct kvec *resv = &rqstp->rq_res.head[0];
533 struct svc_cred *cred = &rqstp->rq_cred;
534 u32 slen, i;
535 int len = argv->iov_len;
537 cred->cr_group_info = NULL;
538 rqstp->rq_client = NULL;
540 if ((len -= 3*4) < 0)
541 return SVC_GARBAGE;
543 svc_getu32(argv); /* length */
544 svc_getu32(argv); /* time stamp */
545 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
546 if (slen > 64 || (len -= (slen + 3)*4) < 0)
547 goto badcred;
548 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
549 argv->iov_len -= slen*4;
551 cred->cr_uid = svc_getnl(argv); /* uid */
552 cred->cr_gid = svc_getnl(argv); /* gid */
553 slen = svc_getnl(argv); /* gids length */
554 if (slen > 16 || (len -= (slen + 2)*4) < 0)
555 goto badcred;
556 cred->cr_group_info = groups_alloc(slen);
557 if (cred->cr_group_info == NULL)
558 return SVC_DROP;
559 for (i = 0; i < slen; i++)
560 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
562 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
563 *authp = rpc_autherr_badverf;
564 return SVC_DENIED;
567 /* Put NULL verifier */
568 svc_putnl(resv, RPC_AUTH_NULL);
569 svc_putnl(resv, 0);
571 return SVC_OK;
573 badcred:
574 *authp = rpc_autherr_badcred;
575 return SVC_DENIED;
578 static int
579 svcauth_unix_release(struct svc_rqst *rqstp)
581 /* Verifier (such as it is) is already in place.
583 if (rqstp->rq_client)
584 auth_domain_put(rqstp->rq_client);
585 rqstp->rq_client = NULL;
586 if (rqstp->rq_cred.cr_group_info)
587 put_group_info(rqstp->rq_cred.cr_group_info);
588 rqstp->rq_cred.cr_group_info = NULL;
590 return 0;
594 struct auth_ops svcauth_unix = {
595 .name = "unix",
596 .owner = THIS_MODULE,
597 .flavour = RPC_AUTH_UNIX,
598 .accept = svcauth_unix_accept,
599 .release = svcauth_unix_release,
600 .domain_release = svcauth_unix_domain_release,
601 .set_client = svcauth_unix_set_client,