Move the uid2sid cache to the parent winbind process
[samba.git] / source3 / winbindd / idmap_cache.c
index a3207663e4f3336bc94dd6320a0498dc2e2abef2..027ce3b6195387382534f34870063aa84d28aa4e 100644 (file)
@@ -301,7 +301,7 @@ failed:
        return NT_STATUS_INTERNAL_DB_CORRUPTION;
 }
 
-/* search the cache for the SID an return a mapping if found *
+/* search the cahce for the SID an return a mapping if found *
  *
  * 4 cases are possible
  *
@@ -343,8 +343,7 @@ NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
        t = strtol((const char *)databuf.dptr, &endptr, 10);
 
        if ((endptr == NULL) || (*endptr != '/')) {
-               DEBUG(2, ("Invalid idmap cache data format: %s\n",
-                         (const char *)databuf.dptr));
+               DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf.dptr));
                /* remove the entry */
                tdb_delete_bystring(cache->tdb, sidkey);
                ret = NT_STATUS_NONE_MAPPED;
@@ -396,22 +395,22 @@ NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
 
 
        /* Check for valid or expired cache hits */
-       if (t <= now) {
+               if (t <= now) {
                /* We're expired. Return not mapped */
-               ret = NT_STATUS_NONE_MAPPED;
-       } else {
-               /* this is not mapped as it was a negative cache hit */
-               id->status = ID_UNMAPPED;
-               ret = NT_STATUS_OK;
-       }
-
+                       ret = NT_STATUS_NONE_MAPPED;
+               } else {
+                       /* this is not mapped as it was a negative cache hit */
+                       id->status = ID_UNMAPPED;
+                       ret = NT_STATUS_OK;
+               }
+       
 done:
        SAFE_FREE(databuf.dptr);
        talloc_free(sidkey);
        return ret;
 }
 
-/* search the cache for the ID an return a mapping if found *
+/* search the cahce for the ID an return a mapping if found *
  *
  * 4 cases are possible
  *
@@ -453,8 +452,7 @@ NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
        t = strtol((const char *)databuf.dptr, &endptr, 10);
 
        if ((endptr == NULL) || (*endptr != '/')) {
-               DEBUG(2, ("Invalid idmap cache data format: %s\n",
-                         (const char *)databuf.dptr));
+               DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf.dptr));
                /* remove the entry */
                tdb_delete_bystring(cache->tdb, idkey);
                ret = NT_STATUS_NONE_MAPPED;
@@ -508,14 +506,14 @@ NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
 
        /* Process the negative cache hit */
 
-       if (t <= now) {
+               if (t <= now) {
                /* We're expired.  Return not mapped */
-               ret = NT_STATUS_NONE_MAPPED;
-       } else {
+                       ret = NT_STATUS_NONE_MAPPED;
+               } else {
                /* this is not mapped is it was a negative cache hit */
-               id->status = ID_UNMAPPED;
-               ret = NT_STATUS_OK;
-       }
+                       id->status = ID_UNMAPPED;
+                       ret = NT_STATUS_OK;
+               }
 
 done:
        SAFE_FREE(databuf.dptr);
@@ -523,3 +521,83 @@ done:
        return ret;
 }
 
+bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
+                             bool *expired)
+{
+       fstring sidstr;
+       char *key;
+       char *value;
+       char *endptr;
+       time_t timeout;
+       uid_t uid;
+       bool ret;
+
+       key = talloc_asprintf(talloc_tos(), "IDMAP/SID2UID/%s",
+                             sid_to_fstring(sidstr, sid));
+       if (key == NULL) {
+               return false;
+       }
+       ret = gencache_get(key, &value, &timeout);
+       TALLOC_FREE(key);
+       if (!ret) {
+               return false;
+       }
+       uid = strtol(value, &endptr, 10);
+       ret = (*endptr == '\0');
+       SAFE_FREE(value);
+       if (ret) {
+               *puid = uid;
+               *expired = (timeout <= time(NULL));
+       }
+       return ret;
+}
+
+bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
+{
+       char *key;
+       char *value;
+       time_t timeout;
+       bool ret;
+
+       key = talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid);
+       if (key == NULL) {
+               return false;
+       }
+       ret = gencache_get(key, &value, &timeout);
+       TALLOC_FREE(key);
+       if (!ret) {
+               return false;
+       }
+       ZERO_STRUCTP(sid);
+       ret = string_to_sid(sid, value);
+       SAFE_FREE(value);
+       if (ret) {
+               *expired = (timeout <= time(NULL));
+       }
+       return ret;
+}
+
+void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
+{
+       time_t now = time(NULL);
+       time_t timeout;
+       fstring sidstr, key, value;
+
+       if (!is_null_sid(sid)) {
+               fstr_sprintf(key, "IDMAP/SID2UID/%s",
+                            sid_to_fstring(sidstr, sid));
+               fstr_sprintf(value, "%d", (int)uid);
+               timeout = (uid == -1)
+                       ? lp_idmap_negative_cache_time()
+                       : lp_idmap_cache_time();
+               gencache_set(key, value, now + timeout);
+       }
+       if (uid != -1) {
+               fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)uid);
+               sid_to_fstring(value, sid);
+               timeout = is_null_sid(sid)
+                       ? lp_idmap_negative_cache_time()
+                       : lp_idmap_cache_time();
+               gencache_set(key, value, now + timeout);
+       }
+}