s3:passdb: fix bug #6509: use gid (not uid) cache in fetch_gid_from_cache().
[ira/wip.git] / source3 / passdb / lookup_sid.c
index 4e11bb84d873490200175aa10c9843da4e2b760f..112225d5049de49d74cb2ff5383f5048cc58bbda 100644 (file)
@@ -468,12 +468,15 @@ static bool lookup_rids(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid,
                   sid_string_dbg(domain_sid)));
 
        if (num_rids) {
-               *names = TALLOC_ARRAY(mem_ctx, const char *, num_rids);
+               *names = TALLOC_ZERO_ARRAY(mem_ctx, const char *, num_rids);
                *types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_rids);
 
                if ((*names == NULL) || (*types == NULL)) {
                        return false;
                }
+
+               for (i = 0; i < num_rids; i++)
+                       (*types)[i] = SID_NAME_UNKNOWN;
        } else {
                *names = NULL;
                *types = NULL;
@@ -746,7 +749,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
        }
 
        dom_infos = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_dom_info,
-                                     MAX_REF_DOMAINS);
+                                     LSA_REF_DOMAIN_LIST_MULTIPLIER);
        if (dom_infos == NULL) {
                result = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -804,7 +807,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
                } else {
                        /* This is a normal SID with rid component */
                        if (!sid_split_rid(&sid, &rid)) {
-                               result = NT_STATUS_INVALID_PARAMETER;
+                               result = NT_STATUS_INVALID_SID;
                                goto fail;
                        }
                }
@@ -816,7 +819,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
                        continue;
                }
 
-               for (j=0; j<MAX_REF_DOMAINS; j++) {
+               for (j=0; j<LSA_REF_DOMAIN_LIST_MULTIPLIER; j++) {
                        if (!dom_infos[j].valid) {
                                break;
                        }
@@ -825,7 +828,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
                        }
                }
 
