blob: dea690aa8bb5f5651b856ffb00e15fa8fbfd1e33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/config.h>
38#include <linux/module.h>
39#include <linux/init.h>
40
41#include <linux/mm.h>
42#include <linux/utsname.h>
43#include <linux/errno.h>
44#include <linux/string.h>
45#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
50#include <linux/smp_lock.h>
51#include <linux/sunrpc/cache.h>
52#include <linux/nfsd_idmap.h>
53#include <linux/list.h>
54#include <linux/sched.h>
55#include <linux/time.h>
56#include <linux/seq_file.h>
57#include <linux/sunrpc/svcauth.h>
58
59/*
60 * Cache entry
61 */
62
63/*
64 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
65 * that.
66 */
67
68#define IDMAP_TYPE_USER 0
69#define IDMAP_TYPE_GROUP 1
70
71struct ent {
72 struct cache_head h;
73 int type; /* User / Group */
74 uid_t id;
75 char name[IDMAP_NAMESZ];
76 char authname[IDMAP_NAMESZ];
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Common entry handling */
80
81#define ENT_HASHBITS 8
82#define ENT_HASHMAX (1 << ENT_HASHBITS)
83#define ENT_HASHMASK (ENT_HASHMAX - 1)
84
85static inline void
86ent_init(struct ent *new, struct ent *itm)
87{
88 new->id = itm->id;
89 new->type = itm->type;
90
91 strlcpy(new->name, itm->name, sizeof(new->name));
92 strlcpy(new->authname, itm->authname, sizeof(new->name));
93}
94
95static inline void
96ent_update(struct ent *new, struct ent *itm)
97{
98 ent_init(new, itm);
99}
100
NeilBrownfd39ca92005-06-23 22:04:03 -0700101static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102ent_put(struct cache_head *ch, struct cache_detail *cd)
103{
104 if (cache_put(ch, cd)) {
105 struct ent *map = container_of(ch, struct ent, h);
106 kfree(map);
107 }
108}
109
110/*
111 * ID -> Name cache
112 */
113
114static struct cache_head *idtoname_table[ENT_HASHMAX];
115
116static uint32_t
117idtoname_hash(struct ent *ent)
118{
119 uint32_t hash;
120
121 hash = hash_str(ent->authname, ENT_HASHBITS);
122 hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
123
124 /* Flip LSB for user/group */
125 if (ent->type == IDMAP_TYPE_GROUP)
126 hash ^= 1;
127
128 return hash;
129}
130
131static void
132idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
133 int *blen)
134{
135 struct ent *ent = container_of(ch, struct ent, h);
136 char idstr[11];
137
138 qword_add(bpp, blen, ent->authname);
139 snprintf(idstr, sizeof(idstr), "%d", ent->id);
140 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
141 qword_add(bpp, blen, idstr);
142
143 (*bpp)[-1] = '\n';
144}
145
146static inline int
147idtoname_match(struct ent *a, struct ent *b)
148{
149 return (a->id == b->id && a->type == b->type &&
150 strcmp(a->authname, b->authname) == 0);
151}
152
153static int
154idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
155{
156 struct ent *ent;
157
158 if (h == NULL) {
159 seq_puts(m, "#domain type id [name]\n");
160 return 0;
161 }
162 ent = container_of(h, struct ent, h);
163 seq_printf(m, "%s %s %d", ent->authname,
164 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
165 ent->id);
166 if (test_bit(CACHE_VALID, &h->flags))
167 seq_printf(m, " %s", ent->name);
168 seq_printf(m, "\n");
169 return 0;
170}
171
172static void
173warn_no_idmapd(struct cache_detail *detail)
174{
175 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
176 detail->last_close? "died" : "not been started");
177}
178
179
180static int idtoname_parse(struct cache_detail *, char *, int);
181static struct ent *idtoname_lookup(struct ent *, int);
182
NeilBrownfd39ca92005-06-23 22:04:03 -0700183static struct cache_detail idtoname_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700184 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .hash_size = ENT_HASHMAX,
186 .hash_table = idtoname_table,
187 .name = "nfs4.idtoname",
188 .cache_put = ent_put,
189 .cache_request = idtoname_request,
190 .cache_parse = idtoname_parse,
191 .cache_show = idtoname_show,
192 .warn_no_listener = warn_no_idmapd,
193};
194
195int
196idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
197{
198 struct ent ent, *res;
199 char *buf1, *bp;
200 int error = -EINVAL;
201
202 if (buf[buflen - 1] != '\n')
203 return (-EINVAL);
204 buf[buflen - 1]= '\0';
205
206 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
207 if (buf1 == NULL)
208 return (-ENOMEM);
209
210 memset(&ent, 0, sizeof(ent));
211
212 /* Authentication name */
213 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
214 goto out;
215 memcpy(ent.authname, buf1, sizeof(ent.authname));
216
217 /* Type */
218 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
219 goto out;
220 ent.type = strcmp(buf1, "user") == 0 ?
221 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
222
223 /* ID */
224 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
225 goto out;
226 ent.id = simple_strtoul(buf1, &bp, 10);
227 if (bp == buf1)
228 goto out;
229
230 /* expiry */
231 ent.h.expiry_time = get_expiry(&buf);
232 if (ent.h.expiry_time == 0)
233 goto out;
234
235 /* Name */
236 error = qword_get(&buf, buf1, PAGE_SIZE);
237 if (error == -EINVAL)
238 goto out;
239 if (error == -ENOENT)
240 set_bit(CACHE_NEGATIVE, &ent.h.flags);
241 else {
242 if (error >= IDMAP_NAMESZ) {
243 error = -EINVAL;
244 goto out;
245 }
246 memcpy(ent.name, buf1, sizeof(ent.name));
247 }
248 error = -ENOMEM;
249 if ((res = idtoname_lookup(&ent, 1)) == NULL)
250 goto out;
251
252 ent_put(&res->h, &idtoname_cache);
253
254 error = 0;
255out:
256 kfree(buf1);
257
258 return error;
259}
260
NeilBrown7d317f22006-03-27 01:15:01 -0800261static DefineSimpleCacheLookup(ent, idtoname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/*
264 * Name -> ID cache
265 */
266
267static struct cache_head *nametoid_table[ENT_HASHMAX];
268
269static inline int
270nametoid_hash(struct ent *ent)
271{
272 return hash_str(ent->name, ENT_HASHBITS);
273}
274
NeilBrownfd39ca92005-06-23 22:04:03 -0700275static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
277 int *blen)
278{
279 struct ent *ent = container_of(ch, struct ent, h);
280
281 qword_add(bpp, blen, ent->authname);
282 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
283 qword_add(bpp, blen, ent->name);
284
285 (*bpp)[-1] = '\n';
286}
287
288static inline int
289nametoid_match(struct ent *a, struct ent *b)
290{
291 return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
292 strcmp(a->authname, b->authname) == 0);
293}
294
295static int
296nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
297{
298 struct ent *ent;
299
300 if (h == NULL) {
301 seq_puts(m, "#domain type name [id]\n");
302 return 0;
303 }
304 ent = container_of(h, struct ent, h);
305 seq_printf(m, "%s %s %s", ent->authname,
306 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
307 ent->name);
308 if (test_bit(CACHE_VALID, &h->flags))
309 seq_printf(m, " %d", ent->id);
310 seq_printf(m, "\n");
311 return 0;
312}
313
314static struct ent *nametoid_lookup(struct ent *, int);
NeilBrownfd39ca92005-06-23 22:04:03 -0700315static int nametoid_parse(struct cache_detail *, char *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
NeilBrownfd39ca92005-06-23 22:04:03 -0700317static struct cache_detail nametoid_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700318 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .hash_size = ENT_HASHMAX,
320 .hash_table = nametoid_table,
321 .name = "nfs4.nametoid",
322 .cache_put = ent_put,
323 .cache_request = nametoid_request,
324 .cache_parse = nametoid_parse,
325 .cache_show = nametoid_show,
326 .warn_no_listener = warn_no_idmapd,
327};
328
NeilBrownfd39ca92005-06-23 22:04:03 -0700329static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
331{
332 struct ent ent, *res;
333 char *buf1;
334 int error = -EINVAL;
335
336 if (buf[buflen - 1] != '\n')
337 return (-EINVAL);
338 buf[buflen - 1]= '\0';
339
340 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
341 if (buf1 == NULL)
342 return (-ENOMEM);
343
344 memset(&ent, 0, sizeof(ent));
345
346 /* Authentication name */
347 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
348 goto out;
349 memcpy(ent.authname, buf1, sizeof(ent.authname));
350
351 /* Type */
352 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
353 goto out;
354 ent.type = strcmp(buf1, "user") == 0 ?
355 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
356
357 /* Name */
358 error = qword_get(&buf, buf1, PAGE_SIZE);
359 if (error <= 0 || error >= IDMAP_NAMESZ)
360 goto out;
361 memcpy(ent.name, buf1, sizeof(ent.name));
362
363 /* expiry */
364 ent.h.expiry_time = get_expiry(&buf);
365 if (ent.h.expiry_time == 0)
366 goto out;
367
368 /* ID */
369 error = get_int(&buf, &ent.id);
370 if (error == -EINVAL)
371 goto out;
372 if (error == -ENOENT)
373 set_bit(CACHE_NEGATIVE, &ent.h.flags);
374
375 error = -ENOMEM;
376 if ((res = nametoid_lookup(&ent, 1)) == NULL)
377 goto out;
378
379 ent_put(&res->h, &nametoid_cache);
380 error = 0;
381out:
382 kfree(buf1);
383
384 return (error);
385}
386
NeilBrown7d317f22006-03-27 01:15:01 -0800387static DefineSimpleCacheLookup(ent, nametoid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389/*
390 * Exported API
391 */
392
393void
394nfsd_idmap_init(void)
395{
396 cache_register(&idtoname_cache);
397 cache_register(&nametoid_cache);
398}
399
400void
401nfsd_idmap_shutdown(void)
402{
Bruce Allanf35279d2005-09-06 15:17:08 -0700403 if (cache_unregister(&idtoname_cache))
404 printk(KERN_ERR "nfsd: failed to unregister idtoname cache\n");
405 if (cache_unregister(&nametoid_cache))
406 printk(KERN_ERR "nfsd: failed to unregister nametoid cache\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/*
410 * Deferred request handling
411 */
412
413struct idmap_defer_req {
414 struct cache_req req;
415 struct cache_deferred_req deferred_req;
416 wait_queue_head_t waitq;
417 atomic_t count;
418};
419
420static inline void
421put_mdr(struct idmap_defer_req *mdr)
422{
423 if (atomic_dec_and_test(&mdr->count))
424 kfree(mdr);
425}
426
427static inline void
428get_mdr(struct idmap_defer_req *mdr)
429{
430 atomic_inc(&mdr->count);
431}
432
433static void
434idmap_revisit(struct cache_deferred_req *dreq, int toomany)
435{
436 struct idmap_defer_req *mdr =
437 container_of(dreq, struct idmap_defer_req, deferred_req);
438
439 wake_up(&mdr->waitq);
440 put_mdr(mdr);
441}
442
443static struct cache_deferred_req *
444idmap_defer(struct cache_req *req)
445{
446 struct idmap_defer_req *mdr =
447 container_of(req, struct idmap_defer_req, req);
448
449 mdr->deferred_req.revisit = idmap_revisit;
450 get_mdr(mdr);
451 return (&mdr->deferred_req);
452}
453
454static inline int
455do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
456 struct cache_detail *detail, struct ent **item,
457 struct idmap_defer_req *mdr)
458{
459 *item = lookup_fn(key, 0);
460 if (!*item)
461 return -ENOMEM;
462 return cache_check(detail, &(*item)->h, &mdr->req);
463}
464
465static inline int
466do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *, int),
467 struct ent *key, struct cache_detail *detail,
468 struct ent **item)
469{
470 int ret = -ENOMEM;
471
472 *item = lookup_fn(key, 0);
473 if (!*item)
474 goto out_err;
475 ret = -ETIMEDOUT;
476 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
477 || (*item)->h.expiry_time < get_seconds()
478 || detail->flush_time > (*item)->h.last_refresh)
479 goto out_put;
480 ret = -ENOENT;
481 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
482 goto out_put;
483 return 0;
484out_put:
485 ent_put(&(*item)->h, detail);
486out_err:
487 *item = NULL;
488 return ret;
489}
490
491static int
492idmap_lookup(struct svc_rqst *rqstp,
493 struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
494 struct cache_detail *detail, struct ent **item)
495{
496 struct idmap_defer_req *mdr;
497 int ret;
498
499 mdr = kmalloc(sizeof(*mdr), GFP_KERNEL);
500 if (!mdr)
501 return -ENOMEM;
502 memset(mdr, 0, sizeof(*mdr));
503 atomic_set(&mdr->count, 1);
504 init_waitqueue_head(&mdr->waitq);
505 mdr->req.defer = idmap_defer;
506 ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
507 if (ret == -EAGAIN) {
508 wait_event_interruptible_timeout(mdr->waitq,
509 test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
510 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
511 }
512 put_mdr(mdr);
513 return ret;
514}
515
516static int
517idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
518 uid_t *id)
519{
520 struct ent *item, key = {
521 .type = type,
522 };
523 int ret;
524
525 if (namelen + 1 > sizeof(key.name))
526 return -EINVAL;
527 memcpy(key.name, name, namelen);
528 key.name[namelen] = '\0';
529 strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
530 ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
531 if (ret == -ENOENT)
532 ret = -ESRCH; /* nfserr_badname */
533 if (ret)
534 return ret;
535 *id = item->id;
536 ent_put(&item->h, &nametoid_cache);
537 return 0;
538}
539
540static int
541idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
542{
543 struct ent *item, key = {
544 .id = id,
545 .type = type,
546 };
547 int ret;
548
549 strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
550 ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
551 if (ret == -ENOENT)
552 return sprintf(name, "%u", id);
553 if (ret)
554 return ret;
555 ret = strlen(item->name);
556 BUG_ON(ret > IDMAP_NAMESZ);
557 memcpy(name, item->name, ret);
558 ent_put(&item->h, &idtoname_cache);
559 return ret;
560}
561
562int
563nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
564 __u32 *id)
565{
566 return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
567}
568
569int
570nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
571 __u32 *id)
572{
573 return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
574}
575
576int
577nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
578{
579 return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
580}
581
582int
583nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
584{
585 return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
586}