diff options
author | Pavel Březina <pbrezina@redhat.com> | 2013-08-03 01:02:02 +0200 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2013-09-05 11:14:17 +0200 |
commit | abac0f3ec2baa597d8e21a8744de6f33506299ce (patch) | |
tree | f556463763af11eb3d19251e19b3729a4ec82680 /src | |
parent | 002113460c411cab449c0d7dea824cea1a6c68e7 (diff) | |
download | sssd-abac0f3ec2baa597d8e21a8744de6f33506299ce.tar.gz sssd-abac0f3ec2baa597d8e21a8744de6f33506299ce.tar.bz2 sssd-abac0f3ec2baa597d8e21a8744de6f33506299ce.zip |
utils: add is_host_in_domain()
Diffstat (limited to 'src')
-rw-r--r-- | src/tests/util-tests.c | 28 | ||||
-rw-r--r-- | src/util/util.c | 15 | ||||
-rw-r--r-- | src/util/util.h | 2 |
3 files changed, 45 insertions, 0 deletions
diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c index 9af12081..fd3dfb9b 100644 --- a/src/tests/util-tests.c +++ b/src/tests/util-tests.c @@ -808,6 +808,33 @@ START_TEST(test_split_on_separator) } END_TEST +START_TEST(test_is_host_in_domain) +{ + struct { + const char *host; + const char *domain; + bool expected; + } data[] = {{"example.com", "example.com", true}, + {"client.example.com", "example.com", true}, + {"client.child.example.com", "example.com", true}, + {"example.com", "child.example.com", false}, + {"client.example.com", "child.example.com", false}, + {"client.child.example.com", "child.example.com", true}, + {"my.com", "example.com", false}, + {"myexample.com", "example.com", false}, + {NULL, NULL, false}}; + bool ret; + int i; + + for (i = 0; data[i].host != NULL; i++) { + ret = is_host_in_domain(data[i].host, data[i].domain); + fail_if(ret != data[i].expected, "Host: %s, Domain: %s, Expected: %d, " + "Got: %d\n", data[i].host, data[i].domain, + data[i].expected, ret); + } +} +END_TEST + Suite *util_suite(void) { Suite *s = suite_create("util"); @@ -824,6 +851,7 @@ Suite *util_suite(void) tcase_add_test (tc_util, test_add_string_to_list); tcase_add_test (tc_util, test_string_in_list); tcase_add_test (tc_util, test_split_on_separator); + tcase_add_test (tc_util, test_is_host_in_domain); tcase_set_timeout(tc_util, 60); TCase *tc_utf8 = tcase_create("utf8"); diff --git a/src/util/util.c b/src/util/util.c index 48029676..fb3bed14 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -724,3 +724,18 @@ int domain_to_basedn(TALLOC_CTX *memctx, const char *domain, char **basedn) *basedn = dn; return EOK; } + +bool is_host_in_domain(const char *host, const char *domain) +{ + int diff = strlen(host) - strlen(domain); + + if (diff == 0 && strcmp(host, domain) == 0) { + return true; + } + + if (diff > 0 && strcmp(host + diff, domain) == 0 && host[diff - 1] == '.') { + return true; + } + + return false; +} diff --git a/src/util/util.h b/src/util/util.h index 516edc81..c2c31f5d 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -506,6 +506,8 @@ void safezero(void *data, size_t size); int domain_to_basedn(TALLOC_CTX *memctx, const char *domain, char **basedn); +bool is_host_in_domain(const char *host, const char *domain); + /* from nscd.c */ enum nscd_db { NSCD_DB_PASSWD, |