diff options
Diffstat (limited to '')
-rw-r--r-- | common/mbox-util.c | 39 | ||||
-rw-r--r-- | common/mbox-util.h | 1 | ||||
-rw-r--r-- | common/t-mbox-util.c | 55 |
3 files changed, 93 insertions, 2 deletions
diff --git a/common/mbox-util.c b/common/mbox-util.c index c1f05b834..76255ba38 100644 --- a/common/mbox-util.c +++ b/common/mbox-util.c @@ -241,3 +241,42 @@ is_valid_user_id (const char *uid) return 1; } + + +/* Returns true if STRING is a valid domain name according to the LDH + * rule. */ +int +is_valid_domain_name (const char *string) +{ + static char const ldh_chars[] = + "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"; + const char *s; + + /* Note that we do not check the length limit of a label or the + * entire name */ + + for (s=string; *s; s++) + if (*s == '.') + { + if (string == s) + return 0; /* Dot at the start of the string. */ + /* (may also be at the end like in ".") */ + if (s[1] == '.') + return 0; /* No - double dot. */ + } + else if (!strchr (ldh_chars, *s)) + return 0; + else if (*s == '-') + { + if (string == s) + return 0; /* Leading hyphen. */ + if (s[-1] == '.') + return 0; /* Hyphen at begin of a label. */ + if (s[1] == '.') + return 0; /* Hyphen at start of a label. */ + if (!s[1]) + return 0; /* Trailing hyphen. */ + } + + return !!*string; +} diff --git a/common/mbox-util.h b/common/mbox-util.h index bce003f31..7355ceef5 100644 --- a/common/mbox-util.h +++ b/common/mbox-util.h @@ -24,6 +24,7 @@ int is_valid_mailbox (const char *name); int is_valid_mailbox_mem (const void *buffer, size_t length); char *mailbox_from_userid (const char *userid); int is_valid_user_id (const char *uid); +int is_valid_domain_name (const char *string); #endif /*GNUPG_COMMON_MBOX_UTIL_H*/ diff --git a/common/t-mbox-util.c b/common/t-mbox-util.c index 979d4b37c..fb1ac12e0 100644 --- a/common/t-mbox-util.c +++ b/common/t-mbox-util.c @@ -33,7 +33,7 @@ static void -run_test (void) +run_mbox_test (void) { static struct { @@ -93,13 +93,64 @@ run_test (void) } +static void +run_dns_test (void) +{ + static struct + { + const char *name; + int valid; + } testtbl[] = + { + { "", 0 }, + { ".", 0 }, + { "-", 0 }, + { "a", 1 }, + { "ab", 1 }, + { "a.b", 1 }, + { "a.b.", 1 }, + { ".a.b.", 0 }, + { ".a.b", 0 }, + { "-a.b", 0 }, + { "a-.b", 0 }, + { "a.-b", 0 }, + { "a.b-", 0 }, + { "a.b-.", 0 }, + { "a..b", 0 }, + { "ab.c", 1 }, + { "a-b.c", 1 }, + { "a-b-.c", 0 }, + { "-a-b.c", 0 }, + { "example.org", 1 }, + { "x.example.org", 1 }, + { "xy.example.org", 1 }, + { "Xy.example.org", 1 }, + { "-Xy.example.org", 0 }, + { "Xy.example-.org", 0 }, + { "foo.example.org..", 0 }, + { "foo.example.org.", 1 }, + { ".foo.example.org.", 0 }, + { "..foo.example.org.", 0 }, + { NULL, 0 } + }; + int idx; + + for (idx=0; testtbl[idx].name; idx++) + { + if (is_valid_domain_name (testtbl[idx].name) != testtbl[idx].valid) + fail (idx); + } +} + + int main (int argc, char **argv) { (void)argc; (void)argv; - run_test (); + run_mbox_test (); + run_dns_test (); return 0; } |