nfsd4: fix share mode permissions
[sfrench/cifs-2.6.git] / fs / nfsd / nfs4idmap.c
1 /*
2  *  fs/nfsd/nfs4idmap.c
3  *
4  *  Mapping of UID/GIDs to name and vice versa.
5  *
6  *  Copyright (c) 2002, 2003 The Regents of the University of
7  *  Michigan.  All rights reserved.
8  *
9  *  Marius Aamodt Eriksen <marius@umich.edu>
10  *
11  *  Redistribution and use in source and binary forms, with or without
12  *  modification, are permitted provided that the following conditions
13  *  are met:
14  *
15  *  1. Redistributions of source code must retain the above copyright
16  *     notice, this list of conditions and the following disclaimer.
17  *  2. Redistributions in binary form must reproduce the above copyright
18  *     notice, this list of conditions and the following disclaimer in the
19  *     documentation and/or other materials provided with the distribution.
20  *  3. Neither the name of the University nor the names of its
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include <linux/module.h>
38 #include <linux/init.h>
39
40 #include <linux/mm.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/sunrpc/cache.h>
49 #include <linux/nfsd_idmap.h>
50 #include <linux/list.h>
51 #include <linux/time.h>
52 #include <linux/seq_file.h>
53 #include <linux/sunrpc/svcauth.h>
54
55 /*
56  * Cache entry
57  */
58
59 /*
60  * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
61  * that.
62  */
63
64 #define IDMAP_TYPE_USER  0
65 #define IDMAP_TYPE_GROUP 1
66
67 struct ent {
68         struct cache_head h;
69         int               type;                /* User / Group */
70         uid_t             id;
71         char              name[IDMAP_NAMESZ];
72         char              authname[IDMAP_NAMESZ];
73 };
74
75 /* Common entry handling */
76
77 #define ENT_HASHBITS          8
78 #define ENT_HASHMAX           (1 << ENT_HASHBITS)
79 #define ENT_HASHMASK          (ENT_HASHMAX - 1)
80
81 static void
82 ent_init(struct cache_head *cnew, struct cache_head *citm)
83 {
84         struct ent *new = container_of(cnew, struct ent, h);
85         struct ent *itm = container_of(citm, struct ent, h);
86
87         new->id = itm->id;
88         new->type = itm->type;
89
90         strlcpy(new->name, itm->name, sizeof(new->name));
91         strlcpy(new->authname, itm->authname, sizeof(new->name));
92 }
93
94 static void
95 ent_put(struct kref *ref)
96 {
97         struct ent *map = container_of(ref, struct ent, h.ref);
98         kfree(map);
99 }
100
101 static struct cache_head *
102 ent_alloc(void)
103 {
104         struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
105         if (e)
106                 return &e->h;
107         else
108                 return NULL;
109 }
110
111 /*
112  * ID -> Name cache
113  */
114
115 static struct cache_head *idtoname_table[ENT_HASHMAX];
116
117 static uint32_t
118 idtoname_hash(struct ent *ent)
119 {
120         uint32_t hash;
121
122         hash = hash_str(ent->authname, ENT_HASHBITS);
123         hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
124
125         /* Flip LSB for user/group */
126         if (ent->type == IDMAP_TYPE_GROUP)
127                 hash ^= 1;
128
129         return hash;
130 }
131
132 static void
133 idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
134     int *blen)
135 {
136         struct ent *ent = container_of(ch, struct ent, h);
137         char idstr[11];
138
139         qword_add(bpp, blen, ent->authname);
140         snprintf(idstr, sizeof(idstr), "%u", ent->id);
141         qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
142         qword_add(bpp, blen, idstr);
143
144         (*bpp)[-1] = '\n';
145 }
146
147 static int
148 idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
149 {
150         return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
151 }
152
153 static int
154 idtoname_match(struct cache_head *ca, struct cache_head *cb)
155 {
156         struct ent *a = container_of(ca, struct ent, h);
157         struct ent *b = container_of(cb, struct ent, h);
158
159         return (a->id == b->id && a->type == b->type &&
160             strcmp(a->authname, b->authname) == 0);
161 }
162
163 static int
164 idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
165 {
166         struct ent *ent;
167
168         if (h == NULL) {
169                 seq_puts(m, "#domain type id [name]\n");
170                 return 0;
171         }
172         ent = container_of(h, struct ent, h);
173         seq_printf(m, "%s %s %u", ent->authname,
174                         ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
175                         ent->id);
176         if (test_bit(CACHE_VALID, &h->flags))
177                 seq_printf(m, " %s", ent->name);
178         seq_printf(m, "\n");
179         return 0;
180 }
181
182 static void
183 warn_no_idmapd(struct cache_detail *detail, int has_died)
184 {
185         printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
186                         has_died ? "died" : "not been started");
187 }
188
189
190 static int         idtoname_parse(struct cache_detail *, char *, int);
191 static struct ent *idtoname_lookup(struct ent *);
192 static struct ent *idtoname_update(struct ent *, struct ent *);
193
194 static struct cache_detail idtoname_cache = {
195         .owner          = THIS_MODULE,
196         .hash_size      = ENT_HASHMAX,
197         .hash_table     = idtoname_table,
198         .name           = "nfs4.idtoname",
199         .cache_put      = ent_put,
200         .cache_upcall   = idtoname_upcall,
201         .cache_parse    = idtoname_parse,
202         .cache_show     = idtoname_show,
203         .warn_no_listener = warn_no_idmapd,
204         .match          = idtoname_match,
205         .init           = ent_init,
206         .update         = ent_init,
207         .alloc          = ent_alloc,
208 };
209
210 static int
211 idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
212 {
213         struct ent ent, *res;
214         char *buf1, *bp;
215         int len;
216         int error = -EINVAL;
217
218         if (buf[buflen - 1] != '\n')
219                 return (-EINVAL);
220         buf[buflen - 1]= '\0';
221
222         buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
223         if (buf1 == NULL)
224                 return (-ENOMEM);
225
226         memset(&ent, 0, sizeof(ent));
227
228         /* Authentication name */
229         if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
230                 goto out;
231         memcpy(ent.authname, buf1, sizeof(ent.authname));
232
233         /* Type */
234         if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
235                 goto out;
236         ent.type = strcmp(buf1, "user") == 0 ?
237                 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
238
239         /* ID */
240         if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
241                 goto out;
242         ent.id = simple_strtoul(buf1, &bp, 10);
243         if (bp == buf1)
244                 goto out;
245
246         /* expiry */
247         ent.h.expiry_time = get_expiry(&buf);
248         if (ent.h.expiry_time == 0)
249                 goto out;
250
251         error = -ENOMEM;
252         res = idtoname_lookup(&ent);
253         if (!res)
254                 goto out;
255
256         /* Name */
257         error = -EINVAL;
258         len = qword_get(&buf, buf1, PAGE_SIZE);
259         if (len < 0)
260                 goto out;
261         if (len == 0)
262                 set_bit(CACHE_NEGATIVE, &ent.h.flags);
263         else if (len >= IDMAP_NAMESZ)
264                 goto out;
265         else
266                 memcpy(ent.name, buf1, sizeof(ent.name));
267         error = -ENOMEM;
268         res = idtoname_update(&ent, res);
269         if (res == NULL)
270                 goto out;
271
272         cache_put(&res->h, &idtoname_cache);
273
274         error = 0;
275 out:
276         kfree(buf1);
277
278         return error;
279 }
280
281
282 static struct ent *
283 idtoname_lookup(struct ent *item)
284 {
285         struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
286                                                     &item->h,
287                                                     idtoname_hash(item));
288         if (ch)
289                 return container_of(ch, struct ent, h);
290         else
291                 return NULL;
292 }
293
294 static struct ent *
295 idtoname_update(struct ent *new, struct ent *old)
296 {
297         struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
298                                                     &new->h, &old->h,
299                                                     idtoname_hash(new));
300         if (ch)
301                 return container_of(ch, struct ent, h);
302         else
303                 return NULL;
304 }
305
306
307 /*
308  * Name -> ID cache
309  */
310
311 static struct cache_head *nametoid_table[ENT_HASHMAX];
312
313 static inline int
314 nametoid_hash(struct ent *ent)
315 {
316         return hash_str(ent->name, ENT_HASHBITS);
317 }
318
319 static void
320 nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
321     int *blen)
322 {
323         struct ent *ent = container_of(ch, struct ent, h);
324
325         qword_add(bpp, blen, ent->authname);
326         qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
327         qword_add(bpp, blen, ent->name);
328
329         (*bpp)[-1] = '\n';
330 }
331
332 static int
333 nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
334 {
335         return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
336 }
337
338 static int
339 nametoid_match(struct cache_head *ca, struct cache_head *cb)
340 {
341         struct ent *a = container_of(ca, struct ent, h);
342         struct ent *b = container_of(cb, struct ent, h);
343
344         return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
345             strcmp(a->authname, b->authname) == 0);
346 }
347
348 static int
349 nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
350 {
351         struct ent *ent;
352
353         if (h == NULL) {
354                 seq_puts(m, "#domain type name [id]\n");
355                 return 0;
356         }
357         ent = container_of(h, struct ent, h);
358         seq_printf(m, "%s %s %s", ent->authname,
359                         ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
360                         ent->name);
361         if (test_bit(CACHE_VALID, &h->flags))
362                 seq_printf(m, " %u", ent->id);
363         seq_printf(m, "\n");
364         return 0;
365 }
366
367 static struct ent *nametoid_lookup(struct ent *);
368 static struct ent *nametoid_update(struct ent *, struct ent *);
369 static int         nametoid_parse(struct cache_detail *, char *, int);
370
371 static struct cache_detail nametoid_cache = {
372         .owner          = THIS_MODULE,
373         .hash_size      = ENT_HASHMAX,
374         .hash_table     = nametoid_table,
375         .name           = "nfs4.nametoid",
376         .cache_put      = ent_put,
377         .cache_upcall   = nametoid_upcall,
378         .cache_parse    = nametoid_parse,
379         .cache_show     = nametoid_show,
380         .warn_no_listener = warn_no_idmapd,
381         .match          = nametoid_match,
382         .init           = ent_init,
383         .update         = ent_init,
384         .alloc          = ent_alloc,
385 };
386
387 static int
388 nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
389 {
390         struct ent ent, *res;
391         char *buf1;
392         int error = -EINVAL;
393
394         if (buf[buflen - 1] != '\n')
395                 return (-EINVAL);
396         buf[buflen - 1]= '\0';
397
398         buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
399         if (buf1 == NULL)
400                 return (-ENOMEM);
401
402         memset(&ent, 0, sizeof(ent));
403
404         /* Authentication name */
405         if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
406                 goto out;
407         memcpy(ent.authname, buf1, sizeof(ent.authname));
408
409         /* Type */
410         if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
411                 goto out;
412         ent.type = strcmp(buf1, "user") == 0 ?
413                 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
414
415         /* Name */
416         error = qword_get(&buf, buf1, PAGE_SIZE);
417         if (error <= 0 || error >= IDMAP_NAMESZ)
418                 goto out;
419         memcpy(ent.name, buf1, sizeof(ent.name));
420
421         /* expiry */
422         ent.h.expiry_time = get_expiry(&buf);
423         if (ent.h.expiry_time == 0)
424                 goto out;
425
426         /* ID */
427         error = get_int(&buf, &ent.id);
428         if (error == -EINVAL)
429                 goto out;
430         if (error == -ENOENT)
431                 set_bit(CACHE_NEGATIVE, &ent.h.flags);
432
433         error = -ENOMEM;
434         res = nametoid_lookup(&ent);
435         if (res == NULL)
436                 goto out;
437         res = nametoid_update(&ent, res);
438         if (res == NULL)
439                 goto out;
440
441         cache_put(&res->h, &nametoid_cache);
442         error = 0;
443 out:
444         kfree(buf1);
445
446         return (error);
447 }
448
449
450 static struct ent *
451 nametoid_lookup(struct ent *item)
452 {
453         struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
454                                                     &item->h,
455                                                     nametoid_hash(item));
456         if (ch)
457                 return container_of(ch, struct ent, h);
458         else
459                 return NULL;
460 }
461
462 static struct ent *
463 nametoid_update(struct ent *new, struct ent *old)
464 {
465         struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
466                                                     &new->h, &old->h,
467                                                     nametoid_hash(new));
468         if (ch)
469                 return container_of(ch, struct ent, h);
470         else
471                 return NULL;
472 }
473
474 /*
475  * Exported API
476  */
477
478 int
479 nfsd_idmap_init(void)
480 {
481         int rv;
482
483         rv = cache_register(&idtoname_cache);
484         if (rv)
485                 return rv;
486         rv = cache_register(&nametoid_cache);
487         if (rv)
488                 cache_unregister(&idtoname_cache);
489         return rv;
490 }
491
492 void
493 nfsd_idmap_shutdown(void)
494 {
495         cache_unregister(&idtoname_cache);
496         cache_unregister(&nametoid_cache);
497 }
498
499 /*
500  * Deferred request handling
501  */
502
503 struct idmap_defer_req {
504        struct cache_req         req;
505        struct cache_deferred_req deferred_req;
506        wait_queue_head_t        waitq;
507        atomic_t                 count;
508 };
509
510 static inline void
511 put_mdr(struct idmap_defer_req *mdr)
512 {
513         if (atomic_dec_and_test(&mdr->count))
514                 kfree(mdr);
515 }
516
517 static inline void
518 get_mdr(struct idmap_defer_req *mdr)
519 {
520         atomic_inc(&mdr->count);
521 }
522
523 static void
524 idmap_revisit(struct cache_deferred_req *dreq, int toomany)
525 {
526         struct idmap_defer_req *mdr =
527                 container_of(dreq, struct idmap_defer_req, deferred_req);
528
529         wake_up(&mdr->waitq);
530         put_mdr(mdr);
531 }
532
533 static struct cache_deferred_req *
534 idmap_defer(struct cache_req *req)
535 {
536         struct idmap_defer_req *mdr =
537                 container_of(req, struct idmap_defer_req, req);
538
539         mdr->deferred_req.revisit = idmap_revisit;
540         get_mdr(mdr);
541         return (&mdr->deferred_req);
542 }
543
544 static inline int
545 do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key,
546                 struct cache_detail *detail, struct ent **item,
547                 struct idmap_defer_req *mdr)
548 {
549         *item = lookup_fn(key);
550         if (!*item)
551                 return -ENOMEM;
552         return cache_check(detail, &(*item)->h, &mdr->req);
553 }
554
555 static inline int
556 do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *),
557                         struct ent *key, struct cache_detail *detail,
558                         struct ent **item)
559 {
560         int ret = -ENOMEM;
561
562         *item = lookup_fn(key);
563         if (!*item)
564                 goto out_err;
565         ret = -ETIMEDOUT;
566         if (!test_bit(CACHE_VALID, &(*item)->h.flags)
567                         || (*item)->h.expiry_time < get_seconds()
568                         || detail->flush_time > (*item)->h.last_refresh)
569                 goto out_put;
570         ret = -ENOENT;
571         if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
572                 goto out_put;
573         return 0;
574 out_put:
575         cache_put(&(*item)->h, detail);
576 out_err:
577         *item = NULL;
578         return ret;
579 }
580
581 static int
582 idmap_lookup(struct svc_rqst *rqstp,
583                 struct ent *(*lookup_fn)(struct ent *), struct ent *key,
584                 struct cache_detail *detail, struct ent **item)
585 {
586         struct idmap_defer_req *mdr;
587         int ret;
588
589         mdr = kzalloc(sizeof(*mdr), GFP_KERNEL);
590         if (!mdr)
591                 return -ENOMEM;
592         atomic_set(&mdr->count, 1);
593         init_waitqueue_head(&mdr->waitq);
594         mdr->req.defer = idmap_defer;
595         ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
596         if (ret == -EAGAIN) {
597                 wait_event_interruptible_timeout(mdr->waitq,
598                         test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
599                 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
600         }
601         put_mdr(mdr);
602         return ret;
603 }
604
605 static char *
606 rqst_authname(struct svc_rqst *rqstp)
607 {
608         struct auth_domain *clp;
609
610         clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
611         return clp->name;
612 }
613
614 static int
615 idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
616                 uid_t *id)
617 {
618         struct ent *item, key = {
619                 .type = type,
620         };
621         int ret;
622
623         if (namelen + 1 > sizeof(key.name))
624                 return -EINVAL;
625         memcpy(key.name, name, namelen);
626         key.name[namelen] = '\0';
627         strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
628         ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
629         if (ret == -ENOENT)
630                 ret = -ESRCH; /* nfserr_badname */
631         if (ret)
632                 return ret;
633         *id = item->id;
634         cache_put(&item->h, &nametoid_cache);
635         return 0;
636 }
637
638 static int
639 idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
640 {
641         struct ent *item, key = {
642                 .id = id,
643                 .type = type,
644         };
645         int ret;
646
647         strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
648         ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
649         if (ret == -ENOENT)
650                 return sprintf(name, "%u", id);
651         if (ret)
652                 return ret;
653         ret = strlen(item->name);
654         BUG_ON(ret > IDMAP_NAMESZ);
655         memcpy(name, item->name, ret);
656         cache_put(&item->h, &idtoname_cache);
657         return ret;
658 }
659
660 int
661 nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
662                 __u32 *id)
663 {
664         return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
665 }
666
667 int
668 nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
669                 __u32 *id)
670 {
671         return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
672 }
673
674 int
675 nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
676 {
677         return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
678 }
679
680 int
681 nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
682 {
683         return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
684 }