diff options
author | Werner Koch <[email protected]> | 2018-07-27 09:56:06 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2018-07-29 16:39:32 +0000 |
commit | 4f59187a17f16d559e37a375501a0add1ca7eee8 (patch) | |
tree | 85f439357d443b3c1a27254a251e3f73690a23e0 /common/mbox-util.c | |
parent | scd: Add support for Trustica Cryptoucan. (diff) | |
download | gnupg-4f59187a17f16d559e37a375501a0add1ca7eee8.tar.gz gnupg-4f59187a17f16d559e37a375501a0add1ca7eee8.zip |
common: New function to validate domain names.
* common/mbox-util.c (is_valid_domain_name): New.
* common/t-mbox-util.c (run_dns_test): New test.
Signed-off-by: Werner Koch <[email protected]>
(cherry picked from commit ddee9f9409fb5a089883eab0fadef7b9b7e61e72)
Diffstat (limited to 'common/mbox-util.c')
-rw-r--r-- | common/mbox-util.c | 39 |
1 files changed, 39 insertions, 0 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; +} |