>From f0b56855435bdec1db8665a0e8e9b7fb603cab81 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Tue, 15 Jan 2013 22:19:36 -0500
Subject: [PATCH 11/11] Introduce IS_SUBDOMAIN() macro

Fixes https://fedorahosted.org/sssd/ticket/1766
---
 src/db/sysdb_search.c                        | 4 ++--
 src/providers/ipa/ipa_subdomains.c           | 4 ++--
 src/responder/common/responder_common.c      | 6 +++---
 src/responder/common/responder_get_domains.c | 2 +-
 src/responder/nss/nsssrv_cmd.c               | 6 +++---
 src/responder/pac/pacsrv_cmd.c               | 2 +-
 src/util/domain_info_utils.c                 | 2 +-
 src/util/util.h                              | 2 ++
 8 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
index 8ff29c37a798dbced6cbb7c3f3239175fbba032a..3226acc6846837ef0f6d491b8c9796e982de4dc0 100644
--- a/src/db/sysdb_search.c
+++ b/src/db/sysdb_search.c
@@ -55,7 +55,7 @@ int sysdb_getpwnam(TALLOC_CTX *mem_ctx,
 
     /* If this is a subomain we need to use fully qualified names for the
      * search as well by default */
-    if (domain->parent && domain->fqnames) {
+    if (IS_SUBDOMAIN(domain) && domain->fqnames) {
         ret = ENOMEM;
         src_name = talloc_asprintf(tmp_ctx, domain->names->fq_fmt,
                                    name, domain->name);
@@ -243,7 +243,7 @@ int sysdb_getgrnam(TALLOC_CTX *mem_ctx,
 
     /* If this is a subomain we need to use fully qualified names for the
      * search as well by default */
-    if (domain->parent && domain->fqnames) {
+    if (IS_SUBDOMAIN(domain) && domain->fqnames) {
         ret = ENOMEM;
         src_name = talloc_asprintf(tmp_ctx, domain->names->fq_fmt,
                                    name, domain->name);
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index deee153b8b90de6178b27af8f729109e16bcbfc6..28811ae7bd49f25d5787b55f218ef1c21df2758e 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -301,7 +301,7 @@ ipa_subdomains_write_mappings(struct sss_domain_info *domain)
     }
 
     for (dom = get_next_domain(domain, true);
-         dom && dom->parent; /* if we get back to a parent, stop */
+         dom && IS_SUBDOMAIN(dom); /* if we get back to a parent, stop */
          dom = get_next_domain(dom, false)) {
         ret = fprintf(fstream, ".%s = %s\n%s = %s\n",
                                dom->name, dom->realm, dom->name, dom->realm);
@@ -379,7 +379,7 @@ static errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
 
     /* check existing subdomains */
     for (dom = get_next_domain(domain, true);
-         dom && dom->parent;
+         dom && IS_SUBDOMAIN(dom); /* if we get back to a parent, stop */
          dom = get_next_domain(dom, false)) {
         for (c = 0; c < count; c++) {
             if (handled[c]) {
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
index 877181f8df1ad8ba8eaa89ea59b73e90ad8186bc..c7aead3e23e25f25023cbebdcc8f65a8f0275615 100644
--- a/src/responder/common/responder_common.c
+++ b/src/responder/common/responder_common.c
@@ -894,15 +894,15 @@ responder_get_domain(TALLOC_CTX *sd_mem_ctx, struct resp_ctx *rctx,
     struct sss_domain_info *ret_dom = NULL;
 
     for (dom = rctx->domains; dom; dom = get_next_domain(dom, true)) {
-        if (!dom->parent) {
+        if (!IS_SUBDOMAIN(dom)) {
             time_diff = now - dom->subdomains_last_checked.tv_sec;
         }
         if (strcasecmp(dom->name, name) == 0 ||
             (dom->flat_name != NULL &&
              strcasecmp(dom->flat_name, name) == 0)) {
             ret_dom = dom;
-            if (!dom->parent ||
-                (dom->parent && time_diff < rctx->domains_timeout)) {
+            if (!IS_SUBDOMAIN(dom) ||
+                (IS_SUBDOMAIN(dom) && time_diff < rctx->domains_timeout)) {
                 break;
             }
         }
diff --git a/src/responder/common/responder_get_domains.c b/src/responder/common/responder_get_domains.c
index b69875e1c37a52060605db5a4e17f75dc5fd0850..defa4a430530e6cc60980371c1311dea94b180e7 100644
--- a/src/responder/common/responder_get_domains.c
+++ b/src/responder/common/responder_get_domains.c
@@ -353,7 +353,7 @@ static errno_t check_last_request(struct resp_ctx *rctx, const char *hint)
 
     if (hint != NULL) {
         for (dom = rctx->domains; dom; dom = get_next_domain(dom, true)) {
-            if (dom->parent == NULL) {
+            if (!IS_SUBDOMAIN(dom)) {
                 diff = now - dom->subdomains_last_checked.tv_sec;
                 /* not a subdomain */
                 continue;
diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c
index 4180b1b1d0912193aadcf11d1dc8e05d704c8090..1ffcd3e94908b1e65ac006696a150e46eb18c9df 100644
--- a/src/responder/nss/nsssrv_cmd.c
+++ b/src/responder/nss/nsssrv_cmd.c
@@ -297,7 +297,7 @@ static int fill_pwent(struct sss_packet *packet,
     size_t dom_len = 0;
     int delim = 1;
     int i, ret, num, t;
-    bool add_domain = (dom->fqnames && (dom->parent == NULL));
+    bool add_domain = (!IS_SUBDOMAIN(dom) && dom->fqnames);
     const char *domain = dom->name;
     const char *namefmt;
     bool packet_initialized = false;
@@ -1902,7 +1902,7 @@ static int fill_members(struct sss_packet *packet,
     size_t blen;
 
     const char *domain = dom->name;
-    bool add_domain = (dom->fqnames && (dom->parent == NULL));
+    bool add_domain = (!IS_SUBDOMAIN(dom) && dom->fqnames);
 
     if (add_domain) {
         delim = 1;
@@ -2031,7 +2031,7 @@ static int fill_grent(struct sss_packet *packet,
     int i = 0;
     int ret, num, memnum;
     size_t rzero, rsize;
-    bool add_domain = (dom->fqnames && (dom->parent == NULL));
+    bool add_domain = (!IS_SUBDOMAIN(dom) && dom->fqnames);
     const char *domain = dom->name;
     const char *namefmt;
     TALLOC_CTX *tmp_ctx = NULL;
diff --git a/src/responder/pac/pacsrv_cmd.c b/src/responder/pac/pacsrv_cmd.c
index aeb41847c0ef2ce201cd9e316eb607d43f5fc641..a8191bb9eaddf88a5bfab04d4b75e75884c2210b 100644
--- a/src/responder/pac/pacsrv_cmd.c
+++ b/src/responder/pac/pacsrv_cmd.c
@@ -453,7 +453,7 @@ struct tevent_req *pac_save_memberships_send(struct pac_req_ctx *pr_ctx)
     state->pr_ctx = pr_ctx;
 
     /* Remote users are members of local groups */
-    if (pr_ctx->dom->parent != NULL) {
+    if (IS_SUBDOMAIN(pr_ctx->dom)) {
         state->group_dom = pr_ctx->dom->parent;
     } else {
         state->group_dom = pr_ctx->dom;
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
index 6db7e157a3e6d1ce84c8dcb0ffc0b45552c6603a..d8ca55da141e7f91d58b18921696f8ac490c92f6 100644
--- a/src/util/domain_info_utils.c
+++ b/src/util/domain_info_utils.c
@@ -33,7 +33,7 @@ struct sss_domain_info *get_next_domain(struct sss_domain_info *domain,
             dom = dom->subdomains;
         } else if (dom->next) {
             dom = dom->next;
-        } else if (descend && dom->parent) {
+        } else if (descend && IS_SUBDOMAIN(dom)) {
             dom = dom->parent->next;
         } else {
             return NULL;
diff --git a/src/util/util.h b/src/util/util.h
index 7697dbb51bca4bc158528105cfd648717352579a..7026f0b934911cd6a4c069411c279d5e8efe764c 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -585,6 +585,8 @@ errno_t sssd_domain_init(TALLOC_CTX *mem_ctx,
                          const char *db_path,
                          struct sss_domain_info **_domain);
 
+#define IS_SUBDOMAIN(dom) ((dom)->parent != NULL)
+
 /* from util_lock.c */
 errno_t sss_br_lock_file(int fd, size_t start, size_t len,
                          int num_tries, useconds_t wait);
-- 
1.8.1.2

