Blame view

fs/nfsd/nfs4idmap.c 16 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
   *  Mapping of UID/GIDs to name and vice versa.
   *
   *  Copyright (c) 2002, 2003 The Regents of the University of
   *  Michigan.  All rights reserved.
   *
   *  Marius Aamodt Eriksen <marius@umich.edu>
   *
   *  Redistribution and use in source and binary forms, with or without
   *  modification, are permitted provided that the following conditions
   *  are met:
   *
   *  1. Redistributions of source code must retain the above copyright
   *     notice, this list of conditions and the following disclaimer.
   *  2. Redistributions in binary form must reproduce the above copyright
   *     notice, this list of conditions and the following disclaimer in the
   *     documentation and/or other materials provided with the distribution.
   *  3. Neither the name of the University nor the names of its
   *     contributors may be used to endorse or promote products derived
   *     from this software without specific prior written permission.
   *
   *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
  #include <linux/seq_file.h>
341eb1844   Boaz Harrosh   nfsd: Source file...
36
  #include <linux/sched.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
37
  #include <linux/slab.h>
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
38
  #include <linux/sunrpc/svc_xprt.h>
f5c8593b9   Stanislav Kinsbursky   NFSd: use network...
39
  #include <net/net_namespace.h>
2ca72e17e   J. Bruce Fields   nfsd4: move idmap...
40
  #include "idmap.h"
3c7260234   J. Bruce Fields   nfsd4: return nfs...
41
  #include "nfsd.h"
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
42
  #include "netns.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
  
  /*
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
45
46
47
48
49
50
51
52
   * Turn off idmapping when using AUTH_SYS.
   */
  static bool nfs4_disable_idmapping = true;
  module_param(nfs4_disable_idmapping, bool, 0644);
  MODULE_PARM_DESC(nfs4_disable_idmapping,
  		"Turn off server's NFSv4 idmapping when using 'sec=sys'");
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
56
57
58
59
   * Cache entry
   */
  
  /*
   * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
   * that.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
  struct ent {
  	struct cache_head h;
  	int               type;		       /* User / Group */
b5663898e   Eric W. Biederman   nfsd: idmap use u...
63
  	u32               id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
  	char              name[IDMAP_NAMESZ];
  	char              authname[IDMAP_NAMESZ];
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
69
70
  /* Common entry handling */
  
  #define ENT_HASHBITS          8
  #define ENT_HASHMAX           (1 << ENT_HASHBITS)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71

