s3-winbindd: Move common code for LDAP id mapping to idmap_utils
authorChristof Schmitt <christof.schmitt@us.ibm.com>
Thu, 21 Feb 2013 19:30:48 +0000 (12:30 -0700)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 9 Mar 2013 05:30:22 +0000 (06:30 +0100)
idmap_ad and idmap_ldap use the same helper functions and the same
maximum query size. Move the code to idmap_utils so that it can be
shared by every module issuing LDAP queries.

Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source3/winbindd/idmap_ad.c
source3/winbindd/idmap_ldap.c
source3/winbindd/idmap_proto.h
source3/winbindd/idmap_util.c

index bfe7d4b91b5d19c956281cdeb40521520866d3d2..d6f31ba6e963dacbc1b8408a0872d7a5a90030ac 100644 (file)
@@ -41,7 +41,6 @@
 
 #define WINBIND_CCACHE_NAME "MEMORY:winbind_ccache"
 
-#define IDMAP_AD_MAX_IDS 30
 #define CHECK_ALLOC_DONE(mem) do { \
      if (!mem) { \
            DEBUG(0, ("Out of memory!\n")); \
@@ -249,40 +248,6 @@ static NTSTATUS idmap_ad_initialize(struct idmap_domain *dom)
        return NT_STATUS_OK;
 }
 
-/************************************************************************
- Search up to IDMAP_AD_MAX_IDS entries in maps for a match.
- ***********************************************************************/
-
-static struct id_map *find_map_by_id(struct id_map **maps, enum id_type type, uint32_t id)
-{
-       int i;
-
-       for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
-               if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
-                       return maps[i];
-               }
-       }
-
-       return NULL;    
-}
-
-/************************************************************************
- Search up to IDMAP_AD_MAX_IDS entries in maps for a match
- ***********************************************************************/
-
-static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
-{
-       int i;
-
-       for (i = 0; maps[i] && i<IDMAP_AD_MAX_IDS; i++) {
-               if (dom_sid_equal(maps[i]->sid, sid)) {
-                       return maps[i];
-               }
-       }
-
-       return NULL;    
-}
-
 /************************************************************************
  ***********************************************************************/
 
@@ -337,7 +302,7 @@ static NTSTATUS idmap_ad_unixids_to_sids(struct idmap_domain *dom, struct id_map
 
 again:
        bidx = idx;
-       for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
+       for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
                switch (ids[idx]->xid.type) {
                case ID_TYPE_UID:     
                        if ( ! u_filter) {
@@ -462,7 +427,7 @@ again:
                        continue;
                }
 
-               map = find_map_by_id(&ids[bidx], type, id);
+               map = idmap_find_map_by_id(&ids[bidx], type, id);
                if (!map) {
                        DEBUG(2, ("WARNING: couldn't match result with requested ID\n"));
                        continue;
@@ -567,7 +532,7 @@ again:
        CHECK_ALLOC_DONE(filter);
 
        bidx = idx;
-       for (i = 0; (i < IDMAP_AD_MAX_IDS) && ids[idx]; i++, idx++) {
+       for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
 
                ids[idx]->status = ID_UNKNOWN;
 
@@ -617,7 +582,7 @@ again:
                        continue;
                }
 
-               map = find_map_by_sid(&ids[bidx], &sid);
+               map = idmap_find_map_by_sid(&ids[bidx], &sid);
                if (!map) {
                        DEBUG(2, ("WARNING: couldn't match result with requested SID\n"));
                        continue;
index 0c58bf4236cefad1db0f877f585a187641cb25ff..03872e7ab1db1bee2f8a03c13aa2dcb972950240 100644 (file)
@@ -663,34 +663,10 @@ static NTSTATUS idmap_ldap_new_mapping(struct idmap_domain *dom, struct id_map *
        return ret;
 }
 
-
-/* max number of ids requested per batch query */
-#define IDMAP_LDAP_MAX_IDS 30
-
 /**********************************
  lookup a set of unix ids.
 **********************************/
 
-/* this function searches up to IDMAP_LDAP_MAX_IDS entries
- * in maps for a match */
-static struct id_map *find_map_by_id(struct id_map **maps,
-                                    enum id_type type,
-                                    uint32_t id)
-{
-       int i;
-
-       for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
-               if (maps[i] == NULL) { /* end of the run */
-                       return NULL;
-               }
-               if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
-                       return maps[i];
-               }
-       }
-
-       return NULL;
-}
-
 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
                                           struct id_map **ids)
 {
@@ -846,7 +822,7 @@ again:
                }
                TALLOC_FREE(tmp);
 
-               map = find_map_by_id(&ids[bidx], type, id);
+               map = idmap_find_map_by_id(&ids[bidx], type, id);
                if (!map) {
                        DEBUG(2, ("WARNING: couldn't match sid (%s) "
                                  "with requested ids\n", sidstr));
@@ -903,24 +879,6 @@ done:
  lookup a set of sids.
 **********************************/
 
-/* this function searches up to IDMAP_LDAP_MAX_IDS entries
- * in maps for a match */
-static struct id_map *find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
-{
-       int i;
-
-       for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
-               if (maps[i] == NULL) { /* end of the run */
-                       return NULL;
-               }
-               if (dom_sid_equal(maps[i]->sid, sid)) {
-                       return maps[i];
-               }
-       }
-
-       return NULL;
-}
-
 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
                                           struct id_map **ids)
 {
@@ -1053,7 +1011,7 @@ again:
                        continue;
                }
 
