diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-08-18 04:44:24 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-08-18 04:44:24 +0000 |
commit | 86980cf6e232dd30de59bec2c32e0a85c81eb1d2 (patch) | |
tree | 68b23af0d396f5b063638b3e9c4f50a33a4473b0 /src/or | |
parent | adccf3aea6ef7d33bd5cfcd322d5168b2c12415b (diff) | |
download | tor-86980cf6e232dd30de59bec2c32e0a85c81eb1d2.tar tor-86980cf6e232dd30de59bec2c32e0a85c81eb1d2.tar.gz |
Functions to test nickname validity
svn:r2269
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/config.c | 4 | ||||
-rw-r--r-- | src/or/or.h | 2 | ||||
-rw-r--r-- | src/or/router.c | 20 | ||||
-rw-r--r-- | src/or/routerparse.c | 9 | ||||
-rw-r--r-- | src/or/test.c | 20 |
5 files changed, 48 insertions, 7 deletions
diff --git a/src/or/config.c b/src/or/config.c index faacdb238..6d09b7764 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -735,6 +735,10 @@ int getconfig(int argc, char **argv, or_options_t *options) { log_fn(LOG_WARN, "Nickname '%s' contains illegal characters.", options->Nickname); result = -1; } + if (strlen(options->Nickname) == 0) { + log_fn(LOG_WARN, "Nickname must have at least one character"); + result = -1; + } if (strlen(options->Nickname) > MAX_NICKNAME_LEN) { log_fn(LOG_WARN, "Nickname '%s' has more than %d characters.", options->Nickname, MAX_NICKNAME_LEN); diff --git a/src/or/or.h b/src/or/or.h index fd5b9ccbd..cc2457c68 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -1386,6 +1386,8 @@ int router_is_me(routerinfo_t *router); int router_rebuild_descriptor(void); int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router, crypto_pk_env_t *ident_key); +int is_legal_nickname(const char *s); +int is_legal_nickname_or_hexdigest(const char *s); /********************************* routerlist.c ***************************/ diff --git a/src/or/router.c b/src/or/router.c index 440e40428..2b89bef8d 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -774,6 +774,26 @@ int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router, return written+1; } +int is_legal_nickname(const char *s) +{ + size_t len; + tor_assert(s); + len = strlen(s); + return len > 0 && len <= MAX_NICKNAME_LEN && + strspn(s,LEGAL_NICKNAME_CHARACTERS)==len; +} +int is_legal_nickname_or_hexdigest(const char *s) +{ + size_t len; + tor_assert(s); + if (*s!='$') + return is_legal_nickname(s); + + len = strlen(s); + return len == HEX_DIGEST_LEN+1 && strspn(s+1,HEX_CHARACTERS)==len-1; +} + + /* Local Variables: mode:c diff --git a/src/or/routerparse.c b/src/or/routerparse.c index f3e6a53f8..f434dcd66 100644 --- a/src/or/routerparse.c +++ b/src/or/routerparse.c @@ -681,13 +681,8 @@ routerinfo_t *router_parse_entry_from_string(const char *s, if (tok->n_args == 2 || tok->n_args == 5 || tok->n_args == 6) { router->nickname = tor_strdup(tok->args[0]); - if (strlen(router->nickname) > MAX_NICKNAME_LEN) { - log_fn(LOG_WARN,"Router nickname too long."); - goto err; - } - if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) != - strlen(router->nickname)) { - log_fn(LOG_WARN, "Router nickname contains illegal characters."); + if (!is_legal_nickname(router->nickname)) { + log_fn(LOG_WARN,"Router nickname is invalid"); goto err; } router->address = tor_strdup(tok->args[1]); diff --git a/src/or/test.c b/src/or/test.c index 80167bc57..d9badb746 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -758,6 +758,26 @@ test_dir_format() test_assert(! crypto_pk_generate_key(pk2)); test_assert(! crypto_pk_generate_key(pk3)); + test_assert( is_legal_nickname("a")); + test_assert(!is_legal_nickname("")); + test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */ + test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */ + test_assert(!is_legal_nickname("hyphen-")); /* bad char */ + test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */ + test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA")); + /* valid */ + test_assert( is_legal_nickname_or_hexdigest( + "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA")); + /* too short */ + test_assert(!is_legal_nickname_or_hexdigest( + "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + /* illegal char */ + test_assert(!is_legal_nickname_or_hexdigest( + "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + test_assert(is_legal_nickname_or_hexdigest("xyzzy")); + test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs")); + test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst")); + get_platform_str(platform, sizeof(platform)); r1.address = "testaddr1.foo.bar"; |