nfs-ganesha 1.4

idmapper_cache.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  * ---------------------------------------
00024  */
00025 
00036 #ifdef HAVE_CONFIG_H
00037 #include "config.h"
00038 #endif
00039 
00040 #ifdef _SOLARIS
00041 #include "solaris_port.h"
00042 #endif
00043 
00044 #include "HashData.h"
00045 #include "HashTable.h"
00046 #include "log.h"
00047 #include "nfs_core.h"
00048 #include "nfs_exports.h"
00049 #include "config_parsing.h"
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <strings.h>
00053 #include <pwd.h>
00054 #include <grp.h>
00055 
00056 #ifdef _APPLE
00057 #define strnlen( s, l ) strlen( s )
00058 #else
00059 size_t strnlen(const char *s, size_t maxlen);
00060 #endif
00061 
00062 /* Hashtable used to cache the hostname, accessed by their IP addess */
00063 hash_table_t *ht_pwnam;
00064 hash_table_t *ht_grnam;
00065 hash_table_t *ht_pwuid;
00066 hash_table_t *ht_grgid;
00067 hash_table_t *ht_uidgid;
00068 
00084 uint32_t idmapper_value_hash_func(hash_parameter_t * p_hparam,
00085                                   hash_buffer_t * buffclef)
00086 {
00087   unsigned int sum = 0;
00088   unsigned int i = 0;
00089   unsigned char c;
00090 
00091   /* Compute the sum of all the characters */
00092   for(i = 0, c = ((char *)buffclef->pdata)[0]; ((char *)buffclef->pdata)[i] != '\0';
00093       c = ((char *)buffclef->pdata)[++i], sum += c) ;
00094 
00095   return (unsigned long)(sum % p_hparam->index_size);
00096 }                               /*  ip_name_value_hash_func */
00097 
00098 
00099 uint32_t namemapper_value_hash_func(hash_parameter_t * p_hparam,
00100                                          hash_buffer_t * buffclef)
00101 {
00102   return ((unsigned long)(buffclef->pdata) % p_hparam->index_size);
00103 }
00104 
00119 uint64_t idmapper_rbt_hash_func(hash_parameter_t * p_hparam,
00120                                 hash_buffer_t * buffclef)
00121 {
00122   unsigned int result;
00123 
00124   if(idmap_compute_hash_value((char *)buffclef->pdata,
00125                               (uint32_t *) & result) != ID_MAPPER_SUCCESS)
00126     return 0;
00127 
00128   return (unsigned long)result;
00129 }                               /* ip_name_rbt_hash_func */
00130 
00131 uint64_t namemapper_rbt_hash_func(hash_parameter_t * p_hparam,
00132                                   hash_buffer_t * buffclef)
00133 {
00134   return (unsigned long)(buffclef->pdata);
00135 }
00136 
00150 int compare_idmapper(hash_buffer_t * buff1, hash_buffer_t * buff2)
00151 {
00152   return strncmp((char *)(buff1->pdata), (char *)(buff2->pdata), PWENT_MAX_LEN);
00153 }                               /* compare_xid */
00154 
00155 int compare_namemapper(hash_buffer_t * buff1, hash_buffer_t * buff2)
00156 {
00157   unsigned long xid1 = (unsigned long)(buff1->pdata);
00158   unsigned long xid2 = (unsigned long)(buff2->pdata);
00159 
00160   return (xid1 == xid2) ? 0 : 1;
00161 }                               /* compare_xid */
00162 
00176 int display_idmapper_key(hash_buffer_t * pbuff, char *str)
00177 {
00178   return sprintf(str, "%s", (char *)(pbuff->pdata));
00179 }                               /* display_idmapper */
00180 
00194 int display_idmapper_val(hash_buffer_t * pbuff, char *str)
00195 {
00196   return sprintf(str, "%lu", (unsigned long)(pbuff->pdata));
00197 }                               /* display_idmapper_val */
00198 
00210 int idmap_uid_init(nfs_idmap_cache_parameter_t param)
00211 {
00212   if((ht_pwnam = HashTable_Init(&param.hash_param)) == NULL)
00213     {
00214       LogCrit(COMPONENT_IDMAPPER,
00215               "NFS ID MAPPER: Cannot init IDMAP_UID cache");
00216       return -1;
00217     }
00218 
00219   return ID_MAPPER_SUCCESS;
00220 }                               /* idmap_uid_init */
00221 
00222 int uidgidmap_init(nfs_idmap_cache_parameter_t param)
00223 {
00224   if((ht_uidgid = HashTable_Init(&param.hash_param)) == NULL)
00225     {
00226       LogCrit(COMPONENT_IDMAPPER,
00227               "NFS UID/GID MAPPER: Cannot init UIDGID_MAP cache");
00228       return -1;
00229     }
00230 
00231   return ID_MAPPER_SUCCESS;
00232 }                               /* idmap_uid_init */
00233 
00234 int idmap_uname_init(nfs_idmap_cache_parameter_t param)
00235 {
00236   if((ht_pwuid = HashTable_Init(&param.hash_param)) == NULL)
00237     {
00238       LogCrit(COMPONENT_IDMAPPER,
00239               "NFS ID MAPPER: Cannot init IDMAP_UNAME cache");
00240       return -1;
00241     }
00242 
00243   return ID_MAPPER_SUCCESS;
00244 }                               /* idmap_uid_init */
00245 
00257 int idmap_gid_init(nfs_idmap_cache_parameter_t param)
00258 {
00259   if((ht_grnam = HashTable_Init(&param.hash_param)) == NULL)
00260     {
00261       LogCrit(COMPONENT_IDMAPPER,
00262               "NFS ID MAPPER: Cannot init IDMAP_GID cache");
00263       return -1;
00264     }
00265 
00266   return ID_MAPPER_SUCCESS;
00267 }                               /* idmap_uid_init */
00268 
00269 int idmap_gname_init(nfs_idmap_cache_parameter_t param)
00270 {
00271   if((ht_grgid = HashTable_Init(&param.hash_param)) == NULL)
00272     {
00273       LogCrit(COMPONENT_IDMAPPER,
00274               "NFS ID MAPPER: Cannot init IDMAP_GNAME cache");
00275       return -1;
00276     }
00277 
00278   return ID_MAPPER_SUCCESS;
00279 }                               /* idmap_uid_init */
00280 
00289 int idmap_compute_hash_value(char *name, uint32_t * phashval)
00290 {
00291    uint32_t res ;
00292 
00293    res = Lookup3_hash_buff( name, strlen( name ) ) ;
00294 
00295     return (int)res ;
00296 }
00297 
00298 int ___idmap_compute_hash_value(char *name, uint32_t * phashval)
00299 {
00300   char padded_name[PWENT_MAX_LEN];
00301   uint64_t computed_value = 0;
00302   unsigned int i = 0;
00303   unsigned int offset = 0;
00304   uint64_t extract = 0;
00305   uint64_t sum = 0;
00306   uint64_t i1;
00307   uint64_t i2;
00308   uint64_t i3;
00309   uint64_t i4;
00310   uint64_t i5;
00311   uint64_t i6;
00312   uint64_t i7;
00313   uint64_t i8;
00314   uint64_t i9;
00315   uint64_t l;
00316 
00317   if(name == NULL || phashval == NULL)
00318     return ID_MAPPER_INVALID_ARGUMENT;
00319 
00320   memset(padded_name, 0, PWENT_MAX_LEN);
00321 
00322   /* Copy the string to the padded one */
00323   for(i = 0; i < strnlen(name, PWENT_MAX_LEN); padded_name[i] = name[i], i++) ;
00324 
00325 #ifdef WITH_PRINTF_DEBUG_PWHASH_COMPUTE
00326   printf("#%s# :", padded_name);
00327 #endif
00328 
00329   /* For each 9 character pack:
00330    *   - keep the 7 first bit (the 8th is often 0: ascii string)
00331    *   - pack 7x9 bit to 63 bits using xor
00332    *   - xor the last 8th bit to a single 0 , or-ed with the rest
00333    * Proceeding with the next 9 bytes pack will produce a new value that is xored with the
00334    * one of the previous iteration */
00335 
00336   for(offset = 0; offset < PWENT_MAX_LEN; offset += 9)
00337     {
00338       /* input name is ascii string, remove 8th bit on each byte, not significant */
00339       i1 = padded_name[offset + 0] & 0x7F;
00340       i2 = (uint64_t) (padded_name[offset + 1] & 0x7F) << 7;
00341       i3 = (uint64_t) (padded_name[offset + 2] & 0x7F) << 14;
00342       i4 = (uint64_t) (padded_name[offset + 3] & 0x7F) << 21;
00343       i5 = (uint64_t) (padded_name[offset + 4] & 0x7F) << 28;
00344       i6 = (uint64_t) (padded_name[offset + 5] & 0x7F) << 35;
00345       i7 = (uint64_t) (padded_name[offset + 6] & 0x7F) << 42;
00346       i8 = (uint64_t) (padded_name[offset + 7] & 0x7F) << 49;
00347       i9 = (uint64_t) (padded_name[offset + 8] & 0x7F) << 56;
00348 
00349       sum = (uint64_t) padded_name[offset + 0] +
00350           (uint64_t) padded_name[offset + 1] +
00351           (uint64_t) padded_name[offset + 2] +
00352           (uint64_t) padded_name[offset + 3] +
00353           (uint64_t) padded_name[offset + 4] +
00354           (uint64_t) padded_name[offset + 5] +
00355           (uint64_t) padded_name[offset + 6] +
00356           (uint64_t) padded_name[offset + 7] + (uint64_t) padded_name[offset + 8];
00357 
00358 #ifdef WITH_PRINTF_DEBUG_PWHASH_COMPUTE
00359       printf("|%llx |%llx |%llx |%llx |%llx |%llx |%llx |%llx |%llx | = ",
00360              i1, i2, i3, i4, i5, i6, i7, i8, i9);
00361 #endif
00362 
00363       /* Get xor combibation of all the 8h bit */
00364       l = (padded_name[offset + 0] & 0x80) ^
00365           (padded_name[offset + 1] & 0x80) ^
00366           (padded_name[offset + 2] & 0x80) ^
00367           (padded_name[offset + 3] & 0x80) ^
00368           (padded_name[offset + 4] & 0x80) ^
00369           (padded_name[offset + 5] & 0x80) ^
00370           (padded_name[offset + 6] & 0x80) ^
00371           (padded_name[offset + 7] & 0x80) ^ (padded_name[offset + 8] & 0x80);
00372 
00373       extract = (i1 ^ i2 ^ i3 ^ i4 ^ i5 ^ i6 ^ i7 ^ i8 ^ i9) | l;
00374 
00375 #ifdef WITH_PRINTF_DEBUG_PWHASH_COMPUTE
00376       printf("%llx ", extract);
00377 #endif
00378 
00379       computed_value ^= extract;
00380       computed_value ^= sum;
00381     }
00382 #ifdef WITH_PRINTF_DEBUG_PWHASH_COMPUTE
00383   printf("\n");
00384 #endif
00385 
00386   computed_value = (computed_value >> 32) + (computed_value & 0x00000000FFFFFFFFLL);
00387 
00388   *phashval = (uint32_t) computed_value;
00389 
00390   return ID_MAPPER_SUCCESS;
00391 }                               /* idmap_compute_hash_value */
00392 
00406 int idmap_add(hash_table_t * ht, char *key, unsigned int val)
00407 {
00408   hash_buffer_t buffkey;
00409   hash_buffer_t buffdata;
00410   int rc;
00411   unsigned long local_val = (unsigned long)val;
00412 
00413   if(ht == NULL || key == NULL)
00414     return ID_MAPPER_INVALID_ARGUMENT;
00415 
00416   if((buffkey.pdata = gsh_malloc(PWENT_MAX_LEN)) == NULL)
00417     return ID_MAPPER_INSERT_MALLOC_ERROR;
00418 
00419   /* Build the key */
00420   strncpy((buffkey.pdata), key, PWENT_MAX_LEN);
00421   buffkey.len = PWENT_MAX_LEN;
00422 
00423   /* Build the value */
00424   buffdata.pdata = (caddr_t) local_val;
00425   buffdata.len = sizeof(unsigned long);
00426   LogFullDebug(COMPONENT_IDMAPPER, "Adding the following principal->uid mapping: %s->%lu",
00427                (char *)buffkey.pdata, (unsigned long int)buffdata.pdata);
00428   rc = HashTable_Test_And_Set(ht, &buffkey, &buffdata,
00429                               HASHTABLE_SET_HOW_SET_NO_OVERWRITE);
00430 
00431   if(rc != HASHTABLE_SUCCESS && rc != HASHTABLE_ERROR_KEY_ALREADY_EXISTS)
00432     return ID_MAPPER_INSERT_MALLOC_ERROR;
00433 
00434   return ID_MAPPER_SUCCESS;
00435 }                               /* idmap_add */
00436 
00437 int namemap_add(hash_table_t * ht, unsigned int key, char *val)
00438 {
00439   hash_buffer_t buffkey;
00440   hash_buffer_t buffdata;
00441   int rc = 0;
00442   unsigned long local_key = (unsigned long)key;
00443 
00444   if(ht == NULL || val == NULL)
00445     return ID_MAPPER_INVALID_ARGUMENT;
00446 
00447   if((buffdata.pdata = gsh_malloc(PWENT_MAX_LEN)) == NULL)
00448     return ID_MAPPER_INSERT_MALLOC_ERROR;
00449 
00450   /* Build the data */
00451   strncpy((buffdata.pdata), val, PWENT_MAX_LEN);
00452   buffdata.len = PWENT_MAX_LEN;
00453 
00454   /* Build the value */
00455   buffkey.pdata = (caddr_t) local_key;
00456   buffkey.len = sizeof(unsigned int);
00457 
00458   LogFullDebug(COMPONENT_IDMAPPER, "Adding the following uid->principal mapping: %lu->%s",
00459                (unsigned long int)buffkey.pdata, (char *)buffdata.pdata);
00460   rc = HashTable_Test_And_Set(ht, &buffkey, &buffdata,
00461                               HASHTABLE_SET_HOW_SET_NO_OVERWRITE);
00462 
00463   if(rc != HASHTABLE_SUCCESS && rc != HASHTABLE_ERROR_KEY_ALREADY_EXISTS)
00464     return ID_MAPPER_INSERT_MALLOC_ERROR;
00465 
00466   return ID_MAPPER_SUCCESS;
00467 }                               /* idmap_add */
00468 
00469 int uidgidmap_add(unsigned int key, unsigned int value)
00470 {
00471   hash_buffer_t buffkey;
00472   hash_buffer_t buffdata;
00473   int rc = 0;
00474   unsigned long local_key = (unsigned long)key;
00475   unsigned long local_val = (unsigned long)value;
00476 
00477   /* Build keys and data, no storage is used there, caddr_t pointers are just charged */
00478   buffkey.pdata = (caddr_t) local_key;
00479   buffkey.len = sizeof(unsigned int);
00480 
00481   buffdata.pdata = (caddr_t) local_val;
00482   buffdata.len = sizeof(unsigned int);
00483 
00484   rc = HashTable_Test_And_Set(ht_uidgid, &buffkey, &buffdata,
00485                               HASHTABLE_SET_HOW_SET_OVERWRITE);
00486 
00487   if(rc != HASHTABLE_SUCCESS && rc != HASHTABLE_ERROR_KEY_ALREADY_EXISTS)
00488     return ID_MAPPER_INSERT_MALLOC_ERROR;
00489 
00490   return ID_MAPPER_SUCCESS;
00491 
00492 }                               /* uidgidmap_add */
00493 
00494 static int uidgidmap_free(hash_buffer_t key, hash_buffer_t val)
00495 {
00496     LogFullDebug(COMPONENT_IDMAPPER, "Freeing uid->gid mapping: %lu->%lu",
00497                  (unsigned long)key.pdata, (unsigned long)val.pdata);
00498   return 1;
00499 }
00500 
00501 int uidgidmap_clear()
00502 {
00503   int rc;
00504   LogInfo(COMPONENT_IDMAPPER, "Clearing all uid->gid map entries.");
00505   rc = HashTable_Delall(ht_uidgid, uidgidmap_free);
00506   if (rc != HASHTABLE_SUCCESS)
00507     return ID_MAPPER_FAIL;
00508   return ID_MAPPER_SUCCESS;
00509 }
00510 
00511 static int idmap_free(hash_buffer_t key, hash_buffer_t val)
00512 {
00513   if (val.pdata != NULL)
00514     LogFullDebug(COMPONENT_IDMAPPER, "Freeing uid->principal mapping: %p->%s",
00515                  key.pdata, (char *)val.pdata);
00516 
00517   /* key is just an integer caste to charptr */
00518   if (val.pdata != NULL)
00519     gsh_free(val.pdata);
00520   return 1;
00521 }
00522 
00523 int idmap_clear()
00524 {
00525   int rc;
00526   LogInfo(COMPONENT_IDMAPPER, "Clearing all principal->uid map entries.");
00527   rc = HashTable_Delall(ht_pwuid, idmap_free);
00528   if (rc != HASHTABLE_SUCCESS)
00529     return ID_MAPPER_FAIL;
00530   return ID_MAPPER_SUCCESS;
00531 }
00532 
00533 static int namemap_free(hash_buffer_t key, hash_buffer_t val)
00534 {
00535   if (key.pdata != NULL)
00536     LogFullDebug(COMPONENT_IDMAPPER, "Freeing principal->uid mapping: %s->%p",
00537                  (char *)key.pdata, val.pdata);
00538 
00539   /* val is just an integer caste to charptr */
00540   if (key.pdata != NULL)
00541     gsh_free(key.pdata);
00542   return 1;
00543 }
00544 
00545 int namemap_clear()
00546 {
00547   int rc;
00548   LogInfo(COMPONENT_IDMAPPER, "Clearing all uid->principal map entries.");
00549   rc = HashTable_Delall(ht_pwnam, namemap_free);
00550   if (rc != HASHTABLE_SUCCESS)
00551     return ID_MAPPER_FAIL;
00552   return ID_MAPPER_SUCCESS;
00553 }
00554 
00555 
00556 int uidmap_add(char *key, unsigned int val)
00557 {
00558   int rc1 = 0;
00559   int rc2 = 0;
00560 
00561   rc1 = idmap_add(ht_pwnam, key, val);
00562   rc2 = namemap_add(ht_pwuid, val, key);
00563 
00564   if(rc1 != ID_MAPPER_SUCCESS)
00565     return rc1;
00566   else if(rc2 != ID_MAPPER_SUCCESS)
00567     return rc2;
00568 
00569   return ID_MAPPER_SUCCESS;
00570 }                               /* uidmap_add */
00571 
00572 int unamemap_add(unsigned int key, char *val)
00573 {
00574   int rc1 = 0;
00575   int rc2 = 0;
00576 
00577   rc1 = namemap_add(ht_pwuid, key, val);
00578   rc2 = idmap_add(ht_pwnam, val, key);
00579 
00580   if(rc1 != ID_MAPPER_SUCCESS)
00581     return rc1;
00582   else if(rc2 != ID_MAPPER_SUCCESS)
00583     return rc2;
00584 
00585   return ID_MAPPER_SUCCESS;
00586 }                               /* unamemap_add */
00587 
00588 int gidmap_add(char *key, unsigned int val)
00589 {
00590   int rc1 = 0;
00591   int rc2 = 0;
00592 
00593   rc1 = idmap_add(ht_grnam, key, val);
00594   rc2 = namemap_add(ht_grgid, val, key);
00595 
00596   if(rc1 != ID_MAPPER_SUCCESS)
00597     return rc1;
00598   else if(rc2 != ID_MAPPER_SUCCESS)
00599     return rc2;
00600 
00601   return ID_MAPPER_SUCCESS;
00602 }                               /* gidmap_add */
00603 
00604 int gnamemap_add(unsigned int key, char *val)
00605 {
00606   int rc1 = 0;
00607   int rc2 = 0;
00608 
00609   rc1 = namemap_add(ht_grgid, key, val);
00610   rc2 = idmap_add(ht_grnam, val, key);
00611 
00612   if(rc1 != ID_MAPPER_SUCCESS)
00613     return rc1;
00614   else if(rc2 != ID_MAPPER_SUCCESS)
00615     return rc2;
00616 
00617   return ID_MAPPER_SUCCESS;
00618 }                               /* gnamemap_add */
00619 
00633 int idmap_get(hash_table_t * ht, char *key, unsigned long *pval)
00634 {
00635   hash_buffer_t buffkey;
00636   hash_buffer_t buffval;
00637   int status;
00638 
00639   if(ht == NULL || key == NULL || pval == NULL)
00640     return ID_MAPPER_INVALID_ARGUMENT;
00641 
00642   buffkey.pdata = (caddr_t) key;
00643   buffkey.len = PWENT_MAX_LEN;
00644 
00645   if(HashTable_Get(ht, &buffkey, &buffval) == HASHTABLE_SUCCESS)
00646     {
00647       *pval = (unsigned long)buffval.pdata;
00648 
00649       status = ID_MAPPER_SUCCESS;
00650     }
00651   else
00652     {
00653       status = ID_MAPPER_NOT_FOUND;
00654     }
00655 
00656   return status;
00657 }                               /* idmap_get */
00658 
00659 int namemap_get(hash_table_t * ht, unsigned int key, char *pval)
00660 {
00661   hash_buffer_t buffkey;
00662   hash_buffer_t buffval;
00663   int status;
00664   long local_key = (long)key;
00665 
00666   if(ht == NULL || pval == NULL)
00667     return ID_MAPPER_INVALID_ARGUMENT;
00668 
00669   buffkey.pdata = (caddr_t) local_key;
00670   buffkey.len = sizeof(unsigned long);
00671 
00672   if(HashTable_Get(ht, &buffkey, &buffval) == HASHTABLE_SUCCESS)
00673     {
00674       strncpy(pval, (char *)buffval.pdata, PWENT_MAX_LEN);
00675 
00676       status = ID_MAPPER_SUCCESS;
00677     }
00678   else
00679     {
00680       status = ID_MAPPER_NOT_FOUND;
00681     }
00682 
00683   return status;
00684 }                               /* idmap_get */
00685 
00686 int uidgidmap_get(unsigned int key, unsigned int *pval)
00687 {
00688   hash_buffer_t buffkey;
00689   hash_buffer_t buffval;
00690   int status;
00691   long local_key = (long)key;
00692 
00693   if(pval == NULL)
00694     return ID_MAPPER_INVALID_ARGUMENT;
00695 
00696   buffkey.pdata = (caddr_t) local_key;
00697   buffkey.len = sizeof(unsigned long);
00698 
00699   if(HashTable_Get(ht_uidgid, &buffkey, &buffval) == HASHTABLE_SUCCESS)
00700     {
00701       memcpy((char *)pval, &(buffval.pdata), sizeof(unsigned int));
00702       status = ID_MAPPER_SUCCESS;
00703     }
00704   else
00705     {
00706       /* WIth RPCSEC_GSS, it may be possible that 0 is not mapped to root */
00707       if(key == 0)
00708         {
00709           *pval = 0;
00710           status = ID_MAPPER_SUCCESS;
00711         }
00712       else
00713         status = ID_MAPPER_NOT_FOUND;
00714     }
00715 
00716   return status;
00717 
00718 }                               /* uidgidmap_get */
00719 
00720 int uidmap_get(char *key, unsigned long *pval)
00721 {
00722   return idmap_get(ht_pwnam, key, pval);
00723 }
00724 
00725 int unamemap_get(unsigned int key, char *val)
00726 {
00727   return namemap_get(ht_pwuid, key, val);
00728 }
00729 
00730 int gidmap_get(char *key, unsigned long *pval)
00731 {
00732   return idmap_get(ht_grnam, key, pval);
00733 }
00734 
00735 int gnamemap_get(unsigned int key, char *val)
00736 {
00737   return namemap_get(ht_grgid, key, val);
00738 }
00739 
00752 int idmap_remove(hash_table_t * ht, char *key)
00753 {
00754   hash_buffer_t buffkey, old_key;
00755   int status;
00756 
00757   if(ht == NULL || key == NULL)
00758     return ID_MAPPER_INVALID_ARGUMENT;
00759 
00760   buffkey.pdata = key;
00761   buffkey.len = PWENT_MAX_LEN;
00762 
00763   if(HashTable_Del(ht, &buffkey, &old_key, NULL) == HASHTABLE_SUCCESS)
00764     {
00765       status = ID_MAPPER_SUCCESS;
00766       gsh_free(old_key.pdata);
00767     }
00768   else
00769     {
00770       status = ID_MAPPER_NOT_FOUND;
00771     }
00772 
00773   return status;
00774 }                               /* idmap_remove */
00775 
00776 int namemap_remove(hash_table_t * ht, unsigned int key)
00777 {
00778   hash_buffer_t buffkey, old_data;
00779   int status;
00780   unsigned long local_key = key;
00781 
00782   if(ht == NULL)
00783     return ID_MAPPER_INVALID_ARGUMENT;
00784 
00785   buffkey.pdata = (void*) local_key;
00786   buffkey.len = sizeof(unsigned long);
00787 
00788   if(HashTable_Del(ht, &buffkey, NULL, &old_data) == HASHTABLE_SUCCESS)
00789     {
00790       status = ID_MAPPER_SUCCESS;
00791       gsh_free(old_data.pdata);
00792     }
00793   else
00794     {
00795       status = ID_MAPPER_NOT_FOUND;
00796     }
00797 
00798   return status;
00799 }                               /* idmap_remove */
00800 
00801 int uidgidmap_remove(unsigned int key)
00802 {
00803   hash_buffer_t buffkey, old_data;
00804   int status;
00805   unsigned long local_key = (unsigned long)key;
00806 
00807   buffkey.pdata = (caddr_t) local_key;
00808   buffkey.len = sizeof(unsigned long);
00809 
00810   if(HashTable_Del(ht_uidgid, &buffkey, NULL, &old_data) == HASHTABLE_SUCCESS)
00811     {
00812       status = ID_MAPPER_SUCCESS;
00813     }
00814   else
00815     {
00816       status = ID_MAPPER_NOT_FOUND;
00817     }
00818 
00819   return status;
00820 }                               /* uidgidmap_remove */
00821 
00822 int uidmap_remove(char *key)
00823 {
00824   return idmap_remove(ht_pwnam, key);
00825 }
00826 
00827 int unamemap_remove(unsigned int key)
00828 {
00829   return namemap_remove(ht_pwuid, key);
00830 }
00831 
00832 int gidmap_remove(char *key)
00833 {
00834   return idmap_remove(ht_grnam, key);
00835 }
00836 
00837 int gnamemap_remove(unsigned int key)
00838 {
00839   return namemap_remove(ht_grgid, key);
00840 }
00841 
00850 int idmap_populate(char *path, idmap_type_t maptype)
00851 {
00852   config_file_t config_file;
00853   config_item_t block;
00854   int var_max;
00855   int var_index;
00856   int err;
00857   char *key_name;
00858   char *key_value;
00859   char label[MAXNAMLEN];
00860   hash_table_t *ht = NULL;
00861   hash_table_t *ht_reverse = NULL;
00862   unsigned int value = 0;
00863   int rc = 0;
00864 
00865   config_file = config_ParseFile(path);
00866 
00867   if(!config_file)
00868     {
00869       LogCrit(COMPONENT_IDMAPPER,
00870               "Can't open file %s", path);
00871 
00872       return ID_MAPPER_INVALID_ARGUMENT;
00873     }
00874 
00875   switch (maptype)
00876     {
00877     case UIDMAP_TYPE:
00878       strncpy(label, CONF_LABEL_UID_MAPPER_TABLE, MAXNAMLEN);
00879       ht = ht_pwnam;
00880       ht_reverse = ht_pwuid;
00881       break;
00882 
00883     case GIDMAP_TYPE:
00884       strncpy(label, CONF_LABEL_GID_MAPPER_TABLE, MAXNAMLEN);
00885       ht = ht_grnam;
00886       ht_reverse = ht_grgid;
00887       break;
00888 
00889     default:
00890       /* Using incoherent value */
00891       return ID_MAPPER_INVALID_ARGUMENT;
00892       break;
00893     }
00894 
00895   /* Get the config BLOCK */
00896   if((block = config_FindItemByName(config_file, label)) == NULL)
00897     {
00898       LogCrit(COMPONENT_IDMAPPER,
00899               "Can't get label %s in file %s", label, path);
00900       return ID_MAPPER_INVALID_ARGUMENT;
00901     }
00902   else if(config_ItemType(block) != CONFIG_ITEM_BLOCK)
00903     {
00904       /* Expected to be a block */
00905       LogCrit(COMPONENT_IDMAPPER,
00906               "Label %s in file %s is expected to be a block", label, path);
00907       return ID_MAPPER_INVALID_ARGUMENT;
00908     }
00909 
00910   var_max = config_GetNbItems(block);
00911 
00912   for(var_index = 0; var_index < var_max; var_index++)
00913     {
00914       config_item_t item;
00915 
00916       item = config_GetItemByIndex(block, var_index);
00917 
00918       /* Get key's name */
00919       if((err = config_GetKeyValue(item, &key_name, &key_value)) != 0)
00920         {
00921           LogCrit(COMPONENT_IDMAPPER,
00922                   "Error reading key[%d] from section \"%s\" of configuration file.",
00923                   var_index, label);
00924           return ID_MAPPER_INVALID_ARGUMENT;
00925         }
00926 
00927       value = atoi(key_value);
00928 
00929       if((rc = idmap_add(ht, key_name, value)) != ID_MAPPER_SUCCESS)
00930         return rc;
00931 
00932       if((rc = namemap_add(ht_reverse, value, key_name)) != ID_MAPPER_SUCCESS)
00933         return rc;
00934 
00935     }
00936 
00937   /* HashTable_Log( ht ) ; */
00938   /* HashTable_Log( ht_reverse ) ; */
00939 
00940   return ID_MAPPER_SUCCESS;
00941 }                               /* idmap_populate_by_conf */
00942 
00958 void idmap_get_stats(idmap_type_t maptype, hash_stat_t * phstat,
00959                      hash_stat_t * phstat_reverse)
00960 {
00961   hash_table_t *ht = NULL;
00962   hash_table_t *ht_reverse = NULL;
00963 
00964   switch (maptype)
00965     {
00966     case UIDMAP_TYPE:
00967       ht = ht_pwnam;
00968       ht_reverse = ht_pwuid;
00969       break;
00970 
00971     case GIDMAP_TYPE:
00972       ht = ht_grnam;
00973       ht_reverse = ht_grgid;
00974       break;
00975 
00976     default:
00977       /* Using incoherent value */
00978       return;
00979       break;
00980     }
00981 
00982   HashTable_GetStats(ht, phstat);
00983   HashTable_GetStats(ht_reverse, phstat_reverse);
00984 
00985 }                               /* idmap_get_stats */