-               map = find_map_by_sid(&ids[bidx], &sid);
+               map = idmap_find_map_by_sid(&ids[bidx], &sid);
                if (!map) {
                        DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
                                  "in ids", sidstr));
index 892b258d6ab41e6191f6f4732d1b5495fe101217..a709807f68e81a58e0d57d1f97f253bfd6d7a758 100644 (file)
@@ -54,5 +54,11 @@ NTSTATUS idmap_tdb_init(void);
 NTSTATUS idmap_uid_to_sid(const char *domname, struct dom_sid *sid, uid_t uid);
 NTSTATUS idmap_gid_to_sid(const char *domname, struct dom_sid *sid, gid_t gid);
 bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom);
+struct id_map *idmap_find_map_by_id(struct id_map **maps, enum id_type type,
+                                   uint32_t id);
+struct id_map *idmap_find_map_by_sid(struct id_map **maps, struct dom_sid *sid);
+
+/* max number of ids requested per LDAP batch query */
+#define IDMAP_LDAP_MAX_IDS 30
 
 #endif /* _WINBINDD_IDMAP_PROTO_H_ */
index 9d9ada71999142011fe2e83135819a311bdc6e49..cbad91d151ef1fdd3469616a58835e445a303a7d 100644 (file)
@@ -172,3 +172,44 @@ bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom)
 
        return true;
 }
+
+/**
+ * Helper for unixids_to_sids: find entry by id in mapping array,
+ * search up to IDMAP_AD_MAX_IDS entries
+ */
+struct id_map *idmap_find_map_by_id(struct id_map **maps, enum id_type type,
+                                   uint32_t id)
+{
+       int i;
+
+       for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
+               if (maps[i] == NULL) { /* end of the run */
+                       return NULL;
+               }
+               if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
+                       return maps[i];
+               }
+       }
+
+       return NULL;
+}
+
+/**
+ * Helper for sids_to_unix_ids: find entry by SID in mapping array,
+ * search up to IDMAP_AD_MAX_IDS entries
+ */
+struct id_map *idmap_find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
+{
+       int i;
+
+       for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
+               if (maps[i] == NULL) { /* end of the run */
+                       return NULL;
+               }
+               if (dom_sid_equal(maps[i]->sid, sid)) {
+                       return maps[i];
+               }
+       }
+
+       return NULL;
+}