dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 3f7b69c3af7b810443a94a1004d41b87e5ca79d8 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Mon, 20 Jun 2016 16:11:11 +0200
Subject: [PATCH 55/62] utils: add is_email_from_domain()

Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
(cherry picked from commit 04d4c4d45f3942a813b7f772737f801f877f4e64)
---
 src/tests/cmocka/test_utils.c | 21 +++++++++++++++++++++
 src/util/domain_info_utils.c  | 27 +++++++++++++++++++++++++++
 src/util/util.h               |  1 +
 3 files changed, 49 insertions(+)

diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
index 5349accc5c6b55f7d725bfdeaf318b90289880dd..09273692968b544aed88de00c747e85ff0b6695c 100644
--- a/src/tests/cmocka/test_utils.c
+++ b/src/tests/cmocka/test_utils.c
@@ -1842,6 +1842,25 @@ static void test_sss_get_domain_mappings_content(void **state)
      * capaths might not be as expected. */
 }
 
+static void test_is_email_from_domain(void **state)
+{
+    struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
+                                                      struct dom_list_test_ctx);
+    struct sss_domain_info *d;
+
+    d = find_domain_by_name(test_ctx->dom_list, "name_0.dom", false);
+    assert_non_null(d);
+
+    assert_false(is_email_from_domain(NULL, NULL));
+    assert_false(is_email_from_domain("hello", NULL));
+    assert_false(is_email_from_domain(NULL, d));
+    assert_false(is_email_from_domain("hello", d));
+    assert_false(is_email_from_domain("hello@hello", d));
+
+    assert_true(is_email_from_domain("hello@name_0.dom", d));
+    assert_true(is_email_from_domain("hello@NaMe_0.DoM", d));
+}
+
 int main(int argc, const char *argv[])
 {
     poptContext pc;
@@ -1870,6 +1889,8 @@ int main(int argc, const char *argv[])
                                         setup_dom_list, teardown_dom_list),
         cmocka_unit_test_setup_teardown(test_find_domain_by_name_disabled,
                                         setup_dom_list, teardown_dom_list),
+        cmocka_unit_test_setup_teardown(test_is_email_from_domain,
+                                        setup_dom_list, teardown_dom_list),
 
         cmocka_unit_test_setup_teardown(test_sss_names_init,
                                         confdb_test_setup,
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
index 587a6b993d2bd70662df8e0b0d5963fa00c84cf8..0feda148bd44b9cefc43c094ddc5a72820412322 100644
--- a/src/util/domain_info_utils.c
+++ b/src/util/domain_info_utils.c
@@ -824,3 +824,30 @@ void sss_domain_set_state(struct sss_domain_info *dom,
 {
     dom->state = state;
 }
+
+bool is_email_from_domain(const char *email, struct sss_domain_info *dom)
+{
+    const char *p;
+
+    if (email == NULL || dom == NULL) {
+        return false;
+    }
+
+    p = strchr(email, '@');
+    if (p == NULL) {
+        DEBUG(SSSDBG_TRACE_ALL,
+              "Input [%s] does not look like an email address.\n", email);
+        return false;
+    }
+
+    if (strcasecmp(p+1, dom->name) == 0) {
+        DEBUG(SSSDBG_TRACE_ALL, "Email [%s] is from domain [%s].\n", email,
+                                                                     dom->name);
+        return true;
+    }
+
+    DEBUG(SSSDBG_TRACE_ALL, "Email [%s] is not from domain [%s].\n", email,
+                                                                     dom->name);
+
+    return false;
+}
diff --git a/src/util/util.h b/src/util/util.h
index 122be90b967fb7793adaff95f3754d7a199fcf48..4449315f8b1a79ec915bc340b46188c440a27fa3 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -514,6 +514,7 @@ struct sss_domain_info *find_domain_by_sid(struct sss_domain_info *domain,
 enum sss_domain_state sss_domain_get_state(struct sss_domain_info *dom);
 void sss_domain_set_state(struct sss_domain_info *dom,
                           enum sss_domain_state state);
+bool is_email_from_domain(const char *email, struct sss_domain_info *dom);
 
 struct sss_domain_info*
 sss_get_domain_by_sid_ldap_fallback(struct sss_domain_info *domain,
-- 
2.4.11