-               if (j == MAX_REF_DOMAINS) {
+               if (j == LSA_REF_DOMAIN_LIST_MULTIPLIER) {
                        /* TODO: What's the right error message here? */
                        result = NT_STATUS_NONE_MAPPED;
                        goto fail;
@@ -869,7 +872,7 @@ NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, int num_sids,
 
        /* Iterate over the domains found */
 
-       for (i=0; i<MAX_REF_DOMAINS; i++) {
+       for (i=0; i<LSA_REF_DOMAIN_LIST_MULTIPLIER; i++) {
                uint32_t *rids;
                const char *domain_name = NULL;
                const char **names;
@@ -1014,7 +1017,7 @@ static bool fetch_sid_from_uid_cache(DOM_SID *psid, uid_t uid)
 
        memcpy(psid, cache_value.data, MIN(sizeof(*psid), cache_value.length));
        SMB_ASSERT(cache_value.length >= offsetof(struct dom_sid, id_auth));
-       SMB_ASSERT(cache_value.length == ndr_size_dom_sid(psid, 0));
+       SMB_ASSERT(cache_value.length == ndr_size_dom_sid(psid, NULL, 0));
 
        return true;
 }
@@ -1028,7 +1031,7 @@ static bool fetch_uid_from_cache( uid_t *puid, const DOM_SID *psid )
        DATA_BLOB cache_value;
 
        if (!memcache_lookup(NULL, SID_UID_CACHE,
-                            data_blob_const(psid, ndr_size_dom_sid(psid, 0)),
+                            data_blob_const(psid, ndr_size_dom_sid(psid, NULL, 0)),
                             &cache_value)) {
                return false;
        }
@@ -1046,11 +1049,11 @@ static bool fetch_uid_from_cache( uid_t *puid, const DOM_SID *psid )
 void store_uid_sid_cache(const DOM_SID *psid, uid_t uid)
 {
        memcache_add(NULL, SID_UID_CACHE,
-                    data_blob_const(psid, ndr_size_dom_sid(psid, 0)),
+                    data_blob_const(psid, ndr_size_dom_sid(psid, NULL, 0)),
                     data_blob_const(&uid, sizeof(uid)));
        memcache_add(NULL, UID_SID_CACHE,
                     data_blob_const(&uid, sizeof(uid)),
-                    data_blob_const(psid, ndr_size_dom_sid(psid, 0)));
+                    data_blob_const(psid, ndr_size_dom_sid(psid, NULL, 0)));
 }
 
 /*****************************************************************
@@ -1069,7 +1072,7 @@ static bool fetch_sid_from_gid_cache(DOM_SID *psid, gid_t gid)
 
        memcpy(psid, cache_value.data, MIN(sizeof(*psid), cache_value.length));
        SMB_ASSERT(cache_value.length >= offsetof(struct dom_sid, id_auth));
-       SMB_ASSERT(cache_value.length == ndr_size_dom_sid(psid, 0));
+       SMB_ASSERT(cache_value.length == ndr_size_dom_sid(psid, NULL, 0));
 
        return true;
 }
@@ -1082,8 +1085,8 @@ static bool fetch_gid_from_cache(gid_t *pgid, const DOM_SID *psid)
 {
        DATA_BLOB cache_value;
 
-       if (!memcache_lookup(NULL, SID_UID_CACHE,
-                            data_blob_const(psid, ndr_size_dom_sid(psid, 0)),
+       if (!memcache_lookup(NULL, SID_GID_CACHE,
+                            data_blob_const(psid, ndr_size_dom_sid(psid, NULL, 0)),
                             &cache_value)) {
                return false;
        }
@@ -1101,15 +1104,15 @@ static bool fetch_gid_from_cache(gid_t *pgid, const DOM_SID *psid)
 void store_gid_sid_cache(const DOM_SID *psid, gid_t gid)
 {
        memcache_add(NULL, SID_GID_CACHE,
-                    data_blob_const(psid, ndr_size_dom_sid(psid, 0)),
+                    data_blob_const(psid, ndr_size_dom_sid(psid, NULL, 0)),
                     data_blob_const(&gid, sizeof(gid)));
        memcache_add(NULL, GID_SID_CACHE,
                     data_blob_const(&gid, sizeof(gid)),
-                    data_blob_const(psid, ndr_size_dom_sid(psid, 0)));
+                    data_blob_const(psid, ndr_size_dom_sid(psid, NULL, 0)));
 }
 
 /*****************************************************************
- uid_t->SID conversion used if winbind is not around
+ *THE LEGACY* convert uid_t to SID function.
 *****************************************************************/  
 
 static void legacy_uid_to_sid(DOM_SID *psid, uid_t uid)
@@ -1143,7 +1146,7 @@ static void legacy_uid_to_sid(DOM_SID *psid, uid_t uid)
 }
 
 /*****************************************************************
- gid_t->SID conversion used if winbind is not around
+ *THE LEGACY* convert gid_t to SID function.
 *****************************************************************/  
 
 static void legacy_gid_to_sid(DOM_SID *psid, gid_t gid)
@@ -1174,7 +1177,7 @@ static void legacy_gid_to_sid(DOM_SID *psid, gid_t gid)
 }
 
 /*****************************************************************
- SID->uid_t conversion used if winbind is not around
+ *THE LEGACY* convert SID to uid function.
 *****************************************************************/  
 
 static bool legacy_sid_to_uid(const DOM_SID *psid, uid_t *puid)
@@ -1217,7 +1220,8 @@ done:
 }
 
 /*****************************************************************
- SID->gid_t conversion used if winbind is not around
+ *THE LEGACY* convert SID to gid function.
+ Group mapping is used for gids that maps to Wellknown SIDs
 *****************************************************************/  
 
 static bool legacy_sid_to_gid(const DOM_SID *psid, gid_t *pgid)
