dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0055-utils-add-is_email_from_domain.patch

b2d430
From 3f7b69c3af7b810443a94a1004d41b87e5ca79d8 Mon Sep 17 00:00:00 2001
b2d430
From: Sumit Bose <sbose@redhat.com>
b2d430
Date: Mon, 20 Jun 2016 16:11:11 +0200
b2d430
Subject: [PATCH 55/62] utils: add is_email_from_domain()
b2d430
b2d430
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
b2d430
(cherry picked from commit 04d4c4d45f3942a813b7f772737f801f877f4e64)
b2d430
---
b2d430
 src/tests/cmocka/test_utils.c | 21 +++++++++++++++++++++
b2d430
 src/util/domain_info_utils.c  | 27 +++++++++++++++++++++++++++
b2d430
 src/util/util.h               |  1 +
b2d430
 3 files changed, 49 insertions(+)
b2d430
b2d430
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
b2d430
index 5349accc5c6b55f7d725bfdeaf318b90289880dd..09273692968b544aed88de00c747e85ff0b6695c 100644
b2d430
--- a/src/tests/cmocka/test_utils.c
b2d430
+++ b/src/tests/cmocka/test_utils.c
b2d430
@@ -1842,6 +1842,25 @@ static void test_sss_get_domain_mappings_content(void **state)
b2d430
      * capaths might not be as expected. */
b2d430
 }
b2d430
 
b2d430
+static void test_is_email_from_domain(void **state)
b2d430
+{
b2d430
+    struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
b2d430
+                                                      struct dom_list_test_ctx);
b2d430
+    struct sss_domain_info *d;
b2d430
+
b2d430
+    d = find_domain_by_name(test_ctx->dom_list, "name_0.dom", false);
b2d430
+    assert_non_null(d);
b2d430
+
b2d430
+    assert_false(is_email_from_domain(NULL, NULL));
b2d430
+    assert_false(is_email_from_domain("hello", NULL));
b2d430
+    assert_false(is_email_from_domain(NULL, d));
b2d430
+    assert_false(is_email_from_domain("hello", d));
b2d430
+    assert_false(is_email_from_domain("hello@hello", d));
b2d430
+
b2d430
+    assert_true(is_email_from_domain("hello@name_0.dom", d));
b2d430
+    assert_true(is_email_from_domain("hello@NaMe_0.DoM", d));
b2d430
+}
b2d430
+
b2d430
 int main(int argc, const char *argv[])
b2d430
 {
b2d430
     poptContext pc;
b2d430
@@ -1870,6 +1889,8 @@ int main(int argc, const char *argv[])
b2d430
                                         setup_dom_list, teardown_dom_list),
b2d430
         cmocka_unit_test_setup_teardown(test_find_domain_by_name_disabled,
b2d430
                                         setup_dom_list, teardown_dom_list),
b2d430
+        cmocka_unit_test_setup_teardown(test_is_email_from_domain,
b2d430
+                                        setup_dom_list, teardown_dom_list),
b2d430
 
b2d430
         cmocka_unit_test_setup_teardown(test_sss_names_init,
b2d430
                                         confdb_test_setup,
b2d430
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
b2d430
index 587a6b993d2bd70662df8e0b0d5963fa00c84cf8..0feda148bd44b9cefc43c094ddc5a72820412322 100644
b2d430
--- a/src/util/domain_info_utils.c
b2d430
+++ b/src/util/domain_info_utils.c
b2d430
@@ -824,3 +824,30 @@ void sss_domain_set_state(struct sss_domain_info *dom,
b2d430
 {
b2d430
     dom->state = state;
b2d430
 }
b2d430
+
b2d430
+bool is_email_from_domain(const char *email, struct sss_domain_info *dom)
b2d430
+{
b2d430
+    const char *p;
b2d430
+
b2d430
+    if (email == NULL || dom == NULL) {
b2d430
+        return false;
b2d430
+    }
b2d430
+
b2d430
+    p = strchr(email, '@');
b2d430
+    if (p == NULL) {
b2d430
+        DEBUG(SSSDBG_TRACE_ALL,
b2d430
+              "Input [%s] does not look like an email address.\n", email);
b2d430
+        return false;
b2d430
+    }
b2d430
+
b2d430
+    if (strcasecmp(p+1, dom->name) == 0) {
b2d430
+        DEBUG(SSSDBG_TRACE_ALL, "Email [%s] is from domain [%s].\n", email,
b2d430
+                                                                     dom->name);
b2d430
+        return true;
b2d430
+    }
b2d430
+
b2d430
+    DEBUG(SSSDBG_TRACE_ALL, "Email [%s] is not from domain [%s].\n", email,
b2d430
+                                                                     dom->name);
b2d430
+
b2d430
+    return false;
b2d430
+}
b2d430
diff --git a/src/util/util.h b/src/util/util.h
b2d430
index 122be90b967fb7793adaff95f3754d7a199fcf48..4449315f8b1a79ec915bc340b46188c440a27fa3 100644
b2d430
--- a/src/util/util.h
b2d430
+++ b/src/util/util.h
b2d430
@@ -514,6 +514,7 @@ struct sss_domain_info *find_domain_by_sid(struct sss_domain_info *domain,
b2d430
 enum sss_domain_state sss_domain_get_state(struct sss_domain_info *dom);
b2d430
 void sss_domain_set_state(struct sss_domain_info *dom,
b2d430
                           enum sss_domain_state state);
b2d430
+bool is_email_from_domain(const char *email, struct sss_domain_info *dom);
b2d430
 
b2d430
 struct sss_domain_info*
b2d430
 sss_get_domain_by_sid_ldap_fallback(struct sss_domain_info *domain,
b2d430
-- 
b2d430
2.4.11
b2d430