2 /* $Id: nfs4_idmap.c,v 1.4 2003/11/05 14:58:59 rees Exp $ */
6 * the regents of the university of michigan
9 * permission is granted to use, copy, create derivative works and redistribute
10 * this software and such derivative works for any purpose, so long as the name
11 * of the university of michigan is not used in any advertising or publicity
12 * pertaining to the use or distribution of this software without specific,
13 * written prior authorization. if the above copyright notice or any other
14 * identification of the university of michigan is included in any copy of any
15 * portion of this software, then the disclaimer below must also be included.
17 * this software is provided as is, without representation from the university
18 * of michigan as to its fitness for any purpose, and without warranty by the
19 * university of michigan of any kind, either express or implied, including
20 * without limitation the implied warranties of merchantability and fitness for
21 * a particular purpose. the regents of the university of michigan shall not be
22 * liable for any damages, including special, indirect, incidental, or
23 * consequential damages, with respect to any claim arising out of or in
24 * connection with the use of the software, even if it has been or is hereafter
25 * advised of the possibility of such damages.
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/lockmgr.h>
36 #include <sys/fnv_hash.h>
38 #include <sys/syscall.h>
39 #include <sys/sysent.h>
40 #include <sys/libkern.h>
42 #include <rpc/rpcclnt.h>
44 #include <nfs4client/nfs4_dev.h>
45 #include <nfs4client/nfs4_idmap.h>
49 #define IDMAP_DEBUG(...) printf(__VA_ARGS__);
51 #define IDMAP_DEBUG(...)
54 #define IDMAP_HASH_SIZE 37
56 MALLOC_DEFINE(M_IDMAP, "idmap", "idmap");
58 #define idmap_entry_get(ID) MALLOC((ID), struct idmap_entry, sizeof(struct idmap_entry), M_IDMAP, M_WAITOK | M_ZERO)
59 #define idmap_entry_put(ID) FREE((ID), M_IDMAP)
64 struct idmap_msg id_info;
66 TAILQ_ENTRY(idmap_entry) id_entry_id;
67 TAILQ_ENTRY(idmap_entry) id_entry_name;
71 TAILQ_HEAD(, idmap_entry) hash_name[IDMAP_HASH_SIZE];
72 TAILQ_HEAD(, idmap_entry) hash_id[IDMAP_HASH_SIZE];
74 struct lock hash_lock;
77 #define IDMAP_RLOCK(lock) lockmgr(lock, LK_SHARED, NULL, curthread)
78 #define IDMAP_WLOCK(lock) lockmgr(lock, LK_EXCLUSIVE, NULL, curthread)
79 #define IDMAP_UNLOCK(lock) lockmgr(lock, LK_RELEASE, NULL, curthread)
82 static struct idmap_hash idmap_uid_hash;
83 static struct idmap_hash idmap_gid_hash;
85 static struct idmap_entry * idmap_name_lookup(uint32_t, char *);
86 static struct idmap_entry * idmap_id_lookup(uint32_t, ident_t);
87 static int idmap_upcall_name(uint32_t, char *, struct idmap_entry **);
88 static int idmap_upcall_id(uint32_t , ident_t, struct idmap_entry ** );
89 static int idmap_add(struct idmap_entry *);
92 idmap_upcall_name(uint32_t type, char * name, struct idmap_entry ** found)
95 struct idmap_entry * e;
98 if (type > IDMAP_MAX_TYPE || type == 0) {
99 IDMAP_DEBUG("bad type %d\n", type);
100 return EINVAL; /* XXX */
103 if (name == NULL || (len = strlen(name)) == 0 || len > IDMAP_MAXNAMELEN) {
104 IDMAP_DEBUG("idmap_upcall_name: bad name\n");
105 return EFAULT; /* XXX */
108 MALLOC(e, struct idmap_entry *, sizeof(struct idmap_entry), M_IDMAP,
111 e->id_info.id_type = type;
112 bcopy(name, e->id_info.id_name, len);
113 e->id_info.id_namelen = len;
116 siz = sizeof(struct idmap_msg);
117 error = nfs4dev_call(NFS4DEV_TYPE_IDMAP, (caddr_t)&e->id_info, siz,
118 (caddr_t)&e->id_info, &siz);
121 IDMAP_DEBUG("error %d in nfs4dev_upcall()\n", error);
126 if (siz != sizeof(struct idmap_msg)) {
127 IDMAP_DEBUG("bad size of returned message\n");
137 idmap_upcall_id(uint32_t type, ident_t id, struct idmap_entry ** found)
140 struct idmap_entry * e;
143 if (type > IDMAP_MAX_TYPE)
144 panic("bad type"); /* XXX */
146 MALLOC(e, struct idmap_entry *, sizeof(struct idmap_entry), M_IDMAP,
149 e->id_info.id_type = type;
150 e->id_info.id_namelen = 0; /* should already */
151 e->id_info.id_id = id;
153 siz = sizeof(struct idmap_msg);
154 error = nfs4dev_call(NFS4DEV_TYPE_IDMAP, (caddr_t)&e->id_info, siz,
155 (caddr_t)&e->id_info, &siz);
158 IDMAP_DEBUG("error %d in nfs4dev_upcall()\n", error);
163 if (siz != sizeof(struct idmap_msg)) {
164 IDMAP_DEBUG("bad size of returned message\n");
174 idmap_hashf(struct idmap_entry *e, uint32_t * hval_id, uint32_t * hval_name)
176 switch (e->id_info.id_type) {
178 *hval_id = e->id_info.id_id.uid % IDMAP_HASH_SIZE;
181 *hval_id = e->id_info.id_id.gid % IDMAP_HASH_SIZE;
185 panic("hashf: bad type!");
189 if (e->id_info.id_namelen == 0)
190 /* XXX */ panic("hashf: bad name");
192 *hval_name = fnv_32_str(e->id_info.id_name, FNV1_32_INIT) % IDMAP_HASH_SIZE;
196 idmap_add(struct idmap_entry * e)
198 struct idmap_hash * hash;
199 uint32_t hval_id, hval_name;
201 if (e->id_info.id_namelen == 0) {
202 printf("idmap_add: name of len 0\n");
206 switch (e->id_info.id_type) {
208 hash = &idmap_uid_hash;
211 hash = &idmap_gid_hash;
215 panic("idmap add: bad type!");
219 idmap_hashf(e, &hval_id, &hval_name);
221 IDMAP_WLOCK(&hash->hash_lock);
223 TAILQ_INSERT_TAIL(&hash->hash_id[hval_id], e, id_entry_id);
224 TAILQ_INSERT_TAIL(&hash->hash_name[hval_name], e, id_entry_name);
226 IDMAP_UNLOCK(&hash->hash_lock);
231 static struct idmap_entry *
232 idmap_id_lookup(uint32_t type, ident_t id)
234 struct idmap_hash * hash;
236 struct idmap_entry * e;
240 hash = &idmap_uid_hash;
241 hval = id.uid % IDMAP_HASH_SIZE;
244 hash = &idmap_gid_hash;
245 hval = id.gid % IDMAP_HASH_SIZE;
249 panic("lookup: bad type!");
254 IDMAP_RLOCK(&hash->hash_lock);
256 TAILQ_FOREACH(e, &hash->hash_id[hval], id_entry_name) {
257 if ((type == IDMAP_TYPE_UID && e->id_info.id_id.uid == id.uid)||
258 (type == IDMAP_TYPE_GID && e->id_info.id_id.gid == id.gid)) {
259 IDMAP_UNLOCK(&hash->hash_lock);
264 IDMAP_UNLOCK(&hash->hash_lock);
268 static struct idmap_entry *
269 idmap_name_lookup(uint32_t type, char * name)
271 struct idmap_hash * hash;
273 struct idmap_entry * e;
278 hash = &idmap_uid_hash;
281 hash = &idmap_gid_hash;
285 panic("lookup: bad type!");
291 if (len == 0 || len > IDMAP_MAXNAMELEN) {
292 IDMAP_DEBUG("bad name length %d\n", len);
296 hval = fnv_32_str(name, FNV1_32_INIT) % IDMAP_HASH_SIZE;
298 IDMAP_RLOCK(&hash->hash_lock);
300 TAILQ_FOREACH(e, &hash->hash_name[hval], id_entry_name) {
301 if ((strlen(e->id_info.id_name) == strlen(name)) && strncmp(e->id_info.id_name, name, strlen(name)) == 0) {
302 IDMAP_UNLOCK(&hash->hash_lock);
307 IDMAP_UNLOCK(&hash->hash_lock);
316 for (i=0; i<IDMAP_HASH_SIZE; i++) {
317 TAILQ_INIT(&idmap_uid_hash.hash_name[i]);
318 TAILQ_INIT(&idmap_uid_hash.hash_id[i]);
320 TAILQ_INIT(&idmap_gid_hash.hash_name[i]);
321 TAILQ_INIT(&idmap_gid_hash.hash_id[i]);
324 lockinit(&idmap_uid_hash.hash_lock, PLOCK, "idmap uid hash table", 0,0);
325 lockinit(&idmap_gid_hash.hash_lock, PLOCK, "idmap gid hash table", 0,0);
329 void idmap_uninit(void)
331 struct idmap_entry * e;
334 lockdestroy(&idmap_uid_hash.hash_lock);
335 lockdestroy(&idmap_gid_hash.hash_lock);
337 for (i=0; i<IDMAP_HASH_SIZE; i++) {
338 while(!TAILQ_EMPTY(&idmap_uid_hash.hash_name[i])) {
339 e = TAILQ_FIRST(&idmap_uid_hash.hash_name[i]);
340 TAILQ_REMOVE(&idmap_uid_hash.hash_name[i], e, id_entry_name);
341 TAILQ_REMOVE(&idmap_uid_hash.hash_id[i], e, id_entry_id);
345 while(!TAILQ_EMPTY(&idmap_gid_hash.hash_name[i])) {
346 e = TAILQ_FIRST(&idmap_gid_hash.hash_name[i]);
347 TAILQ_REMOVE(&idmap_gid_hash.hash_name[i], e, id_entry_name);
348 TAILQ_REMOVE(&idmap_gid_hash.hash_id[i], e, id_entry_id);
356 idmap_uid_to_name(uid_t uid, char ** name, size_t * len)
358 struct idmap_entry * e;
365 if ((e = idmap_id_lookup(IDMAP_TYPE_UID, id)) == NULL) {
366 if ((error = idmap_upcall_id(IDMAP_TYPE_UID, id, &e)) != 0) {
367 IDMAP_DEBUG("error in upcall\n");
372 IDMAP_DEBUG("no error from upcall, but no data returned\n");
376 if (idmap_add(e) != 0) {
377 IDMAP_DEBUG("idmap_add failed\n");
383 *name = e->id_info.id_name;
384 *len = e->id_info.id_namelen;
389 idmap_gid_to_name(gid_t gid, char ** name, size_t * len)
391 struct idmap_entry * e;
398 if ((e = idmap_id_lookup(IDMAP_TYPE_GID, id)) == NULL) {
399 if ((error = idmap_upcall_id(IDMAP_TYPE_GID, id, &e))) {
400 IDMAP_DEBUG("error in upcall\n");
405 IDMAP_DEBUG("no error from upcall, but no data returned\n");
409 if (idmap_add(e) != 0) {
410 IDMAP_DEBUG("idmap_add failed\n");
415 *name = e->id_info.id_name;
416 *len = e->id_info.id_namelen;
421 idmap_name_to_uid(char * name, size_t len, uid_t * id)
423 struct idmap_entry * e;
430 if (len == 0 || len > IDMAP_MAXNAMELEN) {
431 IDMAP_DEBUG("idmap_name_to_uid: bad len\n");
436 MALLOC(namestr, char *, len + 1, M_TEMP, M_WAITOK);
437 bcopy(name, namestr, len);
441 if ((e = idmap_name_lookup(IDMAP_TYPE_UID, namestr)) == NULL) {
442 if ((error = idmap_upcall_name(IDMAP_TYPE_UID, namestr, &e))) {
443 FREE(namestr, M_TEMP);
448 IDMAP_DEBUG("no error from upcall, but no data returned\n");
449 FREE(namestr, M_TEMP);
453 if (idmap_add(e) != 0) {
454 IDMAP_DEBUG("idmap_add failed\n");
459 *id = e->id_info.id_id.uid;
460 FREE(namestr, M_TEMP);
465 idmap_name_to_gid(char * name, size_t len, gid_t * id)
467 struct idmap_entry * e;
475 if (len == 0 || len > IDMAP_MAXNAMELEN) {
476 IDMAP_DEBUG("idmap_name_to_uid: bad len\n");
481 MALLOC(namestr, char *, len + 1, M_TEMP, M_WAITOK);
482 bcopy(name, namestr, len);
486 if ((e = idmap_name_lookup(IDMAP_TYPE_GID, namestr)) == NULL) {
487 if ((error = idmap_upcall_name(IDMAP_TYPE_GID, namestr, &e)) != 0) {
488 IDMAP_DEBUG("error in upcall\n");
489 FREE(namestr, M_TEMP);
494 IDMAP_DEBUG("no error from upcall, but no data returned\n");
495 FREE(namestr, M_TEMP);
499 if (idmap_add(e) != 0) {
500 IDMAP_DEBUG("idmap_add failed\n");
505 *id = e->id_info.id_id.gid;
506 FREE(namestr, M_TEMP);