@@ -1280,25 +1284,49 @@ static bool legacy_sid_to_gid(const DOM_SID *psid, gid_t *pgid)
 }
 
 /*****************************************************************
- uid_t->SID conversion
+ *THE CANONICAL* convert uid_t to SID function.
 *****************************************************************/  
 
 void uid_to_sid(DOM_SID *psid, uid_t uid)
 {
+       bool expired = true;
+       bool ret;
        ZERO_STRUCTP(psid);
 
        if (fetch_sid_from_uid_cache(psid, uid))
                return;
 
-       if (!winbind_uid_to_sid(psid, uid)) {
-               if (!winbind_ping()) {
+       /* Check the winbindd cache directly. */
+       ret = idmap_cache_find_uid2sid(uid, psid, &expired);
+
+       if (ret && !expired && is_null_sid(psid)) {
+               /*
+                * Negative cache entry, we already asked.
+                * do legacy.
+                */
+               legacy_uid_to_sid(psid, uid);
+               return;
+       }
+
+       if (!ret || expired) {
+               /* Not in cache. Ask winbindd. */
+               if (!winbind_uid_to_sid(psid, uid)) {
+                       /*
+                        * We shouldn't return the NULL SID
+                        * here if winbind was running and
+                        * couldn't map, as winbind will have
+                        * added a negative entry that will
+                        * cause us to go though the
+                        * legacy_uid_to_sid()
+                        * function anyway in the case above
+                        * the next time we ask.
+                        */
+                       DEBUG(5, ("uid_to_sid: winbind failed to find a sid "
+                                 "for uid %u\n", (unsigned int)uid));
+
                        legacy_uid_to_sid(psid, uid);
                        return;
                }
-
-               DEBUG(5, ("uid_to_sid: winbind failed to find a sid for uid %u\n",
-                       uid));
-               return;
        }
 
        DEBUG(10,("uid %u -> sid %s\n", (unsigned int)uid,
@@ -1309,40 +1337,67 @@ void uid_to_sid(DOM_SID *psid, uid_t uid)
 }
 
 /*****************************************************************
- gid_t->SID conversion
+ *THE CANONICAL* convert gid_t to SID function.
 *****************************************************************/  
 
 void gid_to_sid(DOM_SID *psid, gid_t gid)
 {
+       bool expired = true;
+       bool ret;
        ZERO_STRUCTP(psid);
 
        if (fetch_sid_from_gid_cache(psid, gid))
                return;
 
-       if (!winbind_gid_to_sid(psid, gid)) {
-               if (!winbind_ping()) {
+       /* Check the winbindd cache directly. */
+       ret = idmap_cache_find_gid2sid(gid, psid, &expired);
+
+       if (ret && !expired && is_null_sid(psid)) {
+               /*
+                * Negative cache entry, we already asked.
+                * do legacy.
+                */
+               legacy_gid_to_sid(psid, gid);
+               return;
+       }
+
+       if (!ret || expired) {
+               /* Not in cache. Ask winbindd. */
+               if (!winbind_gid_to_sid(psid, gid)) {
+                       /*
+                        * We shouldn't return the NULL SID
+                        * here if winbind was running and
+                        * couldn't map, as winbind will have
+                        * added a negative entry that will
+                        * cause us to go though the
+                        * legacy_gid_to_sid()
+                        * function anyway in the case above
+                        * the next time we ask.
+                        */
+                       DEBUG(5, ("gid_to_sid: winbind failed to find a sid "
+                                 "for gid %u\n", (unsigned int)gid));
+
                        legacy_gid_to_sid(psid, gid);
                        return;
                }
-
-               DEBUG(5, ("gid_to_sid: winbind failed to find a sid for gid %u\n",
-                       gid));
-               return;
        }
 
        DEBUG(10,("gid %u -> sid %s\n", (unsigned int)gid,
                  sid_string_dbg(psid)));
-       
+
        store_gid_sid_cache(psid, gid);
        return;
 }
 
 /*****************************************************************
- SID->uid_t conversion
+ *THE CANONICAL* convert SID to uid function.
 *****************************************************************/  
 
 bool sid_to_uid(const DOM_SID *psid, uid_t *puid)
 {
+       bool expired = true;
+       bool ret;
+       uint32 rid;
        gid_t gid;
 
        if (fetch_uid_from_cache(puid, psid))
@@ -1354,27 +1409,35 @@ bool sid_to_uid(const DOM_SID *psid, uid_t *puid)
 
        /* Optimize for the Unix Users Domain
         * as the conversion is straightforward */
-
-       if (sid_check_is_in_unix_users(psid)) {
-               uint32_t rid;
-
-               sid_peek_rid(psid, &rid);
-               *puid = (uid_t)rid;
+       if (sid_peek_check_rid(&global_sid_Unix_Users, psid, &rid)) {
+               uid_t uid = rid;
+               *puid = uid;
 
                /* return here, don't cache */
-               DEBUG(10, ("sid %s -> uid %u\n", sid_string_dbg(psid),
-                          (unsigned int)rid));
+               DEBUG(10,("sid %s -> uid %u\n", sid_string_dbg(psid),
+                       (unsigned int)*puid ));
                return true;
        }
 
-       if (!winbind_sid_to_uid(puid, psid)) {
-               if (!winbind_ping()) {
+       /* Check the winbindd cache directly. */
+       ret = idmap_cache_find_sid2uid(psid, puid, &expired);
+
+       if (ret && !expired && (*puid == (uid_t)-1)) {
+               /*
+                * Negative cache entry, we already asked.
+                * do legacy.
+                */
+               return legacy_sid_to_uid(psid, puid);
+       }
+
+       if (!ret || expired) {
+               /* Not in cache. Ask winbindd. */
+               if (!winbind_sid_to_uid(puid, psid)) {
+                       DEBUG(5, ("winbind failed to find a uid for sid %s\n",
+                                 sid_string_dbg(psid)));
+                       /* winbind failed. do legacy */
                        return legacy_sid_to_uid(psid, puid);
                }
-
-               DEBUG(5, ("winbind failed to find a uid for sid %s\n",
-                         sid_string_dbg(psid)));
-               return false;
        }
 
        /* TODO: Here would be the place to allocate both a gid and a uid for
@@ -1388,11 +1451,14 @@ bool sid_to_uid(const DOM_SID *psid, uid_t *puid)
 }
 
 /*****************************************************************
- SID->gid_t conversion
+ *THE CANONICAL* convert SID to gid function.
+ Group mapping is used for gids that maps to Wellknown SIDs
 *****************************************************************/  
 
 bool sid_to_gid(const DOM_SID *psid, gid_t *pgid)
 {
+       bool expired = true;
+       bool ret;
        uint32 rid;
        uid_t uid;
 
@@ -1404,37 +1470,44 @@ bool sid_to_gid(const DOM_SID *psid, gid_t *pgid)
 
        /* Optimize for the Unix Groups Domain
         * as the conversion is straightforward */
-
-       if (sid_check_is_in_unix_groups(psid)) {
-               uint32_t rid;
-
-               sid_peek_rid(psid, &rid);
-               *pgid = (gid_t)rid;
+       if (sid_peek_check_rid(&global_sid_Unix_Groups, psid, &rid)) {
+               gid_t gid = rid;
+               *pgid = gid;
 
                /* return here, don't cache */
-               DEBUG(10, ("sid %s -> gid %u\n", sid_string_dbg(psid),
-                          (unsigned int)rid));
+               DEBUG(10,("sid %s -> gid %u\n", sid_string_dbg(psid),
+                       (unsigned int)*pgid ));
                return true;
        }
 
-       /* Ask winbindd if it can map this sid to a gid.
-        * (Idmap will check it is a valid SID and of the right type) */
+       /* Check the winbindd cache directly. */
+       ret = idmap_cache_find_sid2gid(psid, pgid, &expired);
+
+       if (ret && !expired && (*pgid == (gid_t)-1)) {
+               /*
+                * Negative cache entry, we already asked.
+                * do legacy.
+                */
+               return legacy_sid_to_gid(psid, pgid);
+       }
+
+       if (!ret || expired) {
+               /* Not in cache or negative. Ask winbindd. */
+               /* Ask winbindd if it can map this sid to a gid.
+                * (Idmap will check it is a valid SID and of the right type) */
+
+               if ( !winbind_sid_to_gid(pgid, psid) ) {
 
-       if ( !winbind_sid_to_gid(pgid, psid) ) {
-               if (!winbind_ping()) {
+                       DEBUG(10,("winbind failed to find a gid for sid %s\n",
+                                 sid_string_dbg(psid)));
+                       /* winbind failed. do legacy */
                        return legacy_sid_to_gid(psid, pgid);
                }
-
-               DEBUG(10,("winbind failed to find a gid for sid %s\n",
-                         sid_string_dbg(psid)));
-               return false;
        }
 
        DEBUG(10,("sid %s -> gid %u\n", sid_string_dbg(psid),
                  (unsigned int)*pgid ));
 
        store_gid_sid_cache(psid, *pgid);
-       
        return true;
 }
-