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