From 9c5adf94cc6e41d0a7a5a770c3d45ee014532889 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 2 Sep 2013 15:15:59 +0200
Subject: [PATCH 4/8] util: add find_subdomain_by_sid()

This function takes domain SID (doesn't have the last component)
or object SID (have all components) and returns subdomain.

The subdomain is found by comparing domain->domainid with the SID.

E.g.
domain SID: S-1-5-21-3940105347-3434501867-2690409756
object SID: S-1-5-21-3940105347-3434501867-2690409756-513

Resolves:
https://fedorahosted.org/sssd/ticket/2034
---
 src/util/domain_info_utils.c | 33 +++++++++++++++++++++++++++++++++
 src/util/util.h              |  2 ++
 2 files changed, 35 insertions(+)

diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
index 8b03e9a53fad8614ab0dccf14dceb30232ad84d4..f9d9057a811f3e08c451d7f6b44bc14202559962 100644
--- a/src/util/domain_info_utils.c
+++ b/src/util/domain_info_utils.c
@@ -95,6 +95,39 @@ struct sss_domain_info *find_subdomain_by_name(struct sss_domain_info *domain,
     return NULL;
 }
 
+struct sss_domain_info *find_subdomain_by_sid(struct sss_domain_info *domain,
+                                              const char *sid)
+{
+    struct sss_domain_info *dom = domain;
+    size_t sid_len = strlen(sid);
+    size_t dom_sid_len;
+
+    while (dom && dom->disabled) {
+        dom = get_next_domain(dom, true);
+    }
+
+    while (dom) {
+        dom_sid_len = strlen(dom->domain_id);
+
+        if (strncasecmp(dom->domain_id, sid, dom_sid_len) == 0) {
+            if (dom_sid_len == sid_len) {
+                /* sid is domain sid */
+                return dom;
+            }
+
+            /* sid is object sid, check if domain sid is align with
+             * sid first subauthority component */
+            if (sid[dom_sid_len] == '-') {
+                return dom;
+            }
+        }
+
+        dom = get_next_domain(dom, true);
+    }
+
+    return NULL;
+}
+
 struct sss_domain_info *new_subdomain(TALLOC_CTX *mem_ctx,
                                       struct sss_domain_info *parent,
                                       const char *name,
diff --git a/src/util/util.h b/src/util/util.h
index c2c31f5d50e86315a3e751daf49cf594550bc7c6..2a77df1d811afbf66ae03d9cc1891f66e8e5a0ec 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -539,6 +539,8 @@ struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
 struct sss_domain_info *find_subdomain_by_name(struct sss_domain_info *domain,
                                                const char *name,
                                                bool match_any);
+struct sss_domain_info *find_subdomain_by_sid(struct sss_domain_info *domain,
+                                              const char *sid);
 bool subdomain_enumerates(struct sss_domain_info *parent,
                           const char *sd_name);
 
-- 
1.7.11.7