f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
72
73
  static void
  ent_init(struct cache_head *cnew, struct cache_head *citm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
  {
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
75
76
  	struct ent *new = container_of(cnew, struct ent, h);
  	struct ent *itm = container_of(citm, struct ent, h);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
  	new->id = itm->id;
  	new->type = itm->type;
  
  	strlcpy(new->name, itm->name, sizeof(new->name));
  	strlcpy(new->authname, itm->authname, sizeof(new->name));
  }
fd39ca9a8   NeilBrown   [PATCH] knfsd: nf...
83
  static void
baab935ff   NeilBrown   [PATCH] knfsd: Co...
84
  ent_put(struct kref *ref)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  {
baab935ff   NeilBrown   [PATCH] knfsd: Co...
86
87
  	struct ent *map = container_of(ref, struct ent, h.ref);
  	kfree(map);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
  }
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
89
90
91
92
93
94
95
96
97
  static struct cache_head *
  ent_alloc(void)
  {
  	struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
  	if (e)
  		return &e->h;
  	else
  		return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
  /*
   * ID -> Name cache
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  static uint32_t
  idtoname_hash(struct ent *ent)
  {
  	uint32_t hash;
  
  	hash = hash_str(ent->authname, ENT_HASHBITS);
  	hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
  
  	/* Flip LSB for user/group */
  	if (ent->type == IDMAP_TYPE_GROUP)
  		hash ^= 1;
  
  	return hash;
  }
  
  static void
  idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
      int *blen)
  {
   	struct ent *ent = container_of(ch, struct ent, h);
  	char idstr[11];
  
  	qword_add(bpp, blen, ent->authname);
0a725fc4d   J. Bruce Fields   nfsd4: idmap upca...
124
  	snprintf(idstr, sizeof(idstr), "%u", ent->id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
  	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  	qword_add(bpp, blen, idstr);
  
  	(*bpp)[-1] = '
  ';
  }
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
131
132
  static int
  idtoname_match(struct cache_head *ca, struct cache_head *cb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  {
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
134
135
  	struct ent *a = container_of(ca, struct ent, h);
  	struct ent *b = container_of(cb, struct ent, h);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  	return (a->id == b->id && a->type == b->type &&
  	    strcmp(a->authname, b->authname) == 0);
  }
  
  static int
  idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  {
  	struct ent *ent;
  
  	if (h == NULL) {
  		seq_puts(m, "#domain type id [name]
  ");
  		return 0;
  	}
  	ent = container_of(h, struct ent, h);
0a725fc4d   J. Bruce Fields   nfsd4: idmap upca...
151
  	seq_printf(m, "%s %s %u", ent->authname,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
154
155
156
157
158
159
160
161
  			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  			ent->id);
  	if (test_bit(CACHE_VALID, &h->flags))
  		seq_printf(m, " %s", ent->name);
  	seq_printf(m, "
  ");
  	return 0;
  }
  
  static void
2da8ca26c   Trond Myklebust   NFSD: Clean up th...
162
  warn_no_idmapd(struct cache_detail *detail, int has_died)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
  {
  	printk("nfsd: nfsv4 idmapping failing: has idmapd %s?
  ",
2da8ca26c   Trond Myklebust   NFSD: Clean up th...
166
  			has_died ? "died" : "not been started");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
  }
  
  
  static int         idtoname_parse(struct cache_detail *, char *, int);
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
171
172
173
  static struct ent *idtoname_lookup(struct cache_detail *, struct ent *);
  static struct ent *idtoname_update(struct cache_detail *, struct ent *,
  				   struct ent *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174

c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
175
  static struct cache_detail idtoname_cache_template = {
f35279d3f   Bruce Allan   [PATCH] sunrpc: c...
176
  	.owner		= THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  	.hash_size	= ENT_HASHMAX,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
  	.name		= "nfs4.idtoname",
  	.cache_put	= ent_put,
73fb847a4   Stanislav Kinsbursky   SUNRPC: introduce...
180
  	.cache_request	= idtoname_request,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
  	.cache_parse	= idtoname_parse,
  	.cache_show	= idtoname_show,
  	.warn_no_listener = warn_no_idmapd,
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
184
185
186
187
  	.match		= idtoname_match,
  	.init		= ent_init,
  	.update		= ent_init,
  	.alloc		= ent_alloc,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
  };
a254b246e   Harvey Harrison   nfsd: fix sparse ...
189
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
193
  idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
  {
  	struct ent ent, *res;
  	char *buf1, *bp;
c9b6cbe56   J. Bruce Fields   knfsd: nfs4 name-...
194
  	int len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  	int error = -EINVAL;
  
  	if (buf[buflen - 1] != '
  ')
  		return (-EINVAL);
  	buf[buflen - 1]= '\0';
  
  	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (buf1 == NULL)
  		return (-ENOMEM);
  
  	memset(&ent, 0, sizeof(ent));
  
  	/* Authentication name */
13c82e8eb   Kinglong Mee   NFSD: Full checki...
209
210
  	len = qword_get(&buf, buf1, PAGE_SIZE);
  	if (len <= 0 || len >= IDMAP_NAMESZ)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  		goto out;
  	memcpy(ent.authname, buf1, sizeof(ent.authname));
  
  	/* Type */
  	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  		goto out;
  	ent.type = strcmp(buf1, "user") == 0 ?
  		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  
  	/* ID */
  	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  		goto out;
  	ent.id = simple_strtoul(buf1, &bp, 10);
  	if (bp == buf1)
  		goto out;
  
  	/* expiry */
  	ent.h.expiry_time = get_expiry(&buf);
  	if (ent.h.expiry_time == 0)
  		goto out;
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
231
  	error = -ENOMEM;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
232
  	res = idtoname_lookup(cd, &ent);
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
233
234
  	if (!res)
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  	/* Name */
c9b6cbe56   J. Bruce Fields   knfsd: nfs4 name-...
236
237
  	error = -EINVAL;
  	len = qword_get(&buf, buf1, PAGE_SIZE);
13c82e8eb   Kinglong Mee   NFSD: Full checki...
238
  	if (len < 0 || len >= IDMAP_NAMESZ)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
  		goto out;
c9b6cbe56   J. Bruce Fields   knfsd: nfs4 name-...
240
  	if (len == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
  		set_bit(CACHE_NEGATIVE, &ent.h.flags);
d4395e03f   J. Bruce Fields   knfsd: fix broken...
242
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  		memcpy(ent.name, buf1, sizeof(ent.name));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
  	error = -ENOMEM;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
245
  	res = idtoname_update(cd, &ent, res);
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
246
  	if (res == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
  		goto out;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
248
  	cache_put(&res->h, cd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
251
  	error = 0;
  out:
  	kfree(buf1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
  	return error;
  }
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
254
  static struct ent *
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
255
  idtoname_lookup(struct cache_detail *cd, struct ent *item)
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
256
  {
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
257
  	struct cache_head *ch = sunrpc_cache_lookup(cd, &item->h,
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
258
259
260
261
262
263
264
265
  						    idtoname_hash(item));
  	if (ch)
  		return container_of(ch, struct ent, h);
  	else
  		return NULL;
  }
  
  static struct ent *
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
266
  idtoname_update(struct cache_detail *cd, struct ent *new, struct ent *old)
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
267
  {
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
268
  	struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
269
270
271
272
273
274
  						    idtoname_hash(new));
  	if (ch)
  		return container_of(ch, struct ent, h);
  	else
  		return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
277
278
  
  /*
   * Name -> ID cache
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
  static inline int
  nametoid_hash(struct ent *ent)
  {
  	return hash_str(ent->name, ENT_HASHBITS);
  }
fd39ca9a8   NeilBrown   [PATCH] knfsd: nf...
284
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
286
287
288
289
290
291
292
293
294
295
296
  nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
      int *blen)
  {
   	struct ent *ent = container_of(ch, struct ent, h);
  
  	qword_add(bpp, blen, ent->authname);
  	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
  	qword_add(bpp, blen, ent->name);
  
  	(*bpp)[-1] = '
  ';
  }
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
297
298
  static int
  nametoid_match(struct cache_head *ca, struct cache_head *cb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
  {
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
300
301
  	struct ent *a = container_of(ca, struct ent, h);
  	struct ent *b = container_of(cb, struct ent, h);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  	return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
  	    strcmp(a->authname, b->authname) == 0);
  }
  
  static int
  nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
  {
  	struct ent *ent;
  
  	if (h == NULL) {
  		seq_puts(m, "#domain type name [id]
  ");
  		return 0;
  	}
  	ent = container_of(h, struct ent, h);
  	seq_printf(m, "%s %s %s", ent->authname,
  			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
  			ent->name);
  	if (test_bit(CACHE_VALID, &h->flags))
0a725fc4d   J. Bruce Fields   nfsd4: idmap upca...
321
  		seq_printf(m, " %u", ent->id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
323
324
325
  	seq_printf(m, "
  ");
  	return 0;
  }
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
326
327
328
  static struct ent *nametoid_lookup(struct cache_detail *, struct ent *);
  static struct ent *nametoid_update(struct cache_detail *, struct ent *,
  				   struct ent *);
fd39ca9a8   NeilBrown   [PATCH] knfsd: nf...
329
  static int         nametoid_parse(struct cache_detail *, char *, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330

9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
331
  static struct cache_detail nametoid_cache_template = {
f35279d3f   Bruce Allan   [PATCH] sunrpc: c...
332
  	.owner		= THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
  	.hash_size	= ENT_HASHMAX,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
  	.name		= "nfs4.nametoid",
  	.cache_put	= ent_put,
73fb847a4   Stanislav Kinsbursky   SUNRPC: introduce...
336
  	.cache_request	= nametoid_request,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
339
  	.cache_parse	= nametoid_parse,
  	.cache_show	= nametoid_show,
  	.warn_no_listener = warn_no_idmapd,
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
340
341
342
343
  	.match		= nametoid_match,
  	.init		= ent_init,
  	.update		= ent_init,
  	.alloc		= ent_alloc,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  };
fd39ca9a8   NeilBrown   [PATCH] knfsd: nf...
345
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
  nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
  {
  	struct ent ent, *res;
  	char *buf1;
48c348b09   Kinglong Mee   NFSD: Fix bad usi...
350
  	int len, error = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
352
353
354
355
356
357
358
359
360
361
362
363
  
  	if (buf[buflen - 1] != '
  ')
  		return (-EINVAL);
  	buf[buflen - 1]= '\0';
  
  	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (buf1 == NULL)
  		return (-ENOMEM);
  
  	memset(&ent, 0, sizeof(ent));
  
  	/* Authentication name */
13c82e8eb   Kinglong Mee   NFSD: Full checki...
364
365
  	len = qword_get(&buf, buf1, PAGE_SIZE);
  	if (len <= 0 || len >= IDMAP_NAMESZ)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
367
368
369
370
371
372
373
374
375
  		goto out;
  	memcpy(ent.authname, buf1, sizeof(ent.authname));
  
  	/* Type */
  	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
  		goto out;
  	ent.type = strcmp(buf1, "user") == 0 ?
  		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
  
  	/* Name */
48c348b09   Kinglong Mee   NFSD: Fix bad usi...
376
377
  	len = qword_get(&buf, buf1, PAGE_SIZE);
  	if (len <= 0 || len >= IDMAP_NAMESZ)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  		goto out;
  	memcpy(ent.name, buf1, sizeof(ent.name));
  
  	/* expiry */
  	ent.h.expiry_time = get_expiry(&buf);
  	if (ent.h.expiry_time == 0)
  		goto out;
  
  	/* ID */
  	error = get_int(&buf, &ent.id);
  	if (error == -EINVAL)
  		goto out;
  	if (error == -ENOENT)
  		set_bit(CACHE_NEGATIVE, &ent.h.flags);
  
  	error = -ENOMEM;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
394
  	res = nametoid_lookup(cd, &ent);
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
395
396
  	if (res == NULL)
  		goto out;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
397
  	res = nametoid_update(cd, &ent, res);
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
398
  	if (res == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  		goto out;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
400
  	cache_put(&res->h, cd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
402
403
  	error = 0;
  out:
  	kfree(buf1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
405
  	return (error);
  }
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
406
407
  
  static struct ent *
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
408
  nametoid_lookup(struct cache_detail *cd, struct ent *item)
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
409
  {
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
410
  	struct cache_head *ch = sunrpc_cache_lookup(cd, &item->h,
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
411
412
413
414
415
416
417
418
  						    nametoid_hash(item));
  	if (ch)
  		return container_of(ch, struct ent, h);
  	else
  		return NULL;
  }
  
  static struct ent *
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
419
  nametoid_update(struct cache_detail *cd, struct ent *new, struct ent *old)
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
420
  {
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
421
  	struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
f9ecc921b   NeilBrown   [PATCH] knfsd: Us...
422
423
424
425
426
427
  						    nametoid_hash(new));
  	if (ch)
  		return container_of(ch, struct ent, h);
  	else
  		return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
429
430
431
  
  /*
   * Exported API
   */
dbf847ecb   J. Bruce Fields   knfsd: allow cach...
432
  int
43ec1a20b   Stanislav Kinsbursky   nfsd: pass networ...
433
  nfsd_idmap_init(struct net *net)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
  {
dbf847ecb   J. Bruce Fields   knfsd: allow cach...
435
  	int rv;
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
436
  	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
dbf847ecb   J. Bruce Fields   knfsd: allow cach...
437

c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
438
439
440
441
  	nn->idtoname_cache = cache_create_net(&idtoname_cache_template, net);
  	if (IS_ERR(nn->idtoname_cache))
  		return PTR_ERR(nn->idtoname_cache);
  	rv = cache_register_net(nn->idtoname_cache, net);
dbf847ecb   J. Bruce Fields   knfsd: allow cach...
442
  	if (rv)
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
443
  		goto destroy_idtoname_cache;
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
444
445
  	nn->nametoid_cache = cache_create_net(&nametoid_cache_template, net);
  	if (IS_ERR(nn->nametoid_cache)) {
92566e287   Julia Lawall   fs/nfsd/nfs4idmap...
446
  		rv = PTR_ERR(nn->nametoid_cache);
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
447
  		goto unregister_idtoname_cache;
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
448
449
450
451
  	}
  	rv = cache_register_net(nn->nametoid_cache, net);
  	if (rv)
  		goto destroy_nametoid_cache;
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
452
  	return 0;
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
453
454
  destroy_nametoid_cache:
  	cache_destroy_net(nn->nametoid_cache, net);
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
455
456
457
458
  unregister_idtoname_cache:
  	cache_unregister_net(nn->idtoname_cache, net);
  destroy_idtoname_cache:
  	cache_destroy_net(nn->idtoname_cache, net);
dbf847ecb   J. Bruce Fields   knfsd: allow cach...
459
  	return rv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
461
462
  }
  
  void
43ec1a20b   Stanislav Kinsbursky   nfsd: pass networ...
463
  nfsd_idmap_shutdown(struct net *net)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
  {
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
465
466
467
  	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  
  	cache_unregister_net(nn->idtoname_cache, net);
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
468
  	cache_unregister_net(nn->nametoid_cache, net);
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
469
  	cache_destroy_net(nn->idtoname_cache, net);
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
470
  	cache_destroy_net(nn->nametoid_cache, net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
  static int
  idmap_lookup(struct svc_rqst *rqstp,
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
474
475
  		struct ent *(*lookup_fn)(struct cache_detail *, struct ent *),
  		struct ent *key, struct cache_detail *detail, struct ent **item)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
  	int ret;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
478
  	*item = lookup_fn(detail, key);
839049a87   NeilBrown   nfsd/idmap: drop ...
479
  	if (!*item)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  		return -ENOMEM;
839049a87   NeilBrown   nfsd/idmap: drop ...
481
482
483
484
485
   retry:
  	ret = cache_check(detail, &(*item)->h, &rqstp->rq_chandle);
  
  	if (ret == -ETIMEDOUT) {
  		struct ent *prev_item = *item;
f890edbbe   Stanislav Kinsbursky   NFSd: remove hard...
486
  		*item = lookup_fn(detail, key);
839049a87   NeilBrown   nfsd/idmap: drop ...
487
488
489
  		if (*item != prev_item)
  			goto retry;
  		cache_put(&(*item)->h, detail);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
  	return ret;
  }
3ab4d8b12   J. Bruce Fields   knfsd: nfsd: set ...
493
494
495
496
497
498
499
500
  static char *
  rqst_authname(struct svc_rqst *rqstp)
  {
  	struct auth_domain *clp;
  
  	clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
  	return clp->name;
  }
3c7260234   J. Bruce Fields   nfsd4: return nfs...
501
  static __be32
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
  idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
b5663898e   Eric W. Biederman   nfsd: idmap use u...
503
  		u32 *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
505
506
507
508
  {
  	struct ent *item, key = {
  		.type = type,
  	};
  	int ret;
9695c7057   Stanislav Kinsbursky   SUNRPC: service r...
509
  	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
  
  	if (namelen + 1 > sizeof(key.name))
3c7260234   J. Bruce Fields   nfsd4: return nfs...
512
  		return nfserr_badowner;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
514
  	memcpy(key.name, name, namelen);
  	key.name[namelen] = '\0';
3ab4d8b12   J. Bruce Fields   knfsd: nfsd: set ...
515
  	strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
516
  	ret = idmap_lookup(rqstp, nametoid_lookup, &key, nn->nametoid_cache, &item);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
517
  	if (ret == -ENOENT)
3c7260234   J. Bruce Fields   nfsd4: return nfs...
518
  		return nfserr_badowner;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
  	if (ret)
3c7260234   J. Bruce Fields   nfsd4: return nfs...
520
  		return nfserrno(ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
  	*id = item->id;
9e75a4dee   Stanislav Kinsbursky   nfsd: make name-t...
522
  	cache_put(&item->h, nn->nametoid_cache);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
523
524
  	return 0;
  }
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
525
  static __be32 encode_ascii_id(struct xdr_stream *xdr, u32 id)
3554116d3   J. Bruce Fields   nfsd4: simplify x...
526
527
528
  {
  	char buf[11];
  	int len;
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
529
  	__be32 *p;
3554116d3   J. Bruce Fields   nfsd4: simplify x...
530
531
  
  	len = sprintf(buf, "%u", id);
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
532
533
  	p = xdr_reserve_space(xdr, len + 4);
  	if (!p)
3554116d3   J. Bruce Fields   nfsd4: simplify x...
534
  		return nfserr_resource;
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
535
  	p = xdr_encode_opaque(p, buf, len);
3554116d3   J. Bruce Fields   nfsd4: simplify x...
536
537
  	return 0;
  }
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
538
539
  static __be32 idmap_id_to_name(struct xdr_stream *xdr,
  			       struct svc_rqst *rqstp, int type, u32 id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
541
542
543
544
  {
  	struct ent *item, key = {
  		.id = id,
  		.type = type,
  	};
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
545
  	__be32 *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
  	int ret;
9695c7057   Stanislav Kinsbursky   SUNRPC: service r...
547
  	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548

3ab4d8b12   J. Bruce Fields   knfsd: nfsd: set ...
549
  	strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
550
  	ret = idmap_lookup(rqstp, idtoname_lookup, &key, nn->idtoname_cache, &item);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
  	if (ret == -ENOENT)
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
552
  		return encode_ascii_id(xdr, id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
  	if (ret)
3554116d3   J. Bruce Fields   nfsd4: simplify x...
554
  		return nfserrno(ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
  	ret = strlen(item->name);
3554116d3   J. Bruce Fields   nfsd4: simplify x...
556
  	WARN_ON_ONCE(ret > IDMAP_NAMESZ);
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
557
558
  	p = xdr_reserve_space(xdr, ret + 4);
  	if (!p)
3554116d3   J. Bruce Fields   nfsd4: simplify x...
559
  		return nfserr_resource;
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
560
  	p = xdr_encode_opaque(p, item->name, ret);
c2e76ef5e   Stanislav Kinsbursky   nfsd: make id-to-...
561
  	cache_put(&item->h, nn->idtoname_cache);
3554116d3   J. Bruce Fields   nfsd4: simplify x...
562
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
  }
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
564
  static bool
b5663898e   Eric W. Biederman   nfsd: idmap use u...
565
  numeric_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
566
567
568
569
570
571
572
573
574
575
  {
  	int ret;
  	char buf[11];
  
  	if (namelen + 1 > sizeof(buf))
  		/* too long to represent a 32-bit id: */
  		return false;
  	/* Just to make sure it's null-terminated: */
  	memcpy(buf, name, namelen);
  	buf[namelen] = '\0';
9959ba0c2   Malahal Naineni   NFSD: pass null t...
576
  	ret = kstrtouint(buf, 10, id);
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
577
578
579
580
  	return ret == 0;
  }
  
  static __be32
b5663898e   Eric W. Biederman   nfsd: idmap use u...
581
  do_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
582
  {
d5497fc69   J. Bruce Fields   nfsd4: move rq_fl...
583
  	if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
584
585
586
587
588
589
590
591
  		if (numeric_name_to_id(rqstp, type, name, namelen, id))
  			return 0;
  		/*
  		 * otherwise, fall through and try idmapping, for
  		 * backwards compatibility with clients sending names:
  		 */
  	return idmap_name_to_id(rqstp, type, name, namelen, id);
  }
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
592
593
  static __be32 encode_name_from_id(struct xdr_stream *xdr,
  				  struct svc_rqst *rqstp, int type, u32 id)
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
594
  {
d5497fc69   J. Bruce Fields   nfsd4: move rq_fl...
595
  	if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
596
597
  		return encode_ascii_id(xdr, id);
  	return idmap_id_to_name(xdr, rqstp, type, id);
e9541ce8e   J. Bruce Fields   nfsd4: allow nume...
598
  }
3c7260234   J. Bruce Fields   nfsd4: return nfs...
599
  __be32
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
  nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
65e10f6d0   Eric W. Biederman   nfsd: Convert idm...
601
  		kuid_t *uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
  {
65e10f6d0   Eric W. Biederman   nfsd: Convert idm...
603
604
605
606
607
608
609
  	__be32 status;
  	u32 id = -1;
  	status = do_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, &id);
  	*uid = make_kuid(&init_user_ns, id);
  	if (!uid_valid(*uid))
  		status = nfserr_badowner;
  	return status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
  }
3c7260234   J. Bruce Fields   nfsd4: return nfs...
611
  __be32
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
  nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
65e10f6d0   Eric W. Biederman   nfsd: Convert idm...
613
  		kgid_t *gid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
  {
65e10f6d0   Eric W. Biederman   nfsd: Convert idm...
615
616
617
618
619
620
621
  	__be32 status;
  	u32 id = -1;
  	status = do_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, &id);
  	*gid = make_kgid(&init_user_ns, id);
  	if (!gid_valid(*gid))
  		status = nfserr_badowner;
  	return status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
  }
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
623
624
  __be32 nfsd4_encode_user(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  			 kuid_t uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625
  {
65e10f6d0   Eric W. Biederman   nfsd: Convert idm...
626
  	u32 id = from_kuid(&init_user_ns, uid);
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
627
  	return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_USER, id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628
  }
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
629
630
  __be32 nfsd4_encode_group(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  			  kgid_t gid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
  {
65e10f6d0   Eric W. Biederman   nfsd: Convert idm...
632
  	u32 id = from_kgid(&init_user_ns, gid);
ddd1ea563   J. Bruce Fields   nfsd4: use xdr_re...
633
  	return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_GROUP, id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
  }